Skip to content

flashnext cuda: the MTP head's fc_h lane is sized for its own part rows - #4801

Merged
ashhart merged 5 commits into
ashhart:mainfrom
BadAd84:flashnext-head-part-rows-up
Nov 24, 2026
Merged

ashhart merged 5 commits into
ashhart:mainfrom
BadAd84:flashnext-head-part-rows-up

Conversation

@BadAd84

@BadAd84 BadAd84 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

What this changes

partRows(cap) sizes the Flash Next pass scratch's part for the out projection and the legacy lane
tile. The MTP head's fc_h lane is a third consumer it did not size for, so from a 128-row
scratch up to 1023 the head's zero slice started past the end of part. The branch adds the
head's term, one private function named after that consumer; nothing else changes.

The arithmetic, all of it in the tree: partRows(cap) was
max(2 * cap, out_sk * min(cap, 63)) with out_sk = 8; forwardHead takes
piece = max(1, min(256, s.cap / 2)); the lane runs streams = 4 rows per pass row
with fc_sk = 4 slices and writes one fp32 slice plus a second, zeroed one, so it needs
2 * streams * piece rows. At a 128-row scratch the piece is 64 rows, so the lane needs
512 rows against the 504 the old term gave (the reverted-tree run prints the same pair: it
needs 512 and is given 504); at 1,023 it is 2048 against 2046,
and from 2048 rows up 2 * cap covers the 2048 the lane asks for.

