-
Notifications
You must be signed in to change notification settings - Fork 26.3k
Implement sort for list of tuples #43448
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
Conversation
💊 CI failures summary and remediationsAs of commit 1f33e25 (more details on the Dr. CI page): ✅ None of the CI failures appear to be your fault 💚
❄️ 2 failures tentatively classified as flakybut reruns have not yet been triggered to confirm:
|
facebook-github-bot
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gmagogsfm has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you have any insight about the performance implications of this change? The sorting functions you removed were templated and converted the entire List<IValue> into a List<T> before sorting. The IValue comparison functions you added check the type tags of the IValue instances and call IValue::toXXX() on them while sorting. I think this is an probably an improvement in terms of code size (less template instantiations), but I'm curious about the runtime.
aten/src/ATen/core/ivalue.cpp
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if there is an opportunity for performance improvement by implementing this explicitly instead of in terms of == and <.
But this proposal comes at the cost of increasing code size. I don't have strong feelings either way, just something to think about.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point. Done.
Good point, I changed basic types' sort kernel back to template-based. Tuple sorting is still dependent on runtime type refining and type checking, so the comparison logic of IValue is still retained. |
facebook-github-bot
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gmagogsfm has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.
facebook-github-bot
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gmagogsfm has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, I think we need some small changes to the runtime op. Do we need to add the > operator ?
It's a little weird now how we allow sorting of basic types, tuples containing them, and also class types, but we don't allow sorting of tuples containing class types. The downside in the current approach with class types is you would have to look up the < function on each comparison, but i think that's probably fine ? I dont know how many models do much class sorting. If that is fine we should merge this implementation with the class impleemntation.
aten/src/ATen/core/ivalue.cpp
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we remove this ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://en.cppreference.com/w/cpp/algorithm/sort says std::sort only needs operator<, so yeah, maybe. 😮
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As mentioned in another comment, std::greater<> needs it for reverse sorting.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you rephrase this ? We can;t handle all tuples, just the tuples we can sort are only those comprised of the other types...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, maybe something like or Tuples of the aforementioned type.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, with the caveat that it isn't exactly true for tuple of classes. Let's discuss in the other comment thread.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we don't seem to be doing the reverse here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops, brainfarted. Really shouldn't have done this at midnight.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're checking copy_return_list three times in this function, can your refactor this function? it's a little hard to follow
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. Reduced to two times, each maps to one distinct action required.
aten/src/ATen/core/ivalue.cpp
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to define the > operator ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I am using std::greater<>() to implement reverse sorting, it relies on operator> .
gmagogsfm
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, I think we need some small changes to the runtime op. Do we need to add the
>operator ?It's a little weird now how we allow sorting of basic types, tuples containing them, and also class types, but we don't allow sorting of tuples containing class types. The downside in the current approach with class types is you would have to look up the
<function on each comparison, but i think that's probably fine ? I dont know how many models do much class sorting. If that is fine we should merge this implementation with the class impleemntation.
Right, tuple of classes can't be sorted due to how classes are handled (I assume that's what you mean by current approach?). I think the main benefit of looking up "<" function for each class is that objects of different class types can be compared against each other as long as they have lt implemented with right type. Do you mean this is an overhead that we should get rid of?
As for merging "this implementation with class implementation", do you mean we should unify every comparison logic into that of IValue? so they can arbitrarily nest?
aten/src/ATen/core/ivalue.cpp
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I am using std::greater<>() to implement reverse sorting, it relies on operator> .
aten/src/ATen/core/ivalue.cpp
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As mentioned in another comment, std::greater<> needs it for reverse sorting.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. Reduced to two times, each maps to one distinct action required.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops, brainfarted. Really shouldn't have done this at midnight.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, with the caveat that it isn't exactly true for tuple of classes. Let's discuss in the other comment thread.
Codecov Report
@@ Coverage Diff @@
## master #43448 +/- ##
=======================================
Coverage 68.07% 68.07%
=======================================
Files 384 384
Lines 49765 49765
=======================================
Hits 33879 33879
Misses 15886 15886 Continue to review full report at Codecov.
|
|
@gmagogsfm could you comment on the large changes ? Why did we have to add the |
This new change is actually still WIP. Sorry for pushing it to gh early, I wanted CI to run. Will update comment once some more changes are done. |
|
You're good, just trying to do my review requested queue. Removing my name for now you can re-request when its ready. |
Just pushed another revision. The main reason behind this change to to make tuples sortable even if there are other types nested in it.
Tests are still running but weekend is right ahead of us, so pushing it out to review now for some early comments. |
aten/src/ATen/core/ivalue.cpp
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here.
test/test_jit.py
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider adding a test case to test the error message for unsortability due to unsupported types or differing types in a nested tuple. e.g.
[(1, (2, {1: 2})), (1, (2, {1: 2})), (1, (0, {1: 2}))]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. Added a test for different types. Unsortable types is covered in an earlier test.
SplitInfinity
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All my comments have been addressed, thanks. I will defer to @eellison for approval.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Intentional?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, slightly clearer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Accident?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, fixed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Intentional?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, to fix a previous accident lol
eellison
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good, would be nice to refactor tuples and list sorting to reuse more logic
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we're double checking copy_return_list here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Discussed offline, this is slightly clearer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now that tuples & classes can compose, could we refactor this so we just have sortIValueListOrThrow or something along those lines ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's surprising to me that we have a separate isSortableTupleType - could we refactor this to isSortableType, and within the Tuple Case recurse on the type members?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similarly - this seems like a general restriction of list sorting, would be nice to refactor to remove tuple-specific casing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we try to refactor this with isSortableListofTuples?
facebook-github-bot
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gmagogsfm has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.
eellison
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! Thanks for making the changes. One small remaining nit
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we refactor isSortableTupleType to isSortableType and then just call that here instead of the two separate branches for Class / Tuple ? It would be a very small change - change input of isSortableTupleType to a single type instead of tuple_type and not iterate and it works
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is actually intentional with a very subtle reason.
If we use isSortableType which, by definition, returns true for other basic types (numbers, strings and tensors), that creates the hazard of someone mistakenly messed up schema matching of aten::sort.basic_types and we end up missing the chance of throwing an error here that clearly says type is incorrect because now we need to return true for them. Not a huge deal, but would like to know what you think on this.
facebook-github-bot
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gmagogsfm has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.
…bjects can be sorted,
facebook-github-bot
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gmagogsfm has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.
|
@gmagogsfm merged this pull request in 2558e57. |
Summary: * Implement tuple sort by traversing contained IValue types and generate a lambda function as comparator for sort. * Tuple, class objects can now arbitrarily nest within each other and still be sortable Fixes #43219 Pull Request resolved: #43448 Reviewed By: eellison Differential Revision: D23352273 Pulled By: gmagogsfm fbshipit-source-id: b6efa8d00e112178de8256da3deebdba7d06c0e1
Fixes #43219