Skip to content
This repository was archived by the owner on Jul 18, 2024. It is now read-only.

Commit 115ced9

Browse files
authored
Merge pull request #143 from Rjasuja/float16_resize_1_3
Add Float16 support for Resize Bilinear and Resize Nearest Neighbor op
2 parents f6981f8 + 0de8647 commit 115ced9

2 files changed

Lines changed: 25 additions & 41 deletions

File tree

ngraph_creator/operations/src/ResizeBilinear.cpp

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,6 @@ ResizeBilinear::ResizeBilinear(int operationIndex) : OperationsBase(operationInd
1212
}
1313

1414
bool ResizeBilinear::validate() {
15-
// TODO Add FLOAT16 check when VPUX plugin is supported
16-
if (!checkOutputOperandType(0, (int32_t)OperandType::TENSOR_FLOAT32) &&
17-
!checkOutputOperandType(0, (int32_t)OperandType::TENSOR_QUANT8_ASYMM)) {
18-
ALOGE("%s check for output types failed", __func__);
19-
return false;
20-
}
21-
22-
if (!checkInputOperandType(0, (int32_t)OperandType::TENSOR_FLOAT32) &&
23-
!checkInputOperandType(0, (int32_t)OperandType::TENSOR_QUANT8_ASYMM)) {
24-
return false;
25-
}
26-
2715
const auto& inputDimensionsSize = getInputOperandDimensions(0).size();
2816
if (inputDimensionsSize != 4) {
2917
ALOGE("%s Invalid dimensions size for input(%lu)", __func__, inputDimensionsSize);
@@ -74,8 +62,7 @@ std::shared_ptr<ngraph::Node> ResizeBilinear::createNode() {
7462

7563
if (!useNchw) inputNode = transpose(NHWC_NCHW, inputNode);
7664
// FLOAT16 type check added for future when VPUX plugin support is added
77-
if (checkInputOperandType(1, (int32_t)OperandType::FLOAT32) ||
78-
checkInputOperandType(1, (int32_t)OperandType::FLOAT16)) {
65+
if (checkInputOperandType(1, (int32_t)OperandType::FLOAT32)) {
7966
// In tensorflow lite, resizing by size is supported. Scaling factors are
8067
// calculated based on output shape.
8168
attrs.shape_calculation_mode = ngraph::op::v4::Interpolate::ShapeCalcMode::sizes;
@@ -87,6 +74,14 @@ std::shared_ptr<ngraph::Node> ResizeBilinear::createNode() {
8774
// integer
8875
width_scale = (float)out_width / (float)input_width;
8976
height_scale = (float)out_height / (float)input_height;
77+
} else if (checkInputOperandType(1, (int32_t)OperandType::FLOAT16)) {
78+
attrs.shape_calculation_mode = ngraph::op::v4::Interpolate::ShapeCalcMode::sizes;
79+
width_scale = sModelInfo->ParseOperationInput<_Float16>(mNnapiOperationIndex, 1);
80+
height_scale = sModelInfo->ParseOperationInput<_Float16>(mNnapiOperationIndex, 2);
81+
out_width = (int)(input_width * width_scale);
82+
out_height = (int)(input_height * height_scale);
83+
width_scale = (float)out_width / (float)input_width;
84+
height_scale = (float)out_height / (float)input_height;
9085
} else if (checkInputOperandType(1, (int32_t)OperandType::INT32)) {
9186
attrs.shape_calculation_mode = ngraph::op::v4::Interpolate::ShapeCalcMode::sizes;
9287
out_width = sModelInfo->ParseOperationInput<int>(mNnapiOperationIndex, 1);

ngraph_creator/operations/src/ResizeNearestNeighbor.cpp

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,6 @@ ResizeNearestNeighbor::ResizeNearestNeighbor(int operationIndex) : OperationsBas
1212
}
1313

1414
bool ResizeNearestNeighbor::validate() {
15-
// TODO Add FLOAT16 check when VPUX plugin is supported
16-
if (!checkOutputOperandType(0, (int32_t)OperandType::TENSOR_FLOAT32) &&
17-
!checkOutputOperandType(0, (int32_t)OperandType::TENSOR_QUANT8_ASYMM)) {
18-
ALOGE("%s check for output types failed", __func__);
19-
return false;
20-
}
21-
22-
if (!checkInputOperandType(0, (int32_t)OperandType::TENSOR_FLOAT32) &&
23-
!checkInputOperandType(0, (int32_t)OperandType::TENSOR_QUANT8_ASYMM)) {
24-
return false;
25-
}
26-
2715
const auto& inputDimensionsSize = getInputOperandDimensions(0).size();
2816
if (inputDimensionsSize != 4) {
2917
ALOGE("%s Invalid dimensions size for input(%lu)", __func__, inputDimensionsSize);
@@ -73,45 +61,46 @@ std::shared_ptr<ngraph::Node> ResizeNearestNeighbor::createNode() {
7361
}
7462

7563
if (!useNchw) inputNode = transpose(NHWC_NCHW, inputNode);
76-
// FLOAT16 type check added for future when VPUX plugin support is added
77-
if (checkInputOperandType(1, (int32_t)OperandType::FLOAT32) ||
78-
checkInputOperandType(1, (int32_t)OperandType::FLOAT16)) {
64+
65+
attrs.shape_calculation_mode = ngraph::op::v4::Interpolate::ShapeCalcMode::sizes;
66+
// mode is passed as "nearest" for Nearest Neighbor interpolation
67+
attrs.mode = ngraph::op::v4::Interpolate::InterpolateMode::nearest;
68+
attrs.nearest_mode = ngraph::op::v4::Interpolate::NearestMode::floor;
69+
70+
if (checkInputOperandType(1, (int32_t)OperandType::FLOAT32)) {
7971
// In tensorflow lite, resizing by size is supported. Scaling factors are
8072
// calculated based on output shape.
81-
attrs.shape_calculation_mode = ngraph::op::v4::Interpolate::ShapeCalcMode::sizes;
8273
width_scale = sModelInfo->ParseOperationInput<float>(mNnapiOperationIndex, 1);
8374
height_scale = sModelInfo->ParseOperationInput<float>(mNnapiOperationIndex, 2);
8475
out_width = (int)(input_width * width_scale);
8576
out_height = (int)(input_height * height_scale);
86-
// Recalculating scaling factors here because of typecasting output shape to
87-
// integer
88-
width_scale = (float)out_width / (float)input_width;
89-
height_scale = (float)out_height / (float)input_height;
77+
} else if (checkInputOperandType(1, (int32_t)OperandType::FLOAT16)) {
78+
width_scale = sModelInfo->ParseOperationInput<_Float16>(mNnapiOperationIndex, 1);
79+
height_scale = sModelInfo->ParseOperationInput<_Float16>(mNnapiOperationIndex, 2);
80+
out_width = (int)(input_width * width_scale);
81+
out_height = (int)(input_height * height_scale);
9082
} else if (checkInputOperandType(1, (int32_t)OperandType::INT32)) {
91-
attrs.shape_calculation_mode = ngraph::op::v4::Interpolate::ShapeCalcMode::sizes;
9283
out_width = sModelInfo->ParseOperationInput<int>(mNnapiOperationIndex, 1);
9384
out_height = sModelInfo->ParseOperationInput<int>(mNnapiOperationIndex, 2);
94-
width_scale = (float)out_width / (float)input_width;
95-
height_scale = (float)out_height / (float)input_height;
9685
}
86+
width_scale = (float)out_width / (float)input_width;
87+
height_scale = (float)out_height / (float)input_height;
9788

9889
if (align_corners == true) {
9990
attrs.coordinate_transformation_mode =
10091
ngraph::op::v4::Interpolate::CoordinateTransformMode::align_corners;
92+
attrs.nearest_mode = ngraph::op::v4::Interpolate::NearestMode::round_prefer_ceil;
10193
} else if (half_pixel == true) {
10294
attrs.coordinate_transformation_mode =
10395
ngraph::op::v4::Interpolate::CoordinateTransformMode::half_pixel;
96+
attrs.nearest_mode = ngraph::op::v4::Interpolate::NearestMode::round_prefer_ceil;
10497
} else {
10598
// If none of the align_corners and half_pixel are true, transformation
10699
// mode is set to asymmetric
107100
attrs.coordinate_transformation_mode =
108101
ngraph::op::v4::Interpolate::CoordinateTransformMode::asymmetric;
109102
}
110103

111-
// mode is passed as "nearest" for Nearest Neighbor interpolation
112-
attrs.mode = ngraph::op::v4::Interpolate::InterpolateMode::nearest;
113-
attrs.nearest_mode = ngraph::op::v4::Interpolate::NearestMode::floor;
114-
115104
std::vector<int32_t> output_shape = {out_height, out_width};
116105
auto outputShapeNode = createConstNode(ngraph::element::i32, {2}, output_shape);
117106

0 commit comments

Comments
 (0)