Skip to content

Commit 52ddaaa

Browse files
hsharma35facebook-github-bot
authored andcommitted
Buckify op_add for FusionG3 and add cxx tests. (#7102)
Summary: Minimal buckification for `op_add` with some tests to reproduce failures. Reviewed By: zonglinpeng Differential Revision: D66532566
1 parent 13a1a30 commit 52ddaaa

File tree

3 files changed

+119
-1
lines changed

3 files changed

+119
-1
lines changed

backends/cadence/fusion_g3/operators/op_add.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,4 +285,4 @@ Tensor& add_scalar_out(
285285
} // namespace native
286286
} // namespace G3
287287
} // namespace impl
288-
} // namespace cadence
288+
} // namespace cadence
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: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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

Comments
 (0)