Question: Why concatenate reversed noise slices for vace_reference_image?
First of all, thank you for the excellent work on this project!
While reading through the codebase, I encountered a confusing implementation detail in the WanVideoUnit_NoiseInitializer class that I hope to get clarified.
# diffsynth/pipelines/wan_video.py
class WanVideoUnit_NoiseInitializer(PipelineUnit):
def __init__(self):
super().__init__(
input_params=("height", "width", "num_frames", "seed", "rand_device", "vace_reference_image"),
output_params=("noise",)
)
def process(self, pipe: WanVideoPipeline, height, width, num_frames, seed, rand_device, vace_reference_image):
length = (num_frames - 1) // 4 + 1
if vace_reference_image is not None:
f = len(vace_reference_image) if isinstance(vace_reference_image, list) else 1
length += f
shape = (1, pipe.vae.model.z_dim, length, height // pipe.vae.upsampling_factor, width // pipe.vae.upsampling_factor)
noise = pipe.generate_noise(shape, seed=seed, rand_device=rand_device)
if vace_reference_image is not None:
noise = torch.concat((noise[:, :, -f:], noise[:, :, :-f]), dim=2)
return {"noise": noise}
Specifically, the line:
noise = torch.concat((noise[:, :, -f:], noise[:, :, :-f]), dim=2)
appears to concatenate the last f frames of the noise with the remaining frames, effectively performing a circular shift/rotation of the noise tensor along the temporal dimension (dim=2).
Question: What is the motivation behind this specific concatenation pattern?
Intuitively, since the noise is random, shuffling/rotating it seems to produce another random tensor with identical statistical properties. Is this reordering serving a specific purpose?
Additionally, I would also like to ask: does the current pipeline only support a single vace_reference_image?
# diffsynth/pipelines/wan_video.py
class WanVideoUnit_InputVideoEmbedder(PipelineUnit):
def __init__(self):
super().__init__(
input_params=("input_video", "noise", "tiled", "tile_size", "tile_stride", "vace_reference_image"),
output_params=("latents", "input_latents"),
onload_model_names=("vae",)
)
def process(self, pipe: WanVideoPipeline, input_video, noise, tiled, tile_size, tile_stride, vace_reference_image):
if input_video is None:
return {"latents": noise}
pipe.load_models_to_device(self.onload_model_names)
input_video = pipe.preprocess_video(input_video)
input_latents = pipe.vae.encode(input_video, device=pipe.device, tiled=tiled, tile_size=tile_size, tile_stride=tile_stride).to(dtype=pipe.torch_dtype, device=pipe.device)
if vace_reference_image is not None:
if not isinstance(vace_reference_image, list):
vace_reference_image = [vace_reference_image]
vace_reference_image = pipe.preprocess_video(vace_reference_image)
vace_reference_latents = pipe.vae.encode(vace_reference_image, device=pipe.device).to(dtype=pipe.torch_dtype, device=pipe.device)
input_latents = torch.concat([vace_reference_latents, input_latents], dim=2)
if pipe.scheduler.training:
return {"latents": noise, "input_latents": input_latents}
else:
latents = pipe.scheduler.add_noise(input_latents, noise, timestep=pipe.scheduler.timesteps[0])
return {"latents": latents}
I noticed that in subsequent units, the vace_reference_image list is directly encoded by the VAE—for example, 5 reference images would be encoded into a latent with t=2 (WAN VAE). However, in the WanVideoUnit_NoiseInitializer, the length is simply incremented by len(vace_reference_image).
This increment logic seems to only hold true when there is a single vace_reference_image. If multiple images are provided, there appears to be a mismatch.
Is this a known limitation, or am I misunderstanding the intended behavior?
@pangy9
First, why is this line necessary?
noise = torch.concat((noise[:, :, -f:], noise[:, :, :-f]), dim=2)
Because if we don’t do this, users will report that the results generated with the same random seed are inconsistent with those from other codebases.
Second, does the current pipeline only support a single vace_reference_image?
Yes. In the base Wan model, the first frame is processed separately, and every subsequent four frames are compressed into a single latent embedding, so the total number of generated frames is always 4n+1. However, the VACE model team modified this mechanism by treating the first frame as a reference image, resulting in a total frame count of always 4n. The model architecture does not provide positions for additional reference images.
Got it. Thank you very much.
First, why is this line necessary?
noise = torch.concat((noise[:, :, -f:], noise[:, :, :-f]), dim=2) Because if we don’t do this, users will report that the results generated with the same random seed are inconsistent with those from other codebases.
Second, does the current pipeline only support a single
vace_reference_image?Yes. In the base Wan model, the first frame is processed separately, and every subsequent four frames are compressed into a single latent embedding, so the total number of generated frames is always 4n+1. However, the VACE model team modified this mechanism by treating the first frame as a reference image, resulting in a total frame count of always 4n. The model architecture does not provide positions for additional reference images.