Skip to content

fix(vulkan): reject small HOST_VISIBLE aperture for moe-cache on discrete AMD - #401

Merged
TheTom merged 2 commits into
TheTom:feature/turboquant-kv-cachefrom
gaigher:patch-1
Nov 22, 2026
Merged

TheTom merged 2 commits into
TheTom:feature/turboquant-kv-cachefrom
gaigher:patch-1

Conversation

@gaigher

@gaigher gaigher commented Oct 17, 2026 •

Copy link
Copy Markdown

Summary

  • Discrete AMD (Polaris, etc.) often exposes a small DEVICE_LOCAL|HOST_VISIBLE|HOST_COHERENT heap (~256 MiB).
  • Vulkan moe-cache treated that as UMA and allocated the expert slab there → failed to allocate ~508 MiB even with free VRAM.
  • Only enable host-mapped when that heap size ≥ configured budget; otherwise use device-local + staging.
  • Override: GGML_VK_MOE_CACHE_FORCE_DEVICE_LOCAL=1.

Evidence (6× RX 580 8GB, GPT-OSS 120B Q4_K_M, profile S-23)

Before: host-mapped → fail_alloc, pools=0
After: UMA heap=256 MiB < need=512 MiB; using device-local + staging → pool 508 MiB, fail_alloc=0, hits≈49%

Performance note (correctness-only PR)

Local A/B @8192 ×3 on Polaris 8GB: --moe-cache off gen ~3352 tok/s vs --moe-cache 512 gen ~369 tok/s (~9× slower; pool thrashing). This PR fixes allocation correctness; it does not claim a production perf win on discrete Polaris — keep --moe-cache off until a better cache strategy (VRAM headroom, budget, HOT_USES).

Test plan

  • Rebuild with -DGGML_VULKAN=ON
  • --moe-cache 512 on discrete AMD: log shows device-local + staging, session ready, pool[...], no fail_alloc
  • True UMA / large aperture: still host-mapped when heap ≥ budget
    (non testable ici — 6× Polaris discrets seulement ; à valider par un reviewer iGPU/ReBAR)
  • GGML_VK_MOE_CACHE_FORCE_DEVICE_LOCAL=1 forces device-local
  • tok/s A/B: --moe-cache off vs 512 on same -ot (regression documented above)

Version Française

Résumé

  • Les GPU AMD discrets (Polaris, etc.) exposent souvent un petit tas DEVICE_LOCAL|HOST_VISIBLE|HOST_COHERENT (~256 MiB).
  • Le moe-cache Vulkan le traitait comme de l’UMA et y allouait la dalle d’experts → failed to allocate ~508 MiB malgré de la VRAM libre.
  • N’activer le host-mapped que si la taille de ce tas ≥ budget configuré ; sinon device-local + staging.
  • Override : GGML_VK_MOE_CACHE_FORCE_DEVICE_LOCAL=1.

Preuves (6× RX 580 8 Go, GPT-OSS 120B Q4_K_M, profil S-23)

Avant : host-mapped → fail_alloc, pools=0
Après : UMA heap=256 MiB < need=512 MiB; using device-local + staging → pool 508 MiB, fail_alloc=0, hits≈49 %

Note perf (PR correctness only)

A/B local @8192 ×3 sur Polaris 8 Go : --moe-cache off gen ~3352 tok/s vs --moe-cache 512 gen ~369 tok/s (~9× plus lent ; thrashing de pool). Cette PR corrige l’allocation ; elle ne revendique pas un gain perf en prod sur Polaris discret — garder --moe-cache off jusqu’à une meilleure stratégie (marge VRAM, budget, HOT_USES).

Plan de test

  • Rebuild avec -DGGML_VULKAN=ON
  • --moe-cache 512 sur AMD discret : log device-local + staging, session ready, pool[...], pas de fail_alloc
  • Vrai UMA / grande aperture : toujours host-mapped si tas ≥ budget
    (non testable ici — 6× Polaris discrets seulement ; à valider par un reviewer iGPU/ReBAR)
  • GGML_VK_MOE_CACHE_FORCE_DEVICE_LOCAL=1 force device-local
  • A/B tok/s : --moe-cache off vs 512 sur le même -ot (régression documentée ci-dessus)

@TheTom

TheTom commented Oct 22, 2026

Copy link
Copy Markdown
Owner

The detection reads right. Taking the largest DEVICE_LOCAL|HOST_VISIBLE|HOST_COHERENT heap and comparing it to the budget is the correct test for a small BAR aperture, and the force override is a good escape hatch.

One blocker. The diff also deletes the queue lock in vk_submit_and_wait (ggml_backend_vk_lock_queue / unlock) and the vk_backend member that feeds it. That has nothing to do with the aperture check. The lock is there because Vulkan needs host access to a VkQueue externally synchronized, vkQueueWaitIdle included, and the backend can hand several contexts the same queue. Without it, cache submissions can race with normal graph submissions. Your branch sits on ca28b4bc0, so this isn't a stale base, the commit itself removes it. Was that on purpose, or did a rebase eat it? Please put it back and keep this PR to the aperture change.

Smaller:

  • Your A/B shows --moe-cache 512 at 369 tok/s against 3352 with it off. I'm fine taking a correctness fix, but when it lands on the device-local + staging path on a discrete card the log should say that path is slow there, or the default should stay off for that config.
  • Only the A/B box in the test plan is ticked. The case I care about is true UMA still going host-mapped. Do you have an iGPU or a resizable-BAR card to confirm the log says host-mapped UMA heap=...?
  • budget_mb == 0 falls back to requiring 1 GiB. Is that reachable (auto budget not resolved at session create)? If so, what happens on a UMA part where the resolved budget ends up bigger than 1 GiB?

@gaigher

gaigher commented Oct 22, 2026

Copy link
Copy Markdown
Author

Sorry about the noise — the queue-lock drop was accidental (full-file web edit against a local tree missing the newer lock), not intentional. Restored in the follow-up; aperture logic unchanged.

Thanks — answering the smaller questions from what we can test here:

1. Slow discrete path / default off
Agreed. On this farm (6× RX 580/570, S-23, @8192 ×3): --moe-cache off gen ~3352 tok/s vs --moe-cache 512 ~369 tok/s (~9×). We already run prod with --moe-cache off. The follow-up also logs on the device-local + staging / aperture path that this path is slow on discrete and that --moe-cache off is preferred.

2. True UMA / ReBAR host-mapped log
I don’t have that hardware. This box is 6× discrete Polaris only (plus an ASPEED BMC display adapter — not a usable Vulkan iGPU). No resizable BAR. The log we do have is the discrete/aperture case: UMA heap=256 MiB < need=512 MiB; using device-local + staging. I can’t tick the true-UMA host-mapped UMA heap=... box myself — needs a reviewer with iGPU or ReBAR.

3. budget_mb == 0 → 1 GiB fallback
Yes, reachable. ggml_backend_sched_set_moe_cache always calls session_create(..., &config) with a non-null config. For --moe-cache on/auto without an explicit MiB, query_config leaves budget_bytes=0, so the early !supplied_config && budget_mb==0 bail does not fire, and the 1 GiB threshold decides host_mapped at session create. The real auto budget (free-minus-reserve) is claimed later; host_mapped is not recomputed. So if a UMA heap is e.g. 1.2 GiB (host-mapped chosen) and the later claim is 2048 MiB, host-mapped alloc can fail. Happy to tighten that in a follow-up (defer until budget is known, or re-check at pool create) if you want it in this PR or a separate one.

Follow-up commit on this PR: vk_backend + queue lock restored, aperture check unchanged, slow-path log added.

@gaigher

gaigher commented Oct 24, 2026 •

Copy link
Copy Markdown
Author

Follow-up pushed: budget claim + refuse staging for on/auto + thrash guard; queue lock unchanged.

Agreed — these are validation axes, not bugs shown by the diff. Short answers from the Polaris farm + current code:

  1. Budget shared across sessions
    Vulkan vk_moe_prepare_budget is intentionally simpler than CUDA’s multi-participant g_physical_budgets coordinator: it snapshots ggml_backend_vk_get_device_memory once per device (budget_ready) and caps by explicit MiB / reserve. Today the scheduler keeps one moe-cache session on the first Vulkan backend (Vulkan0). Two concurrent sessions claiming the same free snapshot without a shared ledger could over-commit — that path isn’t exercised in our single-server llama-server setup. Happy to port CUDA-style physical budget participants in a follow-up if multi-session Vulkan becomes a real use case; not claiming that here.

  2. Thrash guard “sticky for the session”
    Yes, by design and conservative: once staging thrash is clear (full pool + evictions ≥ 80% of fills after 256 fills), further H→D fills were measured at ~tens of GiB / minutes of WaitIdle on Polaris. Hits-only for the rest of that process is safer than adapting into more thrash. If the expert mix shifts later in a long-lived process, a restart (or a future decay/reset of staging_thrash_stopped) would be needed — open to a time-windowed reset if you want that before merge.

  3. Auto / UMA with effective budget > 1 GiB
    session_create still uses the provisional 1 GiB floor when budget_mb==0, then prepare_budget + revalidate_host_mapped re-check against the granted budget. On true UMA (heap ≥ granted), we keep host-mapped; we only refuse when the path would be device-local+staging for on/auto. We still cannot run that UMA case here (discrete Polaris only) — same ask as before for an iGPU/ReBAR reviewer.

  4. Perf after the change
    Measured on this farm (S-23, -t 8, @8192):

off: ~3.9 tok/s gen (prod ref)

on after T6: pools=0, refuse staging, gen ~3.8 (≈ off)

512 opt-in: still not a prod win; thrash guard cuts staging from ~88 GiB to ~2.5 GiB and recovers gen ~3.6 once it trips

This follow-up is safety / correctness of defaults, not a tok/s win claim for staging-on-Polaris. Prod remains --moe-cache off.

Happy to tighten (1) or (2) in-tree if you prefer before merge; (3) needs hardware we don’t have; (4) already logged above.

// Resolve free-minus-reserve (or explicit MiB cap) before GPU setup /
// pool create. Previously budget_mb==0 bailed here, so --moe-cache on/auto
// never activated on Vulkan.
if (!vk_moe_prepare_budget(*session, dev)) {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

This changes the default. --moe-cache defaults to auto, and vk_moe_query_config returns enabled with budget_mb=0. Before, this function returned here for that case, so auto never did anything on Vulkan. Now vk_moe_prepare_budget turns the cache on for any device whose host-visible heap fits free minus reserve, which covers iGPUs and ReBAR cards. Nobody has run that path. Please keep auto inert on Vulkan in this PR (return when budget_mb == 0 unless ALLOW_STAGING is set) and enable auto in a separate PR with a UMA or ReBAR run.


vk_moe_revalidate_host_mapped(dev, budget_bytes);

size_t slots = budget_bytes / expert_size;

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Every pool is sized from the full budget_bytes (dev.budget_limit). Before, that was the explicit MiB cap. With auto it is free minus reserve, so a second pool (another expert size or wtype) claims the same memory again. Size from dev.budget_limit - dev.allocated_bytes like the CUDA path does (remaining = slab_limit - allocated_bytes in moe-cache.cu), and stop when it drops below moe_cache_pool_slots_min slots.

return nullptr;
}

vk_moe_revalidate_host_mapped(dev, budget_bytes);

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

This call can't change anything today because budget_bytes is dev.budget_limit, which vk_moe_prepare_budget already checked. If it ever did flip host_mapped to false after a host-mapped pool exists, vk_moe_plan would take the staging branch and vkCmdCopyBuffer into a slab created without TRANSFER_DST. Please drop it and decide host_mapped once, in vk_moe_prepare_budget.

// than they save. Serve hits only for the rest of the session.
if (!dev.host_mapped && !dev.staging_thrash_stopped &&
pool.free_slots.empty() &&
dev.fills >= 256 &&

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

The thrash guard and the staging counters are new cache policy, not part of the aperture fix. dev.fills and dev.evictions are device-wide while pool.free_slots is only the current pool, and once it trips it stays off for the life of the process. Please move this to its own PR with the Polaris numbers so it can be reviewed by itself.

@TheTom

TheTom commented Nov 11, 2026

Copy link
Copy Markdown
Owner

Thanks @gaigher for the aperture check. The later commits grew past it, so my comments are about splitting those out.

Discrete AMD exposes a tiny DEVICE_LOCAL|HOST_VISIBLE heap that was
misdetected as UMA, so expert slab alloc failed despite free VRAM.
Prefer device-local + staging when UMA heap < budget; allow FORCE_DEVICE_LOCAL.
Restore vk_backend + ggml_backend_vk_lock_queue/unlock accidentally
dropped in a73fd8a. Aperture/UMA heap check unchanged. Log notes that
device-local+staging is slow on discrete; prefer --moe-cache off.
@gaigher

gaigher commented Nov 11, 2026 •

Copy link
Copy Markdown
Author

Sorry for the delay getting back to this, and thanks for the clear split guidance — you’re right that the later commits grew past the aperture check.

Plan

  1. This PR (fix(vulkan): reject small HOST_VISIBLE aperture for moe-cache on discrete AMD #401) — aperture-only again, rebased on current feature/turboquant-kv-cache tip:
    • reject small DEVICE_LOCAL|HOST_VISIBLE heap when < budget
    • fall back to device-local + staging
    • GGML_VK_MOE_CACHE_FORCE_DEVICE_LOCAL=1
    • queue lock kept (vk_backend + lock_queue / unlock)
    • log notes staging is slow on discrete → prefer --moe-cache off
  2. Follow-up PR — thrash guard + staging counters (with the Polaris numbers)
  3. Follow-up PR — Vulkan auto / budget_mb==0 activation + remaining-budget pool sizing (needs UMA/ReBAR validation for true host-mapped; not testable on this 6× Polaris farm)

Re-validated on 6× Polaris before the force-push: --moe-cache 512 logs UMA heap=256 MiB < need=512 MiB; … device-local + staging, session ready, pool[…], fail_alloc=0.

Force-pushed the slimmed branch (patch-1 @ a368dc6). auto stays inert on Vulkan in this PR.

@TheTom
TheTom merged commit f51574f into TheTom:feature/turboquant-kv-cache Nov 22, 2026
superbudvar added a commit to superbudvar/llama-cpp-turboquant that referenced this pull request Nov 22, 2026
…heTom#417, TheTom#371, TheTom#401, TheTom#419)

# Conflicts:
#	AGENTS.md
#	docs/KV-cache-quantization.md
#	tests/CMakeLists.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants