mirror of
https://gitlvb.teallvbs.xyz/IQ.Lvbs/IQ.Pilot.git
synced 2026-07-16 03:22:09 +08:00
115 lines
5.4 KiB
Python
115 lines
5.4 KiB
Python
import os
|
|
import glob
|
|
|
|
Import('env', 'envCython', 'arch', 'cereal', 'messaging', 'common', 'visionipc')
|
|
lenv = env.Clone()
|
|
lenvCython = envCython.Clone()
|
|
|
|
libs = [cereal, messaging, visionipc, common, 'capnp', 'kj', 'pthread']
|
|
frameworks = []
|
|
|
|
common_src = [
|
|
"models/commonmodel.cc",
|
|
"transforms/loadyuv.cc",
|
|
"transforms/transform.cc",
|
|
]
|
|
|
|
# OpenCL is a framework on Mac
|
|
if arch == "Darwin":
|
|
frameworks += ['OpenCL']
|
|
else:
|
|
libs += ['OpenCL']
|
|
|
|
# Set path definitions
|
|
for pathdef, fn in {'TRANSFORM': 'transforms/transform.cl', 'LOADYUV': 'transforms/loadyuv.cl'}.items():
|
|
for xenv in (lenv, lenvCython):
|
|
xenv['CXXFLAGS'].append(f'-D{pathdef}_PATH=\\"{File(fn).abspath}\\"')
|
|
|
|
# Compile cython
|
|
cython_libs = envCython["LIBS"] + libs
|
|
commonmodel_lib = lenv.Library('commonmodel', common_src)
|
|
lenvCython.Program('models/commonmodel_pyx.so', 'models/commonmodel_pyx.pyx', LIBS=[commonmodel_lib, *cython_libs], FRAMEWORKS=frameworks)
|
|
tinygrad_files = sorted(["#"+x for x in glob.glob(env.Dir("#tinygrad_repo").relpath + "/**", recursive=True, root_dir=env.Dir("#").abspath) if 'pycache' not in x])
|
|
|
|
def tg_compile(flags, model_name):
|
|
pythonpath_string = 'PYTHONPATH="${PYTHONPATH}:' + env.Dir("#tinygrad_repo").abspath + '"'
|
|
fn = File(f"models/{model_name}").abspath
|
|
cmd = lenv.Command(
|
|
fn + "_tinygrad.pkl",
|
|
[fn + ".onnx"] + tinygrad_files,
|
|
lenv.PrettyAction(
|
|
f'${{PYWARN}} {pythonpath_string} {flags} python3 {Dir("#tinygrad_repo").abspath}/examples/openpilot/compile3.py {fn}.onnx {fn}_tinygrad.pkl',
|
|
'MODEL', logfile='${TARGET}.log')
|
|
)
|
|
# committed pkls must survive a failed rebuild (Precious: no pre-build
|
|
# delete) and scons -c (NoClean); a failed compile must not brick modeld
|
|
lenv.Precious(cmd)
|
|
lenv.NoClean(cmd)
|
|
return cmd
|
|
|
|
def host_tinygrad_flags(*, float16=False):
|
|
if arch == "larch64":
|
|
base = "DEV=QCOM IMAGE=2 FLOAT16=1 NOLOCALS=1 JIT_BATCH_SIZE=0 OPENPILOT_HACKS=1"
|
|
return base
|
|
if arch == "Darwin":
|
|
base = f'DEV=CPU HOME={os.path.expanduser("~")} IMAGE=0'
|
|
elif arch == "x86_64":
|
|
base = "DEV=CPU:LLVM IMAGE=0"
|
|
else:
|
|
base = "DEV=CPU:LLVM IMAGE=0"
|
|
return f"{base} FLOAT16=1" if float16 else base
|
|
|
|
# Compile small models
|
|
for model_name in ['driving_vision', 'driving_policy', 'dmonitoring_model']:
|
|
# The optimization flags are mandatory on QCOM: without FLOAT16/NOLOCALS/JIT_BATCH_SIZE/OPENPILOT_HACKS these
|
|
# models compile to unoptimized QCOM kernels and run ~20x slower (dmonitoring_model: ~300ms -> ~14ms),
|
|
# which starves the driving model on the shared Adreno. IMAGE=2 (not upstream's IMAGE=1) because the
|
|
# pinned tinygrad (fd992d66) hits an IMAGE=1 codegen bug on this Adreno; IMAGE=2 is correct and fast here.
|
|
flags = host_tinygrad_flags()
|
|
|
|
# Shipped prebuilt pkls: on device, a fresh install has no .sconsign, so scons would recompile these
|
|
# from onnx (5-10 min) even though up-to-date pkls are committed. The check file pins the exact
|
|
# inputs (onnx + tinygrad_repo + flags + metadata script) and output hashes; if it matches, skip
|
|
# declaring the targets entirely. Any mismatch falls back to a normal on-device compile.
|
|
if arch == "larch64":
|
|
from openpilot.selfdrive.modeld.prebuilt_models import verify_prebuilt, outputs_match
|
|
if verify_prebuilt(model_name, flags):
|
|
print(lenv.PrettyNote('SKIP', f"{model_name} — prebuilt pkl"))
|
|
continue
|
|
# Input digest mismatch but the committed artifacts are intact: this is a
|
|
# device with a modified/partial tinygrad_repo (e.g. failed submodule fetch
|
|
# on an install without konn3kt auth). Recompiling here would DELETE the
|
|
# known-good pkl and then fail (compile3.py may not even exist), bricking
|
|
# modeld. Keep the shipped artifacts and say so.
|
|
if outputs_match(model_name):
|
|
print(lenv.PrettyNote('WARN', f"{model_name} — input digest mismatch (tinygrad_repo incomplete/modified?), keeping committed pkl"))
|
|
continue
|
|
elif not os.environ.get("COMPILE_MODELS"):
|
|
# The committed pkls ARE the device (QCOM) artifacts. A host build would
|
|
# overwrite them with host-flavor pkls (and `scons -c` deletes them), which
|
|
# then show up as staged changes and brick devices if committed. Host pkls
|
|
# only on explicit request: COMPILE_MODELS=1 scons ...
|
|
print(lenv.PrettyNote('SKIP', f"{model_name} — QCOM pkl kept (COMPILE_MODELS=1 to build host)"))
|
|
continue
|
|
|
|
fn = File(f"models/{model_name}").abspath
|
|
script_files = [File(Dir("#selfdrive/modeld").File("get_model_metadata.py").abspath)]
|
|
metadata_cmd = f'${{PYWARN}} python3 {Dir("#selfdrive/modeld").abspath}/get_model_metadata.py {fn}.onnx'
|
|
lenv.Command(fn + "_metadata.pkl", [fn + ".onnx"] + tinygrad_files + script_files,
|
|
lenv.PrettyAction(metadata_cmd, 'META', logfile='${TARGET}.log'))
|
|
tg_compile(flags, model_name)
|
|
|
|
# Compile BIG model if USB GPU is available
|
|
if "USBGPU" in os.environ:
|
|
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)
|
|
if b"AMD" in devs:
|
|
print("USB GPU detected... building")
|
|
flags = "DEV=AMD AMD_IFACE=USB AMD_LLVM=1 NOLOCALS=0 IMAGE=0"
|
|
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")
|