From a5673c74d0e4ea57624d1fdb52332024c440bb07 Mon Sep 17 00:00:00 2001 From: Mengwei Liu Date: Mon, 22 Apr 2024 12:01:53 -0700 Subject: [PATCH] Remove fixes_et patches Summary: All of the patches have been upstreamed to ExecuTorch in https://github.com/pytorch/executorch/pull/3201 Test Plan: Rely on CI jobs Reviewers: Subscribers: Tasks: Tags: --- scripts/fixes_et/managed_tensor.h | 101 -------------- scripts/fixes_et/module.cpp | 159 ---------------------- scripts/fixes_et/module.h | 213 ------------------------------ scripts/install_et.sh | 5 - 4 files changed, 478 deletions(-) delete mode 100644 scripts/fixes_et/managed_tensor.h delete mode 100644 scripts/fixes_et/module.cpp delete mode 100644 scripts/fixes_et/module.h diff --git a/scripts/fixes_et/managed_tensor.h b/scripts/fixes_et/managed_tensor.h deleted file mode 100644 index d63f5d374..000000000 --- a/scripts/fixes_et/managed_tensor.h +++ /dev/null @@ -1,101 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. - */ - -#include -#include -#include -#include -#include -#include - -#ifdef USE_ATEN_LIB -#include -#else -#include -#endif -#pragma once - -namespace torch { -namespace executor { - -/** - * A tensor wrapper takes ownership of all the memory of the necessary metadata - * for torch::executor::Tensor. Note that it doesn't own the data memory. - */ -class ManagedTensor { - public: - /// The type used for elements of `sizes()`. - using SizesType = exec_aten::SizesType; - /// The type used for elements of `dim_order()`. - using DimOrderType = exec_aten::DimOrderType; - /// The type used for elements of `strides()`. - using StridesType = exec_aten::StridesType; - ManagedTensor() = delete; - - explicit ManagedTensor( - void* data, - ssize_t numel, - const std::vector& sizes, - ScalarType dtype) - : dtype_(dtype), sizes_(sizes), data_ptr_(data) { -#ifdef USE_ATEN_LIB - tensor_ = torch::from_blob(data, sizes, dtype_); -#else - ssize_t dim = sizes.size(); - dim_order_.resize(dim); - strides_.resize(dim); - for (size_t i = 0; i < dim; ++i) { - dim_order_[i] = i; - } - dim_order_to_stride_nocheck( - sizes.data(), dim_order_.data(), dim, strides_.data()); - tensor_impl_ = std::make_unique( - dtype_, - dim, - sizes_.data(), - data_ptr_, - dim_order_.data(), - strides_.data(), - TensorShapeDynamism::DYNAMIC_BOUND); -#endif - } - - void resize(const std::vector& new_sizes) { - ET_CHECK_MSG( - new_sizes.size() == sizes_.size(), - "Cannot change rank of a managed tensor"); - auto err = resize_tensor( - this->get_aliasing_tensor(), - exec_aten::ArrayRef(new_sizes.data(), new_sizes.size())); - ET_CHECK(err == Error::Ok); - } - - /** - * Get the underlying Tensor object. This is assuming the copying is cheap. - */ - Tensor get_aliasing_tensor() { -#ifdef USE_ATEN_LIB - return tensor_; -#else - return Tensor(tensor_impl_.get()); -#endif - } - - private: - ScalarType dtype_; - std::unique_ptr tensor_impl_; - std::vector sizes_; - std::vector strides_; - std::vector dim_order_; - void* data_ptr_ = nullptr; -#ifdef USE_ATEN_LIB - Tensor tensor_; -#endif -}; -} // namespace executor -} // namespace torch diff --git a/scripts/fixes_et/module.cpp b/scripts/fixes_et/module.cpp deleted file mode 100644 index 2c9733d1d..000000000 --- a/scripts/fixes_et/module.cpp +++ /dev/null @@ -1,159 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. - */ - -#include - -#include -#include -#include - -/** - * Unwrap a Result to obtain its value (direct object, not a pointer). - * If the Result contains an error, propagate the error via trivial function - * return. The macro wraps the object into a unique_ptr. - * - * Note: A function using ET_UNWRAP_UNIQUE should itself return a Result or - * Error. - * - * @param[in] result__ Expression yielding the result to unwrap. - */ -#define ET_UNWRAP_UNIQUE(result__) \ - ({ \ - auto et_result__ = (result__); \ - if (!et_result__.ok()) { \ - return et_result__.error(); \ - } \ - std::make_unique>( \ - std::move(*et_result__)); \ - }) - -namespace torch::executor { - -Module::Module( - const std::string& file_path, - const Module::MlockConfig mlock_config, - std::unique_ptr event_tracer) - : file_path_(file_path), - mlock_config_(mlock_config), - memory_allocator_(std::make_unique()), - event_tracer_(std::move(event_tracer)) { - runtime_init(); -} - -Module::Module( - std::unique_ptr data_loader, - std::unique_ptr memory_allocator, - std::unique_ptr event_tracer) - : data_loader_(std::move(data_loader)), - memory_allocator_( - memory_allocator ? std::move(memory_allocator) - : std::make_unique()), - event_tracer_(std::move(event_tracer)) { - runtime_init(); -} - -Error Module::load(const Program::Verification verification) { - if (!is_loaded()) { - if (!data_loader_) { - data_loader_ = ET_UNWRAP_UNIQUE( - util::MmapDataLoader::from(file_path_.c_str(), [this] { - switch (mlock_config_) { - case MlockConfig::NoMlock: - return util::MmapDataLoader::MlockConfig::NoMlock; - case MlockConfig::UseMlock: - return util::MmapDataLoader::MlockConfig::UseMlock; - case MlockConfig::UseMlockIgnoreErrors: - return util::MmapDataLoader::MlockConfig::UseMlockIgnoreErrors; - } - ET_ASSERT_UNREACHABLE(); - }())); - }; - program_ = - ET_UNWRAP_UNIQUE(Program::load(data_loader_.get(), verification)); - } - return Error::Ok; -} - -bool Module::is_loaded() const { - return program_ != nullptr; -} - -Result> Module::method_names() { - ET_CHECK_OK_OR_RETURN_ERROR(load()); - const auto method_count = program_->num_methods(); - std::unordered_set result; - result.reserve(method_count); - - for (auto index = 0; index < method_count; ++index) { - result.emplace(program_->get_method_name(index).get()); - } - return result; -} - -Error Module::load_method(const std::string& method_name) { - if (!is_method_loaded(method_name)) { - ET_CHECK_OK_OR_RETURN_ERROR(load()); - - MethodHolder method_holder; - const auto method_metadata = - ET_UNWRAP(program_->method_meta(method_name.c_str())); - const auto planned_buffersCount = - method_metadata.num_memory_planned_buffers(); - method_holder.planned_buffers.reserve(planned_buffersCount); - method_holder.planned_spans.reserve(planned_buffersCount); - - for (auto index = 0; index < planned_buffersCount; ++index) { - const auto buffer_size = - method_metadata.memory_planned_buffer_size(index).get(); - method_holder.planned_buffers.emplace_back(buffer_size); - 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( - memory_allocator_.get(), method_holder.planned_memory.get()); - method_holder.method = ET_UNWRAP_UNIQUE(program_->load_method( - method_name.c_str(), - method_holder.memory_manager.get(), - event_tracer_.get())); - methods_.emplace(method_name, std::move(method_holder)); - } - return Error::Ok; -} - -bool Module::is_method_loaded(const std::string& method_name) const { - return methods_.count(method_name); -} - -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( - const std::string& method_name, - const std::vector& input) { - ET_CHECK_OK_OR_RETURN_ERROR(load_method(method_name)); - auto& method = methods_.at(method_name).method; - - for (auto index = 0; index < input.size(); ++index) { - ET_CHECK_OK_OR_RETURN_ERROR(method->set_input(input[index], index)); - } - ET_CHECK_OK_OR_RETURN_ERROR(method->execute()); - - const auto outputs_size = method->outputs_size(); - std::vector outputs(outputs_size); - ET_CHECK_OK_OR_RETURN_ERROR( - method->get_outputs(outputs.data(), outputs_size)); - - return outputs; -} - -} // namespace torch::executor diff --git a/scripts/fixes_et/module.h b/scripts/fixes_et/module.h deleted file mode 100644 index fb70cb084..000000000 --- a/scripts/fixes_et/module.h +++ /dev/null @@ -1,213 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. - */ - -#pragma once - -#include -#include -#include -#include -#include - -#include - -namespace torch::executor { - -/** - * A facade class for loading programs and executing methods within them. - */ -class Module final { - public: - /** - * Enum to define memory locking behavior. - */ - enum class MlockConfig { - /// Do not use memory locking. - NoMlock, - /// Use memory locking and handle errors. - UseMlock, - /// Use memory locking and ignore errors. - UseMlockIgnoreErrors, - }; - - /** - * Constructs an instance by loading a program from a file with specified - * memory locking behavior. - * - * @param[in] file_path The path to the ExecuTorch program file to load. - * @param[in] mlock_config The memory locking configuration to use. - */ - explicit Module( - const std::string& file_path, - const MlockConfig mlock_config = MlockConfig::UseMlock, - std::unique_ptr event_tracer = nullptr); - - /** - * Constructs an instance with the provided data loader and memory allocator. - * - * @param[in] data_loader A DataLoader used for loading program data. - * @param[in] memory_allocator A MemoryAllocator used for memory management. - * @param[in] event_tracer A EventTracer used for tracking and logging events. - */ - explicit Module( - std::unique_ptr data_loader, - std::unique_ptr memory_allocator = nullptr, - std::unique_ptr event_tracer = nullptr); - Module(const Module&) = delete; - Module& operator=(const Module&) = delete; - Module(Module&&) = default; - Module& operator=(Module&&) = default; - - /** - * Loads the program using the specified data loader and memory allocator. - * - * @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 - Error load( - const Program::Verification verification = - Program::Verification::Minimal); - - /** - * Checks if the program is loaded. - * - * @returns true if the program is loaded, false otherwise. - */ - bool is_loaded() const; - - /** - * Get a list of method names available in the loaded program. - * Loads the program and method if needed. - * - * @returns A set of strings containing the names of the methods, or an error - * if the program or method failed to load. - */ - Result> method_names(); - - /** - * Load a specific method from the program and set up memory management if - * needed. The loaded method is cached to reuse the next time it's executed. - * - * @param[in] method_name The name of the method to load. - * - * @returns An Error to indicate success or failure. - */ - __ET_NODISCARD - Error load_method(const std::string& method_name); - - /** - * Checks if a specific method is loaded. - * - * @param[in] method_name The name of the method to check. - * - * @returns true if the method specified by method_name is loaded, false - * otherwise. - */ - bool is_method_loaded(const std::string& method_name) const; - - /** - * Get a method metadata struct by method name. - * Loads the program and method if needed. - * - * @param[in] method_name The name of the method to get the metadata for. - * - * @returns A method metadata, or an error if the program or method failed to - * load. - */ - Result method_meta(const std::string& method_name); - - /** - * Execute a specific method with the given input and retrieve output. - * Loads the program and method before executing if needed. - * - * @param[in] method_name The name of the method to execute. - * @param[in] input A vector of input values to be passed to the method. - * - * @returns A Result object containing either a vector of output values - * from the method or an error to indicate failure. - */ - __ET_NODISCARD - Result> execute( - const std::string& method_name, - const std::vector& input); - - /** - * Execute a specific method without any input values. - * Loads the program and method before executing if needed. - * - * @param[in] method_name The name of the method to execute. - * - * @returns A Result object containing either a vector of output values - * from the method or an error to indicate failure. - */ - __ET_NODISCARD - Result> execute(const std::string& method_name) { - return execute(method_name, {}); - } - - /** - * Execute the 'forward' method with the given input and retrieve output. - * Loads the program and method before executing if needed. - * - * @param[in] input A vector of input values for the 'forward' method. - * - * @returns A Result object containing either a vector of output values - * from the 'forward' method or an error to indicate failure. - */ - __ET_NODISCARD - Result> forward(const std::vector& input) { - return execute("forward", input); - } - - /** - * Execute the 'forward' method without any input values. - * Loads the program and method before executing if needed. - * - * @returns A Result object containing either a vector of output values - * from the 'forward' method or an error to indicate failure. - */ - __ET_NODISCARD - Result> forward() { - return forward({}); - } - - /** - * Retrieves the EventTracer instance being used by the Module. - * EventTracer is used for tracking and logging events during the execution - * of methods. - * - * @returns A pointer to the EventTracer instance. Returns nullptr if no - * EventTracer is set. - */ - EventTracer* event_tracer() const { - return event_tracer_.get(); - } - - private: - struct MethodHolder { - std::vector> planned_buffers; - std::vector> planned_spans; - std::unique_ptr planned_memory; - std::unique_ptr memory_manager; - std::unique_ptr method; - }; - - private: - std::string file_path_; - MlockConfig mlock_config_{MlockConfig::NoMlock}; - std::unique_ptr data_loader_; - std::unique_ptr memory_allocator_; - std::unique_ptr event_tracer_; - std::unique_ptr program_; - std::unordered_map methods_; -}; - -} // namespace torch::executor diff --git a/scripts/install_et.sh b/scripts/install_et.sh index 75223f3af..9dd9a12e9 100755 --- a/scripts/install_et.sh +++ b/scripts/install_et.sh @@ -31,11 +31,6 @@ install_executorch() { git submodule sync git submodule update --init - echo "Applying fixes" - cp ${TORCHCHAT_ROOT}/scripts/fixes_et/module.cpp ${TORCHCHAT_ROOT}/et-build/src/executorch/extension/module/module.cpp # ET uses non-standard C++ that does not compile in GCC - cp ${TORCHCHAT_ROOT}/scripts/fixes_et/managed_tensor.h ${TORCHCHAT_ROOT}/et-build/src/executorch/extension/runner_util/managed_tensor.h # ET is missing headers for vector/memory. This causes downstream issues when building runner-et. - - echo "Building and installing python libraries" echo "Building and installing python libraries" if [ "${ENABLE_ET_PYBIND}" = false ]; then echo "Not installing pybind"