SConscript 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import os
  2. Import('env', 'arch', 'cereal', 'messaging', 'common', 'gpucommon', 'visionipc', 'transformations')
  3. lenv = env.Clone()
  4. libs = [cereal, messaging, common, visionipc, gpucommon,
  5. 'OpenCL', 'SNPE', 'capnp', 'zmq', 'kj', 'yuv']
  6. def get_dlsym_offset():
  7. """Returns the offset between dlopen and dlsym in libdl.so"""
  8. import ctypes
  9. libdl = ctypes.PyDLL('libdl.so')
  10. dlopen = ctypes.cast(libdl.dlopen, ctypes.c_void_p).value
  11. dlsym = ctypes.cast(libdl.dlsym, ctypes.c_void_p).value
  12. return dlsym - dlopen
  13. common_src = [
  14. "models/commonmodel.cc",
  15. "runners/snpemodel.cc",
  16. "transforms/loadyuv.cc",
  17. "transforms/transform.cc"
  18. ]
  19. thneed_src = [
  20. "thneed/thneed_common.cc",
  21. "thneed/thneed_qcom2.cc",
  22. "thneed/serialize.cc",
  23. "runners/thneedmodel.cc",
  24. ]
  25. use_thneed = not GetOption('no_thneed')
  26. if arch == "larch64":
  27. libs += ['gsl', 'CB', 'pthread', 'dl']
  28. if use_thneed:
  29. common_src += thneed_src
  30. dlsym_offset = get_dlsym_offset()
  31. lenv['CXXFLAGS'].append("-DUSE_THNEED")
  32. lenv['CXXFLAGS'].append(f"-DDLSYM_OFFSET={dlsym_offset}")
  33. else:
  34. libs += ['pthread']
  35. if not GetOption('snpe'):
  36. # for onnx support
  37. common_src += ['runners/onnxmodel.cc']
  38. # tell runners to use onnx
  39. lenv['CFLAGS'].append("-DUSE_ONNX_MODEL")
  40. lenv['CXXFLAGS'].append("-DUSE_ONNX_MODEL")
  41. if arch == "Darwin":
  42. # fix OpenCL
  43. del libs[libs.index('OpenCL')]
  44. lenv['FRAMEWORKS'] = ['OpenCL']
  45. # no SNPE on Mac
  46. del libs[libs.index('SNPE')]
  47. del common_src[common_src.index('runners/snpemodel.cc')]
  48. common_model = lenv.Object(common_src)
  49. lenv.Program('_dmonitoringmodeld', [
  50. "dmonitoringmodeld.cc",
  51. "models/dmonitoring.cc",
  52. ]+common_model, LIBS=libs)
  53. # build thneed model
  54. if use_thneed and arch == "larch64" or GetOption('pc_thneed'):
  55. fn = File("models/supercombo").abspath
  56. if GetOption('pc_thneed'):
  57. cmd = f"cd {Dir('#').abspath}/tinygrad_repo && GPU=1 NATIVE_EXPLOG=1 OPTWG=1 UNSAFE_FLOAT4=1 DEBUGCL=1 python3 openpilot/compile.py {fn}.onnx {fn}.thneed"
  58. else:
  59. cmd = f"cd {Dir('#').abspath}/tinygrad_repo && FLOAT16=1 MATMUL=1 PYOPENCL_NO_CACHE=1 NATIVE_EXPLOG=1 OPTWG=1 UNSAFE_FLOAT4=1 DEBUGCL=1 python3 openpilot/compile.py {fn}.onnx {fn}.thneed"
  60. # is there a better way then listing all of tinygrad?
  61. lenv.Command(fn + ".thneed", [fn + ".onnx",
  62. "#tinygrad_repo/openpilot/compile.py",
  63. "#tinygrad_repo/accel/opencl/conv.cl",
  64. "#tinygrad_repo/accel/opencl/matmul.cl",
  65. "#tinygrad_repo/accel/opencl/ops_opencl.py",
  66. "#tinygrad_repo/accel/opencl/preprocessing.py",
  67. "#tinygrad_repo/extra/onnx.py",
  68. "#tinygrad_repo/extra/thneed.py",
  69. "#tinygrad_repo/extra/utils.py",
  70. "#tinygrad_repo/tinygrad/llops/ops_gpu.py",
  71. "#tinygrad_repo/tinygrad/llops/ops_opencl.py",
  72. "#tinygrad_repo/tinygrad/helpers.py",
  73. "#tinygrad_repo/tinygrad/mlops.py",
  74. "#tinygrad_repo/tinygrad/ops.py",
  75. "#tinygrad_repo/tinygrad/shapetracker.py",
  76. "#tinygrad_repo/tinygrad/tensor.py",
  77. "#tinygrad_repo/tinygrad/nn/__init__.py"
  78. ], cmd)
  79. llenv = lenv.Clone()
  80. if GetOption('pc_thneed'):
  81. pc_thneed_src = [
  82. "thneed/thneed_common.cc",
  83. "thneed/thneed_pc.cc",
  84. "thneed/serialize.cc",
  85. "runners/thneedmodel.cc",
  86. ]
  87. llenv['CFLAGS'].append("-DUSE_THNEED")
  88. llenv['CXXFLAGS'].append("-DUSE_THNEED")
  89. common_model += llenv.Object(pc_thneed_src)
  90. libs += ['dl']
  91. llenv.Program('_modeld', [
  92. "modeld.cc",
  93. "models/driving.cc",
  94. ]+common_model, LIBS=libs + transformations)
  95. lenv.Program('_navmodeld', [
  96. "navmodeld.cc",
  97. "models/nav.cc",
  98. ]+common_model, LIBS=libs + transformations)