Skip to content

Commit 7db7a91

Browse files
authored
[Relax][PyTorch] Fix PyTorch Dynamo frontend for Darwin compatibility (#18619)
## Why The llvm_target() function reads `/proc/cpuinfo` which only exists on `Linux`, causing tests to fail on `macOS` with FileNotFoundError. ## How - Add cross-platform CPU feature detection in llvm_target() using platform.system() and sysctl for macOS - Update tests
1 parent 3765df7 commit 7db7a91

File tree

2 files changed

+48
-10
lines changed

2 files changed

+48
-10
lines changed

python/tvm/relax/frontend/torch/dynamo.py

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
# pylint: disable=import-outside-toplevel, unused-argument, use-list-literal
2020
# mypy: ignore-errors
2121
"""PyTorch Dynamo backend of Relax."""
22+
2223
import functools
2324
from typing import Optional
2425

@@ -202,6 +203,43 @@ def _capture(graph_module: fx.GraphModule, example_inputs):
202203

203204
@functools.lru_cache(None)
204205
def llvm_target():
205-
if "avx512" in open("/proc/cpuinfo").read():
206-
return "llvm -mcpu=skylake-avx512"
207-
return "llvm -mcpu=core-avx2"
206+
import platform
207+
import subprocess
208+
209+
AVX512_TARGET = "llvm -mcpu=skylake-avx512"
210+
AVX2_TARGET = "llvm -mcpu=core-avx2"
211+
DEFAULT_TARGET = "llvm"
212+
213+
system = platform.system()
214+
215+
if system == "Linux":
216+
try:
217+
with open("/proc/cpuinfo") as f:
218+
cpuinfo = f.read()
219+
if "avx512" in cpuinfo:
220+
return AVX512_TARGET
221+
return AVX2_TARGET
222+
except FileNotFoundError:
223+
pass
224+
elif system == "Darwin":
225+
try:
226+
result = subprocess.run(
227+
["sysctl", "-n", "machdep.cpu.features"],
228+
capture_output=True,
229+
text=True,
230+
check=False,
231+
)
232+
if result.returncode == 0:
233+
cpu_features = result.stdout.lower()
234+
if "avx512" in cpu_features:
235+
return AVX512_TARGET
236+
if "avx2" in cpu_features:
237+
return AVX2_TARGET
238+
except (FileNotFoundError, subprocess.SubprocessError):
239+
pass
240+
241+
if platform.machine() == "arm64":
242+
return DEFAULT_TARGET
243+
244+
# Default fallback
245+
return DEFAULT_TARGET

tests/python/relax/test_frontend_dynamo.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -206,10 +206,10 @@ def subgraph_0(
206206
# block 0
207207
with R.dataflow():
208208
lv: R.Tensor((10,), dtype="float32") = R.sin(inp_0)
209-
lv1: R.Tensor((10,), dtype="float32") = R.add(lv, R.const(1, "float32"))
209+
lv1: R.Tensor((10,), dtype="float32") = R.add(lv, R.const(1.0, "float32"))
210210
lv2: R.Tensor((10,), dtype="float32") = R.divide(inp_0, lv1)
211211
lv3: R.Tensor((), dtype="float32") = R.sum(inp_1, axis=None, keepdims=False)
212-
lv4: R.Tensor((), dtype="bool") = R.less(lv3, R.const(1, "float32"))
212+
lv4: R.Tensor((), dtype="bool") = R.less(lv3, R.const(1.0, "float32"))
213213
gv: R.Tuple(R.Tensor((10,), dtype="float32"), R.Tensor((), dtype="bool")) = (
214214
lv2,
215215
lv4,
@@ -219,14 +219,14 @@ def subgraph_0(
219219

220220
@R.function
221221
def subgraph_1(
222-
inp_01: R.Tensor((10,), dtype="float32"), inp_11: R.Tensor((10,), dtype="float32")
222+
inp_0: R.Tensor((10,), dtype="float32"), inp_1: R.Tensor((10,), dtype="float32")
223223
) -> R.Tensor((10,), dtype="float32"):
224224
# block 0
225225
with R.dataflow():
226-
lv5: R.Tensor((10,), dtype="float32") = R.multiply(inp_01, inp_11)
227-
gv1: R.Tensor((10,), dtype="float32") = lv5
228-
R.output(gv1)
229-
return gv1
226+
lv: R.Tensor((10,), dtype="float32") = R.multiply(inp_0, inp_1)
227+
gv: R.Tensor((10,), dtype="float32") = lv
228+
R.output(gv)
229+
return gv
230230

231231
mod = dynamo_capture_subgraphs(Input2, torch.randn(10), torch.ones(10))
232232
tvm.ir.assert_structural_equal(mod, Expected2)

0 commit comments

Comments
 (0)