mcpField
S
sglang-skill

sglang-skill

github.com/slowlyc開発17294 回インストール12 週間前に更新
リポジトリ

sglang-skill について

sglang-skill は、SGLang を開発する人向けのソース案内および開発ガイドです。ローカルのコードベースにあるランタイム、カーネル、モデルなどの場所を示し、拡張やデバッグの手順を説明します。

主な機能

  1. 01SGLang のソースパスと検索案内
  2. 02ランタイムとカーネルの参照先
  3. 03モデルと量子化の開発手順
  4. 04サーバー起動と Engine API の例

活用シーン

  1. 01Attention バックエンドやスケジューラーの場所を探す
  2. 02モデルや量子化方式を追加する
  3. 03キャッシュ、MoE、分散処理のコードを追う

インストール

コマンドをコピーしてターミナルで実行します。

$npx skills add https://github.com/slowlyc/agent-gpu-skills --skill sglang-skill

インストール後、Claude Code で/sglang-skillを入力して読み込まれたか確認します。

同梱ファイルupdate-sglang.sh

詳細

SGLang の開発

ソースコードの場所

SGLang のソースコードは、このスキルのインストールディレクトリ内の repos/sglang/ にあります。 実際のパスは使用するツールによって異なります。

  • Cursor:~/.cursor/skills/sglang-skill/repos/sglang/
  • Claude Code:~/.claude/skills/sglang-skill/repos/sglang/
  • Codex:~/.codex/skills/sglang-skill/repos/sglang/

SGLANG_REPO:以下の例では ~/.cursor/skills/sglang-skill/repos/sglang/ をプレースホルダーとして使用しています。実際のパスに置き換えてください。

このパスが存在しない場合は、プロジェクトディレクトリで bash update-repos.sh sglang を実行してください。

コアランタイム(SRT)
SGLANG_REPO/python/sglang/srt/
├── layers/
│   ├── attention/          # Attention backends
│   │   ├── flashinfer_backend.py      # FlashInfer (默认)
│   │   ├── flashinfer_mla_backend.py  # FlashInfer MLA (DeepSeek)
│   │   ├── cutlass_mla_backend.py     # CUTLASS MLA
│   │   ├── flashattention_backend.py  # FlashAttention
│   │   ├── triton_backend.py          # Triton attention
│   │   ├── flashmla_backend.py        # FlashMLA
│   │   ├── nsa_backend.py             # Native Sparse Attention
│   │   ├── tbo_backend.py             # TBO
│   │   ├── fla/                       # Flash Linear Attention
│   │   ├── triton_ops/                # Triton attention ops
│   │   └── wave_ops/                  # Wave attention ops
│   ├── moe/                # MoE routing and dispatch
│   ├── quantization/       # FP8, GPTQ, AWQ, Marlin, etc.
│   ├── deep_gemm_wrapper/  # DeepGEMM 集成
│   └── utils/
├── models/                 # 模型实现 (LLaMA, DeepSeek, Qwen, etc.)
│   └── deepseek_common/    # DeepSeek V2/V3 共享组件
├── managers/               # Scheduler, TokenizerManager, Detokenizer
├── mem_cache/              # KV cache, Radix cache
├── model_executor/         # 模型执行器, forward batch
├── model_loader/           # 模型加载, 权重映射
├── entrypoints/            # 启动入口: Engine, OpenAI API server
├── speculative/            # Speculative decoding
├── disaggregation/         # Disaggregated prefill/decode
├── distributed/            # TP/PP/EP 分布式
├── compilation/            # CUDA Graph, Torch.compile
├── configs/                # 模型配置
├── lora/                   # LoRA 推理
├── eplb/                   # Expert-level load balancing
├── hardware_backend/       # 硬件适配 (CUDA, ROCm, XPU)
└── utils/                  # 工具函数
JIT カーネル(Python CUDA/Triton カーネル)
SGLANG_REPO/python/sglang/jit_kernel/
├── flash_attention/        # Flash Attention 自定义实现
├── flash_attention_v4.py   # Flash Attention v4
├── cutedsl_gdn.py          # CuTeDSL GDN kernel
├── concat_mla.py           # MLA concat kernel
├── norm.py                 # Normalization kernels
├── rope.py                 # RoPE position encoding
├── pos_enc.py              # Position encoding
├── per_tensor_quant_fp8.py # FP8 量化
├── kvcache.py              # KV cache kernels
├── hicache.py              # HiCache kernels
├── gptq_marlin.py          # GPTQ Marlin kernel
├── cuda_wait_value.py      # CUDA sync primitives
└── diffusion/              # Diffusion model kernels
sgl-kernel(C++/CUDA カスタムカーネル)
SGLANG_REPO/sgl-kernel/
├── csrc/
│   ├── attention/          # Custom attention CUDA kernels
│   ├── cutlass_extensions/ # CUTLASS GEMM extensions
│   ├── gemm/               # GEMM kernels
│   ├── moe/                # MoE dispatch/combine kernels
│   ├── quantization/       # Quantization CUDA kernels
│   ├── allreduce/          # AllReduce CUDA kernels
│   ├── speculative/        # Speculative decoding kernels
│   ├── kvcacheio/          # KV cache I/O
│   ├── mamba/              # Mamba SSM kernels
│   ├── memory/             # Memory management
│   └── grammar/            # Grammar-guided generation
├── include/                # C++ headers
├── python/                 # Python bindings
├── tests/                  # Kernel tests
└── benchmark/              # Kernel benchmarks
フロントエンド言語
SGLANG_REPO/python/sglang/lang/   # SGLang 前端 DSL
SGLANG_REPO/examples/             # 使用示例
SGLANG_REPO/benchmark/            # 性能基准
SGLANG_REPO/test/                 # 测试套件
SGLANG_REPO/docs/                 # 文档

