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
a84cd0d1
Commit
a84cd0d1
authored
Feb 08, 2023
by
comfyanonymous
Browse files
Don't unload/reload model from CPU uselessly.
parent
e3e65947
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
62 additions
and
36 deletions
+62
-36
comfy/model_management.py
comfy/model_management.py
+26
-0
comfy/sd.py
comfy/sd.py
+3
-0
nodes.py
nodes.py
+33
-36
No files found.
comfy/model_management.py
0 → 100644
View file @
a84cd0d1
current_loaded_model
=
None
def
unload_model
():
global
current_loaded_model
if
current_loaded_model
is
not
None
:
current_loaded_model
.
model
.
cpu
()
current_loaded_model
.
unpatch_model
()
current_loaded_model
=
None
def
load_model_gpu
(
model
):
global
current_loaded_model
if
model
is
current_loaded_model
:
return
unload_model
()
try
:
real_model
=
model
.
patch_model
()
except
Exception
as
e
:
model
.
unpatch_model
()
raise
e
current_loaded_model
=
model
real_model
.
cuda
()
return
current_loaded_model
comfy/sd.py
View file @
a84cd0d1
...
@@ -2,6 +2,7 @@ import torch
...
@@ -2,6 +2,7 @@ import torch
import
sd1_clip
import
sd1_clip
import
sd2_clip
import
sd2_clip
import
model_management
from
ldm.util
import
instantiate_from_config
from
ldm.util
import
instantiate_from_config
from
ldm.models.autoencoder
import
AutoencoderKL
from
ldm.models.autoencoder
import
AutoencoderKL
from
omegaconf
import
OmegaConf
from
omegaconf
import
OmegaConf
...
@@ -304,6 +305,7 @@ class VAE:
...
@@ -304,6 +305,7 @@ class VAE:
self
.
device
=
device
self
.
device
=
device
def
decode
(
self
,
samples
):
def
decode
(
self
,
samples
):
model_management
.
unload_model
()
self
.
first_stage_model
=
self
.
first_stage_model
.
to
(
self
.
device
)
self
.
first_stage_model
=
self
.
first_stage_model
.
to
(
self
.
device
)
samples
=
samples
.
to
(
self
.
device
)
samples
=
samples
.
to
(
self
.
device
)
pixel_samples
=
self
.
first_stage_model
.
decode
(
1.
/
self
.
scale_factor
*
samples
)
pixel_samples
=
self
.
first_stage_model
.
decode
(
1.
/
self
.
scale_factor
*
samples
)
...
@@ -313,6 +315,7 @@ class VAE:
...
@@ -313,6 +315,7 @@ class VAE:
return
pixel_samples
return
pixel_samples
def
encode
(
self
,
pixel_samples
):
def
encode
(
self
,
pixel_samples
):
model_management
.
unload_model
()
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
)
samples
=
self
.
first_stage_model
.
encode
(
2.
*
pixel_samples
-
1.
).
sample
()
*
self
.
scale_factor
samples
=
self
.
first_stage_model
.
encode
(
2.
*
pixel_samples
-
1.
).
sample
()
*
self
.
scale_factor
...
...
nodes.py
View file @
a84cd0d1
...
@@ -15,6 +15,7 @@ sys.path.append(os.path.join(sys.path[0], "comfy"))
...
@@ -15,6 +15,7 @@ sys.path.append(os.path.join(sys.path[0], "comfy"))
import
comfy.samplers
import
comfy.samplers
import
comfy.sd
import
comfy.sd
import
model_management
supported_ckpt_extensions
=
[
'.ckpt'
]
supported_ckpt_extensions
=
[
'.ckpt'
]
supported_pt_extensions
=
[
'.ckpt'
,
'.pt'
,
'.bin'
]
supported_pt_extensions
=
[
'.ckpt'
,
'.pt'
,
'.bin'
]
...
@@ -353,9 +354,12 @@ def common_ksampler(device, model, seed, steps, cfg, sampler_name, scheduler, po
...
@@ -353,9 +354,12 @@ def common_ksampler(device, model, seed, steps, cfg, sampler_name, scheduler, po
noise
=
torch
.
randn
(
latent_image
.
size
(),
dtype
=
latent_image
.
dtype
,
layout
=
latent_image
.
layout
,
generator
=
torch
.
manual_seed
(
seed
),
device
=
"cpu"
)
noise
=
torch
.
randn
(
latent_image
.
size
(),
dtype
=
latent_image
.
dtype
,
layout
=
latent_image
.
layout
,
generator
=
torch
.
manual_seed
(
seed
),
device
=
"cpu"
)
real_model
=
None
real_model
=
None
try
:
if
device
!=
"cpu"
:
model_management
.
load_model_gpu
(
model
)
real_model
=
model
.
model
else
:
#TODO: cpu support
real_model
=
model
.
patch_model
()
real_model
=
model
.
patch_model
()
real_model
.
to
(
device
)
noise
=
noise
.
to
(
device
)
noise
=
noise
.
to
(
device
)
latent_image
=
latent_image
.
to
(
device
)
latent_image
=
latent_image
.
to
(
device
)
...
@@ -383,13 +387,6 @@ def common_ksampler(device, model, seed, steps, cfg, sampler_name, scheduler, po
...
@@ -383,13 +387,6 @@ def common_ksampler(device, model, seed, steps, cfg, sampler_name, scheduler, po
samples
=
sampler
.
sample
(
noise
,
positive_copy
,
negative_copy
,
cfg
=
cfg
,
latent_image
=
latent_image
,
start_step
=
start_step
,
last_step
=
last_step
,
force_full_denoise
=
force_full_denoise
)
samples
=
sampler
.
sample
(
noise
,
positive_copy
,
negative_copy
,
cfg
=
cfg
,
latent_image
=
latent_image
,
start_step
=
start_step
,
last_step
=
last_step
,
force_full_denoise
=
force_full_denoise
)
samples
=
samples
.
cpu
()
samples
=
samples
.
cpu
()
real_model
.
cpu
()
model
.
unpatch_model
()
except
Exception
as
e
:
if
real_model
is
not
None
:
real_model
.
cpu
()
model
.
unpatch_model
()
raise
e
return
(
samples
,
)
return
(
samples
,
)
...
...
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