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