part is dims = 2560 fp32 a row, so 10240 bytes a row: at 65536 rows the head's term
is inert and part stays at 671088640 bytes, so a long cache pays nothing, and the largest cost of
the change is 15728640 bytes at a 256-row scratch.

  • Issue: Fixes #4800, the bug report this pull request carries: no matching report existed on this tracker
    (searched partRows, fc_h, flashnext scratch, MTP head scratch, CUDA_ERROR_INVALID_VALUE flashnext,
    cuMemsetD8Async; the one hit, dsv41: one request does not repeat bit for bit on one boot (main) #2715, is the DeepSeek-V4.1 bit-repeatability report), so Flash Next on CUDA: a 128 to 1,023-token prompt with drafts on writes past the MTP head's part #4800 is that report.
    The change completes the defect, so Fixes applies.
  • Stacked on: main, gated at 7bd7081, where every check below was run. Main has moved past that tip
    since (556094f at the time of writing), and none of its commits since touches
    zig/src/families/flashnext/, so this one commit still applies to main's tip; posting re-seats it only if the
    maintainer asks for a rebase, and a re-seat re-runs these checks.
  • atc overlap --json, run on this head from a full clone with main fetched first: the snapshot is partial
    (complete: false; limitations otherPullRequestsLimit, pullFilesUnavailable, pullRequestsLimit, timeLimit). Against upstream main at 7bd7081, whose merge base
    with this head is 7bd7081, it counts this commit's 2 changed files and lists
    40 open pull requests: 2 conflicts, 8 clean and
    30 unknown, the last explained by its pullFilesUnavailable limitation. None of those
    conflicts is this change's, measured rather than assumed: the GitHub API's full file list for every one of
    the 40, page by page, contains neither of our two files (0 of
    40 share either). Those 40 are the truncated list its pullRequestsLimit names, so
    the complete set was measured the same way: 9 of the 413 open pull requests change
    cuda_scratch_sizes.zig and none changes the test file, and none of the 9 edits partRows's
    body or the head's term, 7 of them adding a partRowsFor format dispatch beside it and the
    rest extending Sizes. The tool attaches this work to the existing open pull request Flash Next on CUDA: TF_FLASHNEXT_MTP_WEIGHTS selects how the drafter keeps its BF16 linears #3218
    from the same account, because this branch has no pull request yet, so the snapshot's per-entry sharedFiles
    counts are Flash Next on CUDA: TF_FLASHNEXT_MTP_WEIGHTS selects how the drafter keeps its BF16 linears #3218's files against the others, not this change's. Re-run it on the head at posting.

Checks on this head

  • Head commit: 1c06f68, tree 86ef754445071f1373c9dc5f265dd57bd6899d99, 2 files, 133 insertions,
    2 deletions, signed.

  • Lean checker, python3 tools/zig/lean_check.py, on the base and on this head: base 7689
    files and 165 problems, head 7690 files and 165
    problems, and tools/zig/lean_new.py reads 165 problems, 165 on the base, 0 new. The two lean_check.py runs exit 1
    because the tree carries its standing findings; the gate is the pair.

  • Tests run, with the command and pass, fail and skip counts:

    command result
    zig build test-cuda-host rc 0 in 256 s, Build Summary: 474/474 steps succeeded; 8673/8715 tests passed (42 skipped); the Flash Next module's binary ran 316 of 316
    the same on a tree with only partRows reverted (anchor count 1, base body line 1) rc 1 in 200 s, Build Summary: 472/474 steps succeeded (1 failed); 4936/4968 tests passed (31 skipped, 1 failed); the same binary ran 315 pass and 1 fail of 316
    zig build test-flashnext-mtp-plan-host rc 0 in 3 s
    zig build test-environment-host rc 0 in 0 s
    zig build -Dfixed-cpu test-gradient-serving test-qwen-serving-host rc 0 in 3 s
    zig build -Dfixed-cpu test-kernel-contract rc 0 in 289 s
    zig build -Dtarget=aarch64-linux-gnu compile-config-server rc 0 in 243 s, Build Summary: 8/8 steps succeeded
    zig build -Dtarget=aarch64-linux-gnu compile-cuda-tests rc 0 in 177 s, Build Summary: 10/10 steps succeeded
    zig build -Dnvcc=<toolkit>/bin/nvcc compile-cuda-tests rc 0 in 613 s, Build Summary: 183/183 steps succeeded
    zig build -Dnvcc=<toolkit>/bin/nvcc -Dtarget=aarch64-linux-gnu compile-cuda-tests rc 0 in 610 s, Build Summary: 183/183 steps succeeded
    zig build -Dnvcc=<toolkit>/bin/nvcc -Doptimize=fast fatbins install native rc 0 in 735 s, Build Summary: 367/367 steps succeeded
    zig fmt --check build.zig zig rc 0
    python3 tools/zig/cuda_common_check.py rc 0, cuda common: 55 local copies, 0 violations
    python3 tools/zig/cuda_fragment_check.py rc 0, cuda fragments: 2 fragments, 15 includes, 0 misses
    python3 -m unittest discover -s tools/zig -p cuda_common_check_test.py rc 0, 9 tests
    python3 tools/test_pdl_early_loads.py -v rc 0, 17 tests
    python3 pincheck.py <tree>/ rc 0, files 169 failing 0
  • python3 tools/pr_check.py --base 7bd7081e061ca22e3431d2342cd66104dd5fe320, run from this branch's worktree: rc 0 in
    7 s, merge base 7bd7081e061ca22e3431d2342cd66104dd5fe320; 2 changed files; lean 165 vs 165, 0 new, 0 error(s), 1 warning(s), the warning being
    its --pr NUMBER CHANGELOG advisory.

  • CI: not yet run. The branch is pushed for this pull request and no workflow has reported at this head; the host
    gates above are the ones this change reaches, run on a fresh git archive of the head whose every blob was
    checked against the commit (2 files differ from main, and the tree verified with no mismatch).

  • The sweep itself, caps 32 to 4,095 plus 4,096 to 262,144: 896 caps short on main's sizing, from
    128 to 1023, and 0 caps short after the change, over every consumer's
    write range (the out projection, a legacy out lane, and the head's lane at both its reachable and its widest
    pass).

  • Not run, and why: a device run, which is the only place the failure itself can be reproduced and the
    drafted-versus-plain comparison made (see the receipt). The atc overlap snapshot above is partial, and it is
    re-run on the head at posting.

Receipt

  • Main against branch: the head test is the measurement this change turns on. On main's partRows the new
    test fails by name: short at 896 caps, 128 to 1023, with first cap 128 needs 512 rows, partRows gives 504; on the branch it passes and the same
    binary runs 316 of 316 tests.

  • Boxes it reaches: the same Flash Next CUDA path on any box, since the allocation is host code; no per-box
    difference.

  • New switch? None. part grows only where the head's term exceeds the out projection's, and from a
    2048-row scratch up the term is inert.

  • Environment, with the TensorFold commit, Zig and backend toolchain, machine and GPU, checkpoint and
    revision: head 1c06f68; Zig 0.17.0; CUDA 13.3 (V13.3.73); one x86_64 machine with 16 threads running the
    host gates, and nvcc 13.3 compiling the CUDA tests for x86_64 and cross-compiling for aarch64-linux-gnu;
    checkpoint n/a, because no kernel, record or launch changed and the gates are host tests.

  • Paths and platforms the change reaches, which I ran and which I could not: the Flash Next prompt pass and
    the MTP head, on the host tests listed above for x86_64 and aarch64; the CUDA device path is the one I could
    not run.

  • Exactness, from tools/bench_concurrent.py --alone --serial or token_sha against "draft": false:
    n/a on the host. The sizing change leaves every kernel and every arithmetic path as it was; only the
    allocation grows, so a device run is the one that proves a drafted reply equals its "draft": false twin
    (see below).

  • Main against this branch, same machine and checkpoint, posted before asking for a run on our machines:
    not measured on a device. The host-side measurement above is the whole of what this change moves.

  • Decode speed before and after, from tools/bench_openai.py: n/a, no kernel or launch changed. The branch
    changes an allocation size, and on a served chunk of 3,072 rows the size does not change at all.

  • Prompt speed before and after: n/a for the same reason. The cold first request's ttft_s cannot move from a
    scratch allocation whose byte count is unchanged at the served chunk.

  • Licence of any new weights or drafter, or "n/a": n/a, no new weights or drafter.

Checklist

  • The title and description match the head commit, including the receipt and the stack.

  • New tests fail before the change, pass after it, and skip cleanly without their dependency.

  • Comments and docstrings are one line. No measurements or history in the source.

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

  • 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: unticked, because it takes the device run this pull request asks for. The change alters no
    arithmetic, so the risk it changes a reply is nil, but the claim needs that run and I will not tick it
    without it.

  • No precision traded for speed. If the bits change, the description says which sums change and why:
    no bits change.

  • Decode and prompt processing are no slower than main at one stream and the widest admitted; with
    --drafter, no slower than without it, greedy and sampled: unticked, because no device measurement was
    taken. The allocation is unchanged at the served chunk, which is why no host gate shows a difference.

@ashhart

ashhart commented Nov 24, 2026

Copy link
Copy Markdown
Owner

Thanks @BadAd84. The fix covers the missing fc_h scratch bound in both the allocation and its memory estimate. The regression catches the 128-row shortfall and covers the existing consumers and grouped head calls. The projection arithmetic stays unchanged; I have no code changes to request.

One small correction to the description: at 65,536 rows, part is 1,342,177,280 bytes, since it holds two cap-stride slices. That size is unchanged by this patch. The stated maximum increase of 15,728,640 bytes is correct.

  • Synthena

@joshuaswarren

Copy link
Copy Markdown
Contributor

sm_120 cross-check (RTX 5060 Ti, live), head 1c06f682, nvcc 13.5:

tf-cuda-test kernel-contract flashnext: 38/38 kernels pass gates 1-4, 0 fail, 0 missed controls. 30 limit problems - identical set to the sm_120 run on #4799's head (nvcc 13.5 vs 13.3 record drift, incl. fn_hc_upmix at +18688 B dynamic shared vs record). The fc_h part-rows sizing fix keeps every kernel bit-clean on the second arch.

BlivionIaG and others added 5 commits November 24, 2026 15:14
partRows sized the pass scratch's part for two consumers: an out projection's
slice plus the zero slice at cap rows, and a legacy lane tile's out_sk slices
below 64 rows. The MTP head's fc_h lane is a third one. It runs over streams *
rows rows (four streams by a pass's rows), and the lane writes one fp32 slice
of them and zeroes a second, so it needs 2 * streams * rows rows of part.

cuda_tiles.matmul zeroes at part + m rows and cuda_batch_ops.sumSlices then
reads that slice back, so a head pass over more than cap / 4 rows leaves part:
at four streams and a 256-row piece the lane needs 2,048 rows, against 2 * cap
below a 1,024-row scratch, and 4 * cap while the piece is half the scratch.

A head pass takes at most piece_rows rows (cuda_mtp_ops.zig's forwardHead and
headGroup), so any pass over 63 rows can ask for more than part below a
1,024-row scratch. The single-stream path, whose piece is half the scratch,
first overruns at 128 rows, a prompt chunk of about 128 tokens, and runs short
to 1,023.

partRows now takes the maximum over every consumer, the head's term being
2 * streams * min(cap, piece_rows). The term changes nothing from a 1,024-row
scratch up, where the out projection's two cap-stride slices already dominate,
so a long cache's part keeps its 671,088,640 bytes at 32,768 rows. Below that
it costs at most 15,728,640 bytes, at 256 rows.

The host test sweeps every cap against each consumer's write range, with the
head's lane under both a reachable pass and the widest pass the lane admits.
@ashhart
ashhart force-pushed the flashnext-head-part-rows-up branch from 1c06f68 to c470307 Compare November 24, 2026 15:14
@ashhart
ashhart merged commit cb440a0 into ashhart:main Nov 24, 2026
@ashhart

ashhart commented Nov 24, 2026

Copy link
Copy Markdown
Owner

Thank you @BadAd84. 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 that referenced this pull request Nov 24, 2026
ashhart added a commit that referenced this pull request Nov 24, 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.

4 participants