react-native-executorch 0.5.5 → 0.5.7
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.
- package/android/libs/classes.jar +0 -0
- package/android/src/main/cpp/CMakeLists.txt +23 -14
- package/common/rnexecutorch/RnExecutorchInstaller.cpp +4 -21
- package/common/rnexecutorch/host_objects/ModelHostObject.h +67 -51
- package/common/rnexecutorch/models/llm/LLM.cpp +24 -1
- package/common/rnexecutorch/models/llm/LLM.h +4 -1
- package/common/rnexecutorch/models/speech_to_text/SpeechToText.cpp +2 -5
- package/common/rnexecutorch/models/speech_to_text/SpeechToText.h +1 -1
- package/common/rnexecutorch/threads/GlobalThreadPool.h +79 -0
- package/common/rnexecutorch/threads/HighPerformanceThreadPool.h +364 -0
- package/common/rnexecutorch/threads/utils/ThreadUtils.h +29 -0
- package/common/runner/runner.cpp +9 -3
- package/common/runner/runner.h +4 -3
- package/common/runner/text_token_generator.h +28 -10
- package/lib/module/controllers/LLMController.js +21 -2
- package/lib/module/controllers/LLMController.js.map +1 -1
- package/lib/module/hooks/natural_language_processing/useLLM.js +6 -2
- package/lib/module/hooks/natural_language_processing/useLLM.js.map +1 -1
- package/lib/module/modules/natural_language_processing/LLMModule.js +4 -2
- package/lib/module/modules/natural_language_processing/LLMModule.js.map +1 -1
- package/lib/module/types/llm.js.map +1 -1
- package/lib/typescript/controllers/LLMController.d.ts +4 -2
- package/lib/typescript/controllers/LLMController.d.ts.map +1 -1
- package/lib/typescript/hooks/natural_language_processing/useLLM.d.ts.map +1 -1
- package/lib/typescript/modules/natural_language_processing/LLMModule.d.ts +3 -2
- package/lib/typescript/modules/natural_language_processing/LLMModule.d.ts.map +1 -1
- package/lib/typescript/types/llm.d.ts +7 -1
- package/lib/typescript/types/llm.d.ts.map +1 -1
- package/package.json +3 -1
- package/react-native-executorch.podspec +12 -31
- package/src/controllers/LLMController.ts +29 -5
- package/src/hooks/natural_language_processing/useLLM.ts +15 -1
- package/src/modules/natural_language_processing/LLMModule.ts +10 -2
- package/src/types/llm.ts +8 -0
- package/third-party/android/libs/cpuinfo/arm64-v8a/libcpuinfo.so +0 -0
- package/third-party/android/libs/executorch/arm64-v8a/libexecutorch.so +0 -0
- package/third-party/android/libs/executorch/x86_64/libexecutorch.so +0 -0
- package/third-party/android/libs/pthreadpool/arm64-v8a/libpthreadpool.so +0 -0
- package/third-party/android/libs/tokenizers-cpp/arm64-v8a/libsentencepiece.a +0 -0
- package/third-party/android/libs/tokenizers-cpp/arm64-v8a/libtokenizers_c.a +0 -0
- package/third-party/android/libs/tokenizers-cpp/arm64-v8a/libtokenizers_cpp.a +0 -0
- package/third-party/android/libs/tokenizers-cpp/x86_64/libsentencepiece.a +0 -0
- package/third-party/android/libs/tokenizers-cpp/x86_64/libtokenizers_c.a +0 -0
- package/third-party/android/libs/tokenizers-cpp/x86_64/libtokenizers_cpp.a +0 -0
- package/third-party/include/c10/macros/Export.h +2 -86
- package/third-party/include/c10/macros/Macros.h +28 -5
- package/third-party/include/c10/util/BFloat16-inl.h +1 -4
- package/third-party/include/c10/util/BFloat16.h +5 -8
- package/third-party/include/c10/util/Half.h +5 -0
- package/third-party/include/c10/util/bit_cast.h +1 -1
- package/third-party/include/c10/util/complex.h +639 -0
- package/third-party/include/c10/util/complex_math.h +399 -0
- package/third-party/include/c10/util/complex_utils.h +41 -0
- package/third-party/include/c10/util/irange.h +2 -2
- package/third-party/include/c10/util/overflows.h +95 -0
- package/third-party/include/executorch/ExecuTorchError.h +75 -0
- package/third-party/include/executorch/ExecuTorchModule.h +115 -11
- package/third-party/include/executorch/ExecuTorchTensor.h +731 -51
- package/third-party/include/executorch/ExecuTorchValue.h +61 -9
- package/third-party/include/executorch/extension/kernel_util/make_boxed_from_unboxed_functor.h +181 -0
- package/third-party/include/executorch/extension/kernel_util/meta_programming.h +108 -0
- package/third-party/include/executorch/extension/kernel_util/type_list.h +137 -0
- package/third-party/include/executorch/extension/module/bundled_module.h +131 -0
- package/third-party/include/executorch/extension/module/module.h +46 -20
- package/third-party/include/executorch/extension/threadpool/cpuinfo_utils.h +1 -3
- package/third-party/include/executorch/extension/threadpool/threadpool.h +1 -3
- package/third-party/include/executorch/extension/threadpool/threadpool_guard.h +35 -0
- package/third-party/include/executorch/runtime/backend/backend_execution_context.h +3 -3
- package/third-party/include/executorch/runtime/backend/backend_init_context.h +12 -6
- package/third-party/include/executorch/runtime/backend/backend_option_context.h +34 -0
- package/third-party/include/executorch/runtime/backend/interface.h +70 -9
- package/third-party/include/executorch/runtime/backend/options.h +206 -0
- package/third-party/include/executorch/runtime/core/evalue.h +19 -25
- package/third-party/include/executorch/runtime/core/event_tracer.h +32 -17
- package/third-party/include/executorch/runtime/core/event_tracer_hooks.h +23 -14
- package/third-party/include/executorch/runtime/core/exec_aten/exec_aten.h +32 -9
- package/third-party/include/executorch/runtime/core/exec_aten/util/dim_order_util.h +3 -2
- package/third-party/include/executorch/runtime/core/exec_aten/util/scalar_type_util.h +43 -75
- package/third-party/include/executorch/runtime/core/exec_aten/util/tensor_util.h +88 -87
- package/third-party/include/executorch/runtime/core/function_ref.h +100 -0
- package/third-party/include/executorch/runtime/core/named_data_map.h +14 -14
- package/third-party/include/executorch/runtime/core/portable_type/c10/c10/macros/Export.h +2 -86
- package/third-party/include/executorch/runtime/core/portable_type/c10/c10/macros/Macros.h +28 -5
- package/third-party/include/executorch/runtime/core/portable_type/c10/c10/util/BFloat16-inl.h +1 -4
- package/third-party/include/executorch/runtime/core/portable_type/c10/c10/util/BFloat16.h +5 -8
- package/third-party/include/executorch/runtime/core/portable_type/c10/c10/util/Half.h +5 -0
- package/third-party/include/executorch/runtime/core/portable_type/c10/c10/util/bit_cast.h +1 -1
- package/third-party/include/executorch/runtime/core/portable_type/c10/c10/util/complex.h +639 -0
- package/third-party/include/executorch/runtime/core/portable_type/c10/c10/util/complex_math.h +399 -0
- package/third-party/include/executorch/runtime/core/portable_type/c10/c10/util/complex_utils.h +41 -0
- package/third-party/include/executorch/runtime/core/portable_type/c10/c10/util/irange.h +2 -2
- package/third-party/include/executorch/runtime/core/portable_type/c10/c10/util/overflows.h +95 -0
- package/third-party/include/executorch/runtime/core/portable_type/c10/torch/headeronly/macros/Export.h +88 -0
- package/third-party/include/executorch/runtime/core/portable_type/complex.h +6 -29
- package/third-party/include/executorch/runtime/core/portable_type/tensor_impl.h +20 -0
- package/third-party/include/executorch/runtime/core/span.h +4 -0
- package/third-party/include/executorch/runtime/core/tag.h +19 -0
- package/third-party/include/executorch/runtime/core/tensor_layout.h +2 -2
- package/third-party/include/executorch/runtime/executor/method.h +15 -3
- package/third-party/include/executorch/runtime/executor/method_meta.h +34 -5
- package/third-party/include/executorch/runtime/executor/program.h +3 -4
- package/third-party/include/executorch/runtime/executor/pte_data_map.h +9 -8
- package/third-party/include/executorch/runtime/executor/tensor_parser.h +14 -13
- package/third-party/include/executorch/runtime/kernel/kernel_runtime_context.h +5 -5
- package/third-party/include/executorch/runtime/kernel/operator_registry.h +21 -19
- package/third-party/include/executorch/runtime/platform/compiler.h +8 -0
- package/third-party/include/executorch/runtime/platform/platform.h +126 -0
- package/third-party/include/headeronly/macros/Export.h +88 -0
- package/third-party/include/tokenizers-cpp/tokenizers_c.h +61 -0
- package/third-party/include/torch/headeronly/macros/Export.h +88 -0
- package/third-party/ios/ExecutorchLib.xcframework/Info.plist +43 -0
- package/third-party/ios/ExecutorchLib.xcframework/ios-arm64/ExecutorchLib.framework/ExecutorchLib +0 -0
- package/third-party/ios/ExecutorchLib.xcframework/ios-arm64/ExecutorchLib.framework/Info.plist +0 -0
- package/third-party/ios/ExecutorchLib.xcframework/ios-arm64-simulator/ExecutorchLib.framework/ExecutorchLib +0 -0
- package/third-party/ios/ExecutorchLib.xcframework/ios-arm64-simulator/ExecutorchLib.framework/Info.plist +0 -0
- package/third-party/ios/libs/cpuinfo/libcpuinfo.a +0 -0
- package/third-party/ios/libs/pthreadpool/physical-arm64-release/libpthreadpool.a +0 -0
- package/third-party/ios/libs/pthreadpool/simulator-arm64-debug/libpthreadpool.a +0 -0
- package/ios/libs/executorch/libbackend_coreml_ios.a +0 -0
- package/ios/libs/executorch/libbackend_coreml_simulator.a +0 -0
- package/ios/libs/executorch/libbackend_mps_ios.a +0 -0
- package/ios/libs/executorch/libbackend_mps_simulator.a +0 -0
- package/ios/libs/executorch/libbackend_xnnpack_ios.a +0 -0
- package/ios/libs/executorch/libbackend_xnnpack_simulator.a +0 -0
- package/ios/libs/executorch/libexecutorch_ios.a +0 -0
- package/ios/libs/executorch/libexecutorch_simulator.a +0 -0
- package/ios/libs/executorch/libkernels_custom_ios.a +0 -0
- package/ios/libs/executorch/libkernels_custom_simulator.a +0 -0
- package/ios/libs/executorch/libkernels_optimized_ios.a +0 -0
- package/ios/libs/executorch/libkernels_optimized_simulator.a +0 -0
- package/ios/libs/executorch/libkernels_portable_ios.a +0 -0
- package/ios/libs/executorch/libkernels_portable_simulator.a +0 -0
- package/ios/libs/executorch/libkernels_quantized_ios.a +0 -0
- package/ios/libs/executorch/libkernels_quantized_simulator.a +0 -0
- package/third-party/ios/ios.toolchain.cmake +0 -1122
- /package/{ios → third-party/ios}/libs/tokenizers-cpp/physical-arm64-release/libsentencepiece.a +0 -0
- /package/{ios → third-party/ios}/libs/tokenizers-cpp/physical-arm64-release/libtokenizers_c.a +0 -0
- /package/{ios → third-party/ios}/libs/tokenizers-cpp/physical-arm64-release/libtokenizers_cpp.a +0 -0
- /package/{ios → third-party/ios}/libs/tokenizers-cpp/simulator-arm64-debug/libsentencepiece.a +0 -0
- /package/{ios → third-party/ios}/libs/tokenizers-cpp/simulator-arm64-debug/libtokenizers_c.a +0 -0
- /package/{ios → third-party/ios}/libs/tokenizers-cpp/simulator-arm64-debug/libtokenizers_cpp.a +0 -0
package/android/libs/classes.jar
CHANGED
|
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"
|
|
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
|
-
|
|
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
|
-
#
|
|
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
|
-
|
|
96
|
-
|
|
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
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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;
|
|
@@ -90,8 +90,6 @@ void SpeechToText::stream(std::shared_ptr<jsi::Function> callback,
|
|
|
90
90
|
});
|
|
91
91
|
};
|
|
92
92
|
|
|
93
|
-
this->resetStreamState();
|
|
94
|
-
|
|
95
93
|
this->isStreaming = true;
|
|
96
94
|
while (this->isStreaming) {
|
|
97
95
|
if (!this->readyToProcess ||
|
|
@@ -107,14 +105,13 @@ void SpeechToText::stream(std::shared_ptr<jsi::Function> callback,
|
|
|
107
105
|
|
|
108
106
|
std::string committed = this->processor->finish();
|
|
109
107
|
nativeCallback(committed, "", true);
|
|
108
|
+
|
|
109
|
+
this->resetStreamState();
|
|
110
110
|
}
|
|
111
111
|
|
|
112
112
|
void SpeechToText::streamStop() { this->isStreaming = false; }
|
|
113
113
|
|
|
114
114
|
void SpeechToText::streamInsert(std::span<float> waveform) {
|
|
115
|
-
if (!this->isStreaming) {
|
|
116
|
-
throw std::runtime_error("Streaming is not started");
|
|
117
|
-
}
|
|
118
115
|
this->processor->insertAudioChunk(waveform);
|
|
119
116
|
this->readyToProcess = true;
|
|
120
117
|
}
|
|
@@ -28,6 +28,7 @@ public:
|
|
|
28
28
|
void streamInsert(std::span<float> waveform);
|
|
29
29
|
|
|
30
30
|
private:
|
|
31
|
+
std::shared_ptr<react::CallInvoker> callInvoker;
|
|
31
32
|
std::unique_ptr<BaseModel> encoder;
|
|
32
33
|
std::unique_ptr<BaseModel> decoder;
|
|
33
34
|
std::unique_ptr<TokenizerModule> tokenizer;
|
|
@@ -37,7 +38,6 @@ private:
|
|
|
37
38
|
makeOwningBuffer(std::span<const float> vectorView) const;
|
|
38
39
|
|
|
39
40
|
// Stream
|
|
40
|
-
std::shared_ptr<react::CallInvoker> callInvoker;
|
|
41
41
|
std::unique_ptr<stream::OnlineASRProcessor> processor;
|
|
42
42
|
bool isStreaming;
|
|
43
43
|
bool readyToProcess;
|
|
@@ -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
|