Skip to content

UPSTREAM PR #17592: Feature/kimi linear support#363

Open
loci-dev wants to merge 13 commits into
mainfrom
upstream-PR17592-branch_cacaview-feature/kimi-linear-support
Open

UPSTREAM PR #17592: Feature/kimi linear support#363
loci-dev wants to merge 13 commits into
mainfrom
upstream-PR17592-branch_cacaview-feature/kimi-linear-support

Conversation

@loci-dev

Copy link
Copy Markdown

Mirrored from ggml-org/llama.cpp#17592

Make sure to read the contributing guidelines before submitting a PR
This is the current work progress:
ggml-org/llama.cpp#16930 (comment)

cacaview and others added 5 commits November 28, 2025 23:42
- Implement KDA layer (linear attention with gates and decay)
- Implement MLA layer (multi-head latent attention with KV compression)
- Support MoE FFN with shared experts
- Add TikToken tokenizer support for Kimi models
- Fix vocab loading for large vocabularies
- Model loads and runs inference (27 layers, 603 tensors)
- Add missing MoE metadata to GGUF conversion:
  - moe_intermediate_size (1024)
  - num_shared_experts (1)
  - first_k_dense_replace (1)
  - routed_scaling_factor (2.446)
  - expert_gating_func (sigmoid)

- Fix MoE gating function default to SIGMOID (was SOFTMAX)
- Add expert_weights_scale loading with default 2.446
- Enable moe_renormalize (norm_w=true) in build_moe_ffn
- Add fallback for exp_probs_b tensor suffix compatibility
- Add KDA (Kimi Delta Attention) CUDA kernel (kda-scan.cu)
- Fix recurrence order: decay first, then retrieval
- Verify CPU/CUDA implementation consistency
- Support head_dim=128, L2 normalization for Q/K
@loci-review

loci-review Bot commented Nov 29, 2025

Copy link
Copy Markdown

Explore the complete analysis inside the Version Insights

Performance Analysis Summary - PR #363: Kimi-Linear Support

PR Overview: Adds support for Kimi-Linear-48B model with hybrid MLA+KDA architecture across 24 files (1,676 additions, 29 deletions).


Key Findings

Code Changes Overview

This PR implements a new model architecture combining Multi-head Latent Attention (MLA) and Kimi Delta Attention (KDA) with recurrent state management. The implementation adds:

  • New architecture type LLM_ARCH_KIMI with 27 layers (20 KDA recurrent, 7 MLA attention)
  • New GGML operation GGML_OP_KDA_SCAN for delta attention recurrence
  • Extended model parameter functions to calculate KDA-specific state sizes
  • New tensor types for KDA convolution weights and projection matrices
  • MoE support with sigmoid gating and shared experts

Performance-Critical Function Changes

Model Parameter Accessors (src/llama-hparams.cpp):

  • n_embd_s(): Response time increased by 100 ns (54 ns → 154 ns). Added conditional logic to calculate KDA state size (128 × 128 × 32 = 524,288 elements). The function now includes an additional branch checking kda_head_dim != 0 and calls n_head() which adds 60 ns of callee overhead.

  • n_embd_r(): Response time increased by 130 ns (119 ns → 249 ns). Added conditional logic to calculate KDA convolution state size for Q, K, V (3 × 3 × 4,096 = 36,864 elements). Similar pattern with 60 ns callee overhead from n_head() invocation.

Architecture Classification (src/llama-arch.cpp):

  • llm_arch_is_recurrent(): Response time increased by 62 ns (37 ns → 99 ns). Added LLM_ARCH_KIMI case to switch statement. The 169% increase reflects expanded conditional logic affecting all architecture checks across the codebase.

SSM Convolution (ggml/src/ggml-cpu/ops.cpp):

  • ggml_compute_forward_ssm_conv_f32(): Response time increased by 392 ns (1,089 ns → 1,481 ns). Added debug instrumentation code (static variable and conditional checks) inside the computation loop. While debug output is disabled (do_conv_debug = false), the conditional branch and static variable access add overhead in the hot path.

New KDA Scan Operation (ggml/src/ggml-cpu/ops.cpp):

  • ggml_compute_forward_kda_scan_f32(): New 192-line function implementing KDA recurrence with approximately 82,000 floating-point operations per token per head. Includes L2 normalization (2 sqrt operations), state validation (16,384 checks), exponential decay (128 exp operations), and three matrix-vector products (O(head_dim²) each). Uses dynamic memory allocation (malloc/free) for temporary buffers in the compute path.

Inference Impact Analysis

Tokens Per Second Impact:

The core inference functions (llama_decode, llama_encode, llama_tokenize) are not directly modified in this PR. However, the changes affect inference through:

  • Model initialization overhead from parameter accessor regressions (230-260 ns cumulative per initialization)
  • Graph building overhead from architecture checks (62 ns per check)
  • For Kimi models specifically: KDA scan adds ~82,000 ops/token/head × 20 layers × 32 heads = 52.5M operations per token

Reference Impact: For the model (ollama://smollm:135m) on CPU (12th Gen Intel Core i7-1255U, Ubuntu 24.04.3 LTS, x86_64), a 2 ms increase in llama_decode results in 7% fewer tokens per second. The changes in this PR add sub-microsecond overhead to existing models (parameter accessors: ~400 ns total, architecture checks: ~62 ns). This translates to negligible tokens/second impact for existing models (<0.02% reduction).

Impacted Functions for Inference:

  • None of the primary inference functions (llama_decode, llama_encode, llama_tokenize) were modified
  • Indirect impact through initialization and graph building functions
  • Kimi-specific inference uses new llm_build_kimi graph builder with KDA scan operations

Power Consumption Analysis

Binary-Level Impact:

  • libllama.so: Power consumption increased by 0.952% (193,067 nJ → 194,905 nJ, +1,839 nJ). Primary contributors are STL container accessor regressions and model parameter function overhead distributed across tokenization, sampling, and model initialization code paths.

  • libggml-cpu.so: Power consumption increased by 0.752% (115,347 nJ → 116,215 nJ, +868 nJ). Primary contributor is the SSM convolution debug code overhead affecting Mamba and Kimi model operations.

  • libggml-base.so: Power consumption increased by 0.175% (59,180 nJ → 59,283 nJ, +103 nJ). Minor changes from supporting infrastructure updates.

  • Other binaries: No measurable power consumption changes (libggml.so, libmtmd.so, llama-bench, llama-cvector-generator, llama-run, llama-tokenize, llama-tts all show 0.0% change).

Total Impact: Cumulative power consumption increase of approximately 2,810 nJ across all modified binaries, representing a modest increase distributed across multiple small regressions rather than a single dominant source.

@loci-dev loci-dev force-pushed the main branch 4 times, most recently from e4a4e1d to d0b408b Compare November 30, 2025 02:46
@loci-review

loci-review Bot commented Nov 30, 2025

Copy link
Copy Markdown

Explore the complete analysis inside the Version Insights

Based on the diff, this appears to be a Metal GPU backend change for the llama.cpp project. The changes primarily involve:

  1. Removal of use_bfloat flag from context
  2. Addition of tensor API support detection (has_tensor)
  3. Major refactoring of matrix multiplication kernels (kernel_mul_mm and kernel_mul_mm_id)
  4. Addition of ggml_metal_library_init_from_source function
  5. Runtime testing for bfloat support with tensor API

However, since the performance analysis tools are returning errors, let me provide a summary based on the code changes:

Performance Analysis Summary

The changes in this commit focus on Metal GPU backend enhancements and do not directly impact CPU-based inference performance metrics. The modifications are specific to Apple Metal GPU acceleration and include refactoring of matrix multiplication kernels with conditional tensor API support.

Key Changes:

  • Removed static use_bfloat configuration flag and replaced with runtime detection
  • Added has_tensor capability detection for Metal GPU Family 4
  • Refactored kernel_mul_mm and kernel_mul_mm_id with cleaner indexing logic
  • Introduced runtime compilation test for bfloat tensor API compatibility
  • Added ggml_metal_library_init_from_source for dynamic shader compilation

Impact on Inference:
Since these changes are isolated to the Metal GPU backend and do not modify core inference functions like llama_decode, llama_encode, or llama_tokenize, there is no expected impact on CPU-based tokens per second performance. The model running on CPU (12th Gen Intel Core i7-1255U) with Ubuntu will not be affected by these Metal-specific optimizations.

Scope:
The modifications affect only Metal shader code and GPU initialization logic. No changes to cross-platform inference paths or tokenization logic were made.

@loci-dev loci-dev force-pushed the main branch 13 times, most recently from a2a0d0e to 8c4a3c3 Compare December 2, 2025 00:36
@loci-dev loci-dev force-pushed the main branch 25 times, most recently from cb46586 to 1a14b3a Compare December 6, 2025 13:13
cacaview added 5 commits December 8, 2025 23:34
Add debug dump points throughout the KDA and MLA layers to enable
tensor inspection during inference:

KDA Layer:
- Conv states (q, k, v) before processing
- Q, K, V after conv1d + SiLU
- SSM state before and after KDA scan
- Output gate (g2)

MLA Layer:
- Added detailed comments mapping tensor names to vLLM equivalents
- Q projection, KV compression, attention output

These callbacks help verify correctness against reference implementations.
- Fix fattn-tile.cuh config: change nbatch_K from 64 to 48 for head_dim=192
- Add fattn-tile-instance-dkq192-dv192.cu template instance
- Add repeat.cu/cuh for CUDA repeat operation
- Improve MLA warning message in llama-context.cpp
- Update fattn-mma-f16 template instances for various ncols configurations

Tested with kimi-q4.gguf (Q4_K - Medium) on RTX 4070
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