|
| 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/runtime/core/device_memory_buffer.h> |
| 10 | + |
| 11 | +#include <gtest/gtest.h> |
| 12 | + |
| 13 | +#include <executorch/runtime/platform/runtime.h> |
| 14 | + |
| 15 | +using executorch::runtime::DeviceAllocator; |
| 16 | +using executorch::runtime::DeviceMemoryBuffer; |
| 17 | +using executorch::runtime::Error; |
| 18 | +using executorch::runtime::get_device_allocator; |
| 19 | +using executorch::runtime::register_device_allocator; |
| 20 | +using executorch::runtime::Result; |
| 21 | +using executorch::runtime::etensor::DeviceIndex; |
| 22 | +using executorch::runtime::etensor::DeviceType; |
| 23 | + |
| 24 | +/** |
| 25 | + * A mock DeviceAllocator for testing DeviceMemoryBuffer. |
| 26 | + * Returns pointers into a local buffer and tracks call counts. |
| 27 | + */ |
| 28 | +class MockAllocator : public DeviceAllocator { |
| 29 | + public: |
| 30 | + explicit MockAllocator(DeviceType type) : type_(type) {} |
| 31 | + |
| 32 | + Result<void*> allocate( |
| 33 | + size_t nbytes, |
| 34 | + DeviceIndex index, |
| 35 | + size_t alignment = DeviceAllocator::kDefaultAlignment) override { |
| 36 | + allocate_count_++; |
| 37 | + last_allocate_size_ = nbytes; |
| 38 | + last_allocate_alignment_ = alignment; |
| 39 | + return static_cast<void*>(buffer_); |
| 40 | + } |
| 41 | + |
| 42 | + void deallocate(void* ptr, DeviceIndex index) override { |
| 43 | + deallocate_count_++; |
| 44 | + last_deallocate_ptr_ = ptr; |
| 45 | + } |
| 46 | + |
| 47 | + Error copy_host_to_device( |
| 48 | + void* dst, |
| 49 | + const void* src, |
| 50 | + size_t nbytes, |
| 51 | + DeviceIndex index) override { |
| 52 | + return Error::Ok; |
| 53 | + } |
| 54 | + |
| 55 | + Error copy_device_to_host( |
| 56 | + void* dst, |
| 57 | + const void* src, |
| 58 | + size_t nbytes, |
| 59 | + DeviceIndex index) override { |
| 60 | + return Error::Ok; |
| 61 | + } |
| 62 | + |
| 63 | + DeviceType device_type() const override { |
| 64 | + return type_; |
| 65 | + } |
| 66 | + |
| 67 | + int allocate_count_ = 0; |
| 68 | + int deallocate_count_ = 0; |
| 69 | + size_t last_allocate_size_ = 0; |
| 70 | + size_t last_allocate_alignment_ = 0; |
| 71 | + void* last_deallocate_ptr_ = nullptr; |
| 72 | + uint8_t buffer_[256] = {}; |
| 73 | + |
| 74 | + private: |
| 75 | + DeviceType type_; |
| 76 | +}; |
| 77 | + |
| 78 | +// Global mock registered once before all tests run. |
| 79 | +static MockAllocator g_mock_cuda(DeviceType::CUDA); |
| 80 | + |
| 81 | +class DeviceMemoryBufferTest : public ::testing::Test { |
| 82 | + protected: |
| 83 | + static void SetUpTestSuite() { |
| 84 | + executorch::runtime::runtime_init(); |
| 85 | + register_device_allocator(&g_mock_cuda); |
| 86 | + } |
| 87 | + |
| 88 | + void SetUp() override { |
| 89 | + // Reset counters before each test. |
| 90 | + g_mock_cuda.allocate_count_ = 0; |
| 91 | + g_mock_cuda.deallocate_count_ = 0; |
| 92 | + g_mock_cuda.last_allocate_size_ = 0; |
| 93 | + g_mock_cuda.last_allocate_alignment_ = 0; |
| 94 | + g_mock_cuda.last_deallocate_ptr_ = nullptr; |
| 95 | + } |
| 96 | +}; |
| 97 | + |
| 98 | +TEST_F(DeviceMemoryBufferTest, DefaultConstructedIsEmpty) { |
| 99 | + DeviceMemoryBuffer buf; |
| 100 | + EXPECT_EQ(buf.data(), nullptr); |
| 101 | + EXPECT_EQ(buf.size(), 0); |
| 102 | + |
| 103 | + auto span = buf.as_span(); |
| 104 | + EXPECT_EQ(span.data(), nullptr); |
| 105 | + EXPECT_EQ(span.size(), 0); |
| 106 | +} |
| 107 | + |
| 108 | +TEST_F(DeviceMemoryBufferTest, CreateAllocatesAndDestructorDeallocates) { |
| 109 | + { |
| 110 | + auto result = DeviceMemoryBuffer::create(1024, DeviceType::CUDA, 0); |
| 111 | + ASSERT_TRUE(result.ok()); |
| 112 | + |
| 113 | + auto buf = std::move(result.get()); |
| 114 | + EXPECT_NE(buf.data(), nullptr); |
| 115 | + EXPECT_EQ(buf.size(), 1024); |
| 116 | + EXPECT_EQ(g_mock_cuda.allocate_count_, 1); |
| 117 | + EXPECT_EQ(g_mock_cuda.last_allocate_size_, 1024); |
| 118 | + EXPECT_EQ(g_mock_cuda.deallocate_count_, 0); |
| 119 | + } |
| 120 | + EXPECT_EQ(g_mock_cuda.deallocate_count_, 1); |
| 121 | + EXPECT_EQ(g_mock_cuda.last_deallocate_ptr_, g_mock_cuda.buffer_); |
| 122 | +} |
| 123 | + |
| 124 | +TEST_F(DeviceMemoryBufferTest, CreateFailsWithNoRegisteredAllocator) { |
| 125 | + auto result = DeviceMemoryBuffer::create(512, DeviceType::CPU, 0); |
| 126 | + EXPECT_FALSE(result.ok()); |
| 127 | + EXPECT_EQ(result.error(), Error::NotFound); |
| 128 | +} |
| 129 | + |
| 130 | +TEST_F(DeviceMemoryBufferTest, MoveConstructorTransfersOwnership) { |
| 131 | + auto result = DeviceMemoryBuffer::create(256, DeviceType::CUDA, 0); |
| 132 | + ASSERT_TRUE(result.ok()); |
| 133 | + auto original = std::move(result.get()); |
| 134 | + void* original_ptr = original.data(); |
| 135 | + |
| 136 | + DeviceMemoryBuffer moved(std::move(original)); |
| 137 | + |
| 138 | + EXPECT_EQ(original.data(), nullptr); |
| 139 | + EXPECT_EQ(original.size(), 0); |
| 140 | + EXPECT_EQ(moved.data(), original_ptr); |
| 141 | + EXPECT_EQ(moved.size(), 256); |
| 142 | + EXPECT_EQ(g_mock_cuda.deallocate_count_, 0); |
| 143 | +} |
| 144 | + |
| 145 | +TEST_F(DeviceMemoryBufferTest, MoveAssignmentTransfersOwnership) { |
| 146 | + auto result = DeviceMemoryBuffer::create(128, DeviceType::CUDA, 0); |
| 147 | + ASSERT_TRUE(result.ok()); |
| 148 | + auto original = std::move(result.get()); |
| 149 | + void* original_ptr = original.data(); |
| 150 | + |
| 151 | + DeviceMemoryBuffer target; |
| 152 | + target = std::move(original); |
| 153 | + |
| 154 | + EXPECT_EQ(original.data(), nullptr); |
| 155 | + EXPECT_EQ(target.data(), original_ptr); |
| 156 | + EXPECT_EQ(target.size(), 128); |
| 157 | + EXPECT_EQ(g_mock_cuda.deallocate_count_, 0); |
| 158 | +} |
| 159 | + |
| 160 | +TEST_F(DeviceMemoryBufferTest, DestructorNoOpForDefaultConstructed) { |
| 161 | + { DeviceMemoryBuffer buf; } |
| 162 | + EXPECT_EQ(g_mock_cuda.deallocate_count_, 0); |
| 163 | +} |
| 164 | + |
| 165 | +TEST_F(DeviceMemoryBufferTest, AsSpanWrapsDevicePointer) { |
| 166 | + auto result = DeviceMemoryBuffer::create(2048, DeviceType::CUDA, 0); |
| 167 | + ASSERT_TRUE(result.ok()); |
| 168 | + auto buf = std::move(result.get()); |
| 169 | + |
| 170 | + auto span = buf.as_span(); |
| 171 | + EXPECT_EQ(span.data(), static_cast<uint8_t*>(buf.data())); |
| 172 | + EXPECT_EQ(span.size(), 2048); |
| 173 | +} |
| 174 | + |
| 175 | +TEST_F(DeviceMemoryBufferTest, CreateUsesDefaultAlignmentWhenUnspecified) { |
| 176 | + auto result = DeviceMemoryBuffer::create(1024, DeviceType::CUDA, 0); |
| 177 | + ASSERT_TRUE(result.ok()); |
| 178 | + EXPECT_EQ( |
| 179 | + g_mock_cuda.last_allocate_alignment_, DeviceAllocator::kDefaultAlignment); |
| 180 | +} |
| 181 | + |
| 182 | +TEST_F(DeviceMemoryBufferTest, CreateForwardsCustomAlignmentToAllocator) { |
| 183 | + constexpr size_t kCustomAlignment = 512; |
| 184 | + auto result = |
| 185 | + DeviceMemoryBuffer::create(1024, DeviceType::CUDA, 0, kCustomAlignment); |
| 186 | + ASSERT_TRUE(result.ok()); |
| 187 | + EXPECT_EQ(g_mock_cuda.last_allocate_alignment_, kCustomAlignment); |
| 188 | +} |
0 commit comments