[Perf][DreamZero]dreamzero supports CUDA graph, torch.compile, and DiT Caching. (#4213)
Signed-off-by: amy-why-3459 <wuhaiyan17@huawei.com>
This commit is contained in:
1 parent
63ba11b8b1
commit
f05e03aea8
22 files changed
+1204
-177
No files matched your search
@@ -23,6 +23,10 @@ from huggingface_hub import hf_hub_download
|
||||
from transformers import AutoTokenizer, UMT5Config, UMT5EncoderModel
|
||||
from vllm.model_executor.model_loader.weight_utils import default_weight_loader
|
||||
|
||||
from vllm_omni.diffusion.cache.stepcache import (
|
||||
get_stepcache_state,
|
||||
is_stepcache_active,
|
||||
)
|
||||
from vllm_omni.diffusion.data import DiffusionOutput, OmniDiffusionConfig
|
||||
from vllm_omni.diffusion.distributed.autoencoders.autoencoder_kl_wan import (
|
||||
DistributedAutoencoderKLWan,
|
||||
@@ -55,10 +59,6 @@ logger = logging.getLogger(__name__)
|
||||
MAX_DREAMZERO_SESSIONS = 64
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class VideoActionScheduler:
|
||||
"""Wraps video + action schedulers into single .step() interface."""
|
||||
|
||||
@@ -205,6 +205,9 @@ class DreamZeroPipeline(nn.Module, CFGParallelMixin):
|
||||
self._max_session_states = MAX_DREAMZERO_SESSIONS
|
||||
self.state = self._get_or_create_state("default")
|
||||
|
||||
# DiT step cache is configured by StepCacheBackend
|
||||
# (cache_backend="step_cache") via pipeline._stepcache_config.
|
||||
|
||||
# Keep runtime inference settings separate from the training-time config.
|
||||
self.num_inference_steps: int = model_config.get(
|
||||
"num_inference_steps",
|
||||
@@ -306,6 +309,27 @@ class DreamZeroPipeline(nn.Module, CFGParallelMixin):
|
||||
|
||||
def predict_noise(self, **kwargs) -> tuple[torch.Tensor, torch.Tensor]:
|
||||
"""Call CausalWanModel, return (video_pred, action_pred)."""
|
||||
video_pred, action_pred = self._predict_noise_eager(kwargs)
|
||||
|
||||
if is_stepcache_active(self):
|
||||
video_pred = video_pred.clone()
|
||||
if action_pred is not None:
|
||||
action_pred = action_pred.clone()
|
||||
|
||||
if action_pred is None:
|
||||
batch_size = kwargs["hidden_states"].shape[0]
|
||||
action_pred = torch.empty(
|
||||
batch_size,
|
||||
0,
|
||||
self.transformer.action_dim,
|
||||
device=video_pred.device,
|
||||
dtype=video_pred.dtype,
|
||||
)
|
||||
return (video_pred, action_pred)
|
||||
|
||||
def _predict_noise_eager(self, kwargs: dict) -> tuple[torch.Tensor, torch.Tensor | None]:
|
||||
"""Eager DiT forward; also handles KV-cache write-back on prefill."""
|
||||
self._cudagraph_mark_step_begin()
|
||||
video_pred, action_pred, updated_kv_caches = self.transformer(
|
||||
x=kwargs["hidden_states"],
|
||||
timestep=kwargs["timestep_video"],
|
||||
@@ -327,19 +351,118 @@ class DreamZeroPipeline(nn.Module, CFGParallelMixin):
|
||||
for i, kv in enumerate(updated_kv_caches):
|
||||
state.update_kv_cache(i, kv, is_negative=is_neg)
|
||||
|
||||
video_pred = video_pred.clone()
|
||||
if action_pred is not None:
|
||||
action_pred = action_pred.clone()
|
||||
else:
|
||||
batch_size = kwargs["hidden_states"].shape[0]
|
||||
action_pred = torch.empty(
|
||||
batch_size,
|
||||
0,
|
||||
self.transformer.action_dim,
|
||||
device=video_pred.device,
|
||||
dtype=video_pred.dtype,
|
||||
) # CFG-parallel-safe dummy action pred
|
||||
return (video_pred, action_pred)
|
||||
return video_pred, action_pred
|
||||
|
||||
# -----------------------------------------------------------------------
|
||||
# torch.compile setup (paper D.2 extended: encoders + VAE + DiT blocks)
|
||||
# -----------------------------------------------------------------------
|
||||
|
||||
def setup_compile(self) -> None:
|
||||
"""Compile DreamZero encoders, VAE, and per-block DiT for inference.
|
||||
|
||||
Paper D.2 uses ``mode=reduce-overhead``, ``fullgraph=True``, ``dynamic=False``
|
||||
on text/image/VAE and DiT. VAE decode uses a tensor feat_cache patch and
|
||||
compiles ``decoder.forward`` (not ``_decode``, which has a Python frame loop).
|
||||
DiT blocks use per-block ``fullgraph=True``.
|
||||
"""
|
||||
if not torch.cuda.is_available():
|
||||
logger.info("DreamZero setup_compile skipped: CUDA not available.")
|
||||
return
|
||||
|
||||
from vllm_omni.diffusion.models.dreamzero.wan_vae_feat_cache_patch import (
|
||||
apply_wan_vae_feat_cache_tensor_patch,
|
||||
)
|
||||
|
||||
apply_wan_vae_feat_cache_tensor_patch()
|
||||
|
||||
compile_ro = {"mode": "reduce-overhead", "fullgraph": True, "dynamic": False}
|
||||
# DiT blocks: default avoids CUDAGraph overwrite on modulation tensors; encoders use reduce-overhead.
|
||||
dit_compile = {"mode": "default", "fullgraph": True, "dynamic": False}
|
||||
|
||||
logger.info(
|
||||
"DreamZero: torch.compile text/image/VAE encode + per-block DiT (encoders reduce-overhead, DiT default)."
|
||||
)
|
||||
|
||||
try:
|
||||
self.text_encoder.forward = torch.compile(self.text_encoder.forward, **compile_ro)
|
||||
except Exception as exc:
|
||||
logger.warning("DreamZero: text_encoder compile failed (%s); skipping.", exc)
|
||||
|
||||
try:
|
||||
self.image_encoder.model.visual.forward = torch.compile(
|
||||
self.image_encoder.model.visual.forward,
|
||||
**compile_ro,
|
||||
)
|
||||
except Exception as exc:
|
||||
logger.warning("DreamZero: image_encoder compile failed (%s); skipping.", exc)
|
||||
|
||||
try:
|
||||
self.vae.encode = torch.compile(self.vae.encode, **compile_ro)
|
||||
except Exception as exc:
|
||||
logger.warning("DreamZero: vae.encode compile failed (%s); skipping.", exc)
|
||||
|
||||
compiled_blocks = 0
|
||||
for block in self.transformer.blocks:
|
||||
try:
|
||||
block.forward = torch.compile(block.forward, **dit_compile)
|
||||
compiled_blocks += 1
|
||||
except Exception as exc:
|
||||
logger.warning(
|
||||
"DreamZero: transformer block %d compile failed (%s); leaving remaining eager.",
|
||||
compiled_blocks,
|
||||
exc,
|
||||
)
|
||||
break
|
||||
if compiled_blocks:
|
||||
logger.info(
|
||||
"DreamZero: compiled %d/%d transformer blocks.",
|
||||
compiled_blocks,
|
||||
len(self.transformer.blocks),
|
||||
)
|
||||
|
||||
self.warmup_compile()
|
||||
|
||||
def warmup_compile(self) -> None:
|
||||
"""Warm up compiled text/image/VAE paths before timed inference."""
|
||||
if not torch.cuda.is_available():
|
||||
return
|
||||
|
||||
device = next(self.text_encoder.parameters()).device
|
||||
with torch.inference_mode():
|
||||
try:
|
||||
text_tokens = torch.zeros(1, 16, dtype=torch.long, device=device)
|
||||
attention_mask = torch.ones_like(text_tokens)
|
||||
self._encode_text(text_tokens, attention_mask)
|
||||
except Exception as exc:
|
||||
logger.warning("DreamZero compile warmup (text_encoder) skipped: %s", exc)
|
||||
|
||||
try:
|
||||
image = torch.zeros(1, 1, 3, 180, 320, dtype=torch.bfloat16, device=device)
|
||||
self._encode_image(image, self.num_frames, 180, 320)
|
||||
except Exception as exc:
|
||||
logger.warning("DreamZero compile warmup (image_encoder) skipped: %s", exc)
|
||||
|
||||
try:
|
||||
latent_h, latent_w = 180 // 8, 320 // 8
|
||||
dummy_latent = torch.zeros(
|
||||
1,
|
||||
16,
|
||||
self.num_frame_per_block,
|
||||
latent_h * 2,
|
||||
latent_w * 2,
|
||||
dtype=self.vae.dtype,
|
||||
device=device,
|
||||
)
|
||||
mean = self.vae_latents_mean.to(device=device, dtype=self.vae.dtype)
|
||||
inv_std = self.vae_latents_inv_std.to(device=device, dtype=self.vae.dtype)
|
||||
dummy_latent_denorm = dummy_latent / inv_std + mean
|
||||
self._cudagraph_mark_step_begin()
|
||||
self.vae.decode(dummy_latent_denorm, return_dict=False)
|
||||
except Exception as exc:
|
||||
logger.warning("DreamZero compile warmup (vae decode) skipped: %s", exc)
|
||||
|
||||
torch.accelerator.synchronize(device)
|
||||
logger.info("DreamZero compile warmup finished (text / image / vae decode).")
|
||||
|
||||
def combine_cfg_noise(
|
||||
self,
|
||||
@@ -388,12 +511,20 @@ class DreamZeroPipeline(nn.Module, CFGParallelMixin):
|
||||
videos = videos.reshape(b, t, c, h, w).permute(0, 2, 1, 3, 4)
|
||||
return videos.to(dtype=torch.bfloat16)
|
||||
|
||||
@staticmethod
|
||||
def _cudagraph_mark_step_begin() -> None:
|
||||
try:
|
||||
torch.compiler.cudagraph_mark_step_begin()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# -----------------------------------------------------------------------
|
||||
# Text encoding
|
||||
# -----------------------------------------------------------------------
|
||||
|
||||
def _encode_text(self, text_tokens: torch.Tensor, attention_mask: torch.Tensor) -> torch.Tensor:
|
||||
"""Encode text prompt via UMT5."""
|
||||
self._cudagraph_mark_step_begin()
|
||||
seq_lens = attention_mask.gt(0).sum(dim=1).long()
|
||||
prompt_emb = self.text_encoder(
|
||||
text_tokens,
|
||||
@@ -422,6 +553,7 @@ class DreamZeroPipeline(nn.Module, CFGParallelMixin):
|
||||
batch_size = image.shape[0]
|
||||
|
||||
with torch.amp.autocast(dtype=torch.bfloat16, device_type=device.type):
|
||||
self._cudagraph_mark_step_begin()
|
||||
clip_context = self.image_encoder.encode_image(image)
|
||||
|
||||
msk = torch.ones(batch_size, num_frames, height // 8, width // 8, device=device)
|
||||
@@ -469,8 +601,23 @@ class DreamZeroPipeline(nn.Module, CFGParallelMixin):
|
||||
inv_std = self.vae_latents_inv_std.to(device=vae_device, dtype=vae_dtype)
|
||||
latents = latents / inv_std + mean
|
||||
with torch.no_grad():
|
||||
self._cudagraph_mark_step_begin()
|
||||
return self.vae.decode(latents, return_dict=False)[0]
|
||||
|
||||
def decode_accumulated_video_latents(self, session_id: str | None = None) -> torch.Tensor:
|
||||
"""Decode all AR-chunk latents accumulated for ``session_id``."""
|
||||
state = self._get_or_create_state(session_id)
|
||||
latents = state.get_concatenated_video_latents()
|
||||
if latents is None:
|
||||
session_key = str(session_id or "default")
|
||||
raise RuntimeError(f"No accumulated video latents for session {session_key!r}.")
|
||||
return self.decode_video_latents(latents)
|
||||
|
||||
def clear_accumulated_video_latents(self, session_id: str | None = None) -> None:
|
||||
"""Clear accumulated video latents for ``session_id`` without resetting KV state."""
|
||||
state = self._get_or_create_state(session_id)
|
||||
state.clear_video_latents()
|
||||
|
||||
# -----------------------------------------------------------------------
|
||||
# KV cache prefill
|
||||
# -----------------------------------------------------------------------
|
||||
@@ -635,6 +782,12 @@ class DreamZeroPipeline(nn.Module, CFGParallelMixin):
|
||||
|
||||
noisy_input = video_latents
|
||||
noisy_input_action = action_latents
|
||||
# ---- step_cache (StepCacheBackend / dreamzero.git) ----
|
||||
_cached_flow_pred: torch.Tensor | None = None
|
||||
_cached_flow_pred_action: torch.Tensor | None = None
|
||||
_prev_predictions: list[tuple[torch.Tensor]] = []
|
||||
_step_cache = get_stepcache_state(self) if is_stepcache_active(self) else None
|
||||
|
||||
for index in range(len(timesteps_video)):
|
||||
video_timestep = timesteps_video[index]
|
||||
action_timestep = timesteps_action[index]
|
||||
@@ -663,46 +816,58 @@ class DreamZeroPipeline(nn.Module, CFGParallelMixin):
|
||||
else:
|
||||
y = state.ys[:, :, -self.num_frame_per_block :]
|
||||
|
||||
positive_kwargs = dict(
|
||||
hidden_states=noisy_input.transpose(1, 2),
|
||||
timestep_video=timestep,
|
||||
encoder_hidden_states=prompt_embeds,
|
||||
kv_cache=state.get_kv_caches(False),
|
||||
crossattn_cache=state.get_crossattn_caches(False),
|
||||
y=y,
|
||||
clip_feature=state.clip_feas,
|
||||
action=noisy_input_action,
|
||||
timestep_action=timestep_action,
|
||||
is_negative=False,
|
||||
**common_kwargs,
|
||||
)
|
||||
|
||||
# Negative (uncond) kwargs
|
||||
if do_true_cfg and negative_prompt_embeds is not None:
|
||||
negative_kwargs = dict(
|
||||
run_dit = _step_cache is None or _step_cache.should_run_step(_prev_predictions)
|
||||
if run_dit:
|
||||
positive_kwargs = dict(
|
||||
hidden_states=noisy_input.transpose(1, 2),
|
||||
timestep_video=timestep,
|
||||
encoder_hidden_states=negative_prompt_embeds,
|
||||
kv_cache=state.get_kv_caches(True),
|
||||
crossattn_cache=state.get_crossattn_caches(True),
|
||||
encoder_hidden_states=prompt_embeds,
|
||||
kv_cache=state.get_kv_caches(False),
|
||||
crossattn_cache=state.get_crossattn_caches(False),
|
||||
y=y,
|
||||
clip_feature=state.clip_feas,
|
||||
action=noisy_input_action,
|
||||
timestep_action=timestep_action,
|
||||
is_negative=True,
|
||||
is_negative=False,
|
||||
**common_kwargs,
|
||||
)
|
||||
else:
|
||||
negative_kwargs = None
|
||||
|
||||
noise_pred = self.predict_noise_maybe_with_cfg(
|
||||
positive_kwargs=positive_kwargs,
|
||||
negative_kwargs=negative_kwargs,
|
||||
do_true_cfg=do_true_cfg,
|
||||
true_cfg_scale=self.cfg_scale,
|
||||
cfg_normalize=False,
|
||||
)
|
||||
flow_pred, flow_pred_action = noise_pred
|
||||
if do_true_cfg and negative_prompt_embeds is not None:
|
||||
negative_kwargs = dict(
|
||||
hidden_states=noisy_input.transpose(1, 2),
|
||||
timestep_video=timestep,
|
||||
encoder_hidden_states=negative_prompt_embeds,
|
||||
kv_cache=state.get_kv_caches(True),
|
||||
crossattn_cache=state.get_crossattn_caches(True),
|
||||
y=y,
|
||||
clip_feature=state.clip_feas,
|
||||
action=noisy_input_action,
|
||||
timestep_action=timestep_action,
|
||||
is_negative=True,
|
||||
**common_kwargs,
|
||||
)
|
||||
else:
|
||||
negative_kwargs = None
|
||||
|
||||
noise_pred = self.predict_noise_maybe_with_cfg(
|
||||
positive_kwargs=positive_kwargs,
|
||||
negative_kwargs=negative_kwargs,
|
||||
do_true_cfg=do_true_cfg,
|
||||
true_cfg_scale=self.cfg_scale,
|
||||
cfg_normalize=False,
|
||||
)
|
||||
flow_pred, flow_pred_action = noise_pred
|
||||
_cached_flow_pred = flow_pred
|
||||
_cached_flow_pred_action = flow_pred_action
|
||||
|
||||
_prev_predictions.append((flow_pred,))
|
||||
if _step_cache is not None:
|
||||
_step_cache.trim_history(_prev_predictions)
|
||||
else:
|
||||
# Reuse previous prediction (DiT step-skipping cache).
|
||||
assert _cached_flow_pred is not None, "DiT cache: no cached prediction available for step skip."
|
||||
flow_pred = _cached_flow_pred
|
||||
flow_pred_action = _cached_flow_pred_action
|
||||
|
||||
latents = (noisy_input, noisy_input_action)
|
||||
t = (video_timestep, action_timestep)
|
||||
@@ -811,9 +976,12 @@ class DreamZeroPipeline(nn.Module, CFGParallelMixin):
|
||||
# on the next inference request after websocket reset/session switch.
|
||||
if extra_args.get("reset", False):
|
||||
state.reset()
|
||||
# Auto-reset based on model state (before accumulation)
|
||||
if state.should_reset(text_tokens, 0, self.transformer.local_attn_size):
|
||||
state.reset()
|
||||
else:
|
||||
reset_reason = state.reset_reason(text_tokens, 0, self.transformer.local_attn_size)
|
||||
if reset_reason == "session":
|
||||
state.reset()
|
||||
elif reset_reason == "inference":
|
||||
state.reset_inference_state()
|
||||
state.language = text_tokens
|
||||
|
||||
# Frame accumulation: stitched single frame → multi-frame video
|
||||
@@ -959,6 +1127,8 @@ class DreamZeroPipeline(nn.Module, CFGParallelMixin):
|
||||
video_out = torch.cat([image, video_out], dim=1)
|
||||
state.current_start_frame += self.num_frame_per_block
|
||||
|
||||
state.append_video_latents(video_out)
|
||||
|
||||
# q99 denorm: [-1,1] → real values
|
||||
action_out = self._denormalize_action(action_out.float(), embodiment_name)
|
||||
|
||||
|
||||
Reference in new issue
Block a user