Skip to content

Commit a586f3d

Browse files
nood-co1ashhart
authored andcommitted
fix: kill -USR1 prints the stacks of a CUDA server too
cmd_serve returned into _serve_cuda before it registered the SIGUSR1 stack dump, so the dump its comment calls "the way to see where a silent server waits" existed only on the Mac. The registration now comes before the backend branch; nothing else moves. Test: the handler is registered before _serve_cuda runs (host; fails before the change). The CUDA dispatch test stubs the registration, as the MLX tests do. (cherry picked from commit 10e0cc7)
1 parent 71377a5 commit a586f3d

18 files changed

Lines changed: 479 additions & 28 deletions

‎README.md‎

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# TensorFold
22

3-
TensorFold serves text models on Apple Silicon and NVIDIA GPUs through an OpenAI-compatible API.
3+
TensorFold serves language models on Apple Silicon and NVIDIA GPUs through an OpenAI-compatible API.
44
Each model family supplies its own kernels and draft verification.
55

66
```bash
@@ -12,6 +12,11 @@ Use `http://127.0.0.1:8080/v1` as the client base URL and the model ID from `/v1
1212
Python 3.11 or newer is required, and MLX 0.32.2 or newer on a Mac (pip installs it). See the [runbook](RUNBOOK.md)
1313
for installation and a first request.
1414

15+
## Image input
16+
17+
Install `pip install '.[vision]'` from this branch and start a compatible Qwen3.5/3.8 dense checkpoint with `--vision` to accept image and text content parts through the same lane engine.
18+
See [image input](docs/vision.md) for the API, checkpoint requirements, cache behavior and qualification status.
19+
1520
## Models
1621

1722
| Model | Checkpoint | Backend | Drafting |
@@ -21,8 +26,12 @@ for installation and a first request.
2126
| Qwen3.8 Flash Next | `Vontra/Qwen3.8-Flash-Next-MLX-4bit-MTP` | MLX, CUDA | Included MTP head and context copies |
2227
| GLM-5.3-Flash | `Vontra/GLM-5.3-Flash-MLX-4bit-MTP` | MLX on a 256 GB Mac, CUDA with two ranks | MTP; optional DFlash2 on CUDA |
2328
| Gemma 4 26B-A4B | `mlx-community/gemma-4-26b-a4b-it-4bit` | MLX | Context copies; `z-lab/gemma-4-26B-A4B-it-DFlash` is optional |
29+
| DeepSeek-V4-Flash | `mlx-community/DeepSeek-V4-Flash-4bit` | MLX on a 256 GB Mac | DSpark or MTP, converted from DeepSeek's releases |
30+
| Qwen3.8-27B (NVFP4) | `nvidia/Qwen3.8-27B-NVFP4` (ModelOpt: NVFP4 MLP, FP8 attention) | CUDA, one GPU | `z-lab/Qwen3.8-27B-DFlash2` and context copies |
2431
| Qwen3.8-27B (EXL3, experimental) | `turboderp/Qwen3.8-27B-exl3` (branches `3.00bpw`, `4.00bpw`; any codebook, 1 to 8 bits per weight) | CUDA | `z-lab/Qwen3.8-27B-DFlash2` and context copies |
2532
| Qwen3.8 Flash Next (EXL3, experimental) | `turboderp/Qwen3.8-Flash-Next-exl3` (branch `3.05bpw_h5_ng5`; any codebook, a width per tensor) | CUDA | Included MTP head and context copies |
33+
| Ternary Bonsai 2 27B | `prism-ml/Ternary-Bonsai-2-27B-mlx-2bit` | MLX | `z-lab/Qwen3.8-27B-DFlash2` and context copies |
34+
| Qwen3.8 Flash Next (NVFP4) | `local-inference-lab/Qwen3.8-Flash-Next-NVFP4` (ModelOpt: NVFP4 experts, MXFP8 attention and DeltaNet); `RadixArk/Qwen3.8-Flash-Next-NVFP4` (bf16 besides the experts) | CUDA, one GPU | Included MTP head and context copies |
2635

