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::ArrayRef;
20 using exec_aten::ScalarType;
21 using exec_aten::Tensor;
22 using torch::executor::testing::TensorFactory;
23
op_pdist_forward_out(const Tensor & input,double p,Tensor & out)24 Tensor& op_pdist_forward_out(const Tensor& input, double p, Tensor& out) {
25 executorch::runtime::KernelRuntimeContext context{};
26 return torch::executor::aten::_pdist_forward_outf(context, input, p, out);
27 }
28
29 class OpPdistForwardOutTest : public ::testing::Test {
30 protected:
SetUp()31 void SetUp() override {
32 // Since these tests cause ET_LOG to be called, the PAL must be initialized
33 // first.
34 torch::executor::runtime_init();
35 }
36 };
37
TEST_F(OpPdistForwardOutTest,SmokeTest)38 TEST_F(OpPdistForwardOutTest, SmokeTest) {
39 TensorFactory<ScalarType::Float> tfFloat;
40
41 Tensor in = tfFloat.make(
42 {4, 5}, {0, 1, 2, 3, 5, 4, 3, 2, -1, 5, 1, 1, -2, 1, 5, 4, 3, 2, -1, 5});
43 Tensor out = tfFloat.zeros({6});
44
45 Tensor l0 = tfFloat.make({6}, {3., 3., 3., 4., 0., 4.});
46 op_pdist_forward_out(in, 0.0, out);
47 EXPECT_TENSOR_CLOSE(out, l0);
48
49 Tensor l0p5 = tfFloat.make(
50 {6},
51 {29.31370926, 19.48528290, 29.31370926, 43.03986740, 0.0, 43.03986740});
52 op_pdist_forward_out(in, 0.5, out);
53 EXPECT_TENSOR_CLOSE(out, l0p5);
54
55 Tensor l1 = tfFloat.make({6}, {10., 7., 10., 11., 0., 11.});
56 op_pdist_forward_out(in, 1.0, out);
57 EXPECT_TENSOR_CLOSE(out, l1);
58
59 Tensor l1p5 = tfFloat.make(
60 {6}, {7.07743692, 5.19140196, 7.07743692, 7.08359480, 0.0, 7.08359480});
61 op_pdist_forward_out(in, 1.5, out);
62 EXPECT_TENSOR_CLOSE(out, l1p5);
63
64 Tensor l2 =
65 tfFloat.make({6}, {6.0, 4.58257580, 6.0, 5.74456263, 0.0, 5.74456263});
66 op_pdist_forward_out(in, 2.0, out);
67 EXPECT_TENSOR_CLOSE(out, l2);
68
69 Tensor l3 = tfFloat.make(
70 {6}, {5.14256334, 4.17933941, 5.14256334, 4.74745941, 0.0, 4.74745941});
71 op_pdist_forward_out(in, 3.0, out);
72 EXPECT_TENSOR_CLOSE(out, l3);
73
74 Tensor linf = tfFloat.make({6}, {4., 4., 4., 4., 0., 4.});
75 op_pdist_forward_out(in, INFINITY, out);
76 EXPECT_TENSOR_CLOSE(out, linf);
77 }
78