Skip to content

[executorch] Split out HierarchicalAllocator tests #386

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions runtime/core/test/hierarchical_allocator_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* 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.
*/

#include <executorch/runtime/core/hierarchical_allocator.h>
#include <executorch/runtime/core/memory_allocator.h>
#include <executorch/runtime/platform/runtime.h>
#include <executorch/test/utils/alignment.h>

#include <gtest/gtest.h>

using namespace ::testing;
using torch::executor::Error;
using torch::executor::HierarchicalAllocator;
using torch::executor::MemoryAllocator;
using torch::executor::Result;

class HierarchicalAllocatorTest : public ::testing::Test {
protected:
void SetUp() override {
// Since these tests cause ET_LOG to be called, the PAL must be initialized
// first.
torch::executor::runtime_init();
}
};

TEST_F(HierarchicalAllocatorTest, Smoke) {
constexpr size_t n_allocators = 2;
constexpr size_t size0 = 4;
constexpr size_t size1 = 8;
uint8_t mem0[size0];
uint8_t mem1[size1];
MemoryAllocator allocators[n_allocators]{
MemoryAllocator(size0, mem0), MemoryAllocator(size1, mem1)};

HierarchicalAllocator allocator(n_allocators, allocators);

// get_offset_address() success cases
{
// Total size is 4, so off=0 + size=2 fits.
Result<void*> address = allocator.get_offset_address(
/*memory_id=*/0, /*offset_bytes=*/0, /*size_bytes=*/2);
ASSERT_EQ(address.error(), Error::Ok);
ASSERT_NE(address.get(), nullptr);
ASSERT_EQ(address.get(), mem0);
}
{
// Total size is 8, so off=4 + size=4 fits exactly.
Result<void*> address = allocator.get_offset_address(
/*memory_id=*/1, /*offset_bytes=*/4, /*size_bytes=*/4);
ASSERT_EQ(address.error(), Error::Ok);
ASSERT_NE(address.get(), nullptr);
ASSERT_EQ(address.get(), mem1 + 4);
}

// get_offset_address() failure cases
{
// Total size is 4, so off=0 + size=5 is too large.
Result<void*> address = allocator.get_offset_address(
/*memory_id=*/0, /*offset_bytes=*/4, /*size_bytes=*/5);
ASSERT_FALSE(address.ok());
ASSERT_NE(address.error(), Error::Ok);
}
{
// Total size is 4, so off=8 + size=0 is off the end.
Result<void*> address = allocator.get_offset_address(
/*memory_id=*/0, /*offset_bytes=*/8, /*size_bytes=*/0);
ASSERT_FALSE(address.ok());
ASSERT_NE(address.error(), Error::Ok);
}
{
// ID too large; only two zero-indexed entries in the allocator.
Result<void*> address = allocator.get_offset_address(
/*memory_id=*/2, /*offset_bytes=*/0, /*size_bytes=*/2);
ASSERT_FALSE(address.ok());
ASSERT_NE(address.error(), Error::Ok);
}
}
10 changes: 10 additions & 0 deletions runtime/core/test/targets.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,16 @@ def define_common_targets():
],
)

runtime.cxx_test(
name = "hierarchical_allocator_test",
srcs = [
"hierarchical_allocator_test.cpp",
],
deps = [
"//executorch/runtime/core:memory_allocator",
],
)

runtime.cxx_test(
name = "tensor_shape_dynamism_test_aten",
srcs = ["tensor_shape_dynamism_test_aten.cpp"],
Expand Down
54 changes: 0 additions & 54 deletions runtime/executor/test/executor_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,11 @@
#include <executorch/runtime/core/array_ref.h>
#include <executorch/runtime/core/evalue.h>
#include <executorch/runtime/core/exec_aten/exec_aten.h>
#include <executorch/runtime/core/hierarchical_allocator.h>
#include <executorch/runtime/core/memory_allocator.h>
#include <executorch/runtime/executor/executor.h>
#include <executorch/runtime/kernel/kernel_runtime_context.h>
#include <executorch/runtime/kernel/operator_registry.h>
#include <executorch/runtime/platform/assert.h>
#include <executorch/runtime/platform/runtime.h>
#include <executorch/test/utils/DeathTest.h>
#include <executorch/util/TestMemoryConfig.h>

namespace torch {
namespace executor {
Expand Down Expand Up @@ -256,55 +252,5 @@ TEST(PyTreeEValue, DestructedSpec) {
ASSERT_NEAR(child1.leaf().toDouble(), 3.0, 0.01);
}

TEST_F(ExecutorTest, HierarchicalAllocator) {
constexpr size_t n_allocators = 2;
constexpr size_t size0 = 4;
constexpr size_t size1 = 8;
uint8_t mem0[size0];
uint8_t mem1[size1];
MemoryAllocator allocators[n_allocators]{
MemoryAllocator(size0, mem0), MemoryAllocator(size1, mem1)};

HierarchicalAllocator allocator(n_allocators, allocators);

// get_offset_address() success cases
{
// Total size is 4, so off=0 + size=2 fits.
Result<void*> address = allocator.get_offset_address(
/*memory_id=*/0, /*offset_bytes=*/0, /*size_bytes=*/2);
ASSERT_TRUE(address.ok());
ASSERT_NE(address.get(), nullptr);
}
{
// Total size is 8, so off=4 + size=4 fits exactly.
Result<void*> address = allocator.get_offset_address(
/*memory_id=*/1, /*offset_bytes=*/4, /*size_bytes=*/4);
ASSERT_TRUE(address.ok());
ASSERT_NE(address.get(), nullptr);
}

// get_offset_address() failure cases
{
// Total size is 4, so off=0 + size=5 is too large.
Result<void*> address = allocator.get_offset_address(
/*memory_id=*/0, /*offset_bytes=*/4, /*size_bytes=*/5);
ASSERT_FALSE(address.ok());
ASSERT_NE(address.error(), Error::Ok);
}
{
// Total size is 4, so off=8 + size=0 is off the end.
Result<void*> address = allocator.get_offset_address(
/*memory_id=*/0, /*offset_bytes=*/8, /*size_bytes=*/0);
ASSERT_FALSE(address.ok());
ASSERT_NE(address.error(), Error::Ok);
}
{
// ID too large; only two zero-indexed entries in the allocator.
Result<void*> address = allocator.get_offset_address(
/*memory_id=*/2, /*offset_bytes=*/0, /*size_bytes=*/99);
ASSERT_FALSE(address.ok());
ASSERT_NE(address.error(), Error::Ok);
}
}
} // namespace executor
} // namespace torch
14 changes: 6 additions & 8 deletions runtime/executor/test/targets.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,15 @@ def define_common_targets(is_fbcode = False):
"executor_test.cpp",
],
deps = [
"//executorch/runtime/core/exec_aten:lib",
"//executorch/runtime/core:evalue",
"//executorch/extension/pytree:pytree",
"//executorch/kernels/portable:generated_lib", # @manual
"//executorch/runtime/core:core",
"//executorch/runtime/platform:platform",
"//executorch/runtime/kernel:operator_registry",
"//executorch/runtime/executor:executor",
"//executorch/kernels/portable:generated_lib",
"//executorch/runtime/core:evalue",
"//executorch/runtime/core/exec_aten:lib",
"//executorch/runtime/kernel:kernel_runtime_context",
"//executorch/extension/pytree:pytree",
"//executorch/runtime/kernel:operator_registry",
"//executorch/runtime/platform:platform",
"//executorch/test/utils:utils",
"//executorch/util:test_memory_config",
],
)

Expand Down