fix(modernbert): use config hidden_activation in MLP (support silu for Granite)#211
Open
amgix-dev wants to merge 1 commit into
Open
fix(modernbert): use config hidden_activation in MLP (support silu for Granite)#211amgix-dev wants to merge 1 commit into
amgix-dev wants to merge 1 commit into
Conversation
ModernBertMLP always applied gelu_erf on the gate tensor, but some ModernBERT-based models (e.g. ibm-granite/granite-embedding-97m-multilingual-r2) set hidden_activation: "silu" in config.json. Read hidden_activation from config (default: gelu) and use the matching gated-FFN activation. Missing config field keeps existing GeGLU behavior.
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.
Problem
ModernBertMLPalways usedgelu_erf()on the gate branch of the gated FFN, regardless ofconfig.json.Models such as
ibm-granite/granite-embedding-97m-multilingual-r2sethidden_activation: "silu"(SwiGLU-style). Running them with GeGLU produces incorrect hidden states and poor embedding quality.Solution
hidden_activationtoConfig(#[serde(default)]for backward compatibility)"gelu"→gelu_erf()(default when field is absent)"silu"→silu()Backward compatibility
Existing ModernBERT checkpoints without
hidden_activationin config continue to usegelu, matching current behavior.Testing
Verified embedding parity against Sentence Transformers implementation:
ibm-granite/granite-embedding-97m-multilingual-r2(hidden_activation: silu): Sentence Transformer parity, cosine ≈ 1.0Alibaba-NLP/gte-modernbert-base(hidden_activation: gelu): Sentence Transformer parity, cosine ≈ 1.0