Skip to content

Commit

Permalink
add expand_v2 support (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
luo-cheng2021 authored May 12, 2021
1 parent 0b9e8ec commit 1e62148
Show file tree
Hide file tree
Showing 10 changed files with 106 additions and 2 deletions.
39 changes: 39 additions & 0 deletions ngraph/frontend/paddlepaddle/src/op/expand_v2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//*****************************************************************************
// 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 "expand_v2.hpp"
#include <paddlepaddle_frontend/utility.hpp>

namespace ngraph {
namespace frontend {
namespace pdpd {
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");
} else {
throw std::runtime_error("expand: has no shape attribute");
}

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"});
}

}}}}
27 changes: 27 additions & 0 deletions ngraph/frontend/paddlepaddle/src/op/expand_v2.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//*****************************************************************************
// 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 expand_v2 (const NodeContext& node);

}}}}
5 changes: 3 additions & 2 deletions ngraph/frontend/paddlepaddle/src/op_table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
#include "op/logical_not.hpp"
#include "op/range.hpp"
#include "op_table.hpp"

#include "op/expand_v2.hpp"

namespace ngraph {
namespace frontend {
Expand Down Expand Up @@ -123,7 +123,8 @@ std::map<std::string, CreatorFunction> get_supported_ops() {
{"conv2d_transpose", op::conv2d_transpose},
{"equal", op::equal},
{"logical_not", op::logical_not},
{"range", op::range}
{"range", op::range},
{"expand_v2", op::expand_v2}
};
};

Expand Down
36 changes: 36 additions & 0 deletions ngraph/test/files/paddlepaddle/gen_scripts/generate_expand_v2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#
# expand_v2 paddle model generator
#
import numpy as np
from save_model import saveModel
import paddle as pdpd

data_type = 'float32'

def expand_v2(name:str, x, shape:list):
pdpd.enable_static()

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

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

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

saveModel(name, exe, feedkeys=['x'], fetchlist=[out], inputs=[x], outputs=[outs[0]])

return outs[0]

def main():
data = np.random.rand(1, 1, 6).astype(data_type)

expand_v2("expand_v2", data, [2, 3, 6])

if __name__ == "__main__":
main()
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
1 change: 1 addition & 0 deletions ngraph/test/files/paddlepaddle/models/models.csv
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,4 @@ argmax1,
equal,
logical_not,
range,
expand_v2,

0 comments on commit 1e62148

Please sign in to comment.