mlonmcu 0.3.0.dev0__3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (376) hide show
  1. mlonmcu/__init__.py +26 -0
  2. mlonmcu/artifact.py +172 -0
  3. mlonmcu/cli/__init__.py +19 -0
  4. mlonmcu/cli/build.py +119 -0
  5. mlonmcu/cli/cleanup.py +75 -0
  6. mlonmcu/cli/common.py +227 -0
  7. mlonmcu/cli/compile.py +87 -0
  8. mlonmcu/cli/env.py +68 -0
  9. mlonmcu/cli/export.py +93 -0
  10. mlonmcu/cli/flow.py +67 -0
  11. mlonmcu/cli/helper/__init__.py +18 -0
  12. mlonmcu/cli/helper/filter.py +25 -0
  13. mlonmcu/cli/helper/parse.py +159 -0
  14. mlonmcu/cli/init.py +130 -0
  15. mlonmcu/cli/load.py +87 -0
  16. mlonmcu/cli/main.py +116 -0
  17. mlonmcu/cli/models.py +48 -0
  18. mlonmcu/cli/run.py +56 -0
  19. mlonmcu/cli/setup.py +136 -0
  20. mlonmcu/cli/tune.py +48 -0
  21. mlonmcu/config.py +219 -0
  22. mlonmcu/context/__init__.py +22 -0
  23. mlonmcu/context/context.py +545 -0
  24. mlonmcu/context/read_write_filelock.py +327 -0
  25. mlonmcu/environment/__init__.py +18 -0
  26. mlonmcu/environment/config.py +214 -0
  27. mlonmcu/environment/environment.py +501 -0
  28. mlonmcu/environment/init.py +191 -0
  29. mlonmcu/environment/list.py +81 -0
  30. mlonmcu/environment/loader.py +250 -0
  31. mlonmcu/environment/templates.py +69 -0
  32. mlonmcu/environment/writer.py +117 -0
  33. mlonmcu/feature/__init__.py +18 -0
  34. mlonmcu/feature/feature.py +230 -0
  35. mlonmcu/feature/features.py +1537 -0
  36. mlonmcu/feature/type.py +33 -0
  37. mlonmcu/flow/__init__.py +61 -0
  38. mlonmcu/flow/backend.py +234 -0
  39. mlonmcu/flow/framework.py +69 -0
  40. mlonmcu/flow/tflm/__init__.py +23 -0
  41. mlonmcu/flow/tflm/backend/__init__.py +23 -0
  42. mlonmcu/flow/tflm/backend/backend.py +47 -0
  43. mlonmcu/flow/tflm/backend/tflmc.py +129 -0
  44. mlonmcu/flow/tflm/backend/tflmi.py +385 -0
  45. mlonmcu/flow/tflm/framework.py +81 -0
  46. mlonmcu/flow/tvm/__init__.py +23 -0
  47. mlonmcu/flow/tvm/backend/__init__.py +26 -0
  48. mlonmcu/flow/tvm/backend/backend.py +282 -0
  49. mlonmcu/flow/tvm/backend/model_info.py +307 -0
  50. mlonmcu/flow/tvm/backend/python_utils.py +36 -0
  51. mlonmcu/flow/tvm/backend/tuner.py +41 -0
  52. mlonmcu/flow/tvm/backend/tvmaot.py +162 -0
  53. mlonmcu/flow/tvm/backend/tvmaotplus.py +51 -0
  54. mlonmcu/flow/tvm/backend/tvmc_utils.py +172 -0
  55. mlonmcu/flow/tvm/backend/tvmcg.py +102 -0
  56. mlonmcu/flow/tvm/backend/tvmllvm.py +106 -0
  57. mlonmcu/flow/tvm/backend/tvmrt.py +151 -0
  58. mlonmcu/flow/tvm/backend/wrapper.py +645 -0
  59. mlonmcu/flow/tvm/framework.py +96 -0
  60. mlonmcu/logging.py +79 -0
  61. mlonmcu/mlonmcu.py +19 -0
  62. mlonmcu/models/__init__.py +31 -0
  63. mlonmcu/models/convert_data.py +107 -0
  64. mlonmcu/models/frontend.py +571 -0
  65. mlonmcu/models/group.py +29 -0
  66. mlonmcu/models/lookup.py +345 -0
  67. mlonmcu/models/metadata.py +133 -0
  68. mlonmcu/models/model.py +104 -0
  69. mlonmcu/models/options.py +64 -0
  70. mlonmcu/models/utils.py +136 -0
  71. mlonmcu/platform/__init__.py +27 -0
  72. mlonmcu/platform/_platform.py +46 -0
  73. mlonmcu/platform/espidf/__init__.py +25 -0
  74. mlonmcu/platform/espidf/espidf.py +489 -0
  75. mlonmcu/platform/espidf/espidf_target.py +257 -0
  76. mlonmcu/platform/lookup.py +126 -0
  77. mlonmcu/platform/microtvm/__init__.py +25 -0
  78. mlonmcu/platform/microtvm/microtvm.py +580 -0
  79. mlonmcu/platform/microtvm/microtvm_arduino_target.py +68 -0
  80. mlonmcu/platform/microtvm/microtvm_backend.py +64 -0
  81. mlonmcu/platform/microtvm/microtvm_espidf_target.py +94 -0
  82. mlonmcu/platform/microtvm/microtvm_etissvp_target.py +98 -0
  83. mlonmcu/platform/microtvm/microtvm_host_target.py +46 -0
  84. mlonmcu/platform/microtvm/microtvm_spike_target.py +230 -0
  85. mlonmcu/platform/microtvm/microtvm_target.py +189 -0
  86. mlonmcu/platform/microtvm/microtvm_template_target.py +55 -0
  87. mlonmcu/platform/microtvm/microtvm_zephyr_target.py +91 -0
  88. mlonmcu/platform/mlif/__init__.py +25 -0
  89. mlonmcu/platform/mlif/mlif.py +300 -0
  90. mlonmcu/platform/mlif/mlif_target.py +86 -0
  91. mlonmcu/platform/platform.py +244 -0
  92. mlonmcu/platform/tvm/__init__.py +25 -0
  93. mlonmcu/platform/tvm/tvm.py +429 -0
  94. mlonmcu/platform/tvm/tvm_backend.py +57 -0
  95. mlonmcu/platform/tvm/tvm_target.py +165 -0
  96. mlonmcu/platform/zephyr/__init__.py +25 -0
  97. mlonmcu/platform/zephyr/zephyr.py +450 -0
  98. mlonmcu/platform/zephyr/zephyr_target.py +110 -0
  99. mlonmcu/plugins.py +26 -0
  100. mlonmcu/report.py +99 -0
  101. mlonmcu/session/__init__.py +18 -0
  102. mlonmcu/session/postprocess/__init__.py +43 -0
  103. mlonmcu/session/postprocess/postprocess.py +62 -0
  104. mlonmcu/session/postprocess/postprocesses.py +487 -0
  105. mlonmcu/session/run.py +1008 -0
  106. mlonmcu/session/session.py +373 -0
  107. mlonmcu/setup/__init__.py +18 -0
  108. mlonmcu/setup/cache.py +147 -0
  109. mlonmcu/setup/gen_requirements.py +619 -0
  110. mlonmcu/setup/setup.py +285 -0
  111. mlonmcu/setup/task.py +380 -0
  112. mlonmcu/setup/tasks/__init__.py +40 -0
  113. mlonmcu/setup/tasks/arm_gcc.py +58 -0
  114. mlonmcu/setup/tasks/cmsisnn.py +151 -0
  115. mlonmcu/setup/tasks/common.py +36 -0
  116. mlonmcu/setup/tasks/corstone300.py +64 -0
  117. mlonmcu/setup/tasks/espidf.py +87 -0
  118. mlonmcu/setup/tasks/etiss.py +147 -0
  119. mlonmcu/setup/tasks/llvm.py +67 -0
  120. mlonmcu/setup/tasks/mlif.py +49 -0
  121. mlonmcu/setup/tasks/muriscvnn.py +166 -0
  122. mlonmcu/setup/tasks/pulp.py +192 -0
  123. mlonmcu/setup/tasks/riscv_gcc.py +146 -0
  124. mlonmcu/setup/tasks/spike.py +190 -0
  125. mlonmcu/setup/tasks/tf.py +92 -0
  126. mlonmcu/setup/tasks/tflite.py +59 -0
  127. mlonmcu/setup/tasks/tflmc.py +141 -0
  128. mlonmcu/setup/tasks/tvm.py +172 -0
  129. mlonmcu/setup/tasks/utvmcg.py +96 -0
  130. mlonmcu/setup/tasks/zephyr.py +119 -0
  131. mlonmcu/setup/utils.py +374 -0
  132. mlonmcu/target/__init__.py +37 -0
  133. mlonmcu/target/_target.py +41 -0
  134. mlonmcu/target/arm/__init__.py +3 -0
  135. mlonmcu/target/arm/corstone300.py +235 -0
  136. mlonmcu/target/arm/util.py +109 -0
  137. mlonmcu/target/common.py +194 -0
  138. mlonmcu/target/elf.py +256 -0
  139. mlonmcu/target/host_x86.py +84 -0
  140. mlonmcu/target/metrics.py +75 -0
  141. mlonmcu/target/riscv/__init__.py +7 -0
  142. mlonmcu/target/riscv/etiss_pulpino.py +462 -0
  143. mlonmcu/target/riscv/gvsoc_pulp.py +215 -0
  144. mlonmcu/target/riscv/ovpsim.py +301 -0
  145. mlonmcu/target/riscv/riscv.py +185 -0
  146. mlonmcu/target/riscv/riscv_qemu.py +166 -0
  147. mlonmcu/target/riscv/spike.py +243 -0
  148. mlonmcu/target/riscv/util.py +134 -0
  149. mlonmcu/target/target.py +200 -0
  150. mlonmcu/utils.py +49 -0
  151. mlonmcu/version.py +21 -0
  152. mlonmcu-0.3.0.dev0.dist-info/LICENSE +202 -0
  153. mlonmcu-0.3.0.dev0.dist-info/METADATA +343 -0
  154. mlonmcu-0.3.0.dev0.dist-info/NOTICE +8 -0
  155. mlonmcu-0.3.0.dev0.dist-info/RECORD +376 -0
  156. mlonmcu-0.3.0.dev0.dist-info/WHEEL +5 -0
  157. mlonmcu-0.3.0.dev0.dist-info/entry_points.txt +3 -0
  158. mlonmcu-0.3.0.dev0.dist-info/top_level.txt +1 -0
  159. resources/frameworks/tvm/crt_config/README.md +7 -0
  160. resources/frameworks/tvm/crt_config/crt_config.h +53 -0
  161. resources/frameworks/tvm/tuning_records/riscv_cpu_v0.01.log.best +46 -0
  162. resources/patches/fix_esp32c3_march.patch +20 -0
  163. resources/patches/pulp_freertos_support.patch +55 -0
  164. resources/platforms/espidf/micro_speech_esp32devboard/CMakeLists.txt +29 -0
  165. resources/platforms/espidf/micro_speech_esp32devboard/README.md +70 -0
  166. resources/platforms/espidf/micro_speech_esp32devboard/components/mlif2/CMakeLists.txt +78 -0
  167. resources/platforms/espidf/micro_speech_esp32devboard/components/mlif2/Kconfig +76 -0
  168. resources/platforms/espidf/micro_speech_esp32devboard/components/mlif2/default_data.c +26 -0
  169. resources/platforms/espidf/micro_speech_esp32devboard/components/mlif2/default_model_support/process_input.c +35 -0
  170. resources/platforms/espidf/micro_speech_esp32devboard/components/mlif2/default_model_support/process_output.c +45 -0
  171. resources/platforms/espidf/micro_speech_esp32devboard/components/mlif2/include/exit.h +26 -0
  172. resources/platforms/espidf/micro_speech_esp32devboard/components/mlif2/include/ml_interface.h +70 -0
  173. resources/platforms/espidf/micro_speech_esp32devboard/components/mlif2/include/printing.h +31 -0
  174. resources/platforms/espidf/micro_speech_esp32devboard/components/mlif2/ml_interface.c +74 -0
  175. resources/platforms/espidf/micro_speech_esp32devboard/components/mlif2/ml_interface_tflm.cc +64 -0
  176. resources/platforms/espidf/micro_speech_esp32devboard/components/mlif2/ml_interface_tvm.c +53 -0
  177. resources/platforms/espidf/micro_speech_esp32devboard/components/tflite-lib/CMakeLists.txt +140 -0
  178. resources/platforms/espidf/micro_speech_esp32devboard/components/tflite-lib/Kconfig +8 -0
  179. resources/platforms/espidf/micro_speech_esp32devboard/components/tvm-lib/CMakeLists.txt +66 -0
  180. resources/platforms/espidf/micro_speech_esp32devboard/components/tvm-lib/Kconfig +7 -0
  181. resources/platforms/espidf/micro_speech_esp32devboard/example_test.py +38 -0
  182. resources/platforms/espidf/micro_speech_esp32devboard/info.txt +9 -0
  183. resources/platforms/espidf/micro_speech_esp32devboard/main/CMakeLists.txt +58 -0
  184. resources/platforms/espidf/micro_speech_esp32devboard/main/audio_provider.cc +193 -0
  185. resources/platforms/espidf/micro_speech_esp32devboard/main/audio_provider.h +49 -0
  186. resources/platforms/espidf/micro_speech_esp32devboard/main/command_responder.cc +53 -0
  187. resources/platforms/espidf/micro_speech_esp32devboard/main/command_responder.h +35 -0
  188. resources/platforms/espidf/micro_speech_esp32devboard/main/feature_provider.cc +132 -0
  189. resources/platforms/espidf/micro_speech_esp32devboard/main/feature_provider.cc.orig +145 -0
  190. resources/platforms/espidf/micro_speech_esp32devboard/main/feature_provider.h +55 -0
  191. resources/platforms/espidf/micro_speech_esp32devboard/main/kissfft/COPYING +11 -0
  192. resources/platforms/espidf/micro_speech_esp32devboard/main/kissfft/_kiss_fft_guts.h +168 -0
  193. resources/platforms/espidf/micro_speech_esp32devboard/main/kissfft/kiss_fft.c +408 -0
  194. resources/platforms/espidf/micro_speech_esp32devboard/main/kissfft/kiss_fft.h +124 -0
  195. resources/platforms/espidf/micro_speech_esp32devboard/main/kissfft/tools/kiss_fftr.c +159 -0
  196. resources/platforms/espidf/micro_speech_esp32devboard/main/kissfft/tools/kiss_fftr.h +46 -0
  197. resources/platforms/espidf/micro_speech_esp32devboard/main/main.cc +63 -0
  198. resources/platforms/espidf/micro_speech_esp32devboard/main/main_functions.cc +204 -0
  199. resources/platforms/espidf/micro_speech_esp32devboard/main/main_functions.h +37 -0
  200. resources/platforms/espidf/micro_speech_esp32devboard/main/micro_features_generator.cc +121 -0
  201. resources/platforms/espidf/micro_speech_esp32devboard/main/micro_features_generator.h +38 -0
  202. resources/platforms/espidf/micro_speech_esp32devboard/main/micro_model_settings.cc +23 -0
  203. resources/platforms/espidf/micro_speech_esp32devboard/main/micro_model_settings.h +43 -0
  204. resources/platforms/espidf/micro_speech_esp32devboard/main/microfrontend/lib/bits.h +102 -0
  205. resources/platforms/espidf/micro_speech_esp32devboard/main/microfrontend/lib/fft.cc +52 -0
  206. resources/platforms/espidf/micro_speech_esp32devboard/main/microfrontend/lib/fft.h +50 -0
  207. resources/platforms/espidf/micro_speech_esp32devboard/main/microfrontend/lib/fft_util.cc +69 -0
  208. resources/platforms/espidf/micro_speech_esp32devboard/main/microfrontend/lib/fft_util.h +34 -0
  209. resources/platforms/espidf/micro_speech_esp32devboard/main/microfrontend/lib/filterbank.c +134 -0
  210. resources/platforms/espidf/micro_speech_esp32devboard/main/microfrontend/lib/filterbank.h +63 -0
  211. resources/platforms/espidf/micro_speech_esp32devboard/main/microfrontend/lib/filterbank_util.c +220 -0
  212. resources/platforms/espidf/micro_speech_esp32devboard/main/microfrontend/lib/filterbank_util.h +50 -0
  213. resources/platforms/espidf/micro_speech_esp32devboard/main/microfrontend/lib/frontend.c +72 -0
  214. resources/platforms/espidf/micro_speech_esp32devboard/main/microfrontend/lib/frontend.h +64 -0
  215. resources/platforms/espidf/micro_speech_esp32devboard/main/microfrontend/lib/frontend_util.c +85 -0
  216. resources/platforms/espidf/micro_speech_esp32devboard/main/microfrontend/lib/frontend_util.h +52 -0
  217. resources/platforms/espidf/micro_speech_esp32devboard/main/microfrontend/lib/kiss_fft_common.h +48 -0
  218. resources/platforms/espidf/micro_speech_esp32devboard/main/microfrontend/lib/kiss_fft_int16.cc +8 -0
  219. resources/platforms/espidf/micro_speech_esp32devboard/main/microfrontend/lib/kiss_fft_int16.h +34 -0
  220. resources/platforms/espidf/micro_speech_esp32devboard/main/microfrontend/lib/log_lut.c +30 -0
  221. resources/platforms/espidf/micro_speech_esp32devboard/main/microfrontend/lib/log_lut.h +40 -0
  222. resources/platforms/espidf/micro_speech_esp32devboard/main/microfrontend/lib/log_scale.c +83 -0
  223. resources/platforms/espidf/micro_speech_esp32devboard/main/microfrontend/lib/log_scale.h +39 -0
  224. resources/platforms/espidf/micro_speech_esp32devboard/main/microfrontend/lib/log_scale_util.c +27 -0
  225. resources/platforms/espidf/micro_speech_esp32devboard/main/microfrontend/lib/log_scale_util.h +45 -0
  226. resources/platforms/espidf/micro_speech_esp32devboard/main/microfrontend/lib/noise_reduction.c +51 -0
  227. resources/platforms/espidf/micro_speech_esp32devboard/main/microfrontend/lib/noise_reduction.h +46 -0
  228. resources/platforms/espidf/micro_speech_esp32devboard/main/microfrontend/lib/noise_reduction_util.c +45 -0
  229. resources/platforms/espidf/micro_speech_esp32devboard/main/microfrontend/lib/noise_reduction_util.h +50 -0
  230. resources/platforms/espidf/micro_speech_esp32devboard/main/microfrontend/lib/pcan_gain_control.c +56 -0
  231. resources/platforms/espidf/micro_speech_esp32devboard/main/microfrontend/lib/pcan_gain_control.h +47 -0
  232. resources/platforms/espidf/micro_speech_esp32devboard/main/microfrontend/lib/pcan_gain_control_util.c +92 -0
  233. resources/platforms/espidf/micro_speech_esp32devboard/main/microfrontend/lib/pcan_gain_control_util.h +57 -0
  234. resources/platforms/espidf/micro_speech_esp32devboard/main/microfrontend/lib/window.c +70 -0
  235. resources/platforms/espidf/micro_speech_esp32devboard/main/microfrontend/lib/window.h +49 -0
  236. resources/platforms/espidf/micro_speech_esp32devboard/main/microfrontend/lib/window_util.c +73 -0
  237. resources/platforms/espidf/micro_speech_esp32devboard/main/microfrontend/lib/window_util.h +45 -0
  238. resources/platforms/espidf/micro_speech_esp32devboard/main/pins.c +14 -0
  239. resources/platforms/espidf/micro_speech_esp32devboard/main/pins.h +30 -0
  240. resources/platforms/espidf/micro_speech_esp32devboard/main/recognize_commands.cc +150 -0
  241. resources/platforms/espidf/micro_speech_esp32devboard/main/recognize_commands.h +173 -0
  242. resources/platforms/espidf/micro_speech_esp32devboard/main/ringbuf.c +333 -0
  243. resources/platforms/espidf/micro_speech_esp32devboard/main/ringbuf.h +74 -0
  244. resources/platforms/espidf/micro_speech_esp32devboard/sdkconfig.defaults +36 -0
  245. resources/platforms/espidf/project/CMakeLists.txt +29 -0
  246. resources/platforms/espidf/project/README.md +70 -0
  247. resources/platforms/espidf/project/components/mlif/CMakeLists.txt +77 -0
  248. resources/platforms/espidf/project/components/mlif/Kconfig +76 -0
  249. resources/platforms/espidf/project/components/mlif/default_data.c +26 -0
  250. resources/platforms/espidf/project/components/mlif/default_model_support/process_input.c +35 -0
  251. resources/platforms/espidf/project/components/mlif/default_model_support/process_output.c +45 -0
  252. resources/platforms/espidf/project/components/mlif/include/exit.h +26 -0
  253. resources/platforms/espidf/project/components/mlif/include/ml_interface.h +60 -0
  254. resources/platforms/espidf/project/components/mlif/include/printing.h +31 -0
  255. resources/platforms/espidf/project/components/mlif/ml_interface.c +54 -0
  256. resources/platforms/espidf/project/components/mlif/ml_interface_tflm.cc +40 -0
  257. resources/platforms/espidf/project/components/mlif/ml_interface_tvm.c +40 -0
  258. resources/platforms/espidf/project/components/tflite-lib/CMakeLists.txt +142 -0
  259. resources/platforms/espidf/project/components/tflite-lib/Kconfig +8 -0
  260. resources/platforms/espidf/project/components/tvm-lib/CMakeLists.txt +66 -0
  261. resources/platforms/espidf/project/components/tvm-lib/Kconfig +7 -0
  262. resources/platforms/espidf/project/example_test.py +38 -0
  263. resources/platforms/espidf/project/main/CMakeLists.txt +25 -0
  264. resources/platforms/espidf/project/main/main.c +57 -0
  265. resources/platforms/espidf/project/sdkconfig.defaults +26 -0
  266. resources/platforms/zephyr/project/CMakeLists.txt +22 -0
  267. resources/platforms/zephyr/project/Kconfig +101 -0
  268. resources/platforms/zephyr/project/README.rst +33 -0
  269. resources/platforms/zephyr/project/cmake/AddAllSubdirs.cmake +21 -0
  270. resources/platforms/zephyr/project/cmake/ChooseTarget.cmake +22 -0
  271. resources/platforms/zephyr/project/cmake/LinkTFLite.cmake +223 -0
  272. resources/platforms/zephyr/project/cmake/LinkTVM.cmake +68 -0
  273. resources/platforms/zephyr/project/cmake/LookupClang.cmake +41 -0
  274. resources/platforms/zephyr/project/cmake/backends/tflmc.cmake +5 -0
  275. resources/platforms/zephyr/project/cmake/backends/tflmi.cmake +5 -0
  276. resources/platforms/zephyr/project/cmake/backends/tvmaot.cmake +10 -0
  277. resources/platforms/zephyr/project/cmake/backends/tvmaotplus.cmake +10 -0
  278. resources/platforms/zephyr/project/cmake/backends/tvmcg.cmake +10 -0
  279. resources/platforms/zephyr/project/cmake/backends/tvmllvm.cmake +8 -0
  280. resources/platforms/zephyr/project/cmake/backends/tvmrt.cmake +8 -0
  281. resources/platforms/zephyr/project/cmake/extras.cmake +9 -0
  282. resources/platforms/zephyr/project/cmake/features/auto_vectorize_gcc.cmake +65 -0
  283. resources/platforms/zephyr/project/cmake/features/auto_vectorize_llvm.cmake +20 -0
  284. resources/platforms/zephyr/project/cmake/features/cmsisnn.cmake +86 -0
  285. resources/platforms/zephyr/project/cmake/features/muriscvnn.cmake +87 -0
  286. resources/platforms/zephyr/project/cmake/frameworks/tflm.cmake +1 -0
  287. resources/platforms/zephyr/project/cmake/frameworks/tflm_mlif.cmake +8 -0
  288. resources/platforms/zephyr/project/cmake/frameworks/tvm.cmake +1 -0
  289. resources/platforms/zephyr/project/cmake/frameworks/tvm_mlif.cmake +37 -0
  290. resources/platforms/zephyr/project/lib/CMakeLists.txt +13 -0
  291. resources/platforms/zephyr/project/lib/drivers/fake_input.c +10 -0
  292. resources/platforms/zephyr/project/lib/drivers/fake_input.h +8 -0
  293. resources/platforms/zephyr/project/lib/ml_interface/CMakeLists.txt +31 -0
  294. resources/platforms/zephyr/project/lib/ml_interface/default_data.c +8 -0
  295. resources/platforms/zephyr/project/lib/ml_interface/default_model_support/CMakeLists.txt +2 -0
  296. resources/platforms/zephyr/project/lib/ml_interface/default_model_support/process_input.c +17 -0
  297. resources/platforms/zephyr/project/lib/ml_interface/default_model_support/process_output.c +27 -0
  298. resources/platforms/zephyr/project/lib/ml_interface/ml_interface.c +36 -0
  299. resources/platforms/zephyr/project/lib/ml_interface/ml_interface.h +47 -0
  300. resources/platforms/zephyr/project/lib/ml_interface/ml_interface_tflm.cc +22 -0
  301. resources/platforms/zephyr/project/lib/ml_interface/ml_interface_tvm.c +22 -0
  302. resources/platforms/zephyr/project/lib/support/CMakeLists.txt +2 -0
  303. resources/platforms/zephyr/project/lib/support/dummy.c +0 -0
  304. resources/platforms/zephyr/project/lib/support/exit.h +8 -0
  305. resources/platforms/zephyr/project/lib/support/printing.h +11 -0
  306. resources/platforms/zephyr/project/lib/support/quantize.h +23 -0
  307. resources/platforms/zephyr/project/lib/targets/corstone300.c +62 -0
  308. resources/platforms/zephyr/project/lib/targets/default.c +2 -0
  309. resources/platforms/zephyr/project/lib/targets/etiss_pulpino.c +112 -0
  310. resources/platforms/zephyr/project/lib/targets/generic_riscv.c +112 -0
  311. resources/platforms/zephyr/project/lib/targets/riscv_qemu.c +112 -0
  312. resources/platforms/zephyr/project/lib/targets/target.h +2 -0
  313. resources/platforms/zephyr/project/prj.conf +11 -0
  314. resources/platforms/zephyr/project/sample.yaml +19 -0
  315. resources/platforms/zephyr/project/src/main.c +71 -0
  316. resources/platforms/zephyr/project2/CMakeLists.txt +72 -0
  317. resources/platforms/zephyr/project2/Kconfig +101 -0
  318. resources/platforms/zephyr/project2/README.rst +33 -0
  319. resources/platforms/zephyr/project2/cmake/AddAllSubdirs.cmake +21 -0
  320. resources/platforms/zephyr/project2/cmake/ChooseTarget.cmake +22 -0
  321. resources/platforms/zephyr/project2/cmake/LinkTFLite.cmake +223 -0
  322. resources/platforms/zephyr/project2/cmake/LinkTVM.cmake +68 -0
  323. resources/platforms/zephyr/project2/cmake/LookupClang.cmake +41 -0
  324. resources/platforms/zephyr/project2/cmake/backends/tflmc.cmake +5 -0
  325. resources/platforms/zephyr/project2/cmake/backends/tflmi.cmake +5 -0
  326. resources/platforms/zephyr/project2/cmake/backends/tvmaot.cmake +10 -0
  327. resources/platforms/zephyr/project2/cmake/backends/tvmaotplus.cmake +10 -0
  328. resources/platforms/zephyr/project2/cmake/backends/tvmcg.cmake +10 -0
  329. resources/platforms/zephyr/project2/cmake/backends/tvmllvm.cmake +8 -0
  330. resources/platforms/zephyr/project2/cmake/backends/tvmrt.cmake +8 -0
  331. resources/platforms/zephyr/project2/cmake/extras.cmake +9 -0
  332. resources/platforms/zephyr/project2/cmake/features/auto_vectorize_gcc.cmake +65 -0
  333. resources/platforms/zephyr/project2/cmake/features/auto_vectorize_llvm.cmake +20 -0
  334. resources/platforms/zephyr/project2/cmake/features/cmsisnn.cmake +86 -0
  335. resources/platforms/zephyr/project2/cmake/features/muriscvnn.cmake +87 -0
  336. resources/platforms/zephyr/project2/cmake/frameworks/tflm.cmake +1 -0
  337. resources/platforms/zephyr/project2/cmake/frameworks/tflm_mlif.cmake +8 -0
  338. resources/platforms/zephyr/project2/cmake/frameworks/tvm.cmake +1 -0
  339. resources/platforms/zephyr/project2/cmake/frameworks/tvm_mlif.cmake +37 -0
  340. resources/platforms/zephyr/project2/default_data.c +8 -0
  341. resources/platforms/zephyr/project2/default_model_support/CMakeLists.txt +3 -0
  342. resources/platforms/zephyr/project2/default_model_support/process_input.c +17 -0
  343. resources/platforms/zephyr/project2/default_model_support/process_output.c +27 -0
  344. resources/platforms/zephyr/project2/lib/CMakeLists.txt +13 -0
  345. resources/platforms/zephyr/project2/lib/drivers/fake_input.c +10 -0
  346. resources/platforms/zephyr/project2/lib/drivers/fake_input.h +8 -0
  347. resources/platforms/zephyr/project2/lib/support/CMakeLists.txt +2 -0
  348. resources/platforms/zephyr/project2/lib/support/dummy.c +0 -0
  349. resources/platforms/zephyr/project2/lib/support/exit.h +8 -0
  350. resources/platforms/zephyr/project2/lib/support/printing.h +11 -0
  351. resources/platforms/zephyr/project2/lib/support/quantize.h +23 -0
  352. resources/platforms/zephyr/project2/lib/targets/corstone300.c +62 -0
  353. resources/platforms/zephyr/project2/lib/targets/default.c +2 -0
  354. resources/platforms/zephyr/project2/lib/targets/etiss_pulpino.c +112 -0
  355. resources/platforms/zephyr/project2/lib/targets/generic_riscv.c +112 -0
  356. resources/platforms/zephyr/project2/lib/targets/riscv_qemu.c +112 -0
  357. resources/platforms/zephyr/project2/lib/targets/target.h +2 -0
  358. resources/platforms/zephyr/project2/ml_interface.c +36 -0
  359. resources/platforms/zephyr/project2/ml_interface.h +47 -0
  360. resources/platforms/zephyr/project2/ml_interface_tflm.cc +22 -0
  361. resources/platforms/zephyr/project2/ml_interface_tvm.c +22 -0
  362. resources/platforms/zephyr/project2/prj.conf +13 -0
  363. resources/platforms/zephyr/project2/sample.yaml +19 -0
  364. resources/platforms/zephyr/project2/settings.json +6 -0
  365. resources/platforms/zephyr/project2/src/main.c +35 -0
  366. resources/pycharm/.run/README.md +73 -0
  367. resources/pycharm/.run/step1_ init env.run.xml +24 -0
  368. resources/pycharm/.run/step2_ setup.run.xml +25 -0
  369. resources/pycharm/.run/step3_ run resnet on pulpxpulpissimo.run.xml +25 -0
  370. resources/templates/default.yml.j2 +258 -0
  371. resources/templates/dev.yml.j2 +212 -0
  372. resources/templates/minimal.yml.j2 +234 -0
  373. resources/templates/phi.yml.j2 +282 -0
  374. resources/templates/pulp.yml.j2 +267 -0
  375. resources/templates/research.yml.j2 +237 -0
  376. resources/templates/tumeda.yml.j2 +239 -0
@@ -0,0 +1,450 @@
1
+ #
2
+ # Copyright (c) 2022 TUM Department of Electrical and Computer Engineering.
3
+ #
4
+ # This file is part of MLonMCU.
5
+ # See https://github.com/tum-ei-eda/mlonmcu.git for further info.
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+ #
19
+ """Zephyr Platform"""
20
+
21
+ import re
22
+ import os
23
+ import time
24
+ import shutil
25
+ import tempfile
26
+ from pathlib import Path
27
+ import pkg_resources
28
+
29
+
30
+ from mlonmcu.setup import utils
31
+ from mlonmcu.artifact import Artifact, ArtifactFormat
32
+ from mlonmcu.logging import get_logger
33
+ from mlonmcu.target import get_targets
34
+ from mlonmcu.target.target import Target
35
+ from mlonmcu.config import str2bool
36
+
37
+ from ..platform import CompilePlatform, TargetPlatform
38
+ from .zephyr_target import create_zephyr_platform_target
39
+
40
+ logger = get_logger()
41
+
42
+
43
+ def get_project_template(name="project2"): # Workaround which only support tvmaot!!!
44
+ zephyr_templates = pkg_resources.resource_listdir("mlonmcu", os.path.join("..", "resources", "platforms", "zephyr"))
45
+ if name not in zephyr_templates:
46
+ return None
47
+ fname = pkg_resources.resource_filename("mlonmcu", os.path.join("..", "resources", "platforms", "zephyr", name))
48
+ return fname
49
+
50
+
51
+ class ZephyrPlatform(CompilePlatform, TargetPlatform):
52
+ """Zephyr Platform class."""
53
+
54
+ FEATURES = CompilePlatform.FEATURES + TargetPlatform.FEATURES + ["benchmark"]
55
+
56
+ DEFAULTS = {
57
+ **CompilePlatform.DEFAULTS,
58
+ **TargetPlatform.DEFAULTS,
59
+ "project_template": None,
60
+ "project_dir": None,
61
+ "port": None,
62
+ # "port": "/dev/ttyUSB0",
63
+ "baud": 115200,
64
+ "wait_for_user": True,
65
+ "flash_only": False,
66
+ "optimize": None, # values: 0,1,2,3,s
67
+ }
68
+
69
+ REQUIRED = ["zephyr.install_dir", "zephyr.sdk_dir", "zephyr.venv_dir"]
70
+
71
+ def __init__(self, features=None, config=None):
72
+ super().__init__(
73
+ "zephyr",
74
+ features=features,
75
+ config=config,
76
+ )
77
+ self.tempdir = None
78
+ self.project_name = "app"
79
+ self.project_dir = None
80
+
81
+ @property
82
+ def zephyr_install_dir(self):
83
+ return Path(self.config["zephyr.install_dir"])
84
+
85
+ @property
86
+ def zephyr_sdk_dir(self):
87
+ return Path(self.config["zephyr.sdk_dir"])
88
+
89
+ @property
90
+ def zephyr_venv_dir(self):
91
+ return Path(self.config["zephyr.venv_dir"])
92
+
93
+ @property
94
+ def wait_for_user(self):
95
+ value = self.config["wait_for_user"]
96
+ return str2bool(value) if not isinstance(value, (bool, int)) else value
97
+
98
+ @property
99
+ def flash_only(self):
100
+ # TODO: get rid of this
101
+ value = self.config["flash_only"]
102
+ return str2bool(value) if not isinstance(value, (bool, int)) else value
103
+
104
+ @property
105
+ def optimize(self):
106
+ return self.config["optimize"]
107
+
108
+ def invoke_west(self, *args, **kwargs):
109
+ env = os.environ.copy()
110
+ env["ZEPHYR_BASE"] = str(self.zephyr_install_dir / "zephyr")
111
+ env["ZEPHYR_SDK_INSTALL_DIR"] = str(self.zephyr_sdk_dir)
112
+ cmd = ". " + str(self.zephyr_venv_dir / "bin" / "activate") + " && west " + " ".join([str(arg) for arg in args])
113
+ out = utils.exec_getout(
114
+ cmd, shell=True, env=env, **kwargs, executable="/bin/bash"
115
+ ) # TODO: using shell=True is insecure but right now we can not avoid it?
116
+ return out
117
+
118
+ def init_directory(self, path=None, context=None):
119
+ if self.project_dir is not None:
120
+ self.project_dir.mkdir(exist_ok=True)
121
+ logger.debug("Project directory already initialized")
122
+ return
123
+ dir_name = self.name
124
+ if path is not None:
125
+ self.project_dir = Path(path)
126
+ elif self.config["project_dir"] is not None:
127
+ self.project_dir = Path(self.config["project_dir"])
128
+ else:
129
+ if context:
130
+ assert "temp" in context.environment.paths
131
+ self.project_dir = (
132
+ context.environment.paths["temp"].path / dir_name
133
+ ) # TODO: Need to lock this for parallel builds
134
+ else:
135
+ logger.debug(
136
+ "Creating temporary directory because no context was available "
137
+ "and 'zephyr.project_dir' was not supplied"
138
+ )
139
+ self.tempdir = tempfile.TemporaryDirectory()
140
+ self.project_dir = Path(self.tempdir.name) / dir_name
141
+ logger.debug("Temporary project directory: %s", self.project_dir)
142
+ self.project_dir.mkdir(exist_ok=True)
143
+
144
+ def get_supported_targets(self):
145
+ with tempfile.TemporaryDirectory() as temp:
146
+ f = Path(temp) / "CMakeLists.txt"
147
+ # f.touch()
148
+ with open(f, "w") as handle:
149
+ handle.write(
150
+ """
151
+ cmake_minimum_required(VERSION 3.22)
152
+ find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
153
+ project(ProjectName)
154
+ """
155
+ )
156
+ b = Path(temp) / "build"
157
+
158
+ # This will fail
159
+ # Workaround used to get list of supported boards...
160
+ def _handle(code):
161
+ return 0
162
+
163
+ text = self.invoke_west(
164
+ "build", "-d", b, "-b", "help", temp, live=False, print_output=False, handle_exit=_handle
165
+ )
166
+ # Warning: This will fail if a python executable is NOT available in the system. Aliasing
167
+ # python3 to python will not work. Not sure how this would handle a system which only has python2 installed?
168
+ target_names = re.compile(r"^ (\S+)$", re.MULTILINE).findall(text)
169
+
170
+ return [
171
+ f"zephyr_{name}" for name in target_names if len(name) > 0 and " " not in name
172
+ ] # TODO: consider dropping zephyr_ prefix
173
+
174
+ def create_target(self, name):
175
+ assert name in self.get_supported_targets(), f"{name} is not a valid Zephyr target"
176
+ targets = get_targets()
177
+ if name in targets:
178
+ base = targets[name]
179
+ else:
180
+ base = Target
181
+ return create_zephyr_platform_target(name, self, base=base)
182
+
183
+ @property
184
+ def project_template(self):
185
+ return self.config["project_template"]
186
+
187
+ @property
188
+ def port(self):
189
+ return self.config["port"]
190
+
191
+ @property
192
+ def baud(self):
193
+ return self.config["baud"]
194
+
195
+ @property
196
+ def build_dir(self):
197
+ return self.project_dir / "build"
198
+
199
+ def close(self):
200
+ if self.tempdir:
201
+ self.tempdir.cleanup()
202
+
203
+ def get_west_cmake_args(self):
204
+ cmake_defs = {"CMAKE_BUILD_TYPE": "Debug" if self.debug else "Release"}
205
+ return [f"-D{key}={value}" for key, value in cmake_defs.items()]
206
+
207
+ def prepare(self, target, src):
208
+ self.init_directory()
209
+ template_dir = self.project_template
210
+ if template_dir is None:
211
+ template_dir = get_project_template()
212
+ else:
213
+ template_dir = Path(template_dir)
214
+ if not template_dir.is_dir():
215
+ template_dir = get_project_template(name=str(template_dir))
216
+ assert template_dir is not None, f"Provided project template does not exists: {template_dir}"
217
+ shutil.copytree(template_dir, self.project_dir, dirs_exist_ok=True)
218
+
219
+ def write_defaults(filename):
220
+ defs = self.definitions
221
+ with open(filename, "w", encoding="utf-8") as f:
222
+ f.write("CONFIG_CPLUSPLUS=y\n")
223
+ f.write("CONFIG_NEWLIB_LIBC=y\n")
224
+ f.write("CONFIG_REBOOT=y\n")
225
+ f.write("CONFIG_STD_CPP20=y\n")
226
+ f.write("CONFIG_LIB_CPLUSPLUS=y\n")
227
+ f.write("CONFIG_TIMING_FUNCTIONS=y\n")
228
+ # f.write("CONFIG_NEWLIB_LIBC_FLOAT_PRINTF=y\n")
229
+ # f.write("CONFIG_CBPRINTF_FP_SUPPORT=y\n")
230
+ if self.debug:
231
+ f.write("CONFIG_DEBUG=y\n")
232
+ if not self.optimize:
233
+ f.write("CONFIG_DEBUG_OPTIMIZATIONS=y\n")
234
+ else:
235
+ f.write("CONFIG_DEBUG=n\n")
236
+ if self.optimize:
237
+ if str(self.optimize) == "s":
238
+ f.write("CONFIG_SIZE_OPTIMIZATIONS=y\n")
239
+ elif str(self.optimize) == "0":
240
+ f.write("CONFIG_NO_OPTIMIZATIONS=y\n")
241
+ elif str(self.optimize) == "g":
242
+ f.write("CONFIG_DEBUG_OPTIMIZATIONS=y\n")
243
+ elif str(self.optimize) == "2":
244
+ f.write("CONFIG_SPEED_OPTIMIZATIONS=y\n")
245
+ elif str(self.optimize) == "3":
246
+ f.write("CONFIG_SPEED_OPTIMIZATIONS=y\n")
247
+ f.write('CONFIG_COMPILER_OPT="-O3"\n')
248
+ else:
249
+ raise RuntimeError(f"Unsupported optimization level for Zephyr platform: {self.optimize}")
250
+ # f.write("CONFIG_PARTITION_TABLE_SINGLE_APP_LARGE=y\n")
251
+ # if self.debug:
252
+ # f.write("CONFIG_OPTIMIZATION_LEVEL_DEBUG=y\n")
253
+ # f.write("CONFIG_COMPILER_OPTIMIZATION_LEVEL_DEBUG=y\n")
254
+ # else:
255
+ # # Trying to reduce the binary size as much as possible
256
+ # # (https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-guides/performance/size.html)
257
+ # f.write("CONFIG_BOOTLOADER_LOG_LEVEL_NONE=y\n")
258
+ # f.write("CONFIG_BOOTLOADER_LOG_LEVEL=0\n")
259
+ # f.write("CONFIG_BOOT_ROM_LOG_ALWAYS_OFF=y\n")
260
+ # f.write("CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_DISABLE=y\n")
261
+ # f.write("CONFIG_COMPILER_OPTIMIZATION_ASSERTION_LEVEL=0\n")
262
+ # f.write("CONFIG_COMPILER_OPTIMIZATION_CHECKS_SILENT=y\n")
263
+ # f.write("CONFIG_HAL_DEFAULT_ASSERTION_LEVEL=0\n")
264
+ # f.write("CONFIG_LOG_DEFAULT_LEVEL_NONE=y\n")
265
+ # f.write("CONFIG_LOG_DEFAULT_LEVEL=0\n")
266
+ # f.write("CONFIG_LOG_MAXIMUM_LEVEL=0\n")
267
+ # f.write("CONFIG_NEWLIB_NANO_FORMAT=y\n")
268
+ # f.write("CONFIG_COMPILER_OPTIMIZATION_LEVEL_RELEASE=y\n")
269
+ # f.write("CONFIG_OPTIMIZATION_LEVEL_RELEASE=y\n")
270
+ # optimize_for_size = True
271
+ # if optimize_for_size:
272
+ # f.write("CONFIG_COMPILER_OPTIMIZATION_SIZE=y\n")
273
+ # else:
274
+ # f.write("CONFIG_COMPILER_OPTIMIZATION_PERF=y\n")
275
+ # watchdog_sec = 60
276
+ # f.write(f"CONFIG_ESP_TASK_WDT_TIMEOUT_S={watchdog_sec}\n")
277
+ f.write(f'CONFIG_MLONMCU_CODEGEN_DIR="{src}"\n')
278
+ for key, value in defs.items():
279
+ if isinstance(value, bool):
280
+ value = "y" if value else "n"
281
+ else:
282
+ value = f'"{value}"'
283
+ f.write(f"CONFIG_{key}={value}\n")
284
+
285
+ write_defaults(self.project_dir / "prj.conf")
286
+ zephyr_target = target.name.split("_", 1)[-1]
287
+ westArgs = [ # cmake only
288
+ "build",
289
+ "-d",
290
+ self.build_dir,
291
+ "-b",
292
+ zephyr_target,
293
+ self.project_dir,
294
+ *self.get_west_cmake_args(),
295
+ "-c",
296
+ ]
297
+ return self.invoke_west(*westArgs, live=self.print_outputs)
298
+
299
+ def compile(self, target, src=None):
300
+ out = self.prepare(target, src)
301
+ # TODO: build with cmake options
302
+ # TODO: support self.num_threads
303
+ # self.build_dir.mkdir()
304
+ zephyr_target = target.name.split("_", 1)[-1]
305
+ westArgs = [
306
+ "build",
307
+ "-d",
308
+ self.build_dir,
309
+ "-b",
310
+ zephyr_target,
311
+ self.project_dir,
312
+ f"-o=-j{self.num_threads}",
313
+ ]
314
+ out += self.invoke_west(*westArgs, live=self.print_outputs)
315
+ return out
316
+
317
+ def generate_elf(self, src, target, model=None, data_file=None):
318
+ artifacts = []
319
+ out = self.compile(target, src=src)
320
+ elf_name = "zephyr.elf"
321
+ elf_file = self.project_dir / "build" / "zephyr" / elf_name
322
+ # TODO: just use path instead of raw data?
323
+ if self.tempdir:
324
+ # Warning: The porject context will get destroyed afterwards wehen using a temporory directory
325
+ with open(elf_file, "rb") as handle:
326
+ data = handle.read()
327
+ artifact = Artifact("generic_mlif", raw=data, fmt=ArtifactFormat.RAW)
328
+ artifacts.append(artifact)
329
+ else:
330
+ artifact = Artifact(elf_name, path=elf_file, fmt=ArtifactFormat.PATH)
331
+ artifacts.append(artifact)
332
+ metrics = self.get_metrics(elf_file)
333
+ content = metrics.to_csv(include_optional=True) # TODO: store df instead?
334
+ metrics_artifact = Artifact("metrics.csv", content=content, fmt=ArtifactFormat.TEXT)
335
+ artifacts.append(metrics_artifact)
336
+ stdout_artifact = Artifact(
337
+ "zephyr_out.log", content=out, fmt=ArtifactFormat.TEXT # TODO: split into one file per command
338
+ ) # TODO: rename to tvmaot_out.log?
339
+ artifacts.append(stdout_artifact)
340
+ self.artifacts = artifacts
341
+
342
+ def get_serial(self, target):
343
+ port = target.port
344
+ if port is None:
345
+ # Falling back to 'espidf.baud'
346
+ assert self.port is not None, f"Please provide '{target.name}.port'"
347
+ port = self.port
348
+ baud = target.baud
349
+ if baud is None:
350
+ # Falling back to 'espidf.baud'
351
+ assert self.baud is not None, f"Please provide '{target.name}.baud'"
352
+ baud = self.baud
353
+ return port, baud
354
+
355
+ def flash(self, elf, target, timeout=120):
356
+ # Ignore elf, as we use self.project_dir instead
357
+ # TODO: add alternative approach which allows passing elf instead
358
+ if elf is not None:
359
+ logger.debug("Ignoring ELF file for zephyr platform")
360
+ # TODO: implement timeout
361
+ # TODO: make sure that already compiled? -> error or just call compile routine?
362
+ if self.wait_for_user: # INTERACTIVE
363
+ answer = input(
364
+ f"Make sure that the device '{target.name}' is connected before you press [Enter]"
365
+ + " (Type 'Abort' to cancel)"
366
+ )
367
+ if answer.lower() == "abort":
368
+ return ""
369
+ logger.debug("Flashing target software")
370
+
371
+ westArgs = [
372
+ "flash",
373
+ "-d",
374
+ self.build_dir,
375
+ ]
376
+ if "esp32" in target.name: # TODO: implement this for other runners as well
377
+ port, baud = self.get_serial(target)
378
+ if port:
379
+ westArgs.extend(["--esp-device", port])
380
+ if baud:
381
+ westArgs.extend(["--esp-baud-rate", baud])
382
+ self.invoke_west(*westArgs, live=self.print_outputs)
383
+
384
+ def monitor(self, target, timeout=60):
385
+ if self.flash_only:
386
+ return ""
387
+
388
+ port, baud = self.get_serial(target)
389
+
390
+ def _monitor_helper(port, baud, verbose=False, start_match=None, end_match=None, timeout=60):
391
+ # Local import to get rid of pyserial dependency
392
+
393
+ import serial
394
+
395
+ # start_match and end_match are inclusive
396
+ found_start = start_match is None
397
+ outStr = ""
398
+ if timeout:
399
+ pass # TODO: implement timeout
400
+ # The following is a custom initialization sequence inspired by
401
+ # (https://github.com/espressif/esp-idf/blob/master/tools/idf_monitor_base/serial_reader.py)
402
+ # Required for esp32c3!
403
+ high = False
404
+ low = True
405
+ ser = serial.Serial(port, baud)
406
+ ser.close()
407
+ ser.dtr = False
408
+ ser.rts = False
409
+ time.sleep(1)
410
+ ser.dtr = low
411
+ ser.rts = high
412
+ ser.dtr = ser.dtr
413
+ ser.open()
414
+ ser.dtr = high
415
+ ser.rts = low
416
+ ser.dtr = ser.dtr
417
+ time.sleep(0.002)
418
+ ser.rts = high
419
+ ser.dtr = ser.dtr
420
+ try:
421
+ while True:
422
+ try:
423
+ ser_bytes = ser.readline()
424
+ new_line = ser_bytes.decode("utf-8", errors="replace")
425
+ if verbose:
426
+ print(new_line.replace("\n", ""))
427
+ if start_match and start_match in new_line:
428
+ outStr = new_line
429
+ found_start = True
430
+ else:
431
+ outStr = outStr + new_line
432
+ if found_start:
433
+ if end_match and end_match in new_line:
434
+ break
435
+ except KeyboardInterrupt:
436
+ logger.warning("Stopped processing serial port (KeyboardInterrupt)")
437
+ finally:
438
+ ser.close()
439
+ return outStr
440
+
441
+ logger.debug("Monitoring target software")
442
+ # TODO: implement timeout
443
+ return _monitor_helper(
444
+ port,
445
+ baud,
446
+ verbose=self.print_outputs,
447
+ start_match="MLonMCU: START",
448
+ end_match="MLonMCU: STOP",
449
+ timeout=timeout,
450
+ )
@@ -0,0 +1,110 @@
1
+ #
2
+ # Copyright (c) 2022 TUM Department of Electrical and Computer Engineering.
3
+ #
4
+ # This file is part of MLonMCU.
5
+ # See https://github.com/tum-ei-eda/mlonmcu.git for further info.
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+ #
19
+ import re
20
+ import os
21
+
22
+ from mlonmcu.target.target import Target
23
+ from mlonmcu.target.metrics import Metrics
24
+
25
+ from mlonmcu.logging import get_logger
26
+
27
+ logger = get_logger()
28
+
29
+
30
+ def create_zephyr_platform_target(name, platform, base=Target):
31
+ class ZephyrPlatformTarget(base):
32
+ FEATURES = base.FEATURES + []
33
+
34
+ DEFAULTS = {
35
+ **base.DEFAULTS,
36
+ "timeout_sec": 0, # disabled
37
+ "port": None,
38
+ "baud": None,
39
+ }
40
+ REQUIRED = base.REQUIRED + []
41
+
42
+ def __init__(self, features=None, config=None):
43
+ super().__init__(name=name, features=features, config=config)
44
+ self.platform = platform
45
+
46
+ @property
47
+ def timeout_sec(self):
48
+ return int(self.config["timeout_sec"])
49
+
50
+ @property
51
+ def port(self):
52
+ return self.config["port"]
53
+
54
+ @property
55
+ def baud(self):
56
+ return self.config["baud"]
57
+
58
+ def exec(self, program, *args, cwd=os.getcwd(), **kwargs):
59
+ """Use target to execute a executable with given arguments"""
60
+ if len(args) > 0:
61
+ raise RuntimeError("Program arguments are not supported for real hardware devices")
62
+
63
+ assert self.platform is not None, "Zephyr targets need a platform to execute programs"
64
+
65
+ if self.timeout_sec > 0:
66
+ raise NotImplementedError
67
+
68
+ # Zephyr actually wants a project directory, but we only get the elf now. As a workaround we
69
+ # assume the elf is right in the build directory inside the project directory
70
+
71
+ ret = self.platform.run(program, self)
72
+ return ret
73
+
74
+ def parse_stdout(self, out):
75
+ # cpu_cycles = re.search(r"Total Cycles: (.*)", out)
76
+ # if not cpu_cycles:
77
+ # logger.warning("unexpected script output (cycles)")
78
+ # cycles = None
79
+ # else:
80
+ # cycles = int(float(cpu_cycles.group(1)))
81
+ cpu_time_us = re.search(r"Total Time: (.*) us", out)
82
+ if not cpu_time_us:
83
+ logger.warning("unexpected script output (time_us)")
84
+ time_us = None
85
+ else:
86
+ time_us = int(float(cpu_time_us.group(1)))
87
+ # return cycles, time_us
88
+ return time_us
89
+
90
+ def get_metrics(self, elf, directory, handle_exit=None):
91
+ if self.print_outputs:
92
+ out = self.exec(elf, cwd=directory, live=True, handle_exit=handle_exit)
93
+ else:
94
+ out = self.exec(
95
+ elf, cwd=directory, live=False, print_func=lambda *args, **kwargs: None, handle_exit=handle_exit
96
+ )
97
+ # cycles, time_us = self.parse_stdout(out)
98
+ time_us = self.parse_stdout(out)
99
+
100
+ metrics = Metrics()
101
+ # metrics.add("Cycles", cycles)
102
+ time_s = time_us / 1e6 if time_us is not None else time_us
103
+ metrics.add("Runtime [s]", time_s)
104
+
105
+ return metrics, out, []
106
+
107
+ def get_arch(self):
108
+ return "unkwown"
109
+
110
+ return ZephyrPlatformTarget
mlonmcu/plugins.py ADDED
@@ -0,0 +1,26 @@
1
+ #
2
+ # Copyright (c) 2022 TUM Department of Electrical and Computer Engineering.
3
+ #
4
+ # This file is part of MLonMCU.
5
+ # See https://github.com/tum-ei-eda/mlonmcu.git for further info.
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+ #
19
+ """Utilities for MLonMCUs extension mechanism."""
20
+ import importlib
21
+
22
+
23
+ def process_extensions(file):
24
+ spec = importlib.util.spec_from_file_location("plugins.extensions", file)
25
+ mod = importlib.util.module_from_spec(spec)
26
+ spec.loader.exec_module(mod)
mlonmcu/report.py ADDED
@@ -0,0 +1,99 @@
1
+ #
2
+ # Copyright (c) 2022 TUM Department of Electrical and Computer Engineering.
3
+ #
4
+ # This file is part of MLonMCU.
5
+ # See https://github.com/tum-ei-eda/mlonmcu.git for further info.
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+ #
19
+ """Definitions of the Report class used by MLonMCU sessions and runs."""
20
+ from pathlib import Path
21
+ import pandas as pd
22
+
23
+
24
+ pd.set_option("display.max_columns", None)
25
+ pd.set_option("display.max_rows", None)
26
+ pd.set_option("display.width", 0)
27
+
28
+ SUPPORTED_FMTS = ["csv", "xlsx"]
29
+
30
+
31
+ class Report:
32
+ """Report class wrapped around multiple pandas dataframes."""
33
+
34
+ def __init__(self):
35
+ self.pre_df = pd.DataFrame()
36
+ self.main_df = pd.DataFrame()
37
+ self.post_df = pd.DataFrame()
38
+
39
+ @property
40
+ def df(self):
41
+ """Combine the three internal dataframes to a large one and return in."""
42
+ # TODO: handle this properly by either adding NAN or use a single set(pre=, post=, main=) method
43
+ return pd.concat([self.pre_df, self.main_df, self.post_df], axis=1)
44
+
45
+ def export(self, path):
46
+ """Export the report to a file.
47
+
48
+ Arguments
49
+ ---------
50
+ path : str
51
+ Destination path.
52
+
53
+ """
54
+ ext = Path(path).suffix[1:]
55
+ assert ext in SUPPORTED_FMTS, f"Unsupported report format: {ext}"
56
+ parent = Path(path).parent
57
+ if not parent.is_dir():
58
+ parent.mkdir()
59
+ if ext == "csv":
60
+ self.df.to_csv(path, index=False)
61
+ elif ext in ["xlsx", "xls"]:
62
+ self.df.to_excel(path, index=False)
63
+ else:
64
+ raise RuntimeError()
65
+
66
+ # def append(self, *args, **kwargs):
67
+ # self.df = self.df.append(*args, **kwargs, ignore_index=True)
68
+
69
+ def set_pre(self, data):
70
+ """Setter for the left third of the dataframe."""
71
+ self.pre_df = pd.DataFrame.from_records(data).reset_index(drop=True)
72
+
73
+ def set_post(self, data):
74
+ """Setter for the right third of the dataframe."""
75
+ self.post_df = pd.DataFrame.from_records(data).reset_index(drop=True)
76
+
77
+ def set_main(self, data):
78
+ """Setter for the center part of the dataframe."""
79
+ self.main_df = pd.DataFrame.from_records(data).reset_index(drop=True)
80
+
81
+ def set(self, pre=None, main=None, post=None):
82
+ """Setter for the dataframe."""
83
+ size = len(pre)
84
+ self.set_pre(pre if pre is not None else {})
85
+ if len(main) != size:
86
+ assert len(main) == 0
87
+ # self.set_main(pd.Series())
88
+ else:
89
+ self.set_main(main if main is not None else {})
90
+ self.set_post(post if post is not None else {})
91
+
92
+ def add(self, reports):
93
+ """Helper function to append a line to an existing report."""
94
+ if not isinstance(reports, list):
95
+ reports = [reports]
96
+ for report in reports:
97
+ self.pre_df = pd.concat([self.pre_df, report.pre_df], axis=0).reset_index(drop=True)
98
+ self.main_df = pd.concat([self.main_df, report.main_df], axis=0).reset_index(drop=True)
99
+ self.post_df = pd.concat([self.post_df, report.post_df], axis=0).reset_index(drop=True)