Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[package] mangle imported module names #50049

Closed
wants to merge 15 commits into from
Closed

Conversation

suo
Copy link
Member

@suo suo commented Jan 4, 2021

Stack from ghstack:

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

This change also allows imported modules to be TorchScripted

Differential Revision: D25758625

Rationale and implementation immortalized in a big comment in
_mangling.py

[ghstack-poisoned]
@facebook-github-bot
Copy link
Contributor

facebook-github-bot commented Jan 4, 2021

💊 CI failures summary and remediations

As of commit af3fa71 (more details on the Dr. CI page):


  • 2/2 failures possibly* introduced in this PR
    • 2/2 non-CircleCI failure(s)

ci.pytorch.org: 1 failed


This comment was automatically generated by Dr. CI (expand for details).Follow this link to opt-out of these comments for your Pull Requests.

Please report bugs/suggestions to the (internal) Dr. CI Users group.

Rationale and implementation immortalized in a big comment in
_mangling.py

[ghstack-poisoned]
@suo suo requested a review from zdevito January 4, 2021 17:45
suo added a commit that referenced this pull request Jan 4, 2021
Rationale and implementation immortalized in a big comment in
_mangling.py

ghstack-source-id: e7131e1ebe4c87f7a036db6541ed2f8d40de55fa
Pull Request resolved: #50049
Copy link
Contributor

@zdevito zdevito left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest taking the approach of trying to unmangle and mangle the names at the earliest possible point in the process of handling them. Several cases (like trying to resolve a package for __torch_package_0) are probably happening because the mangled prefix hasn't be removed before loading something. The only place mangled names should exist is in the name field of a module and the module field of classes. When those get loaded for use in our package infrastructure they should the be removed right away. Error reporting could further look into what the mangled name was to described what went wrong.

torch/package/exporter.py Outdated Show resolved Hide resolved
torch/package/_mangling.py Outdated Show resolved Hide resolved
torch/package/_custom_import_pickler.py Outdated Show resolved Hide resolved
torch/package/_custom_import_pickler.py Outdated Show resolved Hide resolved
torch/package/exporter.py Outdated Show resolved Hide resolved
torch/package/importer.py Outdated Show resolved Hide resolved
Rationale and implementation immortalized in a big comment in
_mangling.py

