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(loss-functions): add poisson_nll_loss function #22501

Merged
merged 8 commits into from
Sep 22, 2023

Conversation

akshatvishu
Copy link
Contributor

Close #21727

@github-actions
Copy link
Contributor

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 Array API Conform to the Array API Standard, created by The Consortium for Python Data API Standards Ivy API Experimental Run CI for testing API experimental/New feature or request PaddlePaddle Backend Developing the Paddle Paddle Backend. Ivy Functional API labels Aug 24, 2023
@akshatvishu
Copy link
Contributor Author

akshatvishu commented Aug 28, 2023

This below is one of the failing test for my implementation:

______________ test_poisson_nll_loss[cpu-tensorflow-False-False] _______________
ivy_tests/test_ivy/test_functional/test_experimental/test_nn/test_losses.py:158: in test_poisson_nll_loss
    fn_tree="functional.ivy.experimental.poisson_nll_loss",
ivy_tests/test_ivy/test_functional/test_experimental/test_nn/test_losses.py:187: in test_poisson_nll_loss
    helpers.test_function(
ivy_tests/test_ivy/helpers/function_testing.py:459: in test_function
    gradient_test(
ivy_tests/test_ivy/helpers/function_testing.py:1015: in gradient_test
    value_test(
ivy_tests/test_ivy/helpers/assertions.py:169: in value_test
    assert_all_close(
ivy_tests/test_ivy/helpers/assertions.py:62: in assert_all_close
    assert np.allclose(
E   AssertionError:  the results from backend tensorflow and ground truth framework torch do not match
E    [0.]!=[-0.69314719] 
E   
E   
E   Falsifying example: test_poisson_nll_loss(
E       backend_fw='tensorflow',
E       on_device='cpu',
E       dtype_input_target=(['float64', 'float64'], [array([2.]), array([2.])]),
E       log_input=False,
E       full=False,
E       epsilon=1e-08,
E       reduction='none',
E       fn_name='poisson_nll_loss',
E       test_flags=FunctionTestFlags(
E           ground_truth_backend='torch',
E           num_positional_args=0,
E           with_out=False,
E           instance_method=False,
E           test_gradients=True,
E           test_compile=False,
E           as_variable=[False],
E           native_arrays=[False],
E           container=[False],
E       ),
E   )

But when I run it a temp. python file in-order to check it:

import torch
import ivy

ivy.set_backend("tensorflow")
x = ivy.array([2.], dtype=ivy.float64)
y = ivy.array([2.], dtype=ivy.float64)
z = ivy.poisson_nll_loss(x, y, log_input=False, reduction="none")
print(z)
print(z.dtype)
print("TORCH OUTPUT")
x = torch.tensor([2.], dtype=torch.float64)
y = torch.tensor([2.], dtype=torch.float64)
z = torch.nn.functional.poisson_nll_loss(x, y, log_input=False, reduction="none")
print(z)
print(z.dtype)

"""
ivy.array([0.61370563])
float64
TORCH OUTPUT
tensor([0.6137], dtype=torch.float64)
torch.float64
"""

@akshatvishu
Copy link
Contributor Author

same with this test failure for tensorflow backend:

E       AssertionError:  the results from backend tensorflow and ground truth framework torch do not match
E        [0. 0. 0. 0. 0. 0. 0. 0. 0.]!=[-0.07701635 -0.07701635 -0.07701635 -0.07701635 -0.07701635 -0.07701635
E        -0.07701635 -0.07701635 -0.07701635] 
E       
E       
E       Falsifying example: test_poisson_nll_loss(
E           backend_fw='tensorflow',
E           on_device='cpu',
E           dtype_input_target=(['float32', 'float32'],
E            [array([2., 2., 2., 2., 2., 2., 2., 2., 2.], dtype=float32),
E             array([2., 2., 2., 2., 2., 2., 2., 2., 2.], dtype=float32)]),
E           log_input=False,
E           full=False,
E           epsilon=1e-08,
E           reduction='none',
E           fn_name='poisson_nll_loss',
E           test_flags=FunctionTestFlags(
E               ground_truth_backend='torch',
E               num_positional_args=0,
E               with_out=False,
E               instance_method=False,
E               test_gradients=True,
E               test_compile=False,
E               as_variable=[False],
E               native_arrays=[False],
E               container=[False],
E           ),
E       )
E       
E       You can reproduce this example by temporarily adding @reproduce_failure('6.82.6', b'AXicY2Bk4GBAAKahz2QEYgAXHQAv') as a decorator on your test case

ivy_tests/test_ivy/helpers/assertions.py:62: AssertionError

But when I call my implementation then it gives different result:

import torch
import ivy


ivy.set_backend("tensorflow")
x = ivy.array([2., 2., 2., 2., 2., 2., 2., 2., 2.], dtype=ivy.float32)
y = ivy.array([2., 2., 2., 2., 2., 2., 2., 2., 2.], dtype=ivy.float32)
z = ivy.poisson_nll_loss(x, y, log_input=False, reduction="none")
print(z)
print(z.dtype)

x = torch.tensor([2., 2., 2., 2., 2., 2., 2., 2., 2.], dtype=torch.float32)
y = torch.tensor([2., 2., 2., 2., 2., 2., 2., 2., 2.], dtype=torch.float32)
z = torch.nn.functional.poisson_nll_loss(x, y, log_input=False, reduction="none")
print(z)
print(z.dtype)
"""
ivy.array([0.61370564, 0.61370564, 0.61370564, 0.61370564, 0.61370564,
       0.61370564, 0.61370564, 0.61370564, 0.61370564])
float32
tensor([0.6137, 0.6137, 0.6137, 0.6137, 0.6137, 0.6137, 0.6137, 0.6137, 0.6137])
torch.float32
"'"

…le issue

Refined the `poisson_nll_loss` composition function to address discrepancies with the native PaddlePaddle method. This refinement ensures accuracy and is to be replaced once PaddlePaddle promotes the changes from the develop branch to a stable release.

Related to PR in PaddlePaddle: PaddlePaddle/Paddle#56992
@akshatvishu
Copy link
Contributor Author

akshatvishu commented Sep 13, 2023

@kurshakuz Can you please provide any feedback on this ?
Because when I call my implementation to check for inputs which are giving failures, I am not getting different results as seen in test failures and instead I am getting homogeneous results across all backend.

Example:

E       AssertionError:  the results from backend paddle and ground truth framework torch do not match
E        [0.]!=[-0.6931472] 
E       
E       
E       Falsifying example: test_poisson_nll_loss(
E           backend_fw='paddle',
E           on_device='cpu',
E           dtype_input_target=(['float32', 'float32'],
E            [array([2.], dtype=float32), array([2.], dtype=float32)]),
E           log_input=False,
E           full=False,
E           epsilon=1e-08,
E           reduction='none',
E           test_flags=FunctionTestFlags(
E               ground_truth_backend='torch',
E               num_positional_args=0,
E               with_out=False,
E               instance_method=False,
E               test_gradients=True,
E               test_compile=False,
E               as_variable=[False],
E               native_arrays=[False],
E               container=[False],
E               precision_mode=False,
E           ),
E           fn_name='poisson_nll_loss',
E       )

But when I call my function with the same inputs for which it is showing failures, I get same results across all frameworks.

ivy.set_backend("tensorflow")
ivy.set_inplace_mode('strict') 
x = ivy.array([2.], dtype=ivy.float32)
y = ivy.array([2.], dtype=ivy.float32)
z = ivy.poisson_nll_loss(x, y, log_input=False, reduction="none")
print(f"TF backend:{z}")#TF backend:ivy.array([0.61370564])

ivy.set_backend("torch")
ivy.set_inplace_mode('strict') 
x = ivy.array([2.], dtype=ivy.float32)
y = ivy.array([2.], dtype=ivy.float32)
z = ivy.poisson_nll_loss(x, y, log_input=False, reduction="none")
print(f"torch_backend{z}")#torch_backendivy.array([0.61370564])

ivy.set_backend("paddle")
ivy.set_inplace_mode('strict') 
x = ivy.array([2.], dtype=ivy.float32)
y = ivy.array([2.], dtype=ivy.float32)
z = ivy.poisson_nll_loss(x, y, log_input=False, reduction="none")
print(f"paddle_backend{z}") #paddle_backendivy.array([0.61370564])

print("TORCH NATIVE API")
x = torch.tensor([2.], dtype=torch.float32)
y = torch.tensor([2.], dtype=torch.float32)
z = torch.nn.functional.poisson_nll_loss(x, y, log_input=False, reduction="none")
print(f"TORCH NATIVE: {z}") #TORCH NATIVE: tensor([0.6137])

…generation for testing due to numeric instability.
@akshatvishu
Copy link
Contributor Author

akshatvishu commented Sep 13, 2023

Update: So, setting testing_gradient=False , lowering the value generation test range and adding rtol and atol have made the test pass for all backends @kurshakuz

Copy link
Contributor

@kurshakuz kurshakuz left a comment

Choose a reason for hiding this comment

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

Hey! I am very sorry for such delay this PR somehow slipped under the radar. Would you mind removing all changes that are unrelated to the function you are adding?

@akshatvishu
Copy link
Contributor Author

Hey! I am very sorry for such delay this PR somehow slipped under the radar. Would you mind removing all changes that are unrelated to the function you are adding?

sure!

@akshatvishu
Copy link
Contributor Author

Hey @kurshakuz . I removed the out of scope functionality as discussed with Ved and Haider at discord.(refs:LINK)

Copy link
Contributor

@kurshakuz kurshakuz left a comment

Choose a reason for hiding this comment

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

LGTM! Thanks for your contribution!

@kurshakuz kurshakuz merged commit a241455 into ivy-llc:main Sep 22, 2023
101 of 135 checks passed
@akshatvishu
Copy link
Contributor Author

LGTM! Thanks for your contribution!

Thanks for the review @kurshakuz !

@akshatvishu akshatvishu deleted the poison_loss branch September 22, 2023 10:54
iababio pushed a commit to iababio/ivy that referenced this pull request Sep 27, 2023
druvdub pushed a commit to druvdub/ivy that referenced this pull request Oct 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Array API Conform to the Array API Standard, created by The Consortium for Python Data API Standards Ivy API Experimental Run CI for testing API experimental/New feature or request Ivy Functional API PaddlePaddle Backend Developing the Paddle Paddle Backend.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

poisson_nll_loss
3 participants