Repository navigation
Let WanVACEModel return and reuse VACE hints. - #526
Conversation
There was a problem hiding this comment.
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.
14a8a47 to
14dc2b4
Compare
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.
14dc2b4 to
b32e8ab
Compare
entrpn
left a comment
There was a problem hiding this comment.
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)) |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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.
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=Truereturns the unscaled hints, one per VACE layer, instead of the prediction,
and
vace_hintspasses precomputed hints back; the VACE blocks are skippedand
control_hidden_states_scaleis still applied.transformer_forward_passforwards both arguments.VaceWanPipeline2_1.__call__andrun_inferencegetuse_vace_cache,which computes the hints once, from zero latents at the first timestep, and
reuses them at every step.
use_vace_cacheconfig (False inbase_wan_14b.ymlandbase_wan_1_3b.yml), the training and eval steps of the VACE trainercompute 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.