Skip to content

Commit 06a23a1

Browse files
hsharma35facebook-github-bot
authored andcommitted
Buckify op_add for FusionG3 and add cxx tests.
Differential Revision: D66532566
1 parent 5785fc3 commit 06a23a1

File tree

2 files changed

+119
-0
lines changed

2 files changed

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

0 commit comments

Comments
 (0)