sgl-project/sglang · error · ValueError

Unknown approximate mode: {approximate}

Error message

Unknown approximate mode: {approximate}

What it means

The GELU-style activation layer in sglang's multimodal runtime accepts PyTorch's 'approximate' argument, which only supports 'none' (exact GELU) and 'tanh' (tanh approximation). __init__ validates this eagerly and raises for any other string, mirroring torch.nn.functional.gelu's constraint.

Source

Thrown at python/sglang/multimodal_gen/runtime/layers/activation.py:89

        return out


@CustomOp.register("gelu_and_mul")
class GeluAndMul(CustomOp):
    """An activation function for GeGLU.

    The function computes x -> GELU(x[:d]) * x[d:] where d = x.shape[-1] // 2.

    Shapes:
        x: (batch_size, seq_len, 2 * d) or (num_tokens, 2 * d)
        return: (batch_size, seq_len, d) or (num_tokens, d)
    """

    def __init__(self, approximate: str = "none"):
        super().__init__()
        self.approximate = approximate
        if approximate not in ("none", "tanh"):
            raise ValueError(f"Unknown approximate mode: {approximate}")

    def forward_cuda(self, *args, **kwargs) -> Any:
        return self.forward_native(*args, **kwargs)

    def forward_npu(self, x: torch.Tensor) -> torch.Tensor:
        y_npu, _ = torch_npu.npu_geglu(
            x,
            dim=-1,
            approximate=1 if self.approximate == "tanh" else 0,
            activate_left=True,
        )
        return y_npu

    def forward_native(self, x: torch.Tensor) -> torch.Tensor:
        """PyTorch-native implementation equivalent to forward()."""
        d = x.shape[-1] // 2
        return F.gelu(x[..., :d], approximate=self.approximate) * x[..., d:]

View on GitHub (pinned to 0132848349)

Solutions

  1. Pass approximate='none' or 'tanh' only.
  2. Map config names before construction: 'gelu_new'/'gelu_fast' → 'tanh'; plain 'gelu' → 'none'.
  3. If you truly need another mode, use torch.nn.GELU directly or extend the tuple at activation.py:89.

Example fix

# before
layer = GeluLayer(approximate=model_cfg.hidden_act)  # hidden_act='gelu_new'
# after
approx = 'tanh' if model_cfg.hidden_act in ('gelu_new','gelu_pytorch_tanh') else 'none'
layer = GeluLayer(approximate=approx)
Defensive patterns

Strategy: validation

Validate before calling

def norm_approx(name: str) -> str:
    return 'tanh' if name in ('gelu_new','gelu_pytorch_tanh','tanh') else 'none'
approx = norm_approx(cfg.hidden_act)

Type guard

def is_supported_approximate(v: str) -> bool:
    return v in ('none', 'tanh')

Prevention

When it happens

Trigger: Constructing the activation layer with approximate='silu', 'newer', or any non-{'none','tanh'} value, typically because a model config's hidden_act_gelu or activation string was forwarded verbatim into the approximate parameter.

Common situations: Model configs that carry activation strings like 'gelu_new'/'gelu_pytorch_tanh' being passed unnormalized; porting configs from other frameworks that use different approximation names; version changes where a new approximate mode exists upstream but not here.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of sgl-project/sglang@0132848349 (2026-08-28). Data as JSON: /api/errors/6a7435cd3dd376db. Report an issue: GitHub.