huggingface/transformers · error · ValueError

Unknown backend {backend!r}; expected 'auto', 'torchcodec',

Error message

Unknown backend {backend!r}; expected 'auto', 'torchcodec', 'librosa', or 'torchaudio'.

What it means

The DeepGEMM Mega MoE fused path (`fp8_fp4_mega_moe`) requires both `hidden_dim` and the per-expert `intermediate_hidden` to be divisible by 32, because FP8 scale factors are laid out at 32-element granularity and the SF layout transform (`transform_sf_into_required_layout` with recipe (1, 32)) cannot handle remainders. The guard checks `module.hidden_dim % 32` and `module.intermediate_dim % 32` before packing weights.

Source

Thrown at src/transformers/audio_utils.py:237

        `np.ndarray`: A numpy array representing the audio.
    """
    if isinstance(audio, np.ndarray):
        return audio
    if not isinstance(audio, str):
        raise TypeError(
            "Incorrect format used for `audio`. Should be a numpy array or a `str`: an `http(s)://` URL, "
            "a local file path, or a base64-encoded string (optionally wrapped as a `data:...` URI)."
        )

    # torchcodec handles audio/video; librosa only plain audio. `backend` lets callers pin one.
    if backend == "auto":
        resolved_backend = (
            "torchcodec" if is_torchcodec_available() and version.parse("0.3.0") <= TORCHCODEC_VERSION else "librosa"
        )
    elif backend in ("torchcodec", "librosa", "torchaudio"):
        resolved_backend = backend
    else:
        raise ValueError(f"Unknown backend {backend!r}; expected 'auto', 'torchcodec', 'librosa', or 'torchaudio'.")
    # soundfile-based backends (librosa / torchaudio) cannot decode the video-ish formats below.
    use_torchcodec = resolved_backend == "torchcodec"

    # 1. Identify the format from the source string (extension / `data:` media type), without fetching.
    filetype = _format_from_source(audio)
    # 2. With librosa as the only backend, fail fast and clearly on a format it cannot decode.
    if not use_torchcodec and filetype in TORCHCODEC_ONLY_FILETYPES:
        raise RuntimeError(
            f"The audio source is a '{filetype}' file, which librosa cannot decode. {_NEEDS_TORCHCODEC}"
        )

    # 3. Resolve to local path or bytes; sniff format for raw base64 payloads before passing to librosa.
    source = _resolve_audio_source(audio, timeout=timeout)
    if not use_torchcodec and filetype is None and isinstance(source, bytes):
        try:
            filetype = get_audio_filetype(source)
        except ValueError:
            filetype = None

View on GitHub (pinned to a597f97485)

Solutions

  1. Use a model whose `hidden_size` and `moe_intermediate_size` are multiples of 32 (virtually all published DeepSeek-style models are)
  2. If you control the architecture, pad/adjust the dims to a multiple of 32
  3. Otherwise use the standard `deepgemm` or `grouped_mm` experts dispatch, which does not have this fused-layout constraint

Example fix

# before
class CustomMoE(nn.Module):
    hidden_dim = 4100      # not divisible by 32
    intermediate_dim = 1100
model.set_experts_implementation("deepgemm_megamoe")  # -> ValueError

# after
class CustomMoE(nn.Module):
    hidden_dim = 4096      # divisible by 32
    intermediate_dim = 1104
Defensive patterns

Strategy: validation

Validate before calling

cfg = model.config
h, i = cfg.hidden_size, cfg.moe_intermediate_size
if h % 32 or i % 32:
    model.set_experts_implementation("deepgemm")  # megamoe needs dims divisible by 32

Type guard

def megamoe_compatible(cfg) -> bool:
    return cfg.hidden_size % 32 == 0 and getattr(cfg, "moe_intermediate_size", 1) % 32 == 0

Prevention

When it happens

Trigger: Selecting `experts_implementation='deepgemm_megamoe'` on an MoE whose `hidden_size` or `moe_intermediate_size` is not a multiple of 32 (e.g. 4096 is fine; 2048+4, 6144+16, or odd research dimensions are not).

Common situations: Custom/truncated architectures (pruned or width-modified MoEs) with unusual hidden sizes; converting non-standard checkpoints; student models distilled with irregular dims.

Related errors


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