Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions runtime/onert/core/src/compiler/ShapeValidator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,41 @@ void ShapeValidator::visit(const ir::operation::BCQGather &node)
// more shape validation will be done inside kernel.
}

void ShapeValidator::visit(const ir::operation::BroadcastTo &node)
{
const auto &operands = _graph.operands();
const auto output_index{node.getOutputs().at(0)};
if (operands.at(output_index).info().isDynamic())
return;

const auto input_index{node.getInputs().at(ir::operation::BroadcastTo::Input::INPUT)};
const auto shape_index{node.getInputs().at(ir::operation::BroadcastTo::Input::SHAPE)};

std::vector<int32_t> input_shape = operands.at(input_index).shape().dims();
std::vector<int32_t> target_shape = operands.at(shape_index).asVector<int32_t>();

int in_len = input_shape.size();
int tgt_len = target_shape.size();
int max_len = std::max(in_len, tgt_len);

std::vector<int32_t> in_shape_padded(max_len, 1);
std::vector<int32_t> tgt_shape_padded(max_len, 1);

for (int i = 0; i < in_len; i++)
{
in_shape_padded[max_len - in_len + i] = input_shape[i];
}
for (int i = 0; i < tgt_len; i++)
{
tgt_shape_padded[max_len - tgt_len + i] = target_shape[i];
}

for (int i = max_len - 1; i >= 0; --i)
{
OP_REQUIRES((in_shape_padded[i] == tgt_shape_padded[i]) || (in_shape_padded[i] == 1));
}
}
Comment on lines +174 to +194
Copy link
Copy Markdown
Contributor

@glistening glistening Apr 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • We don't need to make two extended shape of copies.
  • 2nd input shape must be greater or equal to input shape.

Refer to the corresponding implementation in tensorflow lite 2.18.0

