|
| 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 | +#include <executorch/extension/module/module.h> |
| 10 | + |
| 11 | +#include <executorch/extension/data_loader/mmap_data_loader.h> |
| 12 | +#include <executorch/extension/memory_allocator/malloc_memory_allocator.h> |
| 13 | +#include <executorch/runtime/platform/runtime.h> |
| 14 | + |
| 15 | +/** |
| 16 | + * Unwrap a Result to obtain its value (direct object, not a pointer). |
| 17 | + * If the Result contains an error, propagate the error via trivial function |
| 18 | + * return. The macro wraps the object into a unique_ptr. |
| 19 | + * |
| 20 | + * Note: A function using ET_UNWRAP_UNIQUE should itself return a Result or |
| 21 | + * Error. |
| 22 | + * |
| 23 | + * @param[in] result__ Expression yielding the result to unwrap. |
| 24 | + */ |
| 25 | +#define ET_UNWRAP_UNIQUE(result__) \ |
| 26 | + ({ \ |
| 27 | + auto et_result__ = (result__); \ |
| 28 | + if (!et_result__.ok()) { \ |
| 29 | + return et_result__.error(); \ |
| 30 | + } \ |
| 31 | + std::make_unique<std::remove_reference_t<decltype(*et_result__)>>( \ |
| 32 | + std::move(*et_result__)); \ |
| 33 | + }) |
| 34 | + |
| 35 | +namespace torch::executor { |
| 36 | + |
| 37 | +Module::Module( |
| 38 | + const std::string& filePath, |
| 39 | + const Module::MlockConfig mlockConfig) |
| 40 | + : filePath_(filePath), |
| 41 | + mlockConfig_(mlockConfig), |
| 42 | + memoryAllocator_(std::make_unique<util::MallocMemoryAllocator>()) { |
| 43 | + runtime_init(); |
| 44 | +} |
| 45 | + |
| 46 | +Module::Module( |
| 47 | + std::unique_ptr<DataLoader> dataLoader, |
| 48 | + std::unique_ptr<MemoryAllocator> memoryAllocator, |
| 49 | + std::unique_ptr<EventTracer> eventTracer) |
| 50 | + : dataLoader_(std::move(dataLoader)), |
| 51 | + memoryAllocator_( |
| 52 | + std::move(memoryAllocator) |
| 53 | + ?: std::make_unique<util::MallocMemoryAllocator>()), |
| 54 | + eventTracer_(std::move(eventTracer)) { |
| 55 | + runtime_init(); |
| 56 | +} |
| 57 | + |
| 58 | +Error Module::load(const Program::Verification verification) { |
| 59 | + if (!isLoaded()) { |
| 60 | + if (!dataLoader_) { |
| 61 | + dataLoader_ = ET_UNWRAP_UNIQUE( |
| 62 | + util::MmapDataLoader::from(filePath_.c_str(), [this] { |
| 63 | + switch (mlockConfig_) { |
| 64 | + case MlockConfig::NoMlock: |
| 65 | + return util::MmapDataLoader::MlockConfig::NoMlock; |
| 66 | + case MlockConfig::UseMlock: |
| 67 | + return util::MmapDataLoader::MlockConfig::UseMlock; |
| 68 | + case MlockConfig::UseMlockIgnoreErrors: |
| 69 | + return util::MmapDataLoader::MlockConfig::UseMlockIgnoreErrors; |
| 70 | + } |
| 71 | + ET_ASSERT_UNREACHABLE(); |
| 72 | + }())); |
| 73 | + }; |
| 74 | + program_ = ET_UNWRAP_UNIQUE(Program::load(dataLoader_.get(), verification)); |
| 75 | + } |
| 76 | + return Error::Ok; |
| 77 | +} |
| 78 | + |
| 79 | +bool Module::isLoaded() const { |
| 80 | + return program_ != nullptr; |
| 81 | +} |
| 82 | + |
| 83 | +Result<std::vector<std::string>> Module::methodNames() { |
| 84 | + ET_CHECK_OK_OR_RETURN_ERROR(load()); |
| 85 | + const auto methodCount = program_->num_methods(); |
| 86 | + std::vector<std::string> result; |
| 87 | + result.reserve(methodCount); |
| 88 | + |
| 89 | + for (auto index = 0; index < methodCount; ++index) { |
| 90 | + result.emplace_back(program_->get_method_name(index).get()); |
| 91 | + } |
| 92 | + return result; |
| 93 | +} |
| 94 | + |
| 95 | +Error Module::loadMethod(const std::string& methodName) { |
| 96 | + if (!isMethodLoaded(methodName)) { |
| 97 | + ET_CHECK_OK_OR_RETURN_ERROR(load()); |
| 98 | + |
| 99 | + MethodHolder methodHolder; |
| 100 | + const auto methodMetadata = |
| 101 | + ET_UNWRAP(program_->method_meta(methodName.c_str())); |
| 102 | + const auto plannedBuffersCount = |
| 103 | + methodMetadata.num_memory_planned_buffers(); |
| 104 | + methodHolder.plannedBuffers.reserve(plannedBuffersCount); |
| 105 | + methodHolder.plannedSpans.reserve(plannedBuffersCount); |
| 106 | + |
| 107 | + for (auto index = 0; index < plannedBuffersCount; ++index) { |
| 108 | + const auto bufferSize = |
| 109 | + methodMetadata.memory_planned_buffer_size(index).get(); |
| 110 | + methodHolder.plannedBuffers.emplace_back(bufferSize); |
| 111 | + methodHolder.plannedSpans.emplace_back( |
| 112 | + methodHolder.plannedBuffers.back().data(), bufferSize); |
| 113 | + } |
| 114 | + methodHolder.plannedMemory = std::make_unique<HierarchicalAllocator>(Span( |
| 115 | + methodHolder.plannedSpans.data(), methodHolder.plannedSpans.size())); |
| 116 | + methodHolder.memoryManager = std::make_unique<MemoryManager>( |
| 117 | + memoryAllocator_.get(), methodHolder.plannedMemory.get()); |
| 118 | + methodHolder.method = ET_UNWRAP_UNIQUE(program_->load_method( |
| 119 | + methodName.c_str(), |
| 120 | + methodHolder.memoryManager.get(), |
| 121 | + eventTracer_.get())); |
| 122 | + methods_.emplace(methodName, std::move(methodHolder)); |
| 123 | + } |
| 124 | + return Error::Ok; |
| 125 | +} |
| 126 | + |
| 127 | +bool Module::isMethodLoaded(const std::string& methodName) const { |
| 128 | + return methods_.count(methodName); |
| 129 | +} |
| 130 | + |
| 131 | +Result<MethodMeta> Module::methodMeta(const std::string& methodName) { |
| 132 | + ET_CHECK_OK_OR_RETURN_ERROR(loadMethod(methodName)); |
| 133 | + return methods_.at(methodName).method->method_meta(); |
| 134 | +} |
| 135 | + |
| 136 | +Result<std::vector<EValue>> Module::execute( |
| 137 | + const std::string& methodName, |
| 138 | + const std::vector<EValue>& input) { |
| 139 | + ET_CHECK_OK_OR_RETURN_ERROR(loadMethod(methodName)); |
| 140 | + auto& method = methods_.at(methodName).method; |
| 141 | + |
| 142 | + for (auto index = 0; index < input.size(); ++index) { |
| 143 | + ET_CHECK_OK_OR_RETURN_ERROR(method->set_input(input[index], index)); |
| 144 | + } |
| 145 | + ET_CHECK_OK_OR_RETURN_ERROR(method->execute()); |
| 146 | + |
| 147 | + const auto outputsSize = method->outputs_size(); |
| 148 | + std::vector<EValue> outputs(outputsSize); |
| 149 | + ET_CHECK_OK_OR_RETURN_ERROR(method->get_outputs(outputs.data(), outputsSize)); |
| 150 | + |
| 151 | + return outputs; |
| 152 | +} |
| 153 | + |
| 154 | +} // namespace torch::executor |
0 commit comments