Skip to content

Commit

Permalink
PaddlePaddle frontend - OP Conversion Part1 (openvinotoolkit#6501)
Browse files Browse the repository at this point in the history
* enable pdpd fuzzy test

* add 6 op conversion

* apply review suggestions

* correct cnpy location in test CMakeLists
  • Loading branch information
zhangYiIntel authored Jul 12, 2021
1 parent 434fcec commit 8fb1182
Show file tree
Hide file tree
Showing 21 changed files with 893 additions and 2 deletions.
57 changes: 57 additions & 0 deletions ngraph/frontend/paddlepaddle/src/op/argmax.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright (C) 2018-2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

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

namespace ngraph
{
namespace frontend
{
namespace pdpd
{
namespace op
{
NamedOutputs argmax(const NodeContext& node)
{
auto data = node.get_ng_input("X");
bool flatten = node.get_attribute<bool>("flatten");
const element::Type& index_element_type = element::i64;
const Output<ngraph::Node> k =
ngraph::opset6::Constant::create(ngraph::element::i64, {}, {1});

if (!flatten)
{
auto axis = node.get_attribute<int64_t>("axis");
const auto axis_to_remove =
ngraph::opset6::Constant::create(element::u64, Shape{}, {axis});
auto node_topk = std::make_shared<ngraph::opset6::TopK>(
data, k, axis, "max", "index", index_element_type);
const auto reshaped_indices = std::make_shared<ngraph::opset6::Squeeze>(
node_topk->output(1), axis_to_remove);
return node.default_single_output_mapping(
{std::make_shared<ngraph::opset6::Convert>(reshaped_indices,
element::i64)},
{"Out"});
}
else
{
int64_t axis = 0;
const Output<ngraph::Node> reshape_flatten =
ngraph::opset6::Constant::create(ngraph::element::i64, {1}, {-1});
auto node_reshape =
std::make_shared<ngraph::opset6::Reshape>(data, reshape_flatten, true);
auto node_topk = std::make_shared<ngraph::opset6::TopK>(
node_reshape, k, axis, "max", "index", index_element_type);
return node.default_single_output_mapping(
{std::make_shared<ngraph::opset6::Convert>(node_topk->output(1),
element::i64)},
{"Out"});
}
}

} // namespace op
} // namespace pdpd
} // namespace frontend
} // namespace ngraph
20 changes: 20 additions & 0 deletions ngraph/frontend/paddlepaddle/src/op/argmax.hpp
Original file line number Diff line number Diff line change
@@ -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 argmax(const NodeContext& node);
}
} // namespace pdpd
} // namespace frontend
} // namespace ngraph
66 changes: 66 additions & 0 deletions ngraph/frontend/paddlepaddle/src/op/assign_value.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright (C) 2018-2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

#include "assign_value.hpp"
#include <ngraph/opsets/opset6.hpp>
namespace ngraph
{
namespace frontend
{
namespace pdpd
{
namespace op
{
NamedOutputs assign_value(const NodeContext& node)
{
std::vector<int32_t> shape = node.get_attribute<std::vector<int32_t>>("shape");
auto dtype = node.get_attribute<ngraph::element::Type>("dtype");
std::shared_ptr<Node> const_node;

switch (dtype)
{
case element::i32:
{
auto values = node.get_attribute<std::vector<int32_t>>("int32_values");
const_node = {opset6::Constant::create(
dtype, Shape{shape.begin(), shape.end()}, values)};
break;
}
case element::f32:
{
std::vector<float> values =
node.get_attribute<std::vector<float>>("fp32_values");
const_node = {opset6::Constant::create(
dtype, Shape{shape.begin(), shape.end()}, values)};
break;
}
case element::boolean:
{
auto values = node.get_attribute<std::vector<int32_t>>("bool_values");
const_node = {opset6::Constant::create(
dtype, Shape{shape.begin(), shape.end()}, values)};
break;
}
case element::i64:
{
auto values = node.get_attribute<std::vector<int64_t>>("int64_values");
const_node = {opset6::Constant::create(
dtype, Shape{shape.begin(), shape.end()}, values)};
break;
}
default:
{
PDPD_OP_VALIDATION_CHECK(
node, false, "assign_value only supports int32, int64, float32, bool");
break;
}
}

return node.default_single_output_mapping({const_node}, {"Out"});
}

} // namespace op
} // namespace pdpd
} // namespace frontend
} // namespace ngraph
21 changes: 21 additions & 0 deletions ngraph/frontend/paddlepaddle/src/op/assign_value.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// 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 assign_value(const NodeContext& node);
}
} // namespace pdpd
} // namespace frontend
} // namespace ngraph
64 changes: 64 additions & 0 deletions ngraph/frontend/paddlepaddle/src/op/batch_norm.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright (C) 2018-2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

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

