|  | 
|  | 1 | + | 
|  | 2 | +/* | 
|  | 3 | + * Copyright (c) Meta Platforms, Inc. and affiliates. | 
|  | 4 | + * All rights reserved. | 
|  | 5 | + * | 
|  | 6 | + * This source code is licensed under the BSD-style license found in the | 
|  | 7 | + * LICENSE file in the root directory of this source tree. | 
|  | 8 | + */ | 
|  | 9 | + | 
|  | 10 | +#include <executorch/exir/backend/test/demos/rpc/ExecutorBackend.h> | 
|  | 11 | +#include <executorch/extension/data_loader/file_data_loader.h> | 
|  | 12 | +#include <executorch/extension/flat_tensor/flat_tensor_data_map.h> | 
|  | 13 | +#include <executorch/runtime/core/error.h> | 
|  | 14 | +#include <executorch/runtime/core/result.h> | 
|  | 15 | +#include <executorch/runtime/executor/method.h> | 
|  | 16 | +#include <executorch/runtime/executor/program.h> | 
|  | 17 | +#include <executorch/runtime/executor/test/managed_memory_manager.h> | 
|  | 18 | +#include <executorch/runtime/platform/runtime.h> | 
|  | 19 | + | 
|  | 20 | +#include <gtest/gtest.h> | 
|  | 21 | + | 
|  | 22 | +using namespace ::testing; | 
|  | 23 | +using executorch::extension::FlatTensorDataMap; | 
|  | 24 | +using executorch::runtime::DataLoader; | 
|  | 25 | +using executorch::runtime::Error; | 
|  | 26 | +using executorch::runtime::Method; | 
|  | 27 | +using executorch::runtime::Program; | 
|  | 28 | +using executorch::runtime::Result; | 
|  | 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 | +class BackendDataSeparationTest : public ::testing::Test { | 
|  | 36 | + protected: | 
|  | 37 | +  void SetUp() override { | 
|  | 38 | +    // Since these tests cause ET_LOG to be called, the PAL must be initialized | 
|  | 39 | +    // first. | 
|  | 40 | +    executorch::runtime::runtime_init(); | 
|  | 41 | + | 
|  | 42 | +    // Make sure that the backend has been registered. Safe to call multiple | 
|  | 43 | +    // times. Doing this at runtime ensures that it's only registered if these | 
|  | 44 | +    // tests are run. | 
|  | 45 | +    ASSERT_EQ(example::register_executor_backend(), Error::Ok); | 
|  | 46 | + | 
|  | 47 | +    // Create data loaders. | 
|  | 48 | +    Result<FileDataLoader> linear_program_loader = FileDataLoader::from( | 
|  | 49 | +        std::getenv("ET_MODULE_LINEAR_DELEGATE_PROGRAM_PATH")); | 
|  | 50 | +    ASSERT_EQ(linear_program_loader.error(), Error::Ok); | 
|  | 51 | +    linear_program_loader_ = std::make_unique<FileDataLoader>( | 
|  | 52 | +        std::move(linear_program_loader.get())); | 
|  | 53 | + | 
|  | 54 | +    Result<FileDataLoader> linear_data_loader = | 
|  | 55 | +        FileDataLoader::from(std::getenv("ET_MODULE_LINEAR_DATA_PATH")); | 
|  | 56 | +    ASSERT_EQ(linear_data_loader.error(), Error::Ok); | 
|  | 57 | +    linear_data_loader_ = | 
|  | 58 | +        std::make_unique<FileDataLoader>(std::move(linear_data_loader.get())); | 
|  | 59 | + | 
|  | 60 | +    // Create programs. | 
|  | 61 | +    Result<Program> linear_program = Program::load( | 
|  | 62 | +        linear_program_loader_.get(), | 
|  | 63 | +        Program::Verification::InternalConsistency); | 
|  | 64 | +    ASSERT_EQ(linear_program.error(), Error::Ok); | 
|  | 65 | +    linear_program_ = | 
|  | 66 | +        std::make_unique<Program>(std::move(linear_program.get())); | 
|  | 67 | + | 
|  | 68 | +    Result<FlatTensorDataMap> linear_data_map = | 
|  | 69 | +        FlatTensorDataMap::load(linear_data_loader_.get()); | 
|  | 70 | +    EXPECT_EQ(linear_data_map.error(), Error::Ok); | 
|  | 71 | +    linear_data_map_ = | 
|  | 72 | +        std::make_unique<FlatTensorDataMap>(std::move(linear_data_map.get())); | 
|  | 73 | +     | 
|  | 74 | +    ET_LOG(Info, "setup done, named_data_map_ = %lu", linear_data_map_->get_num_keys().get()); | 
|  | 75 | +  } | 
|  | 76 | + | 
|  | 77 | + private: | 
|  | 78 | +  std::unique_ptr<FileDataLoader> linear_program_loader_; | 
|  | 79 | +  std::unique_ptr<FileDataLoader> linear_data_loader_; | 
|  | 80 | + | 
|  | 81 | + protected: | 
|  | 82 | +  std::unique_ptr<Program> linear_program_; | 
|  | 83 | +  std::unique_ptr<FlatTensorDataMap> linear_data_map_; | 
|  | 84 | +}; | 
|  | 85 | + | 
|  | 86 | +TEST_F(BackendDataSeparationTest, TestSeparation) { | 
|  | 87 | +  ManagedMemoryManager mmm(kDefaultNonConstMemBytes, kDefaultRuntimeMemBytes); | 
|  | 88 | +  Result<Method> method = linear_program_->load_method( | 
|  | 89 | +      "forward", &mmm.get(), /*event_tracer=*/nullptr, /*named_data_map=*/linear_data_map_.get()); | 
|  | 90 | +  ASSERT_EQ(method.error(), Error::Ok); | 
|  | 91 | + | 
|  | 92 | +  // Can execute the method. | 
|  | 93 | +  Error err = method->execute(); | 
|  | 94 | +  ASSERT_EQ(err, Error::Ok); | 
|  | 95 | +} | 
0 commit comments