Skip to content

Better reshape with autograd support#82754

Closed
YifanShenSZ wants to merge 10 commits intopytorch:masterfrom
YifanShenSZ:nt-reshape
Closed

Better reshape with autograd support#82754
YifanShenSZ wants to merge 10 commits intopytorch:masterfrom
YifanShenSZ:nt-reshape

Conversation

@YifanShenSZ
Copy link
Copy Markdown
Contributor

@YifanShenSZ YifanShenSZ commented Aug 3, 2022

Previous reshape #80981 is ok for forward, but needs improvement for backward: need to handle "sometimes view sometimes copy" behavior.

This pull request fixes it by:

  1. add a new alias dispatch key CompositeImplicitAutogradNestedTensor, which ideally would work as nested-tensor version of CompositeImplicitAutograd
  2. register reshape_nested to reshape by CompositeImplicitAutogradNestedTensor

Side changes:

  • add contiguous memory format support to clone_nested
  • add view_nested
  • add reshape_as_nested

Fix issue #83041

@facebook-github-bot
Copy link
Copy Markdown
Contributor

facebook-github-bot commented Aug 3, 2022

🔗 Helpful links

❌ 1 New Failures, 7 Pending

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

Expand to see more
  • 1/1 failures introduced in this PR

🕵️‍♀️ 1 failure not recognized by patterns:

The following CI failures may be due to changes from the PR
Job Step
GitHub Actions macos-12-py3-arm64 / test (default, 2, 2, macos-m1-12) Unknown

This comment was automatically generated by Dr. CI (expand for details).

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

Click here to manually regenerate this comment.

@YifanShenSZ
Copy link
Copy Markdown
Contributor Author

@pytorchbot rebase

@pytorchmergebot
Copy link
Copy Markdown
Collaborator

@pytorchbot successfully started a rebase job. Check the current status here

@pytorchmergebot
Copy link
Copy Markdown
Collaborator

Successfully rebased nt-reshape onto refs/remotes/origin/master, please pull locally before adding more changes (for example, via git checkout nt-reshape && git pull --rebase)

@YifanShenSZ YifanShenSZ changed the title Better reshape with autograd support [Waiting for [#82658](https://github.com/pytorch/pytorch/pull/82658)] Better reshape with autograd support Aug 11, 2022
@YifanShenSZ YifanShenSZ changed the title [Waiting for [#82658](https://github.com/pytorch/pytorch/pull/82658)] Better reshape with autograd support [Waiting for https://github.com/pytorch/pytorch/pull/82658] Better reshape with autograd support Aug 11, 2022
@YifanShenSZ YifanShenSZ changed the title [Waiting for https://github.com/pytorch/pytorch/pull/82658] Better reshape with autograd support [Waiting for dependency] Better reshape with autograd support Aug 11, 2022
@YifanShenSZ YifanShenSZ marked this pull request as ready for review August 15, 2022 15:07
Comment thread aten/src/ATen/native/nested/NestedTensorMath.cpp Outdated
// efficient implementation of nested_size_tensor_ and nested_stride_tensor.
return wrap_buffer(buffer, sizemat.clone(), stridemat.clone(), offsets);
}
else if (memory_format == c10::MemoryFormat::Contiguous) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I would maybe add a comment that is the case when the memory format is contiguous but self is a non_contiguous nested tensor.

One thing I am trying to hash out now. I think all current nested tensor instances satisfy the bottom two are equal.

nested_tensor.numel() -> this is calculated from nested_sizes to have less elements than its underlying
storage -> nested_tensor.storage.nbytes()//dtype.

In theory one can easily create this tensor with wrap_buffer(), however I am not sure if that is done anywhere.

reshape, transpose, view / all current view ops appear to maintain this invariant but need to confirm.
@jbschlosser

Copy link
Copy Markdown
Contributor Author

@YifanShenSZ YifanShenSZ Aug 15, 2022

Choose a reason for hiding this comment

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

slice is an example to have nested_tensor.numel() < nested_tensor.storage.nbytes()//dtype

nt = torch.nested_tensor([x, y, z])
slice = nt[0 : 3 : 2]

where the memory of y is skipped

Speaking of slice, we can support those advanced indexing in dimension 0 now by extracting the rows of nested_size and nested_stride + the elements of offsets. @jbschlosser wdyt?

Comment thread aten/src/ATen/native/nested/NestedTensorMath.cpp Outdated
Copy link
Copy Markdown
Collaborator

@albanD albanD left a comment

Choose a reason for hiding this comment

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

separating adding the new alias key would allow you to land it without waiting for Driss's change.
I am not sure why view calls into reshape but didn't dive into this in details yet, maybe it is just a function name issue.

// to any of its backends.
// See Note [Undefined in dispatchTable_] for the special handling for Undefined.

if (dispatch_key != DispatchKey::Undefined && isIncludedInAlias(dispatch_key, DispatchKey::CompositeImplicitAutogradNestedTensor)) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

You most likely want to update the comment above.

@YifanShenSZ YifanShenSZ changed the title [Waiting for dependency] Better reshape with autograd support Better reshape with autograd support Aug 18, 2022
@YifanShenSZ YifanShenSZ marked this pull request as draft August 18, 2022 23:26
@YifanShenSZ YifanShenSZ marked this pull request as ready for review August 19, 2022 02:51
@YifanShenSZ YifanShenSZ requested review from albanD and drisspg August 19, 2022 02:52
Comment thread aten/src/ATen/native/nested/NestedTensorMath.cpp Outdated
Comment thread torchgen/native_function_generation.py Outdated
@bdhirsh
Copy link
Copy Markdown
Collaborator

bdhirsh commented Aug 24, 2022

Left some final comments on the codegen but otherwise I think this PR looks good!

@ngimel ngimel added the triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module label Aug 24, 2022
Copy link
Copy Markdown
Collaborator

@albanD albanD left a comment

Choose a reason for hiding this comment

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

SGTM
@drisspg I think we want to import this before landing it to see if internal changes are necessary. Can you take care of that?

return output;
}
else {
AT_ERROR("nested tensor clone supports Preserve and Contiguous memory foramts");
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Always use TORCH_CHECK*

@drisspg
Copy link
Copy Markdown
Contributor

drisspg commented Aug 25, 2022

SGTM @drisspg I think we want to import this before landing it to see if internal changes are necessary. Can you take care of that?

Will work through that now

@facebook-github-bot
Copy link
Copy Markdown
Contributor

@drisspg has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@facebook-github-bot
Copy link
Copy Markdown
Contributor

@drisspg has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@facebook-github-bot
Copy link
Copy Markdown
Contributor

@drisspg has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@YifanShenSZ
Copy link
Copy Markdown
Contributor Author

I'll leave it to you guys 😜

Copy link
Copy Markdown
Collaborator

@bdhirsh bdhirsh left a comment

Choose a reason for hiding this comment

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

nice work!

@drisspg should probably be the one to merge, since he's working on appeasing the internal build

drisspg pushed a commit to drisspg/pytorch that referenced this pull request Aug 31, 2022
Summary:
Pull Request resolved: pytorch#84154

Previous reshape [https://github.com/pytorch/pytorch/issues/80981](https://github.com/pytorch/pytorch/pull/80981) is ok for forward, but needs improvement for backward: need to handle "sometimes view sometimes copy" behavior.

This pull request fixes it by:
1. add a new alias dispatch key `CompositeImplicitAutogradNestedTensor`, which ideally would work as nested-tensor version of `CompositeImplicitAutograd`
2. register `reshape_nested` to `reshape` by `CompositeImplicitAutogradNestedTensor`

Side changes:
* add contiguous memory format support to `clone_nested`
* add `view_nested`
* add `reshape_as_nested`

Fix issue [https://github.com/pytorch/pytorch/issues/83041](https://github.com/pytorch/pytorch/issues/83041)

Pull Request resolved: pytorch#82754

Test Plan:
Imported from GitHub, without a `Test Plan:` line.

**Static Docs Preview: executorch**
|[Full Site](https://our.intern.facebook.com/intern/staticdocs/eph/D39023822/V26/executorch/)|

|**Modified Pages**|

**Static Docs Preview: executorch**
|[Full Site](https://our.intern.facebook.com/intern/staticdocs/eph/D39023822/V17/executorch/)|

|**Modified Pages**|

Reviewed By: albanD, bdhirsh

Differential Revision: D39023822

Pulled By: drisspg

fbshipit-source-id: 87acd4f9fb61cd094fccad7801c25e2a1bfed88b
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Sep 1, 2022

Hey @YifanShenSZ.
You've committed this PR, but it does not have both a 'release notes: ...' and 'topics: ...' label. Please add one of each to the PR. The 'release notes: ...' label should represent the part of PyTorch that this PR changes (fx, autograd, distributed, etc) and the 'topics: ...' label should represent the kind of PR it is (not user facing, new feature, bug fix, perf improvement, etc). The list of valid labels can be found here for the 'release notes: ...' and here for the 'topics: ...'.
For changes that are 'topic: not user facing' there is no need for a release notes label.

facebook-github-bot pushed a commit that referenced this pull request Sep 1, 2022
Summary:
Pull Request resolved: #84154

Previous reshape [https://github.com/pytorch/pytorch/issues/80981](https://github.com/pytorch/pytorch/pull/80981) is ok for forward, but needs improvement for backward: need to handle "sometimes view sometimes copy" behavior.

This pull request fixes it by:
1. add a new alias dispatch key `CompositeImplicitAutogradNestedTensor`, which ideally would work as nested-tensor version of `CompositeImplicitAutograd`
2. register `reshape_nested` to `reshape` by `CompositeImplicitAutogradNestedTensor`

Side changes:
* add contiguous memory format support to `clone_nested`
* add `view_nested`
* add `reshape_as_nested`

Fix issue [https://github.com/pytorch/pytorch/issues/83041](https://github.com/pytorch/pytorch/issues/83041)

Pull Request resolved: #82754

Test Plan:
Imported from GitHub, without a `Test Plan:` line.

**Static Docs Preview: executorch**
|[Full Site](https://our.intern.facebook.com/intern/staticdocs/eph/D39023822/V27/executorch/)|

|**Modified Pages**|

**Static Docs Preview: executorch**
|[Full Site](https://our.intern.facebook.com/intern/staticdocs/eph/D39023822/V17/executorch/)|

|**Modified Pages**|

Reviewed By: albanD, bdhirsh

Differential Revision: D39023822

Pulled By: drisspg

fbshipit-source-id: 872c81dc847d280366ef9f187f9b9bcb06aac73f
laurentdupin pushed a commit to laurentdupin/pytorch that referenced this pull request Apr 25, 2026
The original author is @YifanShenSZ  and the original PR is: pytorch#82754
# Summary:
Previous reshape [https://github.com/pytorch/pytorch/issues/80981](https://github.com/pytorch/pytorch/pull/80981) is ok for forward, but needs improvement for backward: need to handle "sometimes view sometimes copy" behavior.

This pull request fixes it by:
1. add a new alias dispatch key `CompositeImplicitAutogradNestedTensor`, which ideally would work as nested-tensor version of `CompositeImplicitAutograd`
2. register `reshape_nested` to `reshape` by `CompositeImplicitAutogradNestedTensor`

Side changes:
* add contiguous memory format support to `clone_nested`
* add `view_nested`
* add `reshape_as_nested`

Fix issue [https://github.com/pytorch/pytorch/issues/83041](https://github.com/pytorch/pytorch/issues/83041)

Pull Request resolved: pytorch#82754

Test Plan:
Imported from GitHub, without a `Test Plan:` line.

**Static Docs Preview: executorch**
|[Full Site](https://our.intern.facebook.com/intern/staticdocs/eph/D39023822/V13/executorch/)|

|**Modified Pages**|

Reviewed By: albanD

Differential Revision: D39023822

Pulled By: drisspg

Pull Request resolved: pytorch#84154
Approved by: https://github.com/bdhirsh, https://github.com/albanD
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/trunk Trigger trunk jobs on your pull request cla signed module: nestedtensor NestedTensor tag see issue #25032 open source release notes: nested tensor Changes that have a direct impact on nested tensors triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants