huggingface/transformers · error · ValueError

min should be < max (got min: {min}, max: {max})

Error message

min should be < max (got min: {min}, max: {max})

What it means

`deepgemm_fp8_fp4_linear` requires the input tensor to be bf16 or fp16 because `per_token_cast_to_fp8` quantizes from those precisions; any other dtype (fp32, fp8, int) is rejected up front, before the expensive hub-download/JIT kernel load.

Source

Thrown at src/transformers/activations.py:141

        return input * torch.sigmoid(1.702 * input)


class ClippedGELUActivation(nn.Module):
    """
    Clip the range of possible GeLU outputs between [min, max]. This is especially useful for quantization purpose, as
    it allows mapping negatives values in the GeLU spectrum. For more information on this trick, please refer to
    https://huggingface.co/papers/2004.09602.

    Gaussian Error Linear Unit. Original Implementation of the gelu activation function in Google Bert repo when
    initially created.

    For information: OpenAI GPT's gelu is slightly different (and gives slightly different results): 0.5 * x * (1 +
    torch.tanh(math.sqrt(2 / math.pi) * (x + 0.044715 * torch.pow(x, 3)))). See https://huggingface.co/papers/1606.08415
    """

    def __init__(self, min: float, max: float):
        if min > max:
            raise ValueError(f"min should be < max (got min: {min}, max: {max})")

        super().__init__()
        self.min = min
        self.max = max

    def forward(self, x: Tensor) -> Tensor:
        return torch.clip(gelu(x), self.min, self.max)


class AccurateGELUActivation(nn.Module):
    """
    Applies GELU approximation that is faster than default and more accurate than QuickGELU. See:
    https://github.com/hendrycks/GELUs

    Implemented along with MEGA (Moving Average Equipped Gated Attention)
    """

    def __init__(self):

View on GitHub (pinned to a597f97485)

Solutions

  1. Cast the input to bf16/fp16 before the call: `input = input.to(torch.bfloat16)` (or load the model with `torch_dtype=torch.bfloat16`)
  2. Ensure autocast/bf16 training is active so activations reach the linear in half precision
  3. Do not pre-quantize activations yourself — the linear does per-token FP8 casting internally

Example fix

# before
out = deepgemm_fp8_fp4_linear(x.float(), w, w_scale)  # fp32 -> ValueError

# after
out = deepgemm_fp8_fp4_linear(x.to(torch.bfloat16), w, w_scale)
Defensive patterns

Strategy: type-guard

Validate before calling

if input.dtype not in (torch.bfloat16, torch.float16):
    input = input.to(torch.bfloat16)

Type guard

def is_half_precision(t: torch.Tensor) -> bool:
    return t.dtype in (torch.bfloat16, torch.float16)

Prevention

When it happens

Trigger: Passing a float32 hidden state (common when a model runs in fp32 or when a layer upcasts before the linear), or an already-quantized fp8 input, into `deepgemm_fp8_fp4_linear`.

Common situations: Models loaded with `torch_dtype=torch.float32` for debugging; custom forward code that upcasts activations (`.float()`) before experts/linear layers; autocast disabled so activations stay fp32.

Related errors


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