|
| 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/supported_features.h> |
| 11 | +#include <executorch/runtime/core/exec_aten/exec_aten.h> |
| 12 | +#include <executorch/runtime/core/exec_aten/testing_util/tensor_factory.h> |
| 13 | +#include <executorch/runtime/core/exec_aten/testing_util/tensor_util.h> |
| 14 | +#include <executorch/runtime/core/exec_aten/util/tensor_util.h> |
| 15 | + |
| 16 | +#include <executorch/kernels/test/TestUtil.h> |
| 17 | + |
| 18 | +#include <gtest/gtest.h> |
| 19 | + |
| 20 | +using namespace ::testing; |
| 21 | +using executorch::aten::ScalarType; |
| 22 | +using executorch::aten::Tensor; |
| 23 | +using torch::executor::testing::TensorFactory; |
| 24 | + |
| 25 | +class OpUnfoldTest : public OperatorTest { |
| 26 | + protected: |
| 27 | + Tensor& op_unfold_copy_out( |
| 28 | + const Tensor& self, |
| 29 | + int64_t dim, |
| 30 | + int64_t size, |
| 31 | + int64_t step, |
| 32 | + Tensor& out) { |
| 33 | + return torch::executor::aten::unfold_copy_outf( |
| 34 | + context_, self, dim, size, step, out); |
| 35 | + } |
| 36 | + |
| 37 | + template <class CTYPE, ScalarType DTYPE> |
| 38 | + void test_unfold_copy_dtype() { |
| 39 | + TensorFactory<DTYPE> tf; |
| 40 | + |
| 41 | + auto input = tf.make({3, 3}, {1, 2, 3, 4, 5, 6, 7, 8, 9}); |
| 42 | + auto expected = tf.make({3, 2, 2}, {1, 2, 2, 3, 4, 5, 5, 6, 7, 8, 8, 9}); |
| 43 | + auto actual_out = tf.zeros_like(expected); |
| 44 | + op_unfold_copy_out(input, /*dim=*/1, /*size=*/2, /*step=*/1, actual_out); |
| 45 | + EXPECT_TENSOR_CLOSE(actual_out, expected); |
| 46 | + } |
| 47 | +}; |
| 48 | + |
| 49 | +TEST_F(OpUnfoldTest, SmokeTest) { |
| 50 | + TensorFactory<ScalarType::Float> tf; |
| 51 | + const auto input = tf.make({3, 3}, {1, 2, 3, 4, 5, 6, 7, 8, 9}); |
| 52 | + const auto expected = tf.make({3, 1, 2}, {1, 2, 4, 5, 7, 8}); |
| 53 | + auto output = tf.zeros_like(expected); |
| 54 | + |
| 55 | + op_unfold_copy_out(input, /*dim=*/1, /*size=*/2, /*step=*/2, output); |
| 56 | + EXPECT_TENSOR_CLOSE(output, expected); |
| 57 | +} |
| 58 | + |
| 59 | +TEST_F(OpUnfoldTest, DType) { |
| 60 | +#define TEST_ENTRY(ctype, dtype) \ |
| 61 | + test_unfold_copy_dtype<ctype, ScalarType::dtype>(); |
| 62 | + ET_FORALL_REALHBF16_TYPES(TEST_ENTRY); |
| 63 | +#undef TEST_ENTRY |
| 64 | +} |
| 65 | + |
| 66 | +TEST_F(OpUnfoldTest, ZeroDimension) { |
| 67 | + TensorFactory<ScalarType::Float> tf; |
| 68 | + const auto input = tf.make({3, 3}, {1, 2, 3, 4, 5, 6, 7, 8, 9}); |
| 69 | + const auto expected = |
| 70 | + tf.make({2, 3, 2}, {1, 4, 2, 5, 3, 6, 4, 7, 5, 8, 6, 9}); |
| 71 | + auto output = tf.zeros_like(expected); |
| 72 | + |
| 73 | + op_unfold_copy_out(input, /*dim=*/0, /*size=*/2, /*step=*/1, output); |
| 74 | + EXPECT_TENSOR_CLOSE(output, expected); |
| 75 | +} |
| 76 | + |
| 77 | +TEST_F(OpUnfoldTest, NegativeDimension) { |
| 78 | + TensorFactory<ScalarType::Float> tf; |
| 79 | + const auto input = tf.make({3, 3}, {1, 2, 3, 4, 5, 6, 7, 8, 9}); |
| 80 | + const auto expected = tf.make({3, 1, 2}, {1, 2, 4, 5, 7, 8}); |
| 81 | + auto output = tf.zeros_like(expected); |
| 82 | + |
| 83 | + op_unfold_copy_out(input, /*dim=*/-1, /*size=*/2, /*step=*/2, output); |
| 84 | + EXPECT_TENSOR_CLOSE(output, expected); |
| 85 | +} |
| 86 | + |
| 87 | +TEST_F(OpUnfoldTest, LargeStep) { |
| 88 | + TensorFactory<ScalarType::Float> tf; |
| 89 | + const auto input = tf.make({3, 3}, {1, 2, 3, 4, 5, 6, 7, 8, 9}); |
| 90 | + const auto expected = tf.make({3, 1, 2}, {1, 2, 4, 5, 7, 8}); |
| 91 | + auto output = tf.zeros_like(expected); |
| 92 | + |
| 93 | + op_unfold_copy_out(input, /*dim=*/-1, /*size=*/2, /*step=*/5, output); |
| 94 | + EXPECT_TENSOR_CLOSE(output, expected); |
| 95 | +} |
| 96 | + |
| 97 | +TEST_F(OpUnfoldTest, ZeroSize) { |
| 98 | + TensorFactory<ScalarType::Float> tf; |
| 99 | + const auto input = tf.make({3, 3}, {1, 2, 3, 4, 5, 6, 7, 8, 9}); |
| 100 | + const auto expected = tf.make({3, 4, 0}, {}); |
| 101 | + auto output = tf.zeros_like(expected); |
| 102 | + |
| 103 | + op_unfold_copy_out(input, /*dim=*/1, /*size=*/0, /*step=*/1, output); |
| 104 | + EXPECT_TENSOR_CLOSE(output, expected); |
| 105 | +} |
| 106 | + |
| 107 | +TEST_F(OpUnfoldTest, NegativeSizeAndNegativeStepDies) { |
| 108 | + TensorFactory<ScalarType::Float> tf; |
| 109 | + const auto input = tf.make({3, 3}, {1, 2, 3, 4, 5, 6, 7, 8, 9}); |
| 110 | + auto output = tf.zeros({3, 1, 2}); |
| 111 | + |
| 112 | + ET_EXPECT_KERNEL_FAILURE( |
| 113 | + context_, |
| 114 | + op_unfold_copy_out(input, /*dim=*/1, /*size=*/-1, /*step=*/1, output)); |
| 115 | + ET_EXPECT_KERNEL_FAILURE( |
| 116 | + context_, |
| 117 | + op_unfold_copy_out(input, /*dim=*/1, /*size=*/1, /*step=*/-1, output)); |
| 118 | +} |
| 119 | + |
| 120 | +TEST_F(OpUnfoldTest, InvalidDimAndSizeTooLargeDies) { |
| 121 | + TensorFactory<ScalarType::Float> tf; |
| 122 | + const auto input = tf.make({3, 3}, {1, 2, 3, 4, 5, 6, 7, 8, 9}); |
| 123 | + auto output = tf.zeros({3, 1, 2}); |
| 124 | + ET_EXPECT_KERNEL_FAILURE( |
| 125 | + context_, |
| 126 | + op_unfold_copy_out(input, /*dim=*/3, /*size=*/2, /*step=*/1, output)); |
| 127 | + ET_EXPECT_KERNEL_FAILURE( |
| 128 | + context_, |
| 129 | + op_unfold_copy_out(input, /*dim=*/1, /*size=*/10, /*step=*/1, output)); |
| 130 | +} |
0 commit comments