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 #include <gtest/gtest.h>
17
18 using namespace ::testing;
19 using exec_aten::ArrayRef;
20 using exec_aten::optional;
21 using exec_aten::ScalarType;
22 using exec_aten::Tensor;
23 using torch::executor::testing::TensorFactory;
24
25 class OpArgminTest : public OperatorTest {
26 protected:
op_argmin_out(const Tensor & in,optional<int64_t> dim,bool keepdim,Tensor & out)27 Tensor& op_argmin_out(
28 const Tensor& in,
29 optional<int64_t> dim,
30 bool keepdim,
31 Tensor& out) {
32 return torch::executor::aten::argmin_outf(context_, in, dim, keepdim, out);
33 }
34 };
35
TEST_F(OpArgminTest,SanityCheckLong)36 TEST_F(OpArgminTest, SanityCheckLong) {
37 TensorFactory<ScalarType::Long> tf;
38
39 // clang-format off
40 Tensor in = tf.make(
41 { 2, 3, 4 },
42 { 1, 4, 1, 6,
43 5, 8, 5, 6,
44 5, 3, 9, 2,
45
46 3, 9, 1, 4,
47 9, 7, 5, 5,
48 7, 7, 6, 3 });
49
50 Tensor out = tf.zeros({2, 4});
51 Tensor expected = tf.make({2, 4}, {
52 0, 2, 0, 2,
53 0, 1, 0, 2 });
54 Tensor ret = op_argmin_out(in, 1, false, out);
55
56 EXPECT_TENSOR_EQ(out, ret);
57 EXPECT_TENSOR_EQ(out, expected);
58 // clang-format on
59 }
60
TEST_F(OpArgminTest,SanityCheckShort)61 TEST_F(OpArgminTest, SanityCheckShort) {
62 TensorFactory<ScalarType::Long> tfl;
63 TensorFactory<ScalarType::Short> tfs;
64
65 // clang-format off
66 Tensor in = tfs.make(
67 { 2, 3, 4 },
68 { 1, 4, 1, 6,
69 5, 8, 5, 6,
70 5, 3, 9, 2,
71
72 3, 9, 1, 4,
73 9, 7, 5, 5,
74 7, 7, 6, 3 });
75
76 Tensor out = tfl.zeros({2, 4});
77 Tensor expected = tfl.make({2, 4}, {
78 0, 2, 0, 2,
79 0, 1, 0, 2 });
80 Tensor ret = op_argmin_out(in, 1, false, out);
81
82 EXPECT_TENSOR_EQ(out, ret);
83 EXPECT_TENSOR_EQ(out, expected);
84 // clang-format on
85 }
86
TEST_F(OpArgminTest,SanityCheckNullDim)87 TEST_F(OpArgminTest, SanityCheckNullDim) {
88 TensorFactory<ScalarType::Long> tf;
89
90 // clang-format off
91 Tensor in = tf.make(
92 { 2, 3, 4 },
93 { 9, 4, 1, 6,
94 5, 8, 5, 6,
95 5, 3, 9, 2,
96
97 3, 9, 1, 4,
98 9, 7, 5, 5,
99 7, 7, 6, 3 });
100
101 Tensor out = tf.zeros({});
102 Tensor expected = tf.make({}, {2});
103
104 optional<int64_t> dim;
105 Tensor ret = op_argmin_out(in, dim, false, out);
106
107 EXPECT_TENSOR_EQ(out, ret);
108 EXPECT_TENSOR_EQ(out, expected);
109 // clang-format on
110 }
111