検索方法

Grep ツールで検索してください。ファイル全体を読み込まないでください。

Attention と MLA
SGLANG_REPO="$HOME/.cursor/skills/sglang-skill/repos/sglang"

# 查找 attention backend 注册
rg "register\|Backend" $SGLANG_REPO/python/sglang/srt/layers/attention/attention_registry.py

# 查找 FlashInfer MLA 实现
rg "forward\|mla" $SGLANG_REPO/python/sglang/srt/layers/attention/flashinfer_mla_backend.py

# 查找 CUTLASS MLA
rg "cutlass\|mla" $SGLANG_REPO/python/sglang/srt/layers/attention/cutlass_mla_backend.py

# 查找 attention 通用接口
rg "class.*Backend\|def forward" $SGLANG_REPO/python/sglang/srt/layers/attention/base_attn_backend.py
Scheduler と Batching
# Scheduler 核心逻辑
rg "class Scheduler\|def get_next_batch" $SGLANG_REPO/python/sglang/srt/managers/

# Continuous batching 和 chunked prefill
rg "chunk\|prefill\|extend" $SGLANG_REPO/python/sglang/srt/managers/

# CUDA Graph
rg "cuda_graph\|CudaGraph" $SGLANG_REPO/python/sglang/srt/compilation/
KV Cache と Memory
# Radix cache 实现
rg "RadixCache\|radix" $SGLANG_REPO/python/sglang/srt/mem_cache/

# KV cache 管理
rg "class.*Pool\|allocate\|free" $SGLANG_REPO/python/sglang/srt/mem_cache/

# HiCache (hierarchical cache)
rg "HiCache\|hicache" $SGLANG_REPO/python/sglang/srt/mem_cache/
モデル関連
# 查找特定模型实现
rg "class.*ForCausalLM" $SGLANG_REPO/python/sglang/srt/models/

# DeepSeek V2/V3 实现
rg "DeepSeek\|MLA\|MoE" $SGLANG_REPO/python/sglang/srt/models/deepseek_v2.py

