Skip to content

[BUG] Metal JIT build: qmv template instantiation passes 6 args to 5-param kernel templates (runtime abort) #4350

Closed

Description

@PowerBeef

Describe the bug

In JIT-enabled builds (MLX_METAL_JIT=ON), any quantized matrix–vector product that routes through qmv / qmv_fast aborts at runtime with a Metal front-end compile error: the host code passes 6 template arguments to [[kernel]] templates that declare 5 parameters.

Since #3961 ("Optimize large NVFP4 QMV on M5 Max", first released in v0.32.1), qmv() in mlx/backend/metal/quantized.cpp forwards results_per_simdgroup into the template definition:

  auto kernel = get_quantized_kernel_wrapped(
      d,
      kname,
      (fast ? "qmv_fast" : "qmv"),
      mode,
      type_string,
      group_size,
      bits,
      B > 1,
      global_scale.has_value(),
      results_per_simdgroup);   // <-- 6th template arg

but the corresponding kernel templates still take 5 parameters — affine_qmv / affine_qmv_fast in mlx/backend/metal/kernels/quantized.h:

template <
    typename T,
    int group_size,
    const int bits,
    bool batched,
    bool has_global_scale = false>
[[kernel]] void affine_qmv(...)

and likewise fp_qmv / fp_qmv_fast in kernels/fp_quantized.h (the results_per_simdgroup parameter exists only on the inner qmv_impl, not on the [[kernel]] wrappers).

So the JIT path emits e.g.

template [[host_name("affine_qmv_float_gs_64_b_4_batch_0")]] [[kernel]]
decltype(affine_qmv<float, 64, 4, 0, 0, 4>) affine_qmv<float, 64, 4, 0, 0, 4>;

which fails to compile ("too many template arguments") and aborts the process on first use.

AOT builds are unaffected: get_quantized_kernel in nojit_kernels.cpp ignores template_def and resolves the kernel by name from the prebuilt metallib, which is why the default build and CI don't catch this.

Still present on main as of 2026-08-18.

To Reproduce

cmake -B build -DMLX_METAL_JIT=ON && cmake --build build
import mlx.core as mx
w = mx.random.normal((256, 512)).astype(mx.float16)
wq, scales, biases = mx.quantize(w, group_size=64, bits=4)
x = mx.random.normal((1, 512)).astype(mx.float16)  # M=1 -> qmv route
out = mx.quantized_matmul(x, wq, scales=scales, biases=biases,
                          transpose=True, group_size=64, bits=4)
mx.eval(out)  # aborts: JIT compile error in affine_qmv instantiation

(Observed originally through mlx-swift with a regenerated JIT preamble at core v0.32.1: first 4-bit affine matvec aborted with the affine_qmv<float, 64, 4, 0, 0, 4> instantiation error; switching that build to the no-JIT path confirmed the kernels themselves are fine.)

Expected behavior

JIT and AOT builds compile the same kernels; quantized matvec works under MLX_METAL_JIT=ON.

The straightforward fix seems to be adding int results_per_simdgroup = 4 as a trailing template parameter on the four [[kernel]] qmv wrappers (affine_qmv, affine_qmv_fast, fp_qmv, fp_qmv_fast) and threading it through to qmv_impl / qmv_fast_impl, so the host-side 6-argument instantiation is valid in both modes. Happy to open a PR if that direction sounds right.

Desktop

Additional context

Found while maintaining an mlx-swift-based runtime that regenerates the JIT preamble from core v0.32.1. The nojit workaround is fine for us; filing so JIT users don't hit the same runtime abort.

Activity

  1. added a commit that references this issue on Aug 23, 2026
  2. zcbenz commented on Aug 24, 2026

    @zcbenz
    Member

    Closed by #4372.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions