Repository navigation
CUDA (GB10): Nemotron KV caches in the display carveout, opt-in - #487
Conversation
On a DGX Spark the display controller reserves about 1.75 GiB of system memory that MemAvailable never counts. A DRM dumb buffer on the card is backed by it. carveout.zig creates one (DRM_IOCTL_MODE_CREATE_DUMB, a 4096-wide 32-bpp buffer), maps it (MAP_DUMB, mmap) and registers it with cuMemHostRegister: DEVICEMAP first, then DEVICEMAP | IOMEMORY, which is what GB10's driver takes. A Carveout value owns the fd, the dumb handle, the mapping and the registration, and releases them in reverse order. Spans come from a first-fit list (a bump allocator until something is given back). They are 256-byte aligned and zeroed, and each is returned as a DeviceBuffer marked borrowed, so free leaves the memory and the process's device byte counts alone, and give() hands it back. The structs and ioctl numbers follow drm_mode.h, and the unit tests check them, the span placement, exhaustion and the opt-in parsing on any host. Nothing runs unless a caller opens a carveout.
…memory `tf-cuda-test carveout [MiB] [card]` opens the carveout, writes a 1 MiB pattern from the host and reads it back through a device copy, fills a span with tf_probe_fill and reads the floats through the mapping, then times copies into and out of it and an axpy whose input lives there. It prints SKIP and passes when the card cannot be opened. GB10, 1792 MiB, four runs: registered DEVICEMAP|IOMEMORY, MemAvailable moved 0-32 MiB; copies 57-60 GB/s into and 56-60 out of the carveout against 118-121 device to device; axpy reading its input from the carveout 220-234 GB/s against 247-250 from device memory.
With `tensorfold run --carveout` (or TF_CUDA_CARVEOUT=1; size TF_CUDA_CARVEOUT_MIB, default 1792; card TF_DRM_CARD) each sequence's K and V planes go into the display carveout while it holds both: the engine's own sequence first, then the lane sequences newSeq makes. The Mamba state, conv state, window io and scratch stay in device memory. The recurrent state is read and written whole every round, so it is left out on purpose. A sequence gives its planes back when it is freed. Placement only: no kernel or sum changes, so the output does not change. The seven cache and state planes are no longer one device range. Snapshot and restore copy them plane by plane into a packed buffer, reset fills each plane, and the segments' state digest hashes the planes (it skips the alignment gaps the old range covered). The CLI prints the room: the device's free memory plus what is left in the carveout. Off by default. It needs nvidia_drm modeset=1, the card's device node and no display in use. Without the card, the CLI names the step that failed and stops. The idea of keeping caches in GB10's display memory comes from coolbho3k's DeepSeek-v4.1-Flash-2x-DGX-Spark. This is an independent implementation from the DRM UAPI and the CUDA driver API.
… measured The device test now skips before touching the card when the GPU is not integrated, so a desktop card is never asked for a 1.75 GiB dumb buffer. The recipe gives copies at about half speed and kernel reads at about 90%, and says the output should not change rather than that it was checked.
tensorfold-native now serves Nemotron on CUDA, so the family opens the display carveout when TF_CUDA_CARVEOUT=1 is set (TF_CUDA_CARVEOUT_MIB and TF_DRM_CARD as for `tensorfold run`) and hands it to the engine: the own sequence's and each admitted stream's KV planes go there while it holds both. The family owns it and closes it after the engine and lane sequences are gone. A card that cannot be opened stops the load with the failing step logged. Stream admission still counts each stream's full bytes, so the carveout only leaves more memory free than admission assumes. Off by default.
Kernel mode setting, no display or desktop session on the card, access to the card node (video group or a container device), and the check that confirms it. The skip note now names the discrete-GPU case too.
|
Cold-prompt receipt inputs:
|
|
Partial receipt from a second DGX Spark (GB10, kernel 6.17.0-1029-nvidia, driver 580.173.02,
|
…dev hold part of it
|
Thanks for the receipt. The off path matching The on path stopping is by design. The carveout is all or nothing: it takes the full ~1.75 GiB of the display reservation or refuses the load, and never runs with a partial carveout or quietly without it. On a box booted into If you have time to cover the on path:
If it still refuses with gdm stopped and |
|
Follow-up to my partial receipt: the carveout-on path, now on the same Spark set up per the recipe (kernel 6.17.0-1029-nvidia, driver 580.173.02,
Serving, off / on / off / on, each a fresh process:
With the carveout the log shows One thing I hit on the way: these Sparks shipped an |
|
Thank you, @urtho, and thanks to @plotarmordev for both receipts. This is merged into main (e4e4385) with your We gated the default path on a GB10. With TF_CUDA_CARVEOUT unset, the four release prompts (story, code, facts and |
TF_CUDA_CARVEOUT_SOURCE=attic takes the carveout from spark-attic's atticd instead of the card. atticd holds the GB10 display reservation and lends it in whole 2 MiB slices, as file descriptors the engine imports (cuMemImportFromShareableHandle) and maps with CUDA's virtual memory calls, resolved the way arena.Vmm resolves them. The loan is a Carveout like the card's, so take, give, bytes, freeBytes and every caller's KV placement work unchanged. close unmaps the range and closes the socket, and atticd has the slices back. Opt-in and explicit: TF_CUDA_CARVEOUT=1 (or --carveout) still turns the carveout on, and the source defaults to drm. With drm the card path is byte for byte as it was: its defaults, messages and all-or-nothing rule. An unknown source is refused. The attic source keeps the same rule: without TF_CUDA_CARVEOUT_MIB it borrows atticd's longest free run, with it exactly that many MiB in whole slices, and anything short of that stops the load. TENSORFOLD_ATTIC_SOCK (default /run/spark-attic/attic.sock) and TENSORFOLD_ATTIC_KEY (else the first line of /etc/spark-attic/key) point it at atticd. Both places that open a carveout honour the switch: Nemotron's native engine and tensorfold run. docs/recipes/cuda.md describes it. Host tests: the source switch; a fake atticd over a socket pair (requests byte for byte, a descriptor beside its reply, refusals and malformed replies that leak no descriptor, a timeout, no daemon); the sizing rule; the key from the environment or the key file. Credits: urtho wrote cuda.Carveout and its spans (ashhart#487), which this builds on; coolbho3k had the idea of the display carveout as KV room; ezoushen and BadAd84 put display KV into the GLM recipe; Kindling's dispramd introduced the lending protocol atticd speaks; plotarmordev gave receipts on a second Spark; Mia's Qwen3.8-Flash-Next recipe is what this is for; Ash's TensorFold.
TF_CUDA_CARVEOUT=1 puts Flash Next's KV caches in the display carveout too: a contiguous cache holds it one at a time, as many whole planes of a full window as fit (cuda_carve.zig) - the keys and values of the first attention layers (both or neither), then indexer keys, then pooled keys. A shorter cache takes the same planes, each smaller, so it fits too and never needs more device memory than a full window. The DeltaNet states, the n-gram tail, the MTP head's cache and the pool's pages stay in device memory. The memory budget counts the planes out of device memory: Loaded.carved_bytes joins the room cuda_streams.zig counts the streams in (kv_pool.streamsRoom), which the grown streams, the shared page pool, a KV ledger's plan and the kept prompt cache are sized from. So pages or streams admitted grow by about the lent bytes. Nothing is counted when another cache may hold the carveout while the first stream runs: --slide's learner, a page test, or every stream on pages. The carveout opens before the checkpoint load finishes, so a refusal keeps the page cache as any other refusal does. The startup log names the planes, their bytes and the bytes counted. The settings are read through platform/environment.zig, which refuses malformed values: TF_CUDA_CARVEOUT_SOURCE picks the source (commit 1), and the TF_DRM_CARD card and the atticd socket and key follow its rules here too. Opt-in and off by default: without TF_CUDA_CARVEOUT nothing changes. One rank: --tp 2 with the carveout is refused (FlashNextCarveoutUnsupported). Nemotron's placement and the card source are unchanged. Only the planes' addresses change, not the math. Host tests: the plane sizes against seqBytes; the plan at 2,044 MiB, at 1,792 MiB, at a short window and with no room; every cache size up to the window takes the window's planes from an empty carveout, in order; a cache's planes by part; and the pool gaining the carved planes' whole pages (135 pages of 512 tokens for 2,032 MiB at 30,784 bytes a token) while the prompt cache stays at its budget. Test fixtures that build an Engine field by field set carve = null. docs/recipes/cuda.md and flash-next-cuda.md describe it. Credits: urtho wrote cuda.Carveout and its spans (ashhart#487), which this builds on; coolbho3k had the idea of the display carveout as KV room; ezoushen and BadAd84 put display KV into the GLM recipe; Kindling's dispramd introduced the lending protocol atticd speaks; plotarmordev gave receipts on a second Spark; Mia's Qwen3.8-Flash-Next work, her checkpoint and settings, is what this is for; Ash's TensorFold.
TF_CUDA_CARVEOUT=1 puts Flash Next's KV caches in the display carveout too: a contiguous cache holds it one at a time, as many whole planes of a full window as fit (cuda_carve.zig) - the keys and values of the first attention layers (both or neither), then indexer keys, then pooled keys. A shorter cache takes the same planes, each smaller, so it fits too and never needs more device memory than a full window. The DeltaNet states, the n-gram tail, the MTP head's cache and the pool's pages stay in device memory. The memory budget counts the planes out of device memory: Loaded.carved_bytes joins the room cuda_streams.zig counts the streams in (kv_pool.streamsRoom), which the grown streams, the shared page pool, a KV ledger's plan and the kept prompt cache are sized from. So pages or streams admitted grow by about the lent bytes. Nothing is counted when another cache may hold the carveout while the first stream runs: --slide's learner, a page test, or every stream on pages. The carveout opens before the checkpoint load finishes, so a refusal keeps the page cache as any other refusal does. The startup log names the planes, their bytes and the bytes counted. Flash Next makes the same two calls the CLI and Nemotron make: carveout.zig's requested(Settings) reads every row through the settings table, TF_DRM_CARD included, and Carveout.from opens the source the request names and logs a refusal once. The daemon's rows are read only while the daemon is the source, and the card's only while the card is. cuda_carve.zig holds no opener of its own. Opt-in and off by default: without TF_CUDA_CARVEOUT nothing changes. One rank: --tp 2 with the carveout is refused (FlashNextCarveoutUnsupported). Nemotron's placement and the card source are unchanged. Only the planes' addresses change, not the math. Host tests: the plane sizes against seqBytes; the plan at 2,044 MiB, at 1,792 MiB, at a short window and with no room; every cache size up to the window takes the window's planes from an empty carveout, in order; a cache's planes by part; and the pool gaining the carved planes' whole pages (135 pages of 512 tokens for 2,032 MiB at 30,784 bytes a token) while the prompt cache stays at its budget. Test fixtures that build an Engine field by field set carve = null. cuda_lanes.zig and cuda_prompt.zig stay under the 600-line limit: an unused import goes, the layer-map test moves to cuda_prompt_test.zig, and the engine closes the carveout. docs/recipes/cuda.md and flash-next-cuda.md describe it. Credits: urtho wrote cuda.Carveout and its spans (ashhart#487), which this builds on; coolbho3k had the idea of the display carveout as KV room; ezoushen and BadAd84 put display KV into the GLM recipe; Kindling's dispramd introduced the lending protocol atticd speaks; plotarmordev gave receipts on a second Spark; Mia's Qwen3.8-Flash-Next work, her checkpoint and settings, is what this is for; Ash's TensorFold.
TF_CUDA_CARVEOUT=1 puts Flash Next's KV caches in the display carveout too: a contiguous cache holds it one at a time, as many whole planes of a full window as fit (cuda_carve.zig) - the keys and values of the first attention layers (both or neither), then indexer keys, then pooled keys. A shorter cache takes the same planes, each smaller, so it fits too and never needs more device memory than a full window. The DeltaNet states, the n-gram tail, the MTP head's cache and the pool's pages stay in device memory. The memory budget counts the planes out of device memory: Loaded.carved_bytes joins the room cuda_streams.zig counts the streams in (kv_pool.streamsRoom), which the grown streams, the shared page pool, a KV ledger's plan and the kept prompt cache are sized from. So pages or streams admitted grow by about the lent bytes. Nothing is counted when another cache may hold the carveout while the first stream runs: --slide's learner, a page test, or every stream on pages. The carveout opens before the checkpoint load finishes, so a refusal keeps the page cache as any other refusal does. The startup log names the planes, their bytes and the bytes counted. Flash Next makes the same two calls the CLI and Nemotron make: carveout.zig's requested(Settings) reads every row through the settings table, TF_DRM_CARD included, and Carveout.from opens the source the request names and logs a refusal once. The daemon's rows are read only while the daemon is the source, and the card's only while the card is. cuda_carve.zig holds no opener of its own. Opt-in and off by default: without TF_CUDA_CARVEOUT nothing changes. One rank: --tp 2 with the carveout is refused (FlashNextCarveoutUnsupported). Nemotron's placement and the card source are unchanged. Only the planes' addresses change, not the math. Host tests: the plane sizes against seqBytes; the plan at 2,044 MiB, at 1,792 MiB, at a short window and with no room; every cache size up to the window takes the window's planes from an empty carveout, in order; a cache's planes by part; and the pool gaining the carved planes' whole pages (135 pages of 512 tokens for 2,032 MiB at 30,784 bytes a token) while the prompt cache stays at its budget. Test fixtures that build an Engine field by field set carve = null. cuda_lanes.zig and cuda_prompt.zig stay under the 600-line limit: an unused import goes, the layer-map test moves to cuda_prompt_test.zig, and the engine closes the carveout. docs/recipes/cuda.md and flash-next-cuda.md describe it. Credits: urtho wrote cuda.Carveout and its spans (ashhart#487), which this builds on; coolbho3k had the idea of the display carveout as KV room; ezoushen and BadAd84 put display KV into the GLM recipe; Kindling's dispramd introduced the lending protocol atticd speaks; plotarmordev gave receipts on a second Spark; Mia's Qwen3.8-Flash-Next work, her checkpoint and settings, is what this is for; Ash's TensorFold.
TF_CUDA_CARVEOUT=1 puts Flash Next's KV caches in the display carveout too: a contiguous cache holds it one at a time, as many whole planes of a full window as fit (cuda_carve.zig) - the keys and values of the first attention layers (both or neither), then indexer keys, then pooled keys. A shorter cache takes the same planes, each smaller, so it fits too and never needs more device memory than a full window. The DeltaNet states, the n-gram tail, the MTP head's cache and the pool's pages stay in device memory. The memory budget counts the planes out of device memory: Loaded.carved_bytes joins the room cuda_streams.zig counts the streams in (kv_pool.streamsRoom), which the grown streams, the shared page pool, a KV ledger's plan and the kept prompt cache are sized from. So pages or streams admitted grow by about the lent bytes. Nothing is counted when another cache may hold the carveout while the first stream runs: --slide's learner, a page test, or every stream on pages. The carveout opens before the checkpoint load finishes, so a refusal keeps the page cache as any other refusal does. The startup log names the planes, their bytes and the bytes counted. Flash Next makes the same two calls the CLI and Nemotron make: carveout.zig's requested(Settings) reads every row through the settings table, TF_DRM_CARD included, and Carveout.from opens the source the request names and logs a refusal once. The daemon's rows are read only while the daemon is the source, and the card's only while the card is. cuda_carve.zig holds no opener of its own. Opt-in and off by default: without TF_CUDA_CARVEOUT nothing changes. One rank: --tp 2 with the carveout is refused (FlashNextCarveoutUnsupported). Nemotron's placement and the card source are unchanged. Only the planes' addresses change, not the math. Host tests: the plane sizes against seqBytes; the plan at 2,044 MiB, at 1,792 MiB, at a short window and with no room; every cache size up to the window takes the window's planes from an empty carveout, in order; a cache's planes by part; and the pool gaining the carved planes' whole pages (135 pages of 512 tokens for 2,032 MiB at 30,784 bytes a token) while the prompt cache stays at its budget. Test fixtures that build an Engine field by field set carve = null. cuda_lanes.zig and cuda_prompt.zig stay under the 600-line limit: an unused import goes, the layer-map test moves to cuda_prompt_test.zig, and the engine closes the carveout. docs/recipes/cuda.md and flash-next-cuda.md describe it. Credits: urtho wrote cuda.Carveout and its spans (ashhart#487), which this builds on; coolbho3k had the idea of the display carveout as KV room; ezoushen and BadAd84 put display KV into the GLM recipe; Kindling's dispramd introduced the lending protocol atticd speaks; plotarmordev gave receipts on a second Spark; Mia's Qwen3.8-Flash-Next work, her checkpoint and settings, is what this is for; Ash's TensorFold.
TF_CUDA_CARVEOUT=1 puts Flash Next's KV caches in the display carveout too: a contiguous cache holds it one at a time, as many whole planes of a full window as fit (cuda_carve.zig) - the keys and values of the first attention layers (both or neither), then indexer keys, then pooled keys. A shorter cache takes the same planes, each smaller, so it fits too and never needs more device memory than a full window. The DeltaNet states, the n-gram tail, the MTP head's cache and the pool's pages stay in device memory. The memory budget counts the planes out of device memory: Loaded.carved_bytes joins the room cuda_streams.zig counts the streams in (kv_pool.streamsRoom), which the grown streams, the shared page pool, a KV ledger's plan and the kept prompt cache are sized from. So pages or streams admitted grow by about the lent bytes. Nothing is counted when another cache may hold the carveout while the first stream runs: --slide's learner, a page test, or every stream on pages. The carveout opens before the checkpoint load finishes, so a refusal keeps the page cache as any other refusal does. The startup log names the planes, their bytes and the bytes counted. Flash Next makes the same two calls the CLI and Nemotron make: carveout.zig's requested(Settings) reads every row through the settings table, TF_DRM_CARD included, and Carveout.from opens the source the request names and logs a refusal once. The daemon's rows are read only while the daemon is the source, and the card's only while the card is. cuda_carve.zig holds no opener of its own. Opt-in and off by default: without TF_CUDA_CARVEOUT nothing changes. One rank: --tp 2 with the carveout is refused (FlashNextCarveoutUnsupported). Nemotron's placement and the card source are unchanged. Only the planes' addresses change, not the math. Host tests: the plane sizes against seqBytes; the plan at 2,044 MiB, at 1,792 MiB, at a short window and with no room; every cache size up to the window takes the window's planes from an empty carveout, in order; a cache's planes by part; and the pool gaining the carved planes' whole pages (135 pages of 512 tokens for 2,032 MiB at 30,784 bytes a token) while the prompt cache stays at its budget. Test fixtures that build an Engine field by field set carve = null. cuda_lanes.zig and cuda_prompt.zig stay under the 600-line limit: an unused import goes, the layer-map test moves to cuda_prompt_test.zig, and the engine closes the carveout. docs/recipes/cuda.md and flash-next-cuda.md describe it. Credits: urtho wrote cuda.Carveout and its spans (ashhart#487), which this builds on; coolbho3k had the idea of the display carveout as KV room; ezoushen and BadAd84 put display KV into the GLM recipe; Kindling's dispramd introduced the lending protocol atticd speaks; plotarmordev gave receipts on a second Spark; Mia's Qwen3.8-Flash-Next work, her checkpoint and settings, is what this is for; Ash's TensorFold.
TF_CUDA_CARVEOUT=1 puts Flash Next's KV caches in the display carveout too: a contiguous cache holds it one at a time, as many whole planes of a full window as fit (cuda_carve.zig) - the keys and values of the first attention layers (both or neither), then indexer keys, then pooled keys. A shorter cache takes the same planes, each smaller, so it fits too and never needs more device memory than a full window. The DeltaNet states, the n-gram tail, the MTP head's cache and the pool's pages stay in device memory. The memory budget counts the planes out of device memory: Loaded.carved_bytes joins the room cuda_streams.zig counts the streams in (kv_pool.streamsRoom), which the grown streams, the shared page pool, a KV ledger's plan and the kept prompt cache are sized from. So pages or streams admitted grow by about the lent bytes. Nothing is counted when another cache may hold the carveout while the first stream runs: --slide's learner, a page test, or every stream on pages. The carveout opens before the checkpoint load finishes, so a refusal keeps the page cache as any other refusal does. The startup log names the planes, their bytes and the bytes counted. Flash Next makes the same two calls the CLI and Nemotron make: carveout.zig's requested(Settings) reads every row through the settings table, TF_DRM_CARD included, and Carveout.from opens the source the request names and logs a refusal once. The daemon's rows are read only while the daemon is the source, and the card's only while the card is. cuda_carve.zig holds no opener of its own. Opt-in and off by default: without TF_CUDA_CARVEOUT nothing changes. One rank: --tp 2 with the carveout is refused (FlashNextCarveoutUnsupported). Nemotron's placement and the card source are unchanged. Only the planes' addresses change, not the math. Host tests: the plane sizes against seqBytes; the plan at 2,044 MiB, at 1,792 MiB, at a short window and with no room; every cache size up to the window takes the window's planes from an empty carveout, in order; a cache's planes by part; and the pool gaining the carved planes' whole pages (135 pages of 512 tokens for 2,032 MiB at 30,784 bytes a token) while the prompt cache stays at its budget. Test fixtures that build an Engine field by field set carve = null. cuda_lanes.zig and cuda_prompt.zig stay under the 600-line limit: an unused import goes, the layer-map test moves to cuda_prompt_test.zig, and the engine closes the carveout. docs/recipes/cuda.md and flash-next-cuda.md describe it. Credits: urtho wrote cuda.Carveout and its spans (ashhart#487), which this builds on; coolbho3k had the idea of the display carveout as KV room; ezoushen and BadAd84 put display KV into the GLM recipe; Kindling's dispramd introduced the lending protocol atticd speaks; plotarmordev gave receipts on a second Spark; Mia's Qwen3.8-Flash-Next work, her checkpoint and settings, is what this is for; Ash's TensorFold.
TF_CUDA_CARVEOUT=1 puts Flash Next's KV caches in the display carveout too: a contiguous cache holds it one at a time, as many whole planes of a full window as fit (cuda_carve.zig) - the keys and values of the first attention layers (both or neither), then indexer keys, then pooled keys. A shorter cache takes the same planes, each smaller, so it fits too and never needs more device memory than a full window. The DeltaNet states, the n-gram tail, the MTP head's cache and the pool's pages stay in device memory. The memory budget counts the planes out of device memory: Loaded.carved_bytes joins the room cuda_streams.zig counts the streams in (kv_pool.streamsRoom), which the grown streams, the shared page pool, a KV ledger's plan and the kept prompt cache are sized from. So pages or streams admitted grow by about the lent bytes. Nothing is counted when another cache may hold the carveout while the first stream runs: --slide's learner, a page test, or every stream on pages. The carveout opens before the checkpoint load finishes, so a refusal keeps the page cache as any other refusal does. The startup log names the planes, their bytes and the bytes counted. Flash Next makes the same two calls the CLI and Nemotron make: carveout.zig's requested(Settings) reads every row through the settings table, TF_DRM_CARD included, and Carveout.from opens the source the request names and logs a refusal once. The daemon's rows are read only while the daemon is the source, and the card's only while the card is. cuda_carve.zig holds no opener of its own. Opt-in and off by default: without TF_CUDA_CARVEOUT nothing changes. One rank: --tp 2 with the carveout is refused (FlashNextCarveoutUnsupported). Nemotron's placement and the card source are unchanged. Only the planes' addresses change, not the math. Host tests: the plane sizes against seqBytes; the plan at 2,044 MiB, at 1,792 MiB, at a short window and with no room; every cache size up to the window takes the window's planes from an empty carveout, in order; a cache's planes by part; and the pool gaining the carved planes' whole pages (135 pages of 512 tokens for 2,032 MiB at 30,784 bytes a token) while the prompt cache stays at its budget. Test fixtures that build an Engine field by field set carve = null. cuda_lanes.zig and cuda_prompt.zig stay under the 600-line limit: an unused import goes, the layer-map test moves to cuda_prompt_test.zig, and the engine closes the carveout. docs/recipes/cuda.md and flash-next-cuda.md describe it. Credits: urtho wrote cuda.Carveout and its spans (ashhart#487), which this builds on; coolbho3k had the idea of the display carveout as KV room; ezoushen and BadAd84 put display KV into the GLM recipe; Kindling's dispramd introduced the lending protocol atticd speaks; plotarmordev gave receipts on a second Spark; Mia's Qwen3.8-Flash-Next work, her checkpoint and settings, is what this is for; Ash's TensorFold.
TF_CUDA_CARVEOUT=1 puts Flash Next's KV caches in the display carveout too: a contiguous cache holds it one at a time, as many whole planes of a full window as fit (cuda_carve.zig) - the keys and values of the first attention layers (both or neither), then indexer keys, then pooled keys. A shorter cache takes the same planes, each smaller, so it fits too and never needs more device memory than a full window. The DeltaNet states, the n-gram tail, the MTP head's cache and the pool's pages stay in device memory. The memory budget counts the planes out of device memory: Loaded.carved_bytes joins the room cuda_streams.zig counts the streams in (kv_pool.streamsRoom), which the grown streams, the shared page pool, a KV ledger's plan and the kept prompt cache are sized from. So pages or streams admitted grow by about the lent bytes. Nothing is counted when another cache may hold the carveout while the first stream runs: --slide's learner, a page test, or every stream on pages. The carveout opens before the checkpoint load finishes, so a refusal keeps the page cache as any other refusal does. The startup log names the planes, their bytes and the bytes counted. Flash Next makes the same two calls the CLI and Nemotron make: carveout.zig's requested(Settings) reads every row through the settings table, TF_DRM_CARD included, and Carveout.from opens the source the request names and logs a refusal once. The daemon's rows are read only while the daemon is the source, and the card's only while the card is. cuda_carve.zig holds no opener of its own. Opt-in and off by default: without TF_CUDA_CARVEOUT nothing changes. One rank: --tp 2 with the carveout is refused (FlashNextCarveoutUnsupported). Nemotron's placement and the card source are unchanged. Only the planes' addresses change, not the math. Host tests: the plane sizes against seqBytes; the plan at 2,044 MiB, at 1,792 MiB, at a short window and with no room; every cache size up to the window takes the window's planes from an empty carveout, in order; a cache's planes by part; and the pool gaining the carved planes' whole pages (135 pages of 512 tokens for 2,032 MiB at 30,784 bytes a token) while the prompt cache stays at its budget. Test fixtures that build an Engine field by field set carve = null. cuda_lanes.zig and cuda_prompt.zig stay under the 600-line limit: an unused import goes, the layer-map test moves to cuda_prompt_test.zig, and the engine closes the carveout. docs/recipes/cuda.md and flash-next-cuda.md describe it. Credits: urtho wrote cuda.Carveout and its spans (ashhart#487), which this builds on; coolbho3k had the idea of the display carveout as KV room; ezoushen and BadAd84 put display KV into the GLM recipe; Kindling's dispramd introduced the lending protocol atticd speaks; plotarmordev gave receipts on a second Spark; Mia's Qwen3.8-Flash-Next work, her checkpoint and settings, is what this is for; Ash's TensorFold.
TF_CUDA_CARVEOUT=1 puts Flash Next's KV caches in the display carveout too: a contiguous cache holds it one at a time, as many whole planes of a full window as fit (cuda_carve.zig) - the keys and values of the first attention layers (both or neither), then indexer keys, then pooled keys. A shorter cache takes the same planes, each smaller, so it fits too and never needs more device memory than a full window. The DeltaNet states, the n-gram tail, the MTP head's cache and the pool's pages stay in device memory. The memory budget counts the planes out of device memory: Loaded.carved_bytes joins the room cuda_streams.zig counts the streams in (kv_pool.streamsRoom), which the grown streams, the shared page pool, a KV ledger's plan and the kept prompt cache are sized from. So pages or streams admitted grow by about the lent bytes. Nothing is counted when another cache may hold the carveout while the first stream runs: --slide's learner, a page test, or every stream on pages. The carveout opens before the checkpoint load finishes, so a refusal keeps the page cache as any other refusal does. The startup log names the planes, their bytes and the bytes counted. Flash Next makes the same two calls the CLI and Nemotron make: carveout.zig's requested(Settings) reads every row through the settings table, TF_DRM_CARD included, and Carveout.from opens the source the request names and logs a refusal once. The daemon's rows are read only while the daemon is the source, and the card's only while the card is. cuda_carve.zig holds no opener of its own. Opt-in and off by default: without TF_CUDA_CARVEOUT nothing changes. One rank: --tp 2 with the carveout is refused (FlashNextCarveoutUnsupported). Nemotron's placement and the card source are unchanged. Only the planes' addresses change, not the math. Host tests: the plane sizes against seqBytes; the plan at 2,044 MiB, at 1,792 MiB, at a short window and with no room; every cache size up to the window takes the window's planes from an empty carveout, in order; a cache's planes by part; and the pool gaining the carved planes' whole pages (135 pages of 512 tokens for 2,032 MiB at 30,784 bytes a token) while the prompt cache stays at its budget. Test fixtures that build an Engine field by field set carve = null. cuda_lanes.zig and cuda_prompt.zig stay under the 600-line limit: an unused import goes, the layer-map test moves to cuda_prompt_test.zig, and the engine closes the carveout. docs/recipes/cuda.md and flash-next-cuda.md describe it. Credits: urtho wrote cuda.Carveout and its spans (ashhart#487), which this builds on; coolbho3k had the idea of the display carveout as KV room; ezoushen and BadAd84 put display KV into the GLM recipe; Kindling's dispramd introduced the lending protocol atticd speaks; plotarmordev gave receipts on a second Spark; Mia's Qwen3.8-Flash-Next work, her checkpoint and settings, is what this is for; Ash's TensorFold.
Base branch:
main. CONTRIBUTING namesmainas the Zig engine's main line, #452 names no other release branch,and
main,zig-flashnextandzig-previeware all 041d14a, so the diff is the same against any of them.Retarget to
zig-flashnextif you'd rather take Zig work there.What this changes
On GB10,
tensorfold run --carveout, orTF_CUDA_CARVEOUT=1fortensorfold runandtensorfold-native,maps the ~1.75 GiB of memory the display controller reserves (a DRM dumb buffer, registered with CUDA) and
puts Nemotron's KV cache planes there. That frees the same amount of ordinary memory. It is off by default, and
the output tokens don't change. Host setup (kernel mode setting, no display, card access, a check) is in
docs/recipes/cuda.md. Closes #452.Receipt
drm-carveout-v2: 6 commits on 041d14a (the sixth is documentation only) (TensorFold 1.0.1). The base build is 041d14a itself.nvcr.io/nvidia/pytorch:26.07-py3(PyTorch 2.13.0a0+9186a08b2c.nv26.07, nvcc13.3.73,
-Dsm=121,-Doptimize=safeasbox/build.shbuilds). Driver 595.91.07, CUDA 13.3 (13030).nvidia_drm modeset=1 fbdev=0(DGX OS setsmodeset=1), every connector disconnected,multi-user.target.TensorFold/NVIDIA-Nemotron-3.5-Lightning-30B-A3B-MLX-4bit, revision d9d758fb.--gpus all --device /dev/dri/card0. The host user can't open thecard node.
zig/tests/cuda/nemotron/box/README.md: cap1 at the defaults, then cap2 with--context 262144 --repeats 1 --max-tokens 32.aot_pack.pyintocap1(32 kernels) andboth(36 kernels, adding the NCH 513 attentionpair).
mainno longer carries the Python engine the capture runs. The oracle wassrc/tensorfoldfrom4af9503, the last
maintree that had it (parent of 394d566). Its Nemotron draft vocabulary isbyte-identical to
zig/src/families/nemotron/draft_ids.txthere.and two resident monitoring containers (no GPU use) were running. So the scripts' docker steps were run by
hand with the same arguments.
box/build.shprinted SASS-EQUAL for every kernel of the 7 fatbin pairs (SASS-LAYOUT on experts'plan_kernel). It still exits 1, because it callszig/tests/cuda/copies.py, whichmainremoved.tools/bench_concurrent.py --alone --serialortoken_shaagainst"draft": false:Served: the base, this branch off and this branch on (
TF_CUDA_CARVEOUT=1), 3 interleaved sessions each.bench_concurrent.py URL nemotron --tokens 256 --reps 1 --temperatures 0 --levels 1,4 --alone --serial.failures.
sha=was the same in all 9 sessions for every request: code6c283e8c11ae(81 replies), chat
f19104bea28f(81 replies), and each cold prompt's two tokens.CLI, the repository gate (
zig/tests/cuda/nemotron/gate.py, the capture's four prompts, 256 tokens, greedy,against the Python engine's serial tokens):
--no-drafts)--no-drafts)Shas (first 12 hex) in all four runs: story
26c86e423075, codeb61f085a2d9b, factsecc75781fbe3,long
eb17a9a9c8d4.CLI decode below: all 18 runs carry
token_sha256b61f085a2d9be1bcaf62298ebfb2ae7634532b68c5cba4d1c6c20b93d6c361f2.The 18 memory runs (64 tokens) carry one sha,
9e9b73b1aa77....tools/bench_openai.py:Served:
bench_openai.py URL nemotron --tokens 256 --reps 1 --temperatures 0,--context 65600 --prompt-cache-gib 0. Sessions interleaved base, off, on, three times. Decode tok/s:The same sessions, from
bench_concurrent.py. Aggregate tok/s, one value per session:The 4-stream aggregate is bimodal in all three builds (about 290-360 vs 360-460 tok/s), the base included.
Three sessions can't separate a carveout effect from that. With the carveout on, the lane streams' KV
(387 MiB each at this window) sits in the carveout while it has room.
CLI:
tensorfold run MODEL --tokens CODE_IDS --max-tokens 256 --kernels cap1 [--no-drafts] [--carveout],the 41-token
codeprompt, default context. Three reps, interleaved base, off, on within each rep.ms/token:
--no-draftscarved_bytesis 103,809,024 with--carveout: the own sequence's K and V planes, 99 MiB.tools/prefill_cold.py:tools/prefill_cold.pyis not inmain; it is onpython-0.6. It builds chat prompts with the Pythonpackage's tokenizer and template, so its prompts land near each length, not on it.
Instead, a standard-library client sent exact token-id arrays through
/v1/completionsto the same nativeservers,
max_tokens2, temperature 0, streamed. The client (coldw.py, warmed;cold.pywithout the warm-up request) andarrays.jsonfollow in a comment.The arrays hold exactly 2,048, 8,192, 32,768 and 65,536 ids:
Request cold-N.\nplus this repository'szig/src/**/*.zigat 041d14a, sorted by path, tokenized by the server's/tokenize, then cut to N.sha256 of each id list:
9931dbaa...2eda1657f98569...7720279b44904c...6273cbc8b428d6...f2bd62Every request reported
prompt_tokens= N andcached_tokens0. A fresh session ran each round, with--prompt-cache-gib 0, so each prompt started on fresh KV.An unmeasured 1,024-id request opens each session, as
prefill_cold.pydoes. Without it, the firstmeasured request carried a warm-up cost: 2,048 ids took 0.2741 s on the base vs 0.2774 and 0.2782 s on the
branch. That run's 8k to 64k timings match the table below.
TTFT in seconds over three interleaved sessions, median, then prompt tok/s:
No
mlx_lmor vLLM comparison was run. This change moves buffers and leaves the prompt arithmetic alone.Memory receipt, served at
--context 65600(8 streams admitted in every build). The lowest MemAvailable,sampled every 0.2 s over each session (median of 3), and the engine's own device byte counter
(
tensorfold:device_memory_bytesonce loaded, thendevice_memory_peak_bytesat the end):With the carveout on, the startup line reads model 18.06 GiB instead of 18.44 GiB: the own sequence's
387 MiB of KV is carved. The carved spans are borrowed, so the device counter leaves them out.
Memory receipt, CLI (
--no-drafts, 64 tokens, kernel setboth). The lowest MemAvailable, median of 3, andthe peak process RSS:
--contextcuMemGetInfofree is printed only with--carveout(thecache roomline). It read 101,322-101,836MiB at 16,880 and 101,031-101,308 MiB at 262,640, with 1,693 and 253 MiB left in the carveout.
difference. MemAvailable and the engine's counter can.
zig build test:carveout.zigplus one inmemory.zig.--cache-dir. With the cache on ext4,direct_io's "direct reads return the samebytes as buffered reads" fails on the base and on the branch alike (83/84 here, 75/76 base). That test
opens the test binary itself with O_DIRECT, so it is inherited and depends on the filesystem.
zig build test-golden: not applicable on Linux; the step exists only in the macOS (Metal) build graph.python3 tools/zig/lean_check.py: 513 problems on the base (764 files) and 513 on the branch (766 files),so zero added.
columns, 4 multi-line docstrings, 4 files over 600 lines and 1 block comment.
cuda_engine.zig:266,:329and
tests/cuda/main.zig:1.zig fmt --check: the branch's hunks are clean. Two touched files also fail on the base, in lines thisbranch doesn't touch (
cuda_state.zig's size table andscratchPtrs,tests/cuda/main.zig'sinfoprint). They were left alone.
carveout.zigandmemory.zigcompile for aarch64-macos, x86_64-linux and aarch64-linux(
zig test -fno-emit-bin), including a forced analysis ofCarveout.openfor macOS.tf-cuda-test:info,smoke5 PASS (and the expected refusals),graph3 PASS,samplePASS.carveoutpassed in three runs:tf-cuda-test carveout 1792 /dev/dri/card9: SKIP, rc 0.TF_DRM_CARD=/dev/dri/card7withtensorfold run --carveoutorTF_CUDA_CARVEOUT=1 tensorfold-native serve:display carveout of 1792 MiB from /dev/dri/card7: CardUnavailable (needs nvidia_drm modeset=1, the card's device node and no display in use). The server then refuses the load, rc 1.zig build teston a Macalso builds the CUDA host tests, so
carveout.ziggets analyzed there: compile-only status above, notexecuted on macOS.
memory. The device test skips on a GPU that is not integrated, and the option is off unless asked for.
through
Buffers.snapshotandrestore, which this branch changes to walk the seven planes.(NCH 513), in both modes.
Checklist
"draft": false, a resumed prompt equals a fresh one, and concurrent equals solo.Drafted equals serial and concurrent equals solo in every session, carveout off and on. The resumed prompt
was not tested: Nemotron CUDA has no prompt reuse at this base.
kernel or sum changes; only where the KV planes live.
is within 0.0-0.3% of the base medians, inside the run-to-run ranges.
cover new code: DRM ioctl numbers and struct layouts, span placement, opt-in parsing, borrowed-span frees and
the device round trips. They have no "before" to fail on. The device test skips cleanly (shown above).
Notes for review
CUDA runtime: GB10's display memory as device memory.
zig/src/cuda/carveout.zig(new, 232 lines):drm_mode.h,mmap, thencuMemHostRegister(DEVICEMAP, thenDEVICEMAP|IOMEMORY) andcuMemHostGetDevicePointer;DeviceBuffers.abi.zigaddscuMemHostRegister_v2andcuMemHostUnregisterwith the flags. Inmemory.zig,freeleaves a borrowed span alone and keeps it out of the new device byte counters.
tf-cuda-test carveout: round trips and a copy and kernel-read bandwidth probe. It skips without the
card.
Nemotron CUDA: KV caches in the display carveout behind
--carveout.scratch stay in device memory.
The device test skips on a discrete GPU; the recipe wording changes.
The native server's opt-in,
TF_CUDA_CARVEOUT=1. The family owns the carveout and closes it afterthe engine.
Recipe: preparing a DGX Spark for the carveout. Kernel mode setting, no display or desktop session,
access to the card node (the
videogroup or a container device), andtf-cuda-test carveoutas the check.Stream admission still counts each stream's full bytes, so admission stays conservative. Should the
carved room feed admission? That is open for you to decide.
memory.ziggained the device byte counters upstream. A borrowed span now skips bothcuMemFreeand thecounter.
Seq.initnow takes the owning stream as well as the carveout.Seq.bytes, which admission uses, staysthe full size.
seq_fieldsfollows upstream (rule).zig-flashnext):zig/src/families/nemotron/cuda_native.zig, aroundOwnedandopen, andcuda_engine.zig, in different functions. This branch doesn't touchzig/src/native/cuda.zig.cuda_reuse.zigcallse.b.stateBytes(),e.b.snapshot()ande.b.restore(). This branch keepstheir signatures. A snapshot is now packed plane by plane, because the KV pair may live in the carveout.
cuda_native.zig.DeepSeek-v4.1-Flash-2x-DGX-Spark, which
is AGPL-3.0. None of that code is used. This implementation is written from the Linux DRM UAPI
(
drm_mode.h) and NVIDIA's CUDA driver API documentation.dsv41-cuda).