Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def _get_t5_prompt_embeds(
untruncated_ids = tokenizer(prompt, padding="longest", return_tensors="pt").input_ids

if untruncated_ids.shape[-1] >= text_input_ids.shape[-1] and not torch.equal(text_input_ids, untruncated_ids):
removed_text = tokenizer.batch_decode(untruncated_ids[:, tokenizer.model_max_length - 1 : -1])
removed_text = tokenizer.batch_decode(untruncated_ids[:, max_sequence_length - 1 : -1])
logger.warning(
f"The following part of your input was truncated because `max_sequence_length` is set to "
f" {max_sequence_length} tokens: {removed_text}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ def _get_t5_prompt_embeds(
untruncated_ids = self.tokenizer_3(prompt, padding="longest", return_tensors="pt").input_ids

if untruncated_ids.shape[-1] >= text_input_ids.shape[-1] and not torch.equal(text_input_ids, untruncated_ids):
removed_text = self.tokenizer_3.batch_decode(untruncated_ids[:, self.tokenizer_max_length - 1 : -1])
removed_text = self.tokenizer_3.batch_decode(untruncated_ids[:, max_sequence_length - 1 : -1])
logger.warning(
"The following part of your input was truncated because `max_sequence_length` is set to "
f" {max_sequence_length} tokens: {removed_text}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ def _get_t5_prompt_embeds(
untruncated_ids = self.tokenizer_3(prompt, padding="longest", return_tensors="pt").input_ids

if untruncated_ids.shape[-1] >= text_input_ids.shape[-1] and not torch.equal(text_input_ids, untruncated_ids):
removed_text = self.tokenizer_3.batch_decode(untruncated_ids[:, self.tokenizer_max_length - 1 : -1])
removed_text = self.tokenizer_3.batch_decode(untruncated_ids[:, max_sequence_length - 1 : -1])
logger.warning(
"The following part of your input was truncated because `max_sequence_length` is set to "
f" {max_sequence_length} tokens: {removed_text}"
Expand Down
2 changes: 1 addition & 1 deletion src/diffusers/pipelines/flux/pipeline_flux.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ def _get_t5_prompt_embeds(
untruncated_ids = self.tokenizer_2(prompt, padding="longest", return_tensors="pt").input_ids

if untruncated_ids.shape[-1] >= text_input_ids.shape[-1] and not torch.equal(text_input_ids, untruncated_ids):
removed_text = self.tokenizer_2.batch_decode(untruncated_ids[:, self.tokenizer_max_length - 1 : -1])
removed_text = self.tokenizer_2.batch_decode(untruncated_ids[:, max_sequence_length - 1 : -1])
logger.warning(
"The following part of your input was truncated because `max_sequence_length` is set to "
f" {max_sequence_length} tokens: {removed_text}"
Expand Down
2 changes: 1 addition & 1 deletion src/diffusers/pipelines/flux/pipeline_flux_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ def _get_t5_prompt_embeds(
untruncated_ids = self.tokenizer_2(prompt, padding="longest", return_tensors="pt").input_ids

if untruncated_ids.shape[-1] >= text_input_ids.shape[-1] and not torch.equal(text_input_ids, untruncated_ids):
removed_text = self.tokenizer_2.batch_decode(untruncated_ids[:, self.tokenizer_max_length - 1 : -1])
removed_text = self.tokenizer_2.batch_decode(untruncated_ids[:, max_sequence_length - 1 : -1])
logger.warning(
"The following part of your input was truncated because `max_sequence_length` is set to "
f" {max_sequence_length} tokens: {removed_text}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ def _get_t5_prompt_embeds(
untruncated_ids = self.tokenizer_2(prompt, padding="longest", return_tensors="pt").input_ids

if untruncated_ids.shape[-1] >= text_input_ids.shape[-1] and not torch.equal(text_input_ids, untruncated_ids):
removed_text = self.tokenizer_2.batch_decode(untruncated_ids[:, self.tokenizer_max_length - 1 : -1])
removed_text = self.tokenizer_2.batch_decode(untruncated_ids[:, max_sequence_length - 1 : -1])
logger.warning(
"The following part of your input was truncated because `max_sequence_length` is set to "
f" {max_sequence_length} tokens: {removed_text}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ def _get_t5_prompt_embeds(
untruncated_ids = self.tokenizer_2(prompt, padding="longest", return_tensors="pt").input_ids

if untruncated_ids.shape[-1] >= text_input_ids.shape[-1] and not torch.equal(text_input_ids, untruncated_ids):
removed_text = self.tokenizer_2.batch_decode(untruncated_ids[:, self.tokenizer_max_length - 1 : -1])
removed_text = self.tokenizer_2.batch_decode(untruncated_ids[:, max_sequence_length - 1 : -1])
logger.warning(
"The following part of your input was truncated because `max_sequence_length` is set to "
f" {max_sequence_length} tokens: {removed_text}"
Expand Down
2 changes: 1 addition & 1 deletion src/diffusers/pipelines/flux/pipeline_flux_controlnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ def _get_t5_prompt_embeds(
untruncated_ids = self.tokenizer_2(prompt, padding="longest", return_tensors="pt").input_ids

if untruncated_ids.shape[-1] >= text_input_ids.shape[-1] and not torch.equal(text_input_ids, untruncated_ids):
removed_text = self.tokenizer_2.batch_decode(untruncated_ids[:, self.tokenizer_max_length - 1 : -1])
removed_text = self.tokenizer_2.batch_decode(untruncated_ids[:, max_sequence_length - 1 : -1])
logger.warning(
"The following part of your input was truncated because `max_sequence_length` is set to "
f" {max_sequence_length} tokens: {removed_text}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ def _get_t5_prompt_embeds(
untruncated_ids = self.tokenizer_2(prompt, padding="longest", return_tensors="pt").input_ids

if untruncated_ids.shape[-1] >= text_input_ids.shape[-1] and not torch.equal(text_input_ids, untruncated_ids):
removed_text = self.tokenizer_2.batch_decode(untruncated_ids[:, self.tokenizer_max_length - 1 : -1])
removed_text = self.tokenizer_2.batch_decode(untruncated_ids[:, max_sequence_length - 1 : -1])
logger.warning(
"The following part of your input was truncated because `max_sequence_length` is set to "
f" {max_sequence_length} tokens: {removed_text}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ def _get_t5_prompt_embeds(
untruncated_ids = self.tokenizer_2(prompt, padding="longest", return_tensors="pt").input_ids

if untruncated_ids.shape[-1] >= text_input_ids.shape[-1] and not torch.equal(text_input_ids, untruncated_ids):
removed_text = self.tokenizer_2.batch_decode(untruncated_ids[:, self.tokenizer_max_length - 1 : -1])
removed_text = self.tokenizer_2.batch_decode(untruncated_ids[:, max_sequence_length - 1 : -1])
logger.warning(
"The following part of your input was truncated because `max_sequence_length` is set to "
f" {max_sequence_length} tokens: {removed_text}"
Expand Down
2 changes: 1 addition & 1 deletion src/diffusers/pipelines/flux/pipeline_flux_fill.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ def _get_t5_prompt_embeds(
untruncated_ids = self.tokenizer_2(prompt, padding="longest", return_tensors="pt").input_ids

if untruncated_ids.shape[-1] >= text_input_ids.shape[-1] and not torch.equal(text_input_ids, untruncated_ids):
removed_text = self.tokenizer_2.batch_decode(untruncated_ids[:, self.tokenizer_max_length - 1 : -1])
removed_text = self.tokenizer_2.batch_decode(untruncated_ids[:, max_sequence_length - 1 : -1])
logger.warning(
"The following part of your input was truncated because `max_sequence_length` is set to "
f" {max_sequence_length} tokens: {removed_text}"
Expand Down
2 changes: 1 addition & 1 deletion src/diffusers/pipelines/flux/pipeline_flux_img2img.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ def _get_t5_prompt_embeds(
untruncated_ids = self.tokenizer_2(prompt, padding="longest", return_tensors="pt").input_ids

if untruncated_ids.shape[-1] >= text_input_ids.shape[-1] and not torch.equal(text_input_ids, untruncated_ids):
removed_text = self.tokenizer_2.batch_decode(untruncated_ids[:, self.tokenizer_max_length - 1 : -1])
removed_text = self.tokenizer_2.batch_decode(untruncated_ids[:, max_sequence_length - 1 : -1])
logger.warning(
"The following part of your input was truncated because `max_sequence_length` is set to "
f" {max_sequence_length} tokens: {removed_text}"
Expand Down
2 changes: 1 addition & 1 deletion src/diffusers/pipelines/flux/pipeline_flux_inpaint.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ def _get_t5_prompt_embeds(
untruncated_ids = self.tokenizer_2(prompt, padding="longest", return_tensors="pt").input_ids

if untruncated_ids.shape[-1] >= text_input_ids.shape[-1] and not torch.equal(text_input_ids, untruncated_ids):
removed_text = self.tokenizer_2.batch_decode(untruncated_ids[:, self.tokenizer_max_length - 1 : -1])
removed_text = self.tokenizer_2.batch_decode(untruncated_ids[:, max_sequence_length - 1 : -1])
logger.warning(
"The following part of your input was truncated because `max_sequence_length` is set to "
f" {max_sequence_length} tokens: {removed_text}"
Expand Down
2 changes: 1 addition & 1 deletion src/diffusers/pipelines/flux/pipeline_flux_kontext.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ def _get_t5_prompt_embeds(
untruncated_ids = self.tokenizer_2(prompt, padding="longest", return_tensors="pt").input_ids

if untruncated_ids.shape[-1] >= text_input_ids.shape[-1] and not torch.equal(text_input_ids, untruncated_ids):
removed_text = self.tokenizer_2.batch_decode(untruncated_ids[:, self.tokenizer_max_length - 1 : -1])
removed_text = self.tokenizer_2.batch_decode(untruncated_ids[:, max_sequence_length - 1 : -1])
logger.warning(
"The following part of your input was truncated because `max_sequence_length` is set to "
f" {max_sequence_length} tokens: {removed_text}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ def _get_t5_prompt_embeds(
untruncated_ids = self.tokenizer_2(prompt, padding="longest", return_tensors="pt").input_ids

if untruncated_ids.shape[-1] >= text_input_ids.shape[-1] and not torch.equal(text_input_ids, untruncated_ids):
removed_text = self.tokenizer_2.batch_decode(untruncated_ids[:, self.tokenizer_max_length - 1 : -1])
removed_text = self.tokenizer_2.batch_decode(untruncated_ids[:, max_sequence_length - 1 : -1])
logger.warning(
"The following part of your input was truncated because `max_sequence_length` is set to "
f" {max_sequence_length} tokens: {removed_text}"
Expand Down
2 changes: 1 addition & 1 deletion src/diffusers/pipelines/flux/pipeline_flux_prior_redux.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ def _get_t5_prompt_embeds(
untruncated_ids = self.tokenizer_2(prompt, padding="longest", return_tensors="pt").input_ids

if untruncated_ids.shape[-1] >= text_input_ids.shape[-1] and not torch.equal(text_input_ids, untruncated_ids):
removed_text = self.tokenizer_2.batch_decode(untruncated_ids[:, self.tokenizer_max_length - 1 : -1])
removed_text = self.tokenizer_2.batch_decode(untruncated_ids[:, max_sequence_length - 1 : -1])
logger.warning(
"The following part of your input was truncated because `max_sequence_length` is set to "
f" {max_sequence_length} tokens: {removed_text}"
Expand Down
2 changes: 1 addition & 1 deletion src/diffusers/pipelines/pag/pipeline_pag_sd_3.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ def _get_t5_prompt_embeds(
untruncated_ids = self.tokenizer_3(prompt, padding="longest", return_tensors="pt").input_ids

if untruncated_ids.shape[-1] >= text_input_ids.shape[-1] and not torch.equal(text_input_ids, untruncated_ids):
removed_text = self.tokenizer_3.batch_decode(untruncated_ids[:, self.tokenizer_max_length - 1 : -1])
removed_text = self.tokenizer_3.batch_decode(untruncated_ids[:, max_sequence_length - 1 : -1])
logger.warning(
"The following part of your input was truncated because `max_sequence_length` is set to "
f" {max_sequence_length} tokens: {removed_text}"
Expand Down
2 changes: 1 addition & 1 deletion src/diffusers/pipelines/pag/pipeline_pag_sd_3_img2img.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ def _get_t5_prompt_embeds(
untruncated_ids = self.tokenizer_3(prompt, padding="longest", return_tensors="pt").input_ids

if untruncated_ids.shape[-1] >= text_input_ids.shape[-1] and not torch.equal(text_input_ids, untruncated_ids):
removed_text = self.tokenizer_3.batch_decode(untruncated_ids[:, self.tokenizer_max_length - 1 : -1])
removed_text = self.tokenizer_3.batch_decode(untruncated_ids[:, max_sequence_length - 1 : -1])
logger.warning(
"The following part of your input was truncated because `max_sequence_length` is set to "
f" {max_sequence_length} tokens: {removed_text}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ def _get_t5_prompt_embeds(
untruncated_ids = self.tokenizer_3(prompt, padding="longest", return_tensors="pt").input_ids

if untruncated_ids.shape[-1] >= text_input_ids.shape[-1] and not torch.equal(text_input_ids, untruncated_ids):
removed_text = self.tokenizer_3.batch_decode(untruncated_ids[:, self.tokenizer_max_length - 1 : -1])
removed_text = self.tokenizer_3.batch_decode(untruncated_ids[:, max_sequence_length - 1 : -1])
logger.warning(
"The following part of your input was truncated because `max_sequence_length` is set to "
f" {max_sequence_length} tokens: {removed_text}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ def _get_t5_prompt_embeds(
untruncated_ids = self.tokenizer_3(prompt, padding="longest", return_tensors="pt").input_ids

if untruncated_ids.shape[-1] >= text_input_ids.shape[-1] and not torch.equal(text_input_ids, untruncated_ids):
removed_text = self.tokenizer_3.batch_decode(untruncated_ids[:, self.tokenizer_max_length - 1 : -1])
removed_text = self.tokenizer_3.batch_decode(untruncated_ids[:, max_sequence_length - 1 : -1])
logger.warning(
"The following part of your input was truncated because `max_sequence_length` is set to "
f" {max_sequence_length} tokens: {removed_text}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ def _get_t5_prompt_embeds(
untruncated_ids = self.tokenizer_3(prompt, padding="longest", return_tensors="pt").input_ids

if untruncated_ids.shape[-1] >= text_input_ids.shape[-1] and not torch.equal(text_input_ids, untruncated_ids):
removed_text = self.tokenizer_3.batch_decode(untruncated_ids[:, self.tokenizer_max_length - 1 : -1])
removed_text = self.tokenizer_3.batch_decode(untruncated_ids[:, max_sequence_length - 1 : -1])
logger.warning(
"The following part of your input was truncated because `max_sequence_length` is set to "
f" {max_sequence_length} tokens: {removed_text}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ def _get_t5_prompt_embeds(
untruncated_ids = self.tokenizer_2(prompt, padding="longest", return_tensors="pt").input_ids

if untruncated_ids.shape[-1] >= text_input_ids.shape[-1] and not torch.equal(text_input_ids, untruncated_ids):
removed_text = self.tokenizer_2.batch_decode(untruncated_ids[:, self.tokenizer_max_length - 1 : -1])
removed_text = self.tokenizer_2.batch_decode(untruncated_ids[:, max_sequence_length - 1 : -1])
logger.warning(
"The following part of your input was truncated because `max_sequence_length` is set to "
f" {max_sequence_length} tokens: {removed_text}"
Expand Down
33 changes: 33 additions & 0 deletions tests/pipelines/flux/test_pipeline_flux.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import gc
import logging
import os

import numpy as np
Expand All @@ -18,6 +19,7 @@

from ...models.testing_utils.lora import check_if_lora_correctly_set
from ...testing_utils import (
CaptureLogger,
Expectations,
assert_tensors_close,
backend_empty_cache,
Expand Down Expand Up @@ -202,6 +204,37 @@ def test_flux_negative_embeds_shape_check(self):

pipe(**base_inputs, true_cfg_scale=1.0, generator=torch.manual_seed(0))

def test_t5_prompt_truncation_warning(self):
components = self.get_dummy_components()
pipe = self.pipeline_class(**components).to(torch_device)
pipe.set_progress_bar_config(disable=None)

max_sequence_length = 8
prompt = "one two three four five six seven eight nine ten"
untruncated_ids = pipe.tokenizer_2(prompt, padding="longest", return_tensors="pt").input_ids
expected_removed_text = pipe.tokenizer_2.batch_decode(untruncated_ids[:, max_sequence_length - 1 : -1])
assert expected_removed_text != [""]
assert "nine ten" in expected_removed_text[0]

logger = logging.getLogger("diffusers.pipelines.flux.pipeline_flux")
prev_level = logger.level
logger.setLevel(logging.WARNING)
try:
with CaptureLogger(logger) as cap_logger:
pipe.encode_prompt(
prompt="forest",
prompt_2=prompt,
max_sequence_length=max_sequence_length,
device=torch_device,
)
finally:
logger.setLevel(prev_level)

assert (
f"The following part of your input was truncated because `max_sequence_length` is set to {max_sequence_length} tokens: {expected_removed_text}"
in cap_logger.out
)


class TestFluxPipelineIPAdapter(FluxPipelineTesterConfig, FluxIPAdapterTesterMixin):
"""IP-Adapter tests for the Flux pipeline."""
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import gc
import logging

import numpy as np
import pytest
Expand All @@ -13,9 +14,13 @@
)

from diffusers import AutoencoderKL, FlowMatchEulerDiscreteScheduler, SD3Transformer2DModel, StableDiffusion3Pipeline
from diffusers.modular_pipelines.stable_diffusion_3.encoders import (
_get_t5_prompt_embeds as modular_get_t5_prompt_embeds,
)

from ...models.testing_utils.lora import check_if_lora_correctly_set
from ...testing_utils import (
CaptureLogger,
assert_tensors_close,
backend_empty_cache,
numpy_cosine_similarity_distance,
Expand Down Expand Up @@ -172,6 +177,54 @@ def test_skip_guidance_layers(self):
assert not torch.allclose(output_full, output_skip, atol=1e-5), "Outputs should differ when layers are skipped"
assert output_full.shape == output_skip.shape, "Outputs should have the same shape"

def test_t5_prompt_truncation_warning(self):
components = self.get_dummy_components()
pipe = self.pipeline_class(**components).to(torch_device)
pipe.set_progress_bar_config(disable=None)

max_sequence_length = 8
prompt = "one two three four five six seven eight nine ten"
untruncated_ids = pipe.tokenizer_3(prompt, padding="longest", return_tensors="pt").input_ids
expected_removed_text = pipe.tokenizer_3.batch_decode(untruncated_ids[:, max_sequence_length - 1 : -1])
assert expected_removed_text != [""]
assert "nine ten" in expected_removed_text[0]

expected_warning = f"The following part of your input was truncated because `max_sequence_length` is set to {max_sequence_length} tokens: {expected_removed_text}"

logger = logging.getLogger("diffusers.pipelines.stable_diffusion_3.pipeline_stable_diffusion_3")
prev_level = logger.level
logger.setLevel(logging.WARNING)
try:
with CaptureLogger(logger) as cap_logger:
pipe.encode_prompt(
prompt="forest",
prompt_2="forest",
prompt_3=prompt,
max_sequence_length=max_sequence_length,
device=torch_device,
)
finally:
logger.setLevel(prev_level)

assert expected_warning in cap_logger.out

modular_logger = logging.getLogger("diffusers.modular_pipelines.stable_diffusion_3.encoders")
prev_modular_level = modular_logger.level
modular_logger.setLevel(logging.WARNING)
try:
with CaptureLogger(modular_logger) as modular_cap_logger:
modular_get_t5_prompt_embeds(
pipe.text_encoder_3,
pipe.tokenizer_3,
prompt=prompt,
max_sequence_length=max_sequence_length,
device=torch_device,
)
finally:
modular_logger.setLevel(prev_modular_level)

assert expected_warning in modular_cap_logger.out


class TestStableDiffusion3PipelineLoRA(StableDiffusion3PipelineTesterConfig, LoraTesterMixin):
@pytest.mark.parametrize("weight_name", ["lora_diffusers_format.safetensors", "lora_peft_format.safetensors"])
Expand Down