react-native-executorch 0.9.0 → 0.9.1

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 (96) hide show
  1. package/android/libs/classes.jar +0 -0
  2. package/common/rnexecutorch/host_objects/JsiConversions.h +43 -0
  3. package/common/rnexecutorch/models/llm/LLM.cpp +55 -42
  4. package/common/rnexecutorch/models/llm/LLM.h +4 -3
  5. package/common/rnexecutorch/models/llm/Types.h +23 -0
  6. package/common/runner/base_llm_runner.cpp +10 -3
  7. package/common/runner/base_llm_runner.h +1 -0
  8. package/common/runner/constants.h +15 -1
  9. package/common/runner/encoders/audio_encoder.cpp +111 -0
  10. package/common/runner/encoders/audio_encoder.h +40 -0
  11. package/common/runner/encoders/vision_encoder.cpp +0 -1
  12. package/common/runner/irunner.h +5 -0
  13. package/common/runner/multimodal_decoder_runner.h +50 -1
  14. package/common/runner/multimodal_input.h +16 -1
  15. package/common/runner/multimodal_prefiller.cpp +374 -64
  16. package/common/runner/multimodal_prefiller.h +57 -6
  17. package/common/runner/multimodal_runner.cpp +19 -12
  18. package/common/runner/multimodal_runner.h +1 -1
  19. package/common/runner/sampler.cpp +111 -35
  20. package/common/runner/sampler.h +13 -5
  21. package/common/runner/text_decoder_runner.cpp +1 -4
  22. package/common/runner/text_decoder_runner.h +3 -2
  23. package/common/runner/text_prefiller.cpp +8 -8
  24. package/common/runner/text_prefiller.h +8 -1
  25. package/common/runner/text_runner.cpp +35 -9
  26. package/common/runner/text_token_generator.h +2 -3
  27. package/common/runner/util.h +0 -1
  28. package/lib/module/constants/llmDefaults.js +1 -1
  29. package/lib/module/constants/llmDefaults.js.map +1 -1
  30. package/lib/module/constants/modelRegistry.js +33 -2
  31. package/lib/module/constants/modelRegistry.js.map +1 -1
  32. package/lib/module/constants/modelUrls.js +43 -6
  33. package/lib/module/constants/modelUrls.js.map +1 -1
  34. package/lib/module/controllers/LLMController.js +69 -20
  35. package/lib/module/controllers/LLMController.js.map +1 -1
  36. package/lib/module/hooks/natural_language_processing/useLLM.js +1 -5
  37. package/lib/module/hooks/natural_language_processing/useLLM.js.map +1 -1
  38. package/lib/module/modules/natural_language_processing/LLMModule.js +12 -7
  39. package/lib/module/modules/natural_language_processing/LLMModule.js.map +1 -1
  40. package/lib/module/types/llm.js +11 -0
  41. package/lib/module/types/llm.js.map +1 -1
  42. package/lib/typescript/constants/llmDefaults.d.ts +1 -1
  43. package/lib/typescript/constants/llmDefaults.d.ts.map +1 -1
  44. package/lib/typescript/constants/modelRegistry.d.ts +28 -1
  45. package/lib/typescript/constants/modelRegistry.d.ts.map +1 -1
  46. package/lib/typescript/constants/modelUrls.d.ts +40 -12
  47. package/lib/typescript/constants/modelUrls.d.ts.map +1 -1
  48. package/lib/typescript/controllers/LLMController.d.ts +7 -9
  49. package/lib/typescript/controllers/LLMController.d.ts.map +1 -1
  50. package/lib/typescript/modules/natural_language_processing/LLMModule.d.ts +6 -3
  51. package/lib/typescript/modules/natural_language_processing/LLMModule.d.ts.map +1 -1
  52. package/lib/typescript/types/llm.d.ts +63 -36
  53. package/lib/typescript/types/llm.d.ts.map +1 -1
  54. package/package.json +1 -1
  55. package/react-native-executorch.podspec +6 -0
  56. package/src/constants/llmDefaults.ts +1 -1
  57. package/src/constants/modelRegistry.ts +34 -2
  58. package/src/constants/modelUrls.ts +47 -6
  59. package/src/controllers/LLMController.ts +89 -40
  60. package/src/hooks/natural_language_processing/useLLM.ts +5 -6
  61. package/src/modules/natural_language_processing/LLMModule.ts +19 -8
  62. package/src/types/llm.ts +64 -34
  63. package/third-party/android/libs/executorch/arm64-v8a/libexecutorch.so +0 -0
  64. package/third-party/android/libs/executorch/x86_64/libexecutorch.so +0 -0
  65. package/third-party/include/executorch/ExecuTorch.h +2 -0
  66. package/third-party/include/executorch/ExecuTorchModule.h +46 -0
  67. package/third-party/include/executorch/extension/data_loader/buffer_data_loader.h +4 -3
  68. package/third-party/include/executorch/extension/data_loader/mman.h +46 -0
  69. package/third-party/include/executorch/extension/data_loader/mmap_data_loader.h +4 -0
  70. package/third-party/include/executorch/extension/data_loader/shared_ptr_data_loader.h +7 -3
  71. package/third-party/include/executorch/extension/module/module.h +47 -8
  72. package/third-party/include/executorch/extension/tensor/tensor_ptr.h +17 -5
  73. package/third-party/include/executorch/kernels/optimized/Functions.h +12 -0
  74. package/third-party/include/executorch/kernels/optimized/NativeFunctions.h +4 -0
  75. package/third-party/include/executorch/kernels/portable/Functions.h +18 -0
  76. package/third-party/include/executorch/kernels/portable/NativeFunctions.h +6 -0
  77. package/third-party/include/executorch/runtime/backend/backend_options_map.h +37 -0
  78. package/third-party/include/executorch/runtime/core/array_ref.h +3 -1
  79. package/third-party/include/executorch/runtime/core/error.h +1 -0
  80. package/third-party/include/executorch/runtime/core/evalue.h +256 -9
  81. package/third-party/include/executorch/runtime/core/exec_aten/exec_aten.h +24 -0
  82. package/third-party/include/executorch/runtime/core/hierarchical_allocator.h +9 -6
  83. package/third-party/include/executorch/runtime/core/portable_type/device.h +3 -4
  84. package/third-party/include/executorch/runtime/core/portable_type/tensor_impl.h +31 -1
  85. package/third-party/include/executorch/runtime/executor/method.h +9 -3
  86. package/third-party/include/executorch/runtime/executor/method_meta.h +14 -0
  87. package/third-party/include/executorch/runtime/executor/platform_memory_allocator.h +12 -2
  88. package/third-party/include/executorch/runtime/executor/program.h +3 -1
  89. package/third-party/include/executorch/runtime/executor/tensor_parser.h +5 -1
  90. package/third-party/include/executorch/runtime/kernel/operator_registry.h +9 -0
  91. package/third-party/ios/ExecutorchLib.xcframework/ios-arm64/ExecutorchLib.framework/ExecutorchLib +0 -0
  92. package/third-party/ios/ExecutorchLib.xcframework/ios-arm64/ExecutorchLib.framework/Info.plist +0 -0
  93. package/third-party/ios/ExecutorchLib.xcframework/ios-arm64/ExecutorchLib.framework/mlx.metallib +0 -0
  94. package/third-party/ios/ExecutorchLib.xcframework/ios-arm64-simulator/ExecutorchLib.framework/ExecutorchLib +0 -0
  95. package/third-party/ios/ExecutorchLib.xcframework/ios-arm64-simulator/ExecutorchLib.framework/Info.plist +0 -0
  96. package/third-party/ios/ExecutorchLib.xcframework/ios-arm64-simulator/ExecutorchLib.framework/mlx.metallib +0 -0
