Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/react-native-executorch/android/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,7 @@ set(INCLUDE_DIR "${CMAKE_SOURCE_DIR}/../third-party/include")

# Treat third-party headers as system headers to suppress deprecation warnings
include_directories(SYSTEM "${INCLUDE_DIR}")
include_directories(SYSTEM "${INCLUDE_DIR}/cpuinfo")
include_directories(SYSTEM "${INCLUDE_DIR}/pthreadpool")

add_subdirectory("${ANDROID_CPP_DIR}")
Binary file modified packages/react-native-executorch/android/libs/classes.jar
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ target_include_directories(rntests_core PUBLIC
${RNEXECUTORCH_DIR}
${COMMON_DIR}
${PACKAGE_ROOT}/third-party/include
${PACKAGE_ROOT}/third-party/include/cpuinfo
${PACKAGE_ROOT}/third-party/include/pthreadpool
${REACT_NATIVE_DIR}/ReactCommon
${REACT_NATIVE_DIR}/ReactCommon/jsi
${REACT_NATIVE_DIR}/ReactCommon/callinvoker
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ Pod::Spec.new do |s|
phonemis_binaries_path = File.expand_path('$(PODS_TARGET_SRCROOT)/third-party/ios/libs/phonemis', __dir__)

s.user_target_xcconfig = {
"HEADER_SEARCH_PATHS" => "$(PODS_TARGET_SRCROOT)/third-party/include",
"HEADER_SEARCH_PATHS" =>
'"$(PODS_TARGET_SRCROOT)/third-party/include" '+
'"$(PODS_TARGET_SRCROOT)/third-party/include/cpuinfo" '+
'"$(PODS_TARGET_SRCROOT)/third-party/include/pthreadpool"',

"OTHER_LDFLAGS[sdk=iphoneos*]" => [
'$(inherited)',
Expand All @@ -45,6 +48,8 @@ Pod::Spec.new do |s|
'"$(PODS_TARGET_SRCROOT)/ios" '+
'"$(PODS_TARGET_SRCROOT)/third-party/include/executorch/extension/llm/tokenizers/include" '+
'"$(PODS_TARGET_SRCROOT)/third-party/include" '+
'"$(PODS_TARGET_SRCROOT)/third-party/include/cpuinfo" '+
'"$(PODS_TARGET_SRCROOT)/third-party/include/pthreadpool" '+
'"$(PODS_TARGET_SRCROOT)/common" ',
"CLANG_CXX_LANGUAGE_STANDARD" => "c++20",
'EXCLUDED_ARCHS[sdk=iphonesimulator*]' => 'x86_64',
Expand Down
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#pragma once

#include <c10/util/safe_numerics.h>
#include <cstring>
#include <executorch/runtime/core/data_loader.h>
#include <executorch/runtime/core/error.h>
Expand All @@ -33,7 +34,9 @@ class BufferDataLoader final : public executorch::runtime::DataLoader {
executorch::runtime::Result<executorch::runtime::FreeableBuffer>
load(size_t offset, size_t size,
ET_UNUSED const DataLoader::SegmentInfo &segment_info) const override {
ET_CHECK_OR_RETURN_ERROR(offset + size <= size_, InvalidArgument,
size_t total_size;
bool overflow = c10::add_overflows(offset, size, &total_size);
ET_CHECK_OR_RETURN_ERROR(!overflow && total_size <= size_, InvalidArgument,
"offset %zu + size %zu > size_ %zu", offset, size,
size_);
return executorch::runtime::FreeableBuffer(data_ + offset, size,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,4 @@ class HFTokenizer : public Tokenizer {
Model::Ptr _model;
};

} // namespace tokenizers
} // namespace tokenizers
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,4 @@ class ModelConfig {
Model::Ptr create() const;
};

} // namespace tokenizers
} // namespace tokenizers
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,4 @@ class TruncationConfig {
TruncationParams params;
};

} // namespace tokenizers
} // namespace tokenizers
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ using ET_RUNTIME_NAMESPACE::Method;
using ET_RUNTIME_NAMESPACE::MethodMeta;
using ET_RUNTIME_NAMESPACE::NamedDataMap;
using ET_RUNTIME_NAMESPACE::Program;
using runtime::LoadBackendOptionsMap;

class ExecuTorchJni;

Expand Down Expand Up @@ -59,12 +60,28 @@ class Module {
* @param[in] file_path The path to the ExecuTorch program file to load.
* @param[in] load_mode The loading mode to use.
* @param[in] event_tracer A EventTracer used for tracking and logging events.
* @param[in] share_memory_arenas When true, all methods loaded by this Module
* share the same memory-planned buffers for mem_id=1 (activation memory)
* and mem_id=2 (shared mutable buffer memory), sized to the max
* across all methods. mem_id>2 indicates a custom memory plan, and those
* receive fresh memory buffers. share_memory_arenas is required for models
* exported with share_mutable_buffers=true, where methods access shared
* mutable state (e.g., set/get state). When enabled, outputs from one method
* may be invalidated by executing another method, since their output tensors
* can alias the same underlying buffer. Consume or copy outputs before
* calling execute again. NOTE: This class is not thread-safe and performs
* no internal synchronization. Calling execute concurrently on the same
* Module instance from multiple threads is unsafe, regardless of whether
* share_memory_arenas is true or false. When share_memory_arenas is true,
* methods may overwrite each other's data in the shared memory arenas,
* increasing aliasing and the risk of unintended overwrites.
*/
explicit Module(
const std::string &file_path, const LoadMode load_mode = LoadMode::File,
std::unique_ptr<runtime::EventTracer> event_tracer = nullptr,
std::unique_ptr<runtime::MemoryAllocator> memory_allocator = nullptr,
std::unique_ptr<runtime::MemoryAllocator> temp_allocator = nullptr);
std::unique_ptr<runtime::MemoryAllocator> temp_allocator = nullptr,
bool share_memory_arenas = false);

/**
* Constructs an instance by loading a program from a file with specified
Expand All @@ -74,13 +91,16 @@ class Module {
* @param[in] data_map_path The path to a .ptd file.
* @param[in] load_mode The loading mode to use.
* @param[in] event_tracer A EventTracer used for tracking and logging events.
* @param[in] share_memory_arenas When true, all methods loaded by this Module
* share a single set of memory-planned buffers.
*/
explicit Module(
const std::string &file_path, const std::string &data_map_path,
const LoadMode load_mode = LoadMode::File,
std::unique_ptr<runtime::EventTracer> event_tracer = nullptr,
std::unique_ptr<runtime::MemoryAllocator> memory_allocator = nullptr,
std::unique_ptr<runtime::MemoryAllocator> temp_allocator = nullptr);
std::unique_ptr<runtime::MemoryAllocator> temp_allocator = nullptr,
bool share_memory_arenas = false);

/**
* Constructs an instance by loading a program from a file with specified
Expand All @@ -90,13 +110,16 @@ class Module {
* @param[in] data_files The path to one or more .ptd file/s.
* @param[in] load_mode The loading mode to use.
* @param[in] event_tracer A EventTracer used for tracking and logging events.
* @param[in] share_memory_arenas When true, all methods loaded by this Module
* share a single set of memory-planned buffers.
*/
explicit Module(
const std::string &file_path, std::vector<std::string> data_files,
const LoadMode load_mode = LoadMode::File,
std::unique_ptr<runtime::EventTracer> event_tracer = nullptr,
std::unique_ptr<runtime::MemoryAllocator> memory_allocator = nullptr,
std::unique_ptr<runtime::MemoryAllocator> temp_allocator = nullptr);
std::unique_ptr<runtime::MemoryAllocator> temp_allocator = nullptr,
bool share_memory_arenas = false);

/**
* Constructs an instance with the provided data loader and memory allocator.
Expand All @@ -107,13 +130,16 @@ class Module {
* temporary data during kernel or delegate execution.
* @param[in] event_tracer A EventTracer used for tracking and logging events.
* @param[in] data_map_loader A DataLoader used for loading external weights.
* @param[in] share_memory_arenas When true, all methods loaded by this Module
* share a single set of memory-planned buffers.
*/
explicit Module(
std::unique_ptr<runtime::DataLoader> data_loader,
std::unique_ptr<runtime::MemoryAllocator> memory_allocator = nullptr,
std::unique_ptr<runtime::MemoryAllocator> temp_allocator = nullptr,
std::unique_ptr<runtime::EventTracer> event_tracer = nullptr,
std::unique_ptr<runtime::DataLoader> data_map_loader = nullptr);
std::unique_ptr<runtime::DataLoader> data_map_loader = nullptr,
bool share_memory_arenas = false);

/**
* Constructs an instance using an existing shared program.
Expand All @@ -125,13 +151,16 @@ class Module {
* temporary data.
* @param[in] event_tracer A EventTracer used for tracking and logging events.
* @param[in] data_map_loader A DataLoader used for loading external weights.
* @param[in] share_memory_arenas When true, all methods loaded by this Module
* share a single set of memory-planned buffers.
*/
explicit Module(
std::shared_ptr<Program> program,
std::unique_ptr<runtime::MemoryAllocator> memory_allocator = nullptr,
std::unique_ptr<runtime::MemoryAllocator> temp_allocator = nullptr,
std::unique_ptr<runtime::EventTracer> event_tracer = nullptr,
std::unique_ptr<runtime::DataLoader> data_map_loader = nullptr);
std::unique_ptr<runtime::DataLoader> data_map_loader = nullptr,
bool share_memory_arenas = false);

Module(const Module &) = delete;
Module &operator=(const Module &) = delete;
Expand All @@ -150,6 +179,22 @@ class Module {
load(const Program::Verification verification =
Program::Verification::Minimal);

/**
* Loads the program with per-delegate runtime options.
*
* @param[in] backend_options A LoadBackendOptionsMap containing per-delegate
* load-time configuration options. The caller must ensure this object
* outlives any methods loaded with these options.
* @param[in] verification The type of verification to do before returning
* success.
*
* @returns An Error to indicate success or failure of the loading process.
*/
ET_NODISCARD virtual runtime::Error
load(const LoadBackendOptionsMap &backend_options,
const Program::Verification verification =
Program::Verification::Minimal);

/**
* Checks if the program is loaded.
*
Expand Down Expand Up @@ -200,12 +245,13 @@ class Module {
runtime::Error
load_method(const std::string &method_name,
runtime::HierarchicalAllocator *planned_memory = nullptr,
torch::executor::EventTracer *event_tracer = nullptr);
torch::executor::EventTracer *event_tracer = nullptr,
const LoadBackendOptionsMap *backend_options = nullptr);

ET_DEPRECATED ET_NODISCARD runtime::Error inline load_method(
const std::string &method_name,
torch::executor::EventTracer *event_tracer) {
return load_method(method_name, nullptr, event_tracer);
return load_method(method_name, nullptr, event_tracer, nullptr);
}

/**
Expand Down Expand Up @@ -247,13 +293,15 @@ class Module {
*/
ET_NODISCARD inline runtime::Error
load_forward(runtime::HierarchicalAllocator *planned_memory = nullptr,
torch::executor::EventTracer *event_tracer = nullptr) {
return load_method("forward", planned_memory, event_tracer);
torch::executor::EventTracer *event_tracer = nullptr,
const LoadBackendOptionsMap *backend_options = nullptr) {
return load_method("forward", planned_memory, event_tracer,
backend_options);
}

ET_DEPRECATED ET_NODISCARD inline runtime::Error
load_forward(torch::executor::EventTracer *event_tracer) {
return load_forward(nullptr, event_tracer);
return load_forward(nullptr, event_tracer, nullptr);
}

/**
Expand Down Expand Up @@ -612,10 +660,22 @@ class Module {
}

private:
struct MethodHolder {
struct PlannedMemory {
std::vector<std::vector<uint8_t>> planned_buffers;
std::vector<runtime::Span<uint8_t>> planned_spans;
std::unique_ptr<runtime::HierarchicalAllocator> planned_memory;
};
std::unique_ptr<PlannedMemory>
make_planned_memory(const std::vector<size_t> &buffer_sizes);
std::unique_ptr<PlannedMemory> make_planned_memory_with_shared_arenas(
const std::vector<size_t> &buffer_sizes,
std::vector<std::vector<uint8_t>> &shared_arenas);
runtime::Result<std::vector<size_t>>
get_mem_planned_buffer_sizes(const std::string &method_name);
runtime::Result<std::vector<size_t>> get_max_mem_planned_buffer_sizes();

struct MethodHolder {
std::unique_ptr<PlannedMemory> planned_memory;
std::unique_ptr<runtime::MemoryManager> memory_manager;
std::unique_ptr<Method> method;
};
Expand All @@ -631,7 +691,13 @@ class Module {
std::vector<std::unique_ptr<runtime::DataLoader>> data_map_loaders_;
std::vector<std::unique_ptr<NamedDataMap>> named_data_maps_;
std::unique_ptr<NamedDataMap> merged_data_map_;
std::vector<std::vector<uint8_t>> shared_arenas_;
ET_DEPRECATED std::vector<uint8_t> debug_buffer_;
const LoadBackendOptionsMap *backend_options_ = nullptr;
bool share_memory_arenas_;

ET_NODISCARD runtime::Error
load_internal(const Program::Verification verification);

protected:
std::unordered_map<std::string, MethodHolder> methods_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#pragma once

#include <cpuinfo/cpuinfo.h>
#include <cpuinfo.h>

namespace executorch::extension::cpuinfo {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include <memory>
#include <mutex>

#include <pthreadpool/pthreadpool.h>
#include <pthreadpool.h>

#include <executorch/runtime/core/function_ref.h>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@
*/

#pragma once
#include <executorch/runtime/backend/options.h>
#include <executorch/runtime/core/error.h>
#include <executorch/runtime/core/event_tracer.h>
#include <executorch/runtime/core/memory_allocator.h>
#include <executorch/runtime/core/named_data_map.h>
#include <executorch/runtime/core/result.h>
#include <executorch/runtime/core/span.h>

#include <cstring>

#ifdef __GNUC__
// Disable -Wdeprecated-declarations, as some builds use 'Werror'.
Expand All @@ -28,14 +34,16 @@ class BackendInitContext final {
explicit BackendInitContext(MemoryAllocator *runtime_allocator,
EventTracer *event_tracer = nullptr,
const char *method_name = nullptr,
const NamedDataMap *named_data_map = nullptr)
const NamedDataMap *named_data_map = nullptr,
Span<const BackendOption> runtime_specs = {})
: runtime_allocator_(runtime_allocator),
#ifdef ET_EVENT_TRACER_ENABLED
event_tracer_(event_tracer),
#else
event_tracer_(nullptr),
#endif
method_name_(method_name), named_data_map_(named_data_map) {
method_name_(method_name), named_data_map_(named_data_map),
runtime_specs_(runtime_specs) {
}

/** Get the runtime allocator passed from Method. It's the same runtime
Expand Down Expand Up @@ -65,11 +73,55 @@ class BackendInitContext final {
*/
const NamedDataMap *get_named_data_map() const { return named_data_map_; }

/**
* Get the runtime specs (load-time options) for this backend.
* These are per-delegate options passed at Module::load() time.
*
* @return Span of BackendOption containing the runtime specs, or empty span
* if no runtime specs were provided.
*/
Span<const BackendOption> runtime_specs() const { return runtime_specs_; }

/**
* Get a runtime spec value by key and type.
*
* @tparam T The expected type (bool, int, or const char*)
* @param key The option key to look up.
* @return Result containing the value if found and type matches,
* Error::NotFound if key doesn't exist,
* Error::InvalidArgument if key exists but type doesn't match.
*/
template <typename T> Result<T> get_runtime_spec(const char *key) const {
static_assert(
std::is_same_v<T, bool> || std::is_same_v<T, int> ||
std::is_same_v<T, const char *>,
"get_runtime_spec<T> only supports bool, int, and const char*");

for (size_t i = 0; i < runtime_specs_.size(); ++i) {
const auto &opt = runtime_specs_[i];
if (std::strcmp(opt.key, key) == 0) {
if constexpr (std::is_same_v<T, const char *>) {
if (auto *arr = std::get_if<std::array<char, kMaxOptionValueLength>>(
&opt.value)) {
return arr->data();
}
} else {
if (auto *val = std::get_if<T>(&opt.value)) {
return *val;
}
}
return Error::InvalidArgument;
}
}
return Error::NotFound;
}

private:
MemoryAllocator *runtime_allocator_ = nullptr;
EventTracer *event_tracer_ = nullptr;
const char *method_name_ = nullptr;
const NamedDataMap *named_data_map_ = nullptr;
Span<const BackendOption> runtime_specs_;
};

} // namespace ET_RUNTIME_NAMESPACE
Expand Down
Loading
Loading