Skip to content

Commit

Permalink
Simplify CPU allocator arena usage helper function, fix unit tests th…
Browse files Browse the repository at this point in the history
…at check old ifdefs.
  • Loading branch information
edgchen1 committed Nov 18, 2024
1 parent c4f3742 commit 4a70cbd
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 25 deletions.
4 changes: 2 additions & 2 deletions onnxruntime/core/framework/allocator_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ AllocatorPtr CreateAllocator(const AllocatorCreationInfo& info) {
}
}

bool ShouldCpuAllocatorUseArena([[maybe_unused]] bool is_arena_requested) {
bool DoesCpuAllocatorSupportArenaUsage() {

Check warning

Code scanning / PREfast

You can attempt to make 'onnxruntime::DoesCpuAllocatorSupportArenaUsage' constexpr unless it contains any undefined behavior (f.4). Warning

You can attempt to make 'onnxruntime::DoesCpuAllocatorSupportArenaUsage' constexpr unless it contains any undefined behavior (f.4).
#if defined(USE_JEMALLOC) || defined(USE_MIMALLOC)
// We use these allocators instead of the arena.
return false;
Expand All @@ -89,7 +89,7 @@ bool ShouldCpuAllocatorUseArena([[maybe_unused]] bool is_arena_requested) {
if constexpr (sizeof(void*) == 4) {
return false;
} else {
return is_arena_requested;
return true;
}
#endif
}
Expand Down
4 changes: 2 additions & 2 deletions onnxruntime/core/framework/allocator_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ struct AllocatorCreationInfo {
AllocatorPtr CreateAllocator(const AllocatorCreationInfo& info);

/**
* Gets whether a CPU allocator should use an arena or not.
* Gets whether a CPU allocator supports arena usage.
*/
bool ShouldCpuAllocatorUseArena(bool is_arena_requested);
bool DoesCpuAllocatorSupportArenaUsage();

} // namespace onnxruntime
3 changes: 1 addition & 2 deletions onnxruntime/core/providers/cpu/cpu_execution_provider.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ CPUExecutionProvider::CPUExecutionProvider(const CPUExecutionProviderInfo& info)
: IExecutionProvider{onnxruntime::kCpuExecutionProvider}, info_{info} {}

std::vector<AllocatorPtr> CPUExecutionProvider::CreatePreferredAllocators() {
const bool is_arena_requested = info_.create_arena;
const bool create_arena = ShouldCpuAllocatorUseArena(is_arena_requested);
const bool create_arena = DoesCpuAllocatorSupportArenaUsage() ? info_.create_arena : false;
AllocatorCreationInfo device_info{[](int) { return std::make_unique<CPUAllocator>(); },
DEFAULT_CPU_ALLOCATOR_DEVICE_ID, create_arena};

Expand Down
5 changes: 3 additions & 2 deletions onnxruntime/core/session/environment.cc
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,9 @@ Status Environment::CreateAndRegisterAllocator(const OrtMemoryInfo& mem_info, co
}

// determine if arena should be used
const bool is_arena_requested = mem_info.alloc_type == OrtArenaAllocator;
const bool create_arena = ShouldCpuAllocatorUseArena(is_arena_requested);
const bool create_arena = DoesCpuAllocatorSupportArenaUsage()
? (mem_info.alloc_type == OrtArenaAllocator)
: false;

AllocatorPtr allocator_ptr;
// create appropriate DeviceAllocatorRegistrationInfo and allocator based on create_arena
Expand Down
11 changes: 5 additions & 6 deletions onnxruntime/test/framework/allocator_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <absl/base/config.h>

#include "core/framework/allocator.h"
#include "core/framework/allocator_utils.h"

#include "test_utils.h"
#include "gtest/gtest.h"
Expand All @@ -15,12 +16,10 @@ TEST(AllocatorTest, CPUAllocatorTest) {
ASSERT_STREQ(cpu_arena->Info().name, CPU);
EXPECT_EQ(cpu_arena->Info().id, 0);

// arena is disabled for CPUExecutionProvider on x86 and JEMalloc
#if (defined(__amd64__) || defined(_M_AMD64) || defined(__aarch64__) || defined(__loongarch__) || defined(_M_ARM64)) && !defined(USE_JEMALLOC) && !defined(USE_MIMALLOC) && !defined(ABSL_HAVE_ADDRESS_SANITIZER)
EXPECT_EQ(cpu_arena->Info().alloc_type, OrtAllocatorType::OrtArenaAllocator);
#else
EXPECT_EQ(cpu_arena->Info().alloc_type, OrtAllocatorType::OrtDeviceAllocator);
#endif
const auto expected_allocator_type = DoesCpuAllocatorSupportArenaUsage()
? OrtAllocatorType::OrtArenaAllocator
: OrtAllocatorType::OrtDeviceAllocator;
EXPECT_EQ(cpu_arena->Info().alloc_type, expected_allocator_type);

size_t size = 1024;
auto bytes = cpu_arena->Alloc(size);
Expand Down
11 changes: 6 additions & 5 deletions onnxruntime/test/framework/session_state_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <absl/base/config.h>

#include "asserts.h"
#include "core/framework/allocator_utils.h"
#include "core/framework/execution_providers.h"
#include "core/framework/graph_partitioner.h"
#include "core/framework/kernel_registry.h"
Expand Down Expand Up @@ -216,10 +217,12 @@ TEST_P(SessionStateTestP, TestInitializerProcessing) {

// Test that we allocate memory for an initializer from non-arena memory even if we provide an arena-based allocator
// if the relevant session option config flag is set
// For this test we need to enable the arena-based allocator which is not supported on x86 builds, so
// enable this test only on x64 builds
#if (defined(__amd64__) || defined(_M_AMD64) || defined(__aarch64__) || defined(_M_ARM64)) && !defined(USE_MIMALLOC) && !defined(ABSL_HAVE_ADDRESS_SANITIZER)
TEST(SessionStateTest, TestInitializerMemoryAllocatedUsingNonArenaMemory) {
// For this test we need to enable the arena-based allocator.
if (!DoesCpuAllocatorSupportArenaUsage()) {
GTEST_SKIP() << "CPU allocator does not support arena usage.";
}

AllocatorPtr cpu_allocator = std::make_shared<CPUAllocator>();
// Part 1: Feature turned ON (i.e.) allocate from non-arena memory
{
Expand Down Expand Up @@ -348,8 +351,6 @@ TEST(SessionStateTest, TestInitializerMemoryAllocatedUsingNonArenaMemory) {
}
}

#endif

INSTANTIATE_TEST_SUITE_P(SessionStateTests, SessionStateTestP, testing::ValuesIn(param_list));

#ifndef ENABLE_TRAINING_CORE
Expand Down
11 changes: 5 additions & 6 deletions onnxruntime/test/framework/tensor_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License.

#include "core/framework/tensor.h"
#include "core/framework/allocator_utils.h"
#include "test_utils.h"

#include "gmock/gmock.h"
Expand Down Expand Up @@ -137,12 +138,10 @@ TEST(TensorTest, EmptyTensorTest) {
ASSERT_STREQ(location.name, CPU);
EXPECT_EQ(location.id, 0);

// arena is disabled for CPUExecutionProvider on x86 and JEMalloc
#if (defined(__amd64__) || defined(_M_AMD64) || defined(__aarch64__) || defined(__loongarch__) || defined(_M_ARM64)) && !defined(USE_JEMALLOC) && !defined(USE_MIMALLOC) && !defined(ABSL_HAVE_ADDRESS_SANITIZER)
EXPECT_EQ(location.alloc_type, OrtAllocatorType::OrtArenaAllocator);
#else
EXPECT_EQ(location.alloc_type, OrtAllocatorType::OrtDeviceAllocator);
#endif
const auto expected_allocator_type = DoesCpuAllocatorSupportArenaUsage()
? OrtAllocatorType::OrtArenaAllocator
: OrtAllocatorType::OrtDeviceAllocator;
EXPECT_EQ(location.alloc_type, expected_allocator_type);
}

TEST(TensorTest, StringTensorTest) {
Expand Down

0 comments on commit 4a70cbd

Please sign in to comment.