@@ -10,8 +10,11 @@
10
10
 
11
11
  #include <executorch/runtime/core/array_ref.h>
12
12
  #include <executorch/runtime/core/error.h>
13
+ #include <executorch/runtime/core/portable_type/device.h>
13
14
  #include <executorch/runtime/core/portable_type/scalar_type.h>
15
+ #include <executorch/runtime/core/result.h>
14
16
  #include <executorch/runtime/core/tensor_shape_dynamism.h>
17
+ #include <executorch/runtime/platform/compiler.h>
15
18
 
16
19
  // Forward declaration of a helper that provides access to internal resizing
17
20
  // methods of TensorImpl. Real definition is in
@@ -99,11 +102,15 @@ public:
99
102
  * @param strides Strides of the tensor at each dimension. Must contain `dim`
100
103
  * entries.
101
104
  * @param dynamism The mutability of the shape of the tensor.
105
+ * @param device_type The type of device where tensor data resides.
106
+ * @param device_index The device index for multi-device scenarios.
102
107
  */
103
108
  TensorImpl(ScalarType type, ssize_t dim, SizesType *sizes,
104
109
  void *data = nullptr, DimOrderType *dim_order = nullptr,
105
110
  StridesType *strides = nullptr,
106
- TensorShapeDynamism dynamism = TensorShapeDynamism::STATIC);
111
+ TensorShapeDynamism dynamism = TensorShapeDynamism::STATIC,
112
+ DeviceType device_type = DeviceType::CPU,
113
+ DeviceIndex device_index = 0);
107
114
 
108
115
  /**
109
116
  * Returns the size of the tensor in bytes.
@@ -161,6 +168,15 @@ public:
161
168
  /// Returns the mutability of the shape of the tensor.
162
169
  TensorShapeDynamism shape_dynamism() const { return shape_dynamism_; }
163
170
 
171
+ /// Returns the device where tensor data resides.
172
+ Device device() const { return device_; }
173
+
174
+ /// Returns the type of device where tensor data resides.
175
+ DeviceType device_type() const { return device_.type(); }
176
+
177
+ /// Returns the device index, or 0 if default/unspecified.
178
+ DeviceIndex device_index() const { return device_.index(); }
179
+
164
180
  /// Returns a pointer of type T to the constant underlying data blob.
165
181
  template <typename T> inline const T *data() const {
166
182
  return static_cast<const T *>(data());
@@ -238,6 +254,9 @@ private:
238
254
 
239
255
  /// Specifies the mutability of the shape of the tensor.
240
256
  const TensorShapeDynamism shape_dynamism_;
257
+
258
+ /// Device where tensor data resides (CPU, CUDA, etc.)
259
+ Device device_;
241
260
  };
242
261
 
243
262
  /**
@@ -247,6 +266,16 @@ ssize_t compute_numel(
247
266
  const ::executorch::runtime::etensor::TensorImpl::SizesType *sizes,
248
267
  ssize_t dim);
249
268
 
269
+ /**
270
+ * Compute the number of elements based on the sizes of a tensor.
271
+ * Returns Error::InvalidArgument if any intermediate multiplication would
272
+ * overflow ssize_t, or if a size is negative. Prefer this over compute_numel()
273
+ * for paths that can propagate an Error upward.
274
+ */
275
+ ::executorch::runtime::Result<ssize_t>
276
+ safe_numel(const ::executorch::runtime::etensor::TensorImpl::SizesType *sizes,
277
+ ssize_t dim);
278
+
250
279
  /// Appropriate format specifier for the result of calling
251
280
  /// size(). Must be used instead of using zd directly to support ATen
252
281
  /// mode.
@@ -276,6 +305,7 @@ namespace executor {
276
305
  // TODO(T197294990): Remove these deprecated aliases once all users have moved
277
306
  // to the new `::executorch` namespaces.
278
307
  using ::executorch::runtime::etensor::compute_numel;
308
+ using ::executorch::runtime::etensor::safe_numel;
279
309
  using ::executorch::runtime::etensor::TensorImpl;
280
310
  } // namespace executor
281
311
  } // namespace torch
@@ -23,6 +23,7 @@
23
23
  #include <executorch/runtime/executor/memory_manager.h>
