Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Paddle Frontend Op conversion: ROIAlign9,Sqrt,Swish #11661

Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 3 additions & 0 deletions src/core/tests/frontend/paddle/op_fuzzy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ static const std::vector<std::string> models{std::string("argmax"),
std::string("split_test_dim_int64"),
std::string("split_test_list"),
std::string("split_test_list_tensor"),
std::string("sqrt_float32"),
std::string("squeeze"),
std::string("squeeze_null_axes"),
std::string("stack_test_float32"),
Expand All @@ -312,6 +313,8 @@ static const std::vector<std::string> models{std::string("argmax"),
std::string("strided_slice_input2_3"),
std::string("strided_slice_input3_1"),
std::string("strided_slice_input3_2"),
std::string("swish_default_params"),
std::string("swish_beta"),
std::string("tanh"),
std::string("trilinear_downsample_false_0"),
std::string("trilinear_downsample_false_1"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import numpy as np
from save_model import saveModel
import paddle
import ops
import sys


Expand Down Expand Up @@ -34,7 +35,7 @@ def make_rois(batch_size, width, height, pooled_width, pooled_height, spatial_sc
return rois, rois_num


def roi_align(name: str, x_data, rois_data, rois_num_data, pooled_height, pooled_width, spatial_scale, sampling_ratio):
def roi_align(name: str, x_data, rois_data, rois_num_data, pooled_height, pooled_width, spatial_scale, sampling_ratio, aligned):
paddle.enable_static()

with paddle.static.program_guard(paddle.static.Program(), paddle.static.Program()):
Expand All @@ -44,14 +45,13 @@ def roi_align(name: str, x_data, rois_data, rois_num_data, pooled_height, pooled
name='rois', shape=rois_data.shape, dtype=rois_data.dtype)
rois_num = paddle.static.data(
name='rois_num', shape=rois_num_data.shape, dtype=rois_num_data.dtype)
# TODO: 'aligned' attribute is not supported by Paddle 2.1
out = paddle.fluid.layers.roi_align(input=x,
rois=rois,
pooled_height=pooled_height,
pooled_width=pooled_width,
spatial_scale=spatial_scale,
sampling_ratio=sampling_ratio,
rois_num=rois_num)
out = ops.roi_align(input=x,
rois=rois,
output_size=(pooled_height, pooled_width),
spatial_scale=spatial_scale,
sampling_ratio=sampling_ratio,
rois_num=rois_num,
aligned=aligned)

cpu = paddle.static.cpu_places(1)
exe = paddle.static.Executor(cpu[0])
Expand Down Expand Up @@ -81,13 +81,14 @@ def main():
pooled_height = 2
pooled_width = 2
sampling_ratio = -1
aligned = False

roi_per_batch = 1
rois, rois_num = make_rois(batch_size, width, height, pooled_width,
pooled_height, spatial_scale, roi_per_batch)

roi_align("roi_align_test", x, rois, rois_num, pooled_height,
pooled_width, spatial_scale, sampling_ratio)
pooled_width, spatial_scale, sampling_ratio, aligned)

batch_size = 1
channels = 3
Expand All @@ -101,13 +102,14 @@ def main():
pooled_height = 2
pooled_width = 2
sampling_ratio = 2
aligned = True

roi_per_batch = 2
rois, rois_num = make_rois(batch_size, width, height, pooled_width,
pooled_height, spatial_scale, roi_per_batch)

roi_align("roi_align_test2", x, rois, rois_num, pooled_height,
pooled_width, spatial_scale, sampling_ratio)
pooled_width, spatial_scale, sampling_ratio, aligned)


if __name__ == "__main__":
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#
# sqrt paddle model generator
#
import numpy as np
from save_model import saveModel
import paddle as pdpd
import sys


def sqrt(name: str, x, data_type):
pdpd.enable_static()

with pdpd.static.program_guard(pdpd.static.Program(), pdpd.static.Program()):
node_x = pdpd.static.data(
name='input_x', shape=x.shape, dtype=data_type)
out = pdpd.sqrt(x=node_x, name='sqrt')

cpu = pdpd.static.cpu_places(1)
exe = pdpd.static.Executor(cpu[0])
# startup program will call initializer to initialize the parameters.
exe.run(pdpd.static.default_startup_program())

outs = exe.run(
feed={'input_x': x},
fetch_list=[out])

saveModel(name, exe, feedkeys=['input_x'], fetchlist=[out],
inputs=[x], outputs=[outs[0]], target_dir=sys.argv[1])

return outs[0]


def main():

data_type = 'float32'
x = np.array([0.1, 0.2, 0.3, 0.4]).astype(data_type)
sqrt("sqrt_float32", x, data_type)


if __name__ == "__main__":
main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#
# swish paddle model generator
#
import numpy as np
from save_model import saveModel
import paddle as pdpd
import sys


def swish(name: str, x, data_type, input_beta):
pdpd.enable_static()

with pdpd.static.program_guard(pdpd.static.Program(), pdpd.static.Program()):
node_x = pdpd.static.data(
name='input_x', shape=x.shape, dtype=data_type)
out = pdpd.fluid.layers.swish(x=node_x, beta=input_beta, name='swish')

cpu = pdpd.static.cpu_places(1)
exe = pdpd.static.Executor(cpu[0])
# startup program will call initializer to initialize the parameters.
exe.run(pdpd.static.default_startup_program())

outs = exe.run(
feed={'input_x': x},
fetch_list=[out])

saveModel(name, exe, feedkeys=['input_x'], fetchlist=[out],
inputs=[x], outputs=[outs[0]], target_dir=sys.argv[1])

return outs[0]


def main():
data_type = 'float32'
input_beta = 1.0
x = np.random.randn(2, 3).astype(data_type)
swish("swish_default_params", x, data_type, input_beta)

input_beta = 2.0
x = np.random.randn(2, 3).astype(data_type)
swish("swish_beta", x, data_type, input_beta)


if __name__ == "__main__":
main()
4 changes: 2 additions & 2 deletions src/frontends/paddle/src/default_opset.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
// SPDX-License-Identifier: Apache-2.0
//

#include "openvino/opsets/opset8.hpp"
#include "openvino/opsets/opset9.hpp"

namespace ov {
namespace frontend {
namespace paddle {
namespace op {
namespace default_opset = ov::opset8;
namespace default_opset = ov::opset9;

} // namespace op
} // namespace paddle
Expand Down
15 changes: 11 additions & 4 deletions src/frontends/paddle/src/op/roi_align.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,19 @@ namespace ov {
namespace frontend {
namespace paddle {
namespace op {
using AlignedMode = default_opset::ROIAlign::AlignedMode;
using PoolingMode = default_opset::ROIAlign::PoolingMode;
NamedOutputs roi_align(const NodeContext& node) {
const auto data_node = node.get_input("X");
const auto roi_node = node.get_input("ROIs");
// TODO: support 'aligned' feature #82319
const auto aligned = node.get_attribute("aligned", false);
PADDLE_OP_CHECK(node, !aligned, "OpenVINO not support 'aligned' feature!");
// Paddle only use 'avg' interpolation mode
const auto pooling_mode = PoolingMode::AVG;
AlignedMode aligned_mode;
if (aligned)
aligned_mode = AlignedMode::HALF_PIXEL_FOR_NN;
else
aligned_mode = AlignedMode::ASYMMETRIC;

// TODO: support multiple batches #83232
if (data_node.get_partial_shape().rank().is_static() && data_node.get_partial_shape()[0].is_static())
Expand All @@ -35,15 +42,15 @@ NamedOutputs roi_align(const NodeContext& node) {
auto sampling_ratio = node.get_attribute<int>("sampling_ratio", -1);
sampling_ratio = (sampling_ratio <= 0) ? 0 : sampling_ratio;

// Paddle only use 'avg' interpolation mode
return node.default_single_output_mapping({std::make_shared<default_opset::ROIAlign>(data_node,
roi_node,
fake_roisNum_node,
pooled_h,
pooled_w,
sampling_ratio,
spatial_scale,
"avg")},
pooling_mode,
aligned_mode)},
{"Out"});
}
} // namespace op
Expand Down
21 changes: 21 additions & 0 deletions src/frontends/paddle/src/op/sqrt.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (C) 2018-2022 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

#include "default_opset.hpp"
#include "openvino/frontend/paddle/node_context.hpp"

namespace ov {
namespace frontend {
namespace paddle {
namespace op {
NamedOutputs sqrt(const NodeContext& node) {
const auto x = node.get_input("X");

return node.default_single_output_mapping({std::make_shared<default_opset::Sqrt>(x)}, {"Out"});
}

} // namespace op
} // namespace paddle
} // namespace frontend
} // namespace ov
23 changes: 23 additions & 0 deletions src/frontends/paddle/src/op/swish.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (C) 2018-2022 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

#include "default_opset.hpp"
#include "openvino/frontend/paddle/node_context.hpp"

namespace ov {
namespace frontend {
namespace paddle {
namespace op {
NamedOutputs swish(const NodeContext& node) {
const auto x = node.get_input("X");
const float beta = node.get_attribute<float>("beta", 1.0f);
const auto beta_node = default_opset::Constant::create(element::f32, Shape{}, {beta});

return node.default_single_output_mapping({std::make_shared<default_opset::Swish>(x, beta_node)}, {"Out"});
}

} // namespace op
} // namespace paddle
} // namespace frontend
} // namespace ov
4 changes: 4 additions & 0 deletions src/frontends/paddle/src/op_table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,11 @@ OP_CONVERTER(softmax);
OP_CONVERTER(softplus);
OP_CONVERTER(sigmoid);
OP_CONVERTER(split);
OP_CONVERTER(sqrt);
OP_CONVERTER(squeeze);
OP_CONVERTER(stack);
OP_CONVERTER(strided_slice);
OP_CONVERTER(swish);
OP_CONVERTER(tanh);
OP_CONVERTER(transpose2);
OP_CONVERTER(trilinear_interp_v2);
Expand Down Expand Up @@ -173,9 +175,11 @@ std::map<std::string, CreatorFunction> get_supported_ops() {
{"softplus", op::softplus},
{"sigmoid", op::sigmoid},
{"split", op::split},
{"sqrt", op::sqrt},
{"squeeze2", op::squeeze},
{"stack", op::stack},
{"strided_slice", op::strided_slice},
{"swish", op::swish},
{"sync_batch_norm", op::batch_norm},
{"tanh", op::tanh},
{"transpose2", op::transpose2},
Expand Down