Skip to content

ggml-agx: Qwen4-35B-A3B Q8_0/Q4_K_M, MTP, vision offload & M4 Max optimizations - #3

Open
astaff wants to merge 6 commits into
agx/ggml-agxfrom
agx/qwen4-m4-max
Open

astaff wants to merge 6 commits into
agx/ggml-agxfrom
agx/qwen4-m4-max

Conversation

@astaff

@astaff astaff commented Dec 5, 2026

Copy link
Copy Markdown
Owner

Summary

Brings full Qwen4-35B-A3B (Q8_0, Q4_K_M, MTP-Q8_0, MTP-Q4_0, and mmproj-Q8_0 vision) support and bare-metal Apple M4 Max (40c AGX3) performance, memory, and correctness upgrades to ggml-agx.

Commits

  1. ad6cdb5 ggml-agx: dequantize Q8_0 attn_{k,v}_conv_proj weights to F32 at model load
  2. ca451c6 qwen4exp: support Qwen4-35B-A3B model, QSA kpool, template, and parser tests
  3. adc9fa5 model: preserve lazy mmap for CPU-only tensors when GPU lacks mmap
    • Keeps mmap and lazy_mode active for CPU-only tensors (per_layer_token_embd.weight) under --load-mode auto --lazy-mode auto, saving ~10.5 GiB anon RAM and ~9.6 GiB swap on Q8_0.
  4. 3561fe2 ggml-agx: add Qwen4 Q8_0/Q4 MoE, GEMM, FA/QSA, GDN, and vision kernels
    • Zero-bloat planar Q4_0_32x64 / Q4_K_PLANAR_32x256 supertiles.
    • High-occupancy Q8_0_8BLK Fused MoE (s5/s6, sequential gate -> up pass eliminating DRAM bank ping-pong, back-to-back scale loads eliminating in-order DRAM scoreboard stalls, 0:0 spills).
    • Zero-barrier paired-stream EPILOGUE=2 (DSV4_HC_PRE) and 2-row/WG K_SPLIT_ROW=2 Q8_0/BF16 GEMV (≤52 GPR32, ActSG=32).
    • Sparse QSA D=256 FlashAttention MMA (fixing shared-memory sh_kv WAR race + FP16 clamp), general tiled Coopmat MMA FA (d=1..128 for mmproj), and WY-chunked GDN.
  5. 9d039f4 ggml-agx: optimize Qwen4 fusions, vision offload, batched sampler, and hazards
    • 100% graph fusion hit rates across M=1..4 (hcpn=80/80, rtrg=40/40, rgrp=40/40, shdn=40/40, mres=40/40) via rotating 128 KiB rtr_io_bo, GDN rs_replay absorption (-120 server dispatches), and multi-WG parallel ARGMAX/TOP_K.
    • Offloads mmproj IM2COL, UPSCALE, ROPE(VISION, d=72), and FLASH_ATTN_EXT(d=72) to AGX0 (-3.5–4.0 GiB anon RAM, 2.77× faster vision prefill).
    • 1-submit batched GPU sampler (agx_sample_batch_logits) and async D2D copy (agx_copy_tensor_bytes_async).
    • Respects GGML_TENSOR_FLAG_COMPUTE to skip unselected ggml_build_forward_select branches, bounds-checks SET_ROWS, and raises CDM hazard-tracker caps (1024/2048).
  6. e46a94b speculative, server: add GPU-resident MTP, adaptive/batched draft-mtp, and AGX sampler
    • GPU-resident h_nextn_dev handoff, single-submit fused Inject+Draft MTP (moving commit_pending_row off the sampling critical path), adaptive and batched 2x/4x draft-mtp, and batched GPU sampling in llama-server.

Verification & Benchmarks (Apple M4 Max 40c AGX3, NixOS bare-metal)

Correctness & Stability

  • Build: 0 errors, 0 warnings (llama-server, llama-bench, llama-cli, test-backend-ops).
  • test-backend-ops: 100% PASS across MUL_MAT (849/849), MUL_MAT_ID (731/731), FLASH_ATTN_EXT (2,408/2,408), TOP_K (476/476), ROPE (176/176), IM2COL (113/113), GATED_DELTA_NET (89/89), SET_ROWS (55/55), UPSCALE (24/24), DSV4_HC_POST (11/11), ARGMAX (7/7), DSV4_HC_PRE (6/6).
  • Borealis SHADERDB: 0:0 spills across all modified/new shaders.
  • Hazard check (GGML_AGX_HAZARD_CHECK=1): 0 hazards on long-prompt (pp > 2048) QSA + draft-mtp.
  • Full SpecBench Sweep (Q8_0 + MTP-Q8_0 + mmproj-Q8_0): 210/210 OK (0 HTTP 500s, 0 retries).

Performance (Qwen4-35B-A3B-GGUF:Q8_0, MTP-Q8_0, mmproj-Q8_0)

  • llama-bench tg128: 85.96 ± 0.15 tok/s (M=1 REPLAY-M8 GPU 11.27 ms, prefill pp512–4096 = 1,304–1,324 tok/s).
  • SpecBench (Coding, Multimodal, Knowledge Work, mean decode tok/s):
dmax Mode Coding Multimodal Knowledge Work
0 baseline 83.93 84.03 82.58
1 peak 105.93 (1.26×) 108.13 (1.29×) 106.58 (1.29×)
2 fixed 101.84 (1.21×) 106.45 (1.27×) 105.22 (1.27×)
3 fixed 88.05 (1.05×) 95.01 (1.13×) 92.07 (1.11×)
4 fixed 75.73 (0.90×) 81.33 (0.97×) 81.62 (0.99×)
6 fixed 64.35 (0.77×) 68.58 (0.82×) 67.91 (0.82×)
6 adaptive 100.51 (1.20×) 106.04 (1.26×) 103.55 (1.25×)
  • Memory footprint (128K context, -c 131072 -ctk q8_0 -ctv q8_0 --cache-ram 0): RssAnon = 279 MiB (-13.45 GiB vs pre-optimization), GPU user_committed = 43.2 GiB.

…l load

ggml-org NVFP4 Inkling-2 GGUFs store full-attention attn_{k,v}_conv_proj.weight
as Q8_0 (wtype > 2), which trips the megakernel eligibility guard in
agx-inkling2-target.cpp and forces the 1,800-dispatch fallback (~58 tok/s).

Unpack (via agx_repack_get_tensor when repacked) and losslessly dequantize
Q8_0 attn_{k,v}_conv_proj.weight tensors to F32 (0-ULP bit-exact via ggml
Q8_0 to_float, 2.25 MiB for 4 full-attention layers) into a single readonly
SLC-stream BO at model bind time, freeing it on rebind and engine destroy.
Pass the dequantized F32 VAs (AGX_WTYPE_F32) into both the Wave 61 MMA tail
and fallback attn_in dispatches so Inkling-2 hits the 48-pass megakernel.
Keep mmap and lazy_mode active for CPU-only tensors (such as Qwen4 per_layer_token_embd.weight) under --load-mode auto --lazy-mode auto even when the GPU device reports mmap_support == false, saving ~10.5 GiB anon RAM and ~9.6 GiB swap on Qwen4-35B-A3B Q8_0.
Add and optimize AGX3 compute kernels for Qwen4-35B-A3B Q8_0 and Q4_K_M:
- Zero-bloat planar Q4_0_32x64 and Q4_K_PLANAR_32x256 supertile dequantization
- High-occupancy Q8_0_8BLK Fused MoE (s5/s6, sequential gate->up pass, zero DRAM scale stalls, 0:0 spills)
- Paired-stream zero-barrier DSV4_HC_PRE (EPILOGUE=2) and 2-row/WG K_SPLIT_ROW=2 Q8_0/BF16 GEMV
- Sparse QSA D=256 FlashAttention MMA (with sh_kv WAR barrier and FP16 clamp), general tiled Coopmat MMA FA (d=1..128 for vision mmproj), and WY-chunked GDN.
…d hazards

- Planar Q4_0/Q4_K repacking, Split-K M8_DIRECT, and Q8_0/BF16 GEMM/MoE dispatch tuning
- 100% graph fusion hit rates across M=1..4 (hcpn, rtrg, rgrp, shdn, mres) via rotating rtr_io_bo, GDN rs_replay absorption (-120 server dispatches), multi-WG parallel ARGMAX/TOP_K, and zero-spill hc_post_norm/gdn_s128/shexp shaders
- Offload mmproj IM2COL, UPSCALE, ROPE(VISION, d=72), and FLASH_ATTN_EXT(d=72) to AGX0 (-3.5-4.0 GiB anon RAM, 2.77x faster vision prefill)
- 1-submit batched GPU sampler (agx_sample_batch_logits) and async D2D copy (agx_copy_tensor_bytes_async)
- Respect GGML_TENSOR_FLAG_COMPUTE to skip unselected forward_select branches, bounds-check SET_ROWS, and raise CDM hazard-tracker caps (1024/2048).
…, and AGX sampler

- GPU-resident h_nextn_dev handoff and single-submit fused Inject+Draft MTP (moving commit_pending_row off the sampling critical path)
- Adaptive draft-mtp and batched 2x/4x MTP support in llama-server
- Batched GPU sampling integration and small_graph_cache MTP key tracking in llama_context.
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.

1 participant