Skip to content

Commit c0df241

Browse files
committed
[mir] Add Resize v10 operator to ONNX importer
This commit adds Resize operator in version 10 to ONNX importer. Such operator can be used in YuNet models and is required to convert YuNet ONNX model to circle format. ONE-DCO-1.0-Signed-off-by: Arkadiusz Bokowy <a.bokowy@samsung.com>
1 parent 30a67e1 commit c0df241

File tree

4 files changed

+109
-0
lines changed

4 files changed

+109
-0
lines changed

compiler/mir/src/mir_onnx_importer/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ set(MIR_ONNX_IMPORTER_SOURCES
9191
Op/Relu.h
9292
Op/Reshape.cpp
9393
Op/Reshape.h
94+
Op/Resize.cpp
95+
Op/Resize.h
9496
Op/Shape.cpp
9597
Op/Shape.h
9698
Op/Sigmoid.cpp

compiler/mir/src/mir_onnx_importer/ONNXOpRegistration.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
#include "Op/ReduceMean.h"
4848
#include "Op/Relu.h"
4949
#include "Op/Reshape.h"
50+
#include "Op/Resize.h"
5051
#include "Op/Shape.h"
5152
#include "Op/Sigmoid.h"
5253
#include "Op/Softmax.h"
@@ -204,6 +205,10 @@ inline void registerSupportedOps()
204205
REG(Reshape, 5);
205206
UNSUPPORTED(Reshape, firstUnknownOpset);
206207

208+
REG(Resize, 10);
209+
UNSUPPORTED(Resize, 11);
210+
UNSUPPORTED(Resize, firstUnknownOpset);
211+
207212
REG(Shape, 1);
208213
UNSUPPORTED(Shape, firstUnknownOpset);
209214

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright (c) 2026 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 "Resize.h"
18+
19+
#include <stdexcept>
20+
21+
#include "ONNXHelpers.h"
22+
#include "AttributeHelpers.h"
23+
24+
#include "mir/Tensor.h"
25+
26+
#include "mir/ops/ConstantOp.h"
27+
#include "mir/ops/ResizeOp.h"
28+
29+
namespace mir_onnx
30+
{
31+
32+
static mir::ops::ResizeOp::ResizeMethod getResizeMethod(const std::string &mode)
33+
{
34+
if (mode == "nearest")
35+
return mir::ops::ResizeOp::ResizeMethod::nearestNeighbor;
36+
throw std::runtime_error{"Unsupported mode for Resize operator: " + mode};
37+
}
38+
39+
void convertResizeV10(const onnx::NodeProto &onnx_node, ConverterContext *context)
40+
{
41+
std::vector<mir::Operation::Output *> inputs = context->getNodeInputs(onnx_node);
42+
mir::Graph *graph = context->getGraph();
43+
44+
const auto mode = getAttributeValue<std::string>(onnx_node, "mode", "nearest");
45+
const auto resize_method = getResizeMethod(mode);
46+
47+
// Inputs: [0] X, [1] scales
48+
if (inputs.size() != 2)
49+
throw std::runtime_error{"Resize v10: Expected 2 inputs"};
50+
51+
int rank = inputs[0]->getShape().rank();
52+
if (rank != 4)
53+
throw std::runtime_error("Resize v10: Only 4-D input is supported");
54+
55+
auto *scales = dynamic_cast<mir::ops::ConstantOp *>(inputs[1]->getNode());
56+
assert(scales && "Scales could be a constant tensor only");
57+
auto scales_tensor = mir::Tensor<float>(scales->getValue());
58+
auto scales_tensor_elements = scales_tensor.getShape().numElements();
59+
60+
if (scales_tensor_elements != rank)
61+
throw std::runtime_error{
62+
"Resize v10: The number of elements of 'scales' should be the same as the rank of input 'X'"};
63+
64+
std::vector<float> scales_vector(scales_tensor_elements);
65+
for (int i = 0; i < scales_tensor_elements; i++)
66+
scales_vector[i] = scales_tensor.atOffset(i);
67+
68+
auto result =
69+
createOp<mir::ops::ResizeOp>(graph, inputs[0], resize_method, scales_vector)->getOutput(0);
70+
context->setNodeOutputs(onnx_node, {result});
71+
}
72+
73+
} // namespace mir_onnx
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright (c) 2026 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+
#ifndef MIR_ONNX_OP_RESIZE_H
18+
#define MIR_ONNX_OP_RESIZE_H
19+
20+
#include "ONNXNodeConverterRegistry.h"
21+
22+
namespace mir_onnx
23+
{
24+
25+
void convertResizeV10(const onnx::NodeProto &onnx_node, ConverterContext *context);
26+
27+
} // namespace mir_onnx
28+
29+
#endif // MIR_ONNX_OP_RESIZE_H

0 commit comments

Comments
 (0)