Skip to content

model : use exact GELU for ModernBERT encoders - #30108

Merged
taronaeo merged 3 commits into
ggml-org:masterfrom
boshjerns:modernbert-encoder-gelu-erf
Oct 9, 2026
Merged

taronaeo merged 3 commits into
ggml-org:masterfrom
boshjerns:modernbert-encoder-gelu-erf

Conversation

@boshjerns

@boshjerns boshjerns commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

Overview

ModernBERT models with hidden_activation: "gelu" currently run the tanh approximation through ggml_geglu, but transformers uses exact GELU. This switches gelu, and the default when the key is missing, to ggml_geglu_erf.

Names that mean the tanh approximation (gelu_pytorch_tanh, gelu_new, gelu_fast, gelu_accurate, gelu_python_tanh) stay on ggml_geglu, and SiLU models are unchanged. Existing GGUFs work without reconversion.

Follow-up to #29692, where @CISC pointed out that transformers gelu is exact GELU, not the tanh approximation. That PR fixed the rank head, and the encoder FFN had the same mismatch.

Additional information

Tested on CPU in F32 with Alibaba-NLP/gte-reranker-modernbert-base, comparing against transformers on 24 query/passage pairs:

master PR
FFN activation, max error vs exact GELU 5.8e-2 7.4e-6
encoder output, mean error vs transformers 3.5e-4 1.3e-4
encoder output, max error vs transformers 7.2e-3 2.8e-3

The tanh variants and SiLU (ibm-granite/granite-embedding-97m-multilingual-r2) give byte-identical outputs before and after.

The remaining difference comes from the F16 cast of K and V before flash attention in build_attn_mha. With -fa off, scores match transformers to 1e-6 (max 5.7e-6), and every layer matches to about 1e-6, including the sliding window layers on a 411 token input.

On CPU the speed change was within run-to-run noise; the SiLU model, which this change doesn't touch, varied by up to 18% between runs. GPU not tested.

Build with fatal warnings, ctest main|python, server embedding and rerank tests, and test-llama-archs all pass.

Requirements

  • I have read and agree with the contributing guidelines
  • AI usage disclosure: YES. Codex and Claude assisted with the implementation, verification and this description.

@github-actions github-actions Bot added the model Model specific label Oct 7, 2026
Assisted-by: Claude Opus 5.5
@boshjerns
boshjerns marked this pull request as ready for review October 8, 2026 06:57
@boshjerns
boshjerns requested a review from CISC as a code owner October 8, 2026 06:57
Comment thread src/llama-model.cpp Resolved
Assisted-by: Claude Opus 5.5
@CISC CISC added the merge ready A maintainer can use this label to indicate that they consider the changes final and ready to merge. label Oct 8, 2026
@taronaeo
taronaeo merged commit 6184e92 into ggml-org:master Oct 9, 2026
18 of 19 checks passed
ckti pushed a commit to ckti/llama.cpp-consolidated that referenced this pull request Oct 12, 2026
* model : use exact GELU for ModernBERT encoders

Assisted-by: Codex

* model : keep tanh GELU aliases on ggml_geglu

Assisted-by: Claude Opus 5.5

* model : map gelu_python to ggml_geglu_erf

Assisted-by: Claude Opus 5.5
edwardyoon pushed a commit to edwardyoon/focus-llama that referenced this pull request Oct 24, 2026
* model : use exact GELU for ModernBERT encoders

Assisted-by: Codex

* model : keep tanh GELU aliases on ggml_geglu

Assisted-by: Claude Opus 5.5

* model : map gelu_python to ggml_geglu_erf

Assisted-by: Claude Opus 5.5
auRose94 pushed a commit to auRose94/llama.cpp that referenced this pull request Oct 28, 2026
* model : use exact GELU for ModernBERT encoders

Assisted-by: Codex

* model : keep tanh GELU aliases on ggml_geglu

Assisted-by: Claude Opus 5.5

* model : map gelu_python to ggml_geglu_erf

Assisted-by: Claude Opus 5.5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merge ready A maintainer can use this label to indicate that they consider the changes final and ready to merge. model Model specific

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants