-
Notifications
You must be signed in to change notification settings - Fork 5.7k
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
feature and fix: added support for casting modes to give the original… #23439
Conversation
… expected output dtype, also modified promote_types to accept None
Thanks for contributing to Ivy! 😊👏 |
ivy/func_wrapper.py
Outdated
@@ -1162,9 +1162,13 @@ def _wrap_function( | |||
return to_wrap | |||
|
|||
|
|||
def casting_modes_ops(fn): | |||
def casting_modes_ops(fn, dtype_determinator=None): |
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.
everything else looks great! let's probably rename the argument to ret_dtype_target
or something which might be more informative, what do you think?
I'll request @MahmoudAshraf97's review as well on this PR, thanks for making the changes @RickSanchezStoic 💪
ivy/func_wrapper.py
Outdated
if not to_cast and ret_dtype_target: | ||
for arg in ret_dtype_target: | ||
to_cast = ivy.promote_types( | ||
to_cast, | ||
ivy.dtype( | ||
args[arg_names.index(arg)] if arg not in kwargs else kwargs[arg] | ||
), | ||
) |
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 does not take into consideration when the inputs are python scalars or other objects that do not have a dtype
attribute, we should stick to using promote_types_of_inputs
to guarantee consistent results, also to prevent the existence of two identical promotion codes which will double the maintenance efforts
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.
another problem is that this doesn't take into consideration default values that are optional but still affect the output dtype
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.
promote_types_of_inputs
will directly modify the inputs, which shouldn't be done here. The args that are being read here are the ones explicitly passed to the decorator and are supposed to have a type attribute. Lastly, we don't have any promotion rules for scalars( or do we have that?) and if they really do affect the output dtype then we have a problem. Can you post an example that does that?
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.
Hey @MahmoudAshraf97 I remember having this conversation a couple of days back but don't fully remember the specifics, could you please share an example where this would be applicable? Thanks and apologies 😅
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 do have promotion rules for scalars included in ivy.promote_types_of_inputs
but not in the promotion table hence they are promoted to the default dtype in ivy.promote_types
which will lead to an inconsistent promotion results
ivy.promote_types(float_scalar, float16) = float32
ivy.promote_types_of_inputs(float_scalar, float16) = float16
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.
Actually ivy.promote_types
should always receive dtypes (ivy.Dtype
or ivy.NativeDtype
), so ideally the result from ivy.promote_types_of_inputs
is the one which would be used anyway. So ideally float16
would be the promoted dtype as a result 😄
I think we will need a method to test if this decorator is working as expected or not, my suggestion is to enable testing functions using their unsupported dtypes, this will guarentee consistency with frameworks that do support these dtypes |
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.
Just a minor change, rest looks good to me. I agree with @MahmoudAshraf97, would be great to have tests for these too. Also, does this cover all the other corner cases we were discussing? Thanks 😄
Hey @RickSanchezStoic , I guess you reverted my commit when you committed, can you please revise? |
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.
Just requested a few changes! Let's focus on the functions which use ivy.promote_types_of_inputs
for now for a version 0 as there isn't really an exhaustive way to test if there are issues in the output dtype for the other functions until we integrate casting modes into the tests (cc @sherry30), I'll also request @MahmoudAshraf97's review on this PR. Thanks @RickSanchezStoic 😄
ivy/func_wrapper.py
Outdated
if arg not in kwargs: | ||
args[arg_names.index(arg)] = ( | ||
arg_mod | ||
if not ivy.isarray(args[arg_names.index(arg)]) |
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 should be ivy.is_array
instead of ivy.isarray
ivy/func_wrapper.py
Outdated
else: | ||
kwargs[arg] = ( | ||
arg_mod | ||
if not ivy.isarray(args[arg_names.index(arg)]) |
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
ivy/func_wrapper.py
Outdated
@@ -1300,7 +1336,8 @@ def _wrapped(func): | |||
if "frontends" in func.__module__: | |||
# it's a frontend func, no casting modes for this | |||
return func | |||
return casting_modes_ops(func) | |||
|
|||
return casting_modes_ops(func, ret_dtype_target) |
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.
let's remove the blank line above this and pass ret_dtype_target
as a keyword argument
Thank you for this PR, here is the CI results: This pull request does not result in any additional test failures. Congratulations! |
added support for casting modes to give the original expected output type, also modified promote_types to accept None