TfLiteStatus ResizeOutputTensor(TfLiteContext* context,
                                BroadcastToContext* op_context) {
  // Ensures the shape is 1D tensor.
  TF_LITE_ENSURE_EQ(context, NumDimensions(op_context->shape), 1);

  // Ensure output dims is not less than input dims.
  int input_num_dims = NumDimensions(op_context->input);
  int output_num_dims = SizeOfDimension(op_context->shape, 0);
  TF_LITE_ENSURE_MSG(context, input_num_dims <= output_num_dims,
                     "Output shape must be broadcastable from input shape.");
  TF_LITE_ENSURE_MSG(context, output_num_dims <= kMaxDims,
                     "BroadcastTo only supports 1-8D tensor.");

  // Check if output shape is broadcastable from input shape.
  auto get_shape_data = [op_context](int i) -> int32_t {
    if (op_context->shape->type == kTfLiteInt32) {
      return GetTensorData<int32_t>(op_context->shape)[i];
    } else {
      return GetTensorData<int64_t>(op_context->shape)[i];
    }
  };

  int extending_dims = output_num_dims - input_num_dims;
  for (int idx = 0; idx < input_num_dims; ++idx) {
    TF_LITE_ENSURE_MSG(context,
                       (SizeOfDimension(op_context->input, idx) == 1 ||
                        SizeOfDimension(op_context->input, idx) ==
                            get_shape_data(extending_dims + idx)),
                       "Output shape must be broadcastable from input shape.");
  }


void ShapeValidator::visit(const ir::operation::Conv2D &node)
{
const auto &operands = _graph.operands();
Expand Down
1 change: 1 addition & 0 deletions runtime/onert/core/src/compiler/ShapeValidator.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class ShapeValidator : public ir::OperationVisitor
void visit(const ir::operation::BatchToSpaceND &node) override;
void visit(const ir::operation::BCQFullyConnected &node) override;
void visit(const ir::operation::BCQGather &node) override;
void visit(const ir::operation::BroadcastTo &node) override;
void visit(const ir::operation::Conv2D &node) override;
void visit(const ir::operation::Comparison &node) override;
void visit(const ir::operation::DepthwiseConv2D &node) override;
Expand Down
8 changes: 8 additions & 0 deletions runtime/onert/core/src/ir/OperationValidator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,14 @@ void OperationValidator::visit(const operation::BinaryArithmetic &node)
OP_REQUIRES(isSameType(lhs_index, output_index));
}

void OperationValidator::visit(const operation::BroadcastTo &node)
{
const auto input_index(node.getInputs().at(operation::BroadcastTo::Input::INPUT));
const auto output_index(node.getOutputs().at(0));

OP_REQUIRES(isSameType(input_index, output_index));
}

void OperationValidator::visit(const operation::Comparison &node)
{
const auto output_index{node.getOutputs().at(0)};
Expand Down
1 change: 1 addition & 0 deletions runtime/onert/core/src/ir/OperationValidator.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class OperationValidator : public OperationVisitor
void visit(const operation::BatchMatMul &node) override;
void visit(const operation::BatchToSpaceND &node) override;
void visit(const operation::BinaryArithmetic &node) override;
void visit(const operation::BroadcastTo &node) override;
void visit(const operation::Comparison &node) override;
void visit(const operation::Concat &node) override;
void visit(const operation::Conv2D &node) override;
Expand Down
3 changes: 3 additions & 0 deletions runtime/onert/core/src/loader/BaseLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -1721,6 +1721,9 @@ void BaseLoader<LoaderDomain>::loadOperation(const Operator *op, ir::Graph &subg
case BuiltinOperator::BuiltinOperator_BATCH_MATMUL:
loadBatchMatMul(op, subg);
return;
case BuiltinOperator::BuiltinOperator_BROADCAST_TO:
loadOperationTo<ir::operation::BroadcastTo>(op, subg);
return;
case BuiltinOperator::BuiltinOperator_LOG_SOFTMAX:
loadLogSoftmax(op, subg);
return;
Expand Down
7 changes: 7 additions & 0 deletions runtime/tests/nnfw_api/lib/CircleGen.cc
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,13 @@ uint32_t CircleGen::addOperatorBatchMatMul(const OperatorParams &params, bool ad
circle::BuiltinOptions_BatchMatMulOptions, options);
}

uint32_t CircleGen::addOperatorBroadcastTo(const OperatorParams &params)
{
auto options = circle::CreateBroadcastToOptions(_fbb).Union();
return addOperatorWithOptions(params, circle::BuiltinOperator_BROADCAST_TO,
circle::BuiltinOptions_BroadcastToOptions, options);
}

uint32_t CircleGen::addOperatorCast(const OperatorParams &params, circle::TensorType input_type,
circle::TensorType output_type)
{
Expand Down
1 change: 1 addition & 0 deletions runtime/tests/nnfw_api/lib/CircleGen.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ class CircleGen
uint32_t addOperatorBatchMatMul(const OperatorParams &params, bool adj_x, bool adj_y,
bool asymmetric_quantize_inputs = false);
uint32_t addOperatorBatchToSpaceND(const OperatorParams &params);
uint32_t addOperatorBroadcastTo(const OperatorParams &params);
uint32_t addOperatorCast(const OperatorParams &params, circle::TensorType input_type,
circle::TensorType output_type);
uint32_t addOperatorConcatenation(const OperatorParams &params, int axis,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/*
* Copyright (c) 2025 Samsung Electronics Co., Ltd. All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "GenModelTest.h"

TEST_F(GenModelTest, OneOp_BroadcastTo_1D_to_2D)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@seockho-kim We have a requirement to have # of positive tc = # of negative tc.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@seockho-kim We have a requirement to have # of positive tc = # of negative tc.

Okay, I'll update.

{
CircleGen cgen;
const uint32_t shape_buf = cgen.addBuffer(std::vector<int32_t>{3, 3});
int shape = cgen.addTensor({{2}, circle::TensorType::TensorType_INT32, shape_buf});
int in = cgen.addTensor({{3}, circle::TensorType::TensorType_FLOAT32});
int out = cgen.addTensor({{3, 3}, circle::TensorType::TensorType_FLOAT32});
cgen.addOperatorBroadcastTo({{in, shape}, {out}});
cgen.setInputsAndOutputs({in}, {out});

_context = std::make_unique<GenModelTestContext>(cgen.finish());
_context->addTestCase(uniformTCD<float>({{1, 2, 3}}, {{1, 2, 3, 1, 2, 3, 1, 2, 3}}));
_context->setBackends({"cpu"});

SUCCEED();
}

TEST_F(GenModelTest, OneOp_BroadcastTo_2D_to_3D)
{
CircleGen cgen;
const uint32_t shape_buf = cgen.addBuffer(std::vector<int32_t>{3, 2, 2});
int shape = cgen.addTensor({{3}, circle::TensorType::TensorType_INT32, shape_buf});
int in = cgen.addTensor({{2, 2}, circle::TensorType::TensorType_FLOAT32});
int out = cgen.addTensor({{3, 2, 2}, circle::TensorType::TensorType_FLOAT32});
cgen.addOperatorBroadcastTo({{in, shape}, {out}});
cgen.setInputsAndOutputs({in}, {out});

_context = std::make_unique<GenModelTestContext>(cgen.finish());
_context->addTestCase(uniformTCD<float>({{1, 2, 3, 4}}, {{1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4}}));
_context->setBackends({"cpu"});

SUCCEED();
}

TEST_F(GenModelTest, OneOp_BroadcastTo_3D_to_3D)
{
CircleGen cgen;
const uint32_t shape_buf = cgen.addBuffer(std::vector<int32_t>{2, 3, 2});
int shape = cgen.addTensor({{3}, circle::TensorType::TensorType_INT32, shape_buf});
int in = cgen.addTensor({{2, 1, 2}, circle::TensorType::TensorType_FLOAT32});
int out = cgen.addTensor({{2, 3, 2}, circle::TensorType::TensorType_FLOAT32});
cgen.addOperatorBroadcastTo({{in, shape}, {out}});
cgen.setInputsAndOutputs({in}, {out});

_context = std::make_unique<GenModelTestContext>(cgen.finish());
_context->addTestCase(uniformTCD<float>({{1, 2, 3, 4}}, {{1, 2, 1, 2, 1, 2, 3, 4, 3, 4, 3, 4}}));
_context->setBackends({"cpu"});

SUCCEED();
}

TEST_F(GenModelTest, neg_OneOp_BroadcastTo_InputOutputDifferentType)
{
CircleGen cgen;
const uint32_t shape_buf = cgen.addBuffer(std::vector<int32_t>{3, 3});
int shape = cgen.addTensor({{2}, circle::TensorType::TensorType_INT32, shape_buf});
int in = cgen.addTensor({{3}, circle::TensorType::TensorType_INT32});
int out = cgen.addTensor({{3, 3}, circle::TensorType::TensorType_FLOAT32});
cgen.addOperatorBroadcastTo({{in, shape}, {out}});
cgen.setInputsAndOutputs({in}, {out});

_context = std::make_unique<GenModelTestContext>(cgen.finish());
_context->addTestCase(uniformTCD<float>({{1, 2, 3}}, {{1, 2, 3, 1, 2, 3, 1, 2, 3}}));
_context->setBackends({"cpu"});
_context->expectFailModelLoad();

SUCCEED();
}

TEST_F(GenModelTest, neg_OneOp_BroadcastTo_1D_to_2D_InvalidShape)
{
CircleGen cgen;
const uint32_t shape_buf = cgen.addBuffer(std::vector<int32_t>{3, 2});
int shape = cgen.addTensor({{2}, circle::TensorType::TensorType_INT32, shape_buf});
int in = cgen.addTensor({{3}, circle::TensorType::TensorType_FLOAT32});
int out = cgen.addTensor({{3, 2}, circle::TensorType::TensorType_FLOAT32});
cgen.addOperatorBroadcastTo({{in, shape}, {out}});
cgen.setInputsAndOutputs({in}, {out});

_context = std::make_unique<GenModelTestContext>(cgen.finish());
_context->addTestCase(uniformTCD<float>({{1, 2, 3}}, {{1, 2, 3, 1, 2, 3}}));
_context->setBackends({"cpu"});
_context->expectFailCompile();

SUCCEED();
}

TEST_F(GenModelTest, neg_OneOp_BroadcastTo_2D_to_3D_InvalidShape)
{
CircleGen cgen;
const uint32_t shape_buf = cgen.addBuffer(std::vector<int32_t>{2, 1, 3});
int shape = cgen.addTensor({{3}, circle::TensorType::TensorType_INT32, shape_buf});
int in = cgen.addTensor({{2, 3}, circle::TensorType::TensorType_FLOAT32});
int out = cgen.addTensor({{2, 1, 3}, circle::TensorType::TensorType_FLOAT32});
cgen.addOperatorBroadcastTo({{in, shape}, {out}});
cgen.setInputsAndOutputs({in}, {out});

_context = std::make_unique<GenModelTestContext>(cgen.finish());
_context->addTestCase(uniformTCD<float>({{1, 2, 3, 1, 2, 3}}, {{1, 2, 3, 1, 2, 3}}));
_context->setBackends({"cpu"});
_context->expectFailCompile();

SUCCEED();
}

TEST_F(GenModelTest, neg_OneOp_BroadcastTo_3D_to_3D_InvalidShape)
{
CircleGen cgen;
const uint32_t shape_buf = cgen.addBuffer(std::vector<int32_t>{2, 3, 2});
int shape = cgen.addTensor({{3}, circle::TensorType::TensorType_INT32, shape_buf});
int in = cgen.addTensor({{2, 2, 2}, circle::TensorType::TensorType_FLOAT32});
int out = cgen.addTensor({{2, 3, 2}, circle::TensorType::TensorType_FLOAT32});
cgen.addOperatorBroadcastTo({{in, shape}, {out}});
cgen.setInputsAndOutputs({in}, {out});

_context = std::make_unique<GenModelTestContext>(cgen.finish());
_context->addTestCase(
uniformTCD<float>({{1, 2, 1, 2, 1, 2, 1, 2}}, {{1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2}}));
_context->setBackends({"cpu"});
_context->expectFailCompile();

SUCCEED();
}