@@ -500,6 +500,7 @@ def __call__(
500500 negative_prompt_embeds : jax .Array | None = None ,
501501 vae_only : bool = False ,
502502 use_kv_cache : bool = False ,
503+ use_vace_cache : bool = False ,
503504 ):
504505 """Runs the VACE model for the given inputs.
505506
@@ -521,6 +522,9 @@ def __call__(
521522 prompt_embeds: Optional precomputed prompt embeddings for the text encoder.
522523 negative_prompt_embeds: Optional precomputed negative prompt embeddings for the text encoder.
523524 vae_only: Whether to only run the decoder for a given latent.
525+ use_vace_cache: Whether to compute the VACE hints once and reuse them at every denoising step. This only
526+ approximates the full model, so the model needs to be fine-tuned with hints computed the same way, see
527+ `run_inference`.
524528 """
525529 config = getattr (self , "config" , None )
526530 if max_sequence_length is None :
@@ -645,6 +649,7 @@ def __call__(
645649 scheduler = self .scheduler ,
646650 scheduler_state = scheduler_state ,
647651 use_kv_cache = use_kv_cache ,
652+ use_vace_cache = use_vace_cache ,
648653 )
649654
650655 with self .mesh , nn_partitioning .axis_rules (self .config .logical_axis_rules ):
@@ -733,7 +738,7 @@ def prepare_video_latents(
733738 return jnp .stack (latent_list )
734739
735740
736- @partial (aot_cache .cached_jit , static_argnames = ("do_classifier_free_guidance" ,))
741+ @partial (aot_cache .cached_jit , static_argnames = ("do_classifier_free_guidance" , "return_vace_hints" ))
737742def transformer_forward_pass (
738743 graphdef : nnx .graph .GraphDef ,
739744 sharded_state : nnx .State ,
@@ -747,20 +752,31 @@ def transformer_forward_pass(
747752 guidance_scale : float ,
748753 kv_cache = None ,
749754 encoder_attention_mask = None ,
755+ vace_hints = None ,
756+ return_vace_hints = False ,
750757):
751- """Performs a forward pass on the transformer."""
758+ """Performs a forward pass on the transformer.
759+
760+ With `return_vace_hints=True`, returns the VACE hints instead of the noise prediction, to be passed back as
761+ `vace_hints`.
762+ """
752763 if do_classifier_free_guidance and latents .shape [0 ] != prompt_embeds .shape [0 ]:
753764 latents = jnp .concatenate ([latents , latents ], axis = 0 )
754765 wan_transformer = nnx .merge (graphdef , sharded_state , rest_of_state )
755- noise_pred = wan_transformer (
766+ output = wan_transformer (
756767 hidden_states = latents ,
757768 timestep = timestep ,
758769 encoder_hidden_states = prompt_embeds ,
759770 control_hidden_states = control_hidden_states ,
760771 control_hidden_states_scale = control_hidden_states_scale ,
761772 kv_cache = kv_cache ,
762773 encoder_attention_mask = encoder_attention_mask ,
774+ vace_hints = vace_hints ,
775+ return_vace_hints = return_vace_hints ,
763776 )
777+ if return_vace_hints :
778+ return output
779+ noise_pred = output
764780 if do_classifier_free_guidance :
765781 bsz = latents .shape [0 ] // 2
766782 noise_uncond = noise_pred [bsz :]
@@ -784,6 +800,7 @@ def run_inference(
784800 control_hidden_states ,
785801 control_hidden_states_scale ,
786802 use_kv_cache : bool = False ,
803+ use_vace_cache : bool = False ,
787804):
788805 do_classifier_free_guidance = guidance_scale > 1.0
789806 if do_classifier_free_guidance :
@@ -798,6 +815,26 @@ def run_inference(
798815
799816 bsz = latents .shape [0 ] * 2 if do_classifier_free_guidance else latents .shape [0 ]
800817 timesteps = jnp .array (scheduler_state .timesteps , dtype = jnp .int32 )
818+
819+ vace_hints = None
820+ if use_vace_cache :
821+ # The hints are computed once, from zero latents at the first timestep, and reused at every step.
822+ vace_hints = transformer_forward_pass (
823+ graphdef ,
824+ sharded_state ,
825+ rest_of_state ,
826+ jnp .zeros_like (latents ),
827+ jnp .broadcast_to (timesteps [0 ], (bsz ,)),
828+ prompt_embeds ,
829+ control_hidden_states = control_hidden_states ,
830+ control_hidden_states_scale = control_hidden_states_scale ,
831+ do_classifier_free_guidance = do_classifier_free_guidance ,
832+ guidance_scale = guidance_scale ,
833+ kv_cache = kv_cache ,
834+ encoder_attention_mask = encoder_attention_mask ,
835+ return_vace_hints = True ,
836+ )
837+
801838 for step in range (num_inference_steps ):
802839 t = timesteps [step ]
803840 timestep = jnp .broadcast_to (t , (bsz ,))
@@ -815,6 +852,7 @@ def run_inference(
815852 guidance_scale = guidance_scale ,
816853 kv_cache = kv_cache ,
817854 encoder_attention_mask = encoder_attention_mask ,
855+ vace_hints = vace_hints ,
818856 )
819857
820858 latents , scheduler_state = scheduler .step (scheduler_state , noise_pred , t , latents ).to_tuple ()
0 commit comments