From 82972d899388bc0d7acb22965f61bce41e77dbed Mon Sep 17 00:00:00 2001 From: Adeeb Shihadeh Date: Mon, 17 Aug 2026 19:57:55 -0700 Subject: [PATCH 1/7] not worth 1m --- openpilot/selfdrive/modeld/compile_modeld.py | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/openpilot/selfdrive/modeld/compile_modeld.py b/openpilot/selfdrive/modeld/compile_modeld.py index 2d27a41496e8b4..ad9a03819450d5 100755 --- a/openpilot/selfdrive/modeld/compile_modeld.py +++ b/openpilot/selfdrive/modeld/compile_modeld.py @@ -3,7 +3,6 @@ import atexit import math import os -import tempfile import time import shutil from functools import partial @@ -11,7 +10,7 @@ import numpy as np -from openpilot.selfdrive.modeld.helpers import dump_oob, load_oob +from openpilot.selfdrive.modeld.helpers import dump_oob def _patch_tinygrad_fetch_fw(): import hashlib @@ -257,11 +256,7 @@ def random_inputs_run(fn, seed, test_val=None, test_buffers=None, expect_match=T print('capture + replay') test_val, test_buffers = random_inputs_run(jit, SEED) - print('pickle round trip') - with tempfile.TemporaryFile(dir=".") as f: - dump_oob(jit, f) - f.seek(0) - jit = load_oob(f) + print('validate replay') random_inputs_run(jit, SEED, test_val, test_buffers, expect_match=True) random_inputs_run(jit, SEED+1, test_val, test_buffers, expect_match=False) return jit From f6ec62121a327484b25a392f238a2d31aabaa049 Mon Sep 17 00:00:00 2001 From: Adeeb Shihadeh Date: Mon, 17 Aug 2026 20:03:07 -0700 Subject: [PATCH 2/7] saves 24s --- openpilot/selfdrive/modeld/SConscript | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openpilot/selfdrive/modeld/SConscript b/openpilot/selfdrive/modeld/SConscript index 30a31aae271f8e..d6fc2de55519ec 100644 --- a/openpilot/selfdrive/modeld/SConscript +++ b/openpilot/selfdrive/modeld/SConscript @@ -45,7 +45,7 @@ tg_devices = { # which device to put jit inputs to at runtime USBGPU = usbgpu_present() if USBGPU: - usbgpu_tg_flags = f'DEBUG=2 DEV=USB+AMD:LLVM WARP_DEV={tg_backend} FLOAT16=1 JIT_BATCH_SIZE=0 GMMU=0 TC_OPT=2' + usbgpu_tg_flags = f'DEBUG=1 DEV=USB+AMD:LLVM WARP_DEV={tg_backend} FLOAT16=1 JIT_BATCH_SIZE=0 GMMU=0 TC_OPT=2' # the USB+AMD GPU takes an exclusive flock; serialize all targets that touch it usbgpu_lock = File("models/.usb_gpu.lock").abspath From 26a2e4be9d1fe574f60e764c0fffc59ef44a9c72 Mon Sep 17 00:00:00 2001 From: Adeeb Shihadeh Date: Mon, 17 Aug 2026 20:04:26 -0700 Subject: [PATCH 3/7] save 13s --- openpilot/selfdrive/modeld/compile_modeld.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/openpilot/selfdrive/modeld/compile_modeld.py b/openpilot/selfdrive/modeld/compile_modeld.py index ad9a03819450d5..ac7ca0663efa0a 100755 --- a/openpilot/selfdrive/modeld/compile_modeld.py +++ b/openpilot/selfdrive/modeld/compile_modeld.py @@ -269,6 +269,8 @@ def _parse_size(s): def read_file_chunked_to_disk(path): from openpilot.common.file_chunker import open_file_chunked + if os.path.isfile(path): + return path tmp_path = f'{path}.unchunked' with open(tmp_path, 'wb') as f, open_file_chunked(path) as src: shutil.copyfileobj(src, f) From 40feb58fa8dfa09784ee13d8bb2462d4e643b8d9 Mon Sep 17 00:00:00 2001 From: Adeeb Shihadeh Date: Mon, 17 Aug 2026 21:28:48 -0700 Subject: [PATCH 4/7] parallelize model compilation --- openpilot/selfdrive/modeld/SConscript | 5 +-- openpilot/selfdrive/modeld/compile_modeld.py | 42 +++++++++++++++++++- 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/openpilot/selfdrive/modeld/SConscript b/openpilot/selfdrive/modeld/SConscript index d6fc2de55519ec..fbbebe056d700d 100644 --- a/openpilot/selfdrive/modeld/SConscript +++ b/openpilot/selfdrive/modeld/SConscript @@ -45,7 +45,7 @@ tg_devices = { # which device to put jit inputs to at runtime USBGPU = usbgpu_present() if USBGPU: - usbgpu_tg_flags = f'DEBUG=1 DEV=USB+AMD:LLVM WARP_DEV={tg_backend} FLOAT16=1 JIT_BATCH_SIZE=0 GMMU=0 TC_OPT=2' + usbgpu_tg_flags = f'DEBUG=1 DEV=USB+AMD:LLVM WARP_DEV={tg_backend} FLOAT16=1 JIT_BATCH_SIZE=0 GMMU=0 TC_OPT=2 PARALLEL_COMPILE=4' # the USB+AMD GPU takes an exclusive flock; serialize all targets that touch it usbgpu_lock = File("models/.usb_gpu.lock").abspath @@ -79,8 +79,7 @@ for usbgpu in [False, True] if USBGPU else [False]: file_prefix, cmd_flags = ('big_', usbgpu_tg_flags) if usbgpu else ('big_' if os.getenv('BIG_INTO_SMALL') else '', tg_flags) driving_onnx_deps = get_existing_chunks(File(f"models/{file_prefix}driving_supercombo.onnx").abspath) camera_res_args = ' '.join(f'{cw}x{ch}' for cw, ch in CAMERA_CONFIGS) - # CPU 7 is isolated with isolcpus on AGNOS, so explicitly pin the compiler to it. - taskset = 'taskset -c 7 ' if arch == 'comma_arm64' else '' + taskset = 'taskset -c 0-7 ' if arch == 'comma_arm64' else '' cmd = (f'{cmd_flags} {mac_brew_string} {taskset}python3 {modeld_dir}/compile_modeld.py ' f'--model-size {model_w}x{model_h} ' f'--camera-resolutions {camera_res_args} ' diff --git a/openpilot/selfdrive/modeld/compile_modeld.py b/openpilot/selfdrive/modeld/compile_modeld.py index ac7ca0663efa0a..6f28fb75ab863e 100755 --- a/openpilot/selfdrive/modeld/compile_modeld.py +++ b/openpilot/selfdrive/modeld/compile_modeld.py @@ -2,6 +2,7 @@ import argparse import atexit import math +import multiprocessing import os import time import shutil @@ -30,10 +31,49 @@ def fetch_fw(path, name, sha256): from tinygrad.tensor import Tensor -from tinygrad.helpers import Context +from tinygrad.helpers import Context, ContextVar from tinygrad.device import Device from tinygrad.engine.jit import TinyJit +def _tinygrad_compile_worker(args): + ast, renderer, context = args + from tinygrad.codegen import to_program + with Context(**{k:v for k,v in context.items() if k in ContextVar._cache}): + return to_program(ast, renderer) + +def _patch_tinygrad_parallel_compile(): + from tinygrad import codegen + from tinygrad.engine import realize + from tinygrad.uop.ops import Ops + + def program_key(ast, renderer): + config = (codegen.NOOPT, codegen.EMULATED_DTYPES, codegen.NOLOCALS, codegen.USE_TC, codegen.IMAGE, codegen.DISABLE_FAST_IDIV, + codegen.TRANSCENDENTAL, codegen.ALLOW_TF32, codegen.DEFAULT_FLOAT, codegen.DEFAULT_INT) + return (ast.key, type(renderer), renderer.target, *[x.value for x in config]) + + orig_compile_linear = realize.compile_linear + def compile_linear(linear, beam=None, validate=False, input_uops=None, profile=None): + workers = int(os.getenv("PARALLEL_COMPILE", "0")) + beam_val = realize.BEAM.value if beam is None else beam + assert workers <= 1 or beam_val < 1, "PARALLEL_COMPILE and BEAM cannot be enabled together" + if workers > 1: + jobs = {} + for call in linear.src: + if call.op is not Ops.CALL or call.src[0].op not in (Ops.SINK, Ops.PROGRAM): + continue + renderer = Device[call.device if isinstance(call.device, str) else call.device[0]].renderer + if (key:=program_key(call.src[0], renderer)) not in codegen.to_program_cache: + jobs.setdefault(key, (call.src[0], renderer)) + if len(jobs) >= 2: + context = {k:v.value for k,v in ContextVar._cache.items()} + with multiprocessing.get_context("spawn").Pool(min(workers, len(jobs))) as pool: + compiled = pool.map(_tinygrad_compile_worker, ((*item, context) for item in jobs.values()), chunksize=1) + codegen.to_program_cache.update(zip(jobs, compiled, strict=True)) + return orig_compile_linear(linear, beam, validate, input_uops, profile) + realize.compile_linear = compile_linear + +_patch_tinygrad_parallel_compile() + NV12Frame = namedtuple("NV12Frame", ['width', 'height', 'stride', 'y_height', 'uv_height', 'size']) WARP_INPUTS = ['tfm', 'big_tfm'] From b47ebfcb777c42e049eddfefe9aa97b6ce155a4b Mon Sep 17 00:00:00 2001 From: Adeeb Shihadeh Date: Mon, 17 Aug 2026 21:33:38 -0700 Subject: [PATCH 5/7] explain compiler affinity --- openpilot/selfdrive/modeld/SConscript | 1 + 1 file changed, 1 insertion(+) diff --git a/openpilot/selfdrive/modeld/SConscript b/openpilot/selfdrive/modeld/SConscript index fbbebe056d700d..c7992ebb5dc7f3 100644 --- a/openpilot/selfdrive/modeld/SConscript +++ b/openpilot/selfdrive/modeld/SConscript @@ -79,6 +79,7 @@ for usbgpu in [False, True] if USBGPU else [False]: file_prefix, cmd_flags = ('big_', usbgpu_tg_flags) if usbgpu else ('big_' if os.getenv('BIG_INTO_SMALL') else '', tg_flags) driving_onnx_deps = get_existing_chunks(File(f"models/{file_prefix}driving_supercombo.onnx").abspath) camera_res_args = ' '.join(f'{cw}x{ch}' for cw, ch in CAMERA_CONFIGS) + # CPU 7 is isolated with isolcpus on AGNOS, so explicitly include it along with the other parallel compiler workers. taskset = 'taskset -c 0-7 ' if arch == 'comma_arm64' else '' cmd = (f'{cmd_flags} {mac_brew_string} {taskset}python3 {modeld_dir}/compile_modeld.py ' f'--model-size {model_w}x{model_h} ' From 2fd0b1a4f4f50ae6e21ef7e196f7c8fbe1b74e59 Mon Sep 17 00:00:00 2001 From: Adeeb Shihadeh Date: Tue, 18 Aug 2026 11:10:50 -0700 Subject: [PATCH 6/7] full cpufreq --- SConstruct | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/SConstruct b/SConstruct index bb5c35feb785ff..98951b8e13d999 100644 --- a/SConstruct +++ b/SConstruct @@ -55,6 +55,16 @@ assert arch in [ "Darwin", # macOS arm64 (x86 not supported) ] +if arch == "comma_arm64": + from openpilot.common.utils import sudo_write + for cpu in range(4, 8): + sudo_write('1', f'/sys/devices/system/cpu/cpu{cpu}/online') + for policy in ('0', '4'): + governor_path = f'/sys/devices/system/cpu/cpufreq/policy{policy}' + sudo_write('performance', f'{governor_path}/scaling_governor') + with open(f'{governor_path}/cpuinfo_max_freq') as f: + sudo_write(f.read().strip(), f'{governor_path}/scaling_max_freq') + pkg_names = ['acados', 'capnproto', 'ffmpeg', 'json11', 'ncurses', 'zeromq', 'zstd'] pkgs = [importlib.import_module(name) for name in pkg_names] acados = pkgs[pkg_names.index('acados')] From 8b9be3e6a32099ecdcbd78f0b28823cfa9ba925f Mon Sep 17 00:00:00 2001 From: Adeeb Shihadeh Date: Tue, 18 Aug 2026 19:50:52 -0700 Subject: [PATCH 7/7] rm that for now --- openpilot/selfdrive/modeld/SConscript | 4 +- openpilot/selfdrive/modeld/compile_modeld.py | 43 +------------------- 2 files changed, 3 insertions(+), 44 deletions(-) diff --git a/openpilot/selfdrive/modeld/SConscript b/openpilot/selfdrive/modeld/SConscript index c7992ebb5dc7f3..1f40fe1ce1d733 100644 --- a/openpilot/selfdrive/modeld/SConscript +++ b/openpilot/selfdrive/modeld/SConscript @@ -45,7 +45,7 @@ tg_devices = { # which device to put jit inputs to at runtime USBGPU = usbgpu_present() if USBGPU: - usbgpu_tg_flags = f'DEBUG=1 DEV=USB+AMD:LLVM WARP_DEV={tg_backend} FLOAT16=1 JIT_BATCH_SIZE=0 GMMU=0 TC_OPT=2 PARALLEL_COMPILE=4' + usbgpu_tg_flags = f'DEBUG=1 DEV=USB+AMD:LLVM WARP_DEV={tg_backend} FLOAT16=1 JIT_BATCH_SIZE=0 GMMU=0 TC_OPT=2' # the USB+AMD GPU takes an exclusive flock; serialize all targets that touch it usbgpu_lock = File("models/.usb_gpu.lock").abspath @@ -79,7 +79,7 @@ for usbgpu in [False, True] if USBGPU else [False]: file_prefix, cmd_flags = ('big_', usbgpu_tg_flags) if usbgpu else ('big_' if os.getenv('BIG_INTO_SMALL') else '', tg_flags) driving_onnx_deps = get_existing_chunks(File(f"models/{file_prefix}driving_supercombo.onnx").abspath) camera_res_args = ' '.join(f'{cw}x{ch}' for cw, ch in CAMERA_CONFIGS) - # CPU 7 is isolated with isolcpus on AGNOS, so explicitly include it along with the other parallel compiler workers. + # CPU 7 is isolated with isolcpus on AGNOS, so explicitly include it along with the other cores. taskset = 'taskset -c 0-7 ' if arch == 'comma_arm64' else '' cmd = (f'{cmd_flags} {mac_brew_string} {taskset}python3 {modeld_dir}/compile_modeld.py ' f'--model-size {model_w}x{model_h} ' diff --git a/openpilot/selfdrive/modeld/compile_modeld.py b/openpilot/selfdrive/modeld/compile_modeld.py index 6f28fb75ab863e..dec4944e1f8784 100755 --- a/openpilot/selfdrive/modeld/compile_modeld.py +++ b/openpilot/selfdrive/modeld/compile_modeld.py @@ -2,7 +2,6 @@ import argparse import atexit import math -import multiprocessing import os import time import shutil @@ -31,50 +30,10 @@ def fetch_fw(path, name, sha256): from tinygrad.tensor import Tensor -from tinygrad.helpers import Context, ContextVar +from tinygrad.helpers import Context from tinygrad.device import Device from tinygrad.engine.jit import TinyJit -def _tinygrad_compile_worker(args): - ast, renderer, context = args - from tinygrad.codegen import to_program - with Context(**{k:v for k,v in context.items() if k in ContextVar._cache}): - return to_program(ast, renderer) - -def _patch_tinygrad_parallel_compile(): - from tinygrad import codegen - from tinygrad.engine import realize - from tinygrad.uop.ops import Ops - - def program_key(ast, renderer): - config = (codegen.NOOPT, codegen.EMULATED_DTYPES, codegen.NOLOCALS, codegen.USE_TC, codegen.IMAGE, codegen.DISABLE_FAST_IDIV, - codegen.TRANSCENDENTAL, codegen.ALLOW_TF32, codegen.DEFAULT_FLOAT, codegen.DEFAULT_INT) - return (ast.key, type(renderer), renderer.target, *[x.value for x in config]) - - orig_compile_linear = realize.compile_linear - def compile_linear(linear, beam=None, validate=False, input_uops=None, profile=None): - workers = int(os.getenv("PARALLEL_COMPILE", "0")) - beam_val = realize.BEAM.value if beam is None else beam - assert workers <= 1 or beam_val < 1, "PARALLEL_COMPILE and BEAM cannot be enabled together" - if workers > 1: - jobs = {} - for call in linear.src: - if call.op is not Ops.CALL or call.src[0].op not in (Ops.SINK, Ops.PROGRAM): - continue - renderer = Device[call.device if isinstance(call.device, str) else call.device[0]].renderer - if (key:=program_key(call.src[0], renderer)) not in codegen.to_program_cache: - jobs.setdefault(key, (call.src[0], renderer)) - if len(jobs) >= 2: - context = {k:v.value for k,v in ContextVar._cache.items()} - with multiprocessing.get_context("spawn").Pool(min(workers, len(jobs))) as pool: - compiled = pool.map(_tinygrad_compile_worker, ((*item, context) for item in jobs.values()), chunksize=1) - codegen.to_program_cache.update(zip(jobs, compiled, strict=True)) - return orig_compile_linear(linear, beam, validate, input_uops, profile) - realize.compile_linear = compile_linear - -_patch_tinygrad_parallel_compile() - - NV12Frame = namedtuple("NV12Frame", ['width', 'height', 'stride', 'y_height', 'uv_height', 'size']) WARP_INPUTS = ['tfm', 'big_tfm'] POLICY_INPUTS = ['img_q', 'big_img_q', 'feat_q', 'desire_q', 'packed_npy_inputs']