xref: /aosp_15_r20/external/executorch/kernels/test/op_diagonal_copy_test.cpp (revision 523fa7a60841cd1ecfb9cc4201f1ca8b03ed023a)
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/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/platform/runtime.h>
15 
16 #include <gtest/gtest.h>
17 
18 using namespace ::testing;
19 using exec_aten::IntArrayRef;
20 using exec_aten::ScalarType;
21 using exec_aten::Tensor;
22 using torch::executor::testing::TensorFactory;
23 
op_diagonal_copy_out(const Tensor & input,int64_t offset,int64_t dim1,int64_t dim2,Tensor & out)24 Tensor& op_diagonal_copy_out(
25     const Tensor& input,
26     int64_t offset,
27     int64_t dim1,
28     int64_t dim2,
29     Tensor& out) {
30   executorch::runtime::KernelRuntimeContext context{};
31   return torch::executor::aten::diagonal_copy_outf(
32       context, input, offset, dim1, dim2, out);
33 }
34 
35 class OpDiagonalCopyOutTest : public ::testing::Test {
36  protected:
SetUp()37   void SetUp() override {
38     // Since these tests cause ET_LOG to be called, the PAL must be initialized
39     // first.
40     torch::executor::runtime_init();
41   }
42 };
43 
TEST_F(OpDiagonalCopyOutTest,SmokeTest2D)44 TEST_F(OpDiagonalCopyOutTest, SmokeTest2D) {
45   TensorFactory<ScalarType::Float> tfFloat;
46 
47   Tensor input = tfFloat.make({3, 4}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12});
48   Tensor out = tfFloat.zeros({2});
49   Tensor out_expected = tfFloat.make({2}, {5, 10});
50   op_diagonal_copy_out(input, 1, 1, 0, out);
51   EXPECT_TENSOR_CLOSE(out, out_expected);
52 }
53 
TEST_F(OpDiagonalCopyOutTest,SmokeTest3D)54 TEST_F(OpDiagonalCopyOutTest, SmokeTest3D) {
55   TensorFactory<ScalarType::Float> tfFloat;
56 
57   Tensor input =
58       tfFloat.make({2, 3, 2}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12});
59   Tensor out = tfFloat.zeros({3, 1});
60   Tensor out_expected = tfFloat.make({3, 1}, {7, 9, 11});
61   op_diagonal_copy_out(input, -1, 0, -1, out);
62   EXPECT_TENSOR_CLOSE(out, out_expected);
63 }
64 
TEST_F(OpDiagonalCopyOutTest,SmokeTest4D)65 TEST_F(OpDiagonalCopyOutTest, SmokeTest4D) {
66   TensorFactory<ScalarType::Float> tfFloat;
67 
68   Tensor input =
69       tfFloat.make({2, 1, 2, 3}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12});
70   Tensor out = tfFloat.zeros({1, 3, 2});
71   Tensor out_expected = tfFloat.make({1, 3, 2}, {1, 10, 2, 11, 3, 12});
72   op_diagonal_copy_out(input, 0, 0, 2, out);
73   EXPECT_TENSOR_CLOSE(out, out_expected);
74 }
75