huggingface/transformers · error · KeyError

function {activation_string} not found in ACT2FN mapping {li

Error message

function {activation_string} not found in ACT2FN mapping {list(ACT2FN.keys())}

What it means

`deepgemm_bf16_experts_forward` requires bf16 hidden states — the grouped BF16 kernels and the surrounding packing (`per_token_cast`-free path) assume bfloat16 exactly. fp16 or fp32 hidden states are rejected with a ValueError before kernel load.

Source

Thrown at src/transformers/activations.py:357

    "relu": nn.ReLU,
    "relu2": ReLUSquaredActivation,
    "relu6": nn.ReLU6,
    "sigmoid": nn.Sigmoid,
    "silu": SiLUActivation,
    "sqrtsoftplus": SqrtSoftplusActivation,
    "swish": nn.SiLU,
    "tanh": nn.Tanh,
    "prelu": nn.PReLU,
    "xielu": XIELUActivation,
}
ACT2FN = ClassInstantier(ACT2CLS)


def get_activation(activation_string):
    if activation_string in ACT2FN:
        return ACT2FN[activation_string]
    else:
        raise KeyError(f"function {activation_string} not found in ACT2FN mapping {list(ACT2FN.keys())}")


# For backwards compatibility with: from activations import gelu_python
gelu_python = get_activation("gelu_python")
gelu_new = get_activation("gelu_new")
gelu = get_activation("gelu")
gelu_fast = get_activation("gelu_fast")
gelu_pytorch_tanh = get_activation("gelu_pytorch_tanh")
quick_gelu = get_activation("quick_gelu")
silu = get_activation("silu")
mish = get_activation("mish")
linear_act = get_activation("linear")

View on GitHub (pinned to a597f97485)

Solutions

  1. Load/run the model in bfloat16 (`torch_dtype=torch.bfloat16`)
  2. Cast hidden states before the experts call: `hidden_states = hidden_states.to(torch.bfloat16)`
  3. If fp16 must be kept, choose a different experts implementation that supports fp16

Example fix

# before
model = AutoModelForCausalLM.from_pretrained(m, torch_dtype=torch.float16)
h = h.half()  # -> ValueError: requires bfloat16

# after
model = AutoModelForCausalLM.from_pretrained(m, torch_dtype=torch.bfloat16)
Defensive patterns

Strategy: type-guard

Validate before calling

if hidden_states.dtype != torch.bfloat16:
    hidden_states = hidden_states.to(torch.bfloat16)

Type guard

def is_bf16(t: torch.Tensor) -> bool:
    return t.dtype == torch.bfloat16

Prevention

When it happens

Trigger: Running `experts_implementation` that maps to the DeepGEMM BF16 grouped path on a model kept in fp16 (e.g. `torch_dtype=torch.float16`) or fp32; feeding a manually cast fp16 tensor into the experts forward.

Common situations: Mixed-precision training where the router/moe block receives fp16 activations; checkpoints loaded in fp16 for A100-era compatibility then moved to DeepGEMM experts on Hopper.

Related errors


AI-assisted analysis of huggingface/transformers@a597f97485 (2026-08-14). Data as JSON: /api/errors/1faf4ef9c2cf8fe1. Report an issue: GitHub.