Skip to content

Commit

Permalink
add op supoort for:
Browse files Browse the repository at this point in the history
conv2d,
rnn_lstm,
interpolate,
fill_constant
assign_value
uniform_random
  • Loading branch information
zhangYiIntel committed Apr 22, 2021
1 parent 8bcf5f1 commit 4fb44e6
Show file tree
Hide file tree
Showing 19 changed files with 749 additions and 24 deletions.
2 changes: 1 addition & 1 deletion ngraph/frontend/paddlepaddle/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ if(COMMAND ie_add_vs_version_file)
FILEDESCRIPTION "FrontEnd to load and convert PaddlePaddle file format")
endif()

target_link_libraries(paddlepaddle_frontend PRIVATE ${Protobuf_LIBRARIES} PUBLIC ngraph)
target_link_libraries(paddlepaddle_frontend PRIVATE ${Protobuf_LIBRARIES} PUBLIC ngraph PRIVATE ngraph::builder)

# TODO: Consider to remove the following block (inherited from onnx_import just in case).
if (CMAKE_CXX_COMPILER_ID MATCHES "^(Apple)?Clang$")
Expand Down
20 changes: 20 additions & 0 deletions ngraph/frontend/paddlepaddle/src/decoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,5 +157,25 @@ std::vector<std::string> DecoderPDPDProto::get_output_names() const {
return output_names;
}

std::vector<int64_t> DecoderPDPDProto::get_longs(const std::string& name, const std::vector<int64_t>& def) const
{
std::cout << "Running get_longs" << std::endl;
std::vector<proto::OpDesc_Attr> attrs;
for (const auto &attr : op.attrs()) {
if (attr.name() == name)
attrs.push_back(attr);
}
if (attrs.empty()) {
return def;
} else if (attrs.size() > 1) {
// TODO: raise exception here
return def;
} else {
std::vector<int64_t> res;
std::copy(attrs[0].longs().begin(), attrs[0].longs().end(), std::back_inserter(res));
return res;
}
}

}
}
1 change: 1 addition & 0 deletions ngraph/frontend/paddlepaddle/src/decoder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class DecoderPDPDProto
float get_float(const std::string& name, float def = 0.) const;
std::string get_str(const std::string& name, const std::string& def = "") const;
bool get_bool (const std::string& name, bool def = false) const;
std::vector<int64_t> get_longs(const std::string& name, const std::vector<int64_t>& def = {}) const;

// TODO: Further populate get_XXX methods on demand
ngraph::element::Type get_dtype(const std::string& name, ngraph::element::Type def) const;
Expand Down
4 changes: 4 additions & 0 deletions ngraph/frontend/paddlepaddle/src/node_context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ template <>
inline ngraph::element::Type NodeContext::get_attribute (const std::string& name, const ngraph::element::Type& def) const
{ return node.get_dtype(name, def); }

template <>
inline std::vector<int64_t> NodeContext::get_attribute (const std::string& name, const std::vector<int64_t>& def) const
{ return node.get_longs(name, def); }


inline NamedOutputs NodeContext::default_single_output_mapping(const std::shared_ptr<Node>& ngraph_node,
const std::vector<OutPortName>& required_pdpd_out_names) const
Expand Down
33 changes: 33 additions & 0 deletions ngraph/frontend/paddlepaddle/src/op/assign_value.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//*****************************************************************************
// Copyright 2017-2021 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//*****************************************************************************

#include <ngraph/opsets/opset6.hpp>
#include "assign_value.hpp"
namespace ngraph {
namespace frontend {
namespace pdpd {
namespace op {

NamedOutputs assign_value (const NodeContext& node) {
std::vector<float> values = node.get_attribute<std::vector<float>>("fp32_values");
std::vector<int32_t> shape = node.get_attribute<std::vector<int32_t>>("shape");
return node.default_single_output_mapping({opset6::Constant::create(element::f32, Shape{shape.begin(), shape.end()}, values)}, {"Out"});
}

}
}
}
}
31 changes: 31 additions & 0 deletions ngraph/frontend/paddlepaddle/src/op/assign_value.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//*****************************************************************************
// Copyright 2017-2021 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//*****************************************************************************

#pragma once

#include "node_context.hpp"

namespace ngraph {
namespace frontend {
namespace pdpd {
namespace op {

NamedOutputs assign_value (const NodeContext &node);

}
}
}
}
116 changes: 104 additions & 12 deletions ngraph/frontend/paddlepaddle/src/op/conv2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,119 @@
//*****************************************************************************

#include <ngraph/opsets/opset6.hpp>
#include <ngraph/builder/reshape.hpp>
#include "conv2d.hpp"

namespace ngraph {
namespace frontend {
namespace pdpd {
namespace op {

ngraph::op::PadType get_auto_pad(const NodeContext& node)
{
// Default value means use explicitly provided padding values.
ngraph::op::PadType pad_type{ngraph::op::PadType::NOTSET};
auto padding_algorithm = node.get_attribute<std::string>("padding_algorithm");
static std::unordered_multimap<std::string, ngraph::op::PadType>
auto_pad_values{
{"VALID", ngraph::op::PadType::VALID},
{"SAME", ngraph::op::PadType::SAME_UPPER},
{"NOTSET", ngraph::op::PadType::NOTSET},
};

const auto pad_val_it = auto_pad_values.find(padding_algorithm);

if(pad_val_it == auto_pad_values.end()) {
pad_type = ngraph::op::PadType::NOTSET;
} else {
pad_type = pad_val_it->second;
}



return pad_type;
}

std::pair<CoordinateDiff, CoordinateDiff> get_pads(const NodeContext& node,
const size_t kernel_rank)
{
CoordinateDiff pads(kernel_rank, 0);

auto pads_int32 = node.get_attribute<std::vector<int32_t>>("paddings");
pads = CoordinateDiff{std::begin(pads_int32), std::end(pads_int32)};
CoordinateDiff pads_begin;
CoordinateDiff pads_end;


if (pads.size() == kernel_rank * 2)
{
for(size_t i = 0; i < pads.size(); i++)
{
if(i & 0x01)
{
pads_end.push_back(pads[i]);
} else {
pads_begin.push_back(pads[i]);
}
}
return {pads_begin, pads_end};
}
else
{
// No paddings provided or only one side values provided, which means same
// padding at both begin and end of axis.
return {pads, pads};
}
}

std::pair<CoordinateDiff, CoordinateDiff> get_pads(const NodeContext& node)
{
const auto data_rank = node.get_ng_input("Input").get_partial_shape().rank();

const auto data_spatial_dims = data_rank.get_length() - 2;

return get_pads(node, data_spatial_dims);
}

NamedOutputs conv2d (const NodeContext& node) {
auto data = node.get_ng_input("Input");
auto filter = node.get_ng_input("Filter");
// TODO: resolve padding according to spec
auto strides = node.get_attribute<std::vector<int32_t>>("strides");
auto paddings = node.get_attribute<std::vector<int32_t>>("paddings");
auto dilations = node.get_attribute<std::vector<int32_t>>("dilations");
return node.default_single_output_mapping({std::make_shared<ngraph::opset6::Convolution>(
data,
filter,
ngraph::Strides(strides.begin(), strides.end()),
ngraph::CoordinateDiff(paddings.begin(), paddings.end()),
ngraph::CoordinateDiff(paddings.begin(), paddings.end()),
ngraph::Strides(dilations.begin(), dilations.end()))}, {"Output"});
auto filters = node.get_ng_input("Filter");

const auto strides = node.get_attribute<std::vector<int32_t>>("strides");
const auto dilations = node.get_attribute<std::vector<int32_t>>("dilations");
const auto auto_pad_type = get_auto_pad(node);
const auto paddings = get_pads(node);
const auto pads_begin = paddings.first;
const auto pads_end = paddings.second;
const auto groups = node.get_attribute<int32_t>("groups");

if (groups > 1) {
auto filters_shape = filters.get_shape();
filters_shape.at(0) = filters_shape.at(0) / groups;
filters_shape.insert(filters_shape.begin(), groups);

const auto reshaped_filters =
ngraph::builder::opset1::reshape(filters, filters_shape);
return node.default_single_output_mapping({std::make_shared<opset6::GroupConvolution>(
data,
reshaped_filters,
ngraph::Strides(strides.begin(), strides.end()),
pads_begin,
pads_end,
ngraph::Strides(dilations.begin(), dilations.end()),
auto_pad_type)}, {"Output"});
}
else
{
return node.default_single_output_mapping({std::make_shared<opset6::Convolution>(
data,
filters,
ngraph::Strides(strides.begin(), strides.end()),
pads_begin,
pads_end,
ngraph::Strides(dilations.begin(), dilations.end()),
auto_pad_type)}, {"Output"});
}
}

}}}}
32 changes: 32 additions & 0 deletions ngraph/frontend/paddlepaddle/src/op/fill_constant.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//*****************************************************************************
// Copyright 2017-2021 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//*****************************************************************************

#include <ngraph/opsets/opset6.hpp>
#include "fill_constant.hpp"


namespace ngraph {
namespace frontend {
namespace pdpd {
namespace op {

NamedOutputs fill_constant (const NodeContext& node) {
auto value = node.get_attribute<float>("value");
auto shape = node.get_attribute<std::vector<int64_t>>("shape");
return node.default_single_output_mapping({std::make_shared<ngraph::opset6::Constant>(ngraph::element::f32, Shape(shape.begin(), shape.end()), value)}, {"Out"});
}

}}}}
31 changes: 31 additions & 0 deletions ngraph/frontend/paddlepaddle/src/op/fill_constant.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//*****************************************************************************
// Copyright 2017-2021 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//*****************************************************************************

#pragma once

#include "node_context.hpp"

namespace ngraph {
namespace frontend {
namespace pdpd {
namespace op {

NamedOutputs fill_constant(const NodeContext &node);

}
}
}
}
Loading

0 comments on commit 4fb44e6

Please sign in to comment.