diff --git a/backends/cadence/fusion_g3/operators/op_add.cpp b/backends/cadence/fusion_g3/operators/op_add.cpp index b5b5baf9a8c..0a7c7e7e035 100644 --- a/backends/cadence/fusion_g3/operators/op_add.cpp +++ b/backends/cadence/fusion_g3/operators/op_add.cpp @@ -285,4 +285,4 @@ Tensor& add_scalar_out( } // namespace native } // namespace G3 } // namespace impl -} // namespace cadence \ No newline at end of file +} // namespace cadence diff --git a/backends/cadence/fusion_g3/operators/operators.h b/backends/cadence/fusion_g3/operators/operators.h new file mode 100644 index 00000000000..fc4d5ff6252 --- /dev/null +++ b/backends/cadence/fusion_g3/operators/operators.h @@ -0,0 +1,34 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. + */ + +#include +#include + +namespace cadence { +namespace impl { +namespace G3 { +namespace native { + +::executorch::aten::Tensor& add_out( + ::executorch::runtime::KernelRuntimeContext& ctx, + const ::executorch::aten::Tensor& a, + const ::executorch::aten::Tensor& b, + const ::executorch::aten::Scalar& alpha, + ::executorch::aten::Tensor& out); + +::executorch::aten::Tensor& add_scalar_out( + ::executorch::runtime::KernelRuntimeContext& ctx, + const ::executorch::aten::Tensor& a, + const ::executorch::aten::Scalar& b, + const ::executorch::aten::Scalar& alpha, + ::executorch::aten::Tensor& out); + +} // namespace native +} // namespace G3 +} // namespace impl +} // namespace cadence diff --git a/backends/cadence/fusion_g3/operators/tests/test_op_add.cpp b/backends/cadence/fusion_g3/operators/tests/test_op_add.cpp new file mode 100644 index 00000000000..06bf4bf4ec1 --- /dev/null +++ b/backends/cadence/fusion_g3/operators/tests/test_op_add.cpp @@ -0,0 +1,84 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. + */ + +#include +#include + +#include +#include +#include +#include +#include + +namespace cadence { +namespace impl { +namespace G3 { +namespace native { +namespace { + +using ::executorch::aten::Scalar; +using ::executorch::aten::ScalarType; +using ::executorch::aten::Tensor; +using ::executorch::runtime::KernelRuntimeContext; +using ::executorch::runtime::runtime_init; +using ::executorch::runtime::testing::TensorFactory; +using ::testing::Test; + +class FusionG3OperatorTest : public Test { + public: + void SetUp() override { + runtime_init(); + } + + protected: + Tensor& + add_out(const Tensor& a, const Tensor& b, const Scalar& alpha, Tensor& out) { + return cadence::impl::G3::native::add_out(context_, a, b, alpha, out); + } + + KernelRuntimeContext context_; +}; + +TEST_F(FusionG3OperatorTest, TwoDimFloatTensorAddTest) { + TensorFactory tf; + const std::vector sizes{2, 2}; + Tensor out = tf.zeros(sizes); + + // Add two 2x2 tensors. + add_out(tf.make(sizes, {1, 2, 3, 4}), tf.make(sizes, {2, 2, 2, 2}), 1, out); + + EXPECT_TENSOR_EQ(out, tf.make(sizes, {3, 4, 5, 6})); +} + +TEST_F(FusionG3OperatorTest, TensorScalarAddTest) { + TensorFactory tf; + const std::vector sizes{2, 2}; + Tensor out = tf.zeros(sizes); + + // Add 2x2 tensor with scalar. + add_out(tf.make(sizes, {1, 2, 3, 4}), tf.make({1}, {2}), 1, out); + + EXPECT_TENSOR_EQ(out, tf.make(sizes, {3, 4, 5, 6})); +} + +TEST_F(FusionG3OperatorTest, AddWithBroadcastTest) { + TensorFactory tf; + // Broadcast add. + const std::vector size_a{1, 3, 2, 4}, size_b{2, 4}; + Tensor out = tf.zeros(size_a); + + add_out(tf.ones(size_a), tf.ones(size_b), 1, out); + + EXPECT_TENSOR_EQ(out, tf.full(size_a, 2)); +} + +} // namespace +} // namespace native +} // namespace G3 +} // namespace impl +} // namespace cadence