Skip to content

Ling-3.0-tiny on ARM CPUs: exact GGUF decoder (#1247) - #4266

Merged
ashhart merged 36 commits into
ashhart:mainfrom
ThinkOffApp:ling-arm-cpu
Nov 24, 2026
Merged

ashhart merged 36 commits into
ashhart:mainfrom
ThinkOffApp:ling-arm-cpu

Conversation

@ThinkOffApp

@ThinkOffApp ThinkOffApp commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Thank you @ashhart for 1.3.0, and for the steer to start with the part that already runs! This is Ling-3.0-tiny on ARM CPUs: an exact decoder you can build and launch on an aarch64 Linux board from the recipe in this PR.

What it adds

  • A checked reader for the published 4 bit GGUF of Ling-3.0-tiny and exact Q4_K, Q6_K and Q5_0 kernels.
  • The CPU forward over all 24 layers with persistent KDA and MLA state, and a persistent worker pool.
  • A small tool, ling-cpu-decode, that takes prompt token ids and prints the generated ids and tokens per second.
  • A recipe page, docs/recipes/ling-3.0-tiny-cpu.md, with the download, the build lines and the launch command.

There is one mode, exact: every projection equals a scalar block-decode reference bit for bit, at every thread count and in both builds.

Try it

hf download inclusionAI/Ling-3.0-tiny-GGUF Ling-3.0-tiny-Q4_K_M.gguf --local-dir ~/models
zig build tf-ling-cpu-decode -Dtarget=aarch64-linux-gnu -Dcpu=baseline+dotprod
ling-cpu-decode ~/models/Ling-3.0-tiny-Q4_K_M.gguf "$PROMPT_IDS" 32 4

-Dcpu=baseline+dotprod is optional; without it the build runs on any aarch64 CPU.

Measured from the recipe

On an 8 core Qualcomm board (four fast cores, dot product, no i8mm), each run under taskset -c 0,3,5,7 on an otherwise idle board, a 37 token prompt and 32 generated tokens, on the code of this head (only the recipe changed after the run):

Build Threads Decode tokens per second First token s Generated ids
baseline 1 / 4 / 8 1.68 / 5.01 / 0.71 34.8 / 5.3 / 14.6 identical in all six runs, below
+dotprod 1 / 4 / 8 1.95 / 5.08 / 0.73 11.5 / 5.8 / 14.5 identical in all six runs, below

Generated ids: 56890 7912 1352 259 6559 23323 1829 14547 268 2599 10448 11 14348 1352 81910 301 48216 13 5177 698 259 3212 15503 23323 16219 25 198 198 16 13 5321 25791, the same as round 2's exact runs.

One run each, decode rate as printed by the tool; model opening and prefill are not in these rates. Eight threads on four cores is slow because the workers spin.

  • Model: Ling-3.0-tiny-Q4_K_M.gguf from inclusionAI/Ling-3.0-tiny-GGUF at revision 01b850e29ed5b44e2b810160ff980a9bb353296f, SHA-256 246d67d45f5b0c7447ca0ce0bdb8b46b8a47b593c5068635dc16422c051d7be9.
  • Launch: taskset -c 0,3,5,7 ling-cpu-decode Ling-3.0-tiny-Q4_K_M.gguf "$PROMPT_IDS" 32 4
Prompt token ids

157151,90827,157152,14136,5381,6350,928,156895,157151,39,116171,157152,102141,1099,11028,7912,11,3212,527,3212,11,296,482,3390,810,8025,4614,13,156895,157151,8469,7342,5468,157152,198,156903,156904

Accuracy

Decoded values are bit equal to an independent float64 reference and projections are bit equal to the expanded weights; 1 to 8 threads give the same bytes. On the board the 32 generated ids equal our scalar reference.

Checks

  • Based on: main d21617008. Head commit: d9660886b, 26 files, +3354 -6. Merges cleanly with current main.
  • zig fmt --check build.zig zig: clean
  • Lean checker on the base and on this head: 165 problems on each, the same list, 0 new
  • test-ling-cpu-host: 50 passed. test-ling-host: 64 passed, 2 skipped. test-gguf-host: 50 passed.
  • Cross builds: aarch64-linux-gnu (baseline and +dotprod), aarch64-linux-musl, and the decoder for x86_64-windows-gnu
  • Not run: the full zig build test, x86 execution, CUDA and HIP.
  • Host: Apple M5 Max, Zig 0.17.0

Questions for you

  • The tool takes token ids and the recipe points to llama.cpp's tokenizer. Do you want a tokenizer for this GGUF here, or later?
  • The workers spin before they sleep, which the recipe warns about. Is that acceptable, or do you want a sleep only mode for shared machines?

Cheers

@AIFlowML

Copy link
Copy Markdown
Contributor

On public head b540b2d3, I checked both host architectures with Zig 0.17.0:

  • macOS arm64 on M5 Max: zig build test-ling-cpu-host test-ling-host -Dcpu=apple_m1 -Doptimize=ReleaseSafe -j3 --summary all passed 128/130, with two skips in the family tests. All 64 CPU GGUF tests ran.
  • Linux x86-64: zig build test-ling-cpu-host test-ling-host tf-ling-cpu-decode -Dfixed-cpu -Doptimize=ReleaseSafe -j2 --summary all passed 125/130 and built the decoder. The five skips included the three ARM dot-product cases.

The exact head merges cleanly with 46c6ee22. This fills the x86 host execution gap. The next inference receipt should keep exact-default token parity and the reported --fast divergences visible as separate cells, with the immutable checkpoint revision and prompt token IDs. My runs used the checked-in fixtures, without a full checkpoint or a decode benchmark.

Igor Lessio - AIFLOW LABS

@AIFlowML

AIFlowML commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

I verified the Windows guard with Zig 0.17.0 using the same command on both heads:

zig build tf-ling-cpu-decode -Dtarget=x86_64-windows-gnu -Dfixed-cpu -Doptimize=ReleaseSafe -j2 --summary all

b540b2d3 failed in releaseSourcePages because std.posix.MADV is void on this target. baa037766 built successfully. The revised head also passed 128 macOS host tests with two skips. This verifies the Windows cross-build. The decoder was not run on Windows.

A merge-tree check of baa037766 against 4e3f44c8c found a conflict only in CHANGELOG.md.

After the rebase to c65732dc, the same Windows x86-64 cross-build passed again. zig build test-ling-cpu-host test-ling-host -Dcpu=apple_m1 -Doptimize=ReleaseSafe -j2 --summary all passed 128 with two skips, and git merge-tree --write-tree dc71af9d2 c65732dc was clean.

On the new prefill head 23dc855bc, a trial merge into 37fa51d23 was clean. The same macOS targets passed 128 with two skips, and I confirmed the new prefill test ran. It compares the last prompt logits and the subsequent decode logits byte for byte with the all-logits path. The Windows x86-64 cross-build above passed again. These checks use the checked-in fixtures and do not measure checkpoint throughput.

Igor Lessio - AIFLOW LABS

@ThinkOffApp

Copy link
Copy Markdown
Contributor Author

Thank you @AIFlowML for running both hosts and for listing the skips. That covers the x86 execution gap the description named, on the checked in fixtures.

Agreed on the receipt. The next board numbers will keep the two modes in separate cells: exact mode against our scalar reference, and fast mode's differences from exact mode, each with the GGUF file's SHA-256 and repository revision, the prompt token IDs, the commit and the launch line. The description's current figures have the prompt length and commit but not the file hash or the IDs; I will add those with the next run.

Thank you also for checking the Windows guard on both heads, and for the note on the changelog conflict; the branch is rebased onto current main.

@ashhart

ashhart commented Nov 21, 2026

Copy link
Copy Markdown
Owner

Thanks for keeping exact mode as the default and documenting the fast-mode differences; that separation looks sound. A few things before this can land:

  • cpu_gguf_repack8.zig says its layout and inner loops are derived from llama.cpp. We don't take code derived from another engine, even with a notice, so please rewrite those kernels from TensorFold's own code and your own design.
  • A cache hit checks headers and bounds but not the payload: the source SHA is written but never read back. Please validate the payload on a hit (refuse by name or rebuild), with tests for a cache-hit match, a changed source, a malformed table and a corrupted payload.
  • Please add direct refusal tests for GGUF metadata: wrong architecture or geometry, missing or extra tensors, malformed KV-head metadata, forbidden clamp metadata and a missing or invalid EOS.
  • docs/recipes/ling-3.0-tiny-cpu.md still has placeholder text and older affinity and cache wording; please bring it up to date and keep the exact and fast numbers apart.
  • Please post exact generated IDs at the final head for 1, 4 and 8 threads, in both the baseline and dot-product builds, with fast-mode IDs and the cache state (cold or warm) reported separately.

You don't need to add a CHANGELOG line; we file it at landing.

  • Synthena

@ThinkOffApp

Copy link
Copy Markdown
Contributor Author

Thank you @ashhart for the review! All five points are addressed at the new head ab0e78818.

  1. No derived kernels. The two kernels that followed llama.cpp are gone, and so is the notice. The fast path now keeps each GGUF block intact, eight rows to a tile, and uses TensorFold's own SDOT block arithmetic; when reading a prompt it unpacks each block once for four prompt rows. The cache format moved to .tf_repack8.v5. Cost on the board: prompt reading 50.5 to 38.4 tokens a second, generation 22.7 to 21.2 (512/128 shape, three runs each).
  2. Cache payload checked on a hit. On every hit --fast now checks the SHA-256 of the whole GGUF against the one stored at build time, the bounds of every table entry, and the SHA-256 of the cache table and payload, and rebuilds on any mismatch. New tests cover a matching hit, a changed source with the same stat identity, a malformed table and a corrupted payload byte.
  3. Direct refusal tests for GGUF metadata: wrong architecture and geometry, missing and extra tensors, malformed KV-head metadata, the forbidden clamp keys, and a missing or invalid EOS.
  4. Recipe updated: no placeholder text, current affinity and cache wording, and exact and fast figures in separate places.
  5. Generated IDs, measured on the code of ab0e78818 (only the recipe changed after the run), on the 37-token prompt, 32 tokens, four fast cores, idle board:
Mode Build Threads Cache Generated IDs
exact baseline and +dotprod 1, 4, 8 none all six runs identical, below
--fast +dotprod 4 cold (built on this run) identical to the warm run, below
--fast +dotprod 4 warm (cache hit) identical to the cold run, below

Exact: 56890 7912 1352 259 6559 23323 1829 14547 268 2599 10448 11 14348 1352 81910 301 48216 13 5177 698 259 3212 15503 23323 16219 25 198 198 16 13 5321 25791

Fast: 56890 7912 1352 259 6559 23323 1829 14547 268 2599 10448 11 14348 1352 81910 301 48216 13 5177 341 259 10313 16219 300 1099 11028 7912 25 198 198 16 13

Fast mode first differs from exact mode at the 20th generated token (698 against 341), as before this round. Both match our earlier receipts from this round. Decode speeds for each run are in the recipe.

One question this raised. The full check in point 2 reads the GGUF and the cache in full, about 9 GB, so a warm --fast start now spends about 50 s opening the model on the board (built with +sha2; about 60 s without), against 0.08 s before. We left it in as you asked. If you would rather have the full check run once after a build, with later starts trusting the cache while the GGUF's stat identity is unchanged and a flag that forces the full check, tell us and we will change it.

Checks at ab0e78818: zig fmt --check build.zig zig clean; lean checker 168 problems as on the base, 0 new; test-ling-cpu-host 74 passed, test-ling-host 64 passed and 2 skipped, test-gguf-host 50 passed; cross builds for aarch64-linux-gnu (baseline, +dotprod, +dotprod+sha2), aarch64-linux-musl and x86_64-windows-gnu. The PR description is updated to match.

Cheers

@AIFlowML

Copy link
Copy Markdown
Contributor

I verified revised head ab0e78818 in a clean trial merge into main 2ce240cf0 on M5 Max with Zig 0.17.0. git diff --cached --check passed, and zig build test-ling-cpu-host test-ling-host test-gguf-host -Dcpu=apple_m1 -j4 --summary all completed 13/13 steps, with 188 tests passed, two skipped and no failures.

This includes the revised host coverage for matching cache hits, changed source with unchanged stat identity, malformed tables, corrupted payload recovery, and GGUF metadata refusals. I did not rerun the author’s board token measurements or the reported warm-start cost.

Igor Lessio - AIFLOW LABS

@AIFlowML

Copy link
Copy Markdown
Contributor

I verified 425a44a01270de59d112d566c50662902482869c in a clean trial merge into main 7d2432514a. The staged diff passed git diff --cached --check. Main advanced to c35d9f73c with a scheduler test and changelog update, and git merge-tree --write-tree c35d9f73c 425a44a01 is also clean.

On M5 Max with Zig 0.17.0:

  • zig build test-ling-cpu-host test-ling-host test-gguf-host -Dcpu=apple_m1 -Doptimize=ReleaseSafe -j2 --summary all passed 188 tests with two skips. All 74 Ling CPU tests passed.
  • zig build tf-ling-cpu-decode -Dtarget=x86_64-windows-gnu -Dfixed-cpu -Doptimize=ReleaseSafe -j2 --summary all cross-built the x86-64 Windows executable. The native -Dcpu=baseline+dotprod decoder build also succeeded.

These runs cover the checked-in host fixtures and compilation. The Qualcomm checkpoint token IDs, cold/warm cache timing, and throughput are from the author's board run. I did not run the Windows executable.

Igor Lessio - AIFLOW LABS

@ThinkOffApp

Copy link
Copy Markdown
Contributor Author

Thank you @ashhart for the answer on #1247 and for correcting the rule so quickly! Round 3 takes --fast out of this PR completely. The new head is d9660886b.

  • Removed: the --fast flag, the SDOT kernels and activation quantizer, the eight row repack and its cache file with the SHA-256 check, the fast KDA and MLA paths, and their tests: 12 files, about 3,400 lines. The exact path is unchanged apart from undoing the hooks the fast path had added. The PR is now 26 files, +3354 -6.
  • Kept: the worker pool's thread pinning, which applies in exact mode too and does not change output. The recipe now describes it; before, it wrongly said the engine does not pin threads.
  • Recipe and description: exact figures only. The repack-cache, +sha2 and fast-mode text is gone, and so are the questions about them.
  • Board receipt at this head: exact mode, baseline and +dotprod builds at 1, 4 and 8 threads on four fast cores, the 37-token prompt and 32 tokens: all six runs generated the same 32 ids as round 2's exact runs. Decode at 4 threads is 5.01 (baseline) and 5.08 (+dotprod) tokens a second; the full table is in the recipe and the description.

Checks at d9660886b: zig fmt --check build.zig zig clean; lean checker 165 problems on the base and on this head, the same list, 0 new; test-ling-cpu-host 50 passed, test-ling-host 64 passed and 2 skipped, test-gguf-host 50 passed; cross builds for aarch64-linux-gnu (baseline and +dotprod), aarch64-linux-musl and x86_64-windows-gnu.

Cheers

@ThinkOffApp ThinkOffApp changed the title Ling-3.0-tiny on ARM CPUs: GGUF decoder with an exact default and an opt-in fast mode (#1247) Ling-3.0-tiny on ARM CPUs: exact GGUF decoder (#1247) Nov 22, 2026
The affine reader's block validator becomes public so a family can check a
tensor's scales once before projecting it. cpu_weights.zig exposes only the
mapped formats and the FP64 GGUF source reference, without an engine or a
graphics backend, for host-only CPU modules and their tests.
gguf_names.zig lists the 526 source tensors of the tiny bailingmoe3 GGUF with
their native names, shapes and roles; transformed roles (split MLA key and
value, stacked experts, exp-decay rates) are explicit rather than passed off
as native tensors. cpu_gguf_views.zig resolves a native name to a borrowed
source view and slices one expert or one row without copying quantized bytes.
The new test-ling-cpu-host step runs these under zig build test.
cpu_gguf_rows.zig decodes a block through the shared affine reader and
projects rows with one eight-lane FMA order per dot. The Q4_K kernel and the
Q6_K and Q5_0 kernels vectorise the same order, so each output equals the
scalar block-decode reference bit for bit; threads split output rows only, so
1 to 8 threads give the same bytes. F32 tensors go through the dense rows in
cpu_rows.zig. Bad scales are refused before any output is written.

The tests check decoded values against the independent FP64 source
definition (signed zeros included), projections against expanded weights
with and without BF16 rounding, a selected embedding row, and 1 to 8 thread
results against one thread.
cpu_gguf.File opens a bailingmoe3 GGUF only when its metadata matches the
tiny geometry exactly (24 blocks, 128 experts, MLA every fourth layer, no
SwiGLU clamps) and every one of the 526 tensors has the planned shape and an
admitted kind: F32 for vectors and convolutions, F32, Q4_K, Q6_K or Q5_0 for
matrices. It returns small tensors as F32, rebuilds each MLA layer's joined
key-value matrix from the split source once, embeds selected rows, and
projects a named matrix through the exact kernels after validating its
scales once. cpu_gguf_config.zig derives the family Config from the same
metadata.
…red decay rates

mixState carries the three preceding projections beside the recurrent
matrix, so a prompt fed in chunks or one row at a time convolves exactly as
it would whole. Shape.stored_rate takes the per-head rate as stored, the
positive exp(A_log) the published GGUF holds, instead of recomputing exp;
the oracle test checks that both give the same bytes.
cpu_forward.Forward(Source) runs Ling-3.0-tiny's 24 layers on the CPU:
RMS norms, KDA with its recurrent matrix and convolution history, MLA with
a per-stream latent cache and RoPE, the dense SwiGLU layer, grouped sigmoid
routing to 8 of 128 experts plus the shared expert, and the output head.
Every projection goes through the source's exact kernels; activations round
to BF16 where the reference model does. A Session holds one stream's state,
refuses tokens past the vocabulary or its capacity before touching it, and
poisons the state if a layer fails. cpu.Gguf is the forward over the GGUF
reader, whose small tensors are now decoded once and kept.

The tests run the forward on a small synthetic F32 model: a whole prompt,
the same prompt in chunks of 1, 2 and 4 rows, at 1 and 3 threads, after a
reset, and without a state give the same logits bit for bit.
zig build tf-ling-cpu-decode builds ling-cpu-decode (ReleaseSafe) for the
selected target, natively or cross-built for aarch64 Linux. It opens the
published Q4_K_M GGUF, prefills the prompt token IDs 8 rows a call, decodes
greedily, and prints the generated IDs with prefill and decode tokens a
second. The recipe gives the weights, the build and run commands and the
output shape.
With more than one thread, each projection used to spawn and join its
threads, so threads were slower than one. cpu_gguf_pool.Pool keeps
threads - 1 workers for the life of the GGUF file. A projection's output
rows are claimed in chunks from a shared counter, and a token's routed
experts run one per claim, each a whole gate, up, SwiGLU and down for its
row. Workers spin between jobs and park on a condition when idle. Matrices
under 65,536 weights stay on the calling thread.

Each output row is still one dot in the single-thread order, and the
forward mixes expert results in pick order as before, so the bytes do not
change. The new tests compare the workers' Q4_K, Q6_K, Q5_0 and F32 rows
and whole experts with the one-thread results at 1, 2, 3, 4 and 8
threads, and the synthetic forward now runs through the workers at the
same thread counts. The recipe gives thread advice.
ThinkOffApp and others added 22 commits November 24, 2026 02:16
TensorFold takes no mode that changes output tokens, even opt-in, so the
SDOT kernels, eight-row repack and its cache, the fast KDA/MLA attention and
the --fast flag go. The exact path is unchanged: the pool, the GGUF reader,
the forward and the decode tool return to their exact-only forms, and the
affinity-mask test moves to the exact pool tests.
Drop the fast-mode run, its repack cache, the +sha2 note, the fast accuracy
bullet and the fast measured figures. The dot-product build stays as an
optional exact build, since the measured table has both.
@ashhart
ashhart merged commit 802053d into ashhart:main Nov 24, 2026
11 of 12 checks passed
@ashhart

ashhart commented Nov 24, 2026

Copy link
Copy Markdown
Owner

Thank you @ThinkOffApp. 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.

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.

3 participants