|
| 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/kernels/test/FunctionHeaderWrapper.h> // Declares the operator |
| 10 | +#include <executorch/kernels/test/TestUtil.h> |
| 11 | +#include <executorch/kernels/test/supported_features.h> |
| 12 | +#include <executorch/runtime/core/exec_aten/exec_aten.h> |
| 13 | +#include <executorch/runtime/core/exec_aten/testing_util/tensor_factory.h> |
| 14 | +#include <executorch/runtime/core/exec_aten/testing_util/tensor_util.h> |
| 15 | +#include <executorch/runtime/core/exec_aten/util/scalar_type_util.h> |
| 16 | + |
| 17 | +#include <gtest/gtest.h> |
| 18 | + |
| 19 | +using namespace ::testing; |
| 20 | +using exec_aten::DimOrderType; |
| 21 | +using exec_aten::IntArrayRef; |
| 22 | +using exec_aten::optional; |
| 23 | +using exec_aten::OptionalArrayRef; |
| 24 | +using exec_aten::ScalarType; |
| 25 | +using exec_aten::Tensor; |
| 26 | +using torch::executor::testing::TensorFactory; |
| 27 | + |
| 28 | +class OpEmptyDimOrderOutTest : public OperatorTest { |
| 29 | + protected: |
| 30 | + Tensor& op_empty_dim_order_out( |
| 31 | + IntArrayRef size, |
| 32 | + OptionalArrayRef<int64_t> dim_order, |
| 33 | + Tensor& out) { |
| 34 | + return torch::executor::dim_order_ops::_empty_dim_order_outf( |
| 35 | + context_, size, dim_order, out); |
| 36 | + } |
| 37 | + |
| 38 | + template <ScalarType DTYPE> |
| 39 | + void test_op_empty_dim_order_out(std::vector<int32_t>&& size_int32_t) { |
| 40 | + TensorFactory<DTYPE> tf; |
| 41 | + std::vector<int64_t> sizes(size_int32_t.begin(), size_int32_t.end()); |
| 42 | + auto aref = exec_aten::ArrayRef<int64_t>(sizes.data(), sizes.size()); |
| 43 | + OptionalArrayRef<int64_t> dim_order; |
| 44 | + Tensor out = tf.ones(size_int32_t); |
| 45 | + |
| 46 | + op_empty_dim_order_out(aref, dim_order, out); |
| 47 | + } |
| 48 | + |
| 49 | + void too_short_dim_order_die() { |
| 50 | + TensorFactory<ScalarType::Float> tf; |
| 51 | + |
| 52 | + int64_t sizes[3] = {3, 2, 4}; |
| 53 | + auto sizes_aref = exec_aten::ArrayRef<int64_t>(sizes); |
| 54 | + |
| 55 | + int64_t raw_dim_order[2] = {0, 1}; |
| 56 | + auto dim_order = OptionalArrayRef<int64_t>(raw_dim_order); |
| 57 | + Tensor out = |
| 58 | + tf.ones({3, 2, 4}, torch::executor::TensorShapeDynamism::DYNAMIC_BOUND); |
| 59 | + ET_EXPECT_KERNEL_FAILURE( |
| 60 | + context_, op_empty_dim_order_out(sizes_aref, dim_order, out)); |
| 61 | + } |
| 62 | + |
| 63 | + void illegal_dim_order_die() { |
| 64 | + TensorFactory<ScalarType::Float> tf; |
| 65 | + |
| 66 | + int64_t sizes[2] = {3, 2}; |
| 67 | + auto sizes_aref = exec_aten::ArrayRef<int64_t>(sizes); |
| 68 | + |
| 69 | + int64_t raw_dim_order[2] = {1, 2}; |
| 70 | + auto dim_order = OptionalArrayRef<int64_t>(raw_dim_order); |
| 71 | + Tensor out = |
| 72 | + tf.ones({3, 2}, torch::executor::TensorShapeDynamism::DYNAMIC_BOUND); |
| 73 | + ET_EXPECT_KERNEL_FAILURE( |
| 74 | + context_, op_empty_dim_order_out(sizes_aref, dim_order, out)); |
| 75 | + } |
| 76 | + |
| 77 | + void wrong_dim_order_die() { |
| 78 | + TensorFactory<ScalarType::Float> tf; |
| 79 | + |
| 80 | + int64_t sizes[4] = {3, 2, 4, 5}; |
| 81 | + auto sizes_aref = exec_aten::ArrayRef<int64_t>(sizes); |
| 82 | + |
| 83 | + // should be {0, 2, 3, 1} |
| 84 | + int64_t raw_dim_order[4] = {0, 1, 2, 3}; |
| 85 | + auto dim_order = OptionalArrayRef<int64_t>(raw_dim_order); |
| 86 | + Tensor out = tf.full_channels_last( |
| 87 | + {3, 2, 4, 5}, 1, torch::executor::TensorShapeDynamism::DYNAMIC_BOUND); |
| 88 | + ET_EXPECT_KERNEL_FAILURE( |
| 89 | + context_, op_empty_dim_order_out(sizes_aref, dim_order, out)); |
| 90 | + } |
| 91 | +}; |
| 92 | + |
| 93 | +#define GENERATE_TEST(_, DTYPE) \ |
| 94 | + TEST_F(OpEmptyDimOrderOutTest, DTYPE##Tensors) { \ |
| 95 | + test_op_empty_dim_order_out<ScalarType::DTYPE>({2, 3, 4}); \ |
| 96 | + test_op_empty_dim_order_out<ScalarType::DTYPE>({2, 0, 4}); \ |
| 97 | + test_op_empty_dim_order_out<ScalarType::DTYPE>({}); \ |
| 98 | + } |
| 99 | + |
| 100 | +ET_FORALL_REAL_TYPES_AND(Bool, GENERATE_TEST) |
| 101 | + |
| 102 | +TEST_F(OpEmptyDimOrderOutTest, DynamicShapeUpperBoundSameAsExpected) { |
| 103 | + TensorFactory<ScalarType::Float> tf; |
| 104 | + |
| 105 | + int64_t sizes[2] = {3, 2}; |
| 106 | + auto sizes_aref = exec_aten::ArrayRef<int64_t>(sizes); |
| 107 | + OptionalArrayRef<int64_t> dim_order; |
| 108 | + Tensor out = |
| 109 | + tf.ones({3, 2}, torch::executor::TensorShapeDynamism::DYNAMIC_BOUND); |
| 110 | + op_empty_dim_order_out(sizes_aref, dim_order, out); |
| 111 | +} |
| 112 | + |
| 113 | +TEST_F(OpEmptyDimOrderOutTest, ContiguousDimOrderSuccees) { |
| 114 | + TensorFactory<ScalarType::Float> tf; |
| 115 | + |
| 116 | + int64_t sizes[2] = {3, 2}; |
| 117 | + auto sizes_aref = exec_aten::ArrayRef<int64_t>(sizes); |
| 118 | + |
| 119 | + int64_t raw_dim_order[2] = {0, 1}; |
| 120 | + auto dim_order = OptionalArrayRef<int64_t>(raw_dim_order); |
| 121 | + Tensor out = |
| 122 | + tf.ones({3, 2}, torch::executor::TensorShapeDynamism::DYNAMIC_BOUND); |
| 123 | + op_empty_dim_order_out(sizes_aref, dim_order, out); |
| 124 | +} |
| 125 | + |
| 126 | +TEST_F(OpEmptyDimOrderOutTest, ChannelsLastsDimOrderSuccees) { |
| 127 | + TensorFactory<ScalarType::Float> tf; |
| 128 | + |
| 129 | + int64_t sizes[4] = {3, 2, 4, 5}; |
| 130 | + auto sizes_aref = exec_aten::ArrayRef<int64_t>(sizes); |
| 131 | + |
| 132 | + int64_t raw_dim_order[4] = {0, 2, 3, 1}; |
| 133 | + auto dim_order = OptionalArrayRef<int64_t>(raw_dim_order); |
| 134 | + Tensor out = tf.full_channels_last( |
| 135 | + {3, 2, 4, 5}, 1, torch::executor::TensorShapeDynamism::DYNAMIC_BOUND); |
| 136 | + op_empty_dim_order_out(sizes_aref, dim_order, out); |
| 137 | +} |
| 138 | + |
| 139 | +TEST_F(OpEmptyDimOrderOutTest, DynamicShapeUpperBoundLargerThanExpected) { |
| 140 | + TensorFactory<ScalarType::Float> tf; |
| 141 | + |
| 142 | + int64_t sizes[2] = {3, 2}; |
| 143 | + auto sizes_aref = exec_aten::ArrayRef<int64_t>(sizes); |
| 144 | + OptionalArrayRef<int64_t> dim_order; |
| 145 | + Tensor out = |
| 146 | + tf.ones({10, 10}, torch::executor::TensorShapeDynamism::DYNAMIC_BOUND); |
| 147 | + op_empty_dim_order_out(sizes_aref, dim_order, out); |
| 148 | +} |
| 149 | + |
| 150 | +TEST_F(OpEmptyDimOrderOutTest, DynamicShapeUnbound) { |
| 151 | + if (!torch::executor::testing::SupportedFeatures::get()->output_resize) { |
| 152 | + GTEST_SKIP() << "Dynamic shape unbound not supported"; |
| 153 | + } |
| 154 | + TensorFactory<ScalarType::Float> tf; |
| 155 | + |
| 156 | + int64_t sizes[2] = {3, 2}; |
| 157 | + auto sizes_aref = exec_aten::ArrayRef<int64_t>(sizes); |
| 158 | + OptionalArrayRef<int64_t> dim_order; |
| 159 | + Tensor out = |
| 160 | + tf.ones({1, 1}, torch::executor::TensorShapeDynamism::DYNAMIC_UNBOUND); |
| 161 | + op_empty_dim_order_out(sizes_aref, dim_order, out); |
| 162 | +} |
0 commit comments