Skip to content

[Feature] Add MiniCPM-SALA support - #30360

Merged
alexnails merged 11 commits into
sgl-project:mainfrom
cauphenuny:pr/minicpm-sala/main
Aug 24, 2026
Merged

alexnails merged 11 commits into
sgl-project:mainfrom
cauphenuny:pr/minicpm-sala/main

Conversation

@cauphenuny

@cauphenuny cauphenuny commented Jul 7, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

Add native serving support for MiniCPM-SALA checkpoints, which interleave MiniCPM sparse softmax-attention layers with constant-decay Lightning linear-attention layers.

The implementation integrates the model with SGLang's existing attention, linear-attention, CUDA graph, and memory-pool infrastructure. It also replaces the serving-time dependency on the original out-of-tree block-table extension with SGLang JIT kernels.

Modifications

  • Add MiniCPMHybridConfig and register both MiniCPMForCausalLM and MiniCPMSALAForCausalLM.
    • Normalize mixer aliases and repeat mixer patterns to the configured layer count.
    • Parse both flattened and legacy nested sparse-attention configuration.
    • Describe Lightning state using the existing Mamba2CacheParams and linear-attention model registry.
  • Extend the MiniCPM model with SALA mixer support.
    • Add Lightning QKV projection, optional Q/K normalization, RoPE, output normalization and gating, configurable attention scaling, and checkpoint-compatible weight loading.
    • Reuse the in-tree Lightning backend and seg_la kernels instead of introducing a model-specific linear-attention backend or external dependency.
    • Generalize the shared Lightning backend to read the linear KV-head count, layerwise decay mode, and per-layer attention scale from the model config.
  • Add minicpm_flashattn and minicpm_flashinfer sparse-attention backends.
    • Compose with FlashAttentionBackend for dense metadata and CUDA graph state.
    • Keep FlashInfer support as a composed decode path rather than duplicating its wrapper management.
    • Select FA4 automatically on supported Blackwell GPUs.
    • Support dense/sparse prefill and decode, CUDA graph capture/replay, K1/K2 key compression, sparse top-k selection, and optional fused TileLang kernels.
  • Add JIT-compiled CUDA block-table kernels.
    • Use v2 for prefill and v3 for the optimized decode layout, with automatic v2 fallback for unsupported v3 shapes.
    • Cache compiled modules by top-k, head-group count, and block size.
  • Attach the MiniCPM compressed cache to the existing ReqToTokenPool.
    • Reserve and account for K1/K2 capacity without introducing a model-specific pool subclass.
    • Route extend/decode allocation, rollback, request release, streaming-session cleanup, allocator reset, and pool statistics through generic no-op auxiliary-cache hooks.
  • Add MiniCPM model overrides and developer controls.
    • Default sparse SALA models to minicpm_flashattn, or minicpm_flashinfer on supported Blackwell GPUs.
    • Disable radix caching only for configs that actually contain sparse or Lightning layers.
    • Reject unsupported sparse-attention PD disaggregation early.
    • Add SGLANG_MINICPM_FUSE_TOPK, SGLANG_MINICPM_SPLIT_STAGE1, SGLANG_MINICPM_DENSE_AS_SPARSE, and SGLANG_MINICPM_FORCE_DENSE environment switches.
  • Add CPU and CUDA coverage for configuration defaults, model overrides, Lightning dispatch, compressed-cache lifecycle, sparse metadata, block-table kernels, and compression kernels.

Accuracy Tests

The following CPU regression suite passes on the final rebased branch:

112 passed, 8 subtests passed

It covers:

  • MiniCPM config parsing, mixer selection, Lightning cache shape, scaling, and idle batches.
  • Sparse metadata construction, CUDA graph padding, and dense/sparse batch accounting.
  • K1/K2 allocation boundaries, reuse, rollback, cleanup, and streaming-session release.
  • Backend defaults, force-dense remapping, radix-cache gating, and unsupported PD disaggregation.

CUDA correctness tests are included for:

  • v2/v3 block-table outputs against a PyTorch reference.
  • Sparse key compression against a PyTorch reference.

End-to-end checkpoint output accuracy has not yet been recorded on the final rebased branch and should be added before merge.

Speed Tests and Profiling

test/registered/kernels/benchmark/bench_get_block_table.py benchmarks the v2 prefill and v3 decode block-table kernels across token counts.

Checklist

