无可见 GPU 时跳过 xdist 设备绑定#12
Conversation
There was a problem hiding this comment.
Code Review
This pull request adds a check to raise a pytest.UsageError when pytest-xdist is used without any visible CUDA devices, along with a corresponding test. The review feedback suggests that raising an error unnecessarily breaks xdist testing on CPU-only environments. Instead, it is recommended to skip GPU binding and memory allocation logic when no GPUs are detected, and to update the test to verify this skipping behavior.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| if worker_id is not None: | ||
| gpu_id = int(worker_id.replace('gw', '')) | ||
| num_gpus = torch.cuda.device_count() | ||
| if num_gpus == 0: | ||
| raise pytest.UsageError( | ||
| 'pytest-xdist GPU binding requires at least one visible CUDA device' | ||
| ) |
There was a problem hiding this comment.
在没有可见 GPU 的环境下(例如在 CPU 机器上进行本地开发或运行不依赖 GPU 的测试),如果用户使用 pytest-xdist(例如 pytest -n auto),当前修改会直接抛出 pytest.UsageError 从而中断整个测试流程。然而,如果不使用 pytest-xdist,测试却可以正常启动。这种行为是不一致的,并且会完全限制在无 GPU 环境下使用 xdist 进行并发测试。
建议将 GPU 绑定的前置条件直接放入 if 判断中。如果检测到没有可用的 CUDA 设备,则直接跳过 GPU 绑定和内存限制逻辑。这样既能避免 ZeroDivisionError,又不会强行中断无 GPU 环境下的 xdist 测试流程。如果测试本身确实需要 GPU,PyTorch 也会在运行时抛出清晰的 CUDA 不可用错误。
| if worker_id is not None: | |
| gpu_id = int(worker_id.replace('gw', '')) | |
| num_gpus = torch.cuda.device_count() | |
| if num_gpus == 0: | |
| raise pytest.UsageError( | |
| 'pytest-xdist GPU binding requires at least one visible CUDA device' | |
| ) | |
| if worker_id is not None and torch.cuda.device_count() > 0: | |
| gpu_id = int(worker_id.replace('gw', '')) | |
| num_gpus = torch.cuda.device_count() |
| def test_xdist_gpu_binding_reports_no_visible_gpu(monkeypatch): | ||
| import tests.pytest_benchmark_plugin as plugin | ||
|
|
||
| monkeypatch.setenv('PYTEST_XDIST_WORKER', 'gw0') | ||
| monkeypatch.setattr(plugin.torch.cuda, 'device_count', lambda: 0) | ||
|
|
||
| with pytest.raises(pytest.UsageError, match='at least one visible CUDA device'): | ||
| plugin.pytest_configure(DummyConfig()) |
There was a problem hiding this comment.
由于我们在 pytest_benchmark_plugin.py 中将行为修改为“无 GPU 时直接跳过绑定”而不是“抛出异常”,因此对应的测试也需要同步更新,以验证在无 GPU 时不会抛出异常且不会设置 CUDA_VISIBLE_DEVICES 环境变量。
| def test_xdist_gpu_binding_reports_no_visible_gpu(monkeypatch): | |
| import tests.pytest_benchmark_plugin as plugin | |
| monkeypatch.setenv('PYTEST_XDIST_WORKER', 'gw0') | |
| monkeypatch.setattr(plugin.torch.cuda, 'device_count', lambda: 0) | |
| with pytest.raises(pytest.UsageError, match='at least one visible CUDA device'): | |
| plugin.pytest_configure(DummyConfig()) | |
| def test_xdist_gpu_binding_skips_without_gpu(monkeypatch): | |
| import os | |
| import tests.pytest_benchmark_plugin as plugin | |
| monkeypatch.setenv('PYTEST_XDIST_WORKER', 'gw0') | |
| monkeypatch.setattr(plugin.torch.cuda, 'device_count', lambda: 0) | |
| monkeypatch.delenv('CUDA_VISIBLE_DEVICES', raising=False) | |
| plugin.pytest_configure(DummyConfig()) | |
| assert 'CUDA_VISIBLE_DEVICES' not in os.environ |
?? API ?? 2 ??????
这个改动修正了 pytest xdist 场景下对 GPU 绑定的前置假设,在没有可见 GPU 的环境里不再直接报错,而是跳过设备绑定逻辑,让无卡环境也能运行非设备相关测试。这样可以减少 CI 和开发机上的误报。
测试已经调整并通过,覆盖了无 GPU 条件下不会写入设备绑定环境变量的路径,本地插件级校验结果符合预期。