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

Export TorchScript Classes as TorchScript code #46944

Open
alanhdu opened this issue Oct 27, 2020 · 3 comments
Open

Export TorchScript Classes as TorchScript code #46944

alanhdu opened this issue Oct 27, 2020 · 3 comments
Assignees
Labels
oncall: jit Add this issue/PR to JIT oncall triage queue
Projects

Comments

@alanhdu
Copy link
Contributor

alanhdu commented Oct 27, 2020

🚀 Feature

Add an option to export TorchScript classes as TorchScript code when torch.jit.save-ing a module. That way, the resulting file will be hermetically sealed, without relying on any particular code versioning to get it to execute.

Motivation

Right now, if you torch.jit.script a class, PyTorch relies on the actual class code to being available after torch.jit.load-ing the model for instance, in the code below I cannot actually execute my Module without having access to the A class

@torch.jit.script
class A:
    def __init__(self, x: int):
        self.x = x
class Module(torch.nn.Module):
    def forward(self):
        return A(1)
scripted = torch.jit.script(Module())
torch.jit.save(scripted, "test.pt")

This creates a really unfortunate coupling between the code defining the A class and the code need to run the Module class. This is also quite unintuitive, b/c scripted modules do not rely referencing code -- the code dependence seems only to beh ere for TorchScript non-module classes.

In our particular use case, we are using a TorchScript class as a "tuple but with some simple helper methods". Unfortunately, this dependence on our code is preventing us from refactoring our classes in our codebase (e.g. adding new fields) b/c of backwards compatibility with older models we've already serialized.

Pitch

Add an option to export the A class's code into the serialized torchscript module. This would allow torch.jit.script to produce hermetically sealed modules that don't need anything except PyTorch to execute correctly (which would massively simplify deployment for us).

Alternatives

If we don't do this, I think we should clearly document this behavior in the TorchScript classes section -- as I said earlier, b/c serializing a custom nn.Module does not require the underlying class definition, this code dependence came as a surprise to us.

cc @gmagogsfm

@facebook-github-bot facebook-github-bot added the oncall: jit Add this issue/PR to JIT oncall triage queue label Oct 27, 2020
@github-actions github-actions bot added this to Need triage in JIT Triage Oct 27, 2020
@tkuenzle
Copy link

This is the root cause for #42258. As @alanhdu mentioned, it works if the class is available when running the model. But this only works for Python and is a real blocker when running the model in another environment, eg. C++.

@byronyi
Copy link

byronyi commented Mar 18, 2021

Anyone’s working on this? If not I could take a stab.

@SplitInfinity
Copy link

I'm gradually polishing #44324 to close this.

SplitInfinity pushed a commit that referenced this issue Apr 16, 2021
…pt classes"


**Summary**
This commit adds reference semantics to TorchScript class types;
modifications made to them within TorchScript will be visible in Python.

**Test Plan**
This commit adds a unit test to `TestClassType` that checks that
modifications made to a class type instance passed into TorchScript are
visible in Python after executing the scripted function or module.

**Fixes**
This commit closes #41421, #46944.

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

[ghstack-poisoned]
SplitInfinity pushed a commit that referenced this issue Apr 16, 2021
**Summary**
This commit adds reference semantics to TorchScript class types;
modifications made to them within TorchScript will be visible in Python.

**Test Plan**
This commit adds a unit test to `TestClassType` that checks that
modifications made to a class type instance passed into TorchScript are
visible in Python after executing the scripted function or module.

**Fixes**
This commit closes #41421, #46944.

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

[ghstack-poisoned]
SplitInfinity pushed a commit that referenced this issue Apr 16, 2021
…pt classes"


**Summary**
This commit adds reference semantics to TorchScript class types;
modifications made to them within TorchScript will be visible in Python.

**Test Plan**
This commit adds a unit test to `TestClassType` that checks that
modifications made to a class type instance passed into TorchScript are
visible in Python after executing the scripted function or module.

**Fixes**
This commit closes #41421, #46944.

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

[ghstack-poisoned]
SplitInfinity pushed a commit that referenced this issue Apr 16, 2021
**Summary**
This commit adds reference semantics to TorchScript class types;
modifications made to them within TorchScript will be visible in Python.

**Test Plan**
This commit adds a unit test to `TestClassType` that checks that
modifications made to a class type instance passed into TorchScript are
visible in Python after executing the scripted function or module.

**Fixes**
This commit closes #41421, #46944.

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

[ghstack-poisoned]
SplitInfinity pushed a commit that referenced this issue Apr 20, 2021
…pt classes"


**Summary**
This commit adds reference semantics to TorchScript class types;
modifications made to them within TorchScript will be visible in Python.

**Test Plan**
This commit adds a unit test to `TestClassType` that checks that
modifications made to a class type instance passed into TorchScript are
visible in Python after executing the scripted function or module.

**Fixes**
This commit closes #41421, #46944.

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