2736
`tensorfold models` lists families and checkpoints. `tensorfold info MODEL` checks configuration without
2837
fetching weights. `serve` downloads a missing checkpoint; `pull` downloads it ahead of time.
@@ -43,7 +52,9 @@ keep the installed MLX version within the package requirements. The named checkp
4352
`mtp-4bit.safetensors`, which `pull` and `serve` check for.
4453

4554
Flash Next requires 4-bit/group-32 weights. Without an MTP head it can run without MTP drafting on MLX;
46-
on CUDA, explicitly pass `--no-drafts`. Nemotron CUDA requires 4-bit/group-64 weights and an MTP head
55+
on CUDA, explicitly pass `--no-drafts`. On one CUDA GPU it also reads the two NVFP4 exports in the table as they
56+
ship; see [the recipe](docs/recipes/qwen3.8-flash-next.md#nvfp4-checkpoints) for their formats, the checks they
57+
passed and what is not supported. Nemotron CUDA requires 4-bit/group-64 weights and an MTP head
4758
unless `--no-drafts` is set. GLM on MLX reads 4-bit/group-64 weights and mlx-lm's mixed-bit conversions,
4859
whose 5-, 6- and 8-bit tensors take their own row kernels; it needs MLX 0.32.2 or later. GLM CUDA reads
4960
MLX 4-bit/group-64 weights and the experimental `Mia-AiLab/GLM-5.3-Flash-EXL3-TR3-4bpw` conversion. GLM's optional
@@ -55,6 +66,11 @@ Gemma 4 has no draft head. It drafts copies of its context, and chains from z-la
5566
with an 8-bit router, as the mlx-community conversion stores them; `serve` refuses other Gemma 4 layouts
5667
before downloading.
5768

69+
DeepSeek-V4-Flash reads the mlx-community conversion (affine 4-bit/group-64 weights, mxfp4 routed experts) and
70+
needs MLX 0.32.2 or later. Its draft heads come from DeepSeek's MIT-licensed releases, converted once with
71+
`python -m tensorfold.families.deepseek_v4.convert` and passed with `--drafter`; see
72+
[its recipe](docs/recipes/deepseek-v4-flash.md).
73+
5874
See the [recipes](docs/recipes/README.md) for supported formats and backend limits.
5975

6076
## Exact decoding
@@ -80,6 +96,7 @@ between MLX and CUDA, different quantizations, or different tensor-parallel rank
8096
| --- | --- | --- |
8197
| `--host`, `--port` | Listen address, default `127.0.0.1:8080` | Both |
8298
| `--name` | Model ID advertised to clients | Both |
99+
| `--vision` | Opt-in Qwen3.5/3.8 dense image input | Both |
83100
| `--alias` | Additional model IDs | MLX |
84101
| `--context N` | Prompt plus reply capacity | Both |
85102
| `--max-tokens N` | Default reply limit, 4096 | Both |
@@ -95,6 +112,7 @@ between MLX and CUDA, different quantizations, or different tensor-parallel rank
95112
| `--kv-dtype bf16`, `int8`, `int4` | Flash Next: `int8` or `int4` stores keys and values with one fp16 scale per 32 values. Other families and the MLX path refuse it | CUDA |
96113
| `--mtp-confidence P` | Flash Next: stop a draft chain before a later draft under this probability, 0 to 1 (default 0.30) | CUDA |
97114
| `--tp 2 --rank R --master HOST` | Two-rank CUDA execution | CUDA |
115+
| `--decode-share F` | While a prompt prefills, running replies keep moving for this share of each chunk's time and later prompts start later (default 0.25; 0 prefills whole prompts first, as 0.3.6.2) | MLX |
98116
| `--prompt-cache-gib N` | Retained conversation-prefix budget; zero disables retention | MLX |
99117
| `--checkpoint-slots N` | Retained conversation prefixes (default 3 per lane, at least 8); long conversations hit this before the byte budget | MLX |
100118
| `--spill-gib N` | Write evicted conversation prefixes to disk (up to N GiB) and read them back instead of prefilling again; zero disables | MLX |
@@ -111,8 +129,10 @@ length. CUDA does not implement the MLX-only options above. See [API fields](doc
111129

112130
On MLX, omitted `--context` targets the model's metadata window and reduces it to the startup memory
113131
estimate when needed, allowing room to retain a prompt for the next turn. An explicit positive value
114-
that cannot fit one request is refused at startup. `--context 0` removes the metadata cap; finite engine
115-
capacity and memory admission still apply. Use the reported context when configuring client compaction.
132+
that cannot fit one request is refused at startup. A larger explicit window may fit a request without
133+
leaving room to retain its prompt, so a later turn can need a full prefill. `--context 0` removes the
134+
metadata cap; finite engine capacity and memory admission still apply. Use the reported context when
135+
configuring client compaction.
116136

117137
On CUDA, Qwen defaults to the affordable native capacity. GLM targets a dense 2,051-token window,
118138
and Nemotron targets 16,384 tokens; the capacity estimate can lower these defaults. Flash Next's `--kv-dtype int8` or `int4` counts
@@ -153,7 +173,7 @@ prompt fixture, checkpoint revision, runtime, command and measurement output acc
153173
| 32 GB | 22.4 GiB | TBD | TBD | TBD | TBD |
154174
| 36 GB | 25.2 GiB | TBD | TBD | TBD | TBD |
155175
| 48 GB | 33.6 GiB | TBD | TBD | TBD | TBD |
156-
| 64 GB | 44.8 GiB | TBD | TBD | TBD | TBD |
176+
| 64 GB | 44.8 GiB | 140,288 tokens, 43.7 GiB (0.3.5.1) | 152,576 tokens, 39.9 GiB (0.3.5.1) | 262,144 tokens, 42.2 GiB (0.3.5.1) | Not run: 4-bit weights exceed the budget |
157177
| 96 GB | 67.2 GiB | TBD | TBD | TBD | TBD |
158178
| 128 GB | 89.6 GiB | TBD | TBD | TBD | TBD |
159179
| 192 GB | 134.4 GiB | TBD | TBD | TBD | TBD |
@@ -163,6 +183,12 @@ Each model cell needs the fitted context and peak physical process footprint. An
163183
a larger host is not a measurement on hardware with that RAM size. These are qualification slots,
164184
not minimum-memory promises. Weights that exceed the MLX budget are refused before loading.
165185

186+
The 64 GB row comes from @benwilson's run on a 64 GB M5 Pro with TensorFold 0.3.5.1 and MLX 0.31.2 (#70),
187+
not from this release. Each cell is the fitted context at the default budget and the process's lifetime peak
188+
footprint over cold and resumed prompts at that context. The [Qwen3.8-27B recipe](docs/recipes/qwen3.8-27b.md#a-64-gb-m5-pro-on-0351)
189+
lists the checkpoint revisions, fixture, commands and results. On 0.3.5.1 with DFlash2, prompts above about
190+
100,000 tokens were served but not kept for the next turn (#71).
191+
166192
## Prompt caching
167193

168194
On MLX, chunk starts come from the rendered token sequence. Resume points are assistant-message

‎RUNBOOK.md‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ The first start compiles kernels. Container removal discards an unpersisted inst
6666
use a retained container or configure persistent storage when downloads should survive removal.
6767
There is no `tensorfold[cuda]` extra. Qwen3.8-27B, Flash Next and Nemotron have one- and two-rank CUDA
6868
engines; GLM requires two ranks. Nemotron CUDA uses its included MTP head and 4-bit/group-64 weights.
69-
Qwen3.8-27B CUDA requires DFlash2 unless `--no-drafts` selects the serial reference.
69+
Qwen3.8-27B CUDA requires DFlash2 unless `--no-drafts` selects the serial reference. Flash Next also reads
70+
the NVFP4 (ModelOpt FP4) checkpoint `ukisai/Swift-1.5-Qwen3.8-Flash-Next-NVFP4` as it ships — its PLE layer
71+
included, whose table ships as BF16 rows with no per-shard `scales`, a layout the reader takes as it is.
7072

7173
CUDA `--parallel auto` serves one request at a time. To share rounds, set `--parallel N` greater than
7274
one for Qwen3.8-27B on one or two ranks, or Flash Next on one rank. Pass the same N on both Qwen ranks.
@@ -88,8 +90,9 @@ tensorfold serve Vontra/Qwen3.8-27B-MLX-4bit --tp 2 --rank 0 --master 192.0.2.1
8890
```
8991

9092
Replace the documentation address with rank 0's reachable address. Both ranks must agree on context and
91-
drafting settings. The default rendezvous port is 29551. GLM requires two CUDA ranks; Flash Next can use
92-
one or two and needs `--no-drafts` when its checkpoint lacks an MTP head.
93+
drafting settings. The default rendezvous port is 29551. The rendezvous port and the link between the ranks
94+
are not authenticated: keep them on a private link, or firewall the port to the peer. GLM requires two CUDA
95+
ranks; Flash Next can use one or two and needs `--no-drafts` when its checkpoint lacks an MTP head.
9396

9497
## Memory and context
9598

@@ -131,6 +134,7 @@ to refresh installed metadata and dependencies. Update inside the container when
131134
| Rejected checkpoint | Quantization, model family and draft-head requirements |
132135
| Client cannot connect | Server process, `/health`, base URL and model ID |
133136
| Two-rank startup waits | Link reachability, rendezvous port, NCCL devices and matching settings |
137+
| CUDA start stops after `loading …`, GPU idle | `kill -USR1 <pid>` prints every thread's Python stack. A wait in the extension build is a build lock, whose path the start log names: when no other build is running, stop the start, delete the lock and start again |
134138

135139
Unsupported architectures or formats need a family implementation. See [adding a family](docs/recipes/adding-a-family.md)
136140
or [adding a CUDA family](docs/recipes/adding-a-cuda-family.md); forcing an unsupported checkpoint to load

‎THIRD_PARTY_NOTICES.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ Flash Next's optional int8 and int4 KV caches (`families/qwen4_exp/cuda/kvcache.
8181
`src/tensorfold/drafters/vendor/z_lab_dflash/model_mlx.py` is the unmodified `dflash/model_mlx.py` from
8282
[z-lab/dflash](https://github.com/z-lab/dflash), MIT License, Copyright © 2026 Z Lab.
8383

84+
`src/tensorfold/families/deepseek_v4/vendor/encoding_dsv4.py` is the unmodified `encoding/encoding_dsv4.py` of
85+
[deepseek-ai/DeepSeek-V4-Flash](https://huggingface.co/deepseek-ai/DeepSeek-V4-Flash) (revision 60d8d70), and
86+
`tests/fixtures/deepseek_v4/` holds two of its test cases, MIT License, Copyright (c) 2023 DeepSeek.
87+
The MTP layer TensorFold drafts with comes from that checkpoint's last shard (MIT), converted by
88+
`families/deepseek_v4/convert.py`.
89+
8490
TensorFold ships no model weights. The `z-lab/Qwen3.8-27B-DFlash2` model card states Apache-2.0.
8591
The optional `incoai/GLM-5.3-Flash-DFlash2` model card states CC BY-NC-ND 4.0, for non-commercial use
8692
without derivatives. Each checkpoint keeps its own license.

‎docs/api.md‎

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ The base URL is `http://127.0.0.1:8080/v1` with the default server settings.
66
| --- | --- |
77
| `GET /v1/models` | Served model ID; MLX also lists configured aliases |
88
| `GET /health` | Server health and available status information |
9-
| `POST /v1/chat/completions` | Text chat, tools and reasoning; streamed or non-streamed |
9+
| `POST /v1/chat/completions` | Text chat, optional image input, tools and reasoning; streamed or non-streamed |
1010
| `POST /v1/completions` | Raw text without a chat template; MLX also accepts token IDs |
1111

1212
On MLX, a completions body containing a nonempty `messages` list uses chat handling. CUDA completions
1313
require a string `prompt`.
14-
Image, audio and video input or output requests receive HTTP 400.
14+
With `--vision`, supported Qwen3.5/3.8 dense checkpoints accept user `image_url` content parts alongside text.
15+
See [image input](vision.md) for data URLs, public image URLs, limits and cache behavior.
16+
Unsupported image input, audio, video and non-text output requests receive HTTP 400.
1517

1618
## Request fields
1719

@@ -27,20 +29,32 @@ Image, audio and video input or output requests receive HTTP 400.
2729
| `stream` | Server-sent events with final usage | Both |
2830
| `chat_template_kwargs.enable_thinking` | Template thinking toggle | Both |
2931
| `draft` | False selects the serial reference; CUDA rejects it if the engine has no serial switch | Both |
30-
| `ignore_eos` | Disable model end-of-sequence stopping; the reply limit still applies | MLX and GLM CUDA |
31-
| `stop` | Stop at a string or any string in a list; omit the matched text from the response | MLX |
32+
| `ignore_eos` | Disable model end-of-sequence stopping; the reply limit still applies | MLX, and GLM, Qwen3.8-27B and Qwen3.6 CUDA |
33+
| `stop` | Stop at a string or any string in a list; omit the matched text from the response | Both |
3234
| `reasoning_effort` | `none`, `minimal`, `low`, `medium`, `high` or `xhigh` | MLX |
3335
| `thinking_budget` | Token-count limit inside reasoning | MLX |
3436
| `priority` | `background` yields to foreground requests | MLX |
3537

36-
CUDA does not enforce the MLX-only fields above. Its Qwen engines also ignore `ignore_eos`. Unsupported
37-
generation features include multiple choices through `n` and `logprobs`. On MLX, `ignore_eos: true`
38-
keeps user-supplied `stop` strings active, including when a stop string spans streamed chunks.
39-
MLX rejects malformed numeric controls and out-of-vocabulary raw prompt IDs with HTTP 400;
38+
CUDA does not enforce the MLX-only fields above. Its Flash Next and Nemotron engines stop at an end token
39+
whatever `ignore_eos` says. Unsupported generation features include multiple choices through `n` and `logprobs`.
40+
`ignore_eos: true` keeps user-supplied `stop` strings active, including when a stop string spans streamed chunks.
41+
Both backends reject a non-boolean `ignore_eos` or a malformed `stop` with HTTP 400 before a stream opens.
42+
Both backends reject malformed `temperature`, `top_p`, `top_k` and `seed` values with HTTP 400, whether or not
43+
the request samples: booleans, non-finite numbers, non-numeric strings, and non-integral `top_k` or `seed`.
44+
MLX also rejects its other malformed numeric controls and out-of-vocabulary raw prompt IDs.
4045
`top_k` at zero or below disables top-k filtering, and null sampling fields retain server defaults.
4146

47+
On CUDA, stop strings are checked after every token on the generated text, reasoning included, so drafted and
48+
`"draft": false` replies stop at the same token; usage and `token_sha` count the tokens through the one that
49+
completes the match. The GLM engine, and Flash Next and Nemotron on two ranks, decode on after a match to an end
50+
token or the reply limit; the server returns the reply only up to the match. Where a CUDA engine honors
51+
`ignore_eos`, end tokens inside the reply are decoded into its text, as on MLX, and `finish_reason` is `length`
52+
unless a stop string or a tool call ended the reply.
53+
4254
On MLX, `--parallel auto` is the default: requests share rounds within the configured concurrency and memory
43-
budget. Background work waits behind foreground requests. An active background request yields when a
55+
budget. A new prompt prefills one chunk at a time, and running replies take rounds between its chunks for
56+
`--decode-share` of each chunk's time (default 0.25): they keep moving, and queued prompts' first tokens come later.
57+
`--decode-share 0` prefills each prompt whole first, as 0.3.6.2 did. Background work waits behind foreground requests. An active background request yields when a
4458
foreground request needs its lane or memory, then restarts with already-delivered tokens suppressed.
4559
Session-title requests are also treated as background work.
4660

@@ -105,7 +119,11 @@ and fitting guidance before generation. MLX returns HTTP 400 for non-streamed re
105119
`invalid_request_error` event after opening a stream. CUDA returns HTTP 400 before opening a stream.
106120
The 0.3.4.1 MLX server capped that explicit limit to the remaining context.
107121
When the request omits the reply limit, the server still caps its configured default to the remaining context.
108-
MLX also returns generation errors in the stream after streaming starts.
122+
CUDA returns HTTP 400 before generation when the chat template rejects the request or
123+
`chat_template_kwargs` is neither an object nor null. A generation error returns HTTP 500 for a
124+
non-streamed request; after a stream opens, both backends send an error event of type `server_error`,
125+
then `[DONE]`. On two CUDA ranks such an error can leave the ranks out of step, so restart both: the 27B's
126+
`--parallel` decoder refuses every later request until then, and the other two-rank engines don't detect it.
109127
MLX also checks projected memory before prefill. CUDA checks its allocated cache capacity and model window.
110128
A startup capacity estimate is not a measured release capacity.
111129

‎docs/recipes/README.md‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ Each family page describes its supported checkpoint, kernels and operating limit
77
| Nemotron 3.5 Lightning | [MLX](nemotron-3.5.md) |
88
| Qwen3.8-27B | [MLX, quantization and CUDA](qwen3.8-27b.md) |
99
| Qwen3.8 Flash Next | [MLX prefill and CUDA](qwen3.8-flash-next.md) |
10+
| Ternary Bonsai 2 27B | [MLX](ternary-bonsai-2.md) |
1011
| GLM-5.3-Flash | [MLX on a 256 GB Mac, two-rank CUDA](glm-5.3-flash.md) |
1112
| Gemma 4 26B-A4B | [MLX, fused one-row decode](gemma-4.md) |
13+
| DeepSeek-V4-Flash | [MLX on a 256 GB Mac, DSpark and MTP drafts](deepseek-v4-flash.md) |
1214
| Qwen3.6-35B-A3B | [One-GPU CUDA](qwen3.6-moe.md) |
1315

1416
Contributor guides cover [adding an MLX family](adding-a-family.md),
@@ -47,7 +49,8 @@ python3 tools/bench_openai.py http://127.0.0.1:8080 bench \
4749

4850
The client warms each cell, uses seeds 1234 through 1238, and reports the median decode rate after the
4951
first token. Sampled cells use temperature 1, top-k 20 and top-p 0.95. The client sends `ignore_eos: true`;
50-
MLX and GLM CUDA honor it, while the Qwen CUDA engines can stop at EOS before the requested limit.
52+
MLX, GLM CUDA and the Qwen3.8-27B and Qwen3.6 CUDA engines honor it, while the Flash Next and Nemotron CUDA engines can stop
53+
at EOS before the requested limit.
5154
It measures throughput; it does not itself prove token equality. Record checkpoint and tokenizer revisions,
5255
runtime versions, backend, rank count, launch command and output hashes with a result. Compare serial and drafted output separately.
5356

0 commit comments

Comments
 (0)