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

ReLU6 is not fused properly #441

Open
ilouzl opened this issue Dec 24, 2024 · 0 comments
Open

ReLU6 is not fused properly #441

ilouzl opened this issue Dec 24, 2024 · 0 comments
Labels

Comments

@ilouzl
Copy link

ilouzl commented Dec 24, 2024

Description of the bug:

When adding torch.nn.ReLU6 activation after Conv2D, the converter doesn't fuse it the Conv.
Only by tweaking Relu6 a little bit it is fused, but an additional Relu is appended to it.

Actual vs expected behavior:

The outcome of the conversion is a sequence of Conv2D -> Min -> Relu.
Expected output would be a single Conv2d fused with Relu6.

Any other information you'd like to share?

Sample code to reproduce:

import ai_edge_torch
import torch
import torchvision

model = torch.nn.Sequential(torch.nn.Conv2d(3, 32, 3), torch.nn.ReLU6())
sample_inputs = (torch.randn(1, 3, 64, 64),)
torch_output = model(*sample_inputs)
edge_model = ai_edge_torch.convert(model.eval(), sample_inputs)
edge_output = edge_model(*sample_inputs)
edge_model.export('relu6.tflite')


class MyRelu6(torch.nn.Module):
    def __init__(self, inplace=False):
        super(MyRelu6, self).__init__()
        self.inplace = inplace

    def forward(self, x):
        return torch.nn.functional.relu(x, inplace=self.inplace).clamp(0, 6)


model = torch.nn.Sequential(torch.nn.Conv2d(3, 32, 3), MyRelu6())
sample_inputs = (torch.randn(1, 3, 64, 64),)
torch_output = model(*sample_inputs)
edge_model = ai_edge_torch.convert(model.eval(), sample_inputs)
edge_output = edge_model(*sample_inputs)

edge_model.export('custom_relu6.tflite')

LEFT : relu6.tflite
RIGHT: custom_relu6.tflite:

image

@ilouzl ilouzl added the type:bug Bug label Dec 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant