import os import logging import torch import folder_paths import comfy.sd import comfy.utils import comfy.ldm.modules.attention from comfy_api.latest import io NODE_DIR = os.path.dirname(os.path.abspath(__file__)) MODELS_DIR = os.path.join(NODE_DIR, "models") MODEL_EXTENSIONS = (".safetensors", ".sft", ".ckpt", ".pt", ".pth", ".bin") SUBDIR_HINTS = { "diffusion": "models/diffusion", "vae": "models/vae", "audio_vae": "models/audio_vae", "clip": "models/clip", "loras": "models/loras", } def _list_files(subdir): """List model files relative to the pack's models/ folder.""" directory = os.path.join(MODELS_DIR, subdir) results = [] if os.path.isdir(directory): for root, _dirs, files in os.walk(directory): for filename in files: if filename.lower().endswith(MODEL_EXTENSIONS): rel = os.path.relpath(os.path.join(root, filename), directory) results.append(rel.replace("\\", "/")) results.sort() if not results: results.append("".format(SUBDIR_HINTS[subdir])) return results def _resolve(subdir, name): if name.startswith(" attention backend -> LoRA, plus video VAE, audio VAE and CLIP, all read from the node pack's models/ folder. The model set is MiniMax H3-only: the CLIP recipe is locked to CLIPType.MINIMAX and outputs are exactly (model, clip, video_vae, audio_vae) with the attention backend and LoRA already applied to the model.""" _loaded_lora = None # (path, sd, metadata) cache, mirrors native LoraLoader @classmethod def define_schema(cls) -> io.Schema: # The published pack ships its models inside the repo, so default to # the bundled turbo LoRA out of the box; "none" opts out. lora_files = _list_files("loras") lora_default = "none" if lora_files[0].startswith(" 2 else None else: cls._loaded_lora = None if lora is None: lora, lora_metadata = comfy.utils.load_torch_file(lora_path, safe_load=True, return_metadata=True) cls._loaded_lora = (lora_path, lora, lora_metadata) model_lora, clip_lora = comfy.sd.load_lora_for_models( model, clip, lora, strength_model, strength_clip, lora_metadata=lora_metadata ) return model_lora, clip_lora @classmethod def _load_vae(cls, vae_name, subdir="vae"): path = _resolve(subdir, vae_name) sd, metadata = comfy.utils.load_torch_file(path, return_metadata=True) if subdir == "audio_vae" and any(k.startswith("audio_vae.") for k in sd): # Audio VAE checkpoint style (e.g. LTX audio VAE): rename keys to what comfy.sd.VAE expects. sd = comfy.utils.state_dict_prefix_replace( sd, {"audio_vae.": "autoencoder.", "vocoder.": "vocoder."}, filter_keys=True ) vae = comfy.sd.VAE(sd=sd, metadata=metadata) vae.throw_exception_if_invalid() return vae @classmethod def _load_clip(cls, clip_name, device): clip_type_enum = _h3_clip_type() clip_path = _resolve("clip", clip_name) model_options = {} if device == "cpu": model_options["load_device"] = model_options["offload_device"] = torch.device("cpu") clip = comfy.sd.load_clip( ckpt_paths=[clip_path], embedding_directory=folder_paths.get_folder_paths("embeddings"), clip_type=clip_type_enum, model_options=model_options, ) return clip @classmethod def execute( cls, diffusion_model, weight_dtype, attention_backend, lora_name, lora_strength_model, lora_strength_clip, video_vae, audio_vae, clip, clip_device, ) -> io.NodeOutput: # Chain: Load Diffusion Model -> Model Attention Backend -> Lora Loader (needs both model and clip) model = cls._load_diffusion_model(diffusion_model, weight_dtype) model = cls._apply_attention_backend(model, attention_backend) clip_obj = cls._load_clip(clip, clip_device) model, clip_obj = cls._apply_lora(model, clip_obj, lora_name, lora_strength_model, lora_strength_clip) video_vae_obj = cls._load_vae(video_vae, "vae") audio_vae_obj = cls._load_vae(audio_vae, "audio_vae") return io.NodeOutput(model, clip_obj, video_vae_obj, audio_vae_obj)