Skip to content

Implement named tensor unflatten(dim, namedshape).#25658

Closed
zou3519 wants to merge 5 commits intogh/zou3519/138/basefrom
gh/zou3519/138/head
Closed

Implement named tensor unflatten(dim, namedshape).#25658
zou3519 wants to merge 5 commits intogh/zou3519/138/basefrom
gh/zou3519/138/head

Conversation

@zou3519
Copy link
Copy Markdown
Contributor

@zou3519 zou3519 commented Sep 4, 2019

Stack from ghstack:

This unflattens dim according to the shape specified in namedshape.
namedshape may be either an OrderedDict or an iterable of (name, size)
tuples.

Future:

  • It is possible to make it take a dict in Python >= 3.6 because those are
    ordered by default, but I'll leave that task for the future.

Test Plan:

  • new tests [namedtensor ci]

Differential Revision: D17192655

@pytorchbot pytorchbot added module: internals Related to internal abstractions in c10 and ATen module: operators labels Sep 4, 2019
zou3519 added a commit that referenced this pull request Sep 4, 2019
This unflattens `dim` according to the shape specified in `namedshape`.
`namedshape` may be either an OrderedDict or an iterable of (name, size)
tuples.

Future:
- It is possible to make it take a dict in Python >= 3.6 because those are
ordered by default, but I'll leave that task for the future.

Test Plan:
- new tests [namedtensor ci]

ghstack-source-id: 4d552f0
Pull Request resolved: #25658
@zou3519 zou3519 requested review from gchanan, izdeby and nairbv September 4, 2019 21:58
This unflattens `dim` according to the shape specified in `namedshape`.
`namedshape` may be either an OrderedDict or an iterable of (name, size)
tuples.

Future:
- It is possible to make it take a dict in Python >= 3.6 because those are
ordered by default, but I'll leave that task for the future.

Test Plan:
- new tests [namedtensor ci]

