Skip to content
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

Adding command line option to move model sampling to cpu #6502

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions comfy/cli_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ def __call__(self, parser, namespace, values, option_string=None):
fpvae_group.add_argument("--bf16-vae", action="store_true", help="Run the VAE in bf16.")

parser.add_argument("--cpu-vae", action="store_true", help="Run the VAE on the CPU.")
parser.add_argument("--cpu-model-sampling", action="store_true", help="Run the model sampling on the CPU.")

fpte_group = parser.add_mutually_exclusive_group()
fpte_group.add_argument("--fp8_e4m3fn-text-enc", action="store_true", help="Store text encoder weights in fp8 (e4m3fn variant).")
Expand Down
5 changes: 5 additions & 0 deletions comfy/model_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,11 @@ def vae_device():
return torch.device("cpu")
return get_torch_device()

def model_sampling_device():
if args.cpu_model_sampling:
return torch.device("cpu")
return get_torch_device()

def vae_offload_device():
if args.gpu_only:
return get_torch_device()
Expand Down
4 changes: 4 additions & 0 deletions comfy/sd.py
Original file line number Diff line number Diff line change
Expand Up @@ -923,6 +923,10 @@ def load_state_dict_guess_config(sd, output_vae=True, output_clip=True, output_c
if inital_load_device != torch.device("cpu"):
logging.info("loaded diffusion model directly to GPU")
model_management.load_models_gpu([model_patcher], force_full_load=True)
#damcclos: move the model_sampling back to the CPU. The work needed for this is not worth the gpu.
model_sampling_device = model_management.model_sampling_device()
if model_sampling_device == torch.device("cpu"):
model_patcher.model.model_sampling.to(model_sampling_device)

return (model_patcher, clip, vae, clipvision)

Expand Down