Skip to content

[jit] bind autograd.backward and tensor.backward in TorchScript #23913

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

Closed
wants to merge 12 commits into from

Conversation

wanchaol
Copy link
Collaborator

@wanchaol wanchaol commented Aug 6, 2019

Stack from ghstack:

Summary:
This PR bind torch.autograd.backward and tensor.backward to TorchScript,
and make aliasing to the conservative for these two ops, this is mainly
because backward op might write to every input tensors in the graph

Test Plan:

Reviewers:

Subscribers:

Tasks:

Tags:

Pull Request resolved: #23913

Differential Revision: D16923272

@pytorchbot pytorchbot added oncall: jit Add this issue/PR to JIT oncall triage queue module: internals Related to internal abstractions in c10 and ATen labels Aug 6, 2019
wanchaol added a commit that referenced this pull request Aug 6, 2019
Summary:

Test Plan:

Reviewers:

Subscribers:

Tasks:

Tags:

ghstack-source-id: acfaa44
Pull Request resolved: #23913
@wanchaol wanchaol requested a review from suo August 6, 2019 23:12
wanchaol added a commit that referenced this pull request Aug 7, 2019
Summary:

Test Plan:

Reviewers:

Subscribers:

Tasks:

Tags:

ghstack-source-id: 3955a6d
Pull Request resolved: #23913
@wanchaol wanchaol requested a review from zdevito August 8, 2019 22:22
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.

Alias analysis changes have some issues. There should be at least one test that ensures that optimization of grad tensors knows about the mutation of grad tensors.

@@ -665,6 +667,35 @@ void AliasDb::analyzeWait(Node* node) {
}
}

void AliasDb::analyzeBackward(Node* node) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This function doesn't handle a ton of aliasing cases. For instance, what if the grad tensor is stored in a list inside a class somewhere. A better way to do this would be to mark backward as CONSERVATIVE.

self.backward(gradient, keep_graph, create_graph);
return 0;
},
aliasAnalysisSpecialCase()),
Copy link
Contributor

Choose a reason for hiding this comment

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

Just put aliasAnalysisConservative here and this will be correct.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The alias_analaysis does not allow me to make it a conservative kind because of the assertion here. So what I did is that I made them the special_case kind and call analyze_conservative when handling these special cases, this happens before the assert so it should work properly.

Copy link
Contributor

Choose a reason for hiding this comment

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

That assertion is bogus, just remove it.