namespace ngraph
{
namespace frontend
{
namespace pdpd
{
namespace op
{
NamedOutputs batch_norm(const NodeContext& node)
{
auto data = node.get_ng_input("X");
auto gamma = node.get_ng_input("Scale");
auto beta = node.get_ng_input("Bias");
auto mean = node.get_ng_input("Mean");
auto variance = node.get_ng_input("Variance");
auto data_layout = node.get_attribute<std::string>("data_layout");

PDPD_ASSERT((data_layout == "NCHW" || data_layout == "NHWC"),
"Not supported input data layout!");
if (data_layout == "NCHW")
{
return node.default_single_output_mapping(
{std::make_shared<ngraph::opset6::BatchNormInference>(
data,
gamma,
beta,
mean,
variance,
node.get_attribute<float>("epsilon"))},
{"Y"});
}
else
{
auto input_order = ngraph::opset6::Constant::create(
ngraph::element::i64, {4}, {0, 3, 1, 2});
auto data_nchw =
std::make_shared<ngraph::opset6::Transpose>(data, input_order);
auto node_batch_norm = std::make_shared<ngraph::opset6::BatchNormInference>(
data_nchw,
gamma,
beta,
mean,
variance,
node.get_attribute<float>("epsilon"));
auto output_order = ngraph::opset6::Constant::create(
ngraph::element::i64, {4}, {0, 2, 3, 1});
return node.default_single_output_mapping(
{std::make_shared<ngraph::opset6::Transpose>(node_batch_norm,
output_order)},
{"Y"});
}
}

} // namespace op
} // namespace pdpd
} // namespace frontend
} // namespace ngraph
20 changes: 20 additions & 0 deletions ngraph/frontend/paddlepaddle/src/op/batch_norm.hpp
Original file line number Diff line number Diff line change
@@ -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 batch_norm(const NodeContext& node);
}
} // namespace pdpd
} // namespace frontend
} // namespace ngraph
28 changes: 28 additions & 0 deletions ngraph/frontend/paddlepaddle/src/op/cast.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (C) 2018-2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

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

namespace ngraph
{
namespace frontend
{
namespace pdpd
{
namespace op
{
NamedOutputs cast(const NodeContext& node)
{
auto data = node.get_ng_input("X");
auto out_dtype = node.get_attribute<ngraph::element::Type>("out_dtype");

return node.default_single_output_mapping(
{std::make_shared<ngraph::opset6::Convert>(data, out_dtype)}, {"Out"});
}

} // namespace op
} // namespace pdpd
} // namespace frontend
} // namespace ngraph
20 changes: 20 additions & 0 deletions ngraph/frontend/paddlepaddle/src/op/cast.hpp
Original file line number Diff line number Diff line change
@@ -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 cast(const NodeContext& node);
}
} // namespace pdpd
} // namespace frontend
} // namespace ngraph
31 changes: 31 additions & 0 deletions ngraph/frontend/paddlepaddle/src/op/clip.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (C) 2018-2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

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

namespace ngraph
{
namespace frontend
{
namespace pdpd
{
namespace op
{
NamedOutputs clip(const NodeContext& node)
{
auto data = node.get_ng_input("X");
auto min = node.get_attribute<float>("min");
auto max = node.get_attribute<float>("max");
PDPD_OP_VALIDATION_CHECK(
node, max >= min, "clip: max value must greater than min value!");

return node.default_single_output_mapping(
{std::make_shared<ngraph::opset6::Clamp>(data, min, max)}, {"Out"});
}

} // namespace op
} // namespace pdpd
} // namespace frontend
} // namespace ngraph
20 changes: 20 additions & 0 deletions ngraph/frontend/paddlepaddle/src/op/clip.hpp
Original file line number Diff line number Diff line change
@@ -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 clip(const NodeContext& node);
}
} // namespace pdpd
} // namespace frontend
} // namespace ngraph
27 changes: 27 additions & 0 deletions ngraph/frontend/paddlepaddle/src/op/concat.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright (C) 2018-2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

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

namespace ngraph
{
namespace frontend
{
namespace pdpd
{
namespace op
{
NamedOutputs concat(const NodeContext& node)
{
auto data = node.get_ng_inputs("X");
auto axis = node.get_attribute<int>("axis");
return node.default_single_output_mapping(
{std::make_shared<ngraph::opset6::Concat>(data, axis)}, {"Out"});
}

} // namespace op
} // namespace pdpd
} // namespace frontend
} // namespace ngraph
Loading

0 comments on commit 8fb1182

Please sign in to comment.