跳到主内容
@wquguru
精选90r/LocalLLaMA(Reddit)技巧与观点

AMD RX 9070 XT运行Qwen2.5-27B满上下文优化实践

Qwen 27B (IQ4_XS, Q8/Q4 KV) at full 262k context on a 16GB RX 9070 XT: 80+ t/s decode with Adaptive KV Streaming

原文
发到 X
推荐理由

本地部署AMD显卡跑大模型的同学必看,这篇详细拆解了如何用自适应KV流和内核融合在16GB显存上榨干27B模型的262k上下文,参数和配置直接可抄。

I have been working on implementing adaptive KV streaming and RDNA 4 / ROCm optimizations for llama.cpp to enable running Qwen3.8-27B (IQ4_XS weights, Q8_0/Q4_0 KV cache) with its full 262,144-token context window on a single 16 GB RX 9070 XT GPU at 80+ t/s decode.

我一直在为 llama.cpp 实现自适应 KV 流式传输和 RDNA 4 / ROCm 优化,以便在单张 16 GB RX 9070 XT GPU 上以 80+ t/s 的解码速度运行 Qwen3.8-27B(IQ4_XS 权重,Q8_0/Q4_0 KV 缓存),并支持其完整的 262,144-token 上下文窗口。

Qwen 27B IQ4_XS is ~14.3 GB, leaving insufficient VRAM headroom on a 16 GB card. The ASCII-condensed version trims non-ASCII vocabulary down to ~13.6 GB (12.6 GiB), leaving ~3.4 GiB of usable VRAM. Even with a Q8_0/Q4_0 KV cache, 262k context needs ~7.4 GiB for KV tensors alone, triggering OOM past ~65k tokens on llama.cpp.

Qwen 27B IQ4_XS 模型大小约为 14.3 GB,在 16 GB 显卡上留下的 VRAM 余量不足。ASCII 精简版将非 ASCII 词表缩减至约 13.6 GB(12.6 GiB),留下约 3.4 GiB 可用 VRAM。即使使用 Q8_0/Q4_0 KV 缓存,262k 上下文仅 KV 张量就需要约 7.4 GiB,导致在 llama.cpp 中超过约 65k token 时触发 OOM(内存溢出)。

I came across RaymondHuang210129/llama.cpp-adaptive-kv-streaming, which introduced adaptive KV streaming for NVIDIA/CUDA but did not support AMD/ROCm. I ported this to ROCm/HIP, retaining the full 262k cache in host RAM (~7.4 GB) while asynchronously streaming bounded working sets (~450 MiB prefill, ~2,100 MiB decode) over PCIe SDMA alongside compute.

我发现了 RaymondHuang210129/llama.cpp-adaptive-kv-streaming,它引入了针对 NVIDIA/CUDA 的自适应 KV 流式传输,但不支持 AMD/ROCm。我将其移植到 ROCm/HIP,在主机 RAM 中保留完整的 262k 缓存(约 7.4 GB),同时通过 PCIe SDMA 与计算并行异步流式传输有界工作集(预填充约 450 MiB,解码约 2,100 MiB)。

I also incorporated kernel optimizations from stew675/llama-cpp-rdna-boosts (4-row multi-column mmvq, 16-byte vectorized loads for Q4_K/Q5_K, SwiGLU fusions, and single-queue async HIP graphs).

我还整合了 stew675/llama-cpp-rdna-boosts 中的内核优化(4行多列 mmvq、Q4_K/Q5_K 的 16 字节向量化加载、SwiGLU 融合以及单队列异步 HIP 图)。

Further improvements:

进一步的改进:

  • Sibling activation reuse: Reuses quantized activations across parallel projections (gate/up and Q/K/V) to avoid redundant memory passes.
  • In-register fused residual add + RMSNorm: Retains activations in VGPRs between passes and directly writes quantized q8_1 copies for mmvq.
  • Multi-column SwiGLU & GDN fusions: Extends fused Gate/Up + SwiGLU to 4-column verify batches in mmvq and fuses recurrent linear-attention gates for hybrid layers.
  • Online Log-Sum-Exp prefill & pipelined prefetch: Computes chunked prefill across streamed pages via online LSE accumulation and overlaps layer L+1 PCIe transfers with layer L compute.
  • Persistent graph arenas: Reuses arenas across prefill/decode phase switches to eliminate teardown overhead.
  • Stream-optimized speculative decoding: Merged single-head MTP verify catch-up into the first draft step (cutting MTP forward passes in half and preventing draft KV pollution), dedicated arenas for verify graphs to eliminate rebuild bubbles, extended paged decode attention to verify batches, and stacked opportunistic n-grams with probabilistic rejection sampling.
  • 兄弟激活复用:在平行投影(gate/up 和 Q/K/V)之间复用量化激活值,避免冗余的内存访问。
  • 寄存器内融合的残差相加 + RMSNorm:在传递之间将激活值保留在 VGPR 中,并直接写入用于 mmvq 的量化 q8_1 副本。
  • 多列 SwiGLU 和 GDN 融合:将融合的 Gate/Up + SwiGLU 扩展到 mmvq 中的 4 列验证批次,并为混合层融合循环线性注意力门控。
  • 在线 Log-Sum-Exp 预填充和流水线预取:通过在线 LSE 累积在流式页面上计算分块预填充,并将第 L+1 层的 PCIe 传输与第 L 层的计算重叠。
  • 持久化图区域:在预填充/解码阶段切换之间复用区域,以消除拆卸开销。
  • 流优化的投机解码:将单头 MTP 验证追赶合并到第一个草稿步骤中(将 MTP 前向传播次数减半并防止草稿 KV 污染),为验证图分配专用区域以消除重建气泡,将分页解码注意力扩展到验证批次,并结合概率拒绝采样堆叠机会性 n-gram。

Benchmark performance:

基准性能:

  • Decode: 36.5 t/s (1k) -> 32.1 t/s (65k) -> 27.0 t/s (131k) -> 9.5 t/s (262k).
  • Decode (with MTP): 71.8-86.5 t/s (1k-4k) -> 72.9 t/s (65k) -> 78.6 t/s (131k) -> 31.1 t/s (262k).
  • Prefill: 1368.4 t/s (1k) -> 1120.3 t/s (65k) -> 863.0 t/s (131k) -> 585.8 t/s (262k).
  • 解码:36.5 t/s (1k) -> 32.1 t/s (65k) -> 27.0 t/s (131k) -> 9.5 t/s (262k)。
  • 解码(带 MTP):71.8-86.5 t/s (1k-4k) -> 72.9 t/s (65k) -> 78.6 t/s (131k) -> 31.1 t/s (262k)。
  • 预填充:1368.4 t/s (1k) -> 1120.3 t/s (65k) -> 863.0 t/s (131k) -> 585.8 t/s (262k)。

Operating the 9070 XT at a 231W limit (-24% relative to 304W default TDP) incurs only a 3.6% prefill latency penalty and delivers virtually identical decode throughput, saving power and keeping the card quiet during long runs.

将 9070 XT 的运行功耗限制在 231W(相对于默认 TDP 304W 降低 24%),仅带来 3.6% 的预填充延迟惩罚,同时提供几乎相同的解码吞吐量,从而节省功耗并在长时间运行期间保持显卡安静。

Take a look at the repository for the full details: https://github.com/achyudh/llama.cpp/tree/feature/adaptive-kv-stream-rocm

查看仓库以获取完整详情:https://github.com/achyudh/llama.cpp/tree/feature/adaptive-kv-stream-rocm

更进一步:量化金融体系

看懂新闻只是起点——沿量化金融路径,把它变成能交付的工程能力

进入量化体系 →

相似阅读

关联信息,但可能不是同一事件