|
| 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 | + |
| 15 | +#include <gtest/gtest.h> |
| 16 | + |
| 17 | +using namespace ::testing; |
| 18 | +using executorch::aten::ScalarType; |
| 19 | +using executorch::aten::Tensor; |
| 20 | +using torch::executor::testing::TensorFactory; |
| 21 | + |
| 22 | +class OpConjPhysicalOutTest : public OperatorTest { |
| 23 | + protected: |
| 24 | + Tensor& op_conj_physical_out(const Tensor& in, Tensor& out) { |
| 25 | + return torch::executor::aten::_conj_physical_outf(context_, in, out); |
| 26 | + } |
| 27 | +}; |
| 28 | + |
| 29 | +TEST_F(OpConjPhysicalOutTest, ComplexFloatBasic) { |
| 30 | + TensorFactory<ScalarType::ComplexFloat> tf; |
| 31 | + |
| 32 | + const std::vector<int32_t> sizes = {2, 2}; |
| 33 | + |
| 34 | + // Create input: (1+2i), (3+4i), (5-6i), (-7+8i) |
| 35 | + Tensor in = tf.make( |
| 36 | + sizes, |
| 37 | + {executorch::aten::complex<float>(1.0f, 2.0f), |
| 38 | + executorch::aten::complex<float>(3.0f, 4.0f), |
| 39 | + executorch::aten::complex<float>(5.0f, -6.0f), |
| 40 | + executorch::aten::complex<float>(-7.0f, 8.0f)}); |
| 41 | + |
| 42 | + Tensor out = tf.zeros(sizes); |
| 43 | + |
| 44 | + op_conj_physical_out(in, out); |
| 45 | + |
| 46 | + // Expected: (1-2i), (3-4i), (5+6i), (-7-8i) |
| 47 | + Tensor expected = tf.make( |
| 48 | + sizes, |
| 49 | + {executorch::aten::complex<float>(1.0f, -2.0f), |
| 50 | + executorch::aten::complex<float>(3.0f, -4.0f), |
| 51 | + executorch::aten::complex<float>(5.0f, 6.0f), |
| 52 | + executorch::aten::complex<float>(-7.0f, -8.0f)}); |
| 53 | + |
| 54 | + EXPECT_TENSOR_EQ(out, expected); |
| 55 | +} |
| 56 | + |
| 57 | +TEST_F(OpConjPhysicalOutTest, ComplexDoubleBasic) { |
| 58 | + TensorFactory<ScalarType::ComplexDouble> tf; |
| 59 | + |
| 60 | + const std::vector<int32_t> sizes = {3}; |
| 61 | + |
| 62 | + Tensor in = tf.make( |
| 63 | + sizes, |
| 64 | + {executorch::aten::complex<double>(1.5, 2.5), |
| 65 | + executorch::aten::complex<double>(-3.5, 4.5), |
| 66 | + executorch::aten::complex<double>(0.0, -1.0)}); |
| 67 | + |
| 68 | + Tensor out = tf.zeros(sizes); |
| 69 | + |
| 70 | + op_conj_physical_out(in, out); |
| 71 | + |
| 72 | + Tensor expected = tf.make( |
| 73 | + sizes, |
| 74 | + {executorch::aten::complex<double>(1.5, -2.5), |
| 75 | + executorch::aten::complex<double>(-3.5, -4.5), |
| 76 | + executorch::aten::complex<double>(0.0, 1.0)}); |
| 77 | + |
| 78 | + EXPECT_TENSOR_EQ(out, expected); |
| 79 | +} |
| 80 | + |
| 81 | +TEST_F(OpConjPhysicalOutTest, RealPartOnly) { |
| 82 | + TensorFactory<ScalarType::ComplexFloat> tf; |
| 83 | + |
| 84 | + const std::vector<int32_t> sizes = {2}; |
| 85 | + |
| 86 | + // When imaginary part is zero, conjugate negates the imaginary part (0 -> -0) |
| 87 | + // Both are mathematically equivalent, so we verify values directly |
| 88 | + Tensor in = tf.make( |
| 89 | + sizes, |
| 90 | + {executorch::aten::complex<float>(5.0f, 0.0f), |
| 91 | + executorch::aten::complex<float>(-3.0f, 0.0f)}); |
| 92 | + |
| 93 | + Tensor out = tf.zeros(sizes); |
| 94 | + |
| 95 | + op_conj_physical_out(in, out); |
| 96 | + |
| 97 | + // Verify real parts are unchanged and imaginary parts are negated zeros |
| 98 | + const auto* out_data = out.const_data_ptr<executorch::aten::complex<float>>(); |
| 99 | + EXPECT_EQ(out_data[0].real_, 5.0f); |
| 100 | + EXPECT_EQ(out_data[0].imag_, -0.0f); |
| 101 | + EXPECT_EQ(out_data[1].real_, -3.0f); |
| 102 | + EXPECT_EQ(out_data[1].imag_, -0.0f); |
| 103 | +} |
| 104 | + |
| 105 | +TEST_F(OpConjPhysicalOutTest, ImaginaryPartOnly) { |
| 106 | + TensorFactory<ScalarType::ComplexFloat> tf; |
| 107 | + |
| 108 | + const std::vector<int32_t> sizes = {2}; |
| 109 | + |
| 110 | + Tensor in = tf.make( |
| 111 | + sizes, |
| 112 | + {executorch::aten::complex<float>(0.0f, 5.0f), |
| 113 | + executorch::aten::complex<float>(0.0f, -3.0f)}); |
| 114 | + |
| 115 | + Tensor out = tf.zeros(sizes); |
| 116 | + |
| 117 | + op_conj_physical_out(in, out); |
| 118 | + |
| 119 | + Tensor expected = tf.make( |
| 120 | + sizes, |
| 121 | + {executorch::aten::complex<float>(0.0f, -5.0f), |
| 122 | + executorch::aten::complex<float>(0.0f, 3.0f)}); |
| 123 | + |
| 124 | + EXPECT_TENSOR_EQ(out, expected); |
| 125 | +} |
| 126 | + |
| 127 | +TEST_F(OpConjPhysicalOutTest, EmptyTensor) { |
| 128 | + TensorFactory<ScalarType::ComplexFloat> tf; |
| 129 | + |
| 130 | + const std::vector<int32_t> sizes = {0}; |
| 131 | + |
| 132 | + Tensor in = tf.make(sizes, {}); |
| 133 | + Tensor out = tf.zeros(sizes); |
| 134 | + |
| 135 | + op_conj_physical_out(in, out); |
| 136 | + |
| 137 | + EXPECT_EQ(out.numel(), 0); |
| 138 | +} |
| 139 | + |
| 140 | +TEST_F(OpConjPhysicalOutTest, MismatchedDtypeDies) { |
| 141 | + TensorFactory<ScalarType::ComplexFloat> tf_in; |
| 142 | + TensorFactory<ScalarType::ComplexDouble> tf_out; |
| 143 | + |
| 144 | + const std::vector<int32_t> sizes = {2}; |
| 145 | + |
| 146 | + Tensor in = tf_in.make( |
| 147 | + sizes, |
| 148 | + {executorch::aten::complex<float>(1.0f, 2.0f), |
| 149 | + executorch::aten::complex<float>(3.0f, 4.0f)}); |
| 150 | + Tensor out = tf_out.zeros(sizes); |
| 151 | + |
| 152 | + ET_EXPECT_KERNEL_FAILURE(context_, op_conj_physical_out(in, out)); |
| 153 | +} |
0 commit comments