24
24
  #include <executorch/runtime/executor/merged_data_map.h>
25
25
  #include <executorch/runtime/executor/method_meta.h>
26
+ #include <executorch/runtime/kernel/operator_registry.h>
26
27
  #include <executorch/runtime/platform/compiler.h>
27
28
 
28
29
  // Forward declare flatbuffer types. This is a public header and must not
@@ -77,7 +78,7 @@ public:
77
78
  merged_data_map_(std::move(rhs.merged_data_map_)),
78
79
  external_constants_(rhs.external_constants_),
79
80
  n_external_constants_(rhs.n_external_constants_),
80
- init_state_(rhs.init_state_) {
81
+ kernel_registry_(rhs.kernel_registry_), init_state_(rhs.init_state_) {
81
82
  // Required: clear out fields that the dtor looks at, so that we don't free
82
83
  // anything twice.
83
84
  rhs.n_value_ = 0;
@@ -323,13 +324,15 @@ private:
323
324
  };
324
325
 
325
326
  Method(const Program *program, MemoryManager *memory_manager,
326
- EventTracer *event_tracer, MemoryAllocator *temp_allocator)
327
+ EventTracer *event_tracer, MemoryAllocator *temp_allocator,
328
+ Span<const Kernel> kernel_registry = {})
327
329
  : step_state_(), program_(program), memory_manager_(memory_manager),
328
330
  temp_allocator_(temp_allocator), serialization_plan_(nullptr),
329
331
  event_tracer_(event_tracer), n_value_(0), values_(nullptr),
330
332
  input_set_(nullptr), n_delegate_(0), delegates_(nullptr), n_chains_(0),
331
333
  chains_(nullptr), merged_data_map_(nullptr),
332
334
  external_constants_(nullptr), n_external_constants_(0),
335
+ kernel_registry_(kernel_registry),
333
336
  init_state_(InitializationState::Uninitialized) {}
334
337
 
335
338
  /// Static factory used by Program.
@@ -337,7 +340,8 @@ private:
337
340
  load(executorch_flatbuffer::ExecutionPlan *s_plan, const Program *program,
338
341
  MemoryManager *memory_manager, EventTracer *event_tracer,
339
342
  const NamedDataMap *named_data_map,
340
- const LoadBackendOptionsMap *backend_options = nullptr);
343
+ const LoadBackendOptionsMap *backend_options = nullptr,
344
+ Span<const Kernel> kernel_registry = {});
341
345
 
342
346
  /**
343
347
  * Initialize the method from its serialized representation.
@@ -382,6 +386,8 @@ private:
382
386
  NamedData *external_constants_;
383
387
  size_t n_external_constants_ = 0;
384
388
 
389
+ Span<const Kernel> kernel_registry_;
390
+
385
391
  InitializationState init_state_;
386
392
 
387
393
  /**
@@ -9,6 +9,7 @@
9
9
  #pragma once
10
10
 
11
11
  #include <executorch/runtime/core/exec_aten/exec_aten.h>
12
+ #include <executorch/runtime/core/portable_type/device.h>
12
13
  #include <executorch/runtime/core/result.h>
13
14
  #include <executorch/runtime/core/span.h>
14
15
  #include <executorch/runtime/core/tag.h>
@@ -230,6 +231,19 @@ public:
230
231
  */
231
232
  Result<int64_t> memory_planned_buffer_size(size_t index) const;
232
233
 
