-
Notifications
You must be signed in to change notification settings - Fork 63
Adding Vae Decoder in Wan #688
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
Open
mohiso22
wants to merge
4
commits into
quic:main
Choose a base branch
from
mohiso22:diffuser
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+322
−18
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
200 changes: 200 additions & 0 deletions
200
QEfficient/diffusers/models/autoencoders/autoencoder_kl_wan.py
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,200 @@ | ||
| # ----------------------------------------------------------------------------- | ||
| # | ||
| # Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries. | ||
| # SPDX-License-Identifier: BSD-3-Clause | ||
| # | ||
| # ----------------------------------------------------------------------------- | ||
|
|
||
| import torch | ||
| from diffusers.models.autoencoders.autoencoder_kl_wan import ( | ||
| WanDecoder3d, | ||
| WanEncoder3d, | ||
| WanResample, | ||
| WanResidualBlock, | ||
| WanUpsample, | ||
| ) | ||
|
|
||
| CACHE_T = 2 | ||
|
|
||
| modes = [] | ||
|
|
||
| # Used max(0, x.shape[2] - CACHE_T) instead of CACHE_T because x.shape[2] is either 1 or 4, | ||
| # and CACHE_T = 2. This ensures the value never goes negative | ||
|
|
||
|
|
||
| class QEffWanResample(WanResample): | ||
| def __qeff_init__(self): | ||
| # Changed upsampling mode from "nearest-exact" to "nearest" for ONNX compatibility. | ||
| # Since the scale factor is an integer, both modes behave the | ||
| if self.mode in ("upsample2d", "upsample3d"): | ||
| self.resample[0] = WanUpsample(scale_factor=(2.0, 2.0), mode="nearest") | ||
|
|
||
| def forward(self, x, feat_cache=None, feat_idx=[0]): | ||
| b, c, t, h, w = x.size() | ||
| if self.mode == "upsample3d": | ||
| if feat_cache is not None: | ||
| idx = feat_idx[0] | ||
| if feat_cache[idx] is None: | ||
| feat_cache[idx] = "Rep" | ||
| feat_idx[0] += 1 | ||
| else: | ||
| cache_x = x[:, :, max(0, x.shape[2] - CACHE_T) :, :, :].clone() | ||
| if cache_x.shape[2] < 2 and feat_cache[idx] is not None and feat_cache[idx] != "Rep": | ||
| # cache last frame of last two chunk | ||
| cache_x = torch.cat( | ||
| [feat_cache[idx][:, :, -1, :, :].unsqueeze(2).to(cache_x.device), cache_x], dim=2 | ||
| ) | ||
| if cache_x.shape[2] < 2 and feat_cache[idx] is not None and feat_cache[idx] == "Rep": | ||
| cache_x = torch.cat([torch.zeros_like(cache_x).to(cache_x.device), cache_x], dim=2) | ||
| if feat_cache[idx] == "Rep": | ||
| x = self.time_conv(x) | ||
| else: | ||
| x = self.time_conv(x, feat_cache[idx]) | ||
| feat_cache[idx] = cache_x | ||
| feat_idx[0] += 1 | ||
|
|
||
| x = x.reshape(b, 2, c, t, h, w) | ||
| x = torch.stack((x[:, 0, :, :, :, :], x[:, 1, :, :, :, :]), 3) | ||
| x = x.reshape(b, c, t * 2, h, w) | ||
| t = x.shape[2] | ||
| x = x.permute(0, 2, 1, 3, 4).reshape(b * t, c, h, w) | ||
| modes.append(self.mode) | ||
| x = self.resample(x) | ||
| x = x.view(b, t, x.size(1), x.size(2), x.size(3)).permute(0, 2, 1, 3, 4) | ||
|
|
||
| if self.mode == "downsample3d": | ||
| if feat_cache is not None: | ||
| idx = feat_idx[0] | ||
| if feat_cache[idx] is None: | ||
| feat_cache[idx] = x.clone() | ||
| feat_idx[0] += 1 | ||
| else: | ||
| cache_x = x[:, :, -1:, :, :].clone() | ||
| x = self.time_conv(torch.cat([feat_cache[idx][:, :, -1:, :, :], x], 2)) | ||
| feat_cache[idx] = cache_x | ||
| feat_idx[0] += 1 | ||
| return x | ||
|
|
||
|
|
||
| class QEffWanResidualBlock(WanResidualBlock): | ||
| def forward(self, x, feat_cache=None, feat_idx=[0]): | ||
| # Apply shortcut connection | ||
| h = self.conv_shortcut(x) | ||
|
|
||
| # First normalization and activation | ||
| x = self.norm1(x) | ||
| x = self.nonlinearity(x) | ||
|
|
||
| if feat_cache is not None: | ||
| idx = feat_idx[0] | ||
| cache_x = x[:, :, max(0, x.shape[2] - CACHE_T) :, :, :].clone() | ||
| if cache_x.shape[2] < 2 and feat_cache[idx] is not None: | ||
| cache_x = torch.cat([feat_cache[idx][:, :, -1, :, :].unsqueeze(2).to(cache_x.device), cache_x], dim=2) | ||
|
|
||
| x = self.conv1(x, feat_cache[idx]) | ||
| feat_cache[idx] = cache_x | ||
| feat_idx[0] += 1 | ||
| else: | ||
| x = self.conv1(x) | ||
|
|
||
| # Second normalization and activation | ||
| x = self.norm2(x) | ||
| x = self.nonlinearity(x) | ||
|
|
||
| # Dropout | ||
| x = self.dropout(x) | ||
|
|
||
| if feat_cache is not None: | ||
| idx = feat_idx[0] | ||
| cache_x = x[:, :, max(0, x.shape[2] - CACHE_T) :, :, :].clone() | ||
| if cache_x.shape[2] < 2 and feat_cache[idx] is not None: | ||
| cache_x = torch.cat([feat_cache[idx][:, :, -1, :, :].unsqueeze(2).to(cache_x.device), cache_x], dim=2) | ||
|
|
||
| x = self.conv2(x, feat_cache[idx]) | ||
| feat_cache[idx] = cache_x | ||
| feat_idx[0] += 1 | ||
| else: | ||
| x = self.conv2(x) | ||
|
|
||
| # Add residual connection | ||
| return x + h | ||
|
|
||
|
|
||
| class QEffWanEncoder3d(WanEncoder3d): | ||
| def forward(self, x, feat_cache=None, feat_idx=[0]): | ||
| if feat_cache is not None: | ||
| idx = feat_idx[0] | ||
| cache_x = x[:, :, max(0, x.shape[2] - CACHE_T) :, :, :].clone() | ||
| if cache_x.shape[2] < 2 and feat_cache[idx] is not None: | ||
| # cache last frame of last two chunk | ||
| cache_x = torch.cat([feat_cache[idx][:, :, -1, :, :].unsqueeze(2).to(cache_x.device), cache_x], dim=2) | ||
| x = self.conv_in(x, feat_cache[idx]) | ||
| feat_cache[idx] = cache_x | ||
| feat_idx[0] += 1 | ||
| else: | ||
| x = self.conv_in(x) | ||
|
|
||
| ## downsamples | ||
| for layer in self.down_blocks: | ||
| if feat_cache is not None: | ||
| x = layer(x, feat_cache, feat_idx) | ||
| else: | ||
| x = layer(x) | ||
|
|
||
| ## middle | ||
| x = self.mid_block(x, feat_cache, feat_idx) | ||
|
|
||
| ## head | ||
| x = self.norm_out(x) | ||
| x = self.nonlinearity(x) | ||
| if feat_cache is not None: | ||
| idx = feat_idx[0] | ||
| cache_x = x[:, :, max(0, x.shape[2] - CACHE_T) :, :, :].clone() | ||
| if cache_x.shape[2] < 2 and feat_cache[idx] is not None: | ||
| # cache last frame of last two chunk | ||
| cache_x = torch.cat([feat_cache[idx][:, :, -1, :, :].unsqueeze(2).to(cache_x.device), cache_x], dim=2) | ||
| x = self.conv_out(x, feat_cache[idx]) | ||
| feat_cache[idx] = cache_x | ||
| feat_idx[0] += 1 | ||
| else: | ||
| x = self.conv_out(x) | ||
| return x | ||
|
|
||
|
|
||
| class QEffWanDecoder3d(WanDecoder3d): | ||
| def forward(self, x, feat_cache=None, feat_idx=[0], first_chunk=False): | ||
| ## conv1 | ||
| if feat_cache is not None: | ||
| idx = feat_idx[0] | ||
| cache_x = x[:, :, max(0, x.shape[2] - CACHE_T) :, :, :].clone() | ||
| if cache_x.shape[2] < 2 and feat_cache[idx] is not None: | ||
| # cache last frame of last two chunk | ||
| cache_x = torch.cat([feat_cache[idx][:, :, -1, :, :].unsqueeze(2).to(cache_x.device), cache_x], dim=2) | ||
| x = self.conv_in(x, feat_cache[idx]) | ||
| feat_cache[idx] = cache_x | ||
| feat_idx[0] += 1 | ||
| else: | ||
| x = self.conv_in(x) | ||
|
|
||
| ## middle | ||
| x = self.mid_block(x, feat_cache, feat_idx) | ||
|
|
||
| ## upsamples | ||
| for up_block in self.up_blocks: | ||
| x = up_block(x, feat_cache, feat_idx, first_chunk=first_chunk) | ||
|
|
||
| ## head | ||
| x = self.norm_out(x) | ||
| x = self.nonlinearity(x) | ||
| if feat_cache is not None: | ||
| idx = feat_idx[0] | ||
| cache_x = x[:, :, max(0, x.shape[2] - CACHE_T) :, :, :].clone() | ||
| if cache_x.shape[2] < 2 and feat_cache[idx] is not None: | ||
| # cache last frame of last two chunk | ||
| cache_x = torch.cat([feat_cache[idx][:, :, -1, :, :].unsqueeze(2).to(cache_x.device), cache_x], dim=2) | ||
| x = self.conv_out(x, feat_cache[idx]) | ||
| feat_cache[idx] = cache_x | ||
| feat_idx[0] += 1 | ||
| else: | ||
| x = self.conv_out(x) | ||
| return x | ||
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.