Skip to content

metal: Flash Next's hyper-connection stream RMS from one source (#2251) - #5060

Merged
ashhart merged 3 commits into
ashhart:mainfrom
jschmied:dedup-2251-stream-rinv
Nov 25, 2026
Merged

ashhart merged 3 commits into
ashhart:mainfrom
jschmied:dedup-2251-stream-rinv

Conversation

@jschmied

Copy link
Copy Markdown
Contributor

Part of #2251 (group: stream_rinv). Head cbcadb0, one commit on main 0a5c699.

What changes

  • Flash Next's decode HC rows (decode/fn_rows_hc.metal, stream_rinv) and its prompt glue (prefill/fn_prompt.metal, pf_rinv) had the same hand-written stream RMS: rsqrt(sum of nt partial sums / dims + eps).
  • It now lives once, as stream_rinv, in the new core/stream_rinv.metal. sources.zig puts it ahead of flashnext_prompt and flashnext_rows_hc; pf_rinv's one call is renamed. The file includes metal_stdlib itself, because fn_rows_hc's defines come before its own include.
  • Qwen4's HC library takes pf_hc_normed, pf_hc_mix and the RMS out of the prompt glue (flashnext/hybrid_hc_sources.zig). It now reads stream_rinv from qwen4/metal_hc.zig's prompt, which is core/stream_rinv.metal followed by fn_prompt.metal, as sources.flashnext_prompt holds them; source_test.zig uses the same constant.
  • The hash-named qa_hc_* files keep their own copies, as you asked.
  • Kernel names and signatures are unchanged. 7 files, +17 −16.

Core vs backend-only: core/stream_rinv.metal is the shared Metal piece; the rest is Flash Next's and Qwen4's Metal sources and their assembly. No CUDA files.

Same bits (M3 Pro, no tensor units; macOS 27.2, metal 32023.921, Zig 0.17.0; main 0a5c699 against this head)

  • IR, -std=metal4.1 -S -emit-llvm under the libraries' flags (-fmetal-math-mode=safe -fmetal-math-fp32-functions=fast), with and without tensor units. The emitted runtime libraries change only in these, and every function is identical, in the same order:

    • the two Flash Next prompt libraries (runtime_sources/flashnext.zig:56 and replay_prompt.zig:96's header with flashnext_prompt): 16 functions each;
    • Flash Next's HC rows in all five formats the loader builds: 6 functions each;
    • Qwen4's HC library, production and its four planted controls (10 of the 81 files emit-qwen4-metal-sources writes): 4 functions each.
    • Only ModuleID and the source path differ.
  • Kernel contract, tf-kernel-contract --only, main = head (logs identical apart from timings):

    • Qwen4 native HC: 4 pass gates 1-4, 0 fail; 13/13 controls red; 0 limit problems.
    • --only flashnext stops with error: UnknownLaunch after PASS limits: 42 ... 0 problems on main as on this head (as in metal: the two pairs of u32 copy kernels from one source (#2251) #4740), and its labels don't cover the prompt glue or the HC rows. So tf-flashnext-widths ran on both arms with identical output: HC rows 726 checks and prompt 637 checks PASS, every row's bits equal in every window, its negative control red.
  • Family checks: test-qwen4-host test-qwen4-native-mixers-host test-qwen4-metal-sources test-qwen4-prefill-widths-host test-flashnext-prefill-pairs-host test-flashnext-scheduling-host test-kernel-symbols-host on the Mac: 117/117 steps, 212/212 tests. On Linux arm64, test-qwen4-host test-qwen4-native-mixers-host: 17/17 steps, 135/135 tests.

  • Served hash: not run. Every served library's IR is identical, so no reply can change; for the same reason no seeded, resume, concurrency or refusal checks against main.

  • x86 CI green on a merge of this head with two sibling metal: unify duplicated kernels (attention partials, norms, gemm_nax wrappers, glue, RoPE) #2251 groups: merge e239bd5c70 of cbcadb0 (stream_rinv), 3d6950a (rotary scalar) and 36cd5f9 (quantized embed) on main 0a5c699. zig build -Dfixed-cpu test-host test test-golden test-kernel-contract test-server-security test-pull-security fuzz-cortheon check-release-paths: 2622/2622 steps, 27591/27761 tests (170 skipped); check-native-cuda-host compile-cuda-tests 21/21; zig fmt --check and git diff --check clean. pr_check on this head alone: 0 errors, lean 164 vs 164 on main, 0 new.

Proposed CHANGELOG line (for you to place):

  • No personal data, machine names, internal hosts or local paths in the diff or its commits. No AI attribution lines.

@ashhart

ashhart commented Nov 25, 2026

Copy link
Copy Markdown
Owner

Thanks @jschmied. The shared RMS keeps the same additions and rsqrt, and both Flash Next callers and Qwen4's extraction use it correctly. The recorded compiler and width comparisons support this source move; the separate baseline contract failure stays recorded.

This looks ready to land through the usual checks.

  • Synthena

ashhart and others added 3 commits November 25, 2026 16:28
…art#2251)

fn_rows_hc.metal's stream_rinv and fn_prompt.metal's pf_rinv had the same body. core/stream_rinv.metal now holds
it, ahead of both libraries in sources.zig; the Qwen4 HC source reads it from there.
@ashhart
ashhart force-pushed the dedup-2251-stream-rinv branch from cbcadb0 to 5ef3946 Compare November 25, 2026 16:29
@ashhart
ashhart merged commit 46a748c into ashhart:main Nov 25, 2026
12 checks passed
@ashhart

ashhart commented Nov 25, 2026

Copy link
Copy Markdown
Owner

Thank you @jschmied. This is merged into main with your commits under your own name, so you're listed as a TensorFold contributor. It ships in the next release, credited to you in the changelog and the release notes.

ashhart added a commit to jschmied/TensorFold that referenced this pull request Nov 25, 2026
ashhart added a commit that referenced this pull request Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants