Investigated only local files/docs under /user_4813494d/openbmb; no edits made.
Prefill Dataflow
- Submission flags come from demo-sala/prepare_env.sh: relevant settings are
--attention-backend minicpm_flashinfer, --chunked-prefill-size 8192, --dense-as-sparse, --quantization modelopt_fp4.
minicpm_flashinfer is accepted by server args at server_args.py, chunk size is parsed at server_args.py, and validated as divisible by page size at server_args.py.
- Backend registration maps both MiniCPM backend names to
MiniCPMSparseBackend at attention_registry.py.
- Scheduler chunking uses
PrefillAdder; long requests are truncated to the remaining chunk token budget at schedule_policy.py and schedule_policy.py.
ScheduleBatch.prepare_for_extend() builds input_ids, seq_lens, prefix_lens, sparse K1/K2 token counts, and KV allocation at schedule_batch.py.
ForwardBatch moves extend metadata to GPU and computes positions at forward_batch_info.py.
ModelRunner.forward_extend() initializes attention metadata, then calls model forward at model_runner.py.
- Each MiniCPM layer runs norm → attention/mixer → residual → norm → MLP at minicpm.py.
- Standard attention path: qkv projection and RoPE happen in minicpm.py, then
RadixAttention dispatches to the active backend at radix_attention.py.
dense-as-sparse sets dense_len=0, so every standard attention layer is sparse-routed at minicpm_backend.py.
Sparse Attention Path
init_forward_metadata() builds base page table and extend cu-seqlens at minicpm_backend.py.
update_batch_for_sparse() builds K1/K2 compression metadata, sparse batch list, token-to-batch maps, sparse page-table shell, and stage1 lengths at minicpm_backend.py.
- All-sparse fast path creates one sparse row per token/head-group and sets row query length to 1 at minicpm_sparse_utils.py.
- Per standard layer, KV is written first at minicpm_backend.py.
- TopK is computed from compressed K1/K2 via
compressed_attention() at minicpm_backend.py and minicpm_sparse_utils.py.
- Stage1 score, pooling, and TopK selection are at minicpm_sparse_utils.py, minicpm_sparse_utils.py, and minicpm_sparse_utils.py.
- Direct TopK → FlashInfer metadata is at minicpm_sparse_stage2.py and used from minicpm_attention_kernels.py.
- FlashInfer plan/cache/forward happens in minicpm_attention_kernels.py and .
Current Hotspots
Docs identify the main target as long prefill with 8192 chunks, line 39, about 15 chunks, profiled via SGLANG_MINICPM_PROFILE=1 at docs/prefill/current.md.
Hot buckets:
- MLP: ~133 ms/chunk, mostly FP4
gate_up plus fused activation/quant/down path, docs/prefill/current.md.
- GLA: ~120 ms/chunk, qkv and chunk kernel dominant, docs/prefill/current.md.
- Sparse FlashInfer stage2: 8 sparse layers × ~9.8-10 ms, docs/prefill/current.md.
- Stage1 scoring can show up as ~74 ms/chunk in macro profile, docs/prefill/current.md.
- Lower-level nsys notes say the most reliable GPU-side hotspots are NVFP4 GEMM and FlashInfer paged sparse attention; host overhead is also material, docs/prefill/history.md.
Existing Optimizations
- FlashInfer plan cache and cross-forward buffer refresh: minicpm_attention_kernels.py.
- All-sparse metadata fast path: minicpm_sparse_utils.py.
- Derived sparse seqlens: minicpm_backend.py.
- Direct sparse page table and direct TopK-to-FlashInfer indices: minicpm_backend.py, minicpm_sparse_stage2.py.
- Stage1 actual maxlen/direct-pool/skip-extra-zero: minicpm_sparse_utils.py.
- GLA qkv cuDNN shape gate and prewarm: modelopt_quant.py, modelopt_quant.py.
- GLA fused qk_norm+RoPE and fused output norm/gate: minicpm.py, minicpm.py.
- MLP fused SwiGLU + FP4 quant feeding pre-quantized down GEMM: minicpm.py, modelopt_quant.py.
Dead Ends
Already documented as poor ROI or unsafe: TopK/candidate shrink, chunked-prefill-size 16384, broad MLP cuDNN routing, compressed-maxlen old path, TileLang fused TopK path, stage1 dtype/mask shortcuts, GLA chunk API swaps, stage2 blocksplit/page64 two-wrapper route, simple Triton stage2, dense-as-MoE MLP, and FP4-before-SwiGLU intermediate quantization. See docs/prefill/current.md through docs/prefill/current.md.
Ordered Opportunity List
- MLP gated FP4 epilogue: highest leverage. Avoid materializing BF16
gate_up and doing a separate SwiGLU+quant launch. Code: minicpm.py, modelopt_quant.py. Design notes at docs/prefill/current.md.
- Single-path sparse stage2 layout/kernel: current FlashInfer knobs are mostly exhausted; remaining upside needs a KV/block layout or exact custom sparse kernel, not more wrapper tuning. Code: minicpm_backend.py, minicpm_attention_kernels.py.
- Exact fused stage1 score→pool→TopK: worthwhile only if it preserves full K1/K2 semantics and exact TopK. Current implementation spans minicpm_sparse_utils.py to minicpm_sparse_utils.py.
- Host/API overhead cleanup: chase repeated graph instantiation and lazy library load noted in nsys, plus any remaining plan misses. Start at model_runner.py, minicpm_attention_kernels.py, modelopt_quant.py.
- Metadata CPU/GPU overlap: higher risk, but scheduler/sparse metadata work is still serial around GPU work. Relevant files: schedule_policy.py, minicpm_sparse_utils.py, .