Skip to content

nemotron metal: overlap and preload taught-pocket decode work - #775

Merged
ashhart merged 3 commits into
ashhart:mainfrom
ThinkOffApp:pocket-mac-overlap
Oct 13, 2026
Merged

ashhart merged 3 commits into
ashhart:mainfrom
ThinkOffApp:pocket-mac-overlap

Conversation

@ThinkOffApp

Copy link
Copy Markdown
Contributor

What this changes

On Metal, a Nemotron layer that has learned facts (its pocket has used > 0) ran the pocket as three dispatches behind barriers, after the shared expert's down projection. On my M5 Max that made a model with four learned facts decode about 10% slower than the same model untaught.

This PR runs that work beside work the layer already does:

  • A new kernel, tf_pocket_in, does tf_train_lora_in's sums with relu² applied at the store. Each lane loads its inputs 28 at a time before adding them, in the same order, so on every path I tested the bits did not change.
  • It runs beside the shared expert's up projection and the router. The unchanged tf_train_lora_out runs beside the routed experts' down projection. A taught layer adds two dispatches and no additional barriers.
  • Untaught folders, the trainer and prompt chunks over 16 tokens run the same code as before. Chunks of 16 tokens or fewer use the decode path, so they get the change too, with the same tokens.

Result: no measurable overhead on this M5 Max with four learned facts, drafted (with tools/bench_openai.py) and undrafted; answers identical on the tested paths.

Part 3 of #678. The 1.0.8 numbers that showed this cost are Ash's. The CUDA findings in #678 are @philip-pentatonic's, and the CUDA side is untouched by this PR.

Receipt

  • Environment: v1.0.8 (0efbd447) against this branch (8b766bc6). Both were built with zig build dist -Dversion=1.0.8 -Ddist-host-only=true and Zig 0.17.0. The engine is native Metal, with no MLX or PyTorch at runtime.
    • MacBook Pro, M5 Max, 128 GB, macOS 27.0.1.
    • Nemotron 3.5 Lightning 30B-A3B MLX 4-bit at d9d758fb: as published, and a copy with four learned facts.
  • Exactness, from tools/bench_concurrent.py --alone --serial: on both builds and both folders:
    • every concurrent reply equals its solo run (624 of 624 per build);
    • every solo run equals "draft": false (96 of 96 per build);
    • every request's token_sha is the same before and after.
  • Decode speed, from tools/bench_openai.py: drafted, the taught model is 7.6% faster and the untaught one unchanged. Taught/untaught goes from 0.94 to 0.99.
    • Undrafted, with the dev CLI, it goes from 0.901 to 0.999 (161.8 to 179.3 tok/s against 179.6 untaught).
  • Prompt speed, from tools/prefill_cold.py: not run, because 1.0.8 does not have it. prefill_mixers.zig is unchanged. A 15-token prompt, which takes the decode path, started 0.6 ms sooner taught and the same untaught.
  • Tests: zig build test -Dcpu=apple_m1: 1506 of 1514 passed, 8 skipped, and one inherited failing step that also fails on v1.0.8.
    • The new kernel test compares tf_pocket_in with the old two kernels byte for byte.
    • lean_check.py: 539 before and after.
  • Not run: M1 to M4, the M5 Ultra, CUDA, more than four facts, and resumed equals fresh.

Checklist

  • Every token still goes through the lane rounds. No serial path, and nothing that needs drafts off.
  • Drafted output equals "draft": false, a resumed prompt equals a fresh one, and concurrent equals solo. Drafted and concurrent: yes. Resumed equals fresh: not run.
  • No precision traded for speed. Each lane adds the same products in the same order, and the byte and token comparisons above found no difference on the tested paths.
  • Prompt processing is no slower than the last release. Not measured with prefill_cold.py, which 1.0.8 does not have. Chunks over 16 tokens run the same dispatches, and a 15-token prompt was not slower.
  • Platforms: Metal on an M5 Max only. Not M1 to M4, not the M5 Ultra, not CUDA.
  • The new test passes after the change. It fails with the relu removed, with a batch added in reverse, and with the rest's last element dropped. It skips without a Metal device. It cannot run before the change, because the kernel is new.
  • Comments and docstrings are one line. No measurements or history in the source.
  • No personal data, machine names, internal hosts or local paths. No AI attribution lines.

Full receipt

Where the cost was

