Shortcuts

torch.package

Warning

This module is experimental and has not yet been publicly released.

API Reference

class torch.package.DeniedModuleError[source]

This is an exception that is thrown when a pattern added with deny matches a module required during the packaging process.

class torch.package.EmptyMatchError[source]

This is an exception that is thrown when a mock or extern is marked as allow_empty=False, and is not matched with any module during packaging.

class torch.package.PackageExporter(f, importer=<torch.package.importer._SysImporter object>, verbose=True)[source]

Exporters allow you to write packages of code, pickled Python data, and arbitrary binary and text resources into a self-contained package.

Imports 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 extern(). 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.

When source code is added to the package, the exporter optionally can scan it for further code dependencies (dependencies=True). It looks for import statements, resolves relative references to qualified module names, and calls require_module() on each it finds, recursively resolving dependencies.

__init__(f, importer=<torch.package.importer._SysImporter object>, verbose=True)[source]

Create an exporter.

Parameters
  • f – The location to export to. Can be a string/Path object containing a filename, or a binary I/O object.

  • importer – If a single Importer is passed, use that to search for modules. If a sequence of importers are passsed, an OrderedImporter will be constructed out of them.

  • verbose – Print information about dependency resolution to stdout. Useful for tracking down why certain files get included.

close()[source]

Write the package to the filesystem. Any calls after close() are now invalid. It is preferable to use resource guard syntax instead:

with PackageExporter("file.zip") as e:
    ...
deny(include, *, exclude=())[source]

Blocklist modules who names match the given glob patterns from the list of modules the package can import. If a dependency on any matching packages is found, a DeniedModuleError is thrown.

Parameters
  • include (Union[List[str], str]) – A string e.g. “my_package.my_subpackage”, or list of strings for the names of the modules to be externed. This can also be a glob-style pattern, as described in mock()

  • exclude (Union[List[str], str]) – An optional pattern that excludes some patterns that match the include string.

extern(include, *, exclude=(), allow_empty=True)[source]

Include module in the list of external modules the package can import. This will prevent dependency discovery from saving it in the package. The importer will load an external module directly from the standard import system. Code for extern modules must also exist in the process loading the package.

Parameters
  • include (Union[List[str], str]) – A string e.g. “my_package.my_subpackage”, or list of strings for the names of the modules to be externed. This can also be a glob-style pattern, as described in mock()

  • exclude (Union[List[str], str]) – An optional pattern that excludes some patterns that match the include string.

  • allow_empty (bool) – An optional flag that specifies whether the extern modules specified by this call to the extern method must be matched to some module during packaging. If an extern module glob pattern is added with allow_empty=False, and close is called (either explicitly or via __exit__) before any modules match that pattern, an exception is thrown. If allow_empty=True, no such exception is thrown.

file_structure(*, include='**', exclude=())[source]

Creates and returns a Directory file structure representation of package’s zipfile.

Parameters
  • include (Union[List[str], str]) – An optional string e.g. “my_package.my_subpackage”, or optional list of strings for the names of the files to be inluded in the zipfile representation. This can also be a glob-style pattern, as described in mock()

  • exclude (Union[List[str], str]) – An optional pattern that excludes files whose name match the pattern.

Returns

Directory

get_unique_id()[source]

Get an id. This id is guaranteed to only be handed out once for this package.

mock(include, *, exclude=(), allow_empty=True)[source]

Replace some required modules with a mock implementation. Mocked modules will return a fake object for any attribute accessed from it. Because we copy file-by-file, the dependency resolution will sometimes find files that are imported by model files but whose functionality is never used (e.g. custom serialization code or training helpers). Use this function to mock this functionality out without having to modify the original code.

