|
| 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::Result; |
| 19 | +using executorch::runtime::get_device_allocator; |
| 20 | +using executorch::runtime::register_device_allocator; |
| 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(size_t nbytes, DeviceIndex index) override { |
| 33 | + allocate_count_++; |
| 34 | + last_allocate_size_ = nbytes; |
| 35 | + return static_cast<void*>(buffer_); |
| 36 | + } |
| 37 | + |
| 38 | + void deallocate(void* ptr, DeviceIndex index) override { |
| 39 | + deallocate_count_++; |
| 40 | + last_deallocate_ptr_ = ptr; |
| 41 | + } |
| 42 | + |
| 43 | + Error copy_host_to_device( |
| 44 | + void* dst, |
| 45 | + const void* src, |
| 46 | + size_t nbytes, |
| 47 | + DeviceIndex index) override { |
| 48 | + return Error::Ok; |
| 49 | + } |
| 50 | + |
| 51 | + Error copy_device_to_host( |
| 52 | + void* dst, |
| 53 | + const void* src, |
| 54 | + size_t nbytes, |
| 55 | + DeviceIndex index) override { |
| 56 | + return Error::Ok; |
| 57 | + } |
| 58 | + |
| 59 | + DeviceType device_type() const override { |
| 60 | + return type_; |
| 61 | + } |
| 62 | + |
| 63 | + int allocate_count_ = 0; |
| 64 | + int deallocate_count_ = 0; |
| 65 | + size_t last_allocate_size_ = 0; |
| 66 | + void* last_deallocate_ptr_ = nullptr; |
| 67 | + uint8_t buffer_[256] = {}; |
| 68 | + |
| 69 | + private: |
| 70 | + DeviceType type_; |
| 71 | +}; |
| 72 | + |
| 73 | +// Global mock registered once before all tests run. |
| 74 | +static MockAllocator g_mock_cuda(DeviceType::CUDA); |
| 75 | + |
| 76 | +class DeviceMemoryBufferTest : public ::testing::Test { |
| 77 | + protected: |
| 78 | + static void SetUpTestSuite() { |
| 79 | + executorch::runtime::runtime_init(); |
| 80 | + register_device_allocator(DeviceType::CUDA, &g_mock_cuda); |
| 81 | + } |
| 82 | + |
| 83 | + void SetUp() override { |
| 84 | + // Reset counters before each test. |
| 85 | + g_mock_cuda.allocate_count_ = 0; |
| 86 | + g_mock_cuda.deallocate_count_ = 0; |
| 87 | + g_mock_cuda.last_allocate_size_ = 0; |
| 88 | + g_mock_cuda.last_deallocate_ptr_ = nullptr; |
| 89 | + } |
| 90 | +}; |
| 91 | + |
| 92 | +TEST_F(DeviceMemoryBufferTest, DefaultConstructedIsEmpty) { |
| 93 | + DeviceMemoryBuffer buf; |
| 94 | + EXPECT_EQ(buf.data(), nullptr); |
| 95 | + EXPECT_EQ(buf.size(), 0); |
| 96 | + |
| 97 | + auto span = buf.as_span(); |
| 98 | + EXPECT_EQ(span.data(), nullptr); |
| 99 | + EXPECT_EQ(span.size(), 0); |
| 100 | +} |
| 101 | + |
| 102 | +TEST_F(DeviceMemoryBufferTest, CreateAllocatesAndDestructorDeallocates) { |
| 103 | + { |
| 104 | + auto result = DeviceMemoryBuffer::create(1024, DeviceType::CUDA, 0); |
| 105 | + ASSERT_TRUE(result.ok()); |
| 106 | + |
| 107 | + auto buf = std::move(result.get()); |
| 108 | + EXPECT_NE(buf.data(), nullptr); |
| 109 | + EXPECT_EQ(buf.size(), 1024); |
| 110 | + EXPECT_EQ(g_mock_cuda.allocate_count_, 1); |
| 111 | + EXPECT_EQ(g_mock_cuda.last_allocate_size_, 1024); |
| 112 | + EXPECT_EQ(g_mock_cuda.deallocate_count_, 0); |
| 113 | + } |
| 114 | + EXPECT_EQ(g_mock_cuda.deallocate_count_, 1); |
| 115 | + EXPECT_EQ(g_mock_cuda.last_deallocate_ptr_, g_mock_cuda.buffer_); |
| 116 | +} |
| 117 | + |
| 118 | +TEST_F(DeviceMemoryBufferTest, CreateFailsWithNoRegisteredAllocator) { |
| 119 | + auto result = DeviceMemoryBuffer::create(512, DeviceType::CPU, 0); |
| 120 | + EXPECT_FALSE(result.ok()); |
| 121 | + EXPECT_EQ(result.error(), Error::NotFound); |
| 122 | +} |
| 123 | + |
| 124 | +TEST_F(DeviceMemoryBufferTest, MoveConstructorTransfersOwnership) { |
| 125 | + auto result = DeviceMemoryBuffer::create(256, DeviceType::CUDA, 0); |
| 126 | + ASSERT_TRUE(result.ok()); |
| 127 | + auto original = std::move(result.get()); |
| 128 | + void* original_ptr = original.data(); |
| 129 | + |
| 130 | + DeviceMemoryBuffer moved(std::move(original)); |
| 131 | + |
| 132 | + EXPECT_EQ(original.data(), nullptr); |
| 133 | + EXPECT_EQ(original.size(), 0); |
| 134 | + EXPECT_EQ(moved.data(), original_ptr); |
| 135 | + EXPECT_EQ(moved.size(), 256); |
| 136 | + EXPECT_EQ(g_mock_cuda.deallocate_count_, 0); |
| 137 | +} |
| 138 | + |
| 139 | +TEST_F(DeviceMemoryBufferTest, MoveAssignmentTransfersOwnership) { |
| 140 | + auto result = DeviceMemoryBuffer::create(128, DeviceType::CUDA, 0); |
| 141 | + ASSERT_TRUE(result.ok()); |
| 142 | + auto original = std::move(result.get()); |
| 143 | + void* original_ptr = original.data(); |
| 144 | + |
| 145 | + DeviceMemoryBuffer target; |
| 146 | + target = std::move(original); |
| 147 | + |
| 148 | + EXPECT_EQ(original.data(), nullptr); |
| 149 | + EXPECT_EQ(target.data(), original_ptr); |
| 150 | + EXPECT_EQ(target.size(), 128); |
| 151 | + EXPECT_EQ(g_mock_cuda.deallocate_count_, 0); |
| 152 | +} |
| 153 | + |
| 154 | +TEST_F(DeviceMemoryBufferTest, DestructorNoOpForDefaultConstructed) { |
| 155 | + { |
| 156 | + DeviceMemoryBuffer buf; |
| 157 | + } |
| 158 | + EXPECT_EQ(g_mock_cuda.deallocate_count_, 0); |
| 159 | +} |
| 160 | + |
| 161 | +TEST_F(DeviceMemoryBufferTest, AsSpanWrapsDevicePointer) { |
| 162 | + auto result = DeviceMemoryBuffer::create(2048, DeviceType::CUDA, 0); |
| 163 | + ASSERT_TRUE(result.ok()); |
| 164 | + auto buf = std::move(result.get()); |
| 165 | + |
| 166 | + auto span = buf.as_span(); |
| 167 | + EXPECT_EQ(span.data(), static_cast<uint8_t*>(buf.data())); |
| 168 | + EXPECT_EQ(span.size(), 2048); |
| 169 | +} |
0 commit comments