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.
Run and add unit tests
If you add a new feature or fix a bug, please add corresponding unit tests to ensure coverage and prevent regression. SGLang uses Python’s built-in unittest framework. For detailed instructions on running tests and integrating them into CI, refer to test/README.Write documentations
We recommend new contributors start from writing documentation, which helps you quickly understand SGLang codebase. For more details, please refer to docs/README.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. 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 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. Every future commit will trigger CI./rerun-failed-ci: Reruns the failed or flaky tests from the most recent commit./tag-and-rerun-ci: A single command that performs both/tag-run-ci-labeland/rerun-failed-ci./rerun-stage <stage-name>: Reruns a specific test stage without waiting for its dependencies. This is useful when you want to quickly validate a fix for a specific test failure instead of waiting ~30 minutes for preceding stages to complete.
/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-GPU 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 whenever possible.
- 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_output_processor_mixin.py) - 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 mins, 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.,
- 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_ascend.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
Since sglang and sgl-kernel are separate Python packages, our current GitHub CI infrastructure does not support updating a kernel and using it immediately within the same pull request (PR). To add a new kernel or modify an existing one in the sgl-kernel package, you must use multiple PRs. Follow these steps:- Submit a PR to update the sgl-kernel source code without using it in sglang python package (e.g., #8884).
- Bump the version of sgl-kernel (e.g., #9220).
- Once merged, this will trigger an automatic release of the sgl-kernel wheel to PyPI.
- If not urgent, you can wait for other people to release the wheel. A new version will typically be released within one week.
- Apply the changes:
- Update the sgl-kernel version in
sglang/python/pyproject.tomlto use the modified kernels. - Update the related caller code in the sglang to use the new kernel.
- Update the sgl-kernel version in