The dev CLI's tensorfold run … --no-drafts --profile 32 times each dispatch alone. On the four-fact folder (16 taught layers), two runs per build:

Kernel, µs a call v1.0.8 pocket moved only (5cf7c144) this PR
tf_train_lora_in (v1.0.8) or tf_pocket_in 28.5, 28.1 28.4, 28.2 4.6, 5.6
upRelu2, which tf_pocket_in runs beside 21.0, 21.3 21.6, 21.7 20.9, 21.5
  • In v1.0.8, the pocket's input kernel sat on the critical path between barriers in each of the 16 layers.
  • Moving it beside upRelu2 should hide most of its 28 µs, and loading ahead brings it to about 5 µs, shorter than upRelu2. That overlap is my reading of the separate timings, not a GPU trace.
  • With 1 to 4 neurons in a layer and one simdgroup per neuron, the kernel is latency-bound, so each lane's chain of loads set its time.
  • Timed alone in a small harness, the old kernel takes 27.5 to 35.5 µs and the new one 4.5 to 6.4 µs, with byte-identical outputs.

TensorFold's clients

  • 16 servers: both builds × both folders × two rounds, in two series.
    • Order: before-taught, after-taught, after-untaught, before-untaught, then reversed.
    • 4 minutes idle before each, drafts on (the default).
  • In the second series every server started with an empty draft-cost cache (see the note below).
  • The clients' defaults, then CONTRIBUTING.md's forms:
python3 tools/bench_openai.py http://127.0.0.1:PORT local-model --output decode.json
python3 tools/bench_openai.py http://127.0.0.1:PORT local-model --tokens 256 --reps 3 --temperatures 0 --output decode-256.json
python3 tools/bench_concurrent.py http://127.0.0.1:PORT local-model --alone --serial --output concurrent.json
python3 tools/bench_concurrent.py http://127.0.0.1:PORT local-model --tokens 256 --reps 3 --temperatures 0 --levels 1,4 --alone --serial --output concurrent-1-4.json

bench_openai.py, drafted, one stream. tok/s, the mean of two rounds:

