Skip to content

Commit 8158c3b

Browse files
committed
[runtime] Support to load BROADCAST_TO operation
- BROADCAST_TO operation is already supported in runtime, but it was under CUSTOM operation. so it's fixed to be able to load BROADCAST_TO directly. - Added test cases for BROADCAST_TO operation. ONE-DCO-1.0-Signed-off-by: Seockho Kim seockho.kim@samsung.com
1 parent 97d6d49 commit 8158c3b

4 files changed

Lines changed: 79 additions & 0 deletions

File tree

runtime/onert/core/src/loader/BaseLoader.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1721,6 +1721,9 @@ void BaseLoader<LoaderDomain>::loadOperation(const Operator *op, ir::Graph &subg
17211721
case BuiltinOperator::BuiltinOperator_BATCH_MATMUL:
17221722
loadBatchMatMul(op, subg);
17231723
return;
1724+
case BuiltinOperator::BuiltinOperator_BROADCAST_TO:
1725+
loadOperationTo<ir::operation::BroadcastTo>(op, subg);
1726+
return;
17241727
case BuiltinOperator::BuiltinOperator_LOG_SOFTMAX:
17251728
loadLogSoftmax(op, subg);
17261729
return;

runtime/tests/nnfw_api/lib/CircleGen.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,13 @@ uint32_t CircleGen::addOperatorBatchMatMul(const OperatorParams &params, bool ad
148148
circle::BuiltinOptions_BatchMatMulOptions, options);
149149
}
150150

151+
uint32_t CircleGen::addOperatorBroadcastTo(const OperatorParams &params)
152+
{
153+
auto options = circle::CreateBroadcastToOptions(_fbb).Union();
154+
return addOperatorWithOptions(params, circle::BuiltinOperator_BROADCAST_TO,
155+
circle::BuiltinOptions_BroadcastToOptions, options);
156+
}
157+
151158
uint32_t CircleGen::addOperatorCast(const OperatorParams &params, circle::TensorType input_type,
152159
circle::TensorType output_type)
153160
{

runtime/tests/nnfw_api/lib/CircleGen.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ class CircleGen
150150
uint32_t addOperatorBatchMatMul(const OperatorParams &params, bool adj_x, bool adj_y,
151151
bool asymmetric_quantize_inputs = false);
152152
uint32_t addOperatorBatchToSpaceND(const OperatorParams &params);
153+
uint32_t addOperatorBroadcastTo(const OperatorParams &params);
153154
uint32_t addOperatorCast(const OperatorParams &params, circle::TensorType input_type,
154155
circle::TensorType output_type);
155156
uint32_t addOperatorConcatenation(const OperatorParams &params, int axis,
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright (c) 2025 Samsung Electronics Co., Ltd. All Rights Reserved
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "GenModelTest.h"
18+
19+
TEST_F(GenModelTest, OneOp_BroadcastTo_1D_to_2D)
20+
{
21+
CircleGen cgen;
22+
const uint32_t shape_buf = cgen.addBuffer(std::vector<int32_t>{3, 3});
23+
int shape = cgen.addTensor({{2}, circle::TensorType::TensorType_INT32, shape_buf});
24+
int in = cgen.addTensor({{3}, circle::TensorType::TensorType_FLOAT32});
25+
int out = cgen.addTensor({{3, 3}, circle::TensorType::TensorType_FLOAT32});
26+
cgen.addOperatorBroadcastTo({{in, shape}, {out}});
27+
cgen.setInputsAndOutputs({in}, {out});
28+
29+
_context = std::make_unique<GenModelTestContext>(cgen.finish());
30+
_context->addTestCase(uniformTCD<float>({{1, 2, 3}}, {{1, 2, 3, 1, 2, 3, 1, 2, 3}}));
31+
_context->setBackends({"cpu"});
32+
33+
SUCCEED();
34+
}
35+
36+
TEST_F(GenModelTest, OneOp_BroadcastTo_2D_to_3D)
37+
{
38+
CircleGen cgen;
39+
const uint32_t shape_buf = cgen.addBuffer(std::vector<int32_t>{3, 2, 2});
40+
int shape = cgen.addTensor({{3}, circle::TensorType::TensorType_INT32, shape_buf});
41+
int in = cgen.addTensor({{2, 2}, circle::TensorType::TensorType_FLOAT32});
42+
int out = cgen.addTensor({{3, 2, 2}, circle::TensorType::TensorType_FLOAT32});
43+
cgen.addOperatorBroadcastTo({{in, shape}, {out}});
44+
cgen.setInputsAndOutputs({in}, {out});
45+
46+
_context = std::make_unique<GenModelTestContext>(cgen.finish());
47+
_context->addTestCase(uniformTCD<float>({{1, 2, 3, 4}}, {{1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4}}));
48+
_context->setBackends({"cpu"});
49+
50+
SUCCEED();
51+
}
52+
53+
TEST_F(GenModelTest, OneOp_BroadcastTo_3D_to_3D)
54+
{
55+
CircleGen cgen;
56+
const uint32_t shape_buf = cgen.addBuffer(std::vector<int32_t>{2, 3, 2});
57+
int shape = cgen.addTensor({{3}, circle::TensorType::TensorType_INT32, shape_buf});
58+
int in = cgen.addTensor({{2, 1, 2}, circle::TensorType::TensorType_FLOAT32});
59+
int out = cgen.addTensor({{2, 3, 2}, circle::TensorType::TensorType_FLOAT32});
60+
cgen.addOperatorBroadcastTo({{in, shape}, {out}});
61+
cgen.setInputsAndOutputs({in}, {out});
62+
63+
_context = std::make_unique<GenModelTestContext>(cgen.finish());
64+
_context->addTestCase(uniformTCD<float>({{1, 2, 3, 4}}, {{1, 2, 1, 2, 1, 2, 3, 4, 3, 4, 3, 4}}));
65+
_context->setBackends({"cpu"});
66+
67+
SUCCEED();
68+
}

0 commit comments

Comments
 (0)