Parameters
  • include (Union[List[str], str]) –

    A string e.g. “my_package.my_subpackage”, or list of strings for the names of the modules to be mocked out. Strings can also be a glob-style pattern string that may match multiple modules. Any required dependencies that match this pattern string will be mocked out automatically.

    Examples

    ’torch.**’ – matches torch and all submodules of torch, e.g. ‘torch.nn’ and torch.nn.functional’ ‘torch.*’ – matches ‘torch.nn’ or ‘torch.functional’, but not ‘torch.nn.functional’

  • exclude (Union[List[str], str]) – An optional pattern that excludes some patterns that match the include string. e.g. include=’torch.**’, exclude=’torch.foo’ will mock all torch packages except ‘torch.foo’ Default: []

  • allow_empty (bool) – An optional flag that specifies whether the mock implementation(s) specified by this call to the mock method must be matched to some module during packaging. If a mock is added with allow_empty=False, and close is called (either explicitly or via __exit__) and the mock has not been matched to a module used by the package being exported, an exception is thrown. If allow_empty=True, no such exception is thrown.

require_module(module_name, dependencies=True)[source]

This is called by dependencies resolution when it finds that something in the package depends on the module and it is not already present. It then decides how to provide that module. The default resolution rules will mark the module as extern if it is part of the standard library, and call save_module() otherwise. Clients can subclass this object and override this method to provide other behavior, such as automatically mocking out a whole class of modules

save_binary(package, resource, binary)[source]

Save raw bytes to the package.

Parameters
  • package (str) – The name of module package this resource should go it (e.g. “my_package.my_subpackage”)

  • resource (str) – A unique name for the resource, used to identify it to load.

  • binary (str) – The data to save.

save_extern_module(module_name)[source]

Add module_name to the list of external modules, regardless of whether it is required by other modules.

Prefer using extern() to only mark modules extern if they are actually required by the packaged code.

save_mock_module(module_name)[source]

Add module_name to the package, implemented it with a mocked out version that can be imported but does not include any implementations.

Prefer using mock to only include this module if it is required by other modules.

save_module(module, dependencies=True)[source]

Save the code for module into the package. Code for the module is resolved using the importers path to find the module object, and then using its __file__ attribute to find the source code.

Parameters
  • module (Union[str, types.ModuleType]) – e.g. my_package.my_subpackage, code will be saved to provide code for this package.

  • dependencies (bool, optional) – If True, we scan the source for dependencies.

save_pickle(package, resource, obj, dependencies=True)[source]

Save a python object to the archive using pickle. Equivalent to torch.save() but saving into the archive rather than a stand-alone file. Stanard pickle does not save the code, only the objects. If dependencies is true, this method will also scan the pickled objects for which modules are required to reconstruct them and save the relevant code.

To be able to save an object where type(obj).__name__ is my_module.MyObject, my_module.MyObject must resolve to the class of the object according to the importer order. When saving objects that have previously been packaged, the importer’s import_module method will need to be present in the importer list for this to work.

Parameters
  • package (str) – The name of module package this resource should go in (e.g. “my_package.my_subpackage”)

  • resource (str) – A unique name for the resource, used to identify it to load.

  • obj (Any) – The object to save, must be picklable.

  • dependencies (bool, optional) – If True, we scan the source for dependencies.

save_source_file(module_name, file_or_directory, dependencies=True)[source]

Adds the local file system file_or_directory to the source package to provide the code for module_name.

Parameters
  • module_name (str) – e.g. my_package.my_subpackage, code will be saved to provide code for this package.

  • file_or_directory (str) – the path to a file or directory of code. When a directory, all python files in the directory are recursively copied using save_source_file(). If a file is named “/__init__.py” the code is treated as a package.

  • dependencies (bool, optional) – If True, we scan the source for dependencies.

save_source_string(module_name, src, is_package=False, dependencies=True, orig_file_name=None)[source]

Adds src as the source code for module_name in the exported package.

Parameters
  • module_name (str) – e.g. my_package.my_subpackage, code will be saved to provide code for this package.

  • src (str) – The Python source code to save for this package.

  • is_package (bool, optional) – If True, this module is treated as a package. Packages are allowed to have submodules (e.g. my_package.my_subpackage.my_subsubpackage), and resources can be saved inside them. Defaults to False.

  • dependencies (bool, optional) – If True, we scan the source for dependencies.

  • orig_file_name (str, optional) – If present, used in logging to identifying where the source came from. Defaults to None.

