Repository navigation
add support for Metax GPU - #58338
Conversation
There was a problem hiding this comment.
Code Review
This pull request adds support for Metax GPUs to Ray. The changes are well-structured, including documentation, the MetaxGPUAcceleratorManager implementation, and corresponding tests. My review focuses on improving resource safety by ensuring proper cleanup, removing potentially incorrect logic copied from other accelerator managers, and enhancing the robustness and coverage of the new tests.
| try: | ||
| pymxsml.mxSmlExInit() | ||
| except pymxsml.MXSMLEXError: | ||
| return None | ||
| device_name = None | ||
| device_count = pymxsml.mxSmlExDeviceGetCount() | ||
| if device_count > 0: | ||
| handle = pymxsml.mxSmlExDeviceGetHandleByIndex(0) | ||
| device_name = pymxsml.mxSmlExDeviceGetName(handle) | ||
| if isinstance(device_name, bytes): | ||
| device_name = device_name.decode("utf-8") | ||
| pymxsml.mxSmlExShutdown() | ||
| return device_name |
There was a problem hiding this comment.
Similar to get_current_node_num_accelerators, the pymxsml.mxSmlExShutdown() call should be in a finally block to ensure that the pymxsml library is properly shut down, even if errors occur while getting device information. This prevents potential resource leaks.
try:
pymxsml.mxSmlExInit()
except pymxsml.MXSMLEXError:
return None
try:
device_name = None
device_count = pymxsml.mxSmlExDeviceGetCount()
if device_count > 0:
handle = pymxsml.mxSmlExDeviceGetHandleByIndex(0)
device_name = pymxsml.mxSmlExDeviceGetName(handle)
if isinstance(device_name, bytes):
device_name = device_name.decode("utf-8")
finally:
pymxsml.mxSmlExShutdown()
return device_name| def test_metax_gpu_type(shutdown_only): | ||
| with patch( | ||
| "ray._private.accelerators.MetaxGPUAcceleratorManager.get_current_node_accelerator_type", | ||
| return_value="MXC500", | ||
| ): | ||
| from ray.util import accelerators | ||
|
|
||
| ray.init() | ||
| result = MetaxGPUAcceleratorManager.get_current_node_accelerator_type() | ||
| assert result == accelerators.METAX_C500 |
There was a problem hiding this comment.
The test test_metax_gpu_type calls ray.init(), which triggers accelerator detection. This can fail in test environments that do not have Metax GPU hardware or drivers installed because get_current_node_num_accelerators is not mocked. Please mock get_current_node_num_accelerators to make the test more robust and independent of the environment.
| def test_metax_gpu_type(shutdown_only): | |
| with patch( | |
| "ray._private.accelerators.MetaxGPUAcceleratorManager.get_current_node_accelerator_type", | |
| return_value="MXC500", | |
| ): | |
| from ray.util import accelerators | |
| ray.init() | |
| result = MetaxGPUAcceleratorManager.get_current_node_accelerator_type() | |
| assert result == accelerators.METAX_C500 | |
| @patch( | |
| "ray._private.accelerators.MetaxGPUAcceleratorManager.get_current_node_num_accelerators", | |
| return_value=1, | |
| ) | |
| def test_metax_gpu_type(mock_get_num_accelerators, shutdown_only): | |
| with patch( | |
| "ray._private.accelerators.MetaxGPUAcceleratorManager.get_current_node_accelerator_type", | |
| return_value="MXC500", | |
| ): | |
| from ray.util import accelerators | |
| ray.init() | |
| result = MetaxGPUAcceleratorManager.get_current_node_accelerator_type() | |
| assert result == accelerators.METAX_C500 |
|
|
||
| * - METAX GPU | ||
| - GPU | ||
| - Experimental, supported by the community |
| if cuda_visible_devices == "NoDevFiles": | ||
| return [] |
There was a problem hiding this comment.
The check for cuda_visible_devices == "NoDevFiles" is a special case for NVIDIA GPUs when no devices are found. It's unlikely that Metax GPU drivers have the same behavior. This code seems to be copied from the NVIDIA accelerator manager and might be incorrect or dead code in this context. Please remove this check if it's not applicable to Metax GPUs.
|
|
||
| os.environ[ | ||
| MetaxGPUAcceleratorManager.get_visible_accelerator_ids_env_var() | ||
| ] = ",".join([str(i) for i in visible_cuda_devices]) |
| def test_get_current_process_visible_accelerator_ids(monkeypatch): | ||
| monkeypatch.setenv("CUDA_VISIBLE_DEVICES", "0") | ||
| assert MetaxGPUAcceleratorManager.get_current_process_visible_accelerator_ids() == [ | ||
| "0" | ||
| ] | ||
|
|
||
| monkeypatch.setenv("CUDA_VISIBLE_DEVICES", "0,4,7") | ||
| assert MetaxGPUAcceleratorManager.get_current_process_visible_accelerator_ids() == [ | ||
| "0", | ||
| "4", | ||
| "7", | ||
| ] | ||
|
|
||
| monkeypatch.setenv("CUDA_VISIBLE_DEVICES", "") | ||
| assert ( | ||
| MetaxGPUAcceleratorManager.get_current_process_visible_accelerator_ids() == [] | ||
| ) | ||
|
|
||
| del os.environ["CUDA_VISIBLE_DEVICES"] | ||
| assert ( | ||
| MetaxGPUAcceleratorManager.get_current_process_visible_accelerator_ids() is None | ||
| ) |
There was a problem hiding this comment.
To improve test coverage and validate the handling of all edge cases, please add a test case for when CUDA_VISIBLE_DEVICES is set to "NoDevFiles". This is especially important given that this behavior is likely specific to NVIDIA drivers and its applicability to Metax GPUs should be confirmed.
def test_get_current_process_visible_accelerator_ids(monkeypatch):
monkeypatch.setenv("CUDA_VISIBLE_DEVICES", "0")
assert MetaxGPUAcceleratorManager.get_current_process_visible_accelerator_ids() == [
"0"
]
monkeypatch.setenv("CUDA_VISIBLE_DEVICES", "0,4,7")
assert MetaxGPUAcceleratorManager.get_current_process_visible_accelerator_ids() == [
"0",
"4",
"7",
]
monkeypatch.setenv("CUDA_VISIBLE_DEVICES", "")
assert (
MetaxGPUAcceleratorManager.get_current_process_visible_accelerator_ids() == []
)
monkeypatch.setenv("CUDA_VISIBLE_DEVICES", "NoDevFiles")
assert (
MetaxGPUAcceleratorManager.get_current_process_visible_accelerator_ids() == []
)
del os.environ["CUDA_VISIBLE_DEVICES"]
assert (
MetaxGPUAcceleratorManager.get_current_process_visible_accelerator_ids() is None
)|
@jjyao PTAL |
|
Sorry for the late review. I'll take a look after the summit. |
3bdecdb to
c2bec6a
Compare
e053cff to
1b60dbc
Compare
|
This pull request has been automatically marked as stale because it has not had You can always ask for help on our discussion forum or Ray's public slack channel. If you'd like to keep this open, just leave any comment, and the stale label will be removed. |
| return [] | ||
|
|
||
|
|
||
| return list(cuda_visible_devices.split(",")) |
There was a problem hiding this comment.
Bug: Missing "NoDevFiles" check in visible accelerator IDs
The get_current_process_visible_accelerator_ids() method in MetaxGPUAcceleratorManager is missing a check for the special "NoDevFiles" value. All other GPU accelerator managers (NvidiaGPUAcceleratorManager, AMDGPUAcceleratorManager, IntelGPUAcceleratorManager, NPUAcceleratorManager) include a check that returns an empty list when the environment variable equals "NoDevFiles". Since Metax GPUs use CUDA_VISIBLE_DEVICES (same as Nvidia), this special value could be encountered, and without the check, the method would incorrectly return ["NoDevFiles"] instead of [].
jjyao
left a comment
There was a problem hiding this comment.
sorry for the late review, LGTM
|
|
||
| .. tip:: | ||
|
|
||
| You can set the ``CUDA_VISIBLE_DEVICES`` environment variable before starting a Ray node |
There was a problem hiding this comment.
is it really CUDA_VISIBLE_DEVICES?
There was a problem hiding this comment.
Yes, CUDA-compatible here, this env var works too.
| pymxsml.mxSmlExShutdown() | ||
| return device_name | ||
| except Exception: | ||
| logger.exception("Failed to detect GPU type.") |
There was a problem hiding this comment.
| logger.exception("Failed to detect GPU type.") | |
| logger.warning("Failed to detect GPU type.", exc_info=True) |
| "ray._private.accelerators.MetaxGPUAcceleratorManager.get_current_node_num_accelerators", | ||
| return_value=1, | ||
| ) | ||
| def test_metax_gpu_type(mock_get_num_accelerators, shutdown_only): |
There was a problem hiding this comment.
I don't think this test is testing anything, we can just delete it: it mocks a function and then just calls that mocked function.
| MetaxGPUAcceleratorManager.get_current_process_visible_accelerator_ids() == [] | ||
| ) | ||
|
|
||
| del os.environ["CUDA_VISIBLE_DEVICES"] |
|
Lint failures, could you also rebase with master |
f48bcc8 to
5f449cf
Compare
Signed-off-by: XLC127 <[email protected]>
Why are these changes needed?
Metax GPU is rapidly evolving and now supports inference for mainstream LLMs via vLLM. To scale these workloads across clusters, we propose leveraging Ray’s robust distributed task and resource management system.