Review and Merge Process

  1. Ping Merge Oncalls to start the process. See the PR Merge Process.
  2. Get approvals from CODEOWNERS and other reviewers.
  3. Trigger CI tests with comments or contact authorized users to do so.
    • Common commands include /tag-and-rerun-ci, /tag-run-ci-label, /rerun-failed-ci
  4. After green CI and required approvals, ask Merge Oncalls or people with Write permission to merge the PR.

CI States

Latest PR Test (Base): ⏳ Run #32711430603
Latest PR Test (Extra): ⏳ Run #32711430188
Latest PR Test (AMD ROCm 7.2): ⏳ Run #32711430438

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces support for MiniCPM-SALA sparse attention and hybrid models with Simple GLA (Lightning) attention, including JIT-compiled CUDA kernels, memory pool extensions, and an XML tool call detector. The code review highlights several critical issues, including a potential out-of-bounds memory access in the get_block_table_cuda_v3 kernel, a server crash when radix cache is enabled with sparse attention, and multiple GPU-to-CPU synchronization bottlenecks in the forward path caused by operations on GPU tensors (such as torch.max, .any(), .item(), and torch.arange). Additionally, defensive checks are recommended to prevent potential AttributeError and TypeError exceptions during configuration and tool parsing.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread python/sglang/srt/models/minicpm.py Resolved

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds first-class serving support for MiniCPM-SALA hybrid models by introducing a new HF config, extending the MiniCPM model implementation with hybrid mixer support (sparse attention + Simple GLA/lightning layers), adding MiniCPM-specific attention backends and sparse KV metadata/memory pools, and providing a JIT CUDA kernel for sparse block-table generation plus CI tests/benchmarks.

Changes:

  • Add MiniCPMHybridConfig and update model runner / KV-cache builder to treat MiniCPM-SALA as a hybrid (linear-attn + sparse-attn) architecture.
  • Introduce MiniCPM sparse-attention utilities/kernels/backends (FlashAttention + FlashInfer paths), new req-to-token auxiliary sparse index pools, and CUDA-graph-related plumbing.
  • Add minicpm4_xml tool-call parser plus unit tests for sparse metadata, tool-call parsing, and get-block-table JIT kernel (incl. benchmark).

Reviewed changes

Copilot reviewed 31 out of 32 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
test/registered/unit/layers/test_minicpm_sparse_metadata.py Adds CPU tests for sparse metadata behavior (chunked prefill + CUDA-graph padding).
test/registered/jit/test_get_block_table.py Adds CUDA tests for get-block-table v1/v2/v3 parity + golden checks.
test/registered/jit/benchmark/bench_get_block_table.py Adds a Triton perf report benchmark for block-table kernels.
test/registered/function_call/test_minicpm4_xml_detector.py Adds tests for MiniCPM4 XML tool-call detection/parsing (incl. streaming).
python/sglang/srt/utils/hf_transformers/common.py Registers MiniCPMHybridConfig as a supported HF config.
python/sglang/srt/server_args.py Adds MiniCPM backend names + MiniCPM tuning flags and model-arch adjustments.
python/sglang/srt/models/minicpm.py Extends MiniCPM with hybrid mixer selection and SimpleGLA mixer wrapper; updates EntryClass registration.
python/sglang/srt/model_executor/runner/decode_cuda_graph_runner.py Adds a replay side-channel for MiniCPM CUDA-graph replay metadata.
python/sglang/srt/model_executor/model_runner.py Exposes minicpm_hybrid_config and includes it in “mambaish” detection.
python/sglang/srt/model_executor/model_runner_kv_cache_mixin.py Initializes MiniCPM-specific req-to-token pools for sparse/hybrid layouts.
python/sglang/srt/mem_cache/memory_pool.py Adds MiniCPM sparse auxiliary pools and hybrid req-to-token pool implementations.
python/sglang/srt/mem_cache/mamba_radix_cache.py Persists/frees MiniCPM auxiliary sparse indices alongside radix-cache lifecycle.
python/sglang/srt/mem_cache/kv_cache_builder.py Treats MiniCPM hybrid as requiring hybrid-cache handling.
python/sglang/srt/mem_cache/common.py Allocates/writes auxiliary sparse indices during extend/decode allocations.
python/sglang/srt/mem_cache/chunk_cache.py Saves/frees MiniCPM auxiliary sparse indices for chunk-cache lifecycle.
python/sglang/srt/layers/attention/minicpm/sparse_utils.py Adds sparse attention metadata/config/helpers for MiniCPM (builder/analyzer + compression metadata).
python/sglang/srt/layers/attention/minicpm/sparse_kernels.py Adds Triton kernels for key compression and sparse-page-table → flashinfer conversion.
python/sglang/srt/layers/attention/minicpm/fuse_kernel.py Adds TileLang fused kernels for pooled top-k selection (prefill/decode).
python/sglang/srt/layers/attention/minicpm/attention_kernels.py Adds a backend-agnostic attention-kernel interface and FlashInfer/FlashAttn implementations.
python/sglang/srt/layers/attention/hybrid_linear_attn_backend.py Adds SimpleGLAAttnBackend and ALiBi slope helper for lightning layers.
python/sglang/srt/layers/attention/attention_registry.py Registers minicpm_flashattn and minicpm_flashinfer attention backends.
python/sglang/srt/function_call/minicpm4_xml_detector.py Implements MiniCPM4 XML tool-call detection/parsing (non-streaming + streaming).
python/sglang/srt/function_call/function_call_parser.py Registers minicpm4_xml format detector.
python/sglang/srt/configs/minicpm.py Adds MiniCPMHybridConfig including sparse + lightning parameters and cache params.
python/sglang/srt/configs/mamba_utils.py Adds SimpleGLA state shape/cache params for hybrid-linear backends.
python/sglang/srt/configs/init.py Exports MiniCPMHybridConfig.
python/sglang/jit_kernel/utils.py Adds CCCL include-path fallback (flashinfer-bundled) for JIT robustness.
python/sglang/jit_kernel/minicpm_sala/get_block_table.py Adds Python wrapper to JIT-compile/dispatch get-block-table kernels by topk.
python/sglang/jit_kernel/minicpm_sala/init.py Exports get-block-table functions.
python/sglang/jit_kernel/csrc/minicpm_sala/get_block_table.cuh Adds CUDA kernels + tvm-ffi wrappers for get-block-table v1/v2/v3.
python/sglang/srt/layers/attention/minicpm/backend.py Implements MiniCPMSparseBackend dispatching to flash-attn or flashinfer.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@cauphenuny
cauphenuny force-pushed the pr/minicpm-sala/main branch from ad80e7e to ec3320e Compare July 16, 2026 00:26
@cauphenuny
cauphenuny force-pushed the pr/minicpm-sala/main branch from ec3320e to b844e97 Compare July 17, 2026 03:03
@alexnails alexnails added the run-ci CI: run the baseline test suite on this PR label Aug 18, 2026
@cauphenuny

Copy link
Copy Markdown
Contributor Author

/rerun-failed-ci

@cauphenuny
cauphenuny force-pushed the pr/minicpm-sala/main branch from 956b9f3 to fbd5d8c Compare August 20, 2026 07:38
@cauphenuny

Copy link
Copy Markdown
Contributor Author

@DarkSharpness Thanks! We have resolved these issues. Please take a look when you are available.

@DarkSharpness DarkSharpness left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM on JIT kernel side

alexnails and others added 8 commits August 20, 2026 14:30
The five test files added in this branch are pytest-style with no
`if __name__ == "__main__":` block. `collect_tests` treats that as a
hard error, so every Stage A job collecting the base-a-test-cpu file
list died before running a single test.

Add `sys.exit(pytest.main([__file__, "-v"]))` (wrapped, so a failing
test propagates its exit code to the CI runner) plus the `sys` and
`pytest` imports each file was missing.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
…leLang compile stall with mutable local scalars

TileLang 0.1.12 propagates dynamic loop-bound constraints to Z3 via
tile-ai/tvm@3a10c4d. Represent the reassigned sequence lengths as mutable
kernel-local scalars to avoid expanding conditional expressions throughout
layout inference.
@cauphenuny

Copy link
Copy Markdown
Contributor Author

/rerun-failed-ci

2 similar comments
@cauphenuny

Copy link
Copy Markdown
Contributor Author

/rerun-failed-ci

@zhaochenyang20

Copy link
Copy Markdown
Collaborator

/rerun-failed-ci

Resolve attention_registry.py: main added attn_backend_wrapper_for_draft_decode
and this branch added the two MiniCPM sparse backend registrations at the same
spot. Both are kept.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@alexnails
alexnails merged commit 092d85e into sgl-project:main Aug 24, 2026
16 of 20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

jit-kernel memory-pool run-ci CI: run the baseline test suite on this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants