Commit Graph

136 Commits

Author SHA1 Message Date
Stephen Macke
0c972fb5c7 [rfc][pkg] check spec for module source before falling back to file in package exporter (#90258)
Summary: To get source for a particular module, the "correct" thing to do is to check the module's spec and use `get_source` if it's a SourceFileLoader, since subclasses may look elsewhere than the `__file__`, and the spec will give the source of truth. For torch packager, however, we prefer to use linecache, but the loader could still change the file, so we figure out the file for the module using the spec's loader rather than using `module.__file__`, if possible.

Test Plan: This code path will get exercised by CI. Also added a test for remapped files.

Differential Revision: D41412983

Pull Request resolved: https://github.com/pytorch/pytorch/pull/90258
Approved by: https://github.com/PaliC
2022-12-08 20:24:45 +00:00
Atul Jangra
1ba94b3882 Support pickle version 4 by adding missing ops (#90223)
Summary:
In this logic, we are traversing the entries to find the module for STACK_GLOBAL entries.

According to 2837241f22/Lib/pickletools.py (L1799) we need to look for GET, BINGET and LONG_BINGET.

So this diff updates that. Also while testing, I found some cases of empty modules, for cases such as tanh. For this I added the option to skip processing when this is the case.

Test Plan: Tested with f392778829

Differential Revision: D41748595

Pull Request resolved: https://github.com/pytorch/pytorch/pull/90223
Approved by: https://github.com/PaliC
2022-12-08 01:06:40 +00:00
Kurt Mohler
ee28b865ee Deprecate TypedStorage, its derived classes, and all of their public methods (#85303)
Part of #85302

Pull Request resolved: https://github.com/pytorch/pytorch/pull/85303
Approved by: https://github.com/ezyang
2022-11-08 18:11:01 +00:00
Shiyan Deng
633f0d620d [torch package] Treat builtins as default extern module (#88385)
Summary: When using torch deploy, if we do fx transformation and then try to pickle/unpickle a fx GraphModule, it's possible that the GraphModule's code depends on `builtins` but we didn't add it to extern module.

Reviewed By: PaliC

Differential Revision: D40958730

Pull Request resolved: https://github.com/pytorch/pytorch/pull/88385
Approved by: https://github.com/PaliC
2022-11-04 17:35:12 +00:00
Kazuaki Ishizaki
2ddefbdc3c Fix typos used in documents under torch directory (#88300)
This PR fixes typos, in comments of Python files, that are found from a search box at https://pytorch.org/docs/master/search.html

Pull Request resolved: https://github.com/pytorch/pytorch/pull/88300
Approved by: https://github.com/lezcano
2022-11-02 09:38:13 +00:00
Boyoon Jang
da99008d37 fix typo in torch/package/_mock.py (#84508)
Fixed a typo in torch/package/_mock.py
Fixes #84507

Pull Request resolved: https://github.com/pytorch/pytorch/pull/84508
Approved by: https://github.com/H-Huang
2022-09-05 16:48:34 +00:00
Kurt Mohler
14d0296e5c Rename _Typed/_UntypedStorage to Typed/UntypedStorage and update docs (#82438)
### Description

Since the major changes for `_TypedStorage` and `_UntypedStorage` are now complete, they can be renamed to be public.

`TypedStorage._untyped()` is renamed to `TypedStorage.untyped()`.

Documentation for storages is improved as well.

### Issue
Fixes #82436

### Testing
N/A

Pull Request resolved: https://github.com/pytorch/pytorch/pull/82438
Approved by: https://github.com/ezyang
2022-07-30 19:37:08 +00:00
Huy Do
a3fc076f22 Copy black config to ufmt and run lintrunner -a (#82043)
The last entry is `torch/onnx/**/*.py` will be covered in a separated PR to onnx code owner

### Description
After ufmt (black + usort) covers the same set of files as black, when we can remove black and keep only one "true" linter for pytorch
Pull Request resolved: https://github.com/pytorch/pytorch/pull/82043
Approved by: https://github.com/kit1980
2022-07-23 06:04:21 +00:00
joncrall
4c9eae331b Use standard mechanism for stdlib names (#81520)
I noticed that in #81261 all of the stdlib module names were explicitly listed, however as of Python 3.10 the stdlib now has a mechanism for this. https://github.com/python/cpython/issues/87121

I figured it was better to use `sys.stdlib_module_names` going forward for 3.10+ instead of having to maintain this file for every new Python release. For docs see:
https://docs.python.org/3/library/sys.html#sys.stdlib_module_names

I did a symmetric difference to determine what the effective change would be. I verified that everything listed in this file ins included in sys.stdlib_module_names. However, there are files in sys.stdlib_module_names that are not included in the previous hard coded definition. Namely these are:

```
frozenset({'__future__',
           '_abc',
           '_aix_support',
           '_asyncio',
           '_bisect',
           '_blake2',
           '_bootsubprocess',
           '_bz2',
           '_codecs',
           '_codecs_cn',
           '_codecs_hk',
           '_codecs_iso2022',
           '_codecs_jp',
           '_codecs_kr',
           '_codecs_tw',
           '_collections',
           '_collections_abc',
           '_compat_pickle',
           '_compression',
           '_contextvars',
           '_crypt',
           '_csv',
           '_ctypes',
           '_curses',
           '_curses_panel',
           '_datetime',
           '_dbm',
           '_decimal',
           '_elementtree',
           '_frozen_importlib',
           '_frozen_importlib_external',
           '_functools',
           '_gdbm',
           '_hashlib',
           '_heapq',
           '_imp',
           '_io',
           '_json',
           '_locale',
           '_lsprof',
           '_lzma',
           '_markupbase',
           '_md5',
           '_msi',
           '_multibytecodec',
           '_multiprocessing',
           '_opcode',
           '_operator',
           '_osx_support',
           '_overlapped',
           '_pickle',
           '_posixshmem',
           '_posixsubprocess',
           '_py_abc',
           '_pydecimal',
           '_pyio',
           '_queue',
           '_random',
           '_scproxy',
           '_sha1',
           '_sha256',
           '_sha3',
           '_sha512',
           '_signal',
           '_sitebuiltins',
           '_socket',
           '_sqlite3',
           '_sre',
           '_ssl',
           '_stat',
           '_statistics',
           '_string',
           '_strptime',
           '_struct',
           '_symtable',
           '_threading_local',
           '_tkinter',
           '_tracemalloc',
           '_uuid',
           '_warnings',
           '_weakref',
           '_weakrefset',
           '_winapi',
           '_zoneinfo',
           'antigravity',
           'genericpath',
           'idlelib',
           'nt',
           'nturl2path',
           'opcode',
           'pydoc_data',
           'pyexpat',
           'this'})
```

I'm not sure if excluding these matters. I wouldn't think it would, but if it does and it is better to explicitly update this file each time, then feel free to close this.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/81520
Approved by: https://github.com/malfet
2022-07-15 23:11:23 +00:00
Sergii Dymchenko
2f0172bfc4 Remove unused dtype assignment (#81503)
Pull Request resolved: https://github.com/pytorch/pytorch/pull/81503
Approved by: https://github.com/ezyang
2022-07-15 03:43:32 +00:00
Sahan Paliskara
b256ff6a8f Allow torch._C to be recognized a module in torch.package (#80917)
Summary:
This pr addresses https://github.com/pytorch/multipy/issues/82 and https://github.com/pytorch/multipy/issues/44. The changes will be copied over to [pytorch/multipy](https://github.com/pytorch/multipy) as well.

A C extension module behaves a bit differently than a normal python package as it does not contain a `__path__` attribute. However, these modules still have information about their submodules. This PR also checks if a module is a C extension module  and checks if the module we are looking for is in it's children.

For example, if we are importing `torch._C._nn` we check if the parent `torch._C` is a C extension module if necessary, and then check if `torch._C._nn` is a proper child of `torch._C`.

Differential Revision: D37630120

Pull Request resolved: https://github.com/pytorch/pytorch/pull/80917
Approved by: https://github.com/d4l3k
2022-07-12 18:10:22 +00:00
Nikita Shulga
9ed76c8c89 Add 3.10 stdlib to torch.package (#81261)
Copy-n-paste the list from https://github.com/PyCQA/isort/blob/main/isort/stdlibs/py310.py

Tested locally and in https://github.com/pytorch/pytorch/pull/81233
Pull Request resolved: https://github.com/pytorch/pytorch/pull/81261
Approved by: https://github.com/suo
2022-07-11 20:21:28 +00:00
anjali411
4bf076e964 Add __all__ to torch.distributed, futures, fx, nn, package, benchmark submodules (#80520)
Pull Request resolved: https://github.com/pytorch/pytorch/pull/80520
Approved by: https://github.com/rohan-varma
2022-07-08 14:31:24 +00:00
anjali411
3bcc19b29a Add __all__ to various submodules in torch.fx, distributions, distributed, package (#80367)
Pull Request resolved: https://github.com/pytorch/pytorch/pull/80367
Approved by: https://github.com/albanD
2022-06-27 21:27:30 +00:00
PyTorch MergeBot
9db3c517de Add __all__ for torch.nn.modules, torch.distributed.elastic, torch.nn.utils submodules (#80240)
Pull Request resolved: https://github.com/pytorch/pytorch/pull/80240
Approved by: https://github.com/rohan-varma
2022-06-27 17:11:12 +00:00
Tristan Rice
5f7b92024f torch/package: add fix for implicit numpy dependency (#78979)
Summary:
This adds a workaround so that you can call `.numpy()` on models loaded via torch.package. Torch implicitly imports numpy when you make that call and it can't be tracked by normal dependencies since it's from C++.

https://github.com/pytorch/MultiPy/issues/46

Test Plan: CI

Reviewed By: PaliC

Differential Revision: D36915129

Pull Request resolved: https://github.com/pytorch/pytorch/pull/78979
Approved by: https://github.com/PaliC
2022-06-08 17:07:00 +00:00
John Reese
f625bb4bc9 [codemod][usort] apply import merging for fbcode (1 of 11) (#78973)
Summary:
Applies new import merging and sorting from µsort v1.0.

When merging imports, µsort will make a best-effort to move associated
comments to match merged elements, but there are known limitations due to
the diynamic nature of Python and developer tooling. These changes should
not produce any dangerous runtime changes, but may require touch-ups to
satisfy linters and other tooling.

Note that µsort uses case-insensitive, lexicographical sorting, which
results in a different ordering compared to isort. This provides a more
consistent sorting order, matching the case-insensitive order used when
sorting import statements by module name, and ensures that "frog", "FROG",
and "Frog" always sort next to each other.

For details on µsort's sorting and merging semantics, see the user guide:
https://usort.readthedocs.io/en/stable/guide.html#sorting

Test Plan: S271899

Reviewed By: lisroach

Differential Revision: D36402110

Pull Request resolved: https://github.com/pytorch/pytorch/pull/78973
Approved by: https://github.com/osalpekar
2022-06-06 23:44:28 +00:00
Eli Uriegas
1bec7f8468 torch: Fix black linter
Fixes formatting issues when trying to import diff train

Signed-off-by: Eli Uriegas <eliuriegasfb.com>

Pull Request resolved: https://github.com/pytorch/pytorch/pull/77925

Approved by: https://github.com/mehtanirav, https://github.com/osalpekar
2022-05-20 01:14:08 +00:00
Kurt Mohler
aea6e2c396 Merge torch.cuda._UntypedStorage into torch._UntypedStorage (#75459)
Fixes #74933

Pull Request resolved: https://github.com/pytorch/pytorch/pull/75459
Approved by: https://github.com/ezyang
2022-05-19 13:54:39 +00:00
Sadra Barikbin
71d61bb78b Fix typo in torch.package code and docs (#77604)
Fixes #77603

Pull Request resolved: https://github.com/pytorch/pytorch/pull/77604
Approved by: https://github.com/cpuhrsch
2022-05-17 17:35:39 +00:00
Tristan Rice
4d30ebc82a torch/deploy,package: log usage for InterpreterManager, PackageExporter, PackageImporter (#77097)
Summary: This adds logs for usage of deploy and package. These can be used to track where it's being used in production so we can support it better.

Test Plan: no functional changes - existing tests

Reviewed By: PaliC

Differential Revision: D36258876

Pull Request resolved: https://github.com/pytorch/pytorch/pull/77097
Approved by: https://github.com/PaliC
2022-05-10 19:26:44 +00:00
Michael Suo
fb0f285638 [lint] upgrade mypy to latest version
Fixes https://github.com/pytorch/pytorch/issues/75927.

Had to fix some bugs and add some ignores.

To check if clean:
```
lintrunner --paths-cmd='git grep -Il .' --take MYPY,MYPYSTRICT
```

Pull Request resolved: https://github.com/pytorch/pytorch/pull/76753

Approved by: https://github.com/malfet
2022-05-03 20:51:34 +00:00
PyTorch MergeBot
3d7428d9ac Revert "[lint] upgrade mypy to latest version"
This reverts commit 9bf18aab94.

Reverted https://github.com/pytorch/pytorch/pull/76753 on behalf of https://github.com/suo
2022-05-03 20:01:18 +00:00
Michael Suo
9bf18aab94 [lint] upgrade mypy to latest version
Fixes https://github.com/pytorch/pytorch/issues/75927.

Had to fix some bugs and add some ignores.

To check if clean:
```
lintrunner --paths-cmd='git grep -Il .' --take MYPY,MYPYSTRICT
```

Pull Request resolved: https://github.com/pytorch/pytorch/pull/76753

Approved by: https://github.com/malfet
2022-05-03 19:43:28 +00:00
Mike Ruberry
20be31de90 Revert D35423079: [pkg] add generic ZipFile Reader/Writer
Test Plan: revert-hammer

Differential Revision:
D35423079 (d4a709be3d)

Original commit changeset: 31abc4364d5f

Original Phabricator Diff: D35423079 (d4a709be3d)

fbshipit-source-id: 09ca72ebc330088fbfdcf2cabce3b6385c948a47
(cherry picked from commit d458172fb33135243e5cb1a04a5bee9f7d242f30)
2022-04-07 13:33:47 +00:00
Sahan Paliskara
d4a709be3d [pkg] add generic ZipFile Reader/Writer (#72237)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/72237

add a generic zip file reader/writer to torch.package in order to get rid of dependency on torch for non torchscript / tensor related usages of package. This also enables users to create a derived class from the zip file reader/writer classes to have their own serialization/deserialization if it's desired for performance needs.

https://www.internalfb.com/intern/diff/D35423079/ was reverted due to this refactor changing the name of where most of the implementation components of PackageExporter/PackageImporter come from like ModuleActionType_ etc.
This diff also changes the import paths where these components come from to point to the correct file compared to D35423079

Test Plan: Imported from OSS

Reviewed By: malfet

Differential Revision: D35423079

Pulled By: PaliC

fbshipit-source-id: 31abc4364d5fd007911cfb67cf36ebfac5d786f4
(cherry picked from commit 023b0d1445e0b1e1bb7a03c660cd62eb9d26d2a6)
2022-04-06 16:11:13 -07:00
Natalia Gimelshein
00e2c14b78 Revert D33970688: [pkg] add generic ZipFile Reader/Writer
Test Plan: revert-hammer

Differential Revision:
D33970688 (c2c260bfc3)

Original commit changeset: 8a524867e62a

Original Phabricator Diff: D33970688 (c2c260bfc3)

fbshipit-source-id: 18b4aa4e221b86a498fc434c1b453356fc47cfbf
(cherry picked from commit a295c2b58d3d9cfacfc9d11d36fd80aabd97675c)
2022-04-06 05:52:42 +00:00
Sahan Paliskara
c2c260bfc3 [pkg] add generic ZipFile Reader/Writer (#72237)
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/72237

Test Plan: Imported from OSS

Reviewed By: d4l3k, mrshenli

Differential Revision: D33970688

Pulled By: PaliC

fbshipit-source-id: 8a524867e62acb427170cc56a5d6960436a7455f
(cherry picked from commit f8d924fc4ef2a5c43f8410fb359aa0f0ecb29382)
2022-04-05 22:18:19 +00:00
Bradley Davis
4568daf55d [torch.package] add utility for determining where bad modules may come from (#74998)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/74998

this is a cleaned-up version of a util that I commonly use to figure out where new transitive (surprise!) dependencies come from when a model breaks, since it can be difficult in large models to tell exactly what code change indirectly added the depdendency. I tried to keep the opinionated bits out of OSS as much as possible.

Reviewed By: PaliC

Differential Revision: D35265017

fbshipit-source-id: e126e03aa113db6ab79d32d86a69bcbba844875e
(cherry picked from commit ffa0f4b0a294cbcf7413b176d0d2fa0a605b3b9e)
2022-03-31 23:46:10 +00:00
Jane Xu
58f78ff4e0 Revert D35124731: Automatically extern C extension modules in torch.package
Test Plan: revert-hammer

Differential Revision:
D35124731 (0f27b4b7fc)

Original commit changeset: a4b7fdf3210e

Original Phabricator Diff: D35124731 (0f27b4b7fc)

fbshipit-source-id: ee598f18d43ea1becb6f2673c7ae309e4b4dda35
(cherry picked from commit 6248897d18f4f02f0ad7353c4b78f1cbb12250ae)
2022-03-28 20:26:27 +00:00
Jackie Yuan
0f27b4b7fc Automatically extern C extension modules in torch.package (#74702)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/74702

- add flag in constructor.
- add if condition routing to extern c-extension in method `_intern_module`.
- add unit test for the new condition.

Test Plan: Imported from OSS

Reviewed By: PaliC

Differential Revision: D35124731

fbshipit-source-id: a4b7fdf3210e0ad4bfd1ea30fd94595d10405987
(cherry picked from commit 57239a77ae099328025ab2d634e7880bd14a473b)
2022-03-28 16:41:21 +00:00
Jamie McCrindle
11894db9ea Add Python Version to Torch.Package metadata (#74610)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/74610

Adding python version to exported package and reading it on import as per this issue in github https://github.com/pytorch/pytorch/issues/74068
ghstack-source-id: 152003088

Test Plan: CI Tests

Reviewed By: PaliC

Differential Revision: D35062709

fbshipit-source-id: 04091a1255a09b96255112a60d31df127c424193
(cherry picked from commit ed39fd54b8b20918dac89a2873ecccf06aafd724)
2022-03-24 22:48:25 +00:00
CodemodService FBSourceBlackLinterBot
d6edb8473e [AutoAccept][Codemod][FBSourceBlackLinter] Daily arc lint --take BLACK
Reviewed By: zertosh

Differential Revision: D34986353

fbshipit-source-id: 631b046642ade931e5c865b1e8fb7794b8f94e81
(cherry picked from commit 0098c9e3bb2bd37ea717a1bc197090c625a7fb0a)
2022-03-18 18:22:53 +00:00
Sahan Paliskara
96ed3320f8 [pkg] Improve mocked module detection by combining mocked object errors with the rest of the errors in PackageExporter (#74315)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/74315

Now instead of spitting out a NotImplemented Error when the Package Exporter finds an object for a mocked module, it combines these errors with the rest of the Packaging Errors of PackageExporter to get something like

```
torch.package.package_exporter.PackagingError:
* Module was mocked out, but is still being used in the package.Please intern or extern the mocked modules if objects are supposed to be inthe package.
    package_a
      Context: Object(s) '['PackageASubpackageObject']' from module package_a was mocked out during packaging but is being used in resource - obj.pkl in package obj.
    package_a.subpackage
      Context: Object(s) '['PackageASubpackageObject']' from module package_a.subpackage was mocked out during packaging but is being used in resource - obj.pkl in package obj.
```

This makes it significantly easier to fix mocked object errors as they all should appear at once.

Test Plan: Imported from OSS

Reviewed By: d4l3k

Differential Revision: D34951973

Pulled By: PaliC

fbshipit-source-id: 01ee4ba3767967ef9a9bcd69ad86362ebc100b2d
(cherry picked from commit 900edd270ee8f5802fc6e56df08fff6b073ac6f2)
2022-03-17 18:25:46 +00:00
Michael Suo
577bf04872 Revert D34932200: [pkg] Improve mocked module detection by combining mocked object errors with the rest of the errors in PackageExporter
Test Plan: revert-hammer

Differential Revision:
D34932200 (2775701e4b)

Original commit changeset: 7f12bd88dbfb

Original Phabricator Diff: D34932200 (2775701e4b)

fbshipit-source-id: d111d39ed51daeadf74ffbadc7aa21814314c7e6
(cherry picked from commit 0870e86bb2a2f698d12fbc95d4137dd5ed502f58)
2022-03-17 02:05:12 +00:00
Sahan Paliskara
2775701e4b [pkg] Improve mocked module detection by combining mocked object errors with the rest of the errors in PackageExporter (#74315)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/74315

Now instead of spitting out a NotImplemented Error when the Package Exporter finds an object for a mocked module, it combines these errors with the rest of the Packaging Errors of PackageExporter to get something like

```
torch.package.package_exporter.PackagingError:
* Module was mocked out, but is still being used in the package.Please intern or extern the mocked modules if objects are supposed to be inthe package.
    package_a
      Context: Object(s) '['PackageASubpackageObject']' from module package_a was mocked out during packaging but is being used in resource - obj.pkl in package obj.
    package_a.subpackage
      Context: Object(s) '['PackageASubpackageObject']' from module package_a.subpackage was mocked out during packaging but is being used in resource - obj.pkl in package obj.
```

This makes it significantly easier to fix mocked object errors as they all should appear at once.

Test Plan: Imported from OSS

Reviewed By: aivanou

Differential Revision: D34932200

Pulled By: PaliC

fbshipit-source-id: 7f12bd88dbfbad974fd04b5dcaba3203b5c68a04
(cherry picked from commit 73df434ddd3e26f0e4c5ea3dd2ca1b6984736213)
2022-03-17 01:41:57 +00:00
Sahan Paliskara
e7051939fb [pkg] improve error message for module detection on saving pass (#73106)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/73106

The original error message didn't have next steps, and someone got confused. This error message should make debugging a bit easier.

Test Plan: Imported from OSS

Reviewed By: ngimel

Differential Revision: D34559499

Pulled By: PaliC

fbshipit-source-id: fd5fec9c4db10a20775435a587bad24336a671ef
(cherry picked from commit efdcf1e198389ee156a46cf0f8b185d8145d3266)
2022-03-02 03:33:02 +00:00
Kurt Mohler
8e7fe87630 Rename Typed/UntypedStorage to _Typed/_UntypedStorage (#72540)
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/72540

Reviewed By: jbschlosser

Differential Revision: D34216823

Pulled By: bdhirsh

fbshipit-source-id: 1bc9930ab582771ebf02308e035576cd1a0dbe47
(cherry picked from commit 329238f612)
2022-02-15 23:53:01 +00:00
Michael Suo
6a1147d059 [package] fix orderedimporter dummy package check (#72533)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/72533

The current check that we have for dummy packages is too expansive; it
will skip anything without a `__file__`, including extension modules in
the standard library.

So first check if a module was created by torch.package before skipping
it, which should rule out anything accidentally getting skipped (as the
only time torch.package creates something without a `__file__` is the
dummy case).

Test Plan: Imported from OSS

Reviewed By: PaliC

Differential Revision: D34082792

Pulled By: suo

fbshipit-source-id: 18b17eb0f693927697657b20843ec5cd8bcccb47
(cherry picked from commit f571370078)
2022-02-09 01:23:22 +00:00
CodemodService Bot
1edf6f5647 [Codemod][FBSourceBlackLinter] Daily arc lint --take BLACK
Reviewed By: zertosh

Differential Revision: D34028007

fbshipit-source-id: 6bb39fab7232baae3769d68857964a2a4e581e7d
(cherry picked from commit 3a058212e1)
2022-02-05 10:41:02 +00:00
Sahan Paliskara
5c6b897516 enables module modification during repackaging (#71520)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/71520

Specifically this pr is to deal with adverse cases such as D33662192

It is possible for someone to export a package, change something in it, and then attempt to repackage it. In this case it's possible that dependencies of the package are no longer interned. In this case it is not obvious where we would look for these packages, therefore, we throw an error.

Test Plan: Imported from OSS

Reviewed By: bradleyhd

Differential Revision: D33675557

Pulled By: PaliC

fbshipit-source-id: 807962bfb340d30d418617d6e78661a033828314
(cherry picked from commit 1b10c23807)
2022-02-05 04:01:25 +00:00
CodemodService Bot
fdda7b5e8a [Codemod][FBSourceBlackLinter] Daily arc lint --take BLACK
Reviewed By: zertosh

Differential Revision: D33525225

fbshipit-source-id: 973eb9f9a5dfbd70bf0127f44089237969c2bb68
2022-01-11 04:20:46 -08:00
Sahan Paliskara
3ef10da97d add support for pickle v4 (#70642)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/70642

Review history on https://github.com/pytorch/pytorch/pull/70014

Test Plan: Imported from OSS

Reviewed By: suo

Differential Revision: D33414364

Pulled By: PaliC

fbshipit-source-id: 7e7ed491c6f16d4fac3a03f7e403935823c03aa6
2022-01-10 11:13:41 -08:00
Sahan Paliskara
118bd82dde detect mocked module on saving pass (#70641)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/70641

Raises a not implemented error if we attempt to pickle an object which uses a mocked module. Now we no longer have to load the object to get this check, and instead happens right on the saving path.

Review History is on https://github.com/pytorch/pytorch/pull/69793 PR was moved to a different branch due to original branch getting corrupted.

Test Plan: Imported from OSS

Reviewed By: suo

Differential Revision: D33414365

Pulled By: PaliC

fbshipit-source-id: 6d72ddb05c47a3d060e9622ec0b6e5cd6c6c71c8
2022-01-10 11:11:55 -08:00
Bradley Davis
fca8a0acaa Prevent import race condition that leaves torch.package.PackagePickler with unwanted dispatch table entries. (#71025)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/71025

TL;DR In come cases:
1) user imports `dill`, which mutates `_Pickler.dispatch`,
2) user imports lib that imports `torch.package`
3) `PackagePickler.dispatch = _Pickler.dispatch.copy()` makes a copy of the mutated table
4) user calls `dill.extend(use_dill=False)` to reset `_Pickler.dispatch`, expecting everything to be okay
5) `PackagePickler` is used to pickle something like `ModuleDict`. `PackagePickler.dispatch` has stale entries to dill pickle functions like `save_module_dict`, which sometimes hard-code calls to `StockPickler.save_global`, which is unaware of torch.package module prefixes.
6) Exception is raised, e.g. `Got unhandled exception Can't pickle <class '<torch_package_2>.caffe2.mylib'>: it's not found as <class '<torch_package_2>.caffe2.mylib'>`

Differential Revision: D33483672

fbshipit-source-id: d7cd2a925bedf27c02524a6a4c3132a262f5c984
2022-01-09 15:13:39 -08:00
CodemodService Bot
cbc29acca3 [Codemod][FBSourceBlackLinter] Daily arc lint --take BLACK
Reviewed By: zertosh

Differential Revision: D31423202

fbshipit-source-id: 08d249e8546c0bfe6f1145c0571141b90aad03eb
2021-10-05 20:55:56 -07:00
Kurt Mohler
5883523c1d Remove dtype from torch.Storage and use only torch.ByteStorage (#62030)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/62030

Remove dtype tracking from Python Storage interface, remove all the different `<type>Storage` classes except for `ByteStorage`, and update serialization accordingly, while maintaining as much FC/BC as possible

Fixes https://github.com/pytorch/pytorch/issues/47442

* **THE SERIALIZATION FORMAT IS FULLY FC/BC.** We worked very hard to make sure this is the case. We will probably want to break FC at some point to make the serialization structure of tensors make more sense, but not today.
* There is now only a single torch.ByteStorage class. Methods like `Tensor.set_` no longer check that the dtype of storage is appropriate.
* As we no longer know what dtype of a storage is, we've **removed** the size method from Storage, replacing it with nbytes. This is to help catch otherwise silent errors where you confuse number of elements with number of bytes.
* `Storage._new_shared` takes a `nbytes` kwarg and will reject previous positional only calls.  `Storage._new_with_file` and `_set_from_file` require explicit element size arguments.
* It's no longer possible to convert storages to different types using the float/double/etc methods. Instead, do the conversion using a tensor.
* It's no longer possible to allocate a typed storage directly using FloatStorage/DoubleStorage/etc constructors. Instead, construct a tensor and extract its storage. The classes still exist but they are used purely for unpickling.
* The preexisting serialization format stores dtype with storage, and in fact this dtype is used to determine the dtype of the tensor overall.
 To accommodate this case, we introduce a new TypedStorage concept that exists only during unpickling time which is used to temporarily store the dtype so we can construct a tensor. **If you overrode the handling of pickling/unpickling, you MUST add handling for TypedStorage** or your serialization code will degrade to standard file-based serialization.

Original pull request: https://github.com/pytorch/pytorch/pull/59671

Reviewed By: soulitzer, ngimel

Differential Revision: D29466819

Pulled By: ezyang

fbshipit-source-id: 4a14e5d3c2b08e06e558683d97f7378a3180b00e
2021-10-05 13:50:34 -07:00
CodemodService Bot
4fe66d962d [Codemod][FBSourceBlackLinter] Daily arc lint --take BLACK
Reviewed By: zertosh

Differential Revision: D31192084

fbshipit-source-id: 25d490783b876253ddd1ad0a70832766ebd33f51
2021-09-25 06:42:19 -07:00
Tugsbayasgalan (Tugsuu) Manlaibaatar
146817c9d0 Add all_paths utility function (#65602)
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/65602

Test Plan: Imported from OSS

Reviewed By: suo

Differential Revision: D31163681

Pulled By: tugsbayasgalan

fbshipit-source-id: fa0b28b1d3b73efcc7671698a613e695a01cc103
2021-09-25 01:11:20 -07:00
Michael Suo
afa25c77f1 [package] Make it possible to re-save a PackageImporter module (#65101)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/65101

As title. Previously this was guarded against for implementation
simplicity, as we didn't really think there was a use case for saving a
mangled module name directly.

But people started doing stuff like:
```
exporter.save_module(my_imported_obj.__module__)
```
which implicitly passes along the mangled module name.

This PR makes it so that given `PackageImporter` instance can always
import modules that it created, and changes `PackageExporter` to
properly demangle the resulting module name when writing the package to
the export archive.

Differential Revision:
D30975712
D30975712

Test Plan: Imported from OSS

Pulled By: suo

fbshipit-source-id: d9e849bf651713890e72dccdcef74fa52d377149
2021-09-17 16:25:11 -07:00