Skip to content

Commit 8ab15c8

Browse files
Add --mmap-torch-files to enable use of mmap when loading ckpt/pt (#8021)
1 parent 924d771 commit 8ab15c8

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

comfy/cli_args.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,8 @@ class PerformanceFeature(enum.Enum):
142142

143143
parser.add_argument("--fast", nargs="*", type=PerformanceFeature, help="Enable some untested and potentially quality deteriorating optimizations. --fast with no arguments enables everything. You can pass a list specific optimizations if you only want to enable specific ones. Current valid optimizations: fp16_accumulation fp8_matrix_mult cublas_ops")
144144

145+
parser.add_argument("--mmap-torch-files", action="store_true", help="Use mmap when loading ckpt/pt files.")
146+
145147
parser.add_argument("--dont-print-server", action="store_true", help="Don't print server output.")
146148
parser.add_argument("--quick-test-for-ci", action="store_true", help="Quick test for CI.")
147149
parser.add_argument("--windows-standalone-build", action="store_true", help="Windows standalone build: Enable convenient things that most people using the standalone windows build will probably enjoy (like auto opening the page on startup).")

comfy/utils.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
import itertools
2929
from torch.nn.functional import interpolate
3030
from einops import rearrange
31+
from comfy.cli_args import args
32+
33+
MMAP_TORCH_FILES = args.mmap_torch_files
3134

3235
ALWAYS_SAFE_LOAD = False
3336
if hasattr(torch.serialization, "add_safe_globals"): # TODO: this was added in pytorch 2.4, the unsafe path should be removed once earlier versions are deprecated
@@ -67,8 +70,12 @@ def load_torch_file(ckpt, safe_load=False, device=None, return_metadata=False):
6770
raise ValueError("{}\n\nFile path: {}\n\nThe safetensors file is corrupt/incomplete. Check the file size and make sure you have copied/downloaded it correctly.".format(message, ckpt))
6871
raise e
6972
else:
73+
torch_args = {}
74+
if MMAP_TORCH_FILES:
75+
torch_args["mmap"] = True
76+
7077
if safe_load or ALWAYS_SAFE_LOAD:
71-
pl_sd = torch.load(ckpt, map_location=device, weights_only=True)
78+
pl_sd = torch.load(ckpt, map_location=device, weights_only=True, **torch_args)
7279
else:
7380
pl_sd = torch.load(ckpt, map_location=device, pickle_module=comfy.checkpoint_pickle)
7481
if "global_step" in pl_sd:

0 commit comments

Comments
 (0)