Cell Untaught before Untaught after Taught before Taught after
code, 64 tokens, greedy 244.7 248.6 226.8 246.9
chat, 64 tokens, greedy 252.5 256.9 236.4 253.9
code, 256 tokens, greedy 339.0 340.1 321.4 340.9
chat, 256 tokens, greedy (the folders' texts differ) 228.8 236.2 234.4 247.7
code, 64 tokens, temperature 1, seeds 1234-1238 279.1 282.2 260.2 277.1
chat, 64 tokens, temperature 1, seeds 1234-1238 240.1 241.2 221.0 243.7
  • Greedy cells, both rounds (medians with ranges):
    • taught after/before is 1.076 (1.037-1.092);
    • untaught after/before is 1.011 (0.997-1.069).
    • Where both folders write the same tokens (the first three rows), taught/untaught is 0.940 (0.917-0.953) before and 0.994 (0.981-1.011) after.
  • The first series, run with the cached cost tables, gave 1.079, 1.003, 0.930 and 0.998.
    • In it both folders drafted the same tokens per round on those three cells. v1.0.8's pocket adds 0.58-0.77 ms to each drafted round, the same size as its undrafted cost below. This PR adds −0.17 to +0.04 ms.
  • bench_concurrent.py:
    • concurrent equals solo, 624 of 624 per build;
    • solo equals "draft": false, 96 of 96 per build;
    • no failures.
    • The two folders answer 7 of 22 distinct requests differently, so the check can see the pocket.
  • Time to first token, cold, from bench_openai.py:
    • the 15-token code prompt (decode path): 19.35 → 18.73 ms taught, 18.91 → 18.57 untaught;
    • the 35-token chat prompt (prefill path): 45.80 → 45.80 taught, 45.29 → 44.89 untaught.

How far to trust these:

  • The draft cost tables. The engine times a draft-window cost table on a build's first run and caches it per executable.
    • Here the two builds' cached tables were timed five hours apart and differed by up to 75%.
    • With them, the untaught 4-stream aggregate came out 5-11% lower on this PR, although its code did not change. Hence the second series.
    • A drafted multi-stream before/after needs both tables timed under the same conditions.
  • bench_concurrent.py's throughput. I do not quote it. It runs during 90 s of 4-stream decoding, while this MacBook's GPU throttles, and it swung by up to 48% between two servers of the same build and folder.
  • Background GPU load. The desktop kept 30-35% of the GPU busy throughout, in both builds' runs.

Undrafted decode speed

The dev CLI's tensorfold run: greedy, one 36-token prompt, 256 tokens, drafts off. Ten bursts, each with one run per build and folder, order rotated, 90 s idle before each burst:

Build Untaught tok/s Taught tok/s Taught / untaught, same burst Extra ms a token, same burst
v1.0.8 179.8 161.8 0.901 (0.894-0.912) +0.61 (+0.55 to +0.65)
pocket moved only 179.8 173.6 0.965 (0.959-0.987) +0.20 (+0.07 to +0.24)
this PR 179.6 179.3 0.999 (0.992-1.012) +0.005 (−0.07 to +0.05)

On the server with my own client (256 tokens, 8 prompts, "draft": false, 12 measured requests a mode), the round run with the machine otherwise quiet, thinking off / on:

Build Untaught tok/s Taught tok/s Taught / untaught
v1.0.8 176.1 / 174.8 157.6 / 155.8 0.895 / 0.891
pocket moved only 176.0 / 173.8 168.1 / 164.6 0.955 / 0.947
this PR 179.7 / 173.0 177.4 / 175.8 0.987 / 1.016
  • This GPU slows by about a third after a few minutes of steady decoding, hence the idle gaps and short runs.
  • Three more server rounds ran while other jobs kept the CPU busy. They show v1.0.8's cost, but cannot separate the last two builds.
  • Against Ash's M5 Ultra numbers (248 tok/s untaught, 213 after ten facts): his ten-fact cost is 0.663 ms a token, and four facts cost 0.61 here before this change and 0.005 after. If his cost is the same per-layer latency, ten facts would decode near his untaught speed. That is my inference, not a measurement: I have no M5 Ultra and no ten-fact folder.

Same tokens

Greedy, thinking off and on, drafts on and "draft": false, v1.0.8 against this PR:

  • Kernel test. tf_pocket_in against tf_train_lora_in then tf_pocket_relu2, compared byte for byte.
    • It covers 1 and 16 rows, 1, 6 and 64 neurons, and widths 2,688 and 2,728; the second leaves every lane a rest after its batches.
    • The two outputs start from different fill bytes, so a value either kernel skips would show.
  • Server replies. The 18 fact questions from Living Weights: conflicting facts, updates and forgetting (measurements + proposal) #678 in four modes: 72 of 72 equal on the taught folder and 72 of 72 on the untaught one. Equal means token_sha, text, reasoning, token count and finish reason.
  • Full token ids. The dev CLI's run --report: 60 of 60 id arrays identical on each folder, 9,440 and 17,298 tokens.
  • Concurrent, taught and untaught streams in one batch. A --slide server ran a learn request while 4 clients asked the 72 questions. 72 of 72 replies equal their solo runs and v1.0.8's. The 353 streams the learn request finished each have an exact twin in v1.0.8's run.
  • Negative control. The untaught and taught folders differ on 56 of 72 server replies and 50 of 60 id arrays, so these checks can see the pocket.
  • Bytes. Every served copy's *.safetensors and config.json hashed the same before and after.

Tests

  • zig build test -Dcpu=apple_m1: 1506 of 1514 passed, 8 skipped.
    • One step fails, on "a split checkpoint keeps later tensors in the file, packs the rest, and stages the same bytes". It fails the same way on v1.0.8 (1505 of 1513, 8 skipped): the test logs an error on purpose, and the runner counts it.
  • python3 tools/zig/lean_check.py: 539 problems on v1.0.8 and 539 on this branch, all inherited.
  • zig fmt --check on the changed files: clean.
  • zig build dist-smoke: passed.

…'s up and the routed experts' down, no barriers

A taught MoE layer ran its pocket after the shared expert's down projection as three dispatches, each behind a barrier
(tf_train_lora_in, tf_pocket_relu2, tf_train_lora_out), and the routed experts' up projection ran beside the last of
them instead of beside the shared down. Now tf_pocket_in, tf_train_lora_in's loop and sum with tf_pocket_relu2 at its
store and no row lengths, runs beside the router and the shared expert's up: it reads only x and the pocket's weights
and writes only the pocket's z. tf_train_lora_out runs beside the routed experts' down: z and the shared expert's sum
are done at the barrier before it, and the down projection reads and writes neither. Each run of taught rows gets one
of each, as before. A taught layer now adds two dispatches and no barriers, and the shared down is beside the routed
up again. Every product and sum keeps its order, and the pocket still adds into the shared sum after the residual and
the adapter, so a reply's tokens should not change. The prompt path and the trainer keep the three dispatches.

A test runs tf_pocket_in against tf_train_lora_in then tf_pocket_relu2 at 1 and 16 rows over 1, 6 and 64 neurons,
byte-equal on an M5 Max; with the relu taken out of tf_pocket_in it fails.
… docstrings as one line each, at most 120 columns

tools/zig/lean_check.py found the kernel's two-line comment and the two docstrings past 120 columns; the branch now
adds no problem over its base.
…fore it adds them, in the same order

Each lane of tf_pocket_in walked its 84 strided inputs one load and one add at a time, every add waiting on its own
load, so the kernel ran longer alone than upRelu2, which it runs beside. A lane now loads 28 of its x and up elements
before it adds them in tf_train_lora_in's order, then adds any rest one at a time as before; the simd_sum and the
store are unchanged, so every lane's sum keeps its bits. Alone in the dev CLI's profile on an M5 Max (the four-fact
folder, 16 taught layers, 32 steps, two runs) it takes 4.6 and 5.6 us a call instead of 28.4 and 28.2, against
upRelu2's 20.9 to 21.7, and that folder's 256-token greedy reply keeps its token ids.

The kernel test also runs a width of 2728, which leaves every lane a rest after its batches. It passes byte-equal on
the M5 Max; it fails with a batch's 28 added in reverse, and with the rest's last element dropped.
@ashhart
ashhart merged commit ff2f222 into ashhart:main Oct 13, 2026
2 checks passed
jschmied pushed a commit to jschmied/TensorFold that referenced this pull request Oct 13, 2026
@ashhart

ashhart commented Oct 13, 2026

Copy link
Copy Markdown
Owner

Thank you, @ThinkOffApp. Folding the pocket's dot and squared relu into one kernel, and putting both halves beside work that never touches their buffers, is a neat trick, and the 28-wide loads put tf_pocket_in well under the up projection it runs beside on your M5 Max. On an M3 Ultra it took 8% off a taught folder's decode. It landed in 1.0.9 under your name (2494fecfb, 128bd30cb, 889faf415) with the same code as your head, and your head is merged after it with no change to the tree (ff2f222a5), so GitHub shows this PR as merged.

What we checked on an M3 Ultra (no tensor units):

  • Your kernel test. It passes there: tf_pocket_in gives tf_train_lora_in then tf_pocket_relu2's bytes at 1 and 16 rows, 1, 6 and 64 neurons and both widths. With each batch of 28 added in reverse, the same test fails on that GPU (the first word differs in its last bits), so it catches an order change on an M3 too.
  • A taught folder. A clone of Nemotron MLX 4-bit was taught "I like blue." and "My dog is called Ginger." by the build before yours, then served by both builds. Every reply was equal token for token: the 17 release cells and 24 taught-question cells (both facts and two unrelated questions, thinking off and on, drafted and "draft": false). Drafted == plain on both builds.
  • Speed on that folder. Eight prompts, two servers a build in a mirrored order: 8.2% faster greedy (214.3 -> 231.9 tok/s) and 8.3% sampled. "draft": false decode moved more, about 163 -> 183 tok/s, since every row pays the pocket.

The prompt path and the trainer keep the three-dispatch pocket, as your PR says. We haven't run a taught folder on an M5 ourselves, so your M5 Max profile is the M5 figure for now.

@ThinkOffApp

Copy link
Copy Markdown
Contributor Author

Awesome @ashhart! good to see M3 ultras doing 8% on every reply.

The CUDA side is #787 built based on philip-pentatonic's suggestions from #678. On our GB10 with 1.0.8, a folder with 64 facts went from 87 to 100 tps undrafted, the untaught speed, with the same tokens. It also fixes a small bug that is already in 1.0.8: when the learner captures the graphs again while another sequence is bound, the own sequence kept the capture's placeholder sampling rule.

It is rebased on 1.0.9 and in draft while we re-run the receipt against 1.0.9. I'll mark it ready when that is in.

@ashhart ashhart mentioned this pull request Oct 14, 2026
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
The contributor's three commits landed above with the same code; this keeps the PR's head in the history with no change to the tree.
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