Skip to content

initial optimization commits - #13

Closed
Y-Cyan-Zhang wants to merge 1 commit into
PrismML-Eng:masterfrom
Y-Cyan-Zhang:master
Closed

Y-Cyan-Zhang wants to merge 1 commit into
PrismML-Eng:masterfrom
Y-Cyan-Zhang:master

Conversation

@Y-Cyan-Zhang

Copy link
Copy Markdown

Make sure to read the contributing guidelines before submitting a PR

bricklc pushed a commit to bricklc/prism-ml-llama.cpp that referenced this pull request Apr 25, 2026
PrismML-Eng#13)

Models with n_embd_head_k not divisible by QK_TURBO3_GROUP (128) — e.g.
GLM-4.7 Flash / DeepSeek2 MLA with head_dim=576 — previously hard-crashed
with GGML_ASSERT(ne00 % QK_TURBO3_GROUP == 0) in set-rows.cu.

Fix:
- supports_op for SET_ROWS: return false when turbo3 and ne00 % 128 != 0,
  so llama.cpp falls back to an unquantised KV path instead of asserting
- get_best_fattn_kernel: return BEST_FATTN_KERNEL_NONE for turbo3 when
  K head dim is not 128-aligned (VEC kernel only instantiated for D∈{64,128,256})

Affected models can now load with -ctk turbo3 -ctv turbo3; the non-aligned
heads silently use f16 KV while 128-aligned heads continue to use turbo3.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
bricklc pushed a commit to bricklc/prism-ml-llama.cpp that referenced this pull request Apr 25, 2026
…rismML-Eng#13)

Models with n_embd_head_k not divisible by 128 (e.g. GLM-4.7 Flash,
DeepSeek2 MLA with head_dim=192/576) previously crashed with
GGML_ASSERT in set-rows.cu or segfaulted in CPU flash attention.

Changes:
- llama-kv-cache.cpp: detect incompatible head_dim at KV cache init,
  log a warning, and auto-fall back to q8_0 instead of crashing
- ggml-cuda.cu supports_op: return false for turbo3 SET_ROWS when
  ne00 % 128 != 0, and for TURBO_WHT when ne[0] % 128 != 0
- fattn.cu: return BEST_FATTN_KERNEL_NONE for turbo3 at non-128 D
- ggml-cpu.c: add turbo3 entry to type_traits_cpu (vec_dot + from_float)
  so CPU flash attention can handle turbo3 K/V for models where CUDA FA
  returns NONE (e.g. D=192 which has no CUDA FA kernel for any type)
- set-rows.cu: add tail kernel for non-128 remainder (future use)
- turbo-wht.cu: head-dim-aware processing with tail pass-through
- ops.cpp: CPU WHT head-dim-aware with tail identity copy

Tested: DeepSeek-Coder-V2 (head_dim=192) now loads with -ctk turbo3
and auto-falls back to q8_0 with clear warning. Qwen3.5 (head_dim=128)
continues to use turbo3 at full speed (223 t/s).

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
bricklc pushed a commit to bricklc/prism-ml-llama.cpp that referenced this pull request Apr 25, 2026
Models with head_dim divisible by 64 but not 128 (e.g. DeepSeek2 MLA
head_dim=192, GLM-4.7 Flash head_dim=576) now use 64-element WHT
groups instead of crashing or falling back to q8_0.

Key changes:

**64-element WHT groups:**
- turbo-quant.cuh: 64-element sign arrays + FWHT-64 + rotate_64
- set-rows.cu: templated on GROUP_SIZE {128,64}, reads group_size
  from SET_ROWS op_params (set by llama-kv-cache based on head_dim)
- turbo-wht.cu: templated on group_size, head-dim-aware dispatch
- ggml.h/ggml.c: ggml_turbo_wht() now takes explicit group_size
  parameter (0=auto) to handle MLA where output dim differs from
  K head dim
- ops.cpp: CPU WHT parameterized on group_size

**MLA fix — missing Q rotation in K-only build_attn:**
- build_attn(inp_attn_k, ...) had no turbo Q pre-rotation, while
  SET_ROWS was applying WHT to K. Result: <unrotated_Q, rotated_K>
  = garbage. Now all three build_attn variants apply Q rotation.
- Inverse WHT moved inside build_attn_mha (before v_mla projection)
  and also added to the non-FA attention path.

**Guards + fallback:**
- supports_op, fattn.cu, llama-graph.cpp: relaxed from %128 to %64
- llama-kv-cache.cpp: falls back to q8_0 only when head_dim%64!=0
- CPU type_traits_cpu turbo3 entry for CPU FA vec_dot

Tested: GLM-4.7 Flash (head_dim=576) 208 t/s, DeepSeek-V2 (192) 143 t/s,
Qwen3.5 (128) 223 t/s — all producing correct output with turbo3.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
khosravipasha pushed a commit that referenced this pull request Jun 7, 2026
* spec: support MTP

* fix batch size

* rename files

* cont : simplify (#7)

* MTP: clean-up (#9)

* MTP: clean-up

* review: use llama_context_type instead of llama_graph_type

* review: remove llama_model_has_mtp

* review: fix convert issues

* convert: fix pycheck

* review: formatting

* use `mtp-` for identifying mtp models

* convert: fix mtp conversion

* mtp -> draft-mtp

* remove unused llama_arch

* add need_embd in speculative

* llama: allow partial seq_rm for GDN models for speculative decoding

Currently speculative checkpoint needs to restart from a checkpoint
after some draft tokens are not accepted, this leads to some wastage in
running the target again. This PR adds the ability to rollback upto
`draft_max` by storing the GDN intermediates.

* fix pending state

* vulkan: add GDN partial rollback

* meta: extend check to axis 1

* metal: add GDN partial rollback

Extend the gated delta net kernel to store intermediate states for
partial rollback support on the Metal backend.

- Add K (snapshot slot count) as a function constant
- Read input state from slot 0 of the 3D state tensor
- Write intermediate states to different slots during token loop
- For K=1, maintain backward-compatible single-slot behavior

Ref: ggml-org@8c05923

Assisted-by: llama.cpp:local pi

* delta_net_base: use ggml_pad instead of new_tensor

* review: add need_rs_seq

* review: rename part_bounded to n_rs

* review: deslop comments

* review: rename, add asserts

* server : adjust checkpoint logic (#11)

* server : adjust checkpoint logic

* cont : rm asserts

* server-context: fix early exit

* spec : fix compatibility with n-gram and add TODOs (#13)

* metal : cleanup

* llama : fix faulty bitwise check in recurrent memory

* server : disable RS-based MTP in combination with other spec types

* spec : add TODOs

* cont : fix comment

* cont : update comment

* common : fix logic for ngram + mtp compat

* llama-memory: enable checkpointing with partial rollback

* cont: add test-case for loading into a dirty ctx

* llama-memory-recurrent: clear rs_idx in clear

* download: fix mtp path

* llama-arch: fix enorm op

* docs: update docs

* conversion: fix type annotations

---------

Co-authored-by: Georgi Gerganov <[email protected]>
bong-water-water-bong pushed a commit to bong-water-water-bong/llama.cpp that referenced this pull request Jul 15, 2026
…rter-smoke

ci: avoid importing converter help in ROCmFPX check
marcospaulo referenced this pull request in torad-labs/llama.cpp Sep 21, 2026
The lens reads the residual stream's SUM at a layer, and a committed token looks the same there
whether the block's attention fetched it from the context or its feed-forward produced it from
the weights: on slot1-task51545 a verbatim quote of the user (#13 " Christian") and the reach for
the same word 180 tokens later (#190) both enter at L56 and saturate at L60. The sum cannot
separate provenance. The parts can.

With --lens-channels, for every lens layer the qwen35 graph also hands lens_record the block's
input (inpSA), its attention or linear-attention output (before the residual add) and its
feed-forward output (before its residual add). lens_build scales each by the block OUTPUT's rms
(the same 1/sqrt(mean(x^2)+eps) the norm applies to that output) and the output norm weight, and
puts them through the same head, in the same stacked matmul as the layer outputs. Because the head
is linear, in logit space out == prev + attn + ffn for every lens layer and token when no control
vector is applied to that layer: that identity is the instrument check, and a checker that reads
it from the JSONL alone (with a mutation mode that must fail) lives beside the lens README in the
rig. The final layer's out row stays the served logits (b6f4667), so its residual carries that
commit's kernel drift; the check tolerates it there only.

libllama: cparams.lens_channels; llama_set_lens_channels / llama_n_lens_channels (1 or 4) /
llama_get_lens_channel_ith(k, channel, i), the host buffer laid out [k][channel][row][vocab], the
extraction, the row reorder and the reservation sized by the channel count; llama_get_lens_ith is
channel OUT and unchanged. Off (the default) the graph, the buffer and the JSONL are byte for byte
what they were.

llama-server: each line gains "argmax" (the served argmax id) and "channels": per lens layer the
out logit of the sampled token and of the argmax, and for prev/attn/ffn the same two contributions
plus the channel's top-k pushes as {id, piece, v} in logit units. The decode thread copies four
rows per lens layer per token instead of one; the writer queue's line bound is divided by the
channel count so its byte bound is unchanged.

Measured on the Ternary-Bonsai-2-27B PQ2_0 ablated pack, CPU (both cards were serving live heads),
greedy, 36 prompt tokens, 9 generated ("purple giraffes negotiate quietly. Paris"), lens layers
48,52,56,59,60,62,63:
- flag off vs on: tok, layers (top-k ids and p) and served identical on 9/9 tokens; the on file adds
  only "argmax" and "channels";
- the identity: max |out - (prev+attn+ffn)| = 0.028 logits over 9 tokens x 7 layers; the same file
  with one ffn term scaled by 1.5 fails by 4.5 to 5.8 (channels-check.py --mutate);
- the five copied words are written by attention block 59 alone (+6 to +14 logits each, nothing
  from the GDN blocks at 48-56), their subword continuations by the FFN at 62/63, and the recalled
  word enters from blocks 53-55 with block 59's attention adding +0.5 and its FFN pushing -2.4:
  the sum lens shows both "entering late"; the parts show different blocks and different routes.
Not measured: the GPU decode cost of the four-rows-per-layer copy; both cards were serving live
heads. The CPU path needs --no-repack (the CPU repack buffer segfaults on PQ2_0 at load, on the
published a0bacec build as well).

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant