Skip to content

[FR] multidim squeeze and flatten #30702

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

Open
ssnl opened this issue Dec 4, 2019 · 7 comments
Open

[FR] multidim squeeze and flatten #30702

ssnl opened this issue Dec 4, 2019 · 7 comments
Labels
function request A request for a new function or the addition of new arguments/modes to an existing function. module: viewing and reshaping triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module

Comments

@ssnl
Copy link
Collaborator

ssnl commented Dec 4, 2019

squeeze

Previous FR for multidim squeeze: #1951

A PR (#4692) that attempted to implement #1951 was closed because

The behavior of this feature is very non-trivial, e.g., when negative and positive indices are mixed. I'm not sure it solves more problems that it adds anymore.

I would argue against that point, particularly for squeeze.

For squeeze, multidim is perfectly intuitive and defined, i.e., just squeezing the current dimensions the tuple refers. In fact, using a chain of squeeze (as suggested by the comment of #4692) is rather unintuitive.

If one has a tensor that has size 1 at dims 2 and 3, which they want to squeeze, it is far more intuitive to write

t.squeeze((2, 3))

than to write

t.squeeze(2).squeeze(2)

Negative indices does not add to complexity or confusion either.

flatten

Similarly, to merge the dims 1&2 and dims 3&4, it is far more intuitive to write

t.flatten([(1, 2), (3, 4)])

than

t.flatten(1,2).flatten(2,3)
@ssnl ssnl changed the title [FR] multidim squeeze [FR] multidim squeeze and flatten Dec 4, 2019
@zou3519 zou3519 added enhancement Not as big of a feature, but technically not a bug. Should be easy to fix module: operators triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module labels Dec 4, 2019
@zou3519
Copy link
Contributor

zou3519 commented Dec 4, 2019

I agree that it makes sense for squeeze and flatten to have these APIs. I would be happy to review PRs that enable this.

@vadimkantorov
Copy link
Contributor

vadimkantorov commented Jan 3, 2020

squeeze_multiple already exists somewhere in PyTorch

unsqueeze_multiple also exists

another instance of real-world unsqueeze_multiple:

grad = AD_unsqueeze_multiple(grad, dim, n_dims)

@vadimkantorov
Copy link
Contributor

I recently had a practical usecase for unsqueeze_multiple, had to implement it like this:
unsqueeze_multiple = lambda x, dim, k: functools.reduce(lambda acc, _: acc.unsqueeze(dim), range(k), x) (https://gist.github.com/vadimkantorov/ba3d3d099e403f2703facd2765625f27)

@ssnl
Copy link
Collaborator Author

ssnl commented May 15, 2020

Oh, so for unsqueeze_multiple, it is just inserting multiple dims at the same location, right? @vadimkantorov

@vadimkantorov
Copy link
Contributor

vadimkantorov commented May 15, 2020

Yup! Maybe this version better called unsqueeze_repeated. It was useful for forcing a tensor to have a given number of dimensions e.g. 4d

@mruberry mruberry added function request A request for a new function or the addition of new arguments/modes to an existing function. module: viewing and reshaping and removed enhancement Not as big of a feature, but technically not a bug. Should be easy to fix module: operators (deprecated) labels Oct 8, 2020
@vadimkantorov
Copy link
Contributor

vadimkantorov commented Feb 13, 2023

A recent related example: #94750

The code in that example is impl of mutlidim squeeze: x.squeeze(dim = (d1, d2, ...))

@vadimkantorov
Copy link
Contributor

vadimkantorov commented Jul 11, 2024

It appears that multidim squeeze is merged now :)
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
function request A request for a new function or the addition of new arguments/modes to an existing function. module: viewing and reshaping triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module
Projects
None yet
Development

No branches or pull requests

4 participants