How sgl-project/sglang works
SGLang serves LLM and multimodal models, with diffusion generation included in the Python package. The main workflow is to launch a model, accept a generation request, schedule model execution, and return a result; KV caching and optimized kernels support inference. Separate router and model-gateway projects provide deployment and request routing. The graph preserves documented diffusion and cache capabilities, but sampled source does not establish every runtime call path, so uncertain links are omitted.
Main entry points: schedule_batch.py Request Scheduler · engine.py Python Engine · composed_pipeline_base.py Composed Pipeline · transformers.py Model Execution
Components 27
Serving API
- Python Engine
python/sglang/srt/entrypoints/engine.py - Rust HTTP API
rust/sglang-server/src/api_server/http/app.rs - Rust gRPC API
rust/sglang-server/src/api_server/grpc/app.rs - API Handlers
rust/sglang-server/src/api_server/handlers.rs
Inference Runtime
- Request Scheduler
python/sglang/srt/managers/schedule_batch.py - Model Configuration
python/sglang/srt/configs/model_config.py - Model Execution
python/sglang/srt/models/transformers.py - Runtime Context
python/sglang/srt/runtime_context.py - Device Support
python/sglang/srt/utils/device.py - Optimized Kernels
python/sglang/kernels/ops - N-gram Embeddings
python/sglang/srt/model_executor/model_runner_components/ngram_embedding_manager.py
KV and Embedding Cache
- KV Storage
python/sglang/srt/mem_cache/kv/storage.py - Static KV Storage
python/sglang/srt/mem_cache/static_kv_storage.py - HiCache Storage
python/sglang/srt/mem_cache/hicache_storage.py - Buffer Transfer
python/sglang/srt/mem_cache/buffer_mode/pipeline.py - Embedding Cache
python/sglang/srt/mem_cache/embedding_cache_controller.py
Diffusion Generation
- Composed Pipeline
python/sglang/multimodal_gen/runtime/pipelines_core/composed_pipeline_base.py - Image Encoding
python/sglang/multimodal_gen/runtime/pipelines_core/stages/image_encoding.py - Component Residency
python/sglang/multimodal_gen/runtime/managers/memory_managers/component_residency_strategies.py - Component Manager
python/sglang/multimodal_gen/runtime/managers/memory_managers/component_manager.py - Diffusion Arguments
python/sglang/multimodal_gen/runtime/server_args/server_args.py
Routing
- SGL Router
sgl-router/src/main.rs - Worker Registry
sgl-router/src/workers/manager.rs - Engine Ranking
sgl-router/src/state/load_monitor/engine_ranking.rs - Model Gateway
sgl-model-gateway/src/main.rs - Gateway Routers
sgl-model-gateway/src/routers/router_manager.rs
Other components
- Client
Connections 23
- Client → Python Engine: launches model
README.md - Client → Rust HTTP API: sends request
README.md - Rust HTTP API → API Handlers: registers routes
rust/sglang-server/src/api_server/http/app.rs - Rust gRPC API → API Handlers: registers service
rust/sglang-server/src/api_server/grpc/app.rs - Python Engine → Request Scheduler: starts schedulers
python/sglang/srt/entrypoints/engine.py - Request Scheduler → Model Configuration: uses configuration
python/sglang/srt/managers/schedule_batch.py - Request Scheduler → Model Execution: runs model
- Request Scheduler → KV Storage: uses KV storage
python/sglang/srt/mem_cache/kv/storage.py - Request Scheduler → HiCache Storage: uses cache
- Buffer Transfer → HiCache Storage: transfers KV
python/sglang/srt/mem_cache/buffer_mode/pipeline.py - KV Storage → Static KV Storage: coordinates storage
python/sglang/srt/mem_cache/kv/storage.py - Model Execution → Optimized Kernels: executes kernels
- Model Execution → N-gram Embeddings: updates embeddings
- Embedding Cache → Optimized Kernels: copies embeddings
python/sglang/srt/mem_cache/embedding_cache_controller.py - Python Engine → Runtime Context: configures runtime
python/sglang/srt/entrypoints/engine.py - Python Engine → Device Support: selects device
- Composed Pipeline → Image Encoding: composes stages
- Component Manager → Component Residency: manages residency
python/sglang/multimodal_gen/runtime/managers/memory_managers/component_manager.py - Composed Pipeline → Component Manager: loads components
python/sglang/multimodal_gen/runtime/pipelines_core/composed_pipeline_base.py - Composed Pipeline → Diffusion Arguments: uses arguments
- SGL Router → Worker Registry: manages workers
- Worker Registry → Engine Ranking: updates ranking
sgl-router/src/state/load_monitor/engine_ranking.rs - Model Gateway → Gateway Routers: starts routing