From fa628c5359a084a3c3cb19ccc7520cf76294bea5 Mon Sep 17 00:00:00 2001 From: Avaya Aggarwal <119044997+OnePunchMonk@users.noreply.github.com> Date: Fri, 23 Oct 2026 09:28:23 +0530 Subject: [PATCH] [Fix] comfyui: Fix GGUF detection, import guard and VRAM accounting in SGLDiffusion plugin (#43536) Co-authored-by: niehen6174 <1639206518@qq.com> --- .../apps/ComfyUI_SGLDiffusion/__init__.py | 14 ++- .../ComfyUI_SGLDiffusion/core/generator.py | 60 +++--------- .../executors/minimax_h3.py | 60 ++++++++---- .../apps/ComfyUI_SGLDiffusion/nodes.py | 19 ++-- .../loader/comfyui_checkpoints/flux.py | 6 ++ .../loader/comfyui_checkpoints/spec.py | 93 ++++++++++++++++++- .../loader/comfyui_checkpoints/zimage.py | 2 + .../test/unit/test_comfyui_checkpoint_spec.py | 92 +++++++++++++++++- .../test/unit/test_comfyui_generator.py | 60 +++++++++++- .../test/unit/test_comfyui_lora_node.py | 24 +++++ 10 files changed, 344 insertions(+), 86 deletions(-) diff --git a/python/sglang/multimodal_gen/apps/ComfyUI_SGLDiffusion/__init__.py b/python/sglang/multimodal_gen/apps/ComfyUI_SGLDiffusion/__init__.py index dacffc44ba..54dabab6f1 100644 --- a/python/sglang/multimodal_gen/apps/ComfyUI_SGLDiffusion/__init__.py +++ b/python/sglang/multimodal_gen/apps/ComfyUI_SGLDiffusion/__init__.py @@ -2,14 +2,24 @@ ComfyUI SGLang Diffusion nodes package. """ +import logging + +logger = logging.getLogger(__name__) + WEB_DIRECTORY = "./web" try: from .nodes import NODE_CLASS_MAPPINGS, NODE_DISPLAY_NAME_MAPPINGS __all__ = ["NODE_CLASS_MAPPINGS", "NODE_DISPLAY_NAME_MAPPINGS", "WEB_DIRECTORY"] -except ImportError: - # ComfyUI dependencies not available (e.g., in test environment) +except ImportError as exc: + # Missing ComfyUI or SGLang deps; log why, or every node vanishes silently. + logger.error( + "ComfyUI_SGLDiffusion failed to register nodes: %r. If this is a " + "real ComfyUI install, install the diffusion extras with " + "'pip install sglang[diffusion]'.", + exc, + ) NODE_CLASS_MAPPINGS = {} NODE_DISPLAY_NAME_MAPPINGS = {} __all__ = ["NODE_CLASS_MAPPINGS", "NODE_DISPLAY_NAME_MAPPINGS"] diff --git a/python/sglang/multimodal_gen/apps/ComfyUI_SGLDiffusion/core/generator.py b/python/sglang/multimodal_gen/apps/ComfyUI_SGLDiffusion/core/generator.py index 8c2b4d303c..e9c9d7ed0e 100644 --- a/python/sglang/multimodal_gen/apps/ComfyUI_SGLDiffusion/core/generator.py +++ b/python/sglang/multimodal_gen/apps/ComfyUI_SGLDiffusion/core/generator.py @@ -104,48 +104,6 @@ def _looks_like_gguf(path: str) -> bool: ) -def _h3_detect_companion(gguf_ref: str) -> str: - """ComfyUI detect needs H3 safetensors keys; GGUF only overrides the DiT.""" - name = os.path.basename(gguf_ref).lower() - prefer = [] - if "ref2va" in name: - prefer.append("minimax_h3_ref2va_bf16.safetensors") - prefer.extend( - [ - "minimax_h3_fl2va_bf16.safetensors", - "minimax_h3_ref2va_bf16.safetensors", - ] - ) - folders = ( - [os.path.dirname(os.path.abspath(gguf_ref))] - if os.path.dirname(gguf_ref) - else [] - ) - try: - import folder_paths - - folders.extend(folder_paths.get_folder_paths("diffusion_models")) - except Exception: - pass - seen: set[str] = set() - for folder in folders: - if not folder: - continue - for candidate in prefer: - path = os.path.join(folder, candidate) - if path in seen: - continue - seen.add(path) - if os.path.isfile(path): - return path - raise ValueError( - "GGUF transformer needs a MiniMax-H3 BF16 safetensors in " - "models/diffusion_models for ComfyUI architecture detect " - f"(looked for {prefer}). Keep --model-path / unet_name on the " - "BF16 file and pass the GGUF via transformer_weights_path." - ) - - _NATIVE_MODEL_TYPES = {"FastH3Pipeline": "fast_h3", "VDNH3Pipeline": "vdn_h3"} NATIVE_MODEL_TYPES = tuple(_NATIVE_MODEL_TYPES.values()) @@ -210,10 +168,16 @@ def _h3_unet_config_from_native(model_dir: str) -> dict: def _load_state_dict_for_detection(model_path: str): if _looks_like_gguf(model_path): - raise ValueError( - f"Cannot detect architecture from GGUF {model_path!r}. " - "Use a MiniMax-H3 BF16 safetensors for detect." + # Same header-only trick as safetensors: tensor names and logical + # (unpacked) shapes are all detect_unet_config looks at. + from sglang.multimodal_gen.runtime.loader.gguf_weights import ( + read_gguf_tensor_meta, ) + + return { + name: _HeaderTensor(meta.logical_shape) + for name, meta in read_gguf_tensor_meta(model_path).items() + } if model_path.endswith(".safetensors"): from safetensors import safe_open @@ -535,11 +499,11 @@ class SGLDiffusionGenerator: if new_sd is not None: # diffusers mmdit model_config = model_detection.model_config_from_unet(new_sd, "") if model_config is None: - return None + raise ValueError(f"No ComfyUI model config for {model_path!r}") else: # diffusers unet model_config = model_detection.model_config_from_diffusers_unet(sd) if model_config is None: - return None + raise ValueError(f"No ComfyUI model config for {model_path!r}") diffusers_keys = unet_to_diffusers(model_config.unet_config) new_sd = {} @@ -611,8 +575,6 @@ class SGLDiffusionGenerator: else: sgld_options.pop("transformer_weights_path", None) detect_path = model_path - if _looks_like_gguf(model_path): - detect_path = _h3_detect_companion(sgld_options["transformer_weights_path"]) gather_options = { "model_path": detect_path, "model_options": model_options, diff --git a/python/sglang/multimodal_gen/apps/ComfyUI_SGLDiffusion/executors/minimax_h3.py b/python/sglang/multimodal_gen/apps/ComfyUI_SGLDiffusion/executors/minimax_h3.py index 72e0b07a73..e37e0983bc 100644 --- a/python/sglang/multimodal_gen/apps/ComfyUI_SGLDiffusion/executors/minimax_h3.py +++ b/python/sglang/multimodal_gen/apps/ComfyUI_SGLDiffusion/executors/minimax_h3.py @@ -10,28 +10,38 @@ from typing import Any import torch -from sglang.multimodal_gen.configs.sample.minimax_h3 import MiniMaxH3SamplingParams -from sglang.multimodal_gen.configs.sample.minimax_h3_vdn import VDNH3SamplingParams -from sglang.multimodal_gen.runtime.pipelines_core.stages.model_specific_stages.minimax_h3.release_metadata import ( - MiniMaxH3ReleaseMetadata, -) -from sglang.multimodal_gen.runtime.pipelines_core.stages.model_specific_stages.minimax_h3.stages.comfyui_step import ( - DEFAULT_SIGMA_SHIFT_AUDIO, - DEFAULT_SIGMA_SHIFT_VIDEO, - serialize_comfyui_layout, - time_shift_sigma, -) -from sglang.multimodal_gen.runtime.pipelines_core.stages.model_specific_stages.minimax_h3.time_request import ( - minimax_h3_time_shift_sigmas, -) - from .adapter import ComfyUIModelAdapter, PackedForward from .base import SGLDiffusionExecutor -# H3 pins loop/CFG fields with init=False; ComfyUI owns those. -_H3_PINNED_SAMPLING_FIELDS = frozenset( - f.name for f in fields(MiniMaxH3SamplingParams) if not f.init -) +# Same guard as base.py: generator.py imports this module unconditionally, so +# a missing runtime must surface on first use instead of hiding every node. +try: + from sglang.multimodal_gen.configs.sample.minimax_h3 import MiniMaxH3SamplingParams + from sglang.multimodal_gen.configs.sample.minimax_h3_vdn import VDNH3SamplingParams + from sglang.multimodal_gen.runtime.pipelines_core.stages.model_specific_stages.minimax_h3.release_metadata import ( + MiniMaxH3ReleaseMetadata, + ) + from sglang.multimodal_gen.runtime.pipelines_core.stages.model_specific_stages.minimax_h3.stages.comfyui_step import ( + DEFAULT_SIGMA_SHIFT_AUDIO, + DEFAULT_SIGMA_SHIFT_VIDEO, + serialize_comfyui_layout, + time_shift_sigma, + ) + from sglang.multimodal_gen.runtime.pipelines_core.stages.model_specific_stages.minimax_h3.time_request import ( + minimax_h3_time_shift_sigmas, + ) +except ImportError as exc: + _H3_RUNTIME_IMPORT_ERROR: ImportError | None = exc + MiniMaxH3SamplingParams = VDNH3SamplingParams = MiniMaxH3ReleaseMetadata = None + DEFAULT_SIGMA_SHIFT_AUDIO = DEFAULT_SIGMA_SHIFT_VIDEO = 0.0 + serialize_comfyui_layout = time_shift_sigma = minimax_h3_time_shift_sigmas = None + _H3_PINNED_SAMPLING_FIELDS: frozenset[str] = frozenset() +else: + _H3_RUNTIME_IMPORT_ERROR = None + # H3 pins loop/CFG fields with init=False; ComfyUI owns those. + _H3_PINNED_SAMPLING_FIELDS = frozenset( + f.name for f in fields(MiniMaxH3SamplingParams) if not f.init + ) def drop_h3_pinned_sampling_fields(kwargs: dict[str, Any]) -> dict[str, Any]: @@ -223,6 +233,13 @@ class MiniMaxH3Executor(SGLDiffusionExecutor): # ComfyUI MiniMaxH3.extra_conds reads this when a denoise_mask is present. patch_size = (1, 2, 2) + def __init__(self, *args, **kwargs): + if _H3_RUNTIME_IMPORT_ERROR is not None: + raise RuntimeError( + "SGLang diffusion runtime failed to import" + ) from _H3_RUNTIME_IMPORT_ERROR + super().__init__(*args, **kwargs) + def preprocess_text_embeds(self, text_states): """ComfyUI extra_conds runs this once per sample. @@ -368,4 +385,7 @@ class FastH3Executor(_DistilledH3Executor): class VDNH3Executor(_DistilledH3Executor): adapter_cls = VDNH3Adapter - expected_sigmas = distilled_h3_sigmas("vdn_h3") + # Without the runtime __init__ raises anyway; keep the import working. + expected_sigmas = ( + distilled_h3_sigmas("vdn_h3") if _H3_RUNTIME_IMPORT_ERROR is None else [] + ) diff --git a/python/sglang/multimodal_gen/apps/ComfyUI_SGLDiffusion/nodes.py b/python/sglang/multimodal_gen/apps/ComfyUI_SGLDiffusion/nodes.py index 12c352623a..e675a00ba6 100644 --- a/python/sglang/multimodal_gen/apps/ComfyUI_SGLDiffusion/nodes.py +++ b/python/sglang/multimodal_gen/apps/ComfyUI_SGLDiffusion/nodes.py @@ -13,14 +13,19 @@ from .core import SGLDiffusionGenerator, SGLDiffusionServerAPI from .options import SGLDOptions -def _enable_gguf_in_diffusion_models() -> None: - """Let SGLDUNETLoader list ``.gguf`` DiTs. Quantization lives in the file.""" - entry = folder_paths.folder_names_and_paths.get("diffusion_models") - if entry is not None and len(entry) >= 2 and isinstance(entry[1], set): - entry[1].add(".gguf") +def _list_unet_names_including_gguf() -> list[str]: + """SGLDUNETLoader's file list plus ``.gguf`` DiTs. + + Kept local: registering ``.gguf`` in ``folder_paths`` would make ComfyUI's + stock loader offer files it cannot load. + """ + names = set(folder_paths.get_filename_list("diffusion_models")) + for folder in folder_paths.get_folder_paths("diffusion_models"): + files, _ = folder_paths.recursive_search(folder) + names.update(folder_paths.filter_files_extensions(files, {".gguf"})) + return sorted(names) -_enable_gguf_in_diffusion_models() from .utils import ( convert_b64_to_tensor_image, convert_video_to_comfy_video, @@ -138,7 +143,7 @@ class SGLDUNETLoader: def INPUT_TYPES(s): return { "required": { - "unet_name": (folder_paths.get_filename_list("diffusion_models"),), + "unet_name": (_list_unet_names_including_gguf(),), "weight_dtype": (["default", "fp8_e4m3fn", "fp8_e5m2"],), }, "optional": { diff --git a/python/sglang/multimodal_gen/runtime/loader/comfyui_checkpoints/flux.py b/python/sglang/multimodal_gen/runtime/loader/comfyui_checkpoints/flux.py index 9d46f2facd..7c573e44ee 100644 --- a/python/sglang/multimodal_gen/runtime/loader/comfyui_checkpoints/flux.py +++ b/python/sglang/multimodal_gen/runtime/loader/comfyui_checkpoints/flux.py @@ -35,6 +35,12 @@ def _build_dit_config(server_args: ServerArgs) -> FluxConfig: def _checkpoint_has_guidance_embeds(model_path: str) -> bool: + if model_path.endswith(".gguf"): + from sglang.multimodal_gen.runtime.loader.gguf_weights import ( + read_gguf_tensor_meta, + ) + + return "guidance_in.in_layer.weight" in read_gguf_tensor_meta(model_path) with safe_open(model_path, framework="pt", device="cpu") as checkpoint: return "guidance_in.in_layer.weight" in checkpoint.keys() diff --git a/python/sglang/multimodal_gen/runtime/loader/comfyui_checkpoints/spec.py b/python/sglang/multimodal_gen/runtime/loader/comfyui_checkpoints/spec.py index ad4a7e6b8f..a53b113180 100644 --- a/python/sglang/multimodal_gen/runtime/loader/comfyui_checkpoints/spec.py +++ b/python/sglang/multimodal_gen/runtime/loader/comfyui_checkpoints/spec.py @@ -14,8 +14,9 @@ offload -- goes through the regular transformer load path. from __future__ import annotations import os +from collections import defaultdict from collections.abc import Callable, Iterator -from dataclasses import dataclass, field +from dataclasses import dataclass, field, replace from typing import TYPE_CHECKING, Any import torch @@ -27,6 +28,7 @@ from sglang.multimodal_gen.runtime.loader.minimax_h3_weights import ( inspect_minimax_h3_safetensors, resolve_minimax_h3_checkpoint_quantization, ) +from sglang.multimodal_gen.runtime.loader.utils import get_param_names_mapping from sglang.multimodal_gen.runtime.loader.weight_load_plan import WeightLoadPlan from sglang.multimodal_gen.runtime.loader.weight_utils import ( safetensors_weights_iterator, @@ -115,8 +117,78 @@ def get_registered_comfyui_pipeline_names() -> list[str]: def is_comfyui_single_file(model_path: str) -> bool: - """ComfyUI ships DiTs as one safetensors file with no model_index.json.""" - return os.path.isfile(model_path) and model_path.endswith(".safetensors") + """ComfyUI ships DiTs as one safetensors or GGUF file with no model_index.json.""" + return os.path.isfile(model_path) and model_path.endswith((".safetensors", ".gguf")) + + +def _gguf_runtime_layout(tensor_meta: dict, mapping: dict) -> tuple[dict, dict]: + """Key GGUF metadata by runtime names; return it with the fused-shard plan. + + Shards of one fused runtime layer (Z-Image w1/w3 -> w13) keep their own + entries and are concatenated on load, under the fused name quant lookup uses. + """ + from sglang.multimodal_gen.runtime.loader.gguf_weights import ( + remap_gguf_tensor_meta, + ) + + map_name = get_param_names_mapping(mapping) + plain, fused = {}, defaultdict(dict) + for name, meta in tensor_meta.items(): + target, index, _ = map_name(name) + if index is None: + plain[name] = meta + else: + fused[target][index] = (name, meta) + + layout = remap_gguf_tensor_meta(plain, lambda name: map_name(name)[0]) + shard_plan = {} + for target, parts in fused.items(): + metas = [parts[i][1] for i in sorted(parts)] + first = metas[0] + if len({(m.ggml_type, m.is_packed, m.logical_shape[1:]) for m in metas}) > 1: + raise ValueError(f"GGUF shards of {target} differ in type or width") + param_name = ( + f"{target.removesuffix('.weight')}.qweight" if first.is_packed else target + ) + layout[target] = replace( + first, + logical_shape=( + sum(m.logical_shape[0] for m in metas), + *first.logical_shape[1:], + ), + stored_shape=( + sum(m.stored_shape[0] for m in metas), + *first.stored_shape[1:], + ), + param_name=param_name, + ) + for index, (name, meta) in parts.items(): + layout[name] = meta + shard_plan[meta.param_name] = (param_name, index, len(parts)) + return layout, shard_plan + + +def _runtime_gguf_weights(weights, layout: dict, shard_plan: dict, quant_config): + """Fuse planned shards; dequantize packed tensors whose layer built without + GGUF (e.g. Z-Image adaLN ReplicatedLinear). Runs after model construction, + so quant_config.selected is final.""" + import gguf + + packed_types = {m.param_name: m.weight_type for m in layout.values() if m.is_packed} + pending = defaultdict(dict) + for name, tensor in weights: + if name in shard_plan: + name, index, total = shard_plan[name] + pending[name][index] = tensor + if len(pending[name]) < total: + continue + parts = pending.pop(name) + tensor = torch.cat([parts[i] for i in range(total)]) + prefix = name.removesuffix(".qweight") + if name in packed_types and prefix not in quant_config.selected: + value = gguf.dequantize(tensor.numpy(), packed_types[name]) + name, tensor = f"{prefix}.weight", torch.from_numpy(value).bfloat16() + yield name, tensor def _load_comfyui_gguf_transformer( @@ -146,7 +218,10 @@ def _load_comfyui_gguf_transformer( [gguf_file], str(server_args.model_variant or "fl2va") ) - tensor_meta = read_gguf_tensor_meta(gguf_file) + # Quant lookup and weight loading must both see runtime names. + tensor_meta, shard_plan = _gguf_runtime_layout( + read_gguf_tensor_meta(gguf_file), mapping + ) curve = tensor_meta.get("adaln_t_table") if curve is not None: if curve.is_quantized or len(curve.logical_shape) != 2: @@ -188,7 +263,12 @@ def _load_comfyui_gguf_transformer( reduce_dtype=torch.float32, output_dtype=None, strict=False, - weights_iterator=gguf_weights_iterator(gguf_file, tensor_meta), + weights_iterator=_runtime_gguf_weights( + gguf_weights_iterator(gguf_file, tensor_meta), + tensor_meta, + shard_plan, + quant_config, + ), ) finally: model_cls.param_names_mapping = original_mapping @@ -283,6 +363,9 @@ def load_comfyui_transformer( ) gguf_file = resolve_transformer_gguf_to_load(server_args, "transformer") + if gguf_file is None and model_path.endswith(".gguf"): + # A GGUF unet picked in ComfyUI is the model path itself. + gguf_file = model_path if gguf_file is not None: model = _load_comfyui_gguf_transformer( model_cls=model_cls, diff --git a/python/sglang/multimodal_gen/runtime/loader/comfyui_checkpoints/zimage.py b/python/sglang/multimodal_gen/runtime/loader/comfyui_checkpoints/zimage.py index 6347ddbf67..11b2e73fde 100644 --- a/python/sglang/multimodal_gen/runtime/loader/comfyui_checkpoints/zimage.py +++ b/python/sglang/multimodal_gen/runtime/loader/comfyui_checkpoints/zimage.py @@ -46,6 +46,8 @@ def _convert_weights(weights: WeightIterator, dit_config: Any) -> WeightIterator _PARAM_NAMES_MAPPING = { + # GGUF skips _convert_weights; its fused qkv already matches to_qkv. + r"(.*)\.attention\.qkv\.(weight|bias)$": (r"\1.attention.to_qkv.\2", None, None), r"(.*)\.attention\.k_norm\.weight$": (r"\1.attention.norm_k.weight", None, None), r"(.*)\.attention\.q_norm\.weight$": (r"\1.attention.norm_q.weight", None, None), r"(.*)\.attention\.out\.weight$": (r"\1.attention.to_out.0.weight", None, None), diff --git a/python/sglang/multimodal_gen/test/unit/test_comfyui_checkpoint_spec.py b/python/sglang/multimodal_gen/test/unit/test_comfyui_checkpoint_spec.py index 1c82b233fc..d7daa22dce 100644 --- a/python/sglang/multimodal_gen/test/unit/test_comfyui_checkpoint_spec.py +++ b/python/sglang/multimodal_gen/test/unit/test_comfyui_checkpoint_spec.py @@ -10,7 +10,14 @@ from sglang.multimodal_gen.runtime.loader.comfyui_checkpoints import spec as spe _MARKER = {"format": "int8_tensorwise", "convrot": True, "convrot_groupsize": 256} -def _load(monkeypatch, spec, h3_markers=None, quantization=None, local_device="cpu"): +def _load( + monkeypatch, + spec, + h3_markers=None, + quantization=None, + local_device="cpu", + model_path="/models/dit.safetensors", +): """Run load_comfyui_transformer with a dummy spec; return the load kwargs.""" seen = {} @@ -55,10 +62,11 @@ def _load(monkeypatch, spec, h3_markers=None, quantization=None, local_device="c ) pipeline = SimpleNamespace( pipeline_name="DummyPipeline", - model_path="/models/dit.safetensors", + model_path=model_path, get_module=lambda name: "scheduler", ) spec_mod.load_comfyui_transformer(pipeline, server_args) + seen["server_args"] = server_args return seen @@ -103,6 +111,86 @@ def test_h3_spec_still_rejects_non_int8_markers(monkeypatch): _load(monkeypatch, h3, h3_markers={"x": {"format": "nvfp4"}}) +def _write_gguf(path, names): + import gguf + import numpy as np + + writer = gguf.GGUFWriter(str(path), "flux") + for i, name in enumerate(names): + writer.add_tensor(name, np.full((2, 4), i, dtype=np.float32)) + writer.write_header_to_file() + writer.write_kv_data_to_file() + writer.write_tensors_to_file() + writer.close() + + +def test_gguf_unet_loads_with_runtime_names(monkeypatch, tmp_path): + """A GGUF unet loads as the DiT, keyed by runtime layer names for both quant + lookup and loading: Z-Image's qkv is to_qkv and its w1/w3 fuse into w13.""" + from dataclasses import replace + + from sglang.multimodal_gen.configs.models.dits.zimage import ZImageDitConfig + + gguf_path = tmp_path / "z-image-turbo-Q4_K_M.gguf" + _write_gguf( + gguf_path, + [ + "layers.0.attention.qkv.weight", + "layers.0.feed_forward.w1.weight", + "layers.0.feed_forward.w2.weight", + "layers.0.feed_forward.w3.weight", + ], + ) + monkeypatch.setattr( + "sglang.multimodal_gen.runtime.loader.transformer_load_utils." + "_validate_gguf_runtime_support", + lambda *args: None, + ) + zimage = spec_mod.get_comfyui_checkpoint_spec("ZImagePipeline") + seen = _load( + monkeypatch, + replace(zimage, build_dit_config=lambda server_args: ZImageDitConfig()), + model_path=str(gguf_path), + ) + meta = seen["init_params"]["quant_config"].tensor_meta + weights = dict(seen["weights_iterator"]) + assert spec_mod.is_comfyui_single_file(str(gguf_path)) + assert seen["server_args"].model_paths["transformer"] == str(tmp_path) + assert meta["layers.0.attention.to_qkv.weight"].logical_shape == (2, 4) + assert meta["layers.0.feed_forward.w13.weight"].logical_shape == (4, 4) + assert list(weights) == [ + "layers.0.attention.to_qkv.weight", + "layers.0.feed_forward.w2.weight", + "layers.0.feed_forward.w13.weight", + ] + assert weights["layers.0.feed_forward.w13.weight"][:, 0].tolist() == [1, 1, 3, 3] + + +def test_gguf_unet_architecture_is_detected_from_its_header(tmp_path): + """Detection rejected every GGUF, so only MiniMax-H3 with a BF16 companion + file could load one.""" + from sglang.multimodal_gen.apps.ComfyUI_SGLDiffusion.core.generator import ( + _load_state_dict_for_detection, + ) + + path = tmp_path / "flux1-dev-Q4_K_M.gguf" + _write_gguf(path, ["img_in.weight"]) + state_dict = _load_state_dict_for_detection(str(path)) + assert {k: tuple(v.shape) for k, v in state_dict.items()} == { + "img_in.weight": (2, 4) + } + + +def test_flux_guidance_probe_reads_gguf_tensor_names(tmp_path): + from sglang.multimodal_gen.runtime.loader.comfyui_checkpoints import flux + + dev, schnell = tmp_path / "dev.gguf", tmp_path / "schnell.gguf" + _write_gguf(dev, ["img_in.weight", "guidance_in.in_layer.weight"]) + _write_gguf(schnell, ["img_in.weight"]) + assert flux._checkpoint_has_guidance_embeds(str(dev)) + assert not flux._checkpoint_has_guidance_embeds(str(schnell)) + + @pytest.mark.parametrize("quantization", ["fp8", "convrot_int8"]) def test_bf16_file_is_quantized_online(monkeypatch, quantization): """A BF16 file with --quantization used to load with no quant config, so the diff --git a/python/sglang/multimodal_gen/test/unit/test_comfyui_generator.py b/python/sglang/multimodal_gen/test/unit/test_comfyui_generator.py index 9f9d53020d..02f16cba7c 100644 --- a/python/sglang/multimodal_gen/test/unit/test_comfyui_generator.py +++ b/python/sglang/multimodal_gen/test/unit/test_comfyui_generator.py @@ -100,11 +100,16 @@ def _load_with_blocked_import(relpath, module_name, blocked): from unittest import mock import sglang.multimodal_gen.apps.ComfyUI_SGLDiffusion as plugin + from sglang.multimodal_gen.apps.ComfyUI_SGLDiffusion.executors.adapter import ( + _ADAPTERS, + ) path = Path(plugin.__file__).parent / relpath spec = importlib.util.spec_from_file_location(module_name, path) module = importlib.util.module_from_spec(spec) - with mock.patch.dict(sys.modules, {blocked: None}): # None makes import raise + # Private adapter classes register themselves while the module executes. + # Keep the real registry intact for subsequent tests. + with mock.patch.dict(_ADAPTERS), mock.patch.dict(sys.modules, {blocked: None}): spec.loader.exec_module(module) return module @@ -144,6 +149,59 @@ def test_generator_reports_real_runtime_import_error(caplog) -> None: assert isinstance(err.value.__cause__, ImportError) +def test_h3_executor_import_survives_a_missing_runtime() -> None: + """generator.py imports minimax_h3 unconditionally, so an unguarded runtime + import there hid every node, Flux and Z-Image included.""" + blocked = "sglang.multimodal_gen.configs.sample.minimax_h3" + module = _load_with_blocked_import( + "executors/minimax_h3.py", + "sglang.multimodal_gen.apps.ComfyUI_SGLDiffusion.executors._h3_blocked", + blocked, + ) + with pytest.raises(RuntimeError, match="failed to import") as err: + module.MiniMaxH3Executor() + assert isinstance(err.value.__cause__, ImportError) + + +def test_plugin_logs_why_nodes_failed_to_register(caplog) -> None: + module = _load_with_blocked_import( + "__init__.py", "_sgld_init_blocked", "_sgld_init_blocked.nodes" + ) + assert module.NODE_CLASS_MAPPINGS == {} + assert any("failed to register nodes" in r.getMessage() for r in caplog.records) + + +def test_undetectable_checkpoint_raises_instead_of_returning_none( + monkeypatch, +) -> None: + """A bare None return surfaced as 'cannot unpack non-iterable NoneType'.""" + import sys + + from sglang.multimodal_gen.apps.ComfyUI_SGLDiffusion.core import generator + + detection = SimpleNamespace( + unet_prefix_from_state_dict=lambda sd: "", + detect_unet_config=lambda sd, prefix: {"image_model": "flux"}, + model_config_from_unet=lambda sd, prefix: None, + convert_diffusers_mmdit=lambda sd, prefix: None, + model_config_from_diffusers_unet=lambda sd: None, + ) + management = SimpleNamespace(get_torch_device=lambda: "cpu") + utils = SimpleNamespace( + calculate_parameters=lambda sd: 0, + state_dict_prefix_replace=lambda sd, *a, **k: sd, + unet_to_diffusers=None, + ) + comfy = SimpleNamespace(model_detection=detection, model_management=management) + monkeypatch.setitem(sys.modules, "comfy", comfy) + monkeypatch.setitem(sys.modules, "comfy.utils", utils) + monkeypatch.setattr(generator, "_load_state_dict_for_detection", lambda p: {}) + runtime = SGLDiffusionGenerator() + runtime.pipeline_class_dict = {"flux": "FluxPipeline"} + with pytest.raises(ValueError, match="No ComfyUI model config"): + runtime.get_comfyui_model("/models/dit.safetensors") + + def _runtime_that_must_reject() -> SGLDiffusionGenerator: """A loader whose model build fails loudly, so a check that should reject first is caught if it lets the options through.""" diff --git a/python/sglang/multimodal_gen/test/unit/test_comfyui_lora_node.py b/python/sglang/multimodal_gen/test/unit/test_comfyui_lora_node.py index c2be9e996f..c0b6b95a0e 100644 --- a/python/sglang/multimodal_gen/test/unit/test_comfyui_lora_node.py +++ b/python/sglang/multimodal_gen/test/unit/test_comfyui_lora_node.py @@ -3,6 +3,7 @@ import copy import functools +import os import sys import types from contextlib import nullcontext @@ -42,6 +43,29 @@ def _load_nodes(): return nodes +def test_sgld_unet_list_adds_nested_gguf_without_registering_it(monkeypatch) -> None: + """Adding .gguf to the global registry made ComfyUI's stock loader offer + files it cannot load.""" + nodes = _load_nodes() + registry = {"diffusion_models": (["/models"], {".safetensors"})} + files = ["dit.safetensors", "flux/dit-Q4_K_M.gguf", "notes.txt"] + for name, value in { + "folder_names_and_paths": registry, + "get_filename_list": lambda folder: ["dit.safetensors"], + "get_folder_paths": lambda folder: registry[folder][0], + "recursive_search": lambda folder: (files, {}), + "filter_files_extensions": lambda names, exts: [ + n for n in names if os.path.splitext(n)[1] in exts + ], + }.items(): + monkeypatch.setattr(nodes.folder_paths, name, value, raising=False) + assert nodes._list_unet_names_including_gguf() == [ + "dit.safetensors", + "flux/dit-Q4_K_M.gguf", + ] + assert registry["diffusion_models"][1] == {".safetensors"} + + class _Model: """ModelPatcher stand-in: clone() copies patches like SGLDModelPatcher.clone."""