MCPcopy
hub / github.com/kohya-ss/sd-scripts / clean_memory_on_device

Function clean_memory_on_device

library/device_utils.py:42–57  ·  view source on GitHub ↗

r""" Clean memory on the specified device, will be called from training scripts.

(device: Optional[Union[str, torch.device]])

Source from the content-addressed store, hash-verified

40
41
42def clean_memory_on_device(device: Optional[Union[str, torch.device]]):
43 r"""
44 Clean memory on the specified device, will be called from training scripts.
45 """
46 gc.collect()
47 if device is None:
48 return
49 if isinstance(device, str):
50 device = torch.device(device)
51 # device may "cuda" or "cuda:0", so we need to check the type of device
52 if device.type == "cuda":
53 torch.cuda.empty_cache()
54 if device.type == "xpu":
55 torch.xpu.empty_cache()
56 if device.type == "mps":
57 torch.mps.empty_cache()
58
59
60def synchronize_device(device: Optional[Union[str, torch.device]]):

Callers 15

trainFunction · 0.90
sample_imagesFunction · 0.90
sample_image_inferenceFunction · 0.90
load_unet_lazilyMethod · 0.90
trainMethod · 0.90
trainFunction · 0.90
mainFunction · 0.90
trainFunction · 0.90
load_dit_modelFunction · 0.90
prepare_text_inputsFunction · 0.90
process_batch_promptsFunction · 0.90

Calls 1

deviceMethod · 0.45

Tested by

no test coverage detected