From 31f550af4df734c1296143b556dad624db3985fc Mon Sep 17 00:00:00 2001 From: Jonathan Spangler Date: Mon, 26 Oct 2026 15:49:22 -0700 Subject: [PATCH] Fix the JIT instantiation of the NAX head-dim split attention kernel get_steel_attention_nax_kernel passed bv to both attention_nax and attention_nax_dsplit. attention_nax gained a BV parameter in the upstream merge (#38); attention_nax_dsplit has none (its value head dim is BD). With bv in the list, every later argument shifts by one, an integer lands in the MaskType slot, and the library fails to compile: explicit instantiation of 'attention_nax_dsplit' does not refer to a function template ... JIT builds (mlx-swift) compile this kernel at runtime for full attention with head dim 256 or 512 and at least 1,024 query tokens on NAX GPUs, so the compile error surfaced as "Unable to build metal library from source" and the caller's process exited. Builds that use the precompiled metallib were unaffected, because steel_attention_nax.metal instantiates the split kernel without bv. Pass the split kernel the same arguments as its metallib instantiation. The non-split kernel is unchanged. --- mlx/backend/metal/jit_kernels.cpp | 37 ++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/mlx/backend/metal/jit_kernels.cpp b/mlx/backend/metal/jit_kernels.cpp index aad94eba1e..5cfeebd928 100644 --- a/mlx/backend/metal/jit_kernels.cpp +++ b/mlx/backend/metal/jit_kernels.cpp @@ -1417,22 +1417,37 @@ MTL::ComputePipelineState* get_steel_attention_nax_kernel( bool split_d) { const auto& lib_name = kernel_name; auto lib = d.get_library(lib_name, [&]() { + // attention_nax_dsplit has no BV parameter (its value head dim is BD), so + // it takes the same arguments as its instantiation in + // steel_attention_nax.metal. Passing bv shifts every later argument and + // the library fails to compile. + auto template_def = split_d ? get_template_definition( + lib_name, + "attention_nax_dsplit", + get_type_string(q.dtype()), + bq, + bk, + bd, + wm, + wn, + get_type_string(m.dtype())) + : get_template_definition( + lib_name, + "attention_nax", + get_type_string(q.dtype()), + bq, + bk, + bd, + bv, + wm, + wn, + get_type_string(m.dtype())); std::string kernel_source; concatenate( kernel_source, metal::utils(), metal::steel_attention_nax(), - get_template_definition( - lib_name, - split_d ? "attention_nax_dsplit" : "attention_nax", - get_type_string(q.dtype()), - bq, - bk, - bd, - bv, - wm, - wn, - get_type_string(m.dtype()))); + template_def); return kernel_source; }); return d.get_kernel(kernel_name, lib, hash_name, func_consts);