From 9cc751a13f60bd31e1895765b36af10c5457d409 Mon Sep 17 00:00:00 2001 From: tdrussell <6509934+tdrussell@users.noreply.github.com> Date: Sun, 8 Dec 2024 17:41:42 -0600 Subject: [PATCH 1/2] LoRA support --- nodes.py | 178 +++++++++++++++++++++++++++++++++---------------------- 1 file changed, 108 insertions(+), 70 deletions(-) diff --git a/nodes.py b/nodes.py index a633f1e..397f42a 100644 --- a/nodes.py +++ b/nodes.py @@ -1,9 +1,8 @@ import os import torch import json -from typing import List import gc -from .utils import log, check_diffusers_version, print_memory +from .utils import log, print_memory from diffusers.video_processor import VideoProcessor from .hyvideo.constants import PROMPT_TEMPLATE @@ -20,6 +19,8 @@ from accelerate.utils import set_module_tensor_to_device import folder_paths import comfy.model_management as mm from comfy.utils import load_torch_file +import comfy.model_base +import comfy.latent_formats script_directory = os.path.dirname(os.path.abspath(__file__)) @@ -56,7 +57,7 @@ def get_rotary_pos_embed(transformer, video_length, height, width): if len(rope_sizes) != target_ndim: rope_sizes = [1] * (target_ndim - len(rope_sizes)) + rope_sizes # time axis - + if rope_dim_list is None: rope_dim_list = [head_dim // target_ndim for _ in range(target_ndim)] assert ( @@ -75,7 +76,7 @@ class HyVideoBlockSwap: @classmethod def INPUT_TYPES(s): return { - "required": { + "required": { "double_blocks_to_swap": ("INT", {"default": 20, "min": 0, "max": 20, "step": 1, "tooltip": "Number of double blocks to swap"}), "single_blocks_to_swap": ("INT", {"default": 0, "min": 0, "max": 40, "step": 1, "tooltip": "Number of single blocks to swap"}), "offload_txt_in": ("BOOLEAN", {"default": False, "tooltip": "Offload txt_in layer"}), @@ -95,7 +96,7 @@ class HyVideoSTG: @classmethod def INPUT_TYPES(s): return { - "required": { + "required": { "stg_mode": (["STG-A", "STG-R"],), "stg_block_idx": ("INT", {"default": 0, "min": -1, "max": 39, "step": 1, "tooltip": "Block index to apply STG"}), "stg_scale": ("FLOAT", {"default": 1.0, "min": 0.0, "max": 10.0, "step": 0.01, "tooltip": "Recommended values are ≤2.0"}), @@ -112,6 +113,33 @@ class HyVideoSTG: def setargs(self, **kwargs): return (kwargs, ) + +class HyVideoModel(comfy.model_base.BaseModel): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.pipeline = {} + + def __getitem__(self, k): + return self.pipeline[k] + + def __setitem__(self, k, v): + self.pipeline[k] = v + + +class HyVideoModelConfig: + def __init__(self, dtype): + self.unet_config = {} + self.unet_extra_config = {} + self.latent_format = comfy.latent_formats.LatentFormat() + self.latent_format.latent_channels = 16 + self.manual_cast_dtype = dtype + self.sampling_settings = {"multiplier": 1.0} + # Don't know what this is. Value taken from ComfyUI Mochi model. + self.memory_usage_factor = 2.0 + # denoiser is handled by extension + self.unet_config["disable_unet_model_creation"] = True + + #region Model loading class HyVideoModelLoader: @classmethod @@ -119,7 +147,7 @@ class HyVideoModelLoader: return { "required": { "model": (folder_paths.get_filename_list("diffusion_models"), {"tooltip": "These models are loaded from the 'ComfyUI/models/diffusion_models' -folder",}), - + "base_precision": (["fp16", "fp32", "bf16"], {"default": "bf16"}), "quantization": (['disabled', 'fp8_e4m3fn', 'fp8_e4m3fn_fast', 'torchao_fp8dq', "torchao_fp8dqrow", "torchao_int8dq", "torchao_fp6", "torchao_int4", "torchao_int8"], {"default": 'disabled', "tooltip": "optional quantization method"}), "load_device": (["main_device", "offload_device"], {"default": "main_device"}), @@ -136,7 +164,7 @@ class HyVideoModelLoader: } } - RETURN_TYPES = ("HYVIDEOMODEL",) + RETURN_TYPES = ("MODEL",) RETURN_NAMES = ("model", ) FUNCTION = "loadmodel" CATEGORY = "HunyuanVideoWrapper" @@ -202,7 +230,7 @@ class HyVideoModelLoader: # if any(sub in fqn for sub in target_submodules): # return isinstance(module, nn.Linear) # return False - + if "fp6" in quantization: quant_func = fpx_weight_only(3, 2) elif "int4" in quantization: @@ -240,7 +268,7 @@ class HyVideoModelLoader: for name, param in transformer.named_parameters(): if "single_blocks" not in name and "double_blocks" not in name: set_module_tensor_to_device(transformer, name, device=transformer_load_device, dtype=base_dtype, value=sd[name]) - + manual_offloading = False # to disable manual .to(device) calls log.info(f"Quantized transformer blocks to {quantization}") for name, param in transformer.named_parameters(): @@ -256,12 +284,12 @@ class HyVideoModelLoader: for name, param in transformer.named_parameters(): dtype_to_use = base_dtype if any(keyword in name for keyword in params_to_keep) else dtype set_module_tensor_to_device(transformer, name, device=transformer_load_device, dtype=dtype_to_use, value=sd[name]) - + if quantization == "fp8_e4m3fn_fast": from .fp8_optimization import convert_fp8_linear convert_fp8_linear(transformer, base_dtype, params_to_keep=params_to_keep) - + #compile if compile_args is not None: torch._dynamo.config.cache_size_limit = compile_args["dynamo_cache_size_limit"] @@ -286,24 +314,31 @@ class HyVideoModelLoader: reverse=True, solver="euler", ) - + pipe = HunyuanVideoPipeline( transformer=transformer, scheduler=scheduler, progress_bar_config=None ) - - pipeline = { - "pipe": pipe, - "dtype": base_dtype, - "base_path": model_path, - "model_name": model, - "manual_offloading": manual_offloading, - "quantization": "disabled", - "block_swap_args": block_swap_args, - } - return (pipeline,) - + + comfy_model = HyVideoModel( + HyVideoModelConfig(base_dtype), + model_type=comfy.model_base.ModelType.FLOW, + device=mm.get_torch_device(), + ) + comfy_model["pipe"] = pipe + comfy_model["dtype"] = base_dtype + comfy_model["base_path"] = model_path + comfy_model["model_name"] = model + comfy_model["manual_offloading"] = manual_offloading + comfy_model["quantization"] = "disabled" + comfy_model["block_swap_args"] = block_swap_args + comfy_model.diffusion_model = transformer + + patcher = comfy.model_patcher.ModelPatcher(comfy_model, device, offload_device) + + return (patcher,) + #region load VAE class HyVideoVAELoader: @@ -328,7 +363,7 @@ class HyVideoVAELoader: DESCRIPTION = "Loads Hunyuan VAE model from 'ComfyUI/models/vae'" def loadmodel(self, model_name, precision, compile_args=None): - + device = mm.get_torch_device() offload_device = mm.unet_offload_device() @@ -344,21 +379,21 @@ class HyVideoVAELoader: vae.requires_grad_(False) vae.eval() vae.to(device = device, dtype = dtype) - + #compile if compile_args is not None: torch._dynamo.config.cache_size_limit = compile_args["dynamo_cache_size_limit"] vae = torch.compile(vae, fullgraph=compile_args["fullgraph"], dynamic=compile_args["dynamic"], backend=compile_args["backend"], mode=compile_args["mode"]) return (vae,) - - + + class HyVideoTorchCompileSettings: @classmethod def INPUT_TYPES(s): return { - "required": { + "required": { "backend": (["inductor","cudagraphs"], {"default": "inductor"}), "fullgraph": ("BOOLEAN", {"default": False, "tooltip": "Enable full graph mode"}), "mode": (["default", "max-autotune", "max-autotune-no-cudagraphs", "reduce-overhead"], {"default": "default"}), @@ -394,7 +429,7 @@ class HyVideoTorchCompileSettings: } return (compile_args, ) - + #region TextEncode class DownloadAndLoadHyVideoTextEncoder: @@ -404,7 +439,7 @@ class DownloadAndLoadHyVideoTextEncoder: "required": { "llm_model": (["Kijai/llava-llama-3-8b-text-encoder-tokenizer",],), "clip_model": (["disabled","openai/clip-vit-large-patch14",],), - + "precision": (["fp16", "fp32", "bf16"], {"default": "bf16"} ), @@ -423,7 +458,7 @@ class DownloadAndLoadHyVideoTextEncoder: DESCRIPTION = "Loads Hunyuan text_encoder model from 'ComfyUI/models/LLM'" def loadmodel(self, llm_model, clip_model, precision, apply_final_norm=False, hidden_state_skip_layer=2, quantization="disabled"): - + device = mm.get_torch_device() offload_device = mm.unet_offload_device() dtype = {"bf16": torch.bfloat16, "fp16": torch.float16, "fp32": torch.float32}[precision] @@ -472,7 +507,7 @@ class DownloadAndLoadHyVideoTextEncoder: local_dir=base_path, local_dir_use_symlinks=False, ) - + text_encoder = TextEncoder( text_encoder_path=base_path, text_encoder_type="llm", @@ -486,13 +521,13 @@ class DownloadAndLoadHyVideoTextEncoder: dtype=dtype, quantization_config=quantization_config ) - - + + hyvid_text_encoders = { "text_encoder": text_encoder, "text_encoder_2": text_encoder_2, } - + return (hyvid_text_encoders,) class HyVideoCustomPromptTemplate: @@ -515,7 +550,7 @@ class HyVideoCustomPromptTemplate: "crop_start": crop_start, } return (prompt_template_dict,) - + class HyVideoTextEncode: @classmethod def INPUT_TYPES(s): @@ -548,7 +583,7 @@ class HyVideoTextEncode: text_encoder_2 = None negative_prompt = None - + if prompt_template != "disabled": if prompt_template == "custom": prompt_template_dict = custom_prompt_template @@ -573,12 +608,12 @@ class HyVideoTextEncode: batch_size = 1 num_videos_per_prompt = 1 do_classifier_free_guidance = False # not implemented, for now we only have cfg distilled model - + text_inputs = text_encoder.text2tokens(prompt, prompt_template=prompt_template_dict) prompt_outputs = text_encoder.encode(text_inputs, prompt_template=prompt_template_dict, device=device) prompt_embeds = prompt_outputs.hidden_state - + attention_mask = prompt_outputs.attention_mask log.info(f"{text_encoder.text_encoder_type} prompt attention_mask shape: {attention_mask.shape}, masked tokens: {attention_mask[0].sum().item()}") if attention_mask is not None: @@ -695,7 +730,7 @@ class HyVideoTextEncode: prompt_embeds_2 = prompt_embeds_2.to(device=device) negative_prompt_embeds_2, attention_mask_2, negative_attention_mask_2 = None, None, None - + if force_offload: clip_l.cond_stage_model.to(offload_device) mm.soft_empty_cache() @@ -704,7 +739,7 @@ class HyVideoTextEncode: negative_prompt_embeds_2 = None attention_mask_2 = None negative_attention_mask_2 = None - + prompt_embeds_dict = { "prompt_embeds": prompt_embeds, "negative_prompt_embeds": negative_prompt_embeds, @@ -716,15 +751,15 @@ class HyVideoTextEncode: "negative_attention_mask_2": negative_attention_mask_2, } return (prompt_embeds_dict,) - -#region Sampler + +#region Sampler class HyVideoSampler: @classmethod def INPUT_TYPES(s): return { "required": { - "model": ("HYVIDEOMODEL",), + "model": ("MODEL",), "hyvid_embeds": ("HYVIDEMBEDS", ), "width": ("INT", {"default": 512, "min": 64, "max": 4096, "step": 16}), "height": ("INT", {"default": 512, "min": 64, "max": 4096, "step": 16}), @@ -734,7 +769,7 @@ class HyVideoSampler: "flow_shift": ("FLOAT", {"default": 9.0, "min": 0.0, "max": 30.0, "step": 0.01}), "seed": ("INT", {"default": 0, "min": 0, "max": 0xffffffffffffffff}), "force_offload": ("BOOLEAN", {"default": True}), - + }, "optional": { "samples": ("LATENT", {"tooltip": "init Latents to use for video2video process"} ), @@ -749,6 +784,10 @@ class HyVideoSampler: CATEGORY = "HunyuanVideoWrapper" def process(self, model, hyvid_embeds, flow_shift, steps, embedded_guidance_scale, seed, width, height, num_frames, samples=None, denoise_strength=1.0, force_offload=True, stg_args=None): + # We only need this so that LoRA weights get patched. + comfy.model_management.load_models_gpu([model]) + model = model.model + device = mm.get_torch_device() offload_device = mm.unet_offload_device() dtype = model["dtype"] @@ -759,7 +798,7 @@ class HyVideoSampler: raise ValueError( f"STG-A requires attention_mode to be 'sdpa', but got {transformer.attention_mode}." ) - + generator = torch.Generator(device=torch.device("cpu")).manual_seed(seed) if width <= 0 or height <= 0 or num_frames <= 0: @@ -784,7 +823,7 @@ class HyVideoSampler: n_tokens = freqs_cos.shape[0] model["pipe"].scheduler.shift = flow_shift - + # autocast_context = torch.autocast( # mm.get_autocast_device(device), dtype=dtype # ) if any(q in model["quantization"] for q in ("e4m3fn", "GGUF")) else nullcontext() @@ -794,9 +833,9 @@ class HyVideoSampler: #print(name, param.data.device) if "single" not in name and "double" not in name: param.data = param.data.to(device) - + transformer.block_swap( - model["block_swap_args"]["double_blocks_to_swap"] - 1 , + model["block_swap_args"]["double_blocks_to_swap"] - 1 , model["block_swap_args"]["single_blocks_to_swap"] - 1, offload_txt_in = model["block_swap_args"]["offload_txt_in"], offload_img_in = model["block_swap_args"]["offload_img_in"], @@ -807,18 +846,17 @@ class HyVideoSampler: elif model["manual_offloading"]: transformer.to(device) - mm.unload_all_models() mm.soft_empty_cache() gc.collect() - + try: torch.cuda.reset_peak_memory_stats(device) except: pass - + #for name, param in transformer.named_parameters(): # print(name, param.data.device) - + out_latents = model["pipe"]( num_inference_steps=steps, height = target_height, @@ -844,7 +882,7 @@ class HyVideoSampler: torch.cuda.reset_peak_memory_stats(device) except: pass - + if force_offload: if model["manual_offloading"]: transformer.to(offload_device) @@ -855,8 +893,8 @@ class HyVideoSampler: "samples": out_latents },) - -#region VideoDecode + +#region VideoDecode class HyVideoDecode: @classmethod def INPUT_TYPES(s): @@ -865,7 +903,7 @@ class HyVideoDecode: "samples": ("LATENT",), "enable_vae_tiling": ("BOOLEAN", {"default": True, "tooltip": "Drastically reduces memory use but may introduce seams"}), "temporal_tiling_sample_size": ("INT", {"default": 16, "min": 4, "max": 256, "tooltip": "Smaller values use less VRAM, model default is 64 which doesn't fit on most GPUs"}), - }, + }, } RETURN_TYPES = ("IMAGE",) @@ -881,8 +919,8 @@ class HyVideoDecode: generator = torch.Generator(device=torch.device("cpu"))#.manual_seed(seed) vae.to(device) vae.sample_tsize = temporal_tiling_sample_size - - + + expand_temporal_dim = False if len(latents.shape) == 4: if isinstance(vae, AutoencoderKLCausal3D): @@ -925,7 +963,7 @@ class HyVideoDecode: out = out.permute(0, 2, 3, 1).cpu().float() return (out,) - + class HyVideoEncode: @classmethod def INPUT_TYPES(s): @@ -934,7 +972,7 @@ class HyVideoEncode: "image": ("IMAGE",), "enable_vae_tiling": ("BOOLEAN", {"default": True, "tooltip": "Drastically reduces memory use but may introduce seams"}), "temporal_tiling_sample_size": ("INT", {"default": 16, "min": 4, "max": 256, "tooltip": "Smaller values use less VRAM, model default is 64 which doesn't fit on most GPUs"}), - }, + }, } RETURN_TYPES = ("LATENT",) @@ -945,7 +983,7 @@ class HyVideoEncode: def encode(self, vae, image, enable_vae_tiling, temporal_tiling_sample_size): device = mm.get_torch_device() offload_device = mm.unet_offload_device() - + generator = torch.Generator(device=torch.device("cpu"))#.manual_seed(seed) vae.to(device) vae.sample_tsize = temporal_tiling_sample_size @@ -956,8 +994,8 @@ class HyVideoEncode: latents = vae.encode(image).latent_dist.sample(generator) latents = latents * vae.config.scaling_factor vae.to(offload_device) - print("encoded latents shape",latents.shape) - + print("encoded latents shape",latents.shape) + return ({"samples": latents},) @@ -987,7 +1025,7 @@ class CogVideoLatentPreview: latents = samples["samples"].clone() print("in sample", latents.shape) latents = latents.permute(0, 2, 1, 3, 4) # [batch_size, num_channels, num_frames, height, width] - + #[[0.0658900170023352, 0.04687556512203313, -0.056971557475649186], [-0.01265770449940036, -0.02814809569100843, -0.0768912512529372], [0.061456544746314665, 0.0005511617552452358, -0.0652574975291287], [-0.09020669168815276, -0.004755440180558637, -0.023763970904494294], [0.031766964513999865, -0.030959599938418375, 0.08654669098083616], [-0.005981764690055846, -0.08809119252349802, -0.06439852368217663], [-0.0212114426433989, 0.08894281999597677, 0.05155629477559985], [-0.013947446911030725, -0.08987475069900677, -0.08923124751217484], [-0.08235967967978511, 0.07268025379974379, 0.08830486164536037], [-0.08052049179735378, -0.050116143175332195, 0.02023752569687405], [-0.07607527759162447, 0.06827156419895981, 0.08678111754261035], [-0.04689089232553825, 0.017294986041038893, -0.10280492336438908], [-0.06105783150270304, 0.07311850680875913, 0.019995735372550075], [-0.09232589996527711, -0.012869815059053047, -0.04355587834255975], [-0.06679931010802251, 0.018399815879067458, 0.06802404982033876], [-0.013062632927118165, -0.04292991477896661, 0.07476243356192845]] latent_rgb_factors =[[0.11945946736445662, 0.09919175788574555, -0.004832707433877734], [-0.0011977028264356232, 0.05496505130267682, 0.021321622433638193], [-0.014088548986590666, -0.008701477861945644, -0.020991313281459367], [0.03063921972519621, 0.12186477097625073, 0.0139593690235148], [0.0927403067854673, 0.030293187650929136, 0.05083134241694003], [0.0379112441305742, 0.04935199882777209, 0.058562766246777774], [0.017749911959153715, 0.008839453404921545, 0.036005638019226294], [0.10610119248526109, 0.02339855688237826, 0.057154257614084596], [0.1273639464837117, -0.010959856130713416, 0.043268631260428896], [-0.01873510946881321, 0.08220930648486932, 0.10613256772247093], [0.008429116376722327, 0.07623856561000408, 0.09295712117576727], [0.12938137079617007, 0.12360403483892413, 0.04478930933220116], [0.04565908794779364, 0.041064156741596365, -0.017695041535528512], [0.00019003240570281826, -0.013965147883381978, 0.05329669529635849], [0.08082391586738358, 0.11548306825496074, -0.021464170006615893], [-0.01517932393230994, -0.0057985555313003236, 0.07216646476618871]] import random @@ -995,10 +1033,10 @@ class CogVideoLatentPreview: latent_rgb_factors = [[random.uniform(min_val, max_val) for _ in range(3)] for _ in range(16)] out_factors = latent_rgb_factors print(latent_rgb_factors) - + latent_rgb_factors_bias = [0.085, 0.137, 0.158] #latent_rgb_factors_bias = [r_bias, g_bias, b_bias] - + latent_rgb_factors = torch.tensor(latent_rgb_factors, device=latents.device, dtype=latents.dtype).transpose(0, 1) latent_rgb_factors_bias = torch.tensor(latent_rgb_factors_bias, device=latents.device, dtype=latents.dtype) @@ -1019,9 +1057,9 @@ class CogVideoLatentPreview: latent_images_min = latent_images.min() latent_images_max = latent_images.max() latent_images = (latent_images - latent_images_min) / (latent_images_max - latent_images_min) - + return (latent_images.float().cpu(), out_factors) - + NODE_CLASS_MAPPINGS = { "HyVideoSampler": HyVideoSampler, "HyVideoDecode": HyVideoDecode, From 5f5b71dc6209c33f12088eaa6409c4aef08a4f9c Mon Sep 17 00:00:00 2001 From: kijai <40791699+kijai@users.noreply.github.com> Date: Thu, 12 Dec 2024 21:00:51 +0200 Subject: [PATCH 2/2] Move LoRA loading to model loader --- nodes.py | 262 +++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 185 insertions(+), 77 deletions(-) diff --git a/nodes.py b/nodes.py index c16a37e..443aa5a 100644 --- a/nodes.py +++ b/nodes.py @@ -72,6 +72,84 @@ def get_rotary_pos_embed(transformer, video_length, height, width): ) return freqs_cos, freqs_sin +def filter_state_dict_by_blocks(state_dict, blocks_mapping): + filtered_dict = {} + + for key in state_dict: + if 'double_blocks.' in key or 'single_blocks.' in key: + block_pattern = key.split('diffusion_model.')[1].split('.', 2)[0:2] + block_key = f'{block_pattern[0]}.{block_pattern[1]}.' + + if block_key in blocks_mapping: + filtered_dict[key] = state_dict[key] + + return filtered_dict + +class HyVideoLoraBlockEdit: + def __init__(self): + self.loaded_lora = None + + @classmethod + def INPUT_TYPES(s): + arg_dict = {} + argument = ("BOOLEAN", {"default": True}) + + for i in range(20): + arg_dict["double_blocks.{}.".format(i)] = argument + + for i in range(40): + arg_dict["single_blocks.{}.".format(i)] = argument + + return {"required": arg_dict} + + RETURN_TYPES = ("SELECTEDBLOCKS", ) + RETURN_NAMES = ("blocks", ) + OUTPUT_TOOLTIPS = ("The modified diffusion model.",) + FUNCTION = "select" + + CATEGORY = "HunyuanVideoWrapper" + + def select(self, **kwargs): + selected_blocks = {k: v for k, v in kwargs.items() if v is True} + print("Selected blocks: ", selected_blocks) + return (selected_blocks,) +class HyVideoLoraSelect: + @classmethod + def INPUT_TYPES(s): + return { + "required": { + "lora": (folder_paths.get_filename_list("loras"), + {"tooltip": "LORA models are expected to be in ComfyUI/models/loras with .safetensors extension"}), + "strength": ("FLOAT", {"default": 1.0, "min": -10.0, "max": 10.0, "step": 0.0001, "tooltip": "LORA strength, set to 0.0 to unmerge the LORA"}), + }, + "optional": { + "prev_lora":("HYVIDLORA", {"default": None, "tooltip": "For loading multiple LoRAs"}), + "blocks":("SELECTEDBLOCKS", ), + } + } + + RETURN_TYPES = ("HYVIDLORA",) + RETURN_NAMES = ("lora", ) + FUNCTION = "getlorapath" + CATEGORY = "HunyuanVideoWrapper" + DESCRIPTION = "Select a LoRA model from ComfyUI/models/loras" + + def getlorapath(self, lora, strength, blocks=None, prev_lora=None, fuse_lora=False): + loras_list = [] + + lora = { + "path": folder_paths.get_full_path("loras", lora), + "strength": strength, + "name": lora.split(".")[0], + "fuse_lora": fuse_lora, + "blocks": blocks + } + if prev_lora is not None: + loras_list.extend(prev_lora) + + loras_list.append(lora) + return (loras_list,) + class HyVideoBlockSwap: @classmethod def INPUT_TYPES(s): @@ -148,7 +226,7 @@ class HyVideoModelLoader: "required": { "model": (folder_paths.get_filename_list("diffusion_models"), {"tooltip": "These models are loaded from the 'ComfyUI/models/diffusion_models' -folder",}), - "base_precision": (["fp16", "fp32", "bf16"], {"default": "bf16"}), + "base_precision": (["fp32", "bf16"], {"default": "bf16"}), "quantization": (['disabled', 'fp8_e4m3fn', 'fp8_e4m3fn_fast', 'torchao_fp8dq', "torchao_fp8dqrow", "torchao_int8dq", "torchao_fp6", "torchao_int4", "torchao_int8"], {"default": 'disabled', "tooltip": "optional quantization method"}), "load_device": (["main_device", "offload_device"], {"default": "main_device"}), }, @@ -161,16 +239,17 @@ class HyVideoModelLoader: ], {"default": "flash_attn"}), "compile_args": ("COMPILEARGS", ), "block_swap_args": ("BLOCKSWAPARGS", ), + "lora": ("HYVIDLORA", {"default": None}), } } - RETURN_TYPES = ("MODEL",) + RETURN_TYPES = ("HYVIDEOMODEL",) RETURN_NAMES = ("model", ) FUNCTION = "loadmodel" CATEGORY = "HunyuanVideoWrapper" def loadmodel(self, model, base_precision, load_device, quantization, - compile_args=None, attention_mode="sdpa", block_swap_args=None): + compile_args=None, attention_mode="sdpa", block_swap_args=None, lora=None): transformer = None manual_offloading = True if "sage" in attention_mode: @@ -182,7 +261,7 @@ class HyVideoModelLoader: device = mm.get_torch_device() offload_device = mm.unet_offload_device() manual_offloading = True - transformer_load_device = device if load_device == "main_device" else offload_device + transformer_load_device = device if load_device == "main_device" or lora is None else offload_device mm.soft_empty_cache() base_dtype = {"fp8_e4m3fn": torch.float8_e4m3fn, "fp8_e4m3fn_fast": torch.float8_e4m3fn, "bf16": torch.bfloat16, "fp16": torch.float16, "fp32": torch.float32}[base_precision] @@ -212,7 +291,74 @@ class HyVideoModelLoader: **factor_kwargs ) transformer.eval() - if "torchao" in quantization: + + comfy_model = HyVideoModel( + HyVideoModelConfig(base_dtype), + model_type=comfy.model_base.ModelType.FLOW, + device=device, + ) + scheduler = FlowMatchDiscreteScheduler( + shift=9.0, + reverse=True, + solver="euler", + ) + pipe = HunyuanVideoPipeline( + transformer=transformer, + scheduler=scheduler, + progress_bar_config=None, + base_dtype=base_dtype + ) + + if not "torchao" in quantization: + log.info("Using accelerate to load and assign model weights to device...") + if quantization == "fp8_e4m3fn" or quantization == "fp8_e4m3fn_fast": + dtype = torch.float8_e4m3fn + else: + dtype = base_dtype + params_to_keep = {"norm", "bias", "time_in", "vector_in", "guidance_in", "txt_in", "img_in"} + for name, param in transformer.named_parameters(): + dtype_to_use = base_dtype if any(keyword in name for keyword in params_to_keep) else dtype + set_module_tensor_to_device(transformer, name, device=transformer_load_device, dtype=dtype_to_use, value=sd[name]) + + comfy_model.diffusion_model = transformer + patcher = comfy.model_patcher.ModelPatcher(comfy_model, device, offload_device) + + if lora is not None: + from comfy.sd import load_lora_for_models + for l in lora: + lora_path = l["path"] + lora_strength = l["strength"] + lora_sd = load_torch_file(lora_path, safe_load=True) + if l["blocks"]: + lora_sd = filter_state_dict_by_blocks(lora_sd, l["blocks"]) + + #for k in lora_sd.keys(): + # print(k) + + patcher, _ = load_lora_for_models(patcher, None, lora_sd, lora_strength, 0) + + comfy.model_management.load_models_gpu([patcher]) + + if quantization == "fp8_e4m3fn_fast": + from .fp8_optimization import convert_fp8_linear + convert_fp8_linear(patcher.model.diffusion_model, base_dtype, params_to_keep=params_to_keep) + + #compile + if compile_args is not None: + torch._dynamo.config.cache_size_limit = compile_args["dynamo_cache_size_limit"] + if compile_args["compile_single_blocks"]: + for i, block in enumerate(patcher.model.diffusion_model.single_blocks): + patcher.model.diffusion_model.single_blocks[i] = torch.compile(block, fullgraph=compile_args["fullgraph"], dynamic=compile_args["dynamic"], backend=compile_args["backend"], mode=compile_args["mode"]) + if compile_args["compile_double_blocks"]: + for i, block in enumerate(patcher.model.diffusion_model.double_blocks): + patcher.model.diffusion_model.double_blocks[i] = torch.compile(block, fullgraph=compile_args["fullgraph"], dynamic=compile_args["dynamic"], backend=compile_args["backend"], mode=compile_args["mode"]) + if compile_args["compile_txt_in"]: + patcher.model.diffusion_model.txt_in = torch.compile(patcher.model.diffusion_model.txt_in, fullgraph=compile_args["fullgraph"], dynamic=compile_args["dynamic"], backend=compile_args["backend"], mode=compile_args["mode"]) + if compile_args["compile_vector_in"]: + patcher.model.diffusion_model.vector_in = torch.compile(patcher.model.diffusion_model.vector_in, fullgraph=compile_args["fullgraph"], dynamic=compile_args["dynamic"], backend=compile_args["backend"], mode=compile_args["mode"]) + if compile_args["compile_final_layer"]: + patcher.model.diffusion_model.final_layer = torch.compile(patcher.model.diffusion_model.final_layer, fullgraph=compile_args["fullgraph"], dynamic=compile_args["dynamic"], backend=compile_args["backend"], mode=compile_args["mode"]) + elif "torchao" in quantization: try: from torchao.quantization import ( quantize_, @@ -223,7 +369,7 @@ class HyVideoModelLoader: int4_weight_only ) except: - raise ImportError("torchao is not installed, please install torchao to use fp8dq") + raise ImportError("torchao is not installed") # def filter_fn(module: nn.Module, fqn: str) -> bool: # target_submodules = {'attn1', 'ff'} # avoid norm layers, 1.5 at least won't work with quantized norm1 #todo: test other models @@ -246,97 +392,57 @@ class HyVideoModelLoader: quant_func = int8_dynamic_activation_int8_weight() log.info(f"Quantizing model with {quant_func}") + comfy_model.diffusion_model = transformer + patcher = comfy.model_patcher.ModelPatcher(comfy_model, device, offload_device) - for i, block in enumerate(transformer.single_blocks): + if lora is not None: + from comfy.sd import load_lora_for_models + for l in lora: + lora_path = l["path"] + lora_strength = l["strength"] + lora_sd = load_torch_file(lora_path, safe_load=True) + patcher, _ = load_lora_for_models(patcher, None, lora_sd, lora_strength, 0) + + comfy.model_management.load_models_gpu([patcher]) + + for i, block in enumerate(patcher.model.diffusion_model.single_blocks): log.info(f"Quantizing single_block {i}") for name, _ in block.named_parameters(prefix=f"single_blocks.{i}"): #print(f"Parameter name: {name}") - set_module_tensor_to_device(transformer, name, device=transformer_load_device, dtype=base_dtype, value=sd[name]) + set_module_tensor_to_device(patcher.model.diffusion_model, name, device=patcher.model.diffusion_model_load_device, dtype=base_dtype, value=sd[name]) if compile_args is not None: - transformer.single_blocks[i] = torch.compile(block, fullgraph=compile_args["fullgraph"], dynamic=compile_args["dynamic"], backend=compile_args["backend"], mode=compile_args["mode"]) + patcher.model.diffusion_model.single_blocks[i] = torch.compile(block, fullgraph=compile_args["fullgraph"], dynamic=compile_args["dynamic"], backend=compile_args["backend"], mode=compile_args["mode"]) quantize_(block, quant_func) print(block) block.to(offload_device) - for i, block in enumerate(transformer.double_blocks): + for i, block in enumerate(patcher.model.diffusion_model.double_blocks): log.info(f"Quantizing double_block {i}") for name, _ in block.named_parameters(prefix=f"double_blocks.{i}"): #print(f"Parameter name: {name}") - set_module_tensor_to_device(transformer, name, device=transformer_load_device, dtype=base_dtype, value=sd[name]) + set_module_tensor_to_device(patcher.model.diffusion_model, name, device=patcher.model.diffusion_model_load_device, dtype=base_dtype, value=sd[name]) if compile_args is not None: - transformer.double_blocks[i] = torch.compile(block, fullgraph=compile_args["fullgraph"], dynamic=compile_args["dynamic"], backend=compile_args["backend"], mode=compile_args["mode"]) + patcher.model.diffusion_model.double_blocks[i] = torch.compile(block, fullgraph=compile_args["fullgraph"], dynamic=compile_args["dynamic"], backend=compile_args["backend"], mode=compile_args["mode"]) quantize_(block, quant_func) - for name, param in transformer.named_parameters(): + for name, param in patcher.model.diffusion_model.named_parameters(): if "single_blocks" not in name and "double_blocks" not in name: - set_module_tensor_to_device(transformer, name, device=transformer_load_device, dtype=base_dtype, value=sd[name]) + set_module_tensor_to_device(patcher.model.diffusion_model, name, device=patcher.model.diffusion_model_load_device, dtype=base_dtype, value=sd[name]) manual_offloading = False # to disable manual .to(device) calls log.info(f"Quantized transformer blocks to {quantization}") - for name, param in transformer.named_parameters(): + for name, param in patcher.model.diffusion_model.named_parameters(): print(name, param.dtype) #param.data = param.data.to(self.vae_dtype).to(device) - else: - log.info("Using accelerate to load and assign model weights to device...") - if quantization == "fp8_e4m3fn" or quantization == "fp8_e4m3fn_fast": - dtype = torch.float8_e4m3fn - else: - dtype = base_dtype - params_to_keep = {"norm", "bias", "time_in", "vector_in", "guidance_in", "txt_in", "img_in"} - for name, param in transformer.named_parameters(): - dtype_to_use = base_dtype if any(keyword in name for keyword in params_to_keep) else dtype - set_module_tensor_to_device(transformer, name, device=transformer_load_device, dtype=dtype_to_use, value=sd[name]) - - - if quantization == "fp8_e4m3fn_fast": - from .fp8_optimization import convert_fp8_linear - convert_fp8_linear(transformer, base_dtype, params_to_keep=params_to_keep) - - #compile - if compile_args is not None: - torch._dynamo.config.cache_size_limit = compile_args["dynamo_cache_size_limit"] - if compile_args["compile_single_blocks"]: - for i, block in enumerate(transformer.single_blocks): - transformer.single_blocks[i] = torch.compile(block, fullgraph=compile_args["fullgraph"], dynamic=compile_args["dynamic"], backend=compile_args["backend"], mode=compile_args["mode"]) - if compile_args["compile_double_blocks"]: - for i, block in enumerate(transformer.double_blocks): - transformer.double_blocks[i] = torch.compile(block, fullgraph=compile_args["fullgraph"], dynamic=compile_args["dynamic"], backend=compile_args["backend"], mode=compile_args["mode"]) - if compile_args["compile_txt_in"]: - transformer.txt_in = torch.compile(transformer.txt_in, fullgraph=compile_args["fullgraph"], dynamic=compile_args["dynamic"], backend=compile_args["backend"], mode=compile_args["mode"]) - if compile_args["compile_vector_in"]: - transformer.vector_in = torch.compile(transformer.vector_in, fullgraph=compile_args["fullgraph"], dynamic=compile_args["dynamic"], backend=compile_args["backend"], mode=compile_args["mode"]) - if compile_args["compile_final_layer"]: - transformer.final_layer = torch.compile(transformer.final_layer, fullgraph=compile_args["fullgraph"], dynamic=compile_args["dynamic"], backend=compile_args["backend"], mode=compile_args["mode"]) del sd mm.soft_empty_cache() - scheduler = FlowMatchDiscreteScheduler( - shift=9.0, - reverse=True, - solver="euler", - ) - - pipe = HunyuanVideoPipeline( - transformer=transformer, - scheduler=scheduler, - progress_bar_config=None, - base_dtype=base_dtype - ) - - comfy_model = HyVideoModel( - HyVideoModelConfig(base_dtype), - model_type=comfy.model_base.ModelType.FLOW, - device=mm.get_torch_device(), - ) - comfy_model["pipe"] = pipe - comfy_model["dtype"] = base_dtype - comfy_model["base_path"] = model_path - comfy_model["model_name"] = model - comfy_model["manual_offloading"] = manual_offloading - comfy_model["quantization"] = "disabled" - comfy_model["block_swap_args"] = block_swap_args - comfy_model.diffusion_model = transformer - - patcher = comfy.model_patcher.ModelPatcher(comfy_model, device, offload_device) + patcher.model["pipe"] = pipe + patcher.model["dtype"] = base_dtype + patcher.model["base_path"] = model_path + patcher.model["model_name"] = model + patcher.model["manual_offloading"] = manual_offloading + patcher.model["quantization"] = "disabled" + patcher.model["block_swap_args"] = block_swap_args return (patcher,) @@ -760,7 +866,7 @@ class HyVideoSampler: def INPUT_TYPES(s): return { "required": { - "model": ("MODEL",), + "model": ("HYVIDEOMODEL",), "hyvid_embeds": ("HYVIDEMBEDS", ), "width": ("INT", {"default": 512, "min": 64, "max": 4096, "step": 16}), "height": ("INT", {"default": 512, "min": 64, "max": 4096, "step": 16}), @@ -785,8 +891,6 @@ class HyVideoSampler: CATEGORY = "HunyuanVideoWrapper" def process(self, model, hyvid_embeds, flow_shift, steps, embedded_guidance_scale, seed, width, height, num_frames, samples=None, denoise_strength=1.0, force_offload=True, stg_args=None): - # We only need this so that LoRA weights get patched. - comfy.model_management.load_models_gpu([model]) model = model.model device = mm.get_torch_device() @@ -1116,6 +1220,8 @@ NODE_CLASS_MAPPINGS = { "HyVideoSTG": HyVideoSTG, "HyVideoCustomPromptTemplate": HyVideoCustomPromptTemplate, "HyVideoLatentPreview": HyVideoLatentPreview, + "HyVideoLoraSelect": HyVideoLoraSelect, + "HyVideoLoraBlockEdit": HyVideoLoraBlockEdit, } NODE_DISPLAY_NAME_MAPPINGS = { "HyVideoSampler": "HunyuanVideo Sampler", @@ -1130,4 +1236,6 @@ NODE_DISPLAY_NAME_MAPPINGS = { "HyVideoSTG": "HunyuanVideo STG", "HyVideoCustomPromptTemplate": "HunyuanVideo Custom Prompt Template", "HyVideoLatentPreview": "HunyuanVideo Latent Preview", + "HyVideoLoraSelect": "HunyuanVideo Lora Select", + "HyVideoLoraBlockEdit": "HunyuanVideo Lora Block Edit", }