mirror of
https://github.com/zebrajr/pytorch.git
synced 2025-12-07 12:21:27 +01:00
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/45015 torch.package allows you to write packages of code, pickled python data, and arbitrary binary and text resources into a self-contained package. torch.package.PackageExporter writes the packages and torch.package.PackageImporter reads them. The importers can load this code in a hermetic way, such that code is loaded from the package rather than the normal python import system. This allows for the packaging of PyTorch model code and data so that it can be run on a server or used in the future for transfer learning. The code contained in packages is copied file-by-file from the original source when it is created, and the file format is a specially organized zip file. Future users of the package can unzip the package, and edit the code in order to perform custom modifications to it. The importer for packages ensures that code in the module can only be loaded from within the package, except for modules explicitly listed as external using :method:`extern_module`. The file `extern_modules` in the zip archive lists all the modules that a package externally depends on. This prevents "implicit" dependencies where the package runs locally because it is importing a locally-installed package, but then fails when the package is copied to another machine. Test Plan: Imported from OSS Reviewed By: SplitInfinity Differential Revision: D23824337 Pulled By: zdevito fbshipit-source-id: 1247c34ba9b656f9db68a83e31f2a0fbe3bea6bd
40 lines
1.8 KiB
Python
40 lines
1.8 KiB
Python
|
|
_magic_methods = ['__subclasscheck__', '__hex__', '__rmul__',
|
|
'__float__', '__idiv__', '__setattr__', '__div__', '__invert__',
|
|
'__nonzero__', '__rshift__',
|
|
'__eq__', '__pos__', '__round__',
|
|
'__rand__', '__or__', '__complex__', '__divmod__',
|
|
'__len__', '__reversed__', '__copy__', '__reduce__',
|
|
'__deepcopy__', '__rdivmod__', '__rrshift__', '__ifloordiv__',
|
|
'__hash__', '__iand__', '__xor__', '__isub__', '__oct__',
|
|
'__ceil__', '__imod__', '__add__', '__truediv__',
|
|
'__unicode__', '__le__', '__delitem__', '__sizeof__', '__sub__',
|
|
'__ne__', '__pow__', '__bytes__', '__mul__',
|
|
'__itruediv__', '__bool__', '__iter__', '__abs__',
|
|
'__gt__', '__iadd__', '__enter__',
|
|
'__floordiv__', '__call__', '__neg__',
|
|
'__and__', '__ixor__', '__getitem__', '__exit__', '__cmp__',
|
|
'__getstate__', '__index__', '__contains__', '__floor__', '__lt__', '__getattr__',
|
|
'__mod__', '__trunc__', '__delattr__', '__instancecheck__', '__setitem__', '__ipow__',
|
|
'__ilshift__', '__long__', '__irshift__', '__imul__',
|
|
'__lshift__', '__dir__', '__ge__', '__int__', '__ior__']
|
|
|
|
|
|
class MockedObject:
|
|
_name: str
|
|
|
|
def __init__(self, name):
|
|
self.__dict__['_name'] = name
|
|
|
|
def __repr__(self):
|
|
return f"MockedObject({self._name})"
|
|
|
|
|
|
def install_method(method_name):
|
|
def _not_implemented(self, *args, **kwargs):
|
|
raise NotImplementedError(f"Object '{self._name}' was mocked out during packaging but it is being used in {method_name}")
|
|
setattr(MockedObject, method_name, _not_implemented)
|
|
|
|
for method_name in _magic_methods:
|
|
install_method(method_name)
|