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

feat: gradient numpy frontend #23104

Merged
merged 13 commits into from
Mar 3, 2024
Merged

feat: gradient numpy frontend #23104

merged 13 commits into from
Mar 3, 2024

Conversation

sineha17
Copy link
Contributor

@sineha17 sineha17 commented Sep 5, 2023

added gradient function to numpy frontend

out of 5 test cases, 4 pass successfully. paddle backend function expects a tensor attribute but the numpy gradient function uses an array. i'm unable to convert the array to a tensor element for the paddle test case without affecting the input datatype of the other test cases.

please provide some insight on how to fix this error.

close #23053

Checklist

  • Did you add a function?
  • Did you add the tests?
  • Did you follow the steps we provided?

Socials:

@github-actions
Copy link
Contributor

github-actions bot commented Sep 5, 2023

Thanks for contributing to Ivy! 😊👏
Here are some of the important points from our Contributing Guidelines 📝:
1. Feel free to ignore the run_tests (1), run_tests (2), … jobs, and only look at the display_test_results job. 👀 It contains the following two sections:
- Combined Test Results: This shows the results of all the ivy tests that ran on the PR. ✔️
- New Failures Introduced: This lists the tests that are passing on main, but fail on the PR Fork. Please try to make sure that there are no such tests. 💪
2. The lint / Check formatting / check-formatting tests check for the formatting of your code. 📜 If it fails, please check the exact error message in the logs and fix the same. ⚠️🔧
3. Finally, the test-docstrings / run-docstring-tests check for the changes made in docstrings of the functions. This may be skipped, as well. 📚
Happy coding! 🎉👨‍💻

@ivy-leaves ivy-leaves added the NumPy Frontend Developing the NumPy Frontend, checklist triggered by commenting add_frontend_checklist label Sep 5, 2023
@ivy-leaves
Copy link

If you are working on an open task, please edit the PR description to link to the issue you've created.

For more information, please check ToDo List Issues Guide.

Thank you 🤗

