import glob import json import os from SCons.Script import Value from openpilot.common.file_chunker import chunk_file, get_chunk_paths from openpilot.common.transformations.camera import _ar_ox_fisheye, _os_fisheye from openpilot.common.transformations.model import MEDMODEL_INPUT_SIZE, DM_INPUT_SIZE from openpilot.selfdrive.modeld.constants import ModelConstants from tinygrad import Device from openpilot.system.hardware import HARDWARE, PC Import('env', 'arch', 'release') def get_camera_configs(): DEVICE_RESOLUTIONS = { "tici": (_ar_ox_fisheye.width, _ar_ox_fisheye.height), "tizi": (_ar_ox_fisheye.width, _ar_ox_fisheye.height), "mici": (_os_fisheye.width, _os_fisheye.height), } if release or PC or 'CI' in os.environ: return set(DEVICE_RESOLUTIONS.values()) return [DEVICE_RESOLUTIONS[HARDWARE.get_device_type()]] CAMERA_CONFIGS = get_camera_configs() chunker_file = File("#common/file_chunker.py") lenv = env.Clone() 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))] def estimate_pickle_max_size(onnx_size): return 1.2 * onnx_size + 10 * 1024 * 1024 # 20% + 10MB is plenty # get fastest TG config available = set(Device.get_available_devices()) if 'CUDA' in available: tg_backend = 'CUDA' tg_flags = f'DEV={tg_backend}' elif 'QCOM' in available: tg_backend = 'QCOM' tg_flags = f'DEV={tg_backend} IMAGE=1 FLOAT16=1 NOLOCALS=1 JIT_BATCH_SIZE=0 OPENPILOT_HACKS=1' else: tg_backend = 'CPU' if arch == 'Darwin' else 'CPU:LLVM' tg_flags = f'DEV={tg_backend}' def write_tg_compiled_flags(target, source, env): with open(str(target[0]), "w") as f: json.dump({"DEV": tg_backend}, f) f.write("\n") compiled_flags_node = lenv.Command( File("models/tg_compiled_flags.json").abspath, tinygrad_files + [Value(tg_backend)], write_tg_compiled_flags, ) # tinygrad calls brew which needs a $HOME in the env mac_brew_string = f'HOME={os.path.expanduser("~")}' if arch == 'Darwin' else '' # Get model metadata for model_name in ['driving_vision', 'driving_policy', 'dmonitoring_model']: fn = File(f"models/{model_name}").abspath script_files = [File(Dir("#selfdrive/modeld").File("get_model_metadata.py").abspath)] cmd = f'{tg_flags} {mac_brew_string} python3 {Dir("#selfdrive/modeld").abspath}/get_model_metadata.py {fn}.onnx' lenv.Command(fn + "_metadata.pkl", [fn + ".onnx"] + tinygrad_files + script_files + [compiled_flags_node], cmd) modeld_dir = Dir("#selfdrive/modeld").abspath compile_modeld_script = [File(f"{modeld_dir}/compile_modeld.py")] compile_dm_warp_script = [File(f"{modeld_dir}/compile_dm_warp.py")] driving_onnx_deps = [File(f"models/{m}.onnx").abspath for m in ['driving_vision', 'driving_policy']] driving_metadata_deps = [File(f"models/{m}_metadata.pkl").abspath for m in ['driving_vision', 'driving_policy']] model_w, model_h = MEDMODEL_INPUT_SIZE frame_skip = ModelConstants.MODEL_RUN_FREQ // ModelConstants.MODEL_CONTEXT_FREQ pkl_path = File("models/driving_tinygrad.pkl").abspath camera_res_args = ' '.join(f'{cw}x{ch}' for cw, ch in CAMERA_CONFIGS) cmd = (f'{tg_flags} {mac_brew_string} python3 {modeld_dir}/compile_modeld.py ' f'--model-size {model_w}x{model_h} ' f'--camera-resolutions {camera_res_args} ' f'--vision-onnx {File("models/driving_vision.onnx").abspath} ' f'--policy-onnx {File("models/driving_policy.onnx").abspath} ' f'--output {pkl_path} --frame-skip {frame_skip}') node = lenv.Command(pkl_path, tinygrad_files + compile_modeld_script + driving_onnx_deps + driving_metadata_deps + [Value(camera_res_args), chunker_file, compiled_flags_node], cmd) onnx_sizes_sum = sum(os.path.getsize(f) for f in driving_onnx_deps) chunk_targets = get_chunk_paths(pkl_path, estimate_pickle_max_size(onnx_sizes_sum)*2) # TODO make weight dedupe work on QCOM def do_chunk(target, source, env, pkl=pkl_path, chunks=chunk_targets): chunk_file(pkl, chunks) lenv.Command(chunk_targets, node, do_chunk) dm_w, dm_h = DM_INPUT_SIZE for cam_w, cam_h in CAMERA_CONFIGS: dm_pkl_path = File(f"models/dm_warp_{cam_w}x{cam_h}_tinygrad.pkl").abspath cmd = (f'{tg_flags} {mac_brew_string} python3 {modeld_dir}/compile_dm_warp.py ' f'--camera-resolution {cam_w}x{cam_h} --warp-to {dm_w}x{dm_h} ' f'--output {dm_pkl_path}') lenv.Command(dm_pkl_path, tinygrad_files + compile_dm_warp_script + compile_modeld_script + [compiled_flags_node], cmd) def tg_compile(flags, model_name): pythonpath_string = 'PYTHONPATH="${PYTHONPATH}:' + env.Dir("#tinygrad_repo").abspath + '"' fn = File(f"models/{model_name}").abspath pkl = fn + "_tinygrad.pkl" onnx_path = fn + ".onnx" chunk_targets = get_chunk_paths(pkl, estimate_pickle_max_size(os.path.getsize(onnx_path))) compile_node = lenv.Command( pkl, [onnx_path] + tinygrad_files + [chunker_file, compiled_flags_node], f'{pythonpath_string} {flags} python3 {Dir("#tinygrad_repo").abspath}/examples/openpilot/compile3.py {fn}.onnx {pkl}', ) def do_chunk(target, source, env): chunk_file(pkl, chunk_targets) return lenv.Command( chunk_targets, compile_node, do_chunk, ) tg_compile(tg_flags, 'dmonitoring_model')