Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

latent padding training and inference different #660

Open
cdfan0627 opened this issue Jan 12, 2025 · 2 comments
Open

latent padding training and inference different #660

cdfan0627 opened this issue Jan 12, 2025 · 2 comments
Assignees

Comments

@cdfan0627
Copy link

cdfan0627 commented Jan 12, 2025

您好,請問為什麼在CogvideoX 1.5 I2V model training的時候是先補齊latent的shape然後再對image latent 做padding,如下code 是 lora_trainer.py

patch_size_t = self.state.transformer_config.patch_size_t
        if patch_size_t is not None:
            ncopy = latent.shape[2] % patch_size_t
            first_frame = latent[:, :, :1, :, :]  # Get first frame [B, C, 1, H, W]
            latent = torch.cat([first_frame.repeat(1, 1, ncopy, 1, 1), latent], dim=2)
            assert latent.shape[2] % patch_size_t == 0
latent = latent.permute(0, 2, 1, 3, 4)
image_latents = image_latents.permute(0, 2, 1, 3, 4)
padding_shape = (latent.shape[0], latent.shape[1] - 1, *latent.shape[2:])
        latent_padding = image_latents.new_zeros(padding_shape)
        image_latents = torch.cat([image_latents, latent_padding], dim=1)

inference的時候卻對image latent做padding後,再補上第一個frame,如下code是pipeline_cogvideox_image2video.py

padding_shape = (
            batch_size,
            num_frames - 1,
            num_channels_latents,
            height // self.vae_scale_factor_spatial,
            width // self.vae_scale_factor_spatial,
        )

        latent_padding = torch.zeros(padding_shape, device=device, dtype=dtype)
        image_latents = torch.cat([image_latents, latent_padding], dim=1)

        if self.transformer.config.patch_size_t is not None:
            first_frame = image_latents[:, : image_latents.size(1) % self.transformer.config.patch_size_t, ...]
            image_latents = torch.cat([first_frame, image_latents], dim=1)

理論上training跟inference應該要一樣,inference出來的結果才會正確的,所以想請問一下training跟inference哪個是對的呢

@cdfan0627 cdfan0627 changed the title latent padding training and inference latent padding training and inference different Jan 12, 2025
@OleehyO OleehyO self-assigned this Jan 13, 2025
@OleehyO
Copy link
Collaborator

OleehyO commented Jan 13, 2025

pipeline里的代码由于一些历史原因写起来有点啰嗦,建议以training的代码作为参考。虽然实现方式有区别,但是最后的结果都是一样的。

@cdfan0627
Copy link
Author

可是inference的看起來比較合理,因為這樣image_latents的每一個latent才會跟video latent 對齊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants