Delete pipeline_flux_controlnet.py
Browse files- pipeline_flux_controlnet.py +0 -532
pipeline_flux_controlnet.py
DELETED
@@ -1,532 +0,0 @@
|
|
1 |
-
# Copyright 2024 Stability AI and 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 |
-
from typing import Any, Callable, Dict, List, Optional, Union
|
16 |
-
import torch
|
17 |
-
from transformers import (
|
18 |
-
T5EncoderModel,
|
19 |
-
T5TokenizerFast,
|
20 |
-
)
|
21 |
-
from diffusers.image_processor import PipelineImageInput
|
22 |
-
|
23 |
-
from diffusers import AutoencoderKL # Waiting for diffusers udpdate
|
24 |
-
from diffusers.schedulers import FlowMatchEulerDiscreteScheduler
|
25 |
-
from diffusers.schedulers import KarrasDiffusionSchedulers
|
26 |
-
from diffusers.utils import logging
|
27 |
-
from diffusers.pipelines.flux.pipeline_output import FluxPipelineOutput
|
28 |
-
from diffusers.pipelines.flux.pipeline_flux import retrieve_timesteps
|
29 |
-
from .controlnet_flux import FluxControlNetModel, FluxMultiControlNetModel
|
30 |
-
|
31 |
-
from .pipeline_bria import BriaPipeline
|
32 |
-
from transformer_bria import BriaTransformer2DModel
|
33 |
-
from bria_utils import get_original_sigmas
|
34 |
-
|
35 |
-
XLA_AVAILABLE = False
|
36 |
-
|
37 |
-
|
38 |
-
logger = logging.get_logger(__name__) # pylint: disable=invalid-name
|
39 |
-
|
40 |
-
|
41 |
-
class FluxControlNetPipeline(BriaPipeline):
|
42 |
-
r"""
|
43 |
-
Args:
|
44 |
-
transformer ([`SD3Transformer2DModel`]):
|
45 |
-
Conditional Transformer (MMDiT) architecture to denoise the encoded image latents.
|
46 |
-
scheduler ([`FlowMatchEulerDiscreteScheduler`]):
|
47 |
-
A scheduler to be used in combination with `transformer` to denoise the encoded image latents.
|
48 |
-
vae ([`AutoencoderKL`]):
|
49 |
-
Variational Auto-Encoder (VAE) Model to encode and decode images to and from latent representations.
|
50 |
-
text_encoder ([`T5EncoderModel`]):
|
51 |
-
Frozen text-encoder. Stable Diffusion 3 uses
|
52 |
-
[T5](https://huggingface.co/docs/transformers/model_doc/t5#transformers.T5EncoderModel), specifically the
|
53 |
-
[t5-v1_1-xxl](https://huggingface.co/google/t5-v1_1-xxl) variant.
|
54 |
-
tokenizer (`T5TokenizerFast`):
|
55 |
-
Tokenizer of class
|
56 |
-
[T5Tokenizer](https://huggingface.co/docs/transformers/model_doc/t5#transformers.T5Tokenizer).
|
57 |
-
"""
|
58 |
-
|
59 |
-
model_cpu_offload_seq = "text_encoder->text_encoder_2->text_encoder->transformer->vae"
|
60 |
-
_optional_components = []
|
61 |
-
_callback_tensor_inputs = ["latents", "prompt_embeds", "negative_prompt_embeds", "negative_pooled_prompt_embeds"]
|
62 |
-
|
63 |
-
def __init__( # EYAL - removed clip text encoder + tokenizer
|
64 |
-
self,
|
65 |
-
transformer: BriaTransformer2DModel,
|
66 |
-
scheduler: Union[FlowMatchEulerDiscreteScheduler, KarrasDiffusionSchedulers],
|
67 |
-
vae: AutoencoderKL,
|
68 |
-
text_encoder: T5EncoderModel,
|
69 |
-
tokenizer: T5TokenizerFast,
|
70 |
-
controlnet: FluxControlNetModel,
|
71 |
-
):
|
72 |
-
super().__init__(
|
73 |
-
transformer=transformer, scheduler=scheduler, vae=vae, text_encoder=text_encoder, tokenizer=tokenizer
|
74 |
-
)
|
75 |
-
self.register_modules(controlnet=controlnet)
|
76 |
-
|
77 |
-
def prepare_image(
|
78 |
-
self,
|
79 |
-
image,
|
80 |
-
width,
|
81 |
-
height,
|
82 |
-
batch_size,
|
83 |
-
num_images_per_prompt,
|
84 |
-
device,
|
85 |
-
dtype,
|
86 |
-
do_classifier_free_guidance=False,
|
87 |
-
guess_mode=False,
|
88 |
-
):
|
89 |
-
if isinstance(image, torch.Tensor):
|
90 |
-
pass
|
91 |
-
else:
|
92 |
-
image = self.image_processor.preprocess(image, height=height, width=width)
|
93 |
-
|
94 |
-
image_batch_size = image.shape[0]
|
95 |
-
|
96 |
-
if image_batch_size == 1:
|
97 |
-
repeat_by = batch_size
|
98 |
-
else:
|
99 |
-
# image batch size is the same as prompt batch size
|
100 |
-
repeat_by = num_images_per_prompt
|
101 |
-
|
102 |
-
image = image.repeat_interleave(repeat_by, dim=0)
|
103 |
-
|
104 |
-
image = image.to(device=device, dtype=dtype)
|
105 |
-
|
106 |
-
if do_classifier_free_guidance and not guess_mode:
|
107 |
-
image = torch.cat([image] * 2)
|
108 |
-
|
109 |
-
return image
|
110 |
-
|
111 |
-
def prepare_control(self, control_image, width, height, batch_size, num_images_per_prompt, device, control_mode):
|
112 |
-
num_channels_latents = self.transformer.config.in_channels // 4
|
113 |
-
control_image = self.prepare_image(
|
114 |
-
image=control_image,
|
115 |
-
width=width,
|
116 |
-
height=height,
|
117 |
-
batch_size=batch_size * num_images_per_prompt,
|
118 |
-
num_images_per_prompt=num_images_per_prompt,
|
119 |
-
device=device,
|
120 |
-
dtype=self.vae.dtype,
|
121 |
-
)
|
122 |
-
height, width = control_image.shape[-2:]
|
123 |
-
|
124 |
-
# vae encode
|
125 |
-
control_image = self.vae.encode(control_image).latent_dist.sample()
|
126 |
-
control_image = (control_image - self.vae.config.shift_factor) * self.vae.config.scaling_factor
|
127 |
-
|
128 |
-
# pack
|
129 |
-
height_control_image, width_control_image = control_image.shape[2:]
|
130 |
-
control_image = self._pack_latents(
|
131 |
-
control_image,
|
132 |
-
batch_size * num_images_per_prompt,
|
133 |
-
num_channels_latents,
|
134 |
-
height_control_image,
|
135 |
-
width_control_image,
|
136 |
-
)
|
137 |
-
|
138 |
-
# Here we ensure that `control_mode` has the same length as the control_image.
|
139 |
-
if control_mode is not None:
|
140 |
-
if not isinstance(control_mode, int):
|
141 |
-
raise ValueError(" For `FluxControlNet`, `control_mode` should be an `int` or `None`")
|
142 |
-
control_mode = torch.tensor(control_mode).to(device, dtype=torch.long)
|
143 |
-
control_mode = control_mode.view(-1, 1).expand(control_image.shape[0], 1)
|
144 |
-
|
145 |
-
return control_image, control_mode
|
146 |
-
|
147 |
-
def prepare_multi_control(self, control_image, width, height, batch_size, num_images_per_prompt, device, control_mode):
|
148 |
-
num_channels_latents = self.transformer.config.in_channels // 4
|
149 |
-
control_images = []
|
150 |
-
for i, control_image_ in enumerate(control_image):
|
151 |
-
control_image_ = self.prepare_image(
|
152 |
-
image=control_image_,
|
153 |
-
width=width,
|
154 |
-
height=height,
|
155 |
-
batch_size=batch_size * num_images_per_prompt,
|
156 |
-
num_images_per_prompt=num_images_per_prompt,
|
157 |
-
device=device,
|
158 |
-
dtype=self.vae.dtype,
|
159 |
-
)
|
160 |
-
height, width = control_image_.shape[-2:]
|
161 |
-
|
162 |
-
# vae encode
|
163 |
-
control_image_ = self.vae.encode(control_image_).latent_dist.sample()
|
164 |
-
control_image_ = (control_image_ - self.vae.config.shift_factor) * self.vae.config.scaling_factor
|
165 |
-
|
166 |
-
# pack
|
167 |
-
height_control_image, width_control_image = control_image_.shape[2:]
|
168 |
-
control_image_ = self._pack_latents(
|
169 |
-
control_image_,
|
170 |
-
batch_size * num_images_per_prompt,
|
171 |
-
num_channels_latents,
|
172 |
-
height_control_image,
|
173 |
-
width_control_image,
|
174 |
-
)
|
175 |
-
control_images.append(control_image_)
|
176 |
-
|
177 |
-
control_image = control_images
|
178 |
-
|
179 |
-
# Here we ensure that `control_mode` has the same length as the control_image.
|
180 |
-
if isinstance(control_mode, list) and len(control_mode) != len(control_image):
|
181 |
-
raise ValueError(
|
182 |
-
"For Multi-ControlNet, `control_mode` must be a list of the same "
|
183 |
-
+ " length as the number of controlnets (control images) specified"
|
184 |
-
)
|
185 |
-
if not isinstance(control_mode, list):
|
186 |
-
control_mode = [control_mode] * len(control_image)
|
187 |
-
# set control mode
|
188 |
-
control_modes = []
|
189 |
-
for cmode in control_mode:
|
190 |
-
if cmode is None:
|
191 |
-
cmode = -1
|
192 |
-
control_mode = torch.tensor(cmode).expand(control_images[0].shape[0]).to(device, dtype=torch.long)
|
193 |
-
control_modes.append(control_mode)
|
194 |
-
control_mode = control_modes
|
195 |
-
|
196 |
-
return control_image, control_mode
|
197 |
-
|
198 |
-
def get_controlnet_keep(self, timesteps, control_guidance_start, control_guidance_end):
|
199 |
-
controlnet_keep = []
|
200 |
-
for i in range(len(timesteps)):
|
201 |
-
keeps = [
|
202 |
-
1.0 - float(i / len(timesteps) < s or (i + 1) / len(timesteps) > e)
|
203 |
-
for s, e in zip(control_guidance_start, control_guidance_end)
|
204 |
-
]
|
205 |
-
controlnet_keep.append(keeps[0] if isinstance(self.controlnet, FluxControlNetModel) else keeps)
|
206 |
-
return controlnet_keep
|
207 |
-
|
208 |
-
def get_control_start_end(self, control_guidance_start, control_guidance_end):
|
209 |
-
if not isinstance(control_guidance_start, list) and isinstance(control_guidance_end, list):
|
210 |
-
control_guidance_start = len(control_guidance_end) * [control_guidance_start]
|
211 |
-
elif not isinstance(control_guidance_end, list) and isinstance(control_guidance_start, list):
|
212 |
-
control_guidance_end = len(control_guidance_start) * [control_guidance_end]
|
213 |
-
elif not isinstance(control_guidance_start, list) and not isinstance(control_guidance_end, list):
|
214 |
-
mult = 1 # TODO - why is this 1?
|
215 |
-
control_guidance_start, control_guidance_end = (
|
216 |
-
mult * [control_guidance_start],
|
217 |
-
mult * [control_guidance_end],
|
218 |
-
)
|
219 |
-
|
220 |
-
return control_guidance_start, control_guidance_end
|
221 |
-
|
222 |
-
@torch.no_grad()
|
223 |
-
def __call__(
|
224 |
-
self,
|
225 |
-
prompt: Union[str, List[str]] = None,
|
226 |
-
height: Optional[int] = None,
|
227 |
-
width: Optional[int] = None,
|
228 |
-
num_inference_steps: int = 30,
|
229 |
-
timesteps: List[int] = None,
|
230 |
-
guidance_scale: float = 3.5,
|
231 |
-
control_guidance_start: Union[float, List[float]] = 0.0,
|
232 |
-
control_guidance_end: Union[float, List[float]] = 1.0,
|
233 |
-
control_image: Optional[PipelineImageInput] = None,
|
234 |
-
control_mode: Optional[Union[int, List[int]]] = None,
|
235 |
-
controlnet_conditioning_scale: Union[float, List[float]] = 1.0,
|
236 |
-
negative_prompt: Optional[Union[str, List[str]]] = None,
|
237 |
-
num_images_per_prompt: Optional[int] = 1,
|
238 |
-
generator: Optional[Union[torch.Generator, List[torch.Generator]]] = None,
|
239 |
-
latents: Optional[torch.FloatTensor] = None,
|
240 |
-
prompt_embeds: Optional[torch.FloatTensor] = None,
|
241 |
-
negative_prompt_embeds: Optional[torch.FloatTensor] = None,
|
242 |
-
output_type: Optional[str] = "pil",
|
243 |
-
return_dict: bool = True,
|
244 |
-
joint_attention_kwargs: Optional[Dict[str, Any]] = None,
|
245 |
-
callback_on_step_end: Optional[Callable[[int, int, Dict], None]] = None,
|
246 |
-
callback_on_step_end_tensor_inputs: List[str] = ["latents"],
|
247 |
-
max_sequence_length: int = 128,
|
248 |
-
):
|
249 |
-
r"""
|
250 |
-
Function invoked when calling the pipeline for generation.
|
251 |
-
|
252 |
-
Args:
|
253 |
-
prompt (`str` or `List[str]`, *optional*):
|
254 |
-
The prompt or prompts to guide the image generation. If not defined, one has to pass `prompt_embeds`.
|
255 |
-
instead.
|
256 |
-
height (`int`, *optional*, defaults to self.unet.config.sample_size * self.vae_scale_factor):
|
257 |
-
The height in pixels of the generated image. This is set to 1024 by default for the best results.
|
258 |
-
width (`int`, *optional*, defaults to self.unet.config.sample_size * self.vae_scale_factor):
|
259 |
-
The width in pixels of the generated image. This is set to 1024 by default for the best results.
|
260 |
-
num_inference_steps (`int`, *optional*, defaults to 50):
|
261 |
-
The number of denoising steps. More denoising steps usually lead to a higher quality image at the
|
262 |
-
expense of slower inference.
|
263 |
-
timesteps (`List[int]`, *optional*):
|
264 |
-
Custom timesteps to use for the denoising process with schedulers which support a `timesteps` argument
|
265 |
-
in their `set_timesteps` method. If not defined, the default behavior when `num_inference_steps` is
|
266 |
-
passed will be used. Must be in descending order.
|
267 |
-
guidance_scale (`float`, *optional*, defaults to 5.0):
|
268 |
-
Guidance scale as defined in [Classifier-Free Diffusion Guidance](https://arxiv.org/abs/2207.12598).
|
269 |
-
`guidance_scale` is defined as `w` of equation 2. of [Imagen
|
270 |
-
Paper](https://arxiv.org/pdf/2205.11487.pdf). Guidance scale is enabled by setting `guidance_scale >
|
271 |
-
1`. Higher guidance scale encourages to generate images that are closely linked to the text `prompt`,
|
272 |
-
usually at the expense of lower image quality.
|
273 |
-
negative_prompt (`str` or `List[str]`, *optional*):
|
274 |
-
The prompt or prompts not to guide the image generation. If not defined, one has to pass
|
275 |
-
`negative_prompt_embeds` instead. Ignored when not using guidance (i.e., ignored if `guidance_scale` is
|
276 |
-
less than `1`).
|
277 |
-
num_images_per_prompt (`int`, *optional*, defaults to 1):
|
278 |
-
The number of images to generate per prompt.
|
279 |
-
generator (`torch.Generator` or `List[torch.Generator]`, *optional*):
|
280 |
-
One or a list of [torch generator(s)](https://pytorch.org/docs/stable/generated/torch.Generator.html)
|
281 |
-
to make generation deterministic.
|
282 |
-
latents (`torch.FloatTensor`, *optional*):
|
283 |
-
Pre-generated noisy latents, sampled from a Gaussian distribution, to be used as inputs for image
|
284 |
-
generation. Can be used to tweak the same generation with different prompts. If not provided, a latents
|
285 |
-
tensor will ge generated by sampling using the supplied random `generator`.
|
286 |
-
prompt_embeds (`torch.FloatTensor`, *optional*):
|
287 |
-
Pre-generated text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt weighting. If not
|
288 |
-
provided, text embeddings will be generated from `prompt` input argument.
|
289 |
-
negative_prompt_embeds (`torch.FloatTensor`, *optional*):
|
290 |
-
Pre-generated negative text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt
|
291 |
-
weighting. If not provided, negative_prompt_embeds will be generated from `negative_prompt` input
|
292 |
-
argument.
|
293 |
-
output_type (`str`, *optional*, defaults to `"pil"`):
|
294 |
-
The output format of the generate image. Choose between
|
295 |
-
[PIL](https://pillow.readthedocs.io/en/stable/): `PIL.Image.Image` or `np.array`.
|
296 |
-
return_dict (`bool`, *optional*, defaults to `True`):
|
297 |
-
Whether or not to return a [`~pipelines.stable_diffusion_xl.StableDiffusionXLPipelineOutput`] instead
|
298 |
-
of a plain tuple.
|
299 |
-
joint_attention_kwargs (`dict`, *optional*):
|
300 |
-
A kwargs dictionary that if specified is passed along to the `AttentionProcessor` as defined under
|
301 |
-
`self.processor` in
|
302 |
-
[diffusers.models.attention_processor](https://github.com/huggingface/diffusers/blob/main/src/diffusers/models/attention_processor.py).
|
303 |
-
callback_on_step_end (`Callable`, *optional*):
|
304 |
-
A function that calls at the end of each denoising steps during the inference. The function is called
|
305 |
-
with the following arguments: `callback_on_step_end(self: DiffusionPipeline, step: int, timestep: int,
|
306 |
-
callback_kwargs: Dict)`. `callback_kwargs` will include a list of all tensors as specified by
|
307 |
-
`callback_on_step_end_tensor_inputs`.
|
308 |
-
callback_on_step_end_tensor_inputs (`List`, *optional*):
|
309 |
-
The list of tensor inputs for the `callback_on_step_end` function. The tensors specified in the list
|
310 |
-
will be passed as `callback_kwargs` argument. You will only be able to include variables listed in the
|
311 |
-
`._callback_tensor_inputs` attribute of your pipeline class.
|
312 |
-
max_sequence_length (`int` defaults to 256): Maximum sequence length to use with the `prompt`.
|
313 |
-
|
314 |
-
Examples:
|
315 |
-
|
316 |
-
Returns:
|
317 |
-
[`~pipelines.stable_diffusion_xl.StableDiffusionXLPipelineOutput`] or `tuple`:
|
318 |
-
[`~pipelines.stable_diffusion_xl.StableDiffusionXLPipelineOutput`] if `return_dict` is True, otherwise a
|
319 |
-
`tuple`. When returning a tuple, the first element is a list with the generated images.
|
320 |
-
"""
|
321 |
-
|
322 |
-
height = height or self.default_sample_size * self.vae_scale_factor
|
323 |
-
width = width or self.default_sample_size * self.vae_scale_factor
|
324 |
-
control_guidance_start, control_guidance_end = self.get_control_start_end(
|
325 |
-
control_guidance_start=control_guidance_start, control_guidance_end=control_guidance_end
|
326 |
-
)
|
327 |
-
|
328 |
-
# 1. Check inputs. Raise error if not correct
|
329 |
-
self.check_inputs(
|
330 |
-
prompt,
|
331 |
-
height,
|
332 |
-
width,
|
333 |
-
negative_prompt=negative_prompt,
|
334 |
-
prompt_embeds=prompt_embeds,
|
335 |
-
negative_prompt_embeds=negative_prompt_embeds,
|
336 |
-
callback_on_step_end_tensor_inputs=callback_on_step_end_tensor_inputs,
|
337 |
-
max_sequence_length=max_sequence_length,
|
338 |
-
)
|
339 |
-
|
340 |
-
self._guidance_scale = guidance_scale
|
341 |
-
self._joint_attention_kwargs = joint_attention_kwargs
|
342 |
-
self._interrupt = False
|
343 |
-
|
344 |
-
# 2. Define call parameters
|
345 |
-
if prompt is not None and isinstance(prompt, str):
|
346 |
-
batch_size = 1
|
347 |
-
elif prompt is not None and isinstance(prompt, list):
|
348 |
-
batch_size = len(prompt)
|
349 |
-
else:
|
350 |
-
batch_size = prompt_embeds.shape[0]
|
351 |
-
|
352 |
-
device = self._execution_device
|
353 |
-
|
354 |
-
lora_scale = self.joint_attention_kwargs.get("scale", None) if self.joint_attention_kwargs is not None else None
|
355 |
-
|
356 |
-
(prompt_embeds, negative_prompt_embeds, text_ids) = self.encode_prompt(
|
357 |
-
prompt=prompt,
|
358 |
-
negative_prompt=negative_prompt,
|
359 |
-
do_classifier_free_guidance=self.do_classifier_free_guidance,
|
360 |
-
prompt_embeds=prompt_embeds,
|
361 |
-
negative_prompt_embeds=negative_prompt_embeds,
|
362 |
-
device=device,
|
363 |
-
num_images_per_prompt=num_images_per_prompt,
|
364 |
-
max_sequence_length=max_sequence_length,
|
365 |
-
lora_scale=lora_scale,
|
366 |
-
)
|
367 |
-
|
368 |
-
if self.do_classifier_free_guidance:
|
369 |
-
prompt_embeds = torch.cat([negative_prompt_embeds, prompt_embeds], dim=0)
|
370 |
-
|
371 |
-
# 3. Prepare control image
|
372 |
-
if control_image is not None:
|
373 |
-
if isinstance(self.controlnet, FluxControlNetModel):
|
374 |
-
control_image, control_mode = self.prepare_control(
|
375 |
-
control_image=control_image,
|
376 |
-
width=width,
|
377 |
-
height=height,
|
378 |
-
batch_size=batch_size,
|
379 |
-
num_images_per_prompt=num_images_per_prompt,
|
380 |
-
device=device,
|
381 |
-
control_mode=control_mode,
|
382 |
-
)
|
383 |
-
elif isinstance(self.controlnet, FluxMultiControlNetModel):
|
384 |
-
control_image, control_mode = self.prepare_multi_control(
|
385 |
-
control_image=control_image,
|
386 |
-
width=width,
|
387 |
-
height=height,
|
388 |
-
batch_size=batch_size,
|
389 |
-
num_images_per_prompt=num_images_per_prompt,
|
390 |
-
device=device,
|
391 |
-
control_mode=control_mode,
|
392 |
-
)
|
393 |
-
|
394 |
-
# 4. Prepare timesteps
|
395 |
-
# Sample from training sigmas
|
396 |
-
sigmas = get_original_sigmas(
|
397 |
-
num_train_timesteps=self.scheduler.config.num_train_timesteps, num_inference_steps=num_inference_steps
|
398 |
-
)
|
399 |
-
timesteps, num_inference_steps = retrieve_timesteps(
|
400 |
-
self.scheduler, num_inference_steps, device, timesteps, sigmas=sigmas
|
401 |
-
)
|
402 |
-
num_warmup_steps = max(len(timesteps) - num_inference_steps * self.scheduler.order, 0)
|
403 |
-
self._num_timesteps = len(timesteps)
|
404 |
-
|
405 |
-
# 5. Prepare latent variables
|
406 |
-
num_channels_latents = self.transformer.config.in_channels // 4 # due to patch=2, we devide by 4
|
407 |
-
latents, latent_image_ids = self.prepare_latents(
|
408 |
-
batch_size=batch_size * num_images_per_prompt,
|
409 |
-
num_channels_latents=num_channels_latents,
|
410 |
-
height=height,
|
411 |
-
width=width,
|
412 |
-
dtype=prompt_embeds.dtype,
|
413 |
-
device=device,
|
414 |
-
generator=generator,
|
415 |
-
latents=latents,
|
416 |
-
)
|
417 |
-
|
418 |
-
# 6. Create tensor stating which controlnets to keep
|
419 |
-
if control_image is not None:
|
420 |
-
controlnet_keep = self.get_controlnet_keep(
|
421 |
-
timesteps=timesteps,
|
422 |
-
control_guidance_start=control_guidance_start,
|
423 |
-
control_guidance_end=control_guidance_end,
|
424 |
-
)
|
425 |
-
|
426 |
-
# EYAL - added the CFG loop
|
427 |
-
# 7. Denoising loop
|
428 |
-
with self.progress_bar(total=num_inference_steps) as progress_bar:
|
429 |
-
for i, t in enumerate(timesteps):
|
430 |
-
if self.interrupt:
|
431 |
-
continue
|
432 |
-
|
433 |
-
# expand the latents if we are doing classifier free guidance
|
434 |
-
latent_model_input = torch.cat([latents] * 2) if self.do_classifier_free_guidance else latents
|
435 |
-
# if type(self.scheduler) != FlowMatchEulerDiscreteScheduler:
|
436 |
-
if not isinstance(self.scheduler, FlowMatchEulerDiscreteScheduler):
|
437 |
-
latent_model_input = self.scheduler.scale_model_input(latent_model_input, t)
|
438 |
-
|
439 |
-
# broadcast to batch dimension in a way that's compatible with ONNX/Core ML
|
440 |
-
timestep = t.expand(latent_model_input.shape[0])
|
441 |
-
|
442 |
-
# Handling ControlNet
|
443 |
-
if control_image is not None:
|
444 |
-
if isinstance(controlnet_keep[i], list):
|
445 |
-
if isinstance(controlnet_conditioning_scale, list):
|
446 |
-
cond_scale = controlnet_conditioning_scale
|
447 |
-
else:
|
448 |
-
cond_scale = [c * s for c, s in zip(controlnet_conditioning_scale, controlnet_keep[i])]
|
449 |
-
else:
|
450 |
-
controlnet_cond_scale = controlnet_conditioning_scale
|
451 |
-
if isinstance(controlnet_cond_scale, list):
|
452 |
-
controlnet_cond_scale = controlnet_cond_scale[0]
|
453 |
-
cond_scale = controlnet_cond_scale * controlnet_keep[i]
|
454 |
-
|
455 |
-
# controlnet
|
456 |
-
controlnet_block_samples, controlnet_single_block_samples = self.controlnet(
|
457 |
-
hidden_states=latents,
|
458 |
-
controlnet_cond=control_image,
|
459 |
-
controlnet_mode=control_mode,
|
460 |
-
conditioning_scale=cond_scale,
|
461 |
-
timestep=timestep,
|
462 |
-
# guidance=guidance,
|
463 |
-
# pooled_projections=pooled_prompt_embeds,
|
464 |
-
encoder_hidden_states=prompt_embeds,
|
465 |
-
txt_ids=text_ids,
|
466 |
-
img_ids=latent_image_ids,
|
467 |
-
joint_attention_kwargs=self.joint_attention_kwargs,
|
468 |
-
return_dict=False,
|
469 |
-
)
|
470 |
-
else:
|
471 |
-
controlnet_block_samples, controlnet_single_block_samples = None, None
|
472 |
-
|
473 |
-
# This is predicts "v" from flow-matching
|
474 |
-
noise_pred = self.transformer(
|
475 |
-
hidden_states=latent_model_input,
|
476 |
-
timestep=timestep,
|
477 |
-
encoder_hidden_states=prompt_embeds,
|
478 |
-
joint_attention_kwargs=self.joint_attention_kwargs,
|
479 |
-
return_dict=False,
|
480 |
-
txt_ids=text_ids,
|
481 |
-
img_ids=latent_image_ids,
|
482 |
-
controlnet_block_samples=controlnet_block_samples,
|
483 |
-
controlnet_single_block_samples=controlnet_single_block_samples,
|
484 |
-
)[0]
|
485 |
-
|
486 |
-
# perform guidance
|
487 |
-
if self.do_classifier_free_guidance:
|
488 |
-
noise_pred_uncond, noise_pred_text = noise_pred.chunk(2)
|
489 |
-
noise_pred = noise_pred_uncond + self.guidance_scale * (noise_pred_text - noise_pred_uncond)
|
490 |
-
|
491 |
-
# compute the previous noisy sample x_t -> x_t-1
|
492 |
-
latents_dtype = latents.dtype
|
493 |
-
latents = self.scheduler.step(noise_pred, t, latents, return_dict=False)[0]
|
494 |
-
|
495 |
-
if latents.dtype != latents_dtype:
|
496 |
-
if torch.backends.mps.is_available():
|
497 |
-
# some platforms (eg. apple mps) misbehave due to a pytorch bug: https://github.com/pytorch/pytorch/pull/99272
|
498 |
-
latents = latents.to(latents_dtype)
|
499 |
-
|
500 |
-
if callback_on_step_end is not None:
|
501 |
-
callback_kwargs = {}
|
502 |
-
for k in callback_on_step_end_tensor_inputs:
|
503 |
-
callback_kwargs[k] = locals()[k]
|
504 |
-
callback_outputs = callback_on_step_end(self, i, t, callback_kwargs)
|
505 |
-
|
506 |
-
latents = callback_outputs.pop("latents", latents)
|
507 |
-
prompt_embeds = callback_outputs.pop("prompt_embeds", prompt_embeds)
|
508 |
-
negative_prompt_embeds = callback_outputs.pop("negative_prompt_embeds", negative_prompt_embeds)
|
509 |
-
|
510 |
-
# call the callback, if provided
|
511 |
-
if i == len(timesteps) - 1 or ((i + 1) > num_warmup_steps and (i + 1) % self.scheduler.order == 0):
|
512 |
-
progress_bar.update()
|
513 |
-
|
514 |
-
if XLA_AVAILABLE:
|
515 |
-
xm.mark_step()
|
516 |
-
|
517 |
-
if output_type == "latent":
|
518 |
-
image = latents
|
519 |
-
|
520 |
-
else:
|
521 |
-
latents = self._unpack_latents(latents, height, width, self.vae_scale_factor)
|
522 |
-
latents = (latents / self.vae.config.scaling_factor) + self.vae.config.shift_factor
|
523 |
-
image = self.vae.decode(latents.to(dtype=self.vae.dtype), return_dict=False)[0]
|
524 |
-
image = self.image_processor.postprocess(image, output_type=output_type)
|
525 |
-
|
526 |
-
# Offload all models
|
527 |
-
self.maybe_free_model_hooks()
|
528 |
-
|
529 |
-
if not return_dict:
|
530 |
-
return (image,)
|
531 |
-
|
532 |
-
return FluxPipelineOutput(images=image)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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