[ghstack-poisoned]
SplitInfinity pushed a commit that referenced this issue Apr 20, 2021
**Summary**
This commit adds reference semantics to TorchScript class types;
modifications made to them within TorchScript will be visible in Python.

**Test Plan**
This commit adds a unit test to `TestClassType` that checks that
modifications made to a class type instance passed into TorchScript are
visible in Python after executing the scripted function or module.

**Fixes**
This commit closes #41421, #46944.

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

[ghstack-poisoned]
SplitInfinity pushed a commit that referenced this issue Apr 20, 2021
…pt classes"


**Summary**
This commit adds reference semantics to TorchScript class types;
modifications made to them within TorchScript will be visible in Python.

**Test Plan**
This commit adds a unit test to `TestClassType` that checks that
modifications made to a class type instance passed into TorchScript are
visible in Python after executing the scripted function or module.

**Fixes**
This commit closes #41421, #46944.

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

[ghstack-poisoned]
SplitInfinity pushed a commit that referenced this issue Apr 20, 2021
**Summary**
This commit adds reference semantics to TorchScript class types;
modifications made to them within TorchScript will be visible in Python.

**Test Plan**
This commit adds a unit test to `TestClassType` that checks that
modifications made to a class type instance passed into TorchScript are
visible in Python after executing the scripted function or module.

**Fixes**
This commit closes #41421, #46944.

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

[ghstack-poisoned]
SplitInfinity pushed a commit that referenced this issue Apr 21, 2021
…pt classes"


**Summary**
This commit adds reference semantics to TorchScript class types;
modifications made to them within TorchScript will be visible in Python.

**Test Plan**
This commit adds a unit test to `TestClassType` that checks that
modifications made to a class type instance passed into TorchScript are
visible in Python after executing the scripted function or module.

**Fixes**
This commit closes #41421, #46944.

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

[ghstack-poisoned]
SplitInfinity pushed a commit that referenced this issue Apr 21, 2021
**Summary**
This commit adds reference semantics to TorchScript class types;
modifications made to them within TorchScript will be visible in Python.

**Test Plan**
This commit adds a unit test to `TestClassType` that checks that
modifications made to a class type instance passed into TorchScript are
visible in Python after executing the scripted function or module.

**Fixes**
This commit closes #41421, #46944.

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

[ghstack-poisoned]
SplitInfinity pushed a commit that referenced this issue Apr 21, 2021
…pt classes"


**Summary**
This commit adds reference semantics to TorchScript class types;
modifications made to them within TorchScript will be visible in Python.

**Test Plan**
This commit adds a unit test to `TestClassType` that checks that
modifications made to a class type instance passed into TorchScript are
visible in Python after executing the scripted function or module.

**Fixes**
This commit closes #41421, #46944.

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

[ghstack-poisoned]
SplitInfinity pushed a commit that referenced this issue Apr 21, 2021
**Summary**
This commit adds reference semantics to TorchScript class types;
modifications made to them within TorchScript will be visible in Python.

**Test Plan**
This commit adds a unit test to `TestClassType` that checks that
modifications made to a class type instance passed into TorchScript are
visible in Python after executing the scripted function or module.

**Fixes**
This commit closes #41421, #46944.

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

[ghstack-poisoned]
SplitInfinity pushed a commit that referenced this issue Apr 29, 2021
…pt classes"


**Summary**
This commit adds reference semantics to TorchScript class types;
modifications made to them within TorchScript will be visible in Python.

**Test Plan**
This commit adds a unit test to `TestClassType` that checks that
modifications made to a class type instance passed into TorchScript are
visible in Python after executing the scripted function or module.

**Fixes**
This commit closes #41421, #46944.

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

[ghstack-poisoned]
SplitInfinity pushed a commit that referenced this issue Apr 29, 2021
**Summary**
This commit adds reference semantics to TorchScript class types;
modifications made to them within TorchScript will be visible in Python.

**Test Plan**
This commit adds a unit test to `TestClassType` that checks that
modifications made to a class type instance passed into TorchScript are
visible in Python after executing the scripted function or module.

**Fixes**
This commit closes #41421, #46944.

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

[ghstack-poisoned]
SplitInfinity pushed a commit that referenced this issue Apr 29, 2021
…pt classes"


**Summary**
This commit adds reference semantics to TorchScript class types;
modifications made to them within TorchScript will be visible in Python.

**Test Plan**
This commit adds a unit test to `TestClassType` that checks that
modifications made to a class type instance passed into TorchScript are
visible in Python after executing the scripted function or module.

**Fixes**
This commit closes #41421, #46944.

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

[ghstack-poisoned]
SplitInfinity pushed a commit that referenced this issue Apr 29, 2021
**Summary**
This commit adds reference semantics to TorchScript class types;
modifications made to them within TorchScript will be visible in Python.

**Test Plan**
This commit adds a unit test to `TestClassType` that checks that
modifications made to a class type instance passed into TorchScript are
visible in Python after executing the scripted function or module.

**Fixes**
This commit closes #41421, #46944.

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

[ghstack-poisoned]
SplitInfinity pushed a commit that referenced this issue May 4, 2021
…pt classes"


**Summary**
This commit adds reference semantics to TorchScript class types;
modifications made to them within TorchScript will be visible in Python.

**Test Plan**
This commit adds a unit test to `TestClassType` that checks that
modifications made to a class type instance passed into TorchScript are
visible in Python after executing the scripted function or module.

**Fixes**
This commit closes #41421, #46944.

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

[ghstack-poisoned]
SplitInfinity pushed a commit that referenced this issue May 4, 2021
**Summary**
This commit adds reference semantics to TorchScript class types;
modifications made to them within TorchScript will be visible in Python.

**Test Plan**
This commit adds a unit test to `TestClassType` that checks that
modifications made to a class type instance passed into TorchScript are
visible in Python after executing the scripted function or module.

**Fixes**
This commit closes #41421, #46944.

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

[ghstack-poisoned]
SplitInfinity pushed a commit that referenced this issue May 4, 2021
…pt classes"


**Summary**
This commit adds reference semantics to TorchScript class types;
modifications made to them within TorchScript will be visible in Python.

**Test Plan**
This commit adds a unit test to `TestClassType` that checks that
modifications made to a class type instance passed into TorchScript are
visible in Python after executing the scripted function or module.

**Fixes**
This commit closes #41421, #46944.

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

[ghstack-poisoned]
SplitInfinity pushed a commit that referenced this issue May 4, 2021
**Summary**
This commit adds reference semantics to TorchScript class types;
modifications made to them within TorchScript will be visible in Python.

**Test Plan**
This commit adds a unit test to `TestClassType` that checks that
modifications made to a class type instance passed into TorchScript are
visible in Python after executing the scripted function or module.

**Fixes**
This commit closes #41421, #46944.

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

[ghstack-poisoned]
SplitInfinity pushed a commit that referenced this issue Jun 28, 2021
…pt classes"


**Summary**
This commit adds reference semantics to TorchScript class types;
modifications made to them within TorchScript will be visible in Python.

**Test Plan**
This commit adds a unit test to `TestClassType` that checks that
modifications made to a class type instance passed into TorchScript are
visible in Python after executing the scripted function or module.

**Fixes**
This commit closes #41421, #46944.

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

[ghstack-poisoned]
SplitInfinity pushed a commit that referenced this issue Jun 28, 2021
**Summary**
This commit adds reference semantics to TorchScript class types;
modifications made to them within TorchScript will be visible in Python.

**Test Plan**
This commit adds a unit test to `TestClassType` that checks that
modifications made to a class type instance passed into TorchScript are
visible in Python after executing the scripted function or module.

**Fixes**
This commit closes #41421, #46944.

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

[ghstack-poisoned]
SplitInfinity pushed a commit that referenced this issue Jun 29, 2021
…pt classes"


**Summary**
This commit adds reference semantics to TorchScript class types;
modifications made to them within TorchScript will be visible in Python.

**Test Plan**
This commit adds a unit test to `TestClassType` that checks that
modifications made to a class type instance passed into TorchScript are
visible in Python after executing the scripted function or module.

**Fixes**
This commit closes #41421, #46944.

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

[ghstack-poisoned]
SplitInfinity pushed a commit that referenced this issue Jun 29, 2021
**Summary**
This commit adds reference semantics to TorchScript class types;
modifications made to them within TorchScript will be visible in Python.

**Test Plan**
This commit adds a unit test to `TestClassType` that checks that
modifications made to a class type instance passed into TorchScript are
visible in Python after executing the scripted function or module.

**Fixes**
This commit closes #41421, #46944.

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

[ghstack-poisoned]
SplitInfinity pushed a commit that referenced this issue Jun 29, 2021
…pt classes"


**Summary**
This commit adds reference semantics to TorchScript class types;
modifications made to them within TorchScript will be visible in Python.

**Test Plan**
This commit adds a unit test to `TestClassType` that checks that
modifications made to a class type instance passed into TorchScript are
visible in Python after executing the scripted function or module.

**Fixes**
This commit closes #41421, #46944.

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

[ghstack-poisoned]
SplitInfinity pushed a commit that referenced this issue Jun 29, 2021
**Summary**
This commit adds reference semantics to TorchScript class types;
modifications made to them within TorchScript will be visible in Python.

**Test Plan**
This commit adds a unit test to `TestClassType` that checks that
modifications made to a class type instance passed into TorchScript are
visible in Python after executing the scripted function or module.

**Fixes**
This commit closes #41421, #46944.

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

[ghstack-poisoned]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
oncall: jit Add this issue/PR to JIT oncall triage queue
Projects
JIT Triage
  
Pending
Development

No branches or pull requests

5 participants