|
| 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 <gtest/gtest.h> |
| 10 | + |
| 11 | +#include <executorch/kernels/portable/cpu/util/allocate_tensor_util.h> |
| 12 | +#include <executorch/runtime/kernel/kernel_includes.h> |
| 13 | +#include <executorch/runtime/platform/runtime.h> |
| 14 | +#include <executorch/test/utils/DeathTest.h> |
| 15 | +using ScalarType = exec_aten::ScalarType; |
| 16 | + |
| 17 | +class AllocateTest : public ::testing::Test { |
| 18 | + protected: |
| 19 | + void SetUp() override { |
| 20 | + // Since these tests cause ET_LOG to be called, the PAL must be initialized |
| 21 | + // first. |
| 22 | + torch::executor::runtime_init(); |
| 23 | + } |
| 24 | +}; |
| 25 | + |
| 26 | +TEST(AllocateTest, AllocateTensor) { |
| 27 | + uint8_t* temp_allocator_ptr = (uint8_t*)malloc(2048); |
| 28 | + executorch::runtime::MemoryAllocator temp_allocator(2048, temp_allocator_ptr); |
| 29 | + executorch::runtime::KernelRuntimeContext ctx(nullptr, &temp_allocator); |
| 30 | + |
| 31 | + executorch::aten::SizesType sizes[3] = {1, 2, 3}; |
| 32 | + executorch::aten::DimOrderType dim_order[3] = {0, 1, 2}; |
| 33 | + executorch::aten::StridesType strides[3] = {3, 3, 1}; |
| 34 | + |
| 35 | + torch::executor::ArrayRef<exec_aten::SizesType> sizes_ref(sizes, 3); |
| 36 | + torch::executor::ArrayRef<exec_aten::StridesType> strides_ref(strides, 3); |
| 37 | + torch::executor::ArrayRef<exec_aten::DimOrderType> dim_orders_ref( |
| 38 | + dim_order, 3); |
| 39 | + |
| 40 | + torch::executor::allocate_tensor( |
| 41 | + ctx, sizes, dim_order, strides, ScalarType::Float); |
| 42 | + |
| 43 | + free(temp_allocator_ptr); |
| 44 | +} |
| 45 | + |
| 46 | +TEST(AllocateTest, FailAllocateTensor) { |
| 47 | + torch::executor::runtime_init(); |
| 48 | + |
| 49 | + uint8_t* temp_allocator_ptr = (uint8_t*)malloc(16); |
| 50 | + executorch::runtime::MemoryAllocator temp_allocator(16, temp_allocator_ptr); |
| 51 | + executorch::runtime::KernelRuntimeContext ctx(nullptr, &temp_allocator); |
| 52 | + |
| 53 | + executorch::aten::SizesType sizes[3] = {1, 2, 3}; |
| 54 | + executorch::aten::DimOrderType dim_order[3] = {0, 1, 2}; |
| 55 | + executorch::aten::StridesType strides[3] = {3, 3, 1}; |
| 56 | + |
| 57 | + torch::executor::ArrayRef<exec_aten::SizesType> sizes_ref(sizes, 3); |
| 58 | + torch::executor::ArrayRef<exec_aten::StridesType> strides_ref(strides, 3); |
| 59 | + torch::executor::ArrayRef<exec_aten::DimOrderType> dim_orders_ref( |
| 60 | + dim_order, 3); |
| 61 | + |
| 62 | + ET_EXPECT_DEATH( |
| 63 | + torch::executor::allocate_tensor( |
| 64 | + ctx, sizes, dim_order, strides, ScalarType::Float), |
| 65 | + "Failed to malloc"); |
| 66 | + |
| 67 | + free(temp_allocator_ptr); |
| 68 | +} |
0 commit comments