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