3232from tqdm import tqdm
3333
3434import torch
35- from library .device_utils import init_ipex , clean_memory
35+ from library .device_utils import init_ipex , clean_memory_on_device
3636
3737init_ipex ()
3838
@@ -2285,7 +2285,7 @@ def cache_batch_latents(
22852285 info .latents_flipped = flipped_latent
22862286
22872287 if not HIGH_VRAM :
2288- clean_memory ( )
2288+ clean_memory_on_device ( vae . device )
22892289
22902290
22912291def cache_batch_text_encoder_outputs (
@@ -4026,7 +4026,7 @@ def load_target_model(args, weight_dtype, accelerator, unet_use_linear_projectio
40264026 unet .to (accelerator .device )
40274027 vae .to (accelerator .device )
40284028
4029- clean_memory ( )
4029+ clean_memory_on_device ( accelerator . device )
40304030 accelerator .wait_for_everyone ()
40314031
40324032 return text_encoder , vae , unet , load_stable_diffusion_format
@@ -4695,7 +4695,7 @@ def sample_images_common(
46954695 distributed_state = PartialState () # for multi gpu distributed inference. this is a singleton, so it's safe to use it here
46964696
46974697 org_vae_device = vae .device # CPUにいるはず
4698- vae .to (distributed_state .device )
4698+ vae .to (distributed_state .device ) # distributed_state.device is same as accelerator.device
46994699
47004700 # unwrap unet and text_encoder(s)
47014701 unet = accelerator .unwrap_model (unet )
@@ -4752,7 +4752,11 @@ def sample_images_common(
47524752
47534753 # save random state to restore later
47544754 rng_state = torch .get_rng_state ()
4755- cuda_rng_state = torch .cuda .get_rng_state () if torch .cuda .is_available () else None # TODO mps etc. support
4755+ cuda_rng_state = None
4756+ try :
4757+ cuda_rng_state = torch .cuda .get_rng_state () if torch .cuda .is_available () else None
4758+ except Exception :
4759+ pass
47564760
47574761 if distributed_state .num_processes <= 1 :
47584762 # If only one device is available, just use the original prompt list. We don't need to care about the distribution of prompts.
@@ -4774,8 +4778,10 @@ def sample_images_common(
47744778 # clear pipeline and cache to reduce vram usage
47754779 del pipeline
47764780
4777- with torch .cuda .device (torch .cuda .current_device ()):
4778- torch .cuda .empty_cache ()
4781+ # I'm not sure which of these is the correct way to clear the memory, but accelerator's device is used in the pipeline, so I'm using it here.
4782+ # with torch.cuda.device(torch.cuda.current_device()):
4783+ # torch.cuda.empty_cache()
4784+ clean_memory_on_device (accelerator .device )
47794785
47804786 torch .set_rng_state (rng_state )
47814787 if cuda_rng_state is not None :
@@ -4870,10 +4876,6 @@ def sample_image_inference(accelerator: Accelerator, args: argparse.Namespace, p
48704876 pass
48714877# endregion
48724878
4873- # # clear pipeline and cache to reduce vram usage
4874- # del pipeline
4875- # torch.cuda.empty_cache()
4876-
48774879
48784880
48794881# region 前処理用
0 commit comments