-
Notifications
You must be signed in to change notification settings - Fork 53
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
Update __truediv__ comment in python bindings. #3586
Conversation
!build |
// __div__. | ||
// In python, __truediv__ (/) always returns a float regardless of whether | ||
// the input arguments are float or integer. __truediv__ (/) corresponds with | ||
// pytorch torch.true_divide(a, b). The __div__ operator is depreciated in |
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.
// pytorch torch.true_divide(a, b). The __div__ operator is depreciated in | |
// pytorch torch.true_divide(a, b). The __div__ operator is deprecated in |
// In nvfuser, truediv function has the same semantics as python's | ||
// operator __truediv__ (/). The div function truncates the result instead | ||
// of promoting it to float, so it has the same semantics as the C++'s (/) | ||
// operator. In pytorch, torch.div(a, b, rounding_mode='trunc') corresponds | ||
// C-style integer division. |
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.
The first sentence here is incorrect I think. On Python 3.12, __truediv__
of ints promotes to double first and does no truncation:
[ins] In [1]: x = 3
[ins] In [2]: y = 4
[ins] In [3]: x / y
Out[3]: 0.75
[ins] In [4]: x.__truediv__(4)
Out[4]: 0.75
To get truncdiv in Python I think you need to manually truncate:
[ins] In [17]: ((-30)/4).__trunc__()
Out[17]: -7
[ins] In [18]: (-30)//4
Out[18]: -8
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.
How is the first sentence incorrect? It is what the comment says in csrc/ops/arith.h
// truediv: promote to float for integer division, has the same semantics as the
// python's operator /
https://github.com/NVIDIA/Fuser/blob/main/csrc/ops/arith.h#L426-L431
// div: don't promote to float, instead, truncate the result, this has the same
// semantics as the C++'s operator /
https://github.com/NVIDIA/Fuser/blob/main/csrc/ops/arith.h#L432-L437
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 updated the comment to directly mention csrc/ops/arith.h
.
We are changing the dunder __truediv__
in our python API to map to c-style division.
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!
// python's operator __truediv__ (/). The div function in csrc/ops/arith.h | ||
// truncates the result instead of promoting it to float. It has the same | ||
// semantics as the C++'s (/) operator. In pytorch, | ||
// torch.div(a, b, rounding_mode='trunc') corresponds C-style integerw |
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.
// torch.div(a, b, rounding_mode='trunc') corresponds C-style integerw | |
// torch.div(a, b, rounding_mode='trunc') corresponds C-style integer |
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 blame vim. 😄
!build |
This PR updates the comments for
__truediv__
operator defined in python bindings. The current comment does not reflect what the code actually does.Reference: #2837 (review)