2025-05-13 17:12:32 -07:00
|
|
|
import os
|
2024-05-25 18:47:16 -07:00
|
|
|
import glob
|
|
|
|
|
|
2026-02-07 21:36:44 -08:00
|
|
|
Import('env', 'arch')
|
2020-01-17 11:20:17 -08:00
|
|
|
lenv = env.Clone()
|
|
|
|
|
|
2026-02-07 21:36:44 -08:00
|
|
|
tinygrad_root = env.Dir("#").abspath
|
|
|
|
|
tinygrad_files = ["#"+x for x in glob.glob(env.Dir("#tinygrad_repo").relpath + "/**", recursive=True, root_dir=tinygrad_root)
|
|
|
|
|
if 'pycache' not in x and os.path.isfile(os.path.join(tinygrad_root, x))]
|
2024-05-25 18:47:16 -07:00
|
|
|
|
2023-10-19 14:23:51 -07:00
|
|
|
# Get model metadata
|
2025-11-14 14:29:04 -08:00
|
|
|
for model_name in ['driving_vision', 'driving_policy', 'dmonitoring_model']:
|
2025-02-27 17:00:56 -08:00
|
|
|
fn = File(f"models/{model_name}").abspath
|
|
|
|
|
script_files = [File(Dir("#selfdrive/modeld").File("get_model_metadata.py").abspath)]
|
|
|
|
|
cmd = f'python3 {Dir("#selfdrive/modeld").abspath}/get_model_metadata.py {fn}.onnx'
|
|
|
|
|
lenv.Command(fn + "_metadata.pkl", [fn + ".onnx"] + tinygrad_files + script_files, cmd)
|
2023-08-21 19:38:40 -07:00
|
|
|
|
2026-02-07 21:36:44 -08:00
|
|
|
# compile warp
|
|
|
|
|
tg_flags = {
|
|
|
|
|
'larch64': 'DEV=QCOM FLOAT16=1 NOLOCALS=1 JIT_BATCH_SIZE=0',
|
|
|
|
|
'Darwin': f'DEV=CPU HOME={os.path.expanduser("~")}', # tinygrad calls brew which needs a $HOME in the env
|
|
|
|
|
}.get(arch, 'DEV=CPU CPU_LLVM=1')
|
|
|
|
|
image_flag = {
|
|
|
|
|
'larch64': 'IMAGE=2',
|
|
|
|
|
}.get(arch, 'IMAGE=0')
|
|
|
|
|
script_files = [File(Dir("#selfdrive/modeld").File("compile_warp.py").abspath)]
|
|
|
|
|
cmd = f'{tg_flags} python3 {Dir("#selfdrive/modeld").abspath}/compile_warp.py '
|
|
|
|
|
from openpilot.common.transformations.camera import _ar_ox_fisheye, _os_fisheye
|
|
|
|
|
warp_targets = []
|
|
|
|
|
for cam in [_ar_ox_fisheye, _os_fisheye]:
|
|
|
|
|
w, h = cam.width, cam.height
|
|
|
|
|
warp_targets += [File(f"models/warp_{w}x{h}_tinygrad.pkl").abspath, File(f"models/dm_warp_{w}x{h}_tinygrad.pkl").abspath]
|
|
|
|
|
lenv.Command(warp_targets, tinygrad_files + script_files, cmd)
|
|
|
|
|
|
2025-05-20 19:41:58 -07:00
|
|
|
def tg_compile(flags, model_name):
|
|
|
|
|
pythonpath_string = 'PYTHONPATH="${PYTHONPATH}:' + env.Dir("#tinygrad_repo").abspath + '"'
|
|
|
|
|
fn = File(f"models/{model_name}").abspath
|
|
|
|
|
return lenv.Command(
|
|
|
|
|
fn + "_tinygrad.pkl",
|
|
|
|
|
[fn + ".onnx"] + tinygrad_files,
|
2026-02-07 21:36:44 -08:00
|
|
|
f'{pythonpath_string} {flags} {image_flag} python3 {Dir("#tinygrad_repo").abspath}/examples/openpilot/compile3.py {fn}.onnx {fn}_tinygrad.pkl'
|
2025-05-20 19:41:58 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# Compile small models
|
2025-02-27 17:00:56 -08:00
|
|
|
for model_name in ['driving_vision', 'driving_policy', 'dmonitoring_model']:
|
2026-02-07 21:36:44 -08:00
|
|
|
tg_compile(tg_flags, model_name)
|
2025-05-13 17:12:32 -07:00
|
|
|
|
2025-05-20 19:41:58 -07:00
|
|
|
# Compile BIG model if USB GPU is available
|
2025-06-30 05:37:51 +08:00
|
|
|
if "USBGPU" in os.environ:
|
2025-07-12 00:52:18 -07:00
|
|
|
import subprocess
|
|
|
|
|
# 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, cwd=env.Dir('#').abspath)
|
2025-06-30 05:37:51 +08:00
|
|
|
if b"AMD" in devs:
|
|
|
|
|
print("USB GPU detected... building")
|
2025-07-26 21:21:25 -07:00
|
|
|
flags = "DEV=AMD AMD_IFACE=USB AMD_LLVM=1 NOLOCALS=0 IMAGE=0"
|
2025-06-30 05:37:51 +08:00
|
|
|
bp = tg_compile(flags, "big_driving_policy")
|
|
|
|
|
bv = tg_compile(flags, "big_driving_vision")
|
|
|
|
|
lenv.SideEffect('lock', [bp, bv]) # tg doesn't support multi-process so build serially
|
|
|
|
|
else:
|
|
|
|
|
print("USB GPU not detected... skipping")
|