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::optional;
20 using exec_aten::ScalarType;
21 using exec_aten::Tensor;
22 using torch::executor::testing::TensorFactory;
23
24 Tensor&
op_prod_out(const Tensor & self,optional<ScalarType> dtype,Tensor & out)25 op_prod_out(const Tensor& self, optional<ScalarType> dtype, Tensor& out) {
26 executorch::runtime::KernelRuntimeContext context{};
27 return torch::executor::aten::prod_outf(context, self, dtype, out);
28 }
29
op_prod_int_out(const Tensor & self,int64_t dim,bool keepdim,optional<ScalarType> dtype,Tensor & out)30 Tensor& op_prod_int_out(
31 const Tensor& self,
32 int64_t dim,
33 bool keepdim,
34 optional<ScalarType> dtype,
35 Tensor& out) {
36 executorch::runtime::KernelRuntimeContext context{};
37 return torch::executor::aten::prod_outf(
38 context, self, dim, keepdim, dtype, out);
39 }
40
41 class OpProdOutTest : public ::testing::Test {
42 protected:
SetUp()43 void SetUp() override {
44 // Since these tests cause ET_LOG to be called, the PAL must be initialized
45 // first.
46 torch::executor::runtime_init();
47 }
48 };
49
50 class OpProdIntOutTest : public ::testing::Test {
51 protected:
SetUp()52 void SetUp() override {
53 // Since these tests cause ET_LOG to be called, the PAL must be initialized
54 // first.
55 torch::executor::runtime_init();
56 }
57 };
58
TEST_F(OpProdOutTest,SmokeTest)59 TEST_F(OpProdOutTest, SmokeTest) {
60 TensorFactory<ScalarType::Float> tfFloat;
61
62 Tensor self = tfFloat.make({2, 3}, {1, 2, 3, 4, 5, 6});
63 optional<ScalarType> dtype{};
64 Tensor out = tfFloat.zeros({});
65 Tensor out_expected = tfFloat.make({}, {720});
66 op_prod_out(self, dtype, out);
67 EXPECT_TENSOR_CLOSE(out, out_expected);
68 }
69
TEST_F(OpProdIntOutTest,SmokeTest)70 TEST_F(OpProdIntOutTest, SmokeTest) {
71 TensorFactory<ScalarType::Float> tfFloat;
72
73 Tensor self = tfFloat.make({2, 3}, {1, 2, 3, 4, 5, 6});
74 int64_t dim = 0;
75 bool keepdim = false;
76 optional<ScalarType> dtype{};
77 Tensor out = tfFloat.zeros({3});
78 Tensor out_expected = tfFloat.make({3}, {4, 10, 18});
79 op_prod_int_out(self, dim, keepdim, dtype, out);
80 EXPECT_TENSOR_CLOSE(out, out_expected);
81 }
82
TEST_F(OpProdIntOutTest,SmokeTestKeepdim)83 TEST_F(OpProdIntOutTest, SmokeTestKeepdim) {
84 TensorFactory<ScalarType::Float> tfFloat;
85
86 Tensor self = tfFloat.make({2, 3}, {1, 2, 3, 4, 5, 6});
87 int64_t dim = 0;
88 bool keepdim = true;
89 optional<ScalarType> dtype{};
90 Tensor out = tfFloat.zeros({1, 3});
91 Tensor out_expected = tfFloat.make({1, 3}, {4, 10, 18});
92 op_prod_int_out(self, dim, keepdim, dtype, out);
93 EXPECT_TENSOR_CLOSE(out, out_expected);
94 }
95