Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .github/workflows/fork-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,17 @@ jobs:
ctest --test-dir build -R '^qmv_batch_invariant$' \
--output-on-failure --no-tests=error

- name: Run the NAX quantized kernel tests with MLX_NAX_KSPLIT=1
# MLX reads the switch once in each process. The C++ test steps above
# run the case with the default (MLX_NAX_KSPLIT=0); this ctest entry
# runs it again with K-split enabled (tests/CMakeLists.txt).
if: ${{ !cancelled() && steps.build.outcome == 'success' }}
env:
LLVM_PROFILE_FILE: ${{ runner.temp }}/profiles/nax-ksplit-%p-%m.profraw
run: |
ctest --test-dir build -R '^nax_ksplit_enabled$' \
--output-on-failure --no-tests=error

- name: Merge the coverage profiles
if: ${{ !cancelled() && steps.build.outcome == 'success' }}
run: |
Expand Down
13 changes: 12 additions & 1 deletion docs/src/usage/environment_variables.rst
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,21 @@ Metal
give a row different bits at some row counts: ``qmv_mma`` for 6 to 16 rows
of the floating-point modes, and the affine split-K kernel from 8 rows on
GPUs with neural accelerators. The vector limit is 6 to 33 rows; it
depends on the GPU and on the shape. A row alone (``qmv``) can get bits
depends on the GPU and on the shape. A row alone (``qmv``) can get bits
that are different from the bits of the same row among two or more rows.
MLX reads this variable once in each process.

.. envvar:: MLX_NAX_KSPLIT

Enable K-split across all four simdgroups in the staged NAX affine
quantized matmul kernel (``qmm_t_nax_staged_ksplit_impl``) when
``M <= 16`` (reachable at ``8 <= M <= 16`` on M5+ GPUs with neural
accelerators). The default is ``0`` (opt-in), because 4-way K-split
changes the FP32 K-reduction summation order relative to the unsplit
staged loop and to ``M > 16`` rows. Set it to ``1`` to opt in. Setting
``MLX_QMV_BATCH_INVARIANT=1`` disables K-split even when ``MLX_NAX_KSPLIT=1``
is set. MLX reads this variable once in each process.

Advanced tuning
---------------

Expand Down
47 changes: 47 additions & 0 deletions fork.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,50 @@ def:
globs:
- "mlx/backend/metal/kernels/quantized_nax.h"
- "mlx/backend/metal/kernels/fp_quantized_nax.h"
- title: "NAX small-M K-split affine GEMM and double-buffered NVFP4 GEMM/QMV"
description: |
Two M5-series NAX quantized Metal kernel optimizations ported from the Muse
Glimmer 30B challenge line (`Layr-Labs/mlxfast-muse-glimmer-30b-engine`
commits `80b35945` and `9976cc74`):
- `qmm_t_nax_staged_ksplit_impl` in `quantized_nax.h`: when `M <= SM` (`M <= 16`,
reachable at `8 <= M <= 16` on M5+ NAX via `qmm_splitk_nax` and `qmm_nax`)
and `(BM / SM) * 16 == BK` (`BM=64, BK=64, BN=64, WM=4, WN=1`), splits each
`BK` step into 16-column slices across all 4 simdgroups instead of leaving 3
simdgroups idle, then reduces FP32 partial tiles in simdgroup order in
threadgroup memory (FP32 throughout; changes only K-reduction summation order
for `8 <= M <= 16` affine 16-bit products on NAX). Gated via Metal function
constant `use_ksplit` (index 203) through `use_qmm_nax_ksplit(allow_ksplit)`
(`MLX_NAX_KSPLIT`, opt-in default `0`; disabled when `MLX_QMV_BATCH_INVARIANT=1`
or `allow_ksplit=false` for prefill-row invariance coordination with `#71`).
- `FPQuantizedNAXBlockLoader::fetch_nvfp4` / `store_nvfp4`, double-buffered
`Ws` in `fp_qmm_t_impl` / `fp_qmm_t_nax`, and bit-manipulation
`dequantize_fp4_x4_scaled` + `6 <= M <= 8` (`M <= 8`) peeled 1-ahead prefetch
in `fp_qmv_nax` (`fp_quantized_nax.h`): vectorizes NVFP4 (`bits == 4,
group_size == 16, !has_global_scale`, 16-bit `Ws` storage width `Wtype = bfloat`
across `fp_qmm_t_nax`, `fp_gather_qmm_t_nax`, and `fp_gather_qmm_rhs_nax` for
all activation dtypes, plus 16-bit `fp_qmm_n_nax` when instantiated) weight/scale
loads and `half2` E2M1 dequantization (0-ULP bit-identical to stock over all
4,096 `(fp8_e4m3, fp4_e2m1)` pairs), guarded by `K > 0`, `K % 32 == 0`, and
16B `wl` / 2B `scales` alignment checks.
- `tests/nax_quantized_kernel_tests.cpp` logs GPU architecture and NAX status,
covers M5-routed affine K-split (split-K and direct staged, asserting bitwise
prefix parity when K-split is off and bitwise K-split dispatch on NAX when
`MLX_NAX_KSPLIT=1` via the `nax_ksplit_enabled` ctest entry), unbatched/batched
double-buffered NVFP4 GEMM, `6 <= M <= 8` peeled QMV, NVFP4 MoE gather
(`fp_gather_qmm_t_nax` and `fp_gather_qmm_rhs_nax`), and genuinely misaligned
4B/1B offset fallback bitwise parity.
globs:
- "mlx/utils.h"
- "mlx/backend/metal/kernels.h"
- "mlx/backend/metal/jit_kernels.cpp"
- "mlx/backend/metal/nojit_kernels.cpp"
- "mlx/backend/metal/quantized.cpp"
- "mlx/backend/metal/kernels/quantized_nax.h"
- "mlx/backend/metal/kernels/fp_quantized_nax.h"
- "docs/src/usage/environment_variables.rst"
- "tests/nax_quantized_kernel_tests.cpp"
- "tests/CMakeLists.txt"
- ".github/workflows/fork-tests.yml"
- title: "Run-time template of the head-dim split NAX attention"
description: |
Upstream's head-dim split attention (`attention_nax_dsplit`, the NAX full-attention
Expand Down Expand Up @@ -772,6 +816,8 @@ def:
under PRoot (`.github/scripts/fedora-proot/`); the Fedora x86_64 job and
the protected Pages publisher stay on GitHub-hosted runners.
`FORKDIFF.md` describes the qualification and watchdog time bounds.
`AGENTS.md` adds the "Working in our MLX forks" section permitting agent-assisted
contributions in Layr-Labs forks (`56e12b00` on `main`).
`darkbloom-watchdog.yml` remains unchanged;
a rerun keeps these static labels. On a Darkbloom Sandbox that has
no Darkbloom cache, `fork-build-cache` sets `CI_ACTIONS_CACHE=off` and every
Expand Down Expand Up @@ -825,6 +871,7 @@ def:
- ".github/scripts/test-ci-resource-sampler.py"
- ".github/scripts/test-runner-sizes.py"
- "scripts/check_forkdiff.py"
- "AGENTS.md"
- "FORKDIFF.md"
- "docs/ci-codeql-native.md"
- "README.md"
Expand Down
13 changes: 11 additions & 2 deletions mlx/backend/metal/jit_kernels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1293,7 +1293,9 @@ MTL::ComputePipelineState* get_qmm_nax_kernel(
metal::Device& d,
const std::string& kernel_name,
const std::string& template_def,
const std::string& mode) {
const std::string& mode,
const std::string& hash_name,
const metal::MTLFCList& func_consts) {
const auto& lib_name = kernel_name;
auto lib = d.get_library(lib_name, [&]() {
std::string kernel_source;
Expand All @@ -1306,7 +1308,14 @@ MTL::ComputePipelineState* get_qmm_nax_kernel(
template_def);
return kernel_source;
});
return d.get_kernel(kernel_name, lib);
if (func_consts.empty()) {
return d.get_kernel(kernel_name, lib);
}
return d.get_kernel(
kernel_name,
lib,
hash_name.empty() ? kernel_name : hash_name,
func_consts);
}

MTL::ComputePipelineState* get_gather_qmm_nax_kernel(
Expand Down
4 changes: 3 additions & 1 deletion mlx/backend/metal/kernels.h
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,9 @@ MTL::ComputePipelineState* get_qmm_nax_kernel(
metal::Device& d,
const std::string& kernel_name,
const std::string& template_def,
const std::string& mode);
const std::string& mode,
const std::string& hash_name = "",
const metal::MTLFCList& func_consts = {});

MTL::ComputePipelineState* get_gather_qmm_nax_kernel(
metal::Device& d,
Expand Down
Loading
Loading