Skip to content

Implement NumPy-like function torch.msort()#48440

Closed
Kiyosora wants to merge 7 commits intopytorch:masterfrom
Kiyosora:implement_msort
Closed

Implement NumPy-like function torch.msort()#48440
Kiyosora wants to merge 7 commits intopytorch:masterfrom
Kiyosora:implement_msort

Conversation

@Kiyosora
Copy link
Copy Markdown
Contributor

@Kiyosora Kiyosora commented Nov 25, 2020

@dr-ci
Copy link
Copy Markdown

dr-ci Bot commented Nov 25, 2020

💊 CI failures summary and remediations

As of commit 8428c25 (more details on the Dr. CI page):


💚 💚 Looks good so far! There are no failures yet. 💚 💚


This comment was automatically generated by Dr. CI (expand for details).Follow this link to opt-out of these comments for your Pull Requests.

Please report bugs/suggestions on the GitHub issue tracker or post in the (internal) Dr. CI Users group.

See how this bot performed.

This comment has been revised 37 times.

@Kiyosora Kiyosora force-pushed the implement_msort branch 3 times, most recently from bf0e607 to 7648bee Compare November 26, 2020 06:00
@Kiyosora Kiyosora changed the title [WIP] Implement NumPy-like function torch.msort() Implement NumPy-like function torch.msort() Nov 26, 2020
@mruberry mruberry self-requested a review November 27, 2020 06:43
@mruberry mruberry added the triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module label Nov 27, 2020
Comment thread aten/src/ATen/native/Sorting.cpp Outdated
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.

I appreciate that this is more consistent with PyTorch, but let's just return values to be consistent with 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.

Gotcha! Thanks for your advice!

Comment thread aten/src/ATen/native/Sorting.cpp Outdated
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.

With just values supported, msort_out won't take indices.

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.

Addressed!

Comment thread aten/src/ATen/native/Sorting.cpp Outdated
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.

Let's not implement the descending argument for now. NumPy's msort doesn't have one.

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.

Addressed!

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.

These look good but see comments above for tweaks.

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.

Addressed!

Comment thread test/test_namedtuple_return_api.py Outdated
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.

msort will be removed from this list once it returns only the tensor.

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.

Removed!

Comment thread test/test_namedtuple_return_api.py Outdated
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.

msort will be removed from this list, too, once it returns only the tensor

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.

Removed!

Comment thread test/test_torch.py Outdated
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.

PyTorch now requires NumPy to run the test suite, so we no longer need to test for it. This line can be removed.

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.

Removed!

Comment thread test/test_torch.py Outdated
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.

Add a 1D shape and an empty shape, too.

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.

Addressed! But it's seem that np.msort() does not support empty shape tensor, so I distinguish the judging condition of this situation from the general.

>>> import torch
>>> import numpy as np
>>> t=torch.empty([])
>>> t
tensor(0.)
>>> torch.msort(t)
tensor(0.)
>>> np.msort(t.cpu().numpy())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<__array_function__ internals>", line 5, in msort
  File "C:\Users\Medius Axman\anaconda3\envs\pytorch\lib\site-packages\numpy\lib\function_base.py", line 3374, in msort
    b.sort(0)
numpy.AxisError: axis 0 is out of bounds for array of dimension 0

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.

Thank you for noticing this. NumPy is inconsistent with its support of empty tensors, and it's great that we support them even when NumPy doesn't.

Comment thread torch/_tensor_docs.py Outdated
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.

This will need to be updated.

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.

Addressed!

Comment thread torch/_torch_docs.py Outdated
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.

This will need to be updated per the above comments

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.

Addressed!

Comment thread torch/_torch_docs.py Outdated
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.

This sentence an be removed.

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.

Removed!

Comment thread torch/_torch_docs.py Outdated
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.

This sentence can be removed.

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.

Removed!

Comment thread torch/_torch_docs.py Outdated
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.

This will need a small update to be equivalent to torch.sort(t, dim=0)[0]

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.

Addressed!

Comment thread torch/_torch_docs.py Outdated
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.

This can be removed

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.

Removed!

Comment thread torch/_torch_docs.py Outdated
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.

This can be updated to just be "{out}"

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.

Addressed!

Comment thread torch/_torch_docs.py Outdated
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.

When a name is needed for only a single tensor use "t". Print the matrix before sorting it so users can understand the before and after of sorting.

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.

Addressed!

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.

This case can be removed.

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.

Removed!

Comment thread torch/_torch_docs.py Outdated
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.

"along the first" -> "along its first"

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.

Addressed!

Copy link
Copy Markdown
Collaborator

@mruberry mruberry left a comment

Choose a reason for hiding this comment

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

Hey @Kiyosora! Thanks for implementing torch.msort. This looks great and I made a few comments for tweaks. The biggest change is that we can simplify the this implementation to be more consistent with NumPy. I appreciate your thoughtfulness in trying to be more consistent with PyTorch, but let's err on the side of caution for now.

@Kiyosora
Copy link
Copy Markdown
Contributor Author

Hey @Kiyosora, just a couple minor doc updates and a rebase needed since the test suite was recently refactored.

Thank you for noticing me about the refactoring of test case, @mruberry.
I have fixed my case for it, please take a look if it's convenience.

@Kiyosora Kiyosora requested a review from mruberry November 30, 2020 07:53
Copy link
Copy Markdown
Collaborator

@mruberry mruberry left a comment

Choose a reason for hiding this comment

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

Looks great! Thanks @Kiyosora!

Let me know if you would like a recommendation for another PR.

Copy link
Copy Markdown
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

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

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

@Kiyosora
Copy link
Copy Markdown
Contributor Author

Kiyosora commented Dec 2, 2020

Let me know if you would like a recommendation for another PR.

Thank you very much for reviewing this PR, @mruberry .
If there is any recommended issue, I'd like to be help! 😸

@mruberry
Copy link
Copy Markdown
Collaborator

mruberry commented Dec 3, 2020

I would like to recommend a the following options:

  • torch.frexp, analogous to NumPy's frexp. This is challenging because frexp returns two tensors, not one, and there are few functions in PyTorch that use TensorIterator and return two tensors. It may require building some new helpers on top of TensorIterator. If you're interested in developing TensorIterator architecture then this is a good pick.
  • torch.fmin and torch.fmax, analogous to NumPy's fmin and fmax. This is a pair of reductions with particular NaN handling. If you're interesting in learning more about reductions this is a good pick.
  • torch.interp, analogous to NumPy's interp. This is a unique function that could be interesting to implement. This is a good pick if you'd like to think carefully about an algorithm on both the CPU and CUDA.
  • torch.broadcast_to, analogous to NumPy's broadcast_to. This function is a good pick if you're interested in operations that manipulate a tensor's shape. It requires careful review of existing PyTorch operations to verify it's not an alias, and to see which existing PyTorch operations it can be efficiently composed of.

Do any of those sound interesting?

@Kiyosora
Copy link
Copy Markdown
Contributor Author

Kiyosora commented Dec 3, 2020

Do any of those sound interesting?

Wow, thanks so much for such detailed introduction, @mruberry. 👍

These issues all look very attractive, I can't wait to implement them now.

My good buddy (@RockingJavaBean) and I would start to work on them as soon, new PRs would be proposed in near future.

@mruberry
Copy link
Copy Markdown
Collaborator

mruberry commented Dec 3, 2020

Darn. Sorry @Kiyosora, tried to land this but we hit a merge conflict while it was landing. Would you rebase this, please?

@Kiyosora
Copy link
Copy Markdown
Contributor Author

Kiyosora commented Dec 4, 2020

Darn. Sorry @Kiyosora, tried to land this but we hit a merge conflict while it was landing. Would you rebase this, please?

Sure @mruberry , I've rebased the code. 👌

Copy link
Copy Markdown
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

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

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

@facebook-github-bot
Copy link
Copy Markdown
Contributor

@mruberry merged this pull request in 6ab84ca.

@Kiyosora Kiyosora deleted the implement_msort branch December 14, 2020 08:06
facebook-github-bot pushed a commit that referenced this pull request Jan 20, 2021
Summary:
- Implementing the NumPy-like function`torch.fmax()` and `torch.fmin()` recommended in #48440

Pull Request resolved: #49312

Reviewed By: izdeby

Differential Revision: D25887246

Pulled By: heitorschueroff

fbshipit-source-id: d762eeff8b328bfcbe7d48b7ee9d2da72c249691
laurentdupin pushed a commit to laurentdupin/pytorch that referenced this pull request Apr 24, 2026
Summary:
- Related with pytorch#38349
- Implementing the NumPy-like function `torch.msort()` .

Pull Request resolved: pytorch#48440

Reviewed By: bdhirsh

Differential Revision: D25265753

Pulled By: mruberry

fbshipit-source-id: 7709ac5e5667e7541a3dc9048b9c9896b1a6dfa1
laurentdupin pushed a commit to laurentdupin/pytorch that referenced this pull request Apr 24, 2026
)

Summary:
- Implementing the NumPy-like function`torch.fmax()` and `torch.fmin()` recommended in pytorch#48440

Pull Request resolved: pytorch#49312

Reviewed By: izdeby

Differential Revision: D25887246

Pulled By: heitorschueroff

fbshipit-source-id: d762eeff8b328bfcbe7d48b7ee9d2da72c249691
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla signed Merged open source 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.

4 participants