-
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
Builtins frontend #22007
Builtins frontend #22007
Conversation
Thanks for contributing to Ivy! 😊👏 |
ivy/functional/frontends/builtins/math/power_and_logarithmic_functions.py
Show resolved
Hide resolved
|
||
|
||
def _infer_return_array(x: Iterable) -> Callable: | ||
# get module path |
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 you add a docstring that explains what this decorator does overall? It is not very obvious when it should be used.
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 function should always be used within @to_ivy_arrays_and_back
decorator to infer the return array type. Let's consider the jax
frontend. In this case, if decorator receives jax.Array
, its frontend implementation or list
the output will always be converted to a frontend implementation of jax.Array
. However, in case of builtins
the behaviour changes to: almost_any_array -> ivy.Array -> almost_any_array
. In other words, we need to track what array to convert back to. So, if our built-in function recieves np.ndarray
or its frontend implementation, the output will be converted to np_frontend.ndarray
and so on. It also has a default behaviour. By default we convert the output to a frontend implementation of array which corresponds to current backend. This was done to cover the cases, when function accepts scalar arguments, but returns an array. For example, range
. But yeah I will add a docstring.
return x.ivy_array | ||
|
||
# convert native arrays and lists to ivy arrays | ||
elif isinstance(x, ivy.NativeArray): |
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.
If we want lists to get converted, we have to change this to else
.
I think however that ivy functions can already handle array-likes, in which case you would just need to change the comment, not the condition.
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 just forgot to modify the comment.
|
||
jax.config.update("jax_enable_x64", True) | ||
|
||
ret, frontend_array = fn(*args, **kwargs) |
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 way inputs_to_ivy_arrays
and outputs_to_frontend_arrays
have been designed right now they can't be used individually. They would only work when used together in to_ivy_arrays_and_back
.
E.g. ret, frontend_array = fn(*args, **kwargs)
would cause an error if fn
was an unwrapped frontend function because there would not be a frontend_array
returned.
…pes in outputs_to_frontend_arrays
|
||
|
||
def to_ivy_arrays_and_back(fn: Callable) -> Callable: | ||
return outputs_to_frontend_arrays(inputs_to_ivy_arrays(fn)) |
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.
- Make another wrapper inside here like:
@functools.wraps(fn)
def _to_ivy_arrays_and_back(*args, **kwargs):
- Move the
frontend_array = _infer_return_array(args[0]) if len(args) != 0 else None
there. - Call
inputs_to_ivy_arrays
, and then calloutputs_to_frontend_arrays
passingfrontend_array
. - You will need to add
frontend_array
as a new argument likeoutputs_to_frontend_arrays(fn: Callable, frontend_array: str)
.
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.
Thanks! I'll try this approach!
No description provided.