sample_id
stringlengths
21
196
text
stringlengths
105
936k
metadata
dict
category
stringclasses
6 values
huggingface/diffusers:tests/models/transformers/test_models_transformer_bria_fibo.py
# coding=utf-8 # Copyright 2025 HuggingFace Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest import torch from diffusers import BriaFiboTransformer2DModel from ...testing_utils import enable_full_determinism, torch_device from ..test_modeling_common import ModelTesterMixin enable_full_determinism() class BriaFiboTransformerTests(ModelTesterMixin, unittest.TestCase): model_class = BriaFiboTransformer2DModel main_input_name = "hidden_states" # We override the items here because the transformer under consideration is small. model_split_percents = [0.8, 0.7, 0.7] # Skip setting testing with default: AttnProcessor uses_custom_attn_processor = True @property def dummy_input(self): batch_size = 1 num_latent_channels = 48 num_image_channels = 3 height = width = 16 sequence_length = 32 embedding_dim = 64 hidden_states = torch.randn((batch_size, height * width, num_latent_channels)).to(torch_device) encoder_hidden_states = torch.randn((batch_size, sequence_length, embedding_dim)).to(torch_device) text_ids = torch.randn((sequence_length, num_image_channels)).to(torch_device) image_ids = torch.randn((height * width, num_image_channels)).to(torch_device) timestep = torch.tensor([1.0]).to(torch_device).expand(batch_size) return { "hidden_states": hidden_states, "encoder_hidden_states": encoder_hidden_states, "img_ids": image_ids, "txt_ids": text_ids, "timestep": timestep, "text_encoder_layers": [encoder_hidden_states[:, :, :32], encoder_hidden_states[:, :, :32]], } @property def input_shape(self): return (16, 16) @property def output_shape(self): return (256, 48) def prepare_init_args_and_inputs_for_common(self): init_dict = { "patch_size": 1, "in_channels": 48, "num_layers": 1, "num_single_layers": 1, "attention_head_dim": 8, "num_attention_heads": 2, "joint_attention_dim": 64, "text_encoder_dim": 32, "pooled_projection_dim": None, "axes_dims_rope": [0, 4, 4], } inputs_dict = self.dummy_input return init_dict, inputs_dict def test_gradient_checkpointing_is_applied(self): expected_set = {"BriaFiboTransformer2DModel"} super().test_gradient_checkpointing_is_applied(expected_set=expected_set)
{ "repo_id": "huggingface/diffusers", "file_path": "tests/models/transformers/test_models_transformer_bria_fibo.py", "license": "Apache License 2.0", "lines": 72, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
test
huggingface/diffusers:tests/pipelines/bria_fibo/test_pipeline_bria_fibo.py
# Copyright 2024 Bria AI and The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest import numpy as np import torch from transformers import AutoTokenizer from transformers.models.smollm3.modeling_smollm3 import SmolLM3Config, SmolLM3ForCausalLM from diffusers import ( AutoencoderKLWan, BriaFiboPipeline, FlowMatchEulerDiscreteScheduler, ) from diffusers.models.transformers.transformer_bria_fibo import BriaFiboTransformer2DModel from tests.pipelines.test_pipelines_common import PipelineTesterMixin from ...testing_utils import ( enable_full_determinism, torch_device, ) enable_full_determinism() class BriaFiboPipelineFastTests(PipelineTesterMixin, unittest.TestCase): pipeline_class = BriaFiboPipeline params = frozenset(["prompt", "height", "width", "guidance_scale"]) batch_params = frozenset(["prompt"]) test_xformers_attention = False test_layerwise_casting = False test_group_offloading = False supports_dduf = False def get_dummy_components(self): torch.manual_seed(0) transformer = BriaFiboTransformer2DModel( patch_size=1, in_channels=16, num_layers=1, num_single_layers=1, attention_head_dim=8, num_attention_heads=2, joint_attention_dim=64, text_encoder_dim=32, pooled_projection_dim=None, axes_dims_rope=[0, 4, 4], ) torch.manual_seed(0) vae = AutoencoderKLWan( base_dim=160, decoder_base_dim=256, num_res_blocks=2, out_channels=12, patch_size=2, scale_factor_spatial=16, scale_factor_temporal=4, temperal_downsample=[False, True, True], z_dim=16, ) scheduler = FlowMatchEulerDiscreteScheduler() torch.manual_seed(0) text_encoder = SmolLM3ForCausalLM(SmolLM3Config(hidden_size=32)) tokenizer = AutoTokenizer.from_pretrained("hf-internal-testing/tiny-random-t5") components = { "scheduler": scheduler, "text_encoder": text_encoder, "tokenizer": tokenizer, "transformer": transformer, "vae": vae, } return components def get_dummy_inputs(self, device, seed=0): if str(device).startswith("mps"): generator = torch.manual_seed(seed) else: generator = torch.Generator(device="cpu").manual_seed(seed) inputs = { "prompt": "{'text': 'A painting of a squirrel eating a burger'}", "negative_prompt": "bad, ugly", "generator": generator, "num_inference_steps": 2, "guidance_scale": 5.0, "height": 32, "width": 32, "output_type": "np", } return inputs @unittest.skip(reason="will not be supported due to dim-fusion") def test_encode_prompt_works_in_isolation(self): pass def test_bria_fibo_different_prompts(self): pipe = self.pipeline_class(**self.get_dummy_components()) pipe = pipe.to(torch_device) inputs = self.get_dummy_inputs(torch_device) output_same_prompt = pipe(**inputs).images[0] inputs = self.get_dummy_inputs(torch_device) inputs["prompt"] = "a different prompt" output_different_prompts = pipe(**inputs).images[0] max_diff = np.abs(output_same_prompt - output_different_prompts).max() assert max_diff > 1e-6 def test_image_output_shape(self): pipe = self.pipeline_class(**self.get_dummy_components()) pipe = pipe.to(torch_device) inputs = self.get_dummy_inputs(torch_device) height_width_pairs = [(32, 32), (64, 64), (32, 64)] for height, width in height_width_pairs: expected_height = height expected_width = width inputs.update({"height": height, "width": width}) image = pipe(**inputs).images[0] output_height, output_width, _ = image.shape assert (output_height, output_width) == (expected_height, expected_width)
{ "repo_id": "huggingface/diffusers", "file_path": "tests/pipelines/bria_fibo/test_pipeline_bria_fibo.py", "license": "Apache License 2.0", "lines": 117, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
test
huggingface/diffusers:tests/pipelines/kandinsky5/test_kandinsky5.py
# Copyright 2025 The Kandinsky Team and The HuggingFace Team. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest import torch from transformers import ( AutoProcessor, CLIPTextConfig, CLIPTextModel, CLIPTokenizer, Qwen2_5_VLConfig, Qwen2_5_VLForConditionalGeneration, ) from diffusers import ( AutoencoderKLHunyuanVideo, FlowMatchEulerDiscreteScheduler, Kandinsky5T2VPipeline, Kandinsky5Transformer3DModel, ) from ...testing_utils import ( enable_full_determinism, ) from ..test_pipelines_common import PipelineTesterMixin enable_full_determinism() class Kandinsky5T2VPipelineFastTests(PipelineTesterMixin, unittest.TestCase): pipeline_class = Kandinsky5T2VPipeline batch_params = ["prompt", "negative_prompt"] params = frozenset(["prompt", "height", "width", "num_frames", "num_inference_steps", "guidance_scale"]) required_optional_params = { "num_inference_steps", "generator", "latents", "return_dict", "callback_on_step_end", "callback_on_step_end_tensor_inputs", "max_sequence_length", } test_xformers_attention = False supports_optional_components = True supports_dduf = False test_attention_slicing = False def get_dummy_components(self): torch.manual_seed(0) vae = AutoencoderKLHunyuanVideo( act_fn="silu", block_out_channels=[32, 64], down_block_types=[ "HunyuanVideoDownBlock3D", "HunyuanVideoDownBlock3D", ], in_channels=3, latent_channels=16, layers_per_block=1, mid_block_add_attention=False, norm_num_groups=32, out_channels=3, scaling_factor=0.476986, spatial_compression_ratio=8, temporal_compression_ratio=4, up_block_types=[ "HunyuanVideoUpBlock3D", "HunyuanVideoUpBlock3D", ], ) scheduler = FlowMatchEulerDiscreteScheduler(shift=7.0) qwen_hidden_size = 32 torch.manual_seed(0) qwen_config = Qwen2_5_VLConfig( text_config={ "hidden_size": qwen_hidden_size, "intermediate_size": qwen_hidden_size, "num_hidden_layers": 2, "num_attention_heads": 2, "num_key_value_heads": 2, "rope_scaling": { "mrope_section": [2, 2, 4], "rope_type": "default", "type": "default", }, "rope_theta": 1000000.0, }, vision_config={ "depth": 2, "hidden_size": qwen_hidden_size, "intermediate_size": qwen_hidden_size, "num_heads": 2, "out_hidden_size": qwen_hidden_size, }, hidden_size=qwen_hidden_size, vocab_size=152064, vision_end_token_id=151653, vision_start_token_id=151652, vision_token_id=151654, ) text_encoder = Qwen2_5_VLForConditionalGeneration(qwen_config) tokenizer = AutoProcessor.from_pretrained("hf-internal-testing/tiny-random-Qwen2VLForConditionalGeneration") clip_hidden_size = 16 torch.manual_seed(0) clip_config = CLIPTextConfig( bos_token_id=0, eos_token_id=2, hidden_size=clip_hidden_size, intermediate_size=16, layer_norm_eps=1e-05, num_attention_heads=2, num_hidden_layers=2, pad_token_id=1, vocab_size=1000, projection_dim=clip_hidden_size, ) text_encoder_2 = CLIPTextModel(clip_config) tokenizer_2 = CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip") torch.manual_seed(0) transformer = Kandinsky5Transformer3DModel( in_visual_dim=16, in_text_dim=qwen_hidden_size, in_text_dim2=clip_hidden_size, time_dim=16, out_visual_dim=16, patch_size=(1, 2, 2), model_dim=16, ff_dim=32, num_text_blocks=1, num_visual_blocks=2, axes_dims=(1, 1, 2), visual_cond=False, attention_type="regular", ) return { "vae": vae, "text_encoder": text_encoder, "tokenizer": tokenizer, "text_encoder_2": text_encoder_2, "tokenizer_2": tokenizer_2, "transformer": transformer, "scheduler": scheduler, } def get_dummy_inputs(self, device, seed=0): if str(device).startswith("mps"): generator = torch.manual_seed(seed) else: generator = torch.Generator(device=device).manual_seed(seed) return { "prompt": "a red square", "height": 32, "width": 32, "num_frames": 5, "num_inference_steps": 2, "guidance_scale": 4.0, "generator": generator, "output_type": "pt", "max_sequence_length": 8, } def test_inference(self): device = "cpu" components = self.get_dummy_components() pipe = self.pipeline_class(**components) pipe.to(device) pipe.set_progress_bar_config(disable=None) inputs = self.get_dummy_inputs(device) output = pipe(**inputs) video = output.frames[0] self.assertEqual(video.shape, (3, 3, 16, 16)) def test_attention_slicing_forward_pass(self): pass @unittest.skip("Only SDPA or NABLA (flex)") def test_xformers_memory_efficient_attention(self): pass @unittest.skip("TODO:Test does not work") def test_encode_prompt_works_in_isolation(self): pass @unittest.skip("TODO: revisit") def test_inference_batch_single_identical(self): pass
{ "repo_id": "huggingface/diffusers", "file_path": "tests/pipelines/kandinsky5/test_kandinsky5.py", "license": "Apache License 2.0", "lines": 184, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
test
huggingface/diffusers:scripts/convert_hunyuan_image_to_diffusers.py
import argparse import logging import torch from safetensors import safe_open from diffusers import AutoencoderKLHunyuanImage, AutoencoderKLHunyuanImageRefiner, HunyuanImageTransformer2DModel logger = logging.getLogger(__name__) # pylint: disable=invalid-name """ Usage examples ============== python scripts/convert_hunyuan_image_to_diffusers.py \ --model_type hunyuanimage2.1 \ --transformer_checkpoint_path "/raid/yiyi/HunyuanImage-2.1/ckpts/dit/hunyuanimage2.1.safetensors" \ --vae_checkpoint_path "HunyuanImage-2.1/ckpts/vae/vae_2_1/pytorch_model.ckpt" \ --output_path "/raid/yiyi/test-hy21-diffusers" \ --dtype fp32 python scripts/convert_hunyuan_image_to_diffusers.py \ --model_type hunyuanimage2.1-distilled \ --transformer_checkpoint_path "/raid/yiyi/HunyuanImage-2.1/ckpts/dit/hunyuanimage2.1-distilled.safetensors" \ --vae_checkpoint_path "/raid/yiyi/HunyuanImage-2.1/ckpts/vae/vae_2_1/pytorch_model.ckpt" \ --output_path "/raid/yiyi/test-hy21-distilled-diffusers" \ --dtype fp32 python scripts/convert_hunyuan_image_to_diffusers.py \ --model_type hunyuanimage-refiner \ --transformer_checkpoint_path "/raid/yiyi/HunyuanImage-2.1/ckpts/dit/hunyuanimage-refiner.safetensors" \ --vae_checkpoint_path "/raid/yiyi/HunyuanImage-2.1/ckpts/vae/vae_refiner/pytorch_model.pt" \ --output_path "/raid/yiyi/test-hy2-refiner-diffusers" \ --dtype fp32 """ parser = argparse.ArgumentParser() parser.add_argument( "--model_type", type=str, default=None ) # hunyuanimage2.1, hunyuanimage2.1-distilled, hunyuanimage-refiner parser.add_argument("--transformer_checkpoint_path", default=None, type=str) # ckpts/dit/hunyuanimage2.1.safetensors parser.add_argument("--vae_checkpoint_path", default=None, type=str) # ckpts/vae/vae_2_1/pytorch_model.ckpt parser.add_argument("--output_path", type=str) parser.add_argument("--dtype", type=str, default="fp32") args = parser.parse_args() dtype = torch.bfloat16 if args.dtype == "bf16" else torch.float32 # copied from https://github.com/Tencent-Hunyuan/HunyuanImage-2.1/hyimage/models/hunyuan/modules/hunyuanimage_dit.py#L21 def convert_hunyuan_dict_for_tensor_parallel(state_dict): """ Convert a Hunyuan model state dict to be compatible with tensor parallel architectures. Args: state_dict: Original state dict Returns: new_dict: Converted state dict """ new_dict = {} for k, w in state_dict.items(): if k.startswith("double_blocks") and "attn_qkv.weight" in k: hidden_size = w.shape[1] k1 = k.replace("attn_qkv.weight", "attn_q.weight") w1 = w[:hidden_size, :] new_dict[k1] = w1 k2 = k.replace("attn_qkv.weight", "attn_k.weight") w2 = w[hidden_size : 2 * hidden_size, :] new_dict[k2] = w2 k3 = k.replace("attn_qkv.weight", "attn_v.weight") w3 = w[-hidden_size:, :] new_dict[k3] = w3 elif k.startswith("double_blocks") and "attn_qkv.bias" in k: hidden_size = w.shape[0] // 3 k1 = k.replace("attn_qkv.bias", "attn_q.bias") w1 = w[:hidden_size] new_dict[k1] = w1 k2 = k.replace("attn_qkv.bias", "attn_k.bias") w2 = w[hidden_size : 2 * hidden_size] new_dict[k2] = w2 k3 = k.replace("attn_qkv.bias", "attn_v.bias") w3 = w[-hidden_size:] new_dict[k3] = w3 elif k.startswith("single_blocks") and "linear1" in k: hidden_size = state_dict[k.replace("linear1", "linear2")].shape[0] k1 = k.replace("linear1", "linear1_q") w1 = w[:hidden_size] new_dict[k1] = w1 k2 = k.replace("linear1", "linear1_k") w2 = w[hidden_size : 2 * hidden_size] new_dict[k2] = w2 k3 = k.replace("linear1", "linear1_v") w3 = w[2 * hidden_size : 3 * hidden_size] new_dict[k3] = w3 k4 = k.replace("linear1", "linear1_mlp") w4 = w[3 * hidden_size :] new_dict[k4] = w4 elif k.startswith("single_blocks") and "linear2" in k: k1 = k.replace("linear2", "linear2.fc") new_dict[k1] = w else: new_dict[k] = w return new_dict def load_original_vae_checkpoint(args): # "ckpts/vae/vae_2_1/pytorch_model.ckpt" state_dict = torch.load(args.vae_checkpoint_path) if "state_dict" in state_dict: state_dict = state_dict["state_dict"] vae_state_dict = {} for k, v in state_dict.items(): if k.startswith("vae."): vae_state_dict[k.replace("vae.", "")] = v for k, v in vae_state_dict.items(): if "weight" in k: if len(v.shape) == 5 and v.shape[2] == 1: vae_state_dict[k] = v.squeeze(2) else: vae_state_dict[k] = v else: vae_state_dict[k] = v return vae_state_dict def load_original_refiner_vae_checkpoint(args): # "ckpts/vae/vae_refiner/pytorch_model.pt" state_dict = torch.load(args.vae_checkpoint_path) if "state_dict" in state_dict: state_dict = state_dict["state_dict"] vae_state_dict = {} for k, v in state_dict.items(): if k.startswith("vae."): vae_state_dict[k.replace("vae.", "")] = v return vae_state_dict def load_original_transformer_checkpoint(args): # ckpts/dit/hunyuanimage-refiner.safetensors" # ckpts/dit/hunyuanimage2.1.safetensors" state_dict = {} with safe_open(args.transformer_checkpoint_path, framework="pt", device="cpu") as f: for key in f.keys(): state_dict[key] = f.get_tensor(key) if args.model_type == "hunyuanimage-2.1": state_dict = convert_hunyuan_dict_for_tensor_parallel(state_dict) return state_dict def convert_hunyuan_image_transformer_checkpoint_to_diffusers( original_state_dict, use_byt5=True, guidance_distilled=False, use_meanflow=False ): converted_state_dict = {} # 1. byt5_in -> context_embedder_2 if use_byt5: converted_state_dict["context_embedder_2.norm.weight"] = original_state_dict.pop("byt5_in.layernorm.weight") converted_state_dict["context_embedder_2.norm.bias"] = original_state_dict.pop("byt5_in.layernorm.bias") converted_state_dict["context_embedder_2.linear_1.weight"] = original_state_dict.pop("byt5_in.fc1.weight") converted_state_dict["context_embedder_2.linear_1.bias"] = original_state_dict.pop("byt5_in.fc1.bias") converted_state_dict["context_embedder_2.linear_2.weight"] = original_state_dict.pop("byt5_in.fc2.weight") converted_state_dict["context_embedder_2.linear_2.bias"] = original_state_dict.pop("byt5_in.fc2.bias") converted_state_dict["context_embedder_2.linear_3.weight"] = original_state_dict.pop("byt5_in.fc3.weight") converted_state_dict["context_embedder_2.linear_3.bias"] = original_state_dict.pop("byt5_in.fc3.bias") # 2. img_in -> x_embedder converted_state_dict["x_embedder.proj.weight"] = original_state_dict.pop("img_in.proj.weight") converted_state_dict["x_embedder.proj.bias"] = original_state_dict.pop("img_in.proj.bias") # 3. txt_in -> context_embedder (complex mapping) # txt_in.input_embedder -> context_embedder.proj_in converted_state_dict["context_embedder.proj_in.weight"] = original_state_dict.pop("txt_in.input_embedder.weight") converted_state_dict["context_embedder.proj_in.bias"] = original_state_dict.pop("txt_in.input_embedder.bias") # txt_in.t_embedder -> context_embedder.time_text_embed.timestep_embedder converted_state_dict["context_embedder.time_text_embed.timestep_embedder.linear_1.weight"] = ( original_state_dict.pop("txt_in.t_embedder.mlp.0.weight") ) converted_state_dict["context_embedder.time_text_embed.timestep_embedder.linear_1.bias"] = original_state_dict.pop( "txt_in.t_embedder.mlp.0.bias" ) converted_state_dict["context_embedder.time_text_embed.timestep_embedder.linear_2.weight"] = ( original_state_dict.pop("txt_in.t_embedder.mlp.2.weight") ) converted_state_dict["context_embedder.time_text_embed.timestep_embedder.linear_2.bias"] = original_state_dict.pop( "txt_in.t_embedder.mlp.2.bias" ) # txt_in.c_embedder -> context_embedder.time_text_embed.text_embedder converted_state_dict["context_embedder.time_text_embed.text_embedder.linear_1.weight"] = original_state_dict.pop( "txt_in.c_embedder.linear_1.weight" ) converted_state_dict["context_embedder.time_text_embed.text_embedder.linear_1.bias"] = original_state_dict.pop( "txt_in.c_embedder.linear_1.bias" ) converted_state_dict["context_embedder.time_text_embed.text_embedder.linear_2.weight"] = original_state_dict.pop( "txt_in.c_embedder.linear_2.weight" ) converted_state_dict["context_embedder.time_text_embed.text_embedder.linear_2.bias"] = original_state_dict.pop( "txt_in.c_embedder.linear_2.bias" ) # txt_in.individual_token_refiner -> context_embedder.token_refiner for i in range(2): # 2 refiner blocks block_prefix = f"context_embedder.token_refiner.refiner_blocks.{i}." # norm1 converted_state_dict[f"{block_prefix}norm1.weight"] = original_state_dict.pop( f"txt_in.individual_token_refiner.blocks.{i}.norm1.weight" ) converted_state_dict[f"{block_prefix}norm1.bias"] = original_state_dict.pop( f"txt_in.individual_token_refiner.blocks.{i}.norm1.bias" ) # norm2 converted_state_dict[f"{block_prefix}norm2.weight"] = original_state_dict.pop( f"txt_in.individual_token_refiner.blocks.{i}.norm2.weight" ) converted_state_dict[f"{block_prefix}norm2.bias"] = original_state_dict.pop( f"txt_in.individual_token_refiner.blocks.{i}.norm2.bias" ) # Split QKV qkv_weight = original_state_dict.pop(f"txt_in.individual_token_refiner.blocks.{i}.self_attn_qkv.weight") qkv_bias = original_state_dict.pop(f"txt_in.individual_token_refiner.blocks.{i}.self_attn_qkv.bias") q_weight, k_weight, v_weight = torch.chunk(qkv_weight, 3, dim=0) q_bias, k_bias, v_bias = torch.chunk(qkv_bias, 3, dim=0) converted_state_dict[f"{block_prefix}attn.to_q.weight"] = q_weight converted_state_dict[f"{block_prefix}attn.to_q.bias"] = q_bias converted_state_dict[f"{block_prefix}attn.to_k.weight"] = k_weight converted_state_dict[f"{block_prefix}attn.to_k.bias"] = k_bias converted_state_dict[f"{block_prefix}attn.to_v.weight"] = v_weight converted_state_dict[f"{block_prefix}attn.to_v.bias"] = v_bias # attn projection converted_state_dict[f"{block_prefix}attn.to_out.0.weight"] = original_state_dict.pop( f"txt_in.individual_token_refiner.blocks.{i}.self_attn_proj.weight" ) converted_state_dict[f"{block_prefix}attn.to_out.0.bias"] = original_state_dict.pop( f"txt_in.individual_token_refiner.blocks.{i}.self_attn_proj.bias" ) # MLP converted_state_dict[f"{block_prefix}ff.net.0.proj.weight"] = original_state_dict.pop( f"txt_in.individual_token_refiner.blocks.{i}.mlp.fc1.weight" ) converted_state_dict[f"{block_prefix}ff.net.0.proj.bias"] = original_state_dict.pop( f"txt_in.individual_token_refiner.blocks.{i}.mlp.fc1.bias" ) converted_state_dict[f"{block_prefix}ff.net.2.weight"] = original_state_dict.pop( f"txt_in.individual_token_refiner.blocks.{i}.mlp.fc2.weight" ) converted_state_dict[f"{block_prefix}ff.net.2.bias"] = original_state_dict.pop( f"txt_in.individual_token_refiner.blocks.{i}.mlp.fc2.bias" ) # norm_out converted_state_dict[f"{block_prefix}norm_out.linear.weight"] = original_state_dict.pop( f"txt_in.individual_token_refiner.blocks.{i}.adaLN_modulation.1.weight" ) converted_state_dict[f"{block_prefix}norm_out.linear.bias"] = original_state_dict.pop( f"txt_in.individual_token_refiner.blocks.{i}.adaLN_modulation.1.bias" ) # 4. time_in -> time_text_embed.timestep_embedder converted_state_dict["time_guidance_embed.timestep_embedder.linear_1.weight"] = original_state_dict.pop( "time_in.mlp.0.weight" ) converted_state_dict["time_guidance_embed.timestep_embedder.linear_1.bias"] = original_state_dict.pop( "time_in.mlp.0.bias" ) converted_state_dict["time_guidance_embed.timestep_embedder.linear_2.weight"] = original_state_dict.pop( "time_in.mlp.2.weight" ) converted_state_dict["time_guidance_embed.timestep_embedder.linear_2.bias"] = original_state_dict.pop( "time_in.mlp.2.bias" ) # time_r_in -> time_guidance_embed.timestep_r_embedder if use_meanflow: converted_state_dict["time_guidance_embed.timestep_embedder_r.linear_1.weight"] = original_state_dict.pop( "time_r_in.mlp.0.weight" ) converted_state_dict["time_guidance_embed.timestep_embedder_r.linear_1.bias"] = original_state_dict.pop( "time_r_in.mlp.0.bias" ) converted_state_dict["time_guidance_embed.timestep_embedder_r.linear_2.weight"] = original_state_dict.pop( "time_r_in.mlp.2.weight" ) converted_state_dict["time_guidance_embed.timestep_embedder_r.linear_2.bias"] = original_state_dict.pop( "time_r_in.mlp.2.bias" ) # guidance_in -> time_guidance_embed.guidance_embedder if guidance_distilled: converted_state_dict["time_guidance_embed.guidance_embedder.linear_1.weight"] = original_state_dict.pop( "guidance_in.mlp.0.weight" ) converted_state_dict["time_guidance_embed.guidance_embedder.linear_1.bias"] = original_state_dict.pop( "guidance_in.mlp.0.bias" ) converted_state_dict["time_guidance_embed.guidance_embedder.linear_2.weight"] = original_state_dict.pop( "guidance_in.mlp.2.weight" ) converted_state_dict["time_guidance_embed.guidance_embedder.linear_2.bias"] = original_state_dict.pop( "guidance_in.mlp.2.bias" ) # 5. double_blocks -> transformer_blocks for i in range(20): # 20 double blocks block_prefix = f"transformer_blocks.{i}." # norm1 (img_mod) converted_state_dict[f"{block_prefix}norm1.linear.weight"] = original_state_dict.pop( f"double_blocks.{i}.img_mod.linear.weight" ) converted_state_dict[f"{block_prefix}norm1.linear.bias"] = original_state_dict.pop( f"double_blocks.{i}.img_mod.linear.bias" ) # norm1_context (txt_mod) converted_state_dict[f"{block_prefix}norm1_context.linear.weight"] = original_state_dict.pop( f"double_blocks.{i}.txt_mod.linear.weight" ) converted_state_dict[f"{block_prefix}norm1_context.linear.bias"] = original_state_dict.pop( f"double_blocks.{i}.txt_mod.linear.bias" ) # img attention converted_state_dict[f"{block_prefix}attn.to_q.weight"] = original_state_dict.pop( f"double_blocks.{i}.img_attn_q.weight" ) converted_state_dict[f"{block_prefix}attn.to_q.bias"] = original_state_dict.pop( f"double_blocks.{i}.img_attn_q.bias" ) converted_state_dict[f"{block_prefix}attn.to_k.weight"] = original_state_dict.pop( f"double_blocks.{i}.img_attn_k.weight" ) converted_state_dict[f"{block_prefix}attn.to_k.bias"] = original_state_dict.pop( f"double_blocks.{i}.img_attn_k.bias" ) converted_state_dict[f"{block_prefix}attn.to_v.weight"] = original_state_dict.pop( f"double_blocks.{i}.img_attn_v.weight" ) converted_state_dict[f"{block_prefix}attn.to_v.bias"] = original_state_dict.pop( f"double_blocks.{i}.img_attn_v.bias" ) # img attention norms converted_state_dict[f"{block_prefix}attn.norm_q.weight"] = original_state_dict.pop( f"double_blocks.{i}.img_attn_q_norm.weight" ) converted_state_dict[f"{block_prefix}attn.norm_k.weight"] = original_state_dict.pop( f"double_blocks.{i}.img_attn_k_norm.weight" ) # img attention projection converted_state_dict[f"{block_prefix}attn.to_out.0.weight"] = original_state_dict.pop( f"double_blocks.{i}.img_attn_proj.weight" ) converted_state_dict[f"{block_prefix}attn.to_out.0.bias"] = original_state_dict.pop( f"double_blocks.{i}.img_attn_proj.bias" ) # img MLP converted_state_dict[f"{block_prefix}ff.net.0.proj.weight"] = original_state_dict.pop( f"double_blocks.{i}.img_mlp.fc1.weight" ) converted_state_dict[f"{block_prefix}ff.net.0.proj.bias"] = original_state_dict.pop( f"double_blocks.{i}.img_mlp.fc1.bias" ) converted_state_dict[f"{block_prefix}ff.net.2.weight"] = original_state_dict.pop( f"double_blocks.{i}.img_mlp.fc2.weight" ) converted_state_dict[f"{block_prefix}ff.net.2.bias"] = original_state_dict.pop( f"double_blocks.{i}.img_mlp.fc2.bias" ) # txt attention (additional projections) converted_state_dict[f"{block_prefix}attn.add_q_proj.weight"] = original_state_dict.pop( f"double_blocks.{i}.txt_attn_q.weight" ) converted_state_dict[f"{block_prefix}attn.add_q_proj.bias"] = original_state_dict.pop( f"double_blocks.{i}.txt_attn_q.bias" ) converted_state_dict[f"{block_prefix}attn.add_k_proj.weight"] = original_state_dict.pop( f"double_blocks.{i}.txt_attn_k.weight" ) converted_state_dict[f"{block_prefix}attn.add_k_proj.bias"] = original_state_dict.pop( f"double_blocks.{i}.txt_attn_k.bias" ) converted_state_dict[f"{block_prefix}attn.add_v_proj.weight"] = original_state_dict.pop( f"double_blocks.{i}.txt_attn_v.weight" ) converted_state_dict[f"{block_prefix}attn.add_v_proj.bias"] = original_state_dict.pop( f"double_blocks.{i}.txt_attn_v.bias" ) # txt attention norms converted_state_dict[f"{block_prefix}attn.norm_added_q.weight"] = original_state_dict.pop( f"double_blocks.{i}.txt_attn_q_norm.weight" ) converted_state_dict[f"{block_prefix}attn.norm_added_k.weight"] = original_state_dict.pop( f"double_blocks.{i}.txt_attn_k_norm.weight" ) # txt attention projection converted_state_dict[f"{block_prefix}attn.to_add_out.weight"] = original_state_dict.pop( f"double_blocks.{i}.txt_attn_proj.weight" ) converted_state_dict[f"{block_prefix}attn.to_add_out.bias"] = original_state_dict.pop( f"double_blocks.{i}.txt_attn_proj.bias" ) # txt MLP (ff_context) converted_state_dict[f"{block_prefix}ff_context.net.0.proj.weight"] = original_state_dict.pop( f"double_blocks.{i}.txt_mlp.fc1.weight" ) converted_state_dict[f"{block_prefix}ff_context.net.0.proj.bias"] = original_state_dict.pop( f"double_blocks.{i}.txt_mlp.fc1.bias" ) converted_state_dict[f"{block_prefix}ff_context.net.2.weight"] = original_state_dict.pop( f"double_blocks.{i}.txt_mlp.fc2.weight" ) converted_state_dict[f"{block_prefix}ff_context.net.2.bias"] = original_state_dict.pop( f"double_blocks.{i}.txt_mlp.fc2.bias" ) # 6. single_blocks -> single_transformer_blocks for i in range(40): # 40 single blocks block_prefix = f"single_transformer_blocks.{i}." # norm converted_state_dict[f"{block_prefix}norm.linear.weight"] = original_state_dict.pop( f"single_blocks.{i}.modulation.linear.weight" ) converted_state_dict[f"{block_prefix}norm.linear.bias"] = original_state_dict.pop( f"single_blocks.{i}.modulation.linear.bias" ) # attention Q, K, V converted_state_dict[f"{block_prefix}attn.to_q.weight"] = original_state_dict.pop( f"single_blocks.{i}.linear1_q.weight" ) converted_state_dict[f"{block_prefix}attn.to_q.bias"] = original_state_dict.pop( f"single_blocks.{i}.linear1_q.bias" ) converted_state_dict[f"{block_prefix}attn.to_k.weight"] = original_state_dict.pop( f"single_blocks.{i}.linear1_k.weight" ) converted_state_dict[f"{block_prefix}attn.to_k.bias"] = original_state_dict.pop( f"single_blocks.{i}.linear1_k.bias" ) converted_state_dict[f"{block_prefix}attn.to_v.weight"] = original_state_dict.pop( f"single_blocks.{i}.linear1_v.weight" ) converted_state_dict[f"{block_prefix}attn.to_v.bias"] = original_state_dict.pop( f"single_blocks.{i}.linear1_v.bias" ) # attention norms converted_state_dict[f"{block_prefix}attn.norm_q.weight"] = original_state_dict.pop( f"single_blocks.{i}.q_norm.weight" ) converted_state_dict[f"{block_prefix}attn.norm_k.weight"] = original_state_dict.pop( f"single_blocks.{i}.k_norm.weight" ) # MLP projection converted_state_dict[f"{block_prefix}proj_mlp.weight"] = original_state_dict.pop( f"single_blocks.{i}.linear1_mlp.weight" ) converted_state_dict[f"{block_prefix}proj_mlp.bias"] = original_state_dict.pop( f"single_blocks.{i}.linear1_mlp.bias" ) # output projection converted_state_dict[f"{block_prefix}proj_out.weight"] = original_state_dict.pop( f"single_blocks.{i}.linear2.fc.weight" ) converted_state_dict[f"{block_prefix}proj_out.bias"] = original_state_dict.pop( f"single_blocks.{i}.linear2.fc.bias" ) # 7. final_layer -> norm_out + proj_out converted_state_dict["proj_out.weight"] = original_state_dict.pop("final_layer.linear.weight") converted_state_dict["proj_out.bias"] = original_state_dict.pop("final_layer.linear.bias") shift_w, scale_w = original_state_dict.pop("final_layer.adaLN_modulation.1.weight").chunk(2, dim=0) shift_b, scale_b = original_state_dict.pop("final_layer.adaLN_modulation.1.bias").chunk(2, dim=0) converted_state_dict["norm_out.linear.weight"] = torch.cat([scale_w, shift_w], dim=0) converted_state_dict["norm_out.linear.bias"] = torch.cat([scale_b, shift_b], dim=0) return converted_state_dict, original_state_dict def convert_hunyuan_image_vae_checkpoint_to_diffusers( original_state_dict, block_out_channels=[128, 256, 512, 512, 1024, 1024], layers_per_block=2 ): """Convert original VAE state dict to Diffusers format.""" converted = {} # 1. Encoder # 1.1 conv_in converted["encoder.conv_in.weight"] = original_state_dict.pop("encoder.conv_in.weight") converted["encoder.conv_in.bias"] = original_state_dict.pop("encoder.conv_in.bias") # 1.2 down blocks diffusers_block_idx = 0 for block_index in range(len(block_out_channels)): for resnet_block_index in range(layers_per_block): orig_prefix = f"encoder.down.{block_index}.block.{resnet_block_index}" diff_prefix = f"encoder.down_blocks.{diffusers_block_idx}" # resnet blocks converted[f"{diff_prefix}.norm1.weight"] = original_state_dict.pop(f"{orig_prefix}.norm1.weight") converted[f"{diff_prefix}.norm1.bias"] = original_state_dict.pop(f"{orig_prefix}.norm1.bias") converted[f"{diff_prefix}.conv1.weight"] = original_state_dict.pop(f"{orig_prefix}.conv1.weight") converted[f"{diff_prefix}.conv1.bias"] = original_state_dict.pop(f"{orig_prefix}.conv1.bias") converted[f"{diff_prefix}.norm2.weight"] = original_state_dict.pop(f"{orig_prefix}.norm2.weight") converted[f"{diff_prefix}.norm2.bias"] = original_state_dict.pop(f"{orig_prefix}.norm2.bias") converted[f"{diff_prefix}.conv2.weight"] = original_state_dict.pop(f"{orig_prefix}.conv2.weight") converted[f"{diff_prefix}.conv2.bias"] = original_state_dict.pop(f"{orig_prefix}.conv2.bias") diffusers_block_idx += 1 # downsample blocks if f"encoder.down.{block_index}.downsample.conv.weight" in original_state_dict: converted[f"encoder.down_blocks.{diffusers_block_idx}.conv.weight"] = original_state_dict.pop( f"encoder.down.{block_index}.downsample.conv.weight" ) converted[f"encoder.down_blocks.{diffusers_block_idx}.conv.bias"] = original_state_dict.pop( f"encoder.down.{block_index}.downsample.conv.bias" ) diffusers_block_idx += 1 # 1.3 mid block converted["encoder.mid_block.resnets.0.norm1.weight"] = original_state_dict.pop("encoder.mid.block_1.norm1.weight") converted["encoder.mid_block.resnets.0.norm1.bias"] = original_state_dict.pop("encoder.mid.block_1.norm1.bias") converted["encoder.mid_block.resnets.0.conv1.weight"] = original_state_dict.pop("encoder.mid.block_1.conv1.weight") converted["encoder.mid_block.resnets.0.conv1.bias"] = original_state_dict.pop("encoder.mid.block_1.conv1.bias") converted["encoder.mid_block.resnets.0.norm2.weight"] = original_state_dict.pop("encoder.mid.block_1.norm2.weight") converted["encoder.mid_block.resnets.0.norm2.bias"] = original_state_dict.pop("encoder.mid.block_1.norm2.bias") converted["encoder.mid_block.resnets.0.conv2.weight"] = original_state_dict.pop("encoder.mid.block_1.conv2.weight") converted["encoder.mid_block.resnets.0.conv2.bias"] = original_state_dict.pop("encoder.mid.block_1.conv2.bias") converted["encoder.mid_block.resnets.1.norm1.weight"] = original_state_dict.pop("encoder.mid.block_2.norm1.weight") converted["encoder.mid_block.resnets.1.norm1.bias"] = original_state_dict.pop("encoder.mid.block_2.norm1.bias") converted["encoder.mid_block.resnets.1.conv1.weight"] = original_state_dict.pop("encoder.mid.block_2.conv1.weight") converted["encoder.mid_block.resnets.1.conv1.bias"] = original_state_dict.pop("encoder.mid.block_2.conv1.bias") converted["encoder.mid_block.resnets.1.norm2.weight"] = original_state_dict.pop("encoder.mid.block_2.norm2.weight") converted["encoder.mid_block.resnets.1.norm2.bias"] = original_state_dict.pop("encoder.mid.block_2.norm2.bias") converted["encoder.mid_block.resnets.1.conv2.weight"] = original_state_dict.pop("encoder.mid.block_2.conv2.weight") converted["encoder.mid_block.resnets.1.conv2.bias"] = original_state_dict.pop("encoder.mid.block_2.conv2.bias") converted["encoder.mid_block.attentions.0.norm.weight"] = original_state_dict.pop("encoder.mid.attn_1.norm.weight") converted["encoder.mid_block.attentions.0.norm.bias"] = original_state_dict.pop("encoder.mid.attn_1.norm.bias") converted["encoder.mid_block.attentions.0.to_q.weight"] = original_state_dict.pop("encoder.mid.attn_1.q.weight") converted["encoder.mid_block.attentions.0.to_q.bias"] = original_state_dict.pop("encoder.mid.attn_1.q.bias") converted["encoder.mid_block.attentions.0.to_k.weight"] = original_state_dict.pop("encoder.mid.attn_1.k.weight") converted["encoder.mid_block.attentions.0.to_k.bias"] = original_state_dict.pop("encoder.mid.attn_1.k.bias") converted["encoder.mid_block.attentions.0.to_v.weight"] = original_state_dict.pop("encoder.mid.attn_1.v.weight") converted["encoder.mid_block.attentions.0.to_v.bias"] = original_state_dict.pop("encoder.mid.attn_1.v.bias") converted["encoder.mid_block.attentions.0.proj.weight"] = original_state_dict.pop( "encoder.mid.attn_1.proj_out.weight" ) converted["encoder.mid_block.attentions.0.proj.bias"] = original_state_dict.pop("encoder.mid.attn_1.proj_out.bias") # 1.4 encoder output converted["encoder.norm_out.weight"] = original_state_dict.pop("encoder.norm_out.weight") converted["encoder.norm_out.bias"] = original_state_dict.pop("encoder.norm_out.bias") converted["encoder.conv_out.weight"] = original_state_dict.pop("encoder.conv_out.weight") converted["encoder.conv_out.bias"] = original_state_dict.pop("encoder.conv_out.bias") # 2. Decoder # 2.1 conv_in converted["decoder.conv_in.weight"] = original_state_dict.pop("decoder.conv_in.weight") converted["decoder.conv_in.bias"] = original_state_dict.pop("decoder.conv_in.bias") # 2.2 mid block converted["decoder.mid_block.resnets.0.norm1.weight"] = original_state_dict.pop("decoder.mid.block_1.norm1.weight") converted["decoder.mid_block.resnets.0.norm1.bias"] = original_state_dict.pop("decoder.mid.block_1.norm1.bias") converted["decoder.mid_block.resnets.0.conv1.weight"] = original_state_dict.pop("decoder.mid.block_1.conv1.weight") converted["decoder.mid_block.resnets.0.conv1.bias"] = original_state_dict.pop("decoder.mid.block_1.conv1.bias") converted["decoder.mid_block.resnets.0.norm2.weight"] = original_state_dict.pop("decoder.mid.block_1.norm2.weight") converted["decoder.mid_block.resnets.0.norm2.bias"] = original_state_dict.pop("decoder.mid.block_1.norm2.bias") converted["decoder.mid_block.resnets.0.conv2.weight"] = original_state_dict.pop("decoder.mid.block_1.conv2.weight") converted["decoder.mid_block.resnets.0.conv2.bias"] = original_state_dict.pop("decoder.mid.block_1.conv2.bias") converted["decoder.mid_block.resnets.1.norm1.weight"] = original_state_dict.pop("decoder.mid.block_2.norm1.weight") converted["decoder.mid_block.resnets.1.norm1.bias"] = original_state_dict.pop("decoder.mid.block_2.norm1.bias") converted["decoder.mid_block.resnets.1.conv1.weight"] = original_state_dict.pop("decoder.mid.block_2.conv1.weight") converted["decoder.mid_block.resnets.1.conv1.bias"] = original_state_dict.pop("decoder.mid.block_2.conv1.bias") converted["decoder.mid_block.resnets.1.norm2.weight"] = original_state_dict.pop("decoder.mid.block_2.norm2.weight") converted["decoder.mid_block.resnets.1.norm2.bias"] = original_state_dict.pop("decoder.mid.block_2.norm2.bias") converted["decoder.mid_block.resnets.1.conv2.weight"] = original_state_dict.pop("decoder.mid.block_2.conv2.weight") converted["decoder.mid_block.resnets.1.conv2.bias"] = original_state_dict.pop("decoder.mid.block_2.conv2.bias") converted["decoder.mid_block.attentions.0.norm.weight"] = original_state_dict.pop("decoder.mid.attn_1.norm.weight") converted["decoder.mid_block.attentions.0.norm.bias"] = original_state_dict.pop("decoder.mid.attn_1.norm.bias") converted["decoder.mid_block.attentions.0.to_q.weight"] = original_state_dict.pop("decoder.mid.attn_1.q.weight") converted["decoder.mid_block.attentions.0.to_q.bias"] = original_state_dict.pop("decoder.mid.attn_1.q.bias") converted["decoder.mid_block.attentions.0.to_k.weight"] = original_state_dict.pop("decoder.mid.attn_1.k.weight") converted["decoder.mid_block.attentions.0.to_k.bias"] = original_state_dict.pop("decoder.mid.attn_1.k.bias") converted["decoder.mid_block.attentions.0.to_v.weight"] = original_state_dict.pop("decoder.mid.attn_1.v.weight") converted["decoder.mid_block.attentions.0.to_v.bias"] = original_state_dict.pop("decoder.mid.attn_1.v.bias") converted["decoder.mid_block.attentions.0.proj.weight"] = original_state_dict.pop( "decoder.mid.attn_1.proj_out.weight" ) converted["decoder.mid_block.attentions.0.proj.bias"] = original_state_dict.pop("decoder.mid.attn_1.proj_out.bias") # 2.3 up blocks diffusers_block_idx = 0 for up_block_index in range(len(block_out_channels)): # resnet blocks for resnet_block_index in range(layers_per_block + 1): orig_prefix = f"decoder.up.{up_block_index}.block.{resnet_block_index}" diff_prefix = f"decoder.up_blocks.{diffusers_block_idx}" converted[f"{diff_prefix}.norm1.weight"] = original_state_dict.pop(f"{orig_prefix}.norm1.weight") converted[f"{diff_prefix}.norm1.bias"] = original_state_dict.pop(f"{orig_prefix}.norm1.bias") converted[f"{diff_prefix}.conv1.weight"] = original_state_dict.pop(f"{orig_prefix}.conv1.weight") converted[f"{diff_prefix}.conv1.bias"] = original_state_dict.pop(f"{orig_prefix}.conv1.bias") converted[f"{diff_prefix}.norm2.weight"] = original_state_dict.pop(f"{orig_prefix}.norm2.weight") converted[f"{diff_prefix}.norm2.bias"] = original_state_dict.pop(f"{orig_prefix}.norm2.bias") converted[f"{diff_prefix}.conv2.weight"] = original_state_dict.pop(f"{orig_prefix}.conv2.weight") converted[f"{diff_prefix}.conv2.bias"] = original_state_dict.pop(f"{orig_prefix}.conv2.bias") diffusers_block_idx += 1 # upsample blocks if f"decoder.up.{up_block_index}.upsample.conv.weight" in original_state_dict: converted[f"decoder.up_blocks.{diffusers_block_idx}.conv.weight"] = original_state_dict.pop( f"decoder.up.{up_block_index}.upsample.conv.weight" ) converted[f"decoder.up_blocks.{diffusers_block_idx}.conv.bias"] = original_state_dict.pop( f"decoder.up.{up_block_index}.upsample.conv.bias" ) diffusers_block_idx += 1 # 2.4 decoder output converted["decoder.norm_out.weight"] = original_state_dict.pop("decoder.norm_out.weight") converted["decoder.norm_out.bias"] = original_state_dict.pop("decoder.norm_out.bias") converted["decoder.conv_out.weight"] = original_state_dict.pop("decoder.conv_out.weight") converted["decoder.conv_out.bias"] = original_state_dict.pop("decoder.conv_out.bias") return converted, original_state_dict def convert_hunyuan_image_refiner_vae_checkpoint_to_diffusers( original_state_dict, block_out_channels=[128, 256, 512, 1024, 1024], layers_per_block=2 ): converted = {} # 1. Encoder # 1.1 conv_in converted["encoder.conv_in.conv.weight"] = original_state_dict.pop("encoder.conv_in.conv.weight") converted["encoder.conv_in.conv.bias"] = original_state_dict.pop("encoder.conv_in.conv.bias") # 1.2 Down blocks for down_block_index in range(len(block_out_channels)): # 0 to 4 # ResNet blocks for resnet_block_index in range(layers_per_block): # 0 to 1 converted[f"encoder.down_blocks.{down_block_index}.resnets.{resnet_block_index}.norm1.gamma"] = ( original_state_dict.pop(f"encoder.down.{down_block_index}.block.{resnet_block_index}.norm1.gamma") ) converted[f"encoder.down_blocks.{down_block_index}.resnets.{resnet_block_index}.conv1.conv.weight"] = ( original_state_dict.pop( f"encoder.down.{down_block_index}.block.{resnet_block_index}.conv1.conv.weight" ) ) converted[f"encoder.down_blocks.{down_block_index}.resnets.{resnet_block_index}.conv1.conv.bias"] = ( original_state_dict.pop(f"encoder.down.{down_block_index}.block.{resnet_block_index}.conv1.conv.bias") ) converted[f"encoder.down_blocks.{down_block_index}.resnets.{resnet_block_index}.norm2.gamma"] = ( original_state_dict.pop(f"encoder.down.{down_block_index}.block.{resnet_block_index}.norm2.gamma") ) converted[f"encoder.down_blocks.{down_block_index}.resnets.{resnet_block_index}.conv2.conv.weight"] = ( original_state_dict.pop( f"encoder.down.{down_block_index}.block.{resnet_block_index}.conv2.conv.weight" ) ) converted[f"encoder.down_blocks.{down_block_index}.resnets.{resnet_block_index}.conv2.conv.bias"] = ( original_state_dict.pop(f"encoder.down.{down_block_index}.block.{resnet_block_index}.conv2.conv.bias") ) # Downsample (if exists) if f"encoder.down.{down_block_index}.downsample.conv.conv.weight" in original_state_dict: converted[f"encoder.down_blocks.{down_block_index}.downsamplers.0.conv.conv.weight"] = ( original_state_dict.pop(f"encoder.down.{down_block_index}.downsample.conv.conv.weight") ) converted[f"encoder.down_blocks.{down_block_index}.downsamplers.0.conv.conv.bias"] = ( original_state_dict.pop(f"encoder.down.{down_block_index}.downsample.conv.conv.bias") ) # 1.3 Mid block converted["encoder.mid_block.resnets.0.norm1.gamma"] = original_state_dict.pop("encoder.mid.block_1.norm1.gamma") converted["encoder.mid_block.resnets.0.conv1.conv.weight"] = original_state_dict.pop( "encoder.mid.block_1.conv1.conv.weight" ) converted["encoder.mid_block.resnets.0.conv1.conv.bias"] = original_state_dict.pop( "encoder.mid.block_1.conv1.conv.bias" ) converted["encoder.mid_block.resnets.0.norm2.gamma"] = original_state_dict.pop("encoder.mid.block_1.norm2.gamma") converted["encoder.mid_block.resnets.0.conv2.conv.weight"] = original_state_dict.pop( "encoder.mid.block_1.conv2.conv.weight" ) converted["encoder.mid_block.resnets.0.conv2.conv.bias"] = original_state_dict.pop( "encoder.mid.block_1.conv2.conv.bias" ) converted["encoder.mid_block.resnets.1.norm1.gamma"] = original_state_dict.pop("encoder.mid.block_2.norm1.gamma") converted["encoder.mid_block.resnets.1.conv1.conv.weight"] = original_state_dict.pop( "encoder.mid.block_2.conv1.conv.weight" ) converted["encoder.mid_block.resnets.1.conv1.conv.bias"] = original_state_dict.pop( "encoder.mid.block_2.conv1.conv.bias" ) converted["encoder.mid_block.resnets.1.norm2.gamma"] = original_state_dict.pop("encoder.mid.block_2.norm2.gamma") converted["encoder.mid_block.resnets.1.conv2.conv.weight"] = original_state_dict.pop( "encoder.mid.block_2.conv2.conv.weight" ) converted["encoder.mid_block.resnets.1.conv2.conv.bias"] = original_state_dict.pop( "encoder.mid.block_2.conv2.conv.bias" ) # Attention block converted["encoder.mid_block.attentions.0.norm.gamma"] = original_state_dict.pop("encoder.mid.attn_1.norm.gamma") converted["encoder.mid_block.attentions.0.to_q.weight"] = original_state_dict.pop("encoder.mid.attn_1.q.weight") converted["encoder.mid_block.attentions.0.to_q.bias"] = original_state_dict.pop("encoder.mid.attn_1.q.bias") converted["encoder.mid_block.attentions.0.to_k.weight"] = original_state_dict.pop("encoder.mid.attn_1.k.weight") converted["encoder.mid_block.attentions.0.to_k.bias"] = original_state_dict.pop("encoder.mid.attn_1.k.bias") converted["encoder.mid_block.attentions.0.to_v.weight"] = original_state_dict.pop("encoder.mid.attn_1.v.weight") converted["encoder.mid_block.attentions.0.to_v.bias"] = original_state_dict.pop("encoder.mid.attn_1.v.bias") converted["encoder.mid_block.attentions.0.proj_out.weight"] = original_state_dict.pop( "encoder.mid.attn_1.proj_out.weight" ) converted["encoder.mid_block.attentions.0.proj_out.bias"] = original_state_dict.pop( "encoder.mid.attn_1.proj_out.bias" ) # 1.4 Encoder output converted["encoder.norm_out.gamma"] = original_state_dict.pop("encoder.norm_out.gamma") converted["encoder.conv_out.conv.weight"] = original_state_dict.pop("encoder.conv_out.conv.weight") converted["encoder.conv_out.conv.bias"] = original_state_dict.pop("encoder.conv_out.conv.bias") # 2. Decoder # 2.1 conv_in converted["decoder.conv_in.conv.weight"] = original_state_dict.pop("decoder.conv_in.conv.weight") converted["decoder.conv_in.conv.bias"] = original_state_dict.pop("decoder.conv_in.conv.bias") # 2.2 Mid block converted["decoder.mid_block.resnets.0.norm1.gamma"] = original_state_dict.pop("decoder.mid.block_1.norm1.gamma") converted["decoder.mid_block.resnets.0.conv1.conv.weight"] = original_state_dict.pop( "decoder.mid.block_1.conv1.conv.weight" ) converted["decoder.mid_block.resnets.0.conv1.conv.bias"] = original_state_dict.pop( "decoder.mid.block_1.conv1.conv.bias" ) converted["decoder.mid_block.resnets.0.norm2.gamma"] = original_state_dict.pop("decoder.mid.block_1.norm2.gamma") converted["decoder.mid_block.resnets.0.conv2.conv.weight"] = original_state_dict.pop( "decoder.mid.block_1.conv2.conv.weight" ) converted["decoder.mid_block.resnets.0.conv2.conv.bias"] = original_state_dict.pop( "decoder.mid.block_1.conv2.conv.bias" ) converted["decoder.mid_block.resnets.1.norm1.gamma"] = original_state_dict.pop("decoder.mid.block_2.norm1.gamma") converted["decoder.mid_block.resnets.1.conv1.conv.weight"] = original_state_dict.pop( "decoder.mid.block_2.conv1.conv.weight" ) converted["decoder.mid_block.resnets.1.conv1.conv.bias"] = original_state_dict.pop( "decoder.mid.block_2.conv1.conv.bias" ) converted["decoder.mid_block.resnets.1.norm2.gamma"] = original_state_dict.pop("decoder.mid.block_2.norm2.gamma") converted["decoder.mid_block.resnets.1.conv2.conv.weight"] = original_state_dict.pop( "decoder.mid.block_2.conv2.conv.weight" ) converted["decoder.mid_block.resnets.1.conv2.conv.bias"] = original_state_dict.pop( "decoder.mid.block_2.conv2.conv.bias" ) # Decoder attention block converted["decoder.mid_block.attentions.0.norm.gamma"] = original_state_dict.pop("decoder.mid.attn_1.norm.gamma") converted["decoder.mid_block.attentions.0.to_q.weight"] = original_state_dict.pop("decoder.mid.attn_1.q.weight") converted["decoder.mid_block.attentions.0.to_q.bias"] = original_state_dict.pop("decoder.mid.attn_1.q.bias") converted["decoder.mid_block.attentions.0.to_k.weight"] = original_state_dict.pop("decoder.mid.attn_1.k.weight") converted["decoder.mid_block.attentions.0.to_k.bias"] = original_state_dict.pop("decoder.mid.attn_1.k.bias") converted["decoder.mid_block.attentions.0.to_v.weight"] = original_state_dict.pop("decoder.mid.attn_1.v.weight") converted["decoder.mid_block.attentions.0.to_v.bias"] = original_state_dict.pop("decoder.mid.attn_1.v.bias") converted["decoder.mid_block.attentions.0.proj_out.weight"] = original_state_dict.pop( "decoder.mid.attn_1.proj_out.weight" ) converted["decoder.mid_block.attentions.0.proj_out.bias"] = original_state_dict.pop( "decoder.mid.attn_1.proj_out.bias" ) # 2.3 Up blocks for up_block_index in range(len(block_out_channels)): # 0 to 5 # ResNet blocks for resnet_block_index in range(layers_per_block + 1): # 0 to 2 (decoder has 3 resnets per level) converted[f"decoder.up_blocks.{up_block_index}.resnets.{resnet_block_index}.norm1.gamma"] = ( original_state_dict.pop(f"decoder.up.{up_block_index}.block.{resnet_block_index}.norm1.gamma") ) converted[f"decoder.up_blocks.{up_block_index}.resnets.{resnet_block_index}.conv1.conv.weight"] = ( original_state_dict.pop(f"decoder.up.{up_block_index}.block.{resnet_block_index}.conv1.conv.weight") ) converted[f"decoder.up_blocks.{up_block_index}.resnets.{resnet_block_index}.conv1.conv.bias"] = ( original_state_dict.pop(f"decoder.up.{up_block_index}.block.{resnet_block_index}.conv1.conv.bias") ) converted[f"decoder.up_blocks.{up_block_index}.resnets.{resnet_block_index}.norm2.gamma"] = ( original_state_dict.pop(f"decoder.up.{up_block_index}.block.{resnet_block_index}.norm2.gamma") ) converted[f"decoder.up_blocks.{up_block_index}.resnets.{resnet_block_index}.conv2.conv.weight"] = ( original_state_dict.pop(f"decoder.up.{up_block_index}.block.{resnet_block_index}.conv2.conv.weight") ) converted[f"decoder.up_blocks.{up_block_index}.resnets.{resnet_block_index}.conv2.conv.bias"] = ( original_state_dict.pop(f"decoder.up.{up_block_index}.block.{resnet_block_index}.conv2.conv.bias") ) # Upsample (if exists) if f"decoder.up.{up_block_index}.upsample.conv.conv.weight" in original_state_dict: converted[f"decoder.up_blocks.{up_block_index}.upsamplers.0.conv.conv.weight"] = original_state_dict.pop( f"decoder.up.{up_block_index}.upsample.conv.conv.weight" ) converted[f"decoder.up_blocks.{up_block_index}.upsamplers.0.conv.conv.bias"] = original_state_dict.pop( f"decoder.up.{up_block_index}.upsample.conv.conv.bias" ) # 2.4 Decoder output converted["decoder.norm_out.gamma"] = original_state_dict.pop("decoder.norm_out.gamma") converted["decoder.conv_out.conv.weight"] = original_state_dict.pop("decoder.conv_out.conv.weight") converted["decoder.conv_out.conv.bias"] = original_state_dict.pop("decoder.conv_out.conv.bias") return converted, original_state_dict def main(args): if args.model_type == "hunyuanimage2.1": original_transformer_state_dict = load_original_transformer_checkpoint(args) original_vae_state_dict = load_original_vae_checkpoint(args) transformer_config = { "in_channels": 64, "out_channels": 64, "num_attention_heads": 28, "attention_head_dim": 128, "num_layers": 20, "num_single_layers": 40, "num_refiner_layers": 2, "patch_size": (1, 1), "qk_norm": "rms_norm", "guidance_embeds": False, "text_embed_dim": 3584, "text_embed_2_dim": 1472, "rope_theta": 256.0, "rope_axes_dim": (64, 64), } converted_transformer_state_dict, original_transformer_state_dict = ( convert_hunyuan_image_transformer_checkpoint_to_diffusers( original_transformer_state_dict, use_byt5=True, guidance_distilled=False ) ) if original_transformer_state_dict: logger.warning( f"Unused {len(original_transformer_state_dict)} original keys for transformer: {list(original_transformer_state_dict.keys())}" ) transformer = HunyuanImageTransformer2DModel(**transformer_config) missing_keys, unexpected_key = transformer.load_state_dict(converted_transformer_state_dict, strict=True) if missing_keys: logger.warning(f"Missing keys for transformer: {missing_keys}") if unexpected_key: logger.warning(f"Unexpected keys for transformer: {unexpected_key}") transformer.to(dtype).save_pretrained(f"{args.output_path}/transformer") vae_config_diffusers = { "in_channels": 3, "out_channels": 3, "latent_channels": 64, "block_out_channels": [128, 256, 512, 512, 1024, 1024], "layers_per_block": 2, "spatial_compression_ratio": 32, "sample_size": 384, "scaling_factor": 0.75289, "downsample_match_channel": True, "upsample_match_channel": True, } converted_vae_state_dict, original_vae_state_dict = convert_hunyuan_image_vae_checkpoint_to_diffusers( original_vae_state_dict, block_out_channels=[128, 256, 512, 512, 1024, 1024], layers_per_block=2 ) if original_vae_state_dict: logger.warning( f"Unused {len(original_vae_state_dict)} original keys for vae: {list(original_vae_state_dict.keys())}" ) vae = AutoencoderKLHunyuanImage(**vae_config_diffusers) missing_keys, unexpected_key = vae.load_state_dict(converted_vae_state_dict, strict=True) if missing_keys: logger.warning(f"Missing keys for vae: {missing_keys}") if unexpected_key: logger.warning(f"Unexpected keys for vae: {unexpected_key}") vae.to(dtype).save_pretrained(f"{args.output_path}/vae") elif args.model_type == "hunyuanimage2.1-distilled": original_transformer_state_dict = load_original_transformer_checkpoint(args) original_vae_state_dict = load_original_vae_checkpoint(args) transformer_config = { "in_channels": 64, "out_channels": 64, "num_attention_heads": 28, "attention_head_dim": 128, "num_layers": 20, "num_single_layers": 40, "num_refiner_layers": 2, "patch_size": (1, 1), "qk_norm": "rms_norm", "guidance_embeds": True, "text_embed_dim": 3584, "text_embed_2_dim": 1472, "rope_theta": 256.0, "rope_axes_dim": (64, 64), "use_meanflow": True, } converted_transformer_state_dict, original_transformer_state_dict = ( convert_hunyuan_image_transformer_checkpoint_to_diffusers( original_transformer_state_dict, use_byt5=True, guidance_distilled=True, use_meanflow=True ) ) if original_transformer_state_dict: logger.warning( f"Unused {len(original_transformer_state_dict)} original keys for transformer: {list(original_transformer_state_dict.keys())}" ) transformer = HunyuanImageTransformer2DModel(**transformer_config) missing_keys, unexpected_key = transformer.load_state_dict(converted_transformer_state_dict, strict=True) if missing_keys: logger.warning(f"Missing keys for transformer: {missing_keys}") if unexpected_key: logger.warning(f"Unexpected keys for transformer: {unexpected_key}") transformer.to(dtype).save_pretrained(f"{args.output_path}/transformer") vae_config_diffusers = { "in_channels": 3, "out_channels": 3, "latent_channels": 64, "block_out_channels": [128, 256, 512, 512, 1024, 1024], "layers_per_block": 2, "spatial_compression_ratio": 32, "sample_size": 384, "scaling_factor": 0.75289, "downsample_match_channel": True, "upsample_match_channel": True, } converted_vae_state_dict, original_vae_state_dict = convert_hunyuan_image_vae_checkpoint_to_diffusers( original_vae_state_dict, block_out_channels=[128, 256, 512, 512, 1024, 1024], layers_per_block=2 ) if original_vae_state_dict: logger.warning( f"Unused {len(original_vae_state_dict)} original keys for vae: {list(original_vae_state_dict.keys())}" ) vae = AutoencoderKLHunyuanImage(**vae_config_diffusers) missing_keys, unexpected_key = vae.load_state_dict(converted_vae_state_dict, strict=True) if missing_keys: logger.warning(f"Missing keys for vae: {missing_keys}") if unexpected_key: logger.warning(f"Unexpected keys for vae: {unexpected_key}") vae.to(dtype).save_pretrained(f"{args.output_path}/vae") elif args.model_type == "hunyuanimage-refiner": original_transformer_state_dict = load_original_transformer_checkpoint(args) original_vae_state_dict = load_original_refiner_vae_checkpoint(args) transformer_config = { "in_channels": 128, "out_channels": 64, "num_layers": 20, "num_single_layers": 40, "rope_axes_dim": [16, 56, 56], "num_attention_heads": 26, "attention_head_dim": 128, "mlp_ratio": 4, "patch_size": (1, 1, 1), "text_embed_dim": 3584, "guidance_embeds": True, } converted_transformer_state_dict, original_transformer_state_dict = ( convert_hunyuan_image_transformer_checkpoint_to_diffusers( original_transformer_state_dict, use_byt5=False, guidance_distilled=True ) ) if original_transformer_state_dict: logger.warning( f"Unused {len(original_transformer_state_dict)} original keys for transformer: {list(original_transformer_state_dict.keys())}" ) transformer = HunyuanImageTransformer2DModel(**transformer_config) missing_keys, unexpected_key = transformer.load_state_dict(converted_transformer_state_dict, strict=True) if missing_keys: logger.warning(f"Missing keys for transformer: {missing_keys}") if unexpected_key: logger.warning(f"Unexpected keys for transformer: {unexpected_key}") transformer.to(dtype).save_pretrained(f"{args.output_path}/transformer") vae = AutoencoderKLHunyuanImageRefiner() converted_vae_state_dict, original_vae_state_dict = convert_hunyuan_image_refiner_vae_checkpoint_to_diffusers( original_vae_state_dict ) if original_vae_state_dict: logger.warning( f"Unused {len(original_vae_state_dict)} original keys for vae: {list(original_vae_state_dict.keys())}" ) missing_keys, unexpected_key = vae.load_state_dict(converted_vae_state_dict, strict=True) logger.warning(f"Missing keys for vae: {missing_keys}") logger.warning(f"Unexpected keys for vae: {unexpected_key}") vae.to(dtype).save_pretrained(f"{args.output_path}/vae") if __name__ == "__main__": main(args)
{ "repo_id": "huggingface/diffusers", "file_path": "scripts/convert_hunyuan_image_to_diffusers.py", "license": "Apache License 2.0", "lines": 912, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
function_complex
huggingface/diffusers:src/diffusers/guiders/adaptive_projected_guidance_mix.py
# Copyright 2025 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import math from typing import TYPE_CHECKING import torch from ..configuration_utils import register_to_config from .guider_utils import BaseGuidance, GuiderOutput, rescale_noise_cfg if TYPE_CHECKING: from ..modular_pipelines.modular_pipeline import BlockState class AdaptiveProjectedMixGuidance(BaseGuidance): """ Adaptive Projected Guidance (APG) https://huggingface.co/papers/2410.02416 combined with Classifier-Free Guidance (CFG). This guider is used in HunyuanImage2.1 https://github.com/Tencent-Hunyuan/HunyuanImage-2.1 Args: guidance_scale (`float`, defaults to `7.5`): The scale parameter for classifier-free guidance. Higher values result in stronger conditioning on the text prompt, while lower values allow for more freedom in generation. Higher values may lead to saturation and deterioration of image quality. adaptive_projected_guidance_momentum (`float`, defaults to `None`): The momentum parameter for the adaptive projected guidance. Disabled if set to `None`. adaptive_projected_guidance_rescale (`float`, defaults to `15.0`): The rescale factor applied to the noise predictions for adaptive projected guidance. This is used to improve image quality and fix guidance_rescale (`float`, defaults to `0.0`): The rescale factor applied to the noise predictions for classifier-free guidance. This is used to improve image quality and fix overexposure. Based on Section 3.4 from [Common Diffusion Noise Schedules and Sample Steps are Flawed](https://huggingface.co/papers/2305.08891). use_original_formulation (`bool`, defaults to `False`): Whether to use the original formulation of classifier-free guidance as proposed in the paper. By default, we use the diffusers-native implementation that has been in the codebase for a long time. See [~guiders.classifier_free_guidance.ClassifierFreeGuidance] for more details. start (`float`, defaults to `0.0`): The fraction of the total number of denoising steps after which the classifier-free guidance starts. stop (`float`, defaults to `1.0`): The fraction of the total number of denoising steps after which the classifier-free guidance stops. adaptive_projected_guidance_start_step (`int`, defaults to `5`): The step at which the adaptive projected guidance starts (before this step, classifier-free guidance is used, and momentum buffer is updated). enabled (`bool`, defaults to `True`): Whether this guidance is enabled. """ _input_predictions = ["pred_cond", "pred_uncond"] @register_to_config def __init__( self, guidance_scale: float = 3.5, guidance_rescale: float = 0.0, adaptive_projected_guidance_scale: float = 10.0, adaptive_projected_guidance_momentum: float = -0.5, adaptive_projected_guidance_rescale: float = 10.0, eta: float = 0.0, use_original_formulation: bool = False, start: float = 0.0, stop: float = 1.0, adaptive_projected_guidance_start_step: int = 5, enabled: bool = True, ): super().__init__(start, stop, enabled) self.guidance_scale = guidance_scale self.guidance_rescale = guidance_rescale self.adaptive_projected_guidance_scale = adaptive_projected_guidance_scale self.adaptive_projected_guidance_momentum = adaptive_projected_guidance_momentum self.adaptive_projected_guidance_rescale = adaptive_projected_guidance_rescale self.eta = eta self.adaptive_projected_guidance_start_step = adaptive_projected_guidance_start_step self.use_original_formulation = use_original_formulation self.momentum_buffer = None def prepare_inputs(self, data: dict[str, tuple[torch.Tensor, torch.Tensor]]) -> list["BlockState"]: if self._step == 0: if self.adaptive_projected_guidance_momentum is not None: self.momentum_buffer = MomentumBuffer(self.adaptive_projected_guidance_momentum) tuple_indices = [0] if self.num_conditions == 1 else [0, 1] data_batches = [] for tuple_idx, input_prediction in zip(tuple_indices, self._input_predictions): data_batch = self._prepare_batch(data, tuple_idx, input_prediction) data_batches.append(data_batch) return data_batches def prepare_inputs_from_block_state( self, data: "BlockState", input_fields: dict[str, str | tuple[str, str]] ) -> list["BlockState"]: if self._step == 0: if self.adaptive_projected_guidance_momentum is not None: self.momentum_buffer = MomentumBuffer(self.adaptive_projected_guidance_momentum) tuple_indices = [0] if self.num_conditions == 1 else [0, 1] data_batches = [] for tuple_idx, input_prediction in zip(tuple_indices, self._input_predictions): data_batch = self._prepare_batch_from_block_state(input_fields, data, tuple_idx, input_prediction) data_batches.append(data_batch) return data_batches def forward(self, pred_cond: torch.Tensor, pred_uncond: torch.Tensor | None = None) -> GuiderOutput: pred = None # no guidance if not self._is_cfg_enabled(): pred = pred_cond # CFG + update momentum buffer elif not self._is_apg_enabled(): if self.momentum_buffer is not None: update_momentum_buffer(pred_cond, pred_uncond, self.momentum_buffer) # CFG + update momentum buffer shift = pred_cond - pred_uncond pred = pred_cond if self.use_original_formulation else pred_uncond pred = pred + self.guidance_scale * shift # APG elif self._is_apg_enabled(): pred = normalized_guidance( pred_cond, pred_uncond, self.adaptive_projected_guidance_scale, self.momentum_buffer, self.eta, self.adaptive_projected_guidance_rescale, self.use_original_formulation, ) if self.guidance_rescale > 0.0: pred = rescale_noise_cfg(pred, pred_cond, self.guidance_rescale) return GuiderOutput(pred=pred, pred_cond=pred_cond, pred_uncond=pred_uncond) @property def is_conditional(self) -> bool: return self._count_prepared == 1 @property def num_conditions(self) -> int: num_conditions = 1 if self._is_apg_enabled() or self._is_cfg_enabled(): num_conditions += 1 return num_conditions # Copied from diffusers.guiders.classifier_free_guidance.ClassifierFreeGuidance._is_cfg_enabled def _is_cfg_enabled(self) -> bool: if not self._enabled: return False is_within_range = True if self._num_inference_steps is not None: skip_start_step = int(self._start * self._num_inference_steps) skip_stop_step = int(self._stop * self._num_inference_steps) is_within_range = skip_start_step <= self._step < skip_stop_step is_close = False if self.use_original_formulation: is_close = math.isclose(self.guidance_scale, 0.0) else: is_close = math.isclose(self.guidance_scale, 1.0) return is_within_range and not is_close def _is_apg_enabled(self) -> bool: if not self._enabled: return False if not self._is_cfg_enabled(): return False is_within_range = False if self._step is not None: is_within_range = self._step > self.adaptive_projected_guidance_start_step is_close = False if self.use_original_formulation: is_close = math.isclose(self.adaptive_projected_guidance_scale, 0.0) else: is_close = math.isclose(self.adaptive_projected_guidance_scale, 1.0) return is_within_range and not is_close def get_state(self): state = super().get_state() state["momentum_buffer"] = self.momentum_buffer state["is_apg_enabled"] = self._is_apg_enabled() state["is_cfg_enabled"] = self._is_cfg_enabled() return state # Copied from diffusers.guiders.adaptive_projected_guidance.MomentumBuffer class MomentumBuffer: def __init__(self, momentum: float): self.momentum = momentum self.running_average = 0 def update(self, update_value: torch.Tensor): new_average = self.momentum * self.running_average self.running_average = update_value + new_average def __repr__(self) -> str: """ Returns a string representation showing momentum, shape, statistics, and a slice of the running_average. """ if isinstance(self.running_average, torch.Tensor): shape = tuple(self.running_average.shape) # Calculate statistics with torch.no_grad(): stats = { "mean": self.running_average.mean().item(), "std": self.running_average.std().item(), "min": self.running_average.min().item(), "max": self.running_average.max().item(), } # Get a slice (max 3 elements per dimension) slice_indices = tuple(slice(None, min(3, dim)) for dim in shape) sliced_data = self.running_average[slice_indices] # Format the slice for display (convert to float32 for numpy compatibility with bfloat16) slice_str = str(sliced_data.detach().float().cpu().numpy()) if len(slice_str) > 200: # Truncate if too long slice_str = slice_str[:200] + "..." stats_str = ", ".join([f"{k}={v:.4f}" for k, v in stats.items()]) return ( f"MomentumBuffer(\n" f" momentum={self.momentum},\n" f" shape={shape},\n" f" stats=[{stats_str}],\n" f" slice={slice_str}\n" f")" ) else: return f"MomentumBuffer(momentum={self.momentum}, running_average={self.running_average})" def update_momentum_buffer( pred_cond: torch.Tensor, pred_uncond: torch.Tensor, momentum_buffer: MomentumBuffer | None = None, ): diff = pred_cond - pred_uncond if momentum_buffer is not None: momentum_buffer.update(diff) def normalized_guidance( pred_cond: torch.Tensor, pred_uncond: torch.Tensor, guidance_scale: float, momentum_buffer: MomentumBuffer | None = None, eta: float = 1.0, norm_threshold: float = 0.0, use_original_formulation: bool = False, ): if momentum_buffer is not None: update_momentum_buffer(pred_cond, pred_uncond, momentum_buffer) diff = momentum_buffer.running_average else: diff = pred_cond - pred_uncond dim = [-i for i in range(1, len(diff.shape))] if norm_threshold > 0: ones = torch.ones_like(diff) diff_norm = diff.norm(p=2, dim=dim, keepdim=True) scale_factor = torch.minimum(ones, norm_threshold / diff_norm) diff = diff * scale_factor v0, v1 = diff.double(), pred_cond.double() v1 = torch.nn.functional.normalize(v1, dim=dim) v0_parallel = (v0 * v1).sum(dim=dim, keepdim=True) * v1 v0_orthogonal = v0 - v0_parallel diff_parallel, diff_orthogonal = v0_parallel.type_as(diff), v0_orthogonal.type_as(diff) normalized_update = diff_orthogonal + eta * diff_parallel pred = pred_cond if use_original_formulation else pred_uncond pred = pred + guidance_scale * normalized_update return pred
{ "repo_id": "huggingface/diffusers", "file_path": "src/diffusers/guiders/adaptive_projected_guidance_mix.py", "license": "Apache License 2.0", "lines": 248, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
huggingface/diffusers:src/diffusers/models/autoencoders/autoencoder_kl_hunyuanimage.py
# Copyright 2025 The Hunyuan Team and The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import numpy as np import torch import torch.nn as nn import torch.nn.functional as F import torch.utils.checkpoint from ...configuration_utils import ConfigMixin, register_to_config from ...loaders import FromOriginalModelMixin from ...utils import logging from ...utils.accelerate_utils import apply_forward_hook from ..activations import get_activation from ..modeling_outputs import AutoencoderKLOutput from ..modeling_utils import ModelMixin from .vae import AutoencoderMixin, DecoderOutput, DiagonalGaussianDistribution logger = logging.get_logger(__name__) # pylint: disable=invalid-name class HunyuanImageResnetBlock(nn.Module): r""" Residual block with two convolutions and optional channel change. Args: in_channels (int): Number of input channels. out_channels (int): Number of output channels. non_linearity (str, optional): Type of non-linearity to use. Default is "silu". """ def __init__(self, in_channels: int, out_channels: int, non_linearity: str = "silu") -> None: super().__init__() self.in_channels = in_channels self.out_channels = out_channels self.nonlinearity = get_activation(non_linearity) # layers self.norm1 = nn.GroupNorm(num_groups=32, num_channels=in_channels, eps=1e-6, affine=True) self.conv1 = nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=1, padding=1) self.norm2 = nn.GroupNorm(num_groups=32, num_channels=out_channels, eps=1e-6, affine=True) self.conv2 = nn.Conv2d(out_channels, out_channels, kernel_size=3, stride=1, padding=1) if in_channels != out_channels: self.conv_shortcut = nn.Conv2d(in_channels, out_channels, kernel_size=1, stride=1, padding=0) else: self.conv_shortcut = None def forward(self, x): # Apply shortcut connection residual = x # First normalization and activation x = self.norm1(x) x = self.nonlinearity(x) x = self.conv1(x) x = self.norm2(x) x = self.nonlinearity(x) x = self.conv2(x) if self.conv_shortcut is not None: x = self.conv_shortcut(x) # Add residual connection return x + residual class HunyuanImageAttentionBlock(nn.Module): r""" Self-attention with a single head. Args: in_channels (int): The number of channels in the input tensor. """ def __init__(self, in_channels: int): super().__init__() # layers self.norm = nn.GroupNorm(num_groups=32, num_channels=in_channels, eps=1e-6, affine=True) self.to_q = nn.Conv2d(in_channels, in_channels, 1) self.to_k = nn.Conv2d(in_channels, in_channels, 1) self.to_v = nn.Conv2d(in_channels, in_channels, 1) self.proj = nn.Conv2d(in_channels, in_channels, 1) def forward(self, x): identity = x x = self.norm(x) # compute query, key, value query = self.to_q(x) key = self.to_k(x) value = self.to_v(x) batch_size, channels, height, width = query.shape query = query.permute(0, 2, 3, 1).reshape(batch_size, height * width, channels).contiguous() key = key.permute(0, 2, 3, 1).reshape(batch_size, height * width, channels).contiguous() value = value.permute(0, 2, 3, 1).reshape(batch_size, height * width, channels).contiguous() # apply attention x = F.scaled_dot_product_attention(query, key, value) x = x.reshape(batch_size, height, width, channels).permute(0, 3, 1, 2) # output projection x = self.proj(x) return x + identity class HunyuanImageDownsample(nn.Module): """ Downsampling block for spatial reduction. Args: in_channels (int): Number of input channels. out_channels (int): Number of output channels. """ def __init__(self, in_channels: int, out_channels: int): super().__init__() factor = 4 if out_channels % factor != 0: raise ValueError(f"out_channels % factor != 0: {out_channels % factor}") self.conv = nn.Conv2d(in_channels, out_channels // factor, kernel_size=3, stride=1, padding=1) self.group_size = factor * in_channels // out_channels def forward(self, x: torch.Tensor) -> torch.Tensor: h = self.conv(x) B, C, H, W = h.shape h = h.reshape(B, C, H // 2, 2, W // 2, 2) h = h.permute(0, 3, 5, 1, 2, 4) # b, r1, r2, c, h, w h = h.reshape(B, 4 * C, H // 2, W // 2) B, C, H, W = x.shape shortcut = x.reshape(B, C, H // 2, 2, W // 2, 2) shortcut = shortcut.permute(0, 3, 5, 1, 2, 4) # b, r1, r2, c, h, w shortcut = shortcut.reshape(B, 4 * C, H // 2, W // 2) B, C, H, W = shortcut.shape shortcut = shortcut.view(B, h.shape[1], self.group_size, H, W).mean(dim=2) return h + shortcut class HunyuanImageUpsample(nn.Module): """ Upsampling block for spatial expansion. Args: in_channels (int): Number of input channels. out_channels (int): Number of output channels. """ def __init__(self, in_channels: int, out_channels: int): super().__init__() factor = 4 self.conv = nn.Conv2d(in_channels, out_channels * factor, kernel_size=3, stride=1, padding=1) self.repeats = factor * out_channels // in_channels def forward(self, x: torch.Tensor) -> torch.Tensor: h = self.conv(x) B, C, H, W = h.shape h = h.reshape(B, 2, 2, C // 4, H, W) # b, r1, r2, c, h, w h = h.permute(0, 3, 4, 1, 5, 2) # b, c, h, r1, w, r2 h = h.reshape(B, C // 4, H * 2, W * 2) shortcut = x.repeat_interleave(repeats=self.repeats, dim=1) B, C, H, W = shortcut.shape shortcut = shortcut.reshape(B, 2, 2, C // 4, H, W) # b, r1, r2, c, h, w shortcut = shortcut.permute(0, 3, 4, 1, 5, 2) # b, c, h, r1, w, r2 shortcut = shortcut.reshape(B, C // 4, H * 2, W * 2) return h + shortcut class HunyuanImageMidBlock(nn.Module): """ Middle block for HunyuanImageVAE encoder and decoder. Args: in_channels (int): Number of input channels. num_layers (int): Number of layers. """ def __init__(self, in_channels: int, num_layers: int = 1): super().__init__() resnets = [HunyuanImageResnetBlock(in_channels=in_channels, out_channels=in_channels)] attentions = [] for _ in range(num_layers): attentions.append(HunyuanImageAttentionBlock(in_channels)) resnets.append(HunyuanImageResnetBlock(in_channels=in_channels, out_channels=in_channels)) self.resnets = nn.ModuleList(resnets) self.attentions = nn.ModuleList(attentions) def forward(self, x: torch.Tensor) -> torch.Tensor: x = self.resnets[0](x) for attn, resnet in zip(self.attentions, self.resnets[1:]): x = attn(x) x = resnet(x) return x class HunyuanImageEncoder2D(nn.Module): r""" Encoder network that compresses input to latent representation. Args: in_channels (int): Number of input channels. z_channels (int): Number of latent channels. block_out_channels (list of int): Output channels for each block. num_res_blocks (int): Number of residual blocks per block. spatial_compression_ratio (int): Spatial downsampling factor. non_linearity (str): Type of non-linearity to use. Default is "silu". downsample_match_channel (bool): Whether to match channels during downsampling. """ def __init__( self, in_channels: int, z_channels: int, block_out_channels: tuple[int, ...], num_res_blocks: int, spatial_compression_ratio: int, non_linearity: str = "silu", downsample_match_channel: bool = True, ): super().__init__() if block_out_channels[-1] % (2 * z_channels) != 0: raise ValueError( f"block_out_channels[-1 has to be divisible by 2 * out_channels, you have block_out_channels = {block_out_channels[-1]} and out_channels = {z_channels}" ) self.in_channels = in_channels self.z_channels = z_channels self.block_out_channels = block_out_channels self.num_res_blocks = num_res_blocks self.spatial_compression_ratio = spatial_compression_ratio self.group_size = block_out_channels[-1] // (2 * z_channels) self.nonlinearity = get_activation(non_linearity) # init block self.conv_in = nn.Conv2d(in_channels, block_out_channels[0], kernel_size=3, stride=1, padding=1) # downsample blocks self.down_blocks = nn.ModuleList([]) block_in_channel = block_out_channels[0] for i in range(len(block_out_channels)): block_out_channel = block_out_channels[i] # residual blocks for _ in range(num_res_blocks): self.down_blocks.append( HunyuanImageResnetBlock(in_channels=block_in_channel, out_channels=block_out_channel) ) block_in_channel = block_out_channel # downsample block if i < np.log2(spatial_compression_ratio) and i != len(block_out_channels) - 1: if downsample_match_channel: block_out_channel = block_out_channels[i + 1] self.down_blocks.append( HunyuanImageDownsample(in_channels=block_in_channel, out_channels=block_out_channel) ) block_in_channel = block_out_channel # middle blocks self.mid_block = HunyuanImageMidBlock(in_channels=block_out_channels[-1], num_layers=1) # output blocks # Output layers self.norm_out = nn.GroupNorm(num_groups=32, num_channels=block_out_channels[-1], eps=1e-6, affine=True) self.conv_out = nn.Conv2d(block_out_channels[-1], 2 * z_channels, kernel_size=3, stride=1, padding=1) self.gradient_checkpointing = False def forward(self, x: torch.Tensor) -> torch.Tensor: x = self.conv_in(x) ## downsamples for down_block in self.down_blocks: if torch.is_grad_enabled() and self.gradient_checkpointing: x = self._gradient_checkpointing_func(down_block, x) else: x = down_block(x) ## middle if torch.is_grad_enabled() and self.gradient_checkpointing: x = self._gradient_checkpointing_func(self.mid_block, x) else: x = self.mid_block(x) ## head B, C, H, W = x.shape residual = x.view(B, C // self.group_size, self.group_size, H, W).mean(dim=2) x = self.norm_out(x) x = self.nonlinearity(x) x = self.conv_out(x) return x + residual class HunyuanImageDecoder2D(nn.Module): r""" Decoder network that reconstructs output from latent representation. Args: z_channels : int Number of latent channels. out_channels : int Number of output channels. block_out_channels : tuple[int, ...] Output channels for each block. num_res_blocks : int Number of residual blocks per block. spatial_compression_ratio : int Spatial upsampling factor. upsample_match_channel : bool Whether to match channels during upsampling. non_linearity (str): Type of non-linearity to use. Default is "silu". """ def __init__( self, z_channels: int, out_channels: int, block_out_channels: tuple[int, ...], num_res_blocks: int, spatial_compression_ratio: int, upsample_match_channel: bool = True, non_linearity: str = "silu", ): super().__init__() if block_out_channels[0] % z_channels != 0: raise ValueError( f"block_out_channels[0] should be divisible by z_channels but has block_out_channels[0] = {block_out_channels[0]} and z_channels = {z_channels}" ) self.z_channels = z_channels self.block_out_channels = block_out_channels self.num_res_blocks = num_res_blocks self.repeat = block_out_channels[0] // z_channels self.spatial_compression_ratio = spatial_compression_ratio self.nonlinearity = get_activation(non_linearity) self.conv_in = nn.Conv2d(z_channels, block_out_channels[0], kernel_size=3, stride=1, padding=1) # Middle blocks with attention self.mid_block = HunyuanImageMidBlock(in_channels=block_out_channels[0], num_layers=1) # Upsampling blocks block_in_channel = block_out_channels[0] self.up_blocks = nn.ModuleList() for i in range(len(block_out_channels)): block_out_channel = block_out_channels[i] for _ in range(self.num_res_blocks + 1): self.up_blocks.append( HunyuanImageResnetBlock(in_channels=block_in_channel, out_channels=block_out_channel) ) block_in_channel = block_out_channel if i < np.log2(spatial_compression_ratio) and i != len(block_out_channels) - 1: if upsample_match_channel: block_out_channel = block_out_channels[i + 1] self.up_blocks.append(HunyuanImageUpsample(block_in_channel, block_out_channel)) block_in_channel = block_out_channel # Output layers self.norm_out = nn.GroupNorm(num_groups=32, num_channels=block_out_channels[-1], eps=1e-6, affine=True) self.conv_out = nn.Conv2d(block_out_channels[-1], out_channels, kernel_size=3, stride=1, padding=1) self.gradient_checkpointing = False def forward(self, x: torch.Tensor) -> torch.Tensor: h = self.conv_in(x) + x.repeat_interleave(repeats=self.repeat, dim=1) if torch.is_grad_enabled() and self.gradient_checkpointing: h = self._gradient_checkpointing_func(self.mid_block, h) else: h = self.mid_block(h) for up_block in self.up_blocks: if torch.is_grad_enabled() and self.gradient_checkpointing: h = self._gradient_checkpointing_func(up_block, h) else: h = up_block(h) h = self.norm_out(h) h = self.nonlinearity(h) h = self.conv_out(h) return h class AutoencoderKLHunyuanImage(ModelMixin, AutoencoderMixin, ConfigMixin, FromOriginalModelMixin): r""" A VAE model for 2D images with spatial tiling support. This model inherits from [`ModelMixin`]. Check the superclass documentation for it's generic methods implemented for all models (such as downloading or saving). """ _supports_gradient_checkpointing = False # fmt: off @register_to_config def __init__( self, in_channels: int, out_channels: int, latent_channels: int, block_out_channels: tuple[int, ...], layers_per_block: int, spatial_compression_ratio: int, sample_size: int, scaling_factor: float = None, downsample_match_channel: bool = True, upsample_match_channel: bool = True, ) -> None: # fmt: on super().__init__() self.encoder = HunyuanImageEncoder2D( in_channels=in_channels, z_channels=latent_channels, block_out_channels=block_out_channels, num_res_blocks=layers_per_block, spatial_compression_ratio=spatial_compression_ratio, downsample_match_channel=downsample_match_channel, ) self.decoder = HunyuanImageDecoder2D( z_channels=latent_channels, out_channels=out_channels, block_out_channels=list(reversed(block_out_channels)), num_res_blocks=layers_per_block, spatial_compression_ratio=spatial_compression_ratio, upsample_match_channel=upsample_match_channel, ) # Tiling and slicing configuration self.use_slicing = False self.use_tiling = False # Tiling parameters self.tile_sample_min_size = sample_size self.tile_latent_min_size = sample_size // spatial_compression_ratio self.tile_overlap_factor = 0.25 def enable_tiling( self, tile_sample_min_size: int | None = None, tile_overlap_factor: float | None = None, ) -> None: r""" Enable spatial tiled VAE decoding. When this option is enabled, the VAE will split the input tensor into tiles to compute decoding and encoding in several steps. This is useful for saving a large amount of memory and to allow processing larger images. Args: tile_sample_min_size (`int`, *optional*): The minimum size required for a sample to be separated into tiles across the spatial dimension. tile_overlap_factor (`float`, *optional*): The overlap factor required for a latent to be separated into tiles across the spatial dimension. """ self.use_tiling = True self.tile_sample_min_size = tile_sample_min_size or self.tile_sample_min_size self.tile_overlap_factor = tile_overlap_factor or self.tile_overlap_factor self.tile_latent_min_size = self.tile_sample_min_size // self.config.spatial_compression_ratio def _encode(self, x: torch.Tensor): batch_size, num_channels, height, width = x.shape if self.use_tiling and (width > self.tile_sample_min_size or height > self.tile_sample_min_size): return self.tiled_encode(x) enc = self.encoder(x) return enc @apply_forward_hook def encode( self, x: torch.Tensor, return_dict: bool = True ) -> AutoencoderKLOutput | tuple[DiagonalGaussianDistribution]: r""" Encode a batch of images into latents. Args: x (`torch.Tensor`): Input batch of images. return_dict (`bool`, *optional*, defaults to `True`): Whether to return a [`~models.autoencoder_kl.AutoencoderKLOutput`] instead of a plain tuple. Returns: The latent representations of the encoded videos. If `return_dict` is True, a [`~models.autoencoder_kl.AutoencoderKLOutput`] is returned, otherwise a plain `tuple` is returned. """ if self.use_slicing and x.shape[0] > 1: encoded_slices = [self._encode(x_slice) for x_slice in x.split(1)] h = torch.cat(encoded_slices) else: h = self._encode(x) posterior = DiagonalGaussianDistribution(h) if not return_dict: return (posterior,) return AutoencoderKLOutput(latent_dist=posterior) def _decode(self, z: torch.Tensor, return_dict: bool = True): batch_size, num_channels, height, width = z.shape if self.use_tiling and (width > self.tile_latent_min_size or height > self.tile_latent_min_size): return self.tiled_decode(z, return_dict=return_dict) dec = self.decoder(z) if not return_dict: return (dec,) return DecoderOutput(sample=dec) @apply_forward_hook def decode(self, z: torch.Tensor, return_dict: bool = True) -> DecoderOutput | torch.Tensor: r""" Decode a batch of images. Args: z (`torch.Tensor`): Input batch of latent vectors. return_dict (`bool`, *optional*, defaults to `True`): Whether to return a [`~models.vae.DecoderOutput`] instead of a plain tuple. Returns: [`~models.vae.DecoderOutput`] or `tuple`: If return_dict is True, a [`~models.vae.DecoderOutput`] is returned, otherwise a plain `tuple` is returned. """ if self.use_slicing and z.shape[0] > 1: decoded_slices = [self._decode(z_slice).sample for z_slice in z.split(1)] decoded = torch.cat(decoded_slices) else: decoded = self._decode(z).sample if not return_dict: return (decoded,) return DecoderOutput(sample=decoded) def blend_v(self, a: torch.Tensor, b: torch.Tensor, blend_extent: int) -> torch.Tensor: blend_extent = min(a.shape[-2], b.shape[-2], blend_extent) for y in range(blend_extent): b[:, :, y, :] = a[:, :, -blend_extent + y, :] * (1 - y / blend_extent) + b[:, :, y, :] * ( y / blend_extent ) return b def blend_h(self, a: torch.Tensor, b: torch.Tensor, blend_extent: int) -> torch.Tensor: blend_extent = min(a.shape[-1], b.shape[-1], blend_extent) for x in range(blend_extent): b[:, :, :, x] = a[:, :, :, -blend_extent + x] * (1 - x / blend_extent) + b[:, :, :, x] * ( x / blend_extent ) return b def tiled_encode(self, x: torch.Tensor) -> torch.Tensor: """ Encode input using spatial tiling strategy. Args: x (`torch.Tensor`): Input tensor of shape (B, C, T, H, W). Returns: `torch.Tensor`: The latent representation of the encoded images. """ _, _, _, height, width = x.shape overlap_size = int(self.tile_sample_min_size * (1 - self.tile_overlap_factor)) blend_extent = int(self.tile_latent_min_size * self.tile_overlap_factor) row_limit = self.tile_latent_min_size - blend_extent rows = [] for i in range(0, height, overlap_size): row = [] for j in range(0, width, overlap_size): tile = x[:, :, :, i : i + self.tile_sample_min_size, j : j + self.tile_sample_min_size] tile = self.encoder(tile) row.append(tile) rows.append(row) result_rows = [] for i, row in enumerate(rows): result_row = [] for j, tile in enumerate(row): if i > 0: tile = self.blend_v(rows[i - 1][j], tile, blend_extent) if j > 0: tile = self.blend_h(row[j - 1], tile, blend_extent) result_row.append(tile[:, :, :, :row_limit, :row_limit]) result_rows.append(torch.cat(result_row, dim=-1)) moments = torch.cat(result_rows, dim=-2) return moments def tiled_decode(self, z: torch.Tensor, return_dict: bool = True) -> DecoderOutput | torch.Tensor: """ Decode latent using spatial tiling strategy. Args: z (`torch.Tensor`): Latent tensor of shape (B, C, H, W). return_dict (`bool`, *optional*, defaults to `True`): Whether or not to return a [`~models.vae.DecoderOutput`] instead of a plain tuple. Returns: [`~models.vae.DecoderOutput`] or `tuple`: If return_dict is True, a [`~models.vae.DecoderOutput`] is returned, otherwise a plain `tuple` is returned. """ _, _, height, width = z.shape overlap_size = int(self.tile_latent_min_size * (1 - self.tile_overlap_factor)) blend_extent = int(self.tile_sample_min_size * self.tile_overlap_factor) row_limit = self.tile_sample_min_size - blend_extent rows = [] for i in range(0, height, overlap_size): row = [] for j in range(0, width, overlap_size): tile = z[:, :, i : i + self.tile_latent_min_size, j : j + self.tile_latent_min_size] decoded = self.decoder(tile) row.append(decoded) rows.append(row) result_rows = [] for i, row in enumerate(rows): result_row = [] for j, tile in enumerate(row): if i > 0: tile = self.blend_v(rows[i - 1][j], tile, blend_extent) if j > 0: tile = self.blend_h(row[j - 1], tile, blend_extent) result_row.append(tile[:, :, :row_limit, :row_limit]) result_rows.append(torch.cat(result_row, dim=-1)) dec = torch.cat(result_rows, dim=-2) if not return_dict: return (dec,) return DecoderOutput(sample=dec) def forward( self, sample: torch.Tensor, sample_posterior: bool = False, return_dict: bool = True, generator: torch.Generator | None = None, ) -> DecoderOutput | torch.Tensor: """ Args: sample (`torch.Tensor`): Input sample. return_dict (`bool`, *optional*, defaults to `True`): Whether or not to return a [`DecoderOutput`] instead of a plain tuple. """ posterior = self.encode(sample).latent_dist if sample_posterior: z = posterior.sample(generator=generator) else: z = posterior.mode() dec = self.decode(z, return_dict=return_dict) return dec
{ "repo_id": "huggingface/diffusers", "file_path": "src/diffusers/models/autoencoders/autoencoder_kl_hunyuanimage.py", "license": "Apache License 2.0", "lines": 554, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
huggingface/diffusers:src/diffusers/models/autoencoders/autoencoder_kl_hunyuanimage_refiner.py
# Copyright 2025 The Hunyuan Team and The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import numpy as np import torch import torch.nn as nn import torch.nn.functional as F import torch.utils.checkpoint from ...configuration_utils import ConfigMixin, register_to_config from ...utils import logging from ...utils.accelerate_utils import apply_forward_hook from ..activations import get_activation from ..modeling_outputs import AutoencoderKLOutput from ..modeling_utils import ModelMixin from .vae import AutoencoderMixin, DecoderOutput, DiagonalGaussianDistribution logger = logging.get_logger(__name__) # pylint: disable=invalid-name class HunyuanImageRefinerCausalConv3d(nn.Module): def __init__( self, in_channels: int, out_channels: int, kernel_size: int | tuple[int, int, int] = 3, stride: int | tuple[int, int, int] = 1, padding: int | tuple[int, int, int] = 0, dilation: int | tuple[int, int, int] = 1, bias: bool = True, pad_mode: str = "replicate", ) -> None: super().__init__() kernel_size = (kernel_size, kernel_size, kernel_size) if isinstance(kernel_size, int) else kernel_size self.pad_mode = pad_mode self.time_causal_padding = ( kernel_size[0] // 2, kernel_size[0] // 2, kernel_size[1] // 2, kernel_size[1] // 2, kernel_size[2] - 1, 0, ) self.conv = nn.Conv3d(in_channels, out_channels, kernel_size, stride, padding, dilation, bias=bias) def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: hidden_states = F.pad(hidden_states, self.time_causal_padding, mode=self.pad_mode) return self.conv(hidden_states) class HunyuanImageRefinerRMS_norm(nn.Module): r""" A custom RMS normalization layer. Args: dim (int): The number of dimensions to normalize over. channel_first (bool, optional): Whether the input tensor has channels as the first dimension. Default is True. images (bool, optional): Whether the input represents image data. Default is True. bias (bool, optional): Whether to include a learnable bias term. Default is False. """ def __init__(self, dim: int, channel_first: bool = True, images: bool = True, bias: bool = False) -> None: super().__init__() broadcastable_dims = (1, 1, 1) if not images else (1, 1) shape = (dim, *broadcastable_dims) if channel_first else (dim,) self.channel_first = channel_first self.scale = dim**0.5 self.gamma = nn.Parameter(torch.ones(shape)) self.bias = nn.Parameter(torch.zeros(shape)) if bias else 0.0 def forward(self, x): return F.normalize(x, dim=(1 if self.channel_first else -1)) * self.scale * self.gamma + self.bias class HunyuanImageRefinerAttnBlock(nn.Module): def __init__(self, in_channels: int): super().__init__() self.in_channels = in_channels self.norm = HunyuanImageRefinerRMS_norm(in_channels, images=False) self.to_q = nn.Conv3d(in_channels, in_channels, kernel_size=1) self.to_k = nn.Conv3d(in_channels, in_channels, kernel_size=1) self.to_v = nn.Conv3d(in_channels, in_channels, kernel_size=1) self.proj_out = nn.Conv3d(in_channels, in_channels, kernel_size=1) def forward(self, x: torch.Tensor) -> torch.Tensor: identity = x x = self.norm(x) query = self.to_q(x) key = self.to_k(x) value = self.to_v(x) batch_size, channels, frames, height, width = query.shape query = query.reshape(batch_size, channels, frames * height * width).permute(0, 2, 1).unsqueeze(1).contiguous() key = key.reshape(batch_size, channels, frames * height * width).permute(0, 2, 1).unsqueeze(1).contiguous() value = value.reshape(batch_size, channels, frames * height * width).permute(0, 2, 1).unsqueeze(1).contiguous() x = nn.functional.scaled_dot_product_attention(query, key, value, attn_mask=None) # batch_size, 1, frames * height * width, channels x = x.squeeze(1).reshape(batch_size, frames, height, width, channels).permute(0, 4, 1, 2, 3) x = self.proj_out(x) return x + identity class HunyuanImageRefinerUpsampleDCAE(nn.Module): def __init__(self, in_channels: int, out_channels: int, add_temporal_upsample: bool = True): super().__init__() factor = 2 * 2 * 2 if add_temporal_upsample else 1 * 2 * 2 self.conv = HunyuanImageRefinerCausalConv3d(in_channels, out_channels * factor, kernel_size=3) self.add_temporal_upsample = add_temporal_upsample self.repeats = factor * out_channels // in_channels @staticmethod def _dcae_upsample_rearrange(tensor, r1=1, r2=2, r3=2): """ Convert (b, r1*r2*r3*c, f, h, w) -> (b, c, r1*f, r2*h, r3*w) Args: tensor: Input tensor of shape (b, r1*r2*r3*c, f, h, w) r1: temporal upsampling factor r2: height upsampling factor r3: width upsampling factor """ b, packed_c, f, h, w = tensor.shape factor = r1 * r2 * r3 c = packed_c // factor tensor = tensor.view(b, r1, r2, r3, c, f, h, w) tensor = tensor.permute(0, 4, 5, 1, 6, 2, 7, 3) return tensor.reshape(b, c, f * r1, h * r2, w * r3) def forward(self, x: torch.Tensor): r1 = 2 if self.add_temporal_upsample else 1 h = self.conv(x) if self.add_temporal_upsample: h = self._dcae_upsample_rearrange(h, r1=1, r2=2, r3=2) h = h[:, : h.shape[1] // 2] # shortcut computation shortcut = self._dcae_upsample_rearrange(x, r1=1, r2=2, r3=2) shortcut = shortcut.repeat_interleave(repeats=self.repeats // 2, dim=1) else: h = self._dcae_upsample_rearrange(h, r1=r1, r2=2, r3=2) shortcut = x.repeat_interleave(repeats=self.repeats, dim=1) shortcut = self._dcae_upsample_rearrange(shortcut, r1=r1, r2=2, r3=2) return h + shortcut class HunyuanImageRefinerDownsampleDCAE(nn.Module): def __init__(self, in_channels: int, out_channels: int, add_temporal_downsample: bool = True): super().__init__() factor = 2 * 2 * 2 if add_temporal_downsample else 1 * 2 * 2 assert out_channels % factor == 0 # self.conv = Conv3d(in_channels, out_channels // factor, kernel_size=3, stride=1, padding=1) self.conv = HunyuanImageRefinerCausalConv3d(in_channels, out_channels // factor, kernel_size=3) self.add_temporal_downsample = add_temporal_downsample self.group_size = factor * in_channels // out_channels @staticmethod def _dcae_downsample_rearrange(tensor, r1=1, r2=2, r3=2): """ Convert (b, c, r1*f, r2*h, r3*w) -> (b, r1*r2*r3*c, f, h, w) This packs spatial/temporal dimensions into channels (opposite of upsample) """ b, c, packed_f, packed_h, packed_w = tensor.shape f, h, w = packed_f // r1, packed_h // r2, packed_w // r3 tensor = tensor.view(b, c, f, r1, h, r2, w, r3) tensor = tensor.permute(0, 3, 5, 7, 1, 2, 4, 6) return tensor.reshape(b, r1 * r2 * r3 * c, f, h, w) def forward(self, x: torch.Tensor): r1 = 2 if self.add_temporal_downsample else 1 h = self.conv(x) if self.add_temporal_downsample: # h = rearrange(h, "b c f (h r2) (w r3) -> b (r2 r3 c) f h w", r2=2, r3=2) h = self._dcae_downsample_rearrange(h, r1=1, r2=2, r3=2) h = torch.cat([h, h], dim=1) # shortcut computation # shortcut = rearrange(x, "b c f (h r2) (w r3) -> b (r2 r3 c) f h w", r2=2, r3=2) shortcut = self._dcae_downsample_rearrange(x, r1=1, r2=2, r3=2) B, C, T, H, W = shortcut.shape shortcut = shortcut.view(B, h.shape[1], self.group_size // 2, T, H, W).mean(dim=2) else: # h = rearrange(h, "b c (f r1) (h r2) (w r3) -> b (r1 r2 r3 c) f h w", r1=r1, r2=2, r3=2) h = self._dcae_downsample_rearrange(h, r1=r1, r2=2, r3=2) # shortcut = rearrange(x, "b c (f r1) (h r2) (w r3) -> b (r1 r2 r3 c) f h w", r1=r1, r2=2, r3=2) shortcut = self._dcae_downsample_rearrange(x, r1=r1, r2=2, r3=2) B, C, T, H, W = shortcut.shape shortcut = shortcut.view(B, h.shape[1], self.group_size, T, H, W).mean(dim=2) return h + shortcut class HunyuanImageRefinerResnetBlock(nn.Module): def __init__( self, in_channels: int, out_channels: int | None = None, non_linearity: str = "swish", ) -> None: super().__init__() out_channels = out_channels or in_channels self.nonlinearity = get_activation(non_linearity) self.norm1 = HunyuanImageRefinerRMS_norm(in_channels, images=False) self.conv1 = HunyuanImageRefinerCausalConv3d(in_channels, out_channels, kernel_size=3) self.norm2 = HunyuanImageRefinerRMS_norm(out_channels, images=False) self.conv2 = HunyuanImageRefinerCausalConv3d(out_channels, out_channels, kernel_size=3) self.conv_shortcut = None if in_channels != out_channels: self.conv_shortcut = nn.Conv3d(in_channels, out_channels, kernel_size=1, stride=1, padding=0) def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: residual = hidden_states hidden_states = self.norm1(hidden_states) hidden_states = self.nonlinearity(hidden_states) hidden_states = self.conv1(hidden_states) hidden_states = self.norm2(hidden_states) hidden_states = self.nonlinearity(hidden_states) hidden_states = self.conv2(hidden_states) if self.conv_shortcut is not None: residual = self.conv_shortcut(residual) return hidden_states + residual class HunyuanImageRefinerMidBlock(nn.Module): def __init__( self, in_channels: int, num_layers: int = 1, add_attention: bool = True, ) -> None: super().__init__() self.add_attention = add_attention # There is always at least one resnet resnets = [ HunyuanImageRefinerResnetBlock( in_channels=in_channels, out_channels=in_channels, ) ] attentions = [] for _ in range(num_layers): if self.add_attention: attentions.append(HunyuanImageRefinerAttnBlock(in_channels)) else: attentions.append(None) resnets.append( HunyuanImageRefinerResnetBlock( in_channels=in_channels, out_channels=in_channels, ) ) self.attentions = nn.ModuleList(attentions) self.resnets = nn.ModuleList(resnets) self.gradient_checkpointing = False def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: hidden_states = self.resnets[0](hidden_states) for attn, resnet in zip(self.attentions, self.resnets[1:]): if attn is not None: hidden_states = attn(hidden_states) hidden_states = resnet(hidden_states) return hidden_states class HunyuanImageRefinerDownBlock3D(nn.Module): def __init__( self, in_channels: int, out_channels: int, num_layers: int = 1, downsample_out_channels: int | None = None, add_temporal_downsample: int = True, ) -> None: super().__init__() resnets = [] for i in range(num_layers): in_channels = in_channels if i == 0 else out_channels resnets.append( HunyuanImageRefinerResnetBlock( in_channels=in_channels, out_channels=out_channels, ) ) self.resnets = nn.ModuleList(resnets) if downsample_out_channels is not None: self.downsamplers = nn.ModuleList( [ HunyuanImageRefinerDownsampleDCAE( out_channels, out_channels=downsample_out_channels, add_temporal_downsample=add_temporal_downsample, ) ] ) else: self.downsamplers = None self.gradient_checkpointing = False def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: for resnet in self.resnets: hidden_states = resnet(hidden_states) if self.downsamplers is not None: for downsampler in self.downsamplers: hidden_states = downsampler(hidden_states) return hidden_states class HunyuanImageRefinerUpBlock3D(nn.Module): def __init__( self, in_channels: int, out_channels: int, num_layers: int = 1, upsample_out_channels: int | None = None, add_temporal_upsample: bool = True, ) -> None: super().__init__() resnets = [] for i in range(num_layers): input_channels = in_channels if i == 0 else out_channels resnets.append( HunyuanImageRefinerResnetBlock( in_channels=input_channels, out_channels=out_channels, ) ) self.resnets = nn.ModuleList(resnets) if upsample_out_channels is not None: self.upsamplers = nn.ModuleList( [ HunyuanImageRefinerUpsampleDCAE( out_channels, out_channels=upsample_out_channels, add_temporal_upsample=add_temporal_upsample, ) ] ) else: self.upsamplers = None self.gradient_checkpointing = False def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: if torch.is_grad_enabled() and self.gradient_checkpointing: for resnet in self.resnets: hidden_states = self._gradient_checkpointing_func(resnet, hidden_states) else: for resnet in self.resnets: hidden_states = resnet(hidden_states) if self.upsamplers is not None: for upsampler in self.upsamplers: hidden_states = upsampler(hidden_states) return hidden_states class HunyuanImageRefinerEncoder3D(nn.Module): r""" 3D vae encoder for HunyuanImageRefiner. """ def __init__( self, in_channels: int = 3, out_channels: int = 64, block_out_channels: tuple[int, ...] = (128, 256, 512, 1024, 1024), layers_per_block: int = 2, temporal_compression_ratio: int = 4, spatial_compression_ratio: int = 16, downsample_match_channel: bool = True, ) -> None: super().__init__() self.in_channels = in_channels self.out_channels = out_channels self.group_size = block_out_channels[-1] // self.out_channels self.conv_in = HunyuanImageRefinerCausalConv3d(in_channels, block_out_channels[0], kernel_size=3) self.mid_block = None self.down_blocks = nn.ModuleList([]) input_channel = block_out_channels[0] for i in range(len(block_out_channels)): add_spatial_downsample = i < np.log2(spatial_compression_ratio) output_channel = block_out_channels[i] if not add_spatial_downsample: down_block = HunyuanImageRefinerDownBlock3D( num_layers=layers_per_block, in_channels=input_channel, out_channels=output_channel, downsample_out_channels=None, add_temporal_downsample=False, ) input_channel = output_channel else: add_temporal_downsample = i >= np.log2(spatial_compression_ratio // temporal_compression_ratio) downsample_out_channels = block_out_channels[i + 1] if downsample_match_channel else output_channel down_block = HunyuanImageRefinerDownBlock3D( num_layers=layers_per_block, in_channels=input_channel, out_channels=output_channel, downsample_out_channels=downsample_out_channels, add_temporal_downsample=add_temporal_downsample, ) input_channel = downsample_out_channels self.down_blocks.append(down_block) self.mid_block = HunyuanImageRefinerMidBlock(in_channels=block_out_channels[-1]) self.norm_out = HunyuanImageRefinerRMS_norm(block_out_channels[-1], images=False) self.conv_act = nn.SiLU() self.conv_out = HunyuanImageRefinerCausalConv3d(block_out_channels[-1], out_channels, kernel_size=3) self.gradient_checkpointing = False def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: hidden_states = self.conv_in(hidden_states) if torch.is_grad_enabled() and self.gradient_checkpointing: for down_block in self.down_blocks: hidden_states = self._gradient_checkpointing_func(down_block, hidden_states) hidden_states = self._gradient_checkpointing_func(self.mid_block, hidden_states) else: for down_block in self.down_blocks: hidden_states = down_block(hidden_states) hidden_states = self.mid_block(hidden_states) # short_cut = rearrange(hidden_states, "b (c r) f h w -> b c r f h w", r=self.group_size).mean(dim=2) batch_size, _, frame, height, width = hidden_states.shape short_cut = hidden_states.view(batch_size, -1, self.group_size, frame, height, width).mean(dim=2) hidden_states = self.norm_out(hidden_states) hidden_states = self.conv_act(hidden_states) hidden_states = self.conv_out(hidden_states) hidden_states += short_cut return hidden_states class HunyuanImageRefinerDecoder3D(nn.Module): r""" Causal decoder for 3D video-like data used for HunyuanImage-2.1 Refiner. """ def __init__( self, in_channels: int = 32, out_channels: int = 3, block_out_channels: tuple[int, ...] = (1024, 1024, 512, 256, 128), layers_per_block: int = 2, spatial_compression_ratio: int = 16, temporal_compression_ratio: int = 4, upsample_match_channel: bool = True, ): super().__init__() self.layers_per_block = layers_per_block self.in_channels = in_channels self.out_channels = out_channels self.repeat = block_out_channels[0] // self.in_channels self.conv_in = HunyuanImageRefinerCausalConv3d(self.in_channels, block_out_channels[0], kernel_size=3) self.up_blocks = nn.ModuleList([]) # mid self.mid_block = HunyuanImageRefinerMidBlock(in_channels=block_out_channels[0]) # up input_channel = block_out_channels[0] for i in range(len(block_out_channels)): output_channel = block_out_channels[i] add_spatial_upsample = i < np.log2(spatial_compression_ratio) add_temporal_upsample = i < np.log2(temporal_compression_ratio) if add_spatial_upsample or add_temporal_upsample: upsample_out_channels = block_out_channels[i + 1] if upsample_match_channel else output_channel up_block = HunyuanImageRefinerUpBlock3D( num_layers=self.layers_per_block + 1, in_channels=input_channel, out_channels=output_channel, upsample_out_channels=upsample_out_channels, add_temporal_upsample=add_temporal_upsample, ) input_channel = upsample_out_channels else: up_block = HunyuanImageRefinerUpBlock3D( num_layers=self.layers_per_block + 1, in_channels=input_channel, out_channels=output_channel, upsample_out_channels=None, add_temporal_upsample=False, ) input_channel = output_channel self.up_blocks.append(up_block) # out self.norm_out = HunyuanImageRefinerRMS_norm(block_out_channels[-1], images=False) self.conv_act = nn.SiLU() self.conv_out = HunyuanImageRefinerCausalConv3d(block_out_channels[-1], out_channels, kernel_size=3) self.gradient_checkpointing = False def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: hidden_states = self.conv_in(hidden_states) + hidden_states.repeat_interleave(repeats=self.repeat, dim=1) if torch.is_grad_enabled() and self.gradient_checkpointing: hidden_states = self._gradient_checkpointing_func(self.mid_block, hidden_states) for up_block in self.up_blocks: hidden_states = self._gradient_checkpointing_func(up_block, hidden_states) else: hidden_states = self.mid_block(hidden_states) for up_block in self.up_blocks: hidden_states = up_block(hidden_states) # post-process hidden_states = self.norm_out(hidden_states) hidden_states = self.conv_act(hidden_states) hidden_states = self.conv_out(hidden_states) return hidden_states class AutoencoderKLHunyuanImageRefiner(ModelMixin, AutoencoderMixin, ConfigMixin): r""" A VAE model with KL loss for encoding videos into latents and decoding latent representations into videos. Used for HunyuanImage-2.1 Refiner. This model inherits from [`ModelMixin`]. Check the superclass documentation for it's generic methods implemented for all models (such as downloading or saving). """ _supports_gradient_checkpointing = True @register_to_config def __init__( self, in_channels: int = 3, out_channels: int = 3, latent_channels: int = 32, block_out_channels: tuple[int, ...] = (128, 256, 512, 1024, 1024), layers_per_block: int = 2, spatial_compression_ratio: int = 16, temporal_compression_ratio: int = 4, downsample_match_channel: bool = True, upsample_match_channel: bool = True, scaling_factor: float = 1.03682, ) -> None: super().__init__() self.encoder = HunyuanImageRefinerEncoder3D( in_channels=in_channels, out_channels=latent_channels * 2, block_out_channels=block_out_channels, layers_per_block=layers_per_block, temporal_compression_ratio=temporal_compression_ratio, spatial_compression_ratio=spatial_compression_ratio, downsample_match_channel=downsample_match_channel, ) self.decoder = HunyuanImageRefinerDecoder3D( in_channels=latent_channels, out_channels=out_channels, block_out_channels=list(reversed(block_out_channels)), layers_per_block=layers_per_block, temporal_compression_ratio=temporal_compression_ratio, spatial_compression_ratio=spatial_compression_ratio, upsample_match_channel=upsample_match_channel, ) self.spatial_compression_ratio = spatial_compression_ratio self.temporal_compression_ratio = temporal_compression_ratio # When decoding a batch of video latents at a time, one can save memory by slicing across the batch dimension # to perform decoding of a single video latent at a time. self.use_slicing = False # When decoding spatially large video latents, the memory requirement is very high. By breaking the video latent # frames spatially into smaller tiles and performing multiple forward passes for decoding, and then blending the # intermediate tiles together, the memory requirement can be lowered. self.use_tiling = False # The minimal tile height and width for spatial tiling to be used self.tile_sample_min_height = 256 self.tile_sample_min_width = 256 # The minimal distance between two spatial tiles self.tile_sample_stride_height = 192 self.tile_sample_stride_width = 192 self.tile_overlap_factor = 0.25 def enable_tiling( self, tile_sample_min_height: int | None = None, tile_sample_min_width: int | None = None, tile_sample_stride_height: float | None = None, tile_sample_stride_width: float | None = None, tile_overlap_factor: float | None = None, ) -> None: r""" Enable tiled VAE decoding. When this option is enabled, the VAE will split the input tensor into tiles to compute decoding and encoding in several steps. This is useful for saving a large amount of memory and to allow processing larger images. Args: tile_sample_min_height (`int`, *optional*): The minimum height required for a sample to be separated into tiles across the height dimension. tile_sample_min_width (`int`, *optional*): The minimum width required for a sample to be separated into tiles across the width dimension. tile_sample_stride_height (`int`, *optional*): The minimum amount of overlap between two consecutive vertical tiles. This is to ensure that there are no tiling artifacts produced across the height dimension. tile_sample_stride_width (`int`, *optional*): The stride between two consecutive horizontal tiles. This is to ensure that there are no tiling artifacts produced across the width dimension. """ self.use_tiling = True self.tile_sample_min_height = tile_sample_min_height or self.tile_sample_min_height self.tile_sample_min_width = tile_sample_min_width or self.tile_sample_min_width self.tile_sample_stride_height = tile_sample_stride_height or self.tile_sample_stride_height self.tile_sample_stride_width = tile_sample_stride_width or self.tile_sample_stride_width self.tile_overlap_factor = tile_overlap_factor or self.tile_overlap_factor def _encode(self, x: torch.Tensor) -> torch.Tensor: _, _, _, height, width = x.shape if self.use_tiling and (width > self.tile_sample_min_width or height > self.tile_sample_min_height): return self.tiled_encode(x) x = self.encoder(x) return x @apply_forward_hook def encode( self, x: torch.Tensor, return_dict: bool = True ) -> AutoencoderKLOutput | tuple[DiagonalGaussianDistribution]: r""" Encode a batch of images into latents. Args: x (`torch.Tensor`): Input batch of images. return_dict (`bool`, *optional*, defaults to `True`): Whether to return a [`~models.autoencoder_kl.AutoencoderKLOutput`] instead of a plain tuple. Returns: The latent representations of the encoded videos. If `return_dict` is True, a [`~models.autoencoder_kl.AutoencoderKLOutput`] is returned, otherwise a plain `tuple` is returned. """ if self.use_slicing and x.shape[0] > 1: encoded_slices = [self._encode(x_slice) for x_slice in x.split(1)] h = torch.cat(encoded_slices) else: h = self._encode(x) posterior = DiagonalGaussianDistribution(h) if not return_dict: return (posterior,) return AutoencoderKLOutput(latent_dist=posterior) def _decode(self, z: torch.Tensor) -> torch.Tensor: _, _, _, height, width = z.shape tile_latent_min_height = self.tile_sample_min_height // self.spatial_compression_ratio tile_latent_min_width = self.tile_sample_min_width // self.spatial_compression_ratio if self.use_tiling and (width > tile_latent_min_width or height > tile_latent_min_height): return self.tiled_decode(z) dec = self.decoder(z) return dec @apply_forward_hook def decode(self, z: torch.Tensor, return_dict: bool = True) -> DecoderOutput | torch.Tensor: r""" Decode a batch of images. Args: z (`torch.Tensor`): Input batch of latent vectors. return_dict (`bool`, *optional*, defaults to `True`): Whether to return a [`~models.vae.DecoderOutput`] instead of a plain tuple. Returns: [`~models.vae.DecoderOutput`] or `tuple`: If return_dict is True, a [`~models.vae.DecoderOutput`] is returned, otherwise a plain `tuple` is returned. """ if self.use_slicing and z.shape[0] > 1: decoded_slices = [self._decode(z_slice) for z_slice in z.split(1)] decoded = torch.cat(decoded_slices) else: decoded = self._decode(z) if not return_dict: return (decoded,) return DecoderOutput(sample=decoded) def blend_v(self, a: torch.Tensor, b: torch.Tensor, blend_extent: int) -> torch.Tensor: blend_extent = min(a.shape[-2], b.shape[-2], blend_extent) for y in range(blend_extent): b[:, :, :, y, :] = a[:, :, :, -blend_extent + y, :] * (1 - y / blend_extent) + b[:, :, :, y, :] * ( y / blend_extent ) return b def blend_h(self, a: torch.Tensor, b: torch.Tensor, blend_extent: int) -> torch.Tensor: blend_extent = min(a.shape[-1], b.shape[-1], blend_extent) for x in range(blend_extent): b[:, :, :, :, x] = a[:, :, :, :, -blend_extent + x] * (1 - x / blend_extent) + b[:, :, :, :, x] * ( x / blend_extent ) return b def blend_t(self, a: torch.Tensor, b: torch.Tensor, blend_extent: int) -> torch.Tensor: blend_extent = min(a.shape[-3], b.shape[-3], blend_extent) for x in range(blend_extent): b[:, :, x, :, :] = a[:, :, -blend_extent + x, :, :] * (1 - x / blend_extent) + b[:, :, x, :, :] * ( x / blend_extent ) return b def tiled_encode(self, x: torch.Tensor) -> torch.Tensor: r"""Encode a batch of images using a tiled encoder. Args: x (`torch.Tensor`): Input batch of videos. Returns: `torch.Tensor`: The latent representation of the encoded videos. """ _, _, _, height, width = x.shape tile_latent_min_height = self.tile_sample_min_height // self.spatial_compression_ratio tile_latent_min_width = self.tile_sample_min_width // self.spatial_compression_ratio overlap_height = int(tile_latent_min_height * (1 - self.tile_overlap_factor)) # 256 * (1 - 0.25) = 192 overlap_width = int(tile_latent_min_width * (1 - self.tile_overlap_factor)) # 256 * (1 - 0.25) = 192 blend_height = int(tile_latent_min_height * self.tile_overlap_factor) # 8 * 0.25 = 2 blend_width = int(tile_latent_min_width * self.tile_overlap_factor) # 8 * 0.25 = 2 row_limit_height = tile_latent_min_height - blend_height # 8 - 2 = 6 row_limit_width = tile_latent_min_width - blend_width # 8 - 2 = 6 rows = [] for i in range(0, height, overlap_height): row = [] for j in range(0, width, overlap_width): tile = x[ :, :, :, i : i + self.tile_sample_min_height, j : j + self.tile_sample_min_width, ] tile = self.encoder(tile) row.append(tile) rows.append(row) result_rows = [] for i, row in enumerate(rows): result_row = [] for j, tile in enumerate(row): if i > 0: tile = self.blend_v(rows[i - 1][j], tile, blend_height) if j > 0: tile = self.blend_h(row[j - 1], tile, blend_width) result_row.append(tile[:, :, :, :row_limit_height, :row_limit_width]) result_rows.append(torch.cat(result_row, dim=-1)) moments = torch.cat(result_rows, dim=-2) return moments def tiled_decode(self, z: torch.Tensor) -> torch.Tensor: r""" Decode a batch of images using a tiled decoder. Args: z (`torch.Tensor`): Input batch of latent vectors. return_dict (`bool`, *optional*, defaults to `True`): Whether or not to return a [`~models.vae.DecoderOutput`] instead of a plain tuple. Returns: [`~models.vae.DecoderOutput`] or `tuple`: If return_dict is True, a [`~models.vae.DecoderOutput`] is returned, otherwise a plain `tuple` is returned. """ _, _, _, height, width = z.shape tile_latent_min_height = self.tile_sample_min_height // self.spatial_compression_ratio tile_latent_min_width = self.tile_sample_min_width // self.spatial_compression_ratio overlap_height = int(tile_latent_min_height * (1 - self.tile_overlap_factor)) # 8 * (1 - 0.25) = 6 overlap_width = int(tile_latent_min_width * (1 - self.tile_overlap_factor)) # 8 * (1 - 0.25) = 6 blend_height = int(tile_latent_min_height * self.tile_overlap_factor) # 256 * 0.25 = 64 blend_width = int(tile_latent_min_width * self.tile_overlap_factor) # 256 * 0.25 = 64 row_limit_height = tile_latent_min_height - blend_height # 256 - 64 = 192 row_limit_width = tile_latent_min_width - blend_width # 256 - 64 = 192 rows = [] for i in range(0, height, overlap_height): row = [] for j in range(0, width, overlap_width): tile = z[ :, :, :, i : i + tile_latent_min_height, j : j + tile_latent_min_width, ] decoded = self.decoder(tile) row.append(decoded) rows.append(row) result_rows = [] for i, row in enumerate(rows): result_row = [] for j, tile in enumerate(row): if i > 0: tile = self.blend_v(rows[i - 1][j], tile, blend_height) if j > 0: tile = self.blend_h(row[j - 1], tile, blend_width) result_row.append(tile[:, :, :, :row_limit_height, :row_limit_width]) result_rows.append(torch.cat(result_row, dim=-1)) dec = torch.cat(result_rows, dim=-2) return dec def forward( self, sample: torch.Tensor, sample_posterior: bool = False, return_dict: bool = True, generator: torch.Generator | None = None, ) -> DecoderOutput | torch.Tensor: r""" Args: sample (`torch.Tensor`): Input sample. sample_posterior (`bool`, *optional*, defaults to `False`): Whether to sample from the posterior. return_dict (`bool`, *optional*, defaults to `True`): Whether or not to return a [`DecoderOutput`] instead of a plain tuple. """ x = sample posterior = self.encode(x).latent_dist if sample_posterior: z = posterior.sample(generator=generator) else: z = posterior.mode() dec = self.decode(z, return_dict=return_dict) return dec
{ "repo_id": "huggingface/diffusers", "file_path": "src/diffusers/models/autoencoders/autoencoder_kl_hunyuanimage_refiner.py", "license": "Apache License 2.0", "lines": 741, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
huggingface/diffusers:src/diffusers/models/transformers/transformer_hunyuanimage.py
# Copyright 2025 The Hunyuan Team and The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import math from typing import Any import torch import torch.nn as nn import torch.nn.functional as F from diffusers.loaders import FromOriginalModelMixin from ...configuration_utils import ConfigMixin, register_to_config from ...loaders import PeftAdapterMixin from ...utils import apply_lora_scale, logging from ...utils.torch_utils import maybe_allow_in_graph from ..attention import AttentionMixin, FeedForward from ..attention_dispatch import dispatch_attention_fn from ..attention_processor import Attention from ..cache_utils import CacheMixin from ..embeddings import ( CombinedTimestepTextProjEmbeddings, TimestepEmbedding, Timesteps, get_1d_rotary_pos_embed, ) from ..modeling_outputs import Transformer2DModelOutput from ..modeling_utils import ModelMixin from ..normalization import AdaLayerNormContinuous, AdaLayerNormZero, AdaLayerNormZeroSingle logger = logging.get_logger(__name__) # pylint: disable=invalid-name class HunyuanImageAttnProcessor: _attention_backend = None _parallel_config = None def __init__(self): if not hasattr(F, "scaled_dot_product_attention"): raise ImportError( "HunyuanImageAttnProcessor requires PyTorch 2.0. To use it, please upgrade PyTorch to 2.0." ) def __call__( self, attn: Attention, hidden_states: torch.Tensor, encoder_hidden_states: torch.Tensor | None = None, attention_mask: torch.Tensor | None = None, image_rotary_emb: torch.Tensor | None = None, ) -> torch.Tensor: if attn.add_q_proj is None and encoder_hidden_states is not None: hidden_states = torch.cat([hidden_states, encoder_hidden_states], dim=1) # 1. QKV projections query = attn.to_q(hidden_states) key = attn.to_k(hidden_states) value = attn.to_v(hidden_states) query = query.unflatten(2, (attn.heads, -1)) # batch_size, seq_len, heads, head_dim key = key.unflatten(2, (attn.heads, -1)) value = value.unflatten(2, (attn.heads, -1)) # 2. QK normalization if attn.norm_q is not None: query = attn.norm_q(query) if attn.norm_k is not None: key = attn.norm_k(key) # 3. Rotational positional embeddings applied to latent stream if image_rotary_emb is not None: from ..embeddings import apply_rotary_emb if attn.add_q_proj is None and encoder_hidden_states is not None: query = torch.cat( [ apply_rotary_emb( query[:, : -encoder_hidden_states.shape[1]], image_rotary_emb, sequence_dim=1 ), query[:, -encoder_hidden_states.shape[1] :], ], dim=1, ) key = torch.cat( [ apply_rotary_emb(key[:, : -encoder_hidden_states.shape[1]], image_rotary_emb, sequence_dim=1), key[:, -encoder_hidden_states.shape[1] :], ], dim=1, ) else: query = apply_rotary_emb(query, image_rotary_emb, sequence_dim=1) key = apply_rotary_emb(key, image_rotary_emb, sequence_dim=1) # 4. Encoder condition QKV projection and normalization if attn.add_q_proj is not None and encoder_hidden_states is not None: encoder_query = attn.add_q_proj(encoder_hidden_states) encoder_key = attn.add_k_proj(encoder_hidden_states) encoder_value = attn.add_v_proj(encoder_hidden_states) encoder_query = encoder_query.unflatten(2, (attn.heads, -1)) encoder_key = encoder_key.unflatten(2, (attn.heads, -1)) encoder_value = encoder_value.unflatten(2, (attn.heads, -1)) if attn.norm_added_q is not None: encoder_query = attn.norm_added_q(encoder_query) if attn.norm_added_k is not None: encoder_key = attn.norm_added_k(encoder_key) query = torch.cat([query, encoder_query], dim=1) key = torch.cat([key, encoder_key], dim=1) value = torch.cat([value, encoder_value], dim=1) # 5. Attention hidden_states = dispatch_attention_fn( query, key, value, attn_mask=attention_mask, dropout_p=0.0, is_causal=False, backend=self._attention_backend, parallel_config=self._parallel_config, ) hidden_states = hidden_states.flatten(2, 3) hidden_states = hidden_states.to(query.dtype) # 6. Output projection if encoder_hidden_states is not None: hidden_states, encoder_hidden_states = ( hidden_states[:, : -encoder_hidden_states.shape[1]], hidden_states[:, -encoder_hidden_states.shape[1] :], ) if getattr(attn, "to_out", None) is not None: hidden_states = attn.to_out[0](hidden_states) hidden_states = attn.to_out[1](hidden_states) if getattr(attn, "to_add_out", None) is not None: encoder_hidden_states = attn.to_add_out(encoder_hidden_states) return hidden_states, encoder_hidden_states class HunyuanImagePatchEmbed(nn.Module): def __init__( self, patch_size: tuple[int, int, tuple[int, int, int]] = (16, 16), in_chans: int = 3, embed_dim: int = 768, ) -> None: super().__init__() self.patch_size = patch_size if len(patch_size) == 2: self.proj = nn.Conv2d(in_chans, embed_dim, kernel_size=patch_size, stride=patch_size) elif len(patch_size) == 3: self.proj = nn.Conv3d(in_chans, embed_dim, kernel_size=patch_size, stride=patch_size) else: raise ValueError(f"patch_size must be a tuple of length 2 or 3, got {len(patch_size)}") def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: hidden_states = self.proj(hidden_states) hidden_states = hidden_states.flatten(2).transpose(1, 2) return hidden_states class HunyuanImageByT5TextProjection(nn.Module): def __init__(self, in_features: int, hidden_size: int, out_features: int): super().__init__() self.norm = nn.LayerNorm(in_features) self.linear_1 = nn.Linear(in_features, hidden_size) self.linear_2 = nn.Linear(hidden_size, hidden_size) self.linear_3 = nn.Linear(hidden_size, out_features) self.act_fn = nn.GELU() def forward(self, encoder_hidden_states: torch.Tensor) -> torch.Tensor: hidden_states = self.norm(encoder_hidden_states) hidden_states = self.linear_1(hidden_states) hidden_states = self.act_fn(hidden_states) hidden_states = self.linear_2(hidden_states) hidden_states = self.act_fn(hidden_states) hidden_states = self.linear_3(hidden_states) return hidden_states class HunyuanImageAdaNorm(nn.Module): def __init__(self, in_features: int, out_features: int | None = None) -> None: super().__init__() out_features = out_features or 2 * in_features self.linear = nn.Linear(in_features, out_features) self.nonlinearity = nn.SiLU() def forward( self, temb: torch.Tensor ) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]: temb = self.linear(self.nonlinearity(temb)) gate_msa, gate_mlp = temb.chunk(2, dim=1) gate_msa, gate_mlp = gate_msa.unsqueeze(1), gate_mlp.unsqueeze(1) return gate_msa, gate_mlp class HunyuanImageCombinedTimeGuidanceEmbedding(nn.Module): def __init__( self, embedding_dim: int, guidance_embeds: bool = False, use_meanflow: bool = False, ): super().__init__() self.time_proj = Timesteps(num_channels=256, flip_sin_to_cos=True, downscale_freq_shift=0) self.timestep_embedder = TimestepEmbedding(in_channels=256, time_embed_dim=embedding_dim) self.use_meanflow = use_meanflow self.time_proj_r = None self.timestep_embedder_r = None if use_meanflow: self.time_proj_r = Timesteps(num_channels=256, flip_sin_to_cos=True, downscale_freq_shift=0) self.timestep_embedder_r = TimestepEmbedding(in_channels=256, time_embed_dim=embedding_dim) self.guidance_embedder = None if guidance_embeds: self.guidance_embedder = TimestepEmbedding(in_channels=256, time_embed_dim=embedding_dim) def forward( self, timestep: torch.Tensor, timestep_r: torch.Tensor | None = None, guidance: torch.Tensor | None = None, ) -> tuple[torch.Tensor, torch.Tensor]: timesteps_proj = self.time_proj(timestep) timesteps_emb = self.timestep_embedder(timesteps_proj.to(dtype=timestep.dtype)) if timestep_r is not None: timesteps_proj_r = self.time_proj_r(timestep_r) timesteps_emb_r = self.timestep_embedder_r(timesteps_proj_r.to(dtype=timestep.dtype)) timesteps_emb = (timesteps_emb + timesteps_emb_r) / 2 if self.guidance_embedder is not None: guidance_proj = self.time_proj(guidance) guidance_emb = self.guidance_embedder(guidance_proj.to(dtype=timestep.dtype)) conditioning = timesteps_emb + guidance_emb else: conditioning = timesteps_emb return conditioning # IndividualTokenRefinerBlock @maybe_allow_in_graph class HunyuanImageIndividualTokenRefinerBlock(nn.Module): def __init__( self, num_attention_heads: int, # 28 attention_head_dim: int, # 128 mlp_width_ratio: str = 4.0, mlp_drop_rate: float = 0.0, attention_bias: bool = True, ) -> None: super().__init__() hidden_size = num_attention_heads * attention_head_dim self.norm1 = nn.LayerNorm(hidden_size, elementwise_affine=True, eps=1e-6) self.attn = Attention( query_dim=hidden_size, cross_attention_dim=None, heads=num_attention_heads, dim_head=attention_head_dim, bias=attention_bias, ) self.norm2 = nn.LayerNorm(hidden_size, elementwise_affine=True, eps=1e-6) self.ff = FeedForward(hidden_size, mult=mlp_width_ratio, activation_fn="linear-silu", dropout=mlp_drop_rate) self.norm_out = HunyuanImageAdaNorm(hidden_size, 2 * hidden_size) def forward( self, hidden_states: torch.Tensor, temb: torch.Tensor, attention_mask: torch.Tensor | None = None, ) -> torch.Tensor: norm_hidden_states = self.norm1(hidden_states) attn_output = self.attn( hidden_states=norm_hidden_states, encoder_hidden_states=None, attention_mask=attention_mask, ) gate_msa, gate_mlp = self.norm_out(temb) hidden_states = hidden_states + attn_output * gate_msa ff_output = self.ff(self.norm2(hidden_states)) hidden_states = hidden_states + ff_output * gate_mlp return hidden_states class HunyuanImageIndividualTokenRefiner(nn.Module): def __init__( self, num_attention_heads: int, attention_head_dim: int, num_layers: int, mlp_width_ratio: float = 4.0, mlp_drop_rate: float = 0.0, attention_bias: bool = True, ) -> None: super().__init__() self.refiner_blocks = nn.ModuleList( [ HunyuanImageIndividualTokenRefinerBlock( num_attention_heads=num_attention_heads, attention_head_dim=attention_head_dim, mlp_width_ratio=mlp_width_ratio, mlp_drop_rate=mlp_drop_rate, attention_bias=attention_bias, ) for _ in range(num_layers) ] ) def forward( self, hidden_states: torch.Tensor, temb: torch.Tensor, attention_mask: torch.Tensor | None = None, ) -> None: self_attn_mask = None if attention_mask is not None: batch_size = attention_mask.shape[0] seq_len = attention_mask.shape[1] attention_mask = attention_mask.to(hidden_states.device) self_attn_mask_1 = attention_mask.view(batch_size, 1, 1, seq_len).repeat(1, 1, seq_len, 1) self_attn_mask_2 = self_attn_mask_1.transpose(2, 3) self_attn_mask = (self_attn_mask_1 & self_attn_mask_2).bool() self_attn_mask[:, :, :, 0] = True for block in self.refiner_blocks: hidden_states = block(hidden_states, temb, self_attn_mask) return hidden_states # txt_in class HunyuanImageTokenRefiner(nn.Module): def __init__( self, in_channels: int, num_attention_heads: int, attention_head_dim: int, num_layers: int, mlp_ratio: float = 4.0, mlp_drop_rate: float = 0.0, attention_bias: bool = True, ) -> None: super().__init__() hidden_size = num_attention_heads * attention_head_dim self.time_text_embed = CombinedTimestepTextProjEmbeddings( embedding_dim=hidden_size, pooled_projection_dim=in_channels ) self.proj_in = nn.Linear(in_channels, hidden_size, bias=True) self.token_refiner = HunyuanImageIndividualTokenRefiner( num_attention_heads=num_attention_heads, attention_head_dim=attention_head_dim, num_layers=num_layers, mlp_width_ratio=mlp_ratio, mlp_drop_rate=mlp_drop_rate, attention_bias=attention_bias, ) def forward( self, hidden_states: torch.Tensor, timestep: torch.LongTensor, attention_mask: torch.LongTensor | None = None, ) -> torch.Tensor: if attention_mask is None: pooled_hidden_states = hidden_states.mean(dim=1) else: original_dtype = hidden_states.dtype mask_float = attention_mask.float().unsqueeze(-1) pooled_hidden_states = (hidden_states * mask_float).sum(dim=1) / mask_float.sum(dim=1) pooled_hidden_states = pooled_hidden_states.to(original_dtype) temb = self.time_text_embed(timestep, pooled_hidden_states) hidden_states = self.proj_in(hidden_states) hidden_states = self.token_refiner(hidden_states, temb, attention_mask) return hidden_states class HunyuanImageRotaryPosEmbed(nn.Module): def __init__(self, patch_size: tuple | list[int], rope_dim: tuple | list[int], theta: float = 256.0) -> None: super().__init__() if not isinstance(patch_size, (tuple, list)) or len(patch_size) not in [2, 3]: raise ValueError(f"patch_size must be a tuple or list of length 2 or 3, got {patch_size}") if not isinstance(rope_dim, (tuple, list)) or len(rope_dim) not in [2, 3]: raise ValueError(f"rope_dim must be a tuple or list of length 2 or 3, got {rope_dim}") if not len(patch_size) == len(rope_dim): raise ValueError(f"patch_size and rope_dim must have the same length, got {patch_size} and {rope_dim}") self.patch_size = patch_size self.rope_dim = rope_dim self.theta = theta def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: if hidden_states.ndim == 5: _, _, frame, height, width = hidden_states.shape patch_size_frame, patch_size_height, patch_size_width = self.patch_size rope_sizes = [frame // patch_size_frame, height // patch_size_height, width // patch_size_width] elif hidden_states.ndim == 4: _, _, height, width = hidden_states.shape patch_size_height, patch_size_width = self.patch_size rope_sizes = [height // patch_size_height, width // patch_size_width] else: raise ValueError(f"hidden_states must be a 4D or 5D tensor, got {hidden_states.shape}") axes_grids = [] for i in range(len(rope_sizes)): grid = torch.arange(0, rope_sizes[i], device=hidden_states.device, dtype=torch.float32) axes_grids.append(grid) grid = torch.meshgrid(*axes_grids, indexing="ij") # dim x [H, W] grid = torch.stack(grid, dim=0) # [2, H, W] freqs = [] for i in range(len(rope_sizes)): freq = get_1d_rotary_pos_embed(self.rope_dim[i], grid[i].reshape(-1), self.theta, use_real=True) freqs.append(freq) freqs_cos = torch.cat([f[0] for f in freqs], dim=1) # (W * H * T, D / 2) freqs_sin = torch.cat([f[1] for f in freqs], dim=1) # (W * H * T, D / 2) return freqs_cos, freqs_sin @maybe_allow_in_graph class HunyuanImageSingleTransformerBlock(nn.Module): def __init__( self, num_attention_heads: int, attention_head_dim: int, mlp_ratio: float = 4.0, qk_norm: str = "rms_norm", ) -> None: super().__init__() hidden_size = num_attention_heads * attention_head_dim mlp_dim = int(hidden_size * mlp_ratio) self.attn = Attention( query_dim=hidden_size, cross_attention_dim=None, dim_head=attention_head_dim, heads=num_attention_heads, out_dim=hidden_size, bias=True, processor=HunyuanImageAttnProcessor(), qk_norm=qk_norm, eps=1e-6, pre_only=True, ) self.norm = AdaLayerNormZeroSingle(hidden_size, norm_type="layer_norm") self.proj_mlp = nn.Linear(hidden_size, mlp_dim) self.act_mlp = nn.GELU(approximate="tanh") self.proj_out = nn.Linear(hidden_size + mlp_dim, hidden_size) def forward( self, hidden_states: torch.Tensor, encoder_hidden_states: torch.Tensor, temb: torch.Tensor, attention_mask: torch.Tensor | None = None, image_rotary_emb: tuple[torch.Tensor, torch.Tensor] | None = None, *args, **kwargs, ) -> torch.Tensor: text_seq_length = encoder_hidden_states.shape[1] hidden_states = torch.cat([hidden_states, encoder_hidden_states], dim=1) residual = hidden_states # 1. Input normalization norm_hidden_states, gate = self.norm(hidden_states, emb=temb) mlp_hidden_states = self.act_mlp(self.proj_mlp(norm_hidden_states)) norm_hidden_states, norm_encoder_hidden_states = ( norm_hidden_states[:, :-text_seq_length, :], norm_hidden_states[:, -text_seq_length:, :], ) # 2. Attention attn_output, context_attn_output = self.attn( hidden_states=norm_hidden_states, encoder_hidden_states=norm_encoder_hidden_states, attention_mask=attention_mask, image_rotary_emb=image_rotary_emb, ) attn_output = torch.cat([attn_output, context_attn_output], dim=1) # 3. Modulation and residual connection hidden_states = torch.cat([attn_output, mlp_hidden_states], dim=2) hidden_states = gate.unsqueeze(1) * self.proj_out(hidden_states) hidden_states = hidden_states + residual hidden_states, encoder_hidden_states = ( hidden_states[:, :-text_seq_length, :], hidden_states[:, -text_seq_length:, :], ) return hidden_states, encoder_hidden_states @maybe_allow_in_graph class HunyuanImageTransformerBlock(nn.Module): def __init__( self, num_attention_heads: int, attention_head_dim: int, mlp_ratio: float, qk_norm: str = "rms_norm", ) -> None: super().__init__() hidden_size = num_attention_heads * attention_head_dim self.norm1 = AdaLayerNormZero(hidden_size, norm_type="layer_norm") self.norm1_context = AdaLayerNormZero(hidden_size, norm_type="layer_norm") self.attn = Attention( query_dim=hidden_size, cross_attention_dim=None, added_kv_proj_dim=hidden_size, dim_head=attention_head_dim, heads=num_attention_heads, out_dim=hidden_size, context_pre_only=False, bias=True, processor=HunyuanImageAttnProcessor(), qk_norm=qk_norm, eps=1e-6, ) self.norm2 = nn.LayerNorm(hidden_size, elementwise_affine=False, eps=1e-6) self.ff = FeedForward(hidden_size, mult=mlp_ratio, activation_fn="gelu-approximate") self.norm2_context = nn.LayerNorm(hidden_size, elementwise_affine=False, eps=1e-6) self.ff_context = FeedForward(hidden_size, mult=mlp_ratio, activation_fn="gelu-approximate") def forward( self, hidden_states: torch.Tensor, encoder_hidden_states: torch.Tensor, temb: torch.Tensor, attention_mask: torch.Tensor | None = None, image_rotary_emb: tuple[torch.Tensor, torch.Tensor] | None = None, *args, **kwargs, ) -> tuple[torch.Tensor, torch.Tensor]: # 1. Input normalization norm_hidden_states, gate_msa, shift_mlp, scale_mlp, gate_mlp = self.norm1(hidden_states, emb=temb) norm_encoder_hidden_states, c_gate_msa, c_shift_mlp, c_scale_mlp, c_gate_mlp = self.norm1_context( encoder_hidden_states, emb=temb ) # 2. Joint attention attn_output, context_attn_output = self.attn( hidden_states=norm_hidden_states, encoder_hidden_states=norm_encoder_hidden_states, attention_mask=attention_mask, image_rotary_emb=image_rotary_emb, ) # 3. Modulation and residual connection hidden_states = hidden_states + attn_output * gate_msa.unsqueeze(1) encoder_hidden_states = encoder_hidden_states + context_attn_output * c_gate_msa.unsqueeze(1) norm_hidden_states = self.norm2(hidden_states) norm_encoder_hidden_states = self.norm2_context(encoder_hidden_states) norm_hidden_states = norm_hidden_states * (1 + scale_mlp[:, None]) + shift_mlp[:, None] norm_encoder_hidden_states = norm_encoder_hidden_states * (1 + c_scale_mlp[:, None]) + c_shift_mlp[:, None] # 4. Feed-forward ff_output = self.ff(norm_hidden_states) context_ff_output = self.ff_context(norm_encoder_hidden_states) hidden_states = hidden_states + gate_mlp.unsqueeze(1) * ff_output encoder_hidden_states = encoder_hidden_states + c_gate_mlp.unsqueeze(1) * context_ff_output return hidden_states, encoder_hidden_states class HunyuanImageTransformer2DModel( ModelMixin, ConfigMixin, AttentionMixin, PeftAdapterMixin, FromOriginalModelMixin, CacheMixin ): r""" The Transformer model used in [HunyuanImage-2.1](https://github.com/Tencent-Hunyuan/HunyuanImage-2.1). Args: in_channels (`int`, defaults to `16`): The number of channels in the input. out_channels (`int`, defaults to `16`): The number of channels in the output. num_attention_heads (`int`, defaults to `24`): The number of heads to use for multi-head attention. attention_head_dim (`int`, defaults to `128`): The number of channels in each head. num_layers (`int`, defaults to `20`): The number of layers of dual-stream blocks to use. num_single_layers (`int`, defaults to `40`): The number of layers of single-stream blocks to use. num_refiner_layers (`int`, defaults to `2`): The number of layers of refiner blocks to use. mlp_ratio (`float`, defaults to `4.0`): The ratio of the hidden layer size to the input size in the feedforward network. patch_size (`int`, defaults to `2`): The size of the spatial patches to use in the patch embedding layer. patch_size_t (`int`, defaults to `1`): The size of the tmeporal patches to use in the patch embedding layer. qk_norm (`str`, defaults to `rms_norm`): The normalization to use for the query and key projections in the attention layers. guidance_embeds (`bool`, defaults to `True`): Whether to use guidance embeddings in the model. text_embed_dim (`int`, defaults to `4096`): Input dimension of text embeddings from the text encoder. pooled_projection_dim (`int`, defaults to `768`): The dimension of the pooled projection of the text embeddings. rope_theta (`float`, defaults to `256.0`): The value of theta to use in the RoPE layer. rope_axes_dim (`tuple[int]`, defaults to `(16, 56, 56)`): The dimensions of the axes to use in the RoPE layer. image_condition_type (`str`, *optional*, defaults to `None`): The type of image conditioning to use. If `None`, no image conditioning is used. If `latent_concat`, the image is concatenated to the latent stream. If `token_replace`, the image is used to replace first-frame tokens in the latent stream and apply conditioning. """ _supports_gradient_checkpointing = True _skip_layerwise_casting_patterns = ["x_embedder", "context_embedder", "norm"] _no_split_modules = [ "HunyuanImageTransformerBlock", "HunyuanImageSingleTransformerBlock", "HunyuanImagePatchEmbed", "HunyuanImageTokenRefiner", ] _repeated_blocks = ["HunyuanImageTransformerBlock", "HunyuanImageSingleTransformerBlock"] @register_to_config def __init__( self, in_channels: int = 64, out_channels: int = 64, num_attention_heads: int = 28, attention_head_dim: int = 128, num_layers: int = 20, num_single_layers: int = 40, num_refiner_layers: int = 2, mlp_ratio: float = 4.0, patch_size: tuple[int, int] = (1, 1), qk_norm: str = "rms_norm", guidance_embeds: bool = False, text_embed_dim: int = 3584, text_embed_2_dim: int | None = None, rope_theta: float = 256.0, rope_axes_dim: tuple[int, ...] = (64, 64), use_meanflow: bool = False, ) -> None: super().__init__() if not (isinstance(patch_size, (tuple, list)) and len(patch_size) in [2, 3]): raise ValueError(f"patch_size must be a tuple of length 2 or 3, got {patch_size}") inner_dim = num_attention_heads * attention_head_dim out_channels = out_channels or in_channels # 1. Latent and condition embedders self.x_embedder = HunyuanImagePatchEmbed(patch_size, in_channels, inner_dim) self.context_embedder = HunyuanImageTokenRefiner( text_embed_dim, num_attention_heads, attention_head_dim, num_layers=num_refiner_layers ) if text_embed_2_dim is not None: self.context_embedder_2 = HunyuanImageByT5TextProjection(text_embed_2_dim, 2048, inner_dim) else: self.context_embedder_2 = None self.time_guidance_embed = HunyuanImageCombinedTimeGuidanceEmbedding(inner_dim, guidance_embeds, use_meanflow) # 2. RoPE self.rope = HunyuanImageRotaryPosEmbed(patch_size, rope_axes_dim, rope_theta) # 3. Dual stream transformer blocks self.transformer_blocks = nn.ModuleList( [ HunyuanImageTransformerBlock( num_attention_heads, attention_head_dim, mlp_ratio=mlp_ratio, qk_norm=qk_norm ) for _ in range(num_layers) ] ) # 4. Single stream transformer blocks self.single_transformer_blocks = nn.ModuleList( [ HunyuanImageSingleTransformerBlock( num_attention_heads, attention_head_dim, mlp_ratio=mlp_ratio, qk_norm=qk_norm ) for _ in range(num_single_layers) ] ) # 5. Output projection self.norm_out = AdaLayerNormContinuous(inner_dim, inner_dim, elementwise_affine=False, eps=1e-6) self.proj_out = nn.Linear(inner_dim, math.prod(patch_size) * out_channels) self.gradient_checkpointing = False @apply_lora_scale("attention_kwargs") def forward( self, hidden_states: torch.Tensor, timestep: torch.LongTensor, encoder_hidden_states: torch.Tensor, encoder_attention_mask: torch.Tensor, timestep_r: torch.LongTensor | None = None, encoder_hidden_states_2: torch.Tensor | None = None, encoder_attention_mask_2: torch.Tensor | None = None, guidance: torch.Tensor | None = None, attention_kwargs: dict[str, Any] | None = None, return_dict: bool = True, ) -> torch.Tensor | dict[str, torch.Tensor]: if hidden_states.ndim == 4: batch_size, channels, height, width = hidden_states.shape sizes = (height, width) elif hidden_states.ndim == 5: batch_size, channels, frame, height, width = hidden_states.shape sizes = (frame, height, width) else: raise ValueError(f"hidden_states must be a 4D or 5D tensor, got {hidden_states.shape}") post_patch_sizes = tuple(d // p for d, p in zip(sizes, self.config.patch_size)) # 1. RoPE image_rotary_emb = self.rope(hidden_states) # 2. Conditional embeddings encoder_attention_mask = encoder_attention_mask.bool() temb = self.time_guidance_embed(timestep, guidance=guidance, timestep_r=timestep_r) hidden_states = self.x_embedder(hidden_states) encoder_hidden_states = self.context_embedder(encoder_hidden_states, timestep, encoder_attention_mask) if self.context_embedder_2 is not None and encoder_hidden_states_2 is not None: encoder_hidden_states_2 = self.context_embedder_2(encoder_hidden_states_2) encoder_attention_mask_2 = encoder_attention_mask_2.bool() # reorder and combine text tokens: combine valid tokens first, then padding new_encoder_hidden_states = [] new_encoder_attention_mask = [] for text, text_mask, text_2, text_mask_2 in zip( encoder_hidden_states, encoder_attention_mask, encoder_hidden_states_2, encoder_attention_mask_2 ): # Concatenate: [valid_mllm, valid_byt5, invalid_mllm, invalid_byt5] new_encoder_hidden_states.append( torch.cat( [ text_2[text_mask_2], # valid byt5 text[text_mask], # valid mllm text_2[~text_mask_2], # invalid byt5 text[~text_mask], # invalid mllm ], dim=0, ) ) # Apply same reordering to attention masks new_encoder_attention_mask.append( torch.cat( [ text_mask_2[text_mask_2], text_mask[text_mask], text_mask_2[~text_mask_2], text_mask[~text_mask], ], dim=0, ) ) encoder_hidden_states = torch.stack(new_encoder_hidden_states) encoder_attention_mask = torch.stack(new_encoder_attention_mask) attention_mask = torch.nn.functional.pad(encoder_attention_mask, (hidden_states.shape[1], 0), value=True) attention_mask = attention_mask.unsqueeze(1).unsqueeze(2) # 3. Transformer blocks if torch.is_grad_enabled() and self.gradient_checkpointing: for block in self.transformer_blocks: hidden_states, encoder_hidden_states = self._gradient_checkpointing_func( block, hidden_states, encoder_hidden_states, temb, attention_mask=attention_mask, image_rotary_emb=image_rotary_emb, ) for block in self.single_transformer_blocks: hidden_states, encoder_hidden_states = self._gradient_checkpointing_func( block, hidden_states, encoder_hidden_states, temb, attention_mask=attention_mask, image_rotary_emb=image_rotary_emb, ) else: for block in self.transformer_blocks: hidden_states, encoder_hidden_states = block( hidden_states, encoder_hidden_states, temb, attention_mask=attention_mask, image_rotary_emb=image_rotary_emb, ) for block in self.single_transformer_blocks: hidden_states, encoder_hidden_states = block( hidden_states, encoder_hidden_states, temb, attention_mask=attention_mask, image_rotary_emb=image_rotary_emb, ) # 4. Output projection hidden_states = self.norm_out(hidden_states, temb) hidden_states = self.proj_out(hidden_states) # 5. unpatchify # reshape: [batch_size, *post_patch_dims, channels, *patch_size] out_channels = self.config.out_channels reshape_dims = [batch_size] + list(post_patch_sizes) + [out_channels] + list(self.config.patch_size) hidden_states = hidden_states.reshape(*reshape_dims) # create permutation pattern: batch, channels, then interleave post_patch and patch dims # For 4D: [0, 3, 1, 4, 2, 5] -> batch, channels, post_patch_height, patch_size_height, post_patch_width, patch_size_width # For 5D: [0, 4, 1, 5, 2, 6, 3, 7] -> batch, channels, post_patch_frame, patch_size_frame, post_patch_height, patch_size_height, post_patch_width, patch_size_width ndim = len(post_patch_sizes) permute_pattern = [0, ndim + 1] # batch, channels for i in range(ndim): permute_pattern.extend([i + 1, ndim + 2 + i]) # post_patch_sizes[i], patch_sizes[i] hidden_states = hidden_states.permute(*permute_pattern) # flatten patch dimensions: flatten each (post_patch_size, patch_size) pair # batch_size, channels, post_patch_sizes[0] * patch_sizes[0], post_patch_sizes[1] * patch_sizes[1], ... final_dims = [batch_size, out_channels] + [ post_patch * patch for post_patch, patch in zip(post_patch_sizes, self.config.patch_size) ] hidden_states = hidden_states.reshape(*final_dims) if not return_dict: return (hidden_states,) return Transformer2DModelOutput(sample=hidden_states)
{ "repo_id": "huggingface/diffusers", "file_path": "src/diffusers/models/transformers/transformer_hunyuanimage.py", "license": "Apache License 2.0", "lines": 747, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
huggingface/diffusers:src/diffusers/pipelines/hunyuan_image/pipeline_hunyuanimage.py
# Copyright 2025 Hunyuan-Image Team and The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from __future__ import annotations import inspect import re from typing import Any, Callable import numpy as np import torch from transformers import ByT5Tokenizer, Qwen2_5_VLForConditionalGeneration, Qwen2Tokenizer, T5EncoderModel from ...guiders import AdaptiveProjectedMixGuidance from ...image_processor import VaeImageProcessor from ...models import AutoencoderKLHunyuanImage, HunyuanImageTransformer2DModel from ...schedulers import FlowMatchEulerDiscreteScheduler from ...utils import is_torch_xla_available, logging, replace_example_docstring from ...utils.torch_utils import randn_tensor from ..pipeline_utils import DiffusionPipeline from .pipeline_output import HunyuanImagePipelineOutput if is_torch_xla_available(): import torch_xla.core.xla_model as xm XLA_AVAILABLE = True else: XLA_AVAILABLE = False logger = logging.get_logger(__name__) # pylint: disable=invalid-name EXAMPLE_DOC_STRING = """ Examples: ```py >>> import torch >>> from diffusers import HunyuanImagePipeline >>> pipe = HunyuanImagePipeline.from_pretrained( ... "hunyuanvideo-community/HunyuanImage-2.1-Diffusers", torch_dtype=torch.bfloat16 ... ) >>> pipe.to("cuda") >>> prompt = "A cat holding a sign that says hello world" >>> # Depending on the variant being used, the pipeline call will slightly vary. >>> # Refer to the pipeline documentation for more details. >>> image = pipe(prompt, negative_prompt="", num_inference_steps=50).images[0] >>> image.save("hunyuanimage.png") ``` """ def extract_glyph_text(prompt: str): """ Extract text enclosed in quotes for glyph rendering. Finds text in single quotes, double quotes, and Chinese quotes, then formats it for byT5 processing. Args: prompt: Input text prompt Returns: Formatted glyph text string or None if no quoted text found """ text_prompt_texts = [] pattern_quote_single = r"\'(.*?)\'" pattern_quote_double = r"\"(.*?)\"" pattern_quote_chinese_single = r"‘(.*?)’" pattern_quote_chinese_double = r"“(.*?)”" matches_quote_single = re.findall(pattern_quote_single, prompt) matches_quote_double = re.findall(pattern_quote_double, prompt) matches_quote_chinese_single = re.findall(pattern_quote_chinese_single, prompt) matches_quote_chinese_double = re.findall(pattern_quote_chinese_double, prompt) text_prompt_texts.extend(matches_quote_single) text_prompt_texts.extend(matches_quote_double) text_prompt_texts.extend(matches_quote_chinese_single) text_prompt_texts.extend(matches_quote_chinese_double) if text_prompt_texts: glyph_text_formatted = ". ".join([f'Text "{text}"' for text in text_prompt_texts]) + ". " else: glyph_text_formatted = None return glyph_text_formatted # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.retrieve_timesteps def retrieve_timesteps( scheduler, num_inference_steps: int | None = None, device: str | torch.device | None = None, timesteps: list[int] | None = None, sigmas: list[float] | None = None, **kwargs, ): r""" Calls the scheduler's `set_timesteps` method and retrieves timesteps from the scheduler after the call. Handles custom timesteps. Any kwargs will be supplied to `scheduler.set_timesteps`. Args: scheduler (`SchedulerMixin`): The scheduler to get timesteps from. num_inference_steps (`int`): The number of diffusion steps used when generating samples with a pre-trained model. If used, `timesteps` must be `None`. device (`str` or `torch.device`, *optional*): The device to which the timesteps should be moved to. If `None`, the timesteps are not moved. timesteps (`list[int]`, *optional*): Custom timesteps used to override the timestep spacing strategy of the scheduler. If `timesteps` is passed, `num_inference_steps` and `sigmas` must be `None`. sigmas (`list[float]`, *optional*): Custom sigmas used to override the timestep spacing strategy of the scheduler. If `sigmas` is passed, `num_inference_steps` and `timesteps` must be `None`. Returns: `tuple[torch.Tensor, int]`: A tuple where the first element is the timestep schedule from the scheduler and the second element is the number of inference steps. """ if timesteps is not None and sigmas is not None: raise ValueError("Only one of `timesteps` or `sigmas` can be passed. Please choose one to set custom values") if timesteps is not None: accepts_timesteps = "timesteps" in set(inspect.signature(scheduler.set_timesteps).parameters.keys()) if not accepts_timesteps: raise ValueError( f"The current scheduler class {scheduler.__class__}'s `set_timesteps` does not support custom" f" timestep schedules. Please check whether you are using the correct scheduler." ) scheduler.set_timesteps(timesteps=timesteps, device=device, **kwargs) timesteps = scheduler.timesteps num_inference_steps = len(timesteps) elif sigmas is not None: accept_sigmas = "sigmas" in set(inspect.signature(scheduler.set_timesteps).parameters.keys()) if not accept_sigmas: raise ValueError( f"The current scheduler class {scheduler.__class__}'s `set_timesteps` does not support custom" f" sigmas schedules. Please check whether you are using the correct scheduler." ) scheduler.set_timesteps(sigmas=sigmas, device=device, **kwargs) timesteps = scheduler.timesteps num_inference_steps = len(timesteps) else: scheduler.set_timesteps(num_inference_steps, device=device, **kwargs) timesteps = scheduler.timesteps return timesteps, num_inference_steps class HunyuanImagePipeline(DiffusionPipeline): r""" The HunyuanImage pipeline for text-to-image generation. Args: transformer ([`HunyuanImageTransformer2DModel`]): Conditional Transformer (MMDiT) architecture to denoise the encoded image latents. scheduler ([`FlowMatchEulerDiscreteScheduler`]): A scheduler to be used in combination with `transformer` to denoise the encoded image latents. vae ([`AutoencoderKLHunyuanImage`]): Variational Auto-Encoder (VAE) Model to encode and decode images to and from latent representations. text_encoder ([`Qwen2.5-VL-7B-Instruct`]): [Qwen2.5-VL-7B-Instruct](https://huggingface.co/Qwen/Qwen2.5-VL-7B-Instruct), specifically the [Qwen2.5-VL-7B-Instruct](https://huggingface.co/Qwen/Qwen2.5-VL-7B-Instruct) variant. tokenizer (`Qwen2Tokenizer`): Tokenizer of class [Qwen2Tokenizer]. text_encoder_2 ([`T5EncoderModel`]): [T5EncoderModel](https://huggingface.co/docs/transformers/en/model_doc/t5#transformers.T5EncoderModel) variant. tokenizer_2 (`ByT5Tokenizer`): Tokenizer of class [ByT5Tokenizer] guider ([`AdaptiveProjectedMixGuidance`]): [AdaptiveProjectedMixGuidance]to be used to guide the image generation. ocr_guider ([`AdaptiveProjectedMixGuidance`], *optional*): [AdaptiveProjectedMixGuidance] to be used to guide the image generation when text rendering is needed. """ model_cpu_offload_seq = "text_encoder->text_encoder_2->transformer->vae" _callback_tensor_inputs = ["latents", "prompt_embeds"] _optional_components = ["ocr_guider", "guider"] def __init__( self, scheduler: FlowMatchEulerDiscreteScheduler, vae: AutoencoderKLHunyuanImage, text_encoder: Qwen2_5_VLForConditionalGeneration, tokenizer: Qwen2Tokenizer, text_encoder_2: T5EncoderModel, tokenizer_2: ByT5Tokenizer, transformer: HunyuanImageTransformer2DModel, guider: AdaptiveProjectedMixGuidance | None = None, ocr_guider: AdaptiveProjectedMixGuidance | None = None, ): super().__init__() self.register_modules( vae=vae, text_encoder=text_encoder, tokenizer=tokenizer, text_encoder_2=text_encoder_2, tokenizer_2=tokenizer_2, transformer=transformer, scheduler=scheduler, guider=guider, ocr_guider=ocr_guider, ) self.vae_scale_factor = self.vae.config.spatial_compression_ratio if getattr(self, "vae", None) else 32 self.image_processor = VaeImageProcessor(vae_scale_factor=self.vae_scale_factor) self.tokenizer_max_length = 1000 self.tokenizer_2_max_length = 128 self.prompt_template_encode = "<|im_start|>system\nDescribe the image by detailing the color, shape, size, texture, quantity, text, spatial relationships of the objects and background:<|im_end|>\n<|im_start|>user\n{}<|im_end|>" self.prompt_template_encode_start_idx = 34 self.default_sample_size = 64 def _get_qwen_prompt_embeds( self, tokenizer: Qwen2Tokenizer, text_encoder: Qwen2_5_VLForConditionalGeneration, prompt: str | list[str] = None, device: torch.device | None = None, dtype: torch.dtype | None = None, tokenizer_max_length: int = 1000, template: str = "<|im_start|>system\nDescribe the image by detailing the color, shape, size, texture, quantity, text, spatial relationships of the objects and background:<|im_end|>\n<|im_start|>user\n{}<|im_end|>", drop_idx: int = 34, hidden_state_skip_layer: int = 2, ): device = device or self._execution_device dtype = dtype or text_encoder.dtype prompt = [prompt] if isinstance(prompt, str) else prompt txt = [template.format(e) for e in prompt] txt_tokens = tokenizer( txt, max_length=tokenizer_max_length + drop_idx, padding="max_length", truncation=True, return_tensors="pt" ).to(device) encoder_hidden_states = text_encoder( input_ids=txt_tokens.input_ids, attention_mask=txt_tokens.attention_mask, output_hidden_states=True, ) prompt_embeds = encoder_hidden_states.hidden_states[-(hidden_state_skip_layer + 1)] prompt_embeds = prompt_embeds[:, drop_idx:] encoder_attention_mask = txt_tokens.attention_mask[:, drop_idx:] prompt_embeds = prompt_embeds.to(dtype=dtype, device=device) encoder_attention_mask = encoder_attention_mask.to(device=device) return prompt_embeds, encoder_attention_mask def _get_byt5_prompt_embeds( self, tokenizer: ByT5Tokenizer, text_encoder: T5EncoderModel, prompt: str, device: torch.device | None = None, dtype: torch.dtype | None = None, tokenizer_max_length: int = 128, ): device = device or self._execution_device dtype = dtype or text_encoder.dtype if isinstance(prompt, list): raise ValueError("byt5 prompt should be a string") elif prompt is None: raise ValueError("byt5 prompt should not be None") txt_tokens = tokenizer( prompt, padding="max_length", max_length=tokenizer_max_length, truncation=True, add_special_tokens=True, return_tensors="pt", ).to(device) prompt_embeds = text_encoder( input_ids=txt_tokens.input_ids, attention_mask=txt_tokens.attention_mask.float(), )[0] prompt_embeds = prompt_embeds.to(dtype=dtype, device=device) encoder_attention_mask = txt_tokens.attention_mask.to(device=device) return prompt_embeds, encoder_attention_mask def encode_prompt( self, prompt: str | list[str], device: torch.device | None = None, batch_size: int = 1, num_images_per_prompt: int = 1, prompt_embeds: torch.Tensor | None = None, prompt_embeds_mask: torch.Tensor | None = None, prompt_embeds_2: torch.Tensor | None = None, prompt_embeds_mask_2: torch.Tensor | None = None, ): r""" Args: prompt (`str` or `list[str]`, *optional*): prompt to be encoded device: (`torch.device`): torch device batch_size (`int`): batch size of prompts, defaults to 1 num_images_per_prompt (`int`): number of images that should be generated per prompt prompt_embeds (`torch.Tensor`, *optional*): Pre-generated text embeddings. If not provided, text embeddings will be generated from `prompt` input argument. prompt_embeds_mask (`torch.Tensor`, *optional*): Pre-generated text mask. If not provided, text mask will be generated from `prompt` input argument. prompt_embeds_2 (`torch.Tensor`, *optional*): Pre-generated glyph text embeddings from ByT5. If not provided, will be generated from `prompt` input argument using self.tokenizer_2 and self.text_encoder_2. prompt_embeds_mask_2 (`torch.Tensor`, *optional*): Pre-generated glyph text mask from ByT5. If not provided, will be generated from `prompt` input argument using self.tokenizer_2 and self.text_encoder_2. """ device = device or self._execution_device if prompt is None: prompt = [""] * batch_size prompt = [prompt] if isinstance(prompt, str) else prompt if prompt_embeds is None: prompt_embeds, prompt_embeds_mask = self._get_qwen_prompt_embeds( tokenizer=self.tokenizer, text_encoder=self.text_encoder, prompt=prompt, device=device, tokenizer_max_length=self.tokenizer_max_length, template=self.prompt_template_encode, drop_idx=self.prompt_template_encode_start_idx, ) if prompt_embeds_2 is None: prompt_embeds_2_list = [] prompt_embeds_mask_2_list = [] glyph_texts = [extract_glyph_text(p) for p in prompt] for glyph_text in glyph_texts: if glyph_text is None: glyph_text_embeds = torch.zeros( (1, self.tokenizer_2_max_length, self.text_encoder_2.config.d_model), device=device ) glyph_text_embeds_mask = torch.zeros( (1, self.tokenizer_2_max_length), device=device, dtype=torch.int64 ) else: glyph_text_embeds, glyph_text_embeds_mask = self._get_byt5_prompt_embeds( tokenizer=self.tokenizer_2, text_encoder=self.text_encoder_2, prompt=glyph_text, device=device, tokenizer_max_length=self.tokenizer_2_max_length, ) prompt_embeds_2_list.append(glyph_text_embeds) prompt_embeds_mask_2_list.append(glyph_text_embeds_mask) prompt_embeds_2 = torch.cat(prompt_embeds_2_list, dim=0) prompt_embeds_mask_2 = torch.cat(prompt_embeds_mask_2_list, dim=0) _, seq_len, _ = prompt_embeds.shape prompt_embeds = prompt_embeds.repeat(1, num_images_per_prompt, 1) prompt_embeds = prompt_embeds.view(batch_size * num_images_per_prompt, seq_len, -1) prompt_embeds_mask = prompt_embeds_mask.repeat(1, num_images_per_prompt, 1) prompt_embeds_mask = prompt_embeds_mask.view(batch_size * num_images_per_prompt, seq_len) _, seq_len_2, _ = prompt_embeds_2.shape prompt_embeds_2 = prompt_embeds_2.repeat(1, num_images_per_prompt, 1) prompt_embeds_2 = prompt_embeds_2.view(batch_size * num_images_per_prompt, seq_len_2, -1) prompt_embeds_mask_2 = prompt_embeds_mask_2.repeat(1, num_images_per_prompt, 1) prompt_embeds_mask_2 = prompt_embeds_mask_2.view(batch_size * num_images_per_prompt, seq_len_2) return prompt_embeds, prompt_embeds_mask, prompt_embeds_2, prompt_embeds_mask_2 def check_inputs( self, prompt, height, width, negative_prompt=None, prompt_embeds=None, negative_prompt_embeds=None, prompt_embeds_mask=None, negative_prompt_embeds_mask=None, prompt_embeds_2=None, prompt_embeds_mask_2=None, negative_prompt_embeds_2=None, negative_prompt_embeds_mask_2=None, callback_on_step_end_tensor_inputs=None, ): if height % (self.vae_scale_factor * 2) != 0 or width % (self.vae_scale_factor * 2) != 0: logger.warning( f"`height` and `width` have to be divisible by {self.vae_scale_factor * 2} but are {height} and {width}. Dimensions will be resized accordingly" ) if callback_on_step_end_tensor_inputs is not None and not all( k in self._callback_tensor_inputs for k in callback_on_step_end_tensor_inputs ): raise ValueError( f"`callback_on_step_end_tensor_inputs` has to be in {self._callback_tensor_inputs}, but found {[k for k in callback_on_step_end_tensor_inputs if k not in self._callback_tensor_inputs]}" ) if prompt is not None and prompt_embeds is not None: raise ValueError( f"Cannot forward both `prompt`: {prompt} and `prompt_embeds`: {prompt_embeds}. Please make sure to" " only forward one of the two." ) elif prompt is None and prompt_embeds is None: raise ValueError( "Provide either `prompt` or `prompt_embeds`. Cannot leave both `prompt` and `prompt_embeds` undefined." ) elif prompt is not None and (not isinstance(prompt, str) and not isinstance(prompt, list)): raise ValueError(f"`prompt` has to be of type `str` or `list` but is {type(prompt)}") if negative_prompt is not None and negative_prompt_embeds is not None: raise ValueError( f"Cannot forward both `negative_prompt`: {negative_prompt} and `negative_prompt_embeds`:" f" {negative_prompt_embeds}. Please make sure to only forward one of the two." ) if prompt_embeds is not None and prompt_embeds_mask is None: raise ValueError( "If `prompt_embeds` are provided, `prompt_embeds_mask` also have to be passed. Make sure to generate `prompt_embeds_mask` from the same text encoder that was used to generate `prompt_embeds`." ) if negative_prompt_embeds is not None and negative_prompt_embeds_mask is None: raise ValueError( "If `negative_prompt_embeds` are provided, `negative_prompt_embeds_mask` also have to be passed. Make sure to generate `negative_prompt_embeds_mask` from the same text encoder that was used to generate `negative_prompt_embeds`." ) if prompt is None and prompt_embeds_2 is None: raise ValueError( "Provide either `prompt` or `prompt_embeds_2`. Cannot leave both `prompt` and `prompt_embeds_2` undefined." ) if prompt_embeds_2 is not None and prompt_embeds_mask_2 is None: raise ValueError( "If `prompt_embeds_2` are provided, `prompt_embeds_mask_2` also have to be passed. Make sure to generate `prompt_embeds_mask_2` from the same text encoder that was used to generate `prompt_embeds_2`." ) if negative_prompt_embeds_2 is not None and negative_prompt_embeds_mask_2 is None: raise ValueError( "If `negative_prompt_embeds_2` are provided, `negative_prompt_embeds_mask_2` also have to be passed. Make sure to generate `negative_prompt_embeds_mask_2` from the same text encoder that was used to generate `negative_prompt_embeds_2`." ) def prepare_latents( self, batch_size, num_channels_latents, height, width, dtype, device, generator, latents=None, ): height = int(height) // self.vae_scale_factor width = int(width) // self.vae_scale_factor shape = (batch_size, num_channels_latents, height, width) if latents is not None: return latents.to(device=device, dtype=dtype) if isinstance(generator, list) and len(generator) != batch_size: raise ValueError( f"You have passed a list of generators of length {len(generator)}, but requested an effective batch" f" size of {batch_size}. Make sure the batch size matches the length of the generators." ) latents = randn_tensor(shape, generator=generator, device=device, dtype=dtype) return latents @property def attention_kwargs(self): return self._attention_kwargs @property def num_timesteps(self): return self._num_timesteps @property def current_timestep(self): return self._current_timestep @property def interrupt(self): return self._interrupt @torch.no_grad() @replace_example_docstring(EXAMPLE_DOC_STRING) def __call__( self, prompt: str | list[str] = None, negative_prompt: str | list[str] = None, height: int | None = None, width: int | None = None, num_inference_steps: int = 50, distilled_guidance_scale: float | None = 3.25, sigmas: list[float] | None = None, num_images_per_prompt: int = 1, generator: torch.Generator | list[torch.Generator] | None = None, latents: torch.Tensor | None = None, prompt_embeds: torch.Tensor | None = None, prompt_embeds_mask: torch.Tensor | None = None, negative_prompt_embeds: torch.Tensor | None = None, negative_prompt_embeds_mask: torch.Tensor | None = None, prompt_embeds_2: torch.Tensor | None = None, prompt_embeds_mask_2: torch.Tensor | None = None, negative_prompt_embeds_2: torch.Tensor | None = None, negative_prompt_embeds_mask_2: torch.Tensor | None = None, output_type: str | None = "pil", return_dict: bool = True, attention_kwargs: dict[str, Any] | None = None, callback_on_step_end: Callable[[int, int], None] | None = None, callback_on_step_end_tensor_inputs: list[str] = ["latents"], ): r""" Function invoked when calling the pipeline for generation. Args: prompt (`str` or `list[str]`, *optional*): The prompt or prompts to guide the image generation. If not defined, one has to pass `prompt_embeds`. instead. negative_prompt (`str` or `list[str]`, *optional*): The prompt or prompts not to guide the image generation. If not defined and negative_prompt_embeds is not provided, will use an empty negative prompt. Ignored when not using guidance. ). height (`int`, *optional*, defaults to self.unet.config.sample_size * self.vae_scale_factor): The height in pixels of the generated image. This is set to 1024 by default for the best results. width (`int`, *optional*, defaults to self.unet.config.sample_size * self.vae_scale_factor): The width in pixels of the generated image. This is set to 1024 by default for the best results. num_inference_steps (`int`, *optional*, defaults to 50): The number of denoising steps. More denoising steps usually lead to a higher quality image at the expense of slower inference. sigmas (`list[float]`, *optional*): Custom sigmas to use for the denoising process with schedulers which support a `sigmas` argument in their `set_timesteps` method. If not defined, the default behavior when `num_inference_steps` is passed will be used. distilled_guidance_scale (`float`, *optional*, defaults to None): A guidance scale value for guidance distilled models. Unlike the traditional classifier-free guidance where the guidance scale is applied during inference through noise prediction rescaling, guidance distilled models take the guidance scale directly as an input parameter during forward pass. Guidance is enabled by setting `distilled_guidance_scale > 1`. Higher guidance scale encourages to generate images that are closely linked to the text `prompt`, usually at the expense of lower image quality. For guidance distilled models, this parameter is required. For non-distilled models, this parameter will be ignored. num_images_per_prompt (`int`, *optional*, defaults to 1): The number of images to generate per prompt. generator (`torch.Generator` or `list[torch.Generator]`, *optional*): One or a list of [torch generator(s)](https://pytorch.org/docs/stable/generated/torch.Generator.html) to make generation deterministic. latents (`torch.Tensor`, *optional*): Pre-generated noisy latents, sampled from a Gaussian distribution, to be used as inputs for image generation. Can be used to tweak the same generation with different prompts. If not provided, a latents tensor will be generated by sampling using the supplied random `generator`. prompt_embeds (`torch.Tensor`, *optional*): Pre-generated text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt weighting. If not provided, text embeddings will be generated from `prompt` input argument. prompt_embeds_mask (`torch.Tensor`, *optional*): Pre-generated text embeddings mask. Can be used to easily tweak text inputs, *e.g.* prompt weighting. If not provided, text embeddings mask will be generated from `prompt` input argument. prompt_embeds_2 (`torch.Tensor`, *optional*): Pre-generated text embeddings for ocr. Can be used to easily tweak text inputs, *e.g.* prompt weighting. If not provided, text embeddings for ocr will be generated from `prompt` input argument. prompt_embeds_mask_2 (`torch.Tensor`, *optional*): Pre-generated text embeddings mask for ocr. Can be used to easily tweak text inputs, *e.g.* prompt weighting. If not provided, text embeddings mask for ocr will be generated from `prompt` input argument. negative_prompt_embeds (`torch.Tensor`, *optional*): Pre-generated negative text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt weighting. If not provided, negative_prompt_embeds will be generated from `negative_prompt` input argument. negative_prompt_embeds_mask (`torch.Tensor`, *optional*): Pre-generated negative text embeddings mask. Can be used to easily tweak text inputs, *e.g.* prompt weighting. If not provided, negative text embeddings mask will be generated from `negative_prompt` input argument. negative_prompt_embeds_2 (`torch.Tensor`, *optional*): Pre-generated negative text embeddings for ocr. Can be used to easily tweak text inputs, *e.g.* prompt weighting. If not provided, negative text embeddings for ocr will be generated from `negative_prompt` input argument. negative_prompt_embeds_mask_2 (`torch.Tensor`, *optional*): Pre-generated negative text embeddings mask for ocr. Can be used to easily tweak text inputs, *e.g.* prompt weighting. If not provided, negative text embeddings mask for ocr will be generated from `negative_prompt` input argument. output_type (`str`, *optional*, defaults to `"pil"`): The output format of the generate image. Choose between [PIL](https://pillow.readthedocs.io/en/stable/): `PIL.Image.Image` or `np.array`. return_dict (`bool`, *optional*, defaults to `True`): Whether or not to return a [`~pipelines.qwenimage.QwenImagePipelineOutput`] instead of a plain tuple. attention_kwargs (`dict`, *optional*): A kwargs dictionary that if specified is passed along to the `AttentionProcessor` as defined under `self.processor` in [diffusers.models.attention_processor](https://github.com/huggingface/diffusers/blob/main/src/diffusers/models/attention_processor.py). callback_on_step_end (`Callable`, *optional*): A function that calls at the end of each denoising steps during the inference. The function is called with the following arguments: `callback_on_step_end(self: DiffusionPipeline, step: int, timestep: int, callback_kwargs: Dict)`. `callback_kwargs` will include a list of all tensors as specified by `callback_on_step_end_tensor_inputs`. callback_on_step_end_tensor_inputs (`List`, *optional*): The list of tensor inputs for the `callback_on_step_end` function. The tensors specified in the list will be passed as `callback_kwargs` argument. You will only be able to include variables listed in the `._callback_tensor_inputs` attribute of your pipeline class. Examples: Returns: [`~pipelines.hunyuan_image.HunyuanImagePipelineOutput`] or `tuple`: [`~pipelines.hunyuan_image.HunyuanImagePipelineOutput`] if `return_dict` is True, otherwise a `tuple`. When returning a tuple, the first element is a list with the generated images. """ height = height or self.default_sample_size * self.vae_scale_factor width = width or self.default_sample_size * self.vae_scale_factor # 1. Check inputs. Raise error if not correct self.check_inputs( prompt, height, width, negative_prompt=negative_prompt, prompt_embeds=prompt_embeds, negative_prompt_embeds=negative_prompt_embeds, prompt_embeds_mask=prompt_embeds_mask, negative_prompt_embeds_mask=negative_prompt_embeds_mask, callback_on_step_end_tensor_inputs=callback_on_step_end_tensor_inputs, prompt_embeds_2=prompt_embeds_2, prompt_embeds_mask_2=prompt_embeds_mask_2, negative_prompt_embeds_2=negative_prompt_embeds_2, negative_prompt_embeds_mask_2=negative_prompt_embeds_mask_2, ) self._attention_kwargs = attention_kwargs self._current_timestep = None self._interrupt = False # 2. Define call parameters if prompt is not None and isinstance(prompt, str): batch_size = 1 elif prompt is not None and isinstance(prompt, list): batch_size = len(prompt) else: batch_size = prompt_embeds.shape[0] device = self._execution_device # 3. prepare prompt embeds prompt_embeds, prompt_embeds_mask, prompt_embeds_2, prompt_embeds_mask_2 = self.encode_prompt( prompt=prompt, prompt_embeds=prompt_embeds, prompt_embeds_mask=prompt_embeds_mask, device=device, batch_size=batch_size, num_images_per_prompt=num_images_per_prompt, prompt_embeds_2=prompt_embeds_2, prompt_embeds_mask_2=prompt_embeds_mask_2, ) prompt_embeds = prompt_embeds.to(self.transformer.dtype) prompt_embeds_2 = prompt_embeds_2.to(self.transformer.dtype) # select guider if not torch.all(prompt_embeds_2 == 0) and self.ocr_guider is not None: # prompt contains ocr and pipeline has a guider for ocr guider = self.ocr_guider elif self.guider is not None: guider = self.guider # distilled model does not use guidance method, use default guider with enabled=False else: guider = AdaptiveProjectedMixGuidance(enabled=False) if guider._enabled and guider.num_conditions > 1: ( negative_prompt_embeds, negative_prompt_embeds_mask, negative_prompt_embeds_2, negative_prompt_embeds_mask_2, ) = self.encode_prompt( prompt=negative_prompt, prompt_embeds=negative_prompt_embeds, prompt_embeds_mask=negative_prompt_embeds_mask, device=device, batch_size=batch_size, num_images_per_prompt=num_images_per_prompt, prompt_embeds_2=negative_prompt_embeds_2, prompt_embeds_mask_2=negative_prompt_embeds_mask_2, ) negative_prompt_embeds = negative_prompt_embeds.to(self.transformer.dtype) negative_prompt_embeds_2 = negative_prompt_embeds_2.to(self.transformer.dtype) # 4. Prepare latent variables num_channels_latents = self.transformer.config.in_channels latents = self.prepare_latents( batch_size=batch_size * num_images_per_prompt, num_channels_latents=num_channels_latents, height=height, width=width, dtype=prompt_embeds.dtype, device=device, generator=generator, latents=latents, ) # 5. Prepare timesteps sigmas = np.linspace(1.0, 0.0, num_inference_steps + 1)[:-1] if sigmas is None else sigmas timesteps, num_inference_steps = retrieve_timesteps(self.scheduler, num_inference_steps, device, sigmas=sigmas) num_warmup_steps = max(len(timesteps) - num_inference_steps * self.scheduler.order, 0) self._num_timesteps = len(timesteps) # handle guidance (for guidance-distilled model) if self.transformer.config.guidance_embeds and distilled_guidance_scale is None: raise ValueError("`distilled_guidance_scale` is required for guidance-distilled model.") if self.transformer.config.guidance_embeds: guidance = ( torch.tensor( [distilled_guidance_scale] * latents.shape[0], dtype=self.transformer.dtype, device=device ) * 1000.0 ) else: guidance = None if self.attention_kwargs is None: self._attention_kwargs = {} # 6. Denoising loop self.scheduler.set_begin_index(0) with self.progress_bar(total=num_inference_steps) as progress_bar: for i, t in enumerate(timesteps): if self.interrupt: continue self._current_timestep = t # broadcast to batch dimension in a way that's compatible with ONNX/Core ML timestep = t.expand(latents.shape[0]).to(latents.dtype) if self.transformer.config.use_meanflow: if i == len(timesteps) - 1: timestep_r = torch.tensor([0.0], device=device) else: timestep_r = timesteps[i + 1] timestep_r = timestep_r.expand(latents.shape[0]).to(latents.dtype) else: timestep_r = None # Step 1: Collect model inputs needed for the guidance method # conditional inputs should always be first element in the tuple guider_inputs = { "encoder_hidden_states": (prompt_embeds, negative_prompt_embeds), "encoder_attention_mask": (prompt_embeds_mask, negative_prompt_embeds_mask), "encoder_hidden_states_2": (prompt_embeds_2, negative_prompt_embeds_2), "encoder_attention_mask_2": (prompt_embeds_mask_2, negative_prompt_embeds_mask_2), } # Step 2: Update guider's internal state for this denoising step guider.set_state(step=i, num_inference_steps=num_inference_steps, timestep=t) # Step 3: Prepare batched model inputs based on the guidance method # The guider splits model inputs into separate batches for conditional/unconditional predictions. # For CFG with guider_inputs = {"encoder_hidden_states": (prompt_embeds, negative_prompt_embeds)}: # you will get a guider_state with two batches: # guider_state = [ # {"encoder_hidden_states": prompt_embeds, "__guidance_identifier__": "pred_cond"}, # conditional batch # {"encoder_hidden_states": negative_prompt_embeds, "__guidance_identifier__": "pred_uncond"}, # unconditional batch # ] # Other guidance methods may return 1 batch (no guidance) or 3+ batches (e.g., PAG, APG). guider_state = guider.prepare_inputs(guider_inputs) # Step 4: Run the denoiser for each batch # Each batch in guider_state represents a different conditioning (conditional, unconditional, etc.). # We run the model once per batch and store the noise prediction in guider_state_batch.noise_pred. for guider_state_batch in guider_state: guider.prepare_models(self.transformer) # Extract conditioning kwargs for this batch (e.g., encoder_hidden_states) cond_kwargs = { input_name: getattr(guider_state_batch, input_name) for input_name in guider_inputs.keys() } # e.g. "pred_cond"/"pred_uncond" context_name = getattr(guider_state_batch, guider._identifier_key) with self.transformer.cache_context(context_name): # Run denoiser and store noise prediction in this batch guider_state_batch.noise_pred = self.transformer( hidden_states=latents, timestep=timestep, timestep_r=timestep_r, guidance=guidance, attention_kwargs=self.attention_kwargs, return_dict=False, **cond_kwargs, )[0] # Cleanup model (e.g., remove hooks) guider.cleanup_models(self.transformer) # Step 5: Combine predictions using the guidance method # The guider takes all noise predictions from guider_state and combines them according to the guidance algorithm. # Continuing the CFG example, the guider receives: # guider_state = [ # {"encoder_hidden_states": prompt_embeds, "noise_pred": noise_pred_cond, "__guidance_identifier__": "pred_cond"}, # batch 0 # {"encoder_hidden_states": negative_prompt_embeds, "noise_pred": noise_pred_uncond, "__guidance_identifier__": "pred_uncond"}, # batch 1 # ] # And extracts predictions using the __guidance_identifier__: # pred_cond = guider_state[0]["noise_pred"] # extracts noise_pred_cond # pred_uncond = guider_state[1]["noise_pred"] # extracts noise_pred_uncond # Then applies CFG formula: # noise_pred = pred_uncond + guidance_scale * (pred_cond - pred_uncond) # Returns GuiderOutput(pred=noise_pred, pred_cond=pred_cond, pred_uncond=pred_uncond) noise_pred = guider(guider_state)[0] # compute the previous noisy sample x_t -> x_t-1 latents_dtype = latents.dtype latents = self.scheduler.step(noise_pred, t, latents, return_dict=False)[0] if latents.dtype != latents_dtype: if torch.backends.mps.is_available(): # some platforms (eg. apple mps) misbehave due to a pytorch bug: https://github.com/pytorch/pytorch/pull/99272 latents = latents.to(latents_dtype) if callback_on_step_end is not None: callback_kwargs = {} for k in callback_on_step_end_tensor_inputs: callback_kwargs[k] = locals()[k] callback_outputs = callback_on_step_end(self, i, t, callback_kwargs) latents = callback_outputs.pop("latents", latents) prompt_embeds = callback_outputs.pop("prompt_embeds", prompt_embeds) # call the callback, if provided if i == len(timesteps) - 1 or ((i + 1) > num_warmup_steps and (i + 1) % self.scheduler.order == 0): progress_bar.update() if XLA_AVAILABLE: xm.mark_step() self._current_timestep = None if output_type == "latent": image = latents else: latents = latents.to(self.vae.dtype) / self.vae.config.scaling_factor image = self.vae.decode(latents, return_dict=False)[0] image = self.image_processor.postprocess(image, output_type=output_type) # Offload all models self.maybe_free_model_hooks() if not return_dict: return (image,) return HunyuanImagePipelineOutput(images=image)
{ "repo_id": "huggingface/diffusers", "file_path": "src/diffusers/pipelines/hunyuan_image/pipeline_hunyuanimage.py", "license": "Apache License 2.0", "lines": 753, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
huggingface/diffusers:src/diffusers/pipelines/hunyuan_image/pipeline_hunyuanimage_refiner.py
# Copyright 2025 Hunyuan-Image Team and The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from __future__ import annotations import inspect from typing import Any, Callable import numpy as np import torch from transformers import Qwen2_5_VLForConditionalGeneration, Qwen2Tokenizer from ...guiders import AdaptiveProjectedMixGuidance from ...image_processor import PipelineImageInput, VaeImageProcessor from ...models import AutoencoderKLHunyuanImageRefiner, HunyuanImageTransformer2DModel from ...schedulers import FlowMatchEulerDiscreteScheduler from ...utils import is_torch_xla_available, logging, replace_example_docstring from ...utils.torch_utils import randn_tensor from ..pipeline_utils import DiffusionPipeline from .pipeline_output import HunyuanImagePipelineOutput if is_torch_xla_available(): import torch_xla.core.xla_model as xm XLA_AVAILABLE = True else: XLA_AVAILABLE = False logger = logging.get_logger(__name__) # pylint: disable=invalid-name EXAMPLE_DOC_STRING = """ Examples: ```py >>> import torch >>> from diffusers import HunyuanImageRefinerPipeline >>> pipe = HunyuanImageRefinerPipeline.from_pretrained( ... "hunyuanvideo-community/HunyuanImage-2.1-Refiner-Diffusers", torch_dtype=torch.bfloat16 ... ) >>> pipe.to("cuda") >>> prompt = "A cat holding a sign that says hello world" >>> image = load_image("path/to/image.png") >>> # Depending on the variant being used, the pipeline call will slightly vary. >>> # Refer to the pipeline documentation for more details. >>> image = pipe(prompt, image=image, num_inference_steps=4).images[0] >>> image.save("hunyuanimage.png") ``` """ # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.retrieve_timesteps def retrieve_timesteps( scheduler, num_inference_steps: int | None = None, device: str | torch.device | None = None, timesteps: list[int] | None = None, sigmas: list[float] | None = None, **kwargs, ): r""" Calls the scheduler's `set_timesteps` method and retrieves timesteps from the scheduler after the call. Handles custom timesteps. Any kwargs will be supplied to `scheduler.set_timesteps`. Args: scheduler (`SchedulerMixin`): The scheduler to get timesteps from. num_inference_steps (`int`): The number of diffusion steps used when generating samples with a pre-trained model. If used, `timesteps` must be `None`. device (`str` or `torch.device`, *optional*): The device to which the timesteps should be moved to. If `None`, the timesteps are not moved. timesteps (`list[int]`, *optional*): Custom timesteps used to override the timestep spacing strategy of the scheduler. If `timesteps` is passed, `num_inference_steps` and `sigmas` must be `None`. sigmas (`list[float]`, *optional*): Custom sigmas used to override the timestep spacing strategy of the scheduler. If `sigmas` is passed, `num_inference_steps` and `timesteps` must be `None`. Returns: `tuple[torch.Tensor, int]`: A tuple where the first element is the timestep schedule from the scheduler and the second element is the number of inference steps. """ if timesteps is not None and sigmas is not None: raise ValueError("Only one of `timesteps` or `sigmas` can be passed. Please choose one to set custom values") if timesteps is not None: accepts_timesteps = "timesteps" in set(inspect.signature(scheduler.set_timesteps).parameters.keys()) if not accepts_timesteps: raise ValueError( f"The current scheduler class {scheduler.__class__}'s `set_timesteps` does not support custom" f" timestep schedules. Please check whether you are using the correct scheduler." ) scheduler.set_timesteps(timesteps=timesteps, device=device, **kwargs) timesteps = scheduler.timesteps num_inference_steps = len(timesteps) elif sigmas is not None: accept_sigmas = "sigmas" in set(inspect.signature(scheduler.set_timesteps).parameters.keys()) if not accept_sigmas: raise ValueError( f"The current scheduler class {scheduler.__class__}'s `set_timesteps` does not support custom" f" sigmas schedules. Please check whether you are using the correct scheduler." ) scheduler.set_timesteps(sigmas=sigmas, device=device, **kwargs) timesteps = scheduler.timesteps num_inference_steps = len(timesteps) else: scheduler.set_timesteps(num_inference_steps, device=device, **kwargs) timesteps = scheduler.timesteps return timesteps, num_inference_steps # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion_img2img.retrieve_latents def retrieve_latents( encoder_output: torch.Tensor, generator: torch.Generator | None = None, sample_mode: str = "sample" ): if hasattr(encoder_output, "latent_dist") and sample_mode == "sample": return encoder_output.latent_dist.sample(generator) elif hasattr(encoder_output, "latent_dist") and sample_mode == "argmax": return encoder_output.latent_dist.mode() elif hasattr(encoder_output, "latents"): return encoder_output.latents else: raise AttributeError("Could not access latents of provided encoder_output") class HunyuanImageRefinerPipeline(DiffusionPipeline): r""" The HunyuanImage pipeline for text-to-image generation. Args: transformer ([`HunyuanImageTransformer2DModel`]): Conditional Transformer (MMDiT) architecture to denoise the encoded image latents. scheduler ([`FlowMatchEulerDiscreteScheduler`]): A scheduler to be used in combination with `transformer` to denoise the encoded image latents. vae ([`AutoencoderKLHunyuanImageRefiner`]): Variational Auto-Encoder (VAE) Model to encode and decode images to and from latent representations. text_encoder ([`Qwen2.5-VL-7B-Instruct`]): [Qwen2.5-VL-7B-Instruct](https://huggingface.co/Qwen/Qwen2.5-VL-7B-Instruct), specifically the [Qwen2.5-VL-7B-Instruct](https://huggingface.co/Qwen/Qwen2.5-VL-7B-Instruct) variant. tokenizer (`Qwen2Tokenizer`): Tokenizer of class [Qwen2Tokenizer]. """ model_cpu_offload_seq = "text_encoder->transformer->vae" _callback_tensor_inputs = ["latents", "prompt_embeds"] _optional_components = ["guider"] def __init__( self, scheduler: FlowMatchEulerDiscreteScheduler, vae: AutoencoderKLHunyuanImageRefiner, text_encoder: Qwen2_5_VLForConditionalGeneration, tokenizer: Qwen2Tokenizer, transformer: HunyuanImageTransformer2DModel, guider: AdaptiveProjectedMixGuidance | None = None, ): super().__init__() self.register_modules( vae=vae, text_encoder=text_encoder, tokenizer=tokenizer, transformer=transformer, scheduler=scheduler, guider=guider, ) self.vae_scale_factor = self.vae.config.spatial_compression_ratio if getattr(self, "vae", None) else 16 self.image_processor = VaeImageProcessor(vae_scale_factor=self.vae_scale_factor) self.tokenizer_max_length = 256 self.prompt_template_encode = "<|start_header_id|>system<|end_header_id|>\n\nDescribe the image by detailing the color, shape, size, texture, quantity, text, spatial relationships of the objects and background:<|eot_id|><|start_header_id|>user<|end_header_id|>\n\n{}<|eot_id|>" self.prompt_template_encode_start_idx = 36 self.default_sample_size = 64 self.latent_channels = self.transformer.config.in_channels // 2 if getattr(self, "transformer", None) else 64 # Copied from diffusers.pipelines.hunyuan_image.pipeline_hunyuanimage.HunyuanImagePipeline._get_qwen_prompt_embeds def _get_qwen_prompt_embeds( self, tokenizer: Qwen2Tokenizer, text_encoder: Qwen2_5_VLForConditionalGeneration, prompt: str | list[str] = None, device: torch.device | None = None, dtype: torch.dtype | None = None, tokenizer_max_length: int = 1000, template: str = "<|im_start|>system\nDescribe the image by detailing the color, shape, size, texture, quantity, text, spatial relationships of the objects and background:<|im_end|>\n<|im_start|>user\n{}<|im_end|>", drop_idx: int = 34, hidden_state_skip_layer: int = 2, ): device = device or self._execution_device dtype = dtype or text_encoder.dtype prompt = [prompt] if isinstance(prompt, str) else prompt txt = [template.format(e) for e in prompt] txt_tokens = tokenizer( txt, max_length=tokenizer_max_length + drop_idx, padding="max_length", truncation=True, return_tensors="pt" ).to(device) encoder_hidden_states = text_encoder( input_ids=txt_tokens.input_ids, attention_mask=txt_tokens.attention_mask, output_hidden_states=True, ) prompt_embeds = encoder_hidden_states.hidden_states[-(hidden_state_skip_layer + 1)] prompt_embeds = prompt_embeds[:, drop_idx:] encoder_attention_mask = txt_tokens.attention_mask[:, drop_idx:] prompt_embeds = prompt_embeds.to(dtype=dtype, device=device) encoder_attention_mask = encoder_attention_mask.to(device=device) return prompt_embeds, encoder_attention_mask def encode_prompt( self, prompt: str | list[str] | None = None, device: torch.device | None = None, batch_size: int = 1, num_images_per_prompt: int = 1, prompt_embeds: torch.Tensor | None = None, prompt_embeds_mask: torch.Tensor | None = None, ): r""" Args: prompt (`str` or `list[str]`, *optional*): prompt to be encoded device: (`torch.device`): torch device batch_size (`int`): batch size of prompts, defaults to 1 num_images_per_prompt (`int`): number of images that should be generated per prompt prompt_embeds (`torch.Tensor`, *optional*): Pre-generated text embeddings. If not provided, text embeddings will be generated from `prompt` input argument. prompt_embeds_mask (`torch.Tensor`, *optional*): Pre-generated text mask. If not provided, text mask will be generated from `prompt` input argument. prompt_embeds_2 (`torch.Tensor`, *optional*): Pre-generated glyph text embeddings from ByT5. If not provided, will be generated from `prompt` input argument using self.tokenizer_2 and self.text_encoder_2. prompt_embeds_mask_2 (`torch.Tensor`, *optional*): Pre-generated glyph text mask from ByT5. If not provided, will be generated from `prompt` input argument using self.tokenizer_2 and self.text_encoder_2. """ device = device or self._execution_device if prompt is None: prompt = [""] * batch_size prompt = [prompt] if isinstance(prompt, str) else prompt if prompt_embeds is None: prompt_embeds, prompt_embeds_mask = self._get_qwen_prompt_embeds( tokenizer=self.tokenizer, text_encoder=self.text_encoder, prompt=prompt, device=device, tokenizer_max_length=self.tokenizer_max_length, template=self.prompt_template_encode, drop_idx=self.prompt_template_encode_start_idx, ) _, seq_len, _ = prompt_embeds.shape prompt_embeds = prompt_embeds.repeat(1, num_images_per_prompt, 1) prompt_embeds = prompt_embeds.view(batch_size * num_images_per_prompt, seq_len, -1) prompt_embeds_mask = prompt_embeds_mask.repeat(1, num_images_per_prompt, 1) prompt_embeds_mask = prompt_embeds_mask.view(batch_size * num_images_per_prompt, seq_len) return prompt_embeds, prompt_embeds_mask def check_inputs( self, prompt, height, width, negative_prompt=None, prompt_embeds=None, negative_prompt_embeds=None, prompt_embeds_mask=None, negative_prompt_embeds_mask=None, callback_on_step_end_tensor_inputs=None, ): if height % (self.vae_scale_factor * 2) != 0 or width % (self.vae_scale_factor * 2) != 0: logger.warning( f"`height` and `width` have to be divisible by {self.vae_scale_factor * 2} but are {height} and {width}. Dimensions will be resized accordingly" ) if callback_on_step_end_tensor_inputs is not None and not all( k in self._callback_tensor_inputs for k in callback_on_step_end_tensor_inputs ): raise ValueError( f"`callback_on_step_end_tensor_inputs` has to be in {self._callback_tensor_inputs}, but found {[k for k in callback_on_step_end_tensor_inputs if k not in self._callback_tensor_inputs]}" ) if prompt is not None and prompt_embeds is not None: raise ValueError( f"Cannot forward both `prompt`: {prompt} and `prompt_embeds`: {prompt_embeds}. Please make sure to" " only forward one of the two." ) elif prompt is None and prompt_embeds is None: raise ValueError( "Provide either `prompt` or `prompt_embeds`. Cannot leave both `prompt` and `prompt_embeds` undefined." ) elif prompt is not None and (not isinstance(prompt, str) and not isinstance(prompt, list)): raise ValueError(f"`prompt` has to be of type `str` or `list` but is {type(prompt)}") if negative_prompt is not None and negative_prompt_embeds is not None: raise ValueError( f"Cannot forward both `negative_prompt`: {negative_prompt} and `negative_prompt_embeds`:" f" {negative_prompt_embeds}. Please make sure to only forward one of the two." ) if prompt_embeds is not None and prompt_embeds_mask is None: raise ValueError( "If `prompt_embeds` are provided, `prompt_embeds_mask` also have to be passed. Make sure to generate `prompt_embeds_mask` from the same text encoder that was used to generate `prompt_embeds`." ) if negative_prompt_embeds is not None and negative_prompt_embeds_mask is None: raise ValueError( "If `negative_prompt_embeds` are provided, `negative_prompt_embeds_mask` also have to be passed. Make sure to generate `negative_prompt_embeds_mask` from the same text encoder that was used to generate `negative_prompt_embeds`." ) def prepare_latents( self, image_latents, batch_size, num_channels_latents, height, width, dtype, device, generator, latents=None, strength=0.25, ): height = int(height) // self.vae_scale_factor width = int(width) // self.vae_scale_factor shape = (batch_size, num_channels_latents, 1, height, width) if latents is None: latents = randn_tensor(shape, generator=generator, device=device, dtype=dtype) else: latents = latents.to(device=device, dtype=dtype) if batch_size > image_latents.shape[0] and batch_size % image_latents.shape[0] == 0: # expand init_latents for batch_size additional_image_per_prompt = batch_size // image_latents.shape[0] image_latents = torch.cat([image_latents] * additional_image_per_prompt, dim=0) elif batch_size > image_latents.shape[0] and batch_size % image_latents.shape[0] != 0: raise ValueError( f"Cannot duplicate `image` of batch size {image_latents.shape[0]} to {batch_size} text prompts." ) if isinstance(generator, list) and len(generator) != batch_size: raise ValueError( f"You have passed a list of generators of length {len(generator)}, but requested an effective batch" f" size of {batch_size}. Make sure the batch size matches the length of the generators." ) noise = randn_tensor(shape, generator=generator, device=device, dtype=dtype) cond_latents = strength * noise + (1 - strength) * image_latents return latents, cond_latents @staticmethod def _reorder_image_tokens(image_latents): image_latents = torch.cat((image_latents[:, :, :1], image_latents), dim=2) batch_size, num_latent_channels, num_latent_frames, latent_height, latent_width = image_latents.shape image_latents = image_latents.permute(0, 2, 1, 3, 4) image_latents = image_latents.reshape( batch_size, num_latent_frames // 2, num_latent_channels * 2, latent_height, latent_width ) image_latents = image_latents.permute(0, 2, 1, 3, 4).contiguous() return image_latents @staticmethod def _restore_image_tokens_order(latents): """Restore image tokens order by splitting channels and removing first frame slice.""" batch_size, num_latent_channels, num_latent_frames, latent_height, latent_width = latents.shape latents = latents.permute(0, 2, 1, 3, 4) # B, F, C, H, W latents = latents.reshape( batch_size, num_latent_frames * 2, num_latent_channels // 2, latent_height, latent_width ) # B, F*2, C//2, H, W latents = latents.permute(0, 2, 1, 3, 4) # B, C//2, F*2, H, W # Remove first frame slice latents = latents[:, :, 1:] return latents def _encode_vae_image(self, image: torch.Tensor, generator: torch.Generator): if isinstance(generator, list): image_latents = [ retrieve_latents(self.vae.encode(image[i : i + 1]), generator=generator[i], sample_mode="sample") for i in range(image.shape[0]) ] image_latents = torch.cat(image_latents, dim=0) else: image_latents = retrieve_latents(self.vae.encode(image), generator=generator, sample_mode="sample") image_latents = self._reorder_image_tokens(image_latents) image_latents = image_latents * self.vae.config.scaling_factor return image_latents @property def attention_kwargs(self): return self._attention_kwargs @property def num_timesteps(self): return self._num_timesteps @property def current_timestep(self): return self._current_timestep @property def interrupt(self): return self._interrupt @torch.no_grad() @replace_example_docstring(EXAMPLE_DOC_STRING) def __call__( self, prompt: str | list[str] = None, negative_prompt: str | list[str] = None, distilled_guidance_scale: float | None = 3.25, image: PipelineImageInput | None = None, height: int | None = None, width: int | None = None, num_inference_steps: int = 4, sigmas: list[float] | None = None, num_images_per_prompt: int = 1, generator: torch.Generator | list[torch.Generator] | None = None, latents: torch.Tensor | None = None, prompt_embeds: torch.Tensor | None = None, prompt_embeds_mask: torch.Tensor | None = None, negative_prompt_embeds: torch.Tensor | None = None, negative_prompt_embeds_mask: torch.Tensor | None = None, output_type: str | None = "pil", return_dict: bool = True, attention_kwargs: dict[str, Any] | None = None, callback_on_step_end: Callable[[int, int], None] | None = None, callback_on_step_end_tensor_inputs: list[str] = ["latents"], ): r""" Function invoked when calling the pipeline for generation. Args: prompt (`str` or `list[str]`, *optional*): The prompt or prompts to guide the image generation. If not defined, one has to pass `prompt_embeds`. instead. negative_prompt (`str` or `list[str]`, *optional*): The prompt or prompts not to guide the image generation. If not defined, will use an empty negative prompt. Ignored when not using guidance. distilled_guidance_scale (`float`, *optional*, defaults to None): A guidance scale value for guidance distilled models. Unlike the traditional classifier-free guidance where the guidance scale is applied during inference through noise prediction rescaling, guidance distilled models take the guidance scale directly as an input parameter during forward pass. Guidance is enabled by setting `distilled_guidance_scale > 1`. Higher guidance scale encourages to generate images that are closely linked to the text `prompt`, usually at the expense of lower image quality. For guidance distilled models, this parameter is required. For non-distilled models, this parameter will be ignored. num_images_per_prompt (`int`, *optional*, defaults to 1): height (`int`, *optional*, defaults to self.unet.config.sample_size * self.vae_scale_factor): The height in pixels of the generated image. This is set to 1024 by default for the best results. width (`int`, *optional*, defaults to self.unet.config.sample_size * self.vae_scale_factor): The width in pixels of the generated image. This is set to 1024 by default for the best results. num_inference_steps (`int`, *optional*, defaults to 50): The number of denoising steps. More denoising steps usually lead to a higher quality image at the expense of slower inference. sigmas (`list[float]`, *optional*): Custom sigmas to use for the denoising process with schedulers which support a `sigmas` argument in their `set_timesteps` method. If not defined, the default behavior when `num_inference_steps` is passed will be used. num_images_per_prompt (`int`, *optional*, defaults to 1): The number of images to generate per prompt. generator (`torch.Generator` or `list[torch.Generator]`, *optional*): One or a list of [torch generator(s)](https://pytorch.org/docs/stable/generated/torch.Generator.html) to make generation deterministic. latents (`torch.Tensor`, *optional*): Pre-generated noisy latents, sampled from a Gaussian distribution, to be used as inputs for image generation. Can be used to tweak the same generation with different prompts. If not provided, a latents tensor will be generated by sampling using the supplied random `generator`. prompt_embeds (`torch.Tensor`, *optional*): Pre-generated text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt weighting. If not provided, text embeddings will be generated from `prompt` input argument. negative_prompt_embeds (`torch.Tensor`, *optional*): Pre-generated negative text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt weighting. If not provided, negative_prompt_embeds will be generated from `negative_prompt` input argument. output_type (`str`, *optional*, defaults to `"pil"`): The output format of the generate image. Choose between [PIL](https://pillow.readthedocs.io/en/stable/): `PIL.Image.Image` or `np.array`. return_dict (`bool`, *optional*, defaults to `True`): Whether or not to return a [`~pipelines.qwenimage.QwenImagePipelineOutput`] instead of a plain tuple. attention_kwargs (`dict`, *optional*): A kwargs dictionary that if specified is passed along to the `AttentionProcessor` as defined under `self.processor` in [diffusers.models.attention_processor](https://github.com/huggingface/diffusers/blob/main/src/diffusers/models/attention_processor.py). callback_on_step_end (`Callable`, *optional*): A function that calls at the end of each denoising steps during the inference. The function is called with the following arguments: `callback_on_step_end(self: DiffusionPipeline, step: int, timestep: int, callback_kwargs: Dict)`. `callback_kwargs` will include a list of all tensors as specified by `callback_on_step_end_tensor_inputs`. callback_on_step_end_tensor_inputs (`List`, *optional*): The list of tensor inputs for the `callback_on_step_end` function. The tensors specified in the list will be passed as `callback_kwargs` argument. You will only be able to include variables listed in the `._callback_tensor_inputs` attribute of your pipeline class. Examples: Returns: [`~pipelines.hunyuan_image.HunyuanImagePipelineOutput`] or `tuple`: [`~pipelines.hunyuan_image.HunyuanImagePipelineOutput`] if `return_dict` is True, otherwise a `tuple`. When returning a tuple, the first element is a list with the generated images. """ height = height or self.default_sample_size * self.vae_scale_factor width = width or self.default_sample_size * self.vae_scale_factor # 1. Check inputs. Raise error if not correct self.check_inputs( prompt, height, width, negative_prompt=negative_prompt, prompt_embeds=prompt_embeds, negative_prompt_embeds=negative_prompt_embeds, prompt_embeds_mask=prompt_embeds_mask, negative_prompt_embeds_mask=negative_prompt_embeds_mask, callback_on_step_end_tensor_inputs=callback_on_step_end_tensor_inputs, ) self._attention_kwargs = attention_kwargs self._current_timestep = None self._interrupt = False # 2. Define call parameters if prompt is not None and isinstance(prompt, str): batch_size = 1 elif prompt is not None and isinstance(prompt, list): batch_size = len(prompt) else: batch_size = prompt_embeds.shape[0] device = self._execution_device # 3. process image if image is not None and isinstance(image, torch.Tensor) and image.shape[1] == self.latent_channels: image_latents = image else: image = self.image_processor.preprocess(image, height, width) image = image.unsqueeze(2).to(device, dtype=self.vae.dtype) image_latents = self._encode_vae_image(image=image, generator=generator) # 3.prepare prompt embeds if self.guider is not None: guider = self.guider else: # distilled model does not use guidance method, use default guider with enabled=False guider = AdaptiveProjectedMixGuidance(enabled=False) requires_unconditional_embeds = guider._enabled and guider.num_conditions > 1 prompt_embeds, prompt_embeds_mask = self.encode_prompt( prompt=prompt, prompt_embeds=prompt_embeds, prompt_embeds_mask=prompt_embeds_mask, device=device, batch_size=batch_size, num_images_per_prompt=num_images_per_prompt, ) prompt_embeds = prompt_embeds.to(self.transformer.dtype) if requires_unconditional_embeds: ( negative_prompt_embeds, negative_prompt_embeds_mask, ) = self.encode_prompt( prompt=negative_prompt, prompt_embeds=negative_prompt_embeds, prompt_embeds_mask=negative_prompt_embeds_mask, device=device, batch_size=batch_size, num_images_per_prompt=num_images_per_prompt, ) negative_prompt_embeds = negative_prompt_embeds.to(self.transformer.dtype) # 4. Prepare latent variables latents, cond_latents = self.prepare_latents( image_latents=image_latents, batch_size=batch_size * num_images_per_prompt, num_channels_latents=self.latent_channels, height=height, width=width, dtype=prompt_embeds.dtype, device=device, generator=generator, latents=latents, ) # 5. Prepare timesteps sigmas = np.linspace(1.0, 0.0, num_inference_steps + 1)[:-1] if sigmas is None else sigmas timesteps, num_inference_steps = retrieve_timesteps(self.scheduler, num_inference_steps, device, sigmas=sigmas) num_warmup_steps = max(len(timesteps) - num_inference_steps * self.scheduler.order, 0) self._num_timesteps = len(timesteps) # handle guidance (this pipeline only supports guidance-distilled models) if distilled_guidance_scale is None: raise ValueError("`distilled_guidance_scale` is required for guidance-distilled model.") guidance = ( torch.tensor([distilled_guidance_scale] * latents.shape[0], dtype=self.transformer.dtype, device=device) * 1000.0 ) if self.attention_kwargs is None: self._attention_kwargs = {} # 6. Denoising loop self.scheduler.set_begin_index(0) with self.progress_bar(total=num_inference_steps) as progress_bar: for i, t in enumerate(timesteps): if self.interrupt: continue self._current_timestep = t # broadcast to batch dimension in a way that's compatible with ONNX/Core ML latent_model_input = torch.cat([latents, cond_latents], dim=1).to(self.transformer.dtype) timestep = t.expand(latents.shape[0]).to(latents.dtype) # Step 1: Collect model inputs needed for the guidance method # conditional inputs should always be first element in the tuple guider_inputs = { "encoder_hidden_states": (prompt_embeds, negative_prompt_embeds), "encoder_attention_mask": (prompt_embeds_mask, negative_prompt_embeds_mask), } # Step 2: Update guider's internal state for this denoising step guider.set_state(step=i, num_inference_steps=num_inference_steps, timestep=t) # Step 3: Prepare batched model inputs based on the guidance method # The guider splits model inputs into separate batches for conditional/unconditional predictions. # For CFG with guider_inputs = {"encoder_hidden_states": (prompt_embeds, negative_prompt_embeds)}: # you will get a guider_state with two batches: # guider_state = [ # {"encoder_hidden_states": prompt_embeds, "__guidance_identifier__": "pred_cond"}, # conditional batch # {"encoder_hidden_states": negative_prompt_embeds, "__guidance_identifier__": "pred_uncond"}, # unconditional batch # ] # Other guidance methods may return 1 batch (no guidance) or 3+ batches (e.g., PAG, APG). guider_state = guider.prepare_inputs(guider_inputs) # Step 4: Run the denoiser for each batch # Each batch in guider_state represents a different conditioning (conditional, unconditional, etc.). # We run the model once per batch and store the noise prediction in guider_state_batch.noise_pred. for guider_state_batch in guider_state: guider.prepare_models(self.transformer) # Extract conditioning kwargs for this batch (e.g., encoder_hidden_states) cond_kwargs = { input_name: getattr(guider_state_batch, input_name) for input_name in guider_inputs.keys() } # e.g. "pred_cond"/"pred_uncond" context_name = getattr(guider_state_batch, guider._identifier_key) with self.transformer.cache_context(context_name): # Run denoiser and store noise prediction in this batch guider_state_batch.noise_pred = self.transformer( hidden_states=latent_model_input, timestep=timestep, guidance=guidance, attention_kwargs=self.attention_kwargs, return_dict=False, **cond_kwargs, )[0] # Cleanup model (e.g., remove hooks) guider.cleanup_models(self.transformer) # Step 5: Combine predictions using the guidance method # The guider takes all noise predictions from guider_state and combines them according to the guidance algorithm. # Continuing the CFG example, the guider receives: # guider_state = [ # {"encoder_hidden_states": prompt_embeds, "noise_pred": noise_pred_cond, "__guidance_identifier__": "pred_cond"}, # batch 0 # {"encoder_hidden_states": negative_prompt_embeds, "noise_pred": noise_pred_uncond, "__guidance_identifier__": "pred_uncond"}, # batch 1 # ] # And extracts predictions using the __guidance_identifier__: # pred_cond = guider_state[0]["noise_pred"] # extracts noise_pred_cond # pred_uncond = guider_state[1]["noise_pred"] # extracts noise_pred_uncond # Then applies CFG formula: # noise_pred = pred_uncond + guidance_scale * (pred_cond - pred_uncond) # Returns GuiderOutput(pred=noise_pred, pred_cond=pred_cond, pred_uncond=pred_uncond) noise_pred = guider(guider_state)[0] # compute the previous noisy sample x_t -> x_t-1 latents_dtype = latents.dtype latents = self.scheduler.step(noise_pred, t, latents, return_dict=False)[0] if latents.dtype != latents_dtype: if torch.backends.mps.is_available(): # some platforms (eg. apple mps) misbehave due to a pytorch bug: https://github.com/pytorch/pytorch/pull/99272 latents = latents.to(latents_dtype) if callback_on_step_end is not None: callback_kwargs = {} for k in callback_on_step_end_tensor_inputs: callback_kwargs[k] = locals()[k] callback_outputs = callback_on_step_end(self, i, t, callback_kwargs) latents = callback_outputs.pop("latents", latents) prompt_embeds = callback_outputs.pop("prompt_embeds", prompt_embeds) # call the callback, if provided if i == len(timesteps) - 1 or ((i + 1) > num_warmup_steps and (i + 1) % self.scheduler.order == 0): progress_bar.update() if XLA_AVAILABLE: xm.mark_step() self._current_timestep = None if output_type == "latent": image = latents else: latents = latents.to(self.vae.dtype) / self.vae.config.scaling_factor latents = self._restore_image_tokens_order(latents) image = self.vae.decode(latents, return_dict=False)[0] image = self.image_processor.postprocess(image.squeeze(2), output_type=output_type) # Offload all models self.maybe_free_model_hooks() if not return_dict: return (image,) return HunyuanImagePipelineOutput(images=image)
{ "repo_id": "huggingface/diffusers", "file_path": "src/diffusers/pipelines/hunyuan_image/pipeline_hunyuanimage_refiner.py", "license": "Apache License 2.0", "lines": 649, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
huggingface/diffusers:src/diffusers/pipelines/hunyuan_image/pipeline_output.py
from dataclasses import dataclass import numpy as np import PIL.Image from ...utils import BaseOutput @dataclass class HunyuanImagePipelineOutput(BaseOutput): """ Output class for HunyuanImage pipelines. Args: images (`list[PIL.Image.Image]` or `np.ndarray`) List of denoised PIL images of length `batch_size` or numpy array of shape `(batch_size, height, width, num_channels)`. PIL images or numpy array present the denoised images of the diffusion pipeline. """ images: list[PIL.Image.Image, np.ndarray]
{ "repo_id": "huggingface/diffusers", "file_path": "src/diffusers/pipelines/hunyuan_image/pipeline_output.py", "license": "Apache License 2.0", "lines": 14, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
huggingface/diffusers:tests/pipelines/hunyuan_image_21/test_hunyuanimage.py
# Copyright 2025 The HuggingFace Team. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest import numpy as np import torch from transformers import ( ByT5Tokenizer, Qwen2_5_VLConfig, Qwen2_5_VLForConditionalGeneration, Qwen2Tokenizer, T5Config, T5EncoderModel, ) from diffusers import ( AdaptiveProjectedMixGuidance, AutoencoderKLHunyuanImage, FlowMatchEulerDiscreteScheduler, HunyuanImagePipeline, HunyuanImageTransformer2DModel, ) from ...testing_utils import enable_full_determinism from ..test_pipelines_common import FirstBlockCacheTesterMixin, PipelineTesterMixin, to_np enable_full_determinism() class HunyuanImagePipelineFastTests( PipelineTesterMixin, FirstBlockCacheTesterMixin, unittest.TestCase, ): pipeline_class = HunyuanImagePipeline params = frozenset(["prompt", "height", "width"]) batch_params = frozenset(["prompt", "negative_prompt"]) required_optional_params = frozenset( [ "num_inference_steps", "generator", "latents", "return_dict", "callback_on_step_end", "callback_on_step_end_tensor_inputs", ] ) test_xformers_attention = False test_layerwise_casting = True test_group_offloading = True test_attention_slicing = False supports_dduf = False def get_dummy_components(self, num_layers: int = 1, num_single_layers: int = 1, guidance_embeds: bool = False): torch.manual_seed(0) transformer = HunyuanImageTransformer2DModel( in_channels=4, out_channels=4, num_attention_heads=4, attention_head_dim=8, num_layers=num_layers, num_single_layers=num_single_layers, num_refiner_layers=1, patch_size=(1, 1), guidance_embeds=guidance_embeds, text_embed_dim=32, text_embed_2_dim=32, rope_axes_dim=(4, 4), ) torch.manual_seed(0) vae = AutoencoderKLHunyuanImage( in_channels=3, out_channels=3, latent_channels=4, block_out_channels=(32, 64, 64, 64), layers_per_block=1, scaling_factor=0.476986, spatial_compression_ratio=8, sample_size=128, ) torch.manual_seed(0) scheduler = FlowMatchEulerDiscreteScheduler(shift=7.0) if not guidance_embeds: torch.manual_seed(0) guider = AdaptiveProjectedMixGuidance(adaptive_projected_guidance_start_step=2) ocr_guider = AdaptiveProjectedMixGuidance(adaptive_projected_guidance_start_step=3) else: guider = None ocr_guider = None torch.manual_seed(0) config = Qwen2_5_VLConfig( text_config={ "hidden_size": 32, "intermediate_size": 32, "num_hidden_layers": 2, "num_attention_heads": 2, "num_key_value_heads": 2, "rope_scaling": { "mrope_section": [2, 2, 4], "rope_type": "default", "type": "default", }, "rope_theta": 1000000.0, }, vision_config={ "depth": 2, "hidden_size": 32, "intermediate_size": 32, "num_heads": 2, "out_hidden_size": 32, }, hidden_size=32, vocab_size=152064, vision_end_token_id=151653, vision_start_token_id=151652, vision_token_id=151654, ) text_encoder = Qwen2_5_VLForConditionalGeneration(config) tokenizer = Qwen2Tokenizer.from_pretrained("hf-internal-testing/tiny-random-Qwen2VLForConditionalGeneration") torch.manual_seed(0) t5_config = T5Config( d_model=32, d_kv=4, d_ff=16, num_layers=2, num_heads=2, relative_attention_num_buckets=8, relative_attention_max_distance=32, vocab_size=256, feed_forward_proj="gated-gelu", dense_act_fn="gelu_new", is_encoder_decoder=False, use_cache=False, tie_word_embeddings=False, ) text_encoder_2 = T5EncoderModel(t5_config) tokenizer_2 = ByT5Tokenizer() components = { "transformer": transformer, "vae": vae, "scheduler": scheduler, "text_encoder": text_encoder, "text_encoder_2": text_encoder_2, "tokenizer": tokenizer, "tokenizer_2": tokenizer_2, "guider": guider, "ocr_guider": ocr_guider, } return components def get_dummy_inputs(self, device, seed=0): if str(device).startswith("mps"): generator = torch.manual_seed(seed) else: generator = torch.Generator(device=device).manual_seed(seed) inputs = { "prompt": "A painting of a squirrel eating a burger", "generator": generator, "num_inference_steps": 5, "height": 16, "width": 16, "output_type": "pt", } return inputs def test_inference(self): device = "cpu" components = self.get_dummy_components() pipe = self.pipeline_class(**components) pipe.to(device) pipe.set_progress_bar_config(disable=None) inputs = self.get_dummy_inputs(device) image = pipe(**inputs).images generated_image = image[0] self.assertEqual(generated_image.shape, (3, 16, 16)) expected_slice_np = np.array( [0.6252659, 0.51482046, 0.60799813, 0.59267783, 0.488082, 0.5857634, 0.523781, 0.58028054, 0.5674121] ) output_slice = generated_image[0, -3:, -3:].flatten().cpu().numpy() self.assertTrue( np.abs(output_slice - expected_slice_np).max() < 1e-3, f"output_slice: {output_slice}, expected_slice_np: {expected_slice_np}", ) def test_inference_guider(self): device = "cpu" components = self.get_dummy_components() pipe = self.pipeline_class(**components) pipe.to(device) pipe.set_progress_bar_config(disable=None) pipe.guider = pipe.guider.new(guidance_scale=1000) pipe.ocr_guider = pipe.ocr_guider.new(guidance_scale=1000) inputs = self.get_dummy_inputs(device) image = pipe(**inputs).images generated_image = image[0] self.assertEqual(generated_image.shape, (3, 16, 16)) expected_slice_np = np.array( [0.6068114, 0.48716035, 0.5984431, 0.60241306, 0.48849544, 0.5624479, 0.53696984, 0.58964247, 0.54248774] ) output_slice = generated_image[0, -3:, -3:].flatten().cpu().numpy() self.assertTrue( np.abs(output_slice - expected_slice_np).max() < 1e-3, f"output_slice: {output_slice}, expected_slice_np: {expected_slice_np}", ) def test_inference_with_distilled_guidance(self): device = "cpu" components = self.get_dummy_components(guidance_embeds=True) pipe = self.pipeline_class(**components) pipe.to(device) pipe.set_progress_bar_config(disable=None) inputs = self.get_dummy_inputs(device) inputs["distilled_guidance_scale"] = 3.5 image = pipe(**inputs).images generated_image = image[0] self.assertEqual(generated_image.shape, (3, 16, 16)) expected_slice_np = np.array( [0.63667065, 0.5187377, 0.66757566, 0.6320319, 0.4913387, 0.54813194, 0.5335031, 0.5736143, 0.5461346] ) output_slice = generated_image[0, -3:, -3:].flatten().cpu().numpy() self.assertTrue( np.abs(output_slice - expected_slice_np).max() < 1e-3, f"output_slice: {output_slice}, expected_slice_np: {expected_slice_np}", ) def test_vae_tiling(self, expected_diff_max: float = 0.2): generator_device = "cpu" components = self.get_dummy_components() pipe = self.pipeline_class(**components) pipe.to("cpu") pipe.set_progress_bar_config(disable=None) # Without tiling inputs = self.get_dummy_inputs(generator_device) inputs["height"] = inputs["width"] = 128 output_without_tiling = pipe(**inputs)[0] # With tiling pipe.vae.enable_tiling(tile_sample_min_size=96) inputs = self.get_dummy_inputs(generator_device) inputs["height"] = inputs["width"] = 128 output_with_tiling = pipe(**inputs)[0] self.assertLess( (to_np(output_without_tiling) - to_np(output_with_tiling)).max(), expected_diff_max, "VAE tiling should not affect the inference results", ) @unittest.skip("TODO: Test not supported for now because needs to be adjusted to work with guiders.") def test_encode_prompt_works_in_isolation(self): pass
{ "repo_id": "huggingface/diffusers", "file_path": "tests/pipelines/hunyuan_image_21/test_hunyuanimage.py", "license": "Apache License 2.0", "lines": 247, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
test
huggingface/diffusers:tests/others/test_attention_backends.py
""" This test suite exists for the maintainers currently. It's not run in our CI at the moment. Once attention backends become more mature, we can consider including this in our CI. To run this test suite: ```bash export RUN_ATTENTION_BACKEND_TESTS=yes pytest tests/others/test_attention_backends.py ``` Tests were conducted on an H100 with PyTorch 2.8.0 (CUDA 12.9). Slices for the compilation tests in "native" variants were obtained with a torch nightly version (2.10.0.dev20250924+cu128). Tests for aiter backend were conducted and slices for the aiter backend tests collected on a MI355X with torch 2025-09-25 nightly version (ad2f7315ca66b42497047bb7951f696b50f1e81b) and aiter 0.1.5.post4.dev20+ga25e55e79. """ import os import pytest import torch pytestmark = pytest.mark.skipif( os.getenv("RUN_ATTENTION_BACKEND_TESTS", "false") == "false", reason="Feature not mature enough." ) from diffusers import FluxPipeline # noqa: E402 from diffusers.utils import is_torch_version # noqa: E402 # fmt: off FORWARD_CASES = [ ( "flash_hub", torch.tensor([0.0820, 0.0859, 0.0918, 0.1016, 0.0957, 0.0996, 0.0996, 0.1016, 0.2188, 0.2266, 0.2363, 0.2500, 0.2539, 0.2461, 0.2422, 0.2695], dtype=torch.bfloat16) ), ( "_flash_3_hub", torch.tensor([0.0820, 0.0859, 0.0938, 0.1016, 0.0977, 0.0996, 0.1016, 0.1016, 0.2188, 0.2246, 0.2344, 0.2480, 0.2539, 0.2480, 0.2441, 0.2715], dtype=torch.bfloat16), ), ( "native", torch.tensor([0.0820, 0.0859, 0.0938, 0.1016, 0.0957, 0.0996, 0.0996, 0.1016, 0.2188, 0.2266, 0.2363, 0.2500, 0.2539, 0.2480, 0.2461, 0.2734], dtype=torch.bfloat16) ), ( "_native_cudnn", torch.tensor([0.0781, 0.0840, 0.0879, 0.0957, 0.0898, 0.0957, 0.0957, 0.0977, 0.2168, 0.2246, 0.2324, 0.2500, 0.2539, 0.2480, 0.2441, 0.2695], dtype=torch.bfloat16), ), ( "aiter", torch.tensor([0.0781, 0.0820, 0.0879, 0.0957, 0.0898, 0.0938, 0.0957, 0.0957, 0.2285, 0.2363, 0.2461, 0.2637, 0.2695, 0.2617, 0.2617, 0.2891], dtype=torch.bfloat16), ) ] COMPILE_CASES = [ ( "flash_hub", torch.tensor([0.0410, 0.0410, 0.0449, 0.0508, 0.0488, 0.0586, 0.0605, 0.0586, 0.2324, 0.2422, 0.2539, 0.2734, 0.2832, 0.2812, 0.2773, 0.3047], dtype=torch.bfloat16), True ), ( "_flash_3_hub", torch.tensor([0.0410, 0.0410, 0.0449, 0.0508, 0.0508, 0.0605, 0.0625, 0.0605, 0.2344, 0.2461, 0.2578, 0.2734, 0.2852, 0.2812, 0.2773, 0.3047], dtype=torch.bfloat16), True, ), ( "native", torch.tensor([0.0410, 0.0410, 0.0449, 0.0508, 0.0508, 0.0605, 0.0605, 0.0605, 0.2344, 0.2461, 0.2578, 0.2773, 0.2871, 0.2832, 0.2773, 0.3066], dtype=torch.bfloat16), True, ), ( "_native_cudnn", torch.tensor([0.0410, 0.0410, 0.0430, 0.0508, 0.0488, 0.0586, 0.0605, 0.0586, 0.2344, 0.2461, 0.2578, 0.2773, 0.2871, 0.2832, 0.2793, 0.3086], dtype=torch.bfloat16), True, ), ( "aiter", torch.tensor([0.0391, 0.0391, 0.0430, 0.0488, 0.0469, 0.0566, 0.0586, 0.0566, 0.2402, 0.2539, 0.2637, 0.2812, 0.2930, 0.2910, 0.2891, 0.3164], dtype=torch.bfloat16), True, ) ] # fmt: on INFER_KW = { "prompt": "dance doggo dance", "height": 256, "width": 256, "num_inference_steps": 2, "guidance_scale": 3.5, "max_sequence_length": 128, "output_type": "pt", } def _backend_is_probably_supported(pipe, name: str): try: pipe.transformer.set_attention_backend(name) return pipe, True except Exception: return False def _check_if_slices_match(output, expected_slice): img = output.images.detach().cpu() generated_slice = img.flatten() generated_slice = torch.cat([generated_slice[:8], generated_slice[-8:]]) assert torch.allclose(generated_slice, expected_slice, atol=1e-4) @pytest.fixture(scope="session") def device(): if not torch.cuda.is_available(): pytest.skip("CUDA is required for these tests.") return torch.device("cuda:0") @pytest.fixture(scope="session") def pipe(device): repo_id = "black-forest-labs/FLUX.1-dev" pipe = FluxPipeline.from_pretrained(repo_id, torch_dtype=torch.bfloat16).to(device) pipe.set_progress_bar_config(disable=True) return pipe @pytest.mark.parametrize("backend_name,expected_slice", FORWARD_CASES, ids=[c[0] for c in FORWARD_CASES]) def test_forward(pipe, backend_name, expected_slice): out = _backend_is_probably_supported(pipe, backend_name) if isinstance(out, bool): pytest.xfail(f"Backend '{backend_name}' not supported in this environment.") modified_pipe = out[0] out = modified_pipe(**INFER_KW, generator=torch.manual_seed(0)) _check_if_slices_match(out, expected_slice) @pytest.mark.parametrize( "backend_name,expected_slice,error_on_recompile", COMPILE_CASES, ids=[c[0] for c in COMPILE_CASES], ) def test_forward_with_compile(pipe, backend_name, expected_slice, error_on_recompile): if "native" in backend_name and error_on_recompile and not is_torch_version(">=", "2.9.0"): pytest.xfail(f"Test with {backend_name=} is compatible with a higher version of torch.") out = _backend_is_probably_supported(pipe, backend_name) if isinstance(out, bool): pytest.xfail(f"Backend '{backend_name}' not supported in this environment.") modified_pipe = out[0] modified_pipe.transformer.compile(fullgraph=True) torch.compiler.reset() with ( torch._inductor.utils.fresh_inductor_cache(), torch._dynamo.config.patch(error_on_recompile=error_on_recompile), ): out = modified_pipe(**INFER_KW, generator=torch.manual_seed(0)) _check_if_slices_match(out, expected_slice)
{ "repo_id": "huggingface/diffusers", "file_path": "tests/others/test_attention_backends.py", "license": "Apache License 2.0", "lines": 132, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
test
huggingface/diffusers:src/diffusers/models/transformers/transformer_kandinsky.py
# Copyright 2025 The Kandinsky Team and The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import inspect import math from typing import Any import torch import torch.nn as nn import torch.nn.functional as F from torch import Tensor from ...configuration_utils import ConfigMixin, register_to_config from ...loaders import FromOriginalModelMixin, PeftAdapterMixin from ...utils import ( logging, ) from ..attention import AttentionMixin, AttentionModuleMixin from ..attention_dispatch import _CAN_USE_FLEX_ATTN, dispatch_attention_fn from ..cache_utils import CacheMixin from ..modeling_outputs import Transformer2DModelOutput from ..modeling_utils import ModelMixin logger = logging.get_logger(__name__) def get_freqs(dim, max_period=10000.0): freqs = torch.exp(-math.log(max_period) * torch.arange(start=0, end=dim, dtype=torch.float32) / dim) return freqs def fractal_flatten(x, rope, shape, block_mask=False): if block_mask: pixel_size = 8 x = local_patching(x, shape, (1, pixel_size, pixel_size), dim=1) rope = local_patching(rope, shape, (1, pixel_size, pixel_size), dim=1) x = x.flatten(1, 2) rope = rope.flatten(1, 2) else: x = x.flatten(1, 3) rope = rope.flatten(1, 3) return x, rope def fractal_unflatten(x, shape, block_mask=False): if block_mask: pixel_size = 8 x = x.reshape(x.shape[0], -1, pixel_size**2, *x.shape[2:]) x = local_merge(x, shape, (1, pixel_size, pixel_size), dim=1) else: x = x.reshape(*shape, *x.shape[2:]) return x def local_patching(x, shape, group_size, dim=0): batch_size, duration, height, width = shape g1, g2, g3 = group_size x = x.reshape( *x.shape[:dim], duration // g1, g1, height // g2, g2, width // g3, g3, *x.shape[dim + 3 :], ) x = x.permute( *range(len(x.shape[:dim])), dim, dim + 2, dim + 4, dim + 1, dim + 3, dim + 5, *range(dim + 6, len(x.shape)), ) x = x.flatten(dim, dim + 2).flatten(dim + 1, dim + 3) return x def local_merge(x, shape, group_size, dim=0): batch_size, duration, height, width = shape g1, g2, g3 = group_size x = x.reshape( *x.shape[:dim], duration // g1, height // g2, width // g3, g1, g2, g3, *x.shape[dim + 2 :], ) x = x.permute( *range(len(x.shape[:dim])), dim, dim + 3, dim + 1, dim + 4, dim + 2, dim + 5, *range(dim + 6, len(x.shape)), ) x = x.flatten(dim, dim + 1).flatten(dim + 1, dim + 2).flatten(dim + 2, dim + 3) return x def nablaT_v2( q: Tensor, k: Tensor, sta: Tensor, thr: float = 0.9, ): if _CAN_USE_FLEX_ATTN: from torch.nn.attention.flex_attention import BlockMask else: raise ValueError("Nabla attention is not supported with this version of PyTorch") q = q.transpose(1, 2).contiguous() k = k.transpose(1, 2).contiguous() # Map estimation B, h, S, D = q.shape s1 = S // 64 qa = q.reshape(B, h, s1, 64, D).mean(-2) ka = k.reshape(B, h, s1, 64, D).mean(-2).transpose(-2, -1) map = qa @ ka map = torch.softmax(map / math.sqrt(D), dim=-1) # Map binarization vals, inds = map.sort(-1) cvals = vals.cumsum_(-1) mask = (cvals >= 1 - thr).int() mask = mask.gather(-1, inds.argsort(-1)) mask = torch.logical_or(mask, sta) # BlockMask creation kv_nb = mask.sum(-1).to(torch.int32) kv_inds = mask.argsort(dim=-1, descending=True).to(torch.int32) return BlockMask.from_kv_blocks(torch.zeros_like(kv_nb), kv_inds, kv_nb, kv_inds, BLOCK_SIZE=64, mask_mod=None) class Kandinsky5TimeEmbeddings(nn.Module): def __init__(self, model_dim, time_dim, max_period=10000.0): super().__init__() assert model_dim % 2 == 0 self.model_dim = model_dim self.max_period = max_period self.freqs = get_freqs(self.model_dim // 2, self.max_period) self.in_layer = nn.Linear(model_dim, time_dim, bias=True) self.activation = nn.SiLU() self.out_layer = nn.Linear(time_dim, time_dim, bias=True) def forward(self, time): args = torch.outer(time.to(torch.float32), self.freqs.to(device=time.device)) time_embed = torch.cat([torch.cos(args), torch.sin(args)], dim=-1) time_embed = self.out_layer(self.activation(self.in_layer(time_embed))) return time_embed class Kandinsky5TextEmbeddings(nn.Module): def __init__(self, text_dim, model_dim): super().__init__() self.in_layer = nn.Linear(text_dim, model_dim, bias=True) self.norm = nn.LayerNorm(model_dim, elementwise_affine=True) def forward(self, text_embed): text_embed = self.in_layer(text_embed) return self.norm(text_embed).type_as(text_embed) class Kandinsky5VisualEmbeddings(nn.Module): def __init__(self, visual_dim, model_dim, patch_size): super().__init__() self.patch_size = patch_size self.in_layer = nn.Linear(math.prod(patch_size) * visual_dim, model_dim) def forward(self, x): batch_size, duration, height, width, dim = x.shape x = ( x.view( batch_size, duration // self.patch_size[0], self.patch_size[0], height // self.patch_size[1], self.patch_size[1], width // self.patch_size[2], self.patch_size[2], dim, ) .permute(0, 1, 3, 5, 2, 4, 6, 7) .flatten(4, 7) ) return self.in_layer(x) class Kandinsky5RoPE1D(nn.Module): def __init__(self, dim, max_pos=1024, max_period=10000.0): super().__init__() self.max_period = max_period self.dim = dim self.max_pos = max_pos freq = get_freqs(dim // 2, max_period) pos = torch.arange(max_pos, dtype=freq.dtype) self.register_buffer("args", torch.outer(pos, freq), persistent=False) def forward(self, pos): args = self.args[pos] cosine = torch.cos(args) sine = torch.sin(args) rope = torch.stack([cosine, -sine, sine, cosine], dim=-1) rope = rope.view(*rope.shape[:-1], 2, 2) return rope.unsqueeze(-4) class Kandinsky5RoPE3D(nn.Module): def __init__(self, axes_dims, max_pos=(128, 128, 128), max_period=10000.0): super().__init__() self.axes_dims = axes_dims self.max_pos = max_pos self.max_period = max_period for i, (axes_dim, ax_max_pos) in enumerate(zip(axes_dims, max_pos)): freq = get_freqs(axes_dim // 2, max_period) pos = torch.arange(ax_max_pos, dtype=freq.dtype) self.register_buffer(f"args_{i}", torch.outer(pos, freq), persistent=False) def forward(self, shape, pos, scale_factor=(1.0, 1.0, 1.0)): batch_size, duration, height, width = shape args_t = self.args_0[pos[0]] / scale_factor[0] args_h = self.args_1[pos[1]] / scale_factor[1] args_w = self.args_2[pos[2]] / scale_factor[2] args = torch.cat( [ args_t.view(1, duration, 1, 1, -1).repeat(batch_size, 1, height, width, 1), args_h.view(1, 1, height, 1, -1).repeat(batch_size, duration, 1, width, 1), args_w.view(1, 1, 1, width, -1).repeat(batch_size, duration, height, 1, 1), ], dim=-1, ) cosine = torch.cos(args) sine = torch.sin(args) rope = torch.stack([cosine, -sine, sine, cosine], dim=-1) rope = rope.view(*rope.shape[:-1], 2, 2) return rope.unsqueeze(-4) class Kandinsky5Modulation(nn.Module): def __init__(self, time_dim, model_dim, num_params): super().__init__() self.activation = nn.SiLU() self.out_layer = nn.Linear(time_dim, num_params * model_dim) self.out_layer.weight.data.zero_() self.out_layer.bias.data.zero_() def forward(self, x): return self.out_layer(self.activation(x)) class Kandinsky5AttnProcessor: _attention_backend = None _parallel_config = None def __init__(self): if not hasattr(F, "scaled_dot_product_attention"): raise ImportError(f"{self.__class__.__name__} requires PyTorch 2.0. Please upgrade your pytorch version.") def __call__(self, attn, hidden_states, encoder_hidden_states=None, rotary_emb=None, sparse_params=None): # query, key, value = self.get_qkv(x) query = attn.to_query(hidden_states) if encoder_hidden_states is not None: key = attn.to_key(encoder_hidden_states) value = attn.to_value(encoder_hidden_states) shape, cond_shape = query.shape[:-1], key.shape[:-1] query = query.reshape(*shape, attn.num_heads, -1) key = key.reshape(*cond_shape, attn.num_heads, -1) value = value.reshape(*cond_shape, attn.num_heads, -1) else: key = attn.to_key(hidden_states) value = attn.to_value(hidden_states) shape = query.shape[:-1] query = query.reshape(*shape, attn.num_heads, -1) key = key.reshape(*shape, attn.num_heads, -1) value = value.reshape(*shape, attn.num_heads, -1) # query, key = self.norm_qk(query, key) query = attn.query_norm(query.float()).type_as(query) key = attn.key_norm(key.float()).type_as(key) def apply_rotary(x, rope): x_ = x.reshape(*x.shape[:-1], -1, 1, 2).to(torch.float32) x_out = (rope * x_).sum(dim=-1) return x_out.reshape(*x.shape).to(torch.bfloat16) if rotary_emb is not None: query = apply_rotary(query, rotary_emb).type_as(query) key = apply_rotary(key, rotary_emb).type_as(key) if sparse_params is not None: attn_mask = nablaT_v2( query, key, sparse_params["sta_mask"], thr=sparse_params["P"], ) else: attn_mask = None hidden_states = dispatch_attention_fn( query, key, value, attn_mask=attn_mask, backend=self._attention_backend, parallel_config=self._parallel_config, ) hidden_states = hidden_states.flatten(-2, -1) attn_out = attn.out_layer(hidden_states) return attn_out class Kandinsky5Attention(nn.Module, AttentionModuleMixin): _default_processor_cls = Kandinsky5AttnProcessor _available_processors = [ Kandinsky5AttnProcessor, ] def __init__(self, num_channels, head_dim, processor=None): super().__init__() assert num_channels % head_dim == 0 self.num_heads = num_channels // head_dim self.to_query = nn.Linear(num_channels, num_channels, bias=True) self.to_key = nn.Linear(num_channels, num_channels, bias=True) self.to_value = nn.Linear(num_channels, num_channels, bias=True) self.query_norm = nn.RMSNorm(head_dim) self.key_norm = nn.RMSNorm(head_dim) self.out_layer = nn.Linear(num_channels, num_channels, bias=True) if processor is None: processor = self._default_processor_cls() self.set_processor(processor) def forward( self, hidden_states: torch.Tensor, encoder_hidden_states: torch.Tensor | None = None, sparse_params: torch.Tensor | None = None, rotary_emb: tuple[torch.Tensor, torch.Tensor] | None = None, **kwargs, ) -> torch.Tensor: attn_parameters = set(inspect.signature(self.processor.__call__).parameters.keys()) quiet_attn_parameters = {} unused_kwargs = [k for k, _ in kwargs.items() if k not in attn_parameters and k not in quiet_attn_parameters] if len(unused_kwargs) > 0: logger.warning( f"attention_processor_kwargs {unused_kwargs} are not expected by {self.processor.__class__.__name__} and will be ignored." ) kwargs = {k: w for k, w in kwargs.items() if k in attn_parameters} return self.processor( self, hidden_states, encoder_hidden_states=encoder_hidden_states, sparse_params=sparse_params, rotary_emb=rotary_emb, **kwargs, ) class Kandinsky5FeedForward(nn.Module): def __init__(self, dim, ff_dim): super().__init__() self.in_layer = nn.Linear(dim, ff_dim, bias=False) self.activation = nn.GELU() self.out_layer = nn.Linear(ff_dim, dim, bias=False) def forward(self, x): return self.out_layer(self.activation(self.in_layer(x))) class Kandinsky5OutLayer(nn.Module): def __init__(self, model_dim, time_dim, visual_dim, patch_size): super().__init__() self.patch_size = patch_size self.modulation = Kandinsky5Modulation(time_dim, model_dim, 2) self.norm = nn.LayerNorm(model_dim, elementwise_affine=False) self.out_layer = nn.Linear(model_dim, math.prod(patch_size) * visual_dim, bias=True) def forward(self, visual_embed, text_embed, time_embed): shift, scale = torch.chunk(self.modulation(time_embed).unsqueeze(dim=1), 2, dim=-1) visual_embed = ( self.norm(visual_embed.float()) * (scale.float()[:, None, None] + 1.0) + shift.float()[:, None, None] ).type_as(visual_embed) x = self.out_layer(visual_embed) batch_size, duration, height, width, _ = x.shape x = ( x.view( batch_size, duration, height, width, -1, self.patch_size[0], self.patch_size[1], self.patch_size[2], ) .permute(0, 1, 5, 2, 6, 3, 7, 4) .flatten(1, 2) .flatten(2, 3) .flatten(3, 4) ) return x class Kandinsky5TransformerEncoderBlock(nn.Module): def __init__(self, model_dim, time_dim, ff_dim, head_dim): super().__init__() self.text_modulation = Kandinsky5Modulation(time_dim, model_dim, 6) self.self_attention_norm = nn.LayerNorm(model_dim, elementwise_affine=False) self.self_attention = Kandinsky5Attention(model_dim, head_dim, processor=Kandinsky5AttnProcessor()) self.feed_forward_norm = nn.LayerNorm(model_dim, elementwise_affine=False) self.feed_forward = Kandinsky5FeedForward(model_dim, ff_dim) def forward(self, x, time_embed, rope): self_attn_params, ff_params = torch.chunk(self.text_modulation(time_embed).unsqueeze(dim=1), 2, dim=-1) shift, scale, gate = torch.chunk(self_attn_params, 3, dim=-1) out = (self.self_attention_norm(x.float()) * (scale.float() + 1.0) + shift.float()).type_as(x) out = self.self_attention(out, rotary_emb=rope) x = (x.float() + gate.float() * out.float()).type_as(x) shift, scale, gate = torch.chunk(ff_params, 3, dim=-1) out = (self.feed_forward_norm(x.float()) * (scale.float() + 1.0) + shift.float()).type_as(x) out = self.feed_forward(out) x = (x.float() + gate.float() * out.float()).type_as(x) return x class Kandinsky5TransformerDecoderBlock(nn.Module): def __init__(self, model_dim, time_dim, ff_dim, head_dim): super().__init__() self.visual_modulation = Kandinsky5Modulation(time_dim, model_dim, 9) self.self_attention_norm = nn.LayerNorm(model_dim, elementwise_affine=False) self.self_attention = Kandinsky5Attention(model_dim, head_dim, processor=Kandinsky5AttnProcessor()) self.cross_attention_norm = nn.LayerNorm(model_dim, elementwise_affine=False) self.cross_attention = Kandinsky5Attention(model_dim, head_dim, processor=Kandinsky5AttnProcessor()) self.feed_forward_norm = nn.LayerNorm(model_dim, elementwise_affine=False) self.feed_forward = Kandinsky5FeedForward(model_dim, ff_dim) def forward(self, visual_embed, text_embed, time_embed, rope, sparse_params): self_attn_params, cross_attn_params, ff_params = torch.chunk( self.visual_modulation(time_embed).unsqueeze(dim=1), 3, dim=-1 ) shift, scale, gate = torch.chunk(self_attn_params, 3, dim=-1) visual_out = (self.self_attention_norm(visual_embed.float()) * (scale.float() + 1.0) + shift.float()).type_as( visual_embed ) visual_out = self.self_attention(visual_out, rotary_emb=rope, sparse_params=sparse_params) visual_embed = (visual_embed.float() + gate.float() * visual_out.float()).type_as(visual_embed) shift, scale, gate = torch.chunk(cross_attn_params, 3, dim=-1) visual_out = (self.cross_attention_norm(visual_embed.float()) * (scale.float() + 1.0) + shift.float()).type_as( visual_embed ) visual_out = self.cross_attention(visual_out, encoder_hidden_states=text_embed) visual_embed = (visual_embed.float() + gate.float() * visual_out.float()).type_as(visual_embed) shift, scale, gate = torch.chunk(ff_params, 3, dim=-1) visual_out = (self.feed_forward_norm(visual_embed.float()) * (scale.float() + 1.0) + shift.float()).type_as( visual_embed ) visual_out = self.feed_forward(visual_out) visual_embed = (visual_embed.float() + gate.float() * visual_out.float()).type_as(visual_embed) return visual_embed class Kandinsky5Transformer3DModel( ModelMixin, ConfigMixin, PeftAdapterMixin, FromOriginalModelMixin, CacheMixin, AttentionMixin, ): """ A 3D Diffusion Transformer model for video-like data. """ _repeated_blocks = [ "Kandinsky5TransformerEncoderBlock", "Kandinsky5TransformerDecoderBlock", ] _keep_in_fp32_modules = ["time_embeddings", "modulation", "visual_modulation", "text_modulation"] _supports_gradient_checkpointing = True @register_to_config def __init__( self, in_visual_dim=4, in_text_dim=3584, in_text_dim2=768, time_dim=512, out_visual_dim=4, patch_size=(1, 2, 2), model_dim=2048, ff_dim=5120, num_text_blocks=2, num_visual_blocks=32, axes_dims=(16, 24, 24), visual_cond=False, attention_type: str = "regular", attention_causal: bool = None, attention_local: bool = None, attention_glob: bool = None, attention_window: int = None, attention_P: float = None, attention_wT: int = None, attention_wW: int = None, attention_wH: int = None, attention_add_sta: bool = None, attention_method: str = None, ): super().__init__() head_dim = sum(axes_dims) self.in_visual_dim = in_visual_dim self.model_dim = model_dim self.patch_size = patch_size self.visual_cond = visual_cond self.attention_type = attention_type visual_embed_dim = 2 * in_visual_dim + 1 if visual_cond else in_visual_dim # Initialize embeddings self.time_embeddings = Kandinsky5TimeEmbeddings(model_dim, time_dim) self.text_embeddings = Kandinsky5TextEmbeddings(in_text_dim, model_dim) self.pooled_text_embeddings = Kandinsky5TextEmbeddings(in_text_dim2, time_dim) self.visual_embeddings = Kandinsky5VisualEmbeddings(visual_embed_dim, model_dim, patch_size) # Initialize positional embeddings self.text_rope_embeddings = Kandinsky5RoPE1D(head_dim) self.visual_rope_embeddings = Kandinsky5RoPE3D(axes_dims) # Initialize transformer blocks self.text_transformer_blocks = nn.ModuleList( [Kandinsky5TransformerEncoderBlock(model_dim, time_dim, ff_dim, head_dim) for _ in range(num_text_blocks)] ) self.visual_transformer_blocks = nn.ModuleList( [ Kandinsky5TransformerDecoderBlock(model_dim, time_dim, ff_dim, head_dim) for _ in range(num_visual_blocks) ] ) # Initialize output layer self.out_layer = Kandinsky5OutLayer(model_dim, time_dim, out_visual_dim, patch_size) self.gradient_checkpointing = False def forward( self, hidden_states: torch.Tensor, # x encoder_hidden_states: torch.Tensor, # text_embed timestep: torch.Tensor, # time pooled_projections: torch.Tensor, # pooled_text_embed visual_rope_pos: tuple[int, int, int], text_rope_pos: torch.LongTensor, scale_factor: tuple[float, float, float] = (1.0, 1.0, 1.0), sparse_params: dict[str, Any] | None = None, return_dict: bool = True, ) -> Transformer2DModelOutput | torch.FloatTensor: """ Forward pass of the Kandinsky5 3D Transformer. Args: hidden_states (`torch.FloatTensor`): Input visual states encoder_hidden_states (`torch.FloatTensor`): Text embeddings timestep (`torch.Tensor` or `float` or `int`): Current timestep pooled_projections (`torch.FloatTensor`): Pooled text embeddings visual_rope_pos (`tuple[int, int, int]`): Position for visual RoPE text_rope_pos (`torch.LongTensor`): Position for text RoPE scale_factor (`tuple[float, float, float]`, optional): Scale factor for RoPE sparse_params (`dict[str, Any]`, optional): Parameters for sparse attention return_dict (`bool`, optional): Whether to return a dictionary Returns: [`~models.transformer_2d.Transformer2DModelOutput`] or `torch.FloatTensor`: The output of the transformer """ x = hidden_states text_embed = encoder_hidden_states time = timestep pooled_text_embed = pooled_projections text_embed = self.text_embeddings(text_embed) time_embed = self.time_embeddings(time) time_embed = time_embed + self.pooled_text_embeddings(pooled_text_embed) visual_embed = self.visual_embeddings(x) text_rope = self.text_rope_embeddings(text_rope_pos) text_rope = text_rope.unsqueeze(dim=0) for text_transformer_block in self.text_transformer_blocks: if torch.is_grad_enabled() and self.gradient_checkpointing: text_embed = self._gradient_checkpointing_func( text_transformer_block, text_embed, time_embed, text_rope ) else: text_embed = text_transformer_block(text_embed, time_embed, text_rope) visual_shape = visual_embed.shape[:-1] visual_rope = self.visual_rope_embeddings(visual_shape, visual_rope_pos, scale_factor) to_fractal = sparse_params["to_fractal"] if sparse_params is not None else False visual_embed, visual_rope = fractal_flatten(visual_embed, visual_rope, visual_shape, block_mask=to_fractal) for visual_transformer_block in self.visual_transformer_blocks: if torch.is_grad_enabled() and self.gradient_checkpointing: visual_embed = self._gradient_checkpointing_func( visual_transformer_block, visual_embed, text_embed, time_embed, visual_rope, sparse_params, ) else: visual_embed = visual_transformer_block( visual_embed, text_embed, time_embed, visual_rope, sparse_params ) visual_embed = fractal_unflatten(visual_embed, visual_shape, block_mask=to_fractal) x = self.out_layer(visual_embed, text_embed, time_embed) if not return_dict: return x return Transformer2DModelOutput(sample=x)
{ "repo_id": "huggingface/diffusers", "file_path": "src/diffusers/models/transformers/transformer_kandinsky.py", "license": "Apache License 2.0", "lines": 556, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
huggingface/diffusers:src/diffusers/pipelines/kandinsky5/pipeline_kandinsky.py
# Copyright 2025 The Kandinsky Team and The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import html from typing import Callable import regex as re import torch from torch.nn import functional as F from transformers import CLIPTextModel, CLIPTokenizer, Qwen2_5_VLForConditionalGeneration, Qwen2VLProcessor from ...callbacks import MultiPipelineCallbacks, PipelineCallback from ...loaders import KandinskyLoraLoaderMixin from ...models import AutoencoderKLHunyuanVideo from ...models.transformers import Kandinsky5Transformer3DModel from ...schedulers import FlowMatchEulerDiscreteScheduler # Add imports for offloading and tiling from ...utils import ( is_ftfy_available, is_torch_xla_available, logging, replace_example_docstring, ) from ...utils.torch_utils import randn_tensor from ...video_processor import VideoProcessor from ..pipeline_utils import DiffusionPipeline from .pipeline_output import KandinskyPipelineOutput if is_torch_xla_available(): import torch_xla.core.xla_model as xm XLA_AVAILABLE = True else: XLA_AVAILABLE = False logger = logging.get_logger(__name__) # pylint: disable=invalid-name if is_ftfy_available(): import ftfy logger = logging.get_logger(__name__) EXAMPLE_DOC_STRING = """ Examples: ```python >>> import torch >>> from diffusers import Kandinsky5T2VPipeline >>> from diffusers.utils import export_to_video >>> # Available models: >>> # kandinskylab/Kandinsky-5.0-T2V-Pro-sft-5s-Diffusers >>> # kandinskylab/Kandinsky-5.0-T2V-Lite-sft-5s-Diffusers >>> # kandinskylab/Kandinsky-5.0-T2V-Lite-nocfg-5s-Diffusers >>> # kandinskylab/Kandinsky-5.0-T2V-Lite-distilled16steps-5s-Diffusers >>> # kandinskylab/Kandinsky-5.0-T2V-Lite-pretrain-5s-Diffusers >>> # kandinskylab/Kandinsky-5.0-T2V-Lite-sft-10s-Diffusers >>> # kandinskylab/Kandinsky-5.0-T2V-Lite-nocfg-10s-Diffusers >>> # kandinskylab/Kandinsky-5.0-T2V-Lite-distilled16steps-10s-Diffusers >>> # kandinskylab/Kandinsky-5.0-T2V-Lite-pretrain-10s-Diffusers >>> model_id = "kandinskylab/Kandinsky-5.0-T2V-Lite-sft-5s-Diffusers" >>> pipe = Kandinsky5T2VPipeline.from_pretrained(model_id, torch_dtype=torch.bfloat16) >>> pipe = pipe.to("cuda") >>> prompt = "A cat and a dog baking a cake together in a kitchen." >>> negative_prompt = "Static, 2D cartoon, cartoon, 2d animation, paintings, images, worst quality, low quality, ugly, deformed, walking backwards" >>> output = pipe( ... prompt=prompt, ... negative_prompt=negative_prompt, ... height=512, ... width=768, ... num_frames=121, ... num_inference_steps=50, ... guidance_scale=5.0, ... ).frames[0] >>> export_to_video(output, "output.mp4", fps=24, quality=9) ``` """ def basic_clean(text): """ Copied from https://github.com/huggingface/diffusers/blob/main/src/diffusers/pipelines/wan/pipeline_wan.py Clean text using ftfy if available and unescape HTML entities. """ if is_ftfy_available(): text = ftfy.fix_text(text) text = html.unescape(html.unescape(text)) return text.strip() def whitespace_clean(text): """ Copied from https://github.com/huggingface/diffusers/blob/main/src/diffusers/pipelines/wan/pipeline_wan.py Normalize whitespace in text by replacing multiple spaces with single space. """ text = re.sub(r"\s+", " ", text) text = text.strip() return text def prompt_clean(text): """ Copied from https://github.com/huggingface/diffusers/blob/main/src/diffusers/pipelines/wan/pipeline_wan.py Apply both basic cleaning and whitespace normalization to prompts. """ text = whitespace_clean(basic_clean(text)) return text class Kandinsky5T2VPipeline(DiffusionPipeline, KandinskyLoraLoaderMixin): r""" Pipeline for text-to-video generation using Kandinsky 5.0. This model inherits from [`DiffusionPipeline`]. Check the superclass documentation for the generic methods implemented for all pipelines (downloading, saving, running on a particular device, etc.). Args: transformer ([`Kandinsky5Transformer3DModel`]): Conditional Transformer to denoise the encoded video latents. vae ([`AutoencoderKLHunyuanVideo`]): Variational Auto-Encoder Model [hunyuanvideo-community/HunyuanVideo (vae)](https://huggingface.co/hunyuanvideo-community/HunyuanVideo) to encode and decode videos to and from latent representations. text_encoder ([`Qwen2_5_VLForConditionalGeneration`]): Frozen text-encoder [Qwen2.5-VL](https://huggingface.co/Qwen/Qwen2.5-VL-7B-Instruct). tokenizer ([`AutoProcessor`]): Tokenizer for Qwen2.5-VL. text_encoder_2 ([`CLIPTextModel`]): Frozen [CLIP](https://huggingface.co/docs/transformers/model_doc/clip#transformers.CLIPTextModel), specifically the [clip-vit-large-patch14](https://huggingface.co/openai/clip-vit-large-patch14) variant. tokenizer_2 ([`CLIPTokenizer`]): Tokenizer for CLIP. scheduler ([`FlowMatchEulerDiscreteScheduler`]): A scheduler to be used in combination with `transformer` to denoise the encoded video latents. """ model_cpu_offload_seq = "text_encoder->text_encoder_2->transformer->vae" _callback_tensor_inputs = [ "latents", "prompt_embeds_qwen", "prompt_embeds_clip", "negative_prompt_embeds_qwen", "negative_prompt_embeds_clip", ] def __init__( self, transformer: Kandinsky5Transformer3DModel, vae: AutoencoderKLHunyuanVideo, text_encoder: Qwen2_5_VLForConditionalGeneration, tokenizer: Qwen2VLProcessor, text_encoder_2: CLIPTextModel, tokenizer_2: CLIPTokenizer, scheduler: FlowMatchEulerDiscreteScheduler, ): super().__init__() self.register_modules( transformer=transformer, vae=vae, text_encoder=text_encoder, tokenizer=tokenizer, text_encoder_2=text_encoder_2, tokenizer_2=tokenizer_2, scheduler=scheduler, ) self.prompt_template = "\n".join( [ "<|im_start|>system\nYou are a promt engineer. Describe the video in detail.", "Describe how the camera moves or shakes, describe the zoom and view angle, whether it follows the objects.", "Describe the location of the video, main characters or objects and their action.", "Describe the dynamism of the video and presented actions.", "Name the visual style of the video: whether it is a professional footage, user generated content, some kind of animation, video game or scren content.", "Describe the visual effects, postprocessing and transitions if they are presented in the video.", "Pay attention to the order of key actions shown in the scene.<|im_end|>", "<|im_start|>user\n{}<|im_end|>", ] ) self.prompt_template_encode_start_idx = 129 self.vae_scale_factor_temporal = ( self.vae.config.temporal_compression_ratio if getattr(self, "vae", None) else 4 ) self.vae_scale_factor_spatial = self.vae.config.spatial_compression_ratio if getattr(self, "vae", None) else 8 self.video_processor = VideoProcessor(vae_scale_factor=self.vae_scale_factor_spatial) def _get_scale_factor(self, height: int, width: int) -> tuple: """ Calculate the scale factor based on resolution. Args: height (int): Video height width (int): Video width Returns: tuple: Scale factor as (temporal_scale, height_scale, width_scale) """ def between_480p(x): return 480 <= x <= 854 if between_480p(height) and between_480p(width): return (1, 2, 2) else: return (1, 3.16, 3.16) @staticmethod def fast_sta_nabla(T: int, H: int, W: int, wT: int = 3, wH: int = 3, wW: int = 3, device="cuda") -> torch.Tensor: """ Create a sparse temporal attention (STA) mask for efficient video generation. This method generates a mask that limits attention to nearby frames and spatial positions, reducing computational complexity for video generation. Args: T (int): Number of temporal frames H (int): Height in latent space W (int): Width in latent space wT (int): Temporal attention window size wH (int): Height attention window size wW (int): Width attention window size device (str): Device to create tensor on Returns: torch.Tensor: Sparse attention mask of shape (T*H*W, T*H*W) """ l = torch.Tensor([T, H, W]).amax() r = torch.arange(0, l, 1, dtype=torch.int16, device=device) mat = (r.unsqueeze(1) - r.unsqueeze(0)).abs() sta_t, sta_h, sta_w = ( mat[:T, :T].flatten(), mat[:H, :H].flatten(), mat[:W, :W].flatten(), ) sta_t = sta_t <= wT // 2 sta_h = sta_h <= wH // 2 sta_w = sta_w <= wW // 2 sta_hw = (sta_h.unsqueeze(1) * sta_w.unsqueeze(0)).reshape(H, H, W, W).transpose(1, 2).flatten() sta = (sta_t.unsqueeze(1) * sta_hw.unsqueeze(0)).reshape(T, T, H * W, H * W).transpose(1, 2) return sta.reshape(T * H * W, T * H * W) def get_sparse_params(self, sample, device): """ Generate sparse attention parameters for the transformer based on sample dimensions. This method computes the sparse attention configuration needed for efficient video processing in the transformer model. Args: sample (torch.Tensor): Input sample tensor device (torch.device): Device to place tensors on Returns: Dict: Dictionary containing sparse attention parameters """ assert self.transformer.config.patch_size[0] == 1 B, T, H, W, _ = sample.shape T, H, W = ( T // self.transformer.config.patch_size[0], H // self.transformer.config.patch_size[1], W // self.transformer.config.patch_size[2], ) if self.transformer.config.attention_type == "nabla": sta_mask = self.fast_sta_nabla( T, H // 8, W // 8, self.transformer.config.attention_wT, self.transformer.config.attention_wH, self.transformer.config.attention_wW, device=device, ) sparse_params = { "sta_mask": sta_mask.unsqueeze_(0).unsqueeze_(0), "attention_type": self.transformer.config.attention_type, "to_fractal": True, "P": self.transformer.config.attention_P, "wT": self.transformer.config.attention_wT, "wW": self.transformer.config.attention_wW, "wH": self.transformer.config.attention_wH, "add_sta": self.transformer.config.attention_add_sta, "visual_shape": (T, H, W), "method": self.transformer.config.attention_method, } else: sparse_params = None return sparse_params def _encode_prompt_qwen( self, prompt: str | list[str], device: torch.device | None = None, max_sequence_length: int = 256, dtype: torch.dtype | None = None, ): """ Encode prompt using Qwen2.5-VL text encoder. This method processes the input prompt through the Qwen2.5-VL model to generate text embeddings suitable for video generation. Args: prompt (str | list[str]): Input prompt or list of prompts device (torch.device): Device to run encoding on num_videos_per_prompt (int): Number of videos to generate per prompt max_sequence_length (int): Maximum sequence length for tokenization dtype (torch.dtype): Data type for embeddings Returns: tuple[torch.Tensor, torch.Tensor]: Text embeddings and cumulative sequence lengths """ device = device or self._execution_device dtype = dtype or self.text_encoder.dtype full_texts = [self.prompt_template.format(p) for p in prompt] max_allowed_len = self.prompt_template_encode_start_idx + max_sequence_length untruncated_ids = self.tokenizer( text=full_texts, images=None, videos=None, return_tensors="pt", padding="longest", )["input_ids"] if untruncated_ids.shape[-1] > max_allowed_len: for i, text in enumerate(full_texts): tokens = untruncated_ids[i][self.prompt_template_encode_start_idx : -2] removed_text = self.tokenizer.decode(tokens[max_sequence_length - 2 :]) if len(removed_text) > 0: full_texts[i] = text[: -len(removed_text)] logger.warning( "The following part of your input was truncated because `max_sequence_length` is set to " f" {max_sequence_length} tokens: {removed_text}" ) inputs = self.tokenizer( text=full_texts, images=None, videos=None, max_length=max_allowed_len, truncation=True, return_tensors="pt", padding=True, ).to(device) embeds = self.text_encoder( input_ids=inputs["input_ids"], return_dict=True, output_hidden_states=True, )["hidden_states"][-1][:, self.prompt_template_encode_start_idx :] attention_mask = inputs["attention_mask"][:, self.prompt_template_encode_start_idx :] cu_seqlens = torch.cumsum(attention_mask.sum(1), dim=0) cu_seqlens = F.pad(cu_seqlens, (1, 0), value=0).to(dtype=torch.int32) return embeds.to(dtype), cu_seqlens def _encode_prompt_clip( self, prompt: str | list[str], device: torch.device | None = None, dtype: torch.dtype | None = None, ): """ Encode prompt using CLIP text encoder. This method processes the input prompt through the CLIP model to generate pooled embeddings that capture semantic information. Args: prompt (str | list[str]): Input prompt or list of prompts device (torch.device): Device to run encoding on num_videos_per_prompt (int): Number of videos to generate per prompt dtype (torch.dtype): Data type for embeddings Returns: torch.Tensor: Pooled text embeddings from CLIP """ device = device or self._execution_device dtype = dtype or self.text_encoder_2.dtype inputs = self.tokenizer_2( prompt, max_length=77, truncation=True, add_special_tokens=True, padding="max_length", return_tensors="pt", ).to(device) pooled_embed = self.text_encoder_2(**inputs)["pooler_output"] return pooled_embed.to(dtype) def encode_prompt( self, prompt: str | list[str], num_videos_per_prompt: int = 1, max_sequence_length: int = 512, device: torch.device | None = None, dtype: torch.dtype | None = None, ): r""" Encodes a single prompt (positive or negative) into text encoder hidden states. This method combines embeddings from both Qwen2.5-VL and CLIP text encoders to create comprehensive text representations for video generation. Args: prompt (`str` or `list[str]`): Prompt to be encoded. num_videos_per_prompt (`int`, *optional*, defaults to 1): Number of videos to generate per prompt. max_sequence_length (`int`, *optional*, defaults to 512): Maximum sequence length for text encoding. device (`torch.device`, *optional*): Torch device. dtype (`torch.dtype`, *optional*): Torch dtype. Returns: tuple[torch.Tensor, torch.Tensor, torch.Tensor]: - Qwen text embeddings of shape (batch_size * num_videos_per_prompt, sequence_length, embedding_dim) - CLIP pooled embeddings of shape (batch_size * num_videos_per_prompt, clip_embedding_dim) - Cumulative sequence lengths (`cu_seqlens`) for Qwen embeddings of shape (batch_size * num_videos_per_prompt + 1,) """ device = device or self._execution_device dtype = dtype or self.text_encoder.dtype if not isinstance(prompt, list): prompt = [prompt] batch_size = len(prompt) prompt = [prompt_clean(p) for p in prompt] # Encode with Qwen2.5-VL prompt_embeds_qwen, prompt_cu_seqlens = self._encode_prompt_qwen( prompt=prompt, device=device, max_sequence_length=max_sequence_length, dtype=dtype, ) # prompt_embeds_qwen shape: [batch_size, seq_len, embed_dim] # Encode with CLIP prompt_embeds_clip = self._encode_prompt_clip( prompt=prompt, device=device, dtype=dtype, ) # prompt_embeds_clip shape: [batch_size, clip_embed_dim] # Repeat embeddings for num_videos_per_prompt # Qwen embeddings: repeat sequence for each video, then reshape prompt_embeds_qwen = prompt_embeds_qwen.repeat( 1, num_videos_per_prompt, 1 ) # [batch_size, seq_len * num_videos_per_prompt, embed_dim] # Reshape to [batch_size * num_videos_per_prompt, seq_len, embed_dim] prompt_embeds_qwen = prompt_embeds_qwen.view( batch_size * num_videos_per_prompt, -1, prompt_embeds_qwen.shape[-1] ) # CLIP embeddings: repeat for each video prompt_embeds_clip = prompt_embeds_clip.repeat( 1, num_videos_per_prompt, 1 ) # [batch_size, num_videos_per_prompt, clip_embed_dim] # Reshape to [batch_size * num_videos_per_prompt, clip_embed_dim] prompt_embeds_clip = prompt_embeds_clip.view(batch_size * num_videos_per_prompt, -1) # Repeat cumulative sequence lengths for num_videos_per_prompt # Original cu_seqlens: [0, len1, len1+len2, ...] # Need to repeat the differences and reconstruct for repeated prompts # Original differences (lengths) for each prompt in the batch original_lengths = prompt_cu_seqlens.diff() # [len1, len2, ...] # Repeat the lengths for num_videos_per_prompt repeated_lengths = original_lengths.repeat_interleave( num_videos_per_prompt ) # [len1, len1, ..., len2, len2, ...] # Reconstruct the cumulative lengths repeated_cu_seqlens = torch.cat( [torch.tensor([0], device=device, dtype=torch.int32), repeated_lengths.cumsum(0)] ) return prompt_embeds_qwen, prompt_embeds_clip, repeated_cu_seqlens def check_inputs( self, prompt, negative_prompt, height, width, prompt_embeds_qwen=None, prompt_embeds_clip=None, negative_prompt_embeds_qwen=None, negative_prompt_embeds_clip=None, prompt_cu_seqlens=None, negative_prompt_cu_seqlens=None, callback_on_step_end_tensor_inputs=None, max_sequence_length=None, ): """ Validate input parameters for the pipeline. Args: prompt: Input prompt negative_prompt: Negative prompt for guidance height: Video height width: Video width prompt_embeds_qwen: Pre-computed Qwen prompt embeddings prompt_embeds_clip: Pre-computed CLIP prompt embeddings negative_prompt_embeds_qwen: Pre-computed Qwen negative prompt embeddings negative_prompt_embeds_clip: Pre-computed CLIP negative prompt embeddings prompt_cu_seqlens: Pre-computed cumulative sequence lengths for Qwen positive prompt negative_prompt_cu_seqlens: Pre-computed cumulative sequence lengths for Qwen negative prompt callback_on_step_end_tensor_inputs: Callback tensor inputs Raises: ValueError: If inputs are invalid """ if max_sequence_length is not None and max_sequence_length > 1024: raise ValueError("max_sequence_length must be less than 1024") if height % 16 != 0 or width % 16 != 0: raise ValueError(f"`height` and `width` have to be divisible by 16 but are {height} and {width}.") if callback_on_step_end_tensor_inputs is not None and not all( k in self._callback_tensor_inputs for k in callback_on_step_end_tensor_inputs ): raise ValueError( f"`callback_on_step_end_tensor_inputs` has to be in {self._callback_tensor_inputs}, but found {[k for k in callback_on_step_end_tensor_inputs if k not in self._callback_tensor_inputs]}" ) # Check for consistency within positive prompt embeddings and sequence lengths if prompt_embeds_qwen is not None or prompt_embeds_clip is not None or prompt_cu_seqlens is not None: if prompt_embeds_qwen is None or prompt_embeds_clip is None or prompt_cu_seqlens is None: raise ValueError( "If any of `prompt_embeds_qwen`, `prompt_embeds_clip`, or `prompt_cu_seqlens` is provided, " "all three must be provided." ) # Check for consistency within negative prompt embeddings and sequence lengths if ( negative_prompt_embeds_qwen is not None or negative_prompt_embeds_clip is not None or negative_prompt_cu_seqlens is not None ): if ( negative_prompt_embeds_qwen is None or negative_prompt_embeds_clip is None or negative_prompt_cu_seqlens is None ): raise ValueError( "If any of `negative_prompt_embeds_qwen`, `negative_prompt_embeds_clip`, or `negative_prompt_cu_seqlens` is provided, " "all three must be provided." ) # Check if prompt or embeddings are provided (either prompt or all required embedding components for positive) if prompt is None and prompt_embeds_qwen is None: raise ValueError( "Provide either `prompt` or `prompt_embeds_qwen` (and corresponding `prompt_embeds_clip` and `prompt_cu_seqlens`). Cannot leave all undefined." ) # Validate types for prompt and negative_prompt if provided if prompt is not None and (not isinstance(prompt, str) and not isinstance(prompt, list)): raise ValueError(f"`prompt` has to be of type `str` or `list` but is {type(prompt)}") if negative_prompt is not None and ( not isinstance(negative_prompt, str) and not isinstance(negative_prompt, list) ): raise ValueError(f"`negative_prompt` has to be of type `str` or `list` but is {type(negative_prompt)}") def prepare_latents( self, batch_size: int, num_channels_latents: int = 16, height: int = 480, width: int = 832, num_frames: int = 81, dtype: torch.dtype | None = None, device: torch.device | None = None, generator: torch.Generator | list[torch.Generator] | None = None, latents: torch.Tensor | None = None, ) -> torch.Tensor: """ Prepare initial latent variables for video generation. This method creates random noise latents or uses provided latents as starting point for the denoising process. Args: batch_size (int): Number of videos to generate num_channels_latents (int): Number of channels in latent space height (int): Height of generated video width (int): Width of generated video num_frames (int): Number of frames in video dtype (torch.dtype): Data type for latents device (torch.device): Device to create latents on generator (torch.Generator): Random number generator latents (torch.Tensor): Pre-existing latents to use Returns: torch.Tensor: Prepared latent tensor """ if latents is not None: return latents.to(device=device, dtype=dtype) num_latent_frames = (num_frames - 1) // self.vae_scale_factor_temporal + 1 shape = ( batch_size, num_latent_frames, int(height) // self.vae_scale_factor_spatial, int(width) // self.vae_scale_factor_spatial, num_channels_latents, ) if isinstance(generator, list) and len(generator) != batch_size: raise ValueError( f"You have passed a list of generators of length {len(generator)}, but requested an effective batch" f" size of {batch_size}. Make sure the batch size matches the length of the generators." ) latents = randn_tensor(shape, generator=generator, device=device, dtype=dtype) if self.transformer.visual_cond: # For visual conditioning, concatenate with zeros and mask visual_cond = torch.zeros_like(latents) visual_cond_mask = torch.zeros( [ batch_size, num_latent_frames, int(height) // self.vae_scale_factor_spatial, int(width) // self.vae_scale_factor_spatial, 1, ], dtype=latents.dtype, device=latents.device, ) latents = torch.cat([latents, visual_cond, visual_cond_mask], dim=-1) return latents @property def guidance_scale(self): """Get the current guidance scale value.""" return self._guidance_scale @property def num_timesteps(self): """Get the number of denoising timesteps.""" return self._num_timesteps @property def interrupt(self): """Check if generation has been interrupted.""" return self._interrupt @torch.no_grad() @replace_example_docstring(EXAMPLE_DOC_STRING) def __call__( self, prompt: str | list[str] = None, negative_prompt: str | list[str] | None = None, height: int = 512, width: int = 768, num_frames: int = 121, num_inference_steps: int = 50, guidance_scale: float = 5.0, num_videos_per_prompt: int | None = 1, generator: torch.Generator | list[torch.Generator] | None = None, latents: torch.Tensor | None = None, prompt_embeds_qwen: torch.Tensor | None = None, prompt_embeds_clip: torch.Tensor | None = None, negative_prompt_embeds_qwen: torch.Tensor | None = None, negative_prompt_embeds_clip: torch.Tensor | None = None, prompt_cu_seqlens: torch.Tensor | None = None, negative_prompt_cu_seqlens: torch.Tensor | None = None, output_type: str | None = "pil", return_dict: bool = True, callback_on_step_end: Callable[[int, int], None] | PipelineCallback | MultiPipelineCallbacks | None = None, callback_on_step_end_tensor_inputs: list[str] = ["latents"], max_sequence_length: int = 512, ): r""" The call function to the pipeline for generation. Args: prompt (`str` or `list[str]`, *optional*): The prompt or prompts to guide the video generation. If not defined, pass `prompt_embeds` instead. negative_prompt (`str` or `list[str]`, *optional*): The prompt or prompts to avoid during video generation. If not defined, pass `negative_prompt_embeds` instead. Ignored when not using guidance (`guidance_scale` < `1`). height (`int`, defaults to `512`): The height in pixels of the generated video. width (`int`, defaults to `768`): The width in pixels of the generated video. num_frames (`int`, defaults to `25`): The number of frames in the generated video. num_inference_steps (`int`, defaults to `50`): The number of denoising steps. guidance_scale (`float`, defaults to `5.0`): Guidance scale as defined in classifier-free guidance. num_videos_per_prompt (`int`, *optional*, defaults to 1): The number of videos to generate per prompt. generator (`torch.Generator` or `list[torch.Generator]`, *optional*): A torch generator to make generation deterministic. latents (`torch.Tensor`, *optional*): Pre-generated noisy latents. prompt_embeds (`torch.Tensor`, *optional*): Pre-generated text embeddings. negative_prompt_embeds (`torch.Tensor`, *optional*): Pre-generated negative text embeddings. output_type (`str`, *optional*, defaults to `"pil"`): The output format of the generated video. return_dict (`bool`, *optional*, defaults to `True`): Whether or not to return a [`KandinskyPipelineOutput`]. callback_on_step_end (`Callable`, `PipelineCallback`, `MultiPipelineCallbacks`, *optional*): A function that is called at the end of each denoising step. callback_on_step_end_tensor_inputs (`list`, *optional*): The list of tensor inputs for the `callback_on_step_end` function. max_sequence_length (`int`, defaults to `512`): The maximum sequence length for text encoding. Examples: Returns: [`~KandinskyPipelineOutput`] or `tuple`: If `return_dict` is `True`, [`KandinskyPipelineOutput`] is returned, otherwise a `tuple` is returned where the first element is a list with the generated images. """ if isinstance(callback_on_step_end, (PipelineCallback, MultiPipelineCallbacks)): callback_on_step_end_tensor_inputs = callback_on_step_end.tensor_inputs # 1. Check inputs. Raise error if not correct self.check_inputs( prompt=prompt, negative_prompt=negative_prompt, height=height, width=width, prompt_embeds_qwen=prompt_embeds_qwen, prompt_embeds_clip=prompt_embeds_clip, negative_prompt_embeds_qwen=negative_prompt_embeds_qwen, negative_prompt_embeds_clip=negative_prompt_embeds_clip, prompt_cu_seqlens=prompt_cu_seqlens, negative_prompt_cu_seqlens=negative_prompt_cu_seqlens, callback_on_step_end_tensor_inputs=callback_on_step_end_tensor_inputs, max_sequence_length=max_sequence_length, ) if num_frames % self.vae_scale_factor_temporal != 1: logger.warning( f"`num_frames - 1` has to be divisible by {self.vae_scale_factor_temporal}. Rounding to the nearest number." ) num_frames = num_frames // self.vae_scale_factor_temporal * self.vae_scale_factor_temporal + 1 num_frames = max(num_frames, 1) self._guidance_scale = guidance_scale self._interrupt = False device = self._execution_device dtype = self.transformer.dtype # 2. Define call parameters if prompt is not None and isinstance(prompt, str): batch_size = 1 prompt = [prompt] elif prompt is not None and isinstance(prompt, list): batch_size = len(prompt) else: batch_size = prompt_embeds_qwen.shape[0] # 3. Encode input prompt if prompt_embeds_qwen is None: prompt_embeds_qwen, prompt_embeds_clip, prompt_cu_seqlens = self.encode_prompt( prompt=prompt, max_sequence_length=max_sequence_length, device=device, dtype=dtype, ) if self.guidance_scale > 1.0: if negative_prompt is None: negative_prompt = "Static, 2D cartoon, cartoon, 2d animation, paintings, images, worst quality, low quality, ugly, deformed, walking backwards" if isinstance(negative_prompt, str): negative_prompt = [negative_prompt] * len(prompt) if prompt is not None else [negative_prompt] elif len(negative_prompt) != len(prompt): raise ValueError( f"`negative_prompt` must have same length as `prompt`. Got {len(negative_prompt)} vs {len(prompt)}." ) if negative_prompt_embeds_qwen is None: negative_prompt_embeds_qwen, negative_prompt_embeds_clip, negative_prompt_cu_seqlens = ( self.encode_prompt( prompt=negative_prompt, max_sequence_length=max_sequence_length, device=device, dtype=dtype, ) ) # 4. Prepare timesteps self.scheduler.set_timesteps(num_inference_steps, device=device) timesteps = self.scheduler.timesteps # 5. Prepare latent variables num_channels_latents = self.transformer.config.in_visual_dim latents = self.prepare_latents( batch_size * num_videos_per_prompt, num_channels_latents, height, width, num_frames, dtype, device, generator, latents, ) # 6. Prepare rope positions for positional encoding num_latent_frames = (num_frames - 1) // self.vae_scale_factor_temporal + 1 visual_rope_pos = [ torch.arange(num_latent_frames, device=device), torch.arange(height // self.vae_scale_factor_spatial // 2, device=device), torch.arange(width // self.vae_scale_factor_spatial // 2, device=device), ] text_rope_pos = torch.arange(prompt_cu_seqlens.diff().max().item(), device=device) negative_text_rope_pos = ( torch.arange(negative_prompt_cu_seqlens.diff().max().item(), device=device) if negative_prompt_cu_seqlens is not None else None ) # 7. Calculate dynamic scale factor based on resolution scale_factor = self._get_scale_factor(height, width) # 8. Sparse Params for efficient attention sparse_params = self.get_sparse_params(latents, device) # 9. Denoising loop num_warmup_steps = len(timesteps) - num_inference_steps * self.scheduler.order self._num_timesteps = len(timesteps) with self.progress_bar(total=num_inference_steps) as progress_bar: for i, t in enumerate(timesteps): if self.interrupt: continue timestep = t.unsqueeze(0).repeat(batch_size * num_videos_per_prompt) # Predict noise residual pred_velocity = self.transformer( hidden_states=latents.to(dtype), encoder_hidden_states=prompt_embeds_qwen.to(dtype), pooled_projections=prompt_embeds_clip.to(dtype), timestep=timestep.to(dtype), visual_rope_pos=visual_rope_pos, text_rope_pos=text_rope_pos, scale_factor=scale_factor, sparse_params=sparse_params, return_dict=True, ).sample if self.guidance_scale > 1.0 and negative_prompt_embeds_qwen is not None: uncond_pred_velocity = self.transformer( hidden_states=latents.to(dtype), encoder_hidden_states=negative_prompt_embeds_qwen.to(dtype), pooled_projections=negative_prompt_embeds_clip.to(dtype), timestep=timestep.to(dtype), visual_rope_pos=visual_rope_pos, text_rope_pos=negative_text_rope_pos, scale_factor=scale_factor, sparse_params=sparse_params, return_dict=True, ).sample pred_velocity = uncond_pred_velocity + guidance_scale * (pred_velocity - uncond_pred_velocity) # Compute previous sample using the scheduler latents[:, :, :, :, :num_channels_latents] = self.scheduler.step( pred_velocity, t, latents[:, :, :, :, :num_channels_latents], return_dict=False )[0] if callback_on_step_end is not None: callback_kwargs = {} for k in callback_on_step_end_tensor_inputs: callback_kwargs[k] = locals()[k] callback_outputs = callback_on_step_end(self, i, t, callback_kwargs) latents = callback_outputs.pop("latents", latents) prompt_embeds_qwen = callback_outputs.pop("prompt_embeds_qwen", prompt_embeds_qwen) prompt_embeds_clip = callback_outputs.pop("prompt_embeds_clip", prompt_embeds_clip) negative_prompt_embeds_qwen = callback_outputs.pop( "negative_prompt_embeds_qwen", negative_prompt_embeds_qwen ) negative_prompt_embeds_clip = callback_outputs.pop( "negative_prompt_embeds_clip", negative_prompt_embeds_clip ) if i == len(timesteps) - 1 or ((i + 1) > num_warmup_steps and (i + 1) % self.scheduler.order == 0): progress_bar.update() if XLA_AVAILABLE: xm.mark_step() # 10. Post-processing - extract main latents latents = latents[:, :, :, :, :num_channels_latents] # 11. Decode latents to video if output_type != "latent": latents = latents.to(self.vae.dtype) # Reshape and normalize latents video = latents.reshape( batch_size, num_videos_per_prompt, (num_frames - 1) // self.vae_scale_factor_temporal + 1, height // self.vae_scale_factor_spatial, width // self.vae_scale_factor_spatial, num_channels_latents, ) video = video.permute(0, 1, 5, 2, 3, 4) # [batch, num_videos, channels, frames, height, width] video = video.reshape( batch_size * num_videos_per_prompt, num_channels_latents, (num_frames - 1) // self.vae_scale_factor_temporal + 1, height // self.vae_scale_factor_spatial, width // self.vae_scale_factor_spatial, ) # Normalize and decode through VAE video = video / self.vae.config.scaling_factor video = self.vae.decode(video).sample video = self.video_processor.postprocess_video(video, output_type=output_type) else: video = latents # Offload all models self.maybe_free_model_hooks() if not return_dict: return (video,) return KandinskyPipelineOutput(frames=video)
{ "repo_id": "huggingface/diffusers", "file_path": "src/diffusers/pipelines/kandinsky5/pipeline_kandinsky.py", "license": "Apache License 2.0", "lines": 829, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
huggingface/diffusers:src/diffusers/pipelines/kandinsky5/pipeline_output.py
from dataclasses import dataclass import torch from diffusers.utils import BaseOutput @dataclass class KandinskyPipelineOutput(BaseOutput): r""" Output class for kandinsky video pipelines. Args: frames (`torch.Tensor`, `np.ndarray`, or list[list[PIL.Image.Image]]): list of video outputs - It can be a nested list of length `batch_size,` with each sub-list containing denoised PIL image sequences of length `num_frames.` It can also be a NumPy array or Torch tensor of shape `(batch_size, num_frames, channels, height, width)`. """ frames: torch.Tensor @dataclass class KandinskyImagePipelineOutput(BaseOutput): r""" Output class for kandinsky image pipelines. Args: image (`torch.Tensor`, `np.ndarray`, or list[PIL.Image.Image]): List of image outputs - It can be a nested list of length `batch_size,` with each sub-list containing denoised PIL image. It can also be a NumPy array or Torch tensor of shape `(batch_size, channels, height, width)`. """ image: torch.Tensor
{ "repo_id": "huggingface/diffusers", "file_path": "src/diffusers/pipelines/kandinsky5/pipeline_output.py", "license": "Apache License 2.0", "lines": 25, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
huggingface/diffusers:tests/models/autoencoders/testing_utils.py
import inspect import numpy as np import pytest import torch from diffusers.models.autoencoders.vae import DecoderOutput from diffusers.utils.torch_utils import torch_device class AutoencoderTesterMixin: """ Test mixin class specific to VAEs to test for slicing and tiling. Diffusion networks usually don't do slicing and tiling. """ @staticmethod def _accepts_generator(model): model_sig = inspect.signature(model.forward) accepts_generator = "generator" in model_sig.parameters return accepts_generator @staticmethod def _accepts_norm_num_groups(model_class): model_sig = inspect.signature(model_class.__init__) accepts_norm_groups = "norm_num_groups" in model_sig.parameters return accepts_norm_groups def test_forward_with_norm_groups(self): if not self._accepts_norm_num_groups(self.model_class): pytest.skip(f"Test not supported for {self.model_class.__name__}") init_dict, inputs_dict = self.prepare_init_args_and_inputs_for_common() init_dict["norm_num_groups"] = 16 init_dict["block_out_channels"] = (16, 32) model = self.model_class(**init_dict) model.to(torch_device) model.eval() with torch.no_grad(): output = model(**inputs_dict) if isinstance(output, dict): output = output.to_tuple()[0] self.assertIsNotNone(output) expected_shape = inputs_dict["sample"].shape self.assertEqual(output.shape, expected_shape, "Input and output shapes do not match") def test_enable_disable_tiling(self): if not hasattr(self.model_class, "enable_tiling"): pytest.skip(f"Skipping test as {self.model_class.__name__} doesn't support tiling.") init_dict, inputs_dict = self.prepare_init_args_and_inputs_for_common() torch.manual_seed(0) model = self.model_class(**init_dict).to(torch_device) if not hasattr(model, "use_tiling"): pytest.skip(f"Skipping test as {self.model_class.__name__} doesn't support tiling.") inputs_dict.update({"return_dict": False}) _ = inputs_dict.pop("generator", None) accepts_generator = self._accepts_generator(model) torch.manual_seed(0) if accepts_generator: inputs_dict["generator"] = torch.manual_seed(0) output_without_tiling = model(**inputs_dict)[0] # Mochi-1 if isinstance(output_without_tiling, DecoderOutput): output_without_tiling = output_without_tiling.sample torch.manual_seed(0) model.enable_tiling() if accepts_generator: inputs_dict["generator"] = torch.manual_seed(0) output_with_tiling = model(**inputs_dict)[0] if isinstance(output_with_tiling, DecoderOutput): output_with_tiling = output_with_tiling.sample assert ( output_without_tiling.detach().cpu().numpy() - output_with_tiling.detach().cpu().numpy() ).max() < 0.5, "VAE tiling should not affect the inference results" torch.manual_seed(0) model.disable_tiling() if accepts_generator: inputs_dict["generator"] = torch.manual_seed(0) output_without_tiling_2 = model(**inputs_dict)[0] if isinstance(output_without_tiling_2, DecoderOutput): output_without_tiling_2 = output_without_tiling_2.sample assert np.allclose( output_without_tiling.detach().cpu().numpy().all(), output_without_tiling_2.detach().cpu().numpy().all(), ), "Without tiling outputs should match with the outputs when tiling is manually disabled." def test_enable_disable_slicing(self): if not hasattr(self.model_class, "enable_slicing"): pytest.skip(f"Skipping test as {self.model_class.__name__} doesn't support slicing.") init_dict, inputs_dict = self.prepare_init_args_and_inputs_for_common() torch.manual_seed(0) model = self.model_class(**init_dict).to(torch_device) if not hasattr(model, "use_slicing"): pytest.skip(f"Skipping test as {self.model_class.__name__} doesn't support tiling.") inputs_dict.update({"return_dict": False}) _ = inputs_dict.pop("generator", None) accepts_generator = self._accepts_generator(model) if accepts_generator: inputs_dict["generator"] = torch.manual_seed(0) torch.manual_seed(0) output_without_slicing = model(**inputs_dict)[0] # Mochi-1 if isinstance(output_without_slicing, DecoderOutput): output_without_slicing = output_without_slicing.sample torch.manual_seed(0) model.enable_slicing() if accepts_generator: inputs_dict["generator"] = torch.manual_seed(0) output_with_slicing = model(**inputs_dict)[0] if isinstance(output_with_slicing, DecoderOutput): output_with_slicing = output_with_slicing.sample assert ( output_without_slicing.detach().cpu().numpy() - output_with_slicing.detach().cpu().numpy() ).max() < 0.5, "VAE slicing should not affect the inference results" torch.manual_seed(0) model.disable_slicing() if accepts_generator: inputs_dict["generator"] = torch.manual_seed(0) output_without_slicing_2 = model(**inputs_dict)[0] if isinstance(output_without_slicing_2, DecoderOutput): output_without_slicing_2 = output_without_slicing_2.sample assert np.allclose( output_without_slicing.detach().cpu().numpy().all(), output_without_slicing_2.detach().cpu().numpy().all(), ), "Without slicing outputs should match with the outputs when slicing is manually disabled."
{ "repo_id": "huggingface/diffusers", "file_path": "tests/models/autoencoders/testing_utils.py", "license": "Apache License 2.0", "lines": 115, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
test
huggingface/diffusers:src/diffusers/modular_pipelines/flux/inputs.py
# Copyright 2025 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import torch from ...pipelines import FluxPipeline from ...utils import logging from ..modular_pipeline import ModularPipelineBlocks, PipelineState from ..modular_pipeline_utils import InputParam, OutputParam # TODO: consider making these common utilities for modular if they are not pipeline-specific. from ..qwenimage.inputs import calculate_dimension_from_latents, repeat_tensor_to_batch_size from .modular_pipeline import FluxModularPipeline logger = logging.get_logger(__name__) class FluxTextInputStep(ModularPipelineBlocks): model_name = "flux" @property def description(self) -> str: return ( "Text input processing step that standardizes text embeddings for the pipeline.\n" "This step:\n" " 1. Determines `batch_size` and `dtype` based on `prompt_embeds`\n" " 2. Ensures all text embeddings have consistent batch sizes (batch_size * num_images_per_prompt)" ) @property def inputs(self) -> list[InputParam]: return [ InputParam("num_images_per_prompt", default=1), InputParam( "prompt_embeds", required=True, kwargs_type="denoiser_input_fields", type_hint=torch.Tensor, description="Pre-generated text embeddings. Can be generated from text_encoder step.", ), InputParam( "pooled_prompt_embeds", kwargs_type="denoiser_input_fields", type_hint=torch.Tensor, description="Pre-generated pooled text embeddings. Can be generated from text_encoder step.", ), # TODO: support negative embeddings? ] @property def intermediate_outputs(self) -> list[str]: return [ OutputParam( "batch_size", type_hint=int, description="Number of prompts, the final batch size of model inputs should be batch_size * num_images_per_prompt", ), OutputParam( "dtype", type_hint=torch.dtype, description="Data type of model tensor inputs (determined by `prompt_embeds`)", ), OutputParam( "prompt_embeds", type_hint=torch.Tensor, kwargs_type="denoiser_input_fields", description="text embeddings used to guide the image generation", ), OutputParam( "pooled_prompt_embeds", type_hint=torch.Tensor, kwargs_type="denoiser_input_fields", description="pooled text embeddings used to guide the image generation", ), # TODO: support negative embeddings? ] def check_inputs(self, components, block_state): if block_state.prompt_embeds is not None and block_state.pooled_prompt_embeds is not None: if block_state.prompt_embeds.shape[0] != block_state.pooled_prompt_embeds.shape[0]: raise ValueError( "`prompt_embeds` and `pooled_prompt_embeds` must have the same batch size when passed directly, but" f" got: `prompt_embeds` {block_state.prompt_embeds.shape} != `pooled_prompt_embeds`" f" {block_state.pooled_prompt_embeds.shape}." ) @torch.no_grad() def __call__(self, components: FluxModularPipeline, state: PipelineState) -> PipelineState: # TODO: consider adding negative embeddings? block_state = self.get_block_state(state) self.check_inputs(components, block_state) block_state.batch_size = block_state.prompt_embeds.shape[0] block_state.dtype = block_state.prompt_embeds.dtype _, seq_len, _ = block_state.prompt_embeds.shape block_state.prompt_embeds = block_state.prompt_embeds.repeat(1, block_state.num_images_per_prompt, 1) block_state.prompt_embeds = block_state.prompt_embeds.view( block_state.batch_size * block_state.num_images_per_prompt, seq_len, -1 ) pooled_prompt_embeds = block_state.pooled_prompt_embeds.repeat(1, block_state.num_images_per_prompt) block_state.pooled_prompt_embeds = pooled_prompt_embeds.view( block_state.batch_size * block_state.num_images_per_prompt, -1 ) self.set_block_state(state, block_state) return components, state # Adapted from `QwenImageAdditionalInputsStep` class FluxAdditionalInputsStep(ModularPipelineBlocks): model_name = "flux" def __init__( self, image_latent_inputs: list[str] = ["image_latents"], additional_batch_inputs: list[str] = [], ): if not isinstance(image_latent_inputs, list): image_latent_inputs = [image_latent_inputs] if not isinstance(additional_batch_inputs, list): additional_batch_inputs = [additional_batch_inputs] self._image_latent_inputs = image_latent_inputs self._additional_batch_inputs = additional_batch_inputs super().__init__() @property def description(self) -> str: # Functionality section summary_section = ( "Input processing step that:\n" " 1. For image latent inputs: Updates height/width if None, patchifies latents, and expands batch size\n" " 2. For additional batch inputs: Expands batch dimensions to match final batch size" ) # Inputs info inputs_info = "" if self._image_latent_inputs or self._additional_batch_inputs: inputs_info = "\n\nConfigured inputs:" if self._image_latent_inputs: inputs_info += f"\n - Image latent inputs: {self._image_latent_inputs}" if self._additional_batch_inputs: inputs_info += f"\n - Additional batch inputs: {self._additional_batch_inputs}" # Placement guidance placement_section = "\n\nThis block should be placed after the encoder steps and the text input step." return summary_section + inputs_info + placement_section @property def inputs(self) -> list[InputParam]: inputs = [ InputParam(name="num_images_per_prompt", default=1), InputParam(name="batch_size", required=True), InputParam(name="height"), InputParam(name="width"), ] # Add image latent inputs for image_latent_input_name in self._image_latent_inputs: inputs.append(InputParam(name=image_latent_input_name)) # Add additional batch inputs for input_name in self._additional_batch_inputs: inputs.append(InputParam(name=input_name)) return inputs @property def intermediate_outputs(self) -> list[OutputParam]: return [ OutputParam(name="image_height", type_hint=int, description="The height of the image latents"), OutputParam(name="image_width", type_hint=int, description="The width of the image latents"), ] def __call__(self, components: FluxModularPipeline, state: PipelineState) -> PipelineState: block_state = self.get_block_state(state) # Process image latent inputs (height/width calculation, patchify, and batch expansion) for image_latent_input_name in self._image_latent_inputs: image_latent_tensor = getattr(block_state, image_latent_input_name) if image_latent_tensor is None: continue # 1. Calculate height/width from latents height, width = calculate_dimension_from_latents(image_latent_tensor, components.vae_scale_factor) block_state.height = block_state.height or height block_state.width = block_state.width or width if not hasattr(block_state, "image_height"): block_state.image_height = height if not hasattr(block_state, "image_width"): block_state.image_width = width # 2. Patchify the image latent tensor # TODO: Implement patchifier for Flux. latent_height, latent_width = image_latent_tensor.shape[2:] image_latent_tensor = FluxPipeline._pack_latents( image_latent_tensor, block_state.batch_size, image_latent_tensor.shape[1], latent_height, latent_width ) # 3. Expand batch size image_latent_tensor = repeat_tensor_to_batch_size( input_name=image_latent_input_name, input_tensor=image_latent_tensor, num_images_per_prompt=block_state.num_images_per_prompt, batch_size=block_state.batch_size, ) setattr(block_state, image_latent_input_name, image_latent_tensor) # Process additional batch inputs (only batch expansion) for input_name in self._additional_batch_inputs: input_tensor = getattr(block_state, input_name) if input_tensor is None: continue # Only expand batch size input_tensor = repeat_tensor_to_batch_size( input_name=input_name, input_tensor=input_tensor, num_images_per_prompt=block_state.num_images_per_prompt, batch_size=block_state.batch_size, ) setattr(block_state, input_name, input_tensor) self.set_block_state(state, block_state) return components, state class FluxKontextAdditionalInputsStep(FluxAdditionalInputsStep): model_name = "flux-kontext" def __call__(self, components: FluxModularPipeline, state: PipelineState) -> PipelineState: block_state = self.get_block_state(state) # Process image latent inputs (height/width calculation, patchify, and batch expansion) for image_latent_input_name in self._image_latent_inputs: image_latent_tensor = getattr(block_state, image_latent_input_name) if image_latent_tensor is None: continue # 1. Calculate height/width from latents # Unlike the `FluxAdditionalInputsStep`, we don't overwrite the `block.height` and `block.width` height, width = calculate_dimension_from_latents(image_latent_tensor, components.vae_scale_factor) if not hasattr(block_state, "image_height"): block_state.image_height = height if not hasattr(block_state, "image_width"): block_state.image_width = width # 2. Patchify the image latent tensor # TODO: Implement patchifier for Flux. latent_height, latent_width = image_latent_tensor.shape[2:] image_latent_tensor = FluxPipeline._pack_latents( image_latent_tensor, block_state.batch_size, image_latent_tensor.shape[1], latent_height, latent_width ) # 3. Expand batch size image_latent_tensor = repeat_tensor_to_batch_size( input_name=image_latent_input_name, input_tensor=image_latent_tensor, num_images_per_prompt=block_state.num_images_per_prompt, batch_size=block_state.batch_size, ) setattr(block_state, image_latent_input_name, image_latent_tensor) # Process additional batch inputs (only batch expansion) for input_name in self._additional_batch_inputs: input_tensor = getattr(block_state, input_name) if input_tensor is None: continue # Only expand batch size input_tensor = repeat_tensor_to_batch_size( input_name=input_name, input_tensor=input_tensor, num_images_per_prompt=block_state.num_images_per_prompt, batch_size=block_state.batch_size, ) setattr(block_state, input_name, input_tensor) self.set_block_state(state, block_state) return components, state class FluxKontextSetResolutionStep(ModularPipelineBlocks): model_name = "flux-kontext" @property def description(self): return ( "Determines the height and width to be used during the subsequent computations.\n" "It should always be placed _before_ the latent preparation step." ) @property def inputs(self) -> list[InputParam]: inputs = [ InputParam(name="height"), InputParam(name="width"), InputParam(name="max_area", type_hint=int, default=1024**2), ] return inputs @property def intermediate_outputs(self) -> list[OutputParam]: return [ OutputParam(name="height", type_hint=int, description="The height of the initial noisy latents"), OutputParam(name="width", type_hint=int, description="The width of the initial noisy latents"), ] @staticmethod def check_inputs(height, width, vae_scale_factor): if height is not None and height % (vae_scale_factor * 2) != 0: raise ValueError(f"Height must be divisible by {vae_scale_factor * 2} but is {height}") if width is not None and width % (vae_scale_factor * 2) != 0: raise ValueError(f"Width must be divisible by {vae_scale_factor * 2} but is {width}") def __call__(self, components: FluxModularPipeline, state: PipelineState) -> PipelineState: block_state = self.get_block_state(state) height = block_state.height or components.default_height width = block_state.width or components.default_width self.check_inputs(height, width, components.vae_scale_factor) original_height, original_width = height, width max_area = block_state.max_area aspect_ratio = width / height width = round((max_area * aspect_ratio) ** 0.5) height = round((max_area / aspect_ratio) ** 0.5) multiple_of = components.vae_scale_factor * 2 width = width // multiple_of * multiple_of height = height // multiple_of * multiple_of if height != original_height or width != original_width: logger.warning( f"Generation `height` and `width` have been adjusted to {height} and {width} to fit the model requirements." ) block_state.height = height block_state.width = width self.set_block_state(state, block_state) return components, state
{ "repo_id": "huggingface/diffusers", "file_path": "src/diffusers/modular_pipelines/flux/inputs.py", "license": "Apache License 2.0", "lines": 297, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
huggingface/diffusers:src/diffusers/modular_pipelines/mellon_node_utils.py
import copy import json import logging import os # Simple typed wrapper for parameter overrides from dataclasses import asdict, dataclass from typing import Any from huggingface_hub import create_repo, hf_hub_download, upload_file from huggingface_hub.utils import ( EntryNotFoundError, HfHubHTTPError, RepositoryNotFoundError, RevisionNotFoundError, ) from ..utils import HUGGINGFACE_CO_RESOLVE_ENDPOINT from .modular_pipeline_utils import InputParam, OutputParam logger = logging.getLogger(__name__) def _name_to_label(name: str) -> str: """Convert snake_case name to Title Case label.""" return name.replace("_", " ").title() # Template definitions for standard diffuser pipeline parameters MELLON_PARAM_TEMPLATES = { # Image I/O "image": {"label": "Image", "type": "image", "display": "input", "required_block_params": ["image"]}, "images": {"label": "Images", "type": "image", "display": "output", "required_block_params": ["images"]}, "control_image": { "label": "Control Image", "type": "image", "display": "input", "required_block_params": ["control_image"], }, # Latents "latents": {"label": "Latents", "type": "latents", "display": "input", "required_block_params": ["latents"]}, "image_latents": { "label": "Image Latents", "type": "latents", "display": "input", "required_block_params": ["image_latents"], }, "first_frame_latents": { "label": "First Frame Latents", "type": "latents", "display": "input", "required_block_params": ["first_frame_latents"], }, "latents_preview": {"label": "Latents Preview", "type": "latent", "display": "output"}, # Image Latents with Strength "image_latents_with_strength": { "name": "image_latents", # name is not same as template key "label": "Image Latents", "type": "latents", "display": "input", "onChange": {"false": ["height", "width"], "true": ["strength"]}, "required_block_params": ["image_latents", "strength"], }, # Embeddings "embeddings": {"label": "Text Embeddings", "type": "embeddings", "display": "output"}, "image_embeds": { "label": "Image Embeddings", "type": "image_embeds", "display": "output", "required_block_params": ["image_embeds"], }, # Text inputs "prompt": { "label": "Prompt", "type": "string", "display": "textarea", "default": "", "required_block_params": ["prompt"], }, "negative_prompt": { "label": "Negative Prompt", "type": "string", "display": "textarea", "default": "", "required_block_params": ["negative_prompt"], }, # Numeric params "guidance_scale": { "label": "Guidance Scale", "type": "float", "display": "slider", "default": 5.0, "min": 1.0, "max": 30.0, "step": 0.1, }, "strength": { "label": "Strength", "type": "float", "default": 0.5, "min": 0.0, "max": 1.0, "step": 0.01, "required_block_params": ["strength"], }, "height": { "label": "Height", "type": "int", "default": 1024, "min": 64, "step": 8, "required_block_params": ["height"], }, "width": { "label": "Width", "type": "int", "default": 1024, "min": 64, "step": 8, "required_block_params": ["width"], }, "seed": { "label": "Seed", "type": "int", "default": 0, "min": 0, "max": 4294967295, "display": "random", "required_block_params": ["generator"], }, "num_inference_steps": { "label": "Steps", "type": "int", "default": 25, "min": 1, "max": 100, "display": "slider", "required_block_params": ["num_inference_steps"], }, "num_frames": { "label": "Frames", "type": "int", "default": 81, "min": 1, "max": 480, "display": "slider", "required_block_params": ["num_frames"], }, "layers": { "label": "Layers", "type": "int", "default": 4, "min": 1, "max": 10, "display": "slider", "required_block_params": ["layers"], }, "output_type": { "label": "Output Type", "type": "dropdown", "default": "np", "options": ["np", "pil", "pt"], }, # ControlNet "controlnet_conditioning_scale": { "label": "Controlnet Conditioning Scale", "type": "float", "default": 0.5, "min": 0.0, "max": 1.0, "step": 0.01, "required_block_params": ["controlnet_conditioning_scale"], }, "control_guidance_start": { "label": "Control Guidance Start", "type": "float", "default": 0.0, "min": 0.0, "max": 1.0, "step": 0.01, "required_block_params": ["control_guidance_start"], }, "control_guidance_end": { "label": "Control Guidance End", "type": "float", "default": 1.0, "min": 0.0, "max": 1.0, "step": 0.01, "required_block_params": ["control_guidance_end"], }, # Video "videos": {"label": "Videos", "type": "video", "display": "output", "required_block_params": ["videos"]}, # Models "vae": {"label": "VAE", "type": "diffusers_auto_model", "display": "input", "required_block_params": ["vae"]}, "image_encoder": { "label": "Image Encoder", "type": "diffusers_auto_model", "display": "input", "required_block_params": ["image_encoder"], }, "unet": {"label": "Denoise Model", "type": "diffusers_auto_model", "display": "input"}, "scheduler": {"label": "Scheduler", "type": "diffusers_auto_model", "display": "input"}, "controlnet": { "label": "ControlNet Model", "type": "diffusers_auto_model", "display": "input", "required_block_params": ["controlnet"], }, "text_encoders": { "label": "Text Encoders", "type": "diffusers_auto_models", "display": "input", "required_block_params": ["text_encoder"], }, # Bundles/Custom "controlnet_bundle": { "label": "ControlNet", "type": "custom_controlnet", "display": "input", "required_block_params": "controlnet_image", }, "ip_adapter": {"label": "IP Adapter", "type": "custom_ip_adapter", "display": "input"}, "guider": { "label": "Guider", "type": "custom_guider", "display": "input", "onChange": {False: ["guidance_scale"], True: []}, }, "doc": {"label": "Doc", "type": "string", "display": "output"}, } class MellonParamMeta(type): """Metaclass that enables MellonParam.template_name(**overrides) syntax.""" def __getattr__(cls, name: str): if name in MELLON_PARAM_TEMPLATES: def factory(default=None, **overrides): template = MELLON_PARAM_TEMPLATES[name] # Use template's name if specified, otherwise use the key params = {"name": template.get("name", name), **template, **overrides} if default is not None: params["default"] = default return cls(**params) return factory raise AttributeError(f"type object 'MellonParam' has no attribute '{name}'") @dataclass(frozen=True) class MellonParam(metaclass=MellonParamMeta): """ Parameter definition for Mellon nodes. Usage: ```python # From template (standard diffuser params) MellonParam.seed() MellonParam.prompt(default="a cat") MellonParam.latents(display="output") # Generic inputs (for custom blocks) MellonParam.Input.slider("my_scale", default=1.0, min=0.0, max=2.0) MellonParam.Input.dropdown("mode", options=["fast", "slow"]) # Generic outputs MellonParam.Output.image("result_images") # Fully custom MellonParam(name="custom", label="Custom", type="float", default=0.5) ``` """ name: str label: str type: str display: str | None = None default: Any = None min: float | None = None max: float | None = None step: float | None = None options: Any = None value: Any = None fieldOptions: dict[str, Any] | None = None onChange: Any = None onSignal: Any = None required_block_params: str | list[str] | None = None def to_dict(self) -> dict[str, Any]: """Convert to dict for Mellon schema, excluding None values and internal fields.""" data = asdict(self) return {k: v for k, v in data.items() if v is not None and k not in ("name", "required_block_params")} # ========================================================================= # Input: Generic input parameter factories (for custom blocks) # ========================================================================= class Input: """input UI elements for custom blocks.""" @classmethod def image(cls, name: str) -> "MellonParam": """image input.""" return MellonParam(name=name, label=_name_to_label(name), type="image", display="input") @classmethod def textbox(cls, name: str, default: str = "") -> "MellonParam": """text input as textarea.""" return MellonParam( name=name, label=_name_to_label(name), type="string", display="textarea", default=default ) @classmethod def dropdown(cls, name: str, options: list[str] = None, default: str = None) -> "MellonParam": """dropdown selection.""" if options and not default: default = options[0] if not default: default = "" if not options: options = [default] return MellonParam(name=name, label=_name_to_label(name), type="string", options=options, value=default) @classmethod def slider( cls, name: str, default: float = 0, min: float = None, max: float = None, step: float = None ) -> "MellonParam": """slider input.""" is_float = isinstance(default, float) or (step is not None and isinstance(step, float)) param_type = "float" if is_float else "int" if min is None: min = default if max is None: max = default if step is None: step = 0.01 if is_float else 1 return MellonParam( name=name, label=_name_to_label(name), type=param_type, display="slider", default=default, min=min, max=max, step=step, ) @classmethod def number( cls, name: str, default: float = 0, min: float = None, max: float = None, step: float = None ) -> "MellonParam": """number input (no slider).""" is_float = isinstance(default, float) or (step is not None and isinstance(step, float)) param_type = "float" if is_float else "int" return MellonParam( name=name, label=_name_to_label(name), type=param_type, default=default, min=min, max=max, step=step ) @classmethod def seed(cls, name: str = "seed", default: int = 0) -> "MellonParam": """seed input with randomize button.""" return MellonParam( name=name, label=_name_to_label(name), type="int", display="random", default=default, min=0, max=4294967295, ) @classmethod def checkbox(cls, name: str, default: bool = False) -> "MellonParam": """boolean checkbox.""" return MellonParam(name=name, label=_name_to_label(name), type="boolean", value=default) @classmethod def custom_type(cls, name: str, type: str) -> "MellonParam": """custom type input for node connections.""" return MellonParam(name=name, label=_name_to_label(name), type=type, display="input") @classmethod def model(cls, name: str) -> "MellonParam": """model input for diffusers components.""" return MellonParam(name=name, label=_name_to_label(name), type="diffusers_auto_model", display="input") # ========================================================================= # Output: Generic output parameter factories (for custom blocks) # ========================================================================= class Output: """output UI elements for custom blocks.""" @classmethod def image(cls, name: str) -> "MellonParam": """image output.""" return MellonParam(name=name, label=_name_to_label(name), type="image", display="output") @classmethod def video(cls, name: str) -> "MellonParam": """video output.""" return MellonParam(name=name, label=_name_to_label(name), type="video", display="output") @classmethod def text(cls, name: str) -> "MellonParam": """text output.""" return MellonParam(name=name, label=_name_to_label(name), type="string", display="output") @classmethod def custom_type(cls, name: str, type: str) -> "MellonParam": """custom type output for node connections.""" return MellonParam(name=name, label=_name_to_label(name), type=type, display="output") @classmethod def model(cls, name: str) -> "MellonParam": """model output for diffusers components.""" return MellonParam(name=name, label=_name_to_label(name), type="diffusers_auto_model", display="output") def input_param_to_mellon_param(input_param: "InputParam") -> MellonParam: """ Convert an InputParam to a MellonParam using metadata. Args: input_param: An InputParam with optional metadata containing either: - {"mellon": "<type>"} for simple types (image, textbox, slider, etc.) - {"mellon": MellonParam(...)} for full control over UI configuration Returns: MellonParam instance """ name = input_param.name metadata = input_param.metadata mellon_value = metadata.get("mellon") if metadata else None default = input_param.default # If it's already a MellonParam, return it directly if isinstance(mellon_value, MellonParam): return mellon_value mellon_type = mellon_value if mellon_type == "image": return MellonParam.Input.image(name) elif mellon_type == "textbox": return MellonParam.Input.textbox(name, default=default or "") elif mellon_type == "dropdown": return MellonParam.Input.dropdown(name, default=default or "") elif mellon_type == "slider": return MellonParam.Input.slider(name, default=default or 0) elif mellon_type == "number": return MellonParam.Input.number(name, default=default or 0) elif mellon_type == "seed": return MellonParam.Input.seed(name, default=default or 0) elif mellon_type == "checkbox": return MellonParam.Input.checkbox(name, default=default or False) elif mellon_type == "model": return MellonParam.Input.model(name) else: # None or unknown -> custom return MellonParam.Input.custom_type(name, type="custom") def output_param_to_mellon_param(output_param: "OutputParam") -> MellonParam: """ Convert an OutputParam to a MellonParam using metadata. Args: output_param: An OutputParam with optional metadata={"mellon": "<type>"} where type is one of: image, video, text, model. If metadata is None or unknown, maps to "custom". Returns: MellonParam instance """ name = output_param.name metadata = output_param.metadata mellon_type = metadata.get("mellon") if metadata else None if mellon_type == "image": return MellonParam.Output.image(name) elif mellon_type == "video": return MellonParam.Output.video(name) elif mellon_type == "text": return MellonParam.Output.text(name) elif mellon_type == "model": return MellonParam.Output.model(name) else: # None or unknown -> custom return MellonParam.Output.custom_type(name, type="custom") DEFAULT_NODE_SPECS = { "controlnet": None, "denoise": { "inputs": [ MellonParam.embeddings(display="input"), MellonParam.width(), MellonParam.height(), MellonParam.seed(), MellonParam.num_inference_steps(), MellonParam.num_frames(), MellonParam.guidance_scale(), MellonParam.strength(), MellonParam.image_latents_with_strength(), MellonParam.image_latents(), MellonParam.first_frame_latents(), MellonParam.controlnet_bundle(display="input"), ], "model_inputs": [ MellonParam.unet(), MellonParam.guider(), MellonParam.scheduler(), ], "outputs": [ MellonParam.latents(display="output"), MellonParam.latents_preview(), MellonParam.doc(), ], "required_inputs": ["embeddings"], "required_model_inputs": ["unet", "scheduler"], "block_name": "denoise", }, "vae_encoder": { "inputs": [ MellonParam.image(), ], "model_inputs": [ MellonParam.vae(), ], "outputs": [ MellonParam.image_latents(display="output"), MellonParam.doc(), ], "required_inputs": ["image"], "required_model_inputs": ["vae"], "block_name": "vae_encoder", }, "text_encoder": { "inputs": [ MellonParam.prompt(), MellonParam.negative_prompt(), ], "model_inputs": [ MellonParam.text_encoders(), ], "outputs": [ MellonParam.embeddings(display="output"), MellonParam.doc(), ], "required_inputs": ["prompt"], "required_model_inputs": ["text_encoders"], "block_name": "text_encoder", }, "decoder": { "inputs": [ MellonParam.latents(display="input"), ], "model_inputs": [ MellonParam.vae(), ], "outputs": [ MellonParam.images(), MellonParam.videos(), MellonParam.doc(), ], "required_inputs": ["latents"], "required_model_inputs": ["vae"], "block_name": "decode", }, } def mark_required(label: str, marker: str = " *") -> str: """Add required marker to label if not already present.""" if label.endswith(marker): return label return f"{label}{marker}" def node_spec_to_mellon_dict(node_spec: dict[str, Any], node_type: str) -> dict[str, Any]: """ Convert a node spec dict into Mellon format. A node spec is how we define a Mellon diffusers node in code. This function converts it into the `params` map format that Mellon UI expects. The `params` map is a dict where keys are parameter names and values are UI configuration: ```python {"seed": {"label": "Seed", "type": "int", "default": 0}} ``` For Modular Mellon nodes, we need to distinguish: - `inputs`: Pipeline inputs (e.g., seed, prompt, image) - `model_inputs`: Model components (e.g., unet, vae, scheduler) - `outputs`: Node outputs (e.g., latents, images) The node spec also includes: - `required_inputs` / `required_model_inputs`: Which params are required (marked with *) - `block_name`: The modular pipeline block this node corresponds to on backend We provide factory methods for common parameters (e.g., `MellonParam.seed()`, `MellonParam.unet()`) so you don't have to manually specify all the UI configuration. Args: node_spec: Dict with `inputs`, `model_inputs`, `outputs` (lists of MellonParam), plus `required_inputs`, `required_model_inputs`, `block_name`. node_type: The node type string (e.g., "denoise", "controlnet") Returns: Dict with: - `params`: Flat dict of all params in Mellon UI format - `input_names`: List of input parameter names - `model_input_names`: List of model input parameter names - `output_names`: List of output parameter names - `block_name`: The backend block name - `node_type`: The node type Example: ```python node_spec = { "inputs": [MellonParam.seed(), MellonParam.prompt()], "model_inputs": [MellonParam.unet()], "outputs": [MellonParam.latents(display="output")], "required_inputs": ["prompt"], "required_model_inputs": ["unet"], "block_name": "denoise", } result = node_spec_to_mellon_dict(node_spec, "denoise") # Returns: # { # "params": { # "seed": {"label": "Seed", "type": "int", "default": 0}, # "prompt": {"label": "Prompt *", "type": "string", "default": ""}, # * marks required # "unet": {"label": "Denoise Model *", "type": "diffusers_auto_model", "display": "input"}, # "latents": {"label": "Latents", "type": "latents", "display": "output"}, # }, # "input_names": ["seed", "prompt"], # "model_input_names": ["unet"], # "output_names": ["latents"], # "block_name": "denoise", # "node_type": "denoise", # } ``` """ params = {} input_names = [] model_input_names = [] output_names = [] required_inputs = node_spec.get("required_inputs", []) required_model_inputs = node_spec.get("required_model_inputs", []) # Process inputs for p in node_spec.get("inputs", []): param_dict = p.to_dict() if p.name in required_inputs: param_dict["label"] = mark_required(param_dict["label"]) params[p.name] = param_dict input_names.append(p.name) # Process model_inputs for p in node_spec.get("model_inputs", []): param_dict = p.to_dict() if p.name in required_model_inputs: param_dict["label"] = mark_required(param_dict["label"]) params[p.name] = param_dict model_input_names.append(p.name) # Process outputs: add a prefix to the output name if it already exists as an input for p in node_spec.get("outputs", []): if p.name in input_names: # rename to out_<name> output_name = f"out_{p.name}" else: output_name = p.name params[output_name] = p.to_dict() output_names.append(output_name) return { "params": params, "input_names": input_names, "model_input_names": model_input_names, "output_names": output_names, "block_name": node_spec.get("block_name"), "node_type": node_type, } class MellonPipelineConfig: """ Configuration for an entire Mellon pipeline containing multiple nodes. Accepts node specs as dicts with inputs/model_inputs/outputs lists of MellonParam, converts them to Mellon-ready format, and handles save/load to Hub. Example: ```python config = MellonPipelineConfig( node_specs={ "denoise": { "inputs": [MellonParam.seed(), MellonParam.prompt()], "model_inputs": [MellonParam.unet()], "outputs": [MellonParam.latents(display="output")], "required_inputs": ["prompt"], "required_model_inputs": ["unet"], "block_name": "denoise", }, "decoder": { "inputs": [MellonParam.latents(display="input")], "outputs": [MellonParam.images()], "block_name": "decoder", }, }, label="My Pipeline", default_repo="user/my-pipeline", default_dtype="float16", ) # Access Mellon format dict denoise = config.node_params["denoise"] input_names = denoise["input_names"] params = denoise["params"] # Save to Hub config.save("./my_config", push_to_hub=True, repo_id="user/my-pipeline") # Load from Hub loaded = MellonPipelineConfig.load("user/my-pipeline") ``` """ config_name = "mellon_pipeline_config.json" def __init__( self, node_specs: dict[str, dict[str, Any] | None], label: str = "", default_repo: str = "", default_dtype: str = "", ): """ Args: node_specs: Dict mapping node_type to node spec or None. Node spec has: inputs, model_inputs, outputs, required_inputs, required_model_inputs, block_name (all optional) label: Human-readable label for the pipeline default_repo: Default HuggingFace repo for this pipeline default_dtype: Default dtype (e.g., "float16", "bfloat16") """ # Convert all node specs to Mellon format immediately self.node_specs = node_specs self.label = label self.default_repo = default_repo self.default_dtype = default_dtype @property def node_params(self) -> dict[str, Any]: """Lazily compute node_params from node_specs.""" if self.node_specs is None: return self._node_params params = {} for node_type, spec in self.node_specs.items(): if spec is None: params[node_type] = None else: params[node_type] = node_spec_to_mellon_dict(spec, node_type) return params def __repr__(self) -> str: lines = [ f"MellonPipelineConfig(label={self.label!r}, default_repo={self.default_repo!r}, default_dtype={self.default_dtype!r})" ] for node_type, spec in self.node_specs.items(): if spec is None: lines.append(f" {node_type}: None") else: inputs = [p.name for p in spec.get("inputs", [])] model_inputs = [p.name for p in spec.get("model_inputs", [])] outputs = [p.name for p in spec.get("outputs", [])] lines.append(f" {node_type}:") lines.append(f" inputs: {inputs}") lines.append(f" model_inputs: {model_inputs}") lines.append(f" outputs: {outputs}") return "\n".join(lines) def to_dict(self) -> dict[str, Any]: """Convert to a JSON-serializable dictionary.""" return { "label": self.label, "default_repo": self.default_repo, "default_dtype": self.default_dtype, "node_params": self.node_params, } @classmethod def from_dict(cls, data: dict[str, Any]) -> "MellonPipelineConfig": """ Create from a dictionary (loaded from JSON). Note: The mellon_params are already in Mellon format when loading from JSON. """ instance = cls.__new__(cls) instance.node_specs = None instance._node_params = data.get("node_params", {}) instance.label = data.get("label", "") instance.default_repo = data.get("default_repo", "") instance.default_dtype = data.get("default_dtype", "") return instance def to_json_string(self) -> str: """Serialize to JSON string.""" return json.dumps(self.to_dict(), indent=2, sort_keys=False) + "\n" def to_json_file(self, json_file_path: str | os.PathLike): """Save to a JSON file.""" with open(json_file_path, "w", encoding="utf-8") as writer: writer.write(self.to_json_string()) @classmethod def from_json_file(cls, json_file_path: str | os.PathLike) -> "MellonPipelineConfig": """Load from a JSON file.""" with open(json_file_path, "r", encoding="utf-8") as reader: data = json.load(reader) return cls.from_dict(data) def save(self, save_directory: str | os.PathLike, push_to_hub: bool = False, **kwargs): """Save the mellon pipeline config to a directory.""" if os.path.isfile(save_directory): raise AssertionError(f"Provided path ({save_directory}) should be a directory, not a file") os.makedirs(save_directory, exist_ok=True) output_path = os.path.join(save_directory, self.config_name) self.to_json_file(output_path) logger.info(f"Pipeline config saved to {output_path}") if push_to_hub: commit_message = kwargs.pop("commit_message", None) private = kwargs.pop("private", None) create_pr = kwargs.pop("create_pr", False) token = kwargs.pop("token", None) repo_id = kwargs.pop("repo_id", save_directory.split(os.path.sep)[-1]) repo_id = create_repo(repo_id, exist_ok=True, private=private, token=token).repo_id upload_file( path_or_fileobj=output_path, path_in_repo=self.config_name, repo_id=repo_id, token=token, commit_message=commit_message or "Upload MellonPipelineConfig", create_pr=create_pr, ) logger.info(f"Pipeline config pushed to hub: {repo_id}") @classmethod def load( cls, pretrained_model_name_or_path: str | os.PathLike, **kwargs, ) -> "MellonPipelineConfig": """Load a pipeline config from a local path or Hugging Face Hub.""" cache_dir = kwargs.pop("cache_dir", None) local_dir = kwargs.pop("local_dir", None) local_dir_use_symlinks = kwargs.pop("local_dir_use_symlinks", "auto") force_download = kwargs.pop("force_download", False) proxies = kwargs.pop("proxies", None) token = kwargs.pop("token", None) local_files_only = kwargs.pop("local_files_only", False) revision = kwargs.pop("revision", None) subfolder = kwargs.pop("subfolder", None) pretrained_model_name_or_path = str(pretrained_model_name_or_path) if os.path.isfile(pretrained_model_name_or_path): config_file = pretrained_model_name_or_path elif os.path.isdir(pretrained_model_name_or_path): config_file = os.path.join(pretrained_model_name_or_path, cls.config_name) if not os.path.isfile(config_file): raise EnvironmentError(f"No file named {cls.config_name} found in {pretrained_model_name_or_path}") else: try: config_file = hf_hub_download( pretrained_model_name_or_path, filename=cls.config_name, cache_dir=cache_dir, force_download=force_download, proxies=proxies, local_files_only=local_files_only, token=token, revision=revision, subfolder=subfolder, local_dir=local_dir, local_dir_use_symlinks=local_dir_use_symlinks, ) except RepositoryNotFoundError: raise EnvironmentError( f"{pretrained_model_name_or_path} is not a local folder and is not a valid model identifier" " listed on 'https://huggingface.co/models'\nIf this is a private repository, make sure to pass a" " token having permission to this repo with `token` or log in with `hf auth login`." ) except RevisionNotFoundError: raise EnvironmentError( f"{revision} is not a valid git identifier (branch name, tag name or commit id) that exists for" " this model name. Check the model page at" f" 'https://huggingface.co/{pretrained_model_name_or_path}' for available revisions." ) except EntryNotFoundError: raise EnvironmentError( f"{pretrained_model_name_or_path} does not appear to have a file named {cls.config_name}." ) except HfHubHTTPError as err: raise EnvironmentError( "There was a specific connection error when trying to load" f" {pretrained_model_name_or_path}:\n{err}" ) except ValueError: raise EnvironmentError( f"We couldn't connect to '{HUGGINGFACE_CO_RESOLVE_ENDPOINT}' to load this model, couldn't find it" f" in the cached files and it looks like {pretrained_model_name_or_path} is not the path to a" f" directory containing a {cls.config_name} file.\nCheckout your internet connection or see how to" " run the library in offline mode at" " 'https://huggingface.co/docs/diffusers/installation#offline-mode'." ) except EnvironmentError: raise EnvironmentError( f"Can't load config for '{pretrained_model_name_or_path}'. If you were trying to load it from " "'https://huggingface.co/models', make sure you don't have a local directory with the same name. " f"Otherwise, make sure '{pretrained_model_name_or_path}' is the correct path to a directory " f"containing a {cls.config_name} file" ) try: return cls.from_json_file(config_file) except (json.JSONDecodeError, UnicodeDecodeError): raise EnvironmentError(f"The config file at '{config_file}' is not a valid JSON file.") @classmethod def from_blocks( cls, blocks, template: dict[str, dict[str, Any]] | None = None, label: str = "", default_repo: str = "", default_dtype: str = "bfloat16", ) -> "MellonPipelineConfig": """ Create MellonPipelineConfig by matching template against actual pipeline blocks. """ if template is None: template = DEFAULT_NODE_SPECS sub_block_map = dict(blocks.sub_blocks) def filter_spec_for_block(template_spec: dict[str, Any], block) -> dict[str, Any] | None: """Filter template spec params based on what the block actually supports.""" block_input_names = set(block.input_names) block_output_names = set(block.intermediate_output_names) block_component_names = set(block.component_names) filtered_inputs = [ p for p in template_spec.get("inputs", []) if p.required_block_params is None or all(name in block_input_names for name in p.required_block_params) ] filtered_model_inputs = [ p for p in template_spec.get("model_inputs", []) if p.required_block_params is None or all(name in block_component_names for name in p.required_block_params) ] filtered_outputs = [ p for p in template_spec.get("outputs", []) if p.required_block_params is None or all(name in block_output_names for name in p.required_block_params) ] filtered_input_names = {p.name for p in filtered_inputs} filtered_model_input_names = {p.name for p in filtered_model_inputs} filtered_required_inputs = [ r for r in template_spec.get("required_inputs", []) if r in filtered_input_names ] filtered_required_model_inputs = [ r for r in template_spec.get("required_model_inputs", []) if r in filtered_model_input_names ] return { "inputs": filtered_inputs, "model_inputs": filtered_model_inputs, "outputs": filtered_outputs, "required_inputs": filtered_required_inputs, "required_model_inputs": filtered_required_model_inputs, "block_name": template_spec.get("block_name"), } # Build node specs node_specs = {} for node_type, template_spec in template.items(): if template_spec is None: node_specs[node_type] = None continue block_name = template_spec.get("block_name") if block_name is None or block_name not in sub_block_map: node_specs[node_type] = None continue node_specs[node_type] = filter_spec_for_block(template_spec, sub_block_map[block_name]) return cls( node_specs=node_specs, label=label or getattr(blocks, "model_name", ""), default_repo=default_repo, default_dtype=default_dtype, ) @classmethod def from_custom_block( cls, block, node_label: str = None, input_types: dict[str, Any] | None = None, output_types: dict[str, Any] | None = None, ) -> "MellonPipelineConfig": """ Create a MellonPipelineConfig from a custom block. Args: block: A block instance with `inputs`, `outputs`, and `expected_components`/`component_names` properties. Each InputParam/OutputParam should have metadata={"mellon": "<type>"} where type is one of: image, video, text, checkbox, number, slider, dropdown, model. If metadata is None, maps to "custom". node_label: The display label for the node. Defaults to block class name with spaces. input_types: Optional dict mapping input param names to mellon types. Overrides the block's metadata if provided. Example: {"prompt": "textbox", "image": "image"} output_types: Optional dict mapping output param names to mellon types. Overrides the block's metadata if provided. Example: {"prompt": "text", "images": "image"} Returns: MellonPipelineConfig instance """ if node_label is None: class_name = block.__class__.__name__ node_label = "".join([" " + c if c.isupper() else c for c in class_name]).strip() if input_types is None: input_types = {} if output_types is None: output_types = {} inputs = [] model_inputs = [] outputs = [] # Process block inputs for input_param in block.inputs: if input_param.name is None: continue if input_param.name in input_types: input_param = copy.copy(input_param) input_param.metadata = {"mellon": input_types[input_param.name]} print(f" processing input: {input_param.name}, metadata: {input_param.metadata}") inputs.append(input_param_to_mellon_param(input_param)) # Process block outputs for output_param in block.outputs: if output_param.name is None: continue if output_param.name in output_types: output_param = copy.copy(output_param) output_param.metadata = {"mellon": output_types[output_param.name]} outputs.append(output_param_to_mellon_param(output_param)) # Process expected components (all map to model inputs) component_names = block.component_names for component_name in component_names: model_inputs.append(MellonParam.Input.model(component_name)) # Always add doc output outputs.append(MellonParam.doc()) node_spec = { "inputs": inputs, "model_inputs": model_inputs, "outputs": outputs, "required_inputs": [], "required_model_inputs": [], "block_name": "custom", } return cls( node_specs={"custom": node_spec}, label=node_label, )
{ "repo_id": "huggingface/diffusers", "file_path": "src/diffusers/modular_pipelines/mellon_node_utils.py", "license": "Apache License 2.0", "lines": 981, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
function_complex
huggingface/diffusers:src/diffusers/hooks/context_parallel.py
# Copyright 2025 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import copy import functools import inspect from dataclasses import dataclass from typing import Type import torch import torch.distributed as dist if torch.distributed.is_available(): import torch.distributed._functional_collectives as funcol from ..models._modeling_parallel import ( ContextParallelConfig, ContextParallelInput, ContextParallelModelPlan, ContextParallelOutput, gather_size_by_comm, ) from ..utils import get_logger from ..utils.torch_utils import maybe_allow_in_graph, unwrap_module from .hooks import HookRegistry, ModelHook logger = get_logger(__name__) # pylint: disable=invalid-name _CONTEXT_PARALLEL_INPUT_HOOK_TEMPLATE = "cp_input---{}" _CONTEXT_PARALLEL_OUTPUT_HOOK_TEMPLATE = "cp_output---{}" # TODO(aryan): consolidate with ._helpers.TransformerBlockMetadata @dataclass class ModuleForwardMetadata: cached_parameter_indices: dict[str, int] = None _cls: Type = None def _get_parameter_from_args_kwargs(self, identifier: str, args=(), kwargs=None): kwargs = kwargs or {} if identifier in kwargs: return kwargs[identifier], True, None if self.cached_parameter_indices is not None: index = self.cached_parameter_indices.get(identifier, None) if index is None: raise ValueError(f"Parameter '{identifier}' not found in cached indices.") return args[index], False, index if self._cls is None: raise ValueError("Model class is not set for metadata.") parameters = list(inspect.signature(self._cls.forward).parameters.keys()) parameters = parameters[1:] # skip `self` self.cached_parameter_indices = {param: i for i, param in enumerate(parameters)} if identifier not in self.cached_parameter_indices: raise ValueError(f"Parameter '{identifier}' not found in function signature but was requested.") index = self.cached_parameter_indices[identifier] if index >= len(args): raise ValueError(f"Expected {index} arguments but got {len(args)}.") return args[index], False, index def apply_context_parallel( module: torch.nn.Module, parallel_config: ContextParallelConfig, plan: dict[str, ContextParallelModelPlan], ) -> None: """Apply context parallel on a model.""" logger.debug(f"Applying context parallel with CP mesh: {parallel_config._mesh} and plan: {plan}") for module_id, cp_model_plan in plan.items(): submodule = _get_submodule_by_name(module, module_id) if not isinstance(submodule, list): submodule = [submodule] logger.debug(f"Applying ContextParallelHook to {module_id=} identifying a total of {len(submodule)} modules") for m in submodule: if isinstance(cp_model_plan, dict): hook = ContextParallelSplitHook(cp_model_plan, parallel_config) hook_name = _CONTEXT_PARALLEL_INPUT_HOOK_TEMPLATE.format(module_id) elif isinstance(cp_model_plan, (ContextParallelOutput, list, tuple)): if isinstance(cp_model_plan, ContextParallelOutput): cp_model_plan = [cp_model_plan] if not all(isinstance(x, ContextParallelOutput) for x in cp_model_plan): raise ValueError(f"Expected all elements of cp_model_plan to be CPOutput, but got {cp_model_plan}") hook = ContextParallelGatherHook(cp_model_plan, parallel_config) hook_name = _CONTEXT_PARALLEL_OUTPUT_HOOK_TEMPLATE.format(module_id) else: raise ValueError(f"Unsupported context parallel model plan type: {type(cp_model_plan)}") registry = HookRegistry.check_if_exists_or_initialize(m) registry.register_hook(hook, hook_name) def remove_context_parallel(module: torch.nn.Module, plan: dict[str, ContextParallelModelPlan]) -> None: for module_id, cp_model_plan in plan.items(): submodule = _get_submodule_by_name(module, module_id) if not isinstance(submodule, list): submodule = [submodule] for m in submodule: registry = HookRegistry.check_if_exists_or_initialize(m) if isinstance(cp_model_plan, dict): hook_name = _CONTEXT_PARALLEL_INPUT_HOOK_TEMPLATE.format(module_id) elif isinstance(cp_model_plan, (ContextParallelOutput, list, tuple)): hook_name = _CONTEXT_PARALLEL_OUTPUT_HOOK_TEMPLATE.format(module_id) else: raise ValueError(f"Unsupported context parallel model plan type: {type(cp_model_plan)}") registry.remove_hook(hook_name) class ContextParallelSplitHook(ModelHook): def __init__(self, metadata: ContextParallelModelPlan, parallel_config: ContextParallelConfig) -> None: super().__init__() self.metadata = metadata self.parallel_config = parallel_config self.module_forward_metadata = None def initialize_hook(self, module): cls = unwrap_module(module).__class__ self.module_forward_metadata = ModuleForwardMetadata(_cls=cls) return module def pre_forward(self, module, *args, **kwargs): args_list = list(args) for name, cpm in self.metadata.items(): if isinstance(cpm, ContextParallelInput) and cpm.split_output: continue # Maybe the parameter was passed as a keyword argument input_val, is_kwarg, index = self.module_forward_metadata._get_parameter_from_args_kwargs( name, args_list, kwargs ) if input_val is None: continue # The input_val may be a tensor or list/tuple of tensors. In certain cases, user may specify to shard # the output instead of input for a particular layer by setting split_output=True if isinstance(input_val, torch.Tensor): input_val = self._prepare_cp_input(input_val, cpm) elif isinstance(input_val, (list, tuple)): if len(input_val) != len(cpm): raise ValueError( f"Expected input model plan to have {len(input_val)} elements, but got {len(cpm)}." ) sharded_input_val = [] for i, x in enumerate(input_val): if torch.is_tensor(x) and not cpm[i].split_output: x = self._prepare_cp_input(x, cpm[i]) sharded_input_val.append(x) input_val = sharded_input_val else: raise ValueError(f"Unsupported input type: {type(input_val)}") if is_kwarg: kwargs[name] = input_val elif index is not None and index < len(args_list): args_list[index] = input_val else: raise ValueError( f"An unexpected error occurred while processing the input '{name}'. Please open an " f"issue at https://github.com/huggingface/diffusers/issues and provide a minimal reproducible " f"example along with the full stack trace." ) return tuple(args_list), kwargs def post_forward(self, module, output): is_tensor = isinstance(output, torch.Tensor) is_tensor_list = isinstance(output, (list, tuple)) and all(isinstance(x, torch.Tensor) for x in output) if not is_tensor and not is_tensor_list: raise ValueError(f"Expected output to be a tensor or a list/tuple of tensors, but got {type(output)}.") output = [output] if is_tensor else list(output) for index, cpm in self.metadata.items(): if not isinstance(cpm, ContextParallelInput) or not cpm.split_output: continue if index >= len(output): raise ValueError(f"Index {index} out of bounds for output of length {len(output)}.") current_output = output[index] current_output = self._prepare_cp_input(current_output, cpm) output[index] = current_output return output[0] if is_tensor else tuple(output) def _prepare_cp_input(self, x: torch.Tensor, cp_input: ContextParallelInput) -> torch.Tensor: if cp_input.expected_dims is not None and x.dim() != cp_input.expected_dims: logger.warning_once( f"Expected input tensor to have {cp_input.expected_dims} dimensions, but got {x.dim()} dimensions, split will not be applied." ) return x else: if self.parallel_config.ulysses_anything: return PartitionAnythingSharder.shard_anything( x, cp_input.split_dim, self.parallel_config._flattened_mesh ) return EquipartitionSharder.shard(x, cp_input.split_dim, self.parallel_config._flattened_mesh) class ContextParallelGatherHook(ModelHook): def __init__(self, metadata: ContextParallelModelPlan, parallel_config: ContextParallelConfig) -> None: super().__init__() self.metadata = metadata self.parallel_config = parallel_config def post_forward(self, module, output): is_tensor = isinstance(output, torch.Tensor) if is_tensor: output = [output] elif not (isinstance(output, (list, tuple)) and all(isinstance(x, torch.Tensor) for x in output)): raise ValueError(f"Expected output to be a tensor or a list/tuple of tensors, but got {type(output)}.") output = list(output) if len(output) != len(self.metadata): raise ValueError(f"Expected output to have {len(self.metadata)} elements, but got {len(output)}.") for i, cpm in enumerate(self.metadata): if cpm is None: continue if self.parallel_config.ulysses_anything: output[i] = PartitionAnythingSharder.unshard_anything( output[i], cpm.gather_dim, self.parallel_config._flattened_mesh ) else: output[i] = EquipartitionSharder.unshard( output[i], cpm.gather_dim, self.parallel_config._flattened_mesh ) return output[0] if is_tensor else tuple(output) class AllGatherFunction(torch.autograd.Function): @staticmethod def forward(ctx, tensor, dim, group): ctx.dim = dim ctx.group = group ctx.world_size = torch.distributed.get_world_size(group) ctx.rank = torch.distributed.get_rank(group) return funcol.all_gather_tensor(tensor, dim, group=group) @staticmethod def backward(ctx, grad_output): grad_chunks = torch.chunk(grad_output, ctx.world_size, dim=ctx.dim) return grad_chunks[ctx.rank], None, None class EquipartitionSharder: @classmethod def shard(cls, tensor: torch.Tensor, dim: int, mesh: torch.distributed.device_mesh.DeviceMesh) -> torch.Tensor: # NOTE: the following assertion does not have to be true in general. We simply enforce it for now # because the alternate case has not yet been tested/required for any model. assert tensor.size()[dim] % mesh.size() == 0, ( "Tensor size along dimension to be sharded must be divisible by mesh size" ) # The following is not fullgraph compatible with Dynamo (fails in DeviceMesh.get_rank) # return tensor.chunk(mesh.size(), dim=dim)[mesh.get_rank()] return tensor.chunk(mesh.size(), dim=dim)[torch.distributed.get_rank(mesh.get_group())] @classmethod def unshard(cls, tensor: torch.Tensor, dim: int, mesh: torch.distributed.device_mesh.DeviceMesh) -> torch.Tensor: tensor = tensor.contiguous() tensor = AllGatherFunction.apply(tensor, dim, mesh.get_group()) return tensor class AllGatherAnythingFunction(torch.autograd.Function): @staticmethod def forward(ctx, tensor: torch.Tensor, dim: int, group: dist.device_mesh.DeviceMesh): ctx.dim = dim ctx.group = group ctx.world_size = dist.get_world_size(group) ctx.rank = dist.get_rank(group) gathered_tensor = _all_gather_anything(tensor, dim, group) return gathered_tensor @staticmethod def backward(ctx, grad_output): # NOTE: We use `tensor_split` instead of chunk, because the `chunk` # function may return fewer than the specified number of chunks! grad_splits = torch.tensor_split(grad_output, ctx.world_size, dim=ctx.dim) return grad_splits[ctx.rank], None, None class PartitionAnythingSharder: @classmethod def shard_anything( cls, tensor: torch.Tensor, dim: int, mesh: torch.distributed.device_mesh.DeviceMesh ) -> torch.Tensor: assert tensor.size()[dim] >= mesh.size(), ( f"Cannot shard tensor of size {tensor.size()} along dim {dim} across mesh of size {mesh.size()}." ) # NOTE: We use `tensor_split` instead of chunk, because the `chunk` # function may return fewer than the specified number of chunks! return tensor.tensor_split(mesh.size(), dim=dim)[dist.get_rank(mesh.get_group())] @classmethod def unshard_anything( cls, tensor: torch.Tensor, dim: int, mesh: torch.distributed.device_mesh.DeviceMesh ) -> torch.Tensor: tensor = tensor.contiguous() tensor = AllGatherAnythingFunction.apply(tensor, dim, mesh.get_group()) return tensor @functools.lru_cache(maxsize=64) def _fill_gather_shapes(shape: tuple[int], gather_dims: tuple[int], dim: int, world_size: int) -> list[list[int]]: gather_shapes = [] for i in range(world_size): rank_shape = list(copy.deepcopy(shape)) rank_shape[dim] = gather_dims[i] gather_shapes.append(rank_shape) return gather_shapes @maybe_allow_in_graph def _all_gather_anything(tensor: torch.Tensor, dim: int, group: dist.device_mesh.DeviceMesh) -> torch.Tensor: world_size = dist.get_world_size(group=group) tensor = tensor.contiguous() shape = tensor.shape rank_dim = shape[dim] gather_dims = gather_size_by_comm(rank_dim, group) gather_shapes = _fill_gather_shapes(tuple(shape), tuple(gather_dims), dim, world_size) gathered_tensors = [torch.empty(shape, device=tensor.device, dtype=tensor.dtype) for shape in gather_shapes] dist.all_gather(gathered_tensors, tensor, group=group) gathered_tensor = torch.cat(gathered_tensors, dim=dim) return gathered_tensor def _get_submodule_by_name(model: torch.nn.Module, name: str) -> torch.nn.Module | list[torch.nn.Module]: if name.count("*") > 1: raise ValueError("Wildcard '*' can only be used once in the name") return _find_submodule_by_name(model, name) def _find_submodule_by_name(model: torch.nn.Module, name: str) -> torch.nn.Module | list[torch.nn.Module]: if name == "": return model first_atom, remaining_name = name.split(".", 1) if "." in name else (name, "") if first_atom == "*": if not isinstance(model, torch.nn.ModuleList): raise ValueError("Wildcard '*' can only be used with ModuleList") submodules = [] for submodule in model: subsubmodules = _find_submodule_by_name(submodule, remaining_name) if not isinstance(subsubmodules, list): subsubmodules = [subsubmodules] submodules.extend(subsubmodules) return submodules else: if hasattr(model, first_atom): submodule = getattr(model, first_atom) return _find_submodule_by_name(submodule, remaining_name) else: raise ValueError(f"'{first_atom}' is not a submodule of '{model.__class__.__name__}'")
{ "repo_id": "huggingface/diffusers", "file_path": "src/diffusers/hooks/context_parallel.py", "license": "Apache License 2.0", "lines": 308, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
huggingface/diffusers:src/diffusers/models/_modeling_parallel.py
# 🚨🚨🚨 Experimental parallelism support for Diffusers 🚨🚨🚨 # Experimental changes are subject to change and APIs may break without warning. # Copyright 2025 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from dataclasses import dataclass from typing import TYPE_CHECKING, Literal import torch import torch.distributed as dist from ..utils import get_logger if TYPE_CHECKING: pass logger = get_logger(__name__) # pylint: disable=invalid-name # TODO(aryan): add support for the following: # - Unified Attention # - More dispatcher attention backends # - CFG/Data Parallel # - Tensor Parallel @dataclass class ContextParallelConfig: """ Configuration for context parallelism. Args: ring_degree (`int`, *optional*, defaults to `1`): Number of devices to use for Ring Attention. Sequence is split across devices. Each device computes attention between its local Q and KV chunks passed sequentially around ring. Lower memory (only holds 1/N of KV at a time), overlaps compute with communication, but requires N iterations to see all tokens. Best for long sequences with limited memory/bandwidth. Number of devices to use for ring attention within a context parallel region. Must be a divisor of the total number of devices in the context parallel mesh. ulysses_degree (`int`, *optional*, defaults to `1`): Number of devices to use for Ulysses Attention. Sequence split is across devices. Each device computes local QKV, then all-gathers all KV chunks to compute full attention in one pass. Higher memory (stores all KV), requires high-bandwidth all-to-all communication, but lower latency. Best for moderate sequences with good interconnect bandwidth. convert_to_fp32 (`bool`, *optional*, defaults to `True`): Whether to convert output and LSE to float32 for ring attention numerical stability. rotate_method (`str`, *optional*, defaults to `"allgather"`): Method to use for rotating key/value states across devices in ring attention. Currently, only `"allgather"` is supported. """ ring_degree: int | None = None ulysses_degree: int | None = None convert_to_fp32: bool = True # TODO: support alltoall rotate_method: Literal["allgather", "alltoall"] = "allgather" # Whether to enable ulysses anything attention to support # any sequence lengths and any head numbers. ulysses_anything: bool = False _rank: int = None _world_size: int = None _device: torch.device = None _mesh: torch.distributed.device_mesh.DeviceMesh = None _flattened_mesh: torch.distributed.device_mesh.DeviceMesh = None _ring_mesh: torch.distributed.device_mesh.DeviceMesh = None _ulysses_mesh: torch.distributed.device_mesh.DeviceMesh = None _ring_local_rank: int = None _ulysses_local_rank: int = None def __post_init__(self): if self.ring_degree is None: self.ring_degree = 1 if self.ulysses_degree is None: self.ulysses_degree = 1 if self.ring_degree == 1 and self.ulysses_degree == 1: raise ValueError( "Either ring_degree or ulysses_degree must be greater than 1 in order to use context parallel inference" ) if self.ring_degree < 1 or self.ulysses_degree < 1: raise ValueError("`ring_degree` and `ulysses_degree` must be greater than or equal to 1.") if self.rotate_method != "allgather": raise NotImplementedError( f"Only rotate_method='allgather' is supported for now, but got {self.rotate_method}." ) if self.ulysses_anything: if self.ulysses_degree == 1: raise ValueError("ulysses_degree must be greater than 1 for ulysses_anything to be enabled.") if self.ring_degree > 1: raise ValueError("ulysses_anything cannot be enabled when ring_degree > 1.") @property def mesh_shape(self) -> tuple[int, int]: return (self.ring_degree, self.ulysses_degree) @property def mesh_dim_names(self) -> tuple[str, str]: """Dimension names for the device mesh.""" return ("ring", "ulysses") def setup(self, rank: int, world_size: int, device: torch.device, mesh: torch.distributed.device_mesh.DeviceMesh): self._rank = rank self._world_size = world_size self._device = device self._mesh = mesh if self.ulysses_degree * self.ring_degree > world_size: raise ValueError( f"The product of `ring_degree` ({self.ring_degree}) and `ulysses_degree` ({self.ulysses_degree}) must not exceed the world size ({world_size})." ) self._flattened_mesh = self._mesh._flatten() self._ring_mesh = self._mesh["ring"] self._ulysses_mesh = self._mesh["ulysses"] self._ring_local_rank = self._ring_mesh.get_local_rank() self._ulysses_local_rank = self._ulysses_mesh.get_local_rank() @dataclass class ParallelConfig: """ Configuration for applying different parallelisms. Args: context_parallel_config (`ContextParallelConfig`, *optional*): Configuration for context parallelism. """ context_parallel_config: ContextParallelConfig | None = None _rank: int = None _world_size: int = None _device: torch.device = None _mesh: torch.distributed.device_mesh.DeviceMesh = None def setup( self, rank: int, world_size: int, device: torch.device, *, mesh: torch.distributed.device_mesh.DeviceMesh | None = None, ): self._rank = rank self._world_size = world_size self._device = device self._mesh = mesh if self.context_parallel_config is not None: self.context_parallel_config.setup(rank, world_size, device, mesh) @dataclass(frozen=True) class ContextParallelInput: """ Configuration for splitting an input tensor across context parallel region. Args: split_dim (`int`): The dimension along which to split the tensor. expected_dims (`int`, *optional*): The expected number of dimensions of the tensor. If provided, a check will be performed to ensure that the tensor has the expected number of dimensions before splitting. split_output (`bool`, *optional*, defaults to `False`): Whether to split the output tensor of the layer along the given `split_dim` instead of the input tensor. This is useful for layers whose outputs should be split after it does some preprocessing on the inputs (ex: RoPE). """ split_dim: int expected_dims: int | None = None split_output: bool = False def __repr__(self): return f"ContextParallelInput(split_dim={self.split_dim}, expected_dims={self.expected_dims}, split_output={self.split_output})" @dataclass(frozen=True) class ContextParallelOutput: """ Configuration for gathering an output tensor across context parallel region. Args: gather_dim (`int`): The dimension along which to gather the tensor. expected_dims (`int`, *optional*): The expected number of dimensions of the tensor. If provided, a check will be performed to ensure that the tensor has the expected number of dimensions before gathering. """ gather_dim: int expected_dims: int | None = None def __repr__(self): return f"ContextParallelOutput(gather_dim={self.gather_dim}, expected_dims={self.expected_dims})" # A dictionary where keys denote the input to be split across context parallel region, and the # value denotes the sharding configuration. # If the key is a string, it denotes the name of the parameter in the forward function. # If the key is an integer, split_output must be set to True, and it denotes the index of the output # to be split across context parallel region. ContextParallelInputType = dict[ str | int, ContextParallelInput | list[ContextParallelInput] | tuple[ContextParallelInput, ...] ] # A dictionary where keys denote the output to be gathered across context parallel region, and the # value denotes the gathering configuration. ContextParallelOutputType = ContextParallelOutput | list[ContextParallelOutput] | tuple[ContextParallelOutput, ...] # A dictionary where keys denote the module id, and the value denotes how the inputs/outputs of # the module should be split/gathered across context parallel region. ContextParallelModelPlan = dict[str, ContextParallelInputType | ContextParallelOutputType] # Example of a ContextParallelModelPlan (QwenImageTransformer2DModel): # # Each model should define a _cp_plan attribute that contains information on how to shard/gather # tensors at different stages of the forward: # # ```python # _cp_plan = { # "": { # "hidden_states": ContextParallelInput(split_dim=1, expected_dims=3, split_output=False), # "encoder_hidden_states": ContextParallelInput(split_dim=1, expected_dims=3, split_output=False), # "encoder_hidden_states_mask": ContextParallelInput(split_dim=1, expected_dims=2, split_output=False), # }, # "pos_embed": { # 0: ContextParallelInput(split_dim=0, expected_dims=2, split_output=True), # 1: ContextParallelInput(split_dim=0, expected_dims=2, split_output=True), # }, # "proj_out": ContextParallelOutput(gather_dim=1, expected_dims=3), # } # ``` # # The dictionary is a set of module names mapped to their respective CP plan. The inputs/outputs of layers will be # split/gathered according to this at the respective module level. Here, the following happens: # - "": # we specify that we want to split the various inputs across the sequence dim in the pre-forward hook (i.e. before # the actual forward logic of the QwenImageTransformer2DModel is run, we will splitthe inputs) # - "pos_embed": # we specify that we want to split the outputs of the RoPE layer. Since there are two outputs (imag & text freqs), # we can individually specify how they should be split # - "proj_out": # before returning to the user, we gather the entire sequence on each rank in the post-forward hook (after the linear # layer forward has run). # # ContextParallelInput: # specifies how to split the input tensor in the pre-forward or post-forward hook of the layer it is attached to # # ContextParallelOutput: # specifies how to gather the input tensor in the post-forward hook in the layer it is attached to # Below are utility functions for distributed communication in context parallelism. def gather_size_by_comm(size: int, group: dist.ProcessGroup) -> list[int]: r"""Gather the local size from all ranks. size: int, local size return: list[int], list of size from all ranks """ # NOTE(Serving/CP Safety): # Do NOT cache this collective result. # # In "Ulysses Anything" mode, `size` (e.g. per-rank local seq_len / S_LOCAL) # may legitimately differ across ranks. If we cache based on the *local* `size`, # different ranks can have different cache hit/miss patterns across time. # # That can lead to a catastrophic distributed hang: # - some ranks hit cache and *skip* dist.all_gather() # - other ranks miss cache and *enter* dist.all_gather() # This mismatched collective participation will stall the process group and # eventually trigger NCCL watchdog timeouts (often surfacing later as ALLTOALL # timeouts in Ulysses attention). world_size = dist.get_world_size(group=group) # HACK: Use Gloo backend for all_gather to avoid H2D and D2H overhead comm_backends = str(dist.get_backend(group=group)) # NOTE: e.g., dist.init_process_group(backend="cpu:gloo,cuda:nccl") gather_device = "cpu" if "cpu" in comm_backends else torch.accelerator.current_accelerator() gathered_sizes = [torch.empty((1,), device=gather_device, dtype=torch.int64) for _ in range(world_size)] dist.all_gather( gathered_sizes, torch.tensor([size], device=gather_device, dtype=torch.int64), group=group, ) gathered_sizes = [s[0].item() for s in gathered_sizes] # NOTE: DON'T use tolist here due to graph break - Explanation: # Backend compiler `inductor` failed with aten._local_scalar_dense.default return gathered_sizes
{ "repo_id": "huggingface/diffusers", "file_path": "src/diffusers/models/_modeling_parallel.py", "license": "Apache License 2.0", "lines": 254, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
huggingface/diffusers:tests/pipelines/qwenimage/test_qwenimage_edit_plus.py
# Copyright 2025 The HuggingFace Team. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest import numpy as np import pytest import torch from PIL import Image from transformers import Qwen2_5_VLConfig, Qwen2_5_VLForConditionalGeneration, Qwen2Tokenizer, Qwen2VLProcessor from diffusers import ( AutoencoderKLQwenImage, FlowMatchEulerDiscreteScheduler, QwenImageEditPlusPipeline, QwenImageTransformer2DModel, ) from ...testing_utils import enable_full_determinism, torch_device from ..pipeline_params import TEXT_TO_IMAGE_PARAMS from ..test_pipelines_common import PipelineTesterMixin, to_np enable_full_determinism() class QwenImageEditPlusPipelineFastTests(PipelineTesterMixin, unittest.TestCase): pipeline_class = QwenImageEditPlusPipeline params = TEXT_TO_IMAGE_PARAMS - {"cross_attention_kwargs"} batch_params = frozenset(["prompt", "image"]) image_params = frozenset(["image"]) image_latents_params = frozenset(["latents"]) required_optional_params = frozenset( [ "num_inference_steps", "generator", "latents", "return_dict", "callback_on_step_end", "callback_on_step_end_tensor_inputs", ] ) supports_dduf = False test_xformers_attention = False test_layerwise_casting = True test_group_offloading = True def get_dummy_components(self): tiny_ckpt_id = "hf-internal-testing/tiny-random-Qwen2VLForConditionalGeneration" torch.manual_seed(0) transformer = QwenImageTransformer2DModel( patch_size=2, in_channels=16, out_channels=4, num_layers=2, attention_head_dim=16, num_attention_heads=3, joint_attention_dim=16, guidance_embeds=False, axes_dims_rope=(8, 4, 4), ) torch.manual_seed(0) z_dim = 4 vae = AutoencoderKLQwenImage( base_dim=z_dim * 6, z_dim=z_dim, dim_mult=[1, 2, 4], num_res_blocks=1, temperal_downsample=[False, True], latents_mean=[0.0] * z_dim, latents_std=[1.0] * z_dim, ) torch.manual_seed(0) scheduler = FlowMatchEulerDiscreteScheduler() torch.manual_seed(0) config = Qwen2_5_VLConfig( text_config={ "hidden_size": 16, "intermediate_size": 16, "num_hidden_layers": 2, "num_attention_heads": 2, "num_key_value_heads": 2, "rope_scaling": { "mrope_section": [1, 1, 2], "rope_type": "default", "type": "default", }, "rope_theta": 1000000.0, }, vision_config={ "depth": 2, "hidden_size": 16, "intermediate_size": 16, "num_heads": 2, "out_hidden_size": 16, }, hidden_size=16, vocab_size=152064, vision_end_token_id=151653, vision_start_token_id=151652, vision_token_id=151654, ) text_encoder = Qwen2_5_VLForConditionalGeneration(config) tokenizer = Qwen2Tokenizer.from_pretrained(tiny_ckpt_id) components = { "transformer": transformer, "vae": vae, "scheduler": scheduler, "text_encoder": text_encoder, "tokenizer": tokenizer, "processor": Qwen2VLProcessor.from_pretrained(tiny_ckpt_id), } return components def get_dummy_inputs(self, device, seed=0): if str(device).startswith("mps"): generator = torch.manual_seed(seed) else: generator = torch.Generator(device=device).manual_seed(seed) image = Image.new("RGB", (32, 32)) inputs = { "prompt": "dance monkey", "image": [image, image], "negative_prompt": "bad quality", "generator": generator, "num_inference_steps": 2, "true_cfg_scale": 1.0, "height": 32, "width": 32, "max_sequence_length": 16, "output_type": "pt", } return inputs def test_inference(self): device = "cpu" components = self.get_dummy_components() pipe = self.pipeline_class(**components) pipe.to(device) pipe.set_progress_bar_config(disable=None) inputs = self.get_dummy_inputs(device) image = pipe(**inputs).images generated_image = image[0] self.assertEqual(generated_image.shape, (3, 32, 32)) # fmt: off expected_slice = torch.tensor([0.5640, 0.6339, 0.5997, 0.5607, 0.5799, 0.5496, 0.5760, 0.6393, 0.4172, 0.3595, 0.5655, 0.4896, 0.4971, 0.5255, 0.4088, 0.4987]) # fmt: on generated_slice = generated_image.flatten() generated_slice = torch.cat([generated_slice[:8], generated_slice[-8:]]) self.assertTrue(torch.allclose(generated_slice, expected_slice, atol=1e-3)) def test_attention_slicing_forward_pass( self, test_max_difference=True, test_mean_pixel_difference=True, expected_max_diff=1e-3 ): if not self.test_attention_slicing: return components = self.get_dummy_components() pipe = self.pipeline_class(**components) for component in pipe.components.values(): if hasattr(component, "set_default_attn_processor"): component.set_default_attn_processor() pipe.to(torch_device) pipe.set_progress_bar_config(disable=None) generator_device = "cpu" inputs = self.get_dummy_inputs(generator_device) output_without_slicing = pipe(**inputs)[0] pipe.enable_attention_slicing(slice_size=1) inputs = self.get_dummy_inputs(generator_device) output_with_slicing1 = pipe(**inputs)[0] pipe.enable_attention_slicing(slice_size=2) inputs = self.get_dummy_inputs(generator_device) output_with_slicing2 = pipe(**inputs)[0] if test_max_difference: max_diff1 = np.abs(to_np(output_with_slicing1) - to_np(output_without_slicing)).max() max_diff2 = np.abs(to_np(output_with_slicing2) - to_np(output_without_slicing)).max() self.assertLess( max(max_diff1, max_diff2), expected_max_diff, "Attention slicing should not affect the inference results", ) def test_vae_tiling(self, expected_diff_max: float = 0.2): generator_device = "cpu" components = self.get_dummy_components() pipe = self.pipeline_class(**components) pipe.to("cpu") pipe.set_progress_bar_config(disable=None) # Without tiling inputs = self.get_dummy_inputs(generator_device) inputs["height"] = inputs["width"] = 128 output_without_tiling = pipe(**inputs)[0] # With tiling pipe.vae.enable_tiling( tile_sample_min_height=96, tile_sample_min_width=96, tile_sample_stride_height=64, tile_sample_stride_width=64, ) inputs = self.get_dummy_inputs(generator_device) inputs["height"] = inputs["width"] = 128 output_with_tiling = pipe(**inputs)[0] self.assertLess( (to_np(output_without_tiling) - to_np(output_with_tiling)).max(), expected_diff_max, "VAE tiling should not affect the inference results", ) @pytest.mark.xfail(condition=True, reason="Preconfigured embeddings need to be revisited.", strict=True) def test_encode_prompt_works_in_isolation(self, extra_required_param_value_dict=None, atol=1e-4, rtol=1e-4): super().test_encode_prompt_works_in_isolation(extra_required_param_value_dict, atol, rtol) @pytest.mark.xfail(condition=True, reason="Batch of multiple images needs to be revisited", strict=True) def test_num_images_per_prompt(): super().test_num_images_per_prompt() @pytest.mark.xfail(condition=True, reason="Batch of multiple images needs to be revisited", strict=True) def test_inference_batch_consistent(): super().test_inference_batch_consistent() @pytest.mark.xfail(condition=True, reason="Batch of multiple images needs to be revisited", strict=True) def test_inference_batch_single_identical(): super().test_inference_batch_single_identical()
{ "repo_id": "huggingface/diffusers", "file_path": "tests/pipelines/qwenimage/test_qwenimage_edit_plus.py", "license": "Apache License 2.0", "lines": 216, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
test
huggingface/diffusers:src/diffusers/pipelines/qwenimage/pipeline_qwenimage_edit_plus.py
# Copyright 2025 Qwen-Image Team and The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import inspect import math from typing import Any, Callable import numpy as np import torch from transformers import Qwen2_5_VLForConditionalGeneration, Qwen2Tokenizer, Qwen2VLProcessor from ...image_processor import PipelineImageInput, VaeImageProcessor from ...loaders import QwenImageLoraLoaderMixin from ...models import AutoencoderKLQwenImage, QwenImageTransformer2DModel from ...schedulers import FlowMatchEulerDiscreteScheduler from ...utils import is_torch_xla_available, logging, replace_example_docstring from ...utils.torch_utils import randn_tensor from ..pipeline_utils import DiffusionPipeline from .pipeline_output import QwenImagePipelineOutput if is_torch_xla_available(): import torch_xla.core.xla_model as xm XLA_AVAILABLE = True else: XLA_AVAILABLE = False logger = logging.get_logger(__name__) # pylint: disable=invalid-name EXAMPLE_DOC_STRING = """ Examples: ```py >>> import torch >>> from PIL import Image >>> from diffusers import QwenImageEditPlusPipeline >>> from diffusers.utils import load_image >>> pipe = QwenImageEditPlusPipeline.from_pretrained("Qwen/Qwen-Image-Edit-2509", torch_dtype=torch.bfloat16) >>> pipe.to("cuda") >>> image = load_image( ... "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/yarn-art-pikachu.png" ... ).convert("RGB") >>> prompt = ( ... "Make Pikachu hold a sign that says 'Qwen Edit is awesome', yarn art style, detailed, vibrant colors" ... ) >>> # Depending on the variant being used, the pipeline call will slightly vary. >>> # Refer to the pipeline documentation for more details. >>> image = pipe(image, prompt, num_inference_steps=50).images[0] >>> image.save("qwenimage_edit_plus.png") ``` """ CONDITION_IMAGE_SIZE = 384 * 384 VAE_IMAGE_SIZE = 1024 * 1024 # Copied from diffusers.pipelines.qwenimage.pipeline_qwenimage.calculate_shift def calculate_shift( image_seq_len, base_seq_len: int = 256, max_seq_len: int = 4096, base_shift: float = 0.5, max_shift: float = 1.15, ): m = (max_shift - base_shift) / (max_seq_len - base_seq_len) b = base_shift - m * base_seq_len mu = image_seq_len * m + b return mu # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.retrieve_timesteps def retrieve_timesteps( scheduler, num_inference_steps: int | None = None, device: str | torch.device | None = None, timesteps: list[int] | None = None, sigmas: list[float] | None = None, **kwargs, ): r""" Calls the scheduler's `set_timesteps` method and retrieves timesteps from the scheduler after the call. Handles custom timesteps. Any kwargs will be supplied to `scheduler.set_timesteps`. Args: scheduler (`SchedulerMixin`): The scheduler to get timesteps from. num_inference_steps (`int`): The number of diffusion steps used when generating samples with a pre-trained model. If used, `timesteps` must be `None`. device (`str` or `torch.device`, *optional*): The device to which the timesteps should be moved to. If `None`, the timesteps are not moved. timesteps (`list[int]`, *optional*): Custom timesteps used to override the timestep spacing strategy of the scheduler. If `timesteps` is passed, `num_inference_steps` and `sigmas` must be `None`. sigmas (`list[float]`, *optional*): Custom sigmas used to override the timestep spacing strategy of the scheduler. If `sigmas` is passed, `num_inference_steps` and `timesteps` must be `None`. Returns: `tuple[torch.Tensor, int]`: A tuple where the first element is the timestep schedule from the scheduler and the second element is the number of inference steps. """ if timesteps is not None and sigmas is not None: raise ValueError("Only one of `timesteps` or `sigmas` can be passed. Please choose one to set custom values") if timesteps is not None: accepts_timesteps = "timesteps" in set(inspect.signature(scheduler.set_timesteps).parameters.keys()) if not accepts_timesteps: raise ValueError( f"The current scheduler class {scheduler.__class__}'s `set_timesteps` does not support custom" f" timestep schedules. Please check whether you are using the correct scheduler." ) scheduler.set_timesteps(timesteps=timesteps, device=device, **kwargs) timesteps = scheduler.timesteps num_inference_steps = len(timesteps) elif sigmas is not None: accept_sigmas = "sigmas" in set(inspect.signature(scheduler.set_timesteps).parameters.keys()) if not accept_sigmas: raise ValueError( f"The current scheduler class {scheduler.__class__}'s `set_timesteps` does not support custom" f" sigmas schedules. Please check whether you are using the correct scheduler." ) scheduler.set_timesteps(sigmas=sigmas, device=device, **kwargs) timesteps = scheduler.timesteps num_inference_steps = len(timesteps) else: scheduler.set_timesteps(num_inference_steps, device=device, **kwargs) timesteps = scheduler.timesteps return timesteps, num_inference_steps # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion_img2img.retrieve_latents def retrieve_latents( encoder_output: torch.Tensor, generator: torch.Generator | None = None, sample_mode: str = "sample" ): if hasattr(encoder_output, "latent_dist") and sample_mode == "sample": return encoder_output.latent_dist.sample(generator) elif hasattr(encoder_output, "latent_dist") and sample_mode == "argmax": return encoder_output.latent_dist.mode() elif hasattr(encoder_output, "latents"): return encoder_output.latents else: raise AttributeError("Could not access latents of provided encoder_output") def calculate_dimensions(target_area, ratio): width = math.sqrt(target_area * ratio) height = width / ratio width = round(width / 32) * 32 height = round(height / 32) * 32 return width, height class QwenImageEditPlusPipeline(DiffusionPipeline, QwenImageLoraLoaderMixin): r""" The Qwen-Image-Edit pipeline for image editing. Args: transformer ([`QwenImageTransformer2DModel`]): Conditional Transformer (MMDiT) architecture to denoise the encoded image latents. scheduler ([`FlowMatchEulerDiscreteScheduler`]): A scheduler to be used in combination with `transformer` to denoise the encoded image latents. vae ([`AutoencoderKL`]): Variational Auto-Encoder (VAE) Model to encode and decode images to and from latent representations. text_encoder ([`Qwen2.5-VL-7B-Instruct`]): [Qwen2.5-VL-7B-Instruct](https://huggingface.co/Qwen/Qwen2.5-VL-7B-Instruct), specifically the [Qwen2.5-VL-7B-Instruct](https://huggingface.co/Qwen/Qwen2.5-VL-7B-Instruct) variant. tokenizer (`QwenTokenizer`): Tokenizer of class [CLIPTokenizer](https://huggingface.co/docs/transformers/en/model_doc/clip#transformers.CLIPTokenizer). """ model_cpu_offload_seq = "text_encoder->transformer->vae" _callback_tensor_inputs = ["latents", "prompt_embeds"] def __init__( self, scheduler: FlowMatchEulerDiscreteScheduler, vae: AutoencoderKLQwenImage, text_encoder: Qwen2_5_VLForConditionalGeneration, tokenizer: Qwen2Tokenizer, processor: Qwen2VLProcessor, transformer: QwenImageTransformer2DModel, ): super().__init__() self.register_modules( vae=vae, text_encoder=text_encoder, tokenizer=tokenizer, processor=processor, transformer=transformer, scheduler=scheduler, ) self.vae_scale_factor = 2 ** len(self.vae.temperal_downsample) if getattr(self, "vae", None) else 8 self.latent_channels = self.vae.config.z_dim if getattr(self, "vae", None) else 16 # QwenImage latents are turned into 2x2 patches and packed. This means the latent width and height has to be divisible # by the patch size. So the vae scale factor is multiplied by the patch size to account for this self.image_processor = VaeImageProcessor(vae_scale_factor=self.vae_scale_factor * 2) self.tokenizer_max_length = 1024 self.prompt_template_encode = "<|im_start|>system\nDescribe the key features of the input image (color, shape, size, texture, objects, background), then explain how the user's text instruction should alter or modify the image. Generate a new image that meets the user's requirements while maintaining consistency with the original input where appropriate.<|im_end|>\n<|im_start|>user\n{}<|im_end|>\n<|im_start|>assistant\n" self.prompt_template_encode_start_idx = 64 self.default_sample_size = 128 # Copied from diffusers.pipelines.qwenimage.pipeline_qwenimage.QwenImagePipeline._extract_masked_hidden def _extract_masked_hidden(self, hidden_states: torch.Tensor, mask: torch.Tensor): bool_mask = mask.bool() valid_lengths = bool_mask.sum(dim=1) selected = hidden_states[bool_mask] split_result = torch.split(selected, valid_lengths.tolist(), dim=0) return split_result def _get_qwen_prompt_embeds( self, prompt: str | list[str] = None, image: torch.Tensor | None = None, device: torch.device | None = None, dtype: torch.dtype | None = None, ): device = device or self._execution_device dtype = dtype or self.text_encoder.dtype prompt = [prompt] if isinstance(prompt, str) else prompt img_prompt_template = "Picture {}: <|vision_start|><|image_pad|><|vision_end|>" if isinstance(image, list): base_img_prompt = "" for i, img in enumerate(image): base_img_prompt += img_prompt_template.format(i + 1) elif image is not None: base_img_prompt = img_prompt_template.format(1) else: base_img_prompt = "" template = self.prompt_template_encode drop_idx = self.prompt_template_encode_start_idx txt = [template.format(base_img_prompt + e) for e in prompt] model_inputs = self.processor( text=txt, images=image, padding=True, return_tensors="pt", ).to(device) outputs = self.text_encoder( input_ids=model_inputs.input_ids, attention_mask=model_inputs.attention_mask, pixel_values=model_inputs.pixel_values, image_grid_thw=model_inputs.image_grid_thw, output_hidden_states=True, ) hidden_states = outputs.hidden_states[-1] split_hidden_states = self._extract_masked_hidden(hidden_states, model_inputs.attention_mask) split_hidden_states = [e[drop_idx:] for e in split_hidden_states] attn_mask_list = [torch.ones(e.size(0), dtype=torch.long, device=e.device) for e in split_hidden_states] max_seq_len = max([e.size(0) for e in split_hidden_states]) prompt_embeds = torch.stack( [torch.cat([u, u.new_zeros(max_seq_len - u.size(0), u.size(1))]) for u in split_hidden_states] ) encoder_attention_mask = torch.stack( [torch.cat([u, u.new_zeros(max_seq_len - u.size(0))]) for u in attn_mask_list] ) prompt_embeds = prompt_embeds.to(dtype=dtype, device=device) return prompt_embeds, encoder_attention_mask # Copied from diffusers.pipelines.qwenimage.pipeline_qwenimage_edit.QwenImageEditPipeline.encode_prompt def encode_prompt( self, prompt: str | list[str], image: torch.Tensor | None = None, device: torch.device | None = None, num_images_per_prompt: int = 1, prompt_embeds: torch.Tensor | None = None, prompt_embeds_mask: torch.Tensor | None = None, max_sequence_length: int = 1024, ): r""" Args: prompt (`str` or `list[str]`, *optional*): prompt to be encoded image (`torch.Tensor`, *optional*): image to be encoded device: (`torch.device`): torch device num_images_per_prompt (`int`): number of images that should be generated per prompt prompt_embeds (`torch.Tensor`, *optional*): Pre-generated text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt weighting. If not provided, text embeddings will be generated from `prompt` input argument. """ device = device or self._execution_device prompt = [prompt] if isinstance(prompt, str) else prompt batch_size = len(prompt) if prompt_embeds is None else prompt_embeds.shape[0] if prompt_embeds is None: prompt_embeds, prompt_embeds_mask = self._get_qwen_prompt_embeds(prompt, image, device) _, seq_len, _ = prompt_embeds.shape prompt_embeds = prompt_embeds.repeat(1, num_images_per_prompt, 1) prompt_embeds = prompt_embeds.view(batch_size * num_images_per_prompt, seq_len, -1) prompt_embeds_mask = prompt_embeds_mask.repeat(1, num_images_per_prompt, 1) prompt_embeds_mask = prompt_embeds_mask.view(batch_size * num_images_per_prompt, seq_len) if prompt_embeds_mask is not None and prompt_embeds_mask.all(): prompt_embeds_mask = None return prompt_embeds, prompt_embeds_mask # Copied from diffusers.pipelines.qwenimage.pipeline_qwenimage_edit.QwenImageEditPipeline.check_inputs def check_inputs( self, prompt, height, width, negative_prompt=None, prompt_embeds=None, negative_prompt_embeds=None, prompt_embeds_mask=None, negative_prompt_embeds_mask=None, callback_on_step_end_tensor_inputs=None, max_sequence_length=None, ): if height % (self.vae_scale_factor * 2) != 0 or width % (self.vae_scale_factor * 2) != 0: logger.warning( f"`height` and `width` have to be divisible by {self.vae_scale_factor * 2} but are {height} and {width}. Dimensions will be resized accordingly" ) if callback_on_step_end_tensor_inputs is not None and not all( k in self._callback_tensor_inputs for k in callback_on_step_end_tensor_inputs ): raise ValueError( f"`callback_on_step_end_tensor_inputs` has to be in {self._callback_tensor_inputs}, but found {[k for k in callback_on_step_end_tensor_inputs if k not in self._callback_tensor_inputs]}" ) if prompt is not None and prompt_embeds is not None: raise ValueError( f"Cannot forward both `prompt`: {prompt} and `prompt_embeds`: {prompt_embeds}. Please make sure to" " only forward one of the two." ) elif prompt is None and prompt_embeds is None: raise ValueError( "Provide either `prompt` or `prompt_embeds`. Cannot leave both `prompt` and `prompt_embeds` undefined." ) elif prompt is not None and (not isinstance(prompt, str) and not isinstance(prompt, list)): raise ValueError(f"`prompt` has to be of type `str` or `list` but is {type(prompt)}") if negative_prompt is not None and negative_prompt_embeds is not None: raise ValueError( f"Cannot forward both `negative_prompt`: {negative_prompt} and `negative_prompt_embeds`:" f" {negative_prompt_embeds}. Please make sure to only forward one of the two." ) if prompt_embeds is not None and prompt_embeds_mask is None: raise ValueError( "If `prompt_embeds` are provided, `prompt_embeds_mask` also have to be passed. Make sure to generate `prompt_embeds_mask` from the same text encoder that was used to generate `prompt_embeds`." ) if negative_prompt_embeds is not None and negative_prompt_embeds_mask is None: raise ValueError( "If `negative_prompt_embeds` are provided, `negative_prompt_embeds_mask` also have to be passed. Make sure to generate `negative_prompt_embeds_mask` from the same text encoder that was used to generate `negative_prompt_embeds`." ) if max_sequence_length is not None and max_sequence_length > 1024: raise ValueError(f"`max_sequence_length` cannot be greater than 1024 but is {max_sequence_length}") @staticmethod # Copied from diffusers.pipelines.qwenimage.pipeline_qwenimage.QwenImagePipeline._pack_latents def _pack_latents(latents, batch_size, num_channels_latents, height, width): latents = latents.view(batch_size, num_channels_latents, height // 2, 2, width // 2, 2) latents = latents.permute(0, 2, 4, 1, 3, 5) latents = latents.reshape(batch_size, (height // 2) * (width // 2), num_channels_latents * 4) return latents @staticmethod # Copied from diffusers.pipelines.qwenimage.pipeline_qwenimage.QwenImagePipeline._unpack_latents def _unpack_latents(latents, height, width, vae_scale_factor): batch_size, num_patches, channels = latents.shape # VAE applies 8x compression on images but we must also account for packing which requires # latent height and width to be divisible by 2. height = 2 * (int(height) // (vae_scale_factor * 2)) width = 2 * (int(width) // (vae_scale_factor * 2)) latents = latents.view(batch_size, height // 2, width // 2, channels // 4, 2, 2) latents = latents.permute(0, 3, 1, 4, 2, 5) latents = latents.reshape(batch_size, channels // (2 * 2), 1, height, width) return latents # Copied from diffusers.pipelines.qwenimage.pipeline_qwenimage_edit.QwenImageEditPipeline._encode_vae_image def _encode_vae_image(self, image: torch.Tensor, generator: torch.Generator): if isinstance(generator, list): image_latents = [ retrieve_latents(self.vae.encode(image[i : i + 1]), generator=generator[i], sample_mode="argmax") for i in range(image.shape[0]) ] image_latents = torch.cat(image_latents, dim=0) else: image_latents = retrieve_latents(self.vae.encode(image), generator=generator, sample_mode="argmax") latents_mean = ( torch.tensor(self.vae.config.latents_mean) .view(1, self.latent_channels, 1, 1, 1) .to(image_latents.device, image_latents.dtype) ) latents_std = ( torch.tensor(self.vae.config.latents_std) .view(1, self.latent_channels, 1, 1, 1) .to(image_latents.device, image_latents.dtype) ) image_latents = (image_latents - latents_mean) / latents_std return image_latents def prepare_latents( self, images, batch_size, num_channels_latents, height, width, dtype, device, generator, latents=None, ): # VAE applies 8x compression on images but we must also account for packing which requires # latent height and width to be divisible by 2. height = 2 * (int(height) // (self.vae_scale_factor * 2)) width = 2 * (int(width) // (self.vae_scale_factor * 2)) shape = (batch_size, 1, num_channels_latents, height, width) image_latents = None if images is not None: if not isinstance(images, list): images = [images] all_image_latents = [] for image in images: image = image.to(device=device, dtype=dtype) if image.shape[1] != self.latent_channels: image_latents = self._encode_vae_image(image=image, generator=generator) else: image_latents = image if batch_size > image_latents.shape[0] and batch_size % image_latents.shape[0] == 0: # expand init_latents for batch_size additional_image_per_prompt = batch_size // image_latents.shape[0] image_latents = torch.cat([image_latents] * additional_image_per_prompt, dim=0) elif batch_size > image_latents.shape[0] and batch_size % image_latents.shape[0] != 0: raise ValueError( f"Cannot duplicate `image` of batch size {image_latents.shape[0]} to {batch_size} text prompts." ) else: image_latents = torch.cat([image_latents], dim=0) image_latent_height, image_latent_width = image_latents.shape[3:] image_latents = self._pack_latents( image_latents, batch_size, num_channels_latents, image_latent_height, image_latent_width ) all_image_latents.append(image_latents) image_latents = torch.cat(all_image_latents, dim=1) if isinstance(generator, list) and len(generator) != batch_size: raise ValueError( f"You have passed a list of generators of length {len(generator)}, but requested an effective batch" f" size of {batch_size}. Make sure the batch size matches the length of the generators." ) if latents is None: latents = randn_tensor(shape, generator=generator, device=device, dtype=dtype) latents = self._pack_latents(latents, batch_size, num_channels_latents, height, width) else: latents = latents.to(device=device, dtype=dtype) return latents, image_latents @property def guidance_scale(self): return self._guidance_scale @property def attention_kwargs(self): return self._attention_kwargs @property def num_timesteps(self): return self._num_timesteps @property def current_timestep(self): return self._current_timestep @property def interrupt(self): return self._interrupt @torch.no_grad() @replace_example_docstring(EXAMPLE_DOC_STRING) def __call__( self, image: PipelineImageInput | None = None, prompt: str | list[str] = None, negative_prompt: str | list[str] = None, true_cfg_scale: float = 4.0, height: int | None = None, width: int | None = None, num_inference_steps: int = 50, sigmas: list[float] | None = None, guidance_scale: float | None = None, num_images_per_prompt: int = 1, generator: torch.Generator | list[torch.Generator] | None = None, latents: torch.Tensor | None = None, prompt_embeds: torch.Tensor | None = None, prompt_embeds_mask: torch.Tensor | None = None, negative_prompt_embeds: torch.Tensor | None = None, negative_prompt_embeds_mask: torch.Tensor | None = None, output_type: str | None = "pil", return_dict: bool = True, attention_kwargs: dict[str, Any] | None = None, callback_on_step_end: Callable[[int, int], None] | None = None, callback_on_step_end_tensor_inputs: list[str] = ["latents"], max_sequence_length: int = 512, ): r""" Function invoked when calling the pipeline for generation. Args: image (`torch.Tensor`, `PIL.Image.Image`, `np.ndarray`, `list[torch.Tensor]`, `list[PIL.Image.Image]`, or `list[np.ndarray]`): `Image`, numpy array or tensor representing an image batch to be used as the starting point. For both numpy array and pytorch tensor, the expected value range is between `[0, 1]` If it's a tensor or a list or tensors, the expected shape should be `(B, C, H, W)` or `(C, H, W)`. If it is a numpy array or a list of arrays, the expected shape should be `(B, H, W, C)` or `(H, W, C)` It can also accept image latents as `image`, but if passing latents directly it is not encoded again. prompt (`str` or `list[str]`, *optional*): The prompt or prompts to guide the image generation. If not defined, one has to pass `prompt_embeds`. instead. negative_prompt (`str` or `list[str]`, *optional*): The prompt or prompts not to guide the image generation. If not defined, one has to pass `negative_prompt_embeds` instead. Ignored when not using guidance (i.e., ignored if `true_cfg_scale` is not greater than `1`). true_cfg_scale (`float`, *optional*, defaults to 1.0): true_cfg_scale (`float`, *optional*, defaults to 1.0): Guidance scale as defined in [Classifier-Free Diffusion Guidance](https://huggingface.co/papers/2207.12598). `true_cfg_scale` is defined as `w` of equation 2. of [Imagen Paper](https://huggingface.co/papers/2205.11487). Classifier-free guidance is enabled by setting `true_cfg_scale > 1` and a provided `negative_prompt`. Higher guidance scale encourages to generate images that are closely linked to the text `prompt`, usually at the expense of lower image quality. height (`int`, *optional*, defaults to self.unet.config.sample_size * self.vae_scale_factor): The height in pixels of the generated image. This is set to 1024 by default for the best results. width (`int`, *optional*, defaults to self.unet.config.sample_size * self.vae_scale_factor): The width in pixels of the generated image. This is set to 1024 by default for the best results. num_inference_steps (`int`, *optional*, defaults to 50): The number of denoising steps. More denoising steps usually lead to a higher quality image at the expense of slower inference. sigmas (`list[float]`, *optional*): Custom sigmas to use for the denoising process with schedulers which support a `sigmas` argument in their `set_timesteps` method. If not defined, the default behavior when `num_inference_steps` is passed will be used. guidance_scale (`float`, *optional*, defaults to None): A guidance scale value for guidance distilled models. Unlike the traditional classifier-free guidance where the guidance scale is applied during inference through noise prediction rescaling, guidance distilled models take the guidance scale directly as an input parameter during forward pass. Guidance scale is enabled by setting `guidance_scale > 1`. Higher guidance scale encourages to generate images that are closely linked to the text `prompt`, usually at the expense of lower image quality. This parameter in the pipeline is there to support future guidance-distilled models when they come up. It is ignored when not using guidance distilled models. To enable traditional classifier-free guidance, please pass `true_cfg_scale > 1.0` and `negative_prompt` (even an empty negative prompt like " " should enable classifier-free guidance computations). num_images_per_prompt (`int`, *optional*, defaults to 1): The number of images to generate per prompt. generator (`torch.Generator` or `list[torch.Generator]`, *optional*): One or a list of [torch generator(s)](https://pytorch.org/docs/stable/generated/torch.Generator.html) to make generation deterministic. latents (`torch.Tensor`, *optional*): Pre-generated noisy latents, sampled from a Gaussian distribution, to be used as inputs for image generation. Can be used to tweak the same generation with different prompts. If not provided, a latents tensor will be generated by sampling using the supplied random `generator`. prompt_embeds (`torch.Tensor`, *optional*): Pre-generated text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt weighting. If not provided, text embeddings will be generated from `prompt` input argument. negative_prompt_embeds (`torch.Tensor`, *optional*): Pre-generated negative text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt weighting. If not provided, negative_prompt_embeds will be generated from `negative_prompt` input argument. output_type (`str`, *optional*, defaults to `"pil"`): The output format of the generate image. Choose between [PIL](https://pillow.readthedocs.io/en/stable/): `PIL.Image.Image` or `np.array`. return_dict (`bool`, *optional*, defaults to `True`): Whether or not to return a [`~pipelines.qwenimage.QwenImagePipelineOutput`] instead of a plain tuple. attention_kwargs (`dict`, *optional*): A kwargs dictionary that if specified is passed along to the `AttentionProcessor` as defined under `self.processor` in [diffusers.models.attention_processor](https://github.com/huggingface/diffusers/blob/main/src/diffusers/models/attention_processor.py). callback_on_step_end (`Callable`, *optional*): A function that calls at the end of each denoising steps during the inference. The function is called with the following arguments: `callback_on_step_end(self: DiffusionPipeline, step: int, timestep: int, callback_kwargs: Dict)`. `callback_kwargs` will include a list of all tensors as specified by `callback_on_step_end_tensor_inputs`. callback_on_step_end_tensor_inputs (`list`, *optional*): The list of tensor inputs for the `callback_on_step_end` function. The tensors specified in the list will be passed as `callback_kwargs` argument. You will only be able to include variables listed in the `._callback_tensor_inputs` attribute of your pipeline class. max_sequence_length (`int` defaults to 512): Maximum sequence length to use with the `prompt`. Examples: Returns: [`~pipelines.qwenimage.QwenImagePipelineOutput`] or `tuple`: [`~pipelines.qwenimage.QwenImagePipelineOutput`] if `return_dict` is True, otherwise a `tuple`. When returning a tuple, the first element is a list with the generated images. """ image_size = image[-1].size if isinstance(image, list) else image.size calculated_width, calculated_height = calculate_dimensions(1024 * 1024, image_size[0] / image_size[1]) height = height or calculated_height width = width or calculated_width multiple_of = self.vae_scale_factor * 2 width = width // multiple_of * multiple_of height = height // multiple_of * multiple_of # 1. Check inputs. Raise error if not correct self.check_inputs( prompt, height, width, negative_prompt=negative_prompt, prompt_embeds=prompt_embeds, negative_prompt_embeds=negative_prompt_embeds, prompt_embeds_mask=prompt_embeds_mask, negative_prompt_embeds_mask=negative_prompt_embeds_mask, callback_on_step_end_tensor_inputs=callback_on_step_end_tensor_inputs, max_sequence_length=max_sequence_length, ) self._guidance_scale = guidance_scale self._attention_kwargs = attention_kwargs self._current_timestep = None self._interrupt = False # 2. Define call parameters if prompt is not None and isinstance(prompt, str): batch_size = 1 elif prompt is not None and isinstance(prompt, list): batch_size = len(prompt) else: batch_size = prompt_embeds.shape[0] # QwenImageEditPlusPipeline does not currently support batch_size > 1 if batch_size > 1: raise ValueError( f"QwenImageEditPlusPipeline currently only supports batch_size=1, but received batch_size={batch_size}. " "Please process prompts one at a time." ) device = self._execution_device # 3. Preprocess image if image is not None and not (isinstance(image, torch.Tensor) and image.size(1) == self.latent_channels): if not isinstance(image, list): image = [image] condition_image_sizes = [] condition_images = [] vae_image_sizes = [] vae_images = [] for img in image: image_width, image_height = img.size condition_width, condition_height = calculate_dimensions( CONDITION_IMAGE_SIZE, image_width / image_height ) vae_width, vae_height = calculate_dimensions(VAE_IMAGE_SIZE, image_width / image_height) condition_image_sizes.append((condition_width, condition_height)) vae_image_sizes.append((vae_width, vae_height)) condition_images.append(self.image_processor.resize(img, condition_height, condition_width)) vae_images.append(self.image_processor.preprocess(img, vae_height, vae_width).unsqueeze(2)) has_neg_prompt = negative_prompt is not None or ( negative_prompt_embeds is not None and negative_prompt_embeds_mask is not None ) if true_cfg_scale > 1 and not has_neg_prompt: logger.warning( f"true_cfg_scale is passed as {true_cfg_scale}, but classifier-free guidance is not enabled since no negative_prompt is provided." ) elif true_cfg_scale <= 1 and has_neg_prompt: logger.warning( " negative_prompt is passed but classifier-free guidance is not enabled since true_cfg_scale <= 1" ) do_true_cfg = true_cfg_scale > 1 and has_neg_prompt prompt_embeds, prompt_embeds_mask = self.encode_prompt( image=condition_images, prompt=prompt, prompt_embeds=prompt_embeds, prompt_embeds_mask=prompt_embeds_mask, device=device, num_images_per_prompt=num_images_per_prompt, max_sequence_length=max_sequence_length, ) if do_true_cfg: negative_prompt_embeds, negative_prompt_embeds_mask = self.encode_prompt( image=condition_images, prompt=negative_prompt, prompt_embeds=negative_prompt_embeds, prompt_embeds_mask=negative_prompt_embeds_mask, device=device, num_images_per_prompt=num_images_per_prompt, max_sequence_length=max_sequence_length, ) # 4. Prepare latent variables num_channels_latents = self.transformer.config.in_channels // 4 latents, image_latents = self.prepare_latents( vae_images, batch_size * num_images_per_prompt, num_channels_latents, height, width, prompt_embeds.dtype, device, generator, latents, ) img_shapes = [ [ (1, height // self.vae_scale_factor // 2, width // self.vae_scale_factor // 2), *[ (1, vae_height // self.vae_scale_factor // 2, vae_width // self.vae_scale_factor // 2) for vae_width, vae_height in vae_image_sizes ], ] ] * batch_size # 5. Prepare timesteps sigmas = np.linspace(1.0, 1 / num_inference_steps, num_inference_steps) if sigmas is None else sigmas image_seq_len = latents.shape[1] mu = calculate_shift( image_seq_len, self.scheduler.config.get("base_image_seq_len", 256), self.scheduler.config.get("max_image_seq_len", 4096), self.scheduler.config.get("base_shift", 0.5), self.scheduler.config.get("max_shift", 1.15), ) timesteps, num_inference_steps = retrieve_timesteps( self.scheduler, num_inference_steps, device, sigmas=sigmas, mu=mu, ) num_warmup_steps = max(len(timesteps) - num_inference_steps * self.scheduler.order, 0) self._num_timesteps = len(timesteps) # handle guidance if self.transformer.config.guidance_embeds and guidance_scale is None: raise ValueError("guidance_scale is required for guidance-distilled model.") elif self.transformer.config.guidance_embeds: guidance = torch.full([1], guidance_scale, device=device, dtype=torch.float32) guidance = guidance.expand(latents.shape[0]) elif not self.transformer.config.guidance_embeds and guidance_scale is not None: logger.warning( f"guidance_scale is passed as {guidance_scale}, but ignored since the model is not guidance-distilled." ) guidance = None elif not self.transformer.config.guidance_embeds and guidance_scale is None: guidance = None if self.attention_kwargs is None: self._attention_kwargs = {} # 6. Denoising loop self.scheduler.set_begin_index(0) with self.progress_bar(total=num_inference_steps) as progress_bar: for i, t in enumerate(timesteps): if self.interrupt: continue self._current_timestep = t latent_model_input = latents if image_latents is not None: latent_model_input = torch.cat([latents, image_latents], dim=1) # broadcast to batch dimension in a way that's compatible with ONNX/Core ML timestep = t.expand(latents.shape[0]).to(latents.dtype) with self.transformer.cache_context("cond"): noise_pred = self.transformer( hidden_states=latent_model_input, timestep=timestep / 1000, guidance=guidance, encoder_hidden_states_mask=prompt_embeds_mask, encoder_hidden_states=prompt_embeds, img_shapes=img_shapes, attention_kwargs=self.attention_kwargs, return_dict=False, )[0] noise_pred = noise_pred[:, : latents.size(1)] if do_true_cfg: with self.transformer.cache_context("uncond"): neg_noise_pred = self.transformer( hidden_states=latent_model_input, timestep=timestep / 1000, guidance=guidance, encoder_hidden_states_mask=negative_prompt_embeds_mask, encoder_hidden_states=negative_prompt_embeds, img_shapes=img_shapes, attention_kwargs=self.attention_kwargs, return_dict=False, )[0] neg_noise_pred = neg_noise_pred[:, : latents.size(1)] comb_pred = neg_noise_pred + true_cfg_scale * (noise_pred - neg_noise_pred) cond_norm = torch.norm(noise_pred, dim=-1, keepdim=True) noise_norm = torch.norm(comb_pred, dim=-1, keepdim=True) noise_pred = comb_pred * (cond_norm / noise_norm) # compute the previous noisy sample x_t -> x_t-1 latents_dtype = latents.dtype latents = self.scheduler.step(noise_pred, t, latents, return_dict=False)[0] if latents.dtype != latents_dtype: if torch.backends.mps.is_available(): # some platforms (eg. apple mps) misbehave due to a pytorch bug: https://github.com/pytorch/pytorch/pull/99272 latents = latents.to(latents_dtype) if callback_on_step_end is not None: callback_kwargs = {} for k in callback_on_step_end_tensor_inputs: callback_kwargs[k] = locals()[k] callback_outputs = callback_on_step_end(self, i, t, callback_kwargs) latents = callback_outputs.pop("latents", latents) prompt_embeds = callback_outputs.pop("prompt_embeds", prompt_embeds) # call the callback, if provided if i == len(timesteps) - 1 or ((i + 1) > num_warmup_steps and (i + 1) % self.scheduler.order == 0): progress_bar.update() if XLA_AVAILABLE: xm.mark_step() self._current_timestep = None if output_type == "latent": image = latents else: latents = self._unpack_latents(latents, height, width, self.vae_scale_factor) latents = latents.to(self.vae.dtype) latents_mean = ( torch.tensor(self.vae.config.latents_mean) .view(1, self.vae.config.z_dim, 1, 1, 1) .to(latents.device, latents.dtype) ) latents_std = 1.0 / torch.tensor(self.vae.config.latents_std).view(1, self.vae.config.z_dim, 1, 1, 1).to( latents.device, latents.dtype ) latents = latents / latents_std + latents_mean image = self.vae.decode(latents, return_dict=False)[0][:, :, 0] image = self.image_processor.postprocess(image, output_type=output_type) # Offload all models self.maybe_free_model_hooks() if not return_dict: return (image,) return QwenImagePipelineOutput(images=image)
{ "repo_id": "huggingface/diffusers", "file_path": "src/diffusers/pipelines/qwenimage/pipeline_qwenimage_edit_plus.py", "license": "Apache License 2.0", "lines": 779, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
huggingface/diffusers:examples/server-async/Pipelines.py
import logging import os from dataclasses import dataclass, field from typing import List import torch from pydantic import BaseModel from diffusers.pipelines.stable_diffusion_3.pipeline_stable_diffusion_3 import StableDiffusion3Pipeline logger = logging.getLogger(__name__) class TextToImageInput(BaseModel): model: str prompt: str size: str | None = None n: int | None = None @dataclass class PresetModels: SD3: List[str] = field(default_factory=lambda: ["stabilityai/stable-diffusion-3-medium"]) SD3_5: List[str] = field( default_factory=lambda: [ "stabilityai/stable-diffusion-3.5-large", "stabilityai/stable-diffusion-3.5-large-turbo", "stabilityai/stable-diffusion-3.5-medium", ] ) class TextToImagePipelineSD3: def __init__(self, model_path: str | None = None): self.model_path = model_path or os.getenv("MODEL_PATH") self.pipeline: StableDiffusion3Pipeline | None = None self.device: str | None = None def start(self): if torch.cuda.is_available(): model_path = self.model_path or "stabilityai/stable-diffusion-3.5-large" logger.info("Loading CUDA") self.device = "cuda" self.pipeline = StableDiffusion3Pipeline.from_pretrained( model_path, torch_dtype=torch.float16, ).to(device=self.device) elif torch.backends.mps.is_available(): model_path = self.model_path or "stabilityai/stable-diffusion-3.5-medium" logger.info("Loading MPS for Mac M Series") self.device = "mps" self.pipeline = StableDiffusion3Pipeline.from_pretrained( model_path, torch_dtype=torch.bfloat16, ).to(device=self.device) else: raise Exception("No CUDA or MPS device available") class ModelPipelineInitializer: def __init__(self, model: str = "", type_models: str = "t2im"): self.model = model self.type_models = type_models self.pipeline = None self.device = "cuda" if torch.cuda.is_available() else "mps" self.model_type = None def initialize_pipeline(self): if not self.model: raise ValueError("Model name not provided") # Check if model exists in PresetModels preset_models = PresetModels() # Determine which model type we're dealing with if self.model in preset_models.SD3: self.model_type = "SD3" elif self.model in preset_models.SD3_5: self.model_type = "SD3_5" # Create appropriate pipeline based on model type and type_models if self.type_models == "t2im": if self.model_type in ["SD3", "SD3_5"]: self.pipeline = TextToImagePipelineSD3(self.model) else: raise ValueError(f"Model type {self.model_type} not supported for text-to-image") elif self.type_models == "t2v": raise ValueError(f"Unsupported type_models: {self.type_models}") return self.pipeline
{ "repo_id": "huggingface/diffusers", "file_path": "examples/server-async/Pipelines.py", "license": "Apache License 2.0", "lines": 73, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
function_complex
huggingface/diffusers:examples/server-async/serverasync.py
import asyncio import gc import logging import os import random import threading from contextlib import asynccontextmanager from dataclasses import dataclass from typing import Any, Dict, Optional, Type import torch from fastapi import FastAPI, HTTPException, Request from fastapi.concurrency import run_in_threadpool from fastapi.middleware.cors import CORSMiddleware from fastapi.responses import FileResponse from Pipelines import ModelPipelineInitializer from pydantic import BaseModel from utils import RequestScopedPipeline, Utils @dataclass class ServerConfigModels: model: str = "stabilityai/stable-diffusion-3.5-medium" type_models: str = "t2im" constructor_pipeline: Optional[Type] = None custom_pipeline: Optional[Type] = None components: Optional[Dict[str, Any]] = None torch_dtype: Optional[torch.dtype] = None host: str = "0.0.0.0" port: int = 8500 server_config = ServerConfigModels() @asynccontextmanager async def lifespan(app: FastAPI): logging.basicConfig(level=logging.INFO) app.state.logger = logging.getLogger("diffusers-server") os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "max_split_size_mb:128,expandable_segments:True" os.environ["CUDA_LAUNCH_BLOCKING"] = "0" app.state.total_requests = 0 app.state.active_inferences = 0 app.state.metrics_lock = asyncio.Lock() app.state.metrics_task = None app.state.utils_app = Utils( host=server_config.host, port=server_config.port, ) async def metrics_loop(): try: while True: async with app.state.metrics_lock: total = app.state.total_requests active = app.state.active_inferences app.state.logger.info(f"[METRICS] total_requests={total} active_inferences={active}") await asyncio.sleep(5) except asyncio.CancelledError: app.state.logger.info("Metrics loop cancelled") raise app.state.metrics_task = asyncio.create_task(metrics_loop()) try: yield finally: task = app.state.metrics_task if task: task.cancel() try: await task except asyncio.CancelledError: pass try: stop_fn = getattr(model_pipeline, "stop", None) or getattr(model_pipeline, "close", None) if callable(stop_fn): await run_in_threadpool(stop_fn) except Exception as e: app.state.logger.warning(f"Error during pipeline shutdown: {e}") app.state.logger.info("Lifespan shutdown complete") app = FastAPI(lifespan=lifespan) logger = logging.getLogger("DiffusersServer.Pipelines") initializer = ModelPipelineInitializer( model=server_config.model, type_models=server_config.type_models, ) model_pipeline = initializer.initialize_pipeline() model_pipeline.start() request_pipe = RequestScopedPipeline(model_pipeline.pipeline) pipeline_lock = threading.Lock() logger.info(f"Pipeline initialized and ready to receive requests (model ={server_config.model})") app.state.MODEL_INITIALIZER = initializer app.state.MODEL_PIPELINE = model_pipeline app.state.REQUEST_PIPE = request_pipe app.state.PIPELINE_LOCK = pipeline_lock class JSONBodyQueryAPI(BaseModel): model: str | None = None prompt: str negative_prompt: str | None = None num_inference_steps: int = 28 num_images_per_prompt: int = 1 @app.middleware("http") async def count_requests_middleware(request: Request, call_next): async with app.state.metrics_lock: app.state.total_requests += 1 response = await call_next(request) return response @app.get("/") async def root(): return {"message": "Welcome to the Diffusers Server"} @app.post("/api/diffusers/inference") async def api(json: JSONBodyQueryAPI): prompt = json.prompt negative_prompt = json.negative_prompt or "" num_steps = json.num_inference_steps num_images_per_prompt = json.num_images_per_prompt wrapper = app.state.MODEL_PIPELINE initializer = app.state.MODEL_INITIALIZER utils_app = app.state.utils_app if not wrapper or not wrapper.pipeline: raise HTTPException(500, "Model not initialized correctly") if not prompt.strip(): raise HTTPException(400, "No prompt provided") def make_generator(): g = torch.Generator(device=initializer.device) return g.manual_seed(random.randint(0, 10_000_000)) req_pipe = app.state.REQUEST_PIPE def infer(): gen = make_generator() return req_pipe.generate( prompt=prompt, negative_prompt=negative_prompt, generator=gen, num_inference_steps=num_steps, num_images_per_prompt=num_images_per_prompt, device=initializer.device, output_type="pil", ) try: async with app.state.metrics_lock: app.state.active_inferences += 1 output = await run_in_threadpool(infer) async with app.state.metrics_lock: app.state.active_inferences = max(0, app.state.active_inferences - 1) urls = [utils_app.save_image(img) for img in output.images] return {"response": urls} except Exception as e: async with app.state.metrics_lock: app.state.active_inferences = max(0, app.state.active_inferences - 1) logger.error(f"Error during inference: {e}") raise HTTPException(500, f"Error in processing: {e}") finally: if torch.cuda.is_available(): torch.cuda.synchronize() torch.cuda.empty_cache() torch.cuda.reset_peak_memory_stats() torch.cuda.ipc_collect() gc.collect() @app.get("/images/{filename}") async def serve_image(filename: str): utils_app = app.state.utils_app file_path = os.path.join(utils_app.image_dir, filename) if not os.path.isfile(file_path): raise HTTPException(status_code=404, detail="Image not found") return FileResponse(file_path, media_type="image/png") @app.get("/api/status") async def get_status(): memory_info = {} if torch.cuda.is_available(): memory_allocated = torch.cuda.memory_allocated() / 1024**3 # GB memory_reserved = torch.cuda.memory_reserved() / 1024**3 # GB memory_info = { "memory_allocated_gb": round(memory_allocated, 2), "memory_reserved_gb": round(memory_reserved, 2), "device": torch.cuda.get_device_name(0), } return {"current_model": server_config.model, "type_models": server_config.type_models, "memory": memory_info} app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) if __name__ == "__main__": import uvicorn uvicorn.run(app, host=server_config.host, port=server_config.port)
{ "repo_id": "huggingface/diffusers", "file_path": "examples/server-async/serverasync.py", "license": "Apache License 2.0", "lines": 178, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
function_complex
huggingface/diffusers:examples/server-async/test.py
import os import time import urllib.parse import requests SERVER_URL = "http://localhost:8500/api/diffusers/inference" BASE_URL = "http://localhost:8500" DOWNLOAD_FOLDER = "generated_images" WAIT_BEFORE_DOWNLOAD = 2 # seconds os.makedirs(DOWNLOAD_FOLDER, exist_ok=True) def save_from_url(url: str) -> str: """Download the given URL (relative or absolute) and save it locally.""" if url.startswith("/"): direct = BASE_URL.rstrip("/") + url else: direct = url resp = requests.get(direct, timeout=60) resp.raise_for_status() filename = os.path.basename(urllib.parse.urlparse(direct).path) or f"img_{int(time.time())}.png" path = os.path.join(DOWNLOAD_FOLDER, filename) with open(path, "wb") as f: f.write(resp.content) return path def main(): payload = { "prompt": "The T-800 Terminator Robot Returning From The Future, Anime Style", "num_inference_steps": 30, "num_images_per_prompt": 1, } print("Sending request...") try: r = requests.post(SERVER_URL, json=payload, timeout=480) r.raise_for_status() except Exception as e: print(f"Request failed: {e}") return body = r.json().get("response", []) # Normalize to a list urls = body if isinstance(body, list) else [body] if body else [] if not urls: print("No URLs found in the response. Check the server output.") return print(f"Received {len(urls)} URL(s). Waiting {WAIT_BEFORE_DOWNLOAD}s before downloading...") time.sleep(WAIT_BEFORE_DOWNLOAD) for u in urls: try: path = save_from_url(u) print(f"Image saved to: {path}") except Exception as e: print(f"Error downloading {u}: {e}") if __name__ == "__main__": main()
{ "repo_id": "huggingface/diffusers", "file_path": "examples/server-async/test.py", "license": "Apache License 2.0", "lines": 51, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
function_simple
huggingface/diffusers:examples/server-async/utils/requestscopedpipeline.py
import copy import threading from typing import Any, Iterable, List, Optional import torch from diffusers.utils import logging from .scheduler import BaseAsyncScheduler, async_retrieve_timesteps from .wrappers import ThreadSafeImageProcessorWrapper, ThreadSafeTokenizerWrapper, ThreadSafeVAEWrapper logger = logging.get_logger(__name__) class RequestScopedPipeline: DEFAULT_MUTABLE_ATTRS = [ "_all_hooks", "_offload_device", "_progress_bar_config", "_progress_bar", "_rng_state", "_last_seed", "latents", ] def __init__( self, pipeline: Any, mutable_attrs: Optional[Iterable[str]] = None, auto_detect_mutables: bool = True, tensor_numel_threshold: int = 1_000_000, tokenizer_lock: Optional[threading.Lock] = None, wrap_scheduler: bool = True, ): self._base = pipeline self.unet = getattr(pipeline, "unet", None) self.vae = getattr(pipeline, "vae", None) self.text_encoder = getattr(pipeline, "text_encoder", None) self.components = getattr(pipeline, "components", None) self.transformer = getattr(pipeline, "transformer", None) if wrap_scheduler and hasattr(pipeline, "scheduler") and pipeline.scheduler is not None: if not isinstance(pipeline.scheduler, BaseAsyncScheduler): pipeline.scheduler = BaseAsyncScheduler(pipeline.scheduler) self._mutable_attrs = list(mutable_attrs) if mutable_attrs is not None else list(self.DEFAULT_MUTABLE_ATTRS) self._tokenizer_lock = tokenizer_lock if tokenizer_lock is not None else threading.Lock() self._vae_lock = threading.Lock() self._image_lock = threading.Lock() self._auto_detect_mutables = bool(auto_detect_mutables) self._tensor_numel_threshold = int(tensor_numel_threshold) self._auto_detected_attrs: List[str] = [] def _detect_kernel_pipeline(self, pipeline) -> bool: kernel_indicators = [ "text_encoding_cache", "memory_manager", "enable_optimizations", "_create_request_context", "get_optimization_stats", ] return any(hasattr(pipeline, attr) for attr in kernel_indicators) def _make_local_scheduler(self, num_inference_steps: int, device: str | None = None, **clone_kwargs): base_sched = getattr(self._base, "scheduler", None) if base_sched is None: return None if not isinstance(base_sched, BaseAsyncScheduler): wrapped_scheduler = BaseAsyncScheduler(base_sched) else: wrapped_scheduler = base_sched try: return wrapped_scheduler.clone_for_request( num_inference_steps=num_inference_steps, device=device, **clone_kwargs ) except Exception as e: logger.debug(f"clone_for_request failed: {e}; trying shallow copy fallback") try: if hasattr(wrapped_scheduler, "scheduler"): try: copied_scheduler = copy.copy(wrapped_scheduler.scheduler) return BaseAsyncScheduler(copied_scheduler) except Exception: return wrapped_scheduler else: copied_scheduler = copy.copy(wrapped_scheduler) return BaseAsyncScheduler(copied_scheduler) except Exception as e2: logger.warning( f"Shallow copy of scheduler also failed: {e2}. Using original scheduler (*thread-unsafe but functional*)." ) return wrapped_scheduler def _autodetect_mutables(self, max_attrs: int = 40): if not self._auto_detect_mutables: return [] if self._auto_detected_attrs: return self._auto_detected_attrs candidates: List[str] = [] seen = set() for name in dir(self._base): if name.startswith("__"): continue if name in self._mutable_attrs: continue if name in ("to", "save_pretrained", "from_pretrained"): continue try: val = getattr(self._base, name) except Exception: continue import types if callable(val) or isinstance(val, (types.ModuleType, types.FunctionType, types.MethodType)): continue if isinstance(val, (dict, list, set, tuple, bytearray)): candidates.append(name) seen.add(name) else: # try Tensor detection try: if isinstance(val, torch.Tensor): if val.numel() <= self._tensor_numel_threshold: candidates.append(name) seen.add(name) else: logger.debug(f"Ignoring large tensor attr '{name}', numel={val.numel()}") except Exception: continue if len(candidates) >= max_attrs: break self._auto_detected_attrs = candidates logger.debug(f"Autodetected mutable attrs to clone: {self._auto_detected_attrs}") return self._auto_detected_attrs def _is_readonly_property(self, base_obj, attr_name: str) -> bool: try: cls = type(base_obj) descriptor = getattr(cls, attr_name, None) if isinstance(descriptor, property): return descriptor.fset is None if hasattr(descriptor, "__set__") is False and descriptor is not None: return False except Exception: pass return False def _clone_mutable_attrs(self, base, local): attrs_to_clone = list(self._mutable_attrs) attrs_to_clone.extend(self._autodetect_mutables()) EXCLUDE_ATTRS = { "components", } for attr in attrs_to_clone: if attr in EXCLUDE_ATTRS: logger.debug(f"Skipping excluded attr '{attr}'") continue if not hasattr(base, attr): continue if self._is_readonly_property(base, attr): logger.debug(f"Skipping read-only property '{attr}'") continue try: val = getattr(base, attr) except Exception as e: logger.debug(f"Could not getattr('{attr}') on base pipeline: {e}") continue try: if isinstance(val, dict): setattr(local, attr, dict(val)) elif isinstance(val, (list, tuple, set)): setattr(local, attr, list(val)) elif isinstance(val, bytearray): setattr(local, attr, bytearray(val)) else: # small tensors or atomic values if isinstance(val, torch.Tensor): if val.numel() <= self._tensor_numel_threshold: setattr(local, attr, val.clone()) else: # don't clone big tensors, keep reference setattr(local, attr, val) else: try: setattr(local, attr, copy.copy(val)) except Exception: setattr(local, attr, val) except (AttributeError, TypeError) as e: logger.debug(f"Skipping cloning attribute '{attr}' because it is not settable: {e}") continue except Exception as e: logger.debug(f"Unexpected error cloning attribute '{attr}': {e}") continue def _is_tokenizer_component(self, component) -> bool: if component is None: return False tokenizer_methods = ["encode", "decode", "tokenize", "__call__"] has_tokenizer_methods = any(hasattr(component, method) for method in tokenizer_methods) class_name = component.__class__.__name__.lower() has_tokenizer_in_name = "tokenizer" in class_name tokenizer_attrs = ["vocab_size", "pad_token", "eos_token", "bos_token"] has_tokenizer_attrs = any(hasattr(component, attr) for attr in tokenizer_attrs) return has_tokenizer_methods and (has_tokenizer_in_name or has_tokenizer_attrs) def _should_wrap_tokenizers(self) -> bool: return True def generate(self, *args, num_inference_steps: int = 50, device: str | None = None, **kwargs): local_scheduler = self._make_local_scheduler(num_inference_steps=num_inference_steps, device=device) try: local_pipe = copy.copy(self._base) except Exception as e: logger.warning(f"copy.copy(self._base) failed: {e}. Falling back to deepcopy (may increase memory).") local_pipe = copy.deepcopy(self._base) try: if ( hasattr(local_pipe, "vae") and local_pipe.vae is not None and not isinstance(local_pipe.vae, ThreadSafeVAEWrapper) ): local_pipe.vae = ThreadSafeVAEWrapper(local_pipe.vae, self._vae_lock) if ( hasattr(local_pipe, "image_processor") and local_pipe.image_processor is not None and not isinstance(local_pipe.image_processor, ThreadSafeImageProcessorWrapper) ): local_pipe.image_processor = ThreadSafeImageProcessorWrapper( local_pipe.image_processor, self._image_lock ) except Exception as e: logger.debug(f"Could not wrap vae/image_processor: {e}") if local_scheduler is not None: try: timesteps, num_steps, configured_scheduler = async_retrieve_timesteps( local_scheduler.scheduler, num_inference_steps=num_inference_steps, device=device, return_scheduler=True, **{k: v for k, v in kwargs.items() if k in ["timesteps", "sigmas"]}, ) final_scheduler = BaseAsyncScheduler(configured_scheduler) setattr(local_pipe, "scheduler", final_scheduler) except Exception: logger.warning("Could not set scheduler on local pipe; proceeding without replacing scheduler.") self._clone_mutable_attrs(self._base, local_pipe) original_tokenizers = {} if self._should_wrap_tokenizers(): try: for name in dir(local_pipe): if "tokenizer" in name and not name.startswith("_"): tok = getattr(local_pipe, name, None) if tok is not None and self._is_tokenizer_component(tok): if not isinstance(tok, ThreadSafeTokenizerWrapper): original_tokenizers[name] = tok wrapped_tokenizer = ThreadSafeTokenizerWrapper(tok, self._tokenizer_lock) setattr(local_pipe, name, wrapped_tokenizer) if hasattr(local_pipe, "components") and isinstance(local_pipe.components, dict): for key, val in local_pipe.components.items(): if val is None: continue if self._is_tokenizer_component(val): if not isinstance(val, ThreadSafeTokenizerWrapper): original_tokenizers[f"components[{key}]"] = val wrapped_tokenizer = ThreadSafeTokenizerWrapper(val, self._tokenizer_lock) local_pipe.components[key] = wrapped_tokenizer except Exception as e: logger.debug(f"Tokenizer wrapping step encountered an error: {e}") result = None cm = getattr(local_pipe, "model_cpu_offload_context", None) try: if callable(cm): try: with cm(): result = local_pipe(*args, num_inference_steps=num_inference_steps, **kwargs) except TypeError: try: with cm: result = local_pipe(*args, num_inference_steps=num_inference_steps, **kwargs) except Exception as e: logger.debug(f"model_cpu_offload_context usage failed: {e}. Proceeding without it.") result = local_pipe(*args, num_inference_steps=num_inference_steps, **kwargs) else: result = local_pipe(*args, num_inference_steps=num_inference_steps, **kwargs) return result finally: try: for name, tok in original_tokenizers.items(): if name.startswith("components["): key = name[len("components[") : -1] if hasattr(local_pipe, "components") and isinstance(local_pipe.components, dict): local_pipe.components[key] = tok else: setattr(local_pipe, name, tok) except Exception as e: logger.debug(f"Error restoring original tokenizers: {e}")
{ "repo_id": "huggingface/diffusers", "file_path": "examples/server-async/utils/requestscopedpipeline.py", "license": "Apache License 2.0", "lines": 278, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
function_complex
huggingface/diffusers:examples/server-async/utils/scheduler.py
import copy import inspect from typing import Any, List, Optional, Union import torch class BaseAsyncScheduler: def __init__(self, scheduler: Any): self.scheduler = scheduler def __getattr__(self, name: str): if hasattr(self.scheduler, name): return getattr(self.scheduler, name) raise AttributeError(f"'{self.__class__.__name__}' object has no attribute '{name}'") def __setattr__(self, name: str, value): if name == "scheduler": super().__setattr__(name, value) else: if hasattr(self, "scheduler") and hasattr(self.scheduler, name): setattr(self.scheduler, name, value) else: super().__setattr__(name, value) def clone_for_request(self, num_inference_steps: int, device: Union[str, torch.device, None] = None, **kwargs): local = copy.deepcopy(self.scheduler) local.set_timesteps(num_inference_steps=num_inference_steps, device=device, **kwargs) cloned = self.__class__(local) return cloned def __repr__(self): return f"BaseAsyncScheduler({repr(self.scheduler)})" def __str__(self): return f"BaseAsyncScheduler wrapping: {str(self.scheduler)}" def async_retrieve_timesteps( scheduler, num_inference_steps: Optional[int] = None, device: Optional[Union[str, torch.device]] = None, timesteps: Optional[List[int]] = None, sigmas: Optional[List[float]] = None, **kwargs, ): r""" Calls the scheduler's `set_timesteps` method and retrieves timesteps from the scheduler after the call. Handles custom timesteps. Any kwargs will be supplied to `scheduler.set_timesteps`. Backwards compatible: by default the function behaves exactly as before and returns (timesteps_tensor, num_inference_steps) If the caller passes `return_scheduler=True` in kwargs, the function will **not** mutate the passed scheduler. Instead it will use a cloned scheduler if available (via `scheduler.clone_for_request`) or a deepcopy fallback, call `set_timesteps` on that cloned scheduler, and return: (timesteps_tensor, num_inference_steps, scheduler_in_use) Args: scheduler (`SchedulerMixin`): The scheduler to get timesteps from. num_inference_steps (`int`): The number of diffusion steps used when generating samples with a pre-trained model. If used, `timesteps` must be `None`. device (`str` or `torch.device`, *optional*): The device to which the timesteps should be moved to. If `None`, the timesteps are not moved. timesteps (`List[int]`, *optional*): Custom timesteps used to override the timestep spacing strategy of the scheduler. If `timesteps` is passed, `num_inference_steps` and `sigmas` must be `None`. sigmas (`List[float]`, *optional*): Custom sigmas used to override the timestep spacing strategy of the scheduler. If `sigmas` is passed, `num_inference_steps` and `timesteps` must be `None`. Optional kwargs: return_scheduler (bool, default False): if True, return (timesteps, num_inference_steps, scheduler_in_use) where `scheduler_in_use` is a scheduler instance that already has timesteps set. This mode will prefer `scheduler.clone_for_request(...)` if available, to avoid mutating the original scheduler. Returns: `(timesteps_tensor, num_inference_steps)` by default (backwards compatible), or `(timesteps_tensor, num_inference_steps, scheduler_in_use)` if `return_scheduler=True`. """ # pop our optional control kwarg (keeps compatibility) return_scheduler = bool(kwargs.pop("return_scheduler", False)) if timesteps is not None and sigmas is not None: raise ValueError("Only one of `timesteps` or `sigmas` can be passed. Please choose one to set custom values") # choose scheduler to call set_timesteps on scheduler_in_use = scheduler if return_scheduler: # Do not mutate the provided scheduler: prefer to clone if possible if hasattr(scheduler, "clone_for_request"): try: # clone_for_request may accept num_inference_steps or other kwargs; be permissive scheduler_in_use = scheduler.clone_for_request( num_inference_steps=num_inference_steps or 0, device=device ) except Exception: scheduler_in_use = copy.deepcopy(scheduler) else: # fallback deepcopy (scheduler tends to be smallish - acceptable) scheduler_in_use = copy.deepcopy(scheduler) # helper to test if set_timesteps supports a particular kwarg def _accepts(param_name: str) -> bool: try: return param_name in set(inspect.signature(scheduler_in_use.set_timesteps).parameters.keys()) except (ValueError, TypeError): # if signature introspection fails, be permissive and attempt the call later return False # now call set_timesteps on the chosen scheduler_in_use (may be original or clone) if timesteps is not None: accepts_timesteps = _accepts("timesteps") if not accepts_timesteps: raise ValueError( f"The current scheduler class {scheduler_in_use.__class__}'s `set_timesteps` does not support custom" f" timestep schedules. Please check whether you are using the correct scheduler." ) scheduler_in_use.set_timesteps(timesteps=timesteps, device=device, **kwargs) timesteps_out = scheduler_in_use.timesteps num_inference_steps = len(timesteps_out) elif sigmas is not None: accept_sigmas = _accepts("sigmas") if not accept_sigmas: raise ValueError( f"The current scheduler class {scheduler_in_use.__class__}'s `set_timesteps` does not support custom" f" sigmas schedules. Please check whether you are using the correct scheduler." ) scheduler_in_use.set_timesteps(sigmas=sigmas, device=device, **kwargs) timesteps_out = scheduler_in_use.timesteps num_inference_steps = len(timesteps_out) else: # default path scheduler_in_use.set_timesteps(num_inference_steps, device=device, **kwargs) timesteps_out = scheduler_in_use.timesteps if return_scheduler: return timesteps_out, num_inference_steps, scheduler_in_use return timesteps_out, num_inference_steps
{ "repo_id": "huggingface/diffusers", "file_path": "examples/server-async/utils/scheduler.py", "license": "Apache License 2.0", "lines": 121, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
function_complex
huggingface/diffusers:examples/server-async/utils/utils.py
import gc import logging import os import tempfile import uuid import torch logger = logging.getLogger(__name__) class Utils: def __init__(self, host: str = "0.0.0.0", port: int = 8500): self.service_url = f"http://{host}:{port}" self.image_dir = os.path.join(tempfile.gettempdir(), "images") if not os.path.exists(self.image_dir): os.makedirs(self.image_dir) self.video_dir = os.path.join(tempfile.gettempdir(), "videos") if not os.path.exists(self.video_dir): os.makedirs(self.video_dir) def save_image(self, image): if hasattr(image, "to"): try: image = image.to("cpu") except Exception: pass if isinstance(image, torch.Tensor): from torchvision import transforms to_pil = transforms.ToPILImage() image = to_pil(image.squeeze(0).clamp(0, 1)) filename = "img" + str(uuid.uuid4()).split("-")[0] + ".png" image_path = os.path.join(self.image_dir, filename) logger.info(f"Saving image to {image_path}") image.save(image_path, format="PNG", optimize=True) del image gc.collect() if torch.cuda.is_available(): torch.cuda.empty_cache() return os.path.join(self.service_url, "images", filename)
{ "repo_id": "huggingface/diffusers", "file_path": "examples/server-async/utils/utils.py", "license": "Apache License 2.0", "lines": 35, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
function_simple
huggingface/diffusers:src/diffusers/pipelines/lucy/pipeline_lucy_edit.py
# Copyright 2025 The Wan Team and The HuggingFace Team. All rights reserved. # Copyright 2025 The Decart AI Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # # Modifications by Decart AI Team: # - Based on pipeline_wan.py, but with supports receiving a condition video appended to the channel dimension. import html from typing import Any, Callable import regex as re import torch from PIL import Image from transformers import AutoTokenizer, UMT5EncoderModel from ...callbacks import MultiPipelineCallbacks, PipelineCallback from ...loaders import WanLoraLoaderMixin from ...models import AutoencoderKLWan, WanTransformer3DModel from ...schedulers import FlowMatchEulerDiscreteScheduler from ...utils import is_ftfy_available, is_torch_xla_available, logging, replace_example_docstring from ...utils.torch_utils import randn_tensor from ...video_processor import VideoProcessor from ..pipeline_utils import DiffusionPipeline from .pipeline_output import LucyPipelineOutput if is_torch_xla_available(): import torch_xla.core.xla_model as xm XLA_AVAILABLE = True else: XLA_AVAILABLE = False logger = logging.get_logger(__name__) # pylint: disable=invalid-name if is_ftfy_available(): import ftfy EXAMPLE_DOC_STRING = """ Examples: ```python >>> from typing import list >>> import torch >>> from PIL import Image >>> from diffusers import AutoencoderKLWan, LucyEditPipeline >>> from diffusers.utils import export_to_video, load_video >>> # Arguments >>> url = "https://d2drjpuinn46lb.cloudfront.net/painter_original_edit.mp4" >>> prompt = "Change the apron and blouse to a classic clown costume: satin polka-dot jumpsuit in bright primary colors, ruffled white collar, oversized pom-pom buttons, white gloves, oversized red shoes, red foam nose; soft window light from left, eye-level medium shot, natural folds and fabric highlights." >>> negative_prompt = "" >>> num_frames = 81 >>> height = 480 >>> width = 832 >>> # Load video >>> def convert_video(video: list[Image.Image]) -> list[Image.Image]: ... video = load_video(url)[:num_frames] ... video = [video[i].resize((width, height)) for i in range(num_frames)] ... return video >>> video = load_video(url, convert_method=convert_video) >>> # Load model >>> model_id = "decart-ai/Lucy-Edit-Dev" >>> vae = AutoencoderKLWan.from_pretrained(model_id, subfolder="vae", torch_dtype=torch.float32) >>> pipe = LucyEditPipeline.from_pretrained(model_id, vae=vae, torch_dtype=torch.bfloat16) >>> pipe.to("cuda") >>> # Generate video >>> output = pipe( ... prompt=prompt, ... video=video, ... negative_prompt=negative_prompt, ... height=480, ... width=832, ... num_frames=81, ... guidance_scale=5.0, ... ).frames[0] >>> # Export video >>> export_to_video(output, "output.mp4", fps=24) ``` """ def basic_clean(text): text = ftfy.fix_text(text) text = html.unescape(html.unescape(text)) return text.strip() def whitespace_clean(text): text = re.sub(r"\s+", " ", text) text = text.strip() return text def prompt_clean(text): text = whitespace_clean(basic_clean(text)) return text # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion_img2img.retrieve_latents def retrieve_latents( encoder_output: torch.Tensor, generator: torch.Generator | None = None, sample_mode: str = "sample" ): if hasattr(encoder_output, "latent_dist") and sample_mode == "sample": return encoder_output.latent_dist.sample(generator) elif hasattr(encoder_output, "latent_dist") and sample_mode == "argmax": return encoder_output.latent_dist.mode() elif hasattr(encoder_output, "latents"): return encoder_output.latents else: raise AttributeError("Could not access latents of provided encoder_output") class LucyEditPipeline(DiffusionPipeline, WanLoraLoaderMixin): r""" Pipeline for video-to-video generation using Lucy Edit. This model inherits from [`DiffusionPipeline`]. Check the superclass documentation for the generic methods implemented for all pipelines (downloading, saving, running on a particular device, etc.). Args: tokenizer ([`T5Tokenizer`]): Tokenizer from [T5](https://huggingface.co/docs/transformers/en/model_doc/t5#transformers.T5Tokenizer), specifically the [google/umt5-xxl](https://huggingface.co/google/umt5-xxl) variant. text_encoder ([`T5EncoderModel`]): [T5](https://huggingface.co/docs/transformers/en/model_doc/t5#transformers.T5EncoderModel), specifically the [google/umt5-xxl](https://huggingface.co/google/umt5-xxl) variant. transformer ([`WanTransformer3DModel`]): Conditional Transformer to denoise the input latents. scheduler ([`UniPCMultistepScheduler`]): A scheduler to be used in combination with `transformer` to denoise the encoded image latents. vae ([`AutoencoderKLWan`]): Variational Auto-Encoder (VAE) Model to encode and decode videos to and from latent representations. transformer_2 ([`WanTransformer3DModel`], *optional*): Conditional Transformer to denoise the input latents during the low-noise stage. If provided, enables two-stage denoising where `transformer` handles high-noise stages and `transformer_2` handles low-noise stages. If not provided, only `transformer` is used. boundary_ratio (`float`, *optional*, defaults to `None`): Ratio of total timesteps to use as the boundary for switching between transformers in two-stage denoising. The actual boundary timestep is calculated as `boundary_ratio * num_train_timesteps`. When provided, `transformer` handles timesteps >= boundary_timestep and `transformer_2` handles timesteps < boundary_timestep. If `None`, only `transformer` is used for the entire denoising process. """ model_cpu_offload_seq = "text_encoder->transformer->transformer_2->vae" _callback_tensor_inputs = ["latents", "prompt_embeds", "negative_prompt_embeds"] _optional_components = ["transformer", "transformer_2"] def __init__( self, tokenizer: AutoTokenizer, text_encoder: UMT5EncoderModel, vae: AutoencoderKLWan, scheduler: FlowMatchEulerDiscreteScheduler, transformer: WanTransformer3DModel | None = None, transformer_2: WanTransformer3DModel | None = None, boundary_ratio: float | None = None, expand_timesteps: bool = False, # Wan2.2 ti2v ): super().__init__() self.register_modules( vae=vae, text_encoder=text_encoder, tokenizer=tokenizer, transformer=transformer, scheduler=scheduler, transformer_2=transformer_2, ) self.register_to_config(boundary_ratio=boundary_ratio) self.register_to_config(expand_timesteps=expand_timesteps) self.vae_scale_factor_temporal = self.vae.config.scale_factor_temporal if getattr(self, "vae", None) else 4 self.vae_scale_factor_spatial = self.vae.config.scale_factor_spatial if getattr(self, "vae", None) else 8 self.video_processor = VideoProcessor(vae_scale_factor=self.vae_scale_factor_spatial) # Copied from diffusers.pipelines.wan.pipeline_wan.WanPipeline._get_t5_prompt_embeds def _get_t5_prompt_embeds( self, prompt: str | list[str] = None, num_videos_per_prompt: int = 1, max_sequence_length: int = 226, device: torch.device | None = None, dtype: torch.dtype | None = None, ): device = device or self._execution_device dtype = dtype or self.text_encoder.dtype prompt = [prompt] if isinstance(prompt, str) else prompt prompt = [prompt_clean(u) for u in prompt] batch_size = len(prompt) text_inputs = self.tokenizer( prompt, padding="max_length", max_length=max_sequence_length, truncation=True, add_special_tokens=True, return_attention_mask=True, return_tensors="pt", ) text_input_ids, mask = text_inputs.input_ids, text_inputs.attention_mask seq_lens = mask.gt(0).sum(dim=1).long() prompt_embeds = self.text_encoder(text_input_ids.to(device), mask.to(device)).last_hidden_state prompt_embeds = prompt_embeds.to(dtype=dtype, device=device) prompt_embeds = [u[:v] for u, v in zip(prompt_embeds, seq_lens)] prompt_embeds = torch.stack( [torch.cat([u, u.new_zeros(max_sequence_length - u.size(0), u.size(1))]) for u in prompt_embeds], dim=0 ) # duplicate text embeddings for each generation per prompt, using mps friendly method _, seq_len, _ = prompt_embeds.shape prompt_embeds = prompt_embeds.repeat(1, num_videos_per_prompt, 1) prompt_embeds = prompt_embeds.view(batch_size * num_videos_per_prompt, seq_len, -1) return prompt_embeds # Copied from diffusers.pipelines.wan.pipeline_wan.WanPipeline.encode_prompt def encode_prompt( self, prompt: str | list[str], negative_prompt: str | list[str] | None = None, do_classifier_free_guidance: bool = True, num_videos_per_prompt: int = 1, prompt_embeds: torch.Tensor | None = None, negative_prompt_embeds: torch.Tensor | None = None, max_sequence_length: int = 226, device: torch.device | None = None, dtype: torch.dtype | None = None, ): r""" Encodes the prompt into text encoder hidden states. Args: prompt (`str` or `list[str]`, *optional*): prompt to be encoded negative_prompt (`str` or `list[str]`, *optional*): The prompt or prompts not to guide the image generation. If not defined, one has to pass `negative_prompt_embeds` instead. Ignored when not using guidance (i.e., ignored if `guidance_scale` is less than `1`). do_classifier_free_guidance (`bool`, *optional*, defaults to `True`): Whether to use classifier free guidance or not. num_videos_per_prompt (`int`, *optional*, defaults to 1): Number of videos that should be generated per prompt. torch device to place the resulting embeddings on prompt_embeds (`torch.Tensor`, *optional*): Pre-generated text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt weighting. If not provided, text embeddings will be generated from `prompt` input argument. negative_prompt_embeds (`torch.Tensor`, *optional*): Pre-generated negative text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt weighting. If not provided, negative_prompt_embeds will be generated from `negative_prompt` input argument. device: (`torch.device`, *optional*): torch device dtype: (`torch.dtype`, *optional*): torch dtype """ device = device or self._execution_device prompt = [prompt] if isinstance(prompt, str) else prompt if prompt is not None: batch_size = len(prompt) else: batch_size = prompt_embeds.shape[0] if prompt_embeds is None: prompt_embeds = self._get_t5_prompt_embeds( prompt=prompt, num_videos_per_prompt=num_videos_per_prompt, max_sequence_length=max_sequence_length, device=device, dtype=dtype, ) if do_classifier_free_guidance and negative_prompt_embeds is None: negative_prompt = negative_prompt or "" negative_prompt = batch_size * [negative_prompt] if isinstance(negative_prompt, str) else negative_prompt if prompt is not None and type(prompt) is not type(negative_prompt): raise TypeError( f"`negative_prompt` should be the same type to `prompt`, but got {type(negative_prompt)} !=" f" {type(prompt)}." ) elif batch_size != len(negative_prompt): raise ValueError( f"`negative_prompt`: {negative_prompt} has batch size {len(negative_prompt)}, but `prompt`:" f" {prompt} has batch size {batch_size}. Please make sure that passed `negative_prompt` matches" " the batch size of `prompt`." ) negative_prompt_embeds = self._get_t5_prompt_embeds( prompt=negative_prompt, num_videos_per_prompt=num_videos_per_prompt, max_sequence_length=max_sequence_length, device=device, dtype=dtype, ) return prompt_embeds, negative_prompt_embeds def check_inputs( self, video, prompt, negative_prompt, height, width, prompt_embeds=None, negative_prompt_embeds=None, callback_on_step_end_tensor_inputs=None, guidance_scale_2=None, ): if height % 16 != 0 or width % 16 != 0: raise ValueError(f"`height` and `width` have to be divisible by 16 but are {height} and {width}.") if callback_on_step_end_tensor_inputs is not None and not all( k in self._callback_tensor_inputs for k in callback_on_step_end_tensor_inputs ): raise ValueError( f"`callback_on_step_end_tensor_inputs` has to be in {self._callback_tensor_inputs}, but found {[k for k in callback_on_step_end_tensor_inputs if k not in self._callback_tensor_inputs]}" ) if prompt is not None and prompt_embeds is not None: raise ValueError( f"Cannot forward both `prompt`: {prompt} and `prompt_embeds`: {prompt_embeds}. Please make sure to" " only forward one of the two." ) elif negative_prompt is not None and negative_prompt_embeds is not None: raise ValueError( f"Cannot forward both `negative_prompt`: {negative_prompt} and `negative_prompt_embeds`: {negative_prompt_embeds}. Please make sure to" " only forward one of the two." ) elif prompt is None and prompt_embeds is None: raise ValueError( "Provide either `prompt` or `prompt_embeds`. Cannot leave both `prompt` and `prompt_embeds` undefined." ) elif prompt is not None and (not isinstance(prompt, str) and not isinstance(prompt, list)): raise ValueError(f"`prompt` has to be of type `str` or `list` but is {type(prompt)}") elif negative_prompt is not None and ( not isinstance(negative_prompt, str) and not isinstance(negative_prompt, list) ): raise ValueError(f"`negative_prompt` has to be of type `str` or `list` but is {type(negative_prompt)}") if self.config.boundary_ratio is None and guidance_scale_2 is not None: raise ValueError("`guidance_scale_2` is only supported when the pipeline's `boundary_ratio` is not None.") if video is None: raise ValueError("`video` is required, received None.") def prepare_latents( self, video: torch.Tensor | None = None, batch_size: int = 1, num_channels_latents: int = 16, height: int = 480, width: int = 832, dtype: torch.dtype | None = None, device: torch.device | None = None, generator: torch.Generator | None = None, latents: torch.Tensor | None = None, ) -> tuple[torch.Tensor, torch.Tensor]: if isinstance(generator, list) and len(generator) != batch_size: raise ValueError( f"You have passed a list of generators of length {len(generator)}, but requested an effective batch" f" size of {batch_size}. Make sure the batch size matches the length of the generators." ) num_latent_frames = ( (video.size(2) - 1) // self.vae_scale_factor_temporal + 1 if latents is None else latents.size(1) ) shape = ( batch_size, num_channels_latents, num_latent_frames, height // self.vae_scale_factor_spatial, width // self.vae_scale_factor_spatial, ) # Prepare noise latents if latents is None: latents = randn_tensor(shape, generator=generator, device=device, dtype=dtype) else: latents = latents.to(device) # Prepare condition latents condition_latents = [ retrieve_latents(self.vae.encode(vid.unsqueeze(0)), sample_mode="argmax") for vid in video ] condition_latents = torch.cat(condition_latents, dim=0).to(dtype) latents_mean = ( torch.tensor(self.vae.config.latents_mean).view(1, self.vae.config.z_dim, 1, 1, 1).to(device, dtype) ) latents_std = 1.0 / torch.tensor(self.vae.config.latents_std).view(1, self.vae.config.z_dim, 1, 1, 1).to( device, dtype ) condition_latents = (condition_latents - latents_mean) * latents_std # Check shapes assert latents.shape == condition_latents.shape, ( f"Latents shape {latents.shape} does not match expected shape {condition_latents.shape}. Please check the input." ) return latents, condition_latents @property def guidance_scale(self): return self._guidance_scale @property def do_classifier_free_guidance(self): return self._guidance_scale > 1.0 @property def num_timesteps(self): return self._num_timesteps @property def current_timestep(self): return self._current_timestep @property def interrupt(self): return self._interrupt @property def attention_kwargs(self): return self._attention_kwargs @torch.no_grad() @replace_example_docstring(EXAMPLE_DOC_STRING) def __call__( self, video: list[Image.Image], prompt: str | list[str] = None, negative_prompt: str | list[str] = None, height: int = 480, width: int = 832, num_frames: int = 81, num_inference_steps: int = 50, guidance_scale: float = 5.0, guidance_scale_2: float | None = None, num_videos_per_prompt: int | None = 1, generator: torch.Generator | list[torch.Generator] | None = None, latents: torch.Tensor | None = None, prompt_embeds: torch.Tensor | None = None, negative_prompt_embeds: torch.Tensor | None = None, output_type: str | None = "np", return_dict: bool = True, attention_kwargs: dict[str, Any] | None = None, callback_on_step_end: Callable[[int, int], None] | PipelineCallback | MultiPipelineCallbacks | None = None, callback_on_step_end_tensor_inputs: list[str] = ["latents"], max_sequence_length: int = 512, ): r""" The call function to the pipeline for generation. Args: video (`list[Image.Image]`): The video to use as the condition for the video generation. prompt (`str` or `list[str]`, *optional*): The prompt or prompts to guide the image generation. If not defined, pass `prompt_embeds` instead. negative_prompt (`str` or `list[str]`, *optional*): The prompt or prompts to avoid during image generation. If not defined, pass `negative_prompt_embeds` instead. Ignored when not using guidance (`guidance_scale` < `1`). height (`int`, defaults to `480`): The height in pixels of the generated image. width (`int`, defaults to `832`): The width in pixels of the generated image. num_frames (`int`, defaults to `81`): The number of frames in the generated video. num_inference_steps (`int`, defaults to `50`): The number of denoising steps. More denoising steps usually lead to a higher quality image at the expense of slower inference. guidance_scale (`float`, defaults to `5.0`): Guidance scale as defined in [Classifier-Free Diffusion Guidance](https://huggingface.co/papers/2207.12598). `guidance_scale` is defined as `w` of equation 2. of [Imagen Paper](https://huggingface.co/papers/2205.11487). Guidance scale is enabled by setting `guidance_scale > 1`. Higher guidance scale encourages to generate images that are closely linked to the text `prompt`, usually at the expense of lower image quality. guidance_scale_2 (`float`, *optional*, defaults to `None`): Guidance scale for the low-noise stage transformer (`transformer_2`). If `None` and the pipeline's `boundary_ratio` is not None, uses the same value as `guidance_scale`. Only used when `transformer_2` and the pipeline's `boundary_ratio` are not None. num_videos_per_prompt (`int`, *optional*, defaults to 1): The number of images to generate per prompt. generator (`torch.Generator` or `list[torch.Generator]`, *optional*): A [`torch.Generator`](https://pytorch.org/docs/stable/generated/torch.Generator.html) to make generation deterministic. latents (`torch.Tensor`, *optional*): Pre-generated noisy latents sampled from a Gaussian distribution, to be used as inputs for image generation. Can be used to tweak the same generation with different prompts. If not provided, a latents tensor is generated by sampling using the supplied random `generator`. prompt_embeds (`torch.Tensor`, *optional*): Pre-generated text embeddings. Can be used to easily tweak text inputs (prompt weighting). If not provided, text embeddings are generated from the `prompt` input argument. output_type (`str`, *optional*, defaults to `"np"`): The output format of the generated image. Choose between `PIL.Image` or `np.array`. return_dict (`bool`, *optional*, defaults to `True`): Whether or not to return a [`LucyPipelineOutput`] instead of a plain tuple. attention_kwargs (`dict`, *optional*): A kwargs dictionary that if specified is passed along to the `AttentionProcessor` as defined under `self.processor` in [diffusers.models.attention_processor](https://github.com/huggingface/diffusers/blob/main/src/diffusers/models/attention_processor.py). callback_on_step_end (`Callable`, `PipelineCallback`, `MultiPipelineCallbacks`, *optional*): A function or a subclass of `PipelineCallback` or `MultiPipelineCallbacks` that is called at the end of each denoising step during the inference. with the following arguments: `callback_on_step_end(self: DiffusionPipeline, step: int, timestep: int, callback_kwargs: Dict)`. `callback_kwargs` will include a list of all tensors as specified by `callback_on_step_end_tensor_inputs`. callback_on_step_end_tensor_inputs (`list`, *optional*): The list of tensor inputs for the `callback_on_step_end` function. The tensors specified in the list will be passed as `callback_kwargs` argument. You will only be able to include variables listed in the `._callback_tensor_inputs` attribute of your pipeline class. max_sequence_length (`int`, defaults to `512`): The maximum sequence length of the text encoder. If the prompt is longer than this, it will be truncated. If the prompt is shorter, it will be padded to this length. Examples: Returns: [`~LucyPipelineOutput`] or `tuple`: If `return_dict` is `True`, [`LucyPipelineOutput`] is returned, otherwise a `tuple` is returned where the first element is a list with the generated images and the second element is a list of `bool`s indicating whether the corresponding generated image contains "not-safe-for-work" (nsfw) content. """ if isinstance(callback_on_step_end, (PipelineCallback, MultiPipelineCallbacks)): callback_on_step_end_tensor_inputs = callback_on_step_end.tensor_inputs # 1. Check inputs. Raise error if not correct self.check_inputs( video, prompt, negative_prompt, height, width, prompt_embeds, negative_prompt_embeds, callback_on_step_end_tensor_inputs, guidance_scale_2, ) if num_frames % self.vae_scale_factor_temporal != 1: logger.warning( f"`num_frames - 1` has to be divisible by {self.vae_scale_factor_temporal}. Rounding to the nearest number." ) num_frames = num_frames // self.vae_scale_factor_temporal * self.vae_scale_factor_temporal + 1 num_frames = max(num_frames, 1) if self.config.boundary_ratio is not None and guidance_scale_2 is None: guidance_scale_2 = guidance_scale self._guidance_scale = guidance_scale self._guidance_scale_2 = guidance_scale_2 self._attention_kwargs = attention_kwargs self._current_timestep = None self._interrupt = False device = self._execution_device # 2. Define call parameters if prompt is not None and isinstance(prompt, str): batch_size = 1 elif prompt is not None and isinstance(prompt, list): batch_size = len(prompt) else: batch_size = prompt_embeds.shape[0] # 3. Encode input prompt prompt_embeds, negative_prompt_embeds = self.encode_prompt( prompt=prompt, negative_prompt=negative_prompt, do_classifier_free_guidance=self.do_classifier_free_guidance, num_videos_per_prompt=num_videos_per_prompt, prompt_embeds=prompt_embeds, negative_prompt_embeds=negative_prompt_embeds, max_sequence_length=max_sequence_length, device=device, ) transformer_dtype = self.transformer.dtype if self.transformer is not None else self.transformer_2.dtype prompt_embeds = prompt_embeds.to(transformer_dtype) if negative_prompt_embeds is not None: negative_prompt_embeds = negative_prompt_embeds.to(transformer_dtype) # 4. Prepare timesteps self.scheduler.set_timesteps(num_inference_steps, device=device) timesteps = self.scheduler.timesteps # 5. Prepare latent variables num_channels_latents = ( self.transformer.config.out_channels if self.transformer is not None else self.transformer_2.config.out_channels ) video = self.video_processor.preprocess_video(video, height=height, width=width).to( device, dtype=torch.float32 ) latents, condition_latents = self.prepare_latents( video, batch_size * num_videos_per_prompt, num_channels_latents, height, width, torch.float32, device, generator, latents, ) mask = torch.ones(latents.shape, dtype=torch.float32, device=device) # 6. Denoising loop num_warmup_steps = len(timesteps) - num_inference_steps * self.scheduler.order self._num_timesteps = len(timesteps) if self.config.boundary_ratio is not None: boundary_timestep = self.config.boundary_ratio * self.scheduler.config.num_train_timesteps else: boundary_timestep = None with self.progress_bar(total=num_inference_steps) as progress_bar: for i, t in enumerate(timesteps): if self.interrupt: continue self._current_timestep = t if boundary_timestep is None or t >= boundary_timestep: # wan2.1 or high-noise stage in wan2.2 current_model = self.transformer current_guidance_scale = guidance_scale else: # low-noise stage in wan2.2 current_model = self.transformer_2 current_guidance_scale = guidance_scale_2 # latent_model_input = latents.to(transformer_dtype) latent_model_input = torch.cat([latents, condition_latents], dim=1).to(transformer_dtype) # latent_model_input = torch.cat([latents, latents], dim=1).to(transformer_dtype) if self.config.expand_timesteps: # seq_len: num_latent_frames * latent_height//2 * latent_width//2 temp_ts = (mask[0][0][:, ::2, ::2] * t).flatten() # batch_size, seq_len timestep = temp_ts.unsqueeze(0).expand(latents.shape[0], -1) else: timestep = t.expand(latents.shape[0]) with current_model.cache_context("cond"): noise_pred = current_model( hidden_states=latent_model_input, timestep=timestep, encoder_hidden_states=prompt_embeds, attention_kwargs=attention_kwargs, return_dict=False, )[0] if self.do_classifier_free_guidance: with current_model.cache_context("uncond"): noise_uncond = current_model( hidden_states=latent_model_input, timestep=timestep, encoder_hidden_states=negative_prompt_embeds, attention_kwargs=attention_kwargs, return_dict=False, )[0] noise_pred = noise_uncond + current_guidance_scale * (noise_pred - noise_uncond) # compute the previous noisy sample x_t -> x_t-1 latents = self.scheduler.step(noise_pred, t, latents, return_dict=False)[0] if callback_on_step_end is not None: callback_kwargs = {} for k in callback_on_step_end_tensor_inputs: callback_kwargs[k] = locals()[k] callback_outputs = callback_on_step_end(self, i, t, callback_kwargs) latents = callback_outputs.pop("latents", latents) prompt_embeds = callback_outputs.pop("prompt_embeds", prompt_embeds) negative_prompt_embeds = callback_outputs.pop("negative_prompt_embeds", negative_prompt_embeds) # call the callback, if provided if i == len(timesteps) - 1 or ((i + 1) > num_warmup_steps and (i + 1) % self.scheduler.order == 0): progress_bar.update() if XLA_AVAILABLE: xm.mark_step() self._current_timestep = None if not output_type == "latent": latents = latents.to(self.vae.dtype) latents_mean = ( torch.tensor(self.vae.config.latents_mean) .view(1, self.vae.config.z_dim, 1, 1, 1) .to(latents.device, latents.dtype) ) latents_std = 1.0 / torch.tensor(self.vae.config.latents_std).view(1, self.vae.config.z_dim, 1, 1, 1).to( latents.device, latents.dtype ) latents = latents / latents_std + latents_mean video = self.vae.decode(latents, return_dict=False)[0] video = self.video_processor.postprocess_video(video, output_type=output_type) else: video = latents # Offload all models self.maybe_free_model_hooks() if not return_dict: return (video,) return LucyPipelineOutput(frames=video)
{ "repo_id": "huggingface/diffusers", "file_path": "src/diffusers/pipelines/lucy/pipeline_lucy_edit.py", "license": "Apache License 2.0", "lines": 631, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
huggingface/diffusers:src/diffusers/pipelines/lucy/pipeline_output.py
from dataclasses import dataclass import torch from diffusers.utils import BaseOutput @dataclass class LucyPipelineOutput(BaseOutput): r""" Output class for Lucy pipelines. Args: frames (`torch.Tensor`, `np.ndarray`, or list[list[PIL.Image.Image]]): list of video outputs - It can be a nested list of length `batch_size,` with each sub-list containing denoised PIL image sequences of length `num_frames.` It can also be a NumPy array or Torch tensor of shape `(batch_size, num_frames, channels, height, width)`. """ frames: torch.Tensor
{ "repo_id": "huggingface/diffusers", "file_path": "src/diffusers/pipelines/lucy/pipeline_output.py", "license": "Apache License 2.0", "lines": 14, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
huggingface/diffusers:src/diffusers/pipelines/qwenimage/pipeline_qwenimage_controlnet_inpaint.py
# Copyright 2025 Qwen-Image Team, The InstantX Team and The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import inspect from typing import Any, Callable import numpy as np import torch from transformers import Qwen2_5_VLForConditionalGeneration, Qwen2Tokenizer from ...image_processor import PipelineImageInput, VaeImageProcessor from ...loaders import QwenImageLoraLoaderMixin from ...models import AutoencoderKLQwenImage, QwenImageTransformer2DModel from ...models.controlnets.controlnet_qwenimage import QwenImageControlNetModel, QwenImageMultiControlNetModel from ...schedulers import FlowMatchEulerDiscreteScheduler from ...utils import is_torch_xla_available, logging, replace_example_docstring from ...utils.torch_utils import randn_tensor from ..pipeline_utils import DiffusionPipeline from .pipeline_output import QwenImagePipelineOutput if is_torch_xla_available(): import torch_xla.core.xla_model as xm XLA_AVAILABLE = True else: XLA_AVAILABLE = False logger = logging.get_logger(__name__) # pylint: disable=invalid-name EXAMPLE_DOC_STRING = """ Examples: ```py >>> import torch >>> from diffusers.utils import load_image >>> from diffusers import QwenImageControlNetModel, QwenImageControlNetInpaintPipeline >>> base_model_path = "Qwen/Qwen-Image" >>> controlnet_model_path = "InstantX/Qwen-Image-ControlNet-Inpainting" >>> controlnet = QwenImageControlNetModel.from_pretrained(controlnet_model_path, torch_dtype=torch.bfloat16) >>> pipe = QwenImageControlNetInpaintPipeline.from_pretrained( ... base_model_path, controlnet=controlnet, torch_dtype=torch.bfloat16 ... ).to("cuda") >>> image = load_image( ... "https://huggingface.co/InstantX/Qwen-Image-ControlNet-Inpainting/resolve/main/assets/images/image1.png" ... ) >>> mask_image = load_image( ... "https://huggingface.co/InstantX/Qwen-Image-ControlNet-Inpainting/resolve/main/assets/masks/mask1.png" ... ) >>> prompt = "一辆绿色的出租车行驶在路上" >>> result = pipe( ... prompt=prompt, ... control_image=image, ... control_mask=mask_image, ... controlnet_conditioning_scale=1.0, ... width=mask_image.size[0], ... height=mask_image.size[1], ... true_cfg_scale=4.0, ... ).images[0] >>> image.save("qwenimage_controlnet_inpaint.png") ``` """ # Coped from diffusers.pipelines.qwenimage.pipeline_qwenimage.calculate_shift def calculate_shift( image_seq_len, base_seq_len: int = 256, max_seq_len: int = 4096, base_shift: float = 0.5, max_shift: float = 1.15, ): m = (max_shift - base_shift) / (max_seq_len - base_seq_len) b = base_shift - m * base_seq_len mu = image_seq_len * m + b return mu # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion_img2img.retrieve_latents def retrieve_latents( encoder_output: torch.Tensor, generator: torch.Generator | None = None, sample_mode: str = "sample" ): if hasattr(encoder_output, "latent_dist") and sample_mode == "sample": return encoder_output.latent_dist.sample(generator) elif hasattr(encoder_output, "latent_dist") and sample_mode == "argmax": return encoder_output.latent_dist.mode() elif hasattr(encoder_output, "latents"): return encoder_output.latents else: raise AttributeError("Could not access latents of provided encoder_output") # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.retrieve_timesteps def retrieve_timesteps( scheduler, num_inference_steps: int | None = None, device: str | torch.device | None = None, timesteps: list[int] | None = None, sigmas: list[float] | None = None, **kwargs, ): r""" Calls the scheduler's `set_timesteps` method and retrieves timesteps from the scheduler after the call. Handles custom timesteps. Any kwargs will be supplied to `scheduler.set_timesteps`. Args: scheduler (`SchedulerMixin`): The scheduler to get timesteps from. num_inference_steps (`int`): The number of diffusion steps used when generating samples with a pre-trained model. If used, `timesteps` must be `None`. device (`str` or `torch.device`, *optional*): The device to which the timesteps should be moved to. If `None`, the timesteps are not moved. timesteps (`list[int]`, *optional*): Custom timesteps used to override the timestep spacing strategy of the scheduler. If `timesteps` is passed, `num_inference_steps` and `sigmas` must be `None`. sigmas (`list[float]`, *optional*): Custom sigmas used to override the timestep spacing strategy of the scheduler. If `sigmas` is passed, `num_inference_steps` and `timesteps` must be `None`. Returns: `tuple[torch.Tensor, int]`: A tuple where the first element is the timestep schedule from the scheduler and the second element is the number of inference steps. """ if timesteps is not None and sigmas is not None: raise ValueError("Only one of `timesteps` or `sigmas` can be passed. Please choose one to set custom values") if timesteps is not None: accepts_timesteps = "timesteps" in set(inspect.signature(scheduler.set_timesteps).parameters.keys()) if not accepts_timesteps: raise ValueError( f"The current scheduler class {scheduler.__class__}'s `set_timesteps` does not support custom" f" timestep schedules. Please check whether you are using the correct scheduler." ) scheduler.set_timesteps(timesteps=timesteps, device=device, **kwargs) timesteps = scheduler.timesteps num_inference_steps = len(timesteps) elif sigmas is not None: accept_sigmas = "sigmas" in set(inspect.signature(scheduler.set_timesteps).parameters.keys()) if not accept_sigmas: raise ValueError( f"The current scheduler class {scheduler.__class__}'s `set_timesteps` does not support custom" f" sigmas schedules. Please check whether you are using the correct scheduler." ) scheduler.set_timesteps(sigmas=sigmas, device=device, **kwargs) timesteps = scheduler.timesteps num_inference_steps = len(timesteps) else: scheduler.set_timesteps(num_inference_steps, device=device, **kwargs) timesteps = scheduler.timesteps return timesteps, num_inference_steps class QwenImageControlNetInpaintPipeline(DiffusionPipeline, QwenImageLoraLoaderMixin): r""" The QwenImage pipeline for text-to-image generation. Args: transformer ([`QwenImageTransformer2DModel`]): Conditional Transformer (MMDiT) architecture to denoise the encoded image latents. scheduler ([`FlowMatchEulerDiscreteScheduler`]): A scheduler to be used in combination with `transformer` to denoise the encoded image latents. vae ([`AutoencoderKL`]): Variational Auto-Encoder (VAE) Model to encode and decode images to and from latent representations. text_encoder ([`Qwen2.5-VL-7B-Instruct`]): [Qwen2.5-VL-7B-Instruct](https://huggingface.co/Qwen/Qwen2.5-VL-7B-Instruct), specifically the [Qwen2.5-VL-7B-Instruct](https://huggingface.co/Qwen/Qwen2.5-VL-7B-Instruct) variant. tokenizer (`QwenTokenizer`): Tokenizer of class [CLIPTokenizer](https://huggingface.co/docs/transformers/en/model_doc/clip#transformers.CLIPTokenizer). """ model_cpu_offload_seq = "text_encoder->transformer->vae" _callback_tensor_inputs = ["latents", "prompt_embeds"] def __init__( self, scheduler: FlowMatchEulerDiscreteScheduler, vae: AutoencoderKLQwenImage, text_encoder: Qwen2_5_VLForConditionalGeneration, tokenizer: Qwen2Tokenizer, transformer: QwenImageTransformer2DModel, controlnet: QwenImageControlNetModel, ): super().__init__() self.register_modules( vae=vae, text_encoder=text_encoder, tokenizer=tokenizer, transformer=transformer, scheduler=scheduler, controlnet=controlnet, ) self.vae_scale_factor = 2 ** len(self.vae.temperal_downsample) if getattr(self, "vae", None) else 8 # QwenImage latents are turned into 2x2 patches and packed. This means the latent width and height has to be divisible # by the patch size. So the vae scale factor is multiplied by the patch size to account for this self.image_processor = VaeImageProcessor(vae_scale_factor=self.vae_scale_factor * 2) self.mask_processor = VaeImageProcessor( vae_scale_factor=self.vae_scale_factor * 2, do_resize=True, do_convert_grayscale=True, do_normalize=False, do_binarize=True, ) self.tokenizer_max_length = 1024 self.prompt_template_encode = "<|im_start|>system\nDescribe the image by detailing the color, shape, size, texture, quantity, text, spatial relationships of the objects and background:<|im_end|>\n<|im_start|>user\n{}<|im_end|>\n<|im_start|>assistant\n" self.prompt_template_encode_start_idx = 34 self.default_sample_size = 128 # Coped from diffusers.pipelines.qwenimage.pipeline_qwenimage.extract_masked_hidden def _extract_masked_hidden(self, hidden_states: torch.Tensor, mask: torch.Tensor): bool_mask = mask.bool() valid_lengths = bool_mask.sum(dim=1) selected = hidden_states[bool_mask] split_result = torch.split(selected, valid_lengths.tolist(), dim=0) return split_result # Coped from diffusers.pipelines.qwenimage.pipeline_qwenimage.get_qwen_prompt_embeds def _get_qwen_prompt_embeds( self, prompt: str | list[str] = None, device: torch.device | None = None, dtype: torch.dtype | None = None, ): device = device or self._execution_device dtype = dtype or self.text_encoder.dtype prompt = [prompt] if isinstance(prompt, str) else prompt template = self.prompt_template_encode drop_idx = self.prompt_template_encode_start_idx txt = [template.format(e) for e in prompt] txt_tokens = self.tokenizer( txt, max_length=self.tokenizer_max_length + drop_idx, padding=True, truncation=True, return_tensors="pt" ).to(self.device) encoder_hidden_states = self.text_encoder( input_ids=txt_tokens.input_ids, attention_mask=txt_tokens.attention_mask, output_hidden_states=True, ) hidden_states = encoder_hidden_states.hidden_states[-1] split_hidden_states = self._extract_masked_hidden(hidden_states, txt_tokens.attention_mask) split_hidden_states = [e[drop_idx:] for e in split_hidden_states] attn_mask_list = [torch.ones(e.size(0), dtype=torch.long, device=e.device) for e in split_hidden_states] max_seq_len = max([e.size(0) for e in split_hidden_states]) prompt_embeds = torch.stack( [torch.cat([u, u.new_zeros(max_seq_len - u.size(0), u.size(1))]) for u in split_hidden_states] ) encoder_attention_mask = torch.stack( [torch.cat([u, u.new_zeros(max_seq_len - u.size(0))]) for u in attn_mask_list] ) prompt_embeds = prompt_embeds.to(dtype=dtype, device=device) return prompt_embeds, encoder_attention_mask # Coped from diffusers.pipelines.qwenimage.pipeline_qwenimage.encode_prompt def encode_prompt( self, prompt: str | list[str], device: torch.device | None = None, num_images_per_prompt: int = 1, prompt_embeds: torch.Tensor | None = None, prompt_embeds_mask: torch.Tensor | None = None, max_sequence_length: int = 1024, ): r""" Args: prompt (`str` or `list[str]`, *optional*): prompt to be encoded device: (`torch.device`): torch device num_images_per_prompt (`int`): number of images that should be generated per prompt prompt_embeds (`torch.Tensor`, *optional*): Pre-generated text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt weighting. If not provided, text embeddings will be generated from `prompt` input argument. """ device = device or self._execution_device prompt = [prompt] if isinstance(prompt, str) else prompt batch_size = len(prompt) if prompt_embeds is None else prompt_embeds.shape[0] if prompt_embeds is None: prompt_embeds, prompt_embeds_mask = self._get_qwen_prompt_embeds(prompt, device) _, seq_len, _ = prompt_embeds.shape prompt_embeds = prompt_embeds.repeat(1, num_images_per_prompt, 1) prompt_embeds = prompt_embeds.view(batch_size * num_images_per_prompt, seq_len, -1) prompt_embeds_mask = prompt_embeds_mask.repeat(1, num_images_per_prompt, 1) prompt_embeds_mask = prompt_embeds_mask.view(batch_size * num_images_per_prompt, seq_len) if prompt_embeds_mask is not None and prompt_embeds_mask.all(): prompt_embeds_mask = None return prompt_embeds, prompt_embeds_mask def check_inputs( self, prompt, height, width, negative_prompt=None, prompt_embeds=None, negative_prompt_embeds=None, prompt_embeds_mask=None, negative_prompt_embeds_mask=None, callback_on_step_end_tensor_inputs=None, max_sequence_length=None, ): if height % (self.vae_scale_factor * 2) != 0 or width % (self.vae_scale_factor * 2) != 0: logger.warning( f"`height` and `width` have to be divisible by {self.vae_scale_factor * 2} but are {height} and {width}. Dimensions will be resized accordingly" ) if callback_on_step_end_tensor_inputs is not None and not all( k in self._callback_tensor_inputs for k in callback_on_step_end_tensor_inputs ): raise ValueError( f"`callback_on_step_end_tensor_inputs` has to be in {self._callback_tensor_inputs}, but found {[k for k in callback_on_step_end_tensor_inputs if k not in self._callback_tensor_inputs]}" ) if prompt is not None and prompt_embeds is not None: raise ValueError( f"Cannot forward both `prompt`: {prompt} and `prompt_embeds`: {prompt_embeds}. Please make sure to" " only forward one of the two." ) elif prompt is None and prompt_embeds is None: raise ValueError( "Provide either `prompt` or `prompt_embeds`. Cannot leave both `prompt` and `prompt_embeds` undefined." ) elif prompt is not None and (not isinstance(prompt, str) and not isinstance(prompt, list)): raise ValueError(f"`prompt` has to be of type `str` or `list` but is {type(prompt)}") if negative_prompt is not None and negative_prompt_embeds is not None: raise ValueError( f"Cannot forward both `negative_prompt`: {negative_prompt} and `negative_prompt_embeds`:" f" {negative_prompt_embeds}. Please make sure to only forward one of the two." ) if prompt_embeds is not None and prompt_embeds_mask is None: raise ValueError( "If `prompt_embeds` are provided, `prompt_embeds_mask` also have to be passed. Make sure to generate `prompt_embeds_mask` from the same text encoder that was used to generate `prompt_embeds`." ) if negative_prompt_embeds is not None and negative_prompt_embeds_mask is None: raise ValueError( "If `negative_prompt_embeds` are provided, `negative_prompt_embeds_mask` also have to be passed. Make sure to generate `negative_prompt_embeds_mask` from the same text encoder that was used to generate `negative_prompt_embeds`." ) if max_sequence_length is not None and max_sequence_length > 1024: raise ValueError(f"`max_sequence_length` cannot be greater than 1024 but is {max_sequence_length}") @staticmethod # Copied from diffusers.pipelines.qwenimage.pipeline_qwenimage.QwenImagePipeline._pack_latents def _pack_latents(latents, batch_size, num_channels_latents, height, width): latents = latents.view(batch_size, num_channels_latents, height // 2, 2, width // 2, 2) latents = latents.permute(0, 2, 4, 1, 3, 5) latents = latents.reshape(batch_size, (height // 2) * (width // 2), num_channels_latents * 4) return latents @staticmethod # Copied from diffusers.pipelines.qwenimage.pipeline_qwenimage.QwenImagePipeline._unpack_latents def _unpack_latents(latents, height, width, vae_scale_factor): batch_size, num_patches, channels = latents.shape # VAE applies 8x compression on images but we must also account for packing which requires # latent height and width to be divisible by 2. height = 2 * (int(height) // (vae_scale_factor * 2)) width = 2 * (int(width) // (vae_scale_factor * 2)) latents = latents.view(batch_size, height // 2, width // 2, channels // 4, 2, 2) latents = latents.permute(0, 3, 1, 4, 2, 5) latents = latents.reshape(batch_size, channels // (2 * 2), 1, height, width) return latents def enable_vae_slicing(self): r""" Enable sliced VAE decoding. When this option is enabled, the VAE will split the input tensor in slices to compute decoding in several steps. This is useful to save some memory and allow larger batch sizes. """ self.vae.enable_slicing() def disable_vae_slicing(self): r""" Disable sliced VAE decoding. If `enable_vae_slicing` was previously enabled, this method will go back to computing decoding in one step. """ self.vae.disable_slicing() def enable_vae_tiling(self): r""" Enable tiled VAE decoding. When this option is enabled, the VAE will split the input tensor into tiles to compute decoding and encoding in several steps. This is useful for saving a large amount of memory and to allow processing larger images. """ self.vae.enable_tiling() def disable_vae_tiling(self): r""" Disable tiled VAE decoding. If `enable_vae_tiling` was previously enabled, this method will go back to computing decoding in one step. """ self.vae.disable_tiling() # Copied from diffusers.pipelines.qwenimage.pipeline_qwenimage.QwenImagePipeline.prepare_latents def prepare_latents( self, batch_size, num_channels_latents, height, width, dtype, device, generator, latents=None, ): # VAE applies 8x compression on images but we must also account for packing which requires # latent height and width to be divisible by 2. height = 2 * (int(height) // (self.vae_scale_factor * 2)) width = 2 * (int(width) // (self.vae_scale_factor * 2)) shape = (batch_size, 1, num_channels_latents, height, width) if latents is not None: return latents.to(device=device, dtype=dtype) if isinstance(generator, list) and len(generator) != batch_size: raise ValueError( f"You have passed a list of generators of length {len(generator)}, but requested an effective batch" f" size of {batch_size}. Make sure the batch size matches the length of the generators." ) latents = randn_tensor(shape, generator=generator, device=device, dtype=dtype) latents = self._pack_latents(latents, batch_size, num_channels_latents, height, width) return latents # Copied from diffusers.pipelines.controlnet_sd3.pipeline_stable_diffusion_3_controlnet.StableDiffusion3ControlNetPipeline.prepare_image def prepare_image( self, image, width, height, batch_size, num_images_per_prompt, device, dtype, do_classifier_free_guidance=False, guess_mode=False, ): if isinstance(image, torch.Tensor): pass else: image = self.image_processor.preprocess(image, height=height, width=width) image_batch_size = image.shape[0] if image_batch_size == 1: repeat_by = batch_size else: # image batch size is the same as prompt batch size repeat_by = num_images_per_prompt image = image.repeat_interleave(repeat_by, dim=0) image = image.to(device=device, dtype=dtype) if do_classifier_free_guidance and not guess_mode: image = torch.cat([image] * 2) return image def prepare_image_with_mask( self, image, mask, width, height, batch_size, num_images_per_prompt, device, dtype, do_classifier_free_guidance=False, guess_mode=False, ): if isinstance(image, torch.Tensor): pass else: image = self.image_processor.preprocess(image, height=height, width=width) image_batch_size = image.shape[0] if image_batch_size == 1: repeat_by = batch_size else: # image batch size is the same as prompt batch size repeat_by = num_images_per_prompt image = image.repeat_interleave(repeat_by, dim=0) image = image.to(device=device, dtype=dtype) # (bsz, 3, height_ori, width_ori) # Prepare mask if isinstance(mask, torch.Tensor): pass else: mask = self.mask_processor.preprocess(mask, height=height, width=width) mask = mask.repeat_interleave(repeat_by, dim=0) mask = mask.to(device=device, dtype=dtype) # (bsz, 1, height_ori, width_ori) if image.ndim == 4: image = image.unsqueeze(2) if mask.ndim == 4: mask = mask.unsqueeze(2) # Get masked image masked_image = image.clone() masked_image[(mask > 0.5).repeat(1, 3, 1, 1, 1)] = -1 # (bsz, 3, 1, height_ori, width_ori) self.vae_scale_factor = 2 ** len(self.vae.temperal_downsample) latents_mean = (torch.tensor(self.vae.config.latents_mean).view(1, self.vae.config.z_dim, 1, 1, 1)).to(device) latents_std = 1.0 / torch.tensor(self.vae.config.latents_std).view(1, self.vae.config.z_dim, 1, 1, 1).to( device ) # Encode to latents image_latents = self.vae.encode(masked_image.to(self.vae.dtype)).latent_dist.sample() image_latents = (image_latents - latents_mean) * latents_std image_latents = image_latents.to(dtype) # torch.Size([1, 16, 1, height_ori//8, width_ori//8]) mask = torch.nn.functional.interpolate( mask, size=(image_latents.shape[-3], image_latents.shape[-2], image_latents.shape[-1]) ) mask = 1 - mask # torch.Size([1, 1, 1, height_ori//8, width_ori//8]) control_image = torch.cat( [image_latents, mask], dim=1 ) # torch.Size([1, 16+1, 1, height_ori//8, width_ori//8]) control_image = control_image.permute(0, 2, 1, 3, 4) # torch.Size([1, 1, 16+1, height_ori//8, width_ori//8]) # pack control_image = self._pack_latents( control_image, batch_size=control_image.shape[0], num_channels_latents=control_image.shape[2], height=control_image.shape[3], width=control_image.shape[4], ) if do_classifier_free_guidance and not guess_mode: control_image = torch.cat([control_image] * 2) return control_image @property def guidance_scale(self): return self._guidance_scale @property def attention_kwargs(self): return self._attention_kwargs @property def num_timesteps(self): return self._num_timesteps @property def current_timestep(self): return self._current_timestep @property def interrupt(self): return self._interrupt @torch.no_grad() @replace_example_docstring(EXAMPLE_DOC_STRING) def __call__( self, prompt: str | list[str] = None, negative_prompt: str | list[str] = None, true_cfg_scale: float = 4.0, height: int | None = None, width: int | None = None, num_inference_steps: int = 50, sigmas: list[float] | None = None, guidance_scale: float = 1.0, control_guidance_start: float | list[float] = 0.0, control_guidance_end: float | list[float] = 1.0, control_image: PipelineImageInput = None, control_mask: PipelineImageInput = None, controlnet_conditioning_scale: float | list[float] = 1.0, num_images_per_prompt: int = 1, generator: torch.Generator | list[torch.Generator] | None = None, latents: torch.Tensor | None = None, prompt_embeds: torch.Tensor | None = None, prompt_embeds_mask: torch.Tensor | None = None, negative_prompt_embeds: torch.Tensor | None = None, negative_prompt_embeds_mask: torch.Tensor | None = None, output_type: str | None = "pil", return_dict: bool = True, attention_kwargs: dict[str, Any] | None = None, callback_on_step_end: Callable[[int, int], None] | None = None, callback_on_step_end_tensor_inputs: list[str] = ["latents"], max_sequence_length: int = 512, ): r""" Function invoked when calling the pipeline for generation. Args: prompt (`str` or `list[str]`, *optional*): The prompt or prompts to guide the image generation. If not defined, one has to pass `prompt_embeds`. instead. negative_prompt (`str` or `list[str]`, *optional*): The prompt or prompts not to guide the image generation. If not defined, one has to pass `negative_prompt_embeds` instead. Ignored when not using guidance (i.e., ignored if `true_cfg_scale` is not greater than `1`). true_cfg_scale (`float`, *optional*, defaults to 1.0): When > 1.0 and a provided `negative_prompt`, enables true classifier-free guidance. height (`int`, *optional*, defaults to self.unet.config.sample_size * self.vae_scale_factor): The height in pixels of the generated image. This is set to 1024 by default for the best results. width (`int`, *optional*, defaults to self.unet.config.sample_size * self.vae_scale_factor): The width in pixels of the generated image. This is set to 1024 by default for the best results. num_inference_steps (`int`, *optional*, defaults to 50): The number of denoising steps. More denoising steps usually lead to a higher quality image at the expense of slower inference. sigmas (`list[float]`, *optional*): Custom sigmas to use for the denoising process with schedulers which support a `sigmas` argument in their `set_timesteps` method. If not defined, the default behavior when `num_inference_steps` is passed will be used. guidance_scale (`float`, *optional*, defaults to 3.5): Guidance scale as defined in [Classifier-Free Diffusion Guidance](https://huggingface.co/papers/2207.12598). `guidance_scale` is defined as `w` of equation 2. of [Imagen Paper](https://huggingface.co/papers/2205.11487). Guidance scale is enabled by setting `guidance_scale > 1`. Higher guidance scale encourages to generate images that are closely linked to the text `prompt`, usually at the expense of lower image quality. num_images_per_prompt (`int`, *optional*, defaults to 1): The number of images to generate per prompt. generator (`torch.Generator` or `list[torch.Generator]`, *optional*): One or a list of [torch generator(s)](https://pytorch.org/docs/stable/generated/torch.Generator.html) to make generation deterministic. latents (`torch.Tensor`, *optional*): Pre-generated noisy latents, sampled from a Gaussian distribution, to be used as inputs for image generation. Can be used to tweak the same generation with different prompts. If not provided, a latents tensor will be generated by sampling using the supplied random `generator`. prompt_embeds (`torch.Tensor`, *optional*): Pre-generated text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt weighting. If not provided, text embeddings will be generated from `prompt` input argument. negative_prompt_embeds (`torch.Tensor`, *optional*): Pre-generated negative text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt weighting. If not provided, negative_prompt_embeds will be generated from `negative_prompt` input argument. output_type (`str`, *optional*, defaults to `"pil"`): The output format of the generate image. Choose between [PIL](https://pillow.readthedocs.io/en/stable/): `PIL.Image.Image` or `np.array`. return_dict (`bool`, *optional*, defaults to `True`): Whether or not to return a [`~pipelines.qwenimage.QwenImagePipelineOutput`] instead of a plain tuple. attention_kwargs (`dict`, *optional*): A kwargs dictionary that if specified is passed along to the `AttentionProcessor` as defined under `self.processor` in [diffusers.models.attention_processor](https://github.com/huggingface/diffusers/blob/main/src/diffusers/models/attention_processor.py). callback_on_step_end (`Callable`, *optional*): A function that calls at the end of each denoising steps during the inference. The function is called with the following arguments: `callback_on_step_end(self: DiffusionPipeline, step: int, timestep: int, callback_kwargs: Dict)`. `callback_kwargs` will include a list of all tensors as specified by `callback_on_step_end_tensor_inputs`. callback_on_step_end_tensor_inputs (`list`, *optional*): The list of tensor inputs for the `callback_on_step_end` function. The tensors specified in the list will be passed as `callback_kwargs` argument. You will only be able to include variables listed in the `._callback_tensor_inputs` attribute of your pipeline class. max_sequence_length (`int` defaults to 512): Maximum sequence length to use with the `prompt`. Examples: Returns: [`~pipelines.qwenimage.QwenImagePipelineOutput`] or `tuple`: [`~pipelines.qwenimage.QwenImagePipelineOutput`] if `return_dict` is True, otherwise a `tuple`. When returning a tuple, the first element is a list with the generated images. """ height = height or self.default_sample_size * self.vae_scale_factor width = width or self.default_sample_size * self.vae_scale_factor if not isinstance(control_guidance_start, list) and isinstance(control_guidance_end, list): control_guidance_start = len(control_guidance_end) * [control_guidance_start] elif not isinstance(control_guidance_end, list) and isinstance(control_guidance_start, list): control_guidance_end = len(control_guidance_start) * [control_guidance_end] elif not isinstance(control_guidance_start, list) and not isinstance(control_guidance_end, list): mult = len(control_image) if isinstance(self.controlnet, QwenImageMultiControlNetModel) else 1 control_guidance_start, control_guidance_end = ( mult * [control_guidance_start], mult * [control_guidance_end], ) # 1. Check inputs. Raise error if not correct self.check_inputs( prompt, height, width, negative_prompt=negative_prompt, prompt_embeds=prompt_embeds, negative_prompt_embeds=negative_prompt_embeds, prompt_embeds_mask=prompt_embeds_mask, negative_prompt_embeds_mask=negative_prompt_embeds_mask, callback_on_step_end_tensor_inputs=callback_on_step_end_tensor_inputs, max_sequence_length=max_sequence_length, ) self._guidance_scale = guidance_scale self._attention_kwargs = attention_kwargs self._current_timestep = None self._interrupt = False # 2. Define call parameters if prompt is not None and isinstance(prompt, str): batch_size = 1 elif prompt is not None and isinstance(prompt, list): batch_size = len(prompt) else: batch_size = prompt_embeds.shape[0] device = self._execution_device has_neg_prompt = negative_prompt is not None or ( negative_prompt_embeds is not None and negative_prompt_embeds_mask is not None ) do_true_cfg = true_cfg_scale > 1 and has_neg_prompt prompt_embeds, prompt_embeds_mask = self.encode_prompt( prompt=prompt, prompt_embeds=prompt_embeds, prompt_embeds_mask=prompt_embeds_mask, device=device, num_images_per_prompt=num_images_per_prompt, max_sequence_length=max_sequence_length, ) if do_true_cfg: negative_prompt_embeds, negative_prompt_embeds_mask = self.encode_prompt( prompt=negative_prompt, prompt_embeds=negative_prompt_embeds, prompt_embeds_mask=negative_prompt_embeds_mask, device=device, num_images_per_prompt=num_images_per_prompt, max_sequence_length=max_sequence_length, ) # 3. Prepare control image num_channels_latents = self.transformer.config.in_channels // 4 if isinstance(self.controlnet, QwenImageControlNetModel): control_image = self.prepare_image_with_mask( image=control_image, mask=control_mask, width=width, height=height, batch_size=batch_size * num_images_per_prompt, num_images_per_prompt=num_images_per_prompt, device=device, dtype=self.vae.dtype, ) # 4. Prepare latent variables num_channels_latents = self.transformer.config.in_channels // 4 latents = self.prepare_latents( batch_size * num_images_per_prompt, num_channels_latents, height, width, prompt_embeds.dtype, device, generator, latents, ) img_shapes = [(1, height // self.vae_scale_factor // 2, width // self.vae_scale_factor // 2)] * batch_size # 5. Prepare timesteps sigmas = np.linspace(1.0, 1 / num_inference_steps, num_inference_steps) if sigmas is None else sigmas image_seq_len = latents.shape[1] mu = calculate_shift( image_seq_len, self.scheduler.config.get("base_image_seq_len", 256), self.scheduler.config.get("max_image_seq_len", 4096), self.scheduler.config.get("base_shift", 0.5), self.scheduler.config.get("max_shift", 1.15), ) timesteps, num_inference_steps = retrieve_timesteps( self.scheduler, num_inference_steps, device, sigmas=sigmas, mu=mu, ) num_warmup_steps = max(len(timesteps) - num_inference_steps * self.scheduler.order, 0) self._num_timesteps = len(timesteps) controlnet_keep = [] for i in range(len(timesteps)): keeps = [ 1.0 - float(i / len(timesteps) < s or (i + 1) / len(timesteps) > e) for s, e in zip(control_guidance_start, control_guidance_end) ] controlnet_keep.append(keeps[0] if isinstance(self.controlnet, QwenImageControlNetModel) else keeps) # handle guidance if self.transformer.config.guidance_embeds: guidance = torch.full([1], guidance_scale, device=device, dtype=torch.float32) guidance = guidance.expand(latents.shape[0]) else: guidance = None if self.attention_kwargs is None: self._attention_kwargs = {} # 6. Denoising loop self.scheduler.set_begin_index(0) with self.progress_bar(total=num_inference_steps) as progress_bar: for i, t in enumerate(timesteps): if self.interrupt: continue self._current_timestep = t # broadcast to batch dimension in a way that's compatible with ONNX/Core ML timestep = t.expand(latents.shape[0]).to(latents.dtype) if isinstance(controlnet_keep[i], list): cond_scale = [c * s for c, s in zip(controlnet_conditioning_scale, controlnet_keep[i])] else: controlnet_cond_scale = controlnet_conditioning_scale if isinstance(controlnet_cond_scale, list): controlnet_cond_scale = controlnet_cond_scale[0] cond_scale = controlnet_cond_scale * controlnet_keep[i] # controlnet controlnet_block_samples = self.controlnet( hidden_states=latents, controlnet_cond=control_image.to(dtype=latents.dtype, device=device), conditioning_scale=cond_scale, timestep=timestep / 1000, encoder_hidden_states=prompt_embeds, encoder_hidden_states_mask=prompt_embeds_mask, img_shapes=img_shapes, return_dict=False, ) with self.transformer.cache_context("cond"): noise_pred = self.transformer( hidden_states=latents, timestep=timestep / 1000, encoder_hidden_states=prompt_embeds, encoder_hidden_states_mask=prompt_embeds_mask, img_shapes=img_shapes, controlnet_block_samples=controlnet_block_samples, attention_kwargs=self.attention_kwargs, return_dict=False, )[0] if do_true_cfg: with self.transformer.cache_context("uncond"): neg_noise_pred = self.transformer( hidden_states=latents, timestep=timestep / 1000, guidance=guidance, encoder_hidden_states_mask=negative_prompt_embeds_mask, encoder_hidden_states=negative_prompt_embeds, img_shapes=img_shapes, controlnet_block_samples=controlnet_block_samples, attention_kwargs=self.attention_kwargs, return_dict=False, )[0] comb_pred = neg_noise_pred + true_cfg_scale * (noise_pred - neg_noise_pred) cond_norm = torch.norm(noise_pred, dim=-1, keepdim=True) noise_norm = torch.norm(comb_pred, dim=-1, keepdim=True) noise_pred = comb_pred * (cond_norm / noise_norm) # compute the previous noisy sample x_t -> x_t-1 latents_dtype = latents.dtype latents = self.scheduler.step(noise_pred, t, latents, return_dict=False)[0] if latents.dtype != latents_dtype: if torch.backends.mps.is_available(): # some platforms (eg. apple mps) misbehave due to a pytorch bug: https://github.com/pytorch/pytorch/pull/99272 latents = latents.to(latents_dtype) if callback_on_step_end is not None: callback_kwargs = {} for k in callback_on_step_end_tensor_inputs: callback_kwargs[k] = locals()[k] callback_outputs = callback_on_step_end(self, i, t, callback_kwargs) latents = callback_outputs.pop("latents", latents) prompt_embeds = callback_outputs.pop("prompt_embeds", prompt_embeds) # call the callback, if provided if i == len(timesteps) - 1 or ((i + 1) > num_warmup_steps and (i + 1) % self.scheduler.order == 0): progress_bar.update() if XLA_AVAILABLE: xm.mark_step() self._current_timestep = None if output_type == "latent": image = latents else: latents = self._unpack_latents(latents, height, width, self.vae_scale_factor) latents = latents.to(self.vae.dtype) latents_mean = ( torch.tensor(self.vae.config.latents_mean) .view(1, self.vae.config.z_dim, 1, 1, 1) .to(latents.device, latents.dtype) ) latents_std = 1.0 / torch.tensor(self.vae.config.latents_std).view(1, self.vae.config.z_dim, 1, 1, 1).to( latents.device, latents.dtype ) latents = latents / latents_std + latents_mean image = self.vae.decode(latents, return_dict=False)[0][:, :, 0] image = self.image_processor.postprocess(image, output_type=output_type) # Offload all models self.maybe_free_model_hooks() if not return_dict: return (image,) return QwenImagePipelineOutput(images=image)
{ "repo_id": "huggingface/diffusers", "file_path": "src/diffusers/pipelines/qwenimage/pipeline_qwenimage_controlnet_inpaint.py", "license": "Apache License 2.0", "lines": 817, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
huggingface/diffusers:src/diffusers/modular_pipelines/qwenimage/before_denoise.py
# Copyright 2025 Qwen-Image Team and The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import inspect import numpy as np import torch from ...models import QwenImageControlNetModel, QwenImageMultiControlNetModel from ...schedulers import FlowMatchEulerDiscreteScheduler from ...utils.torch_utils import randn_tensor, unwrap_module from ..modular_pipeline import ModularPipelineBlocks, PipelineState from ..modular_pipeline_utils import ComponentSpec, InputParam, OutputParam from .modular_pipeline import QwenImageLayeredPachifier, QwenImageModularPipeline, QwenImagePachifier # Copied from diffusers.pipelines.qwenimage.pipeline_qwenimage.calculate_shift def calculate_shift( image_seq_len, base_seq_len: int = 256, max_seq_len: int = 4096, base_shift: float = 0.5, max_shift: float = 1.15, ): m = (max_shift - base_shift) / (max_seq_len - base_seq_len) b = base_shift - m * base_seq_len mu = image_seq_len * m + b return mu # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.retrieve_timesteps def retrieve_timesteps( scheduler, num_inference_steps: int | None = None, device: str | torch.device | None = None, timesteps: list[int] | None = None, sigmas: list[float] | None = None, **kwargs, ): r""" Calls the scheduler's `set_timesteps` method and retrieves timesteps from the scheduler after the call. Handles custom timesteps. Any kwargs will be supplied to `scheduler.set_timesteps`. Args: scheduler (`SchedulerMixin`): The scheduler to get timesteps from. num_inference_steps (`int`): The number of diffusion steps used when generating samples with a pre-trained model. If used, `timesteps` must be `None`. device (`str` or `torch.device`, *optional*): The device to which the timesteps should be moved to. If `None`, the timesteps are not moved. timesteps (`list[int]`, *optional*): Custom timesteps used to override the timestep spacing strategy of the scheduler. If `timesteps` is passed, `num_inference_steps` and `sigmas` must be `None`. sigmas (`list[float]`, *optional*): Custom sigmas used to override the timestep spacing strategy of the scheduler. If `sigmas` is passed, `num_inference_steps` and `timesteps` must be `None`. Returns: `tuple[torch.Tensor, int]`: A tuple where the first element is the timestep schedule from the scheduler and the second element is the number of inference steps. """ if timesteps is not None and sigmas is not None: raise ValueError("Only one of `timesteps` or `sigmas` can be passed. Please choose one to set custom values") if timesteps is not None: accepts_timesteps = "timesteps" in set(inspect.signature(scheduler.set_timesteps).parameters.keys()) if not accepts_timesteps: raise ValueError( f"The current scheduler class {scheduler.__class__}'s `set_timesteps` does not support custom" f" timestep schedules. Please check whether you are using the correct scheduler." ) scheduler.set_timesteps(timesteps=timesteps, device=device, **kwargs) timesteps = scheduler.timesteps num_inference_steps = len(timesteps) elif sigmas is not None: accept_sigmas = "sigmas" in set(inspect.signature(scheduler.set_timesteps).parameters.keys()) if not accept_sigmas: raise ValueError( f"The current scheduler class {scheduler.__class__}'s `set_timesteps` does not support custom" f" sigmas schedules. Please check whether you are using the correct scheduler." ) scheduler.set_timesteps(sigmas=sigmas, device=device, **kwargs) timesteps = scheduler.timesteps num_inference_steps = len(timesteps) else: scheduler.set_timesteps(num_inference_steps, device=device, **kwargs) timesteps = scheduler.timesteps return timesteps, num_inference_steps # modified from diffusers.pipelines.stable_diffusion_3.pipeline_stable_diffusion_3_img2img.StableDiffusion3Img2ImgPipeline.get_timesteps def get_timesteps(scheduler, num_inference_steps, strength): # get the original timestep using init_timestep init_timestep = min(num_inference_steps * strength, num_inference_steps) t_start = int(max(num_inference_steps - init_timestep, 0)) timesteps = scheduler.timesteps[t_start * scheduler.order :] if hasattr(scheduler, "set_begin_index"): scheduler.set_begin_index(t_start * scheduler.order) return timesteps, num_inference_steps - t_start # ==================== # 1. PREPARE LATENTS # ==================== # auto_docstring class QwenImagePrepareLatentsStep(ModularPipelineBlocks): """ Prepare initial random noise for the generation process Components: pachifier (`QwenImagePachifier`) Inputs: latents (`Tensor`, *optional*): Pre-generated noisy latents for image generation. height (`int`, *optional*): The height in pixels of the generated image. width (`int`, *optional*): The width in pixels of the generated image. num_images_per_prompt (`int`, *optional*, defaults to 1): The number of images to generate per prompt. generator (`Generator`, *optional*): Torch generator for deterministic generation. batch_size (`int`, *optional*, defaults to 1): Number of prompts, the final batch size of model inputs should be batch_size * num_images_per_prompt. Can be generated in input step. dtype (`dtype`, *optional*, defaults to torch.float32): The dtype of the model inputs, can be generated in input step. Outputs: height (`int`): if not set, updated to default value width (`int`): if not set, updated to default value latents (`Tensor`): The initial latents to use for the denoising process """ model_name = "qwenimage" @property def description(self) -> str: return "Prepare initial random noise for the generation process" @property def expected_components(self) -> list[ComponentSpec]: return [ ComponentSpec("pachifier", QwenImagePachifier, default_creation_method="from_config"), ] @property def inputs(self) -> list[InputParam]: return [ InputParam.template("latents"), InputParam.template("height"), InputParam.template("width"), InputParam.template("num_images_per_prompt"), InputParam.template("generator"), InputParam.template("batch_size"), InputParam.template("dtype"), ] @property def intermediate_outputs(self) -> list[OutputParam]: return [ OutputParam(name="height", type_hint=int, description="if not set, updated to default value"), OutputParam(name="width", type_hint=int, description="if not set, updated to default value"), OutputParam( name="latents", type_hint=torch.Tensor, description="The initial latents to use for the denoising process", ), ] @staticmethod def check_inputs(height, width, vae_scale_factor): if height is not None and height % (vae_scale_factor * 2) != 0: raise ValueError(f"Height must be divisible by {vae_scale_factor * 2} but is {height}") if width is not None and width % (vae_scale_factor * 2) != 0: raise ValueError(f"Width must be divisible by {vae_scale_factor * 2} but is {width}") @torch.no_grad() def __call__(self, components: QwenImageModularPipeline, state: PipelineState) -> PipelineState: block_state = self.get_block_state(state) self.check_inputs( height=block_state.height, width=block_state.width, vae_scale_factor=components.vae_scale_factor, ) device = components._execution_device batch_size = block_state.batch_size * block_state.num_images_per_prompt # we can update the height and width here since it's used to generate the initial block_state.height = block_state.height or components.default_height block_state.width = block_state.width or components.default_width # VAE applies 8x compression on images but we must also account for packing which requires # latent height and width to be divisible by 2. latent_height = 2 * (int(block_state.height) // (components.vae_scale_factor * 2)) latent_width = 2 * (int(block_state.width) // (components.vae_scale_factor * 2)) shape = (batch_size, components.num_channels_latents, 1, latent_height, latent_width) if isinstance(block_state.generator, list) and len(block_state.generator) != batch_size: raise ValueError( f"You have passed a list of generators of length {len(block_state.generator)}, but requested an effective batch" f" size of {batch_size}. Make sure the batch size matches the length of the generators." ) if block_state.latents is None: block_state.latents = randn_tensor( shape, generator=block_state.generator, device=device, dtype=block_state.dtype ) block_state.latents = components.pachifier.pack_latents(block_state.latents) self.set_block_state(state, block_state) return components, state # auto_docstring class QwenImageLayeredPrepareLatentsStep(ModularPipelineBlocks): """ Prepare initial random noise (B, layers+1, C, H, W) for the generation process Components: pachifier (`QwenImageLayeredPachifier`) Inputs: latents (`Tensor`, *optional*): Pre-generated noisy latents for image generation. height (`int`, *optional*): The height in pixels of the generated image. width (`int`, *optional*): The width in pixels of the generated image. layers (`int`, *optional*, defaults to 4): Number of layers to extract from the image num_images_per_prompt (`int`, *optional*, defaults to 1): The number of images to generate per prompt. generator (`Generator`, *optional*): Torch generator for deterministic generation. batch_size (`int`, *optional*, defaults to 1): Number of prompts, the final batch size of model inputs should be batch_size * num_images_per_prompt. Can be generated in input step. dtype (`dtype`, *optional*, defaults to torch.float32): The dtype of the model inputs, can be generated in input step. Outputs: height (`int`): if not set, updated to default value width (`int`): if not set, updated to default value latents (`Tensor`): The initial latents to use for the denoising process """ model_name = "qwenimage-layered" @property def description(self) -> str: return "Prepare initial random noise (B, layers+1, C, H, W) for the generation process" @property def expected_components(self) -> list[ComponentSpec]: return [ ComponentSpec("pachifier", QwenImageLayeredPachifier, default_creation_method="from_config"), ] @property def inputs(self) -> list[InputParam]: return [ InputParam.template("latents"), InputParam.template("height"), InputParam.template("width"), InputParam.template("layers"), InputParam.template("num_images_per_prompt"), InputParam.template("generator"), InputParam.template("batch_size"), InputParam.template("dtype"), ] @property def intermediate_outputs(self) -> list[OutputParam]: return [ OutputParam(name="height", type_hint=int, description="if not set, updated to default value"), OutputParam(name="width", type_hint=int, description="if not set, updated to default value"), OutputParam( name="latents", type_hint=torch.Tensor, description="The initial latents to use for the denoising process", ), ] @staticmethod def check_inputs(height, width, vae_scale_factor): if height is not None and height % (vae_scale_factor * 2) != 0: raise ValueError(f"Height must be divisible by {vae_scale_factor * 2} but is {height}") if width is not None and width % (vae_scale_factor * 2) != 0: raise ValueError(f"Width must be divisible by {vae_scale_factor * 2} but is {width}") @torch.no_grad() def __call__(self, components: QwenImageModularPipeline, state: PipelineState) -> PipelineState: block_state = self.get_block_state(state) self.check_inputs( height=block_state.height, width=block_state.width, vae_scale_factor=components.vae_scale_factor, ) device = components._execution_device batch_size = block_state.batch_size * block_state.num_images_per_prompt # we can update the height and width here since it's used to generate the initial block_state.height = block_state.height or components.default_height block_state.width = block_state.width or components.default_width # VAE applies 8x compression on images but we must also account for packing which requires # latent height and width to be divisible by 2. latent_height = 2 * (int(block_state.height) // (components.vae_scale_factor * 2)) latent_width = 2 * (int(block_state.width) // (components.vae_scale_factor * 2)) shape = (batch_size, block_state.layers + 1, components.num_channels_latents, latent_height, latent_width) if isinstance(block_state.generator, list) and len(block_state.generator) != batch_size: raise ValueError( f"You have passed a list of generators of length {len(block_state.generator)}, but requested an effective batch" f" size of {batch_size}. Make sure the batch size matches the length of the generators." ) if block_state.latents is None: block_state.latents = randn_tensor( shape, generator=block_state.generator, device=device, dtype=block_state.dtype ) block_state.latents = components.pachifier.pack_latents(block_state.latents) self.set_block_state(state, block_state) return components, state # auto_docstring class QwenImagePrepareLatentsWithStrengthStep(ModularPipelineBlocks): """ Step that adds noise to image latents for image-to-image/inpainting. Should be run after set_timesteps, prepare_latents. Both noise and image latents should alreadybe patchified. Components: scheduler (`FlowMatchEulerDiscreteScheduler`) Inputs: latents (`Tensor`): The initial random noised, can be generated in prepare latent step. image_latents (`Tensor`): image latents used to guide the image generation. Can be generated from vae_encoder step. (Can be generated from vae encoder and updated in input step.) timesteps (`Tensor`): The timesteps to use for the denoising process. Can be generated in set_timesteps step. Outputs: initial_noise (`Tensor`): The initial random noised used for inpainting denoising. latents (`Tensor`): The scaled noisy latents to use for inpainting/image-to-image denoising. """ model_name = "qwenimage" @property def description(self) -> str: return "Step that adds noise to image latents for image-to-image/inpainting. Should be run after set_timesteps, prepare_latents. Both noise and image latents should alreadybe patchified." @property def expected_components(self) -> list[ComponentSpec]: return [ ComponentSpec("scheduler", FlowMatchEulerDiscreteScheduler), ] @property def inputs(self) -> list[InputParam]: return [ InputParam( name="latents", required=True, type_hint=torch.Tensor, description="The initial random noised, can be generated in prepare latent step.", ), InputParam.template("image_latents", note="Can be generated from vae encoder and updated in input step."), InputParam( name="timesteps", required=True, type_hint=torch.Tensor, description="The timesteps to use for the denoising process. Can be generated in set_timesteps step.", ), ] @property def intermediate_outputs(self) -> list[OutputParam]: return [ OutputParam( name="initial_noise", type_hint=torch.Tensor, description="The initial random noised used for inpainting denoising.", ), OutputParam( name="latents", type_hint=torch.Tensor, description="The scaled noisy latents to use for inpainting/image-to-image denoising.", ), ] @staticmethod def check_inputs(image_latents, latents): if image_latents.shape[0] != latents.shape[0]: raise ValueError( f"`image_latents` must have have same batch size as `latents`, but got {image_latents.shape[0]} and {latents.shape[0]}" ) if image_latents.ndim != 3: raise ValueError(f"`image_latents` must have 3 dimensions (patchified), but got {image_latents.ndim}") @torch.no_grad() def __call__(self, components: QwenImageModularPipeline, state: PipelineState) -> PipelineState: block_state = self.get_block_state(state) self.check_inputs( image_latents=block_state.image_latents, latents=block_state.latents, ) # prepare latent timestep latent_timestep = block_state.timesteps[:1].repeat(block_state.latents.shape[0]) # make copy of initial_noise block_state.initial_noise = block_state.latents # scale noise block_state.latents = components.scheduler.scale_noise( block_state.image_latents, latent_timestep, block_state.latents ) self.set_block_state(state, block_state) return components, state # auto_docstring class QwenImageCreateMaskLatentsStep(ModularPipelineBlocks): """ Step that creates mask latents from preprocessed mask_image by interpolating to latent space. Components: pachifier (`QwenImagePachifier`) Inputs: processed_mask_image (`Tensor`): The processed mask to use for the inpainting process. height (`int`): The height in pixels of the generated image. width (`int`): The width in pixels of the generated image. dtype (`dtype`, *optional*, defaults to torch.float32): The dtype of the model inputs, can be generated in input step. Outputs: mask (`Tensor`): The mask to use for the inpainting process. """ model_name = "qwenimage" @property def description(self) -> str: return "Step that creates mask latents from preprocessed mask_image by interpolating to latent space." @property def expected_components(self) -> list[ComponentSpec]: return [ ComponentSpec("pachifier", QwenImagePachifier, default_creation_method="from_config"), ] @property def inputs(self) -> list[InputParam]: return [ InputParam( name="processed_mask_image", required=True, type_hint=torch.Tensor, description="The processed mask to use for the inpainting process.", ), InputParam.template("height", required=True), InputParam.template("width", required=True), InputParam.template("dtype"), ] @property def intermediate_outputs(self) -> list[OutputParam]: return [ OutputParam( name="mask", type_hint=torch.Tensor, description="The mask to use for the inpainting process." ), ] @torch.no_grad() def __call__(self, components: QwenImageModularPipeline, state: PipelineState) -> PipelineState: block_state = self.get_block_state(state) device = components._execution_device # VAE applies 8x compression on images but we must also account for packing which requires # latent height and width to be divisible by 2. height_latents = 2 * (int(block_state.height) // (components.vae_scale_factor * 2)) width_latents = 2 * (int(block_state.width) // (components.vae_scale_factor * 2)) block_state.mask = torch.nn.functional.interpolate( block_state.processed_mask_image, size=(height_latents, width_latents), ) block_state.mask = block_state.mask.unsqueeze(2) block_state.mask = block_state.mask.repeat(1, components.num_channels_latents, 1, 1, 1) block_state.mask = block_state.mask.to(device=device, dtype=block_state.dtype) block_state.mask = components.pachifier.pack_latents(block_state.mask) self.set_block_state(state, block_state) return components, state # ==================== # 2. SET TIMESTEPS # ==================== # auto_docstring class QwenImageSetTimestepsStep(ModularPipelineBlocks): """ Step that sets the scheduler's timesteps for text-to-image generation. Should be run after prepare latents step. Components: scheduler (`FlowMatchEulerDiscreteScheduler`) Inputs: num_inference_steps (`int`, *optional*, defaults to 50): The number of denoising steps. sigmas (`list`, *optional*): Custom sigmas for the denoising process. latents (`Tensor`): The initial random noised latents for the denoising process. Can be generated in prepare latents step. Outputs: timesteps (`Tensor`): The timesteps to use for the denoising process """ model_name = "qwenimage" @property def description(self) -> str: return "Step that sets the scheduler's timesteps for text-to-image generation. Should be run after prepare latents step." @property def expected_components(self) -> list[ComponentSpec]: return [ ComponentSpec("scheduler", FlowMatchEulerDiscreteScheduler), ] @property def inputs(self) -> list[InputParam]: return [ InputParam.template("num_inference_steps"), InputParam.template("sigmas"), InputParam( name="latents", required=True, type_hint=torch.Tensor, description="The initial random noised latents for the denoising process. Can be generated in prepare latents step.", ), ] @property def intermediate_outputs(self) -> list[OutputParam]: return [ OutputParam( name="timesteps", type_hint=torch.Tensor, description="The timesteps to use for the denoising process" ), ] def __call__(self, components: QwenImageModularPipeline, state: PipelineState) -> PipelineState: block_state = self.get_block_state(state) device = components._execution_device sigmas = ( np.linspace(1.0, 1 / block_state.num_inference_steps, block_state.num_inference_steps) if block_state.sigmas is None else block_state.sigmas ) mu = calculate_shift( image_seq_len=block_state.latents.shape[1], base_seq_len=components.scheduler.config.get("base_image_seq_len", 256), max_seq_len=components.scheduler.config.get("max_image_seq_len", 4096), base_shift=components.scheduler.config.get("base_shift", 0.5), max_shift=components.scheduler.config.get("max_shift", 1.15), ) block_state.timesteps, block_state.num_inference_steps = retrieve_timesteps( scheduler=components.scheduler, num_inference_steps=block_state.num_inference_steps, device=device, sigmas=sigmas, mu=mu, ) components.scheduler.set_begin_index(0) self.set_block_state(state, block_state) return components, state # auto_docstring class QwenImageLayeredSetTimestepsStep(ModularPipelineBlocks): """ Set timesteps step for QwenImage Layered with custom mu calculation based on image_latents. Components: scheduler (`FlowMatchEulerDiscreteScheduler`) Inputs: num_inference_steps (`int`, *optional*, defaults to 50): The number of denoising steps. sigmas (`list`, *optional*): Custom sigmas for the denoising process. image_latents (`Tensor`): image latents used to guide the image generation. Can be generated from vae_encoder step. Outputs: timesteps (`Tensor`): The timesteps to use for the denoising process. """ model_name = "qwenimage-layered" @property def description(self) -> str: return "Set timesteps step for QwenImage Layered with custom mu calculation based on image_latents." @property def expected_components(self) -> list[ComponentSpec]: return [ ComponentSpec("scheduler", FlowMatchEulerDiscreteScheduler), ] @property def inputs(self) -> list[InputParam]: return [ InputParam.template("num_inference_steps"), InputParam.template("sigmas"), InputParam.template("image_latents"), ] @property def intermediate_outputs(self) -> list[OutputParam]: return [ OutputParam( name="timesteps", type_hint=torch.Tensor, description="The timesteps to use for the denoising process." ), ] @torch.no_grad() def __call__(self, components, state: PipelineState) -> PipelineState: block_state = self.get_block_state(state) device = components._execution_device # Layered-specific mu calculation base_seqlen = 256 * 256 / 16 / 16 # = 256 mu = (block_state.image_latents.shape[1] / base_seqlen) ** 0.5 # Default sigmas if not provided sigmas = ( np.linspace(1.0, 1 / block_state.num_inference_steps, block_state.num_inference_steps) if block_state.sigmas is None else block_state.sigmas ) block_state.timesteps, block_state.num_inference_steps = retrieve_timesteps( components.scheduler, block_state.num_inference_steps, device, sigmas=sigmas, mu=mu, ) components.scheduler.set_begin_index(0) self.set_block_state(state, block_state) return components, state # auto_docstring class QwenImageSetTimestepsWithStrengthStep(ModularPipelineBlocks): """ Step that sets the scheduler's timesteps for image-to-image generation, and inpainting. Should be run after prepare latents step. Components: scheduler (`FlowMatchEulerDiscreteScheduler`) Inputs: num_inference_steps (`int`, *optional*, defaults to 50): The number of denoising steps. sigmas (`list`, *optional*): Custom sigmas for the denoising process. latents (`Tensor`): The latents to use for the denoising process. Can be generated in prepare latents step. strength (`float`, *optional*, defaults to 0.9): Strength for img2img/inpainting. Outputs: timesteps (`Tensor`): The timesteps to use for the denoising process. num_inference_steps (`int`): The number of denoising steps to perform at inference time. Updated based on strength. """ model_name = "qwenimage" @property def description(self) -> str: return "Step that sets the scheduler's timesteps for image-to-image generation, and inpainting. Should be run after prepare latents step." @property def expected_components(self) -> list[ComponentSpec]: return [ ComponentSpec("scheduler", FlowMatchEulerDiscreteScheduler), ] @property def inputs(self) -> list[InputParam]: return [ InputParam.template("num_inference_steps"), InputParam.template("sigmas"), InputParam( "latents", required=True, type_hint=torch.Tensor, description="The latents to use for the denoising process. Can be generated in prepare latents step.", ), InputParam.template("strength", default=0.9), ] @property def intermediate_outputs(self) -> list[OutputParam]: return [ OutputParam( name="timesteps", type_hint=torch.Tensor, description="The timesteps to use for the denoising process.", ), OutputParam( name="num_inference_steps", type_hint=int, description="The number of denoising steps to perform at inference time. Updated based on strength.", ), ] def __call__(self, components: QwenImageModularPipeline, state: PipelineState) -> PipelineState: block_state = self.get_block_state(state) device = components._execution_device sigmas = ( np.linspace(1.0, 1 / block_state.num_inference_steps, block_state.num_inference_steps) if block_state.sigmas is None else block_state.sigmas ) mu = calculate_shift( image_seq_len=block_state.latents.shape[1], base_seq_len=components.scheduler.config.get("base_image_seq_len", 256), max_seq_len=components.scheduler.config.get("max_image_seq_len", 4096), base_shift=components.scheduler.config.get("base_shift", 0.5), max_shift=components.scheduler.config.get("max_shift", 1.15), ) block_state.timesteps, block_state.num_inference_steps = retrieve_timesteps( scheduler=components.scheduler, num_inference_steps=block_state.num_inference_steps, device=device, sigmas=sigmas, mu=mu, ) block_state.timesteps, block_state.num_inference_steps = get_timesteps( scheduler=components.scheduler, num_inference_steps=block_state.num_inference_steps, strength=block_state.strength, ) self.set_block_state(state, block_state) return components, state # ==================== # 3. OTHER INPUTS FOR DENOISER # ==================== ## RoPE inputs for denoiser # auto_docstring class QwenImageRoPEInputsStep(ModularPipelineBlocks): """ Step that prepares the RoPE inputs for the denoising process. Should be place after prepare_latents step Inputs: batch_size (`int`, *optional*, defaults to 1): Number of prompts, the final batch size of model inputs should be batch_size * num_images_per_prompt. Can be generated in input step. height (`int`): The height in pixels of the generated image. width (`int`): The width in pixels of the generated image. prompt_embeds_mask (`Tensor`): mask for the text embeddings. Can be generated from text_encoder step. negative_prompt_embeds_mask (`Tensor`, *optional*): mask for the negative text embeddings. Can be generated from text_encoder step. Outputs: img_shapes (`list`): The shapes of the images latents, used for RoPE calculation """ model_name = "qwenimage" @property def description(self) -> str: return ( "Step that prepares the RoPE inputs for the denoising process. Should be place after prepare_latents step" ) @property def inputs(self) -> list[InputParam]: return [ InputParam.template("batch_size"), InputParam.template("height", required=True), InputParam.template("width", required=True), InputParam.template("prompt_embeds_mask"), InputParam.template("negative_prompt_embeds_mask"), ] @property def intermediate_outputs(self) -> list[OutputParam]: return [ OutputParam( name="img_shapes", kwargs_type="denoiser_input_fields", type_hint=list[list[tuple[int, int, int]]], description="The shapes of the images latents, used for RoPE calculation", ), ] def __call__(self, components: QwenImageModularPipeline, state: PipelineState) -> PipelineState: block_state = self.get_block_state(state) block_state.img_shapes = [ [ ( 1, block_state.height // components.vae_scale_factor // 2, block_state.width // components.vae_scale_factor // 2, ) ] ] * block_state.batch_size self.set_block_state(state, block_state) return components, state # auto_docstring class QwenImageEditRoPEInputsStep(ModularPipelineBlocks): """ Step that prepares the RoPE inputs for denoising process. This is used in QwenImage Edit. Should be placed after prepare_latents step Inputs: batch_size (`int`, *optional*, defaults to 1): Number of prompts, the final batch size of model inputs should be batch_size * num_images_per_prompt. Can be generated in input step. image_height (`int`): The height of the reference image. Can be generated in input step. image_width (`int`): The width of the reference image. Can be generated in input step. height (`int`): The height in pixels of the generated image. width (`int`): The width in pixels of the generated image. prompt_embeds_mask (`Tensor`): mask for the text embeddings. Can be generated from text_encoder step. negative_prompt_embeds_mask (`Tensor`, *optional*): mask for the negative text embeddings. Can be generated from text_encoder step. Outputs: img_shapes (`list`): The shapes of the images latents, used for RoPE calculation """ model_name = "qwenimage" @property def description(self) -> str: return "Step that prepares the RoPE inputs for denoising process. This is used in QwenImage Edit. Should be placed after prepare_latents step" @property def inputs(self) -> list[InputParam]: return [ InputParam.template("batch_size"), InputParam( name="image_height", required=True, type_hint=int, description="The height of the reference image. Can be generated in input step.", ), InputParam( name="image_width", required=True, type_hint=int, description="The width of the reference image. Can be generated in input step.", ), InputParam.template("height", required=True), InputParam.template("width", required=True), InputParam.template("prompt_embeds_mask"), InputParam.template("negative_prompt_embeds_mask"), ] @property def intermediate_outputs(self) -> list[OutputParam]: return [ OutputParam( name="img_shapes", kwargs_type="denoiser_input_fields", type_hint=list[list[tuple[int, int, int]]], description="The shapes of the images latents, used for RoPE calculation", ), ] def __call__(self, components: QwenImageModularPipeline, state: PipelineState) -> PipelineState: block_state = self.get_block_state(state) # for edit, image size can be different from the target size (height/width) block_state.img_shapes = [ [ ( 1, block_state.height // components.vae_scale_factor // 2, block_state.width // components.vae_scale_factor // 2, ), ( 1, block_state.image_height // components.vae_scale_factor // 2, block_state.image_width // components.vae_scale_factor // 2, ), ] ] * block_state.batch_size self.set_block_state(state, block_state) return components, state # auto_docstring class QwenImageEditPlusRoPEInputsStep(ModularPipelineBlocks): """ Step that prepares the RoPE inputs for denoising process. This is used in QwenImage Edit Plus. Unlike Edit, Edit Plus handles lists of image_height/image_width for multiple reference images. Should be placed after prepare_latents step. Inputs: batch_size (`int`, *optional*, defaults to 1): Number of prompts, the final batch size of model inputs should be batch_size * num_images_per_prompt. Can be generated in input step. image_height (`list`): The heights of the reference images. Can be generated in input step. image_width (`list`): The widths of the reference images. Can be generated in input step. height (`int`): The height in pixels of the generated image. width (`int`): The width in pixels of the generated image. prompt_embeds_mask (`Tensor`): mask for the text embeddings. Can be generated from text_encoder step. negative_prompt_embeds_mask (`Tensor`, *optional*): mask for the negative text embeddings. Can be generated from text_encoder step. Outputs: img_shapes (`list`): The shapes of the image latents, used for RoPE calculation txt_seq_lens (`list`): The sequence lengths of the prompt embeds, used for RoPE calculation negative_txt_seq_lens (`list`): The sequence lengths of the negative prompt embeds, used for RoPE calculation """ model_name = "qwenimage-edit-plus" @property def description(self) -> str: return ( "Step that prepares the RoPE inputs for denoising process. This is used in QwenImage Edit Plus.\n" "Unlike Edit, Edit Plus handles lists of image_height/image_width for multiple reference images.\n" "Should be placed after prepare_latents step." ) @property def inputs(self) -> list[InputParam]: return [ InputParam.template("batch_size"), InputParam( name="image_height", required=True, type_hint=list[int], description="The heights of the reference images. Can be generated in input step.", ), InputParam( name="image_width", required=True, type_hint=list[int], description="The widths of the reference images. Can be generated in input step.", ), InputParam.template("height", required=True), InputParam.template("width", required=True), InputParam.template("prompt_embeds_mask"), InputParam.template("negative_prompt_embeds_mask"), ] @property def intermediate_outputs(self) -> list[OutputParam]: return [ OutputParam( name="img_shapes", kwargs_type="denoiser_input_fields", type_hint=list[list[tuple[int, int, int]]], description="The shapes of the image latents, used for RoPE calculation", ), OutputParam( name="txt_seq_lens", kwargs_type="denoiser_input_fields", type_hint=list[int], description="The sequence lengths of the prompt embeds, used for RoPE calculation", ), OutputParam( name="negative_txt_seq_lens", kwargs_type="denoiser_input_fields", type_hint=list[int], description="The sequence lengths of the negative prompt embeds, used for RoPE calculation", ), ] def __call__(self, components: QwenImageModularPipeline, state: PipelineState) -> PipelineState: block_state = self.get_block_state(state) vae_scale_factor = components.vae_scale_factor # Edit Plus: image_height and image_width are lists block_state.img_shapes = [ [ (1, block_state.height // vae_scale_factor // 2, block_state.width // vae_scale_factor // 2), *[ (1, img_height // vae_scale_factor // 2, img_width // vae_scale_factor // 2) for img_height, img_width in zip(block_state.image_height, block_state.image_width) ], ] ] * block_state.batch_size block_state.txt_seq_lens = ( block_state.prompt_embeds_mask.sum(dim=1).tolist() if block_state.prompt_embeds_mask is not None else None ) block_state.negative_txt_seq_lens = ( block_state.negative_prompt_embeds_mask.sum(dim=1).tolist() if block_state.negative_prompt_embeds_mask is not None else None ) self.set_block_state(state, block_state) return components, state # auto_docstring class QwenImageLayeredRoPEInputsStep(ModularPipelineBlocks): """ Step that prepares the RoPE inputs for the denoising process. Should be place after prepare_latents step Inputs: batch_size (`int`, *optional*, defaults to 1): Number of prompts, the final batch size of model inputs should be batch_size * num_images_per_prompt. Can be generated in input step. layers (`int`, *optional*, defaults to 4): Number of layers to extract from the image height (`int`): The height in pixels of the generated image. width (`int`): The width in pixels of the generated image. prompt_embeds_mask (`Tensor`): mask for the text embeddings. Can be generated from text_encoder step. negative_prompt_embeds_mask (`Tensor`, *optional*): mask for the negative text embeddings. Can be generated from text_encoder step. Outputs: img_shapes (`list`): The shapes of the image latents, used for RoPE calculation txt_seq_lens (`list`): The sequence lengths of the prompt embeds, used for RoPE calculation negative_txt_seq_lens (`list`): The sequence lengths of the negative prompt embeds, used for RoPE calculation additional_t_cond (`Tensor`): The additional t cond, used for RoPE calculation """ model_name = "qwenimage-layered" @property def description(self) -> str: return ( "Step that prepares the RoPE inputs for the denoising process. Should be place after prepare_latents step" ) @property def inputs(self) -> list[InputParam]: return [ InputParam.template("batch_size"), InputParam.template("layers"), InputParam.template("height", required=True), InputParam.template("width", required=True), InputParam.template("prompt_embeds_mask"), InputParam.template("negative_prompt_embeds_mask"), ] @property def intermediate_outputs(self) -> list[OutputParam]: return [ OutputParam( name="img_shapes", type_hint=list[list[tuple[int, int, int]]], kwargs_type="denoiser_input_fields", description="The shapes of the image latents, used for RoPE calculation", ), OutputParam( name="txt_seq_lens", type_hint=list[int], kwargs_type="denoiser_input_fields", description="The sequence lengths of the prompt embeds, used for RoPE calculation", ), OutputParam( name="negative_txt_seq_lens", type_hint=list[int], kwargs_type="denoiser_input_fields", description="The sequence lengths of the negative prompt embeds, used for RoPE calculation", ), OutputParam( name="additional_t_cond", type_hint=torch.Tensor, kwargs_type="denoiser_input_fields", description="The additional t cond, used for RoPE calculation", ), ] @torch.no_grad() def __call__(self, components, state: PipelineState) -> PipelineState: block_state = self.get_block_state(state) device = components._execution_device # All shapes are the same for Layered shape = ( 1, block_state.height // components.vae_scale_factor // 2, block_state.width // components.vae_scale_factor // 2, ) # layers+1 output shapes + 1 condition shape (all same) block_state.img_shapes = [[shape] * (block_state.layers + 2)] * block_state.batch_size # txt_seq_lens block_state.txt_seq_lens = ( block_state.prompt_embeds_mask.sum(dim=1).tolist() if block_state.prompt_embeds_mask is not None else None ) block_state.negative_txt_seq_lens = ( block_state.negative_prompt_embeds_mask.sum(dim=1).tolist() if block_state.negative_prompt_embeds_mask is not None else None ) block_state.additional_t_cond = torch.tensor([0] * block_state.batch_size).to(device=device, dtype=torch.long) self.set_block_state(state, block_state) return components, state ## ControlNet inputs for denoiser # auto_docstring class QwenImageControlNetBeforeDenoiserStep(ModularPipelineBlocks): """ step that prepare inputs for controlnet. Insert before the Denoise Step, after set_timesteps step. Components: controlnet (`QwenImageControlNetModel`) Inputs: control_guidance_start (`float`, *optional*, defaults to 0.0): When to start applying ControlNet. control_guidance_end (`float`, *optional*, defaults to 1.0): When to stop applying ControlNet. controlnet_conditioning_scale (`float`, *optional*, defaults to 1.0): Scale for ControlNet conditioning. control_image_latents (`Tensor`): The control image latents to use for the denoising process. Can be generated in controlnet vae encoder step. timesteps (`Tensor`): The timesteps to use for the denoising process. Can be generated in set_timesteps step. Outputs: controlnet_keep (`list`): The controlnet keep values """ model_name = "qwenimage" @property def expected_components(self) -> list[ComponentSpec]: return [ ComponentSpec("controlnet", QwenImageControlNetModel), ] @property def description(self) -> str: return "step that prepare inputs for controlnet. Insert before the Denoise Step, after set_timesteps step." @property def inputs(self) -> list[InputParam]: return [ InputParam.template("control_guidance_start"), InputParam.template("control_guidance_end"), InputParam.template("controlnet_conditioning_scale"), InputParam( name="control_image_latents", required=True, type_hint=torch.Tensor, description="The control image latents to use for the denoising process. Can be generated in controlnet vae encoder step.", ), InputParam( name="timesteps", required=True, type_hint=torch.Tensor, description="The timesteps to use for the denoising process. Can be generated in set_timesteps step.", ), ] @property def intermediate_outputs(self) -> list[OutputParam]: return [ OutputParam("controlnet_keep", type_hint=list[float], description="The controlnet keep values"), ] @torch.no_grad() def __call__(self, components: QwenImageModularPipeline, state: PipelineState) -> PipelineState: block_state = self.get_block_state(state) controlnet = unwrap_module(components.controlnet) # control_guidance_start/control_guidance_end (align format) if not isinstance(block_state.control_guidance_start, list) and isinstance( block_state.control_guidance_end, list ): block_state.control_guidance_start = len(block_state.control_guidance_end) * [ block_state.control_guidance_start ] elif not isinstance(block_state.control_guidance_end, list) and isinstance( block_state.control_guidance_start, list ): block_state.control_guidance_end = len(block_state.control_guidance_start) * [ block_state.control_guidance_end ] elif not isinstance(block_state.control_guidance_start, list) and not isinstance( block_state.control_guidance_end, list ): mult = ( len(block_state.control_image_latents) if isinstance(controlnet, QwenImageMultiControlNetModel) else 1 ) block_state.control_guidance_start, block_state.control_guidance_end = ( mult * [block_state.control_guidance_start], mult * [block_state.control_guidance_end], ) # controlnet_conditioning_scale (align format) if isinstance(controlnet, QwenImageMultiControlNetModel) and isinstance( block_state.controlnet_conditioning_scale, float ): block_state.controlnet_conditioning_scale = [block_state.controlnet_conditioning_scale] * mult # controlnet_keep block_state.controlnet_keep = [] for i in range(len(block_state.timesteps)): keeps = [ 1.0 - float(i / len(block_state.timesteps) < s or (i + 1) / len(block_state.timesteps) > e) for s, e in zip(block_state.control_guidance_start, block_state.control_guidance_end) ] block_state.controlnet_keep.append(keeps[0] if isinstance(controlnet, QwenImageControlNetModel) else keeps) self.set_block_state(state, block_state) return components, state
{ "repo_id": "huggingface/diffusers", "file_path": "src/diffusers/modular_pipelines/qwenimage/before_denoise.py", "license": "Apache License 2.0", "lines": 1113, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
huggingface/diffusers:src/diffusers/modular_pipelines/qwenimage/decoders.py
# Copyright 2025 Qwen-Image Team and The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from typing import Any import torch from ...configuration_utils import FrozenDict from ...image_processor import InpaintProcessor, VaeImageProcessor from ...models import AutoencoderKLQwenImage from ...utils import logging from ..modular_pipeline import ModularPipelineBlocks, PipelineState from ..modular_pipeline_utils import ComponentSpec, InputParam, OutputParam from .modular_pipeline import QwenImageLayeredPachifier, QwenImageModularPipeline, QwenImagePachifier logger = logging.get_logger(__name__) # after denoising loop (unpack latents) # auto_docstring class QwenImageAfterDenoiseStep(ModularPipelineBlocks): """ Step that unpack the latents from 3D tensor (batch_size, sequence_length, channels) into 5D tensor (batch_size, channels, 1, height, width) Components: pachifier (`QwenImagePachifier`) Inputs: height (`int`): The height in pixels of the generated image. width (`int`): The width in pixels of the generated image. latents (`Tensor`): The latents to decode, can be generated in the denoise step. Outputs: latents (`Tensor`): The denoisedlatents unpacked to B, C, 1, H, W """ model_name = "qwenimage" @property def description(self) -> str: return "Step that unpack the latents from 3D tensor (batch_size, sequence_length, channels) into 5D tensor (batch_size, channels, 1, height, width)" @property def expected_components(self) -> list[ComponentSpec]: components = [ ComponentSpec("pachifier", QwenImagePachifier, default_creation_method="from_config"), ] return components @property def inputs(self) -> list[InputParam]: return [ InputParam.template("height", required=True), InputParam.template("width", required=True), InputParam( name="latents", required=True, type_hint=torch.Tensor, description="The latents to decode, can be generated in the denoise step.", ), ] @property def intermediate_outputs(self) -> list[OutputParam]: return [ OutputParam( name="latents", type_hint=torch.Tensor, description="The denoisedlatents unpacked to B, C, 1, H, W" ), ] @torch.no_grad() def __call__(self, components: QwenImageModularPipeline, state: PipelineState) -> PipelineState: block_state = self.get_block_state(state) vae_scale_factor = components.vae_scale_factor block_state.latents = components.pachifier.unpack_latents( block_state.latents, block_state.height, block_state.width, vae_scale_factor=vae_scale_factor ) self.set_block_state(state, block_state) return components, state # auto_docstring class QwenImageLayeredAfterDenoiseStep(ModularPipelineBlocks): """ Unpack latents from (B, seq, C*4) to (B, C, layers+1, H, W) after denoising. Components: pachifier (`QwenImageLayeredPachifier`) Inputs: latents (`Tensor`): The denoised latents to decode, can be generated in the denoise step. height (`int`): The height in pixels of the generated image. width (`int`): The width in pixels of the generated image. layers (`int`, *optional*, defaults to 4): Number of layers to extract from the image Outputs: latents (`Tensor`): Denoised latents. (unpacked to B, C, layers+1, H, W) """ model_name = "qwenimage-layered" @property def description(self) -> str: return "Unpack latents from (B, seq, C*4) to (B, C, layers+1, H, W) after denoising." @property def expected_components(self) -> list[ComponentSpec]: return [ ComponentSpec("pachifier", QwenImageLayeredPachifier, default_creation_method="from_config"), ] @property def inputs(self) -> list[InputParam]: return [ InputParam( name="latents", required=True, type_hint=torch.Tensor, description="The denoised latents to decode, can be generated in the denoise step.", ), InputParam.template("height", required=True), InputParam.template("width", required=True), InputParam.template("layers"), ] @property def intermediate_outputs(self) -> list[OutputParam]: return [ OutputParam.template("latents", note="unpacked to B, C, layers+1, H, W"), ] @torch.no_grad() def __call__(self, components, state: PipelineState) -> PipelineState: block_state = self.get_block_state(state) # Unpack: (B, seq, C*4) -> (B, C, layers+1, H, W) block_state.latents = components.pachifier.unpack_latents( block_state.latents, block_state.height, block_state.width, block_state.layers, components.vae_scale_factor, ) self.set_block_state(state, block_state) return components, state # decode step # auto_docstring class QwenImageDecoderStep(ModularPipelineBlocks): """ Step that decodes the latents to images Components: vae (`AutoencoderKLQwenImage`) Inputs: latents (`Tensor`): The denoised latents to decode, can be generated in the denoise step and unpacked in the after denoise step. Outputs: images (`list`): Generated images. (tensor output of the vae decoder.) """ model_name = "qwenimage" @property def description(self) -> str: return "Step that decodes the latents to images" @property def expected_components(self) -> list[ComponentSpec]: components = [ ComponentSpec("vae", AutoencoderKLQwenImage), ] return components @property def inputs(self) -> list[InputParam]: return [ InputParam( name="latents", required=True, type_hint=torch.Tensor, description="The denoised latents to decode, can be generated in the denoise step and unpacked in the after denoise step.", ), ] @property def intermediate_outputs(self) -> list[OutputParam]: return [OutputParam.template("images", note="tensor output of the vae decoder.")] @torch.no_grad() def __call__(self, components: QwenImageModularPipeline, state: PipelineState) -> PipelineState: block_state = self.get_block_state(state) # YiYi Notes: remove support for output_type = "latents', we can just skip decode/encode step in modular if block_state.latents.ndim == 4: block_state.latents = block_state.latents.unsqueeze(dim=1) elif block_state.latents.ndim != 5: raise ValueError( f"expect latents to be a 4D or 5D tensor but got: {block_state.latents.shape}. Please make sure the latents are unpacked before decode step." ) block_state.latents = block_state.latents.to(components.vae.dtype) latents_mean = ( torch.tensor(components.vae.config.latents_mean) .view(1, components.vae.config.z_dim, 1, 1, 1) .to(block_state.latents.device, block_state.latents.dtype) ) latents_std = 1.0 / torch.tensor(components.vae.config.latents_std).view( 1, components.vae.config.z_dim, 1, 1, 1 ).to(block_state.latents.device, block_state.latents.dtype) block_state.latents = block_state.latents / latents_std + latents_mean block_state.images = components.vae.decode(block_state.latents, return_dict=False)[0][:, :, 0] self.set_block_state(state, block_state) return components, state # auto_docstring class QwenImageLayeredDecoderStep(ModularPipelineBlocks): """ Decode unpacked latents (B, C, layers+1, H, W) into layer images. Components: vae (`AutoencoderKLQwenImage`) image_processor (`VaeImageProcessor`) Inputs: latents (`Tensor`): The denoised latents to decode, can be generated in the denoise step and unpacked in the after denoise step. output_type (`str`, *optional*, defaults to pil): Output format: 'pil', 'np', 'pt'. Outputs: images (`list`): Generated images. """ model_name = "qwenimage-layered" @property def description(self) -> str: return "Decode unpacked latents (B, C, layers+1, H, W) into layer images." @property def expected_components(self) -> list[ComponentSpec]: return [ ComponentSpec("vae", AutoencoderKLQwenImage), ComponentSpec( "image_processor", VaeImageProcessor, config=FrozenDict({"vae_scale_factor": 16}), default_creation_method="from_config", ), ] @property def inputs(self) -> list[InputParam]: return [ InputParam( name="latents", required=True, type_hint=torch.Tensor, description="The denoised latents to decode, can be generated in the denoise step and unpacked in the after denoise step.", ), InputParam.template("output_type"), ] @property def intermediate_outputs(self) -> list[OutputParam]: return [OutputParam.template("images")] @torch.no_grad() def __call__(self, components, state: PipelineState) -> PipelineState: block_state = self.get_block_state(state) latents = block_state.latents # 1. VAE normalization latents = latents.to(components.vae.dtype) latents_mean = ( torch.tensor(components.vae.config.latents_mean) .view(1, components.vae.config.z_dim, 1, 1, 1) .to(latents.device, latents.dtype) ) latents_std = 1.0 / torch.tensor(components.vae.config.latents_std).view( 1, components.vae.config.z_dim, 1, 1, 1 ).to(latents.device, latents.dtype) latents = latents / latents_std + latents_mean # 2. Reshape for batch decoding: (B, C, layers+1, H, W) -> (B*layers, C, 1, H, W) b, c, f, h, w = latents.shape # 3. Remove first frame (composite), keep layers frames latents = latents[:, :, 1:] latents = latents.permute(0, 2, 1, 3, 4).reshape(-1, c, 1, h, w) # 4. Decode: (B*layers, C, 1, H, W) -> (B*layers, C, H, W) image = components.vae.decode(latents, return_dict=False)[0] image = image.squeeze(2) # 5. Postprocess - returns flat list of B*layers images image = components.image_processor.postprocess(image, output_type=block_state.output_type) # 6. Chunk into list per batch item images = [] for bidx in range(b): images.append(image[bidx * f : (bidx + 1) * f]) block_state.images = images self.set_block_state(state, block_state) return components, state # postprocess the decoded images # auto_docstring class QwenImageProcessImagesOutputStep(ModularPipelineBlocks): """ postprocess the generated image Components: image_processor (`VaeImageProcessor`) Inputs: images (`Tensor`): the generated image tensor from decoders step output_type (`str`, *optional*, defaults to pil): Output format: 'pil', 'np', 'pt'. Outputs: images (`list`): Generated images. """ model_name = "qwenimage" @property def description(self) -> str: return "postprocess the generated image" @property def expected_components(self) -> list[ComponentSpec]: return [ ComponentSpec( "image_processor", VaeImageProcessor, config=FrozenDict({"vae_scale_factor": 16}), default_creation_method="from_config", ), ] @property def inputs(self) -> list[InputParam]: return [ InputParam( name="images", required=True, type_hint=torch.Tensor, description="the generated image tensor from decoders step", ), InputParam.template("output_type"), ] @property def intermediate_outputs(self) -> list[OutputParam]: return [OutputParam.template("images")] @staticmethod def check_inputs(output_type): if output_type not in ["pil", "np", "pt"]: raise ValueError(f"Invalid output_type: {output_type}") @torch.no_grad() def __call__(self, components: QwenImageModularPipeline, state: PipelineState): block_state = self.get_block_state(state) self.check_inputs(block_state.output_type) block_state.images = components.image_processor.postprocess( image=block_state.images, output_type=block_state.output_type, ) self.set_block_state(state, block_state) return components, state # auto_docstring class QwenImageInpaintProcessImagesOutputStep(ModularPipelineBlocks): """ postprocess the generated image, optional apply the mask overally to the original image.. Components: image_mask_processor (`InpaintProcessor`) Inputs: images (`Tensor`): the generated image tensor from decoders step output_type (`str`, *optional*, defaults to pil): Output format: 'pil', 'np', 'pt'. mask_overlay_kwargs (`dict`, *optional*): The kwargs for the postprocess step to apply the mask overlay. generated in InpaintProcessImagesInputStep. Outputs: images (`list`): Generated images. """ model_name = "qwenimage" @property def description(self) -> str: return "postprocess the generated image, optional apply the mask overally to the original image.." @property def expected_components(self) -> list[ComponentSpec]: return [ ComponentSpec( "image_mask_processor", InpaintProcessor, config=FrozenDict({"vae_scale_factor": 16}), default_creation_method="from_config", ), ] @property def inputs(self) -> list[InputParam]: return [ InputParam( name="images", required=True, type_hint=torch.Tensor, description="the generated image tensor from decoders step", ), InputParam.template("output_type"), InputParam( name="mask_overlay_kwargs", type_hint=dict[str, Any], description="The kwargs for the postprocess step to apply the mask overlay. generated in InpaintProcessImagesInputStep.", ), ] @property def intermediate_outputs(self) -> list[OutputParam]: return [OutputParam.template("images")] @staticmethod def check_inputs(output_type, mask_overlay_kwargs): if output_type not in ["pil", "np", "pt"]: raise ValueError(f"Invalid output_type: {output_type}") if mask_overlay_kwargs and output_type != "pil": raise ValueError("only support output_type 'pil' for mask overlay") @torch.no_grad() def __call__(self, components: QwenImageModularPipeline, state: PipelineState): block_state = self.get_block_state(state) self.check_inputs(block_state.output_type, block_state.mask_overlay_kwargs) if block_state.mask_overlay_kwargs is None: mask_overlay_kwargs = {} else: mask_overlay_kwargs = block_state.mask_overlay_kwargs block_state.images = components.image_mask_processor.postprocess( image=block_state.images, **mask_overlay_kwargs, ) self.set_block_state(state, block_state) return components, state
{ "repo_id": "huggingface/diffusers", "file_path": "src/diffusers/modular_pipelines/qwenimage/decoders.py", "license": "Apache License 2.0", "lines": 406, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
huggingface/diffusers:src/diffusers/modular_pipelines/qwenimage/denoise.py
# Copyright 2025 Qwen-Image Team and The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import inspect import torch from ...configuration_utils import FrozenDict from ...guiders import ClassifierFreeGuidance from ...models import QwenImageControlNetModel, QwenImageTransformer2DModel from ...schedulers import FlowMatchEulerDiscreteScheduler from ...utils import logging from ..modular_pipeline import BlockState, LoopSequentialPipelineBlocks, ModularPipelineBlocks, PipelineState from ..modular_pipeline_utils import ComponentSpec, InputParam, OutputParam from .modular_pipeline import QwenImageModularPipeline logger = logging.get_logger(__name__) # ==================== # 1. LOOP STEPS (run at each denoising step) # ==================== # loop step:before denoiser class QwenImageLoopBeforeDenoiser(ModularPipelineBlocks): model_name = "qwenimage" @property def description(self) -> str: return ( "step within the denoising loop that prepares the latent input for the denoiser. " "This block should be used to compose the `sub_blocks` attribute of a `LoopSequentialPipelineBlocks` " "object (e.g. `QwenImageDenoiseLoopWrapper`)" ) @property def inputs(self) -> list[InputParam]: return [ InputParam( name="latents", required=True, type_hint=torch.Tensor, description="The initial latents to use for the denoising process. Can be generated in prepare_latent step.", ), ] @torch.no_grad() def __call__(self, components: QwenImageModularPipeline, block_state: BlockState, i: int, t: torch.Tensor): # one timestep block_state.timestep = t.expand(block_state.latents.shape[0]).to(block_state.latents.dtype) block_state.latent_model_input = block_state.latents return components, block_state class QwenImageEditLoopBeforeDenoiser(ModularPipelineBlocks): model_name = "qwenimage-edit" @property def description(self) -> str: return ( "step within the denoising loop that prepares the latent input for the denoiser. " "This block should be used to compose the `sub_blocks` attribute of a `LoopSequentialPipelineBlocks` " "object (e.g. `QwenImageDenoiseLoopWrapper`)" ) @property def inputs(self) -> list[InputParam]: return [ InputParam( name="latents", required=True, type_hint=torch.Tensor, description="The initial latents to use for the denoising process. Can be generated in prepare_latent step.", ), InputParam.template("image_latents"), ] @torch.no_grad() def __call__(self, components: QwenImageModularPipeline, block_state: BlockState, i: int, t: torch.Tensor): # one timestep block_state.latent_model_input = torch.cat([block_state.latents, block_state.image_latents], dim=1) block_state.timestep = t.expand(block_state.latents.shape[0]).to(block_state.latents.dtype) return components, block_state class QwenImageLoopBeforeDenoiserControlNet(ModularPipelineBlocks): model_name = "qwenimage" @property def expected_components(self) -> list[ComponentSpec]: return [ ComponentSpec( "guider", ClassifierFreeGuidance, config=FrozenDict({"guidance_scale": 4.0}), default_creation_method="from_config", ), ComponentSpec("controlnet", QwenImageControlNetModel), ] @property def description(self) -> str: return ( "step within the denoising loop that runs the controlnet before the denoiser. " "This block should be used to compose the `sub_blocks` attribute of a `LoopSequentialPipelineBlocks` " "object (e.g. `QwenImageDenoiseLoopWrapper`)" ) @property def inputs(self) -> list[InputParam]: return [ InputParam( "control_image_latents", required=True, type_hint=torch.Tensor, description="The control image to use for the denoising process. Can be generated in prepare_controlnet_inputs step.", ), InputParam.template("controlnet_conditioning_scale", note="updated in prepare_controlnet_inputs step."), InputParam( name="controlnet_keep", required=True, type_hint=list[float], description="The controlnet keep values. Can be generated in prepare_controlnet_inputs step.", ), ] @torch.no_grad() def __call__(self, components: QwenImageModularPipeline, block_state: BlockState, i: int, t: int): # cond_scale for the timestep (controlnet input) if isinstance(block_state.controlnet_keep[i], list): block_state.cond_scale = [ c * s for c, s in zip(block_state.controlnet_conditioning_scale, block_state.controlnet_keep[i]) ] else: controlnet_cond_scale = block_state.controlnet_conditioning_scale if isinstance(controlnet_cond_scale, list): controlnet_cond_scale = controlnet_cond_scale[0] block_state.cond_scale = controlnet_cond_scale * block_state.controlnet_keep[i] # run controlnet for the guidance batch controlnet_block_samples = components.controlnet( hidden_states=block_state.latent_model_input, controlnet_cond=block_state.control_image_latents, conditioning_scale=block_state.cond_scale, timestep=block_state.timestep / 1000, img_shapes=block_state.img_shapes, encoder_hidden_states=block_state.prompt_embeds, encoder_hidden_states_mask=block_state.prompt_embeds_mask, return_dict=False, ) block_state.additional_cond_kwargs["controlnet_block_samples"] = controlnet_block_samples return components, block_state # loop step:denoiser class QwenImageLoopDenoiser(ModularPipelineBlocks): model_name = "qwenimage" @property def description(self) -> str: return ( "step within the denoising loop that denoise the latent input for the denoiser. " "This block should be used to compose the `sub_blocks` attribute of a `LoopSequentialPipelineBlocks` " "object (e.g. `QwenImageDenoiseLoopWrapper`)" ) @property def expected_components(self) -> list[ComponentSpec]: return [ ComponentSpec( "guider", ClassifierFreeGuidance, config=FrozenDict({"guidance_scale": 4.0}), default_creation_method="from_config", ), ComponentSpec("transformer", QwenImageTransformer2DModel), ] @property def inputs(self) -> list[InputParam]: return [ InputParam.template("attention_kwargs"), InputParam.template("denoiser_input_fields"), InputParam( "img_shapes", required=True, type_hint=list[tuple[int, int]], description="The shape of the image latents for RoPE calculation. can be generated in prepare_additional_inputs step.", ), ] @torch.no_grad() def __call__(self, components: QwenImageModularPipeline, block_state: BlockState, i: int, t: torch.Tensor): guider_inputs = { "encoder_hidden_states": ( getattr(block_state, "prompt_embeds", None), getattr(block_state, "negative_prompt_embeds", None), ), "encoder_hidden_states_mask": ( getattr(block_state, "prompt_embeds_mask", None), getattr(block_state, "negative_prompt_embeds_mask", None), ), } transformer_args = set(inspect.signature(components.transformer.forward).parameters.keys()) additional_cond_kwargs = {} for field_name, field_value in block_state.denoiser_input_fields.items(): if field_name in transformer_args and field_name not in guider_inputs: additional_cond_kwargs[field_name] = field_value block_state.additional_cond_kwargs.update(additional_cond_kwargs) components.guider.set_state(step=i, num_inference_steps=block_state.num_inference_steps, timestep=t) guider_state = components.guider.prepare_inputs(guider_inputs) for guider_state_batch in guider_state: components.guider.prepare_models(components.transformer) cond_kwargs = {input_name: getattr(guider_state_batch, input_name) for input_name in guider_inputs.keys()} # YiYi TODO: add cache context guider_state_batch.noise_pred = components.transformer( hidden_states=block_state.latent_model_input, timestep=block_state.timestep / 1000, attention_kwargs=block_state.attention_kwargs, return_dict=False, **cond_kwargs, **block_state.additional_cond_kwargs, )[0] components.guider.cleanup_models(components.transformer) guider_output = components.guider(guider_state) # apply guidance rescale pred_cond_norm = torch.norm(guider_output.pred_cond, dim=-1, keepdim=True) pred_norm = torch.norm(guider_output.pred, dim=-1, keepdim=True) block_state.noise_pred = guider_output.pred * (pred_cond_norm / pred_norm) return components, block_state class QwenImageEditLoopDenoiser(ModularPipelineBlocks): model_name = "qwenimage-edit" @property def description(self) -> str: return ( "step within the denoising loop that denoise the latent input for the denoiser. " "This block should be used to compose the `sub_blocks` attribute of a `LoopSequentialPipelineBlocks` " "object (e.g. `QwenImageDenoiseLoopWrapper`)" ) @property def expected_components(self) -> list[ComponentSpec]: return [ ComponentSpec( "guider", ClassifierFreeGuidance, config=FrozenDict({"guidance_scale": 4.0}), default_creation_method="from_config", ), ComponentSpec("transformer", QwenImageTransformer2DModel), ] @property def inputs(self) -> list[InputParam]: return [ InputParam.template("attention_kwargs"), InputParam.template("denoiser_input_fields"), InputParam( "img_shapes", required=True, type_hint=list[tuple[int, int]], description="The shape of the image latents for RoPE calculation. Can be generated in prepare_additional_inputs step.", ), ] @torch.no_grad() def __call__(self, components: QwenImageModularPipeline, block_state: BlockState, i: int, t: torch.Tensor): guider_inputs = { "encoder_hidden_states": ( getattr(block_state, "prompt_embeds", None), getattr(block_state, "negative_prompt_embeds", None), ), "encoder_hidden_states_mask": ( getattr(block_state, "prompt_embeds_mask", None), getattr(block_state, "negative_prompt_embeds_mask", None), ), } transformer_args = set(inspect.signature(components.transformer.forward).parameters.keys()) additional_cond_kwargs = {} for field_name, field_value in block_state.denoiser_input_fields.items(): if field_name in transformer_args and field_name not in guider_inputs: additional_cond_kwargs[field_name] = field_value block_state.additional_cond_kwargs.update(additional_cond_kwargs) components.guider.set_state(step=i, num_inference_steps=block_state.num_inference_steps, timestep=t) guider_state = components.guider.prepare_inputs(guider_inputs) for guider_state_batch in guider_state: components.guider.prepare_models(components.transformer) cond_kwargs = {input_name: getattr(guider_state_batch, input_name) for input_name in guider_inputs.keys()} # YiYi TODO: add cache context guider_state_batch.noise_pred = components.transformer( hidden_states=block_state.latent_model_input, timestep=block_state.timestep / 1000, attention_kwargs=block_state.attention_kwargs, return_dict=False, **cond_kwargs, **block_state.additional_cond_kwargs, )[0] components.guider.cleanup_models(components.transformer) guider_output = components.guider(guider_state) pred = guider_output.pred[:, : block_state.latents.size(1)] pred_cond = guider_output.pred_cond[:, : block_state.latents.size(1)] # apply guidance rescale pred_cond_norm = torch.norm(pred_cond, dim=-1, keepdim=True) pred_norm = torch.norm(pred, dim=-1, keepdim=True) block_state.noise_pred = pred * (pred_cond_norm / pred_norm) return components, block_state # loop step:after denoiser class QwenImageLoopAfterDenoiser(ModularPipelineBlocks): model_name = "qwenimage" @property def description(self) -> str: return ( "step within the denoising loop that updates the latents. " "This block should be used to compose the `sub_blocks` attribute of a `LoopSequentialPipelineBlocks` " "object (e.g. `QwenImageDenoiseLoopWrapper`)" ) @property def expected_components(self) -> list[ComponentSpec]: return [ ComponentSpec("scheduler", FlowMatchEulerDiscreteScheduler), ] @property def intermediate_outputs(self) -> list[OutputParam]: return [ OutputParam.template("latents"), ] @torch.no_grad() def __call__(self, components: QwenImageModularPipeline, block_state: BlockState, i: int, t: torch.Tensor): latents_dtype = block_state.latents.dtype block_state.latents = components.scheduler.step( block_state.noise_pred, t, block_state.latents, return_dict=False, )[0] if block_state.latents.dtype != latents_dtype: if torch.backends.mps.is_available(): # some platforms (eg. apple mps) misbehave due to a pytorch bug: https://github.com/pytorch/pytorch/pull/99272 block_state.latents = block_state.latents.to(latents_dtype) return components, block_state class QwenImageLoopAfterDenoiserInpaint(ModularPipelineBlocks): model_name = "qwenimage" @property def description(self) -> str: return ( "step within the denoising loop that updates the latents using mask and image_latents for inpainting. " "This block should be used to compose the `sub_blocks` attribute of a `LoopSequentialPipelineBlocks` " "object (e.g. `QwenImageDenoiseLoopWrapper`)" ) @property def inputs(self) -> list[InputParam]: return [ InputParam( "mask", required=True, type_hint=torch.Tensor, description="The mask to use for the inpainting process. Can be generated in inpaint prepare latents step.", ), InputParam.template("image_latents"), InputParam( "initial_noise", required=True, type_hint=torch.Tensor, description="The initial noise to use for the inpainting process. Can be generated in inpaint prepare latents step.", ), ] @property def intermediate_outputs(self) -> list[OutputParam]: return [ OutputParam.template("latents"), ] @torch.no_grad() def __call__(self, components: QwenImageModularPipeline, block_state: BlockState, i: int, t: torch.Tensor): block_state.init_latents_proper = block_state.image_latents if i < len(block_state.timesteps) - 1: block_state.noise_timestep = block_state.timesteps[i + 1] block_state.init_latents_proper = components.scheduler.scale_noise( block_state.init_latents_proper, torch.tensor([block_state.noise_timestep]), block_state.initial_noise ) block_state.latents = ( 1 - block_state.mask ) * block_state.init_latents_proper + block_state.mask * block_state.latents return components, block_state # ==================== # 2. DENOISE LOOP WRAPPER: define the denoising loop logic # ==================== class QwenImageDenoiseLoopWrapper(LoopSequentialPipelineBlocks): model_name = "qwenimage" @property def description(self) -> str: return ( "Pipeline block that iteratively denoise the latents over `timesteps`. " "The specific steps with each iteration can be customized with `sub_blocks` attributes" ) @property def loop_expected_components(self) -> list[ComponentSpec]: return [ ComponentSpec("scheduler", FlowMatchEulerDiscreteScheduler), ] @property def loop_inputs(self) -> list[InputParam]: return [ InputParam( name="timesteps", required=True, type_hint=torch.Tensor, description="The timesteps to use for the denoising process. Can be generated in set_timesteps step.", ), InputParam.template("num_inference_steps", required=True), ] @torch.no_grad() def __call__(self, components: QwenImageModularPipeline, state: PipelineState) -> PipelineState: block_state = self.get_block_state(state) block_state.num_warmup_steps = max( len(block_state.timesteps) - block_state.num_inference_steps * components.scheduler.order, 0 ) block_state.additional_cond_kwargs = {} with self.progress_bar(total=block_state.num_inference_steps) as progress_bar: for i, t in enumerate(block_state.timesteps): components, block_state = self.loop_step(components, block_state, i=i, t=t) if i == len(block_state.timesteps) - 1 or ( (i + 1) > block_state.num_warmup_steps and (i + 1) % components.scheduler.order == 0 ): progress_bar.update() self.set_block_state(state, block_state) return components, state # ==================== # 3. DENOISE STEPS: compose the denoising loop with loop wrapper + loop steps # ==================== # Qwen Image (text2image, image2image) # auto_docstring class QwenImageDenoiseStep(QwenImageDenoiseLoopWrapper): """ Denoise step that iteratively denoise the latents. Its loop logic is defined in `QwenImageDenoiseLoopWrapper.__call__` method At each iteration, it runs blocks defined in `sub_blocks` sequencially: - `QwenImageLoopBeforeDenoiser` - `QwenImageLoopDenoiser` - `QwenImageLoopAfterDenoiser` This block supports text2image and image2image tasks for QwenImage. Components: guider (`ClassifierFreeGuidance`) transformer (`QwenImageTransformer2DModel`) scheduler (`FlowMatchEulerDiscreteScheduler`) Inputs: timesteps (`Tensor`): The timesteps to use for the denoising process. Can be generated in set_timesteps step. num_inference_steps (`int`): The number of denoising steps. latents (`Tensor`): The initial latents to use for the denoising process. Can be generated in prepare_latent step. attention_kwargs (`dict`, *optional*): Additional kwargs for attention processors. **denoiser_input_fields (`None`, *optional*): conditional model inputs for the denoiser: e.g. prompt_embeds, negative_prompt_embeds, etc. img_shapes (`list`): The shape of the image latents for RoPE calculation. can be generated in prepare_additional_inputs step. Outputs: latents (`Tensor`): Denoised latents. """ model_name = "qwenimage" block_classes = [ QwenImageLoopBeforeDenoiser, QwenImageLoopDenoiser, QwenImageLoopAfterDenoiser, ] block_names = ["before_denoiser", "denoiser", "after_denoiser"] @property def description(self) -> str: return ( "Denoise step that iteratively denoise the latents.\n" "Its loop logic is defined in `QwenImageDenoiseLoopWrapper.__call__` method\n" "At each iteration, it runs blocks defined in `sub_blocks` sequencially:\n" " - `QwenImageLoopBeforeDenoiser`\n" " - `QwenImageLoopDenoiser`\n" " - `QwenImageLoopAfterDenoiser`\n" "This block supports text2image and image2image tasks for QwenImage." ) # Qwen Image (inpainting) # auto_docstring class QwenImageInpaintDenoiseStep(QwenImageDenoiseLoopWrapper): """ Denoise step that iteratively denoise the latents. Its loop logic is defined in `QwenImageDenoiseLoopWrapper.__call__` method At each iteration, it runs blocks defined in `sub_blocks` sequencially: - `QwenImageLoopBeforeDenoiser` - `QwenImageLoopDenoiser` - `QwenImageLoopAfterDenoiser` - `QwenImageLoopAfterDenoiserInpaint` This block supports inpainting tasks for QwenImage. Components: guider (`ClassifierFreeGuidance`) transformer (`QwenImageTransformer2DModel`) scheduler (`FlowMatchEulerDiscreteScheduler`) Inputs: timesteps (`Tensor`): The timesteps to use for the denoising process. Can be generated in set_timesteps step. num_inference_steps (`int`): The number of denoising steps. latents (`Tensor`): The initial latents to use for the denoising process. Can be generated in prepare_latent step. attention_kwargs (`dict`, *optional*): Additional kwargs for attention processors. **denoiser_input_fields (`None`, *optional*): conditional model inputs for the denoiser: e.g. prompt_embeds, negative_prompt_embeds, etc. img_shapes (`list`): The shape of the image latents for RoPE calculation. can be generated in prepare_additional_inputs step. mask (`Tensor`): The mask to use for the inpainting process. Can be generated in inpaint prepare latents step. image_latents (`Tensor`): image latents used to guide the image generation. Can be generated from vae_encoder step. initial_noise (`Tensor`): The initial noise to use for the inpainting process. Can be generated in inpaint prepare latents step. Outputs: latents (`Tensor`): Denoised latents. """ model_name = "qwenimage" block_classes = [ QwenImageLoopBeforeDenoiser, QwenImageLoopDenoiser, QwenImageLoopAfterDenoiser, QwenImageLoopAfterDenoiserInpaint, ] block_names = ["before_denoiser", "denoiser", "after_denoiser", "after_denoiser_inpaint"] @property def description(self) -> str: return ( "Denoise step that iteratively denoise the latents. \n" "Its loop logic is defined in `QwenImageDenoiseLoopWrapper.__call__` method \n" "At each iteration, it runs blocks defined in `sub_blocks` sequencially:\n" " - `QwenImageLoopBeforeDenoiser`\n" " - `QwenImageLoopDenoiser`\n" " - `QwenImageLoopAfterDenoiser`\n" " - `QwenImageLoopAfterDenoiserInpaint`\n" "This block supports inpainting tasks for QwenImage." ) # Qwen Image (text2image, image2image) with controlnet # auto_docstring class QwenImageControlNetDenoiseStep(QwenImageDenoiseLoopWrapper): """ Denoise step that iteratively denoise the latents. Its loop logic is defined in `QwenImageDenoiseLoopWrapper.__call__` method At each iteration, it runs blocks defined in `sub_blocks` sequencially: - `QwenImageLoopBeforeDenoiser` - `QwenImageLoopBeforeDenoiserControlNet` - `QwenImageLoopDenoiser` - `QwenImageLoopAfterDenoiser` This block supports text2img/img2img tasks with controlnet for QwenImage. Components: guider (`ClassifierFreeGuidance`) controlnet (`QwenImageControlNetModel`) transformer (`QwenImageTransformer2DModel`) scheduler (`FlowMatchEulerDiscreteScheduler`) Inputs: timesteps (`Tensor`): The timesteps to use for the denoising process. Can be generated in set_timesteps step. num_inference_steps (`int`): The number of denoising steps. latents (`Tensor`): The initial latents to use for the denoising process. Can be generated in prepare_latent step. control_image_latents (`Tensor`): The control image to use for the denoising process. Can be generated in prepare_controlnet_inputs step. controlnet_conditioning_scale (`float`, *optional*, defaults to 1.0): Scale for ControlNet conditioning. (updated in prepare_controlnet_inputs step.) controlnet_keep (`list`): The controlnet keep values. Can be generated in prepare_controlnet_inputs step. attention_kwargs (`dict`, *optional*): Additional kwargs for attention processors. **denoiser_input_fields (`None`, *optional*): conditional model inputs for the denoiser: e.g. prompt_embeds, negative_prompt_embeds, etc. img_shapes (`list`): The shape of the image latents for RoPE calculation. can be generated in prepare_additional_inputs step. Outputs: latents (`Tensor`): Denoised latents. """ model_name = "qwenimage" block_classes = [ QwenImageLoopBeforeDenoiser, QwenImageLoopBeforeDenoiserControlNet, QwenImageLoopDenoiser, QwenImageLoopAfterDenoiser, ] block_names = ["before_denoiser", "before_denoiser_controlnet", "denoiser", "after_denoiser"] @property def description(self) -> str: return ( "Denoise step that iteratively denoise the latents. \n" "Its loop logic is defined in `QwenImageDenoiseLoopWrapper.__call__` method \n" "At each iteration, it runs blocks defined in `sub_blocks` sequencially:\n" " - `QwenImageLoopBeforeDenoiser`\n" " - `QwenImageLoopBeforeDenoiserControlNet`\n" " - `QwenImageLoopDenoiser`\n" " - `QwenImageLoopAfterDenoiser`\n" "This block supports text2img/img2img tasks with controlnet for QwenImage." ) # Qwen Image (inpainting) with controlnet # auto_docstring class QwenImageInpaintControlNetDenoiseStep(QwenImageDenoiseLoopWrapper): """ Denoise step that iteratively denoise the latents. Its loop logic is defined in `QwenImageDenoiseLoopWrapper.__call__` method At each iteration, it runs blocks defined in `sub_blocks` sequencially: - `QwenImageLoopBeforeDenoiser` - `QwenImageLoopBeforeDenoiserControlNet` - `QwenImageLoopDenoiser` - `QwenImageLoopAfterDenoiser` - `QwenImageLoopAfterDenoiserInpaint` This block supports inpainting tasks with controlnet for QwenImage. Components: guider (`ClassifierFreeGuidance`) controlnet (`QwenImageControlNetModel`) transformer (`QwenImageTransformer2DModel`) scheduler (`FlowMatchEulerDiscreteScheduler`) Inputs: timesteps (`Tensor`): The timesteps to use for the denoising process. Can be generated in set_timesteps step. num_inference_steps (`int`): The number of denoising steps. latents (`Tensor`): The initial latents to use for the denoising process. Can be generated in prepare_latent step. control_image_latents (`Tensor`): The control image to use for the denoising process. Can be generated in prepare_controlnet_inputs step. controlnet_conditioning_scale (`float`, *optional*, defaults to 1.0): Scale for ControlNet conditioning. (updated in prepare_controlnet_inputs step.) controlnet_keep (`list`): The controlnet keep values. Can be generated in prepare_controlnet_inputs step. attention_kwargs (`dict`, *optional*): Additional kwargs for attention processors. **denoiser_input_fields (`None`, *optional*): conditional model inputs for the denoiser: e.g. prompt_embeds, negative_prompt_embeds, etc. img_shapes (`list`): The shape of the image latents for RoPE calculation. can be generated in prepare_additional_inputs step. mask (`Tensor`): The mask to use for the inpainting process. Can be generated in inpaint prepare latents step. image_latents (`Tensor`): image latents used to guide the image generation. Can be generated from vae_encoder step. initial_noise (`Tensor`): The initial noise to use for the inpainting process. Can be generated in inpaint prepare latents step. Outputs: latents (`Tensor`): Denoised latents. """ model_name = "qwenimage" block_classes = [ QwenImageLoopBeforeDenoiser, QwenImageLoopBeforeDenoiserControlNet, QwenImageLoopDenoiser, QwenImageLoopAfterDenoiser, QwenImageLoopAfterDenoiserInpaint, ] block_names = [ "before_denoiser", "before_denoiser_controlnet", "denoiser", "after_denoiser", "after_denoiser_inpaint", ] @property def description(self) -> str: return ( "Denoise step that iteratively denoise the latents. \n" "Its loop logic is defined in `QwenImageDenoiseLoopWrapper.__call__` method \n" "At each iteration, it runs blocks defined in `sub_blocks` sequencially:\n" " - `QwenImageLoopBeforeDenoiser`\n" " - `QwenImageLoopBeforeDenoiserControlNet`\n" " - `QwenImageLoopDenoiser`\n" " - `QwenImageLoopAfterDenoiser`\n" " - `QwenImageLoopAfterDenoiserInpaint`\n" "This block supports inpainting tasks with controlnet for QwenImage." ) # Qwen Image Edit (image2image) # auto_docstring class QwenImageEditDenoiseStep(QwenImageDenoiseLoopWrapper): """ Denoise step that iteratively denoise the latents. Its loop logic is defined in `QwenImageDenoiseLoopWrapper.__call__` method At each iteration, it runs blocks defined in `sub_blocks` sequencially: - `QwenImageEditLoopBeforeDenoiser` - `QwenImageEditLoopDenoiser` - `QwenImageLoopAfterDenoiser` This block supports QwenImage Edit. Components: guider (`ClassifierFreeGuidance`) transformer (`QwenImageTransformer2DModel`) scheduler (`FlowMatchEulerDiscreteScheduler`) Inputs: timesteps (`Tensor`): The timesteps to use for the denoising process. Can be generated in set_timesteps step. num_inference_steps (`int`): The number of denoising steps. latents (`Tensor`): The initial latents to use for the denoising process. Can be generated in prepare_latent step. image_latents (`Tensor`): image latents used to guide the image generation. Can be generated from vae_encoder step. attention_kwargs (`dict`, *optional*): Additional kwargs for attention processors. **denoiser_input_fields (`None`, *optional*): conditional model inputs for the denoiser: e.g. prompt_embeds, negative_prompt_embeds, etc. img_shapes (`list`): The shape of the image latents for RoPE calculation. Can be generated in prepare_additional_inputs step. Outputs: latents (`Tensor`): Denoised latents. """ model_name = "qwenimage-edit" block_classes = [ QwenImageEditLoopBeforeDenoiser, QwenImageEditLoopDenoiser, QwenImageLoopAfterDenoiser, ] block_names = ["before_denoiser", "denoiser", "after_denoiser"] @property def description(self) -> str: return ( "Denoise step that iteratively denoise the latents. \n" "Its loop logic is defined in `QwenImageDenoiseLoopWrapper.__call__` method \n" "At each iteration, it runs blocks defined in `sub_blocks` sequencially:\n" " - `QwenImageEditLoopBeforeDenoiser`\n" " - `QwenImageEditLoopDenoiser`\n" " - `QwenImageLoopAfterDenoiser`\n" "This block supports QwenImage Edit." ) # Qwen Image Edit (inpainting) # auto_docstring class QwenImageEditInpaintDenoiseStep(QwenImageDenoiseLoopWrapper): """ Denoise step that iteratively denoise the latents. Its loop logic is defined in `QwenImageDenoiseLoopWrapper.__call__` method At each iteration, it runs blocks defined in `sub_blocks` sequencially: - `QwenImageEditLoopBeforeDenoiser` - `QwenImageEditLoopDenoiser` - `QwenImageLoopAfterDenoiser` - `QwenImageLoopAfterDenoiserInpaint` This block supports inpainting tasks for QwenImage Edit. Components: guider (`ClassifierFreeGuidance`) transformer (`QwenImageTransformer2DModel`) scheduler (`FlowMatchEulerDiscreteScheduler`) Inputs: timesteps (`Tensor`): The timesteps to use for the denoising process. Can be generated in set_timesteps step. num_inference_steps (`int`): The number of denoising steps. latents (`Tensor`): The initial latents to use for the denoising process. Can be generated in prepare_latent step. image_latents (`Tensor`): image latents used to guide the image generation. Can be generated from vae_encoder step. attention_kwargs (`dict`, *optional*): Additional kwargs for attention processors. **denoiser_input_fields (`None`, *optional*): conditional model inputs for the denoiser: e.g. prompt_embeds, negative_prompt_embeds, etc. img_shapes (`list`): The shape of the image latents for RoPE calculation. Can be generated in prepare_additional_inputs step. mask (`Tensor`): The mask to use for the inpainting process. Can be generated in inpaint prepare latents step. initial_noise (`Tensor`): The initial noise to use for the inpainting process. Can be generated in inpaint prepare latents step. Outputs: latents (`Tensor`): Denoised latents. """ model_name = "qwenimage-edit" block_classes = [ QwenImageEditLoopBeforeDenoiser, QwenImageEditLoopDenoiser, QwenImageLoopAfterDenoiser, QwenImageLoopAfterDenoiserInpaint, ] block_names = ["before_denoiser", "denoiser", "after_denoiser", "after_denoiser_inpaint"] @property def description(self) -> str: return ( "Denoise step that iteratively denoise the latents. \n" "Its loop logic is defined in `QwenImageDenoiseLoopWrapper.__call__` method \n" "At each iteration, it runs blocks defined in `sub_blocks` sequencially:\n" " - `QwenImageEditLoopBeforeDenoiser`\n" " - `QwenImageEditLoopDenoiser`\n" " - `QwenImageLoopAfterDenoiser`\n" " - `QwenImageLoopAfterDenoiserInpaint`\n" "This block supports inpainting tasks for QwenImage Edit." ) # Qwen Image Layered (image2image) # auto_docstring class QwenImageLayeredDenoiseStep(QwenImageDenoiseLoopWrapper): """ Denoise step that iteratively denoise the latents. Its loop logic is defined in `QwenImageDenoiseLoopWrapper.__call__` method At each iteration, it runs blocks defined in `sub_blocks` sequencially: - `QwenImageEditLoopBeforeDenoiser` - `QwenImageEditLoopDenoiser` - `QwenImageLoopAfterDenoiser` This block supports QwenImage Layered. Components: guider (`ClassifierFreeGuidance`) transformer (`QwenImageTransformer2DModel`) scheduler (`FlowMatchEulerDiscreteScheduler`) Inputs: timesteps (`Tensor`): The timesteps to use for the denoising process. Can be generated in set_timesteps step. num_inference_steps (`int`): The number of denoising steps. latents (`Tensor`): The initial latents to use for the denoising process. Can be generated in prepare_latent step. image_latents (`Tensor`): image latents used to guide the image generation. Can be generated from vae_encoder step. attention_kwargs (`dict`, *optional*): Additional kwargs for attention processors. **denoiser_input_fields (`None`, *optional*): conditional model inputs for the denoiser: e.g. prompt_embeds, negative_prompt_embeds, etc. img_shapes (`list`): The shape of the image latents for RoPE calculation. Can be generated in prepare_additional_inputs step. Outputs: latents (`Tensor`): Denoised latents. """ model_name = "qwenimage-layered" block_classes = [ QwenImageEditLoopBeforeDenoiser, QwenImageEditLoopDenoiser, QwenImageLoopAfterDenoiser, ] block_names = ["before_denoiser", "denoiser", "after_denoiser"] @property def description(self) -> str: return ( "Denoise step that iteratively denoise the latents. \n" "Its loop logic is defined in `QwenImageDenoiseLoopWrapper.__call__` method \n" "At each iteration, it runs blocks defined in `sub_blocks` sequencially:\n" " - `QwenImageEditLoopBeforeDenoiser`\n" " - `QwenImageEditLoopDenoiser`\n" " - `QwenImageLoopAfterDenoiser`\n" "This block supports QwenImage Layered." )
{ "repo_id": "huggingface/diffusers", "file_path": "src/diffusers/modular_pipelines/qwenimage/denoise.py", "license": "Apache License 2.0", "lines": 807, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
huggingface/diffusers:src/diffusers/modular_pipelines/qwenimage/encoders.py
# Copyright 2025 Qwen-Image Team and The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """ Text and VAE encoder blocks for QwenImage pipelines. """ import PIL import torch from transformers import Qwen2_5_VLForConditionalGeneration, Qwen2Tokenizer, Qwen2VLProcessor from ...configuration_utils import FrozenDict from ...guiders import ClassifierFreeGuidance from ...image_processor import InpaintProcessor, VaeImageProcessor, is_valid_image, is_valid_image_imagelist from ...models import AutoencoderKLQwenImage, QwenImageControlNetModel, QwenImageMultiControlNetModel from ...pipelines.qwenimage.pipeline_qwenimage_edit import calculate_dimensions from ...utils import logging from ...utils.torch_utils import unwrap_module from ..modular_pipeline import ModularPipelineBlocks, PipelineState from ..modular_pipeline_utils import ComponentSpec, InputParam, OutputParam from .modular_pipeline import QwenImageModularPipeline from .prompt_templates import ( QWENIMAGE_EDIT_PLUS_IMG_TEMPLATE, QWENIMAGE_EDIT_PLUS_PROMPT_TEMPLATE, QWENIMAGE_EDIT_PLUS_PROMPT_TEMPLATE_START_IDX, QWENIMAGE_EDIT_PROMPT_TEMPLATE, QWENIMAGE_EDIT_PROMPT_TEMPLATE_START_IDX, QWENIMAGE_LAYERED_CAPTION_PROMPT_CN, QWENIMAGE_LAYERED_CAPTION_PROMPT_EN, QWENIMAGE_PROMPT_TEMPLATE, QWENIMAGE_PROMPT_TEMPLATE_START_IDX, ) logger = logging.get_logger(__name__) def _extract_masked_hidden(hidden_states: torch.Tensor, mask: torch.Tensor): bool_mask = mask.bool() valid_lengths = bool_mask.sum(dim=1) selected = hidden_states[bool_mask] split_result = torch.split(selected, valid_lengths.tolist(), dim=0) return split_result def get_qwen_prompt_embeds( text_encoder, tokenizer, prompt: str | list[str] = None, prompt_template_encode: str = QWENIMAGE_PROMPT_TEMPLATE, prompt_template_encode_start_idx: int = QWENIMAGE_PROMPT_TEMPLATE_START_IDX, tokenizer_max_length: int = 1024, device: torch.device | None = None, ): prompt = [prompt] if isinstance(prompt, str) else prompt template = prompt_template_encode drop_idx = prompt_template_encode_start_idx txt = [template.format(e) for e in prompt] txt_tokens = tokenizer( txt, max_length=tokenizer_max_length + drop_idx, padding=True, truncation=True, return_tensors="pt" ).to(device) encoder_hidden_states = text_encoder( input_ids=txt_tokens.input_ids, attention_mask=txt_tokens.attention_mask, output_hidden_states=True, ) hidden_states = encoder_hidden_states.hidden_states[-1] split_hidden_states = _extract_masked_hidden(hidden_states, txt_tokens.attention_mask) split_hidden_states = [e[drop_idx:] for e in split_hidden_states] attn_mask_list = [torch.ones(e.size(0), dtype=torch.long, device=e.device) for e in split_hidden_states] max_seq_len = max([e.size(0) for e in split_hidden_states]) prompt_embeds = torch.stack( [torch.cat([u, u.new_zeros(max_seq_len - u.size(0), u.size(1))]) for u in split_hidden_states] ) encoder_attention_mask = torch.stack( [torch.cat([u, u.new_zeros(max_seq_len - u.size(0))]) for u in attn_mask_list] ) prompt_embeds = prompt_embeds.to(device=device) return prompt_embeds, encoder_attention_mask def get_qwen_prompt_embeds_edit( text_encoder, processor, prompt: str | list[str] = None, image: torch.Tensor | None = None, prompt_template_encode: str = QWENIMAGE_EDIT_PROMPT_TEMPLATE, prompt_template_encode_start_idx: int = QWENIMAGE_EDIT_PROMPT_TEMPLATE_START_IDX, device: torch.device | None = None, ): prompt = [prompt] if isinstance(prompt, str) else prompt template = prompt_template_encode drop_idx = prompt_template_encode_start_idx txt = [template.format(e) for e in prompt] model_inputs = processor( text=txt, images=image, padding=True, return_tensors="pt", ).to(device) outputs = text_encoder( input_ids=model_inputs.input_ids, attention_mask=model_inputs.attention_mask, pixel_values=model_inputs.pixel_values, image_grid_thw=model_inputs.image_grid_thw, output_hidden_states=True, ) hidden_states = outputs.hidden_states[-1] split_hidden_states = _extract_masked_hidden(hidden_states, model_inputs.attention_mask) split_hidden_states = [e[drop_idx:] for e in split_hidden_states] attn_mask_list = [torch.ones(e.size(0), dtype=torch.long, device=e.device) for e in split_hidden_states] max_seq_len = max([e.size(0) for e in split_hidden_states]) prompt_embeds = torch.stack( [torch.cat([u, u.new_zeros(max_seq_len - u.size(0), u.size(1))]) for u in split_hidden_states] ) encoder_attention_mask = torch.stack( [torch.cat([u, u.new_zeros(max_seq_len - u.size(0))]) for u in attn_mask_list] ) prompt_embeds = prompt_embeds.to(device=device) return prompt_embeds, encoder_attention_mask def get_qwen_prompt_embeds_edit_plus( text_encoder, processor, prompt: str | list[str] = None, image: torch.Tensor | list[PIL.Image.Image, PIL.Image.Image] | None = None, prompt_template_encode: str = QWENIMAGE_EDIT_PLUS_PROMPT_TEMPLATE, img_template_encode: str = QWENIMAGE_EDIT_PLUS_IMG_TEMPLATE, prompt_template_encode_start_idx: int = QWENIMAGE_EDIT_PLUS_PROMPT_TEMPLATE_START_IDX, device: torch.device | None = None, ): prompt = [prompt] if isinstance(prompt, str) else prompt if isinstance(image, list): base_img_prompt = "" for i, img in enumerate(image): base_img_prompt += img_template_encode.format(i + 1) elif image is not None: base_img_prompt = img_template_encode.format(1) else: base_img_prompt = "" template = prompt_template_encode drop_idx = prompt_template_encode_start_idx txt = [template.format(base_img_prompt + e) for e in prompt] model_inputs = processor( text=txt, images=image, padding=True, return_tensors="pt", ).to(device) outputs = text_encoder( input_ids=model_inputs.input_ids, attention_mask=model_inputs.attention_mask, pixel_values=model_inputs.pixel_values, image_grid_thw=model_inputs.image_grid_thw, output_hidden_states=True, ) hidden_states = outputs.hidden_states[-1] split_hidden_states = _extract_masked_hidden(hidden_states, model_inputs.attention_mask) split_hidden_states = [e[drop_idx:] for e in split_hidden_states] attn_mask_list = [torch.ones(e.size(0), dtype=torch.long, device=e.device) for e in split_hidden_states] max_seq_len = max([e.size(0) for e in split_hidden_states]) prompt_embeds = torch.stack( [torch.cat([u, u.new_zeros(max_seq_len - u.size(0), u.size(1))]) for u in split_hidden_states] ) encoder_attention_mask = torch.stack( [torch.cat([u, u.new_zeros(max_seq_len - u.size(0))]) for u in attn_mask_list] ) prompt_embeds = prompt_embeds.to(device=device) return prompt_embeds, encoder_attention_mask # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion_img2img.retrieve_latents def retrieve_latents( encoder_output: torch.Tensor, generator: torch.Generator | None = None, sample_mode: str = "sample" ): if hasattr(encoder_output, "latent_dist") and sample_mode == "sample": return encoder_output.latent_dist.sample(generator) elif hasattr(encoder_output, "latent_dist") and sample_mode == "argmax": return encoder_output.latent_dist.mode() elif hasattr(encoder_output, "latents"): return encoder_output.latents else: raise AttributeError("Could not access latents of provided encoder_output") # Modified from diffusers.pipelines.qwenimage.pipeline_qwenimage.QwenImagePipeline._encode_vae_image def encode_vae_image( image: torch.Tensor, vae: AutoencoderKLQwenImage, generator: torch.Generator, device: torch.device, dtype: torch.dtype, latent_channels: int = 16, sample_mode: str = "argmax", ): if not isinstance(image, torch.Tensor): raise ValueError(f"Expected image to be a tensor, got {type(image)}.") # preprocessed image should be a 4D tensor: batch_size, num_channels, height, width if image.dim() == 4: image = image.unsqueeze(2) elif image.dim() != 5: raise ValueError(f"Expected image dims 4 or 5, got {image.dim()}.") image = image.to(device=device, dtype=dtype) if isinstance(generator, list): image_latents = [ retrieve_latents(vae.encode(image[i : i + 1]), generator=generator[i], sample_mode=sample_mode) for i in range(image.shape[0]) ] image_latents = torch.cat(image_latents, dim=0) else: image_latents = retrieve_latents(vae.encode(image), generator=generator, sample_mode=sample_mode) latents_mean = ( torch.tensor(vae.config.latents_mean) .view(1, latent_channels, 1, 1, 1) .to(image_latents.device, image_latents.dtype) ) latents_std = ( torch.tensor(vae.config.latents_std) .view(1, latent_channels, 1, 1, 1) .to(image_latents.device, image_latents.dtype) ) image_latents = (image_latents - latents_mean) / latents_std return image_latents # ==================== # 1. RESIZE # ==================== # In QwenImage pipelines, resize is a separate step because the resized image is used in VL encoding and vae encoder blocks: # # image (PIL.Image.Image) # │ # ▼ # resized_image ([PIL.Image.Image]) # │ # ├──► text_encoder ──► prompt_embeds, prompt_embeds_mask # │ (VL encoding needs the resized image for vision-language fusion) # │ # └──► image_processor ──► processed_image (torch.Tensor, pixel space) # │ # ▼ # vae_encoder ──► image_latents (torch.Tensor, latent space) # # In most of our other pipelines, resizing is done as part of the image preprocessing step. # ==================== # auto_docstring class QwenImageEditResizeStep(ModularPipelineBlocks): """ Image Resize step that resize the image to target area while maintaining the aspect ratio. Components: image_resize_processor (`VaeImageProcessor`) Inputs: image (`Image | list`): Reference image(s) for denoising. Can be a single image or list of images. Outputs: resized_image (`list`): The resized images """ model_name = "qwenimage-edit" @property def description(self) -> str: return "Image Resize step that resize the image to target area while maintaining the aspect ratio." @property def expected_components(self) -> list[ComponentSpec]: return [ ComponentSpec( "image_resize_processor", VaeImageProcessor, config=FrozenDict({"vae_scale_factor": 16}), default_creation_method="from_config", ), ] @property def inputs(self) -> list[InputParam]: return [InputParam.template("image")] @property def intermediate_outputs(self) -> list[OutputParam]: return [ OutputParam( name="resized_image", type_hint=list[PIL.Image.Image], description="The resized images", ), ] @torch.no_grad() def __call__(self, components: QwenImageModularPipeline, state: PipelineState): block_state = self.get_block_state(state) images = block_state.image if not is_valid_image_imagelist(images): raise ValueError(f"Images must be image or list of images but are {type(images)}") if is_valid_image(images): images = [images] image_width, image_height = images[0].size calculated_width, calculated_height, _ = calculate_dimensions(1024 * 1024, image_width / image_height) resized_images = [ components.image_resize_processor.resize(image, height=calculated_height, width=calculated_width) for image in images ] block_state.resized_image = resized_images self.set_block_state(state, block_state) return components, state # auto_docstring class QwenImageLayeredResizeStep(ModularPipelineBlocks): """ Image Resize step that resize the image to a target area (defined by the resolution parameter from user) while maintaining the aspect ratio. Components: image_resize_processor (`VaeImageProcessor`) Inputs: image (`Image | list`): Reference image(s) for denoising. Can be a single image or list of images. resolution (`int`, *optional*, defaults to 640): The target area to resize the image to, can be 1024 or 640 Outputs: resized_image (`list`): The resized images """ model_name = "qwenimage-layered" @property def description(self) -> str: return "Image Resize step that resize the image to a target area (defined by the resolution parameter from user) while maintaining the aspect ratio." @property def expected_components(self) -> list[ComponentSpec]: return [ ComponentSpec( "image_resize_processor", VaeImageProcessor, config=FrozenDict({"vae_scale_factor": 16}), default_creation_method="from_config", ), ] @property def inputs(self) -> list[InputParam]: return [ InputParam.template("image"), InputParam( name="resolution", default=640, type_hint=int, description="The target area to resize the image to, can be 1024 or 640", ), ] @property def intermediate_outputs(self) -> list[OutputParam]: return [ OutputParam( name="resized_image", type_hint=list[PIL.Image.Image], description="The resized images", ) ] @staticmethod def check_inputs(resolution: int): if resolution not in [1024, 640]: raise ValueError(f"Resolution must be 1024 or 640 but is {resolution}") @torch.no_grad() def __call__(self, components: QwenImageModularPipeline, state: PipelineState): block_state = self.get_block_state(state) self.check_inputs(resolution=block_state.resolution) images = block_state.image if not is_valid_image_imagelist(images): raise ValueError(f"Images must be image or list of images but are {type(images)}") if is_valid_image(images): images = [images] image_width, image_height = images[0].size target_area = block_state.resolution * block_state.resolution calculated_width, calculated_height, _ = calculate_dimensions(target_area, image_width / image_height) resized_images = [ components.image_resize_processor.resize(image, height=calculated_height, width=calculated_width) for image in images ] block_state.resized_image = resized_images self.set_block_state(state, block_state) return components, state # auto_docstring class QwenImageEditPlusResizeStep(ModularPipelineBlocks): """ Resize images for QwenImage Edit Plus pipeline. Produces two outputs: resized_image (1024x1024) for VAE encoding, resized_cond_image (384x384) for VL text encoding. Each image is resized independently based on its own aspect ratio. Components: image_resize_processor (`VaeImageProcessor`) Inputs: image (`Image | list`): Reference image(s) for denoising. Can be a single image or list of images. Outputs: resized_image (`list`): Images resized to 1024x1024 target area for VAE encoding resized_cond_image (`list`): Images resized to 384x384 target area for VL text encoding """ model_name = "qwenimage-edit-plus" @property def description(self) -> str: return ( "Resize images for QwenImage Edit Plus pipeline.\n" "Produces two outputs: resized_image (1024x1024) for VAE encoding, " "resized_cond_image (384x384) for VL text encoding.\n" "Each image is resized independently based on its own aspect ratio." ) @property def expected_components(self) -> list[ComponentSpec]: return [ ComponentSpec( "image_resize_processor", VaeImageProcessor, config=FrozenDict({"vae_scale_factor": 16}), default_creation_method="from_config", ), ] @property def inputs(self) -> list[InputParam]: # image return [InputParam.template("image")] @property def intermediate_outputs(self) -> list[OutputParam]: return [ OutputParam( name="resized_image", type_hint=list[PIL.Image.Image], description="Images resized to 1024x1024 target area for VAE encoding", ), OutputParam( name="resized_cond_image", type_hint=list[PIL.Image.Image], description="Images resized to 384x384 target area for VL text encoding", ), ] @torch.no_grad() def __call__(self, components: QwenImageModularPipeline, state: PipelineState): block_state = self.get_block_state(state) images = block_state.image if not is_valid_image_imagelist(images): raise ValueError(f"Images must be image or list of images but are {type(images)}") if is_valid_image(images): images = [images] # Resize each image independently based on its own aspect ratio resized_images = [] resized_cond_images = [] for image in images: image_width, image_height = image.size # For VAE encoder (1024x1024 target area) vae_width, vae_height, _ = calculate_dimensions(1024 * 1024, image_width / image_height) resized_images.append(components.image_resize_processor.resize(image, height=vae_height, width=vae_width)) # For VL text encoder (384x384 target area) vl_width, vl_height, _ = calculate_dimensions(384 * 384, image_width / image_height) resized_cond_images.append( components.image_resize_processor.resize(image, height=vl_height, width=vl_width) ) block_state.resized_image = resized_images block_state.resized_cond_image = resized_cond_images self.set_block_state(state, block_state) return components, state # ==================== # 2. GET IMAGE PROMPT # ==================== # auto_docstring class QwenImageLayeredGetImagePromptStep(ModularPipelineBlocks): """ Auto-caption step that generates a text prompt from the input image if none is provided. Uses the VL model (text_encoder) to generate a description of the image. If prompt is already provided, this step passes through unchanged. Components: text_encoder (`Qwen2_5_VLForConditionalGeneration`) processor (`Qwen2VLProcessor`) Inputs: prompt (`str`, *optional*): The prompt or prompts to guide image generation. resized_image (`Image`): The image to generate caption from, should be resized use the resize step use_en_prompt (`bool`, *optional*, defaults to False): Whether to use English prompt template Outputs: prompt (`str`): The prompt or prompts to guide image generation. If not provided, updated using image caption """ model_name = "qwenimage-layered" def __init__(self): self.image_caption_prompt_en = QWENIMAGE_LAYERED_CAPTION_PROMPT_EN self.image_caption_prompt_cn = QWENIMAGE_LAYERED_CAPTION_PROMPT_CN super().__init__() @property def description(self) -> str: return ( "Auto-caption step that generates a text prompt from the input image if none is provided.\n" "Uses the VL model (text_encoder) to generate a description of the image.\n" "If prompt is already provided, this step passes through unchanged." ) @property def expected_components(self) -> list[ComponentSpec]: return [ ComponentSpec("text_encoder", Qwen2_5_VLForConditionalGeneration), ComponentSpec("processor", Qwen2VLProcessor), ] @property def inputs(self) -> list[InputParam]: return [ InputParam.template( "prompt", required=False ), # it is not required for qwenimage-layered, unlike other pipelines InputParam( name="resized_image", required=True, type_hint=PIL.Image.Image, description="The image to generate caption from, should be resized use the resize step", ), InputParam( name="use_en_prompt", default=False, type_hint=bool, description="Whether to use English prompt template", ), ] @property def intermediate_outputs(self) -> list[OutputParam]: return [ OutputParam( name="prompt", type_hint=str, description="The prompt or prompts to guide image generation. If not provided, updated using image caption", ), ] @torch.no_grad() def __call__(self, components: QwenImageModularPipeline, state: PipelineState) -> PipelineState: block_state = self.get_block_state(state) device = components._execution_device # If prompt is empty or None, generate caption from image if block_state.prompt is None or block_state.prompt == "" or block_state.prompt == " ": if block_state.use_en_prompt: caption_prompt = self.image_caption_prompt_en else: caption_prompt = self.image_caption_prompt_cn model_inputs = components.processor( text=caption_prompt, images=block_state.resized_image, padding=True, return_tensors="pt", ).to(device) generated_ids = components.text_encoder.generate(**model_inputs, max_new_tokens=512) generated_ids_trimmed = [ out_ids[len(in_ids) :] for in_ids, out_ids in zip(model_inputs.input_ids, generated_ids) ] output_text = components.processor.batch_decode( generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False )[0] block_state.prompt = output_text.strip() self.set_block_state(state, block_state) return components, state # ==================== # 3. TEXT ENCODER # ==================== # auto_docstring class QwenImageTextEncoderStep(ModularPipelineBlocks): """ Text Encoder step that generates text embeddings to guide the image generation. Components: text_encoder (`Qwen2_5_VLForConditionalGeneration`): The text encoder to use tokenizer (`Qwen2Tokenizer`): The tokenizer to use guider (`ClassifierFreeGuidance`) Inputs: prompt (`str`): The prompt or prompts to guide image generation. negative_prompt (`str`, *optional*): The prompt or prompts not to guide the image generation. max_sequence_length (`int`, *optional*, defaults to 1024): Maximum sequence length for prompt encoding. Outputs: prompt_embeds (`Tensor`): The prompt embeddings. prompt_embeds_mask (`Tensor`): The encoder attention mask. negative_prompt_embeds (`Tensor`): The negative prompt embeddings. negative_prompt_embeds_mask (`Tensor`): The negative prompt embeddings mask. """ model_name = "qwenimage" def __init__(self): self.prompt_template_encode = QWENIMAGE_PROMPT_TEMPLATE self.prompt_template_encode_start_idx = QWENIMAGE_PROMPT_TEMPLATE_START_IDX self.tokenizer_max_length = 1024 super().__init__() @property def description(self) -> str: return "Text Encoder step that generates text embeddings to guide the image generation." @property def expected_components(self) -> list[ComponentSpec]: return [ ComponentSpec("text_encoder", Qwen2_5_VLForConditionalGeneration, description="The text encoder to use"), ComponentSpec("tokenizer", Qwen2Tokenizer, description="The tokenizer to use"), ComponentSpec( "guider", ClassifierFreeGuidance, config=FrozenDict({"guidance_scale": 4.0}), default_creation_method="from_config", ), ] @property def inputs(self) -> list[InputParam]: return [ InputParam.template("prompt"), InputParam.template("negative_prompt"), InputParam.template("max_sequence_length", default=1024), ] @property def intermediate_outputs(self) -> list[OutputParam]: return [ OutputParam.template("prompt_embeds"), OutputParam.template("prompt_embeds_mask"), OutputParam.template("negative_prompt_embeds"), OutputParam.template("negative_prompt_embeds_mask"), ] @staticmethod def check_inputs(prompt, negative_prompt, max_sequence_length): if not isinstance(prompt, str) and not isinstance(prompt, list): raise ValueError(f"`prompt` has to be of type `str` or `list` but is {type(prompt)}") if ( negative_prompt is not None and not isinstance(negative_prompt, str) and not isinstance(negative_prompt, list) ): raise ValueError(f"`negative_prompt` has to be of type `str` or `list` but is {type(negative_prompt)}") if max_sequence_length is not None and max_sequence_length > 1024: raise ValueError(f"`max_sequence_length` cannot be greater than 1024 but is {max_sequence_length}") @torch.no_grad() def __call__(self, components: QwenImageModularPipeline, state: PipelineState): block_state = self.get_block_state(state) device = components._execution_device self.check_inputs(block_state.prompt, block_state.negative_prompt, block_state.max_sequence_length) block_state.prompt_embeds, block_state.prompt_embeds_mask = get_qwen_prompt_embeds( components.text_encoder, components.tokenizer, prompt=block_state.prompt, prompt_template_encode=self.prompt_template_encode, prompt_template_encode_start_idx=self.prompt_template_encode_start_idx, tokenizer_max_length=self.tokenizer_max_length, device=device, ) block_state.prompt_embeds = block_state.prompt_embeds[:, : block_state.max_sequence_length] block_state.prompt_embeds_mask = block_state.prompt_embeds_mask[:, : block_state.max_sequence_length] block_state.negative_prompt_embeds = None block_state.negative_prompt_embeds_mask = None if components.requires_unconditional_embeds: negative_prompt = block_state.negative_prompt or "" block_state.negative_prompt_embeds, block_state.negative_prompt_embeds_mask = get_qwen_prompt_embeds( components.text_encoder, components.tokenizer, prompt=negative_prompt, prompt_template_encode=self.prompt_template_encode, prompt_template_encode_start_idx=self.prompt_template_encode_start_idx, tokenizer_max_length=self.tokenizer_max_length, device=device, ) block_state.negative_prompt_embeds = block_state.negative_prompt_embeds[ :, : block_state.max_sequence_length ] block_state.negative_prompt_embeds_mask = block_state.negative_prompt_embeds_mask[ :, : block_state.max_sequence_length ] self.set_block_state(state, block_state) return components, state # auto_docstring class QwenImageEditTextEncoderStep(ModularPipelineBlocks): """ Text Encoder step that processes both prompt and image together to generate text embeddings for guiding image generation. Components: text_encoder (`Qwen2_5_VLForConditionalGeneration`) processor (`Qwen2VLProcessor`) guider (`ClassifierFreeGuidance`) Inputs: prompt (`str`): The prompt or prompts to guide image generation. negative_prompt (`str`, *optional*): The prompt or prompts not to guide the image generation. resized_image (`Image`): The image prompt to encode, should be resized using resize step Outputs: prompt_embeds (`Tensor`): The prompt embeddings. prompt_embeds_mask (`Tensor`): The encoder attention mask. negative_prompt_embeds (`Tensor`): The negative prompt embeddings. negative_prompt_embeds_mask (`Tensor`): The negative prompt embeddings mask. """ model_name = "qwenimage" def __init__(self): self.prompt_template_encode = QWENIMAGE_EDIT_PROMPT_TEMPLATE self.prompt_template_encode_start_idx = QWENIMAGE_EDIT_PROMPT_TEMPLATE_START_IDX super().__init__() @property def description(self) -> str: return "Text Encoder step that processes both prompt and image together to generate text embeddings for guiding image generation." @property def expected_components(self) -> list[ComponentSpec]: return [ ComponentSpec("text_encoder", Qwen2_5_VLForConditionalGeneration), ComponentSpec("processor", Qwen2VLProcessor), ComponentSpec( "guider", ClassifierFreeGuidance, config=FrozenDict({"guidance_scale": 4.0}), default_creation_method="from_config", ), ] @property def inputs(self) -> list[InputParam]: return [ InputParam.template("prompt"), InputParam.template("negative_prompt"), InputParam( name="resized_image", required=True, type_hint=PIL.Image.Image, description="The image prompt to encode, should be resized using resize step", ), ] @property def intermediate_outputs(self) -> list[OutputParam]: return [ OutputParam.template("prompt_embeds"), OutputParam.template("prompt_embeds_mask"), OutputParam.template("negative_prompt_embeds"), OutputParam.template("negative_prompt_embeds_mask"), ] @staticmethod def check_inputs(prompt, negative_prompt): if not isinstance(prompt, str) and not isinstance(prompt, list): raise ValueError(f"`prompt` has to be of type `str` or `list` but is {type(prompt)}") if ( negative_prompt is not None and not isinstance(negative_prompt, str) and not isinstance(negative_prompt, list) ): raise ValueError(f"`negative_prompt` has to be of type `str` or `list` but is {type(negative_prompt)}") @torch.no_grad() def __call__(self, components: QwenImageModularPipeline, state: PipelineState): block_state = self.get_block_state(state) self.check_inputs(block_state.prompt, block_state.negative_prompt) device = components._execution_device block_state.prompt_embeds, block_state.prompt_embeds_mask = get_qwen_prompt_embeds_edit( components.text_encoder, components.processor, prompt=block_state.prompt, image=block_state.resized_image, prompt_template_encode=self.prompt_template_encode, prompt_template_encode_start_idx=self.prompt_template_encode_start_idx, device=device, ) block_state.negative_prompt_embeds = None block_state.negative_prompt_embeds_mask = None if components.requires_unconditional_embeds: negative_prompt = block_state.negative_prompt or " " block_state.negative_prompt_embeds, block_state.negative_prompt_embeds_mask = get_qwen_prompt_embeds_edit( components.text_encoder, components.processor, prompt=negative_prompt, image=block_state.resized_image, prompt_template_encode=self.prompt_template_encode, prompt_template_encode_start_idx=self.prompt_template_encode_start_idx, device=device, ) self.set_block_state(state, block_state) return components, state # auto_docstring class QwenImageEditPlusTextEncoderStep(ModularPipelineBlocks): """ Text Encoder step for QwenImage Edit Plus that processes prompt and multiple images together to generate text embeddings for guiding image generation. Components: text_encoder (`Qwen2_5_VLForConditionalGeneration`) processor (`Qwen2VLProcessor`) guider (`ClassifierFreeGuidance`) Inputs: prompt (`str`): The prompt or prompts to guide image generation. negative_prompt (`str`, *optional*): The prompt or prompts not to guide the image generation. resized_cond_image (`Tensor`): The image(s) to encode, can be a single image or list of images, should be resized to 384x384 using resize step Outputs: prompt_embeds (`Tensor`): The prompt embeddings. prompt_embeds_mask (`Tensor`): The encoder attention mask. negative_prompt_embeds (`Tensor`): The negative prompt embeddings. negative_prompt_embeds_mask (`Tensor`): The negative prompt embeddings mask. """ model_name = "qwenimage-edit-plus" def __init__(self): self.prompt_template_encode = QWENIMAGE_EDIT_PLUS_PROMPT_TEMPLATE self.img_template_encode = QWENIMAGE_EDIT_PLUS_IMG_TEMPLATE self.prompt_template_encode_start_idx = QWENIMAGE_EDIT_PLUS_PROMPT_TEMPLATE_START_IDX super().__init__() @property def description(self) -> str: return ( "Text Encoder step for QwenImage Edit Plus that processes prompt and multiple images together " "to generate text embeddings for guiding image generation." ) @property def expected_components(self) -> list[ComponentSpec]: return [ ComponentSpec("text_encoder", Qwen2_5_VLForConditionalGeneration), ComponentSpec("processor", Qwen2VLProcessor), ComponentSpec( "guider", ClassifierFreeGuidance, config=FrozenDict({"guidance_scale": 4.0}), default_creation_method="from_config", ), ] @property def inputs(self) -> list[InputParam]: return [ InputParam.template("prompt"), InputParam.template("negative_prompt"), InputParam( name="resized_cond_image", required=True, type_hint=torch.Tensor, description="The image(s) to encode, can be a single image or list of images, should be resized to 384x384 using resize step", ), ] @property def intermediate_outputs(self) -> list[OutputParam]: return [ OutputParam.template("prompt_embeds"), OutputParam.template("prompt_embeds_mask"), OutputParam.template("negative_prompt_embeds"), OutputParam.template("negative_prompt_embeds_mask"), ] @staticmethod def check_inputs(prompt, negative_prompt): if not isinstance(prompt, str) and not isinstance(prompt, list): raise ValueError(f"`prompt` has to be of type `str` or `list` but is {type(prompt)}") if ( negative_prompt is not None and not isinstance(negative_prompt, str) and not isinstance(negative_prompt, list) ): raise ValueError(f"`negative_prompt` has to be of type `str` or `list` but is {type(negative_prompt)}") @torch.no_grad() def __call__(self, components: QwenImageModularPipeline, state: PipelineState): block_state = self.get_block_state(state) self.check_inputs(block_state.prompt, block_state.negative_prompt) device = components._execution_device block_state.prompt_embeds, block_state.prompt_embeds_mask = get_qwen_prompt_embeds_edit_plus( components.text_encoder, components.processor, prompt=block_state.prompt, image=block_state.resized_cond_image, prompt_template_encode=self.prompt_template_encode, img_template_encode=self.img_template_encode, prompt_template_encode_start_idx=self.prompt_template_encode_start_idx, device=device, ) block_state.negative_prompt_embeds = None block_state.negative_prompt_embeds_mask = None if components.requires_unconditional_embeds: negative_prompt = block_state.negative_prompt or " " block_state.negative_prompt_embeds, block_state.negative_prompt_embeds_mask = ( get_qwen_prompt_embeds_edit_plus( components.text_encoder, components.processor, prompt=negative_prompt, image=block_state.resized_cond_image, prompt_template_encode=self.prompt_template_encode, img_template_encode=self.img_template_encode, prompt_template_encode_start_idx=self.prompt_template_encode_start_idx, device=device, ) ) self.set_block_state(state, block_state) return components, state # ==================== # 4. IMAGE PREPROCESS # ==================== # auto_docstring class QwenImageInpaintProcessImagesInputStep(ModularPipelineBlocks): """ Image Preprocess step for inpainting task. This processes the image and mask inputs together. Images will be resized to the given height and width. Components: image_mask_processor (`InpaintProcessor`) Inputs: mask_image (`Image`): Mask image for inpainting. image (`Image | list`): Reference image(s) for denoising. Can be a single image or list of images. height (`int`, *optional*): The height in pixels of the generated image. width (`int`, *optional*): The width in pixels of the generated image. padding_mask_crop (`int`, *optional*): Padding for mask cropping in inpainting. Outputs: processed_image (`Tensor`): The processed image processed_mask_image (`Tensor`): The processed mask image mask_overlay_kwargs (`dict`): The kwargs for the postprocess step to apply the mask overlay """ model_name = "qwenimage" @property def description(self) -> str: return "Image Preprocess step for inpainting task. This processes the image and mask inputs together. Images will be resized to the given height and width." @property def expected_components(self) -> list[ComponentSpec]: return [ ComponentSpec( "image_mask_processor", InpaintProcessor, config=FrozenDict({"vae_scale_factor": 16}), default_creation_method="from_config", ), ] @property def inputs(self) -> list[InputParam]: return [ InputParam.template("mask_image"), InputParam.template("image"), InputParam.template("height"), InputParam.template("width"), InputParam.template("padding_mask_crop"), ] @property def intermediate_outputs(self) -> list[OutputParam]: return [ OutputParam( name="processed_image", type_hint=torch.Tensor, description="The processed image", ), OutputParam( name="processed_mask_image", type_hint=torch.Tensor, description="The processed mask image", ), OutputParam( name="mask_overlay_kwargs", type_hint=dict, description="The kwargs for the postprocess step to apply the mask overlay", ), ] @staticmethod def check_inputs(height, width, vae_scale_factor): if height is not None and height % (vae_scale_factor * 2) != 0: raise ValueError(f"Height must be divisible by {vae_scale_factor * 2} but is {height}") if width is not None and width % (vae_scale_factor * 2) != 0: raise ValueError(f"Width must be divisible by {vae_scale_factor * 2} but is {width}") @torch.no_grad() def __call__(self, components: QwenImageModularPipeline, state: PipelineState): block_state = self.get_block_state(state) self.check_inputs( height=block_state.height, width=block_state.width, vae_scale_factor=components.vae_scale_factor ) height = block_state.height or components.default_height width = block_state.width or components.default_width block_state.processed_image, block_state.processed_mask_image, block_state.mask_overlay_kwargs = ( components.image_mask_processor.preprocess( image=block_state.image, mask=block_state.mask_image, height=height, width=width, padding_mask_crop=block_state.padding_mask_crop, ) ) self.set_block_state(state, block_state) return components, state # auto_docstring class QwenImageEditInpaintProcessImagesInputStep(ModularPipelineBlocks): """ Image Preprocess step for inpainting task. This processes the image and mask inputs together. Images should be resized first. Components: image_mask_processor (`InpaintProcessor`) Inputs: mask_image (`Image`): Mask image for inpainting. resized_image (`Image`): The resized image. should be generated using a resize step padding_mask_crop (`int`, *optional*): Padding for mask cropping in inpainting. Outputs: processed_image (`Tensor`): The processed image processed_mask_image (`Tensor`): The processed mask image mask_overlay_kwargs (`dict`): The kwargs for the postprocess step to apply the mask overlay """ model_name = "qwenimage-edit" @property def description(self) -> str: return "Image Preprocess step for inpainting task. This processes the image and mask inputs together. Images should be resized first." @property def expected_components(self) -> list[ComponentSpec]: return [ ComponentSpec( "image_mask_processor", InpaintProcessor, config=FrozenDict({"vae_scale_factor": 16}), default_creation_method="from_config", ), ] @property def inputs(self) -> list[InputParam]: return [ InputParam.template("mask_image"), InputParam( name="resized_image", required=True, type_hint=PIL.Image.Image, description="The resized image. should be generated using a resize step", ), InputParam.template("padding_mask_crop"), ] @property def intermediate_outputs(self) -> list[OutputParam]: return [ OutputParam(name="processed_image", type_hint=torch.Tensor, description="The processed image"), OutputParam( name="processed_mask_image", type_hint=torch.Tensor, description="The processed mask image", ), OutputParam( name="mask_overlay_kwargs", type_hint=dict, description="The kwargs for the postprocess step to apply the mask overlay", ), ] @torch.no_grad() def __call__(self, components: QwenImageModularPipeline, state: PipelineState): block_state = self.get_block_state(state) width, height = block_state.resized_image[0].size block_state.processed_image, block_state.processed_mask_image, block_state.mask_overlay_kwargs = ( components.image_mask_processor.preprocess( image=block_state.resized_image, mask=block_state.mask_image, height=height, width=width, padding_mask_crop=block_state.padding_mask_crop, ) ) self.set_block_state(state, block_state) return components, state # auto_docstring class QwenImageProcessImagesInputStep(ModularPipelineBlocks): """ Image Preprocess step. will resize the image to the given height and width. Components: image_processor (`VaeImageProcessor`) Inputs: image (`Image | list`): Reference image(s) for denoising. Can be a single image or list of images. height (`int`, *optional*): The height in pixels of the generated image. width (`int`, *optional*): The width in pixels of the generated image. Outputs: processed_image (`Tensor`): The processed image """ model_name = "qwenimage" @property def description(self) -> str: return "Image Preprocess step. will resize the image to the given height and width." @property def expected_components(self) -> list[ComponentSpec]: return [ ComponentSpec( "image_processor", VaeImageProcessor, config=FrozenDict({"vae_scale_factor": 16}), default_creation_method="from_config", ), ] @property def inputs(self) -> list[InputParam]: return [ InputParam.template("image"), InputParam.template("height"), InputParam.template("width"), ] @property def intermediate_outputs(self) -> list[OutputParam]: return [ OutputParam( name="processed_image", type_hint=torch.Tensor, description="The processed image", ) ] @staticmethod def check_inputs(height, width, vae_scale_factor): if height is not None and height % (vae_scale_factor * 2) != 0: raise ValueError(f"Height must be divisible by {vae_scale_factor * 2} but is {height}") if width is not None and width % (vae_scale_factor * 2) != 0: raise ValueError(f"Width must be divisible by {vae_scale_factor * 2} but is {width}") @torch.no_grad() def __call__(self, components: QwenImageModularPipeline, state: PipelineState): block_state = self.get_block_state(state) self.check_inputs( height=block_state.height, width=block_state.width, vae_scale_factor=components.vae_scale_factor ) height = block_state.height or components.default_height width = block_state.width or components.default_width block_state.processed_image = components.image_processor.preprocess( image=block_state.image, height=height, width=width, ) self.set_block_state(state, block_state) return components, state # auto_docstring class QwenImageEditProcessImagesInputStep(ModularPipelineBlocks): """ Image Preprocess step. Images needs to be resized first. Components: image_processor (`VaeImageProcessor`) Inputs: resized_image (`list`): The resized image. should be generated using a resize step Outputs: processed_image (`Tensor`): The processed image """ model_name = "qwenimage-edit" @property def description(self) -> str: return "Image Preprocess step. Images needs to be resized first." @property def expected_components(self) -> list[ComponentSpec]: return [ ComponentSpec( "image_processor", VaeImageProcessor, config=FrozenDict({"vae_scale_factor": 16}), default_creation_method="from_config", ), ] @property def inputs(self) -> list[InputParam]: return [ InputParam( name="resized_image", required=True, type_hint=list[PIL.Image.Image], description="The resized image. should be generated using a resize step", ), ] @property def intermediate_outputs(self) -> list[OutputParam]: return [ OutputParam( name="processed_image", type_hint=torch.Tensor, description="The processed image", ) ] @torch.no_grad() def __call__(self, components: QwenImageModularPipeline, state: PipelineState): block_state = self.get_block_state(state) width, height = block_state.resized_image[0].size block_state.processed_image = components.image_processor.preprocess( image=block_state.resized_image, height=height, width=width, ) self.set_block_state(state, block_state) return components, state # auto_docstring class QwenImageEditPlusProcessImagesInputStep(ModularPipelineBlocks): """ Image Preprocess step. Images can be resized first. If a list of images is provided, will return a list of processed images. Components: image_processor (`VaeImageProcessor`) Inputs: resized_image (`list`): The resized image. should be generated using a resize step Outputs: processed_image (`Tensor`): The processed image """ model_name = "qwenimage-edit-plus" @property def description(self) -> str: return "Image Preprocess step. Images can be resized first. If a list of images is provided, will return a list of processed images." @property def expected_components(self) -> list[ComponentSpec]: return [ ComponentSpec( "image_processor", VaeImageProcessor, config=FrozenDict({"vae_scale_factor": 16}), default_creation_method="from_config", ), ] @property def inputs(self) -> list[InputParam]: return [ InputParam( name="resized_image", required=True, type_hint=list[PIL.Image.Image], description="The resized image. should be generated using a resize step", ) ] @property def intermediate_outputs(self) -> list[OutputParam]: return [ OutputParam( name="processed_image", type_hint=torch.Tensor, description="The processed image", ) ] @torch.no_grad() def __call__(self, components: QwenImageModularPipeline, state: PipelineState): block_state = self.get_block_state(state) image = block_state.resized_image is_image_list = isinstance(image, list) if not is_image_list: image = [image] processed_images = [] for img in image: img_width, img_height = img.size processed_images.append( components.image_processor.preprocess(image=img, height=img_height, width=img_width) ) if is_image_list: block_state.processed_image = processed_images else: block_state.processed_image = processed_images[0] self.set_block_state(state, block_state) return components, state # ==================== # 5. VAE ENCODER # ==================== # auto_docstring class QwenImageVaeEncoderStep(ModularPipelineBlocks): """ VAE Encoder step that converts processed_image into latent representations image_latents. Handles both single images and lists of images with varied resolutions. Components: vae (`AutoencoderKLQwenImage`) Inputs: processed_image (`Tensor`): The image tensor to encode generator (`Generator`, *optional*): Torch generator for deterministic generation. Outputs: image_latents (`Tensor`): The latent representation of the input image. """ model_name = "qwenimage" def __init__(self, input: InputParam | None = None, output: OutputParam | None = None): """Initialize a VAE encoder step for converting images to latent representations. Handles both single images and lists of images. When input is a list, outputs a list of latents. When input is a single tensor, outputs a single latent tensor. Args: input (InputParam, optional): Input parameter for the processed image. Defaults to "processed_image". output (OutputParam, optional): Output parameter for the image latents. Defaults to "image_latents". """ if input is None: input = InputParam( name="processed_image", required=True, type_hint=torch.Tensor, description="The image tensor to encode" ) if output is None: output = OutputParam.template("image_latents") if not isinstance(input, InputParam): raise ValueError(f"input must be InputParam but is {type(input)}") if not isinstance(output, OutputParam): raise ValueError(f"output must be OutputParam but is {type(output)}") self._input = input self._output = output self._image_input_name = input.name self._image_latents_output_name = output.name super().__init__() @property def description(self) -> str: return ( f"VAE Encoder step that converts {self._image_input_name} into latent representations {self._image_latents_output_name}.\n" "Handles both single images and lists of images with varied resolutions." ) @property def expected_components(self) -> list[ComponentSpec]: return [ComponentSpec("vae", AutoencoderKLQwenImage)] @property def inputs(self) -> list[InputParam]: return [ self._input, # default is "processed_image" InputParam.template("generator"), ] @property def intermediate_outputs(self) -> list[OutputParam]: return [self._output] # default is "image_latents" @torch.no_grad() def __call__(self, components: QwenImageModularPipeline, state: PipelineState) -> PipelineState: block_state = self.get_block_state(state) device = components._execution_device dtype = components.vae.dtype image = getattr(block_state, self._image_input_name) is_image_list = isinstance(image, list) if not is_image_list: image = [image] # Handle both single image and list of images image_latents = [] for img in image: image_latents.append( encode_vae_image( image=img, vae=components.vae, generator=block_state.generator, device=device, dtype=dtype, latent_channels=components.num_channels_latents, ) ) if not is_image_list: image_latents = image_latents[0] setattr(block_state, self._image_latents_output_name, image_latents) self.set_block_state(state, block_state) return components, state # auto_docstring class QwenImageControlNetVaeEncoderStep(ModularPipelineBlocks): """ VAE Encoder step that converts `control_image` into latent representations control_image_latents. Components: vae (`AutoencoderKLQwenImage`) controlnet (`QwenImageControlNetModel`) control_image_processor (`VaeImageProcessor`) Inputs: control_image (`Image`): Control image for ControlNet conditioning. height (`int`, *optional*): The height in pixels of the generated image. width (`int`, *optional*): The width in pixels of the generated image. generator (`Generator`, *optional*): Torch generator for deterministic generation. Outputs: control_image_latents (`Tensor`): The latents representing the control image """ model_name = "qwenimage" @property def description(self) -> str: return "VAE Encoder step that converts `control_image` into latent representations control_image_latents.\n" @property def expected_components(self) -> list[ComponentSpec]: components = [ ComponentSpec("vae", AutoencoderKLQwenImage), ComponentSpec("controlnet", QwenImageControlNetModel), ComponentSpec( "control_image_processor", VaeImageProcessor, config=FrozenDict({"vae_scale_factor": 16}), default_creation_method="from_config", ), ] return components @property def inputs(self) -> list[InputParam]: inputs = [ InputParam.template("control_image"), InputParam.template("height"), InputParam.template("width"), InputParam.template("generator"), ] return inputs @property def intermediate_outputs(self) -> list[OutputParam]: return [ OutputParam( "control_image_latents", type_hint=torch.Tensor, description="The latents representing the control image", ) ] @staticmethod def check_inputs(height, width, vae_scale_factor): if height is not None and height % (vae_scale_factor * 2) != 0: raise ValueError(f"Height must be divisible by {vae_scale_factor * 2} but is {height}") if width is not None and width % (vae_scale_factor * 2) != 0: raise ValueError(f"Width must be divisible by {vae_scale_factor * 2} but is {width}") @torch.no_grad() def __call__(self, components: QwenImageModularPipeline, state: PipelineState) -> PipelineState: block_state = self.get_block_state(state) self.check_inputs(block_state.height, block_state.width, components.vae_scale_factor) device = components._execution_device dtype = components.vae.dtype height = block_state.height or components.default_height width = block_state.width or components.default_width controlnet = unwrap_module(components.controlnet) if isinstance(controlnet, QwenImageMultiControlNetModel) and not isinstance(block_state.control_image, list): block_state.control_image = [block_state.control_image] if isinstance(controlnet, QwenImageMultiControlNetModel): block_state.control_image_latents = [] for control_image_ in block_state.control_image: control_image_ = components.control_image_processor.preprocess( image=control_image_, height=height, width=width, ) control_image_latents_ = encode_vae_image( image=control_image_, vae=components.vae, generator=block_state.generator, device=device, dtype=dtype, latent_channels=components.num_channels_latents, sample_mode="sample", ) block_state.control_image_latents.append(control_image_latents_) elif isinstance(controlnet, QwenImageControlNetModel): control_image = components.control_image_processor.preprocess( image=block_state.control_image, height=height, width=width, ) block_state.control_image_latents = encode_vae_image( image=control_image, vae=components.vae, generator=block_state.generator, device=device, dtype=dtype, latent_channels=components.num_channels_latents, sample_mode="sample", ) else: raise ValueError( f"Expected controlnet to be a QwenImageControlNetModel or QwenImageMultiControlNetModel, got {type(controlnet)}" ) self.set_block_state(state, block_state) return components, state # ==================== # 6. PERMUTE LATENTS # ==================== # auto_docstring class QwenImageLayeredPermuteLatentsStep(ModularPipelineBlocks): """ Permute image latents from (B, C, 1, H, W) to (B, 1, C, H, W) for Layered packing. Inputs: image_latents (`Tensor`): image latents used to guide the image generation. Can be generated from vae_encoder step. Outputs: image_latents (`Tensor`): The latent representation of the input image. (permuted from [B, C, 1, H, W] to [B, 1, C, H, W]) """ model_name = "qwenimage-layered" @property def description(self) -> str: return "Permute image latents from (B, C, 1, H, W) to (B, 1, C, H, W) for Layered packing." @property def inputs(self) -> list[InputParam]: return [ InputParam.template("image_latents"), ] @property def intermediate_outputs(self) -> list[OutputParam]: return [ OutputParam.template("image_latents", note="permuted from [B, C, 1, H, W] to [B, 1, C, H, W]"), ] @torch.no_grad() def __call__(self, components, state: PipelineState) -> PipelineState: block_state = self.get_block_state(state) # Permute: (B, C, 1, H, W) -> (B, 1, C, H, W) latents = block_state.image_latents block_state.image_latents = latents.permute(0, 2, 1, 3, 4) self.set_block_state(state, block_state) return components, state
{ "repo_id": "huggingface/diffusers", "file_path": "src/diffusers/modular_pipelines/qwenimage/encoders.py", "license": "Apache License 2.0", "lines": 1469, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
huggingface/diffusers:src/diffusers/modular_pipelines/qwenimage/inputs.py
# Copyright 2025 Qwen-Image Team and The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import torch from ...models import QwenImageMultiControlNetModel from ..modular_pipeline import ModularPipelineBlocks, PipelineState from ..modular_pipeline_utils import ComponentSpec, InputParam, OutputParam from .modular_pipeline import QwenImageLayeredPachifier, QwenImageModularPipeline, QwenImagePachifier def repeat_tensor_to_batch_size( input_name: str, input_tensor: torch.Tensor, batch_size: int, num_images_per_prompt: int = 1, ) -> torch.Tensor: """Repeat tensor elements to match the final batch size. This function expands a tensor's batch dimension to match the final batch size (batch_size * num_images_per_prompt) by repeating each element along dimension 0. The input tensor must have batch size 1 or batch_size. The function will: - If batch size is 1: repeat each element (batch_size * num_images_per_prompt) times - If batch size equals batch_size: repeat each element num_images_per_prompt times Args: input_name (str): Name of the input tensor (used for error messages) input_tensor (torch.Tensor): The tensor to repeat. Must have batch size 1 or batch_size. batch_size (int): The base batch size (number of prompts) num_images_per_prompt (int, optional): Number of images to generate per prompt. Defaults to 1. Returns: torch.Tensor: The repeated tensor with final batch size (batch_size * num_images_per_prompt) Raises: ValueError: If input_tensor is not a torch.Tensor or has invalid batch size Examples: tensor = torch.tensor([[1, 2, 3]]) # shape: [1, 3] repeated = repeat_tensor_to_batch_size("image", tensor, batch_size=2, num_images_per_prompt=2) repeated # tensor([[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]]) - shape: [4, 3] tensor = torch.tensor([[1, 2, 3], [4, 5, 6]]) # shape: [2, 3] repeated = repeat_tensor_to_batch_size("image", tensor, batch_size=2, num_images_per_prompt=2) repeated # tensor([[1, 2, 3], [1, 2, 3], [4, 5, 6], [4, 5, 6]]) - shape: [4, 3] """ # make sure input is a tensor if not isinstance(input_tensor, torch.Tensor): raise ValueError(f"`{input_name}` must be a tensor") # make sure input tensor e.g. image_latents has batch size 1 or batch_size same as prompts if input_tensor.shape[0] == 1: repeat_by = batch_size * num_images_per_prompt elif input_tensor.shape[0] == batch_size: repeat_by = num_images_per_prompt else: raise ValueError( f"`{input_name}` must have have batch size 1 or {batch_size}, but got {input_tensor.shape[0]}" ) # expand the tensor to match the batch_size * num_images_per_prompt input_tensor = input_tensor.repeat_interleave(repeat_by, dim=0) return input_tensor def calculate_dimension_from_latents(latents: torch.Tensor, vae_scale_factor: int) -> tuple[int, int]: """Calculate image dimensions from latent tensor dimensions. This function converts latent space dimensions to image space dimensions by multiplying the latent height and width by the VAE scale factor. Args: latents (torch.Tensor): The latent tensor. Must have 4 or 5 dimensions. Expected shapes: [batch, channels, height, width] or [batch, channels, frames, height, width] vae_scale_factor (int): The scale factor used by the VAE to compress images. Typically 8 for most VAEs (image is 8x larger than latents in each dimension) Returns: tuple[int, int]: The calculated image dimensions as (height, width) Raises: ValueError: If latents tensor doesn't have 4 or 5 dimensions """ # make sure the latents are not packed if latents.ndim != 4 and latents.ndim != 5: raise ValueError(f"unpacked latents must have 4 or 5 dimensions, but got {latents.ndim}") latent_height, latent_width = latents.shape[-2:] height = latent_height * vae_scale_factor width = latent_width * vae_scale_factor return height, width # auto_docstring class QwenImageTextInputsStep(ModularPipelineBlocks): """ Text input processing step that standardizes text embeddings for the pipeline. This step: 1. Determines `batch_size` and `dtype` based on `prompt_embeds` 2. Ensures all text embeddings have consistent batch sizes (batch_size * num_images_per_prompt) This block should be placed after all encoder steps to process the text embeddings before they are used in subsequent pipeline steps. Inputs: num_images_per_prompt (`int`, *optional*, defaults to 1): The number of images to generate per prompt. prompt_embeds (`Tensor`): text embeddings used to guide the image generation. Can be generated from text_encoder step. prompt_embeds_mask (`Tensor`): mask for the text embeddings. Can be generated from text_encoder step. negative_prompt_embeds (`Tensor`, *optional*): negative text embeddings used to guide the image generation. Can be generated from text_encoder step. negative_prompt_embeds_mask (`Tensor`, *optional*): mask for the negative text embeddings. Can be generated from text_encoder step. Outputs: batch_size (`int`): The batch size of the prompt embeddings dtype (`dtype`): The data type of the prompt embeddings prompt_embeds (`Tensor`): The prompt embeddings. (batch-expanded) prompt_embeds_mask (`Tensor`): The encoder attention mask. (batch-expanded) negative_prompt_embeds (`Tensor`): The negative prompt embeddings. (batch-expanded) negative_prompt_embeds_mask (`Tensor`): The negative prompt embeddings mask. (batch-expanded) """ model_name = "qwenimage" @property def description(self) -> str: summary_section = ( "Text input processing step that standardizes text embeddings for the pipeline.\n" "This step:\n" " 1. Determines `batch_size` and `dtype` based on `prompt_embeds`\n" " 2. Ensures all text embeddings have consistent batch sizes (batch_size * num_images_per_prompt)" ) # Placement guidance placement_section = "\n\nThis block should be placed after all encoder steps to process the text embeddings before they are used in subsequent pipeline steps." return summary_section + placement_section @property def inputs(self) -> list[InputParam]: return [ InputParam.template("num_images_per_prompt"), InputParam.template("prompt_embeds"), InputParam.template("prompt_embeds_mask"), InputParam.template("negative_prompt_embeds"), InputParam.template("negative_prompt_embeds_mask"), ] @property def intermediate_outputs(self) -> list[OutputParam]: return [ OutputParam(name="batch_size", type_hint=int, description="The batch size of the prompt embeddings"), OutputParam(name="dtype", type_hint=torch.dtype, description="The data type of the prompt embeddings"), OutputParam.template("prompt_embeds", note="batch-expanded"), OutputParam.template("prompt_embeds_mask", note="batch-expanded"), OutputParam.template("negative_prompt_embeds", note="batch-expanded"), OutputParam.template("negative_prompt_embeds_mask", note="batch-expanded"), ] @staticmethod def check_inputs( prompt_embeds, prompt_embeds_mask, negative_prompt_embeds, negative_prompt_embeds_mask, ): if negative_prompt_embeds is not None and negative_prompt_embeds_mask is None: raise ValueError("`negative_prompt_embeds_mask` is required when `negative_prompt_embeds` is not None") if negative_prompt_embeds is None and negative_prompt_embeds_mask is not None: raise ValueError("cannot pass `negative_prompt_embeds_mask` without `negative_prompt_embeds`") if prompt_embeds_mask.shape[0] != prompt_embeds.shape[0]: raise ValueError("`prompt_embeds_mask` must have the same batch size as `prompt_embeds`") elif negative_prompt_embeds is not None and negative_prompt_embeds.shape[0] != prompt_embeds.shape[0]: raise ValueError("`negative_prompt_embeds` must have the same batch size as `prompt_embeds`") elif ( negative_prompt_embeds_mask is not None and negative_prompt_embeds_mask.shape[0] != prompt_embeds.shape[0] ): raise ValueError("`negative_prompt_embeds_mask` must have the same batch size as `prompt_embeds`") def __call__(self, components: QwenImageModularPipeline, state: PipelineState) -> PipelineState: block_state = self.get_block_state(state) self.check_inputs( prompt_embeds=block_state.prompt_embeds, prompt_embeds_mask=block_state.prompt_embeds_mask, negative_prompt_embeds=block_state.negative_prompt_embeds, negative_prompt_embeds_mask=block_state.negative_prompt_embeds_mask, ) block_state.batch_size = block_state.prompt_embeds.shape[0] block_state.dtype = block_state.prompt_embeds.dtype _, seq_len, _ = block_state.prompt_embeds.shape block_state.prompt_embeds = block_state.prompt_embeds.repeat(1, block_state.num_images_per_prompt, 1) block_state.prompt_embeds = block_state.prompt_embeds.view( block_state.batch_size * block_state.num_images_per_prompt, seq_len, -1 ) block_state.prompt_embeds_mask = block_state.prompt_embeds_mask.repeat(1, block_state.num_images_per_prompt, 1) block_state.prompt_embeds_mask = block_state.prompt_embeds_mask.view( block_state.batch_size * block_state.num_images_per_prompt, seq_len ) if block_state.negative_prompt_embeds is not None: _, seq_len, _ = block_state.negative_prompt_embeds.shape block_state.negative_prompt_embeds = block_state.negative_prompt_embeds.repeat( 1, block_state.num_images_per_prompt, 1 ) block_state.negative_prompt_embeds = block_state.negative_prompt_embeds.view( block_state.batch_size * block_state.num_images_per_prompt, seq_len, -1 ) block_state.negative_prompt_embeds_mask = block_state.negative_prompt_embeds_mask.repeat( 1, block_state.num_images_per_prompt, 1 ) block_state.negative_prompt_embeds_mask = block_state.negative_prompt_embeds_mask.view( block_state.batch_size * block_state.num_images_per_prompt, seq_len ) self.set_block_state(state, block_state) return components, state # auto_docstring class QwenImageAdditionalInputsStep(ModularPipelineBlocks): """ Input processing step that: 1. For image latent inputs: Updates height/width if None, patchifies, and expands batch size 2. For additional batch inputs: Expands batch dimensions to match final batch size Configured inputs: - Image latent inputs: ['image_latents'] This block should be placed after the encoder steps and the text input step. Components: pachifier (`QwenImagePachifier`) Inputs: num_images_per_prompt (`int`, *optional*, defaults to 1): The number of images to generate per prompt. batch_size (`int`, *optional*, defaults to 1): Number of prompts, the final batch size of model inputs should be batch_size * num_images_per_prompt. Can be generated in input step. height (`int`, *optional*): The height in pixels of the generated image. width (`int`, *optional*): The width in pixels of the generated image. image_latents (`Tensor`): image latents used to guide the image generation. Can be generated from vae_encoder step. Outputs: image_height (`int`): The image height calculated from the image latents dimension image_width (`int`): The image width calculated from the image latents dimension height (`int`): if not provided, updated to image height width (`int`): if not provided, updated to image width image_latents (`Tensor`): image latents used to guide the image generation. Can be generated from vae_encoder step. (patchified and batch-expanded) """ model_name = "qwenimage" def __init__( self, image_latent_inputs: list[InputParam] | None = None, additional_batch_inputs: list[InputParam] | None = None, ): # by default, process `image_latents` if image_latent_inputs is None: image_latent_inputs = [InputParam.template("image_latents")] if additional_batch_inputs is None: additional_batch_inputs = [] if not isinstance(image_latent_inputs, list): raise ValueError(f"image_latent_inputs must be a list, but got {type(image_latent_inputs)}") else: for input_param in image_latent_inputs: if not isinstance(input_param, InputParam): raise ValueError(f"image_latent_inputs must be a list of InputParam, but got {type(input_param)}") if not isinstance(additional_batch_inputs, list): raise ValueError(f"additional_batch_inputs must be a list, but got {type(additional_batch_inputs)}") else: for input_param in additional_batch_inputs: if not isinstance(input_param, InputParam): raise ValueError( f"additional_batch_inputs must be a list of InputParam, but got {type(input_param)}" ) self._image_latent_inputs = image_latent_inputs self._additional_batch_inputs = additional_batch_inputs super().__init__() @property def description(self) -> str: summary_section = ( "Input processing step that:\n" " 1. For image latent inputs: Updates height/width if None, patchifies, and expands batch size\n" " 2. For additional batch inputs: Expands batch dimensions to match final batch size" ) inputs_info = "" if self._image_latent_inputs or self._additional_batch_inputs: inputs_info = "\n\nConfigured inputs:" if self._image_latent_inputs: inputs_info += f"\n - Image latent inputs: {[p.name for p in self._image_latent_inputs]}" if self._additional_batch_inputs: inputs_info += f"\n - Additional batch inputs: {[p.name for p in self._additional_batch_inputs]}" placement_section = "\n\nThis block should be placed after the encoder steps and the text input step." return summary_section + inputs_info + placement_section @property def expected_components(self) -> list[ComponentSpec]: return [ ComponentSpec("pachifier", QwenImagePachifier, default_creation_method="from_config"), ] @property def inputs(self) -> list[InputParam]: inputs = [ InputParam.template("num_images_per_prompt"), InputParam.template("batch_size"), InputParam.template("height"), InputParam.template("width"), ] # default is `image_latents` inputs += self._image_latent_inputs + self._additional_batch_inputs return inputs @property def intermediate_outputs(self) -> list[OutputParam]: outputs = [ OutputParam( name="image_height", type_hint=int, description="The image height calculated from the image latents dimension", ), OutputParam( name="image_width", type_hint=int, description="The image width calculated from the image latents dimension", ), ] # `height`/`width` are not new outputs, but they will be updated if any image latent inputs are provided if len(self._image_latent_inputs) > 0: outputs.append( OutputParam(name="height", type_hint=int, description="if not provided, updated to image height") ) outputs.append( OutputParam(name="width", type_hint=int, description="if not provided, updated to image width") ) # image latent inputs are modified in place (patchified and batch-expanded) for input_param in self._image_latent_inputs: outputs.append( OutputParam( name=input_param.name, type_hint=input_param.type_hint, description=input_param.description + " (patchified and batch-expanded)", ) ) # additional batch inputs (batch-expanded only) for input_param in self._additional_batch_inputs: outputs.append( OutputParam( name=input_param.name, type_hint=input_param.type_hint, description=input_param.description + " (batch-expanded)", ) ) return outputs def __call__(self, components: QwenImageModularPipeline, state: PipelineState) -> PipelineState: block_state = self.get_block_state(state) # Process image latent inputs for input_param in self._image_latent_inputs: image_latent_input_name = input_param.name image_latent_tensor = getattr(block_state, image_latent_input_name) if image_latent_tensor is None: continue # 1. Calculate height/width from latents and update if not provided height, width = calculate_dimension_from_latents(image_latent_tensor, components.vae_scale_factor) block_state.height = block_state.height or height block_state.width = block_state.width or width if not hasattr(block_state, "image_height"): block_state.image_height = height if not hasattr(block_state, "image_width"): block_state.image_width = width # 2. Patchify image_latent_tensor = components.pachifier.pack_latents(image_latent_tensor) # 3. Expand batch size image_latent_tensor = repeat_tensor_to_batch_size( input_name=image_latent_input_name, input_tensor=image_latent_tensor, num_images_per_prompt=block_state.num_images_per_prompt, batch_size=block_state.batch_size, ) setattr(block_state, image_latent_input_name, image_latent_tensor) # Process additional batch inputs (only batch expansion) for input_param in self._additional_batch_inputs: input_name = input_param.name input_tensor = getattr(block_state, input_name) if input_tensor is None: continue input_tensor = repeat_tensor_to_batch_size( input_name=input_name, input_tensor=input_tensor, num_images_per_prompt=block_state.num_images_per_prompt, batch_size=block_state.batch_size, ) setattr(block_state, input_name, input_tensor) self.set_block_state(state, block_state) return components, state # auto_docstring class QwenImageEditPlusAdditionalInputsStep(ModularPipelineBlocks): """ Input processing step for Edit Plus that: 1. For image latent inputs (list): Collects heights/widths, patchifies each, concatenates, expands batch 2. For additional batch inputs: Expands batch dimensions to match final batch size Height/width defaults to last image in the list. Configured inputs: - Image latent inputs: ['image_latents'] This block should be placed after the encoder steps and the text input step. Components: pachifier (`QwenImagePachifier`) Inputs: num_images_per_prompt (`int`, *optional*, defaults to 1): The number of images to generate per prompt. batch_size (`int`, *optional*, defaults to 1): Number of prompts, the final batch size of model inputs should be batch_size * num_images_per_prompt. Can be generated in input step. height (`int`, *optional*): The height in pixels of the generated image. width (`int`, *optional*): The width in pixels of the generated image. image_latents (`Tensor`): image latents used to guide the image generation. Can be generated from vae_encoder step. Outputs: image_height (`list`): The image heights calculated from the image latents dimension image_width (`list`): The image widths calculated from the image latents dimension height (`int`): if not provided, updated to image height width (`int`): if not provided, updated to image width image_latents (`Tensor`): image latents used to guide the image generation. Can be generated from vae_encoder step. (patchified, concatenated, and batch-expanded) """ model_name = "qwenimage-edit-plus" def __init__( self, image_latent_inputs: list[InputParam] | None = None, additional_batch_inputs: list[InputParam] | None = None, ): if image_latent_inputs is None: image_latent_inputs = [InputParam.template("image_latents")] if additional_batch_inputs is None: additional_batch_inputs = [] if not isinstance(image_latent_inputs, list): raise ValueError(f"image_latent_inputs must be a list, but got {type(image_latent_inputs)}") else: for input_param in image_latent_inputs: if not isinstance(input_param, InputParam): raise ValueError(f"image_latent_inputs must be a list of InputParam, but got {type(input_param)}") if not isinstance(additional_batch_inputs, list): raise ValueError(f"additional_batch_inputs must be a list, but got {type(additional_batch_inputs)}") else: for input_param in additional_batch_inputs: if not isinstance(input_param, InputParam): raise ValueError( f"additional_batch_inputs must be a list of InputParam, but got {type(input_param)}" ) self._image_latent_inputs = image_latent_inputs self._additional_batch_inputs = additional_batch_inputs super().__init__() @property def description(self) -> str: summary_section = ( "Input processing step for Edit Plus that:\n" " 1. For image latent inputs (list): Collects heights/widths, patchifies each, concatenates, expands batch\n" " 2. For additional batch inputs: Expands batch dimensions to match final batch size\n" " Height/width defaults to last image in the list." ) inputs_info = "" if self._image_latent_inputs or self._additional_batch_inputs: inputs_info = "\n\nConfigured inputs:" if self._image_latent_inputs: inputs_info += f"\n - Image latent inputs: {[p.name for p in self._image_latent_inputs]}" if self._additional_batch_inputs: inputs_info += f"\n - Additional batch inputs: {[p.name for p in self._additional_batch_inputs]}" placement_section = "\n\nThis block should be placed after the encoder steps and the text input step." return summary_section + inputs_info + placement_section @property def expected_components(self) -> list[ComponentSpec]: return [ ComponentSpec("pachifier", QwenImagePachifier, default_creation_method="from_config"), ] @property def inputs(self) -> list[InputParam]: inputs = [ InputParam.template("num_images_per_prompt"), InputParam.template("batch_size"), InputParam.template("height"), InputParam.template("width"), ] # default is `image_latents` inputs += self._image_latent_inputs + self._additional_batch_inputs return inputs @property def intermediate_outputs(self) -> list[OutputParam]: outputs = [ OutputParam( name="image_height", type_hint=list[int], description="The image heights calculated from the image latents dimension", ), OutputParam( name="image_width", type_hint=list[int], description="The image widths calculated from the image latents dimension", ), ] # `height`/`width` are updated if any image latent inputs are provided if len(self._image_latent_inputs) > 0: outputs.append( OutputParam(name="height", type_hint=int, description="if not provided, updated to image height") ) outputs.append( OutputParam(name="width", type_hint=int, description="if not provided, updated to image width") ) # image latent inputs are modified in place (patchified, concatenated, and batch-expanded) for input_param in self._image_latent_inputs: outputs.append( OutputParam( name=input_param.name, type_hint=input_param.type_hint, description=input_param.description + " (patchified, concatenated, and batch-expanded)", ) ) # additional batch inputs (batch-expanded only) for input_param in self._additional_batch_inputs: outputs.append( OutputParam( name=input_param.name, type_hint=input_param.type_hint, description=input_param.description + " (batch-expanded)", ) ) return outputs def __call__(self, components: QwenImageModularPipeline, state: PipelineState) -> PipelineState: block_state = self.get_block_state(state) # Process image latent inputs for input_param in self._image_latent_inputs: image_latent_input_name = input_param.name image_latent_tensor = getattr(block_state, image_latent_input_name) if image_latent_tensor is None: continue is_list = isinstance(image_latent_tensor, list) if not is_list: image_latent_tensor = [image_latent_tensor] image_heights = [] image_widths = [] packed_image_latent_tensors = [] for i, img_latent_tensor in enumerate(image_latent_tensor): # 1. Calculate height/width from latents height, width = calculate_dimension_from_latents(img_latent_tensor, components.vae_scale_factor) image_heights.append(height) image_widths.append(width) # 2. Patchify img_latent_tensor = components.pachifier.pack_latents(img_latent_tensor) # 3. Expand batch size img_latent_tensor = repeat_tensor_to_batch_size( input_name=f"{image_latent_input_name}[{i}]", input_tensor=img_latent_tensor, num_images_per_prompt=block_state.num_images_per_prompt, batch_size=block_state.batch_size, ) packed_image_latent_tensors.append(img_latent_tensor) # Concatenate all packed latents along dim=1 packed_image_latent_tensors = torch.cat(packed_image_latent_tensors, dim=1) # Output lists of heights/widths block_state.image_height = image_heights block_state.image_width = image_widths # Default height/width from last image block_state.height = block_state.height or image_heights[-1] block_state.width = block_state.width or image_widths[-1] setattr(block_state, image_latent_input_name, packed_image_latent_tensors) # Process additional batch inputs (only batch expansion) for input_param in self._additional_batch_inputs: input_name = input_param.name input_tensor = getattr(block_state, input_name) if input_tensor is None: continue input_tensor = repeat_tensor_to_batch_size( input_name=input_name, input_tensor=input_tensor, num_images_per_prompt=block_state.num_images_per_prompt, batch_size=block_state.batch_size, ) setattr(block_state, input_name, input_tensor) self.set_block_state(state, block_state) return components, state # same as QwenImageAdditionalInputsStep, but with layered pachifier. # auto_docstring class QwenImageLayeredAdditionalInputsStep(ModularPipelineBlocks): """ Input processing step for Layered that: 1. For image latent inputs: Updates height/width if None, patchifies with layered pachifier, and expands batch size 2. For additional batch inputs: Expands batch dimensions to match final batch size Configured inputs: - Image latent inputs: ['image_latents'] This block should be placed after the encoder steps and the text input step. Components: pachifier (`QwenImageLayeredPachifier`) Inputs: num_images_per_prompt (`int`, *optional*, defaults to 1): The number of images to generate per prompt. batch_size (`int`, *optional*, defaults to 1): Number of prompts, the final batch size of model inputs should be batch_size * num_images_per_prompt. Can be generated in input step. image_latents (`Tensor`): image latents used to guide the image generation. Can be generated from vae_encoder step. Outputs: image_height (`int`): The image height calculated from the image latents dimension image_width (`int`): The image width calculated from the image latents dimension height (`int`): if not provided, updated to image height width (`int`): if not provided, updated to image width image_latents (`Tensor`): image latents used to guide the image generation. Can be generated from vae_encoder step. (patchified with layered pachifier and batch-expanded) """ model_name = "qwenimage-layered" def __init__( self, image_latent_inputs: list[InputParam] | None = None, additional_batch_inputs: list[InputParam] | None = None, ): if image_latent_inputs is None: image_latent_inputs = [InputParam.template("image_latents")] if additional_batch_inputs is None: additional_batch_inputs = [] if not isinstance(image_latent_inputs, list): raise ValueError(f"image_latent_inputs must be a list, but got {type(image_latent_inputs)}") else: for input_param in image_latent_inputs: if not isinstance(input_param, InputParam): raise ValueError(f"image_latent_inputs must be a list of InputParam, but got {type(input_param)}") if not isinstance(additional_batch_inputs, list): raise ValueError(f"additional_batch_inputs must be a list, but got {type(additional_batch_inputs)}") else: for input_param in additional_batch_inputs: if not isinstance(input_param, InputParam): raise ValueError( f"additional_batch_inputs must be a list of InputParam, but got {type(input_param)}" ) self._image_latent_inputs = image_latent_inputs self._additional_batch_inputs = additional_batch_inputs super().__init__() @property def description(self) -> str: summary_section = ( "Input processing step for Layered that:\n" " 1. For image latent inputs: Updates height/width if None, patchifies with layered pachifier, and expands batch size\n" " 2. For additional batch inputs: Expands batch dimensions to match final batch size" ) inputs_info = "" if self._image_latent_inputs or self._additional_batch_inputs: inputs_info = "\n\nConfigured inputs:" if self._image_latent_inputs: inputs_info += f"\n - Image latent inputs: {[p.name for p in self._image_latent_inputs]}" if self._additional_batch_inputs: inputs_info += f"\n - Additional batch inputs: {[p.name for p in self._additional_batch_inputs]}" placement_section = "\n\nThis block should be placed after the encoder steps and the text input step." return summary_section + inputs_info + placement_section @property def expected_components(self) -> list[ComponentSpec]: return [ ComponentSpec("pachifier", QwenImageLayeredPachifier, default_creation_method="from_config"), ] @property def inputs(self) -> list[InputParam]: inputs = [ InputParam.template("num_images_per_prompt"), InputParam.template("batch_size"), ] # default is `image_latents` inputs += self._image_latent_inputs + self._additional_batch_inputs return inputs @property def intermediate_outputs(self) -> list[OutputParam]: outputs = [ OutputParam( name="image_height", type_hint=int, description="The image height calculated from the image latents dimension", ), OutputParam( name="image_width", type_hint=int, description="The image width calculated from the image latents dimension", ), ] if len(self._image_latent_inputs) > 0: outputs.append( OutputParam(name="height", type_hint=int, description="if not provided, updated to image height") ) outputs.append( OutputParam(name="width", type_hint=int, description="if not provided, updated to image width") ) # Add outputs for image latent inputs (patchified with layered pachifier and batch-expanded) for input_param in self._image_latent_inputs: outputs.append( OutputParam( name=input_param.name, type_hint=input_param.type_hint, description=input_param.description + " (patchified with layered pachifier and batch-expanded)", ) ) # Add outputs for additional batch inputs (batch-expanded only) for input_param in self._additional_batch_inputs: outputs.append( OutputParam( name=input_param.name, type_hint=input_param.type_hint, description=input_param.description + " (batch-expanded)", ) ) return outputs def __call__(self, components: QwenImageModularPipeline, state: PipelineState) -> PipelineState: block_state = self.get_block_state(state) # Process image latent inputs for input_param in self._image_latent_inputs: image_latent_input_name = input_param.name image_latent_tensor = getattr(block_state, image_latent_input_name) if image_latent_tensor is None: continue # 1. Calculate height/width from latents and update if not provided # Layered latents are (B, layers, C, H, W) height = image_latent_tensor.shape[3] * components.vae_scale_factor width = image_latent_tensor.shape[4] * components.vae_scale_factor block_state.height = height block_state.width = width if not hasattr(block_state, "image_height"): block_state.image_height = height if not hasattr(block_state, "image_width"): block_state.image_width = width # 2. Patchify with layered pachifier image_latent_tensor = components.pachifier.pack_latents(image_latent_tensor) # 3. Expand batch size image_latent_tensor = repeat_tensor_to_batch_size( input_name=image_latent_input_name, input_tensor=image_latent_tensor, num_images_per_prompt=block_state.num_images_per_prompt, batch_size=block_state.batch_size, ) setattr(block_state, image_latent_input_name, image_latent_tensor) # Process additional batch inputs (only batch expansion) for input_param in self._additional_batch_inputs: input_name = input_param.name input_tensor = getattr(block_state, input_name) if input_tensor is None: continue input_tensor = repeat_tensor_to_batch_size( input_name=input_name, input_tensor=input_tensor, num_images_per_prompt=block_state.num_images_per_prompt, batch_size=block_state.batch_size, ) setattr(block_state, input_name, input_tensor) self.set_block_state(state, block_state) return components, state # auto_docstring class QwenImageControlNetInputsStep(ModularPipelineBlocks): """ prepare the `control_image_latents` for controlnet. Insert after all the other inputs steps. Inputs: control_image_latents (`Tensor`): The control image latents to use for the denoising process. Can be generated in controlnet vae encoder step. batch_size (`int`, *optional*, defaults to 1): Number of prompts, the final batch size of model inputs should be batch_size * num_images_per_prompt. Can be generated in input step. num_images_per_prompt (`int`, *optional*, defaults to 1): The number of images to generate per prompt. height (`int`, *optional*): The height in pixels of the generated image. width (`int`, *optional*): The width in pixels of the generated image. Outputs: control_image_latents (`Tensor`): The control image latents (patchified and batch-expanded). height (`int`): if not provided, updated to control image height width (`int`): if not provided, updated to control image width """ model_name = "qwenimage" @property def description(self) -> str: return "prepare the `control_image_latents` for controlnet. Insert after all the other inputs steps." @property def inputs(self) -> list[InputParam]: return [ InputParam( name="control_image_latents", required=True, type_hint=torch.Tensor, description="The control image latents to use for the denoising process. Can be generated in controlnet vae encoder step.", ), InputParam.template("batch_size"), InputParam.template("num_images_per_prompt"), InputParam.template("height"), InputParam.template("width"), ] @property def intermediate_outputs(self) -> list[OutputParam]: return [ OutputParam( name="control_image_latents", type_hint=torch.Tensor, description="The control image latents (patchified and batch-expanded).", ), OutputParam(name="height", type_hint=int, description="if not provided, updated to control image height"), OutputParam(name="width", type_hint=int, description="if not provided, updated to control image width"), ] @torch.no_grad() def __call__(self, components: QwenImageModularPipeline, state: PipelineState) -> PipelineState: block_state = self.get_block_state(state) if isinstance(components.controlnet, QwenImageMultiControlNetModel): control_image_latents = [] # loop through each control_image_latents for i, control_image_latents_ in enumerate(block_state.control_image_latents): # 1. update height/width if not provided height, width = calculate_dimension_from_latents(control_image_latents_, components.vae_scale_factor) block_state.height = block_state.height or height block_state.width = block_state.width or width # 2. pack control_image_latents_ = components.pachifier.pack_latents(control_image_latents_) # 3. repeat to match the batch size control_image_latents_ = repeat_tensor_to_batch_size( input_name=f"control_image_latents[{i}]", input_tensor=control_image_latents_, num_images_per_prompt=block_state.num_images_per_prompt, batch_size=block_state.batch_size, ) control_image_latents.append(control_image_latents_) block_state.control_image_latents = control_image_latents else: # 1. update height/width if not provided height, width = calculate_dimension_from_latents( block_state.control_image_latents, components.vae_scale_factor ) block_state.height = block_state.height or height block_state.width = block_state.width or width # 2. pack block_state.control_image_latents = components.pachifier.pack_latents(block_state.control_image_latents) # 3. repeat to match the batch size block_state.control_image_latents = repeat_tensor_to_batch_size( input_name="control_image_latents", input_tensor=block_state.control_image_latents, num_images_per_prompt=block_state.num_images_per_prompt, batch_size=block_state.batch_size, ) block_state.control_image_latents = block_state.control_image_latents self.set_block_state(state, block_state) return components, state
{ "repo_id": "huggingface/diffusers", "file_path": "src/diffusers/modular_pipelines/qwenimage/inputs.py", "license": "Apache License 2.0", "lines": 840, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
huggingface/diffusers:src/diffusers/modular_pipelines/qwenimage/modular_pipeline.py
# Copyright 2025 Qwen-Image Team and The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from ...configuration_utils import ConfigMixin, register_to_config from ...loaders import QwenImageLoraLoaderMixin from ..modular_pipeline import ModularPipeline class QwenImagePachifier(ConfigMixin): """ A class to pack and unpack latents for QwenImage. """ config_name = "config.json" @register_to_config def __init__(self, patch_size: int = 2): super().__init__() def pack_latents(self, latents): if latents.ndim != 4 and latents.ndim != 5: raise ValueError(f"Latents must have 4 or 5 dimensions, but got {latents.ndim}") if latents.ndim == 4: latents = latents.unsqueeze(2) batch_size, num_channels_latents, num_latent_frames, latent_height, latent_width = latents.shape patch_size = self.config.patch_size if latent_height % patch_size != 0 or latent_width % patch_size != 0: raise ValueError( f"Latent height and width must be divisible by {patch_size}, but got {latent_height} and {latent_width}" ) latents = latents.view( batch_size, num_channels_latents, latent_height // patch_size, patch_size, latent_width // patch_size, patch_size, ) latents = latents.permute( 0, 2, 4, 1, 3, 5 ) # Batch_size, num_patches_height, num_patches_width, num_channels_latents, patch_size, patch_size latents = latents.reshape( batch_size, (latent_height // patch_size) * (latent_width // patch_size), num_channels_latents * patch_size * patch_size, ) return latents def unpack_latents(self, latents, height, width, vae_scale_factor=8): if latents.ndim != 3: raise ValueError(f"Latents must have 3 dimensions, but got {latents.ndim}") batch_size, num_patches, channels = latents.shape patch_size = self.config.patch_size # VAE applies 8x compression on images but we must also account for packing which requires # latent height and width to be divisible by 2. height = patch_size * (int(height) // (vae_scale_factor * patch_size)) width = patch_size * (int(width) // (vae_scale_factor * patch_size)) latents = latents.view( batch_size, height // patch_size, width // patch_size, channels // (patch_size * patch_size), patch_size, patch_size, ) latents = latents.permute(0, 3, 1, 4, 2, 5) latents = latents.reshape(batch_size, channels // (patch_size * patch_size), 1, height, width) return latents class QwenImageLayeredPachifier(ConfigMixin): """ A class to pack and unpack latents for QwenImage Layered. Unlike QwenImagePachifier, this handles 5D latents with shape (B, layers+1, C, H, W). """ config_name = "config.json" @register_to_config def __init__(self, patch_size: int = 2): super().__init__() def pack_latents(self, latents): """ Pack latents from (B, layers, C, H, W) to (B, layers * H/2 * W/2, C*4). """ if latents.ndim != 5: raise ValueError(f"Latents must have 5 dimensions (B, layers, C, H, W), but got {latents.ndim}") batch_size, layers, num_channels_latents, latent_height, latent_width = latents.shape patch_size = self.config.patch_size if latent_height % patch_size != 0 or latent_width % patch_size != 0: raise ValueError( f"Latent height and width must be divisible by {patch_size}, but got {latent_height} and {latent_width}" ) latents = latents.view( batch_size, layers, num_channels_latents, latent_height // patch_size, patch_size, latent_width // patch_size, patch_size, ) latents = latents.permute(0, 1, 3, 5, 2, 4, 6) latents = latents.reshape( batch_size, layers * (latent_height // patch_size) * (latent_width // patch_size), num_channels_latents * patch_size * patch_size, ) return latents def unpack_latents(self, latents, height, width, layers, vae_scale_factor=8): """ Unpack latents from (B, seq, C*4) to (B, C, layers+1, H, W). """ if latents.ndim != 3: raise ValueError(f"Latents must have 3 dimensions, but got {latents.ndim}") batch_size, _, channels = latents.shape patch_size = self.config.patch_size height = patch_size * (int(height) // (vae_scale_factor * patch_size)) width = patch_size * (int(width) // (vae_scale_factor * patch_size)) latents = latents.view( batch_size, layers + 1, height // patch_size, width // patch_size, channels // (patch_size * patch_size), patch_size, patch_size, ) latents = latents.permute(0, 1, 4, 2, 5, 3, 6) latents = latents.reshape( batch_size, layers + 1, channels // (patch_size * patch_size), height, width, ) latents = latents.permute(0, 2, 1, 3, 4) # (b, c, f, h, w) return latents class QwenImageModularPipeline(ModularPipeline, QwenImageLoraLoaderMixin): """ A ModularPipeline for QwenImage. > [!WARNING] > This is an experimental feature and is likely to change in the future. """ default_blocks_name = "QwenImageAutoBlocks" @property def default_height(self): return self.default_sample_size * self.vae_scale_factor @property def default_width(self): return self.default_sample_size * self.vae_scale_factor @property def default_sample_size(self): return 128 @property def vae_scale_factor(self): vae_scale_factor = 8 if hasattr(self, "vae") and self.vae is not None: vae_scale_factor = 2 ** len(self.vae.temperal_downsample) return vae_scale_factor @property def num_channels_latents(self): num_channels_latents = 16 if hasattr(self, "transformer") and self.transformer is not None: num_channels_latents = self.transformer.config.in_channels // 4 return num_channels_latents @property def is_guidance_distilled(self): is_guidance_distilled = False if hasattr(self, "transformer") and self.transformer is not None: is_guidance_distilled = self.transformer.config.guidance_embeds return is_guidance_distilled @property def requires_unconditional_embeds(self): requires_unconditional_embeds = False if hasattr(self, "guider") and self.guider is not None: requires_unconditional_embeds = self.guider._enabled and self.guider.num_conditions > 1 return requires_unconditional_embeds class QwenImageEditModularPipeline(ModularPipeline, QwenImageLoraLoaderMixin): """ A ModularPipeline for QwenImage-Edit. > [!WARNING] > This is an experimental feature and is likely to change in the future. """ default_blocks_name = "QwenImageEditAutoBlocks" # YiYi TODO: qwen edit should not provide default height/width, should be derived from the resized input image (after adjustment) produced by the resize step. @property def default_height(self): return self.default_sample_size * self.vae_scale_factor @property def default_width(self): return self.default_sample_size * self.vae_scale_factor @property def default_sample_size(self): return 128 @property def vae_scale_factor(self): vae_scale_factor = 8 if hasattr(self, "vae") and self.vae is not None: vae_scale_factor = 2 ** len(self.vae.temperal_downsample) return vae_scale_factor @property def num_channels_latents(self): num_channels_latents = 16 if hasattr(self, "transformer") and self.transformer is not None: num_channels_latents = self.transformer.config.in_channels // 4 return num_channels_latents @property def is_guidance_distilled(self): is_guidance_distilled = False if hasattr(self, "transformer") and self.transformer is not None: is_guidance_distilled = self.transformer.config.guidance_embeds return is_guidance_distilled @property def requires_unconditional_embeds(self): requires_unconditional_embeds = False if hasattr(self, "guider") and self.guider is not None: requires_unconditional_embeds = self.guider._enabled and self.guider.num_conditions > 1 return requires_unconditional_embeds class QwenImageEditPlusModularPipeline(QwenImageEditModularPipeline): """ A ModularPipeline for QwenImage-Edit Plus. > [!WARNING] > This is an experimental feature and is likely to change in the future. """ default_blocks_name = "QwenImageEditPlusAutoBlocks" class QwenImageLayeredModularPipeline(QwenImageModularPipeline): """ A ModularPipeline for QwenImage-Layered. > [!WARNING] > This is an experimental feature and is likely to change in the future. """ default_blocks_name = "QwenImageLayeredAutoBlocks"
{ "repo_id": "huggingface/diffusers", "file_path": "src/diffusers/modular_pipelines/qwenimage/modular_pipeline.py", "license": "Apache License 2.0", "lines": 229, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
huggingface/diffusers:src/diffusers/quantizers/modelopt/modelopt_quantizer.py
from typing import TYPE_CHECKING, Any from ...utils import ( get_module_from_name, is_accelerate_available, is_nvidia_modelopt_available, is_torch_available, logging, ) from ..base import DiffusersQuantizer if TYPE_CHECKING: from ...models.modeling_utils import ModelMixin if is_torch_available(): import torch import torch.nn as nn if is_accelerate_available(): from accelerate.utils import set_module_tensor_to_device logger = logging.get_logger(__name__) class NVIDIAModelOptQuantizer(DiffusersQuantizer): r""" Diffusers Quantizer for Nvidia-Model Optimizer """ use_keep_in_fp32_modules = True requires_calibration = False required_packages = ["nvidia_modelopt"] def __init__(self, quantization_config, **kwargs): super().__init__(quantization_config, **kwargs) def validate_environment(self, *args, **kwargs): if not is_nvidia_modelopt_available(): raise ImportError( "Loading an nvidia-modelopt quantized model requires nvidia-modelopt library (`pip install nvidia-modelopt`)" ) self.offload = False device_map = kwargs.get("device_map", None) if isinstance(device_map, dict): if "cpu" in device_map.values() or "disk" in device_map.values(): if self.pre_quantized: raise ValueError( "You are attempting to perform cpu/disk offload with a pre-quantized modelopt model " "This is not supported yet. Please remove the CPU or disk device from the `device_map` argument." ) else: self.offload = True def check_if_quantized_param( self, model: "ModelMixin", param_value: "torch.Tensor", param_name: str, state_dict: dict[str, Any], **kwargs, ): # ModelOpt imports diffusers internally. This is here to prevent circular imports from modelopt.torch.quantization.utils import is_quantized module, tensor_name = get_module_from_name(model, param_name) if self.pre_quantized: return True elif is_quantized(module) and "weight" in tensor_name: return True return False def create_quantized_param( self, model: "ModelMixin", param_value: "torch.Tensor", param_name: str, target_device: "torch.device", *args, **kwargs, ): """ Create the quantized parameter by calling .calibrate() after setting it to the module. """ # ModelOpt imports diffusers internally. This is here to prevent circular imports import modelopt.torch.quantization as mtq dtype = kwargs.get("dtype", torch.float32) module, tensor_name = get_module_from_name(model, param_name) if self.pre_quantized: module._parameters[tensor_name] = torch.nn.Parameter(param_value.to(device=target_device)) else: set_module_tensor_to_device(model, param_name, target_device, param_value, dtype) mtq.calibrate( module, self.quantization_config.modelopt_config["algorithm"], self.quantization_config.forward_loop ) mtq.compress(module) module.weight.requires_grad = False def adjust_max_memory(self, max_memory: dict[str, int | str]) -> dict[str, int | str]: max_memory = {key: val * 0.90 for key, val in max_memory.items()} return max_memory def adjust_target_dtype(self, target_dtype: "torch.dtype") -> "torch.dtype": if self.quantization_config.quant_type == "FP8": target_dtype = torch.float8_e4m3fn return target_dtype def update_torch_dtype(self, torch_dtype: "torch.dtype" = None) -> "torch.dtype": if torch_dtype is None: logger.info("You did not specify `torch_dtype` in `from_pretrained`. Setting it to `torch.float32`.") torch_dtype = torch.float32 return torch_dtype def get_conv_param_names(self, model: "ModelMixin") -> list[str]: """ Get parameter names for all convolutional layers in a HuggingFace ModelMixin. Includes Conv1d/2d/3d and ConvTranspose1d/2d/3d. """ conv_types = ( nn.Conv1d, nn.Conv2d, nn.Conv3d, nn.ConvTranspose1d, nn.ConvTranspose2d, nn.ConvTranspose3d, ) conv_param_names = [] for name, module in model.named_modules(): if isinstance(module, conv_types): for param_name, _ in module.named_parameters(recurse=False): conv_param_names.append(f"{name}.{param_name}") return conv_param_names def _process_model_before_weight_loading( self, model: "ModelMixin", device_map, keep_in_fp32_modules: list[str] = [], **kwargs, ): # ModelOpt imports diffusers internally. This is here to prevent circular imports import modelopt.torch.opt as mto if self.pre_quantized: return modules_to_not_convert = self.quantization_config.modules_to_not_convert if modules_to_not_convert is None: modules_to_not_convert = [] if isinstance(modules_to_not_convert, str): modules_to_not_convert = [modules_to_not_convert] modules_to_not_convert.extend(keep_in_fp32_modules) if self.quantization_config.disable_conv_quantization: modules_to_not_convert.extend(self.get_conv_param_names(model)) for module in modules_to_not_convert: self.quantization_config.modelopt_config["quant_cfg"]["*" + module + "*"] = {"enable": False} self.quantization_config.modules_to_not_convert = modules_to_not_convert mto.apply_mode(model, mode=[("quantize", self.quantization_config.modelopt_config)]) model.config.quantization_config = self.quantization_config def _process_model_after_weight_loading(self, model, **kwargs): # ModelOpt imports diffusers internally. This is here to prevent circular imports from modelopt.torch.opt import ModeloptStateManager if self.pre_quantized: return model for _, m in model.named_modules(): if hasattr(m, ModeloptStateManager._state_key) and m is not model: ModeloptStateManager.remove_state(m) return model @property def is_trainable(self): return True @property def is_serializable(self): self.quantization_config.check_model_patching(operation="saving") return True
{ "repo_id": "huggingface/diffusers", "file_path": "src/diffusers/quantizers/modelopt/modelopt_quantizer.py", "license": "Apache License 2.0", "lines": 154, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
function_complex
huggingface/diffusers:tests/quantization/modelopt/test_modelopt.py
import gc import tempfile import unittest from diffusers import NVIDIAModelOptConfig, SD3Transformer2DModel, StableDiffusion3Pipeline from diffusers.utils import is_nvidia_modelopt_available, is_torch_available from diffusers.utils.testing_utils import ( backend_empty_cache, backend_reset_peak_memory_stats, enable_full_determinism, nightly, numpy_cosine_similarity_distance, require_accelerate, require_big_accelerator, require_modelopt_version_greater_or_equal, require_torch_cuda_compatibility, torch_device, ) if is_nvidia_modelopt_available(): import modelopt.torch.quantization as mtq if is_torch_available(): import torch from ..utils import LoRALayer, get_memory_consumption_stat enable_full_determinism() @nightly @require_big_accelerator @require_accelerate @require_modelopt_version_greater_or_equal("0.33.1") class ModelOptBaseTesterMixin: model_id = "hf-internal-testing/tiny-sd3-pipe" model_cls = SD3Transformer2DModel pipeline_cls = StableDiffusion3Pipeline torch_dtype = torch.bfloat16 expected_memory_reduction = 0.0 keep_in_fp32_module = "" modules_to_not_convert = "" _test_torch_compile = False def setUp(self): backend_reset_peak_memory_stats(torch_device) backend_empty_cache(torch_device) gc.collect() def tearDown(self): backend_reset_peak_memory_stats(torch_device) backend_empty_cache(torch_device) gc.collect() def get_dummy_init_kwargs(self): return {"quant_type": "FP8"} def get_dummy_model_init_kwargs(self): return { "pretrained_model_name_or_path": self.model_id, "torch_dtype": self.torch_dtype, "quantization_config": NVIDIAModelOptConfig(**self.get_dummy_init_kwargs()), "subfolder": "transformer", } def test_modelopt_layers(self): model = self.model_cls.from_pretrained(**self.get_dummy_model_init_kwargs()) for name, module in model.named_modules(): if isinstance(module, torch.nn.Linear): assert mtq.utils.is_quantized(module) def test_modelopt_memory_usage(self): inputs = self.get_dummy_inputs() inputs = { k: v.to(device=torch_device, dtype=torch.bfloat16) for k, v in inputs.items() if not isinstance(v, bool) } unquantized_model = self.model_cls.from_pretrained( self.model_id, torch_dtype=self.torch_dtype, subfolder="transformer" ) unquantized_model.to(torch_device) unquantized_model_memory = get_memory_consumption_stat(unquantized_model, inputs) quantized_model = self.model_cls.from_pretrained(**self.get_dummy_model_init_kwargs()) quantized_model.to(torch_device) quantized_model_memory = get_memory_consumption_stat(quantized_model, inputs) assert unquantized_model_memory / quantized_model_memory >= self.expected_memory_reduction def test_keep_modules_in_fp32(self): _keep_in_fp32_modules = self.model_cls._keep_in_fp32_modules self.model_cls._keep_in_fp32_modules = self.keep_in_fp32_module model = self.model_cls.from_pretrained(**self.get_dummy_model_init_kwargs()) model.to(torch_device) for name, module in model.named_modules(): if isinstance(module, torch.nn.Linear): if name in model._keep_in_fp32_modules: assert module.weight.dtype == torch.float32 self.model_cls._keep_in_fp32_modules = _keep_in_fp32_modules def test_modules_to_not_convert(self): init_kwargs = self.get_dummy_model_init_kwargs() quantization_config_kwargs = self.get_dummy_init_kwargs() quantization_config_kwargs.update({"modules_to_not_convert": self.modules_to_not_convert}) quantization_config = NVIDIAModelOptConfig(**quantization_config_kwargs) init_kwargs.update({"quantization_config": quantization_config}) model = self.model_cls.from_pretrained(**init_kwargs) model.to(torch_device) for name, module in model.named_modules(): if name in self.modules_to_not_convert: assert not mtq.utils.is_quantized(module) def test_dtype_assignment(self): model = self.model_cls.from_pretrained(**self.get_dummy_model_init_kwargs()) with self.assertRaises(ValueError): model.to(torch.float16) with self.assertRaises(ValueError): device_0 = f"{torch_device}:0" model.to(device=device_0, dtype=torch.float16) with self.assertRaises(ValueError): model.float() with self.assertRaises(ValueError): model.half() model.to(torch_device) def test_serialization(self): model = self.model_cls.from_pretrained(**self.get_dummy_model_init_kwargs()) inputs = self.get_dummy_inputs() model.to(torch_device) with torch.no_grad(): model_output = model(**inputs) with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained(tmp_dir) saved_model = self.model_cls.from_pretrained( tmp_dir, torch_dtype=torch.bfloat16, ) saved_model.to(torch_device) with torch.no_grad(): saved_model_output = saved_model(**inputs) assert torch.allclose(model_output.sample, saved_model_output.sample, rtol=1e-5, atol=1e-5) def test_torch_compile(self): if not self._test_torch_compile: return model = self.model_cls.from_pretrained(**self.get_dummy_model_init_kwargs()) compiled_model = torch.compile(model, mode="max-autotune", fullgraph=True, dynamic=False) model.to(torch_device) with torch.no_grad(): model_output = model(**self.get_dummy_inputs()).sample compiled_model.to(torch_device) with torch.no_grad(): compiled_model_output = compiled_model(**self.get_dummy_inputs()).sample model_output = model_output.detach().float().cpu().numpy() compiled_model_output = compiled_model_output.detach().float().cpu().numpy() max_diff = numpy_cosine_similarity_distance(model_output.flatten(), compiled_model_output.flatten()) assert max_diff < 1e-3 def test_device_map_error(self): with self.assertRaises(ValueError): _ = self.model_cls.from_pretrained( **self.get_dummy_model_init_kwargs(), device_map={0: "8GB", "cpu": "16GB"}, ) def get_dummy_inputs(self): batch_size = 1 seq_len = 16 height = width = 32 num_latent_channels = 4 caption_channels = 8 torch.manual_seed(0) hidden_states = torch.randn((batch_size, num_latent_channels, height, width)).to( torch_device, dtype=torch.bfloat16 ) encoder_hidden_states = torch.randn((batch_size, seq_len, caption_channels)).to( torch_device, dtype=torch.bfloat16 ) timestep = torch.tensor([1.0]).to(torch_device, dtype=torch.bfloat16).expand(batch_size) return { "hidden_states": hidden_states, "encoder_hidden_states": encoder_hidden_states, "timestep": timestep, } def test_model_cpu_offload(self): init_kwargs = self.get_dummy_init_kwargs() transformer = self.model_cls.from_pretrained( self.model_id, quantization_config=NVIDIAModelOptConfig(**init_kwargs), subfolder="transformer", torch_dtype=torch.bfloat16, ) pipe = self.pipeline_cls.from_pretrained(self.model_id, transformer=transformer, torch_dtype=torch.bfloat16) pipe.enable_model_cpu_offload(device=torch_device) _ = pipe("a cat holding a sign that says hello", num_inference_steps=2) def test_training(self): quantization_config = NVIDIAModelOptConfig(**self.get_dummy_init_kwargs()) quantized_model = self.model_cls.from_pretrained( self.model_id, subfolder="transformer", quantization_config=quantization_config, torch_dtype=torch.bfloat16, ).to(torch_device) for param in quantized_model.parameters(): param.requires_grad = False if param.ndim == 1: param.data = param.data.to(torch.float32) for _, module in quantized_model.named_modules(): if hasattr(module, "to_q"): module.to_q = LoRALayer(module.to_q, rank=4) if hasattr(module, "to_k"): module.to_k = LoRALayer(module.to_k, rank=4) if hasattr(module, "to_v"): module.to_v = LoRALayer(module.to_v, rank=4) with torch.amp.autocast(str(torch_device), dtype=torch.bfloat16): inputs = self.get_dummy_inputs() output = quantized_model(**inputs)[0] output.norm().backward() for module in quantized_model.modules(): if isinstance(module, LoRALayer): self.assertTrue(module.adapter[1].weight.grad is not None) class SanaTransformerFP8WeightsTest(ModelOptBaseTesterMixin, unittest.TestCase): expected_memory_reduction = 0.6 def get_dummy_init_kwargs(self): return {"quant_type": "FP8"} class SanaTransformerINT8WeightsTest(ModelOptBaseTesterMixin, unittest.TestCase): expected_memory_reduction = 0.6 _test_torch_compile = True def get_dummy_init_kwargs(self): return {"quant_type": "INT8"} @require_torch_cuda_compatibility(8.0) class SanaTransformerINT4WeightsTest(ModelOptBaseTesterMixin, unittest.TestCase): expected_memory_reduction = 0.55 def get_dummy_init_kwargs(self): return { "quant_type": "INT4", "block_quantize": 128, "channel_quantize": -1, "disable_conv_quantization": True, } @require_torch_cuda_compatibility(8.0) class SanaTransformerNF4WeightsTest(ModelOptBaseTesterMixin, unittest.TestCase): expected_memory_reduction = 0.65 def get_dummy_init_kwargs(self): return { "quant_type": "NF4", "block_quantize": 128, "channel_quantize": -1, "scale_block_quantize": 8, "scale_channel_quantize": -1, "modules_to_not_convert": ["conv"], } @require_torch_cuda_compatibility(8.0) class SanaTransformerNVFP4WeightsTest(ModelOptBaseTesterMixin, unittest.TestCase): expected_memory_reduction = 0.65 def get_dummy_init_kwargs(self): return { "quant_type": "NVFP4", "block_quantize": 128, "channel_quantize": -1, "scale_block_quantize": 8, "scale_channel_quantize": -1, "modules_to_not_convert": ["conv"], }
{ "repo_id": "huggingface/diffusers", "file_path": "tests/quantization/modelopt/test_modelopt.py", "license": "Apache License 2.0", "lines": 241, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
test
huggingface/diffusers:src/diffusers/pipelines/qwenimage/pipeline_qwenimage_edit_inpaint.py
# Copyright 2025 Qwen-Image Team and The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import inspect import math from typing import Any, Callable import numpy as np import PIL.Image import torch from transformers import Qwen2_5_VLForConditionalGeneration, Qwen2Tokenizer, Qwen2VLProcessor from ...image_processor import PipelineImageInput, VaeImageProcessor from ...loaders import QwenImageLoraLoaderMixin from ...models import AutoencoderKLQwenImage, QwenImageTransformer2DModel from ...schedulers import FlowMatchEulerDiscreteScheduler from ...utils import deprecate, is_torch_xla_available, logging, replace_example_docstring from ...utils.torch_utils import randn_tensor from ..pipeline_utils import DiffusionPipeline from .pipeline_output import QwenImagePipelineOutput if is_torch_xla_available(): import torch_xla.core.xla_model as xm XLA_AVAILABLE = True else: XLA_AVAILABLE = False logger = logging.get_logger(__name__) # pylint: disable=invalid-name EXAMPLE_DOC_STRING = """ Examples: ```py >>> import torch >>> from PIL import Image >>> from diffusers import QwenImageEditInpaintPipeline >>> from diffusers.utils import load_image >>> pipe = QwenImageEditInpaintPipeline.from_pretrained("Qwen/Qwen-Image-Edit", torch_dtype=torch.bfloat16) >>> pipe.to("cuda") >>> prompt = "Face of a yellow cat, high resolution, sitting on a park bench" >>> img_url = "https://raw.githubusercontent.com/CompVis/latent-diffusion/main/data/inpainting_examples/overture-creations-5sI6fQgYIuo.png" >>> mask_url = "https://raw.githubusercontent.com/CompVis/latent-diffusion/main/data/inpainting_examples/overture-creations-5sI6fQgYIuo_mask.png" >>> source = load_image(img_url) >>> mask = load_image(mask_url) >>> image = pipe( ... prompt=prompt, negative_prompt=" ", image=source, mask_image=mask, strength=1.0, num_inference_steps=50 ... ).images[0] >>> image.save("qwenimage_inpainting.png") ``` """ # Copied from diffusers.pipelines.qwenimage.pipeline_qwenimage.calculate_shift def calculate_shift( image_seq_len, base_seq_len: int = 256, max_seq_len: int = 4096, base_shift: float = 0.5, max_shift: float = 1.15, ): m = (max_shift - base_shift) / (max_seq_len - base_seq_len) b = base_shift - m * base_seq_len mu = image_seq_len * m + b return mu # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.retrieve_timesteps def retrieve_timesteps( scheduler, num_inference_steps: int | None = None, device: str | torch.device | None = None, timesteps: list[int] | None = None, sigmas: list[float] | None = None, **kwargs, ): r""" Calls the scheduler's `set_timesteps` method and retrieves timesteps from the scheduler after the call. Handles custom timesteps. Any kwargs will be supplied to `scheduler.set_timesteps`. Args: scheduler (`SchedulerMixin`): The scheduler to get timesteps from. num_inference_steps (`int`): The number of diffusion steps used when generating samples with a pre-trained model. If used, `timesteps` must be `None`. device (`str` or `torch.device`, *optional*): The device to which the timesteps should be moved to. If `None`, the timesteps are not moved. timesteps (`list[int]`, *optional*): Custom timesteps used to override the timestep spacing strategy of the scheduler. If `timesteps` is passed, `num_inference_steps` and `sigmas` must be `None`. sigmas (`list[float]`, *optional*): Custom sigmas used to override the timestep spacing strategy of the scheduler. If `sigmas` is passed, `num_inference_steps` and `timesteps` must be `None`. Returns: `tuple[torch.Tensor, int]`: A tuple where the first element is the timestep schedule from the scheduler and the second element is the number of inference steps. """ if timesteps is not None and sigmas is not None: raise ValueError("Only one of `timesteps` or `sigmas` can be passed. Please choose one to set custom values") if timesteps is not None: accepts_timesteps = "timesteps" in set(inspect.signature(scheduler.set_timesteps).parameters.keys()) if not accepts_timesteps: raise ValueError( f"The current scheduler class {scheduler.__class__}'s `set_timesteps` does not support custom" f" timestep schedules. Please check whether you are using the correct scheduler." ) scheduler.set_timesteps(timesteps=timesteps, device=device, **kwargs) timesteps = scheduler.timesteps num_inference_steps = len(timesteps) elif sigmas is not None: accept_sigmas = "sigmas" in set(inspect.signature(scheduler.set_timesteps).parameters.keys()) if not accept_sigmas: raise ValueError( f"The current scheduler class {scheduler.__class__}'s `set_timesteps` does not support custom" f" sigmas schedules. Please check whether you are using the correct scheduler." ) scheduler.set_timesteps(sigmas=sigmas, device=device, **kwargs) timesteps = scheduler.timesteps num_inference_steps = len(timesteps) else: scheduler.set_timesteps(num_inference_steps, device=device, **kwargs) timesteps = scheduler.timesteps return timesteps, num_inference_steps # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion_img2img.retrieve_latents def retrieve_latents( encoder_output: torch.Tensor, generator: torch.Generator | None = None, sample_mode: str = "sample" ): if hasattr(encoder_output, "latent_dist") and sample_mode == "sample": return encoder_output.latent_dist.sample(generator) elif hasattr(encoder_output, "latent_dist") and sample_mode == "argmax": return encoder_output.latent_dist.mode() elif hasattr(encoder_output, "latents"): return encoder_output.latents else: raise AttributeError("Could not access latents of provided encoder_output") # Copied from diffusers.pipelines.qwenimage.pipeline_qwenimage_edit.calculate_dimensions def calculate_dimensions(target_area, ratio): width = math.sqrt(target_area * ratio) height = width / ratio width = round(width / 32) * 32 height = round(height / 32) * 32 return width, height, None class QwenImageEditInpaintPipeline(DiffusionPipeline, QwenImageLoraLoaderMixin): r""" The Qwen-Image-Edit pipeline for image editing. Args: transformer ([`QwenImageTransformer2DModel`]): Conditional Transformer (MMDiT) architecture to denoise the encoded image latents. scheduler ([`FlowMatchEulerDiscreteScheduler`]): A scheduler to be used in combination with `transformer` to denoise the encoded image latents. vae ([`AutoencoderKL`]): Variational Auto-Encoder (VAE) Model to encode and decode images to and from latent representations. text_encoder ([`Qwen2.5-VL-7B-Instruct`]): [Qwen2.5-VL-7B-Instruct](https://huggingface.co/Qwen/Qwen2.5-VL-7B-Instruct), specifically the [Qwen2.5-VL-7B-Instruct](https://huggingface.co/Qwen/Qwen2.5-VL-7B-Instruct) variant. tokenizer (`QwenTokenizer`): Tokenizer of class [CLIPTokenizer](https://huggingface.co/docs/transformers/en/model_doc/clip#transformers.CLIPTokenizer). """ model_cpu_offload_seq = "text_encoder->transformer->vae" _callback_tensor_inputs = ["latents", "prompt_embeds"] def __init__( self, scheduler: FlowMatchEulerDiscreteScheduler, vae: AutoencoderKLQwenImage, text_encoder: Qwen2_5_VLForConditionalGeneration, tokenizer: Qwen2Tokenizer, processor: Qwen2VLProcessor, transformer: QwenImageTransformer2DModel, ): super().__init__() self.register_modules( vae=vae, text_encoder=text_encoder, tokenizer=tokenizer, processor=processor, transformer=transformer, scheduler=scheduler, ) self.vae_scale_factor = 2 ** len(self.vae.temperal_downsample) if getattr(self, "vae", None) else 8 self.latent_channels = self.vae.config.z_dim if getattr(self, "vae", None) else 16 # QwenImage latents are turned into 2x2 patches and packed. This means the latent width and height has to be divisible # by the patch size. So the vae scale factor is multiplied by the patch size to account for this self.image_processor = VaeImageProcessor(vae_scale_factor=self.vae_scale_factor * 2) self.mask_processor = VaeImageProcessor( vae_scale_factor=self.vae_scale_factor * 2, vae_latent_channels=self.latent_channels, do_normalize=False, do_binarize=True, do_convert_grayscale=True, ) self.vl_processor = processor self.tokenizer_max_length = 1024 self.prompt_template_encode = "<|im_start|>system\nDescribe the key features of the input image (color, shape, size, texture, objects, background), then explain how the user's text instruction should alter or modify the image. Generate a new image that meets the user's requirements while maintaining consistency with the original input where appropriate.<|im_end|>\n<|im_start|>user\n<|vision_start|><|image_pad|><|vision_end|>{}<|im_end|>\n<|im_start|>assistant\n" self.prompt_template_encode_start_idx = 64 self.default_sample_size = 128 # Copied from diffusers.pipelines.qwenimage.pipeline_qwenimage.QwenImagePipeline._extract_masked_hidden def _extract_masked_hidden(self, hidden_states: torch.Tensor, mask: torch.Tensor): bool_mask = mask.bool() valid_lengths = bool_mask.sum(dim=1) selected = hidden_states[bool_mask] split_result = torch.split(selected, valid_lengths.tolist(), dim=0) return split_result # Copied from diffusers.pipelines.qwenimage.pipeline_qwenimage_edit.QwenImageEditPipeline._get_qwen_prompt_embeds def _get_qwen_prompt_embeds( self, prompt: str | list[str] = None, image: torch.Tensor | None = None, device: torch.device | None = None, dtype: torch.dtype | None = None, ): device = device or self._execution_device dtype = dtype or self.text_encoder.dtype prompt = [prompt] if isinstance(prompt, str) else prompt template = self.prompt_template_encode drop_idx = self.prompt_template_encode_start_idx txt = [template.format(e) for e in prompt] model_inputs = self.processor( text=txt, images=image, padding=True, return_tensors="pt", ).to(device) outputs = self.text_encoder( input_ids=model_inputs.input_ids, attention_mask=model_inputs.attention_mask, pixel_values=model_inputs.pixel_values, image_grid_thw=model_inputs.image_grid_thw, output_hidden_states=True, ) hidden_states = outputs.hidden_states[-1] split_hidden_states = self._extract_masked_hidden(hidden_states, model_inputs.attention_mask) split_hidden_states = [e[drop_idx:] for e in split_hidden_states] attn_mask_list = [torch.ones(e.size(0), dtype=torch.long, device=e.device) for e in split_hidden_states] max_seq_len = max([e.size(0) for e in split_hidden_states]) prompt_embeds = torch.stack( [torch.cat([u, u.new_zeros(max_seq_len - u.size(0), u.size(1))]) for u in split_hidden_states] ) encoder_attention_mask = torch.stack( [torch.cat([u, u.new_zeros(max_seq_len - u.size(0))]) for u in attn_mask_list] ) prompt_embeds = prompt_embeds.to(dtype=dtype, device=device) return prompt_embeds, encoder_attention_mask # Copied from diffusers.pipelines.qwenimage.pipeline_qwenimage_edit.QwenImageEditPipeline.encode_prompt def encode_prompt( self, prompt: str | list[str], image: torch.Tensor | None = None, device: torch.device | None = None, num_images_per_prompt: int = 1, prompt_embeds: torch.Tensor | None = None, prompt_embeds_mask: torch.Tensor | None = None, max_sequence_length: int = 1024, ): r""" Args: prompt (`str` or `list[str]`, *optional*): prompt to be encoded image (`torch.Tensor`, *optional*): image to be encoded device: (`torch.device`): torch device num_images_per_prompt (`int`): number of images that should be generated per prompt prompt_embeds (`torch.Tensor`, *optional*): Pre-generated text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt weighting. If not provided, text embeddings will be generated from `prompt` input argument. """ device = device or self._execution_device prompt = [prompt] if isinstance(prompt, str) else prompt batch_size = len(prompt) if prompt_embeds is None else prompt_embeds.shape[0] if prompt_embeds is None: prompt_embeds, prompt_embeds_mask = self._get_qwen_prompt_embeds(prompt, image, device) _, seq_len, _ = prompt_embeds.shape prompt_embeds = prompt_embeds.repeat(1, num_images_per_prompt, 1) prompt_embeds = prompt_embeds.view(batch_size * num_images_per_prompt, seq_len, -1) prompt_embeds_mask = prompt_embeds_mask.repeat(1, num_images_per_prompt, 1) prompt_embeds_mask = prompt_embeds_mask.view(batch_size * num_images_per_prompt, seq_len) if prompt_embeds_mask is not None and prompt_embeds_mask.all(): prompt_embeds_mask = None return prompt_embeds, prompt_embeds_mask # Copied from diffusers.pipelines.qwenimage.pipeline_qwenimage_inpaint.QwenImageInpaintPipeline.check_inputs def check_inputs( self, prompt, image, mask_image, strength, height, width, output_type, negative_prompt=None, prompt_embeds=None, negative_prompt_embeds=None, prompt_embeds_mask=None, negative_prompt_embeds_mask=None, callback_on_step_end_tensor_inputs=None, padding_mask_crop=None, max_sequence_length=None, ): if strength < 0 or strength > 1: raise ValueError(f"The value of strength should in [0.0, 1.0] but is {strength}") if height % (self.vae_scale_factor * 2) != 0 or width % (self.vae_scale_factor * 2) != 0: logger.warning( f"`height` and `width` have to be divisible by {self.vae_scale_factor * 2} but are {height} and {width}. Dimensions will be resized accordingly" ) if callback_on_step_end_tensor_inputs is not None and not all( k in self._callback_tensor_inputs for k in callback_on_step_end_tensor_inputs ): raise ValueError( f"`callback_on_step_end_tensor_inputs` has to be in {self._callback_tensor_inputs}, but found {[k for k in callback_on_step_end_tensor_inputs if k not in self._callback_tensor_inputs]}" ) if prompt is not None and prompt_embeds is not None: raise ValueError( f"Cannot forward both `prompt`: {prompt} and `prompt_embeds`: {prompt_embeds}. Please make sure to" " only forward one of the two." ) elif prompt is None and prompt_embeds is None: raise ValueError( "Provide either `prompt` or `prompt_embeds`. Cannot leave both `prompt` and `prompt_embeds` undefined." ) elif prompt is not None and (not isinstance(prompt, str) and not isinstance(prompt, list)): raise ValueError(f"`prompt` has to be of type `str` or `list` but is {type(prompt)}") if negative_prompt is not None and negative_prompt_embeds is not None: raise ValueError( f"Cannot forward both `negative_prompt`: {negative_prompt} and `negative_prompt_embeds`:" f" {negative_prompt_embeds}. Please make sure to only forward one of the two." ) if padding_mask_crop is not None: if not isinstance(image, PIL.Image.Image): raise ValueError( f"The image should be a PIL image when inpainting mask crop, but is of type {type(image)}." ) if not isinstance(mask_image, PIL.Image.Image): raise ValueError( f"The mask image should be a PIL image when inpainting mask crop, but is of type" f" {type(mask_image)}." ) if output_type != "pil": raise ValueError(f"The output type should be PIL when inpainting mask crop, but is {output_type}.") if max_sequence_length is not None and max_sequence_length > 1024: raise ValueError(f"`max_sequence_length` cannot be greater than 1024 but is {max_sequence_length}") @staticmethod # Copied from diffusers.pipelines.qwenimage.pipeline_qwenimage.QwenImagePipeline._pack_latents def _pack_latents(latents, batch_size, num_channels_latents, height, width): latents = latents.view(batch_size, num_channels_latents, height // 2, 2, width // 2, 2) latents = latents.permute(0, 2, 4, 1, 3, 5) latents = latents.reshape(batch_size, (height // 2) * (width // 2), num_channels_latents * 4) return latents @staticmethod # Copied from diffusers.pipelines.qwenimage.pipeline_qwenimage.QwenImagePipeline._unpack_latents def _unpack_latents(latents, height, width, vae_scale_factor): batch_size, num_patches, channels = latents.shape # VAE applies 8x compression on images but we must also account for packing which requires # latent height and width to be divisible by 2. height = 2 * (int(height) // (vae_scale_factor * 2)) width = 2 * (int(width) // (vae_scale_factor * 2)) latents = latents.view(batch_size, height // 2, width // 2, channels // 4, 2, 2) latents = latents.permute(0, 3, 1, 4, 2, 5) latents = latents.reshape(batch_size, channels // (2 * 2), 1, height, width) return latents # Copied from diffusers.pipelines.qwenimage.pipeline_qwenimage_img2img.QwenImageImg2ImgPipeline._encode_vae_image def _encode_vae_image(self, image: torch.Tensor, generator: torch.Generator): if isinstance(generator, list): image_latents = [ retrieve_latents(self.vae.encode(image[i : i + 1]), generator=generator[i]) for i in range(image.shape[0]) ] image_latents = torch.cat(image_latents, dim=0) else: image_latents = retrieve_latents(self.vae.encode(image), generator=generator) latents_mean = ( torch.tensor(self.vae.config.latents_mean) .view(1, self.vae.config.z_dim, 1, 1, 1) .to(image_latents.device, image_latents.dtype) ) latents_std = 1.0 / torch.tensor(self.vae.config.latents_std).view(1, self.vae.config.z_dim, 1, 1, 1).to( image_latents.device, image_latents.dtype ) image_latents = (image_latents - latents_mean) * latents_std return image_latents # Copied from diffusers.pipelines.stable_diffusion_3.pipeline_stable_diffusion_3_img2img.StableDiffusion3Img2ImgPipeline.get_timesteps def get_timesteps(self, num_inference_steps, strength, device): # get the original timestep using init_timestep init_timestep = min(num_inference_steps * strength, num_inference_steps) t_start = int(max(num_inference_steps - init_timestep, 0)) timesteps = self.scheduler.timesteps[t_start * self.scheduler.order :] if hasattr(self.scheduler, "set_begin_index"): self.scheduler.set_begin_index(t_start * self.scheduler.order) return timesteps, num_inference_steps - t_start def enable_vae_slicing(self): r""" Enable sliced VAE decoding. When this option is enabled, the VAE will split the input tensor in slices to compute decoding in several steps. This is useful to save some memory and allow larger batch sizes. """ depr_message = f"Calling `enable_vae_slicing()` on a `{self.__class__.__name__}` is deprecated and this method will be removed in a future version. Please use `pipe.vae.enable_slicing()`." deprecate( "enable_vae_slicing", "0.40.0", depr_message, ) self.vae.enable_slicing() def disable_vae_slicing(self): r""" Disable sliced VAE decoding. If `enable_vae_slicing` was previously enabled, this method will go back to computing decoding in one step. """ depr_message = f"Calling `disable_vae_slicing()` on a `{self.__class__.__name__}` is deprecated and this method will be removed in a future version. Please use `pipe.vae.disable_slicing()`." deprecate( "disable_vae_slicing", "0.40.0", depr_message, ) self.vae.disable_slicing() def enable_vae_tiling(self): r""" Enable tiled VAE decoding. When this option is enabled, the VAE will split the input tensor into tiles to compute decoding and encoding in several steps. This is useful for saving a large amount of memory and to allow processing larger images. """ depr_message = f"Calling `enable_vae_tiling()` on a `{self.__class__.__name__}` is deprecated and this method will be removed in a future version. Please use `pipe.vae.enable_tiling()`." deprecate( "enable_vae_tiling", "0.40.0", depr_message, ) self.vae.enable_tiling() def disable_vae_tiling(self): r""" Disable tiled VAE decoding. If `enable_vae_tiling` was previously enabled, this method will go back to computing decoding in one step. """ depr_message = f"Calling `disable_vae_tiling()` on a `{self.__class__.__name__}` is deprecated and this method will be removed in a future version. Please use `pipe.vae.disable_tiling()`." deprecate( "disable_vae_tiling", "0.40.0", depr_message, ) self.vae.disable_tiling() # Copied from diffusers.pipelines.qwenimage.pipeline_qwenimage_inpaint.QwenImageInpaintPipeline.prepare_latents def prepare_latents( self, image, timestep, batch_size, num_channels_latents, height, width, dtype, device, generator, latents=None, ): if isinstance(generator, list) and len(generator) != batch_size: raise ValueError( f"You have passed a list of generators of length {len(generator)}, but requested an effective batch" f" size of {batch_size}. Make sure the batch size matches the length of the generators." ) # VAE applies 8x compression on images but we must also account for packing which requires # latent height and width to be divisible by 2. height = 2 * (int(height) // (self.vae_scale_factor * 2)) width = 2 * (int(width) // (self.vae_scale_factor * 2)) shape = (batch_size, 1, num_channels_latents, height, width) # If image is [B,C,H,W] -> add T=1. If it's already [B,C,T,H,W], leave it. if image.dim() == 4: image = image.unsqueeze(2) elif image.dim() != 5: raise ValueError(f"Expected image dims 4 or 5, got {image.dim()}.") if latents is not None: return latents.to(device=device, dtype=dtype) image = image.to(device=device, dtype=dtype) if image.shape[1] != self.latent_channels: image_latents = self._encode_vae_image(image=image, generator=generator) # [B,z,1,H',W'] else: image_latents = image if batch_size > image_latents.shape[0] and batch_size % image_latents.shape[0] == 0: # expand init_latents for batch_size additional_image_per_prompt = batch_size // image_latents.shape[0] image_latents = torch.cat([image_latents] * additional_image_per_prompt, dim=0) elif batch_size > image_latents.shape[0] and batch_size % image_latents.shape[0] != 0: raise ValueError( f"Cannot duplicate `image` of batch size {image_latents.shape[0]} to {batch_size} text prompts." ) else: image_latents = torch.cat([image_latents], dim=0) image_latents = image_latents.transpose(1, 2) # [B,1,z,H',W'] if latents is None: noise = randn_tensor(shape, generator=generator, device=device, dtype=dtype) latents = self.scheduler.scale_noise(image_latents, timestep, noise) else: noise = latents.to(device) latents = noise noise = self._pack_latents(noise, batch_size, num_channels_latents, height, width) image_latents = self._pack_latents(image_latents, batch_size, num_channels_latents, height, width) latents = self._pack_latents(latents, batch_size, num_channels_latents, height, width) return latents, noise, image_latents # Copied from diffusers.pipelines.qwenimage.pipeline_qwenimage_inpaint.QwenImageInpaintPipeline.prepare_mask_latents def prepare_mask_latents( self, mask, masked_image, batch_size, num_channels_latents, num_images_per_prompt, height, width, dtype, device, generator, ): # VAE applies 8x compression on images but we must also account for packing which requires # latent height and width to be divisible by 2. height = 2 * (int(height) // (self.vae_scale_factor * 2)) width = 2 * (int(width) // (self.vae_scale_factor * 2)) # resize the mask to latents shape as we concatenate the mask to the latents # we do that before converting to dtype to avoid breaking in case we're using cpu_offload # and half precision mask = torch.nn.functional.interpolate(mask, size=(height, width)) mask = mask.to(device=device, dtype=dtype) batch_size = batch_size * num_images_per_prompt if masked_image.dim() == 4: masked_image = masked_image.unsqueeze(2) elif masked_image.dim() != 5: raise ValueError(f"Expected image dims 4 or 5, got {masked_image.dim()}.") masked_image = masked_image.to(device=device, dtype=dtype) if masked_image.shape[1] == self.latent_channels: masked_image_latents = masked_image else: masked_image_latents = self._encode_vae_image(image=masked_image, generator=generator) # duplicate mask and masked_image_latents for each generation per prompt, using mps friendly method if mask.shape[0] < batch_size: if not batch_size % mask.shape[0] == 0: raise ValueError( "The passed mask and the required batch size don't match. Masks are supposed to be duplicated to" f" a total batch size of {batch_size}, but {mask.shape[0]} masks were passed. Make sure the number" " of masks that you pass is divisible by the total requested batch size." ) mask = mask.repeat(batch_size // mask.shape[0], 1, 1, 1) if masked_image_latents.shape[0] < batch_size: if not batch_size % masked_image_latents.shape[0] == 0: raise ValueError( "The passed images and the required batch size don't match. Images are supposed to be duplicated" f" to a total batch size of {batch_size}, but {masked_image_latents.shape[0]} images were passed." " Make sure the number of images that you pass is divisible by the total requested batch size." ) masked_image_latents = masked_image_latents.repeat(batch_size // masked_image_latents.shape[0], 1, 1, 1, 1) # aligning device to prevent device errors when concating it with the latent model input masked_image_latents = masked_image_latents.to(device=device, dtype=dtype) masked_image_latents = self._pack_latents( masked_image_latents, batch_size, num_channels_latents, height, width, ) mask = self._pack_latents( mask.repeat(1, num_channels_latents, 1, 1), batch_size, num_channels_latents, height, width, ) return mask, masked_image_latents @property def guidance_scale(self): return self._guidance_scale @property def attention_kwargs(self): return self._attention_kwargs @property def num_timesteps(self): return self._num_timesteps @property def current_timestep(self): return self._current_timestep @property def interrupt(self): return self._interrupt @torch.no_grad() @replace_example_docstring(EXAMPLE_DOC_STRING) def __call__( self, image: PipelineImageInput | None = None, prompt: str | list[str] = None, negative_prompt: str | list[str] = None, mask_image: PipelineImageInput = None, masked_image_latents: PipelineImageInput = None, true_cfg_scale: float = 4.0, height: int | None = None, width: int | None = None, padding_mask_crop: int | None = None, strength: float = 0.6, num_inference_steps: int = 50, sigmas: list[float] | None = None, guidance_scale: float | None = None, num_images_per_prompt: int = 1, generator: torch.Generator | list[torch.Generator] | None = None, latents: torch.Tensor | None = None, prompt_embeds: torch.Tensor | None = None, prompt_embeds_mask: torch.Tensor | None = None, negative_prompt_embeds: torch.Tensor | None = None, negative_prompt_embeds_mask: torch.Tensor | None = None, output_type: str | None = "pil", return_dict: bool = True, attention_kwargs: dict[str, Any] | None = None, callback_on_step_end: Callable[[int, int], None] | None = None, callback_on_step_end_tensor_inputs: list[str] = ["latents"], max_sequence_length: int = 512, ): r""" Function invoked when calling the pipeline for generation. Args: image (`torch.Tensor`, `PIL.Image.Image`, `np.ndarray`, `list[torch.Tensor]`, `list[PIL.Image.Image]`, or `list[np.ndarray]`): `Image`, numpy array or tensor representing an image batch to be used as the starting point. For both numpy array and pytorch tensor, the expected value range is between `[0, 1]` If it's a tensor or a list or tensors, the expected shape should be `(B, C, H, W)` or `(C, H, W)`. If it is a numpy array or a list of arrays, the expected shape should be `(B, H, W, C)` or `(H, W, C)` It can also accept image latents as `image`, but if passing latents directly it is not encoded again. prompt (`str` or `list[str]`, *optional*): The prompt or prompts to guide the image generation. If not defined, one has to pass `prompt_embeds`. instead. negative_prompt (`str` or `list[str]`, *optional*): The prompt or prompts not to guide the image generation. If not defined, one has to pass `negative_prompt_embeds` instead. Ignored when not using guidance (i.e., ignored if `true_cfg_scale` is not greater than `1`). true_cfg_scale (`float`, *optional*, defaults to 1.0): true_cfg_scale (`float`, *optional*, defaults to 1.0): Guidance scale as defined in [Classifier-Free Diffusion Guidance](https://huggingface.co/papers/2207.12598). `true_cfg_scale` is defined as `w` of equation 2. of [Imagen Paper](https://huggingface.co/papers/2205.11487). Classifier-free guidance is enabled by setting `true_cfg_scale > 1` and a provided `negative_prompt`. Higher guidance scale encourages to generate images that are closely linked to the text `prompt`, usually at the expense of lower image quality. mask_image (`torch.Tensor`, `PIL.Image.Image`, `np.ndarray`, `list[torch.Tensor]`, `list[PIL.Image.Image]`, or `list[np.ndarray]`): `Image`, numpy array or tensor representing an image batch to mask `image`. White pixels in the mask are repainted while black pixels are preserved. If `mask_image` is a PIL image, it is converted to a single channel (luminance) before use. If it's a numpy array or pytorch tensor, it should contain one color channel (L) instead of 3, so the expected shape for pytorch tensor would be `(B, 1, H, W)`, `(B, H, W)`, `(1, H, W)`, `(H, W)`. And for numpy array would be for `(B, H, W, 1)`, `(B, H, W)`, `(H, W, 1)`, or `(H, W)`. mask_image_latent (`torch.Tensor`, `list[torch.Tensor]`): `Tensor` representing an image batch to mask `image` generated by VAE. If not provided, the mask latents tensor will ge generated by `mask_image`. height (`int`, *optional*, defaults to self.unet.config.sample_size * self.vae_scale_factor): The height in pixels of the generated image. This is set to 1024 by default for the best results. width (`int`, *optional*, defaults to self.unet.config.sample_size * self.vae_scale_factor): The width in pixels of the generated image. This is set to 1024 by default for the best results. padding_mask_crop (`int`, *optional*, defaults to `None`): The size of margin in the crop to be applied to the image and masking. If `None`, no crop is applied to image and mask_image. If `padding_mask_crop` is not `None`, it will first find a rectangular region with the same aspect ration of the image and contains all masked area, and then expand that area based on `padding_mask_crop`. The image and mask_image will then be cropped based on the expanded area before resizing to the original image size for inpainting. This is useful when the masked area is small while the image is large and contain information irrelevant for inpainting, such as background. strength (`float`, *optional*, defaults to 1.0): Indicates extent to transform the reference `image`. Must be between 0 and 1. `image` is used as a starting point and more noise is added the higher the `strength`. The number of denoising steps depends on the amount of noise initially added. When `strength` is 1, added noise is maximum and the denoising process runs for the full number of iterations specified in `num_inference_steps`. A value of 1 essentially ignores `image`. num_inference_steps (`int`, *optional*, defaults to 50): The number of denoising steps. More denoising steps usually lead to a higher quality image at the expense of slower inference. sigmas (`list[float]`, *optional*): Custom sigmas to use for the denoising process with schedulers which support a `sigmas` argument in their `set_timesteps` method. If not defined, the default behavior when `num_inference_steps` is passed will be used. guidance_scale (`float`, *optional*, defaults to None): A guidance scale value for guidance distilled models. Unlike the traditional classifier-free guidance where the guidance scale is applied during inference through noise prediction rescaling, guidance distilled models take the guidance scale directly as an input parameter during forward pass. Guidance scale is enabled by setting `guidance_scale > 1`. Higher guidance scale encourages to generate images that are closely linked to the text `prompt`, usually at the expense of lower image quality. This parameter in the pipeline is there to support future guidance-distilled models when they come up. It is ignored when not using guidance distilled models. To enable traditional classifier-free guidance, please pass `true_cfg_scale > 1.0` and `negative_prompt` (even an empty negative prompt like " " should enable classifier-free guidance computations). num_images_per_prompt (`int`, *optional*, defaults to 1): The number of images to generate per prompt. generator (`torch.Generator` or `list[torch.Generator]`, *optional*): One or a list of [torch generator(s)](https://pytorch.org/docs/stable/generated/torch.Generator.html) to make generation deterministic. latents (`torch.Tensor`, *optional*): Pre-generated noisy latents, sampled from a Gaussian distribution, to be used as inputs for image generation. Can be used to tweak the same generation with different prompts. If not provided, a latents tensor will be generated by sampling using the supplied random `generator`. prompt_embeds (`torch.Tensor`, *optional*): Pre-generated text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt weighting. If not provided, text embeddings will be generated from `prompt` input argument. negative_prompt_embeds (`torch.Tensor`, *optional*): Pre-generated negative text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt weighting. If not provided, negative_prompt_embeds will be generated from `negative_prompt` input argument. output_type (`str`, *optional*, defaults to `"pil"`): The output format of the generate image. Choose between [PIL](https://pillow.readthedocs.io/en/stable/): `PIL.Image.Image` or `np.array`. return_dict (`bool`, *optional*, defaults to `True`): Whether or not to return a [`~pipelines.qwenimage.QwenImagePipelineOutput`] instead of a plain tuple. attention_kwargs (`dict`, *optional*): A kwargs dictionary that if specified is passed along to the `AttentionProcessor` as defined under `self.processor` in [diffusers.models.attention_processor](https://github.com/huggingface/diffusers/blob/main/src/diffusers/models/attention_processor.py). callback_on_step_end (`Callable`, *optional*): A function that calls at the end of each denoising steps during the inference. The function is called with the following arguments: `callback_on_step_end(self: DiffusionPipeline, step: int, timestep: int, callback_kwargs: Dict)`. `callback_kwargs` will include a list of all tensors as specified by `callback_on_step_end_tensor_inputs`. callback_on_step_end_tensor_inputs (`list`, *optional*): The list of tensor inputs for the `callback_on_step_end` function. The tensors specified in the list will be passed as `callback_kwargs` argument. You will only be able to include variables listed in the `._callback_tensor_inputs` attribute of your pipeline class. max_sequence_length (`int` defaults to 512): Maximum sequence length to use with the `prompt`. Examples: Returns: [`~pipelines.qwenimage.QwenImagePipelineOutput`] or `tuple`: [`~pipelines.qwenimage.QwenImagePipelineOutput`] if `return_dict` is True, otherwise a `tuple`. When returning a tuple, the first element is a list with the generated images. """ image_size = image[0].size if isinstance(image, list) else image.size calculated_width, calculated_height, _ = calculate_dimensions(1024 * 1024, image_size[0] / image_size[1]) # height and width are the same as the calculated height and width height = calculated_height width = calculated_width multiple_of = self.vae_scale_factor * 2 width = width // multiple_of * multiple_of height = height // multiple_of * multiple_of # 1. Check inputs. Raise error if not correct self.check_inputs( prompt, image, mask_image, strength, height, width, output_type=output_type, negative_prompt=negative_prompt, prompt_embeds=prompt_embeds, negative_prompt_embeds=negative_prompt_embeds, prompt_embeds_mask=prompt_embeds_mask, negative_prompt_embeds_mask=negative_prompt_embeds_mask, callback_on_step_end_tensor_inputs=callback_on_step_end_tensor_inputs, padding_mask_crop=padding_mask_crop, max_sequence_length=max_sequence_length, ) self._guidance_scale = guidance_scale self._attention_kwargs = attention_kwargs self._current_timestep = None self._interrupt = False # 2. Define call parameters if prompt is not None and isinstance(prompt, str): batch_size = 1 elif prompt is not None and isinstance(prompt, list): batch_size = len(prompt) else: batch_size = prompt_embeds.shape[0] device = self._execution_device # 3. Preprocess image if padding_mask_crop is not None: crops_coords = self.mask_processor.get_crop_region(mask_image, width, height, pad=padding_mask_crop) resize_mode = "fill" else: crops_coords = None resize_mode = "default" if image is not None and not (isinstance(image, torch.Tensor) and image.size(1) == self.latent_channels): image = self.image_processor.resize(image, calculated_height, calculated_width) original_image = image prompt_image = image image = self.image_processor.preprocess( image, height=calculated_height, width=calculated_width, crops_coords=crops_coords, resize_mode=resize_mode, ) image = image.to(dtype=torch.float32) has_neg_prompt = negative_prompt is not None or ( negative_prompt_embeds is not None and negative_prompt_embeds_mask is not None ) if true_cfg_scale > 1 and not has_neg_prompt: logger.warning( f"true_cfg_scale is passed as {true_cfg_scale}, but classifier-free guidance is not enabled since no negative_prompt is provided." ) elif true_cfg_scale <= 1 and has_neg_prompt: logger.warning( " negative_prompt is passed but classifier-free guidance is not enabled since true_cfg_scale <= 1" ) do_true_cfg = true_cfg_scale > 1 and has_neg_prompt prompt_embeds, prompt_embeds_mask = self.encode_prompt( image=prompt_image, prompt=prompt, prompt_embeds=prompt_embeds, prompt_embeds_mask=prompt_embeds_mask, device=device, num_images_per_prompt=num_images_per_prompt, max_sequence_length=max_sequence_length, ) if do_true_cfg: negative_prompt_embeds, negative_prompt_embeds_mask = self.encode_prompt( image=prompt_image, prompt=negative_prompt, prompt_embeds=negative_prompt_embeds, prompt_embeds_mask=negative_prompt_embeds_mask, device=device, num_images_per_prompt=num_images_per_prompt, max_sequence_length=max_sequence_length, ) # 4. Prepare timesteps sigmas = np.linspace(1.0, 1 / num_inference_steps, num_inference_steps) if sigmas is None else sigmas image_seq_len = (int(height) // self.vae_scale_factor // 2) * (int(width) // self.vae_scale_factor // 2) mu = calculate_shift( image_seq_len, self.scheduler.config.get("base_image_seq_len", 256), self.scheduler.config.get("max_image_seq_len", 4096), self.scheduler.config.get("base_shift", 0.5), self.scheduler.config.get("max_shift", 1.15), ) timesteps, num_inference_steps = retrieve_timesteps( self.scheduler, num_inference_steps, device, sigmas=sigmas, mu=mu, ) timesteps, num_inference_steps = self.get_timesteps(num_inference_steps, strength, device) if num_inference_steps < 1: raise ValueError( f"After adjusting the num_inference_steps by strength parameter: {strength}, the number of pipeline" f"steps is {num_inference_steps} which is < 1 and not appropriate for this pipeline." ) latent_timestep = timesteps[:1].repeat(batch_size * num_images_per_prompt) # 5. Prepare latent variables num_channels_latents = self.transformer.config.in_channels // 4 latents, noise, image_latents = self.prepare_latents( image, latent_timestep, batch_size * num_images_per_prompt, num_channels_latents, height, width, prompt_embeds.dtype, device, generator, latents, ) mask_condition = self.mask_processor.preprocess( mask_image, height=height, width=width, resize_mode=resize_mode, crops_coords=crops_coords ) if masked_image_latents is None: masked_image = image * (mask_condition < 0.5) else: masked_image = masked_image_latents mask, masked_image_latents = self.prepare_mask_latents( mask_condition, masked_image, batch_size, num_channels_latents, num_images_per_prompt, height, width, prompt_embeds.dtype, device, generator, ) img_shapes = [ [ (1, height // self.vae_scale_factor // 2, width // self.vae_scale_factor // 2), (1, calculated_height // self.vae_scale_factor // 2, calculated_width // self.vae_scale_factor // 2), ] ] * batch_size num_warmup_steps = max(len(timesteps) - num_inference_steps * self.scheduler.order, 0) self._num_timesteps = len(timesteps) # handle guidance if self.transformer.config.guidance_embeds and guidance_scale is None: raise ValueError("guidance_scale is required for guidance-distilled model.") elif self.transformer.config.guidance_embeds: guidance = torch.full([1], guidance_scale, device=device, dtype=torch.float32) guidance = guidance.expand(latents.shape[0]) elif not self.transformer.config.guidance_embeds and guidance_scale is not None: logger.warning( f"guidance_scale is passed as {guidance_scale}, but ignored since the model is not guidance-distilled." ) guidance = None elif not self.transformer.config.guidance_embeds and guidance_scale is None: guidance = None if self.attention_kwargs is None: self._attention_kwargs = {} # 6. Denoising loop with self.progress_bar(total=num_inference_steps) as progress_bar: for i, t in enumerate(timesteps): if self.interrupt: continue self._current_timestep = t latent_model_input = latents if image_latents is not None: latent_model_input = torch.cat([latents, image_latents], dim=1) # broadcast to batch dimension in a way that's compatible with ONNX/Core ML timestep = t.expand(latents.shape[0]).to(latents.dtype) with self.transformer.cache_context("cond"): noise_pred = self.transformer( hidden_states=latent_model_input, timestep=timestep / 1000, guidance=guidance, encoder_hidden_states_mask=prompt_embeds_mask, encoder_hidden_states=prompt_embeds, img_shapes=img_shapes, attention_kwargs=self.attention_kwargs, return_dict=False, )[0] noise_pred = noise_pred[:, : latents.size(1)] if do_true_cfg: with self.transformer.cache_context("uncond"): neg_noise_pred = self.transformer( hidden_states=latent_model_input, timestep=timestep / 1000, guidance=guidance, encoder_hidden_states_mask=negative_prompt_embeds_mask, encoder_hidden_states=negative_prompt_embeds, img_shapes=img_shapes, attention_kwargs=self.attention_kwargs, return_dict=False, )[0] neg_noise_pred = neg_noise_pred[:, : latents.size(1)] comb_pred = neg_noise_pred + true_cfg_scale * (noise_pred - neg_noise_pred) cond_norm = torch.norm(noise_pred, dim=-1, keepdim=True) noise_norm = torch.norm(comb_pred, dim=-1, keepdim=True) noise_pred = comb_pred * (cond_norm / noise_norm) # compute the previous noisy sample x_t -> x_t-1 latents_dtype = latents.dtype latents = self.scheduler.step(noise_pred, t, latents, return_dict=False)[0] # for 64 channel transformer only. init_latents_proper = image_latents init_mask = mask if i < len(timesteps) - 1: noise_timestep = timesteps[i + 1] init_latents_proper = self.scheduler.scale_noise( init_latents_proper, torch.tensor([noise_timestep]), noise ) latents = (1 - init_mask) * init_latents_proper + init_mask * latents if latents.dtype != latents_dtype: if torch.backends.mps.is_available(): # some platforms (eg. apple mps) misbehave due to a pytorch bug: https://github.com/pytorch/pytorch/pull/99272 latents = latents.to(latents_dtype) if callback_on_step_end is not None: callback_kwargs = {} for k in callback_on_step_end_tensor_inputs: callback_kwargs[k] = locals()[k] callback_outputs = callback_on_step_end(self, i, t, callback_kwargs) latents = callback_outputs.pop("latents", latents) prompt_embeds = callback_outputs.pop("prompt_embeds", prompt_embeds) # call the callback, if provided if i == len(timesteps) - 1 or ((i + 1) > num_warmup_steps and (i + 1) % self.scheduler.order == 0): progress_bar.update() if XLA_AVAILABLE: xm.mark_step() self._current_timestep = None if output_type == "latent": image = latents else: latents = self._unpack_latents(latents, height, width, self.vae_scale_factor) latents = latents.to(self.vae.dtype) latents_mean = ( torch.tensor(self.vae.config.latents_mean) .view(1, self.vae.config.z_dim, 1, 1, 1) .to(latents.device, latents.dtype) ) latents_std = 1.0 / torch.tensor(self.vae.config.latents_std).view(1, self.vae.config.z_dim, 1, 1, 1).to( latents.device, latents.dtype ) latents = latents / latents_std + latents_mean image = self.vae.decode(latents, return_dict=False)[0][:, :, 0] image = self.image_processor.postprocess(image, output_type=output_type) if padding_mask_crop is not None: image = [ self.image_processor.apply_overlay(mask_image, original_image, i, crops_coords) for i in image ] # Offload all models self.maybe_free_model_hooks() if not return_dict: return (image,) return QwenImagePipelineOutput(images=image)
{ "repo_id": "huggingface/diffusers", "file_path": "src/diffusers/pipelines/qwenimage/pipeline_qwenimage_edit_inpaint.py", "license": "Apache License 2.0", "lines": 978, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
huggingface/diffusers:tests/testing_utils.py
import functools import glob import importlib import importlib.metadata import inspect import io import logging import multiprocessing import os import random import re import struct import sys import tempfile import time import urllib.parse from collections import UserDict from contextlib import contextmanager from io import BytesIO, StringIO from pathlib import Path from typing import TYPE_CHECKING, Any, Callable, Tuple, Union import numpy as np import PIL.Image import PIL.ImageOps import pytest import requests from numpy.linalg import norm from packaging import version from diffusers.utils.constants import DIFFUSERS_REQUEST_TIMEOUT from diffusers.utils.import_utils import ( BACKENDS_MAPPING, is_accelerate_available, is_bitsandbytes_available, is_compel_available, is_flax_available, is_gguf_available, is_kernels_available, is_note_seq_available, is_nvidia_modelopt_version, is_onnx_available, is_opencv_available, is_optimum_quanto_available, is_peft_available, is_timm_available, is_torch_available, is_torch_version, is_torchao_available, is_torchsde_available, is_transformers_available, ) from diffusers.utils.logging import get_logger if is_torch_available(): import torch IS_ROCM_SYSTEM = torch.version.hip is not None IS_CUDA_SYSTEM = torch.version.cuda is not None IS_XPU_SYSTEM = getattr(torch.version, "xpu", None) is not None else: IS_ROCM_SYSTEM = False IS_CUDA_SYSTEM = False IS_XPU_SYSTEM = False IS_GITHUB_ACTIONS = os.getenv("GITHUB_ACTIONS") == "true" and os.getenv("DIFFUSERS_IS_CI") == "yes" global_rng = random.Random() logger = get_logger(__name__) _required_peft_version = is_peft_available() and version.parse( version.parse(importlib.metadata.version("peft")).base_version ) > version.parse("0.5") _required_transformers_version = is_transformers_available() and version.parse( version.parse(importlib.metadata.version("transformers")).base_version ) > version.parse("4.33") USE_PEFT_BACKEND = _required_peft_version and _required_transformers_version BIG_GPU_MEMORY = int(os.getenv("BIG_GPU_MEMORY", 40)) if is_torch_available(): import torch # Set a backend environment variable for any extra module import required for a custom accelerator if "DIFFUSERS_TEST_BACKEND" in os.environ: backend = os.environ["DIFFUSERS_TEST_BACKEND"] try: _ = importlib.import_module(backend) except ModuleNotFoundError as e: raise ModuleNotFoundError( f"Failed to import `DIFFUSERS_TEST_BACKEND` '{backend}'! This should be the name of an installed module \ to enable a specified backend.):\n{e}" ) from e if "DIFFUSERS_TEST_DEVICE" in os.environ: torch_device = os.environ["DIFFUSERS_TEST_DEVICE"] try: # try creating device to see if provided device is valid _ = torch.device(torch_device) except RuntimeError as e: raise RuntimeError( f"Unknown testing device specified by environment variable `DIFFUSERS_TEST_DEVICE`: {torch_device}" ) from e logger.info(f"torch_device overrode to {torch_device}") else: if torch.cuda.is_available(): torch_device = "cuda" elif torch.xpu.is_available(): torch_device = "xpu" else: torch_device = "cpu" is_torch_higher_equal_than_1_12 = version.parse( version.parse(torch.__version__).base_version ) >= version.parse("1.12") if is_torch_higher_equal_than_1_12: # Some builds of torch 1.12 don't have the mps backend registered. See #892 for more details mps_backend_registered = hasattr(torch.backends, "mps") torch_device = "mps" if (mps_backend_registered and torch.backends.mps.is_available()) else torch_device from diffusers.utils.torch_utils import get_torch_cuda_device_capability def torch_all_close(a, b, *args, **kwargs): if not is_torch_available(): raise ValueError("PyTorch needs to be installed to use this function.") if not torch.allclose(a, b, *args, **kwargs): assert False, f"Max diff is absolute {(a - b).abs().max()}. Diff tensor is {(a - b).abs()}." return True def assert_tensors_close( actual: "torch.Tensor", expected: "torch.Tensor", atol: float = 1e-5, rtol: float = 1e-5, msg: str = "", ) -> None: """ Assert that two tensors are close within tolerance. Uses the same formula as torch.allclose: |actual - expected| <= atol + rtol * |expected| Provides concise, actionable error messages without dumping full tensors. Args: actual: The actual tensor from the computation. expected: The expected tensor to compare against. atol: Absolute tolerance. rtol: Relative tolerance. msg: Optional message prefix for the assertion error. Raises: AssertionError: If tensors have different shapes or values exceed tolerance. Example: >>> assert_tensors_close(output, expected_output, atol=1e-5, rtol=1e-5, msg="Forward pass") """ if not is_torch_available(): raise ValueError("PyTorch needs to be installed to use this function.") if actual.shape != expected.shape: raise AssertionError(f"{msg} Shape mismatch: actual {actual.shape} vs expected {expected.shape}") if not torch.allclose(actual, expected, atol=atol, rtol=rtol): abs_diff = (actual - expected).abs() max_diff = abs_diff.max().item() flat_idx = abs_diff.argmax().item() max_idx = tuple(idx.item() for idx in torch.unravel_index(torch.tensor(flat_idx), actual.shape)) threshold = atol + rtol * expected.abs() mismatched = (abs_diff > threshold).sum().item() total = actual.numel() raise AssertionError( f"{msg}\n" f"Tensors not close! Mismatched elements: {mismatched}/{total} ({100 * mismatched / total:.1f}%)\n" f" Max diff: {max_diff:.6e} at index {max_idx}\n" f" Actual: {actual.flatten()[flat_idx].item():.6e}\n" f" Expected: {expected.flatten()[flat_idx].item():.6e}\n" f" atol: {atol:.6e}, rtol: {rtol:.6e}" ) def numpy_cosine_similarity_distance(a, b): similarity = np.dot(a, b) / (norm(a) * norm(b)) distance = 1.0 - similarity.mean() return distance def check_if_dicts_are_equal(dict1, dict2): dict1, dict2 = dict1.copy(), dict2.copy() for key, value in dict1.items(): if isinstance(value, set): dict1[key] = sorted(value) for key, value in dict2.items(): if isinstance(value, set): dict2[key] = sorted(value) for key in dict1: if key not in dict2: return False if dict1[key] != dict2[key]: return False for key in dict2: if key not in dict1: return False return True def print_tensor_test( tensor, limit_to_slices=None, max_torch_print=None, filename="test_corrections.txt", expected_tensor_name="expected_slice", ): if max_torch_print: torch.set_printoptions(threshold=10_000) test_name = os.environ.get("PYTEST_CURRENT_TEST") if not torch.is_tensor(tensor): tensor = torch.from_numpy(tensor) if limit_to_slices: tensor = tensor[0, -3:, -3:, -1] tensor_str = str(tensor.detach().cpu().flatten().to(torch.float32)).replace("\n", "") # format is usually: # expected_slice = np.array([-0.5713, -0.3018, -0.9814, 0.04663, -0.879, 0.76, -1.734, 0.1044, 1.161]) output_str = tensor_str.replace("tensor", f"{expected_tensor_name} = np.array") test_file, test_class, test_fn = test_name.split("::") test_fn = test_fn.split()[0] with open(filename, "a") as f: print("::".join([test_file, test_class, test_fn, output_str]), file=f) def get_tests_dir(append_path=None): """ Args: append_path: optional path to append to the tests dir path Return: The full path to the `tests` dir, so that the tests can be invoked from anywhere. Optionally `append_path` is joined after the `tests` dir the former is provided. """ # this function caller's __file__ caller__file__ = inspect.stack()[1][1] tests_dir = os.path.abspath(os.path.dirname(caller__file__)) while not tests_dir.endswith("tests"): tests_dir = os.path.dirname(tests_dir) if append_path: return Path(tests_dir, append_path).as_posix() else: return tests_dir # Taken from the following PR: # https://github.com/huggingface/accelerate/pull/1964 def str_to_bool(value) -> int: """ Converts a string representation of truth to `True` (1) or `False` (0). True values are `y`, `yes`, `t`, `true`, `on`, and `1`; False value are `n`, `no`, `f`, `false`, `off`, and `0`; """ value = value.lower() if value in ("y", "yes", "t", "true", "on", "1"): return 1 elif value in ("n", "no", "f", "false", "off", "0"): return 0 else: raise ValueError(f"invalid truth value {value}") def parse_flag_from_env(key, default=False): try: value = os.environ[key] except KeyError: # KEY isn't set, default to `default`. _value = default else: # KEY is set, convert it to True or False. try: _value = str_to_bool(value) except ValueError: # More values are supported, but let's keep the message simple. raise ValueError(f"If set, {key} must be yes or no.") return _value _run_slow_tests = parse_flag_from_env("RUN_SLOW", default=False) _run_nightly_tests = parse_flag_from_env("RUN_NIGHTLY", default=False) def floats_tensor(shape, scale=1.0, rng=None, name=None): """Creates a random float32 tensor""" if rng is None: rng = global_rng total_dims = 1 for dim in shape: total_dims *= dim values = [] for _ in range(total_dims): values.append(rng.random() * scale) return torch.tensor(data=values, dtype=torch.float).view(shape).contiguous() def slow(test_case): """ Decorator marking a test as slow. Slow tests are skipped by default. Set the RUN_SLOW environment variable to a truthy value to run them. """ return pytest.mark.skipif(not _run_slow_tests, reason="test is slow")(test_case) def nightly(test_case): """ Decorator marking a test that runs nightly in the diffusers CI. Slow tests are skipped by default. Set the RUN_NIGHTLY environment variable to a truthy value to run them. """ return pytest.mark.skipif(not _run_nightly_tests, reason="test is nightly")(test_case) def is_torch_compile(test_case): """ Decorator marking a test as a torch.compile test. These tests can be filtered using: pytest -m "not compile" to skip pytest -m compile to run only these tests """ return pytest.mark.compile(test_case) def is_single_file(test_case): """ Decorator marking a test as a single file loading test. These tests can be filtered using: pytest -m "not single_file" to skip pytest -m single_file to run only these tests """ return pytest.mark.single_file(test_case) def is_lora(test_case): """ Decorator marking a test as a LoRA test. These tests can be filtered using: pytest -m "not lora" to skip pytest -m lora to run only these tests """ return pytest.mark.lora(test_case) def is_ip_adapter(test_case): """ Decorator marking a test as an IP Adapter test. These tests can be filtered using: pytest -m "not ip_adapter" to skip pytest -m ip_adapter to run only these tests """ return pytest.mark.ip_adapter(test_case) def is_training(test_case): """ Decorator marking a test as a training test. These tests can be filtered using: pytest -m "not training" to skip pytest -m training to run only these tests """ return pytest.mark.training(test_case) def is_attention(test_case): """ Decorator marking a test as an attention test. These tests can be filtered using: pytest -m "not attention" to skip pytest -m attention to run only these tests """ return pytest.mark.attention(test_case) def is_memory(test_case): """ Decorator marking a test as a memory optimization test. These tests can be filtered using: pytest -m "not memory" to skip pytest -m memory to run only these tests """ return pytest.mark.memory(test_case) def is_cpu_offload(test_case): """ Decorator marking a test as a CPU offload test. These tests can be filtered using: pytest -m "not cpu_offload" to skip pytest -m cpu_offload to run only these tests """ return pytest.mark.cpu_offload(test_case) def is_group_offload(test_case): """ Decorator marking a test as a group offload test. These tests can be filtered using: pytest -m "not group_offload" to skip pytest -m group_offload to run only these tests """ return pytest.mark.group_offload(test_case) def is_quantization(test_case): """ Decorator marking a test as a quantization test. These tests can be filtered using: pytest -m "not quantization" to skip pytest -m quantization to run only these tests """ return pytest.mark.quantization(test_case) def is_bitsandbytes(test_case): """ Decorator marking a test as a BitsAndBytes quantization test. These tests can be filtered using: pytest -m "not bitsandbytes" to skip pytest -m bitsandbytes to run only these tests """ return pytest.mark.bitsandbytes(test_case) def is_quanto(test_case): """ Decorator marking a test as a Quanto quantization test. These tests can be filtered using: pytest -m "not quanto" to skip pytest -m quanto to run only these tests """ return pytest.mark.quanto(test_case) def is_torchao(test_case): """ Decorator marking a test as a TorchAO quantization test. These tests can be filtered using: pytest -m "not torchao" to skip pytest -m torchao to run only these tests """ return pytest.mark.torchao(test_case) def is_gguf(test_case): """ Decorator marking a test as a GGUF quantization test. These tests can be filtered using: pytest -m "not gguf" to skip pytest -m gguf to run only these tests """ return pytest.mark.gguf(test_case) def is_modelopt(test_case): """ Decorator marking a test as a NVIDIA ModelOpt quantization test. These tests can be filtered using: pytest -m "not modelopt" to skip pytest -m modelopt to run only these tests """ return pytest.mark.modelopt(test_case) def is_context_parallel(test_case): """ Decorator marking a test as a context parallel inference test. These tests can be filtered using: pytest -m "not context_parallel" to skip pytest -m context_parallel to run only these tests """ return pytest.mark.context_parallel(test_case) def is_cache(test_case): """ Decorator marking a test as a cache test. These tests can be filtered using: pytest -m "not cache" to skip pytest -m cache to run only these tests """ return pytest.mark.cache(test_case) def require_torch(test_case): """ Decorator marking a test that requires PyTorch. These tests are skipped when PyTorch isn't installed. """ return pytest.mark.skipif(not is_torch_available(), reason="test requires PyTorch")(test_case) def require_torch_2(test_case): """ Decorator marking a test that requires PyTorch 2. These tests are skipped when it isn't installed. """ return pytest.mark.skipif( not (is_torch_available() and is_torch_version(">=", "2.0.0")), reason="test requires PyTorch 2" )(test_case) def require_torch_version_greater_equal(torch_version): """Decorator marking a test that requires torch with a specific version or greater.""" def decorator(test_case): correct_torch_version = is_torch_available() and is_torch_version(">=", torch_version) return pytest.mark.skipif( not correct_torch_version, reason=f"test requires torch with the version greater than or equal to {torch_version}", )(test_case) return decorator def require_torch_version_greater(torch_version): """Decorator marking a test that requires torch with a specific version greater.""" def decorator(test_case): correct_torch_version = is_torch_available() and is_torch_version(">", torch_version) return pytest.mark.skipif( not correct_torch_version, reason=f"test requires torch with the version greater than {torch_version}" )(test_case) return decorator def require_torch_gpu(test_case): """Decorator marking a test that requires CUDA and PyTorch.""" return pytest.mark.skipif(torch_device != "cuda", reason="test requires PyTorch+CUDA")(test_case) def require_torch_cuda_compatibility(expected_compute_capability): def decorator(test_case): if torch.cuda.is_available(): current_compute_capability = get_torch_cuda_device_capability() return pytest.mark.skipif( float(current_compute_capability) != float(expected_compute_capability), reason="Test not supported for this compute capability.", )(test_case) return test_case return decorator # These decorators are for accelerator-specific behaviours that are not GPU-specific def require_torch_accelerator(test_case): """Decorator marking a test that requires an accelerator backend and PyTorch.""" return pytest.mark.skipif(torch_device == "cpu", reason="test requires accelerator+PyTorch")(test_case) def require_torch_multi_gpu(test_case): """ Decorator marking a test that requires a multi-GPU setup (in PyTorch). These tests are skipped on a machine without multiple GPUs. To run *only* the multi_gpu tests, assuming all test names contain multi_gpu: $ pytest -sv ./tests -k "multi_gpu" """ if not is_torch_available(): return pytest.mark.skip(reason="test requires PyTorch")(test_case) import torch return pytest.mark.skipif(torch.cuda.device_count() <= 1, reason="test requires multiple GPUs")(test_case) def require_torch_multi_accelerator(test_case): """ Decorator marking a test that requires a multi-accelerator setup (in PyTorch). These tests are skipped on a machine without multiple hardware accelerators. """ if not is_torch_available(): return pytest.mark.skip(reason="test requires PyTorch")(test_case) import torch return pytest.mark.skipif( not (torch.cuda.device_count() > 1 or torch.xpu.device_count() > 1), reason="test requires multiple hardware accelerators", )(test_case) def require_torch_accelerator_with_fp16(test_case): """Decorator marking a test that requires an accelerator with support for the FP16 data type.""" return pytest.mark.skipif( not _is_torch_fp16_available(torch_device), reason="test requires accelerator with fp16 support" )(test_case) def require_torch_accelerator_with_fp64(test_case): """Decorator marking a test that requires an accelerator with support for the FP64 data type.""" return pytest.mark.skipif( not _is_torch_fp64_available(torch_device), reason="test requires accelerator with fp64 support" )(test_case) def require_big_gpu_with_torch_cuda(test_case): """ Decorator marking a test that requires a bigger GPU (24GB) for execution. Some example pipelines: Flux, SD3, Cog, etc. """ if not is_torch_available(): return pytest.mark.skip(reason="test requires PyTorch")(test_case) import torch if not torch.cuda.is_available(): return pytest.mark.skip(reason="test requires PyTorch CUDA")(test_case) device_properties = torch.cuda.get_device_properties(0) total_memory = device_properties.total_memory / (1024**3) return pytest.mark.skipif( total_memory < BIG_GPU_MEMORY, reason=f"test requires a GPU with at least {BIG_GPU_MEMORY} GB memory" )(test_case) def require_big_accelerator(test_case): """ Decorator marking a test that requires a bigger hardware accelerator (24GB) for execution. Some example pipelines: Flux, SD3, Cog, etc. """ import pytest test_case = pytest.mark.big_accelerator(test_case) if not is_torch_available(): return pytest.mark.skip(reason="test requires PyTorch")(test_case) import torch if not (torch.cuda.is_available() or torch.xpu.is_available()): return pytest.mark.skip(reason="test requires PyTorch CUDA")(test_case) if torch.xpu.is_available(): device_properties = torch.xpu.get_device_properties(0) else: device_properties = torch.cuda.get_device_properties(0) total_memory = device_properties.total_memory / (1024**3) return pytest.mark.skipif( total_memory < BIG_GPU_MEMORY, reason=f"test requires a hardware accelerator with at least {BIG_GPU_MEMORY} GB memory", )(test_case) def require_torch_accelerator_with_training(test_case): """Decorator marking a test that requires an accelerator with support for training.""" return pytest.mark.skipif( not (is_torch_available() and backend_supports_training(torch_device)), reason="test requires accelerator with training support", )(test_case) def skip_mps(test_case): """Decorator marking a test to skip if torch_device is 'mps'""" return pytest.mark.skipif(torch_device == "mps", reason="test requires non 'mps' device")(test_case) def require_flax(test_case): """ Decorator marking a test that requires JAX & Flax. These tests are skipped when one / both are not installed """ return pytest.mark.skipif(not is_flax_available(), reason="test requires JAX & Flax")(test_case) def require_compel(test_case): """ Decorator marking a test that requires compel: https://github.com/damian0815/compel. These tests are skipped when the library is not installed. """ return pytest.mark.skipif(not is_compel_available(), reason="test requires compel")(test_case) def require_onnxruntime(test_case): """ Decorator marking a test that requires onnxruntime. These tests are skipped when onnxruntime isn't installed. """ return pytest.mark.skipif(not is_onnx_available(), reason="test requires onnxruntime")(test_case) def require_note_seq(test_case): """ Decorator marking a test that requires note_seq. These tests are skipped when note_seq isn't installed. """ return pytest.mark.skipif(not is_note_seq_available(), reason="test requires note_seq")(test_case) def require_accelerator(test_case): """ Decorator marking a test that requires a hardware accelerator backend. These tests are skipped when there are no hardware accelerator available. """ return pytest.mark.skipif(torch_device == "cpu", reason="test requires a hardware accelerator")(test_case) def require_torchsde(test_case): """ Decorator marking a test that requires torchsde. These tests are skipped when torchsde isn't installed. """ return pytest.mark.skipif(not is_torchsde_available(), reason="test requires torchsde")(test_case) def require_peft_backend(test_case): """ Decorator marking a test that requires PEFT backend, this would require some specific versions of PEFT and transformers. """ return pytest.mark.skipif(not USE_PEFT_BACKEND, reason="test requires PEFT backend")(test_case) def require_timm(test_case): """ Decorator marking a test that requires timm. These tests are skipped when timm isn't installed. """ return pytest.mark.skipif(not is_timm_available(), reason="test requires timm")(test_case) def require_bitsandbytes(test_case): """ Decorator marking a test that requires bitsandbytes. These tests are skipped when bitsandbytes isn't installed. """ return pytest.mark.skipif(not is_bitsandbytes_available(), reason="test requires bitsandbytes")(test_case) def require_quanto(test_case): """ Decorator marking a test that requires quanto. These tests are skipped when quanto isn't installed. """ return pytest.mark.skipif(not is_optimum_quanto_available(), reason="test requires quanto")(test_case) def require_accelerate(test_case): """ Decorator marking a test that requires accelerate. These tests are skipped when accelerate isn't installed. """ return pytest.mark.skipif(not is_accelerate_available(), reason="test requires accelerate")(test_case) def require_peft_version_greater(peft_version): """ Decorator marking a test that requires PEFT backend with a specific version, this would require some specific versions of PEFT and transformers. """ def decorator(test_case): correct_peft_version = is_peft_available() and version.parse( version.parse(importlib.metadata.version("peft")).base_version ) > version.parse(peft_version) return pytest.mark.skipif( not correct_peft_version, reason=f"test requires PEFT backend with the version greater than {peft_version}" )(test_case) return decorator def require_transformers_version_greater(transformers_version): """ Decorator marking a test that requires transformers with a specific version, this would require some specific versions of PEFT and transformers. """ def decorator(test_case): correct_transformers_version = is_transformers_available() and version.parse( version.parse(importlib.metadata.version("transformers")).base_version ) > version.parse(transformers_version) return pytest.mark.skipif( not correct_transformers_version, reason=f"test requires transformers with the version greater than {transformers_version}", )(test_case) return decorator def require_accelerate_version_greater(accelerate_version): def decorator(test_case): correct_accelerate_version = is_accelerate_available() and version.parse( version.parse(importlib.metadata.version("accelerate")).base_version ) > version.parse(accelerate_version) return pytest.mark.skipif( not correct_accelerate_version, reason=f"Test requires accelerate with the version greater than {accelerate_version}.", )(test_case) return decorator def require_bitsandbytes_version_greater(bnb_version): def decorator(test_case): correct_bnb_version = is_bitsandbytes_available() and version.parse( version.parse(importlib.metadata.version("bitsandbytes")).base_version ) > version.parse(bnb_version) return pytest.mark.skipif( not correct_bnb_version, reason=f"Test requires bitsandbytes with the version greater than {bnb_version}." )(test_case) return decorator def require_hf_hub_version_greater(hf_hub_version): def decorator(test_case): correct_hf_hub_version = version.parse( version.parse(importlib.metadata.version("huggingface_hub")).base_version ) > version.parse(hf_hub_version) return pytest.mark.skipif( not correct_hf_hub_version, reason=f"Test requires huggingface_hub with the version greater than {hf_hub_version}.", )(test_case) return decorator def require_gguf_version_greater_or_equal(gguf_version): def decorator(test_case): correct_gguf_version = is_gguf_available() and version.parse( version.parse(importlib.metadata.version("gguf")).base_version ) >= version.parse(gguf_version) return pytest.mark.skipif( not correct_gguf_version, reason=f"Test requires gguf with the version greater than {gguf_version}." )(test_case) return decorator def require_torchao_version_greater_or_equal(torchao_version): def decorator(test_case): correct_torchao_version = is_torchao_available() and version.parse( version.parse(importlib.metadata.version("torchao")).base_version ) >= version.parse(torchao_version) return pytest.mark.skipif( not correct_torchao_version, reason=f"Test requires torchao with version greater than {torchao_version}." )(test_case) return decorator def require_kernels_version_greater_or_equal(kernels_version): def decorator(test_case): correct_kernels_version = is_kernels_available() and version.parse( version.parse(importlib.metadata.version("kernels")).base_version ) >= version.parse(kernels_version) return pytest.mark.skipif( not correct_kernels_version, reason=f"Test requires kernels with version greater than {kernels_version}." )(test_case) return decorator def require_modelopt_version_greater_or_equal(modelopt_version): def decorator(test_case): return pytest.mark.skipif( not is_nvidia_modelopt_version(">=", modelopt_version), reason=f"Test requires modelopt with version greater than {modelopt_version}.", )(test_case) return decorator def deprecate_after_peft_backend(test_case): """ Decorator marking a test that will be skipped after PEFT backend """ return pytest.mark.skipif(USE_PEFT_BACKEND, reason="test skipped in favor of PEFT backend")(test_case) def get_python_version(): sys_info = sys.version_info major, minor = sys_info.major, sys_info.minor return major, minor def load_numpy(arry: str | np.ndarray, local_path: str | None = None) -> np.ndarray: if isinstance(arry, str): if local_path is not None: # local_path can be passed to correct images of tests return Path(local_path, arry.split("/")[-5], arry.split("/")[-2], arry.split("/")[-1]).as_posix() elif arry.startswith("http://") or arry.startswith("https://"): response = requests.get(arry, timeout=DIFFUSERS_REQUEST_TIMEOUT) response.raise_for_status() arry = np.load(BytesIO(response.content)) elif os.path.isfile(arry): arry = np.load(arry) else: raise ValueError( f"Incorrect path or url, URLs must start with `http://` or `https://`, and {arry} is not a valid path" ) elif isinstance(arry, np.ndarray): pass else: raise ValueError( "Incorrect format used for numpy ndarray. Should be an url linking to an image, a local path, or a" " ndarray." ) return arry def load_pt(url: str, map_location: str | None = None, weights_only: bool = True): response = requests.get(url, timeout=DIFFUSERS_REQUEST_TIMEOUT) response.raise_for_status() arry = torch.load(BytesIO(response.content), map_location=map_location, weights_only=weights_only) return arry def load_image(image: str | PIL.Image.Image) -> PIL.Image.Image: """ Loads `image` to a PIL Image. Args: image (`str` or `PIL.Image.Image`): The image to convert to the PIL Image format. Returns: `PIL.Image.Image`: A PIL Image. """ if isinstance(image, str): if image.startswith("http://") or image.startswith("https://"): image = PIL.Image.open(requests.get(image, stream=True, timeout=DIFFUSERS_REQUEST_TIMEOUT).raw) elif os.path.isfile(image): image = PIL.Image.open(image) else: raise ValueError( f"Incorrect path or url, URLs must start with `http://` or `https://`, and {image} is not a valid path" ) elif isinstance(image, PIL.Image.Image): image = image else: raise ValueError( "Incorrect format used for image. Should be an url linking to an image, a local path, or a PIL image." ) image = PIL.ImageOps.exif_transpose(image) image = image.convert("RGB") return image def preprocess_image(image: PIL.Image, batch_size: int): w, h = image.size w, h = (x - x % 8 for x in (w, h)) # resize to integer multiple of 8 image = image.resize((w, h), resample=PIL.Image.LANCZOS) image = np.array(image).astype(np.float32) / 255.0 image = np.vstack([image[None].transpose(0, 3, 1, 2)] * batch_size) image = torch.from_numpy(image) return 2.0 * image - 1.0 def export_to_gif(image: list[PIL.Image.Image], output_gif_path: str = None) -> str: if output_gif_path is None: output_gif_path = tempfile.NamedTemporaryFile(suffix=".gif").name image[0].save( output_gif_path, save_all=True, append_images=image[1:], optimize=False, duration=100, loop=0, ) return output_gif_path @contextmanager def buffered_writer(raw_f): f = io.BufferedWriter(raw_f) yield f f.flush() def export_to_ply(mesh, output_ply_path: str = None): """ Write a PLY file for a mesh. """ if output_ply_path is None: output_ply_path = tempfile.NamedTemporaryFile(suffix=".ply").name coords = mesh.verts.detach().cpu().numpy() faces = mesh.faces.cpu().numpy() rgb = np.stack([mesh.vertex_channels[x].detach().cpu().numpy() for x in "RGB"], axis=1) with buffered_writer(open(output_ply_path, "wb")) as f: f.write(b"ply\n") f.write(b"format binary_little_endian 1.0\n") f.write(bytes(f"element vertex {len(coords)}\n", "ascii")) f.write(b"property float x\n") f.write(b"property float y\n") f.write(b"property float z\n") if rgb is not None: f.write(b"property uchar red\n") f.write(b"property uchar green\n") f.write(b"property uchar blue\n") if faces is not None: f.write(bytes(f"element face {len(faces)}\n", "ascii")) f.write(b"property list uchar int vertex_index\n") f.write(b"end_header\n") if rgb is not None: rgb = (rgb * 255.499).round().astype(int) vertices = [ (*coord, *rgb) for coord, rgb in zip( coords.tolist(), rgb.tolist(), ) ] format = struct.Struct("<3f3B") for item in vertices: f.write(format.pack(*item)) else: format = struct.Struct("<3f") for vertex in coords.tolist(): f.write(format.pack(*vertex)) if faces is not None: format = struct.Struct("<B3I") for tri in faces.tolist(): f.write(format.pack(len(tri), *tri)) return output_ply_path def export_to_obj(mesh, output_obj_path: str = None): if output_obj_path is None: output_obj_path = tempfile.NamedTemporaryFile(suffix=".obj").name verts = mesh.verts.detach().cpu().numpy() faces = mesh.faces.cpu().numpy() vertex_colors = np.stack([mesh.vertex_channels[x].detach().cpu().numpy() for x in "RGB"], axis=1) vertices = [ "{} {} {} {} {} {}".format(*coord, *color) for coord, color in zip(verts.tolist(), vertex_colors.tolist()) ] faces = ["f {} {} {}".format(str(tri[0] + 1), str(tri[1] + 1), str(tri[2] + 1)) for tri in faces.tolist()] combined_data = ["v " + vertex for vertex in vertices] + faces with open(output_obj_path, "w") as f: f.writelines("\n".join(combined_data)) def export_to_video(video_frames: list[np.ndarray], output_video_path: str = None) -> str: if is_opencv_available(): import cv2 else: raise ImportError(BACKENDS_MAPPING["opencv"][1].format("export_to_video")) if output_video_path is None: output_video_path = tempfile.NamedTemporaryFile(suffix=".mp4").name fourcc = cv2.VideoWriter_fourcc(*"mp4v") h, w, c = video_frames[0].shape video_writer = cv2.VideoWriter(output_video_path, fourcc, fps=8, frameSize=(w, h)) for i in range(len(video_frames)): img = cv2.cvtColor(video_frames[i], cv2.COLOR_RGB2BGR) video_writer.write(img) return output_video_path def load_hf_numpy(path) -> np.ndarray: base_url = "https://huggingface.co/datasets/fusing/diffusers-testing/resolve/main" if not path.startswith("http://") and not path.startswith("https://"): path = os.path.join(base_url, urllib.parse.quote(path)) return load_numpy(path) # --- pytest conf functions --- # # to avoid multiple invocation from tests/conftest.py and examples/conftest.py - make sure it's called only once pytest_opt_registered = {} def pytest_addoption_shared(parser): """ This function is to be called from `conftest.py` via `pytest_addoption` wrapper that has to be defined there. It allows loading both `conftest.py` files at once without causing a failure due to adding the same `pytest` option. """ option = "--make-reports" if option not in pytest_opt_registered: parser.addoption( option, action="store", default=False, help="generate report files. The value of this option is used as a prefix to report names", ) pytest_opt_registered[option] = 1 def pytest_terminal_summary_main(tr, id): """ Generate multiple reports at the end of test suite run - each report goes into a dedicated file in the current directory. The report files are prefixed with the test suite name. This function emulates --duration and -rA pytest arguments. This function is to be called from `conftest.py` via `pytest_terminal_summary` wrapper that has to be defined there. Args: - tr: `terminalreporter` passed from `conftest.py` - id: unique id like `tests` or `examples` that will be incorporated into the final reports filenames - this is needed as some jobs have multiple runs of pytest, so we can't have them overwrite each other. NB: this functions taps into a private _pytest API and while unlikely, it could break should pytest do internal changes - also it calls default internal methods of terminalreporter which can be hijacked by various `pytest-` plugins and interfere. """ from _pytest.config import create_terminal_writer if not len(id): id = "tests" config = tr.config orig_writer = config.get_terminal_writer() orig_tbstyle = config.option.tbstyle orig_reportchars = tr.reportchars dir = "reports" Path(dir).mkdir(parents=True, exist_ok=True) report_files = { k: f"{dir}/{id}_{k}.txt" for k in [ "durations", "errors", "failures_long", "failures_short", "failures_line", "passes", "stats", "summary_short", "warnings", ] } # custom durations report # note: there is no need to call pytest --durations=XX to get this separate report # adapted from https://github.com/pytest-dev/pytest/blob/897f151e/src/_pytest/runner.py#L66 dlist = [] for replist in tr.stats.values(): for rep in replist: if hasattr(rep, "duration"): dlist.append(rep) if dlist: dlist.sort(key=lambda x: x.duration, reverse=True) with open(report_files["durations"], "w") as f: durations_min = 0.05 # sec f.write("slowest durations\n") for i, rep in enumerate(dlist): if rep.duration < durations_min: f.write(f"{len(dlist) - i} durations < {durations_min} secs were omitted") break f.write(f"{rep.duration:02.2f}s {rep.when:<8} {rep.nodeid}\n") def summary_failures_short(tr): # expecting that the reports were --tb=long (default) so we chop them off here to the last frame reports = tr.getreports("failed") if not reports: return tr.write_sep("=", "FAILURES SHORT STACK") for rep in reports: msg = tr._getfailureheadline(rep) tr.write_sep("_", msg, red=True, bold=True) # chop off the optional leading extra frames, leaving only the last one longrepr = re.sub(r".*_ _ _ (_ ){10,}_ _ ", "", rep.longreprtext, 0, re.M | re.S) tr._tw.line(longrepr) # note: not printing out any rep.sections to keep the report short # use ready-made report funcs, we are just hijacking the filehandle to log to a dedicated file each # adapted from https://github.com/pytest-dev/pytest/blob/897f151e/src/_pytest/terminal.py#L814 # note: some pytest plugins may interfere by hijacking the default `terminalreporter` (e.g. # pytest-instafail does that) # report failures with line/short/long styles config.option.tbstyle = "auto" # full tb with open(report_files["failures_long"], "w") as f: tr._tw = create_terminal_writer(config, f) tr.summary_failures() # config.option.tbstyle = "short" # short tb with open(report_files["failures_short"], "w") as f: tr._tw = create_terminal_writer(config, f) summary_failures_short(tr) config.option.tbstyle = "line" # one line per error with open(report_files["failures_line"], "w") as f: tr._tw = create_terminal_writer(config, f) tr.summary_failures() with open(report_files["errors"], "w") as f: tr._tw = create_terminal_writer(config, f) tr.summary_errors() with open(report_files["warnings"], "w") as f: tr._tw = create_terminal_writer(config, f) tr.summary_warnings() # normal warnings tr.summary_warnings() # final warnings tr.reportchars = "wPpsxXEf" # emulate -rA (used in summary_passes() and short_test_summary()) with open(report_files["passes"], "w") as f: tr._tw = create_terminal_writer(config, f) tr.summary_passes() with open(report_files["summary_short"], "w") as f: tr._tw = create_terminal_writer(config, f) tr.short_test_summary() with open(report_files["stats"], "w") as f: tr._tw = create_terminal_writer(config, f) tr.summary_stats() # restore: tr._tw = orig_writer tr.reportchars = orig_reportchars config.option.tbstyle = orig_tbstyle # Adapted from https://github.com/huggingface/transformers/blob/000e52aec8850d3fe2f360adc6fd256e5b47fe4c/src/transformers..testing_utils.py#L1905 def is_flaky(max_attempts: int = 5, wait_before_retry: float | None = None, description: str | None = None): """ To decorate flaky tests (methods or entire classes). They will be retried on failures. Args: max_attempts (`int`, *optional*, defaults to 5): The maximum number of attempts to retry the flaky test. wait_before_retry (`float`, *optional*): If provided, will wait that number of seconds before retrying the test. description (`str`, *optional*): A string to describe the situation (what / where / why is flaky, link to GH issue/PR comments, errors, etc.) """ def decorator(obj): # If decorating a class, wrap each test method on it if inspect.isclass(obj): for attr_name, attr_value in list(obj.__dict__.items()): if callable(attr_value) and attr_name.startswith("test"): # recursively decorate the method setattr(obj, attr_name, decorator(attr_value)) return obj # Otherwise we're decorating a single test function / method @functools.wraps(obj) def wrapper(*args, **kwargs): retry_count = 1 while retry_count < max_attempts: try: return obj(*args, **kwargs) except Exception as err: msg = ( f"[FLAKY] {description or obj.__name__!r} " f"failed on attempt {retry_count}/{max_attempts}: {err}" ) print(msg, file=sys.stderr) if wait_before_retry is not None: time.sleep(wait_before_retry) retry_count += 1 return obj(*args, **kwargs) return wrapper return decorator # Taken from: https://github.com/huggingface/transformers/blob/3658488ff77ff8d45101293e749263acf437f4d5/src/transformers..testing_utils.py#L1787 def run_test_in_subprocess(test_case, target_func, inputs=None, timeout=None): """ To run a test in a subprocess. In particular, this can avoid (GPU) memory issue. Args: test_case: The test case object that will run `target_func`. target_func (`Callable`): The function implementing the actual testing logic. inputs (`dict`, *optional*, defaults to `None`): The inputs that will be passed to `target_func` through an (input) queue. timeout (`int`, *optional*, defaults to `None`): The timeout (in seconds) that will be passed to the input and output queues. If not specified, the env. variable `PYTEST_TIMEOUT` will be checked. If still `None`, its value will be set to `600`. """ if timeout is None: timeout = int(os.environ.get("PYTEST_TIMEOUT", 600)) start_methohd = "spawn" ctx = multiprocessing.get_context(start_methohd) input_queue = ctx.Queue(1) output_queue = ctx.JoinableQueue(1) # We can't send test case objects to the child, otherwise we get issues regarding pickle. input_queue.put(inputs, timeout=timeout) process = ctx.Process(target=target_func, args=(input_queue, output_queue, timeout)) process.start() # Kill the child process if we can't get outputs from it in time: otherwise, the hanging subprocess prevents # the test to exit properly. try: results = output_queue.get(timeout=timeout) output_queue.task_done() except Exception as e: process.terminate() test_case.fail(e) process.join(timeout=timeout) if results["error"] is not None: test_case.fail(f"{results['error']}") class CaptureLogger: """ Args: Context manager to capture `logging` streams logger: 'logging` logger object Returns: The captured output is available via `self.out` Example: ```python >>> from diffusers import logging >>> from diffusers..testing_utils import CaptureLogger >>> msg = "Testing 1, 2, 3" >>> logging.set_verbosity_info() >>> logger = logging.get_logger("diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.py") >>> with CaptureLogger(logger) as cl: ... logger.info(msg) >>> assert cl.out, msg + "\n" ``` """ def __init__(self, logger): self.logger = logger self.io = StringIO() self.sh = logging.StreamHandler(self.io) self.out = "" def __enter__(self): self.logger.addHandler(self.sh) return self def __exit__(self, *exc): self.logger.removeHandler(self.sh) self.out = self.io.getvalue() def __repr__(self): return f"captured: {self.out}\n" def enable_full_determinism(): """ Helper function for reproducible behavior during distributed training. See - https://pytorch.org/docs/stable/notes/randomness.html for pytorch """ # Enable PyTorch deterministic mode. This potentially requires either the environment # variable 'CUDA_LAUNCH_BLOCKING' or 'CUBLAS_WORKSPACE_CONFIG' to be set, # depending on the CUDA version, so we set them both here os.environ["CUDA_LAUNCH_BLOCKING"] = "1" # Use larger workspace size for PyTorch 2.10+ to avoid CUBLAS_STATUS_NOT_INITIALIZED errors # (catches 2.11 dev versions which report as >= 2.10) if is_torch_version(">=", "2.10"): os.environ["CUBLAS_WORKSPACE_CONFIG"] = ":4096:8" else: os.environ["CUBLAS_WORKSPACE_CONFIG"] = ":16:8" torch.use_deterministic_algorithms(True) # Enable CUDNN deterministic mode torch.backends.cudnn.deterministic = True torch.backends.cudnn.benchmark = False torch.backends.cuda.matmul.allow_tf32 = False def disable_full_determinism(): os.environ["CUDA_LAUNCH_BLOCKING"] = "0" os.environ["CUBLAS_WORKSPACE_CONFIG"] = "" torch.use_deterministic_algorithms(False) # Utils for custom and alternative accelerator devices def _is_torch_fp16_available(device): if not is_torch_available(): return False import torch device = torch.device(device) try: x = torch.zeros((2, 2), dtype=torch.float16).to(device) _ = torch.mul(x, x) return True except Exception as e: if device.type == "cuda": raise ValueError( f"You have passed a device of type 'cuda' which should work with 'fp16', but 'cuda' does not seem to be correctly installed on your machine: {e}" ) return False def _is_torch_fp64_available(device): if not is_torch_available(): return False import torch device = torch.device(device) try: x = torch.zeros((2, 2), dtype=torch.float64).to(device) _ = torch.mul(x, x) return True except Exception as e: if device.type == "cuda": raise ValueError( f"You have passed a device of type 'cuda' which should work with 'fp64', but 'cuda' does not seem to be correctly installed on your machine: {e}" ) return False # Guard these lookups for when Torch is not used - alternative accelerator support is for PyTorch if is_torch_available(): # Behaviour flags BACKEND_SUPPORTS_TRAINING = {"cuda": True, "xpu": True, "cpu": True, "mps": False, "default": True} # Function definitions BACKEND_EMPTY_CACHE = { "cuda": torch.cuda.empty_cache, "xpu": torch.xpu.empty_cache, "cpu": None, "mps": torch.mps.empty_cache, "default": None, } BACKEND_DEVICE_COUNT = { "cuda": torch.cuda.device_count, "xpu": torch.xpu.device_count, "cpu": lambda: 0, "mps": lambda: 0, "default": 0, } BACKEND_MANUAL_SEED = { "cuda": torch.cuda.manual_seed, "xpu": torch.xpu.manual_seed, "cpu": torch.manual_seed, "mps": torch.mps.manual_seed, "default": torch.manual_seed, } BACKEND_RESET_PEAK_MEMORY_STATS = { "cuda": torch.cuda.reset_peak_memory_stats, "xpu": getattr(torch.xpu, "reset_peak_memory_stats", None), "cpu": None, "mps": None, "default": None, } BACKEND_RESET_MAX_MEMORY_ALLOCATED = { "cuda": torch.cuda.reset_max_memory_allocated, "xpu": getattr(torch.xpu, "reset_peak_memory_stats", None), "cpu": None, "mps": None, "default": None, } BACKEND_MAX_MEMORY_ALLOCATED = { "cuda": torch.cuda.max_memory_allocated, "xpu": getattr(torch.xpu, "max_memory_allocated", None), "cpu": 0, "mps": 0, "default": 0, } BACKEND_SYNCHRONIZE = { "cuda": torch.cuda.synchronize, "xpu": getattr(torch.xpu, "synchronize", None), "cpu": None, "mps": None, "default": None, } # This dispatches a defined function according to the accelerator from the function definitions. def _device_agnostic_dispatch(device: str, dispatch_table: dict[str, Callable], *args, **kwargs): if device not in dispatch_table: return dispatch_table["default"](*args, **kwargs) fn = dispatch_table[device] # Some device agnostic functions return values. Need to guard against 'None' instead at # user level if not callable(fn): return fn return fn(*args, **kwargs) # These are callables which automatically dispatch the function specific to the accelerator def backend_manual_seed(device: str, seed: int): return _device_agnostic_dispatch(device, BACKEND_MANUAL_SEED, seed) def backend_synchronize(device: str): return _device_agnostic_dispatch(device, BACKEND_SYNCHRONIZE) def backend_empty_cache(device: str): return _device_agnostic_dispatch(device, BACKEND_EMPTY_CACHE) def backend_device_count(device: str): return _device_agnostic_dispatch(device, BACKEND_DEVICE_COUNT) def backend_reset_peak_memory_stats(device: str): return _device_agnostic_dispatch(device, BACKEND_RESET_PEAK_MEMORY_STATS) def backend_reset_max_memory_allocated(device: str): return _device_agnostic_dispatch(device, BACKEND_RESET_MAX_MEMORY_ALLOCATED) def backend_max_memory_allocated(device: str): return _device_agnostic_dispatch(device, BACKEND_MAX_MEMORY_ALLOCATED) # These are callables which return boolean behaviour flags and can be used to specify some # device agnostic alternative where the feature is unsupported. def backend_supports_training(device: str): if not is_torch_available(): return False if device not in BACKEND_SUPPORTS_TRAINING: device = "default" return BACKEND_SUPPORTS_TRAINING[device] # Guard for when Torch is not available if is_torch_available(): # Update device function dict mapping def update_mapping_from_spec(device_fn_dict: dict[str, Callable], attribute_name: str): try: # Try to import the function directly spec_fn = getattr(device_spec_module, attribute_name) device_fn_dict[torch_device] = spec_fn except AttributeError as e: # If the function doesn't exist, and there is no default, throw an error if "default" not in device_fn_dict: raise AttributeError( f"`{attribute_name}` not found in '{device_spec_path}' and no default fallback function found." ) from e if "DIFFUSERS_TEST_DEVICE_SPEC" in os.environ: device_spec_path = os.environ["DIFFUSERS_TEST_DEVICE_SPEC"] if not Path(device_spec_path).is_file(): raise ValueError(f"Specified path to device specification file is not found. Received {device_spec_path}") try: import_name = device_spec_path[: device_spec_path.index(".py")] except ValueError as e: raise ValueError(f"Provided device spec file is not a Python file! Received {device_spec_path}") from e device_spec_module = importlib.import_module(import_name) try: device_name = device_spec_module.DEVICE_NAME except AttributeError: raise AttributeError("Device spec file did not contain `DEVICE_NAME`") if "DIFFUSERS_TEST_DEVICE" in os.environ and torch_device != device_name: msg = f"Mismatch between environment variable `DIFFUSERS_TEST_DEVICE` '{torch_device}' and device found in spec '{device_name}'\n" msg += "Either unset `DIFFUSERS_TEST_DEVICE` or ensure it matches device spec name." raise ValueError(msg) torch_device = device_name # Add one entry here for each `BACKEND_*` dictionary. update_mapping_from_spec(BACKEND_MANUAL_SEED, "MANUAL_SEED_FN") update_mapping_from_spec(BACKEND_EMPTY_CACHE, "EMPTY_CACHE_FN") update_mapping_from_spec(BACKEND_DEVICE_COUNT, "DEVICE_COUNT_FN") update_mapping_from_spec(BACKEND_SUPPORTS_TRAINING, "SUPPORTS_TRAINING") update_mapping_from_spec(BACKEND_RESET_PEAK_MEMORY_STATS, "RESET_PEAK_MEMORY_STATS_FN") update_mapping_from_spec(BACKEND_RESET_MAX_MEMORY_ALLOCATED, "RESET_MAX_MEMORY_ALLOCATED_FN") update_mapping_from_spec(BACKEND_MAX_MEMORY_ALLOCATED, "MAX_MEMORY_ALLOCATED_FN") # Modified from https://github.com/huggingface/transformers/blob/cdfb018d0300fef3b07d9220f3efe9c2a9974662/src/transformers..testing_utils.py#L3090 # Type definition of key used in `Expectations` class. DeviceProperties = Tuple[Union[str, None], Union[int, None]] @functools.lru_cache def get_device_properties() -> DeviceProperties: """ Get environment device properties. """ if IS_CUDA_SYSTEM or IS_ROCM_SYSTEM: import torch major, _ = torch.cuda.get_device_capability() if IS_ROCM_SYSTEM: return ("rocm", major) else: return ("cuda", major) elif IS_XPU_SYSTEM: import torch # To get more info of the architecture meaning and bit allocation, refer to https://github.com/intel/llvm/blob/sycl/sycl/include/sycl/ext/oneapi/experimental/device_architecture.def arch = torch.xpu.get_device_capability()["architecture"] gen_mask = 0x000000FF00000000 gen = (arch & gen_mask) >> 32 return ("xpu", gen) else: return (torch_device, None) if TYPE_CHECKING: DevicePropertiesUserDict = UserDict[DeviceProperties, Any] else: DevicePropertiesUserDict = UserDict if is_torch_available(): from diffusers.hooks._common import _GO_LC_SUPPORTED_PYTORCH_LAYERS from diffusers.hooks.group_offloading import ( _GROUP_ID_LAZY_LEAF, _compute_group_hash, _find_parent_module_in_module_dict, _gather_buffers_with_no_group_offloading_parent, _gather_parameters_with_no_group_offloading_parent, ) def _get_expected_safetensors_files( module: torch.nn.Module, offload_to_disk_path: str, offload_type: str, num_blocks_per_group: int | None = None, block_modules: list[str] | None = None, module_prefix: str = "", ) -> set[str]: expected_files = set() def get_hashed_filename(group_id: str) -> str: short_hash = _compute_group_hash(group_id) return os.path.join(offload_to_disk_path, f"group_{short_hash}.safetensors") if offload_type == "block_level": if num_blocks_per_group is None: raise ValueError("num_blocks_per_group must be provided for 'block_level' offloading.") block_modules_set = set(block_modules) if block_modules is not None else set() modules_with_group_offloading = set() unmatched_modules = [] for name, submodule in module.named_children(): if name in block_modules_set: new_prefix = f"{module_prefix}{name}." if module_prefix else f"{name}." submodule_files = _get_expected_safetensors_files( submodule, offload_to_disk_path, offload_type, num_blocks_per_group, block_modules, new_prefix ) expected_files.update(submodule_files) modules_with_group_offloading.add(name) elif isinstance(submodule, (torch.nn.ModuleList, torch.nn.Sequential)): for i in range(0, len(submodule), num_blocks_per_group): current_modules = submodule[i : i + num_blocks_per_group] if not current_modules: continue group_id = f"{module_prefix}{name}_{i}_{i + len(current_modules) - 1}" expected_files.add(get_hashed_filename(group_id)) for j in range(i, i + len(current_modules)): modules_with_group_offloading.add(f"{name}.{j}") else: unmatched_modules.append(submodule) parameters = _gather_parameters_with_no_group_offloading_parent(module, modules_with_group_offloading) buffers = _gather_buffers_with_no_group_offloading_parent(module, modules_with_group_offloading) if len(unmatched_modules) > 0 or len(parameters) > 0 or len(buffers) > 0: expected_files.add(get_hashed_filename(f"{module_prefix}{module.__class__.__name__}_unmatched_group")) elif offload_type == "leaf_level": # Handle leaf-level module groups for name, submodule in module.named_modules(): if isinstance(submodule, _GO_LC_SUPPORTED_PYTORCH_LAYERS): # These groups will always have parameters, so a file is expected expected_files.add(get_hashed_filename(name)) # Handle groups for non-leaf parameters/buffers modules_with_group_offloading = { name for name, sm in module.named_modules() if isinstance(sm, _GO_LC_SUPPORTED_PYTORCH_LAYERS) } parameters = _gather_parameters_with_no_group_offloading_parent(module, modules_with_group_offloading) buffers = _gather_buffers_with_no_group_offloading_parent(module, modules_with_group_offloading) all_orphans = parameters + buffers if all_orphans: parent_to_tensors = {} module_dict = dict(module.named_modules()) for tensor_name, _ in all_orphans: parent_name = _find_parent_module_in_module_dict(tensor_name, module_dict) if parent_name not in parent_to_tensors: parent_to_tensors[parent_name] = [] parent_to_tensors[parent_name].append(tensor_name) for parent_name in parent_to_tensors: # A file is expected for each parent that gathers orphaned tensors expected_files.add(get_hashed_filename(parent_name)) expected_files.add(get_hashed_filename(_GROUP_ID_LAZY_LEAF)) else: raise ValueError(f"Unsupported offload_type: {offload_type}") return expected_files def _check_safetensors_serialization( module: torch.nn.Module, offload_to_disk_path: str, offload_type: str, num_blocks_per_group: int | None = None, block_modules: list[str] | None = None, ) -> bool: if not os.path.isdir(offload_to_disk_path): return False, None, None expected_files = _get_expected_safetensors_files( module, offload_to_disk_path, offload_type, num_blocks_per_group, block_modules ) actual_files = set(glob.glob(os.path.join(offload_to_disk_path, "*.safetensors"))) missing_files = expected_files - actual_files extra_files = actual_files - expected_files is_correct = not missing_files and not extra_files return is_correct, extra_files, missing_files class Expectations(DevicePropertiesUserDict): def get_expectation(self) -> Any: """ Find best matching expectation based on environment device properties. """ return self.find_expectation(get_device_properties()) @staticmethod def is_default(key: DeviceProperties) -> bool: return all(p is None for p in key) @staticmethod def score(key: DeviceProperties, other: DeviceProperties) -> int: """ Returns score indicating how similar two instances of the `Properties` tuple are. Points are calculated using bits, but documented as int. Rules are as follows: * Matching `type` gives 8 points. * Semi-matching `type`, for example cuda and rocm, gives 4 points. * Matching `major` (compute capability major version) gives 2 points. * Default expectation (if present) gives 1 points. """ (device_type, major) = key (other_device_type, other_major) = other score = 0b0 if device_type == other_device_type: score |= 0b1000 elif device_type in ["cuda", "rocm"] and other_device_type in ["cuda", "rocm"]: score |= 0b100 if major == other_major and other_major is not None: score |= 0b10 if Expectations.is_default(other): score |= 0b1 return int(score) def find_expectation(self, key: DeviceProperties = (None, None)) -> Any: """ Find best matching expectation based on provided device properties. """ (result_key, result) = max(self.data.items(), key=lambda x: Expectations.score(key, x[0])) if Expectations.score(key, result_key) == 0: raise ValueError(f"No matching expectation found for {key}") return result def __repr__(self): return f"{self.data}"
{ "repo_id": "huggingface/diffusers", "file_path": "tests/testing_utils.py", "license": "Apache License 2.0", "lines": 1405, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
test
huggingface/diffusers:tests/pipelines/qwenimage/test_qwenimage_controlnet.py
# Copyright 2025 The HuggingFace Team. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest import numpy as np import torch from transformers import Qwen2_5_VLConfig, Qwen2_5_VLForConditionalGeneration, Qwen2Tokenizer from diffusers import ( AutoencoderKLQwenImage, FlowMatchEulerDiscreteScheduler, QwenImageControlNetModel, QwenImageControlNetPipeline, QwenImageMultiControlNetModel, QwenImageTransformer2DModel, ) from diffusers.utils.testing_utils import enable_full_determinism, torch_device from diffusers.utils.torch_utils import randn_tensor from ..pipeline_params import TEXT_TO_IMAGE_PARAMS from ..test_pipelines_common import PipelineTesterMixin, to_np enable_full_determinism() class QwenControlNetPipelineFastTests(PipelineTesterMixin, unittest.TestCase): pipeline_class = QwenImageControlNetPipeline params = (TEXT_TO_IMAGE_PARAMS | frozenset(["control_image", "controlnet_conditioning_scale"])) - { "cross_attention_kwargs" } batch_params = frozenset(["prompt", "negative_prompt", "control_image"]) image_params = frozenset(["control_image"]) image_latents_params = frozenset(["latents"]) required_optional_params = frozenset( [ "num_inference_steps", "generator", "latents", "control_image", "controlnet_conditioning_scale", "return_dict", "callback_on_step_end", "callback_on_step_end_tensor_inputs", ] ) supports_dduf = False test_xformers_attention = False test_layerwise_casting = True test_group_offloading = True def get_dummy_components(self): torch.manual_seed(0) transformer = QwenImageTransformer2DModel( patch_size=2, in_channels=16, out_channels=4, num_layers=2, attention_head_dim=16, num_attention_heads=3, joint_attention_dim=16, guidance_embeds=False, axes_dims_rope=(8, 4, 4), ) torch.manual_seed(0) controlnet = QwenImageControlNetModel( patch_size=2, in_channels=16, out_channels=4, num_layers=2, attention_head_dim=16, num_attention_heads=3, joint_attention_dim=16, axes_dims_rope=(8, 4, 4), ) torch.manual_seed(0) z_dim = 4 vae = AutoencoderKLQwenImage( base_dim=z_dim * 6, z_dim=z_dim, dim_mult=[1, 2, 4], num_res_blocks=1, temperal_downsample=[False, True], latents_mean=[0.0] * z_dim, latents_std=[1.0] * z_dim, ) torch.manual_seed(0) scheduler = FlowMatchEulerDiscreteScheduler() torch.manual_seed(0) config = Qwen2_5_VLConfig( text_config={ "hidden_size": 16, "intermediate_size": 16, "num_hidden_layers": 2, "num_attention_heads": 2, "num_key_value_heads": 2, "rope_scaling": { "mrope_section": [1, 1, 2], "rope_type": "default", "type": "default", }, "rope_theta": 1_000_000.0, }, vision_config={ "depth": 2, "hidden_size": 16, "intermediate_size": 16, "num_heads": 2, "out_hidden_size": 16, }, hidden_size=16, vocab_size=152064, vision_end_token_id=151653, vision_start_token_id=151652, vision_token_id=151654, ) text_encoder = Qwen2_5_VLForConditionalGeneration(config) tokenizer = Qwen2Tokenizer.from_pretrained("hf-internal-testing/tiny-random-Qwen2VLForConditionalGeneration") components = { "transformer": transformer, "vae": vae, "scheduler": scheduler, "text_encoder": text_encoder, "tokenizer": tokenizer, "controlnet": controlnet, } return components def get_dummy_inputs(self, device, seed=0): if str(device).startswith("mps"): generator = torch.manual_seed(seed) else: generator = torch.Generator(device=device).manual_seed(seed) control_image = randn_tensor( (1, 3, 32, 32), generator=generator, device=torch.device(device), dtype=torch.float32, ) inputs = { "prompt": "dance monkey", "negative_prompt": "bad quality", "generator": generator, "num_inference_steps": 2, "guidance_scale": 3.0, "true_cfg_scale": 1.0, "height": 32, "width": 32, "max_sequence_length": 16, "control_image": control_image, "controlnet_conditioning_scale": 0.5, "output_type": "pt", } return inputs def test_qwen_controlnet(self): device = "cpu" components = self.get_dummy_components() pipe = self.pipeline_class(**components) pipe.to(device) pipe.set_progress_bar_config(disable=None) inputs = self.get_dummy_inputs(device) image = pipe(**inputs).images generated_image = image[0] self.assertEqual(generated_image.shape, (3, 32, 32)) # Expected slice from the generated image expected_slice = torch.tensor( [ 0.4726, 0.5549, 0.6324, 0.6548, 0.4968, 0.4639, 0.4749, 0.4898, 0.4725, 0.4645, 0.4435, 0.3339, 0.3400, 0.4630, 0.3879, 0.4406, ] ) generated_slice = generated_image.flatten() generated_slice = torch.cat([generated_slice[:8], generated_slice[-8:]]) self.assertTrue(torch.allclose(generated_slice, expected_slice, atol=5e-3)) def test_qwen_controlnet_multicondition(self): device = "cpu" components = self.get_dummy_components() components["controlnet"] = QwenImageMultiControlNetModel([components["controlnet"]]) pipe = self.pipeline_class(**components) pipe.to(device) pipe.set_progress_bar_config(disable=None) inputs = self.get_dummy_inputs(device) control_image = inputs["control_image"] inputs["control_image"] = [control_image, control_image] inputs["controlnet_conditioning_scale"] = [0.5, 0.5] image = pipe(**inputs).images generated_image = image[0] self.assertEqual(generated_image.shape, (3, 32, 32)) # Expected slice from the generated image expected_slice = torch.tensor( [ 0.6239, 0.6642, 0.5768, 0.6039, 0.5270, 0.5070, 0.5006, 0.5271, 0.4506, 0.3085, 0.3435, 0.5152, 0.5096, 0.5422, 0.4286, 0.5752, ] ) generated_slice = generated_image.flatten() generated_slice = torch.cat([generated_slice[:8], generated_slice[-8:]]) self.assertTrue(torch.allclose(generated_slice, expected_slice, atol=5e-3)) def test_attention_slicing_forward_pass( self, test_max_difference=True, test_mean_pixel_difference=True, expected_max_diff=1e-3 ): if not self.test_attention_slicing: return components = self.get_dummy_components() pipe = self.pipeline_class(**components) for component in pipe.components.values(): if hasattr(component, "set_default_attn_processor"): component.set_default_attn_processor() pipe.to(torch_device) pipe.set_progress_bar_config(disable=None) generator_device = "cpu" inputs = self.get_dummy_inputs(generator_device) output_without_slicing = pipe(**inputs)[0] pipe.enable_attention_slicing(slice_size=1) inputs = self.get_dummy_inputs(generator_device) output_with_slicing1 = pipe(**inputs)[0] pipe.enable_attention_slicing(slice_size=2) inputs = self.get_dummy_inputs(generator_device) output_with_slicing2 = pipe(**inputs)[0] if test_max_difference: max_diff1 = np.abs(to_np(output_with_slicing1) - to_np(output_without_slicing)).max() max_diff2 = np.abs(to_np(output_with_slicing2) - to_np(output_without_slicing)).max() self.assertLess( max(max_diff1, max_diff2), expected_max_diff, "Attention slicing should not affect the inference results", ) def test_inference_batch_single_identical(self): self._test_inference_batch_single_identical(batch_size=3, expected_max_diff=1e-1) def test_vae_tiling(self, expected_diff_max: float = 0.2): generator_device = "cpu" components = self.get_dummy_components() pipe = self.pipeline_class(**components) pipe.to("cpu") pipe.set_progress_bar_config(disable=None) # Without tiling inputs = self.get_dummy_inputs(generator_device) inputs["height"] = inputs["width"] = 128 inputs["control_image"] = randn_tensor( (1, 3, 128, 128), generator=inputs["generator"], device=torch.device(generator_device), dtype=torch.float32, ) output_without_tiling = pipe(**inputs)[0] # With tiling pipe.vae.enable_tiling( tile_sample_min_height=96, tile_sample_min_width=96, tile_sample_stride_height=64, tile_sample_stride_width=64, ) inputs = self.get_dummy_inputs(generator_device) inputs["height"] = inputs["width"] = 128 inputs["control_image"] = randn_tensor( (1, 3, 128, 128), generator=inputs["generator"], device=torch.device(generator_device), dtype=torch.float32, ) output_with_tiling = pipe(**inputs)[0] self.assertLess( (to_np(output_without_tiling) - to_np(output_with_tiling)).max(), expected_diff_max, "VAE tiling should not affect the inference results", )
{ "repo_id": "huggingface/diffusers", "file_path": "tests/pipelines/qwenimage/test_qwenimage_controlnet.py", "license": "Apache License 2.0", "lines": 296, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
test
huggingface/diffusers:src/diffusers/models/controlnets/controlnet_qwenimage.py
# Copyright 2025 Black Forest Labs, The HuggingFace Team and The InstantX Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from dataclasses import dataclass from typing import Any import torch import torch.nn as nn from ...configuration_utils import ConfigMixin, register_to_config from ...loaders import FromOriginalModelMixin, PeftAdapterMixin from ...utils import ( BaseOutput, apply_lora_scale, deprecate, logging, ) from ..attention import AttentionMixin from ..cache_utils import CacheMixin from ..controlnets.controlnet import zero_module from ..modeling_outputs import Transformer2DModelOutput from ..modeling_utils import ModelMixin from ..transformers.transformer_qwenimage import ( QwenEmbedRope, QwenImageTransformerBlock, QwenTimestepProjEmbeddings, RMSNorm, compute_text_seq_len_from_mask, ) logger = logging.get_logger(__name__) # pylint: disable=invalid-name @dataclass class QwenImageControlNetOutput(BaseOutput): controlnet_block_samples: tuple[torch.Tensor] class QwenImageControlNetModel( ModelMixin, AttentionMixin, ConfigMixin, PeftAdapterMixin, FromOriginalModelMixin, CacheMixin ): _supports_gradient_checkpointing = True @register_to_config def __init__( self, patch_size: int = 2, in_channels: int = 64, out_channels: int | None = 16, num_layers: int = 60, attention_head_dim: int = 128, num_attention_heads: int = 24, joint_attention_dim: int = 3584, axes_dims_rope: tuple[int, int, int] = (16, 56, 56), extra_condition_channels: int = 0, # for controlnet-inpainting ): super().__init__() self.out_channels = out_channels or in_channels self.inner_dim = num_attention_heads * attention_head_dim self.pos_embed = QwenEmbedRope(theta=10000, axes_dim=list(axes_dims_rope), scale_rope=True) self.time_text_embed = QwenTimestepProjEmbeddings(embedding_dim=self.inner_dim) self.txt_norm = RMSNorm(joint_attention_dim, eps=1e-6) self.img_in = nn.Linear(in_channels, self.inner_dim) self.txt_in = nn.Linear(joint_attention_dim, self.inner_dim) self.transformer_blocks = nn.ModuleList( [ QwenImageTransformerBlock( dim=self.inner_dim, num_attention_heads=num_attention_heads, attention_head_dim=attention_head_dim, ) for _ in range(num_layers) ] ) # controlnet_blocks self.controlnet_blocks = nn.ModuleList([]) for _ in range(len(self.transformer_blocks)): self.controlnet_blocks.append(zero_module(nn.Linear(self.inner_dim, self.inner_dim))) self.controlnet_x_embedder = zero_module( torch.nn.Linear(in_channels + extra_condition_channels, self.inner_dim) ) self.gradient_checkpointing = False @classmethod def from_transformer( cls, transformer, num_layers: int = 5, attention_head_dim: int = 128, num_attention_heads: int = 24, load_weights_from_transformer=True, extra_condition_channels: int = 0, ): config = dict(transformer.config) config["num_layers"] = num_layers config["attention_head_dim"] = attention_head_dim config["num_attention_heads"] = num_attention_heads config["extra_condition_channels"] = extra_condition_channels controlnet = cls.from_config(config) if load_weights_from_transformer: controlnet.pos_embed.load_state_dict(transformer.pos_embed.state_dict()) controlnet.time_text_embed.load_state_dict(transformer.time_text_embed.state_dict()) controlnet.img_in.load_state_dict(transformer.img_in.state_dict()) controlnet.txt_in.load_state_dict(transformer.txt_in.state_dict()) controlnet.transformer_blocks.load_state_dict(transformer.transformer_blocks.state_dict(), strict=False) controlnet.controlnet_x_embedder = zero_module(controlnet.controlnet_x_embedder) return controlnet @apply_lora_scale("joint_attention_kwargs") def forward( self, hidden_states: torch.Tensor, controlnet_cond: torch.Tensor, conditioning_scale: float = 1.0, encoder_hidden_states: torch.Tensor = None, encoder_hidden_states_mask: torch.Tensor = None, timestep: torch.LongTensor = None, img_shapes: list[tuple[int, int, int]] | None = None, txt_seq_lens: list[int] | None = None, joint_attention_kwargs: dict[str, Any] | None = None, return_dict: bool = True, ) -> torch.FloatTensor | Transformer2DModelOutput: """ The [`QwenImageControlNetModel`] forward method. Args: hidden_states (`torch.FloatTensor` of shape `(batch size, channel, height, width)`): Input `hidden_states`. controlnet_cond (`torch.Tensor`): The conditional input tensor of shape `(batch_size, sequence_length, hidden_size)`. conditioning_scale (`float`, defaults to `1.0`): The scale factor for ControlNet outputs. encoder_hidden_states (`torch.FloatTensor` of shape `(batch size, sequence_len, embed_dims)`): Conditional embeddings (embeddings computed from the input conditions such as prompts) to use. encoder_hidden_states_mask (`torch.Tensor` of shape `(batch_size, text_sequence_length)`, *optional*): Mask for the encoder hidden states. Expected to have 1.0 for valid tokens and 0.0 for padding tokens. Used in the attention processor to prevent attending to padding tokens. The mask can have any pattern (not just contiguous valid tokens followed by padding) since it's applied element-wise in attention. timestep ( `torch.LongTensor`): Used to indicate denoising step. img_shapes (`list[tuple[int, int, int]]`, *optional*): Image shapes for RoPE computation. txt_seq_lens (`list[int]`, *optional*): **Deprecated**. Not needed anymore, we use `encoder_hidden_states` instead to infer text sequence length. joint_attention_kwargs (`dict`, *optional*): A kwargs dictionary that if specified is passed along to the `AttentionProcessor` as defined under `self.processor` in [diffusers.models.attention_processor](https://github.com/huggingface/diffusers/blob/main/src/diffusers/models/attention_processor.py). return_dict (`bool`, *optional*, defaults to `True`): Whether or not to return a [`~models.controlnet.ControlNetOutput`] instead of a plain tuple. Returns: If `return_dict` is True, a [`~models.controlnet.ControlNetOutput`] is returned, otherwise a `tuple` where the first element is the controlnet block samples. """ # Handle deprecated txt_seq_lens parameter if txt_seq_lens is not None: deprecate( "txt_seq_lens", "0.39.0", "Passing `txt_seq_lens` to `QwenImageControlNetModel.forward()` is deprecated and will be removed in " "version 0.39.0. The text sequence length is now automatically inferred from `encoder_hidden_states` " "and `encoder_hidden_states_mask`.", standard_warn=False, ) hidden_states = self.img_in(hidden_states) # add hidden_states = hidden_states + self.controlnet_x_embedder(controlnet_cond) temb = self.time_text_embed(timestep, hidden_states) # Use the encoder_hidden_states sequence length for RoPE computation and normalize mask text_seq_len, _, encoder_hidden_states_mask = compute_text_seq_len_from_mask( encoder_hidden_states, encoder_hidden_states_mask ) image_rotary_emb = self.pos_embed(img_shapes, max_txt_seq_len=text_seq_len, device=hidden_states.device) timestep = timestep.to(hidden_states.dtype) encoder_hidden_states = self.txt_norm(encoder_hidden_states) encoder_hidden_states = self.txt_in(encoder_hidden_states) # Construct joint attention mask once to avoid reconstructing in every block block_attention_kwargs = joint_attention_kwargs.copy() if joint_attention_kwargs is not None else {} if encoder_hidden_states_mask is not None: # Build joint mask: [text_mask, all_ones_for_image] batch_size, image_seq_len = hidden_states.shape[:2] image_mask = torch.ones((batch_size, image_seq_len), dtype=torch.bool, device=hidden_states.device) joint_attention_mask = torch.cat([encoder_hidden_states_mask, image_mask], dim=1) block_attention_kwargs["attention_mask"] = joint_attention_mask block_samples = () for block in self.transformer_blocks: if torch.is_grad_enabled() and self.gradient_checkpointing: encoder_hidden_states, hidden_states = self._gradient_checkpointing_func( block, hidden_states, encoder_hidden_states, None, # Don't pass encoder_hidden_states_mask (using attention_mask instead) temb, image_rotary_emb, block_attention_kwargs, ) else: encoder_hidden_states, hidden_states = block( hidden_states=hidden_states, encoder_hidden_states=encoder_hidden_states, encoder_hidden_states_mask=None, # Don't pass (using attention_mask instead) temb=temb, image_rotary_emb=image_rotary_emb, joint_attention_kwargs=block_attention_kwargs, ) block_samples = block_samples + (hidden_states,) # controlnet block controlnet_block_samples = () for block_sample, controlnet_block in zip(block_samples, self.controlnet_blocks): block_sample = controlnet_block(block_sample) controlnet_block_samples = controlnet_block_samples + (block_sample,) # scaling controlnet_block_samples = [sample * conditioning_scale for sample in controlnet_block_samples] controlnet_block_samples = None if len(controlnet_block_samples) == 0 else controlnet_block_samples if not return_dict: return controlnet_block_samples return QwenImageControlNetOutput( controlnet_block_samples=controlnet_block_samples, ) class QwenImageMultiControlNetModel(ModelMixin, ConfigMixin, PeftAdapterMixin, FromOriginalModelMixin, CacheMixin): r""" `QwenImageMultiControlNetModel` wrapper class for Multi-QwenImageControlNetModel This module is a wrapper for multiple instances of the `QwenImageControlNetModel`. The `forward()` API is designed to be compatible with `QwenImageControlNetModel`. Args: controlnets (`list[QwenImageControlNetModel]`): Provides additional conditioning to the unet during the denoising process. You must set multiple `QwenImageControlNetModel` as a list. """ def __init__(self, controlnets): super().__init__() self.nets = nn.ModuleList(controlnets) def forward( self, hidden_states: torch.FloatTensor, controlnet_cond: list[torch.tensor], conditioning_scale: list[float], encoder_hidden_states: torch.Tensor = None, encoder_hidden_states_mask: torch.Tensor = None, timestep: torch.LongTensor = None, img_shapes: list[tuple[int, int, int]] | None = None, txt_seq_lens: list[int] | None = None, joint_attention_kwargs: dict[str, Any] | None = None, return_dict: bool = True, ) -> QwenImageControlNetOutput | tuple: if txt_seq_lens is not None: deprecate( "txt_seq_lens", "0.39.0", "Passing `txt_seq_lens` to `QwenImageMultiControlNetModel.forward()` is deprecated and will be " "removed in version 0.39.0. The text sequence length is now automatically inferred from " "`encoder_hidden_states` and `encoder_hidden_states_mask`.", standard_warn=False, ) # ControlNet-Union with multiple conditions # only load one ControlNet for saving memories if len(self.nets) == 1: controlnet = self.nets[0] for i, (image, scale) in enumerate(zip(controlnet_cond, conditioning_scale)): block_samples = controlnet( hidden_states=hidden_states, controlnet_cond=image, conditioning_scale=scale, encoder_hidden_states=encoder_hidden_states, encoder_hidden_states_mask=encoder_hidden_states_mask, timestep=timestep, img_shapes=img_shapes, joint_attention_kwargs=joint_attention_kwargs, return_dict=return_dict, ) # merge samples if i == 0: control_block_samples = block_samples else: if block_samples is not None and control_block_samples is not None: control_block_samples = [ control_block_sample + block_sample for control_block_sample, block_sample in zip(control_block_samples, block_samples) ] else: raise ValueError("QwenImageMultiControlNetModel only supports a single controlnet-union now.") return control_block_samples
{ "repo_id": "huggingface/diffusers", "file_path": "src/diffusers/models/controlnets/controlnet_qwenimage.py", "license": "Apache License 2.0", "lines": 282, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
huggingface/diffusers:src/diffusers/pipelines/qwenimage/pipeline_qwenimage_controlnet.py
# Copyright 2025 Qwen-Image Team, InstantX Team and The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import inspect from typing import Any, Callable import numpy as np import torch from transformers import Qwen2_5_VLForConditionalGeneration, Qwen2Tokenizer from ...image_processor import PipelineImageInput, VaeImageProcessor from ...loaders import QwenImageLoraLoaderMixin from ...models import AutoencoderKLQwenImage, QwenImageTransformer2DModel from ...models.controlnets.controlnet_qwenimage import QwenImageControlNetModel, QwenImageMultiControlNetModel from ...schedulers import FlowMatchEulerDiscreteScheduler from ...utils import deprecate, is_torch_xla_available, logging, replace_example_docstring from ...utils.torch_utils import randn_tensor from ..pipeline_utils import DiffusionPipeline from .pipeline_output import QwenImagePipelineOutput if is_torch_xla_available(): import torch_xla.core.xla_model as xm XLA_AVAILABLE = True else: XLA_AVAILABLE = False logger = logging.get_logger(__name__) # pylint: disable=invalid-name EXAMPLE_DOC_STRING = """ Examples: ```py >>> import torch >>> from diffusers.utils import load_image >>> from diffusers import QwenImageControlNetModel, QwenImageMultiControlNetModel, QwenImageControlNetPipeline >>> # QwenImageControlNetModel >>> controlnet = QwenImageControlNetModel.from_pretrained( ... "InstantX/Qwen-Image-ControlNet-Union", torch_dtype=torch.bfloat16 ... ) >>> pipe = QwenImageControlNetPipeline.from_pretrained( ... "Qwen/Qwen-Image", controlnet=controlnet, torch_dtype=torch.bfloat16 ... ) >>> pipe.to("cuda") >>> prompt = "Aesthetics art, traditional asian pagoda, elaborate golden accents, sky blue and white color palette, swirling cloud pattern, digital illustration, east asian architecture, ornamental rooftop, intricate detailing on building, cultural representation." >>> negative_prompt = " " >>> control_image = load_image( ... "https://huggingface.co/InstantX/Qwen-Image-ControlNet-Union/resolve/main/conds/canny.png" ... ) >>> # Depending on the variant being used, the pipeline call will slightly vary. >>> # Refer to the pipeline documentation for more details. >>> image = pipe( ... prompt, ... negative_prompt=negative_prompt, ... control_image=control_image, ... controlnet_conditioning_scale=1.0, ... num_inference_steps=30, ... true_cfg_scale=4.0, ... ).images[0] >>> image.save("qwenimage_cn_union.png") >>> # QwenImageMultiControlNetModel >>> controlnet = QwenImageControlNetModel.from_pretrained( ... "InstantX/Qwen-Image-ControlNet-Union", torch_dtype=torch.bfloat16 ... ) >>> controlnet = QwenImageMultiControlNetModel([controlnet]) >>> pipe = QwenImageControlNetPipeline.from_pretrained( ... "Qwen/Qwen-Image", controlnet=controlnet, torch_dtype=torch.bfloat16 ... ) >>> pipe.to("cuda") >>> prompt = "Aesthetics art, traditional asian pagoda, elaborate golden accents, sky blue and white color palette, swirling cloud pattern, digital illustration, east asian architecture, ornamental rooftop, intricate detailing on building, cultural representation." >>> negative_prompt = " " >>> control_image = load_image( ... "https://huggingface.co/InstantX/Qwen-Image-ControlNet-Union/resolve/main/conds/canny.png" ... ) >>> # Depending on the variant being used, the pipeline call will slightly vary. >>> # Refer to the pipeline documentation for more details. >>> image = pipe( ... prompt, ... negative_prompt=negative_prompt, ... control_image=[control_image, control_image], ... controlnet_conditioning_scale=[0.5, 0.5], ... num_inference_steps=30, ... true_cfg_scale=4.0, ... ).images[0] >>> image.save("qwenimage_cn_union_multi.png") ``` """ # Coped from diffusers.pipelines.qwenimage.pipeline_qwenimage.calculate_shift def calculate_shift( image_seq_len, base_seq_len: int = 256, max_seq_len: int = 4096, base_shift: float = 0.5, max_shift: float = 1.15, ): m = (max_shift - base_shift) / (max_seq_len - base_seq_len) b = base_shift - m * base_seq_len mu = image_seq_len * m + b return mu # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion_img2img.retrieve_latents def retrieve_latents( encoder_output: torch.Tensor, generator: torch.Generator | None = None, sample_mode: str = "sample" ): if hasattr(encoder_output, "latent_dist") and sample_mode == "sample": return encoder_output.latent_dist.sample(generator) elif hasattr(encoder_output, "latent_dist") and sample_mode == "argmax": return encoder_output.latent_dist.mode() elif hasattr(encoder_output, "latents"): return encoder_output.latents else: raise AttributeError("Could not access latents of provided encoder_output") # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.retrieve_timesteps def retrieve_timesteps( scheduler, num_inference_steps: int | None = None, device: str | torch.device | None = None, timesteps: list[int] | None = None, sigmas: list[float] | None = None, **kwargs, ): r""" Calls the scheduler's `set_timesteps` method and retrieves timesteps from the scheduler after the call. Handles custom timesteps. Any kwargs will be supplied to `scheduler.set_timesteps`. Args: scheduler (`SchedulerMixin`): The scheduler to get timesteps from. num_inference_steps (`int`): The number of diffusion steps used when generating samples with a pre-trained model. If used, `timesteps` must be `None`. device (`str` or `torch.device`, *optional*): The device to which the timesteps should be moved to. If `None`, the timesteps are not moved. timesteps (`list[int]`, *optional*): Custom timesteps used to override the timestep spacing strategy of the scheduler. If `timesteps` is passed, `num_inference_steps` and `sigmas` must be `None`. sigmas (`list[float]`, *optional*): Custom sigmas used to override the timestep spacing strategy of the scheduler. If `sigmas` is passed, `num_inference_steps` and `timesteps` must be `None`. Returns: `tuple[torch.Tensor, int]`: A tuple where the first element is the timestep schedule from the scheduler and the second element is the number of inference steps. """ if timesteps is not None and sigmas is not None: raise ValueError("Only one of `timesteps` or `sigmas` can be passed. Please choose one to set custom values") if timesteps is not None: accepts_timesteps = "timesteps" in set(inspect.signature(scheduler.set_timesteps).parameters.keys()) if not accepts_timesteps: raise ValueError( f"The current scheduler class {scheduler.__class__}'s `set_timesteps` does not support custom" f" timestep schedules. Please check whether you are using the correct scheduler." ) scheduler.set_timesteps(timesteps=timesteps, device=device, **kwargs) timesteps = scheduler.timesteps num_inference_steps = len(timesteps) elif sigmas is not None: accept_sigmas = "sigmas" in set(inspect.signature(scheduler.set_timesteps).parameters.keys()) if not accept_sigmas: raise ValueError( f"The current scheduler class {scheduler.__class__}'s `set_timesteps` does not support custom" f" sigmas schedules. Please check whether you are using the correct scheduler." ) scheduler.set_timesteps(sigmas=sigmas, device=device, **kwargs) timesteps = scheduler.timesteps num_inference_steps = len(timesteps) else: scheduler.set_timesteps(num_inference_steps, device=device, **kwargs) timesteps = scheduler.timesteps return timesteps, num_inference_steps class QwenImageControlNetPipeline(DiffusionPipeline, QwenImageLoraLoaderMixin): r""" The QwenImage pipeline for text-to-image generation. Args: transformer ([`QwenImageTransformer2DModel`]): Conditional Transformer (MMDiT) architecture to denoise the encoded image latents. scheduler ([`FlowMatchEulerDiscreteScheduler`]): A scheduler to be used in combination with `transformer` to denoise the encoded image latents. vae ([`AutoencoderKL`]): Variational Auto-Encoder (VAE) Model to encode and decode images to and from latent representations. text_encoder ([`Qwen2.5-VL-7B-Instruct`]): [Qwen2.5-VL-7B-Instruct](https://huggingface.co/Qwen/Qwen2.5-VL-7B-Instruct), specifically the [Qwen2.5-VL-7B-Instruct](https://huggingface.co/Qwen/Qwen2.5-VL-7B-Instruct) variant. tokenizer (`QwenTokenizer`): Tokenizer of class [CLIPTokenizer](https://huggingface.co/docs/transformers/en/model_doc/clip#transformers.CLIPTokenizer). """ model_cpu_offload_seq = "text_encoder->transformer->vae" _callback_tensor_inputs = ["latents", "prompt_embeds"] def __init__( self, scheduler: FlowMatchEulerDiscreteScheduler, vae: AutoencoderKLQwenImage, text_encoder: Qwen2_5_VLForConditionalGeneration, tokenizer: Qwen2Tokenizer, transformer: QwenImageTransformer2DModel, controlnet: QwenImageControlNetModel | QwenImageMultiControlNetModel, ): super().__init__() self.register_modules( vae=vae, text_encoder=text_encoder, tokenizer=tokenizer, transformer=transformer, scheduler=scheduler, controlnet=controlnet, ) self.vae_scale_factor = 2 ** len(self.vae.temperal_downsample) if getattr(self, "vae", None) else 8 # QwenImage latents are turned into 2x2 patches and packed. This means the latent width and height has to be divisible # by the patch size. So the vae scale factor is multiplied by the patch size to account for this self.image_processor = VaeImageProcessor(vae_scale_factor=self.vae_scale_factor * 2) self.tokenizer_max_length = 1024 self.prompt_template_encode = "<|im_start|>system\nDescribe the image by detailing the color, shape, size, texture, quantity, text, spatial relationships of the objects and background:<|im_end|>\n<|im_start|>user\n{}<|im_end|>\n<|im_start|>assistant\n" self.prompt_template_encode_start_idx = 34 self.default_sample_size = 128 # Coped from diffusers.pipelines.qwenimage.pipeline_qwenimage.extract_masked_hidden def _extract_masked_hidden(self, hidden_states: torch.Tensor, mask: torch.Tensor): bool_mask = mask.bool() valid_lengths = bool_mask.sum(dim=1) selected = hidden_states[bool_mask] split_result = torch.split(selected, valid_lengths.tolist(), dim=0) return split_result # Coped from diffusers.pipelines.qwenimage.pipeline_qwenimage.get_qwen_prompt_embeds def _get_qwen_prompt_embeds( self, prompt: str | list[str] = None, device: torch.device | None = None, dtype: torch.dtype | None = None, ): device = device or self._execution_device dtype = dtype or self.text_encoder.dtype prompt = [prompt] if isinstance(prompt, str) else prompt template = self.prompt_template_encode drop_idx = self.prompt_template_encode_start_idx txt = [template.format(e) for e in prompt] txt_tokens = self.tokenizer( txt, max_length=self.tokenizer_max_length + drop_idx, padding=True, truncation=True, return_tensors="pt" ).to(device) encoder_hidden_states = self.text_encoder( input_ids=txt_tokens.input_ids, attention_mask=txt_tokens.attention_mask, output_hidden_states=True, ) hidden_states = encoder_hidden_states.hidden_states[-1] split_hidden_states = self._extract_masked_hidden(hidden_states, txt_tokens.attention_mask) split_hidden_states = [e[drop_idx:] for e in split_hidden_states] attn_mask_list = [torch.ones(e.size(0), dtype=torch.long, device=e.device) for e in split_hidden_states] max_seq_len = max([e.size(0) for e in split_hidden_states]) prompt_embeds = torch.stack( [torch.cat([u, u.new_zeros(max_seq_len - u.size(0), u.size(1))]) for u in split_hidden_states] ) encoder_attention_mask = torch.stack( [torch.cat([u, u.new_zeros(max_seq_len - u.size(0))]) for u in attn_mask_list] ) prompt_embeds = prompt_embeds.to(dtype=dtype, device=device) return prompt_embeds, encoder_attention_mask # Coped from diffusers.pipelines.qwenimage.pipeline_qwenimage.encode_prompt def encode_prompt( self, prompt: str | list[str], device: torch.device | None = None, num_images_per_prompt: int = 1, prompt_embeds: torch.Tensor | None = None, prompt_embeds_mask: torch.Tensor | None = None, max_sequence_length: int = 1024, ): r""" Args: prompt (`str` or `list[str]`, *optional*): prompt to be encoded device: (`torch.device`): torch device num_images_per_prompt (`int`): number of images that should be generated per prompt prompt_embeds (`torch.Tensor`, *optional*): Pre-generated text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt weighting. If not provided, text embeddings will be generated from `prompt` input argument. """ device = device or self._execution_device prompt = [prompt] if isinstance(prompt, str) else prompt batch_size = len(prompt) if prompt_embeds is None else prompt_embeds.shape[0] if prompt_embeds is None: prompt_embeds, prompt_embeds_mask = self._get_qwen_prompt_embeds(prompt, device) _, seq_len, _ = prompt_embeds.shape prompt_embeds = prompt_embeds.repeat(1, num_images_per_prompt, 1) prompt_embeds = prompt_embeds.view(batch_size * num_images_per_prompt, seq_len, -1) if prompt_embeds_mask is not None: prompt_embeds_mask = prompt_embeds_mask.repeat(1, num_images_per_prompt, 1) prompt_embeds_mask = prompt_embeds_mask.view(batch_size * num_images_per_prompt, seq_len) if prompt_embeds_mask.all(): prompt_embeds_mask = None return prompt_embeds, prompt_embeds_mask def check_inputs( self, prompt, height, width, negative_prompt=None, prompt_embeds=None, negative_prompt_embeds=None, prompt_embeds_mask=None, negative_prompt_embeds_mask=None, callback_on_step_end_tensor_inputs=None, max_sequence_length=None, ): if height % (self.vae_scale_factor * 2) != 0 or width % (self.vae_scale_factor * 2) != 0: logger.warning( f"`height` and `width` have to be divisible by {self.vae_scale_factor * 2} but are {height} and {width}. Dimensions will be resized accordingly" ) if callback_on_step_end_tensor_inputs is not None and not all( k in self._callback_tensor_inputs for k in callback_on_step_end_tensor_inputs ): raise ValueError( f"`callback_on_step_end_tensor_inputs` has to be in {self._callback_tensor_inputs}, but found {[k for k in callback_on_step_end_tensor_inputs if k not in self._callback_tensor_inputs]}" ) if prompt is not None and prompt_embeds is not None: raise ValueError( f"Cannot forward both `prompt`: {prompt} and `prompt_embeds`: {prompt_embeds}. Please make sure to" " only forward one of the two." ) elif prompt is None and prompt_embeds is None: raise ValueError( "Provide either `prompt` or `prompt_embeds`. Cannot leave both `prompt` and `prompt_embeds` undefined." ) elif prompt is not None and (not isinstance(prompt, str) and not isinstance(prompt, list)): raise ValueError(f"`prompt` has to be of type `str` or `list` but is {type(prompt)}") if negative_prompt is not None and negative_prompt_embeds is not None: raise ValueError( f"Cannot forward both `negative_prompt`: {negative_prompt} and `negative_prompt_embeds`:" f" {negative_prompt_embeds}. Please make sure to only forward one of the two." ) if max_sequence_length is not None and max_sequence_length > 1024: raise ValueError(f"`max_sequence_length` cannot be greater than 1024 but is {max_sequence_length}") @staticmethod # Copied from diffusers.pipelines.qwenimage.pipeline_qwenimage.QwenImagePipeline._pack_latents def _pack_latents(latents, batch_size, num_channels_latents, height, width): latents = latents.view(batch_size, num_channels_latents, height // 2, 2, width // 2, 2) latents = latents.permute(0, 2, 4, 1, 3, 5) latents = latents.reshape(batch_size, (height // 2) * (width // 2), num_channels_latents * 4) return latents @staticmethod # Copied from diffusers.pipelines.qwenimage.pipeline_qwenimage.QwenImagePipeline._unpack_latents def _unpack_latents(latents, height, width, vae_scale_factor): batch_size, num_patches, channels = latents.shape # VAE applies 8x compression on images but we must also account for packing which requires # latent height and width to be divisible by 2. height = 2 * (int(height) // (vae_scale_factor * 2)) width = 2 * (int(width) // (vae_scale_factor * 2)) latents = latents.view(batch_size, height // 2, width // 2, channels // 4, 2, 2) latents = latents.permute(0, 3, 1, 4, 2, 5) latents = latents.reshape(batch_size, channels // (2 * 2), 1, height, width) return latents def enable_vae_slicing(self): r""" Enable sliced VAE decoding. When this option is enabled, the VAE will split the input tensor in slices to compute decoding in several steps. This is useful to save some memory and allow larger batch sizes. """ depr_message = f"Calling `enable_vae_slicing()` on a `{self.__class__.__name__}` is deprecated and this method will be removed in a future version. Please use `pipe.vae.enable_slicing()`." deprecate( "enable_vae_slicing", "0.40.0", depr_message, ) self.vae.enable_slicing() def disable_vae_slicing(self): r""" Disable sliced VAE decoding. If `enable_vae_slicing` was previously enabled, this method will go back to computing decoding in one step. """ depr_message = f"Calling `disable_vae_slicing()` on a `{self.__class__.__name__}` is deprecated and this method will be removed in a future version. Please use `pipe.vae.disable_slicing()`." deprecate( "disable_vae_slicing", "0.40.0", depr_message, ) self.vae.disable_slicing() def enable_vae_tiling(self): r""" Enable tiled VAE decoding. When this option is enabled, the VAE will split the input tensor into tiles to compute decoding and encoding in several steps. This is useful for saving a large amount of memory and to allow processing larger images. """ depr_message = f"Calling `enable_vae_tiling()` on a `{self.__class__.__name__}` is deprecated and this method will be removed in a future version. Please use `pipe.vae.enable_tiling()`." deprecate( "enable_vae_tiling", "0.40.0", depr_message, ) self.vae.enable_tiling() def disable_vae_tiling(self): r""" Disable tiled VAE decoding. If `enable_vae_tiling` was previously enabled, this method will go back to computing decoding in one step. """ depr_message = f"Calling `disable_vae_tiling()` on a `{self.__class__.__name__}` is deprecated and this method will be removed in a future version. Please use `pipe.vae.disable_tiling()`." deprecate( "disable_vae_tiling", "0.40.0", depr_message, ) self.vae.disable_tiling() # Copied from diffusers.pipelines.qwenimage.pipeline_qwenimage.QwenImagePipeline.prepare_latents def prepare_latents( self, batch_size, num_channels_latents, height, width, dtype, device, generator, latents=None, ): # VAE applies 8x compression on images but we must also account for packing which requires # latent height and width to be divisible by 2. height = 2 * (int(height) // (self.vae_scale_factor * 2)) width = 2 * (int(width) // (self.vae_scale_factor * 2)) shape = (batch_size, 1, num_channels_latents, height, width) if latents is not None: return latents.to(device=device, dtype=dtype) if isinstance(generator, list) and len(generator) != batch_size: raise ValueError( f"You have passed a list of generators of length {len(generator)}, but requested an effective batch" f" size of {batch_size}. Make sure the batch size matches the length of the generators." ) latents = randn_tensor(shape, generator=generator, device=device, dtype=dtype) latents = self._pack_latents(latents, batch_size, num_channels_latents, height, width) return latents # Copied from diffusers.pipelines.controlnet_sd3.pipeline_stable_diffusion_3_controlnet.StableDiffusion3ControlNetPipeline.prepare_image def prepare_image( self, image, width, height, batch_size, num_images_per_prompt, device, dtype, do_classifier_free_guidance=False, guess_mode=False, ): if isinstance(image, torch.Tensor): pass else: image = self.image_processor.preprocess(image, height=height, width=width) image_batch_size = image.shape[0] if image_batch_size == 1: repeat_by = batch_size else: # image batch size is the same as prompt batch size repeat_by = num_images_per_prompt image = image.repeat_interleave(repeat_by, dim=0) image = image.to(device=device, dtype=dtype) if do_classifier_free_guidance and not guess_mode: image = torch.cat([image] * 2) return image @property def guidance_scale(self): return self._guidance_scale @property def attention_kwargs(self): return self._attention_kwargs @property def num_timesteps(self): return self._num_timesteps @property def current_timestep(self): return self._current_timestep @property def interrupt(self): return self._interrupt @torch.no_grad() @replace_example_docstring(EXAMPLE_DOC_STRING) def __call__( self, prompt: str | list[str] = None, negative_prompt: str | list[str] = None, true_cfg_scale: float = 4.0, height: int | None = None, width: int | None = None, num_inference_steps: int = 50, sigmas: list[float] | None = None, guidance_scale: float | None = None, control_guidance_start: float | list[float] = 0.0, control_guidance_end: float | list[float] = 1.0, control_image: PipelineImageInput = None, controlnet_conditioning_scale: float | list[float] = 1.0, num_images_per_prompt: int = 1, generator: torch.Generator | list[torch.Generator] | None = None, latents: torch.Tensor | None = None, prompt_embeds: torch.Tensor | None = None, prompt_embeds_mask: torch.Tensor | None = None, negative_prompt_embeds: torch.Tensor | None = None, negative_prompt_embeds_mask: torch.Tensor | None = None, output_type: str | None = "pil", return_dict: bool = True, attention_kwargs: dict[str, Any] | None = None, callback_on_step_end: Callable[[int, int], None] | None = None, callback_on_step_end_tensor_inputs: list[str] = ["latents"], max_sequence_length: int = 512, ): r""" Function invoked when calling the pipeline for generation. Args: prompt (`str` or `list[str]`, *optional*): The prompt or prompts to guide the image generation. If not defined, one has to pass `prompt_embeds`. instead. negative_prompt (`str` or `list[str]`, *optional*): The prompt or prompts not to guide the image generation. If not defined, one has to pass `negative_prompt_embeds` instead. Ignored when not using guidance (i.e., ignored if `true_cfg_scale` is not greater than `1`). true_cfg_scale (`float`, *optional*, defaults to 1.0): Guidance scale as defined in [Classifier-Free Diffusion Guidance](https://huggingface.co/papers/2207.12598). `true_cfg_scale` is defined as `w` of equation 2. of [Imagen Paper](https://huggingface.co/papers/2205.11487). Classifier-free guidance is enabled by setting `true_cfg_scale > 1` and a provided `negative_prompt`. Higher guidance scale encourages to generate images that are closely linked to the text `prompt`, usually at the expense of lower image quality. height (`int`, *optional*, defaults to self.unet.config.sample_size * self.vae_scale_factor): The height in pixels of the generated image. This is set to 1024 by default for the best results. width (`int`, *optional*, defaults to self.unet.config.sample_size * self.vae_scale_factor): The width in pixels of the generated image. This is set to 1024 by default for the best results. num_inference_steps (`int`, *optional*, defaults to 50): The number of denoising steps. More denoising steps usually lead to a higher quality image at the expense of slower inference. sigmas (`list[float]`, *optional*): Custom sigmas to use for the denoising process with schedulers which support a `sigmas` argument in their `set_timesteps` method. If not defined, the default behavior when `num_inference_steps` is passed will be used. guidance_scale (`float`, *optional*, defaults to None): A guidance scale value for guidance distilled models. Unlike the traditional classifier-free guidance where the guidance scale is applied during inference through noise prediction rescaling, guidance distilled models take the guidance scale directly as an input parameter during forward pass. Guidance scale is enabled by setting `guidance_scale > 1`. Higher guidance scale encourages to generate images that are closely linked to the text `prompt`, usually at the expense of lower image quality. This parameter in the pipeline is there to support future guidance-distilled models when they come up. It is ignored when not using guidance distilled models. To enable traditional classifier-free guidance, please pass `true_cfg_scale > 1.0` and `negative_prompt` (even an empty negative prompt like " " should enable classifier-free guidance computations). num_images_per_prompt (`int`, *optional*, defaults to 1): The number of images to generate per prompt. generator (`torch.Generator` or `list[torch.Generator]`, *optional*): One or a list of [torch generator(s)](https://pytorch.org/docs/stable/generated/torch.Generator.html) to make generation deterministic. latents (`torch.Tensor`, *optional*): Pre-generated noisy latents, sampled from a Gaussian distribution, to be used as inputs for image generation. Can be used to tweak the same generation with different prompts. If not provided, a latents tensor will be generated by sampling using the supplied random `generator`. prompt_embeds (`torch.Tensor`, *optional*): Pre-generated text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt weighting. If not provided, text embeddings will be generated from `prompt` input argument. negative_prompt_embeds (`torch.Tensor`, *optional*): Pre-generated negative text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt weighting. If not provided, negative_prompt_embeds will be generated from `negative_prompt` input argument. output_type (`str`, *optional*, defaults to `"pil"`): The output format of the generate image. Choose between [PIL](https://pillow.readthedocs.io/en/stable/): `PIL.Image.Image` or `np.array`. return_dict (`bool`, *optional*, defaults to `True`): Whether or not to return a [`~pipelines.qwenimage.QwenImagePipelineOutput`] instead of a plain tuple. attention_kwargs (`dict`, *optional*): A kwargs dictionary that if specified is passed along to the `AttentionProcessor` as defined under `self.processor` in [diffusers.models.attention_processor](https://github.com/huggingface/diffusers/blob/main/src/diffusers/models/attention_processor.py). callback_on_step_end (`Callable`, *optional*): A function that calls at the end of each denoising steps during the inference. The function is called with the following arguments: `callback_on_step_end(self: DiffusionPipeline, step: int, timestep: int, callback_kwargs: Dict)`. `callback_kwargs` will include a list of all tensors as specified by `callback_on_step_end_tensor_inputs`. callback_on_step_end_tensor_inputs (`list`, *optional*): The list of tensor inputs for the `callback_on_step_end` function. The tensors specified in the list will be passed as `callback_kwargs` argument. You will only be able to include variables listed in the `._callback_tensor_inputs` attribute of your pipeline class. max_sequence_length (`int` defaults to 512): Maximum sequence length to use with the `prompt`. Examples: Returns: [`~pipelines.qwenimage.QwenImagePipelineOutput`] or `tuple`: [`~pipelines.qwenimage.QwenImagePipelineOutput`] if `return_dict` is True, otherwise a `tuple`. When returning a tuple, the first element is a list with the generated images. """ height = height or self.default_sample_size * self.vae_scale_factor width = width or self.default_sample_size * self.vae_scale_factor if not isinstance(control_guidance_start, list) and isinstance(control_guidance_end, list): control_guidance_start = len(control_guidance_end) * [control_guidance_start] elif not isinstance(control_guidance_end, list) and isinstance(control_guidance_start, list): control_guidance_end = len(control_guidance_start) * [control_guidance_end] elif not isinstance(control_guidance_start, list) and not isinstance(control_guidance_end, list): mult = len(control_image) if isinstance(self.controlnet, QwenImageMultiControlNetModel) else 1 control_guidance_start, control_guidance_end = ( mult * [control_guidance_start], mult * [control_guidance_end], ) # 1. Check inputs. Raise error if not correct self.check_inputs( prompt, height, width, negative_prompt=negative_prompt, prompt_embeds=prompt_embeds, negative_prompt_embeds=negative_prompt_embeds, prompt_embeds_mask=prompt_embeds_mask, negative_prompt_embeds_mask=negative_prompt_embeds_mask, callback_on_step_end_tensor_inputs=callback_on_step_end_tensor_inputs, max_sequence_length=max_sequence_length, ) self._guidance_scale = guidance_scale self._attention_kwargs = attention_kwargs self._current_timestep = None self._interrupt = False # 2. Define call parameters if prompt is not None and isinstance(prompt, str): batch_size = 1 elif prompt is not None and isinstance(prompt, list): batch_size = len(prompt) else: batch_size = prompt_embeds.shape[0] device = self._execution_device has_neg_prompt = negative_prompt is not None or ( negative_prompt_embeds is not None and negative_prompt_embeds_mask is not None ) if true_cfg_scale > 1 and not has_neg_prompt: logger.warning( f"true_cfg_scale is passed as {true_cfg_scale}, but classifier-free guidance is not enabled since no negative_prompt is provided." ) elif true_cfg_scale <= 1 and has_neg_prompt: logger.warning( " negative_prompt is passed but classifier-free guidance is not enabled since true_cfg_scale <= 1" ) do_true_cfg = true_cfg_scale > 1 and has_neg_prompt prompt_embeds, prompt_embeds_mask = self.encode_prompt( prompt=prompt, prompt_embeds=prompt_embeds, prompt_embeds_mask=prompt_embeds_mask, device=device, num_images_per_prompt=num_images_per_prompt, max_sequence_length=max_sequence_length, ) if do_true_cfg: negative_prompt_embeds, negative_prompt_embeds_mask = self.encode_prompt( prompt=negative_prompt, prompt_embeds=negative_prompt_embeds, prompt_embeds_mask=negative_prompt_embeds_mask, device=device, num_images_per_prompt=num_images_per_prompt, max_sequence_length=max_sequence_length, ) # 3. Prepare control image num_channels_latents = self.transformer.config.in_channels // 4 if isinstance(self.controlnet, QwenImageControlNetModel): control_image = self.prepare_image( image=control_image, width=width, height=height, batch_size=batch_size * num_images_per_prompt, num_images_per_prompt=num_images_per_prompt, device=device, dtype=self.vae.dtype, ) height, width = control_image.shape[-2:] if control_image.ndim == 4: control_image = control_image.unsqueeze(2) # vae encode self.vae_scale_factor = 2 ** len(self.vae.temperal_downsample) latents_mean = (torch.tensor(self.vae.config.latents_mean).view(1, self.vae.config.z_dim, 1, 1, 1)).to( device ) latents_std = 1.0 / torch.tensor(self.vae.config.latents_std).view(1, self.vae.config.z_dim, 1, 1, 1).to( device ) control_image = retrieve_latents(self.vae.encode(control_image), generator=generator) control_image = (control_image - latents_mean) * latents_std control_image = control_image.permute(0, 2, 1, 3, 4) # pack control_image = self._pack_latents( control_image, batch_size=control_image.shape[0], num_channels_latents=num_channels_latents, height=control_image.shape[3], width=control_image.shape[4], ).to(dtype=prompt_embeds.dtype, device=device) else: if isinstance(self.controlnet, QwenImageMultiControlNetModel): control_images = [] for control_image_ in control_image: control_image_ = self.prepare_image( image=control_image_, width=width, height=height, batch_size=batch_size * num_images_per_prompt, num_images_per_prompt=num_images_per_prompt, device=device, dtype=self.vae.dtype, ) height, width = control_image_.shape[-2:] if control_image_.ndim == 4: control_image_ = control_image_.unsqueeze(2) # vae encode self.vae_scale_factor = 2 ** len(self.vae.temperal_downsample) latents_mean = ( torch.tensor(self.vae.config.latents_mean).view(1, self.vae.config.z_dim, 1, 1, 1) ).to(device) latents_std = 1.0 / torch.tensor(self.vae.config.latents_std).view( 1, self.vae.config.z_dim, 1, 1, 1 ).to(device) control_image_ = retrieve_latents(self.vae.encode(control_image_), generator=generator) control_image_ = (control_image_ - latents_mean) * latents_std control_image_ = control_image_.permute(0, 2, 1, 3, 4) # pack control_image_ = self._pack_latents( control_image_, batch_size=control_image_.shape[0], num_channels_latents=num_channels_latents, height=control_image_.shape[3], width=control_image_.shape[4], ).to(dtype=prompt_embeds.dtype, device=device) control_images.append(control_image_) control_image = control_images # 4. Prepare latent variables num_channels_latents = self.transformer.config.in_channels // 4 latents = self.prepare_latents( batch_size * num_images_per_prompt, num_channels_latents, height, width, prompt_embeds.dtype, device, generator, latents, ) img_shapes = [(1, height // self.vae_scale_factor // 2, width // self.vae_scale_factor // 2)] * batch_size # 5. Prepare timesteps sigmas = np.linspace(1.0, 1 / num_inference_steps, num_inference_steps) if sigmas is None else sigmas image_seq_len = latents.shape[1] mu = calculate_shift( image_seq_len, self.scheduler.config.get("base_image_seq_len", 256), self.scheduler.config.get("max_image_seq_len", 4096), self.scheduler.config.get("base_shift", 0.5), self.scheduler.config.get("max_shift", 1.15), ) timesteps, num_inference_steps = retrieve_timesteps( self.scheduler, num_inference_steps, device, sigmas=sigmas, mu=mu, ) num_warmup_steps = max(len(timesteps) - num_inference_steps * self.scheduler.order, 0) self._num_timesteps = len(timesteps) controlnet_keep = [] for i in range(len(timesteps)): keeps = [ 1.0 - float(i / len(timesteps) < s or (i + 1) / len(timesteps) > e) for s, e in zip(control_guidance_start, control_guidance_end) ] controlnet_keep.append(keeps[0] if isinstance(self.controlnet, QwenImageControlNetModel) else keeps) # handle guidance if self.transformer.config.guidance_embeds and guidance_scale is None: raise ValueError("guidance_scale is required for guidance-distilled model.") elif self.transformer.config.guidance_embeds: guidance = torch.full([1], guidance_scale, device=device, dtype=torch.float32) guidance = guidance.expand(latents.shape[0]) elif not self.transformer.config.guidance_embeds and guidance_scale is not None: logger.warning( f"guidance_scale is passed as {guidance_scale}, but ignored since the model is not guidance-distilled." ) guidance = None elif not self.transformer.config.guidance_embeds and guidance_scale is None: guidance = None if self.attention_kwargs is None: self._attention_kwargs = {} # 6. Denoising loop self.scheduler.set_begin_index(0) with self.progress_bar(total=num_inference_steps) as progress_bar: for i, t in enumerate(timesteps): if self.interrupt: continue self._current_timestep = t # broadcast to batch dimension in a way that's compatible with ONNX/Core ML timestep = t.expand(latents.shape[0]).to(latents.dtype) if isinstance(controlnet_keep[i], list): cond_scale = [c * s for c, s in zip(controlnet_conditioning_scale, controlnet_keep[i])] else: controlnet_cond_scale = controlnet_conditioning_scale if isinstance(controlnet_cond_scale, list): controlnet_cond_scale = controlnet_cond_scale[0] cond_scale = controlnet_cond_scale * controlnet_keep[i] # controlnet controlnet_block_samples = self.controlnet( hidden_states=latents, controlnet_cond=control_image, conditioning_scale=cond_scale, timestep=timestep / 1000, encoder_hidden_states=prompt_embeds, encoder_hidden_states_mask=prompt_embeds_mask, img_shapes=img_shapes, return_dict=False, ) with self.transformer.cache_context("cond"): noise_pred = self.transformer( hidden_states=latents, timestep=timestep / 1000, encoder_hidden_states=prompt_embeds, encoder_hidden_states_mask=prompt_embeds_mask, img_shapes=img_shapes, controlnet_block_samples=controlnet_block_samples, attention_kwargs=self.attention_kwargs, return_dict=False, )[0] if do_true_cfg: with self.transformer.cache_context("uncond"): neg_noise_pred = self.transformer( hidden_states=latents, timestep=timestep / 1000, guidance=guidance, encoder_hidden_states_mask=negative_prompt_embeds_mask, encoder_hidden_states=negative_prompt_embeds, img_shapes=img_shapes, controlnet_block_samples=controlnet_block_samples, attention_kwargs=self.attention_kwargs, return_dict=False, )[0] comb_pred = neg_noise_pred + true_cfg_scale * (noise_pred - neg_noise_pred) cond_norm = torch.norm(noise_pred, dim=-1, keepdim=True) noise_norm = torch.norm(comb_pred, dim=-1, keepdim=True) noise_pred = comb_pred * (cond_norm / noise_norm) # compute the previous noisy sample x_t -> x_t-1 latents_dtype = latents.dtype latents = self.scheduler.step(noise_pred, t, latents, return_dict=False)[0] if latents.dtype != latents_dtype: if torch.backends.mps.is_available(): # some platforms (eg. apple mps) misbehave due to a pytorch bug: https://github.com/pytorch/pytorch/pull/99272 latents = latents.to(latents_dtype) if callback_on_step_end is not None: callback_kwargs = {} for k in callback_on_step_end_tensor_inputs: callback_kwargs[k] = locals()[k] callback_outputs = callback_on_step_end(self, i, t, callback_kwargs) latents = callback_outputs.pop("latents", latents) prompt_embeds = callback_outputs.pop("prompt_embeds", prompt_embeds) # call the callback, if provided if i == len(timesteps) - 1 or ((i + 1) > num_warmup_steps and (i + 1) % self.scheduler.order == 0): progress_bar.update() if XLA_AVAILABLE: xm.mark_step() self._current_timestep = None if output_type == "latent": image = latents else: latents = self._unpack_latents(latents, height, width, self.vae_scale_factor) latents = latents.to(self.vae.dtype) latents_mean = ( torch.tensor(self.vae.config.latents_mean) .view(1, self.vae.config.z_dim, 1, 1, 1) .to(latents.device, latents.dtype) ) latents_std = 1.0 / torch.tensor(self.vae.config.latents_std).view(1, self.vae.config.z_dim, 1, 1, 1).to( latents.device, latents.dtype ) latents = latents / latents_std + latents_mean image = self.vae.decode(latents, return_dict=False)[0][:, :, 0] image = self.image_processor.postprocess(image, output_type=output_type) # Offload all models self.maybe_free_model_hooks() if not return_dict: return (image,) return QwenImagePipelineOutput(images=image)
{ "repo_id": "huggingface/diffusers", "file_path": "src/diffusers/pipelines/qwenimage/pipeline_qwenimage_controlnet.py", "license": "Apache License 2.0", "lines": 867, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
huggingface/diffusers:src/diffusers/models/transformers/transformer_bria.py
import inspect from typing import Any import numpy as np import torch import torch.nn as nn import torch.nn.functional as F from ...configuration_utils import ConfigMixin, register_to_config from ...loaders import FromOriginalModelMixin, PeftAdapterMixin from ...utils import apply_lora_scale, logging from ...utils.torch_utils import maybe_allow_in_graph from ..attention import AttentionModuleMixin, FeedForward from ..attention_dispatch import dispatch_attention_fn from ..cache_utils import CacheMixin from ..embeddings import TimestepEmbedding, apply_rotary_emb, get_timestep_embedding from ..modeling_outputs import Transformer2DModelOutput from ..modeling_utils import ModelMixin from ..normalization import AdaLayerNormContinuous, AdaLayerNormZero, AdaLayerNormZeroSingle logger = logging.get_logger(__name__) # pylint: disable=invalid-name def _get_projections(attn: "BriaAttention", hidden_states, encoder_hidden_states=None): query = attn.to_q(hidden_states) key = attn.to_k(hidden_states) value = attn.to_v(hidden_states) encoder_query = encoder_key = encoder_value = None if encoder_hidden_states is not None and attn.added_kv_proj_dim is not None: encoder_query = attn.add_q_proj(encoder_hidden_states) encoder_key = attn.add_k_proj(encoder_hidden_states) encoder_value = attn.add_v_proj(encoder_hidden_states) return query, key, value, encoder_query, encoder_key, encoder_value def _get_fused_projections(attn: "BriaAttention", hidden_states, encoder_hidden_states=None): query, key, value = attn.to_qkv(hidden_states).chunk(3, dim=-1) encoder_query = encoder_key = encoder_value = (None,) if encoder_hidden_states is not None and hasattr(attn, "to_added_qkv"): encoder_query, encoder_key, encoder_value = attn.to_added_qkv(encoder_hidden_states).chunk(3, dim=-1) return query, key, value, encoder_query, encoder_key, encoder_value def _get_qkv_projections(attn: "BriaAttention", hidden_states, encoder_hidden_states=None): if attn.fused_projections: return _get_fused_projections(attn, hidden_states, encoder_hidden_states) return _get_projections(attn, hidden_states, encoder_hidden_states) def get_1d_rotary_pos_embed( dim: int, pos: np.ndarray | int, theta: float = 10000.0, use_real=False, linear_factor=1.0, ntk_factor=1.0, repeat_interleave_real=True, freqs_dtype=torch.float32, # torch.float32, torch.float64 (flux) ): """ Precompute the frequency tensor for complex exponentials (cis) with given dimensions. This function calculates a frequency tensor with complex exponentials using the given dimension 'dim' and the end index 'end'. The 'theta' parameter scales the frequencies. The returned tensor contains complex values in complex64 data type. Args: dim (`int`): Dimension of the frequency tensor. pos (`np.ndarray` or `int`): Position indices for the frequency tensor. [S] or scalar theta (`float`, *optional*, defaults to 10000.0): Scaling factor for frequency computation. Defaults to 10000.0. use_real (`bool`, *optional*): If True, return real part and imaginary part separately. Otherwise, return complex numbers. linear_factor (`float`, *optional*, defaults to 1.0): Scaling factor for the context extrapolation. Defaults to 1.0. ntk_factor (`float`, *optional*, defaults to 1.0): Scaling factor for the NTK-Aware RoPE. Defaults to 1.0. repeat_interleave_real (`bool`, *optional*, defaults to `True`): If `True` and `use_real`, real part and imaginary part are each interleaved with themselves to reach `dim`. Otherwise, they are concateanted with themselves. freqs_dtype (`torch.float32` or `torch.float64`, *optional*, defaults to `torch.float32`): the dtype of the frequency tensor. Returns: `torch.Tensor`: Precomputed frequency tensor with complex exponentials. [S, D/2] """ assert dim % 2 == 0 if isinstance(pos, int): pos = torch.arange(pos) if isinstance(pos, np.ndarray): pos = torch.from_numpy(pos) # type: ignore # [S] theta = theta * ntk_factor freqs = ( 1.0 / (theta ** (torch.arange(0, dim, 2, dtype=freqs_dtype, device=pos.device)[: (dim // 2)] / dim)) / linear_factor ) # [D/2] freqs = torch.outer(pos, freqs) # type: ignore # [S, D/2] if use_real and repeat_interleave_real: # bria freqs_cos = freqs.cos().repeat_interleave(2, dim=1).float() # [S, D] freqs_sin = freqs.sin().repeat_interleave(2, dim=1).float() # [S, D] return freqs_cos, freqs_sin elif use_real: # stable audio, allegro freqs_cos = torch.cat([freqs.cos(), freqs.cos()], dim=-1).float() # [S, D] freqs_sin = torch.cat([freqs.sin(), freqs.sin()], dim=-1).float() # [S, D] return freqs_cos, freqs_sin else: # lumina freqs_cis = torch.polar(torch.ones_like(freqs), freqs) # complex64 # [S, D/2] return freqs_cis class BriaAttnProcessor: _attention_backend = None _parallel_config = None def __init__(self): if not hasattr(F, "scaled_dot_product_attention"): raise ImportError(f"{self.__class__.__name__} requires PyTorch 2.0. Please upgrade your pytorch version.") def __call__( self, attn: "BriaAttention", hidden_states: torch.Tensor, encoder_hidden_states: torch.Tensor = None, attention_mask: torch.Tensor | None = None, image_rotary_emb: torch.Tensor | None = None, ) -> torch.Tensor: query, key, value, encoder_query, encoder_key, encoder_value = _get_qkv_projections( attn, hidden_states, encoder_hidden_states ) query = query.unflatten(-1, (attn.heads, -1)) key = key.unflatten(-1, (attn.heads, -1)) value = value.unflatten(-1, (attn.heads, -1)) query = attn.norm_q(query) key = attn.norm_k(key) if attn.added_kv_proj_dim is not None: encoder_query = encoder_query.unflatten(-1, (attn.heads, -1)) encoder_key = encoder_key.unflatten(-1, (attn.heads, -1)) encoder_value = encoder_value.unflatten(-1, (attn.heads, -1)) encoder_query = attn.norm_added_q(encoder_query) encoder_key = attn.norm_added_k(encoder_key) query = torch.cat([encoder_query, query], dim=1) key = torch.cat([encoder_key, key], dim=1) value = torch.cat([encoder_value, value], dim=1) if image_rotary_emb is not None: query = apply_rotary_emb(query, image_rotary_emb, sequence_dim=1) key = apply_rotary_emb(key, image_rotary_emb, sequence_dim=1) hidden_states = dispatch_attention_fn( query, key, value, attn_mask=attention_mask, backend=self._attention_backend, parallel_config=self._parallel_config, ) hidden_states = hidden_states.flatten(2, 3) hidden_states = hidden_states.to(query.dtype) if encoder_hidden_states is not None: encoder_hidden_states, hidden_states = hidden_states.split_with_sizes( [encoder_hidden_states.shape[1], hidden_states.shape[1] - encoder_hidden_states.shape[1]], dim=1 ) hidden_states = attn.to_out[0](hidden_states) hidden_states = attn.to_out[1](hidden_states) encoder_hidden_states = attn.to_add_out(encoder_hidden_states) return hidden_states, encoder_hidden_states else: return hidden_states class BriaAttention(torch.nn.Module, AttentionModuleMixin): _default_processor_cls = BriaAttnProcessor _available_processors = [ BriaAttnProcessor, ] def __init__( self, query_dim: int, heads: int = 8, dim_head: int = 64, dropout: float = 0.0, bias: bool = False, added_kv_proj_dim: int | None = None, added_proj_bias: bool | None = True, out_bias: bool = True, eps: float = 1e-5, out_dim: int = None, context_pre_only: bool | None = None, pre_only: bool = False, elementwise_affine: bool = True, processor=None, ): super().__init__() self.head_dim = dim_head self.inner_dim = out_dim if out_dim is not None else dim_head * heads self.query_dim = query_dim self.use_bias = bias self.dropout = dropout self.out_dim = out_dim if out_dim is not None else query_dim self.context_pre_only = context_pre_only self.pre_only = pre_only self.heads = out_dim // dim_head if out_dim is not None else heads self.added_kv_proj_dim = added_kv_proj_dim self.added_proj_bias = added_proj_bias self.norm_q = torch.nn.RMSNorm(dim_head, eps=eps, elementwise_affine=elementwise_affine) self.norm_k = torch.nn.RMSNorm(dim_head, eps=eps, elementwise_affine=elementwise_affine) self.to_q = torch.nn.Linear(query_dim, self.inner_dim, bias=bias) self.to_k = torch.nn.Linear(query_dim, self.inner_dim, bias=bias) self.to_v = torch.nn.Linear(query_dim, self.inner_dim, bias=bias) if not self.pre_only: self.to_out = torch.nn.ModuleList([]) self.to_out.append(torch.nn.Linear(self.inner_dim, self.out_dim, bias=out_bias)) self.to_out.append(torch.nn.Dropout(dropout)) if added_kv_proj_dim is not None: self.norm_added_q = torch.nn.RMSNorm(dim_head, eps=eps) self.norm_added_k = torch.nn.RMSNorm(dim_head, eps=eps) self.add_q_proj = torch.nn.Linear(added_kv_proj_dim, self.inner_dim, bias=added_proj_bias) self.add_k_proj = torch.nn.Linear(added_kv_proj_dim, self.inner_dim, bias=added_proj_bias) self.add_v_proj = torch.nn.Linear(added_kv_proj_dim, self.inner_dim, bias=added_proj_bias) self.to_add_out = torch.nn.Linear(self.inner_dim, query_dim, bias=out_bias) if processor is None: processor = self._default_processor_cls() self.set_processor(processor) def forward( self, hidden_states: torch.Tensor, encoder_hidden_states: torch.Tensor | None = None, attention_mask: torch.Tensor | None = None, image_rotary_emb: torch.Tensor | None = None, **kwargs, ) -> torch.Tensor: attn_parameters = set(inspect.signature(self.processor.__call__).parameters.keys()) quiet_attn_parameters = {"ip_adapter_masks", "ip_hidden_states"} unused_kwargs = [k for k, _ in kwargs.items() if k not in attn_parameters and k not in quiet_attn_parameters] if len(unused_kwargs) > 0: logger.warning( f"attention_kwargs {unused_kwargs} are not expected by {self.processor.__class__.__name__} and will be ignored." ) kwargs = {k: w for k, w in kwargs.items() if k in attn_parameters} return self.processor(self, hidden_states, encoder_hidden_states, attention_mask, image_rotary_emb, **kwargs) class BriaEmbedND(torch.nn.Module): # modified from https://github.com/black-forest-labs/flux/blob/c00d7c60b085fce8058b9df845e036090873f2ce/src/flux/modules/layers.py#L11 def __init__(self, theta: int, axes_dim: list[int]): super().__init__() self.theta = theta self.axes_dim = axes_dim def forward(self, ids: torch.Tensor) -> torch.Tensor: n_axes = ids.shape[-1] cos_out = [] sin_out = [] pos = ids.float() is_mps = ids.device.type == "mps" freqs_dtype = torch.float32 if is_mps else torch.float64 for i in range(n_axes): cos, sin = get_1d_rotary_pos_embed( self.axes_dim[i], pos[:, i], theta=self.theta, repeat_interleave_real=True, use_real=True, freqs_dtype=freqs_dtype, ) cos_out.append(cos) sin_out.append(sin) freqs_cos = torch.cat(cos_out, dim=-1).to(ids.device) freqs_sin = torch.cat(sin_out, dim=-1).to(ids.device) return freqs_cos, freqs_sin class BriaTimesteps(nn.Module): def __init__( self, num_channels: int, flip_sin_to_cos: bool, downscale_freq_shift: float, scale: int = 1, time_theta=10000 ): super().__init__() self.num_channels = num_channels self.flip_sin_to_cos = flip_sin_to_cos self.downscale_freq_shift = downscale_freq_shift self.scale = scale self.time_theta = time_theta def forward(self, timesteps): t_emb = get_timestep_embedding( timesteps, self.num_channels, flip_sin_to_cos=self.flip_sin_to_cos, downscale_freq_shift=self.downscale_freq_shift, scale=self.scale, max_period=self.time_theta, ) return t_emb class BriaTimestepProjEmbeddings(nn.Module): def __init__(self, embedding_dim, time_theta): super().__init__() self.time_proj = BriaTimesteps( num_channels=256, flip_sin_to_cos=True, downscale_freq_shift=0, time_theta=time_theta ) self.timestep_embedder = TimestepEmbedding(in_channels=256, time_embed_dim=embedding_dim) def forward(self, timestep, dtype): timesteps_proj = self.time_proj(timestep) timesteps_emb = self.timestep_embedder(timesteps_proj.to(dtype=dtype)) # (N, D) return timesteps_emb class BriaPosEmbed(torch.nn.Module): # modified from https://github.com/black-forest-labs/flux/blob/c00d7c60b085fce8058b9df845e036090873f2ce/src/flux/modules/layers.py#L11 def __init__(self, theta: int, axes_dim: list[int]): super().__init__() self.theta = theta self.axes_dim = axes_dim def forward(self, ids: torch.Tensor) -> torch.Tensor: n_axes = ids.shape[-1] cos_out = [] sin_out = [] pos = ids.float() is_mps = ids.device.type == "mps" freqs_dtype = torch.float32 if is_mps else torch.float64 for i in range(n_axes): cos, sin = get_1d_rotary_pos_embed( self.axes_dim[i], pos[:, i], theta=self.theta, repeat_interleave_real=True, use_real=True, freqs_dtype=freqs_dtype, ) cos_out.append(cos) sin_out.append(sin) freqs_cos = torch.cat(cos_out, dim=-1).to(ids.device) freqs_sin = torch.cat(sin_out, dim=-1).to(ids.device) return freqs_cos, freqs_sin @maybe_allow_in_graph class BriaTransformerBlock(nn.Module): def __init__( self, dim: int, num_attention_heads: int, attention_head_dim: int, qk_norm: str = "rms_norm", eps: float = 1e-6 ): super().__init__() self.norm1 = AdaLayerNormZero(dim) self.norm1_context = AdaLayerNormZero(dim) self.attn = BriaAttention( query_dim=dim, added_kv_proj_dim=dim, dim_head=attention_head_dim, heads=num_attention_heads, out_dim=dim, context_pre_only=False, bias=True, processor=BriaAttnProcessor(), eps=eps, ) self.norm2 = nn.LayerNorm(dim, elementwise_affine=False, eps=1e-6) self.ff = FeedForward(dim=dim, dim_out=dim, activation_fn="gelu-approximate") self.norm2_context = nn.LayerNorm(dim, elementwise_affine=False, eps=1e-6) self.ff_context = FeedForward(dim=dim, dim_out=dim, activation_fn="gelu-approximate") def forward( self, hidden_states: torch.Tensor, encoder_hidden_states: torch.Tensor, temb: torch.Tensor, image_rotary_emb: tuple[torch.Tensor, torch.Tensor] | None = None, attention_kwargs: dict[str, Any] | None = None, ) -> tuple[torch.Tensor, torch.Tensor]: norm_hidden_states, gate_msa, shift_mlp, scale_mlp, gate_mlp = self.norm1(hidden_states, emb=temb) norm_encoder_hidden_states, c_gate_msa, c_shift_mlp, c_scale_mlp, c_gate_mlp = self.norm1_context( encoder_hidden_states, emb=temb ) attention_kwargs = attention_kwargs or {} # Attention. attention_outputs = self.attn( hidden_states=norm_hidden_states, encoder_hidden_states=norm_encoder_hidden_states, image_rotary_emb=image_rotary_emb, **attention_kwargs, ) if len(attention_outputs) == 2: attn_output, context_attn_output = attention_outputs elif len(attention_outputs) == 3: attn_output, context_attn_output, ip_attn_output = attention_outputs # Process attention outputs for the `hidden_states`. attn_output = gate_msa.unsqueeze(1) * attn_output hidden_states = hidden_states + attn_output norm_hidden_states = self.norm2(hidden_states) norm_hidden_states = norm_hidden_states * (1 + scale_mlp[:, None]) + shift_mlp[:, None] ff_output = self.ff(norm_hidden_states) ff_output = gate_mlp.unsqueeze(1) * ff_output hidden_states = hidden_states + ff_output if len(attention_outputs) == 3: hidden_states = hidden_states + ip_attn_output # Process attention outputs for the `encoder_hidden_states`. context_attn_output = c_gate_msa.unsqueeze(1) * context_attn_output encoder_hidden_states = encoder_hidden_states + context_attn_output norm_encoder_hidden_states = self.norm2_context(encoder_hidden_states) norm_encoder_hidden_states = norm_encoder_hidden_states * (1 + c_scale_mlp[:, None]) + c_shift_mlp[:, None] context_ff_output = self.ff_context(norm_encoder_hidden_states) encoder_hidden_states = encoder_hidden_states + c_gate_mlp.unsqueeze(1) * context_ff_output if encoder_hidden_states.dtype == torch.float16: encoder_hidden_states = encoder_hidden_states.clip(-65504, 65504) return encoder_hidden_states, hidden_states @maybe_allow_in_graph class BriaSingleTransformerBlock(nn.Module): def __init__(self, dim: int, num_attention_heads: int, attention_head_dim: int, mlp_ratio: float = 4.0): super().__init__() self.mlp_hidden_dim = int(dim * mlp_ratio) self.norm = AdaLayerNormZeroSingle(dim) self.proj_mlp = nn.Linear(dim, self.mlp_hidden_dim) self.act_mlp = nn.GELU(approximate="tanh") self.proj_out = nn.Linear(dim + self.mlp_hidden_dim, dim) processor = BriaAttnProcessor() self.attn = BriaAttention( query_dim=dim, dim_head=attention_head_dim, heads=num_attention_heads, out_dim=dim, bias=True, processor=processor, eps=1e-6, pre_only=True, ) def forward( self, hidden_states: torch.Tensor, encoder_hidden_states: torch.Tensor, temb: torch.Tensor, image_rotary_emb: tuple[torch.Tensor, torch.Tensor] | None = None, attention_kwargs: dict[str, Any] | None = None, ) -> tuple[torch.Tensor, torch.Tensor]: text_seq_len = encoder_hidden_states.shape[1] hidden_states = torch.cat([encoder_hidden_states, hidden_states], dim=1) residual = hidden_states norm_hidden_states, gate = self.norm(hidden_states, emb=temb) mlp_hidden_states = self.act_mlp(self.proj_mlp(norm_hidden_states)) attention_kwargs = attention_kwargs or {} attn_output = self.attn( hidden_states=norm_hidden_states, image_rotary_emb=image_rotary_emb, **attention_kwargs, ) hidden_states = torch.cat([attn_output, mlp_hidden_states], dim=2) gate = gate.unsqueeze(1) hidden_states = gate * self.proj_out(hidden_states) hidden_states = residual + hidden_states if hidden_states.dtype == torch.float16: hidden_states = hidden_states.clip(-65504, 65504) encoder_hidden_states, hidden_states = hidden_states[:, :text_seq_len], hidden_states[:, text_seq_len:] return encoder_hidden_states, hidden_states class BriaTransformer2DModel(ModelMixin, ConfigMixin, PeftAdapterMixin, FromOriginalModelMixin, CacheMixin): """ The Transformer model introduced in Flux. Based on FluxPipeline with several changes: - no pooled embeddings - We use zero padding for prompts - No guidance embedding since this is not a distilled version Reference: https://blackforestlabs.ai/announcing-black-forest-labs/ Parameters: patch_size (`int`): Patch size to turn the input data into small patches. in_channels (`int`, *optional*, defaults to 16): The number of channels in the input. num_layers (`int`, *optional*, defaults to 18): The number of layers of MMDiT blocks to use. num_single_layers (`int`, *optional*, defaults to 18): The number of layers of single DiT blocks to use. attention_head_dim (`int`, *optional*, defaults to 64): The number of channels in each head. num_attention_heads (`int`, *optional*, defaults to 18): The number of heads to use for multi-head attention. joint_attention_dim (`int`, *optional*): The number of `encoder_hidden_states` dimensions to use. pooled_projection_dim (`int`): Number of dimensions to use when projecting the `pooled_projections`. guidance_embeds (`bool`, defaults to False): Whether to use guidance embeddings. """ _supports_gradient_checkpointing = True @register_to_config def __init__( self, patch_size: int = 1, in_channels: int = 64, num_layers: int = 19, num_single_layers: int = 38, attention_head_dim: int = 128, num_attention_heads: int = 24, joint_attention_dim: int = 4096, pooled_projection_dim: int = None, guidance_embeds: bool = False, axes_dims_rope: list[int] = [16, 56, 56], rope_theta=10000, time_theta=10000, ): super().__init__() self.out_channels = in_channels self.inner_dim = self.config.num_attention_heads * self.config.attention_head_dim self.pos_embed = BriaEmbedND(theta=rope_theta, axes_dim=axes_dims_rope) self.time_embed = BriaTimestepProjEmbeddings(embedding_dim=self.inner_dim, time_theta=time_theta) if guidance_embeds: self.guidance_embed = BriaTimestepProjEmbeddings(embedding_dim=self.inner_dim) self.context_embedder = nn.Linear(self.config.joint_attention_dim, self.inner_dim) self.x_embedder = torch.nn.Linear(self.config.in_channels, self.inner_dim) self.transformer_blocks = nn.ModuleList( [ BriaTransformerBlock( dim=self.inner_dim, num_attention_heads=self.config.num_attention_heads, attention_head_dim=self.config.attention_head_dim, ) for i in range(self.config.num_layers) ] ) self.single_transformer_blocks = nn.ModuleList( [ BriaSingleTransformerBlock( dim=self.inner_dim, num_attention_heads=self.config.num_attention_heads, attention_head_dim=self.config.attention_head_dim, ) for i in range(self.config.num_single_layers) ] ) self.norm_out = AdaLayerNormContinuous(self.inner_dim, self.inner_dim, elementwise_affine=False, eps=1e-6) self.proj_out = nn.Linear(self.inner_dim, patch_size * patch_size * self.out_channels, bias=True) self.gradient_checkpointing = False @apply_lora_scale("attention_kwargs") def forward( self, hidden_states: torch.Tensor, encoder_hidden_states: torch.Tensor = None, pooled_projections: torch.Tensor = None, timestep: torch.LongTensor = None, img_ids: torch.Tensor = None, txt_ids: torch.Tensor = None, guidance: torch.Tensor = None, attention_kwargs: dict[str, Any] | None = None, return_dict: bool = True, controlnet_block_samples=None, controlnet_single_block_samples=None, ) -> tuple[torch.Tensor] | Transformer2DModelOutput: """ The [`BriaTransformer2DModel`] forward method. Args: hidden_states (`torch.FloatTensor` of shape `(batch size, channel, height, width)`): Input `hidden_states`. encoder_hidden_states (`torch.FloatTensor` of shape `(batch size, sequence_len, embed_dims)`): Conditional embeddings (embeddings computed from the input conditions such as prompts) to use. pooled_projections (`torch.FloatTensor` of shape `(batch_size, projection_dim)`): Embeddings projected from the embeddings of input conditions. timestep ( `torch.LongTensor`): Used to indicate denoising step. block_controlnet_hidden_states: (`list` of `torch.Tensor`): A list of tensors that if specified are added to the residuals of transformer blocks. attention_kwargs (`dict`, *optional*): A kwargs dictionary that if specified is passed along to the `AttentionProcessor` as defined under `self.processor` in [diffusers.models.attention_processor](https://github.com/huggingface/diffusers/blob/main/src/diffusers/models/attention_processor.py). return_dict (`bool`, *optional*, defaults to `True`): Whether or not to return a [`~models.transformer_2d.Transformer2DModelOutput`] instead of a plain tuple. Returns: If `return_dict` is True, an [`~models.transformer_2d.Transformer2DModelOutput`] is returned, otherwise a `tuple` where the first element is the sample tensor. """ hidden_states = self.x_embedder(hidden_states) timestep = timestep.to(hidden_states.dtype) if guidance is not None: guidance = guidance.to(hidden_states.dtype) else: guidance = None temb = self.time_embed(timestep, dtype=hidden_states.dtype) if guidance: temb += self.guidance_embed(guidance, dtype=hidden_states.dtype) encoder_hidden_states = self.context_embedder(encoder_hidden_states) if len(txt_ids.shape) == 3: txt_ids = txt_ids[0] if len(img_ids.shape) == 3: img_ids = img_ids[0] ids = torch.cat((txt_ids, img_ids), dim=0) image_rotary_emb = self.pos_embed(ids) for index_block, block in enumerate(self.transformer_blocks): if torch.is_grad_enabled() and self.gradient_checkpointing: encoder_hidden_states, hidden_states = self._gradient_checkpointing_func( block, hidden_states, encoder_hidden_states, temb, image_rotary_emb, attention_kwargs, ) else: encoder_hidden_states, hidden_states = block( hidden_states=hidden_states, encoder_hidden_states=encoder_hidden_states, temb=temb, image_rotary_emb=image_rotary_emb, ) # controlnet residual if controlnet_block_samples is not None: interval_control = len(self.transformer_blocks) / len(controlnet_block_samples) interval_control = int(np.ceil(interval_control)) hidden_states = hidden_states + controlnet_block_samples[index_block // interval_control] for index_block, block in enumerate(self.single_transformer_blocks): if torch.is_grad_enabled() and self.gradient_checkpointing: encoder_hidden_states, hidden_states = self._gradient_checkpointing_func( block, hidden_states, encoder_hidden_states, temb, image_rotary_emb, attention_kwargs, ) else: encoder_hidden_states, hidden_states = block( hidden_states=hidden_states, encoder_hidden_states=encoder_hidden_states, temb=temb, image_rotary_emb=image_rotary_emb, ) # controlnet residual if controlnet_single_block_samples is not None: interval_control = len(self.single_transformer_blocks) / len(controlnet_single_block_samples) interval_control = int(np.ceil(interval_control)) hidden_states[:, encoder_hidden_states.shape[1] :, ...] = ( hidden_states[:, encoder_hidden_states.shape[1] :, ...] + controlnet_single_block_samples[index_block // interval_control] ) hidden_states = self.norm_out(hidden_states, temb) output = self.proj_out(hidden_states) if not return_dict: return (output,) return Transformer2DModelOutput(sample=output)
{ "repo_id": "huggingface/diffusers", "file_path": "src/diffusers/models/transformers/transformer_bria.py", "license": "Apache License 2.0", "lines": 595, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
function_complex
huggingface/diffusers:src/diffusers/pipelines/bria/pipeline_bria.py
from typing import Any, Callable import numpy as np import torch from transformers import ( CLIPImageProcessor, CLIPVisionModelWithProjection, T5EncoderModel, T5TokenizerFast, ) from ...image_processor import VaeImageProcessor from ...loaders import FluxLoraLoaderMixin from ...models import AutoencoderKL from ...models.transformers.transformer_bria import BriaTransformer2DModel from ...pipelines import DiffusionPipeline from ...pipelines.bria.pipeline_output import BriaPipelineOutput from ...pipelines.flux.pipeline_flux import calculate_shift, retrieve_timesteps from ...schedulers import ( DDIMScheduler, EulerAncestralDiscreteScheduler, FlowMatchEulerDiscreteScheduler, KarrasDiffusionSchedulers, ) from ...utils import ( USE_PEFT_BACKEND, is_torch_xla_available, logging, replace_example_docstring, scale_lora_layers, unscale_lora_layers, ) from ...utils.torch_utils import randn_tensor if is_torch_xla_available(): import torch_xla.core.xla_model as xm XLA_AVAILABLE = True else: XLA_AVAILABLE = False logger = logging.get_logger(__name__) # pylint: disable=invalid-name EXAMPLE_DOC_STRING = """ Examples: ```py >>> import torch >>> from diffusers import BriaPipeline >>> pipe = BriaPipeline.from_pretrained("briaai/BRIA-3.2", torch_dtype=torch.bfloat16) >>> pipe.to("cuda") # BRIA's T5 text encoder is sensitive to precision. We need to cast it to bfloat16 and keep the final layer in float32. >>> pipe.text_encoder = pipe.text_encoder.to(dtype=torch.bfloat16) >>> for block in pipe.text_encoder.encoder.block: ... block.layer[-1].DenseReluDense.wo.to(dtype=torch.float32) # BRIA's VAE is not supported in mixed precision, so we use float32. >>> if pipe.vae.config.shift_factor == 0: ... pipe.vae.to(dtype=torch.float32) >>> prompt = "Photorealistic food photography of a stack of fluffy pancakes on a white plate, with maple syrup being poured over them. On top of the pancakes are the words 'BRIA 3.2' in bold, yellow, 3D letters. The background is dark and out of focus." >>> image = pipe(prompt).images[0] >>> image.save("bria.png") ``` """ def is_ng_none(negative_prompt): return ( negative_prompt is None or negative_prompt == "" or (isinstance(negative_prompt, list) and negative_prompt[0] is None) or (type(negative_prompt) == list and negative_prompt[0] == "") ) def get_original_sigmas(num_train_timesteps=1000, num_inference_steps=1000): timesteps = np.linspace(1, num_train_timesteps, num_train_timesteps, dtype=np.float32)[::-1].copy() sigmas = timesteps / num_train_timesteps inds = [int(ind) for ind in np.linspace(0, num_train_timesteps - 1, num_inference_steps)] new_sigmas = sigmas[inds] return new_sigmas class BriaPipeline(DiffusionPipeline): r""" Based on FluxPipeline with several changes: - no pooled embeddings - We use zero padding for prompts - No guidance embedding since this is not a distilled version Args: transformer ([`BriaTransformer2DModel`]): Conditional Transformer (MMDiT) architecture to denoise the encoded image latents. scheduler ([`FlowMatchEulerDiscreteScheduler`]): A scheduler to be used in combination with `transformer` to denoise the encoded image latents. vae ([`AutoencoderKL`]): Variational Auto-Encoder (VAE) Model to encode and decode images to and from latent representations. text_encoder ([`T5EncoderModel`]): Frozen text-encoder. Bria uses [T5](https://huggingface.co/docs/transformers/model_doc/t5#transformers.T5EncoderModel), specifically the [t5-v1_1-xxl](https://huggingface.co/google/t5-v1_1-xxl) variant. tokenizer (`T5TokenizerFast`): Tokenizer of class [T5Tokenizer](https://huggingface.co/docs/transformers/model_doc/t5#transformers.T5Tokenizer). """ model_cpu_offload_seq = "text_encoder->text_encoder_2->image_encoder->transformer->vae" _optional_components = ["image_encoder", "feature_extractor"] _callback_tensor_inputs = ["latents", "prompt_embeds"] def __init__( self, transformer: BriaTransformer2DModel, scheduler: FlowMatchEulerDiscreteScheduler | KarrasDiffusionSchedulers, vae: AutoencoderKL, text_encoder: T5EncoderModel, tokenizer: T5TokenizerFast, image_encoder: CLIPVisionModelWithProjection = None, feature_extractor: CLIPImageProcessor = None, ): self.register_modules( vae=vae, text_encoder=text_encoder, tokenizer=tokenizer, transformer=transformer, scheduler=scheduler, image_encoder=image_encoder, feature_extractor=feature_extractor, ) self.vae_scale_factor = ( 2 ** (len(self.vae.config.block_out_channels)) if hasattr(self, "vae") and self.vae is not None else 16 ) self.image_processor = VaeImageProcessor(vae_scale_factor=self.vae_scale_factor) self.default_sample_size = 64 # due to patchify=> 128,128 => res of 1k,1k if self.vae.config.shift_factor is None: self.vae.config.shift_factor = 0 self.vae.to(dtype=torch.float32) def encode_prompt( self, prompt: str | list[str], device: torch.device | None = None, num_images_per_prompt: int = 1, do_classifier_free_guidance: bool = True, negative_prompt: str | list[str] | None = None, prompt_embeds: torch.FloatTensor | None = None, negative_prompt_embeds: torch.FloatTensor | None = None, max_sequence_length: int = 128, lora_scale: float | None = None, ): r""" Args: prompt (`str` or `list[str]`, *optional*): prompt to be encoded device: (`torch.device`): torch device num_images_per_prompt (`int`): number of images that should be generated per prompt do_classifier_free_guidance (`bool`): whether to use classifier free guidance or not negative_prompt (`str` or `list[str]`, *optional*): The prompt or prompts not to guide the image generation. If not defined, one has to pass `negative_prompt_embeds` instead. Ignored when not using guidance (i.e., ignored if `guidance_scale` is less than `1`). prompt_embeds (`torch.FloatTensor`, *optional*): Pre-generated text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt weighting. If not provided, text embeddings will be generated from `prompt` input argument. negative_prompt_embeds (`torch.FloatTensor`, *optional*): Pre-generated negative text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt weighting. If not provided, negative_prompt_embeds will be generated from `negative_prompt` input argument. """ device = device or self._execution_device # set lora scale so that monkey patched LoRA # function of text encoder can correctly access it if lora_scale is not None and isinstance(self, FluxLoraLoaderMixin): self._lora_scale = lora_scale # dynamically adjust the LoRA scale if self.text_encoder is not None and USE_PEFT_BACKEND: scale_lora_layers(self.text_encoder, lora_scale) prompt = [prompt] if isinstance(prompt, str) else prompt if prompt is not None: batch_size = len(prompt) else: batch_size = prompt_embeds.shape[0] if prompt_embeds is None: prompt_embeds = self._get_t5_prompt_embeds( prompt=prompt, num_images_per_prompt=num_images_per_prompt, max_sequence_length=max_sequence_length, device=device, ) if do_classifier_free_guidance and negative_prompt_embeds is None: if not is_ng_none(negative_prompt): negative_prompt = ( batch_size * [negative_prompt] if isinstance(negative_prompt, str) else negative_prompt ) if prompt is not None and type(prompt) is not type(negative_prompt): raise TypeError( f"`negative_prompt` should be the same type to `prompt`, but got {type(negative_prompt)} !=" f" {type(prompt)}." ) elif batch_size != len(negative_prompt): raise ValueError( f"`negative_prompt`: {negative_prompt} has batch size {len(negative_prompt)}, but `prompt`:" f" {prompt} has batch size {batch_size}. Please make sure that passed `negative_prompt` matches" " the batch size of `prompt`." ) negative_prompt_embeds = self._get_t5_prompt_embeds( prompt=negative_prompt, num_images_per_prompt=num_images_per_prompt, max_sequence_length=max_sequence_length, device=device, ) else: negative_prompt_embeds = torch.zeros_like(prompt_embeds) if self.text_encoder is not None: if isinstance(self, FluxLoraLoaderMixin) and USE_PEFT_BACKEND: # Retrieve the original scale by scaling back the LoRA layers unscale_lora_layers(self.text_encoder, lora_scale) text_ids = torch.zeros(batch_size, prompt_embeds.shape[1], 3).to(device=device) text_ids = text_ids.repeat(num_images_per_prompt, 1, 1) return prompt_embeds, negative_prompt_embeds, text_ids @property def guidance_scale(self): return self._guidance_scale # here `guidance_scale` is defined analog to the guidance weight `w` of equation (2) # of the Imagen paper: https://huggingface.co/papers/2205.11487 . `guidance_scale = 1` # corresponds to doing no classifier free guidance. @property def do_classifier_free_guidance(self): return self._guidance_scale > 1 @property def attention_kwargs(self): return self._attention_kwargs @attention_kwargs.setter def attention_kwargs(self, value): self._attention_kwargs = value @property def num_timesteps(self): return self._num_timesteps @property def interrupt(self): return self._interrupt def check_inputs( self, prompt, height, width, negative_prompt=None, prompt_embeds=None, negative_prompt_embeds=None, callback_on_step_end_tensor_inputs=None, max_sequence_length=None, ): if height % (self.vae_scale_factor * 2) != 0 or width % (self.vae_scale_factor * 2) != 0: logger.warning( f"`height` and `width` have to be divisible by {self.vae_scale_factor * 2} but are {height} and {width}. Dimensions will be resized accordingly" ) if callback_on_step_end_tensor_inputs is not None and not all( k in self._callback_tensor_inputs for k in callback_on_step_end_tensor_inputs ): raise ValueError( f"`callback_on_step_end_tensor_inputs` has to be in {self._callback_tensor_inputs}, but found {[k for k in callback_on_step_end_tensor_inputs if k not in self._callback_tensor_inputs]}" ) if prompt is not None and prompt_embeds is not None: raise ValueError( f"Cannot forward both `prompt`: {prompt} and `prompt_embeds`: {prompt_embeds}. Please make sure to" " only forward one of the two." ) elif prompt is None and prompt_embeds is None: raise ValueError( "Provide either `prompt` or `prompt_embeds`. Cannot leave both `prompt` and `prompt_embeds` undefined." ) elif prompt is not None and (not isinstance(prompt, str) and not isinstance(prompt, list)): raise ValueError(f"`prompt` has to be of type `str` or `list` but is {type(prompt)}") if negative_prompt is not None and negative_prompt_embeds is not None: raise ValueError( f"Cannot forward both `negative_prompt`: {negative_prompt} and `negative_prompt_embeds`:" f" {negative_prompt_embeds}. Please make sure to only forward one of the two." ) if prompt_embeds is not None and negative_prompt_embeds is not None: if prompt_embeds.shape != negative_prompt_embeds.shape: raise ValueError( "`prompt_embeds` and `negative_prompt_embeds` must have the same shape when passed directly, but" f" got: `prompt_embeds` {prompt_embeds.shape} != `negative_prompt_embeds`" f" {negative_prompt_embeds.shape}." ) if max_sequence_length is not None and max_sequence_length > 512: raise ValueError(f"`max_sequence_length` cannot be greater than 512 but is {max_sequence_length}") def _get_t5_prompt_embeds( self, prompt: str | list[str] = None, num_images_per_prompt: int = 1, max_sequence_length: int = 128, device: torch.device | None = None, ): tokenizer = self.tokenizer text_encoder = self.text_encoder device = device or text_encoder.device prompt = [prompt] if isinstance(prompt, str) else prompt batch_size = len(prompt) prompt_embeds_list = [] for p in prompt: text_inputs = tokenizer( p, # padding="max_length", max_length=max_sequence_length, truncation=True, add_special_tokens=True, return_tensors="pt", ) text_input_ids = text_inputs.input_ids 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[:, 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}" ) prompt_embeds = text_encoder(text_input_ids.to(device))[0] # Concat zeros to max_sequence b, seq_len, dim = prompt_embeds.shape if seq_len < max_sequence_length: padding = torch.zeros( (b, max_sequence_length - seq_len, dim), dtype=prompt_embeds.dtype, device=prompt_embeds.device ) prompt_embeds = torch.concat([prompt_embeds, padding], dim=1) prompt_embeds_list.append(prompt_embeds) prompt_embeds = torch.concat(prompt_embeds_list, dim=0) prompt_embeds = prompt_embeds.to(device=device) # duplicate text embeddings and attention mask for each generation per prompt, using mps friendly method prompt_embeds = prompt_embeds.repeat(1, num_images_per_prompt, 1) prompt_embeds = prompt_embeds.view(batch_size * num_images_per_prompt, max_sequence_length, -1) prompt_embeds = prompt_embeds.to(dtype=self.transformer.dtype) return prompt_embeds def prepare_latents( self, batch_size, num_channels_latents, height, width, dtype, device, generator, latents=None, ): # VAE applies 8x compression on images but we must also account for packing which requires # latent height and width to be divisible by 2. height = 2 * (int(height) // self.vae_scale_factor) width = 2 * (int(width) // self.vae_scale_factor) shape = (batch_size, num_channels_latents, height, width) if latents is not None: latent_image_ids = self._prepare_latent_image_ids(batch_size, height // 2, width // 2, device, dtype) return latents.to(device=device, dtype=dtype), latent_image_ids if isinstance(generator, list) and len(generator) != batch_size: raise ValueError( f"You have passed a list of generators of length {len(generator)}, but requested an effective batch" f" size of {batch_size}. Make sure the batch size matches the length of the generators." ) latents = randn_tensor(shape, generator=generator, device=device, dtype=dtype) latents = self._pack_latents(latents, batch_size, num_channels_latents, height, width) latent_image_ids = self._prepare_latent_image_ids(batch_size, height // 2, width // 2, device, dtype) return latents, latent_image_ids @staticmethod def _pack_latents(latents, batch_size, num_channels_latents, height, width): latents = latents.view(batch_size, num_channels_latents, height // 2, 2, width // 2, 2) latents = latents.permute(0, 2, 4, 1, 3, 5) latents = latents.reshape(batch_size, (height // 2) * (width // 2), num_channels_latents * 4) return latents @staticmethod def _unpack_latents(latents, height, width, vae_scale_factor): batch_size, num_patches, channels = latents.shape height = height // vae_scale_factor width = width // vae_scale_factor latents = latents.view(batch_size, height, width, channels // 4, 2, 2) latents = latents.permute(0, 3, 1, 4, 2, 5) latents = latents.reshape(batch_size, channels // (2 * 2), height * 2, width * 2) return latents @staticmethod def _prepare_latent_image_ids(batch_size, height, width, device, dtype): latent_image_ids = torch.zeros(height, width, 3) latent_image_ids[..., 1] = latent_image_ids[..., 1] + torch.arange(height)[:, None] latent_image_ids[..., 2] = latent_image_ids[..., 2] + torch.arange(width)[None, :] latent_image_id_height, latent_image_id_width, latent_image_id_channels = latent_image_ids.shape latent_image_ids = latent_image_ids.repeat(batch_size, 1, 1, 1) latent_image_ids = latent_image_ids.reshape( batch_size, latent_image_id_height * latent_image_id_width, latent_image_id_channels ) return latent_image_ids.to(device=device, dtype=dtype) @torch.no_grad() @replace_example_docstring(EXAMPLE_DOC_STRING) def __call__( self, prompt: str | list[str] = None, height: int | None = None, width: int | None = None, num_inference_steps: int = 30, timesteps: list[int] = None, guidance_scale: float = 5, negative_prompt: str | list[str] | None = None, num_images_per_prompt: int | None = 1, generator: torch.Generator | list[torch.Generator] | None = None, latents: torch.FloatTensor | None = None, prompt_embeds: torch.FloatTensor | None = None, negative_prompt_embeds: torch.FloatTensor | None = None, output_type: str | None = "pil", return_dict: bool = True, attention_kwargs: dict[str, Any] | None = None, callback_on_step_end: Callable[[int, int], None] | None = None, callback_on_step_end_tensor_inputs: list[str] = ["latents"], max_sequence_length: int = 128, clip_value: None | float = None, normalize: bool = False, ): r""" Function invoked when calling the pipeline for generation. Args: prompt (`str` or `list[str]`, *optional*): The prompt or prompts to guide the image generation. If not defined, one has to pass `prompt_embeds`. instead. height (`int`, *optional*, defaults to self.unet.config.sample_size * self.vae_scale_factor): The height in pixels of the generated image. This is set to 1024 by default for the best results. width (`int`, *optional*, defaults to self.unet.config.sample_size * self.vae_scale_factor): The width in pixels of the generated image. This is set to 1024 by default for the best results. num_inference_steps (`int`, *optional*, defaults to 50): The number of denoising steps. More denoising steps usually lead to a higher quality image at the expense of slower inference. timesteps (`list[int]`, *optional*): Custom timesteps to use for the denoising process with schedulers which support a `timesteps` argument in their `set_timesteps` method. If not defined, the default behavior when `num_inference_steps` is passed will be used. Must be in descending order. guidance_scale (`float`, *optional*, defaults to 5.0): <<<<<<< HEAD Guidance scale as defined in [Classifier-Free Diffusion Guidance](https://arxiv.org/abs/2207.12598). `guidance_scale` is defined as `w` of equation 2. of [Imagen Paper](https://arxiv.org/pdf/2205.11487.pdf). Guidance scale is enabled by setting `guidance_scale > 1`. Higher guidance scale encourages to generate images that are closely linked to the text `prompt`, usually at the expense of lower image quality. negative_prompt (`str` or `list[str]`, *optional*): ======= Guidance scale as defined in [Classifier-Free Diffusion Guidance](https://huggingface.co/papers/2207.12598). `guidance_scale` is defined as `w` of equation 2. of [Imagen Paper](https://huggingface.co/papers/2205.11487). Guidance scale is enabled by setting `guidance_scale > 1`. Higher guidance scale encourages to generate images that are closely linked to the text `prompt`, usually at the expense of lower image quality. negative_prompt (`str` or `list[str]`, *optional*): >>>>>>> main The prompt or prompts not to guide the image generation. If not defined, one has to pass `negative_prompt_embeds` instead. Ignored when not using guidance (i.e., ignored if `guidance_scale` is less than `1`). num_images_per_prompt (`int`, *optional*, defaults to 1): The number of images to generate per prompt. generator (`torch.Generator` or `list[torch.Generator]`, *optional*): One or a list of [torch generator(s)](https://pytorch.org/docs/stable/generated/torch.Generator.html) to make generation deterministic. latents (`torch.FloatTensor`, *optional*): Pre-generated noisy latents, sampled from a Gaussian distribution, to be used as inputs for image generation. Can be used to tweak the same generation with different prompts. If not provided, a latents tensor will be generated by sampling using the supplied random `generator`. prompt_embeds (`torch.FloatTensor`, *optional*): Pre-generated text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt weighting. If not provided, text embeddings will be generated from `prompt` input argument. negative_prompt_embeds (`torch.FloatTensor`, *optional*): Pre-generated negative text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt weighting. If not provided, negative_prompt_embeds will be generated from `negative_prompt` input argument. output_type (`str`, *optional*, defaults to `"pil"`): The output format of the generate image. Choose between [PIL](https://pillow.readthedocs.io/en/stable/): `PIL.Image.Image` or `np.array`. return_dict (`bool`, *optional*, defaults to `True`): Whether or not to return a [`~pipelines.bria.BriaPipelineOutput`] instead of a plain tuple. attention_kwargs (`dict`, *optional*): A kwargs dictionary that if specified is passed along to the `AttentionProcessor` as defined under `self.processor` in [diffusers.models.attention_processor](https://github.com/huggingface/diffusers/blob/main/src/diffusers/models/attention_processor.py). callback_on_step_end (`Callable`, *optional*): A function that calls at the end of each denoising steps during the inference. The function is called with the following arguments: `callback_on_step_end(self: DiffusionPipeline, step: int, timestep: int, callback_kwargs: Dict)`. `callback_kwargs` will include a list of all tensors as specified by `callback_on_step_end_tensor_inputs`. callback_on_step_end_tensor_inputs (`list`, *optional*): The list of tensor inputs for the `callback_on_step_end` function. The tensors specified in the list will be passed as `callback_kwargs` argument. You will only be able to include variables listed in the `._callback_tensor_inputs` attribute of your pipeline class. max_sequence_length (`int` defaults to 256): Maximum sequence length to use with the `prompt`. Examples: Returns: [`~pipelines.bria.BriaPipelineOutput`] or `tuple`: [`~pipelines.bria.BriaPipelineOutput`] if `return_dict` is True, otherwise a `tuple`. When returning a tuple, the first element is a list with the generated images. """ height = height or self.default_sample_size * self.vae_scale_factor width = width or self.default_sample_size * self.vae_scale_factor # 1. Check inputs. Raise error if not correct self.check_inputs( prompt=prompt, height=height, width=width, prompt_embeds=prompt_embeds, callback_on_step_end_tensor_inputs=callback_on_step_end_tensor_inputs, max_sequence_length=max_sequence_length, ) self._guidance_scale = guidance_scale self.attention_kwargs = attention_kwargs self._interrupt = False # 2. Define call parameters if prompt is not None and isinstance(prompt, str): batch_size = 1 elif prompt is not None and isinstance(prompt, list): batch_size = len(prompt) else: batch_size = prompt_embeds.shape[0] device = self._execution_device lora_scale = self.attention_kwargs.get("scale", None) if self.attention_kwargs is not None else None (prompt_embeds, negative_prompt_embeds, text_ids) = self.encode_prompt( prompt=prompt, negative_prompt=negative_prompt, do_classifier_free_guidance=self.do_classifier_free_guidance, prompt_embeds=prompt_embeds, negative_prompt_embeds=negative_prompt_embeds, device=device, num_images_per_prompt=num_images_per_prompt, max_sequence_length=max_sequence_length, lora_scale=lora_scale, ) if self.do_classifier_free_guidance: prompt_embeds = torch.cat([negative_prompt_embeds, prompt_embeds], dim=0) # 5. Prepare latent variables num_channels_latents = self.transformer.config.in_channels // 4 # due to patch=2, we devide by 4 latents, latent_image_ids = self.prepare_latents( batch_size * num_images_per_prompt, num_channels_latents, height, width, prompt_embeds.dtype, device, generator, latents, ) if ( isinstance(self.scheduler, FlowMatchEulerDiscreteScheduler) and self.scheduler.config["use_dynamic_shifting"] ): sigmas = np.linspace(1.0, 1 / num_inference_steps, num_inference_steps) image_seq_len = latents.shape[1] mu = calculate_shift( image_seq_len, self.scheduler.config.base_image_seq_len, self.scheduler.config.max_image_seq_len, self.scheduler.config.base_shift, self.scheduler.config.max_shift, ) timesteps, num_inference_steps = retrieve_timesteps( self.scheduler, num_inference_steps, device, timesteps, sigmas, mu=mu, ) else: # 4. Prepare timesteps # Sample from training sigmas if isinstance(self.scheduler, DDIMScheduler) or isinstance( self.scheduler, EulerAncestralDiscreteScheduler ): timesteps, num_inference_steps = retrieve_timesteps( self.scheduler, num_inference_steps, device, None, None ) else: sigmas = get_original_sigmas( num_train_timesteps=self.scheduler.config.num_train_timesteps, num_inference_steps=num_inference_steps, ) timesteps, num_inference_steps = retrieve_timesteps( self.scheduler, num_inference_steps, device, timesteps, sigmas=sigmas ) num_warmup_steps = max(len(timesteps) - num_inference_steps * self.scheduler.order, 0) self._num_timesteps = len(timesteps) if len(latent_image_ids.shape) == 3: latent_image_ids = latent_image_ids[0] if len(text_ids.shape) == 3: text_ids = text_ids[0] # 6. Denoising loop with self.progress_bar(total=num_inference_steps) as progress_bar: for i, t in enumerate(timesteps): if self.interrupt: continue # expand the latents if we are doing classifier free guidance latent_model_input = torch.cat([latents] * 2) if self.do_classifier_free_guidance else latents if type(self.scheduler) != FlowMatchEulerDiscreteScheduler: latent_model_input = self.scheduler.scale_model_input(latent_model_input, t) # broadcast to batch dimension in a way that's compatible with ONNX/Core ML timestep = t.expand(latent_model_input.shape[0]) # This is predicts "v" from flow-matching or eps from diffusion noise_pred = self.transformer( hidden_states=latent_model_input, timestep=timestep, encoder_hidden_states=prompt_embeds, attention_kwargs=self.attention_kwargs, return_dict=False, txt_ids=text_ids, img_ids=latent_image_ids, )[0] # perform guidance if self.do_classifier_free_guidance: noise_pred_uncond, noise_pred_text = noise_pred.chunk(2) cfg_noise_pred_text = noise_pred_text.std() noise_pred = noise_pred_uncond + self.guidance_scale * (noise_pred_text - noise_pred_uncond) if normalize: noise_pred = noise_pred * (0.7 * (cfg_noise_pred_text / noise_pred.std())) + 0.3 * noise_pred if clip_value: assert clip_value > 0 noise_pred = noise_pred.clip(-clip_value, clip_value) # compute the previous noisy sample x_t -> x_t-1 latents_dtype = latents.dtype latents = self.scheduler.step(noise_pred, t, latents, return_dict=False)[0] if latents.dtype != latents_dtype: if torch.backends.mps.is_available(): # some platforms (eg. apple mps) misbehave due to a pytorch bug: https://github.com/pytorch/pytorch/pull/99272 latents = latents.to(latents_dtype) if callback_on_step_end is not None: callback_kwargs = {} for k in callback_on_step_end_tensor_inputs: callback_kwargs[k] = locals()[k] callback_outputs = callback_on_step_end(self, i, t, callback_kwargs) latents = callback_outputs.pop("latents", latents) prompt_embeds = callback_outputs.pop("prompt_embeds", prompt_embeds) negative_prompt_embeds = callback_outputs.pop("negative_prompt_embeds", negative_prompt_embeds) # call the callback, if provided if i == len(timesteps) - 1 or ((i + 1) > num_warmup_steps and (i + 1) % self.scheduler.order == 0): progress_bar.update() if XLA_AVAILABLE: xm.mark_step() if output_type == "latent": image = latents else: latents = self._unpack_latents(latents, height, width, self.vae_scale_factor) latents = (latents.to(dtype=torch.float32) / self.vae.config.scaling_factor) + self.vae.config.shift_factor image = self.vae.decode(latents.to(dtype=self.vae.dtype), return_dict=False)[0] image = self.image_processor.postprocess(image, output_type=output_type) # Offload all models self.maybe_free_model_hooks() if not return_dict: return (image,) return BriaPipelineOutput(images=image)
{ "repo_id": "huggingface/diffusers", "file_path": "src/diffusers/pipelines/bria/pipeline_bria.py", "license": "Apache License 2.0", "lines": 634, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
function_complex
huggingface/diffusers:src/diffusers/pipelines/bria/pipeline_output.py
from dataclasses import dataclass import numpy as np import PIL.Image from ...utils import BaseOutput @dataclass class BriaPipelineOutput(BaseOutput): """ Output class for Bria pipelines. Args: images (`list[PIL.Image.Image]` or `np.ndarray`) list of denoised PIL images of length `batch_size` or numpy array of shape `(batch_size, height, width, num_channels)`. PIL images or numpy array present the denoised images of the diffusion pipeline. """ images: list[PIL.Image.Image] | np.ndarray
{ "repo_id": "huggingface/diffusers", "file_path": "src/diffusers/pipelines/bria/pipeline_output.py", "license": "Apache License 2.0", "lines": 14, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
huggingface/diffusers:tests/models/transformers/test_models_transformer_bria.py
# coding=utf-8 # Copyright 2025 HuggingFace Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest import torch from diffusers import BriaTransformer2DModel from diffusers.models.attention_processor import FluxIPAdapterJointAttnProcessor2_0 from diffusers.models.embeddings import ImageProjection from ...testing_utils import enable_full_determinism, torch_device from ..test_modeling_common import LoraHotSwappingForModelTesterMixin, ModelTesterMixin, TorchCompileTesterMixin enable_full_determinism() def create_bria_ip_adapter_state_dict(model): # "ip_adapter" (cross-attention weights) ip_cross_attn_state_dict = {} key_id = 0 for name in model.attn_processors.keys(): if name.startswith("single_transformer_blocks"): continue joint_attention_dim = model.config["joint_attention_dim"] hidden_size = model.config["num_attention_heads"] * model.config["attention_head_dim"] sd = FluxIPAdapterJointAttnProcessor2_0( hidden_size=hidden_size, cross_attention_dim=joint_attention_dim, scale=1.0 ).state_dict() ip_cross_attn_state_dict.update( { f"{key_id}.to_k_ip.weight": sd["to_k_ip.0.weight"], f"{key_id}.to_v_ip.weight": sd["to_v_ip.0.weight"], f"{key_id}.to_k_ip.bias": sd["to_k_ip.0.bias"], f"{key_id}.to_v_ip.bias": sd["to_v_ip.0.bias"], } ) key_id += 1 # "image_proj" (ImageProjection layer weights) image_projection = ImageProjection( cross_attention_dim=model.config["joint_attention_dim"], image_embed_dim=model.config["pooled_projection_dim"], num_image_text_embeds=4, ) ip_image_projection_state_dict = {} sd = image_projection.state_dict() ip_image_projection_state_dict.update( { "proj.weight": sd["image_embeds.weight"], "proj.bias": sd["image_embeds.bias"], "norm.weight": sd["norm.weight"], "norm.bias": sd["norm.bias"], } ) del sd ip_state_dict = {} ip_state_dict.update({"image_proj": ip_image_projection_state_dict, "ip_adapter": ip_cross_attn_state_dict}) return ip_state_dict class BriaTransformerTests(ModelTesterMixin, unittest.TestCase): model_class = BriaTransformer2DModel main_input_name = "hidden_states" # We override the items here because the transformer under consideration is small. model_split_percents = [0.8, 0.7, 0.7] # Skip setting testing with default: AttnProcessor uses_custom_attn_processor = True @property def dummy_input(self): batch_size = 1 num_latent_channels = 4 num_image_channels = 3 height = width = 4 sequence_length = 48 embedding_dim = 32 hidden_states = torch.randn((batch_size, height * width, num_latent_channels)).to(torch_device) encoder_hidden_states = torch.randn((batch_size, sequence_length, embedding_dim)).to(torch_device) text_ids = torch.randn((sequence_length, num_image_channels)).to(torch_device) image_ids = torch.randn((height * width, num_image_channels)).to(torch_device) timestep = torch.tensor([1.0]).to(torch_device).expand(batch_size) return { "hidden_states": hidden_states, "encoder_hidden_states": encoder_hidden_states, "img_ids": image_ids, "txt_ids": text_ids, "timestep": timestep, } @property def input_shape(self): return (16, 4) @property def output_shape(self): return (16, 4) def prepare_init_args_and_inputs_for_common(self): init_dict = { "patch_size": 1, "in_channels": 4, "num_layers": 1, "num_single_layers": 1, "attention_head_dim": 8, "num_attention_heads": 2, "joint_attention_dim": 32, "pooled_projection_dim": None, "axes_dims_rope": [0, 4, 4], } inputs_dict = self.dummy_input return init_dict, inputs_dict def test_deprecated_inputs_img_txt_ids_3d(self): init_dict, inputs_dict = self.prepare_init_args_and_inputs_for_common() model = self.model_class(**init_dict) model.to(torch_device) model.eval() with torch.no_grad(): output_1 = model(**inputs_dict).to_tuple()[0] # update inputs_dict with txt_ids and img_ids as 3d tensors (deprecated) text_ids_3d = inputs_dict["txt_ids"].unsqueeze(0) image_ids_3d = inputs_dict["img_ids"].unsqueeze(0) assert text_ids_3d.ndim == 3, "text_ids_3d should be a 3d tensor" assert image_ids_3d.ndim == 3, "img_ids_3d should be a 3d tensor" inputs_dict["txt_ids"] = text_ids_3d inputs_dict["img_ids"] = image_ids_3d with torch.no_grad(): output_2 = model(**inputs_dict).to_tuple()[0] self.assertEqual(output_1.shape, output_2.shape) self.assertTrue( torch.allclose(output_1, output_2, atol=1e-5), msg="output with deprecated inputs (img_ids and txt_ids as 3d torch tensors) are not equal as them as 2d inputs", ) def test_gradient_checkpointing_is_applied(self): expected_set = {"BriaTransformer2DModel"} super().test_gradient_checkpointing_is_applied(expected_set=expected_set) class BriaTransformerCompileTests(TorchCompileTesterMixin, unittest.TestCase): model_class = BriaTransformer2DModel def prepare_init_args_and_inputs_for_common(self): return BriaTransformerTests().prepare_init_args_and_inputs_for_common() class BriaTransformerLoRAHotSwapTests(LoraHotSwappingForModelTesterMixin, unittest.TestCase): model_class = BriaTransformer2DModel def prepare_init_args_and_inputs_for_common(self): return BriaTransformerTests().prepare_init_args_and_inputs_for_common()
{ "repo_id": "huggingface/diffusers", "file_path": "tests/models/transformers/test_models_transformer_bria.py", "license": "Apache License 2.0", "lines": 142, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
test
huggingface/diffusers:tests/pipelines/bria/test_pipeline_bria.py
# Copyright 2024 Bria AI and The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import gc import tempfile import unittest import numpy as np import torch from huggingface_hub import hf_hub_download from transformers import AutoConfig, T5EncoderModel, T5TokenizerFast from diffusers import ( AutoencoderKL, BriaTransformer2DModel, FlowMatchEulerDiscreteScheduler, ) from diffusers.pipelines.bria import BriaPipeline # from ..test_pipelines_common import PipelineTesterMixin, check_qkv_fused_layers_exist from tests.pipelines.test_pipelines_common import PipelineTesterMixin, to_np from ...testing_utils import ( backend_empty_cache, enable_full_determinism, numpy_cosine_similarity_distance, require_torch_accelerator, slow, torch_device, ) enable_full_determinism() class BriaPipelineFastTests(PipelineTesterMixin, unittest.TestCase): pipeline_class = BriaPipeline params = frozenset(["prompt", "height", "width", "guidance_scale", "prompt_embeds"]) batch_params = frozenset(["prompt"]) test_xformers_attention = False # there is no xformers processor for Flux test_xformers_attention = False test_layerwise_casting = True test_group_offloading = True def get_dummy_components(self): torch.manual_seed(0) transformer = BriaTransformer2DModel( patch_size=1, in_channels=16, num_layers=1, num_single_layers=1, attention_head_dim=8, num_attention_heads=2, joint_attention_dim=32, pooled_projection_dim=None, axes_dims_rope=[0, 4, 4], ) torch.manual_seed(0) vae = AutoencoderKL( act_fn="silu", block_out_channels=(32,), in_channels=3, out_channels=3, down_block_types=["DownEncoderBlock2D"], up_block_types=["UpDecoderBlock2D"], latent_channels=4, sample_size=32, shift_factor=0, scaling_factor=0.13025, use_post_quant_conv=True, use_quant_conv=True, force_upcast=False, ) scheduler = FlowMatchEulerDiscreteScheduler() torch.manual_seed(0) config = AutoConfig.from_pretrained("hf-internal-testing/tiny-random-t5") text_encoder = T5EncoderModel(config) tokenizer = T5TokenizerFast.from_pretrained("hf-internal-testing/tiny-random-t5") components = { "scheduler": scheduler, "text_encoder": text_encoder, "tokenizer": tokenizer, "transformer": transformer, "vae": vae, "image_encoder": None, "feature_extractor": None, } return components def get_dummy_inputs(self, device, seed=0): if str(device).startswith("mps"): generator = torch.manual_seed(seed) else: generator = torch.Generator(device="cpu").manual_seed(seed) inputs = { "prompt": "A painting of a squirrel eating a burger", "negative_prompt": "bad, ugly", "generator": generator, "num_inference_steps": 2, "guidance_scale": 5.0, "height": 16, "width": 16, "max_sequence_length": 48, "output_type": "np", } return inputs def test_encode_prompt_works_in_isolation(self): pass def test_bria_different_prompts(self): pipe = self.pipeline_class(**self.get_dummy_components()).to(torch_device) inputs = self.get_dummy_inputs(torch_device) output_same_prompt = pipe(**inputs).images[0] inputs = self.get_dummy_inputs(torch_device) inputs["prompt"] = "a different prompt" output_different_prompts = pipe(**inputs).images[0] max_diff = np.abs(output_same_prompt - output_different_prompts).max() assert max_diff > 1e-6 def test_image_output_shape(self): pipe = self.pipeline_class(**self.get_dummy_components()).to(torch_device) inputs = self.get_dummy_inputs(torch_device) height_width_pairs = [(32, 32), (72, 57)] for height, width in height_width_pairs: expected_height = height - height % (pipe.vae_scale_factor * 2) expected_width = width - width % (pipe.vae_scale_factor * 2) inputs.update({"height": height, "width": width}) image = pipe(**inputs).images[0] output_height, output_width, _ = image.shape assert (output_height, output_width) == (expected_height, expected_width) @unittest.skipIf(torch_device not in ["cuda", "xpu"], reason="float16 requires CUDA or XPU") @require_torch_accelerator def test_save_load_float16(self, expected_max_diff=1e-2): components = self.get_dummy_components() for name, module in components.items(): if hasattr(module, "half"): components[name] = module.to(torch_device).half() pipe = self.pipeline_class(**components) for component in pipe.components.values(): if hasattr(component, "set_default_attn_processor"): component.set_default_attn_processor() pipe.to(torch_device) pipe.set_progress_bar_config(disable=None) inputs = self.get_dummy_inputs(torch_device) output = pipe(**inputs)[0] with tempfile.TemporaryDirectory() as tmpdir: pipe.save_pretrained(tmpdir) pipe_loaded = self.pipeline_class.from_pretrained(tmpdir, torch_dtype=torch.float16) for component in pipe_loaded.components.values(): if hasattr(component, "set_default_attn_processor"): component.set_default_attn_processor() pipe_loaded.to(torch_device) pipe_loaded.set_progress_bar_config(disable=None) for name, component in pipe_loaded.components.items(): if name == "vae": continue if hasattr(component, "dtype"): self.assertTrue( component.dtype == torch.float16, f"`{name}.dtype` switched from `float16` to {component.dtype} after loading.", ) inputs = self.get_dummy_inputs(torch_device) output_loaded = pipe_loaded(**inputs)[0] max_diff = np.abs(to_np(output) - to_np(output_loaded)).max() self.assertLess( max_diff, expected_max_diff, "The output of the fp16 pipeline changed after saving and loading." ) def test_bria_image_output_shape(self): pipe = self.pipeline_class(**self.get_dummy_components()).to(torch_device) inputs = self.get_dummy_inputs(torch_device) height_width_pairs = [(16, 16), (32, 32), (64, 64)] for height, width in height_width_pairs: expected_height = height - height % (pipe.vae_scale_factor * 2) expected_width = width - width % (pipe.vae_scale_factor * 2) inputs.update({"height": height, "width": width}) image = pipe(**inputs).images[0] output_height, output_width, _ = image.shape assert (output_height, output_width) == (expected_height, expected_width) def test_to_dtype(self): components = self.get_dummy_components() pipe = self.pipeline_class(**components) pipe.set_progress_bar_config(disable=None) model_dtypes = [component.dtype for component in components.values() if hasattr(component, "dtype")] self.assertTrue([dtype == torch.float32 for dtype in model_dtypes] == [True, True, True]) def test_torch_dtype_dict(self): components = self.get_dummy_components() pipe = self.pipeline_class(**components) with tempfile.TemporaryDirectory() as tmpdirname: pipe.save_pretrained(tmpdirname) torch_dtype_dict = {"transformer": torch.bfloat16, "default": torch.float16} loaded_pipe = self.pipeline_class.from_pretrained(tmpdirname, torch_dtype=torch_dtype_dict) self.assertEqual(loaded_pipe.transformer.dtype, torch.bfloat16) self.assertEqual(loaded_pipe.text_encoder.dtype, torch.float16) self.assertEqual(loaded_pipe.vae.dtype, torch.float16) with tempfile.TemporaryDirectory() as tmpdirname: pipe.save_pretrained(tmpdirname) torch_dtype_dict = {"default": torch.float16} loaded_pipe = self.pipeline_class.from_pretrained(tmpdirname, torch_dtype=torch_dtype_dict) self.assertEqual(loaded_pipe.transformer.dtype, torch.float16) self.assertEqual(loaded_pipe.text_encoder.dtype, torch.float16) self.assertEqual(loaded_pipe.vae.dtype, torch.float16) @slow @require_torch_accelerator class BriaPipelineSlowTests(unittest.TestCase): pipeline_class = BriaPipeline repo_id = "briaai/BRIA-3.2" def setUp(self): super().setUp() gc.collect() backend_empty_cache(torch_device) def tearDown(self): super().tearDown() gc.collect() backend_empty_cache(torch_device) def get_inputs(self, device, seed=0): generator = torch.Generator(device="cpu").manual_seed(seed) prompt_embeds = torch.load( hf_hub_download(repo_id="diffusers/test-slices", repo_type="dataset", filename="flux/prompt_embeds.pt") ).to(torch_device) return { "prompt_embeds": prompt_embeds, "num_inference_steps": 2, "guidance_scale": 0.0, "max_sequence_length": 256, "output_type": "np", "generator": generator, } def test_bria_inference_bf16(self): pipe = self.pipeline_class.from_pretrained( self.repo_id, torch_dtype=torch.bfloat16, text_encoder=None, tokenizer=None ) pipe.to(torch_device) inputs = self.get_inputs(torch_device) image = pipe(**inputs).images[0] image_slice = image[0, :10, :10].flatten() expected_slice = np.array( [ 0.59729785, 0.6153719, 0.595112, 0.5884763, 0.59366125, 0.5795311, 0.58325, 0.58449626, 0.57737637, 0.58432233, 0.5867875, 0.57824117, 0.5819089, 0.5830988, 0.57730293, 0.57647324, 0.5769151, 0.57312685, 0.57926565, 0.5823928, 0.57783926, 0.57162863, 0.575649, 0.5745547, 0.5740556, 0.5799735, 0.57799566, 0.5715559, 0.5771242, 0.5773058, ], dtype=np.float32, ) max_diff = numpy_cosine_similarity_distance(expected_slice, image_slice) self.assertLess(max_diff, 1e-4, f"Image slice is different from expected slice: {max_diff:.4f}")
{ "repo_id": "huggingface/diffusers", "file_path": "tests/pipelines/bria/test_pipeline_bria.py", "license": "Apache License 2.0", "lines": 271, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
test
huggingface/diffusers:tests/pipelines/qwenimage/test_qwenimage_edit.py
# Copyright 2025 The HuggingFace Team. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest import numpy as np import pytest import torch from PIL import Image from transformers import Qwen2_5_VLConfig, Qwen2_5_VLForConditionalGeneration, Qwen2Tokenizer, Qwen2VLProcessor from diffusers import ( AutoencoderKLQwenImage, FlowMatchEulerDiscreteScheduler, QwenImageEditPipeline, QwenImageTransformer2DModel, ) from ...testing_utils import enable_full_determinism, torch_device from ..pipeline_params import TEXT_TO_IMAGE_PARAMS from ..test_pipelines_common import PipelineTesterMixin, to_np enable_full_determinism() class QwenImageEditPipelineFastTests(PipelineTesterMixin, unittest.TestCase): pipeline_class = QwenImageEditPipeline params = TEXT_TO_IMAGE_PARAMS - {"cross_attention_kwargs"} batch_params = frozenset(["prompt", "image"]) image_params = frozenset(["image"]) image_latents_params = frozenset(["latents"]) required_optional_params = frozenset( [ "num_inference_steps", "generator", "latents", "return_dict", "callback_on_step_end", "callback_on_step_end_tensor_inputs", ] ) supports_dduf = False test_xformers_attention = False test_layerwise_casting = True test_group_offloading = True def get_dummy_components(self): tiny_ckpt_id = "hf-internal-testing/tiny-random-Qwen2VLForConditionalGeneration" torch.manual_seed(0) transformer = QwenImageTransformer2DModel( patch_size=2, in_channels=16, out_channels=4, num_layers=2, attention_head_dim=16, num_attention_heads=3, joint_attention_dim=16, guidance_embeds=False, axes_dims_rope=(8, 4, 4), ) torch.manual_seed(0) z_dim = 4 vae = AutoencoderKLQwenImage( base_dim=z_dim * 6, z_dim=z_dim, dim_mult=[1, 2, 4], num_res_blocks=1, temperal_downsample=[False, True], latents_mean=[0.0] * z_dim, latents_std=[1.0] * z_dim, ) torch.manual_seed(0) scheduler = FlowMatchEulerDiscreteScheduler() torch.manual_seed(0) config = Qwen2_5_VLConfig( text_config={ "hidden_size": 16, "intermediate_size": 16, "num_hidden_layers": 2, "num_attention_heads": 2, "num_key_value_heads": 2, "rope_scaling": { "mrope_section": [1, 1, 2], "rope_type": "default", "type": "default", }, "rope_theta": 1000000.0, }, vision_config={ "depth": 2, "hidden_size": 16, "intermediate_size": 16, "num_heads": 2, "out_hidden_size": 16, }, hidden_size=16, vocab_size=152064, vision_end_token_id=151653, vision_start_token_id=151652, vision_token_id=151654, ) text_encoder = Qwen2_5_VLForConditionalGeneration(config).eval() tokenizer = Qwen2Tokenizer.from_pretrained(tiny_ckpt_id) components = { "transformer": transformer, "vae": vae, "scheduler": scheduler, "text_encoder": text_encoder, "tokenizer": tokenizer, "processor": Qwen2VLProcessor.from_pretrained(tiny_ckpt_id), } return components def get_dummy_inputs(self, device, seed=0): if str(device).startswith("mps"): generator = torch.manual_seed(seed) else: generator = torch.Generator(device=device).manual_seed(seed) inputs = { "prompt": "dance monkey", "image": Image.new("RGB", (32, 32)), "negative_prompt": "bad quality", "generator": generator, "num_inference_steps": 2, "true_cfg_scale": 1.0, "height": 32, "width": 32, "max_sequence_length": 16, "output_type": "pt", } return inputs def test_inference(self): device = "cpu" components = self.get_dummy_components() pipe = self.pipeline_class(**components) pipe.to(device) pipe.set_progress_bar_config(disable=None) inputs = self.get_dummy_inputs(device) image = pipe(**inputs).images generated_image = image[0] self.assertEqual(generated_image.shape, (3, 32, 32)) # fmt: off expected_slice = torch.tensor([0.5637, 0.6341, 0.6001, 0.5620, 0.5794, 0.5498, 0.5757, 0.6389, 0.4174, 0.3597, 0.5649, 0.4894, 0.4969, 0.5255, 0.4083, 0.4986]) # fmt: on generated_slice = generated_image.flatten() generated_slice = torch.cat([generated_slice[:8], generated_slice[-8:]]) self.assertTrue(torch.allclose(generated_slice, expected_slice, atol=5e-3)) def test_inference_batch_single_identical(self): self._test_inference_batch_single_identical(batch_size=3, expected_max_diff=1e-1) def test_attention_slicing_forward_pass( self, test_max_difference=True, test_mean_pixel_difference=True, expected_max_diff=1e-3 ): if not self.test_attention_slicing: return components = self.get_dummy_components() pipe = self.pipeline_class(**components) for component in pipe.components.values(): if hasattr(component, "set_default_attn_processor"): component.set_default_attn_processor() pipe.to(torch_device) pipe.set_progress_bar_config(disable=None) generator_device = "cpu" inputs = self.get_dummy_inputs(generator_device) output_without_slicing = pipe(**inputs)[0] pipe.enable_attention_slicing(slice_size=1) inputs = self.get_dummy_inputs(generator_device) output_with_slicing1 = pipe(**inputs)[0] pipe.enable_attention_slicing(slice_size=2) inputs = self.get_dummy_inputs(generator_device) output_with_slicing2 = pipe(**inputs)[0] if test_max_difference: max_diff1 = np.abs(to_np(output_with_slicing1) - to_np(output_without_slicing)).max() max_diff2 = np.abs(to_np(output_with_slicing2) - to_np(output_without_slicing)).max() self.assertLess( max(max_diff1, max_diff2), expected_max_diff, "Attention slicing should not affect the inference results", ) def test_vae_tiling(self, expected_diff_max: float = 0.2): generator_device = "cpu" components = self.get_dummy_components() pipe = self.pipeline_class(**components) pipe.to("cpu") pipe.set_progress_bar_config(disable=None) # Without tiling inputs = self.get_dummy_inputs(generator_device) inputs["height"] = inputs["width"] = 128 output_without_tiling = pipe(**inputs)[0] # With tiling pipe.vae.enable_tiling( tile_sample_min_height=96, tile_sample_min_width=96, tile_sample_stride_height=64, tile_sample_stride_width=64, ) inputs = self.get_dummy_inputs(generator_device) inputs["height"] = inputs["width"] = 128 output_with_tiling = pipe(**inputs)[0] self.assertLess( (to_np(output_without_tiling) - to_np(output_with_tiling)).max(), expected_diff_max, "VAE tiling should not affect the inference results", ) @pytest.mark.xfail(condition=True, reason="Preconfigured embeddings need to be revisited.", strict=True) def test_encode_prompt_works_in_isolation(self, extra_required_param_value_dict=None, atol=1e-4, rtol=1e-4): super().test_encode_prompt_works_in_isolation(extra_required_param_value_dict, atol, rtol)
{ "repo_id": "huggingface/diffusers", "file_path": "tests/pipelines/qwenimage/test_qwenimage_edit.py", "license": "Apache License 2.0", "lines": 208, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
test
huggingface/diffusers:src/diffusers/pipelines/qwenimage/pipeline_qwenimage_edit.py
# Copyright 2025 Qwen-Image Team and The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import inspect import math from typing import Any, Callable import numpy as np import torch from transformers import Qwen2_5_VLForConditionalGeneration, Qwen2Tokenizer, Qwen2VLProcessor from ...image_processor import PipelineImageInput, VaeImageProcessor from ...loaders import QwenImageLoraLoaderMixin from ...models import AutoencoderKLQwenImage, QwenImageTransformer2DModel from ...schedulers import FlowMatchEulerDiscreteScheduler from ...utils import deprecate, is_torch_xla_available, logging, replace_example_docstring from ...utils.torch_utils import randn_tensor from ..pipeline_utils import DiffusionPipeline from .pipeline_output import QwenImagePipelineOutput if is_torch_xla_available(): import torch_xla.core.xla_model as xm XLA_AVAILABLE = True else: XLA_AVAILABLE = False logger = logging.get_logger(__name__) # pylint: disable=invalid-name EXAMPLE_DOC_STRING = """ Examples: ```py >>> import torch >>> from PIL import Image >>> from diffusers import QwenImageEditPipeline >>> from diffusers.utils import load_image >>> pipe = QwenImageEditPipeline.from_pretrained("Qwen/Qwen-Image-Edit", torch_dtype=torch.bfloat16) >>> pipe.to("cuda") >>> image = load_image( ... "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/yarn-art-pikachu.png" ... ).convert("RGB") >>> prompt = ( ... "Make Pikachu hold a sign that says 'Qwen Edit is awesome', yarn art style, detailed, vibrant colors" ... ) >>> # Depending on the variant being used, the pipeline call will slightly vary. >>> # Refer to the pipeline documentation for more details. >>> image = pipe(image, prompt, num_inference_steps=50).images[0] >>> image.save("qwenimage_edit.png") ``` """ # Copied from diffusers.pipelines.qwenimage.pipeline_qwenimage.calculate_shift def calculate_shift( image_seq_len, base_seq_len: int = 256, max_seq_len: int = 4096, base_shift: float = 0.5, max_shift: float = 1.15, ): m = (max_shift - base_shift) / (max_seq_len - base_seq_len) b = base_shift - m * base_seq_len mu = image_seq_len * m + b return mu # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.retrieve_timesteps def retrieve_timesteps( scheduler, num_inference_steps: int | None = None, device: str | torch.device | None = None, timesteps: list[int] | None = None, sigmas: list[float] | None = None, **kwargs, ): r""" Calls the scheduler's `set_timesteps` method and retrieves timesteps from the scheduler after the call. Handles custom timesteps. Any kwargs will be supplied to `scheduler.set_timesteps`. Args: scheduler (`SchedulerMixin`): The scheduler to get timesteps from. num_inference_steps (`int`): The number of diffusion steps used when generating samples with a pre-trained model. If used, `timesteps` must be `None`. device (`str` or `torch.device`, *optional*): The device to which the timesteps should be moved to. If `None`, the timesteps are not moved. timesteps (`list[int]`, *optional*): Custom timesteps used to override the timestep spacing strategy of the scheduler. If `timesteps` is passed, `num_inference_steps` and `sigmas` must be `None`. sigmas (`list[float]`, *optional*): Custom sigmas used to override the timestep spacing strategy of the scheduler. If `sigmas` is passed, `num_inference_steps` and `timesteps` must be `None`. Returns: `tuple[torch.Tensor, int]`: A tuple where the first element is the timestep schedule from the scheduler and the second element is the number of inference steps. """ if timesteps is not None and sigmas is not None: raise ValueError("Only one of `timesteps` or `sigmas` can be passed. Please choose one to set custom values") if timesteps is not None: accepts_timesteps = "timesteps" in set(inspect.signature(scheduler.set_timesteps).parameters.keys()) if not accepts_timesteps: raise ValueError( f"The current scheduler class {scheduler.__class__}'s `set_timesteps` does not support custom" f" timestep schedules. Please check whether you are using the correct scheduler." ) scheduler.set_timesteps(timesteps=timesteps, device=device, **kwargs) timesteps = scheduler.timesteps num_inference_steps = len(timesteps) elif sigmas is not None: accept_sigmas = "sigmas" in set(inspect.signature(scheduler.set_timesteps).parameters.keys()) if not accept_sigmas: raise ValueError( f"The current scheduler class {scheduler.__class__}'s `set_timesteps` does not support custom" f" sigmas schedules. Please check whether you are using the correct scheduler." ) scheduler.set_timesteps(sigmas=sigmas, device=device, **kwargs) timesteps = scheduler.timesteps num_inference_steps = len(timesteps) else: scheduler.set_timesteps(num_inference_steps, device=device, **kwargs) timesteps = scheduler.timesteps return timesteps, num_inference_steps # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion_img2img.retrieve_latents def retrieve_latents( encoder_output: torch.Tensor, generator: torch.Generator | None = None, sample_mode: str = "sample" ): if hasattr(encoder_output, "latent_dist") and sample_mode == "sample": return encoder_output.latent_dist.sample(generator) elif hasattr(encoder_output, "latent_dist") and sample_mode == "argmax": return encoder_output.latent_dist.mode() elif hasattr(encoder_output, "latents"): return encoder_output.latents else: raise AttributeError("Could not access latents of provided encoder_output") def calculate_dimensions(target_area, ratio): width = math.sqrt(target_area * ratio) height = width / ratio width = round(width / 32) * 32 height = round(height / 32) * 32 return width, height, None class QwenImageEditPipeline(DiffusionPipeline, QwenImageLoraLoaderMixin): r""" The Qwen-Image-Edit pipeline for image editing. Args: transformer ([`QwenImageTransformer2DModel`]): Conditional Transformer (MMDiT) architecture to denoise the encoded image latents. scheduler ([`FlowMatchEulerDiscreteScheduler`]): A scheduler to be used in combination with `transformer` to denoise the encoded image latents. vae ([`AutoencoderKL`]): Variational Auto-Encoder (VAE) Model to encode and decode images to and from latent representations. text_encoder ([`Qwen2.5-VL-7B-Instruct`]): [Qwen2.5-VL-7B-Instruct](https://huggingface.co/Qwen/Qwen2.5-VL-7B-Instruct), specifically the [Qwen2.5-VL-7B-Instruct](https://huggingface.co/Qwen/Qwen2.5-VL-7B-Instruct) variant. tokenizer (`QwenTokenizer`): Tokenizer of class [CLIPTokenizer](https://huggingface.co/docs/transformers/en/model_doc/clip#transformers.CLIPTokenizer). """ model_cpu_offload_seq = "text_encoder->transformer->vae" _callback_tensor_inputs = ["latents", "prompt_embeds"] def __init__( self, scheduler: FlowMatchEulerDiscreteScheduler, vae: AutoencoderKLQwenImage, text_encoder: Qwen2_5_VLForConditionalGeneration, tokenizer: Qwen2Tokenizer, processor: Qwen2VLProcessor, transformer: QwenImageTransformer2DModel, ): super().__init__() self.register_modules( vae=vae, text_encoder=text_encoder, tokenizer=tokenizer, processor=processor, transformer=transformer, scheduler=scheduler, ) self.vae_scale_factor = 2 ** len(self.vae.temperal_downsample) if getattr(self, "vae", None) else 8 self.latent_channels = self.vae.config.z_dim if getattr(self, "vae", None) else 16 # QwenImage latents are turned into 2x2 patches and packed. This means the latent width and height has to be divisible # by the patch size. So the vae scale factor is multiplied by the patch size to account for this self.image_processor = VaeImageProcessor(vae_scale_factor=self.vae_scale_factor * 2) self.tokenizer_max_length = 1024 self.prompt_template_encode = "<|im_start|>system\nDescribe the key features of the input image (color, shape, size, texture, objects, background), then explain how the user's text instruction should alter or modify the image. Generate a new image that meets the user's requirements while maintaining consistency with the original input where appropriate.<|im_end|>\n<|im_start|>user\n<|vision_start|><|image_pad|><|vision_end|>{}<|im_end|>\n<|im_start|>assistant\n" self.prompt_template_encode_start_idx = 64 self.default_sample_size = 128 # Copied from diffusers.pipelines.qwenimage.pipeline_qwenimage.QwenImagePipeline._extract_masked_hidden def _extract_masked_hidden(self, hidden_states: torch.Tensor, mask: torch.Tensor): bool_mask = mask.bool() valid_lengths = bool_mask.sum(dim=1) selected = hidden_states[bool_mask] split_result = torch.split(selected, valid_lengths.tolist(), dim=0) return split_result def _get_qwen_prompt_embeds( self, prompt: str | list[str] = None, image: torch.Tensor | None = None, device: torch.device | None = None, dtype: torch.dtype | None = None, ): device = device or self._execution_device dtype = dtype or self.text_encoder.dtype prompt = [prompt] if isinstance(prompt, str) else prompt template = self.prompt_template_encode drop_idx = self.prompt_template_encode_start_idx txt = [template.format(e) for e in prompt] model_inputs = self.processor( text=txt, images=image, padding=True, return_tensors="pt", ).to(device) outputs = self.text_encoder( input_ids=model_inputs.input_ids, attention_mask=model_inputs.attention_mask, pixel_values=model_inputs.pixel_values, image_grid_thw=model_inputs.image_grid_thw, output_hidden_states=True, ) hidden_states = outputs.hidden_states[-1] split_hidden_states = self._extract_masked_hidden(hidden_states, model_inputs.attention_mask) split_hidden_states = [e[drop_idx:] for e in split_hidden_states] attn_mask_list = [torch.ones(e.size(0), dtype=torch.long, device=e.device) for e in split_hidden_states] max_seq_len = max([e.size(0) for e in split_hidden_states]) prompt_embeds = torch.stack( [torch.cat([u, u.new_zeros(max_seq_len - u.size(0), u.size(1))]) for u in split_hidden_states] ) encoder_attention_mask = torch.stack( [torch.cat([u, u.new_zeros(max_seq_len - u.size(0))]) for u in attn_mask_list] ) prompt_embeds = prompt_embeds.to(dtype=dtype, device=device) return prompt_embeds, encoder_attention_mask def encode_prompt( self, prompt: str | list[str], image: torch.Tensor | None = None, device: torch.device | None = None, num_images_per_prompt: int = 1, prompt_embeds: torch.Tensor | None = None, prompt_embeds_mask: torch.Tensor | None = None, max_sequence_length: int = 1024, ): r""" Args: prompt (`str` or `list[str]`, *optional*): prompt to be encoded image (`torch.Tensor`, *optional*): image to be encoded device: (`torch.device`): torch device num_images_per_prompt (`int`): number of images that should be generated per prompt prompt_embeds (`torch.Tensor`, *optional*): Pre-generated text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt weighting. If not provided, text embeddings will be generated from `prompt` input argument. """ device = device or self._execution_device prompt = [prompt] if isinstance(prompt, str) else prompt batch_size = len(prompt) if prompt_embeds is None else prompt_embeds.shape[0] if prompt_embeds is None: prompt_embeds, prompt_embeds_mask = self._get_qwen_prompt_embeds(prompt, image, device) _, seq_len, _ = prompt_embeds.shape prompt_embeds = prompt_embeds.repeat(1, num_images_per_prompt, 1) prompt_embeds = prompt_embeds.view(batch_size * num_images_per_prompt, seq_len, -1) prompt_embeds_mask = prompt_embeds_mask.repeat(1, num_images_per_prompt, 1) prompt_embeds_mask = prompt_embeds_mask.view(batch_size * num_images_per_prompt, seq_len) if prompt_embeds_mask is not None and prompt_embeds_mask.all(): prompt_embeds_mask = None return prompt_embeds, prompt_embeds_mask def check_inputs( self, prompt, height, width, negative_prompt=None, prompt_embeds=None, negative_prompt_embeds=None, prompt_embeds_mask=None, negative_prompt_embeds_mask=None, callback_on_step_end_tensor_inputs=None, max_sequence_length=None, ): if height % (self.vae_scale_factor * 2) != 0 or width % (self.vae_scale_factor * 2) != 0: logger.warning( f"`height` and `width` have to be divisible by {self.vae_scale_factor * 2} but are {height} and {width}. Dimensions will be resized accordingly" ) if callback_on_step_end_tensor_inputs is not None and not all( k in self._callback_tensor_inputs for k in callback_on_step_end_tensor_inputs ): raise ValueError( f"`callback_on_step_end_tensor_inputs` has to be in {self._callback_tensor_inputs}, but found {[k for k in callback_on_step_end_tensor_inputs if k not in self._callback_tensor_inputs]}" ) if prompt is not None and prompt_embeds is not None: raise ValueError( f"Cannot forward both `prompt`: {prompt} and `prompt_embeds`: {prompt_embeds}. Please make sure to" " only forward one of the two." ) elif prompt is None and prompt_embeds is None: raise ValueError( "Provide either `prompt` or `prompt_embeds`. Cannot leave both `prompt` and `prompt_embeds` undefined." ) elif prompt is not None and (not isinstance(prompt, str) and not isinstance(prompt, list)): raise ValueError(f"`prompt` has to be of type `str` or `list` but is {type(prompt)}") if negative_prompt is not None and negative_prompt_embeds is not None: raise ValueError( f"Cannot forward both `negative_prompt`: {negative_prompt} and `negative_prompt_embeds`:" f" {negative_prompt_embeds}. Please make sure to only forward one of the two." ) if prompt_embeds is not None and prompt_embeds_mask is None: raise ValueError( "If `prompt_embeds` are provided, `prompt_embeds_mask` also have to be passed. Make sure to generate `prompt_embeds_mask` from the same text encoder that was used to generate `prompt_embeds`." ) if negative_prompt_embeds is not None and negative_prompt_embeds_mask is None: raise ValueError( "If `negative_prompt_embeds` are provided, `negative_prompt_embeds_mask` also have to be passed. Make sure to generate `negative_prompt_embeds_mask` from the same text encoder that was used to generate `negative_prompt_embeds`." ) if max_sequence_length is not None and max_sequence_length > 1024: raise ValueError(f"`max_sequence_length` cannot be greater than 1024 but is {max_sequence_length}") @staticmethod # Copied from diffusers.pipelines.qwenimage.pipeline_qwenimage.QwenImagePipeline._pack_latents def _pack_latents(latents, batch_size, num_channels_latents, height, width): latents = latents.view(batch_size, num_channels_latents, height // 2, 2, width // 2, 2) latents = latents.permute(0, 2, 4, 1, 3, 5) latents = latents.reshape(batch_size, (height // 2) * (width // 2), num_channels_latents * 4) return latents @staticmethod # Copied from diffusers.pipelines.qwenimage.pipeline_qwenimage.QwenImagePipeline._unpack_latents def _unpack_latents(latents, height, width, vae_scale_factor): batch_size, num_patches, channels = latents.shape # VAE applies 8x compression on images but we must also account for packing which requires # latent height and width to be divisible by 2. height = 2 * (int(height) // (vae_scale_factor * 2)) width = 2 * (int(width) // (vae_scale_factor * 2)) latents = latents.view(batch_size, height // 2, width // 2, channels // 4, 2, 2) latents = latents.permute(0, 3, 1, 4, 2, 5) latents = latents.reshape(batch_size, channels // (2 * 2), 1, height, width) return latents def _encode_vae_image(self, image: torch.Tensor, generator: torch.Generator): if isinstance(generator, list): image_latents = [ retrieve_latents(self.vae.encode(image[i : i + 1]), generator=generator[i], sample_mode="argmax") for i in range(image.shape[0]) ] image_latents = torch.cat(image_latents, dim=0) else: image_latents = retrieve_latents(self.vae.encode(image), generator=generator, sample_mode="argmax") latents_mean = ( torch.tensor(self.vae.config.latents_mean) .view(1, self.latent_channels, 1, 1, 1) .to(image_latents.device, image_latents.dtype) ) latents_std = ( torch.tensor(self.vae.config.latents_std) .view(1, self.latent_channels, 1, 1, 1) .to(image_latents.device, image_latents.dtype) ) image_latents = (image_latents - latents_mean) / latents_std return image_latents def enable_vae_slicing(self): r""" Enable sliced VAE decoding. When this option is enabled, the VAE will split the input tensor in slices to compute decoding in several steps. This is useful to save some memory and allow larger batch sizes. """ depr_message = f"Calling `enable_vae_slicing()` on a `{self.__class__.__name__}` is deprecated and this method will be removed in a future version. Please use `pipe.vae.enable_slicing()`." deprecate( "enable_vae_slicing", "0.40.0", depr_message, ) self.vae.enable_slicing() def disable_vae_slicing(self): r""" Disable sliced VAE decoding. If `enable_vae_slicing` was previously enabled, this method will go back to computing decoding in one step. """ depr_message = f"Calling `disable_vae_slicing()` on a `{self.__class__.__name__}` is deprecated and this method will be removed in a future version. Please use `pipe.vae.disable_slicing()`." deprecate( "disable_vae_slicing", "0.40.0", depr_message, ) self.vae.disable_slicing() def enable_vae_tiling(self): r""" Enable tiled VAE decoding. When this option is enabled, the VAE will split the input tensor into tiles to compute decoding and encoding in several steps. This is useful for saving a large amount of memory and to allow processing larger images. """ depr_message = f"Calling `enable_vae_tiling()` on a `{self.__class__.__name__}` is deprecated and this method will be removed in a future version. Please use `pipe.vae.enable_tiling()`." deprecate( "enable_vae_tiling", "0.40.0", depr_message, ) self.vae.enable_tiling() def disable_vae_tiling(self): r""" Disable tiled VAE decoding. If `enable_vae_tiling` was previously enabled, this method will go back to computing decoding in one step. """ depr_message = f"Calling `disable_vae_tiling()` on a `{self.__class__.__name__}` is deprecated and this method will be removed in a future version. Please use `pipe.vae.disable_tiling()`." deprecate( "disable_vae_tiling", "0.40.0", depr_message, ) self.vae.disable_tiling() def prepare_latents( self, image, batch_size, num_channels_latents, height, width, dtype, device, generator, latents=None, ): # VAE applies 8x compression on images but we must also account for packing which requires # latent height and width to be divisible by 2. height = 2 * (int(height) // (self.vae_scale_factor * 2)) width = 2 * (int(width) // (self.vae_scale_factor * 2)) shape = (batch_size, 1, num_channels_latents, height, width) image_latents = None if image is not None: image = image.to(device=device, dtype=dtype) if image.shape[1] != self.latent_channels: image_latents = self._encode_vae_image(image=image, generator=generator) else: image_latents = image if batch_size > image_latents.shape[0] and batch_size % image_latents.shape[0] == 0: # expand init_latents for batch_size additional_image_per_prompt = batch_size // image_latents.shape[0] image_latents = torch.cat([image_latents] * additional_image_per_prompt, dim=0) elif batch_size > image_latents.shape[0] and batch_size % image_latents.shape[0] != 0: raise ValueError( f"Cannot duplicate `image` of batch size {image_latents.shape[0]} to {batch_size} text prompts." ) else: image_latents = torch.cat([image_latents], dim=0) image_latent_height, image_latent_width = image_latents.shape[3:] image_latents = self._pack_latents( image_latents, batch_size, num_channels_latents, image_latent_height, image_latent_width ) if isinstance(generator, list) and len(generator) != batch_size: raise ValueError( f"You have passed a list of generators of length {len(generator)}, but requested an effective batch" f" size of {batch_size}. Make sure the batch size matches the length of the generators." ) if latents is None: latents = randn_tensor(shape, generator=generator, device=device, dtype=dtype) latents = self._pack_latents(latents, batch_size, num_channels_latents, height, width) else: latents = latents.to(device=device, dtype=dtype) return latents, image_latents @property def guidance_scale(self): return self._guidance_scale @property def attention_kwargs(self): return self._attention_kwargs @property def num_timesteps(self): return self._num_timesteps @property def current_timestep(self): return self._current_timestep @property def interrupt(self): return self._interrupt @torch.no_grad() @replace_example_docstring(EXAMPLE_DOC_STRING) def __call__( self, image: PipelineImageInput | None = None, prompt: str | list[str] = None, negative_prompt: str | list[str] = None, true_cfg_scale: float = 4.0, height: int | None = None, width: int | None = None, num_inference_steps: int = 50, sigmas: list[float] | None = None, guidance_scale: float | None = None, num_images_per_prompt: int = 1, generator: torch.Generator | list[torch.Generator] | None = None, latents: torch.Tensor | None = None, prompt_embeds: torch.Tensor | None = None, prompt_embeds_mask: torch.Tensor | None = None, negative_prompt_embeds: torch.Tensor | None = None, negative_prompt_embeds_mask: torch.Tensor | None = None, output_type: str | None = "pil", return_dict: bool = True, attention_kwargs: dict[str, Any] | None = None, callback_on_step_end: Callable[[int, int], None] | None = None, callback_on_step_end_tensor_inputs: list[str] = ["latents"], max_sequence_length: int = 512, ): r""" Function invoked when calling the pipeline for generation. Args: image (`torch.Tensor`, `PIL.Image.Image`, `np.ndarray`, `list[torch.Tensor]`, `list[PIL.Image.Image]`, or `list[np.ndarray]`): `Image`, numpy array or tensor representing an image batch to be used as the starting point. For both numpy array and pytorch tensor, the expected value range is between `[0, 1]` If it's a tensor or a list or tensors, the expected shape should be `(B, C, H, W)` or `(C, H, W)`. If it is a numpy array or a list of arrays, the expected shape should be `(B, H, W, C)` or `(H, W, C)` It can also accept image latents as `image`, but if passing latents directly it is not encoded again. prompt (`str` or `list[str]`, *optional*): The prompt or prompts to guide the image generation. If not defined, one has to pass `prompt_embeds`. instead. negative_prompt (`str` or `list[str]`, *optional*): The prompt or prompts not to guide the image generation. If not defined, one has to pass `negative_prompt_embeds` instead. Ignored when not using guidance (i.e., ignored if `true_cfg_scale` is not greater than `1`). true_cfg_scale (`float`, *optional*, defaults to 1.0): true_cfg_scale (`float`, *optional*, defaults to 1.0): Guidance scale as defined in [Classifier-Free Diffusion Guidance](https://huggingface.co/papers/2207.12598). `true_cfg_scale` is defined as `w` of equation 2. of [Imagen Paper](https://huggingface.co/papers/2205.11487). Classifier-free guidance is enabled by setting `true_cfg_scale > 1` and a provided `negative_prompt`. Higher guidance scale encourages to generate images that are closely linked to the text `prompt`, usually at the expense of lower image quality. height (`int`, *optional*, defaults to self.unet.config.sample_size * self.vae_scale_factor): The height in pixels of the generated image. This is set to 1024 by default for the best results. width (`int`, *optional*, defaults to self.unet.config.sample_size * self.vae_scale_factor): The width in pixels of the generated image. This is set to 1024 by default for the best results. num_inference_steps (`int`, *optional*, defaults to 50): The number of denoising steps. More denoising steps usually lead to a higher quality image at the expense of slower inference. sigmas (`list[float]`, *optional*): Custom sigmas to use for the denoising process with schedulers which support a `sigmas` argument in their `set_timesteps` method. If not defined, the default behavior when `num_inference_steps` is passed will be used. guidance_scale (`float`, *optional*, defaults to None): A guidance scale value for guidance distilled models. Unlike the traditional classifier-free guidance where the guidance scale is applied during inference through noise prediction rescaling, guidance distilled models take the guidance scale directly as an input parameter during forward pass. Guidance scale is enabled by setting `guidance_scale > 1`. Higher guidance scale encourages to generate images that are closely linked to the text `prompt`, usually at the expense of lower image quality. This parameter in the pipeline is there to support future guidance-distilled models when they come up. It is ignored when not using guidance distilled models. To enable traditional classifier-free guidance, please pass `true_cfg_scale > 1.0` and `negative_prompt` (even an empty negative prompt like " " should enable classifier-free guidance computations). num_images_per_prompt (`int`, *optional*, defaults to 1): The number of images to generate per prompt. generator (`torch.Generator` or `list[torch.Generator]`, *optional*): One or a list of [torch generator(s)](https://pytorch.org/docs/stable/generated/torch.Generator.html) to make generation deterministic. latents (`torch.Tensor`, *optional*): Pre-generated noisy latents, sampled from a Gaussian distribution, to be used as inputs for image generation. Can be used to tweak the same generation with different prompts. If not provided, a latents tensor will be generated by sampling using the supplied random `generator`. prompt_embeds (`torch.Tensor`, *optional*): Pre-generated text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt weighting. If not provided, text embeddings will be generated from `prompt` input argument. negative_prompt_embeds (`torch.Tensor`, *optional*): Pre-generated negative text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt weighting. If not provided, negative_prompt_embeds will be generated from `negative_prompt` input argument. output_type (`str`, *optional*, defaults to `"pil"`): The output format of the generate image. Choose between [PIL](https://pillow.readthedocs.io/en/stable/): `PIL.Image.Image` or `np.array`. return_dict (`bool`, *optional*, defaults to `True`): Whether or not to return a [`~pipelines.qwenimage.QwenImagePipelineOutput`] instead of a plain tuple. attention_kwargs (`dict`, *optional*): A kwargs dictionary that if specified is passed along to the `AttentionProcessor` as defined under `self.processor` in [diffusers.models.attention_processor](https://github.com/huggingface/diffusers/blob/main/src/diffusers/models/attention_processor.py). callback_on_step_end (`Callable`, *optional*): A function that calls at the end of each denoising steps during the inference. The function is called with the following arguments: `callback_on_step_end(self: DiffusionPipeline, step: int, timestep: int, callback_kwargs: Dict)`. `callback_kwargs` will include a list of all tensors as specified by `callback_on_step_end_tensor_inputs`. callback_on_step_end_tensor_inputs (`list`, *optional*): The list of tensor inputs for the `callback_on_step_end` function. The tensors specified in the list will be passed as `callback_kwargs` argument. You will only be able to include variables listed in the `._callback_tensor_inputs` attribute of your pipeline class. max_sequence_length (`int` defaults to 512): Maximum sequence length to use with the `prompt`. Examples: Returns: [`~pipelines.qwenimage.QwenImagePipelineOutput`] or `tuple`: [`~pipelines.qwenimage.QwenImagePipelineOutput`] if `return_dict` is True, otherwise a `tuple`. When returning a tuple, the first element is a list with the generated images. """ image_size = image[0].size if isinstance(image, list) else image.size calculated_width, calculated_height, _ = calculate_dimensions(1024 * 1024, image_size[0] / image_size[1]) height = height or calculated_height width = width or calculated_width multiple_of = self.vae_scale_factor * 2 width = width // multiple_of * multiple_of height = height // multiple_of * multiple_of # 1. Check inputs. Raise error if not correct self.check_inputs( prompt, height, width, negative_prompt=negative_prompt, prompt_embeds=prompt_embeds, negative_prompt_embeds=negative_prompt_embeds, prompt_embeds_mask=prompt_embeds_mask, negative_prompt_embeds_mask=negative_prompt_embeds_mask, callback_on_step_end_tensor_inputs=callback_on_step_end_tensor_inputs, max_sequence_length=max_sequence_length, ) self._guidance_scale = guidance_scale self._attention_kwargs = attention_kwargs self._current_timestep = None self._interrupt = False # 2. Define call parameters if prompt is not None and isinstance(prompt, str): batch_size = 1 elif prompt is not None and isinstance(prompt, list): batch_size = len(prompt) else: batch_size = prompt_embeds.shape[0] device = self._execution_device # 3. Preprocess image if image is not None and not (isinstance(image, torch.Tensor) and image.size(1) == self.latent_channels): image = self.image_processor.resize(image, calculated_height, calculated_width) prompt_image = image image = self.image_processor.preprocess(image, calculated_height, calculated_width) image = image.unsqueeze(2) has_neg_prompt = negative_prompt is not None or ( negative_prompt_embeds is not None and negative_prompt_embeds_mask is not None ) if true_cfg_scale > 1 and not has_neg_prompt: logger.warning( f"true_cfg_scale is passed as {true_cfg_scale}, but classifier-free guidance is not enabled since no negative_prompt is provided." ) elif true_cfg_scale <= 1 and has_neg_prompt: logger.warning( " negative_prompt is passed but classifier-free guidance is not enabled since true_cfg_scale <= 1" ) do_true_cfg = true_cfg_scale > 1 and has_neg_prompt prompt_embeds, prompt_embeds_mask = self.encode_prompt( image=prompt_image, prompt=prompt, prompt_embeds=prompt_embeds, prompt_embeds_mask=prompt_embeds_mask, device=device, num_images_per_prompt=num_images_per_prompt, max_sequence_length=max_sequence_length, ) if do_true_cfg: negative_prompt_embeds, negative_prompt_embeds_mask = self.encode_prompt( image=prompt_image, prompt=negative_prompt, prompt_embeds=negative_prompt_embeds, prompt_embeds_mask=negative_prompt_embeds_mask, device=device, num_images_per_prompt=num_images_per_prompt, max_sequence_length=max_sequence_length, ) # 4. Prepare latent variables num_channels_latents = self.transformer.config.in_channels // 4 latents, image_latents = self.prepare_latents( image, batch_size * num_images_per_prompt, num_channels_latents, height, width, prompt_embeds.dtype, device, generator, latents, ) img_shapes = [ [ (1, height // self.vae_scale_factor // 2, width // self.vae_scale_factor // 2), (1, calculated_height // self.vae_scale_factor // 2, calculated_width // self.vae_scale_factor // 2), ] ] * batch_size # 5. Prepare timesteps sigmas = np.linspace(1.0, 1 / num_inference_steps, num_inference_steps) if sigmas is None else sigmas image_seq_len = latents.shape[1] mu = calculate_shift( image_seq_len, self.scheduler.config.get("base_image_seq_len", 256), self.scheduler.config.get("max_image_seq_len", 4096), self.scheduler.config.get("base_shift", 0.5), self.scheduler.config.get("max_shift", 1.15), ) timesteps, num_inference_steps = retrieve_timesteps( self.scheduler, num_inference_steps, device, sigmas=sigmas, mu=mu, ) num_warmup_steps = max(len(timesteps) - num_inference_steps * self.scheduler.order, 0) self._num_timesteps = len(timesteps) # handle guidance if self.transformer.config.guidance_embeds and guidance_scale is None: raise ValueError("guidance_scale is required for guidance-distilled model.") elif self.transformer.config.guidance_embeds: guidance = torch.full([1], guidance_scale, device=device, dtype=torch.float32) guidance = guidance.expand(latents.shape[0]) elif not self.transformer.config.guidance_embeds and guidance_scale is not None: logger.warning( f"guidance_scale is passed as {guidance_scale}, but ignored since the model is not guidance-distilled." ) guidance = None elif not self.transformer.config.guidance_embeds and guidance_scale is None: guidance = None if self.attention_kwargs is None: self._attention_kwargs = {} # 6. Denoising loop self.scheduler.set_begin_index(0) with self.progress_bar(total=num_inference_steps) as progress_bar: for i, t in enumerate(timesteps): if self.interrupt: continue self._current_timestep = t latent_model_input = latents if image_latents is not None: latent_model_input = torch.cat([latents, image_latents], dim=1) # broadcast to batch dimension in a way that's compatible with ONNX/Core ML timestep = t.expand(latents.shape[0]).to(latents.dtype) with self.transformer.cache_context("cond"): noise_pred = self.transformer( hidden_states=latent_model_input, timestep=timestep / 1000, guidance=guidance, encoder_hidden_states_mask=prompt_embeds_mask, encoder_hidden_states=prompt_embeds, img_shapes=img_shapes, attention_kwargs=self.attention_kwargs, return_dict=False, )[0] noise_pred = noise_pred[:, : latents.size(1)] if do_true_cfg: with self.transformer.cache_context("uncond"): neg_noise_pred = self.transformer( hidden_states=latent_model_input, timestep=timestep / 1000, guidance=guidance, encoder_hidden_states_mask=negative_prompt_embeds_mask, encoder_hidden_states=negative_prompt_embeds, img_shapes=img_shapes, attention_kwargs=self.attention_kwargs, return_dict=False, )[0] neg_noise_pred = neg_noise_pred[:, : latents.size(1)] comb_pred = neg_noise_pred + true_cfg_scale * (noise_pred - neg_noise_pred) cond_norm = torch.norm(noise_pred, dim=-1, keepdim=True) noise_norm = torch.norm(comb_pred, dim=-1, keepdim=True) noise_pred = comb_pred * (cond_norm / noise_norm) # compute the previous noisy sample x_t -> x_t-1 latents_dtype = latents.dtype latents = self.scheduler.step(noise_pred, t, latents, return_dict=False)[0] if latents.dtype != latents_dtype: if torch.backends.mps.is_available(): # some platforms (eg. apple mps) misbehave due to a pytorch bug: https://github.com/pytorch/pytorch/pull/99272 latents = latents.to(latents_dtype) if callback_on_step_end is not None: callback_kwargs = {} for k in callback_on_step_end_tensor_inputs: callback_kwargs[k] = locals()[k] callback_outputs = callback_on_step_end(self, i, t, callback_kwargs) latents = callback_outputs.pop("latents", latents) prompt_embeds = callback_outputs.pop("prompt_embeds", prompt_embeds) # call the callback, if provided if i == len(timesteps) - 1 or ((i + 1) > num_warmup_steps and (i + 1) % self.scheduler.order == 0): progress_bar.update() if XLA_AVAILABLE: xm.mark_step() self._current_timestep = None if output_type == "latent": image = latents else: latents = self._unpack_latents(latents, height, width, self.vae_scale_factor) latents = latents.to(self.vae.dtype) latents_mean = ( torch.tensor(self.vae.config.latents_mean) .view(1, self.vae.config.z_dim, 1, 1, 1) .to(latents.device, latents.dtype) ) latents_std = 1.0 / torch.tensor(self.vae.config.latents_std).view(1, self.vae.config.z_dim, 1, 1, 1).to( latents.device, latents.dtype ) latents = latents / latents_std + latents_mean image = self.vae.decode(latents, return_dict=False)[0][:, :, 0] image = self.image_processor.postprocess(image, output_type=output_type) # Offload all models self.maybe_free_model_hooks() if not return_dict: return (image,) return QwenImagePipelineOutput(images=image)
{ "repo_id": "huggingface/diffusers", "file_path": "src/diffusers/pipelines/qwenimage/pipeline_qwenimage_edit.py", "license": "Apache License 2.0", "lines": 787, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
huggingface/diffusers:src/diffusers/pipelines/qwenimage/pipeline_qwenimage_img2img.py
import inspect from typing import Any, Callable import numpy as np import torch from transformers import Qwen2_5_VLForConditionalGeneration, Qwen2Tokenizer from ...image_processor import PipelineImageInput, VaeImageProcessor from ...loaders import QwenImageLoraLoaderMixin from ...models import AutoencoderKLQwenImage, QwenImageTransformer2DModel from ...schedulers import FlowMatchEulerDiscreteScheduler from ...utils import deprecate, is_torch_xla_available, logging, replace_example_docstring from ...utils.torch_utils import randn_tensor from ..pipeline_utils import DiffusionPipeline from .pipeline_output import QwenImagePipelineOutput if is_torch_xla_available(): import torch_xla.core.xla_model as xm XLA_AVAILABLE = True else: XLA_AVAILABLE = False logger = logging.get_logger(__name__) # pylint: disable=invalid-name EXAMPLE_DOC_STRING = """ Examples: ```py >>> import torch >>> from diffusers import QwenImageImg2ImgPipeline >>> from diffusers.utils import load_image >>> pipe = QwenImageImg2ImgPipeline.from_pretrained("Qwen/Qwen-Image", torch_dtype=torch.bfloat16) >>> pipe = pipe.to("cuda") >>> url = "https://raw.githubusercontent.com/CompVis/stable-diffusion/main/assets/stable-samples/img2img/sketch-mountains-input.jpg" >>> init_image = load_image(url).resize((1024, 1024)) >>> prompt = "cat wizard, gandalf, lord of the rings, detailed, fantasy, cute, adorable, Pixar, Disney" >>> images = pipe(prompt=prompt, negative_prompt=" ", image=init_image, strength=0.95).images[0] >>> images.save("qwenimage_img2img.png") ``` """ # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion_img2img.retrieve_latents def retrieve_latents( encoder_output: torch.Tensor, generator: torch.Generator | None = None, sample_mode: str = "sample" ): if hasattr(encoder_output, "latent_dist") and sample_mode == "sample": return encoder_output.latent_dist.sample(generator) elif hasattr(encoder_output, "latent_dist") and sample_mode == "argmax": return encoder_output.latent_dist.mode() elif hasattr(encoder_output, "latents"): return encoder_output.latents else: raise AttributeError("Could not access latents of provided encoder_output") # Copied from diffusers.pipelines.qwenimage.pipeline_qwenimage.calculate_shift def calculate_shift( image_seq_len, base_seq_len: int = 256, max_seq_len: int = 4096, base_shift: float = 0.5, max_shift: float = 1.15, ): m = (max_shift - base_shift) / (max_seq_len - base_seq_len) b = base_shift - m * base_seq_len mu = image_seq_len * m + b return mu # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.retrieve_timesteps def retrieve_timesteps( scheduler, num_inference_steps: int | None = None, device: str | torch.device | None = None, timesteps: list[int] | None = None, sigmas: list[float] | None = None, **kwargs, ): r""" Calls the scheduler's `set_timesteps` method and retrieves timesteps from the scheduler after the call. Handles custom timesteps. Any kwargs will be supplied to `scheduler.set_timesteps`. Args: scheduler (`SchedulerMixin`): The scheduler to get timesteps from. num_inference_steps (`int`): The number of diffusion steps used when generating samples with a pre-trained model. If used, `timesteps` must be `None`. device (`str` or `torch.device`, *optional*): The device to which the timesteps should be moved to. If `None`, the timesteps are not moved. timesteps (`list[int]`, *optional*): Custom timesteps used to override the timestep spacing strategy of the scheduler. If `timesteps` is passed, `num_inference_steps` and `sigmas` must be `None`. sigmas (`list[float]`, *optional*): Custom sigmas used to override the timestep spacing strategy of the scheduler. If `sigmas` is passed, `num_inference_steps` and `timesteps` must be `None`. Returns: `tuple[torch.Tensor, int]`: A tuple where the first element is the timestep schedule from the scheduler and the second element is the number of inference steps. """ if timesteps is not None and sigmas is not None: raise ValueError("Only one of `timesteps` or `sigmas` can be passed. Please choose one to set custom values") if timesteps is not None: accepts_timesteps = "timesteps" in set(inspect.signature(scheduler.set_timesteps).parameters.keys()) if not accepts_timesteps: raise ValueError( f"The current scheduler class {scheduler.__class__}'s `set_timesteps` does not support custom" f" timestep schedules. Please check whether you are using the correct scheduler." ) scheduler.set_timesteps(timesteps=timesteps, device=device, **kwargs) timesteps = scheduler.timesteps num_inference_steps = len(timesteps) elif sigmas is not None: accept_sigmas = "sigmas" in set(inspect.signature(scheduler.set_timesteps).parameters.keys()) if not accept_sigmas: raise ValueError( f"The current scheduler class {scheduler.__class__}'s `set_timesteps` does not support custom" f" sigmas schedules. Please check whether you are using the correct scheduler." ) scheduler.set_timesteps(sigmas=sigmas, device=device, **kwargs) timesteps = scheduler.timesteps num_inference_steps = len(timesteps) else: scheduler.set_timesteps(num_inference_steps, device=device, **kwargs) timesteps = scheduler.timesteps return timesteps, num_inference_steps class QwenImageImg2ImgPipeline(DiffusionPipeline, QwenImageLoraLoaderMixin): r""" The QwenImage pipeline for text-to-image generation. Args: transformer ([`QwenImageTransformer2DModel`]): Conditional Transformer (MMDiT) architecture to denoise the encoded image latents. scheduler ([`FlowMatchEulerDiscreteScheduler`]): A scheduler to be used in combination with `transformer` to denoise the encoded image latents. vae ([`AutoencoderKL`]): Variational Auto-Encoder (VAE) Model to encode and decode images to and from latent representations. text_encoder ([`Qwen2.5-VL-7B-Instruct`]): [Qwen2.5-VL-7B-Instruct](https://huggingface.co/Qwen/Qwen2.5-VL-7B-Instruct), specifically the [Qwen2.5-VL-7B-Instruct](https://huggingface.co/Qwen/Qwen2.5-VL-7B-Instruct) variant. tokenizer (`QwenTokenizer`): Tokenizer of class [CLIPTokenizer](https://huggingface.co/docs/transformers/en/model_doc/clip#transformers.CLIPTokenizer). """ model_cpu_offload_seq = "text_encoder->transformer->vae" _callback_tensor_inputs = ["latents", "prompt_embeds"] def __init__( self, scheduler: FlowMatchEulerDiscreteScheduler, vae: AutoencoderKLQwenImage, text_encoder: Qwen2_5_VLForConditionalGeneration, tokenizer: Qwen2Tokenizer, transformer: QwenImageTransformer2DModel, ): super().__init__() self.register_modules( vae=vae, text_encoder=text_encoder, tokenizer=tokenizer, transformer=transformer, scheduler=scheduler, ) self.vae_scale_factor = 2 ** len(self.vae.temperal_downsample) if getattr(self, "vae", None) else 8 # QwenImage latents are turned into 2x2 patches and packed. This means the latent width and height has to be divisible # by the patch size. So the vae scale factor is multiplied by the patch size to account for this self.latent_channels = self.vae.config.z_dim if getattr(self, "vae", None) else 16 self.image_processor = VaeImageProcessor( vae_scale_factor=self.vae_scale_factor * 2, vae_latent_channels=self.latent_channels ) self.tokenizer_max_length = 1024 self.prompt_template_encode = "<|im_start|>system\nDescribe the image by detailing the color, shape, size, texture, quantity, text, spatial relationships of the objects and background:<|im_end|>\n<|im_start|>user\n{}<|im_end|>\n<|im_start|>assistant\n" self.prompt_template_encode_start_idx = 34 self.default_sample_size = 128 # Copied from diffusers.pipelines.qwenimage.pipeline_qwenimage.QwenImagePipeline._extract_masked_hidden def _extract_masked_hidden(self, hidden_states: torch.Tensor, mask: torch.Tensor): bool_mask = mask.bool() valid_lengths = bool_mask.sum(dim=1) selected = hidden_states[bool_mask] split_result = torch.split(selected, valid_lengths.tolist(), dim=0) return split_result # Copied from diffusers.pipelines.qwenimage.pipeline_qwenimage.QwenImagePipeline._get_qwen_prompt_embeds def _get_qwen_prompt_embeds( self, prompt: str | list[str] = None, device: torch.device | None = None, dtype: torch.dtype | None = None, ): device = device or self._execution_device dtype = dtype or self.text_encoder.dtype prompt = [prompt] if isinstance(prompt, str) else prompt template = self.prompt_template_encode drop_idx = self.prompt_template_encode_start_idx txt = [template.format(e) for e in prompt] txt_tokens = self.tokenizer( txt, max_length=self.tokenizer_max_length + drop_idx, padding=True, truncation=True, return_tensors="pt" ).to(device) encoder_hidden_states = self.text_encoder( input_ids=txt_tokens.input_ids, attention_mask=txt_tokens.attention_mask, output_hidden_states=True, ) hidden_states = encoder_hidden_states.hidden_states[-1] split_hidden_states = self._extract_masked_hidden(hidden_states, txt_tokens.attention_mask) split_hidden_states = [e[drop_idx:] for e in split_hidden_states] attn_mask_list = [torch.ones(e.size(0), dtype=torch.long, device=e.device) for e in split_hidden_states] max_seq_len = max([e.size(0) for e in split_hidden_states]) prompt_embeds = torch.stack( [torch.cat([u, u.new_zeros(max_seq_len - u.size(0), u.size(1))]) for u in split_hidden_states] ) encoder_attention_mask = torch.stack( [torch.cat([u, u.new_zeros(max_seq_len - u.size(0))]) for u in attn_mask_list] ) prompt_embeds = prompt_embeds.to(dtype=dtype, device=device) return prompt_embeds, encoder_attention_mask def _encode_vae_image(self, image: torch.Tensor, generator: torch.Generator): if isinstance(generator, list): image_latents = [ retrieve_latents(self.vae.encode(image[i : i + 1]), generator=generator[i]) for i in range(image.shape[0]) ] image_latents = torch.cat(image_latents, dim=0) else: image_latents = retrieve_latents(self.vae.encode(image), generator=generator) latents_mean = ( torch.tensor(self.vae.config.latents_mean) .view(1, self.vae.config.z_dim, 1, 1, 1) .to(image_latents.device, image_latents.dtype) ) latents_std = 1.0 / torch.tensor(self.vae.config.latents_std).view(1, self.vae.config.z_dim, 1, 1, 1).to( image_latents.device, image_latents.dtype ) image_latents = (image_latents - latents_mean) * latents_std return image_latents # Copied from diffusers.pipelines.stable_diffusion_3.pipeline_stable_diffusion_3_img2img.StableDiffusion3Img2ImgPipeline.get_timesteps def get_timesteps(self, num_inference_steps, strength, device): # get the original timestep using init_timestep init_timestep = min(num_inference_steps * strength, num_inference_steps) t_start = int(max(num_inference_steps - init_timestep, 0)) timesteps = self.scheduler.timesteps[t_start * self.scheduler.order :] if hasattr(self.scheduler, "set_begin_index"): self.scheduler.set_begin_index(t_start * self.scheduler.order) return timesteps, num_inference_steps - t_start # Copied from diffusers.pipelines.qwenimage.pipeline_qwenimage.QwenImagePipeline.encode_prompt def encode_prompt( self, prompt: str | list[str], device: torch.device | None = None, num_images_per_prompt: int = 1, prompt_embeds: torch.Tensor | None = None, prompt_embeds_mask: torch.Tensor | None = None, max_sequence_length: int = 1024, ): r""" Args: prompt (`str` or `list[str]`, *optional*): prompt to be encoded device: (`torch.device`): torch device num_images_per_prompt (`int`): number of images that should be generated per prompt prompt_embeds (`torch.Tensor`, *optional*): Pre-generated text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt weighting. If not provided, text embeddings will be generated from `prompt` input argument. """ device = device or self._execution_device prompt = [prompt] if isinstance(prompt, str) else prompt batch_size = len(prompt) if prompt_embeds is None else prompt_embeds.shape[0] if prompt_embeds is None: prompt_embeds, prompt_embeds_mask = self._get_qwen_prompt_embeds(prompt, device) prompt_embeds = prompt_embeds[:, :max_sequence_length] _, seq_len, _ = prompt_embeds.shape prompt_embeds = prompt_embeds.repeat(1, num_images_per_prompt, 1) prompt_embeds = prompt_embeds.view(batch_size * num_images_per_prompt, seq_len, -1) if prompt_embeds_mask is not None: prompt_embeds_mask = prompt_embeds_mask[:, :max_sequence_length] prompt_embeds_mask = prompt_embeds_mask.repeat(1, num_images_per_prompt, 1) prompt_embeds_mask = prompt_embeds_mask.view(batch_size * num_images_per_prompt, seq_len) if prompt_embeds_mask.all(): prompt_embeds_mask = None return prompt_embeds, prompt_embeds_mask def check_inputs( self, prompt, strength, height, width, negative_prompt=None, prompt_embeds=None, negative_prompt_embeds=None, prompt_embeds_mask=None, negative_prompt_embeds_mask=None, callback_on_step_end_tensor_inputs=None, max_sequence_length=None, ): if strength < 0 or strength > 1: raise ValueError(f"The value of strength should in [0.0, 1.0] but is {strength}") if height % (self.vae_scale_factor * 2) != 0 or width % (self.vae_scale_factor * 2) != 0: logger.warning( f"`height` and `width` have to be divisible by {self.vae_scale_factor * 2} but are {height} and {width}. Dimensions will be resized accordingly" ) if callback_on_step_end_tensor_inputs is not None and not all( k in self._callback_tensor_inputs for k in callback_on_step_end_tensor_inputs ): raise ValueError( f"`callback_on_step_end_tensor_inputs` has to be in {self._callback_tensor_inputs}, but found {[k for k in callback_on_step_end_tensor_inputs if k not in self._callback_tensor_inputs]}" ) if prompt is not None and prompt_embeds is not None: raise ValueError( f"Cannot forward both `prompt`: {prompt} and `prompt_embeds`: {prompt_embeds}. Please make sure to" " only forward one of the two." ) elif prompt is None and prompt_embeds is None: raise ValueError( "Provide either `prompt` or `prompt_embeds`. Cannot leave both `prompt` and `prompt_embeds` undefined." ) elif prompt is not None and (not isinstance(prompt, str) and not isinstance(prompt, list)): raise ValueError(f"`prompt` has to be of type `str` or `list` but is {type(prompt)}") if negative_prompt is not None and negative_prompt_embeds is not None: raise ValueError( f"Cannot forward both `negative_prompt`: {negative_prompt} and `negative_prompt_embeds`:" f" {negative_prompt_embeds}. Please make sure to only forward one of the two." ) if max_sequence_length is not None and max_sequence_length > 1024: raise ValueError(f"`max_sequence_length` cannot be greater than 1024 but is {max_sequence_length}") @staticmethod # Copied from diffusers.pipelines.qwenimage.pipeline_qwenimage.QwenImagePipeline._pack_latents def _pack_latents(latents, batch_size, num_channels_latents, height, width): latents = latents.view(batch_size, num_channels_latents, height // 2, 2, width // 2, 2) latents = latents.permute(0, 2, 4, 1, 3, 5) latents = latents.reshape(batch_size, (height // 2) * (width // 2), num_channels_latents * 4) return latents @staticmethod # Copied from diffusers.pipelines.qwenimage.pipeline_qwenimage.QwenImagePipeline._unpack_latents def _unpack_latents(latents, height, width, vae_scale_factor): batch_size, num_patches, channels = latents.shape # VAE applies 8x compression on images but we must also account for packing which requires # latent height and width to be divisible by 2. height = 2 * (int(height) // (vae_scale_factor * 2)) width = 2 * (int(width) // (vae_scale_factor * 2)) latents = latents.view(batch_size, height // 2, width // 2, channels // 4, 2, 2) latents = latents.permute(0, 3, 1, 4, 2, 5) latents = latents.reshape(batch_size, channels // (2 * 2), 1, height, width) return latents def enable_vae_slicing(self): r""" Enable sliced VAE decoding. When this option is enabled, the VAE will split the input tensor in slices to compute decoding in several steps. This is useful to save some memory and allow larger batch sizes. """ depr_message = f"Calling `enable_vae_slicing()` on a `{self.__class__.__name__}` is deprecated and this method will be removed in a future version. Please use `pipe.vae.enable_slicing()`." deprecate( "enable_vae_slicing", "0.40.0", depr_message, ) self.vae.enable_slicing() def disable_vae_slicing(self): r""" Disable sliced VAE decoding. If `enable_vae_slicing` was previously enabled, this method will go back to computing decoding in one step. """ depr_message = f"Calling `disable_vae_slicing()` on a `{self.__class__.__name__}` is deprecated and this method will be removed in a future version. Please use `pipe.vae.disable_slicing()`." deprecate( "disable_vae_slicing", "0.40.0", depr_message, ) self.vae.disable_slicing() def enable_vae_tiling(self): r""" Enable tiled VAE decoding. When this option is enabled, the VAE will split the input tensor into tiles to compute decoding and encoding in several steps. This is useful for saving a large amount of memory and to allow processing larger images. """ depr_message = f"Calling `enable_vae_tiling()` on a `{self.__class__.__name__}` is deprecated and this method will be removed in a future version. Please use `pipe.vae.enable_tiling()`." deprecate( "enable_vae_tiling", "0.40.0", depr_message, ) self.vae.enable_tiling() def disable_vae_tiling(self): r""" Disable tiled VAE decoding. If `enable_vae_tiling` was previously enabled, this method will go back to computing decoding in one step. """ depr_message = f"Calling `disable_vae_tiling()` on a `{self.__class__.__name__}` is deprecated and this method will be removed in a future version. Please use `pipe.vae.disable_tiling()`." deprecate( "disable_vae_tiling", "0.40.0", depr_message, ) self.vae.disable_tiling() def prepare_latents( self, image, timestep, batch_size, num_channels_latents, height, width, dtype, device, generator, latents=None, ): if isinstance(generator, list) and len(generator) != batch_size: raise ValueError( f"You have passed a list of generators of length {len(generator)}, but requested an effective batch" f" size of {batch_size}. Make sure the batch size matches the length of the generators." ) # VAE applies 8x compression on images but we must also account for packing which requires # latent height and width to be divisible by 2. height = 2 * (int(height) // (self.vae_scale_factor * 2)) width = 2 * (int(width) // (self.vae_scale_factor * 2)) shape = (batch_size, 1, num_channels_latents, height, width) # If image is [B,C,H,W] -> add T=1. If it's already [B,C,T,H,W], leave it. if image.dim() == 4: image = image.unsqueeze(2) elif image.dim() != 5: raise ValueError(f"Expected image dims 4 or 5, got {image.dim()}.") if latents is not None: return latents.to(device=device, dtype=dtype) image = image.to(device=device, dtype=dtype) if image.shape[1] != self.latent_channels: image_latents = self._encode_vae_image(image=image, generator=generator) # [B,z,1,H',W'] else: image_latents = image if batch_size > image_latents.shape[0] and batch_size % image_latents.shape[0] == 0: # expand init_latents for batch_size additional_image_per_prompt = batch_size // image_latents.shape[0] image_latents = torch.cat([image_latents] * additional_image_per_prompt, dim=0) elif batch_size > image_latents.shape[0] and batch_size % image_latents.shape[0] != 0: raise ValueError( f"Cannot duplicate `image` of batch size {image_latents.shape[0]} to {batch_size} text prompts." ) else: image_latents = torch.cat([image_latents], dim=0) image_latents = image_latents.transpose(1, 2) # [B,1,z,H',W'] noise = randn_tensor(shape, generator=generator, device=device, dtype=dtype) latents = self.scheduler.scale_noise(image_latents, timestep, noise) latents = self._pack_latents(latents, batch_size, num_channels_latents, height, width) return latents @property def guidance_scale(self): return self._guidance_scale @property def attention_kwargs(self): return self._attention_kwargs @property def num_timesteps(self): return self._num_timesteps @property def current_timestep(self): return self._current_timestep @property def interrupt(self): return self._interrupt @torch.no_grad() @replace_example_docstring(EXAMPLE_DOC_STRING) def __call__( self, prompt: str | list[str] = None, negative_prompt: str | list[str] = None, true_cfg_scale: float = 4.0, image: PipelineImageInput = None, height: int | None = None, width: int | None = None, strength: float = 0.6, num_inference_steps: int = 50, sigmas: list[float] | None = None, guidance_scale: float | None = None, num_images_per_prompt: int = 1, generator: torch.Generator | list[torch.Generator] | None = None, latents: torch.Tensor | None = None, prompt_embeds: torch.Tensor | None = None, prompt_embeds_mask: torch.Tensor | None = None, negative_prompt_embeds: torch.Tensor | None = None, negative_prompt_embeds_mask: torch.Tensor | None = None, output_type: str | None = "pil", return_dict: bool = True, attention_kwargs: dict[str, Any] | None = None, callback_on_step_end: Callable[[int, int], None] | None = None, callback_on_step_end_tensor_inputs: list[str] = ["latents"], max_sequence_length: int = 512, ): r""" Function invoked when calling the pipeline for generation. Args: prompt (`str` or `list[str]`, *optional*): The prompt or prompts to guide the image generation. If not defined, one has to pass `prompt_embeds`. instead. negative_prompt (`str` or `list[str]`, *optional*): The prompt or prompts not to guide the image generation. If not defined, one has to pass `negative_prompt_embeds` instead. Ignored when not using guidance (i.e., ignored if `true_cfg_scale` is not greater than `1`). image (`torch.Tensor`, `PIL.Image.Image`, `np.ndarray`, `list[torch.Tensor]`, `list[PIL.Image.Image]`, or `list[np.ndarray]`): `Image`, numpy array or tensor representing an image batch to be used as the starting point. For both numpy array and pytorch tensor, the expected value range is between `[0, 1]` If it's a tensor or a list or tensors, the expected shape should be `(B, C, H, W)` or `(C, H, W)`. If it is a numpy array or a list of arrays, the expected shape should be `(B, H, W, C)` or `(H, W, C)` It can also accept image latents as `image`, but if passing latents directly it is not encoded again. true_cfg_scale (`float`, *optional*, defaults to 1.0): Guidance scale as defined in [Classifier-Free Diffusion Guidance](https://huggingface.co/papers/2207.12598). `true_cfg_scale` is defined as `w` of equation 2. of [Imagen Paper](https://huggingface.co/papers/2205.11487). Classifier-free guidance is enabled by setting `true_cfg_scale > 1` and a provided `negative_prompt`. Higher guidance scale encourages to generate images that are closely linked to the text `prompt`, usually at the expense of lower image quality. height (`int`, *optional*, defaults to self.unet.config.sample_size * self.vae_scale_factor): The height in pixels of the generated image. This is set to 1024 by default for the best results. width (`int`, *optional*, defaults to self.unet.config.sample_size * self.vae_scale_factor): The width in pixels of the generated image. This is set to 1024 by default for the best results. strength (`float`, *optional*, defaults to 1.0): Indicates extent to transform the reference `image`. Must be between 0 and 1. `image` is used as a starting point and more noise is added the higher the `strength`. The number of denoising steps depends on the amount of noise initially added. When `strength` is 1, added noise is maximum and the denoising process runs for the full number of iterations specified in `num_inference_steps`. A value of 1 essentially ignores `image`. num_inference_steps (`int`, *optional*, defaults to 50): The number of denoising steps. More denoising steps usually lead to a higher quality image at the expense of slower inference. sigmas (`list[float]`, *optional*): Custom sigmas to use for the denoising process with schedulers which support a `sigmas` argument in their `set_timesteps` method. If not defined, the default behavior when `num_inference_steps` is passed will be used. guidance_scale (`float`, *optional*, defaults to None): A guidance scale value for guidance distilled models. Unlike the traditional classifier-free guidance where the guidance scale is applied during inference through noise prediction rescaling, guidance distilled models take the guidance scale directly as an input parameter during forward pass. Guidance scale is enabled by setting `guidance_scale > 1`. Higher guidance scale encourages to generate images that are closely linked to the text `prompt`, usually at the expense of lower image quality. This parameter in the pipeline is there to support future guidance-distilled models when they come up. It is ignored when not using guidance distilled models. To enable traditional classifier-free guidance, please pass `true_cfg_scale > 1.0` and `negative_prompt` (even an empty negative prompt like " " should enable classifier-free guidance computations). num_images_per_prompt (`int`, *optional*, defaults to 1): The number of images to generate per prompt. generator (`torch.Generator` or `list[torch.Generator]`, *optional*): One or a list of [torch generator(s)](https://pytorch.org/docs/stable/generated/torch.Generator.html) to make generation deterministic. latents (`torch.Tensor`, *optional*): Pre-generated noisy latents, sampled from a Gaussian distribution, to be used as inputs for image generation. Can be used to tweak the same generation with different prompts. If not provided, a latents tensor will be generated by sampling using the supplied random `generator`. prompt_embeds (`torch.Tensor`, *optional*): Pre-generated text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt weighting. If not provided, text embeddings will be generated from `prompt` input argument. negative_prompt_embeds (`torch.Tensor`, *optional*): Pre-generated negative text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt weighting. If not provided, negative_prompt_embeds will be generated from `negative_prompt` input argument. output_type (`str`, *optional*, defaults to `"pil"`): The output format of the generate image. Choose between [PIL](https://pillow.readthedocs.io/en/stable/): `PIL.Image.Image` or `np.array`. return_dict (`bool`, *optional*, defaults to `True`): Whether or not to return a [`~pipelines.qwenimage.QwenImagePipelineOutput`] instead of a plain tuple. attention_kwargs (`dict`, *optional*): A kwargs dictionary that if specified is passed along to the `AttentionProcessor` as defined under `self.processor` in [diffusers.models.attention_processor](https://github.com/huggingface/diffusers/blob/main/src/diffusers/models/attention_processor.py). callback_on_step_end (`Callable`, *optional*): A function that calls at the end of each denoising steps during the inference. The function is called with the following arguments: `callback_on_step_end(self: DiffusionPipeline, step: int, timestep: int, callback_kwargs: Dict)`. `callback_kwargs` will include a list of all tensors as specified by `callback_on_step_end_tensor_inputs`. callback_on_step_end_tensor_inputs (`list`, *optional*): The list of tensor inputs for the `callback_on_step_end` function. The tensors specified in the list will be passed as `callback_kwargs` argument. You will only be able to include variables listed in the `._callback_tensor_inputs` attribute of your pipeline class. max_sequence_length (`int` defaults to 512): Maximum sequence length to use with the `prompt`. Examples: Returns: [`~pipelines.qwenimage.QwenImagePipelineOutput`] or `tuple`: [`~pipelines.qwenimage.QwenImagePipelineOutput`] if `return_dict` is True, otherwise a `tuple`. When returning a tuple, the first element is a list with the generated images. """ height = height or self.default_sample_size * self.vae_scale_factor width = width or self.default_sample_size * self.vae_scale_factor # 1. Check inputs. Raise error if not correct self.check_inputs( prompt, strength, height, width, negative_prompt=negative_prompt, prompt_embeds=prompt_embeds, negative_prompt_embeds=negative_prompt_embeds, prompt_embeds_mask=prompt_embeds_mask, negative_prompt_embeds_mask=negative_prompt_embeds_mask, callback_on_step_end_tensor_inputs=callback_on_step_end_tensor_inputs, max_sequence_length=max_sequence_length, ) self._guidance_scale = guidance_scale self._attention_kwargs = attention_kwargs self._current_timestep = None self._interrupt = False # 2. Preprocess image init_image = self.image_processor.preprocess(image, height=height, width=width) init_image = init_image.to(dtype=torch.float32) # 3. Define call parameters if prompt is not None and isinstance(prompt, str): batch_size = 1 elif prompt is not None and isinstance(prompt, list): batch_size = len(prompt) else: batch_size = prompt_embeds.shape[0] device = self._execution_device has_neg_prompt = negative_prompt is not None or ( negative_prompt_embeds is not None and negative_prompt_embeds_mask is not None ) if true_cfg_scale > 1 and not has_neg_prompt: logger.warning( f"true_cfg_scale is passed as {true_cfg_scale}, but classifier-free guidance is not enabled since no negative_prompt is provided." ) elif true_cfg_scale <= 1 and has_neg_prompt: logger.warning( " negative_prompt is passed but classifier-free guidance is not enabled since true_cfg_scale <= 1" ) do_true_cfg = true_cfg_scale > 1 and has_neg_prompt prompt_embeds, prompt_embeds_mask = self.encode_prompt( prompt=prompt, prompt_embeds=prompt_embeds, prompt_embeds_mask=prompt_embeds_mask, device=device, num_images_per_prompt=num_images_per_prompt, max_sequence_length=max_sequence_length, ) if do_true_cfg: negative_prompt_embeds, negative_prompt_embeds_mask = self.encode_prompt( prompt=negative_prompt, prompt_embeds=negative_prompt_embeds, prompt_embeds_mask=negative_prompt_embeds_mask, device=device, num_images_per_prompt=num_images_per_prompt, max_sequence_length=max_sequence_length, ) # 4. Prepare timesteps sigmas = np.linspace(1.0, 1 / num_inference_steps, num_inference_steps) if sigmas is None else sigmas image_seq_len = (int(height) // self.vae_scale_factor // 2) * (int(width) // self.vae_scale_factor // 2) mu = calculate_shift( image_seq_len, self.scheduler.config.get("base_image_seq_len", 256), self.scheduler.config.get("max_image_seq_len", 4096), self.scheduler.config.get("base_shift", 0.5), self.scheduler.config.get("max_shift", 1.15), ) timesteps, num_inference_steps = retrieve_timesteps( self.scheduler, num_inference_steps, device, sigmas=sigmas, mu=mu, ) timesteps, num_inference_steps = self.get_timesteps(num_inference_steps, strength, device) if num_inference_steps < 1: raise ValueError( f"After adjusting the num_inference_steps by strength parameter: {strength}, the number of pipeline" f"steps is {num_inference_steps} which is < 1 and not appropriate for this pipeline." ) latent_timestep = timesteps[:1].repeat(batch_size * num_images_per_prompt) # 5. Prepare latent variables num_channels_latents = self.transformer.config.in_channels // 4 latents = self.prepare_latents( init_image, latent_timestep, batch_size * num_images_per_prompt, num_channels_latents, height, width, prompt_embeds.dtype, device, generator, latents, ) img_shapes = [[(1, height // self.vae_scale_factor // 2, width // self.vae_scale_factor // 2)]] * batch_size num_warmup_steps = max(len(timesteps) - num_inference_steps * self.scheduler.order, 0) self._num_timesteps = len(timesteps) # handle guidance if self.transformer.config.guidance_embeds and guidance_scale is None: raise ValueError("guidance_scale is required for guidance-distilled model.") elif self.transformer.config.guidance_embeds: guidance = torch.full([1], guidance_scale, device=device, dtype=torch.float32) guidance = guidance.expand(latents.shape[0]) elif not self.transformer.config.guidance_embeds and guidance_scale is not None: logger.warning( f"guidance_scale is passed as {guidance_scale}, but ignored since the model is not guidance-distilled." ) guidance = None elif not self.transformer.config.guidance_embeds and guidance_scale is None: guidance = None if self.attention_kwargs is None: self._attention_kwargs = {} # 6. Denoising loop with self.progress_bar(total=num_inference_steps) as progress_bar: for i, t in enumerate(timesteps): if self.interrupt: continue self._current_timestep = t # broadcast to batch dimension in a way that's compatible with ONNX/Core ML timestep = t.expand(latents.shape[0]).to(latents.dtype) with self.transformer.cache_context("cond"): noise_pred = self.transformer( hidden_states=latents, timestep=timestep / 1000, guidance=guidance, encoder_hidden_states_mask=prompt_embeds_mask, encoder_hidden_states=prompt_embeds, img_shapes=img_shapes, attention_kwargs=self.attention_kwargs, return_dict=False, )[0] if do_true_cfg: with self.transformer.cache_context("uncond"): neg_noise_pred = self.transformer( hidden_states=latents, timestep=timestep / 1000, guidance=guidance, encoder_hidden_states_mask=negative_prompt_embeds_mask, encoder_hidden_states=negative_prompt_embeds, img_shapes=img_shapes, attention_kwargs=self.attention_kwargs, return_dict=False, )[0] comb_pred = neg_noise_pred + true_cfg_scale * (noise_pred - neg_noise_pred) cond_norm = torch.norm(noise_pred, dim=-1, keepdim=True) noise_norm = torch.norm(comb_pred, dim=-1, keepdim=True) noise_pred = comb_pred * (cond_norm / noise_norm) # compute the previous noisy sample x_t -> x_t-1 latents_dtype = latents.dtype latents = self.scheduler.step(noise_pred, t, latents, return_dict=False)[0] if latents.dtype != latents_dtype: if torch.backends.mps.is_available(): # some platforms (eg. apple mps) misbehave due to a pytorch bug: https://github.com/pytorch/pytorch/pull/99272 latents = latents.to(latents_dtype) if callback_on_step_end is not None: callback_kwargs = {} for k in callback_on_step_end_tensor_inputs: callback_kwargs[k] = locals()[k] callback_outputs = callback_on_step_end(self, i, t, callback_kwargs) latents = callback_outputs.pop("latents", latents) prompt_embeds = callback_outputs.pop("prompt_embeds", prompt_embeds) # call the callback, if provided if i == len(timesteps) - 1 or ((i + 1) > num_warmup_steps and (i + 1) % self.scheduler.order == 0): progress_bar.update() if XLA_AVAILABLE: xm.mark_step() self._current_timestep = None if output_type == "latent": image = latents else: latents = self._unpack_latents(latents, height, width, self.vae_scale_factor) latents = latents.to(self.vae.dtype) latents_mean = ( torch.tensor(self.vae.config.latents_mean) .view(1, self.vae.config.z_dim, 1, 1, 1) .to(latents.device, latents.dtype) ) latents_std = 1.0 / torch.tensor(self.vae.config.latents_std).view(1, self.vae.config.z_dim, 1, 1, 1).to( latents.device, latents.dtype ) latents = latents / latents_std + latents_mean image = self.vae.decode(latents, return_dict=False)[0][:, :, 0] image = self.image_processor.postprocess(image, output_type=output_type) # Offload all models self.maybe_free_model_hooks() if not return_dict: return (image,) return QwenImagePipelineOutput(images=image)
{ "repo_id": "huggingface/diffusers", "file_path": "src/diffusers/pipelines/qwenimage/pipeline_qwenimage_img2img.py", "license": "Apache License 2.0", "lines": 755, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
function_complex
huggingface/diffusers:src/diffusers/pipelines/qwenimage/pipeline_qwenimage_inpaint.py
import inspect from typing import Any, Callable import numpy as np import PIL.Image import torch from transformers import Qwen2_5_VLForConditionalGeneration, Qwen2Tokenizer from ...image_processor import PipelineImageInput, VaeImageProcessor from ...loaders import QwenImageLoraLoaderMixin from ...models import AutoencoderKLQwenImage, QwenImageTransformer2DModel from ...schedulers import FlowMatchEulerDiscreteScheduler from ...utils import deprecate, is_torch_xla_available, logging, replace_example_docstring from ...utils.torch_utils import randn_tensor from ..pipeline_utils import DiffusionPipeline from .pipeline_output import QwenImagePipelineOutput if is_torch_xla_available(): import torch_xla.core.xla_model as xm XLA_AVAILABLE = True else: XLA_AVAILABLE = False logger = logging.get_logger(__name__) # pylint: disable=invalid-name EXAMPLE_DOC_STRING = """ Examples: ```py >>> import torch >>> from diffusers import QwenImageInpaintPipeline >>> from diffusers.utils import load_image >>> pipe = QwenImageInpaintPipeline.from_pretrained("Qwen/Qwen-Image", torch_dtype=torch.bfloat16) >>> pipe.to("cuda") >>> prompt = "Face of a yellow cat, high resolution, sitting on a park bench" >>> img_url = "https://raw.githubusercontent.com/CompVis/latent-diffusion/main/data/inpainting_examples/overture-creations-5sI6fQgYIuo.png" >>> mask_url = "https://raw.githubusercontent.com/CompVis/latent-diffusion/main/data/inpainting_examples/overture-creations-5sI6fQgYIuo_mask.png" >>> source = load_image(img_url) >>> mask = load_image(mask_url) >>> image = pipe(prompt=prompt, negative_prompt=" ", image=source, mask_image=mask, strength=0.85).images[0] >>> image.save("qwenimage_inpainting.png") ``` """ # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion_img2img.retrieve_latents def retrieve_latents( encoder_output: torch.Tensor, generator: torch.Generator | None = None, sample_mode: str = "sample" ): if hasattr(encoder_output, "latent_dist") and sample_mode == "sample": return encoder_output.latent_dist.sample(generator) elif hasattr(encoder_output, "latent_dist") and sample_mode == "argmax": return encoder_output.latent_dist.mode() elif hasattr(encoder_output, "latents"): return encoder_output.latents else: raise AttributeError("Could not access latents of provided encoder_output") # Copied from diffusers.pipelines.qwenimage.pipeline_qwenimage.calculate_shift def calculate_shift( image_seq_len, base_seq_len: int = 256, max_seq_len: int = 4096, base_shift: float = 0.5, max_shift: float = 1.15, ): m = (max_shift - base_shift) / (max_seq_len - base_seq_len) b = base_shift - m * base_seq_len mu = image_seq_len * m + b return mu # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.retrieve_timesteps def retrieve_timesteps( scheduler, num_inference_steps: int | None = None, device: str | torch.device | None = None, timesteps: list[int] | None = None, sigmas: list[float] | None = None, **kwargs, ): r""" Calls the scheduler's `set_timesteps` method and retrieves timesteps from the scheduler after the call. Handles custom timesteps. Any kwargs will be supplied to `scheduler.set_timesteps`. Args: scheduler (`SchedulerMixin`): The scheduler to get timesteps from. num_inference_steps (`int`): The number of diffusion steps used when generating samples with a pre-trained model. If used, `timesteps` must be `None`. device (`str` or `torch.device`, *optional*): The device to which the timesteps should be moved to. If `None`, the timesteps are not moved. timesteps (`list[int]`, *optional*): Custom timesteps used to override the timestep spacing strategy of the scheduler. If `timesteps` is passed, `num_inference_steps` and `sigmas` must be `None`. sigmas (`list[float]`, *optional*): Custom sigmas used to override the timestep spacing strategy of the scheduler. If `sigmas` is passed, `num_inference_steps` and `timesteps` must be `None`. Returns: `tuple[torch.Tensor, int]`: A tuple where the first element is the timestep schedule from the scheduler and the second element is the number of inference steps. """ if timesteps is not None and sigmas is not None: raise ValueError("Only one of `timesteps` or `sigmas` can be passed. Please choose one to set custom values") if timesteps is not None: accepts_timesteps = "timesteps" in set(inspect.signature(scheduler.set_timesteps).parameters.keys()) if not accepts_timesteps: raise ValueError( f"The current scheduler class {scheduler.__class__}'s `set_timesteps` does not support custom" f" timestep schedules. Please check whether you are using the correct scheduler." ) scheduler.set_timesteps(timesteps=timesteps, device=device, **kwargs) timesteps = scheduler.timesteps num_inference_steps = len(timesteps) elif sigmas is not None: accept_sigmas = "sigmas" in set(inspect.signature(scheduler.set_timesteps).parameters.keys()) if not accept_sigmas: raise ValueError( f"The current scheduler class {scheduler.__class__}'s `set_timesteps` does not support custom" f" sigmas schedules. Please check whether you are using the correct scheduler." ) scheduler.set_timesteps(sigmas=sigmas, device=device, **kwargs) timesteps = scheduler.timesteps num_inference_steps = len(timesteps) else: scheduler.set_timesteps(num_inference_steps, device=device, **kwargs) timesteps = scheduler.timesteps return timesteps, num_inference_steps class QwenImageInpaintPipeline(DiffusionPipeline, QwenImageLoraLoaderMixin): r""" The QwenImage pipeline for text-to-image generation. Args: transformer ([`QwenImageTransformer2DModel`]): Conditional Transformer (MMDiT) architecture to denoise the encoded image latents. scheduler ([`FlowMatchEulerDiscreteScheduler`]): A scheduler to be used in combination with `transformer` to denoise the encoded image latents. vae ([`AutoencoderKL`]): Variational Auto-Encoder (VAE) Model to encode and decode images to and from latent representations. text_encoder ([`Qwen2.5-VL-7B-Instruct`]): [Qwen2.5-VL-7B-Instruct](https://huggingface.co/Qwen/Qwen2.5-VL-7B-Instruct), specifically the [Qwen2.5-VL-7B-Instruct](https://huggingface.co/Qwen/Qwen2.5-VL-7B-Instruct) variant. tokenizer (`QwenTokenizer`): Tokenizer of class [CLIPTokenizer](https://huggingface.co/docs/transformers/en/model_doc/clip#transformers.CLIPTokenizer). """ model_cpu_offload_seq = "text_encoder->transformer->vae" _callback_tensor_inputs = ["latents", "prompt_embeds"] def __init__( self, scheduler: FlowMatchEulerDiscreteScheduler, vae: AutoencoderKLQwenImage, text_encoder: Qwen2_5_VLForConditionalGeneration, tokenizer: Qwen2Tokenizer, transformer: QwenImageTransformer2DModel, ): super().__init__() self.register_modules( vae=vae, text_encoder=text_encoder, tokenizer=tokenizer, transformer=transformer, scheduler=scheduler, ) self.vae_scale_factor = 2 ** len(self.vae.temperal_downsample) if getattr(self, "vae", None) else 8 # QwenImage latents are turned into 2x2 patches and packed. This means the latent width and height has to be divisible # by the patch size. So the vae scale factor is multiplied by the patch size to account for this self.latent_channels = self.vae.config.z_dim if getattr(self, "vae", None) else 16 self.image_processor = VaeImageProcessor( vae_scale_factor=self.vae_scale_factor * 2, vae_latent_channels=self.latent_channels ) self.mask_processor = VaeImageProcessor( vae_scale_factor=self.vae_scale_factor * 2, vae_latent_channels=self.latent_channels, do_normalize=False, do_binarize=True, do_convert_grayscale=True, ) self.tokenizer_max_length = 1024 self.prompt_template_encode = "<|im_start|>system\nDescribe the image by detailing the color, shape, size, texture, quantity, text, spatial relationships of the objects and background:<|im_end|>\n<|im_start|>user\n{}<|im_end|>\n<|im_start|>assistant\n" self.prompt_template_encode_start_idx = 34 self.default_sample_size = 128 # Copied from diffusers.pipelines.qwenimage.pipeline_qwenimage.QwenImagePipeline._extract_masked_hidden def _extract_masked_hidden(self, hidden_states: torch.Tensor, mask: torch.Tensor): bool_mask = mask.bool() valid_lengths = bool_mask.sum(dim=1) selected = hidden_states[bool_mask] split_result = torch.split(selected, valid_lengths.tolist(), dim=0) return split_result # Copied from diffusers.pipelines.qwenimage.pipeline_qwenimage.QwenImagePipeline._get_qwen_prompt_embeds def _get_qwen_prompt_embeds( self, prompt: str | list[str] = None, device: torch.device | None = None, dtype: torch.dtype | None = None, ): device = device or self._execution_device dtype = dtype or self.text_encoder.dtype prompt = [prompt] if isinstance(prompt, str) else prompt template = self.prompt_template_encode drop_idx = self.prompt_template_encode_start_idx txt = [template.format(e) for e in prompt] txt_tokens = self.tokenizer( txt, max_length=self.tokenizer_max_length + drop_idx, padding=True, truncation=True, return_tensors="pt" ).to(device) encoder_hidden_states = self.text_encoder( input_ids=txt_tokens.input_ids, attention_mask=txt_tokens.attention_mask, output_hidden_states=True, ) hidden_states = encoder_hidden_states.hidden_states[-1] split_hidden_states = self._extract_masked_hidden(hidden_states, txt_tokens.attention_mask) split_hidden_states = [e[drop_idx:] for e in split_hidden_states] attn_mask_list = [torch.ones(e.size(0), dtype=torch.long, device=e.device) for e in split_hidden_states] max_seq_len = max([e.size(0) for e in split_hidden_states]) prompt_embeds = torch.stack( [torch.cat([u, u.new_zeros(max_seq_len - u.size(0), u.size(1))]) for u in split_hidden_states] ) encoder_attention_mask = torch.stack( [torch.cat([u, u.new_zeros(max_seq_len - u.size(0))]) for u in attn_mask_list] ) prompt_embeds = prompt_embeds.to(dtype=dtype, device=device) return prompt_embeds, encoder_attention_mask # Copied from diffusers.pipelines.qwenimage.pipeline_qwenimage_img2img.QwenImageImg2ImgPipeline._encode_vae_image def _encode_vae_image(self, image: torch.Tensor, generator: torch.Generator): if isinstance(generator, list): image_latents = [ retrieve_latents(self.vae.encode(image[i : i + 1]), generator=generator[i]) for i in range(image.shape[0]) ] image_latents = torch.cat(image_latents, dim=0) else: image_latents = retrieve_latents(self.vae.encode(image), generator=generator) latents_mean = ( torch.tensor(self.vae.config.latents_mean) .view(1, self.vae.config.z_dim, 1, 1, 1) .to(image_latents.device, image_latents.dtype) ) latents_std = 1.0 / torch.tensor(self.vae.config.latents_std).view(1, self.vae.config.z_dim, 1, 1, 1).to( image_latents.device, image_latents.dtype ) image_latents = (image_latents - latents_mean) * latents_std return image_latents # Copied from diffusers.pipelines.stable_diffusion_3.pipeline_stable_diffusion_3_img2img.StableDiffusion3Img2ImgPipeline.get_timesteps def get_timesteps(self, num_inference_steps, strength, device): # get the original timestep using init_timestep init_timestep = min(num_inference_steps * strength, num_inference_steps) t_start = int(max(num_inference_steps - init_timestep, 0)) timesteps = self.scheduler.timesteps[t_start * self.scheduler.order :] if hasattr(self.scheduler, "set_begin_index"): self.scheduler.set_begin_index(t_start * self.scheduler.order) return timesteps, num_inference_steps - t_start # Copied from diffusers.pipelines.qwenimage.pipeline_qwenimage.QwenImagePipeline.encode_prompt def encode_prompt( self, prompt: str | list[str], device: torch.device | None = None, num_images_per_prompt: int = 1, prompt_embeds: torch.Tensor | None = None, prompt_embeds_mask: torch.Tensor | None = None, max_sequence_length: int = 1024, ): r""" Args: prompt (`str` or `list[str]`, *optional*): prompt to be encoded device: (`torch.device`): torch device num_images_per_prompt (`int`): number of images that should be generated per prompt prompt_embeds (`torch.Tensor`, *optional*): Pre-generated text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt weighting. If not provided, text embeddings will be generated from `prompt` input argument. """ device = device or self._execution_device prompt = [prompt] if isinstance(prompt, str) else prompt batch_size = len(prompt) if prompt_embeds is None else prompt_embeds.shape[0] if prompt_embeds is None: prompt_embeds, prompt_embeds_mask = self._get_qwen_prompt_embeds(prompt, device) prompt_embeds = prompt_embeds[:, :max_sequence_length] _, seq_len, _ = prompt_embeds.shape prompt_embeds = prompt_embeds.repeat(1, num_images_per_prompt, 1) prompt_embeds = prompt_embeds.view(batch_size * num_images_per_prompt, seq_len, -1) if prompt_embeds_mask is not None: prompt_embeds_mask = prompt_embeds_mask[:, :max_sequence_length] prompt_embeds_mask = prompt_embeds_mask.repeat(1, num_images_per_prompt, 1) prompt_embeds_mask = prompt_embeds_mask.view(batch_size * num_images_per_prompt, seq_len) if prompt_embeds_mask.all(): prompt_embeds_mask = None return prompt_embeds, prompt_embeds_mask def check_inputs( self, prompt, image, mask_image, strength, height, width, output_type, negative_prompt=None, prompt_embeds=None, negative_prompt_embeds=None, prompt_embeds_mask=None, negative_prompt_embeds_mask=None, callback_on_step_end_tensor_inputs=None, padding_mask_crop=None, max_sequence_length=None, ): if strength < 0 or strength > 1: raise ValueError(f"The value of strength should in [0.0, 1.0] but is {strength}") if height % (self.vae_scale_factor * 2) != 0 or width % (self.vae_scale_factor * 2) != 0: logger.warning( f"`height` and `width` have to be divisible by {self.vae_scale_factor * 2} but are {height} and {width}. Dimensions will be resized accordingly" ) if callback_on_step_end_tensor_inputs is not None and not all( k in self._callback_tensor_inputs for k in callback_on_step_end_tensor_inputs ): raise ValueError( f"`callback_on_step_end_tensor_inputs` has to be in {self._callback_tensor_inputs}, but found {[k for k in callback_on_step_end_tensor_inputs if k not in self._callback_tensor_inputs]}" ) if prompt is not None and prompt_embeds is not None: raise ValueError( f"Cannot forward both `prompt`: {prompt} and `prompt_embeds`: {prompt_embeds}. Please make sure to" " only forward one of the two." ) elif prompt is None and prompt_embeds is None: raise ValueError( "Provide either `prompt` or `prompt_embeds`. Cannot leave both `prompt` and `prompt_embeds` undefined." ) elif prompt is not None and (not isinstance(prompt, str) and not isinstance(prompt, list)): raise ValueError(f"`prompt` has to be of type `str` or `list` but is {type(prompt)}") if negative_prompt is not None and negative_prompt_embeds is not None: raise ValueError( f"Cannot forward both `negative_prompt`: {negative_prompt} and `negative_prompt_embeds`:" f" {negative_prompt_embeds}. Please make sure to only forward one of the two." ) if padding_mask_crop is not None: if not isinstance(image, PIL.Image.Image): raise ValueError( f"The image should be a PIL image when inpainting mask crop, but is of type {type(image)}." ) if not isinstance(mask_image, PIL.Image.Image): raise ValueError( f"The mask image should be a PIL image when inpainting mask crop, but is of type" f" {type(mask_image)}." ) if output_type != "pil": raise ValueError(f"The output type should be PIL when inpainting mask crop, but is {output_type}.") if max_sequence_length is not None and max_sequence_length > 1024: raise ValueError(f"`max_sequence_length` cannot be greater than 1024 but is {max_sequence_length}") @staticmethod # Copied from diffusers.pipelines.qwenimage.pipeline_qwenimage.QwenImagePipeline._pack_latents def _pack_latents(latents, batch_size, num_channels_latents, height, width): latents = latents.view(batch_size, num_channels_latents, height // 2, 2, width // 2, 2) latents = latents.permute(0, 2, 4, 1, 3, 5) latents = latents.reshape(batch_size, (height // 2) * (width // 2), num_channels_latents * 4) return latents @staticmethod # Copied from diffusers.pipelines.qwenimage.pipeline_qwenimage.QwenImagePipeline._unpack_latents def _unpack_latents(latents, height, width, vae_scale_factor): batch_size, num_patches, channels = latents.shape # VAE applies 8x compression on images but we must also account for packing which requires # latent height and width to be divisible by 2. height = 2 * (int(height) // (vae_scale_factor * 2)) width = 2 * (int(width) // (vae_scale_factor * 2)) latents = latents.view(batch_size, height // 2, width // 2, channels // 4, 2, 2) latents = latents.permute(0, 3, 1, 4, 2, 5) latents = latents.reshape(batch_size, channels // (2 * 2), 1, height, width) return latents def enable_vae_slicing(self): r""" Enable sliced VAE decoding. When this option is enabled, the VAE will split the input tensor in slices to compute decoding in several steps. This is useful to save some memory and allow larger batch sizes. """ depr_message = f"Calling `enable_vae_slicing()` on a `{self.__class__.__name__}` is deprecated and this method will be removed in a future version. Please use `pipe.vae.enable_slicing()`." deprecate( "enable_vae_slicing", "0.40.0", depr_message, ) self.vae.enable_slicing() def disable_vae_slicing(self): r""" Disable sliced VAE decoding. If `enable_vae_slicing` was previously enabled, this method will go back to computing decoding in one step. """ depr_message = f"Calling `disable_vae_slicing()` on a `{self.__class__.__name__}` is deprecated and this method will be removed in a future version. Please use `pipe.vae.disable_slicing()`." deprecate( "disable_vae_slicing", "0.40.0", depr_message, ) self.vae.disable_slicing() def enable_vae_tiling(self): r""" Enable tiled VAE decoding. When this option is enabled, the VAE will split the input tensor into tiles to compute decoding and encoding in several steps. This is useful for saving a large amount of memory and to allow processing larger images. """ depr_message = f"Calling `enable_vae_tiling()` on a `{self.__class__.__name__}` is deprecated and this method will be removed in a future version. Please use `pipe.vae.enable_tiling()`." deprecate( "enable_vae_tiling", "0.40.0", depr_message, ) self.vae.enable_tiling() def disable_vae_tiling(self): r""" Disable tiled VAE decoding. If `enable_vae_tiling` was previously enabled, this method will go back to computing decoding in one step. """ depr_message = f"Calling `disable_vae_tiling()` on a `{self.__class__.__name__}` is deprecated and this method will be removed in a future version. Please use `pipe.vae.disable_tiling()`." deprecate( "disable_vae_tiling", "0.40.0", depr_message, ) self.vae.disable_tiling() def prepare_latents( self, image, timestep, batch_size, num_channels_latents, height, width, dtype, device, generator, latents=None, ): if isinstance(generator, list) and len(generator) != batch_size: raise ValueError( f"You have passed a list of generators of length {len(generator)}, but requested an effective batch" f" size of {batch_size}. Make sure the batch size matches the length of the generators." ) # VAE applies 8x compression on images but we must also account for packing which requires # latent height and width to be divisible by 2. height = 2 * (int(height) // (self.vae_scale_factor * 2)) width = 2 * (int(width) // (self.vae_scale_factor * 2)) shape = (batch_size, 1, num_channels_latents, height, width) # If image is [B,C,H,W] -> add T=1. If it's already [B,C,T,H,W], leave it. if image.dim() == 4: image = image.unsqueeze(2) elif image.dim() != 5: raise ValueError(f"Expected image dims 4 or 5, got {image.dim()}.") if latents is not None: return latents.to(device=device, dtype=dtype) image = image.to(device=device, dtype=dtype) if image.shape[1] != self.latent_channels: image_latents = self._encode_vae_image(image=image, generator=generator) # [B,z,1,H',W'] else: image_latents = image if batch_size > image_latents.shape[0] and batch_size % image_latents.shape[0] == 0: # expand init_latents for batch_size additional_image_per_prompt = batch_size // image_latents.shape[0] image_latents = torch.cat([image_latents] * additional_image_per_prompt, dim=0) elif batch_size > image_latents.shape[0] and batch_size % image_latents.shape[0] != 0: raise ValueError( f"Cannot duplicate `image` of batch size {image_latents.shape[0]} to {batch_size} text prompts." ) else: image_latents = torch.cat([image_latents], dim=0) image_latents = image_latents.transpose(1, 2) # [B,1,z,H',W'] if latents is None: noise = randn_tensor(shape, generator=generator, device=device, dtype=dtype) latents = self.scheduler.scale_noise(image_latents, timestep, noise) else: noise = latents.to(device) latents = noise noise = self._pack_latents(noise, batch_size, num_channels_latents, height, width) image_latents = self._pack_latents(image_latents, batch_size, num_channels_latents, height, width) latents = self._pack_latents(latents, batch_size, num_channels_latents, height, width) return latents, noise, image_latents def prepare_mask_latents( self, mask, masked_image, batch_size, num_channels_latents, num_images_per_prompt, height, width, dtype, device, generator, ): # VAE applies 8x compression on images but we must also account for packing which requires # latent height and width to be divisible by 2. height = 2 * (int(height) // (self.vae_scale_factor * 2)) width = 2 * (int(width) // (self.vae_scale_factor * 2)) # resize the mask to latents shape as we concatenate the mask to the latents # we do that before converting to dtype to avoid breaking in case we're using cpu_offload # and half precision mask = torch.nn.functional.interpolate(mask, size=(height, width)) mask = mask.to(device=device, dtype=dtype) batch_size = batch_size * num_images_per_prompt if masked_image.dim() == 4: masked_image = masked_image.unsqueeze(2) elif masked_image.dim() != 5: raise ValueError(f"Expected image dims 4 or 5, got {masked_image.dim()}.") masked_image = masked_image.to(device=device, dtype=dtype) if masked_image.shape[1] == self.latent_channels: masked_image_latents = masked_image else: masked_image_latents = self._encode_vae_image(image=masked_image, generator=generator) # duplicate mask and masked_image_latents for each generation per prompt, using mps friendly method if mask.shape[0] < batch_size: if not batch_size % mask.shape[0] == 0: raise ValueError( "The passed mask and the required batch size don't match. Masks are supposed to be duplicated to" f" a total batch size of {batch_size}, but {mask.shape[0]} masks were passed. Make sure the number" " of masks that you pass is divisible by the total requested batch size." ) mask = mask.repeat(batch_size // mask.shape[0], 1, 1, 1) if masked_image_latents.shape[0] < batch_size: if not batch_size % masked_image_latents.shape[0] == 0: raise ValueError( "The passed images and the required batch size don't match. Images are supposed to be duplicated" f" to a total batch size of {batch_size}, but {masked_image_latents.shape[0]} images were passed." " Make sure the number of images that you pass is divisible by the total requested batch size." ) masked_image_latents = masked_image_latents.repeat(batch_size // masked_image_latents.shape[0], 1, 1, 1, 1) # aligning device to prevent device errors when concating it with the latent model input masked_image_latents = masked_image_latents.to(device=device, dtype=dtype) masked_image_latents = self._pack_latents( masked_image_latents, batch_size, num_channels_latents, height, width, ) mask = self._pack_latents( mask.repeat(1, num_channels_latents, 1, 1), batch_size, num_channels_latents, height, width, ) return mask, masked_image_latents @property def guidance_scale(self): return self._guidance_scale @property def attention_kwargs(self): return self._attention_kwargs @property def num_timesteps(self): return self._num_timesteps @property def current_timestep(self): return self._current_timestep @property def interrupt(self): return self._interrupt @torch.no_grad() @replace_example_docstring(EXAMPLE_DOC_STRING) def __call__( self, prompt: str | list[str] = None, negative_prompt: str | list[str] = None, true_cfg_scale: float = 4.0, image: PipelineImageInput = None, mask_image: PipelineImageInput = None, masked_image_latents: PipelineImageInput = None, height: int | None = None, width: int | None = None, padding_mask_crop: int | None = None, strength: float = 0.6, num_inference_steps: int = 50, sigmas: list[float] | None = None, guidance_scale: float | None = None, num_images_per_prompt: int = 1, generator: torch.Generator | list[torch.Generator] | None = None, latents: torch.Tensor | None = None, prompt_embeds: torch.Tensor | None = None, prompt_embeds_mask: torch.Tensor | None = None, negative_prompt_embeds: torch.Tensor | None = None, negative_prompt_embeds_mask: torch.Tensor | None = None, output_type: str | None = "pil", return_dict: bool = True, attention_kwargs: dict[str, Any] | None = None, callback_on_step_end: Callable[[int, int], None] | None = None, callback_on_step_end_tensor_inputs: list[str] = ["latents"], max_sequence_length: int = 512, ): r""" Function invoked when calling the pipeline for generation. Args: prompt (`str` or `list[str]`, *optional*): The prompt or prompts to guide the image generation. If not defined, one has to pass `prompt_embeds`. instead. negative_prompt (`str` or `list[str]`, *optional*): The prompt or prompts not to guide the image generation. If not defined, one has to pass `negative_prompt_embeds` instead. Ignored when not using guidance (i.e., ignored if `true_cfg_scale` is not greater than `1`). image (`torch.Tensor`, `PIL.Image.Image`, `np.ndarray`, `list[torch.Tensor]`, `list[PIL.Image.Image]`, or `list[np.ndarray]`): `Image`, numpy array or tensor representing an image batch to be used as the starting point. For both numpy array and pytorch tensor, the expected value range is between `[0, 1]` If it's a tensor or a list or tensors, the expected shape should be `(B, C, H, W)` or `(C, H, W)`. If it is a numpy array or a list of arrays, the expected shape should be `(B, H, W, C)` or `(H, W, C)` It can also accept image latents as `image`, but if passing latents directly it is not encoded again. true_cfg_scale (`float`, *optional*, defaults to 1.0): Guidance scale as defined in [Classifier-Free Diffusion Guidance](https://huggingface.co/papers/2207.12598). `true_cfg_scale` is defined as `w` of equation 2. of [Imagen Paper](https://huggingface.co/papers/2205.11487). Classifier-free guidance is enabled by setting `true_cfg_scale > 1` and a provided `negative_prompt`. Higher guidance scale encourages to generate images that are closely linked to the text `prompt`, usually at the expense of lower image quality. mask_image (`torch.Tensor`, `PIL.Image.Image`, `np.ndarray`, `list[torch.Tensor]`, `list[PIL.Image.Image]`, or `list[np.ndarray]`): `Image`, numpy array or tensor representing an image batch to mask `image`. White pixels in the mask are repainted while black pixels are preserved. If `mask_image` is a PIL image, it is converted to a single channel (luminance) before use. If it's a numpy array or pytorch tensor, it should contain one color channel (L) instead of 3, so the expected shape for pytorch tensor would be `(B, 1, H, W)`, `(B, H, W)`, `(1, H, W)`, `(H, W)`. And for numpy array would be for `(B, H, W, 1)`, `(B, H, W)`, `(H, W, 1)`, or `(H, W)`. mask_image_latent (`torch.Tensor`, `list[torch.Tensor]`): `Tensor` representing an image batch to mask `image` generated by VAE. If not provided, the mask latents tensor will be generated by `mask_image`. height (`int`, *optional*, defaults to self.unet.config.sample_size * self.vae_scale_factor): The height in pixels of the generated image. This is set to 1024 by default for the best results. width (`int`, *optional*, defaults to self.unet.config.sample_size * self.vae_scale_factor): The width in pixels of the generated image. This is set to 1024 by default for the best results. padding_mask_crop (`int`, *optional*, defaults to `None`): The size of margin in the crop to be applied to the image and masking. If `None`, no crop is applied to image and mask_image. If `padding_mask_crop` is not `None`, it will first find a rectangular region with the same aspect ration of the image and contains all masked area, and then expand that area based on `padding_mask_crop`. The image and mask_image will then be cropped based on the expanded area before resizing to the original image size for inpainting. This is useful when the masked area is small while the image is large and contain information irrelevant for inpainting, such as background. strength (`float`, *optional*, defaults to 1.0): Indicates extent to transform the reference `image`. Must be between 0 and 1. `image` is used as a starting point and more noise is added the higher the `strength`. The number of denoising steps depends on the amount of noise initially added. When `strength` is 1, added noise is maximum and the denoising process runs for the full number of iterations specified in `num_inference_steps`. A value of 1 essentially ignores `image`. num_inference_steps (`int`, *optional*, defaults to 50): The number of denoising steps. More denoising steps usually lead to a higher quality image at the expense of slower inference. sigmas (`list[float]`, *optional*): Custom sigmas to use for the denoising process with schedulers which support a `sigmas` argument in their `set_timesteps` method. If not defined, the default behavior when `num_inference_steps` is passed will be used. guidance_scale (`float`, *optional*, defaults to None): A guidance scale value for guidance distilled models. Unlike the traditional classifier-free guidance where the guidance scale is applied during inference through noise prediction rescaling, guidance distilled models take the guidance scale directly as an input parameter during forward pass. Guidance scale is enabled by setting `guidance_scale > 1`. Higher guidance scale encourages to generate images that are closely linked to the text `prompt`, usually at the expense of lower image quality. This parameter in the pipeline is there to support future guidance-distilled models when they come up. It is ignored when not using guidance distilled models. To enable traditional classifier-free guidance, please pass `true_cfg_scale > 1.0` and `negative_prompt` (even an empty negative prompt like " " should enable classifier-free guidance computations). num_images_per_prompt (`int`, *optional*, defaults to 1): The number of images to generate per prompt. generator (`torch.Generator` or `list[torch.Generator]`, *optional*): One or a list of [torch generator(s)](https://pytorch.org/docs/stable/generated/torch.Generator.html) to make generation deterministic. latents (`torch.Tensor`, *optional*): Pre-generated noisy latents, sampled from a Gaussian distribution, to be used as inputs for image generation. Can be used to tweak the same generation with different prompts. If not provided, a latents tensor will be generated by sampling using the supplied random `generator`. prompt_embeds (`torch.Tensor`, *optional*): Pre-generated text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt weighting. If not provided, text embeddings will be generated from `prompt` input argument. negative_prompt_embeds (`torch.Tensor`, *optional*): Pre-generated negative text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt weighting. If not provided, negative_prompt_embeds will be generated from `negative_prompt` input argument. output_type (`str`, *optional*, defaults to `"pil"`): The output format of the generate image. Choose between [PIL](https://pillow.readthedocs.io/en/stable/): `PIL.Image.Image` or `np.array`. return_dict (`bool`, *optional*, defaults to `True`): Whether or not to return a [`~pipelines.qwenimage.QwenImagePipelineOutput`] instead of a plain tuple. attention_kwargs (`dict`, *optional*): A kwargs dictionary that if specified is passed along to the `AttentionProcessor` as defined under `self.processor` in [diffusers.models.attention_processor](https://github.com/huggingface/diffusers/blob/main/src/diffusers/models/attention_processor.py). callback_on_step_end (`Callable`, *optional*): A function that calls at the end of each denoising steps during the inference. The function is called with the following arguments: `callback_on_step_end(self: DiffusionPipeline, step: int, timestep: int, callback_kwargs: Dict)`. `callback_kwargs` will include a list of all tensors as specified by `callback_on_step_end_tensor_inputs`. callback_on_step_end_tensor_inputs (`list`, *optional*): The list of tensor inputs for the `callback_on_step_end` function. The tensors specified in the list will be passed as `callback_kwargs` argument. You will only be able to include variables listed in the `._callback_tensor_inputs` attribute of your pipeline class. max_sequence_length (`int` defaults to 512): Maximum sequence length to use with the `prompt`. Examples: Returns: [`~pipelines.qwenimage.QwenImagePipelineOutput`] or `tuple`: [`~pipelines.qwenimage.QwenImagePipelineOutput`] if `return_dict` is True, otherwise a `tuple`. When returning a tuple, the first element is a list with the generated images. """ height = height or self.default_sample_size * self.vae_scale_factor width = width or self.default_sample_size * self.vae_scale_factor # 1. Check inputs. Raise error if not correct self.check_inputs( prompt, image, mask_image, strength, height, width, output_type=output_type, negative_prompt=negative_prompt, prompt_embeds=prompt_embeds, negative_prompt_embeds=negative_prompt_embeds, prompt_embeds_mask=prompt_embeds_mask, negative_prompt_embeds_mask=negative_prompt_embeds_mask, callback_on_step_end_tensor_inputs=callback_on_step_end_tensor_inputs, padding_mask_crop=padding_mask_crop, max_sequence_length=max_sequence_length, ) self._guidance_scale = guidance_scale self._attention_kwargs = attention_kwargs self._current_timestep = None self._interrupt = False # 2. Preprocess image if padding_mask_crop is not None: crops_coords = self.mask_processor.get_crop_region(mask_image, width, height, pad=padding_mask_crop) resize_mode = "fill" else: crops_coords = None resize_mode = "default" original_image = image init_image = self.image_processor.preprocess( image, height=height, width=width, crops_coords=crops_coords, resize_mode=resize_mode ) init_image = init_image.to(dtype=torch.float32) # 3. Define call parameters if prompt is not None and isinstance(prompt, str): batch_size = 1 elif prompt is not None and isinstance(prompt, list): batch_size = len(prompt) else: batch_size = prompt_embeds.shape[0] device = self._execution_device has_neg_prompt = negative_prompt is not None or ( negative_prompt_embeds is not None and negative_prompt_embeds_mask is not None ) if true_cfg_scale > 1 and not has_neg_prompt: logger.warning( f"true_cfg_scale is passed as {true_cfg_scale}, but classifier-free guidance is not enabled since no negative_prompt is provided." ) elif true_cfg_scale <= 1 and has_neg_prompt: logger.warning( " negative_prompt is passed but classifier-free guidance is not enabled since true_cfg_scale <= 1" ) do_true_cfg = true_cfg_scale > 1 and has_neg_prompt prompt_embeds, prompt_embeds_mask = self.encode_prompt( prompt=prompt, prompt_embeds=prompt_embeds, prompt_embeds_mask=prompt_embeds_mask, device=device, num_images_per_prompt=num_images_per_prompt, max_sequence_length=max_sequence_length, ) if do_true_cfg: negative_prompt_embeds, negative_prompt_embeds_mask = self.encode_prompt( prompt=negative_prompt, prompt_embeds=negative_prompt_embeds, prompt_embeds_mask=negative_prompt_embeds_mask, device=device, num_images_per_prompt=num_images_per_prompt, max_sequence_length=max_sequence_length, ) # 4. Prepare timesteps sigmas = np.linspace(1.0, 1 / num_inference_steps, num_inference_steps) if sigmas is None else sigmas image_seq_len = (int(height) // self.vae_scale_factor // 2) * (int(width) // self.vae_scale_factor // 2) mu = calculate_shift( image_seq_len, self.scheduler.config.get("base_image_seq_len", 256), self.scheduler.config.get("max_image_seq_len", 4096), self.scheduler.config.get("base_shift", 0.5), self.scheduler.config.get("max_shift", 1.15), ) timesteps, num_inference_steps = retrieve_timesteps( self.scheduler, num_inference_steps, device, sigmas=sigmas, mu=mu, ) timesteps, num_inference_steps = self.get_timesteps(num_inference_steps, strength, device) if num_inference_steps < 1: raise ValueError( f"After adjusting the num_inference_steps by strength parameter: {strength}, the number of pipeline" f"steps is {num_inference_steps} which is < 1 and not appropriate for this pipeline." ) latent_timestep = timesteps[:1].repeat(batch_size * num_images_per_prompt) # 5. Prepare latent variables num_channels_latents = self.transformer.config.in_channels // 4 latents, noise, image_latents = self.prepare_latents( init_image, latent_timestep, batch_size * num_images_per_prompt, num_channels_latents, height, width, prompt_embeds.dtype, device, generator, latents, ) mask_condition = self.mask_processor.preprocess( mask_image, height=height, width=width, resize_mode=resize_mode, crops_coords=crops_coords ) if masked_image_latents is None: masked_image = init_image * (mask_condition < 0.5) else: masked_image = masked_image_latents mask, masked_image_latents = self.prepare_mask_latents( mask_condition, masked_image, batch_size, num_channels_latents, num_images_per_prompt, height, width, prompt_embeds.dtype, device, generator, ) img_shapes = [[(1, height // self.vae_scale_factor // 2, width // self.vae_scale_factor // 2)]] * batch_size num_warmup_steps = max(len(timesteps) - num_inference_steps * self.scheduler.order, 0) self._num_timesteps = len(timesteps) # handle guidance if self.transformer.config.guidance_embeds and guidance_scale is None: raise ValueError("guidance_scale is required for guidance-distilled model.") elif self.transformer.config.guidance_embeds: guidance = torch.full([1], guidance_scale, device=device, dtype=torch.float32) guidance = guidance.expand(latents.shape[0]) elif not self.transformer.config.guidance_embeds and guidance_scale is not None: logger.warning( f"guidance_scale is passed as {guidance_scale}, but ignored since the model is not guidance-distilled." ) guidance = None elif not self.transformer.config.guidance_embeds and guidance_scale is None: guidance = None if self.attention_kwargs is None: self._attention_kwargs = {} # 6. Denoising loop with self.progress_bar(total=num_inference_steps) as progress_bar: for i, t in enumerate(timesteps): if self.interrupt: continue self._current_timestep = t # broadcast to batch dimension in a way that's compatible with ONNX/Core ML timestep = t.expand(latents.shape[0]).to(latents.dtype) with self.transformer.cache_context("cond"): noise_pred = self.transformer( hidden_states=latents, timestep=timestep / 1000, guidance=guidance, encoder_hidden_states_mask=prompt_embeds_mask, encoder_hidden_states=prompt_embeds, img_shapes=img_shapes, attention_kwargs=self.attention_kwargs, return_dict=False, )[0] if do_true_cfg: with self.transformer.cache_context("uncond"): neg_noise_pred = self.transformer( hidden_states=latents, timestep=timestep / 1000, guidance=guidance, encoder_hidden_states_mask=negative_prompt_embeds_mask, encoder_hidden_states=negative_prompt_embeds, img_shapes=img_shapes, attention_kwargs=self.attention_kwargs, return_dict=False, )[0] comb_pred = neg_noise_pred + true_cfg_scale * (noise_pred - neg_noise_pred) cond_norm = torch.norm(noise_pred, dim=-1, keepdim=True) noise_norm = torch.norm(comb_pred, dim=-1, keepdim=True) noise_pred = comb_pred * (cond_norm / noise_norm) # compute the previous noisy sample x_t -> x_t-1 latents_dtype = latents.dtype latents = self.scheduler.step(noise_pred, t, latents, return_dict=False)[0] # for 64 channel transformer only. init_latents_proper = image_latents init_mask = mask if i < len(timesteps) - 1: noise_timestep = timesteps[i + 1] init_latents_proper = self.scheduler.scale_noise( init_latents_proper, torch.tensor([noise_timestep]), noise ) latents = (1 - init_mask) * init_latents_proper + init_mask * latents if latents.dtype != latents_dtype: if torch.backends.mps.is_available(): # some platforms (eg. apple mps) misbehave due to a pytorch bug: https://github.com/pytorch/pytorch/pull/99272 latents = latents.to(latents_dtype) if callback_on_step_end is not None: callback_kwargs = {} for k in callback_on_step_end_tensor_inputs: callback_kwargs[k] = locals()[k] callback_outputs = callback_on_step_end(self, i, t, callback_kwargs) latents = callback_outputs.pop("latents", latents) prompt_embeds = callback_outputs.pop("prompt_embeds", prompt_embeds) # call the callback, if provided if i == len(timesteps) - 1 or ((i + 1) > num_warmup_steps and (i + 1) % self.scheduler.order == 0): progress_bar.update() if XLA_AVAILABLE: xm.mark_step() self._current_timestep = None if output_type == "latent": image = latents else: latents = self._unpack_latents(latents, height, width, self.vae_scale_factor) latents = latents.to(self.vae.dtype) latents_mean = ( torch.tensor(self.vae.config.latents_mean) .view(1, self.vae.config.z_dim, 1, 1, 1) .to(latents.device, latents.dtype) ) latents_std = 1.0 / torch.tensor(self.vae.config.latents_std).view(1, self.vae.config.z_dim, 1, 1, 1).to( latents.device, latents.dtype ) latents = latents / latents_std + latents_mean image = self.vae.decode(latents, return_dict=False)[0][:, :, 0] image = self.image_processor.postprocess(image, output_type=output_type) if padding_mask_crop is not None: image = [ self.image_processor.apply_overlay(mask_image, original_image, i, crops_coords) for i in image ] # Offload all models self.maybe_free_model_hooks() if not return_dict: return (image,) return QwenImagePipelineOutput(images=image)
{ "repo_id": "huggingface/diffusers", "file_path": "src/diffusers/pipelines/qwenimage/pipeline_qwenimage_inpaint.py", "license": "Apache License 2.0", "lines": 919, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
function_complex
huggingface/diffusers:tests/pipelines/qwenimage/test_qwenimage_img2img.py
import random import unittest import numpy as np import torch from transformers import Qwen2_5_VLConfig, Qwen2_5_VLForConditionalGeneration, Qwen2Tokenizer from diffusers import ( AutoencoderKLQwenImage, FlowMatchEulerDiscreteScheduler, QwenImageImg2ImgPipeline, QwenImageTransformer2DModel, ) from ...testing_utils import ( enable_full_determinism, floats_tensor, torch_device, ) from ..test_pipelines_common import PipelineTesterMixin, to_np enable_full_determinism() class QwenImageImg2ImgPipelineFastTests(unittest.TestCase, PipelineTesterMixin): pipeline_class = QwenImageImg2ImgPipeline params = frozenset(["prompt", "image", "height", "width", "guidance_scale", "true_cfg_scale", "strength"]) batch_params = frozenset(["prompt", "image"]) image_params = frozenset(["image"]) image_latents_params = frozenset(["latents"]) required_optional_params = frozenset( [ "num_inference_steps", "generator", "latents", "return_dict", "callback_on_step_end", "callback_on_step_end_tensor_inputs", ] ) supports_dduf = False test_xformers_attention = False test_attention_slicing = True test_layerwise_casting = True test_group_offloading = True def get_dummy_components(self): torch.manual_seed(0) transformer = QwenImageTransformer2DModel( patch_size=2, in_channels=16, out_channels=4, num_layers=2, attention_head_dim=16, num_attention_heads=3, joint_attention_dim=16, guidance_embeds=False, axes_dims_rope=(8, 4, 4), ) torch.manual_seed(0) z_dim = 4 vae = AutoencoderKLQwenImage( base_dim=z_dim * 6, z_dim=z_dim, dim_mult=[1, 2, 4], num_res_blocks=1, temperal_downsample=[False, True], latents_mean=[0.0] * 4, latents_std=[1.0] * 4, ) torch.manual_seed(0) scheduler = FlowMatchEulerDiscreteScheduler() torch.manual_seed(0) config = Qwen2_5_VLConfig( text_config={ "hidden_size": 16, "intermediate_size": 16, "num_hidden_layers": 2, "num_attention_heads": 2, "num_key_value_heads": 2, "rope_scaling": { "mrope_section": [1, 1, 2], "rope_type": "default", "type": "default", }, "rope_theta": 1000000.0, }, vision_config={ "depth": 2, "hidden_size": 16, "intermediate_size": 16, "num_heads": 2, "out_hidden_size": 16, }, hidden_size=16, vocab_size=152064, vision_end_token_id=151653, vision_start_token_id=151652, vision_token_id=151654, ) text_encoder = Qwen2_5_VLForConditionalGeneration(config) tokenizer = Qwen2Tokenizer.from_pretrained("hf-internal-testing/tiny-random-Qwen2VLForConditionalGeneration") return { "transformer": transformer, "vae": vae, "scheduler": scheduler, "text_encoder": text_encoder, "tokenizer": tokenizer, } def get_dummy_inputs(self, device, seed=0): image = floats_tensor((1, 3, 32, 32), rng=random.Random(seed)).to(device) if str(device).startswith("mps"): generator = torch.manual_seed(seed) else: generator = torch.Generator(device="cpu").manual_seed(seed) inputs = { "image": image, "prompt": "dance monkey", "negative_prompt": "bad quality", "generator": generator, "num_inference_steps": 2, "guidance_scale": 3.0, "true_cfg_scale": 1.0, "height": 32, "width": 32, "max_sequence_length": 16, "output_type": "pt", } return inputs def test_inference(self): device = "cpu" components = self.get_dummy_components() pipe = self.pipeline_class(**components) pipe.to(device) pipe.set_progress_bar_config(disable=None) inputs = self.get_dummy_inputs(device) image = pipe(**inputs).images generated_image = image[0] self.assertEqual(generated_image.shape, (3, 32, 32)) def test_inference_batch_single_identical(self): self._test_inference_batch_single_identical(batch_size=3, expected_max_diff=1e-1) def test_attention_slicing_forward_pass( self, test_max_difference=True, test_mean_pixel_difference=True, expected_max_diff=1e-3 ): if not self.test_attention_slicing: return components = self.get_dummy_components() pipe = self.pipeline_class(**components) for component in pipe.components.values(): if hasattr(component, "set_default_attn_processor"): component.set_default_attn_processor() pipe.to(torch_device) pipe.set_progress_bar_config(disable=None) generator_device = "cpu" inputs = self.get_dummy_inputs(generator_device) output_without_slicing = pipe(**inputs).images[0] pipe.enable_attention_slicing(slice_size=1) inputs = self.get_dummy_inputs(generator_device) output_with_slicing1 = pipe(**inputs).images[0] pipe.enable_attention_slicing(slice_size=2) inputs = self.get_dummy_inputs(generator_device) output_with_slicing2 = pipe(**inputs).images[0] if test_max_difference: max_diff1 = np.abs(to_np(output_with_slicing1) - to_np(output_without_slicing)).max() max_diff2 = np.abs(to_np(output_with_slicing2) - to_np(output_without_slicing)).max() self.assertLess( max(max_diff1, max_diff2), expected_max_diff, "Attention slicing should not affect the inference results", ) def test_vae_tiling(self, expected_diff_max: float = 0.2): generator_device = "cpu" components = self.get_dummy_components() pipe = self.pipeline_class(**components) pipe.to("cpu") pipe.set_progress_bar_config(disable=None) # Without tiling inputs = self.get_dummy_inputs(generator_device) inputs["height"] = inputs["width"] = 128 output_without_tiling = pipe(**inputs)[0] # With tiling pipe.vae.enable_tiling( tile_sample_min_height=96, tile_sample_min_width=96, tile_sample_stride_height=64, tile_sample_stride_width=64, ) inputs = self.get_dummy_inputs(generator_device) inputs["height"] = inputs["width"] = 128 output_with_tiling = pipe(**inputs)[0] self.assertLess( (to_np(output_without_tiling) - to_np(output_with_tiling)).max(), expected_diff_max, "VAE tiling should not affect the inference results", )
{ "repo_id": "huggingface/diffusers", "file_path": "tests/pipelines/qwenimage/test_qwenimage_img2img.py", "license": "Apache License 2.0", "lines": 188, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
test
huggingface/diffusers:tests/pipelines/qwenimage/test_qwenimage_inpaint.py
# Copyright 2025 The HuggingFace Team. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import random import unittest import numpy as np import torch from transformers import Qwen2_5_VLConfig, Qwen2_5_VLForConditionalGeneration, Qwen2Tokenizer from diffusers import ( AutoencoderKLQwenImage, FlowMatchEulerDiscreteScheduler, QwenImageInpaintPipeline, QwenImageTransformer2DModel, ) from ...testing_utils import enable_full_determinism, floats_tensor, torch_device from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS from ..test_pipelines_common import PipelineTesterMixin, to_np enable_full_determinism() class QwenImageInpaintPipelineFastTests(PipelineTesterMixin, unittest.TestCase): pipeline_class = QwenImageInpaintPipeline params = TEXT_TO_IMAGE_PARAMS - {"cross_attention_kwargs"} batch_params = TEXT_TO_IMAGE_BATCH_PARAMS image_params = TEXT_TO_IMAGE_IMAGE_PARAMS image_latents_params = TEXT_TO_IMAGE_IMAGE_PARAMS required_optional_params = frozenset( [ "num_inference_steps", "generator", "latents", "return_dict", "callback_on_step_end", "callback_on_step_end_tensor_inputs", ] ) supports_dduf = False test_xformers_attention = False test_layerwise_casting = True test_group_offloading = True def get_dummy_components(self): torch.manual_seed(0) transformer = QwenImageTransformer2DModel( patch_size=2, in_channels=16, out_channels=4, num_layers=2, attention_head_dim=16, num_attention_heads=3, joint_attention_dim=16, guidance_embeds=False, axes_dims_rope=(8, 4, 4), ) torch.manual_seed(0) z_dim = 4 vae = AutoencoderKLQwenImage( base_dim=z_dim * 6, z_dim=z_dim, dim_mult=[1, 2, 4], num_res_blocks=1, temperal_downsample=[False, True], # fmt: off latents_mean=[0.0] * 4, latents_std=[1.0] * 4, # fmt: on ) torch.manual_seed(0) scheduler = FlowMatchEulerDiscreteScheduler() torch.manual_seed(0) config = Qwen2_5_VLConfig( text_config={ "hidden_size": 16, "intermediate_size": 16, "num_hidden_layers": 2, "num_attention_heads": 2, "num_key_value_heads": 2, "rope_scaling": { "mrope_section": [1, 1, 2], "rope_type": "default", "type": "default", }, "rope_theta": 1000000.0, }, vision_config={ "depth": 2, "hidden_size": 16, "intermediate_size": 16, "num_heads": 2, "out_hidden_size": 16, }, hidden_size=16, vocab_size=152064, vision_end_token_id=151653, vision_start_token_id=151652, vision_token_id=151654, ) text_encoder = Qwen2_5_VLForConditionalGeneration(config) tokenizer = Qwen2Tokenizer.from_pretrained("hf-internal-testing/tiny-random-Qwen2VLForConditionalGeneration") components = { "transformer": transformer, "vae": vae, "scheduler": scheduler, "text_encoder": text_encoder, "tokenizer": tokenizer, } return components def get_dummy_inputs(self, device, seed=0): image = floats_tensor((1, 3, 32, 32), rng=random.Random(seed)).to(device) mask_image = torch.ones((1, 1, 32, 32)).to(device) if str(device).startswith("mps"): generator = torch.manual_seed(seed) else: generator = torch.Generator(device=device).manual_seed(seed) inputs = { "prompt": "dance monkey", "negative_prompt": "bad quality", "image": image, "mask_image": mask_image, "generator": generator, "num_inference_steps": 2, "guidance_scale": 3.0, "true_cfg_scale": 1.0, "height": 32, "width": 32, "max_sequence_length": 16, "output_type": "pt", } return inputs def test_inference(self): device = "cpu" components = self.get_dummy_components() pipe = self.pipeline_class(**components) pipe.to(device) pipe.set_progress_bar_config(disable=None) inputs = self.get_dummy_inputs(device) image = pipe(**inputs).images generated_image = image[0] self.assertEqual(generated_image.shape, (3, 32, 32)) def test_inference_batch_single_identical(self): self._test_inference_batch_single_identical(batch_size=3, expected_max_diff=1e-1) def test_attention_slicing_forward_pass( self, test_max_difference=True, test_mean_pixel_difference=True, expected_max_diff=1e-3 ): if not self.test_attention_slicing: return components = self.get_dummy_components() pipe = self.pipeline_class(**components) for component in pipe.components.values(): if hasattr(component, "set_default_attn_processor"): component.set_default_attn_processor() pipe.to(torch_device) pipe.set_progress_bar_config(disable=None) generator_device = "cpu" inputs = self.get_dummy_inputs(generator_device) output_without_slicing = pipe(**inputs)[0] pipe.enable_attention_slicing(slice_size=1) inputs = self.get_dummy_inputs(generator_device) output_with_slicing1 = pipe(**inputs)[0] pipe.enable_attention_slicing(slice_size=2) inputs = self.get_dummy_inputs(generator_device) output_with_slicing2 = pipe(**inputs)[0] if test_max_difference: max_diff1 = np.abs(to_np(output_with_slicing1) - to_np(output_without_slicing)).max() max_diff2 = np.abs(to_np(output_with_slicing2) - to_np(output_without_slicing)).max() self.assertLess( max(max_diff1, max_diff2), expected_max_diff, "Attention slicing should not affect the inference results", ) def test_vae_tiling(self, expected_diff_max: float = 0.2): generator_device = "cpu" components = self.get_dummy_components() pipe = self.pipeline_class(**components) pipe.to("cpu") pipe.set_progress_bar_config(disable=None) # Without tiling inputs = self.get_dummy_inputs(generator_device) inputs["height"] = inputs["width"] = 128 output_without_tiling = pipe(**inputs)[0] # With tiling pipe.vae.enable_tiling( tile_sample_min_height=96, tile_sample_min_width=96, tile_sample_stride_height=64, tile_sample_stride_width=64, ) inputs = self.get_dummy_inputs(generator_device) inputs["height"] = inputs["width"] = 128 output_with_tiling = pipe(**inputs)[0] self.assertLess( (to_np(output_without_tiling) - to_np(output_with_tiling)).max(), expected_diff_max, "VAE tiling should not affect the inference results", )
{ "repo_id": "huggingface/diffusers", "file_path": "tests/pipelines/qwenimage/test_qwenimage_inpaint.py", "license": "Apache License 2.0", "lines": 202, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
test
huggingface/diffusers:tests/models/transformers/test_models_transformer_qwenimage.py
# coding=utf-8 # Copyright 2025 HuggingFace Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest import torch from diffusers import QwenImageTransformer2DModel from diffusers.models.transformers.transformer_qwenimage import compute_text_seq_len_from_mask from ...testing_utils import enable_full_determinism, torch_device from ..test_modeling_common import ModelTesterMixin, TorchCompileTesterMixin enable_full_determinism() class QwenImageTransformerTests(ModelTesterMixin, unittest.TestCase): model_class = QwenImageTransformer2DModel main_input_name = "hidden_states" # We override the items here because the transformer under consideration is small. model_split_percents = [0.7, 0.6, 0.6] # Skip setting testing with default: AttnProcessor uses_custom_attn_processor = True @property def dummy_input(self): return self.prepare_dummy_input() @property def input_shape(self): return (16, 16) @property def output_shape(self): return (16, 16) def prepare_dummy_input(self, height=4, width=4): batch_size = 1 num_latent_channels = embedding_dim = 16 sequence_length = 7 vae_scale_factor = 4 hidden_states = torch.randn((batch_size, height * width, num_latent_channels)).to(torch_device) encoder_hidden_states = torch.randn((batch_size, sequence_length, embedding_dim)).to(torch_device) encoder_hidden_states_mask = torch.ones((batch_size, sequence_length)).to(torch_device, torch.long) timestep = torch.tensor([1.0]).to(torch_device).expand(batch_size) orig_height = height * 2 * vae_scale_factor orig_width = width * 2 * vae_scale_factor img_shapes = [(1, orig_height // vae_scale_factor // 2, orig_width // vae_scale_factor // 2)] * batch_size return { "hidden_states": hidden_states, "encoder_hidden_states": encoder_hidden_states, "encoder_hidden_states_mask": encoder_hidden_states_mask, "timestep": timestep, "img_shapes": img_shapes, } def prepare_init_args_and_inputs_for_common(self): init_dict = { "patch_size": 2, "in_channels": 16, "out_channels": 4, "num_layers": 2, "attention_head_dim": 16, "num_attention_heads": 3, "joint_attention_dim": 16, "guidance_embeds": False, "axes_dims_rope": (8, 4, 4), } inputs_dict = self.dummy_input return init_dict, inputs_dict def test_gradient_checkpointing_is_applied(self): expected_set = {"QwenImageTransformer2DModel"} super().test_gradient_checkpointing_is_applied(expected_set=expected_set) def test_infers_text_seq_len_from_mask(self): """Test that compute_text_seq_len_from_mask correctly infers sequence lengths and returns tensors.""" init_dict, inputs = self.prepare_init_args_and_inputs_for_common() model = self.model_class(**init_dict).to(torch_device) # Test 1: Contiguous mask with padding at the end (only first 2 tokens valid) encoder_hidden_states_mask = inputs["encoder_hidden_states_mask"].clone() encoder_hidden_states_mask[:, 2:] = 0 # Only first 2 tokens are valid rope_text_seq_len, per_sample_len, normalized_mask = compute_text_seq_len_from_mask( inputs["encoder_hidden_states"], encoder_hidden_states_mask ) # Verify rope_text_seq_len is returned as an int (for torch.compile compatibility) self.assertIsInstance(rope_text_seq_len, int) # Verify per_sample_len is computed correctly (max valid position + 1 = 2) self.assertIsInstance(per_sample_len, torch.Tensor) self.assertEqual(int(per_sample_len.max().item()), 2) # Verify mask is normalized to bool dtype self.assertTrue(normalized_mask.dtype == torch.bool) self.assertEqual(normalized_mask.sum().item(), 2) # Only 2 True values # Verify rope_text_seq_len is at least the sequence length self.assertGreaterEqual(rope_text_seq_len, inputs["encoder_hidden_states"].shape[1]) # Test 2: Verify model runs successfully with inferred values inputs["encoder_hidden_states_mask"] = normalized_mask with torch.no_grad(): output = model(**inputs) self.assertEqual(output.sample.shape[1], inputs["hidden_states"].shape[1]) # Test 3: Different mask pattern (padding at beginning) encoder_hidden_states_mask2 = inputs["encoder_hidden_states_mask"].clone() encoder_hidden_states_mask2[:, :3] = 0 # First 3 tokens are padding encoder_hidden_states_mask2[:, 3:] = 1 # Last 4 tokens are valid rope_text_seq_len2, per_sample_len2, normalized_mask2 = compute_text_seq_len_from_mask( inputs["encoder_hidden_states"], encoder_hidden_states_mask2 ) # Max valid position is 6 (last token), so per_sample_len should be 7 self.assertEqual(int(per_sample_len2.max().item()), 7) self.assertEqual(normalized_mask2.sum().item(), 4) # 4 True values # Test 4: No mask provided (None case) rope_text_seq_len_none, per_sample_len_none, normalized_mask_none = compute_text_seq_len_from_mask( inputs["encoder_hidden_states"], None ) self.assertEqual(rope_text_seq_len_none, inputs["encoder_hidden_states"].shape[1]) self.assertIsInstance(rope_text_seq_len_none, int) self.assertIsNone(per_sample_len_none) self.assertIsNone(normalized_mask_none) def test_non_contiguous_attention_mask(self): """Test that non-contiguous masks work correctly (e.g., [1, 0, 1, 0, 1, 0, 0])""" init_dict, inputs = self.prepare_init_args_and_inputs_for_common() model = self.model_class(**init_dict).to(torch_device) # Create a non-contiguous mask pattern: valid, padding, valid, padding, etc. encoder_hidden_states_mask = inputs["encoder_hidden_states_mask"].clone() # Pattern: [True, False, True, False, True, False, False] encoder_hidden_states_mask[:, 1] = 0 encoder_hidden_states_mask[:, 3] = 0 encoder_hidden_states_mask[:, 5:] = 0 inferred_rope_len, per_sample_len, normalized_mask = compute_text_seq_len_from_mask( inputs["encoder_hidden_states"], encoder_hidden_states_mask ) self.assertEqual(int(per_sample_len.max().item()), 5) self.assertEqual(inferred_rope_len, inputs["encoder_hidden_states"].shape[1]) self.assertIsInstance(inferred_rope_len, int) self.assertTrue(normalized_mask.dtype == torch.bool) inputs["encoder_hidden_states_mask"] = normalized_mask with torch.no_grad(): output = model(**inputs) self.assertEqual(output.sample.shape[1], inputs["hidden_states"].shape[1]) def test_txt_seq_lens_deprecation(self): """Test that passing txt_seq_lens raises a deprecation warning.""" init_dict, inputs = self.prepare_init_args_and_inputs_for_common() model = self.model_class(**init_dict).to(torch_device) # Prepare inputs with txt_seq_lens (deprecated parameter) txt_seq_lens = [inputs["encoder_hidden_states"].shape[1]] # Remove encoder_hidden_states_mask to use the deprecated path inputs_with_deprecated = inputs.copy() inputs_with_deprecated.pop("encoder_hidden_states_mask") inputs_with_deprecated["txt_seq_lens"] = txt_seq_lens # Test that deprecation warning is raised with self.assertWarns(FutureWarning) as warning_context: with torch.no_grad(): output = model(**inputs_with_deprecated) # Verify the warning message mentions the deprecation warning_message = str(warning_context.warning) self.assertIn("txt_seq_lens", warning_message) self.assertIn("deprecated", warning_message) self.assertIn("encoder_hidden_states_mask", warning_message) # Verify the model still works correctly despite the deprecation self.assertEqual(output.sample.shape[1], inputs["hidden_states"].shape[1]) def test_layered_model_with_mask(self): """Test QwenImageTransformer2DModel with use_layer3d_rope=True (layered model).""" # Create layered model config init_dict = { "patch_size": 2, "in_channels": 16, "out_channels": 4, "num_layers": 2, "attention_head_dim": 16, "num_attention_heads": 3, "joint_attention_dim": 16, "axes_dims_rope": (8, 4, 4), # Must match attention_head_dim (8+4+4=16) "use_layer3d_rope": True, # Enable layered RoPE "use_additional_t_cond": True, # Enable additional time conditioning } model = self.model_class(**init_dict).to(torch_device) # Verify the model uses QwenEmbedLayer3DRope from diffusers.models.transformers.transformer_qwenimage import QwenEmbedLayer3DRope self.assertIsInstance(model.pos_embed, QwenEmbedLayer3DRope) # Test single generation with layered structure batch_size = 1 text_seq_len = 7 img_h, img_w = 4, 4 layers = 4 # For layered model: (layers + 1) because we have N layers + 1 combined image hidden_states = torch.randn(batch_size, (layers + 1) * img_h * img_w, 16).to(torch_device) encoder_hidden_states = torch.randn(batch_size, text_seq_len, 16).to(torch_device) # Create mask with some padding encoder_hidden_states_mask = torch.ones(batch_size, text_seq_len).to(torch_device) encoder_hidden_states_mask[0, 5:] = 0 # Only 5 valid tokens timestep = torch.tensor([1.0]).to(torch_device) # additional_t_cond for use_additional_t_cond=True (0 or 1 index for embedding) addition_t_cond = torch.tensor([0], dtype=torch.long).to(torch_device) # Layer structure: 4 layers + 1 condition image img_shapes = [ [ (1, img_h, img_w), # layer 0 (1, img_h, img_w), # layer 1 (1, img_h, img_w), # layer 2 (1, img_h, img_w), # layer 3 (1, img_h, img_w), # condition image (last one gets special treatment) ] ] with torch.no_grad(): output = model( hidden_states=hidden_states, encoder_hidden_states=encoder_hidden_states, encoder_hidden_states_mask=encoder_hidden_states_mask, timestep=timestep, img_shapes=img_shapes, additional_t_cond=addition_t_cond, ) self.assertEqual(output.sample.shape[1], hidden_states.shape[1]) class QwenImageTransformerCompileTests(TorchCompileTesterMixin, unittest.TestCase): model_class = QwenImageTransformer2DModel def prepare_init_args_and_inputs_for_common(self): return QwenImageTransformerTests().prepare_init_args_and_inputs_for_common() def prepare_dummy_input(self, height, width): return QwenImageTransformerTests().prepare_dummy_input(height=height, width=width) def test_torch_compile_recompilation_and_graph_break(self): super().test_torch_compile_recompilation_and_graph_break() def test_torch_compile_with_and_without_mask(self): """Test that torch.compile works with both None mask and padding mask.""" init_dict, inputs = self.prepare_init_args_and_inputs_for_common() model = self.model_class(**init_dict).to(torch_device) model.eval() model.compile(mode="default", fullgraph=True) # Test 1: Run with None mask (no padding, all tokens are valid) inputs_no_mask = inputs.copy() inputs_no_mask["encoder_hidden_states_mask"] = None # First run to allow compilation with torch.no_grad(): output_no_mask = model(**inputs_no_mask) # Second run to verify no recompilation with ( torch._inductor.utils.fresh_inductor_cache(), torch._dynamo.config.patch(error_on_recompile=True), torch.no_grad(), ): output_no_mask_2 = model(**inputs_no_mask) self.assertEqual(output_no_mask.sample.shape[1], inputs["hidden_states"].shape[1]) self.assertEqual(output_no_mask_2.sample.shape[1], inputs["hidden_states"].shape[1]) # Test 2: Run with all-ones mask (should behave like None) inputs_all_ones = inputs.copy() # Keep the all-ones mask self.assertTrue(inputs_all_ones["encoder_hidden_states_mask"].all().item()) # First run to allow compilation with torch.no_grad(): output_all_ones = model(**inputs_all_ones) # Second run to verify no recompilation with ( torch._inductor.utils.fresh_inductor_cache(), torch._dynamo.config.patch(error_on_recompile=True), torch.no_grad(), ): output_all_ones_2 = model(**inputs_all_ones) self.assertEqual(output_all_ones.sample.shape[1], inputs["hidden_states"].shape[1]) self.assertEqual(output_all_ones_2.sample.shape[1], inputs["hidden_states"].shape[1]) # Test 3: Run with actual padding mask (has zeros) inputs_with_padding = inputs.copy() mask_with_padding = inputs["encoder_hidden_states_mask"].clone() mask_with_padding[:, 4:] = 0 # Last 3 tokens are padding inputs_with_padding["encoder_hidden_states_mask"] = mask_with_padding # First run to allow compilation with torch.no_grad(): output_with_padding = model(**inputs_with_padding) # Second run to verify no recompilation with ( torch._inductor.utils.fresh_inductor_cache(), torch._dynamo.config.patch(error_on_recompile=True), torch.no_grad(), ): output_with_padding_2 = model(**inputs_with_padding) self.assertEqual(output_with_padding.sample.shape[1], inputs["hidden_states"].shape[1]) self.assertEqual(output_with_padding_2.sample.shape[1], inputs["hidden_states"].shape[1]) # Verify that outputs are different (mask should affect results) self.assertFalse(torch.allclose(output_no_mask.sample, output_with_padding.sample, atol=1e-3))
{ "repo_id": "huggingface/diffusers", "file_path": "tests/models/transformers/test_models_transformer_qwenimage.py", "license": "Apache License 2.0", "lines": 275, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
test
huggingface/diffusers:tests/modular_pipelines/stable_diffusion_xl/test_modular_pipeline_stable_diffusion_xl.py
# coding=utf-8 # Copyright 2025 HuggingFace Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import random from typing import Any, Dict import numpy as np import torch from PIL import Image from diffusers import ClassifierFreeGuidance, StableDiffusionXLAutoBlocks, StableDiffusionXLModularPipeline from diffusers.loaders import ModularIPAdapterMixin from ...models.unets.test_models_unet_2d_condition import create_ip_adapter_state_dict from ...testing_utils import enable_full_determinism, floats_tensor, torch_device from ..test_modular_pipelines_common import ModularGuiderTesterMixin, ModularPipelineTesterMixin enable_full_determinism() class SDXLModularTesterMixin: """ This mixin defines method to create pipeline, base input and base test across all SDXL modular tests. """ def _test_stable_diffusion_xl_euler(self, expected_image_shape, expected_slice, expected_max_diff=1e-2): sd_pipe = self.get_pipeline().to(torch_device) inputs = self.get_dummy_inputs() image = sd_pipe(**inputs, output="images") image_slice = image[0, -3:, -3:, -1].cpu() assert image.shape == expected_image_shape max_diff = torch.abs(image_slice.flatten() - expected_slice).max() assert max_diff < expected_max_diff, f"Image slice does not match expected slice. Max Difference: {max_diff}" class SDXLModularIPAdapterTesterMixin: """ This mixin is designed to test IP Adapter. """ def test_pipeline_inputs_and_blocks(self): blocks = self.pipeline_blocks_class() parameters = blocks.input_names assert issubclass(self.pipeline_class, ModularIPAdapterMixin) assert "ip_adapter_image" in parameters, ( "`ip_adapter_image` argument must be supported by the `__call__` method" ) assert "ip_adapter" in blocks.sub_blocks, "pipeline must contain an IPAdapter block" _ = blocks.sub_blocks.pop("ip_adapter") parameters = blocks.input_names assert "ip_adapter_image" not in parameters, ( "`ip_adapter_image` argument must be removed from the `__call__` method" ) def _get_dummy_image_embeds(self, cross_attention_dim: int = 32): return torch.randn((1, 1, cross_attention_dim), device=torch_device) def _get_dummy_faceid_image_embeds(self, cross_attention_dim: int = 32): return torch.randn((1, 1, 1, cross_attention_dim), device=torch_device) def _get_dummy_masks(self, input_size: int = 64): _masks = torch.zeros((1, 1, input_size, input_size), device=torch_device) _masks[0, :, :, : int(input_size / 2)] = 1 return _masks def _modify_inputs_for_ip_adapter_test(self, inputs: Dict[str, Any]): blocks = self.pipeline_blocks_class() _ = blocks.sub_blocks.pop("ip_adapter") parameters = blocks.input_names if "image" in parameters and "strength" in parameters: inputs["num_inference_steps"] = 4 inputs["output_type"] = "pt" return inputs def test_ip_adapter(self, expected_max_diff: float = 1e-4, expected_pipe_slice=None): r"""Tests for IP-Adapter. The following scenarios are tested: - Single IP-Adapter with scale=0 should produce same output as no IP-Adapter. - Multi IP-Adapter with scale=0 should produce same output as no IP-Adapter. - Single IP-Adapter with scale!=0 should produce different output compared to no IP-Adapter. - Multi IP-Adapter with scale!=0 should produce different output compared to no IP-Adapter. """ # Raising the tolerance for this test when it's run on a CPU because we # compare against static slices and that can be shaky (with a VVVV low probability). expected_max_diff = 9e-4 if torch_device == "cpu" else expected_max_diff blocks = self.pipeline_blocks_class() _ = blocks.sub_blocks.pop("ip_adapter") pipe = blocks.init_pipeline(self.pretrained_model_name_or_path) pipe.load_components(torch_dtype=torch.float32) pipe = pipe.to(torch_device) cross_attention_dim = pipe.unet.config.get("cross_attention_dim") # forward pass without ip adapter inputs = self._modify_inputs_for_ip_adapter_test(self.get_dummy_inputs()) if expected_pipe_slice is None: output_without_adapter = pipe(**inputs, output="images") else: output_without_adapter = expected_pipe_slice # 1. Single IP-Adapter test cases adapter_state_dict = create_ip_adapter_state_dict(pipe.unet) pipe.unet._load_ip_adapter_weights(adapter_state_dict) # forward pass with single ip adapter, but scale=0 which should have no effect inputs = self._modify_inputs_for_ip_adapter_test(self.get_dummy_inputs()) inputs["ip_adapter_embeds"] = [self._get_dummy_image_embeds(cross_attention_dim)] inputs["negative_ip_adapter_embeds"] = [self._get_dummy_image_embeds(cross_attention_dim)] pipe.set_ip_adapter_scale(0.0) output_without_adapter_scale = pipe(**inputs, output="images") if expected_pipe_slice is not None: output_without_adapter_scale = output_without_adapter_scale[0, -3:, -3:, -1].flatten() # forward pass with single ip adapter, but with scale of adapter weights inputs = self._modify_inputs_for_ip_adapter_test(self.get_dummy_inputs()) inputs["ip_adapter_embeds"] = [self._get_dummy_image_embeds(cross_attention_dim)] inputs["negative_ip_adapter_embeds"] = [self._get_dummy_image_embeds(cross_attention_dim)] pipe.set_ip_adapter_scale(42.0) output_with_adapter_scale = pipe(**inputs, output="images") if expected_pipe_slice is not None: output_with_adapter_scale = output_with_adapter_scale[0, -3:, -3:, -1].flatten() max_diff_without_adapter_scale = torch.abs(output_without_adapter_scale - output_without_adapter).max() max_diff_with_adapter_scale = torch.abs(output_with_adapter_scale - output_without_adapter).max() assert max_diff_without_adapter_scale < expected_max_diff, ( "Output without ip-adapter must be same as normal inference" ) assert max_diff_with_adapter_scale > 1e-2, "Output with ip-adapter must be different from normal inference" # 2. Multi IP-Adapter test cases adapter_state_dict_1 = create_ip_adapter_state_dict(pipe.unet) adapter_state_dict_2 = create_ip_adapter_state_dict(pipe.unet) pipe.unet._load_ip_adapter_weights([adapter_state_dict_1, adapter_state_dict_2]) # forward pass with multi ip adapter, but scale=0 which should have no effect inputs = self._modify_inputs_for_ip_adapter_test(self.get_dummy_inputs()) inputs["ip_adapter_embeds"] = [self._get_dummy_image_embeds(cross_attention_dim)] * 2 inputs["negative_ip_adapter_embeds"] = [self._get_dummy_image_embeds(cross_attention_dim)] * 2 pipe.set_ip_adapter_scale([0.0, 0.0]) output_without_multi_adapter_scale = pipe(**inputs, output="images") if expected_pipe_slice is not None: output_without_multi_adapter_scale = output_without_multi_adapter_scale[0, -3:, -3:, -1].flatten() # forward pass with multi ip adapter, but with scale of adapter weights inputs = self._modify_inputs_for_ip_adapter_test(self.get_dummy_inputs()) inputs["ip_adapter_embeds"] = [self._get_dummy_image_embeds(cross_attention_dim)] * 2 inputs["negative_ip_adapter_embeds"] = [self._get_dummy_image_embeds(cross_attention_dim)] * 2 pipe.set_ip_adapter_scale([42.0, 42.0]) output_with_multi_adapter_scale = pipe(**inputs, output="images") if expected_pipe_slice is not None: output_with_multi_adapter_scale = output_with_multi_adapter_scale[0, -3:, -3:, -1].flatten() max_diff_without_multi_adapter_scale = torch.abs( output_without_multi_adapter_scale - output_without_adapter ).max() max_diff_with_multi_adapter_scale = torch.abs(output_with_multi_adapter_scale - output_without_adapter).max() assert max_diff_without_multi_adapter_scale < expected_max_diff, ( "Output without multi-ip-adapter must be same as normal inference" ) assert max_diff_with_multi_adapter_scale > 1e-2, ( "Output with multi-ip-adapter scale must be different from normal inference" ) class SDXLModularControlNetTesterMixin: """ This mixin is designed to test ControlNet. """ def test_pipeline_inputs(self): blocks = self.pipeline_blocks_class() parameters = blocks.input_names assert "control_image" in parameters, "`control_image` argument must be supported by the `__call__` method" assert "controlnet_conditioning_scale" in parameters, ( "`controlnet_conditioning_scale` argument must be supported by the `__call__` method" ) def _modify_inputs_for_controlnet_test(self, inputs: Dict[str, Any]): controlnet_embedder_scale_factor = 2 image = torch.randn( (1, 3, 32 * controlnet_embedder_scale_factor, 32 * controlnet_embedder_scale_factor), device=torch_device, ) inputs["control_image"] = image return inputs def test_controlnet(self, expected_max_diff: float = 1e-4, expected_pipe_slice=None): r"""Tests for ControlNet. The following scenarios are tested: - Single ControlNet with scale=0 should produce same output as no ControlNet. - Single ControlNet with scale!=0 should produce different output compared to no ControlNet. """ # Raising the tolerance for this test when it's run on a CPU because we # compare against static slices and that can be shaky (with a VVVV low probability). expected_max_diff = 9e-4 if torch_device == "cpu" else expected_max_diff pipe = self.get_pipeline().to(torch_device) # forward pass without controlnet inputs = self.get_dummy_inputs() output_without_controlnet = pipe(**inputs, output="images") output_without_controlnet = output_without_controlnet[0, -3:, -3:, -1].flatten() # forward pass with single controlnet, but scale=0 which should have no effect inputs = self._modify_inputs_for_controlnet_test(self.get_dummy_inputs()) inputs["controlnet_conditioning_scale"] = 0.0 output_without_controlnet_scale = pipe(**inputs, output="images") output_without_controlnet_scale = output_without_controlnet_scale[0, -3:, -3:, -1].flatten() # forward pass with single controlnet, but with scale of adapter weights inputs = self._modify_inputs_for_controlnet_test(self.get_dummy_inputs()) inputs["controlnet_conditioning_scale"] = 42.0 output_with_controlnet_scale = pipe(**inputs, output="images") output_with_controlnet_scale = output_with_controlnet_scale[0, -3:, -3:, -1].flatten() max_diff_without_controlnet_scale = torch.abs( output_without_controlnet_scale - output_without_controlnet ).max() max_diff_with_controlnet_scale = torch.abs(output_with_controlnet_scale - output_without_controlnet).max() assert max_diff_without_controlnet_scale < expected_max_diff, ( "Output without controlnet must be same as normal inference" ) assert max_diff_with_controlnet_scale > 1e-2, "Output with controlnet must be different from normal inference" def test_controlnet_cfg(self): pipe = self.get_pipeline().to(torch_device) # forward pass with CFG not applied guider = ClassifierFreeGuidance(guidance_scale=1.0) pipe.update_components(guider=guider) inputs = self._modify_inputs_for_controlnet_test(self.get_dummy_inputs()) out_no_cfg = pipe(**inputs, output="images") # forward pass with CFG applied guider = ClassifierFreeGuidance(guidance_scale=7.5) pipe.update_components(guider=guider) inputs = self._modify_inputs_for_controlnet_test(self.get_dummy_inputs()) out_cfg = pipe(**inputs, output="images") assert out_cfg.shape == out_no_cfg.shape max_diff = torch.abs(out_cfg - out_no_cfg).max() assert max_diff > 1e-2, "Output with CFG must be different from normal inference" TEXT2IMAGE_WORKFLOWS = { "text2image": [ ("text_encoder", "StableDiffusionXLTextEncoderStep"), ("denoise.input", "StableDiffusionXLInputStep"), ("denoise.before_denoise.set_timesteps", "StableDiffusionXLSetTimestepsStep"), ("denoise.before_denoise.prepare_latents", "StableDiffusionXLPrepareLatentsStep"), ("denoise.before_denoise.prepare_add_cond", "StableDiffusionXLPrepareAdditionalConditioningStep"), ("denoise.denoise", "StableDiffusionXLDenoiseStep"), ("decode", "StableDiffusionXLDecodeStep"), ], "controlnet_text2image": [ ("text_encoder", "StableDiffusionXLTextEncoderStep"), ("denoise.input", "StableDiffusionXLInputStep"), ("denoise.before_denoise.set_timesteps", "StableDiffusionXLSetTimestepsStep"), ("denoise.before_denoise.prepare_latents", "StableDiffusionXLPrepareLatentsStep"), ("denoise.before_denoise.prepare_add_cond", "StableDiffusionXLPrepareAdditionalConditioningStep"), ("denoise.controlnet_input", "StableDiffusionXLControlNetInputStep"), ("denoise.denoise", "StableDiffusionXLControlNetDenoiseStep"), ("decode", "StableDiffusionXLDecodeStep"), ], "controlnet_union_text2image": [ ("text_encoder", "StableDiffusionXLTextEncoderStep"), ("denoise.input", "StableDiffusionXLInputStep"), ("denoise.before_denoise.set_timesteps", "StableDiffusionXLSetTimestepsStep"), ("denoise.before_denoise.prepare_latents", "StableDiffusionXLPrepareLatentsStep"), ("denoise.before_denoise.prepare_add_cond", "StableDiffusionXLPrepareAdditionalConditioningStep"), ("denoise.controlnet_input", "StableDiffusionXLControlNetUnionInputStep"), ("denoise.denoise", "StableDiffusionXLControlNetDenoiseStep"), ("decode", "StableDiffusionXLDecodeStep"), ], "ip_adapter_text2image": [ ("text_encoder", "StableDiffusionXLTextEncoderStep"), ("ip_adapter", "StableDiffusionXLIPAdapterStep"), ("denoise.input", "StableDiffusionXLInputStep"), ("denoise.before_denoise.set_timesteps", "StableDiffusionXLSetTimestepsStep"), ("denoise.before_denoise.prepare_latents", "StableDiffusionXLPrepareLatentsStep"), ("denoise.before_denoise.prepare_add_cond", "StableDiffusionXLPrepareAdditionalConditioningStep"), ("denoise.denoise", "StableDiffusionXLDenoiseStep"), ("decode", "StableDiffusionXLDecodeStep"), ], "ip_adapter_controlnet_text2image": [ ("text_encoder", "StableDiffusionXLTextEncoderStep"), ("ip_adapter", "StableDiffusionXLIPAdapterStep"), ("denoise.input", "StableDiffusionXLInputStep"), ("denoise.before_denoise.set_timesteps", "StableDiffusionXLSetTimestepsStep"), ("denoise.before_denoise.prepare_latents", "StableDiffusionXLPrepareLatentsStep"), ("denoise.before_denoise.prepare_add_cond", "StableDiffusionXLPrepareAdditionalConditioningStep"), ("denoise.controlnet_input", "StableDiffusionXLControlNetInputStep"), ("denoise.denoise", "StableDiffusionXLControlNetDenoiseStep"), ("decode", "StableDiffusionXLDecodeStep"), ], } class TestSDXLModularPipelineFast( SDXLModularTesterMixin, SDXLModularIPAdapterTesterMixin, SDXLModularControlNetTesterMixin, ModularGuiderTesterMixin, ModularPipelineTesterMixin, ): """Test cases for Stable Diffusion XL modular pipeline fast tests.""" pipeline_class = StableDiffusionXLModularPipeline pipeline_blocks_class = StableDiffusionXLAutoBlocks pretrained_model_name_or_path = "hf-internal-testing/tiny-sdxl-modular" params = frozenset( [ "prompt", "height", "width", "negative_prompt", "cross_attention_kwargs", ] ) batch_params = frozenset(["prompt", "negative_prompt"]) expected_image_output_shape = (1, 3, 64, 64) expected_workflow_blocks = TEXT2IMAGE_WORKFLOWS def get_dummy_inputs(self, seed=0): generator = self.get_generator(seed) inputs = { "prompt": "A painting of a squirrel eating a burger", "generator": generator, "num_inference_steps": 2, "output_type": "pt", } return inputs def test_stable_diffusion_xl_euler(self): self._test_stable_diffusion_xl_euler( expected_image_shape=self.expected_image_output_shape, expected_slice=torch.tensor( [0.3886, 0.4685, 0.4953, 0.4217, 0.4317, 0.3945, 0.4847, 0.4704, 0.4731], ), expected_max_diff=1e-2, ) def test_inference_batch_single_identical(self): super().test_inference_batch_single_identical(expected_max_diff=3e-3) IMAGE2IMAGE_WORKFLOWS = { "image2image": [ ("text_encoder", "StableDiffusionXLTextEncoderStep"), ("vae_encoder", "StableDiffusionXLVaeEncoderStep"), ("denoise.input", "StableDiffusionXLInputStep"), ("denoise.before_denoise.set_timesteps", "StableDiffusionXLImg2ImgSetTimestepsStep"), ("denoise.before_denoise.prepare_latents", "StableDiffusionXLImg2ImgPrepareLatentsStep"), ("denoise.before_denoise.prepare_add_cond", "StableDiffusionXLImg2ImgPrepareAdditionalConditioningStep"), ("denoise.denoise", "StableDiffusionXLDenoiseStep"), ("decode", "StableDiffusionXLDecodeStep"), ], "controlnet_image2image": [ ("text_encoder", "StableDiffusionXLTextEncoderStep"), ("vae_encoder", "StableDiffusionXLVaeEncoderStep"), ("denoise.input", "StableDiffusionXLInputStep"), ("denoise.before_denoise.set_timesteps", "StableDiffusionXLImg2ImgSetTimestepsStep"), ("denoise.before_denoise.prepare_latents", "StableDiffusionXLImg2ImgPrepareLatentsStep"), ("denoise.before_denoise.prepare_add_cond", "StableDiffusionXLImg2ImgPrepareAdditionalConditioningStep"), ("denoise.controlnet_input", "StableDiffusionXLControlNetInputStep"), ("denoise.denoise", "StableDiffusionXLControlNetDenoiseStep"), ("decode", "StableDiffusionXLDecodeStep"), ], "controlnet_union_image2image": [ ("text_encoder", "StableDiffusionXLTextEncoderStep"), ("vae_encoder", "StableDiffusionXLVaeEncoderStep"), ("denoise.input", "StableDiffusionXLInputStep"), ("denoise.before_denoise.set_timesteps", "StableDiffusionXLImg2ImgSetTimestepsStep"), ("denoise.before_denoise.prepare_latents", "StableDiffusionXLImg2ImgPrepareLatentsStep"), ("denoise.before_denoise.prepare_add_cond", "StableDiffusionXLImg2ImgPrepareAdditionalConditioningStep"), ("denoise.controlnet_input", "StableDiffusionXLControlNetUnionInputStep"), ("denoise.denoise", "StableDiffusionXLControlNetDenoiseStep"), ("decode", "StableDiffusionXLDecodeStep"), ], "ip_adapter_image2image": [ ("text_encoder", "StableDiffusionXLTextEncoderStep"), ("ip_adapter", "StableDiffusionXLIPAdapterStep"), ("vae_encoder", "StableDiffusionXLVaeEncoderStep"), ("denoise.input", "StableDiffusionXLInputStep"), ("denoise.before_denoise.set_timesteps", "StableDiffusionXLImg2ImgSetTimestepsStep"), ("denoise.before_denoise.prepare_latents", "StableDiffusionXLImg2ImgPrepareLatentsStep"), ("denoise.before_denoise.prepare_add_cond", "StableDiffusionXLImg2ImgPrepareAdditionalConditioningStep"), ("denoise.denoise", "StableDiffusionXLDenoiseStep"), ("decode", "StableDiffusionXLDecodeStep"), ], "ip_adapter_controlnet_image2image": [ ("text_encoder", "StableDiffusionXLTextEncoderStep"), ("ip_adapter", "StableDiffusionXLIPAdapterStep"), ("vae_encoder", "StableDiffusionXLVaeEncoderStep"), ("denoise.input", "StableDiffusionXLInputStep"), ("denoise.before_denoise.set_timesteps", "StableDiffusionXLImg2ImgSetTimestepsStep"), ("denoise.before_denoise.prepare_latents", "StableDiffusionXLImg2ImgPrepareLatentsStep"), ("denoise.before_denoise.prepare_add_cond", "StableDiffusionXLImg2ImgPrepareAdditionalConditioningStep"), ("denoise.controlnet_input", "StableDiffusionXLControlNetInputStep"), ("denoise.denoise", "StableDiffusionXLControlNetDenoiseStep"), ("decode", "StableDiffusionXLDecodeStep"), ], } class TestSDXLImg2ImgModularPipelineFast( SDXLModularTesterMixin, SDXLModularIPAdapterTesterMixin, SDXLModularControlNetTesterMixin, ModularGuiderTesterMixin, ModularPipelineTesterMixin, ): """Test cases for Stable Diffusion XL image-to-image modular pipeline fast tests.""" pipeline_class = StableDiffusionXLModularPipeline pipeline_blocks_class = StableDiffusionXLAutoBlocks pretrained_model_name_or_path = "hf-internal-testing/tiny-sdxl-modular" params = frozenset( [ "prompt", "height", "width", "negative_prompt", "cross_attention_kwargs", "image", ] ) batch_params = frozenset(["prompt", "negative_prompt", "image"]) expected_image_output_shape = (1, 3, 64, 64) expected_workflow_blocks = IMAGE2IMAGE_WORKFLOWS def get_dummy_inputs(self, seed=0): generator = self.get_generator(seed) inputs = { "prompt": "A painting of a squirrel eating a burger", "generator": generator, "num_inference_steps": 4, "output_type": "pt", } image = floats_tensor((1, 3, 32, 32), rng=random.Random(seed)).to(torch_device) image = image.cpu().permute(0, 2, 3, 1)[0] init_image = Image.fromarray(np.uint8(image)).convert("RGB").resize((64, 64)) inputs["image"] = init_image inputs["strength"] = 0.5 return inputs def test_stable_diffusion_xl_euler(self): self._test_stable_diffusion_xl_euler( expected_image_shape=self.expected_image_output_shape, expected_slice=torch.tensor([0.5246, 0.4466, 0.444, 0.3246, 0.4443, 0.5108, 0.5225, 0.559, 0.5147]), expected_max_diff=1e-2, ) def test_inference_batch_single_identical(self): super().test_inference_batch_single_identical(expected_max_diff=3e-3) INPAINTING_WORKFLOWS = { "inpainting": [ ("text_encoder", "StableDiffusionXLTextEncoderStep"), ("vae_encoder", "StableDiffusionXLInpaintVaeEncoderStep"), ("input", "StableDiffusionXLInputStep"), ("set_timesteps", "StableDiffusionXLImg2ImgSetTimestepsStep"), ("prepare_latents", "StableDiffusionXLInpaintPrepareLatentsStep"), ("prepare_add_cond", "StableDiffusionXLImg2ImgPrepareAdditionalConditioningStep"), ("denoise", "StableDiffusionXLInpaintDenoiseStep"), ("decode", "StableDiffusionXLInpaintDecodeStep"), ], "controlnet_inpainting": [ ("text_encoder", "StableDiffusionXLTextEncoderStep"), ("vae_encoder", "StableDiffusionXLInpaintVaeEncoderStep"), ("input", "StableDiffusionXLInputStep"), ("set_timesteps", "StableDiffusionXLImg2ImgSetTimestepsStep"), ("prepare_latents", "StableDiffusionXLInpaintPrepareLatentsStep"), ("prepare_add_cond", "StableDiffusionXLImg2ImgPrepareAdditionalConditioningStep"), ("controlnet_input", "StableDiffusionXLControlNetInputStep"), ("denoise", "StableDiffusionXLInpaintControlNetDenoiseStep"), ("decode", "StableDiffusionXLInpaintDecodeStep"), ], "controlnet_union_inpainting": [ ("text_encoder", "StableDiffusionXLTextEncoderStep"), ("vae_encoder", "StableDiffusionXLInpaintVaeEncoderStep"), ("input", "StableDiffusionXLInputStep"), ("set_timesteps", "StableDiffusionXLImg2ImgSetTimestepsStep"), ("prepare_latents", "StableDiffusionXLInpaintPrepareLatentsStep"), ("prepare_add_cond", "StableDiffusionXLImg2ImgPrepareAdditionalConditioningStep"), ("controlnet_input", "StableDiffusionXLControlNetUnionInputStep"), ("denoise", "StableDiffusionXLInpaintControlNetDenoiseStep"), ("decode", "StableDiffusionXLInpaintDecodeStep"), ], "ip_adapter_inpainting": [ ("text_encoder", "StableDiffusionXLTextEncoderStep"), ("ip_adapter", "StableDiffusionXLIPAdapterStep"), ("vae_encoder", "StableDiffusionXLInpaintVaeEncoderStep"), ("input", "StableDiffusionXLInputStep"), ("set_timesteps", "StableDiffusionXLImg2ImgSetTimestepsStep"), ("prepare_latents", "StableDiffusionXLInpaintPrepareLatentsStep"), ("prepare_add_cond", "StableDiffusionXLImg2ImgPrepareAdditionalConditioningStep"), ("denoise", "StableDiffusionXLInpaintDenoiseStep"), ("decode", "StableDiffusionXLInpaintDecodeStep"), ], "ip_adapter_controlnet_inpainting": [ ("text_encoder", "StableDiffusionXLTextEncoderStep"), ("ip_adapter", "StableDiffusionXLIPAdapterStep"), ("vae_encoder", "StableDiffusionXLInpaintVaeEncoderStep"), ("input", "StableDiffusionXLInputStep"), ("set_timesteps", "StableDiffusionXLImg2ImgSetTimestepsStep"), ("prepare_latents", "StableDiffusionXLInpaintPrepareLatentsStep"), ("prepare_add_cond", "StableDiffusionXLImg2ImgPrepareAdditionalConditioningStep"), ("controlnet_input", "StableDiffusionXLControlNetInputStep"), ("denoise", "StableDiffusionXLInpaintControlNetDenoiseStep"), ("decode", "StableDiffusionXLInpaintDecodeStep"), ], } class SDXLInpaintingModularPipelineFastTests( SDXLModularTesterMixin, SDXLModularIPAdapterTesterMixin, SDXLModularControlNetTesterMixin, ModularGuiderTesterMixin, ModularPipelineTesterMixin, ): """Test cases for Stable Diffusion XL inpainting modular pipeline fast tests.""" pipeline_class = StableDiffusionXLModularPipeline pipeline_blocks_class = StableDiffusionXLAutoBlocks pretrained_model_name_or_path = "hf-internal-testing/tiny-sdxl-modular" params = frozenset( [ "prompt", "height", "width", "negative_prompt", "cross_attention_kwargs", "image", "mask_image", ] ) batch_params = frozenset(["prompt", "negative_prompt", "image", "mask_image"]) expected_image_output_shape = (1, 3, 64, 64) expected_workflow_blocks = INPAINTING_WORKFLOWS def get_dummy_inputs(self, device, seed=0): generator = self.get_generator(seed) inputs = { "prompt": "A painting of a squirrel eating a burger", "generator": generator, "num_inference_steps": 4, "output_type": "pt", } image = floats_tensor((1, 3, 32, 32), rng=random.Random(seed)).to(device) image = image.cpu().permute(0, 2, 3, 1)[0] init_image = Image.fromarray(np.uint8(image)).convert("RGB").resize((64, 64)) # create mask image[8:, 8:, :] = 255 mask_image = Image.fromarray(np.uint8(image)).convert("L").resize((64, 64)) inputs["image"] = init_image inputs["mask_image"] = mask_image inputs["strength"] = 1.0 return inputs def test_stable_diffusion_xl_euler(self): self._test_stable_diffusion_xl_euler( expected_image_shape=self.expected_image_output_shape, expected_slice=torch.tensor( [ 0.40872607, 0.38842705, 0.34893104, 0.47837183, 0.43792963, 0.5332134, 0.3716843, 0.47274873, 0.45000193, ], device=torch_device, ), expected_max_diff=1e-2, ) def test_inference_batch_single_identical(self): super().test_inference_batch_single_identical(expected_max_diff=3e-3)
{ "repo_id": "huggingface/diffusers", "file_path": "tests/modular_pipelines/stable_diffusion_xl/test_modular_pipeline_stable_diffusion_xl.py", "license": "Apache License 2.0", "lines": 532, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
test
huggingface/diffusers:tests/modular_pipelines/test_modular_pipelines_common.py
import gc import json import os import tempfile from typing import Callable import pytest import torch import diffusers from diffusers import AutoModel, ComponentsManager, ModularPipeline, ModularPipelineBlocks from diffusers.guiders import ClassifierFreeGuidance from diffusers.modular_pipelines.modular_pipeline_utils import ( ComponentSpec, ConfigSpec, InputParam, OutputParam, generate_modular_model_card_content, ) from diffusers.utils import logging from ..testing_utils import backend_empty_cache, numpy_cosine_similarity_distance, require_accelerator, torch_device class ModularPipelineTesterMixin: """ It provides a set of common tests for each modular pipeline, including: - test_pipeline_call_signature: check if the pipeline's __call__ method has all required parameters - test_inference_batch_consistent: check if the pipeline's __call__ method can handle batch inputs - test_inference_batch_single_identical: check if the pipeline's __call__ method can handle single input - test_float16_inference: check if the pipeline's __call__ method can handle float16 inputs - test_to_device: check if the pipeline's __call__ method can handle different devices """ # Canonical parameters that are passed to `__call__` regardless # of the type of pipeline. They are always optional and have common # sense default values. optional_params = frozenset(["num_inference_steps", "num_images_per_prompt", "latents", "output_type"]) # this is modular specific: generator needs to be a intermediate input because it's mutable intermediate_params = frozenset(["generator"]) # Output type for the pipeline (e.g., "images" for image pipelines, "videos" for video pipelines) # Subclasses can override this to change the expected output type output_name = "images" def get_generator(self, seed=0): generator = torch.Generator("cpu").manual_seed(seed) return generator @property def pipeline_class(self) -> Callable | ModularPipeline: raise NotImplementedError( "You need to set the attribute `pipeline_class = ClassNameOfPipeline` in the child test class. " "See existing pipeline tests for reference." ) @property def pretrained_model_name_or_path(self) -> str: raise NotImplementedError( "You need to set the attribute `pretrained_model_name_or_path` in the child test class. See existing pipeline tests for reference." ) @property def pipeline_blocks_class(self) -> Callable | ModularPipelineBlocks: raise NotImplementedError( "You need to set the attribute `pipeline_blocks_class = ClassNameOfPipelineBlocks` in the child test class. " "See existing pipeline tests for reference." ) def get_dummy_inputs(self, seed=0): raise NotImplementedError( "You need to implement `get_dummy_inputs(self, device, seed)` in the child test class. " "See existing pipeline tests for reference." ) @property def params(self) -> frozenset: raise NotImplementedError( "You need to set the attribute `params` in the child test class. " "`params` are checked for if all values are present in `__call__`'s signature." " You can set `params` using one of the common set of parameters defined in `pipeline_params.py`" " e.g., `TEXT_TO_IMAGE_PARAMS` defines the common parameters used in text to " "image pipelines, including prompts and prompt embedding overrides." "If your pipeline's set of arguments has minor changes from one of the common sets of arguments, " "do not make modifications to the existing common sets of arguments. I.e. a text to image pipeline " "with non-configurable height and width arguments should set the attribute as " "`params = TEXT_TO_IMAGE_PARAMS - {'height', 'width'}`. " "See existing pipeline tests for reference." ) @property def batch_params(self) -> frozenset: raise NotImplementedError( "You need to set the attribute `batch_params` in the child test class. " "`batch_params` are the parameters required to be batched when passed to the pipeline's " "`__call__` method. `pipeline_params.py` provides some common sets of parameters such as " "`TEXT_TO_IMAGE_BATCH_PARAMS`, `IMAGE_VARIATION_BATCH_PARAMS`, etc... If your pipeline's " "set of batch arguments has minor changes from one of the common sets of batch arguments, " "do not make modifications to the existing common sets of batch arguments. I.e. a text to " "image pipeline `negative_prompt` is not batched should set the attribute as " "`batch_params = TEXT_TO_IMAGE_BATCH_PARAMS - {'negative_prompt'}`. " "See existing pipeline tests for reference." ) @property def expected_workflow_blocks(self) -> dict: raise NotImplementedError( "You need to set the attribute `expected_workflow_blocks` in the child test class. " "`expected_workflow_blocks` is a dictionary that maps workflow names to list of block names. " "See existing pipeline tests for reference." ) def setup_method(self): # clean up the VRAM before each test torch.compiler.reset() gc.collect() backend_empty_cache(torch_device) def teardown_method(self): # clean up the VRAM after each test in case of CUDA runtime errors torch.compiler.reset() gc.collect() backend_empty_cache(torch_device) def get_pipeline(self, components_manager=None, torch_dtype=torch.float32): pipeline = self.pipeline_blocks_class().init_pipeline( self.pretrained_model_name_or_path, components_manager=components_manager ) pipeline.load_components(torch_dtype=torch_dtype) pipeline.set_progress_bar_config(disable=None) return pipeline def test_pipeline_call_signature(self): pipe = self.get_pipeline() input_parameters = pipe.blocks.input_names optional_parameters = pipe.default_call_parameters def _check_for_parameters(parameters, expected_parameters, param_type): remaining_parameters = {param for param in parameters if param not in expected_parameters} assert len(remaining_parameters) == 0, ( f"Required {param_type} parameters not present: {remaining_parameters}" ) _check_for_parameters(self.params, input_parameters, "input") _check_for_parameters(self.optional_params, optional_parameters, "optional") def test_inference_batch_consistent(self, batch_sizes=[2], batch_generator=True): pipe = self.get_pipeline().to(torch_device) inputs = self.get_dummy_inputs() inputs["generator"] = self.get_generator(0) logger = logging.get_logger(pipe.__module__) logger.setLevel(level=diffusers.logging.FATAL) # prepare batched inputs batched_inputs = [] for batch_size in batch_sizes: batched_input = {} batched_input.update(inputs) for name in self.batch_params: if name not in inputs: continue value = inputs[name] batched_input[name] = batch_size * [value] if batch_generator and "generator" in inputs: batched_input["generator"] = [self.get_generator(i) for i in range(batch_size)] if "batch_size" in inputs: batched_input["batch_size"] = batch_size batched_inputs.append(batched_input) logger.setLevel(level=diffusers.logging.WARNING) for batch_size, batched_input in zip(batch_sizes, batched_inputs): output = pipe(**batched_input, output=self.output_name) assert len(output) == batch_size, "Output is different from expected batch size" def test_inference_batch_single_identical( self, batch_size=2, expected_max_diff=1e-4, ): pipe = self.get_pipeline().to(torch_device) inputs = self.get_dummy_inputs() # Reset generator in case it is has been used in self.get_dummy_inputs inputs["generator"] = self.get_generator(0) logger = logging.get_logger(pipe.__module__) logger.setLevel(level=diffusers.logging.FATAL) # batchify inputs batched_inputs = {} batched_inputs.update(inputs) for name in self.batch_params: if name not in inputs: continue value = inputs[name] batched_inputs[name] = batch_size * [value] if "generator" in inputs: batched_inputs["generator"] = [self.get_generator(i) for i in range(batch_size)] if "batch_size" in inputs: batched_inputs["batch_size"] = batch_size output = pipe(**inputs, output=self.output_name) output_batch = pipe(**batched_inputs, output=self.output_name) assert output_batch.shape[0] == batch_size # For batch comparison, we only need to compare the first item if output_batch.shape[0] == batch_size and output.shape[0] == 1: output_batch = output_batch[0:1] max_diff = torch.abs(output_batch - output).max() assert max_diff < expected_max_diff, "Batch inference results different from single inference results" @require_accelerator def test_float16_inference(self, expected_max_diff=5e-2): pipe = self.get_pipeline() pipe.to(torch_device, torch.float32) pipe_fp16 = self.get_pipeline() pipe_fp16.to(torch_device, torch.float16) inputs = self.get_dummy_inputs() # Reset generator in case it is used inside dummy inputs if "generator" in inputs: inputs["generator"] = self.get_generator(0) output = pipe(**inputs, output=self.output_name) fp16_inputs = self.get_dummy_inputs() # Reset generator in case it is used inside dummy inputs if "generator" in fp16_inputs: fp16_inputs["generator"] = self.get_generator(0) output_fp16 = pipe_fp16(**fp16_inputs, output=self.output_name) output_tensor = output.float().cpu() output_fp16_tensor = output_fp16.float().cpu() # Check for NaNs in outputs (can happen with tiny models in FP16) if torch.isnan(output_tensor).any() or torch.isnan(output_fp16_tensor).any(): pytest.skip("FP16 inference produces NaN values - this is a known issue with tiny models") max_diff = numpy_cosine_similarity_distance( output_tensor.flatten().numpy(), output_fp16_tensor.flatten().numpy() ) # Check if cosine similarity is NaN (which can happen if vectors are zero or very small) if torch.isnan(torch.tensor(max_diff)): pytest.skip("Cosine similarity is NaN - outputs may be too small for reliable comparison") assert max_diff < expected_max_diff, f"FP16 inference is different from FP32 inference (max_diff: {max_diff})" @require_accelerator def test_to_device(self): pipe = self.get_pipeline().to("cpu") model_devices = [ component.device.type for component in pipe.components.values() if hasattr(component, "device") ] assert all(device == "cpu" for device in model_devices), "All pipeline components are not on CPU" pipe.to(torch_device) model_devices = [ component.device.type for component in pipe.components.values() if hasattr(component, "device") ] assert all(device == torch_device for device in model_devices), ( "All pipeline components are not on accelerator device" ) def test_inference_is_not_nan_cpu(self): pipe = self.get_pipeline().to("cpu") inputs = self.get_dummy_inputs() output = pipe(**inputs, output=self.output_name) assert torch.isnan(output).sum() == 0, "CPU Inference returns NaN" @require_accelerator def test_inference_is_not_nan(self): pipe = self.get_pipeline().to(torch_device) inputs = self.get_dummy_inputs() output = pipe(**inputs, output=self.output_name) assert torch.isnan(output).sum() == 0, "Accelerator Inference returns NaN" def test_num_images_per_prompt(self): pipe = self.get_pipeline().to(torch_device) if "num_images_per_prompt" not in pipe.blocks.input_names: pytest.mark.skip("Skipping test as `num_images_per_prompt` is not present in input names.") batch_sizes = [1, 2] num_images_per_prompts = [1, 2] for batch_size in batch_sizes: for num_images_per_prompt in num_images_per_prompts: inputs = self.get_dummy_inputs() for key in inputs.keys(): if key in self.batch_params: inputs[key] = batch_size * [inputs[key]] images = pipe(**inputs, num_images_per_prompt=num_images_per_prompt, output=self.output_name) assert images.shape[0] == batch_size * num_images_per_prompt @require_accelerator def test_components_auto_cpu_offload_inference_consistent(self): base_pipe = self.get_pipeline().to(torch_device) cm = ComponentsManager() cm.enable_auto_cpu_offload(device=torch_device) offload_pipe = self.get_pipeline(components_manager=cm) image_slices = [] for pipe in [base_pipe, offload_pipe]: inputs = self.get_dummy_inputs() image = pipe(**inputs, output=self.output_name) image_slices.append(image[0, -3:, -3:, -1].flatten()) assert torch.abs(image_slices[0] - image_slices[1]).max() < 1e-3 def test_save_from_pretrained(self): pipes = [] base_pipe = self.get_pipeline().to(torch_device) pipes.append(base_pipe) with tempfile.TemporaryDirectory() as tmpdirname: base_pipe.save_pretrained(tmpdirname) pipe = ModularPipeline.from_pretrained(tmpdirname).to(torch_device) pipe.load_components(torch_dtype=torch.float32) pipe.to(torch_device) pipes.append(pipe) image_slices = [] for pipe in pipes: inputs = self.get_dummy_inputs() image = pipe(**inputs, output=self.output_name) image_slices.append(image[0, -3:, -3:, -1].flatten()) assert torch.abs(image_slices[0] - image_slices[1]).max() < 1e-3 def test_modular_index_consistency(self): pipe = self.get_pipeline() components_spec = pipe._component_specs components = sorted(components_spec.keys()) with tempfile.TemporaryDirectory() as tmpdir: pipe.save_pretrained(tmpdir) index_file = os.path.join(tmpdir, "modular_model_index.json") assert os.path.exists(index_file) with open(index_file) as f: index_contents = json.load(f) compulsory_keys = {"_blocks_class_name", "_class_name", "_diffusers_version"} for k in compulsory_keys: assert k in index_contents to_check_attrs = {"pretrained_model_name_or_path", "revision", "subfolder"} for component in components: spec = components_spec[component] for attr in to_check_attrs: if getattr(spec, "pretrained_model_name_or_path", None) is not None: for attr in to_check_attrs: assert component in index_contents, f"{component} should be present in index but isn't." attr_value_from_index = index_contents[component][2][attr] assert getattr(spec, attr) == attr_value_from_index def test_workflow_map(self): blocks = self.pipeline_blocks_class() if blocks._workflow_map is None: pytest.skip("Skipping test as _workflow_map is not set") assert hasattr(self, "expected_workflow_blocks") and self.expected_workflow_blocks, ( "expected_workflow_blocks must be defined in the test class" ) for workflow_name, expected_blocks in self.expected_workflow_blocks.items(): workflow_blocks = blocks.get_workflow(workflow_name) actual_blocks = list(workflow_blocks.sub_blocks.items()) # Check that the number of blocks matches assert len(actual_blocks) == len(expected_blocks), ( f"Workflow '{workflow_name}' has {len(actual_blocks)} blocks, expected {len(expected_blocks)}" ) # Check that each block name and type matches for i, ((actual_name, actual_block), (expected_name, expected_class_name)) in enumerate( zip(actual_blocks, expected_blocks) ): assert actual_name == expected_name assert actual_block.__class__.__name__ == expected_class_name, ( f"Workflow '{workflow_name}': block '{actual_name}' has type " f"{actual_block.__class__.__name__}, expected {expected_class_name}" ) class ModularGuiderTesterMixin: def test_guider_cfg(self, expected_max_diff=1e-2): pipe = self.get_pipeline().to(torch_device) # forward pass with CFG not applied guider = ClassifierFreeGuidance(guidance_scale=1.0) pipe.update_components(guider=guider) inputs = self.get_dummy_inputs() out_no_cfg = pipe(**inputs, output=self.output_name) # forward pass with CFG applied guider = ClassifierFreeGuidance(guidance_scale=7.5) pipe.update_components(guider=guider) inputs = self.get_dummy_inputs() out_cfg = pipe(**inputs, output=self.output_name) assert out_cfg.shape == out_no_cfg.shape max_diff = torch.abs(out_cfg - out_no_cfg).max() assert max_diff > expected_max_diff, "Output with CFG must be different from normal inference" class TestModularModelCardContent: def create_mock_block(self, name="TestBlock", description="Test block description"): class MockBlock: def __init__(self, name, description): self.__class__.__name__ = name self.description = description self.sub_blocks = {} return MockBlock(name, description) def create_mock_blocks( self, class_name="TestBlocks", description="Test pipeline description", num_blocks=2, components=None, configs=None, inputs=None, outputs=None, trigger_inputs=None, model_name=None, ): class MockBlocks: def __init__(self): self.__class__.__name__ = class_name self.description = description self.sub_blocks = {} self.expected_components = components or [] self.expected_configs = configs or [] self.inputs = inputs or [] self.outputs = outputs or [] self.trigger_inputs = trigger_inputs self.model_name = model_name blocks = MockBlocks() # Add mock sub-blocks for i in range(num_blocks): block_name = f"block_{i}" blocks.sub_blocks[block_name] = self.create_mock_block(f"Block{i}", f"Description for block {i}") return blocks def test_basic_model_card_content_structure(self): """Test that all expected keys are present in the output.""" blocks = self.create_mock_blocks() content = generate_modular_model_card_content(blocks) expected_keys = [ "pipeline_name", "model_description", "blocks_description", "components_description", "configs_section", "io_specification_section", "trigger_inputs_section", "tags", ] for key in expected_keys: assert key in content, f"Expected key '{key}' not found in model card content" assert isinstance(content["tags"], list), "Tags should be a list" def test_pipeline_name_generation(self): """Test that pipeline name is correctly generated from blocks class name.""" blocks = self.create_mock_blocks(class_name="StableDiffusionBlocks") content = generate_modular_model_card_content(blocks) assert content["pipeline_name"] == "StableDiffusion Pipeline" def test_tags_generation_text_to_image(self): """Test that text-to-image tags are correctly generated.""" blocks = self.create_mock_blocks(trigger_inputs=None) content = generate_modular_model_card_content(blocks) assert "modular-diffusers" in content["tags"] assert "diffusers" in content["tags"] assert "text-to-image" in content["tags"] def test_tags_generation_with_trigger_inputs(self): """Test that tags are correctly generated based on trigger inputs.""" # Test inpainting blocks = self.create_mock_blocks(trigger_inputs=["mask", "prompt"]) content = generate_modular_model_card_content(blocks) assert "inpainting" in content["tags"] # Test image-to-image blocks = self.create_mock_blocks(trigger_inputs=["image", "prompt"]) content = generate_modular_model_card_content(blocks) assert "image-to-image" in content["tags"] # Test controlnet blocks = self.create_mock_blocks(trigger_inputs=["control_image", "prompt"]) content = generate_modular_model_card_content(blocks) assert "controlnet" in content["tags"] def test_tags_with_model_name(self): """Test that model name is included in tags when present.""" blocks = self.create_mock_blocks(model_name="stable-diffusion-xl") content = generate_modular_model_card_content(blocks) assert "stable-diffusion-xl" in content["tags"] def test_components_description_formatting(self): """Test that components are correctly formatted.""" components = [ ComponentSpec(name="vae", description="VAE component"), ComponentSpec(name="text_encoder", description="Text encoder component"), ] blocks = self.create_mock_blocks(components=components) content = generate_modular_model_card_content(blocks) assert "vae" in content["components_description"] assert "text_encoder" in content["components_description"] # Should be enumerated assert "1." in content["components_description"] def test_components_description_empty(self): """Test handling of pipelines without components.""" blocks = self.create_mock_blocks(components=None) content = generate_modular_model_card_content(blocks) assert "No specific components required" in content["components_description"] def test_configs_section_with_configs(self): """Test that configs section is generated when configs are present.""" configs = [ ConfigSpec(name="num_train_timesteps", default=1000, description="Number of training timesteps"), ] blocks = self.create_mock_blocks(configs=configs) content = generate_modular_model_card_content(blocks) assert "## Configuration Parameters" in content["configs_section"] def test_configs_section_empty(self): """Test that configs section is empty when no configs are present.""" blocks = self.create_mock_blocks(configs=None) content = generate_modular_model_card_content(blocks) assert content["configs_section"] == "" def test_inputs_description_required_and_optional(self): """Test that required and optional inputs are correctly formatted.""" inputs = [ InputParam(name="prompt", type_hint=str, required=True, description="The input prompt"), InputParam(name="num_steps", type_hint=int, required=False, default=50, description="Number of steps"), ] blocks = self.create_mock_blocks(inputs=inputs) content = generate_modular_model_card_content(blocks) io_section = content["io_specification_section"] assert "**Inputs:**" in io_section assert "prompt" in io_section assert "num_steps" in io_section assert "*optional*" in io_section assert "defaults to `50`" in io_section def test_inputs_description_empty(self): """Test handling of pipelines without specific inputs.""" blocks = self.create_mock_blocks(inputs=[]) content = generate_modular_model_card_content(blocks) assert "No specific inputs defined" in content["io_specification_section"] def test_outputs_description_formatting(self): """Test that outputs are correctly formatted.""" outputs = [ OutputParam(name="images", type_hint=torch.Tensor, description="Generated images"), ] blocks = self.create_mock_blocks(outputs=outputs) content = generate_modular_model_card_content(blocks) io_section = content["io_specification_section"] assert "images" in io_section assert "Generated images" in io_section def test_outputs_description_empty(self): """Test handling of pipelines without specific outputs.""" blocks = self.create_mock_blocks(outputs=[]) content = generate_modular_model_card_content(blocks) assert "Standard pipeline outputs" in content["io_specification_section"] def test_trigger_inputs_section_with_triggers(self): """Test that trigger inputs section is generated when present.""" blocks = self.create_mock_blocks(trigger_inputs=["mask", "image"]) content = generate_modular_model_card_content(blocks) assert "### Conditional Execution" in content["trigger_inputs_section"] assert "`mask`" in content["trigger_inputs_section"] assert "`image`" in content["trigger_inputs_section"] def test_trigger_inputs_section_empty(self): """Test that trigger inputs section is empty when not present.""" blocks = self.create_mock_blocks(trigger_inputs=None) content = generate_modular_model_card_content(blocks) assert content["trigger_inputs_section"] == "" def test_model_description_includes_block_count(self): """Test that model description includes the number of blocks.""" blocks = self.create_mock_blocks(num_blocks=5) content = generate_modular_model_card_content(blocks) assert "5-block architecture" in content["model_description"] class TestAutoModelLoadIdTagging: def test_automodel_tags_load_id(self): model = AutoModel.from_pretrained("hf-internal-testing/tiny-stable-diffusion-xl-pipe", subfolder="unet") assert hasattr(model, "_diffusers_load_id"), "Model should have _diffusers_load_id attribute" assert model._diffusers_load_id != "null", "_diffusers_load_id should not be 'null'" # Verify load_id contains the expected fields load_id = model._diffusers_load_id assert "hf-internal-testing/tiny-stable-diffusion-xl-pipe" in load_id assert "unet" in load_id def test_automodel_update_components(self): pipe = ModularPipeline.from_pretrained("hf-internal-testing/tiny-stable-diffusion-xl-pipe") pipe.load_components(torch_dtype=torch.float32) auto_model = AutoModel.from_pretrained("hf-internal-testing/tiny-stable-diffusion-xl-pipe", subfolder="unet") pipe.update_components(unet=auto_model) assert pipe.unet is auto_model assert "unet" in pipe._component_specs spec = pipe._component_specs["unet"] assert spec.pretrained_model_name_or_path == "hf-internal-testing/tiny-stable-diffusion-xl-pipe" assert spec.subfolder == "unet" class TestLoadComponentsSkipBehavior: def test_load_components_skips_already_loaded(self): pipe = ModularPipeline.from_pretrained("hf-internal-testing/tiny-stable-diffusion-xl-pipe") pipe.load_components(torch_dtype=torch.float32) original_unet = pipe.unet pipe.load_components() # Verify that the unet is the same object (not reloaded) assert pipe.unet is original_unet, "load_components should skip already loaded components" def test_load_components_selective_loading(self): pipe = ModularPipeline.from_pretrained("hf-internal-testing/tiny-stable-diffusion-xl-pipe") pipe.load_components(names="unet", torch_dtype=torch.float32) # Verify only requested component was loaded. assert hasattr(pipe, "unet") assert pipe.unet is not None assert getattr(pipe, "vae", None) is None def test_load_components_skips_invalid_pretrained_path(self): pipe = ModularPipeline.from_pretrained("hf-internal-testing/tiny-stable-diffusion-xl-pipe") pipe._component_specs["test_component"] = ComponentSpec( name="test_component", type_hint=torch.nn.Module, pretrained_model_name_or_path=None, default_creation_method="from_pretrained", ) pipe.load_components(torch_dtype=torch.float32) # Verify test_component was not loaded assert not hasattr(pipe, "test_component") or pipe.test_component is None class TestCustomModelSavePretrained: def test_save_pretrained_updates_index_for_local_model(self, tmp_path): """When a component without _diffusers_load_id (custom/local model) is saved, modular_model_index.json should point to the save directory.""" import json pipe = ModularPipeline.from_pretrained("hf-internal-testing/tiny-stable-diffusion-xl-pipe") pipe.load_components(torch_dtype=torch.float32) pipe.unet._diffusers_load_id = "null" save_dir = str(tmp_path / "my-pipeline") pipe.save_pretrained(save_dir) with open(os.path.join(save_dir, "modular_model_index.json")) as f: index = json.load(f) _library, _cls, unet_spec = index["unet"] assert unet_spec["pretrained_model_name_or_path"] == save_dir assert unet_spec["subfolder"] == "unet" _library, _cls, vae_spec = index["vae"] assert vae_spec["pretrained_model_name_or_path"] == "hf-internal-testing/tiny-stable-diffusion-xl-pipe" def test_save_pretrained_roundtrip_with_local_model(self, tmp_path): """A pipeline with a custom/local model should be saveable and re-loadable with identical outputs.""" pipe = ModularPipeline.from_pretrained("hf-internal-testing/tiny-stable-diffusion-xl-pipe") pipe.load_components(torch_dtype=torch.float32) pipe.unet._diffusers_load_id = "null" original_state_dict = pipe.unet.state_dict() save_dir = str(tmp_path / "my-pipeline") pipe.save_pretrained(save_dir) loaded_pipe = ModularPipeline.from_pretrained(save_dir) loaded_pipe.load_components(torch_dtype=torch.float32) assert loaded_pipe.unet is not None assert loaded_pipe.unet.__class__.__name__ == pipe.unet.__class__.__name__ loaded_state_dict = loaded_pipe.unet.state_dict() assert set(original_state_dict.keys()) == set(loaded_state_dict.keys()) for key in original_state_dict: assert torch.equal(original_state_dict[key], loaded_state_dict[key]), f"Mismatch in {key}" def test_save_pretrained_overwrite_modular_index(self, tmp_path): """With overwrite_modular_index=True, all component references should point to the save directory.""" import json pipe = ModularPipeline.from_pretrained("hf-internal-testing/tiny-stable-diffusion-xl-pipe") pipe.load_components(torch_dtype=torch.float32) save_dir = str(tmp_path / "my-pipeline") pipe.save_pretrained(save_dir, overwrite_modular_index=True) with open(os.path.join(save_dir, "modular_model_index.json")) as f: index = json.load(f) for component_name in ["unet", "vae", "text_encoder", "text_encoder_2"]: if component_name not in index: continue _library, _cls, spec = index[component_name] assert spec["pretrained_model_name_or_path"] == save_dir, ( f"{component_name} should point to save dir but got {spec['pretrained_model_name_or_path']}" ) assert spec["subfolder"] == component_name loaded_pipe = ModularPipeline.from_pretrained(save_dir) loaded_pipe.load_components(torch_dtype=torch.float32) assert loaded_pipe.unet is not None assert loaded_pipe.vae is not None class TestModularPipelineInitFallback: """Test that ModularPipeline.__init__ falls back to default_blocks_name when _blocks_class_name is a base class (e.g. SequentialPipelineBlocks saved by from_blocks_dict).""" def test_init_fallback_when_blocks_class_name_is_base_class(self, tmp_path): # 1. Load pipeline and get a workflow (returns a base SequentialPipelineBlocks) pipe = ModularPipeline.from_pretrained("hf-internal-testing/tiny-stable-diffusion-xl-pipe") t2i_blocks = pipe.blocks.get_workflow("text2image") assert t2i_blocks.__class__.__name__ == "SequentialPipelineBlocks" # 2. Use init_pipeline to create a new pipeline from the workflow blocks t2i_pipe = t2i_blocks.init_pipeline("hf-internal-testing/tiny-stable-diffusion-xl-pipe") # 3. Save and reload — the saved config will have _blocks_class_name="SequentialPipelineBlocks" save_dir = str(tmp_path / "pipeline") t2i_pipe.save_pretrained(save_dir) loaded_pipe = ModularPipeline.from_pretrained(save_dir) # 4. Verify it fell back to default_blocks_name and has correct blocks assert loaded_pipe.__class__.__name__ == pipe.__class__.__name__ assert loaded_pipe._blocks.__class__.__name__ == pipe._blocks.__class__.__name__ assert len(loaded_pipe._blocks.sub_blocks) == len(pipe._blocks.sub_blocks)
{ "repo_id": "huggingface/diffusers", "file_path": "tests/modular_pipelines/test_modular_pipelines_common.py", "license": "Apache License 2.0", "lines": 620, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
test
huggingface/diffusers:src/diffusers/guiders/frequency_decoupled_guidance.py
# Copyright 2025 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from __future__ import annotations import math from typing import TYPE_CHECKING import torch from ..configuration_utils import register_to_config from ..utils import is_kornia_available from .guider_utils import BaseGuidance, GuiderOutput, rescale_noise_cfg if TYPE_CHECKING: from ..modular_pipelines.modular_pipeline import BlockState _CAN_USE_KORNIA = is_kornia_available() if _CAN_USE_KORNIA: from kornia.geometry import pyrup as upsample_and_blur_func from kornia.geometry.transform import build_laplacian_pyramid as build_laplacian_pyramid_func else: upsample_and_blur_func = None build_laplacian_pyramid_func = None def project(v0: torch.Tensor, v1: torch.Tensor, upcast_to_double: bool = True) -> tuple[torch.Tensor, torch.Tensor]: """ Project vector v0 onto vector v1, returning the parallel and orthogonal components of v0. Implementation from paper (Algorithm 2). """ # v0 shape: [B, ...] # v1 shape: [B, ...] # Assume first dim is a batch dim and all other dims are channel or "spatial" dims all_dims_but_first = list(range(1, len(v0.shape))) if upcast_to_double: dtype = v0.dtype v0, v1 = v0.double(), v1.double() v1 = torch.nn.functional.normalize(v1, dim=all_dims_but_first) v0_parallel = (v0 * v1).sum(dim=all_dims_but_first, keepdim=True) * v1 v0_orthogonal = v0 - v0_parallel if upcast_to_double: v0_parallel = v0_parallel.to(dtype) v0_orthogonal = v0_orthogonal.to(dtype) return v0_parallel, v0_orthogonal def build_image_from_pyramid(pyramid: list[torch.Tensor]) -> torch.Tensor: """ Recovers the data space latents from the Laplacian pyramid frequency space. Implementation from the paper (Algorithm 2). """ # pyramid shapes: [[B, C, H, W], [B, C, H/2, W/2], ...] img = pyramid[-1] for i in range(len(pyramid) - 2, -1, -1): img = upsample_and_blur_func(img) + pyramid[i] return img class FrequencyDecoupledGuidance(BaseGuidance): """ Frequency-Decoupled Guidance (FDG): https://huggingface.co/papers/2506.19713 FDG is a technique similar to (and based on) classifier-free guidance (CFG) which is used to improve generation quality and condition-following in diffusion models. Like CFG, during training we jointly train the model on both conditional and unconditional data, and use a combination of the two during inference. (If you want more details on how CFG works, you can check out the CFG guider.) FDG differs from CFG in that the normal CFG prediction is instead decoupled into low- and high-frequency components using a frequency transform (such as a Laplacian pyramid). The CFG update is then performed in frequency space separately for the low- and high-frequency components with different guidance scales. Finally, the inverse frequency transform is used to map the CFG frequency predictions back to data space (e.g. pixel space for images) to form the final FDG prediction. For images, the FDG authors found that using low guidance scales for the low-frequency components retains sample diversity and realistic color composition, while using high guidance scales for high-frequency components enhances sample quality (such as better visual details). Therefore, they recommend using low guidance scales (low w_low) for the low-frequency components and high guidance scales (high w_high) for the high-frequency components. As an example, they suggest w_low = 5.0 and w_high = 10.0 for Stable Diffusion XL (see Table 8 in the paper). As with CFG, Diffusers implements the scaling and shifting on the unconditional prediction based on the [Imagen paper](https://huggingface.co/papers/2205.11487), which is equivalent to what the original CFG paper proposed in theory. [x_pred = x_uncond + scale * (x_cond - x_uncond)] The `use_original_formulation` argument can be set to `True` to use the original CFG formulation mentioned in the paper. By default, we use the diffusers-native implementation that has been in the codebase for a long time. Args: guidance_scales (`list[float]`, defaults to `[10.0, 5.0]`): The scale parameter for frequency-decoupled guidance for each frequency component, listed from highest frequency level to lowest. Higher values result in stronger conditioning on the text prompt, while lower values allow for more freedom in generation. Higher values may lead to saturation and deterioration of image quality. The FDG authors recommend using higher guidance scales for higher frequency components and lower guidance scales for lower frequency components (so `guidance_scales` should typically be sorted in descending order). guidance_rescale (`float` or `list[float]`, defaults to `0.0`): The rescale factor applied to the noise predictions. This is used to improve image quality and fix overexposure. Based on Section 3.4 from [Common Diffusion Noise Schedules and Sample Steps are Flawed](https://huggingface.co/papers/2305.08891). If a list is supplied, it should be the same length as `guidance_scales`. parallel_weights (`float` or `list[float]`, *optional*): Optional weights for the parallel component of each frequency component of the projected CFG shift. If not set, the weights will default to `1.0` for all components, which corresponds to using the normal CFG shift (that is, equal weights for the parallel and orthogonal components). If set, a value in `[0, 1]` is recommended. If a list is supplied, it should be the same length as `guidance_scales`. use_original_formulation (`bool`, defaults to `False`): Whether to use the original formulation of classifier-free guidance as proposed in the paper. By default, we use the diffusers-native implementation that has been in the codebase for a long time. See [~guiders.classifier_free_guidance.ClassifierFreeGuidance] for more details. start (`float` or `list[float]`, defaults to `0.0`): The fraction of the total number of denoising steps after which guidance starts. If a list is supplied, it should be the same length as `guidance_scales`. stop (`float` or `list[float]`, defaults to `1.0`): The fraction of the total number of denoising steps after which guidance stops. If a list is supplied, it should be the same length as `guidance_scales`. guidance_rescale_space (`str`, defaults to `"data"`): Whether to performance guidance rescaling in `"data"` space (after the full FDG update in data space) or in `"freq"` space (right after the CFG update, for each freq level). Note that frequency space rescaling is speculative and may not produce expected results. If `"data"` is set, the first `guidance_rescale` value will be used; otherwise, per-frequency-level guidance rescale values will be used if available. upcast_to_double (`bool`, defaults to `True`): Whether to upcast certain operations, such as the projection operation when using `parallel_weights`, to float64 when performing guidance. This may result in better performance at the cost of increased runtime. """ _input_predictions = ["pred_cond", "pred_uncond"] @register_to_config def __init__( self, guidance_scales: list[float] | tuple[float] = [10.0, 5.0], guidance_rescale: float | list[float] | tuple[float] = 0.0, parallel_weights: float | list[float] | tuple[float] | None = None, use_original_formulation: bool = False, start: float | list[float] | tuple[float] = 0.0, stop: float | list[float] | tuple[float] = 1.0, guidance_rescale_space: str = "data", upcast_to_double: bool = True, enabled: bool = True, ): if not _CAN_USE_KORNIA: raise ImportError( "The `FrequencyDecoupledGuidance` guider cannot be instantiated because the `kornia` library on which " "it depends is not available in the current environment. You can install `kornia` with `pip install " "kornia`." ) # Set start to earliest start for any freq component and stop to latest stop for any freq component min_start = start if isinstance(start, float) else min(start) max_stop = stop if isinstance(stop, float) else max(stop) super().__init__(min_start, max_stop, enabled) self.guidance_scales = guidance_scales self.levels = len(guidance_scales) if isinstance(guidance_rescale, float): self.guidance_rescale = [guidance_rescale] * self.levels elif len(guidance_rescale) == self.levels: self.guidance_rescale = guidance_rescale else: raise ValueError( f"`guidance_rescale` has length {len(guidance_rescale)} but should have the same length as " f"`guidance_scales` ({len(self.guidance_scales)})" ) # Whether to perform guidance rescaling in frequency space (right after the CFG update) or data space (after # transforming from frequency space back to data space) if guidance_rescale_space not in ["data", "freq"]: raise ValueError( f"Guidance rescale space is {guidance_rescale_space} but must be one of `data` or `freq`." ) self.guidance_rescale_space = guidance_rescale_space if parallel_weights is None: # Use normal CFG shift (equal weights for parallel and orthogonal components) self.parallel_weights = [1.0] * self.levels elif isinstance(parallel_weights, float): self.parallel_weights = [parallel_weights] * self.levels elif len(parallel_weights) == self.levels: self.parallel_weights = parallel_weights else: raise ValueError( f"`parallel_weights` has length {len(parallel_weights)} but should have the same length as " f"`guidance_scales` ({len(self.guidance_scales)})" ) self.use_original_formulation = use_original_formulation self.upcast_to_double = upcast_to_double if isinstance(start, float): self.guidance_start = [start] * self.levels elif len(start) == self.levels: self.guidance_start = start else: raise ValueError( f"`start` has length {len(start)} but should have the same length as `guidance_scales` " f"({len(self.guidance_scales)})" ) if isinstance(stop, float): self.guidance_stop = [stop] * self.levels elif len(stop) == self.levels: self.guidance_stop = stop else: raise ValueError( f"`stop` has length {len(stop)} but should have the same length as `guidance_scales` " f"({len(self.guidance_scales)})" ) def prepare_inputs(self, data: dict[str, tuple[torch.Tensor, torch.Tensor]]) -> list["BlockState"]: tuple_indices = [0] if self.num_conditions == 1 else [0, 1] data_batches = [] for tuple_idx, input_prediction in zip(tuple_indices, self._input_predictions): data_batch = self._prepare_batch(data, tuple_idx, input_prediction) data_batches.append(data_batch) return data_batches def prepare_inputs_from_block_state( self, data: "BlockState", input_fields: dict[str, str | tuple[str, str]] ) -> list["BlockState"]: tuple_indices = [0] if self.num_conditions == 1 else [0, 1] data_batches = [] for tuple_idx, input_prediction in zip(tuple_indices, self._input_predictions): data_batch = self._prepare_batch_from_block_state(input_fields, data, tuple_idx, input_prediction) data_batches.append(data_batch) return data_batches def forward(self, pred_cond: torch.Tensor, pred_uncond: torch.Tensor | None = None) -> GuiderOutput: pred = None if not self._is_fdg_enabled(): pred = pred_cond else: # Apply the frequency transform (e.g. Laplacian pyramid) to the conditional and unconditional predictions. pred_cond_pyramid = build_laplacian_pyramid_func(pred_cond, self.levels) pred_uncond_pyramid = build_laplacian_pyramid_func(pred_uncond, self.levels) # From high frequencies to low frequencies, following the paper implementation pred_guided_pyramid = [] parameters = zip(self.guidance_scales, self.parallel_weights, self.guidance_rescale) for level, (guidance_scale, parallel_weight, guidance_rescale) in enumerate(parameters): if self._is_fdg_enabled_for_level(level): # Get the cond/uncond preds (in freq space) at the current frequency level pred_cond_freq = pred_cond_pyramid[level] pred_uncond_freq = pred_uncond_pyramid[level] shift = pred_cond_freq - pred_uncond_freq # Apply parallel weights, if used (1.0 corresponds to using the normal CFG shift) if not math.isclose(parallel_weight, 1.0): shift_parallel, shift_orthogonal = project(shift, pred_cond_freq, self.upcast_to_double) shift = parallel_weight * shift_parallel + shift_orthogonal # Apply CFG update for the current frequency level pred = pred_cond_freq if self.use_original_formulation else pred_uncond_freq pred = pred + guidance_scale * shift if self.guidance_rescale_space == "freq" and guidance_rescale > 0.0: pred = rescale_noise_cfg(pred, pred_cond_freq, guidance_rescale) # Add the current FDG guided level to the FDG prediction pyramid pred_guided_pyramid.append(pred) else: # Add the current pred_cond_pyramid level as the "non-FDG" prediction pred_guided_pyramid.append(pred_cond_freq) # Convert from frequency space back to data (e.g. pixel) space by applying inverse freq transform pred = build_image_from_pyramid(pred_guided_pyramid) # If rescaling in data space, use the first elem of self.guidance_rescale as the "global" rescale value # across all freq levels if self.guidance_rescale_space == "data" and self.guidance_rescale[0] > 0.0: pred = rescale_noise_cfg(pred, pred_cond, self.guidance_rescale[0]) return GuiderOutput(pred=pred, pred_cond=pred_cond, pred_uncond=pred_uncond) @property def is_conditional(self) -> bool: return self._count_prepared == 1 @property def num_conditions(self) -> int: num_conditions = 1 if self._is_fdg_enabled(): num_conditions += 1 return num_conditions def _is_fdg_enabled(self) -> bool: if not self._enabled: return False is_within_range = True if self._num_inference_steps is not None: skip_start_step = int(self._start * self._num_inference_steps) skip_stop_step = int(self._stop * self._num_inference_steps) is_within_range = skip_start_step <= self._step < skip_stop_step is_close = False if self.use_original_formulation: is_close = all(math.isclose(guidance_scale, 0.0) for guidance_scale in self.guidance_scales) else: is_close = all(math.isclose(guidance_scale, 1.0) for guidance_scale in self.guidance_scales) return is_within_range and not is_close def _is_fdg_enabled_for_level(self, level: int) -> bool: if not self._enabled: return False is_within_range = True if self._num_inference_steps is not None: skip_start_step = int(self.guidance_start[level] * self._num_inference_steps) skip_stop_step = int(self.guidance_stop[level] * self._num_inference_steps) is_within_range = skip_start_step <= self._step < skip_stop_step is_close = False if self.use_original_formulation: is_close = math.isclose(self.guidance_scales[level], 0.0) else: is_close = math.isclose(self.guidance_scales[level], 1.0) return is_within_range and not is_close
{ "repo_id": "huggingface/diffusers", "file_path": "src/diffusers/guiders/frequency_decoupled_guidance.py", "license": "Apache License 2.0", "lines": 282, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
huggingface/diffusers:src/diffusers/hooks/utils.py
# Copyright 2025 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import torch from ._common import _ALL_TRANSFORMER_BLOCK_IDENTIFIERS, _ATTENTION_CLASSES, _FEEDFORWARD_CLASSES def _get_identifiable_transformer_blocks_in_module(module: torch.nn.Module): module_list_with_transformer_blocks = [] for name, submodule in module.named_modules(): name_endswith_identifier = any(name.endswith(identifier) for identifier in _ALL_TRANSFORMER_BLOCK_IDENTIFIERS) is_ModuleList = isinstance(submodule, torch.nn.ModuleList) if name_endswith_identifier and is_ModuleList: module_list_with_transformer_blocks.append((name, submodule)) return module_list_with_transformer_blocks def _get_identifiable_attention_layers_in_module(module: torch.nn.Module): attention_layers = [] for name, submodule in module.named_modules(): if isinstance(submodule, _ATTENTION_CLASSES): attention_layers.append((name, submodule)) return attention_layers def _get_identifiable_feedforward_layers_in_module(module: torch.nn.Module): feedforward_layers = [] for name, submodule in module.named_modules(): if isinstance(submodule, _FEEDFORWARD_CLASSES): feedforward_layers.append((name, submodule)) return feedforward_layers
{ "repo_id": "huggingface/diffusers", "file_path": "src/diffusers/hooks/utils.py", "license": "Apache License 2.0", "lines": 35, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
huggingface/diffusers:examples/dreambooth/test_dreambooth_lora_qwenimage.py
# coding=utf-8 # Copyright 2025 HuggingFace Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import json import logging import os import sys import tempfile import safetensors from diffusers.loaders.lora_base import LORA_ADAPTER_METADATA_KEY sys.path.append("..") from test_examples_utils import ExamplesTestsAccelerate, run_command # noqa: E402 logging.basicConfig(level=logging.DEBUG) logger = logging.getLogger() stream_handler = logging.StreamHandler(sys.stdout) logger.addHandler(stream_handler) class DreamBoothLoRAQwenImage(ExamplesTestsAccelerate): instance_data_dir = "docs/source/en/imgs" instance_prompt = "photo" pretrained_model_name_or_path = "hf-internal-testing/tiny-qwenimage-pipe" script_path = "examples/dreambooth/train_dreambooth_lora_qwen_image.py" transformer_layer_type = "transformer_blocks.0.attn.to_k" def test_dreambooth_lora_qwen(self): with tempfile.TemporaryDirectory() as tmpdir: test_args = f""" {self.script_path} --pretrained_model_name_or_path {self.pretrained_model_name_or_path} --instance_data_dir {self.instance_data_dir} --instance_prompt {self.instance_prompt} --resolution 64 --train_batch_size 1 --gradient_accumulation_steps 1 --max_train_steps 2 --learning_rate 5.0e-04 --scale_lr --lr_scheduler constant --lr_warmup_steps 0 --output_dir {tmpdir} """.split() run_command(self._launch_args + test_args) # save_pretrained smoke test self.assertTrue(os.path.isfile(os.path.join(tmpdir, "pytorch_lora_weights.safetensors"))) # make sure the state_dict has the correct naming in the parameters. lora_state_dict = safetensors.torch.load_file(os.path.join(tmpdir, "pytorch_lora_weights.safetensors")) is_lora = all("lora" in k for k in lora_state_dict.keys()) self.assertTrue(is_lora) # when not training the text encoder, all the parameters in the state dict should start # with `"transformer"` in their names. starts_with_transformer = all(key.startswith("transformer") for key in lora_state_dict.keys()) self.assertTrue(starts_with_transformer) def test_dreambooth_lora_latent_caching(self): with tempfile.TemporaryDirectory() as tmpdir: test_args = f""" {self.script_path} --pretrained_model_name_or_path {self.pretrained_model_name_or_path} --instance_data_dir {self.instance_data_dir} --instance_prompt {self.instance_prompt} --resolution 64 --train_batch_size 1 --gradient_accumulation_steps 1 --max_train_steps 2 --cache_latents --learning_rate 5.0e-04 --scale_lr --lr_scheduler constant --lr_warmup_steps 0 --output_dir {tmpdir} """.split() run_command(self._launch_args + test_args) # save_pretrained smoke test self.assertTrue(os.path.isfile(os.path.join(tmpdir, "pytorch_lora_weights.safetensors"))) # make sure the state_dict has the correct naming in the parameters. lora_state_dict = safetensors.torch.load_file(os.path.join(tmpdir, "pytorch_lora_weights.safetensors")) is_lora = all("lora" in k for k in lora_state_dict.keys()) self.assertTrue(is_lora) # when not training the text encoder, all the parameters in the state dict should start # with `"transformer"` in their names. starts_with_transformer = all(key.startswith("transformer") for key in lora_state_dict.keys()) self.assertTrue(starts_with_transformer) def test_dreambooth_lora_layers(self): with tempfile.TemporaryDirectory() as tmpdir: test_args = f""" {self.script_path} --pretrained_model_name_or_path {self.pretrained_model_name_or_path} --instance_data_dir {self.instance_data_dir} --instance_prompt {self.instance_prompt} --resolution 64 --train_batch_size 1 --gradient_accumulation_steps 1 --max_train_steps 2 --cache_latents --learning_rate 5.0e-04 --scale_lr --lora_layers {self.transformer_layer_type} --lr_scheduler constant --lr_warmup_steps 0 --output_dir {tmpdir} """.split() run_command(self._launch_args + test_args) # save_pretrained smoke test self.assertTrue(os.path.isfile(os.path.join(tmpdir, "pytorch_lora_weights.safetensors"))) # make sure the state_dict has the correct naming in the parameters. lora_state_dict = safetensors.torch.load_file(os.path.join(tmpdir, "pytorch_lora_weights.safetensors")) is_lora = all("lora" in k for k in lora_state_dict.keys()) self.assertTrue(is_lora) # when not training the text encoder, all the parameters in the state dict should start # with `"transformer"` in their names. In this test, we only params of # transformer.transformer_blocks.0.attn.to_k should be in the state dict starts_with_transformer = all( key.startswith(f"transformer.{self.transformer_layer_type}") for key in lora_state_dict.keys() ) self.assertTrue(starts_with_transformer) def test_dreambooth_lora_qwen_checkpointing_checkpoints_total_limit(self): with tempfile.TemporaryDirectory() as tmpdir: test_args = f""" {self.script_path} --pretrained_model_name_or_path={self.pretrained_model_name_or_path} --instance_data_dir={self.instance_data_dir} --output_dir={tmpdir} --instance_prompt={self.instance_prompt} --resolution=64 --train_batch_size=1 --gradient_accumulation_steps=1 --max_train_steps=6 --checkpoints_total_limit=2 --checkpointing_steps=2 """.split() run_command(self._launch_args + test_args) self.assertEqual( {x for x in os.listdir(tmpdir) if "checkpoint" in x}, {"checkpoint-4", "checkpoint-6"}, ) def test_dreambooth_lora_qwen_checkpointing_checkpoints_total_limit_removes_multiple_checkpoints(self): with tempfile.TemporaryDirectory() as tmpdir: test_args = f""" {self.script_path} --pretrained_model_name_or_path={self.pretrained_model_name_or_path} --instance_data_dir={self.instance_data_dir} --output_dir={tmpdir} --instance_prompt={self.instance_prompt} --resolution=64 --train_batch_size=1 --gradient_accumulation_steps=1 --max_train_steps=4 --checkpointing_steps=2 """.split() run_command(self._launch_args + test_args) self.assertEqual({x for x in os.listdir(tmpdir) if "checkpoint" in x}, {"checkpoint-2", "checkpoint-4"}) resume_run_args = f""" {self.script_path} --pretrained_model_name_or_path={self.pretrained_model_name_or_path} --instance_data_dir={self.instance_data_dir} --output_dir={tmpdir} --instance_prompt={self.instance_prompt} --resolution=64 --train_batch_size=1 --gradient_accumulation_steps=1 --max_train_steps=8 --checkpointing_steps=2 --resume_from_checkpoint=checkpoint-4 --checkpoints_total_limit=2 """.split() run_command(self._launch_args + resume_run_args) self.assertEqual({x for x in os.listdir(tmpdir) if "checkpoint" in x}, {"checkpoint-6", "checkpoint-8"}) def test_dreambooth_lora_with_metadata(self): # Use a `lora_alpha` that is different from `rank`. lora_alpha = 8 rank = 4 with tempfile.TemporaryDirectory() as tmpdir: test_args = f""" {self.script_path} --pretrained_model_name_or_path {self.pretrained_model_name_or_path} --instance_data_dir {self.instance_data_dir} --instance_prompt {self.instance_prompt} --resolution 64 --train_batch_size 1 --gradient_accumulation_steps 1 --max_train_steps 2 --lora_alpha={lora_alpha} --rank={rank} --learning_rate 5.0e-04 --scale_lr --lr_scheduler constant --lr_warmup_steps 0 --output_dir {tmpdir} """.split() run_command(self._launch_args + test_args) # save_pretrained smoke test state_dict_file = os.path.join(tmpdir, "pytorch_lora_weights.safetensors") self.assertTrue(os.path.isfile(state_dict_file)) # Check if the metadata was properly serialized. with safetensors.torch.safe_open(state_dict_file, framework="pt", device="cpu") as f: metadata = f.metadata() or {} metadata.pop("format", None) raw = metadata.get(LORA_ADAPTER_METADATA_KEY) if raw: raw = json.loads(raw) loaded_lora_alpha = raw["transformer.lora_alpha"] self.assertTrue(loaded_lora_alpha == lora_alpha) loaded_lora_rank = raw["transformer.r"] self.assertTrue(loaded_lora_rank == rank)
{ "repo_id": "huggingface/diffusers", "file_path": "examples/dreambooth/test_dreambooth_lora_qwenimage.py", "license": "Apache License 2.0", "lines": 212, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
test
huggingface/diffusers:examples/dreambooth/train_dreambooth_lora_qwen_image.py
#!/usr/bin/env python # coding=utf-8 # Copyright 2025 The HuggingFace Inc. team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # /// script # dependencies = [ # "diffusers @ git+https://github.com/huggingface/diffusers.git", # "torch>=2.0.0", # "accelerate>=0.31.0", # "transformers>=4.41.2", # "ftfy", # "tensorboard", # "Jinja2", # "peft>=0.11.1", # "sentencepiece", # "torchvision", # "datasets", # "bitsandbytes", # "prodigyopt", # ] # /// import argparse import copy import itertools import json import logging import math import os import random import shutil import warnings from contextlib import nullcontext from pathlib import Path import numpy as np import torch import transformers from accelerate import Accelerator, DistributedType from accelerate.logging import get_logger from accelerate.utils import DistributedDataParallelKwargs, ProjectConfiguration, set_seed from huggingface_hub import create_repo, upload_folder from huggingface_hub.utils import insecure_hashlib from peft import LoraConfig, prepare_model_for_kbit_training, set_peft_model_state_dict from peft.utils import get_peft_model_state_dict from PIL import Image from PIL.ImageOps import exif_transpose from torch.utils.data import Dataset from torchvision import transforms from torchvision.transforms.functional import crop from tqdm.auto import tqdm from transformers import Qwen2_5_VLForConditionalGeneration, Qwen2Tokenizer import diffusers from diffusers import ( AutoencoderKLQwenImage, BitsAndBytesConfig, FlowMatchEulerDiscreteScheduler, QwenImagePipeline, QwenImageTransformer2DModel, ) from diffusers.optimization import get_scheduler from diffusers.training_utils import ( _collate_lora_metadata, cast_training_params, compute_density_for_timestep_sampling, compute_loss_weighting_for_sd3, free_memory, offload_models, ) from diffusers.utils import ( check_min_version, convert_unet_state_dict_to_peft, is_wandb_available, ) from diffusers.utils.hub_utils import load_or_create_model_card, populate_model_card from diffusers.utils.import_utils import is_torch_npu_available from diffusers.utils.torch_utils import is_compiled_module if is_wandb_available(): import wandb # Will error if the minimal version of diffusers is not installed. Remove at your own risks. check_min_version("0.37.0.dev0") logger = get_logger(__name__) if is_torch_npu_available(): torch.npu.config.allow_internal_format = False def save_model_card( repo_id: str, images=None, base_model: str = None, instance_prompt=None, validation_prompt=None, repo_folder=None, ): widget_dict = [] if images is not None: for i, image in enumerate(images): image.save(os.path.join(repo_folder, f"image_{i}.png")) widget_dict.append( {"text": validation_prompt if validation_prompt else " ", "output": {"url": f"image_{i}.png"}} ) model_description = f""" # HiDream Image DreamBooth LoRA - {repo_id} <Gallery /> ## Model description These are {repo_id} DreamBooth LoRA weights for {base_model}. The weights were trained using [DreamBooth](https://dreambooth.github.io/) with the [Qwen Image diffusers trainer](https://github.com/huggingface/diffusers/blob/main/examples/dreambooth/README_qwen.md). ## Trigger words You should use `{instance_prompt}` to trigger the image generation. ## Download model [Download the *.safetensors LoRA]({repo_id}/tree/main) in the Files & versions tab. ## Use it with the [🧨 diffusers library](https://github.com/huggingface/diffusers) ```py >>> import torch >>> from diffusers import QwenImagePipeline >>> pipe = QwenImagePipeline.from_pretrained( ... "Qwen/Qwen-Image", ... torch_dtype=torch.bfloat16, ... ) >>> pipe.enable_model_cpu_offload() >>> pipe.load_lora_weights(f"{repo_id}") >>> image = pipe(f"{instance_prompt}").images[0] ``` For more details, including weighting, merging and fusing LoRAs, check the [documentation on loading LoRAs in diffusers](https://huggingface.co/docs/diffusers/main/en/using-diffusers/loading_adapters) """ model_card = load_or_create_model_card( repo_id_or_path=repo_id, from_training=True, license="apache-2.0", base_model=base_model, prompt=instance_prompt, model_description=model_description, widget=widget_dict, ) tags = [ "text-to-image", "diffusers-training", "diffusers", "lora", "qwen-image", "qwen-image-diffusers", "template:sd-lora", ] model_card = populate_model_card(model_card, tags=tags) model_card.save(os.path.join(repo_folder, "README.md")) def log_validation( pipeline, args, accelerator, pipeline_args, epoch, torch_dtype, is_final_validation=False, ): args.num_validation_images = args.num_validation_images if args.num_validation_images else 1 logger.info( f"Running validation... \n Generating {args.num_validation_images} images with prompt:" f" {args.validation_prompt}." ) pipeline = pipeline.to(accelerator.device, dtype=torch_dtype) pipeline.set_progress_bar_config(disable=True) # run inference generator = torch.Generator(device=accelerator.device).manual_seed(args.seed) if args.seed is not None else None autocast_ctx = torch.autocast(accelerator.device.type) if not is_final_validation else nullcontext() images = [] for _ in range(args.num_validation_images): with autocast_ctx: image = pipeline( prompt_embeds=pipeline_args["prompt_embeds"], prompt_embeds_mask=pipeline_args["prompt_embeds_mask"], generator=generator, ).images[0] images.append(image) for tracker in accelerator.trackers: phase_name = "test" if is_final_validation else "validation" if tracker.name == "tensorboard": np_images = np.stack([np.asarray(img) for img in images]) tracker.writer.add_images(phase_name, np_images, epoch, dataformats="NHWC") if tracker.name == "wandb": tracker.log( { phase_name: [ wandb.Image(image, caption=f"{i}: {args.validation_prompt}") for i, image in enumerate(images) ] } ) del pipeline free_memory() return images def parse_args(input_args=None): parser = argparse.ArgumentParser(description="Simple example of a training script.") parser.add_argument( "--pretrained_model_name_or_path", type=str, default=None, required=True, help="Path to pretrained model or model identifier from huggingface.co/models.", ) parser.add_argument( "--pretrained_tokenizer_4_name_or_path", type=str, default="meta-llama/Meta-Llama-3.1-8B-Instruct", help="Path to pretrained model or model identifier from huggingface.co/models.", ) parser.add_argument( "--pretrained_text_encoder_4_name_or_path", type=str, default="meta-llama/Meta-Llama-3.1-8B-Instruct", help="Path to pretrained model or model identifier from huggingface.co/models.", ) parser.add_argument( "--bnb_quantization_config_path", type=str, default=None, help="Quantization config in a JSON file that will be used to define the bitsandbytes quant config of the DiT.", ) parser.add_argument( "--revision", type=str, default=None, required=False, help="Revision of pretrained model identifier from huggingface.co/models.", ) parser.add_argument( "--variant", type=str, default=None, help="Variant of the model files of the pretrained model identifier from huggingface.co/models, 'e.g.' fp16", ) parser.add_argument( "--dataset_name", type=str, default=None, help=( "The name of the Dataset (from the HuggingFace hub) containing the training data of instance images (could be your own, possibly private," " dataset). It can also be a path pointing to a local copy of a dataset in your filesystem," " or to a folder containing files that 🤗 Datasets can understand." ), ) parser.add_argument( "--dataset_config_name", type=str, default=None, help="The config of the Dataset, leave as None if there's only one config.", ) parser.add_argument( "--instance_data_dir", type=str, default=None, help=("A folder containing the training data. "), ) parser.add_argument( "--cache_dir", type=str, default=None, help="The directory where the downloaded models and datasets will be stored.", ) parser.add_argument( "--image_column", type=str, default="image", help="The column of the dataset containing the target image. By " "default, the standard Image Dataset maps out 'file_name' " "to 'image'.", ) parser.add_argument( "--caption_column", type=str, default=None, help="The column of the dataset containing the instance prompt for each image", ) parser.add_argument("--repeats", type=int, default=1, help="How many times to repeat the training data.") parser.add_argument( "--class_data_dir", type=str, default=None, required=False, help="A folder containing the training data of class images.", ) parser.add_argument( "--instance_prompt", type=str, default=None, required=True, help="The prompt with identifier specifying the instance, e.g. 'photo of a TOK dog', 'in the style of TOK'", ) parser.add_argument( "--class_prompt", type=str, default=None, help="The prompt to specify images in the same class as provided instance images.", ) parser.add_argument( "--max_sequence_length", type=int, default=512, help="Maximum sequence length to use with the Qwen2.5 VL as text encoder.", ) parser.add_argument( "--validation_prompt", type=str, default=None, help="A prompt that is used during validation to verify that the model is learning.", ) parser.add_argument( "--skip_final_inference", default=False, action="store_true", help="Whether to skip the final inference step with loaded lora weights upon training completion. This will run intermediate validation inference if `validation_prompt` is provided. Specify to reduce memory.", ) parser.add_argument( "--final_validation_prompt", type=str, default=None, help="A prompt that is used during a final validation to verify that the model is learning. Ignored if `--validation_prompt` is provided.", ) parser.add_argument( "--num_validation_images", type=int, default=4, help="Number of images that should be generated during validation with `validation_prompt`.", ) parser.add_argument( "--validation_epochs", type=int, default=50, help=( "Run dreambooth validation every X epochs. Dreambooth validation consists of running the prompt" " `args.validation_prompt` multiple times: `args.num_validation_images`." ), ) parser.add_argument( "--rank", type=int, default=4, help=("The dimension of the LoRA update matrices."), ) parser.add_argument( "--lora_alpha", type=int, default=4, help="LoRA alpha to be used for additional scaling.", ) parser.add_argument("--lora_dropout", type=float, default=0.0, help="Dropout probability for LoRA layers") parser.add_argument( "--with_prior_preservation", default=False, action="store_true", help="Flag to add prior preservation loss.", ) parser.add_argument("--prior_loss_weight", type=float, default=1.0, help="The weight of prior preservation loss.") parser.add_argument( "--num_class_images", type=int, default=100, help=( "Minimal class images for prior preservation loss. If there are not enough images already present in" " class_data_dir, additional images will be sampled with class_prompt." ), ) parser.add_argument( "--output_dir", type=str, default="hidream-dreambooth-lora", help="The output directory where the model predictions and checkpoints will be written.", ) parser.add_argument("--seed", type=int, default=None, help="A seed for reproducible training.") parser.add_argument( "--resolution", type=int, default=512, help=( "The resolution for input images, all the images in the train/validation dataset will be resized to this" " resolution" ), ) parser.add_argument( "--center_crop", default=False, action="store_true", help=( "Whether to center crop the input images to the resolution. If not set, the images will be randomly" " cropped. The images will be resized to the resolution first before cropping." ), ) parser.add_argument( "--random_flip", action="store_true", help="whether to randomly flip images horizontally", ) parser.add_argument( "--train_batch_size", type=int, default=4, help="Batch size (per device) for the training dataloader." ) parser.add_argument( "--sample_batch_size", type=int, default=4, help="Batch size (per device) for sampling images." ) parser.add_argument("--num_train_epochs", type=int, default=1) parser.add_argument( "--max_train_steps", type=int, default=None, help="Total number of training steps to perform. If provided, overrides num_train_epochs.", ) parser.add_argument( "--checkpointing_steps", type=int, default=500, help=( "Save a checkpoint of the training state every X updates. These checkpoints can be used both as final" " checkpoints in case they are better than the last checkpoint, and are also suitable for resuming" " training using `--resume_from_checkpoint`." ), ) parser.add_argument( "--checkpoints_total_limit", type=int, default=None, help=("Max number of checkpoints to store."), ) parser.add_argument( "--resume_from_checkpoint", type=str, default=None, help=( "Whether training should be resumed from a previous checkpoint. Use a path saved by" ' `--checkpointing_steps`, or `"latest"` to automatically select the last available checkpoint.' ), ) parser.add_argument( "--gradient_accumulation_steps", type=int, default=1, help="Number of updates steps to accumulate before performing a backward/update pass.", ) parser.add_argument( "--gradient_checkpointing", action="store_true", help="Whether or not to use gradient checkpointing to save memory at the expense of slower backward pass.", ) parser.add_argument( "--learning_rate", type=float, default=1e-4, help="Initial learning rate (after the potential warmup period) to use.", ) parser.add_argument( "--scale_lr", action="store_true", default=False, help="Scale the learning rate by the number of GPUs, gradient accumulation steps, and batch size.", ) parser.add_argument( "--lr_scheduler", type=str, default="constant", help=( 'The scheduler type to use. Choose between ["linear", "cosine", "cosine_with_restarts", "polynomial",' ' "constant", "constant_with_warmup"]' ), ) parser.add_argument( "--lr_warmup_steps", type=int, default=500, help="Number of steps for the warmup in the lr scheduler." ) parser.add_argument( "--lr_num_cycles", type=int, default=1, help="Number of hard resets of the lr in cosine_with_restarts scheduler.", ) parser.add_argument("--lr_power", type=float, default=1.0, help="Power factor of the polynomial scheduler.") parser.add_argument( "--dataloader_num_workers", type=int, default=0, help=( "Number of subprocesses to use for data loading. 0 means that the data will be loaded in the main process." ), ) parser.add_argument( "--weighting_scheme", type=str, default="none", choices=["sigma_sqrt", "logit_normal", "mode", "cosmap", "none"], help=('We default to the "none" weighting scheme for uniform sampling and uniform loss'), ) parser.add_argument( "--logit_mean", type=float, default=0.0, help="mean to use when using the `'logit_normal'` weighting scheme." ) parser.add_argument( "--logit_std", type=float, default=1.0, help="std to use when using the `'logit_normal'` weighting scheme." ) parser.add_argument( "--mode_scale", type=float, default=1.29, help="Scale of mode weighting scheme. Only effective when using the `'mode'` as the `weighting_scheme`.", ) parser.add_argument( "--optimizer", type=str, default="AdamW", help=('The optimizer type to use. Choose between ["AdamW", "prodigy"]'), ) parser.add_argument( "--use_8bit_adam", action="store_true", help="Whether or not to use 8-bit Adam from bitsandbytes. Ignored if optimizer is not set to AdamW", ) parser.add_argument( "--adam_beta1", type=float, default=0.9, help="The beta1 parameter for the Adam and Prodigy optimizers." ) parser.add_argument( "--adam_beta2", type=float, default=0.999, help="The beta2 parameter for the Adam and Prodigy optimizers." ) parser.add_argument( "--prodigy_beta3", type=float, default=None, help="coefficients for computing the Prodigy stepsize using running averages. If set to None, " "uses the value of square root of beta2. Ignored if optimizer is adamW", ) parser.add_argument("--prodigy_decouple", type=bool, default=True, help="Use AdamW style decoupled weight decay") parser.add_argument("--adam_weight_decay", type=float, default=1e-04, help="Weight decay to use for unet params") parser.add_argument( "--lora_layers", type=str, default=None, help=( 'The transformer modules to apply LoRA training on. Please specify the layers in a comma separated. E.g. - "to_k,to_q,to_v" will result in lora training of attention layers only' ), ) parser.add_argument( "--adam_epsilon", type=float, default=1e-08, help="Epsilon value for the Adam optimizer and Prodigy optimizers.", ) parser.add_argument( "--prodigy_use_bias_correction", type=bool, default=True, help="Turn on Adam's bias correction. True by default. Ignored if optimizer is adamW", ) parser.add_argument( "--prodigy_safeguard_warmup", type=bool, default=True, help="Remove lr from the denominator of D estimate to avoid issues during warm-up stage. True by default. " "Ignored if optimizer is adamW", ) parser.add_argument("--max_grad_norm", default=1.0, type=float, help="Max gradient norm.") parser.add_argument("--push_to_hub", action="store_true", help="Whether or not to push the model to the Hub.") parser.add_argument("--hub_token", type=str, default=None, help="The token to use to push to the Model Hub.") parser.add_argument( "--hub_model_id", type=str, default=None, help="The name of the repository to keep in sync with the local `output_dir`.", ) parser.add_argument( "--logging_dir", type=str, default="logs", help=( "[TensorBoard](https://www.tensorflow.org/tensorboard) log directory. Will default to" " *output_dir/runs/**CURRENT_DATETIME_HOSTNAME***." ), ) parser.add_argument( "--allow_tf32", action="store_true", help=( "Whether or not to allow TF32 on Ampere GPUs. Can be used to speed up training. For more information, see" " https://pytorch.org/docs/stable/notes/cuda.html#tensorfloat-32-tf32-on-ampere-devices" ), ) parser.add_argument( "--cache_latents", action="store_true", default=False, help="Cache the VAE latents", ) parser.add_argument( "--report_to", type=str, default="tensorboard", help=( 'The integration to report the results and logs to. Supported platforms are `"tensorboard"`' ' (default), `"wandb"` and `"comet_ml"`. Use `"all"` to report to all integrations.' ), ) parser.add_argument( "--mixed_precision", type=str, default=None, choices=["no", "fp16", "bf16"], help=( "Whether to use mixed precision. Choose between fp16 and bf16 (bfloat16). Bf16 requires PyTorch >=" " 1.10.and an Nvidia Ampere GPU. Default to the value of accelerate config of the current system or the" " flag passed with the `accelerate.launch` command. Use this argument to override the accelerate config." ), ) parser.add_argument( "--upcast_before_saving", action="store_true", default=False, help=( "Whether to upcast the trained transformer layers to float32 before saving (at the end of training). " "Defaults to precision dtype used for training to save memory" ), ) parser.add_argument( "--offload", action="store_true", help="Whether to offload the VAE and the text encoder to CPU when they are not used.", ) parser.add_argument("--local_rank", type=int, default=-1, help="For distributed training: local_rank") if input_args is not None: args = parser.parse_args(input_args) else: args = parser.parse_args() if args.dataset_name is None and args.instance_data_dir is None: raise ValueError("Specify either `--dataset_name` or `--instance_data_dir`") if args.dataset_name is not None and args.instance_data_dir is not None: raise ValueError("Specify only one of `--dataset_name` or `--instance_data_dir`") env_local_rank = int(os.environ.get("LOCAL_RANK", -1)) if env_local_rank != -1 and env_local_rank != args.local_rank: args.local_rank = env_local_rank if args.with_prior_preservation: if args.class_data_dir is None: raise ValueError("You must specify a data directory for class images.") if args.class_prompt is None: raise ValueError("You must specify prompt for class images.") else: # logger is not available yet if args.class_data_dir is not None: warnings.warn("You need not use --class_data_dir without --with_prior_preservation.") if args.class_prompt is not None: warnings.warn("You need not use --class_prompt without --with_prior_preservation.") return args class DreamBoothDataset(Dataset): """ A dataset to prepare the instance and class images with the prompts for fine-tuning the model. It pre-processes the images. """ def __init__( self, instance_data_root, instance_prompt, class_prompt, class_data_root=None, class_num=None, size=1024, repeats=1, center_crop=False, ): self.size = size self.center_crop = center_crop self.instance_prompt = instance_prompt self.custom_instance_prompts = None self.class_prompt = class_prompt # if --dataset_name is provided or a metadata jsonl file is provided in the local --instance_data directory, # we load the training data using load_dataset if args.dataset_name is not None: try: from datasets import load_dataset except ImportError: raise ImportError( "You are trying to load your data using the datasets library. If you wish to train using custom " "captions please install the datasets library: `pip install datasets`. If you wish to load a " "local folder containing images only, specify --instance_data_dir instead." ) # Downloading and loading a dataset from the hub. # See more about loading custom images at # https://huggingface.co/docs/datasets/v2.0.0/en/dataset_script dataset = load_dataset( args.dataset_name, args.dataset_config_name, cache_dir=args.cache_dir, ) # Preprocessing the datasets. column_names = dataset["train"].column_names # 6. Get the column names for input/target. if args.image_column is None: image_column = column_names[0] logger.info(f"image column defaulting to {image_column}") else: image_column = args.image_column if image_column not in column_names: raise ValueError( f"`--image_column` value '{args.image_column}' not found in dataset columns. Dataset columns are: {', '.join(column_names)}" ) instance_images = dataset["train"][image_column] if args.caption_column is None: logger.info( "No caption column provided, defaulting to instance_prompt for all images. If your dataset " "contains captions/prompts for the images, make sure to specify the " "column as --caption_column" ) self.custom_instance_prompts = None else: if args.caption_column not in column_names: raise ValueError( f"`--caption_column` value '{args.caption_column}' not found in dataset columns. Dataset columns are: {', '.join(column_names)}" ) custom_instance_prompts = dataset["train"][args.caption_column] # create final list of captions according to --repeats self.custom_instance_prompts = [] for caption in custom_instance_prompts: self.custom_instance_prompts.extend(itertools.repeat(caption, repeats)) else: self.instance_data_root = Path(instance_data_root) if not self.instance_data_root.exists(): raise ValueError("Instance images root doesn't exists.") instance_images = [Image.open(path) for path in list(Path(instance_data_root).iterdir())] self.custom_instance_prompts = None self.instance_images = [] for img in instance_images: self.instance_images.extend(itertools.repeat(img, repeats)) self.pixel_values = [] train_resize = transforms.Resize(size, interpolation=transforms.InterpolationMode.BILINEAR) train_crop = transforms.CenterCrop(size) if center_crop else transforms.RandomCrop(size) train_flip = transforms.RandomHorizontalFlip(p=1.0) train_transforms = transforms.Compose( [ transforms.ToTensor(), transforms.Normalize([0.5], [0.5]), ] ) for image in self.instance_images: image = exif_transpose(image) if not image.mode == "RGB": image = image.convert("RGB") image = train_resize(image) if args.random_flip and random.random() < 0.5: # flip image = train_flip(image) if args.center_crop: y1 = max(0, int(round((image.height - args.resolution) / 2.0))) x1 = max(0, int(round((image.width - args.resolution) / 2.0))) image = train_crop(image) else: y1, x1, h, w = train_crop.get_params(image, (args.resolution, args.resolution)) image = crop(image, y1, x1, h, w) image = train_transforms(image) self.pixel_values.append(image) self.num_instance_images = len(self.instance_images) self._length = self.num_instance_images if class_data_root is not None: self.class_data_root = Path(class_data_root) self.class_data_root.mkdir(parents=True, exist_ok=True) self.class_images_path = list(self.class_data_root.iterdir()) if class_num is not None: self.num_class_images = min(len(self.class_images_path), class_num) else: self.num_class_images = len(self.class_images_path) self._length = max(self.num_class_images, self.num_instance_images) else: self.class_data_root = None self.image_transforms = transforms.Compose( [ transforms.Resize(size, interpolation=transforms.InterpolationMode.BILINEAR), transforms.CenterCrop(size) if center_crop else transforms.RandomCrop(size), transforms.ToTensor(), transforms.Normalize([0.5], [0.5]), ] ) def __len__(self): return self._length def __getitem__(self, index): example = {} instance_image = self.pixel_values[index % self.num_instance_images] example["instance_images"] = instance_image if self.custom_instance_prompts: caption = self.custom_instance_prompts[index % self.num_instance_images] if caption: example["instance_prompt"] = caption else: example["instance_prompt"] = self.instance_prompt else: # custom prompts were provided, but length does not match size of image dataset example["instance_prompt"] = self.instance_prompt if self.class_data_root: class_image = Image.open(self.class_images_path[index % self.num_class_images]) class_image = exif_transpose(class_image) if not class_image.mode == "RGB": class_image = class_image.convert("RGB") example["class_images"] = self.image_transforms(class_image) example["class_prompt"] = self.class_prompt return example def collate_fn(examples, with_prior_preservation=False): pixel_values = [example["instance_images"] for example in examples] prompts = [example["instance_prompt"] for example in examples] # Concat class and instance examples for prior preservation. # We do this to avoid doing two forward passes. if with_prior_preservation: pixel_values += [example["class_images"] for example in examples] prompts += [example["class_prompt"] for example in examples] pixel_values = torch.stack(pixel_values) # Qwen expects a `num_frames` dimension too. if pixel_values.ndim == 4: pixel_values = pixel_values.unsqueeze(2) pixel_values = pixel_values.to(memory_format=torch.contiguous_format).float() batch = {"pixel_values": pixel_values, "prompts": prompts} return batch class PromptDataset(Dataset): "A simple dataset to prepare the prompts to generate class images on multiple GPUs." def __init__(self, prompt, num_samples): self.prompt = prompt self.num_samples = num_samples def __len__(self): return self.num_samples def __getitem__(self, index): example = {} example["prompt"] = self.prompt example["index"] = index return example def main(args): if args.report_to == "wandb" and args.hub_token is not None: raise ValueError( "You cannot use both --report_to=wandb and --hub_token due to a security risk of exposing your token." " Please use `hf auth login` to authenticate with the Hub." ) if torch.backends.mps.is_available() and args.mixed_precision == "bf16": # due to pytorch#99272, MPS does not yet support bfloat16. raise ValueError( "Mixed precision training with bfloat16 is not supported on MPS. Please use fp16 (recommended) or fp32 instead." ) logging_dir = Path(args.output_dir, args.logging_dir) accelerator_project_config = ProjectConfiguration(project_dir=args.output_dir, logging_dir=logging_dir) kwargs = DistributedDataParallelKwargs(find_unused_parameters=True) accelerator = Accelerator( gradient_accumulation_steps=args.gradient_accumulation_steps, mixed_precision=args.mixed_precision, log_with=args.report_to, project_config=accelerator_project_config, kwargs_handlers=[kwargs], ) # Disable AMP for MPS. if torch.backends.mps.is_available(): accelerator.native_amp = False if args.report_to == "wandb": if not is_wandb_available(): raise ImportError("Make sure to install wandb if you want to use it for logging during training.") # Make one log on every process with the configuration for debugging. logging.basicConfig( format="%(asctime)s - %(levelname)s - %(name)s - %(message)s", datefmt="%m/%d/%Y %H:%M:%S", level=logging.INFO, ) logger.info(accelerator.state, main_process_only=False) if accelerator.is_local_main_process: transformers.utils.logging.set_verbosity_warning() diffusers.utils.logging.set_verbosity_info() else: transformers.utils.logging.set_verbosity_error() diffusers.utils.logging.set_verbosity_error() # If passed along, set the training seed now. if args.seed is not None: set_seed(args.seed) # Generate class images if prior preservation is enabled. if args.with_prior_preservation: class_images_dir = Path(args.class_data_dir) if not class_images_dir.exists(): class_images_dir.mkdir(parents=True) cur_class_images = len(list(class_images_dir.iterdir())) if cur_class_images < args.num_class_images: pipeline = QwenImagePipeline.from_pretrained( args.pretrained_model_name_or_path, torch_dtype=torch.bfloat16 if args.mixed_precision == "bf16" else torch.float16, revision=args.revision, variant=args.variant, ) pipeline.set_progress_bar_config(disable=True) num_new_images = args.num_class_images - cur_class_images logger.info(f"Number of class images to sample: {num_new_images}.") sample_dataset = PromptDataset(args.class_prompt, num_new_images) sample_dataloader = torch.utils.data.DataLoader(sample_dataset, batch_size=args.sample_batch_size) sample_dataloader = accelerator.prepare(sample_dataloader) pipeline.to(accelerator.device) for example in tqdm( sample_dataloader, desc="Generating class images", disable=not accelerator.is_local_main_process ): images = pipeline(example["prompt"]).images for i, image in enumerate(images): hash_image = insecure_hashlib.sha1(image.tobytes()).hexdigest() image_filename = class_images_dir / f"{example['index'][i] + cur_class_images}-{hash_image}.jpg" image.save(image_filename) pipeline.to("cpu") del pipeline free_memory() # Handle the repository creation if accelerator.is_main_process: if args.output_dir is not None: os.makedirs(args.output_dir, exist_ok=True) if args.push_to_hub: repo_id = create_repo( repo_id=args.hub_model_id or Path(args.output_dir).name, exist_ok=True, ).repo_id # Load the tokenizers tokenizer = Qwen2Tokenizer.from_pretrained( args.pretrained_model_name_or_path, subfolder="tokenizer", revision=args.revision, ) # For mixed precision training we cast all non-trainable weights (vae, text_encoder and transformer) to half-precision # as these weights are only used for inference, keeping weights in full precision is not required. weight_dtype = torch.float32 if accelerator.mixed_precision == "fp16": weight_dtype = torch.float16 elif accelerator.mixed_precision == "bf16": weight_dtype = torch.bfloat16 # Load scheduler and models noise_scheduler = FlowMatchEulerDiscreteScheduler.from_pretrained( args.pretrained_model_name_or_path, subfolder="scheduler", revision=args.revision, shift=3.0 ) noise_scheduler_copy = copy.deepcopy(noise_scheduler) vae = AutoencoderKLQwenImage.from_pretrained( args.pretrained_model_name_or_path, subfolder="vae", revision=args.revision, variant=args.variant, ) vae_scale_factor = 2 ** len(vae.temperal_downsample) latents_mean = (torch.tensor(vae.config.latents_mean).view(1, vae.config.z_dim, 1, 1, 1)).to(accelerator.device) latents_std = 1.0 / torch.tensor(vae.config.latents_std).view(1, vae.config.z_dim, 1, 1, 1).to(accelerator.device) text_encoder = Qwen2_5_VLForConditionalGeneration.from_pretrained( args.pretrained_model_name_or_path, subfolder="text_encoder", revision=args.revision, torch_dtype=weight_dtype ) quantization_config = None if args.bnb_quantization_config_path is not None: with open(args.bnb_quantization_config_path, "r") as f: config_kwargs = json.load(f) if "load_in_4bit" in config_kwargs and config_kwargs["load_in_4bit"]: config_kwargs["bnb_4bit_compute_dtype"] = weight_dtype quantization_config = BitsAndBytesConfig(**config_kwargs) transformer = QwenImageTransformer2DModel.from_pretrained( args.pretrained_model_name_or_path, subfolder="transformer", revision=args.revision, variant=args.variant, quantization_config=quantization_config, torch_dtype=weight_dtype, ) if args.bnb_quantization_config_path is not None: transformer = prepare_model_for_kbit_training(transformer, use_gradient_checkpointing=False) # We only train the additional adapter LoRA layers transformer.requires_grad_(False) vae.requires_grad_(False) text_encoder.requires_grad_(False) if torch.backends.mps.is_available() and weight_dtype == torch.bfloat16: # due to pytorch#99272, MPS does not yet support bfloat16. raise ValueError( "Mixed precision training with bfloat16 is not supported on MPS. Please use fp16 (recommended) or fp32 instead." ) to_kwargs = {"dtype": weight_dtype, "device": accelerator.device} if not args.offload else {"dtype": weight_dtype} # flux vae is stable in bf16 so load it in weight_dtype to reduce memory vae.to(**to_kwargs) text_encoder.to(**to_kwargs) # we never offload the transformer to CPU, so we can just use the accelerator device transformer_to_kwargs = ( {"device": accelerator.device} if args.bnb_quantization_config_path is not None else {"device": accelerator.device, "dtype": weight_dtype} ) transformer.to(**transformer_to_kwargs) # Initialize a text encoding pipeline and keep it to CPU for now. text_encoding_pipeline = QwenImagePipeline.from_pretrained( args.pretrained_model_name_or_path, vae=None, transformer=None, tokenizer=tokenizer, text_encoder=text_encoder, scheduler=None, ) if args.gradient_checkpointing: transformer.enable_gradient_checkpointing() if args.lora_layers is not None: target_modules = [layer.strip() for layer in args.lora_layers.split(",")] else: target_modules = ["to_k", "to_q", "to_v", "to_out.0"] # now we will add new LoRA weights the transformer layers transformer_lora_config = LoraConfig( r=args.rank, lora_alpha=args.lora_alpha, lora_dropout=args.lora_dropout, init_lora_weights="gaussian", target_modules=target_modules, ) transformer.add_adapter(transformer_lora_config) def unwrap_model(model): model = accelerator.unwrap_model(model) model = model._orig_mod if is_compiled_module(model) else model return model # create custom saving & loading hooks so that `accelerator.save_state(...)` serializes in a nice format def save_model_hook(models, weights, output_dir): if accelerator.is_main_process: transformer_lora_layers_to_save = None modules_to_save = {} for model in models: if isinstance(unwrap_model(model), type(unwrap_model(transformer))): model = unwrap_model(model) transformer_lora_layers_to_save = get_peft_model_state_dict(model) modules_to_save["transformer"] = model else: raise ValueError(f"unexpected save model: {model.__class__}") # make sure to pop weight so that corresponding model is not saved again if weights: weights.pop() QwenImagePipeline.save_lora_weights( output_dir, transformer_lora_layers=transformer_lora_layers_to_save, **_collate_lora_metadata(modules_to_save), ) def load_model_hook(models, input_dir): transformer_ = None if not accelerator.distributed_type == DistributedType.DEEPSPEED: while len(models) > 0: model = models.pop() if isinstance(unwrap_model(model), type(unwrap_model(transformer))): model = unwrap_model(model) transformer_ = model else: raise ValueError(f"unexpected save model: {model.__class__}") else: transformer_ = QwenImageTransformer2DModel.from_pretrained( args.pretrained_model_name_or_path, subfolder="transformer" ) transformer_.add_adapter(transformer_lora_config) lora_state_dict = QwenImagePipeline.lora_state_dict(input_dir) transformer_state_dict = { f"{k.replace('transformer.', '')}": v for k, v in lora_state_dict.items() if k.startswith("transformer.") } transformer_state_dict = convert_unet_state_dict_to_peft(transformer_state_dict) incompatible_keys = set_peft_model_state_dict(transformer_, transformer_state_dict, adapter_name="default") if incompatible_keys is not None: # check only for unexpected keys unexpected_keys = getattr(incompatible_keys, "unexpected_keys", None) if unexpected_keys: logger.warning( f"Loading adapter weights from state_dict led to unexpected keys not found in the model: " f" {unexpected_keys}. " ) # Make sure the trainable params are in float32. This is again needed since the base models # are in `weight_dtype`. More details: # https://github.com/huggingface/diffusers/pull/6514#discussion_r1449796804 if args.mixed_precision == "fp16": models = [transformer_] # only upcast trainable parameters (LoRA) into fp32 cast_training_params(models) accelerator.register_save_state_pre_hook(save_model_hook) accelerator.register_load_state_pre_hook(load_model_hook) # Enable TF32 for faster training on Ampere GPUs, # cf https://pytorch.org/docs/stable/notes/cuda.html#tensorfloat-32-tf32-on-ampere-devices if args.allow_tf32 and torch.cuda.is_available(): torch.backends.cuda.matmul.allow_tf32 = True if args.scale_lr: args.learning_rate = ( args.learning_rate * args.gradient_accumulation_steps * args.train_batch_size * accelerator.num_processes ) # Make sure the trainable params are in float32. if args.mixed_precision == "fp16": models = [transformer] # only upcast trainable parameters (LoRA) into fp32 cast_training_params(models, dtype=torch.float32) transformer_lora_parameters = list(filter(lambda p: p.requires_grad, transformer.parameters())) # Optimization parameters transformer_parameters_with_lr = {"params": transformer_lora_parameters, "lr": args.learning_rate} params_to_optimize = [transformer_parameters_with_lr] # Optimizer creation if not (args.optimizer.lower() == "prodigy" or args.optimizer.lower() == "adamw"): logger.warning( f"Unsupported choice of optimizer: {args.optimizer}.Supported optimizers include [adamW, prodigy]." "Defaulting to adamW" ) args.optimizer = "adamw" if args.use_8bit_adam and not args.optimizer.lower() == "adamw": logger.warning( f"use_8bit_adam is ignored when optimizer is not set to 'AdamW'. Optimizer was " f"set to {args.optimizer.lower()}" ) if args.optimizer.lower() == "adamw": if args.use_8bit_adam: try: import bitsandbytes as bnb except ImportError: raise ImportError( "To use 8-bit Adam, please install the bitsandbytes library: `pip install bitsandbytes`." ) optimizer_class = bnb.optim.AdamW8bit else: optimizer_class = torch.optim.AdamW optimizer = optimizer_class( params_to_optimize, betas=(args.adam_beta1, args.adam_beta2), weight_decay=args.adam_weight_decay, eps=args.adam_epsilon, ) if args.optimizer.lower() == "prodigy": try: import prodigyopt except ImportError: raise ImportError("To use Prodigy, please install the prodigyopt library: `pip install prodigyopt`") optimizer_class = prodigyopt.Prodigy if args.learning_rate <= 0.1: logger.warning( "Learning rate is too low. When using prodigy, it's generally better to set learning rate around 1.0" ) optimizer = optimizer_class( params_to_optimize, betas=(args.adam_beta1, args.adam_beta2), beta3=args.prodigy_beta3, weight_decay=args.adam_weight_decay, eps=args.adam_epsilon, decouple=args.prodigy_decouple, use_bias_correction=args.prodigy_use_bias_correction, safeguard_warmup=args.prodigy_safeguard_warmup, ) # Dataset and DataLoaders creation: train_dataset = DreamBoothDataset( instance_data_root=args.instance_data_dir, instance_prompt=args.instance_prompt, class_prompt=args.class_prompt, class_data_root=args.class_data_dir if args.with_prior_preservation else None, class_num=args.num_class_images, size=args.resolution, repeats=args.repeats, center_crop=args.center_crop, ) train_dataloader = torch.utils.data.DataLoader( train_dataset, batch_size=args.train_batch_size, shuffle=True, collate_fn=lambda examples: collate_fn(examples, args.with_prior_preservation), num_workers=args.dataloader_num_workers, ) def compute_text_embeddings(prompt, text_encoding_pipeline): with torch.no_grad(): prompt_embeds, prompt_embeds_mask = text_encoding_pipeline.encode_prompt( prompt=prompt, max_sequence_length=args.max_sequence_length ) return prompt_embeds, prompt_embeds_mask # If no type of tuning is done on the text_encoder and custom instance prompts are NOT # provided (i.e. the --instance_prompt is used for all images), we encode the instance prompt once to avoid # the redundant encoding. if not train_dataset.custom_instance_prompts: with offload_models(text_encoding_pipeline, device=accelerator.device, offload=args.offload): instance_prompt_embeds, instance_prompt_embeds_mask = compute_text_embeddings( args.instance_prompt, text_encoding_pipeline ) # Handle class prompt for prior-preservation. if args.with_prior_preservation: with offload_models(text_encoding_pipeline, device=accelerator.device, offload=args.offload): class_prompt_embeds, class_prompt_embeds_mask = compute_text_embeddings( args.class_prompt, text_encoding_pipeline ) validation_embeddings = {} if args.validation_prompt is not None: with offload_models(text_encoding_pipeline, device=accelerator.device, offload=args.offload): (validation_embeddings["prompt_embeds"], validation_embeddings["prompt_embeds_mask"]) = ( compute_text_embeddings(args.validation_prompt, text_encoding_pipeline) ) # If custom instance prompts are NOT provided (i.e. the instance prompt is used for all images), # pack the statically computed variables appropriately here. This is so that we don't # have to pass them to the dataloader. if not train_dataset.custom_instance_prompts: prompt_embeds = instance_prompt_embeds prompt_embeds_mask = instance_prompt_embeds_mask if args.with_prior_preservation: prompt_embeds = torch.cat([prompt_embeds, class_prompt_embeds], dim=0) prompt_embeds_mask = torch.cat([prompt_embeds_mask, class_prompt_embeds_mask], dim=0) # if cache_latents is set to True, we encode images to latents and store them. # Similar to pre-encoding in the case of a single instance prompt, if custom prompts are provided # we encode them in advance as well. precompute_latents = args.cache_latents or train_dataset.custom_instance_prompts if precompute_latents: prompt_embeds_cache = [] prompt_embeds_mask_cache = [] latents_cache = [] for batch in tqdm(train_dataloader, desc="Caching latents"): with torch.no_grad(): if args.cache_latents: with offload_models(vae, device=accelerator.device, offload=args.offload): batch["pixel_values"] = batch["pixel_values"].to( accelerator.device, non_blocking=True, dtype=vae.dtype ) latents_cache.append(vae.encode(batch["pixel_values"]).latent_dist) if train_dataset.custom_instance_prompts: with offload_models(text_encoding_pipeline, device=accelerator.device, offload=args.offload): prompt_embeds, prompt_embeds_mask = compute_text_embeddings( batch["prompts"], text_encoding_pipeline ) prompt_embeds_cache.append(prompt_embeds) prompt_embeds_mask_cache.append(prompt_embeds_mask) # move back to cpu before deleting to ensure memory is freed see: https://github.com/huggingface/diffusers/issues/11376#issue-3008144624 if args.cache_latents: vae = vae.to("cpu") del vae # move back to cpu before deleting to ensure memory is freed see: https://github.com/huggingface/diffusers/issues/11376#issue-3008144624 text_encoding_pipeline = text_encoding_pipeline.to("cpu") del text_encoder, tokenizer free_memory() # Scheduler and math around the number of training steps. overrode_max_train_steps = False num_update_steps_per_epoch = math.ceil(len(train_dataloader) / args.gradient_accumulation_steps) if args.max_train_steps is None: args.max_train_steps = args.num_train_epochs * num_update_steps_per_epoch overrode_max_train_steps = True lr_scheduler = get_scheduler( args.lr_scheduler, optimizer=optimizer, num_warmup_steps=args.lr_warmup_steps * accelerator.num_processes, num_training_steps=args.max_train_steps * accelerator.num_processes, num_cycles=args.lr_num_cycles, power=args.lr_power, ) # Prepare everything with our `accelerator`. transformer, optimizer, train_dataloader, lr_scheduler = accelerator.prepare( transformer, optimizer, train_dataloader, lr_scheduler ) # We need to recalculate our total training steps as the size of the training dataloader may have changed. num_update_steps_per_epoch = math.ceil(len(train_dataloader) / args.gradient_accumulation_steps) if overrode_max_train_steps: args.max_train_steps = args.num_train_epochs * num_update_steps_per_epoch # Afterwards we recalculate our number of training epochs args.num_train_epochs = math.ceil(args.max_train_steps / num_update_steps_per_epoch) # We need to initialize the trackers we use, and also store our configuration. # The trackers initializes automatically on the main process. if accelerator.is_main_process: tracker_name = "dreambooth-qwen-image-lora" accelerator.init_trackers(tracker_name, config=vars(args)) # Train! total_batch_size = args.train_batch_size * accelerator.num_processes * args.gradient_accumulation_steps logger.info("***** Running training *****") logger.info(f" Num examples = {len(train_dataset)}") logger.info(f" Num batches each epoch = {len(train_dataloader)}") logger.info(f" Num Epochs = {args.num_train_epochs}") logger.info(f" Instantaneous batch size per device = {args.train_batch_size}") logger.info(f" Total train batch size (w. parallel, distributed & accumulation) = {total_batch_size}") logger.info(f" Gradient Accumulation steps = {args.gradient_accumulation_steps}") logger.info(f" Total optimization steps = {args.max_train_steps}") global_step = 0 first_epoch = 0 # Potentially load in the weights and states from a previous save if args.resume_from_checkpoint: if args.resume_from_checkpoint != "latest": path = os.path.basename(args.resume_from_checkpoint) else: # Get the mos recent checkpoint dirs = os.listdir(args.output_dir) dirs = [d for d in dirs if d.startswith("checkpoint")] dirs = sorted(dirs, key=lambda x: int(x.split("-")[1])) path = dirs[-1] if len(dirs) > 0 else None if path is None: accelerator.print( f"Checkpoint '{args.resume_from_checkpoint}' does not exist. Starting a new training run." ) args.resume_from_checkpoint = None initial_global_step = 0 else: accelerator.print(f"Resuming from checkpoint {path}") accelerator.load_state(os.path.join(args.output_dir, path)) global_step = int(path.split("-")[1]) initial_global_step = global_step first_epoch = global_step // num_update_steps_per_epoch else: initial_global_step = 0 progress_bar = tqdm( range(0, args.max_train_steps), initial=initial_global_step, desc="Steps", # Only show the progress bar once on each machine. disable=not accelerator.is_local_main_process, ) def get_sigmas(timesteps, n_dim=4, dtype=torch.float32): sigmas = noise_scheduler_copy.sigmas.to(device=accelerator.device, dtype=dtype) schedule_timesteps = noise_scheduler_copy.timesteps.to(accelerator.device) timesteps = timesteps.to(accelerator.device) step_indices = [(schedule_timesteps == t).nonzero().item() for t in timesteps] sigma = sigmas[step_indices].flatten() while len(sigma.shape) < n_dim: sigma = sigma.unsqueeze(-1) return sigma for epoch in range(first_epoch, args.num_train_epochs): transformer.train() for step, batch in enumerate(train_dataloader): models_to_accumulate = [transformer] prompts = batch["prompts"] with accelerator.accumulate(models_to_accumulate): # encode batch prompts when custom prompts are provided for each image - if train_dataset.custom_instance_prompts: prompt_embeds = prompt_embeds_cache[step] prompt_embeds_mask = prompt_embeds_mask_cache[step] else: num_repeat_elements = len(prompts) prompt_embeds = prompt_embeds.repeat(num_repeat_elements, 1, 1) if prompt_embeds_mask is not None: prompt_embeds_mask = prompt_embeds_mask.repeat(num_repeat_elements, 1) # Convert images to latent space if args.cache_latents: model_input = latents_cache[step].sample() else: with offload_models(vae, device=accelerator.device, offload=args.offload): pixel_values = batch["pixel_values"].to(dtype=vae.dtype) model_input = vae.encode(pixel_values).latent_dist.sample() model_input = (model_input - latents_mean) * latents_std model_input = model_input.to(dtype=weight_dtype) # Sample noise that we'll add to the latents noise = torch.randn_like(model_input) bsz = model_input.shape[0] # Sample a random timestep for each image # for weighting schemes where we sample timesteps non-uniformly u = compute_density_for_timestep_sampling( weighting_scheme=args.weighting_scheme, batch_size=bsz, logit_mean=args.logit_mean, logit_std=args.logit_std, mode_scale=args.mode_scale, ) indices = (u * noise_scheduler_copy.config.num_train_timesteps).long() timesteps = noise_scheduler_copy.timesteps[indices].to(device=model_input.device) # Add noise according to flow matching. # zt = (1 - texp) * x + texp * z1 sigmas = get_sigmas(timesteps, n_dim=model_input.ndim, dtype=model_input.dtype) noisy_model_input = (1.0 - sigmas) * model_input + sigmas * noise # Predict the noise residual img_shapes = [ (1, args.resolution // vae_scale_factor // 2, args.resolution // vae_scale_factor // 2) ] * bsz # transpose the dimensions noisy_model_input = noisy_model_input.permute(0, 2, 1, 3, 4) packed_noisy_model_input = QwenImagePipeline._pack_latents( noisy_model_input, batch_size=model_input.shape[0], num_channels_latents=model_input.shape[1], height=model_input.shape[3], width=model_input.shape[4], ) model_pred = transformer( hidden_states=packed_noisy_model_input, encoder_hidden_states=prompt_embeds, encoder_hidden_states_mask=prompt_embeds_mask, timestep=timesteps / 1000, img_shapes=img_shapes, return_dict=False, )[0] model_pred = QwenImagePipeline._unpack_latents( model_pred, args.resolution, args.resolution, vae_scale_factor ) # these weighting schemes use a uniform timestep sampling # and instead post-weight the loss weighting = compute_loss_weighting_for_sd3(weighting_scheme=args.weighting_scheme, sigmas=sigmas) target = noise - model_input if args.with_prior_preservation: # Chunk the noise and model_pred into two parts and compute the loss on each part separately. model_pred, model_pred_prior = torch.chunk(model_pred, 2, dim=0) target, target_prior = torch.chunk(target, 2, dim=0) # Compute prior loss prior_loss = torch.mean( (weighting.float() * (model_pred_prior.float() - target_prior.float()) ** 2).reshape( target_prior.shape[0], -1 ), 1, ) prior_loss = prior_loss.mean() # Compute regular loss. loss = torch.mean( (weighting.float() * (model_pred.float() - target.float()) ** 2).reshape(target.shape[0], -1), 1, ) loss = loss.mean() if args.with_prior_preservation: # Add the prior loss to the instance loss. loss = loss + args.prior_loss_weight * prior_loss accelerator.backward(loss) if accelerator.sync_gradients: params_to_clip = transformer.parameters() accelerator.clip_grad_norm_(params_to_clip, args.max_grad_norm) optimizer.step() lr_scheduler.step() optimizer.zero_grad() # Checks if the accelerator has performed an optimization step behind the scenes if accelerator.sync_gradients: progress_bar.update(1) global_step += 1 if accelerator.is_main_process or accelerator.distributed_type == DistributedType.DEEPSPEED: if global_step % args.checkpointing_steps == 0: # _before_ saving state, check if this save would set us over the `checkpoints_total_limit` if args.checkpoints_total_limit is not None: checkpoints = os.listdir(args.output_dir) checkpoints = [d for d in checkpoints if d.startswith("checkpoint")] checkpoints = sorted(checkpoints, key=lambda x: int(x.split("-")[1])) # before we save the new checkpoint, we need to have at _most_ `checkpoints_total_limit - 1` checkpoints if len(checkpoints) >= args.checkpoints_total_limit: num_to_remove = len(checkpoints) - args.checkpoints_total_limit + 1 removing_checkpoints = checkpoints[0:num_to_remove] logger.info( f"{len(checkpoints)} checkpoints already exist, removing {len(removing_checkpoints)} checkpoints" ) logger.info(f"removing checkpoints: {', '.join(removing_checkpoints)}") for removing_checkpoint in removing_checkpoints: removing_checkpoint = os.path.join(args.output_dir, removing_checkpoint) shutil.rmtree(removing_checkpoint) save_path = os.path.join(args.output_dir, f"checkpoint-{global_step}") accelerator.save_state(save_path) logger.info(f"Saved state to {save_path}") logs = {"loss": loss.detach().item(), "lr": lr_scheduler.get_last_lr()[0]} progress_bar.set_postfix(**logs) accelerator.log(logs, step=global_step) if global_step >= args.max_train_steps: break if accelerator.is_main_process: if args.validation_prompt is not None and epoch % args.validation_epochs == 0: # create pipeline pipeline = QwenImagePipeline.from_pretrained( args.pretrained_model_name_or_path, tokenizer=None, text_encoder=None, transformer=accelerator.unwrap_model(transformer), revision=args.revision, variant=args.variant, torch_dtype=weight_dtype, ) images = log_validation( pipeline=pipeline, args=args, accelerator=accelerator, pipeline_args=validation_embeddings, torch_dtype=weight_dtype, epoch=epoch, ) del pipeline images = None free_memory() # Save the lora layers accelerator.wait_for_everyone() if accelerator.is_main_process: modules_to_save = {} transformer = unwrap_model(transformer) if args.bnb_quantization_config_path is None: if args.upcast_before_saving: transformer.to(torch.float32) else: transformer = transformer.to(weight_dtype) transformer_lora_layers = get_peft_model_state_dict(transformer) modules_to_save["transformer"] = transformer QwenImagePipeline.save_lora_weights( save_directory=args.output_dir, transformer_lora_layers=transformer_lora_layers, **_collate_lora_metadata(modules_to_save), ) images = [] run_validation = (args.validation_prompt and args.num_validation_images > 0) or (args.final_validation_prompt) should_run_final_inference = not args.skip_final_inference and run_validation if should_run_final_inference: # Final inference # Load previous pipeline pipeline = QwenImagePipeline.from_pretrained( args.pretrained_model_name_or_path, tokenizer=None, text_encoder=None, revision=args.revision, variant=args.variant, torch_dtype=weight_dtype, ) # load attention processors pipeline.load_lora_weights(args.output_dir) # run inference images = log_validation( pipeline=pipeline, args=args, accelerator=accelerator, pipeline_args=validation_embeddings, epoch=epoch, is_final_validation=True, torch_dtype=weight_dtype, ) del pipeline free_memory() validation_prompt = args.validation_prompt if args.validation_prompt else args.final_validation_prompt save_model_card( (args.hub_model_id or Path(args.output_dir).name) if not args.push_to_hub else repo_id, images=images, base_model=args.pretrained_model_name_or_path, instance_prompt=args.instance_prompt, validation_prompt=validation_prompt, repo_folder=args.output_dir, ) if args.push_to_hub: upload_folder( repo_id=repo_id, folder_path=args.output_dir, commit_message="End of training", ignore_patterns=["step_*", "epoch_*"], ) images = None accelerator.end_training() if __name__ == "__main__": args = parse_args() main(args)
{ "repo_id": "huggingface/diffusers", "file_path": "examples/dreambooth/train_dreambooth_lora_qwen_image.py", "license": "Apache License 2.0", "lines": 1506, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
huggingface/diffusers:tests/lora/test_lora_layers_qwenimage.py
# coding=utf-8 # Copyright 2025 HuggingFace Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import sys import unittest import torch from transformers import Qwen2_5_VLForConditionalGeneration, Qwen2Tokenizer from diffusers import ( AutoencoderKLQwenImage, FlowMatchEulerDiscreteScheduler, QwenImagePipeline, QwenImageTransformer2DModel, ) from ..testing_utils import floats_tensor, require_peft_backend sys.path.append(".") from .utils import PeftLoraLoaderMixinTests # noqa: E402 @require_peft_backend class QwenImageLoRATests(unittest.TestCase, PeftLoraLoaderMixinTests): pipeline_class = QwenImagePipeline scheduler_cls = FlowMatchEulerDiscreteScheduler scheduler_kwargs = {} transformer_kwargs = { "patch_size": 2, "in_channels": 16, "out_channels": 4, "num_layers": 2, "attention_head_dim": 16, "num_attention_heads": 3, "joint_attention_dim": 16, "guidance_embeds": False, "axes_dims_rope": (8, 4, 4), } transformer_cls = QwenImageTransformer2DModel z_dim = 4 vae_kwargs = { "base_dim": z_dim * 6, "z_dim": z_dim, "dim_mult": [1, 2, 4], "num_res_blocks": 1, "temperal_downsample": [False, True], "latents_mean": [0.0] * 4, "latents_std": [1.0] * 4, } vae_cls = AutoencoderKLQwenImage tokenizer_cls, tokenizer_id = Qwen2Tokenizer, "hf-internal-testing/tiny-random-Qwen25VLForCondGen" text_encoder_cls, text_encoder_id = ( Qwen2_5_VLForConditionalGeneration, "hf-internal-testing/tiny-random-Qwen25VLForCondGen", ) denoiser_target_modules = ["to_q", "to_k", "to_v", "to_out.0"] supports_text_encoder_loras = False @property def output_shape(self): return (1, 8, 8, 3) def get_dummy_inputs(self, with_generator=True): batch_size = 1 sequence_length = 10 num_channels = 4 sizes = (32, 32) generator = torch.manual_seed(0) noise = floats_tensor((batch_size, num_channels) + sizes) input_ids = torch.randint(1, sequence_length, size=(batch_size, sequence_length), generator=generator) pipeline_inputs = { "prompt": "A painting of a squirrel eating a burger", "num_inference_steps": 4, "guidance_scale": 0.0, "height": 8, "width": 8, "output_type": "np", } if with_generator: pipeline_inputs.update({"generator": generator}) return noise, input_ids, pipeline_inputs @unittest.skip("Not supported in Qwen Image.") def test_simple_inference_with_text_denoiser_block_scale(self): pass @unittest.skip("Not supported in Qwen Image.") def test_simple_inference_with_text_denoiser_block_scale_for_all_dict_options(self): pass @unittest.skip("Not supported in Qwen Image.") def test_modify_padding_mode(self): pass
{ "repo_id": "huggingface/diffusers", "file_path": "tests/lora/test_lora_layers_qwenimage.py", "license": "Apache License 2.0", "lines": 93, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
test
huggingface/diffusers:tests/pipelines/qwenimage/test_qwenimage.py
# Copyright 2025 The HuggingFace Team. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest import numpy as np import torch from transformers import Qwen2_5_VLConfig, Qwen2_5_VLForConditionalGeneration, Qwen2Tokenizer from diffusers import ( AutoencoderKLQwenImage, FlowMatchEulerDiscreteScheduler, QwenImagePipeline, QwenImageTransformer2DModel, ) from ...testing_utils import enable_full_determinism, torch_device from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS from ..test_pipelines_common import PipelineTesterMixin, to_np enable_full_determinism() class QwenImagePipelineFastTests(PipelineTesterMixin, unittest.TestCase): pipeline_class = QwenImagePipeline params = TEXT_TO_IMAGE_PARAMS - {"cross_attention_kwargs"} batch_params = TEXT_TO_IMAGE_BATCH_PARAMS image_params = TEXT_TO_IMAGE_IMAGE_PARAMS image_latents_params = TEXT_TO_IMAGE_IMAGE_PARAMS required_optional_params = frozenset( [ "num_inference_steps", "generator", "latents", "return_dict", "callback_on_step_end", "callback_on_step_end_tensor_inputs", ] ) supports_dduf = False test_xformers_attention = False test_layerwise_casting = True test_group_offloading = True def get_dummy_components(self): torch.manual_seed(0) transformer = QwenImageTransformer2DModel( patch_size=2, in_channels=16, out_channels=4, num_layers=2, attention_head_dim=16, num_attention_heads=3, joint_attention_dim=16, guidance_embeds=False, axes_dims_rope=(8, 4, 4), ) torch.manual_seed(0) z_dim = 4 vae = AutoencoderKLQwenImage( base_dim=z_dim * 6, z_dim=z_dim, dim_mult=[1, 2, 4], num_res_blocks=1, temperal_downsample=[False, True], # fmt: off latents_mean=[0.0] * 4, latents_std=[1.0] * 4, # fmt: on ) torch.manual_seed(0) scheduler = FlowMatchEulerDiscreteScheduler() torch.manual_seed(0) config = Qwen2_5_VLConfig( text_config={ "hidden_size": 16, "intermediate_size": 16, "num_hidden_layers": 2, "num_attention_heads": 2, "num_key_value_heads": 2, "rope_scaling": { "mrope_section": [1, 1, 2], "rope_type": "default", "type": "default", }, "rope_theta": 1000000.0, }, vision_config={ "depth": 2, "hidden_size": 16, "intermediate_size": 16, "num_heads": 2, "out_hidden_size": 16, }, hidden_size=16, vocab_size=152064, vision_end_token_id=151653, vision_start_token_id=151652, vision_token_id=151654, ) text_encoder = Qwen2_5_VLForConditionalGeneration(config).eval() tokenizer = Qwen2Tokenizer.from_pretrained("hf-internal-testing/tiny-random-Qwen2VLForConditionalGeneration") components = { "transformer": transformer, "vae": vae, "scheduler": scheduler, "text_encoder": text_encoder, "tokenizer": tokenizer, } return components def get_dummy_inputs(self, device, seed=0): if str(device).startswith("mps"): generator = torch.manual_seed(seed) else: generator = torch.Generator(device=device).manual_seed(seed) inputs = { "prompt": "dance monkey", "negative_prompt": "bad quality", "generator": generator, "num_inference_steps": 2, "guidance_scale": 3.0, "true_cfg_scale": 1.0, "height": 32, "width": 32, "max_sequence_length": 16, "output_type": "pt", } return inputs def test_inference(self): device = "cpu" components = self.get_dummy_components() pipe = self.pipeline_class(**components) pipe.to(device) pipe.set_progress_bar_config(disable=None) inputs = self.get_dummy_inputs(device) image = pipe(**inputs).images generated_image = image[0] self.assertEqual(generated_image.shape, (3, 32, 32)) # fmt: off expected_slice = torch.tensor([0.5633, 0.6368, 0.6015, 0.5637, 0.5817, 0.5528, 0.5718, 0.6326, 0.4147, 0.3556, 0.5623, 0.4833, 0.4971, 0.5262, 0.4087, 0.5021]) # fmt: on generated_slice = generated_image.flatten() generated_slice = torch.cat([generated_slice[:8], generated_slice[-8:]]) self.assertTrue(torch.allclose(generated_slice, expected_slice, atol=5e-3)) def test_inference_batch_single_identical(self): self._test_inference_batch_single_identical(batch_size=3, expected_max_diff=1e-1) def test_attention_slicing_forward_pass( self, test_max_difference=True, test_mean_pixel_difference=True, expected_max_diff=1e-3 ): if not self.test_attention_slicing: return components = self.get_dummy_components() pipe = self.pipeline_class(**components) for component in pipe.components.values(): if hasattr(component, "set_default_attn_processor"): component.set_default_attn_processor() pipe.to(torch_device) pipe.set_progress_bar_config(disable=None) generator_device = "cpu" inputs = self.get_dummy_inputs(generator_device) output_without_slicing = pipe(**inputs)[0] pipe.enable_attention_slicing(slice_size=1) inputs = self.get_dummy_inputs(generator_device) output_with_slicing1 = pipe(**inputs)[0] pipe.enable_attention_slicing(slice_size=2) inputs = self.get_dummy_inputs(generator_device) output_with_slicing2 = pipe(**inputs)[0] if test_max_difference: max_diff1 = np.abs(to_np(output_with_slicing1) - to_np(output_without_slicing)).max() max_diff2 = np.abs(to_np(output_with_slicing2) - to_np(output_without_slicing)).max() self.assertLess( max(max_diff1, max_diff2), expected_max_diff, "Attention slicing should not affect the inference results", ) def test_vae_tiling(self, expected_diff_max: float = 0.2): generator_device = "cpu" components = self.get_dummy_components() pipe = self.pipeline_class(**components) pipe.to("cpu") pipe.set_progress_bar_config(disable=None) # Without tiling inputs = self.get_dummy_inputs(generator_device) inputs["height"] = inputs["width"] = 128 output_without_tiling = pipe(**inputs)[0] # With tiling pipe.vae.enable_tiling( tile_sample_min_height=96, tile_sample_min_width=96, tile_sample_stride_height=64, tile_sample_stride_width=64, ) inputs = self.get_dummy_inputs(generator_device) inputs["height"] = inputs["width"] = 128 output_with_tiling = pipe(**inputs)[0] self.assertLess( (to_np(output_without_tiling) - to_np(output_with_tiling)).max(), expected_diff_max, "VAE tiling should not affect the inference results", )
{ "repo_id": "huggingface/diffusers", "file_path": "tests/pipelines/qwenimage/test_qwenimage.py", "license": "Apache License 2.0", "lines": 203, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
test
huggingface/diffusers:tests/pipelines/wan/test_wan_22.py
# Copyright 2025 The HuggingFace Team. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import tempfile import unittest import numpy as np import torch from transformers import AutoConfig, AutoTokenizer, T5EncoderModel from diffusers import AutoencoderKLWan, UniPCMultistepScheduler, WanPipeline, WanTransformer3DModel from ...testing_utils import enable_full_determinism, torch_device from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS from ..test_pipelines_common import PipelineTesterMixin enable_full_determinism() class Wan22PipelineFastTests(PipelineTesterMixin, unittest.TestCase): pipeline_class = WanPipeline params = TEXT_TO_IMAGE_PARAMS - {"cross_attention_kwargs"} batch_params = TEXT_TO_IMAGE_BATCH_PARAMS image_params = TEXT_TO_IMAGE_IMAGE_PARAMS image_latents_params = TEXT_TO_IMAGE_IMAGE_PARAMS required_optional_params = frozenset( [ "num_inference_steps", "generator", "latents", "return_dict", "callback_on_step_end", "callback_on_step_end_tensor_inputs", ] ) test_xformers_attention = False supports_dduf = False def get_dummy_components(self): torch.manual_seed(0) vae = AutoencoderKLWan( base_dim=3, z_dim=16, dim_mult=[1, 1, 1, 1], num_res_blocks=1, temperal_downsample=[False, True, True], ) torch.manual_seed(0) scheduler = UniPCMultistepScheduler(prediction_type="flow_prediction", use_flow_sigmas=True, flow_shift=3.0) config = AutoConfig.from_pretrained("hf-internal-testing/tiny-random-t5") text_encoder = T5EncoderModel(config) tokenizer = AutoTokenizer.from_pretrained("hf-internal-testing/tiny-random-t5") torch.manual_seed(0) transformer = WanTransformer3DModel( patch_size=(1, 2, 2), num_attention_heads=2, attention_head_dim=12, in_channels=16, out_channels=16, text_dim=32, freq_dim=256, ffn_dim=32, num_layers=2, cross_attn_norm=True, qk_norm="rms_norm_across_heads", rope_max_seq_len=32, ) torch.manual_seed(0) transformer_2 = WanTransformer3DModel( patch_size=(1, 2, 2), num_attention_heads=2, attention_head_dim=12, in_channels=16, out_channels=16, text_dim=32, freq_dim=256, ffn_dim=32, num_layers=2, cross_attn_norm=True, qk_norm="rms_norm_across_heads", rope_max_seq_len=32, ) components = { "transformer": transformer, "vae": vae, "scheduler": scheduler, "text_encoder": text_encoder, "tokenizer": tokenizer, "transformer_2": transformer_2, "boundary_ratio": 0.875, } return components def get_dummy_inputs(self, device, seed=0): if str(device).startswith("mps"): generator = torch.manual_seed(seed) else: generator = torch.Generator(device=device).manual_seed(seed) inputs = { "prompt": "dance monkey", "negative_prompt": "negative", "generator": generator, "num_inference_steps": 2, "guidance_scale": 6.0, "height": 16, "width": 16, "num_frames": 9, "max_sequence_length": 16, "output_type": "pt", } return inputs def test_inference(self): device = "cpu" components = self.get_dummy_components() pipe = self.pipeline_class( **components, ) pipe.to(device) pipe.set_progress_bar_config(disable=None) inputs = self.get_dummy_inputs(device) video = pipe(**inputs).frames generated_video = video[0] self.assertEqual(generated_video.shape, (9, 3, 16, 16)) # fmt: off expected_slice = torch.tensor([0.4525, 0.452, 0.4485, 0.4534, 0.4524, 0.4529, 0.454, 0.453, 0.5127, 0.5326, 0.5204, 0.5253, 0.5439, 0.5424, 0.5133, 0.5078]) # fmt: on generated_slice = generated_video.flatten() generated_slice = torch.cat([generated_slice[:8], generated_slice[-8:]]) self.assertTrue(torch.allclose(generated_slice, expected_slice, atol=1e-3)) @unittest.skip("Test not supported") def test_attention_slicing_forward_pass(self): pass def test_save_load_optional_components(self, expected_max_difference=1e-4): optional_component = "transformer" components = self.get_dummy_components() components[optional_component] = None components["boundary_ratio"] = 1.0 # for wan 2.2 14B, transformer is not used when boundary_ratio is 1.0 pipe = self.pipeline_class(**components) for component in pipe.components.values(): if hasattr(component, "set_default_attn_processor"): component.set_default_attn_processor() pipe.to(torch_device) pipe.set_progress_bar_config(disable=None) generator_device = "cpu" inputs = self.get_dummy_inputs(generator_device) torch.manual_seed(0) output = pipe(**inputs)[0] with tempfile.TemporaryDirectory() as tmpdir: pipe.save_pretrained(tmpdir, safe_serialization=False) pipe_loaded = self.pipeline_class.from_pretrained(tmpdir) for component in pipe_loaded.components.values(): if hasattr(component, "set_default_attn_processor"): component.set_default_attn_processor() pipe_loaded.to(torch_device) pipe_loaded.set_progress_bar_config(disable=None) self.assertTrue( getattr(pipe_loaded, "transformer") is None, "`transformer` did not stay set to None after loading.", ) inputs = self.get_dummy_inputs(generator_device) torch.manual_seed(0) output_loaded = pipe_loaded(**inputs)[0] max_diff = np.abs(output.detach().cpu().numpy() - output_loaded.detach().cpu().numpy()).max() self.assertLess(max_diff, expected_max_difference) class Wan225BPipelineFastTests(PipelineTesterMixin, unittest.TestCase): pipeline_class = WanPipeline params = TEXT_TO_IMAGE_PARAMS - {"cross_attention_kwargs"} batch_params = TEXT_TO_IMAGE_BATCH_PARAMS image_params = TEXT_TO_IMAGE_IMAGE_PARAMS image_latents_params = TEXT_TO_IMAGE_IMAGE_PARAMS required_optional_params = frozenset( [ "num_inference_steps", "generator", "latents", "return_dict", "callback_on_step_end", "callback_on_step_end_tensor_inputs", ] ) test_xformers_attention = False supports_dduf = False def get_dummy_components(self): torch.manual_seed(0) vae = AutoencoderKLWan( base_dim=3, z_dim=48, in_channels=12, out_channels=12, is_residual=True, patch_size=2, latents_mean=[0.0] * 48, latents_std=[1.0] * 48, dim_mult=[1, 1, 1, 1], num_res_blocks=1, scale_factor_spatial=16, scale_factor_temporal=4, temperal_downsample=[False, True, True], ) torch.manual_seed(0) scheduler = UniPCMultistepScheduler(prediction_type="flow_prediction", use_flow_sigmas=True, flow_shift=3.0) config = AutoConfig.from_pretrained("hf-internal-testing/tiny-random-t5") text_encoder = T5EncoderModel(config) tokenizer = AutoTokenizer.from_pretrained("hf-internal-testing/tiny-random-t5") torch.manual_seed(0) transformer = WanTransformer3DModel( patch_size=(1, 2, 2), num_attention_heads=2, attention_head_dim=12, in_channels=48, out_channels=48, text_dim=32, freq_dim=256, ffn_dim=32, num_layers=2, cross_attn_norm=True, qk_norm="rms_norm_across_heads", rope_max_seq_len=32, ) components = { "transformer": transformer, "vae": vae, "scheduler": scheduler, "text_encoder": text_encoder, "tokenizer": tokenizer, "transformer_2": None, "boundary_ratio": None, "expand_timesteps": True, } return components def get_dummy_inputs(self, device, seed=0): if str(device).startswith("mps"): generator = torch.manual_seed(seed) else: generator = torch.Generator(device=device).manual_seed(seed) inputs = { "prompt": "dance monkey", "negative_prompt": "negative", # TODO "generator": generator, "num_inference_steps": 2, "guidance_scale": 6.0, "height": 32, "width": 32, "num_frames": 9, "max_sequence_length": 16, "output_type": "pt", } return inputs def test_inference(self): device = "cpu" components = self.get_dummy_components() pipe = self.pipeline_class( **components, ) pipe.to(device) pipe.set_progress_bar_config(disable=None) inputs = self.get_dummy_inputs(device) video = pipe(**inputs).frames generated_video = video[0] self.assertEqual(generated_video.shape, (9, 3, 32, 32)) # fmt: off expected_slice = torch.tensor([[[0.4814, 0.4298, 0.5094, 0.4289, 0.5061, 0.4301, 0.5043, 0.4284, 0.5375, 0.5965, 0.5527, 0.6014, 0.5228, 0.6076, 0.6644, 0.5651]]]) # fmt: on generated_slice = generated_video.flatten() generated_slice = torch.cat([generated_slice[:8], generated_slice[-8:]]) self.assertTrue( torch.allclose(generated_slice, expected_slice, atol=1e-3), f"generated_slice: {generated_slice}, expected_slice: {expected_slice}", ) @unittest.skip("Test not supported") def test_attention_slicing_forward_pass(self): pass def test_components_function(self): init_components = self.get_dummy_components() init_components.pop("boundary_ratio") init_components.pop("expand_timesteps") pipe = self.pipeline_class(**init_components) self.assertTrue(hasattr(pipe, "components")) self.assertTrue(set(pipe.components.keys()) == set(init_components.keys())) def test_save_load_optional_components(self, expected_max_difference=1e-4): optional_component = "transformer_2" components = self.get_dummy_components() components[optional_component] = None pipe = self.pipeline_class(**components) for component in pipe.components.values(): if hasattr(component, "set_default_attn_processor"): component.set_default_attn_processor() pipe.to(torch_device) pipe.set_progress_bar_config(disable=None) generator_device = "cpu" inputs = self.get_dummy_inputs(generator_device) torch.manual_seed(0) output = pipe(**inputs)[0] with tempfile.TemporaryDirectory() as tmpdir: pipe.save_pretrained(tmpdir, safe_serialization=False) pipe_loaded = self.pipeline_class.from_pretrained(tmpdir) for component in pipe_loaded.components.values(): if hasattr(component, "set_default_attn_processor"): component.set_default_attn_processor() pipe_loaded.to(torch_device) pipe_loaded.set_progress_bar_config(disable=None) self.assertTrue( getattr(pipe_loaded, optional_component) is None, f"`{optional_component}` did not stay set to None after loading.", ) inputs = self.get_dummy_inputs(generator_device) torch.manual_seed(0) output_loaded = pipe_loaded(**inputs)[0] max_diff = np.abs(output.detach().cpu().numpy() - output_loaded.detach().cpu().numpy()).max() self.assertLess(max_diff, expected_max_difference) def test_inference_batch_single_identical(self): self._test_inference_batch_single_identical(expected_max_diff=2e-3)
{ "repo_id": "huggingface/diffusers", "file_path": "tests/pipelines/wan/test_wan_22.py", "license": "Apache License 2.0", "lines": 315, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
test
huggingface/diffusers:tests/pipelines/wan/test_wan_22_image_to_video.py
# Copyright 2025 The HuggingFace Team. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import tempfile import unittest import numpy as np import torch from PIL import Image from transformers import AutoConfig, AutoTokenizer, T5EncoderModel from diffusers import AutoencoderKLWan, UniPCMultistepScheduler, WanImageToVideoPipeline, WanTransformer3DModel from ...testing_utils import ( enable_full_determinism, torch_device, ) from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS from ..test_pipelines_common import PipelineTesterMixin enable_full_determinism() class Wan22ImageToVideoPipelineFastTests(PipelineTesterMixin, unittest.TestCase): pipeline_class = WanImageToVideoPipeline params = TEXT_TO_IMAGE_PARAMS - {"cross_attention_kwargs"} batch_params = TEXT_TO_IMAGE_BATCH_PARAMS image_params = TEXT_TO_IMAGE_IMAGE_PARAMS image_latents_params = TEXT_TO_IMAGE_IMAGE_PARAMS required_optional_params = frozenset( [ "num_inference_steps", "generator", "latents", "return_dict", "callback_on_step_end", "callback_on_step_end_tensor_inputs", ] ) test_xformers_attention = False supports_dduf = False def get_dummy_components(self): torch.manual_seed(0) vae = AutoencoderKLWan( base_dim=3, z_dim=16, dim_mult=[1, 1, 1, 1], num_res_blocks=1, temperal_downsample=[False, True, True], ) torch.manual_seed(0) scheduler = UniPCMultistepScheduler(prediction_type="flow_prediction", use_flow_sigmas=True, flow_shift=3.0) config = AutoConfig.from_pretrained("hf-internal-testing/tiny-random-t5") text_encoder = T5EncoderModel(config) tokenizer = AutoTokenizer.from_pretrained("hf-internal-testing/tiny-random-t5") torch.manual_seed(0) transformer = WanTransformer3DModel( patch_size=(1, 2, 2), num_attention_heads=2, attention_head_dim=12, in_channels=36, out_channels=16, text_dim=32, freq_dim=256, ffn_dim=32, num_layers=2, cross_attn_norm=True, qk_norm="rms_norm_across_heads", rope_max_seq_len=32, ) torch.manual_seed(0) transformer_2 = WanTransformer3DModel( patch_size=(1, 2, 2), num_attention_heads=2, attention_head_dim=12, in_channels=36, out_channels=16, text_dim=32, freq_dim=256, ffn_dim=32, num_layers=2, cross_attn_norm=True, qk_norm="rms_norm_across_heads", rope_max_seq_len=32, ) components = { "transformer": transformer, "vae": vae, "scheduler": scheduler, "text_encoder": text_encoder, "tokenizer": tokenizer, "transformer_2": transformer_2, "image_encoder": None, "image_processor": None, "boundary_ratio": 0.875, } return components def get_dummy_inputs(self, device, seed=0): if str(device).startswith("mps"): generator = torch.manual_seed(seed) else: generator = torch.Generator(device=device).manual_seed(seed) image_height = 16 image_width = 16 image = Image.new("RGB", (image_width, image_height)) inputs = { "image": image, "prompt": "dance monkey", "negative_prompt": "negative", # TODO "height": image_height, "width": image_width, "generator": generator, "num_inference_steps": 2, "guidance_scale": 6.0, "num_frames": 9, "max_sequence_length": 16, "output_type": "pt", } return inputs def test_inference(self): device = "cpu" components = self.get_dummy_components() pipe = self.pipeline_class( **components, ) pipe.to(device) pipe.set_progress_bar_config(disable=None) inputs = self.get_dummy_inputs(device) video = pipe(**inputs).frames generated_video = video[0] self.assertEqual(generated_video.shape, (9, 3, 16, 16)) # fmt: off expected_slice = torch.tensor([0.4527, 0.4526, 0.4498, 0.4539, 0.4521, 0.4524, 0.4533, 0.4535, 0.5154, 0.5353, 0.5200, 0.5174, 0.5434, 0.5301, 0.5199, 0.5216]) # fmt: on generated_slice = generated_video.flatten() generated_slice = torch.cat([generated_slice[:8], generated_slice[-8:]]) self.assertTrue( torch.allclose(generated_slice, expected_slice, atol=1e-3), f"generated_slice: {generated_slice}, expected_slice: {expected_slice}", ) @unittest.skip("Test not supported") def test_attention_slicing_forward_pass(self): pass def test_save_load_optional_components(self, expected_max_difference=1e-4): optional_component = ["transformer", "image_encoder", "image_processor"] components = self.get_dummy_components() for component in optional_component: components[component] = None components["boundary_ratio"] = 1.0 # for wan 2.2 14B, transformer is not used when boundary_ratio is 1.0 pipe = self.pipeline_class(**components) for component in pipe.components.values(): if hasattr(component, "set_default_attn_processor"): component.set_default_attn_processor() pipe.to(torch_device) pipe.set_progress_bar_config(disable=None) generator_device = "cpu" inputs = self.get_dummy_inputs(generator_device) torch.manual_seed(0) output = pipe(**inputs)[0] with tempfile.TemporaryDirectory() as tmpdir: pipe.save_pretrained(tmpdir, safe_serialization=False) pipe_loaded = self.pipeline_class.from_pretrained(tmpdir) for component in pipe_loaded.components.values(): if hasattr(component, "set_default_attn_processor"): component.set_default_attn_processor() pipe_loaded.to(torch_device) pipe_loaded.set_progress_bar_config(disable=None) for component in optional_component: self.assertTrue( getattr(pipe_loaded, component) is None, f"`{component}` did not stay set to None after loading.", ) inputs = self.get_dummy_inputs(generator_device) torch.manual_seed(0) output_loaded = pipe_loaded(**inputs)[0] max_diff = np.abs(output.detach().cpu().numpy() - output_loaded.detach().cpu().numpy()).max() self.assertLess(max_diff, expected_max_difference) class Wan225BImageToVideoPipelineFastTests(PipelineTesterMixin, unittest.TestCase): pipeline_class = WanImageToVideoPipeline params = TEXT_TO_IMAGE_PARAMS - {"cross_attention_kwargs"} batch_params = TEXT_TO_IMAGE_BATCH_PARAMS image_params = TEXT_TO_IMAGE_IMAGE_PARAMS image_latents_params = TEXT_TO_IMAGE_IMAGE_PARAMS required_optional_params = frozenset( [ "num_inference_steps", "generator", "latents", "return_dict", "callback_on_step_end", "callback_on_step_end_tensor_inputs", ] ) test_xformers_attention = False supports_dduf = False def get_dummy_components(self): torch.manual_seed(0) vae = AutoencoderKLWan( base_dim=3, z_dim=48, in_channels=12, out_channels=12, is_residual=True, patch_size=2, latents_mean=[0.0] * 48, latents_std=[1.0] * 48, dim_mult=[1, 1, 1, 1], num_res_blocks=1, scale_factor_spatial=16, scale_factor_temporal=4, temperal_downsample=[False, True, True], ) torch.manual_seed(0) scheduler = UniPCMultistepScheduler(prediction_type="flow_prediction", use_flow_sigmas=True, flow_shift=3.0) config = AutoConfig.from_pretrained("hf-internal-testing/tiny-random-t5") text_encoder = T5EncoderModel(config) tokenizer = AutoTokenizer.from_pretrained("hf-internal-testing/tiny-random-t5") torch.manual_seed(0) transformer = WanTransformer3DModel( patch_size=(1, 2, 2), num_attention_heads=2, attention_head_dim=12, in_channels=48, out_channels=48, text_dim=32, freq_dim=256, ffn_dim=32, num_layers=2, cross_attn_norm=True, qk_norm="rms_norm_across_heads", rope_max_seq_len=32, ) components = { "transformer": transformer, "vae": vae, "scheduler": scheduler, "text_encoder": text_encoder, "tokenizer": tokenizer, "transformer_2": None, "image_encoder": None, "image_processor": None, "boundary_ratio": None, "expand_timesteps": True, } return components def get_dummy_inputs(self, device, seed=0): if str(device).startswith("mps"): generator = torch.manual_seed(seed) else: generator = torch.Generator(device=device).manual_seed(seed) image_height = 32 image_width = 32 image = Image.new("RGB", (image_width, image_height)) inputs = { "image": image, "prompt": "dance monkey", "negative_prompt": "negative", # TODO "height": image_height, "width": image_width, "generator": generator, "num_inference_steps": 2, "guidance_scale": 6.0, "num_frames": 9, "max_sequence_length": 16, "output_type": "pt", } return inputs def test_inference(self): device = "cpu" components = self.get_dummy_components() pipe = self.pipeline_class( **components, ) pipe.to(device) pipe.set_progress_bar_config(disable=None) inputs = self.get_dummy_inputs(device) video = pipe(**inputs).frames generated_video = video[0] self.assertEqual(generated_video.shape, (9, 3, 32, 32)) # fmt: off expected_slice = torch.tensor([[0.4833, 0.4305, 0.5100, 0.4299, 0.5056, 0.4298, 0.5052, 0.4332, 0.5550, 0.6092, 0.5536, 0.5928, 0.5199, 0.5864, 0.6705, 0.5493]]) # fmt: on generated_slice = generated_video.flatten() generated_slice = torch.cat([generated_slice[:8], generated_slice[-8:]]) self.assertTrue( torch.allclose(generated_slice, expected_slice, atol=1e-3), f"generated_slice: {generated_slice}, expected_slice: {expected_slice}", ) @unittest.skip("Test not supported") def test_attention_slicing_forward_pass(self): pass def test_components_function(self): init_components = self.get_dummy_components() init_components.pop("boundary_ratio") init_components.pop("expand_timesteps") pipe = self.pipeline_class(**init_components) self.assertTrue(hasattr(pipe, "components")) self.assertTrue(set(pipe.components.keys()) == set(init_components.keys())) def test_save_load_optional_components(self, expected_max_difference=1e-4): optional_component = ["transformer_2", "image_encoder", "image_processor"] components = self.get_dummy_components() for component in optional_component: components[component] = None pipe = self.pipeline_class(**components) for component in pipe.components.values(): if hasattr(component, "set_default_attn_processor"): component.set_default_attn_processor() pipe.to(torch_device) pipe.set_progress_bar_config(disable=None) generator_device = "cpu" inputs = self.get_dummy_inputs(generator_device) torch.manual_seed(0) output = pipe(**inputs)[0] with tempfile.TemporaryDirectory() as tmpdir: pipe.save_pretrained(tmpdir, safe_serialization=False) pipe_loaded = self.pipeline_class.from_pretrained(tmpdir) for component in pipe_loaded.components.values(): if hasattr(component, "set_default_attn_processor"): component.set_default_attn_processor() pipe_loaded.to(torch_device) pipe_loaded.set_progress_bar_config(disable=None) for component in optional_component: self.assertTrue( getattr(pipe_loaded, component) is None, f"`{component}` did not stay set to None after loading.", ) inputs = self.get_dummy_inputs(generator_device) torch.manual_seed(0) output_loaded = pipe_loaded(**inputs)[0] max_diff = np.abs(output.detach().cpu().numpy() - output_loaded.detach().cpu().numpy()).max() self.assertLess(max_diff, expected_max_difference) def test_inference_batch_single_identical(self): self._test_inference_batch_single_identical(expected_max_diff=2e-3) @unittest.skip("Test not supported") def test_callback_inputs(self): pass
{ "repo_id": "huggingface/diffusers", "file_path": "tests/pipelines/wan/test_wan_22_image_to_video.py", "license": "Apache License 2.0", "lines": 342, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
test
huggingface/diffusers:src/diffusers/models/autoencoders/autoencoder_kl_qwenimage.py
# Copyright 2025 The Qwen-Image Team, Wan Team and The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # # We gratefully acknowledge the Wan Team for their outstanding contributions. # QwenImageVAE is further fine-tuned from the Wan Video VAE to achieve improved performance. # For more information about the Wan VAE, please refer to: # - GitHub: https://github.com/Wan-Video/Wan2.1 # - Paper: https://huggingface.co/papers/2503.20314 import torch import torch.nn as nn import torch.nn.functional as F from ...configuration_utils import ConfigMixin, register_to_config from ...loaders import FromOriginalModelMixin from ...utils import logging from ...utils.accelerate_utils import apply_forward_hook from ..activations import get_activation from ..modeling_outputs import AutoencoderKLOutput from ..modeling_utils import ModelMixin from .vae import AutoencoderMixin, DecoderOutput, DiagonalGaussianDistribution logger = logging.get_logger(__name__) # pylint: disable=invalid-name CACHE_T = 2 class QwenImageCausalConv3d(nn.Conv3d): r""" A custom 3D causal convolution layer with feature caching support. This layer extends the standard Conv3D layer by ensuring causality in the time dimension and handling feature caching for efficient inference. Args: in_channels (int): Number of channels in the input image out_channels (int): Number of channels produced by the convolution kernel_size (int or tuple): Size of the convolving kernel stride (int or tuple, optional): Stride of the convolution. Default: 1 padding (int or tuple, optional): Zero-padding added to all three sides of the input. Default: 0 """ def __init__( self, in_channels: int, out_channels: int, kernel_size: int | tuple[int, int, int], stride: int | tuple[int, int, int] = 1, padding: int | tuple[int, int, int] = 0, ) -> None: super().__init__( in_channels=in_channels, out_channels=out_channels, kernel_size=kernel_size, stride=stride, padding=padding, ) # Set up causal padding self._padding = (self.padding[2], self.padding[2], self.padding[1], self.padding[1], 2 * self.padding[0], 0) self.padding = (0, 0, 0) def forward(self, x, cache_x=None): padding = list(self._padding) if cache_x is not None and self._padding[4] > 0: cache_x = cache_x.to(x.device) x = torch.cat([cache_x, x], dim=2) padding[4] -= cache_x.shape[2] x = F.pad(x, padding) return super().forward(x) class QwenImageRMS_norm(nn.Module): r""" A custom RMS normalization layer. Args: dim (int): The number of dimensions to normalize over. channel_first (bool, optional): Whether the input tensor has channels as the first dimension. Default is True. images (bool, optional): Whether the input represents image data. Default is True. bias (bool, optional): Whether to include a learnable bias term. Default is False. """ def __init__(self, dim: int, channel_first: bool = True, images: bool = True, bias: bool = False) -> None: super().__init__() broadcastable_dims = (1, 1, 1) if not images else (1, 1) shape = (dim, *broadcastable_dims) if channel_first else (dim,) self.channel_first = channel_first self.scale = dim**0.5 self.gamma = nn.Parameter(torch.ones(shape)) self.bias = nn.Parameter(torch.zeros(shape)) if bias else 0.0 def forward(self, x): return F.normalize(x, dim=(1 if self.channel_first else -1)) * self.scale * self.gamma + self.bias class QwenImageUpsample(nn.Upsample): r""" Perform upsampling while ensuring the output tensor has the same data type as the input. Args: x (torch.Tensor): Input tensor to be upsampled. Returns: torch.Tensor: Upsampled tensor with the same data type as the input. """ def forward(self, x): return super().forward(x.float()).type_as(x) class QwenImageResample(nn.Module): r""" A custom resampling module for 2D and 3D data. Args: dim (int): The number of input/output channels. mode (str): The resampling mode. Must be one of: - 'none': No resampling (identity operation). - 'upsample2d': 2D upsampling with nearest-exact interpolation and convolution. - 'upsample3d': 3D upsampling with nearest-exact interpolation, convolution, and causal 3D convolution. - 'downsample2d': 2D downsampling with zero-padding and convolution. - 'downsample3d': 3D downsampling with zero-padding, convolution, and causal 3D convolution. """ def __init__(self, dim: int, mode: str) -> None: super().__init__() self.dim = dim self.mode = mode # layers if mode == "upsample2d": self.resample = nn.Sequential( QwenImageUpsample(scale_factor=(2.0, 2.0), mode="nearest-exact"), nn.Conv2d(dim, dim // 2, 3, padding=1), ) elif mode == "upsample3d": self.resample = nn.Sequential( QwenImageUpsample(scale_factor=(2.0, 2.0), mode="nearest-exact"), nn.Conv2d(dim, dim // 2, 3, padding=1), ) self.time_conv = QwenImageCausalConv3d(dim, dim * 2, (3, 1, 1), padding=(1, 0, 0)) elif mode == "downsample2d": self.resample = nn.Sequential(nn.ZeroPad2d((0, 1, 0, 1)), nn.Conv2d(dim, dim, 3, stride=(2, 2))) elif mode == "downsample3d": self.resample = nn.Sequential(nn.ZeroPad2d((0, 1, 0, 1)), nn.Conv2d(dim, dim, 3, stride=(2, 2))) self.time_conv = QwenImageCausalConv3d(dim, dim, (3, 1, 1), stride=(2, 1, 1), padding=(0, 0, 0)) else: self.resample = nn.Identity() def forward(self, x, feat_cache=None, feat_idx=[0]): b, c, t, h, w = x.size() if self.mode == "upsample3d": if feat_cache is not None: idx = feat_idx[0] if feat_cache[idx] is None: feat_cache[idx] = "Rep" feat_idx[0] += 1 else: cache_x = x[:, :, -CACHE_T:, :, :].clone() if cache_x.shape[2] < 2 and feat_cache[idx] is not None and feat_cache[idx] != "Rep": # cache last frame of last two chunk cache_x = torch.cat( [feat_cache[idx][:, :, -1, :, :].unsqueeze(2).to(cache_x.device), cache_x], dim=2 ) if cache_x.shape[2] < 2 and feat_cache[idx] is not None and feat_cache[idx] == "Rep": cache_x = torch.cat([torch.zeros_like(cache_x).to(cache_x.device), cache_x], dim=2) if feat_cache[idx] == "Rep": x = self.time_conv(x) else: x = self.time_conv(x, feat_cache[idx]) feat_cache[idx] = cache_x feat_idx[0] += 1 x = x.reshape(b, 2, c, t, h, w) x = torch.stack((x[:, 0, :, :, :, :], x[:, 1, :, :, :, :]), 3) x = x.reshape(b, c, t * 2, h, w) t = x.shape[2] x = x.permute(0, 2, 1, 3, 4).reshape(b * t, c, h, w) x = self.resample(x) x = x.view(b, t, x.size(1), x.size(2), x.size(3)).permute(0, 2, 1, 3, 4) if self.mode == "downsample3d": if feat_cache is not None: idx = feat_idx[0] if feat_cache[idx] is None: feat_cache[idx] = x.clone() feat_idx[0] += 1 else: cache_x = x[:, :, -1:, :, :].clone() x = self.time_conv(torch.cat([feat_cache[idx][:, :, -1:, :, :], x], 2)) feat_cache[idx] = cache_x feat_idx[0] += 1 return x class QwenImageResidualBlock(nn.Module): r""" A custom residual block module. Args: in_dim (int): Number of input channels. out_dim (int): Number of output channels. dropout (float, optional): Dropout rate for the dropout layer. Default is 0.0. non_linearity (str, optional): Type of non-linearity to use. Default is "silu". """ def __init__( self, in_dim: int, out_dim: int, dropout: float = 0.0, non_linearity: str = "silu", ) -> None: super().__init__() self.in_dim = in_dim self.out_dim = out_dim self.nonlinearity = get_activation(non_linearity) # layers self.norm1 = QwenImageRMS_norm(in_dim, images=False) self.conv1 = QwenImageCausalConv3d(in_dim, out_dim, 3, padding=1) self.norm2 = QwenImageRMS_norm(out_dim, images=False) self.dropout = nn.Dropout(dropout) self.conv2 = QwenImageCausalConv3d(out_dim, out_dim, 3, padding=1) self.conv_shortcut = QwenImageCausalConv3d(in_dim, out_dim, 1) if in_dim != out_dim else nn.Identity() def forward(self, x, feat_cache=None, feat_idx=[0]): # Apply shortcut connection h = self.conv_shortcut(x) # First normalization and activation x = self.norm1(x) x = self.nonlinearity(x) if feat_cache is not None: idx = feat_idx[0] cache_x = x[:, :, -CACHE_T:, :, :].clone() if cache_x.shape[2] < 2 and feat_cache[idx] is not None: cache_x = torch.cat([feat_cache[idx][:, :, -1, :, :].unsqueeze(2).to(cache_x.device), cache_x], dim=2) x = self.conv1(x, feat_cache[idx]) feat_cache[idx] = cache_x feat_idx[0] += 1 else: x = self.conv1(x) # Second normalization and activation x = self.norm2(x) x = self.nonlinearity(x) # Dropout x = self.dropout(x) if feat_cache is not None: idx = feat_idx[0] cache_x = x[:, :, -CACHE_T:, :, :].clone() if cache_x.shape[2] < 2 and feat_cache[idx] is not None: cache_x = torch.cat([feat_cache[idx][:, :, -1, :, :].unsqueeze(2).to(cache_x.device), cache_x], dim=2) x = self.conv2(x, feat_cache[idx]) feat_cache[idx] = cache_x feat_idx[0] += 1 else: x = self.conv2(x) # Add residual connection return x + h class QwenImageAttentionBlock(nn.Module): r""" Causal self-attention with a single head. Args: dim (int): The number of channels in the input tensor. """ def __init__(self, dim): super().__init__() self.dim = dim # layers self.norm = QwenImageRMS_norm(dim) self.to_qkv = nn.Conv2d(dim, dim * 3, 1) self.proj = nn.Conv2d(dim, dim, 1) def forward(self, x): identity = x batch_size, channels, time, height, width = x.size() x = x.permute(0, 2, 1, 3, 4).reshape(batch_size * time, channels, height, width) x = self.norm(x) # compute query, key, value qkv = self.to_qkv(x) qkv = qkv.reshape(batch_size * time, 1, channels * 3, -1) qkv = qkv.permute(0, 1, 3, 2).contiguous() q, k, v = qkv.chunk(3, dim=-1) # apply attention x = F.scaled_dot_product_attention(q, k, v) x = x.squeeze(1).permute(0, 2, 1).reshape(batch_size * time, channels, height, width) # output projection x = self.proj(x) # Reshape back: [(b*t), c, h, w] -> [b, c, t, h, w] x = x.view(batch_size, time, channels, height, width) x = x.permute(0, 2, 1, 3, 4) return x + identity class QwenImageMidBlock(nn.Module): """ Middle block for QwenImageVAE encoder and decoder. Args: dim (int): Number of input/output channels. dropout (float): Dropout rate. non_linearity (str): Type of non-linearity to use. """ def __init__(self, dim: int, dropout: float = 0.0, non_linearity: str = "silu", num_layers: int = 1): super().__init__() self.dim = dim # Create the components resnets = [QwenImageResidualBlock(dim, dim, dropout, non_linearity)] attentions = [] for _ in range(num_layers): attentions.append(QwenImageAttentionBlock(dim)) resnets.append(QwenImageResidualBlock(dim, dim, dropout, non_linearity)) self.attentions = nn.ModuleList(attentions) self.resnets = nn.ModuleList(resnets) self.gradient_checkpointing = False def forward(self, x, feat_cache=None, feat_idx=[0]): # First residual block x = self.resnets[0](x, feat_cache, feat_idx) # Process through attention and residual blocks for attn, resnet in zip(self.attentions, self.resnets[1:]): if attn is not None: x = attn(x) x = resnet(x, feat_cache, feat_idx) return x class QwenImageEncoder3d(nn.Module): r""" A 3D encoder module. Args: dim (int): The base number of channels in the first layer. z_dim (int): The dimensionality of the latent space. dim_mult (list of int): Multipliers for the number of channels in each block. num_res_blocks (int): Number of residual blocks in each block. attn_scales (list of float): Scales at which to apply attention mechanisms. temperal_downsample (list of bool): Whether to downsample temporally in each block. dropout (float): Dropout rate for the dropout layers. non_linearity (str): Type of non-linearity to use. """ def __init__( self, dim=128, z_dim=4, dim_mult=[1, 2, 4, 4], num_res_blocks=2, attn_scales=[], temperal_downsample=[True, True, False], dropout=0.0, input_channels=3, non_linearity: str = "silu", ): super().__init__() self.dim = dim self.z_dim = z_dim self.dim_mult = dim_mult self.num_res_blocks = num_res_blocks self.attn_scales = attn_scales self.temperal_downsample = temperal_downsample self.nonlinearity = get_activation(non_linearity) # dimensions dims = [dim * u for u in [1] + dim_mult] scale = 1.0 # init block self.conv_in = QwenImageCausalConv3d(input_channels, dims[0], 3, padding=1) # downsample blocks self.down_blocks = nn.ModuleList([]) for i, (in_dim, out_dim) in enumerate(zip(dims[:-1], dims[1:])): # residual (+attention) blocks for _ in range(num_res_blocks): self.down_blocks.append(QwenImageResidualBlock(in_dim, out_dim, dropout)) if scale in attn_scales: self.down_blocks.append(QwenImageAttentionBlock(out_dim)) in_dim = out_dim # downsample block if i != len(dim_mult) - 1: mode = "downsample3d" if temperal_downsample[i] else "downsample2d" self.down_blocks.append(QwenImageResample(out_dim, mode=mode)) scale /= 2.0 # middle blocks self.mid_block = QwenImageMidBlock(out_dim, dropout, non_linearity, num_layers=1) # output blocks self.norm_out = QwenImageRMS_norm(out_dim, images=False) self.conv_out = QwenImageCausalConv3d(out_dim, z_dim, 3, padding=1) self.gradient_checkpointing = False def forward(self, x, feat_cache=None, feat_idx=[0]): if feat_cache is not None: idx = feat_idx[0] cache_x = x[:, :, -CACHE_T:, :, :].clone() if cache_x.shape[2] < 2 and feat_cache[idx] is not None: # cache last frame of last two chunk cache_x = torch.cat([feat_cache[idx][:, :, -1, :, :].unsqueeze(2).to(cache_x.device), cache_x], dim=2) x = self.conv_in(x, feat_cache[idx]) feat_cache[idx] = cache_x feat_idx[0] += 1 else: x = self.conv_in(x) ## downsamples for layer in self.down_blocks: if feat_cache is not None: x = layer(x, feat_cache, feat_idx) else: x = layer(x) ## middle x = self.mid_block(x, feat_cache, feat_idx) ## head x = self.norm_out(x) x = self.nonlinearity(x) if feat_cache is not None: idx = feat_idx[0] cache_x = x[:, :, -CACHE_T:, :, :].clone() if cache_x.shape[2] < 2 and feat_cache[idx] is not None: # cache last frame of last two chunk cache_x = torch.cat([feat_cache[idx][:, :, -1, :, :].unsqueeze(2).to(cache_x.device), cache_x], dim=2) x = self.conv_out(x, feat_cache[idx]) feat_cache[idx] = cache_x feat_idx[0] += 1 else: x = self.conv_out(x) return x class QwenImageUpBlock(nn.Module): """ A block that handles upsampling for the QwenImageVAE decoder. Args: in_dim (int): Input dimension out_dim (int): Output dimension num_res_blocks (int): Number of residual blocks dropout (float): Dropout rate upsample_mode (str, optional): Mode for upsampling ('upsample2d' or 'upsample3d') non_linearity (str): Type of non-linearity to use """ def __init__( self, in_dim: int, out_dim: int, num_res_blocks: int, dropout: float = 0.0, upsample_mode: str | None = None, non_linearity: str = "silu", ): super().__init__() self.in_dim = in_dim self.out_dim = out_dim # Create layers list resnets = [] # Add residual blocks and attention if needed current_dim = in_dim for _ in range(num_res_blocks + 1): resnets.append(QwenImageResidualBlock(current_dim, out_dim, dropout, non_linearity)) current_dim = out_dim self.resnets = nn.ModuleList(resnets) # Add upsampling layer if needed self.upsamplers = None if upsample_mode is not None: self.upsamplers = nn.ModuleList([QwenImageResample(out_dim, mode=upsample_mode)]) self.gradient_checkpointing = False def forward(self, x, feat_cache=None, feat_idx=[0]): """ Forward pass through the upsampling block. Args: x (torch.Tensor): Input tensor feat_cache (list, optional): Feature cache for causal convolutions feat_idx (list, optional): Feature index for cache management Returns: torch.Tensor: Output tensor """ for resnet in self.resnets: if feat_cache is not None: x = resnet(x, feat_cache, feat_idx) else: x = resnet(x) if self.upsamplers is not None: if feat_cache is not None: x = self.upsamplers[0](x, feat_cache, feat_idx) else: x = self.upsamplers[0](x) return x class QwenImageDecoder3d(nn.Module): r""" A 3D decoder module. Args: dim (int): The base number of channels in the first layer. z_dim (int): The dimensionality of the latent space. dim_mult (list of int): Multipliers for the number of channels in each block. num_res_blocks (int): Number of residual blocks in each block. attn_scales (list of float): Scales at which to apply attention mechanisms. temperal_upsample (list of bool): Whether to upsample temporally in each block. dropout (float): Dropout rate for the dropout layers. non_linearity (str): Type of non-linearity to use. """ def __init__( self, dim=128, z_dim=4, dim_mult=[1, 2, 4, 4], num_res_blocks=2, attn_scales=[], temperal_upsample=[False, True, True], dropout=0.0, input_channels=3, non_linearity: str = "silu", ): super().__init__() self.dim = dim self.z_dim = z_dim self.dim_mult = dim_mult self.num_res_blocks = num_res_blocks self.attn_scales = attn_scales self.temperal_upsample = temperal_upsample self.nonlinearity = get_activation(non_linearity) # dimensions dims = [dim * u for u in [dim_mult[-1]] + dim_mult[::-1]] scale = 1.0 / 2 ** (len(dim_mult) - 2) # init block self.conv_in = QwenImageCausalConv3d(z_dim, dims[0], 3, padding=1) # middle blocks self.mid_block = QwenImageMidBlock(dims[0], dropout, non_linearity, num_layers=1) # upsample blocks self.up_blocks = nn.ModuleList([]) for i, (in_dim, out_dim) in enumerate(zip(dims[:-1], dims[1:])): # residual (+attention) blocks if i > 0: in_dim = in_dim // 2 # Determine if we need upsampling upsample_mode = None if i != len(dim_mult) - 1: upsample_mode = "upsample3d" if temperal_upsample[i] else "upsample2d" # Create and add the upsampling block up_block = QwenImageUpBlock( in_dim=in_dim, out_dim=out_dim, num_res_blocks=num_res_blocks, dropout=dropout, upsample_mode=upsample_mode, non_linearity=non_linearity, ) self.up_blocks.append(up_block) # Update scale for next iteration if upsample_mode is not None: scale *= 2.0 # output blocks self.norm_out = QwenImageRMS_norm(out_dim, images=False) self.conv_out = QwenImageCausalConv3d(out_dim, input_channels, 3, padding=1) self.gradient_checkpointing = False def forward(self, x, feat_cache=None, feat_idx=[0]): ## conv1 if feat_cache is not None: idx = feat_idx[0] cache_x = x[:, :, -CACHE_T:, :, :].clone() if cache_x.shape[2] < 2 and feat_cache[idx] is not None: # cache last frame of last two chunk cache_x = torch.cat([feat_cache[idx][:, :, -1, :, :].unsqueeze(2).to(cache_x.device), cache_x], dim=2) x = self.conv_in(x, feat_cache[idx]) feat_cache[idx] = cache_x feat_idx[0] += 1 else: x = self.conv_in(x) ## middle x = self.mid_block(x, feat_cache, feat_idx) ## upsamples for up_block in self.up_blocks: x = up_block(x, feat_cache, feat_idx) ## head x = self.norm_out(x) x = self.nonlinearity(x) if feat_cache is not None: idx = feat_idx[0] cache_x = x[:, :, -CACHE_T:, :, :].clone() if cache_x.shape[2] < 2 and feat_cache[idx] is not None: # cache last frame of last two chunk cache_x = torch.cat([feat_cache[idx][:, :, -1, :, :].unsqueeze(2).to(cache_x.device), cache_x], dim=2) x = self.conv_out(x, feat_cache[idx]) feat_cache[idx] = cache_x feat_idx[0] += 1 else: x = self.conv_out(x) return x class AutoencoderKLQwenImage(ModelMixin, AutoencoderMixin, ConfigMixin, FromOriginalModelMixin): r""" A VAE model with KL loss for encoding videos into latents and decoding latent representations into videos. This model inherits from [`ModelMixin`]. Check the superclass documentation for it's generic methods implemented for all models (such as downloading or saving). """ _supports_gradient_checkpointing = False # fmt: off @register_to_config def __init__( self, base_dim: int = 96, z_dim: int = 16, dim_mult: list[int] = [1, 2, 4, 4], num_res_blocks: int = 2, attn_scales: list[float] = [], temperal_downsample: list[bool] = [False, True, True], dropout: float = 0.0, input_channels: int = 3, latents_mean: list[float] = [-0.7571, -0.7089, -0.9113, 0.1075, -0.1745, 0.9653, -0.1517, 1.5508, 0.4134, -0.0715, 0.5517, -0.3632, -0.1922, -0.9497, 0.2503, -0.2921], latents_std: list[float] = [2.8184, 1.4541, 2.3275, 2.6558, 1.2196, 1.7708, 2.6052, 2.0743, 3.2687, 2.1526, 2.8652, 1.5579, 1.6382, 1.1253, 2.8251, 1.9160], ) -> None: # fmt: on super().__init__() self.z_dim = z_dim self.temperal_downsample = temperal_downsample self.temperal_upsample = temperal_downsample[::-1] self.encoder = QwenImageEncoder3d( base_dim, z_dim * 2, dim_mult, num_res_blocks, attn_scales, self.temperal_downsample, dropout, input_channels ) self.quant_conv = QwenImageCausalConv3d(z_dim * 2, z_dim * 2, 1) self.post_quant_conv = QwenImageCausalConv3d(z_dim, z_dim, 1) self.decoder = QwenImageDecoder3d( base_dim, z_dim, dim_mult, num_res_blocks, attn_scales, self.temperal_upsample, dropout, input_channels ) self.spatial_compression_ratio = 2 ** len(self.temperal_downsample) # When decoding a batch of video latents at a time, one can save memory by slicing across the batch dimension # to perform decoding of a single video latent at a time. self.use_slicing = False # When decoding spatially large video latents, the memory requirement is very high. By breaking the video latent # frames spatially into smaller tiles and performing multiple forward passes for decoding, and then blending the # intermediate tiles together, the memory requirement can be lowered. self.use_tiling = False # The minimal tile height and width for spatial tiling to be used self.tile_sample_min_height = 256 self.tile_sample_min_width = 256 # The minimal distance between two spatial tiles self.tile_sample_stride_height = 192 self.tile_sample_stride_width = 192 # Precompute and cache conv counts for encoder and decoder for clear_cache speedup self._cached_conv_counts = { "decoder": sum(isinstance(m, QwenImageCausalConv3d) for m in self.decoder.modules()) if self.decoder is not None else 0, "encoder": sum(isinstance(m, QwenImageCausalConv3d) for m in self.encoder.modules()) if self.encoder is not None else 0, } def enable_tiling( self, tile_sample_min_height: int | None = None, tile_sample_min_width: int | None = None, tile_sample_stride_height: float | None = None, tile_sample_stride_width: float | None = None, ) -> None: r""" Enable tiled VAE decoding. When this option is enabled, the VAE will split the input tensor into tiles to compute decoding and encoding in several steps. This is useful for saving a large amount of memory and to allow processing larger images. Args: tile_sample_min_height (`int`, *optional*): The minimum height required for a sample to be separated into tiles across the height dimension. tile_sample_min_width (`int`, *optional*): The minimum width required for a sample to be separated into tiles across the width dimension. tile_sample_stride_height (`int`, *optional*): The minimum amount of overlap between two consecutive vertical tiles. This is to ensure that there are no tiling artifacts produced across the height dimension. tile_sample_stride_width (`int`, *optional*): The stride between two consecutive horizontal tiles. This is to ensure that there are no tiling artifacts produced across the width dimension. """ self.use_tiling = True self.tile_sample_min_height = tile_sample_min_height or self.tile_sample_min_height self.tile_sample_min_width = tile_sample_min_width or self.tile_sample_min_width self.tile_sample_stride_height = tile_sample_stride_height or self.tile_sample_stride_height self.tile_sample_stride_width = tile_sample_stride_width or self.tile_sample_stride_width def clear_cache(self): def _count_conv3d(model): count = 0 for m in model.modules(): if isinstance(m, QwenImageCausalConv3d): count += 1 return count self._conv_num = _count_conv3d(self.decoder) self._conv_idx = [0] self._feat_map = [None] * self._conv_num # cache encode self._enc_conv_num = _count_conv3d(self.encoder) self._enc_conv_idx = [0] self._enc_feat_map = [None] * self._enc_conv_num def _encode(self, x: torch.Tensor): _, _, num_frame, height, width = x.shape if self.use_tiling and (width > self.tile_sample_min_width or height > self.tile_sample_min_height): return self.tiled_encode(x) self.clear_cache() iter_ = 1 + (num_frame - 1) // 4 for i in range(iter_): self._enc_conv_idx = [0] if i == 0: out = self.encoder(x[:, :, :1, :, :], feat_cache=self._enc_feat_map, feat_idx=self._enc_conv_idx) else: out_ = self.encoder( x[:, :, 1 + 4 * (i - 1) : 1 + 4 * i, :, :], feat_cache=self._enc_feat_map, feat_idx=self._enc_conv_idx, ) out = torch.cat([out, out_], 2) enc = self.quant_conv(out) self.clear_cache() return enc @apply_forward_hook def encode( self, x: torch.Tensor, return_dict: bool = True ) -> AutoencoderKLOutput | tuple[DiagonalGaussianDistribution]: r""" Encode a batch of images into latents. Args: x (`torch.Tensor`): Input batch of images. return_dict (`bool`, *optional*, defaults to `True`): Whether to return a [`~models.autoencoder_kl.AutoencoderKLOutput`] instead of a plain tuple. Returns: The latent representations of the encoded videos. If `return_dict` is True, a [`~models.autoencoder_kl.AutoencoderKLOutput`] is returned, otherwise a plain `tuple` is returned. """ if self.use_slicing and x.shape[0] > 1: encoded_slices = [self._encode(x_slice) for x_slice in x.split(1)] h = torch.cat(encoded_slices) else: h = self._encode(x) posterior = DiagonalGaussianDistribution(h) if not return_dict: return (posterior,) return AutoencoderKLOutput(latent_dist=posterior) def _decode(self, z: torch.Tensor, return_dict: bool = True): _, _, num_frame, height, width = z.shape tile_latent_min_height = self.tile_sample_min_height // self.spatial_compression_ratio tile_latent_min_width = self.tile_sample_min_width // self.spatial_compression_ratio if self.use_tiling and (width > tile_latent_min_width or height > tile_latent_min_height): return self.tiled_decode(z, return_dict=return_dict) self.clear_cache() x = self.post_quant_conv(z) for i in range(num_frame): self._conv_idx = [0] if i == 0: out = self.decoder(x[:, :, i : i + 1, :, :], feat_cache=self._feat_map, feat_idx=self._conv_idx) else: out_ = self.decoder(x[:, :, i : i + 1, :, :], feat_cache=self._feat_map, feat_idx=self._conv_idx) out = torch.cat([out, out_], 2) out = torch.clamp(out, min=-1.0, max=1.0) self.clear_cache() if not return_dict: return (out,) return DecoderOutput(sample=out) @apply_forward_hook def decode(self, z: torch.Tensor, return_dict: bool = True) -> DecoderOutput | torch.Tensor: r""" Decode a batch of images. Args: z (`torch.Tensor`): Input batch of latent vectors. return_dict (`bool`, *optional*, defaults to `True`): Whether to return a [`~models.vae.DecoderOutput`] instead of a plain tuple. Returns: [`~models.vae.DecoderOutput`] or `tuple`: If return_dict is True, a [`~models.vae.DecoderOutput`] is returned, otherwise a plain `tuple` is returned. """ if self.use_slicing and z.shape[0] > 1: decoded_slices = [self._decode(z_slice).sample for z_slice in z.split(1)] decoded = torch.cat(decoded_slices) else: decoded = self._decode(z).sample if not return_dict: return (decoded,) return DecoderOutput(sample=decoded) def blend_v(self, a: torch.Tensor, b: torch.Tensor, blend_extent: int) -> torch.Tensor: blend_extent = min(a.shape[-2], b.shape[-2], blend_extent) for y in range(blend_extent): b[:, :, :, y, :] = a[:, :, :, -blend_extent + y, :] * (1 - y / blend_extent) + b[:, :, :, y, :] * ( y / blend_extent ) return b def blend_h(self, a: torch.Tensor, b: torch.Tensor, blend_extent: int) -> torch.Tensor: blend_extent = min(a.shape[-1], b.shape[-1], blend_extent) for x in range(blend_extent): b[:, :, :, :, x] = a[:, :, :, :, -blend_extent + x] * (1 - x / blend_extent) + b[:, :, :, :, x] * ( x / blend_extent ) return b def tiled_encode(self, x: torch.Tensor) -> AutoencoderKLOutput: r"""Encode a batch of images using a tiled encoder. Args: x (`torch.Tensor`): Input batch of videos. Returns: `torch.Tensor`: The latent representation of the encoded videos. """ _, _, num_frames, height, width = x.shape latent_height = height // self.spatial_compression_ratio latent_width = width // self.spatial_compression_ratio tile_latent_min_height = self.tile_sample_min_height // self.spatial_compression_ratio tile_latent_min_width = self.tile_sample_min_width // self.spatial_compression_ratio tile_latent_stride_height = self.tile_sample_stride_height // self.spatial_compression_ratio tile_latent_stride_width = self.tile_sample_stride_width // self.spatial_compression_ratio blend_height = tile_latent_min_height - tile_latent_stride_height blend_width = tile_latent_min_width - tile_latent_stride_width # Split x into overlapping tiles and encode them separately. # The tiles have an overlap to avoid seams between tiles. rows = [] for i in range(0, height, self.tile_sample_stride_height): row = [] for j in range(0, width, self.tile_sample_stride_width): self.clear_cache() time = [] frame_range = 1 + (num_frames - 1) // 4 for k in range(frame_range): self._enc_conv_idx = [0] if k == 0: tile = x[:, :, :1, i : i + self.tile_sample_min_height, j : j + self.tile_sample_min_width] else: tile = x[ :, :, 1 + 4 * (k - 1) : 1 + 4 * k, i : i + self.tile_sample_min_height, j : j + self.tile_sample_min_width, ] tile = self.encoder(tile, feat_cache=self._enc_feat_map, feat_idx=self._enc_conv_idx) tile = self.quant_conv(tile) time.append(tile) row.append(torch.cat(time, dim=2)) rows.append(row) self.clear_cache() result_rows = [] for i, row in enumerate(rows): result_row = [] for j, tile in enumerate(row): # blend the above tile and the left tile # to the current tile and add the current tile to the result row if i > 0: tile = self.blend_v(rows[i - 1][j], tile, blend_height) if j > 0: tile = self.blend_h(row[j - 1], tile, blend_width) result_row.append(tile[:, :, :, :tile_latent_stride_height, :tile_latent_stride_width]) result_rows.append(torch.cat(result_row, dim=-1)) enc = torch.cat(result_rows, dim=3)[:, :, :, :latent_height, :latent_width] return enc def tiled_decode(self, z: torch.Tensor, return_dict: bool = True) -> DecoderOutput | torch.Tensor: r""" Decode a batch of images using a tiled decoder. Args: z (`torch.Tensor`): Input batch of latent vectors. return_dict (`bool`, *optional*, defaults to `True`): Whether or not to return a [`~models.vae.DecoderOutput`] instead of a plain tuple. Returns: [`~models.vae.DecoderOutput`] or `tuple`: If return_dict is True, a [`~models.vae.DecoderOutput`] is returned, otherwise a plain `tuple` is returned. """ _, _, num_frames, height, width = z.shape sample_height = height * self.spatial_compression_ratio sample_width = width * self.spatial_compression_ratio tile_latent_min_height = self.tile_sample_min_height // self.spatial_compression_ratio tile_latent_min_width = self.tile_sample_min_width // self.spatial_compression_ratio tile_latent_stride_height = self.tile_sample_stride_height // self.spatial_compression_ratio tile_latent_stride_width = self.tile_sample_stride_width // self.spatial_compression_ratio blend_height = self.tile_sample_min_height - self.tile_sample_stride_height blend_width = self.tile_sample_min_width - self.tile_sample_stride_width # Split z into overlapping tiles and decode them separately. # The tiles have an overlap to avoid seams between tiles. rows = [] for i in range(0, height, tile_latent_stride_height): row = [] for j in range(0, width, tile_latent_stride_width): self.clear_cache() time = [] for k in range(num_frames): self._conv_idx = [0] tile = z[:, :, k : k + 1, i : i + tile_latent_min_height, j : j + tile_latent_min_width] tile = self.post_quant_conv(tile) decoded = self.decoder(tile, feat_cache=self._feat_map, feat_idx=self._conv_idx) time.append(decoded) row.append(torch.cat(time, dim=2)) rows.append(row) self.clear_cache() result_rows = [] for i, row in enumerate(rows): result_row = [] for j, tile in enumerate(row): # blend the above tile and the left tile # to the current tile and add the current tile to the result row if i > 0: tile = self.blend_v(rows[i - 1][j], tile, blend_height) if j > 0: tile = self.blend_h(row[j - 1], tile, blend_width) result_row.append(tile[:, :, :, : self.tile_sample_stride_height, : self.tile_sample_stride_width]) result_rows.append(torch.cat(result_row, dim=-1)) dec = torch.cat(result_rows, dim=3)[:, :, :, :sample_height, :sample_width] if not return_dict: return (dec,) return DecoderOutput(sample=dec) def forward( self, sample: torch.Tensor, sample_posterior: bool = False, return_dict: bool = True, generator: torch.Generator | None = None, ) -> DecoderOutput | torch.Tensor: """ Args: sample (`torch.Tensor`): Input sample. return_dict (`bool`, *optional*, defaults to `True`): Whether or not to return a [`DecoderOutput`] instead of a plain tuple. """ x = sample posterior = self.encode(x).latent_dist if sample_posterior: z = posterior.sample(generator=generator) else: z = posterior.mode() dec = self.decode(z, return_dict=return_dict) return dec
{ "repo_id": "huggingface/diffusers", "file_path": "src/diffusers/models/autoencoders/autoencoder_kl_qwenimage.py", "license": "Apache License 2.0", "lines": 879, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
huggingface/diffusers:src/diffusers/models/transformers/transformer_qwenimage.py
# Copyright 2025 Qwen-Image Team, The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import functools import math from math import prod from typing import Any import numpy as np import torch import torch.nn as nn import torch.nn.functional as F from ...configuration_utils import ConfigMixin, register_to_config from ...loaders import FromOriginalModelMixin, PeftAdapterMixin from ...utils import apply_lora_scale, deprecate, logging from ...utils.torch_utils import maybe_allow_in_graph from .._modeling_parallel import ContextParallelInput, ContextParallelOutput from ..attention import AttentionMixin, FeedForward from ..attention_dispatch import dispatch_attention_fn from ..attention_processor import Attention from ..cache_utils import CacheMixin from ..embeddings import TimestepEmbedding, Timesteps from ..modeling_outputs import Transformer2DModelOutput from ..modeling_utils import ModelMixin from ..normalization import AdaLayerNormContinuous, RMSNorm logger = logging.get_logger(__name__) # pylint: disable=invalid-name def get_timestep_embedding( timesteps: torch.Tensor, embedding_dim: int, flip_sin_to_cos: bool = False, downscale_freq_shift: float = 1, scale: float = 1, max_period: int = 10000, ) -> torch.Tensor: """ This matches the implementation in Denoising Diffusion Probabilistic Models: Create sinusoidal timestep embeddings. Args timesteps (torch.Tensor): a 1-D Tensor of N indices, one per batch element. These may be fractional. embedding_dim (int): the dimension of the output. flip_sin_to_cos (bool): Whether the embedding order should be `cos, sin` (if True) or `sin, cos` (if False) downscale_freq_shift (float): Controls the delta between frequencies between dimensions scale (float): Scaling factor applied to the embeddings. max_period (int): Controls the maximum frequency of the embeddings Returns torch.Tensor: an [N x dim] Tensor of positional embeddings. """ assert len(timesteps.shape) == 1, "Timesteps should be a 1d-array" half_dim = embedding_dim // 2 exponent = -math.log(max_period) * torch.arange( start=0, end=half_dim, dtype=torch.float32, device=timesteps.device ) exponent = exponent / (half_dim - downscale_freq_shift) emb = torch.exp(exponent).to(timesteps.dtype) emb = timesteps[:, None].float() * emb[None, :] # scale embeddings emb = scale * emb # concat sine and cosine embeddings emb = torch.cat([torch.sin(emb), torch.cos(emb)], dim=-1) # flip sine and cosine embeddings if flip_sin_to_cos: emb = torch.cat([emb[:, half_dim:], emb[:, :half_dim]], dim=-1) # zero pad if embedding_dim % 2 == 1: emb = torch.nn.functional.pad(emb, (0, 1, 0, 0)) return emb def apply_rotary_emb_qwen( x: torch.Tensor, freqs_cis: torch.Tensor | tuple[torch.Tensor], use_real: bool = True, use_real_unbind_dim: int = -1, ) -> tuple[torch.Tensor, torch.Tensor]: """ Apply rotary embeddings to input tensors using the given frequency tensor. This function applies rotary embeddings to the given query or key 'x' tensors using the provided frequency tensor 'freqs_cis'. The input tensors are reshaped as complex numbers, and the frequency tensor is reshaped for broadcasting compatibility. The resulting tensors contain rotary embeddings and are returned as real tensors. Args: x (`torch.Tensor`): Query or key tensor to apply rotary embeddings. [B, S, H, D] xk (torch.Tensor): Key tensor to apply freqs_cis (`tuple[torch.Tensor]`): Precomputed frequency tensor for complex exponentials. ([S, D], [S, D],) Returns: tuple[torch.Tensor, torch.Tensor]: tuple of modified query tensor and key tensor with rotary embeddings. """ if use_real: cos, sin = freqs_cis # [S, D] cos = cos[None, None] sin = sin[None, None] cos, sin = cos.to(x.device), sin.to(x.device) if use_real_unbind_dim == -1: # Used for flux, cogvideox, hunyuan-dit x_real, x_imag = x.reshape(*x.shape[:-1], -1, 2).unbind(-1) # [B, S, H, D//2] x_rotated = torch.stack([-x_imag, x_real], dim=-1).flatten(3) elif use_real_unbind_dim == -2: # Used for Stable Audio, OmniGen, CogView4 and Cosmos x_real, x_imag = x.reshape(*x.shape[:-1], 2, -1).unbind(-2) # [B, S, H, D//2] x_rotated = torch.cat([-x_imag, x_real], dim=-1) else: raise ValueError(f"`use_real_unbind_dim={use_real_unbind_dim}` but should be -1 or -2.") out = (x.float() * cos + x_rotated.float() * sin).to(x.dtype) return out else: x_rotated = torch.view_as_complex(x.float().reshape(*x.shape[:-1], -1, 2)) freqs_cis = freqs_cis.unsqueeze(1) x_out = torch.view_as_real(x_rotated * freqs_cis).flatten(3) return x_out.type_as(x) def compute_text_seq_len_from_mask( encoder_hidden_states: torch.Tensor, encoder_hidden_states_mask: torch.Tensor | None ) -> tuple[int, torch.Tensor | None, torch.Tensor | None]: """ Compute text sequence length without assuming contiguous masks. Returns length for RoPE and a normalized bool mask. """ batch_size, text_seq_len = encoder_hidden_states.shape[:2] if encoder_hidden_states_mask is None: return text_seq_len, None, None if encoder_hidden_states_mask.shape[:2] != (batch_size, text_seq_len): raise ValueError( f"`encoder_hidden_states_mask` shape {encoder_hidden_states_mask.shape} must match " f"(batch_size, text_seq_len)=({batch_size}, {text_seq_len})." ) if encoder_hidden_states_mask.dtype != torch.bool: encoder_hidden_states_mask = encoder_hidden_states_mask.to(torch.bool) position_ids = torch.arange(text_seq_len, device=encoder_hidden_states.device, dtype=torch.long) active_positions = torch.where(encoder_hidden_states_mask, position_ids, position_ids.new_zeros(())) has_active = encoder_hidden_states_mask.any(dim=1) per_sample_len = torch.where( has_active, active_positions.max(dim=1).values + 1, torch.as_tensor(text_seq_len, device=encoder_hidden_states.device), ) return text_seq_len, per_sample_len, encoder_hidden_states_mask class QwenTimestepProjEmbeddings(nn.Module): def __init__(self, embedding_dim, use_additional_t_cond=False): super().__init__() self.time_proj = Timesteps(num_channels=256, flip_sin_to_cos=True, downscale_freq_shift=0, scale=1000) self.timestep_embedder = TimestepEmbedding(in_channels=256, time_embed_dim=embedding_dim) self.use_additional_t_cond = use_additional_t_cond if use_additional_t_cond: self.addition_t_embedding = nn.Embedding(2, embedding_dim) def forward(self, timestep, hidden_states, addition_t_cond=None): timesteps_proj = self.time_proj(timestep) timesteps_emb = self.timestep_embedder(timesteps_proj.to(dtype=hidden_states.dtype)) # (N, D) conditioning = timesteps_emb if self.use_additional_t_cond: if addition_t_cond is None: raise ValueError("When additional_t_cond is True, addition_t_cond must be provided.") addition_t_emb = self.addition_t_embedding(addition_t_cond) addition_t_emb = addition_t_emb.to(dtype=hidden_states.dtype) conditioning = conditioning + addition_t_emb return conditioning class QwenEmbedRope(nn.Module): def __init__(self, theta: int, axes_dim: list[int], scale_rope=False): super().__init__() self.theta = theta self.axes_dim = axes_dim pos_index = torch.arange(4096) neg_index = torch.arange(4096).flip(0) * -1 - 1 self.pos_freqs = torch.cat( [ self.rope_params(pos_index, self.axes_dim[0], self.theta), self.rope_params(pos_index, self.axes_dim[1], self.theta), self.rope_params(pos_index, self.axes_dim[2], self.theta), ], dim=1, ) self.neg_freqs = torch.cat( [ self.rope_params(neg_index, self.axes_dim[0], self.theta), self.rope_params(neg_index, self.axes_dim[1], self.theta), self.rope_params(neg_index, self.axes_dim[2], self.theta), ], dim=1, ) # DO NOT USING REGISTER BUFFER HERE, IT WILL CAUSE COMPLEX NUMBERS LOSE ITS IMAGINARY PART self.scale_rope = scale_rope def rope_params(self, index, dim, theta=10000): """ Args: index: [0, 1, 2, 3] 1D Tensor representing the position index of the token """ assert dim % 2 == 0 freqs = torch.outer(index, 1.0 / torch.pow(theta, torch.arange(0, dim, 2).to(torch.float32).div(dim))) freqs = torch.polar(torch.ones_like(freqs), freqs) return freqs def forward( self, video_fhw: tuple[int, int, int, list[tuple[int, int, int]]], txt_seq_lens: list[int] | None = None, device: torch.device = None, max_txt_seq_len: int | torch.Tensor | None = None, ) -> tuple[torch.Tensor, torch.Tensor]: """ Args: video_fhw (`tuple[int, int, int]` or `list[tuple[int, int, int]]`): A list of 3 integers [frame, height, width] representing the shape of the video. txt_seq_lens (`list[int]`, *optional*, **Deprecated**): Deprecated parameter. Use `max_txt_seq_len` instead. If provided, the maximum value will be used. device: (`torch.device`, *optional*): The device on which to perform the RoPE computation. max_txt_seq_len (`int` or `torch.Tensor`, *optional*): The maximum text sequence length for RoPE computation. This should match the encoder hidden states sequence length. Can be either an int or a scalar tensor (for torch.compile compatibility). """ # Handle deprecated txt_seq_lens parameter if txt_seq_lens is not None: deprecate( "txt_seq_lens", "0.39.0", "Passing `txt_seq_lens` is deprecated and will be removed in version 0.39.0. " "Please use `max_txt_seq_len` instead. " "The new parameter accepts a single int or tensor value representing the maximum text sequence length.", standard_warn=False, ) if max_txt_seq_len is None: # Use max of txt_seq_lens for backward compatibility max_txt_seq_len = max(txt_seq_lens) if isinstance(txt_seq_lens, list) else txt_seq_lens if max_txt_seq_len is None: raise ValueError("Either `max_txt_seq_len` or `txt_seq_lens` (deprecated) must be provided.") # Validate batch inference with variable-sized images if isinstance(video_fhw, list) and len(video_fhw) > 1: # Check if all instances have the same size first_fhw = video_fhw[0] if not all(fhw == first_fhw for fhw in video_fhw): logger.warning( "Batch inference with variable-sized images is not currently supported in QwenEmbedRope. " "All images in the batch should have the same dimensions (frame, height, width). " f"Detected sizes: {video_fhw}. Using the first image's dimensions {first_fhw} " "for RoPE computation, which may lead to incorrect results for other images in the batch." ) if isinstance(video_fhw, list): video_fhw = video_fhw[0] if not isinstance(video_fhw, list): video_fhw = [video_fhw] vid_freqs = [] max_vid_index = 0 for idx, fhw in enumerate(video_fhw): frame, height, width = fhw # RoPE frequencies are cached via a lru_cache decorator on _compute_video_freqs video_freq = self._compute_video_freqs(frame, height, width, idx, device) vid_freqs.append(video_freq) if self.scale_rope: max_vid_index = max(height // 2, width // 2, max_vid_index) else: max_vid_index = max(height, width, max_vid_index) max_txt_seq_len_int = int(max_txt_seq_len) # Create device-specific copy for text freqs without modifying self.pos_freqs txt_freqs = self.pos_freqs.to(device)[max_vid_index : max_vid_index + max_txt_seq_len_int, ...] vid_freqs = torch.cat(vid_freqs, dim=0) return vid_freqs, txt_freqs @functools.lru_cache(maxsize=128) def _compute_video_freqs( self, frame: int, height: int, width: int, idx: int = 0, device: torch.device = None ) -> torch.Tensor: seq_lens = frame * height * width pos_freqs = self.pos_freqs.to(device) if device is not None else self.pos_freqs neg_freqs = self.neg_freqs.to(device) if device is not None else self.neg_freqs freqs_pos = pos_freqs.split([x // 2 for x in self.axes_dim], dim=1) freqs_neg = neg_freqs.split([x // 2 for x in self.axes_dim], dim=1) freqs_frame = freqs_pos[0][idx : idx + frame].view(frame, 1, 1, -1).expand(frame, height, width, -1) if self.scale_rope: freqs_height = torch.cat([freqs_neg[1][-(height - height // 2) :], freqs_pos[1][: height // 2]], dim=0) freqs_height = freqs_height.view(1, height, 1, -1).expand(frame, height, width, -1) freqs_width = torch.cat([freqs_neg[2][-(width - width // 2) :], freqs_pos[2][: width // 2]], dim=0) freqs_width = freqs_width.view(1, 1, width, -1).expand(frame, height, width, -1) else: freqs_height = freqs_pos[1][:height].view(1, height, 1, -1).expand(frame, height, width, -1) freqs_width = freqs_pos[2][:width].view(1, 1, width, -1).expand(frame, height, width, -1) freqs = torch.cat([freqs_frame, freqs_height, freqs_width], dim=-1).reshape(seq_lens, -1) return freqs.clone().contiguous() class QwenEmbedLayer3DRope(nn.Module): def __init__(self, theta: int, axes_dim: list[int], scale_rope=False): super().__init__() self.theta = theta self.axes_dim = axes_dim pos_index = torch.arange(4096) neg_index = torch.arange(4096).flip(0) * -1 - 1 self.pos_freqs = torch.cat( [ self.rope_params(pos_index, self.axes_dim[0], self.theta), self.rope_params(pos_index, self.axes_dim[1], self.theta), self.rope_params(pos_index, self.axes_dim[2], self.theta), ], dim=1, ) self.neg_freqs = torch.cat( [ self.rope_params(neg_index, self.axes_dim[0], self.theta), self.rope_params(neg_index, self.axes_dim[1], self.theta), self.rope_params(neg_index, self.axes_dim[2], self.theta), ], dim=1, ) self.scale_rope = scale_rope def rope_params(self, index, dim, theta=10000): """ Args: index: [0, 1, 2, 3] 1D Tensor representing the position index of the token """ assert dim % 2 == 0 freqs = torch.outer(index, 1.0 / torch.pow(theta, torch.arange(0, dim, 2).to(torch.float32).div(dim))) freqs = torch.polar(torch.ones_like(freqs), freqs) return freqs def forward( self, video_fhw: tuple[int, int, int, list[tuple[int, int, int]]], max_txt_seq_len: int | torch.Tensor, device: torch.device = None, ) -> tuple[torch.Tensor, torch.Tensor]: """ Args: video_fhw (`tuple[int, int, int]` or `list[tuple[int, int, int]]`): A list of 3 integers [frame, height, width] representing the shape of the video, or a list of layer structures. max_txt_seq_len (`int` or `torch.Tensor`): The maximum text sequence length for RoPE computation. This should match the encoder hidden states sequence length. Can be either an int or a scalar tensor (for torch.compile compatibility). device: (`torch.device`, *optional*): The device on which to perform the RoPE computation. """ # Validate batch inference with variable-sized images # In Layer3DRope, the outer list represents batch, inner list/tuple represents layers if isinstance(video_fhw, list) and len(video_fhw) > 1: # Check if this is batch inference (list of layer lists/tuples) first_entry = video_fhw[0] if not all(entry == first_entry for entry in video_fhw): logger.warning( "Batch inference with variable-sized images is not currently supported in QwenEmbedLayer3DRope. " "All images in the batch should have the same layer structure. " f"Detected sizes: {video_fhw}. Using the first image's layer structure {first_entry} " "for RoPE computation, which may lead to incorrect results for other images in the batch." ) if isinstance(video_fhw, list): video_fhw = video_fhw[0] if not isinstance(video_fhw, list): video_fhw = [video_fhw] vid_freqs = [] max_vid_index = 0 layer_num = len(video_fhw) - 1 for idx, fhw in enumerate(video_fhw): frame, height, width = fhw if idx != layer_num: video_freq = self._compute_video_freqs(frame, height, width, idx, device) else: ### For the condition image, we set the layer index to -1 video_freq = self._compute_condition_freqs(frame, height, width, device) vid_freqs.append(video_freq) if self.scale_rope: max_vid_index = max(height // 2, width // 2, max_vid_index) else: max_vid_index = max(height, width, max_vid_index) max_vid_index = max(max_vid_index, layer_num) max_txt_seq_len_int = int(max_txt_seq_len) # Create device-specific copy for text freqs without modifying self.pos_freqs txt_freqs = self.pos_freqs.to(device)[max_vid_index : max_vid_index + max_txt_seq_len_int, ...] vid_freqs = torch.cat(vid_freqs, dim=0) return vid_freqs, txt_freqs @functools.lru_cache(maxsize=None) def _compute_video_freqs(self, frame, height, width, idx=0, device: torch.device = None): seq_lens = frame * height * width pos_freqs = self.pos_freqs.to(device) if device is not None else self.pos_freqs neg_freqs = self.neg_freqs.to(device) if device is not None else self.neg_freqs freqs_pos = pos_freqs.split([x // 2 for x in self.axes_dim], dim=1) freqs_neg = neg_freqs.split([x // 2 for x in self.axes_dim], dim=1) freqs_frame = freqs_pos[0][idx : idx + frame].view(frame, 1, 1, -1).expand(frame, height, width, -1) if self.scale_rope: freqs_height = torch.cat([freqs_neg[1][-(height - height // 2) :], freqs_pos[1][: height // 2]], dim=0) freqs_height = freqs_height.view(1, height, 1, -1).expand(frame, height, width, -1) freqs_width = torch.cat([freqs_neg[2][-(width - width // 2) :], freqs_pos[2][: width // 2]], dim=0) freqs_width = freqs_width.view(1, 1, width, -1).expand(frame, height, width, -1) else: freqs_height = freqs_pos[1][:height].view(1, height, 1, -1).expand(frame, height, width, -1) freqs_width = freqs_pos[2][:width].view(1, 1, width, -1).expand(frame, height, width, -1) freqs = torch.cat([freqs_frame, freqs_height, freqs_width], dim=-1).reshape(seq_lens, -1) return freqs.clone().contiguous() @functools.lru_cache(maxsize=None) def _compute_condition_freqs(self, frame, height, width, device: torch.device = None): seq_lens = frame * height * width pos_freqs = self.pos_freqs.to(device) if device is not None else self.pos_freqs neg_freqs = self.neg_freqs.to(device) if device is not None else self.neg_freqs freqs_pos = pos_freqs.split([x // 2 for x in self.axes_dim], dim=1) freqs_neg = neg_freqs.split([x // 2 for x in self.axes_dim], dim=1) freqs_frame = freqs_neg[0][-1:].view(frame, 1, 1, -1).expand(frame, height, width, -1) if self.scale_rope: freqs_height = torch.cat([freqs_neg[1][-(height - height // 2) :], freqs_pos[1][: height // 2]], dim=0) freqs_height = freqs_height.view(1, height, 1, -1).expand(frame, height, width, -1) freqs_width = torch.cat([freqs_neg[2][-(width - width // 2) :], freqs_pos[2][: width // 2]], dim=0) freqs_width = freqs_width.view(1, 1, width, -1).expand(frame, height, width, -1) else: freqs_height = freqs_pos[1][:height].view(1, height, 1, -1).expand(frame, height, width, -1) freqs_width = freqs_pos[2][:width].view(1, 1, width, -1).expand(frame, height, width, -1) freqs = torch.cat([freqs_frame, freqs_height, freqs_width], dim=-1).reshape(seq_lens, -1) return freqs.clone().contiguous() class QwenDoubleStreamAttnProcessor2_0: """ Attention processor for Qwen double-stream architecture, matching DoubleStreamLayerMegatron logic. This processor implements joint attention computation where text and image streams are processed together. """ _attention_backend = None _parallel_config = None def __init__(self): if not hasattr(F, "scaled_dot_product_attention"): raise ImportError( "QwenDoubleStreamAttnProcessor2_0 requires PyTorch 2.0, to use it, please upgrade PyTorch to 2.0." ) def __call__( self, attn: Attention, hidden_states: torch.FloatTensor, # Image stream encoder_hidden_states: torch.FloatTensor = None, # Text stream encoder_hidden_states_mask: torch.FloatTensor = None, attention_mask: torch.FloatTensor | None = None, image_rotary_emb: torch.Tensor | None = None, ) -> torch.FloatTensor: if encoder_hidden_states is None: raise ValueError("QwenDoubleStreamAttnProcessor2_0 requires encoder_hidden_states (text stream)") seq_txt = encoder_hidden_states.shape[1] # Compute QKV for image stream (sample projections) img_query = attn.to_q(hidden_states) img_key = attn.to_k(hidden_states) img_value = attn.to_v(hidden_states) # Compute QKV for text stream (context projections) txt_query = attn.add_q_proj(encoder_hidden_states) txt_key = attn.add_k_proj(encoder_hidden_states) txt_value = attn.add_v_proj(encoder_hidden_states) # Reshape for multi-head attention img_query = img_query.unflatten(-1, (attn.heads, -1)) img_key = img_key.unflatten(-1, (attn.heads, -1)) img_value = img_value.unflatten(-1, (attn.heads, -1)) txt_query = txt_query.unflatten(-1, (attn.heads, -1)) txt_key = txt_key.unflatten(-1, (attn.heads, -1)) txt_value = txt_value.unflatten(-1, (attn.heads, -1)) # Apply QK normalization if attn.norm_q is not None: img_query = attn.norm_q(img_query) if attn.norm_k is not None: img_key = attn.norm_k(img_key) if attn.norm_added_q is not None: txt_query = attn.norm_added_q(txt_query) if attn.norm_added_k is not None: txt_key = attn.norm_added_k(txt_key) # Apply RoPE if image_rotary_emb is not None: img_freqs, txt_freqs = image_rotary_emb img_query = apply_rotary_emb_qwen(img_query, img_freqs, use_real=False) img_key = apply_rotary_emb_qwen(img_key, img_freqs, use_real=False) txt_query = apply_rotary_emb_qwen(txt_query, txt_freqs, use_real=False) txt_key = apply_rotary_emb_qwen(txt_key, txt_freqs, use_real=False) # Concatenate for joint attention # Order: [text, image] joint_query = torch.cat([txt_query, img_query], dim=1) joint_key = torch.cat([txt_key, img_key], dim=1) joint_value = torch.cat([txt_value, img_value], dim=1) joint_hidden_states = dispatch_attention_fn( joint_query, joint_key, joint_value, attn_mask=attention_mask, dropout_p=0.0, is_causal=False, backend=self._attention_backend, parallel_config=self._parallel_config, ) # Reshape back joint_hidden_states = joint_hidden_states.flatten(2, 3) joint_hidden_states = joint_hidden_states.to(joint_query.dtype) # Split attention outputs back txt_attn_output = joint_hidden_states[:, :seq_txt, :] # Text part img_attn_output = joint_hidden_states[:, seq_txt:, :] # Image part # Apply output projections img_attn_output = attn.to_out[0](img_attn_output.contiguous()) if len(attn.to_out) > 1: img_attn_output = attn.to_out[1](img_attn_output) # dropout txt_attn_output = attn.to_add_out(txt_attn_output.contiguous()) return img_attn_output, txt_attn_output @maybe_allow_in_graph class QwenImageTransformerBlock(nn.Module): def __init__( self, dim: int, num_attention_heads: int, attention_head_dim: int, qk_norm: str = "rms_norm", eps: float = 1e-6, zero_cond_t: bool = False, ): super().__init__() self.dim = dim self.num_attention_heads = num_attention_heads self.attention_head_dim = attention_head_dim # Image processing modules self.img_mod = nn.Sequential( nn.SiLU(), nn.Linear(dim, 6 * dim, bias=True), # For scale, shift, gate for norm1 and norm2 ) self.img_norm1 = nn.LayerNorm(dim, elementwise_affine=False, eps=eps) self.attn = Attention( query_dim=dim, cross_attention_dim=None, # Enable cross attention for joint computation added_kv_proj_dim=dim, # Enable added KV projections for text stream dim_head=attention_head_dim, heads=num_attention_heads, out_dim=dim, context_pre_only=False, bias=True, processor=QwenDoubleStreamAttnProcessor2_0(), qk_norm=qk_norm, eps=eps, ) self.img_norm2 = nn.LayerNorm(dim, elementwise_affine=False, eps=eps) self.img_mlp = FeedForward(dim=dim, dim_out=dim, activation_fn="gelu-approximate") # Text processing modules self.txt_mod = nn.Sequential( nn.SiLU(), nn.Linear(dim, 6 * dim, bias=True), # For scale, shift, gate for norm1 and norm2 ) self.txt_norm1 = nn.LayerNorm(dim, elementwise_affine=False, eps=eps) # Text doesn't need separate attention - it's handled by img_attn joint computation self.txt_norm2 = nn.LayerNorm(dim, elementwise_affine=False, eps=eps) self.txt_mlp = FeedForward(dim=dim, dim_out=dim, activation_fn="gelu-approximate") self.zero_cond_t = zero_cond_t def _modulate(self, x, mod_params, index=None): """Apply modulation to input tensor""" # x: b l d, shift: b d, scale: b d, gate: b d shift, scale, gate = mod_params.chunk(3, dim=-1) if index is not None: # Assuming mod_params batch dim is 2*actual_batch (chunked into 2 parts) # So shift, scale, gate have shape [2*actual_batch, d] actual_batch = shift.size(0) // 2 shift_0, shift_1 = shift[:actual_batch], shift[actual_batch:] # each: [actual_batch, d] scale_0, scale_1 = scale[:actual_batch], scale[actual_batch:] gate_0, gate_1 = gate[:actual_batch], gate[actual_batch:] # index: [b, l] where b is actual batch size # Expand to [b, l, 1] to match feature dimension index_expanded = index.unsqueeze(-1) # [b, l, 1] # Expand chunks to [b, 1, d] then broadcast to [b, l, d] shift_0_exp = shift_0.unsqueeze(1) # [b, 1, d] shift_1_exp = shift_1.unsqueeze(1) # [b, 1, d] scale_0_exp = scale_0.unsqueeze(1) scale_1_exp = scale_1.unsqueeze(1) gate_0_exp = gate_0.unsqueeze(1) gate_1_exp = gate_1.unsqueeze(1) # Use torch.where to select based on index shift_result = torch.where(index_expanded == 0, shift_0_exp, shift_1_exp) scale_result = torch.where(index_expanded == 0, scale_0_exp, scale_1_exp) gate_result = torch.where(index_expanded == 0, gate_0_exp, gate_1_exp) else: shift_result = shift.unsqueeze(1) scale_result = scale.unsqueeze(1) gate_result = gate.unsqueeze(1) return x * (1 + scale_result) + shift_result, gate_result def forward( self, hidden_states: torch.Tensor, encoder_hidden_states: torch.Tensor, encoder_hidden_states_mask: torch.Tensor, temb: torch.Tensor, image_rotary_emb: tuple[torch.Tensor, torch.Tensor] | None = None, joint_attention_kwargs: dict[str, Any] | None = None, modulate_index: list[int] | None = None, ) -> tuple[torch.Tensor, torch.Tensor]: # Get modulation parameters for both streams img_mod_params = self.img_mod(temb) # [B, 6*dim] if self.zero_cond_t: temb = torch.chunk(temb, 2, dim=0)[0] txt_mod_params = self.txt_mod(temb) # [B, 6*dim] # Split modulation parameters for norm1 and norm2 img_mod1, img_mod2 = img_mod_params.chunk(2, dim=-1) # Each [B, 3*dim] txt_mod1, txt_mod2 = txt_mod_params.chunk(2, dim=-1) # Each [B, 3*dim] # Process image stream - norm1 + modulation img_normed = self.img_norm1(hidden_states) img_modulated, img_gate1 = self._modulate(img_normed, img_mod1, modulate_index) # Process text stream - norm1 + modulation txt_normed = self.txt_norm1(encoder_hidden_states) txt_modulated, txt_gate1 = self._modulate(txt_normed, txt_mod1) # Use QwenAttnProcessor2_0 for joint attention computation # This directly implements the DoubleStreamLayerMegatron logic: # 1. Computes QKV for both streams # 2. Applies QK normalization and RoPE # 3. Concatenates and runs joint attention # 4. Splits results back to separate streams joint_attention_kwargs = joint_attention_kwargs or {} attn_output = self.attn( hidden_states=img_modulated, # Image stream (will be processed as "sample") encoder_hidden_states=txt_modulated, # Text stream (will be processed as "context") encoder_hidden_states_mask=encoder_hidden_states_mask, image_rotary_emb=image_rotary_emb, **joint_attention_kwargs, ) # QwenAttnProcessor2_0 returns (img_output, txt_output) when encoder_hidden_states is provided img_attn_output, txt_attn_output = attn_output # Apply attention gates and add residual (like in Megatron) hidden_states = hidden_states + img_gate1 * img_attn_output encoder_hidden_states = encoder_hidden_states + txt_gate1 * txt_attn_output # Process image stream - norm2 + MLP img_normed2 = self.img_norm2(hidden_states) img_modulated2, img_gate2 = self._modulate(img_normed2, img_mod2, modulate_index) img_mlp_output = self.img_mlp(img_modulated2) hidden_states = hidden_states + img_gate2 * img_mlp_output # Process text stream - norm2 + MLP txt_normed2 = self.txt_norm2(encoder_hidden_states) txt_modulated2, txt_gate2 = self._modulate(txt_normed2, txt_mod2) txt_mlp_output = self.txt_mlp(txt_modulated2) encoder_hidden_states = encoder_hidden_states + txt_gate2 * txt_mlp_output # Clip to prevent overflow for fp16 if encoder_hidden_states.dtype == torch.float16: encoder_hidden_states = encoder_hidden_states.clip(-65504, 65504) if hidden_states.dtype == torch.float16: hidden_states = hidden_states.clip(-65504, 65504) return encoder_hidden_states, hidden_states class QwenImageTransformer2DModel( ModelMixin, ConfigMixin, PeftAdapterMixin, FromOriginalModelMixin, CacheMixin, AttentionMixin ): """ The Transformer model introduced in Qwen. Args: patch_size (`int`, defaults to `2`): Patch size to turn the input data into small patches. in_channels (`int`, defaults to `64`): The number of channels in the input. out_channels (`int`, *optional*, defaults to `None`): The number of channels in the output. If not specified, it defaults to `in_channels`. num_layers (`int`, defaults to `60`): The number of layers of dual stream DiT blocks to use. attention_head_dim (`int`, defaults to `128`): The number of dimensions to use for each attention head. num_attention_heads (`int`, defaults to `24`): The number of attention heads to use. joint_attention_dim (`int`, defaults to `3584`): The number of dimensions to use for the joint attention (embedding/channel dimension of `encoder_hidden_states`). guidance_embeds (`bool`, defaults to `False`): Whether to use guidance embeddings for guidance-distilled variant of the model. axes_dims_rope (`tuple[int]`, defaults to `(16, 56, 56)`): The dimensions to use for the rotary positional embeddings. """ _supports_gradient_checkpointing = True _no_split_modules = ["QwenImageTransformerBlock"] _skip_layerwise_casting_patterns = ["pos_embed", "norm"] _repeated_blocks = ["QwenImageTransformerBlock"] # Make CP plan compatible with https://github.com/huggingface/diffusers/pull/12702 _cp_plan = { "transformer_blocks.0": { "hidden_states": ContextParallelInput(split_dim=1, expected_dims=3, split_output=False), "encoder_hidden_states": ContextParallelInput(split_dim=1, expected_dims=3, split_output=False), }, "transformer_blocks.*": { "modulate_index": ContextParallelInput(split_dim=1, expected_dims=2, split_output=False), }, "pos_embed": { 0: ContextParallelInput(split_dim=0, expected_dims=2, split_output=True), 1: ContextParallelInput(split_dim=0, expected_dims=2, split_output=True), }, "proj_out": ContextParallelOutput(gather_dim=1, expected_dims=3), } @register_to_config def __init__( self, patch_size: int = 2, in_channels: int = 64, out_channels: int | None = 16, num_layers: int = 60, attention_head_dim: int = 128, num_attention_heads: int = 24, joint_attention_dim: int = 3584, guidance_embeds: bool = False, # TODO: this should probably be removed axes_dims_rope: tuple[int, int, int] = (16, 56, 56), zero_cond_t: bool = False, use_additional_t_cond: bool = False, use_layer3d_rope: bool = False, ): super().__init__() self.out_channels = out_channels or in_channels self.inner_dim = num_attention_heads * attention_head_dim if not use_layer3d_rope: self.pos_embed = QwenEmbedRope(theta=10000, axes_dim=list(axes_dims_rope), scale_rope=True) else: self.pos_embed = QwenEmbedLayer3DRope(theta=10000, axes_dim=list(axes_dims_rope), scale_rope=True) self.time_text_embed = QwenTimestepProjEmbeddings( embedding_dim=self.inner_dim, use_additional_t_cond=use_additional_t_cond ) self.txt_norm = RMSNorm(joint_attention_dim, eps=1e-6) self.img_in = nn.Linear(in_channels, self.inner_dim) self.txt_in = nn.Linear(joint_attention_dim, self.inner_dim) self.transformer_blocks = nn.ModuleList( [ QwenImageTransformerBlock( dim=self.inner_dim, num_attention_heads=num_attention_heads, attention_head_dim=attention_head_dim, zero_cond_t=zero_cond_t, ) for _ in range(num_layers) ] ) self.norm_out = AdaLayerNormContinuous(self.inner_dim, self.inner_dim, elementwise_affine=False, eps=1e-6) self.proj_out = nn.Linear(self.inner_dim, patch_size * patch_size * self.out_channels, bias=True) self.gradient_checkpointing = False self.zero_cond_t = zero_cond_t @apply_lora_scale("attention_kwargs") def forward( self, hidden_states: torch.Tensor, encoder_hidden_states: torch.Tensor = None, encoder_hidden_states_mask: torch.Tensor = None, timestep: torch.LongTensor = None, img_shapes: list[tuple[int, int, int]] | None = None, txt_seq_lens: list[int] | None = None, guidance: torch.Tensor = None, # TODO: this should probably be removed attention_kwargs: dict[str, Any] | None = None, controlnet_block_samples=None, additional_t_cond=None, return_dict: bool = True, ) -> torch.Tensor | Transformer2DModelOutput: """ The [`QwenTransformer2DModel`] forward method. Args: hidden_states (`torch.Tensor` of shape `(batch_size, image_sequence_length, in_channels)`): Input `hidden_states`. encoder_hidden_states (`torch.Tensor` of shape `(batch_size, text_sequence_length, joint_attention_dim)`): Conditional embeddings (embeddings computed from the input conditions such as prompts) to use. encoder_hidden_states_mask (`torch.Tensor` of shape `(batch_size, text_sequence_length)`, *optional*): Mask for the encoder hidden states. Expected to have 1.0 for valid tokens and 0.0 for padding tokens. Used in the attention processor to prevent attending to padding tokens. The mask can have any pattern (not just contiguous valid tokens followed by padding) since it's applied element-wise in attention. timestep ( `torch.LongTensor`): Used to indicate denoising step. img_shapes (`list[tuple[int, int, int]]`, *optional*): Image shapes for RoPE computation. txt_seq_lens (`list[int]`, *optional*, **Deprecated**): Deprecated parameter. Use `encoder_hidden_states_mask` instead. If provided, the maximum value will be used to compute RoPE sequence length. guidance (`torch.Tensor`, *optional*): Guidance tensor for conditional generation. attention_kwargs (`dict`, *optional*): A kwargs dictionary that if specified is passed along to the `AttentionProcessor` as defined under `self.processor` in [diffusers.models.attention_processor](https://github.com/huggingface/diffusers/blob/main/src/diffusers/models/attention_processor.py). controlnet_block_samples (*optional*): ControlNet block samples to add to the transformer blocks. return_dict (`bool`, *optional*, defaults to `True`): Whether or not to return a [`~models.transformer_2d.Transformer2DModelOutput`] instead of a plain tuple. Returns: If `return_dict` is True, an [`~models.transformer_2d.Transformer2DModelOutput`] is returned, otherwise a `tuple` where the first element is the sample tensor. """ if txt_seq_lens is not None: deprecate( "txt_seq_lens", "0.39.0", "Passing `txt_seq_lens` is deprecated and will be removed in version 0.39.0. " "Please use `encoder_hidden_states_mask` instead. " "The mask-based approach is more flexible and supports variable-length sequences.", standard_warn=False, ) hidden_states = self.img_in(hidden_states) timestep = timestep.to(hidden_states.dtype) if self.zero_cond_t: timestep = torch.cat([timestep, timestep * 0], dim=0) modulate_index = torch.tensor( [[0] * prod(sample[0]) + [1] * sum([prod(s) for s in sample[1:]]) for sample in img_shapes], device=timestep.device, dtype=torch.int, ) else: modulate_index = None encoder_hidden_states = self.txt_norm(encoder_hidden_states) encoder_hidden_states = self.txt_in(encoder_hidden_states) # Use the encoder_hidden_states sequence length for RoPE computation and normalize mask text_seq_len, _, encoder_hidden_states_mask = compute_text_seq_len_from_mask( encoder_hidden_states, encoder_hidden_states_mask ) if guidance is not None: guidance = guidance.to(hidden_states.dtype) * 1000 temb = ( self.time_text_embed(timestep, hidden_states, additional_t_cond) if guidance is None else self.time_text_embed(timestep, guidance, hidden_states, additional_t_cond) ) image_rotary_emb = self.pos_embed(img_shapes, max_txt_seq_len=text_seq_len, device=hidden_states.device) # Construct joint attention mask once to avoid reconstructing in every block # This eliminates 60 GPU syncs during training while maintaining torch.compile compatibility block_attention_kwargs = attention_kwargs.copy() if attention_kwargs is not None else {} if encoder_hidden_states_mask is not None: # Build joint mask: [text_mask, all_ones_for_image] batch_size, image_seq_len = hidden_states.shape[:2] image_mask = torch.ones((batch_size, image_seq_len), dtype=torch.bool, device=hidden_states.device) joint_attention_mask = torch.cat([encoder_hidden_states_mask, image_mask], dim=1) block_attention_kwargs["attention_mask"] = joint_attention_mask for index_block, block in enumerate(self.transformer_blocks): if torch.is_grad_enabled() and self.gradient_checkpointing: encoder_hidden_states, hidden_states = self._gradient_checkpointing_func( block, hidden_states, encoder_hidden_states, None, # Don't pass encoder_hidden_states_mask (using attention_mask instead) temb, image_rotary_emb, block_attention_kwargs, modulate_index, ) else: encoder_hidden_states, hidden_states = block( hidden_states=hidden_states, encoder_hidden_states=encoder_hidden_states, encoder_hidden_states_mask=None, # Don't pass (using attention_mask instead) temb=temb, image_rotary_emb=image_rotary_emb, joint_attention_kwargs=block_attention_kwargs, modulate_index=modulate_index, ) # controlnet residual if controlnet_block_samples is not None: interval_control = len(self.transformer_blocks) / len(controlnet_block_samples) interval_control = int(np.ceil(interval_control)) hidden_states = hidden_states + controlnet_block_samples[index_block // interval_control] if self.zero_cond_t: temb = temb.chunk(2, dim=0)[0] # Use only the image part (hidden_states) from the dual-stream blocks hidden_states = self.norm_out(hidden_states, temb) output = self.proj_out(hidden_states) if not return_dict: return (output,) return Transformer2DModelOutput(sample=output)
{ "repo_id": "huggingface/diffusers", "file_path": "src/diffusers/models/transformers/transformer_qwenimage.py", "license": "Apache License 2.0", "lines": 838, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
huggingface/diffusers:src/diffusers/pipelines/qwenimage/pipeline_output.py
from dataclasses import dataclass import numpy as np import PIL.Image from ...utils import BaseOutput @dataclass class QwenImagePipelineOutput(BaseOutput): """ Output class for Stable Diffusion pipelines. Args: images (`list[PIL.Image.Image]` or `np.ndarray`) list of denoised PIL images of length `batch_size` or numpy array of shape `(batch_size, height, width, num_channels)`. PIL images or numpy array present the denoised images of the diffusion pipeline. """ images: list[PIL.Image.Image] | np.ndarray
{ "repo_id": "huggingface/diffusers", "file_path": "src/diffusers/pipelines/qwenimage/pipeline_output.py", "license": "Apache License 2.0", "lines": 14, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
huggingface/diffusers:src/diffusers/pipelines/qwenimage/pipeline_qwenimage.py
# Copyright 2025 Qwen-Image Team and The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import inspect from typing import Any, Callable import numpy as np import torch from transformers import Qwen2_5_VLForConditionalGeneration, Qwen2Tokenizer from ...image_processor import VaeImageProcessor from ...loaders import QwenImageLoraLoaderMixin from ...models import AutoencoderKLQwenImage, QwenImageTransformer2DModel from ...schedulers import FlowMatchEulerDiscreteScheduler from ...utils import deprecate, is_torch_xla_available, logging, replace_example_docstring from ...utils.torch_utils import randn_tensor from ..pipeline_utils import DiffusionPipeline from .pipeline_output import QwenImagePipelineOutput if is_torch_xla_available(): import torch_xla.core.xla_model as xm XLA_AVAILABLE = True else: XLA_AVAILABLE = False logger = logging.get_logger(__name__) # pylint: disable=invalid-name EXAMPLE_DOC_STRING = """ Examples: ```py >>> import torch >>> from diffusers import QwenImagePipeline >>> pipe = QwenImagePipeline.from_pretrained("Qwen/Qwen-Image", torch_dtype=torch.bfloat16) >>> pipe.to("cuda") >>> prompt = "A cat holding a sign that says hello world" >>> # Depending on the variant being used, the pipeline call will slightly vary. >>> # Refer to the pipeline documentation for more details. >>> image = pipe(prompt, num_inference_steps=50).images[0] >>> image.save("qwenimage.png") ``` """ def calculate_shift( image_seq_len, base_seq_len: int = 256, max_seq_len: int = 4096, base_shift: float = 0.5, max_shift: float = 1.15, ): m = (max_shift - base_shift) / (max_seq_len - base_seq_len) b = base_shift - m * base_seq_len mu = image_seq_len * m + b return mu # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.retrieve_timesteps def retrieve_timesteps( scheduler, num_inference_steps: int | None = None, device: str | torch.device | None = None, timesteps: list[int] | None = None, sigmas: list[float] | None = None, **kwargs, ): r""" Calls the scheduler's `set_timesteps` method and retrieves timesteps from the scheduler after the call. Handles custom timesteps. Any kwargs will be supplied to `scheduler.set_timesteps`. Args: scheduler (`SchedulerMixin`): The scheduler to get timesteps from. num_inference_steps (`int`): The number of diffusion steps used when generating samples with a pre-trained model. If used, `timesteps` must be `None`. device (`str` or `torch.device`, *optional*): The device to which the timesteps should be moved to. If `None`, the timesteps are not moved. timesteps (`list[int]`, *optional*): Custom timesteps used to override the timestep spacing strategy of the scheduler. If `timesteps` is passed, `num_inference_steps` and `sigmas` must be `None`. sigmas (`list[float]`, *optional*): Custom sigmas used to override the timestep spacing strategy of the scheduler. If `sigmas` is passed, `num_inference_steps` and `timesteps` must be `None`. Returns: `tuple[torch.Tensor, int]`: A tuple where the first element is the timestep schedule from the scheduler and the second element is the number of inference steps. """ if timesteps is not None and sigmas is not None: raise ValueError("Only one of `timesteps` or `sigmas` can be passed. Please choose one to set custom values") if timesteps is not None: accepts_timesteps = "timesteps" in set(inspect.signature(scheduler.set_timesteps).parameters.keys()) if not accepts_timesteps: raise ValueError( f"The current scheduler class {scheduler.__class__}'s `set_timesteps` does not support custom" f" timestep schedules. Please check whether you are using the correct scheduler." ) scheduler.set_timesteps(timesteps=timesteps, device=device, **kwargs) timesteps = scheduler.timesteps num_inference_steps = len(timesteps) elif sigmas is not None: accept_sigmas = "sigmas" in set(inspect.signature(scheduler.set_timesteps).parameters.keys()) if not accept_sigmas: raise ValueError( f"The current scheduler class {scheduler.__class__}'s `set_timesteps` does not support custom" f" sigmas schedules. Please check whether you are using the correct scheduler." ) scheduler.set_timesteps(sigmas=sigmas, device=device, **kwargs) timesteps = scheduler.timesteps num_inference_steps = len(timesteps) else: scheduler.set_timesteps(num_inference_steps, device=device, **kwargs) timesteps = scheduler.timesteps return timesteps, num_inference_steps class QwenImagePipeline(DiffusionPipeline, QwenImageLoraLoaderMixin): r""" The QwenImage pipeline for text-to-image generation. Args: transformer ([`QwenImageTransformer2DModel`]): Conditional Transformer (MMDiT) architecture to denoise the encoded image latents. scheduler ([`FlowMatchEulerDiscreteScheduler`]): A scheduler to be used in combination with `transformer` to denoise the encoded image latents. vae ([`AutoencoderKL`]): Variational Auto-Encoder (VAE) Model to encode and decode images to and from latent representations. text_encoder ([`Qwen2.5-VL-7B-Instruct`]): [Qwen2.5-VL-7B-Instruct](https://huggingface.co/Qwen/Qwen2.5-VL-7B-Instruct), specifically the [Qwen2.5-VL-7B-Instruct](https://huggingface.co/Qwen/Qwen2.5-VL-7B-Instruct) variant. tokenizer (`QwenTokenizer`): Tokenizer of class [CLIPTokenizer](https://huggingface.co/docs/transformers/en/model_doc/clip#transformers.CLIPTokenizer). """ model_cpu_offload_seq = "text_encoder->transformer->vae" _callback_tensor_inputs = ["latents", "prompt_embeds"] def __init__( self, scheduler: FlowMatchEulerDiscreteScheduler, vae: AutoencoderKLQwenImage, text_encoder: Qwen2_5_VLForConditionalGeneration, tokenizer: Qwen2Tokenizer, transformer: QwenImageTransformer2DModel, ): super().__init__() self.register_modules( vae=vae, text_encoder=text_encoder, tokenizer=tokenizer, transformer=transformer, scheduler=scheduler, ) self.vae_scale_factor = 2 ** len(self.vae.temperal_downsample) if getattr(self, "vae", None) else 8 # QwenImage latents are turned into 2x2 patches and packed. This means the latent width and height has to be divisible # by the patch size. So the vae scale factor is multiplied by the patch size to account for this self.image_processor = VaeImageProcessor(vae_scale_factor=self.vae_scale_factor * 2) self.tokenizer_max_length = 1024 self.prompt_template_encode = "<|im_start|>system\nDescribe the image by detailing the color, shape, size, texture, quantity, text, spatial relationships of the objects and background:<|im_end|>\n<|im_start|>user\n{}<|im_end|>\n<|im_start|>assistant\n" self.prompt_template_encode_start_idx = 34 self.default_sample_size = 128 def _extract_masked_hidden(self, hidden_states: torch.Tensor, mask: torch.Tensor): bool_mask = mask.bool() valid_lengths = bool_mask.sum(dim=1) selected = hidden_states[bool_mask] split_result = torch.split(selected, valid_lengths.tolist(), dim=0) return split_result def _get_qwen_prompt_embeds( self, prompt: str | list[str] = None, device: torch.device | None = None, dtype: torch.dtype | None = None, ): device = device or self._execution_device dtype = dtype or self.text_encoder.dtype prompt = [prompt] if isinstance(prompt, str) else prompt template = self.prompt_template_encode drop_idx = self.prompt_template_encode_start_idx txt = [template.format(e) for e in prompt] txt_tokens = self.tokenizer( txt, max_length=self.tokenizer_max_length + drop_idx, padding=True, truncation=True, return_tensors="pt" ).to(device) encoder_hidden_states = self.text_encoder( input_ids=txt_tokens.input_ids, attention_mask=txt_tokens.attention_mask, output_hidden_states=True, ) hidden_states = encoder_hidden_states.hidden_states[-1] split_hidden_states = self._extract_masked_hidden(hidden_states, txt_tokens.attention_mask) split_hidden_states = [e[drop_idx:] for e in split_hidden_states] attn_mask_list = [torch.ones(e.size(0), dtype=torch.long, device=e.device) for e in split_hidden_states] max_seq_len = max([e.size(0) for e in split_hidden_states]) prompt_embeds = torch.stack( [torch.cat([u, u.new_zeros(max_seq_len - u.size(0), u.size(1))]) for u in split_hidden_states] ) encoder_attention_mask = torch.stack( [torch.cat([u, u.new_zeros(max_seq_len - u.size(0))]) for u in attn_mask_list] ) prompt_embeds = prompt_embeds.to(dtype=dtype, device=device) return prompt_embeds, encoder_attention_mask def encode_prompt( self, prompt: str | list[str], device: torch.device | None = None, num_images_per_prompt: int = 1, prompt_embeds: torch.Tensor | None = None, prompt_embeds_mask: torch.Tensor | None = None, max_sequence_length: int = 1024, ): r""" Args: prompt (`str` or `list[str]`, *optional*): prompt to be encoded device: (`torch.device`): torch device num_images_per_prompt (`int`): number of images that should be generated per prompt prompt_embeds (`torch.Tensor`, *optional*): Pre-generated text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt weighting. If not provided, text embeddings will be generated from `prompt` input argument. """ device = device or self._execution_device prompt = [prompt] if isinstance(prompt, str) else prompt batch_size = len(prompt) if prompt_embeds is None else prompt_embeds.shape[0] if prompt_embeds is None: prompt_embeds, prompt_embeds_mask = self._get_qwen_prompt_embeds(prompt, device) prompt_embeds = prompt_embeds[:, :max_sequence_length] _, seq_len, _ = prompt_embeds.shape prompt_embeds = prompt_embeds.repeat(1, num_images_per_prompt, 1) prompt_embeds = prompt_embeds.view(batch_size * num_images_per_prompt, seq_len, -1) if prompt_embeds_mask is not None: prompt_embeds_mask = prompt_embeds_mask[:, :max_sequence_length] prompt_embeds_mask = prompt_embeds_mask.repeat(1, num_images_per_prompt, 1) prompt_embeds_mask = prompt_embeds_mask.view(batch_size * num_images_per_prompt, seq_len) if prompt_embeds_mask.all(): prompt_embeds_mask = None return prompt_embeds, prompt_embeds_mask def check_inputs( self, prompt, height, width, negative_prompt=None, prompt_embeds=None, negative_prompt_embeds=None, prompt_embeds_mask=None, negative_prompt_embeds_mask=None, callback_on_step_end_tensor_inputs=None, max_sequence_length=None, ): if height % (self.vae_scale_factor * 2) != 0 or width % (self.vae_scale_factor * 2) != 0: logger.warning( f"`height` and `width` have to be divisible by {self.vae_scale_factor * 2} but are {height} and {width}. Dimensions will be resized accordingly" ) if callback_on_step_end_tensor_inputs is not None and not all( k in self._callback_tensor_inputs for k in callback_on_step_end_tensor_inputs ): raise ValueError( f"`callback_on_step_end_tensor_inputs` has to be in {self._callback_tensor_inputs}, but found {[k for k in callback_on_step_end_tensor_inputs if k not in self._callback_tensor_inputs]}" ) if prompt is not None and prompt_embeds is not None: raise ValueError( f"Cannot forward both `prompt`: {prompt} and `prompt_embeds`: {prompt_embeds}. Please make sure to" " only forward one of the two." ) elif prompt is None and prompt_embeds is None: raise ValueError( "Provide either `prompt` or `prompt_embeds`. Cannot leave both `prompt` and `prompt_embeds` undefined." ) elif prompt is not None and (not isinstance(prompt, str) and not isinstance(prompt, list)): raise ValueError(f"`prompt` has to be of type `str` or `list` but is {type(prompt)}") if negative_prompt is not None and negative_prompt_embeds is not None: raise ValueError( f"Cannot forward both `negative_prompt`: {negative_prompt} and `negative_prompt_embeds`:" f" {negative_prompt_embeds}. Please make sure to only forward one of the two." ) if max_sequence_length is not None and max_sequence_length > 1024: raise ValueError(f"`max_sequence_length` cannot be greater than 1024 but is {max_sequence_length}") @staticmethod def _pack_latents(latents, batch_size, num_channels_latents, height, width): latents = latents.view(batch_size, num_channels_latents, height // 2, 2, width // 2, 2) latents = latents.permute(0, 2, 4, 1, 3, 5) latents = latents.reshape(batch_size, (height // 2) * (width // 2), num_channels_latents * 4) return latents @staticmethod def _unpack_latents(latents, height, width, vae_scale_factor): batch_size, num_patches, channels = latents.shape # VAE applies 8x compression on images but we must also account for packing which requires # latent height and width to be divisible by 2. height = 2 * (int(height) // (vae_scale_factor * 2)) width = 2 * (int(width) // (vae_scale_factor * 2)) latents = latents.view(batch_size, height // 2, width // 2, channels // 4, 2, 2) latents = latents.permute(0, 3, 1, 4, 2, 5) latents = latents.reshape(batch_size, channels // (2 * 2), 1, height, width) return latents def enable_vae_slicing(self): r""" Enable sliced VAE decoding. When this option is enabled, the VAE will split the input tensor in slices to compute decoding in several steps. This is useful to save some memory and allow larger batch sizes. """ depr_message = f"Calling `enable_vae_slicing()` on a `{self.__class__.__name__}` is deprecated and this method will be removed in a future version. Please use `pipe.vae.enable_slicing()`." deprecate( "enable_vae_slicing", "0.40.0", depr_message, ) self.vae.enable_slicing() def disable_vae_slicing(self): r""" Disable sliced VAE decoding. If `enable_vae_slicing` was previously enabled, this method will go back to computing decoding in one step. """ depr_message = f"Calling `disable_vae_slicing()` on a `{self.__class__.__name__}` is deprecated and this method will be removed in a future version. Please use `pipe.vae.disable_slicing()`." deprecate( "disable_vae_slicing", "0.40.0", depr_message, ) self.vae.disable_slicing() def enable_vae_tiling(self): r""" Enable tiled VAE decoding. When this option is enabled, the VAE will split the input tensor into tiles to compute decoding and encoding in several steps. This is useful for saving a large amount of memory and to allow processing larger images. """ depr_message = f"Calling `enable_vae_tiling()` on a `{self.__class__.__name__}` is deprecated and this method will be removed in a future version. Please use `pipe.vae.enable_tiling()`." deprecate( "enable_vae_tiling", "0.40.0", depr_message, ) self.vae.enable_tiling() def disable_vae_tiling(self): r""" Disable tiled VAE decoding. If `enable_vae_tiling` was previously enabled, this method will go back to computing decoding in one step. """ depr_message = f"Calling `disable_vae_tiling()` on a `{self.__class__.__name__}` is deprecated and this method will be removed in a future version. Please use `pipe.vae.disable_tiling()`." deprecate( "disable_vae_tiling", "0.40.0", depr_message, ) self.vae.disable_tiling() def prepare_latents( self, batch_size, num_channels_latents, height, width, dtype, device, generator, latents=None, ): # VAE applies 8x compression on images but we must also account for packing which requires # latent height and width to be divisible by 2. height = 2 * (int(height) // (self.vae_scale_factor * 2)) width = 2 * (int(width) // (self.vae_scale_factor * 2)) shape = (batch_size, 1, num_channels_latents, height, width) if latents is not None: return latents.to(device=device, dtype=dtype) if isinstance(generator, list) and len(generator) != batch_size: raise ValueError( f"You have passed a list of generators of length {len(generator)}, but requested an effective batch" f" size of {batch_size}. Make sure the batch size matches the length of the generators." ) latents = randn_tensor(shape, generator=generator, device=device, dtype=dtype) latents = self._pack_latents(latents, batch_size, num_channels_latents, height, width) return latents @property def guidance_scale(self): return self._guidance_scale @property def attention_kwargs(self): return self._attention_kwargs @property def num_timesteps(self): return self._num_timesteps @property def current_timestep(self): return self._current_timestep @property def interrupt(self): return self._interrupt @torch.no_grad() @replace_example_docstring(EXAMPLE_DOC_STRING) def __call__( self, prompt: str | list[str] = None, negative_prompt: str | list[str] = None, true_cfg_scale: float = 4.0, height: int | None = None, width: int | None = None, num_inference_steps: int = 50, sigmas: list[float] | None = None, guidance_scale: float | None = None, num_images_per_prompt: int = 1, generator: torch.Generator | list[torch.Generator] | None = None, latents: torch.Tensor | None = None, prompt_embeds: torch.Tensor | None = None, prompt_embeds_mask: torch.Tensor | None = None, negative_prompt_embeds: torch.Tensor | None = None, negative_prompt_embeds_mask: torch.Tensor | None = None, output_type: str | None = "pil", return_dict: bool = True, attention_kwargs: dict[str, Any] | None = None, callback_on_step_end: Callable[[int, int], None] | None = None, callback_on_step_end_tensor_inputs: list[str] = ["latents"], max_sequence_length: int = 512, ): r""" Function invoked when calling the pipeline for generation. Args: prompt (`str` or `list[str]`, *optional*): The prompt or prompts to guide the image generation. If not defined, one has to pass `prompt_embeds`. instead. negative_prompt (`str` or `list[str]`, *optional*): The prompt or prompts not to guide the image generation. If not defined, one has to pass `negative_prompt_embeds` instead. Ignored when not using guidance (i.e., ignored if `true_cfg_scale` is not greater than `1`). true_cfg_scale (`float`, *optional*, defaults to 1.0): Guidance scale as defined in [Classifier-Free Diffusion Guidance](https://huggingface.co/papers/2207.12598). `true_cfg_scale` is defined as `w` of equation 2. of [Imagen Paper](https://huggingface.co/papers/2205.11487). Classifier-free guidance is enabled by setting `true_cfg_scale > 1` and a provided `negative_prompt`. Higher guidance scale encourages to generate images that are closely linked to the text `prompt`, usually at the expense of lower image quality. height (`int`, *optional*, defaults to self.unet.config.sample_size * self.vae_scale_factor): The height in pixels of the generated image. This is set to 1024 by default for the best results. width (`int`, *optional*, defaults to self.unet.config.sample_size * self.vae_scale_factor): The width in pixels of the generated image. This is set to 1024 by default for the best results. num_inference_steps (`int`, *optional*, defaults to 50): The number of denoising steps. More denoising steps usually lead to a higher quality image at the expense of slower inference. sigmas (`list[float]`, *optional*): Custom sigmas to use for the denoising process with schedulers which support a `sigmas` argument in their `set_timesteps` method. If not defined, the default behavior when `num_inference_steps` is passed will be used. guidance_scale (`float`, *optional*, defaults to None): A guidance scale value for guidance distilled models. Unlike the traditional classifier-free guidance where the guidance scale is applied during inference through noise prediction rescaling, guidance distilled models take the guidance scale directly as an input parameter during forward pass. Guidance scale is enabled by setting `guidance_scale > 1`. Higher guidance scale encourages to generate images that are closely linked to the text `prompt`, usually at the expense of lower image quality. This parameter in the pipeline is there to support future guidance-distilled models when they come up. It is ignored when not using guidance distilled models. To enable traditional classifier-free guidance, please pass `true_cfg_scale > 1.0` and `negative_prompt` (even an empty negative prompt like " " should enable classifier-free guidance computations). num_images_per_prompt (`int`, *optional*, defaults to 1): The number of images to generate per prompt. generator (`torch.Generator` or `list[torch.Generator]`, *optional*): One or a list of [torch generator(s)](https://pytorch.org/docs/stable/generated/torch.Generator.html) to make generation deterministic. latents (`torch.Tensor`, *optional*): Pre-generated noisy latents, sampled from a Gaussian distribution, to be used as inputs for image generation. Can be used to tweak the same generation with different prompts. If not provided, a latents tensor will be generated by sampling using the supplied random `generator`. prompt_embeds (`torch.Tensor`, *optional*): Pre-generated text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt weighting. If not provided, text embeddings will be generated from `prompt` input argument. negative_prompt_embeds (`torch.Tensor`, *optional*): Pre-generated negative text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt weighting. If not provided, negative_prompt_embeds will be generated from `negative_prompt` input argument. output_type (`str`, *optional*, defaults to `"pil"`): The output format of the generate image. Choose between [PIL](https://pillow.readthedocs.io/en/stable/): `PIL.Image.Image` or `np.array`. return_dict (`bool`, *optional*, defaults to `True`): Whether or not to return a [`~pipelines.qwenimage.QwenImagePipelineOutput`] instead of a plain tuple. attention_kwargs (`dict`, *optional*): A kwargs dictionary that if specified is passed along to the `AttentionProcessor` as defined under `self.processor` in [diffusers.models.attention_processor](https://github.com/huggingface/diffusers/blob/main/src/diffusers/models/attention_processor.py). callback_on_step_end (`Callable`, *optional*): A function that calls at the end of each denoising steps during the inference. The function is called with the following arguments: `callback_on_step_end(self: DiffusionPipeline, step: int, timestep: int, callback_kwargs: Dict)`. `callback_kwargs` will include a list of all tensors as specified by `callback_on_step_end_tensor_inputs`. callback_on_step_end_tensor_inputs (`list`, *optional*): The list of tensor inputs for the `callback_on_step_end` function. The tensors specified in the list will be passed as `callback_kwargs` argument. You will only be able to include variables listed in the `._callback_tensor_inputs` attribute of your pipeline class. max_sequence_length (`int` defaults to 512): Maximum sequence length to use with the `prompt`. Examples: Returns: [`~pipelines.qwenimage.QwenImagePipelineOutput`] or `tuple`: [`~pipelines.qwenimage.QwenImagePipelineOutput`] if `return_dict` is True, otherwise a `tuple`. When returning a tuple, the first element is a list with the generated images. """ height = height or self.default_sample_size * self.vae_scale_factor width = width or self.default_sample_size * self.vae_scale_factor # 1. Check inputs. Raise error if not correct self.check_inputs( prompt, height, width, negative_prompt=negative_prompt, prompt_embeds=prompt_embeds, negative_prompt_embeds=negative_prompt_embeds, prompt_embeds_mask=prompt_embeds_mask, negative_prompt_embeds_mask=negative_prompt_embeds_mask, callback_on_step_end_tensor_inputs=callback_on_step_end_tensor_inputs, max_sequence_length=max_sequence_length, ) self._guidance_scale = guidance_scale self._attention_kwargs = attention_kwargs self._current_timestep = None self._interrupt = False # 2. Define call parameters if prompt is not None and isinstance(prompt, str): batch_size = 1 elif prompt is not None and isinstance(prompt, list): batch_size = len(prompt) else: batch_size = prompt_embeds.shape[0] device = self._execution_device has_neg_prompt = negative_prompt is not None or ( negative_prompt_embeds is not None and negative_prompt_embeds_mask is not None ) if true_cfg_scale > 1 and not has_neg_prompt: logger.warning( f"true_cfg_scale is passed as {true_cfg_scale}, but classifier-free guidance is not enabled since no negative_prompt is provided." ) elif true_cfg_scale <= 1 and has_neg_prompt: logger.warning( " negative_prompt is passed but classifier-free guidance is not enabled since true_cfg_scale <= 1" ) do_true_cfg = true_cfg_scale > 1 and has_neg_prompt prompt_embeds, prompt_embeds_mask = self.encode_prompt( prompt=prompt, prompt_embeds=prompt_embeds, prompt_embeds_mask=prompt_embeds_mask, device=device, num_images_per_prompt=num_images_per_prompt, max_sequence_length=max_sequence_length, ) if do_true_cfg: negative_prompt_embeds, negative_prompt_embeds_mask = self.encode_prompt( prompt=negative_prompt, prompt_embeds=negative_prompt_embeds, prompt_embeds_mask=negative_prompt_embeds_mask, device=device, num_images_per_prompt=num_images_per_prompt, max_sequence_length=max_sequence_length, ) # 4. Prepare latent variables num_channels_latents = self.transformer.config.in_channels // 4 latents = self.prepare_latents( batch_size * num_images_per_prompt, num_channels_latents, height, width, prompt_embeds.dtype, device, generator, latents, ) img_shapes = [[(1, height // self.vae_scale_factor // 2, width // self.vae_scale_factor // 2)]] * batch_size # 5. Prepare timesteps sigmas = np.linspace(1.0, 1 / num_inference_steps, num_inference_steps) if sigmas is None else sigmas image_seq_len = latents.shape[1] mu = calculate_shift( image_seq_len, self.scheduler.config.get("base_image_seq_len", 256), self.scheduler.config.get("max_image_seq_len", 4096), self.scheduler.config.get("base_shift", 0.5), self.scheduler.config.get("max_shift", 1.15), ) timesteps, num_inference_steps = retrieve_timesteps( self.scheduler, num_inference_steps, device, sigmas=sigmas, mu=mu, ) num_warmup_steps = max(len(timesteps) - num_inference_steps * self.scheduler.order, 0) self._num_timesteps = len(timesteps) # handle guidance if self.transformer.config.guidance_embeds and guidance_scale is None: raise ValueError("guidance_scale is required for guidance-distilled model.") elif self.transformer.config.guidance_embeds: guidance = torch.full([1], guidance_scale, device=device, dtype=torch.float32) guidance = guidance.expand(latents.shape[0]) elif not self.transformer.config.guidance_embeds and guidance_scale is not None: logger.warning( f"guidance_scale is passed as {guidance_scale}, but ignored since the model is not guidance-distilled." ) guidance = None elif not self.transformer.config.guidance_embeds and guidance_scale is None: guidance = None if self.attention_kwargs is None: self._attention_kwargs = {} # 6. Denoising loop self.scheduler.set_begin_index(0) with self.progress_bar(total=num_inference_steps) as progress_bar: for i, t in enumerate(timesteps): if self.interrupt: continue self._current_timestep = t # broadcast to batch dimension in a way that's compatible with ONNX/Core ML timestep = t.expand(latents.shape[0]).to(latents.dtype) with self.transformer.cache_context("cond"): noise_pred = self.transformer( hidden_states=latents, timestep=timestep / 1000, guidance=guidance, encoder_hidden_states_mask=prompt_embeds_mask, encoder_hidden_states=prompt_embeds, img_shapes=img_shapes, attention_kwargs=self.attention_kwargs, return_dict=False, )[0] if do_true_cfg: with self.transformer.cache_context("uncond"): neg_noise_pred = self.transformer( hidden_states=latents, timestep=timestep / 1000, guidance=guidance, encoder_hidden_states_mask=negative_prompt_embeds_mask, encoder_hidden_states=negative_prompt_embeds, img_shapes=img_shapes, attention_kwargs=self.attention_kwargs, return_dict=False, )[0] comb_pred = neg_noise_pred + true_cfg_scale * (noise_pred - neg_noise_pred) cond_norm = torch.norm(noise_pred, dim=-1, keepdim=True) noise_norm = torch.norm(comb_pred, dim=-1, keepdim=True) noise_pred = comb_pred * (cond_norm / noise_norm) # compute the previous noisy sample x_t -> x_t-1 latents_dtype = latents.dtype latents = self.scheduler.step(noise_pred, t, latents, return_dict=False)[0] if latents.dtype != latents_dtype: if torch.backends.mps.is_available(): # some platforms (eg. apple mps) misbehave due to a pytorch bug: https://github.com/pytorch/pytorch/pull/99272 latents = latents.to(latents_dtype) if callback_on_step_end is not None: callback_kwargs = {} for k in callback_on_step_end_tensor_inputs: callback_kwargs[k] = locals()[k] callback_outputs = callback_on_step_end(self, i, t, callback_kwargs) latents = callback_outputs.pop("latents", latents) prompt_embeds = callback_outputs.pop("prompt_embeds", prompt_embeds) # call the callback, if provided if i == len(timesteps) - 1 or ((i + 1) > num_warmup_steps and (i + 1) % self.scheduler.order == 0): progress_bar.update() if XLA_AVAILABLE: xm.mark_step() self._current_timestep = None if output_type == "latent": image = latents else: latents = self._unpack_latents(latents, height, width, self.vae_scale_factor) latents = latents.to(self.vae.dtype) latents_mean = ( torch.tensor(self.vae.config.latents_mean) .view(1, self.vae.config.z_dim, 1, 1, 1) .to(latents.device, latents.dtype) ) latents_std = 1.0 / torch.tensor(self.vae.config.latents_std).view(1, self.vae.config.z_dim, 1, 1, 1).to( latents.device, latents.dtype ) latents = latents / latents_std + latents_mean image = self.vae.decode(latents, return_dict=False)[0][:, :, 0] image = self.image_processor.postprocess(image, output_type=output_type) # Offload all models self.maybe_free_model_hooks() if not return_dict: return (image,) return QwenImagePipelineOutput(images=image)
{ "repo_id": "huggingface/diffusers", "file_path": "src/diffusers/pipelines/qwenimage/pipeline_qwenimage.py", "license": "Apache License 2.0", "lines": 665, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
huggingface/diffusers:src/diffusers/modular_pipelines/flux/before_denoise.py
# Copyright 2025 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import inspect import numpy as np import torch from ...pipelines import FluxPipeline from ...schedulers import FlowMatchEulerDiscreteScheduler from ...utils import logging from ...utils.torch_utils import randn_tensor from ..modular_pipeline import ModularPipelineBlocks, PipelineState from ..modular_pipeline_utils import ComponentSpec, InputParam, OutputParam from .modular_pipeline import FluxModularPipeline logger = logging.get_logger(__name__) # pylint: disable=invalid-name # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.retrieve_timesteps def retrieve_timesteps( scheduler, num_inference_steps: int | None = None, device: str | torch.device | None = None, timesteps: list[int] | None = None, sigmas: list[float] | None = None, **kwargs, ): r""" Calls the scheduler's `set_timesteps` method and retrieves timesteps from the scheduler after the call. Handles custom timesteps. Any kwargs will be supplied to `scheduler.set_timesteps`. Args: scheduler (`SchedulerMixin`): The scheduler to get timesteps from. num_inference_steps (`int`): The number of diffusion steps used when generating samples with a pre-trained model. If used, `timesteps` must be `None`. device (`str` or `torch.device`, *optional*): The device to which the timesteps should be moved to. If `None`, the timesteps are not moved. timesteps (`list[int]`, *optional*): Custom timesteps used to override the timestep spacing strategy of the scheduler. If `timesteps` is passed, `num_inference_steps` and `sigmas` must be `None`. sigmas (`list[float]`, *optional*): Custom sigmas used to override the timestep spacing strategy of the scheduler. If `sigmas` is passed, `num_inference_steps` and `timesteps` must be `None`. Returns: `tuple[torch.Tensor, int]`: A tuple where the first element is the timestep schedule from the scheduler and the second element is the number of inference steps. """ if timesteps is not None and sigmas is not None: raise ValueError("Only one of `timesteps` or `sigmas` can be passed. Please choose one to set custom values") if timesteps is not None: accepts_timesteps = "timesteps" in set(inspect.signature(scheduler.set_timesteps).parameters.keys()) if not accepts_timesteps: raise ValueError( f"The current scheduler class {scheduler.__class__}'s `set_timesteps` does not support custom" f" timestep schedules. Please check whether you are using the correct scheduler." ) scheduler.set_timesteps(timesteps=timesteps, device=device, **kwargs) timesteps = scheduler.timesteps num_inference_steps = len(timesteps) elif sigmas is not None: accept_sigmas = "sigmas" in set(inspect.signature(scheduler.set_timesteps).parameters.keys()) if not accept_sigmas: raise ValueError( f"The current scheduler class {scheduler.__class__}'s `set_timesteps` does not support custom" f" sigmas schedules. Please check whether you are using the correct scheduler." ) scheduler.set_timesteps(sigmas=sigmas, device=device, **kwargs) timesteps = scheduler.timesteps num_inference_steps = len(timesteps) else: scheduler.set_timesteps(num_inference_steps, device=device, **kwargs) timesteps = scheduler.timesteps return timesteps, num_inference_steps # Copied from diffusers.pipelines.flux.pipeline_flux.calculate_shift def calculate_shift( image_seq_len, base_seq_len: int = 256, max_seq_len: int = 4096, base_shift: float = 0.5, max_shift: float = 1.15, ): m = (max_shift - base_shift) / (max_seq_len - base_seq_len) b = base_shift - m * base_seq_len mu = image_seq_len * m + b return mu # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion_img2img.retrieve_latents def retrieve_latents( encoder_output: torch.Tensor, generator: torch.Generator | None = None, sample_mode: str = "sample" ): if hasattr(encoder_output, "latent_dist") and sample_mode == "sample": return encoder_output.latent_dist.sample(generator) elif hasattr(encoder_output, "latent_dist") and sample_mode == "argmax": return encoder_output.latent_dist.mode() elif hasattr(encoder_output, "latents"): return encoder_output.latents else: raise AttributeError("Could not access latents of provided encoder_output") def _get_initial_timesteps_and_optionals( transformer, scheduler, batch_size, height, width, vae_scale_factor, num_inference_steps, guidance_scale, sigmas, device, ): image_seq_len = (int(height) // vae_scale_factor // 2) * (int(width) // vae_scale_factor // 2) sigmas = np.linspace(1.0, 1 / num_inference_steps, num_inference_steps) if sigmas is None else sigmas if hasattr(scheduler.config, "use_flow_sigmas") and scheduler.config.use_flow_sigmas: sigmas = None mu = calculate_shift( image_seq_len, scheduler.config.get("base_image_seq_len", 256), scheduler.config.get("max_image_seq_len", 4096), scheduler.config.get("base_shift", 0.5), scheduler.config.get("max_shift", 1.15), ) timesteps, num_inference_steps = retrieve_timesteps(scheduler, num_inference_steps, device, sigmas=sigmas, mu=mu) if transformer.config.guidance_embeds: guidance = torch.full([1], guidance_scale, device=device, dtype=torch.float32) guidance = guidance.expand(batch_size) else: guidance = None return timesteps, num_inference_steps, sigmas, guidance class FluxSetTimestepsStep(ModularPipelineBlocks): model_name = "flux" @property def expected_components(self) -> list[ComponentSpec]: return [ComponentSpec("scheduler", FlowMatchEulerDiscreteScheduler)] @property def description(self) -> str: return "Step that sets the scheduler's timesteps for inference" @property def inputs(self) -> list[InputParam]: return [ InputParam("num_inference_steps", default=50), InputParam("timesteps"), InputParam("sigmas"), InputParam("guidance_scale", default=3.5), InputParam("latents", type_hint=torch.Tensor), InputParam("num_images_per_prompt", default=1), InputParam("height", type_hint=int), InputParam("width", type_hint=int), InputParam( "batch_size", required=True, type_hint=int, 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.", ), ] @property def intermediate_outputs(self) -> list[OutputParam]: return [ OutputParam("timesteps", type_hint=torch.Tensor, description="The timesteps to use for inference"), OutputParam( "num_inference_steps", type_hint=int, description="The number of denoising steps to perform at inference time", ), OutputParam("guidance", type_hint=torch.Tensor, description="Optional guidance to be used."), ] @torch.no_grad() def __call__(self, components: FluxModularPipeline, state: PipelineState) -> PipelineState: block_state = self.get_block_state(state) block_state.device = components._execution_device scheduler = components.scheduler transformer = components.transformer batch_size = block_state.batch_size * block_state.num_images_per_prompt timesteps, num_inference_steps, sigmas, guidance = _get_initial_timesteps_and_optionals( transformer, scheduler, batch_size, block_state.height, block_state.width, components.vae_scale_factor, block_state.num_inference_steps, block_state.guidance_scale, block_state.sigmas, block_state.device, ) block_state.timesteps = timesteps block_state.num_inference_steps = num_inference_steps block_state.sigmas = sigmas block_state.guidance = guidance # We set the index here to remove DtoH sync, helpful especially during compilation. # Check out more details here: https://github.com/huggingface/diffusers/pull/11696 components.scheduler.set_begin_index(0) self.set_block_state(state, block_state) return components, state class FluxImg2ImgSetTimestepsStep(ModularPipelineBlocks): model_name = "flux" @property def expected_components(self) -> list[ComponentSpec]: return [ComponentSpec("scheduler", FlowMatchEulerDiscreteScheduler)] @property def description(self) -> str: return "Step that sets the scheduler's timesteps for inference" @property def inputs(self) -> list[InputParam]: return [ InputParam("num_inference_steps", default=50), InputParam("timesteps"), InputParam("sigmas"), InputParam("strength", default=0.6), InputParam("guidance_scale", default=3.5), InputParam("num_images_per_prompt", default=1), InputParam("height", type_hint=int), InputParam("width", type_hint=int), InputParam( "batch_size", required=True, type_hint=int, 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.", ), ] @property def intermediate_outputs(self) -> list[OutputParam]: return [ OutputParam("timesteps", type_hint=torch.Tensor, description="The timesteps to use for inference"), OutputParam( "num_inference_steps", type_hint=int, description="The number of denoising steps to perform at inference time", ), OutputParam("guidance", type_hint=torch.Tensor, description="Optional guidance to be used."), ] @staticmethod # Copied from diffusers.pipelines.stable_diffusion_3.pipeline_stable_diffusion_3_img2img.StableDiffusion3Img2ImgPipeline.get_timesteps with self.scheduler->scheduler def get_timesteps(scheduler, num_inference_steps, strength, device): # get the original timestep using init_timestep init_timestep = min(num_inference_steps * strength, num_inference_steps) t_start = int(max(num_inference_steps - init_timestep, 0)) timesteps = scheduler.timesteps[t_start * scheduler.order :] if hasattr(scheduler, "set_begin_index"): scheduler.set_begin_index(t_start * scheduler.order) return timesteps, num_inference_steps - t_start @torch.no_grad() def __call__(self, components: FluxModularPipeline, state: PipelineState) -> PipelineState: block_state = self.get_block_state(state) block_state.device = components._execution_device block_state.height = block_state.height or components.default_height block_state.width = block_state.width or components.default_width scheduler = components.scheduler transformer = components.transformer batch_size = block_state.batch_size * block_state.num_images_per_prompt timesteps, num_inference_steps, sigmas, guidance = _get_initial_timesteps_and_optionals( transformer, scheduler, batch_size, block_state.height, block_state.width, components.vae_scale_factor, block_state.num_inference_steps, block_state.guidance_scale, block_state.sigmas, block_state.device, ) timesteps, num_inference_steps = self.get_timesteps( scheduler, num_inference_steps, block_state.strength, block_state.device ) block_state.timesteps = timesteps block_state.num_inference_steps = num_inference_steps block_state.sigmas = sigmas block_state.guidance = guidance self.set_block_state(state, block_state) return components, state class FluxPrepareLatentsStep(ModularPipelineBlocks): model_name = "flux" @property def expected_components(self) -> list[ComponentSpec]: return [] @property def description(self) -> str: return "Prepare latents step that prepares the latents for the text-to-image generation process" @property def inputs(self) -> list[InputParam]: return [ InputParam("height", type_hint=int), InputParam("width", type_hint=int), InputParam("latents", type_hint=torch.Tensor | None), InputParam("num_images_per_prompt", type_hint=int, default=1), InputParam("generator"), InputParam( "batch_size", required=True, type_hint=int, 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.", ), InputParam("dtype", type_hint=torch.dtype, description="The dtype of the model inputs"), ] @property def intermediate_outputs(self) -> list[OutputParam]: return [ OutputParam( "latents", type_hint=torch.Tensor, description="The initial latents to use for the denoising process" ), ] @staticmethod def check_inputs(components, block_state): if (block_state.height is not None and block_state.height % (components.vae_scale_factor * 2) != 0) or ( block_state.width is not None and block_state.width % (components.vae_scale_factor * 2) != 0 ): logger.warning( f"`height` and `width` have to be divisible by {components.vae_scale_factor} but are {block_state.height} and {block_state.width}." ) @staticmethod def prepare_latents( comp, batch_size, num_channels_latents, height, width, dtype, device, generator, latents=None, ): height = 2 * (int(height) // (comp.vae_scale_factor * 2)) width = 2 * (int(width) // (comp.vae_scale_factor * 2)) shape = (batch_size, num_channels_latents, height, width) if latents is not None: return latents.to(device=device, dtype=dtype) if isinstance(generator, list) and len(generator) != batch_size: raise ValueError( f"You have passed a list of generators of length {len(generator)}, but requested an effective batch" f" size of {batch_size}. Make sure the batch size matches the length of the generators." ) # TODO: move packing latents code to a patchifier similar to Qwen latents = randn_tensor(shape, generator=generator, device=device, dtype=dtype) latents = FluxPipeline._pack_latents(latents, batch_size, num_channels_latents, height, width) return latents @torch.no_grad() def __call__(self, components: FluxModularPipeline, state: PipelineState) -> PipelineState: block_state = self.get_block_state(state) block_state.height = block_state.height or components.default_height block_state.width = block_state.width or components.default_width block_state.device = components._execution_device block_state.num_channels_latents = components.num_channels_latents self.check_inputs(components, block_state) batch_size = block_state.batch_size * block_state.num_images_per_prompt block_state.latents = self.prepare_latents( components, batch_size, block_state.num_channels_latents, block_state.height, block_state.width, block_state.dtype, block_state.device, block_state.generator, block_state.latents, ) self.set_block_state(state, block_state) return components, state class FluxImg2ImgPrepareLatentsStep(ModularPipelineBlocks): model_name = "flux" @property def description(self) -> str: return "Step that adds noise to image latents for image-to-image. Should be run after `set_timesteps`," " `prepare_latents`. Both noise and image latents should already be patchified." @property def expected_components(self) -> list[ComponentSpec]: return [ComponentSpec("scheduler", FlowMatchEulerDiscreteScheduler)] @property def inputs(self) -> list[InputParam]: return [ InputParam( name="latents", required=True, type_hint=torch.Tensor, description="The initial random noised, can be generated in prepare latent step.", ), InputParam( name="image_latents", required=True, type_hint=torch.Tensor, description="The image latents to use for the denoising process. Can be generated in vae encoder and packed in input step.", ), InputParam( name="timesteps", required=True, type_hint=torch.Tensor, description="The timesteps to use for the denoising process. Can be generated in set_timesteps step.", ), ] @property def intermediate_outputs(self) -> list[OutputParam]: return [ OutputParam( name="initial_noise", type_hint=torch.Tensor, description="The initial random noised used for inpainting denoising.", ), ] @staticmethod def check_inputs(image_latents, latents): if image_latents.shape[0] != latents.shape[0]: raise ValueError( f"`image_latents` must have have same batch size as `latents`, but got {image_latents.shape[0]} and {latents.shape[0]}" ) if image_latents.ndim != 3: raise ValueError(f"`image_latents` must have 3 dimensions (patchified), but got {image_latents.ndim}") @torch.no_grad() def __call__(self, components: FluxModularPipeline, state: PipelineState) -> PipelineState: block_state = self.get_block_state(state) self.check_inputs(image_latents=block_state.image_latents, latents=block_state.latents) # prepare latent timestep latent_timestep = block_state.timesteps[:1].repeat(block_state.latents.shape[0]) # make copy of initial_noise block_state.initial_noise = block_state.latents # scale noise block_state.latents = components.scheduler.scale_noise( block_state.image_latents, latent_timestep, block_state.latents ) self.set_block_state(state, block_state) return components, state class FluxRoPEInputsStep(ModularPipelineBlocks): model_name = "flux" @property def description(self) -> str: return "Step that prepares the RoPE inputs for the denoising process. Should be placed after text encoder and latent preparation steps." @property def inputs(self) -> list[InputParam]: return [ InputParam(name="height", required=True), InputParam(name="width", required=True), InputParam(name="prompt_embeds"), ] @property def intermediate_outputs(self) -> list[OutputParam]: return [ OutputParam( name="txt_ids", kwargs_type="denoiser_input_fields", type_hint=list[int], description="The sequence lengths of the prompt embeds, used for RoPE calculation.", ), OutputParam( name="img_ids", kwargs_type="denoiser_input_fields", type_hint=list[int], description="The sequence lengths of the image latents, used for RoPE calculation.", ), ] def __call__(self, components: FluxModularPipeline, state: PipelineState) -> PipelineState: block_state = self.get_block_state(state) prompt_embeds = block_state.prompt_embeds device, dtype = prompt_embeds.device, prompt_embeds.dtype block_state.txt_ids = torch.zeros(prompt_embeds.shape[1], 3).to( device=prompt_embeds.device, dtype=prompt_embeds.dtype ) height = 2 * (int(block_state.height) // (components.vae_scale_factor * 2)) width = 2 * (int(block_state.width) // (components.vae_scale_factor * 2)) block_state.img_ids = FluxPipeline._prepare_latent_image_ids(None, height // 2, width // 2, device, dtype) self.set_block_state(state, block_state) return components, state class FluxKontextRoPEInputsStep(ModularPipelineBlocks): model_name = "flux-kontext" @property def description(self) -> str: return "Step that prepares the RoPE inputs for the denoising process of Flux Kontext. Should be placed after text encoder and latent preparation steps." @property def inputs(self) -> list[InputParam]: return [ InputParam(name="image_height"), InputParam(name="image_width"), InputParam(name="height"), InputParam(name="width"), InputParam(name="prompt_embeds"), ] @property def intermediate_outputs(self) -> list[OutputParam]: return [ OutputParam( name="txt_ids", kwargs_type="denoiser_input_fields", type_hint=list[int], description="The sequence lengths of the prompt embeds, used for RoPE calculation.", ), OutputParam( name="img_ids", kwargs_type="denoiser_input_fields", type_hint=list[int], description="The sequence lengths of the image latents, used for RoPE calculation.", ), ] def __call__(self, components: FluxModularPipeline, state: PipelineState) -> PipelineState: block_state = self.get_block_state(state) prompt_embeds = block_state.prompt_embeds device, dtype = prompt_embeds.device, prompt_embeds.dtype block_state.txt_ids = torch.zeros(prompt_embeds.shape[1], 3).to( device=prompt_embeds.device, dtype=prompt_embeds.dtype ) img_ids = None if ( getattr(block_state, "image_height", None) is not None and getattr(block_state, "image_width", None) is not None ): image_latent_height = 2 * (int(block_state.image_height) // (components.vae_scale_factor * 2)) image_latent_width = 2 * (int(block_state.image_width) // (components.vae_scale_factor * 2)) img_ids = FluxPipeline._prepare_latent_image_ids( None, image_latent_height // 2, image_latent_width // 2, device, dtype ) # image ids are the same as latent ids with the first dimension set to 1 instead of 0 img_ids[..., 0] = 1 height = 2 * (int(block_state.height) // (components.vae_scale_factor * 2)) width = 2 * (int(block_state.width) // (components.vae_scale_factor * 2)) latent_ids = FluxPipeline._prepare_latent_image_ids(None, height // 2, width // 2, device, dtype) if img_ids is not None: latent_ids = torch.cat([latent_ids, img_ids], dim=0) block_state.img_ids = latent_ids self.set_block_state(state, block_state) return components, state
{ "repo_id": "huggingface/diffusers", "file_path": "src/diffusers/modular_pipelines/flux/before_denoise.py", "license": "Apache License 2.0", "lines": 522, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
huggingface/diffusers:src/diffusers/modular_pipelines/flux/decoders.py
# Copyright 2025 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from typing import Any import numpy as np import PIL import torch from ...configuration_utils import FrozenDict from ...models import AutoencoderKL from ...utils import logging from ...video_processor import VaeImageProcessor from ..modular_pipeline import ModularPipelineBlocks, PipelineState from ..modular_pipeline_utils import ComponentSpec, InputParam, OutputParam logger = logging.get_logger(__name__) # pylint: disable=invalid-name def _unpack_latents(latents, height, width, vae_scale_factor): batch_size, num_patches, channels = latents.shape # VAE applies 8x compression on images but we must also account for packing which requires # latent height and width to be divisible by 2. height = 2 * (int(height) // (vae_scale_factor * 2)) width = 2 * (int(width) // (vae_scale_factor * 2)) latents = latents.view(batch_size, height // 2, width // 2, channels // 4, 2, 2) latents = latents.permute(0, 3, 1, 4, 2, 5) latents = latents.reshape(batch_size, channels // (2 * 2), height, width) return latents class FluxDecodeStep(ModularPipelineBlocks): model_name = "flux" @property def expected_components(self) -> list[ComponentSpec]: return [ ComponentSpec("vae", AutoencoderKL), ComponentSpec( "image_processor", VaeImageProcessor, config=FrozenDict({"vae_scale_factor": 16}), default_creation_method="from_config", ), ] @property def description(self) -> str: return "Step that decodes the denoised latents into images" @property def inputs(self) -> list[tuple[str, Any]]: return [ InputParam("output_type", default="pil"), InputParam("height", default=1024), InputParam("width", default=1024), InputParam( "latents", required=True, type_hint=torch.Tensor, description="The denoised latents from the denoising step", ), ] @property def intermediate_outputs(self) -> list[str]: return [ OutputParam( "images", type_hint=list[PIL.Image.Image] | torch.Tensor | np.ndarray, description="The generated images, can be a list of PIL.Image.Image, torch.Tensor or a numpy array", ) ] @torch.no_grad() def __call__(self, components, state: PipelineState) -> PipelineState: block_state = self.get_block_state(state) vae = components.vae if not block_state.output_type == "latent": latents = block_state.latents latents = _unpack_latents(latents, block_state.height, block_state.width, components.vae_scale_factor) latents = (latents / vae.config.scaling_factor) + vae.config.shift_factor block_state.images = vae.decode(latents, return_dict=False)[0] block_state.images = components.image_processor.postprocess( block_state.images, output_type=block_state.output_type ) else: block_state.images = block_state.latents self.set_block_state(state, block_state) return components, state
{ "repo_id": "huggingface/diffusers", "file_path": "src/diffusers/modular_pipelines/flux/decoders.py", "license": "Apache License 2.0", "lines": 88, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
huggingface/diffusers:src/diffusers/modular_pipelines/flux/denoise.py
# Copyright 2025 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from typing import Any import torch from ...models import FluxTransformer2DModel from ...schedulers import FlowMatchEulerDiscreteScheduler from ...utils import logging from ..modular_pipeline import ( BlockState, LoopSequentialPipelineBlocks, ModularPipelineBlocks, PipelineState, ) from ..modular_pipeline_utils import ComponentSpec, InputParam, OutputParam from .modular_pipeline import FluxModularPipeline logger = logging.get_logger(__name__) # pylint: disable=invalid-name class FluxLoopDenoiser(ModularPipelineBlocks): model_name = "flux" @property def expected_components(self) -> list[ComponentSpec]: return [ComponentSpec("transformer", FluxTransformer2DModel)] @property def description(self) -> str: return ( "Step within the denoising loop that denoise the latents. " "This block should be used to compose the `sub_blocks` attribute of a `LoopSequentialPipelineBlocks` " "object (e.g. `FluxDenoiseLoopWrapper`)" ) @property def inputs(self) -> list[tuple[str, Any]]: return [ InputParam("joint_attention_kwargs"), InputParam( "latents", required=True, type_hint=torch.Tensor, description="The initial latents to use for the denoising process. Can be generated in prepare_latent step.", ), InputParam( "guidance", required=False, type_hint=torch.Tensor, description="Guidance scale as a tensor", ), InputParam( "prompt_embeds", required=True, type_hint=torch.Tensor, description="Prompt embeddings", ), InputParam( "pooled_prompt_embeds", required=True, type_hint=torch.Tensor, description="Pooled prompt embeddings", ), InputParam( "txt_ids", required=True, type_hint=torch.Tensor, description="IDs computed from text sequence needed for RoPE", ), InputParam( "img_ids", required=True, type_hint=torch.Tensor, description="IDs computed from image sequence needed for RoPE", ), ] @torch.no_grad() def __call__( self, components: FluxModularPipeline, block_state: BlockState, i: int, t: torch.Tensor ) -> PipelineState: noise_pred = components.transformer( hidden_states=block_state.latents, timestep=t.flatten() / 1000, guidance=block_state.guidance, encoder_hidden_states=block_state.prompt_embeds, pooled_projections=block_state.pooled_prompt_embeds, joint_attention_kwargs=block_state.joint_attention_kwargs, txt_ids=block_state.txt_ids, img_ids=block_state.img_ids, return_dict=False, )[0] block_state.noise_pred = noise_pred return components, block_state class FluxKontextLoopDenoiser(ModularPipelineBlocks): model_name = "flux-kontext" @property def expected_components(self) -> list[ComponentSpec]: return [ComponentSpec("transformer", FluxTransformer2DModel)] @property def description(self) -> str: return ( "Step within the denoising loop that denoise the latents for Flux Kontext. " "This block should be used to compose the `sub_blocks` attribute of a `LoopSequentialPipelineBlocks` " "object (e.g. `FluxDenoiseLoopWrapper`)" ) @property def inputs(self) -> list[tuple[str, Any]]: return [ InputParam("joint_attention_kwargs"), InputParam( "latents", required=True, type_hint=torch.Tensor, description="The initial latents to use for the denoising process. Can be generated in prepare_latent step.", ), InputParam( "image_latents", type_hint=torch.Tensor, description="Image latents to use for the denoising process. Can be generated in prepare_latent step.", ), InputParam( "guidance", required=False, type_hint=torch.Tensor, description="Guidance scale as a tensor", ), InputParam( "prompt_embeds", required=True, type_hint=torch.Tensor, description="Prompt embeddings", ), InputParam( "pooled_prompt_embeds", required=True, type_hint=torch.Tensor, description="Pooled prompt embeddings", ), InputParam( "txt_ids", required=True, type_hint=torch.Tensor, description="IDs computed from text sequence needed for RoPE", ), InputParam( "img_ids", required=True, type_hint=torch.Tensor, description="IDs computed from latent sequence needed for RoPE", ), ] @torch.no_grad() def __call__( self, components: FluxModularPipeline, block_state: BlockState, i: int, t: torch.Tensor ) -> PipelineState: latents = block_state.latents latent_model_input = latents image_latents = block_state.image_latents if image_latents is not None: latent_model_input = torch.cat([latent_model_input, image_latents], dim=1) timestep = t.expand(latents.shape[0]).to(latents.dtype) noise_pred = components.transformer( hidden_states=latent_model_input, timestep=timestep / 1000, guidance=block_state.guidance, encoder_hidden_states=block_state.prompt_embeds, pooled_projections=block_state.pooled_prompt_embeds, joint_attention_kwargs=block_state.joint_attention_kwargs, txt_ids=block_state.txt_ids, img_ids=block_state.img_ids, return_dict=False, )[0] noise_pred = noise_pred[:, : latents.size(1)] block_state.noise_pred = noise_pred return components, block_state class FluxLoopAfterDenoiser(ModularPipelineBlocks): model_name = "flux" @property def expected_components(self) -> list[ComponentSpec]: return [ComponentSpec("scheduler", FlowMatchEulerDiscreteScheduler)] @property def description(self) -> str: return ( "step within the denoising loop that update the latents. " "This block should be used to compose the `sub_blocks` attribute of a `LoopSequentialPipelineBlocks` " "object (e.g. `FluxDenoiseLoopWrapper`)" ) @property def inputs(self) -> list[tuple[str, Any]]: return [] @property def intermediate_inputs(self) -> list[str]: return [InputParam("generator")] @property def intermediate_outputs(self) -> list[OutputParam]: return [OutputParam("latents", type_hint=torch.Tensor, description="The denoised latents")] @torch.no_grad() def __call__(self, components: FluxModularPipeline, block_state: BlockState, i: int, t: torch.Tensor): # Perform scheduler step using the predicted output latents_dtype = block_state.latents.dtype block_state.latents = components.scheduler.step( block_state.noise_pred, t, block_state.latents, return_dict=False, )[0] if block_state.latents.dtype != latents_dtype: block_state.latents = block_state.latents.to(latents_dtype) return components, block_state class FluxDenoiseLoopWrapper(LoopSequentialPipelineBlocks): model_name = "flux" @property def description(self) -> str: return ( "Pipeline block that iteratively denoise the latents over `timesteps`. " "The specific steps with each iteration can be customized with `sub_blocks` attributes" ) @property def loop_expected_components(self) -> list[ComponentSpec]: return [ ComponentSpec("scheduler", FlowMatchEulerDiscreteScheduler), ComponentSpec("transformer", FluxTransformer2DModel), ] @property def loop_inputs(self) -> list[InputParam]: return [ InputParam( "timesteps", required=True, type_hint=torch.Tensor, description="The timesteps to use for the denoising process. Can be generated in set_timesteps step.", ), InputParam( "num_inference_steps", required=True, type_hint=int, description="The number of inference steps to use for the denoising process. Can be generated in set_timesteps step.", ), ] @torch.no_grad() def __call__(self, components: FluxModularPipeline, state: PipelineState) -> PipelineState: block_state = self.get_block_state(state) block_state.num_warmup_steps = max( len(block_state.timesteps) - block_state.num_inference_steps * components.scheduler.order, 0 ) with self.progress_bar(total=block_state.num_inference_steps) as progress_bar: for i, t in enumerate(block_state.timesteps): components, block_state = self.loop_step(components, block_state, i=i, t=t) if i == len(block_state.timesteps) - 1 or ( (i + 1) > block_state.num_warmup_steps and (i + 1) % components.scheduler.order == 0 ): progress_bar.update() self.set_block_state(state, block_state) return components, state class FluxDenoiseStep(FluxDenoiseLoopWrapper): block_classes = [FluxLoopDenoiser, FluxLoopAfterDenoiser] block_names = ["denoiser", "after_denoiser"] @property def description(self) -> str: return ( "Denoise step that iteratively denoise the latents. \n" "Its loop logic is defined in `FluxDenoiseLoopWrapper.__call__` method \n" "At each iteration, it runs blocks defined in `sub_blocks` sequentially:\n" " - `FluxLoopDenoiser`\n" " - `FluxLoopAfterDenoiser`\n" "This block supports both text2image and img2img tasks." ) class FluxKontextDenoiseStep(FluxDenoiseLoopWrapper): model_name = "flux-kontext" block_classes = [FluxKontextLoopDenoiser, FluxLoopAfterDenoiser] block_names = ["denoiser", "after_denoiser"] @property def description(self) -> str: return ( "Denoise step that iteratively denoise the latents. \n" "Its loop logic is defined in `FluxDenoiseLoopWrapper.__call__` method \n" "At each iteration, it runs blocks defined in `sub_blocks` sequentially:\n" " - `FluxKontextLoopDenoiser`\n" " - `FluxLoopAfterDenoiser`\n" "This block supports both text2image and img2img tasks." )
{ "repo_id": "huggingface/diffusers", "file_path": "src/diffusers/modular_pipelines/flux/denoise.py", "license": "Apache License 2.0", "lines": 285, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
huggingface/diffusers:src/diffusers/modular_pipelines/flux/encoders.py
# Copyright 2025 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import html import regex as re import torch from transformers import CLIPTextModel, CLIPTokenizer, T5EncoderModel, T5TokenizerFast from ...configuration_utils import FrozenDict from ...image_processor import VaeImageProcessor, is_valid_image, is_valid_image_imagelist from ...loaders import FluxLoraLoaderMixin, TextualInversionLoaderMixin from ...models import AutoencoderKL from ...utils import USE_PEFT_BACKEND, is_ftfy_available, logging, scale_lora_layers, unscale_lora_layers from ..modular_pipeline import ModularPipelineBlocks, PipelineState from ..modular_pipeline_utils import ComponentSpec, InputParam, OutputParam from .modular_pipeline import FluxModularPipeline if is_ftfy_available(): import ftfy logger = logging.get_logger(__name__) # pylint: disable=invalid-name def basic_clean(text): text = ftfy.fix_text(text) text = html.unescape(html.unescape(text)) return text.strip() def whitespace_clean(text): text = re.sub(r"\s+", " ", text) text = text.strip() return text def prompt_clean(text): text = whitespace_clean(basic_clean(text)) return text # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion_img2img.retrieve_latents def retrieve_latents( encoder_output: torch.Tensor, generator: torch.Generator | None = None, sample_mode: str = "sample" ): if hasattr(encoder_output, "latent_dist") and sample_mode == "sample": return encoder_output.latent_dist.sample(generator) elif hasattr(encoder_output, "latent_dist") and sample_mode == "argmax": return encoder_output.latent_dist.mode() elif hasattr(encoder_output, "latents"): return encoder_output.latents else: raise AttributeError("Could not access latents of provided encoder_output") def encode_vae_image(vae: AutoencoderKL, image: torch.Tensor, generator: torch.Generator, sample_mode="sample"): if isinstance(generator, list): image_latents = [ retrieve_latents(vae.encode(image[i : i + 1]), generator=generator[i], sample_mode=sample_mode) for i in range(image.shape[0]) ] image_latents = torch.cat(image_latents, dim=0) else: image_latents = retrieve_latents(vae.encode(image), generator=generator, sample_mode=sample_mode) image_latents = (image_latents - vae.config.shift_factor) * vae.config.scaling_factor return image_latents class FluxProcessImagesInputStep(ModularPipelineBlocks): model_name = "flux" @property def description(self) -> str: return "Image Preprocess step." @property def expected_components(self) -> list[ComponentSpec]: return [ ComponentSpec( "image_processor", VaeImageProcessor, config=FrozenDict({"vae_scale_factor": 16, "vae_latent_channels": 16}), default_creation_method="from_config", ), ] @property def inputs(self) -> list[InputParam]: return [InputParam("resized_image"), InputParam("image"), InputParam("height"), InputParam("width")] @property def intermediate_outputs(self) -> list[OutputParam]: return [OutputParam(name="processed_image")] @staticmethod def check_inputs(height, width, vae_scale_factor): if height is not None and height % (vae_scale_factor * 2) != 0: raise ValueError(f"Height must be divisible by {vae_scale_factor * 2} but is {height}") if width is not None and width % (vae_scale_factor * 2) != 0: raise ValueError(f"Width must be divisible by {vae_scale_factor * 2} but is {width}") @torch.no_grad() def __call__(self, components: FluxModularPipeline, state: PipelineState): block_state = self.get_block_state(state) if block_state.resized_image is None and block_state.image is None: raise ValueError("`resized_image` and `image` cannot be None at the same time") if block_state.resized_image is None: image = block_state.image self.check_inputs( height=block_state.height, width=block_state.width, vae_scale_factor=components.vae_scale_factor ) height = block_state.height or components.default_height width = block_state.width or components.default_width else: width, height = block_state.resized_image[0].size image = block_state.resized_image block_state.processed_image = components.image_processor.preprocess(image=image, height=height, width=width) self.set_block_state(state, block_state) return components, state class FluxKontextProcessImagesInputStep(ModularPipelineBlocks): model_name = "flux-kontext" @property def description(self) -> str: return ( "Image preprocess step for Flux Kontext. The preprocessed image goes to the VAE.\n" "Kontext works as a T2I model, too, in case no input image is provided." ) @property def expected_components(self) -> list[ComponentSpec]: return [ ComponentSpec( "image_processor", VaeImageProcessor, config=FrozenDict({"vae_scale_factor": 16}), default_creation_method="from_config", ), ] @property def inputs(self) -> list[InputParam]: return [InputParam("image"), InputParam("_auto_resize", type_hint=bool, default=True)] @property def intermediate_outputs(self) -> list[OutputParam]: return [OutputParam(name="processed_image")] @torch.no_grad() def __call__(self, components: FluxModularPipeline, state: PipelineState): from ...pipelines.flux.pipeline_flux_kontext import PREFERRED_KONTEXT_RESOLUTIONS block_state = self.get_block_state(state) images = block_state.image if images is None: block_state.processed_image = None else: multiple_of = components.image_processor.config.vae_scale_factor if not is_valid_image_imagelist(images): raise ValueError(f"Images must be image or list of images but are {type(images)}") if is_valid_image(images): images = [images] img = images[0] image_height, image_width = components.image_processor.get_default_height_width(img) aspect_ratio = image_width / image_height _auto_resize = block_state._auto_resize if _auto_resize: # Kontext is trained on specific resolutions, using one of them is recommended _, image_width, image_height = min( (abs(aspect_ratio - w / h), w, h) for w, h in PREFERRED_KONTEXT_RESOLUTIONS ) image_width = image_width // multiple_of * multiple_of image_height = image_height // multiple_of * multiple_of images = components.image_processor.resize(images, image_height, image_width) block_state.processed_image = components.image_processor.preprocess(images, image_height, image_width) self.set_block_state(state, block_state) return components, state class FluxVaeEncoderStep(ModularPipelineBlocks): model_name = "flux" def __init__( self, input_name: str = "processed_image", output_name: str = "image_latents", sample_mode: str = "sample" ): """Initialize a VAE encoder step for converting images to latent representations. Both the input and output names are configurable so this block can be configured to process to different image inputs (e.g., "processed_image" -> "image_latents", "processed_control_image" -> "control_image_latents"). Args: input_name (str, optional): Name of the input image tensor. Defaults to "processed_image". Examples: "processed_image" or "processed_control_image" output_name (str, optional): Name of the output latent tensor. Defaults to "image_latents". Examples: "image_latents" or "control_image_latents" sample_mode (str, optional): Sampling mode to be used. Examples: # Basic usage with default settings (includes image processor): # FluxImageVaeEncoderDynamicStep() # Custom input/output names for control image: # FluxImageVaeEncoderDynamicStep( input_name="processed_control_image", output_name="control_image_latents" ) """ self._image_input_name = input_name self._image_latents_output_name = output_name self.sample_mode = sample_mode super().__init__() @property def description(self) -> str: return f"Dynamic VAE Encoder step that converts {self._image_input_name} into latent representations {self._image_latents_output_name}.\n" @property def expected_components(self) -> list[ComponentSpec]: components = [ComponentSpec("vae", AutoencoderKL)] return components @property def inputs(self) -> list[InputParam]: inputs = [InputParam(self._image_input_name), InputParam("generator")] return inputs @property def intermediate_outputs(self) -> list[OutputParam]: return [ OutputParam( self._image_latents_output_name, type_hint=torch.Tensor, description="The latents representing the reference image", ) ] @torch.no_grad() def __call__(self, components: FluxModularPipeline, state: PipelineState) -> PipelineState: block_state = self.get_block_state(state) image = getattr(block_state, self._image_input_name) if image is None: setattr(block_state, self._image_latents_output_name, None) else: device = components._execution_device dtype = components.vae.dtype image = image.to(device=device, dtype=dtype) # Encode image into latents image_latents = encode_vae_image( image=image, vae=components.vae, generator=block_state.generator, sample_mode=self.sample_mode ) setattr(block_state, self._image_latents_output_name, image_latents) self.set_block_state(state, block_state) return components, state class FluxTextEncoderStep(ModularPipelineBlocks): model_name = "flux" @property def description(self) -> str: return "Text Encoder step that generate text_embeddings to guide the image generation" @property def expected_components(self) -> list[ComponentSpec]: return [ ComponentSpec("text_encoder", CLIPTextModel), ComponentSpec("tokenizer", CLIPTokenizer), ComponentSpec("text_encoder_2", T5EncoderModel), ComponentSpec("tokenizer_2", T5TokenizerFast), ] @property def inputs(self) -> list[InputParam]: return [ InputParam("prompt"), InputParam("prompt_2"), InputParam("max_sequence_length", type_hint=int, default=512, required=False), InputParam("joint_attention_kwargs"), ] @property def intermediate_outputs(self) -> list[OutputParam]: return [ OutputParam( "prompt_embeds", kwargs_type="denoiser_input_fields", type_hint=torch.Tensor, description="text embeddings used to guide the image generation", ), OutputParam( "pooled_prompt_embeds", kwargs_type="denoiser_input_fields", type_hint=torch.Tensor, description="pooled text embeddings used to guide the image generation", ), ] @staticmethod def check_inputs(block_state): for prompt in [block_state.prompt, block_state.prompt_2]: if prompt is not None and (not isinstance(prompt, str) and not isinstance(prompt, list)): raise ValueError(f"`prompt` or `prompt_2` has to be of type `str` or `list` but is {type(prompt)}") @staticmethod def _get_t5_prompt_embeds(components, prompt: str | list[str], max_sequence_length: int, device: torch.device): dtype = components.text_encoder_2.dtype prompt = [prompt] if isinstance(prompt, str) else prompt if isinstance(components, TextualInversionLoaderMixin): prompt = components.maybe_convert_prompt(prompt, components.tokenizer_2) text_inputs = components.tokenizer_2( prompt, padding="max_length", max_length=max_sequence_length, truncation=True, return_length=False, return_overflowing_tokens=False, return_tensors="pt", ) text_input_ids = text_inputs.input_ids untruncated_ids = components.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 = components.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}" ) prompt_embeds = components.text_encoder_2(text_input_ids.to(device), output_hidden_states=False)[0] prompt_embeds = prompt_embeds.to(dtype=dtype, device=device) return prompt_embeds @staticmethod def _get_clip_prompt_embeds(components, prompt: str | list[str], device: torch.device): prompt = [prompt] if isinstance(prompt, str) else prompt if isinstance(components, TextualInversionLoaderMixin): prompt = components.maybe_convert_prompt(prompt, components.tokenizer) text_inputs = components.tokenizer( prompt, padding="max_length", max_length=components.tokenizer.model_max_length, truncation=True, return_overflowing_tokens=False, return_length=False, return_tensors="pt", ) text_input_ids = text_inputs.input_ids tokenizer_max_length = components.tokenizer.model_max_length untruncated_ids = components.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 = components.tokenizer.batch_decode(untruncated_ids[:, tokenizer_max_length - 1 : -1]) logger.warning( "The following part of your input was truncated because CLIP can only handle sequences up to" f" {tokenizer_max_length} tokens: {removed_text}" ) prompt_embeds = components.text_encoder(text_input_ids.to(device), output_hidden_states=False) # Use pooled output of CLIPTextModel prompt_embeds = prompt_embeds.pooler_output prompt_embeds = prompt_embeds.to(dtype=components.text_encoder.dtype, device=device) return prompt_embeds @staticmethod def encode_prompt( components, prompt: str | list[str], prompt_2: str | list[str], device: torch.device | None = None, prompt_embeds: torch.FloatTensor | None = None, pooled_prompt_embeds: torch.FloatTensor | None = None, max_sequence_length: int = 512, lora_scale: float | None = None, ): device = device or components._execution_device # set lora scale so that monkey patched LoRA # function of text encoder can correctly access it if lora_scale is not None and isinstance(components, FluxLoraLoaderMixin): components._lora_scale = lora_scale # dynamically adjust the LoRA scale if components.text_encoder is not None and USE_PEFT_BACKEND: scale_lora_layers(components.text_encoder, lora_scale) if components.text_encoder_2 is not None and USE_PEFT_BACKEND: scale_lora_layers(components.text_encoder_2, lora_scale) prompt = [prompt] if isinstance(prompt, str) else prompt if prompt_embeds is None: prompt_2 = prompt_2 or prompt prompt_2 = [prompt_2] if isinstance(prompt_2, str) else prompt_2 # We only use the pooled prompt output from the CLIPTextModel pooled_prompt_embeds = FluxTextEncoderStep._get_clip_prompt_embeds( components, prompt=prompt, device=device, ) prompt_embeds = FluxTextEncoderStep._get_t5_prompt_embeds( components, prompt=prompt_2, max_sequence_length=max_sequence_length, device=device, ) if components.text_encoder is not None: if isinstance(components, FluxLoraLoaderMixin) and USE_PEFT_BACKEND: # Retrieve the original scale by scaling back the LoRA layers unscale_lora_layers(components.text_encoder, lora_scale) if components.text_encoder_2 is not None: if isinstance(components, FluxLoraLoaderMixin) and USE_PEFT_BACKEND: # Retrieve the original scale by scaling back the LoRA layers unscale_lora_layers(components.text_encoder_2, lora_scale) return prompt_embeds, pooled_prompt_embeds @torch.no_grad() def __call__(self, components: FluxModularPipeline, state: PipelineState) -> PipelineState: # Get inputs and intermediates block_state = self.get_block_state(state) self.check_inputs(block_state) block_state.device = components._execution_device # Encode input prompt block_state.text_encoder_lora_scale = ( block_state.joint_attention_kwargs.get("scale", None) if block_state.joint_attention_kwargs is not None else None ) block_state.prompt_embeds, block_state.pooled_prompt_embeds = self.encode_prompt( components, prompt=block_state.prompt, prompt_2=None, prompt_embeds=None, pooled_prompt_embeds=None, device=block_state.device, max_sequence_length=block_state.max_sequence_length, lora_scale=block_state.text_encoder_lora_scale, ) # Add outputs self.set_block_state(state, block_state) return components, state
{ "repo_id": "huggingface/diffusers", "file_path": "src/diffusers/modular_pipelines/flux/encoders.py", "license": "Apache License 2.0", "lines": 387, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
huggingface/diffusers:src/diffusers/modular_pipelines/flux/modular_pipeline.py
# Copyright 2025 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from ...loaders import FluxLoraLoaderMixin, TextualInversionLoaderMixin from ...utils import logging from ..modular_pipeline import ModularPipeline logger = logging.get_logger(__name__) # pylint: disable=invalid-name class FluxModularPipeline(ModularPipeline, FluxLoraLoaderMixin, TextualInversionLoaderMixin): """ A ModularPipeline for Flux. > [!WARNING] > This is an experimental feature and is likely to change in the future. """ default_blocks_name = "FluxAutoBlocks" @property def default_height(self): return self.default_sample_size * self.vae_scale_factor @property def default_width(self): return self.default_sample_size * self.vae_scale_factor @property def default_sample_size(self): return 128 @property def vae_scale_factor(self): vae_scale_factor = 8 if getattr(self, "vae", None) is not None: vae_scale_factor = 2 ** (len(self.vae.config.block_out_channels) - 1) return vae_scale_factor @property def num_channels_latents(self): num_channels_latents = 16 if getattr(self, "transformer", None): num_channels_latents = self.transformer.config.in_channels // 4 return num_channels_latents class FluxKontextModularPipeline(FluxModularPipeline): """ A ModularPipeline for Flux Kontext. > [!WARNING] > This is an experimental feature and is likely to change in the future. """ default_blocks_name = "FluxKontextAutoBlocks"
{ "repo_id": "huggingface/diffusers", "file_path": "src/diffusers/modular_pipelines/flux/modular_pipeline.py", "license": "Apache License 2.0", "lines": 50, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
huggingface/diffusers:src/diffusers/modular_pipelines/wan/before_denoise.py
# Copyright 2025 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import inspect import torch from ...models import WanTransformer3DModel from ...schedulers import UniPCMultistepScheduler from ...utils import logging from ...utils.torch_utils import randn_tensor from ..modular_pipeline import ModularPipelineBlocks, PipelineState from ..modular_pipeline_utils import ComponentSpec, InputParam, OutputParam from .modular_pipeline import WanModularPipeline logger = logging.get_logger(__name__) # pylint: disable=invalid-name # TODO(yiyi, aryan): We need another step before text encoder to set the `num_inference_steps` attribute for guider so that # things like when to do guidance and how many conditions to be prepared can be determined. Currently, this is done by # always assuming you want to do guidance in the Guiders. So, negative embeddings are prepared regardless of what the # configuration of guider is. def repeat_tensor_to_batch_size( input_name: str, input_tensor: torch.Tensor, batch_size: int, num_videos_per_prompt: int = 1, ) -> torch.Tensor: """Repeat tensor elements to match the final batch size. This function expands a tensor's batch dimension to match the final batch size (batch_size * num_videos_per_prompt) by repeating each element along dimension 0. The input tensor must have batch size 1 or batch_size. The function will: - If batch size is 1: repeat each element (batch_size * num_videos_per_prompt) times - If batch size equals batch_size: repeat each element num_videos_per_prompt times Args: input_name (str): Name of the input tensor (used for error messages) input_tensor (torch.Tensor): The tensor to repeat. Must have batch size 1 or batch_size. batch_size (int): The base batch size (number of prompts) num_videos_per_prompt (int, optional): Number of videos to generate per prompt. Defaults to 1. Returns: torch.Tensor: The repeated tensor with final batch size (batch_size * num_videos_per_prompt) Raises: ValueError: If input_tensor is not a torch.Tensor or has invalid batch size Examples: tensor = torch.tensor([[1, 2, 3]]) # shape: [1, 3] repeated = repeat_tensor_to_batch_size("image", tensor, batch_size=2, num_videos_per_prompt=2) repeated # tensor([[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]]) - shape: [4, 3] tensor = torch.tensor([[1, 2, 3], [4, 5, 6]]) # shape: [2, 3] repeated = repeat_tensor_to_batch_size("image", tensor, batch_size=2, num_videos_per_prompt=2) repeated # tensor([[1, 2, 3], [1, 2, 3], [4, 5, 6], [4, 5, 6]]) - shape: [4, 3] """ # make sure input is a tensor if not isinstance(input_tensor, torch.Tensor): raise ValueError(f"`{input_name}` must be a tensor") # make sure input tensor e.g. image_latents has batch size 1 or batch_size same as prompts if input_tensor.shape[0] == 1: repeat_by = batch_size * num_videos_per_prompt elif input_tensor.shape[0] == batch_size: repeat_by = num_videos_per_prompt else: raise ValueError( f"`{input_name}` must have have batch size 1 or {batch_size}, but got {input_tensor.shape[0]}" ) # expand the tensor to match the batch_size * num_videos_per_prompt input_tensor = input_tensor.repeat_interleave(repeat_by, dim=0) return input_tensor def calculate_dimension_from_latents( latents: torch.Tensor, vae_scale_factor_temporal: int, vae_scale_factor_spatial: int ) -> tuple[int, int]: """Calculate image dimensions from latent tensor dimensions. This function converts latent temporal and spatial dimensions to image temporal and spatial dimensions by multiplying the latent num_frames/height/width by the VAE scale factor. Args: latents (torch.Tensor): The latent tensor. Must have 4 or 5 dimensions. Expected shapes: [batch, channels, height, width] or [batch, channels, frames, height, width] vae_scale_factor_temporal (int): The scale factor used by the VAE to compress temporal dimension. Typically 4 for most VAEs (video is 4x larger than latents in temporal dimension) vae_scale_factor_spatial (int): The scale factor used by the VAE to compress spatial dimension. Typically 8 for most VAEs (image is 8x larger than latents in each dimension) Returns: tuple[int, int]: The calculated image dimensions as (height, width) Raises: ValueError: If latents tensor doesn't have 4 or 5 dimensions """ if latents.ndim != 5: raise ValueError(f"latents must have 5 dimensions, but got {latents.ndim}") _, _, num_latent_frames, latent_height, latent_width = latents.shape num_frames = (num_latent_frames - 1) * vae_scale_factor_temporal + 1 height = latent_height * vae_scale_factor_spatial width = latent_width * vae_scale_factor_spatial return num_frames, height, width # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.retrieve_timesteps def retrieve_timesteps( scheduler, num_inference_steps: int | None = None, device: str | torch.device | None = None, timesteps: list[int] | None = None, sigmas: list[float] | None = None, **kwargs, ): r""" Calls the scheduler's `set_timesteps` method and retrieves timesteps from the scheduler after the call. Handles custom timesteps. Any kwargs will be supplied to `scheduler.set_timesteps`. Args: scheduler (`SchedulerMixin`): The scheduler to get timesteps from. num_inference_steps (`int`): The number of diffusion steps used when generating samples with a pre-trained model. If used, `timesteps` must be `None`. device (`str` or `torch.device`, *optional*): The device to which the timesteps should be moved to. If `None`, the timesteps are not moved. timesteps (`list[int]`, *optional*): Custom timesteps used to override the timestep spacing strategy of the scheduler. If `timesteps` is passed, `num_inference_steps` and `sigmas` must be `None`. sigmas (`list[float]`, *optional*): Custom sigmas used to override the timestep spacing strategy of the scheduler. If `sigmas` is passed, `num_inference_steps` and `timesteps` must be `None`. Returns: `tuple[torch.Tensor, int]`: A tuple where the first element is the timestep schedule from the scheduler and the second element is the number of inference steps. """ if timesteps is not None and sigmas is not None: raise ValueError("Only one of `timesteps` or `sigmas` can be passed. Please choose one to set custom values") if timesteps is not None: accepts_timesteps = "timesteps" in set(inspect.signature(scheduler.set_timesteps).parameters.keys()) if not accepts_timesteps: raise ValueError( f"The current scheduler class {scheduler.__class__}'s `set_timesteps` does not support custom" f" timestep schedules. Please check whether you are using the correct scheduler." ) scheduler.set_timesteps(timesteps=timesteps, device=device, **kwargs) timesteps = scheduler.timesteps num_inference_steps = len(timesteps) elif sigmas is not None: accept_sigmas = "sigmas" in set(inspect.signature(scheduler.set_timesteps).parameters.keys()) if not accept_sigmas: raise ValueError( f"The current scheduler class {scheduler.__class__}'s `set_timesteps` does not support custom" f" sigmas schedules. Please check whether you are using the correct scheduler." ) scheduler.set_timesteps(sigmas=sigmas, device=device, **kwargs) timesteps = scheduler.timesteps num_inference_steps = len(timesteps) else: scheduler.set_timesteps(num_inference_steps, device=device, **kwargs) timesteps = scheduler.timesteps return timesteps, num_inference_steps class WanTextInputStep(ModularPipelineBlocks): model_name = "wan" @property def description(self) -> str: return ( "Input processing step that:\n" " 1. Determines `batch_size` and `dtype` based on `prompt_embeds`\n" " 2. Adjusts input tensor shapes based on `batch_size` (number of prompts) and `num_videos_per_prompt`\n\n" "All input tensors are expected to have either batch_size=1 or match the batch_size\n" "of prompt_embeds. The tensors will be duplicated across the batch dimension to\n" "have a final batch_size of batch_size * num_videos_per_prompt." ) @property def expected_components(self) -> list[ComponentSpec]: return [ ComponentSpec("transformer", WanTransformer3DModel), ] @property def inputs(self) -> list[InputParam]: return [ InputParam("num_videos_per_prompt", default=1), InputParam( "prompt_embeds", required=True, type_hint=torch.Tensor, description="Pre-generated text embeddings. Can be generated from text_encoder step.", ), InputParam( "negative_prompt_embeds", type_hint=torch.Tensor, description="Pre-generated negative text embeddings. Can be generated from text_encoder step.", ), ] @property def intermediate_outputs(self) -> list[str]: return [ OutputParam( "batch_size", type_hint=int, description="Number of prompts, the final batch size of model inputs should be batch_size * num_videos_per_prompt", ), OutputParam( "dtype", type_hint=torch.dtype, description="Data type of model tensor inputs (determined by `transformer.dtype`)", ), ] def check_inputs(self, components, block_state): if block_state.prompt_embeds is not None and block_state.negative_prompt_embeds is not None: if block_state.prompt_embeds.shape != block_state.negative_prompt_embeds.shape: raise ValueError( "`prompt_embeds` and `negative_prompt_embeds` must have the same shape when passed directly, but" f" got: `prompt_embeds` {block_state.prompt_embeds.shape} != `negative_prompt_embeds`" f" {block_state.negative_prompt_embeds.shape}." ) @torch.no_grad() def __call__(self, components: WanModularPipeline, state: PipelineState) -> PipelineState: block_state = self.get_block_state(state) self.check_inputs(components, block_state) block_state.batch_size = block_state.prompt_embeds.shape[0] block_state.dtype = block_state.prompt_embeds.dtype _, seq_len, _ = block_state.prompt_embeds.shape block_state.prompt_embeds = block_state.prompt_embeds.repeat(1, block_state.num_videos_per_prompt, 1) block_state.prompt_embeds = block_state.prompt_embeds.view( block_state.batch_size * block_state.num_videos_per_prompt, seq_len, -1 ) if block_state.negative_prompt_embeds is not None: _, seq_len, _ = block_state.negative_prompt_embeds.shape block_state.negative_prompt_embeds = block_state.negative_prompt_embeds.repeat( 1, block_state.num_videos_per_prompt, 1 ) block_state.negative_prompt_embeds = block_state.negative_prompt_embeds.view( block_state.batch_size * block_state.num_videos_per_prompt, seq_len, -1 ) self.set_block_state(state, block_state) return components, state class WanAdditionalInputsStep(ModularPipelineBlocks): model_name = "wan" def __init__( self, image_latent_inputs: list[str] = ["image_condition_latents"], additional_batch_inputs: list[str] = [], ): """Initialize a configurable step that standardizes the inputs for the denoising step. It:\n" This step handles multiple common tasks to prepare inputs for the denoising step: 1. For encoded image latents, use it update height/width if None, and expands batch size 2. For additional_batch_inputs: Only expands batch dimensions to match final batch size This is a dynamic block that allows you to configure which inputs to process. Args: image_latent_inputs (list[str], optional): Names of image latent tensors to process. In additional to adjust batch size of these inputs, they will be used to determine height/width. Can be a single string or list of strings. Defaults to ["image_condition_latents"]. additional_batch_inputs (List[str], optional): Names of additional conditional input tensors to expand batch size. These tensors will only have their batch dimensions adjusted to match the final batch size. Can be a single string or list of strings. Defaults to []. Examples: # Configure to process image_condition_latents (default behavior) WanAdditionalInputsStep() # Configure to process image latents and additional batch inputs WanAdditionalInputsStep( image_latent_inputs=["image_condition_latents"], additional_batch_inputs=["image_embeds"] ) """ if not isinstance(image_latent_inputs, list): image_latent_inputs = [image_latent_inputs] if not isinstance(additional_batch_inputs, list): additional_batch_inputs = [additional_batch_inputs] self._image_latent_inputs = image_latent_inputs self._additional_batch_inputs = additional_batch_inputs super().__init__() @property def description(self) -> str: # Functionality section summary_section = ( "Input processing step that:\n" " 1. For image latent inputs: Updates height/width if None, and expands batch size\n" " 2. For additional batch inputs: Expands batch dimensions to match final batch size" ) # Inputs info inputs_info = "" if self._image_latent_inputs or self._additional_batch_inputs: inputs_info = "\n\nConfigured inputs:" if self._image_latent_inputs: inputs_info += f"\n - Image latent inputs: {self._image_latent_inputs}" if self._additional_batch_inputs: inputs_info += f"\n - Additional batch inputs: {self._additional_batch_inputs}" # Placement guidance placement_section = "\n\nThis block should be placed after the encoder steps and the text input step." return summary_section + inputs_info + placement_section @property def inputs(self) -> list[InputParam]: inputs = [ InputParam(name="num_videos_per_prompt", default=1), InputParam(name="batch_size", required=True), InputParam(name="height"), InputParam(name="width"), InputParam(name="num_frames"), ] # Add image latent inputs for image_latent_input_name in self._image_latent_inputs: inputs.append(InputParam(name=image_latent_input_name)) # Add additional batch inputs for input_name in self._additional_batch_inputs: inputs.append(InputParam(name=input_name)) return inputs def __call__(self, components: WanModularPipeline, state: PipelineState) -> PipelineState: block_state = self.get_block_state(state) # Process image latent inputs (height/width calculation, patchify, and batch expansion) for image_latent_input_name in self._image_latent_inputs: image_latent_tensor = getattr(block_state, image_latent_input_name) if image_latent_tensor is None: continue # 1. Calculate num_frames, height/width from latents num_frames, height, width = calculate_dimension_from_latents( image_latent_tensor, components.vae_scale_factor_temporal, components.vae_scale_factor_spatial ) block_state.num_frames = block_state.num_frames or num_frames block_state.height = block_state.height or height block_state.width = block_state.width or width # 3. Expand batch size image_latent_tensor = repeat_tensor_to_batch_size( input_name=image_latent_input_name, input_tensor=image_latent_tensor, num_videos_per_prompt=block_state.num_videos_per_prompt, batch_size=block_state.batch_size, ) setattr(block_state, image_latent_input_name, image_latent_tensor) # Process additional batch inputs (only batch expansion) for input_name in self._additional_batch_inputs: input_tensor = getattr(block_state, input_name) if input_tensor is None: continue # Only expand batch size input_tensor = repeat_tensor_to_batch_size( input_name=input_name, input_tensor=input_tensor, num_videos_per_prompt=block_state.num_videos_per_prompt, batch_size=block_state.batch_size, ) setattr(block_state, input_name, input_tensor) self.set_block_state(state, block_state) return components, state class WanSetTimestepsStep(ModularPipelineBlocks): model_name = "wan" @property def expected_components(self) -> list[ComponentSpec]: return [ ComponentSpec("scheduler", UniPCMultistepScheduler), ] @property def description(self) -> str: return "Step that sets the scheduler's timesteps for inference" @property def inputs(self) -> list[InputParam]: return [ InputParam("num_inference_steps", default=50), InputParam("timesteps"), InputParam("sigmas"), ] @torch.no_grad() def __call__(self, components: WanModularPipeline, state: PipelineState) -> PipelineState: block_state = self.get_block_state(state) device = components._execution_device block_state.timesteps, block_state.num_inference_steps = retrieve_timesteps( components.scheduler, block_state.num_inference_steps, device, block_state.timesteps, block_state.sigmas, ) self.set_block_state(state, block_state) return components, state class WanPrepareLatentsStep(ModularPipelineBlocks): model_name = "wan" @property def description(self) -> str: return "Prepare latents step that prepares the latents for the text-to-video generation process" @property def inputs(self) -> list[InputParam]: return [ InputParam("height", type_hint=int), InputParam("width", type_hint=int), InputParam("num_frames", type_hint=int), InputParam("latents", type_hint=torch.Tensor | None), InputParam("num_videos_per_prompt", type_hint=int, default=1), InputParam("generator"), InputParam( "batch_size", required=True, type_hint=int, description="Number of prompts, the final batch size of model inputs should be `batch_size * num_videos_per_prompt`. Can be generated in input step.", ), InputParam("dtype", type_hint=torch.dtype, description="The dtype of the model inputs"), ] @property def intermediate_outputs(self) -> list[OutputParam]: return [ OutputParam( "latents", type_hint=torch.Tensor, description="The initial latents to use for the denoising process" ) ] @staticmethod def check_inputs(components, block_state): if (block_state.height is not None and block_state.height % components.vae_scale_factor_spatial != 0) or ( block_state.width is not None and block_state.width % components.vae_scale_factor_spatial != 0 ): raise ValueError( f"`height` and `width` have to be divisible by {components.vae_scale_factor_spatial} but are {block_state.height} and {block_state.width}." ) if block_state.num_frames is not None and ( block_state.num_frames < 1 or (block_state.num_frames - 1) % components.vae_scale_factor_temporal != 0 ): raise ValueError( f"`num_frames` has to be greater than 0, and (num_frames - 1) must be divisible by {components.vae_scale_factor_temporal}, but got {block_state.num_frames}." ) @staticmethod # Copied from diffusers.pipelines.wan.pipeline_wan.WanPipeline.prepare_latents with self->comp def prepare_latents( comp, batch_size: int, num_channels_latents: int = 16, height: int = 480, width: int = 832, num_frames: int = 81, dtype: torch.dtype | None = None, device: torch.device | None = None, generator: torch.Generator | list[torch.Generator] | None = None, latents: torch.Tensor | None = None, ) -> torch.Tensor: if latents is not None: return latents.to(device=device, dtype=dtype) num_latent_frames = (num_frames - 1) // comp.vae_scale_factor_temporal + 1 shape = ( batch_size, num_channels_latents, num_latent_frames, int(height) // comp.vae_scale_factor_spatial, int(width) // comp.vae_scale_factor_spatial, ) if isinstance(generator, list) and len(generator) != batch_size: raise ValueError( f"You have passed a list of generators of length {len(generator)}, but requested an effective batch" f" size of {batch_size}. Make sure the batch size matches the length of the generators." ) latents = randn_tensor(shape, generator=generator, device=device, dtype=dtype) return latents @torch.no_grad() def __call__(self, components: WanModularPipeline, state: PipelineState) -> PipelineState: block_state = self.get_block_state(state) self.check_inputs(components, block_state) device = components._execution_device dtype = torch.float32 # Wan latents should be torch.float32 for best quality block_state.height = block_state.height or components.default_height block_state.width = block_state.width or components.default_width block_state.num_frames = block_state.num_frames or components.default_num_frames block_state.latents = self.prepare_latents( components, batch_size=block_state.batch_size * block_state.num_videos_per_prompt, num_channels_latents=components.num_channels_latents, height=block_state.height, width=block_state.width, num_frames=block_state.num_frames, dtype=dtype, device=device, generator=block_state.generator, latents=block_state.latents, ) self.set_block_state(state, block_state) return components, state
{ "repo_id": "huggingface/diffusers", "file_path": "src/diffusers/modular_pipelines/wan/before_denoise.py", "license": "Apache License 2.0", "lines": 460, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
huggingface/diffusers:src/diffusers/modular_pipelines/wan/decoders.py
# Copyright 2025 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from typing import Any import numpy as np import PIL import torch from ...configuration_utils import FrozenDict from ...models import AutoencoderKLWan from ...utils import logging from ...video_processor import VideoProcessor from ..modular_pipeline import ModularPipelineBlocks, PipelineState from ..modular_pipeline_utils import ComponentSpec, InputParam, OutputParam logger = logging.get_logger(__name__) # pylint: disable=invalid-name class WanVaeDecoderStep(ModularPipelineBlocks): model_name = "wan" @property def expected_components(self) -> list[ComponentSpec]: return [ ComponentSpec("vae", AutoencoderKLWan), ComponentSpec( "video_processor", VideoProcessor, config=FrozenDict({"vae_scale_factor": 8}), default_creation_method="from_config", ), ] @property def description(self) -> str: return "Step that decodes the denoised latents into images" @property def inputs(self) -> list[tuple[str, Any]]: return [ InputParam( "latents", required=True, type_hint=torch.Tensor, description="The denoised latents from the denoising step", ), InputParam( "output_type", default="np", type_hint=str, description="The output type of the decoded videos" ), ] @property def intermediate_outputs(self) -> list[str]: return [ OutputParam( "videos", type_hint=list[list[PIL.Image.Image]] | list[torch.Tensor] | list[np.ndarray], description="The generated videos, can be a PIL.Image.Image, torch.Tensor or a numpy array", ) ] @torch.no_grad() def __call__(self, components, state: PipelineState) -> PipelineState: block_state = self.get_block_state(state) vae_dtype = components.vae.dtype latents = block_state.latents latents_mean = ( torch.tensor(components.vae.config.latents_mean) .view(1, components.vae.config.z_dim, 1, 1, 1) .to(latents.device, latents.dtype) ) latents_std = 1.0 / torch.tensor(components.vae.config.latents_std).view( 1, components.vae.config.z_dim, 1, 1, 1 ).to(latents.device, latents.dtype) latents = latents / latents_std + latents_mean latents = latents.to(vae_dtype) block_state.videos = components.vae.decode(latents, return_dict=False)[0] output_type = getattr(block_state, "output_type", "np") block_state.videos = components.video_processor.postprocess_video(block_state.videos, output_type=output_type) self.set_block_state(state, block_state) return components, state
{ "repo_id": "huggingface/diffusers", "file_path": "src/diffusers/modular_pipelines/wan/decoders.py", "license": "Apache License 2.0", "lines": 82, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
huggingface/diffusers:src/diffusers/modular_pipelines/wan/denoise.py
# Copyright 2025 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from typing import Any import torch from ...configuration_utils import FrozenDict from ...guiders import ClassifierFreeGuidance from ...models import WanTransformer3DModel from ...schedulers import UniPCMultistepScheduler from ...utils import logging from ..modular_pipeline import ( BlockState, LoopSequentialPipelineBlocks, ModularPipelineBlocks, PipelineState, ) from ..modular_pipeline_utils import ComponentSpec, ConfigSpec, InputParam from .modular_pipeline import WanModularPipeline logger = logging.get_logger(__name__) # pylint: disable=invalid-name class WanLoopBeforeDenoiser(ModularPipelineBlocks): model_name = "wan" @property def description(self) -> str: return ( "step within the denoising loop that prepares the latent input for the denoiser. " "This block should be used to compose the `sub_blocks` attribute of a `LoopSequentialPipelineBlocks` " "object (e.g. `WanDenoiseLoopWrapper`)" ) @property def inputs(self) -> list[InputParam]: return [ InputParam( "latents", required=True, type_hint=torch.Tensor, description="The initial latents to use for the denoising process. Can be generated in prepare_latent step.", ), InputParam( "dtype", required=True, type_hint=torch.dtype, description="The dtype of the model inputs. Can be generated in input step.", ), ] @torch.no_grad() def __call__(self, components: WanModularPipeline, block_state: BlockState, i: int, t: torch.Tensor): block_state.latent_model_input = block_state.latents.to(block_state.dtype) return components, block_state class WanImage2VideoLoopBeforeDenoiser(ModularPipelineBlocks): model_name = "wan" @property def description(self) -> str: return ( "step within the denoising loop that prepares the latent input for the denoiser. " "This block should be used to compose the `sub_blocks` attribute of a `LoopSequentialPipelineBlocks` " "object (e.g. `WanDenoiseLoopWrapper`)" ) @property def inputs(self) -> list[InputParam]: return [ InputParam( "latents", required=True, type_hint=torch.Tensor, description="The initial latents to use for the denoising process. Can be generated in prepare_latent step.", ), InputParam( "image_condition_latents", required=True, type_hint=torch.Tensor, description="The image condition latents to use for the denoising process. Can be generated in prepare_first_frame_latents/prepare_first_last_frame_latents step.", ), InputParam( "dtype", required=True, type_hint=torch.dtype, description="The dtype of the model inputs. Can be generated in input step.", ), ] @torch.no_grad() def __call__(self, components: WanModularPipeline, block_state: BlockState, i: int, t: torch.Tensor): block_state.latent_model_input = torch.cat( [block_state.latents, block_state.image_condition_latents], dim=1 ).to(block_state.dtype) return components, block_state class WanLoopDenoiser(ModularPipelineBlocks): model_name = "wan" def __init__( self, guider_input_fields: dict[str, Any] = {"encoder_hidden_states": ("prompt_embeds", "negative_prompt_embeds")}, ): """Initialize a denoiser block that calls the denoiser model. This block is used in Wan2.1. Args: guider_input_fields: A dictionary that maps each argument expected by the denoiser model (for example, "encoder_hidden_states") to data stored on 'block_state'. The value can be either: - A tuple of strings. For instance, {"encoder_hidden_states": ("prompt_embeds", "negative_prompt_embeds")} tells the guider to read `block_state.prompt_embeds` and `block_state.negative_prompt_embeds` and pass them as the conditional and unconditional batches of 'encoder_hidden_states'. - A string. For example, {"encoder_hidden_image": "image_embeds"} makes the guider forward `block_state.image_embeds` for both conditional and unconditional batches. """ if not isinstance(guider_input_fields, dict): raise ValueError(f"guider_input_fields must be a dictionary but is {type(guider_input_fields)}") self._guider_input_fields = guider_input_fields super().__init__() @property def expected_components(self) -> list[ComponentSpec]: return [ ComponentSpec( "guider", ClassifierFreeGuidance, config=FrozenDict({"guidance_scale": 5.0}), default_creation_method="from_config", ), ComponentSpec("transformer", WanTransformer3DModel), ] @property def description(self) -> str: return ( "Step within the denoising loop that denoise the latents with guidance. " "This block should be used to compose the `sub_blocks` attribute of a `LoopSequentialPipelineBlocks` " "object (e.g. `WanDenoiseLoopWrapper`)" ) @property def inputs(self) -> list[tuple[str, Any]]: inputs = [ InputParam("attention_kwargs"), InputParam( "num_inference_steps", required=True, type_hint=int, description="The number of inference steps to use for the denoising process. Can be generated in set_timesteps step.", ), ] guider_input_names = [] for value in self._guider_input_fields.values(): if isinstance(value, tuple): guider_input_names.extend(value) else: guider_input_names.append(value) for name in guider_input_names: inputs.append(InputParam(name=name, required=True, type_hint=torch.Tensor)) return inputs @torch.no_grad() def __call__( self, components: WanModularPipeline, block_state: BlockState, i: int, t: torch.Tensor ) -> PipelineState: components.guider.set_state(step=i, num_inference_steps=block_state.num_inference_steps, timestep=t) # The guider splits model inputs into separate batches for conditional/unconditional predictions. # For CFG with guider_inputs = {"encoder_hidden_states": (prompt_embeds, negative_prompt_embeds)}: # you will get a guider_state with two batches: # guider_state = [ # {"encoder_hidden_states": prompt_embeds, "__guidance_identifier__": "pred_cond"}, # conditional batch # {"encoder_hidden_states": negative_prompt_embeds, "__guidance_identifier__": "pred_uncond"}, # unconditional batch # ] # Other guidance methods may return 1 batch (no guidance) or 3+ batches (e.g., PAG, APG). guider_state = components.guider.prepare_inputs_from_block_state(block_state, self._guider_input_fields) # run the denoiser for each guidance batch for guider_state_batch in guider_state: components.guider.prepare_models(components.transformer) cond_kwargs = guider_state_batch.as_dict() cond_kwargs = { k: v.to(block_state.dtype) if isinstance(v, torch.Tensor) else v for k, v in cond_kwargs.items() if k in self._guider_input_fields.keys() } # Predict the noise residual # store the noise_pred in guider_state_batch so that we can apply guidance across all batches guider_state_batch.noise_pred = components.transformer( hidden_states=block_state.latent_model_input.to(block_state.dtype), timestep=t.expand(block_state.latent_model_input.shape[0]).to(block_state.dtype), attention_kwargs=block_state.attention_kwargs, return_dict=False, **cond_kwargs, )[0] components.guider.cleanup_models(components.transformer) # Perform guidance block_state.noise_pred = components.guider(guider_state)[0] return components, block_state class Wan22LoopDenoiser(ModularPipelineBlocks): model_name = "wan" def __init__( self, guider_input_fields: dict[str, Any] = {"encoder_hidden_states": ("prompt_embeds", "negative_prompt_embeds")}, ): """Initialize a denoiser block that calls the denoiser model. This block is used in Wan2.2. Args: guider_input_fields: A dictionary that maps each argument expected by the denoiser model (for example, "encoder_hidden_states") to data stored on `block_state`. The value can be either: - A tuple of strings. For instance, `{"encoder_hidden_states": ("prompt_embeds", "negative_prompt_embeds")}` tells the guider to read `block_state.prompt_embeds` and `block_state.negative_prompt_embeds` and pass them as the conditional and unconditional batches of `encoder_hidden_states`. - A string. For example, `{"encoder_hidden_image": "image_embeds"}` makes the guider forward `block_state.image_embeds` for both conditional and unconditional batches. """ if not isinstance(guider_input_fields, dict): raise ValueError(f"guider_input_fields must be a dictionary but is {type(guider_input_fields)}") self._guider_input_fields = guider_input_fields super().__init__() @property def expected_components(self) -> list[ComponentSpec]: return [ ComponentSpec( "guider", ClassifierFreeGuidance, config=FrozenDict({"guidance_scale": 4.0}), default_creation_method="from_config", ), ComponentSpec( "guider_2", ClassifierFreeGuidance, config=FrozenDict({"guidance_scale": 3.0}), default_creation_method="from_config", ), ComponentSpec("transformer", WanTransformer3DModel), ComponentSpec("transformer_2", WanTransformer3DModel), ] @property def description(self) -> str: return ( "Step within the denoising loop that denoise the latents with guidance. " "This block should be used to compose the `sub_blocks` attribute of a `LoopSequentialPipelineBlocks` " "object (e.g. `WanDenoiseLoopWrapper`)" ) @property def expected_configs(self) -> list[ConfigSpec]: return [ ConfigSpec( name="boundary_ratio", default=0.875, description="The boundary ratio to divide the denoising loop into high noise and low noise stages.", ), ] @property def inputs(self) -> list[tuple[str, Any]]: inputs = [ InputParam("attention_kwargs"), InputParam( "num_inference_steps", required=True, type_hint=int, description="The number of inference steps to use for the denoising process. Can be generated in set_timesteps step.", ), ] guider_input_names = [] for value in self._guider_input_fields.values(): if isinstance(value, tuple): guider_input_names.extend(value) else: guider_input_names.append(value) for name in guider_input_names: inputs.append(InputParam(name=name, required=True, type_hint=torch.Tensor)) return inputs @torch.no_grad() def __call__( self, components: WanModularPipeline, block_state: BlockState, i: int, t: torch.Tensor ) -> PipelineState: boundary_timestep = components.config.boundary_ratio * components.num_train_timesteps if t >= boundary_timestep: block_state.current_model = components.transformer block_state.guider = components.guider else: block_state.current_model = components.transformer_2 block_state.guider = components.guider_2 block_state.guider.set_state(step=i, num_inference_steps=block_state.num_inference_steps, timestep=t) # The guider splits model inputs into separate batches for conditional/unconditional predictions. # For CFG with guider_inputs = {"encoder_hidden_states": (prompt_embeds, negative_prompt_embeds)}: # you will get a guider_state with two batches: # guider_state = [ # {"encoder_hidden_states": prompt_embeds, "__guidance_identifier__": "pred_cond"}, # conditional batch # {"encoder_hidden_states": negative_prompt_embeds, "__guidance_identifier__": "pred_uncond"}, # unconditional batch # ] # Other guidance methods may return 1 batch (no guidance) or 3+ batches (e.g., PAG, APG). guider_state = block_state.guider.prepare_inputs_from_block_state(block_state, self._guider_input_fields) # run the denoiser for each guidance batch for guider_state_batch in guider_state: block_state.guider.prepare_models(block_state.current_model) cond_kwargs = guider_state_batch.as_dict() cond_kwargs = { k: v.to(block_state.dtype) if isinstance(v, torch.Tensor) else v for k, v in cond_kwargs.items() if k in self._guider_input_fields.keys() } # Predict the noise residual # store the noise_pred in guider_state_batch so that we can apply guidance across all batches guider_state_batch.noise_pred = block_state.current_model( hidden_states=block_state.latent_model_input.to(block_state.dtype), timestep=t.expand(block_state.latent_model_input.shape[0]).to(block_state.dtype), attention_kwargs=block_state.attention_kwargs, return_dict=False, **cond_kwargs, )[0] block_state.guider.cleanup_models(block_state.current_model) # Perform guidance block_state.noise_pred = block_state.guider(guider_state)[0] return components, block_state class WanLoopAfterDenoiser(ModularPipelineBlocks): model_name = "wan" @property def expected_components(self) -> list[ComponentSpec]: return [ ComponentSpec("scheduler", UniPCMultistepScheduler), ] @property def description(self) -> str: return ( "step within the denoising loop that update the latents. " "This block should be used to compose the `sub_blocks` attribute of a `LoopSequentialPipelineBlocks` " "object (e.g. `WanDenoiseLoopWrapper`)" ) @torch.no_grad() def __call__(self, components: WanModularPipeline, block_state: BlockState, i: int, t: torch.Tensor): # Perform scheduler step using the predicted output latents_dtype = block_state.latents.dtype block_state.latents = components.scheduler.step( block_state.noise_pred.float(), t, block_state.latents.float(), return_dict=False, )[0] if block_state.latents.dtype != latents_dtype: block_state.latents = block_state.latents.to(latents_dtype) return components, block_state class WanDenoiseLoopWrapper(LoopSequentialPipelineBlocks): model_name = "wan" @property def description(self) -> str: return ( "Pipeline block that iteratively denoise the latents over `timesteps`. " "The specific steps with each iteration can be customized with `sub_blocks` attributes" ) @property def loop_expected_components(self) -> list[ComponentSpec]: return [ ComponentSpec("scheduler", UniPCMultistepScheduler), ] @property def loop_inputs(self) -> list[InputParam]: return [ InputParam( "timesteps", required=True, type_hint=torch.Tensor, description="The timesteps to use for the denoising process. Can be generated in set_timesteps step.", ), InputParam( "num_inference_steps", required=True, type_hint=int, description="The number of inference steps to use for the denoising process. Can be generated in set_timesteps step.", ), ] @torch.no_grad() def __call__(self, components: WanModularPipeline, state: PipelineState) -> PipelineState: block_state = self.get_block_state(state) block_state.num_warmup_steps = max( len(block_state.timesteps) - block_state.num_inference_steps * components.scheduler.order, 0 ) with self.progress_bar(total=block_state.num_inference_steps) as progress_bar: for i, t in enumerate(block_state.timesteps): components, block_state = self.loop_step(components, block_state, i=i, t=t) if i == len(block_state.timesteps) - 1 or ( (i + 1) > block_state.num_warmup_steps and (i + 1) % components.scheduler.order == 0 ): progress_bar.update() self.set_block_state(state, block_state) return components, state class WanDenoiseStep(WanDenoiseLoopWrapper): block_classes = [ WanLoopBeforeDenoiser, WanLoopDenoiser( guider_input_fields={ "encoder_hidden_states": ("prompt_embeds", "negative_prompt_embeds"), } ), WanLoopAfterDenoiser, ] block_names = ["before_denoiser", "denoiser", "after_denoiser"] @property def description(self) -> str: return ( "Denoise step that iteratively denoise the latents. \n" "Its loop logic is defined in `WanDenoiseLoopWrapper.__call__` method \n" "At each iteration, it runs blocks defined in `sub_blocks` sequentially:\n" " - `WanLoopBeforeDenoiser`\n" " - `WanLoopDenoiser`\n" " - `WanLoopAfterDenoiser`\n" "This block supports text-to-video tasks for wan2.1." ) class Wan22DenoiseStep(WanDenoiseLoopWrapper): block_classes = [ WanLoopBeforeDenoiser, Wan22LoopDenoiser( guider_input_fields={ "encoder_hidden_states": ("prompt_embeds", "negative_prompt_embeds"), } ), WanLoopAfterDenoiser, ] block_names = ["before_denoiser", "denoiser", "after_denoiser"] @property def description(self) -> str: return ( "Denoise step that iteratively denoise the latents. \n" "Its loop logic is defined in `WanDenoiseLoopWrapper.__call__` method \n" "At each iteration, it runs blocks defined in `sub_blocks` sequentially:\n" " - `WanLoopBeforeDenoiser`\n" " - `Wan22LoopDenoiser`\n" " - `WanLoopAfterDenoiser`\n" "This block supports text-to-video tasks for Wan2.2." ) class WanImage2VideoDenoiseStep(WanDenoiseLoopWrapper): block_classes = [ WanImage2VideoLoopBeforeDenoiser, WanLoopDenoiser( guider_input_fields={ "encoder_hidden_states": ("prompt_embeds", "negative_prompt_embeds"), "encoder_hidden_states_image": "image_embeds", } ), WanLoopAfterDenoiser, ] block_names = ["before_denoiser", "denoiser", "after_denoiser"] @property def description(self) -> str: return ( "Denoise step that iteratively denoise the latents. \n" "Its loop logic is defined in `WanDenoiseLoopWrapper.__call__` method \n" "At each iteration, it runs blocks defined in `sub_blocks` sequentially:\n" " - `WanImage2VideoLoopBeforeDenoiser`\n" " - `WanLoopDenoiser`\n" " - `WanLoopAfterDenoiser`\n" "This block supports image-to-video tasks for wan2.1." ) class Wan22Image2VideoDenoiseStep(WanDenoiseLoopWrapper): block_classes = [ WanImage2VideoLoopBeforeDenoiser, Wan22LoopDenoiser( guider_input_fields={ "encoder_hidden_states": ("prompt_embeds", "negative_prompt_embeds"), } ), WanLoopAfterDenoiser, ] block_names = ["before_denoiser", "denoiser", "after_denoiser"] @property def description(self) -> str: return ( "Denoise step that iteratively denoise the latents. \n" "Its loop logic is defined in `WanDenoiseLoopWrapper.__call__` method \n" "At each iteration, it runs blocks defined in `sub_blocks` sequentially:\n" " - `WanImage2VideoLoopBeforeDenoiser`\n" " - `WanLoopDenoiser`\n" " - `WanLoopAfterDenoiser`\n" "This block supports image-to-video tasks for Wan2.2." )
{ "repo_id": "huggingface/diffusers", "file_path": "src/diffusers/modular_pipelines/wan/denoise.py", "license": "Apache License 2.0", "lines": 468, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
huggingface/diffusers:src/diffusers/modular_pipelines/wan/encoders.py
# Copyright 2025 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import html import numpy as np import PIL import regex as re import torch from transformers import AutoTokenizer, CLIPImageProcessor, CLIPVisionModel, UMT5EncoderModel from ...configuration_utils import FrozenDict from ...guiders import ClassifierFreeGuidance from ...image_processor import PipelineImageInput from ...models import AutoencoderKLWan from ...utils import is_ftfy_available, is_torchvision_available, logging from ...video_processor import VideoProcessor from ..modular_pipeline import ModularPipelineBlocks, PipelineState from ..modular_pipeline_utils import ComponentSpec, InputParam, OutputParam from .modular_pipeline import WanModularPipeline if is_ftfy_available(): import ftfy if is_torchvision_available(): from torchvision import transforms logger = logging.get_logger(__name__) # pylint: disable=invalid-name def basic_clean(text): text = ftfy.fix_text(text) text = html.unescape(html.unescape(text)) return text.strip() def whitespace_clean(text): text = re.sub(r"\s+", " ", text) text = text.strip() return text def prompt_clean(text): text = whitespace_clean(basic_clean(text)) return text def get_t5_prompt_embeds( text_encoder: UMT5EncoderModel, tokenizer: AutoTokenizer, prompt: str | list[str], max_sequence_length: int, device: torch.device, ): dtype = text_encoder.dtype prompt = [prompt] if isinstance(prompt, str) else prompt prompt = [prompt_clean(u) for u in prompt] text_inputs = tokenizer( prompt, padding="max_length", max_length=max_sequence_length, truncation=True, add_special_tokens=True, return_attention_mask=True, return_tensors="pt", ) text_input_ids, mask = text_inputs.input_ids, text_inputs.attention_mask seq_lens = mask.gt(0).sum(dim=1).long() prompt_embeds = text_encoder(text_input_ids.to(device), mask.to(device)).last_hidden_state prompt_embeds = prompt_embeds.to(dtype=dtype, device=device) prompt_embeds = [u[:v] for u, v in zip(prompt_embeds, seq_lens)] prompt_embeds = torch.stack( [torch.cat([u, u.new_zeros(max_sequence_length - u.size(0), u.size(1))]) for u in prompt_embeds], dim=0 ) return prompt_embeds def encode_image( image: PipelineImageInput, image_processor: CLIPImageProcessor, image_encoder: CLIPVisionModel, device: torch.device | None = None, ): image = image_processor(images=image, return_tensors="pt").to(device) image_embeds = image_encoder(**image, output_hidden_states=True) return image_embeds.hidden_states[-2] # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion_img2img.retrieve_latents def retrieve_latents( encoder_output: torch.Tensor, generator: torch.Generator | None = None, sample_mode: str = "sample" ): if hasattr(encoder_output, "latent_dist") and sample_mode == "sample": return encoder_output.latent_dist.sample(generator) elif hasattr(encoder_output, "latent_dist") and sample_mode == "argmax": return encoder_output.latent_dist.mode() elif hasattr(encoder_output, "latents"): return encoder_output.latents else: raise AttributeError("Could not access latents of provided encoder_output") def encode_vae_image( video_tensor: torch.Tensor, vae: AutoencoderKLWan, generator: torch.Generator, device: torch.device, dtype: torch.dtype, latent_channels: int = 16, ): if not isinstance(video_tensor, torch.Tensor): raise ValueError(f"Expected video_tensor to be a tensor, got {type(video_tensor)}.") if isinstance(generator, list) and len(generator) != video_tensor.shape[0]: raise ValueError( f"You have passed a list of generators of length {len(generator)}, but it is not same as number of images {video_tensor.shape[0]}." ) video_tensor = video_tensor.to(device=device, dtype=dtype) if isinstance(generator, list): video_latents = [ retrieve_latents(vae.encode(video_tensor[i : i + 1]), generator=generator[i], sample_mode="argmax") for i in range(video_tensor.shape[0]) ] video_latents = torch.cat(video_latents, dim=0) else: video_latents = retrieve_latents(vae.encode(video_tensor), sample_mode="argmax") latents_mean = ( torch.tensor(vae.config.latents_mean) .view(1, latent_channels, 1, 1, 1) .to(video_latents.device, video_latents.dtype) ) latents_std = 1.0 / torch.tensor(vae.config.latents_std).view(1, latent_channels, 1, 1, 1).to( video_latents.device, video_latents.dtype ) video_latents = (video_latents - latents_mean) * latents_std return video_latents class WanTextEncoderStep(ModularPipelineBlocks): model_name = "wan" @property def description(self) -> str: return "Text Encoder step that generate text_embeddings to guide the video generation" @property def expected_components(self) -> list[ComponentSpec]: return [ ComponentSpec("text_encoder", UMT5EncoderModel), ComponentSpec("tokenizer", AutoTokenizer), ComponentSpec( "guider", ClassifierFreeGuidance, config=FrozenDict({"guidance_scale": 5.0}), default_creation_method="from_config", ), ] @property def inputs(self) -> list[InputParam]: return [ InputParam("prompt"), InputParam("negative_prompt"), InputParam("max_sequence_length", default=512), ] @property def intermediate_outputs(self) -> list[OutputParam]: return [ OutputParam( "prompt_embeds", type_hint=torch.Tensor, kwargs_type="denoiser_input_fields", description="text embeddings used to guide the image generation", ), OutputParam( "negative_prompt_embeds", type_hint=torch.Tensor, kwargs_type="denoiser_input_fields", description="negative text embeddings used to guide the image generation", ), ] @staticmethod def check_inputs(block_state): if block_state.prompt is not None and ( not isinstance(block_state.prompt, str) and not isinstance(block_state.prompt, list) ): raise ValueError(f"`prompt` has to be of type `str` or `list` but is {type(block_state.prompt)}") @staticmethod def encode_prompt( components, prompt: str, device: torch.device | None = None, prepare_unconditional_embeds: bool = True, negative_prompt: str | None = None, max_sequence_length: int = 512, ): r""" Encodes the prompt into text encoder hidden states. Args: prompt (`str` or `list[str]`, *optional*): prompt to be encoded device: (`torch.device`): torch device prepare_unconditional_embeds (`bool`): whether to use prepare unconditional embeddings or not negative_prompt (`str` or `list[str]`, *optional*): The prompt or prompts not to guide the image generation. If not defined, one has to pass `negative_prompt_embeds` instead. Ignored when not using guidance (i.e., ignored if `guidance_scale` is less than `1`). max_sequence_length (`int`, defaults to `512`): The maximum number of text tokens to be used for the generation process. """ device = device or components._execution_device if not isinstance(prompt, list): prompt = [prompt] batch_size = len(prompt) prompt_embeds = get_t5_prompt_embeds( text_encoder=components.text_encoder, tokenizer=components.tokenizer, prompt=prompt, max_sequence_length=max_sequence_length, device=device, ) if prepare_unconditional_embeds: negative_prompt = negative_prompt or "" negative_prompt = batch_size * [negative_prompt] if isinstance(negative_prompt, str) else negative_prompt if prompt is not None and type(prompt) is not type(negative_prompt): raise TypeError( f"`negative_prompt` should be the same type to `prompt`, but got {type(negative_prompt)} !=" f" {type(prompt)}." ) elif batch_size != len(negative_prompt): raise ValueError( f"`negative_prompt`: {negative_prompt} has batch size {len(negative_prompt)}, but `prompt`:" f" {prompt} has batch size {batch_size}. Please make sure that passed `negative_prompt` matches" " the batch size of `prompt`." ) negative_prompt_embeds = get_t5_prompt_embeds( text_encoder=components.text_encoder, tokenizer=components.tokenizer, prompt=negative_prompt, max_sequence_length=max_sequence_length, device=device, ) return prompt_embeds, negative_prompt_embeds @torch.no_grad() def __call__(self, components: WanModularPipeline, state: PipelineState) -> PipelineState: # Get inputs and intermediates block_state = self.get_block_state(state) self.check_inputs(block_state) block_state.device = components._execution_device # Encode input prompt ( block_state.prompt_embeds, block_state.negative_prompt_embeds, ) = self.encode_prompt( components=components, prompt=block_state.prompt, device=block_state.device, prepare_unconditional_embeds=components.requires_unconditional_embeds, negative_prompt=block_state.negative_prompt, max_sequence_length=block_state.max_sequence_length, ) # Add outputs self.set_block_state(state, block_state) return components, state class WanImageResizeStep(ModularPipelineBlocks): model_name = "wan" @property def description(self) -> str: return "Image Resize step that resize the image to the target area (height * width) while maintaining the aspect ratio." @property def inputs(self) -> list[InputParam]: return [ InputParam("image", type_hint=PIL.Image.Image, required=True), InputParam("height", type_hint=int, default=480), InputParam("width", type_hint=int, default=832), ] @property def intermediate_outputs(self) -> list[OutputParam]: return [ OutputParam("resized_image", type_hint=PIL.Image.Image), ] def __call__(self, components: WanModularPipeline, state: PipelineState) -> PipelineState: block_state = self.get_block_state(state) max_area = block_state.height * block_state.width image = block_state.image aspect_ratio = image.height / image.width mod_value = components.vae_scale_factor_spatial * components.patch_size_spatial block_state.height = round(np.sqrt(max_area * aspect_ratio)) // mod_value * mod_value block_state.width = round(np.sqrt(max_area / aspect_ratio)) // mod_value * mod_value block_state.resized_image = image.resize((block_state.width, block_state.height)) self.set_block_state(state, block_state) return components, state class WanImageCropResizeStep(ModularPipelineBlocks): model_name = "wan" @property def description(self) -> str: return "Image Resize step that resize the last_image to the same size of first frame image with center crop." @property def inputs(self) -> list[InputParam]: return [ InputParam( "resized_image", type_hint=PIL.Image.Image, required=True, description="The resized first frame image" ), InputParam("last_image", type_hint=PIL.Image.Image, required=True, description="The last frameimage"), ] @property def intermediate_outputs(self) -> list[OutputParam]: return [ OutputParam("resized_last_image", type_hint=PIL.Image.Image), ] def __call__(self, components: WanModularPipeline, state: PipelineState) -> PipelineState: block_state = self.get_block_state(state) height = block_state.resized_image.height width = block_state.resized_image.width image = block_state.last_image # Calculate resize ratio to match first frame dimensions resize_ratio = max(width / image.width, height / image.height) # Resize the image width = round(image.width * resize_ratio) height = round(image.height * resize_ratio) size = [width, height] resized_image = transforms.functional.center_crop(image, size) block_state.resized_last_image = resized_image self.set_block_state(state, block_state) return components, state class WanImageEncoderStep(ModularPipelineBlocks): model_name = "wan" @property def description(self) -> str: return "Image Encoder step that generate image_embeds based on first frame image to guide the video generation" @property def expected_components(self) -> list[ComponentSpec]: return [ ComponentSpec("image_processor", CLIPImageProcessor), ComponentSpec("image_encoder", CLIPVisionModel), ] @property def inputs(self) -> list[InputParam]: return [ InputParam("resized_image", type_hint=PIL.Image.Image, required=True), ] @property def intermediate_outputs(self) -> list[OutputParam]: return [ OutputParam("image_embeds", type_hint=torch.Tensor, description="The image embeddings"), ] def __call__(self, components: WanModularPipeline, state: PipelineState) -> PipelineState: block_state = self.get_block_state(state) device = components._execution_device image = block_state.resized_image image_embeds = encode_image( image_processor=components.image_processor, image_encoder=components.image_encoder, image=image, device=device, ) block_state.image_embeds = image_embeds self.set_block_state(state, block_state) return components, state class WanFirstLastFrameImageEncoderStep(ModularPipelineBlocks): model_name = "wan" @property def description(self) -> str: return "Image Encoder step that generate image_embeds based on first and last frame images to guide the video generation" @property def expected_components(self) -> list[ComponentSpec]: return [ ComponentSpec("image_processor", CLIPImageProcessor), ComponentSpec("image_encoder", CLIPVisionModel), ] @property def inputs(self) -> list[InputParam]: return [ InputParam("resized_image", type_hint=PIL.Image.Image, required=True), InputParam("resized_last_image", type_hint=PIL.Image.Image, required=True), ] @property def intermediate_outputs(self) -> list[OutputParam]: return [ OutputParam("image_embeds", type_hint=torch.Tensor, description="The image embeddings"), ] def __call__(self, components: WanModularPipeline, state: PipelineState) -> PipelineState: block_state = self.get_block_state(state) device = components._execution_device first_frame_image = block_state.resized_image last_frame_image = block_state.resized_last_image image_embeds = encode_image( image_processor=components.image_processor, image_encoder=components.image_encoder, image=[first_frame_image, last_frame_image], device=device, ) block_state.image_embeds = image_embeds self.set_block_state(state, block_state) return components, state class WanVaeEncoderStep(ModularPipelineBlocks): model_name = "wan" @property def description(self) -> str: return "Vae Image Encoder step that generate condition_latents based on first frame image to guide the video generation" @property def expected_components(self) -> list[ComponentSpec]: return [ ComponentSpec("vae", AutoencoderKLWan), ComponentSpec( "video_processor", VideoProcessor, config=FrozenDict({"vae_scale_factor": 8}), default_creation_method="from_config", ), ] @property def inputs(self) -> list[InputParam]: return [ InputParam("resized_image", type_hint=PIL.Image.Image, required=True), InputParam("height"), InputParam("width"), InputParam("num_frames", type_hint=int, default=81), InputParam("generator"), ] @property def intermediate_outputs(self) -> list[OutputParam]: return [ OutputParam( "first_frame_latents", type_hint=torch.Tensor, description="video latent representation with the first frame image condition", ), ] @staticmethod def check_inputs(components, block_state): if (block_state.height is not None and block_state.height % components.vae_scale_factor_spatial != 0) or ( block_state.width is not None and block_state.width % components.vae_scale_factor_spatial != 0 ): raise ValueError( f"`height` and `width` have to be divisible by {components.vae_scale_factor_spatial} but are {block_state.height} and {block_state.width}." ) if block_state.num_frames is not None and ( block_state.num_frames < 1 or (block_state.num_frames - 1) % components.vae_scale_factor_temporal != 0 ): raise ValueError( f"`num_frames` has to be greater than 0, and (num_frames - 1) must be divisible by {components.vae_scale_factor_temporal}, but got {block_state.num_frames}." ) def __call__(self, components: WanModularPipeline, state: PipelineState) -> PipelineState: block_state = self.get_block_state(state) self.check_inputs(components, block_state) image = block_state.resized_image device = components._execution_device dtype = torch.float32 vae_dtype = components.vae.dtype height = block_state.height or components.default_height width = block_state.width or components.default_width num_frames = block_state.num_frames or components.default_num_frames image_tensor = components.video_processor.preprocess(image, height=height, width=width).to( device=device, dtype=dtype ) if image_tensor.dim() == 4: image_tensor = image_tensor.unsqueeze(2) video_tensor = torch.cat( [ image_tensor, image_tensor.new_zeros(image_tensor.shape[0], image_tensor.shape[1], num_frames - 1, height, width), ], dim=2, ).to(device=device, dtype=dtype) block_state.first_frame_latents = encode_vae_image( video_tensor=video_tensor, vae=components.vae, generator=block_state.generator, device=device, dtype=vae_dtype, latent_channels=components.num_channels_latents, ) self.set_block_state(state, block_state) return components, state class WanPrepareFirstFrameLatentsStep(ModularPipelineBlocks): model_name = "wan" @property def description(self) -> str: return "step that prepares the masked first frame latents and add it to the latent condition" @property def inputs(self) -> list[InputParam]: return [ InputParam("first_frame_latents", type_hint=torch.Tensor | None), InputParam("num_frames", required=True), ] @property def intermediate_outputs(self) -> list[OutputParam]: return [ OutputParam("image_condition_latents", type_hint=torch.Tensor | None), ] def __call__(self, components: WanModularPipeline, state: PipelineState) -> PipelineState: block_state = self.get_block_state(state) batch_size, _, _, latent_height, latent_width = block_state.first_frame_latents.shape mask_lat_size = torch.ones(batch_size, 1, block_state.num_frames, latent_height, latent_width) mask_lat_size[:, :, list(range(1, block_state.num_frames))] = 0 first_frame_mask = mask_lat_size[:, :, 0:1] first_frame_mask = torch.repeat_interleave( first_frame_mask, dim=2, repeats=components.vae_scale_factor_temporal ) mask_lat_size = torch.concat([first_frame_mask, mask_lat_size[:, :, 1:, :]], dim=2) mask_lat_size = mask_lat_size.view( batch_size, -1, components.vae_scale_factor_temporal, latent_height, latent_width ) mask_lat_size = mask_lat_size.transpose(1, 2) mask_lat_size = mask_lat_size.to(block_state.first_frame_latents.device) block_state.image_condition_latents = torch.concat([mask_lat_size, block_state.first_frame_latents], dim=1) self.set_block_state(state, block_state) return components, state class WanFirstLastFrameVaeEncoderStep(ModularPipelineBlocks): model_name = "wan" @property def description(self) -> str: return "Vae Image Encoder step that generate condition_latents based on first and last frame images to guide the video generation" @property def expected_components(self) -> list[ComponentSpec]: return [ ComponentSpec("vae", AutoencoderKLWan), ComponentSpec( "video_processor", VideoProcessor, config=FrozenDict({"vae_scale_factor": 8}), default_creation_method="from_config", ), ] @property def inputs(self) -> list[InputParam]: return [ InputParam("resized_image", type_hint=PIL.Image.Image, required=True), InputParam("resized_last_image", type_hint=PIL.Image.Image, required=True), InputParam("height"), InputParam("width"), InputParam("num_frames", type_hint=int, default=81), InputParam("generator"), ] @property def intermediate_outputs(self) -> list[OutputParam]: return [ OutputParam( "first_last_frame_latents", type_hint=torch.Tensor, description="video latent representation with the first and last frame images condition", ), ] @staticmethod def check_inputs(components, block_state): if (block_state.height is not None and block_state.height % components.vae_scale_factor_spatial != 0) or ( block_state.width is not None and block_state.width % components.vae_scale_factor_spatial != 0 ): raise ValueError( f"`height` and `width` have to be divisible by {components.vae_scale_factor_spatial} but are {block_state.height} and {block_state.width}." ) if block_state.num_frames is not None and ( block_state.num_frames < 1 or (block_state.num_frames - 1) % components.vae_scale_factor_temporal != 0 ): raise ValueError( f"`num_frames` has to be greater than 0, and (num_frames - 1) must be divisible by {components.vae_scale_factor_temporal}, but got {block_state.num_frames}." ) def __call__(self, components: WanModularPipeline, state: PipelineState) -> PipelineState: block_state = self.get_block_state(state) self.check_inputs(components, block_state) first_frame_image = block_state.resized_image last_frame_image = block_state.resized_last_image device = components._execution_device dtype = torch.float32 vae_dtype = components.vae.dtype height = block_state.height or components.default_height width = block_state.width or components.default_width num_frames = block_state.num_frames or components.default_num_frames first_image_tensor = components.video_processor.preprocess(first_frame_image, height=height, width=width).to( device=device, dtype=dtype ) first_image_tensor = first_image_tensor.unsqueeze(2) last_image_tensor = components.video_processor.preprocess(last_frame_image, height=height, width=width).to( device=device, dtype=dtype ) last_image_tensor = last_image_tensor.unsqueeze(2) video_tensor = torch.cat( [ first_image_tensor, first_image_tensor.new_zeros( first_image_tensor.shape[0], first_image_tensor.shape[1], num_frames - 2, height, width ), last_image_tensor, ], dim=2, ).to(device=device, dtype=dtype) block_state.first_last_frame_latents = encode_vae_image( video_tensor=video_tensor, vae=components.vae, generator=block_state.generator, device=device, dtype=vae_dtype, latent_channels=components.num_channels_latents, ) self.set_block_state(state, block_state) return components, state class WanPrepareFirstLastFrameLatentsStep(ModularPipelineBlocks): model_name = "wan" @property def description(self) -> str: return "step that prepares the masked latents with first and last frames and add it to the latent condition" @property def inputs(self) -> list[InputParam]: return [ InputParam("first_last_frame_latents", type_hint=torch.Tensor | None), InputParam("num_frames", type_hint=int, required=True), ] @property def intermediate_outputs(self) -> list[OutputParam]: return [ OutputParam("image_condition_latents", type_hint=torch.Tensor | None), ] def __call__(self, components: WanModularPipeline, state: PipelineState) -> PipelineState: block_state = self.get_block_state(state) batch_size, _, _, latent_height, latent_width = block_state.first_last_frame_latents.shape mask_lat_size = torch.ones(batch_size, 1, block_state.num_frames, latent_height, latent_width) mask_lat_size[:, :, list(range(1, block_state.num_frames - 1))] = 0 first_frame_mask = mask_lat_size[:, :, 0:1] first_frame_mask = torch.repeat_interleave( first_frame_mask, dim=2, repeats=components.vae_scale_factor_temporal ) mask_lat_size = torch.concat([first_frame_mask, mask_lat_size[:, :, 1:, :]], dim=2) mask_lat_size = mask_lat_size.view( batch_size, -1, components.vae_scale_factor_temporal, latent_height, latent_width ) mask_lat_size = mask_lat_size.transpose(1, 2) mask_lat_size = mask_lat_size.to(block_state.first_last_frame_latents.device) block_state.image_condition_latents = torch.concat( [mask_lat_size, block_state.first_last_frame_latents], dim=1 ) self.set_block_state(state, block_state) return components, state
{ "repo_id": "huggingface/diffusers", "file_path": "src/diffusers/modular_pipelines/wan/encoders.py", "license": "Apache License 2.0", "lines": 620, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
huggingface/diffusers:src/diffusers/modular_pipelines/wan/modular_pipeline.py
# Copyright 2025 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from ...loaders import WanLoraLoaderMixin from ...pipelines.pipeline_utils import StableDiffusionMixin from ...utils import logging from ..modular_pipeline import ModularPipeline logger = logging.get_logger(__name__) # pylint: disable=invalid-name class WanModularPipeline( ModularPipeline, StableDiffusionMixin, WanLoraLoaderMixin, ): """ A ModularPipeline for Wan2.1 text2video. > [!WARNING] > This is an experimental feature and is likely to change in the future. """ default_blocks_name = "WanBlocks" @property def default_height(self): return self.default_sample_height * self.vae_scale_factor_spatial @property def default_width(self): return self.default_sample_width * self.vae_scale_factor_spatial @property def default_num_frames(self): return (self.default_sample_num_frames - 1) * self.vae_scale_factor_temporal + 1 @property def default_sample_height(self): return 60 @property def default_sample_width(self): return 104 @property def default_sample_num_frames(self): return 21 @property def patch_size_spatial(self): patch_size_spatial = 2 if hasattr(self, "transformer") and self.transformer is not None: patch_size_spatial = self.transformer.config.patch_size[1] return patch_size_spatial @property def vae_scale_factor_spatial(self): vae_scale_factor = 8 if hasattr(self, "vae") and self.vae is not None: vae_scale_factor = 2 ** len(self.vae.temperal_downsample) return vae_scale_factor @property def vae_scale_factor_temporal(self): vae_scale_factor = 4 if hasattr(self, "vae") and self.vae is not None: vae_scale_factor = 2 ** sum(self.vae.temperal_downsample) return vae_scale_factor @property def num_channels_transformer(self): num_channels_transformer = 16 if hasattr(self, "transformer") and self.transformer is not None: num_channels_transformer = self.transformer.config.in_channels return num_channels_transformer @property def num_channels_latents(self): num_channels_latents = 16 if hasattr(self, "vae") and self.vae is not None: num_channels_latents = self.vae.config.z_dim return num_channels_latents @property def requires_unconditional_embeds(self): requires_unconditional_embeds = False if hasattr(self, "guider") and self.guider is not None: requires_unconditional_embeds = self.guider._enabled and self.guider.num_conditions > 1 return requires_unconditional_embeds @property def num_train_timesteps(self): num_train_timesteps = 1000 if hasattr(self, "scheduler") and self.scheduler is not None: num_train_timesteps = self.scheduler.config.num_train_timesteps return num_train_timesteps class WanImage2VideoModularPipeline(WanModularPipeline): """ A ModularPipeline for Wan2.1 image2video (both I2V and FLF2V). > [!WARNING] > This is an experimental feature and is likely to change in the future. """ default_blocks_name = "WanImage2VideoAutoBlocks" class Wan22ModularPipeline(WanModularPipeline): """ A ModularPipeline for Wan2.2 text2video. > [!WARNING] > This is an experimental feature and is likely to change in the future. """ default_blocks_name = "Wan22Blocks" class Wan22Image2VideoModularPipeline(Wan22ModularPipeline): """ A ModularPipeline for Wan2.2 image2video. > [!WARNING] > This is an experimental feature and is likely to change in the future. """ default_blocks_name = "Wan22Image2VideoBlocks"
{ "repo_id": "huggingface/diffusers", "file_path": "src/diffusers/modular_pipelines/wan/modular_pipeline.py", "license": "Apache License 2.0", "lines": 106, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
huggingface/diffusers:src/diffusers/models/attention_dispatch.py
# Copyright 2025 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from __future__ import annotations import contextlib import functools import inspect import math from dataclasses import dataclass from enum import Enum from typing import TYPE_CHECKING, Any, Callable import torch import torch.distributed as dist import torch.nn.functional as F if torch.distributed.is_available(): import torch.distributed._functional_collectives as funcol from ..utils import ( get_logger, is_aiter_available, is_aiter_version, is_flash_attn_3_available, is_flash_attn_available, is_flash_attn_version, is_kernels_available, is_sageattention_available, is_sageattention_version, is_torch_npu_available, is_torch_version, is_torch_xla_available, is_torch_xla_version, is_xformers_available, is_xformers_version, ) from ..utils.constants import DIFFUSERS_ATTN_BACKEND, DIFFUSERS_ATTN_CHECKS from ..utils.torch_utils import maybe_allow_in_graph from ._modeling_parallel import gather_size_by_comm if TYPE_CHECKING: from ._modeling_parallel import ParallelConfig _REQUIRED_FLASH_VERSION = "2.6.3" _REQUIRED_AITER_VERSION = "0.1.5" _REQUIRED_SAGE_VERSION = "2.1.1" _REQUIRED_FLEX_VERSION = "2.5.0" _REQUIRED_XLA_VERSION = "2.2" _REQUIRED_XFORMERS_VERSION = "0.0.29" logger = get_logger(__name__) # pylint: disable=invalid-name _CAN_USE_FLASH_ATTN = is_flash_attn_available() and is_flash_attn_version(">=", _REQUIRED_FLASH_VERSION) _CAN_USE_FLASH_ATTN_3 = is_flash_attn_3_available() _CAN_USE_AITER_ATTN = is_aiter_available() and is_aiter_version(">=", _REQUIRED_AITER_VERSION) _CAN_USE_SAGE_ATTN = is_sageattention_available() and is_sageattention_version(">=", _REQUIRED_SAGE_VERSION) _CAN_USE_FLEX_ATTN = is_torch_version(">=", _REQUIRED_FLEX_VERSION) _CAN_USE_NPU_ATTN = is_torch_npu_available() _CAN_USE_XLA_ATTN = is_torch_xla_available() and is_torch_xla_version(">=", _REQUIRED_XLA_VERSION) _CAN_USE_XFORMERS_ATTN = is_xformers_available() and is_xformers_version(">=", _REQUIRED_XFORMERS_VERSION) if _CAN_USE_FLASH_ATTN: try: from flash_attn import flash_attn_func, flash_attn_varlen_func from flash_attn.flash_attn_interface import _wrapped_flash_attn_backward, _wrapped_flash_attn_forward except (ImportError, OSError, RuntimeError) as e: # Handle ABI mismatch or other import failures gracefully. # This can happen when flash_attn was compiled against a different PyTorch version. logger.warning(f"flash_attn is installed but failed to import: {e}. Falling back to native PyTorch attention.") _CAN_USE_FLASH_ATTN = False flash_attn_func = None flash_attn_varlen_func = None _wrapped_flash_attn_backward = None _wrapped_flash_attn_forward = None else: flash_attn_func = None flash_attn_varlen_func = None _wrapped_flash_attn_backward = None _wrapped_flash_attn_forward = None if _CAN_USE_FLASH_ATTN_3: try: from flash_attn_interface import flash_attn_func as flash_attn_3_func from flash_attn_interface import flash_attn_varlen_func as flash_attn_3_varlen_func except (ImportError, OSError, RuntimeError) as e: logger.warning(f"flash_attn_3 failed to import: {e}. Falling back to native attention.") _CAN_USE_FLASH_ATTN_3 = False flash_attn_3_func = None flash_attn_3_varlen_func = None else: flash_attn_3_func = None flash_attn_3_varlen_func = None if _CAN_USE_AITER_ATTN: try: from aiter import flash_attn_func as aiter_flash_attn_func except (ImportError, OSError, RuntimeError) as e: logger.warning(f"aiter failed to import: {e}. Falling back to native attention.") _CAN_USE_AITER_ATTN = False aiter_flash_attn_func = None else: aiter_flash_attn_func = None if _CAN_USE_SAGE_ATTN: try: from sageattention import ( sageattn, sageattn_qk_int8_pv_fp8_cuda, sageattn_qk_int8_pv_fp8_cuda_sm90, sageattn_qk_int8_pv_fp16_cuda, sageattn_qk_int8_pv_fp16_triton, sageattn_varlen, ) except (ImportError, OSError, RuntimeError) as e: logger.warning(f"sageattention failed to import: {e}. Falling back to native attention.") _CAN_USE_SAGE_ATTN = False sageattn = None sageattn_qk_int8_pv_fp8_cuda = None sageattn_qk_int8_pv_fp8_cuda_sm90 = None sageattn_qk_int8_pv_fp16_cuda = None sageattn_qk_int8_pv_fp16_triton = None sageattn_varlen = None else: sageattn = None sageattn_qk_int8_pv_fp16_cuda = None sageattn_qk_int8_pv_fp16_triton = None sageattn_qk_int8_pv_fp8_cuda = None sageattn_qk_int8_pv_fp8_cuda_sm90 = None sageattn_varlen = None if _CAN_USE_FLEX_ATTN: try: # We cannot import the flex_attention function from the package directly because it is expected (from the # pytorch documentation) that the user may compile it. If we import directly, we will not have access to the # compiled function. import torch.nn.attention.flex_attention as flex_attention except (ImportError, OSError, RuntimeError) as e: logger.warning(f"flex_attention failed to import: {e}. Falling back to native attention.") _CAN_USE_FLEX_ATTN = False flex_attention = None else: flex_attention = None if _CAN_USE_NPU_ATTN: try: from torch_npu import npu_fusion_attention except (ImportError, OSError, RuntimeError) as e: logger.warning(f"torch_npu failed to import: {e}. Falling back to native attention.") _CAN_USE_NPU_ATTN = False npu_fusion_attention = None else: npu_fusion_attention = None if _CAN_USE_XLA_ATTN: try: from torch_xla.experimental.custom_kernel import flash_attention as xla_flash_attention except (ImportError, OSError, RuntimeError) as e: logger.warning(f"torch_xla failed to import: {e}. Falling back to native attention.") _CAN_USE_XLA_ATTN = False xla_flash_attention = None else: xla_flash_attention = None if _CAN_USE_XFORMERS_ATTN: try: import xformers.ops as xops except (ImportError, OSError, RuntimeError) as e: logger.warning(f"xformers failed to import: {e}. Falling back to native attention.") _CAN_USE_XFORMERS_ATTN = False xops = None else: xops = None # Version guard for PyTorch compatibility - custom_op was added in PyTorch 2.4 if torch.__version__ >= "2.4.0": _custom_op = torch.library.custom_op _register_fake = torch.library.register_fake else: def custom_op_no_op(name, fn=None, /, *, mutates_args, device_types=None, schema=None): def wrap(func): return func return wrap if fn is None else fn def register_fake_no_op(op, fn=None, /, *, lib=None, _stacklevel=1): def wrap(func): return func return wrap if fn is None else fn _custom_op = custom_op_no_op _register_fake = register_fake_no_op # TODO(aryan): Add support for the following: # - Sage Attention++ # - block sparse, radial and other attention methods # - CP with sage attention, flex, xformers, other missing backends # - Add support for normal and CP training with backends that don't support it yet class AttentionBackendName(str, Enum): # EAGER = "eager" # `flash-attn` FLASH = "flash" FLASH_HUB = "flash_hub" FLASH_VARLEN = "flash_varlen" FLASH_VARLEN_HUB = "flash_varlen_hub" _FLASH_3 = "_flash_3" _FLASH_VARLEN_3 = "_flash_varlen_3" _FLASH_3_HUB = "_flash_3_hub" _FLASH_3_VARLEN_HUB = "_flash_3_varlen_hub" # `aiter` AITER = "aiter" # PyTorch native FLEX = "flex" NATIVE = "native" _NATIVE_CUDNN = "_native_cudnn" _NATIVE_EFFICIENT = "_native_efficient" _NATIVE_FLASH = "_native_flash" _NATIVE_MATH = "_native_math" _NATIVE_NPU = "_native_npu" _NATIVE_XLA = "_native_xla" # `sageattention` SAGE = "sage" SAGE_HUB = "sage_hub" SAGE_VARLEN = "sage_varlen" _SAGE_QK_INT8_PV_FP8_CUDA = "_sage_qk_int8_pv_fp8_cuda" _SAGE_QK_INT8_PV_FP8_CUDA_SM90 = "_sage_qk_int8_pv_fp8_cuda_sm90" _SAGE_QK_INT8_PV_FP16_CUDA = "_sage_qk_int8_pv_fp16_cuda" _SAGE_QK_INT8_PV_FP16_TRITON = "_sage_qk_int8_pv_fp16_triton" # TODO: let's not add support for Sparge Attention now because it requires tuning per model # We can look into supporting something "autotune"-ing in the future # SPARGE = "sparge" # `xformers` XFORMERS = "xformers" class _AttentionBackendRegistry: _backends = {} _constraints = {} _supported_arg_names = {} _supports_context_parallel = set() _active_backend = AttentionBackendName(DIFFUSERS_ATTN_BACKEND) _checks_enabled = DIFFUSERS_ATTN_CHECKS @classmethod def register( cls, backend: AttentionBackendName, constraints: list[Callable] | None = None, supports_context_parallel: bool = False, ): logger.debug(f"Registering attention backend: {backend} with constraints: {constraints}") def decorator(func): cls._backends[backend] = func cls._constraints[backend] = constraints or [] cls._supported_arg_names[backend] = set(inspect.signature(func).parameters.keys()) if supports_context_parallel: cls._supports_context_parallel.add(backend.value) return func return decorator @classmethod def get_active_backend(cls): return cls._active_backend, cls._backends[cls._active_backend] @classmethod def set_active_backend(cls, backend: str): cls._active_backend = backend @classmethod def list_backends(cls): return list(cls._backends.keys()) @classmethod def _is_context_parallel_available( cls, backend: AttentionBackendName, ) -> bool: supports_context_parallel = backend.value in cls._supports_context_parallel return supports_context_parallel @dataclass class _HubKernelConfig: """Configuration for downloading and using a hub-based attention kernel.""" repo_id: str function_attr: str revision: str | None = None kernel_fn: Callable | None = None wrapped_forward_attr: str | None = None wrapped_backward_attr: str | None = None wrapped_forward_fn: Callable | None = None wrapped_backward_fn: Callable | None = None # Registry for hub-based attention kernels _HUB_KERNELS_REGISTRY: dict["AttentionBackendName", _HubKernelConfig] = { # TODO: temporary revision for now. Remove when merged upstream into `main`. AttentionBackendName._FLASH_3_HUB: _HubKernelConfig( repo_id="kernels-community/flash-attn3", function_attr="flash_attn_func", revision="fake-ops-return-probs", wrapped_forward_attr="flash_attn_interface._flash_attn_forward", wrapped_backward_attr="flash_attn_interface._flash_attn_backward", ), AttentionBackendName._FLASH_3_VARLEN_HUB: _HubKernelConfig( repo_id="kernels-community/flash-attn3", function_attr="flash_attn_varlen_func", # revision="fake-ops-return-probs", ), AttentionBackendName.FLASH_HUB: _HubKernelConfig( repo_id="kernels-community/flash-attn2", function_attr="flash_attn_func", revision=None, wrapped_forward_attr="flash_attn_interface._wrapped_flash_attn_forward", wrapped_backward_attr="flash_attn_interface._wrapped_flash_attn_backward", ), AttentionBackendName.FLASH_VARLEN_HUB: _HubKernelConfig( repo_id="kernels-community/flash-attn2", function_attr="flash_attn_varlen_func", revision=None ), AttentionBackendName.SAGE_HUB: _HubKernelConfig( repo_id="kernels-community/sage_attention", function_attr="sageattn", revision=None ), } @contextlib.contextmanager def attention_backend(backend: str | AttentionBackendName = AttentionBackendName.NATIVE): """ Context manager to set the active attention backend. """ if backend not in _AttentionBackendRegistry._backends: raise ValueError(f"Backend {backend} is not registered.") backend = AttentionBackendName(backend) _check_attention_backend_requirements(backend) _maybe_download_kernel_for_backend(backend) old_backend = _AttentionBackendRegistry._active_backend _AttentionBackendRegistry.set_active_backend(backend) try: yield finally: _AttentionBackendRegistry.set_active_backend(old_backend) def dispatch_attention_fn( query: torch.Tensor, key: torch.Tensor, value: torch.Tensor, attn_mask: torch.Tensor | None = None, dropout_p: float = 0.0, is_causal: bool = False, scale: float | None = None, enable_gqa: bool = False, attention_kwargs: dict[str, Any] | None = None, *, backend: AttentionBackendName | None = None, parallel_config: "ParallelConfig" | None = None, ) -> torch.Tensor: attention_kwargs = attention_kwargs or {} if backend is None: # If no backend is specified, we either use the default backend (set via the DIFFUSERS_ATTN_BACKEND environment # variable), or we use a custom backend based on whether user is using the `attention_backend` context manager backend_name, backend_fn = _AttentionBackendRegistry.get_active_backend() else: backend_name = AttentionBackendName(backend) backend_fn = _AttentionBackendRegistry._backends.get(backend_name) kwargs = { "query": query, "key": key, "value": value, "attn_mask": attn_mask, "dropout_p": dropout_p, "is_causal": is_causal, "scale": scale, **attention_kwargs, "_parallel_config": parallel_config, } if is_torch_version(">=", "2.5.0"): kwargs["enable_gqa"] = enable_gqa if _AttentionBackendRegistry._checks_enabled: removed_kwargs = set(kwargs) - set(_AttentionBackendRegistry._supported_arg_names[backend_name]) if removed_kwargs: logger.warning(f"Removing unsupported arguments for attention backend {backend_name}: {removed_kwargs}.") for check in _AttentionBackendRegistry._constraints.get(backend_name): check(**kwargs) kwargs = {k: v for k, v in kwargs.items() if k in _AttentionBackendRegistry._supported_arg_names[backend_name]} return backend_fn(**kwargs) # ===== Checks ===== # A list of very simple functions to catch common errors quickly when debugging. def _check_attn_mask_or_causal(attn_mask: torch.Tensor | None, is_causal: bool, **kwargs) -> None: if attn_mask is not None and is_causal: raise ValueError("`is_causal` cannot be True when `attn_mask` is not None.") def _check_device(query: torch.Tensor, key: torch.Tensor, value: torch.Tensor, **kwargs) -> None: if query.device != key.device or query.device != value.device: raise ValueError("Query, key, and value must be on the same device.") if query.dtype != key.dtype or query.dtype != value.dtype: raise ValueError("Query, key, and value must have the same dtype.") def _check_device_cuda(query: torch.Tensor, key: torch.Tensor, value: torch.Tensor, **kwargs) -> None: _check_device(query, key, value) if query.device.type != "cuda": raise ValueError("Query, key, and value must be on a CUDA device.") def _check_device_cuda_atleast_smXY(major: int, minor: int) -> Callable: def check_device_cuda(query: torch.Tensor, key: torch.Tensor, value: torch.Tensor, **kwargs) -> None: _check_device_cuda(query, key, value) if torch.cuda.get_device_capability(query.device) < (major, minor): raise ValueError( f"Query, key, and value must be on a CUDA device with compute capability >= {major}.{minor}." ) return check_device_cuda def _check_qkv_dtype_match(query: torch.Tensor, key: torch.Tensor, value: torch.Tensor, **kwargs) -> None: if query.dtype != key.dtype: raise ValueError("Query and key must have the same dtype.") if query.dtype != value.dtype: raise ValueError("Query and value must have the same dtype.") def _check_qkv_dtype_bf16_or_fp16(query: torch.Tensor, key: torch.Tensor, value: torch.Tensor, **kwargs) -> None: _check_qkv_dtype_match(query, key, value) if query.dtype not in (torch.bfloat16, torch.float16): raise ValueError("Query, key, and value must be either bfloat16 or float16.") def _check_shape( query: torch.Tensor, key: torch.Tensor, value: torch.Tensor, attn_mask: torch.Tensor | None = None, **kwargs, ) -> None: # Expected shapes: # query: (batch_size, seq_len_q, num_heads, head_dim) # key: (batch_size, seq_len_kv, num_heads, head_dim) # value: (batch_size, seq_len_kv, num_heads, head_dim) # attn_mask: (seq_len_q, seq_len_kv) or (batch_size, seq_len_q, seq_len_kv) # or (batch_size, num_heads, seq_len_q, seq_len_kv) if query.shape[-1] != key.shape[-1]: raise ValueError("Query and key must have the same head dimension.") if key.shape[-3] != value.shape[-3]: raise ValueError("Key and value must have the same sequence length.") if attn_mask is not None and attn_mask.shape[-1] != key.shape[-3]: raise ValueError("Attention mask must match the key's sequence length.") # ===== Helper functions ===== def _check_attention_backend_requirements(backend: AttentionBackendName) -> None: if backend in [AttentionBackendName.FLASH, AttentionBackendName.FLASH_VARLEN]: if not _CAN_USE_FLASH_ATTN: raise RuntimeError( f"Flash Attention backend '{backend.value}' is not usable because of missing package or the version is too old. Please install `flash-attn>={_REQUIRED_FLASH_VERSION}`." ) elif backend in [AttentionBackendName._FLASH_3, AttentionBackendName._FLASH_VARLEN_3]: if not _CAN_USE_FLASH_ATTN_3: raise RuntimeError( f"Flash Attention 3 backend '{backend.value}' is not usable because of missing package or the version is too old. Please build FA3 beta release from source." ) elif backend in [ AttentionBackendName.FLASH_HUB, AttentionBackendName.FLASH_VARLEN_HUB, AttentionBackendName._FLASH_3_HUB, AttentionBackendName._FLASH_3_VARLEN_HUB, AttentionBackendName.SAGE_HUB, ]: if not is_kernels_available(): raise RuntimeError( f"Backend '{backend.value}' is not usable because the `kernels` package isn't available. Please install it with `pip install kernels`." ) elif backend == AttentionBackendName.AITER: if not _CAN_USE_AITER_ATTN: raise RuntimeError( f"Aiter Attention backend '{backend.value}' is not usable because of missing package or the version is too old. Please install `aiter>={_REQUIRED_AITER_VERSION}`." ) elif backend in [ AttentionBackendName.SAGE, AttentionBackendName.SAGE_VARLEN, AttentionBackendName._SAGE_QK_INT8_PV_FP8_CUDA, AttentionBackendName._SAGE_QK_INT8_PV_FP8_CUDA_SM90, AttentionBackendName._SAGE_QK_INT8_PV_FP16_CUDA, AttentionBackendName._SAGE_QK_INT8_PV_FP16_TRITON, ]: if not _CAN_USE_SAGE_ATTN: raise RuntimeError( f"Sage Attention backend '{backend.value}' is not usable because of missing package or the version is too old. Please install `sageattention>={_REQUIRED_SAGE_VERSION}`." ) elif backend == AttentionBackendName.FLEX: if not _CAN_USE_FLEX_ATTN: raise RuntimeError( f"Flex Attention backend '{backend.value}' is not usable because of missing package or the version is too old. Please install `torch>=2.5.0`." ) elif backend == AttentionBackendName._NATIVE_NPU: if not _CAN_USE_NPU_ATTN: raise RuntimeError( f"NPU Attention backend '{backend.value}' is not usable because of missing package or the version is too old. Please install `torch_npu`." ) elif backend == AttentionBackendName._NATIVE_XLA: if not _CAN_USE_XLA_ATTN: raise RuntimeError( f"XLA Attention backend '{backend.value}' is not usable because of missing package or the version is too old. Please install `torch_xla>={_REQUIRED_XLA_VERSION}`." ) elif backend == AttentionBackendName.XFORMERS: if not _CAN_USE_XFORMERS_ATTN: raise RuntimeError( f"Xformers Attention backend '{backend.value}' is not usable because of missing package or the version is too old. Please install `xformers>={_REQUIRED_XFORMERS_VERSION}`." ) @functools.lru_cache(maxsize=128) def _prepare_for_flash_attn_or_sage_varlen_without_mask( batch_size: int, seq_len_q: int, seq_len_kv: int, device: torch.device | None = None, ): seqlens_q = torch.full((batch_size,), seq_len_q, dtype=torch.int32, device=device) seqlens_k = torch.full((batch_size,), seq_len_kv, dtype=torch.int32, device=device) cu_seqlens_q = torch.zeros(batch_size + 1, dtype=torch.int32, device=device) cu_seqlens_k = torch.zeros(batch_size + 1, dtype=torch.int32, device=device) cu_seqlens_q[1:] = torch.cumsum(seqlens_q, dim=0) cu_seqlens_k[1:] = torch.cumsum(seqlens_k, dim=0) max_seqlen_q = seqlens_q.max().item() max_seqlen_k = seqlens_k.max().item() return (seqlens_q, seqlens_k), (cu_seqlens_q, cu_seqlens_k), (max_seqlen_q, max_seqlen_k) def _prepare_for_flash_attn_or_sage_varlen_with_mask( batch_size: int, seq_len_q: int, attn_mask: torch.Tensor, device: torch.device | None = None, ): seqlens_q = torch.full((batch_size,), seq_len_q, dtype=torch.int32, device=device) seqlens_k = attn_mask.sum(dim=1, dtype=torch.int32) cu_seqlens_q = torch.zeros(batch_size + 1, dtype=torch.int32, device=device) cu_seqlens_k = torch.zeros(batch_size + 1, dtype=torch.int32, device=device) cu_seqlens_q[1:] = torch.cumsum(seqlens_q, dim=0) cu_seqlens_k[1:] = torch.cumsum(seqlens_k, dim=0) max_seqlen_q = seqlens_q.max().item() max_seqlen_k = seqlens_k.max().item() return (seqlens_q, seqlens_k), (cu_seqlens_q, cu_seqlens_k), (max_seqlen_q, max_seqlen_k) def _prepare_for_flash_attn_or_sage_varlen( batch_size: int, seq_len_q: int, seq_len_kv: int, attn_mask: torch.Tensor | None = None, device: torch.device | None = None, ) -> None: if attn_mask is None: return _prepare_for_flash_attn_or_sage_varlen_without_mask(batch_size, seq_len_q, seq_len_kv, device) return _prepare_for_flash_attn_or_sage_varlen_with_mask(batch_size, seq_len_q, attn_mask, device) def _normalize_attn_mask(attn_mask: torch.Tensor, batch_size: int, seq_len_k: int) -> torch.Tensor: """ Normalize an attention mask to shape [batch_size, seq_len_k] (bool) suitable for inferring seqlens_[q|k] in FlashAttention/Sage varlen. Supports 1D to 4D shapes and common broadcasting patterns. """ if attn_mask.dtype != torch.bool: raise ValueError(f"Attention mask must be of type bool, got {attn_mask.dtype}.") if attn_mask.ndim == 1: # [seq_len_k] -> broadcast across batch attn_mask = attn_mask.unsqueeze(0).expand(batch_size, seq_len_k) elif attn_mask.ndim == 2: # [batch_size, seq_len_k]. Maybe broadcast across batch if attn_mask.size(0) not in [1, batch_size]: raise ValueError( f"attn_mask.shape[0] ({attn_mask.shape[0]}) must be 1 or {batch_size} for 2D attention mask." ) attn_mask = attn_mask.expand(batch_size, seq_len_k) elif attn_mask.ndim == 3: # [batch_size, seq_len_q, seq_len_k] -> reduce over query dimension # We do this reduction because we know that arbitrary QK masks is not supported in Flash/Sage varlen. if attn_mask.size(0) not in [1, batch_size]: raise ValueError( f"attn_mask.shape[0] ({attn_mask.shape[0]}) must be 1 or {batch_size} for 3D attention mask." ) attn_mask = attn_mask.any(dim=1) attn_mask = attn_mask.expand(batch_size, seq_len_k) elif attn_mask.ndim == 4: # [batch_size, num_heads, seq_len_q, seq_len_k] or broadcastable versions if attn_mask.size(0) not in [1, batch_size]: raise ValueError( f"attn_mask.shape[0] ({attn_mask.shape[0]}) must be 1 or {batch_size} for 4D attention mask." ) attn_mask = attn_mask.expand(batch_size, -1, -1, seq_len_k) # [B, H, Q, K] attn_mask = attn_mask.any(dim=(1, 2)) # [B, K] else: raise ValueError(f"Unsupported attention mask shape: {attn_mask.shape}") if attn_mask.shape != (batch_size, seq_len_k): raise ValueError( f"Normalized attention mask shape mismatch: got {attn_mask.shape}, expected ({batch_size}, {seq_len_k})" ) return attn_mask def _flex_attention_causal_mask_mod(batch_idx, head_idx, q_idx, kv_idx): return q_idx >= kv_idx # ===== Helpers for downloading kernels ===== def _resolve_kernel_attr(module, attr_path: str): target = module for attr in attr_path.split("."): if not hasattr(target, attr): raise AttributeError(f"Kernel module '{module.__name__}' does not define attribute path '{attr_path}'.") target = getattr(target, attr) return target def _maybe_download_kernel_for_backend(backend: AttentionBackendName) -> None: if backend not in _HUB_KERNELS_REGISTRY: return config = _HUB_KERNELS_REGISTRY[backend] needs_kernel = config.kernel_fn is None needs_wrapped_forward = config.wrapped_forward_attr is not None and config.wrapped_forward_fn is None needs_wrapped_backward = config.wrapped_backward_attr is not None and config.wrapped_backward_fn is None if not (needs_kernel or needs_wrapped_forward or needs_wrapped_backward): return try: from kernels import get_kernel kernel_module = get_kernel(config.repo_id, revision=config.revision) if needs_kernel: config.kernel_fn = _resolve_kernel_attr(kernel_module, config.function_attr) if needs_wrapped_forward: config.wrapped_forward_fn = _resolve_kernel_attr(kernel_module, config.wrapped_forward_attr) if needs_wrapped_backward: config.wrapped_backward_fn = _resolve_kernel_attr(kernel_module, config.wrapped_backward_attr) except Exception as e: logger.error(f"An error occurred while fetching kernel '{config.repo_id}' from the Hub: {e}") raise # ===== torch op registrations ===== # Registrations are required for fullgraph tracing compatibility # TODO: this is only required because the beta release FA3 does not have it. There is a PR adding # this but it was never merged: https://github.com/Dao-AILab/flash-attention/pull/1590 @_custom_op("_diffusers_flash_attn_3::_flash_attn_forward", mutates_args=(), device_types="cuda") def _wrapped_flash_attn_3( q: torch.Tensor, k: torch.Tensor, v: torch.Tensor, softmax_scale: float | None = None, causal: bool = False, qv: torch.Tensor | None = None, q_descale: torch.Tensor | None = None, k_descale: torch.Tensor | None = None, v_descale: torch.Tensor | None = None, attention_chunk: int = 0, softcap: float = 0.0, num_splits: int = 1, pack_gqa: bool | None = None, deterministic: bool = False, sm_margin: int = 0, ) -> tuple[torch.Tensor, torch.Tensor]: # Hardcoded for now because pytorch does not support tuple/int type hints window_size = (-1, -1) result = flash_attn_3_func( q=q, k=k, v=v, softmax_scale=softmax_scale, causal=causal, qv=qv, q_descale=q_descale, k_descale=k_descale, v_descale=v_descale, window_size=window_size, attention_chunk=attention_chunk, softcap=softcap, num_splits=num_splits, pack_gqa=pack_gqa, deterministic=deterministic, sm_margin=sm_margin, return_attn_probs=True, ) out, lse, *_ = result lse = lse.permute(0, 2, 1) return out, lse @_register_fake("_diffusers_flash_attn_3::_flash_attn_forward") def _( q: torch.Tensor, k: torch.Tensor, v: torch.Tensor, softmax_scale: float | None = None, causal: bool = False, qv: torch.Tensor | None = None, q_descale: torch.Tensor | None = None, k_descale: torch.Tensor | None = None, v_descale: torch.Tensor | None = None, attention_chunk: int = 0, softcap: float = 0.0, num_splits: int = 1, pack_gqa: bool | None = None, deterministic: bool = False, sm_margin: int = 0, ) -> tuple[torch.Tensor, torch.Tensor]: window_size = (-1, -1) # noqa: F841 # A lot of the parameters here are not yet used in any way within diffusers. # We can safely ignore for now and keep the fake op shape propagation simple. batch_size, seq_len, num_heads, head_dim = q.shape lse_shape = (batch_size, seq_len, num_heads) return torch.empty_like(q), q.new_empty(lse_shape) # ===== Helper functions to use attention backends with templated CP autograd functions ===== def _native_attention_forward_op( ctx: torch.autograd.function.FunctionCtx, query: torch.Tensor, key: torch.Tensor, value: torch.Tensor, attn_mask: torch.Tensor | None = None, dropout_p: float = 0.0, is_causal: bool = False, scale: float | None = None, enable_gqa: bool = False, return_lse: bool = False, _save_ctx: bool = True, _parallel_config: "ParallelConfig" | None = None, ): # Native attention does not return_lse if return_lse: raise ValueError("Native attention does not support return_lse=True") # used for backward pass if _save_ctx: ctx.save_for_backward(query, key, value) ctx.attn_mask = attn_mask ctx.dropout_p = dropout_p ctx.is_causal = is_causal ctx.scale = scale ctx.enable_gqa = enable_gqa query, key, value = (x.permute(0, 2, 1, 3) for x in (query, key, value)) out = torch.nn.functional.scaled_dot_product_attention( query=query, key=key, value=value, attn_mask=attn_mask, dropout_p=dropout_p, is_causal=is_causal, scale=scale, enable_gqa=enable_gqa, ) out = out.permute(0, 2, 1, 3) return out def _native_attention_backward_op( ctx: torch.autograd.function.FunctionCtx, grad_out: torch.Tensor, *args, **kwargs, ): query, key, value = ctx.saved_tensors query.requires_grad_(True) key.requires_grad_(True) value.requires_grad_(True) query_t, key_t, value_t = (x.permute(0, 2, 1, 3) for x in (query, key, value)) out = torch.nn.functional.scaled_dot_product_attention( query=query_t, key=key_t, value=value_t, attn_mask=ctx.attn_mask, dropout_p=ctx.dropout_p, is_causal=ctx.is_causal, scale=ctx.scale, enable_gqa=ctx.enable_gqa, ) out = out.permute(0, 2, 1, 3) grad_out_t = grad_out.permute(0, 2, 1, 3) grad_query_t, grad_key_t, grad_value_t = torch.autograd.grad( outputs=out, inputs=[query_t, key_t, value_t], grad_outputs=grad_out_t, retain_graph=False ) grad_query = grad_query_t.permute(0, 2, 1, 3) grad_key = grad_key_t.permute(0, 2, 1, 3) grad_value = grad_value_t.permute(0, 2, 1, 3) return grad_query, grad_key, grad_value # https://github.com/pytorch/pytorch/blob/8904ba638726f8c9a5aff5977c4aa76c9d2edfa6/aten/src/ATen/native/native_functions.yaml#L14958 # forward declaration: # aten::_scaled_dot_product_cudnn_attention(Tensor query, Tensor key, Tensor value, Tensor? attn_bias, bool compute_log_sumexp, float dropout_p=0., bool is_causal=False, bool return_debug_mask=False, *, float? scale=None) -> (Tensor output, Tensor logsumexp, Tensor cum_seq_q, Tensor cum_seq_k, SymInt max_q, SymInt max_k, Tensor philox_seed, Tensor philox_offset, Tensor debug_attn_mask) def _cudnn_attention_forward_op( ctx: torch.autograd.function.FunctionCtx, query: torch.Tensor, key: torch.Tensor, value: torch.Tensor, attn_mask: torch.Tensor | None = None, dropout_p: float = 0.0, is_causal: bool = False, scale: float | None = None, enable_gqa: bool = False, return_lse: bool = False, _save_ctx: bool = True, _parallel_config: "ParallelConfig" | None = None, ): if enable_gqa: raise ValueError("`enable_gqa` is not yet supported for cuDNN attention.") tensors_to_save = () # Contiguous is a must here! Calling cuDNN backend with aten ops produces incorrect results # if the input tensors are not contiguous. query = query.transpose(1, 2).contiguous() key = key.transpose(1, 2).contiguous() value = value.transpose(1, 2).contiguous() tensors_to_save += (query, key, value) out, lse, cum_seq_q, cum_seq_k, max_q, max_k, philox_seed, philox_offset, debug_attn_mask = ( torch.ops.aten._scaled_dot_product_cudnn_attention( query=query, key=key, value=value, attn_bias=attn_mask, compute_log_sumexp=return_lse, dropout_p=dropout_p, is_causal=is_causal, return_debug_mask=False, scale=scale, ) ) tensors_to_save += (out, lse, cum_seq_q, cum_seq_k, philox_seed, philox_offset) if _save_ctx: ctx.save_for_backward(*tensors_to_save) ctx.dropout_p = dropout_p ctx.is_causal = is_causal ctx.scale = scale ctx.attn_mask = attn_mask ctx.max_q = max_q ctx.max_k = max_k out = out.transpose(1, 2).contiguous() if lse is not None: lse = lse.transpose(1, 2).contiguous() return (out, lse) if return_lse else out # backward declaration: # aten::_scaled_dot_product_cudnn_attention_backward(Tensor grad_out, Tensor query, Tensor key, Tensor value, Tensor out, Tensor logsumexp, Tensor philox_seed, Tensor philox_offset, Tensor attn_bias, Tensor cum_seq_q, Tensor cum_seq_k, SymInt max_q, SymInt max_k, float dropout_p, bool is_causal, *, float? scale=None) -> (Tensor, Tensor, Tensor) def _cudnn_attention_backward_op( ctx: torch.autograd.function.FunctionCtx, grad_out: torch.Tensor, *args, **kwargs, ): query, key, value, out, lse, cum_seq_q, cum_seq_k, philox_seed, philox_offset = ctx.saved_tensors grad_out = grad_out.transpose(1, 2).contiguous() key = key.transpose(1, 2).contiguous() value = value.transpose(1, 2).contiguous() # Cannot pass first 5 arguments as kwargs because: https://github.com/pytorch/pytorch/blob/d26ca5de058dbcf56ac52bb43e84dd98df2ace97/torch/_dynamo/variables/torch.py#L1341 grad_query, grad_key, grad_value = torch.ops.aten._scaled_dot_product_cudnn_attention_backward( grad_out, query, key, value, out, logsumexp=lse, philox_seed=philox_seed, philox_offset=philox_offset, attn_bias=ctx.attn_mask, cum_seq_q=cum_seq_q, cum_seq_k=cum_seq_k, max_q=ctx.max_q, max_k=ctx.max_k, dropout_p=ctx.dropout_p, is_causal=ctx.is_causal, scale=ctx.scale, ) grad_query, grad_key, grad_value = (x.transpose(1, 2).contiguous() for x in (grad_query, grad_key, grad_value)) return grad_query, grad_key, grad_value # https://github.com/pytorch/pytorch/blob/e33fa0ece36a93dbc8ff19b0251b8d99f8ae8668/aten/src/ATen/native/native_functions.yaml#L15135 # forward declaration: # aten::_scaled_dot_product_flash_attention(Tensor query, Tensor key, Tensor value, float dropout_p=0.0, bool is_causal=False, bool return_debug_mask=False, *, float? scale=None) -> (Tensor output, Tensor logsumexp, Tensor cum_seq_q, Tensor cum_seq_k, SymInt max_q, SymInt max_k, Tensor rng_state, Tensor unused, Tensor debug_attn_mask) def _native_flash_attention_forward_op( ctx: torch.autograd.function.FunctionCtx, query: torch.Tensor, key: torch.Tensor, value: torch.Tensor, attn_mask: torch.Tensor | None = None, dropout_p: float = 0.0, is_causal: bool = False, scale: float | None = None, enable_gqa: bool = False, return_lse: bool = False, _save_ctx: bool = True, _parallel_config: "ParallelConfig" | None = None, ): if enable_gqa: raise ValueError("`enable_gqa` is not yet supported for native flash attention.") tensors_to_save = () query = query.transpose(1, 2).contiguous() key = key.transpose(1, 2).contiguous() value = value.transpose(1, 2).contiguous() tensors_to_save += (query, key, value) out, lse, cum_seq_q, cum_seq_k, max_q, max_k, philox_seed, philox_offset, debug_attn_mask = ( torch.ops.aten._scaled_dot_product_flash_attention( query=query, key=key, value=value, dropout_p=dropout_p, is_causal=is_causal, return_debug_mask=False, scale=scale, ) ) tensors_to_save += (out, lse, cum_seq_q, cum_seq_k, philox_seed, philox_offset) if _save_ctx: ctx.save_for_backward(*tensors_to_save) ctx.dropout_p = dropout_p ctx.is_causal = is_causal ctx.scale = scale ctx.max_q = max_q ctx.max_k = max_k out = out.transpose(1, 2).contiguous() if lse is not None: lse = lse.transpose(1, 2).contiguous() return (out, lse) if return_lse else out # https://github.com/pytorch/pytorch/blob/e33fa0ece36a93dbc8ff19b0251b8d99f8ae8668/aten/src/ATen/native/native_functions.yaml#L15153 # backward declaration: # aten::_scaled_dot_product_flash_attention_backward(Tensor grad_out, Tensor query, Tensor key, Tensor value, Tensor out, Tensor logsumexp, Tensor cum_seq_q, Tensor cum_seq_k, SymInt max_q, SymInt max_k, float dropout_p, bool is_causal, Tensor philox_seed, Tensor philox_offset, *, float? scale=None) -> (Tensor grad_query, Tensor grad_key, Tensor grad_value) def _native_flash_attention_backward_op( ctx: torch.autograd.function.FunctionCtx, grad_out: torch.Tensor, *args, **kwargs, ): query, key, value, out, lse, cum_seq_q, cum_seq_k, philox_seed, philox_offset = ctx.saved_tensors grad_out = grad_out.transpose(1, 2).contiguous() key = key.transpose(1, 2).contiguous() value = value.transpose(1, 2).contiguous() grad_query, grad_key, grad_value = torch.ops.aten._scaled_dot_product_flash_attention_backward( grad_out, query, key, value, out, logsumexp=lse, philox_seed=philox_seed, philox_offset=philox_offset, cum_seq_q=cum_seq_q, cum_seq_k=cum_seq_k, max_q=ctx.max_q, max_k=ctx.max_k, dropout_p=ctx.dropout_p, is_causal=ctx.is_causal, scale=ctx.scale, ) grad_query, grad_key, grad_value = (x.transpose(1, 2).contiguous() for x in (grad_query, grad_key, grad_value)) return grad_query, grad_key, grad_value # Adapted from: https://github.com/Dao-AILab/flash-attention/blob/fd2fc9d85c8e54e5c20436465bca709bc1a6c5a1/flash_attn/flash_attn_interface.py#L807 def _flash_attention_forward_op( ctx: torch.autograd.function.FunctionCtx, query: torch.Tensor, key: torch.Tensor, value: torch.Tensor, attn_mask: torch.Tensor | None = None, dropout_p: float = 0.0, is_causal: bool = False, scale: float | None = None, enable_gqa: bool = False, return_lse: bool = False, _save_ctx: bool = True, _parallel_config: "ParallelConfig" | None = None, ): if attn_mask is not None: raise ValueError("`attn_mask` is not yet supported for flash-attn 2.") if enable_gqa: raise ValueError("`enable_gqa` is not yet supported for flash-attn 2.") # Hardcoded for now window_size = (-1, -1) softcap = 0.0 alibi_slopes = None deterministic = False grad_enabled = any(x.requires_grad for x in (query, key, value)) if scale is None: scale = query.shape[-1] ** (-0.5) # flash-attn only returns LSE if dropout_p > 0. So, we need to workaround. if grad_enabled or (_parallel_config is not None and _parallel_config.context_parallel_config._world_size > 1): dropout_p = dropout_p if dropout_p > 0 else 1e-30 with torch.set_grad_enabled(grad_enabled): out, lse, S_dmask, rng_state = _wrapped_flash_attn_forward( query, key, value, dropout_p, scale, is_causal, window_size[0], window_size[1], softcap, alibi_slopes, return_lse, ) lse = lse.permute(0, 2, 1) if _save_ctx: ctx.save_for_backward(query, key, value, out, lse, rng_state) ctx.dropout_p = dropout_p ctx.scale = scale ctx.is_causal = is_causal ctx.window_size = window_size ctx.softcap = softcap ctx.alibi_slopes = alibi_slopes ctx.deterministic = deterministic return (out, lse) if return_lse else out def _flash_attention_backward_op( ctx: torch.autograd.function.FunctionCtx, grad_out: torch.Tensor, *args, **kwargs, ): query, key, value, out, lse, rng_state = ctx.saved_tensors grad_query, grad_key, grad_value = torch.empty_like(query), torch.empty_like(key), torch.empty_like(value) lse_d = _wrapped_flash_attn_backward( # noqa: F841 grad_out, query, key, value, out, lse, grad_query, grad_key, grad_value, ctx.dropout_p, ctx.scale, ctx.is_causal, ctx.window_size[0], ctx.window_size[1], ctx.softcap, ctx.alibi_slopes, ctx.deterministic, rng_state, ) # Head dimension may have been padded grad_query = grad_query[..., : grad_out.shape[-1]] grad_key = grad_key[..., : grad_out.shape[-1]] grad_value = grad_value[..., : grad_out.shape[-1]] return grad_query, grad_key, grad_value def _flash_attention_hub_forward_op( ctx: torch.autograd.function.FunctionCtx, query: torch.Tensor, key: torch.Tensor, value: torch.Tensor, attn_mask: torch.Tensor | None = None, dropout_p: float = 0.0, is_causal: bool = False, scale: float | None = None, enable_gqa: bool = False, return_lse: bool = False, _save_ctx: bool = True, _parallel_config: "ParallelConfig" | None = None, ): if attn_mask is not None: raise ValueError("`attn_mask` is not yet supported for flash-attn hub kernels.") if enable_gqa: raise ValueError("`enable_gqa` is not yet supported for flash-attn hub kernels.") config = _HUB_KERNELS_REGISTRY[AttentionBackendName.FLASH_HUB] wrapped_forward_fn = config.wrapped_forward_fn wrapped_backward_fn = config.wrapped_backward_fn if wrapped_forward_fn is None or wrapped_backward_fn is None: raise RuntimeError( "Flash attention hub kernels must expose `_wrapped_flash_attn_forward` and `_wrapped_flash_attn_backward` " "for context parallel execution." ) if scale is None: scale = query.shape[-1] ** (-0.5) window_size = (-1, -1) softcap = 0.0 alibi_slopes = None deterministic = False grad_enabled = any(x.requires_grad for x in (query, key, value)) if grad_enabled or (_parallel_config is not None and _parallel_config.context_parallel_config._world_size > 1): dropout_p = dropout_p if dropout_p > 0 else 1e-30 with torch.set_grad_enabled(grad_enabled): out, lse, S_dmask, rng_state = wrapped_forward_fn( query, key, value, dropout_p, scale, is_causal, window_size[0], window_size[1], softcap, alibi_slopes, return_lse, ) lse = lse.permute(0, 2, 1).contiguous() if _save_ctx: ctx.save_for_backward(query, key, value, out, lse, rng_state) ctx.dropout_p = dropout_p ctx.scale = scale ctx.is_causal = is_causal ctx.window_size = window_size ctx.softcap = softcap ctx.alibi_slopes = alibi_slopes ctx.deterministic = deterministic return (out, lse) if return_lse else out def _flash_attention_hub_backward_op( ctx: torch.autograd.function.FunctionCtx, grad_out: torch.Tensor, *args, **kwargs, ): config = _HUB_KERNELS_REGISTRY[AttentionBackendName.FLASH_HUB] wrapped_backward_fn = config.wrapped_backward_fn if wrapped_backward_fn is None: raise RuntimeError( "Flash attention hub kernels must expose `_wrapped_flash_attn_backward` for context parallel execution." ) query, key, value, out, lse, rng_state = ctx.saved_tensors grad_query, grad_key, grad_value = torch.empty_like(query), torch.empty_like(key), torch.empty_like(value) _ = wrapped_backward_fn( grad_out, query, key, value, out, lse, grad_query, grad_key, grad_value, ctx.dropout_p, ctx.scale, ctx.is_causal, ctx.window_size[0], ctx.window_size[1], ctx.softcap, ctx.alibi_slopes, ctx.deterministic, rng_state, ) grad_query = grad_query[..., : grad_out.shape[-1]] grad_key = grad_key[..., : grad_out.shape[-1]] grad_value = grad_value[..., : grad_out.shape[-1]] return grad_query, grad_key, grad_value def _flash_attention_3_hub_forward_op( ctx: torch.autograd.function.FunctionCtx, query: torch.Tensor, key: torch.Tensor, value: torch.Tensor, attn_mask: torch.Tensor | None = None, dropout_p: float = 0.0, is_causal: bool = False, scale: float | None = None, enable_gqa: bool = False, return_lse: bool = False, _save_ctx: bool = True, _parallel_config: "ParallelConfig" | None = None, *, window_size: tuple[int, int] = (-1, -1), softcap: float = 0.0, num_splits: int = 1, pack_gqa: bool | None = None, deterministic: bool = False, sm_margin: int = 0, ): if attn_mask is not None: raise ValueError("`attn_mask` is not yet supported for flash-attn 3 hub kernels.") if dropout_p != 0.0: raise ValueError("`dropout_p` is not yet supported for flash-attn 3 hub kernels.") if enable_gqa: raise ValueError("`enable_gqa` is not yet supported for flash-attn 3 hub kernels.") config = _HUB_KERNELS_REGISTRY[AttentionBackendName._FLASH_3_HUB] wrapped_forward_fn = config.wrapped_forward_fn if wrapped_forward_fn is None: raise RuntimeError( "Flash attention 3 hub kernels must expose `flash_attn_interface._flash_attn_forward` " "for context parallel execution." ) if scale is None: scale = query.shape[-1] ** (-0.5) out, softmax_lse, *_ = wrapped_forward_fn( query, key, value, None, None, # k_new, v_new None, # qv None, # out None, None, None, # cu_seqlens_q/k/k_new None, None, # seqused_q/k None, None, # max_seqlen_q/k None, None, None, # page_table, kv_batch_idx, leftpad_k None, None, None, # rotary_cos/sin, seqlens_rotary None, None, None, # q_descale, k_descale, v_descale scale, causal=is_causal, window_size_left=window_size[0], window_size_right=window_size[1], attention_chunk=0, softcap=softcap, num_splits=num_splits, pack_gqa=pack_gqa, sm_margin=sm_margin, ) lse = softmax_lse.permute(0, 2, 1).contiguous() if return_lse else None if _save_ctx: ctx.save_for_backward(query, key, value, out, softmax_lse) ctx.scale = scale ctx.is_causal = is_causal ctx.window_size = window_size ctx.softcap = softcap ctx.deterministic = deterministic ctx.sm_margin = sm_margin return (out, lse) if return_lse else out def _flash_attention_3_hub_backward_op( ctx: torch.autograd.function.FunctionCtx, grad_out: torch.Tensor, *args, **kwargs, ): config = _HUB_KERNELS_REGISTRY[AttentionBackendName._FLASH_3_HUB] wrapped_backward_fn = config.wrapped_backward_fn if wrapped_backward_fn is None: raise RuntimeError( "Flash attention 3 hub kernels must expose `flash_attn_interface._flash_attn_backward` " "for context parallel execution." ) query, key, value, out, softmax_lse = ctx.saved_tensors grad_query = torch.empty_like(query) grad_key = torch.empty_like(key) grad_value = torch.empty_like(value) wrapped_backward_fn( grad_out, query, key, value, out, softmax_lse, None, None, # cu_seqlens_q, cu_seqlens_k None, None, # seqused_q, seqused_k None, None, # max_seqlen_q, max_seqlen_k grad_query, grad_key, grad_value, ctx.scale, ctx.is_causal, ctx.window_size[0], ctx.window_size[1], ctx.softcap, ctx.deterministic, ctx.sm_margin, ) grad_query = grad_query[..., : grad_out.shape[-1]] grad_key = grad_key[..., : grad_out.shape[-1]] grad_value = grad_value[..., : grad_out.shape[-1]] return grad_query, grad_key, grad_value def _sage_attention_forward_op( ctx: torch.autograd.function.FunctionCtx, query: torch.Tensor, key: torch.Tensor, value: torch.Tensor, attn_mask: torch.Tensor | None = None, dropout_p: float = 0.0, is_causal: bool = False, scale: float | None = None, enable_gqa: bool = False, return_lse: bool = False, _save_ctx: bool = True, _parallel_config: "ParallelConfig" | None = None, ): if attn_mask is not None: raise ValueError("`attn_mask` is not yet supported for Sage attention.") if dropout_p > 0.0: raise ValueError("`dropout_p` is not yet supported for Sage attention.") if enable_gqa: raise ValueError("`enable_gqa` is not yet supported for Sage attention.") out = sageattn( q=query, k=key, v=value, tensor_layout="NHD", is_causal=is_causal, sm_scale=scale, return_lse=return_lse, ) lse = None if return_lse: out, lse, *_ = out lse = lse.permute(0, 2, 1) return (out, lse) if return_lse else out def _sage_attention_hub_forward_op( ctx: torch.autograd.function.FunctionCtx, query: torch.Tensor, key: torch.Tensor, value: torch.Tensor, attn_mask: torch.Tensor | None = None, dropout_p: float = 0.0, is_causal: bool = False, scale: float | None = None, enable_gqa: bool = False, return_lse: bool = False, _save_ctx: bool = True, _parallel_config: "ParallelConfig" | None = None, ): if attn_mask is not None: raise ValueError("`attn_mask` is not yet supported for Sage attention.") if dropout_p > 0.0: raise ValueError("`dropout_p` is not yet supported for Sage attention.") if enable_gqa: raise ValueError("`enable_gqa` is not yet supported for Sage attention.") func = _HUB_KERNELS_REGISTRY[AttentionBackendName.SAGE_HUB].kernel_fn out = func( q=query, k=key, v=value, tensor_layout="NHD", is_causal=is_causal, sm_scale=scale, return_lse=return_lse, ) lse = None if return_lse: out, lse, *_ = out lse = lse.permute(0, 2, 1).contiguous() return (out, lse) if return_lse else out def _sage_attention_backward_op( ctx: torch.autograd.function.FunctionCtx, grad_out: torch.Tensor, *args, ): raise NotImplementedError("Backward pass is not implemented for Sage attention.") def _maybe_modify_attn_mask_npu(query: torch.Tensor, key: torch.Tensor, attn_mask: torch.Tensor | None = None): # Skip Attention Mask if all values are 1, `None` mask can speedup the computation if attn_mask is not None and torch.all(attn_mask != 0): attn_mask = None # Reshape Attention Mask: [batch_size, seq_len_k] -> [batch_size, 1, sqe_len_q, seq_len_k] # https://www.hiascend.com/document/detail/zh/Pytorch/730/apiref/torchnpuCustomsapi/docs/context/torch_npu-npu_fusion_attention.md if ( attn_mask is not None and attn_mask.ndim == 2 and attn_mask.shape[0] == query.shape[0] and attn_mask.shape[1] == key.shape[1] ): B, Sq, Skv = attn_mask.shape[0], query.shape[1], key.shape[1] attn_mask = ~attn_mask.to(torch.bool) attn_mask = attn_mask.unsqueeze(1).expand(B, Sq, Skv).unsqueeze(1).contiguous() return attn_mask def _npu_attention_forward_op( ctx: torch.autograd.function.FunctionCtx, query: torch.Tensor, key: torch.Tensor, value: torch.Tensor, attn_mask: torch.Tensor | None = None, dropout_p: float = 0.0, is_causal: bool = False, scale: float | None = None, enable_gqa: bool = False, return_lse: bool = False, _save_ctx: bool = True, _parallel_config: "ParallelConfig" | None = None, ): if return_lse: raise ValueError("NPU attention backend does not support setting `return_lse=True`.") attn_mask = _maybe_modify_attn_mask_npu(query, key, attn_mask) out = npu_fusion_attention( query, key, value, query.size(2), # num_heads atten_mask=attn_mask, input_layout="BSND", pse=None, scale=1.0 / math.sqrt(query.shape[-1]) if scale is None else scale, pre_tockens=65536, next_tockens=65536, keep_prob=1.0 - dropout_p, sync=False, inner_precise=0, )[0] return out # Not implemented yet. def _npu_attention_backward_op( ctx: torch.autograd.function.FunctionCtx, grad_out: torch.Tensor, *args, **kwargs, ): raise NotImplementedError("Backward pass is not implemented for Npu Fusion Attention.") # ===== Context parallel ===== # Reference: # - https://github.com/pytorch/pytorch/blob/f58a680d09e13658a52c6ba05c63c15759846bcc/torch/distributed/_functional_collectives.py#L827 # - https://github.com/pytorch/pytorch/blob/f58a680d09e13658a52c6ba05c63c15759846bcc/torch/distributed/_functional_collectives.py#L246 # For fullgraph=True tracing compatibility (since FakeTensor does not have a `wait` method): def _wait_tensor(tensor): if isinstance(tensor, funcol.AsyncCollectiveTensor): tensor = tensor.wait() return tensor def _all_to_all_single(x: torch.Tensor, group) -> torch.Tensor: shape = x.shape # HACK: We need to flatten because despite making tensors contiguous, torch single-file-ization # to benchmark triton codegen fails somewhere: # buf25 = torch.ops._c10d_functional.all_to_all_single.default(buf24, [1, 1], [1, 1], '3') # ValueError: Tensors must be contiguous x = x.flatten() x = funcol.all_to_all_single(x, None, None, group) x = x.reshape(shape) x = _wait_tensor(x) return x def _all_to_all_dim_exchange(x: torch.Tensor, scatter_idx: int = 2, gather_idx: int = 1, group=None) -> torch.Tensor: """ Perform dimension sharding / reassembly across processes using _all_to_all_single. This utility reshapes and redistributes tensor `x` across the given process group, across sequence dimension or head dimension flexibly by accepting scatter_idx and gather_idx. Args: x (torch.Tensor): Input tensor. Expected shapes: - When scatter_idx=2, gather_idx=1: (batch_size, seq_len_local, num_heads, head_dim) - When scatter_idx=1, gather_idx=2: (batch_size, seq_len, num_heads_local, head_dim) scatter_idx (int) : Dimension along which the tensor is partitioned before all-to-all. gather_idx (int): Dimension along which the output is reassembled after all-to-all. group : Distributed process group for the Ulysses group. Returns: torch.Tensor: Tensor with globally exchanged dimensions. - For (scatter_idx=2 → gather_idx=1): (batch_size, seq_len, num_heads_local, head_dim) - For (scatter_idx=1 → gather_idx=2): (batch_size, seq_len_local, num_heads, head_dim) """ group_world_size = torch.distributed.get_world_size(group) if scatter_idx == 2 and gather_idx == 1: # Used before Ulysses sequence parallel (SP) attention. Scatters the gathers sequence # dimension and scatters head dimension batch_size, seq_len_local, num_heads, head_dim = x.shape seq_len = seq_len_local * group_world_size num_heads_local = num_heads // group_world_size # B, S_LOCAL, H, D -> group_world_size, S_LOCAL, B, H_LOCAL, D x_temp = ( x.reshape(batch_size, seq_len_local, group_world_size, num_heads_local, head_dim) .transpose(0, 2) .contiguous() ) if group_world_size > 1: out = _all_to_all_single(x_temp, group=group) else: out = x_temp # group_world_size, S_LOCAL, B, H_LOCAL, D -> B, S, H_LOCAL, D out = out.reshape(seq_len, batch_size, num_heads_local, head_dim).permute(1, 0, 2, 3).contiguous() out = out.reshape(batch_size, seq_len, num_heads_local, head_dim) return out elif scatter_idx == 1 and gather_idx == 2: # Used after ulysses sequence parallel in unified SP. gathers the head dimension # scatters back the sequence dimension. batch_size, seq_len, num_heads_local, head_dim = x.shape num_heads = num_heads_local * group_world_size seq_len_local = seq_len // group_world_size # B, S, H_LOCAL, D -> group_world_size, H_LOCAL, S_LOCAL, B, D x_temp = ( x.reshape(batch_size, group_world_size, seq_len_local, num_heads_local, head_dim) .permute(1, 3, 2, 0, 4) .reshape(group_world_size, num_heads_local, seq_len_local, batch_size, head_dim) ) if group_world_size > 1: output = _all_to_all_single(x_temp, group) else: output = x_temp output = output.reshape(num_heads, seq_len_local, batch_size, head_dim).transpose(0, 2).contiguous() output = output.reshape(batch_size, seq_len_local, num_heads, head_dim) return output else: raise ValueError("Invalid scatter/gather indices for _all_to_all_dim_exchange.") class SeqAllToAllDim(torch.autograd.Function): """ all_to_all operation for unified sequence parallelism. uses _all_to_all_dim_exchange, see _all_to_all_dim_exchange for more info. """ @staticmethod def forward(ctx, group, input, scatter_id=2, gather_id=1): ctx.group = group ctx.scatter_id = scatter_id ctx.gather_id = gather_id return _all_to_all_dim_exchange(input, scatter_id, gather_id, group) @staticmethod def backward(ctx, grad_outputs): grad_input = SeqAllToAllDim.apply( ctx.group, grad_outputs, ctx.gather_id, # reversed ctx.scatter_id, # reversed ) return (None, grad_input, None, None) # Below are helper functions to handle abritrary head num and abritrary sequence length for Ulysses Anything Attention. def _maybe_pad_qkv_head(x: torch.Tensor, H: int, group: dist.ProcessGroup) -> tuple[torch.Tensor, int]: r"""Maybe pad the head dimension to be divisible by world_size. x: torch.Tensor, shape (B, S_LOCAL, H, D) H: int, original global head num return: tuple[torch.Tensor, int], padded tensor (B, S_LOCAL, H + H_PAD, D) and H_PAD """ world_size = dist.get_world_size(group=group) H_PAD = 0 if H % world_size != 0: H_PAD = world_size - (H % world_size) NEW_H_LOCAL = (H + H_PAD) // world_size # e.g., Allow: H=30, world_size=8 -> NEW_H_LOCAL=4, H_PAD=2. # NOT ALLOW: H=30, world_size=16 -> NEW_H_LOCAL=2, H_PAD=14. assert H_PAD < NEW_H_LOCAL, f"Padding head num {H_PAD} should be less than new local head num {NEW_H_LOCAL}" x = F.pad(x, (0, 0, 0, H_PAD)).contiguous() return x, H_PAD def _maybe_unpad_qkv_head(x: torch.Tensor, H_PAD: int, group: dist.ProcessGroup) -> torch.Tensor: r"""Maybe unpad the head dimension. x: torch.Tensor, shape (B, S_GLOBAL, H_LOCAL + H_PAD, D) H_PAD: int, head padding num return: torch.Tensor, unpadded tensor (B, S_GLOBAL, H_LOCAL, D) """ rank = dist.get_rank(group=group) world_size = dist.get_world_size(group=group) # Only the last rank may have padding if H_PAD > 0 and rank == world_size - 1: x = x[:, :, :-H_PAD, :] return x.contiguous() def _maybe_pad_o_head(x: torch.Tensor, H: int, group: dist.ProcessGroup) -> tuple[torch.Tensor, int]: r"""Maybe pad the head dimension to be divisible by world_size. x: torch.Tensor, shape (B, S_GLOBAL, H_LOCAL, D) H: int, original global head num return: tuple[torch.Tensor, int], padded tensor (B, S_GLOBAL, H_LOCAL + H_PAD, D) and H_PAD """ if H is None: return x, 0 rank = dist.get_rank(group=group) world_size = dist.get_world_size(group=group) H_PAD = 0 # Only the last rank may need padding if H % world_size != 0: # We need to broadcast H_PAD to all ranks to keep consistency # in unpadding step later for all ranks. H_PAD = world_size - (H % world_size) NEW_H_LOCAL = (H + H_PAD) // world_size assert H_PAD < NEW_H_LOCAL, f"Padding head num {H_PAD} should be less than new local head num {NEW_H_LOCAL}" if rank == world_size - 1: x = F.pad(x, (0, 0, 0, H_PAD)).contiguous() return x, H_PAD def _maybe_unpad_o_head(x: torch.Tensor, H_PAD: int, group: dist.ProcessGroup) -> torch.Tensor: r"""Maybe unpad the head dimension. x: torch.Tensor, shape (B, S_LOCAL, H_GLOBAL + H_PAD, D) H_PAD: int, head padding num return: torch.Tensor, unpadded tensor (B, S_LOCAL, H_GLOBAL, D) """ if H_PAD > 0: x = x[:, :, :-H_PAD, :] return x.contiguous() def ulysses_anything_metadata(query: torch.Tensor, **kwargs) -> dict: # query: (B, S_LOCAL, H_GLOBAL, D) assert len(query.shape) == 4, "Query tensor must be 4-dimensional of shape (B, S_LOCAL, H_GLOBAL, D)" extra_kwargs = {} extra_kwargs["NUM_QO_HEAD"] = query.shape[2] extra_kwargs["Q_S_LOCAL"] = query.shape[1] # Add other kwargs if needed in future return extra_kwargs @maybe_allow_in_graph def all_to_all_single_any_qkv_async( x: torch.Tensor, group: dist.ProcessGroup, **kwargs ) -> Callable[..., torch.Tensor]: r""" x: torch.Tensor, shape (B, S_LOCAL, H, D) return: Callable that returns (B, S_GLOBAL, H_LOCAL, D) """ world_size = dist.get_world_size(group=group) B, S_LOCAL, H, D = x.shape x, H_PAD = _maybe_pad_qkv_head(x, H, group) H_LOCAL = (H + H_PAD) // world_size # (world_size, S_LOCAL, B, H_LOCAL, D) x = x.reshape(B, S_LOCAL, world_size, H_LOCAL, D).permute(2, 1, 0, 3, 4).contiguous() input_split_sizes = [S_LOCAL] * world_size # S_LOCAL maybe not equal for all ranks in dynamic shape case, # since we don't know the actual shape before this timing, thus, # we have to use all gather to collect the S_LOCAL first. output_split_sizes = gather_size_by_comm(S_LOCAL, group) x = x.flatten(0, 1) # (world_size * S_LOCAL, B, H_LOCAL, D) x = funcol.all_to_all_single(x, output_split_sizes, input_split_sizes, group) def wait() -> torch.Tensor: nonlocal x, H_PAD x = _wait_tensor(x) # (S_GLOBAL, B, H_LOCAL, D) # (S_GLOBAL, B, H_LOCAL, D) # -> (B, S_GLOBAL, H_LOCAL, D) x = x.permute(1, 0, 2, 3).contiguous() x = _maybe_unpad_qkv_head(x, H_PAD, group) return x return wait @maybe_allow_in_graph def all_to_all_single_any_o_async(x: torch.Tensor, group: dist.ProcessGroup, **kwargs) -> Callable[..., torch.Tensor]: r""" x: torch.Tensor, shape (B, S_GLOBAL, H_LOCAL, D) return: Callable that returns (B, S_LOCAL, H_GLOBAL, D) """ # Assume H is provided in kwargs, since we can't infer H from x's shape. # The padding logic needs H to determine if padding is necessary. H = kwargs.get("NUM_QO_HEAD", None) world_size = dist.get_world_size(group=group) x, H_PAD = _maybe_pad_o_head(x, H, group) shape = x.shape # (B, S_GLOBAL, H_LOCAL, D) (B, S_GLOBAL, H_LOCAL, D) = shape # input_split: e.g, S_GLOBAL=9 input splits across ranks [[5,4], [5,4],..] # output_split: e.g, S_GLOBAL=9 output splits across ranks [[5,5], [4,4],..] # WARN: In some cases, e.g, joint attn in Qwen-Image, the S_LOCAL can not infer # from tensor split due to: if c = torch.cat((a, b)), world_size=4, then, # c.tensor_split(4)[0].shape[1] may != to (a.tensor_split(4)[0].shape[1] + # b.tensor_split(4)[0].shape[1]) S_LOCAL = kwargs.get("Q_S_LOCAL") input_split_sizes = gather_size_by_comm(S_LOCAL, group) x = x.permute(1, 0, 2, 3).contiguous() # (S_GLOBAL, B, H_LOCAL, D) output_split_sizes = [S_LOCAL] * world_size x = funcol.all_to_all_single(x, output_split_sizes, input_split_sizes, group) def wait() -> torch.Tensor: nonlocal x, H_PAD x = _wait_tensor(x) # (S_GLOBAL, B, H_LOCAL, D) x = x.reshape(world_size, S_LOCAL, B, H_LOCAL, D) x = x.permute(2, 1, 0, 3, 4).contiguous() x = x.reshape(B, S_LOCAL, world_size * H_LOCAL, D) x = _maybe_unpad_o_head(x, H_PAD, group) return x return wait class TemplatedRingAttention(torch.autograd.Function): @staticmethod def forward( ctx: torch.autograd.function.FunctionCtx, query: torch.Tensor, key: torch.Tensor, value: torch.Tensor, attn_mask: torch.Tensor | None, dropout_p: float, is_causal: bool, scale: float | None, enable_gqa: bool, return_lse: bool, forward_op, backward_op, _parallel_config: "ParallelConfig" | None = None, ): ring_mesh = _parallel_config.context_parallel_config._ring_mesh rank = _parallel_config.context_parallel_config._ring_local_rank world_size = _parallel_config.context_parallel_config.ring_degree next_rank = (rank + 1) % world_size prev_out = prev_lse = None ctx.forward_op = forward_op ctx.backward_op = backward_op ctx.q_shape = query.shape ctx.kv_shape = key.shape ctx._parallel_config = _parallel_config kv_buffer = torch.cat([key.flatten(), value.flatten()]).contiguous() kv_buffer = funcol.all_gather_tensor(kv_buffer, gather_dim=0, group=ring_mesh.get_group()) kv_buffer = kv_buffer.chunk(world_size) for i in range(world_size): if i > 0: kv = kv_buffer[next_rank] key_numel = key.numel() key = kv[:key_numel].reshape_as(key) value = kv[key_numel:].reshape_as(value) next_rank = (next_rank + 1) % world_size out, lse = forward_op( ctx, query, key, value, attn_mask, dropout_p, is_causal, scale, enable_gqa, True, _save_ctx=i == 0, _parallel_config=_parallel_config, ) if _parallel_config.context_parallel_config.convert_to_fp32: out = out.to(torch.float32) lse = lse.to(torch.float32) # Refer to: # https://github.com/huggingface/diffusers/pull/12693#issuecomment-3627519544 if is_torch_version("<", "2.9.0"): lse = lse.unsqueeze(-1) if prev_out is not None: out = prev_out - torch.nn.functional.sigmoid(lse - prev_lse) * (prev_out - out) lse = prev_lse - torch.nn.functional.logsigmoid(prev_lse - lse) prev_out = out prev_lse = lse out = out.to(query.dtype) lse = lse.squeeze(-1) return (out, lse) if return_lse else out @staticmethod def backward( ctx: torch.autograd.function.FunctionCtx, grad_out: torch.Tensor, *args, ): ring_mesh = ctx._parallel_config.context_parallel_config._ring_mesh rank = ctx._parallel_config.context_parallel_config._ring_local_rank world_size = ctx._parallel_config.context_parallel_config.ring_degree next_rank = (rank + 1) % world_size next_ranks = list(range(1, world_size)) + [0] accum_dtype = torch.float32 if ctx._parallel_config.context_parallel_config.convert_to_fp32 else grad_out.dtype grad_query = torch.zeros(ctx.q_shape, dtype=accum_dtype, device=grad_out.device) grad_key = torch.zeros(ctx.kv_shape, dtype=accum_dtype, device=grad_out.device) grad_value = torch.zeros(ctx.kv_shape, dtype=accum_dtype, device=grad_out.device) next_grad_kv = None query, key, value, *_ = ctx.saved_tensors kv_buffer = torch.cat([key.flatten(), value.flatten()]).contiguous() kv_buffer = funcol.all_gather_tensor(kv_buffer, gather_dim=0, group=ring_mesh.get_group()) kv_buffer = kv_buffer.chunk(world_size) for i in range(world_size): if i > 0: kv = kv_buffer[next_rank] key_numel = key.numel() key = kv[:key_numel].reshape_as(key) value = kv[key_numel:].reshape_as(value) next_rank = (next_rank + 1) % world_size grad_query_op, grad_key_op, grad_value_op, *_ = ctx.backward_op(ctx, grad_out) if i > 0: grad_kv_buffer = _wait_tensor(next_grad_kv) grad_key_numel = grad_key.numel() grad_key = grad_kv_buffer[:grad_key_numel].reshape_as(grad_key) grad_value = grad_kv_buffer[grad_key_numel:].reshape_as(grad_value) grad_query += grad_query_op grad_key += grad_key_op grad_value += grad_value_op if i < world_size - 1: grad_kv_buffer = torch.cat([grad_key.flatten(), grad_value.flatten()]).contiguous() next_grad_kv = funcol.permute_tensor(grad_kv_buffer, next_ranks, group=ring_mesh.get_group()) grad_query, grad_key, grad_value = (x.to(grad_out.dtype) for x in (grad_query, grad_key, grad_value)) return grad_query, grad_key, grad_value, None, None, None, None, None, None, None, None, None class TemplatedUlyssesAttention(torch.autograd.Function): @staticmethod def forward( ctx: torch.autograd.function.FunctionCtx, query: torch.Tensor, key: torch.Tensor, value: torch.Tensor, attn_mask: torch.Tensor | None, dropout_p: float, is_causal: bool, scale: float | None, enable_gqa: bool, return_lse: bool, forward_op, backward_op, _parallel_config: "ParallelConfig" | None = None, ): ulysses_mesh = _parallel_config.context_parallel_config._ulysses_mesh world_size = _parallel_config.context_parallel_config.ulysses_degree group = ulysses_mesh.get_group() ctx.forward_op = forward_op ctx.backward_op = backward_op ctx._parallel_config = _parallel_config B, S_Q_LOCAL, H, D = query.shape _, S_KV_LOCAL, _, _ = key.shape H_LOCAL = H // world_size query = query.reshape(B, S_Q_LOCAL, world_size, H_LOCAL, D).permute(2, 1, 0, 3, 4).contiguous() key = key.reshape(B, S_KV_LOCAL, world_size, H_LOCAL, D).permute(2, 1, 0, 3, 4).contiguous() value = value.reshape(B, S_KV_LOCAL, world_size, H_LOCAL, D).permute(2, 1, 0, 3, 4).contiguous() query, key, value = (_all_to_all_single(x, group) for x in (query, key, value)) query, key, value = (x.flatten(0, 1).permute(1, 0, 2, 3).contiguous() for x in (query, key, value)) out = forward_op( ctx, query, key, value, attn_mask, dropout_p, is_causal, scale, enable_gqa, return_lse, _save_ctx=True, _parallel_config=_parallel_config, ) if return_lse: out, lse, *_ = out out = out.reshape(B, world_size, S_Q_LOCAL, H_LOCAL, D).permute(1, 3, 0, 2, 4).contiguous() out = _all_to_all_single(out, group) out = out.flatten(0, 1).permute(1, 2, 0, 3).contiguous() if return_lse: lse = lse.reshape(B, world_size, S_Q_LOCAL, H_LOCAL).permute(1, 3, 0, 2).contiguous() lse = _all_to_all_single(lse, group) lse = lse.flatten(0, 1).permute(1, 2, 0).contiguous() else: lse = None return (out, lse) if return_lse else out @staticmethod def backward( ctx: torch.autograd.function.FunctionCtx, grad_out: torch.Tensor, *args, ): ulysses_mesh = ctx._parallel_config.context_parallel_config._ulysses_mesh world_size = ctx._parallel_config.context_parallel_config.ulysses_degree group = ulysses_mesh.get_group() B, S_LOCAL, H, D = grad_out.shape H_LOCAL = H // world_size grad_out = grad_out.reshape(B, S_LOCAL, world_size, H_LOCAL, D).permute(2, 1, 0, 3, 4).contiguous() grad_out = _all_to_all_single(grad_out, group) grad_out = grad_out.flatten(0, 1).permute(1, 0, 2, 3).contiguous() grad_query_op, grad_key_op, grad_value_op, *_ = ctx.backward_op(ctx, grad_out) grad_query, grad_key, grad_value = ( x.reshape(B, world_size, S_LOCAL, H_LOCAL, D).permute(1, 3, 0, 2, 4).contiguous() for x in (grad_query_op, grad_key_op, grad_value_op) ) grad_query, grad_key, grad_value = (_all_to_all_single(x, group) for x in (grad_query, grad_key, grad_value)) grad_query, grad_key, grad_value = ( x.flatten(0, 1).permute(1, 2, 0, 3).contiguous() for x in (grad_query, grad_key, grad_value) ) return grad_query, grad_key, grad_value, None, None, None, None, None, None, None, None, None class TemplatedUlyssesAnythingAttention(torch.autograd.Function): @staticmethod def forward( ctx: torch.autograd.function.FunctionCtx, query: torch.Tensor, key: torch.Tensor, value: torch.Tensor, attn_mask: torch.Tensor, dropout_p: float, is_causal: bool, scale: float, enable_gqa: bool, return_lse: bool, forward_op, backward_op, _parallel_config: "ParallelConfig" | None = None, **kwargs, ): ulysses_mesh = _parallel_config.context_parallel_config._ulysses_mesh group = ulysses_mesh.get_group() ctx.forward_op = forward_op ctx.backward_op = backward_op ctx._parallel_config = _parallel_config metadata = ulysses_anything_metadata(query) query_wait = all_to_all_single_any_qkv_async(query, group, **metadata) key_wait = all_to_all_single_any_qkv_async(key, group, **metadata) value_wait = all_to_all_single_any_qkv_async(value, group, **metadata) query = query_wait() # type: torch.Tensor key = key_wait() # type: torch.Tensor value = value_wait() # type: torch.Tensor out = forward_op( ctx, query, key, value, attn_mask, dropout_p, is_causal, scale, enable_gqa, return_lse, _save_ctx=False, # ulysses anything only support forward pass now. _parallel_config=_parallel_config, ) if return_lse: out, lse, *_ = out # out: (B, S_Q_GLOBAL, H_LOCAL, D) -> (B, S_Q_LOCAL, H_GLOBAL, D) out_wait = all_to_all_single_any_o_async(out, group, **metadata) if return_lse: # lse: (B, S_Q_GLOBAL, H_LOCAL) lse = lse.unsqueeze(-1) # (B, S_Q_GLOBAL, H_LOCAL, D=1) lse_wait = all_to_all_single_any_o_async(lse, group, **metadata) out = out_wait() # type: torch.Tensor lse = lse_wait() # type: torch.Tensor lse = lse.squeeze(-1).contiguous() # (B, S_Q_LOCAL, H_GLOBAL) else: out = out_wait() # type: torch.Tensor lse = None return (out, lse) if return_lse else out @staticmethod def backward( ctx: torch.autograd.function.FunctionCtx, grad_out: torch.Tensor, *args, ): raise NotImplementedError("Backward pass for Ulysses Anything Attention in diffusers is not implemented yet.") def _templated_unified_attention( query: torch.Tensor, key: torch.Tensor, value: torch.Tensor, attn_mask: torch.Tensor, dropout_p: float, is_causal: bool, scale: float, enable_gqa: bool, return_lse: bool, forward_op, backward_op, _parallel_config: "ParallelConfig" | None = None, scatter_idx: int = 2, gather_idx: int = 1, ): """ Unified Sequence Parallelism attention combining Ulysses and ring attention. See: https://arxiv.org/abs/2405.07719 """ ulysses_mesh = _parallel_config.context_parallel_config._ulysses_mesh ulysses_group = ulysses_mesh.get_group() query = SeqAllToAllDim.apply(ulysses_group, query, scatter_idx, gather_idx) key = SeqAllToAllDim.apply(ulysses_group, key, scatter_idx, gather_idx) value = SeqAllToAllDim.apply(ulysses_group, value, scatter_idx, gather_idx) out = TemplatedRingAttention.apply( query, key, value, attn_mask, dropout_p, is_causal, scale, enable_gqa, return_lse, forward_op, backward_op, _parallel_config, ) if return_lse: context_layer, lse, *_ = out else: context_layer = out # context_layer is of shape (B, S, H_LOCAL, D) output = SeqAllToAllDim.apply( ulysses_group, context_layer, gather_idx, scatter_idx, ) if return_lse: # lse is of shape (B, S, H_LOCAL, 1) # Refer to: # https://github.com/huggingface/diffusers/pull/12693#issuecomment-3627519544 if is_torch_version("<", "2.9.0"): lse = lse.unsqueeze(-1) # (B, S, H_LOCAL, 1) lse = SeqAllToAllDim.apply(ulysses_group, lse, gather_idx, scatter_idx) lse = lse.squeeze(-1) return (output, lse) return output def _templated_context_parallel_attention( query: torch.Tensor, key: torch.Tensor, value: torch.Tensor, attn_mask: torch.Tensor | None = None, dropout_p: float = 0.0, is_causal: bool = False, scale: float | None = None, enable_gqa: bool = False, return_lse: bool = False, *, forward_op, backward_op, _parallel_config: "ParallelConfig" | None = None, ): if is_causal: raise ValueError("Causal attention is not yet supported for templated attention.") if enable_gqa: raise ValueError("GQA is not yet supported for templated attention.") # TODO: add support for unified attention with ring/ulysses degree both being > 1 if ( _parallel_config.context_parallel_config.ring_degree > 1 and _parallel_config.context_parallel_config.ulysses_degree > 1 ): return _templated_unified_attention( query, key, value, attn_mask, dropout_p, is_causal, scale, enable_gqa, return_lse, forward_op, backward_op, _parallel_config, ) elif _parallel_config.context_parallel_config.ring_degree > 1: return TemplatedRingAttention.apply( query, key, value, attn_mask, dropout_p, is_causal, scale, enable_gqa, return_lse, forward_op, backward_op, _parallel_config, ) elif _parallel_config.context_parallel_config.ulysses_degree > 1: if _parallel_config.context_parallel_config.ulysses_anything: # For Any sequence lengths and Any head num support return TemplatedUlyssesAnythingAttention.apply( query, key, value, attn_mask, dropout_p, is_causal, scale, enable_gqa, return_lse, forward_op, backward_op, _parallel_config, ) else: return TemplatedUlyssesAttention.apply( query, key, value, attn_mask, dropout_p, is_causal, scale, enable_gqa, return_lse, forward_op, backward_op, _parallel_config, ) else: raise ValueError("Reaching this branch of code is unexpected. Please report a bug.") # ===== Attention backends ===== @_AttentionBackendRegistry.register( AttentionBackendName.FLASH, constraints=[_check_device, _check_qkv_dtype_bf16_or_fp16, _check_shape], supports_context_parallel=True, ) def _flash_attention( query: torch.Tensor, key: torch.Tensor, value: torch.Tensor, attn_mask: torch.Tensor | None = None, dropout_p: float = 0.0, is_causal: bool = False, scale: float | None = None, return_lse: bool = False, _parallel_config: "ParallelConfig" | None = None, ) -> torch.Tensor: lse = None if attn_mask is not None: raise ValueError("`attn_mask` is not supported for flash-attn 2.") if _parallel_config is None: out = flash_attn_func( q=query, k=key, v=value, dropout_p=dropout_p, softmax_scale=scale, causal=is_causal, return_attn_probs=return_lse, ) if return_lse: out, lse, *_ = out else: out = _templated_context_parallel_attention( query, key, value, None, dropout_p, is_causal, scale, False, return_lse, forward_op=_flash_attention_forward_op, backward_op=_flash_attention_backward_op, _parallel_config=_parallel_config, ) if return_lse: out, lse = out return (out, lse) if return_lse else out @_AttentionBackendRegistry.register( AttentionBackendName.FLASH_HUB, constraints=[_check_device, _check_qkv_dtype_bf16_or_fp16, _check_shape], supports_context_parallel=True, ) def _flash_attention_hub( query: torch.Tensor, key: torch.Tensor, value: torch.Tensor, attn_mask: torch.Tensor | None = None, dropout_p: float = 0.0, is_causal: bool = False, scale: float | None = None, return_lse: bool = False, _parallel_config: "ParallelConfig" | None = None, ) -> torch.Tensor: lse = None if attn_mask is not None: raise ValueError("`attn_mask` is not supported for flash-attn 2.") func = _HUB_KERNELS_REGISTRY[AttentionBackendName.FLASH_HUB].kernel_fn if _parallel_config is None: out = func( q=query, k=key, v=value, dropout_p=dropout_p, softmax_scale=scale, causal=is_causal, return_attn_probs=return_lse, ) if return_lse: out, lse, *_ = out else: out = _templated_context_parallel_attention( query, key, value, None, dropout_p, is_causal, scale, False, return_lse, forward_op=_flash_attention_hub_forward_op, backward_op=_flash_attention_hub_backward_op, _parallel_config=_parallel_config, ) if return_lse: out, lse = out return (out, lse) if return_lse else out @_AttentionBackendRegistry.register( AttentionBackendName.FLASH_VARLEN_HUB, constraints=[_check_device, _check_qkv_dtype_bf16_or_fp16, _check_shape], supports_context_parallel=False, ) def _flash_varlen_attention_hub( query: torch.Tensor, key: torch.Tensor, value: torch.Tensor, attn_mask: torch.Tensor | None = None, dropout_p: float = 0.0, scale: float | None = None, is_causal: bool = False, return_lse: bool = False, _parallel_config: "ParallelConfig" | None = None, ) -> torch.Tensor: batch_size, seq_len_q, _, _ = query.shape _, seq_len_kv, _, _ = key.shape if attn_mask is not None: attn_mask = _normalize_attn_mask(attn_mask, batch_size, seq_len_kv) (_, seqlens_k), (cu_seqlens_q, cu_seqlens_k), (max_seqlen_q, max_seqlen_k) = ( _prepare_for_flash_attn_or_sage_varlen( batch_size, seq_len_q, seq_len_kv, attn_mask=attn_mask, device=query.device ) ) key_valid, value_valid = [], [] for b in range(batch_size): valid_len = seqlens_k[b] key_valid.append(key[b, :valid_len]) value_valid.append(value[b, :valid_len]) query_packed = query.flatten(0, 1) key_packed = torch.cat(key_valid, dim=0) value_packed = torch.cat(value_valid, dim=0) func = _HUB_KERNELS_REGISTRY[AttentionBackendName.FLASH_VARLEN_HUB].kernel_fn out = func( q=query_packed, k=key_packed, v=value_packed, cu_seqlens_q=cu_seqlens_q, cu_seqlens_k=cu_seqlens_k, max_seqlen_q=max_seqlen_q, max_seqlen_k=max_seqlen_k, dropout_p=dropout_p, softmax_scale=scale, causal=is_causal, return_attn_probs=return_lse, ) out = out.unflatten(0, (batch_size, -1)) return out @_AttentionBackendRegistry.register( AttentionBackendName.FLASH_VARLEN, constraints=[_check_device, _check_qkv_dtype_bf16_or_fp16, _check_shape], ) def _flash_varlen_attention( query: torch.Tensor, key: torch.Tensor, value: torch.Tensor, attn_mask: torch.Tensor | None = None, dropout_p: float = 0.0, scale: float | None = None, is_causal: bool = False, return_lse: bool = False, _parallel_config: "ParallelConfig" | None = None, ) -> torch.Tensor: batch_size, seq_len_q, _, _ = query.shape _, seq_len_kv, _, _ = key.shape if attn_mask is not None: attn_mask = _normalize_attn_mask(attn_mask, batch_size, seq_len_kv) (_, seqlens_k), (cu_seqlens_q, cu_seqlens_k), (max_seqlen_q, max_seqlen_k) = ( _prepare_for_flash_attn_or_sage_varlen( batch_size, seq_len_q, seq_len_kv, attn_mask=attn_mask, device=query.device ) ) key_valid, value_valid = [], [] for b in range(batch_size): valid_len = seqlens_k[b] key_valid.append(key[b, :valid_len]) value_valid.append(value[b, :valid_len]) query_packed = query.flatten(0, 1) key_packed = torch.cat(key_valid, dim=0) value_packed = torch.cat(value_valid, dim=0) out = flash_attn_varlen_func( q=query_packed, k=key_packed, v=value_packed, cu_seqlens_q=cu_seqlens_q, cu_seqlens_k=cu_seqlens_k, max_seqlen_q=max_seqlen_q, max_seqlen_k=max_seqlen_k, dropout_p=dropout_p, softmax_scale=scale, causal=is_causal, return_attn_probs=return_lse, ) out = out.unflatten(0, (batch_size, -1)) return out @_AttentionBackendRegistry.register( AttentionBackendName._FLASH_3, constraints=[_check_device, _check_qkv_dtype_bf16_or_fp16, _check_shape], ) def _flash_attention_3( query: torch.Tensor, key: torch.Tensor, value: torch.Tensor, attn_mask: torch.Tensor | None = None, scale: float | None = None, is_causal: bool = False, return_lse: bool = False, _parallel_config: "ParallelConfig" | None = None, ) -> torch.Tensor: if attn_mask is not None: raise ValueError("`attn_mask` is not supported for flash-attn 3.") out, lse = _wrapped_flash_attn_3( q=query, k=key, v=value, softmax_scale=scale, causal=is_causal, ) return (out, lse) if return_lse else out @_AttentionBackendRegistry.register( AttentionBackendName._FLASH_3_HUB, constraints=[_check_device, _check_qkv_dtype_bf16_or_fp16, _check_shape], supports_context_parallel=True, ) def _flash_attention_3_hub( query: torch.Tensor, key: torch.Tensor, value: torch.Tensor, attn_mask: torch.Tensor | None = None, scale: float | None = None, is_causal: bool = False, window_size: tuple[int, int] = (-1, -1), softcap: float = 0.0, deterministic: bool = False, return_attn_probs: bool = False, _parallel_config: "ParallelConfig" | None = None, ) -> torch.Tensor: if attn_mask is not None: raise ValueError("`attn_mask` is not supported for flash-attn 3.") func = _HUB_KERNELS_REGISTRY[AttentionBackendName._FLASH_3_HUB].kernel_fn if _parallel_config is None: out = func( q=query, k=key, v=value, softmax_scale=scale, causal=is_causal, qv=None, q_descale=None, k_descale=None, v_descale=None, window_size=window_size, softcap=softcap, num_splits=1, pack_gqa=None, deterministic=deterministic, sm_margin=0, return_attn_probs=return_attn_probs, ) return (out[0], out[1]) if return_attn_probs else out forward_op = functools.partial( _flash_attention_3_hub_forward_op, window_size=window_size, softcap=softcap, num_splits=1, pack_gqa=None, deterministic=deterministic, sm_margin=0, ) backward_op = functools.partial( _flash_attention_3_hub_backward_op, window_size=window_size, softcap=softcap, num_splits=1, pack_gqa=None, deterministic=deterministic, sm_margin=0, ) out = _templated_context_parallel_attention( query, key, value, None, 0.0, is_causal, scale, False, return_attn_probs, forward_op=forward_op, backward_op=backward_op, _parallel_config=_parallel_config, ) if return_attn_probs: out, lse = out return out, lse return out @_AttentionBackendRegistry.register( AttentionBackendName._FLASH_3_VARLEN_HUB, constraints=[_check_device, _check_qkv_dtype_bf16_or_fp16, _check_shape], supports_context_parallel=False, ) def _flash_attention_3_varlen_hub( query: torch.Tensor, key: torch.Tensor, value: torch.Tensor, attn_mask: torch.Tensor | None = None, scale: float | None = None, is_causal: bool = False, return_lse: bool = False, _parallel_config: "ParallelConfig" | None = None, ) -> torch.Tensor: batch_size, seq_len_q, _, _ = query.shape _, seq_len_kv, _, _ = key.shape if attn_mask is not None: attn_mask = _normalize_attn_mask(attn_mask, batch_size, seq_len_kv) (_, seqlens_k), (cu_seqlens_q, cu_seqlens_k), (max_seqlen_q, max_seqlen_k) = ( _prepare_for_flash_attn_or_sage_varlen( batch_size, seq_len_q, seq_len_kv, attn_mask=attn_mask, device=query.device ) ) key_valid, value_valid = [], [] for b in range(batch_size): valid_len = seqlens_k[b] key_valid.append(key[b, :valid_len]) value_valid.append(value[b, :valid_len]) query_packed = query.flatten(0, 1) key_packed = torch.cat(key_valid, dim=0) value_packed = torch.cat(value_valid, dim=0) func = _HUB_KERNELS_REGISTRY[AttentionBackendName._FLASH_3_VARLEN_HUB].kernel_fn out, lse, *_ = func( q=query_packed, k=key_packed, v=value_packed, cu_seqlens_q=cu_seqlens_q, cu_seqlens_k=cu_seqlens_k, max_seqlen_q=max_seqlen_q, max_seqlen_k=max_seqlen_k, softmax_scale=scale, causal=is_causal, ) out = out.unflatten(0, (batch_size, -1)) return (out, lse) if return_lse else out @_AttentionBackendRegistry.register( AttentionBackendName._FLASH_VARLEN_3, constraints=[_check_device, _check_qkv_dtype_bf16_or_fp16, _check_shape], ) def _flash_varlen_attention_3( query: torch.Tensor, key: torch.Tensor, value: torch.Tensor, attn_mask: torch.Tensor | None = None, scale: float | None = None, is_causal: bool = False, return_lse: bool = False, _parallel_config: "ParallelConfig" | None = None, ) -> torch.Tensor: batch_size, seq_len_q, _, _ = query.shape _, seq_len_kv, _, _ = key.shape if attn_mask is not None: attn_mask = _normalize_attn_mask(attn_mask, batch_size, seq_len_kv) (_, seqlens_k), (cu_seqlens_q, cu_seqlens_k), (max_seqlen_q, max_seqlen_k) = ( _prepare_for_flash_attn_or_sage_varlen( batch_size, seq_len_q, seq_len_kv, attn_mask=attn_mask, device=query.device ) ) key_valid, value_valid = [], [] for b in range(batch_size): valid_len = seqlens_k[b] key_valid.append(key[b, :valid_len]) value_valid.append(value[b, :valid_len]) query_packed = query.flatten(0, 1) key_packed = torch.cat(key_valid, dim=0) value_packed = torch.cat(value_valid, dim=0) result = flash_attn_3_varlen_func( q=query_packed, k=key_packed, v=value_packed, cu_seqlens_q=cu_seqlens_q, cu_seqlens_k=cu_seqlens_k, max_seqlen_q=max_seqlen_q, max_seqlen_k=max_seqlen_k, softmax_scale=scale, causal=is_causal, return_attn_probs=return_lse, ) if isinstance(result, tuple): out, lse, *_ = result else: out = result lse = None out = out.unflatten(0, (batch_size, -1)) return (out, lse) if return_lse else out @_AttentionBackendRegistry.register( AttentionBackendName.AITER, constraints=[_check_device_cuda, _check_qkv_dtype_bf16_or_fp16, _check_shape], ) def _aiter_flash_attention( query: torch.Tensor, key: torch.Tensor, value: torch.Tensor, attn_mask: torch.Tensor | None = None, dropout_p: float = 0.0, is_causal: bool = False, scale: float | None = None, return_lse: bool = False, _parallel_config: "ParallelConfig" | None = None, ) -> torch.Tensor: if attn_mask is not None: raise ValueError("`attn_mask` is not supported for aiter attention") if not return_lse and torch.is_grad_enabled(): # aiter requires return_lse=True by assertion when gradients are enabled. out, lse, *_ = aiter_flash_attn_func( q=query, k=key, v=value, dropout_p=dropout_p, softmax_scale=scale, causal=is_causal, return_lse=True, ) else: out = aiter_flash_attn_func( q=query, k=key, v=value, dropout_p=dropout_p, softmax_scale=scale, causal=is_causal, return_lse=return_lse, ) if return_lse: out, lse, *_ = out return (out, lse) if return_lse else out @_AttentionBackendRegistry.register( AttentionBackendName.FLEX, constraints=[_check_attn_mask_or_causal, _check_device, _check_shape], ) def _native_flex_attention( query: torch.Tensor, key: torch.Tensor, value: torch.Tensor, attn_mask: torch.Tensor | "flex_attention.BlockMask" | None = None, is_causal: bool = False, scale: float | None = None, enable_gqa: bool = False, return_lse: bool = False, _parallel_config: "ParallelConfig" | None = None, ) -> torch.Tensor: # TODO: should we LRU cache the block mask creation? score_mod = None block_mask = None batch_size, seq_len_q, num_heads, _ = query.shape _, seq_len_kv, _, _ = key.shape if attn_mask is None or isinstance(attn_mask, flex_attention.BlockMask): block_mask = attn_mask elif is_causal: block_mask = flex_attention.create_block_mask( _flex_attention_causal_mask_mod, batch_size, num_heads, seq_len_q, seq_len_kv, query.device ) elif torch.is_tensor(attn_mask): if attn_mask.ndim == 2: attn_mask = attn_mask.view(attn_mask.size(0), 1, attn_mask.size(1), 1) attn_mask = attn_mask.expand(batch_size, num_heads, seq_len_q, seq_len_kv) if attn_mask.dtype == torch.bool: # TODO: this probably does not work but verify! def mask_mod(batch_idx, head_idx, q_idx, kv_idx): return attn_mask[batch_idx, head_idx, q_idx, kv_idx] block_mask = flex_attention.create_block_mask( mask_mod, batch_size, None, seq_len_q, seq_len_kv, query.device ) else: def score_mod(score, batch_idx, head_idx, q_idx, kv_idx): return score + attn_mask[batch_idx, head_idx, q_idx, kv_idx] else: raise ValueError("Attention mask must be either None, a BlockMask, or a 2D/4D tensor.") query, key, value = (x.permute(0, 2, 1, 3) for x in (query, key, value)) out = flex_attention.flex_attention( query=query, key=key, value=value, score_mod=score_mod, block_mask=block_mask, scale=scale, enable_gqa=enable_gqa, return_lse=return_lse, ) out = out.permute(0, 2, 1, 3) return out def _prepare_additive_attn_mask( attn_mask: torch.Tensor, target_dtype: torch.dtype, reshape_4d: bool = True ) -> torch.Tensor: """ Convert a 2D attention mask to an additive mask, optionally reshaping to 4D for SDPA. This helper is used by both native SDPA and xformers backends to handle both boolean and additive masks. Args: attn_mask: 2D tensor [batch_size, seq_len_k] - Boolean: True means attend, False means mask out - Additive: 0.0 means attend, -inf means mask out target_dtype: The dtype to convert the mask to (usually query.dtype) reshape_4d: If True, reshape from [batch_size, seq_len_k] to [batch_size, 1, 1, seq_len_k] for broadcasting Returns: Additive mask tensor where 0.0 means attend and -inf means mask out. Shape is [batch_size, seq_len_k] if reshape_4d=False, or [batch_size, 1, 1, seq_len_k] if reshape_4d=True. """ # Check if the mask is boolean or already additive if attn_mask.dtype == torch.bool: # Convert boolean to additive: True -> 0.0, False -> -inf attn_mask = torch.where(attn_mask, 0.0, float("-inf")) # Convert to target dtype attn_mask = attn_mask.to(dtype=target_dtype) else: # Already additive mask - just ensure correct dtype attn_mask = attn_mask.to(dtype=target_dtype) # Optionally reshape to 4D for broadcasting in attention mechanisms if reshape_4d: batch_size, seq_len_k = attn_mask.shape attn_mask = attn_mask.view(batch_size, 1, 1, seq_len_k) return attn_mask @_AttentionBackendRegistry.register( AttentionBackendName.NATIVE, constraints=[_check_device, _check_shape], supports_context_parallel=True, ) def _native_attention( query: torch.Tensor, key: torch.Tensor, value: torch.Tensor, attn_mask: torch.Tensor | None = None, dropout_p: float = 0.0, is_causal: bool = False, scale: float | None = None, enable_gqa: bool = False, return_lse: bool = False, _parallel_config: "ParallelConfig" | None = None, ) -> torch.Tensor: if return_lse: raise ValueError("Native attention backend does not support setting `return_lse=True`.") # Reshape 2D mask to 4D for SDPA # SDPA accepts both boolean masks (torch.bool) and additive masks (float) if ( attn_mask is not None and attn_mask.ndim == 2 and attn_mask.shape[0] == query.shape[0] and attn_mask.shape[1] == key.shape[1] ): # Just reshape [batch_size, seq_len_k] -> [batch_size, 1, 1, seq_len_k] # SDPA handles both boolean and additive masks correctly attn_mask = attn_mask.unsqueeze(1).unsqueeze(1) if _parallel_config is None: query, key, value = (x.permute(0, 2, 1, 3) for x in (query, key, value)) out = torch.nn.functional.scaled_dot_product_attention( query=query, key=key, value=value, attn_mask=attn_mask, dropout_p=dropout_p, is_causal=is_causal, scale=scale, enable_gqa=enable_gqa, ) out = out.permute(0, 2, 1, 3) else: out = _templated_context_parallel_attention( query, key, value, attn_mask, dropout_p, is_causal, scale, enable_gqa, return_lse, forward_op=_native_attention_forward_op, backward_op=_native_attention_backward_op, _parallel_config=_parallel_config, ) return out @_AttentionBackendRegistry.register( AttentionBackendName._NATIVE_CUDNN, constraints=[_check_device, _check_qkv_dtype_bf16_or_fp16, _check_shape], supports_context_parallel=True, ) def _native_cudnn_attention( query: torch.Tensor, key: torch.Tensor, value: torch.Tensor, attn_mask: torch.Tensor | None = None, dropout_p: float = 0.0, is_causal: bool = False, scale: float | None = None, enable_gqa: bool = False, return_lse: bool = False, _parallel_config: "ParallelConfig" | None = None, ) -> torch.Tensor: lse = None if _parallel_config is None and not return_lse: query, key, value = (x.permute(0, 2, 1, 3).contiguous() for x in (query, key, value)) with torch.nn.attention.sdpa_kernel(torch.nn.attention.SDPBackend.CUDNN_ATTENTION): out = torch.nn.functional.scaled_dot_product_attention( query=query, key=key, value=value, attn_mask=attn_mask, dropout_p=dropout_p, is_causal=is_causal, scale=scale, enable_gqa=enable_gqa, ) out = out.permute(0, 2, 1, 3) else: out = _templated_context_parallel_attention( query, key, value, attn_mask, dropout_p, is_causal, scale, enable_gqa, return_lse, forward_op=_cudnn_attention_forward_op, backward_op=_cudnn_attention_backward_op, _parallel_config=_parallel_config, ) if return_lse: out, lse = out return (out, lse) if return_lse else out @_AttentionBackendRegistry.register( AttentionBackendName._NATIVE_EFFICIENT, constraints=[_check_device, _check_shape], ) def _native_efficient_attention( query: torch.Tensor, key: torch.Tensor, value: torch.Tensor, attn_mask: torch.Tensor | None = None, dropout_p: float = 0.0, is_causal: bool = False, scale: float | None = None, enable_gqa: bool = False, return_lse: bool = False, _parallel_config: "ParallelConfig" | None = None, ) -> torch.Tensor: if return_lse: raise ValueError("Native efficient attention backend does not support setting `return_lse=True`.") query, key, value = (x.permute(0, 2, 1, 3) for x in (query, key, value)) with torch.nn.attention.sdpa_kernel(torch.nn.attention.SDPBackend.EFFICIENT_ATTENTION): out = torch.nn.functional.scaled_dot_product_attention( query=query, key=key, value=value, attn_mask=attn_mask, dropout_p=dropout_p, is_causal=is_causal, scale=scale, enable_gqa=enable_gqa, ) out = out.permute(0, 2, 1, 3) return out @_AttentionBackendRegistry.register( AttentionBackendName._NATIVE_FLASH, constraints=[_check_device, _check_qkv_dtype_bf16_or_fp16, _check_shape], supports_context_parallel=True, ) def _native_flash_attention( query: torch.Tensor, key: torch.Tensor, value: torch.Tensor, attn_mask: torch.Tensor | None = None, dropout_p: float = 0.0, is_causal: bool = False, scale: float | None = None, enable_gqa: bool = False, return_lse: bool = False, _parallel_config: "ParallelConfig" | None = None, ) -> torch.Tensor: if attn_mask is not None: raise ValueError("`attn_mask` is not supported for aiter attention") lse = None if _parallel_config is None and not return_lse: query, key, value = (x.permute(0, 2, 1, 3) for x in (query, key, value)) with torch.nn.attention.sdpa_kernel(torch.nn.attention.SDPBackend.FLASH_ATTENTION): out = torch.nn.functional.scaled_dot_product_attention( query=query, key=key, value=value, attn_mask=None, # not supported dropout_p=dropout_p, is_causal=is_causal, scale=scale, enable_gqa=enable_gqa, ) out = out.permute(0, 2, 1, 3) else: out = _templated_context_parallel_attention( query, key, value, None, dropout_p, is_causal, scale, enable_gqa, return_lse, forward_op=_native_flash_attention_forward_op, backward_op=_native_flash_attention_backward_op, _parallel_config=_parallel_config, ) if return_lse: out, lse = out return (out, lse) if return_lse else out @_AttentionBackendRegistry.register( AttentionBackendName._NATIVE_MATH, constraints=[_check_device, _check_shape], ) def _native_math_attention( query: torch.Tensor, key: torch.Tensor, value: torch.Tensor, attn_mask: torch.Tensor | None = None, dropout_p: float = 0.0, is_causal: bool = False, scale: float | None = None, enable_gqa: bool = False, return_lse: bool = False, _parallel_config: "ParallelConfig" | None = None, ) -> torch.Tensor: if return_lse: raise ValueError("Native math attention backend does not support setting `return_lse=True`.") query, key, value = (x.permute(0, 2, 1, 3) for x in (query, key, value)) with torch.nn.attention.sdpa_kernel(torch.nn.attention.SDPBackend.MATH): out = torch.nn.functional.scaled_dot_product_attention( query=query, key=key, value=value, attn_mask=attn_mask, dropout_p=dropout_p, is_causal=is_causal, scale=scale, enable_gqa=enable_gqa, ) out = out.permute(0, 2, 1, 3) return out @_AttentionBackendRegistry.register( AttentionBackendName._NATIVE_NPU, constraints=[_check_device, _check_qkv_dtype_bf16_or_fp16, _check_shape], supports_context_parallel=True, ) def _native_npu_attention( query: torch.Tensor, key: torch.Tensor, value: torch.Tensor, attn_mask: torch.Tensor | None = None, dropout_p: float = 0.0, scale: float | None = None, return_lse: bool = False, _parallel_config: "ParallelConfig" | None = None, ) -> torch.Tensor: if return_lse: raise ValueError("NPU attention backend does not support setting `return_lse=True`.") if _parallel_config is None: attn_mask = _maybe_modify_attn_mask_npu(query, key, attn_mask) out = npu_fusion_attention( query, key, value, query.size(2), # num_heads atten_mask=attn_mask, input_layout="BSND", pse=None, scale=1.0 / math.sqrt(query.shape[-1]) if scale is None else scale, pre_tockens=65536, next_tockens=65536, keep_prob=1.0 - dropout_p, sync=False, inner_precise=0, )[0] else: out = _templated_context_parallel_attention( query, key, value, attn_mask, dropout_p, None, scale, None, return_lse, forward_op=_npu_attention_forward_op, backward_op=_npu_attention_backward_op, _parallel_config=_parallel_config, ) return out # Reference: https://github.com/pytorch/xla/blob/06c5533de6588f6b90aa1655d9850bcf733b90b4/torch_xla/experimental/custom_kernel.py#L853 @_AttentionBackendRegistry.register( AttentionBackendName._NATIVE_XLA, constraints=[_check_device, _check_shape], ) def _native_xla_attention( query: torch.Tensor, key: torch.Tensor, value: torch.Tensor, attn_mask: torch.Tensor | None = None, is_causal: bool = False, return_lse: bool = False, _parallel_config: "ParallelConfig" | None = None, ) -> torch.Tensor: if attn_mask is not None: raise ValueError("`attn_mask` is not supported for XLA attention") if return_lse: raise ValueError("XLA attention backend does not support setting `return_lse=True`.") query, key, value = (x.permute(0, 2, 1, 3) for x in (query, key, value)) query = query / math.sqrt(query.shape[-1]) out = xla_flash_attention( q=query, k=key, v=value, causal=is_causal, ) out = out.permute(0, 2, 1, 3) return out @_AttentionBackendRegistry.register( AttentionBackendName.SAGE, constraints=[_check_device_cuda, _check_qkv_dtype_bf16_or_fp16, _check_shape], supports_context_parallel=True, ) def _sage_attention( query: torch.Tensor, key: torch.Tensor, value: torch.Tensor, attn_mask: torch.Tensor | None = None, is_causal: bool = False, scale: float | None = None, return_lse: bool = False, _parallel_config: "ParallelConfig" | None = None, ) -> torch.Tensor: if attn_mask is not None: raise ValueError("`attn_mask` is not supported for sage attention") lse = None if _parallel_config is None: out = sageattn( q=query, k=key, v=value, tensor_layout="NHD", is_causal=is_causal, sm_scale=scale, return_lse=return_lse, ) if return_lse: out, lse, *_ = out else: out = _templated_context_parallel_attention( query, key, value, None, 0.0, is_causal, scale, False, return_lse, forward_op=_sage_attention_forward_op, backward_op=_sage_attention_backward_op, _parallel_config=_parallel_config, ) if return_lse: out, lse = out return (out, lse) if return_lse else out @_AttentionBackendRegistry.register( AttentionBackendName.SAGE_HUB, constraints=[_check_device_cuda, _check_qkv_dtype_bf16_or_fp16, _check_shape], supports_context_parallel=True, ) def _sage_attention_hub( query: torch.Tensor, key: torch.Tensor, value: torch.Tensor, attn_mask: torch.Tensor | None = None, is_causal: bool = False, scale: float | None = None, return_lse: bool = False, _parallel_config: "ParallelConfig" | None = None, ) -> torch.Tensor: if attn_mask is not None: raise ValueError("`attn_mask` is not supported for sage attention") lse = None func = _HUB_KERNELS_REGISTRY[AttentionBackendName.SAGE_HUB].kernel_fn if _parallel_config is None: out = func( q=query, k=key, v=value, tensor_layout="NHD", is_causal=is_causal, sm_scale=scale, return_lse=return_lse, ) if return_lse: out, lse, *_ = out else: out = _templated_context_parallel_attention( query, key, value, None, 0.0, is_causal, scale, False, return_lse, forward_op=_sage_attention_hub_forward_op, backward_op=_sage_attention_backward_op, _parallel_config=_parallel_config, ) if return_lse: out, lse = out return (out, lse) if return_lse else out @_AttentionBackendRegistry.register( AttentionBackendName.SAGE_VARLEN, constraints=[_check_device_cuda, _check_qkv_dtype_bf16_or_fp16, _check_shape], ) def _sage_varlen_attention( query: torch.Tensor, key: torch.Tensor, value: torch.Tensor, attn_mask: torch.Tensor | None = None, is_causal: bool = False, scale: float | None = None, return_lse: bool = False, _parallel_config: "ParallelConfig" | None = None, ) -> torch.Tensor: if return_lse: raise ValueError("Sage varlen backend does not support setting `return_lse=True`.") batch_size, seq_len_q, _, _ = query.shape _, seq_len_kv, _, _ = key.shape if attn_mask is not None: attn_mask = _normalize_attn_mask(attn_mask, batch_size, seq_len_kv) (_, seqlens_k), (cu_seqlens_q, cu_seqlens_k), (max_seqlen_q, max_seqlen_k) = ( _prepare_for_flash_attn_or_sage_varlen( batch_size, seq_len_q, seq_len_kv, attn_mask=attn_mask, device=query.device ) ) key_valid, value_valid = [], [] for b in range(batch_size): valid_len = seqlens_k[b] key_valid.append(key[b, :valid_len]) value_valid.append(value[b, :valid_len]) query_packed = query.flatten(0, 1) key_packed = torch.cat(key_valid, dim=0) value_packed = torch.cat(value_valid, dim=0) out = sageattn_varlen( q=query_packed, k=key_packed, v=value_packed, cu_seqlens_q=cu_seqlens_q, cu_seqlens_k=cu_seqlens_k, max_seqlen_q=max_seqlen_q, max_seqlen_k=max_seqlen_k, is_causal=is_causal, sm_scale=scale, ) out = out.unflatten(0, (batch_size, -1)) return out @_AttentionBackendRegistry.register( AttentionBackendName._SAGE_QK_INT8_PV_FP8_CUDA, constraints=[_check_device_cuda_atleast_smXY(9, 0), _check_shape], ) def _sage_qk_int8_pv_fp8_cuda_attention( query: torch.Tensor, key: torch.Tensor, value: torch.Tensor, attn_mask: torch.Tensor | None = None, is_causal: bool = False, scale: float | None = None, return_lse: bool = False, _parallel_config: "ParallelConfig" | None = None, ) -> torch.Tensor: if attn_mask is not None: raise ValueError("`attn_mask` is not supported for sage attention") return sageattn_qk_int8_pv_fp8_cuda( q=query, k=key, v=value, tensor_layout="NHD", is_causal=is_causal, sm_scale=scale, return_lse=return_lse, ) @_AttentionBackendRegistry.register( AttentionBackendName._SAGE_QK_INT8_PV_FP8_CUDA_SM90, constraints=[_check_device_cuda_atleast_smXY(9, 0), _check_shape], ) def _sage_qk_int8_pv_fp8_cuda_sm90_attention( query: torch.Tensor, key: torch.Tensor, value: torch.Tensor, attn_mask: torch.Tensor | None = None, is_causal: bool = False, scale: float | None = None, return_lse: bool = False, _parallel_config: "ParallelConfig" | None = None, ) -> torch.Tensor: if attn_mask is not None: raise ValueError("`attn_mask` is not supported for sage attention") return sageattn_qk_int8_pv_fp8_cuda_sm90( q=query, k=key, v=value, tensor_layout="NHD", is_causal=is_causal, sm_scale=scale, return_lse=return_lse, ) @_AttentionBackendRegistry.register( AttentionBackendName._SAGE_QK_INT8_PV_FP16_CUDA, constraints=[_check_device_cuda_atleast_smXY(8, 0), _check_shape], ) def _sage_qk_int8_pv_fp16_cuda_attention( query: torch.Tensor, key: torch.Tensor, value: torch.Tensor, attn_mask: torch.Tensor | None = None, is_causal: bool = False, scale: float | None = None, return_lse: bool = False, _parallel_config: "ParallelConfig" | None = None, ) -> torch.Tensor: if attn_mask is not None: raise ValueError("`attn_mask` is not supported for sage attention") return sageattn_qk_int8_pv_fp16_cuda( q=query, k=key, v=value, tensor_layout="NHD", is_causal=is_causal, sm_scale=scale, return_lse=return_lse, ) @_AttentionBackendRegistry.register( AttentionBackendName._SAGE_QK_INT8_PV_FP16_TRITON, constraints=[_check_device_cuda_atleast_smXY(8, 0), _check_shape], ) def _sage_qk_int8_pv_fp16_triton_attention( query: torch.Tensor, key: torch.Tensor, value: torch.Tensor, attn_mask: torch.Tensor | None = None, is_causal: bool = False, scale: float | None = None, return_lse: bool = False, _parallel_config: "ParallelConfig" | None = None, ) -> torch.Tensor: if attn_mask is not None: raise ValueError("`attn_mask` is not supported for sage attention") return sageattn_qk_int8_pv_fp16_triton( q=query, k=key, v=value, tensor_layout="NHD", is_causal=is_causal, sm_scale=scale, return_lse=return_lse, ) @_AttentionBackendRegistry.register( AttentionBackendName.XFORMERS, constraints=[_check_attn_mask_or_causal, _check_device, _check_shape], ) def _xformers_attention( query: torch.Tensor, key: torch.Tensor, value: torch.Tensor, attn_mask: torch.Tensor | None = None, dropout_p: float = 0.0, is_causal: bool = False, scale: float | None = None, enable_gqa: bool = False, return_lse: bool = False, _parallel_config: "ParallelConfig" | None = None, ) -> torch.Tensor: if return_lse: raise ValueError("xformers attention backend does not support setting `return_lse=True`.") batch_size, seq_len_q, num_heads_q, _ = query.shape _, seq_len_kv, num_heads_kv, _ = key.shape if is_causal: attn_mask = xops.LowerTriangularMask() elif attn_mask is not None: if attn_mask.ndim == 2: # Convert 2D mask to 4D for xformers # Mask can be boolean (True=attend, False=mask) or additive (0.0=attend, -inf=mask) # xformers requires 4D additive masks [batch, heads, seq_q, seq_k] # Need memory alignment - create larger tensor and slice for alignment original_seq_len = attn_mask.size(1) aligned_seq_len = ((original_seq_len + 7) // 8) * 8 # Round up to multiple of 8 # Create aligned 4D tensor and slice to ensure proper memory layout aligned_mask = torch.zeros( (batch_size, num_heads_q, seq_len_q, aligned_seq_len), dtype=query.dtype, device=query.device, ) # Convert to 4D additive mask (handles both boolean and additive inputs) mask_additive = _prepare_additive_attn_mask( attn_mask, target_dtype=query.dtype ) # [batch, 1, 1, seq_len_k] # Broadcast to [batch, heads, seq_q, seq_len_k] aligned_mask[:, :, :, :original_seq_len] = mask_additive # Mask out the padding (already -inf from zeros -> where with default) aligned_mask[:, :, :, original_seq_len:] = float("-inf") # Slice to actual size with proper alignment attn_mask = aligned_mask[:, :, :, :seq_len_kv] elif attn_mask.ndim != 4: raise ValueError("Only 2D and 4D attention masks are supported for xformers attention.") elif attn_mask.ndim == 4: attn_mask = attn_mask.expand(batch_size, num_heads_q, seq_len_q, seq_len_kv).type_as(query) if enable_gqa: if num_heads_q % num_heads_kv != 0: raise ValueError("Number of heads in query must be divisible by number of heads in key/value.") num_heads_per_group = num_heads_q // num_heads_kv query = query.unflatten(2, (num_heads_kv, -1)) key = key.unflatten(2, (num_heads_kv, -1)).expand(-1, -1, -1, num_heads_per_group, -1) value = value.unflatten(2, (num_heads_kv, -1)).expand(-1, -1, -1, num_heads_per_group, -1) out = xops.memory_efficient_attention(query, key, value, attn_mask, dropout_p, scale) if enable_gqa: out = out.flatten(2, 3) return out
{ "repo_id": "huggingface/diffusers", "file_path": "src/diffusers/models/attention_dispatch.py", "license": "Apache License 2.0", "lines": 3083, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
huggingface/diffusers:scripts/convert_skyreelsv2_to_diffusers.py
import argparse import os import pathlib from typing import Any, Dict import torch from accelerate import init_empty_weights from huggingface_hub import hf_hub_download from safetensors.torch import load_file from transformers import AutoProcessor, AutoTokenizer, CLIPVisionModelWithProjection, UMT5EncoderModel from diffusers import ( AutoencoderKLWan, SkyReelsV2DiffusionForcingPipeline, SkyReelsV2ImageToVideoPipeline, SkyReelsV2Pipeline, SkyReelsV2Transformer3DModel, UniPCMultistepScheduler, ) TRANSFORMER_KEYS_RENAME_DICT = { "time_embedding.0": "condition_embedder.time_embedder.linear_1", "time_embedding.2": "condition_embedder.time_embedder.linear_2", "text_embedding.0": "condition_embedder.text_embedder.linear_1", "text_embedding.2": "condition_embedder.text_embedder.linear_2", "time_projection.1": "condition_embedder.time_proj", "head.modulation": "scale_shift_table", "head.head": "proj_out", "modulation": "scale_shift_table", "ffn.0": "ffn.net.0.proj", "ffn.2": "ffn.net.2", "fps_projection.0": "fps_projection.net.0.proj", "fps_projection.2": "fps_projection.net.2", # Hack to swap the layer names # The original model calls the norms in following order: norm1, norm3, norm2 # We convert it to: norm1, norm2, norm3 "norm2": "norm__placeholder", "norm3": "norm2", "norm__placeholder": "norm3", # For the I2V model "img_emb.proj.0": "condition_embedder.image_embedder.norm1", "img_emb.proj.1": "condition_embedder.image_embedder.ff.net.0.proj", "img_emb.proj.3": "condition_embedder.image_embedder.ff.net.2", "img_emb.proj.4": "condition_embedder.image_embedder.norm2", # for the FLF2V model "img_emb.emb_pos": "condition_embedder.image_embedder.pos_embed", # Add attention component mappings "self_attn.q": "attn1.to_q", "self_attn.k": "attn1.to_k", "self_attn.v": "attn1.to_v", "self_attn.o": "attn1.to_out.0", "self_attn.norm_q": "attn1.norm_q", "self_attn.norm_k": "attn1.norm_k", "cross_attn.q": "attn2.to_q", "cross_attn.k": "attn2.to_k", "cross_attn.v": "attn2.to_v", "cross_attn.o": "attn2.to_out.0", "cross_attn.norm_q": "attn2.norm_q", "cross_attn.norm_k": "attn2.norm_k", "attn2.to_k_img": "attn2.add_k_proj", "attn2.to_v_img": "attn2.add_v_proj", "attn2.norm_k_img": "attn2.norm_added_k", } TRANSFORMER_SPECIAL_KEYS_REMAP = {} def update_state_dict_(state_dict: Dict[str, Any], old_key: str, new_key: str) -> dict[str, Any]: state_dict[new_key] = state_dict.pop(old_key) def load_sharded_safetensors(dir: pathlib.Path): if "720P" in str(dir): file_paths = list(dir.glob("diffusion_pytorch_model*.safetensors")) else: file_paths = list(dir.glob("model*.safetensors")) state_dict = {} for path in file_paths: state_dict.update(load_file(path)) return state_dict def get_transformer_config(model_type: str) -> dict[str, Any]: if model_type == "SkyReels-V2-DF-1.3B-540P": config = { "model_id": "Skywork/SkyReels-V2-DF-1.3B-540P", "diffusers_config": { "added_kv_proj_dim": None, "attention_head_dim": 128, "cross_attn_norm": True, "eps": 1e-06, "ffn_dim": 8960, "freq_dim": 256, "in_channels": 16, "num_attention_heads": 12, "inject_sample_info": True, "num_layers": 30, "out_channels": 16, "patch_size": [1, 2, 2], "qk_norm": "rms_norm_across_heads", "text_dim": 4096, }, } elif model_type == "SkyReels-V2-DF-14B-720P": config = { "model_id": "Skywork/SkyReels-V2-DF-14B-720P", "diffusers_config": { "added_kv_proj_dim": None, "attention_head_dim": 128, "cross_attn_norm": True, "eps": 1e-06, "ffn_dim": 13824, "freq_dim": 256, "in_channels": 16, "num_attention_heads": 40, "inject_sample_info": False, "num_layers": 40, "out_channels": 16, "patch_size": [1, 2, 2], "qk_norm": "rms_norm_across_heads", "text_dim": 4096, }, } elif model_type == "SkyReels-V2-DF-14B-540P": config = { "model_id": "Skywork/SkyReels-V2-DF-14B-540P", "diffusers_config": { "added_kv_proj_dim": None, "attention_head_dim": 128, "cross_attn_norm": True, "eps": 1e-06, "ffn_dim": 13824, "freq_dim": 256, "in_channels": 16, "num_attention_heads": 40, "inject_sample_info": False, "num_layers": 40, "out_channels": 16, "patch_size": [1, 2, 2], "qk_norm": "rms_norm_across_heads", "text_dim": 4096, }, } elif model_type == "SkyReels-V2-T2V-14B-720P": config = { "model_id": "Skywork/SkyReels-V2-T2V-14B-720P", "diffusers_config": { "added_kv_proj_dim": None, "attention_head_dim": 128, "cross_attn_norm": True, "eps": 1e-06, "ffn_dim": 13824, "freq_dim": 256, "in_channels": 16, "num_attention_heads": 40, "inject_sample_info": False, "num_layers": 40, "out_channels": 16, "patch_size": [1, 2, 2], "qk_norm": "rms_norm_across_heads", "text_dim": 4096, }, } elif model_type == "SkyReels-V2-T2V-14B-540P": config = { "model_id": "Skywork/SkyReels-V2-T2V-14B-540P", "diffusers_config": { "added_kv_proj_dim": None, "attention_head_dim": 128, "cross_attn_norm": True, "eps": 1e-06, "ffn_dim": 13824, "freq_dim": 256, "in_channels": 16, "num_attention_heads": 40, "inject_sample_info": False, "num_layers": 40, "out_channels": 16, "patch_size": [1, 2, 2], "qk_norm": "rms_norm_across_heads", "text_dim": 4096, }, } elif model_type == "SkyReels-V2-I2V-1.3B-540P": config = { "model_id": "Skywork/SkyReels-V2-I2V-1.3B-540P", "diffusers_config": { "added_kv_proj_dim": 1536, "attention_head_dim": 128, "cross_attn_norm": True, "eps": 1e-06, "ffn_dim": 8960, "freq_dim": 256, "in_channels": 36, "num_attention_heads": 12, "inject_sample_info": False, "num_layers": 30, "out_channels": 16, "patch_size": [1, 2, 2], "qk_norm": "rms_norm_across_heads", "text_dim": 4096, "image_dim": 1280, }, } elif model_type == "SkyReels-V2-I2V-14B-540P": config = { "model_id": "Skywork/SkyReels-V2-I2V-14B-540P", "diffusers_config": { "added_kv_proj_dim": 5120, "attention_head_dim": 128, "cross_attn_norm": True, "eps": 1e-06, "ffn_dim": 13824, "freq_dim": 256, "in_channels": 36, "num_attention_heads": 40, "inject_sample_info": False, "num_layers": 40, "out_channels": 16, "patch_size": [1, 2, 2], "qk_norm": "rms_norm_across_heads", "text_dim": 4096, "image_dim": 1280, }, } elif model_type == "SkyReels-V2-I2V-14B-720P": config = { "model_id": "Skywork/SkyReels-V2-I2V-14B-720P", "diffusers_config": { "added_kv_proj_dim": 5120, "attention_head_dim": 128, "cross_attn_norm": True, "eps": 1e-06, "ffn_dim": 13824, "freq_dim": 256, "in_channels": 36, "num_attention_heads": 40, "inject_sample_info": False, "num_layers": 40, "out_channels": 16, "patch_size": [1, 2, 2], "qk_norm": "rms_norm_across_heads", "text_dim": 4096, "image_dim": 1280, }, } elif model_type == "SkyReels-V2-FLF2V-1.3B-540P": config = { "model_id": "Skywork/SkyReels-V2-I2V-1.3B-540P", "diffusers_config": { "added_kv_proj_dim": 1536, "attention_head_dim": 128, "cross_attn_norm": True, "eps": 1e-06, "ffn_dim": 8960, "freq_dim": 256, "in_channels": 36, "num_attention_heads": 12, "inject_sample_info": False, "num_layers": 30, "out_channels": 16, "patch_size": [1, 2, 2], "qk_norm": "rms_norm_across_heads", "text_dim": 4096, "image_dim": 1280, "pos_embed_seq_len": 514, }, } elif model_type == "SkyReels-V2-FLF2V-14B-540P": config = { "model_id": "Skywork/SkyReels-V2-I2V-14B-540P", "diffusers_config": { "added_kv_proj_dim": 5120, "attention_head_dim": 128, "cross_attn_norm": True, "eps": 1e-06, "ffn_dim": 13824, "freq_dim": 256, "in_channels": 36, "num_attention_heads": 40, "inject_sample_info": False, "num_layers": 40, "out_channels": 16, "patch_size": [1, 2, 2], "qk_norm": "rms_norm_across_heads", "text_dim": 4096, "image_dim": 1280, "pos_embed_seq_len": 514, }, } elif model_type == "SkyReels-V2-FLF2V-14B-720P": config = { "model_id": "Skywork/SkyReels-V2-I2V-14B-720P", "diffusers_config": { "added_kv_proj_dim": 5120, "attention_head_dim": 128, "cross_attn_norm": True, "eps": 1e-06, "ffn_dim": 13824, "freq_dim": 256, "in_channels": 36, "num_attention_heads": 40, "inject_sample_info": False, "num_layers": 40, "out_channels": 16, "patch_size": [1, 2, 2], "qk_norm": "rms_norm_across_heads", "text_dim": 4096, "image_dim": 1280, "pos_embed_seq_len": 514, }, } return config def convert_transformer(model_type: str): config = get_transformer_config(model_type) diffusers_config = config["diffusers_config"] model_id = config["model_id"] if "1.3B" in model_type: original_state_dict = load_file(hf_hub_download(model_id, "model.safetensors")) else: os.makedirs(model_type, exist_ok=True) model_dir = pathlib.Path(model_type) if "720P" in model_type: top_shard = 7 if "I2V" in model_type else 6 zeros = "0" * (4 if "I2V" or "T2V" in model_type else 3) model_name = "diffusion_pytorch_model" elif "540P" in model_type: top_shard = 14 if "I2V" in model_type else 12 model_name = "model" for i in range(1, top_shard + 1): shard_path = f"{model_name}-{i:05d}-of-{zeros}{top_shard}.safetensors" hf_hub_download(model_id, shard_path, local_dir=model_dir) original_state_dict = load_sharded_safetensors(model_dir) with init_empty_weights(): transformer = SkyReelsV2Transformer3DModel.from_config(diffusers_config) for key in list(original_state_dict.keys()): new_key = key[:] for replace_key, rename_key in TRANSFORMER_KEYS_RENAME_DICT.items(): new_key = new_key.replace(replace_key, rename_key) update_state_dict_(original_state_dict, key, new_key) for key in list(original_state_dict.keys()): for special_key, handler_fn_inplace in TRANSFORMER_SPECIAL_KEYS_REMAP.items(): if special_key not in key: continue handler_fn_inplace(key, original_state_dict) if "FLF2V" in model_type: if ( hasattr(transformer.condition_embedder, "image_embedder") and hasattr(transformer.condition_embedder.image_embedder, "pos_embed") and transformer.condition_embedder.image_embedder.pos_embed is not None ): pos_embed_shape = transformer.condition_embedder.image_embedder.pos_embed.shape original_state_dict["condition_embedder.image_embedder.pos_embed"] = torch.zeros(pos_embed_shape) transformer.load_state_dict(original_state_dict, strict=True, assign=True) return transformer def convert_vae(): vae_ckpt_path = hf_hub_download("Wan-AI/Wan2.1-T2V-14B", "Wan2.1_VAE.pth") old_state_dict = torch.load(vae_ckpt_path, weights_only=True) new_state_dict = {} # Create mappings for specific components middle_key_mapping = { # Encoder middle block "encoder.middle.0.residual.0.gamma": "encoder.mid_block.resnets.0.norm1.gamma", "encoder.middle.0.residual.2.bias": "encoder.mid_block.resnets.0.conv1.bias", "encoder.middle.0.residual.2.weight": "encoder.mid_block.resnets.0.conv1.weight", "encoder.middle.0.residual.3.gamma": "encoder.mid_block.resnets.0.norm2.gamma", "encoder.middle.0.residual.6.bias": "encoder.mid_block.resnets.0.conv2.bias", "encoder.middle.0.residual.6.weight": "encoder.mid_block.resnets.0.conv2.weight", "encoder.middle.2.residual.0.gamma": "encoder.mid_block.resnets.1.norm1.gamma", "encoder.middle.2.residual.2.bias": "encoder.mid_block.resnets.1.conv1.bias", "encoder.middle.2.residual.2.weight": "encoder.mid_block.resnets.1.conv1.weight", "encoder.middle.2.residual.3.gamma": "encoder.mid_block.resnets.1.norm2.gamma", "encoder.middle.2.residual.6.bias": "encoder.mid_block.resnets.1.conv2.bias", "encoder.middle.2.residual.6.weight": "encoder.mid_block.resnets.1.conv2.weight", # Decoder middle block "decoder.middle.0.residual.0.gamma": "decoder.mid_block.resnets.0.norm1.gamma", "decoder.middle.0.residual.2.bias": "decoder.mid_block.resnets.0.conv1.bias", "decoder.middle.0.residual.2.weight": "decoder.mid_block.resnets.0.conv1.weight", "decoder.middle.0.residual.3.gamma": "decoder.mid_block.resnets.0.norm2.gamma", "decoder.middle.0.residual.6.bias": "decoder.mid_block.resnets.0.conv2.bias", "decoder.middle.0.residual.6.weight": "decoder.mid_block.resnets.0.conv2.weight", "decoder.middle.2.residual.0.gamma": "decoder.mid_block.resnets.1.norm1.gamma", "decoder.middle.2.residual.2.bias": "decoder.mid_block.resnets.1.conv1.bias", "decoder.middle.2.residual.2.weight": "decoder.mid_block.resnets.1.conv1.weight", "decoder.middle.2.residual.3.gamma": "decoder.mid_block.resnets.1.norm2.gamma", "decoder.middle.2.residual.6.bias": "decoder.mid_block.resnets.1.conv2.bias", "decoder.middle.2.residual.6.weight": "decoder.mid_block.resnets.1.conv2.weight", } # Create a mapping for attention blocks attention_mapping = { # Encoder middle attention "encoder.middle.1.norm.gamma": "encoder.mid_block.attentions.0.norm.gamma", "encoder.middle.1.to_qkv.weight": "encoder.mid_block.attentions.0.to_qkv.weight", "encoder.middle.1.to_qkv.bias": "encoder.mid_block.attentions.0.to_qkv.bias", "encoder.middle.1.proj.weight": "encoder.mid_block.attentions.0.proj.weight", "encoder.middle.1.proj.bias": "encoder.mid_block.attentions.0.proj.bias", # Decoder middle attention "decoder.middle.1.norm.gamma": "decoder.mid_block.attentions.0.norm.gamma", "decoder.middle.1.to_qkv.weight": "decoder.mid_block.attentions.0.to_qkv.weight", "decoder.middle.1.to_qkv.bias": "decoder.mid_block.attentions.0.to_qkv.bias", "decoder.middle.1.proj.weight": "decoder.mid_block.attentions.0.proj.weight", "decoder.middle.1.proj.bias": "decoder.mid_block.attentions.0.proj.bias", } # Create a mapping for the head components head_mapping = { # Encoder head "encoder.head.0.gamma": "encoder.norm_out.gamma", "encoder.head.2.bias": "encoder.conv_out.bias", "encoder.head.2.weight": "encoder.conv_out.weight", # Decoder head "decoder.head.0.gamma": "decoder.norm_out.gamma", "decoder.head.2.bias": "decoder.conv_out.bias", "decoder.head.2.weight": "decoder.conv_out.weight", } # Create a mapping for the quant components quant_mapping = { "conv1.weight": "quant_conv.weight", "conv1.bias": "quant_conv.bias", "conv2.weight": "post_quant_conv.weight", "conv2.bias": "post_quant_conv.bias", } # Process each key in the state dict for key, value in old_state_dict.items(): # Handle middle block keys using the mapping if key in middle_key_mapping: new_key = middle_key_mapping[key] new_state_dict[new_key] = value # Handle attention blocks using the mapping elif key in attention_mapping: new_key = attention_mapping[key] new_state_dict[new_key] = value # Handle head keys using the mapping elif key in head_mapping: new_key = head_mapping[key] new_state_dict[new_key] = value # Handle quant keys using the mapping elif key in quant_mapping: new_key = quant_mapping[key] new_state_dict[new_key] = value # Handle encoder conv1 elif key == "encoder.conv1.weight": new_state_dict["encoder.conv_in.weight"] = value elif key == "encoder.conv1.bias": new_state_dict["encoder.conv_in.bias"] = value # Handle decoder conv1 elif key == "decoder.conv1.weight": new_state_dict["decoder.conv_in.weight"] = value elif key == "decoder.conv1.bias": new_state_dict["decoder.conv_in.bias"] = value # Handle encoder downsamples elif key.startswith("encoder.downsamples."): # Convert to down_blocks new_key = key.replace("encoder.downsamples.", "encoder.down_blocks.") # Convert residual block naming but keep the original structure if ".residual.0.gamma" in new_key: new_key = new_key.replace(".residual.0.gamma", ".norm1.gamma") elif ".residual.2.bias" in new_key: new_key = new_key.replace(".residual.2.bias", ".conv1.bias") elif ".residual.2.weight" in new_key: new_key = new_key.replace(".residual.2.weight", ".conv1.weight") elif ".residual.3.gamma" in new_key: new_key = new_key.replace(".residual.3.gamma", ".norm2.gamma") elif ".residual.6.bias" in new_key: new_key = new_key.replace(".residual.6.bias", ".conv2.bias") elif ".residual.6.weight" in new_key: new_key = new_key.replace(".residual.6.weight", ".conv2.weight") elif ".shortcut.bias" in new_key: new_key = new_key.replace(".shortcut.bias", ".conv_shortcut.bias") elif ".shortcut.weight" in new_key: new_key = new_key.replace(".shortcut.weight", ".conv_shortcut.weight") new_state_dict[new_key] = value # Handle decoder upsamples elif key.startswith("decoder.upsamples."): # Convert to up_blocks parts = key.split(".") block_idx = int(parts[2]) # Group residual blocks if "residual" in key: if block_idx in [0, 1, 2]: new_block_idx = 0 resnet_idx = block_idx elif block_idx in [4, 5, 6]: new_block_idx = 1 resnet_idx = block_idx - 4 elif block_idx in [8, 9, 10]: new_block_idx = 2 resnet_idx = block_idx - 8 elif block_idx in [12, 13, 14]: new_block_idx = 3 resnet_idx = block_idx - 12 else: # Keep as is for other blocks new_state_dict[key] = value continue # Convert residual block naming if ".residual.0.gamma" in key: new_key = f"decoder.up_blocks.{new_block_idx}.resnets.{resnet_idx}.norm1.gamma" elif ".residual.2.bias" in key: new_key = f"decoder.up_blocks.{new_block_idx}.resnets.{resnet_idx}.conv1.bias" elif ".residual.2.weight" in key: new_key = f"decoder.up_blocks.{new_block_idx}.resnets.{resnet_idx}.conv1.weight" elif ".residual.3.gamma" in key: new_key = f"decoder.up_blocks.{new_block_idx}.resnets.{resnet_idx}.norm2.gamma" elif ".residual.6.bias" in key: new_key = f"decoder.up_blocks.{new_block_idx}.resnets.{resnet_idx}.conv2.bias" elif ".residual.6.weight" in key: new_key = f"decoder.up_blocks.{new_block_idx}.resnets.{resnet_idx}.conv2.weight" else: new_key = key new_state_dict[new_key] = value # Handle shortcut connections elif ".shortcut." in key: if block_idx == 4: new_key = key.replace(".shortcut.", ".resnets.0.conv_shortcut.") new_key = new_key.replace("decoder.upsamples.4", "decoder.up_blocks.1") else: new_key = key.replace("decoder.upsamples.", "decoder.up_blocks.") new_key = new_key.replace(".shortcut.", ".conv_shortcut.") new_state_dict[new_key] = value # Handle upsamplers elif ".resample." in key or ".time_conv." in key: if block_idx == 3: new_key = key.replace(f"decoder.upsamples.{block_idx}", "decoder.up_blocks.0.upsamplers.0") elif block_idx == 7: new_key = key.replace(f"decoder.upsamples.{block_idx}", "decoder.up_blocks.1.upsamplers.0") elif block_idx == 11: new_key = key.replace(f"decoder.upsamples.{block_idx}", "decoder.up_blocks.2.upsamplers.0") else: new_key = key.replace("decoder.upsamples.", "decoder.up_blocks.") new_state_dict[new_key] = value else: new_key = key.replace("decoder.upsamples.", "decoder.up_blocks.") new_state_dict[new_key] = value else: # Keep other keys unchanged new_state_dict[key] = value with init_empty_weights(): vae = AutoencoderKLWan() vae.load_state_dict(new_state_dict, strict=True, assign=True) return vae def get_args(): parser = argparse.ArgumentParser() parser.add_argument("--model_type", type=str, default=None) parser.add_argument("--output_path", type=str, required=True) parser.add_argument("--dtype", default="fp32") return parser.parse_args() DTYPE_MAPPING = { "fp32": torch.float32, "fp16": torch.float16, "bf16": torch.bfloat16, } if __name__ == "__main__": args = get_args() transformer = None dtype = DTYPE_MAPPING[args.dtype] transformer = convert_transformer(args.model_type).to(dtype=dtype) vae = convert_vae() text_encoder = UMT5EncoderModel.from_pretrained("google/umt5-xxl") tokenizer = AutoTokenizer.from_pretrained("google/umt5-xxl") scheduler = UniPCMultistepScheduler( prediction_type="flow_prediction", num_train_timesteps=1000, use_flow_sigmas=True, ) if "I2V" in args.model_type or "FLF2V" in args.model_type: image_encoder = CLIPVisionModelWithProjection.from_pretrained("laion/CLIP-ViT-H-14-laion2B-s32B-b79K") image_processor = AutoProcessor.from_pretrained("laion/CLIP-ViT-H-14-laion2B-s32B-b79K") pipe = SkyReelsV2ImageToVideoPipeline( transformer=transformer, text_encoder=text_encoder, tokenizer=tokenizer, vae=vae, scheduler=scheduler, image_encoder=image_encoder, image_processor=image_processor, ) elif "T2V" in args.model_type: pipe = SkyReelsV2Pipeline( transformer=transformer, text_encoder=text_encoder, tokenizer=tokenizer, vae=vae, scheduler=scheduler, ) elif "DF" in args.model_type: pipe = SkyReelsV2DiffusionForcingPipeline( transformer=transformer, text_encoder=text_encoder, tokenizer=tokenizer, vae=vae, scheduler=scheduler, ) pipe.save_pretrained( args.output_path, safe_serialization=True, max_shard_size="5GB", # push_to_hub=True, # repo_id=f"<place_holder>/{args.model_type}-Diffusers", )
{ "repo_id": "huggingface/diffusers", "file_path": "scripts/convert_skyreelsv2_to_diffusers.py", "license": "Apache License 2.0", "lines": 589, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
function_complex
huggingface/diffusers:src/diffusers/models/transformers/transformer_skyreels_v2.py
# Copyright 2025 The SkyReels Team, The Wan Team and The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import math from typing import Any import torch import torch.nn as nn import torch.nn.functional as F from ...configuration_utils import ConfigMixin, register_to_config from ...loaders import FromOriginalModelMixin, PeftAdapterMixin from ...utils import apply_lora_scale, deprecate, logging from ...utils.torch_utils import maybe_allow_in_graph from ..attention import AttentionMixin, AttentionModuleMixin, FeedForward from ..attention_dispatch import dispatch_attention_fn from ..cache_utils import CacheMixin from ..embeddings import ( PixArtAlphaTextProjection, TimestepEmbedding, get_1d_rotary_pos_embed, get_1d_sincos_pos_embed_from_grid, ) from ..modeling_outputs import Transformer2DModelOutput from ..modeling_utils import ModelMixin, get_parameter_dtype from ..normalization import FP32LayerNorm logger = logging.get_logger(__name__) # pylint: disable=invalid-name def _get_qkv_projections( attn: "SkyReelsV2Attention", hidden_states: torch.Tensor, encoder_hidden_states: torch.Tensor ): # encoder_hidden_states is only passed for cross-attention if encoder_hidden_states is None: encoder_hidden_states = hidden_states if attn.fused_projections: if attn.cross_attention_dim_head is None: # In self-attention layers, we can fuse the entire QKV projection into a single linear query, key, value = attn.to_qkv(hidden_states).chunk(3, dim=-1) else: # In cross-attention layers, we can only fuse the KV projections into a single linear query = attn.to_q(hidden_states) key, value = attn.to_kv(encoder_hidden_states).chunk(2, dim=-1) else: query = attn.to_q(hidden_states) key = attn.to_k(encoder_hidden_states) value = attn.to_v(encoder_hidden_states) return query, key, value def _get_added_kv_projections(attn: "SkyReelsV2Attention", encoder_hidden_states_img: torch.Tensor): if attn.fused_projections: key_img, value_img = attn.to_added_kv(encoder_hidden_states_img).chunk(2, dim=-1) else: key_img = attn.add_k_proj(encoder_hidden_states_img) value_img = attn.add_v_proj(encoder_hidden_states_img) return key_img, value_img class SkyReelsV2AttnProcessor: _attention_backend = None _parallel_config = None def __init__(self): if not hasattr(F, "scaled_dot_product_attention"): raise ImportError( "SkyReelsV2AttnProcessor requires PyTorch 2.0. To use it, please upgrade PyTorch to 2.0." ) def __call__( self, attn: "SkyReelsV2Attention", hidden_states: torch.Tensor, encoder_hidden_states: torch.Tensor | None = None, attention_mask: torch.Tensor | None = None, rotary_emb: tuple[torch.Tensor, torch.Tensor] | None = None, ) -> torch.Tensor: encoder_hidden_states_img = None if attn.add_k_proj is not None: # 512 is the context length of the text encoder, hardcoded for now image_context_length = encoder_hidden_states.shape[1] - 512 encoder_hidden_states_img = encoder_hidden_states[:, :image_context_length] encoder_hidden_states = encoder_hidden_states[:, image_context_length:] query, key, value = _get_qkv_projections(attn, hidden_states, encoder_hidden_states) query = attn.norm_q(query) key = attn.norm_k(key) query = query.unflatten(2, (attn.heads, -1)) key = key.unflatten(2, (attn.heads, -1)) value = value.unflatten(2, (attn.heads, -1)) if rotary_emb is not None: def apply_rotary_emb( hidden_states: torch.Tensor, freqs_cos: torch.Tensor, freqs_sin: torch.Tensor, ): x1, x2 = hidden_states.unflatten(-1, (-1, 2)).unbind(-1) cos = freqs_cos[..., 0::2] sin = freqs_sin[..., 1::2] out = torch.empty_like(hidden_states) out[..., 0::2] = x1 * cos - x2 * sin out[..., 1::2] = x1 * sin + x2 * cos return out.type_as(hidden_states) query = apply_rotary_emb(query, *rotary_emb) key = apply_rotary_emb(key, *rotary_emb) # I2V task hidden_states_img = None if encoder_hidden_states_img is not None: key_img, value_img = _get_added_kv_projections(attn, encoder_hidden_states_img) key_img = attn.norm_added_k(key_img) key_img = key_img.unflatten(2, (attn.heads, -1)) value_img = value_img.unflatten(2, (attn.heads, -1)) hidden_states_img = dispatch_attention_fn( query, key_img, value_img, attn_mask=None, dropout_p=0.0, is_causal=False, backend=self._attention_backend, parallel_config=self._parallel_config, ) hidden_states_img = hidden_states_img.flatten(2, 3) hidden_states_img = hidden_states_img.type_as(query) hidden_states = dispatch_attention_fn( query, key, value, attn_mask=attention_mask, dropout_p=0.0, is_causal=False, backend=self._attention_backend, parallel_config=self._parallel_config, ) hidden_states = hidden_states.flatten(2, 3) hidden_states = hidden_states.type_as(query) if hidden_states_img is not None: hidden_states = hidden_states + hidden_states_img hidden_states = attn.to_out[0](hidden_states) hidden_states = attn.to_out[1](hidden_states) return hidden_states class SkyReelsV2AttnProcessor2_0: def __new__(cls, *args, **kwargs): deprecation_message = ( "The SkyReelsV2AttnProcessor2_0 class is deprecated and will be removed in a future version. " "Please use SkyReelsV2AttnProcessor instead. " ) deprecate("SkyReelsV2AttnProcessor2_0", "1.0.0", deprecation_message, standard_warn=False) return SkyReelsV2AttnProcessor(*args, **kwargs) class SkyReelsV2Attention(torch.nn.Module, AttentionModuleMixin): _default_processor_cls = SkyReelsV2AttnProcessor _available_processors = [SkyReelsV2AttnProcessor] def __init__( self, dim: int, heads: int = 8, dim_head: int = 64, eps: float = 1e-5, dropout: float = 0.0, added_kv_proj_dim: int | None = None, cross_attention_dim_head: int | None = None, processor=None, is_cross_attention=None, ): super().__init__() self.inner_dim = dim_head * heads self.heads = heads self.added_kv_proj_dim = added_kv_proj_dim self.cross_attention_dim_head = cross_attention_dim_head self.kv_inner_dim = self.inner_dim if cross_attention_dim_head is None else cross_attention_dim_head * heads self.to_q = torch.nn.Linear(dim, self.inner_dim, bias=True) self.to_k = torch.nn.Linear(dim, self.kv_inner_dim, bias=True) self.to_v = torch.nn.Linear(dim, self.kv_inner_dim, bias=True) self.to_out = torch.nn.ModuleList( [ torch.nn.Linear(self.inner_dim, dim, bias=True), torch.nn.Dropout(dropout), ] ) self.norm_q = torch.nn.RMSNorm(dim_head * heads, eps=eps, elementwise_affine=True) self.norm_k = torch.nn.RMSNorm(dim_head * heads, eps=eps, elementwise_affine=True) self.add_k_proj = self.add_v_proj = None if added_kv_proj_dim is not None: self.add_k_proj = torch.nn.Linear(added_kv_proj_dim, self.inner_dim, bias=True) self.add_v_proj = torch.nn.Linear(added_kv_proj_dim, self.inner_dim, bias=True) self.norm_added_k = torch.nn.RMSNorm(dim_head * heads, eps=eps) self.is_cross_attention = cross_attention_dim_head is not None self.set_processor(processor) def fuse_projections(self): if getattr(self, "fused_projections", False): return if self.cross_attention_dim_head is None: concatenated_weights = torch.cat([self.to_q.weight.data, self.to_k.weight.data, self.to_v.weight.data]) concatenated_bias = torch.cat([self.to_q.bias.data, self.to_k.bias.data, self.to_v.bias.data]) out_features, in_features = concatenated_weights.shape with torch.device("meta"): self.to_qkv = nn.Linear(in_features, out_features, bias=True) self.to_qkv.load_state_dict( {"weight": concatenated_weights, "bias": concatenated_bias}, strict=True, assign=True ) else: concatenated_weights = torch.cat([self.to_k.weight.data, self.to_v.weight.data]) concatenated_bias = torch.cat([self.to_k.bias.data, self.to_v.bias.data]) out_features, in_features = concatenated_weights.shape with torch.device("meta"): self.to_kv = nn.Linear(in_features, out_features, bias=True) self.to_kv.load_state_dict( {"weight": concatenated_weights, "bias": concatenated_bias}, strict=True, assign=True ) if self.added_kv_proj_dim is not None: concatenated_weights = torch.cat([self.add_k_proj.weight.data, self.add_v_proj.weight.data]) concatenated_bias = torch.cat([self.add_k_proj.bias.data, self.add_v_proj.bias.data]) out_features, in_features = concatenated_weights.shape with torch.device("meta"): self.to_added_kv = nn.Linear(in_features, out_features, bias=True) self.to_added_kv.load_state_dict( {"weight": concatenated_weights, "bias": concatenated_bias}, strict=True, assign=True ) self.fused_projections = True @torch.no_grad() def unfuse_projections(self): if not getattr(self, "fused_projections", False): return if hasattr(self, "to_qkv"): delattr(self, "to_qkv") if hasattr(self, "to_kv"): delattr(self, "to_kv") if hasattr(self, "to_added_kv"): delattr(self, "to_added_kv") self.fused_projections = False def forward( self, hidden_states: torch.Tensor, encoder_hidden_states: torch.Tensor | None = None, attention_mask: torch.Tensor | None = None, rotary_emb: tuple[torch.Tensor, torch.Tensor] | None = None, **kwargs, ) -> torch.Tensor: return self.processor(self, hidden_states, encoder_hidden_states, attention_mask, rotary_emb, **kwargs) class SkyReelsV2ImageEmbedding(torch.nn.Module): def __init__(self, in_features: int, out_features: int, pos_embed_seq_len=None): super().__init__() self.norm1 = FP32LayerNorm(in_features) self.ff = FeedForward(in_features, out_features, mult=1, activation_fn="gelu") self.norm2 = FP32LayerNorm(out_features) if pos_embed_seq_len is not None: self.pos_embed = nn.Parameter(torch.zeros(1, pos_embed_seq_len, in_features)) else: self.pos_embed = None def forward(self, encoder_hidden_states_image: torch.Tensor) -> torch.Tensor: if self.pos_embed is not None: batch_size, seq_len, embed_dim = encoder_hidden_states_image.shape encoder_hidden_states_image = encoder_hidden_states_image.view(-1, 2 * seq_len, embed_dim) encoder_hidden_states_image = encoder_hidden_states_image + self.pos_embed hidden_states = self.norm1(encoder_hidden_states_image) hidden_states = self.ff(hidden_states) hidden_states = self.norm2(hidden_states) return hidden_states class SkyReelsV2Timesteps(nn.Module): def __init__(self, num_channels: int, flip_sin_to_cos: bool, output_type: str = "pt"): super().__init__() self.num_channels = num_channels self.output_type = output_type self.flip_sin_to_cos = flip_sin_to_cos def forward(self, timesteps: torch.Tensor) -> torch.Tensor: original_shape = timesteps.shape t_emb = get_1d_sincos_pos_embed_from_grid( self.num_channels, timesteps, output_type=self.output_type, flip_sin_to_cos=self.flip_sin_to_cos, ) # Reshape back to maintain batch structure if len(original_shape) > 1: t_emb = t_emb.reshape(*original_shape, self.num_channels) return t_emb class SkyReelsV2TimeTextImageEmbedding(nn.Module): def __init__( self, dim: int, time_freq_dim: int, time_proj_dim: int, text_embed_dim: int, image_embed_dim: int | None = None, pos_embed_seq_len: int | None = None, ): super().__init__() self.timesteps_proj = SkyReelsV2Timesteps(num_channels=time_freq_dim, flip_sin_to_cos=True) self.time_embedder = TimestepEmbedding(in_channels=time_freq_dim, time_embed_dim=dim) self.act_fn = nn.SiLU() self.time_proj = nn.Linear(dim, time_proj_dim) self.text_embedder = PixArtAlphaTextProjection(text_embed_dim, dim, act_fn="gelu_tanh") self.image_embedder = None if image_embed_dim is not None: self.image_embedder = SkyReelsV2ImageEmbedding(image_embed_dim, dim, pos_embed_seq_len=pos_embed_seq_len) def forward( self, timestep: torch.Tensor, encoder_hidden_states: torch.Tensor, encoder_hidden_states_image: torch.Tensor | None = None, ): timestep = self.timesteps_proj(timestep) time_embedder_dtype = get_parameter_dtype(self.time_embedder) if timestep.dtype != time_embedder_dtype and time_embedder_dtype != torch.int8: timestep = timestep.to(time_embedder_dtype) temb = self.time_embedder(timestep).type_as(encoder_hidden_states) timestep_proj = self.time_proj(self.act_fn(temb)) encoder_hidden_states = self.text_embedder(encoder_hidden_states) if encoder_hidden_states_image is not None: encoder_hidden_states_image = self.image_embedder(encoder_hidden_states_image) return temb, timestep_proj, encoder_hidden_states, encoder_hidden_states_image class SkyReelsV2RotaryPosEmbed(nn.Module): def __init__( self, attention_head_dim: int, patch_size: tuple[int, int, int], max_seq_len: int, theta: float = 10000.0, ): super().__init__() self.attention_head_dim = attention_head_dim self.patch_size = patch_size self.max_seq_len = max_seq_len h_dim = w_dim = 2 * (attention_head_dim // 6) t_dim = attention_head_dim - h_dim - w_dim freqs_dtype = torch.float32 if torch.backends.mps.is_available() else torch.float64 self.t_dim = t_dim self.h_dim = h_dim self.w_dim = w_dim freqs_cos = [] freqs_sin = [] for dim in [t_dim, h_dim, w_dim]: freq_cos, freq_sin = get_1d_rotary_pos_embed( dim, max_seq_len, theta, use_real=True, repeat_interleave_real=True, freqs_dtype=freqs_dtype, ) freqs_cos.append(freq_cos) freqs_sin.append(freq_sin) self.register_buffer("freqs_cos", torch.cat(freqs_cos, dim=1), persistent=False) self.register_buffer("freqs_sin", torch.cat(freqs_sin, dim=1), persistent=False) def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: batch_size, num_channels, num_frames, height, width = hidden_states.shape p_t, p_h, p_w = self.patch_size ppf, pph, ppw = num_frames // p_t, height // p_h, width // p_w split_sizes = [self.t_dim, self.h_dim, self.w_dim] freqs_cos = self.freqs_cos.split(split_sizes, dim=1) freqs_sin = self.freqs_sin.split(split_sizes, dim=1) freqs_cos_f = freqs_cos[0][:ppf].view(ppf, 1, 1, -1).expand(ppf, pph, ppw, -1) freqs_cos_h = freqs_cos[1][:pph].view(1, pph, 1, -1).expand(ppf, pph, ppw, -1) freqs_cos_w = freqs_cos[2][:ppw].view(1, 1, ppw, -1).expand(ppf, pph, ppw, -1) freqs_sin_f = freqs_sin[0][:ppf].view(ppf, 1, 1, -1).expand(ppf, pph, ppw, -1) freqs_sin_h = freqs_sin[1][:pph].view(1, pph, 1, -1).expand(ppf, pph, ppw, -1) freqs_sin_w = freqs_sin[2][:ppw].view(1, 1, ppw, -1).expand(ppf, pph, ppw, -1) freqs_cos = torch.cat([freqs_cos_f, freqs_cos_h, freqs_cos_w], dim=-1).reshape(1, ppf * pph * ppw, 1, -1) freqs_sin = torch.cat([freqs_sin_f, freqs_sin_h, freqs_sin_w], dim=-1).reshape(1, ppf * pph * ppw, 1, -1) return freqs_cos, freqs_sin @maybe_allow_in_graph class SkyReelsV2TransformerBlock(nn.Module): def __init__( self, dim: int, ffn_dim: int, num_heads: int, qk_norm: str = "rms_norm_across_heads", cross_attn_norm: bool = False, eps: float = 1e-6, added_kv_proj_dim: int | None = None, ): super().__init__() # 1. Self-attention self.norm1 = FP32LayerNorm(dim, eps, elementwise_affine=False) self.attn1 = SkyReelsV2Attention( dim=dim, heads=num_heads, dim_head=dim // num_heads, eps=eps, cross_attention_dim_head=None, processor=SkyReelsV2AttnProcessor(), ) # 2. Cross-attention self.attn2 = SkyReelsV2Attention( dim=dim, heads=num_heads, dim_head=dim // num_heads, eps=eps, added_kv_proj_dim=added_kv_proj_dim, cross_attention_dim_head=dim // num_heads, processor=SkyReelsV2AttnProcessor(), ) self.norm2 = FP32LayerNorm(dim, eps, elementwise_affine=True) if cross_attn_norm else nn.Identity() # 3. Feed-forward self.ffn = FeedForward(dim, inner_dim=ffn_dim, activation_fn="gelu-approximate") self.norm3 = FP32LayerNorm(dim, eps, elementwise_affine=False) self.scale_shift_table = nn.Parameter(torch.randn(1, 6, dim) / dim**0.5) def forward( self, hidden_states: torch.Tensor, encoder_hidden_states: torch.Tensor, temb: torch.Tensor, rotary_emb: torch.Tensor, attention_mask: torch.Tensor, ) -> torch.Tensor: if temb.dim() == 3: shift_msa, scale_msa, gate_msa, c_shift_msa, c_scale_msa, c_gate_msa = ( self.scale_shift_table + temb.float() ).chunk(6, dim=1) elif temb.dim() == 4: # For 4D temb in Diffusion Forcing framework, we assume the shape is (b, 6, f * pp_h * pp_w, inner_dim) e = (self.scale_shift_table.unsqueeze(2) + temb.float()).chunk(6, dim=1) shift_msa, scale_msa, gate_msa, c_shift_msa, c_scale_msa, c_gate_msa = [ei.squeeze(1) for ei in e] # 1. Self-attention norm_hidden_states = (self.norm1(hidden_states.float()) * (1 + scale_msa) + shift_msa).type_as(hidden_states) attn_output = self.attn1(norm_hidden_states, None, attention_mask, rotary_emb) hidden_states = (hidden_states.float() + attn_output * gate_msa).type_as(hidden_states) # 2. Cross-attention norm_hidden_states = self.norm2(hidden_states.float()).type_as(hidden_states) attn_output = self.attn2(norm_hidden_states, encoder_hidden_states, None, None) hidden_states = hidden_states + attn_output # 3. Feed-forward norm_hidden_states = (self.norm3(hidden_states.float()) * (1 + c_scale_msa) + c_shift_msa).type_as( hidden_states ) ff_output = self.ffn(norm_hidden_states) hidden_states = (hidden_states.float() + ff_output.float() * c_gate_msa).type_as(hidden_states) return hidden_states class SkyReelsV2Transformer3DModel( ModelMixin, ConfigMixin, PeftAdapterMixin, FromOriginalModelMixin, CacheMixin, AttentionMixin ): r""" A Transformer model for video-like data used in the Wan-based SkyReels-V2 model. Args: patch_size (`tuple[int]`, defaults to `(1, 2, 2)`): 3D patch dimensions for video embedding (t_patch, h_patch, w_patch). num_attention_heads (`int`, defaults to `16`): Fixed length for text embeddings. attention_head_dim (`int`, defaults to `128`): The number of channels in each head. in_channels (`int`, defaults to `16`): The number of channels in the input. out_channels (`int`, defaults to `16`): The number of channels in the output. text_dim (`int`, defaults to `4096`): Input dimension for text embeddings. freq_dim (`int`, defaults to `256`): Dimension for sinusoidal time embeddings. ffn_dim (`int`, defaults to `8192`): Intermediate dimension in feed-forward network. num_layers (`int`, defaults to `32`): The number of layers of transformer blocks to use. window_size (`tuple[int]`, defaults to `(-1, -1)`): Window size for local attention (-1 indicates global attention). cross_attn_norm (`bool`, defaults to `True`): Enable cross-attention normalization. qk_norm (`str`, *optional*, defaults to `"rms_norm_across_heads"`): Enable query/key normalization. eps (`float`, defaults to `1e-6`): Epsilon value for normalization layers. inject_sample_info (`bool`, defaults to `False`): Whether to inject sample information into the model. image_dim (`int`, *optional*): The dimension of the image embeddings. added_kv_proj_dim (`int`, *optional*): The dimension of the added key/value projection. rope_max_seq_len (`int`, defaults to `1024`): The maximum sequence length for the rotary embeddings. pos_embed_seq_len (`int`, *optional*): The sequence length for the positional embeddings. """ _supports_gradient_checkpointing = True _skip_layerwise_casting_patterns = ["patch_embedding", "condition_embedder", "norm"] _no_split_modules = ["SkyReelsV2TransformerBlock"] _keep_in_fp32_modules = ["time_embedder", "scale_shift_table", "norm1", "norm2", "norm3"] _keys_to_ignore_on_load_unexpected = ["norm_added_q"] _repeated_blocks = ["SkyReelsV2TransformerBlock"] @register_to_config def __init__( self, patch_size: tuple[int] = (1, 2, 2), num_attention_heads: int = 16, attention_head_dim: int = 128, in_channels: int = 16, out_channels: int = 16, text_dim: int = 4096, freq_dim: int = 256, ffn_dim: int = 8192, num_layers: int = 32, cross_attn_norm: bool = True, qk_norm: str | None = "rms_norm_across_heads", eps: float = 1e-6, image_dim: int | None = None, added_kv_proj_dim: int | None = None, rope_max_seq_len: int = 1024, pos_embed_seq_len: int | None = None, inject_sample_info: bool = False, num_frame_per_block: int = 1, ) -> None: super().__init__() inner_dim = num_attention_heads * attention_head_dim out_channels = out_channels or in_channels # 1. Patch & position embedding self.rope = SkyReelsV2RotaryPosEmbed(attention_head_dim, patch_size, rope_max_seq_len) self.patch_embedding = nn.Conv3d(in_channels, inner_dim, kernel_size=patch_size, stride=patch_size) # 2. Condition embeddings # image_embedding_dim=1280 for I2V model self.condition_embedder = SkyReelsV2TimeTextImageEmbedding( dim=inner_dim, time_freq_dim=freq_dim, time_proj_dim=inner_dim * 6, text_embed_dim=text_dim, image_embed_dim=image_dim, pos_embed_seq_len=pos_embed_seq_len, ) # 3. Transformer blocks self.blocks = nn.ModuleList( [ SkyReelsV2TransformerBlock( inner_dim, ffn_dim, num_attention_heads, qk_norm, cross_attn_norm, eps, added_kv_proj_dim ) for _ in range(num_layers) ] ) # 4. Output norm & projection self.norm_out = FP32LayerNorm(inner_dim, eps, elementwise_affine=False) self.proj_out = nn.Linear(inner_dim, out_channels * math.prod(patch_size)) self.scale_shift_table = nn.Parameter(torch.randn(1, 2, inner_dim) / inner_dim**0.5) if inject_sample_info: self.fps_embedding = nn.Embedding(2, inner_dim) self.fps_projection = FeedForward(inner_dim, inner_dim * 6, mult=1, activation_fn="linear-silu") self.gradient_checkpointing = False @apply_lora_scale("attention_kwargs") def forward( self, hidden_states: torch.Tensor, timestep: torch.LongTensor, encoder_hidden_states: torch.Tensor, encoder_hidden_states_image: torch.Tensor | None = None, enable_diffusion_forcing: bool = False, fps: torch.Tensor | None = None, return_dict: bool = True, attention_kwargs: dict[str, Any] | None = None, ) -> torch.Tensor | dict[str, torch.Tensor]: batch_size, num_channels, num_frames, height, width = hidden_states.shape p_t, p_h, p_w = self.config.patch_size post_patch_num_frames = num_frames // p_t post_patch_height = height // p_h post_patch_width = width // p_w rotary_emb = self.rope(hidden_states) hidden_states = self.patch_embedding(hidden_states) hidden_states = hidden_states.flatten(2).transpose(1, 2) causal_mask = None if self.config.num_frame_per_block > 1: block_num = post_patch_num_frames // self.config.num_frame_per_block range_tensor = torch.arange(block_num, device=hidden_states.device).repeat_interleave( self.config.num_frame_per_block ) causal_mask = range_tensor.unsqueeze(0) <= range_tensor.unsqueeze(1) # f, f causal_mask = causal_mask.view(post_patch_num_frames, 1, 1, post_patch_num_frames, 1, 1) causal_mask = causal_mask.repeat( 1, post_patch_height, post_patch_width, 1, post_patch_height, post_patch_width ) causal_mask = causal_mask.reshape( post_patch_num_frames * post_patch_height * post_patch_width, post_patch_num_frames * post_patch_height * post_patch_width, ) causal_mask = causal_mask.unsqueeze(0).unsqueeze(0) temb, timestep_proj, encoder_hidden_states, encoder_hidden_states_image = self.condition_embedder( timestep, encoder_hidden_states, encoder_hidden_states_image ) timestep_proj = timestep_proj.unflatten(-1, (6, -1)) if encoder_hidden_states_image is not None: encoder_hidden_states = torch.concat([encoder_hidden_states_image, encoder_hidden_states], dim=1) if self.config.inject_sample_info: fps = torch.tensor(fps, dtype=torch.long, device=hidden_states.device) fps_emb = self.fps_embedding(fps) if enable_diffusion_forcing: timestep_proj = timestep_proj + self.fps_projection(fps_emb).unflatten(1, (6, -1)).repeat( timestep.shape[1], 1, 1 ) else: timestep_proj = timestep_proj + self.fps_projection(fps_emb).unflatten(1, (6, -1)) if enable_diffusion_forcing: b, f = timestep.shape temb = temb.view(b, f, 1, 1, -1) timestep_proj = timestep_proj.view(b, f, 1, 1, 6, -1) # (b, f, 1, 1, 6, inner_dim) temb = temb.repeat(1, 1, post_patch_height, post_patch_width, 1).flatten(1, 3) timestep_proj = timestep_proj.repeat(1, 1, post_patch_height, post_patch_width, 1, 1).flatten( 1, 3 ) # (b, f, pp_h, pp_w, 6, inner_dim) -> (b, f * pp_h * pp_w, 6, inner_dim) timestep_proj = timestep_proj.transpose(1, 2).contiguous() # (b, 6, f * pp_h * pp_w, inner_dim) # 4. Transformer blocks if torch.is_grad_enabled() and self.gradient_checkpointing: for block in self.blocks: hidden_states = self._gradient_checkpointing_func( block, hidden_states, encoder_hidden_states, timestep_proj, rotary_emb, causal_mask, ) else: for block in self.blocks: hidden_states = block( hidden_states, encoder_hidden_states, timestep_proj, rotary_emb, causal_mask, ) if temb.dim() == 2: # If temb is 2D, we assume it has time 1-D time embedding values for each batch. # For models: # - Skywork/SkyReels-V2-T2V-14B-540P-Diffusers # - Skywork/SkyReels-V2-T2V-14B-720P-Diffusers # - Skywork/SkyReels-V2-I2V-1.3B-540P-Diffusers # - Skywork/SkyReels-V2-I2V-14B-540P-Diffusers # - Skywork/SkyReels-V2-I2V-14B-720P-Diffusers shift, scale = (self.scale_shift_table + temb.unsqueeze(1)).chunk(2, dim=1) elif temb.dim() == 3: # If temb is 3D, we assume it has 2-D time embedding values for each batch. # Each time embedding tensor includes values for each latent frame; thus Diffusion Forcing. # For models: # - Skywork/SkyReels-V2-DF-1.3B-540P-Diffusers # - Skywork/SkyReels-V2-DF-14B-540P-Diffusers # - Skywork/SkyReels-V2-DF-14B-720P-Diffusers shift, scale = (self.scale_shift_table.unsqueeze(2) + temb.unsqueeze(1)).chunk(2, dim=1) shift, scale = shift.squeeze(1), scale.squeeze(1) # Move the shift and scale tensors to the same device as hidden_states. # When using multi-GPU inference via accelerate these will be on the # first device rather than the last device, which hidden_states ends up # on. shift = shift.to(hidden_states.device) scale = scale.to(hidden_states.device) hidden_states = (self.norm_out(hidden_states.float()) * (1 + scale) + shift).type_as(hidden_states) hidden_states = self.proj_out(hidden_states) hidden_states = hidden_states.reshape( batch_size, post_patch_num_frames, post_patch_height, post_patch_width, p_t, p_h, p_w, -1 ) hidden_states = hidden_states.permute(0, 7, 1, 4, 2, 5, 3, 6) output = hidden_states.flatten(6, 7).flatten(4, 5).flatten(2, 3) if not return_dict: return (output,) return Transformer2DModelOutput(sample=output) def _set_ar_attention(self, causal_block_size: int): self.register_to_config(num_frame_per_block=causal_block_size)
{ "repo_id": "huggingface/diffusers", "file_path": "src/diffusers/models/transformers/transformer_skyreels_v2.py", "license": "Apache License 2.0", "lines": 648, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
huggingface/diffusers:src/diffusers/pipelines/skyreels_v2/pipeline_output.py
from dataclasses import dataclass import torch from diffusers.utils import BaseOutput @dataclass class SkyReelsV2PipelineOutput(BaseOutput): r""" Output class for SkyReelsV2 pipelines. Args: frames (`torch.Tensor`, `np.ndarray`, or list[list[PIL.Image.Image]]): list of video outputs - It can be a nested list of length `batch_size,` with each sub-list containing denoised PIL image sequences of length `num_frames.` It can also be a NumPy array or Torch tensor of shape `(batch_size, num_frames, channels, height, width)`. """ frames: torch.Tensor
{ "repo_id": "huggingface/diffusers", "file_path": "src/diffusers/pipelines/skyreels_v2/pipeline_output.py", "license": "Apache License 2.0", "lines": 14, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
huggingface/diffusers:src/diffusers/pipelines/skyreels_v2/pipeline_skyreels_v2.py
# Copyright 2025 The SkyReels-V2 Team, The Wan Team and The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import html from typing import Any, Callable import regex as re import torch from transformers import AutoTokenizer, T5EncoderModel, UMT5EncoderModel from ...callbacks import MultiPipelineCallbacks, PipelineCallback from ...loaders import SkyReelsV2LoraLoaderMixin from ...models import AutoencoderKLWan, SkyReelsV2Transformer3DModel from ...schedulers import UniPCMultistepScheduler from ...utils import is_ftfy_available, is_torch_xla_available, logging, replace_example_docstring from ...utils.torch_utils import randn_tensor from ...video_processor import VideoProcessor from ..pipeline_utils import DiffusionPipeline from .pipeline_output import SkyReelsV2PipelineOutput if is_torch_xla_available(): import torch_xla.core.xla_model as xm XLA_AVAILABLE = True else: XLA_AVAILABLE = False logger = logging.get_logger(__name__) # pylint: disable=invalid-name if is_ftfy_available(): import ftfy EXAMPLE_DOC_STRING = """\ Examples: ```py >>> import torch >>> from diffusers import ( ... SkyReelsV2Pipeline, ... UniPCMultistepScheduler, ... AutoencoderKLWan, ... ) >>> from diffusers.utils import export_to_video >>> # Load the pipeline >>> # Available models: >>> # - Skywork/SkyReels-V2-T2V-14B-540P-Diffusers >>> # - Skywork/SkyReels-V2-T2V-14B-720P-Diffusers >>> vae = AutoencoderKLWan.from_pretrained( ... "Skywork/SkyReels-V2-T2V-14B-720P-Diffusers", ... subfolder="vae", ... torch_dtype=torch.float32, ... ) >>> pipe = SkyReelsV2Pipeline.from_pretrained( ... "Skywork/SkyReels-V2-T2V-14B-720P-Diffusers", ... vae=vae, ... torch_dtype=torch.bfloat16, ... ) >>> flow_shift = 8.0 # 8.0 for T2V, 5.0 for I2V >>> pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config, flow_shift=flow_shift) >>> pipe = pipe.to("cuda") >>> prompt = "A cat and a dog baking a cake together in a kitchen. The cat is carefully measuring flour, while the dog is stirring the batter with a wooden spoon. The kitchen is cozy, with sunlight streaming through the window." >>> output = pipe( ... prompt=prompt, ... num_inference_steps=50, ... height=544, ... width=960, ... guidance_scale=6.0, # 6.0 for T2V, 5.0 for I2V ... num_frames=97, ... ).frames[0] >>> export_to_video(output, "video.mp4", fps=24, quality=8) ``` """ def basic_clean(text): text = ftfy.fix_text(text) text = html.unescape(html.unescape(text)) return text.strip() def whitespace_clean(text): text = re.sub(r"\s+", " ", text) text = text.strip() return text def prompt_clean(text): text = whitespace_clean(basic_clean(text)) return text class SkyReelsV2Pipeline(DiffusionPipeline, SkyReelsV2LoraLoaderMixin): r""" Pipeline for Text-to-Video (t2v) generation using SkyReels-V2. This model inherits from [`DiffusionPipeline`]. Check the superclass documentation for the generic methods implemented for all pipelines (downloading, saving, running on a particular device, etc.). Args: tokenizer ([`T5Tokenizer`]): Tokenizer from [T5](https://huggingface.co/docs/transformers/en/model_doc/t5#transformers.T5Tokenizer), specifically the [google/umt5-xxl](https://huggingface.co/google/umt5-xxl) variant. text_encoder ([`T5EncoderModel`]): [T5](https://huggingface.co/docs/transformers/en/model_doc/t5#transformers.T5EncoderModel), specifically the [google/umt5-xxl](https://huggingface.co/google/umt5-xxl) variant. transformer ([`SkyReelsV2Transformer3DModel`]): Conditional Transformer to denoise the input latents. scheduler ([`UniPCMultistepScheduler`]): A scheduler to be used in combination with `transformer` to denoise the encoded image latents. vae ([`AutoencoderKLWan`]): Variational Auto-Encoder (VAE) Model to encode and decode videos to and from latent representations. """ model_cpu_offload_seq = "text_encoder->transformer->vae" _callback_tensor_inputs = ["latents", "prompt_embeds", "negative_prompt_embeds"] def __init__( self, tokenizer: AutoTokenizer, text_encoder: T5EncoderModel | UMT5EncoderModel, transformer: SkyReelsV2Transformer3DModel, vae: AutoencoderKLWan, scheduler: UniPCMultistepScheduler, ): super().__init__() self.register_modules( vae=vae, text_encoder=text_encoder, tokenizer=tokenizer, transformer=transformer, scheduler=scheduler, ) self.vae_scale_factor_temporal = 2 ** sum(self.vae.temperal_downsample) if getattr(self, "vae", None) else 4 self.vae_scale_factor_spatial = 2 ** len(self.vae.temperal_downsample) if getattr(self, "vae", None) else 8 self.video_processor = VideoProcessor(vae_scale_factor=self.vae_scale_factor_spatial) # Copied from diffusers.pipelines.wan.pipeline_wan.WanPipeline._get_t5_prompt_embeds def _get_t5_prompt_embeds( self, prompt: str | list[str] = None, num_videos_per_prompt: int = 1, max_sequence_length: int = 226, device: torch.device | None = None, dtype: torch.dtype | None = None, ): device = device or self._execution_device dtype = dtype or self.text_encoder.dtype prompt = [prompt] if isinstance(prompt, str) else prompt prompt = [prompt_clean(u) for u in prompt] batch_size = len(prompt) text_inputs = self.tokenizer( prompt, padding="max_length", max_length=max_sequence_length, truncation=True, add_special_tokens=True, return_attention_mask=True, return_tensors="pt", ) text_input_ids, mask = text_inputs.input_ids, text_inputs.attention_mask seq_lens = mask.gt(0).sum(dim=1).long() prompt_embeds = self.text_encoder(text_input_ids.to(device), mask.to(device)).last_hidden_state prompt_embeds = prompt_embeds.to(dtype=dtype, device=device) prompt_embeds = [u[:v] for u, v in zip(prompt_embeds, seq_lens)] prompt_embeds = torch.stack( [torch.cat([u, u.new_zeros(max_sequence_length - u.size(0), u.size(1))]) for u in prompt_embeds], dim=0 ) # duplicate text embeddings for each generation per prompt, using mps friendly method _, seq_len, _ = prompt_embeds.shape prompt_embeds = prompt_embeds.repeat(1, num_videos_per_prompt, 1) prompt_embeds = prompt_embeds.view(batch_size * num_videos_per_prompt, seq_len, -1) return prompt_embeds # Copied from diffusers.pipelines.wan.pipeline_wan.WanPipeline.encode_prompt def encode_prompt( self, prompt: str | list[str], negative_prompt: str | list[str] | None = None, do_classifier_free_guidance: bool = True, num_videos_per_prompt: int = 1, prompt_embeds: torch.Tensor | None = None, negative_prompt_embeds: torch.Tensor | None = None, max_sequence_length: int = 226, device: torch.device | None = None, dtype: torch.dtype | None = None, ): r""" Encodes the prompt into text encoder hidden states. Args: prompt (`str` or `list[str]`, *optional*): prompt to be encoded negative_prompt (`str` or `list[str]`, *optional*): The prompt or prompts not to guide the image generation. If not defined, one has to pass `negative_prompt_embeds` instead. Ignored when not using guidance (i.e., ignored if `guidance_scale` is less than `1`). do_classifier_free_guidance (`bool`, *optional*, defaults to `True`): Whether to use classifier free guidance or not. num_videos_per_prompt (`int`, *optional*, defaults to 1): Number of videos that should be generated per prompt. torch device to place the resulting embeddings on prompt_embeds (`torch.Tensor`, *optional*): Pre-generated text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt weighting. If not provided, text embeddings will be generated from `prompt` input argument. negative_prompt_embeds (`torch.Tensor`, *optional*): Pre-generated negative text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt weighting. If not provided, negative_prompt_embeds will be generated from `negative_prompt` input argument. device: (`torch.device`, *optional*): torch device dtype: (`torch.dtype`, *optional*): torch dtype """ device = device or self._execution_device prompt = [prompt] if isinstance(prompt, str) else prompt if prompt is not None: batch_size = len(prompt) else: batch_size = prompt_embeds.shape[0] if prompt_embeds is None: prompt_embeds = self._get_t5_prompt_embeds( prompt=prompt, num_videos_per_prompt=num_videos_per_prompt, max_sequence_length=max_sequence_length, device=device, dtype=dtype, ) if do_classifier_free_guidance and negative_prompt_embeds is None: negative_prompt = negative_prompt or "" negative_prompt = batch_size * [negative_prompt] if isinstance(negative_prompt, str) else negative_prompt if prompt is not None and type(prompt) is not type(negative_prompt): raise TypeError( f"`negative_prompt` should be the same type to `prompt`, but got {type(negative_prompt)} !=" f" {type(prompt)}." ) elif batch_size != len(negative_prompt): raise ValueError( f"`negative_prompt`: {negative_prompt} has batch size {len(negative_prompt)}, but `prompt`:" f" {prompt} has batch size {batch_size}. Please make sure that passed `negative_prompt` matches" " the batch size of `prompt`." ) negative_prompt_embeds = self._get_t5_prompt_embeds( prompt=negative_prompt, num_videos_per_prompt=num_videos_per_prompt, max_sequence_length=max_sequence_length, device=device, dtype=dtype, ) return prompt_embeds, negative_prompt_embeds def check_inputs( self, prompt, negative_prompt, height, width, prompt_embeds=None, negative_prompt_embeds=None, callback_on_step_end_tensor_inputs=None, ): if height % 16 != 0 or width % 16 != 0: raise ValueError(f"`height` and `width` have to be divisible by 16 but are {height} and {width}.") if callback_on_step_end_tensor_inputs is not None and not all( k in self._callback_tensor_inputs for k in callback_on_step_end_tensor_inputs ): raise ValueError( f"`callback_on_step_end_tensor_inputs` has to be in {self._callback_tensor_inputs}, but found {[k for k in callback_on_step_end_tensor_inputs if k not in self._callback_tensor_inputs]}" ) if prompt is not None and prompt_embeds is not None: raise ValueError( f"Cannot forward both `prompt`: {prompt} and `prompt_embeds`: {prompt_embeds}. Please make sure to" " only forward one of the two." ) elif negative_prompt is not None and negative_prompt_embeds is not None: raise ValueError( f"Cannot forward both `negative_prompt`: {negative_prompt} and `negative_prompt_embeds`: {negative_prompt_embeds}. Please make sure to" " only forward one of the two." ) elif prompt is None and prompt_embeds is None: raise ValueError( "Provide either `prompt` or `prompt_embeds`. Cannot leave both `prompt` and `prompt_embeds` undefined." ) elif prompt is not None and (not isinstance(prompt, str) and not isinstance(prompt, list)): raise ValueError(f"`prompt` has to be of type `str` or `list` but is {type(prompt)}") elif negative_prompt is not None and ( not isinstance(negative_prompt, str) and not isinstance(negative_prompt, list) ): raise ValueError(f"`negative_prompt` has to be of type `str` or `list` but is {type(negative_prompt)}") # Copied from diffusers.pipelines.wan.pipeline_wan.WanPipeline.prepare_latents def prepare_latents( self, batch_size: int, num_channels_latents: int = 16, height: int = 480, width: int = 832, num_frames: int = 81, dtype: torch.dtype | None = None, device: torch.device | None = None, generator: torch.Generator | list[torch.Generator] | None = None, latents: torch.Tensor | None = None, ) -> torch.Tensor: if latents is not None: return latents.to(device=device, dtype=dtype) num_latent_frames = (num_frames - 1) // self.vae_scale_factor_temporal + 1 shape = ( batch_size, num_channels_latents, num_latent_frames, int(height) // self.vae_scale_factor_spatial, int(width) // self.vae_scale_factor_spatial, ) if isinstance(generator, list) and len(generator) != batch_size: raise ValueError( f"You have passed a list of generators of length {len(generator)}, but requested an effective batch" f" size of {batch_size}. Make sure the batch size matches the length of the generators." ) latents = randn_tensor(shape, generator=generator, device=device, dtype=dtype) return latents @property def guidance_scale(self): return self._guidance_scale @property def do_classifier_free_guidance(self): return self._guidance_scale > 1.0 @property def num_timesteps(self): return self._num_timesteps @property def current_timestep(self): return self._current_timestep @property def interrupt(self): return self._interrupt @property def attention_kwargs(self): return self._attention_kwargs @torch.no_grad() @replace_example_docstring(EXAMPLE_DOC_STRING) def __call__( self, prompt: str | list[str] = None, negative_prompt: str | list[str] = None, height: int = 544, width: int = 960, num_frames: int = 97, num_inference_steps: int = 50, guidance_scale: float = 6.0, num_videos_per_prompt: int | None = 1, generator: torch.Generator | list[torch.Generator] | None = None, latents: torch.Tensor | None = None, prompt_embeds: torch.Tensor | None = None, negative_prompt_embeds: torch.Tensor | None = None, output_type: str | None = "np", return_dict: bool = True, attention_kwargs: dict[str, Any] | None = None, callback_on_step_end: Callable[[int, int], None] | PipelineCallback | MultiPipelineCallbacks | None = None, callback_on_step_end_tensor_inputs: list[str] = ["latents"], max_sequence_length: int = 512, ): r""" The call function to the pipeline for generation. Args: prompt (`str` or `list[str]`, *optional*): The prompt or prompts to guide the image generation. If not defined, one has to pass `prompt_embeds`. instead. height (`int`, defaults to `544`): The height in pixels of the generated image. width (`int`, defaults to `960`): The width in pixels of the generated image. num_frames (`int`, defaults to `97`): The number of frames in the generated video. num_inference_steps (`int`, defaults to `50`): The number of denoising steps. More denoising steps usually lead to a higher quality image at the expense of slower inference. guidance_scale (`float`, defaults to `6.0`): Guidance scale as defined in [Classifier-Free Diffusion Guidance](https://huggingface.co/papers/2207.12598). `guidance_scale` is defined as `w` of equation 2. of [Imagen Paper](https://huggingface.co/papers/2205.11487). Guidance scale is enabled by setting `guidance_scale > 1`. Higher guidance scale encourages to generate images that are closely linked to the text `prompt`, usually at the expense of lower image quality. num_videos_per_prompt (`int`, *optional*, defaults to 1): The number of images to generate per prompt. generator (`torch.Generator` or `list[torch.Generator]`, *optional*): A [`torch.Generator`](https://pytorch.org/docs/stable/generated/torch.Generator.html) to make generation deterministic. latents (`torch.Tensor`, *optional*): Pre-generated noisy latents sampled from a Gaussian distribution, to be used as inputs for image generation. Can be used to tweak the same generation with different prompts. If not provided, a latents tensor is generated by sampling using the supplied random `generator`. prompt_embeds (`torch.Tensor`, *optional*): Pre-generated text embeddings. Can be used to easily tweak text inputs (prompt weighting). If not provided, text embeddings are generated from the `prompt` input argument. output_type (`str`, *optional*, defaults to `"np"`): The output format of the generated image. Choose between `PIL.Image` or `np.array`. return_dict (`bool`, *optional*, defaults to `True`): Whether or not to return a [`SkyReelsV2PipelineOutput`] instead of a plain tuple. attention_kwargs (`dict`, *optional*): A kwargs dictionary that if specified is passed along to the `AttentionProcessor` as defined under `self.processor` in [diffusers.models.attention_processor](https://github.com/huggingface/diffusers/blob/main/src/diffusers/models/attention_processor.py). callback_on_step_end (`Callable`, `PipelineCallback`, `MultiPipelineCallbacks`, *optional*): A function or a subclass of `PipelineCallback` or `MultiPipelineCallbacks` that is called at the end of each denoising step during the inference. with the following arguments: `callback_on_step_end(self: DiffusionPipeline, step: int, timestep: int, callback_kwargs: Dict)`. `callback_kwargs` will include a list of all tensors as specified by `callback_on_step_end_tensor_inputs`. callback_on_step_end_tensor_inputs (`list`, *optional*): The list of tensor inputs for the `callback_on_step_end` function. The tensors specified in the list will be passed as `callback_kwargs` argument. You will only be able to include variables listed in the `._callback_tensor_inputs` attribute of your pipeline class. max_sequence_length (`int`, *optional*, defaults to `512`): The maximum sequence length for the text encoder. Examples: Returns: [`~SkyReelsV2PipelineOutput`] or `tuple`: If `return_dict` is `True`, [`SkyReelsV2PipelineOutput`] is returned, otherwise a `tuple` is returned where the first element is a list with the generated images and the second element is a list of `bool`s indicating whether the corresponding generated image contains "not-safe-for-work" (nsfw) content. """ if isinstance(callback_on_step_end, (PipelineCallback, MultiPipelineCallbacks)): callback_on_step_end_tensor_inputs = callback_on_step_end.tensor_inputs # 1. Check inputs. Raise error if not correct self.check_inputs( prompt, negative_prompt, height, width, prompt_embeds, negative_prompt_embeds, callback_on_step_end_tensor_inputs, ) if num_frames % self.vae_scale_factor_temporal != 1: logger.warning( f"`num_frames - 1` has to be divisible by {self.vae_scale_factor_temporal}. Rounding to the nearest number." ) num_frames = num_frames // self.vae_scale_factor_temporal * self.vae_scale_factor_temporal + 1 num_frames = max(num_frames, 1) self._guidance_scale = guidance_scale self._attention_kwargs = attention_kwargs self._current_timestep = None self._interrupt = False device = self._execution_device # 2. Define call parameters if prompt is not None and isinstance(prompt, str): batch_size = 1 elif prompt is not None and isinstance(prompt, list): batch_size = len(prompt) else: batch_size = prompt_embeds.shape[0] # 3. Encode input prompt prompt_embeds, negative_prompt_embeds = self.encode_prompt( prompt=prompt, negative_prompt=negative_prompt, do_classifier_free_guidance=self.do_classifier_free_guidance, num_videos_per_prompt=num_videos_per_prompt, prompt_embeds=prompt_embeds, negative_prompt_embeds=negative_prompt_embeds, max_sequence_length=max_sequence_length, device=device, ) transformer_dtype = self.transformer.dtype prompt_embeds = prompt_embeds.to(transformer_dtype) if negative_prompt_embeds is not None: negative_prompt_embeds = negative_prompt_embeds.to(transformer_dtype) # 4. Prepare timesteps self.scheduler.set_timesteps(num_inference_steps, device=device) timesteps = self.scheduler.timesteps # 5. Prepare latent variables num_channels_latents = self.transformer.config.in_channels latents = self.prepare_latents( batch_size * num_videos_per_prompt, num_channels_latents, height, width, num_frames, torch.float32, device, generator, latents, ) # 6. Denoising loop num_warmup_steps = len(timesteps) - num_inference_steps * self.scheduler.order self._num_timesteps = len(timesteps) with self.progress_bar(total=num_inference_steps) as progress_bar: for i, t in enumerate(timesteps): if self.interrupt: continue self._current_timestep = t latent_model_input = latents.to(transformer_dtype) timestep = t.expand(latents.shape[0]) with self.transformer.cache_context("cond"): noise_pred = self.transformer( hidden_states=latent_model_input, timestep=timestep, encoder_hidden_states=prompt_embeds, attention_kwargs=attention_kwargs, return_dict=False, )[0] if self.do_classifier_free_guidance: with self.transformer.cache_context("uncond"): noise_uncond = self.transformer( hidden_states=latent_model_input, timestep=timestep, encoder_hidden_states=negative_prompt_embeds, attention_kwargs=attention_kwargs, return_dict=False, )[0] noise_pred = noise_uncond + guidance_scale * (noise_pred - noise_uncond) # compute the previous noisy sample x_t -> x_t-1 latents = self.scheduler.step(noise_pred, t, latents, return_dict=False)[0] if callback_on_step_end is not None: callback_kwargs = {} for k in callback_on_step_end_tensor_inputs: callback_kwargs[k] = locals()[k] callback_outputs = callback_on_step_end(self, i, t, callback_kwargs) latents = callback_outputs.pop("latents", latents) prompt_embeds = callback_outputs.pop("prompt_embeds", prompt_embeds) negative_prompt_embeds = callback_outputs.pop("negative_prompt_embeds", negative_prompt_embeds) # call the callback, if provided if i == len(timesteps) - 1 or ((i + 1) > num_warmup_steps and (i + 1) % self.scheduler.order == 0): progress_bar.update() if XLA_AVAILABLE: xm.mark_step() self._current_timestep = None if not output_type == "latent": latents = latents.to(self.vae.dtype) latents_mean = ( torch.tensor(self.vae.config.latents_mean) .view(1, self.vae.config.z_dim, 1, 1, 1) .to(latents.device, latents.dtype) ) latents_std = 1.0 / torch.tensor(self.vae.config.latents_std).view(1, self.vae.config.z_dim, 1, 1, 1).to( latents.device, latents.dtype ) latents = latents / latents_std + latents_mean video = self.vae.decode(latents, return_dict=False)[0] video = self.video_processor.postprocess_video(video, output_type=output_type) else: video = latents # Offload all models self.maybe_free_model_hooks() if not return_dict: return (video,) return SkyReelsV2PipelineOutput(frames=video)
{ "repo_id": "huggingface/diffusers", "file_path": "src/diffusers/pipelines/skyreels_v2/pipeline_skyreels_v2.py", "license": "Apache License 2.0", "lines": 528, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
huggingface/diffusers:src/diffusers/pipelines/skyreels_v2/pipeline_skyreels_v2_diffusion_forcing.py
# Copyright 2025 The SkyReels-V2 Team, The Wan Team and The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import html import math import re from copy import deepcopy from typing import Any, Callable import torch from transformers import AutoTokenizer, T5EncoderModel, UMT5EncoderModel from ...callbacks import MultiPipelineCallbacks, PipelineCallback from ...loaders import SkyReelsV2LoraLoaderMixin from ...models import AutoencoderKLWan, SkyReelsV2Transformer3DModel from ...schedulers import UniPCMultistepScheduler from ...utils import is_ftfy_available, is_torch_xla_available, logging, replace_example_docstring from ...utils.torch_utils import randn_tensor from ...video_processor import VideoProcessor from ..pipeline_utils import DiffusionPipeline from .pipeline_output import SkyReelsV2PipelineOutput if is_torch_xla_available(): import torch_xla.core.xla_model as xm XLA_AVAILABLE = True else: XLA_AVAILABLE = False logger = logging.get_logger(__name__) # pylint: disable=invalid-name if is_ftfy_available(): import ftfy EXAMPLE_DOC_STRING = """\ Examples: ```py >>> import torch >>> from diffusers import ( ... SkyReelsV2DiffusionForcingPipeline, ... UniPCMultistepScheduler, ... AutoencoderKLWan, ... ) >>> from diffusers.utils import export_to_video >>> # Load the pipeline >>> # Available models: >>> # - Skywork/SkyReels-V2-DF-1.3B-540P-Diffusers >>> # - Skywork/SkyReels-V2-DF-14B-540P-Diffusers >>> # - Skywork/SkyReels-V2-DF-14B-720P-Diffusers >>> vae = AutoencoderKLWan.from_pretrained( ... "Skywork/SkyReels-V2-DF-14B-720P-Diffusers", ... subfolder="vae", ... torch_dtype=torch.float32, ... ) >>> pipe = SkyReelsV2DiffusionForcingPipeline.from_pretrained( ... "Skywork/SkyReels-V2-DF-14B-720P-Diffusers", ... vae=vae, ... torch_dtype=torch.bfloat16, ... ) >>> flow_shift = 8.0 # 8.0 for T2V, 5.0 for I2V >>> pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config, flow_shift=flow_shift) >>> pipe = pipe.to("cuda") >>> prompt = "A cat and a dog baking a cake together in a kitchen. The cat is carefully measuring flour, while the dog is stirring the batter with a wooden spoon. The kitchen is cozy, with sunlight streaming through the window." >>> output = pipe( ... prompt=prompt, ... num_inference_steps=30, ... height=544, ... width=960, ... guidance_scale=6.0, # 6.0 for T2V, 5.0 for I2V ... num_frames=97, ... ar_step=5, # Controls asynchronous inference (0 for synchronous mode) ... causal_block_size=5, # Number of frames processed together in a causal block ... overlap_history=None, # Number of frames to overlap for smooth transitions in long videos ... addnoise_condition=20, # Improves consistency in long video generation ... ).frames[0] >>> export_to_video(output, "video.mp4", fps=24, quality=8) ``` """ def basic_clean(text): text = ftfy.fix_text(text) text = html.unescape(html.unescape(text)) return text.strip() def whitespace_clean(text): text = re.sub(r"\s+", " ", text) text = text.strip() return text def prompt_clean(text): text = whitespace_clean(basic_clean(text)) return text # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion_img2img.retrieve_latents def retrieve_latents( encoder_output: torch.Tensor, generator: torch.Generator | None = None, sample_mode: str = "sample" ): if hasattr(encoder_output, "latent_dist") and sample_mode == "sample": return encoder_output.latent_dist.sample(generator) elif hasattr(encoder_output, "latent_dist") and sample_mode == "argmax": return encoder_output.latent_dist.mode() elif hasattr(encoder_output, "latents"): return encoder_output.latents else: raise AttributeError("Could not access latents of provided encoder_output") class SkyReelsV2DiffusionForcingPipeline(DiffusionPipeline, SkyReelsV2LoraLoaderMixin): """ Pipeline for Text-to-Video (t2v) generation using SkyReels-V2 with diffusion forcing. This model inherits from [`DiffusionPipeline`]. Check the superclass documentation for the generic methods implemented for all pipelines (downloading, saving, running on a specific device, etc.). Args: tokenizer ([`AutoTokenizer`]): Tokenizer from [T5](https://huggingface.co/docs/transformers/en/model_doc/t5#transformers.T5Tokenizer), specifically the [google/umt5-xxl](https://huggingface.co/google/umt5-xxl) variant. text_encoder ([`UMT5EncoderModel`]): [T5](https://huggingface.co/docs/transformers/en/model_doc/t5#transformers.T5EncoderModel), specifically the [google/umt5-xxl](https://huggingface.co/google/umt5-xxl) variant. transformer ([`SkyReelsV2Transformer3DModel`]): Conditional Transformer to denoise the encoded image latents. scheduler ([`UniPCMultistepScheduler`]): A scheduler to be used in combination with `transformer` to denoise the encoded image latents. vae ([`AutoencoderKLWan`]): Variational Auto-Encoder (VAE) Model to encode and decode videos to and from latent representations. """ model_cpu_offload_seq = "text_encoder->transformer->vae" _callback_tensor_inputs = ["latents", "prompt_embeds", "negative_prompt_embeds"] def __init__( self, tokenizer: AutoTokenizer, text_encoder: T5EncoderModel | UMT5EncoderModel, transformer: SkyReelsV2Transformer3DModel, vae: AutoencoderKLWan, scheduler: UniPCMultistepScheduler, ): super().__init__() self.register_modules( vae=vae, text_encoder=text_encoder, tokenizer=tokenizer, transformer=transformer, scheduler=scheduler, ) self.vae_scale_factor_temporal = 2 ** sum(self.vae.temperal_downsample) if getattr(self, "vae", None) else 4 self.vae_scale_factor_spatial = 2 ** len(self.vae.temperal_downsample) if getattr(self, "vae", None) else 8 self.video_processor = VideoProcessor(vae_scale_factor=self.vae_scale_factor_spatial) # Copied from diffusers.pipelines.wan.pipeline_wan.WanPipeline._get_t5_prompt_embeds def _get_t5_prompt_embeds( self, prompt: str | list[str] = None, num_videos_per_prompt: int = 1, max_sequence_length: int = 226, device: torch.device | None = None, dtype: torch.dtype | None = None, ): device = device or self._execution_device dtype = dtype or self.text_encoder.dtype prompt = [prompt] if isinstance(prompt, str) else prompt prompt = [prompt_clean(u) for u in prompt] batch_size = len(prompt) text_inputs = self.tokenizer( prompt, padding="max_length", max_length=max_sequence_length, truncation=True, add_special_tokens=True, return_attention_mask=True, return_tensors="pt", ) text_input_ids, mask = text_inputs.input_ids, text_inputs.attention_mask seq_lens = mask.gt(0).sum(dim=1).long() prompt_embeds = self.text_encoder(text_input_ids.to(device), mask.to(device)).last_hidden_state prompt_embeds = prompt_embeds.to(dtype=dtype, device=device) prompt_embeds = [u[:v] for u, v in zip(prompt_embeds, seq_lens)] prompt_embeds = torch.stack( [torch.cat([u, u.new_zeros(max_sequence_length - u.size(0), u.size(1))]) for u in prompt_embeds], dim=0 ) # duplicate text embeddings for each generation per prompt, using mps friendly method _, seq_len, _ = prompt_embeds.shape prompt_embeds = prompt_embeds.repeat(1, num_videos_per_prompt, 1) prompt_embeds = prompt_embeds.view(batch_size * num_videos_per_prompt, seq_len, -1) return prompt_embeds # Copied from diffusers.pipelines.wan.pipeline_wan.WanPipeline.encode_prompt def encode_prompt( self, prompt: str | list[str], negative_prompt: str | list[str] | None = None, do_classifier_free_guidance: bool = True, num_videos_per_prompt: int = 1, prompt_embeds: torch.Tensor | None = None, negative_prompt_embeds: torch.Tensor | None = None, max_sequence_length: int = 226, device: torch.device | None = None, dtype: torch.dtype | None = None, ): r""" Encodes the prompt into text encoder hidden states. Args: prompt (`str` or `list[str]`, *optional*): prompt to be encoded negative_prompt (`str` or `list[str]`, *optional*): The prompt or prompts not to guide the image generation. If not defined, one has to pass `negative_prompt_embeds` instead. Ignored when not using guidance (i.e., ignored if `guidance_scale` is less than `1`). do_classifier_free_guidance (`bool`, *optional*, defaults to `True`): Whether to use classifier free guidance or not. num_videos_per_prompt (`int`, *optional*, defaults to 1): Number of videos that should be generated per prompt. torch device to place the resulting embeddings on prompt_embeds (`torch.Tensor`, *optional*): Pre-generated text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt weighting. If not provided, text embeddings will be generated from `prompt` input argument. negative_prompt_embeds (`torch.Tensor`, *optional*): Pre-generated negative text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt weighting. If not provided, negative_prompt_embeds will be generated from `negative_prompt` input argument. device: (`torch.device`, *optional*): torch device dtype: (`torch.dtype`, *optional*): torch dtype """ device = device or self._execution_device prompt = [prompt] if isinstance(prompt, str) else prompt if prompt is not None: batch_size = len(prompt) else: batch_size = prompt_embeds.shape[0] if prompt_embeds is None: prompt_embeds = self._get_t5_prompt_embeds( prompt=prompt, num_videos_per_prompt=num_videos_per_prompt, max_sequence_length=max_sequence_length, device=device, dtype=dtype, ) if do_classifier_free_guidance and negative_prompt_embeds is None: negative_prompt = negative_prompt or "" negative_prompt = batch_size * [negative_prompt] if isinstance(negative_prompt, str) else negative_prompt if prompt is not None and type(prompt) is not type(negative_prompt): raise TypeError( f"`negative_prompt` should be the same type to `prompt`, but got {type(negative_prompt)} !=" f" {type(prompt)}." ) elif batch_size != len(negative_prompt): raise ValueError( f"`negative_prompt`: {negative_prompt} has batch size {len(negative_prompt)}, but `prompt`:" f" {prompt} has batch size {batch_size}. Please make sure that passed `negative_prompt` matches" " the batch size of `prompt`." ) negative_prompt_embeds = self._get_t5_prompt_embeds( prompt=negative_prompt, num_videos_per_prompt=num_videos_per_prompt, max_sequence_length=max_sequence_length, device=device, dtype=dtype, ) return prompt_embeds, negative_prompt_embeds def check_inputs( self, prompt, negative_prompt, height, width, prompt_embeds=None, negative_prompt_embeds=None, callback_on_step_end_tensor_inputs=None, overlap_history=None, num_frames=None, base_num_frames=None, ): if height % 16 != 0 or width % 16 != 0: raise ValueError(f"`height` and `width` have to be divisible by 16 but are {height} and {width}.") if callback_on_step_end_tensor_inputs is not None and not all( k in self._callback_tensor_inputs for k in callback_on_step_end_tensor_inputs ): raise ValueError( f"`callback_on_step_end_tensor_inputs` has to be in {self._callback_tensor_inputs}, but found {[k for k in callback_on_step_end_tensor_inputs if k not in self._callback_tensor_inputs]}" ) if prompt is not None and prompt_embeds is not None: raise ValueError( f"Cannot forward both `prompt`: {prompt} and `prompt_embeds`: {prompt_embeds}. Please make sure to" " only forward one of the two." ) elif negative_prompt is not None and negative_prompt_embeds is not None: raise ValueError( f"Cannot forward both `negative_prompt`: {negative_prompt} and `negative_prompt_embeds`: {negative_prompt_embeds}. Please make sure to" " only forward one of the two." ) elif prompt is None and prompt_embeds is None: raise ValueError( "Provide either `prompt` or `prompt_embeds`. Cannot leave both `prompt` and `prompt_embeds` undefined." ) elif prompt is not None and (not isinstance(prompt, str) and not isinstance(prompt, list)): raise ValueError(f"`prompt` has to be of type `str` or `list` but is {type(prompt)}") elif negative_prompt is not None and ( not isinstance(negative_prompt, str) and not isinstance(negative_prompt, list) ): raise ValueError(f"`negative_prompt` has to be of type `str` or `list` but is {type(negative_prompt)}") if num_frames > base_num_frames and overlap_history is None: raise ValueError( "`overlap_history` is required when `num_frames` exceeds `base_num_frames` to ensure smooth transitions in long video generation. " "Please specify a value for `overlap_history`. Recommended values are 17 or 37." ) def prepare_latents( self, batch_size: int, num_channels_latents: int = 16, height: int = 480, width: int = 832, num_frames: int = 97, dtype: torch.dtype | None = None, device: torch.device | None = None, generator: torch.Generator | list[torch.Generator] | None = None, latents: torch.Tensor | None = None, base_latent_num_frames: int | None = None, video_latents: torch.Tensor | None = None, causal_block_size: int | None = None, overlap_history_latent_frames: int | None = None, long_video_iter: int | None = None, ) -> torch.Tensor: if latents is not None: return latents.to(device=device, dtype=dtype) num_latent_frames = (num_frames - 1) // self.vae_scale_factor_temporal + 1 latent_height = height // self.vae_scale_factor_spatial latent_width = width // self.vae_scale_factor_spatial prefix_video_latents = None prefix_video_latents_frames = 0 if video_latents is not None: # long video generation at the iterations other than the first one prefix_video_latents = video_latents[:, :, -overlap_history_latent_frames:] if prefix_video_latents.shape[2] % causal_block_size != 0: truncate_len_latents = prefix_video_latents.shape[2] % causal_block_size logger.warning( f"The length of prefix video latents is truncated by {truncate_len_latents} frames for the causal block size alignment. " f"This truncation ensures compatibility with the causal block size, which is required for proper processing. " f"However, it may slightly affect the continuity of the generated video at the truncation boundary." ) prefix_video_latents = prefix_video_latents[:, :, :-truncate_len_latents] prefix_video_latents_frames = prefix_video_latents.shape[2] finished_frame_num = ( long_video_iter * (base_latent_num_frames - overlap_history_latent_frames) + overlap_history_latent_frames ) left_frame_num = num_latent_frames - finished_frame_num num_latent_frames = min(left_frame_num + overlap_history_latent_frames, base_latent_num_frames) elif base_latent_num_frames is not None: # long video generation at the first iteration num_latent_frames = base_latent_num_frames else: # short video generation num_latent_frames = (num_frames - 1) // self.vae_scale_factor_temporal + 1 shape = ( batch_size, num_channels_latents, num_latent_frames, latent_height, latent_width, ) if isinstance(generator, list) and len(generator) != batch_size: raise ValueError( f"You have passed a list of generators of length {len(generator)}, but requested an effective batch" f" size of {batch_size}. Make sure the batch size matches the length of the generators." ) latents = randn_tensor(shape, generator=generator, device=device, dtype=dtype) return latents, num_latent_frames, prefix_video_latents, prefix_video_latents_frames def generate_timestep_matrix( self, num_latent_frames: int, step_template: torch.Tensor, base_num_latent_frames: int, ar_step: int = 5, num_pre_ready: int = 0, causal_block_size: int = 1, shrink_interval_with_mask: bool = False, ) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor, list[tuple]]: """ This function implements the core diffusion forcing algorithm that creates a coordinated denoising schedule across temporal frames. It supports both synchronous and asynchronous generation modes: **Synchronous Mode** (ar_step=0, causal_block_size=1): - All frames are denoised simultaneously at each timestep - Each frame follows the same denoising trajectory: [1000, 800, 600, ..., 0] - Simpler but may have less temporal consistency for long videos **Asynchronous Mode** (ar_step>0, causal_block_size>1): - Frames are grouped into causal blocks and processed block/chunk-wise - Each block is denoised in a staggered pattern creating a "denoising wave" - Earlier blocks are more denoised, later blocks lag behind by ar_step timesteps - Creates stronger temporal dependencies and better consistency Args: num_latent_frames (int): Total number of latent frames to generate step_template (torch.Tensor): Base timestep schedule (e.g., [1000, 800, 600, ..., 0]) base_num_latent_frames (int): Maximum frames the model can process in one forward pass ar_step (int, optional): Autoregressive step size for temporal lag. 0 = synchronous, >0 = asynchronous. Defaults to 5. num_pre_ready (int, optional): Number of frames already denoised (e.g., from prefix in a video2video task). Defaults to 0. causal_block_size (int, optional): Number of frames processed as a causal block. Defaults to 1. shrink_interval_with_mask (bool, optional): Whether to optimize processing intervals. Defaults to False. Returns: tuple containing: - step_matrix (torch.Tensor): Matrix of timesteps for each frame at each iteration Shape: [num_iterations, num_latent_frames] - step_index (torch.Tensor): Index matrix for timestep lookup Shape: [num_iterations, num_latent_frames] - step_update_mask (torch.Tensor): Boolean mask indicating which frames to update Shape: [num_iterations, num_latent_frames] - valid_interval (list[tuple]): list of (start, end) intervals for each iteration Raises: ValueError: If ar_step is too small for the given configuration """ # Initialize lists to store the scheduling matrices and metadata step_matrix, step_index = [], [] # Will store timestep values and indices for each iteration update_mask, valid_interval = [], [] # Will store update masks and processing intervals # Calculate total number of denoising iterations (add 1 for initial noise state) num_iterations = len(step_template) + 1 # Convert frame counts to block counts for causal processing # Each block contains causal_block_size frames that are processed together # E.g.: 25 frames ÷ 5 = 5 blocks total num_blocks = num_latent_frames // causal_block_size base_num_blocks = base_num_latent_frames // causal_block_size # Validate ar_step is sufficient for the given configuration # In asynchronous mode, we need enough timesteps to create the staggered pattern if base_num_blocks < num_blocks: min_ar_step = len(step_template) / base_num_blocks if ar_step < min_ar_step: raise ValueError(f"`ar_step` should be at least {math.ceil(min_ar_step)} in your setting") # Extend step_template with boundary values for easier indexing # 999: dummy value for counter starting from 1 # 0: final timestep (completely denoised) step_template = torch.cat( [ torch.tensor([999], dtype=torch.int64, device=step_template.device), step_template.long(), torch.tensor([0], dtype=torch.int64, device=step_template.device), ] ) # Initialize the previous row state (tracks denoising progress for each block) # 0 means not started, num_iterations means fully denoised pre_row = torch.zeros(num_blocks, dtype=torch.long) # Mark pre-ready frames (e.g., from prefix video for a video2video task) as already at final denoising state if num_pre_ready > 0: pre_row[: num_pre_ready // causal_block_size] = num_iterations # Main loop: Generate denoising schedule until all frames are fully denoised while not torch.all(pre_row >= (num_iterations - 1)): # Create new row representing the next denoising step new_row = torch.zeros(num_blocks, dtype=torch.long) # Apply diffusion forcing logic for each block for i in range(num_blocks): if i == 0 or pre_row[i - 1] >= ( num_iterations - 1 ): # the first frame or the last frame is completely denoised new_row[i] = pre_row[i] + 1 else: # Asynchronous mode: lag behind previous block by ar_step timesteps # This creates the "diffusion forcing" staggered pattern new_row[i] = new_row[i - 1] - ar_step # Clamp values to valid range [0, num_iterations] new_row = new_row.clamp(0, num_iterations) # Create update mask: True for blocks that need denoising update at this iteration # Exclude blocks that haven't started (new_row != pre_row) or are finished (new_row != num_iterations) # Final state example: [False, ..., False, True, True, True, True, True] # where first 20 frames are done (False) and last 5 frames still need updates (True) update_mask.append((new_row != pre_row) & (new_row != num_iterations)) # Store the iteration state step_index.append(new_row) # Index into step_template step_matrix.append(step_template[new_row]) # Actual timestep values pre_row = new_row # Update for next iteration # For videos longer than model capacity, we process in sliding windows terminal_flag = base_num_blocks # Optional optimization: shrink interval based on first update mask if shrink_interval_with_mask: idx_sequence = torch.arange(num_blocks, dtype=torch.int64) update_mask = update_mask[0] update_mask_idx = idx_sequence[update_mask] last_update_idx = update_mask_idx[-1].item() terminal_flag = last_update_idx + 1 # Each interval defines which frames to process in the current forward pass for curr_mask in update_mask: # Extend terminal flag if current mask has updates beyond current terminal if terminal_flag < num_blocks and curr_mask[terminal_flag]: terminal_flag += 1 # Create interval: [start, end) where start ensures we don't exceed model capacity valid_interval.append((max(terminal_flag - base_num_blocks, 0), terminal_flag)) # Convert lists to tensors for efficient processing step_update_mask = torch.stack(update_mask, dim=0) step_index = torch.stack(step_index, dim=0) step_matrix = torch.stack(step_matrix, dim=0) # Each block's schedule is replicated to all frames within that block if causal_block_size > 1: # Expand each block to causal_block_size frames step_update_mask = step_update_mask.unsqueeze(-1).repeat(1, 1, causal_block_size).flatten(1).contiguous() step_index = step_index.unsqueeze(-1).repeat(1, 1, causal_block_size).flatten(1).contiguous() step_matrix = step_matrix.unsqueeze(-1).repeat(1, 1, causal_block_size).flatten(1).contiguous() # Scale intervals from block-level to frame-level valid_interval = [(s * causal_block_size, e * causal_block_size) for s, e in valid_interval] return step_matrix, step_index, step_update_mask, valid_interval @property def guidance_scale(self): return self._guidance_scale @property def do_classifier_free_guidance(self): return self._guidance_scale > 1.0 @property def num_timesteps(self): return self._num_timesteps @property def current_timestep(self): return self._current_timestep @property def interrupt(self): return self._interrupt @property def attention_kwargs(self): return self._attention_kwargs @torch.no_grad() @replace_example_docstring(EXAMPLE_DOC_STRING) def __call__( self, prompt: str | list[str], negative_prompt: str | list[str] = None, height: int = 544, width: int = 960, num_frames: int = 97, num_inference_steps: int = 50, guidance_scale: float = 6.0, num_videos_per_prompt: int | None = 1, generator: torch.Generator | list[torch.Generator] | None = None, latents: torch.Tensor | None = None, prompt_embeds: torch.Tensor | None = None, negative_prompt_embeds: torch.Tensor | None = None, output_type: str | None = "np", return_dict: bool = True, attention_kwargs: dict[str, Any] | None = None, callback_on_step_end: Callable[[int, int], None] | PipelineCallback | MultiPipelineCallbacks | None = None, callback_on_step_end_tensor_inputs: list[str] = ["latents"], max_sequence_length: int = 512, overlap_history: int | None = None, addnoise_condition: float = 0, base_num_frames: int = 97, ar_step: int = 0, causal_block_size: int | None = None, fps: int = 24, ): r""" The call function to the pipeline for generation. Args: prompt (`str` or `list[str]`, *optional*): The prompt or prompts to guide the image generation. If not defined, one has to pass `prompt_embeds`. instead. negative_prompt (`str` or `list[str]`, *optional*): The prompt or prompts not to guide the image generation. If not defined, one has to pass `negative_prompt_embeds` instead. Ignored when not using guidance (i.e., ignored if `guidance_scale` is less than `1`). height (`int`, defaults to `544`): The height of the generated video. width (`int`, defaults to `960`): The width of the generated video. num_frames (`int`, defaults to `97`): The number of frames in the generated video. num_inference_steps (`int`, defaults to `50`): The number of denoising steps. More denoising steps usually lead to a higher quality image at the expense of slower inference. guidance_scale (`float`, defaults to `6.0`): Guidance scale as defined in [Classifier-Free Diffusion Guidance](https://huggingface.co/papers/2207.12598). `guidance_scale` is defined as `w` of equation 2. of [Imagen Paper](https://huggingface.co/papers/2205.11487). Guidance scale is enabled by setting `guidance_scale > 1`. Higher guidance scale encourages to generate images that are closely linked to the text `prompt`, usually at the expense of lower image quality. (**6.0 for T2V**, **5.0 for I2V**) num_videos_per_prompt (`int`, *optional*, defaults to 1): The number of images to generate per prompt. generator (`torch.Generator` or `list[torch.Generator]`, *optional*): A [`torch.Generator`](https://pytorch.org/docs/stable/generated/torch.Generator.html) to make generation deterministic. latents (`torch.Tensor`, *optional*): Pre-generated noisy latents sampled from a Gaussian distribution, to be used as inputs for image generation. Can be used to tweak the same generation with different prompts. If not provided, a latents tensor is generated by sampling using the supplied random `generator`. prompt_embeds (`torch.Tensor`, *optional*): Pre-generated text embeddings. Can be used to easily tweak text inputs (prompt weighting). If not provided, text embeddings are generated from the `prompt` input argument. negative_prompt_embeds (`torch.Tensor`, *optional*): Pre-generated text embeddings. Can be used to easily tweak text inputs (prompt weighting). If not provided, text embeddings are generated from the `negative_prompt` input argument. output_type (`str`, *optional*, defaults to `"np"`): The output format of the generated image. Choose between `PIL.Image` or `np.array`. return_dict (`bool`, *optional*, defaults to `True`): Whether or not to return a [`SkyReelsV2PipelineOutput`] instead of a plain tuple. attention_kwargs (`dict`, *optional*): A kwargs dictionary that if specified is passed along to the `AttentionProcessor` as defined under `self.processor` in [diffusers.models.attention_processor](https://github.com/huggingface/diffusers/blob/main/src/diffusers/models/attention_processor.py). callback_on_step_end (`Callable`, `PipelineCallback`, `MultiPipelineCallbacks`, *optional*): A function or a subclass of `PipelineCallback` or `MultiPipelineCallbacks` that is called at the end of each denoising step during the inference. with the following arguments: `callback_on_step_end(self: DiffusionPipeline, step: int, timestep: int, callback_kwargs: Dict)`. `callback_kwargs` will include a list of all tensors as specified by `callback_on_step_end_tensor_inputs`. callback_on_step_end_tensor_inputs (`list`, *optional*): The list of tensor inputs for the `callback_on_step_end` function. The tensors specified in the list will be passed as `callback_kwargs` argument. You will only be able to include variables listed in the `._callback_tensor_inputs` attribute of your pipeline class. max_sequence_length (`int`, *optional*, defaults to `512`): The maximum sequence length of the prompt. overlap_history (`int`, *optional*, defaults to `None`): Number of frames to overlap for smooth transitions in long videos. If `None`, the pipeline assumes short video generation mode, and no overlap is applied. 17 and 37 are recommended to set. addnoise_condition (`float`, *optional*, defaults to `0`): This is used to help smooth the long video generation by adding some noise to the clean condition. Too large noise can cause the inconsistency as well. 20 is a recommended value, and you may try larger ones, but it is recommended to not exceed 50. base_num_frames (`int`, *optional*, defaults to `97`): 97 or 121 | Base frame count (**97 for 540P**, **121 for 720P**) ar_step (`int`, *optional*, defaults to `0`): Controls asynchronous inference (0 for synchronous mode) You can set `ar_step=5` to enable asynchronous inference. When asynchronous inference, `causal_block_size=5` is recommended while it is not supposed to be set for synchronous generation. Asynchronous inference will take more steps to diffuse the whole sequence which means it will be SLOWER than synchronous mode. In our experiments, asynchronous inference may improve the instruction following and visual consistent performance. causal_block_size (`int`, *optional*, defaults to `None`): The number of frames in each block/chunk. Recommended when using asynchronous inference (when ar_step > 0) fps (`int`, *optional*, defaults to `24`): Frame rate of the generated video Examples: Returns: [`~SkyReelsV2PipelineOutput`] or `tuple`: If `return_dict` is `True`, [`SkyReelsV2PipelineOutput`] is returned, otherwise a `tuple` is returned where the first element is a list with the generated images and the second element is a list of `bool`s indicating whether the corresponding generated image contains "not-safe-for-work" (nsfw) content. """ if isinstance(callback_on_step_end, (PipelineCallback, MultiPipelineCallbacks)): callback_on_step_end_tensor_inputs = callback_on_step_end.tensor_inputs # 1. Check inputs. Raise error if not correct self.check_inputs( prompt, negative_prompt, height, width, prompt_embeds, negative_prompt_embeds, callback_on_step_end_tensor_inputs, overlap_history, num_frames, base_num_frames, ) if addnoise_condition > 60: logger.warning( f"The value of 'addnoise_condition' is too large ({addnoise_condition}) and may cause inconsistencies in long video generation. A value of 20 is recommended." ) if num_frames % self.vae_scale_factor_temporal != 1: logger.warning( f"`num_frames - 1` has to be divisible by {self.vae_scale_factor_temporal}. Rounding to the nearest number." ) num_frames = num_frames // self.vae_scale_factor_temporal * self.vae_scale_factor_temporal + 1 num_frames = max(num_frames, 1) self._guidance_scale = guidance_scale self._attention_kwargs = attention_kwargs self._current_timestep = None self._interrupt = False device = self._execution_device # 2. Define call parameters if prompt is not None and isinstance(prompt, str): batch_size = 1 elif prompt is not None and isinstance(prompt, list): batch_size = len(prompt) else: batch_size = prompt_embeds.shape[0] # 3. Encode input prompt prompt_embeds, negative_prompt_embeds = self.encode_prompt( prompt=prompt, negative_prompt=negative_prompt, do_classifier_free_guidance=self.do_classifier_free_guidance, num_videos_per_prompt=num_videos_per_prompt, prompt_embeds=prompt_embeds, negative_prompt_embeds=negative_prompt_embeds, max_sequence_length=max_sequence_length, device=device, ) transformer_dtype = self.transformer.dtype prompt_embeds = prompt_embeds.to(transformer_dtype) if negative_prompt_embeds is not None: negative_prompt_embeds = negative_prompt_embeds.to(transformer_dtype) # 4. Prepare timesteps self.scheduler.set_timesteps(num_inference_steps, device=device) timesteps = self.scheduler.timesteps if causal_block_size is None: causal_block_size = self.transformer.config.num_frame_per_block else: self.transformer._set_ar_attention(causal_block_size) fps_embeds = [fps] * prompt_embeds.shape[0] fps_embeds = [0 if i == 16 else 1 for i in fps_embeds] # Determine if we're doing long video generation is_long_video = overlap_history is not None and base_num_frames is not None and num_frames > base_num_frames # Initialize accumulated_latents to store all latents in one tensor accumulated_latents = None if is_long_video: # Long video generation setup overlap_history_latent_frames = (overlap_history - 1) // self.vae_scale_factor_temporal + 1 num_latent_frames = (num_frames - 1) // self.vae_scale_factor_temporal + 1 base_latent_num_frames = ( (base_num_frames - 1) // self.vae_scale_factor_temporal + 1 if base_num_frames is not None else num_latent_frames ) n_iter = ( 1 + (num_latent_frames - base_latent_num_frames - 1) // (base_latent_num_frames - overlap_history_latent_frames) + 1 ) else: # Short video generation setup n_iter = 1 base_latent_num_frames = (num_frames - 1) // self.vae_scale_factor_temporal + 1 # Loop through iterations (multiple iterations only for long videos) for iter_idx in range(n_iter): if is_long_video: logger.debug(f"Processing iteration {iter_idx + 1}/{n_iter} for long video generation...") # 5. Prepare latent variables num_channels_latents = self.transformer.config.in_channels latents, current_num_latent_frames, prefix_video_latents, prefix_video_latents_frames = ( self.prepare_latents( batch_size * num_videos_per_prompt, num_channels_latents, height, width, num_frames, torch.float32, device, generator, latents if iter_idx == 0 else None, video_latents=accumulated_latents, # Pass latents directly instead of decoded video base_latent_num_frames=base_latent_num_frames if is_long_video else None, causal_block_size=causal_block_size, overlap_history_latent_frames=overlap_history_latent_frames if is_long_video else None, long_video_iter=iter_idx if is_long_video else None, ) ) if prefix_video_latents_frames > 0: latents[:, :, :prefix_video_latents_frames, :, :] = prefix_video_latents.to(transformer_dtype) # 6. Prepare sample schedulers and timestep matrix sample_schedulers = [] for _ in range(current_num_latent_frames): sample_scheduler = deepcopy(self.scheduler) sample_scheduler.set_timesteps(num_inference_steps, device=device) sample_schedulers.append(sample_scheduler) # Different matrix generation for short vs long video step_matrix, _, step_update_mask, valid_interval = self.generate_timestep_matrix( current_num_latent_frames, timesteps, current_num_latent_frames if is_long_video else base_latent_num_frames, ar_step, prefix_video_latents_frames, causal_block_size, ) # 7. Denoising loop num_warmup_steps = len(timesteps) - num_inference_steps * self.scheduler.order self._num_timesteps = len(step_matrix) with self.progress_bar(total=len(step_matrix)) as progress_bar: for i, t in enumerate(step_matrix): if self.interrupt: continue self._current_timestep = t valid_interval_start, valid_interval_end = valid_interval[i] latent_model_input = ( latents[:, :, valid_interval_start:valid_interval_end, :, :].to(transformer_dtype).clone() ) timestep = t.expand(latents.shape[0], -1)[:, valid_interval_start:valid_interval_end].clone() if addnoise_condition > 0 and valid_interval_start < prefix_video_latents_frames: noise_factor = 0.001 * addnoise_condition latent_model_input[:, :, valid_interval_start:prefix_video_latents_frames, :, :] = ( latent_model_input[:, :, valid_interval_start:prefix_video_latents_frames, :, :] * (1.0 - noise_factor) + torch.randn_like( latent_model_input[:, :, valid_interval_start:prefix_video_latents_frames, :, :] ) * noise_factor ) timestep[:, valid_interval_start:prefix_video_latents_frames] = addnoise_condition with self.transformer.cache_context("cond"): noise_pred = self.transformer( hidden_states=latent_model_input, timestep=timestep, encoder_hidden_states=prompt_embeds, enable_diffusion_forcing=True, fps=fps_embeds, attention_kwargs=attention_kwargs, return_dict=False, )[0] if self.do_classifier_free_guidance: with self.transformer.cache_context("uncond"): noise_uncond = self.transformer( hidden_states=latent_model_input, timestep=timestep, encoder_hidden_states=negative_prompt_embeds, enable_diffusion_forcing=True, fps=fps_embeds, attention_kwargs=attention_kwargs, return_dict=False, )[0] noise_pred = noise_uncond + guidance_scale * (noise_pred - noise_uncond) update_mask_i = step_update_mask[i] for idx in range(valid_interval_start, valid_interval_end): if update_mask_i[idx].item(): latents[:, :, idx, :, :] = sample_schedulers[idx].step( noise_pred[:, :, idx - valid_interval_start, :, :], t[idx], latents[:, :, idx, :, :], return_dict=False, )[0] if callback_on_step_end is not None: callback_kwargs = {} for k in callback_on_step_end_tensor_inputs: callback_kwargs[k] = locals()[k] callback_outputs = callback_on_step_end(self, i, t, callback_kwargs) latents = callback_outputs.pop("latents", latents) prompt_embeds = callback_outputs.pop("prompt_embeds", prompt_embeds) negative_prompt_embeds = callback_outputs.pop("negative_prompt_embeds", negative_prompt_embeds) # call the callback, if provided if i == len(step_matrix) - 1 or ( (i + 1) > num_warmup_steps and (i + 1) % self.scheduler.order == 0 ): progress_bar.update() if XLA_AVAILABLE: xm.mark_step() # Handle latent accumulation for long videos or use the current latents for short videos if is_long_video: if accumulated_latents is None: accumulated_latents = latents else: # Keep overlap frames for conditioning but don't include them in final output accumulated_latents = torch.cat( [accumulated_latents, latents[:, :, overlap_history_latent_frames:]], dim=2 ) if is_long_video: latents = accumulated_latents self._current_timestep = None # Final decoding step - convert latents to pixels if not output_type == "latent": latents = latents.to(self.vae.dtype) latents_mean = ( torch.tensor(self.vae.config.latents_mean) .view(1, self.vae.config.z_dim, 1, 1, 1) .to(latents.device, latents.dtype) ) latents_std = 1.0 / torch.tensor(self.vae.config.latents_std).view(1, self.vae.config.z_dim, 1, 1, 1).to( latents.device, latents.dtype ) latents = latents / latents_std + latents_mean video = self.vae.decode(latents, return_dict=False)[0] video = self.video_processor.postprocess_video(video, output_type=output_type) else: video = latents # Offload all models self.maybe_free_model_hooks() if not return_dict: return (video,) return SkyReelsV2PipelineOutput(frames=video)
{ "repo_id": "huggingface/diffusers", "file_path": "src/diffusers/pipelines/skyreels_v2/pipeline_skyreels_v2_diffusion_forcing.py", "license": "Apache License 2.0", "lines": 853, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
huggingface/diffusers:src/diffusers/pipelines/skyreels_v2/pipeline_skyreels_v2_diffusion_forcing_i2v.py
# Copyright 2025 The SkyReels-V2 Team, The Wan Team and The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import html import math import re from copy import deepcopy from typing import Any, Callable import PIL import torch from transformers import AutoTokenizer, T5EncoderModel, UMT5EncoderModel from diffusers.image_processor import PipelineImageInput from diffusers.utils.torch_utils import randn_tensor from diffusers.video_processor import VideoProcessor from ...callbacks import MultiPipelineCallbacks, PipelineCallback from ...loaders import SkyReelsV2LoraLoaderMixin from ...models import AutoencoderKLWan, SkyReelsV2Transformer3DModel from ...schedulers import UniPCMultistepScheduler from ...utils import is_ftfy_available, is_torch_xla_available, logging, replace_example_docstring from ..pipeline_utils import DiffusionPipeline from .pipeline_output import SkyReelsV2PipelineOutput if is_torch_xla_available(): import torch_xla.core.xla_model as xm XLA_AVAILABLE = True else: XLA_AVAILABLE = False logger = logging.get_logger(__name__) # pylint: disable=invalid-name if is_ftfy_available(): import ftfy EXAMPLE_DOC_STRING = """\ Examples: ```py >>> import torch >>> from diffusers import ( ... SkyReelsV2DiffusionForcingImageToVideoPipeline, ... UniPCMultistepScheduler, ... AutoencoderKLWan, ... ) >>> from diffusers.utils import export_to_video >>> from PIL import Image >>> # Load the pipeline >>> # Available models: >>> # - Skywork/SkyReels-V2-DF-1.3B-540P-Diffusers >>> # - Skywork/SkyReels-V2-DF-14B-540P-Diffusers >>> # - Skywork/SkyReels-V2-DF-14B-720P-Diffusers >>> vae = AutoencoderKLWan.from_pretrained( ... "Skywork/SkyReels-V2-DF-14B-720P-Diffusers", ... subfolder="vae", ... torch_dtype=torch.float32, ... ) >>> pipe = SkyReelsV2DiffusionForcingImageToVideoPipeline.from_pretrained( ... "Skywork/SkyReels-V2-DF-14B-720P-Diffusers", ... vae=vae, ... torch_dtype=torch.bfloat16, ... ) >>> flow_shift = 5.0 # 8.0 for T2V, 5.0 for I2V >>> pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config, flow_shift=flow_shift) >>> pipe = pipe.to("cuda") >>> prompt = "A cat and a dog baking a cake together in a kitchen. The cat is carefully measuring flour, while the dog is stirring the batter with a wooden spoon. The kitchen is cozy, with sunlight streaming through the window." >>> image = Image.open("path/to/image.png") >>> output = pipe( ... image=image, ... prompt=prompt, ... num_inference_steps=50, ... height=544, ... width=960, ... guidance_scale=5.0, # 6.0 for T2V, 5.0 for I2V ... num_frames=97, ... ar_step=0, # Controls asynchronous inference (0 for synchronous mode) ... overlap_history=None, # Number of frames to overlap for smooth transitions in long videos ... addnoise_condition=20, # Improves consistency in long video generation ... ).frames[0] >>> export_to_video(output, "video.mp4", fps=24, quality=8) ``` """ def basic_clean(text): text = ftfy.fix_text(text) text = html.unescape(html.unescape(text)) return text.strip() def whitespace_clean(text): text = re.sub(r"\s+", " ", text) text = text.strip() return text def prompt_clean(text): text = whitespace_clean(basic_clean(text)) return text # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion_img2img.retrieve_latents def retrieve_latents( encoder_output: torch.Tensor, generator: torch.Generator | None = None, sample_mode: str = "sample" ): if hasattr(encoder_output, "latent_dist") and sample_mode == "sample": return encoder_output.latent_dist.sample(generator) elif hasattr(encoder_output, "latent_dist") and sample_mode == "argmax": return encoder_output.latent_dist.mode() elif hasattr(encoder_output, "latents"): return encoder_output.latents else: raise AttributeError("Could not access latents of provided encoder_output") class SkyReelsV2DiffusionForcingImageToVideoPipeline(DiffusionPipeline, SkyReelsV2LoraLoaderMixin): """ Pipeline for Image-to-Video (i2v) generation using SkyReels-V2 with diffusion forcing. This model inherits from [`DiffusionPipeline`]. Check the superclass documentation for the generic methods implemented for all pipelines (downloading, saving, running on a specific device, etc.). Args: tokenizer ([`AutoTokenizer`]): Tokenizer from [T5](https://huggingface.co/docs/transformers/en/model_doc/t5#transformers.T5Tokenizer), specifically the [google/umt5-xxl](https://huggingface.co/google/umt5-xxl) variant. text_encoder ([`UMT5EncoderModel`]): [T5](https://huggingface.co/docs/transformers/en/model_doc/t5#transformers.T5EncoderModel), specifically the [google/umt5-xxl](https://huggingface.co/google/umt5-xxl) variant. transformer ([`SkyReelsV2Transformer3DModel`]): Conditional Transformer to denoise the encoded image latents. scheduler ([`UniPCMultistepScheduler`]): A scheduler to be used in combination with `transformer` to denoise the encoded image latents. vae ([`AutoencoderKLWan`]): Variational Auto-Encoder (VAE) Model to encode and decode videos to and from latent representations. """ model_cpu_offload_seq = "text_encoder->transformer->vae" _callback_tensor_inputs = ["latents", "prompt_embeds", "negative_prompt_embeds"] def __init__( self, tokenizer: AutoTokenizer, text_encoder: T5EncoderModel | UMT5EncoderModel, transformer: SkyReelsV2Transformer3DModel, vae: AutoencoderKLWan, scheduler: UniPCMultistepScheduler, ): super().__init__() self.register_modules( vae=vae, text_encoder=text_encoder, tokenizer=tokenizer, transformer=transformer, scheduler=scheduler, ) self.vae_scale_factor_temporal = 2 ** sum(self.vae.temperal_downsample) if getattr(self, "vae", None) else 4 self.vae_scale_factor_spatial = 2 ** len(self.vae.temperal_downsample) if getattr(self, "vae", None) else 8 self.video_processor = VideoProcessor(vae_scale_factor=self.vae_scale_factor_spatial) # Copied from diffusers.pipelines.wan.pipeline_wan.WanPipeline._get_t5_prompt_embeds def _get_t5_prompt_embeds( self, prompt: str | list[str] = None, num_videos_per_prompt: int = 1, max_sequence_length: int = 226, device: torch.device | None = None, dtype: torch.dtype | None = None, ): device = device or self._execution_device dtype = dtype or self.text_encoder.dtype prompt = [prompt] if isinstance(prompt, str) else prompt prompt = [prompt_clean(u) for u in prompt] batch_size = len(prompt) text_inputs = self.tokenizer( prompt, padding="max_length", max_length=max_sequence_length, truncation=True, add_special_tokens=True, return_attention_mask=True, return_tensors="pt", ) text_input_ids, mask = text_inputs.input_ids, text_inputs.attention_mask seq_lens = mask.gt(0).sum(dim=1).long() prompt_embeds = self.text_encoder(text_input_ids.to(device), mask.to(device)).last_hidden_state prompt_embeds = prompt_embeds.to(dtype=dtype, device=device) prompt_embeds = [u[:v] for u, v in zip(prompt_embeds, seq_lens)] prompt_embeds = torch.stack( [torch.cat([u, u.new_zeros(max_sequence_length - u.size(0), u.size(1))]) for u in prompt_embeds], dim=0 ) # duplicate text embeddings for each generation per prompt, using mps friendly method _, seq_len, _ = prompt_embeds.shape prompt_embeds = prompt_embeds.repeat(1, num_videos_per_prompt, 1) prompt_embeds = prompt_embeds.view(batch_size * num_videos_per_prompt, seq_len, -1) return prompt_embeds # Copied from diffusers.pipelines.wan.pipeline_wan.WanPipeline.encode_prompt def encode_prompt( self, prompt: str | list[str], negative_prompt: str | list[str] | None = None, do_classifier_free_guidance: bool = True, num_videos_per_prompt: int = 1, prompt_embeds: torch.Tensor | None = None, negative_prompt_embeds: torch.Tensor | None = None, max_sequence_length: int = 226, device: torch.device | None = None, dtype: torch.dtype | None = None, ): r""" Encodes the prompt into text encoder hidden states. Args: prompt (`str` or `list[str]`, *optional*): prompt to be encoded negative_prompt (`str` or `list[str]`, *optional*): The prompt or prompts not to guide the image generation. If not defined, one has to pass `negative_prompt_embeds` instead. Ignored when not using guidance (i.e., ignored if `guidance_scale` is less than `1`). do_classifier_free_guidance (`bool`, *optional*, defaults to `True`): Whether to use classifier free guidance or not. num_videos_per_prompt (`int`, *optional*, defaults to 1): Number of videos that should be generated per prompt. torch device to place the resulting embeddings on prompt_embeds (`torch.Tensor`, *optional*): Pre-generated text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt weighting. If not provided, text embeddings will be generated from `prompt` input argument. negative_prompt_embeds (`torch.Tensor`, *optional*): Pre-generated negative text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt weighting. If not provided, negative_prompt_embeds will be generated from `negative_prompt` input argument. device: (`torch.device`, *optional*): torch device dtype: (`torch.dtype`, *optional*): torch dtype """ device = device or self._execution_device prompt = [prompt] if isinstance(prompt, str) else prompt if prompt is not None: batch_size = len(prompt) else: batch_size = prompt_embeds.shape[0] if prompt_embeds is None: prompt_embeds = self._get_t5_prompt_embeds( prompt=prompt, num_videos_per_prompt=num_videos_per_prompt, max_sequence_length=max_sequence_length, device=device, dtype=dtype, ) if do_classifier_free_guidance and negative_prompt_embeds is None: negative_prompt = negative_prompt or "" negative_prompt = batch_size * [negative_prompt] if isinstance(negative_prompt, str) else negative_prompt if prompt is not None and type(prompt) is not type(negative_prompt): raise TypeError( f"`negative_prompt` should be the same type to `prompt`, but got {type(negative_prompt)} !=" f" {type(prompt)}." ) elif batch_size != len(negative_prompt): raise ValueError( f"`negative_prompt`: {negative_prompt} has batch size {len(negative_prompt)}, but `prompt`:" f" {prompt} has batch size {batch_size}. Please make sure that passed `negative_prompt` matches" " the batch size of `prompt`." ) negative_prompt_embeds = self._get_t5_prompt_embeds( prompt=negative_prompt, num_videos_per_prompt=num_videos_per_prompt, max_sequence_length=max_sequence_length, device=device, dtype=dtype, ) return prompt_embeds, negative_prompt_embeds def check_inputs( self, prompt, negative_prompt, image, height, width, prompt_embeds=None, negative_prompt_embeds=None, image_embeds=None, callback_on_step_end_tensor_inputs=None, overlap_history=None, num_frames=None, base_num_frames=None, ): if image is not None and image_embeds is not None: raise ValueError( f"Cannot forward both `image`: {image} and `image_embeds`: {image_embeds}. Please make sure to" " only forward one of the two." ) if image is None and image_embeds is None: raise ValueError( "Provide either `image` or `image_embeds`. Cannot leave both `image` and `image_embeds` undefined." ) if image is not None and not isinstance(image, torch.Tensor) and not isinstance(image, PIL.Image.Image): raise ValueError(f"`image` has to be of type `torch.Tensor` or `PIL.Image.Image` but is {type(image)}") if height % 16 != 0 or width % 16 != 0: raise ValueError(f"`height` and `width` have to be divisible by 16 but are {height} and {width}.") if callback_on_step_end_tensor_inputs is not None and not all( k in self._callback_tensor_inputs for k in callback_on_step_end_tensor_inputs ): raise ValueError( f"`callback_on_step_end_tensor_inputs` has to be in {self._callback_tensor_inputs}, but found {[k for k in callback_on_step_end_tensor_inputs if k not in self._callback_tensor_inputs]}" ) if prompt is not None and prompt_embeds is not None: raise ValueError( f"Cannot forward both `prompt`: {prompt} and `prompt_embeds`: {prompt_embeds}. Please make sure to" " only forward one of the two." ) elif negative_prompt is not None and negative_prompt_embeds is not None: raise ValueError( f"Cannot forward both `negative_prompt`: {negative_prompt} and `negative_prompt_embeds`: {negative_prompt_embeds}. Please make sure to" " only forward one of the two." ) elif prompt is None and prompt_embeds is None: raise ValueError( "Provide either `prompt` or `prompt_embeds`. Cannot leave both `prompt` and `prompt_embeds` undefined." ) elif prompt is not None and (not isinstance(prompt, str) and not isinstance(prompt, list)): raise ValueError(f"`prompt` has to be of type `str` or `list` but is {type(prompt)}") elif negative_prompt is not None and ( not isinstance(negative_prompt, str) and not isinstance(negative_prompt, list) ): raise ValueError(f"`negative_prompt` has to be of type `str` or `list` but is {type(negative_prompt)}") if num_frames > base_num_frames and overlap_history is None: raise ValueError( "`overlap_history` is required when `num_frames` exceeds `base_num_frames` to ensure smooth transitions in long video generation. " "Please specify a value for `overlap_history`. Recommended values are 17 or 37." ) def prepare_latents( self, image: PipelineImageInput | None, batch_size: int, num_channels_latents: int = 16, height: int = 480, width: int = 832, num_frames: int = 97, dtype: torch.dtype | None = None, device: torch.device | None = None, generator: torch.Generator | list[torch.Generator] | None = None, latents: torch.Tensor | None = None, last_image: torch.Tensor | None = None, video_latents: torch.Tensor | None = None, base_latent_num_frames: int | None = None, causal_block_size: int | None = None, overlap_history_latent_frames: int | None = None, long_video_iter: int | None = None, ) -> tuple[torch.Tensor, torch.Tensor]: num_latent_frames = (num_frames - 1) // self.vae_scale_factor_temporal + 1 latent_height = height // self.vae_scale_factor_spatial latent_width = width // self.vae_scale_factor_spatial prefix_video_latents_frames = 0 if video_latents is not None: # long video generation at the iterations other than the first one condition = video_latents[:, :, -overlap_history_latent_frames:] if condition.shape[2] % causal_block_size != 0: truncate_len_latents = condition.shape[2] % causal_block_size logger.warning( f"The length of prefix video latents is truncated by {truncate_len_latents} frames for the causal block size alignment. " f"This truncation ensures compatibility with the causal block size, which is required for proper processing. " f"However, it may slightly affect the continuity of the generated video at the truncation boundary." ) condition = condition[:, :, :-truncate_len_latents] prefix_video_latents_frames = condition.shape[2] finished_frame_num = ( long_video_iter * (base_latent_num_frames - overlap_history_latent_frames) + overlap_history_latent_frames ) left_frame_num = num_latent_frames - finished_frame_num num_latent_frames = min(left_frame_num + overlap_history_latent_frames, base_latent_num_frames) elif base_latent_num_frames is not None: # long video generation at the first iteration num_latent_frames = base_latent_num_frames else: # short video generation num_latent_frames = (num_frames - 1) // self.vae_scale_factor_temporal + 1 shape = (batch_size, num_channels_latents, num_latent_frames, latent_height, latent_width) if isinstance(generator, list) and len(generator) != batch_size: raise ValueError( f"You have passed a list of generators of length {len(generator)}, but requested an effective batch" f" size of {batch_size}. Make sure the batch size matches the length of the generators." ) if latents is None: latents = randn_tensor(shape, generator=generator, device=device, dtype=dtype) else: latents = latents.to(device=device, dtype=dtype) if image is not None: image = image.unsqueeze(2) if last_image is not None: last_image = last_image.unsqueeze(2) video_condition = torch.cat([image, last_image], dim=0) else: video_condition = image video_condition = video_condition.to(device=device, dtype=self.vae.dtype) latents_mean = ( torch.tensor(self.vae.config.latents_mean) .view(1, self.vae.config.z_dim, 1, 1, 1) .to(latents.device, latents.dtype) ) latents_std = 1.0 / torch.tensor(self.vae.config.latents_std).view(1, self.vae.config.z_dim, 1, 1, 1).to( latents.device, latents.dtype ) if isinstance(generator, list): latent_condition = [ retrieve_latents(self.vae.encode(video_condition), sample_mode="argmax") for _ in generator ] latent_condition = torch.cat(latent_condition) else: latent_condition = retrieve_latents(self.vae.encode(video_condition), sample_mode="argmax") latent_condition = latent_condition.repeat_interleave(batch_size, dim=0) latent_condition = latent_condition.to(dtype) condition = (latent_condition - latents_mean) * latents_std prefix_video_latents_frames = condition.shape[2] return latents, num_latent_frames, condition, prefix_video_latents_frames # Copied from diffusers.pipelines.skyreels_v2.pipeline_skyreels_v2_diffusion_forcing.SkyReelsV2DiffusionForcingPipeline.generate_timestep_matrix def generate_timestep_matrix( self, num_latent_frames: int, step_template: torch.Tensor, base_num_latent_frames: int, ar_step: int = 5, num_pre_ready: int = 0, causal_block_size: int = 1, shrink_interval_with_mask: bool = False, ) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor, list[tuple]]: """ This function implements the core diffusion forcing algorithm that creates a coordinated denoising schedule across temporal frames. It supports both synchronous and asynchronous generation modes: **Synchronous Mode** (ar_step=0, causal_block_size=1): - All frames are denoised simultaneously at each timestep - Each frame follows the same denoising trajectory: [1000, 800, 600, ..., 0] - Simpler but may have less temporal consistency for long videos **Asynchronous Mode** (ar_step>0, causal_block_size>1): - Frames are grouped into causal blocks and processed block/chunk-wise - Each block is denoised in a staggered pattern creating a "denoising wave" - Earlier blocks are more denoised, later blocks lag behind by ar_step timesteps - Creates stronger temporal dependencies and better consistency Args: num_latent_frames (int): Total number of latent frames to generate step_template (torch.Tensor): Base timestep schedule (e.g., [1000, 800, 600, ..., 0]) base_num_latent_frames (int): Maximum frames the model can process in one forward pass ar_step (int, optional): Autoregressive step size for temporal lag. 0 = synchronous, >0 = asynchronous. Defaults to 5. num_pre_ready (int, optional): Number of frames already denoised (e.g., from prefix in a video2video task). Defaults to 0. causal_block_size (int, optional): Number of frames processed as a causal block. Defaults to 1. shrink_interval_with_mask (bool, optional): Whether to optimize processing intervals. Defaults to False. Returns: tuple containing: - step_matrix (torch.Tensor): Matrix of timesteps for each frame at each iteration Shape: [num_iterations, num_latent_frames] - step_index (torch.Tensor): Index matrix for timestep lookup Shape: [num_iterations, num_latent_frames] - step_update_mask (torch.Tensor): Boolean mask indicating which frames to update Shape: [num_iterations, num_latent_frames] - valid_interval (list[tuple]): list of (start, end) intervals for each iteration Raises: ValueError: If ar_step is too small for the given configuration """ # Initialize lists to store the scheduling matrices and metadata step_matrix, step_index = [], [] # Will store timestep values and indices for each iteration update_mask, valid_interval = [], [] # Will store update masks and processing intervals # Calculate total number of denoising iterations (add 1 for initial noise state) num_iterations = len(step_template) + 1 # Convert frame counts to block counts for causal processing # Each block contains causal_block_size frames that are processed together # E.g.: 25 frames ÷ 5 = 5 blocks total num_blocks = num_latent_frames // causal_block_size base_num_blocks = base_num_latent_frames // causal_block_size # Validate ar_step is sufficient for the given configuration # In asynchronous mode, we need enough timesteps to create the staggered pattern if base_num_blocks < num_blocks: min_ar_step = len(step_template) / base_num_blocks if ar_step < min_ar_step: raise ValueError(f"`ar_step` should be at least {math.ceil(min_ar_step)} in your setting") # Extend step_template with boundary values for easier indexing # 999: dummy value for counter starting from 1 # 0: final timestep (completely denoised) step_template = torch.cat( [ torch.tensor([999], dtype=torch.int64, device=step_template.device), step_template.long(), torch.tensor([0], dtype=torch.int64, device=step_template.device), ] ) # Initialize the previous row state (tracks denoising progress for each block) # 0 means not started, num_iterations means fully denoised pre_row = torch.zeros(num_blocks, dtype=torch.long) # Mark pre-ready frames (e.g., from prefix video for a video2video task) as already at final denoising state if num_pre_ready > 0: pre_row[: num_pre_ready // causal_block_size] = num_iterations # Main loop: Generate denoising schedule until all frames are fully denoised while not torch.all(pre_row >= (num_iterations - 1)): # Create new row representing the next denoising step new_row = torch.zeros(num_blocks, dtype=torch.long) # Apply diffusion forcing logic for each block for i in range(num_blocks): if i == 0 or pre_row[i - 1] >= ( num_iterations - 1 ): # the first frame or the last frame is completely denoised new_row[i] = pre_row[i] + 1 else: # Asynchronous mode: lag behind previous block by ar_step timesteps # This creates the "diffusion forcing" staggered pattern new_row[i] = new_row[i - 1] - ar_step # Clamp values to valid range [0, num_iterations] new_row = new_row.clamp(0, num_iterations) # Create update mask: True for blocks that need denoising update at this iteration # Exclude blocks that haven't started (new_row != pre_row) or are finished (new_row != num_iterations) # Final state example: [False, ..., False, True, True, True, True, True] # where first 20 frames are done (False) and last 5 frames still need updates (True) update_mask.append((new_row != pre_row) & (new_row != num_iterations)) # Store the iteration state step_index.append(new_row) # Index into step_template step_matrix.append(step_template[new_row]) # Actual timestep values pre_row = new_row # Update for next iteration # For videos longer than model capacity, we process in sliding windows terminal_flag = base_num_blocks # Optional optimization: shrink interval based on first update mask if shrink_interval_with_mask: idx_sequence = torch.arange(num_blocks, dtype=torch.int64) update_mask = update_mask[0] update_mask_idx = idx_sequence[update_mask] last_update_idx = update_mask_idx[-1].item() terminal_flag = last_update_idx + 1 # Each interval defines which frames to process in the current forward pass for curr_mask in update_mask: # Extend terminal flag if current mask has updates beyond current terminal if terminal_flag < num_blocks and curr_mask[terminal_flag]: terminal_flag += 1 # Create interval: [start, end) where start ensures we don't exceed model capacity valid_interval.append((max(terminal_flag - base_num_blocks, 0), terminal_flag)) # Convert lists to tensors for efficient processing step_update_mask = torch.stack(update_mask, dim=0) step_index = torch.stack(step_index, dim=0) step_matrix = torch.stack(step_matrix, dim=0) # Each block's schedule is replicated to all frames within that block if causal_block_size > 1: # Expand each block to causal_block_size frames step_update_mask = step_update_mask.unsqueeze(-1).repeat(1, 1, causal_block_size).flatten(1).contiguous() step_index = step_index.unsqueeze(-1).repeat(1, 1, causal_block_size).flatten(1).contiguous() step_matrix = step_matrix.unsqueeze(-1).repeat(1, 1, causal_block_size).flatten(1).contiguous() # Scale intervals from block-level to frame-level valid_interval = [(s * causal_block_size, e * causal_block_size) for s, e in valid_interval] return step_matrix, step_index, step_update_mask, valid_interval @property def guidance_scale(self): return self._guidance_scale @property def do_classifier_free_guidance(self): return self._guidance_scale > 1.0 @property def num_timesteps(self): return self._num_timesteps @property def current_timestep(self): return self._current_timestep @property def interrupt(self): return self._interrupt @property def attention_kwargs(self): return self._attention_kwargs @torch.no_grad() @replace_example_docstring(EXAMPLE_DOC_STRING) def __call__( self, image: PipelineImageInput, prompt: str | list[str] = None, negative_prompt: str | list[str] = None, height: int = 544, width: int = 960, num_frames: int = 97, num_inference_steps: int = 50, guidance_scale: float = 5.0, num_videos_per_prompt: int | None = 1, generator: torch.Generator | list[torch.Generator] | None = None, latents: torch.Tensor | None = None, prompt_embeds: torch.Tensor | None = None, negative_prompt_embeds: torch.Tensor | None = None, image_embeds: torch.Tensor | None = None, last_image: torch.Tensor | None = None, output_type: str | None = "np", return_dict: bool = True, attention_kwargs: dict[str, Any] | None = None, callback_on_step_end: Callable[[int, int], None] | PipelineCallback | MultiPipelineCallbacks | None = None, callback_on_step_end_tensor_inputs: list[str] = ["latents"], max_sequence_length: int = 512, overlap_history: int | None = None, addnoise_condition: float = 0, base_num_frames: int = 97, ar_step: int = 0, causal_block_size: int | None = None, fps: int = 24, ): r""" The call function to the pipeline for generation. Args: image (`PipelineImageInput`): The input image to condition the generation on. Must be an image, a list of images or a `torch.Tensor`. prompt (`str` or `list[str]`, *optional*): The prompt or prompts to guide the image generation. If not defined, one has to pass `prompt_embeds`. instead. negative_prompt (`str` or `list[str]`, *optional*): The prompt or prompts not to guide the image generation. If not defined, one has to pass `negative_prompt_embeds` instead. Ignored when not using guidance (i.e., ignored if `guidance_scale` is less than `1`). height (`int`, defaults to `544`): The height of the generated video. width (`int`, defaults to `960`): The width of the generated video. num_frames (`int`, defaults to `97`): The number of frames in the generated video. num_inference_steps (`int`, defaults to `50`): The number of denoising steps. More denoising steps usually lead to a higher quality image at the expense of slower inference. guidance_scale (`float`, defaults to `5.0`): Guidance scale as defined in [Classifier-Free Diffusion Guidance](https://huggingface.co/papers/2207.12598). `guidance_scale` is defined as `w` of equation 2. of [Imagen Paper](https://huggingface.co/papers/2205.11487). Guidance scale is enabled by setting `guidance_scale > 1`. Higher guidance scale encourages to generate images that are closely linked to the text `prompt`, usually at the expense of lower image quality. (**6.0 for T2V**, **5.0 for I2V**) num_videos_per_prompt (`int`, *optional*, defaults to 1): The number of images to generate per prompt. generator (`torch.Generator` or `list[torch.Generator]`, *optional*): A [`torch.Generator`](https://pytorch.org/docs/stable/generated/torch.Generator.html) to make generation deterministic. latents (`torch.Tensor`, *optional*): Pre-generated noisy latents sampled from a Gaussian distribution, to be used as inputs for image generation. Can be used to tweak the same generation with different prompts. If not provided, a latents tensor is generated by sampling using the supplied random `generator`. prompt_embeds (`torch.Tensor`, *optional*): Pre-generated text embeddings. Can be used to easily tweak text inputs (prompt weighting). If not provided, text embeddings are generated from the `prompt` input argument. negative_prompt_embeds (`torch.Tensor`, *optional*): Pre-generated text embeddings. Can be used to easily tweak text inputs (prompt weighting). If not provided, text embeddings are generated from the `negative_prompt` input argument. image_embeds (`torch.Tensor`, *optional*): Pre-generated image embeddings. Can be used to easily tweak image inputs (weighting). If not provided, image embeddings are generated from the `image` input argument. last_image (`torch.Tensor`, *optional*): Pre-generated image embeddings. Can be used to easily tweak image inputs (weighting). If not provided, image embeddings are generated from the `image` input argument. output_type (`str`, *optional*, defaults to `"np"`): The output format of the generated image. Choose between `PIL.Image` or `np.array`. return_dict (`bool`, *optional*, defaults to `True`): Whether or not to return a [`SkyReelsV2PipelineOutput`] instead of a plain tuple. attention_kwargs (`dict`, *optional*): A kwargs dictionary that if specified is passed along to the `AttentionProcessor` as defined under `self.processor` in [diffusers.models.attention_processor](https://github.com/huggingface/diffusers/blob/main/src/diffusers/models/attention_processor.py). callback_on_step_end (`Callable`, `PipelineCallback`, `MultiPipelineCallbacks`, *optional*): A function or a subclass of `PipelineCallback` or `MultiPipelineCallbacks` that is called at the end of each denoising step during the inference. with the following arguments: `callback_on_step_end(self: DiffusionPipeline, step: int, timestep: int, callback_kwargs: Dict)`. `callback_kwargs` will include a list of all tensors as specified by `callback_on_step_end_tensor_inputs`. callback_on_step_end_tensor_inputs (`list`, *optional*): The list of tensor inputs for the `callback_on_step_end` function. The tensors specified in the list will be passed as `callback_kwargs` argument. You will only be able to include variables listed in the `._callback_tensor_inputs` attribute of your pipeline class. max_sequence_length (`int`, *optional*, defaults to `512`): The maximum sequence length of the prompt. overlap_history (`int`, *optional*, defaults to `None`): Number of frames to overlap for smooth transitions in long videos. If `None`, the pipeline assumes short video generation mode, and no overlap is applied. 17 and 37 are recommended to set. addnoise_condition (`float`, *optional*, defaults to `0`): This is used to help smooth the long video generation by adding some noise to the clean condition. Too large noise can cause the inconsistency as well. 20 is a recommended value, and you may try larger ones, but it is recommended to not exceed 50. base_num_frames (`int`, *optional*, defaults to `97`): 97 or 121 | Base frame count (**97 for 540P**, **121 for 720P**) ar_step (`int`, *optional*, defaults to `0`): Controls asynchronous inference (0 for synchronous mode) You can set `ar_step=5` to enable asynchronous inference. When asynchronous inference, `causal_block_size=5` is recommended while it is not supposed to be set for synchronous generation. Asynchronous inference will take more steps to diffuse the whole sequence which means it will be SLOWER than synchronous mode. In our experiments, asynchronous inference may improve the instruction following and visual consistent performance. causal_block_size (`int`, *optional*, defaults to `None`): The number of frames in each block/chunk. Recommended when using asynchronous inference (when ar_step > 0) fps (`int`, *optional*, defaults to `24`): Frame rate of the generated video Examples: Returns: [`~SkyReelsV2PipelineOutput`] or `tuple`: If `return_dict` is `True`, [`SkyReelsV2PipelineOutput`] is returned, otherwise a `tuple` is returned where the first element is a list with the generated images and the second element is a list of `bool`s indicating whether the corresponding generated image contains "not-safe-for-work" (nsfw) content. """ if isinstance(callback_on_step_end, (PipelineCallback, MultiPipelineCallbacks)): callback_on_step_end_tensor_inputs = callback_on_step_end.tensor_inputs # 1. Check inputs. Raise error if not correct self.check_inputs( prompt, negative_prompt, image, height, width, prompt_embeds, negative_prompt_embeds, image_embeds, callback_on_step_end_tensor_inputs, overlap_history, num_frames, base_num_frames, ) if addnoise_condition > 60: logger.warning( f"The value of 'addnoise_condition' is too large ({addnoise_condition}) and may cause inconsistencies in long video generation. A value of 20 is recommended." ) if num_frames % self.vae_scale_factor_temporal != 1: logger.warning( f"`num_frames - 1` has to be divisible by {self.vae_scale_factor_temporal}. Rounding to the nearest number." ) num_frames = num_frames // self.vae_scale_factor_temporal * self.vae_scale_factor_temporal + 1 num_frames = max(num_frames, 1) self._guidance_scale = guidance_scale self._attention_kwargs = attention_kwargs self._current_timestep = None self._interrupt = False device = self._execution_device # 2. Define call parameters if prompt is not None and isinstance(prompt, str): batch_size = 1 elif prompt is not None and isinstance(prompt, list): batch_size = len(prompt) else: batch_size = prompt_embeds.shape[0] # 3. Encode input prompt prompt_embeds, negative_prompt_embeds = self.encode_prompt( prompt=prompt, negative_prompt=negative_prompt, do_classifier_free_guidance=self.do_classifier_free_guidance, num_videos_per_prompt=num_videos_per_prompt, prompt_embeds=prompt_embeds, negative_prompt_embeds=negative_prompt_embeds, max_sequence_length=max_sequence_length, device=device, ) transformer_dtype = self.transformer.dtype prompt_embeds = prompt_embeds.to(transformer_dtype) if negative_prompt_embeds is not None: negative_prompt_embeds = negative_prompt_embeds.to(transformer_dtype) # 4. Prepare timesteps self.scheduler.set_timesteps(num_inference_steps, device=device) timesteps = self.scheduler.timesteps if causal_block_size is None: causal_block_size = self.transformer.config.num_frame_per_block else: self.transformer._set_ar_attention(causal_block_size) fps_embeds = [fps] * prompt_embeds.shape[0] fps_embeds = [0 if i == 16 else 1 for i in fps_embeds] # Determine if we're doing long video generation is_long_video = overlap_history is not None and base_num_frames is not None and num_frames > base_num_frames # Initialize accumulated_latents to store all latents in one tensor accumulated_latents = None if is_long_video: # Long video generation setup overlap_history_latent_frames = (overlap_history - 1) // self.vae_scale_factor_temporal + 1 num_latent_frames = (num_frames - 1) // self.vae_scale_factor_temporal + 1 base_latent_num_frames = ( (base_num_frames - 1) // self.vae_scale_factor_temporal + 1 if base_num_frames is not None else num_latent_frames ) n_iter = ( 1 + (num_latent_frames - base_latent_num_frames - 1) // (base_latent_num_frames - overlap_history_latent_frames) + 1 ) else: # Short video generation setup n_iter = 1 base_latent_num_frames = (num_frames - 1) // self.vae_scale_factor_temporal + 1 image = self.video_processor.preprocess(image, height=height, width=width).to(device, dtype=torch.float32) if last_image is not None: last_image = self.video_processor.preprocess(last_image, height=height, width=width).to( device, dtype=torch.float32 ) # Loop through iterations (multiple iterations only for long videos) for iter_idx in range(n_iter): if is_long_video: logger.debug(f"Processing iteration {iter_idx + 1}/{n_iter} for long video generation...") num_channels_latents = self.vae.config.z_dim latents, current_num_latent_frames, condition, prefix_video_latents_frames = self.prepare_latents( image if iter_idx == 0 else None, batch_size * num_videos_per_prompt, num_channels_latents, height, width, num_frames, torch.float32, device, generator, latents if iter_idx == 0 else None, last_image, video_latents=accumulated_latents, # Pass latents directly instead of decoded video base_latent_num_frames=base_latent_num_frames if is_long_video else None, causal_block_size=causal_block_size, overlap_history_latent_frames=overlap_history_latent_frames if is_long_video else None, long_video_iter=iter_idx if is_long_video else None, ) if iter_idx == 0: latents[:, :, :prefix_video_latents_frames, :, :] = condition[: (condition.shape[0] + 1) // 2].to( transformer_dtype ) else: latents[:, :, :prefix_video_latents_frames, :, :] = condition.to(transformer_dtype) if iter_idx == 0 and last_image is not None: end_video_latents = condition[condition.shape[0] // 2 :].to(transformer_dtype) if last_image is not None and iter_idx + 1 == n_iter: latents = torch.cat([latents, end_video_latents], dim=2) base_latent_num_frames += prefix_video_latents_frames current_num_latent_frames += prefix_video_latents_frames # 4. Prepare sample schedulers and timestep matrix sample_schedulers = [] for _ in range(current_num_latent_frames): sample_scheduler = deepcopy(self.scheduler) sample_scheduler.set_timesteps(num_inference_steps, device=device) sample_schedulers.append(sample_scheduler) step_matrix, _, step_update_mask, valid_interval = self.generate_timestep_matrix( current_num_latent_frames, timesteps, base_latent_num_frames, ar_step, prefix_video_latents_frames, causal_block_size, ) if last_image is not None and iter_idx + 1 == n_iter: step_matrix[:, -prefix_video_latents_frames:] = 0 step_update_mask[:, -prefix_video_latents_frames:] = False # 6. Denoising loop num_warmup_steps = len(timesteps) - num_inference_steps * self.scheduler.order self._num_timesteps = len(step_matrix) with self.progress_bar(total=len(step_matrix)) as progress_bar: for i, t in enumerate(step_matrix): if self.interrupt: continue self._current_timestep = t valid_interval_start, valid_interval_end = valid_interval[i] latent_model_input = ( latents[:, :, valid_interval_start:valid_interval_end, :, :].to(transformer_dtype).clone() ) timestep = t.expand(latents.shape[0], -1)[:, valid_interval_start:valid_interval_end].clone() if addnoise_condition > 0 and valid_interval_start < prefix_video_latents_frames: noise_factor = 0.001 * addnoise_condition latent_model_input[:, :, valid_interval_start:prefix_video_latents_frames, :, :] = ( latent_model_input[:, :, valid_interval_start:prefix_video_latents_frames, :, :] * (1.0 - noise_factor) + torch.randn_like( latent_model_input[:, :, valid_interval_start:prefix_video_latents_frames, :, :] ) * noise_factor ) timestep[:, valid_interval_start:prefix_video_latents_frames] = addnoise_condition with self.transformer.cache_context("cond"): noise_pred = self.transformer( hidden_states=latent_model_input, timestep=timestep, encoder_hidden_states=prompt_embeds, enable_diffusion_forcing=True, fps=fps_embeds, attention_kwargs=attention_kwargs, return_dict=False, )[0] if self.do_classifier_free_guidance: with self.transformer.cache_context("uncond"): noise_uncond = self.transformer( hidden_states=latent_model_input, timestep=timestep, encoder_hidden_states=negative_prompt_embeds, enable_diffusion_forcing=True, fps=fps_embeds, attention_kwargs=attention_kwargs, return_dict=False, )[0] noise_pred = noise_uncond + guidance_scale * (noise_pred - noise_uncond) update_mask_i = step_update_mask[i] for idx in range(valid_interval_start, valid_interval_end): if update_mask_i[idx].item(): latents[:, :, idx, :, :] = sample_schedulers[idx].step( noise_pred[:, :, idx - valid_interval_start, :, :], t[idx], latents[:, :, idx, :, :], return_dict=False, )[0] if callback_on_step_end is not None: callback_kwargs = {} for k in callback_on_step_end_tensor_inputs: callback_kwargs[k] = locals()[k] callback_outputs = callback_on_step_end(self, i, t, callback_kwargs) latents = callback_outputs.pop("latents", latents) prompt_embeds = callback_outputs.pop("prompt_embeds", prompt_embeds) negative_prompt_embeds = callback_outputs.pop("negative_prompt_embeds", negative_prompt_embeds) # call the callback, if provided if i == len(step_matrix) - 1 or ( (i + 1) > num_warmup_steps and (i + 1) % self.scheduler.order == 0 ): progress_bar.update() if XLA_AVAILABLE: xm.mark_step() # Handle latent accumulation for long videos or use the current latents for short videos if is_long_video: if accumulated_latents is None: accumulated_latents = latents else: # Keep overlap frames for conditioning but don't include them in final output accumulated_latents = torch.cat( [accumulated_latents, latents[:, :, overlap_history_latent_frames:]], dim=2, ) if is_long_video: latents = accumulated_latents self._current_timestep = None # Final decoding step - convert latents to pixels if not output_type == "latent": if last_image is not None: latents = latents[:, :, :-prefix_video_latents_frames, :, :].to(self.vae.dtype) latents_mean = ( torch.tensor(self.vae.config.latents_mean) .view(1, self.vae.config.z_dim, 1, 1, 1) .to(latents.device, latents.dtype) ) latents_std = 1.0 / torch.tensor(self.vae.config.latents_std).view(1, self.vae.config.z_dim, 1, 1, 1).to( latents.device, latents.dtype ) latents = latents / latents_std + latents_mean video = self.vae.decode(latents, return_dict=False)[0] video = self.video_processor.postprocess_video(video, output_type=output_type) else: video = latents # Offload all models self.maybe_free_model_hooks() if not return_dict: return (video,) return SkyReelsV2PipelineOutput(frames=video)
{ "repo_id": "huggingface/diffusers", "file_path": "src/diffusers/pipelines/skyreels_v2/pipeline_skyreels_v2_diffusion_forcing_i2v.py", "license": "Apache License 2.0", "lines": 925, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
huggingface/diffusers:src/diffusers/pipelines/skyreels_v2/pipeline_skyreels_v2_diffusion_forcing_v2v.py
# Copyright 2025 The SkyReels-V2 Team, The Wan Team and The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import html import inspect import math import re from copy import deepcopy from typing import Any, Callable import torch from PIL import Image from transformers import AutoTokenizer, T5EncoderModel, UMT5EncoderModel from ...callbacks import MultiPipelineCallbacks, PipelineCallback from ...loaders import SkyReelsV2LoraLoaderMixin from ...models import AutoencoderKLWan, SkyReelsV2Transformer3DModel from ...schedulers import UniPCMultistepScheduler from ...utils import is_ftfy_available, is_torch_xla_available, logging, replace_example_docstring from ...utils.torch_utils import randn_tensor from ...video_processor import VideoProcessor from ..pipeline_utils import DiffusionPipeline from .pipeline_output import SkyReelsV2PipelineOutput if is_torch_xla_available(): import torch_xla.core.xla_model as xm XLA_AVAILABLE = True else: XLA_AVAILABLE = False logger = logging.get_logger(__name__) # pylint: disable=invalid-name if is_ftfy_available(): import ftfy EXAMPLE_DOC_STRING = """\ Examples: ```py >>> import torch >>> from diffusers import ( ... SkyReelsV2DiffusionForcingVideoToVideoPipeline, ... UniPCMultistepScheduler, ... AutoencoderKLWan, ... ) >>> from diffusers.utils import export_to_video >>> # Load the pipeline >>> # Available models: >>> # - Skywork/SkyReels-V2-DF-1.3B-540P-Diffusers >>> # - Skywork/SkyReels-V2-DF-14B-540P-Diffusers >>> # - Skywork/SkyReels-V2-DF-14B-720P-Diffusers >>> vae = AutoencoderKLWan.from_pretrained( ... "Skywork/SkyReels-V2-DF-14B-720P-Diffusers", ... subfolder="vae", ... torch_dtype=torch.float32, ... ) >>> pipe = SkyReelsV2DiffusionForcingVideoToVideoPipeline.from_pretrained( ... "Skywork/SkyReels-V2-DF-14B-720P-Diffusers", ... vae=vae, ... torch_dtype=torch.bfloat16, ... ) >>> flow_shift = 8.0 # 8.0 for T2V, 5.0 for I2V >>> pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config, flow_shift=flow_shift) >>> pipe = pipe.to("cuda") >>> prompt = "A cat and a dog baking a cake together in a kitchen. The cat is carefully measuring flour, while the dog is stirring the batter with a wooden spoon. The kitchen is cozy, with sunlight streaming through the window." >>> output = pipe( ... prompt=prompt, ... num_inference_steps=50, ... height=544, ... width=960, ... guidance_scale=6.0, # 6.0 for T2V, 5.0 for I2V ... num_frames=97, ... ar_step=0, # Controls asynchronous inference (0 for synchronous mode) ... overlap_history=None, # Number of frames to overlap for smooth transitions in long videos ... addnoise_condition=20, # Improves consistency in long video generation ... ).frames[0] >>> export_to_video(output, "video.mp4", fps=24, quality=8) ``` """ def basic_clean(text): text = ftfy.fix_text(text) text = html.unescape(html.unescape(text)) return text.strip() def whitespace_clean(text): text = re.sub(r"\s+", " ", text) text = text.strip() return text def prompt_clean(text): text = whitespace_clean(basic_clean(text)) return text # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.retrieve_timesteps def retrieve_timesteps( scheduler, num_inference_steps: int | None = None, device: str | torch.device | None = None, timesteps: list[int] | None = None, sigmas: list[float] | None = None, **kwargs, ): r""" Calls the scheduler's `set_timesteps` method and retrieves timesteps from the scheduler after the call. Handles custom timesteps. Any kwargs will be supplied to `scheduler.set_timesteps`. Args: scheduler (`SchedulerMixin`): The scheduler to get timesteps from. num_inference_steps (`int`): The number of diffusion steps used when generating samples with a pre-trained model. If used, `timesteps` must be `None`. device (`str` or `torch.device`, *optional*): The device to which the timesteps should be moved to. If `None`, the timesteps are not moved. timesteps (`list[int]`, *optional*): Custom timesteps used to override the timestep spacing strategy of the scheduler. If `timesteps` is passed, `num_inference_steps` and `sigmas` must be `None`. sigmas (`list[float]`, *optional*): Custom sigmas used to override the timestep spacing strategy of the scheduler. If `sigmas` is passed, `num_inference_steps` and `timesteps` must be `None`. Returns: `tuple[torch.Tensor, int]`: A tuple where the first element is the timestep schedule from the scheduler and the second element is the number of inference steps. """ if timesteps is not None and sigmas is not None: raise ValueError("Only one of `timesteps` or `sigmas` can be passed. Please choose one to set custom values") if timesteps is not None: accepts_timesteps = "timesteps" in set(inspect.signature(scheduler.set_timesteps).parameters.keys()) if not accepts_timesteps: raise ValueError( f"The current scheduler class {scheduler.__class__}'s `set_timesteps` does not support custom" f" timestep schedules. Please check whether you are using the correct scheduler." ) scheduler.set_timesteps(timesteps=timesteps, device=device, **kwargs) timesteps = scheduler.timesteps num_inference_steps = len(timesteps) elif sigmas is not None: accept_sigmas = "sigmas" in set(inspect.signature(scheduler.set_timesteps).parameters.keys()) if not accept_sigmas: raise ValueError( f"The current scheduler class {scheduler.__class__}'s `set_timesteps` does not support custom" f" sigmas schedules. Please check whether you are using the correct scheduler." ) scheduler.set_timesteps(sigmas=sigmas, device=device, **kwargs) timesteps = scheduler.timesteps num_inference_steps = len(timesteps) else: scheduler.set_timesteps(num_inference_steps, device=device, **kwargs) timesteps = scheduler.timesteps return timesteps, num_inference_steps # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion_img2img.retrieve_latents def retrieve_latents( encoder_output: torch.Tensor, generator: torch.Generator | None = None, sample_mode: str = "sample" ): if hasattr(encoder_output, "latent_dist") and sample_mode == "sample": return encoder_output.latent_dist.sample(generator) elif hasattr(encoder_output, "latent_dist") and sample_mode == "argmax": return encoder_output.latent_dist.mode() elif hasattr(encoder_output, "latents"): return encoder_output.latents else: raise AttributeError("Could not access latents of provided encoder_output") class SkyReelsV2DiffusionForcingVideoToVideoPipeline(DiffusionPipeline, SkyReelsV2LoraLoaderMixin): """ Pipeline for Video-to-Video (v2v) generation using SkyReels-V2 with diffusion forcing. This model inherits from [`DiffusionPipeline`]. Check the superclass documentation for the generic methods implemented for all pipelines (downloading, saving, running on a specific device, etc.). Args: tokenizer ([`AutoTokenizer`]): Tokenizer from [T5](https://huggingface.co/docs/transformers/en/model_doc/t5#transformers.T5Tokenizer), specifically the [google/umt5-xxl](https://huggingface.co/google/umt5-xxl) variant. text_encoder ([`UMT5EncoderModel`]): [T5](https://huggingface.co/docs/transformers/en/model_doc/t5#transformers.T5EncoderModel), specifically the [google/umt5-xxl](https://huggingface.co/google/umt5-xxl) variant. transformer ([`SkyReelsV2Transformer3DModel`]): Conditional Transformer to denoise the encoded image latents. scheduler ([`UniPCMultistepScheduler`]): A scheduler to be used in combination with `transformer` to denoise the encoded image latents. vae ([`AutoencoderKLWan`]): Variational Auto-Encoder (VAE) Model to encode and decode videos to and from latent representations. """ model_cpu_offload_seq = "text_encoder->transformer->vae" _callback_tensor_inputs = ["latents", "prompt_embeds", "negative_prompt_embeds"] def __init__( self, tokenizer: AutoTokenizer, text_encoder: T5EncoderModel | UMT5EncoderModel, transformer: SkyReelsV2Transformer3DModel, vae: AutoencoderKLWan, scheduler: UniPCMultistepScheduler, ): super().__init__() self.register_modules( vae=vae, text_encoder=text_encoder, tokenizer=tokenizer, transformer=transformer, scheduler=scheduler, ) self.vae_scale_factor_temporal = 2 ** sum(self.vae.temperal_downsample) if getattr(self, "vae", None) else 4 self.vae_scale_factor_spatial = 2 ** len(self.vae.temperal_downsample) if getattr(self, "vae", None) else 8 self.video_processor = VideoProcessor(vae_scale_factor=self.vae_scale_factor_spatial) # Copied from diffusers.pipelines.wan.pipeline_wan.WanPipeline._get_t5_prompt_embeds def _get_t5_prompt_embeds( self, prompt: str | list[str] = None, num_videos_per_prompt: int = 1, max_sequence_length: int = 226, device: torch.device | None = None, dtype: torch.dtype | None = None, ): device = device or self._execution_device dtype = dtype or self.text_encoder.dtype prompt = [prompt] if isinstance(prompt, str) else prompt prompt = [prompt_clean(u) for u in prompt] batch_size = len(prompt) text_inputs = self.tokenizer( prompt, padding="max_length", max_length=max_sequence_length, truncation=True, add_special_tokens=True, return_attention_mask=True, return_tensors="pt", ) text_input_ids, mask = text_inputs.input_ids, text_inputs.attention_mask seq_lens = mask.gt(0).sum(dim=1).long() prompt_embeds = self.text_encoder(text_input_ids.to(device), mask.to(device)).last_hidden_state prompt_embeds = prompt_embeds.to(dtype=dtype, device=device) prompt_embeds = [u[:v] for u, v in zip(prompt_embeds, seq_lens)] prompt_embeds = torch.stack( [torch.cat([u, u.new_zeros(max_sequence_length - u.size(0), u.size(1))]) for u in prompt_embeds], dim=0 ) # duplicate text embeddings for each generation per prompt, using mps friendly method _, seq_len, _ = prompt_embeds.shape prompt_embeds = prompt_embeds.repeat(1, num_videos_per_prompt, 1) prompt_embeds = prompt_embeds.view(batch_size * num_videos_per_prompt, seq_len, -1) return prompt_embeds # Copied from diffusers.pipelines.wan.pipeline_wan.WanPipeline.encode_prompt def encode_prompt( self, prompt: str | list[str], negative_prompt: str | list[str] | None = None, do_classifier_free_guidance: bool = True, num_videos_per_prompt: int = 1, prompt_embeds: torch.Tensor | None = None, negative_prompt_embeds: torch.Tensor | None = None, max_sequence_length: int = 226, device: torch.device | None = None, dtype: torch.dtype | None = None, ): r""" Encodes the prompt into text encoder hidden states. Args: prompt (`str` or `list[str]`, *optional*): prompt to be encoded negative_prompt (`str` or `list[str]`, *optional*): The prompt or prompts not to guide the image generation. If not defined, one has to pass `negative_prompt_embeds` instead. Ignored when not using guidance (i.e., ignored if `guidance_scale` is less than `1`). do_classifier_free_guidance (`bool`, *optional*, defaults to `True`): Whether to use classifier free guidance or not. num_videos_per_prompt (`int`, *optional*, defaults to 1): Number of videos that should be generated per prompt. torch device to place the resulting embeddings on prompt_embeds (`torch.Tensor`, *optional*): Pre-generated text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt weighting. If not provided, text embeddings will be generated from `prompt` input argument. negative_prompt_embeds (`torch.Tensor`, *optional*): Pre-generated negative text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt weighting. If not provided, negative_prompt_embeds will be generated from `negative_prompt` input argument. device: (`torch.device`, *optional*): torch device dtype: (`torch.dtype`, *optional*): torch dtype """ device = device or self._execution_device prompt = [prompt] if isinstance(prompt, str) else prompt if prompt is not None: batch_size = len(prompt) else: batch_size = prompt_embeds.shape[0] if prompt_embeds is None: prompt_embeds = self._get_t5_prompt_embeds( prompt=prompt, num_videos_per_prompt=num_videos_per_prompt, max_sequence_length=max_sequence_length, device=device, dtype=dtype, ) if do_classifier_free_guidance and negative_prompt_embeds is None: negative_prompt = negative_prompt or "" negative_prompt = batch_size * [negative_prompt] if isinstance(negative_prompt, str) else negative_prompt if prompt is not None and type(prompt) is not type(negative_prompt): raise TypeError( f"`negative_prompt` should be the same type to `prompt`, but got {type(negative_prompt)} !=" f" {type(prompt)}." ) elif batch_size != len(negative_prompt): raise ValueError( f"`negative_prompt`: {negative_prompt} has batch size {len(negative_prompt)}, but `prompt`:" f" {prompt} has batch size {batch_size}. Please make sure that passed `negative_prompt` matches" " the batch size of `prompt`." ) negative_prompt_embeds = self._get_t5_prompt_embeds( prompt=negative_prompt, num_videos_per_prompt=num_videos_per_prompt, max_sequence_length=max_sequence_length, device=device, dtype=dtype, ) return prompt_embeds, negative_prompt_embeds def check_inputs( self, prompt, negative_prompt, height, width, video=None, latents=None, prompt_embeds=None, negative_prompt_embeds=None, callback_on_step_end_tensor_inputs=None, overlap_history=None, num_frames=None, base_num_frames=None, ): if height % 16 != 0 or width % 16 != 0: raise ValueError(f"`height` and `width` have to be divisible by 16 but are {height} and {width}.") if callback_on_step_end_tensor_inputs is not None and not all( k in self._callback_tensor_inputs for k in callback_on_step_end_tensor_inputs ): raise ValueError( f"`callback_on_step_end_tensor_inputs` has to be in {self._callback_tensor_inputs}, but found {[k for k in callback_on_step_end_tensor_inputs if k not in self._callback_tensor_inputs]}" ) if prompt is not None and prompt_embeds is not None: raise ValueError( f"Cannot forward both `prompt`: {prompt} and `prompt_embeds`: {prompt_embeds}. Please make sure to" " only forward one of the two." ) elif negative_prompt is not None and negative_prompt_embeds is not None: raise ValueError( f"Cannot forward both `negative_prompt`: {negative_prompt} and `negative_prompt_embeds`: {negative_prompt_embeds}. Please make sure to" " only forward one of the two." ) elif prompt is None and prompt_embeds is None: raise ValueError( "Provide either `prompt` or `prompt_embeds`. Cannot leave both `prompt` and `prompt_embeds` undefined." ) elif prompt is not None and (not isinstance(prompt, str) and not isinstance(prompt, list)): raise ValueError(f"`prompt` has to be of type `str` or `list` but is {type(prompt)}") elif negative_prompt is not None and ( not isinstance(negative_prompt, str) and not isinstance(negative_prompt, list) ): raise ValueError(f"`negative_prompt` has to be of type `str` or `list` but is {type(negative_prompt)}") if video is not None and latents is not None: raise ValueError("Only one of `video` or `latents` should be provided") if num_frames > base_num_frames and overlap_history is None: raise ValueError( "`overlap_history` is required when `num_frames` exceeds `base_num_frames` to ensure smooth transitions in long video generation. " "Please specify a value for `overlap_history`. Recommended values are 17 or 37." ) def prepare_latents( self, video: torch.Tensor, batch_size: int = 1, num_channels_latents: int = 16, height: int = 480, width: int = 832, num_frames: int = 97, dtype: torch.dtype | None = None, device: torch.device | None = None, generator: torch.Generator | list[torch.Generator] | None = None, latents: torch.Tensor | None = None, video_latents: torch.Tensor | None = None, base_latent_num_frames: int | None = None, overlap_history: int | None = None, causal_block_size: int | None = None, overlap_history_latent_frames: int | None = None, long_video_iter: int | None = None, ) -> torch.Tensor: if latents is not None: return latents.to(device=device, dtype=dtype) num_latent_frames = ( (num_frames - 1) // self.vae_scale_factor_temporal + 1 if latents is None else latents.shape[2] ) latent_height = height // self.vae_scale_factor_spatial latent_width = width // self.vae_scale_factor_spatial if long_video_iter == 0: prefix_video_latents = [ retrieve_latents( self.vae.encode( vid.unsqueeze(0)[:, :, -overlap_history:] if vid.dim() == 4 else vid[:, :, -overlap_history:] ), sample_mode="argmax", ) for vid in video ] prefix_video_latents = torch.cat(prefix_video_latents, dim=0).to(dtype) latents_mean = ( torch.tensor(self.vae.config.latents_mean) .view(1, self.vae.config.z_dim, 1, 1, 1) .to(device, self.vae.dtype) ) latents_std = 1.0 / torch.tensor(self.vae.config.latents_std).view(1, self.vae.config.z_dim, 1, 1, 1).to( device, self.vae.dtype ) prefix_video_latents = (prefix_video_latents - latents_mean) * latents_std else: prefix_video_latents = video_latents[:, :, -overlap_history_latent_frames:] if prefix_video_latents.shape[2] % causal_block_size != 0: truncate_len_latents = prefix_video_latents.shape[2] % causal_block_size logger.warning( f"The length of prefix video latents is truncated by {truncate_len_latents} frames for the causal block size alignment. " f"This truncation ensures compatibility with the causal block size, which is required for proper processing. " f"However, it may slightly affect the continuity of the generated video at the truncation boundary." ) prefix_video_latents = prefix_video_latents[:, :, :-truncate_len_latents] prefix_video_latents_frames = prefix_video_latents.shape[2] finished_frame_num = ( long_video_iter * (base_latent_num_frames - overlap_history_latent_frames) + overlap_history_latent_frames ) left_frame_num = num_latent_frames - finished_frame_num num_latent_frames = min(left_frame_num + overlap_history_latent_frames, base_latent_num_frames) shape = ( batch_size, num_channels_latents, num_latent_frames, latent_height, latent_width, ) if isinstance(generator, list) and len(generator) != batch_size: raise ValueError( f"You have passed a list of generators of length {len(generator)}, but requested an effective batch" f" size of {batch_size}. Make sure the batch size matches the length of the generators." ) latents = randn_tensor(shape, generator=generator, device=device, dtype=dtype) return latents, num_latent_frames, prefix_video_latents, prefix_video_latents_frames # Copied from diffusers.pipelines.skyreels_v2.pipeline_skyreels_v2_diffusion_forcing.SkyReelsV2DiffusionForcingPipeline.generate_timestep_matrix def generate_timestep_matrix( self, num_latent_frames: int, step_template: torch.Tensor, base_num_latent_frames: int, ar_step: int = 5, num_pre_ready: int = 0, causal_block_size: int = 1, shrink_interval_with_mask: bool = False, ) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor, list[tuple]]: """ This function implements the core diffusion forcing algorithm that creates a coordinated denoising schedule across temporal frames. It supports both synchronous and asynchronous generation modes: **Synchronous Mode** (ar_step=0, causal_block_size=1): - All frames are denoised simultaneously at each timestep - Each frame follows the same denoising trajectory: [1000, 800, 600, ..., 0] - Simpler but may have less temporal consistency for long videos **Asynchronous Mode** (ar_step>0, causal_block_size>1): - Frames are grouped into causal blocks and processed block/chunk-wise - Each block is denoised in a staggered pattern creating a "denoising wave" - Earlier blocks are more denoised, later blocks lag behind by ar_step timesteps - Creates stronger temporal dependencies and better consistency Args: num_latent_frames (int): Total number of latent frames to generate step_template (torch.Tensor): Base timestep schedule (e.g., [1000, 800, 600, ..., 0]) base_num_latent_frames (int): Maximum frames the model can process in one forward pass ar_step (int, optional): Autoregressive step size for temporal lag. 0 = synchronous, >0 = asynchronous. Defaults to 5. num_pre_ready (int, optional): Number of frames already denoised (e.g., from prefix in a video2video task). Defaults to 0. causal_block_size (int, optional): Number of frames processed as a causal block. Defaults to 1. shrink_interval_with_mask (bool, optional): Whether to optimize processing intervals. Defaults to False. Returns: tuple containing: - step_matrix (torch.Tensor): Matrix of timesteps for each frame at each iteration Shape: [num_iterations, num_latent_frames] - step_index (torch.Tensor): Index matrix for timestep lookup Shape: [num_iterations, num_latent_frames] - step_update_mask (torch.Tensor): Boolean mask indicating which frames to update Shape: [num_iterations, num_latent_frames] - valid_interval (list[tuple]): list of (start, end) intervals for each iteration Raises: ValueError: If ar_step is too small for the given configuration """ # Initialize lists to store the scheduling matrices and metadata step_matrix, step_index = [], [] # Will store timestep values and indices for each iteration update_mask, valid_interval = [], [] # Will store update masks and processing intervals # Calculate total number of denoising iterations (add 1 for initial noise state) num_iterations = len(step_template) + 1 # Convert frame counts to block counts for causal processing # Each block contains causal_block_size frames that are processed together # E.g.: 25 frames ÷ 5 = 5 blocks total num_blocks = num_latent_frames // causal_block_size base_num_blocks = base_num_latent_frames // causal_block_size # Validate ar_step is sufficient for the given configuration # In asynchronous mode, we need enough timesteps to create the staggered pattern if base_num_blocks < num_blocks: min_ar_step = len(step_template) / base_num_blocks if ar_step < min_ar_step: raise ValueError(f"`ar_step` should be at least {math.ceil(min_ar_step)} in your setting") # Extend step_template with boundary values for easier indexing # 999: dummy value for counter starting from 1 # 0: final timestep (completely denoised) step_template = torch.cat( [ torch.tensor([999], dtype=torch.int64, device=step_template.device), step_template.long(), torch.tensor([0], dtype=torch.int64, device=step_template.device), ] ) # Initialize the previous row state (tracks denoising progress for each block) # 0 means not started, num_iterations means fully denoised pre_row = torch.zeros(num_blocks, dtype=torch.long) # Mark pre-ready frames (e.g., from prefix video for a video2video task) as already at final denoising state if num_pre_ready > 0: pre_row[: num_pre_ready // causal_block_size] = num_iterations # Main loop: Generate denoising schedule until all frames are fully denoised while not torch.all(pre_row >= (num_iterations - 1)): # Create new row representing the next denoising step new_row = torch.zeros(num_blocks, dtype=torch.long) # Apply diffusion forcing logic for each block for i in range(num_blocks): if i == 0 or pre_row[i - 1] >= ( num_iterations - 1 ): # the first frame or the last frame is completely denoised new_row[i] = pre_row[i] + 1 else: # Asynchronous mode: lag behind previous block by ar_step timesteps # This creates the "diffusion forcing" staggered pattern new_row[i] = new_row[i - 1] - ar_step # Clamp values to valid range [0, num_iterations] new_row = new_row.clamp(0, num_iterations) # Create update mask: True for blocks that need denoising update at this iteration # Exclude blocks that haven't started (new_row != pre_row) or are finished (new_row != num_iterations) # Final state example: [False, ..., False, True, True, True, True, True] # where first 20 frames are done (False) and last 5 frames still need updates (True) update_mask.append((new_row != pre_row) & (new_row != num_iterations)) # Store the iteration state step_index.append(new_row) # Index into step_template step_matrix.append(step_template[new_row]) # Actual timestep values pre_row = new_row # Update for next iteration # For videos longer than model capacity, we process in sliding windows terminal_flag = base_num_blocks # Optional optimization: shrink interval based on first update mask if shrink_interval_with_mask: idx_sequence = torch.arange(num_blocks, dtype=torch.int64) update_mask = update_mask[0] update_mask_idx = idx_sequence[update_mask] last_update_idx = update_mask_idx[-1].item() terminal_flag = last_update_idx + 1 # Each interval defines which frames to process in the current forward pass for curr_mask in update_mask: # Extend terminal flag if current mask has updates beyond current terminal if terminal_flag < num_blocks and curr_mask[terminal_flag]: terminal_flag += 1 # Create interval: [start, end) where start ensures we don't exceed model capacity valid_interval.append((max(terminal_flag - base_num_blocks, 0), terminal_flag)) # Convert lists to tensors for efficient processing step_update_mask = torch.stack(update_mask, dim=0) step_index = torch.stack(step_index, dim=0) step_matrix = torch.stack(step_matrix, dim=0) # Each block's schedule is replicated to all frames within that block if causal_block_size > 1: # Expand each block to causal_block_size frames step_update_mask = step_update_mask.unsqueeze(-1).repeat(1, 1, causal_block_size).flatten(1).contiguous() step_index = step_index.unsqueeze(-1).repeat(1, 1, causal_block_size).flatten(1).contiguous() step_matrix = step_matrix.unsqueeze(-1).repeat(1, 1, causal_block_size).flatten(1).contiguous() # Scale intervals from block-level to frame-level valid_interval = [(s * causal_block_size, e * causal_block_size) for s, e in valid_interval] return step_matrix, step_index, step_update_mask, valid_interval @property def guidance_scale(self): return self._guidance_scale @property def do_classifier_free_guidance(self): return self._guidance_scale > 1.0 @property def num_timesteps(self): return self._num_timesteps @property def current_timestep(self): return self._current_timestep @property def interrupt(self): return self._interrupt @property def attention_kwargs(self): return self._attention_kwargs @torch.no_grad() @replace_example_docstring(EXAMPLE_DOC_STRING) def __call__( self, video: list[Image.Image], prompt: str | list[str] = None, negative_prompt: str | list[str] = None, height: int = 544, width: int = 960, num_frames: int = 120, num_inference_steps: int = 50, guidance_scale: float = 6.0, num_videos_per_prompt: int | None = 1, generator: torch.Generator | list[torch.Generator] | None = None, latents: torch.Tensor | None = None, prompt_embeds: torch.Tensor | None = None, negative_prompt_embeds: torch.Tensor | None = None, output_type: str | None = "np", return_dict: bool = True, attention_kwargs: dict[str, Any] | None = None, callback_on_step_end: Callable[[int, int], None] | PipelineCallback | MultiPipelineCallbacks | None = None, callback_on_step_end_tensor_inputs: list[str] = ["latents"], max_sequence_length: int = 512, overlap_history: int | None = None, addnoise_condition: float = 0, base_num_frames: int = 97, ar_step: int = 0, causal_block_size: int | None = None, fps: int = 24, ): r""" The call function to the pipeline for generation. Args: video (`list[Image.Image]`): The video to guide the video generation. prompt (`str` or `list[str]`, *optional*): The prompt or prompts to guide the video generation. If not defined, one has to pass `prompt_embeds`. instead. negative_prompt (`str` or `list[str]`, *optional*): The prompt or prompts not to guide the video generation. If not defined, one has to pass `negative_prompt_embeds` instead. Ignored when not using guidance (i.e., ignored if `guidance_scale` is less than `1`). height (`int`, defaults to `544`): The height of the generated video. width (`int`, defaults to `960`): The width of the generated video. num_frames (`int`, defaults to `120`): The number of frames in the generated video. num_inference_steps (`int`, defaults to `50`): The number of denoising steps. More denoising steps usually lead to a higher quality image at the expense of slower inference. guidance_scale (`float`, defaults to `6.0`): Guidance scale as defined in [Classifier-Free Diffusion Guidance](https://arxiv.org/abs/2207.12598). `guidance_scale` is defined as `w` of equation 2. of [Imagen Paper](https://arxiv.org/pdf/2205.11487.pdf). Guidance scale is enabled by setting `guidance_scale > 1`. Higher guidance scale encourages to generate images that are closely linked to the text `prompt`, usually at the expense of lower image quality. (**6.0 for T2V**, **5.0 for I2V**) num_videos_per_prompt (`int`, *optional*, defaults to 1): The number of images to generate per prompt. generator (`torch.Generator` or `list[torch.Generator]`, *optional*): A [`torch.Generator`](https://pytorch.org/docs/stable/generated/torch.Generator.html) to make generation deterministic. latents (`torch.Tensor`, *optional*): Pre-generated noisy latents sampled from a Gaussian distribution, to be used as inputs for image generation. Can be used to tweak the same generation with different prompts. If not provided, a latents tensor is generated by sampling using the supplied random `generator`. prompt_embeds (`torch.Tensor`, *optional*): Pre-generated text embeddings. Can be used to easily tweak text inputs (prompt weighting). If not provided, text embeddings are generated from the `prompt` input argument. negative_prompt_embeds (`torch.Tensor`, *optional*): Pre-generated text embeddings. Can be used to easily tweak text inputs (prompt weighting). If not provided, text embeddings are generated from the `negative_prompt` input argument. output_type (`str`, *optional*, defaults to `"np"`): The output format of the generated image. Choose between `PIL.Image` or `np.array`. return_dict (`bool`, *optional*, defaults to `True`): Whether or not to return a [`SkyReelsV2PipelineOutput`] instead of a plain tuple. attention_kwargs (`dict`, *optional*): A kwargs dictionary that if specified is passed along to the `AttentionProcessor` as defined under `self.processor` in [diffusers.models.attention_processor](https://github.com/huggingface/diffusers/blob/main/src/diffusers/models/attention_processor.py). callback_on_step_end (`Callable`, `PipelineCallback`, `MultiPipelineCallbacks`, *optional*): A function or a subclass of `PipelineCallback` or `MultiPipelineCallbacks` that is called at the end of each denoising step during the inference. with the following arguments: `callback_on_step_end(self: DiffusionPipeline, step: int, timestep: int, callback_kwargs: Dict)`. `callback_kwargs` will include a list of all tensors as specified by `callback_on_step_end_tensor_inputs`. callback_on_step_end_tensor_inputs (`list`, *optional*): The list of tensor inputs for the `callback_on_step_end` function. The tensors specified in the list will be passed as `callback_kwargs` argument. You will only be able to include variables listed in the `._callback_tensor_inputs` attribute of your pipeline class. max_sequence_length (`int`, *optional*, defaults to `512`): The maximum sequence length of the prompt. overlap_history (`int`, *optional*, defaults to `None`): Number of frames to overlap for smooth transitions in long videos. If `None`, the pipeline assumes short video generation mode, and no overlap is applied. 17 and 37 are recommended to set. addnoise_condition (`float`, *optional*, defaults to `0`): This is used to help smooth the long video generation by adding some noise to the clean condition. Too large noise can cause the inconsistency as well. 20 is a recommended value, and you may try larger ones, but it is recommended to not exceed 50. base_num_frames (`int`, *optional*, defaults to `97`): 97 or 121 | Base frame count (**97 for 540P**, **121 for 720P**) ar_step (`int`, *optional*, defaults to `0`): Controls asynchronous inference (0 for synchronous mode) You can set `ar_step=5` to enable asynchronous inference. When asynchronous inference, `causal_block_size=5` is recommended while it is not supposed to be set for synchronous generation. Asynchronous inference will take more steps to diffuse the whole sequence which means it will be SLOWER than synchronous mode. In our experiments, asynchronous inference may improve the instruction following and visual consistent performance. causal_block_size (`int`, *optional*, defaults to `None`): The number of frames in each block/chunk. Recommended when using asynchronous inference (when ar_step > 0) fps (`int`, *optional*, defaults to `24`): Frame rate of the generated video Examples: Returns: [`~SkyReelsV2PipelineOutput`] or `tuple`: If `return_dict` is `True`, [`SkyReelsV2PipelineOutput`] is returned, otherwise a `tuple` is returned where the first element is a list with the generated images and the second element is a list of `bool`s indicating whether the corresponding generated image contains "not-safe-for-work" (nsfw) content. """ if isinstance(callback_on_step_end, (PipelineCallback, MultiPipelineCallbacks)): callback_on_step_end_tensor_inputs = callback_on_step_end.tensor_inputs height = height or self.transformer.config.sample_height * self.vae_scale_factor_spatial width = width or self.transformer.config.sample_width * self.vae_scale_factor_spatial num_videos_per_prompt = 1 # 1. Check inputs. Raise error if not correct self.check_inputs( prompt, negative_prompt, height, width, video, latents, prompt_embeds, negative_prompt_embeds, callback_on_step_end_tensor_inputs, overlap_history, num_frames, base_num_frames, ) if addnoise_condition > 60: logger.warning( f"The value of 'addnoise_condition' is too large ({addnoise_condition}) and may cause inconsistencies in long video generation. A value of 20 is recommended." ) if num_frames % self.vae_scale_factor_temporal != 1: logger.warning( f"`num_frames - 1` has to be divisible by {self.vae_scale_factor_temporal}. Rounding to the nearest number." ) num_frames = num_frames // self.vae_scale_factor_temporal * self.vae_scale_factor_temporal + 1 num_frames = max(num_frames, 1) self._guidance_scale = guidance_scale self._attention_kwargs = attention_kwargs self._current_timestep = None self._interrupt = False device = self._execution_device # 2. Define call parameters if prompt is not None and isinstance(prompt, str): batch_size = 1 elif prompt is not None and isinstance(prompt, list): batch_size = len(prompt) else: batch_size = prompt_embeds.shape[0] # 3. Encode input prompt prompt_embeds, negative_prompt_embeds = self.encode_prompt( prompt=prompt, negative_prompt=negative_prompt, do_classifier_free_guidance=self.do_classifier_free_guidance, num_videos_per_prompt=num_videos_per_prompt, prompt_embeds=prompt_embeds, negative_prompt_embeds=negative_prompt_embeds, max_sequence_length=max_sequence_length, device=device, ) transformer_dtype = self.transformer.dtype prompt_embeds = prompt_embeds.to(transformer_dtype) if negative_prompt_embeds is not None: negative_prompt_embeds = negative_prompt_embeds.to(transformer_dtype) # 4. Prepare timesteps self.scheduler.set_timesteps(num_inference_steps, device=device) timesteps = self.scheduler.timesteps if latents is None: video_original = self.video_processor.preprocess_video(video, height=height, width=width).to( device, dtype=torch.float32 ) if causal_block_size is None: causal_block_size = self.transformer.config.num_frame_per_block else: self.transformer._set_ar_attention(causal_block_size) fps_embeds = [fps] * prompt_embeds.shape[0] fps_embeds = [0 if i == 16 else 1 for i in fps_embeds] # Long video generation accumulated_latents = None overlap_history_latent_frames = (overlap_history - 1) // self.vae_scale_factor_temporal + 1 num_latent_frames = (num_frames - 1) // self.vae_scale_factor_temporal + 1 base_latent_num_frames = ( (base_num_frames - 1) // self.vae_scale_factor_temporal + 1 if base_num_frames is not None else num_latent_frames ) n_iter = ( 1 + (num_latent_frames - base_latent_num_frames - 1) // (base_latent_num_frames - overlap_history_latent_frames) + 1 ) for long_video_iter in range(n_iter): logger.debug(f"Processing iteration {long_video_iter + 1}/{n_iter} for long video generation...") # 5. Prepare latent variables num_channels_latents = self.transformer.config.in_channels latents, current_num_latent_frames, prefix_video_latents, prefix_video_latents_frames = ( self.prepare_latents( video_original, batch_size * num_videos_per_prompt, num_channels_latents, height, width, num_frames, torch.float32, device, generator, latents if long_video_iter == 0 else None, video_latents=accumulated_latents, # Pass latents directly instead of decoded video overlap_history=overlap_history, base_latent_num_frames=base_latent_num_frames, causal_block_size=causal_block_size, overlap_history_latent_frames=overlap_history_latent_frames, long_video_iter=long_video_iter, ) ) if prefix_video_latents_frames > 0: latents[:, :, :prefix_video_latents_frames, :, :] = prefix_video_latents.to(transformer_dtype) # 4. Prepare sample schedulers and timestep matrix sample_schedulers = [] for _ in range(current_num_latent_frames): sample_scheduler = deepcopy(self.scheduler) sample_scheduler.set_timesteps(num_inference_steps, device=device) sample_schedulers.append(sample_scheduler) step_matrix, _, step_update_mask, valid_interval = self.generate_timestep_matrix( current_num_latent_frames, timesteps, current_num_latent_frames, ar_step, prefix_video_latents_frames, causal_block_size, ) # 6. Denoising loop num_warmup_steps = len(timesteps) - num_inference_steps * self.scheduler.order self._num_timesteps = len(step_matrix) with self.progress_bar(total=len(step_matrix)) as progress_bar: for i, t in enumerate(step_matrix): if self.interrupt: continue self._current_timestep = t valid_interval_start, valid_interval_end = valid_interval[i] latent_model_input = ( latents[:, :, valid_interval_start:valid_interval_end, :, :].to(transformer_dtype).clone() ) timestep = t.expand(latents.shape[0], -1)[:, valid_interval_start:valid_interval_end].clone() if addnoise_condition > 0 and valid_interval_start < prefix_video_latents_frames: noise_factor = 0.001 * addnoise_condition latent_model_input[:, :, valid_interval_start:prefix_video_latents_frames, :, :] = ( latent_model_input[:, :, valid_interval_start:prefix_video_latents_frames, :, :] * (1.0 - noise_factor) + torch.randn_like( latent_model_input[:, :, valid_interval_start:prefix_video_latents_frames, :, :] ) * noise_factor ) timestep[:, valid_interval_start:prefix_video_latents_frames] = addnoise_condition with self.transformer.cache_context("cond"): noise_pred = self.transformer( hidden_states=latent_model_input, timestep=timestep, encoder_hidden_states=prompt_embeds, enable_diffusion_forcing=True, fps=fps_embeds, attention_kwargs=attention_kwargs, return_dict=False, )[0] if self.do_classifier_free_guidance: with self.transformer.cache_context("uncond"): noise_uncond = self.transformer( hidden_states=latent_model_input, timestep=timestep, encoder_hidden_states=negative_prompt_embeds, enable_diffusion_forcing=True, fps=fps_embeds, attention_kwargs=attention_kwargs, return_dict=False, )[0] noise_pred = noise_uncond + guidance_scale * (noise_pred - noise_uncond) update_mask_i = step_update_mask[i] for idx in range(valid_interval_start, valid_interval_end): if update_mask_i[idx].item(): latents[:, :, idx, :, :] = sample_schedulers[idx].step( noise_pred[:, :, idx - valid_interval_start, :, :], t[idx], latents[:, :, idx, :, :], return_dict=False, )[0] if callback_on_step_end is not None: callback_kwargs = {} for k in callback_on_step_end_tensor_inputs: callback_kwargs[k] = locals()[k] callback_outputs = callback_on_step_end(self, i, t, callback_kwargs) latents = callback_outputs.pop("latents", latents) prompt_embeds = callback_outputs.pop("prompt_embeds", prompt_embeds) negative_prompt_embeds = callback_outputs.pop("negative_prompt_embeds", negative_prompt_embeds) # call the callback, if provided if i == len(step_matrix) - 1 or ( (i + 1) > num_warmup_steps and (i + 1) % self.scheduler.order == 0 ): progress_bar.update() if XLA_AVAILABLE: xm.mark_step() if accumulated_latents is None: accumulated_latents = latents else: # Keep overlap frames for conditioning but don't include them in final output accumulated_latents = torch.cat( [accumulated_latents, latents[:, :, overlap_history_latent_frames:]], dim=2 ) latents = accumulated_latents self._current_timestep = None # Final decoding step - convert latents to pixels if not output_type == "latent": latents = latents.to(self.vae.dtype) latents_mean = ( torch.tensor(self.vae.config.latents_mean) .view(1, self.vae.config.z_dim, 1, 1, 1) .to(latents.device, latents.dtype) ) latents_std = 1.0 / torch.tensor(self.vae.config.latents_std).view(1, self.vae.config.z_dim, 1, 1, 1).to( latents.device, latents.dtype ) latents = latents / latents_std + latents_mean video_generated = self.vae.decode(latents, return_dict=False)[0] video = torch.cat([video_original, video_generated], dim=2) video = self.video_processor.postprocess_video(video, output_type=output_type) else: video = latents # Offload all models self.maybe_free_model_hooks() if not return_dict: return (video,) return SkyReelsV2PipelineOutput(frames=video)
{ "repo_id": "huggingface/diffusers", "file_path": "src/diffusers/pipelines/skyreels_v2/pipeline_skyreels_v2_diffusion_forcing_v2v.py", "license": "Apache License 2.0", "lines": 933, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
huggingface/diffusers:src/diffusers/pipelines/skyreels_v2/pipeline_skyreels_v2_i2v.py
# Copyright 2025 The SkyReels-V2 Team, The Wan Team and The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import html from typing import Any, Callable import PIL import regex as re import torch from transformers import AutoTokenizer, CLIPProcessor, CLIPVisionModelWithProjection, T5EncoderModel, UMT5EncoderModel from ...callbacks import MultiPipelineCallbacks, PipelineCallback from ...image_processor import PipelineImageInput from ...loaders import SkyReelsV2LoraLoaderMixin from ...models import AutoencoderKLWan, SkyReelsV2Transformer3DModel from ...schedulers import UniPCMultistepScheduler from ...utils import is_ftfy_available, is_torch_xla_available, logging, replace_example_docstring from ...utils.torch_utils import randn_tensor from ...video_processor import VideoProcessor from ..pipeline_utils import DiffusionPipeline from .pipeline_output import SkyReelsV2PipelineOutput if is_torch_xla_available(): import torch_xla.core.xla_model as xm XLA_AVAILABLE = True else: XLA_AVAILABLE = False logger = logging.get_logger(__name__) # pylint: disable=invalid-name if is_ftfy_available(): import ftfy EXAMPLE_DOC_STRING = """\ Examples: ```py >>> import torch >>> from diffusers import ( ... SkyReelsV2ImageToVideoPipeline, ... UniPCMultistepScheduler, ... AutoencoderKLWan, ... ) >>> from diffusers.utils import export_to_video >>> from PIL import Image >>> # Load the pipeline >>> # Available models: >>> # - Skywork/SkyReels-V2-I2V-1.3B-540P-Diffusers >>> # - Skywork/SkyReels-V2-I2V-14B-540P-Diffusers >>> # - Skywork/SkyReels-V2-I2V-14B-720P-Diffusers >>> vae = AutoencoderKLWan.from_pretrained( ... "Skywork/SkyReels-V2-I2V-14B-720P-Diffusers", ... subfolder="vae", ... torch_dtype=torch.float32, ... ) >>> pipe = SkyReelsV2ImageToVideoPipeline.from_pretrained( ... "Skywork/SkyReels-V2-I2V-14B-720P-Diffusers", ... vae=vae, ... torch_dtype=torch.bfloat16, ... ) >>> flow_shift = 5.0 # 8.0 for T2V, 5.0 for I2V >>> pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config, flow_shift=flow_shift) >>> pipe = pipe.to("cuda") >>> prompt = "A cat and a dog baking a cake together in a kitchen. The cat is carefully measuring flour, while the dog is stirring the batter with a wooden spoon. The kitchen is cozy, with sunlight streaming through the window." >>> image = Image.open("path/to/image.png") >>> output = pipe( ... image=image, ... prompt=prompt, ... num_inference_steps=50, ... height=544, ... width=960, ... guidance_scale=5.0, # 6.0 for T2V, 5.0 for I2V ... num_frames=97, ... ).frames[0] >>> export_to_video(output, "video.mp4", fps=24, quality=8) ``` """ def basic_clean(text): text = ftfy.fix_text(text) text = html.unescape(html.unescape(text)) return text.strip() def whitespace_clean(text): text = re.sub(r"\s+", " ", text) text = text.strip() return text def prompt_clean(text): text = whitespace_clean(basic_clean(text)) return text # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion_img2img.retrieve_latents def retrieve_latents( encoder_output: torch.Tensor, generator: torch.Generator | None = None, sample_mode: str = "sample" ): if hasattr(encoder_output, "latent_dist") and sample_mode == "sample": return encoder_output.latent_dist.sample(generator) elif hasattr(encoder_output, "latent_dist") and sample_mode == "argmax": return encoder_output.latent_dist.mode() elif hasattr(encoder_output, "latents"): return encoder_output.latents else: raise AttributeError("Could not access latents of provided encoder_output") class SkyReelsV2ImageToVideoPipeline(DiffusionPipeline, SkyReelsV2LoraLoaderMixin): r""" Pipeline for Image-to-Video (i2v) generation using SkyReels-V2. This model inherits from [`DiffusionPipeline`]. Check the superclass documentation for the generic methods implemented for all pipelines (downloading, saving, running on a particular device, etc.). Args: tokenizer ([`T5Tokenizer`]): Tokenizer from [T5](https://huggingface.co/docs/transformers/en/model_doc/t5#transformers.T5Tokenizer), specifically the [google/umt5-xxl](https://huggingface.co/google/umt5-xxl) variant. text_encoder ([`T5EncoderModel`]): [T5](https://huggingface.co/docs/transformers/en/model_doc/t5#transformers.T5EncoderModel), specifically the [google/umt5-xxl](https://huggingface.co/google/umt5-xxl) variant. image_encoder ([`CLIPVisionModelWithProjection`]): [CLIP](https://huggingface.co/docs/transformers/model_doc/clip#transformers.CLIPVisionModelWithProjection), specifically the [clip-vit-huge-patch14](https://github.com/mlfoundations/open_clip/blob/main/docs/PRETRAINED.md#vit-h14-xlm-roberta-large) variant. transformer ([`SkyReelsV2Transformer3DModel`]): Conditional Transformer to denoise the input latents. scheduler ([`UniPCMultistepScheduler`]): A scheduler to be used in combination with `transformer` to denoise the encoded image latents. vae ([`AutoencoderKLWan`]): Variational Auto-Encoder (VAE) Model to encode and decode videos to and from latent representations. """ model_cpu_offload_seq = "text_encoder->image_encoder->transformer->vae" _callback_tensor_inputs = ["latents", "prompt_embeds", "negative_prompt_embeds"] def __init__( self, tokenizer: AutoTokenizer, text_encoder: T5EncoderModel | UMT5EncoderModel, image_encoder: CLIPVisionModelWithProjection, image_processor: CLIPProcessor, transformer: SkyReelsV2Transformer3DModel, vae: AutoencoderKLWan, scheduler: UniPCMultistepScheduler, ): super().__init__() self.register_modules( vae=vae, text_encoder=text_encoder, tokenizer=tokenizer, image_encoder=image_encoder, transformer=transformer, scheduler=scheduler, image_processor=image_processor, ) self.vae_scale_factor_temporal = 2 ** sum(self.vae.temperal_downsample) if getattr(self, "vae", None) else 4 self.vae_scale_factor_spatial = 2 ** len(self.vae.temperal_downsample) if getattr(self, "vae", None) else 8 self.video_processor = VideoProcessor(vae_scale_factor=self.vae_scale_factor_spatial) self.image_processor = image_processor # Copied from diffusers.pipelines.wan.pipeline_wan_i2v.WanImageToVideoPipeline._get_t5_prompt_embeds def _get_t5_prompt_embeds( self, prompt: str | list[str] = None, num_videos_per_prompt: int = 1, max_sequence_length: int = 512, device: torch.device | None = None, dtype: torch.dtype | None = None, ): device = device or self._execution_device dtype = dtype or self.text_encoder.dtype prompt = [prompt] if isinstance(prompt, str) else prompt prompt = [prompt_clean(u) for u in prompt] batch_size = len(prompt) text_inputs = self.tokenizer( prompt, padding="max_length", max_length=max_sequence_length, truncation=True, add_special_tokens=True, return_attention_mask=True, return_tensors="pt", ) text_input_ids, mask = text_inputs.input_ids, text_inputs.attention_mask seq_lens = mask.gt(0).sum(dim=1).long() prompt_embeds = self.text_encoder(text_input_ids.to(device), mask.to(device)).last_hidden_state prompt_embeds = prompt_embeds.to(dtype=dtype, device=device) prompt_embeds = [u[:v] for u, v in zip(prompt_embeds, seq_lens)] prompt_embeds = torch.stack( [torch.cat([u, u.new_zeros(max_sequence_length - u.size(0), u.size(1))]) for u in prompt_embeds], dim=0 ) # duplicate text embeddings for each generation per prompt, using mps friendly method _, seq_len, _ = prompt_embeds.shape prompt_embeds = prompt_embeds.repeat(1, num_videos_per_prompt, 1) prompt_embeds = prompt_embeds.view(batch_size * num_videos_per_prompt, seq_len, -1) return prompt_embeds # Copied from diffusers.pipelines.wan.pipeline_wan_i2v.WanImageToVideoPipeline.encode_image def encode_image( self, image: PipelineImageInput, device: torch.device | None = None, ): device = device or self._execution_device image = self.image_processor(images=image, return_tensors="pt").to(device) image_embeds = self.image_encoder(**image, output_hidden_states=True) return image_embeds.hidden_states[-2] # Copied from diffusers.pipelines.wan.pipeline_wan_i2v.WanImageToVideoPipeline.encode_prompt def encode_prompt( self, prompt: str | list[str], negative_prompt: str | list[str] | None = None, do_classifier_free_guidance: bool = True, num_videos_per_prompt: int = 1, prompt_embeds: torch.Tensor | None = None, negative_prompt_embeds: torch.Tensor | None = None, max_sequence_length: int = 226, device: torch.device | None = None, dtype: torch.dtype | None = None, ): r""" Encodes the prompt into text encoder hidden states. Args: prompt (`str` or `list[str]`, *optional*): prompt to be encoded negative_prompt (`str` or `list[str]`, *optional*): The prompt or prompts not to guide the image generation. If not defined, one has to pass `negative_prompt_embeds` instead. Ignored when not using guidance (i.e., ignored if `guidance_scale` is less than `1`). do_classifier_free_guidance (`bool`, *optional*, defaults to `True`): Whether to use classifier free guidance or not. num_videos_per_prompt (`int`, *optional*, defaults to 1): Number of videos that should be generated per prompt. torch device to place the resulting embeddings on prompt_embeds (`torch.Tensor`, *optional*): Pre-generated text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt weighting. If not provided, text embeddings will be generated from `prompt` input argument. negative_prompt_embeds (`torch.Tensor`, *optional*): Pre-generated negative text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt weighting. If not provided, negative_prompt_embeds will be generated from `negative_prompt` input argument. device: (`torch.device`, *optional*): torch device dtype: (`torch.dtype`, *optional*): torch dtype """ device = device or self._execution_device prompt = [prompt] if isinstance(prompt, str) else prompt if prompt is not None: batch_size = len(prompt) else: batch_size = prompt_embeds.shape[0] if prompt_embeds is None: prompt_embeds = self._get_t5_prompt_embeds( prompt=prompt, num_videos_per_prompt=num_videos_per_prompt, max_sequence_length=max_sequence_length, device=device, dtype=dtype, ) if do_classifier_free_guidance and negative_prompt_embeds is None: negative_prompt = negative_prompt or "" negative_prompt = batch_size * [negative_prompt] if isinstance(negative_prompt, str) else negative_prompt if prompt is not None and type(prompt) is not type(negative_prompt): raise TypeError( f"`negative_prompt` should be the same type to `prompt`, but got {type(negative_prompt)} !=" f" {type(prompt)}." ) elif batch_size != len(negative_prompt): raise ValueError( f"`negative_prompt`: {negative_prompt} has batch size {len(negative_prompt)}, but `prompt`:" f" {prompt} has batch size {batch_size}. Please make sure that passed `negative_prompt` matches" " the batch size of `prompt`." ) negative_prompt_embeds = self._get_t5_prompt_embeds( prompt=negative_prompt, num_videos_per_prompt=num_videos_per_prompt, max_sequence_length=max_sequence_length, device=device, dtype=dtype, ) return prompt_embeds, negative_prompt_embeds def check_inputs( self, prompt, negative_prompt, image, height, width, prompt_embeds=None, negative_prompt_embeds=None, image_embeds=None, callback_on_step_end_tensor_inputs=None, ): if image is not None and image_embeds is not None: raise ValueError( f"Cannot forward both `image`: {image} and `image_embeds`: {image_embeds}. Please make sure to" " only forward one of the two." ) if image is None and image_embeds is None: raise ValueError( "Provide either `image` or `prompt_embeds`. Cannot leave both `image` and `image_embeds` undefined." ) if image is not None and not isinstance(image, torch.Tensor) and not isinstance(image, PIL.Image.Image): raise ValueError(f"`image` has to be of type `torch.Tensor` or `PIL.Image.Image` but is {type(image)}") if height % 16 != 0 or width % 16 != 0: raise ValueError(f"`height` and `width` have to be divisible by 16 but are {height} and {width}.") if callback_on_step_end_tensor_inputs is not None and not all( k in self._callback_tensor_inputs for k in callback_on_step_end_tensor_inputs ): raise ValueError( f"`callback_on_step_end_tensor_inputs` has to be in {self._callback_tensor_inputs}, but found {[k for k in callback_on_step_end_tensor_inputs if k not in self._callback_tensor_inputs]}" ) if prompt is not None and prompt_embeds is not None: raise ValueError( f"Cannot forward both `prompt`: {prompt} and `prompt_embeds`: {prompt_embeds}. Please make sure to" " only forward one of the two." ) elif negative_prompt is not None and negative_prompt_embeds is not None: raise ValueError( f"Cannot forward both `negative_prompt`: {negative_prompt} and `negative_prompt_embeds`: {negative_prompt_embeds}. Please make sure to" " only forward one of the two." ) elif prompt is None and prompt_embeds is None: raise ValueError( "Provide either `prompt` or `prompt_embeds`. Cannot leave both `prompt` and `prompt_embeds` undefined." ) elif prompt is not None and (not isinstance(prompt, str) and not isinstance(prompt, list)): raise ValueError(f"`prompt` has to be of type `str` or `list` but is {type(prompt)}") elif negative_prompt is not None and ( not isinstance(negative_prompt, str) and not isinstance(negative_prompt, list) ): raise ValueError(f"`negative_prompt` has to be of type `str` or `list` but is {type(negative_prompt)}") def prepare_latents( self, image: PipelineImageInput, batch_size: int, num_channels_latents: int = 16, height: int = 480, width: int = 832, num_frames: int = 81, dtype: torch.dtype | None = None, device: torch.device | None = None, generator: torch.Generator | list[torch.Generator] | None = None, latents: torch.Tensor | None = None, last_image: torch.Tensor | None = None, ) -> tuple[torch.Tensor, torch.Tensor]: num_latent_frames = (num_frames - 1) // self.vae_scale_factor_temporal + 1 latent_height = height // self.vae_scale_factor_spatial latent_width = width // self.vae_scale_factor_spatial shape = (batch_size, num_channels_latents, num_latent_frames, latent_height, latent_width) if isinstance(generator, list) and len(generator) != batch_size: raise ValueError( f"You have passed a list of generators of length {len(generator)}, but requested an effective batch" f" size of {batch_size}. Make sure the batch size matches the length of the generators." ) if latents is None: latents = randn_tensor(shape, generator=generator, device=device, dtype=dtype) else: latents = latents.to(device=device, dtype=dtype) image = image.unsqueeze(2) if last_image is None: video_condition = torch.cat( [image, image.new_zeros(image.shape[0], image.shape[1], num_frames - 1, height, width)], dim=2 ) else: last_image = last_image.unsqueeze(2) video_condition = torch.cat( [image, image.new_zeros(image.shape[0], image.shape[1], num_frames - 2, height, width), last_image], dim=2, ) video_condition = video_condition.to(device=device, dtype=self.vae.dtype) latents_mean = ( torch.tensor(self.vae.config.latents_mean) .view(1, self.vae.config.z_dim, 1, 1, 1) .to(latents.device, latents.dtype) ) latents_std = 1.0 / torch.tensor(self.vae.config.latents_std).view(1, self.vae.config.z_dim, 1, 1, 1).to( latents.device, latents.dtype ) if isinstance(generator, list): latent_condition = [ retrieve_latents(self.vae.encode(video_condition), sample_mode="argmax") for _ in generator ] latent_condition = torch.cat(latent_condition) else: latent_condition = retrieve_latents(self.vae.encode(video_condition), sample_mode="argmax") latent_condition = latent_condition.repeat(batch_size, 1, 1, 1, 1) latent_condition = latent_condition.to(dtype) latent_condition = (latent_condition - latents_mean) * latents_std mask_lat_size = torch.ones(batch_size, 1, num_frames, latent_height, latent_width) if last_image is None: mask_lat_size[:, :, list(range(1, num_frames))] = 0 else: mask_lat_size[:, :, list(range(1, num_frames - 1))] = 0 first_frame_mask = mask_lat_size[:, :, 0:1] first_frame_mask = torch.repeat_interleave(first_frame_mask, dim=2, repeats=self.vae_scale_factor_temporal) mask_lat_size = torch.concat([first_frame_mask, mask_lat_size[:, :, 1:, :]], dim=2) mask_lat_size = mask_lat_size.view(batch_size, -1, self.vae_scale_factor_temporal, latent_height, latent_width) mask_lat_size = mask_lat_size.transpose(1, 2) mask_lat_size = mask_lat_size.to(latent_condition.device) return latents, torch.concat([mask_lat_size, latent_condition], dim=1) @property def guidance_scale(self): return self._guidance_scale @property def do_classifier_free_guidance(self): return self._guidance_scale > 1 @property def num_timesteps(self): return self._num_timesteps @property def current_timestep(self): return self._current_timestep @property def interrupt(self): return self._interrupt @property def attention_kwargs(self): return self._attention_kwargs @torch.no_grad() @replace_example_docstring(EXAMPLE_DOC_STRING) def __call__( self, image: PipelineImageInput, prompt: str | list[str] = None, negative_prompt: str | list[str] = None, height: int = 544, width: int = 960, num_frames: int = 97, num_inference_steps: int = 50, guidance_scale: float = 5.0, num_videos_per_prompt: int | None = 1, generator: torch.Generator | list[torch.Generator] | None = None, latents: torch.Tensor | None = None, prompt_embeds: torch.Tensor | None = None, negative_prompt_embeds: torch.Tensor | None = None, image_embeds: torch.Tensor | None = None, last_image: torch.Tensor | None = None, output_type: str | None = "np", return_dict: bool = True, attention_kwargs: dict[str, Any] | None = None, callback_on_step_end: Callable[[int, int], None] | PipelineCallback | MultiPipelineCallbacks | None = None, callback_on_step_end_tensor_inputs: list[str] = ["latents"], max_sequence_length: int = 512, ): r""" The call function to the pipeline for generation. Args: image (`PipelineImageInput`): The input image to condition the generation on. Must be an image, a list of images or a `torch.Tensor`. prompt (`str` or `list[str]`, *optional*): The prompt or prompts to guide the image generation. If not defined, one has to pass `prompt_embeds`. instead. negative_prompt (`str` or `list[str]`, *optional*): The prompt or prompts not to guide the image generation. If not defined, one has to pass `negative_prompt_embeds` instead. Ignored when not using guidance (i.e., ignored if `guidance_scale` is less than `1`). height (`int`, defaults to `544`): The height of the generated video. width (`int`, defaults to `960`): The width of the generated video. num_frames (`int`, defaults to `97`): The number of frames in the generated video. num_inference_steps (`int`, defaults to `50`): The number of denoising steps. More denoising steps usually lead to a higher quality image at the expense of slower inference. guidance_scale (`float`, defaults to `5.0`): Guidance scale as defined in [Classifier-Free Diffusion Guidance](https://huggingface.co/papers/2207.12598). `guidance_scale` is defined as `w` of equation 2. of [Imagen Paper](https://huggingface.co/papers/2205.11487). Guidance scale is enabled by setting `guidance_scale > 1`. Higher guidance scale encourages to generate images that are closely linked to the text `prompt`, usually at the expense of lower image quality. num_videos_per_prompt (`int`, *optional*, defaults to 1): The number of images to generate per prompt. generator (`torch.Generator` or `list[torch.Generator]`, *optional*): A [`torch.Generator`](https://pytorch.org/docs/stable/generated/torch.Generator.html) to make generation deterministic. latents (`torch.Tensor`, *optional*): Pre-generated noisy latents sampled from a Gaussian distribution, to be used as inputs for image generation. Can be used to tweak the same generation with different prompts. If not provided, a latents tensor is generated by sampling using the supplied random `generator`. prompt_embeds (`torch.Tensor`, *optional*): Pre-generated text embeddings. Can be used to easily tweak text inputs (prompt weighting). If not provided, text embeddings are generated from the `prompt` input argument. negative_prompt_embeds (`torch.Tensor`, *optional*): Pre-generated text embeddings. Can be used to easily tweak text inputs (prompt weighting). If not provided, text embeddings are generated from the `negative_prompt` input argument. image_embeds (`torch.Tensor`, *optional*): Pre-generated image embeddings. Can be used to easily tweak image inputs (weighting). If not provided, image embeddings are generated from the `image` input argument. output_type (`str`, *optional*, defaults to `"np"`): The output format of the generated image. Choose between `PIL.Image` or `np.array`. return_dict (`bool`, *optional*, defaults to `True`): Whether or not to return a [`WanPipelineOutput`] instead of a plain tuple. attention_kwargs (`dict`, *optional*): A kwargs dictionary that if specified is passed along to the `AttentionProcessor` as defined under `self.processor` in [diffusers.models.attention_processor](https://github.com/huggingface/diffusers/blob/main/src/diffusers/models/attention_processor.py). callback_on_step_end (`Callable`, `PipelineCallback`, `MultiPipelineCallbacks`, *optional*): A function or a subclass of `PipelineCallback` or `MultiPipelineCallbacks` that is called at the end of each denoising step during the inference. with the following arguments: `callback_on_step_end(self: DiffusionPipeline, step: int, timestep: int, callback_kwargs: Dict)`. `callback_kwargs` will include a list of all tensors as specified by `callback_on_step_end_tensor_inputs`. callback_on_step_end_tensor_inputs (`list`, *optional*): The list of tensor inputs for the `callback_on_step_end` function. The tensors specified in the list will be passed as `callback_kwargs` argument. You will only be able to include variables listed in the `._callback_tensor_inputs` attribute of your pipeline class. max_sequence_length (`int`, *optional*, defaults to `512`): The maximum sequence length of the prompt. Examples: Returns: [`~SkyReelsV2PipelineOutput`] or `tuple`: If `return_dict` is `True`, [`SkyReelsV2PipelineOutput`] is returned, otherwise a `tuple` is returned where the first element is a list with the generated images and the second element is a list of `bool`s indicating whether the corresponding generated image contains "not-safe-for-work" (nsfw) content. """ if isinstance(callback_on_step_end, (PipelineCallback, MultiPipelineCallbacks)): callback_on_step_end_tensor_inputs = callback_on_step_end.tensor_inputs # 1. Check inputs. Raise error if not correct self.check_inputs( prompt, negative_prompt, image, height, width, prompt_embeds, negative_prompt_embeds, image_embeds, callback_on_step_end_tensor_inputs, ) if num_frames % self.vae_scale_factor_temporal != 1: logger.warning( f"`num_frames - 1` has to be divisible by {self.vae_scale_factor_temporal}. Rounding to the nearest number." ) num_frames = num_frames // self.vae_scale_factor_temporal * self.vae_scale_factor_temporal + 1 num_frames = max(num_frames, 1) self._guidance_scale = guidance_scale self._attention_kwargs = attention_kwargs self._current_timestep = None self._interrupt = False device = self._execution_device # 2. Define call parameters if prompt is not None and isinstance(prompt, str): batch_size = 1 elif prompt is not None and isinstance(prompt, list): batch_size = len(prompt) else: batch_size = prompt_embeds.shape[0] # 3. Encode input prompt prompt_embeds, negative_prompt_embeds = self.encode_prompt( prompt=prompt, negative_prompt=negative_prompt, do_classifier_free_guidance=self.do_classifier_free_guidance, num_videos_per_prompt=num_videos_per_prompt, prompt_embeds=prompt_embeds, negative_prompt_embeds=negative_prompt_embeds, max_sequence_length=max_sequence_length, device=device, ) # Encode image embedding transformer_dtype = self.transformer.dtype prompt_embeds = prompt_embeds.to(transformer_dtype) if negative_prompt_embeds is not None: negative_prompt_embeds = negative_prompt_embeds.to(transformer_dtype) if image_embeds is None: if last_image is None: image_embeds = self.encode_image(image, device) else: image_embeds = self.encode_image([image, last_image], device) image_embeds = image_embeds.repeat(batch_size, 1, 1) image_embeds = image_embeds.to(transformer_dtype) # 4. Prepare timesteps self.scheduler.set_timesteps(num_inference_steps, device=device) timesteps = self.scheduler.timesteps # 5. Prepare latent variables num_channels_latents = self.vae.config.z_dim image = self.video_processor.preprocess(image, height=height, width=width).to(device, dtype=torch.float32) if last_image is not None: last_image = self.video_processor.preprocess(last_image, height=height, width=width).to( device, dtype=torch.float32 ) latents, condition = self.prepare_latents( image, batch_size * num_videos_per_prompt, num_channels_latents, height, width, num_frames, torch.float32, device, generator, latents, last_image, ) # 6. Denoising loop num_warmup_steps = len(timesteps) - num_inference_steps * self.scheduler.order self._num_timesteps = len(timesteps) with self.progress_bar(total=num_inference_steps) as progress_bar: for i, t in enumerate(timesteps): if self.interrupt: continue self._current_timestep = t latent_model_input = torch.cat([latents, condition], dim=1).to(transformer_dtype) timestep = t.expand(latents.shape[0]) with self.transformer.cache_context("cond"): noise_pred = self.transformer( hidden_states=latent_model_input, timestep=timestep, encoder_hidden_states=prompt_embeds, encoder_hidden_states_image=image_embeds, attention_kwargs=attention_kwargs, return_dict=False, )[0] if self.do_classifier_free_guidance: with self.transformer.cache_context("uncond"): noise_uncond = self.transformer( hidden_states=latent_model_input, timestep=timestep, encoder_hidden_states=negative_prompt_embeds, encoder_hidden_states_image=image_embeds, attention_kwargs=attention_kwargs, return_dict=False, )[0] noise_pred = noise_uncond + guidance_scale * (noise_pred - noise_uncond) # compute the previous noisy sample x_t -> x_t-1 latents = self.scheduler.step(noise_pred, t, latents, return_dict=False)[0] if callback_on_step_end is not None: callback_kwargs = {} for k in callback_on_step_end_tensor_inputs: callback_kwargs[k] = locals()[k] callback_outputs = callback_on_step_end(self, i, t, callback_kwargs) latents = callback_outputs.pop("latents", latents) prompt_embeds = callback_outputs.pop("prompt_embeds", prompt_embeds) negative_prompt_embeds = callback_outputs.pop("negative_prompt_embeds", negative_prompt_embeds) # call the callback, if provided if i == len(timesteps) - 1 or ((i + 1) > num_warmup_steps and (i + 1) % self.scheduler.order == 0): progress_bar.update() if XLA_AVAILABLE: xm.mark_step() self._current_timestep = None if not output_type == "latent": latents = latents.to(self.vae.dtype) latents_mean = ( torch.tensor(self.vae.config.latents_mean) .view(1, self.vae.config.z_dim, 1, 1, 1) .to(latents.device, latents.dtype) ) latents_std = 1.0 / torch.tensor(self.vae.config.latents_std).view(1, self.vae.config.z_dim, 1, 1, 1).to( latents.device, latents.dtype ) latents = latents / latents_std + latents_mean video = self.vae.decode(latents, return_dict=False)[0] video = self.video_processor.postprocess_video(video, output_type=output_type) else: video = latents # Offload all models self.maybe_free_model_hooks() if not return_dict: return (video,) return SkyReelsV2PipelineOutput(frames=video)
{ "repo_id": "huggingface/diffusers", "file_path": "src/diffusers/pipelines/skyreels_v2/pipeline_skyreels_v2_i2v.py", "license": "Apache License 2.0", "lines": 652, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
huggingface/diffusers:tests/models/transformers/test_models_transformer_skyreels_v2.py
# Copyright 2024 HuggingFace Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest import torch from diffusers import SkyReelsV2Transformer3DModel from ...testing_utils import ( enable_full_determinism, torch_device, ) from ..test_modeling_common import ModelTesterMixin, TorchCompileTesterMixin enable_full_determinism() class SkyReelsV2Transformer3DTests(ModelTesterMixin, TorchCompileTesterMixin, unittest.TestCase): model_class = SkyReelsV2Transformer3DModel main_input_name = "hidden_states" uses_custom_attn_processor = True @property def dummy_input(self): batch_size = 1 num_channels = 4 num_frames = 2 height = 16 width = 16 text_encoder_embedding_dim = 16 sequence_length = 12 hidden_states = torch.randn((batch_size, num_channels, num_frames, height, width)).to(torch_device) timestep = torch.randint(0, 1000, size=(batch_size,)).to(torch_device) encoder_hidden_states = torch.randn((batch_size, sequence_length, text_encoder_embedding_dim)).to(torch_device) return { "hidden_states": hidden_states, "encoder_hidden_states": encoder_hidden_states, "timestep": timestep, } @property def input_shape(self): return (4, 1, 16, 16) @property def output_shape(self): return (4, 1, 16, 16) def prepare_init_args_and_inputs_for_common(self): init_dict = { "patch_size": (1, 2, 2), "num_attention_heads": 2, "attention_head_dim": 12, "in_channels": 4, "out_channels": 4, "text_dim": 16, "freq_dim": 256, "ffn_dim": 32, "num_layers": 2, "cross_attn_norm": True, "qk_norm": "rms_norm_across_heads", "rope_max_seq_len": 32, } inputs_dict = self.dummy_input return init_dict, inputs_dict def test_gradient_checkpointing_is_applied(self): expected_set = {"SkyReelsV2Transformer3DModel"} super().test_gradient_checkpointing_is_applied(expected_set=expected_set)
{ "repo_id": "huggingface/diffusers", "file_path": "tests/models/transformers/test_models_transformer_skyreels_v2.py", "license": "Apache License 2.0", "lines": 69, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
test
huggingface/diffusers:tests/pipelines/skyreels_v2/test_skyreels_v2.py
# Copyright 2024 The HuggingFace Team. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest import numpy as np import torch from transformers import AutoTokenizer, T5EncoderModel from diffusers import AutoencoderKLWan, SkyReelsV2Pipeline, SkyReelsV2Transformer3DModel, UniPCMultistepScheduler from ...testing_utils import enable_full_determinism from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS from ..test_pipelines_common import PipelineTesterMixin enable_full_determinism() class SkyReelsV2PipelineFastTests(PipelineTesterMixin, unittest.TestCase): pipeline_class = SkyReelsV2Pipeline params = TEXT_TO_IMAGE_PARAMS - {"cross_attention_kwargs"} batch_params = TEXT_TO_IMAGE_BATCH_PARAMS image_params = TEXT_TO_IMAGE_IMAGE_PARAMS image_latents_params = TEXT_TO_IMAGE_IMAGE_PARAMS required_optional_params = frozenset( [ "num_inference_steps", "generator", "latents", "return_dict", "callback_on_step_end", "callback_on_step_end_tensor_inputs", ] ) test_xformers_attention = False supports_dduf = False def get_dummy_components(self): torch.manual_seed(0) vae = AutoencoderKLWan( base_dim=3, z_dim=16, dim_mult=[1, 1, 1, 1], num_res_blocks=1, temperal_downsample=[False, True, True], ) torch.manual_seed(0) scheduler = UniPCMultistepScheduler(flow_shift=8.0, use_flow_sigmas=True) text_encoder = T5EncoderModel.from_pretrained("hf-internal-testing/tiny-random-t5") tokenizer = AutoTokenizer.from_pretrained("hf-internal-testing/tiny-random-t5") torch.manual_seed(0) transformer = SkyReelsV2Transformer3DModel( patch_size=(1, 2, 2), num_attention_heads=2, attention_head_dim=12, in_channels=16, out_channels=16, text_dim=32, freq_dim=256, ffn_dim=32, num_layers=2, cross_attn_norm=True, qk_norm="rms_norm_across_heads", rope_max_seq_len=32, ) components = { "transformer": transformer, "vae": vae, "scheduler": scheduler, "text_encoder": text_encoder, "tokenizer": tokenizer, } return components def get_dummy_inputs(self, device, seed=0): if str(device).startswith("mps"): generator = torch.manual_seed(seed) else: generator = torch.Generator(device=device).manual_seed(seed) inputs = { "prompt": "dance monkey", "negative_prompt": "negative", # TODO "generator": generator, "num_inference_steps": 2, "guidance_scale": 6.0, "height": 16, "width": 16, "num_frames": 9, "max_sequence_length": 16, "output_type": "pt", } return inputs def test_inference(self): device = "cpu" components = self.get_dummy_components() pipe = self.pipeline_class(**components) pipe.to(device) pipe.set_progress_bar_config(disable=None) inputs = self.get_dummy_inputs(device) video = pipe(**inputs).frames generated_video = video[0] self.assertEqual(generated_video.shape, (9, 3, 16, 16)) expected_video = torch.randn(9, 3, 16, 16) max_diff = np.abs(generated_video - expected_video).max() self.assertLessEqual(max_diff, 1e10) @unittest.skip("Test not supported") def test_attention_slicing_forward_pass(self): pass
{ "repo_id": "huggingface/diffusers", "file_path": "tests/pipelines/skyreels_v2/test_skyreels_v2.py", "license": "Apache License 2.0", "lines": 110, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
test
huggingface/diffusers:tests/pipelines/skyreels_v2/test_skyreels_v2_df.py
# Copyright 2024 The HuggingFace Team. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest import numpy as np import torch from transformers import AutoTokenizer, T5EncoderModel from diffusers import ( AutoencoderKLWan, SkyReelsV2DiffusionForcingPipeline, SkyReelsV2Transformer3DModel, UniPCMultistepScheduler, ) from ...testing_utils import enable_full_determinism from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS from ..test_pipelines_common import PipelineTesterMixin enable_full_determinism() class SkyReelsV2DiffusionForcingPipelineFastTests(PipelineTesterMixin, unittest.TestCase): pipeline_class = SkyReelsV2DiffusionForcingPipeline params = TEXT_TO_IMAGE_PARAMS - {"cross_attention_kwargs"} batch_params = TEXT_TO_IMAGE_BATCH_PARAMS image_params = TEXT_TO_IMAGE_IMAGE_PARAMS image_latents_params = TEXT_TO_IMAGE_IMAGE_PARAMS required_optional_params = frozenset( [ "num_inference_steps", "generator", "latents", "return_dict", "callback_on_step_end", "callback_on_step_end_tensor_inputs", ] ) test_xformers_attention = False supports_dduf = False def get_dummy_components(self): torch.manual_seed(0) vae = AutoencoderKLWan( base_dim=3, z_dim=16, dim_mult=[1, 1, 1, 1], num_res_blocks=1, temperal_downsample=[False, True, True], ) torch.manual_seed(0) scheduler = UniPCMultistepScheduler(flow_shift=8.0, use_flow_sigmas=True) text_encoder = T5EncoderModel.from_pretrained("hf-internal-testing/tiny-random-t5") tokenizer = AutoTokenizer.from_pretrained("hf-internal-testing/tiny-random-t5") torch.manual_seed(0) transformer = SkyReelsV2Transformer3DModel( patch_size=(1, 2, 2), num_attention_heads=2, attention_head_dim=12, in_channels=16, out_channels=16, text_dim=32, freq_dim=256, ffn_dim=32, num_layers=2, cross_attn_norm=True, qk_norm="rms_norm_across_heads", rope_max_seq_len=32, ) components = { "transformer": transformer, "vae": vae, "scheduler": scheduler, "text_encoder": text_encoder, "tokenizer": tokenizer, } return components def get_dummy_inputs(self, device, seed=0): if str(device).startswith("mps"): generator = torch.manual_seed(seed) else: generator = torch.Generator(device=device).manual_seed(seed) inputs = { "prompt": "dance monkey", "negative_prompt": "negative", # TODO "generator": generator, "num_inference_steps": 2, "guidance_scale": 6.0, "height": 16, "width": 16, "num_frames": 9, "max_sequence_length": 16, "output_type": "pt", } return inputs def test_inference(self): device = "cpu" components = self.get_dummy_components() pipe = self.pipeline_class(**components) pipe.to(device) pipe.set_progress_bar_config(disable=None) inputs = self.get_dummy_inputs(device) video = pipe(**inputs).frames generated_video = video[0] self.assertEqual(generated_video.shape, (9, 3, 16, 16)) expected_video = torch.randn(9, 3, 16, 16) max_diff = np.abs(generated_video - expected_video).max() self.assertLessEqual(max_diff, 1e10) @unittest.skip("Test not supported") def test_attention_slicing_forward_pass(self): pass
{ "repo_id": "huggingface/diffusers", "file_path": "tests/pipelines/skyreels_v2/test_skyreels_v2_df.py", "license": "Apache License 2.0", "lines": 115, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
test
huggingface/diffusers:tests/pipelines/skyreels_v2/test_skyreels_v2_df_image_to_video.py
# Copyright 2024 The HuggingFace Team. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest import numpy as np import torch from PIL import Image from transformers import AutoTokenizer, T5EncoderModel from diffusers import ( AutoencoderKLWan, SkyReelsV2DiffusionForcingImageToVideoPipeline, SkyReelsV2Transformer3DModel, UniPCMultistepScheduler, ) from ...testing_utils import enable_full_determinism from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS from ..test_pipelines_common import PipelineTesterMixin enable_full_determinism() class SkyReelsV2DiffusionForcingImageToVideoPipelineFastTests(PipelineTesterMixin, unittest.TestCase): pipeline_class = SkyReelsV2DiffusionForcingImageToVideoPipeline params = TEXT_TO_IMAGE_PARAMS - {"cross_attention_kwargs", "height", "width"} batch_params = TEXT_TO_IMAGE_BATCH_PARAMS image_params = TEXT_TO_IMAGE_IMAGE_PARAMS image_latents_params = TEXT_TO_IMAGE_IMAGE_PARAMS required_optional_params = frozenset( [ "num_inference_steps", "generator", "latents", "return_dict", "callback_on_step_end", "callback_on_step_end_tensor_inputs", ] ) test_xformers_attention = False supports_dduf = False def get_dummy_components(self): torch.manual_seed(0) vae = AutoencoderKLWan( base_dim=3, z_dim=16, dim_mult=[1, 1, 1, 1], num_res_blocks=1, temperal_downsample=[False, True, True], ) torch.manual_seed(0) scheduler = UniPCMultistepScheduler(flow_shift=5.0, use_flow_sigmas=True) text_encoder = T5EncoderModel.from_pretrained("hf-internal-testing/tiny-random-t5") tokenizer = AutoTokenizer.from_pretrained("hf-internal-testing/tiny-random-t5") torch.manual_seed(0) transformer = SkyReelsV2Transformer3DModel( patch_size=(1, 2, 2), num_attention_heads=2, attention_head_dim=12, in_channels=16, out_channels=16, text_dim=32, freq_dim=256, ffn_dim=32, num_layers=2, cross_attn_norm=True, qk_norm="rms_norm_across_heads", rope_max_seq_len=32, image_dim=4, ) components = { "transformer": transformer, "vae": vae, "scheduler": scheduler, "text_encoder": text_encoder, "tokenizer": tokenizer, } return components def get_dummy_inputs(self, device, seed=0): if str(device).startswith("mps"): generator = torch.manual_seed(seed) else: generator = torch.Generator(device=device).manual_seed(seed) image_height = 16 image_width = 16 image = Image.new("RGB", (image_width, image_height)) inputs = { "image": image, "prompt": "dance monkey", "negative_prompt": "negative", # TODO "height": image_height, "width": image_width, "generator": generator, "num_inference_steps": 2, "guidance_scale": 5.0, "num_frames": 9, "max_sequence_length": 16, "output_type": "pt", } return inputs def test_inference(self): device = "cpu" components = self.get_dummy_components() pipe = self.pipeline_class(**components) pipe.to(device) pipe.set_progress_bar_config(disable=None) inputs = self.get_dummy_inputs(device) video = pipe(**inputs).frames generated_video = video[0] self.assertEqual(generated_video.shape, (9, 3, 16, 16)) expected_video = torch.randn(9, 3, 16, 16) max_diff = np.abs(generated_video - expected_video).max() self.assertLessEqual(max_diff, 1e10) @unittest.skip("Test not supported") def test_attention_slicing_forward_pass(self): pass @unittest.skip("TODO: revisit failing as it requires a very high threshold to pass") def test_inference_batch_single_identical(self): pass class SkyReelsV2DiffusionForcingImageToVideoPipelineFastTests(SkyReelsV2DiffusionForcingImageToVideoPipelineFastTests): def get_dummy_components(self): torch.manual_seed(0) vae = AutoencoderKLWan( base_dim=3, z_dim=16, dim_mult=[1, 1, 1, 1], num_res_blocks=1, temperal_downsample=[False, True, True], ) torch.manual_seed(0) scheduler = UniPCMultistepScheduler(flow_shift=5.0, use_flow_sigmas=True) text_encoder = T5EncoderModel.from_pretrained("hf-internal-testing/tiny-random-t5") tokenizer = AutoTokenizer.from_pretrained("hf-internal-testing/tiny-random-t5") torch.manual_seed(0) transformer = SkyReelsV2Transformer3DModel( patch_size=(1, 2, 2), num_attention_heads=2, attention_head_dim=12, in_channels=16, out_channels=16, text_dim=32, freq_dim=256, ffn_dim=32, num_layers=2, cross_attn_norm=True, qk_norm="rms_norm_across_heads", rope_max_seq_len=32, image_dim=4, pos_embed_seq_len=2 * (4 * 4 + 1), ) components = { "transformer": transformer, "vae": vae, "scheduler": scheduler, "text_encoder": text_encoder, "tokenizer": tokenizer, } return components def get_dummy_inputs(self, device, seed=0): if str(device).startswith("mps"): generator = torch.manual_seed(seed) else: generator = torch.Generator(device=device).manual_seed(seed) image_height = 16 image_width = 16 image = Image.new("RGB", (image_width, image_height)) last_image = Image.new("RGB", (image_width, image_height)) inputs = { "image": image, "last_image": last_image, "prompt": "dance monkey", "negative_prompt": "negative", "height": image_height, "width": image_width, "generator": generator, "num_inference_steps": 2, "guidance_scale": 5.0, "num_frames": 9, "max_sequence_length": 16, "output_type": "pt", } return inputs
{ "repo_id": "huggingface/diffusers", "file_path": "tests/pipelines/skyreels_v2/test_skyreels_v2_df_image_to_video.py", "license": "Apache License 2.0", "lines": 187, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
test
huggingface/diffusers:tests/pipelines/skyreels_v2/test_skyreels_v2_df_video_to_video.py
# Copyright 2025 The HuggingFace Team. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import inspect import unittest import numpy as np import torch from PIL import Image from transformers import AutoTokenizer, T5EncoderModel from diffusers import ( AutoencoderKLWan, SkyReelsV2DiffusionForcingVideoToVideoPipeline, SkyReelsV2Transformer3DModel, UniPCMultistepScheduler, ) from ...testing_utils import enable_full_determinism, torch_device from ..pipeline_params import TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS from ..test_pipelines_common import PipelineTesterMixin enable_full_determinism() class SkyReelsV2DiffusionForcingVideoToVideoPipelineFastTests(PipelineTesterMixin, unittest.TestCase): pipeline_class = SkyReelsV2DiffusionForcingVideoToVideoPipeline params = TEXT_TO_IMAGE_PARAMS - {"cross_attention_kwargs"} batch_params = frozenset(["video", "prompt", "negative_prompt"]) image_latents_params = TEXT_TO_IMAGE_IMAGE_PARAMS required_optional_params = frozenset( [ "num_inference_steps", "generator", "latents", "return_dict", "callback_on_step_end", "callback_on_step_end_tensor_inputs", ] ) test_xformers_attention = False supports_dduf = False def get_dummy_components(self): torch.manual_seed(0) vae = AutoencoderKLWan( base_dim=3, z_dim=16, dim_mult=[1, 1, 1, 1], num_res_blocks=1, temperal_downsample=[False, True, True], ) torch.manual_seed(0) scheduler = UniPCMultistepScheduler(flow_shift=5.0, use_flow_sigmas=True) text_encoder = T5EncoderModel.from_pretrained("hf-internal-testing/tiny-random-t5") tokenizer = AutoTokenizer.from_pretrained("hf-internal-testing/tiny-random-t5") torch.manual_seed(0) transformer = SkyReelsV2Transformer3DModel( patch_size=(1, 2, 2), num_attention_heads=2, attention_head_dim=12, in_channels=16, out_channels=16, text_dim=32, freq_dim=256, ffn_dim=32, num_layers=2, cross_attn_norm=True, qk_norm="rms_norm_across_heads", rope_max_seq_len=32, ) components = { "transformer": transformer, "vae": vae, "scheduler": scheduler, "text_encoder": text_encoder, "tokenizer": tokenizer, } return components def get_dummy_inputs(self, device, seed=0): if str(device).startswith("mps"): generator = torch.manual_seed(seed) else: generator = torch.Generator(device=device).manual_seed(seed) video = [Image.new("RGB", (16, 16))] * 7 inputs = { "video": video, "prompt": "dance monkey", "negative_prompt": "negative", # TODO "generator": generator, "num_inference_steps": 4, "guidance_scale": 6.0, "height": 16, "width": 16, "max_sequence_length": 16, "output_type": "pt", "overlap_history": 3, "num_frames": 17, "base_num_frames": 5, } return inputs def test_inference(self): device = "cpu" components = self.get_dummy_components() pipe = self.pipeline_class(**components) pipe.to(device) pipe.set_progress_bar_config(disable=None) inputs = self.get_dummy_inputs(device) video = pipe(**inputs).frames generated_video = video[0] total_frames = len(inputs["video"]) + inputs["num_frames"] expected_shape = (total_frames, 3, 16, 16) self.assertEqual(generated_video.shape, expected_shape) expected_video = torch.randn(*expected_shape) max_diff = np.abs(generated_video - expected_video).max() self.assertLessEqual(max_diff, 1e10) def test_callback_cfg(self): sig = inspect.signature(self.pipeline_class.__call__) has_callback_tensor_inputs = "callback_on_step_end_tensor_inputs" in sig.parameters has_callback_step_end = "callback_on_step_end" in sig.parameters if not (has_callback_tensor_inputs and has_callback_step_end): return if "guidance_scale" not in sig.parameters: return components = self.get_dummy_components() pipe = self.pipeline_class(**components) pipe.to(torch_device) pipe.set_progress_bar_config(disable=None) self.assertTrue( hasattr(pipe, "_callback_tensor_inputs"), f" {self.pipeline_class} should have `_callback_tensor_inputs` that defines a list of tensor variables its callback function can use as inputs", ) # Track the number of callback calls for diffusion forcing pipelines callback_call_count = [0] # Use list to make it mutable in closure def callback_increase_guidance(pipe, i, t, callback_kwargs): pipe._guidance_scale += 1.0 callback_call_count[0] += 1 return callback_kwargs inputs = self.get_dummy_inputs(torch_device) # use cfg guidance because some pipelines modify the shape of the latents # outside of the denoising loop inputs["guidance_scale"] = 2.0 inputs["callback_on_step_end"] = callback_increase_guidance inputs["callback_on_step_end_tensor_inputs"] = pipe._callback_tensor_inputs _ = pipe(**inputs)[0] # For diffusion forcing pipelines, use the actual callback count # since they run multiple iterations with nested denoising loops expected_guidance_scale = inputs["guidance_scale"] + callback_call_count[0] assert pipe.guidance_scale == expected_guidance_scale @unittest.skip("Test not supported") def test_attention_slicing_forward_pass(self): pass @unittest.skip( "SkyReelsV2DiffusionForcingVideoToVideoPipeline has to run in mixed precision. Casting the entire pipeline will result in errors" ) def test_float16_inference(self): pass @unittest.skip( "SkyReelsV2DiffusionForcingVideoToVideoPipeline has to run in mixed precision. Save/Load the entire pipeline in FP16 will result in errors" ) def test_save_load_float16(self): pass
{ "repo_id": "huggingface/diffusers", "file_path": "tests/pipelines/skyreels_v2/test_skyreels_v2_df_video_to_video.py", "license": "Apache License 2.0", "lines": 165, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
test
huggingface/diffusers:tests/pipelines/skyreels_v2/test_skyreels_v2_image_to_video.py
# Copyright 2024 The HuggingFace Team. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest import numpy as np import torch from PIL import Image from transformers import ( AutoTokenizer, CLIPImageProcessor, CLIPVisionConfig, CLIPVisionModelWithProjection, T5EncoderModel, ) from diffusers import ( AutoencoderKLWan, SkyReelsV2ImageToVideoPipeline, SkyReelsV2Transformer3DModel, UniPCMultistepScheduler, ) from ...testing_utils import enable_full_determinism from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS from ..test_pipelines_common import PipelineTesterMixin enable_full_determinism() class SkyReelsV2ImageToVideoPipelineFastTests(PipelineTesterMixin, unittest.TestCase): pipeline_class = SkyReelsV2ImageToVideoPipeline params = TEXT_TO_IMAGE_PARAMS - {"cross_attention_kwargs", "height", "width"} batch_params = TEXT_TO_IMAGE_BATCH_PARAMS image_params = TEXT_TO_IMAGE_IMAGE_PARAMS image_latents_params = TEXT_TO_IMAGE_IMAGE_PARAMS required_optional_params = frozenset( [ "num_inference_steps", "generator", "latents", "return_dict", "callback_on_step_end", "callback_on_step_end_tensor_inputs", ] ) test_xformers_attention = False supports_dduf = False def get_dummy_components(self): torch.manual_seed(0) vae = AutoencoderKLWan( base_dim=3, z_dim=16, dim_mult=[1, 1, 1, 1], num_res_blocks=1, temperal_downsample=[False, True, True], ) torch.manual_seed(0) scheduler = UniPCMultistepScheduler(flow_shift=5.0, use_flow_sigmas=True) text_encoder = T5EncoderModel.from_pretrained("hf-internal-testing/tiny-random-t5") tokenizer = AutoTokenizer.from_pretrained("hf-internal-testing/tiny-random-t5") torch.manual_seed(0) transformer = SkyReelsV2Transformer3DModel( patch_size=(1, 2, 2), num_attention_heads=2, attention_head_dim=12, in_channels=36, out_channels=16, text_dim=32, freq_dim=256, ffn_dim=32, num_layers=2, cross_attn_norm=True, qk_norm="rms_norm_across_heads", rope_max_seq_len=32, image_dim=4, ) torch.manual_seed(0) image_encoder_config = CLIPVisionConfig( hidden_size=4, projection_dim=4, num_hidden_layers=2, num_attention_heads=2, image_size=32, intermediate_size=16, patch_size=1, ) image_encoder = CLIPVisionModelWithProjection(image_encoder_config) torch.manual_seed(0) image_processor = CLIPImageProcessor(crop_size=32, size=32) components = { "transformer": transformer, "vae": vae, "scheduler": scheduler, "text_encoder": text_encoder, "tokenizer": tokenizer, "image_encoder": image_encoder, "image_processor": image_processor, } return components def get_dummy_inputs(self, device, seed=0): if str(device).startswith("mps"): generator = torch.manual_seed(seed) else: generator = torch.Generator(device=device).manual_seed(seed) image_height = 16 image_width = 16 image = Image.new("RGB", (image_width, image_height)) inputs = { "image": image, "prompt": "dance monkey", "negative_prompt": "negative", # TODO "height": image_height, "width": image_width, "generator": generator, "num_inference_steps": 2, "guidance_scale": 6.0, "num_frames": 9, "max_sequence_length": 16, "output_type": "pt", } return inputs def test_inference(self): device = "cpu" components = self.get_dummy_components() pipe = self.pipeline_class(**components) pipe.to(device) pipe.set_progress_bar_config(disable=None) inputs = self.get_dummy_inputs(device) video = pipe(**inputs).frames generated_video = video[0] self.assertEqual(generated_video.shape, (9, 3, 16, 16)) expected_video = torch.randn(9, 3, 16, 16) max_diff = np.abs(generated_video - expected_video).max() self.assertLessEqual(max_diff, 1e10) def test_inference_with_last_image(self): device = "cpu" components = self.get_dummy_components() torch.manual_seed(0) components["transformer"] = SkyReelsV2Transformer3DModel( patch_size=(1, 2, 2), num_attention_heads=2, attention_head_dim=12, in_channels=36, out_channels=16, text_dim=32, freq_dim=256, ffn_dim=32, num_layers=2, cross_attn_norm=True, pos_embed_seq_len=2 * (4 * 4 + 1), qk_norm="rms_norm_across_heads", rope_max_seq_len=32, image_dim=4, ) torch.manual_seed(0) image_encoder_config = CLIPVisionConfig( hidden_size=4, projection_dim=4, num_hidden_layers=2, num_attention_heads=2, image_size=4, intermediate_size=16, patch_size=1, ) components["image_encoder"] = CLIPVisionModelWithProjection(image_encoder_config) torch.manual_seed(0) components["image_processor"] = CLIPImageProcessor(crop_size=4, size=4) pipe = self.pipeline_class(**components) pipe.to(device) pipe.set_progress_bar_config(disable=None) inputs = self.get_dummy_inputs(device) image_height = 16 image_width = 16 last_image = Image.new("RGB", (image_width, image_height)) inputs["last_image"] = last_image video = pipe(**inputs).frames generated_video = video[0] self.assertEqual(generated_video.shape, (9, 3, 16, 16)) expected_video = torch.randn(9, 3, 16, 16) max_diff = np.abs(generated_video - expected_video).max() self.assertLessEqual(max_diff, 1e10) @unittest.skip("Test not supported") def test_attention_slicing_forward_pass(self): pass @unittest.skip("TODO: revisit failing as it requires a very high threshold to pass") def test_inference_batch_single_identical(self): pass
{ "repo_id": "huggingface/diffusers", "file_path": "tests/pipelines/skyreels_v2/test_skyreels_v2_image_to_video.py", "license": "Apache License 2.0", "lines": 192, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
test
huggingface/diffusers:src/diffusers/quantizers/pipe_quant_config.py
# Copyright 2025 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from __future__ import annotations import inspect from ..utils import is_transformers_available, logging from .quantization_config import QuantizationConfigMixin as DiffQuantConfigMixin try: from transformers.utils.quantization_config import QuantizationConfigMixin as TransformersQuantConfigMixin except ImportError: class TransformersQuantConfigMixin: pass logger = logging.get_logger(__name__) class PipelineQuantizationConfig: """ Configuration class to be used when applying quantization on-the-fly to [`~DiffusionPipeline.from_pretrained`]. Args: quant_backend (`str`): Quantization backend to be used. When using this option, we assume that the backend is available to both `diffusers` and `transformers`. quant_kwargs (`dict`): Params to initialize the quantization backend class. components_to_quantize (`list`): Components of a pipeline to be quantized. quant_mapping (`dict`): Mapping defining the quantization specs to be used for the pipeline components. When using this argument, users are not expected to provide `quant_backend`, `quant_kawargs`, and `components_to_quantize`. """ def __init__( self, quant_backend: str = None, quant_kwargs: dict[str, str | float | int | dict] = None, components_to_quantize: list[str] | str | None = None, quant_mapping: dict[str, DiffQuantConfigMixin | "TransformersQuantConfigMixin"] = None, ): self.quant_backend = quant_backend # Initialize kwargs to be {} to set to the defaults. self.quant_kwargs = quant_kwargs or {} if components_to_quantize: if isinstance(components_to_quantize, str): components_to_quantize = [components_to_quantize] self.components_to_quantize = components_to_quantize self.quant_mapping = quant_mapping self.config_mapping = {} # book-keeping Example: `{module_name: quant_config}` self.post_init() def post_init(self): quant_mapping = self.quant_mapping self.is_granular = True if quant_mapping is not None else False self._validate_init_args() def _validate_init_args(self): if self.quant_backend and self.quant_mapping: raise ValueError("Both `quant_backend` and `quant_mapping` cannot be specified at the same time.") if not self.quant_mapping and not self.quant_backend: raise ValueError("Must provide a `quant_backend` when not providing a `quant_mapping`.") if not self.quant_kwargs and not self.quant_mapping: raise ValueError("Both `quant_kwargs` and `quant_mapping` cannot be None.") if self.quant_backend is not None: self._validate_init_kwargs_in_backends() if self.quant_mapping is not None: self._validate_quant_mapping_args() def _validate_init_kwargs_in_backends(self): quant_backend = self.quant_backend self._check_backend_availability(quant_backend) quant_config_mapping_transformers, quant_config_mapping_diffusers = self._get_quant_config_list() if quant_config_mapping_transformers is not None: init_kwargs_transformers = inspect.signature(quant_config_mapping_transformers[quant_backend].__init__) init_kwargs_transformers = {name for name in init_kwargs_transformers.parameters if name != "self"} else: init_kwargs_transformers = None init_kwargs_diffusers = inspect.signature(quant_config_mapping_diffusers[quant_backend].__init__) init_kwargs_diffusers = {name for name in init_kwargs_diffusers.parameters if name != "self"} if init_kwargs_transformers != init_kwargs_diffusers: raise ValueError( "The signatures of the __init__ methods of the quantization config classes in `diffusers` and `transformers` don't match. " f"Please provide a `quant_mapping` instead, in the {self.__class__.__name__} class. Refer to [the docs](https://huggingface.co/docs/diffusers/main/en/quantization/overview#pipeline-level-quantization) to learn more about how " "this mapping would look like." ) def _validate_quant_mapping_args(self): quant_mapping = self.quant_mapping transformers_map, diffusers_map = self._get_quant_config_list() available_transformers = list(transformers_map.values()) if transformers_map else None available_diffusers = list(diffusers_map.values()) for module_name, config in quant_mapping.items(): if any(isinstance(config, cfg) for cfg in available_diffusers): continue if available_transformers and any(isinstance(config, cfg) for cfg in available_transformers): continue if available_transformers: raise ValueError( f"Provided config for module_name={module_name} could not be found. " f"Available diffusers configs: {available_diffusers}; " f"Available transformers configs: {available_transformers}." ) else: raise ValueError( f"Provided config for module_name={module_name} could not be found. " f"Available diffusers configs: {available_diffusers}." ) def _check_backend_availability(self, quant_backend: str): quant_config_mapping_transformers, quant_config_mapping_diffusers = self._get_quant_config_list() available_backends_transformers = ( list(quant_config_mapping_transformers.keys()) if quant_config_mapping_transformers else None ) available_backends_diffusers = list(quant_config_mapping_diffusers.keys()) if ( available_backends_transformers and quant_backend not in available_backends_transformers ) or quant_backend not in quant_config_mapping_diffusers: error_message = f"Provided quant_backend={quant_backend} was not found." if available_backends_transformers: error_message += f"\nAvailable ones (transformers): {available_backends_transformers}." error_message += f"\nAvailable ones (diffusers): {available_backends_diffusers}." raise ValueError(error_message) def _resolve_quant_config(self, is_diffusers: bool = True, module_name: str = None): quant_config_mapping_transformers, quant_config_mapping_diffusers = self._get_quant_config_list() quant_mapping = self.quant_mapping components_to_quantize = self.components_to_quantize # Granular case if self.is_granular and module_name in quant_mapping: logger.debug(f"Initializing quantization config class for {module_name}.") config = quant_mapping[module_name] self.config_mapping.update({module_name: config}) return config # Global config case else: should_quantize = False # Only quantize the modules requested for. if components_to_quantize and module_name in components_to_quantize: should_quantize = True # No specification for `components_to_quantize` means all modules should be quantized. elif not self.is_granular and not components_to_quantize: should_quantize = True if should_quantize: logger.debug(f"Initializing quantization config class for {module_name}.") mapping_to_use = quant_config_mapping_diffusers if is_diffusers else quant_config_mapping_transformers quant_config_cls = mapping_to_use[self.quant_backend] quant_kwargs = self.quant_kwargs quant_obj = quant_config_cls(**quant_kwargs) self.config_mapping.update({module_name: quant_obj}) return quant_obj # Fallback: no applicable configuration found. return None def _get_quant_config_list(self): if is_transformers_available(): from transformers.quantizers.auto import ( AUTO_QUANTIZATION_CONFIG_MAPPING as quant_config_mapping_transformers, ) else: quant_config_mapping_transformers = None from ..quantizers.auto import AUTO_QUANTIZATION_CONFIG_MAPPING as quant_config_mapping_diffusers return quant_config_mapping_transformers, quant_config_mapping_diffusers def __repr__(self): out = "" config_mapping = dict(sorted(self.config_mapping.copy().items())) for module_name, config in config_mapping.items(): out += f"{module_name} {config}" return out
{ "repo_id": "huggingface/diffusers", "file_path": "src/diffusers/quantizers/pipe_quant_config.py", "license": "Apache License 2.0", "lines": 163, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
huggingface/diffusers:src/diffusers/commands/custom_blocks.py
# Copyright 2025 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """ Usage example: TODO """ import ast import importlib.util import os from argparse import ArgumentParser, Namespace from pathlib import Path from ..utils import logging from . import BaseDiffusersCLICommand EXPECTED_PARENT_CLASSES = ["ModularPipelineBlocks"] CONFIG = "config.json" def conversion_command_factory(args: Namespace): return CustomBlocksCommand(args.block_module_name, args.block_class_name) class CustomBlocksCommand(BaseDiffusersCLICommand): @staticmethod def register_subcommand(parser: ArgumentParser): conversion_parser = parser.add_parser("custom_blocks") conversion_parser.add_argument( "--block_module_name", type=str, default="block.py", help="Module filename in which the custom block will be implemented.", ) conversion_parser.add_argument( "--block_class_name", type=str, default=None, help="Name of the custom block. If provided None, we will try to infer it.", ) conversion_parser.set_defaults(func=conversion_command_factory) def __init__(self, block_module_name: str = "block.py", block_class_name: str = None): self.logger = logging.get_logger("diffusers-cli/custom_blocks") self.block_module_name = Path(block_module_name) self.block_class_name = block_class_name def run(self): # determine the block to be saved. out = self._get_class_names(self.block_module_name) classes_found = list({cls for cls, _ in out}) if self.block_class_name is not None: child_class, parent_class = self._choose_block(out, self.block_class_name) if child_class is None and parent_class is None: raise ValueError( "`block_class_name` could not be retrieved. Available classes from " f"{self.block_module_name}:\n{classes_found}" ) else: self.logger.info( f"Found classes: {classes_found} will be using {classes_found[0]}. " "If this needs to be changed, re-run the command specifying `block_class_name`." ) child_class, parent_class = out[0][0], out[0][1] # dynamically get the custom block and initialize it to call `save_pretrained` in the current directory. # the user is responsible for running it, so I guess that is safe? module_name = f"__dynamic__{self.block_module_name.stem}" spec = importlib.util.spec_from_file_location(module_name, str(self.block_module_name)) module = importlib.util.module_from_spec(spec) spec.loader.exec_module(module) getattr(module, child_class)().save_pretrained(os.getcwd()) # or, we could create it manually. # automap = self._create_automap(parent_class=parent_class, child_class=child_class) # with open(CONFIG, "w") as f: # json.dump(automap, f) with open("requirements.txt", "w") as f: f.write("") def _choose_block(self, candidates, chosen=None): for cls, base in candidates: if cls == chosen: return cls, base return None, None def _get_class_names(self, file_path): source = file_path.read_text(encoding="utf-8") try: tree = ast.parse(source, filename=file_path) except SyntaxError as e: raise ValueError(f"Could not parse {file_path!r}: {e}") from e results: list[tuple[str, str]] = [] for node in tree.body: if not isinstance(node, ast.ClassDef): continue # extract all base names for this class base_names = [bname for b in node.bases if (bname := self._get_base_name(b)) is not None] # for each allowed base that appears in the class's bases, emit a tuple for allowed in EXPECTED_PARENT_CLASSES: if allowed in base_names: results.append((node.name, allowed)) return results def _get_base_name(self, node: ast.expr): if isinstance(node, ast.Name): return node.id elif isinstance(node, ast.Attribute): val = self._get_base_name(node.value) return f"{val}.{node.attr}" if val else node.attr return None def _create_automap(self, parent_class, child_class): module = str(self.block_module_name).replace(".py", "").rsplit(".", 1)[-1] auto_map = {f"{parent_class}": f"{module}.{child_class}"} return {"auto_map": auto_map}
{ "repo_id": "huggingface/diffusers", "file_path": "src/diffusers/commands/custom_blocks.py", "license": "Apache License 2.0", "lines": 112, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
huggingface/diffusers:src/diffusers/guiders/adaptive_projected_guidance.py
# Copyright 2025 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from __future__ import annotations import math from typing import TYPE_CHECKING import torch from ..configuration_utils import register_to_config from .guider_utils import BaseGuidance, GuiderOutput, rescale_noise_cfg if TYPE_CHECKING: from ..modular_pipelines.modular_pipeline import BlockState class AdaptiveProjectedGuidance(BaseGuidance): """ Adaptive Projected Guidance (APG): https://huggingface.co/papers/2410.02416 Args: guidance_scale (`float`, defaults to `7.5`): The scale parameter for classifier-free guidance. Higher values result in stronger conditioning on the text prompt, while lower values allow for more freedom in generation. Higher values may lead to saturation and deterioration of image quality. adaptive_projected_guidance_momentum (`float`, defaults to `None`): The momentum parameter for the adaptive projected guidance. Disabled if set to `None`. adaptive_projected_guidance_rescale (`float`, defaults to `15.0`): The rescale factor applied to the noise predictions. This is used to improve image quality and fix guidance_rescale (`float`, defaults to `0.0`): The rescale factor applied to the noise predictions. This is used to improve image quality and fix overexposure. Based on Section 3.4 from [Common Diffusion Noise Schedules and Sample Steps are Flawed](https://huggingface.co/papers/2305.08891). use_original_formulation (`bool`, defaults to `False`): Whether to use the original formulation of classifier-free guidance as proposed in the paper. By default, we use the diffusers-native implementation that has been in the codebase for a long time. See [~guiders.classifier_free_guidance.ClassifierFreeGuidance] for more details. start (`float`, defaults to `0.0`): The fraction of the total number of denoising steps after which guidance starts. stop (`float`, defaults to `1.0`): The fraction of the total number of denoising steps after which guidance stops. """ _input_predictions = ["pred_cond", "pred_uncond"] @register_to_config def __init__( self, guidance_scale: float = 7.5, adaptive_projected_guidance_momentum: float | None = None, adaptive_projected_guidance_rescale: float = 15.0, eta: float = 1.0, guidance_rescale: float = 0.0, use_original_formulation: bool = False, start: float = 0.0, stop: float = 1.0, enabled: bool = True, ): super().__init__(start, stop, enabled) self.guidance_scale = guidance_scale self.adaptive_projected_guidance_momentum = adaptive_projected_guidance_momentum self.adaptive_projected_guidance_rescale = adaptive_projected_guidance_rescale self.eta = eta self.guidance_rescale = guidance_rescale self.use_original_formulation = use_original_formulation self.momentum_buffer = None def prepare_inputs(self, data: dict[str, tuple[torch.Tensor, torch.Tensor]]) -> list["BlockState"]: if self._step == 0: if self.adaptive_projected_guidance_momentum is not None: self.momentum_buffer = MomentumBuffer(self.adaptive_projected_guidance_momentum) tuple_indices = [0] if self.num_conditions == 1 else [0, 1] data_batches = [] for tuple_idx, input_prediction in zip(tuple_indices, self._input_predictions): data_batch = self._prepare_batch(data, tuple_idx, input_prediction) data_batches.append(data_batch) return data_batches def prepare_inputs_from_block_state( self, data: "BlockState", input_fields: dict[str, str | tuple[str, str]] ) -> list["BlockState"]: if self._step == 0: if self.adaptive_projected_guidance_momentum is not None: self.momentum_buffer = MomentumBuffer(self.adaptive_projected_guidance_momentum) tuple_indices = [0] if self.num_conditions == 1 else [0, 1] data_batches = [] for tuple_idx, input_prediction in zip(tuple_indices, self._input_predictions): data_batch = self._prepare_batch_from_block_state(input_fields, data, tuple_idx, input_prediction) data_batches.append(data_batch) return data_batches def forward(self, pred_cond: torch.Tensor, pred_uncond: torch.Tensor | None = None) -> GuiderOutput: pred = None if not self._is_apg_enabled(): pred = pred_cond else: pred = normalized_guidance( pred_cond, pred_uncond, self.guidance_scale, self.momentum_buffer, self.eta, self.adaptive_projected_guidance_rescale, self.use_original_formulation, ) if self.guidance_rescale > 0.0: pred = rescale_noise_cfg(pred, pred_cond, self.guidance_rescale) return GuiderOutput(pred=pred, pred_cond=pred_cond, pred_uncond=pred_uncond) @property def is_conditional(self) -> bool: return self._count_prepared == 1 @property def num_conditions(self) -> int: num_conditions = 1 if self._is_apg_enabled(): num_conditions += 1 return num_conditions def _is_apg_enabled(self) -> bool: if not self._enabled: return False is_within_range = True if self._num_inference_steps is not None: skip_start_step = int(self._start * self._num_inference_steps) skip_stop_step = int(self._stop * self._num_inference_steps) is_within_range = skip_start_step <= self._step < skip_stop_step is_close = False if self.use_original_formulation: is_close = math.isclose(self.guidance_scale, 0.0) else: is_close = math.isclose(self.guidance_scale, 1.0) return is_within_range and not is_close class MomentumBuffer: def __init__(self, momentum: float): self.momentum = momentum self.running_average = 0 def update(self, update_value: torch.Tensor): new_average = self.momentum * self.running_average self.running_average = update_value + new_average def __repr__(self) -> str: """ Returns a string representation showing momentum, shape, statistics, and a slice of the running_average. """ if isinstance(self.running_average, torch.Tensor): shape = tuple(self.running_average.shape) # Calculate statistics with torch.no_grad(): stats = { "mean": self.running_average.mean().item(), "std": self.running_average.std().item(), "min": self.running_average.min().item(), "max": self.running_average.max().item(), } # Get a slice (max 3 elements per dimension) slice_indices = tuple(slice(None, min(3, dim)) for dim in shape) sliced_data = self.running_average[slice_indices] # Format the slice for display (convert to float32 for numpy compatibility with bfloat16) slice_str = str(sliced_data.detach().float().cpu().numpy()) if len(slice_str) > 200: # Truncate if too long slice_str = slice_str[:200] + "..." stats_str = ", ".join([f"{k}={v:.4f}" for k, v in stats.items()]) return ( f"MomentumBuffer(\n" f" momentum={self.momentum},\n" f" shape={shape},\n" f" stats=[{stats_str}],\n" f" slice={slice_str}\n" f")" ) else: return f"MomentumBuffer(momentum={self.momentum}, running_average={self.running_average})" def normalized_guidance( pred_cond: torch.Tensor, pred_uncond: torch.Tensor, guidance_scale: float, momentum_buffer: MomentumBuffer | None = None, eta: float = 1.0, norm_threshold: float = 0.0, use_original_formulation: bool = False, ): diff = pred_cond - pred_uncond dim = [-i for i in range(1, len(diff.shape))] if momentum_buffer is not None: momentum_buffer.update(diff) diff = momentum_buffer.running_average if norm_threshold > 0: ones = torch.ones_like(diff) diff_norm = diff.norm(p=2, dim=dim, keepdim=True) scale_factor = torch.minimum(ones, norm_threshold / diff_norm) diff = diff * scale_factor v0, v1 = diff.double(), pred_cond.double() v1 = torch.nn.functional.normalize(v1, dim=dim) v0_parallel = (v0 * v1).sum(dim=dim, keepdim=True) * v1 v0_orthogonal = v0 - v0_parallel diff_parallel, diff_orthogonal = v0_parallel.type_as(diff), v0_orthogonal.type_as(diff) normalized_update = diff_orthogonal + eta * diff_parallel pred = pred_cond if use_original_formulation else pred_uncond pred = pred + guidance_scale * normalized_update return pred
{ "repo_id": "huggingface/diffusers", "file_path": "src/diffusers/guiders/adaptive_projected_guidance.py", "license": "Apache License 2.0", "lines": 197, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
huggingface/diffusers:src/diffusers/guiders/auto_guidance.py
# Copyright 2025 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from __future__ import annotations import math from typing import TYPE_CHECKING, Any import torch from ..configuration_utils import register_to_config from ..hooks import HookRegistry, LayerSkipConfig from ..hooks.layer_skip import _apply_layer_skip_hook from .guider_utils import BaseGuidance, GuiderOutput, rescale_noise_cfg if TYPE_CHECKING: from ..modular_pipelines.modular_pipeline import BlockState class AutoGuidance(BaseGuidance): """ AutoGuidance: https://huggingface.co/papers/2406.02507 Args: guidance_scale (`float`, defaults to `7.5`): The scale parameter for classifier-free guidance. Higher values result in stronger conditioning on the text prompt, while lower values allow for more freedom in generation. Higher values may lead to saturation and deterioration of image quality. auto_guidance_layers (`int` or `list[int]`, *optional*): The layer indices to apply skip layer guidance to. Can be a single integer or a list of integers. If not provided, `skip_layer_config` must be provided. auto_guidance_config (`LayerSkipConfig` or `list[LayerSkipConfig]`, *optional*): The configuration for the skip layer guidance. Can be a single `LayerSkipConfig` or a list of `LayerSkipConfig`. If not provided, `skip_layer_guidance_layers` must be provided. dropout (`float`, *optional*): The dropout probability for autoguidance on the enabled skip layers (either with `auto_guidance_layers` or `auto_guidance_config`). If not provided, the dropout probability will be set to 1.0. guidance_rescale (`float`, defaults to `0.0`): The rescale factor applied to the noise predictions. This is used to improve image quality and fix overexposure. Based on Section 3.4 from [Common Diffusion Noise Schedules and Sample Steps are Flawed](https://huggingface.co/papers/2305.08891). use_original_formulation (`bool`, defaults to `False`): Whether to use the original formulation of classifier-free guidance as proposed in the paper. By default, we use the diffusers-native implementation that has been in the codebase for a long time. See [~guiders.classifier_free_guidance.ClassifierFreeGuidance] for more details. start (`float`, defaults to `0.0`): The fraction of the total number of denoising steps after which guidance starts. stop (`float`, defaults to `1.0`): The fraction of the total number of denoising steps after which guidance stops. """ _input_predictions = ["pred_cond", "pred_uncond"] @register_to_config def __init__( self, guidance_scale: float = 7.5, auto_guidance_layers: int | list[int] | None = None, auto_guidance_config: LayerSkipConfig | list[LayerSkipConfig] | dict[str, Any] = None, dropout: float | None = None, guidance_rescale: float = 0.0, use_original_formulation: bool = False, start: float = 0.0, stop: float = 1.0, enabled: bool = True, ): super().__init__(start, stop, enabled) self.guidance_scale = guidance_scale self.auto_guidance_layers = auto_guidance_layers self.auto_guidance_config = auto_guidance_config self.dropout = dropout self.guidance_rescale = guidance_rescale self.use_original_formulation = use_original_formulation is_layer_or_config_provided = auto_guidance_layers is not None or auto_guidance_config is not None is_layer_and_config_provided = auto_guidance_layers is not None and auto_guidance_config is not None if not is_layer_or_config_provided: raise ValueError( "Either `auto_guidance_layers` or `auto_guidance_config` must be provided to enable AutoGuidance." ) if is_layer_and_config_provided: raise ValueError("Only one of `auto_guidance_layers` or `auto_guidance_config` can be provided.") if auto_guidance_config is None and dropout is None: raise ValueError("`dropout` must be provided if `auto_guidance_layers` is provided.") if auto_guidance_layers is not None: if isinstance(auto_guidance_layers, int): auto_guidance_layers = [auto_guidance_layers] if not isinstance(auto_guidance_layers, list): raise ValueError( f"Expected `auto_guidance_layers` to be an int or a list of ints, but got {type(auto_guidance_layers)}." ) auto_guidance_config = [ LayerSkipConfig(layer, fqn="auto", dropout=dropout) for layer in auto_guidance_layers ] if isinstance(auto_guidance_config, dict): auto_guidance_config = LayerSkipConfig.from_dict(auto_guidance_config) if isinstance(auto_guidance_config, LayerSkipConfig): auto_guidance_config = [auto_guidance_config] if not isinstance(auto_guidance_config, list): raise ValueError( f"Expected `auto_guidance_config` to be a LayerSkipConfig or a list of LayerSkipConfig, but got {type(auto_guidance_config)}." ) elif isinstance(next(iter(auto_guidance_config), None), dict): auto_guidance_config = [LayerSkipConfig.from_dict(config) for config in auto_guidance_config] self.auto_guidance_config = auto_guidance_config self._auto_guidance_hook_names = [f"AutoGuidance_{i}" for i in range(len(self.auto_guidance_config))] def prepare_models(self, denoiser: torch.nn.Module) -> None: self._count_prepared += 1 if self._is_ag_enabled() and self.is_unconditional: for name, config in zip(self._auto_guidance_hook_names, self.auto_guidance_config): _apply_layer_skip_hook(denoiser, config, name=name) def cleanup_models(self, denoiser: torch.nn.Module) -> None: if self._is_ag_enabled() and self.is_unconditional: for name in self._auto_guidance_hook_names: registry = HookRegistry.check_if_exists_or_initialize(denoiser) registry.remove_hook(name, recurse=True) def prepare_inputs(self, data: dict[str, tuple[torch.Tensor, torch.Tensor]]) -> list["BlockState"]: tuple_indices = [0] if self.num_conditions == 1 else [0, 1] data_batches = [] for tuple_idx, input_prediction in zip(tuple_indices, self._input_predictions): data_batch = self._prepare_batch(data, tuple_idx, input_prediction) data_batches.append(data_batch) return data_batches def prepare_inputs_from_block_state( self, data: "BlockState", input_fields: dict[str, str | tuple[str, str]] ) -> list["BlockState"]: tuple_indices = [0] if self.num_conditions == 1 else [0, 1] data_batches = [] for tuple_idx, input_prediction in zip(tuple_indices, self._input_predictions): data_batch = self._prepare_batch_from_block_state(input_fields, data, tuple_idx, input_prediction) data_batches.append(data_batch) return data_batches def forward(self, pred_cond: torch.Tensor, pred_uncond: torch.Tensor | None = None) -> GuiderOutput: pred = None if not self._is_ag_enabled(): pred = pred_cond else: shift = pred_cond - pred_uncond pred = pred_cond if self.use_original_formulation else pred_uncond pred = pred + self.guidance_scale * shift if self.guidance_rescale > 0.0: pred = rescale_noise_cfg(pred, pred_cond, self.guidance_rescale) return GuiderOutput(pred=pred, pred_cond=pred_cond, pred_uncond=pred_uncond) @property def is_conditional(self) -> bool: return self._count_prepared == 1 @property def num_conditions(self) -> int: num_conditions = 1 if self._is_ag_enabled(): num_conditions += 1 return num_conditions def _is_ag_enabled(self) -> bool: if not self._enabled: return False is_within_range = True if self._num_inference_steps is not None: skip_start_step = int(self._start * self._num_inference_steps) skip_stop_step = int(self._stop * self._num_inference_steps) is_within_range = skip_start_step <= self._step < skip_stop_step is_close = False if self.use_original_formulation: is_close = math.isclose(self.guidance_scale, 0.0) else: is_close = math.isclose(self.guidance_scale, 1.0) return is_within_range and not is_close
{ "repo_id": "huggingface/diffusers", "file_path": "src/diffusers/guiders/auto_guidance.py", "license": "Apache License 2.0", "lines": 166, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license