torch.package adds support for creating packages containing both artifacts and arbitrary
PyTorch code. These packages can be saved, shared, used to load and execute models
at a later date or on a different machine, and can even be deployed to production using
torch::deploy.
This document contains tutorials, how-to guides, explanations, and an API reference that
will help you learn more about torch.package and how to use it.
pickle
It is possible to construct malicious pickle data which will execute arbitrary code during unpickling.
Never unpackage data that could have come from an untrusted source, or that could have been tampered with.
For more information, review the documentation for the pickle module.
Tutorials
Packaging your first model
A tutorial that guides you through packaging and unpackaging a simple model is available
on Colab.
After completing this exercise, you will be familiar with the basic API for creating and using
Torch packages.
How do I…
See what is inside a package?
Treat the package like a ZIP archive
The container format for a torch.package is ZIP, so any tools that work with standard ZIP files should
work for exploring the contents. Some common ways to interact with ZIP files:
unzipmy_package.pt will unzip the torch.package archive to disk, where you can freely inspect its contents.
The Python zipfile module provides a standard way to read and write ZIP archive contents.
fromzipfileimportZipFilewithZipFile("my_package.pt")asmyzip:file_bytes=myzip.read("torchvision/models/resnet.py")# edit file_bytes in some waymyzip.writestr("torchvision/models/resnet.py",new_file_bytes)
vim has the ability to natively read ZIP archives. You can even edit files and :write them back into the archive!
# add this to your .vimrc to treat `*.pt` filesas zip filesauBufReadCmd *.ptcall zip#Browse(expand("<amatch>"))~vi my_package.pt
Use the file_structure() API
PackageImporter provides a file_structure() method, which will return a printable
and queryable Directory object. The Directory object is a simple directory structure that you can use to explore the
current contents of a torch.package.
The Directory object itself is directly printable and will print out a file tree representation. To filter what is returned,
use the glob-style and filtering arguments.
See why a given module was included as a dependency?
Say there is a given module foo, and you want to know why your PackageExporter is pulling in foo as a dependency.
If you would like to see how a given module src depends on foo, the PackageExporter.all_paths() method will
return a DOT-formatted graph showing all the dependency paths between and .
Include arbitrary resources with my package and access them later?
PackageExporter exposes three methods, save_pickle, save_text and save_binary that allow you to save
Python objects, text, and binary data to a package.
withtorch.PackageExporter("package.pt")asexporter:# Pickles the object and saves to `my_resources/tensor.pkl` in the archive.exporter.
Customize how a class is packaged?
torch.package allows for the customization of how classes are packaged. This behavior is accessed through defining the method
__reduce_package__ on a class and by defining a corresponding de-packaging function. This is similar to defining __reduce__ for
Python’s normal pickling process.
Steps:
Define the method __reduce_package__(self,exporter:PackageExporter) on the target class. This method should do the work to save the class instance inside of the package, and should return a tuple of the corresponding de-packaging function with the arguments needed to invoke the de-packaging function. This method is called by the PackageExporter when it encounters an instance of the target class.
Define a de-packaging function for the class. This de-packaging function should do the work to reconstruct and return an instance of the class. The function signature’s first parameter should be a PackageImporter instance, and the rest of the parameters are user defined.
Test in my source code whether or not it is executing inside a package?
A PackageImporter will add the attribute __torch_package__ to every module that it initializes. Your code can check for the
presence of this attribute to determine whether it is executing in a packaged context or not.
# In foo/bar.py:if"__torch_package__"indir():# true if the code is being loaded from a packagedefis_in_package():returnTrueUserException=Exceptionelse
Patch code into a package?
PackageExporter offers a save_source_string() method that allows one to save arbitrary Python source code to a module of your choosing.
withPackageExporter(f)asexporter:# Save the my_module.foo available in your current Python environment.exporter.save_module("my_module.foo")# This saves the provided string to my_module/foo.py in the package archive.# It will override the my_module.foo that was previously saved.exporter
withPackageExporter(f)asexporter:# saves text to my_resource/a.txt in the archiveexporter.save_text("my_resource","a.txt",
Distinguish between packaged code and non-packaged code?
To tell if an object’s code is from a torch.package, use the torch.package.is_from_package() function.
Note: if an object is from a package but its definition is from a module marked extern or from stdlib,
this check will return False.
To re-export an object that was previously imported by a PackageImporter, you must make the new PackageExporter
aware of the original PackageImporter so that it can find source code for your object’s dependencies.
A torch.package file is a ZIP archive which conventionally uses the .pt extension. Inside the ZIP archive, there are two kinds of files:
Framework files, which are placed in the .data/.
User files, which is everything else.
As an example, this is what a fully packaged ResNet model from torchvision looks like:
resnet
├── .data # All framework-specific data is stored here.
│ │ # It's named to avoid conflicts with user-serialized code.
│ ├── 94286146172688.storage # tensor data
│ ├── 94286146172784.storage
│ ├── extern_modules # text file with names of extern modules (e.g. 'torch')
│ ├── version # version metadata
│ ├── ...
├── model # the pickled model
│ └── model.pkl
└── torchvision # all code dependencies are captured as source files
└── models
├── resnet.py
└── utils.py
Framework files
The .data/ directory is owned by torch.package, and its contents are considered to be a private implementation detail.
The torch.package format makes no guarantees about the contents of .data/, but any changes made will be backward compatible
(that is, newer version of PyTorch will always be able to load older torch.packages).
Currently, the .data/ directory contains the following items:
version: a version number for the serialized format, so that the torch.package import infrastructures knows how to load this package.
extern_modules: a list of modules that are considered . modules will be imported using the loading environment’s system importer.
User files
All other files in the archive were put there by a user. The layout is identical to a Python
regular package. For a deeper dive in how Python packaging works,
please consult this essay (it’s slightly out of date, so double-check implementation details
with the Python reference documentation.
<package root>
├── model # the pickled model
│ └── model.pkl
├── another_package
│ ├── __init__.py
│ ├── foo.txt # a resource file , see importlib.resources
│ └── ...
└── torchvision
└── models
├── resnet.py # torchvision.models.resnet
└── utils.py # torchvision.models.utils
How torch.package finds your code’s dependencies
Analyzing an object’s dependencies
When you issue a save_pickle(obj,...) call, PackageExporter will pickle the object normally. Then, it uses the
pickletools standard library module to parse the pickle bytecode.
In a pickle, an object is saved along with a GLOBAL opcode that describes where to find the implementation of the object’s type, like:
GLOBAL'torchvision.models.resnet Resnet`
Dependency Management
torch.package automatically finds the Python modules that your code and objects depend on. This process is called dependency resolution.
For each module that the dependency resolver finds, you must specify an action to take.
The allowed actions are:
intern: put this module into the package.
extern: declare this module as an external dependency of the package.
mock: stub out this module.
deny: depending on this module will raise an error during package export.
Finally, there is one more important action that is not technically part of torch.package:
Refactoring: remove or change the dependencies in your code.
Note that actions are only defined on entire Python modules. There is no way to package “just” a function or class from a module and leave the rest out.
This is by design. Python does not offer clean boundaries between objects defined in a module. The only defined unit of dependency organization is a
module, so that’s what uses.
torch.package sharp edges
Avoid global state in your modules
Python makes it really easy to bind objects and run code at module-level scope. This is generally fine—after all, functions and classes are bound to
names this way. However, things become more complicated when you define an object at module scope with the intention of mutating it, introducing mutable
global state.
Mutable global state is quite useful—it can reduce boilerplate, allow for open registration into tables, etc. But unless employed very carefully, it can
cause complications when used with torch.package.
Every PackageImporter creates an independent environment for its contents. This is nice because it means we load multiple packages and ensure
they are isolated from each other, but when modules are written in a way that assumes shared mutable global state, this behavior can create hard-to-debug
errors.
Types are not shared between packages and the loading environment
Any class that you import from a will be a version of the class specific to that importer. For example:
How torch.package keeps packages isolated from each other
Each PackageImporter instance creates an independent, isolated environment for its modules and objects. Modules in a package can only import
other packaged modules, or modules marked extern. If you use multiple PackageImporter instances to load a single package, you will get
multiple independent environments that do not interact.
This is achieved by extending Python’s import infrastructure with a custom importer. PackageImporter provides the same core API as the
importlib importer; namely, it implements the import_module and methods.
This exception is raised when there is an issue with exporting a package.
PackageExporter will attempt to gather up all the errors and present
them to you at once.
classtorch.package.EmptyMatchError
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.
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.
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.
,
classtorch.package.Directory(name, is_dir)
A file structure representation. Organized as Directory nodes that have lists of
their Directory children. Directories for a package are created by calling
PackageImporter.file_structure().
Find all broken modules in a PackagingError, and for each one, return the
dependency path in which the module was first encountered.
E.g. broken module m.n.o was added to a dependency graph while processing a.b.c,
then re-encountered while processing d.e.f. This method would return
{‘m.n.o’: [‘a’, ‘b’, ‘c’]}
Trace the execution of a callable in order to determine which modules it uses.
Parameters:
callable (Callable[[Any], Any]) – The callable to execute and trace.
inputs () – The input to use during tracing. The modules used by ‘callable’ when invoked by each set of inputs
are union-ed to determine all modules used by the callable for the purposes of packaging.
include
exclude
withPackageExporter('my_package.pt')aspe:pe.save_pickle('models','model_1.pkl',mod)importer=PackageImporter('my_package.pt')# can limit printed items with include/exclude argsprint(importer.file_structure(include=["**/utils.py","**/*.pkl"],exclude="**/*.storage"))print(importer.file_structure())# will print out all files
PackageImporter exposes complementary methods named load_pickle, load_text and load_binary that allow you to load
Python objects, text and binary data from a package.
# foo.py [Example of customizing how class Foo is packaged]fromtorch.packageimportPackageExporter,PackageImporterimporttimeclassFoo:def__init__(self,my_string:str):super().__init__()self.my_string=my_stringself.time_imported=0self.time_exported=0def__reduce_package__(self,exporter:PackageExporter):""" Called by ``torch.package.PackageExporter``'s Pickler's ``persistent_id`` when saving an instance of this object. This method should do the work to save this object inside of the ``torch.package`` archive. Returns function w/ arguments to load the object from a ``torch.package.PackageImporter``'s Pickler's ``persistent_load`` function. """# use this pattern to ensure no naming conflicts with normal dependencies,# anything saved under this module name shouldn't conflict with other# items in the packagegenerated_module_name=f"foo-generated._{exporter.get_unique_id()}"exporter.save_text(generated_module_name,"foo.txt",self.my_string+", with exporter modification!",)time_exported=time.clock_gettime(1)# returns de-packaging function w/ arguments to invoke withreturn(unpackage_foo,(generated_module_name,time_exported,))defunpackage_foo(importer:PackageImporter,generated_module_name:str,time_exported:float)->Foo:""" Called by ``torch.package.PackageImporter``'s Pickler's ``persistent_load`` function when depickling a Foo object. Performs work of loading and returning a Foo instance from a ``torch.package`` archive. """time_imported=time.clock_gettime(1)foo=Foo(importer.load_text(generated_module_name,"foo.txt"))foo.time_imported=time_importedfoo.time_exported=time_exportedreturnfoo
# example of saving instances of class Fooimporttorchfromtorch.packageimportPackageImporter,PackageExporterimportfoofoo_1=foo.Foo("foo_1 initial string")foo_2=foo.Foo("foo_2 initial string")withPackageExporter('foo_package.pt')aspe:# save as normal, no extra work necessarype.save_pickle('foo_collection','foo1.pkl',foo_1)pe.save_pickle('foo_collection','foo2.pkl',foo_2)pi=PackageImporter('foo_package.pt')print(pi.file_structure())imported_foo=pi.load_pickle('foo_collection','foo1.pkl')print(f"foo_1 string: '{imported_foo.my_string}'")print(f"foo_1 export time: {imported_foo.time_exported}")print(f"foo_1 import time: {imported_foo.time_imported}")
Warning: in general, it’s bad practice to have code that behaves differently depending on whether it’s packaged or not. This can lead to
hard-to-debug issues that are sensitive to how you imported your code. If your package is intended to be heavily used, consider restructuring
your code so that it behaves the same way no matter how it was loaded.
.
save_source_string
(
"my_module.foo"
,
textwrap
.
dedent
(
"""\
def my_function():
print('hello world')
"""
))
# If you want to treat my_module.bar as a package
# (e.g. save to `my_module/bar/__init__.py` instead of `my_module/bar.py)
# pass is_package=True,
exporter
.
save_source_string
(
"my_module.bar"
,
"def foo(): print('hello')
\n
"
,
is_package
=
True
)
importer
=
PackageImporter
(
f
)
importer
.
import_module
(
"my_module.foo"
)
.
my_function
()
# prints 'hello world'
"hello world!"
)
# saves the tensor to my_pickle/obj.pkl
exporter
.
save_pickle
(
"my_pickle"
,
"obj.pkl"
,
torch
.
ones
(
2
,
2
))
# see below for module contents
exporter
.
save_module
(
"foo"
)
exporter
.
save_module
(
"bar"
)
The importlib.resources API allows access to resources from within packaged code.
Using importlib.resources is the recommended way to access package contents from within packaged code, since it complies
with the Python standard. However, it is also possible to access the parent :class:PackageImporter instance itself from within
packaged code.
# bar.py:importtorch_package_importer# this is the PackageImporter that imported this module.# Prints "hello world!", equivalent to importlib.resources.read_textdefget_my_resource():returntorch_package_importer.load_text("my_resource","a.txt")# You also do things that the importlib.resources API does not support, like loading# a pickled object from the package.defget_my_pickle():returntorch_package_importer.load_pickle("my_pickle","obj.pkl")
The dependency resolver will gather up all GLOBAL ops and mark them as dependencies of your pickled object.
For more information about pickling and the pickle format, please consult the Python docs.
Analyzing a module’s dependencies
When a Python module is identified as a dependency, torch.package walks the module’s python AST representation and looks for import statements with
full support for the standard forms: fromximporty, importz, fromwimportvasu, etc. When one of these import statements are
encountered, torch.package registers the imported modules as dependencies that are then themselves parsed in the same AST walking way.
Note: AST parsing has limited support for the __import__(...) syntax and does not support importlib.import_module calls. In general, you should
not expect dynamic imports to be detected by torch.package.
torch.package
Actions are applied to modules using patterns. Patterns can either be module names ("foo.bar") or globs (like "foo.**"). You associate a pattern
with an action using methods on PackageExporter, e.g.
If a module matches a pattern, the corresponding action is applied to it. For a given module, patterns will be checked in the order that they were defined,
and the first action will be taken.
intern
If a module is intern-ed, it will be placed into the package.
This action is your model code, or any related code you want to package. For example, if you are trying to package a ResNet from torchvision,
you will need to intern the module torchvision.models.resnet.
On package import, when your packaged code tries to import an intern-ed module, PackageImporter will look inside your package for that module.
If it can’t find that module, an error will be raised. This ensures that each PackageImporter is isolated from the loading environment—even
if you have my_interned_module available in both your package and the loading environment, PackageImporter will only use the version in your
package.
Note: Only Python source modules can be intern-ed. Other kinds of modules, like C extension modules and bytecode modules, will raise an error if
you attempt to intern them. These kinds of modules need to be mock-ed or extern-ed.
extern
If a module is extern-ed, it will not be packaged. Instead, it will be added to a list of external dependencies for this package. You can find this
list on package_exporter.extern_modules.
On package import, when the packaged code tries to import an extern-ed module, PackageImporter will use the default Python importer to find
that module, as if you did importlib.import_module("my_externed_module"). If it can’t find that module, an error will be raised.
In this way, you can depend on third-party libraries like numpy and scipy from within your package without having to package them too.
Warning: If any external library changes in a backwards-incompatible way, your package may fail to load. If you need long-term reproducibility
for your package, try to limit your use of extern.
mock
If a module is mock-ed, it will not be packaged. Instead a stub module will be packaged in its place. The stub module will allow you to retrieve
objects from it (so that frommy_mocked_moduleimportfoo will not error), but any use of that object will raise a NotImplementedError.
mock should be used for code that you “know” will not be needed in the loaded package, but you still want available for use in non-packaged contents.
For example, initialization/configuration code, or code only used for debugging/training.
Warning: In general, mock should be used as a last resort. It introduces behavioral differences between packaged code and non-packaged code,
which may lead to later confusion. Prefer instead to refactor your code to remove unwanted dependencies.
Refactoring
The best way to manage dependencies is to not have dependencies at all! Often, code can be refactored to remove unnecessary dependencies. Here are some
guidelines for writing code with clean dependencies (which are also generally good practices!):
Include only what you use. Do not leave unused imports in your code. The dependency resolver is not smart enough to tell that they are indeed unused,
and will try to process them.
Qualify your imports. For example, instead of writing import foo and later using foo.bar.baz, prefer to write fromfoo.barimportbaz. This more
precisely specifies your real dependency (foo.bar) and lets the dependency resolver know you don’t need all of foo.
Split up large files with unrelated functionality into smaller ones. If your utils module contains a hodge-podge of unrelated functionality, any module
that depends on utils will need to pull in lots of unrelated dependencies, even if you only needed a small part of it. Prefer instead to define
single-purpose modules that can be packaged independently of one another.
Patterns
Patterns allow you to specify groups of modules with a convenient syntax. The syntax and behavior of patterns follows the Bazel/Buck
glob().
A module that we are trying to match against a pattern is called a candidate. A candidate is composed of a list of segments separated by a
separator string, e.g. foo.bar.baz.
A pattern contains one or more segments. Segments can be:
A literal string (e.g. foo), which matches exactly.
A string containing a wildcard (e.g. torch, or foo*baz*). The wildcard matches any string, including the empty string.
A double wildcard (**). This matches against zero or more complete segments.
Examples:
torch.**: matches torch and all its submodules, e.g. torch.nn and torch.nn.functional.
torch.*: matches torch.nn or torch.functional, but not torch.nn.functional or torch
torch*.**: matches torch, torchvision, and all of their submodules
When specifying actions, you can pass multiple patterns, e.g.
A module will match against this action if it matches any of the patterns.
You can also specify patterns to exclude, e.g.
exporter.mock("**",exclude=["torchvision.**"])
A module will not match against this action if it matches any of the exclude patterns. In this example, we are mocking all modules except
torchvision and its submodules.
When a module could potentially match against multiple actions, the first action defined will be taken.
In this example, MyClass and imported_MyClass are not the same type. In this specific example, MyClass and imported_MyClass have exactly the
same implementation, so you might think it’s okay to consider them the same class. But consider the situation where imported_MyClass is coming from an
older package with an entirely different implementation of MyClass — in that case, it’s unsafe to consider them the same class.
Under the hood, each importer has a prefix that allows it to uniquely identify classes:
print(MyClass.__name__)# prints "foo.MyClass"print(imported_MyClass.__name__)# prints <torch_package_0>.foo.MyClass
That means you should not expect isinstance checks to work when one of the arguments is from a package and the other is not. If you need this
functionality, consider the following options:
Doing duck typing (just using the class instead of explicitly checking that it is of a given type).
Make the typing relationship an explicit part of the class contract. For example, you can add an attribute tag self.handler="handle_me_this_way" and have client code check for the value of handler instead of checking the type directly.
__import__
When you invoke PackageImporter.import_module(), PackageImporter will construct and return a new module, much as the system importer does.
However, PackageImporter patches the returned module to use self (i.e. that PackageImporter instance) to fulfill future import
requests by looking in the package rather than searching the user’s Python environment.
Mangling
To avoid confusion (“is this foo.bar object the one from my package, or the one from my Python environment?”), PackageImporter mangles the
__name__ and __file__ of all imported modules, by adding a mangle prefix to them.
For __name__, a name like torchvision.models.resnet18 becomes <torch_package_0>.torchvision.models.resnet18.
For __file__, a name like torchvision/models/resnet18.py becomes <torch_package_0>.torchvision/modules/resnet18.py.
Name mangling helps avoid inadvertent punning of module names between different packages, and helps you debug by making stack traces and print
statements more clearly show whether they are referring to packaged code or not. For developer-facing details about mangling, consult
mangling.md in torch/package/.
When source code is added to the package, the exporter can optionally scan it
for further code dependencies (dependencies=True). It looks for import statements,
resolves relative references to qualified module names, and performs an action specified by the user
(See: extern(), mock(), and intern()).
f (str | PathLike[str] | IO[bytes]) – The location to export to. Can be a string/Path object containing a filename
or a binary I/O object.
importer (Importer | Sequence[Importer]) – If a single Importer is passed, use that to search for modules.
If a sequence of importers are passed, an OrderedImporter will be constructed out of them.
debug (bool) – If set to True, add path of broken modules to PackagingErrors.
add_dependency(module_name, dependencies=True)
Given a module, add it to the dependency graph according to patterns
specified by the user.
Blocklist modules whose 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 PackagingError is raised.
Parameters:
include (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 (list[str] | str) – An optional pattern that excludes some patterns that match the include string.
dependency_graph_string()
Returns digraph string representation of dependencies in package.
Returns:
A string representation of dependencies in package.
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 (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 (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.
externed_modules()
Return all modules that are currently externed.
Returns:
A list containing the names of modules which will be
externed in this package.
Specify modules that should be packaged. A module must match some intern pattern in order to be
included in the package and have its dependencies processed recursively.
Parameters:
include (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 (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 intern modules specified by this call
to the intern method must be matched to some module during packaging. If an intern 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.
interned_modules()
Return all modules that are currently interned.
Returns:
A list containing the names of modules which will be
interned in this package.
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.
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 (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: is [].
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.
mocked_modules()
Return all modules that are currently mocked.
Returns:
A list containing the names of modules which will be
mocked in this package.
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_name (str) – 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 a python object to the archive using pickle. Equivalent to torch.save() but saving into
the archive rather than a stand-alone file. Standard 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.
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.
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.
save_text(package, resource, text)
Save text data to the package.
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.
Open file_or_buffer for importing. This checks that the imported package only requires modules
allowed by module_allowed
Parameters:
file_or_buffer (str | PathLike[str] | IO[bytes] | PyTorchFileReader) – a file-like object (has to implement read(), readline(), tell(), and seek()),
a string, or an os.PathLike object containing a filename.
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=())
Returns a file structure representation of package’s zipfile.
Parameters:
include (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 included in the zipfile representation. This can also be
a glob-style pattern, as described in PackageExporter.mock()
exclude (list[str] | str) – An optional pattern that excludes files whose name match the pattern.
Returns internal identifier that torch.package uses to distinguish PackageImporter instances.
Looks like:
<torch_package_0>
import_module(name, package=None)
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.