mirror of
https://gitlvb.teallvbs.xyz/IQ.Lvbs/IQ.Pilot.git
synced 2026-07-25 05:22:11 +08:00
IQ.Pilot Release Commit @ 4fcea4d
This commit is contained in:
@@ -0,0 +1,100 @@
|
||||
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 ['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)
|
||||
Reference in New Issue
Block a user