본문으로 건너뛰기

[SGLang] 프로젝트 전체 아키텍처 분석 - 개요 및 목차

SGLang이란

SGLang은 LMSYS(Large Model Systems Organization)에서 개발한 LLM 추론/서빙 오픈소스 프로젝트다. "Fast serving framework for large language models and vision language models"을 표방하며, 프론트엔드 DSL(Domain-Specific Language)과 고성능 런타임을 모두 제공하는 것이 특징이다.

핵심 논문: SGLang: Efficient Execution of Structured Language Model Programs (2023)

주요 성능 지표

기술 성능 향상 출처
RadixAttention (프리픽스 캐싱) 5x 추론 속도 향상 LMSYS Blog 2024/01
Compressed FSM (JSON 디코딩) 3x 더 빠른 구조화 출력 LMSYS Blog 2024/02
DeepSeek MLA 최적화 7x 어텐션 가속 LMSYS Blog 2024/09
torch.compile 통합 1.5x 컴파일 최적화 LMSYS Blog 2024/09
GB200 NVL72 PD+EP 3.8x Prefill, 4.8x Decode throughput LMSYS Blog 2025/09

SGLang vs vLLM 핵심 차이

항목 vLLM SGLang
핵심 캐싱 PagedAttention (블록 할당) RadixAttention (Radix Tree 프리픽스 공유)
프론트엔드 DSL 없음 SGL Language (gen, select, function)
스케줄러 Async Scheduling Zero-Overhead CPU Scheduler
Disaggregation KV Transfer Connectors Prefill-Decode 분리 서버
커스텀 커널 외부 의존 sgl-kernel (자체 C++/CUDA)

전체 아키텍처

 ┌──────────────────────────────────────────────────────────────────────────┐
                       클라이언트 (HTTP / gRPC / SDK)                       
 └────────────────────────────────┬─────────────────────────────────────────┘
                                  
                                  
 ┌──────────────────────────────────────────────────────────────────────────┐
   1. Entry Point                    python/sglang/srt/entrypoints/       
      HTTP Server (FastAPI) / Engine / OpenAI·Anthropic·Ollama API        
 └────────────────────────────────┬─────────────────────────────────────────┘
                                  
                                  
 ┌──────────────────────────────────────────────────────────────────────────┐
   2. Frontend Language                   python/sglang/lang/             
      SGL DSL: gen()  IR  Interpreter  Backend                        
 └────────────────────────────────┬─────────────────────────────────────────┘
                                  
                                  
 ┌──────────────────────────────────────────────────────────────────────────┐
   3. TokenizerManager                                                    
      비동기 토큰화  GenerateReqInput  TokenizedGenerateReqInput         
                                                                          
               ──── ZMQ IPC ────▶                                         
                                                                          
   4. Scheduler (Zero-Overhead CPU)       python/sglang/srt/managers/     
        ┌─────────────┐   ┌──────────────┐                               
        SchedulePolicy│──▶│ ScheduleBatch                               
        └──────┬───────┘   └──────┬───────┘                               
                                                                         
                                                                         
   ┌─────────────────────┐                                               
    5. RadixCache                                                      
       (KV Cache)                                                      
   └─────────────────────┘                                               
 └────────────────────────────────┼────────────────────────────────────────┘
                                  
                                  
 ┌──────────────────────────────────────────────────────────────────────────┐
   6. Attention Backends          python/sglang/srt/layers/attention/     
      FlashAttention  FlashInfer  MLA  NSA  Mamba  GDN  FLA  ...  
 ├─────────────────────────────────────────────────────────────────────────┤
   7. TP Worker  Model Runner                                           
      ForwardBatch  Model Forward  CUDA Graphs                         
 ├─────────────────────────────────────────────────────────────────────────┤
   8~9. Model Layers                                                      
      Quantization (FP8/FP4/AWQ/INT8) │ MoE (Fused/EP/CUTLASS)          
 └────────────────────────────────┬─────────────────────────────────────────┘
                                  
                                  
 ┌──────────────────────────────────────────────────────────────────────────┐
   10. Speculative Decoding (EAGLE/N-gram/DFlash)                         
   11. Constrained Decoding (XGrammar/Outlines/LLGuidance)                
                                                                          
   Sampler  BatchTokenIDOutput                                           
 └────────────────────────────────┬─────────────────────────────────────────┘
                                  
                                  
 ┌──────────────────────────────────────────────────────────────────────────┐
   DetokenizerManager ◀── ZMQ IPC ── 토큰  텍스트  HTTP Response       
 └──────────────────────────────────────────────────────────────────────────┘

 ┌──────────────────────────────────────────────────────────────────────────┐
   횡단 관심사 (Cross-Cutting Concerns)                                    
   12. Distributed (TP/PP/DP/EP)  13. Disaggregation (PD 분리)           
   14. LoRA Adapter Serving       16. Multimodal (Vision/Audio)          
 └──────────────────────────────────────────────────────────────────────────┘

프로세스 아키텍처

SGLang은 멀티프로세스 IPC(Inter-Process Communication) 구조를 사용한다. 각 프로세스는 ZMQ 소켓으로 통신하며, CPU 바운드 작업과 GPU 바운드 작업을 분리한다.

Main Process (HTTP/Engine)
├── FastAPI Server (port 30000)
└── TokenizerManager
        │
        ├──(ZMQ)──▶ Scheduler (subprocess)
        │               ├── SchedulePolicy
        │               ├── RadixCache
        │               └── TP Workers (GPU별 subprocess)
        │                       ├── Model Runner
        │                       └── CUDA Graph Runner
        │
        └──(ZMQ)──▶ DetokenizerManager (subprocess)

시리즈 목차

1. Entry Point & Server

2. Frontend Language (SGL DSL)

3. Tokenizer & Detokenizer

4. Scheduler

5. Memory & KV Cache

6. Attention Backends

7. Model Runner & Worker

8. Quantization

9. Mixture of Experts

10. Speculative Decoding

11. Constrained Decoding

12. Distributed Systems

13. Disaggregated Serving

14. LoRA Adapter Serving

15. Sampling & Output

16. Multimodal Processing

17. Model Layers & 기타

부록

핵심 데이터 흐름

GenerateReqInput → TokenizedGenerateReqInput → Req → ScheduleBatch → ForwardBatch → BatchTokenIDOutput → BatchStrOutput
단계 데이터 구조 위치
HTTP 입력 GenerateReqInput python/sglang/srt/managers/io_struct.py
토큰화 결과 TokenizedGenerateReqInput python/sglang/srt/managers/io_struct.py
스케줄러 요청 Req python/sglang/srt/managers/schedule_batch.py
배치 구성 ScheduleBatch python/sglang/srt/managers/schedule_batch.py
GPU 텐서 ForwardBatch python/sglang/srt/model_executor/forward_batch_info.py
토큰 출력 BatchTokenIDOutput python/sglang/srt/managers/io_struct.py
텍스트 출력 BatchStrOutput python/sglang/srt/managers/io_struct.py

관련 논문

논문 적용 계층
SGLang: Efficient Execution of Structured Language Model Programs 전체 아키텍처
FlashAttention: Fast and Memory-Efficient Exact Attention 어텐션 백엔드
Efficient Memory Management for LLM Serving with PagedAttention 메모리 관리
EAGLE: Speculative Sampling Requires Rethinking Feature Uncertainty 투기적 디코딩
DeepSeek-V2: A Strong, Economical, and Efficient MoE Language Model MLA, MoE
Sparse Flash Attention 스파스 어텐션

참고 자료

댓글

관련 포스트

SGLang 의 다른글