-
Notifications
You must be signed in to change notification settings - Fork 6.9k
add support for Metax GPU #58338
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
add support for Metax GPU #58338
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| import logging | ||
| import os | ||
| from typing import List, Optional, Tuple | ||
|
|
||
| from ray._private.accelerators.accelerator import AcceleratorManager | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
| CUDA_VISIBLE_DEVICES_ENV_VAR = "CUDA_VISIBLE_DEVICES" | ||
| NOSET_CUDA_VISIBLE_DEVICES_ENV_VAR = "RAY_EXPERIMENTAL_NOSET_CUDA_VISIBLE_DEVICES" | ||
|
|
||
|
|
||
| class MetaxGPUAcceleratorManager(AcceleratorManager): | ||
| """Metax GPU accelerators.""" | ||
|
|
||
| @staticmethod | ||
| def get_resource_name() -> str: | ||
| return "GPU" | ||
|
|
||
| @staticmethod | ||
| def get_visible_accelerator_ids_env_var() -> str: | ||
| return CUDA_VISIBLE_DEVICES_ENV_VAR | ||
|
|
||
| @staticmethod | ||
| def get_current_process_visible_accelerator_ids() -> Optional[List[str]]: | ||
| cuda_visible_devices = os.environ.get( | ||
| MetaxGPUAcceleratorManager.get_visible_accelerator_ids_env_var(), None | ||
| ) | ||
| if cuda_visible_devices is None: | ||
| return None | ||
|
|
||
| if cuda_visible_devices == "": | ||
| return [] | ||
|
|
||
| if cuda_visible_devices == "NoDevFiles": | ||
| return [] | ||
|
Comment on lines
+35
to
+36
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The check for |
||
|
|
||
| return list(cuda_visible_devices.split(",")) | ||
|
|
||
| @staticmethod | ||
| def get_current_node_num_accelerators() -> int: | ||
| try: | ||
| import pymxsml.mxsml_extension as pymxsml | ||
|
|
||
| try: | ||
| pymxsml.mxSmlExInit() | ||
| except pymxsml.MXSMLEXError: | ||
| return 0 | ||
| device_count = pymxsml.mxSmlExDeviceGetCount() | ||
| pymxsml.mxSmlExShutdown() | ||
| return device_count | ||
| except Exception as e: | ||
| logger.debug("Could not import pymxsml: %s", e) | ||
| return 0 | ||
|
|
||
| @staticmethod | ||
| def get_current_node_accelerator_type() -> Optional[str]: | ||
| try: | ||
| import pymxsml.mxsml_extension as pymxsml | ||
|
|
||
| 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 | ||
| except Exception: | ||
| logger.exception("Failed to detect GPU type.") | ||
| return None | ||
|
|
||
| @staticmethod | ||
| def validate_resource_request_quantity( | ||
| quantity: float, | ||
| ) -> Tuple[bool, Optional[str]]: | ||
| return (True, None) | ||
|
|
||
| @staticmethod | ||
| def set_current_process_visible_accelerator_ids( | ||
| visible_cuda_devices: List[str], | ||
| ) -> None: | ||
| if os.environ.get(NOSET_CUDA_VISIBLE_DEVICES_ENV_VAR): | ||
| return | ||
|
|
||
| os.environ[ | ||
| MetaxGPUAcceleratorManager.get_visible_accelerator_ids_env_var() | ||
| ] = ",".join([str(i) for i in visible_cuda_devices]) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,81 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import os | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import sys | ||||||||||||||||||||||||||||||||||||||||||||||||||
| from unittest.mock import patch | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| import pytest | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| import ray | ||||||||||||||||||||||||||||||||||||||||||||||||||
| from ray._private.accelerators import ( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| MetaxGPUAcceleratorManager, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| get_accelerator_manager_for_resource, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| @patch( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| "ray._private.accelerators.MetaxGPUAcceleratorManager.get_current_node_num_accelerators", | ||||||||||||||||||||||||||||||||||||||||||||||||||
| return_value=4, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| def test_visible_metax_gpu_ids(mock_get_num_accelerators, monkeypatch, shutdown_only): | ||||||||||||||||||||||||||||||||||||||||||||||||||
| monkeypatch.setenv("CUDA_VISIBLE_DEVICES", "0,1,2") | ||||||||||||||||||||||||||||||||||||||||||||||||||
| del get_accelerator_manager_for_resource._resource_name_to_accelerator_manager | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ray.init() | ||||||||||||||||||||||||||||||||||||||||||||||||||
| assert mock_get_num_accelerators.called | ||||||||||||||||||||||||||||||||||||||||||||||||||
| assert ray.available_resources()["GPU"] == 3 | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| 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 | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+26
to
+35
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The test
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| 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 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+38
to
+59
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To improve test coverage and validate the handling of all edge cases, please add a test case for when 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
) |
||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| def test_set_current_process_visible_accelerator_ids(): | ||||||||||||||||||||||||||||||||||||||||||||||||||
| MetaxGPUAcceleratorManager.set_current_process_visible_accelerator_ids(["0"]) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| assert os.environ["CUDA_VISIBLE_DEVICES"] == "0" | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| MetaxGPUAcceleratorManager.set_current_process_visible_accelerator_ids(["0", "1"]) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| assert os.environ["CUDA_VISIBLE_DEVICES"] == "0,1" | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| MetaxGPUAcceleratorManager.set_current_process_visible_accelerator_ids( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ["0", "1", "7"] | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| assert os.environ["CUDA_VISIBLE_DEVICES"] == "0,1,7" | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: CUDA_VISIBLE_DEVICES leaks across tests - fix cleanuptest_set_current_process_visible_accelerator_ids sets CUDA_VISIBLE_DEVICES but never unsets it, causing the env var to leak across the test process and potentially affecting subsequent tests’ GPU detection and resource resolution. Add cleanup at the end (e.g., del os.environ["CUDA_VISIBLE_DEVICES"]) to prevent flakiness. |
||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| del os.environ["CUDA_VISIBLE_DEVICES"] | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| if __name__ == "__main__": | ||||||||||||||||||||||||||||||||||||||||||||||||||
| if os.environ.get("PARALLEL_CI"): | ||||||||||||||||||||||||||||||||||||||||||||||||||
| sys.exit(pytest.main(["-n", "auto", "--boxed", "-vs", __file__])) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| sys.exit(pytest.main(["-sv", __file__])) | ||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: Bug
The
MetaxGPUAcceleratorManagerclass is missing from the module's__all__list. This prevents direct import, which is inconsistent with other accelerator managers and can causeImportErrorfor users.