Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Match NumPy by considering NaNs to be larger than any number when sorting#15886

Closed
umanwizard wants to merge 3 commits into
pytorch:masterfrom
umanwizard:sort_with_nans
Closed

Match NumPy by considering NaNs to be larger than any number when sorting#15886
umanwizard wants to merge 3 commits into
pytorch:masterfrom
umanwizard:sort_with_nans

Conversation

@umanwizard
Copy link
Copy Markdown
Contributor

Fixes #15764

@umanwizard umanwizard requested a review from soumith January 9, 2019 20:48
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.

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

@ailzhang
Copy link
Copy Markdown
Contributor

Thanks for working on this! With sort methods I think we also have problems with float('-inf'). Is it going to be in a separate PR btw?

@umanwizard
Copy link
Copy Markdown
Contributor Author

@ailzhang What is the problem with -inf ? It seems to work as I expect:

In [3]: x
Out[3]: tensor([3., -inf, 1.])

In [4]: x.sort()
Out[4]: (tensor([-inf, 1., 3.]), tensor([1, 2, 0]))

In [5]: x.sort(descending=True)
Out[5]: (tensor([3., 1., -inf]), tensor([0, 2, 1]))

@ailzhang
Copy link
Copy Markdown
Contributor

Actually the problem was indeed caused by nan. ninf is sorted correctly. Please ignore my comment then. Thanks @umanwizard !

Copy link
Copy Markdown
Contributor

@ailzhang ailzhang left a comment

Choose a reason for hiding this comment

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

LGTM! CI failures seem unrelated except the clang-tidy one. Good to go once CI is green.

@ngimel
Copy link
Copy Markdown
Collaborator

ngimel commented Jan 10, 2019

Both thrust and quicksort in TH require strict weak ordering comparator https://thrust.github.io/doc/group__sorting.html#ga3f47925d80f4970d5730051dba1c5603.

#define GT_OR_NAN(x, y) \
  ((x != x) || (x > y))

is not strict weak order.

@ailzhang
Copy link
Copy Markdown
Contributor

@ngimel yea I agree on the solution is not strict weak order. Hmmm why do we enforce strict weak ordering btw (could you give an example) ? Thanks!

@umanwizard
Copy link
Copy Markdown
Contributor Author

umanwizard commented Jan 10, 2019

@ngimel Thank you for the hint. If I understand correctly, I can fix this by making those macros/functions return false if both the arguments are NaN.

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.

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

zdevito pushed a commit to zdevito/ATen that referenced this pull request Jan 11, 2019
…ting (#15886)

Summary:
Fixes #15764
Pull Request resolved: pytorch/pytorch#15886

Differential Revision: D13612971

Pulled By: umanwizard

fbshipit-source-id: 91f552a25d1fd108f2f0b10e09a0ce0364f8c21e
@soumith soumith added the cherry-picked This PR was cherry-picked onto a release branch from master label Jan 17, 2019
soumith pushed a commit that referenced this pull request Jan 17, 2019
…ting (#15886)

Summary:
Fixes #15764
Pull Request resolved: #15886

Differential Revision: D13612971

Pulled By: umanwizard

fbshipit-source-id: 91f552a25d1fd108f2f0b10e09a0ce0364f8c21e
soumith pushed a commit that referenced this pull request Jan 18, 2019
…ting (#15886)

Summary:
Fixes #15764
Pull Request resolved: #15886

Differential Revision: D13612971

Pulled By: umanwizard

fbshipit-source-id: 91f552a25d1fd108f2f0b10e09a0ce0364f8c21e
@ezyang ezyang added the merged label Jun 25, 2019
laurentdupin pushed a commit to laurentdupin/pytorch that referenced this pull request Apr 24, 2026
…ting (pytorch#15886)

Summary:
Fixes pytorch#15764
Pull Request resolved: pytorch#15886

Differential Revision: D13612971

Pulled By: umanwizard

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

Labels

cherry-picked This PR was cherry-picked onto a release branch from master

Projects

None yet

Development

Successfully merging this pull request may close these issues.

torch.argsort descends wrongly

6 participants