11import os
22import glob
3+ from openpilot .common .file_chunker import chunk_file , get_chunk_paths
34
45Import ('env' , 'arch' )
6+ chunker_file = File ("#common/file_chunker.py" )
57lenv = env .Clone ()
6- CHUNK_BYTES = int (os .environ .get ("TG_CHUNK_BYTES" , str (45 * 1024 * 1024 )))
78
89tinygrad_root = env .Dir ("#" ).abspath
910tinygrad_files = ["#" + x for x in glob .glob (env .Dir ("#tinygrad_repo" ).relpath + "/**" , recursive = True , root_dir = tinygrad_root )
1011 if 'pycache' not in x and os .path .isfile (os .path .join (tinygrad_root , x ))]
1112
13+ def estimate_pickle_max_size (onnx_size ):
14+ return 1.2 * onnx_size + 10 * 1024 * 1024 # 20% + 10MB is plenty
15+
1216# Get model metadata
1317for model_name in ['driving_vision' , 'driving_policy' , 'dmonitoring_model' ]:
1418 fn = File (f"models/{ model_name } " ).abspath
@@ -26,39 +30,29 @@ image_flag = {
2630 'larch64' : 'IMAGE=2' ,
2731}.get (arch , 'IMAGE=0' )
2832script_files = [File (Dir ("#selfdrive/modeld" ).File ("compile_warp.py" ).abspath )]
29- cmd = f'{ tg_flags } python3 { Dir ("#selfdrive/modeld" ).abspath } /compile_warp.py '
33+ compile_warp_cmd = f'{ tg_flags } python3 { Dir ("#selfdrive/modeld" ).abspath } /compile_warp.py '
3034from openpilot .common .transformations .camera import _ar_ox_fisheye , _os_fisheye
3135warp_targets = []
3236for cam in [_ar_ox_fisheye , _os_fisheye ]:
3337 w , h = cam .width , cam .height
3438 warp_targets += [File (f"models/warp_{ w } x{ h } _tinygrad.pkl" ).abspath , File (f"models/dm_warp_{ w } x{ h } _tinygrad.pkl" ).abspath ]
35- lenv .Command (warp_targets , tinygrad_files + script_files , cmd )
39+ lenv .Command (warp_targets , tinygrad_files + script_files , compile_warp_cmd )
3640
3741def tg_compile (flags , model_name ):
3842 pythonpath_string = 'PYTHONPATH="${PYTHONPATH}:' + env .Dir ("#tinygrad_repo" ).abspath + '"'
3943 fn = File (f"models/{ model_name } " ).abspath
40-
41- out = fn + "_tinygrad.pkl"
42- full = out + ".full"
43- parts = out + ".parts"
44-
45- full_node = lenv .Command (
46- full ,
47- [fn + ".onnx" ] + tinygrad_files ,
48- f'{ pythonpath_string } { flags } { image_flag } python3 { Dir ("#tinygrad_repo" ).abspath } /examples/openpilot/compile3.py { fn } .onnx { full } '
44+ pkl = fn + "_tinygrad.pkl"
45+ onnx_path = fn + ".onnx"
46+ chunk_targets = get_chunk_paths (pkl , estimate_pickle_max_size (os .path .getsize (onnx_path )))
47+ def do_chunk (target , source , env ):
48+ chunk_file (pkl , chunk_targets )
49+ return lenv .Command (
50+ chunk_targets ,
51+ [onnx_path ] + tinygrad_files + [chunker_file ],
52+ [f'{ pythonpath_string } { flags } { image_flag } python3 { Dir ("#tinygrad_repo" ).abspath } /examples/openpilot/compile3.py { fn } .onnx { pkl } ' ,
53+ do_chunk ]
4954 )
5055
51- split_script = File (Dir ("#selfdrive/modeld" ).File ("external_pickle.py" ).abspath )
52- parts_node = lenv .Command (
53- parts ,
54- [full_node , split_script , Value (str (CHUNK_BYTES ))],
55- [f'python3 { split_script .abspath } { full } { out } { CHUNK_BYTES } ' , Delete (full )],
56- )
57-
58- lenv .NoCache (parts_node )
59- lenv .AlwaysBuild (parts_node )
60- return parts_node
61-
6256# Compile small models
6357for model_name in ['driving_vision' , 'driving_policy' , 'dmonitoring_model' ]:
6458 tg_compile (tg_flags , model_name )
0 commit comments