[Model] Add BAGEL step execution and continuous batching (#6359)

This commit is contained in:
Trigger authored and GitHub committed 2026-09-04 01:36:11 +00:00
1 parent fda3af8bbc
commit e43c787544
24 files changed
+1965 -108

No files matched your search

@@ -9,6 +9,7 @@
from collections.abc import Iterable, Sequence
from dataclasses import dataclass
from typing import Any
import numpy as np
import torch
@@ -364,15 +365,19 @@ class NaiveCache:
@staticmethod
def merge(caches: Sequence["NaiveCache"]) -> "NaiveCache":
"""Merge per-branch NaiveCaches into one for batched attention;
this lets us do the forward passes for CFG in one batched pass,
although it's worth noting that this is currently only used
for single request. We need this so that gen mode knows the
respective kv lengths, and can split things back out as needed.
"""Merge NaiveCaches into one for packed request/CFG attention.
Already-merged inputs keep their per-sequence lengths so nested
request-by-CFG packing can split cache rows back out correctly.
"""
num_layers = caches[0].num_layers
merged = NaiveCache(num_layers)
lens = [c.seq_lens for c in caches]
lens = []
for cache in caches:
if cache.key_values_lens is None:
lens.append(cache.seq_lens)
else:
lens.extend(cache.key_values_lens)
merged.key_values_lens = lens
nonempty = [c for c in caches if c.key_cache[0] is not None]
@@ -1251,12 +1256,22 @@ class Bagel(CFGParallelMixin, nn.Module):
config_class = BagelConfig
base_model_prefix = "bagel"
# Flow-matching denoise schedule convention. Official BAGEL samples
# ``num_timesteps`` points over [1, 0] and drops the terminal t=0, yielding
# ``num_timesteps - 1`` Euler steps. Lance samples one extra point
# (``num_timesteps + 1``) for ``num_timesteps`` steps; ``LanceBagel`` flips
# this on. See https://github.com/vllm-project/vllm-omni/issues/4470.
_denoise_schedule_extra_step: bool = False
# Include the terminal t=0 sample so ``num_timesteps`` means exactly
# ``num_timesteps`` Euler updates. Subclasses may override this for a
# model-specific schedule convention.
_denoise_schedule_extra_step: bool = True
def prepare_denoise_schedule(
self,
x_t: torch.Tensor,
num_timesteps: int,
timestep_shift: float,
) -> tuple[torch.Tensor, torch.Tensor]:
"""Build BAGEL's flow-matching timesteps and per-step deltas."""
num_sample_points = num_timesteps + 1 if self._denoise_schedule_extra_step else num_timesteps
schedule = torch.linspace(1, 0, num_sample_points, device=x_t.device)
schedule = timestep_shift * schedule / (1 + (timestep_shift - 1) * schedule)
return schedule[:-1], schedule[:-1] - schedule[1:]
def __init__(
self,
@@ -1819,14 +1834,8 @@ class Bagel(CFGParallelMixin, nn.Module):
frame_condition_token_indexes = frame_condition_token_indexes.to(x_t.device).long()
pinned_x_t = x_t[frame_condition_token_indexes].clone()
# Build the flow-matching schedule. BAGEL drops the terminal t=0 for
# ``num_timesteps - 1`` Euler steps; Lance keeps it for ``num_timesteps``.
# ``_denoise_schedule_extra_step`` (overridden by ``LanceBagel``) selects which.
num_sample_points = num_timesteps + 1 if self._denoise_schedule_extra_step else num_timesteps
timesteps = torch.linspace(1, 0, num_sample_points, device=x_t.device)
timesteps = timestep_shift * timesteps / (1 + (timestep_shift - 1) * timesteps)
dts = timesteps[:-1] - timesteps[1:]
timesteps = timesteps[:-1]
# Build ``num_timesteps`` flow-matching Euler updates over [1, 0].
timesteps, dts = Bagel.prepare_denoise_schedule(self, x_t, num_timesteps, timestep_shift)
# Optional trajectory recording for RL rollout data collection
trajectory_latents: list[torch.Tensor] | None = [] if return_trajectory_latents else None
@@ -2228,6 +2237,52 @@ class Bagel(CFGParallelMixin, nn.Module):
return v_t
def _combine_cfg_per_request(
self,
v_t: torch.Tensor,
cfg_text_v_t: torch.Tensor,
cfg_img_v_t: torch.Tensor | None,
cfg_vae_lengths: Sequence[int],
cfg_text_scales: Sequence[float],
cfg_img_scales: Sequence[float],
cfg_renorm_type: str,
cfg_renorm_min: float,
) -> torch.Tensor:
"""Apply BAGEL CFG independently to each packed request."""
if len(cfg_text_scales) != len(cfg_vae_lengths):
raise ValueError("cfg_text_scales must match cfg_vae_lengths.")
if len(cfg_img_scales) != len(cfg_vae_lengths):
raise ValueError("cfg_img_scales must match cfg_vae_lengths.")
gen_parts = v_t.split(list(cfg_vae_lengths))
text_parts = cfg_text_v_t.split(list(cfg_vae_lengths))
img_parts = (
cfg_img_v_t.split(list(cfg_vae_lengths)) if cfg_img_v_t is not None else [None] * len(cfg_vae_lengths)
)
return torch.cat(
[
gen_part
if text_scale <= 1.0
else self._combine_cfg(
gen_part,
text_part,
img_part,
text_scale,
img_scale,
cfg_renorm_type,
cfg_renorm_min,
)
for gen_part, text_part, img_part, text_scale, img_scale in zip(
gen_parts,
text_parts,
img_parts,
cfg_text_scales,
cfg_img_scales,
strict=True,
)
]
)
# ── CFGParallelMixin hooks ──
# Bagel mounts CFGParallelMixin (see class declaration) to reuse the shared
# N-branch CFG dispatch/all_gather logic in predict_noise_with_multi_branch_cfg.
@@ -2247,7 +2302,7 @@ class Bagel(CFGParallelMixin, nn.Module):
def combine_multi_branch_cfg_noise(
self,
predictions: list[torch.Tensor],
true_cfg_scale: dict[str, float],
true_cfg_scale: dict[str, Any],
cfg_normalize: bool = False,
) -> torch.Tensor:
"""Combine gen/text/img branch velocities via Bagel's renorm CFG.
@@ -2257,6 +2312,18 @@ class Bagel(CFGParallelMixin, nn.Module):
because renormalization is folded into ``_combine_cfg`` itself.
"""
cfg_img_v_t = predictions[2] if len(predictions) > 2 else None
cfg_vae_lengths = true_cfg_scale.get("cfg_vae_lengths")
if cfg_vae_lengths is not None:
return self._combine_cfg_per_request(
predictions[0],
predictions[1],
cfg_img_v_t,
cfg_vae_lengths,
true_cfg_scale["cfg_text_scales"],
true_cfg_scale["cfg_img_scales"],
true_cfg_scale["cfg_renorm_type"],
true_cfg_scale["cfg_renorm_min"],
)
return self._combine_cfg(
predictions[0],
predictions[1],
@@ -2270,7 +2337,7 @@ class Bagel(CFGParallelMixin, nn.Module):
def forward_single_branch(
self,
x_t: torch.Tensor,
timestep: torch.LongTensor,
timestep: torch.Tensor,
packed_vae_token_indexes: torch.LongTensor,
packed_vae_position_ids: torch.LongTensor,
packed_text_ids: torch.LongTensor,
@@ -2379,7 +2446,7 @@ class Bagel(CFGParallelMixin, nn.Module):
def forward(
self,
x_t: torch.Tensor,
timestep: torch.LongTensor,
timestep: torch.Tensor,
packed_vae_token_indexes: torch.LongTensor,
packed_vae_position_ids: torch.LongTensor,
packed_text_ids: torch.LongTensor,
@@ -2393,6 +2460,9 @@ class Bagel(CFGParallelMixin, nn.Module):
cfg_img_scale: float = 1.0,
cfg_branch_pids: list[torch.Tensor] | None = None,
cfg_branch_caches: list[NaiveCache] | None = None,
cfg_vae_lengths: list[int] | None = None,
cfg_text_scales: list[float] | None = None,
cfg_img_scales: list[float] | None = None,
):
# Build query sequence (identical for all CFG branches)
packed_text_embedding = self.language_model.forward(
@@ -2416,11 +2486,16 @@ class Bagel(CFGParallelMixin, nn.Module):
extra_inputs["packed_vae_token_indexes"] = packed_vae_token_indexes
extra_inputs["packed_text_indexes"] = packed_text_indexes
use_cfg = cfg_text_scale > 1.0
has_cfg_branches = cfg_branch_pids is not None and cfg_branch_caches is not None
use_cfg = has_cfg_branches and (
cfg_text_scale > 1.0 or (cfg_text_scales is not None and any(scale > 1.0 for scale in cfg_text_scales))
)
cfg_text_v_t = None
cfg_img_v_t = None
if use_cfg and cfg_branch_pids is not None and cfg_branch_caches is not None:
if use_cfg:
assert cfg_branch_pids is not None
assert cfg_branch_caches is not None
num_branches = len(cfg_branch_pids)
seq_len = int(packed_seqlens.sum())
@@ -2466,14 +2541,30 @@ class Bagel(CFGParallelMixin, nn.Module):
# ── CFG combination ──
if use_cfg:
v_t = self._combine_cfg(
v_t,
cfg_text_v_t,
cfg_img_v_t,
cfg_text_scale,
cfg_img_scale,
cfg_renorm_type,
cfg_renorm_min,
)
if cfg_vae_lengths is None:
v_t = self._combine_cfg(
v_t,
cfg_text_v_t,
cfg_img_v_t,
cfg_text_scale,
cfg_img_scale,
cfg_renorm_type,
cfg_renorm_min,
)
else:
if cfg_text_scales is None:
raise ValueError("cfg_text_scales must be provided with cfg_vae_lengths.")
if cfg_img_scales is None:
cfg_img_scales = [cfg_img_scale] * len(cfg_vae_lengths)
v_t = self._combine_cfg_per_request(
v_t,
cfg_text_v_t,
cfg_img_v_t,
cfg_vae_lengths,
cfg_text_scales,
cfg_img_scales,
cfg_renorm_type,
cfg_renorm_min,
)
return v_t
@@ -8,11 +8,11 @@ from __future__ import annotations
import json
import os
from collections.abc import Iterable
from copy import deepcopy
from collections.abc import Iterable, Sequence
from copy import copy, deepcopy
from dataclasses import dataclass
from math import isqrt
from typing import ClassVar
from typing import TYPE_CHECKING, Any, ClassVar
import numpy as np
import torch
@@ -25,10 +25,15 @@ from vllm.model_executor.models.utils import AutoWeightsLoader
from vllm.transformers_utils.configs.bagel import BagelConfig
from vllm_omni.diffusion.data import DiffusionOutput, OmniDiffusionConfig
from vllm_omni.diffusion.distributed.parallel_state import (
get_cfg_group,
get_classifier_free_guidance_world_size,
)
from vllm_omni.diffusion.distributed.utils import get_local_device
from vllm_omni.diffusion.model_loader.diffusers_loader import DiffusersPipelineLoader
from vllm_omni.diffusion.models.interface import SupportsComponentDiscovery
from vllm_omni.diffusion.profiler.diffusion_pipeline_profiler import DiffusionPipelineProfilerMixin
from vllm_omni.diffusion.request import OmniDiffusionRequest
from vllm_omni.diffusion.worker.request_batch import DiffusionRequestBatch
from vllm_omni.model_executor.model_loader.weight_utils import download_weights_from_hf_specific
@@ -37,6 +42,11 @@ from .bagel_transformer import Bagel, NaiveCache, Qwen2MoTConfig, Qwen2MoTForCau
logger = init_logger(__name__)
if TYPE_CHECKING:
from vllm_omni.diffusion.worker.input_batch import InputBatch
from vllm_omni.diffusion.worker.utils import StepRequestState
from vllm_omni.inputs.data import OmniDiffusionSamplingParams, OmniPromptType
@dataclass
class BagelGenParams:
@@ -95,6 +105,144 @@ def get_bagel_post_process_func(od_config: OmniDiffusionConfig):
return post_process_func
def _resolve_bagel_image_geometry(od_config: OmniDiffusionConfig) -> tuple[int, int]:
"""Return the effective ``(latent_downsample, max_latent_size)``.
Some BAGEL checkpoints advertise a stale ``max_latent_size`` in
``config.json``. Model loading already corrects it from the positional
embedding weight, so admission preprocessing must do the same or it can
compute a different img2img shape from the Worker.
"""
model = getattr(od_config, "model", None)
if not model:
raise ValueError("BAGEL img2img preprocessing requires od_config.model.")
if os.path.exists(model):
model_path = model
else:
model_path = download_weights_from_hf_specific(
model,
None,
["*"],
revision=getattr(od_config, "revision", None),
)
config_path = os.path.join(model_path, "config.json")
with open(config_path, encoding="utf-8") as f:
bagel_cfg = json.load(f)
vae_config = bagel_cfg.get("vae_config") or {}
latent_downsample = int(vae_config.get("downsample", 8)) * int(bagel_cfg.get("latent_patch_size", 2))
max_latent_size = int(bagel_cfg.get("max_latent_size", 32))
index_path = os.path.join(model_path, "model.safetensors.index.json")
if os.path.exists(index_path):
with open(index_path, encoding="utf-8") as f:
weight_map = json.load(f).get("weight_map") or {}
position_key = next(
(key for key in ("latent_pos_embed.pos_embed", "bagel.latent_pos_embed.pos_embed") if key in weight_map),
None,
)
if position_key is not None:
from safetensors import safe_open
shard_path = os.path.join(model_path, weight_map[position_key])
with safe_open(shard_path, framework="pt") as f:
num_positions = int(f.get_slice(position_key).get_shape()[0])
inferred_size = isqrt(num_positions)
if inferred_size * inferred_size != num_positions:
raise ValueError(f"BAGEL latent position embedding length must be a square, got {num_positions}.")
max_latent_size = inferred_size
return latent_downsample, max_latent_size
def _bagel_effective_image_size(
image_size: tuple[int, int],
*,
latent_downsample: int,
max_latent_size: int,
) -> tuple[int, int]:
"""Match BAGEL's img2img resize and return ``(height, width)``."""
width, height = image_size
if width <= 0 or height <= 0:
raise ValueError(f"BAGEL img2img input must have positive dimensions, got {width}x{height}.")
max_image_size = int(max_latent_size * latent_downsample)
scale = min(max_image_size / max(width, height), 1.0)
min_image_size = min(256, max_image_size)
scale = max(scale, min_image_size / min(width, height))
resized_width = max(
latent_downsample,
int(round(width * scale / latent_downsample) * latent_downsample),
)
resized_height = max(
latent_downsample,
int(round(height * scale / latent_downsample) * latent_downsample),
)
return min(resized_height, max_image_size), min(resized_width, max_image_size)
def get_bagel_pre_process_func(od_config: OmniDiffusionConfig):
"""Resolve BAGEL execution mode and step-batch compatibility."""
step_execution = bool(getattr(od_config, "step_execution", False))
image_geometry: tuple[int, int] | None = None
def pre_process_func(request: OmniDiffusionRequest):
nonlocal image_geometry
sampling = request.sampling_params
kv_metadata = getattr(sampling, "kv_metadata", None) or {}
image_shape = kv_metadata.get("image_shape")
if image_shape is not None:
sampling.height, sampling.width = (int(value) for value in image_shape)
elif isinstance(request.prompt, dict):
modalities = request.prompt.get("modalities") or []
multi_modal_data = request.prompt.get("multi_modal_data") or {}
image_input = multi_modal_data.get("img2img")
if image_input is None and "text" not in modalities:
image_input = multi_modal_data.get("image")
if isinstance(image_input, list):
image_input = image_input[0] if image_input else None
if image_input is not None:
if isinstance(image_input, str):
with Image.open(image_input) as image:
input_size = image.size
else:
input_size = image_input.size
if image_geometry is None:
image_geometry = _resolve_bagel_image_geometry(od_config)
latent_downsample, max_latent_size = image_geometry
sampling.height, sampling.width = _bagel_effective_image_size(
input_size,
latent_downsample=latent_downsample,
max_latent_size=max_latent_size,
)
extra_args = request.sampling_params.extra_args or {}
cfg_interval = tuple(extra_args.get("cfg_interval", (0.4, 1.0)))
request.batch_compatibility_key = (
"bagel_cfg",
float(extra_args.get("cfg_text_scale", 4.0)),
float(extra_args.get("cfg_img_scale", 1.5)),
cfg_interval,
extra_args.get("cfg_renorm_type", "global"),
float(extra_args.get("cfg_renorm_min", 0.0)),
)
# Preserve legacy plain-string behavior: only an explicit modality
# request selects Bagel.generate_text() instead of stepwise denoising.
if step_execution and isinstance(request.prompt, dict):
modalities = request.prompt.get("modalities") or []
if "text" in modalities:
request.use_step_execution = False
return request
return pre_process_func
@dataclass
class _VaeCfg:
z_channels: int = 16
@@ -168,6 +316,7 @@ class BagelPipeline(nn.Module, SupportsComponentDiscovery, DiffusionPipelineProf
"bagel.connector",
"bagel.vit_pos_embed",
]
supports_step_execution: ClassVar[bool] = True
def __init__(self, *, od_config: OmniDiffusionConfig, prefix: str = ""):
super().__init__()
@@ -347,17 +496,26 @@ class BagelPipeline(nn.Module, SupportsComponentDiscovery, DiffusionPipelineProf
"""This model only supports a single prompt, not a batched request.""",
"""Taking only the first image for now.""",
)
return self._forward_single(req.prompts[0], req.sampling_params)
@torch.inference_mode()
def _forward_single(
self,
first_prompt: OmniPromptType,
sampling: OmniDiffusionSamplingParams,
*,
prepare_only: bool = False,
) -> DiffusionOutput | dict[str, Any]:
# TODO: In online mode, sometimes it receives [{"prompts": None}, {...}], so cannot use .get("...", "")
# TODO: May be some data formatting operations on the API side. Hack for now.
first_prompt = req.prompts[0]
prompt = first_prompt if isinstance(req.prompts[0], str) else (req.prompts[0].get("prompt") or "")
prompt = first_prompt if isinstance(first_prompt, str) else (first_prompt.get("prompt") or "")
max_hw = int(self.bagel.max_latent_size * self.bagel.latent_downsample)
if req.sampling_params.height is None and req.sampling_params.width is None:
if sampling.height is None and sampling.width is None:
height = width = max_hw
else:
height = int(req.sampling_params.height) if req.sampling_params.height is not None else max_hw
width = int(req.sampling_params.width) if req.sampling_params.width is not None else max_hw
height = int(sampling.height) if sampling.height is not None else max_hw
width = int(sampling.width) if sampling.width is not None else max_hw
if height > max_hw or width > max_hw:
raise ValueError(
f"Requested resolution {height}x{width} exceeds Bagel checkpoint limit "
@@ -366,7 +524,7 @@ class BagelPipeline(nn.Module, SupportsComponentDiscovery, DiffusionPipelineProf
)
image_shape = (height, width)
extra_args = getattr(req.sampling_params, "extra_args", {}) or {}
extra_args = getattr(sampling, "extra_args", {}) or {}
cfg_text_scale = extra_args.get("cfg_text_scale", 4.0)
cfg_img_scale = extra_args.get("cfg_img_scale", 1.5)
@@ -375,7 +533,7 @@ class BagelPipeline(nn.Module, SupportsComponentDiscovery, DiffusionPipelineProf
cfg_renorm_min = extra_args.get("cfg_renorm_min", 0.0)
gen_params = BagelGenParams(
num_timesteps=int(req.sampling_params.num_inference_steps or 50),
num_timesteps=int(sampling.num_inference_steps or 50),
timestep_shift=float(extra_args.get("timestep_shift", 3.0)),
cfg_text_scale=cfg_text_scale,
cfg_img_scale=cfg_img_scale,
@@ -392,34 +550,30 @@ class BagelPipeline(nn.Module, SupportsComponentDiscovery, DiffusionPipelineProf
cfg_text_context = deepcopy(gen_context)
cfg_img_context = deepcopy(gen_context)
injected_kv = req.sampling_params.past_key_values
injected_kv = sampling.past_key_values
if injected_kv is not None:
logger.info("Using injected KV Cache (direct)")
injected_kv = NaiveCache.from_object(injected_kv)
gen_context["past_key_values"] = injected_kv
seq_len = injected_kv.key_cache[0].shape[0]
gen_context["kv_lens"] = [seq_len]
if req.sampling_params.kv_metadata and "ropes" in req.sampling_params.kv_metadata:
gen_context["ropes"] = req.sampling_params.kv_metadata["ropes"]
if sampling.kv_metadata and "ropes" in sampling.kv_metadata:
gen_context["ropes"] = sampling.kv_metadata["ropes"]
else:
gen_context["ropes"] = [seq_len]
if req.sampling_params.kv_metadata and "image_shape" in req.sampling_params.kv_metadata:
image_shape = tuple(req.sampling_params.kv_metadata["image_shape"])
if sampling.kv_metadata and "image_shape" in sampling.kv_metadata:
image_shape = tuple(sampling.kv_metadata["image_shape"])
branch_kvs = getattr(req.sampling_params, "cfg_branch_past_key_values", None) or {}
branch_metadata = getattr(req.sampling_params, "cfg_branch_kv_metadata", None) or {}
active_branch = getattr(req.sampling_params, "cfg_active_branch", None)
branch_roles = getattr(req.sampling_params, "cfg_branch_roles", None) or list(branch_kvs.keys())
branch_kvs = getattr(sampling, "cfg_branch_past_key_values", None) or {}
branch_metadata = getattr(sampling, "cfg_branch_kv_metadata", None) or {}
active_branch = getattr(sampling, "cfg_active_branch", None)
branch_roles = getattr(sampling, "cfg_branch_roles", None) or list(branch_kvs.keys())
cfg_text_kv = getattr(req.sampling_params, "cfg_text_past_key_values", None) or branch_kvs.get("cfg_text")
cfg_text_metadata = getattr(req.sampling_params, "cfg_text_kv_metadata", None) or branch_metadata.get(
"cfg_text"
)
cfg_img_kv = getattr(req.sampling_params, "cfg_img_past_key_values", None) or branch_kvs.get("cfg_img")
cfg_img_metadata = getattr(req.sampling_params, "cfg_img_kv_metadata", None) or branch_metadata.get(
"cfg_img"
)
cfg_text_kv = getattr(sampling, "cfg_text_past_key_values", None) or branch_kvs.get("cfg_text")
cfg_text_metadata = getattr(sampling, "cfg_text_kv_metadata", None) or branch_metadata.get("cfg_text")
cfg_img_kv = getattr(sampling, "cfg_img_past_key_values", None) or branch_kvs.get("cfg_img")
cfg_img_metadata = getattr(sampling, "cfg_img_kv_metadata", None) or branch_metadata.get("cfg_img")
cfg_parallel_contract = (
active_branch is not None or bool(branch_roles) or cfg_text_kv is not None or cfg_img_kv is not None
@@ -455,8 +609,8 @@ class BagelPipeline(nn.Module, SupportsComponentDiscovery, DiffusionPipelineProf
cfg_img_seq_len = injected_kv.key_cache[0].shape[0]
cfg_img_context["past_key_values"] = injected_kv
cfg_img_context["kv_lens"] = [cfg_img_seq_len]
if req.sampling_params.kv_metadata and "ropes" in req.sampling_params.kv_metadata:
cfg_img_context["ropes"] = req.sampling_params.kv_metadata["ropes"]
if sampling.kv_metadata and "ropes" in sampling.kv_metadata:
cfg_img_context["ropes"] = sampling.kv_metadata["ropes"]
else:
cfg_img_context["ropes"] = [cfg_img_seq_len]
else:
@@ -748,10 +902,10 @@ class BagelPipeline(nn.Module, SupportsComponentDiscovery, DiffusionPipelineProf
)
# ---- Image generation (text2img / img2img) ----
if req.sampling_params.seed is not None:
torch.manual_seed(req.sampling_params.seed)
if sampling.seed is not None:
torch.manual_seed(sampling.seed)
if self.device.type == "cuda":
torch.cuda.manual_seed(req.sampling_params.seed)
torch.cuda.manual_seed(sampling.seed)
generation_input = self.bagel.prepare_vae_latent(
curr_kvlens=gen_context["kv_lens"],
@@ -787,7 +941,7 @@ class BagelPipeline(nn.Module, SupportsComponentDiscovery, DiffusionPipelineProf
# NOTE: For now we disable device specific noise regeneration so that e2e tests can run
# on both CUDA and ROCm. Context: https://github.com/vllm-project/vllm-omni/pull/4081
# self._regen_init_noise_on_device(generation_input, req.sampling_params.seed)
# self._regen_init_noise_on_device(generation_input, sampling.seed)
# text cfg
generation_input_cfg_text = self.bagel.prepare_vae_latent_cfg(
@@ -808,6 +962,19 @@ class BagelPipeline(nn.Module, SupportsComponentDiscovery, DiffusionPipelineProf
if torch.is_tensor(v):
generation_input_cfg_img[k] = v.to(self.device)
if prepare_only:
return {
"generation_input": generation_input,
"cfg_text_packed_position_ids": generation_input_cfg_text["cfg_packed_position_ids"],
"cfg_img_packed_position_ids": generation_input_cfg_img["cfg_packed_position_ids"],
"gen_context": gen_context,
"cfg_text_context": cfg_text_context,
"cfg_img_context": cfg_img_context,
"gen_params": gen_params,
"image_shape": image_shape,
"think_text": think_text,
}
with torch.autocast(
device_type=self.device.type,
enabled=self.device.type != "cpu",
@@ -827,12 +994,33 @@ class BagelPipeline(nn.Module, SupportsComponentDiscovery, DiffusionPipelineProf
**generation_input,
cfg_text_packed_position_ids=generation_input_cfg_text["cfg_packed_position_ids"],
cfg_img_packed_position_ids=generation_input_cfg_img["cfg_packed_position_ids"],
return_trajectory_latents=req.sampling_params.return_trajectory_latents,
return_trajectory_latents=sampling.return_trajectory_latents,
scheduler=self.scheduler,
scheduler_kwargs=self.scheduler_kwargs,
)
img = self._decode_image_from_latent(self.bagel, self.vae, latents[0], image_shape)
return self._build_image_output(
latents[0],
image_shape,
trajectory_latents=trajectory_latents,
trajectory_timesteps=trajectory_timesteps,
trajectory_log_probs=trajectory_log_probs,
return_trajectory_decoded=sampling.return_trajectory_decoded,
think_text=think_text,
)
def _build_image_output(
self,
latent: torch.Tensor,
image_shape: tuple[int, int],
*,
trajectory_latents: list[torch.Tensor] | None = None,
trajectory_timesteps: list[torch.Tensor] | None = None,
trajectory_log_probs: list[torch.Tensor] | None = None,
return_trajectory_decoded: bool = False,
think_text: str | None = None,
) -> DiffusionOutput:
img = self._decode_image_from_latent(self.bagel, self.vae, latent, image_shape)
# Build trajectory output when requested
trajectory_latents_stacked: torch.Tensor | None = None
@@ -841,7 +1029,7 @@ class BagelPipeline(nn.Module, SupportsComponentDiscovery, DiffusionPipelineProf
if trajectory_latents:
trajectory_latents_stacked = torch.stack(trajectory_latents)
trajectory_timesteps_stacked = torch.stack(trajectory_timesteps)
if req.sampling_params.return_trajectory_decoded:
if return_trajectory_decoded:
trajectory_decoded = [
self._decode_image_from_latent(self.bagel, self.vae, lat, image_shape) for lat in trajectory_latents
]
@@ -875,6 +1063,317 @@ class BagelPipeline(nn.Module, SupportsComponentDiscovery, DiffusionPipelineProf
stage_durations=self.stage_durations if hasattr(self, "stage_durations") else None,
)
def prepare_encode(
self,
state: StepRequestState,
**kwargs: object,
) -> StepRequestState:
"""Populate *state* with BAGEL inputs, latents, timesteps, and CFG config."""
del kwargs
if self.bagel._sp_size > 1:
raise NotImplementedError("BAGEL step execution does not currently support sequence parallelism.")
sampling = state.sampling
prompt = state.prompt if state.prompt is not None else ""
if isinstance(prompt, dict):
if "text" in (prompt.get("modalities") or []):
raise NotImplementedError("BAGEL text output is not supported by step execution.")
ctx = self._forward_single(prompt, sampling, prepare_only=True)
if not isinstance(ctx, dict):
raise RuntimeError("BAGEL step preparation did not produce an image-generation context.")
generation_input = ctx["generation_input"]
latents = generation_input["packed_init_noises"]
gen_params = ctx["gen_params"]
timesteps, dts = self.bagel.prepare_denoise_schedule(
latents,
gen_params.num_timesteps,
gen_params.timestep_shift,
)
if len(timesteps) == 0:
raise ValueError("BAGEL step execution requires num_inference_steps >= 1.")
# Keep request-local scheduler progress while sharing immutable config
# and tensor buffers with the pipeline scheduler.
req_scheduler = copy(self.scheduler)
# Populate state from generation context.
state.latents = latents
state.timesteps = timesteps
state.step_index = 0
state.scheduler = req_scheduler
state.do_true_cfg = gen_params.cfg_text_scale > 1.0
state.img_shapes = [ctx["image_shape"]]
state.extra.update(
{
"bagel_generation_input": {
key: value for key, value in generation_input.items() if key != "packed_init_noises"
},
"bagel_cfg_text_packed_position_ids": ctx["cfg_text_packed_position_ids"],
"bagel_cfg_img_packed_position_ids": ctx["cfg_img_packed_position_ids"],
"bagel_gen_context": ctx["gen_context"],
"bagel_cfg_text_context": ctx["cfg_text_context"],
"bagel_cfg_img_context": ctx["cfg_img_context"],
"bagel_gen_params": gen_params,
"bagel_dts": dts,
"bagel_image_shape": ctx["image_shape"],
"bagel_scheduler_kwargs": dict(self.scheduler_kwargs),
"bagel_think_text": ctx["think_text"],
"bagel_trajectory_latents": (
# Trajectory output needs immutable snapshots because each
# later scheduler update replaces or mutates the latents.
[latents.clone()] if sampling.return_trajectory_latents and len(timesteps) > 0 else []
),
"bagel_trajectory_timesteps": [],
"bagel_trajectory_log_probs": [],
}
)
return state
@staticmethod
def _pack_step_generation_inputs(
states: Sequence[StepRequestState],
) -> dict[str, torch.Tensor]:
generation_inputs = [state.extra["bagel_generation_input"] for state in states]
packed_seqlens = [generation_input["packed_seqlens"] for generation_input in generation_inputs]
if any(seqlens.numel() != 1 for seqlens in packed_seqlens):
raise ValueError("BAGEL step execution expects one packed sequence per request.")
seq_lengths = [int(seqlens.item()) for seqlens in packed_seqlens]
if len(set(seq_lengths)) != 1:
raise ValueError("BAGEL step batching requires matching packed sequence lengths.")
query_offset = 0
text_indexes = []
vae_indexes = []
for generation_input, seq_len in zip(generation_inputs, seq_lengths, strict=True):
text_indexes.append(generation_input["packed_text_indexes"] + query_offset)
vae_indexes.append(generation_input["packed_vae_token_indexes"] + query_offset)
query_offset += seq_len
return {
"packed_text_ids": torch.cat([item["packed_text_ids"] for item in generation_inputs]),
"packed_text_indexes": torch.cat(text_indexes),
"packed_vae_position_ids": torch.cat([item["packed_vae_position_ids"] for item in generation_inputs]),
"packed_vae_token_indexes": torch.cat(vae_indexes),
"packed_seqlens": torch.cat(packed_seqlens),
"packed_position_ids": torch.cat([item["packed_position_ids"] for item in generation_inputs]),
}
def _build_denoise_kwargs(
self,
input_batch: InputBatch,
states: Sequence[StepRequestState],
) -> dict[str, Any]:
parallel_config = getattr(self.bagel, "parallel_config", None)
cfg_parallel_size = getattr(parallel_config, "cfg_parallel_size", 1)
packed = self._pack_step_generation_inputs(states)
vae_lengths = [int(state.latents.shape[0]) for state in states]
if sum(vae_lengths) != int(input_batch.latents.shape[0]):
raise ValueError("BAGEL packed latent rows do not match InputBatch.latents.")
cfg_settings = {
(
state.extra["bagel_gen_params"].cfg_text_scale,
state.extra["bagel_gen_params"].cfg_img_scale,
tuple(state.extra["bagel_gen_params"].cfg_interval),
state.extra["bagel_gen_params"].cfg_renorm_type,
state.extra["bagel_gen_params"].cfg_renorm_min,
)
for state in states
}
if len(cfg_settings) != 1:
raise ValueError("Mixed BAGEL CFG settings cannot share one step batch.")
cfg_text_scale, cfg_img_scale, _cfg_interval, cfg_renorm_type, cfg_renorm_min = next(iter(cfg_settings))
cfg_text_scales = []
cfg_img_scales = []
for state in states:
gen_params = state.extra["bagel_gen_params"]
timestep = state.current_timestep
if timestep is None:
raise ValueError(f"BAGEL request {state.request_id} has no current timestep.")
t_value = float(timestep.item())
in_cfg_window = t_value > gen_params.cfg_interval[0] and t_value <= gen_params.cfg_interval[1]
text_scale = gen_params.cfg_text_scale if in_cfg_window else 1.0
cfg_text_scales.append(text_scale)
cfg_img_scales.append(gen_params.cfg_img_scale if in_cfg_window and text_scale > 1.0 else 1.0)
use_cfg_text = any(scale > 1.0 for scale in cfg_text_scales)
use_cfg_img = use_cfg_text and any(scale > 1.0 for scale in cfg_img_scales)
configured_cfg_text = any(state.extra["bagel_gen_params"].cfg_text_scale > 1.0 for state in states)
configured_cfg_img = any(
state.extra["bagel_gen_params"].cfg_text_scale > 1.0 and state.extra["bagel_gen_params"].cfg_img_scale > 1.0
for state in states
)
build_cfg_text = use_cfg_text or (cfg_parallel_size > 1 and configured_cfg_text)
build_cfg_img = use_cfg_img or (cfg_parallel_size > 1 and configured_cfg_img)
gen_cache = NaiveCache.merge([state.extra["bagel_gen_context"]["past_key_values"] for state in states])
cfg_branch_pids = None
cfg_branch_caches = None
if build_cfg_text:
cfg_branch_pids = [
packed["packed_position_ids"],
torch.cat([state.extra["bagel_cfg_text_packed_position_ids"] for state in states]),
]
cfg_branch_caches = [
gen_cache,
NaiveCache.merge([state.extra["bagel_cfg_text_context"]["past_key_values"] for state in states]),
]
if build_cfg_img:
cfg_branch_pids.append(
torch.cat([state.extra["bagel_cfg_img_packed_position_ids"] for state in states])
)
cfg_branch_caches.append(
NaiveCache.merge([state.extra["bagel_cfg_img_context"]["past_key_values"] for state in states])
)
return {
"x_t": input_batch.latents,
"timestep": input_batch.timesteps,
"past_key_values": gen_cache,
"cfg_renorm_min": cfg_renorm_min,
"cfg_renorm_type": cfg_renorm_type,
"cfg_text_scale": cfg_text_scale,
"cfg_img_scale": cfg_img_scale,
"cfg_branch_pids": cfg_branch_pids,
"cfg_branch_caches": cfg_branch_caches,
"cfg_vae_lengths": vae_lengths if use_cfg_text else None,
"cfg_text_scales": cfg_text_scales if use_cfg_text else None,
"cfg_img_scales": cfg_img_scales if use_cfg_text else None,
**packed,
}
def _denoise_step_cfg_parallel(self, denoise_kwargs: dict[str, Any]) -> torch.Tensor:
cfg_branch_pids = denoise_kwargs["cfg_branch_pids"]
cfg_branch_caches = denoise_kwargs["cfg_branch_caches"]
if cfg_branch_pids is None or cfg_branch_caches is None:
return self.bagel.forward(**denoise_kwargs)
cfg_world_size = get_classifier_free_guidance_world_size()
num_branches = len(cfg_branch_pids)
if cfg_world_size == 2 and num_branches == 3:
raise ValueError(
f"Image CFG (cfg_img_scale={denoise_kwargs['cfg_img_scale']}) requires cfg_parallel_size=3, "
"but got cfg_parallel_size=2. Use cfg_parallel_size=3 to enable image CFG in parallel mode."
)
common_keys = (
"x_t",
"timestep",
"packed_vae_token_indexes",
"packed_vae_position_ids",
"packed_text_ids",
"packed_text_indexes",
"packed_seqlens",
)
common = {key: denoise_kwargs[key] for key in common_keys}
branches_kwargs = [
{**common, "packed_position_ids": position_ids, "past_key_values": cache}
for position_ids, cache in zip(cfg_branch_pids, cfg_branch_caches, strict=True)
]
result = self.bagel.predict_noise_with_multi_branch_cfg(
do_true_cfg=denoise_kwargs["cfg_text_scales"] is not None,
true_cfg_scale={
"cfg_text_scale": denoise_kwargs["cfg_text_scale"],
"cfg_img_scale": denoise_kwargs["cfg_img_scale"],
"cfg_renorm_type": denoise_kwargs["cfg_renorm_type"],
"cfg_renorm_min": denoise_kwargs["cfg_renorm_min"],
"cfg_vae_lengths": denoise_kwargs["cfg_vae_lengths"],
"cfg_text_scales": denoise_kwargs["cfg_text_scales"],
"cfg_img_scales": denoise_kwargs["cfg_img_scales"],
},
branches_kwargs=branches_kwargs,
)
if not isinstance(result, torch.Tensor):
raise TypeError("BAGEL CFG parallel step must return one velocity tensor.")
return result
def denoise_step(
self,
input_batch: InputBatch,
*,
states: Sequence[StepRequestState] | None = None,
**kwargs: object,
) -> torch.Tensor:
"""One denoise step: pack request-local BAGEL state and run the model."""
del kwargs
states = tuple(states or input_batch.states)
if not states:
raise ValueError("BAGEL denoise_step requires at least one request state.")
denoise_kwargs = self._build_denoise_kwargs(input_batch, states)
parallel_config = getattr(self.bagel, "parallel_config", None)
use_cfg_parallel = (
getattr(parallel_config, "cfg_parallel_size", 1) > 1 and denoise_kwargs["cfg_branch_pids"] is not None
)
with torch.autocast(
device_type=self.device.type,
enabled=self.device.type != "cpu",
dtype=self.od_config.dtype,
):
if use_cfg_parallel:
if any(state.step_index == 0 for state in states):
get_cfg_group().broadcast(input_batch.latents, src=0)
return self._denoise_step_cfg_parallel(denoise_kwargs)
return self.bagel.forward(**denoise_kwargs)
def step_scheduler(
self,
state: StepRequestState,
noise_pred: torch.Tensor,
**kwargs: object,
) -> None:
"""One scheduler step: update ``state.latents`` and advance ``step_index``."""
del kwargs
t = state.current_timestep
if t is None or state.latents is None:
raise ValueError(f"BAGEL request {state.request_id} is not ready for a scheduler step.")
dt = state.extra["bagel_dts"][state.step_index]
log_prob = None
if state.scheduler is not None:
output = state.scheduler.step(
noise_pred.to(state.latents.device),
t,
state.latents,
dt,
**state.extra["bagel_scheduler_kwargs"],
)
state.latents = output.prev_sample
log_prob = getattr(output, "log_prob", None)
else:
state.latents = state.latents - noise_pred.to(state.latents.device) * dt
if state.sampling.return_trajectory_latents:
# Preserve this opt-in step snapshot before the next update.
state.extra["bagel_trajectory_latents"].append(state.latents.clone())
state.extra["bagel_trajectory_timesteps"].append(t)
if log_prob is not None:
state.extra["bagel_trajectory_log_probs"].append(log_prob)
state.step_index += 1
def post_decode(
self,
state: StepRequestState,
**kwargs: object,
) -> DiffusionOutput:
"""Decode final latents from *state*."""
del kwargs
if state.latents is None:
raise ValueError(f"BAGEL request {state.request_id} has no latents to decode.")
image_shape = state.extra["bagel_image_shape"]
return_trajectory_decoded = state.sampling.return_trajectory_decoded
return self._build_image_output(
state.latents,
image_shape,
trajectory_latents=state.extra["bagel_trajectory_latents"],
trajectory_timesteps=state.extra["bagel_trajectory_timesteps"],
trajectory_log_probs=state.extra["bagel_trajectory_log_probs"],
return_trajectory_decoded=return_trajectory_decoded,
think_text=state.extra["bagel_think_text"],
)
def load_weights(self, weights: Iterable[tuple[str, torch.Tensor]]) -> set[str]:
state = self.state_dict()
allowed = set(state.keys())
@@ -278,9 +278,9 @@ class LanceBagel(Bagel):
``forward_cache_update_vae`` — is reused unchanged.
"""
# Upstream Lance samples ``num_timesteps + 1`` schedule points (one extra
# over official BAGEL) so the denoise loop runs exactly ``num_timesteps``
# Euler steps. See Bagel.generate_image and issue #4470.
# Upstream Lance uses the same ``num_timesteps + 1`` schedule-point
# convention as BAGEL, so the denoise loop runs exactly ``num_timesteps``
# Euler steps.
_denoise_schedule_extra_step: bool = True
@staticmethod