[CI] Fix gpu deps import in cpu test (#21950)

This commit is contained in:
Ke Bao
2026-04-03 00:06:31 +08:00
committed by GitHub
parent 083304ca44
commit b21db86e2f
5 changed files with 78 additions and 6 deletions
+27 -1
View File
@@ -32,7 +32,9 @@ Tests can use CPU or GPU — the key criterion is **no server process**.
diff-cover coverage.xml --compare-branch=origin/main --fail-under=60
```
## Example
## Examples
### Basic unit test
```python
"""Unit tests for <module> — no server, no model loading."""
@@ -57,6 +59,30 @@ if __name__ == "__main__":
unittest.main()
```
### Stubbing GPU-only imports for CPU tests
Some modules (e.g. `scheduler.py`, `io_struct.py`) transitively import packages like
`sgl_kernel` that require a GPU to initialize. To run pure-mock tests against these
modules on CPU-only CI, stub the problematic package **before** importing it.
`maybe_stub_sgl_kernel()` in `test_utils.py` does this for `sgl_kernel`: it's a no-op
on GPU machines, and on CPU it installs a `sys.meta_path` finder that auto-creates empty
stub modules for all `sgl_kernel.*` submodules.
```python
from sglang.test.ci.ci_register import register_cpu_ci
from sglang.test.test_utils import maybe_stub_sgl_kernel
maybe_stub_sgl_kernel() # must precede any import that pulls in sgl_kernel
from sglang.srt.managers.io_struct import FlushCacheReqInput
from sglang.srt.managers.scheduler import Scheduler
register_cpu_ci(est_time=2, suite="stage-a-test-cpu")
```
The same pattern can be applied to other GPU-only packages: try importing the real package, and if it fails, register a `sys.meta_path` finder that stubs it. See `maybe_stub_sgl_kernel()` in `python/sglang/test/test_utils.py` for the implementation.
## Rules
- **No** `popen_launch_server()` or `Engine(...)`.
@@ -1,11 +1,15 @@
import unittest
from unittest.mock import MagicMock, patch
from sglang.test.ci.ci_register import register_cpu_ci
from sglang.test.test_utils import maybe_stub_sgl_kernel
maybe_stub_sgl_kernel()
from sglang.srt.managers.io_struct import FlushCacheReqInput
from sglang.srt.managers.scheduler import Scheduler
from sglang.test.ci.ci_register import register_cpu_ci
register_cpu_ci(est_time=2, suite="stage-a-cpu-only")
register_cpu_ci(est_time=2, suite="stage-a-test-cpu")
class TestSchedulerFlushCache(unittest.TestCase):
@@ -2,11 +2,15 @@ import unittest
from collections import deque
from unittest.mock import MagicMock
from sglang.test.ci.ci_register import register_cpu_ci
from sglang.test.test_utils import maybe_stub_sgl_kernel
maybe_stub_sgl_kernel()
from sglang.srt.managers.io_struct import PauseGenerationReqInput
from sglang.srt.managers.scheduler import Scheduler
from sglang.test.ci.ci_register import register_cpu_ci
register_cpu_ci(est_time=2, suite="stage-a-cpu-only")
register_cpu_ci(est_time=2, suite="stage-a-test-cpu")
class TestSchedulerPauseGeneration(unittest.TestCase):