Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
9f0d4c7
XPU backend: Qwen3.8-27B on Intel Arc Pro B70 (Xe2) over Level Zero
Mirrdhyn Oct 14, 2026
25ef56c
XPU: drop the Triton SPIR-V; streams sized from the device's free mem…
Mirrdhyn Oct 14, 2026
ba9193f
Add the Intel GPU (Level Zero) runtime and Nemotron 3.5 Lightning on …
BHCC2025 Oct 8, 2026
f8da783
Arc recipe: decode figures of the earlier decode attention measured o…
BHCC2025 Oct 9, 2026
0fdb2be
Arc recipe: prompt attention before and after measured with the CLI o…
BHCC2025 Oct 9, 2026
eb9a960
Arc: remove the debug and path switches, one admission rule, one-line…
BHCC2025 Oct 10, 2026
c936acf
xpu: Nemotron on the XPU backend's Level Zero table (ze.zig), not a s…
BHCC2025 Oct 15, 2026
5dc655b
xpu nemotron: windows take the decode step's arithmetic, a one-row wi…
BHCC2025 Oct 15, 2026
cd41379
xpu nemotron: prompt rows take the decode step's arithmetic, so a pro…
BHCC2025 Oct 15, 2026
ebb49f1
Nemotron on Intel Arc in the XPU server, beside Qwen3.8-27B
BHCC2025 Oct 15, 2026
d8b996c
xpu server: Nemotron's MTP head and copy drafts through the lane core…
BHCC2025 Oct 14, 2026
8aa5fc3
xpu server: a one-row round is the decode step itself
BHCC2025 Oct 15, 2026
9ec1385
xpu server: concurrent requests share rounds, each stream on its own …
BHCC2025 Oct 14, 2026
5375580
xpu server: the prompt cache, a resumed turn equal to a fresh prompt
BHCC2025 Oct 15, 2026
9674eb6
Arc recipe: drafts, concurrent streams and the prompt cache through t…
BHCC2025 Oct 15, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions THIRD_PARTY_NOTICES.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,11 @@ The multimodal rotary and image-feature integration is adapted from MiaAI-Lab's
[Flash Next vision patch 0008](https://github.com/MiaAI-Lab/Qwen3.8-Flash-Next-Single-DGX-Spark-TensorFold/blob/a3aa89835022c55ca8e55008c37785954834e04f/patches/0008-flash-next-vision.patch),
MIT License, Copyright (c) 2026 MiaAI-Lab. The license is included in `LICENSES/MiaAI-Lab-MIT.txt`.
The port preserves the v0.5 CUDA execution APIs and adds an offline EXL3 vision adapter.

## Intel GPU engine (XPU)

The Intel GPU kernels and host code (`zig/kernels/xpu/`, `zig/src/xpu/`, the `xpu_*` files of the families) are written for
TensorFold. What they follow or copy:

- The Level Zero loader and the Intel compute runtime are loaded at run time and are not bundled; `ocloc` compiles the kernels
at build time and is not distributed.
3 changes: 3 additions & 0 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const cuda_build = @import("zig/build/cuda.zig");
const dist_build = @import("zig/build/dist.zig");
const qwen27_build = @import("zig/build/qwen27.zig");
const hip_build = @import("zig/build/hip.zig");
const xpu_build = @import("zig/build/xpu.zig");
const grammar_build = @import("zig/build/grammar.zig");
const counters_build = @import("zig/build/metal_counters.zig");
const exl3_build = @import("zig/build/exl3.zig");
Expand Down Expand Up @@ -155,6 +156,7 @@ pub fn build(b: *std.Build) void {
const xgrammar = grammar_build.option(b);
dist_build.targets(b, draft_ids, build_options, release_version, xgrammar);
cuda_build.hostTests(b, draft_ids, build_options, test_step);
xpu_build.hostTests(b, test_step);
if (target.result.os.tag == .linux) {
// the engine seam's tests (lane_host.zig and its siblings), which the macOS branch runs from its own api module
const lanes_mod = b.createModule(.{ .root_source_file = b.path("zig/src/core/lanes/lanes.zig"), .target = target, .optimize = optimize, .link_libc = true });
Expand All @@ -163,6 +165,7 @@ pub fn build(b: *std.Build) void {
}
hip_build.steps(b, target, test_step);
exlr8_build.hostTests(b, target, test_step);
xpu_build.steps(b, target, optimize, build_options);
const lanes_cpu = b.createModule(.{ .root_source_file = b.path("zig/src/core/lanes/lanes.zig"), .target = b.graph.host, .optimize = .debug, .link_libc = true });
const disk_cpu = b.addTest(.{ .root_module = b.createModule(.{
.root_source_file = b.path("zig/src/glm_disk_tests.zig"),
Expand Down
3 changes: 2 additions & 1 deletion docs/recipes/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Each family page describes its supported checkpoint, kernels and operating limit

| Family | Recipe |
| --- | --- |
| Nemotron 3.5 Lightning | [MLX](nemotron-3.5.md) |
| Nemotron 3.5 Lightning | [MLX](nemotron-3.5.md), [Intel Arc, experimental](xpu-arc.md) |
| Qwen3.8-27B | [MLX, quantization and CUDA](qwen3.8-27b.md) |
| Qwen3.5-2B | [Native Metal preview, tied affine 4-bit](qwen3.5-2b-native.md) |
| Qwen3.8 Flash Next | [MLX prefill and CUDA](qwen3.8-flash-next.md), [CUDA images](flash-next-vision.md) |
Expand All @@ -25,6 +25,7 @@ from its family pages, not measured per card.
| --- | --- |
| CUDA, other families | Compute capability 8.9 or newer: Ada RTX 40, Hopper, and Blackwell cards (RTX 50, RTX PRO 6000, DGX Spark GB10). RTX 30 (8.6) is not supported yet. |
| Flash Next, CUDA | sm_120 and sm_121 only: DGX Spark GB10, RTX 50, RTX PRO 6000. A card below sm_120 refuses Flash Next at startup. |
| Intel GPU (XPU), Nemotron | Experimental. Weights need 17.3 GiB of device memory (18 to 19.4 GB in use from 4k to 128k context); measured on an Arc Pro B70 (32 GB). Cards below 24 GB are untested and a 16 GB card cannot hold the weights. |
| MLX, Apple Silicon | GLM-5.3-Flash is written for a 256 GB Mac, about 151 GiB resident. DeepSeek-V4-Flash is the same, about 151 GiB resident. Flash Next's default command sizes to a 128 GiB M4 Max. Qwen3.8-27B on a 32 GB Mac needs more than the default 22.4 GiB. Machine classes, not measured minimums. |

New families go on the Zig engine: [adding a Zig family](adding-a-zig-family.md) has the steps in order, what each
Expand Down
171 changes: 171 additions & 0 deletions docs/recipes/xpu-arc.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
# Intel Arc (XPU): Nemotron 3.5 Lightning

Status: experimental. One checkpoint, one card class, one request at a time. Everything below was measured on the setup in
[Tested setup](#tested-setup); nothing is claimed for other cards.

The Intel GPU backend runs OpenCL C kernels compiled to SPIR-V (`ocloc`) through Level Zero, on the same loader table as the
Qwen3.8-27B XPU runner (`zig/src/xpu/ze.zig`), and has no CUDA or Metal code in its path. The token-id CLI is a separate
Linux build (`-Dxpu`); the XPU server (`zig build xpu-native`, `--backend auto`) serves Nemotron beside Qwen3.8-27B.

## Supported

| Item | State |
| --- | --- |
| Model | Nemotron 3.5 Lightning 30B-A3B, MLX affine 4-bit, group 64 (the checkpoint named in [nemotron-3.5.md](nemotron-3.5.md)) |
| Card | Intel Arc Pro B70 (Battlemage, 32 GB) |
| Not supported | NVFP4 and FP8-Mamba checkpoints: refused at load with `NVFP4 checkpoints are not supported on the XPU backend yet; use the MLX 4-bit checkpoint`. They need FP4/FP8 dequantisation kernels that do not exist yet. |
| Drafts | The checkpoint's MTP head (`mtp-4bit.safetensors`) and copy drafts through the lane core, windows of up to 16 rows; `--no-drafts` or `TENSORFOLD_XPU_MTP=0` leaves the head unloaded |
| Streams | Up to `--parallel` streams a round, each on its own sequence (Mamba states, KV, head) |
| Prompt cache | Kept at 512-row prompt chunk ends and restored by device copy; `--prompt-cache-gib`, else 4 GiB within the free device memory less 2 GiB |
| Not supported | Tool-call gates, grammars and structured output |
| Sampling | Greedy uses the device argmax. A request with temperature above 0 is drawn on the host from the step's logits with the lane core's keyed sampler: temperature, `top_k`, `top_p`, `min_p` and `seed`; the draw is keyed by seed, absolute token position and candidate id. Presence and repetition penalties are not implemented. |
| Qwen3.8 | Not covered by this page. |

## Build and run

Needs Zig 0.17.0, `ocloc` and the Level Zero loader with the Intel compute runtime, on Linux with the `xe` driver.
`zig build` defaults to `-Doptimize=fast` (ReleaseFast); every number on this page was measured with it, and a debug build is much slower.

```bash
zig build -Dxpu # tensorfold-xpu (token-id CLI) and tf-xpu-test
zig build -Dxpu xpu-tests # tf-xpu-<name>-test programs (checks and benchmarks)
zig build xpu-native -Dngen-include=NGEN # zig-out/xpu/bin/tensorfold-native, the XPU server (docs/xpu/README.md)

tensorfold-xpu devices
tensorfold-xpu run MODEL --tokens-file ids.txt --max-tokens 128 --prefill 512
zig-out/xpu/bin/tensorfold-native serve MODEL --context 32768
```

`--prefill ROWS` prefills the prompt in windows of ROWS tokens (16 and up); the plain loop then decodes. `--context` defaults
to the model window capped at 32768 in `serve`; the server lists the family `nemotron_h` with format `mlx-q4g64`.

Run model programs one at a time: `tools/zig/xpu_guard.sh SECONDS COMMAND...` takes the GPU lock, refuses to start while other
processes hold more than 8 GB of device memory and stops the command only with SIGINT.
Every program applies the same rule at start: it refuses to run while other processes hold more than 8 GB of device
memory, and there is no override.

## Tested setup

Intel Arc Pro B70 Graphics (32.5 GB maximum single allocation), Linux 7.0.0 (`xe` driver), Zig 0.17.0, a local copy of the
Nemotron MLX 4-bit checkpoint (`TensorFold/NVIDIA-Nemotron-3.5-Lightning-30B-A3B-MLX-4bit`, revision `d9d758fb83953437f7263256b0d96157e2a348b8`,
729 tensors, 17.27 GiB of weights). The commit under test is the head of the pull request.

## Measurements

Decode, 128 greedy tokens from a 5-token prompt: 5.72 ms/token, 175 tok/s (`tensorfold-xpu run`, five runs on different
commits: 173.6 to 174.8).
Prompts take the decode step's arithmetic at every chunk size (`nem_prow.cl`: each output sums in the decode's lane order;
a sub-group holds 8 rows and 8 outputs, so a weight word is unpacked once for 8 rows and an input word once for 8 outputs;
routed experts take their rows in the same order; attention runs the decode kernel per row, 16 rows a launch). Cold prompts,
`tensorfold-xpu run --prefill 512`, random token ids, 16 tokens generated:

| Prompt tokens | Prefill s | Prefill tok/s | Earlier prefill GEMMs, tok/s | Decode tok/s after it |
| ---: | ---: | ---: | ---: | ---: |
| 2,048 | 2.41 | 851 | 3,108 | 176.0 |
| 8,192 | 9.85 | 831 | 3,086 | 172.0 |
| 32,768 | 43.4 | 755 | 2,888 (11.1 s) | 164.4 |
| 131,072 | 256.5 | 511 | 2,281 | 138.8 |

The same prompts one token at a time run at about 167 tok/s, and in 16-row windows at about 215 tok/s. The 32,768-token
prompt gives the same tokens afterwards as with the earlier prefill GEMMs (digest `64a5a3409b0b`).

Through the XPU server with the MTP head loaded (`tools/zig/xpu_draft_receipt.py`, 256 tokens, two runs each): `"draft": false`
replies 173.0 to 174.3 tok/s greedy (a one-row round is the decode step); drafted greedy 157.3 (code), 167.4 (prose), 169.0
(list) and 171.8 (copy) tok/s, 0.90 to 0.99 of plain: each extra window row costs about 3.7 ms, mostly the routed experts.
Seeded (T 0.8, seed 7) replies run at about 60 tok/s, the host drawing from 131,072 logits a row.

Concurrency (`tools/zig/xpu_concurrent_receipt.py`, `--parallel 4`, 4 streams of 192 tokens, greedy and seeded): 72.0 and 73.6
tok/s aggregate against 83.8 for the same requests one after another; each stream's window is its own forward pass.

Prompt cache (`tools/zig/xpu_resume_receipt.py`, a two-turn chat over a 6,685-token document): turn 1 prefills in 8.52 s, turn 2
resumes 6,656 tokens and prefills in 0.21 s instead of 8.67 s.

Memory, measured on the 32 GB card in a host with 62.2 GiB of RAM and 24 threads (kernel 7.0.0, `tensorfold-native serve`,
KV caches and scratch allocated at load for the chosen context): device allocation counter 17.85 GB for the weights, 18.40 GB
at `--context 4096`, 19.24 GB at 131,200 and 20.11 GB at 262,144 (the native window) without the head, 19.36 GB at 32,768
with it, unchanged by the prompts that follow.
Host process: 339 MB resident after load, 355 MB at its peak after a 262,080-token prompt (VmHWM); the engine pins only its
staging buffers. Load is ready in 7 s with the checkpoint in the page cache. After SIGINT the server exits in 0.2 s with status
0 and no device memory left. The weights alone are 17.3 GiB, so this model does not fit a 16 GB card; no 16 GB claim is made.

## Accuracy

Against the CUDA reference of the same checkpoint (teacher-forced, the reference's own tokens fed):

| Check | Result |
| --- | --- |
| 4 prompts x 48 steps, top-1 | 185 of 192 (188 with the earlier per-row pair; every miss is a near tie, see below) |
| 15 prompts, 1,440 positions, plain decode | top-1 1,421 (98.7%), mean abs log-probability difference 0.0233, 95th percentile 0.109, max 0.514 (1,422, 0.0232, 0.105, 0.750 with the earlier per-row pair) |
| same, prompt in 512-row or 128-row windows | identical to plain decode: 1,421, 0.0233, 0.1085, 0.514 |

The 19 top-1 misses of the plain run are listed with both margins by `WIDE_LIST=file tools/zig/xpu_compare_wide.py plain`:
all have a reference margin (top-1 over top-2) of at most 0.375 and a gap on our side of at most 0.625 (log-probability steps at this
precision are 0.125), and 13 of the 19 are within 0.25 on both sides. The earlier outlier of the run with the per-row decode
attention (prompt 12, step 53: reference margin 1.375, ours 0.125 the other way) is gone; with the earlier per-row pair it is still there. It came from the state the single-row
kernels built over the 1,000-token prompt, not from the step's own kernels, and the model is sensitive to the order of accumulation
between its decode and prefill paths at such positions; the reference engine shows the same sensitivity (see the parity results below).
On the 4-prompt gate the new decode attention has 7 misses against 4 with the earlier per-row pair; each of the 7 has a reference margin of
at most 0.25 and our gap at most 0.375, two of them exact ties on our side (a tie goes to the lower token id, the reference's
choice differs). The greedy reply to a test prompt differs by one word between the two kernels (the digests differ); each kernel is
deterministic.

KL divergence against BF16 (llama.cpp scoring, same public corpus, 16-row windows): the full 360-chunk run (91,800 scored tokens) gave
mean KLD 0.0775, same top-1 89.84%, perplexity ratio 1.0591, measured with an earlier build. On this branch, where windows take the
decode's arithmetic, the first 64 chunks give mean KLD 0.0866 +- 0.0011 and same top-1 87.41 +- 0.26% (0.0864 and 87.24% with the
earlier matrix-engine windows).

Generation parity against TensorFold 0.6.4 on CUDA (41 prompts, 200 greedy tokens each, prompt in 512-row windows): 14 of 41
replies are identical token for token; 25 of the 27 first divergences are exact bf16 ties or within 0.25 logit, the other two
are 0.5 and 1.125. Teacher-forced on the reference's tokens, 5,098 of 5,159 positions agree (98.8%); 49 of the 61 mismatches
are ties or within 0.25. The other 12 (the same kind as wide prompt 12 step 53 above) have a reference margin of 0.375 to 2.5
where our gap is 0.125; feeding the same prompt and the reference's tokens to the CUDA engine as one prefill, its prefill path
sided with our order or tied at three of them (short3@54, qa2@34, qa4@87) and with its own decode order at four (short3@96,
short3@155, summ1@64, code3@142), which means the reference's own decode and prefill paths differ from each other by up to
about 1.5 logit at such positions. Our engine is deterministic (41 of 41 reruns identical).

Projection classes against an independent FP64 reference (`tf-xpu-nem_fp64-test`: real 4-bit codes dequantised exactly in
f64, random bf16 rows shaped like each layer's input, first and last layer of each kind, decode, 8-row window and 8- and
64-row prompt kernels separately; every class is within the test's bounds, and the window and prompt kernels give the decode
kernels' figures):

| Output | Classes | Aggregate error | Worst column (relative to that column's max) | Notes |
| --- | --- | ---: | ---: | --- |
| bf16 | Mamba in/out, attention q/k/v/o, router gate, embed | 1.6e-3 | 3.9e-3 | at most 0.54 ulp; 99.8-100% of outputs equal the correctly rounded f64 value |
| bf16, relu2 | shared up, routed fc1 | 3.5-3.8e-3 | 1.1e-2 | 74-77% exact: the pre-activation is rounded to bf16 before squaring |
| fp32 | shared down, routed fc2, lm_head | 5e-8 to 1e-7 | 6e-7 | |

## Exactness

- Window rows: a window of up to 16 rows takes the decode step's arithmetic (the row kernels of `nem_rows.cl`: each output's lane
sums in the decode's order, each weight read once for all the rows), so a one-row window equals the decode step bit for bit, and
windows of 2, 3, 4, 8 and 16 rows give the same logits over 40 tokens, including a pending-then-commit case
(`tf-xpu-nem_rows-test`).
- Prompt chunk size: a prompt cut anywhere gives the bits of the same prompt decoded one token at a time. After 640 tokens in
chunks of 16, 17, 100, 128 and 512 rows, the Mamba conv state, SSM state and KV bytes are byte-identical to the row-by-row
state and the next 8 decode logits are identical (`tf-xpu-nem_seam-test`); prefill in chunks of 32, 100 and 512 rows gives
the row-by-row last-token logits (`tf-xpu-nem_prefill-test`).
- Drafts: every drafted reply's `token_sha` equals its `"draft": false` run, 8 of 8 (greedy and seeded; code, prose, list,
copy); a server without the head gives the same seeded `token_sha`s, 4 of 4.
- Streams: 4 concurrent replies equal their solo runs, 4 of 4 in each of two rounds (greedy and seeded).
- Prompt cache: a resumed turn's `token_sha` equals the same turn with `--prompt-cache-gib 0`, greedy and seeded.
- Requests: A client that disconnects mid-stream cancels its request
and the next request gives the same tokens as before; seeded sampling repeats exactly; SIGINT or SIGTERM during a generation ends
the running stream after its current token with an error event (`ShuttingDown`) and `[DONE]`, answers a queued
request with the error `the server is shutting down`, exits with status 0 within 0.2 s and leaves no device memory held.
- Host-side slot ring: forwards queued without a sync equal synced forwards (`tf-xpu-nem_slot-test`).

## Tests

`tools/zig/xpu_compare_ref.py` (forced tokens against the reference), `xpu_compare_wide.py`, `xpu_nem_needle.py`,
and the `tf-xpu-*-test` programs (`runner-all`, `nem_rows`, `nem_prefill`, `nem_slot`, `nem_seam`,
`nem_fp64`, `nem_format`, `panic`). `zig build test` passes except `direct_io: direct reads return the same bytes as buffered
reads`, which fails the same way on an unmodified upstream 1.0.2 checkout on this machine.

## Known limits

- Decode about 172 tok/s at short context, 137 tok/s at 128k and 113 tok/s at 256k; drafts do not speed it up on this MoE yet,
and concurrent streams do not share weight reads.
- Prompts run at about 850 tok/s at 2k tokens and 511 tok/s at 128k: the decode's fp32 arithmetic on the vector units, about a
quarter of the earlier prefill GEMMs' speed, for prompts whose bits do not depend on how they are cut.
- A shutdown during a generation ends the stream with an error event rather than a finish reason.
- Single card, single process: the 17.3 GiB of weights leave about 14 GB on a 32 GB card.
Loading