|
| 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 | +/** |
| 10 | + * Tests that device info (device_type) is correctly parsed from serialized |
| 11 | + * tensors in .pte files into TensorImpl at runtime. |
| 12 | + * |
| 13 | + * Uses a .pte exported with DeviceAwarePartitioner (CUDA device annotation) |
| 14 | + * so that delegate output tensors carry device_type=CUDA in ExtraTensorInfo. |
| 15 | + */ |
| 16 | + |
| 17 | +#include <executorch/runtime/executor/tensor_parser.h> |
| 18 | + |
| 19 | +#include <executorch/extension/data_loader/file_data_loader.h> |
| 20 | +#include <executorch/runtime/core/exec_aten/exec_aten.h> |
| 21 | +#include <executorch/runtime/executor/test/managed_memory_manager.h> |
| 22 | +#include <executorch/schema/program_generated.h> |
| 23 | + |
| 24 | +#include <gtest/gtest.h> |
| 25 | + |
| 26 | +using executorch::aten::Tensor; |
| 27 | +using executorch::runtime::Error; |
| 28 | +using executorch::runtime::Program; |
| 29 | +using executorch::runtime::Result; |
| 30 | +using executorch::runtime::deserialization::parseTensor; |
| 31 | +using executorch::runtime::testing::ManagedMemoryManager; |
| 32 | +using torch::executor::util::FileDataLoader; |
| 33 | + |
| 34 | +constexpr size_t kDefaultNonConstMemBytes = 32 * 1024U; |
| 35 | +constexpr size_t kDefaultRuntimeMemBytes = 32 * 1024U; |
| 36 | + |
| 37 | +namespace executorch { |
| 38 | +namespace runtime { |
| 39 | +namespace testing { |
| 40 | +class ProgramTestFriend final { |
| 41 | + public: |
| 42 | + const static executorch_flatbuffer::Program* GetInternalProgram( |
| 43 | + const Program* program) { |
| 44 | + return program->internal_program_; |
| 45 | + } |
| 46 | +}; |
| 47 | +} // namespace testing |
| 48 | +} // namespace runtime |
| 49 | +} // namespace executorch |
| 50 | + |
| 51 | +using executorch::runtime::testing::ProgramTestFriend; |
| 52 | + |
| 53 | +class TensorParserDeviceTest : public ::testing::Test { |
| 54 | + protected: |
| 55 | + void SetUp() override { |
| 56 | + const char* path = std::getenv("ET_MODULE_ADD_WITH_DEVICE_PATH"); |
| 57 | + ASSERT_NE(path, nullptr) |
| 58 | + << "ET_MODULE_ADD_WITH_DEVICE_PATH env var not set"; |
| 59 | + Result<FileDataLoader> loader = FileDataLoader::from(path); |
| 60 | + ASSERT_EQ(loader.error(), Error::Ok); |
| 61 | + loader_ = std::make_unique<FileDataLoader>(std::move(loader.get())); |
| 62 | + } |
| 63 | + |
| 64 | + std::unique_ptr<FileDataLoader> loader_; |
| 65 | +}; |
| 66 | + |
| 67 | +TEST_F(TensorParserDeviceTest, CUDADeviceParsedFromPteFile) { |
| 68 | + Result<Program> program = |
| 69 | + Program::load(loader_.get(), Program::Verification::Minimal); |
| 70 | + ASSERT_EQ(program.error(), Error::Ok); |
| 71 | + |
| 72 | + ManagedMemoryManager mmm(kDefaultNonConstMemBytes, kDefaultRuntimeMemBytes); |
| 73 | + |
| 74 | + const executorch_flatbuffer::Program* internal_program = |
| 75 | + ProgramTestFriend::GetInternalProgram(&program.get()); |
| 76 | + auto* execution_plan = |
| 77 | + internal_program->execution_plan()->GetMutableObject(0); |
| 78 | + auto* flatbuffer_values = execution_plan->values(); |
| 79 | + |
| 80 | + int cuda_tensor_count = 0; |
| 81 | + int cpu_tensor_count = 0; |
| 82 | + |
| 83 | + for (uint32_t i = 0; i < flatbuffer_values->size(); ++i) { |
| 84 | + auto* serialization_value = flatbuffer_values->Get(i); |
| 85 | + if (serialization_value->val_type() != |
| 86 | + executorch_flatbuffer::KernelTypes::Tensor) { |
| 87 | + continue; |
| 88 | + } |
| 89 | + |
| 90 | + auto* s_tensor = serialization_value->val_as_Tensor(); |
| 91 | + |
| 92 | + Result<Tensor> tensor = parseTensor(&program.get(), &mmm.get(), s_tensor); |
| 93 | + if (!tensor.ok()) { |
| 94 | + bool has_cuda = s_tensor->extra_tensor_info() != nullptr && |
| 95 | + s_tensor->extra_tensor_info()->device_type() == |
| 96 | + executorch_flatbuffer::DeviceType::CUDA; |
| 97 | + if (has_cuda) { |
| 98 | + cuda_tensor_count++; |
| 99 | + } |
| 100 | + continue; |
| 101 | + } |
| 102 | + |
| 103 | + Tensor t = tensor.get(); |
| 104 | + auto device_type = t.unsafeGetTensorImpl()->device_type(); |
| 105 | + |
| 106 | + if (device_type == executorch::runtime::etensor::DeviceType::CUDA) { |
| 107 | + cuda_tensor_count++; |
| 108 | + EXPECT_EQ(t.unsafeGetTensorImpl()->device_index(), 0) |
| 109 | + << "CUDA tensor should have device_index=0"; |
| 110 | + } else { |
| 111 | + EXPECT_EQ(device_type, executorch::runtime::etensor::DeviceType::CPU); |
| 112 | + EXPECT_EQ(t.unsafeGetTensorImpl()->device_index(), 0) |
| 113 | + << "CPU tensor should have device_index=0"; |
| 114 | + cpu_tensor_count++; |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + EXPECT_EQ(cuda_tensor_count, 3) |
| 119 | + << "Expected 3 CUDA tensors (2 delegate inputs + 1 delegate output)"; |
| 120 | + EXPECT_EQ(cpu_tensor_count, 0) |
| 121 | + << "Expected 0 CPU tensors (all annotated as CUDA)"; |
| 122 | +} |
| 123 | + |
| 124 | +TEST_F(TensorParserDeviceTest, NonDelegatedTensorsDefaultToCPU) { |
| 125 | + Result<Program> program = |
| 126 | + Program::load(loader_.get(), Program::Verification::Minimal); |
| 127 | + ASSERT_EQ(program.error(), Error::Ok); |
| 128 | + |
| 129 | + ManagedMemoryManager mmm(kDefaultNonConstMemBytes, kDefaultRuntimeMemBytes); |
| 130 | + |
| 131 | + const executorch_flatbuffer::Program* internal_program = |
| 132 | + ProgramTestFriend::GetInternalProgram(&program.get()); |
| 133 | + auto* execution_plan = |
| 134 | + internal_program->execution_plan()->GetMutableObject(0); |
| 135 | + auto* flatbuffer_values = execution_plan->values(); |
| 136 | + |
| 137 | + for (uint32_t i = 0; i < flatbuffer_values->size(); ++i) { |
| 138 | + auto* serialization_value = flatbuffer_values->Get(i); |
| 139 | + if (serialization_value->val_type() != |
| 140 | + executorch_flatbuffer::KernelTypes::Tensor) { |
| 141 | + continue; |
| 142 | + } |
| 143 | + |
| 144 | + auto* s_tensor = serialization_value->val_as_Tensor(); |
| 145 | + bool has_cuda_device = s_tensor->extra_tensor_info() != nullptr && |
| 146 | + s_tensor->extra_tensor_info()->device_type() == |
| 147 | + executorch_flatbuffer::DeviceType::CUDA; |
| 148 | + |
| 149 | + // Only check tensors that are NOT annotated as CUDA |
| 150 | + if (has_cuda_device) { |
| 151 | + continue; |
| 152 | + } |
| 153 | + |
| 154 | + Result<Tensor> tensor = parseTensor(&program.get(), &mmm.get(), s_tensor); |
| 155 | + if (!tensor.ok()) { |
| 156 | + continue; |
| 157 | + } |
| 158 | + |
| 159 | + Tensor t = tensor.get(); |
| 160 | + EXPECT_EQ( |
| 161 | + t.unsafeGetTensorImpl()->device_type(), |
| 162 | + executorch::runtime::etensor::DeviceType::CPU) |
| 163 | + << "Tensor at index " << i |
| 164 | + << " without CUDA annotation should default to CPU"; |
| 165 | + EXPECT_EQ(t.unsafeGetTensorImpl()->device_index(), 0) |
| 166 | + << "Tensor at index " << i |
| 167 | + << " without device annotation should have device_index=0"; |
| 168 | + } |
| 169 | +} |
0 commit comments