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
chenpangpang
ComfyUI
Commits
5eddfdd8
Commit
5eddfdd8
authored
Jan 02, 2024
by
comfyanonymous
Browse files
Refactor VAE code.
Replace constants with downscale_ratio and latent_channels.
parent
8e2c99e3
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
10 additions
and
8 deletions
+10
-8
comfy/sd.py
comfy/sd.py
+10
-8
No files found.
comfy/sd.py
View file @
5eddfdd8
...
...
@@ -157,6 +157,8 @@ class VAE:
self
.
memory_used_encode
=
lambda
shape
,
dtype
:
(
1767
*
shape
[
2
]
*
shape
[
3
])
*
model_management
.
dtype_size
(
dtype
)
#These are for AutoencoderKL and need tweaking (should be lower)
self
.
memory_used_decode
=
lambda
shape
,
dtype
:
(
2178
*
shape
[
2
]
*
shape
[
3
]
*
64
)
*
model_management
.
dtype_size
(
dtype
)
self
.
downscale_ratio
=
8
self
.
latent_channels
=
4
if
config
is
None
:
if
"decoder.mid.block_1.mix_factor"
in
sd
:
...
...
@@ -204,9 +206,9 @@ class VAE:
decode_fn
=
lambda
a
:
(
self
.
first_stage_model
.
decode
(
a
.
to
(
self
.
vae_dtype
).
to
(
self
.
device
))
+
1.0
).
float
()
output
=
torch
.
clamp
((
(
comfy
.
utils
.
tiled_scale
(
samples
,
decode_fn
,
tile_x
//
2
,
tile_y
*
2
,
overlap
,
upscale_amount
=
8
,
output_device
=
self
.
output_device
,
pbar
=
pbar
)
+
comfy
.
utils
.
tiled_scale
(
samples
,
decode_fn
,
tile_x
*
2
,
tile_y
//
2
,
overlap
,
upscale_amount
=
8
,
output_device
=
self
.
output_device
,
pbar
=
pbar
)
+
comfy
.
utils
.
tiled_scale
(
samples
,
decode_fn
,
tile_x
,
tile_y
,
overlap
,
upscale_amount
=
8
,
output_device
=
self
.
output_device
,
pbar
=
pbar
))
(
comfy
.
utils
.
tiled_scale
(
samples
,
decode_fn
,
tile_x
//
2
,
tile_y
*
2
,
overlap
,
upscale_amount
=
self
.
downscale_ratio
,
output_device
=
self
.
output_device
,
pbar
=
pbar
)
+
comfy
.
utils
.
tiled_scale
(
samples
,
decode_fn
,
tile_x
*
2
,
tile_y
//
2
,
overlap
,
upscale_amount
=
self
.
downscale_ratio
,
output_device
=
self
.
output_device
,
pbar
=
pbar
)
+
comfy
.
utils
.
tiled_scale
(
samples
,
decode_fn
,
tile_x
,
tile_y
,
overlap
,
upscale_amount
=
self
.
downscale_ratio
,
output_device
=
self
.
output_device
,
pbar
=
pbar
))
/
3.0
)
/
2.0
,
min
=
0.0
,
max
=
1.0
)
return
output
...
...
@@ -217,9 +219,9 @@ class VAE:
pbar
=
comfy
.
utils
.
ProgressBar
(
steps
)
encode_fn
=
lambda
a
:
self
.
first_stage_model
.
encode
((
2.
*
a
-
1.
).
to
(
self
.
vae_dtype
).
to
(
self
.
device
)).
float
()
samples
=
comfy
.
utils
.
tiled_scale
(
pixel_samples
,
encode_fn
,
tile_x
,
tile_y
,
overlap
,
upscale_amount
=
(
1
/
8
),
out_channels
=
4
,
output_device
=
self
.
output_device
,
pbar
=
pbar
)
samples
+=
comfy
.
utils
.
tiled_scale
(
pixel_samples
,
encode_fn
,
tile_x
*
2
,
tile_y
//
2
,
overlap
,
upscale_amount
=
(
1
/
8
),
out_channels
=
4
,
output_device
=
self
.
output_device
,
pbar
=
pbar
)
samples
+=
comfy
.
utils
.
tiled_scale
(
pixel_samples
,
encode_fn
,
tile_x
//
2
,
tile_y
*
2
,
overlap
,
upscale_amount
=
(
1
/
8
),
out_channels
=
4
,
output_device
=
self
.
output_device
,
pbar
=
pbar
)
samples
=
comfy
.
utils
.
tiled_scale
(
pixel_samples
,
encode_fn
,
tile_x
,
tile_y
,
overlap
,
upscale_amount
=
(
1
/
self
.
downscale_ratio
),
out_channels
=
self
.
latent_channels
,
output_device
=
self
.
output_device
,
pbar
=
pbar
)
samples
+=
comfy
.
utils
.
tiled_scale
(
pixel_samples
,
encode_fn
,
tile_x
*
2
,
tile_y
//
2
,
overlap
,
upscale_amount
=
(
1
/
self
.
downscale_ratio
),
out_channels
=
self
.
latent_channels
,
output_device
=
self
.
output_device
,
pbar
=
pbar
)
samples
+=
comfy
.
utils
.
tiled_scale
(
pixel_samples
,
encode_fn
,
tile_x
//
2
,
tile_y
*
2
,
overlap
,
upscale_amount
=
(
1
/
self
.
downscale_ratio
),
out_channels
=
self
.
latent_channels
,
output_device
=
self
.
output_device
,
pbar
=
pbar
)
samples
/=
3.0
return
samples
...
...
@@ -231,7 +233,7 @@ class VAE:
batch_number
=
int
(
free_memory
/
memory_used
)
batch_number
=
max
(
1
,
batch_number
)
pixel_samples
=
torch
.
empty
((
samples_in
.
shape
[
0
],
3
,
round
(
samples_in
.
shape
[
2
]
*
8
),
round
(
samples_in
.
shape
[
3
]
*
8
)),
device
=
self
.
output_device
)
pixel_samples
=
torch
.
empty
((
samples_in
.
shape
[
0
],
3
,
round
(
samples_in
.
shape
[
2
]
*
self
.
downscale_ratio
),
round
(
samples_in
.
shape
[
3
]
*
self
.
downscale_ratio
)),
device
=
self
.
output_device
)
for
x
in
range
(
0
,
samples_in
.
shape
[
0
],
batch_number
):
samples
=
samples_in
[
x
:
x
+
batch_number
].
to
(
self
.
vae_dtype
).
to
(
self
.
device
)
pixel_samples
[
x
:
x
+
batch_number
]
=
torch
.
clamp
((
self
.
first_stage_model
.
decode
(
samples
).
to
(
self
.
output_device
).
float
()
+
1.0
)
/
2.0
,
min
=
0.0
,
max
=
1.0
)
...
...
@@ -255,7 +257,7 @@ class VAE:
free_memory
=
model_management
.
get_free_memory
(
self
.
device
)
batch_number
=
int
(
free_memory
/
memory_used
)
batch_number
=
max
(
1
,
batch_number
)
samples
=
torch
.
empty
((
pixel_samples
.
shape
[
0
],
4
,
round
(
pixel_samples
.
shape
[
2
]
//
8
),
round
(
pixel_samples
.
shape
[
3
]
//
8
)),
device
=
self
.
output_device
)
samples
=
torch
.
empty
((
pixel_samples
.
shape
[
0
],
self
.
latent_channels
,
round
(
pixel_samples
.
shape
[
2
]
//
self
.
downscale_ratio
),
round
(
pixel_samples
.
shape
[
3
]
//
self
.
downscale_ratio
)),
device
=
self
.
output_device
)
for
x
in
range
(
0
,
pixel_samples
.
shape
[
0
],
batch_number
):
pixels_in
=
(
2.
*
pixel_samples
[
x
:
x
+
batch_number
]
-
1.
).
to
(
self
.
vae_dtype
).
to
(
self
.
device
)
samples
[
x
:
x
+
batch_number
]
=
self
.
first_stage_model
.
encode
(
pixels_in
).
to
(
self
.
output_device
).
float
()
...
...
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