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
27df7410
Commit
27df7410
authored
May 03, 2023
by
pythongosssss
Browse files
reduce duplication
parent
06ad35b4
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
11 additions
and
9 deletions
+11
-9
comfy/sd.py
comfy/sd.py
+5
-9
comfy/utils.py
comfy/utils.py
+6
-0
No files found.
comfy/sd.py
View file @
27df7410
...
@@ -438,10 +438,8 @@ class VAE:
...
@@ -438,10 +438,8 @@ class VAE:
self
.
device
=
device
self
.
device
=
device
def
decode_tiled_
(
self
,
samples
,
tile_x
=
64
,
tile_y
=
64
,
overlap
=
16
):
def
decode_tiled_
(
self
,
samples
,
tile_x
=
64
,
tile_y
=
64
,
overlap
=
16
):
it_1
=
-
(
samples
.
shape
[
2
]
//
-
(
tile_y
*
2
-
overlap
))
*
-
(
samples
.
shape
[
3
]
//
-
(
tile_x
//
2
-
overlap
))
steps
=
samples
.
shape
[
0
]
*
utils
.
get_tiled_scale_steps
(
samples
.
shape
[
3
],
samples
.
shape
[
2
],
tile_x
,
tile_y
,
overlap
)
it_2
=
-
(
samples
.
shape
[
2
]
//
-
(
tile_y
//
2
-
overlap
))
*
-
(
samples
.
shape
[
3
]
//
-
(
tile_x
*
2
-
overlap
))
pbar
=
tqdm
(
total
=
steps
)
it_3
=
-
(
samples
.
shape
[
2
]
//
-
(
tile_y
-
overlap
))
*
-
(
samples
.
shape
[
3
]
//
-
(
tile_x
-
overlap
))
pbar
=
tqdm
(
total
=
samples
.
shape
[
0
]
*
(
it_1
+
it_2
+
it_3
))
decode_fn
=
lambda
a
:
(
self
.
first_stage_model
.
decode
(
1.
/
self
.
scale_factor
*
a
.
to
(
self
.
device
))
+
1.0
)
decode_fn
=
lambda
a
:
(
self
.
first_stage_model
.
decode
(
1.
/
self
.
scale_factor
*
a
.
to
(
self
.
device
))
+
1.0
)
output
=
torch
.
clamp
((
output
=
torch
.
clamp
((
...
@@ -492,11 +490,9 @@ class VAE:
...
@@ -492,11 +490,9 @@ class VAE:
self
.
first_stage_model
=
self
.
first_stage_model
.
to
(
self
.
device
)
self
.
first_stage_model
=
self
.
first_stage_model
.
to
(
self
.
device
)
pixel_samples
=
pixel_samples
.
movedim
(
-
1
,
1
).
to
(
self
.
device
)
pixel_samples
=
pixel_samples
.
movedim
(
-
1
,
1
).
to
(
self
.
device
)
it_1
=
-
(
pixel_samples
.
shape
[
2
]
//
-
(
tile_y
*
2
-
overlap
))
*
-
(
pixel_samples
.
shape
[
3
]
//
-
(
tile_x
//
2
-
overlap
))
steps
=
utils
.
get_tiled_scale_steps
(
pixel_samples
.
shape
[
3
],
pixel_samples
.
shape
[
2
],
tile_x
,
tile_y
,
overlap
)
it_2
=
-
(
pixel_samples
.
shape
[
2
]
//
-
(
tile_y
//
2
-
overlap
))
*
-
(
pixel_samples
.
shape
[
3
]
//
-
(
tile_x
*
2
-
overlap
))
pbar
=
tqdm
(
total
=
steps
)
it_3
=
-
(
pixel_samples
.
shape
[
2
]
//
-
(
tile_y
-
overlap
))
*
-
(
pixel_samples
.
shape
[
3
]
//
-
(
tile_x
-
overlap
))
pbar
=
tqdm
(
total
=
(
it_1
+
it_2
+
it_3
))
samples
=
utils
.
tiled_scale
(
pixel_samples
,
lambda
a
:
self
.
first_stage_model
.
encode
(
2.
*
a
-
1.
).
sample
()
*
self
.
scale_factor
,
tile_x
,
tile_y
,
overlap
,
upscale_amount
=
(
1
/
8
),
out_channels
=
4
,
pbar
=
pbar
)
samples
=
utils
.
tiled_scale
(
pixel_samples
,
lambda
a
:
self
.
first_stage_model
.
encode
(
2.
*
a
-
1.
).
sample
()
*
self
.
scale_factor
,
tile_x
,
tile_y
,
overlap
,
upscale_amount
=
(
1
/
8
),
out_channels
=
4
,
pbar
=
pbar
)
samples
+=
utils
.
tiled_scale
(
pixel_samples
,
lambda
a
:
self
.
first_stage_model
.
encode
(
2.
*
a
-
1.
).
sample
()
*
self
.
scale_factor
,
tile_x
*
2
,
tile_y
//
2
,
overlap
,
upscale_amount
=
(
1
/
8
),
out_channels
=
4
,
pbar
=
pbar
)
samples
+=
utils
.
tiled_scale
(
pixel_samples
,
lambda
a
:
self
.
first_stage_model
.
encode
(
2.
*
a
-
1.
).
sample
()
*
self
.
scale_factor
,
tile_x
*
2
,
tile_y
//
2
,
overlap
,
upscale_amount
=
(
1
/
8
),
out_channels
=
4
,
pbar
=
pbar
)
samples
+=
utils
.
tiled_scale
(
pixel_samples
,
lambda
a
:
self
.
first_stage_model
.
encode
(
2.
*
a
-
1.
).
sample
()
*
self
.
scale_factor
,
tile_x
//
2
,
tile_y
*
2
,
overlap
,
upscale_amount
=
(
1
/
8
),
out_channels
=
4
,
pbar
=
pbar
)
samples
+=
utils
.
tiled_scale
(
pixel_samples
,
lambda
a
:
self
.
first_stage_model
.
encode
(
2.
*
a
-
1.
).
sample
()
*
self
.
scale_factor
,
tile_x
//
2
,
tile_y
*
2
,
overlap
,
upscale_amount
=
(
1
/
8
),
out_channels
=
4
,
pbar
=
pbar
)
...
...
comfy/utils.py
View file @
27df7410
...
@@ -62,6 +62,12 @@ def common_upscale(samples, width, height, upscale_method, crop):
...
@@ -62,6 +62,12 @@ def common_upscale(samples, width, height, upscale_method, crop):
s
=
samples
s
=
samples
return
torch
.
nn
.
functional
.
interpolate
(
s
,
size
=
(
height
,
width
),
mode
=
upscale_method
)
return
torch
.
nn
.
functional
.
interpolate
(
s
,
size
=
(
height
,
width
),
mode
=
upscale_method
)
def
get_tiled_scale_steps
(
width
,
height
,
tile_x
,
tile_y
,
overlap
):
it_1
=
-
(
height
//
-
(
tile_y
*
2
-
overlap
))
*
-
(
width
//
-
(
tile_x
//
2
-
overlap
))
it_2
=
-
(
height
//
-
(
tile_y
//
2
-
overlap
))
*
-
(
width
//
-
(
tile_x
*
2
-
overlap
))
it_3
=
-
(
height
//
-
(
tile_y
-
overlap
))
*
-
(
width
//
-
(
tile_x
-
overlap
))
return
it_1
+
it_2
+
it_3
@
torch
.
inference_mode
()
@
torch
.
inference_mode
()
def
tiled_scale
(
samples
,
function
,
tile_x
=
64
,
tile_y
=
64
,
overlap
=
8
,
upscale_amount
=
4
,
out_channels
=
3
,
pbar
=
None
):
def
tiled_scale
(
samples
,
function
,
tile_x
=
64
,
tile_y
=
64
,
overlap
=
8
,
upscale_amount
=
4
,
out_channels
=
3
,
pbar
=
None
):
output
=
torch
.
empty
((
samples
.
shape
[
0
],
out_channels
,
round
(
samples
.
shape
[
2
]
*
upscale_amount
),
round
(
samples
.
shape
[
3
]
*
upscale_amount
)),
device
=
"cpu"
)
output
=
torch
.
empty
((
samples
.
shape
[
0
],
out_channels
,
round
(
samples
.
shape
[
2
]
*
upscale_amount
),
round
(
samples
.
shape
[
3
]
*
upscale_amount
)),
device
=
"cpu"
)
...
...
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