Skip to content

Upstream/fast gelu - #53

Open
DrJesseGlass wants to merge 3 commits into
downstreamfrom
upstream/fast-gelu
Open

Upstream/fast gelu#53
DrJesseGlass wants to merge 3 commits into
downstreamfrom
upstream/fast-gelu

Conversation

@DrJesseGlass

Copy link
Copy Markdown
Owner

No description provided.

ivarflakstad and others added 3 commits June 26, 2026 23:12
The scalar Gelu path spends nearly all its time in one libm tanh call per
element; encoder-style models run tens of millions of GELUs per image. This
adds a NEON f32_vec override for Gelu on aarch64 (when neither mkl nor
accelerate provide one): tanh via the Eigen-style rational approximation
x*P(x^2)/Q(x^2), clamped and saturating exactly to +-1 outside +-7.9053111
so gelu(x) is exactly x / exactly 0 for large inputs. Max error <= 2e-6 vs
libm across a full sweep (unit test included) - considerably tighter than
the f16 lookup table other CPU inference stacks use for GELU.

CANDLE_FAST_GELU=0 restores the exact libm path for bit-reproducibility.

Measured on Granite-Docling's SigLIP encoder (12 layers, 3.1M GELU/layer,
Apple M1 4 threads): encoder step 971 -> 905 ms.
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.

2 participants