Preload model weights on CPU
Browse files97 unmodified lines9899100101102103711 unmodified lines81581681781881982082135 unmodified lines857858859860861862863864865866545 unmodified lines1412141314141415141614171418141914201421142214231424142514261427142814292 unmodified lines14321433143414351436143714381439144014411442144314441445144614471448144914501451145214531105 unmodified lines25592560256125622563256425652566256797 unmodified linesOUTPUT_DIR = DATA_DIR / "output"IMAGE_DIR = DATA_DIR / "img"TIMEZONE = ZoneInfo("Asia/Singapore")MIB = 1024 * 1024SALT_SIZE = 16NONCE_SIZE = 12711 unmodified linesargs.cpu_vae = Falsefrom nodes import (CLIPLoader,CLIPTextEncode,CheckpointLoaderSimple,ConditioningSetMask,35 unmodified linesapply_lora=comfy.sd.load_lora_for_models,chains={},checkpoint=CheckpointLoaderSimple(),clip_loader=CLIPLoader(),clips={},lora=LoraLoader(),load_torch_file=comfy.utils.load_torch_file,encode=CLIPTextEncode(),mask=ConditioningSetMask(),folders=folder_paths,545 unmodified linesreturn image, first_seed, second_seed, detailer_seeds@spaces.GPU(duration=GPU_DURATION)def load_model_gpu(name):if name in state["models"]:return state["models"][name]if is_anima_model(name):if ANIMA_CLIP not in state["clips"]:log(f"Loading text encoder {ANIMA_CLIP}")state["clips"][ANIMA_CLIP] = state["clip_loader"].load_clip(clip_name=ANIMA_CLIP,type="stable_diffusion",device="default",)[0]log(f"Loading diffusion model {name}")model = state["unet"].load_unet(unet_name=name,2 unmodified linesstate["models"][name] = model, state["clips"][ANIMA_CLIP]else:log(f"Loading checkpoint {name}")state["models"][name] = state["checkpoint"].load_checkpoint(ckpt_name=name)[:2]log(f"Loaded {name}")return state["models"][name]def load_model(name):if name in state["models"]:return state["models"][name]stage_model(model_kind(name), name)if is_anima_model(name):stage_model("clip", ANIMA_CLIP)return load_model_gpu(name)def load_lora(name):if name not in state["loras"]:stage_model("loras", name)1105 unmodified linesmodel_name = models[0]model, clip = load_chain(model_name, [])vae = load_vae(vae_name(model_name))cpu = torch.device("cpu")clip.patcher.load_device = cpuclip.patcher.offload_device = cpulatent = state["latent"].generate(width=PING_SIZE,height=PING_SIZE,97 unmodified lines9899100101102103104711 unmodified lines81681781881982082135 unmodified lines857858859860861862863864865866867868869545 unmodified lines14151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814392 unmodified lines144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671105 unmodified lines2573257425752576257725782579258097 unmodified linesOUTPUT_DIR = DATA_DIR / "output"IMAGE_DIR = DATA_DIR / "img"TIMEZONE = ZoneInfo("Asia/Singapore")CPU_DEVICE = torch.device("cpu")MIB = 1024 * 1024SALT_SIZE = 16NONCE_SIZE = 12711 unmodified linesargs.cpu_vae = Falsefrom nodes import (CLIPTextEncode,CheckpointLoaderSimple,ConditioningSetMask,35 unmodified linesapply_lora=comfy.sd.load_lora_for_models,chains={},checkpoint=CheckpointLoaderSimple(),clip_type=comfy.sd.CLIPType.STABLE_DIFFUSION,clips={},lora=LoraLoader(),load_checkpoint=comfy.sd.load_checkpoint_guess_config,load_clip=comfy.sd.load_clip,load_torch_file=comfy.utils.load_torch_file,model_management=comfy.sd.model_management,encode=CLIPTextEncode(),mask=ConditioningSetMask(),folders=folder_paths,545 unmodified linesreturn image, first_seed, second_seed, detailer_seedsdef load_model(name):if name in state["models"]:return state["models"][name]stage_model(model_kind(name), name)if is_anima_model(name):stage_model("clip", ANIMA_CLIP)if ANIMA_CLIP not in state["clips"]:log(f"Loading text encoder {ANIMA_CLIP}")path = state["folders"].get_full_path_or_raise(COMFY_KINDS["clip"], ANIMA_CLIP)state["clips"][ANIMA_CLIP] = state["load_clip"]([path],embedding_directory=state["folders"].get_folder_paths("embeddings"),clip_type=state["clip_type"],model_options={"initial_device": CPU_DEVICE},)log(f"Loading diffusion model {name}")model = state["unet"].load_unet(unet_name=name,2 unmodified linesstate["models"][name] = model, state["clips"][ANIMA_CLIP]else:log(f"Loading checkpoint {name}")path = state["folders"].get_full_path_or_raise("checkpoints", name)initial_device = state["model_management"].unet_inital_load_devicestate["model_management"].unet_inital_load_device = (lambda *_: CPU_DEVICE)try:state["models"][name] = state["load_checkpoint"](path,output_vae=False,embedding_directory=state["folders"].get_folder_paths("embeddings"),te_model_options={"initial_device": CPU_DEVICE},)[:2]finally:state["model_management"].unet_inital_load_device = initial_devicelog(f"Loaded {name}")return state["models"][name]def load_lora(name):if name not in state["loras"]:stage_model("loras", name)1105 unmodified linesmodel_name = models[0]model, clip = load_chain(model_name, [])vae = load_vae(vae_name(model_name))clip.patcher.load_device = CPU_DEVICEclip.patcher.offload_device = CPU_DEVICElatent = state["latent"].generate(width=PING_SIZE,height=PING_SIZE,
app.py+37-24