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

Function save

torch/serialization.py:575–633  ·  view source on GitHub ↗

save(obj, f, pickle_module=pickle, pickle_protocol=DEFAULT_PROTOCOL, _use_new_zipfile_serialization=True) Saves an object to a disk file. See also: :ref:`saving-loading-tensors` Args: obj: saved object f: a file-like object (has to implement write and flush) or a strin

(
    obj: object,
    f: FILE_LIKE,
    pickle_module: Any = pickle,
    pickle_protocol: int = DEFAULT_PROTOCOL,
    _use_new_zipfile_serialization: bool = True,
    _disable_byteorder_record: bool = False
)

Source from the content-addressed store, hash-verified

573
574
575def save(
576 obj: object,
577 f: FILE_LIKE,
578 pickle_module: Any = pickle,
579 pickle_protocol: int = DEFAULT_PROTOCOL,
580 _use_new_zipfile_serialization: bool = True,
581 _disable_byteorder_record: bool = False
582) -> None:
583 # Reference: https://github.com/pytorch/pytorch/issues/54354
584 # The first line of this docstring overrides the one Sphinx generates for the
585 # documentation. We need it so that Sphinx doesn't leak `pickle`s path from
586 # the build environment (e.g. `<module 'pickle' from '/leaked/path').
587
588 """save(obj, f, pickle_module=pickle, pickle_protocol=DEFAULT_PROTOCOL, _use_new_zipfile_serialization=True)
589
590 Saves an object to a disk file.
591
592 See also: :ref:`saving-loading-tensors`
593
594 Args:
595 obj: saved object
596 f: a file-like object (has to implement write and flush) or a string or
597 os.PathLike object containing a file name
598 pickle_module: module used for pickling metadata and objects
599 pickle_protocol: can be specified to override the default protocol
600
601 .. note::
602 A common PyTorch convention is to save tensors using .pt file extension.
603
604 .. note::
605 PyTorch preserves storage sharing across serialization. See
606 :ref:`preserve-storage-sharing` for more details.
607
608 .. note::
609 The 1.6 release of PyTorch switched ``torch.save`` to use a new
610 zipfile-based file format. ``torch.load`` still retains the ability to
611 load files in the old format. If for any reason you want ``torch.save``
612 to use the old format, pass the kwarg ``_use_new_zipfile_serialization=False``.
613
614 Example:
615 >>> # xdoctest: +SKIP("makes cwd dirty")
616 >>> # Save to file
617 >>> x = torch.tensor([0, 1, 2, 3, 4])
618 >>> torch.save(x, 'tensor.pt')
619 >>> # Save to io.BytesIO buffer
620 >>> buffer = io.BytesIO()
621 >>> torch.save(x, buffer)
622 """
623 torch._C._log_api_usage_once("torch.save")
624 _check_dill_version(pickle_module)
625 _check_save_filelike(f)
626
627 if _use_new_zipfile_serialization:
628 with _open_zipfile_writer(f) as opened_zipfile:
629 _save(obj, opened_zipfile, pickle_module, pickle_protocol, _disable_byteorder_record)
630 return
631 else:
632 with _open_file_like(f, 'wb') as opened_file:

Callers

nothing calls this directly

Calls 6

_check_dill_versionFunction · 0.85
_check_save_filelikeFunction · 0.85
_open_zipfile_writerFunction · 0.85
_saveFunction · 0.85
_open_file_likeFunction · 0.85
_legacy_saveFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…