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

[Unity][BugFix] Fix a bug in relax gelu_tanh computation #298

Closed
wants to merge 1 commit into from

Conversation

rickzx
Copy link
Contributor

@rickzx rickzx commented Nov 30, 2023

The gelu_tanh computation (e.g. the gelu_new activation in huggingface API) is incorrect in the current relax code. This PR fixes the bug.

Correctness testing passes (which fails previously):

inp = torch.randn((2, 4), dtype=torch.float32)

def tanh_gelu(input):
    return 0.5 * input * (1.0 + np.tanh(np.sqrt(2.0 / np.pi) * (input + 0.044715 * torch.pow(input, 3.0))))

out1 = tanh_gelu(inp)

class TanhGelu(nn.Module):
    def __init__(self):
        pass

    def forward(self, x: nn.Tensor):
        return op.gelu(x, "tanh")

forward_spec = {"forward": {"x": spec.Tensor([2, 4], dtype="float32")}}
gelu = TanhGelu().jit(forward_spec)
out2 = gelu['forward'](inp)

assert torch.allclose(out1, out2)

Updated unit tests

@rickzx
Copy link
Contributor Author

rickzx commented Nov 30, 2023

As @CharlieFRuan suggested, opened another PR in tvm/unity branch: apache/tvm#16188. Will close this one

@rickzx rickzx closed this Nov 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant