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

[Bug]: torch frontend inconsistent gradient behaviour of torch.nn.relu6 #10584

Closed
1 of 4 tasks
MahmoudAshraf97 opened this issue Feb 15, 2023 · 0 comments · Fixed by #10587
Closed
1 of 4 tasks

[Bug]: torch frontend inconsistent gradient behaviour of torch.nn.relu6 #10584

MahmoudAshraf97 opened this issue Feb 15, 2023 · 0 comments · Fixed by #10587
Assignees
Labels
Bug Report Report bugs detected in Ivy. PyTorch Frontend Developing the PyTorch Frontend, checklist triggered by commenting add_frontend_checklist

Comments

@MahmoudAshraf97
Copy link
Contributor

MahmoudAshraf97 commented Feb 15, 2023

Bug Explanation

the implementation of torch.nn.relu6 in Pytorch Frontend uses ivy.minimum and ivy.maximum, this implementation is identical to ivy.clip which uses torch.clamp when using torch backend.

The problem arises when calculating gradients because torch.clamp has a gradient of 1 when the input is 0 or 6 while torch.nn.relu6 has a gradient of 0 when the input is 0 or 6 (at the non-differentiable point)

Steps to Reproduce Bug

import ivy
import torch
import ivy.functional.frontends.torch as ivy_torch

x = torch.linspace(-3,8,12)
x.requires_grad = True
y = ivy_torch.nn.functional.relu6(x)  #ivy.minimum(ivy.maximum(x, 0), 6)
(y - 1.0).pow(2).sum().backward()
x_grad = x.grad


x_torch = torch.linspace(-3,8,12)
x_torch.requires_grad = True
y_torch = torch.nn.functional.relu6(x_torch)
(y_torch - 1.0).pow(2).sum().backward()
x_torch_grad = x_torch.grad
print(ivy.allclose(y, y_torch)) #identical results
>>> ivy.array(True)
print(ivy.allclose(x_grad, x_torch_grad)) #inconsistent gradients
>>> ivy.array(False)

Environment

Windows 11 using Docker and VS Code

Ivy Version

v1.1.9

Backend

  • NumPy
  • TensorFlow
  • PyTorch
  • JAX

Device

No response

@MahmoudAshraf97 MahmoudAshraf97 added PyTorch Frontend Developing the PyTorch Frontend, checklist triggered by commenting add_frontend_checklist Bug Report Report bugs detected in Ivy. labels Feb 15, 2023
@MahmoudAshraf97 MahmoudAshraf97 linked a pull request Feb 15, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Report Report bugs detected in Ivy. PyTorch Frontend Developing the PyTorch Frontend, checklist triggered by commenting add_frontend_checklist
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants