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

Function save

imperative/python/megengine/serialization.py:9–69  ·  view source on GitHub ↗

r"""Save an object to disk file. The saved object must be a :class:`~.module.Module`, :attr:`.Module.state_dict` or :attr:`.Optimizer.state_dict`. See :ref:`serialization-guide` for more details. Args: obj: object to be saved. f: a string of file name or a text file

(obj, f, pickle_module=pickle, pickle_protocol=pickle.DEFAULT_PROTOCOL)

Source from the content-addressed store, hash-verified

7
8
9def save(obj, f, pickle_module=pickle, pickle_protocol=pickle.DEFAULT_PROTOCOL):
10 r"""Save an object to disk file.
11 The saved object must be a :class:`~.module.Module`,
12 :attr:`.Module.state_dict` or :attr:`.Optimizer.state_dict`.
13 See :ref:`serialization-guide` for more details.
14
15 Args:
16 obj: object to be saved.
17 f: a string of file name or a text file object to which ``obj`` is saved to.
18 pickle_module: the module to use for pickling.
19 pickle_protocol: the protocol to use for pickling.
20
21 Return:
22 None.
23
24 .. admonition:: If you are using MegEngine with different Python versions
25 :class: warning
26
27 Different Python version may use different DEFAULT/HIGHEST pickle protocol.
28 If you want to :func:`~megengine.load` the saved object in another Python version,
29 please make sure you have used the same protocol.
30
31 .. admonition:: You can select to use ``pickle`` module directly
32
33 This interface is a wrapper of :func:`pickle.dump`. If you want to use ``pickle``,
34 See :py:mod:`pickle` for more information about how to set ``pickle_protocol``:
35
36 * :py:data:`pickle.HIGHEST_PROTOCOL` - the highest protocol version available.
37 * :py:data:`pickle.DEFAULT_PROTOCOL` - the default protocol version used for pickling.
38
39 Examples:
40
41 If you want to save object in a higher protocol version which current version Python
42 not support, you can install other pickle module instead of the build-in one.
43 Take ``pickle5`` as an example:
44
45 >>> import pickle5 as pickle # doctest: +SKIP
46
47 It's a backport of the pickle 5 protocol (PEP 574) and other pickle changes.
48 So you can use it to save object in pickle 5 protocol and load it in Python 3.8+.
49
50 Or you can use ``pickle5`` in this way (only used with this interface):
51
52 .. code-block:: python
53
54 import pickle5
55 import megengine
56
57 megengine.save(obj, f, pickle_module=pickle5, pickle_protocol=5)
58
59 """
60 if isinstance(f, str):
61 with open(f, "wb") as fout:
62 save(
63 obj, fout, pickle_module=pickle_module, pickle_protocol=pickle_protocol
64 )
65 return
66

Callers 9

metaMethod · 0.85
saveMethod · 0.85
saveMethod · 0.85
saveMethod · 0.85
saveMethod · 0.85
saveMethod · 0.85
saveMethod · 0.85
saveMethod · 0.85
Vector<float, 8>Class · 0.85

Calls 3

max_recursion_limitFunction · 0.90
formatMethod · 0.45
dumpMethod · 0.45

Tested by

no test coverage detected