From e3961eb6fad022214b9fbe8cbbf12bc0dd6517c1 Mon Sep 17 00:00:00 2001 From: metamergebot Date: Thu, 15 Oct 2026 17:50:43 -0700 Subject: [PATCH] [Fix] utils: Allowlist torch.bcomplex32 in SafeUnpickler (#44440) Co-authored-by: fei-xx Co-authored-by: zx3xyy --- python/sglang/srt/utils/common.py | 1 + .../unit/utils/test_safe_unpickler.py | 29 ++++++++++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/python/sglang/srt/utils/common.py b/python/sglang/srt/utils/common.py index 04fc1026a4..a66496264a 100644 --- a/python/sglang/srt/utils/common.py +++ b/python/sglang/srt/utils/common.py @@ -3299,6 +3299,7 @@ class SafeUnpickler(pickle.Unpickler): ("torch", "Size"), ("torch", "device"), ("torch", "dtype"), + ("torch", "bcomplex32"), ("torch", "bfloat16"), ("torch", "bit"), ("torch", "bits16"), diff --git a/test/registered/unit/utils/test_safe_unpickler.py b/test/registered/unit/utils/test_safe_unpickler.py index 441c5c8b7a..0e7dade229 100644 --- a/test/registered/unit/utils/test_safe_unpickler.py +++ b/test/registered/unit/utils/test_safe_unpickler.py @@ -6,7 +6,11 @@ from types import SimpleNamespace import torch -from sglang.srt.utils.common import MultiprocessingSerializer, safe_pickle_loads +from sglang.srt.utils.common import ( + MultiprocessingSerializer, + SafeUnpickler, + safe_pickle_loads, +) from sglang.test.ci.ci_register import register_cpu_ci from sglang.test.test_utils import CustomTestCase @@ -57,6 +61,29 @@ class TestSafeUnpickler(CustomTestCase): self.assertEqual(restored.index, value.index) self.assertEqual(restored.parser("11"), 11) + def test_allowlist_covers_torch_dtypes_and_tensor_classes(self): + # A pickled tensor carries its dtype and tensor class as GLOBAL + # opcodes, so every dtype the installed torch exposes must be listed + # or a payload using it is refused. + torch_globals = { + ("torch", name) + for name in dir(torch) + if isinstance(getattr(torch, name), torch.dtype) + } + torch_globals.update( + (cls.__module__, cls.__name__) + for cls in (*torch._storage_classes, *torch._tensor_classes) + ) + + missing = sorted(torch_globals - SafeUnpickler.ALLOWED_GLOBALS) + + self.assertEqual( + missing, + [], + f"torch {torch.__version__} globals missing from " + f"SafeUnpickler.ALLOWED_GLOBALS: {missing}", + ) + def test_round_trips_tensor_payload(self): value = [("weight", torch.arange(6).reshape(2, 3))]