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

Function load

imperative/python/megengine/serialization.py:114–187  ·  view source on GitHub ↗

r"""Load an object saved with :func:`~.megengine.save` from a file. Args: f: a string of file name or a text file object from which to load. map_location: defines device mapping. See examples for usage. pickle_module: the module to use for pickling. Return:

(f, map_location=None, pickle_module=pickle)

Source from the content-addressed store, hash-verified

112
113
114def load(f, map_location=None, pickle_module=pickle):
115 r"""Load an object saved with :func:`~.megengine.save` from a file.
116
117 Args:
118 f: a string of file name or a text file object from which to load.
119 map_location: defines device mapping. See examples for usage.
120 pickle_module: the module to use for pickling.
121
122 Return:
123 None.
124
125 Note:
126
127 If you will call :func:`~.megengine.set_default_device()`, please do it
128 before :func:`~.megengine.load()`.
129
130 .. admonition:: If you are using MegEngine with different Python versions
131 :class: warning
132
133 Different Python version may use different DEFAULT/HIGHEST pickle protocol.
134 If you want to :func:`~megengine.load` the saved object in another Python version,
135 please make sure you have used the same protocol.
136
137 .. admonition:: You can select to use ``pickle`` module directly
138
139 This interface is a wrapper of :func:`pickle.load`. If you want to use ``pickle``,
140 See :py:mod:`pickle` for more information about how to set ``pickle_protocol``:
141
142 * :py:data:`pickle.HIGHEST_PROTOCOL` - the highest protocol version available.
143 * :py:data:`pickle.DEFAULT_PROTOCOL` - the default protocol version used for pickling.
144
145 Examples:
146
147 This example shows how to load tenors to different devices:
148
149 .. code-block::
150
151 import megengine as mge
152
153 # Load tensors to the same device as defined in model.pkl
154 mge.load('model.pkl')
155
156 # Load all tensors to gpu0.
157 mge.load('model.pkl', map_location='gpu0')
158
159 # Load all tensors originally on gpu0 to cpu0
160 mge.load('model.pkl', map_location={'gpu0':'cpu0'})
161
162 # Load all tensors to cpu0
163 mge.load('model.pkl', map_location=lambda dev: 'cpu0')
164
165 If you are using a lower version of Python (<3.8),
166 you can use other pickle module like ``pickle5`` to load object saved in pickle 5 protocol:
167
168 >>> import pickle5 as pickle # doctest: +SKIP
169
170 Or you can use ``pickle5`` in this way (only used with this interface):
171

Callers

nothing calls this directly

Calls 3

dmapClass · 0.85
loadMethod · 0.45

Tested by

no test coverage detected