Skip to content
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

LeakyRelu #13488 #13492

Merged
merged 20 commits into from
Apr 6, 2023
Merged

LeakyRelu #13488 #13492

merged 20 commits into from
Apr 6, 2023

Conversation

kaushal07wick
Copy link
Contributor

No description provided.

@ivy-leaves ivy-leaves added the TensorFlow Frontend Developing the TensorFlow Frontend, checklist triggered by commenting add_frontend_checklist label Mar 31, 2023
@kaushal07wick
Copy link
Contributor Author

Hi @sherry30 , I am trying to implement the LeakyRelu function for the tensorflow raw_ops.
Your, sugestion would be helpful to pass the tests locally.

@kaushal07wick
Copy link
Contributor Author

hey @sherry30 , while running the tests locally, I get this error

ivy_tests/test_ivy/test_frontends/test_tensorflow/test_raw_ops.py:13 (test_tensorflow_LeakyReLU[cpu-ivy.functional.backends.numpy-False-False])
@handle_frontend_test(
>       fn_tree="tensorflow.raw_ops.LeakyRelu",
        dtype_and_x=helpers.dtype_and_values(
            available_dtypes=helpers.get_dtypes("numeric"),
            min_num_dims=1,
        ),
        test_with_out=st.just(False),
    )

ivy_tests/test_ivy/test_frontends/test_tensorflow/test_raw_ops.py:15: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
ivy_tests/test_ivy/test_frontends/test_tensorflow/test_raw_ops.py:33: in test_tensorflow_LeakyReLU
    return helpers.test_frontend_function(
ivy_tests/test_ivy/helpers/function_testing.py:897: in test_frontend_function
    raise e
ivy_tests/test_ivy/helpers/function_testing.py:876: in test_frontend_function
    frontend_ret = frontend_fw.__dict__[fn_name](
/usr/local/lib/python3.8/dist-packages/tensorflow/python/util/tf_export.py:400: in wrapper
    return f(**kwargs)
/usr/local/lib/python3.8/dist-packages/tensorflow/python/ops/gen_nn_ops.py:5152: in leaky_relu
    _ops.raise_from_not_ok_status(e, name)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

e = _NotOkStatusException(), name = None

    def raise_from_not_ok_status(e, name):
      e.message += (" name: " + name if name is not None else "")
>     raise core._status_to_exception(e) from None  # pylint: disable=protected-access
E     tensorflow.python.framework.errors_impl.InvalidArgumentError: Value for attr 'T' of uint16 is not in the list of allowed values: half, bfloat16, float, double
E     	; NodeDef: {{node LeakyRelu}}; Op<name=LeakyRelu; signature=features:T -> activations:T; attr=alpha:float,default=0.2; attr=T:type,default=DT_FLOAT,allowed=[DT_HALF, DT_BFLOAT16, DT_FLOAT, DT_DOUBLE]> [Op:LeakyRelu]
E     Falsifying example: test_tensorflow_LeakyReLU(
E         dtype_and_x=(['uint16'], [array([0], dtype=uint16)]),
E         fn_tree='ivy.functional.frontends.tensorflow.raw_ops.LeakyRelu',
E         test_flags=num_positional_args=0. with_out=False. inplace=False. native_arrays=[False]. as_variable=[False]. generate_frontend_arrays=False. ,
E         frontend='tensorflow',
E         on_device='cpu',
E     )
E     
E     You can reproduce this example by temporarily adding @reproduce_failure('6.55.0', b'AAQBAAAAAAAAAAAAAA==') as a decorator on your test case

/usr/local/lib/python3.8/dist-packages/tensorflow/python/framework/ops.py:7164: InvalidArgumentError

Copy link
Contributor

@sherry30 sherry30 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi,

Your PR looks good but a few changes are required.
First off, There are lint errors introduced by your changes, can you fix those?

Other than that, I've requested changes on specific lines.

The error that you're facing seems to be because you're testing on numeric data types while this tf function only expects floats. So you can change this where you're generating data in the test function.

Hope this review was helpful, let me know if you need help with anything else 😃
Thanks

@@ -487,3 +487,7 @@ def relu6(features, name=None):
@to_ivy_arrays_and_back
def softmax(logits, axis=None, name=None):
return ivy.softmax(logits, axis=axis)


def leaky_relu(features, name=None):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tf.nn.leaky_relu also accepts alpha as an argument, you also have to pass that here

LeakyRelu = to_ivy_arrays_and_back(
map_raw_ops_alias(
tf_frontend.nn.leaky_relu,
kwargs_to_update={"x":"features"},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need for this line since raw_ops version of leaky_realu has the same argument name

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i will just remove this line right

@@ -3598,4 +3625,4 @@ def test_tensorflow_BatchMatMulV3(
Tout=Tout,
adj_x=adj_x,
adj_y=adj_y,
)
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

accidentally changed... ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah

on_device,
):
dtype, x = dtype_and_x
alpha = 0.2 # set the alpha value for LeakyReLU
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you shouldn't hard code this value of alpha for the test, you should generate this with hypothesis in the decorator above.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hey @sherry30 , I am getting various error while trying to generate data via hypothesis for the alpha variable.

kindly check on to it. can u just give me a rough outline.

@handle_frontend_test(
fn_tree="tensorflow.raw_ops.LeakyRelu",
dtype_and_x=helpers.dtype_and_values(
available_dtypes=helpers.get_dtypes("numeric"),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change numeric to float since tf.nn.leaky_relu only expects float data types

Copy link
Contributor

@sherry30 sherry30 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi,

Seems like this function hasn't been assigned to you in this ToDo list here. You can follow this doc to get it assigned.

Other than that, Can you remove other extra changes I specified on specific lines.
Also can you add a test for leaky_relu in the nn module since you added that function as well, It will be pretty similar to this one you've added for raw_ops version of leaky_relu.

Let me know if you have any other questions 😄

Thanks

@@ -3598,6 +3633,7 @@ def test_tensorflow_BatchMatMulV3(
Tout=Tout,
adj_x=adj_x,
adj_y=adj_y,

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this

@@ -3622,3 +3658,4 @@ def test_tensorflow_Size( # NOQA
input=x[0],
out_type=output_dtype,
)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this

@kaushal07wick
Copy link
Contributor Author

hey @sherry30 , can u tell me about the data generation for the alpha variable.

@kaushal07wick
Copy link
Contributor Author

Hi,

Seems like this function hasn't been assigned to you in this ToDo list here. You can follow this doc to get it assigned.

Other than that, Can you remove other extra changes I specified on specific lines. Also can you add a test for leaky_relu in the nn module since you added that function as well, It will be pretty similar to this one you've added for raw_ops version of leaky_relu.

Let me know if you have any other questions smile

Thanks

@sherry30 I made the comment for the issue assignment.

@kaushal07wick
Copy link
Contributor Author

@sherry30 I have made some changes, and tried to generate data for alpha but it shows error, should I make a composite strategy for it or would it be unnecessary.

@kaushal07wick
Copy link
Contributor Author

@sherry30 while testing the code, it showed this error.

            _print_traceback_history()
>           raise ivy.utils.exceptions.IvyBackendException(fn.__name__, str(e))
E           ivy.utils.exceptions.IvyBackendException: numpy: leaky_relu: leaky_relu() takes 1 positional argument but 2 were given
E           Falsifying example: test_tensorflow_LeakyReLU(
E               dtype_and_x=(['float64'], [array([-1.])]),
E               alpha=0.5,
E               test_flags=num_positional_args=0. with_out=False. inplace=False. native_arrays=[False]. as_variable=[False]. generate_frontend_arrays=False. ,
E               fn_tree='ivy.functional.frontends.tensorflow.raw_ops.LeakyRelu',
E               frontend='tensorflow',
E               on_device='cpu',
E           )

Copy link
Contributor

@sherry30 sherry30 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Kaushal,

A composite strategy would be unnecessary since this can be done by simply helpers.floats, let me know if that gives some errors.

The 2nd error you're getting is because ivy.leaky_relu doesn not accept alpha as a positional argument. to fix this I've requested change on specific line.

thanks

@@ -489,7 +489,11 @@ def softmax(logits, axis=None, name=None):
return ivy.softmax(logits, axis=axis)


def leaky_relu(features, alpha, name=None):
return ivy.leaky_relu(features, alpha)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be alpha=alpha on this line

@kaushal07wick
Copy link
Contributor Author

hi @sherry30 , after making the changes it passed all three tests (numpy, jax, tensorflow) but for the pytorch it failed and showed this error

>           raise ivy.utils.exceptions.IvyBackendException(fn.__name__, str(e))
E           ivy.utils.exceptions.IvyBackendException: torch: leaky_relu: "leaky_relu_cpu" not implemented for 'Half'
E           Falsifying example: test_tensorflow_LeakyReLU(
E               dtype_and_x=(['float16'], [array([-1.], dtype=float16)]),
E               alpha=0.5,
E               test_flags=num_positional_args=0. with_out=False. inplace=False. native_arrays=[False]. as_variable=[False]. generate_frontend_arrays=False. ,
E               fn_tree='ivy.functional.frontends.tensorflow.raw_ops.LeakyRelu',
E               frontend='tensorflow',
E               on_device='cpu',
E           )
E           

@kaushal07wick
Copy link
Contributor Author

Screenshot from 2023-04-04 18-49-18

Copy link
Contributor

@sherry30 sherry30 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi,

Just a few more changes requested. Can you also checkout the lint errors. there are some new introduced from your changes. Once this is done I'll merge it 👍

Thanks

@@ -15,6 +15,36 @@
)


@handle_frontend_test(
fn_tree="tensorflow.raw_ops.LeakyRelu",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this test function is supposed to test the leaky_relu in the nn module.
change it to that function.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

test_with_out=st.just(False),
alpha=helpers.floats(min_value=0, max_value=1)
)
def test_tensorflow_LeakyReLU( # NOQA
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change the name to test_tensorflow_leaky_relu

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

ivy/functional/frontends/tensorflow/nn.py Show resolved Hide resolved
@kaushal07wick
Copy link
Contributor Author

hey @sherry30 , I formatted the code, the lint doesn't shows any error while running locally, but here it shows error.
kindly check once. I don't find any such error.

@sherry30
Copy link
Contributor

sherry30 commented Apr 5, 2023

You can check the details of the lint error here and then open up Analysing the code with Flake8 and then you can search on the top right bar the file name of the the files you changed and fix them one by one

@kaushal07wick
Copy link
Contributor Author

You can check the details of the lint error here and then open up Analysing the code with Flake8 and then you can search on the top right bar the file name of the the files you changed and fix them one by one

I did that

@kaushal07wick
Copy link
Contributor Author

hey @sherry30 lint errors, from raw_ops and test_raw_ops are removed.
I think you can merge the PR.

Copy link
Contributor

@sherry30 sherry30 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright lgtm 👍

Thanks for the contribution 💪 and sorry for such a long process 😅

@sherry30 sherry30 merged commit 946d3e3 into ivy-llc:master Apr 6, 2023
@kaushal07wick
Copy link
Contributor Author

Alright lgtm +1

Thanks for the contribution muscle and sorry for such a long process sweat_smile

No worries, thanks @sherry30 for your help.

@kaushal07wick kaushal07wick mentioned this pull request Apr 6, 2023
MuhammedAshraf2020 pushed a commit to MuhammedAshraf2020/ivy that referenced this pull request Apr 9, 2023
Co-authored-by: sherry30 <sherrytst30@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
TensorFlow Frontend Developing the TensorFlow Frontend, checklist triggered by commenting add_frontend_checklist
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants