From 93603472cf426120ca19da723bb27fcfb72f2866 Mon Sep 17 00:00:00 2001 From: Zhang Yi3 Date: Wed, 14 Jul 2021 12:48:17 +0800 Subject: [PATCH 01/15] [FrontEnd]enable pdpd ops conversion part3 --- ngraph/frontend/paddlepaddle/src/op/mul.cpp | 67 +++ ngraph/frontend/paddlepaddle/src/op/mul.hpp | 20 + ngraph/frontend/paddlepaddle/src/op/pad3d.cpp | 117 +++++ ngraph/frontend/paddlepaddle/src/op/pad3d.hpp | 20 + .../frontend/paddlepaddle/src/op/pool2d.cpp | 265 +++++++++++ .../frontend/paddlepaddle/src/op/pool2d.hpp | 20 + ngraph/frontend/paddlepaddle/src/op/pow.cpp | 41 ++ ngraph/frontend/paddlepaddle/src/op/pow.hpp | 20 + ngraph/frontend/paddlepaddle/src/op/range.cpp | 41 ++ ngraph/frontend/paddlepaddle/src/op/range.hpp | 20 + ngraph/frontend/paddlepaddle/src/op/relu6.cpp | 28 ++ ngraph/frontend/paddlepaddle/src/op/relu6.hpp | 20 + .../frontend/paddlepaddle/src/op/reshape2.cpp | 56 +++ .../frontend/paddlepaddle/src/op/reshape2.hpp | 20 + ngraph/frontend/paddlepaddle/src/op/shape.cpp | 26 ++ ngraph/frontend/paddlepaddle/src/op/shape.hpp | 20 + .../frontend/paddlepaddle/src/op/sigmoid.cpp | 26 ++ .../frontend/paddlepaddle/src/op/sigmoid.hpp | 20 + ngraph/frontend/paddlepaddle/src/op/slice.cpp | 61 +++ ngraph/frontend/paddlepaddle/src/op/slice.hpp | 20 + .../frontend/paddlepaddle/src/op/softmax.cpp | 34 ++ .../frontend/paddlepaddle/src/op/softmax.hpp | 20 + .../frontend/paddlepaddle/src/op/squeeze.cpp | 34 ++ .../frontend/paddlepaddle/src/op/squeeze.hpp | 20 + .../paddlepaddle/src/op/unsqueeze.cpp | 30 ++ .../paddlepaddle/src/op/unsqueeze.hpp | 20 + .../frontend/paddlepaddle/src/op/yolo_box.cpp | 440 ++++++++++++++++++ .../frontend/paddlepaddle/src/op/yolo_box.hpp | 20 + ngraph/frontend/paddlepaddle/src/op_table.cpp | 31 +- 29 files changed, 1576 insertions(+), 1 deletion(-) create mode 100644 ngraph/frontend/paddlepaddle/src/op/mul.cpp create mode 100644 ngraph/frontend/paddlepaddle/src/op/mul.hpp create mode 100644 ngraph/frontend/paddlepaddle/src/op/pad3d.cpp create mode 100644 ngraph/frontend/paddlepaddle/src/op/pad3d.hpp create mode 100644 ngraph/frontend/paddlepaddle/src/op/pool2d.cpp create mode 100644 ngraph/frontend/paddlepaddle/src/op/pool2d.hpp create mode 100644 ngraph/frontend/paddlepaddle/src/op/pow.cpp create mode 100644 ngraph/frontend/paddlepaddle/src/op/pow.hpp create mode 100644 ngraph/frontend/paddlepaddle/src/op/range.cpp create mode 100644 ngraph/frontend/paddlepaddle/src/op/range.hpp create mode 100644 ngraph/frontend/paddlepaddle/src/op/relu6.cpp create mode 100644 ngraph/frontend/paddlepaddle/src/op/relu6.hpp create mode 100644 ngraph/frontend/paddlepaddle/src/op/reshape2.cpp create mode 100644 ngraph/frontend/paddlepaddle/src/op/reshape2.hpp create mode 100644 ngraph/frontend/paddlepaddle/src/op/shape.cpp create mode 100644 ngraph/frontend/paddlepaddle/src/op/shape.hpp create mode 100644 ngraph/frontend/paddlepaddle/src/op/sigmoid.cpp create mode 100644 ngraph/frontend/paddlepaddle/src/op/sigmoid.hpp create mode 100644 ngraph/frontend/paddlepaddle/src/op/slice.cpp create mode 100644 ngraph/frontend/paddlepaddle/src/op/slice.hpp create mode 100644 ngraph/frontend/paddlepaddle/src/op/softmax.cpp create mode 100644 ngraph/frontend/paddlepaddle/src/op/softmax.hpp create mode 100644 ngraph/frontend/paddlepaddle/src/op/squeeze.cpp create mode 100644 ngraph/frontend/paddlepaddle/src/op/squeeze.hpp create mode 100644 ngraph/frontend/paddlepaddle/src/op/unsqueeze.cpp create mode 100644 ngraph/frontend/paddlepaddle/src/op/unsqueeze.hpp create mode 100644 ngraph/frontend/paddlepaddle/src/op/yolo_box.cpp create mode 100644 ngraph/frontend/paddlepaddle/src/op/yolo_box.hpp diff --git a/ngraph/frontend/paddlepaddle/src/op/mul.cpp b/ngraph/frontend/paddlepaddle/src/op/mul.cpp new file mode 100644 index 00000000000000..bb03f97eda74b8 --- /dev/null +++ b/ngraph/frontend/paddlepaddle/src/op/mul.cpp @@ -0,0 +1,67 @@ +// Copyright (C) 2018-2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#include "mul.hpp" +#include + +namespace ngraph +{ + namespace frontend + { + namespace pdpd + { + namespace op + { + NamedOutputs mul(const NodeContext& node) + { + auto x = node.get_ng_input("X"); + auto y = node.get_ng_input("Y"); + PDPD_OP_VALIDATION_CHECK(node, + x.get_partial_shape().rank().is_static(), + "matmul: X rank must be static!"); + int64_t x_rank = x.get_partial_shape().rank().get_length(); + PDPD_OP_VALIDATION_CHECK(node, + y.get_partial_shape().rank().is_static() && + y.get_partial_shape().rank().get_length() == 2, + "matmul: Y rank must be static, and 2!"); + if (x_rank > 2) + { + auto shape = std::make_shared(x); + int64_t x_num_col_dims = node.get_attribute("x_num_col_dims"); + auto axis = ngraph::opset6::Constant::create(ngraph::element::i64, {}, {0}); + auto split_lengths = ngraph::opset6::Constant::create( + ngraph::element::i64, {2}, {x_num_col_dims, x_rank - x_num_col_dims}); + auto split = std::make_shared( + shape, axis, split_lengths); + auto f_dim_red_axis = + ngraph::opset6::Constant::create(ngraph::element::i64, {}, {0}); + auto first_dim_reduce = std::make_shared( + split->output(0), f_dim_red_axis); + auto f_dim_shape = + ngraph::opset6::Constant::create(ngraph::element::i64, {1}, {1}); + auto first_dim = std::make_shared( + first_dim_reduce, f_dim_shape, false); + auto s_dim_red_axis = + ngraph::opset6::Constant::create(ngraph::element::i64, {}, {0}); + auto second_dim_reduce = std::make_shared( + split->output(1), s_dim_red_axis); + auto s_dim_shape = + ngraph::opset6::Constant::create(ngraph::element::i64, {1}, {1}); + auto second_dim = std::make_shared( + second_dim_reduce, s_dim_shape, false); + auto out_shape = std::make_shared( + ngraph::NodeVector{first_dim, second_dim}, 0); + auto x_reshaped = + std::make_shared(x, out_shape, false); + return node.default_single_output_mapping( + {std::make_shared(x_reshaped, y)}, {"Out"}); + } + return node.default_single_output_mapping( + {std::make_shared(x, y)}, {"Out"}); + } + + } // namespace op + } // namespace pdpd + } // namespace frontend +} // namespace ngraph \ No newline at end of file diff --git a/ngraph/frontend/paddlepaddle/src/op/mul.hpp b/ngraph/frontend/paddlepaddle/src/op/mul.hpp new file mode 100644 index 00000000000000..b6ef8f5a3f23e7 --- /dev/null +++ b/ngraph/frontend/paddlepaddle/src/op/mul.hpp @@ -0,0 +1,20 @@ +// Copyright (C) 2018-2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#pragma once +#include "node_context.hpp" + +namespace ngraph +{ + namespace frontend + { + namespace pdpd + { + namespace op + { + NamedOutputs mul(const NodeContext& node); + } + } // namespace pdpd + } // namespace frontend +} // namespace ngraph \ No newline at end of file diff --git a/ngraph/frontend/paddlepaddle/src/op/pad3d.cpp b/ngraph/frontend/paddlepaddle/src/op/pad3d.cpp new file mode 100644 index 00000000000000..530a382a65304e --- /dev/null +++ b/ngraph/frontend/paddlepaddle/src/op/pad3d.cpp @@ -0,0 +1,117 @@ +// Copyright (C) 2018-2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#include "pad3d.hpp" +#include + +namespace ngraph +{ + namespace frontend + { + namespace pdpd + { + namespace op + { + NamedOutputs pad3d(const NodeContext& node) + { + auto data = node.get_ng_input("X"); + auto mode = node.get_attribute("mode"); + auto value = node.get_attribute("value", 0.0); + auto data_format = node.get_attribute("data_format"); + + auto paddings = std::vector(6, 0); + + // padding of type int feature only supported by PaddlePaddle 'develop' + // version(>=2.1.0) + if (node.has_attribute>("paddings")) + { + auto paddings_vector = node.get_attribute>("paddings"); + PDPD_OP_VALIDATION_CHECK(node, + paddings_vector.size() == 6, + "paddings Params size should be 6 in pad3d!"); + paddings = paddings_vector; + } + else if (node.has_attribute("paddings")) + { + auto padding_int = node.get_attribute("paddings"); + for (int i = 0; i < 6; i++) + paddings[i] = padding_int; + } + else + { + throw ngraph::ngraph_error("Unsupported paddings attribute!"); + } + + auto pads_begin = std::vector(5, 0); + auto pads_end = std::vector(5, 0); + + Output values; + Output padding_begin; + Output padding_end; + + ngraph::op::PadMode pad_mode; + // TODO Support Circular mode in #55704 + if (mode == "constant") + { + pad_mode = ngraph::op::PadMode::CONSTANT; + values = ngraph::opset6::Constant::create( + element::f32, ngraph::Shape{}, {value}); + } + else if (mode == "reflect") + { + pad_mode = ngraph::op::PadMode::REFLECT; + } + else if (mode == "replicate") + { + pad_mode = ngraph::op::PadMode::EDGE; + } + else + { + throw ngraph::ngraph_error("Unsupported 3d paddings mode: [" + mode + "]"); + } + + if (data_format == "NCDHW") + { + pads_begin[4] = paddings[0]; // left + pads_end[4] = paddings[1]; // right + pads_begin[3] = paddings[2]; // top + pads_end[3] = paddings[3]; // down + pads_begin[2] = paddings[4]; // front + pads_end[2] = paddings[5]; // back + } + else if (data_format == "NDHWC") + { + pads_begin[3] = paddings[0]; // left + pads_end[3] = paddings[1]; // right + pads_begin[2] = paddings[2]; // top + pads_end[2] = paddings[3]; // down + pads_begin[1] = paddings[4]; // front + pads_end[1] = paddings[5]; // back + } + else + { + throw ngraph::ngraph_error("Unsupported 3d paddings data_format: [" + + data_format + "]"); + } + + padding_begin = ngraph::opset6::Constant::create( + element::i32, ngraph::Shape{pads_begin.size()}, pads_begin); + padding_end = ngraph::opset6::Constant::create( + element::i32, ngraph::Shape{pads_end.size()}, pads_end); + + if (mode == "constant") + return node.default_single_output_mapping( + {std::make_shared( + data, padding_begin, padding_end, values, pad_mode)}, + {"Out"}); + else + return node.default_single_output_mapping( + {std::make_shared( + data, padding_begin, padding_end, pad_mode)}, + {"Out"}); + } + } // namespace op + } // namespace pdpd + } // namespace frontend +} // namespace ngraph diff --git a/ngraph/frontend/paddlepaddle/src/op/pad3d.hpp b/ngraph/frontend/paddlepaddle/src/op/pad3d.hpp new file mode 100644 index 00000000000000..1e1a3bbed44954 --- /dev/null +++ b/ngraph/frontend/paddlepaddle/src/op/pad3d.hpp @@ -0,0 +1,20 @@ +// Copyright (C) 2018-2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#pragma once +#include "node_context.hpp" + +namespace ngraph +{ + namespace frontend + { + namespace pdpd + { + namespace op + { + NamedOutputs pad3d(const NodeContext& node); + } + } // namespace pdpd + } // namespace frontend +} // namespace ngraph diff --git a/ngraph/frontend/paddlepaddle/src/op/pool2d.cpp b/ngraph/frontend/paddlepaddle/src/op/pool2d.cpp new file mode 100644 index 00000000000000..334378a41fd785 --- /dev/null +++ b/ngraph/frontend/paddlepaddle/src/op/pool2d.cpp @@ -0,0 +1,265 @@ +//***************************************************************************** +// Copyright (C) 2018-2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// +//***************************************************************************** + +#include "pool2d.hpp" +#include + +namespace ngraph +{ + namespace frontend + { + namespace pdpd + { + namespace op + { + // helper func - get pad_begin and pad_end + static void get_paddings(const NodeContext& node, + ngraph::Shape& pad_begin, + ngraph::Shape& pad_end, + ngraph::op::PadType& auto_pad) + { + if (node.has_attribute("padding_algorithm")) + { + auto pad_algo = node.get_attribute("padding_algorithm"); + if (pad_algo == "SAME") + { + auto_pad = ngraph::op::PadType::SAME_UPPER; + } + else if (pad_algo == "VALID") + { + auto_pad = ngraph::op::PadType::VALID; + } + else if (pad_algo == "EXPLICIT") + { + auto_pad = ngraph::op::PadType::EXPLICIT; + } + else + { + throw std::runtime_error("Unsupported pooling padding_algorithm " + + pad_algo); + } + } + else + { + // adaptive_maxpool with no such attr. + auto_pad = ngraph::op::PadType::EXPLICIT; + } + + /*If pool padding size is a tuple or list, it could be in three forms: + [pad_height, pad_width] or [pad_height_top, pad_height_bottom, pad_width_left, + pad_width_right], and when data_format is “NCHW”, pool_padding can be in the + form [[0,0], [0,0], [pad_height_top, pad_height_bottom], [pad_width_left, + pad_width_right]]. when data_format is “NHWC”, pool_padding can be in the form + [[0,0], [pad_height_top, pad_height_bottom], [pad_width_left, pad_width_right], + [0,0]]. Otherwise, the pool padding size will be a square of an int.*/ + auto paddings = node.get_attribute>("paddings"); + + // Default is empty for 'adaptive max pooling' + auto data_format = node.get_attribute("data_format", {}); + + // TODO: need to support NHWC input #55483 + switch (paddings.size()) + { + case 2: + pad_begin = Shape{static_cast(paddings[0]), + static_cast(paddings[1])}; + pad_end = pad_begin; + break; + case 4: + pad_begin = Shape{static_cast(paddings[0]), + static_cast(paddings[2])}; + pad_end = Shape{static_cast(paddings[1]), + static_cast(paddings[3])}; + break; + default: + throw std::runtime_error("Unsupported pooling paddings " + + std::to_string(paddings.size())); + } + } + + NamedOutputs pool2d(const NodeContext& node) + { + auto data = node.get_ng_input("X"); + + auto pooling_type = node.get_attribute("pooling_type", {}); + auto global_pooling = node.get_attribute("global_pooling"); + auto adaptive = node.get_attribute("adaptive"); + auto kernel_shape = node.get_attribute>("ksize"); + + auto rounding_type = node.get_attribute("ceil_mode", false) + ? ngraph::op::RoundingType::CEIL + : ngraph::op::RoundingType::FLOOR; + + if (pooling_type.empty()) + { + pooling_type = "max"; + } + + PDPD_ASSERT((pooling_type == "max") || (pooling_type == "avg"), + "pool2d: not supported pooling type !"); + PDPD_ASSERT(kernel_shape.size() == 1 || kernel_shape.size() == 2, + "pool2d: ksize must be 1 or 2!"); + + PartialShape input_shape = data.get_partial_shape(); + + int32_t input_rank = input_shape.rank().get_length(); + PDPD_ASSERT(input_rank >= 2, "input tensor rank must be greater than 2"); + + auto auto_pad = ngraph::op::PadType::EXPLICIT; + ngraph::Shape pad_begin, pad_end; + get_paddings(node, pad_begin, pad_end, auto_pad); + + if (global_pooling || + (adaptive && std::any_of(kernel_shape.begin(), + kernel_shape.end(), + [](int32_t i) { return i == 1; }))) + { + if (pooling_type == "max") + { + auto axes = ngraph::opset6::Constant::create( + ngraph::element::i64, {2}, {input_rank - 2, input_rank - 1}); + return node.default_single_output_mapping( + {std::make_shared(data, axes, true)}, + {"Out"}); + } + else + { + auto axes = ngraph::opset6::Constant::create( + ngraph::element::i64, {2}, {input_rank - 2, input_rank - 1}); + return node.default_single_output_mapping( + {std::make_shared(data, axes, true)}, + {"Out"}); + } + } + else if (adaptive) + { + PDPD_ASSERT(input_shape[2].is_static() && input_shape[3].is_static(), + "pool2d: spatial dim must be static when using adaptive pool"); + uint64_t pool_size_Height, pool_size_Width; + uint64_t input_h = input_shape[input_rank - 2].get_length(); + uint64_t input_w = input_shape[input_rank - 1].get_length(); + + if (kernel_shape.size() == 1) + { + // Not tested: implemented according to spec, but can't generate real + // model to test + pool_size_Height = pool_size_Width = kernel_shape[0]; + } + else + { + pool_size_Height = kernel_shape[0]; + pool_size_Width = kernel_shape[1]; + } + + uint64_t stride_h = int64_t(input_h / pool_size_Height); + uint64_t stride_w = int64_t(input_w / pool_size_Width); + uint64_t kernel_h = input_h - (pool_size_Height - 1) * stride_h; + uint64_t kernel_w = input_w - (pool_size_Width - 1) * stride_w; + + PDPD_ASSERT(stride_h >= 1 && stride_w >= 1, + "Pool2d stride must be greater than 1"); + + if (pooling_type == "max") + { + return node.default_single_output_mapping( + {std::make_shared( + data, + ngraph::Strides{stride_h, stride_w}, + pad_begin, + pad_end, + ngraph::Shape{kernel_h, kernel_w}, + rounding_type, + auto_pad)}, + {"Out"}); + } + else + { + bool exclude_pad = node.get_attribute("exclusive", false); + return node.default_single_output_mapping( + {std::make_shared( + data, + ngraph::Strides{stride_h, stride_w}, + pad_begin, + pad_end, + ngraph::Shape{kernel_h, kernel_w}, + exclude_pad, + rounding_type, + auto_pad)}, + {"Out"}); + } + } + else + { + auto strides = node.get_attribute>("strides"); + auto paddings = node.get_attribute>("paddings"); + + uint64_t kernel_h, kernel_w; + if (kernel_shape.size() == 1) + { + // Not tested: implemented according to spec, but can't generate real + // model to test + kernel_h = kernel_w = kernel_shape[0]; + } + else + { + kernel_h = kernel_shape[0]; + kernel_w = kernel_shape[1]; + } + + PDPD_ASSERT(kernel_h > 0 && kernel_w > 0, + "pool2d kernel shape must be greater than 0"); + + // Note: this shape check is only valid when the spatial dim of input_shape + // is static. + if (input_shape[2].is_static() && input_shape[3].is_static()) + { + uint64_t input_h = input_shape[input_rank - 2].get_length(); + uint64_t input_w = input_shape[input_rank - 1].get_length(); + if ((input_h > 0) && (input_h + pad_begin[0] + pad_end[0] < kernel_h)) + { + kernel_h = input_h + pad_begin[0] + pad_end[0]; + } + if ((input_w > 0) && (input_w + pad_begin[1] + pad_end[1] < kernel_w)) + { + kernel_w = input_w + pad_begin[1] + pad_end[1]; + } + } + + if (pooling_type == "max") + { + return node.default_single_output_mapping( + {std::make_shared( + data, + ngraph::Strides(strides.begin(), strides.end()), + pad_begin, + pad_end, + ngraph::Shape{kernel_h, kernel_w}, + rounding_type, + auto_pad)}, + {"Out"}); + } + else + { + bool exclude_pad = node.get_attribute("exclusive", false); + return node.default_single_output_mapping( + {std::make_shared( + data, + ngraph::Strides(strides.begin(), strides.end()), + pad_begin, + pad_end, + ngraph::Shape{kernel_h, kernel_w}, + exclude_pad, + rounding_type, + auto_pad)}, + {"Out"}); + } + } + } + + } // namespace op + } // namespace pdpd + } // namespace frontend +} // namespace ngraph \ No newline at end of file diff --git a/ngraph/frontend/paddlepaddle/src/op/pool2d.hpp b/ngraph/frontend/paddlepaddle/src/op/pool2d.hpp new file mode 100644 index 00000000000000..844bc6c99a6771 --- /dev/null +++ b/ngraph/frontend/paddlepaddle/src/op/pool2d.hpp @@ -0,0 +1,20 @@ +// Copyright (C) 2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#pragma once +#include "node_context.hpp" + +namespace ngraph +{ + namespace frontend + { + namespace pdpd + { + namespace op + { + NamedOutputs pool2d(const NodeContext& node); + } + } // namespace pdpd + } // namespace frontend +} // namespace ngraph \ No newline at end of file diff --git a/ngraph/frontend/paddlepaddle/src/op/pow.cpp b/ngraph/frontend/paddlepaddle/src/op/pow.cpp new file mode 100644 index 00000000000000..be1cd3e9d7dcd3 --- /dev/null +++ b/ngraph/frontend/paddlepaddle/src/op/pow.cpp @@ -0,0 +1,41 @@ +// Copyright (C) 2018-2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// +#include "pow.hpp" +#include +#include +#include + +namespace ngraph +{ + namespace frontend + { + namespace pdpd + { + namespace op + { + NamedOutputs pow(const NodeContext& node) + { + auto x = node.get_ng_input("X"); + auto dtype = x.get_element_type(); + Output factor_node; + if (node.has_ng_input("FactorTensor")) + { + factor_node = node.get_ng_input("FactorTensor"); + if (factor_node.get_element_type() != dtype) + factor_node = std::make_shared(factor_node, dtype); + } + else + { + factor_node = builder::make_constant( + dtype, Shape{1}, node.get_attribute("factor")); + } + + return node.default_single_output_mapping( + {std::make_shared(x, factor_node)}, {"Out"}); + } + + } // namespace op + } // namespace pdpd + } // namespace frontend +} // namespace ngraph \ No newline at end of file diff --git a/ngraph/frontend/paddlepaddle/src/op/pow.hpp b/ngraph/frontend/paddlepaddle/src/op/pow.hpp new file mode 100644 index 00000000000000..85f14104db4cf6 --- /dev/null +++ b/ngraph/frontend/paddlepaddle/src/op/pow.hpp @@ -0,0 +1,20 @@ +// Copyright (C) 2018-2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#pragma once +#include "node_context.hpp" + +namespace ngraph +{ + namespace frontend + { + namespace pdpd + { + namespace op + { + NamedOutputs pow(const NodeContext& node); + } + } // namespace pdpd + } // namespace frontend +} // namespace ngraph \ No newline at end of file diff --git a/ngraph/frontend/paddlepaddle/src/op/range.cpp b/ngraph/frontend/paddlepaddle/src/op/range.cpp new file mode 100644 index 00000000000000..972374b617170c --- /dev/null +++ b/ngraph/frontend/paddlepaddle/src/op/range.cpp @@ -0,0 +1,41 @@ +// Copyright (C) 2018-2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#include "range.hpp" +#include + +namespace ngraph +{ + namespace frontend + { + namespace pdpd + { + namespace op + { + NamedOutputs range(const NodeContext& node) + { + auto start = node.get_ng_input("Start"); + auto stop = node.get_ng_input("End"); + auto step = node.get_ng_input("Step"); + auto type = node.get_out_port_type("Out"); + PDPD_ASSERT(type == element::i64 || type == element::i32 || + type == element::f32, + "Only supports int32, int64, float32"); + + const auto axis = ngraph::opset6::Constant::create(element::i64, Shape{}, {0}); + auto start_scalar = std::make_shared(start, axis); + auto stop_scalar = std::make_shared(stop, axis); + auto step_scalar = std::make_shared(step, axis); + + // TODO to support other data types other than FP32 #55267 + return node.default_single_output_mapping( + {std::make_shared( + start_scalar, stop_scalar, step_scalar, type)}, + {"Out"}); + } + + } // namespace op + } // namespace pdpd + } // namespace frontend +} // namespace ngraph diff --git a/ngraph/frontend/paddlepaddle/src/op/range.hpp b/ngraph/frontend/paddlepaddle/src/op/range.hpp new file mode 100644 index 00000000000000..1789f3bf671f7f --- /dev/null +++ b/ngraph/frontend/paddlepaddle/src/op/range.hpp @@ -0,0 +1,20 @@ +// Copyright (C) 2018-2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#pragma once +#include "node_context.hpp" + +namespace ngraph +{ + namespace frontend + { + namespace pdpd + { + namespace op + { + NamedOutputs range(const NodeContext& node); + } + } // namespace pdpd + } // namespace frontend +} // namespace ngraph \ No newline at end of file diff --git a/ngraph/frontend/paddlepaddle/src/op/relu6.cpp b/ngraph/frontend/paddlepaddle/src/op/relu6.cpp new file mode 100644 index 00000000000000..c5430d67343919 --- /dev/null +++ b/ngraph/frontend/paddlepaddle/src/op/relu6.cpp @@ -0,0 +1,28 @@ +// Copyright (C) 2018-2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#include "relu6.hpp" +#include +#include + +namespace ngraph +{ + namespace frontend + { + namespace pdpd + { + namespace op + { + NamedOutputs relu6(const NodeContext& node) + { + auto data = node.get_ng_input("X"); + auto threshold = node.get_attribute("threshold", 6.0f); + return node.default_single_output_mapping( + {std::make_shared(data, 0.0, threshold)}, {"Out"}); + } + + } // namespace op + } // namespace pdpd + } // namespace frontend +} // namespace ngraph \ No newline at end of file diff --git a/ngraph/frontend/paddlepaddle/src/op/relu6.hpp b/ngraph/frontend/paddlepaddle/src/op/relu6.hpp new file mode 100644 index 00000000000000..0d555e4c8e2746 --- /dev/null +++ b/ngraph/frontend/paddlepaddle/src/op/relu6.hpp @@ -0,0 +1,20 @@ +// Copyright (C) 2018-2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#pragma once +#include "node_context.hpp" + +namespace ngraph +{ + namespace frontend + { + namespace pdpd + { + namespace op + { + NamedOutputs relu6(const NodeContext& node); + } + } // namespace pdpd + } // namespace frontend +} // namespace ngraph \ No newline at end of file diff --git a/ngraph/frontend/paddlepaddle/src/op/reshape2.cpp b/ngraph/frontend/paddlepaddle/src/op/reshape2.cpp new file mode 100644 index 00000000000000..3e612c16939095 --- /dev/null +++ b/ngraph/frontend/paddlepaddle/src/op/reshape2.cpp @@ -0,0 +1,56 @@ +// Copyright (C) 2018-2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#include "reshape2.hpp" +#include +#include + +namespace ngraph +{ + namespace frontend + { + namespace pdpd + { + namespace op + { + NamedOutputs reshape2(const NodeContext& node) + { + auto data = node.get_ng_input("X"); + if (!node.has_ng_input("Shape") && !node.has_ng_input("ShapeTensor")) + { + auto shape_attr = node.get_attribute>("shape"); + auto shape_node = ngraph::opset6::Constant::create( + ngraph::element::i32, {shape_attr.size()}, shape_attr); + return node.default_single_output_mapping( + {std::make_shared(data, shape_node, true)}, + {"Out"}); + } + else + { + std::string name = "Shape"; + if (node.has_ng_input("ShapeTensor")) + { + name = "ShapeTensor"; + } + + auto nodes = node.get_ng_inputs(name); + ngraph::NodeVector node_vec; + for (auto& input_node : nodes) + { + auto cast = + std::make_shared(input_node, element::i64); + node_vec.push_back(cast); + } + + auto shape_node = std::make_shared(node_vec, 0); + return node.default_single_output_mapping( + {std::make_shared(data, shape_node, true)}, + {"Out"}); + } + } + + } // namespace op + } // namespace pdpd + } // namespace frontend +} // namespace ngraph \ No newline at end of file diff --git a/ngraph/frontend/paddlepaddle/src/op/reshape2.hpp b/ngraph/frontend/paddlepaddle/src/op/reshape2.hpp new file mode 100644 index 00000000000000..390fd45cf0c386 --- /dev/null +++ b/ngraph/frontend/paddlepaddle/src/op/reshape2.hpp @@ -0,0 +1,20 @@ +// Copyright (C) 2018-2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#pragma once +#include "node_context.hpp" + +namespace ngraph +{ + namespace frontend + { + namespace pdpd + { + namespace op + { + NamedOutputs reshape2(const NodeContext& node); + } // namespace op + } // namespace pdpd + } // namespace frontend +} // namespace ngraph \ No newline at end of file diff --git a/ngraph/frontend/paddlepaddle/src/op/shape.cpp b/ngraph/frontend/paddlepaddle/src/op/shape.cpp new file mode 100644 index 00000000000000..d10eac182dfcad --- /dev/null +++ b/ngraph/frontend/paddlepaddle/src/op/shape.cpp @@ -0,0 +1,26 @@ +// Copyright (C) 2018-2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#include "shape.hpp" +#include + +namespace ngraph +{ + namespace frontend + { + namespace pdpd + { + namespace op + { + NamedOutputs shape(const NodeContext& node) + { + auto data = node.get_ng_input("Input"); + return node.default_single_output_mapping( + {std::make_shared(data)}, {"Out"}); + } + + } // namespace op + } // namespace pdpd + } // namespace frontend +} // namespace ngraph \ No newline at end of file diff --git a/ngraph/frontend/paddlepaddle/src/op/shape.hpp b/ngraph/frontend/paddlepaddle/src/op/shape.hpp new file mode 100644 index 00000000000000..8e8be97c539167 --- /dev/null +++ b/ngraph/frontend/paddlepaddle/src/op/shape.hpp @@ -0,0 +1,20 @@ +// Copyright (C) 2018-2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#pragma once +#include "node_context.hpp" + +namespace ngraph +{ + namespace frontend + { + namespace pdpd + { + namespace op + { + NamedOutputs shape(const NodeContext& node); + } + } // namespace pdpd + } // namespace frontend +} // namespace ngraph \ No newline at end of file diff --git a/ngraph/frontend/paddlepaddle/src/op/sigmoid.cpp b/ngraph/frontend/paddlepaddle/src/op/sigmoid.cpp new file mode 100644 index 00000000000000..786e009ca18a4a --- /dev/null +++ b/ngraph/frontend/paddlepaddle/src/op/sigmoid.cpp @@ -0,0 +1,26 @@ +// Copyright (C) 2018-2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#include "sigmoid.hpp" +#include + +namespace ngraph +{ + namespace frontend + { + namespace pdpd + { + namespace op + { + NamedOutputs sigmoid(const NodeContext& node) + { + auto data = node.get_ng_input("X"); + return node.default_single_output_mapping( + {std::make_shared(data)}, {"Out"}); + } + + } // namespace op + } // namespace pdpd + } // namespace frontend +} // namespace ngraph \ No newline at end of file diff --git a/ngraph/frontend/paddlepaddle/src/op/sigmoid.hpp b/ngraph/frontend/paddlepaddle/src/op/sigmoid.hpp new file mode 100644 index 00000000000000..15b62324b81f65 --- /dev/null +++ b/ngraph/frontend/paddlepaddle/src/op/sigmoid.hpp @@ -0,0 +1,20 @@ +// Copyright (C) 2018-2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#pragma once +#include "node_context.hpp" + +namespace ngraph +{ + namespace frontend + { + namespace pdpd + { + namespace op + { + NamedOutputs sigmoid(const NodeContext& node); + } + } // namespace pdpd + } // namespace frontend +} // namespace ngraph \ No newline at end of file diff --git a/ngraph/frontend/paddlepaddle/src/op/slice.cpp b/ngraph/frontend/paddlepaddle/src/op/slice.cpp new file mode 100644 index 00000000000000..5dd14d0179b212 --- /dev/null +++ b/ngraph/frontend/paddlepaddle/src/op/slice.cpp @@ -0,0 +1,61 @@ +// Copyright (C) 2018-2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#include "slice.hpp" +#include +#include + +namespace ngraph +{ + namespace frontend + { + namespace pdpd + { + namespace op + { + NamedOutputs slice(const NodeContext& node) + { + auto data = node.get_ng_input("Input"); + auto axes = node.get_attribute>("axes"); + // TODO: support tensor type #55266 + auto starts = node.get_attribute>("starts"); + // TODO: support tensor type #55266 + auto ends = node.get_attribute>("ends"); + auto data_rank = data.get_partial_shape().rank(); + size_t shape_size = data_rank.get_length(); + std::vector fixedStarts(shape_size, 0); + std::vector fixedEnds(shape_size, INT_MAX); + + int n = 0; + for (auto i : axes) + { + PDPD_OP_VALIDATION_CHECK(node, + i < (int32_t)shape_size, + "slice: axes must be less than the X rank."); + fixedStarts[i] = starts[n]; + fixedEnds[i] = ends[n]; + n++; + } + + auto startsNode = ngraph::opset6::Constant::create( + ngraph::element::i32, {shape_size}, fixedStarts); + auto endsNode = ngraph::opset6::Constant::create( + ngraph::element::i32, {shape_size}, fixedEnds); + auto stridesNode = ngraph::opset6::Constant::create( + ngraph::element::i32, {shape_size}, std::vector(shape_size, 1)); + return node.default_single_output_mapping( + {std::make_shared( + data, + startsNode, + endsNode, + stridesNode, + std::vector(shape_size, 0), + std::vector(shape_size, 0))}, + {"Out"}); + } + + } // namespace op + } // namespace pdpd + } // namespace frontend +} // namespace ngraph \ No newline at end of file diff --git a/ngraph/frontend/paddlepaddle/src/op/slice.hpp b/ngraph/frontend/paddlepaddle/src/op/slice.hpp new file mode 100644 index 00000000000000..fe691be1509f0b --- /dev/null +++ b/ngraph/frontend/paddlepaddle/src/op/slice.hpp @@ -0,0 +1,20 @@ +// Copyright (C) 2018-2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#pragma once +#include "node_context.hpp" + +namespace ngraph +{ + namespace frontend + { + namespace pdpd + { + namespace op + { + NamedOutputs slice(const NodeContext& node); + } + } // namespace pdpd + } // namespace frontend +} // namespace ngraph \ No newline at end of file diff --git a/ngraph/frontend/paddlepaddle/src/op/softmax.cpp b/ngraph/frontend/paddlepaddle/src/op/softmax.cpp new file mode 100644 index 00000000000000..0fb79073a57e40 --- /dev/null +++ b/ngraph/frontend/paddlepaddle/src/op/softmax.cpp @@ -0,0 +1,34 @@ +// Copyright (C) 2018-2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#include "softmax.hpp" +#include + +namespace ngraph +{ + namespace frontend + { + namespace pdpd + { + namespace op + { + NamedOutputs softmax(const NodeContext& node) + { + auto data = node.get_ng_input("X"); + auto axis = node.get_attribute("axis"); + if (axis < 0) + { + PDPD_OP_VALIDATION_CHECK(node, + data.get_partial_shape().rank().is_static(), + "Softmax rank must be static"); + auto data_rank = data.get_partial_shape().rank().get_length(); + axis = data_rank + axis; + } + return node.default_single_output_mapping( + {std::make_shared(data, axis)}, {"Out"}); + } + } // namespace op + } // namespace pdpd + } // namespace frontend +} // namespace ngraph \ No newline at end of file diff --git a/ngraph/frontend/paddlepaddle/src/op/softmax.hpp b/ngraph/frontend/paddlepaddle/src/op/softmax.hpp new file mode 100644 index 00000000000000..65d3686d61ae39 --- /dev/null +++ b/ngraph/frontend/paddlepaddle/src/op/softmax.hpp @@ -0,0 +1,20 @@ +// Copyright (C) 2018-2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#pragma once +#include "node_context.hpp" + +namespace ngraph +{ + namespace frontend + { + namespace pdpd + { + namespace op + { + NamedOutputs softmax(const NodeContext& node); + } // namespace op + } // namespace pdpd + } // namespace frontend +} // namespace ngraph \ No newline at end of file diff --git a/ngraph/frontend/paddlepaddle/src/op/squeeze.cpp b/ngraph/frontend/paddlepaddle/src/op/squeeze.cpp new file mode 100644 index 00000000000000..485b713eea6155 --- /dev/null +++ b/ngraph/frontend/paddlepaddle/src/op/squeeze.cpp @@ -0,0 +1,34 @@ +// Copyright (C) 2018-2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#include "squeeze.hpp" +#include + +namespace ngraph +{ + namespace frontend + { + namespace pdpd + { + namespace op + { + NamedOutputs squeeze(const NodeContext& node) + { + auto data = node.get_ng_input("X"); + std::vector axes; + if (node.has_attribute>("axes")) + { + axes = node.get_attribute>("axes"); + } + + auto axesNode = + ngraph::opset6::Constant::create(ngraph::element::i32, {axes.size()}, axes); + return node.default_single_output_mapping( + {std::make_shared(data, axesNode)}, {"Out"}); + } + + } // namespace op + } // namespace pdpd + } // namespace frontend +} // namespace ngraph \ No newline at end of file diff --git a/ngraph/frontend/paddlepaddle/src/op/squeeze.hpp b/ngraph/frontend/paddlepaddle/src/op/squeeze.hpp new file mode 100644 index 00000000000000..d41e3bbb6a3c51 --- /dev/null +++ b/ngraph/frontend/paddlepaddle/src/op/squeeze.hpp @@ -0,0 +1,20 @@ +// Copyright (C) 2018-2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#pragma once +#include "node_context.hpp" + +namespace ngraph +{ + namespace frontend + { + namespace pdpd + { + namespace op + { + NamedOutputs squeeze(const NodeContext& node); + } + } // namespace pdpd + } // namespace frontend +} // namespace ngraph \ No newline at end of file diff --git a/ngraph/frontend/paddlepaddle/src/op/unsqueeze.cpp b/ngraph/frontend/paddlepaddle/src/op/unsqueeze.cpp new file mode 100644 index 00000000000000..dc9b7e0bb9c2e9 --- /dev/null +++ b/ngraph/frontend/paddlepaddle/src/op/unsqueeze.cpp @@ -0,0 +1,30 @@ +// Copyright (C) 2018-2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#include "unsqueeze.hpp" +#include + +namespace ngraph +{ + namespace frontend + { + namespace pdpd + { + namespace op + { + NamedOutputs unsqueeze(const NodeContext& node) + { + // TODO to support data type other than int32_t #55168 + auto data = node.get_ng_input("X"); + auto axes = node.get_attribute>("axes"); + auto axesNode = + ngraph::opset6::Constant::create(ngraph::element::i32, {axes.size()}, axes); + return node.default_single_output_mapping( + {std::make_shared(data, axesNode)}, {"Out"}); + } + + } // namespace op + } // namespace pdpd + } // namespace frontend +} // namespace ngraph diff --git a/ngraph/frontend/paddlepaddle/src/op/unsqueeze.hpp b/ngraph/frontend/paddlepaddle/src/op/unsqueeze.hpp new file mode 100644 index 00000000000000..9a9a5000fd487a --- /dev/null +++ b/ngraph/frontend/paddlepaddle/src/op/unsqueeze.hpp @@ -0,0 +1,20 @@ +// Copyright (C) 2018-2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#pragma once +#include "node_context.hpp" + +namespace ngraph +{ + namespace frontend + { + namespace pdpd + { + namespace op + { + NamedOutputs unsqueeze(const NodeContext& node); + } + } // namespace pdpd + } // namespace frontend +} // namespace ngraph \ No newline at end of file diff --git a/ngraph/frontend/paddlepaddle/src/op/yolo_box.cpp b/ngraph/frontend/paddlepaddle/src/op/yolo_box.cpp new file mode 100644 index 00000000000000..854d42041b4761 --- /dev/null +++ b/ngraph/frontend/paddlepaddle/src/op/yolo_box.cpp @@ -0,0 +1,440 @@ +// Copyright (C) 2018-2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// +#include // std::numeric_limits +#include + +#include +#include "yolo_box.hpp" + +namespace ngraph +{ + namespace frontend + { + namespace pdpd + { + namespace op + { + using namespace opset6; + using namespace element; + + // reference + // Paddle/python/paddle/fluid/tests/unittests/test_yolo_box_op.py + // Paddle/paddle/fluid/operators/detection/yolo_box_op.h + // Paddle2ONNX/paddle2onnx/op_mapper/detection/yolo_box.py - clip_bbox is not used + // by Paddle2ONNX. + NamedOutputs yolo_box(const NodeContext& node_context) + { + auto data = node_context.get_ng_input("X"); + auto image_size = node_context.get_ng_input("ImgSize"); + + // get shape of X + auto input_shape = std::make_shared(data, i64); + auto indices_batchsize = Constant::create(i64, {1}, {0}); + auto indices_height = Constant::create(i64, {1}, {2}); + auto indices_width = Constant::create(i64, {1}, {3}); + auto const_axis0 = Constant::create(i64, {1}, {0}); + auto input_height = + std::make_shared(input_shape, indices_height, const_axis0); // H + auto input_width = + std::make_shared(input_shape, indices_width, const_axis0); // W + auto batch_size = + std::make_shared(input_shape, indices_batchsize, const_axis0); // N + + int32_t class_num = node_context.get_attribute("class_num"); + auto const_class_num = Constant::create(i64, {1}, {class_num}); + + // PDPD anchors attribute is of type int32. Convert to float for computing + // convinient. + auto _anchors = node_context.get_attribute>("anchors"); + std::vector anchors(_anchors.begin(), _anchors.end()); + uint32_t num_anchors = anchors.size() / 2; + auto const_num_anchors = Constant::create(i64, {1}, {num_anchors}); + + auto default_scale = 1.0f; + auto scale_x_y = node_context.get_attribute("scale_x_y", default_scale); + + auto downsample_ratio = node_context.get_attribute("downsample_ratio"); + auto const_downsample_ratio = + Constant::create(i64, {1}, {downsample_ratio}); + auto scaled_input_height = + std::make_shared(input_height, const_downsample_ratio); + auto scaled_input_width = + std::make_shared(input_width, const_downsample_ratio); + + // score_shape {batch_size, input_height * input_width * num_anchors, class_num} + auto node_mul_whc = std::make_shared(input_height, input_width); + node_mul_whc = std::make_shared(node_mul_whc, const_num_anchors); + auto score_shape = std::make_shared( + NodeVector{batch_size, node_mul_whc, const_class_num}, 0); + + auto conf_thresh = node_context.get_attribute("conf_thresh"); + auto const_conf_thresh = Constant::create(f32, {1}, {conf_thresh}); + + auto clip_bbox = node_context.get_attribute("clip_bbox"); + + std::cout << "num_anchors: " << num_anchors << " scale_x_y: " << scale_x_y + << std::endl; + std::cout << "downsample_ratio: " << downsample_ratio + << " conf_thresh: " << conf_thresh << std::endl; + std::cout << "class_num: " << class_num << " image_size: " << image_size + << std::endl; + + // main X + // node_x_shape {batch_size, num_anchors, 5 + class_num, input_height, + // input_width} + auto const_class_num_plus5 = + Constant::create(i64, {1}, {5 + class_num}); + auto node_x_shape = std::make_shared(NodeVector{batch_size, + const_num_anchors, + const_class_num_plus5, + input_height, + input_width}, + 0); + + auto node_x_reshape = std::make_shared(data, node_x_shape, false); + + auto node_input_order = Constant::create(i64, {5}, {0, 1, 3, 4, 2}); + auto node_x_transpose = + std::make_shared(node_x_reshape, node_input_order); + + // range x/y + // range_x: shape {1, input_width} containing 0...input_width + // range_y: shape {input_height, 1} containing 0...input_height + auto const_start = Constant::create(f32, {}, {0.f}); + auto const_step = Constant::create(f32, {}, {1.f}); + auto reduction_axes = Constant::create(i64, {1}, {0}); + + auto scaler_input_width = + std::make_shared(input_width, reduction_axes, false); + auto range_x = + std::make_shared(const_start, scaler_input_width, const_step, f32); + auto node_range_x = std::make_shared( + range_x, Constant::create(i64, {1}, {0})); + + auto scaler_input_height = + std::make_shared(input_height, reduction_axes, false); + auto range_y = + std::make_shared(const_start, scaler_input_height, const_step, f32); + auto node_range_y = std::make_shared( + range_y, Constant::create(i64, {1}, {1})); + + auto node_range_x_shape = std::make_shared( + NodeVector{Constant::create(i64, {1}, {1}), input_width}, 0); + auto node_range_y_shape = std::make_shared( + NodeVector{input_height, Constant::create(i64, {1}, {1})}, 0); + + auto node_grid_x = + std::make_shared(node_range_x, node_range_y_shape); // shape (H, W) + auto node_grid_y = std::make_shared(node_range_y, node_range_x_shape); + + // main X (part2) + auto node_split_axis = Constant::create(i64, {1}, {-1}); + auto node_split_lengths = + Constant::create(i64, {6}, {1, 1, 1, 1, 1, class_num}); + auto node_split_input = std::make_shared( + node_x_transpose, node_split_axis, node_split_lengths); + + auto node_box_x = + node_split_input->output(0); // shape (batch_size, num_anchors, H, W, 1) + auto node_box_y = node_split_input->output(1); + auto node_box_w = node_split_input->output(2); + auto node_box_h = node_split_input->output(3); + auto node_conf = node_split_input->output(4); + auto node_prob = node_split_input->output(5); + + // x/y + std::shared_ptr node_box_x_sigmoid = + std::make_shared(node_box_x); + std::shared_ptr node_box_y_sigmoid = + std::make_shared(node_box_y); + + if (std::fabs(scale_x_y - default_scale) > 1e-6) + { // float not-equal + float bias_x_y = -0.5 * (scale_x_y - 1.0); + + auto scale_x_y_node = Constant::create(f32, {1}, {scale_x_y}); + auto bias_x_y_node = Constant::create(f32, {1}, {bias_x_y}); + + node_box_x_sigmoid = + std::make_shared(node_box_x_sigmoid, scale_x_y_node); + node_box_x_sigmoid = + std::make_shared(node_box_x_sigmoid, bias_x_y_node); + + node_box_y_sigmoid = + std::make_shared(node_box_y_sigmoid, scale_x_y_node); + node_box_y_sigmoid = + std::make_shared(node_box_y_sigmoid, bias_x_y_node); + } + + auto squeeze_box_x = Constant::create(i64, {1}, {4}); + auto node_box_x_squeeze = + std::make_shared(node_box_x_sigmoid, squeeze_box_x); + + auto squeeze_box_y = Constant::create(i64, {1}, {4}); + auto node_box_y_squeeze = + std::make_shared(node_box_y_sigmoid, squeeze_box_y); + + auto node_box_x_add_grid = + std::make_shared(node_grid_x, node_box_x_squeeze); + auto node_box_y_add_grid = + std::make_shared(node_grid_y, node_box_y_squeeze); + + auto node_input_h = std::make_shared(input_height, element::f32); + auto node_input_w = std::make_shared(input_width, element::f32); + + auto node_box_x_encode = + std::make_shared(node_box_x_add_grid, node_input_w); + auto node_box_y_encode = + std::make_shared(node_box_y_add_grid, node_input_h); + + // w/h + auto node_anchor_tensor = + Constant::create(f32, {num_anchors, 2}, anchors); + auto split_axis = Constant::create(i64, {}, {1}); + auto node_anchor_split = + std::make_shared(node_anchor_tensor, split_axis, 2); + + auto node_anchor_w_origin = node_anchor_split->output(0); + auto node_anchor_h_origin = node_anchor_split->output(1); + + auto float_input_height = + std::make_shared(scaled_input_height, element::f32); + auto node_anchor_h = + std::make_shared(node_anchor_h_origin, float_input_height); + auto float_input_width = + std::make_shared(scaled_input_width, element::f32); + auto node_anchor_w = + std::make_shared(node_anchor_w_origin, float_input_width); + + auto node_new_anchor_shape = + Constant::create(i64, {4}, {1, num_anchors, 1, 1}); + auto node_anchor_w_reshape = + std::make_shared(node_anchor_w, node_new_anchor_shape, false); + auto node_anchor_h_reshape = + std::make_shared(node_anchor_h, node_new_anchor_shape, false); + + auto squeeze_box_wh = Constant::create(i64, {1}, {4}); + auto node_box_w_squeeze = std::make_shared(node_box_w, squeeze_box_wh); + auto node_box_h_squeeze = std::make_shared(node_box_h, squeeze_box_wh); + + auto node_box_w_exp = std::make_shared(node_box_w_squeeze); + auto node_box_h_exp = std::make_shared(node_box_h_squeeze); + + auto node_box_w_encode = + std::make_shared(node_box_w_exp, node_anchor_w_reshape); + auto node_box_h_encode = + std::make_shared(node_box_h_exp, node_anchor_h_reshape); + + // confidence + auto node_conf_sigmoid = std::make_shared(node_conf); + + auto node_concat = std::make_shared( + NodeVector{Constant::create(i64, {1}, {1}), + const_num_anchors, + input_height, + input_width, + Constant::create(i64, {1}, {1})}, + 0); + auto node_conf_thresh = std::make_shared( + const_conf_thresh, + node_concat); // {1, num_anchors, input_height, input_width, 1} + + auto node_conf_sub = + std::make_shared(node_conf_sigmoid, node_conf_thresh); + + auto node_conf_clip = std::make_shared( + node_conf_sub, 0.0f, std::numeric_limits::max()); + + auto node_zeros = Constant::create(f32, {1}, {0}); + auto node_conf_clip_bool = + std::make_shared(node_conf_clip, node_zeros); + + auto node_conf_clip_cast = std::make_shared(node_conf_clip_bool, f32); + + auto node_conf_set_zero = + std::make_shared(node_conf_sigmoid, node_conf_clip_cast); + + /* probability */ + auto node_prob_sigmoid = std::make_shared(node_prob); + + auto node_new_shape = std::make_shared( + NodeVector{batch_size, + const_num_anchors, + input_height, + input_width, + Constant::create(i64, {1}, {1})}, + 0); + auto node_conf_new_shape = std::make_shared( + node_conf_set_zero, + node_new_shape, + false); // {batch_size, int(num_anchors), input_height, input_width, 1} + + // broadcast confidence * probability of each category + auto node_score = + std::make_shared(node_prob_sigmoid, node_conf_new_shape); + + // for bbox which has object (greater than threshold) + auto node_conf_bool = + std::make_shared(node_conf_new_shape, node_zeros); + + auto node_box_x_new_shape = + std::make_shared(node_box_x_encode, node_new_shape, false); + auto node_box_y_new_shape = + std::make_shared(node_box_y_encode, node_new_shape, false); + auto node_box_w_new_shape = + std::make_shared(node_box_w_encode, node_new_shape, false); + auto node_box_h_new_shape = + std::make_shared(node_box_h_encode, node_new_shape, false); + auto node_pred_box = + std::make_shared(OutputVector{node_box_x_new_shape, + node_box_y_new_shape, + node_box_w_new_shape, + node_box_h_new_shape}, + 4); + + auto node_conf_cast = std::make_shared(node_conf_bool, f32); + + auto node_pred_box_mul_conf = + std::make_shared(node_pred_box, node_conf_cast); + + auto node_box_shape = std::make_shared( + NodeVector{ + batch_size, node_mul_whc, Constant::create(i64, {1}, {4})}, + 0); + auto node_pred_box_new_shape = std::make_shared( + node_pred_box_mul_conf, + node_box_shape, + false); // {batch_size, int(num_anchors) * input_height * input_width, 4} + + auto pred_box_split_axis = Constant::create(i64, {}, {2}); + auto node_pred_box_split = + std::make_shared(node_pred_box_new_shape, pred_box_split_axis, 4); + + auto node_pred_box_x = node_pred_box_split->output(0); + auto node_pred_box_y = node_pred_box_split->output(1); + auto node_pred_box_w = node_pred_box_split->output(2); + auto node_pred_box_h = node_pred_box_split->output(3); + + /* x,y,w,h -> x1,y1,x2,y2 */ + auto node_number_two = Constant::create(f32, {1}, {2.0f}); + auto node_half_w = std::make_shared(node_pred_box_w, node_number_two); + auto node_half_h = std::make_shared(node_pred_box_h, node_number_two); + + auto node_pred_box_x1 = + std::make_shared(node_pred_box_x, node_half_w); + auto node_pred_box_y1 = + std::make_shared(node_pred_box_y, node_half_h); + + auto node_pred_box_x2 = std::make_shared(node_pred_box_x, node_half_w); + auto node_pred_box_y2 = std::make_shared(node_pred_box_y, node_half_h); + + /* map normalized coords to original image */ + auto indices_height_imgsize = Constant::create(i64, {1}, {0}); + auto indices_width_imgsize = Constant::create(i64, {1}, {1}); + auto const_axis1 = Constant::create(i64, {1}, {1}); + auto node_img_height = std::make_shared( + image_size, indices_height_imgsize, const_axis1); // shape_image_size[0] + auto node_img_width = std::make_shared( + image_size, indices_width_imgsize, const_axis1); // shape_image_size[1] + + auto node_img_width_cast = std::make_shared(node_img_width, f32); + auto node_img_height_cast = std::make_shared(node_img_height, f32); + + auto squeeze_axes2 = Constant::create(i64, {1}, {2}); + auto node_pred_box_x1_reshape = std::make_shared( + node_pred_box_x1, + squeeze_axes2); // shape (N,C,1) -> (N,C) for upcomping multiply. + auto node_pred_box_y1_reshape = + std::make_shared(node_pred_box_y1, squeeze_axes2); + auto node_pred_box_x2_reshape = + std::make_shared(node_pred_box_x2, squeeze_axes2); + auto node_pred_box_y2_reshape = + std::make_shared(node_pred_box_y2, squeeze_axes2); + + auto node_pred_box_x1_squeeze = + std::make_shared(node_pred_box_x1_reshape, node_img_width_cast); + auto node_pred_box_y1_squeeze = + std::make_shared(node_pred_box_y1_reshape, node_img_height_cast); + auto node_pred_box_x2_squeeze = + std::make_shared(node_pred_box_x2_reshape, node_img_width_cast); + auto node_pred_box_y2_squeeze = + std::make_shared(node_pred_box_y2_reshape, node_img_height_cast); + + std::shared_ptr node_pred_box_result; + if (clip_bbox) + { + auto node_number_one = Constant::create(f32, {1}, {1.0}); + auto node_new_img_height = + std::make_shared(node_img_height_cast, node_number_one); + auto node_new_img_width = + std::make_shared(node_img_width_cast, node_number_one); + auto node_pred_box_x2_sub_w = std::make_shared( + node_pred_box_x2_squeeze, node_new_img_width); // x2 - (w-1) + auto node_pred_box_y2_sub_h = std::make_shared( + node_pred_box_y2_squeeze, node_new_img_height); // y2 - (h-1) + + auto max_const = std::numeric_limits::max(); + auto node_pred_box_x1_clip = + std::make_shared(node_pred_box_x1_squeeze, 0.0f, max_const); + auto node_pred_box_y1_clip = + std::make_shared(node_pred_box_y1_squeeze, 0.0f, max_const); + auto node_pred_box_x2_clip = + std::make_shared(node_pred_box_x2_sub_w, 0.0f, max_const); + auto node_pred_box_y2_clip = + std::make_shared(node_pred_box_y2_sub_h, 0.0f, max_const); + + auto node_pred_box_x2_res = std::make_shared( + node_pred_box_x2_squeeze, node_pred_box_x2_clip); + auto node_pred_box_y2_res = std::make_shared( + node_pred_box_y2_squeeze, node_pred_box_y2_clip); + + auto node_pred_box_x1_clip2 = std::make_shared( + node_pred_box_x1_clip, squeeze_axes2); // reshape back to (N,C,1) + auto node_pred_box_y1_clip2 = + std::make_shared(node_pred_box_y1_clip, squeeze_axes2); + auto node_pred_box_x2_res2 = + std::make_shared(node_pred_box_x2_res, squeeze_axes2); + auto node_pred_box_y2_res2 = + std::make_shared(node_pred_box_y2_res, squeeze_axes2); + + node_pred_box_result = + std::make_shared(OutputVector{node_pred_box_x1_clip2, + node_pred_box_y1_clip2, + node_pred_box_x2_res2, + node_pred_box_y2_res2}, + -1); // outputs=node.output('Boxes') + } + else + { + auto node_pred_box_x1_decode = std::make_shared( + node_pred_box_x1_squeeze, squeeze_axes2); // reshape back to (N,C,1) + auto node_pred_box_y1_decode = + std::make_shared(node_pred_box_y1_squeeze, squeeze_axes2); + auto node_pred_box_x2_decode = + std::make_shared(node_pred_box_x2_squeeze, squeeze_axes2); + auto node_pred_box_y2_decode = + std::make_shared(node_pred_box_y2_squeeze, squeeze_axes2); + + node_pred_box_result = + std::make_shared(OutputVector{node_pred_box_x1_decode, + node_pred_box_y1_decode, + node_pred_box_x2_decode, + node_pred_box_y2_decode}, + -1); // outputs=node.output('Boxes') + } + + // + auto node_score_new_shape = std::make_shared( + node_score, score_shape, false); // outputs=node.output('Scores') + + NamedOutputs outputs; + outputs["Boxes"] = {node_pred_box_result}; + outputs["Scores"] = {node_score_new_shape}; + return outputs; + } + + } // namespace op + } // namespace pdpd + } // namespace frontend +} // namespace ngraph diff --git a/ngraph/frontend/paddlepaddle/src/op/yolo_box.hpp b/ngraph/frontend/paddlepaddle/src/op/yolo_box.hpp new file mode 100644 index 00000000000000..f71a98fa60be29 --- /dev/null +++ b/ngraph/frontend/paddlepaddle/src/op/yolo_box.hpp @@ -0,0 +1,20 @@ +// Copyright (C) 2018-2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#pragma once +#include "node_context.hpp" + +namespace ngraph +{ + namespace frontend + { + namespace pdpd + { + namespace op + { + NamedOutputs yolo_box(const NodeContext& node); + } + } // namespace pdpd + } // namespace frontend +} // namespace ngraph \ No newline at end of file diff --git a/ngraph/frontend/paddlepaddle/src/op_table.cpp b/ngraph/frontend/paddlepaddle/src/op_table.cpp index 916737fc0c2ede..a93b21bb32ada5 100644 --- a/ngraph/frontend/paddlepaddle/src/op_table.cpp +++ b/ngraph/frontend/paddlepaddle/src/op_table.cpp @@ -9,9 +9,23 @@ #include "op/concat.hpp" #include "op/conv2d.hpp" #include "op/elementwise_ops.hpp" +#include "op/mul.hpp" +#include "op/pad3d.hpp" +#include "op/pool2d.hpp" +#include "op/pow.hpp" +#include "op/range.hpp" #include "op/relu.hpp" +#include "op/relu6.hpp" +#include "op/reshape2.hpp" #include "op/scale.hpp" +#include "op/shape.hpp" +#include "op/sigmoid.hpp" +#include "op/slice.hpp" +#include "op/softmax.hpp" #include "op/split.hpp" +#include "op/squeeze.hpp" +#include "op/unsqueeze.hpp" +#include "op/yolo_box.hpp" #include "op_table.hpp" @@ -37,9 +51,24 @@ namespace ngraph {"elementwise_mul", op::elementwise_mul}, {"elementwise_pow", op::elementwise_pow}, {"elementwise_sub", op::elementwise_sub}, + {"max_pool2d_with_index", op::pool2d}, + {"mul", op::mul}, + {"pad3d", op::pad3d}, + {"pow", op::pow}, + {"pool2d", op::pool2d}, + {"range", op::range}, {"relu", op::relu}, + {"relu6", op::relu6}, + {"reshape2", op::reshape2}, {"scale", op::scale}, - {"split", op::split}}; + {"shape", op::shape}, + {"slice", op::slice}, + {"softmax", op::softmax}, + {"sigmoid", op::sigmoid}, + {"split", op::split}, + {"squeeze2", op::squeeze}, + {"unsqueeze2", op::unsqueeze}, + {"yolo_box", op::yolo_box}}; }; } // namespace pdpd From a15b5dd2c94be3ef3eb46528d7d9b2c135c394ee Mon Sep 17 00:00:00 2001 From: Mang Guo Date: Wed, 14 Jul 2021 04:26:04 -0400 Subject: [PATCH 02/15] Add adaptive pool2d op conversion (#1) --- .../frontend/paddlepaddle/src/op/pool2d.cpp | 50 +++++++------------ 1 file changed, 17 insertions(+), 33 deletions(-) diff --git a/ngraph/frontend/paddlepaddle/src/op/pool2d.cpp b/ngraph/frontend/paddlepaddle/src/op/pool2d.cpp index 334378a41fd785..7b8d98351da8b9 100644 --- a/ngraph/frontend/paddlepaddle/src/op/pool2d.cpp +++ b/ngraph/frontend/paddlepaddle/src/op/pool2d.cpp @@ -6,6 +6,7 @@ #include "pool2d.hpp" #include +#include namespace ngraph { @@ -138,56 +139,39 @@ namespace ngraph { PDPD_ASSERT(input_shape[2].is_static() && input_shape[3].is_static(), "pool2d: spatial dim must be static when using adaptive pool"); - uint64_t pool_size_Height, pool_size_Width; - uint64_t input_h = input_shape[input_rank - 2].get_length(); - uint64_t input_w = input_shape[input_rank - 1].get_length(); + auto pool_size = std::vector(2, 0); if (kernel_shape.size() == 1) { // Not tested: implemented according to spec, but can't generate real // model to test - pool_size_Height = pool_size_Width = kernel_shape[0]; + pool_size[0] = pool_size[1] = kernel_shape[0]; } else { - pool_size_Height = kernel_shape[0]; - pool_size_Width = kernel_shape[1]; + pool_size[0] = kernel_shape[0]; + pool_size[1] = kernel_shape[1]; } - uint64_t stride_h = int64_t(input_h / pool_size_Height); - uint64_t stride_w = int64_t(input_w / pool_size_Width); - uint64_t kernel_h = input_h - (pool_size_Height - 1) * stride_h; - uint64_t kernel_w = input_w - (pool_size_Width - 1) * stride_w; - - PDPD_ASSERT(stride_h >= 1 && stride_w >= 1, - "Pool2d stride must be greater than 1"); + const Output output_shape = ngraph::opset6::Constant::create( + ngraph::element::i64, {pool_size.size()}, pool_size); if (pooling_type == "max") { - return node.default_single_output_mapping( - {std::make_shared( - data, - ngraph::Strides{stride_h, stride_w}, - pad_begin, - pad_end, - ngraph::Shape{kernel_h, kernel_w}, - rounding_type, - auto_pad)}, - {"Out"}); + std::vector> pool_outputs; + pool_outputs = std::make_shared( + data, output_shape, ngraph::element::i64) + ->outputs(); + NamedOutputs outputs; + outputs["Out"] = {pool_outputs[0]}; + outputs["Mask"] = {pool_outputs[1]}; + return outputs; } else { - bool exclude_pad = node.get_attribute("exclusive", false); return node.default_single_output_mapping( - {std::make_shared( - data, - ngraph::Strides{stride_h, stride_w}, - pad_begin, - pad_end, - ngraph::Shape{kernel_h, kernel_w}, - exclude_pad, - rounding_type, - auto_pad)}, + {std::make_shared(data, + output_shape)}, {"Out"}); } } From f8b6b4c2731e64087d1aee78b73c9277f8cb64d6 Mon Sep 17 00:00:00 2001 From: Luo Cheng Date: Mon, 19 Jul 2021 18:10:59 +0800 Subject: [PATCH 03/15] param support tensor (#2) --- ngraph/frontend/paddlepaddle/src/op/slice.cpp | 81 ++++++++++++------- .../paddlepaddle/src/op/unsqueeze.cpp | 21 +++-- 2 files changed, 67 insertions(+), 35 deletions(-) diff --git a/ngraph/frontend/paddlepaddle/src/op/slice.cpp b/ngraph/frontend/paddlepaddle/src/op/slice.cpp index 5dd14d0179b212..e1c1af63512780 100644 --- a/ngraph/frontend/paddlepaddle/src/op/slice.cpp +++ b/ngraph/frontend/paddlepaddle/src/op/slice.cpp @@ -18,43 +18,64 @@ namespace ngraph { auto data = node.get_ng_input("Input"); auto axes = node.get_attribute>("axes"); - // TODO: support tensor type #55266 - auto starts = node.get_attribute>("starts"); - // TODO: support tensor type #55266 - auto ends = node.get_attribute>("ends"); - auto data_rank = data.get_partial_shape().rank(); - size_t shape_size = data_rank.get_length(); - std::vector fixedStarts(shape_size, 0); - std::vector fixedEnds(shape_size, INT_MAX); + Output start_idx_node, end_idx_node; + if (node.has_ng_input("StartsTensor")) + { + start_idx_node = node.get_ng_input("StartsTensor"); + } + else if (node.has_ng_input("StartsTensorList")) + { + auto inputs = node.get_ng_inputs("StartsTensorList"); + start_idx_node = std::make_shared(inputs, 0); + } + else + { + auto starts = node.get_attribute>("starts"); + start_idx_node = + opset6::Constant::create(element::i32, {starts.size()}, starts); + } - int n = 0; - for (auto i : axes) + if (node.has_ng_input("EndsTensor")) { - PDPD_OP_VALIDATION_CHECK(node, - i < (int32_t)shape_size, - "slice: axes must be less than the X rank."); - fixedStarts[i] = starts[n]; - fixedEnds[i] = ends[n]; - n++; + end_idx_node = node.get_ng_input("EndsTensor"); } + else if (node.has_ng_input("EndsTensorList")) + { + auto inputs = node.get_ng_inputs("EndsTensorList"); + end_idx_node = std::make_shared(inputs, 0); + } + else + { + auto ends = node.get_attribute>("ends"); + end_idx_node = opset6::Constant::create(element::i32, {ends.size()}, ends); + } + + // the shape of input, such as [1, 1, 3, 3] + auto shape_node = std::make_shared(data, element::Type_t::i32); + // the input dim, such as [4] + auto shape_shape_node = + std::make_shared(shape_node, element::i32); + auto const_0_node = opset6::Constant::create(element::i32, {}, {0}); + auto const_max_node = opset6::Constant::create(element::i32, {}, {INT_MAX}); + // array [0:max) + auto start_node = + std::make_shared(const_0_node, shape_shape_node); + auto end_node = + std::make_shared(const_max_node, shape_shape_node); + auto axes_node = opset6::Constant::create(element::i32, {axes.size(), 1}, axes); + auto fixed_start_node = std::make_shared( + start_node, axes_node, start_idx_node); + auto fixed_end_node = std::make_shared( + end_node, axes_node, end_idx_node); - auto startsNode = ngraph::opset6::Constant::create( - ngraph::element::i32, {shape_size}, fixedStarts); - auto endsNode = ngraph::opset6::Constant::create( - ngraph::element::i32, {shape_size}, fixedEnds); - auto stridesNode = ngraph::opset6::Constant::create( - ngraph::element::i32, {shape_size}, std::vector(shape_size, 1)); return node.default_single_output_mapping( - {std::make_shared( - data, - startsNode, - endsNode, - stridesNode, - std::vector(shape_size, 0), - std::vector(shape_size, 0))}, + {std::make_shared(data, + fixed_start_node, + fixed_end_node, + std::vector{}, + std::vector{})}, {"Out"}); } - } // namespace op } // namespace pdpd } // namespace frontend diff --git a/ngraph/frontend/paddlepaddle/src/op/unsqueeze.cpp b/ngraph/frontend/paddlepaddle/src/op/unsqueeze.cpp index dc9b7e0bb9c2e9..fad5e489faea99 100644 --- a/ngraph/frontend/paddlepaddle/src/op/unsqueeze.cpp +++ b/ngraph/frontend/paddlepaddle/src/op/unsqueeze.cpp @@ -15,15 +15,26 @@ namespace ngraph { NamedOutputs unsqueeze(const NodeContext& node) { - // TODO to support data type other than int32_t #55168 auto data = node.get_ng_input("X"); - auto axes = node.get_attribute>("axes"); - auto axesNode = - ngraph::opset6::Constant::create(ngraph::element::i32, {axes.size()}, axes); + Output axesNode; + if (node.has_ng_input("AxesTensor")) + { + axesNode = node.get_ng_input("AxesTensor"); + } + else if (node.has_ng_input("AxesTensorList")) + { + auto inputs = node.get_ng_inputs("AxesTensorList"); + axesNode = std::make_shared(inputs, 0); + } + else + { + auto axes = node.get_attribute>("axes"); + axesNode = ngraph::opset6::Constant::create( + ngraph::element::i32, {axes.size()}, axes); + } return node.default_single_output_mapping( {std::make_shared(data, axesNode)}, {"Out"}); } - } // namespace op } // namespace pdpd } // namespace frontend From 5f870a04864a46239d93ff72261091661eb78cfc Mon Sep 17 00:00:00 2001 From: zhangYiIntel Date: Wed, 21 Jul 2021 13:42:07 +0800 Subject: [PATCH 04/15] add missing sync_batch_norm --- ngraph/frontend/paddlepaddle/src/op_table.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/ngraph/frontend/paddlepaddle/src/op_table.cpp b/ngraph/frontend/paddlepaddle/src/op_table.cpp index a93b21bb32ada5..6efa5fda60c392 100644 --- a/ngraph/frontend/paddlepaddle/src/op_table.cpp +++ b/ngraph/frontend/paddlepaddle/src/op_table.cpp @@ -67,6 +67,7 @@ namespace ngraph {"sigmoid", op::sigmoid}, {"split", op::split}, {"squeeze2", op::squeeze}, + {"sync_batch_norm", op::batch_norm}, {"unsqueeze2", op::unsqueeze}, {"yolo_box", op::yolo_box}}; }; From abf1e86a78667f5836c2de1a756e913d89ccfa28 Mon Sep 17 00:00:00 2001 From: zhangYiIntel Date: Fri, 23 Jul 2021 13:56:10 +0800 Subject: [PATCH 05/15] Update pow.cpp --- ngraph/frontend/paddlepaddle/src/op/pow.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/ngraph/frontend/paddlepaddle/src/op/pow.cpp b/ngraph/frontend/paddlepaddle/src/op/pow.cpp index be1cd3e9d7dcd3..cb7111fe98f855 100644 --- a/ngraph/frontend/paddlepaddle/src/op/pow.cpp +++ b/ngraph/frontend/paddlepaddle/src/op/pow.cpp @@ -2,7 +2,6 @@ // SPDX-License-Identifier: Apache-2.0 // #include "pow.hpp" -#include #include #include @@ -27,8 +26,8 @@ namespace ngraph } else { - factor_node = builder::make_constant( - dtype, Shape{1}, node.get_attribute("factor")); + factor_node = ngraph::opset6::Constant::create( + dtype, Shape{1}, {node.get_attribute("factor")}); } return node.default_single_output_mapping( @@ -38,4 +37,4 @@ namespace ngraph } // namespace op } // namespace pdpd } // namespace frontend -} // namespace ngraph \ No newline at end of file +} // namespace ngraph From cd1bfc09c5ddcb1325284316319c4ae79915d549 Mon Sep 17 00:00:00 2001 From: Luo Cheng Date: Tue, 27 Jul 2021 10:29:11 +0800 Subject: [PATCH 06/15] deal empty axis (#5) * deal empty axis * apply review comments --- ngraph/frontend/paddlepaddle/src/op/squeeze.cpp | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/ngraph/frontend/paddlepaddle/src/op/squeeze.cpp b/ngraph/frontend/paddlepaddle/src/op/squeeze.cpp index 485b713eea6155..488702708f204e 100644 --- a/ngraph/frontend/paddlepaddle/src/op/squeeze.cpp +++ b/ngraph/frontend/paddlepaddle/src/op/squeeze.cpp @@ -22,12 +22,19 @@ namespace ngraph axes = node.get_attribute>("axes"); } - auto axesNode = - ngraph::opset6::Constant::create(ngraph::element::i32, {axes.size()}, axes); - return node.default_single_output_mapping( - {std::make_shared(data, axesNode)}, {"Out"}); + std::shared_ptr out; + if (!axes.empty()) + { + auto axesNode = + ngraph::opset6::Constant::create(ngraph::element::i32, {axes.size()}, axes); + out = std::make_shared(data, axesNode); + } + else + { + out = std::make_shared(data); + } + return node.default_single_output_mapping(out, {"Out"}); } - } // namespace op } // namespace pdpd } // namespace frontend From bdb3f4f269454341cf1b75cb755a7c050ae5ba3e Mon Sep 17 00:00:00 2001 From: Luo Cheng Date: Tue, 27 Jul 2021 11:14:46 +0800 Subject: [PATCH 07/15] fix code style * fix code style --- ngraph/frontend/paddlepaddle/src/op/squeeze.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ngraph/frontend/paddlepaddle/src/op/squeeze.cpp b/ngraph/frontend/paddlepaddle/src/op/squeeze.cpp index 488702708f204e..98e2d081a17fc0 100644 --- a/ngraph/frontend/paddlepaddle/src/op/squeeze.cpp +++ b/ngraph/frontend/paddlepaddle/src/op/squeeze.cpp @@ -25,8 +25,8 @@ namespace ngraph std::shared_ptr out; if (!axes.empty()) { - auto axesNode = - ngraph::opset6::Constant::create(ngraph::element::i32, {axes.size()}, axes); + auto axesNode = ngraph::opset6::Constant::create( + ngraph::element::i32, {axes.size()}, axes); out = std::make_shared(data, axesNode); } else From ce5c1eae4acf3d951a36e972d7e582c089fd655f Mon Sep 17 00:00:00 2001 From: Zhang Yi3 Date: Tue, 27 Jul 2021 11:56:45 +0800 Subject: [PATCH 08/15] change shape to i32 --- ngraph/frontend/paddlepaddle/src/op/range.cpp | 1 - ngraph/frontend/paddlepaddle/src/op/shape.cpp | 3 ++- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/ngraph/frontend/paddlepaddle/src/op/range.cpp b/ngraph/frontend/paddlepaddle/src/op/range.cpp index 972374b617170c..c5c6e05de1b7d1 100644 --- a/ngraph/frontend/paddlepaddle/src/op/range.cpp +++ b/ngraph/frontend/paddlepaddle/src/op/range.cpp @@ -28,7 +28,6 @@ namespace ngraph auto stop_scalar = std::make_shared(stop, axis); auto step_scalar = std::make_shared(step, axis); - // TODO to support other data types other than FP32 #55267 return node.default_single_output_mapping( {std::make_shared( start_scalar, stop_scalar, step_scalar, type)}, diff --git a/ngraph/frontend/paddlepaddle/src/op/shape.cpp b/ngraph/frontend/paddlepaddle/src/op/shape.cpp index d10eac182dfcad..35831e63b481c8 100644 --- a/ngraph/frontend/paddlepaddle/src/op/shape.cpp +++ b/ngraph/frontend/paddlepaddle/src/op/shape.cpp @@ -16,8 +16,9 @@ namespace ngraph NamedOutputs shape(const NodeContext& node) { auto data = node.get_ng_input("Input"); + auto shape_node = std::make_shared(data); return node.default_single_output_mapping( - {std::make_shared(data)}, {"Out"}); + {std::make_shared(shape_node, element::i32)}, {"Out"}); } } // namespace op From a463fd11ea2d27ef52c6442d4eca2e17d7ad4733 Mon Sep 17 00:00:00 2001 From: Zhang Yi3 Date: Tue, 27 Jul 2021 12:00:03 +0800 Subject: [PATCH 09/15] fix code in shape --- ngraph/frontend/paddlepaddle/src/op/shape.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ngraph/frontend/paddlepaddle/src/op/shape.cpp b/ngraph/frontend/paddlepaddle/src/op/shape.cpp index 35831e63b481c8..fd1abbe42d54d5 100644 --- a/ngraph/frontend/paddlepaddle/src/op/shape.cpp +++ b/ngraph/frontend/paddlepaddle/src/op/shape.cpp @@ -18,7 +18,8 @@ namespace ngraph auto data = node.get_ng_input("Input"); auto shape_node = std::make_shared(data); return node.default_single_output_mapping( - {std::make_shared(shape_node, element::i32)}, {"Out"}); + {std::make_shared(shape_node, element::i32)}, + {"Out"}); } } // namespace op From dc7c5358c1d3729f74f61e46dab10bd096f372ef Mon Sep 17 00:00:00 2001 From: Zhang Yi3 Date: Fri, 30 Jul 2021 21:00:29 +0800 Subject: [PATCH 10/15] fix code style --- ngraph/frontend/paddlepaddle/src/op_table.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/ngraph/frontend/paddlepaddle/src/op_table.cpp b/ngraph/frontend/paddlepaddle/src/op_table.cpp index 891cf4feb5f8f1..e17a377c77d38b 100644 --- a/ngraph/frontend/paddlepaddle/src/op_table.cpp +++ b/ngraph/frontend/paddlepaddle/src/op_table.cpp @@ -138,7 +138,6 @@ namespace ngraph {"transpose2", op::transpose2}, {"unsqueeze2", op::unsqueeze}, {"yolo_box", op::yolo_box}}; - }; } // namespace pdpd From 1a3be8e13490f16e40f1adb5a0dcf34a1f81b9f5 Mon Sep 17 00:00:00 2001 From: Zhang Yi3 Date: Fri, 30 Jul 2021 21:29:00 +0800 Subject: [PATCH 11/15] fix paddle code style --- ngraph/frontend/paddlepaddle/src/op/mul.cpp | 2 +- ngraph/frontend/paddlepaddle/src/op/pad3d.cpp | 2 +- ngraph/frontend/paddlepaddle/src/op/pool2d.cpp | 2 +- ngraph/frontend/paddlepaddle/src/op/pow.cpp | 2 +- ngraph/frontend/paddlepaddle/src/op/range.cpp | 2 +- ngraph/frontend/paddlepaddle/src/op/relu6.cpp | 2 +- ngraph/frontend/paddlepaddle/src/op/reshape2.cpp | 2 +- ngraph/frontend/paddlepaddle/src/op/shape.cpp | 2 +- ngraph/frontend/paddlepaddle/src/op/sigmoid.cpp | 2 +- ngraph/frontend/paddlepaddle/src/op/slice.cpp | 2 +- ngraph/frontend/paddlepaddle/src/op/softmax.cpp | 2 +- ngraph/frontend/paddlepaddle/src/op/squeeze.cpp | 2 +- ngraph/frontend/paddlepaddle/src/op/unsqueeze.cpp | 2 +- 13 files changed, 13 insertions(+), 13 deletions(-) diff --git a/ngraph/frontend/paddlepaddle/src/op/mul.cpp b/ngraph/frontend/paddlepaddle/src/op/mul.cpp index 813cd27bf40d53..130fd148084091 100644 --- a/ngraph/frontend/paddlepaddle/src/op/mul.cpp +++ b/ngraph/frontend/paddlepaddle/src/op/mul.cpp @@ -2,8 +2,8 @@ // SPDX-License-Identifier: Apache-2.0 // -#include #include +#include namespace ngraph { diff --git a/ngraph/frontend/paddlepaddle/src/op/pad3d.cpp b/ngraph/frontend/paddlepaddle/src/op/pad3d.cpp index f56957e6388c40..8b508d0426391d 100644 --- a/ngraph/frontend/paddlepaddle/src/op/pad3d.cpp +++ b/ngraph/frontend/paddlepaddle/src/op/pad3d.cpp @@ -2,8 +2,8 @@ // SPDX-License-Identifier: Apache-2.0 // -#include #include +#include namespace ngraph { diff --git a/ngraph/frontend/paddlepaddle/src/op/pool2d.cpp b/ngraph/frontend/paddlepaddle/src/op/pool2d.cpp index 1148bd00f8b588..2c901550941beb 100644 --- a/ngraph/frontend/paddlepaddle/src/op/pool2d.cpp +++ b/ngraph/frontend/paddlepaddle/src/op/pool2d.cpp @@ -4,9 +4,9 @@ // //***************************************************************************** -#include #include #include +#include namespace ngraph { diff --git a/ngraph/frontend/paddlepaddle/src/op/pow.cpp b/ngraph/frontend/paddlepaddle/src/op/pow.cpp index 6201717e06b31f..2bab9dc6043f14 100644 --- a/ngraph/frontend/paddlepaddle/src/op/pow.cpp +++ b/ngraph/frontend/paddlepaddle/src/op/pow.cpp @@ -1,8 +1,8 @@ // Copyright (C) 2018-2021 Intel Corporation // SPDX-License-Identifier: Apache-2.0 // -#include #include +#include #include namespace ngraph diff --git a/ngraph/frontend/paddlepaddle/src/op/range.cpp b/ngraph/frontend/paddlepaddle/src/op/range.cpp index 2745452bbbb547..e03acb0d2af373 100644 --- a/ngraph/frontend/paddlepaddle/src/op/range.cpp +++ b/ngraph/frontend/paddlepaddle/src/op/range.cpp @@ -2,8 +2,8 @@ // SPDX-License-Identifier: Apache-2.0 // -#include #include +#include namespace ngraph { diff --git a/ngraph/frontend/paddlepaddle/src/op/relu6.cpp b/ngraph/frontend/paddlepaddle/src/op/relu6.cpp index 50dbd78394fa1f..8c17505494b6f6 100644 --- a/ngraph/frontend/paddlepaddle/src/op/relu6.cpp +++ b/ngraph/frontend/paddlepaddle/src/op/relu6.cpp @@ -2,8 +2,8 @@ // SPDX-License-Identifier: Apache-2.0 // -#include #include +#include #include namespace ngraph diff --git a/ngraph/frontend/paddlepaddle/src/op/reshape2.cpp b/ngraph/frontend/paddlepaddle/src/op/reshape2.cpp index 546e0e0b5ae4f4..b39943cece2b68 100644 --- a/ngraph/frontend/paddlepaddle/src/op/reshape2.cpp +++ b/ngraph/frontend/paddlepaddle/src/op/reshape2.cpp @@ -2,8 +2,8 @@ // SPDX-License-Identifier: Apache-2.0 // -#include #include +#include #include namespace ngraph diff --git a/ngraph/frontend/paddlepaddle/src/op/shape.cpp b/ngraph/frontend/paddlepaddle/src/op/shape.cpp index 978e9e420203e9..836b98d33e2ecd 100644 --- a/ngraph/frontend/paddlepaddle/src/op/shape.cpp +++ b/ngraph/frontend/paddlepaddle/src/op/shape.cpp @@ -2,8 +2,8 @@ // SPDX-License-Identifier: Apache-2.0 // -#include #include +#include namespace ngraph { diff --git a/ngraph/frontend/paddlepaddle/src/op/sigmoid.cpp b/ngraph/frontend/paddlepaddle/src/op/sigmoid.cpp index e4d5126d4f056e..2f9b8edb48edcc 100644 --- a/ngraph/frontend/paddlepaddle/src/op/sigmoid.cpp +++ b/ngraph/frontend/paddlepaddle/src/op/sigmoid.cpp @@ -2,8 +2,8 @@ // SPDX-License-Identifier: Apache-2.0 // -#include #include +#include namespace ngraph { diff --git a/ngraph/frontend/paddlepaddle/src/op/slice.cpp b/ngraph/frontend/paddlepaddle/src/op/slice.cpp index 3da3f4b764be7e..1a245f7384985e 100644 --- a/ngraph/frontend/paddlepaddle/src/op/slice.cpp +++ b/ngraph/frontend/paddlepaddle/src/op/slice.cpp @@ -2,9 +2,9 @@ // SPDX-License-Identifier: Apache-2.0 // -#include #include #include +#include namespace ngraph { diff --git a/ngraph/frontend/paddlepaddle/src/op/softmax.cpp b/ngraph/frontend/paddlepaddle/src/op/softmax.cpp index ce819085128f10..5be1423a19703e 100644 --- a/ngraph/frontend/paddlepaddle/src/op/softmax.cpp +++ b/ngraph/frontend/paddlepaddle/src/op/softmax.cpp @@ -2,8 +2,8 @@ // SPDX-License-Identifier: Apache-2.0 // -#include #include +#include namespace ngraph { diff --git a/ngraph/frontend/paddlepaddle/src/op/squeeze.cpp b/ngraph/frontend/paddlepaddle/src/op/squeeze.cpp index 4b40dc10193ab6..a79910af0098e1 100644 --- a/ngraph/frontend/paddlepaddle/src/op/squeeze.cpp +++ b/ngraph/frontend/paddlepaddle/src/op/squeeze.cpp @@ -2,8 +2,8 @@ // SPDX-License-Identifier: Apache-2.0 // -#include #include +#include namespace ngraph { diff --git a/ngraph/frontend/paddlepaddle/src/op/unsqueeze.cpp b/ngraph/frontend/paddlepaddle/src/op/unsqueeze.cpp index 87b35b8d3128dc..265fe80025b4b9 100644 --- a/ngraph/frontend/paddlepaddle/src/op/unsqueeze.cpp +++ b/ngraph/frontend/paddlepaddle/src/op/unsqueeze.cpp @@ -2,8 +2,8 @@ // SPDX-License-Identifier: Apache-2.0 // -#include #include +#include namespace ngraph { From eaa184fc92c403930c6b6c2b0fb17199e1a48857 Mon Sep 17 00:00:00 2001 From: Zhang Yi3 Date: Fri, 30 Jul 2021 23:44:43 +0800 Subject: [PATCH 12/15] remove redandent ops --- ngraph/frontend/paddlepaddle/src/op_table.cpp | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/ngraph/frontend/paddlepaddle/src/op_table.cpp b/ngraph/frontend/paddlepaddle/src/op_table.cpp index e17a377c77d38b..d64bc42c81c085 100644 --- a/ngraph/frontend/paddlepaddle/src/op_table.cpp +++ b/ngraph/frontend/paddlepaddle/src/op_table.cpp @@ -22,7 +22,6 @@ namespace ngraph OP_CONVERTER(concat); OP_CONVERTER(conv2d); OP_CONVERTER(conv2d_transpose); - OP_CONVERTER(deformable_conv); OP_CONVERTER(dropout); OP_CONVERTER(elementwise_add); OP_CONVERTER(elementwise_div); @@ -43,9 +42,7 @@ namespace ngraph OP_CONVERTER(log); OP_CONVERTER(logical_not); OP_CONVERTER(matmul); - OP_CONVERTER(matrix_nms); OP_CONVERTER(mul); - OP_CONVERTER(multiclass_nms); OP_CONVERTER(nearest_interp_v2); OP_CONVERTER(pad3d); OP_CONVERTER(pow); @@ -89,8 +86,6 @@ namespace ngraph {"concat", op::concat}, {"conv2d", op::conv2d}, {"conv2d_transpose", op::conv2d_transpose}, - {"deformable_conv", op::deformable_conv}, - {"deformable_conv_v1", op::deformable_conv}, {"depthwise_conv2d", op::conv2d}, {"depthwise_conv2d_transpose", op::conv2d_transpose}, {"dropout", op::dropout}, @@ -113,10 +108,8 @@ namespace ngraph {"log", op::log}, {"logical_not", op::logical_not}, {"matmul", op::matmul}, - {"matrix_nms", op::matrix_nms}, {"max_pool2d_with_index", op::pool2d}, {"mul", op::mul}, - {"multiclass_nms3", op::multiclass_nms}, {"nearest_interp_v2", op::nearest_interp_v2}, {"nearest_interp", op::nearest_interp_v2}, {"pad3d", op::pad3d}, @@ -142,4 +135,4 @@ namespace ngraph } // namespace pdpd } // namespace frontend -} // namespace ngraph +} // namespace ngraph \ No newline at end of file From 620d274df2dac852c85552fb4d7a438f8e964fae Mon Sep 17 00:00:00 2001 From: Zhang Yi3 Date: Mon, 2 Aug 2021 10:38:46 +0800 Subject: [PATCH 13/15] fix maxAdativePool --- ngraph/frontend/paddlepaddle/src/op/pool2d.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ngraph/frontend/paddlepaddle/src/op/pool2d.cpp b/ngraph/frontend/paddlepaddle/src/op/pool2d.cpp index 2c901550941beb..65a7c3576b962d 100644 --- a/ngraph/frontend/paddlepaddle/src/op/pool2d.cpp +++ b/ngraph/frontend/paddlepaddle/src/op/pool2d.cpp @@ -160,7 +160,7 @@ namespace ngraph { std::vector> pool_outputs; pool_outputs = std::make_shared( - data, output_shape, ngraph::element::i64) + data, output_shape, ngraph::element::i32) ->outputs(); NamedOutputs outputs; outputs["Out"] = {pool_outputs[0]}; From 363c655192ec561693dc9e2707b2242e8d1f78ee Mon Sep 17 00:00:00 2001 From: Zhang Yi3 Date: Tue, 3 Aug 2021 14:16:30 +0800 Subject: [PATCH 14/15] fix expand_v2 --- ngraph/frontend/paddlepaddle/src/op/expand_v2.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/ngraph/frontend/paddlepaddle/src/op/expand_v2.cpp b/ngraph/frontend/paddlepaddle/src/op/expand_v2.cpp index bc2bc1d233e59d..1b5ec1dd3d69f2 100644 --- a/ngraph/frontend/paddlepaddle/src/op/expand_v2.cpp +++ b/ngraph/frontend/paddlepaddle/src/op/expand_v2.cpp @@ -61,7 +61,11 @@ namespace ngraph fixed_shape_node, input_shape_node, false); return node.default_single_output_mapping( - {std::make_shared(x, repeated_node)}, {"Out"}); + {std::make_shared( + x, + std::make_shared(repeated_node, + element::i64))}, + {"Out"}); } } // namespace op From 437cc07f7c68b6038d7aa03557d2ae2251f7a739 Mon Sep 17 00:00:00 2001 From: Zhang Yi3 Date: Tue, 3 Aug 2021 20:36:46 +0800 Subject: [PATCH 15/15] remove redandent code --- ngraph/frontend/paddlepaddle/src/op/range.cpp | 3 --- ngraph/frontend/paddlepaddle/src/op/shape.cpp | 6 ++---- ngraph/frontend/paddlepaddle/src/op/yolo_box.cpp | 7 ------- 3 files changed, 2 insertions(+), 14 deletions(-) diff --git a/ngraph/frontend/paddlepaddle/src/op/range.cpp b/ngraph/frontend/paddlepaddle/src/op/range.cpp index e03acb0d2af373..c445c650942493 100644 --- a/ngraph/frontend/paddlepaddle/src/op/range.cpp +++ b/ngraph/frontend/paddlepaddle/src/op/range.cpp @@ -19,9 +19,6 @@ namespace ngraph auto stop = node.get_ng_input("End"); auto step = node.get_ng_input("Step"); auto type = node.get_out_port_type("Out"); - PDPD_ASSERT(type == element::i64 || type == element::i32 || - type == element::f32, - "Only supports int32, int64, float32"); const auto axis = ngraph::opset6::Constant::create(element::i64, Shape{}, {0}); auto start_scalar = std::make_shared(start, axis); diff --git a/ngraph/frontend/paddlepaddle/src/op/shape.cpp b/ngraph/frontend/paddlepaddle/src/op/shape.cpp index 836b98d33e2ecd..fe1c2bd72d390e 100644 --- a/ngraph/frontend/paddlepaddle/src/op/shape.cpp +++ b/ngraph/frontend/paddlepaddle/src/op/shape.cpp @@ -16,10 +16,8 @@ namespace ngraph NamedOutputs shape(const NodeContext& node) { auto data = node.get_ng_input("Input"); - auto shape_node = std::make_shared(data); - return node.default_single_output_mapping( - {std::make_shared(shape_node, element::i32)}, - {"Out"}); + auto shape_node = std::make_shared(data, element::i32); + return node.default_single_output_mapping({shape_node}, {"Out"}); } } // namespace op diff --git a/ngraph/frontend/paddlepaddle/src/op/yolo_box.cpp b/ngraph/frontend/paddlepaddle/src/op/yolo_box.cpp index b0b8b30f4b05d7..ccbc3f61341b7a 100644 --- a/ngraph/frontend/paddlepaddle/src/op/yolo_box.cpp +++ b/ngraph/frontend/paddlepaddle/src/op/yolo_box.cpp @@ -73,13 +73,6 @@ namespace ngraph auto clip_bbox = node_context.get_attribute("clip_bbox"); - std::cout << "num_anchors: " << num_anchors << " scale_x_y: " << scale_x_y - << std::endl; - std::cout << "downsample_ratio: " << downsample_ratio - << " conf_thresh: " << conf_thresh << std::endl; - std::cout << "class_num: " << class_num << " image_size: " << image_size - << std::endl; - // main X // node_x_shape {batch_size, num_anchors, 5 + class_num, input_height, // input_width}