Install SGLang from Source
Prepare Environment
Before contributing, please ensure that your environment is set up correctly. Follow the steps in the Installation Guide to install the necessary dependencies. We recommend using docker to build the environment.Fork and clone the repository
Note: New contributors do not have the write permission to push to the official SGLang repo. Please fork the repository under your GitHub account, then clone your fork locally.Format code with pre-commit
We use pre-commit to maintain consistent code style checks. Before pushing your changes, please run:pre-commit run --all-filesmanually runs all configured checks, applying fixes if possible. If it fails the first time, re-run it to ensure lint errors are fully resolved. Make sure your code passes all checks before creating a Pull Request.- Do not commit directly to the
mainbranch. Always create a new branch (e.g.,feature/my-new-feature), push your changes, and open a PR from that branch. - Link checking with lychee is enforced in CI. By default, it is not blocking local commits.
- To run local link checks manually, use:
pre-commit run --hook-stage manual lychee --all-files.
Run and add tests
All NPU tests are end-to-end (E2E) and require launching a server with real model weights. Tests live undertest/registered/ascend/, organized by model type and functionality:
Adding a test
Seetest_npu_sampling_backend.py for a complete example. Key steps:
- Place your test file in the appropriate directory under
test/registered/ascend/. - Extend
CustomTestCase(fromsglang.test.test_utils) for CI retry support. - Launch server with
popen_launch_server()insetUpClassand clean up withkill_process_tree()intearDownClass. - Register your test with
register_npu_ci():
Running tests locally
Register models for CI
If you need to use model which is not inpython/sglang/test/ascend/test_ascend_utils.py list. Follow these steps:
- Register account and upload your model to modelscope.
-
Make sure your model is pre-cached on the CI server and is at the path “/data/ascend-ci-share-pkking-sglang/modelscope/hub/models//”.
If this is not the case, use following command on CI server:
Note: If you don’t have access to CI server, please ask maintainers (zl19940307@163.com) to download your model.
- Add model to
python/sglang/test/ascend/test_ascend_utils.py(use docker"/root/.cache/modelscope/hub/models/{your_model_repo}/{your_model}"path).
Write documentation
We recommend new contributors start by writing documentation, which helps you quickly understand SGLang codebase. For more details, please refer to docs/README.md.Test the accuracy
If your code changes the model output, please run the accuracy tests. A quick sanity check is the few-shot GSM8K.Benchmark the speed
Refer to Benchmark and Profiling.Requesting a review for merge
You can follow the pull request merge process described in MAINTAINER.md. You will need to work with the Merge Oncall, Codeowner, and other reviewers to get their approvals. Then your PR can be merged.How to Trigger CI Tests
We have a lot of open PRs but limited CI machines, so only top and trusted contributors have permission to trigger CI tests. Users with permission are listed in the CI_PERMISSIONS.json PR authors can always use/rerun-failed-ci on their own PRs, even if they are not listed in CI_PERMISSIONS.json.
For CI to run on a pull request, it must have the “run-ci” label. Authorized users can add the label or rerun failed tests by commenting on the PR with one of these commands:
/tag-run-ci-label: Adds the “run-ci” label. Only future commits trigger CI; the current commit is unaffected. Add theextraargument (/tag-run-ci-label extra) to additionally apply the “run-ci-extra” label, opting the PR into the extra test workflow (pr-test-extra.yml)./rerun-failed-ci: Reruns workflows from the latest commit with conclusion failed, flaky, or skipped./tag-and-rerun-ci: Runs both. Use this on a fresh PR to kick off CI on the current commit —/tag-run-ci-labelalone won’t. Accepts the sameextraargument (/tag-and-rerun-ci extra)./rerun-stage <stage-name>: Reruns a single test stage without waiting for its dependencies. Useful for quickly validating a specific test fix instead of waiting ~30 minutes for preceding stages./rerun-test <test-spec> [<test-spec> ...]: Reruns one or more specific tests directly, bypassing stage boundaries. Each<test-spec>is pytest-style<file>::<TestClass>[.<test_method>](the::TestClassand.<test_method>parts are optional). The handler resolves each spec, groups specs by their registered runner-label, and dispatches one Rerun Test workflow per group. Examples:/rerun-test test_srt_endpoint.py,/rerun-test registered/core/test_srt_endpoint.py::TestSRTEndpoint.test_simple_decode,/rerun-test test_a.py test_b.py(multiple at once).
/rerun-failed-ci comments, you can also trigger the command by editing an existing comment and adding any suffix (e.g., /rerun-failed-ci try again).
Example of rerunning a single test stage: /rerun-stage unit-test-backend-4-gpu.
If you don’t have permission, please ask maintainers to trigger CI for you.
CI rate limits
Due to CI scheduling and limited resources, higher-priority PRs may preempt running jobs. In such cases, you may need to rerun the tests. We apply CI rate limits to prevent abuse and ensure fair usage of our CI resources. Each CI workflow has a default limit defined in its workflow configuration file. For example, in pr-gate.yml, the default cooldown period is 120 minutes, and each workflow can override it via thecool-down-minutes input parameter:
Code style guidance
- Avoid code duplication. If the same code snippet (more than five lines) appears multiple times, extract it into a shared function.
- Minimize device synchronization. Reduce expensive CPU-NPU synchronization operations, such as
tensor.item()ortensor.cpu(), whenever possible. Use vectorized code. - Prioritize extreme efficiency. SGLang is a runtime, and most of your code runs on the critical path for every request. Optimize all minor overheads as much as possible, especially in the model forward code.
- A common pattern is some runtime checks in the model forward pass (e.g., this). These are very likely the same for every layer. Please cache the result as a single boolean value in
__init__whenever possible.
- A common pattern is some runtime checks in the model forward pass (e.g., this). These are very likely the same for every layer. Please cache the result as a single boolean value in
- Make functions as pure as possible. Avoid in-place modification of arguments.
- Keep files concise. If a file exceeds 2,000 lines of code, split it into multiple smaller files. (e.g.,
scheduler.py,scheduler_pp_mixin.py) - In a file, put core data structures at the top of the file. Put utility functions at the bottom of the file.
- Keep tests run fast.
- If a single test file run longer than 500 seconds, split it into multiple smaller files (e.g.,
test_eagle_infer_a.py,test_eagle_infer_b.py). - If a single job in a github workflow runs longer than 30 min, split it into smaller jobs/steps.
- Reuse server launches in your unit tests to make tests run faster.
- If a single test file run longer than 500 seconds, split it into multiple smaller files (e.g.,
- Never use
pickle.loads(),pickle.load(), orrecv_pyobj()to deserialize untrusted or network-received data. Python’s pickle module is not secure — it can execute arbitrary code during deserialization. Use safe serialization formats such as msgpack or JSON instead. - When supporting new hardware or features, follow these guidelines:
- Do not drastically change existing code.
- Always prefer new files to introduce specific components for your new hardware (e.g.,
allocator_npu.py). - If you write multiple if/else blocks for new features, ensure the common path (e.g., NVIDIA hardware or the existing code path) is the first branch.
How to update sgl-kernel-npu
Sgl-kernel-npu is the separate kernel package for Ascend NPU, containing both Ascend C and Triton operators. It is maintained in the sgl-kernel-npu repository. For detailed guidance on developing and integrating operators (Ascend C directory structure, PyTorch op registration, build, test, and code style), see the Ascend NPU Operator Development Guide.Multi-PR workflow
Since SGLang and sgl-kernel-npu are separate Python packages, dependency updates require a multi-PR workflow:- Submit sgl-kernel-npu PR: Add or modify operators in the sgl-kernel-npu repository following the operator development guide. Ensure all tests pass.
- Bump sgl-kernel-npu version: Update the version number. Merging triggers an automatic PyPI release. If not urgent, wait for a regular release (typically within one week).
- Reference the new version in SGLang:
- Update the
SGLANG_KERNEL_NPU_TAGargument indocker/npu.Dockerfileto the new sgl-kernel-npu release tag. - Use the new operator in SGLang code.
- Update the
Tips for newcomers
If you want to contribute but don’t have a specific idea in mind, pick issues labeled “good first issue” or “help wanted”. These tasks typically have lower complexity and provide an excellent introduction to the codebase. Also check out the following materials as startup guide:- Mini-SGLang for a quick overview on the structure of sglang.
- Code Walk-through for a deeper look into SGLang’s workflow.
- GTC-2026 Training Lab for hands-on practices of how to do optimization, benchmarking, or profiling on a launched SGLang instance.
