[diffusion] comfyui: return several server-mode images as one IMAGE batch (#44088)

Co-authored-by: niehen6174 <1639206518@qq.com>
This commit is contained in:
Avaya Aggarwal
2026-10-15 10:38:37 +08:00
committed by GitHub
co-authored by niehen6174
parent 4851d182a1
commit 0cf683ea8d
2 changed files with 40 additions and 8 deletions
@@ -33,7 +33,7 @@ Connect to a standalone SGLang Diffusion server.
1. **Start SGLang Diffusion Server**: Ensure the server is running and accessible.
2. **Connect to Server**: Use the `SGLDiffusion Server Model` node to connect (default: `http://localhost:3000/v1`).
3. **Generate Content**:
- `SGLDiffusion Generate Image`: For text-to-image and image editing.
- `SGLDiffusion Generate Image`: For text-to-image and image editing. Set `num_images` (1 to 10) to get several images from one request as one `IMAGE` batch, in order. Image `i` uses `seed + i`, so the same settings and seed reproduce the same batch; a seed of `-1` lets the server pick a random seed.
- `SGLDiffusion Generate Video`: For text-to-video and image-to-video.
- `SGLDiffusion Generate MiniMax-H3`: For joint video-and-audio on a standalone server.
4. **LoRA Support**: Use `SGLDiffusion Server Set LoRA` and `SGLDiffusion Server Unset LoRA`.
@@ -28,6 +28,28 @@ from .utils import (
resolve_video_path,
)
# The server clamps n to this range (image_api.py).
MAX_SERVER_IMAGES = 10
def _decode_image_batch(response: dict, num_images: int) -> torch.Tensor:
"""Decode the server's images, in order, into one ComfyUI IMAGE batch."""
items = [item.get("b64_json") for item in response.get("data") or []]
if not items or not all(items):
raise RuntimeError("No image data in response")
if len(items) < num_images:
raise RuntimeError(
f"Requested {num_images} images but the server returned {len(items)}"
)
images = [convert_b64_to_tensor_image(b64) for b64 in items[:num_images]]
shapes = {tuple(image.shape[1:]) for image in images}
if len(shapes) > 1:
raise RuntimeError(
f"Server returned images of different sizes {sorted(shapes)}; "
"they cannot be combined into one IMAGE batch"
)
return torch.cat(images, dim=0)
class SGLDH3DistilledSigmas:
"""The checkpoint's trained rectified-flow grid, independent of scheduler presets."""
@@ -420,6 +442,16 @@ class SGLDiffusionGenerateImage:
"default": False,
},
),
"num_images": (
"INT",
{
"default": 1,
"min": 1,
"max": MAX_SERVER_IMAGES,
"tooltip": "Images to generate in one request, returned as "
"one IMAGE batch. Image i uses seed + i.",
},
),
},
}
@@ -441,10 +473,15 @@ class SGLDiffusionGenerateImage:
width: int = 1024,
height: int = 1024,
enable_teacache: bool = False,
num_images: int = 1,
):
"""Generate image using SGLang Diffusion API."""
if not positive_prompt:
raise ValueError("Prompt cannot be empty")
if not 1 <= num_images <= MAX_SERVER_IMAGES:
raise ValueError(
f"num_images must be between 1 and {MAX_SERVER_IMAGES}, got {num_images}"
)
size = f"{width}x{height}"
@@ -452,6 +489,7 @@ class SGLDiffusionGenerateImage:
request_params = {
"prompt": positive_prompt,
"size": size,
"n": num_images,
"response_format": "b64_json",
}
@@ -481,13 +519,7 @@ class SGLDiffusionGenerateImage:
except Exception as e:
raise RuntimeError(f"Failed to generate image: {str(e)}")
# Decode base64 image
if not response["data"] or not response["data"][0]["b64_json"]:
raise RuntimeError("No image data in response")
image_data = response["data"][0]["b64_json"]
image = convert_b64_to_tensor_image(image_data)
return (image,)
return (_decode_image_batch(response, num_images),)
class SGLDiffusionGenerateVideo: