Skip to content

Let WanVACEModel return and reuse VACE hints. - #526

Merged
copybara-service[bot] merged 1 commit into
mainfrom
ninatu/wan_vace_cache
Nov 23, 2026
Merged

copybara-service[bot] merged 1 commit into
mainfrom
ninatu/wan_vace_cache

Conversation

@ninatu

@ninatu ninatu commented Nov 20, 2026 •

Copy link
Copy Markdown
Collaborator

The VACE blocks compute a "hint" per VACE layer that is added to the main
transformer blocks. The hints depend on the noisy latents and the timestep, so
they are recomputed at every denoising step.

This change makes it possible to compute the hints once and reuse them for
other steps, which skips the VACE blocks and can speed up inference:

  • WanVACEModel.__call__ gets two optional arguments: return_vace_hints=True
    returns the unscaled hints, one per VACE layer, instead of the prediction,
    and vace_hints passes precomputed hints back; the VACE blocks are skipped
    and control_hidden_states_scale is still applied.
  • transformer_forward_pass forwards both arguments.
  • VaceWanPipeline2_1.__call__ and run_inference get use_vace_cache,
    which computes the hints once, from zero latents at the first timestep, and
    reuses them at every step.
  • With the new use_vace_cache config (False in base_wan_14b.yml and
    base_wan_1_3b.yml), the training and eval steps of the VACE trainer
    compute the hints the same way, from zero latents at timestep 999, the first
    inference timestep. Computing them from zero latents instead of the noisy
    latents also prevents the model from reading the flow-matching target off
    the hints.

Reusing hints across steps only approximates the full model, so the model needs
fine-tuning with hints computed the same way to keep the quality. The defaults
keep the current behavior.

@ninatu
ninatu requested a review from entrpn as a code owner November 20, 2026 12:45
@github-actions

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces VACE caching to the Wan VACE transformer model, allowing precomputed VACE hints to be passed in or returned to skip VACE blocks and speed up inference. However, the review feedback points out that the pipeline integration is incomplete. Specifically, the transformer_forward_pass function, the denoising loop run_inference, and the pipeline's __call__ method do not yet support or propagate the return_vace_hints parameter, making the caching feature unusable during pipeline inference. The reviewer recommends updating these pipeline components to fully expose and implement the caching logic.

@ninatu
ninatu force-pushed the ninatu/wan_vace_cache branch 2 times, most recently from 14a8a47 to 14dc2b4 Compare November 20, 2026 15:01
The VACE blocks compute a "hint" per VACE layer that is added to the main transformer blocks. The hints depend on the noisy latents and the timestep, so they are recomputed at every denoising step.

This change makes it possible to compute the hints once and reuse them for other steps, which skips the VACE blocks and can speed up inference:

WanVACEModel.__call__ gets two optional arguments: return_vace_hints=True returns the unscaled hints, one per VACE layer, instead of the prediction, and vace_hints passes precomputed hints back; the VACE blocks are skipped and control_hidden_states_scale is still applied.
transformer_forward_pass forwards both arguments.
VaceWanPipeline2_1.__call__ and run_inference get use_vace_cache, which computes the hints once, from zero latents at the first timestep, and reuses them at every step.
With the new use_vace_cache config (False in base_wan_14b.yml and base_wan_1_3b.yml), the training and eval steps of the VACE trainer compute the hints the same way, from zero latents at timestep 999, the first inference timestep. Computing them from zero latents instead of the noisy latents also prevents the model from reading the flow-matching target off the hints.
Reusing hints across steps only approximates the full model, so the model needs fine-tuning with hints computed the same way to keep the quality. The defaults keep the current behavior.
@ninatu
ninatu force-pushed the ninatu/wan_vace_cache branch from 14dc2b4 to b32e8ab Compare November 20, 2026 16:38

@entrpn entrpn left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM overall! The VACE hint caching design is clean: return_vace_hints is properly marked static on transformer_forward_pass (compatible with aot_cache), hints are returned unscaled while preserving CFG batch ordering before the CFG split, and precomputing hints from zero latents at t=999 avoids flow-matching target leakage during fine-tuning. Left two small, non-blocking suggestions below.

raise ValueError(f"Length of `vace_hints` {len(vace_hints)} should be equal to {len(self.config.vace_layers)}.")

hidden_states = jnp.transpose(hidden_states, (0, 2, 3, 4, 1))
control_hidden_states = jnp.transpose(control_hidden_states, (0, 2, 3, 4, 1))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional (eager compute / memory): When precomputed vace_hints are passed in, control_hidden_states is unused after the vace_patch_embedding block, so XLA dead-code-eliminates it under jit, but eager WanVACEModel(..., vace_hints=...) calls still run the 3D conv + padding, and callers with cached hints cannot pass control_hidden_states=None (run_inference also has to keep passing [2*B, 96, F, H, W] control_hidden_states on every step). Consider making control_hidden_states: Optional[jax.Array] = None (requiring it only when vace_hints is None) and moving the control_hidden_states transpose / vace_patch_embedding / padding block inside if vace_hints is None:.

scheduler = FlaxUniPCMultistepScheduler(
num_train_timesteps=1000, prediction_type="flow_prediction", use_flow_sigmas=True, flow_shift=3.0
)
scheduler_state = scheduler.set_timesteps(scheduler.create_state(), num_inference_steps=1, shape=latents.shape)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional (test coverage): With num_inference_steps=1, the denoising loop only executes at timesteps[0], which is the same timestep used to precompute vace_hints. Using num_inference_steps=2 here would also verify that step 1 reuses the timesteps[0] zero-latent hints rather than recomputing hints at timesteps[1]. A small unit test covering train_step / eval_step with use_vace_cache=True in wan_vace_trainer.py would also be great if convenient.

@copybara-service
copybara-service Bot merged commit d2536ff into main Nov 23, 2026
29 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants