Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
renzhc
diffusers_dcu
Commits
58d2b10a
Unverified
Commit
58d2b10a
authored
Jul 31, 2025
by
YiYi Xu
Committed by
GitHub
Jul 31, 2025
Browse files
[wan2.2] fix vae patches (#12041)
up
parent
20e0740b
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
25 additions
and
53 deletions
+25
-53
src/diffusers/models/autoencoders/autoencoder_kl_wan.py
src/diffusers/models/autoencoders/autoencoder_kl_wan.py
+25
-53
No files found.
src/diffusers/models/autoencoders/autoencoder_kl_wan.py
View file @
58d2b10a
...
@@ -913,38 +913,21 @@ def patchify(x, patch_size):
...
@@ -913,38 +913,21 @@ def patchify(x, patch_size):
if
patch_size
==
1
:
if
patch_size
==
1
:
return
x
return
x
if
x
.
dim
()
==
4
:
if
x
.
dim
()
!=
5
:
# x shape: [batch_size, channels, height, width]
raise
ValueError
(
f
"Invalid input shape:
{
x
.
shape
}
"
)
batch_size
,
channels
,
height
,
width
=
x
.
shape
# x shape: [batch_size, channels, frames, height, width]
batch_size
,
channels
,
frames
,
height
,
width
=
x
.
shape
# Ensure height and width are divisible by patch_size
if
height
%
patch_size
!=
0
or
width
%
patch_size
!=
0
:
raise
ValueError
(
f
"Height (
{
height
}
) and width (
{
width
}
) must be divisible by patch_size (
{
patch_size
}
)"
)
# Reshape to [batch_size, channels, height//patch_size, patch_size, width//patch_size, patch_size]
x
=
x
.
view
(
batch_size
,
channels
,
height
//
patch_size
,
patch_size
,
width
//
patch_size
,
patch_size
)
# Rearrange to [batch_size, channels * patch_size * patch_size, height//patch_size, width//patch_size]
x
=
x
.
permute
(
0
,
1
,
3
,
5
,
2
,
4
).
contiguous
()
x
=
x
.
view
(
batch_size
,
channels
*
patch_size
*
patch_size
,
height
//
patch_size
,
width
//
patch_size
)
elif
x
.
dim
()
==
5
:
# x shape: [batch_size, channels, frames, height, width]
batch_size
,
channels
,
frames
,
height
,
width
=
x
.
shape
# Ensure height and width are divisible by patch_size
if
height
%
patch_size
!=
0
or
width
%
patch_size
!=
0
:
raise
ValueError
(
f
"Height (
{
height
}
) and width (
{
width
}
) must be divisible by patch_size (
{
patch_size
}
)"
)
# Reshape to [batch_size, channels, frames, height//patch_size, patch_size, width//patch_size, patch_size]
# Ensure height and width are divisible by patch_size
x
=
x
.
view
(
batch_size
,
channels
,
frames
,
height
//
patch_size
,
patch_size
,
width
//
patch_size
,
patch_size
)
if
height
%
patch_size
!=
0
or
width
%
patch_size
!=
0
:
raise
ValueError
(
f
"Height (
{
height
}
) and width (
{
width
}
) must be divisible by patch_size (
{
patch_size
}
)"
)
# Rearrange to [batch_size, channels * patch_size * patch_size, frames, height//patch_size, width//patch_size]
# Reshape to [batch_size, channels, frames, height//patch_size, patch_size, width//patch_size, patch_size]
x
=
x
.
permute
(
0
,
1
,
4
,
6
,
2
,
3
,
5
).
contiguous
()
x
=
x
.
view
(
batch_size
,
channels
,
frames
,
height
//
patch_size
,
patch_size
,
width
//
patch_size
,
patch_size
)
x
=
x
.
view
(
batch_size
,
channels
*
patch_size
*
patch_size
,
frames
,
height
//
patch_size
,
width
//
patch_size
)
else
:
# Rearrange to [batch_size, channels * patch_size * patch_size, frames, height//patch_size, width//patch_size]
raise
ValueError
(
f
"Invalid input shape:
{
x
.
shape
}
"
)
x
=
x
.
permute
(
0
,
1
,
6
,
4
,
2
,
3
,
5
).
contiguous
()
x
=
x
.
view
(
batch_size
,
channels
*
patch_size
*
patch_size
,
frames
,
height
//
patch_size
,
width
//
patch_size
)
return
x
return
x
...
@@ -953,29 +936,18 @@ def unpatchify(x, patch_size):
...
@@ -953,29 +936,18 @@ def unpatchify(x, patch_size):
if
patch_size
==
1
:
if
patch_size
==
1
:
return
x
return
x
if
x
.
dim
()
==
4
:
if
x
.
dim
()
!=
5
:
# x shape: [b, (c * patch_size * patch_size), h, w]
raise
ValueError
(
f
"Invalid input shape:
{
x
.
shape
}
"
)
batch_size
,
c_patches
,
height
,
width
=
x
.
shape
# x shape: [batch_size, (channels * patch_size * patch_size), frame, height, width]
channels
=
c_patches
//
(
patch_size
*
patch_size
)
batch_size
,
c_patches
,
frames
,
height
,
width
=
x
.
shape
channels
=
c_patches
//
(
patch_size
*
patch_size
)
# Reshape to [b, c, patch_size, patch_size, h, w]
x
=
x
.
view
(
batch_size
,
channels
,
patch_size
,
patch_size
,
height
,
width
)
# Rearrange to [b, c, h * patch_size, w * patch_size]
x
=
x
.
permute
(
0
,
1
,
4
,
2
,
5
,
3
).
contiguous
()
x
=
x
.
view
(
batch_size
,
channels
,
height
*
patch_size
,
width
*
patch_size
)
elif
x
.
dim
()
==
5
:
# x shape: [batch_size, (channels * patch_size * patch_size), frame, height, width]
batch_size
,
c_patches
,
frames
,
height
,
width
=
x
.
shape
channels
=
c_patches
//
(
patch_size
*
patch_size
)
# Reshape to [b, c, patch_size, patch_size, f, h, w]
# Reshape to [b, c, patch_size, patch_size, f, h, w]
x
=
x
.
view
(
batch_size
,
channels
,
patch_size
,
patch_size
,
frames
,
height
,
width
)
x
=
x
.
view
(
batch_size
,
channels
,
patch_size
,
patch_size
,
frames
,
height
,
width
)
# Rearrange to [b, c, f, h * patch_size, w * patch_size]
# Rearrange to [b, c, f, h * patch_size, w * patch_size]
x
=
x
.
permute
(
0
,
1
,
4
,
5
,
2
,
6
,
3
).
contiguous
()
x
=
x
.
permute
(
0
,
1
,
4
,
5
,
3
,
6
,
2
).
contiguous
()
x
=
x
.
view
(
batch_size
,
channels
,
frames
,
height
*
patch_size
,
width
*
patch_size
)
x
=
x
.
view
(
batch_size
,
channels
,
frames
,
height
*
patch_size
,
width
*
patch_size
)
return
x
return
x
...
@@ -1044,7 +1016,6 @@ class AutoencoderKLWan(ModelMixin, ConfigMixin, FromOriginalModelMixin):
...
@@ -1044,7 +1016,6 @@ class AutoencoderKLWan(ModelMixin, ConfigMixin, FromOriginalModelMixin):
patch_size
:
Optional
[
int
]
=
None
,
patch_size
:
Optional
[
int
]
=
None
,
scale_factor_temporal
:
Optional
[
int
]
=
4
,
scale_factor_temporal
:
Optional
[
int
]
=
4
,
scale_factor_spatial
:
Optional
[
int
]
=
8
,
scale_factor_spatial
:
Optional
[
int
]
=
8
,
clip_output
:
bool
=
True
,
)
->
None
:
)
->
None
:
super
().
__init__
()
super
().
__init__
()
...
@@ -1244,10 +1215,11 @@ class AutoencoderKLWan(ModelMixin, ConfigMixin, FromOriginalModelMixin):
...
@@ -1244,10 +1215,11 @@ class AutoencoderKLWan(ModelMixin, ConfigMixin, FromOriginalModelMixin):
out_
=
self
.
decoder
(
x
[:,
:,
i
:
i
+
1
,
:,
:],
feat_cache
=
self
.
_feat_map
,
feat_idx
=
self
.
_conv_idx
)
out_
=
self
.
decoder
(
x
[:,
:,
i
:
i
+
1
,
:,
:],
feat_cache
=
self
.
_feat_map
,
feat_idx
=
self
.
_conv_idx
)
out
=
torch
.
cat
([
out
,
out_
],
2
)
out
=
torch
.
cat
([
out
,
out_
],
2
)
if
self
.
config
.
clip_output
:
out
=
torch
.
clamp
(
out
,
min
=-
1.0
,
max
=
1.0
)
if
self
.
config
.
patch_size
is
not
None
:
if
self
.
config
.
patch_size
is
not
None
:
out
=
unpatchify
(
out
,
patch_size
=
self
.
config
.
patch_size
)
out
=
unpatchify
(
out
,
patch_size
=
self
.
config
.
patch_size
)
out
=
torch
.
clamp
(
out
,
min
=-
1.0
,
max
=
1.0
)
self
.
clear_cache
()
self
.
clear_cache
()
if
not
return_dict
:
if
not
return_dict
:
return
(
out
,)
return
(
out
,)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment