ik_llama_opt/common
David Young c07a052315
MLA tensor parallelism under -sm graph (DEEPSEEK2/GLM_DSA/MISTRAL4) (#1821)
* MLA tensor parallelism under -sm graph (DEEPSEEK2/GLM_DSA/MISTRAL4)

Extends -sm graph (split-mode graph) to MLA-style attention across the
DEEPSEEK2, GLM_DSA, and MISTRAL4 architectures. Previously these archs
fell back to -sm layer regardless of the user's flag.

Implementation:
- Per-rank attention build in build_deepseek2_tp_attention with
  view-sliced FlashAttention, split-buffer output projection, and
  ggml_reduce across devices
- wk_b / wv_b absorbed weights replicated per device via materialize()
  in llm_prepare_mla (these can't live in a split buffer)
- KV cache replication path (replicated_k_l) for graph-mode TP
- distribute_mla_tensors_for_split_mode_graph routes attention/norm
  tensors into ctx_split; expert tensors stay per-layer
- Implements ggml_backend_cuda_split_buffer_get_tensor for the
  replicated / row-split / col-split inverse paths
- Early-reject guard in src/llama.cpp that auto-downgrades -sm graph
  to -sm layer (with a warning) when incompatible loader flags are set:
  -ncmoe, -cmoe, -ot, -rtr, -muge

New CLI flag:
- -gap | --graph-attn-precision <f16|f32>  (default f16)

See the PR description for the full validation matrix (3 archs x 2/4/8
GPU counts), perf numbers, VRAM accounting, and known limitations.

* Some tweaks

* materialize lambda: per-head split for graph-mode tp_replicate

7dd19e19 changed wk_b/wv_b distribution from mirror to per-head split
(split_dim=2) via prepare_split_tensors. That path only fires when
wk_b/wv_b are loaded from GGUF.

Models that store only wkv_b in GGUF derive wk_b/wv_b at load via
llm_prepare_mla, going through the materialize lambda, which was
untouched and still produced mirror replicas (split_dim=-1, full n_head
per device).

build_deepseek2_tp_attention now does mul_mat(wk_b_local, q_nope_perm)
without the prior view_3d slice, so a mirror replica passes an n_head
tensor where the kernel expects n_head_local. Result: silent SIGSEGV
right after model load.

Mirror logic in materialize is replaced with the same per-head split as
prepare_split_tensors: head_offsets derived from wo split, each rank
gets a tensor with ne[2]=n_head_local, data copied from the appropriate
source byte slice. Singular `computed` tensor keeps full metadata for
tensors_by_name lookups.

Tested: 8x3090, -sm graph -mla 3 -fa on now boots cleanly and
sweep-benches without crash. Log confirms new path: "Computed
blk.X.attn_k_b.weight ... split across N devices on dim=2".

* cleanup: indent fix + remove dead view_3d slicing and debug printf

- build_deepseek2.cpp: re-indent the self_attention block in
  build_deepseek2_layer_attention (lines 253-670). Block was at column 0
  inside a function body; now at the expected 4/8-space indent.
- build_deepseek2.cpp: drop the commented-out view_3d slicing and debug
  printfs left over after 7dd19e19's switch to direct mul_mat on
  per-rank wk_b_local / wv_b_local. Update the stale 'wk_b is
  replicated (split_dim=-1)' comment to match the new split_dim=2
  reality.
- ggml-cuda.cu: remove the leftover debug printf in
  ggml_backend_cuda_split_buffer_get_tensor.

No behavior change. Verified with a clean rebuild and DSV2.5 +
GLM-4.7-Flash sweep-bench runs.

* llm_load_tensors: gate incompatible-flag warning to MLA archs

The -ncmoe / -rtr / -muge / -ot warning under -sm graph currently fires
for all archs that support graph mode. That's an over-reach: the
incompatibility is specific to the MLA TP paths (DEEPSEEK2, GLM_DSA,
MISTRAL4) — Gemma4 graph mode existed pre-PR and works with those flags.
Gate the warning to MLA archs only.

Also refreshes two stale comments left over from the wk_b/wv_b
mirror -> per-head-split rewrite:
- src/llama.cpp llm_prepare_mla: "Replicate wk_b/wv_b ..." now reads
  "Per-head split wk_b/wv_b ..." to match what the materialize lambda
  actually does post-823a39e2.
- src/llama-load-tensors.cpp distribute_mla_tensors_for_split_mode_graph:
  drop the wkv_b row-split mention (wkv_b is no longer created under
  graph mode after 7dd19e19) and correct the wk_b/wv_b distribution
  description (per-head split, not per-device replicated).

---------

Co-authored-by: Kawrakow <iwankawrakow@gmail.com>
2026-05-19 08:36:17 +03:00
..
cmake Merge mainline llama.cpp (#3) 2024-07-27 07:55:01 +02:00
jinja AutoParser: improve reasoning budget and handling of space/newline in tool calls (#1819) 2026-05-19 08:34:19 +03:00
CMakeLists.txt Autoparser - complete refactoring of parser architecture (#1376) 2026-04-22 10:04:13 +02:00
base64.hpp llava : expose as a shared library for downstream projects (#3613) 2023-11-07 00:36:23 +03:00
build-info.cpp.in build : link against build info instead of compiling against it (#3879) 2023-11-02 08:50:16 +02:00
chat-auto-parser-generator.cpp AutoParser: improve reasoning budget and handling of space/newline in tool calls (#1819) 2026-05-19 08:34:19 +03:00
chat-auto-parser-helpers.cpp Autoparser - complete refactoring of parser architecture (#1376) 2026-04-22 10:04:13 +02:00
chat-auto-parser-helpers.h Autoparser - complete refactoring of parser architecture (#1376) 2026-04-22 10:04:13 +02:00
chat-auto-parser.h Autoparser - complete refactoring of parser architecture (#1376) 2026-04-22 10:04:13 +02:00
chat-diff-analyzer.cpp AutoParser: improve reasoning budget and handling of space/newline in tool calls (#1819) 2026-05-19 08:34:19 +03:00
chat-peg-parser.cpp AutoParser: improve reasoning budget and handling of space/newline in tool calls (#1819) 2026-05-19 08:34:19 +03:00
chat-peg-parser.h AutoParser: improve reasoning budget and handling of space/newline in tool calls (#1819) 2026-05-19 08:34:19 +03:00
chat.cpp AutoParser: improve reasoning budget and handling of space/newline in tool calls (#1819) 2026-05-19 08:34:19 +03:00
chat.h AutoParser: improve reasoning budget and handling of space/newline in tool calls (#1819) 2026-05-19 08:34:19 +03:00
common.cpp MLA tensor parallelism under -sm graph (DEEPSEEK2/GLM_DSA/MISTRAL4) (#1821) 2026-05-19 08:36:17 +03:00
common.h MLA tensor parallelism under -sm graph (DEEPSEEK2/GLM_DSA/MISTRAL4) (#1821) 2026-05-19 08:36:17 +03:00
console.cpp check C++ code with -Wmissing-declarations (#3184) 2023-09-15 15:38:27 -04:00
console.h gguf : new file format with flexible meta data (beta) (#2398) 2023-08-21 23:07:43 +03:00
json-partial.cpp common : introduce composable PEG parser combinators for chat parsing and new jinja template engine (#1369) 2026-03-09 11:03:33 +01:00
json-partial.h Move minja and nlohmann/json to vendor (#802) 2025-09-27 09:12:35 +02:00
json-schema-to-grammar.cpp Autoparser - complete refactoring of parser architecture (#1376) 2026-04-22 10:04:13 +02:00
json-schema-to-grammar.h common : introduce composable PEG parser combinators for chat parsing and new jinja template engine (#1369) 2026-03-09 11:03:33 +01:00
llguidance.cpp Tool calls support from mainline (#723) 2025-09-01 08:38:49 +03:00
log.cpp Refactor chat and server file (#1062) 2025-12-15 08:27:20 +01:00
log.h Server: refactor and rename functions (#1151) 2026-01-18 08:16:57 +02:00
ngram-cache.cpp spec : add self speculative decoding, ngram and refactor (#1261) 2026-02-13 19:04:55 +01:00
ngram-cache.h spec : add self speculative decoding, ngram and refactor (#1261) 2026-02-13 19:04:55 +01:00
ngram-map.cpp Speculative checkpoints for recurrent models (#1669) 2026-04-24 09:59:30 +02:00
ngram-map.h spec : add self speculative decoding, ngram and refactor (#1261) 2026-02-13 19:04:55 +01:00
ngram-mod.cpp spec : add self speculative decoding, ngram and refactor (#1261) 2026-02-13 19:04:55 +01:00
ngram-mod.h spec : add self speculative decoding, ngram and refactor (#1261) 2026-02-13 19:04:55 +01:00
peg-parser.cpp Autoparser - complete refactoring of parser architecture (#1376) 2026-04-22 10:04:13 +02:00
peg-parser.h Autoparser - complete refactoring of parser architecture (#1376) 2026-04-22 10:04:13 +02:00
reasoning-budget.cpp AutoParser: improve reasoning budget and handling of space/newline in tool calls (#1819) 2026-05-19 08:34:19 +03:00
reasoning-budget.h AutoParser: improve reasoning budget and handling of space/newline in tool calls (#1819) 2026-05-19 08:34:19 +03:00
regex-partial.cpp Autoparser - complete refactoring of parser architecture (#1376) 2026-04-22 10:04:13 +02:00
regex-partial.h Tool calls support from mainline (#723) 2025-09-01 08:38:49 +03:00
sampling.cpp AutoParser: improve reasoning budget and handling of space/newline in tool calls (#1819) 2026-05-19 08:34:19 +03:00
sampling.h AutoParser: improve reasoning budget and handling of space/newline in tool calls (#1819) 2026-05-19 08:34:19 +03:00
spec-tuner.cpp Pre-allocate buffers for hybrid model checkpoints (#1774) 2026-05-12 07:21:25 +03:00
spec-tuner.h Pre-allocate buffers for hybrid model checkpoints (#1774) 2026-05-12 07:21:25 +03:00
speculative.cpp spec : disacard last drafted token with low prob (#1820) 2026-05-19 08:35:35 +03:00
speculative.h Allow dual speculative decoding (#1789) 2026-05-15 10:10:40 +03:00
suffix-tree.cpp suffix-spec: load corpus in chunks (#1721) 2026-05-04 07:56:07 +03:00
suffix-tree.h Self-decoding: Adds support for suffix decoding (#1646) 2026-04-18 16:10:10 +02:00
train.cpp Server: refactor and rename functions (#1151) 2026-01-18 08:16:57 +02:00
train.h sync : ggml (backend v2) (#3912) 2023-11-13 14:16:23 +02:00
unicode.cpp Autoparser - complete refactoring of parser architecture (#1376) 2026-04-22 10:04:13 +02:00
unicode.h Autoparser - complete refactoring of parser architecture (#1376) 2026-04-22 10:04:13 +02:00