[Perf] GLM Image (#920)
Signed-off-by: JaredforReal <w13431838023@gmail.com> Signed-off-by: Jared Wen <w13431838023@gmail.com> Signed-off-by: Hongsheng Liu <liuhongsheng4@huawei.com> Co-authored-by: 汪志鹏 <wangzhipeng628@gmail.com> Co-authored-by: Hongsheng Liu <liuhongsheng4@huawei.com>
This commit is contained in:
25 files changed
+5182
-221
No files matched your search
@@ -0,0 +1,3 @@
|
||||
from .glm_image_ar import GlmImageForConditionalGeneration
|
||||
|
||||
__all__ = ["GlmImageForConditionalGeneration"]
|
||||
File diff suppressed because it is too large.
Load diff
@@ -63,6 +63,11 @@ _OMNI_MODELS = {
|
||||
"qwen3_tts_code2wav",
|
||||
"Qwen3TTSCode2Wav",
|
||||
),
|
||||
"GlmImageForConditionalGeneration": (
|
||||
"glm_image",
|
||||
"glm_image_ar",
|
||||
"GlmImageForConditionalGeneration",
|
||||
),
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
# Stage config for running GLM-Image with 2-stage architecture
|
||||
# Stage 0: AR Model (vLLM implementation) - generates prior_token_ids
|
||||
# Stage 1: Diffusion (DiT + VAE) - denoising and image decoding
|
||||
|
||||
stage_args:
|
||||
# Stage 0: AR Model (GlmImageForConditionalGeneration)
|
||||
# This stage uses the vLLM-optimized AR model to generate prior tokens
|
||||
# for conditioning the diffusion process.
|
||||
- stage_id: 0
|
||||
stage_type: llm
|
||||
runtime:
|
||||
process: true
|
||||
devices: "0"
|
||||
max_batch_size: 1
|
||||
requires_multimodal_data: true # Required for i2i mode to receive source images
|
||||
engine_args:
|
||||
model_stage: ar
|
||||
model_arch: GlmImageForConditionalGeneration
|
||||
model_subdir: vision_language_encoder # AR model config.json is in this subdirectory
|
||||
tokenizer_subdir: processor # Use processor's tokenizer (not ByT5 from tokenizer/)
|
||||
worker_cls: vllm_omni.worker.gpu_ar_worker.GPUARWorker
|
||||
scheduler_cls: vllm_omni.core.sched.omni_ar_scheduler.OmniARScheduler
|
||||
gpu_memory_utilization: 0.6
|
||||
enforce_eager: false
|
||||
trust_remote_code: true
|
||||
engine_output_type: token_ids # Output prior_token_ids for diffusion stage
|
||||
distributed_executor_backend: "mp"
|
||||
enable_prefix_caching: false
|
||||
max_num_batched_tokens: 32768
|
||||
final_output: false # AR is not the final output
|
||||
is_comprehension: true
|
||||
default_sampling_params:
|
||||
temperature: 0.9 # From model's generation_config.json
|
||||
top_p: 0.75 # From model's generation_config.json
|
||||
top_k: 16512 # vision_vocab_size from generation_config.json
|
||||
max_tokens: 1281 # For 1024x1024: small(16x16=256) + large(32x32=1024) + EOS(1)
|
||||
stop_token_ids: [16385] # eos_token_id from generation_config.json
|
||||
seed: 42
|
||||
detokenize: false
|
||||
|
||||
# Stage 1: Diffusion (DiT + VAE)
|
||||
# This stage receives prior_token_ids from AR and performs denoising + VAE decode
|
||||
- stage_id: 1
|
||||
stage_type: diffusion
|
||||
runtime:
|
||||
process: true
|
||||
devices: "1" # Can use different GPU, or same GPU if memory allows
|
||||
max_batch_size: 1
|
||||
requires_multimodal_data: true # Required for i2i mode to pass condition images
|
||||
engine_args:
|
||||
model_stage: dit
|
||||
model_arch: GlmImagePipeline # Required for diffusion model class resolution
|
||||
# Diffusion-specific parameters
|
||||
num_gpus: 1
|
||||
enforce_eager: true
|
||||
trust_remote_code: true
|
||||
distributed_executor_backend: "mp"
|
||||
engine_input_source: [0] # Input from AR stage
|
||||
custom_process_input_func: vllm_omni.model_executor.stage_input_processors.glm_image.ar2diffusion
|
||||
final_output: true
|
||||
final_output_type: image
|
||||
default_sampling_params:
|
||||
# Diffusion-specific parameters only (no LLM params like temperature/top_p/top_k)
|
||||
seed: 42
|
||||
num_inference_steps: 50
|
||||
guidance_scale: 1.5
|
||||
height: 1024
|
||||
width: 1024
|
||||
|
||||
# Top-level runtime config
|
||||
runtime:
|
||||
enabled: true
|
||||
defaults:
|
||||
window_size: -1 # Trigger downstream only after full upstream completion
|
||||
max_inflight: 1 # Process serially within each stage
|
||||
|
||||
edges:
|
||||
- from: 0 # AR → Diffusion: trigger after AR completes
|
||||
to: 1
|
||||
window_size: -1
|
||||
@@ -0,0 +1,93 @@
|
||||
# Stage config for running GLM-Image with 2-stage architecture (MultiConnector version)
|
||||
# Stage 0: AR Model (vLLM implementation) - generates prior_token_ids
|
||||
# Stage 1: Diffusion (DiT + VAE) - denoising and image decoding
|
||||
#
|
||||
# This config uses OmniConnectors for inter-stage communication,
|
||||
# enabling efficient tensor transfer between stages on different processes/nodes.
|
||||
|
||||
stage_args:
|
||||
# Stage 0: AR Model (GlmImageForConditionalGeneration)
|
||||
# This stage uses the vLLM-optimized AR model to generate prior tokens
|
||||
# for conditioning the diffusion process.
|
||||
- stage_id: 0
|
||||
stage_type: llm
|
||||
runtime:
|
||||
process: true
|
||||
devices: "0"
|
||||
max_batch_size: 1
|
||||
requires_multimodal_data: true # Required for i2i mode to receive source images
|
||||
engine_args:
|
||||
model_stage: ar
|
||||
model_arch: GlmImageForConditionalGeneration
|
||||
worker_cls: vllm_omni.worker.gpu_ar_worker.GPUARWorker
|
||||
scheduler_cls: vllm_omni.core.sched.omni_ar_scheduler.OmniARScheduler
|
||||
gpu_memory_utilization: 0.6
|
||||
enforce_eager: false
|
||||
trust_remote_code: true
|
||||
engine_output_type: token_ids # Output prior_token_ids for diffusion stage
|
||||
distributed_executor_backend: "mp"
|
||||
enable_prefix_caching: false
|
||||
max_num_batched_tokens: 32768
|
||||
hf_config_name: vision_language_encoder # Subfolder in model path
|
||||
final_output: false # AR is not the final output
|
||||
is_comprehension: true
|
||||
default_sampling_params:
|
||||
temperature: 0.9 # From model's generation_config.json
|
||||
top_p: 0.75 # From model's generation_config.json
|
||||
top_k: 16512 # vision_vocab_size from generation_config.json
|
||||
max_tokens: 1281 # For 1024x1024: small(16x16=256) + large(32x32=1024) + EOS(1)
|
||||
stop_token_ids: [16385] # eos_token_id from generation_config.json
|
||||
seed: 42
|
||||
detokenize: false
|
||||
|
||||
# Stage 1: Diffusion (DiT + VAE)
|
||||
# This stage receives prior_token_ids from AR and performs denoising + VAE decode
|
||||
- stage_id: 1
|
||||
stage_type: diffusion
|
||||
runtime:
|
||||
process: true
|
||||
devices: "1" # Use separate GPU for diffusion
|
||||
max_batch_size: 1
|
||||
requires_multimodal_data: true # Required for i2i mode to pass condition images
|
||||
engine_args:
|
||||
model_stage: dit
|
||||
# Diffusion-specific parameters
|
||||
num_gpus: 1
|
||||
enforce_eager: true
|
||||
trust_remote_code: true
|
||||
distributed_executor_backend: "mp"
|
||||
engine_input_source: [0] # Input from AR stage
|
||||
custom_process_input_func: vllm_omni.model_executor.stage_input_processors.glm_image.ar2diffusion
|
||||
final_output: true
|
||||
final_output_type: image
|
||||
default_sampling_params:
|
||||
seed: 42
|
||||
num_inference_steps: 50
|
||||
guidance_scale: 1.5
|
||||
height: 1024
|
||||
width: 1024
|
||||
|
||||
# Top-level runtime config with MultiConnector support
|
||||
runtime:
|
||||
enabled: true
|
||||
defaults:
|
||||
window_size: -1 # Trigger downstream only after full upstream completion
|
||||
max_inflight: 1 # Process serially within each stage
|
||||
|
||||
edges:
|
||||
- from: 0 # AR → Diffusion
|
||||
to: 1
|
||||
window_size: -1
|
||||
|
||||
# OmniConnector configuration for efficient inter-stage tensor transfer
|
||||
connectors:
|
||||
- type: tensor_transfer
|
||||
source_stage: 0
|
||||
target_stage: 1
|
||||
# Transfer prior_token_ids efficiently between stages
|
||||
fields:
|
||||
- name: prior_token_ids
|
||||
dtype: int64
|
||||
- name: prior_token_image_ids
|
||||
dtype: int64
|
||||
optional: true
|
||||
@@ -0,0 +1,265 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
"""Stage input processor for GLM-Image: AR → Diffusion transition."""
|
||||
|
||||
from typing import Any
|
||||
|
||||
import torch
|
||||
from vllm.inputs import TextPrompt
|
||||
from vllm.logger import init_logger
|
||||
|
||||
from vllm_omni.inputs.data import OmniTokensPrompt
|
||||
|
||||
logger = init_logger(__name__)
|
||||
|
||||
|
||||
def _upsample_token_ids(token_ids: torch.Tensor, token_h: int, token_w: int) -> torch.Tensor:
|
||||
"""Upsample token IDs by 2x using nearest neighbor interpolation.
|
||||
|
||||
GLM-Image AR model generates tokens at 32x downsampling, but DiT expects
|
||||
16x downsampling, so we need to upsample by 2x.
|
||||
|
||||
Args:
|
||||
token_ids: Prior token IDs of shape [num_tokens]
|
||||
token_h: Height in token space (at 32x downsampling)
|
||||
token_w: Width in token space (at 32x downsampling)
|
||||
|
||||
Returns:
|
||||
Upsampled token IDs of shape [num_tokens * 4]
|
||||
"""
|
||||
token_ids = token_ids.view(1, 1, token_h, token_w)
|
||||
token_ids = torch.nn.functional.interpolate(token_ids.float(), scale_factor=2, mode="nearest").to(dtype=torch.long)
|
||||
token_ids = token_ids.view(-1)
|
||||
return token_ids
|
||||
|
||||
|
||||
def _parse_generated_tokens(
|
||||
token_ids: list[int],
|
||||
height: int,
|
||||
width: int,
|
||||
factor: int = 32,
|
||||
is_i2i: bool = False,
|
||||
) -> tuple[torch.Tensor, int, int]:
|
||||
"""Parse AR-generated tokens to extract prior_token_ids.
|
||||
|
||||
Args:
|
||||
token_ids: Generated token IDs from AR model
|
||||
height: Target image height
|
||||
width: Target image width
|
||||
factor: Downsampling factor (default 32)
|
||||
is_i2i: Whether this is image-to-image mode. In i2i mode, the AR model
|
||||
generates only large image tokens (no small preview tokens).
|
||||
"""
|
||||
# Calculate token dimensions for target image
|
||||
token_h = height // factor
|
||||
token_w = width // factor
|
||||
large_image_tokens = token_h * token_w
|
||||
|
||||
# Calculate small preview image dimensions (used in text-to-image)
|
||||
small_token_h = token_h // 2
|
||||
small_token_w = token_w // 2
|
||||
small_image_tokens = small_token_h * small_token_w
|
||||
|
||||
token_tensor = torch.tensor(token_ids, dtype=torch.long)
|
||||
|
||||
# Remove EOS token (16385) from the end if present
|
||||
eos_token_id = 16385
|
||||
if len(token_ids) > 0 and token_ids[-1] == eos_token_id:
|
||||
token_tensor = token_tensor[:-1]
|
||||
|
||||
actual_tokens = len(token_tensor)
|
||||
|
||||
logger.debug(
|
||||
f"[_parse_generated_tokens] height={height}, width={width}, "
|
||||
f"token_h={token_h}, token_w={token_w}, "
|
||||
f"large_image_tokens={large_image_tokens}, small_image_tokens={small_image_tokens}, "
|
||||
f"actual_tokens={actual_tokens}"
|
||||
)
|
||||
|
||||
if is_i2i:
|
||||
# Image-to-image mode: check if AR generated small+large tokens (like t2i) or just large tokens
|
||||
# Some AR models output small+large even in i2i mode
|
||||
if actual_tokens >= small_image_tokens + large_image_tokens:
|
||||
# AR generated full t2i-style output, extract large tokens after small
|
||||
large_start = small_image_tokens
|
||||
large_end = large_start + large_image_tokens
|
||||
prior_token_ids_d32 = token_tensor[large_start:large_end]
|
||||
actual_h, actual_w = token_h, token_w
|
||||
else:
|
||||
# AR generated only large tokens (pure i2i output)
|
||||
prior_token_ids_d32 = token_tensor[:large_image_tokens]
|
||||
actual_h, actual_w = token_h, token_w
|
||||
elif actual_tokens >= small_image_tokens + large_image_tokens:
|
||||
# Text-to-image: extract large image tokens after small image tokens
|
||||
large_start = small_image_tokens
|
||||
large_end = large_start + large_image_tokens
|
||||
prior_token_ids_d32 = token_tensor[large_start:large_end]
|
||||
actual_h, actual_w = token_h, token_w
|
||||
elif actual_tokens >= large_image_tokens:
|
||||
# Image-to-image: large image tokens are at the beginning
|
||||
prior_token_ids_d32 = token_tensor[:large_image_tokens]
|
||||
actual_h, actual_w = token_h, token_w
|
||||
else:
|
||||
# Insufficient tokens - try to infer the actual grid size
|
||||
import math
|
||||
|
||||
for scale in [1, 2, 4]:
|
||||
test_h = token_h // scale
|
||||
test_w = token_w // scale
|
||||
test_small_h = test_h // 2
|
||||
test_small_w = test_w // 2
|
||||
test_large = test_h * test_w
|
||||
test_small = test_small_h * test_small_w
|
||||
|
||||
if actual_tokens >= test_small + test_large:
|
||||
prior_token_ids_d32 = token_tensor[test_small : test_small + test_large]
|
||||
actual_h, actual_w = test_h, test_w
|
||||
height = test_h * factor
|
||||
width = test_w * factor
|
||||
logger.warning(f"Adjusted grid to {test_h}x{test_w}, output will be {height}x{width}")
|
||||
break
|
||||
elif actual_tokens >= test_large:
|
||||
prior_token_ids_d32 = token_tensor[:test_large]
|
||||
actual_h, actual_w = test_h, test_w
|
||||
height = test_h * factor
|
||||
width = test_w * factor
|
||||
logger.warning(f"Adjusted grid to {test_h}x{test_w}, output will be {height}x{width}")
|
||||
break
|
||||
else:
|
||||
sqrt_tokens = int(math.sqrt(actual_tokens))
|
||||
actual_h = actual_w = sqrt_tokens
|
||||
usable_tokens = sqrt_tokens * sqrt_tokens
|
||||
prior_token_ids_d32 = token_tensor[:usable_tokens]
|
||||
height = sqrt_tokens * factor
|
||||
width = sqrt_tokens * factor
|
||||
logger.error(f"Grid pattern mismatch. Using {sqrt_tokens}x{sqrt_tokens}, output: {height}x{width}")
|
||||
|
||||
# Upsample from 32x to 16x
|
||||
prior_token_ids = _upsample_token_ids(prior_token_ids_d32, actual_h, actual_w)
|
||||
|
||||
return prior_token_ids, height, width
|
||||
|
||||
|
||||
def ar2diffusion(
|
||||
stage_list: list[Any],
|
||||
engine_input_source: list[int],
|
||||
prompt: OmniTokensPrompt | TextPrompt | list | None = None,
|
||||
requires_multimodal_data: bool = False,
|
||||
) -> list[dict[str, Any]]:
|
||||
"""Process AR stage outputs to create Diffusion stage inputs."""
|
||||
if not engine_input_source:
|
||||
raise ValueError("engine_input_source cannot be empty")
|
||||
|
||||
source_stage_id = engine_input_source[0]
|
||||
if source_stage_id >= len(stage_list):
|
||||
raise IndexError(f"Invalid stage_id: {source_stage_id}")
|
||||
|
||||
if stage_list[source_stage_id].engine_outputs is None:
|
||||
raise RuntimeError(f"Stage {source_stage_id} has no outputs yet")
|
||||
|
||||
ar_outputs = stage_list[source_stage_id].engine_outputs
|
||||
diffusion_inputs = []
|
||||
|
||||
# Normalize prompt to list
|
||||
if not isinstance(prompt, list):
|
||||
prompt = [prompt] if prompt is not None else [{}]
|
||||
|
||||
for i, ar_output in enumerate(ar_outputs):
|
||||
output = ar_output.outputs[0]
|
||||
generated_token_ids = output.token_ids
|
||||
|
||||
# Get original prompt info
|
||||
original_prompt = prompt[i] if i < len(prompt) else {}
|
||||
if isinstance(original_prompt, dict):
|
||||
pass
|
||||
elif hasattr(original_prompt, "_asdict"):
|
||||
original_prompt = original_prompt._asdict()
|
||||
elif hasattr(original_prompt, "__dict__"):
|
||||
original_prompt = vars(original_prompt)
|
||||
else:
|
||||
original_prompt = {}
|
||||
|
||||
height = original_prompt.get("height", 1024)
|
||||
width = original_prompt.get("width", 1024)
|
||||
text_prompt = original_prompt.get("prompt", "")
|
||||
|
||||
# Detect i2i mode first by checking if multimodal_output contains prior_token_image_ids
|
||||
is_i2i = False
|
||||
if hasattr(ar_output, "multimodal_output") and ar_output.multimodal_output:
|
||||
mm_output = ar_output.multimodal_output
|
||||
if isinstance(mm_output, dict) and mm_output.get("prior_token_image_ids") is not None:
|
||||
is_i2i = True
|
||||
|
||||
# Parse and upsample prior tokens
|
||||
prior_token_ids, pixel_h, pixel_w = _parse_generated_tokens(generated_token_ids, height, width, is_i2i=is_i2i)
|
||||
|
||||
# Get prior_token_image_ids from AR model output (for i2i mode)
|
||||
# This contains VQ-VAE tokens from input image, used for KV cache conditioning
|
||||
# NOTE: multimodal_output is attached to ar_output (RequestOutput), NOT output (CompletionOutput)
|
||||
prior_token_image_ids = None
|
||||
|
||||
# Check ar_output (RequestOutput) for multimodal_output - this is the correct location
|
||||
if hasattr(ar_output, "multimodal_output") and ar_output.multimodal_output:
|
||||
mm_output = ar_output.multimodal_output
|
||||
if isinstance(mm_output, dict):
|
||||
raw_prior_image_ids = mm_output.get("prior_token_image_ids")
|
||||
if raw_prior_image_ids is not None:
|
||||
# Handle different formats:
|
||||
# 1. Single tensor -> wrap in list
|
||||
# 2. List of tensors -> use as-is
|
||||
# 3. List of Python lists (from serialization) -> convert to tensors
|
||||
if isinstance(raw_prior_image_ids, torch.Tensor):
|
||||
prior_token_image_ids = [raw_prior_image_ids]
|
||||
elif isinstance(raw_prior_image_ids, list):
|
||||
# Check if elements are tensors or Python lists
|
||||
if raw_prior_image_ids and isinstance(raw_prior_image_ids[0], torch.Tensor):
|
||||
prior_token_image_ids = raw_prior_image_ids
|
||||
elif raw_prior_image_ids and isinstance(raw_prior_image_ids[0], list):
|
||||
# Convert Python lists back to tensors
|
||||
prior_token_image_ids = [torch.tensor(ids, dtype=torch.long) for ids in raw_prior_image_ids]
|
||||
else:
|
||||
logger.warning(
|
||||
f"[ar2diffusion] Request {i}: unexpected prior_token_image_ids format: "
|
||||
f"{type(raw_prior_image_ids[0]) if raw_prior_image_ids else 'empty'}"
|
||||
)
|
||||
else:
|
||||
# Fallback: also check output (CompletionOutput) in case of different vLLM versions
|
||||
if hasattr(output, "multimodal_output") and output.multimodal_output:
|
||||
mm_output = output.multimodal_output
|
||||
logger.debug(f"[ar2diffusion] Request {i}: found multimodal_output on CompletionOutput (fallback)")
|
||||
if isinstance(mm_output, dict):
|
||||
raw_prior_image_ids = mm_output.get("prior_token_image_ids")
|
||||
if raw_prior_image_ids is not None:
|
||||
if isinstance(raw_prior_image_ids, torch.Tensor):
|
||||
prior_token_image_ids = [raw_prior_image_ids]
|
||||
elif isinstance(raw_prior_image_ids, list):
|
||||
prior_token_image_ids = raw_prior_image_ids
|
||||
|
||||
diffusion_input = {
|
||||
"prompt": text_prompt,
|
||||
"height": pixel_h,
|
||||
"width": pixel_w,
|
||||
"extra": {
|
||||
"prior_token_ids": prior_token_ids,
|
||||
"prior_token_image_ids": prior_token_image_ids,
|
||||
},
|
||||
}
|
||||
|
||||
if requires_multimodal_data:
|
||||
mm_data = original_prompt.get("multi_modal_data")
|
||||
if mm_data:
|
||||
pil_image = mm_data.get("image")
|
||||
if pil_image is None:
|
||||
# Try "images" (plural) as fallback
|
||||
images = mm_data.get("images")
|
||||
if images:
|
||||
pil_image = images[0] if isinstance(images, list) else images
|
||||
diffusion_input["pil_image"] = pil_image
|
||||
|
||||
for key in ["seed", "num_inference_steps", "guidance_scale", "negative_prompt"]:
|
||||
if key in original_prompt:
|
||||
diffusion_input[key] = original_prompt[key]
|
||||
|
||||
diffusion_inputs.append(diffusion_input)
|
||||
|
||||
return diffusion_inputs
|
||||
Reference in new issue
Block a user