Skip to content

Deepseek MLA Optimizations V2#195

Merged
ikawrakow merged 4 commits into
ik/mlafrom
s6/mla
Feb 9, 2025
Merged

Deepseek MLA Optimizations V2#195
ikawrakow merged 4 commits into
ik/mlafrom
s6/mla

Conversation

@saood06

@saood06 saood06 commented Feb 8, 2025

Copy link
Copy Markdown
Collaborator

@ikawrakow

This PR contains the following things

  • A fairydreaming commit that is supposed to increase PP
  • Avoid allocating the MHA KV cache in MLA mode
  • Adds a change I originally missed that is used for gguf-py.

I will follow up with:

  • Having all the MoE experts load during warmup, that can be placed in this PR if you want, or a separate one. It is a very large QoL feature for large MoE. Without it the model is slowly loaded in on use, with it, the model is loaded immediately and at a faster rate.
  • The mmap based KV cache buffer, it is functional but I have yet to make it a CLI option.

@saood06 saood06 mentioned this pull request Feb 8, 2025
@ikawrakow ikawrakow self-requested a review February 9, 2025 07:33

@ikawrakow ikawrakow left a comment

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.

Looks good. I added a minor change to check if wk_b and wv_b are available before turning on MLA (so we don't crash if someone is using an old model and asked for MLA).

PP-4096 for Q8_0_R8 quantized DeepSeek-Lite with -mla goes up to 292 t/s from 275 t/s with this change.

@ikawrakow ikawrakow merged commit d58dee8 into ik/mla Feb 9, 2025
ikawrakow added a commit that referenced this pull request Feb 9, 2025
* Deepseek MLA Optimizations

Co-authored-by: Stanisław Szymczyk <sszymczy@gmail.com>

* Make MLA optional

* Remove some unnecessary copies in the MLA attention

* Deepseek MLA Optimizations V2 (#195)

* Avoid allocating MHA KV cache when MLA is turned on

* Added missing gguf-py file

* Added final optimizations

Co-authored-by: Stanisław Szymczyk <sszymczy@gmail.com>

* Make sure we do have wk_b and wv_b before enabling MLA

---------

Co-authored-by: Stanisław Szymczyk <sszymczy@gmail.com>
Co-authored-by: Iwan Kawrakow <iwan.kawrakow@gmail.com>

* Use type_k and type_v to set the types of the MLA caches

They were hard-coded at f16.
On my Ryzen-7950X with native bf16 support I get a fairly
significant PP performance boost with bf16 KV-cache:
PP-4096 = 320 t/s up from 292 t/s with fp16 KV-cache.

* Better gemm strategy when nth > nhead

It gives a ~10% PP performance boost for DeepSeek-Lite with 32 threads
(with or without MLA).
Before this commit, when nth > nhead heads were processed
sequentially with all nth threads participating in each
matrix multiplication. Now we ind the gcd of nhead and
nth and split threads into nth/gcd groups, each group
processing nhead/gcd heads.

---------

Co-authored-by: Saood Karim <saood05@gmail.com>
Co-authored-by: Stanisław Szymczyk <sszymczy@gmail.com>
Co-authored-by: Iwan Kawrakow <iwan.kawrakow@gmail.com>
slartibardfast pushed a commit to slartibardfast/ik_llama.cpp that referenced this pull request Jun 9, 2026
…0 GEMV (v0) + gate

Implements the small-batch (ne11<=K) read-once GEMV carve-out, byte-identical to
production MMQ mul_mat_q_split_k<Q4_0,...,split_k=4>. v0 is one thread per output
element (correctness-first); the float-accumulation order reproduces MMQ's exactly:
4 contiguous ascending split-K chunks (boundaries nb*s/4 aligned to blocks_per_iter=8,
tail dropped), one (float(dot32)*dA)*dB add per 32-block (NOT per-8-group — MMQ's k01
loop steps per-block for Q4_0 since dA,dB are one scale per 32), combined
p0+((p1+p2)+p3) via the split-K fixup. Default-off (env GGML_CUDA_M1_GEMV); wired in
ggml_cuda_op_mul_mat_q behind a runtime toggle + carve-out threshold K (default 8).

test-m1-gemv-byte-identity: A/B's MMQ vs GEMV through the identical dispatcher,
asserts max_abs_diff=0.0 per column across ne11 in {1,2,3,4,8} x K in
{1024,1280,5120,17408} (all real Q4_0 K-dims). ALL OK.

Finding (pre-existing MMQ bug, noted not fixed per CLAUDE.md 3): MMQ split_k=4
leaves dst uninitialized for K<1024 (nb<32) — slice-0 returns early without writing
dst when the first K-quarter rounds to empty after blocks_per_iter alignment, then the
fixup adds onto garbage (MMQ returns +/-3e38/inf at K=256/288/768; the GEMV stays
finite). All production K are multiples of 256 and >=5120, outside the buggy domain.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
slartibardfast pushed a commit to slartibardfast/ik_llama.cpp that referenced this pull request Jun 9, 2026
…sced read, ordered fold) — byte-identical, 16.7 t/s

Still byte-identical (FMA-contraction fix: shuffle X=dot*dA and dB separately, fold
p+=X*dB so it contracts to fma(X,dB,p) matching MMQ; materializing X*dB before the add
diverged ~1 ULP). Perf 16.71 vs MMQ 27.09 tg128 — the ordered fold on lane 0 serializes
the warp (31 lanes idle). Checkpoint before v2 (lane-per-row parallel folds + SMEM-staged
coalesced load), which removes the serial-fold bottleneck.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
slartibardfast pushed a commit to slartibardfast/ik_llama.cpp that referenced this pull request Jun 9, 2026
…staged, parallel folds) — byte-identical, 7.1 t/s

v2 removes v1's serial-fold bottleneck (each lane folds its own row in parallel) and
stages weights through SMEM for coalesced global reads, but is SLOWER (7.1 vs MMQ 27.1):
lane-strided 18-byte SMEM block reads cause heavy bank conflicts, the per-row cooperative
load is partly serial, and 18KB/block SMEM caps occupancy. Gate now also requires
ne00%256==0 (the tiled load needs nb%8==0).

Three byte-identical designs measured, all < MMQ (v0 9.4 / v1 16.7 / v2 7.1 vs 27.1).
Strong evidence the byte-identity constraint (MMQ's exact serial reduction order) forbids
the lean warp-reduce that gave stock mmvq its +27% (ikawrakow#194). Decode-GEMV A-vs-B fork
re-opens with data. Byte-identity itself remains PROVEN+correct (test ALL OK).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
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