Better reshape with autograd support#82754
Better reshape with autograd support#82754YifanShenSZ wants to merge 10 commits intopytorch:masterfrom YifanShenSZ:nt-reshape
Conversation
🔗 Helpful links
❌ 1 New Failures, 7 PendingAs of commit 7c93475 (more details on the Dr. CI page): Expand to see more
🕵️♀️ 1 failure not recognized by patterns:The following CI failures may be due to changes from the PR
This comment was automatically generated by Dr. CI (expand for details).Please report bugs/suggestions to the (internal) Dr. CI Users group. |
|
@pytorchbot rebase |
|
@pytorchbot successfully started a rebase job. Check the current status here |
|
Successfully rebased |
| // 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) { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
albanD
left a comment
There was a problem hiding this comment.
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)) { |
There was a problem hiding this comment.
You most likely want to update the comment above.
|
Left some final comments on the codegen but otherwise I think this PR looks good! |
| return output; | ||
| } | ||
| else { | ||
| AT_ERROR("nested tensor clone supports Preserve and Contiguous memory foramts"); |
Will work through that now |
|
@drisspg has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator. |
|
@drisspg has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator. |
|
@drisspg has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator. |
|
I'll leave it to you guys 😜 |
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
|
Hey @YifanShenSZ. |
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
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
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:
CompositeImplicitAutogradNestedTensor, which ideally would work as nested-tensor version ofCompositeImplicitAutogradreshape_nestedtoreshapebyCompositeImplicitAutogradNestedTensorSide changes:
clone_nestedview_nestedreshape_as_nestedFix issue #83041