MCPcopy Create free account
hub / github.com/pytorch/pytorch / load

Function load

torch/serialization.py:866–1040  ·  view source on GitHub ↗

load(f, map_location=None, pickle_module=pickle, *, weights_only=False, mmap=None, **pickle_load_args) Loads an object saved with :func:`torch.save` from a file. :func:`torch.load` uses Python's unpickling facilities but treats storages, which underlie tensors, specially. They are firs

(
    f: FILE_LIKE,
    map_location: MAP_LOCATION = None,
    pickle_module: Any = None,
    *,
    weights_only: bool = False,
    mmap: Optional[bool] = None,
    **pickle_load_args: Any
)

Source from the content-addressed store, hash-verified

864
865
866def load(
867 f: FILE_LIKE,
868 map_location: MAP_LOCATION = None,
869 pickle_module: Any = None,
870 *,
871 weights_only: bool = False,
872 mmap: Optional[bool] = None,
873 **pickle_load_args: Any
874) -> Any:
875 # Reference: https://github.com/pytorch/pytorch/issues/54354
876 # The first line of this docstring overrides the one Sphinx generates for the
877 # documentation. We need it so that Sphinx doesn't leak `pickle`s path from
878 # the build environment (e.g. `<module 'pickle' from '/leaked/path').
879
880 """load(f, map_location=None, pickle_module=pickle, *, weights_only=False, mmap=None, **pickle_load_args)
881
882 Loads an object saved with :func:`torch.save` from a file.
883
884 :func:`torch.load` uses Python&#x27;s unpickling facilities but treats storages,
885 which underlie tensors, specially. They are first deserialized on the
886 CPU and are then moved to the device they were saved from. If this fails
887 (e.g. because the run time system doesn&#x27;t have certain devices), an exception
888 is raised. However, storages can be dynamically remapped to an alternative
889 set of devices using the :attr:`map_location` argument.
890
891 If :attr:`map_location` is a callable, it will be called once for each serialized
892 storage with two arguments: storage and location. The storage argument
893 will be the initial deserialization of the storage, residing on the CPU.
894 Each serialized storage has a location tag associated with it which
895 identifies the device it was saved from, and this tag is the second
896 argument passed to :attr:`map_location`. The builtin location tags are ``'cpu'``
897 for CPU tensors and ``'cuda:device_id'`` (e.g. ``'cuda:2'``) for CUDA tensors.
898 :attr:`map_location` should return either ``None`` or a storage. If
899 :attr:`map_location` returns a storage, it will be used as the final deserialized
900 object, already moved to the right device. Otherwise, :func:`torch.load` will
901 fall back to the default behavior, as if :attr:`map_location` wasn&#x27;t specified.
902
903 If :attr:`map_location` is a :class:`torch.device` object or a string containing
904 a device tag, it indicates the location where all tensors should be loaded.
905
906 Otherwise, if :attr:`map_location` is a dict, it will be used to remap location tags
907 appearing in the file (keys), to ones that specify where to put the
908 storages (values).
909
910 User extensions can register their own location tags and tagging and
911 deserialization methods using :func:`torch.serialization.register_package`.
912
913 Args:
914 f: a file-like object (has to implement :meth:`read`, :meth:`readline`, :meth:`tell`, and :meth:`seek`),
915 or a string or os.PathLike object containing a file name
916 map_location: a function, :class:`torch.device`, string or a dict specifying how to remap storage
917 locations
918 pickle_module: module used for unpickling metadata and objects (has to
919 match the :attr:`pickle_module` used to serialize file)
920 weights_only: Indicates whether unpickler should be restricted to
921 loading only tensors, primitive types and dictionaries
922 mmap: Indicates whether the file should be mmaped rather than loading all the storages into memory.
923 Typically, tensor storages in the file will first be moved from disk to CPU memory, after which they

Callers

nothing calls this directly

Calls 15

_check_dill_versionFunction · 0.85
_open_file_likeFunction · 0.85
_is_zipfileFunction · 0.85
_is_torchscript_zipFunction · 0.85
isinstanceFunction · 0.85
_loadFunction · 0.85
_legacy_loadFunction · 0.85
tellMethod · 0.80
warnMethod · 0.80
seekMethod · 0.80
lowerMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…