@handle_frontend_test(
fn_tree="numpy.gradient",
dtype_input_axis=helpers.dtype_values_axis(
available_dtypes=("float32", "float16", "float64"),
Copy link
Contributor

Choose a reason for hiding this comment

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

the available dtypes should be a strategy that gets all valid dtypes, you should then set the supported dtypes for the function using one of the decorators specified here https://unify.ai/docs/ivy/overview/deep_dive/data_types.html#supported-and-unsupported-data-types

Copy link
Contributor

Choose a reason for hiding this comment

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

please add support for edge_order argument in testing

Comment on lines +385 to +388
varargs=helpers.ints(
min_value=-3,
max_value=3,
),
Copy link
Contributor

Choose a reason for hiding this comment

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

the varargs argument can be one of the following as per the function documentation:

  1. single scalar to specify a sample distance for all dimensions.
  2. N scalars to specify a constant sample distance for each dimension.
    i.e. dx, dy, dz, ...
  3. N arrays to specify the coordinates of the values along each
    dimension of F. The length of the array must match the size of
    the corresponding dimension
  4. Any combination of N scalars/arrays with the meaning of 2. and 3.

so please extend the testing strategy to accommodate these cases

Copy link
Contributor Author

Choose a reason for hiding this comment

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

okay, i'm working on it. thank you

@sineha17
Copy link
Contributor Author

On adding edge_order argument in testing, an exception is raised stating edge_order should not be greater than axis.shape. For example, if axis=0 and edge_order=1 (default value), there is no error produced but in case of axis=0 and edge_order=2, the said exception is raised. For multidimentional arrays the shape of axis will be greater than 1 and in this case edge_order=2 does not produce any error.
I do not know how to restrict the value of edge_order to be less than axis size in the frontend. So far, I've noticed that not adding the edge_order argument in testing and passing only the default value (edge_order=1) is not throwing any error.

@sineha17
Copy link
Contributor Author

in case of paddle backend, the following error occurs.

ivy.utils.exceptions.IvyValueError: paddle: gradient: paddle: prod: (InvalidArgument) prod(): argument 'x' (position 0) must be Tensor, but got Array (at /paddle/paddle/fluid/pybind/eager_utils.cc:1005)

I have tried adding function decorators which convert numpy array to tensor, also tried importing ivy.paddle/ivy.torch in test file and calling the funtion paddle.tensor/torch.to_tensor inside an if block when backend=='paddle' and still could not resolve this error.

@MahmoudAshraf97
Copy link
Contributor

On adding edge_order argument in testing, an exception is raised stating edge_order should not be greater than axis.shape. For example, if axis=0 and edge_order=1 (default value), there is no error produced but in case of axis=0 and edge_order=2, the said exception is raised. For multidimentional arrays the shape of axis will be greater than 1 and in this case edge_order=2 does not produce any error. I do not know how to restrict the value of edge_order to be less than axis size in the frontend. So far, I've noticed that not adding the edge_order argument in testing and passing only the default value (edge_order=1) is not throwing any error.

to fix this, you might need to write a more complex testing strategy that samples edge_order based on the shape and the axis argument.

in case of paddle backend, the following error occurs.

ivy.utils.exceptions.IvyValueError: paddle: gradient: paddle: prod: (InvalidArgument) prod(): argument 'x' (position 0) must be Tensor, but got Array (at /paddle/paddle/fluid/pybind/eager_utils.cc:1005)

I have tried adding function decorators which convert numpy array to tensor, also tried importing ivy.paddle/ivy.torch in test file and calling the funtion paddle.tensor/torch.to_tensor inside an if block when backend=='paddle' and still could not resolve this error.

can you please share the entire error trace?

@sineha17
Copy link
Contributor Author

sineha17 commented Sep 19, 2023

ivy_tests/test_ivy/test_frontends/test_numpy/test_mathematical_functions/test_miscellaneous.py:375: in test_numpy_gradient
    fn_tree="numpy.gradient",
ivy_tests/test_ivy/test_frontends/test_numpy/test_mathematical_functions/test_miscellaneous.py:404: in test_numpy_gradient
    helpers.test_frontend_function(
ivy_tests/test_ivy/helpers/function_testing.py:747: in test_frontend_function
    ret = get_frontend_ret(
ivy_tests/test_ivy/helpers/function_testing.py:2409: in get_frontend_ret
    with ivy_backend.PreciseMode(precision_mode):
ivy/functional/ivy/general.py:86: in __exit__
    raise exc_val
ivy_tests/test_ivy/helpers/function_testing.py:2410: in get_frontend_ret
    ret = frontend_fn(*args, **kwargs)
ivy/functional/frontends/numpy/func_wrapper.py:502: in _outputs_to_frontend_arrays
    ret = fn(*args, **kwargs)
ivy/functional/frontends/numpy/func_wrapper.py:454: in _inputs_to_ivy_arrays_np
    return fn(*ivy_args, **ivy_kwargs)
ivy/functional/frontends/numpy/mathematical_functions/miscellaneous.py:340: in gradient
    return ivy.gradient(f, spacing=varargs, axis=axis, edge_order=edge_order)
ivy/utils/exceptions.py:362: in _handle_exceptions
    _handle_exceptions_helper(e, type(e))
ivy/utils/exceptions.py:371: in _handle_exceptions_helper
    raise cls(fn.__name__, str(e), include_backend=True)
E   ivy.utils.exceptions.IvyValueError: paddle: gradient: paddle: prod: (InvalidArgument) prod(): argument 'x' (position 0) must be Tensor, but got Array (at /paddle/paddle/fluid/pybind/eager_utils.cc:1005)
E   
E   Falsifying example: test_numpy_gradient(
E       frontend='numpy',
E       backend_fw='paddle',
E       on_device='cpu',
E       dtype_input_axis=(['float64'], [array([-1., -1.])], 0),
E       varargs=0,
E       fn_tree='ivy.functional.frontends.numpy.gradient',
E       test_flags=FrontendFunctionTestFlags(
E           num_positional_args=0,
E           with_out=False,
E           inplace=False,
E           as_variable=[False],
E           native_arrays=[False],
E           test_compile=False,
E           generate_frontend_arrays=False,
E           transpile=False,
E           precision_mode=False,
E       ),
E   )```

@MahmoudAshraf97 MahmoudAshraf97 removed their request for review October 1, 2023 21:26
@MahmoudAshraf97
Copy link
Contributor

The problem is solved now, please sync your branch with main and try again

@sineha17
Copy link
Contributor Author

sineha17 commented Oct 4, 2023

The problem is solved now, please sync your branch with main and try again

Will do, thank you for the update.

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

PR Compliance Checks

Thank you for your Pull Request! We have run several checks on this pull request in order to make sure it's suitable for merging into this project. The results are listed in the following section.

Protected Branch

In order to be considered for merging, the pull request changes must not be implemented on the "main" branch. This is described in our Contributing Guide. We are closing this pull request and we would suggest that you implement your changes as described in our Contributing Guide and open a new pull request.

@github-actions
Copy link
Contributor

github-actions bot commented Oct 4, 2023

Thank you for this PR, here is the CI results:


Failed tests:

This PR introduces the following new failing tests:
ivy_tests/test_ivy/test_functional/test_experimental/test_core/test_elementwise.py::test_diff,paddle
ivy_tests/test_ivy/test_frontends/test_tensorflow/test_math.py::test_tensorflow_scalar_mul,numpy
ivy_tests/test_ivy/test_frontends/test_tensorflow/test_math.py::test_tensorflow_scalar_mul,jax
ivy_tests/test_ivy/test_frontends/test_tensorflow/test_math.py::test_tensorflow_scalar_mul,tensorflow
ivy_tests/test_ivy/test_frontends/test_tensorflow/test_math.py::test_tensorflow_scalar_mul,torch
ivy_tests/test_ivy/test_frontends/test_torch/test_tensor.py::test_torch_tensor_neg,numpy
ivy_tests/test_ivy/test_frontends/test_torch/test_tensor.py::test_torch_tensor_neg,jax
ivy_tests/test_ivy/test_frontends/test_torch/test_tensor.py::test_torch_tensor_neg,torch
ivy_tests/test_ivy/test_frontends/test_torch/test_tensor.py::test_torch_tensor_neg,paddle
ivy_tests/test_ivy/test_frontends/test_torch/test_tensor.py::test_torch_tensor_neg,tensorflow

@sineha17 sineha17 changed the title gradient numpy frontend feat: gradient numpy frontend Oct 4, 2023
@MahmoudAshraf97
Copy link
Contributor

Hi, are you still working on this?

@sineha17
Copy link
Contributor Author

Hi, are you still working on this?

in my local, all the test cases are passing. I've committed all the changes here.

Copy link
Contributor

@Ishticode Ishticode left a comment

Choose a reason for hiding this comment

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

@sineha17
Thanks for the PR and following through with the instruction above. I've tested locally and tests are passing. Apologies for the delays in merging on our end. Thank you very much.

@Ishticode Ishticode merged commit 3d8c5e7 into ivy-llc:main Mar 3, 2024
240 of 282 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NumPy Frontend Developing the NumPy Frontend, checklist triggered by commenting add_frontend_checklist
Projects
None yet
Development

Successfully merging this pull request may close these issues.

gradient
5 participants