|
| 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 Module's device-aware memory allocation path works correctly. |
| 11 | + * |
| 12 | + * Uses ModuleAddWithDevice.pte which has: |
| 13 | + * non_const_buffer_sizes: [0, 48] (1 buffer, index 0 reserved) |
| 14 | + * non_const_buffer_device: [{buffer_idx=1, device_type=CUDA, device_index=0}] |
| 15 | + * |
| 16 | + * Since we don't have a real CUDA backend, we test that: |
| 17 | + * 1. CPU-only models load through Module without invoking device allocator |
| 18 | + * 2. Device-annotated models trigger DeviceMemoryBuffer::create via a mock |
| 19 | + */ |
| 20 | + |
| 21 | +#include <executorch/extension/module/module.h> |
| 22 | + |
| 23 | +#include <gtest/gtest.h> |
| 24 | + |
| 25 | +#include <executorch/runtime/core/device_allocator.h> |
| 26 | +#include <executorch/runtime/core/device_memory_buffer.h> |
| 27 | +#include <executorch/runtime/platform/runtime.h> |
| 28 | + |
| 29 | +using executorch::extension::Module; |
| 30 | +using executorch::runtime::DeviceAllocator; |
| 31 | +using executorch::runtime::DeviceMemoryBuffer; |
| 32 | +using executorch::runtime::Error; |
| 33 | +using executorch::runtime::Result; |
| 34 | +using executorch::runtime::register_device_allocator; |
| 35 | +using executorch::runtime::etensor::DeviceIndex; |
| 36 | +using executorch::runtime::etensor::DeviceType; |
| 37 | + |
| 38 | +namespace { |
| 39 | + |
| 40 | +class MockCudaAllocator : public DeviceAllocator { |
| 41 | + public: |
| 42 | + Result<void*> allocate(size_t nbytes, DeviceIndex index) override { |
| 43 | + allocate_count_++; |
| 44 | + last_allocate_size_ = nbytes; |
| 45 | + last_allocate_index_ = index; |
| 46 | + buffer_ = std::make_unique<uint8_t[]>(nbytes); |
| 47 | + return static_cast<void*>(buffer_.get()); |
| 48 | + } |
| 49 | + |
| 50 | + void deallocate(void* ptr, DeviceIndex index) override { |
| 51 | + deallocate_count_++; |
| 52 | + buffer_.reset(); |
| 53 | + } |
| 54 | + |
| 55 | + Error copy_host_to_device(void*, const void*, size_t, DeviceIndex) override { |
| 56 | + return Error::Ok; |
| 57 | + } |
| 58 | + |
| 59 | + Error copy_device_to_host(void*, const void*, size_t, DeviceIndex) override { |
| 60 | + return Error::Ok; |
| 61 | + } |
| 62 | + |
| 63 | + DeviceType device_type() const override { |
| 64 | + return DeviceType::CUDA; |
| 65 | + } |
| 66 | + |
| 67 | + int allocate_count_ = 0; |
| 68 | + int deallocate_count_ = 0; |
| 69 | + size_t last_allocate_size_ = 0; |
| 70 | + DeviceIndex last_allocate_index_ = -1; |
| 71 | + |
| 72 | + private: |
| 73 | + std::unique_ptr<uint8_t[]> buffer_; |
| 74 | +}; |
| 75 | + |
| 76 | +} // namespace |
| 77 | + |
| 78 | +static MockCudaAllocator g_mock_cuda; |
| 79 | + |
| 80 | +class ModuleDeviceMemoryTest : public ::testing::Test { |
| 81 | + protected: |
| 82 | + static void SetUpTestSuite() { |
| 83 | + executorch::runtime::runtime_init(); |
| 84 | + register_device_allocator(DeviceType::CUDA, &g_mock_cuda); |
| 85 | + } |
| 86 | + |
| 87 | + void SetUp() override { |
| 88 | + g_mock_cuda.allocate_count_ = 0; |
| 89 | + g_mock_cuda.deallocate_count_ = 0; |
| 90 | + g_mock_cuda.last_allocate_size_ = 0; |
| 91 | + g_mock_cuda.last_allocate_index_ = -1; |
| 92 | + } |
| 93 | +}; |
| 94 | + |
| 95 | +TEST_F(ModuleDeviceMemoryTest, CpuOnlyModelDoesNotAllocateDeviceMemory) { |
| 96 | + const char* path = std::getenv("ET_MODULE_ADD_PATH"); |
| 97 | + ASSERT_NE(path, nullptr) << "ET_MODULE_ADD_PATH not set"; |
| 98 | + |
| 99 | + Module module(path); |
| 100 | + auto err = module.load_method("forward"); |
| 101 | + ASSERT_EQ(err, Error::Ok); |
| 102 | + |
| 103 | + EXPECT_EQ(g_mock_cuda.allocate_count_, 0) |
| 104 | + << "CPU-only model should not allocate device memory"; |
| 105 | +} |
| 106 | + |
| 107 | +TEST_F(ModuleDeviceMemoryTest, DeviceMemoryBufferCreateCallsAllocator) { |
| 108 | + // Directly test DeviceMemoryBuffer::create with the registered mock. |
| 109 | + // This verifies the RAII allocation/deallocation path that Module uses. |
| 110 | + { |
| 111 | + auto result = DeviceMemoryBuffer::create(48, DeviceType::CUDA, 0); |
| 112 | + ASSERT_TRUE(result.ok()); |
| 113 | + auto buf = std::move(result.get()); |
| 114 | + |
| 115 | + EXPECT_EQ(g_mock_cuda.allocate_count_, 1); |
| 116 | + EXPECT_EQ(g_mock_cuda.last_allocate_size_, 48); |
| 117 | + EXPECT_EQ(g_mock_cuda.last_allocate_index_, 0); |
| 118 | + EXPECT_NE(buf.data(), nullptr); |
| 119 | + EXPECT_EQ(buf.size(), 48); |
| 120 | + |
| 121 | + // as_span() wraps the device pointer for HierarchicalAllocator. |
| 122 | + auto span = buf.as_span(); |
| 123 | + EXPECT_EQ(span.data(), static_cast<uint8_t*>(buf.data())); |
| 124 | + EXPECT_EQ(span.size(), 48); |
| 125 | + |
| 126 | + EXPECT_EQ(g_mock_cuda.deallocate_count_, 0); |
| 127 | + } |
| 128 | + // RAII deallocation on scope exit. |
| 129 | + EXPECT_EQ(g_mock_cuda.deallocate_count_, 1); |
| 130 | +} |
| 131 | + |
| 132 | +TEST_F(ModuleDeviceMemoryTest, DeviceModelMethodMetaReportsCudaBuffer) { |
| 133 | + // Verify MethodMeta reports the correct device for buffers in the |
| 134 | + // device-annotated model, without needing to load the full method. |
| 135 | + const char* path = std::getenv("ET_MODULE_ADD_WITH_DEVICE_PATH"); |
| 136 | + ASSERT_NE(path, nullptr) << "ET_MODULE_ADD_WITH_DEVICE_PATH not set"; |
| 137 | + |
| 138 | + Module module(path); |
| 139 | + auto err = module.load(); |
| 140 | + ASSERT_EQ(err, Error::Ok); |
| 141 | + |
| 142 | + auto meta = module.method_meta("forward"); |
| 143 | + ASSERT_TRUE(meta.ok()); |
| 144 | + |
| 145 | + // ModuleAddWithDevice has 1 planned buffer (48 bytes) on CUDA. |
| 146 | + ASSERT_EQ(meta->num_memory_planned_buffers(), 1); |
| 147 | + |
| 148 | + auto size = meta->memory_planned_buffer_size(0); |
| 149 | + ASSERT_TRUE(size.ok()); |
| 150 | + EXPECT_EQ(size.get(), 48); |
| 151 | + |
| 152 | + auto device = meta->memory_planned_buffer_device(0); |
| 153 | + ASSERT_TRUE(device.ok()); |
| 154 | + EXPECT_EQ(device->type(), DeviceType::CUDA); |
| 155 | + EXPECT_EQ(device->index(), 0); |
| 156 | +} |
| 157 | + |
| 158 | +TEST_F( |
| 159 | + ModuleDeviceMemoryTest, |
| 160 | + DeviceModelWithSharedArenasReturnsNotSupported) { |
| 161 | + const char* path = std::getenv("ET_MODULE_ADD_WITH_DEVICE_PATH"); |
| 162 | + ASSERT_NE(path, nullptr) << "ET_MODULE_ADD_WITH_DEVICE_PATH not set"; |
| 163 | + |
| 164 | + // share_memory_arenas = true with a device-annotated model should fail. |
| 165 | + Module module( |
| 166 | + path, |
| 167 | + Module::LoadMode::File, |
| 168 | + /*event_tracer=*/nullptr, |
| 169 | + /*memory_allocator=*/nullptr, |
| 170 | + /*temp_allocator=*/nullptr, |
| 171 | + /*share_memory_arenas=*/true); |
| 172 | + |
| 173 | + auto err = module.load_method("forward"); |
| 174 | + EXPECT_EQ(err, Error::NotSupported); |
| 175 | +} |
| 176 | + |
| 177 | +TEST_F( |
| 178 | + ModuleDeviceMemoryTest, |
| 179 | + LoadMethodAllocatesDeviceMemoryAndDeallocatesOnDestroy) { |
| 180 | + const char* path = std::getenv("ET_MODULE_ADD_WITH_DEVICE_PATH"); |
| 181 | + ASSERT_NE(path, nullptr) << "ET_MODULE_ADD_WITH_DEVICE_PATH not set"; |
| 182 | + |
| 183 | + { |
| 184 | + Module module(path); |
| 185 | + auto err = module.load_method("forward"); |
| 186 | + |
| 187 | + // Regardless of whether load_method succeeds or fails (e.g. due to |
| 188 | + // backend init issues), the device-aware memory allocation path |
| 189 | + // (make_planned_memory_with_devices) runs BEFORE backend init. |
| 190 | + EXPECT_EQ(g_mock_cuda.allocate_count_, 1) |
| 191 | + << "Expected 1 device allocation for the CUDA buffer" |
| 192 | + << " (actual: " << g_mock_cuda.allocate_count_ << ")" |
| 193 | + << ", deallocate_count=" << g_mock_cuda.deallocate_count_ |
| 194 | + << ", load_method returned error=" << static_cast<int>(err); |
| 195 | + EXPECT_EQ(g_mock_cuda.last_allocate_size_, 48) |
| 196 | + << "Expected 48 bytes allocated (3 CUDA tensors sharing one buffer)"; |
| 197 | + EXPECT_EQ(g_mock_cuda.last_allocate_index_, 0) |
| 198 | + << "Expected device_index=0 (cuda:0)"; |
| 199 | + |
| 200 | + if (err == Error::Ok) { |
| 201 | + // Success path: MethodHolder moved into methods_ map. |
| 202 | + // DeviceMemoryBuffer is alive as long as Module is alive. |
| 203 | + EXPECT_EQ(g_mock_cuda.deallocate_count_, 0) |
| 204 | + << "No deallocation while method is loaded"; |
| 205 | + } else { |
| 206 | + // Error path: local MethodHolder destroyed on return from load_method. |
| 207 | + // RAII deallocation already happened. |
| 208 | + EXPECT_EQ(g_mock_cuda.deallocate_count_, 1) |
| 209 | + << "RAII deallocation on error path"; |
| 210 | + } |
| 211 | + } |
| 212 | + |
| 213 | + // After Module destroyed, all device memory must be freed. |
| 214 | + EXPECT_EQ(g_mock_cuda.deallocate_count_, 1) |
| 215 | + << "Expected deallocation after Module destroyed"; |
| 216 | +} |
0 commit comments