megengine.save¶
- save(obj, f, pickle_module=pickle, pickle_protocol=pickle.DEFAULT_PROTOCOL)[source]¶
Save an object to disk file. The saved object must be a
Module,Module.state_dictorOptimizer.state_dict. See Save and Load Models (S&L) for more details.- Parameters
obj – object to be saved.
f – a string of file name or a text file object to which
objis saved to.pickle_module – the module to use for pickling.
pickle_protocol – the protocol to use for pickling.
- Returns
None.
If you are using MegEngine with different Python versions
Different Python version may use different DEFAULT/HIGHEST pickle protocol. If you want to
loadthe saved object in another Python version, please make sure you have used the same protocol.You can select to use
picklemodule directlyThis interface is a wrapper of
pickle.dump. If you want to usepickle, Seepicklefor more information about how to setpickle_protocol:pickle.HIGHEST_PROTOCOL- the highest protocol version available.pickle.DEFAULT_PROTOCOL- the default protocol version used for pickling.
Examples
If you want to save object in a higher protocol version which current version Python not support, you can install other pickle module instead of the build-in one. Take
pickle5as an example:>>> import pickle5 as pickle
It’s a backport of the pickle 5 protocol (PEP 574) and other pickle changes. So you can use it to save object in pickle 5 protocol and load it in Python 3.8+.
Or you can use
pickle5in this way (only used with this interface):import pickle5 import megengine megengine.save(obj, f, pickle_module=pickle5, pickle_protocol=5)