diff --git a/extension/module/module.cpp b/extension/module/module.cpp index f9f1d6be0f7..725fe707bb7 100644 --- a/extension/module/module.cpp +++ b/extension/module/module.cpp @@ -33,42 +33,26 @@ std::move(*et_result__)); \ }) -using ::exec_aten::Tensor; -using ::executorch::extension::FileDataLoader; -using ::executorch::extension::MallocMemoryAllocator; -using ::executorch::extension::MmapDataLoader; -using ::executorch::runtime::DataLoader; -using ::executorch::runtime::Error; -using ::executorch::runtime::EValue; -using ::executorch::runtime::EventTracer; -using ::executorch::runtime::HierarchicalAllocator; -using ::executorch::runtime::MemoryAllocator; -using ::executorch::runtime::MemoryManager; -using ::executorch::runtime::MethodMeta; -using ::executorch::runtime::Program; -using ::executorch::runtime::Result; -using ::executorch::runtime::Span; - namespace executorch { namespace extension { Module::Module( const std::string& file_path, - const Module::LoadMode load_mode, - std::unique_ptr event_tracer) + const LoadMode load_mode, + std::unique_ptr event_tracer) : file_path_(file_path), load_mode_(load_mode), memory_allocator_(std::make_unique()), temp_allocator_(std::make_unique()), event_tracer_(std::move(event_tracer)) { - ::executorch::runtime::runtime_init(); + runtime::runtime_init(); } Module::Module( - std::unique_ptr data_loader, - std::unique_ptr memory_allocator, - std::unique_ptr temp_allocator, - std::unique_ptr event_tracer) + std::unique_ptr data_loader, + std::unique_ptr memory_allocator, + std::unique_ptr temp_allocator, + std::unique_ptr event_tracer) : data_loader_(std::move(data_loader)), memory_allocator_( memory_allocator ? std::move(memory_allocator) @@ -77,14 +61,14 @@ Module::Module( temp_allocator ? std::move(temp_allocator) : std::make_unique()), event_tracer_(std::move(event_tracer)) { - ::executorch::runtime::runtime_init(); + runtime::runtime_init(); } Module::Module( - std::shared_ptr program, - std::unique_ptr memory_allocator, - std::unique_ptr temp_allocator, - std::unique_ptr event_tracer) + std::shared_ptr program, + std::unique_ptr memory_allocator, + std::unique_ptr temp_allocator, + std::unique_ptr event_tracer) : program_(std::move(program)), memory_allocator_( memory_allocator ? std::move(memory_allocator) @@ -93,10 +77,10 @@ Module::Module( temp_allocator ? std::move(temp_allocator) : std::make_unique()), event_tracer_(std::move(event_tracer)) { - ::executorch::runtime::runtime_init(); + runtime::runtime_init(); } -Error Module::load(const Program::Verification verification) { +runtime::Error Module::load(const runtime::Program::Verification verification) { if (!is_loaded()) { if (!data_loader_) { switch (load_mode_) { @@ -119,15 +103,15 @@ Error Module::load(const Program::Verification verification) { break; } }; - auto program = - ET_UNWRAP_UNIQUE(Program::load(data_loader_.get(), verification)); - program_ = std::shared_ptr( - program.release(), [](Program* pointer) { delete pointer; }); + auto program = ET_UNWRAP_UNIQUE( + runtime::Program::load(data_loader_.get(), verification)); + program_ = std::shared_ptr( + program.release(), [](runtime::Program* pointer) { delete pointer; }); } - return Error::Ok; + return runtime::Error::Ok; } -Result> Module::method_names() { +runtime::Result> Module::method_names() { ET_CHECK_OK_OR_RETURN_ERROR(load()); const auto method_count = program_->num_methods(); std::unordered_set result; @@ -139,7 +123,7 @@ Result> Module::method_names() { return result; } -Error Module::load_method(const std::string& method_name) { +runtime::Error Module::load_method(const std::string& method_name) { if (!is_method_loaded(method_name)) { ET_CHECK_OK_OR_RETURN_ERROR(load()); @@ -158,10 +142,11 @@ Error Module::load_method(const std::string& method_name) { method_holder.planned_spans.emplace_back( method_holder.planned_buffers.back().data(), buffer_size); } - method_holder.planned_memory = std::make_unique(Span( - method_holder.planned_spans.data(), - method_holder.planned_spans.size())); - method_holder.memory_manager = std::make_unique( + method_holder.planned_memory = + std::make_unique(runtime::Span( + method_holder.planned_spans.data(), + method_holder.planned_spans.size())); + method_holder.memory_manager = std::make_unique( memory_allocator_.get(), method_holder.planned_memory.get(), temp_allocator_.get()); @@ -171,33 +156,36 @@ Error Module::load_method(const std::string& method_name) { event_tracer_.get())); methods_.emplace(method_name, std::move(method_holder)); } - return Error::Ok; + return runtime::Error::Ok; } -Result Module::method_meta(const std::string& method_name) { +runtime::Result Module::method_meta( + const std::string& method_name) { ET_CHECK_OK_OR_RETURN_ERROR(load_method(method_name)); return methods_.at(method_name).method->method_meta(); } -Result> Module::execute( +runtime::Result> Module::execute( const std::string& method_name, - const std::vector& input) { + const std::vector& input) { ET_CHECK_OK_OR_RETURN_ERROR(load_method(method_name)); auto& method = methods_.at(method_name).method; ET_CHECK_OK_OR_RETURN_ERROR(method->set_inputs( - exec_aten::ArrayRef(input.data(), input.size()))); + exec_aten::ArrayRef(input.data(), input.size()))); ET_CHECK_OK_OR_RETURN_ERROR(method->execute()); const auto outputs_size = method->outputs_size(); - std::vector outputs(outputs_size); + std::vector outputs(outputs_size); ET_CHECK_OK_OR_RETURN_ERROR( method->get_outputs(outputs.data(), outputs_size)); return outputs; } -Error Module::set_output_data_ptr(EValue output_value, size_t output_index) { +runtime::Error Module::set_output_data_ptr( + runtime::EValue output_value, + size_t output_index) { ET_CHECK_OK_OR_RETURN_ERROR(load_method("forward")); auto& output_tensor = output_value.toTensor(); auto& method = methods_.at("forward").method; diff --git a/extension/module/module.h b/extension/module/module.h index 8ae7e436556..052489fb331 100644 --- a/extension/module/module.h +++ b/extension/module/module.h @@ -48,8 +48,7 @@ class Module final { explicit Module( const std::string& file_path, const LoadMode load_mode = LoadMode::MmapUseMlock, - std::unique_ptr<::executorch::runtime::EventTracer> event_tracer = - nullptr); + std::unique_ptr event_tracer = nullptr); /** * Constructs an instance with the provided data loader and memory allocator. @@ -61,13 +60,10 @@ class Module final { * @param[in] event_tracer A EventTracer used for tracking and logging events. */ explicit Module( - std::unique_ptr<::executorch::runtime::DataLoader> data_loader, - std::unique_ptr<::executorch::runtime::MemoryAllocator> memory_allocator = - nullptr, - std::unique_ptr<::executorch::runtime::MemoryAllocator> temp_allocator = - nullptr, - std::unique_ptr<::executorch::runtime::EventTracer> event_tracer = - nullptr); + std::unique_ptr data_loader, + std::unique_ptr memory_allocator = nullptr, + std::unique_ptr temp_allocator = nullptr, + std::unique_ptr event_tracer = nullptr); /** * Constructs an instance using an existing shared program. @@ -80,13 +76,10 @@ class Module final { * @param[in] event_tracer A EventTracer used for tracking and logging events. */ explicit Module( - std::shared_ptr<::executorch::runtime::Program> program, - std::unique_ptr<::executorch::runtime::MemoryAllocator> memory_allocator = - nullptr, - std::unique_ptr<::executorch::runtime::MemoryAllocator> temp_allocator = - nullptr, - std::unique_ptr<::executorch::runtime::EventTracer> event_tracer = - nullptr); + std::shared_ptr program, + std::unique_ptr memory_allocator = nullptr, + std::unique_ptr temp_allocator = nullptr, + std::unique_ptr event_tracer = nullptr); Module(const Module&) = delete; Module& operator=(const Module&) = delete; @@ -102,9 +95,9 @@ class Module final { * @returns An Error to indicate success or failure of the loading process. */ ET_NODISCARD - ::executorch::runtime::Error load( - const ::executorch::runtime::Program::Verification verification = - ::executorch::runtime::Program::Verification::Minimal); + runtime::Error load( + const runtime::Program::Verification verification = + runtime::Program::Verification::Minimal); /** * Checks if the program is loaded. @@ -121,7 +114,7 @@ class Module final { * * @returns Shared pointer to the program or nullptr if it's not yet loaded. */ - inline std::shared_ptr<::executorch::runtime::Program> program() const { + inline std::shared_ptr program() const { return program_; } @@ -132,7 +125,7 @@ class Module final { * @returns A set of strings containing the names of the methods, or an error * if the program or method failed to load. */ - ::executorch::runtime::Result> method_names(); + runtime::Result> method_names(); /** * Load a specific method from the program and set up memory management if @@ -143,7 +136,7 @@ class Module final { * @returns An Error to indicate success or failure. */ ET_NODISCARD - ::executorch::runtime::Error load_method(const std::string& method_name); + runtime::Error load_method(const std::string& method_name); /** * Checks if a specific method is loaded. @@ -166,7 +159,7 @@ class Module final { * @returns A method metadata, or an error if the program or method failed to * load. */ - ::executorch::runtime::Result<::executorch::runtime::MethodMeta> method_meta( + runtime::Result method_meta( const std::string& method_name); /** @@ -180,10 +173,9 @@ class Module final { * from the method or an error to indicate failure. */ ET_NODISCARD - ::executorch::runtime::Result> - execute( + runtime::Result> execute( const std::string& method_name, - const std::vector<::executorch::runtime::EValue>& input); + const std::vector& input); /** * Execute a specific method with a single input value. @@ -195,13 +187,10 @@ class Module final { * @returns A Result object containing either a vector of output values * from the method or an error to indicate failure. */ - ET_NODISCARD inline ::executorch::runtime::Result< - std::vector<::executorch::runtime::EValue>> - execute( + ET_NODISCARD inline runtime::Result> execute( const std::string& method_name, - const ::executorch::runtime::EValue& input) { - return execute( - method_name, std::vector<::executorch::runtime::EValue>{input}); + const runtime::EValue& input) { + return execute(method_name, std::vector{input}); } /** @@ -213,10 +202,9 @@ class Module final { * @returns A Result object containing either a vector of output values * from the method or an error to indicate failure. */ - ET_NODISCARD inline ::executorch::runtime::Result< - std::vector<::executorch::runtime::EValue>> - execute(const std::string& method_name) { - return execute(method_name, std::vector<::executorch::runtime::EValue>{}); + ET_NODISCARD inline runtime::Result> execute( + const std::string& method_name) { + return execute(method_name, std::vector{}); } /** @@ -229,13 +217,12 @@ class Module final { * @returns A Result object containing either the first output value from the * method or an error to indicate failure. */ - ET_NODISCARD inline ::executorch::runtime::Result< - ::executorch::runtime::EValue> - get(const std::string& method_name, - const std::vector<::executorch::runtime::EValue>& input) { + ET_NODISCARD inline runtime::Result get( + const std::string& method_name, + const std::vector& input) { auto result = ET_UNWRAP(execute(method_name, input)); if (result.empty()) { - return ::executorch::runtime::Error::InvalidArgument; + return runtime::Error::InvalidArgument; } return result[0]; } @@ -250,11 +237,10 @@ class Module final { * @returns A Result object containing either the first output value from the * method or an error to indicate failure. */ - ET_NODISCARD inline ::executorch::runtime::Result< - ::executorch::runtime::EValue> - get(const std::string& method_name, - const ::executorch::runtime::EValue& input) { - return get(method_name, std::vector<::executorch::runtime::EValue>{input}); + ET_NODISCARD inline runtime::Result get( + const std::string& method_name, + const runtime::EValue& input) { + return get(method_name, std::vector{input}); } /** @@ -266,10 +252,9 @@ class Module final { * @returns A Result object containing either the first output value from the * method or an error to indicate failure. */ - ET_NODISCARD inline ::executorch::runtime::Result< - ::executorch::runtime::EValue> - get(const std::string& method_name) { - return get(method_name, std::vector<::executorch::runtime::EValue>{}); + ET_NODISCARD inline runtime::Result get( + const std::string& method_name) { + return get(method_name, std::vector{}); } /** @@ -281,9 +266,8 @@ class Module final { * @returns A Result object containing either a vector of output values * from the 'forward' method or an error to indicate failure. */ - ET_NODISCARD inline ::executorch::runtime::Result< - std::vector<::executorch::runtime::EValue>> - forward(const std::vector<::executorch::runtime::EValue>& input) { + ET_NODISCARD inline runtime::Result> forward( + const std::vector& input) { return execute("forward", input); } @@ -296,10 +280,9 @@ class Module final { * @returns A Result object containing either a vector of output values * from the 'forward' method or an error to indicate failure. */ - ET_NODISCARD inline ::executorch::runtime::Result< - std::vector<::executorch::runtime::EValue>> - forward(const ::executorch::runtime::EValue& input) { - return forward(std::vector<::executorch::runtime::EValue>{input}); + ET_NODISCARD inline runtime::Result> forward( + const runtime::EValue& input) { + return forward(std::vector{input}); } /** @@ -309,10 +292,8 @@ class Module final { * @returns A Result object containing either a vector of output values * from the 'forward' method or an error to indicate failure. */ - ET_NODISCARD inline ::executorch::runtime::Result< - std::vector<::executorch::runtime::EValue>> - forward() { - return forward(std::vector<::executorch::runtime::EValue>{}); + ET_NODISCARD inline runtime::Result> forward() { + return forward(std::vector{}); } /** @@ -323,7 +304,7 @@ class Module final { * @returns A pointer to the EventTracer instance. Returns nullptr if no * EventTracer is set. */ - inline ::executorch::runtime::EventTracer* event_tracer() const { + inline runtime::EventTracer* event_tracer() const { return event_tracer_.get(); } @@ -335,28 +316,27 @@ class Module final { * * @returns An Error to indicate success or failure of the loading process. */ - ::executorch::runtime::Error set_output_data_ptr( - ::executorch::runtime::EValue output_value, + runtime::Error set_output_data_ptr( + runtime::EValue output_value, size_t output_index); private: struct MethodHolder { std::vector> planned_buffers; - std::vector<::executorch::runtime::Span> planned_spans; - std::unique_ptr<::executorch::runtime::HierarchicalAllocator> - planned_memory; - std::unique_ptr<::executorch::runtime::MemoryManager> memory_manager; - std::unique_ptr<::executorch::runtime::Method> method; + std::vector> planned_spans; + std::unique_ptr planned_memory; + std::unique_ptr memory_manager; + std::unique_ptr method; }; private: std::string file_path_; LoadMode load_mode_{LoadMode::MmapUseMlock}; - std::shared_ptr<::executorch::runtime::Program> program_; - std::unique_ptr<::executorch::runtime::DataLoader> data_loader_; - std::unique_ptr<::executorch::runtime::MemoryAllocator> memory_allocator_; - std::unique_ptr<::executorch::runtime::MemoryAllocator> temp_allocator_; - std::unique_ptr<::executorch::runtime::EventTracer> event_tracer_; + std::shared_ptr program_; + std::unique_ptr data_loader_; + std::unique_ptr memory_allocator_; + std::unique_ptr temp_allocator_; + std::unique_ptr event_tracer_; std::unordered_map methods_; friend class ExecuTorchJni;