Differential Revision: [D17192655](https://our.internmc.facebook.com/intern/diff/D17192655)
zou3519 added a commit that referenced this pull request Sep 6, 2019
This unflattens `dim` according to the shape specified in `namedshape`.
`namedshape` may be either an OrderedDict or an iterable of (name, size)
tuples.

Future:
- It is possible to make it take a dict in Python >= 3.6 because those are
ordered by default, but I'll leave that task for the future.

Test Plan:
- new tests [namedtensor ci]

ghstack-source-id: 5be58c6
Pull Request resolved: #25658
This unflattens `dim` according to the shape specified in `namedshape`.
`namedshape` may be either an OrderedDict or an iterable of (name, size)
tuples.

Future:
- It is possible to make it take a dict in Python >= 3.6 because those are
ordered by default, but I'll leave that task for the future.

Test Plan:
- new tests [namedtensor ci]

Differential Revision: [D17192655](https://our.internmc.facebook.com/intern/diff/D17192655)
zou3519 added a commit that referenced this pull request Sep 9, 2019
This unflattens `dim` according to the shape specified in `namedshape`.
`namedshape` may be either an OrderedDict or an iterable of (name, size)
tuples.

Future:
- It is possible to make it take a dict in Python >= 3.6 because those are
ordered by default, but I'll leave that task for the future.

Test Plan:
- new tests [namedtensor ci]

ghstack-source-id: 3207bd8
Pull Request resolved: #25658
- func: align_tensors(Tensor[] tensors) -> Tensor[]
named_guard: False

- func: unflatten(Tensor self, Dimname dim, int[] sizes, DimnameList names) -> Tensor
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.

is there any close equivalent in numpy?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

No, in numpy one would use reshape. I did some research into the naming of this a while back, below is a summary:

In xarray (which is numpy + named axis), they use stack and unstack. The naming is based on pandas arrays' stack and unstack, which work in a similar manner.

In addition, Sasha Rush's namedtensor design uses stack and split.

Finally, salesforce/matchbox (autobatching with pytorch tensors) uses split_dim.

My proposal is to use flatten and unflatten instead of the above names. This is because stack and split already have semantic meaning: in pytorch, stack is an operation that takes a list of tensors and returns a new tensor; split is an operation that takes a tensor and splits it into multiple other tensors. Something like stack_dim and split_dim would make the semantics clearer, but since we already have flatten as a part of the API, unflatten is a reasonable counterpart.

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.

@zou3519 actually this probably also needs an entry in the docs

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

No named tensors docs yet, but they'll come before the release.

This unflattens `dim` according to the shape specified in `namedshape`.
`namedshape` may be either an OrderedDict or an iterable of (name, size)
tuples.

Future:
- It is possible to make it take a dict in Python >= 3.6 because those are
ordered by default, but I'll leave that task for the future.

Test Plan:
- new tests [namedtensor ci]

Differential Revision: [D17192655](https://our.internmc.facebook.com/intern/diff/D17192655)
zou3519 added a commit that referenced this pull request Sep 10, 2019
This unflattens `dim` according to the shape specified in `namedshape`.
`namedshape` may be either an OrderedDict or an iterable of (name, size)
tuples.

Future:
- It is possible to make it take a dict in Python >= 3.6 because those are
ordered by default, but I'll leave that task for the future.

Test Plan:
- new tests [namedtensor ci]

ghstack-source-id: 97f0895
Pull Request resolved: #25658
This unflattens `dim` according to the shape specified in `namedshape`.
`namedshape` may be either an OrderedDict or an iterable of (name, size)
tuples.

Future:
- It is possible to make it take a dict in Python >= 3.6 because those are
ordered by default, but I'll leave that task for the future.

Test Plan:
- new tests [namedtensor ci]

Differential Revision: [D17192655](https://our.internmc.facebook.com/intern/diff/D17192655)
zou3519 added a commit that referenced this pull request Sep 11, 2019
This unflattens `dim` according to the shape specified in `namedshape`.
`namedshape` may be either an OrderedDict or an iterable of (name, size)
tuples.

Future:
- It is possible to make it take a dict in Python >= 3.6 because those are
ordered by default, but I'll leave that task for the future.

Test Plan:
- new tests [namedtensor ci]

ghstack-source-id: 492d90f
Pull Request resolved: #25658
This unflattens `dim` according to the shape specified in `namedshape`.
`namedshape` may be either an OrderedDict or an iterable of (name, size)
tuples.

Future:
- It is possible to make it take a dict in Python >= 3.6 because those are
ordered by default, but I'll leave that task for the future.

Test Plan:
- new tests [namedtensor ci]

Differential Revision: [D17192655](https://our.internmc.facebook.com/intern/diff/D17192655)
zou3519 added a commit that referenced this pull request Sep 17, 2019
This unflattens `dim` according to the shape specified in `namedshape`.
`namedshape` may be either an OrderedDict or an iterable of (name, size)
tuples.

Future:
- It is possible to make it take a dict in Python >= 3.6 because those are
ordered by default, but I'll leave that task for the future.

Test Plan:
- new tests [namedtensor ci]

ghstack-source-id: 74c497c
Pull Request resolved: #25658
zdevito pushed a commit to zdevito/ATen that referenced this pull request Sep 18, 2019
Summary:
Pull Request resolved: pytorch/pytorch#25658

This unflattens `dim` according to the shape specified in `namedshape`.
`namedshape` may be either an OrderedDict or an iterable of (name, size)
tuples.

Future:
- It is possible to make it take a dict in Python >= 3.6 because those are
ordered by default, but I'll leave that task for the future.

Test Plan: - new tests [namedtensor ci]

Differential Revision: D17192655

Pulled By: zou3519

fbshipit-source-id: fd9bd2f462c23a4df1c23d66f2aa95076ff1b160
@facebook-github-bot
Copy link
Copy Markdown
Contributor

@zou3519 merged this pull request in 0038111.

@facebook-github-bot facebook-github-bot deleted the gh/zou3519/138/head branch October 28, 2019 22:23
laurentdupin pushed a commit to laurentdupin/pytorch that referenced this pull request Apr 24, 2026
Summary:
Pull Request resolved: pytorch#25658

This unflattens `dim` according to the shape specified in `namedshape`.
`namedshape` may be either an OrderedDict or an iterable of (name, size)
tuples.

Future:
- It is possible to make it take a dict in Python >= 3.6 because those are
ordered by default, but I'll leave that task for the future.

Test Plan: - new tests [namedtensor ci]

Differential Revision: D17192655

Pulled By: zou3519

fbshipit-source-id: fd9bd2f462c23a4df1c23d66f2aa95076ff1b160
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants