Skip to content

Commit 8fff9e4

Browse files
committed
model: centralize hidden_act -> llm_ffn_op_type mapping
Per CISC review: the GGUF '<arch>.hidden_activation' string -> enum mapping should be centralized in llama-model.cpp, mirroring how rope_scaling_type is handled, rather than open-coded inside each arch-specific loader. Adds llm_ffn_op_type_from_string() in llama-model.{h,cpp} and rewires modern-bert's hparams loader to use it. The default fallback stays LLM_FFN_GEGLU, matching the prior behavior.
1 parent 136a15c commit 8fff9e4

3 files changed

Lines changed: 29 additions & 4 deletions

File tree

src/llama-model.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,28 @@ static llama_rope_scaling_type llama_rope_scaling_type_from_string(const std::st
813813
return LLAMA_ROPE_SCALING_TYPE_UNSPECIFIED;
814814
}
815815

816+
// Maps the GGUF `<arch>.hidden_activation` string to the FFN op type used by the
817+
// graph builders. Only gated activations that map cleanly to llm_ffn_op_type are
818+
// listed; unrecognized values fall back to GeGLU, which matches the historical
819+
// default for ModernBert-style architectures.
820+
static const std::map<std::string, llm_ffn_op_type> LLM_FFN_OP_TYPES_FROM_STRING = {
821+
{ "gelu", LLM_FFN_GEGLU },
822+
{ "geglu", LLM_FFN_GEGLU },
823+
{ "silu", LLM_FFN_SWIGLU },
824+
{ "swish", LLM_FFN_SWIGLU },
825+
{ "swiglu", LLM_FFN_SWIGLU },
826+
{ "relu", LLM_FFN_RELU },
827+
{ "reglu", LLM_FFN_REGLU },
828+
};
829+
830+
llm_ffn_op_type llm_ffn_op_type_from_string(const std::string & name, llm_ffn_op_type fallback) {
831+
const auto it = LLM_FFN_OP_TYPES_FROM_STRING.find(name);
832+
if (it != LLM_FFN_OP_TYPES_FROM_STRING.end()) {
833+
return it->second;
834+
}
835+
return fallback;
836+
}
837+
816838
// CPU: ACCEL -> GPU host -> CPU extra -> CPU
817839
static buft_list_t make_cpu_buft_list(const std::vector<llama_device> & devices, bool use_extra_bufts, bool no_host) {
818840
buft_list_t buft_list;

src/llama-model.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,10 @@ enum llm_type {
144144

145145
std::string llama_rope_scaling_type_name(llama_rope_scaling_type rope_scaling_type);
146146

147+
// Map a GGUF activation-name string to llm_ffn_op_type. Returns `fallback` if
148+
// the string is empty or not recognized.
149+
llm_ffn_op_type llm_ffn_op_type_from_string(const std::string & name, llm_ffn_op_type fallback);
150+
147151
struct llama_layer_posnet {
148152
// resnet
149153
struct ggml_tensor * norm1 = nullptr;

src/models/modern-bert.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,9 @@ void llama_model_modern_bert::load_arch_hparams(llama_model_loader & ml) {
1616

1717
// Some ModernBert derivatives (e.g. IBM Granite Embedding 97m R2) use
1818
// SiLU/SwiGLU in the FFN instead of the default GELU/GeGLU.
19-
std::string hidden_act = "gelu";
20-
ml.get_key(LLM_KV_HIDDEN_ACT, hidden_act, false);
21-
if (hidden_act == "silu" || hidden_act == "swish") {
22-
hparams.llm_ffn_op = LLM_FFN_SWIGLU;
19+
std::string hidden_act;
20+
if (ml.get_key(LLM_KV_HIDDEN_ACT, hidden_act, false)) {
21+
hparams.llm_ffn_op = llm_ffn_op_type_from_string(hidden_act, LLM_FFN_GEGLU);
2322
}
2423

2524
switch (hparams.n_layer) {

0 commit comments

Comments
 (0)