Skip to content

llama/cuda: tiered KV cache (--kv-vram-cells): VRAM head, pinned-host tail in one VMM range - #319

Open
professorpalmer wants to merge 3 commits into
PrismML-Eng:prismfrom
professorpalmer:claude/tiered-kv
Open

professorpalmer wants to merge 3 commits into
PrismML-Eng:prismfrom
professorpalmer:claude/tiered-kv

Conversation

@professorpalmer

Copy link
Copy Markdown

One of the five pieces of #285, split per bri-prism's request on #317 so each can land on its own. Single commit on current prism (eaecb50c7), applies without conflicts.

What it does

llama_context_params.n_kv_vram_cells / --kv-vram-cells N keeps cells [0, N) of each layer's K/V in device memory and the rest in pinned host memory mapped into the same device range with CUDA VMM (cuMemCreate with CU_MEM_LOCATION_TYPE_HOST); ggml_backend_cuda_tier_buffer_type is exported through the registry. Kernels are unchanged and output is bit-identical to an all-device cache. When an attention op reaches the host tail, the used rows are copied into a VRAM staging buffer by the copy engine (a second VMM alias maps the same VRAM head plus the staging buffer) and the op reads the alias: prefill reads the tail once per op instead of once per query tile (146 -> 280 tok/s at 180k on the 4070), decode moves it by DMA (+28% at 180k). Plumbed through llama_cparams into the standard KV cache and the hybrid memory with a trailing defaulted constructor parameter; other memory types untouched. Works on Windows/WDDM.

Why: q8_0 at 262k is 9.1 GB of K/V for Bonsai 2 27B and does not fit next to the weights on 12 GB; #221 reached 262k with q4_0 (mean KLD 0.00218 vs f16 K/V, 97.9% top-1 agreement) where q8_0 measures 0.00017 / 99.4%.

Receipts (RTX 4070 12 GB, sm_89, PCIe 4.0 x16, GDDR6X +1500 MHz as in #221; llama-server, one slot, greedy, MTP head, genuine prefill)

depth prefill tok/s decode tok/s #221 recipe at that depth
4k 1,100 83
16k 1,100 106
32k 918 100 47.6 (96k/q8_0, drafting cut off at 24k)
64k 724 87 36.9
112k (last VRAM position) 539 70 q8_0 does not fit
131k 374 41 q8_0 does not fit
180k 298 27 q8_0 does not fit
258k 229 14.7 q8_0 does not fit

The decode numbers above 32k include the MTP drafting-window commit (separate PR) and the FA GQA-decode commit that is being folded into #307; the tiered cache itself changes no arithmetic.

  • Tiered vs all-VRAM KV, same flags, drafting on: identical sha256 of 256-token code and prose continuations at 4k, 20k and 40k; rechecked on the rebased head with --kv-vram-cells 2048 vs all-VRAM, q8_0, greedy, at 4096 / 20480 / 40960 tokens: 6/6 identical.
  • test-backend-ops -b CUDA0 on the stacked head and again on the rebased head: FLASH_ATTN_EXT 2994/2994, MUL_MAT 1516/1516, GATED_DELTA_NET 39/39, GET_ROWS 119/119 (MUL_MAT_ID 1034/1035: the intermittent m=70, n=1 case already in prism, fixed in tests: MUL_MAT_ID cases for the one-column mat-vec store (small K, several expert rows per block) #295).
  • On WDDM the VRAM line must stay below the point where Windows demotes a background process: ~1.3 GB below free VRAM with the display on the card, ~1.0 GB with it on an iGPU (10-minute soaks). Past the line decode is PCIe-bound.

Serving recipe and all receipts: https://github.com/professorpalmer/bonsai-ada-surgery/blob/main/docs/Q8_FULL_CONTEXT.md

professorpalmer added a commit to professorpalmer/llama.cpp-ada-ternary that referenced this pull request Oct 8, 2026
…ff; drop --spec-draft-n-max-tail

Review of PrismML-Eng#320:
- The window cutoff was the minimum position of the whole batch and was applied to every slot, so a deep
  slot could remove the rows of a shallow slot that was not in the batch, and a shallow slot in the batch
  left extra rows in a deep one. Now each sequence in the batch gets its own cutoff, and sequences that
  are not in the batch keep their rows.
- --spec-draft-n-max-tail needed n_kv_vram_cells from the tiered-KV change (PrismML-Eng#319), so this branch did not
  compile on its own. The tail flag, its per-slot fields and the n_max widening at load are removed; the
  tail returns stacked on the tiered-KV change, with the requested draft limits kept apart from the
  allocation size (the widening was also recaptured as the normal limit after sleep/wake).
- --spec-draft-window help text: one sentence, no forced line break.
@bri-prism

Copy link
Copy Markdown
Collaborator

Retested on Linux (H200, driver 570): with --kv-vram-cells set, it still fails at startup with cuMemCreate(host): invalid argument, and the context then fails to load. The Linux driver doesn't accept CU_MEM_LOCATION_TYPE_HOST for cuMemCreate, but CU_MEM_LOCATION_TYPE_HOST_NUMA with id set to the GPU's CU_DEVICE_ATTRIBUTE_HOST_NUMA_ID does. I tried that one change locally and it works: output is token-identical to an all-VRAM cache at 512 cells (staging on and off) and at a 29k-token prompt with 8192 cells in VRAM. Could you (1) use HOST_NUMA on non-Windows builds, and (2) fall back to the plain device buffer with a warning when the tier allocation fails, instead of failing context creation? Smaller points: please append n_kv_vram_cells at the end of llama_context_params rather than in the middle (ABI), and mention the cost in the help text. On an H200 with 72% of the cache in host memory, decode at 29k drops from about 250 to 60 t/s, which is the right trade on a 12 GB card but not on one where the cache fits. Also, have you run the staging path with CUDA graphs enabled?

professorpalmer added a commit to professorpalmer/llama.cpp-ada-ternary that referenced this pull request Oct 27, 2026
…ff; drop --spec-draft-n-max-tail

Review of PrismML-Eng#320:
- The window cutoff was the minimum position of the whole batch and was applied to every slot, so a deep
  slot could remove the rows of a shallow slot that was not in the batch, and a shallow slot in the batch
  left extra rows in a deep one. Now each sequence in the batch gets its own cutoff, and sequences that
  are not in the batch keep their rows.
- --spec-draft-n-max-tail needed n_kv_vram_cells from the tiered-KV change (PrismML-Eng#319), so this branch did not
  compile on its own. The tail flag, its per-slot fields and the n_max widening at load are removed; the
  tail returns stacked on the tiered-KV change, with the requested draft limits kept apart from the
  allocation size (the widening was also recaptured as the normal limit after sleep/wake).
- --spec-draft-window help text: one sentence, no forced line break.
@professorpalmer

Copy link
Copy Markdown
Author

Thank you for the Linux test and the fix. All points are in b56e06723, rebased on prism c96baf4ab:

  1. HOST_NUMA: outside Windows (and with CUDA 12.2 or later) the host pages use CU_MEM_LOCATION_TYPE_HOST_NUMA with the id from CU_DEVICE_ATTRIBUTE_HOST_NUMA_ID of the GPU. If the attribute is not available, it keeps CU_MEM_LOCATION_TYPE_HOST. Windows is unchanged.
  2. Fallback: when the tiered buffer cannot be allocated, the KV cache logs a warning and allocates the same tensors in the plain device buffer (the whole cache in VRAM). Context creation fails only when that also fails.
  3. ABI: n_kv_vram_cells is now the last member of llama_context_params, and the default initializer is in the same order.
  4. Help text: it states the cost (your H200 numbers: 72 % of the cache in host memory, 250 -> 60 t/s at 29k), and that the option is for caches that do not fit in VRAM. It also names the fallback.

CUDA graphs: yes. The staging copy is captured with the graph and replayed at each step, so it copies the current host rows. RTX 4070, Bonsai 2 27B PTQ1_0, -c 16384, q4_0 K/V, --kv-vram-cells 4096, an 11,743-token prompt, 64 greedy tokens:

Run Decode Tokens against an all-VRAM cache
all VRAM 66.4 t/s reference
tier 4096, CUDA graphs on (default) 47.2 t/s identical
tier 4096, GGML_CUDA_DISABLE_GRAPHS=1 44.9 t/s identical

0 CUDA errors. With prism's head-72 padding path, the conflict in ggml_cuda_flash_attn_ext resolved as: the pad72 early return first, then the staging. A CPU-only build of this branch alone compiles and links llama-server.

I could not test the HOST_NUMA path here (Windows only). Could you retest it on the H200 with this head?

professorpalmer added a commit to professorpalmer/llama.cpp-ada-ternary that referenced this pull request Nov 12, 2026
…ff; drop --spec-draft-n-max-tail

Review of PrismML-Eng#320:
- The window cutoff was the minimum position of the whole batch and was applied to every slot, so a deep
  slot could remove the rows of a shallow slot that was not in the batch, and a shallow slot in the batch
  left extra rows in a deep one. Now each sequence in the batch gets its own cutoff, and sequences that
  are not in the batch keep their rows.
- --spec-draft-n-max-tail needed n_kv_vram_cells from the tiered-KV change (PrismML-Eng#319), so this branch did not
  compile on its own. The tail flag, its per-slot fields and the n_max widening at load are removed; the
  tail returns stacked on the tiered-KV change, with the requested draft limits kept apart from the
  allocation size (the widening was also recaptured as the normal limit after sleep/wake).
- --spec-draft-window help text: one sentence, no forced line break.
@professorpalmer

professorpalmer commented Nov 12, 2026 •

Copy link
Copy Markdown
Author

I rebased this PR onto prism 6447906bf. The new head is 7779cc62e. The two commits stay.

Conflicts and how I resolved them:

  • include/llama.h, src/llama-context.cpp, common/common.cpp: prism added moe_cache_size to llama_context_params. I kept it. n_kv_vram_cells is still the last member, and the default initializer has the same order.
  • src/llama-kv-cache.h, src/llama-kv-cache.cpp: prism added the name_tag parameter at the end of the llama_kv_cache constructor. I put n_kv_vram_cells after name_tag. Both have a default value, so the prism callers did not change. The two callers in this PR (llama-model.cpp and llama-memory-hybrid.cpp) now give "" for name_tag.

RTX 4070 (sm_89), CUDA 13, Windows:

  • test-backend-ops -o FLASH_ATTN_EXT -b CUDA0: 4438/4438 passed.
  • test-backend-ops -o MUL_MAT -b CUDA0: 2943/2943 passed.

Bonsai 2 27B PTQ1_0, -c 16384, q4_0 K/V, an 11,743-token prompt, 64 greedy tokens (the same check as before):

Run Decode Tokens against an all-VRAM cache
all VRAM 59.3 t/s reference
--kv-vram-cells 4096, CUDA graphs on (default) 45.2 t/s identical
--kv-vram-cells 4096, GGML_CUDA_DISABLE_GRAPHS=1 38.3 t/s identical

The log shows tiered KV: cells [0, 4096) in VRAM, [4096, 16384) in host memory and 0 CUDA errors. The prompt text is not the same as on 10-27, and a CPU-only server from another job ran at the same time. Thus do not compare these speeds with the 10-27 speeds. The token identity is the result that counts here. I could not test the HOST_NUMA path (Windows only).

sb32445 pushed a commit to sb32445/llama.cpp that referenced this pull request Nov 20, 2026
…ff; drop --spec-draft-n-max-tail

Review of PrismML-Eng#320:
- The window cutoff was the minimum position of the whole batch and was applied to every slot, so a deep
  slot could remove the rows of a shallow slot that was not in the batch, and a shallow slot in the batch
  left extra rows in a deep one. Now each sequence in the batch gets its own cutoff, and sequences that
  are not in the batch keep their rows.
- --spec-draft-n-max-tail needed n_kv_vram_cells from the tiered-KV change (PrismML-Eng#319), so this branch did not
  compile on its own. The tail flag, its per-slot fields and the n_max widening at load are removed; the
  tail returns stacked on the tiered-KV change, with the requested draft limits kept apart from the
  allocation size (the widening was also recaptured as the normal limit after sleep/wake).
- --spec-draft-window help text: one sentence, no forced line break.

@bri-prism bri-prism left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working through the earlier points. HOST_NUMA, the fallback, the struct order and the help text all look right in this head. I'll rerun the Linux check on 7779cc62e since you can't test that path.

A few things I'd like fixed before merging:

  1. The staging buffers in ggml_cuda_tier_staging_handle are shared process-wide and looked up by slot only, so the device passed in is ignored after the first call. With a model split across two GPUs, the second GPU's alias maps the first GPU's staging memory. With pipeline parallelism (on by default for layer split) two GPUs can then run attention at the same time into the same staging slot. Could you key the staging by device, and either disable tiering when the model spans more than one device or give each tier buffer its own staging? The same sharing applies to two tiered contexts in one process.
  2. With flash attention off, V is stored transposed, so splitting the V buffer by bytes puts the first channels (all cells) in VRAM and the rest in host memory. Results are still correct, but every decode step reads most of V over PCIe and there's no staging on that path. Please add !v_trans to the gate in llama_kv_cache, or say in the help text that it needs -fa on.
  3. The "tiered KV: cells [0, N) in VRAM ..." line is printed before the per-layer checks, so it still shows when tiering is skipped (different K and V cache types, --no-kv-unified with several sequences, MLA, non-CUDA device). Could you log it after the decision, and warn once with the reason when the option is set but not applied?

Smaller:

  • The tier allocator assumes the buffer is exactly K then V of equal size. That's true with the per-layer buffer types, but an assert on it would make the assumption explicit.
  • The graph-capture asserts now accept a buffer from any CUDA device. The set/get_tensor_async asserts keep the device check, so these could too.
  • The per-layer buffer types and the staging handles are never freed, so they leak each time a context is created. Fine for a server, but worth a comment.
  • Have you checked how --fit sizes things with this on? The tier buffer reports its full size as device memory, and the staging VRAM isn't counted anywhere.

This is fine to keep as a default-off, experimental fork feature. Once the staging and gating points are in, I'm happy to take it.

@sb32445

sb32445 commented Nov 26, 2026

Copy link
Copy Markdown

Linux test of this head (7779cc62e, unchanged) on a consumer card, since the HOST_NUMA path could not be tested on Windows: RTX 4070 12 GB (cc 8.9), Linux 6.18, driver 620.73, CUDA 13.5. Bonsai 2 27B PTQ1_0, one slot, plain decode (no speculative drafting), chat endpoint, greedy, 64 tokens.

The tiered buffer is allocated on Linux: ggml_backend_cuda_tier_alloc: ... 6.00 MiB VRAM + 12.00 MiB host for each tier buffer at -c 16384, no fallback warning, no CUDA errors.

Case Decode VRAM used (whole GPU, after loading) Output against all-VRAM
-c 16384, q4_0 K/V, 11,030-token prompt, all VRAM 63.8 t/s 6250 MiB reference
same, --kv-vram-cells 4096, CUDA graphs on 50.1 t/s 6070 MiB identical
same, --kv-vram-cells 4096, GGML_CUDA_DISABLE_GRAPHS=1 48.2 t/s 6056 MiB identical
-c 114688, q4_0 K/V, 57,103-token prompt, all VRAM 55.8 t/s 8074 MiB reference
same, --kv-vram-cells 16384 (about 70 % of the used cells in host memory) 21.0 t/s 6456 MiB identical

test-backend-ops -o FLASH_ATTN_EXT -b CUDA0: 4438/4438 passed. -o MUL_MAT: 2942/2943; the failing case is pq2_0 m=1, n=1 or 2 (4 of 5 reruns fail one such case, ERR 0.0005 to 0.005). This PR does not touch the mat-mul code, and on current prism f70247610 the same filter passes 304/304, so I think it is unrelated.

Three of the review points, checked on this head:

  • Point 2 (-fa off): with f16 K/V (quantized V needs FA) and --kv-vram-cells 4096 at -c 16384, -fa off gives 7.5 t/s against 28.3 t/s with -fa on (all-VRAM -fa off: 53.5 t/s); output identical in both. So the transposed-V path is correct, but about 3.8x slower than the FA path with the same tiering.
  • Point 3 (log line): -ctk q4_0 -ctv q8_0 --kv-vram-cells 4096 prints tiered KV: cells [0, 4096) in VRAM, [4096, 16384) in host memory, but the cache is then the plain CUDA0 KV buffer size = 416.00 MiB and no warning says why.
  • --fit: at -c 262144 it projects 10451 MiB of device memory both without and with --kv-vram-cells 16384; the actual use after loading is 10810 MiB against 6798 MiB. Here nothing changes because it fits either way, but on a tighter card --fit would likely offload layers that would have fit.

Not testable here: more than one GPU (point 1).

The two commits apply unchanged onto current prism f70247610 (git range-diff shows them identical). We have run them (on earlier prism bases, since today on f70247610) together with several follow-up patches from your bonsai-ada-surgery series (tier prefill, prefetch pipeline, draft-tail size), in our local server since 2026-11-19, with contexts up to 258k tokens on this 12 GB card; that combination is not what this PR contains, so it is only a usage note.

(Tests run with Claude Code.)

@sb32445

sb32445 commented Nov 26, 2026

Copy link
Copy Markdown

A correction to the MUL_MAT note in my test above: I wrote that the pq2_0 filter passes 304/304 on plain prism, but that was only 5 runs. With 20 interleaved runs of -o MUL_MAT -p type_a=pq2_0, plain prism (ce40e8137) also fails one pq2_0 m=1 case (n=1 or 2) in 3 of 20 runs. So that failure is flaky on prism as well and still unrelated to this PR.

@professorpalmer

Copy link
Copy Markdown
Author

@bri-prism Thank you for the review, and sorry for the slow answer. We agree with every point, and we are making the changes now:

  1. Staging by device. We will key the staging by device. We will also turn tiering off, with one warning, when the model spans more than one device. Two tiered contexts in one process get separate staging.
  2. v_trans. We will add !v_trans to the gate in llama_kv_cache, so tiering needs flash attention. The help text will say so.
  3. Log line. The "tiered KV" line moves after the per-layer decision. When the option is set but not applied, there is one warning with the reason.

The smaller points go in the same push: the assert on the K-then-V layout, the device check in the graph-capture asserts, and a comment on the lifetime of the buffer types and the staging handles. We will also check --fit with tiering on and report what we see.

We will post here when the new head is up, with our Windows checks.

@sb32445 Thank you for the Linux test of the HOST_NUMA path on 7779cc62e and for the MUL_MAT correction. That path was the one we could not test.

… tail in one VMM range

--kv-vram-cells N (llama_context_params.n_kv_vram_cells) keeps cells [0, N) of each layer's K/V in device
memory and the rest in pinned host memory, mapped into the same device virtual range with CUDA VMM
(cuMemCreate with CU_MEM_LOCATION_TYPE_HOST), so a context can be larger than VRAM. Attention kernels are
unchanged: they only touch host pages once a sequence is that deep. Output is bit-identical to an
all-device cache.

When an attention op's K/V range reaches the host tail, the used host rows are first copied into a VRAM
staging buffer with the copy engine (a second VMM alias maps the same VRAM head pages plus the staging
buffer), and the op reads the all-VRAM alias. Prefill reads the tail once per op instead of once per
query tile; decode moves it by DMA instead of SMs reading host memory from inside the kernel.
GGML_CUDA_KV_TIER_STAGING=0 disables staging.

Bonsai 2 27B on an RTX 4070 12 GB: q8_0 K/V at the full 262,144-token window (9.1 GB of KV), first ~94k
positions in VRAM. Staging: prefill in the tail 146 -> 280 tok/s at 180k, decode +28% at 180k.
Plumbed through llama_cparams to the standard KV cache and the hybrid (attention + recurrent) memory via a
trailing defaulted constructor parameter; other memory types are untouched.
… Windows (the Linux driver rejects CU_MEM_LOCATION_TYPE_HOST for cuMemCreate); fall back to the plain device buffer with a warning when the tiered buffer cannot be allocated; n_kv_vram_cells at the end of llama_context_params (ABI); the cost in the help text
…lash attention or with layers on several devices; log after the decision

- staging buffers: one set per (device, group), one group per KV cache, with a reference count; freed with the last tiered buffer that maps them. ggml_cuda_tier_stage checks the device, and its entry list stays valid while other threads add buffers
- llama_kv_cache: tiering is off when V is transposed (no flash attention) or the offloaded KV layers are on more than one device
- the "tiered KV" line is printed after the per-layer checks and the allocation, with the layer count. One warning gives the reason when n_kv_vram_cells is set and a layer does not use it
- asserts: a tiered buffer is K then V of equal size; the graph-capture asserts check the device again
- memory breakdown (--fit): a tiered buffer counts its VRAM head and the staging buffers as device memory, and its tail as host memory
- help text: the conditions for tiering
@professorpalmer

Copy link
Copy Markdown
Author

@bri-prism The changes are up. The branch is rebased onto prism 43f31f400. The two earlier commits did not change (git range-diff shows them identical); they are now 26890e57e and e1091cfd1. All review fixes are in one new commit, f34c2cfea.

1. Staging by device and by cache

  • The staging buffers now have the key (device, group, host run). llama_kv_cache uses one group for each cache. Thus two tiered contexts in one process have separate staging buffers.
  • Each tiered buffer holds a reference to its staging buffers. The VRAM is released with the last tiered buffer of the cache. Before, the staging stayed for the life of the process.
  • ggml_cuda_tier_stage gets the device of the backend and uses only the ranges of that device.
  • Tiering is off, with one warning, when the offloaded KV layers are on more than one device.
  • One more fault that we found here: the table of tiered ranges was a std::vector, and ggml_cuda_tier_stage kept a pointer into it after it released the mutex. A second context that allocated its cache at that time could move the vector. The table is now a std::list.

You were right about two contexts. The old head fails that check and the new head passes it (see "Two contexts" below).

Cost: each tiered context now has its own staging buffers. Their VRAM is the host tail of one layer (K and V). Example: 126 MiB at -c 131072 with 16384 cells in VRAM (q4_0, the 27B model).

2. v_trans

!v_trans is in the gate. Without flash attention the cache stays in the plain device buffer, and the warning says that tiering needs -fa on. The help text of --kv-vram-cells and the comment in llama.h give the conditions.

3. Log line

The "tiered KV" line is printed after the per-layer checks and after the allocation. It gives the number of layers:

llama_kv_cache: tiered KV: cells [0, 4096) in VRAM, [4096, 16384) in host memory (16 of 16 layers)

When the option is set and a layer does not use it, there is one warning with the reason:

llama_kv_cache: tiered KV: n_kv_vram_cells = 4096 is not applied to 8 of 8 layers: the K and V cache types are different

The reasons are: different K and V cache types, no flash attention, more than one stream (--no-kv-unified with several sequences), MLA, the KV cache not offloaded, layers on more than one device, a layer that is not on a CUDA device with VMM, K and V of different sizes, and a tiered buffer that could not be allocated. The last one was a warning for each layer before. It is now part of the same single warning.

If N is not smaller than the context, the same line is printed at INFO level, because that is not a fault.

Smaller points

  • Asserts: ggml_backend_cuda_tier_alloc asserts that the buffer size divides into the parts with no bytes left over. llama_kv_cache asserts after the allocation that K starts at the base of the buffer, that V starts at one half of the buffer, and that K and V have the same size.
  • The graph-capture asserts check the device again. One helper, ggml_backend_buft_is_cuda_device, is used there and in the four set/get_tensor_async asserts.
  • Lifetime: a comment at ggml_backend_cuda_tier_buffer_type says that the buffer types are never freed (one small context for each new name). The staging buffers are freed now, so the comment says that too.

--fit

What we saw: the tier buffer type belongs to the CUDA device, so --fit counted its full size as device memory, and it did not count the staging VRAM. This agrees with the numbers from @sb32445.

The fix was small, so it is in the same commit. llama_kv_cache::memory_breakdown() now counts the VRAM head of each tiered buffer and the staging buffers as device memory, and the host tail as host memory.

RTX 4070, Bonsai 2 27B PTQ1_0, q4_0 K/V, --kv-vram-cells 16384:

Case all VRAM tiered
llama-fit-params --fit-print on -c 262144: CUDA0 context / host context 4757 / 0 MiB 707 / 4320 MiB
llama-fit-params -c 262144 (no -ngl): result -ngl 61 (5 layers off the GPU) -ngl -1 (no change, all 66 layers)
llama-server -c 131072 -ngl 99: projected device memory 8129 MiB 6239 MiB
the same server: GPU memory used after loading (nvidia-smi, 0 MiB before) 8307 MiB 6447 MiB

The projection is about 200 MiB below the measured use in the two columns, so tiering adds no new error. One limit: with GGML_CUDA_KV_TIER_STAGING=0 the projection still counts the staging buffers. Thus it is too high by the host tail of one layer.

Tests

RTX 4070 (sm_89), CUDA 13, Windows. Each one is a single run.

test-backend-ops -o FLASH_ATTN_EXT -b CUDA0: 4857/4857 passed.

Identity at one depth. Bonsai 2 27B PTQ1_0, a 10,937-token prompt, 64 greedy tokens:

llama-completion -m <model> -ngl 99 -fit off -c 16384 -ctk q4_0 -ctv q4_0 -fa on --temp 0 -s 1 -n 64 -f prompt.txt -no-cnv --no-display-prompt --no-warmup -lv 4 [--kv-vram-cells 4096]
Run Prefill Decode Output against all VRAM
all VRAM 1275 t/s 56.8 t/s reference
--kv-vram-cells 4096, CUDA graphs on (default) 1078 t/s 51.5 t/s identical
--kv-vram-cells 4096, GGML_CUDA_DISABLE_GRAPHS=1 1080 t/s 45.8 t/s identical

0 CUDA errors. Other jobs used the CPU of this PC at the same time, so the speeds are not exact.

Two contexts. This is a small program that is not in the PR (it uses only llama.h). It makes two contexts on one model and decodes a different 6,000-token prompt in each one, from two threads at the same time. Then it compares 32 greedy tokens and the last logits with an all-VRAM reference. Ternary-Bonsai-2-4B-PTQ1_0, n_ctx 16384, 2048 cells in VRAM:

Step Old head (e1091cfd1) New head (f34c2cfea)
two all-VRAM contexts at the same time (control), 3 rounds pass pass
one tiered context pass pass
two tiered contexts at the same time, 3 rounds, q4_0 FAIL: 5 of 6 runs with wrong tokens (13 to 29 of 32), all 6 with different logits (max difference 1.3 to 19.7) pass: 0 tokens differ, logit difference 0
one context freed, the other runs again pass pass
a new context made, then the two at the same time FAIL pass
staging buffers made in the run 2 8 (2 for each tiered context)

The new head also passes the same steps with f16 K/V (2 rounds).

The warnings. Ternary-Bonsai-2-4B-PTQ1_0, llama-completion ... -c 16384 -n 1 --kv-vram-cells 4096, with:

Arguments Result
-ctk q4_0 -ctv q4_0 -fa on tiered, 8 of 8 layers
-ctk q4_0 -ctv q4_0 (-fa auto) tiered, 8 of 8 layers
-ctk q4_0 -ctv q8_0 -fa on warning: the K and V cache types are different
-fa off warning: it needs flash attention (-fa on)
-np 2, and llama-server --no-kv-unified -np 2 warning: it needs a unified KV cache (--kv-unified) or one sequence
-c 8192 --kv-vram-cells 16384 INFO line: the VRAM cells cover the whole cache
GGML_CUDA_DEVICES=2, -ts 1,1 (layers on CUDA0 and CUDA1) warning: the layers are on more than one device; plain buffers on CUDA0 and CUDA1
GGML_CUDA_DEVICES=2, -dev CUDA0 tiered, 8 of 8 layers
-ngl 10 tiered, 3 of 8 layers; warning for the 5 layers that are not on a CUDA device
-nkvo warning: the KV cache is not offloaded

When no layer is tiered, the KV buffers are the plain buffers and there is no "cells [0, N) in VRAM" line.

A CPU-only build of this head compiles and links llama-server and llama-completion. With --kv-vram-cells it prints the warning and runs.

Not tested

  • Two physical GPUs. This PC has one visible GPU. The more-than-one-device case used two virtual devices on the one RTX 4070 (GGML_CUDA_DEVICES=2). That shows the gate and the warning. It does not show two real cards.
  • Linux. The HOST_NUMA path did not change in this commit, but the new commit did not run on Linux.
  • The warnings for an MLA model and for K and V of different sizes (no such model here).
  • The fallback when a tiered buffer cannot be allocated. Its log text changed, and we could not cause the failure here.
  • A Debug build. The graph-capture asserts are assert(), so a Release build does not compile them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants