The most powerful and modular diffusion model GUI, api and backend with a graph/nodes interface. The fastest local inference engine in the world.
Remove unsafe pickle loading code that was used on pytorch older than 2.4 (#12473)
comfyanonymouse1ede29d827d
diff --git a/comfy/checkpoint_pickle.py b/comfy/checkpoint_pickle.pydeleted file mode 100644index 206551d3c..000000000--- a/comfy/checkpoint_pickle.py+++ /dev/null@@ -1,13 +0,0 @@-import pickle--load = pickle.load--class Empty:- pass--class Unpickler(pickle.Unpickler):- def find_class(self, module, name):- #TODO: safe unpickle- if module.startswith("pytorch_lightning"):- return Empty- return super().find_class(module, name)diff --git a/comfy/utils.py b/comfy/utils.pyindex d553a7c1b..c1ce540b5 100644--- a/comfy/utils.py+++ b/comfy/utils.py@@ -20,7 +20,7 @@import torchimport mathimport struct-import comfy.checkpoint_pickle+import comfy.memory_managementimport safetensors.torchimport numpy as npfrom PIL import Image@@ -38,26 +38,26 @@ import warningsMMAP_TORCH_FILES = args.mmap_torch_filesDISABLE_MMAP = args.disable_mmap-ALWAYS_SAFE_LOAD = False-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++if True: # ckpt/pt file whitelist for safe loading of old sd filesclass ModelCheckpoint:passModelCheckpoint.__module__ = "pytorch_lightning.callbacks.model_checkpoint"def scalar(*args, **kwargs):- from numpy.core.multiarray import scalar as sc- return sc(*args, **kwargs)+ return Nonescalar.__module__ = "numpy.core.multiarray"from numpy import dtypefrom numpy.dtypes import Float64DType- from _codecs import encode++ def encode(*args, **kwargs): # no longer necessary on newer torch+ return None+ encode.__module__ = "_codecs"torch.serialization.add_safe_globals([ModelCheckpoint, scalar, dtype, Float64DType, encode])- ALWAYS_SAFE_LOAD = Truelogging.info("Checkpoint files will always be loaded safely.")-else:- logging.warning("Warning, you are using an old pytorch version and some ckpt/pt files might be loaded unsafely. Upgrading to 2.4 or above is recommended as older versions of pytorch are no longer supported.")+# Current as of safetensors 0.7.0_TYPES = {@@ -140,11 +140,8 @@ def load_torch_file(ckpt, safe_load=False, device=None, return_metadata=False):if MMAP_TORCH_FILES:torch_args["mmap"] = True- if safe_load or ALWAYS_SAFE_LOAD:- pl_sd = torch.load(ckpt, map_location=device, weights_only=True, **torch_args)- else:- logging.warning("WARNING: loading {} unsafely, upgrade your pytorch to 2.4 or newer to load this file safely.".format(ckpt))- pl_sd = torch.load(ckpt, map_location=device, pickle_module=comfy.checkpoint_pickle)+ pl_sd = torch.load(ckpt, map_location=device, weights_only=True, **torch_args)+if "state_dict" in pl_sd:sd = pl_sd["state_dict"]else: