[PR #4885] [Engine] Metal: opt-in silu(z) output gate for the GDN kernels (function constant 12) #5234

Open
opened 2026-11-07 16:33:40 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Niko1221/Strata/pull/4885
Author: @keithhu1982
Created: 11/1/2026
Status: 🔄 Open

Base: main ← Head: metal-gdn-silu-z


📝 Commits (1)

  • 1373b5a [Engine] Metal: opt-in silu(z) output gate for the GDN kernels (function constant 12)

📊 Changes

3 files changed (+459 additions, -4 deletions)

View changed files

📝 metal/kernels/gdn.metal (+12 -1)
📝 metal/kernels/gdn_prompt.metal (+13 -3)
➕ metal/tests/q36_gdn_parity.cpp (+434 -0)

📄 Description

Problem and result

The Metal GDN kernels (gdn_step in gdn.metal, gdn_post in gdn_prompt.metal) gate the gated-delta output with
sigmoid(z), the Flash-Next gate. Qwen3.6-35B-A3B (qwen35moe) uses rms(o) * gamma * silu(z), so the Mac engine cannot
reuse these kernels for it today.

This PR adds an opt-in function constant 12 (GDN_SILU_Z). When it is true the gate is silu(z) = z * sigmoid(z). When it
is unset or false, the arithmetic is unchanged. Nothing in the tree sets the constant yet, so no existing pipeline changes
behaviour.

#3932 (Qwen3.6-35B-A3B on the Mac engine). The scope has not been agreed there yet, so this is a draft. It is the smallest
self-contained piece of that port, and it is useful to look at on its own: it is the only change to a kernel that
Flash-Next also uses. The full port (separate metal/src/qwen35/ path, no changes under src/, include/, setup.py or
serve/) is on the metal-qwen36 branch of my fork, rebased on
c62d5e3a, if you want to see where it leads.

Approach

A function constant rather than a second kernel, so the Flash-Next pipelines compile to the same code and the two models
share one GDN implementation. The declaration is guarded because gdn.metal and gdn_prompt.metal can share one
translation unit.

metal/tests/q36_gdn_parity.cpp covers the Qwen3.6 shapes (h_k 16, h_v 32, C 8192, z 4096) with the constant on, against
a double-precision CPU reference: decode windows 1..8, verify/commit, fold keep 1..T bitwise, plane mode, gdn_post and the
prompt chain (gdn_pre / gdn_scanr_4 / gdn_post).

Validation

Revision: this branch on c62d5e3a. Apple M5 Pro, 64 GB, macOS 26.5, Metal build (cmake -S metal -B build-metal -G Ninja -DCMAKE_BUILD_TYPE=Release, then ninja -C build-metal gdn_parity gdn_prompt_parity q36_gdn_parity).

Passed:

  • build-metal/gdn_parity: ALL PASS. build-metal/gdn_prompt_parity: ALL PASS. build-metal/q36_gdn_parity: PASS.
  • The default path is bitwise unchanged. I added a temporary FNV-1a hash of every output buffer (y, state, history, f16 y)
    to gdn_parity and gdn_prompt_parity (not part of this PR) and ran the same binaries with
    STRATA_METAL_KERNELS pointing at the kernels of c62d5e3a and at this branch's, twice each:
    • gdn_parity: 66c63985874a54e1 with both kernel sets, both runs.
    • gdn_prompt_parity: 3016dcb224b5d59d with both kernel sets, both runs.
    • Control: changing the sigmoid constant 1.0f to 1.0000001f in the c62d5e3a kernels changes the hashes
      (fcf92c820ee74cf1, 09ce8dfb05bd4923), so the hash does see the gate.
  • With the full port on top (fork branch metal-qwen36), real model Unsloth UD-Q6_K_XL against llama.cpp Metal one token at
    a time (metal/tools/q36_gate.py all): mean KL 3.73e-4 (median 1.6e-8), top-1 99.7 % over 10 prompts x 128 steps; 8 of 10
    greedy runs identical for 128 tokens, the 2 divergences at llama.cpp near-ties (margins 0.08); layers and MTP gates PASS.

Failed:

None observed.

Not run:

  • A Flash-Next end-to-end run (same prompt, old and new binary). The Flash-Next model is not on this machine any more; the
    bitwise kernel hashes above are the evidence for the default path.
  • Other Apple chips (M1 to M4). Only the M5 Pro was tested.
  • CUDA, HIP and SYCL builds: the PR changes only metal/kernels/*.metal and adds one file under metal/tests/, which those
    builds do not compile.

Limits

On its own this PR does not make any model use silu(z). It only matters together with the Qwen3.6 Metal path from #3932.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Niko1221/Strata/pull/4885 **Author:** [@keithhu1982](https://github.com/keithhu1982) **Created:** 11/1/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `metal-gdn-silu-z` --- ### 📝 Commits (1) - [`1373b5a`](https://github.com/Niko1221/Strata/commit/1373b5a0f8fb3a36f0479de53607363b3da3681c) [Engine] Metal: opt-in silu(z) output gate for the GDN kernels (function constant 12) ### 📊 Changes **3 files changed** (+459 additions, -4 deletions) <details> <summary>View changed files</summary> 📝 `metal/kernels/gdn.metal` (+12 -1) 📝 `metal/kernels/gdn_prompt.metal` (+13 -3) ➕ `metal/tests/q36_gdn_parity.cpp` (+434 -0) </details> ### 📄 Description ## Problem and result The Metal GDN kernels (`gdn_step` in `gdn.metal`, `gdn_post` in `gdn_prompt.metal`) gate the gated-delta output with sigmoid(z), the Flash-Next gate. Qwen3.6-35B-A3B (`qwen35moe`) uses rms(o) * gamma * silu(z), so the Mac engine cannot reuse these kernels for it today. This PR adds an opt-in function constant 12 (`GDN_SILU_Z`). When it is true the gate is silu(z) = z * sigmoid(z). When it is unset or false, the arithmetic is unchanged. Nothing in the tree sets the constant yet, so no existing pipeline changes behaviour. ## Related discussion #3932 (Qwen3.6-35B-A3B on the Mac engine). The scope has not been agreed there yet, so this is a draft. It is the smallest self-contained piece of that port, and it is useful to look at on its own: it is the only change to a kernel that Flash-Next also uses. The full port (separate `metal/src/qwen35/` path, no changes under `src/`, `include/`, `setup.py` or `serve/`) is on the [`metal-qwen36`](https://github.com/keithhu1982/Strata/tree/metal-qwen36) branch of my fork, rebased on c62d5e3a, if you want to see where it leads. ## Approach A function constant rather than a second kernel, so the Flash-Next pipelines compile to the same code and the two models share one GDN implementation. The declaration is guarded because `gdn.metal` and `gdn_prompt.metal` can share one translation unit. `metal/tests/q36_gdn_parity.cpp` covers the Qwen3.6 shapes (h_k 16, h_v 32, C 8192, z 4096) with the constant on, against a double-precision CPU reference: decode windows 1..8, verify/commit, fold keep 1..T bitwise, plane mode, `gdn_post` and the prompt chain (`gdn_pre` / `gdn_scanr_4` / `gdn_post`). ## Validation Revision: this branch on c62d5e3a. Apple M5 Pro, 64 GB, macOS 26.5, Metal build (`cmake -S metal -B build-metal -G Ninja -DCMAKE_BUILD_TYPE=Release`, then `ninja -C build-metal gdn_parity gdn_prompt_parity q36_gdn_parity`). **Passed:** - `build-metal/gdn_parity`: ALL PASS. `build-metal/gdn_prompt_parity`: ALL PASS. `build-metal/q36_gdn_parity`: PASS. - The default path is bitwise unchanged. I added a temporary FNV-1a hash of every output buffer (y, state, history, f16 y) to `gdn_parity` and `gdn_prompt_parity` (not part of this PR) and ran the same binaries with `STRATA_METAL_KERNELS` pointing at the kernels of c62d5e3a and at this branch's, twice each: - `gdn_parity`: `66c63985874a54e1` with both kernel sets, both runs. - `gdn_prompt_parity`: `3016dcb224b5d59d` with both kernel sets, both runs. - Control: changing the sigmoid constant `1.0f` to `1.0000001f` in the c62d5e3a kernels changes the hashes (`fcf92c820ee74cf1`, `09ce8dfb05bd4923`), so the hash does see the gate. - With the full port on top (fork branch `metal-qwen36`), real model Unsloth UD-Q6_K_XL against llama.cpp Metal one token at a time (`metal/tools/q36_gate.py all`): mean KL 3.73e-4 (median 1.6e-8), top-1 99.7 % over 10 prompts x 128 steps; 8 of 10 greedy runs identical for 128 tokens, the 2 divergences at llama.cpp near-ties (margins 0.08); layers and MTP gates PASS. **Failed:** None observed. **Not run:** - A Flash-Next end-to-end run (same prompt, old and new binary). The Flash-Next model is not on this machine any more; the bitwise kernel hashes above are the evidence for the default path. - Other Apple chips (M1 to M4). Only the M5 Pro was tested. - CUDA, HIP and SYCL builds: the PR changes only `metal/kernels/*.metal` and adds one file under `metal/tests/`, which those builds do not compile. ## Limits On its own this PR does not make any model use silu(z). It only matters together with the Qwen3.6 Metal path from #3932. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Niko1221/Strata#5234
No description provided.