@@ -49,7 +49,16 @@ namespace jit {

namespace {

template<class T>
// XXX: This function is to specialize IValue for tensor type in
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a copy/paste that propagates a comment that is incorrect in this context. Let's just inline this instead:

auto gradient_ivalue = pop(stack);
auto gradient = gradient_ivalue.isNone() ? at::Tensor() : gradient_ivalue.toTensor()

Summary:

Test Plan:

Reviewers:

Subscribers:

Tasks:

Tags:

Pull Request resolved: #23913
Summary:

Test Plan:

Reviewers:

Subscribers:

Tasks:

Tags:

Pull Request resolved: #23913
wanchaol added a commit that referenced this pull request Aug 16, 2019
Summary:

Test Plan:

Reviewers:

Subscribers:

Tasks:

Tags:

ghstack-source-id: 15ba100
Pull Request resolved: #23913
Summary:

Test Plan:

Reviewers:

Subscribers:

Tasks:

Tags:

Pull Request resolved: #23913
wanchaol added a commit that referenced this pull request Aug 19, 2019
Summary:

Test Plan:

Reviewers:

Subscribers:

Tasks:

Tags:

ghstack-source-id: 6d44719
Pull Request resolved: #23913
Summary:

Test Plan:

Reviewers:

Subscribers:

Tasks:

Tags:

Pull Request resolved: #23913
wanchaol added a commit that referenced this pull request Aug 20, 2019
Summary:

Test Plan:

Reviewers:

Subscribers:

Tasks:

Tags:

ghstack-source-id: 04c2353
Pull Request resolved: #23913
Summary:

Test Plan:

Reviewers:

Subscribers:

Tasks:

Tags:

Pull Request resolved: #23913
wanchaol added a commit that referenced this pull request Aug 20, 2019
Summary:

Test Plan:

Reviewers:

Subscribers:

Tasks:

Tags:

ghstack-source-id: 15e5961
Pull Request resolved: #23913
…cript"

Summary:
This PR bind torch.autograd.backward and tensor.backward to TorchScript,
and make aliasing to the conservative for these two ops, this is mainly
because backward op might write to every input tensors in the graph

Test Plan:

Reviewers:

Subscribers:

Tasks:

Tags:

Pull Request resolved: #23913
@wanchaol wanchaol changed the title [jit] bind backward in TorchScript [jit] bind autograd.backward and tensor.backward in TorchScript Aug 20, 2019
@wanchaol wanchaol requested a review from zdevito August 20, 2019 19:09
…cript"

Summary:
This PR bind torch.autograd.backward and tensor.backward to TorchScript,
and make aliasing to the conservative for these two ops, this is mainly
because backward op might write to every input tensors in the graph

Test Plan:

Reviewers:

Subscribers:

Tasks:

Tags:

Pull Request resolved: #23913
wanchaol added a commit that referenced this pull request Aug 20, 2019
Summary:
This PR bind torch.autograd.backward and tensor.backward to TorchScript,
and make aliasing to the conservative for these two ops, this is mainly
because backward op might write to every input tensors in the graph

Test Plan:

Reviewers:

Subscribers:

Tasks:

Tags:

ghstack-source-id: 6db038e
Pull Request resolved: #23913
…cript"

Summary:
This PR bind torch.autograd.backward and tensor.backward to TorchScript,
and make aliasing to the conservative for these two ops, this is mainly
because backward op might write to every input tensors in the graph

Test Plan:

Reviewers:

Subscribers:

Tasks:

Tags:

Pull Request resolved: #23913

Differential Revision: [D16923272](https://our.internmc.facebook.com/intern/diff/D16923272)
wanchaol added a commit that referenced this pull request Aug 20, 2019
Summary:
This PR bind torch.autograd.backward and tensor.backward to TorchScript,
and make aliasing to the conservative for these two ops, this is mainly
because backward op might write to every input tensors in the graph

Test Plan:

Reviewers:

Subscribers:

Tasks:

Tags:

ghstack-source-id: c15fc38
Pull Request resolved: #23913
… in TorchScript"

Summary:
This PR bind torch.autograd.backward and tensor.backward to TorchScript,
and make aliasing to the conservative for these two ops, this is mainly
because backward op might write to every input tensors in the graph

Test Plan:

Reviewers:

Subscribers:

Tasks:

Tags:

Pull Request resolved: #23913

Differential Revision: [D16923272](https://our.internmc.facebook.com/intern/diff/D16923272)
wanchaol added a commit that referenced this pull request Aug 20, 2019
Summary:
This PR bind torch.autograd.backward and tensor.backward to TorchScript,
and make aliasing to the conservative for these two ops, this is mainly
because backward op might write to every input tensors in the graph

Test Plan:

Reviewers:

Subscribers:

Tasks:

Tags:

ghstack-source-id: 97385c2
Pull Request resolved: #23913
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.

This is good, but let's fix aliasAnalysisConservative rather than special casing backward.

… autograd.backward and tensor.backward in TorchScript"

Summary:
This PR bind torch.autograd.backward and tensor.backward to TorchScript,
and make aliasing to the conservative for these two ops, this is mainly
because backward op might write to every input tensors in the graph

Test Plan:

Reviewers:

Subscribers:

Tasks:

Tags:

Pull Request resolved: #23913

Differential Revision: [D16923272](https://our.internmc.facebook.com/intern/diff/D16923272)
wanchaol added a commit that referenced this pull request Aug 23, 2019
…n on "[jit] bind autograd.backward and tensor.backward in TorchScript"

Summary:
This PR bind torch.autograd.backward and tensor.backward to TorchScript,
and make aliasing to the conservative for these two ops, this is mainly
because backward op might write to every input tensors in the graph

Test Plan:

Reviewers:

Subscribers:

Tasks:

Tags:

Pull Request resolved: #23913

Differential Revision: [D16923272](https://our.internmc.facebook.com/intern/diff/D16923272)
…cript"

Summary:
This PR bind torch.autograd.backward and tensor.backward to TorchScript,
and make aliasing to the conservative for these two ops, this is mainly
because backward op might write to every input tensors in the graph

Test Plan:

Reviewers:

Subscribers:

Tasks:

Tags:

Pull Request resolved: #23913

Differential Revision: [D16923272](https://our.internmc.facebook.com/intern/diff/D16923272)
wanchaol added a commit that referenced this pull request Aug 23, 2019
…ckward in TorchScript"

Summary:
This PR bind torch.autograd.backward and tensor.backward to TorchScript,
and make aliasing to the conservative for these two ops, this is mainly
because backward op might write to every input tensors in the graph

Test Plan:

Reviewers:

Subscribers:

Tasks:

Tags:

Pull Request resolved: #23913

Differential Revision: [D16923272](https://our.internmc.facebook.com/intern/diff/D16923272)
…cript"

Summary:
This PR bind torch.autograd.backward and tensor.backward to TorchScript,
and make aliasing to the conservative for these two ops, this is mainly
because backward op might write to every input tensors in the graph

Test Plan:

Reviewers:

Subscribers:

Tasks:

Tags:

Pull Request resolved: #23913

Differential Revision: [D16923272](https://our.internmc.facebook.com/intern/diff/D16923272)
wanchaol added a commit that referenced this pull request Aug 23, 2019
…ckward in TorchScript"

Summary:
This PR bind torch.autograd.backward and tensor.backward to TorchScript,
and make aliasing to the conservative for these two ops, this is mainly
because backward op might write to every input tensors in the graph

Test Plan:

Reviewers:

Subscribers:

Tasks:

Tags:

Pull Request resolved: #23913

Differential Revision: [D16923272](https://our.internmc.facebook.com/intern/diff/D16923272)
wanchaol added a commit that referenced this pull request Aug 23, 2019
Summary:
This PR bind torch.autograd.backward and tensor.backward to TorchScript,
and make aliasing to the conservative for these two ops, this is mainly
because backward op might write to every input tensors in the graph

Test Plan:

Reviewers:

Subscribers:

Tasks:

Tags:

ghstack-source-id: d66873c
Pull Request resolved: #23913
@zou3519 zou3519 deleted the gh/wanchaol/45/head branch August 26, 2019 19:12
zdevito pushed a commit to zdevito/ATen that referenced this pull request Aug 26, 2019
Summary:
Pull Request resolved: pytorch/pytorch#23913

This PR bind torch.autograd.backward and tensor.backward to TorchScript,
and make aliasing to the conservative for these two ops, this is mainly
because backward op might write to every input tensors in the graph

Test Plan: Imported from OSS

Differential Revision: D16923272

fbshipit-source-id: 8a4016c62e00d00e0dee3d8c599d3aca220202f7
@facebook-github-bot
Copy link
Contributor

This pull request has been merged in 969c918.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Merged module: internals Related to internal abstractions in c10 and ATen oncall: jit Add this issue/PR to JIT oncall triage queue
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants