react-native-executorch 0.5.6 → 0.5.8

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 (237) hide show
  1. package/android/libs/classes.jar +0 -0
  2. package/android/src/main/cpp/CMakeLists.txt +23 -14
  3. package/common/rnexecutorch/RnExecutorchInstaller.cpp +4 -21
  4. package/common/rnexecutorch/host_objects/ModelHostObject.h +67 -51
  5. package/common/rnexecutorch/models/llm/LLM.cpp +24 -1
  6. package/common/rnexecutorch/models/llm/LLM.h +4 -1
  7. package/common/rnexecutorch/threads/GlobalThreadPool.h +79 -0
  8. package/common/rnexecutorch/threads/HighPerformanceThreadPool.h +364 -0
  9. package/common/rnexecutorch/threads/utils/ThreadUtils.h +29 -0
  10. package/common/runner/runner.cpp +9 -3
  11. package/common/runner/runner.h +4 -3
  12. package/common/runner/text_token_generator.h +28 -10
  13. package/lib/Error.js +53 -0
  14. package/lib/ThreadPool.d.ts +10 -0
  15. package/lib/ThreadPool.js +28 -0
  16. package/lib/common/Logger.d.ts +8 -0
  17. package/lib/common/Logger.js +19 -0
  18. package/lib/constants/directories.js +2 -0
  19. package/lib/constants/llmDefaults.d.ts +6 -0
  20. package/lib/constants/llmDefaults.js +16 -0
  21. package/lib/constants/modelUrls.d.ts +223 -0
  22. package/lib/constants/modelUrls.js +322 -0
  23. package/lib/constants/ocr/models.d.ts +882 -0
  24. package/lib/constants/ocr/models.js +182 -0
  25. package/lib/constants/ocr/symbols.js +139 -0
  26. package/lib/constants/sttDefaults.d.ts +28 -0
  27. package/lib/constants/sttDefaults.js +68 -0
  28. package/lib/controllers/LLMController.d.ts +47 -0
  29. package/lib/controllers/LLMController.js +213 -0
  30. package/lib/controllers/OCRController.js +67 -0
  31. package/lib/controllers/SpeechToTextController.d.ts +56 -0
  32. package/lib/controllers/SpeechToTextController.js +349 -0
  33. package/lib/controllers/VerticalOCRController.js +70 -0
  34. package/lib/hooks/computer_vision/useClassification.d.ts +15 -0
  35. package/lib/hooks/computer_vision/useClassification.js +7 -0
  36. package/lib/hooks/computer_vision/useImageEmbeddings.d.ts +15 -0
  37. package/lib/hooks/computer_vision/useImageEmbeddings.js +7 -0
  38. package/lib/hooks/computer_vision/useImageSegmentation.d.ts +38 -0
  39. package/lib/hooks/computer_vision/useImageSegmentation.js +7 -0
  40. package/lib/hooks/computer_vision/useOCR.d.ts +20 -0
  41. package/lib/hooks/computer_vision/useOCR.js +41 -0
  42. package/lib/hooks/computer_vision/useObjectDetection.d.ts +15 -0
  43. package/lib/hooks/computer_vision/useObjectDetection.js +7 -0
  44. package/lib/hooks/computer_vision/useStyleTransfer.d.ts +15 -0
  45. package/lib/hooks/computer_vision/useStyleTransfer.js +7 -0
  46. package/lib/hooks/computer_vision/useVerticalOCR.d.ts +21 -0
  47. package/lib/hooks/computer_vision/useVerticalOCR.js +43 -0
  48. package/lib/hooks/general/useExecutorchModule.d.ts +13 -0
  49. package/lib/hooks/general/useExecutorchModule.js +7 -0
  50. package/lib/hooks/natural_language_processing/useLLM.d.ts +10 -0
  51. package/lib/hooks/natural_language_processing/useLLM.js +78 -0
  52. package/lib/hooks/natural_language_processing/useSpeechToText.d.ts +27 -0
  53. package/lib/hooks/natural_language_processing/useSpeechToText.js +49 -0
  54. package/lib/hooks/natural_language_processing/useTextEmbeddings.d.ts +16 -0
  55. package/lib/hooks/natural_language_processing/useTextEmbeddings.js +7 -0
  56. package/lib/hooks/natural_language_processing/useTokenizer.d.ts +17 -0
  57. package/lib/hooks/natural_language_processing/useTokenizer.js +52 -0
  58. package/lib/hooks/useModule.js +45 -0
  59. package/lib/hooks/useNonStaticModule.d.ts +20 -0
  60. package/lib/hooks/useNonStaticModule.js +49 -0
  61. package/lib/index.d.ts +48 -0
  62. package/lib/index.js +58 -0
  63. package/lib/module/controllers/LLMController.js +21 -2
  64. package/lib/module/controllers/LLMController.js.map +1 -1
  65. package/lib/module/hooks/natural_language_processing/useLLM.js +6 -2
  66. package/lib/module/hooks/natural_language_processing/useLLM.js.map +1 -1
  67. package/lib/module/modules/natural_language_processing/LLMModule.js +7 -2
  68. package/lib/module/modules/natural_language_processing/LLMModule.js.map +1 -1
  69. package/lib/module/types/llm.js.map +1 -1
  70. package/lib/modules/BaseModule.js +25 -0
  71. package/lib/modules/BaseNonStaticModule.js +14 -0
  72. package/lib/modules/computer_vision/ClassificationModule.d.ts +8 -0
  73. package/lib/modules/computer_vision/ClassificationModule.js +17 -0
  74. package/lib/modules/computer_vision/ImageEmbeddingsModule.d.ts +8 -0
  75. package/lib/modules/computer_vision/ImageEmbeddingsModule.js +17 -0
  76. package/lib/modules/computer_vision/ImageSegmentationModule.d.ts +11 -0
  77. package/lib/modules/computer_vision/ImageSegmentationModule.js +27 -0
  78. package/lib/modules/computer_vision/OCRModule.d.ts +14 -0
  79. package/lib/modules/computer_vision/OCRModule.js +17 -0
  80. package/lib/modules/computer_vision/ObjectDetectionModule.d.ts +9 -0
  81. package/lib/modules/computer_vision/ObjectDetectionModule.js +17 -0
  82. package/lib/modules/computer_vision/StyleTransferModule.d.ts +8 -0
  83. package/lib/modules/computer_vision/StyleTransferModule.js +17 -0
  84. package/lib/modules/computer_vision/VerticalOCRModule.d.ts +14 -0
  85. package/lib/modules/computer_vision/VerticalOCRModule.js +19 -0
  86. package/lib/modules/general/ExecutorchModule.d.ts +7 -0
  87. package/lib/modules/general/ExecutorchModule.js +14 -0
  88. package/lib/modules/natural_language_processing/LLMModule.d.ts +28 -0
  89. package/lib/modules/natural_language_processing/LLMModule.js +45 -0
  90. package/lib/modules/natural_language_processing/SpeechToTextModule.d.ts +24 -0
  91. package/lib/modules/natural_language_processing/SpeechToTextModule.js +36 -0
  92. package/lib/modules/natural_language_processing/TextEmbeddingsModule.d.ts +9 -0
  93. package/lib/modules/natural_language_processing/TextEmbeddingsModule.js +21 -0
  94. package/lib/modules/natural_language_processing/TokenizerModule.d.ts +12 -0
  95. package/lib/modules/natural_language_processing/TokenizerModule.js +30 -0
  96. package/lib/native/NativeETInstaller.js +2 -0
  97. package/lib/native/NativeOCR.js +2 -0
  98. package/lib/native/NativeVerticalOCR.js +2 -0
  99. package/lib/native/RnExecutorchModules.d.ts +7 -0
  100. package/lib/native/RnExecutorchModules.js +18 -0
  101. package/lib/tsconfig.tsbuildinfo +1 -0
  102. package/lib/types/common.d.ts +32 -0
  103. package/lib/types/common.js +25 -0
  104. package/lib/types/imageSegmentation.js +26 -0
  105. package/lib/types/llm.d.ts +46 -0
  106. package/lib/types/llm.js +9 -0
  107. package/lib/types/objectDetection.js +94 -0
  108. package/lib/types/ocr.js +1 -0
  109. package/lib/types/stt.d.ts +94 -0
  110. package/lib/types/stt.js +85 -0
  111. package/lib/typescript/controllers/LLMController.d.ts +4 -2
  112. package/lib/typescript/controllers/LLMController.d.ts.map +1 -1
  113. package/lib/typescript/hooks/natural_language_processing/useLLM.d.ts.map +1 -1
  114. package/lib/typescript/modules/natural_language_processing/LLMModule.d.ts +4 -2
  115. package/lib/typescript/modules/natural_language_processing/LLMModule.d.ts.map +1 -1
  116. package/lib/typescript/types/llm.d.ts +7 -1
  117. package/lib/typescript/types/llm.d.ts.map +1 -1
  118. package/lib/utils/ResourceFetcher.d.ts +24 -0
  119. package/lib/utils/ResourceFetcher.js +305 -0
  120. package/lib/utils/ResourceFetcherUtils.d.ts +54 -0
  121. package/lib/utils/ResourceFetcherUtils.js +127 -0
  122. package/lib/utils/llm.d.ts +6 -0
  123. package/lib/utils/llm.js +72 -0
  124. package/lib/utils/stt.js +21 -0
  125. package/package.json +3 -1
  126. package/react-native-executorch.podspec +12 -31
  127. package/src/controllers/LLMController.ts +29 -5
  128. package/src/hooks/natural_language_processing/useLLM.ts +15 -1
  129. package/src/modules/natural_language_processing/LLMModule.ts +14 -2
  130. package/src/types/llm.ts +8 -0
  131. package/third-party/android/libs/cpuinfo/arm64-v8a/libcpuinfo.so +0 -0
  132. package/third-party/android/libs/executorch/arm64-v8a/libexecutorch.so +0 -0
  133. package/third-party/android/libs/executorch/x86_64/libexecutorch.so +0 -0
  134. package/third-party/android/libs/pthreadpool/arm64-v8a/libpthreadpool.so +0 -0
  135. package/third-party/android/libs/tokenizers-cpp/arm64-v8a/libsentencepiece.a +0 -0
  136. package/third-party/android/libs/tokenizers-cpp/arm64-v8a/libtokenizers_c.a +0 -0
  137. package/third-party/android/libs/tokenizers-cpp/arm64-v8a/libtokenizers_cpp.a +0 -0
  138. package/third-party/android/libs/tokenizers-cpp/x86_64/libsentencepiece.a +0 -0
  139. package/third-party/android/libs/tokenizers-cpp/x86_64/libtokenizers_c.a +0 -0
  140. package/third-party/android/libs/tokenizers-cpp/x86_64/libtokenizers_cpp.a +0 -0
  141. package/third-party/include/c10/macros/Export.h +2 -86
  142. package/third-party/include/c10/macros/Macros.h +28 -5
  143. package/third-party/include/c10/util/BFloat16-inl.h +1 -4
  144. package/third-party/include/c10/util/BFloat16.h +5 -8
  145. package/third-party/include/c10/util/Half.h +5 -0
  146. package/third-party/include/c10/util/bit_cast.h +1 -1
  147. package/third-party/include/c10/util/complex.h +639 -0
  148. package/third-party/include/c10/util/complex_math.h +399 -0
  149. package/third-party/include/c10/util/complex_utils.h +41 -0
  150. package/third-party/include/c10/util/irange.h +2 -2
  151. package/third-party/include/c10/util/overflows.h +95 -0
  152. package/third-party/include/executorch/ExecuTorchError.h +75 -0
  153. package/third-party/include/executorch/ExecuTorchModule.h +115 -11
  154. package/third-party/include/executorch/ExecuTorchTensor.h +731 -51
  155. package/third-party/include/executorch/ExecuTorchValue.h +61 -9
  156. package/third-party/include/executorch/extension/kernel_util/make_boxed_from_unboxed_functor.h +181 -0
  157. package/third-party/include/executorch/extension/kernel_util/meta_programming.h +108 -0
  158. package/third-party/include/executorch/extension/kernel_util/type_list.h +137 -0
  159. package/third-party/include/executorch/extension/module/bundled_module.h +131 -0
  160. package/third-party/include/executorch/extension/module/module.h +46 -20
  161. package/third-party/include/executorch/extension/threadpool/cpuinfo_utils.h +1 -3
  162. package/third-party/include/executorch/extension/threadpool/threadpool.h +1 -3
  163. package/third-party/include/executorch/extension/threadpool/threadpool_guard.h +35 -0
  164. package/third-party/include/executorch/runtime/backend/backend_execution_context.h +3 -3
  165. package/third-party/include/executorch/runtime/backend/backend_init_context.h +12 -6
  166. package/third-party/include/executorch/runtime/backend/backend_option_context.h +34 -0
  167. package/third-party/include/executorch/runtime/backend/interface.h +70 -9
  168. package/third-party/include/executorch/runtime/backend/options.h +206 -0
  169. package/third-party/include/executorch/runtime/core/evalue.h +19 -25
  170. package/third-party/include/executorch/runtime/core/event_tracer.h +32 -17
  171. package/third-party/include/executorch/runtime/core/event_tracer_hooks.h +23 -14
  172. package/third-party/include/executorch/runtime/core/exec_aten/exec_aten.h +32 -9
  173. package/third-party/include/executorch/runtime/core/exec_aten/util/dim_order_util.h +3 -2
  174. package/third-party/include/executorch/runtime/core/exec_aten/util/scalar_type_util.h +43 -75
  175. package/third-party/include/executorch/runtime/core/exec_aten/util/tensor_util.h +88 -87
  176. package/third-party/include/executorch/runtime/core/function_ref.h +100 -0
  177. package/third-party/include/executorch/runtime/core/named_data_map.h +14 -14
  178. package/third-party/include/executorch/runtime/core/portable_type/c10/c10/macros/Export.h +2 -86
  179. package/third-party/include/executorch/runtime/core/portable_type/c10/c10/macros/Macros.h +28 -5
  180. package/third-party/include/executorch/runtime/core/portable_type/c10/c10/util/BFloat16-inl.h +1 -4
  181. package/third-party/include/executorch/runtime/core/portable_type/c10/c10/util/BFloat16.h +5 -8
  182. package/third-party/include/executorch/runtime/core/portable_type/c10/c10/util/Half.h +5 -0
  183. package/third-party/include/executorch/runtime/core/portable_type/c10/c10/util/bit_cast.h +1 -1
  184. package/third-party/include/executorch/runtime/core/portable_type/c10/c10/util/complex.h +639 -0
  185. package/third-party/include/executorch/runtime/core/portable_type/c10/c10/util/complex_math.h +399 -0
  186. package/third-party/include/executorch/runtime/core/portable_type/c10/c10/util/complex_utils.h +41 -0
  187. package/third-party/include/executorch/runtime/core/portable_type/c10/c10/util/irange.h +2 -2
  188. package/third-party/include/executorch/runtime/core/portable_type/c10/c10/util/overflows.h +95 -0
  189. package/third-party/include/executorch/runtime/core/portable_type/c10/torch/headeronly/macros/Export.h +88 -0
  190. package/third-party/include/executorch/runtime/core/portable_type/complex.h +6 -29
  191. package/third-party/include/executorch/runtime/core/portable_type/tensor_impl.h +20 -0
  192. package/third-party/include/executorch/runtime/core/span.h +4 -0
  193. package/third-party/include/executorch/runtime/core/tag.h +19 -0
  194. package/third-party/include/executorch/runtime/core/tensor_layout.h +2 -2
  195. package/third-party/include/executorch/runtime/executor/method.h +15 -3
  196. package/third-party/include/executorch/runtime/executor/method_meta.h +34 -5
  197. package/third-party/include/executorch/runtime/executor/program.h +3 -4
  198. package/third-party/include/executorch/runtime/executor/pte_data_map.h +9 -8
  199. package/third-party/include/executorch/runtime/executor/tensor_parser.h +14 -13
  200. package/third-party/include/executorch/runtime/kernel/kernel_runtime_context.h +5 -5
  201. package/third-party/include/executorch/runtime/kernel/operator_registry.h +21 -19
  202. package/third-party/include/executorch/runtime/platform/compiler.h +8 -0
  203. package/third-party/include/executorch/runtime/platform/platform.h +126 -0
  204. package/third-party/include/headeronly/macros/Export.h +88 -0
  205. package/third-party/include/tokenizers-cpp/tokenizers_c.h +61 -0
  206. package/third-party/include/torch/headeronly/macros/Export.h +88 -0
  207. package/third-party/ios/ExecutorchLib.xcframework/Info.plist +43 -0
  208. package/third-party/ios/ExecutorchLib.xcframework/ios-arm64/ExecutorchLib.framework/ExecutorchLib +0 -0
  209. package/third-party/ios/ExecutorchLib.xcframework/ios-arm64/ExecutorchLib.framework/Info.plist +0 -0
  210. package/third-party/ios/ExecutorchLib.xcframework/ios-arm64-simulator/ExecutorchLib.framework/ExecutorchLib +0 -0
  211. package/third-party/ios/ExecutorchLib.xcframework/ios-arm64-simulator/ExecutorchLib.framework/Info.plist +0 -0
  212. package/third-party/ios/libs/cpuinfo/libcpuinfo.a +0 -0
  213. package/third-party/ios/libs/pthreadpool/physical-arm64-release/libpthreadpool.a +0 -0
  214. package/third-party/ios/libs/pthreadpool/simulator-arm64-debug/libpthreadpool.a +0 -0
  215. package/ios/libs/executorch/libbackend_coreml_ios.a +0 -0
  216. package/ios/libs/executorch/libbackend_coreml_simulator.a +0 -0
  217. package/ios/libs/executorch/libbackend_mps_ios.a +0 -0
  218. package/ios/libs/executorch/libbackend_mps_simulator.a +0 -0
  219. package/ios/libs/executorch/libbackend_xnnpack_ios.a +0 -0
  220. package/ios/libs/executorch/libbackend_xnnpack_simulator.a +0 -0
  221. package/ios/libs/executorch/libexecutorch_ios.a +0 -0
  222. package/ios/libs/executorch/libexecutorch_simulator.a +0 -0
  223. package/ios/libs/executorch/libkernels_custom_ios.a +0 -0
  224. package/ios/libs/executorch/libkernels_custom_simulator.a +0 -0
  225. package/ios/libs/executorch/libkernels_optimized_ios.a +0 -0
  226. package/ios/libs/executorch/libkernels_optimized_simulator.a +0 -0
  227. package/ios/libs/executorch/libkernels_portable_ios.a +0 -0
  228. package/ios/libs/executorch/libkernels_portable_simulator.a +0 -0
  229. package/ios/libs/executorch/libkernels_quantized_ios.a +0 -0
  230. package/ios/libs/executorch/libkernels_quantized_simulator.a +0 -0
  231. package/third-party/ios/ios.toolchain.cmake +0 -1122
  232. /package/{ios → third-party/ios}/libs/tokenizers-cpp/physical-arm64-release/libsentencepiece.a +0 -0
  233. /package/{ios → third-party/ios}/libs/tokenizers-cpp/physical-arm64-release/libtokenizers_c.a +0 -0
  234. /package/{ios → third-party/ios}/libs/tokenizers-cpp/physical-arm64-release/libtokenizers_cpp.a +0 -0
  235. /package/{ios → third-party/ios}/libs/tokenizers-cpp/simulator-arm64-debug/libsentencepiece.a +0 -0
  236. /package/{ios → third-party/ios}/libs/tokenizers-cpp/simulator-arm64-debug/libtokenizers_c.a +0 -0
  237. /package/{ios → third-party/ios}/libs/tokenizers-cpp/simulator-arm64-debug/libtokenizers_cpp.a +0 -0
Binary file
@@ -1,11 +1,12 @@
1
1
  cmake_minimum_required(VERSION 3.13)
2
2
 
3
3
  file(GLOB_RECURSE ANDROID_CPP_SOURCES CONFIGURE_DEPENDS "${ANDROID_CPP_DIR}/*.cpp")
4
- file(GLOB_RECURSE COMMON_CPP_SOURCES CONFIGURE_DEPENDS "${COMMON_CPP_DIR}/*.cpp" "${COMMON_CPP_DIR}/*.c")
4
+ file(GLOB_RECURSE COMMON_CPP_SOURCES CONFIGURE_DEPENDS "${COMMON_CPP_DIR}/*.cpp")
5
+ file(GLOB_RECURSE COMMON_C_SOURCES CONFIGURE_DEPENDS "${COMMON_CPP_DIR}/*.c")
5
6
  file(GLOB_RECURSE TEST_CPP_SOURCES "${COMMON_CPP_DIR}/rnexecutorch/tests/*.cpp")
6
7
  list(REMOVE_ITEM COMMON_CPP_SOURCES ${TEST_CPP_SOURCES})
7
8
 
8
- add_library(react-native-executorch SHARED ${ANDROID_CPP_SOURCES} ${COMMON_CPP_SOURCES})
9
+ add_library(react-native-executorch SHARED ${ANDROID_CPP_SOURCES} ${COMMON_CPP_SOURCES} ${COMMON_C_SOURCES})
9
10
 
