react-native-executorch 0.5.15 → 0.6.0

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 (277) hide show
  1. package/README.md +42 -36
  2. package/android/CMakeLists.txt +13 -25
  3. package/android/build.gradle +2 -3
  4. package/android/libs/classes.jar +0 -0
  5. package/android/src/main/cpp/CMakeLists.txt +2 -1
  6. package/common/rnexecutorch/RnExecutorchInstaller.cpp +18 -0
  7. package/common/rnexecutorch/TokenizerModule.cpp +3 -3
  8. package/common/rnexecutorch/data_processing/Numerical.cpp +31 -23
  9. package/common/rnexecutorch/data_processing/Numerical.h +6 -1
  10. package/common/rnexecutorch/data_processing/dsp.cpp +0 -46
  11. package/common/rnexecutorch/host_objects/JsiConversions.h +16 -0
  12. package/common/rnexecutorch/host_objects/ModelHostObject.h +26 -11
  13. package/common/rnexecutorch/jsi/OwningArrayBuffer.h +19 -2
  14. package/common/rnexecutorch/metaprogramming/TypeConcepts.h +0 -20
  15. package/common/rnexecutorch/models/BaseModel.cpp +12 -11
  16. package/common/rnexecutorch/models/BaseModel.h +18 -10
  17. package/common/rnexecutorch/models/embeddings/BaseEmbeddings.cpp +3 -11
  18. package/common/rnexecutorch/models/embeddings/text/TextEmbeddings.cpp +0 -1
  19. package/common/rnexecutorch/models/image_segmentation/ImageSegmentation.cpp +6 -12
  20. package/common/rnexecutorch/models/llm/LLM.cpp +25 -8
  21. package/common/rnexecutorch/models/llm/LLM.h +4 -4
  22. package/common/rnexecutorch/models/ocr/CTCLabelConverter.h +1 -1
  23. package/common/rnexecutorch/models/ocr/utils/RecognitionHandlerUtils.cpp +7 -4
  24. package/common/rnexecutorch/models/speech_to_text/SpeechToText.cpp +8 -13
  25. package/common/rnexecutorch/models/speech_to_text/SpeechToText.h +1 -3
  26. package/common/rnexecutorch/models/speech_to_text/asr/ASR.cpp +12 -19
  27. package/common/rnexecutorch/models/speech_to_text/asr/ASR.h +4 -5
  28. package/common/rnexecutorch/models/text_to_image/Constants.h +9 -0
  29. package/common/rnexecutorch/models/text_to_image/Decoder.cpp +32 -0
  30. package/common/rnexecutorch/models/text_to_image/Decoder.h +24 -0
  31. package/common/rnexecutorch/models/text_to_image/Encoder.cpp +44 -0
  32. package/common/rnexecutorch/models/text_to_image/Encoder.h +32 -0
  33. package/common/rnexecutorch/models/text_to_image/Scheduler.cpp +152 -0
  34. package/common/rnexecutorch/models/text_to_image/Scheduler.h +41 -0
  35. package/common/rnexecutorch/models/text_to_image/TextToImage.cpp +141 -0
  36. package/common/rnexecutorch/models/text_to_image/TextToImage.h +64 -0
  37. package/common/rnexecutorch/models/text_to_image/UNet.cpp +38 -0
  38. package/common/rnexecutorch/models/text_to_image/UNet.h +28 -0
  39. package/common/rnexecutorch/models/voice_activity_detection/Constants.h +27 -0
  40. package/common/rnexecutorch/models/voice_activity_detection/Types.h +12 -0
  41. package/common/rnexecutorch/models/voice_activity_detection/Utils.cpp +15 -0
  42. package/common/rnexecutorch/models/voice_activity_detection/Utils.h +13 -0
  43. package/common/rnexecutorch/models/voice_activity_detection/VoiceActivityDetection.cpp +160 -0
  44. package/common/rnexecutorch/models/voice_activity_detection/VoiceActivityDetection.h +36 -0
  45. package/common/rnexecutorch/tests/CMakeLists.txt +30 -0
  46. package/common/rnexecutorch/tests/NumericalTest.cpp +110 -0
  47. package/common/rnexecutorch/tests/README.md +30 -13
  48. package/common/rnexecutorch/threads/GlobalThreadPool.h +4 -0
  49. package/common/runner/arange_util.cpp +44 -0
  50. package/common/runner/arange_util.h +37 -0
  51. package/common/runner/constants.h +28 -0
  52. package/common/runner/io_manager.h +240 -0
  53. package/common/runner/irunner.h +87 -16
  54. package/common/runner/kernel_includes.h +23 -0
  55. package/common/runner/runner.cpp +151 -66
  56. package/common/runner/runner.h +39 -22
  57. package/common/runner/sampler.cpp +8 -1
  58. package/common/runner/sampler.h +4 -2
  59. package/common/runner/stats.h +1 -4
  60. package/common/runner/text_decoder_runner.cpp +26 -12
  61. package/common/runner/text_decoder_runner.h +52 -31
  62. package/common/runner/text_prefiller.cpp +46 -12
  63. package/common/runner/text_prefiller.h +38 -4
  64. package/common/runner/text_token_generator.h +51 -26
  65. package/common/runner/util.h +53 -8
  66. package/ios/RnExecutorch.xcodeproj/project.pbxproj +0 -23
  67. package/lib/module/Error.js +1 -0
  68. package/lib/module/Error.js.map +1 -1
  69. package/lib/module/constants/directories.js +1 -1
  70. package/lib/module/constants/directories.js.map +1 -1
  71. package/lib/module/constants/modelUrls.js +32 -1
  72. package/lib/module/constants/modelUrls.js.map +1 -1
  73. package/lib/module/constants/ocr/models.js +7 -7
  74. package/lib/module/constants/ocr/models.js.map +1 -1
  75. package/lib/module/constants/ocr/symbols.js +3 -2
  76. package/lib/module/constants/ocr/symbols.js.map +1 -1
  77. package/lib/module/controllers/LLMController.js +10 -1
  78. package/lib/module/controllers/LLMController.js.map +1 -1
  79. package/lib/module/controllers/OCRController.js +3 -3
  80. package/lib/module/controllers/OCRController.js.map +1 -1
  81. package/lib/module/controllers/VerticalOCRController.js +2 -2
  82. package/lib/module/controllers/VerticalOCRController.js.map +1 -1
  83. package/lib/module/hooks/computer_vision/useOCR.js +3 -3
  84. package/lib/module/hooks/computer_vision/useOCR.js.map +1 -1
  85. package/lib/module/hooks/{useNonStaticModule.js → computer_vision/useTextToImage.js} +21 -16
  86. package/lib/module/hooks/computer_vision/useTextToImage.js.map +1 -0
  87. package/lib/module/hooks/computer_vision/useVerticalOCR.js +3 -3
  88. package/lib/module/hooks/computer_vision/useVerticalOCR.js.map +1 -1
  89. package/lib/module/hooks/natural_language_processing/useLLM.js +3 -3
  90. package/lib/module/hooks/natural_language_processing/useLLM.js.map +1 -1
  91. package/lib/module/hooks/natural_language_processing/useTokenizer.js +5 -5
  92. package/lib/module/hooks/natural_language_processing/useTokenizer.js.map +1 -1
  93. package/lib/module/hooks/natural_language_processing/useVAD.js +13 -0
  94. package/lib/module/hooks/natural_language_processing/useVAD.js.map +1 -0
  95. package/lib/module/index.js +7 -2
  96. package/lib/module/index.js.map +1 -1
  97. package/lib/module/modules/computer_vision/OCRModule.js +2 -2
  98. package/lib/module/modules/computer_vision/OCRModule.js.map +1 -1
  99. package/lib/module/modules/computer_vision/TextToImageModule.js +48 -0
  100. package/lib/module/modules/computer_vision/TextToImageModule.js.map +1 -0
  101. package/lib/module/modules/computer_vision/VerticalOCRModule.js +2 -2
  102. package/lib/module/modules/computer_vision/VerticalOCRModule.js.map +1 -1
  103. package/lib/module/modules/natural_language_processing/SpeechToTextModule.js +7 -4
  104. package/lib/module/modules/natural_language_processing/SpeechToTextModule.js.map +1 -1
  105. package/lib/module/modules/natural_language_processing/VADModule.js +19 -0
  106. package/lib/module/modules/natural_language_processing/VADModule.js.map +1 -0
  107. package/lib/module/types/llm.js.map +1 -1
  108. package/lib/module/types/vad.js +2 -0
  109. package/lib/module/types/vad.js.map +1 -0
  110. package/lib/module/utils/ResourceFetcher.js +2 -1
  111. package/lib/module/utils/ResourceFetcher.js.map +1 -1
  112. package/lib/module/utils/ResourceFetcherUtils.js +6 -6
  113. package/lib/module/utils/ResourceFetcherUtils.js.map +1 -1
  114. package/lib/typescript/Error.d.ts +1 -0
  115. package/lib/typescript/Error.d.ts.map +1 -1
  116. package/lib/typescript/constants/modelUrls.d.ts +23 -0
  117. package/lib/typescript/constants/modelUrls.d.ts.map +1 -1
  118. package/lib/typescript/constants/ocr/symbols.d.ts +1 -1
  119. package/lib/typescript/constants/ocr/symbols.d.ts.map +1 -1
  120. package/lib/typescript/controllers/LLMController.d.ts.map +1 -1
  121. package/lib/typescript/controllers/OCRController.d.ts +1 -1
  122. package/lib/typescript/controllers/OCRController.d.ts.map +1 -1
  123. package/lib/typescript/controllers/VerticalOCRController.d.ts +1 -1
  124. package/lib/typescript/controllers/VerticalOCRController.d.ts.map +1 -1
  125. package/lib/typescript/hooks/computer_vision/useOCR.d.ts +1 -1
  126. package/lib/typescript/hooks/computer_vision/useOCR.d.ts.map +1 -1
  127. package/lib/typescript/hooks/computer_vision/useTextToImage.d.ts +22 -0
  128. package/lib/typescript/hooks/computer_vision/useTextToImage.d.ts.map +1 -0
  129. package/lib/typescript/hooks/computer_vision/useVerticalOCR.d.ts +1 -1
  130. package/lib/typescript/hooks/computer_vision/useVerticalOCR.d.ts.map +1 -1
  131. package/lib/typescript/hooks/natural_language_processing/useLLM.d.ts.map +1 -1
  132. package/lib/typescript/hooks/natural_language_processing/useSpeechToText.d.ts +2 -2
  133. package/lib/typescript/hooks/natural_language_processing/useVAD.d.ts +16 -0
  134. package/lib/typescript/hooks/natural_language_processing/useVAD.d.ts.map +1 -0
  135. package/lib/typescript/index.d.ts +8 -1
  136. package/lib/typescript/index.d.ts.map +1 -1
  137. package/lib/typescript/modules/computer_vision/OCRModule.d.ts +1 -1
  138. package/lib/typescript/modules/computer_vision/OCRModule.d.ts.map +1 -1
  139. package/lib/typescript/modules/computer_vision/TextToImageModule.d.ts +16 -0
  140. package/lib/typescript/modules/computer_vision/TextToImageModule.d.ts.map +1 -0
  141. package/lib/typescript/modules/computer_vision/VerticalOCRModule.d.ts +1 -1
  142. package/lib/typescript/modules/computer_vision/VerticalOCRModule.d.ts.map +1 -1
  143. package/lib/typescript/modules/natural_language_processing/SpeechToTextModule.d.ts +3 -2
  144. package/lib/typescript/modules/natural_language_processing/SpeechToTextModule.d.ts.map +1 -1
  145. package/lib/typescript/modules/natural_language_processing/VADModule.d.ts +10 -0
  146. package/lib/typescript/modules/natural_language_processing/VADModule.d.ts.map +1 -0
  147. package/lib/typescript/types/llm.d.ts +2 -0
  148. package/lib/typescript/types/llm.d.ts.map +1 -1
  149. package/lib/typescript/types/vad.d.ts +5 -0
  150. package/lib/typescript/types/vad.d.ts.map +1 -0
  151. package/lib/typescript/utils/ResourceFetcher.d.ts +29 -0
  152. package/lib/typescript/utils/ResourceFetcher.d.ts.map +1 -1
  153. package/lib/typescript/utils/ResourceFetcherUtils.d.ts +2 -2
  154. package/lib/typescript/utils/ResourceFetcherUtils.d.ts.map +1 -1
  155. package/package.json +11 -8
  156. package/react-native-executorch.podspec +9 -9
  157. package/src/Error.ts +1 -0
  158. package/src/constants/directories.ts +1 -1
  159. package/src/constants/modelUrls.ts +36 -1
  160. package/src/constants/ocr/models.ts +7 -7
  161. package/src/constants/ocr/symbols.ts +3 -2
  162. package/src/controllers/LLMController.ts +12 -1
  163. package/src/controllers/OCRController.ts +3 -3
  164. package/src/controllers/VerticalOCRController.ts +2 -2
  165. package/src/hooks/computer_vision/useOCR.ts +4 -5
  166. package/src/hooks/computer_vision/useTextToImage.ts +92 -0
  167. package/src/hooks/computer_vision/useVerticalOCR.ts +4 -5
  168. package/src/hooks/natural_language_processing/useLLM.ts +3 -4
  169. package/src/hooks/natural_language_processing/useTokenizer.ts +5 -5
  170. package/src/hooks/natural_language_processing/useVAD.ts +15 -0
  171. package/src/index.ts +20 -1
  172. package/src/modules/computer_vision/OCRModule.ts +2 -2
  173. package/src/modules/computer_vision/TextToImageModule.ts +93 -0
  174. package/src/modules/computer_vision/VerticalOCRModule.ts +2 -2
  175. package/src/modules/natural_language_processing/SpeechToTextModule.ts +8 -4
  176. package/src/modules/natural_language_processing/VADModule.ts +27 -0
  177. package/src/types/llm.ts +2 -0
  178. package/src/types/vad.ts +4 -0
  179. package/src/utils/ResourceFetcher.ts +2 -1
  180. package/src/utils/ResourceFetcherUtils.ts +8 -8
  181. package/third-party/android/libs/cpuinfo/arm64-v8a/libcpuinfo.so +0 -0
  182. package/third-party/android/libs/executorch/arm64-v8a/libexecutorch.so +0 -0
  183. package/third-party/android/libs/executorch/x86_64/libexecutorch.so +0 -0
  184. package/third-party/android/libs/pthreadpool/arm64-v8a/libpthreadpool.so +0 -0
  185. package/third-party/include/c10/macros/Export.h +0 -78
  186. package/third-party/include/c10/macros/Macros.h +1 -520
  187. package/third-party/include/c10/util/BFloat16-inl.h +1 -339
  188. package/third-party/include/c10/util/BFloat16.h +1 -122
  189. package/third-party/include/c10/util/Half-inl.h +1 -347
  190. package/third-party/include/c10/util/Half.h +6 -419
  191. package/third-party/include/c10/util/TypeSafeSignMath.h +1 -133
  192. package/third-party/include/c10/util/bit_cast.h +1 -43
  193. package/third-party/include/c10/util/complex.h +1 -568
  194. package/third-party/include/c10/util/floating_point_utils.h +1 -33
  195. package/third-party/include/c10/util/irange.h +1 -1
  196. package/third-party/include/c10/util/llvmMathExtras.h +866 -0
  197. package/third-party/include/c10/util/safe_numerics.h +97 -0
  198. package/third-party/include/executorch/ExecuTorchError.h +6 -7
  199. package/third-party/include/executorch/ExecuTorchLLM/ExecuTorchLLM.h +12 -0
  200. package/third-party/include/executorch/ExecuTorchLLM/ExecuTorchLLMConfig.h +56 -0
  201. package/third-party/include/executorch/ExecuTorchLLM/ExecuTorchLLMError.h +16 -0
  202. package/third-party/include/executorch/ExecuTorchLLM/ExecuTorchLLMMultimodalRunner.h +227 -0
  203. package/third-party/include/executorch/ExecuTorchLLM/ExecuTorchLLMTextRunner.h +97 -0
  204. package/third-party/include/executorch/ExecuTorchLLM/module.modulemap +4 -0
  205. package/third-party/include/executorch/ExecuTorchLog.h +1 -0
  206. package/third-party/include/executorch/ExecuTorchModule.h +177 -4
  207. package/third-party/include/executorch/ExecuTorchTensor.h +3 -4
  208. package/third-party/include/executorch/ExecuTorchValue.h +1 -7
  209. package/third-party/include/executorch/extension/module/module.h +139 -8
  210. package/third-party/include/executorch/extension/tensor/tensor.h +1 -0
  211. package/third-party/include/executorch/extension/tensor/tensor_ptr.h +88 -26
  212. package/third-party/include/executorch/extension/threadpool/threadpool.h +4 -1
  213. package/third-party/include/executorch/runtime/backend/backend_init_context.h +6 -0
  214. package/third-party/include/executorch/runtime/backend/interface.h +1 -1
  215. package/third-party/include/executorch/runtime/core/error.h +76 -49
  216. package/third-party/include/executorch/runtime/core/exec_aten/util/scalar_type_util.h +18 -4
  217. package/third-party/include/executorch/runtime/core/memory_allocator.h +12 -2
  218. package/third-party/include/executorch/runtime/core/named_data_map.h +1 -11
  219. package/third-party/include/executorch/runtime/core/portable_type/c10/c10/macros/Export.h +0 -78
  220. package/third-party/include/executorch/runtime/core/portable_type/c10/c10/macros/Macros.h +1 -520
  221. package/third-party/include/executorch/runtime/core/portable_type/c10/c10/util/BFloat16-inl.h +1 -339
  222. package/third-party/include/executorch/runtime/core/portable_type/c10/c10/util/BFloat16.h +1 -122
  223. package/third-party/include/executorch/runtime/core/portable_type/c10/c10/util/Half-inl.h +1 -347
  224. package/third-party/include/executorch/runtime/core/portable_type/c10/c10/util/Half.h +6 -419
  225. package/third-party/include/executorch/runtime/core/portable_type/c10/c10/util/TypeSafeSignMath.h +1 -133
  226. package/third-party/include/executorch/runtime/core/portable_type/c10/c10/util/bit_cast.h +1 -43
  227. package/third-party/include/executorch/runtime/core/portable_type/c10/c10/util/complex.h +1 -568
  228. package/third-party/include/executorch/runtime/core/portable_type/c10/c10/util/floating_point_utils.h +1 -33
  229. package/third-party/include/executorch/runtime/core/portable_type/c10/c10/util/irange.h +1 -1
  230. package/third-party/include/executorch/runtime/core/portable_type/c10/c10/util/llvmMathExtras.h +866 -0
  231. package/third-party/include/executorch/runtime/core/portable_type/c10/c10/util/safe_numerics.h +97 -0
  232. package/third-party/include/executorch/runtime/core/portable_type/c10/torch/headeronly/macros/Export.h +66 -0
  233. package/third-party/include/executorch/runtime/core/portable_type/c10/torch/headeronly/macros/Macros.h +553 -0
  234. package/third-party/include/executorch/runtime/core/portable_type/c10/torch/headeronly/util/BFloat16.h +477 -0
  235. package/third-party/include/executorch/runtime/core/portable_type/c10/torch/headeronly/util/Half.h +781 -0
  236. package/third-party/include/executorch/runtime/core/portable_type/c10/torch/headeronly/util/TypeSafeSignMath.h +141 -0
  237. package/third-party/include/executorch/runtime/core/portable_type/c10/torch/headeronly/util/bit_cast.h +49 -0
  238. package/third-party/include/executorch/runtime/core/portable_type/c10/torch/headeronly/util/complex.h +593 -0
  239. package/third-party/include/executorch/runtime/core/portable_type/c10/torch/headeronly/util/floating_point_utils.h +38 -0
  240. package/third-party/include/executorch/runtime/core/tensor_layout.h +1 -1
  241. package/third-party/include/executorch/runtime/executor/merged_data_map.h +142 -0
  242. package/third-party/include/executorch/runtime/executor/method.h +21 -8
  243. package/third-party/include/executorch/runtime/executor/method_meta.h +20 -2
  244. package/third-party/include/executorch/runtime/executor/program.h +0 -10
  245. package/third-party/include/executorch/runtime/kernel/operator_registry.h +1 -1
  246. package/third-party/include/executorch/runtime/platform/compiler.h +2 -0
  247. package/third-party/include/executorch/schema/extended_header.h +10 -1
  248. package/third-party/include/torch/headeronly/macros/Export.h +66 -0
  249. package/third-party/include/torch/headeronly/macros/Macros.h +553 -0
  250. package/third-party/include/torch/headeronly/util/BFloat16.h +477 -0
  251. package/third-party/include/torch/headeronly/util/Half.h +781 -0
  252. package/third-party/include/torch/headeronly/util/TypeSafeSignMath.h +141 -0
  253. package/third-party/include/torch/headeronly/util/bit_cast.h +49 -0
  254. package/third-party/include/torch/headeronly/util/complex.h +593 -0
  255. package/third-party/include/torch/headeronly/util/floating_point_utils.h +38 -0
  256. package/third-party/ios/ExecutorchLib.xcframework/ios-arm64/ExecutorchLib.framework/ExecutorchLib +0 -0
  257. package/third-party/ios/ExecutorchLib.xcframework/ios-arm64/ExecutorchLib.framework/Info.plist +0 -0
  258. package/third-party/ios/ExecutorchLib.xcframework/ios-arm64-simulator/ExecutorchLib.framework/ExecutorchLib +0 -0
  259. package/third-party/ios/ExecutorchLib.xcframework/ios-arm64-simulator/ExecutorchLib.framework/Info.plist +0 -0
  260. package/common/rnexecutorch/tests/run_all_tests.sh +0 -14
  261. package/common/rnexecutorch/tests/run_test.sh +0 -18
  262. package/ios/RnExecutorch/utils/Conversions.h +0 -14
  263. package/ios/RnExecutorch/utils/ETError.h +0 -26
  264. package/ios/RnExecutorch/utils/ImageProcessor.h +0 -15
  265. package/ios/RnExecutorch/utils/ImageProcessor.mm +0 -147
  266. package/ios/RnExecutorch/utils/Numerical.h +0 -3
  267. package/ios/RnExecutorch/utils/Numerical.mm +0 -18
  268. package/ios/RnExecutorch/utils/ScalarType.h +0 -14
  269. package/ios/RnExecutorch/utils/ScalarType.mm +0 -21
  270. package/lib/module/hooks/useNonStaticModule.js.map +0 -1
  271. package/lib/typescript/hooks/useNonStaticModule.d.ts +0 -21
  272. package/lib/typescript/hooks/useNonStaticModule.d.ts.map +0 -1
  273. package/src/hooks/useNonStaticModule.ts +0 -74
  274. package/third-party/include/executorch/extension/kernel_util/make_boxed_from_unboxed_functor.h +0 -181
  275. package/third-party/include/executorch/extension/kernel_util/meta_programming.h +0 -108
  276. package/third-party/include/executorch/extension/kernel_util/type_list.h +0 -137
  277. package/third-party/include/executorch/extension/threadpool/threadpool_guard.h +0 -35
@@ -0,0 +1,97 @@
1
+ #pragma once
2
+ #include <c10/macros/Macros.h>
3
+
4
+ #include <cstddef>
5
+ #include <cstdint>
6
+
7
+ // GCC has __builtin_mul_overflow from before it supported __has_builtin
8
+ #ifdef _MSC_VER
9
+ #define C10_HAS_BUILTIN_OVERFLOW() (0)
10
+ #include <c10/util/llvmMathExtras.h>
11
+ #include <intrin.h>
12
+ #else
13
+ #define C10_HAS_BUILTIN_OVERFLOW() (1)
14
+ #endif
15
+
16
+ namespace c10 {
17
+
18
+ C10_ALWAYS_INLINE bool add_overflows(uint64_t a, uint64_t b, uint64_t *out) {
19
+ #if C10_HAS_BUILTIN_OVERFLOW()
20
+ return __builtin_add_overflow(a, b, out);
21
+ #else
22
+ unsigned long long tmp;
23
+ #if defined(_M_IX86) || defined(_M_X64)
24
+ auto carry = _addcarry_u64(0, a, b, &tmp);
25
+ #else
26
+ tmp = a + b;
27
+ unsigned long long vector = (a & b) ^ ((a ^ b) & ~tmp);
28
+ auto carry = vector >> 63;
29
+ #endif
30
+ *out = tmp;
31
+ return carry;
32
+ #endif
33
+ }
34
+
35
+ template <typename T> C10_ALWAYS_INLINE bool mul_overflows(T a, T b, T *out) {
36
+ #if C10_HAS_BUILTIN_OVERFLOW()
37
+ return __builtin_mul_overflow(a, b, out);
38
+ #else
39
+ static_assert(std::is_integral_v<T>,
40
+ "mul_overflows only supports integral types");
41
+
42
+ if constexpr (std::is_signed_v<T>) {
43
+ // For signed types, use the division-based check
44
+ volatile T tmp = a * b;
45
+ *out = tmp;
46
+ if (a == 0 || b == 0) {
47
+ return false;
48
+ }
49
+ return !(a == tmp / b);
50
+ } else {
51
+ // For unsigned types, use leading zeros approach
52
+ // This test isn't exact, but avoids doing integer division
53
+ *out = a * b;
54
+ constexpr int bits = sizeof(T) * 8;
55
+ return ((c10::llvm::countLeadingZeros(a) +
56
+ c10::llvm::countLeadingZeros(b)) < bits);
57
+ }
58
+ #endif
59
+ }
60
+
61
+ C10_ALWAYS_INLINE bool mul_overflows(uint64_t a, uint64_t b, uint64_t *out) {
62
+ return mul_overflows<uint64_t>(a, b, out);
63
+ }
64
+
65
+ template <typename It>
66
+ bool safe_multiplies_u64(It first, It last, uint64_t *out) {
67
+ #if C10_HAS_BUILTIN_OVERFLOW()
68
+ uint64_t prod = 1;
69
+ bool overflow = false;
70
+ for (; first != last; ++first) {
71
+ overflow |= c10::mul_overflows(prod, *first, &prod);
72
+ }
73
+ *out = prod;
74
+ return overflow;
75
+ #else
76
+ uint64_t prod = 1;
77
+ uint64_t prod_log2 = 0;
78
+ bool is_zero = false;
79
+ for (; first != last; ++first) {
80
+ auto x = static_cast<uint64_t>(*first);
81
+ prod *= x;
82
+ // log2(0) isn't valid, so need to track it specially
83
+ is_zero |= (x == 0);
84
+ prod_log2 += c10::llvm::Log2_64_Ceil(x);
85
+ }
86
+ *out = prod;
87
+ // This test isn't exact, but avoids doing integer division
88
+ return !is_zero && (prod_log2 >= 64);
89
+ #endif
90
+ }
91
+
92
+ template <typename Container>
93
+ bool safe_multiplies_u64(const Container &c, uint64_t *out) {
94
+ return safe_multiplies_u64(c.begin(), c.end(), out);
95
+ }
96
+
97
+ } // namespace c10
@@ -57,8 +57,8 @@ typedef NS_ERROR_ENUM(ExecuTorchErrorDomain, ExecuTorchErrorCode){
57
57
  * @return An NSString containing the error description.
58
58
  */
59
59
  FOUNDATION_EXPORT
60
- __attribute__((deprecated("This API is experimental."))) NSString *
61
- ExecuTorchErrorDescription(ExecuTorchErrorCode code)
60
+ NS_RETURNS_RETAINED
61
+ NSString *ExecuTorchErrorDescription(ExecuTorchErrorCode code)
62
62
  NS_SWIFT_NAME(ErrorDescription(_:));
63
63
 
64
64
  /**
@@ -70,8 +70,8 @@ ExecuTorchErrorDescription(ExecuTorchErrorCode code)
70
70
  */
71
71
  FOUNDATION_EXPORT
72
72
  NS_RETURNS_RETAINED
73
- __attribute__((deprecated("This API is experimental."))) NSError *
74
- ExecuTorchErrorWithCode(ExecuTorchErrorCode code) NS_SWIFT_NAME(Error(code:));
73
+ NSError *ExecuTorchErrorWithCode(ExecuTorchErrorCode code)
74
+ NS_SWIFT_NAME(Error(code:));
75
75
 
76
76
  /**
77
77
  * Create an NSError in the ExecuTorch domain for the given code.
@@ -83,9 +83,8 @@ ExecuTorchErrorWithCode(ExecuTorchErrorCode code) NS_SWIFT_NAME(Error(code:));
83
83
  */
84
84
  FOUNDATION_EXPORT
85
85
  NS_RETURNS_RETAINED
86
- __attribute__((deprecated("This API is experimental."))) NSError *
87
- ExecuTorchErrorWithCodeAndDescription(ExecuTorchErrorCode code,
88
- NSString *__nullable description)
86
+ NSError *ExecuTorchErrorWithCodeAndDescription(ExecuTorchErrorCode code,
87
+ NSString *__nullable description)
89
88
  NS_SWIFT_NAME(Error(code:description:));
90
89
 
91
90
  NS_ASSUME_NONNULL_END
@@ -0,0 +1,12 @@
1
+ /*
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ * All rights reserved.
4
+ *
5
+ * This source code is licensed under the BSD-style license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+
9
+ #import "ExecuTorchLLMConfig.h"
10
+ #import "ExecuTorchLLMError.h"
11
+ #import "ExecuTorchLLMMultimodalRunner.h"
12
+ #import "ExecuTorchLLMTextRunner.h"
@@ -0,0 +1,56 @@
1
+ /*
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ * All rights reserved.
4
+ *
5
+ * This source code is licensed under the BSD-style license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+
9
+ #import <Foundation/Foundation.h>
10
+
11
+ NS_ASSUME_NONNULL_BEGIN
12
+
13
+ /**
14
+ A configuration object for text generation.
15
+
16
+ This class wraps the underlying C++ GenerationConfig so that default
17
+ values and future fields remain a single source of truth in C++.
18
+ */
19
+ NS_SWIFT_NAME(Config)
20
+ __attribute__((deprecated("This API is experimental.")))
21
+ __attribute__((objc_subclassing_restricted))
22
+ @interface ExecuTorchLLMConfig : NSObject<NSCopying>
23
+
24
+ /** Whether to echo the input prompt in the output. */
25
+ @property(nonatomic, getter=isEchoEnabled) BOOL echoEnabled;
26
+
27
+ /** Maximum number of new tokens to generate. */
28
+ @property(nonatomic) NSInteger maximumNewTokens;
29
+
30
+ /** Whether this is a warmup run. */
31
+ @property(nonatomic, getter=isWarming) BOOL warming;
32
+
33
+ /** Maximum total sequence length. */
34
+ @property(nonatomic) NSInteger sequenceLength;
35
+
36
+ /** Temperature for sampling. */
37
+ @property(nonatomic) double temperature;
38
+
39
+ /** Number of BOS tokens to add. */
40
+ @property(nonatomic) NSInteger bosCount;
41
+
42
+ /** Number of EOS tokens to add. */
43
+ @property(nonatomic) NSInteger eosCount;
44
+
45
+ /**
46
+ Initializes a configuration and invokes the block to mutate it.
47
+
48
+ @param block A block that receives the newly initialized configuration.
49
+ @return An initialized ExecuTorchLLMConfig instance.
50
+ */
51
+ - (instancetype)initWithBlock:(NS_NOESCAPE void (^)(ExecuTorchLLMConfig *))block
52
+ NS_SWIFT_NAME(init(_:));
53
+
54
+ @end
55
+
56
+ NS_ASSUME_NONNULL_END
@@ -0,0 +1,16 @@
1
+ /*
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ * All rights reserved.
4
+ *
5
+ * This source code is licensed under the BSD-style license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+
9
+ #import <Foundation/Foundation.h>
10
+
11
+ NS_ASSUME_NONNULL_BEGIN
12
+
13
+ FOUNDATION_EXPORT NSErrorDomain const
14
+ ExecuTorchLLMErrorDomain NS_SWIFT_NAME(ErrorDomain);
15
+
16
+ NS_ASSUME_NONNULL_END
@@ -0,0 +1,227 @@
1
+ /*
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ * All rights reserved.
4
+ *
5
+ * This source code is licensed under the BSD-style license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+
9
+ #import "ExecuTorchLLMConfig.h"
10
+
11
+ NS_ASSUME_NONNULL_BEGIN
12
+
13
+ /**
14
+ Types of multimodal inputs supported by the ExecuTorch LLM APIs.
15
+ Must be in sync with the C++ enum in llm/runner/multimodal_input.h
16
+ */
17
+ typedef NS_ENUM(NSInteger, ExecuTorchLLMMultimodalInputType) {
18
+ ExecuTorchLLMMultimodalInputTypeText,
19
+ ExecuTorchLLMMultimodalInputTypeImage,
20
+ ExecuTorchLLMMultimodalInputTypeAudio,
21
+ ExecuTorchLLMMultimodalInputTypeUnsupported,
22
+ } NS_SWIFT_NAME(MultimodalInputType);
23
+
24
+ /**
25
+ A container for image inputs used with multimodal generation APIs.
26
+ */
27
+ NS_SWIFT_NAME(Image)
28
+ __attribute__((deprecated("This API is experimental.")))
29
+ __attribute__((objc_subclassing_restricted))
30
+ @interface ExecuTorchLLMImage : NSObject<NSCopying>
31
+
32
+ /**
33
+ Initializes an image container with the provided data and dimensions.
34
+
35
+ @param data Raw image bytes.
36
+ @param width Image width in pixels.
37
+ @param height Image height in pixels.
38
+ @param channels Number of channels.
39
+ @return An initialized ExecuTorchLLMImage instance.
40
+ */
41
+ - (instancetype)initWithData:(NSData *)data
42
+ width:(NSInteger)width
43
+ height:(NSInteger)height
44
+ channels:(NSInteger)channels NS_DESIGNATED_INITIALIZER;
45
+
46
+ - (instancetype)initWithFloatData:(NSData *)data
47
+ width:(NSInteger)width
48
+ height:(NSInteger)height
49
+ channels:(NSInteger)channels NS_DESIGNATED_INITIALIZER;
50
+
51
+ @property(nonatomic, readonly) NSData *data;
52
+
53
+ @property(nonatomic, readonly) NSInteger width;
54
+
55
+ @property(nonatomic, readonly) NSInteger height;
56
+
57
+ @property(nonatomic, readonly) NSInteger channels;
58
+
59
+ @property(nonatomic, readonly) BOOL isFloat;
60
+
61
+ + (instancetype)new NS_UNAVAILABLE;
62
+ - (instancetype)init NS_UNAVAILABLE;
63
+
64
+ @end
65
+
66
+ /**
67
+ A container for pre-processed audio features.
68
+ */
69
+ NS_SWIFT_NAME(Audio)
70
+ __attribute__((deprecated("This API is experimental.")))
71
+ __attribute__((objc_subclassing_restricted))
72
+ @interface ExecuTorchLLMAudio : NSObject<NSCopying>
73
+
74
+ /**
75
+ Initializes an audio features container with the provided data and shape.
76
+
77
+ @param data Feature buffer.
78
+ @param batchSize Batch dimension size.
79
+ @param bins Number of frequency bins.
80
+ @param frames Number of time frames.
81
+ @return An initialized ExecuTorchLLMAudio instance.
82
+ */
83
+ - (instancetype)initWithData:(NSData *)data
84
+ batchSize:(NSInteger)batchSize
85
+ bins:(NSInteger)bins
86
+ frames:(NSInteger)frames NS_DESIGNATED_INITIALIZER;
87
+
88
+ - (instancetype)initWithFloatData:(NSData *)data
89
+ batchSize:(NSInteger)batchSize
90
+ bins:(NSInteger)bins
91
+ frames:(NSInteger)frames NS_DESIGNATED_INITIALIZER;
92
+
93
+ @property(nonatomic, readonly) NSData *data;
94
+
95
+ @property(nonatomic, readonly) NSInteger batchSize;
96
+
97
+ @property(nonatomic, readonly) NSInteger bins;
98
+
99
+ @property(nonatomic, readonly) NSInteger frames;
100
+
101
+ @property(nonatomic, readonly) BOOL isFloat;
102
+
103
+ + (instancetype)new NS_UNAVAILABLE;
104
+ - (instancetype)init NS_UNAVAILABLE;
105
+
106
+ @end
107
+
108
+ /**
109
+ A tagged container for a single multimodal input item used by
110
+ multimodal generation APIs.
111
+ */
112
+ NS_SWIFT_NAME(MultimodalInput)
113
+ __attribute__((deprecated("This API is experimental.")))
114
+ __attribute__((objc_subclassing_restricted))
115
+ @interface ExecuTorchLLMMultimodalInput : NSObject<NSCopying>
116
+
117
+ /**
118
+ Creates a text input.
119
+
120
+ @param text The UTF-8 text to provide as input.
121
+ @return A retained ExecuTorchLLMMultimodalInput instance of type Text.
122
+ */
123
+ + (instancetype)inputWithText:(NSString *)text
124
+ NS_SWIFT_NAME(init(_:)) NS_RETURNS_RETAINED;
125
+
126
+ /**
127
+ Creates an image input.
128
+
129
+ @param image The image payload to provide as input.
130
+ @return A retained ExecuTorchLLMMultimodalInput instance of type Image.
131
+ */
132
+ + (instancetype)inputWithImage:(ExecuTorchLLMImage *)image
133
+ NS_SWIFT_NAME(init(_:)) NS_RETURNS_RETAINED;
134
+
135
+ /**
136
+ Creates an audio-features input.
137
+
138
+ @param audio The pre-processed audio features to provide as input.
139
+ @return A retained ExecuTorchLLMMultimodalInput instance of type Audio.
140
+ */
141
+ + (instancetype)inputWithAudio:(ExecuTorchLLMAudio *)audio
142
+ NS_SWIFT_NAME(init(_:)) NS_RETURNS_RETAINED;
143
+
144
+ @property(nonatomic, readonly) ExecuTorchLLMMultimodalInputType type;
145
+
146
+ @property(nonatomic, readonly, nullable) NSString *text;
147
+
148
+ @property(nonatomic, readonly, nullable) ExecuTorchLLMImage *image;
149
+
150
+ @property(nonatomic, readonly, nullable) ExecuTorchLLMAudio *audio;
151
+
152
+ + (instancetype)new NS_UNAVAILABLE;
153
+ - (instancetype)init NS_UNAVAILABLE;
154
+
155
+ @end
156
+
157
+ /**
158
+ A wrapper class for the C++ llm::MultimodalRunner that provides
159
+ Objective-C APIs to load models, manage tokenization, accept mixed
160
+ input modalities, generate text sequences, and stop the runner.
161
+ */
162
+ NS_SWIFT_NAME(MultimodalRunner)
163
+ __attribute__((deprecated("This API is experimental.")))
164
+ __attribute__((objc_subclassing_restricted))
165
+ @interface ExecuTorchLLMMultimodalRunner : NSObject
166
+
167
+ /**
168
+ Initializes a multimodal LLM runner with the given model and tokenizer paths.
169
+
170
+ @param modelPath File system path to the serialized model.
171
+ @param tokenizerPath File system path to the tokenizer data.
172
+ @return An initialized ExecuTorchLLMMultimodalRunner instance.
173
+ */
174
+ - (instancetype)initWithModelPath:(NSString *)modelPath
175
+ tokenizerPath:(NSString *)tokenizerPath
176
+ NS_DESIGNATED_INITIALIZER;
177
+
178
+ /**
179
+ Checks whether the underlying model has been successfully loaded.
180
+
181
+ @return YES if the model is loaded, NO otherwise.
182
+ */
183
+ - (BOOL)isLoaded;
184
+
185
+ /**
186
+ Loads the model into memory, returning an error if loading fails.
187
+
188
+ @param error On failure, populated with an NSError explaining the issue.
189
+ @return YES if loading succeeds, NO if an error occurred.
190
+ */
191
+ - (BOOL)loadWithError:(NSError **)error;
192
+
193
+ /**
194
+ Generates text given a list of multimodal inputs. A default configuration
195
+ is created and passed to the configuration block for in-place mutation.
196
+
197
+ The token callback, if provided, is invoked for each generated token.
198
+
199
+ @param inputs An ordered array of multimodal inputs.
200
+ @param config A configuration object.
201
+ @param callback A block called with each generated token as an NSString.
202
+ @param error On failure, populated with an NSError explaining the issue.
203
+ @return YES if generation completes successfully, NO if an error occurred.
204
+ */
205
+ - (BOOL)generateWithInputs:(NSArray<ExecuTorchLLMMultimodalInput *> *)inputs
206
+ config:(ExecuTorchLLMConfig *)config
207
+ tokenCallback:(nullable void (^)(NSString *))callback
208
+ error:(NSError **)error
209
+ NS_SWIFT_NAME(generate(_:_:tokenCallback:));
210
+
211
+ /**
212
+ Stop producing new tokens and terminate the current generation process.
213
+ */
214
+ - (void)stop;
215
+
216
+ /**
217
+ Remove the prefilled tokens from the KV cache and reset the start position
218
+ to 0. It also clears the stats for previous runs.
219
+ */
220
+ - (void)reset;
221
+
222
+ + (instancetype)new NS_UNAVAILABLE;
223
+ - (instancetype)init NS_UNAVAILABLE;
224
+
225
+ @end
226
+
227
+ NS_ASSUME_NONNULL_END
@@ -0,0 +1,97 @@
1
+ /*
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ * All rights reserved.
4
+ *
5
+ * This source code is licensed under the BSD-style license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+
9
+ #import "ExecuTorchLLMConfig.h"
10
+
11
+ NS_ASSUME_NONNULL_BEGIN
12
+
13
+ /**
14
+ A wrapper class for the C++ llm::TextLLMRunner that provides
15
+ Objective-C APIs to load models, manage tokenization with custom
16
+ special tokens, generate text sequences, and stop the runner.
17
+ */
18
+ NS_SWIFT_NAME(TextRunner)
19
+ __attribute__((deprecated("This API is experimental.")))
20
+ @interface ExecuTorchLLMTextRunner : NSObject
21
+
22
+ /**
23
+ Initializes a text LLM runner with the given model and tokenizer paths,
24
+ and a list of special tokens to include in the tokenizer.
25
+
26
+ @param modelPath File system path to the serialized model.
27
+ @param tokenizerPath File system path to the tokenizer data.
28
+ @return An initialized ExecuTorchLLMTextRunner instance.
29
+ */
30
+ - (instancetype)initWithModelPath:(NSString *)modelPath
31
+ tokenizerPath:(NSString *)tokenizerPath;
32
+
33
+ /**
34
+ Initializes a text LLM runner with the given model and tokenizer paths,
35
+ and a list of special tokens to include in the tokenizer.
36
+
37
+ @param modelPath File system path to the serialized model.
38
+ @param tokenizerPath File system path to the tokenizer data.
39
+ @param specialTokens An array of NSString special tokens to use during
40
+ tokenization.
41
+ @return An initialized ExecuTorchLLMTextRunner instance.
42
+ */
43
+ - (instancetype)initWithModelPath:(NSString *)modelPath
44
+ tokenizerPath:(NSString *)tokenizerPath
45
+ specialTokens:(NSArray<NSString *> *)specialTokens
46
+ NS_DESIGNATED_INITIALIZER;
47
+
48
+ /**
49
+ Checks whether the underlying model has been successfully loaded.
50
+
51
+ @return YES if the model is loaded, NO otherwise.
52
+ */
53
+ - (BOOL)isLoaded;
54
+
55
+ /**
56
+ Loads the model into memory, returning an error if loading fails.
57
+
58
+ @param error On failure, populated with an NSError explaining the issue.
59
+ @return YES if loading succeeds, NO if an error occurred.
60
+ */
61
+ - (BOOL)loadWithError:(NSError **)error;
62
+
63
+ /**
64
+ Generates text given an input prompt. A default configuration
65
+ is created and passed to the configuration block for in-place mutation.
66
+
67
+ The token callback, if provided, is invoked for each generated token.
68
+
69
+ @param prompt The initial text prompt to generate from.
70
+ @param config A configuration object.
71
+ @param callback A block called with each generated token as an NSString.
72
+ @param error On failure, populated with an NSError explaining the issue.
73
+ @return YES if generation completes successfully, NO if an error occurred.
74
+ */
75
+ - (BOOL)generateWithPrompt:(NSString *)prompt
76
+ config:(ExecuTorchLLMConfig *)config
77
+ tokenCallback:(nullable void (^)(NSString *token))callback
78
+ error:(NSError **)error
79
+ NS_SWIFT_NAME(generate(_:_:tokenCallback:));
80
+
81
+ /**
82
+ Stop producing new tokens and terminate the current generation process.
83
+ */
84
+ - (void)stop;
85
+
86
+ /**
87
+ Remove the prefilled tokens from the KV cache and reset the start position
88
+ to 0. It also clears the stats for previous runs.
89
+ */
90
+ - (void)reset;
91
+
92
+ + (instancetype)new NS_UNAVAILABLE;
93
+ - (instancetype)init NS_UNAVAILABLE;
94
+
95
+ @end
96
+
97
+ NS_ASSUME_NONNULL_END
@@ -0,0 +1,4 @@
1
+ module ExecuTorchLLM {
2
+ umbrella header "ExecuTorchLLM.h"
3
+ export *
4
+ }
@@ -49,6 +49,7 @@ NS_SWIFT_NAME(LogSink)
49
49
  * A singleton class for managing log sinks and dispatching log messages.
50
50
  */
51
51
  NS_SWIFT_NAME(Log)
52
+ __attribute__((objc_subclassing_restricted))
52
53
  @interface ExecuTorchLog : NSObject
53
54
 
54
55
  /// The shared singleton log instance.