|
| 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 <gtest/gtest.h> |
| 10 | +#include <stdio.h> |
| 11 | + |
| 12 | +#include <executorch/backends/cadence/fusion_g3/operators/operators.h> |
| 13 | +#include <executorch/runtime/core/exec_aten/exec_aten.h> |
| 14 | +#include <executorch/runtime/core/exec_aten/testing_util/tensor_factory.h> |
| 15 | +#include <executorch/runtime/core/exec_aten/testing_util/tensor_util.h> |
| 16 | +#include <executorch/runtime/platform/runtime.h> |
| 17 | + |
| 18 | +namespace cadence { |
| 19 | +namespace impl { |
| 20 | +namespace G3 { |
| 21 | +namespace native { |
| 22 | +namespace { |
| 23 | + |
| 24 | +using ::executorch::aten::Scalar; |
| 25 | +using ::executorch::aten::ScalarType; |
| 26 | +using ::executorch::aten::Tensor; |
| 27 | +using ::executorch::runtime::KernelRuntimeContext; |
| 28 | +using ::executorch::runtime::runtime_init; |
| 29 | +using ::executorch::runtime::testing::TensorFactory; |
| 30 | +using ::testing::Test; |
| 31 | + |
| 32 | +class FusionG3OperatorTest : public Test { |
| 33 | + public: |
| 34 | + void SetUp() override { |
| 35 | + runtime_init(); |
| 36 | + } |
| 37 | + |
| 38 | + protected: |
| 39 | + Tensor& |
| 40 | + add_out(const Tensor& a, const Tensor& b, const Scalar& alpha, Tensor& out) { |
| 41 | + return cadence::impl::G3::native::add_out(context_, a, b, alpha, out); |
| 42 | + } |
| 43 | + |
| 44 | + KernelRuntimeContext context_; |
| 45 | +}; |
| 46 | + |
| 47 | +TEST_F(FusionG3OperatorTest, TwoDimFloatTensorAddTest) { |
| 48 | + TensorFactory<ScalarType::Float> tf; |
| 49 | + const std::vector<int> sizes{2, 2}; |
| 50 | + Tensor out = tf.zeros(sizes); |
| 51 | + |
| 52 | + // Add two 2x2 tensors. |
| 53 | + add_out(tf.make(sizes, {1, 2, 3, 4}), tf.make(sizes, {2, 2, 2, 2}), 1, out); |
| 54 | + |
| 55 | + EXPECT_TENSOR_EQ(out, tf.make(sizes, {3, 4, 5, 6})); |
| 56 | +} |
| 57 | + |
| 58 | +TEST_F(FusionG3OperatorTest, TensorScalarAddTest) { |
| 59 | + TensorFactory<ScalarType::Float> tf; |
| 60 | + const std::vector<int> sizes{2, 2}; |
| 61 | + Tensor out = tf.zeros(sizes); |
| 62 | + |
| 63 | + // Add 2x2 tensor with scalar. |
| 64 | + add_out(tf.make(sizes, {1, 2, 3, 4}), tf.make({1}, {2}), 1, out); |
| 65 | + |
| 66 | + EXPECT_TENSOR_EQ(out, tf.make(sizes, {3, 4, 5, 6})); |
| 67 | +} |
| 68 | + |
| 69 | +TEST_F(FusionG3OperatorTest, AddWithBroadcastTest) { |
| 70 | + TensorFactory<ScalarType::Float> tf; |
| 71 | + // Broadcast add. |
| 72 | + const std::vector<int> size_a{1, 3, 2, 4}, size_b{2, 4}; |
| 73 | + Tensor out = tf.zeros(size_a); |
| 74 | + |
| 75 | + add_out(tf.ones(size_a), tf.ones(size_b), 1, out); |
| 76 | + |
| 77 | + EXPECT_TENSOR_EQ(out, tf.full(size_a, 2)); |
| 78 | +} |
| 79 | + |
| 80 | +} // namespace |
| 81 | +} // namespace native |
| 82 | +} // namespace G3 |
| 83 | +} // namespace impl |
| 84 | +} // namespace cadence |
0 commit comments