[VLM] avoid CUDA placement on non-CUDA platforms (#38750)

This commit is contained in:
Vincent Liu
2026-09-19 11:13:22 +08:00
committed by GitHub
parent c3aa09b0db
commit 090263eff6
3 changed files with 85 additions and 13 deletions
@@ -75,6 +75,21 @@ class TestMmProcessConfigValidation(CustomTestCase):
class TestBaseProcessorConfigExtraction(CustomTestCase):
"""Verify BaseMultimodalProcessor.__init__ extracts configs from server_args."""
def _patch_platform(self, cuda_alike, device_type):
platforms = SimpleNamespace(
current_platform=SimpleNamespace(
is_cuda_alike=lambda: cuda_alike,
device_type=device_type,
)
)
return patch.multiple(
"sglang.srt.multimodal.processors.base_processor",
_is_cpu=False,
_is_xpu=False,
_is_npu=False,
platforms=platforms,
)
def _make_processor(
self,
mm_process_config,
@@ -210,12 +225,23 @@ class TestBaseProcessorConfigExtraction(CustomTestCase):
9.30 -> 4.02 req/s on GB300 for full-page images."""
from transformers import BaseImageProcessor
proc = self._make_processor(
{}, image_processor=MagicMock(spec=BaseImageProcessor)
)
with self._patch_platform(cuda_alike=True, device_type="cuda"):
proc = self._make_processor(
{}, image_processor=MagicMock(spec=BaseImageProcessor)
)
self.assertEqual(proc.mm_processor_worker_num, 1)
self.assertIsNone(proc.mm_processor_executor)
def test_non_accelerator_fast_processor_gets_two_workers(self):
from transformers import BaseImageProcessor
with self._patch_platform(cuda_alike=False, device_type="custom"):
proc = self._make_processor(
{}, image_processor=MagicMock(spec=BaseImageProcessor)
)
self.assertEqual(proc.mm_processor_worker_num, 2)
self.assertIsNotNone(proc.mm_processor_executor)
def test_explicit_request_overrides_the_path_decision(self):
"""The server argument wins: an operator who measured their own workload
can still ask for concurrency on the GPU path."""
@@ -240,7 +266,10 @@ class TestBaseProcessorConfigExtraction(CustomTestCase):
BaseMultimodalProcessor,
)
with patch.object(BaseMultimodalProcessor, "auto_mm_processor_worker_num", 3):
with (
patch.object(BaseMultimodalProcessor, "auto_mm_processor_worker_num", 3),
self._patch_platform(cuda_alike=True, device_type="cuda"),
):
proc = self._make_processor(
{}, image_processor=MagicMock(spec=BaseImageProcessor)
)
@@ -51,10 +51,16 @@ class TestFastImageProcessorDevice(CustomTestCase):
reset_context()
self.addCleanup(reset_context)
def _device(self, processor, **platform):
def _device(self, processor, cuda_alike=True, device_type="cuda", **platform):
flags = {"_is_cpu": False, "_is_xpu": False, "_is_npu": False}
flags.update(platform)
with patch.multiple(BASE, **flags):
platforms = SimpleNamespace(
current_platform=SimpleNamespace(
is_cuda_alike=lambda: cuda_alike,
device_type=device_type,
)
)
with patch.multiple(BASE, platforms=platforms, **flags):
return processor._fast_image_processor_device(_Processor())
def test_device_follows_the_instance_base_gpu_id(self):
@@ -92,6 +98,23 @@ class TestFastImageProcessorDevice(CustomTestCase):
device = processor._fast_image_processor_device(Glm4vProcessor())
self.assertIsNone(device)
def test_platform_respects_cuda_compatibility(self):
processor = _make(base_gpu_id=3)
for cuda_alike, device_type, expected in (
(False, "other", None),
(True, "cuda", "cuda:3"),
(True, "musa", "musa:3"),
):
with self.subTest(cuda_alike=cuda_alike, device_type=device_type):
self.assertEqual(
self._device(
processor,
cuda_alike=cuda_alike,
device_type=device_type,
),
expected,
)
class TestFastImageProcessorMemoryPool(CustomTestCase):
def setUp(self):
@@ -161,6 +184,18 @@ class TestFastImageProcessorMemoryPool(CustomTestCase):
events.append("exit")
with (
patch.multiple(
BASE,
_is_cpu=False,
_is_xpu=False,
_is_npu=False,
platforms=SimpleNamespace(
current_platform=SimpleNamespace(
is_cuda_alike=lambda: True,
device_type="cuda",
)
),
),
patch(f"{BASE}.BaseImageProcessor", ImageProcessor),
patch(f"{BASE}.torch.cuda.device", return_value=nullcontext()),
patch(f"{BASE}.torch.cuda.MemPool", return_value="pool"),