save_text(package, resource, text)[source]

Save text data to the package.

Parameters
  • package (str) – The name of module package this resource should go it (e.g. “my_package.my_subpackage”)

  • resource (str) – A unique name for the resource, used to identify it to load.

  • text (str) – The contents to save.

class torch.package.PackageImporter(file_or_buffer, module_allowed=<function PackageImporter.<lambda>>)[source]

Importers allow you to load code written to packages by PackageExporter. Code is loaded in a hermetic way, using files 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 importer for packages ensures that code in the module can only be loaded from within the package, except for modules explicitly listed as external during export. 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.

__init__(file_or_buffer, module_allowed=<function PackageImporter.<lambda>>)[source]

Open file_or_buffer for importing. This checks that the imported package only requires modules allowed by module_allowed

Parameters
  • file_or_buffer – a file-like object (has to implement read(), readline(), tell(), and seek()), or a string or os.PathLike object containing a file name.

  • module_allowed (Callable[[str], bool], optional) – A method to determine if a externally provided module should be allowed. Can be used to ensure packages loaded do not depend on modules that the server does not support. Defaults to allowing anything.

Raises

ImportError – If the package will use a disallowed module.

file_structure(*, include='**', exclude=())[source]

Returns a file structure representation of package’s zipfile.

Parameters
  • include (Union[List[str], str]) – An optional string e.g. “my_package.my_subpackage”, or optional list of strings for the names of the files to be inluded in the zipfile representation. This can also be a glob-style pattern, as described in PackageExporter.mock()

  • exclude (Union[List[str], str]) – An optional pattern that excludes files whose name match the pattern.

Returns

Directory

id()[source]

Returns internal identifier that torch.package uses to distinguish PackageImporter instances. Looks like:

<torch_package_0>
import_module(name, package=None)[source]

Load a module from the package if it hasn’t already been loaded, and then return the module. Modules are loaded locally to the importer and will appear in self.modules rather than sys.modules

Parameters
  • name (str) – Fully qualified name of the module to load.

  • package ([type], optional) – Unused, but present to match the signature of importlib.import_module. Defaults to None.

Returns

the (possibly already) loaded module.

Return type

types.ModuleType

load_binary(package, resource)[source]

Load raw bytes.

Parameters
  • package (str) – The name of module package (e.g. “my_package.my_subpackage”)

  • resource (str) – The unique name for the resource.

Returns

The loaded data.

Return type

bytes

load_pickle(package, resource, map_location=None)[source]

Unpickles the resource from the package, loading any modules that are needed to construct the objects using import_module()

Parameters
  • package (str) – The name of module package (e.g. “my_package.my_subpackage”)

  • resource (str) – The unique name for the resource.

  • map_location – Passed to torch.load to determine how tensors are mapped to devices. Defaults to None.

Returns

the unpickled object.

Return type

Any

load_text(package, resource, encoding='utf-8', errors='strict')[source]

Load a string.

Parameters
  • package (str) – The name of module package (e.g. “my_package.my_subpackage”)

  • resource (str) – The unique name for the resource.

  • encoding (str, optional) – Passed to decode. Defaults to ‘utf-8’.

  • errors (str, optional) – Passed to decode. Defaults to ‘strict’.

Returns

The loaded text.

Return type

str

class torch.package.Directory(name, is_dir)[source]

A file structure representation. Organized as Directory nodes that have lists of their Directory children. Directories for a package are created by calling PackageExporter.file_structure() or PackageImporter.file_structure().

has_file(filename)[source]

Checks if a file is present in a Directory.

Parameters

filename (str) – Path of file to search for.

Returns

if a Directory contains the specified file.

Return type

bool

Docs

Access comprehensive developer documentation for PyTorch

View Docs

Tutorials

Get in-depth tutorials for beginners and advanced developers

View Tutorials

Resources

Find development resources and get your questions answered

View Resources