diff --git a/python/sglang/srt/utils/weight_checker.py b/python/sglang/srt/utils/weight_checker.py index 0fdacbca0a..0af086eac9 100644 --- a/python/sglang/srt/utils/weight_checker.py +++ b/python/sglang/srt/utils/weight_checker.py @@ -9,6 +9,7 @@ from pydantic import BaseModel, ConfigDict from sglang.srt.managers.mm_utils import tensor_hash from sglang.srt.mem_cache.storage.mmap.mmap_allocator import alloc_mmap +from sglang.srt.platforms import current_platform from sglang.srt.runtime_context import get_parallel from sglang.srt.utils.weight_checker_comparator import ( CHUNK_NUMEL, @@ -205,7 +206,7 @@ class WeightChecker: def _compute_checksum( self, skip_tensor_list: Optional[List[str]] = None, *, role: str ) -> Dict: - torch.cuda.synchronize() + current_platform.synchronize() start = time.perf_counter() quantized_set = _build_quantized_set(self._get_model()) @@ -222,7 +223,7 @@ class WeightChecker: overall = overall_checksum(checksums) - torch.cuda.synchronize() + current_platform.synchronize() elapsed = time.perf_counter() - start logger.info( f"[WeightChecker] checksum computed for {len(checksums)} tensors in {elapsed:.3f}s" diff --git a/test/registered/unit/utils/test_weight_checker_cpu.py b/test/registered/unit/utils/test_weight_checker_cpu.py new file mode 100644 index 0000000000..b8918fa7b4 --- /dev/null +++ b/test/registered/unit/utils/test_weight_checker_cpu.py @@ -0,0 +1,32 @@ +"""WeightChecker checksum on a non-CUDA device (CPU stands in for TPU).""" + +import unittest +from unittest.mock import patch + +import torch +from torch import nn + +from sglang.srt.utils.weight_checker import ChecksumInfo, WeightChecker +from sglang.test.ci.ci_register import register_cpu_ci +from sglang.test.test_utils import CustomTestCase, enter_scope, published_topology + +register_cpu_ci(est_time=5, suite="base-a-test-cpu") + + +class TestChecksumWithoutCuda(CustomTestCase): + def test_checksum_does_not_require_cuda(self): + """The checksum action must work on hosts without CUDA, e.g. TPU engines + checked by an RL trainer after every weight update.""" + model = nn.Linear(4, 4) + enter_scope(self, published_topology()) + checker = WeightChecker(get_model=lambda: model) + with patch.object( + torch.cuda, "synchronize", side_effect=RuntimeError("no CUDA device") + ): + out = checker.handle("checksum", role="target") + info = ChecksumInfo.model_validate(out) + self.assertEqual(set(info.checksums), {"weight", "bias"}) + + +if __name__ == "__main__": + unittest.main()