|
| 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 <cstring> |
| 10 | + |
| 11 | +#include <executorch/extension/data_loader/file_data_loader.h> |
| 12 | +#include <executorch/extension/runner_util/inputs.h> |
| 13 | +#include <executorch/runtime/executor/program.h> |
| 14 | +#include <executorch/runtime/executor/test/managed_memory_manager.h> |
| 15 | +#include <executorch/runtime/kernel/kernel_runtime_context.h> |
| 16 | +#include <executorch/runtime/kernel/operator_registry.h> |
| 17 | +#include <executorch/runtime/platform/runtime.h> |
| 18 | + |
| 19 | +#include <gtest/gtest.h> |
| 20 | + |
| 21 | +using executorch::runtime::Error; |
| 22 | +using executorch::runtime::EValue; |
| 23 | +using executorch::runtime::Kernel; |
| 24 | +using executorch::runtime::KernelRuntimeContext; |
| 25 | +using executorch::runtime::Method; |
| 26 | +using executorch::runtime::Program; |
| 27 | +using executorch::runtime::Result; |
| 28 | +using executorch::runtime::Span; |
| 29 | +using executorch::runtime::testing::ManagedMemoryManager; |
| 30 | +using torch::executor::util::FileDataLoader; |
| 31 | + |
| 32 | +constexpr size_t kDefaultNonConstMemBytes = 32 * 1024U; |
| 33 | +constexpr size_t kDefaultRuntimeMemBytes = 32 * 1024U; |
| 34 | + |
| 35 | +namespace { |
| 36 | + |
| 37 | +// aten::add.out args: [self, other, out, out] |
| 38 | +void multiply_by_two( |
| 39 | + KernelRuntimeContext& /*context*/, |
| 40 | + Span<EValue*> args) { |
| 41 | + auto& in = args[0]->toTensor(); |
| 42 | + auto& out = args[args.size() - 1]->toTensor(); |
| 43 | + for (ssize_t i = 0; i < in.numel(); ++i) { |
| 44 | + out.mutable_data_ptr<float>()[i] = in.const_data_ptr<float>()[i] * 2.0f; |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +void multiply_by_three( |
| 49 | + KernelRuntimeContext& /*context*/, |
| 50 | + Span<EValue*> args) { |
| 51 | + auto& in = args[0]->toTensor(); |
| 52 | + auto& out = args[args.size() - 1]->toTensor(); |
| 53 | + for (ssize_t i = 0; i < in.numel(); ++i) { |
| 54 | + out.mutable_data_ptr<float>()[i] = in.const_data_ptr<float>()[i] * 3.0f; |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +} // namespace |
| 59 | + |
| 60 | +class KernelRegistryTest : public ::testing::Test { |
| 61 | + protected: |
| 62 | + void SetUp() override { |
| 63 | + executorch::runtime::runtime_init(); |
| 64 | + |
| 65 | + const char* path = std::getenv("ET_MODULE_ADD_PATH"); |
| 66 | + ASSERT_NE(path, nullptr) |
| 67 | + << "ET_MODULE_ADD_PATH environment variable must be set"; |
| 68 | + |
| 69 | + Result<FileDataLoader> loader = FileDataLoader::from(path); |
| 70 | + ASSERT_EQ(loader.error(), Error::Ok); |
| 71 | + loader_ = std::make_unique<FileDataLoader>(std::move(loader.get())); |
| 72 | + |
| 73 | + Result<Program> program = Program::load(loader_.get()); |
| 74 | + ASSERT_EQ(program.error(), Error::Ok); |
| 75 | + program_ = std::make_unique<Program>(std::move(program.get())); |
| 76 | + } |
| 77 | + |
| 78 | + std::unique_ptr<FileDataLoader> loader_; |
| 79 | + std::unique_ptr<Program> program_; |
| 80 | +}; |
| 81 | + |
| 82 | +TEST_F(KernelRegistryTest, MethodScopedKernelOverridesGlobal) { |
| 83 | + // Create two fallback kernels for aten::add.out with different behavior. |
| 84 | + Kernel kernel_x2( |
| 85 | + "aten::add.out", multiply_by_two); |
| 86 | + Kernel kernel_x3( |
| 87 | + "aten::add.out", multiply_by_three); |
| 88 | + |
| 89 | + Span<const Kernel> registry_x2(&kernel_x2, 1); |
| 90 | + Span<const Kernel> registry_x3(&kernel_x3, 1); |
| 91 | + |
| 92 | + // Load two methods with different kernel registries. |
| 93 | + ManagedMemoryManager mmm_a(kDefaultNonConstMemBytes, kDefaultRuntimeMemBytes); |
| 94 | + Result<Method> method_a = program_->load_method( |
| 95 | + "forward", |
| 96 | + &mmm_a.get(), |
| 97 | + /*event_tracer=*/nullptr, |
| 98 | + /*named_data_map=*/nullptr, |
| 99 | + /*backend_options=*/nullptr, |
| 100 | + registry_x2); |
| 101 | + ASSERT_EQ(method_a.error(), Error::Ok); |
| 102 | + |
| 103 | + ManagedMemoryManager mmm_b(kDefaultNonConstMemBytes, kDefaultRuntimeMemBytes); |
| 104 | + Result<Method> method_b = program_->load_method( |
| 105 | + "forward", |
| 106 | + &mmm_b.get(), |
| 107 | + /*event_tracer=*/nullptr, |
| 108 | + /*named_data_map=*/nullptr, |
| 109 | + /*backend_options=*/nullptr, |
| 110 | + registry_x3); |
| 111 | + ASSERT_EQ(method_b.error(), Error::Ok); |
| 112 | + |
| 113 | + // Prepare inputs: tensor inputs + alpha scalar (input index 2). |
| 114 | + auto inputs_a = torch::executor::util::prepare_input_tensors(method_a.get()); |
| 115 | + ASSERT_EQ(inputs_a.error(), Error::Ok); |
| 116 | + ASSERT_EQ(method_a->set_input(EValue(1.0), 2), Error::Ok); |
| 117 | + |
| 118 | + auto inputs_b = torch::executor::util::prepare_input_tensors(method_b.get()); |
| 119 | + ASSERT_EQ(inputs_b.error(), Error::Ok); |
| 120 | + ASSERT_EQ(method_b->set_input(EValue(1.0), 2), Error::Ok); |
| 121 | + |
| 122 | + // Execute both methods. |
| 123 | + ASSERT_EQ(method_a->execute(), Error::Ok); |
| 124 | + ASSERT_EQ(method_b->execute(), Error::Ok); |
| 125 | + |
| 126 | + // Check outputs: method_a should have 2.0, method_b should have 3.0. |
| 127 | + const auto& out_a = method_a->get_output(0).toTensor(); |
| 128 | + const auto& out_b = method_b->get_output(0).toTensor(); |
| 129 | + |
| 130 | + ASSERT_GT(out_a.numel(), 0); |
| 131 | + for (ssize_t i = 0; i < out_a.numel(); ++i) { |
| 132 | + EXPECT_FLOAT_EQ(out_a.const_data_ptr<float>()[i], 2.0f) |
| 133 | + << "method_a output[" << i << "] should be 2.0"; |
| 134 | + } |
| 135 | + for (ssize_t i = 0; i < out_b.numel(); ++i) { |
| 136 | + EXPECT_FLOAT_EQ(out_b.const_data_ptr<float>()[i], 3.0f) |
| 137 | + << "method_b output[" << i << "] should be 3.0"; |
| 138 | + } |
| 139 | +} |
0 commit comments