Differential Revision: [D25758625](https://our.internmc.facebook.com/intern/diff/D25758625)

[ghstack-poisoned]
suo added a commit that referenced this pull request Jan 5, 2021
Rationale and implementation immortalized in a big comment in
_mangling.py

ghstack-source-id: 18f6cd3ed80da0f5f74295e4cd5930779309e863
Pull Request resolved: #50049
Rationale and implementation immortalized in a big comment in
_mangling.py

Differential Revision: [D25758625](https://our.internmc.facebook.com/intern/diff/D25758625)

[ghstack-poisoned]
Rationale and implementation immortalized in a big comment in
_mangling.py

Differential Revision: [D25758625](https://our.internmc.facebook.com/intern/diff/D25758625)

[ghstack-poisoned]
suo added a commit that referenced this pull request Jan 8, 2021
Rationale and implementation immortalized in a big comment in
_mangling.py

ghstack-source-id: e91e29bf1c1d5b0d37e03168b80b6a2a9ff8f7a8
Pull Request resolved: #50049
@suo suo requested a review from zdevito January 8, 2021 19:15
Rationale and implementation immortalized in a big comment in
_mangling.py

Differential Revision: [D25758625](https://our.internmc.facebook.com/intern/diff/D25758625)

[ghstack-poisoned]
suo added a commit that referenced this pull request Jan 10, 2021
Rationale and implementation immortalized in a big comment in
_mangling.py

ghstack-source-id: 4d44af17adb3fadd34a50b5fcf34d13e267c5ed8
Pull Request resolved: #50049
Rationale and implementation immortalized in a big comment in
_mangling.py

Differential Revision: [D25758625](https://our.internmc.facebook.com/intern/diff/D25758625)

[ghstack-poisoned]
suo added a commit that referenced this pull request Jan 11, 2021
Rationale and implementation immortalized in a big comment in
`torch/package/mangling.md`.

This change also allows imported modules to be TorchScripted

ghstack-source-id: 051e34b6d2d1d14f54ff98683c58f532adc5764a
Pull Request resolved: #50049
Rationale and implementation immortalized in a big comment in
`torch/package/mangling.md`.

This change also allows imported modules to be TorchScripted

Differential Revision: [D25758625](https://our.internmc.facebook.com/intern/diff/D25758625)

[ghstack-poisoned]
Rationale and implementation immortalized in a big comment in
`torch/package/mangling.md`.

This change also allows imported modules to be TorchScripted

Differential Revision: [D25758625](https://our.internmc.facebook.com/intern/diff/D25758625)

[ghstack-poisoned]
suo added a commit that referenced this pull request Jan 11, 2021
Rationale and implementation immortalized in a big comment in
`torch/package/mangling.md`.

This change also allows imported modules to be TorchScripted

ghstack-source-id: 291d7304ed06574836c8c7ed042a2b55a808ef0c
Pull Request resolved: #50049
- `__module__`
- `__file__`
2. No mangled names should be serialized by `PackageExporter`.
3. Every entry point to `PackageImporter` and `PackageExporter` must
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like this rule should be that you must pass demanded names to the package importer and exporter. The surface area of the importer/exporter API is to large to try to maintain this invariant.

@@ -198,6 +228,7 @@ def _get_source_of_module(self, module: types.ModuleType) -> str:
return ''.join(result)

def require_module_if_not_provided(self, module_name: str, dependencies=True):
module_name = self._demangle(module_name)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like this demangling is coming too late. The only mangled properties are the __name__ fields of modules. I feel like the only safe place to put a demangle call is directly in the same function that looks up __name__ from the module. Otherwise, you get this big separation between when a mangled name is extract and when it is used. The only place we should want mangled names is in the __name__ field so that Python infra doesn't get confused about module lookup.

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

This change also allows imported modules to be TorchScripted

Differential Revision: [D25758625](https://our.internmc.facebook.com/intern/diff/D25758625)

[ghstack-poisoned]
Rationale and implementation immortalized in a big comment in
`torch/package/mangling.md`.

This change also allows imported modules to be TorchScripted

Differential Revision: [D25758625](https://our.internmc.facebook.com/intern/diff/D25758625)

[ghstack-poisoned]
Rationale and implementation immortalized in a big comment in
`torch/package/mangling.md`.

This change also allows imported modules to be TorchScripted

Differential Revision: [D25758625](https://our.internmc.facebook.com/intern/diff/D25758625)

[ghstack-poisoned]
Rationale and implementation immortalized in a big comment in
`torch/package/mangling.md`.

This change also allows imported modules to be TorchScripted

Differential Revision: [D25758625](https://our.internmc.facebook.com/intern/diff/D25758625)

[ghstack-poisoned]
suo added a commit that referenced this pull request Jan 12, 2021
Rationale and implementation immortalized in a big comment in
`torch/package/mangling.md`.

This change also allows imported modules to be TorchScripted

ghstack-source-id: ed64eaff1ba947d27e7cb79fad7818699c2ac46e
Pull Request resolved: #50049
@suo suo requested a review from zdevito January 12, 2021 23:33
Copy link
Contributor

@zdevito zdevito left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Only minor comments.

torch/package/_custom_import_pickler.py Outdated Show resolved Hide resolved
torch/package/_mangling.py Outdated Show resolved Hide resolved
torch/package/exporter.py Outdated Show resolved Hide resolved
@codecov
Copy link

codecov bot commented Jan 13, 2021

Codecov Report

Merging #50049 (f4226cd) into gh/suo/380/base (4a3a378) will decrease coverage by 0.00%.
The diff coverage is 96.42%.

@@                 Coverage Diff                 @@
##           gh/suo/380/base   #50049      +/-   ##
===================================================
- Coverage            80.73%   80.72%   -0.01%     
===================================================
  Files                 1910     1910              
  Lines               207182   207101      -81     
===================================================
- Hits                167267   167187      -80     
+ Misses               39915    39914       -1     

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

This change also allows imported modules to be TorchScripted

Differential Revision: [D25758625](https://our.internmc.facebook.com/intern/diff/D25758625)

[ghstack-poisoned]
Rationale and implementation immortalized in a big comment in
`torch/package/mangling.md`.

This change also allows imported modules to be TorchScripted

Differential Revision: [D25758625](https://our.internmc.facebook.com/intern/diff/D25758625)

[ghstack-poisoned]
suo added a commit that referenced this pull request Jan 13, 2021
Rationale and implementation immortalized in a big comment in
`torch/package/mangling.md`.

This change also allows imported modules to be TorchScripted

ghstack-source-id: 34c5650d71eda7155a33142f34558efc6b9ba901
Pull Request resolved: #50049
@facebook-github-bot
Copy link
Contributor

@suo merged this pull request in 0b49778.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants