Repository navigation
Fix KL reduction for video VAE posteriors - #15471
Open
taking-lying-flat wants to merge 1 commit into
Open
taking-lying-flat wants to merge 1 commit into
taking-lying-flat wants to merge 1 commit into
Conversation
Contributor
|
Hi @taking-lying-flat, thanks for the PR! It does not appear to link an issue it fixes.
See the contribution guide for more details. Please note that PRs without a linked issue are likely to be automatically closed 10 days after this notice. Once the PR links an existing issue, the author box is ticked, or the |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
Fix
DiagonalGaussianDistribution.kl()to sum every non-batch dimension in both the standard-prior andkl(other)branches. The fixed axes[1, 2, 3]leave the width axis unreduced for video posteriors[B,C,T,H,W], returning[B,W]instead of[B]. If training usesposterior.kl().mean()for total KL per video averaged over the batch, its loss and encoder gradients are consequently scaled by1/W.Reproduced through
AutoencoderKLWan.encode()with the officialWan-AI/Wan2.1-T2V-1.3B-DiffusersVAE at revision0fad780a534b6463e45facd96134c9f345acfa5b, CPU FP32, PyTorch 2.11.0+cu128, and Diffusers main12d5c7a53a7c. Input[2,3,5,32,48]yields posterior[2,16,2,4,6]:encoder.conv_out.weightgradient norm[2,6]Independent(Normal, 4)reference[2][2]Multiplying the original gradient by 6 matches the reference with relative L2 error
1.38e-7; after the fix, the error is4.23e-8. Thekl(other)branch has the same width-dependent scaling bug. Callers already using.kl().sum()/batch_sizeobtain the correct scalar loss; image posteriors retain the same reduction.Reproduce
Self-review
Used Codex and the repository's
self-reviewskill. READY: no blocking or non-blocking findings, no added dead code or dependencies. The change is limited to KL event-dimension reduction.