Commit Graph

37 Commits

Author SHA1 Message Date
Maggie Moss
d1a6e006e0 Fix syntax for pyrefly errors (#166496)
Last one! This ensures all existing suppressions match the syntax expected and will silence only one error code

pyrefly check
lintrunner

Pull Request resolved: https://github.com/pytorch/pytorch/pull/166496
Approved by: https://github.com/Skylion007, https://github.com/mlazos
2025-10-29 20:00:25 +00:00
Maggie Moss
5f18f240de Add initial suppressions for pyrefly (#164177)
Adds suppressions to pyrefly will typecheck clean: https://github.com/pytorch/pytorch/issues/163283

Test plan:
`python3 scripts/lintrunner.py`
`pyrefly check`

---

Pyrefly check before: https://gist.github.com/maggiemoss/3a0aa0b6cdda0e449cd5743d5fce2c60
After:

```
 INFO Checking project configured at `/Users/maggiemoss/python_projects/pytorch/pyrefly.toml`
 INFO 0 errors (1,063 ignored)
```

Pull Request resolved: https://github.com/pytorch/pytorch/pull/164177
Approved by: https://github.com/Lucaskabela
2025-10-02 20:57:41 +00:00
Dave Lei
8ce81bcee1 [Torch Package] Make get names of OrderedImporters support fallback to importers (#155743)
Summary:
OrderedImporters is supposed to be an importer which tries out every single importer in self._importers. However the get_name API does not follow this behavior and only uses the get_name from the basic Importer class.
This change is to update the OrderedImporters get_name API so that it tries the get_name API of every single importers.

Differential Revision: D76463252

Pull Request resolved: https://github.com/pytorch/pytorch/pull/155743
Approved by: https://github.com/jcwchen, https://github.com/jingsh
2025-08-06 02:26:10 +00:00
Aaron Orenstein
0afd335174 PEP585 update - torch/nn torch/optim torch/package torch/profiler torch/serialization torch/sparse torch/xpu (#145175)
See #145101 for details.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/145175
Approved by: https://github.com/bobrenjc93
2025-01-21 16:57:27 +00:00
PyTorch MergeBot
5fd881a5b6 Revert "PEP585 update - torch/nn torch/optim torch/package torch/profiler torch/serialization torch/sparse torch/xpu (#145175)"
This reverts commit 54a00af2c6.

Reverted https://github.com/pytorch/pytorch/pull/145175 on behalf of https://github.com/huydhn due to Sorry for reverting your change but it seems to break some trunk tests ([comment](https://github.com/pytorch/pytorch/pull/145175#issuecomment-2603418267))
2025-01-21 00:49:55 +00:00
Aaron Orenstein
54a00af2c6 PEP585 update - torch/nn torch/optim torch/package torch/profiler torch/serialization torch/sparse torch/xpu (#145175)
See #145101 for details.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/145175
Approved by: https://github.com/bobrenjc93
2025-01-20 22:32:59 +00:00
Xuehai Pan
758a0a88a2 [BE][Easy] enable ruff rule PIE790: unnecessary pass statement (#133200)
This PR removes unnecessary `pass` statement. This is semanticly safe because the bytecode for the Python code does not change.

Note that if there is a docstring in the function, a empty function does not need a `pass` statement as placeholder.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/133200
Approved by: https://github.com/malfet, https://github.com/eqy, https://github.com/kit1980
2024-08-15 15:50:19 +00:00
Xuehai Pan
30293319a8 [BE][Easy][19/19] enforce style for empty lines in import segments in torch/[o-z]*/ (#129771)
See https://github.com/pytorch/pytorch/pull/129751#issue-2380881501. Most changes are auto-generated by linter.

You can review these PRs via:

```bash
git diff --ignore-all-space --ignore-blank-lines HEAD~1
```

Pull Request resolved: https://github.com/pytorch/pytorch/pull/129771
Approved by: https://github.com/justinchuby, https://github.com/janeyx99
2024-08-01 17:07:14 +00:00
Aaron Orenstein
8db9dfa2d7 Flip default value for mypy disallow_untyped_defs [9/11] (#127846)
See #127836 for details.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/127846
Approved by: https://github.com/ezyang
ghstack dependencies: #127842, #127843, #127844, #127845
2024-06-08 18:50:06 +00:00
Kazuaki Ishizaki
b5f9696d81 Fix typo under torch directory (#110824)
This PR fixes typo `the the` of comments and exception messages in files under `torch` directory.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/110824
Approved by: https://github.com/H-Huang
2023-10-09 19:16:43 +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
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
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
Michael Suo
ed4cda0183 [pkg] opt into autoformat
Summary: woooo

Test Plan: arc lint --apply-patches --take BLACK --paths-cmd 'hg files -I "caffe2/**/*.py"'

Reviewed By: SplitInfinity

Differential Revision: D28608934

fbshipit-source-id: 7768fed50a87883a95319376c0a6d73a9492bdcc
2021-05-21 15:03:52 -07:00
Michael Suo
cb234e606d [package] fix corner case in PacakgeImporter.whichmodule (#57651)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/57651

We've gone back and forth on whether to emulate the `sys.modules` lookup
behavior in our own `whichmodule`, the provided test is a concrete case
for doing so.

An additional minor cleanup is to make the type of `self.modules` in
importers `Dict[str, ModuleType]`. Modules could only be None in the
dictionary in older versions of the import system

Test Plan: Imported from OSS

Reviewed By: SplitInfinity

Differential Revision: D28226536

Pulled By: suo

fbshipit-source-id: c2e6da91651ddaa4fbf7171555df9e5cbe1060fd
2021-05-06 14:15:21 -07:00
Sam Estep
75024e228c Add lint for unqualified type: ignore (#56290)
Summary:
The other half of https://github.com/pytorch/pytorch/issues/56272.

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

Test Plan:
CI should pass on the tip of this PR, and we know that the lint works because the following CI runs (before this PR was finished) failed:

- https://github.com/pytorch/pytorch/runs/2384511062
- https://github.com/pytorch/pytorch/actions/runs/765036024

Reviewed By: seemethere

Differential Revision: D27867219

Pulled By: samestep

fbshipit-source-id: e648f07b6822867e70833e23ddafe7fb7eaca235
2021-04-21 08:07:23 -07:00
Michael Suo
162e1003c9 [package] fix whichmodule for OrderedImporter (#55646)
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/55646

Test Plan: Imported from OSS

Reviewed By: SplitInfinity

Differential Revision: D27664238

Pulled By: suo

fbshipit-source-id: 752ba568ade2dbd268a7c1d5b3a12f5c396fcfbb
2021-04-09 13:26:44 -07:00
Michael Suo
8c2c9450cc [package] autoformat (#53783)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/53783

Use isort + black on torch/package/

Test Plan: Imported from OSS

Reviewed By: SplitInfinity

Differential Revision: D26969020

Pulled By: suo

fbshipit-source-id: e2c0738e79bf41b6342355eb7025998178c35dc9
2021-03-15 17:18:43 -07:00
Michael Suo
17bc70e6f7 [package] make importer a little more obscure (#51676)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/51676

We offer the ability to access the importer from within packaged modules by doing
`import resources`. This behavior is nice (and more powerful than the
importlib resources API), but I think `resources` is too common a name
(pip has a package for it)

Change to `import torch_package_importer` but open to bikeshedding

Test Plan: Imported from OSS

Reviewed By: jamesr66a

Differential Revision: D26620314

Pulled By: suo

fbshipit-source-id: 0942c99f02c0f55f5f3a1b2566961018b796bdd4
2021-03-10 12:13:15 -08:00
Michael Suo
b4d8f4af82 [package] implement get_resource_reader API (#51674)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/51674

See
https://docs.python.org/3/library/importlib.html#importlib.abc.ResourceReader

Test Plan: Imported from OSS

Reviewed By: zdevito

Differential Revision: D26237034

Pulled By: suo

fbshipit-source-id: 4c19f6172d16b710737528d3de48372873b9368d
2021-03-10 12:11:11 -08:00
Michael Suo
ac122a5a6d [package] catch exceptions from calling reduce function. (#53061)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/53061

We only care about evaluating the string return version. If `reduce()`
throws an error, we should just continue on with pickling.

Test Plan: Imported from OSS

Reviewed By: Lilyjjo

Differential Revision: D26737652

Pulled By: suo

fbshipit-source-id: 0b6fbbe345ad0b6a33330b2efa39d7bab703193d
2021-03-01 21:27:08 -08:00
Michael Suo
d5ac929b62 [package] Introduce Importer to manage module namespace collisions. (#51975)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/51975

See comments in code.

Test Plan: Imported from OSS

Reviewed By: zdevito

Differential Revision: D26340592

Pulled By: suo

fbshipit-source-id: 61b16bafad15e19060710ad2d8487c776d672847
2021-02-19 10:06:04 -08:00
Michael Suo
76e8324370 [package] rename ex/importer.py to package_ex/importer.py (#52320)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/52320

as title

Test Plan: Imported from OSS

Reviewed By: zdevito

Differential Revision: D26468416

Pulled By: suo

fbshipit-source-id: 890eecea76426918daff900402fbcbc149e48535
2021-02-19 10:04:14 -08:00
Zachary DeVito
60518d10f6 [deploy] torch::deploy API (#51754)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/51754

This API allows you to manage multiple python interpreters in a single
process to deploy PyTorch models packaged with torch.package.

torch/csrc/deploy/deploy.h contains the API definition
torch/csrc/deploy/test_deploy.cpp has some examples.

Notes:
* mutex is added to PyTorchStreamReader to make it safe to use from multiple threads at once.
* USE_DEPLOY is only true for the special libtorch_deployinterpreter.so library, when enabled
  we use a hash table to maintain PyObject <> at::Tensor mappping rather than the internal pointer
  in Tensor since >1 interpreter may have a reference to the tensor.
* serialization.py has some additional functions for creating pickle objects
  but keeping storages in memory for use transfering tensors between interpreters

Test Plan: Imported from OSS

Reviewed By: wconstab

Differential Revision: D26329468

Pulled By: zdevito

fbshipit-source-id: d75f4ebb9a27f1d911179d9996041bcb3ca04a07
2021-02-18 02:30:08 -08:00
Michael Suo
c357f8b826 [package] make torch.package produce unified format (#51826)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/51826

Looks like this:
```
resnet.pt
├── .data  # Data folder named so it can't clash with torch.package codemodules.
│   │      # Names/extensions automatically added to avoid namingconflicts.
│   ├── 94286146172688.storage   # tensor data
│   ├── 94286146172784.storage
│   ├── extern_modules           # torch.package metadata
│   ├── version                  # version metadata
│   └── ...
├── model  # package pickled model created w/
│   │      # exporter.save_pickel('model','model.pkl', resnet_model)
│   └── model.pkl
└── torchvision  # all code dependencies for packaged picked
    └── models   # models are captured as source files
            ├── resnet.py
                    └── utils.py
```

Since `version` is hardcoded in our zip reader/writer implementation,
add it as an option that defaults to "version" but accepts other
locations for putting the version metadata.

Test Plan: Imported from OSS

Reviewed By: zdevito

Differential Revision: D26295649

Pulled By: suo

fbshipit-source-id: 2d75feeb7de0f78196b4d0b6e2b814a7d58bd1dd
2021-02-09 07:45:59 -08:00
Michael Suo
85b25257ff [package] Use custom persistent_load in PackageImporter (#51595)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/51595

Right now `PackageExporter` defines its own `persistent_id` but
`PackageImporter` uses the one defined in `torch.serialization`. I have
some downstream plans to customize this so this PR just splits it out.

Not to fear! I know this introduces some duplication and potential for
different behavior between `torch.save` and `torch.package`, but I have
plans to re-unify them soon.

Test Plan: Imported from OSS

Reviewed By: zdevito

Differential Revision: D26211578

Pulled By: suo

fbshipit-source-id: 48a2ccaefb2525e1498ad68b75c46d9de3d479b7
2021-02-09 07:45:55 -08:00
Michael Suo
003a240e68 [package] use WeakValueDictionary for global imported module registry (#51666)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/51666

This ensures the modules will get properly unloaded when all references
to them die

Test Plan: Imported from OSS

Reviewed By: Lilyjjo

Differential Revision: D26232574

Pulled By: suo

fbshipit-source-id: a9889965aa35ba2f6cbbfbdd13e02357cc706cab
2021-02-04 09:42:18 -08:00
Michael Suo
55a4aa79aa [package] patch inspect.getfile to work with PackageImporter (#51568)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/51568

The default behavior of inspect.getfile doesn't work on classes imported
from PackageImporter, because it returns the following.

    sys.modules[kls.__module__].__file__

Looking in `sys.modules` is hard-coded behavior. So, patch it to first
check a similar registry of PackageImported modules we maintain.

Test Plan: Imported from OSS

Reviewed By: yf225

Differential Revision: D26201236

Pulled By: suo

fbshipit-source-id: aaf5d7ee8ca0155619c8185e64f70a30152ac567
2021-02-02 11:29:29 -08:00
Michael Suo
2ba2ab9e46 [packaging] add support for BytesIO (#50838)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/50838

Similar to `torch.save` and `torch.jit.save`, accept a IO-like object
instead of just a file.

Test Plan: Imported from OSS

Reviewed By: nikithamalgifb

Differential Revision: D25982719

Pulled By: suo

fbshipit-source-id: 42f3665932bbaa6897215002d116df6338edae50
2021-01-22 08:33:39 -08:00
Michael Suo
b75cdceb44 [package] Properly demangle all accesses of __name__ in importer.py (#50711)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/50711

As title, missed a few of these.

Test Plan: Imported from OSS

Reviewed By: yf225

Differential Revision: D25949363

Pulled By: suo

fbshipit-source-id: 197743fe7097d2ac894421a99c072696c3b8cd70
2021-01-18 23:43:46 -08:00
Michael Suo
0b49778666 [package] mangle imported module names (#50049)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/50049

Rationale and implementation immortalized in a big comment in
`torch/package/mangling.md`.

This change also allows imported modules to be TorchScripted

Test Plan: Imported from OSS

Reviewed By: pbelevich

Differential Revision: D25758625

Pulled By: suo

fbshipit-source-id: 77a99dd2024c76716cfa6e59c3855ed590efda8b
2021-01-13 16:32:36 -08:00
Zachary DeVito
adbb74ded9 [package] pre-emptively install submodules (#48799)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/48799

Python's IMPORT_FROM bytecode will bypass the import infrastructure
when a packaging being loaded as part of a cirular dependency is being
accessed from the module _before_ that package has finished loading
and is installed on the module. Since we cannot override the lookup
on sys.modules, this PR pre-emptively does the module assignment before
running the submodules initialization code.

Note: this appears to work, but it is not clear to me why python doesn't
do this by default. It is possible that the logic for creating modules
is flexible enough in generic python that this interception between creating
the module and running its code is not always possible.

Test Plan: Imported from OSS

Reviewed By: suo

Differential Revision: D25312467

Pulled By: zdevito

fbshipit-source-id: 6fe3132af29364ccb2b3cabdd2b847d0a09eb515
2020-12-07 17:12:04 -08:00
Zachary DeVito
88735f2cc9 [package] move importer logic into import pickler (#48632)
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/48632

Test Plan: Imported from OSS

Reviewed By: suo

Differential Revision: D25236017

Pulled By: zdevito

fbshipit-source-id: 57fd80d36ddf390ae35c58adf6dddbf15a1347c1
2020-12-02 12:24:44 -08:00
Zachary DeVito
25ed739ac9 [packaging] rstrip fix (#45166)
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/45166

Test Plan: Imported from OSS

Reviewed By: suo

Differential Revision: D23852505

Pulled By: zdevito

fbshipit-source-id: 6bb743b37333ae19fc24629686e8d06aef812c50
2020-09-22 21:23:47 -07:00
Zachary DeVito
cb75addee4 torch.package - a way to package models and code (#45015)
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
2020-09-22 21:21:21 -07:00