Skip to content

Commit e7baa6a

Browse files
author
Contributor
committed
fix: address PR review feedback for get_maca_arch
- Catch OSError in addition to CalledProcessError for robustness - Support MACA_ARCH env var to skip macainfo execution - Support MACA_PATH env var as fallback path - Rewrite tests to use direct import instead of sys.modules pollution Co-Authored-By: AtomCode (deepseek-v4-flash) <noreply@atomgit.com
1 parent a6c2bb3 commit e7baa6a

2 files changed

Lines changed: 113 additions & 1 deletion

File tree

python/tvm/contrib/mxcc.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,14 @@ def get_maca_arch(maca_path="/opt/maca"):
201201
gpu_arch : str
202202
The MetaX GPU architecture
203203
"""
204+
# Check MACA_ARCH environment variable first
205+
gpu_arch = os.environ.get("MACA_ARCH")
206+
if gpu_arch:
207+
return gpu_arch.lower()
208+
204209
gpu_arch = "xcore1000"
210+
# resolve maca path from parameter or environment variable
211+
maca_path = maca_path or os.environ.get("MACA_PATH", "/opt/maca")
205212
# check if maca is installed
206213
if not os.path.exists(maca_path):
207214
print("MACA not detected, using default xcore1000")
@@ -215,7 +222,7 @@ def get_maca_arch(maca_path="/opt/maca"):
215222
if match:
216223
gpu_arch = match.group(1)
217224
return gpu_arch.lower()
218-
except subprocess.CalledProcessError:
225+
except (subprocess.CalledProcessError, OSError):
219226
print(
220227
f"Unable to execute macainfo command, \
221228
please ensure MACA is installed and you have an MetaX GPU on your system.\
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
"""Tests for MACA architecture detection"""
18+
import os
19+
import subprocess
20+
import unittest
21+
from unittest.mock import patch
22+
23+
from tvm.contrib import mxcc
24+
25+
26+
class TestMacaArchDetection(unittest.TestCase):
27+
"""Test cases for get_maca_arch function"""
28+
29+
def test_env_maca_arch(self):
30+
"""Test MACA_ARCH environment variable is used when set"""
31+
with patch.dict(os.environ, {"MACA_ARCH": "xcore2000"}):
32+
arch = mxcc.get_maca_arch()
33+
self.assertEqual(arch, "xcore2000")
34+
35+
def test_env_maca_arch_overrides_maca_path(self):
36+
"""Test MACA_ARCH takes priority even when maca_path is given"""
37+
with patch.dict(os.environ, {"MACA_ARCH": "xcore3000"}):
38+
arch = mxcc.get_maca_arch(maca_path="/nonexistent")
39+
self.assertEqual(arch, "xcore3000")
40+
41+
def test_maca_not_installed_default(self):
42+
"""Test default arch is returned when MACA is not installed"""
43+
with patch.dict(os.environ, {}, clear=True):
44+
with patch("os.path.exists", return_value=False):
45+
arch = mxcc.get_maca_arch(maca_path="/nonexistent")
46+
self.assertEqual(arch, "xcore1000")
47+
48+
def test_maca_path_env_var(self):
49+
"""Test MACA_PATH environment variable is used as fallback path"""
50+
with patch.dict(os.environ, {"MACA_PATH": "/custom/maca"}, clear=True):
51+
with patch("os.path.exists", return_value=True):
52+
with patch(
53+
"subprocess.check_output",
54+
return_value=b"Name: XCORE1800\n",
55+
):
56+
arch = mxcc.get_maca_arch()
57+
self.assertEqual(arch, "xcore1800")
58+
59+
def test_oserror_is_caught(self):
60+
"""Test OSError (e.g. missing binary) is caught gracefully"""
61+
with patch.dict(os.environ, {}, clear=True):
62+
with patch("os.path.exists", return_value=True):
63+
with patch(
64+
"subprocess.check_output",
65+
side_effect=OSError("macainfo binary not found"),
66+
):
67+
arch = mxcc.get_maca_arch(maca_path="/valid/path")
68+
self.assertEqual(arch, "xcore1000")
69+
70+
def test_calledprocesserror_is_caught(self):
71+
"""Test CalledProcessError is caught gracefully"""
72+
with patch.dict(os.environ, {}, clear=True):
73+
with patch("os.path.exists", return_value=True):
74+
with patch(
75+
"subprocess.check_output",
76+
side_effect=subprocess.CalledProcessError(1, "macainfo"),
77+
):
78+
arch = mxcc.get_maca_arch(maca_path="/valid/path")
79+
self.assertEqual(arch, "xcore1000")
80+
81+
def test_macainfo_parsing(self):
82+
"""Test parsing of macainfo output"""
83+
with patch.dict(os.environ, {}, clear=True):
84+
with patch("os.path.exists", return_value=True):
85+
with patch(
86+
"subprocess.check_output",
87+
return_value=b"Name: XCORE2000\n",
88+
):
89+
arch = mxcc.get_maca_arch(maca_path="/valid/path")
90+
self.assertEqual(arch, "xcore2000")
91+
92+
def test_macainfo_parsing_no_match(self):
93+
"""Test fallback to default when macainfo output has no matching name"""
94+
with patch.dict(os.environ, {}, clear=True):
95+
with patch("os.path.exists", return_value=True):
96+
with patch(
97+
"subprocess.check_output",
98+
return_value=b"Name: UNKNOWN_DEVICE\n",
99+
):
100+
arch = mxcc.get_maca_arch(maca_path="/valid/path")
101+
self.assertEqual(arch, "xcore1000")
102+
103+
104+
if __name__ == "__main__":
105+
unittest.main()

0 commit comments

Comments
 (0)