Skip to content

lfm2: d1-3B on CUDA, text and pictures, answering System One and decisions - #844

Merged
ashhart merged 6 commits into
ashhart:mainfrom
plotarmordev:feat/lfm2-d1-cuda
Oct 16, 2026
Merged

ashhart merged 6 commits into
ashhart:mainfrom
plotarmordev:feat/lfm2-d1-cuda

Conversation

@plotarmordev

Copy link
Copy Markdown
Contributor

What this changes

d1-3B (Liquid AI's LFM2-VL decision model) on CUDA in the native engine, answering POST /v1/systemone and /v1/decisions. On top of #842 (tokenizer) and #843 (System One and the decide hook); their commits come first here.

The recipe this ports, with its container build, checkpoint checksums and benchmarks, is github.com/plotarmordev/d1-tensorfold. That repository goes public within the next 24 hours; until then the link does not resolve.

  • Engine (native/lfm2.zig, families/lfm2/model.zig): a decision engine, not a lane engine. Each request is one prompt pass over the shared state (the trunk) and a branch a question, packed on one row axis; each branch's labels are read at its last row (labelLogits), so the vocabulary head is not computed for System One. Branches are packed into passes within the context, 256 branches and 4,096 labels. Chat and completions are refused (Info.decides).
  • Text model (lfm2.cu, kernels.zig, config.zig, weights.zig): LFM2's gated short convolution with each row's taps in its own sequence (branch, then trunk), QK RMS norm and rotary, add-norm and SiLU gate in bf16 with f32 sums, the projections as cuBLASLt GEMMs (w1|w3 and q|k|v stacked at load).
  • Attention (prefill_attention.cu, new prefill_attention_rows.cuh, attention.zig): the existing prompt-attention kernel's body moves to a header included by each kernel, and gains three compile-time switches: a key range a block (a branch sees the whole trunk, then its own rows causally), no mask (the vision tower), and a head dim padded inside the kernel (72 to 80). New instantiations: tree_kernel<64,8,4,8> (LFM2's GQA, 32 by 8 heads) and full_kernel<72,8,8> (SigLIP2). Nemotron's pattn_kernel<128,8,8,8> compiles to byte-identical SASS (cuobjdump -sass against main's build).
  • Pictures (image.zig, png.zig, jpeg.zig, resample.zig): data URL to RGB as Pillow decodes it (PNG all bit depths and colour types, baseline and progressive JPEG, EXIF orientation), the 1024×1024 pixel cap, then Lfm2VlImageProcessor's resize, 512-pixel tiling and thumbnail, normalization and 16-pixel patches, and the placeholder ids. Malformed files are refused, never trapped. The decoders port libjpeg-turbo and Pillow's arithmetic so pixels are byte-equal; credits and licenses are in THIRD_PARTY_NOTICES.md and LICENSES/.
  • Vision tower (vision.zig, lfm2_vision.cu): SigLIP2's NaFlex tower (position grid resized with antialiasing per tile, 27 pre-LN layers, tanh GELU) and Lfm2VlMultiModalProjector (pixel unshuffle, exact GELU), a tile at a time; features replace the placeholder rows' embeddings. Linear biases run in cuBLASLt's epilogue where the plain GEMM's algorithm takes it (Linear.initEpilogue, runBias; Linear.init and every existing caller unchanged), else in a bias kernel with the same single rounding.

Receipt

  • Environment: this branch (main 2962468f + tokenizer: an ASCII class inside (?i:) folds to its case variants #842 + server: /v1/systemone, llama-server's System One request, through an engine decide hook #843), Zig 0.17.0, CUDA 13, DGX Spark (GB10, sm_121), LiquidAI/d1-3B BF16 (model_type: lfm2_vl), --context 32768. Reference: Transformers' bf16 answers and the recipe's TensorFold Python 0.6 engine (container d1-tensorfold), both on the recipe's parity set (48 text requests and one picture, 99 questions).

  • Exactness (this head's build):

    top answer agrees max abs prob diff p50 p95
    this engine vs Transformers bf16 98 of 99 0.0267 0.0035 0.0206
    Python 0.6 engine vs Transformers bf16 98 of 99 0.0251 0.0039 0.0186
    this engine vs the Python 0.6 engine 99 of 99 0.0209 0.0013 0.0083

    The picture question ("how many cats": one / two / more) gives "two" at 0.98388, Transformers 0.98412.
    tf-cuda-test lfm2-kernels: 41 of 41 comparisons byte-equal to host references that round as torch's bf16 modules do. lfm2-attention: causal, tree (trunks 0 to 3,400, 1 to 64 branches) and full attention within an f64 bound; every branch bit-equal to causal attention over trunk ++ branch. lfm2-vision: the tower's kernels byte-equal to host references, cuBLASLt's bias epilogue byte-equal to the f32 GEMM plus bias on exact inputs, and three pictures' features (1 to 7 tiles) against Transformers' CPU bf16 tower (cosine ≥ 0.998). glue (Nemotron): 1,242 of 1,242 byte-equal.

  • Decode speed: n/a, a decision writes no tokens.

  • Prompt speed, the recipe's bench (bench/systemone_bench.py, 3 rounds of 50, two alternating starts of each engine), p50:

    cell Python 0.6 engine this engine
    one question, 39 tokens 25.30 / 25.36 ms 24.74 / 24.68 ms
    three questions, 116 tokens 28.61 / 28.89 ms 27.64 / 27.80 ms
    3,446-token state 272.5 / 274.1 ms 261.7 / 260.5 ms
    one picture, 145 tokens 44.67 / 48.37 ms 44.75 / 44.87 ms
    64 packed states 39.0 / 37.9 a second 40.8 / 40.6 a second

    The Grill's systemone A/B/A2 on the recipe's audited plan (100 bursts a cell): the 3,446-token state PASS (272.6 → 259.1 ms) and 64 packed states PASS (40.4 → 42.1 a second); one question, three questions and the picture INCONCLUSIVE (means within 1%: 24.6 → 24.4, 27.7 → 27.5, 43.0 → 43.0 ms; single-request spread wider than the plan's ±5% gate). No failures in any arm.

  • Tests run: zig build test 48/48 steps, 760/765 passed (5 skipped); server tests 210/210 and 110/110; zig build test-golden 309/319 equal, 0 unexpected differences; tf-cuda-test lfm2-kernels, lfm2-attention, lfm2-vision and glue PASS on GB10; zig fmt --check; lean_check as on main.

  • Not run, and why: Metal (the family is CUDA only; a Mac server has no lfm2_vl backend and refuses the checkpoint as before). Other CUDA GPUs than GB10 (not available). The NVFP4/quantized d1 variants (the checkpoint is BF16 only).

Checklist

  • Every token still goes through the lane rounds. No serial path, and nothing that needs drafts off. (Not applicable: d1 writes no tokens. A decision is one prompt pass, so the engine is a decision engine beside the lane engines, not a scheduler beside them; text requests are refused.)
  • Drafted output equals "draft": false, a resumed prompt equals a fresh one, and concurrent equals solo. (A request's answers do not depend on what else is packed with it beyond bf16 GEMM sums: every branch's attention is bit-equal to its solo causal pass.)
  • No precision traded for speed. Where the bits differ from the reference: the vision tower's attention rounds its probabilities to bf16 for the P·V product, as GPU flash attention does; the tree attention runs one f32 softmax over trunk and branch.
  • Prompt processing is no slower than the last release. (Nemotron's prompt attention compiles to the same SASS.)
  • The description says which platforms I ran: CUDA GB10 only.
  • New tests fail before the change, pass after it, and skip cleanly without their dependency. (The GPU tests run under tf-cuda-test; the CPU tests, including the picture goldens and a seeded set of truncated and altered files, run in zig build test.)
  • 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.

@ashhart
ashhart merged commit 4b9cf62 into ashhart:main Oct 16, 2026
4 checks passed
ashhart added a commit that referenced this pull request Oct 16, 2026
ashhart added a commit that referenced this pull request Oct 16, 2026
#626's three loader commits are in both histories as the same patches. The tree keeps the HIP series' qwen3_5/weights files and zig/build/hip.zig (one coreModule), drops qwen3_5/weights/experts.zig (core/quant stacks the experts now), and puts #626's CHANGELOG line before #631's.
ashhart added a commit that referenced this pull request Oct 16, 2026
Qwen3.5, Qwen3.6 and Flash Next on AMD GPUs through the opt-in HIP build (#617, #621, #626, #631, #697, #655,
#811-#818), bracket classes in the tokenizer (#842), System One requests (#843), the shared norm's loads (#796) and
d1-3B on CUDA (#844). CHANGELOG keeps every Unreleased line from both sides, this side's first. The kernel contract's
tf_rms_rows_bf16 record takes #796's values on sm_86, sm_89, sm_120 and sm_121; its sm_80 slot (from #846) takes the 32
static bytes #796 leaves, with the 79 registers sm_80 had.
ashhart added a commit that referenced this pull request Oct 16, 2026
The CUDA LFM2-VL family (d1-3B, #844) lives at zig/src/families/lfm2/ and zig/src/native/lfm2.zig, the paths this
Metal family used. The Metal LFM2-MoE family moves to zig/src/families/lfm2_moe/ (its root file lfm2_moe.zig) and
zig/src/native/lfm2_moe.zig, and tensorfold.zig exports it as lfm2_moe. A pure move: every file keeps its bytes but the
import lines that name the new paths.
@ashhart

ashhart commented Oct 16, 2026

Copy link
Copy Markdown
Owner

Thank you, @plotarmordev. d1-3B on CUDA arrived with host references for every kernel, the attention bound and the parity receipts.

It ships in TensorFold 1.0.12 under your name (ab7dd95ea), after #842 and #843, and your head is merged with no further change (4b9cf62f8), so this pull request shows as merged. We kept images.json and vision.json and left out the Python scripts that import the 0.6.6 engine (bdfc96554); your d1 recipe repository is a good home for them.

One change is ours, not a flaw in your work. The project loads outside libraries at run time rather than carrying code rewritten from them, so d1's pictures now decode through the shared decoder in zig/src/core/picture (820e2cb30), JPEG through libjpeg-turbo 3.1.4.1's libturbojpeg at Pillow's settings. Your goldens were the check: every picture gives the same RGB bytes, tiles, patches and placeholder ids as before.

On a GB10, lfm2-kernels is 41 of 41 byte-equal and lfm2-attention and lfm2-vision pass. System One answered the same way twice, alone and four at once, one question in 24.8 ms at the median. Nemotron's tokens and its prompt attention kernel are unchanged.

Posted by Ash's AI Assistant Synthena

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
…date

The contributor's commit landed above with the same code; the two fixture scripts that recorded its JSON goldens through the Python engine stay out of the tree, and the JSON stays. This keeps the PR's head in the history with no change to the tree.
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
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
 with the HIP series

ashhart#626's three loader commits are in both histories as the same patches. The tree keeps the HIP series' qwen3_5/weights files and zig/build/hip.zig (one coreModule), drops qwen3_5/weights/experts.zig (core/quant stacks the experts now), and puts ashhart#626's CHANGELOG line before ashhart#631's.
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
…rt#844 onto the candidate

Qwen3.5, Qwen3.6 and Flash Next on AMD GPUs through the opt-in HIP build (ashhart#617, ashhart#621, ashhart#626, ashhart#631, ashhart#697, ashhart#655,
ashhart#811-ashhart#818), bracket classes in the tokenizer (ashhart#842), System One requests (ashhart#843), the shared norm's loads (ashhart#796) and
d1-3B on CUDA (ashhart#844). CHANGELOG keeps every Unreleased line from both sides, this side's first. The kernel contract's
tf_rms_rows_bf16 record takes ashhart#796's values on sm_86, sm_89, sm_120 and sm_121; its sm_80 slot (from ashhart#846) takes the 32
static bytes ashhart#796 leaves, with the 79 registers sm_80 had.
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
The CUDA LFM2-VL family (d1-3B, ashhart#844) lives at zig/src/families/lfm2/ and zig/src/native/lfm2.zig, the paths this
Metal family used. The Metal LFM2-MoE family moves to zig/src/families/lfm2_moe/ (its root file lfm2_moe.zig) and
zig/src/native/lfm2_moe.zig, and tensorfold.zig exports it as lfm2_moe. A pure move: every file keeps its bytes but the
import lines that name the new paths.
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