Repository navigation
Use erfc for numerically stable GELU - #681
Merged
Merged
Conversation
This was referenced Sep 26, 2026
polvalente
approved these changes
Sep 26, 2026
Member
|
@hfiguera could you point to Nx/EXLA/Torchx main here so we can green out the suite? |
Contributor
Author
Sure. I will do it. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The erf-based GELU formula loses precision in the negative tail through cancellation in
1 + erf(x / sqrt(2)). Multiplying byxbefore dividing by two can also overflow for large finite positive inputs.Compute GELU as
(x / 2) * erfc(-x / sqrt(2)). This retains small negative tails and avoids that intermediate overflow. Update the documented outputs and add regressions for f32/f64 tails, large finite inputs, vectorized inputs, and a nonlinear gradient composition.Validation: the combined activation/EXLA expression suite passed 421 tests on each of macOS ARM64 and Linux x86_64, with 48 evaluator-only doctests excluded from the EXLA run. The EXLA vectorization regression uses the block-output fix in elixir-nx/nx#1842; the scalar GELU reformulation is independent of that fix.