Skip to content

Commit b1a58f4

Browse files
committed
[executorch] Split out HierarchicalAllocator tests
Pull Request resolved: #386 A following diff will modify HierarchicalAllocator and add more tests. Clean up the headers and deps of `executor_test` while I'm here. ghstack-source-id: 201066528 @exported-using-ghexport Differential Revision: [D49344930](https://our.internmc.facebook.com/intern/diff/D49344930/)
1 parent 1f5e54a commit b1a58f4

File tree

4 files changed

+98
-62
lines changed

4 files changed

+98
-62
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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/hierarchical_allocator.h>
10+
#include <executorch/runtime/core/memory_allocator.h>
11+
#include <executorch/runtime/platform/runtime.h>
12+
#include <executorch/test/utils/alignment.h>
13+
14+
#include <gtest/gtest.h>
15+
16+
using namespace ::testing;
17+
using torch::executor::Error;
18+
using torch::executor::HierarchicalAllocator;
19+
using torch::executor::MemoryAllocator;
20+
using torch::executor::Result;
21+
22+
class HierarchicalAllocatorTest : public ::testing::Test {
23+
protected:
24+
void SetUp() override {
25+
// Since these tests cause ET_LOG to be called, the PAL must be initialized
26+
// first.
27+
torch::executor::runtime_init();
28+
}
29+
};
30+
31+
TEST_F(HierarchicalAllocatorTest, Smoke) {
32+
constexpr size_t n_allocators = 2;
33+
constexpr size_t size0 = 4;
34+
constexpr size_t size1 = 8;
35+
uint8_t mem0[size0];
36+
uint8_t mem1[size1];
37+
MemoryAllocator allocators[n_allocators]{
38+
MemoryAllocator(size0, mem0), MemoryAllocator(size1, mem1)};
39+
40+
HierarchicalAllocator allocator(n_allocators, allocators);
41+
42+
// get_offset_address() success cases
43+
{
44+
// Total size is 4, so off=0 + size=2 fits.
45+
Result<void*> address = allocator.get_offset_address(
46+
/*memory_id=*/0, /*offset_bytes=*/0, /*size_bytes=*/2);
47+
ASSERT_EQ(address.error(), Error::Ok);
48+
ASSERT_NE(address.get(), nullptr);
49+
ASSERT_EQ(address.get(), mem0);
50+
}
51+
{
52+
// Total size is 8, so off=4 + size=4 fits exactly.
53+
Result<void*> address = allocator.get_offset_address(
54+
/*memory_id=*/1, /*offset_bytes=*/4, /*size_bytes=*/4);
55+
ASSERT_EQ(address.error(), Error::Ok);
56+
ASSERT_NE(address.get(), nullptr);
57+
ASSERT_EQ(address.get(), mem1 + 4);
58+
}
59+
60+
// get_offset_address() failure cases
61+
{
62+
// Total size is 4, so off=0 + size=5 is too large.
63+
Result<void*> address = allocator.get_offset_address(
64+
/*memory_id=*/0, /*offset_bytes=*/4, /*size_bytes=*/5);
65+
ASSERT_FALSE(address.ok());
66+
ASSERT_NE(address.error(), Error::Ok);
67+
}
68+
{
69+
// Total size is 4, so off=8 + size=0 is off the end.
70+
Result<void*> address = allocator.get_offset_address(
71+
/*memory_id=*/0, /*offset_bytes=*/8, /*size_bytes=*/0);
72+
ASSERT_FALSE(address.ok());
73+
ASSERT_NE(address.error(), Error::Ok);
74+
}
75+
{
76+
// ID too large; only two zero-indexed entries in the allocator.
77+
Result<void*> address = allocator.get_offset_address(
78+
/*memory_id=*/2, /*offset_bytes=*/0, /*size_bytes=*/2);
79+
ASSERT_FALSE(address.ok());
80+
ASSERT_NE(address.error(), Error::Ok);
81+
}
82+
}

runtime/core/test/targets.bzl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,16 @@ def define_common_targets():
7373
],
7474
)
7575

76+
runtime.cxx_test(
77+
name = "hierarchical_allocator_test",
78+
srcs = [
79+
"hierarchical_allocator_test.cpp",
80+
],
81+
deps = [
82+
"//executorch/runtime/core:memory_allocator",
83+
],
84+
)
85+
7686
runtime.cxx_test(
7787
name = "tensor_shape_dynamism_test_aten",
7888
srcs = ["tensor_shape_dynamism_test_aten.cpp"],

runtime/executor/test/executor_test.cpp

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,11 @@
1818
#include <executorch/runtime/core/array_ref.h>
1919
#include <executorch/runtime/core/evalue.h>
2020
#include <executorch/runtime/core/exec_aten/exec_aten.h>
21-
#include <executorch/runtime/core/hierarchical_allocator.h>
22-
#include <executorch/runtime/core/memory_allocator.h>
23-
#include <executorch/runtime/executor/executor.h>
2421
#include <executorch/runtime/kernel/kernel_runtime_context.h>
2522
#include <executorch/runtime/kernel/operator_registry.h>
2623
#include <executorch/runtime/platform/assert.h>
2724
#include <executorch/runtime/platform/runtime.h>
2825
#include <executorch/test/utils/DeathTest.h>
29-
#include <executorch/util/TestMemoryConfig.h>
3026

3127
namespace torch {
3228
namespace executor {
@@ -256,55 +252,5 @@ TEST(PyTreeEValue, DestructedSpec) {
256252
ASSERT_NEAR(child1.leaf().toDouble(), 3.0, 0.01);
257253
}
258254

259-
TEST_F(ExecutorTest, HierarchicalAllocator) {
260-
constexpr size_t n_allocators = 2;
261-
constexpr size_t size0 = 4;
262-
constexpr size_t size1 = 8;
263-
uint8_t mem0[size0];
264-
uint8_t mem1[size1];
265-
MemoryAllocator allocators[n_allocators]{
266-
MemoryAllocator(size0, mem0), MemoryAllocator(size1, mem1)};
267-
268-
HierarchicalAllocator allocator(n_allocators, allocators);
269-
270-
// get_offset_address() success cases
271-
{
272-
// Total size is 4, so off=0 + size=2 fits.
273-
Result<void*> address = allocator.get_offset_address(
274-
/*memory_id=*/0, /*offset_bytes=*/0, /*size_bytes=*/2);
275-
ASSERT_TRUE(address.ok());
276-
ASSERT_NE(address.get(), nullptr);
277-
}
278-
{
279-
// Total size is 8, so off=4 + size=4 fits exactly.
280-
Result<void*> address = allocator.get_offset_address(
281-
/*memory_id=*/1, /*offset_bytes=*/4, /*size_bytes=*/4);
282-
ASSERT_TRUE(address.ok());
283-
ASSERT_NE(address.get(), nullptr);
284-
}
285-
286-
// get_offset_address() failure cases
287-
{
288-
// Total size is 4, so off=0 + size=5 is too large.
289-
Result<void*> address = allocator.get_offset_address(
290-
/*memory_id=*/0, /*offset_bytes=*/4, /*size_bytes=*/5);
291-
ASSERT_FALSE(address.ok());
292-
ASSERT_NE(address.error(), Error::Ok);
293-
}
294-
{
295-
// Total size is 4, so off=8 + size=0 is off the end.
296-
Result<void*> address = allocator.get_offset_address(
297-
/*memory_id=*/0, /*offset_bytes=*/8, /*size_bytes=*/0);
298-
ASSERT_FALSE(address.ok());
299-
ASSERT_NE(address.error(), Error::Ok);
300-
}
301-
{
302-
// ID too large; only two zero-indexed entries in the allocator.
303-
Result<void*> address = allocator.get_offset_address(
304-
/*memory_id=*/2, /*offset_bytes=*/0, /*size_bytes=*/99);
305-
ASSERT_FALSE(address.ok());
306-
ASSERT_NE(address.error(), Error::Ok);
307-
}
308-
}
309255
} // namespace executor
310256
} // namespace torch

runtime/executor/test/targets.bzl

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,15 @@ def define_common_targets(is_fbcode = False):
6363
"executor_test.cpp",
6464
],
6565
deps = [
66-
"//executorch/runtime/core/exec_aten:lib",
67-
"//executorch/runtime/core:evalue",
66+
"//executorch/extension/pytree:pytree",
67+
"//executorch/kernels/portable:generated_lib", # @manual
6868
"//executorch/runtime/core:core",
69-
"//executorch/runtime/platform:platform",
70-
"//executorch/runtime/kernel:operator_registry",
71-
"//executorch/runtime/executor:executor",
72-
"//executorch/kernels/portable:generated_lib",
69+
"//executorch/runtime/core:evalue",
70+
"//executorch/runtime/core/exec_aten:lib",
7371
"//executorch/runtime/kernel:kernel_runtime_context",
74-
"//executorch/extension/pytree:pytree",
72+
"//executorch/runtime/kernel:operator_registry",
73+
"//executorch/runtime/platform:platform",
7574
"//executorch/test/utils:utils",
76-
"//executorch/util:test_memory_config",
7775
],
7876
)
7977

0 commit comments

Comments
 (0)