Skip to content

add support for Metax GPU - #58338

Merged
jjyao merged 3 commits into
ray-project:masterfrom
XLC127:support_metax
Dec 5, 2025
Merged

jjyao merged 3 commits into
ray-project:masterfrom
XLC127:support_metax

Conversation

@XLC127

@XLC127 XLC127 commented Oct 31, 2025 •

Copy link
Copy Markdown
Contributor

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.

@XLC127
XLC127 requested review from a team as code owners October 31, 2025 09:52
cursor[bot]

This comment was marked as outdated.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +56 to +68
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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

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

Comment on lines +23 to +32
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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

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.

Suggested change
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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

There is a trailing whitespace at the end of this line. Please remove it to maintain consistent formatting.

Suggested change
- Experimental, supported by the community
- Experimental, supported by the community

Comment on lines +35 to +36
if cuda_visible_devices == "NoDevFiles":
return []

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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])

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The visible_cuda_devices parameter is already a List[str], so the str(i) conversion inside the list comprehension is redundant. You can directly join the list elements.

        ] = ",".join(visible_cuda_devices)

Comment on lines +35 to +56
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
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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
    )

@edoakes

edoakes commented Oct 31, 2025

Copy link
Copy Markdown
Collaborator

@jjyao PTAL

@ray-gardener ray-gardener Bot added core Issues that should be addressed in Ray Core llm community-contribution Contributed by the community labels Oct 31, 2025
@jjyao

jjyao commented Nov 3, 2025

Copy link
Copy Markdown
Contributor

Sorry for the late review. I'll take a look after the summit.

@XLC127
XLC127 force-pushed the support_metax branch 5 times, most recently from 3bdecdb to c2bec6a Compare November 3, 2025 09:20
@XLC127
XLC127 force-pushed the support_metax branch 2 times, most recently from e053cff to 1b60dbc Compare November 6, 2025 08:34
@github-actions

Copy link
Copy Markdown

This pull request has been automatically marked as stale because it has not had
any activity for 14 days. It will be closed in another 14 days if no further activity occurs.
Thank you for your contributions.

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.

@github-actions github-actions Bot added the stale The issue is stale. It will be closed within 7 days unless there are further conversation label Nov 20, 2025
return []


return list(cuda_visible_devices.split(","))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 [].

Fix in Cursor Fix in Web

@jjyao jjyao left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry for the late review, LGTM


.. tip::

You can set the ``CUDA_VISIBLE_DEVICES`` environment variable before starting a Ray node

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it really CUDA_VISIBLE_DEVICES?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, CUDA-compatible here, this env var works too.

pymxsml.mxSmlExShutdown()
return device_name
except Exception:
logger.exception("Failed to detect GPU type.")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
logger.exception("Failed to detect GPU type.")
logger.warning("Failed to detect GPU type.", exc_info=True)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

"ray._private.accelerators.MetaxGPUAcceleratorManager.get_current_node_num_accelerators",
return_value=1,
)
def test_metax_gpu_type(mock_get_num_accelerators, shutdown_only):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

MetaxGPUAcceleratorManager.get_current_process_visible_accelerator_ids() == []
)

del os.environ["CUDA_VISIBLE_DEVICES"]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use monkeypatch.delenv

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@jjyao jjyao added the go add ONLY when ready to merge, run all tests label Dec 2, 2025
@jjyao jjyao removed the stale The issue is stale. It will be closed within 7 days unless there are further conversation label Dec 2, 2025
@jjyao

jjyao commented Dec 2, 2025

Copy link
Copy Markdown
Contributor

Lint failures, could you also rebase with master

@XLC127
XLC127 force-pushed the support_metax branch 2 times, most recently from f48bcc8 to 5f449cf Compare December 3, 2025 03:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community-contribution Contributed by the community core Issues that should be addressed in Ray Core go add ONLY when ready to merge, run all tests llm

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants