gemini-code-assist[bot] commented on code in PR #18619:
URL: https://github.com/apache/tvm/pull/18619#discussion_r2648881320
##########
python/tvm/relax/frontend/torch/dynamo.py:
##########
@@ -202,6 +203,38 @@ def _capture(graph_module: fx.GraphModule, example_inputs):
@functools.lru_cache(None)
def llvm_target():
- if "avx512" in open("/proc/cpuinfo").read():
- return "llvm -mcpu=skylake-avx512"
- return "llvm -mcpu=core-avx2"
+ import platform
+ import subprocess
+
+ system = platform.system()
+
+ if system == "Linux":
+ try:
+ with open("/proc/cpuinfo") as f:
+ cpuinfo = f.read()
+ if "avx512" in cpuinfo:
+ return "llvm -mcpu=skylake-avx512"
+ return "llvm -mcpu=core-avx2"
+ except FileNotFoundError:
+ pass
+ elif system == "Darwin":
+ try:
+ result = subprocess.run(
+ ["sysctl", "-n", "machdep.cpu.features"],
+ capture_output=True,
+ text=True,
+ check=False,
+ )
+ cpu_features = result.stdout.lower()
+ if "avx512" in cpu_features:
+ return "llvm -mcpu=skylake-avx512"
+ if "avx2" in cpu_features:
+ return "llvm -mcpu=core-avx2"
+ except (FileNotFoundError, subprocess.SubprocessError):
+ pass
+
+ if platform.machine() == "arm64":
+ return "llvm"
+
+ # Default fallback
+ return "llvm"
Review Comment:

To improve maintainability and robustness, you can define the target strings
as constants to avoid repetition. Also, it's a good practice to check the
`returncode` of the `subprocess.run` call to ensure the command executed
successfully before parsing its output.
```python
import platform
import subprocess
AVX512_TARGET = "llvm -mcpu=skylake-avx512"
AVX2_TARGET = "llvm -mcpu=core-avx2"
DEFAULT_TARGET = "llvm"
system = platform.system()
if system == "Linux":
try:
with open("/proc/cpuinfo") as f:
cpuinfo = f.read()
if "avx512" in cpuinfo:
return AVX512_TARGET
return AVX2_TARGET
except FileNotFoundError:
pass
elif system == "Darwin":
try:
result = subprocess.run(
["sysctl", "-n", "machdep.cpu.features"],
capture_output=True,
text=True,
check=False,
)
if result.returncode == 0:
cpu_features = result.stdout.lower()
if "avx512" in cpu_features:
return AVX512_TARGET
if "avx2" in cpu_features:
return AVX2_TARGET
except (FileNotFoundError, subprocess.SubprocessError):
pass
if platform.machine() == "arm64":
return DEFAULT_TARGET
# Default fallback
return DEFAULT_TARGET
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]