The most powerful and modular diffusion model GUI, api and backend with a graph/nodes interface. The fastest local inference engine in the world.
Add attempt to work around the safetensors mmap issue. (#8928)
comfyanonymous50afba747cd3
diff --git a/comfy/cli_args.py b/comfy/cli_args.pyindex 7234a7ba0..ef0d4337e 100644--- a/comfy/cli_args.py+++ b/comfy/cli_args.py@@ -144,6 +144,7 @@ class PerformanceFeature(enum.Enum):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")parser.add_argument("--mmap-torch-files", action="store_true", help="Use mmap when loading ckpt/pt files.")+parser.add_argument("--disable-mmap", action="store_true", help="Don't use mmap when loading safetensors.")parser.add_argument("--dont-print-server", action="store_true", help="Don't print server output.")parser.add_argument("--quick-test-for-ci", action="store_true", help="Quick test for CI.")diff --git a/comfy/utils.py b/comfy/utils.pyindex f8e01f713..9c076a0e0 100644--- a/comfy/utils.py+++ b/comfy/utils.py@@ -31,6 +31,7 @@ from einops import rearrangefrom comfy.cli_args import argsMMAP_TORCH_FILES = args.mmap_torch_files+DISABLE_MMAP = args.disable_mmapALWAYS_SAFE_LOAD = Falseif 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@@ -58,7 +59,10 @@ def load_torch_file(ckpt, safe_load=False, device=None, return_metadata=False):with safetensors.safe_open(ckpt, framework="pt", device=device.type) as f:sd = {}for k in f.keys():- sd[k] = f.get_tensor(k)+ tensor = f.get_tensor(k)+ if DISABLE_MMAP: # TODO: Not sure if this is the best way to bypass the mmap issues+ tensor = tensor.to(device=device, copy=True)+ sd[k] = tensorif return_metadata:metadata = f.metadata()except Exception as e: