diffusers 0.34.0__py3-none-any.whl → 0.35.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- diffusers/__init__.py +98 -1
- diffusers/callbacks.py +35 -0
- diffusers/commands/custom_blocks.py +134 -0
- diffusers/commands/diffusers_cli.py +2 -0
- diffusers/commands/fp16_safetensors.py +1 -1
- diffusers/configuration_utils.py +11 -2
- diffusers/dependency_versions_table.py +3 -3
- diffusers/guiders/__init__.py +41 -0
- diffusers/guiders/adaptive_projected_guidance.py +188 -0
- diffusers/guiders/auto_guidance.py +190 -0
- diffusers/guiders/classifier_free_guidance.py +141 -0
- diffusers/guiders/classifier_free_zero_star_guidance.py +152 -0
- diffusers/guiders/frequency_decoupled_guidance.py +327 -0
- diffusers/guiders/guider_utils.py +309 -0
- diffusers/guiders/perturbed_attention_guidance.py +271 -0
- diffusers/guiders/skip_layer_guidance.py +262 -0
- diffusers/guiders/smoothed_energy_guidance.py +251 -0
- diffusers/guiders/tangential_classifier_free_guidance.py +143 -0
- diffusers/hooks/__init__.py +17 -0
- diffusers/hooks/_common.py +56 -0
- diffusers/hooks/_helpers.py +293 -0
- diffusers/hooks/faster_cache.py +7 -6
- diffusers/hooks/first_block_cache.py +259 -0
- diffusers/hooks/group_offloading.py +292 -286
- diffusers/hooks/hooks.py +56 -1
- diffusers/hooks/layer_skip.py +263 -0
- diffusers/hooks/layerwise_casting.py +2 -7
- diffusers/hooks/pyramid_attention_broadcast.py +14 -11
- diffusers/hooks/smoothed_energy_guidance_utils.py +167 -0
- diffusers/hooks/utils.py +43 -0
- diffusers/loaders/__init__.py +6 -0
- diffusers/loaders/ip_adapter.py +255 -4
- diffusers/loaders/lora_base.py +63 -30
- diffusers/loaders/lora_conversion_utils.py +434 -53
- diffusers/loaders/lora_pipeline.py +834 -37
- diffusers/loaders/peft.py +28 -5
- diffusers/loaders/single_file_model.py +44 -11
- diffusers/loaders/single_file_utils.py +170 -2
- diffusers/loaders/transformer_flux.py +9 -10
- diffusers/loaders/transformer_sd3.py +6 -1
- diffusers/loaders/unet.py +22 -5
- diffusers/loaders/unet_loader_utils.py +5 -2
- diffusers/models/__init__.py +8 -0
- diffusers/models/attention.py +484 -3
- diffusers/models/attention_dispatch.py +1218 -0
- diffusers/models/attention_processor.py +105 -663
- diffusers/models/auto_model.py +2 -2
- diffusers/models/autoencoders/__init__.py +1 -0
- diffusers/models/autoencoders/autoencoder_dc.py +14 -1
- diffusers/models/autoencoders/autoencoder_kl.py +1 -1
- diffusers/models/autoencoders/autoencoder_kl_cosmos.py +3 -1
- diffusers/models/autoencoders/autoencoder_kl_qwenimage.py +1070 -0
- diffusers/models/autoencoders/autoencoder_kl_wan.py +370 -40
- diffusers/models/cache_utils.py +31 -9
- diffusers/models/controlnets/controlnet_flux.py +5 -5
- diffusers/models/controlnets/controlnet_union.py +4 -4
- diffusers/models/embeddings.py +26 -34
- diffusers/models/model_loading_utils.py +233 -1
- diffusers/models/modeling_flax_utils.py +1 -2
- diffusers/models/modeling_utils.py +159 -94
- diffusers/models/transformers/__init__.py +2 -0
- diffusers/models/transformers/transformer_chroma.py +16 -117
- diffusers/models/transformers/transformer_cogview4.py +36 -2
- diffusers/models/transformers/transformer_cosmos.py +11 -4
- diffusers/models/transformers/transformer_flux.py +372 -132
- diffusers/models/transformers/transformer_hunyuan_video.py +6 -0
- diffusers/models/transformers/transformer_ltx.py +104 -23
- diffusers/models/transformers/transformer_qwenimage.py +645 -0
- diffusers/models/transformers/transformer_skyreels_v2.py +607 -0
- diffusers/models/transformers/transformer_wan.py +298 -85
- diffusers/models/transformers/transformer_wan_vace.py +15 -21
- diffusers/models/unets/unet_2d_condition.py +2 -1
- diffusers/modular_pipelines/__init__.py +83 -0
- diffusers/modular_pipelines/components_manager.py +1068 -0
- diffusers/modular_pipelines/flux/__init__.py +66 -0
- diffusers/modular_pipelines/flux/before_denoise.py +689 -0
- diffusers/modular_pipelines/flux/decoders.py +109 -0
- diffusers/modular_pipelines/flux/denoise.py +227 -0
- diffusers/modular_pipelines/flux/encoders.py +412 -0
- diffusers/modular_pipelines/flux/modular_blocks.py +181 -0
- diffusers/modular_pipelines/flux/modular_pipeline.py +59 -0
- diffusers/modular_pipelines/modular_pipeline.py +2446 -0
- diffusers/modular_pipelines/modular_pipeline_utils.py +672 -0
- diffusers/modular_pipelines/node_utils.py +665 -0
- diffusers/modular_pipelines/stable_diffusion_xl/__init__.py +77 -0
- diffusers/modular_pipelines/stable_diffusion_xl/before_denoise.py +1874 -0
- diffusers/modular_pipelines/stable_diffusion_xl/decoders.py +208 -0
- diffusers/modular_pipelines/stable_diffusion_xl/denoise.py +771 -0
- diffusers/modular_pipelines/stable_diffusion_xl/encoders.py +887 -0
- diffusers/modular_pipelines/stable_diffusion_xl/modular_blocks.py +380 -0
- diffusers/modular_pipelines/stable_diffusion_xl/modular_pipeline.py +365 -0
- diffusers/modular_pipelines/wan/__init__.py +66 -0
- diffusers/modular_pipelines/wan/before_denoise.py +365 -0
- diffusers/modular_pipelines/wan/decoders.py +105 -0
- diffusers/modular_pipelines/wan/denoise.py +261 -0
- diffusers/modular_pipelines/wan/encoders.py +242 -0
- diffusers/modular_pipelines/wan/modular_blocks.py +144 -0
- diffusers/modular_pipelines/wan/modular_pipeline.py +90 -0
- diffusers/pipelines/__init__.py +31 -0
- diffusers/pipelines/audioldm2/pipeline_audioldm2.py +2 -3
- diffusers/pipelines/auto_pipeline.py +17 -13
- diffusers/pipelines/chroma/pipeline_chroma.py +5 -5
- diffusers/pipelines/chroma/pipeline_chroma_img2img.py +5 -5
- diffusers/pipelines/cogvideo/pipeline_cogvideox.py +9 -8
- diffusers/pipelines/cogvideo/pipeline_cogvideox_fun_control.py +9 -8
- diffusers/pipelines/cogvideo/pipeline_cogvideox_image2video.py +10 -9
- diffusers/pipelines/cogvideo/pipeline_cogvideox_video2video.py +9 -8
- diffusers/pipelines/cogview4/pipeline_cogview4.py +16 -15
- diffusers/pipelines/controlnet/pipeline_controlnet_blip_diffusion.py +3 -2
- diffusers/pipelines/controlnet/pipeline_controlnet_union_inpaint_sd_xl.py +212 -93
- diffusers/pipelines/controlnet/pipeline_controlnet_union_sd_xl.py +7 -3
- diffusers/pipelines/controlnet/pipeline_controlnet_union_sd_xl_img2img.py +194 -92
- diffusers/pipelines/deprecated/stable_diffusion_variants/pipeline_cycle_diffusion.py +1 -1
- diffusers/pipelines/dit/pipeline_dit.py +3 -1
- diffusers/pipelines/flux/__init__.py +4 -0
- diffusers/pipelines/flux/pipeline_flux.py +34 -26
- diffusers/pipelines/flux/pipeline_flux_control.py +8 -8
- diffusers/pipelines/flux/pipeline_flux_control_img2img.py +1 -1
- diffusers/pipelines/flux/pipeline_flux_control_inpaint.py +1 -1
- diffusers/pipelines/flux/pipeline_flux_controlnet.py +1 -1
- diffusers/pipelines/flux/pipeline_flux_controlnet_image_to_image.py +1 -1
- diffusers/pipelines/flux/pipeline_flux_controlnet_inpainting.py +1 -1
- diffusers/pipelines/flux/pipeline_flux_fill.py +1 -1
- diffusers/pipelines/flux/pipeline_flux_img2img.py +1 -1
- diffusers/pipelines/flux/pipeline_flux_inpaint.py +1 -1
- diffusers/pipelines/flux/pipeline_flux_kontext.py +1134 -0
- diffusers/pipelines/flux/pipeline_flux_kontext_inpaint.py +1460 -0
- diffusers/pipelines/flux/pipeline_flux_prior_redux.py +1 -1
- diffusers/pipelines/flux/pipeline_output.py +6 -4
- diffusers/pipelines/hidream_image/pipeline_hidream_image.py +5 -5
- diffusers/pipelines/hunyuan_video/pipeline_hunyuan_video.py +25 -24
- diffusers/pipelines/ltx/pipeline_ltx.py +13 -12
- diffusers/pipelines/ltx/pipeline_ltx_condition.py +10 -9
- diffusers/pipelines/ltx/pipeline_ltx_image2video.py +13 -12
- diffusers/pipelines/mochi/pipeline_mochi.py +9 -8
- diffusers/pipelines/pipeline_flax_utils.py +2 -2
- diffusers/pipelines/pipeline_loading_utils.py +24 -2
- diffusers/pipelines/pipeline_utils.py +22 -15
- diffusers/pipelines/pixart_alpha/pipeline_pixart_alpha.py +3 -1
- diffusers/pipelines/pixart_alpha/pipeline_pixart_sigma.py +20 -0
- diffusers/pipelines/qwenimage/__init__.py +55 -0
- diffusers/pipelines/qwenimage/pipeline_output.py +21 -0
- diffusers/pipelines/qwenimage/pipeline_qwenimage.py +726 -0
- diffusers/pipelines/qwenimage/pipeline_qwenimage_edit.py +849 -0
- diffusers/pipelines/qwenimage/pipeline_qwenimage_img2img.py +829 -0
- diffusers/pipelines/qwenimage/pipeline_qwenimage_inpaint.py +1015 -0
- diffusers/pipelines/sana/pipeline_sana_sprint.py +5 -5
- diffusers/pipelines/skyreels_v2/__init__.py +59 -0
- diffusers/pipelines/skyreels_v2/pipeline_output.py +20 -0
- diffusers/pipelines/skyreels_v2/pipeline_skyreels_v2.py +610 -0
- diffusers/pipelines/skyreels_v2/pipeline_skyreels_v2_diffusion_forcing.py +978 -0
- diffusers/pipelines/skyreels_v2/pipeline_skyreels_v2_diffusion_forcing_i2v.py +1059 -0
- diffusers/pipelines/skyreels_v2/pipeline_skyreels_v2_diffusion_forcing_v2v.py +1063 -0
- diffusers/pipelines/skyreels_v2/pipeline_skyreels_v2_i2v.py +745 -0
- diffusers/pipelines/stable_diffusion/pipeline_onnx_stable_diffusion.py +2 -1
- diffusers/pipelines/stable_diffusion/pipeline_onnx_stable_diffusion_inpaint.py +1 -1
- diffusers/pipelines/stable_diffusion/pipeline_onnx_stable_diffusion_upscale.py +1 -1
- diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion.py +2 -1
- diffusers/pipelines/stable_diffusion_3/pipeline_stable_diffusion_3.py +6 -5
- diffusers/pipelines/wan/pipeline_wan.py +78 -20
- diffusers/pipelines/wan/pipeline_wan_i2v.py +112 -32
- diffusers/pipelines/wan/pipeline_wan_vace.py +1 -2
- diffusers/quantizers/__init__.py +1 -177
- diffusers/quantizers/base.py +11 -0
- diffusers/quantizers/gguf/utils.py +92 -3
- diffusers/quantizers/pipe_quant_config.py +202 -0
- diffusers/quantizers/torchao/torchao_quantizer.py +26 -0
- diffusers/schedulers/scheduling_deis_multistep.py +8 -1
- diffusers/schedulers/scheduling_dpmsolver_multistep.py +6 -0
- diffusers/schedulers/scheduling_dpmsolver_singlestep.py +6 -0
- diffusers/schedulers/scheduling_scm.py +0 -1
- diffusers/schedulers/scheduling_unipc_multistep.py +10 -1
- diffusers/schedulers/scheduling_utils.py +2 -2
- diffusers/schedulers/scheduling_utils_flax.py +1 -1
- diffusers/training_utils.py +78 -0
- diffusers/utils/__init__.py +10 -0
- diffusers/utils/constants.py +4 -0
- diffusers/utils/dummy_pt_objects.py +312 -0
- diffusers/utils/dummy_torch_and_transformers_objects.py +255 -0
- diffusers/utils/dynamic_modules_utils.py +84 -25
- diffusers/utils/hub_utils.py +33 -17
- diffusers/utils/import_utils.py +70 -0
- diffusers/utils/peft_utils.py +11 -8
- diffusers/utils/testing_utils.py +136 -10
- diffusers/utils/torch_utils.py +18 -0
- {diffusers-0.34.0.dist-info → diffusers-0.35.1.dist-info}/METADATA +6 -6
- {diffusers-0.34.0.dist-info → diffusers-0.35.1.dist-info}/RECORD +191 -127
- {diffusers-0.34.0.dist-info → diffusers-0.35.1.dist-info}/LICENSE +0 -0
- {diffusers-0.34.0.dist-info → diffusers-0.35.1.dist-info}/WHEEL +0 -0
- {diffusers-0.34.0.dist-info → diffusers-0.35.1.dist-info}/entry_points.txt +0 -0
- {diffusers-0.34.0.dist-info → diffusers-0.35.1.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,689 @@
|
|
1
|
+
# Copyright 2025 The HuggingFace Team. All rights reserved.
|
2
|
+
#
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at
|
6
|
+
#
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
|
15
|
+
import inspect
|
16
|
+
from typing import Any, List, Optional, Tuple, Union
|
17
|
+
|
18
|
+
import numpy as np
|
19
|
+
import torch
|
20
|
+
|
21
|
+
from ...models import AutoencoderKL
|
22
|
+
from ...schedulers import FlowMatchEulerDiscreteScheduler
|
23
|
+
from ...utils import logging
|
24
|
+
from ...utils.torch_utils import randn_tensor
|
25
|
+
from ..modular_pipeline import ModularPipelineBlocks, PipelineState
|
26
|
+
from ..modular_pipeline_utils import ComponentSpec, InputParam, OutputParam
|
27
|
+
from .modular_pipeline import FluxModularPipeline
|
28
|
+
|
29
|
+
|
30
|
+
logger = logging.get_logger(__name__) # pylint: disable=invalid-name
|
31
|
+
|
32
|
+
|
33
|
+
# Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.retrieve_timesteps
|
34
|
+
def retrieve_timesteps(
|
35
|
+
scheduler,
|
36
|
+
num_inference_steps: Optional[int] = None,
|
37
|
+
device: Optional[Union[str, torch.device]] = None,
|
38
|
+
timesteps: Optional[List[int]] = None,
|
39
|
+
sigmas: Optional[List[float]] = None,
|
40
|
+
**kwargs,
|
41
|
+
):
|
42
|
+
r"""
|
43
|
+
Calls the scheduler's `set_timesteps` method and retrieves timesteps from the scheduler after the call. Handles
|
44
|
+
custom timesteps. Any kwargs will be supplied to `scheduler.set_timesteps`.
|
45
|
+
|
46
|
+
Args:
|
47
|
+
scheduler (`SchedulerMixin`):
|
48
|
+
The scheduler to get timesteps from.
|
49
|
+
num_inference_steps (`int`):
|
50
|
+
The number of diffusion steps used when generating samples with a pre-trained model. If used, `timesteps`
|
51
|
+
must be `None`.
|
52
|
+
device (`str` or `torch.device`, *optional*):
|
53
|
+
The device to which the timesteps should be moved to. If `None`, the timesteps are not moved.
|
54
|
+
timesteps (`List[int]`, *optional*):
|
55
|
+
Custom timesteps used to override the timestep spacing strategy of the scheduler. If `timesteps` is passed,
|
56
|
+
`num_inference_steps` and `sigmas` must be `None`.
|
57
|
+
sigmas (`List[float]`, *optional*):
|
58
|
+
Custom sigmas used to override the timestep spacing strategy of the scheduler. If `sigmas` is passed,
|
59
|
+
`num_inference_steps` and `timesteps` must be `None`.
|
60
|
+
|
61
|
+
Returns:
|
62
|
+
`Tuple[torch.Tensor, int]`: A tuple where the first element is the timestep schedule from the scheduler and the
|
63
|
+
second element is the number of inference steps.
|
64
|
+
"""
|
65
|
+
if timesteps is not None and sigmas is not None:
|
66
|
+
raise ValueError("Only one of `timesteps` or `sigmas` can be passed. Please choose one to set custom values")
|
67
|
+
if timesteps is not None:
|
68
|
+
accepts_timesteps = "timesteps" in set(inspect.signature(scheduler.set_timesteps).parameters.keys())
|
69
|
+
if not accepts_timesteps:
|
70
|
+
raise ValueError(
|
71
|
+
f"The current scheduler class {scheduler.__class__}'s `set_timesteps` does not support custom"
|
72
|
+
f" timestep schedules. Please check whether you are using the correct scheduler."
|
73
|
+
)
|
74
|
+
scheduler.set_timesteps(timesteps=timesteps, device=device, **kwargs)
|
75
|
+
timesteps = scheduler.timesteps
|
76
|
+
num_inference_steps = len(timesteps)
|
77
|
+
elif sigmas is not None:
|
78
|
+
accept_sigmas = "sigmas" in set(inspect.signature(scheduler.set_timesteps).parameters.keys())
|
79
|
+
if not accept_sigmas:
|
80
|
+
raise ValueError(
|
81
|
+
f"The current scheduler class {scheduler.__class__}'s `set_timesteps` does not support custom"
|
82
|
+
f" sigmas schedules. Please check whether you are using the correct scheduler."
|
83
|
+
)
|
84
|
+
scheduler.set_timesteps(sigmas=sigmas, device=device, **kwargs)
|
85
|
+
timesteps = scheduler.timesteps
|
86
|
+
num_inference_steps = len(timesteps)
|
87
|
+
else:
|
88
|
+
scheduler.set_timesteps(num_inference_steps, device=device, **kwargs)
|
89
|
+
timesteps = scheduler.timesteps
|
90
|
+
return timesteps, num_inference_steps
|
91
|
+
|
92
|
+
|
93
|
+
# Copied from diffusers.pipelines.flux.pipeline_flux.calculate_shift
|
94
|
+
def calculate_shift(
|
95
|
+
image_seq_len,
|
96
|
+
base_seq_len: int = 256,
|
97
|
+
max_seq_len: int = 4096,
|
98
|
+
base_shift: float = 0.5,
|
99
|
+
max_shift: float = 1.15,
|
100
|
+
):
|
101
|
+
m = (max_shift - base_shift) / (max_seq_len - base_seq_len)
|
102
|
+
b = base_shift - m * base_seq_len
|
103
|
+
mu = image_seq_len * m + b
|
104
|
+
return mu
|
105
|
+
|
106
|
+
|
107
|
+
# Adapted from the original implementation.
|
108
|
+
def prepare_latents_img2img(
|
109
|
+
vae, scheduler, image, timestep, batch_size, num_channels_latents, height, width, dtype, device, generator
|
110
|
+
):
|
111
|
+
if isinstance(generator, list) and len(generator) != batch_size:
|
112
|
+
raise ValueError(
|
113
|
+
f"You have passed a list of generators of length {len(generator)}, but requested an effective batch"
|
114
|
+
f" size of {batch_size}. Make sure the batch size matches the length of the generators."
|
115
|
+
)
|
116
|
+
|
117
|
+
vae_scale_factor = 2 ** (len(vae.config.block_out_channels) - 1)
|
118
|
+
latent_channels = vae.config.latent_channels
|
119
|
+
|
120
|
+
# VAE applies 8x compression on images but we must also account for packing which requires
|
121
|
+
# latent height and width to be divisible by 2.
|
122
|
+
height = 2 * (int(height) // (vae_scale_factor * 2))
|
123
|
+
width = 2 * (int(width) // (vae_scale_factor * 2))
|
124
|
+
shape = (batch_size, num_channels_latents, height, width)
|
125
|
+
latent_image_ids = _prepare_latent_image_ids(batch_size, height // 2, width // 2, device, dtype)
|
126
|
+
|
127
|
+
image = image.to(device=device, dtype=dtype)
|
128
|
+
if image.shape[1] != latent_channels:
|
129
|
+
image_latents = _encode_vae_image(image=image, generator=generator)
|
130
|
+
else:
|
131
|
+
image_latents = image
|
132
|
+
if batch_size > image_latents.shape[0] and batch_size % image_latents.shape[0] == 0:
|
133
|
+
# expand init_latents for batch_size
|
134
|
+
additional_image_per_prompt = batch_size // image_latents.shape[0]
|
135
|
+
image_latents = torch.cat([image_latents] * additional_image_per_prompt, dim=0)
|
136
|
+
elif batch_size > image_latents.shape[0] and batch_size % image_latents.shape[0] != 0:
|
137
|
+
raise ValueError(
|
138
|
+
f"Cannot duplicate `image` of batch size {image_latents.shape[0]} to {batch_size} text prompts."
|
139
|
+
)
|
140
|
+
else:
|
141
|
+
image_latents = torch.cat([image_latents], dim=0)
|
142
|
+
|
143
|
+
noise = randn_tensor(shape, generator=generator, device=device, dtype=dtype)
|
144
|
+
latents = scheduler.scale_noise(image_latents, timestep, noise)
|
145
|
+
latents = _pack_latents(latents, batch_size, num_channels_latents, height, width)
|
146
|
+
return latents, latent_image_ids
|
147
|
+
|
148
|
+
|
149
|
+
# Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion_img2img.retrieve_latents
|
150
|
+
def retrieve_latents(
|
151
|
+
encoder_output: torch.Tensor, generator: Optional[torch.Generator] = None, sample_mode: str = "sample"
|
152
|
+
):
|
153
|
+
if hasattr(encoder_output, "latent_dist") and sample_mode == "sample":
|
154
|
+
return encoder_output.latent_dist.sample(generator)
|
155
|
+
elif hasattr(encoder_output, "latent_dist") and sample_mode == "argmax":
|
156
|
+
return encoder_output.latent_dist.mode()
|
157
|
+
elif hasattr(encoder_output, "latents"):
|
158
|
+
return encoder_output.latents
|
159
|
+
else:
|
160
|
+
raise AttributeError("Could not access latents of provided encoder_output")
|
161
|
+
|
162
|
+
|
163
|
+
def _pack_latents(latents, batch_size, num_channels_latents, height, width):
|
164
|
+
latents = latents.view(batch_size, num_channels_latents, height // 2, 2, width // 2, 2)
|
165
|
+
latents = latents.permute(0, 2, 4, 1, 3, 5)
|
166
|
+
latents = latents.reshape(batch_size, (height // 2) * (width // 2), num_channels_latents * 4)
|
167
|
+
|
168
|
+
return latents
|
169
|
+
|
170
|
+
|
171
|
+
def _prepare_latent_image_ids(batch_size, height, width, device, dtype):
|
172
|
+
latent_image_ids = torch.zeros(height, width, 3)
|
173
|
+
latent_image_ids[..., 1] = latent_image_ids[..., 1] + torch.arange(height)[:, None]
|
174
|
+
latent_image_ids[..., 2] = latent_image_ids[..., 2] + torch.arange(width)[None, :]
|
175
|
+
|
176
|
+
latent_image_id_height, latent_image_id_width, latent_image_id_channels = latent_image_ids.shape
|
177
|
+
|
178
|
+
latent_image_ids = latent_image_ids.reshape(
|
179
|
+
latent_image_id_height * latent_image_id_width, latent_image_id_channels
|
180
|
+
)
|
181
|
+
|
182
|
+
return latent_image_ids.to(device=device, dtype=dtype)
|
183
|
+
|
184
|
+
|
185
|
+
# Cannot use "# Copied from" because it introduces weird indentation errors.
|
186
|
+
def _encode_vae_image(vae, image: torch.Tensor, generator: torch.Generator):
|
187
|
+
if isinstance(generator, list):
|
188
|
+
image_latents = [
|
189
|
+
retrieve_latents(vae.encode(image[i : i + 1]), generator=generator[i]) for i in range(image.shape[0])
|
190
|
+
]
|
191
|
+
image_latents = torch.cat(image_latents, dim=0)
|
192
|
+
else:
|
193
|
+
image_latents = retrieve_latents(vae.encode(image), generator=generator)
|
194
|
+
|
195
|
+
image_latents = (image_latents - vae.config.shift_factor) * vae.config.scaling_factor
|
196
|
+
|
197
|
+
return image_latents
|
198
|
+
|
199
|
+
|
200
|
+
def _get_initial_timesteps_and_optionals(
|
201
|
+
transformer,
|
202
|
+
scheduler,
|
203
|
+
batch_size,
|
204
|
+
height,
|
205
|
+
width,
|
206
|
+
vae_scale_factor,
|
207
|
+
num_inference_steps,
|
208
|
+
guidance_scale,
|
209
|
+
sigmas,
|
210
|
+
device,
|
211
|
+
):
|
212
|
+
image_seq_len = (int(height) // vae_scale_factor // 2) * (int(width) // vae_scale_factor // 2)
|
213
|
+
|
214
|
+
sigmas = np.linspace(1.0, 1 / num_inference_steps, num_inference_steps) if sigmas is None else sigmas
|
215
|
+
if hasattr(scheduler.config, "use_flow_sigmas") and scheduler.config.use_flow_sigmas:
|
216
|
+
sigmas = None
|
217
|
+
mu = calculate_shift(
|
218
|
+
image_seq_len,
|
219
|
+
scheduler.config.get("base_image_seq_len", 256),
|
220
|
+
scheduler.config.get("max_image_seq_len", 4096),
|
221
|
+
scheduler.config.get("base_shift", 0.5),
|
222
|
+
scheduler.config.get("max_shift", 1.15),
|
223
|
+
)
|
224
|
+
timesteps, num_inference_steps = retrieve_timesteps(scheduler, num_inference_steps, device, sigmas=sigmas, mu=mu)
|
225
|
+
if transformer.config.guidance_embeds:
|
226
|
+
guidance = torch.full([1], guidance_scale, device=device, dtype=torch.float32)
|
227
|
+
guidance = guidance.expand(batch_size)
|
228
|
+
else:
|
229
|
+
guidance = None
|
230
|
+
|
231
|
+
return timesteps, num_inference_steps, sigmas, guidance
|
232
|
+
|
233
|
+
|
234
|
+
class FluxInputStep(ModularPipelineBlocks):
|
235
|
+
model_name = "flux"
|
236
|
+
|
237
|
+
@property
|
238
|
+
def description(self) -> str:
|
239
|
+
return (
|
240
|
+
"Input processing step that:\n"
|
241
|
+
" 1. Determines `batch_size` and `dtype` based on `prompt_embeds`\n"
|
242
|
+
" 2. Adjusts input tensor shapes based on `batch_size` (number of prompts) and `num_images_per_prompt`\n\n"
|
243
|
+
"All input tensors are expected to have either batch_size=1 or match the batch_size\n"
|
244
|
+
"of prompt_embeds. The tensors will be duplicated across the batch dimension to\n"
|
245
|
+
"have a final batch_size of batch_size * num_images_per_prompt."
|
246
|
+
)
|
247
|
+
|
248
|
+
@property
|
249
|
+
def inputs(self) -> List[InputParam]:
|
250
|
+
return [
|
251
|
+
InputParam("num_images_per_prompt", default=1),
|
252
|
+
InputParam(
|
253
|
+
"prompt_embeds",
|
254
|
+
required=True,
|
255
|
+
type_hint=torch.Tensor,
|
256
|
+
description="Pre-generated text embeddings. Can be generated from text_encoder step.",
|
257
|
+
),
|
258
|
+
InputParam(
|
259
|
+
"pooled_prompt_embeds",
|
260
|
+
type_hint=torch.Tensor,
|
261
|
+
description="Pre-generated pooled text embeddings. Can be generated from text_encoder step.",
|
262
|
+
),
|
263
|
+
# TODO: support negative embeddings?
|
264
|
+
]
|
265
|
+
|
266
|
+
@property
|
267
|
+
def intermediate_outputs(self) -> List[str]:
|
268
|
+
return [
|
269
|
+
OutputParam(
|
270
|
+
"batch_size",
|
271
|
+
type_hint=int,
|
272
|
+
description="Number of prompts, the final batch size of model inputs should be batch_size * num_images_per_prompt",
|
273
|
+
),
|
274
|
+
OutputParam(
|
275
|
+
"dtype",
|
276
|
+
type_hint=torch.dtype,
|
277
|
+
description="Data type of model tensor inputs (determined by `prompt_embeds`)",
|
278
|
+
),
|
279
|
+
OutputParam(
|
280
|
+
"prompt_embeds",
|
281
|
+
type_hint=torch.Tensor,
|
282
|
+
description="text embeddings used to guide the image generation",
|
283
|
+
),
|
284
|
+
OutputParam(
|
285
|
+
"pooled_prompt_embeds",
|
286
|
+
type_hint=torch.Tensor,
|
287
|
+
description="pooled text embeddings used to guide the image generation",
|
288
|
+
),
|
289
|
+
# TODO: support negative embeddings?
|
290
|
+
]
|
291
|
+
|
292
|
+
def check_inputs(self, components, block_state):
|
293
|
+
if block_state.prompt_embeds is not None and block_state.pooled_prompt_embeds is not None:
|
294
|
+
if block_state.prompt_embeds.shape[0] != block_state.pooled_prompt_embeds.shape[0]:
|
295
|
+
raise ValueError(
|
296
|
+
"`prompt_embeds` and `pooled_prompt_embeds` must have the same batch size when passed directly, but"
|
297
|
+
f" got: `prompt_embeds` {block_state.prompt_embeds.shape} != `pooled_prompt_embeds`"
|
298
|
+
f" {block_state.pooled_prompt_embeds.shape}."
|
299
|
+
)
|
300
|
+
|
301
|
+
@torch.no_grad()
|
302
|
+
def __call__(self, components: FluxModularPipeline, state: PipelineState) -> PipelineState:
|
303
|
+
# TODO: consider adding negative embeddings?
|
304
|
+
block_state = self.get_block_state(state)
|
305
|
+
self.check_inputs(components, block_state)
|
306
|
+
|
307
|
+
block_state.batch_size = block_state.prompt_embeds.shape[0]
|
308
|
+
block_state.dtype = block_state.prompt_embeds.dtype
|
309
|
+
|
310
|
+
_, seq_len, _ = block_state.prompt_embeds.shape
|
311
|
+
block_state.prompt_embeds = block_state.prompt_embeds.repeat(1, block_state.num_images_per_prompt, 1)
|
312
|
+
block_state.prompt_embeds = block_state.prompt_embeds.view(
|
313
|
+
block_state.batch_size * block_state.num_images_per_prompt, seq_len, -1
|
314
|
+
)
|
315
|
+
self.set_block_state(state, block_state)
|
316
|
+
|
317
|
+
return components, state
|
318
|
+
|
319
|
+
|
320
|
+
class FluxSetTimestepsStep(ModularPipelineBlocks):
|
321
|
+
model_name = "flux"
|
322
|
+
|
323
|
+
@property
|
324
|
+
def expected_components(self) -> List[ComponentSpec]:
|
325
|
+
return [ComponentSpec("scheduler", FlowMatchEulerDiscreteScheduler)]
|
326
|
+
|
327
|
+
@property
|
328
|
+
def description(self) -> str:
|
329
|
+
return "Step that sets the scheduler's timesteps for inference"
|
330
|
+
|
331
|
+
@property
|
332
|
+
def inputs(self) -> List[InputParam]:
|
333
|
+
return [
|
334
|
+
InputParam("num_inference_steps", default=50),
|
335
|
+
InputParam("timesteps"),
|
336
|
+
InputParam("sigmas"),
|
337
|
+
InputParam("guidance_scale", default=3.5),
|
338
|
+
InputParam("latents", type_hint=torch.Tensor),
|
339
|
+
InputParam("num_images_per_prompt", default=1),
|
340
|
+
InputParam("height", type_hint=int),
|
341
|
+
InputParam("width", type_hint=int),
|
342
|
+
InputParam(
|
343
|
+
"batch_size",
|
344
|
+
required=True,
|
345
|
+
type_hint=int,
|
346
|
+
description="Number of prompts, the final batch size of model inputs should be `batch_size * num_images_per_prompt`. Can be generated in input step.",
|
347
|
+
),
|
348
|
+
]
|
349
|
+
|
350
|
+
@property
|
351
|
+
def intermediate_outputs(self) -> List[OutputParam]:
|
352
|
+
return [
|
353
|
+
OutputParam("timesteps", type_hint=torch.Tensor, description="The timesteps to use for inference"),
|
354
|
+
OutputParam(
|
355
|
+
"num_inference_steps",
|
356
|
+
type_hint=int,
|
357
|
+
description="The number of denoising steps to perform at inference time",
|
358
|
+
),
|
359
|
+
OutputParam("guidance", type_hint=torch.Tensor, description="Optional guidance to be used."),
|
360
|
+
]
|
361
|
+
|
362
|
+
@torch.no_grad()
|
363
|
+
def __call__(self, components: FluxModularPipeline, state: PipelineState) -> PipelineState:
|
364
|
+
block_state = self.get_block_state(state)
|
365
|
+
block_state.device = components._execution_device
|
366
|
+
|
367
|
+
scheduler = components.scheduler
|
368
|
+
transformer = components.transformer
|
369
|
+
|
370
|
+
batch_size = block_state.batch_size * block_state.num_images_per_prompt
|
371
|
+
timesteps, num_inference_steps, sigmas, guidance = _get_initial_timesteps_and_optionals(
|
372
|
+
transformer,
|
373
|
+
scheduler,
|
374
|
+
batch_size,
|
375
|
+
block_state.height,
|
376
|
+
block_state.width,
|
377
|
+
components.vae_scale_factor,
|
378
|
+
block_state.num_inference_steps,
|
379
|
+
block_state.guidance_scale,
|
380
|
+
block_state.sigmas,
|
381
|
+
block_state.device,
|
382
|
+
)
|
383
|
+
block_state.timesteps = timesteps
|
384
|
+
block_state.num_inference_steps = num_inference_steps
|
385
|
+
block_state.sigmas = sigmas
|
386
|
+
block_state.guidance = guidance
|
387
|
+
|
388
|
+
self.set_block_state(state, block_state)
|
389
|
+
return components, state
|
390
|
+
|
391
|
+
|
392
|
+
class FluxImg2ImgSetTimestepsStep(ModularPipelineBlocks):
|
393
|
+
model_name = "flux"
|
394
|
+
|
395
|
+
@property
|
396
|
+
def expected_components(self) -> List[ComponentSpec]:
|
397
|
+
return [ComponentSpec("scheduler", FlowMatchEulerDiscreteScheduler)]
|
398
|
+
|
399
|
+
@property
|
400
|
+
def description(self) -> str:
|
401
|
+
return "Step that sets the scheduler's timesteps for inference"
|
402
|
+
|
403
|
+
@property
|
404
|
+
def inputs(self) -> List[InputParam]:
|
405
|
+
return [
|
406
|
+
InputParam("num_inference_steps", default=50),
|
407
|
+
InputParam("timesteps"),
|
408
|
+
InputParam("sigmas"),
|
409
|
+
InputParam("strength", default=0.6),
|
410
|
+
InputParam("guidance_scale", default=3.5),
|
411
|
+
InputParam("num_images_per_prompt", default=1),
|
412
|
+
InputParam("height", type_hint=int),
|
413
|
+
InputParam("width", type_hint=int),
|
414
|
+
InputParam(
|
415
|
+
"batch_size",
|
416
|
+
required=True,
|
417
|
+
type_hint=int,
|
418
|
+
description="Number of prompts, the final batch size of model inputs should be `batch_size * num_images_per_prompt`. Can be generated in input step.",
|
419
|
+
),
|
420
|
+
]
|
421
|
+
|
422
|
+
@property
|
423
|
+
def intermediate_outputs(self) -> List[OutputParam]:
|
424
|
+
return [
|
425
|
+
OutputParam("timesteps", type_hint=torch.Tensor, description="The timesteps to use for inference"),
|
426
|
+
OutputParam(
|
427
|
+
"num_inference_steps",
|
428
|
+
type_hint=int,
|
429
|
+
description="The number of denoising steps to perform at inference time",
|
430
|
+
),
|
431
|
+
OutputParam(
|
432
|
+
"latent_timestep",
|
433
|
+
type_hint=torch.Tensor,
|
434
|
+
description="The timestep that represents the initial noise level for image-to-image generation",
|
435
|
+
),
|
436
|
+
OutputParam("guidance", type_hint=torch.Tensor, description="Optional guidance to be used."),
|
437
|
+
]
|
438
|
+
|
439
|
+
@staticmethod
|
440
|
+
# Copied from diffusers.pipelines.stable_diffusion_3.pipeline_stable_diffusion_3_img2img.StableDiffusion3Img2ImgPipeline.get_timesteps with self.scheduler->scheduler
|
441
|
+
def get_timesteps(scheduler, num_inference_steps, strength, device):
|
442
|
+
# get the original timestep using init_timestep
|
443
|
+
init_timestep = min(num_inference_steps * strength, num_inference_steps)
|
444
|
+
|
445
|
+
t_start = int(max(num_inference_steps - init_timestep, 0))
|
446
|
+
timesteps = scheduler.timesteps[t_start * scheduler.order :]
|
447
|
+
if hasattr(scheduler, "set_begin_index"):
|
448
|
+
scheduler.set_begin_index(t_start * scheduler.order)
|
449
|
+
|
450
|
+
return timesteps, num_inference_steps - t_start
|
451
|
+
|
452
|
+
@torch.no_grad()
|
453
|
+
def __call__(self, components: FluxModularPipeline, state: PipelineState) -> PipelineState:
|
454
|
+
block_state = self.get_block_state(state)
|
455
|
+
block_state.device = components._execution_device
|
456
|
+
|
457
|
+
scheduler = components.scheduler
|
458
|
+
transformer = components.transformer
|
459
|
+
batch_size = block_state.batch_size * block_state.num_images_per_prompt
|
460
|
+
timesteps, num_inference_steps, sigmas, guidance = _get_initial_timesteps_and_optionals(
|
461
|
+
transformer,
|
462
|
+
scheduler,
|
463
|
+
batch_size,
|
464
|
+
block_state.height,
|
465
|
+
block_state.width,
|
466
|
+
components.vae_scale_factor,
|
467
|
+
block_state.num_inference_steps,
|
468
|
+
block_state.guidance_scale,
|
469
|
+
block_state.sigmas,
|
470
|
+
block_state.device,
|
471
|
+
)
|
472
|
+
timesteps, num_inference_steps = self.get_timesteps(
|
473
|
+
scheduler, num_inference_steps, block_state.strength, block_state.device
|
474
|
+
)
|
475
|
+
block_state.timesteps = timesteps
|
476
|
+
block_state.num_inference_steps = num_inference_steps
|
477
|
+
block_state.sigmas = sigmas
|
478
|
+
block_state.guidance = guidance
|
479
|
+
|
480
|
+
block_state.latent_timestep = timesteps[:1].repeat(batch_size)
|
481
|
+
|
482
|
+
self.set_block_state(state, block_state)
|
483
|
+
return components, state
|
484
|
+
|
485
|
+
|
486
|
+
class FluxPrepareLatentsStep(ModularPipelineBlocks):
|
487
|
+
model_name = "flux"
|
488
|
+
|
489
|
+
@property
|
490
|
+
def expected_components(self) -> List[ComponentSpec]:
|
491
|
+
return []
|
492
|
+
|
493
|
+
@property
|
494
|
+
def description(self) -> str:
|
495
|
+
return "Prepare latents step that prepares the latents for the text-to-image generation process"
|
496
|
+
|
497
|
+
@property
|
498
|
+
def inputs(self) -> List[InputParam]:
|
499
|
+
return [
|
500
|
+
InputParam("height", type_hint=int),
|
501
|
+
InputParam("width", type_hint=int),
|
502
|
+
InputParam("latents", type_hint=Optional[torch.Tensor]),
|
503
|
+
InputParam("num_images_per_prompt", type_hint=int, default=1),
|
504
|
+
InputParam("generator"),
|
505
|
+
InputParam(
|
506
|
+
"batch_size",
|
507
|
+
required=True,
|
508
|
+
type_hint=int,
|
509
|
+
description="Number of prompts, the final batch size of model inputs should be `batch_size * num_images_per_prompt`. Can be generated in input step.",
|
510
|
+
),
|
511
|
+
InputParam("dtype", type_hint=torch.dtype, description="The dtype of the model inputs"),
|
512
|
+
]
|
513
|
+
|
514
|
+
@property
|
515
|
+
def intermediate_outputs(self) -> List[OutputParam]:
|
516
|
+
return [
|
517
|
+
OutputParam(
|
518
|
+
"latents", type_hint=torch.Tensor, description="The initial latents to use for the denoising process"
|
519
|
+
),
|
520
|
+
OutputParam(
|
521
|
+
"latent_image_ids",
|
522
|
+
type_hint=torch.Tensor,
|
523
|
+
description="IDs computed from the image sequence needed for RoPE",
|
524
|
+
),
|
525
|
+
]
|
526
|
+
|
527
|
+
@staticmethod
|
528
|
+
def check_inputs(components, block_state):
|
529
|
+
if (block_state.height is not None and block_state.height % (components.vae_scale_factor * 2) != 0) or (
|
530
|
+
block_state.width is not None and block_state.width % (components.vae_scale_factor * 2) != 0
|
531
|
+
):
|
532
|
+
logger.warning(
|
533
|
+
f"`height` and `width` have to be divisible by {components.vae_scale_factor} but are {block_state.height} and {block_state.width}."
|
534
|
+
)
|
535
|
+
|
536
|
+
@staticmethod
|
537
|
+
def prepare_latents(
|
538
|
+
comp,
|
539
|
+
batch_size,
|
540
|
+
num_channels_latents,
|
541
|
+
height,
|
542
|
+
width,
|
543
|
+
dtype,
|
544
|
+
device,
|
545
|
+
generator,
|
546
|
+
latents=None,
|
547
|
+
):
|
548
|
+
# Couldn't use the `prepare_latents` method directly from Flux because I decided to copy over
|
549
|
+
# the packing methods here. So, for example, `comp._pack_latents()` won't work if we were
|
550
|
+
# to go with the "# Copied from ..." approach. Or maybe there's a way?
|
551
|
+
|
552
|
+
# VAE applies 8x compression on images but we must also account for packing which requires
|
553
|
+
# latent height and width to be divisible by 2.
|
554
|
+
height = 2 * (int(height) // (comp.vae_scale_factor * 2))
|
555
|
+
width = 2 * (int(width) // (comp.vae_scale_factor * 2))
|
556
|
+
|
557
|
+
shape = (batch_size, num_channels_latents, height, width)
|
558
|
+
|
559
|
+
if latents is not None:
|
560
|
+
latent_image_ids = _prepare_latent_image_ids(batch_size, height // 2, width // 2, device, dtype)
|
561
|
+
return latents.to(device=device, dtype=dtype), latent_image_ids
|
562
|
+
|
563
|
+
if isinstance(generator, list) and len(generator) != batch_size:
|
564
|
+
raise ValueError(
|
565
|
+
f"You have passed a list of generators of length {len(generator)}, but requested an effective batch"
|
566
|
+
f" size of {batch_size}. Make sure the batch size matches the length of the generators."
|
567
|
+
)
|
568
|
+
|
569
|
+
latents = randn_tensor(shape, generator=generator, device=device, dtype=dtype)
|
570
|
+
latents = _pack_latents(latents, batch_size, num_channels_latents, height, width)
|
571
|
+
|
572
|
+
latent_image_ids = _prepare_latent_image_ids(batch_size, height // 2, width // 2, device, dtype)
|
573
|
+
|
574
|
+
return latents, latent_image_ids
|
575
|
+
|
576
|
+
@torch.no_grad()
|
577
|
+
def __call__(self, components: FluxModularPipeline, state: PipelineState) -> PipelineState:
|
578
|
+
block_state = self.get_block_state(state)
|
579
|
+
|
580
|
+
block_state.height = block_state.height or components.default_height
|
581
|
+
block_state.width = block_state.width or components.default_width
|
582
|
+
block_state.device = components._execution_device
|
583
|
+
block_state.dtype = torch.bfloat16 # TODO: okay to hardcode this?
|
584
|
+
block_state.num_channels_latents = components.num_channels_latents
|
585
|
+
|
586
|
+
self.check_inputs(components, block_state)
|
587
|
+
batch_size = block_state.batch_size * block_state.num_images_per_prompt
|
588
|
+
block_state.latents, block_state.latent_image_ids = self.prepare_latents(
|
589
|
+
components,
|
590
|
+
batch_size,
|
591
|
+
block_state.num_channels_latents,
|
592
|
+
block_state.height,
|
593
|
+
block_state.width,
|
594
|
+
block_state.dtype,
|
595
|
+
block_state.device,
|
596
|
+
block_state.generator,
|
597
|
+
block_state.latents,
|
598
|
+
)
|
599
|
+
|
600
|
+
self.set_block_state(state, block_state)
|
601
|
+
|
602
|
+
return components, state
|
603
|
+
|
604
|
+
|
605
|
+
class FluxImg2ImgPrepareLatentsStep(ModularPipelineBlocks):
|
606
|
+
model_name = "flux"
|
607
|
+
|
608
|
+
@property
|
609
|
+
def expected_components(self) -> List[ComponentSpec]:
|
610
|
+
return [ComponentSpec("vae", AutoencoderKL), ComponentSpec("scheduler", FlowMatchEulerDiscreteScheduler)]
|
611
|
+
|
612
|
+
@property
|
613
|
+
def description(self) -> str:
|
614
|
+
return "Step that prepares the latents for the image-to-image generation process"
|
615
|
+
|
616
|
+
@property
|
617
|
+
def inputs(self) -> List[Tuple[str, Any]]:
|
618
|
+
return [
|
619
|
+
InputParam("height", type_hint=int),
|
620
|
+
InputParam("width", type_hint=int),
|
621
|
+
InputParam("latents", type_hint=Optional[torch.Tensor]),
|
622
|
+
InputParam("num_images_per_prompt", type_hint=int, default=1),
|
623
|
+
InputParam("generator"),
|
624
|
+
InputParam(
|
625
|
+
"image_latents",
|
626
|
+
required=True,
|
627
|
+
type_hint=torch.Tensor,
|
628
|
+
description="The latents representing the reference image for image-to-image/inpainting generation. Can be generated in vae_encode step.",
|
629
|
+
),
|
630
|
+
InputParam(
|
631
|
+
"latent_timestep",
|
632
|
+
required=True,
|
633
|
+
type_hint=torch.Tensor,
|
634
|
+
description="The timestep that represents the initial noise level for image-to-image/inpainting generation. Can be generated in set_timesteps step.",
|
635
|
+
),
|
636
|
+
InputParam(
|
637
|
+
"batch_size",
|
638
|
+
required=True,
|
639
|
+
type_hint=int,
|
640
|
+
description="Number of prompts, the final batch size of model inputs should be batch_size * num_images_per_prompt. Can be generated in input step.",
|
641
|
+
),
|
642
|
+
InputParam("dtype", required=True, type_hint=torch.dtype, description="The dtype of the model inputs"),
|
643
|
+
]
|
644
|
+
|
645
|
+
@property
|
646
|
+
def intermediate_outputs(self) -> List[OutputParam]:
|
647
|
+
return [
|
648
|
+
OutputParam(
|
649
|
+
"latents", type_hint=torch.Tensor, description="The initial latents to use for the denoising process"
|
650
|
+
),
|
651
|
+
OutputParam(
|
652
|
+
"latent_image_ids",
|
653
|
+
type_hint=torch.Tensor,
|
654
|
+
description="IDs computed from the image sequence needed for RoPE",
|
655
|
+
),
|
656
|
+
]
|
657
|
+
|
658
|
+
@torch.no_grad()
|
659
|
+
def __call__(self, components: FluxModularPipeline, state: PipelineState) -> PipelineState:
|
660
|
+
block_state = self.get_block_state(state)
|
661
|
+
|
662
|
+
block_state.height = block_state.height or components.default_height
|
663
|
+
block_state.width = block_state.width or components.default_width
|
664
|
+
block_state.device = components._execution_device
|
665
|
+
block_state.dtype = torch.bfloat16 # TODO: okay to hardcode this?
|
666
|
+
block_state.num_channels_latents = components.num_channels_latents
|
667
|
+
block_state.dtype = block_state.dtype if block_state.dtype is not None else components.vae.dtype
|
668
|
+
block_state.device = components._execution_device
|
669
|
+
|
670
|
+
# TODO: implement `check_inputs`
|
671
|
+
batch_size = block_state.batch_size * block_state.num_images_per_prompt
|
672
|
+
if block_state.latents is None:
|
673
|
+
block_state.latents, block_state.latent_image_ids = prepare_latents_img2img(
|
674
|
+
components.vae,
|
675
|
+
components.scheduler,
|
676
|
+
block_state.image_latents,
|
677
|
+
block_state.latent_timestep,
|
678
|
+
batch_size,
|
679
|
+
block_state.num_channels_latents,
|
680
|
+
block_state.height,
|
681
|
+
block_state.width,
|
682
|
+
block_state.dtype,
|
683
|
+
block_state.device,
|
684
|
+
block_state.generator,
|
685
|
+
)
|
686
|
+
|
687
|
+
self.set_block_state(state, block_state)
|
688
|
+
|
689
|
+
return components, state
|