234
+ /**
235
+ * Get the device placement for the specified memory-planned buffer.
236
+ *
237
+ * For CPU-only programs (no non_const_buffer_device in the PTE), all buffers
238
+ * default to Device{CPU, 0}. For programs with device annotations, returns
239
+ * the device type and index that the buffer should be allocated on.
240
+ *
241
+ * @param[in] index The index of the buffer to look up (0-based, same
242
+ * indexing as memory_planned_buffer_size()).
243
+ * @returns The Device on success, or an error on failure.
244
+ */
245
+ Result<etensor::Device> memory_planned_buffer_device(size_t index) const;
246
+
233
247
  /**
234
248
  * Check to see if a backend is used in this method.
235
249
  *
@@ -12,6 +12,7 @@
12
12
  #include <cstdint>
13
13
  #include <stdio.h>
14
14
 
15
+ #include <c10/util/safe_numerics.h>
15
16
  #include <executorch/runtime/core/memory_allocator.h>
16
17
  #include <executorch/runtime/platform/log.h>
17
18
  #include <executorch/runtime/platform/platform.h>
@@ -46,8 +47,17 @@ public:
46
47
  return nullptr;
47
48
  }
48
49
 
49
- // Allocate enough memory for the node, the data and the alignment bump.
50
- size_t alloc_size = sizeof(AllocationNode) + size + alignment;
50
+ // Check for overflow before computing total allocation size.
51
+ // Allocate enough for the node, data, and alignment bump (at most
52
+ // alignment - 1 extra bytes to align the data pointer).
53
+ size_t alloc_size = 0;
54
+ if (c10::add_overflows(sizeof(AllocationNode), size, &alloc_size) ||
55
+ c10::add_overflows(alloc_size, alignment - 1, &alloc_size)) {
56
+ ET_LOG(Error, "Allocation size overflow: size %zu, alignment %zu", size,
57
+ alignment);
58
+ return nullptr;
59
+ }
60
+
51
61
  void *node_memory = runtime::pal_allocate(alloc_size);
52
62
 
53
63
  // If allocation failed, log message and return nullptr.
@@ -21,6 +21,7 @@
21
21
  #include <executorch/runtime/executor/method.h>
22
22
  #include <executorch/runtime/executor/method_meta.h>
23
23
  #include <executorch/runtime/executor/pte_data_map.h>
24
+ #include <executorch/runtime/kernel/operator_registry.h>
24
25
  #include <executorch/runtime/platform/compiler.h>
25
26
 
26
27
  // Forward declare flatbuffer types. This is a public header and must not
@@ -148,7 +149,8 @@ public:
148
149
  load_method(const char *method_name, MemoryManager *memory_manager,
149
150
  EventTracer *event_tracer = nullptr,
150
151
  const NamedDataMap *named_data_map = nullptr,
151
- const LoadBackendOptionsMap *backend_options = nullptr) const;
152
+ const LoadBackendOptionsMap *backend_options = nullptr,
153
+ Span<const Kernel> kernel_registry = {}) const;
152
154
 
153
155
  /**
154
156
  * Gathers metadata for the named method.
@@ -92,8 +92,12 @@ parseListOptionalType(const flatbuffers::Vector<int32_t> *value_indices,
92
92
  ET_CHECK_OR_RETURN_ERROR(
93
93
  index >= 0 && static_cast<size_t>(index) < values_len, InvalidProgram,
94
94
  "Invalid value index %" PRId32 " for ListOptional", index);
95
+ auto optional_result = values[index].tryToOptional<T>();
96
+ if (!optional_result.ok()) {
97
+ return optional_result.error();
98
+ }
95
99
  new (&optional_tensor_list[output_idx])
96
- std::optional<T>(values[index].toOptional<T>());
100
+ std::optional<T>(std::move(optional_result.get()));
97
101
  evalp_list[output_idx] = &values[static_cast<size_t>(index)];
98
102
  }
99
103
  output_idx++;
@@ -216,6 +216,15 @@ bool registry_has_op_function(const char *name,
216
216
  get_op_function_from_registry(const char *name,
217
217
  Span<const TensorMeta> meta_list = {});
218
218
 
219
+ /**
220
+ * Returns the operator with a given name and TensorMeta list from the provided
221
+ * kernel list instead of the global registry.
222
+ */
223
+ ::executorch::runtime::Result<OpFunction>
224
+ get_op_function_from_registry(const char *name,
225
+ Span<const TensorMeta> meta_list,
226
+ Span<const Kernel> kernel_list);
227
+
219
228
  /**
220
229
  * Returns all registered kernels.
221
230
  */