Skip to content

Commit

Permalink
update code with review (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
luo-cheng2021 authored May 12, 2021
1 parent 1e62148 commit 8e8b374
Show file tree
Hide file tree
Showing 61 changed files with 146 additions and 269 deletions.
33 changes: 13 additions & 20 deletions ngraph/frontend/paddlepaddle/src/op/expand_v2.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,6 @@
//*****************************************************************************
// Copyright 2017-2021 Intel Corporation
// Copyright (C) 2018-2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
// 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 "expand_v2.hpp"
Expand All @@ -25,14 +13,19 @@ namespace op {

NamedOutputs expand_v2 (const NodeContext& node) {
auto x = node.get_ng_input("X");
std::vector<int32_t> shapeExpected;
if (node.has_attribute<std::vector<int32_t>>("shape")) {
shapeExpected = node.get_attribute<std::vector<int32_t>>("shape");
Output<Node> shapeExpectedNode;
if (node.has_ng_input("Shape")) {
shapeExpectedNode = node.get_ng_input("Shape");
} else {
throw std::runtime_error("expand: has no shape attribute");
std::vector<int32_t> shapeExpected;
if (node.has_attribute<std::vector<int32_t>>("shape")) {
shapeExpected = node.get_attribute<std::vector<int32_t>>("shape");
} else {
throw std::runtime_error("expand: has no shape attribute");
}

shapeExpectedNode = ngraph::opset6::Constant::create(ngraph::element::i32, {shapeExpected.size()}, shapeExpected);
}

auto shapeExpectedNode = ngraph::opset6::Constant::create(ngraph::element::i32, {shapeExpected.size()}, shapeExpected);
return node.default_single_output_mapping({std::make_shared<ngraph::opset6::Broadcast>(x, shapeExpectedNode)}, {"Out"});
}

Expand Down
16 changes: 2 additions & 14 deletions ngraph/frontend/paddlepaddle/src/op/expand_v2.hpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,6 @@
//*****************************************************************************
// Copyright 2017-2021 Intel Corporation
// Copyright (C) 2018-2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
// 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"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,6 @@
//*****************************************************************************
// Copyright 2017-2021 Intel Corporation
// Copyright (C) 2018-2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
// 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_batch_size_like.hpp"
Expand Down Expand Up @@ -66,23 +54,47 @@ static std::shared_ptr<Node> set_val(int32_t idx, std::shared_ptr<Node> val_node
return std::make_shared<ngraph::opset6::Concat>(nodes, 0);
}

static Output<Node> get_seed_node(const NodeContext& node) {
auto dtype = node.get_attribute<element::Type>("dtype");
Output<Node> val_node;
auto str_value = node.get_attribute<std::string>("str_value");
switch (dtype)
{
case element::i32:
val_node = ngraph::opset6::Constant::create(dtype, { 1 }, { std::stoi(str_value) });
break;
case element::i64:
val_node = ngraph::opset6::Constant::create(dtype, { 1 }, { std::stoll(str_value) });
break;
case element::f32:
val_node = ngraph::opset6::Constant::create(dtype, { 1 }, { std::stof(str_value) });
break;
case element::f64:
val_node = ngraph::opset6::Constant::create(dtype, { 1 }, { std::stod(str_value) });
break;
default:
throw std::runtime_error("fill_constant_batch_size_like: dtype value is invalid");
}

return val_node;
}

NamedOutputs fill_constant_batch_size_like (const NodeContext& node) {
auto input_dim_idx = node.get_attribute<int32_t>("input_dim_idx");
auto output_dim_idx = node.get_attribute<int32_t>("output_dim_idx");
auto value = node.get_attribute<float>("value");
auto shapes = node.get_attribute<std::vector<int32_t> >("shape");
auto input = node.get_ng_input("Input");
auto input_shape = std::make_shared<ngraph::opset6::ShapeOf>(input, element::i32);
// because Gather&Scatter does not support evaluate then
// 1, cat the array:
// shape[0, shape[output_dim_idx]) + input_shape[input_dim_idx] + shape[shape[output_dim_idx + 1], -1]
auto input_val_node = get_val(input_dim_idx, input_shape);
auto shapes_node = ngraph::opset6::Constant::create(ngraph::element::i32, { shapes.size() }, shapes);
auto shape_node = set_val(output_dim_idx, input_val_node, shapes_node);

// 2, use the shape broadcast the node
auto val_const = ngraph::opset6::Constant::create(ngraph::element::f32, { 1 }, { value });
auto val_node = get_seed_node(node);
return node.default_single_output_mapping(
{std::make_shared<ngraph::opset6::Broadcast>(val_const, shape_node)},
{std::make_shared<ngraph::opset6::Broadcast>(val_node, shape_node)},
{"Out"});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,6 @@
//*****************************************************************************
// Copyright 2017-2021 Intel Corporation
// Copyright (C) 2018-2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
// 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"
Expand Down
17 changes: 3 additions & 14 deletions ngraph/frontend/paddlepaddle/src/op/greater_equal.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,6 @@
//*****************************************************************************
// Copyright 2017-2021 Intel Corporation
// Copyright (C) 2018-2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
// 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 "greater_equal.hpp"
Expand All @@ -26,6 +14,7 @@ namespace op {
NamedOutputs greater_equal (const NodeContext& node) {
auto x = node.get_ng_input("X");
auto y = node.get_ng_input("Y");
// TODO: support the data type of 'Out' equal to the type of input
return node.default_single_output_mapping({std::make_shared<ngraph::opset6::GreaterEqual>(x, y)}, {"Out"});
}

Expand Down
16 changes: 2 additions & 14 deletions ngraph/frontend/paddlepaddle/src/op/greater_equal.hpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,6 @@
//*****************************************************************************
// Copyright 2017-2021 Intel Corporation
// Copyright (C) 2018-2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
// 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"
Expand Down
17 changes: 2 additions & 15 deletions ngraph/frontend/paddlepaddle/src/op/hard_sigmoid.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,6 @@
//*****************************************************************************
// Copyright 2017-2021 Intel Corporation
// Copyright (C) 2018-2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
// 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 "hard_sigmoid.hpp"
Expand All @@ -28,7 +16,6 @@ NamedOutputs hard_sigmoid (const NodeContext& node) {
auto slope = 0.2f;
if (node.has_attribute<float>("slope")) {
slope = node.get_attribute<float>("slope");
PDPD_ASSERT(slope >= 0, "hard_sigmoid: slope must greater than 0!");
}
auto offset = 0.5f;
if (node.has_attribute<float>("offset")) {
Expand Down
16 changes: 2 additions & 14 deletions ngraph/frontend/paddlepaddle/src/op/hard_sigmoid.hpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,6 @@
//*****************************************************************************
// Copyright 2017-2021 Intel Corporation
// Copyright (C) 2018-2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
// 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"
Expand Down
16 changes: 2 additions & 14 deletions ngraph/frontend/paddlepaddle/src/op/hard_swish.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,6 @@
//*****************************************************************************
// Copyright 2017-2021 Intel Corporation
// Copyright (C) 2018-2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
// 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 "hard_swish.hpp"
Expand Down
16 changes: 2 additions & 14 deletions ngraph/frontend/paddlepaddle/src/op/hard_swish.hpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,6 @@
//*****************************************************************************
// Copyright 2017-2021 Intel Corporation
// Copyright (C) 2018-2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
// 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"
Expand Down
16 changes: 2 additions & 14 deletions ngraph/frontend/paddlepaddle/src/op/log.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,6 @@
//*****************************************************************************
// Copyright 2017-2021 Intel Corporation
// Copyright (C) 2018-2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
// 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 "log.hpp"
Expand Down
16 changes: 2 additions & 14 deletions ngraph/frontend/paddlepaddle/src/op/log.hpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,6 @@
//*****************************************************************************
// Copyright 2017-2021 Intel Corporation
// Copyright (C) 2018-2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
// 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"
Expand Down
28 changes: 10 additions & 18 deletions ngraph/frontend/paddlepaddle/src/op/pow.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,6 @@
//*****************************************************************************
// Copyright 2017-2021 Intel Corporation
// Copyright (C) 2018-2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
// 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 "pow.hpp"
Expand All @@ -26,11 +14,15 @@ namespace op {
NamedOutputs pow (const NodeContext& node) {
auto x = node.get_ng_input("X");
Output<Node> factorNode;
auto factor = 1.0f;
if (node.has_attribute<float>("factor")) {
factor = node.get_attribute<float>("factor");
if (node.has_ng_input("FactorTensor")) {
factorNode = node.get_ng_input("FactorTensor");
} else {
auto factor = 1.0f;
if (node.has_attribute<float>("factor")) {
factor = node.get_attribute<float>("factor");
}
factorNode = ngraph::opset6::Constant::create(ngraph::element::f32, {}, {factor});
}
factorNode = ngraph::opset6::Constant::create(ngraph::element::f32, {}, {factor});
return node.default_single_output_mapping({std::make_shared<ngraph::opset6::Power>(x, factorNode)}, {"Out"});
}

Expand Down
Loading

0 comments on commit 8e8b374

Please sign in to comment.