diff --git a/.gitignore b/.gitignore index 40438f5fd0..8c4218c928 100644 --- a/.gitignore +++ b/.gitignore @@ -70,9 +70,8 @@ flycheck_* cppcheck_report.txt comma*.sh -selfdrive/modeld/thneed/compile -selfdrive/modeld/models/*.thneed selfdrive/modeld/models/*.pkl +selfdrive/modeld/models/*.json *.bz2 *.zst diff --git a/selfdrive/modeld/SConscript b/selfdrive/modeld/SConscript index 10f3876190..96d32b2b35 100644 --- a/selfdrive/modeld/SConscript +++ b/selfdrive/modeld/SConscript @@ -14,9 +14,11 @@ common_src = [ "transforms/transform.cc", ] -# OpenCL is a framework on Mac if arch == "Darwin": + # OpenCL is a framework on Mac frameworks += ['OpenCL'] + # Fix for "Error: $HOME must be set to run brew" + lenv['ENV']['HOME'] = os.environ['HOME'] else: libs += ['OpenCL'] @@ -47,22 +49,40 @@ def tg_compile(flags, model_name): f'{pythonpath_string} {flags} python3 {Dir("#tinygrad_repo").abspath}/examples/openpilot/compile3.py {fn}.onnx {fn}_tinygrad.pkl' ) -# Compile small models -for model_name in ['driving_vision', 'driving_policy', 'dmonitoring_model']: - flags = { - 'larch64': 'QCOM=1', - 'Darwin': 'CPU=1 IMAGE=0 JIT=2', - }.get(arch, 'LLVM=1 LLVMOPT=1 BEAM=0 IMAGE=0 JIT=2') - tg_compile(flags, model_name) +def tg_dump_flags(flags, prefix): + flags_dict = {} + for flag_pair in flags.split(): + k, v = flag_pair.split('=') + flags_dict[k] = v + import json + with open(f'models/{prefix}_flags.json', 'w') as f: + json.dump(flags_dict, f) -# Compile BIG model if USB GPU is available import subprocess -from tinygrad import Device # because tg doesn't support multi-process devs = subprocess.check_output('python3 -c "from tinygrad import Device; print(list(Device.get_available_devices()))"', shell=True) +print("Available tinygrad devices:", devs) + +if b"QCOM" in devs: + flags = 'QCOM=1' +elif b"METAL" in devs: + flags = 'METAL=1 IMAGE=0 NOLOCALS=0' +elif b"GPU" in devs: + flags = 'GPU=1' +elif b"LLVM" in devs: + flags = 'LLVM=1 LLVMOPT=1 BEAM=0 IMAGE=0 JIT=2' +else: + flags = 'CPU=1 IMAGE=0 JIT=2' + +# Compile small models +for model_name in ['driving_vision', 'driving_policy', 'dmonitoring_model']: + tg_compile(flags, model_name) + +tg_dump_flags(flags, 'dmonitoringmodeld') + +# Compile BIG model if USB GPU is available if b"AMD" in devs: - del Device print("USB GPU detected... building") flags = "AMD=1 AMD_IFACE=USB AMD_LLVM=1 NOLOCALS=0 IMAGE=0" bp = tg_compile(flags, "big_driving_policy") @@ -70,3 +90,5 @@ if b"AMD" in devs: lenv.SideEffect('lock', [bp, bv]) # tg doesn't support multi-process so build serially else: print("USB GPU not detected... skipping") + +tg_dump_flags(flags, 'modeld') diff --git a/selfdrive/modeld/dmonitoringmodeld.py b/selfdrive/modeld/dmonitoringmodeld.py index 2a3df3f652..53477bb003 100755 --- a/selfdrive/modeld/dmonitoringmodeld.py +++ b/selfdrive/modeld/dmonitoringmodeld.py @@ -1,5 +1,6 @@ #!/usr/bin/env python3 import os +from pathlib import Path from openpilot.system.hardware import TICI from tinygrad.tensor import Tensor from tinygrad.dtype import dtypes @@ -7,13 +8,15 @@ if TICI: from openpilot.selfdrive.modeld.runners.tinygrad_helpers import qcom_tensor_from_opencl_address os.environ['QCOM'] = '1' else: - os.environ['LLVM'] = '1' + import json + with open(Path(__file__).parent / 'models/dmonitoringmodeld_flags.json') as f: + flags = json.load(f) + os.environ.update(flags) import math import time import pickle import ctypes import numpy as np -from pathlib import Path from setproctitle import setproctitle from cereal import messaging diff --git a/selfdrive/modeld/modeld.py b/selfdrive/modeld/modeld.py index 1e3d1782e6..32b5eda80b 100755 --- a/selfdrive/modeld/modeld.py +++ b/selfdrive/modeld/modeld.py @@ -1,5 +1,6 @@ #!/usr/bin/env python3 import os +from pathlib import Path from openpilot.system.hardware import TICI USBGPU = "USBGPU" in os.environ if USBGPU: @@ -9,8 +10,10 @@ elif TICI: from openpilot.selfdrive.modeld.runners.tinygrad_helpers import qcom_tensor_from_opencl_address os.environ['QCOM'] = '1' else: - os.environ['LLVM'] = '1' - os.environ['JIT'] = '2' + import json + with open(Path(__file__).parent / 'models/modeld_flags.json') as f: + flags = json.load(f) + os.environ.update(flags) from tinygrad.tensor import Tensor from tinygrad.dtype import dtypes import time @@ -18,7 +21,6 @@ import pickle import numpy as np import cereal.messaging as messaging from cereal import car, log -from pathlib import Path from setproctitle import setproctitle from cereal.messaging import PubMaster, SubMaster from msgq.visionipc import VisionIpcClient, VisionStreamType, VisionBuf diff --git a/tools/mac_setup.sh b/tools/mac_setup.sh index d23052d0f0..6fe81726ba 100755 --- a/tools/mac_setup.sh +++ b/tools/mac_setup.sh @@ -43,7 +43,7 @@ brew "glfw" brew "libarchive" brew "libusb" brew "libtool" -brew "llvm" +brew "llvm@19" brew "openssl@3.0" brew "qt@5" brew "zeromq"