10
11
  find_package(ReactAndroid REQUIRED CONFIG)
11
12
  find_package(fbjni REQUIRED CONFIG)
@@ -41,23 +42,21 @@ add_library(executorch SHARED IMPORTED)
41
42
  set_target_properties(executorch PROPERTIES
42
43
  IMPORTED_LOCATION "${LIBS_DIR}/executorch/${ANDROID_ABI}/libexecutorch.so")
43
44
 
44
-
45
+
45
46
  if(ANDROID_ABI STREQUAL "arm64-v8a")
46
47
  target_compile_definitions(react-native-executorch PRIVATE ARCH_ARM64)
47
48
 
48
49
  # ------- pthreadpool -------
49
50
  add_library(pthreadpool SHARED IMPORTED)
50
-
51
+
51
52
  set_target_properties(pthreadpool PROPERTIES
52
- IMPORTED_LOCATION "${LIBS_DIR}/pthreadpool/${ANDROID_ABI}/libpthreadpool.so"
53
- INTERFACE_INCLUDE_DIRECTORIES "${LIBS_DIR}/../../include/pthreadpool/")
54
-
53
+ IMPORTED_LOCATION "${LIBS_DIR}/pthreadpool/${ANDROID_ABI}/libpthreadpool.so")
54
+
55
55
  # ------- cpuinfo -------
56
56
  add_library(cpuinfo SHARED IMPORTED)
57
-
57
+
58
58
  set_target_properties(cpuinfo PROPERTIES
59
- IMPORTED_LOCATION "${LIBS_DIR}/cpuinfo/${ANDROID_ABI}/libcpuinfo.so"
60
- INTERFACE_INCLUDE_DIRECTORIES "${LIBS_DIR}/../../include/cpuinfo/")
59
+ IMPORTED_LOCATION "${LIBS_DIR}/cpuinfo/${ANDROID_ABI}/libcpuinfo.so")
61
60
  set(EXECUTORCH_LIBS
62
61
  "pthreadpool"
63
62
  "cpuinfo"
@@ -66,7 +65,7 @@ endif()
66
65
 
67
66
  # ------- OpenCV -------
68
67
 
69
- set(OPENCV_LIBS
68
+ set(OPENCV_LIBS
70
69
  "${LIBS_DIR}/opencv/${ANDROID_ABI}/libopencv_core.a"
71
70
  "${LIBS_DIR}/opencv/${ANDROID_ABI}/libopencv_features2d.a"
72
71
  "${LIBS_DIR}/opencv/${ANDROID_ABI}/libopencv_highgui.a"
@@ -85,15 +84,25 @@ elseif(ANDROID_ABI STREQUAL "x86_64")
85
84
  set(OPENCV_THIRD_PARTY_LIBS "")
86
85
  endif()
87
86
 
87
+ # ------- tokenizers-cpp -------
88
+
89
+ set(TOKENIZERS_LIBS
90
+ "${LIBS_DIR}/tokenizers-cpp/${ANDROID_ABI}/libtokenizers_c.a"
91
+ "${LIBS_DIR}/tokenizers-cpp/${ANDROID_ABI}/libtokenizers_cpp.a"
92
+ "${LIBS_DIR}/tokenizers-cpp/${ANDROID_ABI}/libsentencepiece.a"
93
+ )
88
94
  # --------------
89
95
 
96
+ target_link_options(react-native-executorch PRIVATE -fopenmp -static-openmp)
97
+
90
98
  target_link_libraries(
91
- react-native-executorch
92
- ${LINK_LIBRARIES}
99
+ react-native-executorch
100
+ ${LINK_LIBRARIES}
93
101
  ${RN_VERSION_LINK_LIBRARIES}
94
102
  ${OPENCV_LIBS}
95
103
  ${OPENCV_THIRD_PARTY_LIBS}
104
+ ${TOKENIZERS_LIBS}
96
105
  executorch
97
106
  ${EXECUTORCH_LIBS}
98
107
  z
99
- )
108
+ )
@@ -12,12 +12,8 @@
12
12
  #include <rnexecutorch/models/speech_to_text/SpeechToText.h>
13
13
  #include <rnexecutorch/models/style_transfer/StyleTransfer.h>
14
14
  #include <rnexecutorch/models/vertical_ocr/VerticalOCR.h>
15
-
16
- #if defined(__ANDROID__) && defined(__aarch64__)
17
- #include <executorch/extension/threadpool/cpuinfo_utils.h>
18
- #include <executorch/extension/threadpool/threadpool.h>
19
- #include <rnexecutorch/Log.h>
20
- #endif
15
+ #include <rnexecutorch/threads/GlobalThreadPool.h>
16
+ #include <rnexecutorch/threads/utils/ThreadUtils.h>
21
17
 
22
18
  namespace rnexecutorch {
23
19
 
@@ -92,21 +88,8 @@ void RnExecutorchInstaller::injectJSIBindings(
92
88
  RnExecutorchInstaller::loadModel<models::speech_to_text::SpeechToText>(
93
89
  jsiRuntime, jsCallInvoker, "loadSpeechToText"));
94
90
 
95
- #if defined(__ANDROID__) && defined(__aarch64__)
96
- auto num_of_perf_cores =
97
- ::executorch::extension::cpuinfo::get_num_performant_cores();
98
- log(LOG_LEVEL::Info, "Detected ", num_of_perf_cores, " performant cores");
99
- // setting num_of_cores to floor(num_of_perf_cores / 2) + 1) because depending
100
- // on cpu arch as when possible we want to leave at least 2 performant cores
101
- // for other tasks (setting more actually results in drop of performance). For
102
- // older devices (i.e. samsung s22) resolves to 3 cores, and for newer ones
103
- // (like OnePlus 12) resolves to 4, which when benchamrked gives highest
104
- // throughput.
105
- auto num_of_cores = static_cast<uint32_t>(num_of_perf_cores / 2) + 1;
106
- ::executorch::extension::threadpool::get_threadpool()
107
- ->_unsafe_reset_threadpool(num_of_cores);
108
- log(LOG_LEVEL::Info, "Configuring xnnpack for ", num_of_cores, " threads");
109
- #endif
91
+ threads::utils::unsafeSetupThreadPool();
92
+ threads::GlobalThreadPool::initialize();
110
93
  }
111
94
 
112
95
  } // namespace rnexecutorch
@@ -19,6 +19,7 @@
19
19
  #include <rnexecutorch/models/llm/LLM.h>
20
20
  #include <rnexecutorch/models/ocr/OCR.h>
21
21
  #include <rnexecutorch/models/vertical_ocr/VerticalOCR.h>
22
+ #include <rnexecutorch/threads/GlobalThreadPool.h>
22
23
 
23
24
  namespace rnexecutorch {
24
25
 
@@ -114,6 +115,19 @@ public:
114
115
  ModelHostObject<Model>, synchronousHostFunction<&Model::interrupt>,
115
116
  "interrupt"));
