-
Notifications
You must be signed in to change notification settings - Fork 767
Make ElementComparison optional for dtypes
#4255
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
base: main
Are you sure you want to change the base?
Conversation
ElementComparison optional for dtypes
Codecov Report❌ Patch coverage is ❌ Your patch check has failed because the patch coverage (73.02%) is below the target coverage (80.00%). You can increase the patch coverage or adjust the target coverage. Additional details and impacted files@@ Coverage Diff @@
## main #4255 +/- ##
==========================================
- Coverage 69.03% 69.01% -0.03%
==========================================
Files 1409 1410 +1
Lines 165880 165941 +61
==========================================
+ Hits 114520 114524 +4
- Misses 51360 51417 +57 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
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.
Splitting Orderable from Numeric makes sense!
Backend implementations will still require float/int ops with orderable methods (e.g., float_greater) but can be left out as unimplemented, and for custom tensor kinds the API can be restricted so as to not expose the high-lever tensor methods that way.
Not sure why the changes to LibTorch elements were necessary for these changes though 🤔 we could accept different int elem types if we want to extend support, but unclear why it's needed for this PR specifically
And maybe Ordered could be a better name instead of Orderable?
| type FloatTensorPrimitive: TensorMetadata + 'static; | ||
| /// Default float element type. | ||
| type FloatElem: Element; | ||
| type FloatElem: Element + ElementComparison; |
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.
iirc ElementComparison was added specially for sorting ops, which have a default cpu implementation that operates on TensorData elements. We could likely remove the stricter ElementComparison bound, and only require ElementEquality, except for the data sorting implementation
burn/crates/burn-backend/src/backend/ops/sort.rs
Lines 370 to 373 in a969aad
| /// Compare two elements | |
| fn compare<E: ElementComparison>(a: &E, b: &E, descending: bool) -> Ordering { | |
| if descending { b.cmp(a) } else { a.cmp(b) } | |
| } |
Would have to check, but I believe everything else should still compile and work.
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.
iirc ElementComparison was added specially for sorting ops, which have a default cpu implementation that operates on TensorData elements. We could likely remove the stricter ElementComparison bound, and only require ElementEquality, except for the data sorting implementation
right now, {Float,Int}TensorOps bundles methods that rely on both Orderable and Numeric, hence the added constraint. How would removing the bound work? Would we just remove the ordering ops from {Float,Int}TensorOps traits and then leave it to the stuff defined under orderable, tensorData sort impl, or would we take a similar approach to what was done to numeric and define a float,int ordering trait that would be implemented directly on the end element types (just moving the existing methods to a new home)? or were you thinking of another approach entirely?
What should be done about the onehot methods that need both?
sort of. The first approach I took you would have issues converting from element types without ordering to element types with ordering. I left ordering off of bool tensors mainly to test that conversion still worked.
I can remove the constraint for int (since ElementComparison is already implemented for i64), but I had to add a type for to make it so that other element types didn't have the additional constraint applied to floats
sure |
So for making custom element types, not every type will have all the current properties required of elements. An example of this is the fact that complex numbers don't have a linear ordering. Outside of ordering alone, making it to where groups of ops are optional also lowers the amount of initial work necessary to add a new data type, or to make a new variant of an existing data type.
Checklist
cargo run-checkscommand has been executed.Related Issues/PRs
Changes
ElementOrdering) from ElementTesting
confirmed the project builds, still need to run the actual checks