[diffusion] comfyui: report the real runtime import error (#43178)

This commit is contained in:
Avaya Aggarwal
2026-10-10 09:05:12 +08:00
committed by GitHub
parent ff3aa58f11
commit 66e6b68d82
3 changed files with 75 additions and 4 deletions
@@ -101,10 +101,16 @@ def _load_state_dict_for_detection(model_path: str):
try:
from sglang.multimodal_gen import DiffGenerator
except ImportError:
except ImportError as exc:
# Keep the nodes importable so ComfyUI shows this; fail clearly on first use.
_RUNTIME_IMPORT_ERROR: ImportError | None = exc
logger.error(
"Error: sglang.multimodal_gen is not installed. Please install it using 'pip install sglang[diffusion]'"
"Failed to import the SGLang diffusion runtime: %r. Install the diffusion "
"extras with 'pip install sglang[diffusion]'.",
exc,
)
else:
_RUNTIME_IMPORT_ERROR = None
def _load_executor_classes():
@@ -185,6 +191,10 @@ class SGLDiffusionGenerator:
"""Initialize the diffusion generator."""
if self.generator is not None:
return self.generator
if _RUNTIME_IMPORT_ERROR is not None:
raise RuntimeError(
"SGLang diffusion runtime failed to import"
) from _RUNTIME_IMPORT_ERROR
if kwargs is None:
kwargs = {}
# Set comfyui_mode for ComfyUI integration
@@ -9,10 +9,15 @@ import torch
try:
from sglang.multimodal_gen.configs.sample.sampling_params import SamplingParams
from sglang.multimodal_gen.runtime.entrypoints.utils import prepare_request
except ImportError:
except ImportError as exc:
# Keep the nodes importable so ComfyUI shows this; fail clearly on first use.
_RUNTIME_IMPORT_ERROR: ImportError | None = exc
print(
"Error: sglang.multimodal_gen is not installed. Please install it using 'pip install sglang[diffusion]'"
f"Error: failed to import the SGLang diffusion runtime: {exc!r}. "
"Install the diffusion extras with 'pip install sglang[diffusion]'."
)
else:
_RUNTIME_IMPORT_ERROR = None
class SGLDiffusionExecutor(torch.nn.Module):
@@ -115,6 +120,10 @@ class SGLDiffusionExecutor(torch.nn.Module):
}
def _execute_packed(self, packed, x, timestep):
if _RUNTIME_IMPORT_ERROR is not None:
raise RuntimeError(
"SGLang diffusion runtime failed to import"
) from _RUNTIME_IMPORT_ERROR
ensure = getattr(self, "_ensure_runtime", None)
if ensure is not None:
ensure(self)
@@ -83,3 +83,55 @@ def test_kill_generator_only_touches_owned_workers() -> None:
runtime.kill_generator()
assert owned.terminated is True
assert owned.killed is False
def _load_with_blocked_import(relpath, module_name, blocked):
"""Import a private copy of a plugin module while ``blocked`` fails to import."""
import importlib.util
import sys
from pathlib import Path
from unittest import mock
import sglang.multimodal_gen.apps.ComfyUI_SGLDiffusion as plugin
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
spec.loader.exec_module(module)
return module
def test_executor_reports_real_runtime_import_error(capsys) -> None:
import pytest
import torch
blocked = "sglang.multimodal_gen.runtime.entrypoints.utils"
base = _load_with_blocked_import("executors/base.py", "_sgld_base_blocked", blocked)
printed = capsys.readouterr().out
assert blocked in printed
assert "is not installed" not in printed
class _Executor(base.SGLDiffusionExecutor):
def __init__(self):
torch.nn.Module.__init__(self)
with pytest.raises(RuntimeError, match="failed to import") as err:
_Executor()._execute_packed(None, None, None)
assert isinstance(err.value.__cause__, ImportError)
assert blocked in str(err.value.__cause__)
def test_generator_reports_real_runtime_import_error(caplog) -> None:
import pytest
blocked = "sglang.multimodal_gen"
module = _load_with_blocked_import(
"core/generator.py",
"sglang.multimodal_gen.apps.ComfyUI_SGLDiffusion.core._generator_blocked",
blocked,
)
assert any(blocked in record.getMessage() for record in caplog.records)
with pytest.raises(RuntimeError, match="failed to import") as err:
module.SGLDiffusionGenerator().init_generator("flux", "FluxPipeline", {})
assert isinstance(err.value.__cause__, ImportError)