116
117
 
118
+ addFunctions(JSI_EXPORT_FUNCTION(
119
+ ModelHostObject<Model>,
120
+ synchronousHostFunction<&Model::getGeneratedTokenCount>,
121
+ "getGeneratedTokenCount"));
122
+
123
+ addFunctions(
124
+ JSI_EXPORT_FUNCTION(ModelHostObject<Model>,
125
+ synchronousHostFunction<&Model::setCountInterval>,
126
+ "setCountInterval"));
127
+
128
+ addFunctions(JSI_EXPORT_FUNCTION(
129
+ ModelHostObject<Model>,
130
+ synchronousHostFunction<&Model::setTimeInterval>, "setTimeInterval"));
117
131
  addFunctions(
118
132
  JSI_EXPORT_FUNCTION(ModelHostObject<Model>, unload, "unload"));
119
133
  }
@@ -195,58 +209,60 @@ public:
195
209
  // We need to dispatch a thread if we want the function to be
196
210
  // asynchronous. In this thread all accesses to jsi::Runtime need to
197
211
  // be done via the callInvoker.
198
- std::thread([this, promise,
199
- argsConverted = std::move(argsConverted)]() {
200
- try {
201
- if constexpr (std::is_void_v<decltype(std::apply(
202
- std::bind_front(FnPtr, model),
203
- argsConverted))>) {
204
- // For void functions, just call the function and resolve with
205
- // undefined
206
- std::apply(std::bind_front(FnPtr, model),
207
- std::move(argsConverted));
208
- callInvoker->invokeAsync([promise](jsi::Runtime &runtime) {
209
- promise->resolve(jsi::Value::undefined());
210
- });
211
- } else {
212
- // For non-void functions, capture the result and convert it
213
- auto result = std::apply(std::bind_front(FnPtr, model),
214
- std::move(argsConverted));
215
- // The result is copied. It should either be quickly copiable,
216
- // or passed with a shared_ptr.
217
- callInvoker->invokeAsync(
218
- [promise, result](jsi::Runtime &runtime) {
219
- promise->resolve(jsi_conversion::getJsiValue(
220
- std::move(result), runtime));
221
- });
222
- }
223
- } catch (const std::runtime_error &e) {
224
- // This catch should be merged with the next two
225
- // (std::runtime_error and jsi::JSError inherits from
226
- // std::exception) HOWEVER react native has broken RTTI which
227
- // breaks proper exception type checking. Remove when the
228
- // following change is present in our version:
229
- // https://github.com/facebook/react-native/commit/3132cc88dd46f95898a756456bebeeb6c248f20e
230
- callInvoker->invokeAsync([e = std::move(e), promise]() {
231
- promise->reject(e.what());
232
- });
233
- return;
234
- } catch (const jsi::JSError &e) {
235
- callInvoker->invokeAsync([e = std::move(e), promise]() {
236
- promise->reject(e.what());
237
- });
238
- return;
239
- } catch (const std::exception &e) {
240
- callInvoker->invokeAsync([e = std::move(e), promise]() {
241
- promise->reject(e.what());
212
+ threads::GlobalThreadPool::detach(
213
+ [this, promise, argsConverted = std::move(argsConverted)]() {
214
+ try {
215
+ if constexpr (std::is_void_v<decltype(std::apply(
216
+ std::bind_front(FnPtr, model),
217
+ argsConverted))>) {
218
+ // For void functions, just call the function and resolve
219
+ // with undefined
220
+ std::apply(std::bind_front(FnPtr, model),
221
+ std::move(argsConverted));
222
+ callInvoker->invokeAsync(
223
+ [promise](jsi::Runtime &runtime) {
224
+ promise->resolve(jsi::Value::undefined());
225
+ });
226
+ } else {
227
+ // For non-void functions, capture the result and convert
228
+ // it
229
+ auto result = std::apply(std::bind_front(FnPtr, model),
230
+ std::move(argsConverted));
231
+ // The result is copied. It should either be quickly
232
+ // copiable, or passed with a shared_ptr.
233
+ callInvoker->invokeAsync(
234
+ [promise, result](jsi::Runtime &runtime) {
235
+ promise->resolve(jsi_conversion::getJsiValue(
236
+ std::move(result), runtime));
237
+ });
238
+ }
239
+ } catch (const std::runtime_error &e) {
240
+ // This catch should be merged with the next two
241
+ // (std::runtime_error and jsi::JSError inherits from
242
+ // std::exception) HOWEVER react native has broken RTTI
243
+ // which breaks proper exception type checking. Remove when
244
+ // the following change is present in our version:
245
+ // https://github.com/facebook/react-native/commit/3132cc88dd46f95898a756456bebeeb6c248f20e
246
+ callInvoker->invokeAsync([e = std::move(e), promise]() {
247
+ promise->reject(e.what());
248
+ });
249
+ return;
250
+ } catch (const jsi::JSError &e) {
251
+ callInvoker->invokeAsync([e = std::move(e), promise]() {
252
+ promise->reject(e.what());
253
+ });
254
+ return;
255
+ } catch (const std::exception &e) {
256
+ callInvoker->invokeAsync([e = std::move(e), promise]() {
257
+ promise->reject(e.what());
258
+ });
259
+ return;
260
+ } catch (...) {
261
+ callInvoker->invokeAsync(
262
+ [promise]() { promise->reject("Unknown error"); });
263
+ return;
264
+ }
242
265
  });
243
- return;
244
- } catch (...) {
245
- callInvoker->invokeAsync(
246
- [promise]() { promise->reject("Unknown error"); });
247
- return;
248
- }
249
- }).detach();
250
266
  } catch (...) {
251
267
  promise->reject("Couldn't parse JS arguments in a native function");
252
268
  }
@@ -1,7 +1,9 @@
1
1
  #include "LLM.h"
2
2
 
3
+ #include <atomic>
3
4
  #include <executorch/extension/tensor/tensor.h>
4
5
  #include <filesystem>
6
+ #include <rnexecutorch/threads/GlobalThreadPool.h>
5
7
 
6
8
  namespace rnexecutorch::models::llm {
7
9
  using namespace facebook;
@@ -49,10 +51,31 @@ void LLM::interrupt() {
49
51
  runner->stop();
50
52
  }
51
53
 
52
- std::size_t LLM::getMemoryLowerBound() const noexcept {
54
+ size_t LLM::getGeneratedTokenCount() const noexcept {
55
+ if (!runner || !runner->is_loaded()) {
56
+ return 0;
57
+ }
58
+ return runner->stats_.num_generated_tokens;
59
+ }
60
+
61
+ size_t LLM::getMemoryLowerBound() const noexcept {
53
62
  return memorySizeLowerBound;
54
63
  }
55
64
 
65
+ void LLM::setCountInterval(size_t countInterval) {
66
+ if (!runner || !runner->is_loaded()) {
67
+ throw std::runtime_error("Can't configure a model that's not loaded!");
68
+ }
69
+ runner->set_count_interval(countInterval);
70
+ }
71
+
72
+ void LLM::setTimeInterval(size_t timeInterval) {
73
+ if (!runner || !runner->is_loaded()) {
74
+ throw std::runtime_error("Can't configure a model that's not loaded!");
75
+ }
76
+ runner->set_time_interval(timeInterval);
77
+ }
78
+
56
79
  void LLM::unload() noexcept { runner.reset(nullptr); }
57
80
 
58
81
  } // namespace rnexecutorch::models::llm
@@ -21,7 +21,10 @@ public:
21
21
  void generate(std::string input, std::shared_ptr<jsi::Function> callback);
22
22
  void interrupt();
23
23
  void unload() noexcept;
24
- std::size_t getMemoryLowerBound() const noexcept;
24
+ size_t getGeneratedTokenCount() const noexcept;
25
+ size_t getMemoryLowerBound() const noexcept;
26
+ void setCountInterval(size_t countInterval);
27
+ void setTimeInterval(size_t timeInterval);
25
28
 
26
29
  private:
27
30
  size_t memorySizeLowerBound;
@@ -0,0 +1,79 @@
1
+ // GlobalThreadPool.h
2
+ #pragma once
3
+
4
+ #include <executorch/extension/threadpool/cpuinfo_utils.h>
5
+ #include <memory>
6
+ #include <mutex>
7
+ #include <optional>
8
+ #include <rnexecutorch/Log.h>
9
+ #include <rnexecutorch/threads/HighPerformanceThreadPool.h>
10
+
11
+ namespace rnexecutorch::threads {
12
+
13
+ class GlobalThreadPool {
14
+ public:
15
+ GlobalThreadPool() = delete;
16
+ GlobalThreadPool(const GlobalThreadPool &) = delete;
17
+ GlobalThreadPool &operator=(const GlobalThreadPool &) = delete;
18
+ GlobalThreadPool(GlobalThreadPool &&) = delete;
19
+ GlobalThreadPool &operator=(GlobalThreadPool &&) = delete;
20
+
21
+ static HighPerformanceThreadPool &get() {
22
+ if (!instance) {
23
+ initialize();
24
+ }
25
+ return *instance;
26
+ }
27
+
28
+ static void initialize(std::optional<uint32_t> numThreads = std::nullopt,
29
+ ThreadConfig config = {}) {
30
+ std::call_once(initFlag, [&numThreads, config]() {
31
+ if (!numThreads) {
32
+ numThreads =
33
+ ::executorch::extension::cpuinfo::get_num_performant_cores();
34
+ }
35
+
36
+ log(rnexecutorch::LOG_LEVEL::Info, "Initializing global thread pool with",
37
+ numThreads, "threads");
38
+ instance = std::make_unique<HighPerformanceThreadPool>(numThreads.value(),
39
+ config);
40
+ });
41
+ }
42
+
43
+ // Convenience methods that mirror std::thread interface
44
+ template <typename Func, typename... Args>
45
+ static auto async(Func &&func, Args &&...args) {
46
+ return get().submit(std::forward<Func>(func), std::forward<Args>(args)...);
47
+ }
48
+
49
+ template <typename Func, typename... Args>
50
+ static auto async_high_priority(Func &&func, Args &&...args) {
51
+ return get().submitWithPriority(Priority::HIGH, std::forward<Func>(func),
52
+ std::forward<Args>(args)...);
53
+ }
54
+
55
+ // Fire and forget (like std::thread{}.detach())
56
+ template <typename Func, typename... Args>
57
+ static void detach(Func &&func, Args &&...args) {
58
+ get().submitDetached(std::forward<Func>(func), std::forward<Args>(args)...);
59
+ }
60
+
61
+ // Execute and wait (like std::thread{}.join())
62
+ template <typename Func, typename... Args>
63
+ static auto execute(Func &&func, Args &&...args) {
64
+ return get().execute(std::forward<Func>(func), std::forward<Args>(args)...);
65
+ }
66
+
67
+ static void shutdown() {
68
+ if (instance) {
69
+ instance->shutdown();
70
+ instance.reset();
71
+ }
72
+ }
73
+
74
+ private:
75
+ inline static std::unique_ptr<HighPerformanceThreadPool> instance;
76
+ inline static std::once_flag initFlag;
77
+ };
78
+
79
+ } // namespace rnexecutorch::threads