--- title: "SGLang Plugin System" metatags: description: "Allows hardware vendors and developers to extend SGLang without modifying the main repository code." --- ## Overview Allows hardware vendors and developers to extend SGLang **without modifying the main repository code**. The framework provides two plugin types, both discovered via Python's standard `setuptools` entry_points:
| Plugin Type | Entry Point Group | Purpose |
|---|---|---|
| Hardware Platform Plugin | sglang.srt.platforms |
Register a custom hardware platform (device operations, KV cache pools, attention backends, graph capture, compilation backends, etc.) |
| General Plugin | sglang.srt.plugins |
Inject hooks (before/after/around/replace) into any function/method, or replace entire classes |
| Call Site | Process | Timing |
|---|---|---|
cli/serve.py serve() |
Main | Before prepare_server_args() |
launch_server.py __main__ |
Main | Before prepare_server_args() |
engine.py _launch_subprocesses() |
Main | Before server_args.check_server_args() |
scheduler.py run_scheduler_process() |
Subprocess | Before Scheduler() construction |
| Method | Default | Description |
|---|---|---|
is_cuda() |
Based on _enum |
Whether this is an NVIDIA CUDA platform |
is_rocm() |
Based on _enum |
Whether this is an AMD ROCm platform |
is_npu() |
Based on _enum |
Whether this is a Huawei NPU platform |
is_cpu() |
Based on _enum |
Whether this is a CPU-only platform |
is_xpu() |
Based on _enum |
Whether this is an Intel XPU platform |
is_musa() |
Based on _enum |
Whether this is a Moore Threads MUSA platform |
is_cuda_alike() |
CUDA+ROCM+MUSA | True if the hardware supports CUDA-like APIs |
is_out_of_tree() |
True for OOT |
Automatically detected based on _enum = PlatformEnum.OOT |
| Method | Default | Status | Description |
|---|---|---|---|
get_device(local_rank) |
raise NotImplementedError |
Planned | Return torch.device for a given local rank |
set_device(device) |
raise NotImplementedError |
Planned | Set the current device |
get_device_name(device_id) |
raise NotImplementedError |
Planned | Get human-readable device name |
get_device_uuid(device_id) |
raise NotImplementedError |
Planned | Get unique device identifier |
get_device_capability(device_id) |
raise NotImplementedError |
Planned | Get DeviceCapability(major, minor). None if N/A |
empty_cache() |
pass |
Planned | Release cached device memory |
synchronize() |
pass |
Planned | Synchronize device operations |
get_device_total_memory(device_id) |
raise NotImplementedError |
Active | Get total device memory in bytes |
get_available_memory(device_id) |
raise NotImplementedError |
Planned | Return (free_bytes, total_bytes) |
get_current_memory_usage(device) |
raise NotImplementedError |
Active | Get current peak memory usage in bytes |
get_torch_distributed_backend_str() |
raise NotImplementedError |
Planned | Distributed backend string (e.g. "nccl", "hccl") |
get_communicator_class() |
None |
Planned | Platform-specific communicator class |
inference_mode() |
torch.inference_mode(True) |
Planned | Return inference mode context manager |
seed_everything(seed) |
Set random/np/torch seeds | Planned | Set random seeds for reproducibility |
verify_quantization(quant) |
pass |
Planned | Validate quantization method support |
get_cpu_architecture() |
Auto-detect x86/arm | Planned | Detect CPU architecture (CpuArchEnum) |
| Type | Description |
|---|---|
PlatformEnum |
Enumeration of platform types: CUDA, ROCM, CPU, XPU, MUSA, NPU, TPU, MPS, OOT, UNSPECIFIED |
CpuArchEnum |
CPU architecture: X86, ARM, UNSPECIFIED |
DeviceCapability |
NamedTuple(major, minor) with comparison support. Methods: as_version_str(), to_int() |
| Method | Default | Description |
|---|---|---|
support_cuda_graph() |
False |
Whether device graph capture is supported (plain CUDA graph) |
support_piecewise_cuda_graph() |
False |
Whether piecewise CUDA graph (torch.compile backend) is supported |
supports_fp8() |
False |
Whether FP8 quantization is supported |
is_pin_memory_available() |
True |
Whether pinned memory is available |
| Method | Default | Description |
|---|---|---|
get_default_attention_backend() |
raise NotImplementedError |
Default attention backend name |
get_graph_runner_cls() |
raise NotImplementedError |
Graph Runner class |
get_mha_kv_pool_cls() |
raise NotImplementedError |
MHA KV cache pool class |
get_mla_kv_pool_cls() |
raise NotImplementedError |
MLA KV cache pool class |
get_nsa_kv_pool_cls() |
raise NotImplementedError |
NSA KV cache pool class (DeepSeek V3.2) |
get_paged_allocator_cls() |
raise NotImplementedError |
Paged allocator class |
get_piecewise_backend_cls() |
raise NotImplementedError |
Piecewise compilation backend class |
get_compile_backend(mode) |
"inductor" |
Compilation backend string |
get_dispatch_key_name() |
"native" |
MultiPlatformOp dispatch key name |
| Method | Invocation Timing | Purpose |
|---|---|---|
apply_server_args_defaults(server_args) |
After ServerArgs parsing, in __post_init__ |
Set platform-specific defaults |
init_backend() |
In each worker, before model construction | One-time backend initialization |
| Variable | Description |
|---|---|
SGLANG_PLATFORM |
Select the platform plugin by entry_point name (e.g. kunlun, demo_cuda). When set, only the named plugin's activate() is called (front-loading filter) — other plugins are not touched. Additionally, general plugins (sglang.srt.plugins) from unselected platform packages are automatically skipped to avoid importing their dependencies. Required when multiple plugins would activate. Errors if the name is not found or if the plugin's hardware is unavailable. |
SGLANG_PLUGINS |
Comma-separated whitelist of general plugin names to load (group: sglang.srt.plugins). If unset, all discovered general plugins are loaded. |
| Hook Type | Signature | Description |
|---|---|---|
| BEFORE | fn(*args, **kwargs) -> (args, kwargs) \| None |
Runs before the original. Return None to keep args unchanged, or (args, kwargs) to modify. |
| AFTER | fn(result, *args, **kwargs) -> new_result \| None |
Runs after the original. Return None to keep result, or a new value to replace. |
| AROUND | fn(original_fn, *args, **kwargs) -> result |
Wraps the original. You must call original_fn yourself. Full control over execution. |
| REPLACE | fn(*args, **kwargs) -> result or class |
Replace the original function or class entirely. For class targets, pass a replacement class directly — it is substituted via setattr preserving isinstance()/issubclass() semantics. |
| Target | Description |
|---|---|
sglang.srt.server_args.ServerArgs.add_cli_args |
Add custom CLI arguments |
sglang.srt.server_args.ServerArgs.__post_init__ |
Modify ServerArgs after parsing |
sglang.srt.server_args.ServerArgs.check_server_args |
Add/relax validation |
sglang.srt.managers.scheduler.Scheduler.__init__ |
Custom scheduler state |
sglang.srt.managers.scheduler.Scheduler.get_next_batch_to_run |
Custom scheduling policy |
sglang.srt.managers.scheduler.Scheduler.run_batch |
Profiling / inspection |
sglang.srt.managers.scheduler.Scheduler.process_batch_result |
Custom metrics |
sglang.srt.managers.tp_worker.TpModelWorker.__init__ |
Custom worker state |
sglang.srt.managers.tp_worker.TpModelWorker.forward_batch_generation |
Forward pass wrapping |
| File | Description |
|---|---|
sglang/srt/platforms/device_mixin.py |
PlatformEnum + DeviceMixin base class |
sglang/srt/platforms/interface.py |
SRTPlatform base class (extends DeviceMixin) |
sglang/srt/platforms/__init__.py |
current_platform lazy singleton + discovery logic |
sglang/srt/plugins/__init__.py |
load_plugins() + load_plugins_by_group() |
sglang/srt/plugins/hook_registry.py |
HookRegistry, HookType, plugin_hook decorator |