# 模型加载和权重映射
rg "load_weight\|weight_map" $SGLANG_REPO/python/sglang/srt/model_loader/
MoE
# MoE routing
rg "TopK\|router\|expert" $SGLANG_REPO/python/sglang/srt/layers/moe/

# MoE CUDA kernels
rg "moe" $SGLANG_REPO/sgl-kernel/csrc/moe/
量子化
# FP8 量化
rg "fp8\|float8" $SGLANG_REPO/python/sglang/srt/layers/quantization/

# GPTQ/AWQ/Marlin
rg "gptq\|awq\|marlin" $SGLANG_REPO/python/sglang/srt/layers/quantization/
Speculative Decoding
rg "speculative\|draft\|verify" $SGLANG_REPO/python/sglang/srt/speculative/
分散処理
# TP/PP/EP
rg "tensor_parallel\|pipeline_parallel\|expert_parallel" $SGLANG_REPO/python/sglang/srt/distributed/

# Disaggregated serving
rg "disagg\|prefill_worker\|decode_worker" $SGLANG_REPO/python/sglang/srt/disaggregation/

ソースの使い分け

必要なものソースパス
Attention バックエンドのインターフェースSRT layerssrt/layers/attention/base_attn_backend.py
FlashInfer attentionSRT layerssrt/layers/attention/flashinfer_backend.py
MLA(DeepSeek)SRT layerssrt/layers/attention/*mla*.py
MoE のルーティング/ディスパッチSRT layerssrt/layers/moe/
量子化(FP8/GPTQ/AWQ)SRT layerssrt/layers/quantization/
SchedulerSRT managerssrt/managers/
KV cache/Radix cacheSRT mem_cachesrt/mem_cache/
モデルの実装SRT modelssrt/models/
DeepSeek V2/V3SRT modelssrt/models/deepseek_v2.py, deepseek_common/
Speculative decodingSRT speculativesrt/speculative/
Disaggregated servingSRT disaggsrt/disaggregation/
TP/PP/EP 分散処理SRT distributedsrt/distributed/
CUDA GraphSRT compilationsrt/compilation/
モデルの読み込みSRT model_loadersrt/model_loader/
起動エントリーポイントSRT entrypointssrt/entrypoints/
JIT Triton カーネルjit_kerneljit_kernel/
カスタム CUDA カーネルsgl-kernelsgl-kernel/csrc/
CUTLASS 拡張機能sgl-kernelsgl-kernel/csrc/cutlass_extensions/
フロントエンド DSLlangpython/sglang/lang/
使用例examplesexamples/

よくある開発シナリオ

新しい Attention Backend の追加
  1. base_attn_backend.py の AttnBackend を継承します
  2. forward() メソッドを実装します
  3. attention_registry.py に登録します
  4. テンプレートとして flashinfer_backend.py を参照します
新しいモデルの追加
  1. srt/models/ にモデルファイルを作成します
  2. ForCausalLM クラスを実装します
  3. load_weights() メソッドを実装します
  4. テンプレートとして srt/models/llama.py を参照します
新しい量子化方式の追加
  1. srt/layers/quantization/ に量子化モジュールを追加します
  2. 量子化ファクトリーに登録します
  3. fp8_kernel.py または gptq.py を参照します

起動とデバッグ

# 启动 OpenAI 兼容 API server
python -m sglang.launch_server --model-path meta-llama/Meta-Llama-3-8B-Instruct --tp 1

# 使用 Engine API (Python)
from sglang import Engine
engine = Engine(model_path="meta-llama/Meta-Llama-3-8B-Instruct")

# Profiling
python -m sglang.launch_server --model-path ... --enable-torch-compile
nsys profile -o report python -m sglang.launch_server ...

SGLang のソースコードを更新する

# 在 cursor-gpu-skills 项目目录下
bash update-repos.sh sglang

参考資料

コメント