Skip to content

Commit

Permalink
Merge branch 'yi3/update_to_latest' into new_API
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangYiIntel authored Apr 25, 2021
2 parents f39985a + 311d305 commit c16bcba
Show file tree
Hide file tree
Showing 11 changed files with 340 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//*****************************************************************************
// 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_batch_size_like.hpp"
#include <paddlepaddle_frontend/utility.hpp>

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

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 parial_shape = input.get_partial_shape();
PDPD_ASSERT(parial_shape.is_static(), "fill_constant_batch_size_like: must use static shape.");
auto static_shape = parial_shape.get_shape();
PDPD_ASSERT(input_dim_idx < (int32_t)static_shape.size(), "fill_constant_batch_size_like: input_dim_idx should not exceed input dims.");
PDPD_ASSERT(output_dim_idx < (int32_t)shapes.size(), "fill_constant_batch_size_like: output_dim_idx should not exceed shapes dims.");
shapes[output_dim_idx] = static_shape[input_dim_idx];
return node.default_single_output_mapping(
{std::make_shared<ngraph::opset6::Constant>(ngraph::element::f32, Shape(shapes.begin(), shapes.end()), value)},
{"Out"});
}

}}}}
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 fill_constant_batch_size_like (const NodeContext& node);

}}}}
32 changes: 32 additions & 0 deletions ngraph/frontend/paddlepaddle/src/op/greater_equal.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 "greater_equal.hpp"
#include <paddlepaddle_frontend/utility.hpp>

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

NamedOutputs greater_equal (const NodeContext& node) {
auto x = node.get_ng_input("X");
auto y = node.get_ng_input("Y");
return node.default_single_output_mapping({std::make_shared<ngraph::opset6::GreaterEqual>(x, y)}, {"Out"});
}

}}}}
27 changes: 27 additions & 0 deletions ngraph/frontend/paddlepaddle/src/op/greater_equal.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 greater_equal (const NodeContext& node);

}}}}
31 changes: 31 additions & 0 deletions ngraph/frontend/paddlepaddle/src/op/log.cpp
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.
//*****************************************************************************

#include <ngraph/opsets/opset6.hpp>
#include "log.hpp"
#include <paddlepaddle_frontend/utility.hpp>

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

NamedOutputs log (const NodeContext& node) {
auto x = node.get_ng_input("X");
return node.default_single_output_mapping({std::make_shared<ngraph::opset6::Log>(x)}, {"Out"});
}

}}}}
27 changes: 27 additions & 0 deletions ngraph/frontend/paddlepaddle/src/op/log.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 log (const NodeContext& node);

}}}}
9 changes: 7 additions & 2 deletions ngraph/frontend/paddlepaddle/src/op_table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@
#include "op/slice.hpp"
#include "op/hard_swish.hpp"
#include "op/clip.hpp"

#include "op/greater_equal.hpp"
#include "op/log.hpp"
#include "op/fill_constant_batch_size_like.hpp"
#include "op_table.hpp"


Expand Down Expand Up @@ -99,7 +101,10 @@ std::map<std::string, CreatorFunction> get_supported_ops() {
{"unsqueeze2", op::unsqueeze},
{"slice", op::slice},
{"hard_swish", op::hard_swish},
{"clip", op::clip}
{"clip", op::clip},
{"greater_equal", op::greater_equal},
{"log", op::log},
{"fill_constant_batch_size_like", op::fill_constant_batch_size_like}
};
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#
# fill_const paddle model generator
#
import numpy as np
from save_model import saveModel
import paddle as pdpd

data_type = 'float32'

def fill_constant(name : str, shape : list, dtype, value):
pdpd.enable_static()

with pdpd.static.program_guard(pdpd.static.Program(), pdpd.static.Program()):
out = pdpd.fluid.layers.fill_constant(shape=shape, value=value, dtype=dtype, name='fill_constant')

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(
fetch_list=[out])

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

return outs[0]

def main():
fill_constant("fill_constant", [2, 3, 4], data_type, 0.03)

if __name__ == "__main__":
main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#
# fill_constant_batch_size_like paddle model generator
#
import numpy as np
from save_model import saveModel
import paddle as pdpd

data_type = 'float32'

def fill_constant_batch_size_like(name : str, x, shape, dtype, value, input_dim_idx=0, output_dim_idx=0):
pdpd.enable_static()

with pdpd.static.program_guard(pdpd.static.Program(), pdpd.static.Program()):
like = pdpd.static.data(name='x', shape=x.shape, dtype = data_type)
out = pdpd.fluid.layers.fill_constant_batch_size_like(input=like, shape=shape, \
value=value, dtype=dtype, \
output_dim_idx=output_dim_idx, input_dim_idx=input_dim_idx)

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():
x = np.random.rand(4, 3, 2).astype(data_type)
fill_constant_batch_size_like("fill_constant_batch_size_like", \
x, [1, -1, 3], data_type, 0.03, 2, 1)

if __name__ == "__main__":
main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#
# greater_equal paddle model generator
#
import numpy as np
from save_model import saveModel
import paddle as pdpd

data_type = 'float32'

def greater_equal(name : str, x, y):
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)
node_y = pdpd.static.data(name='y', shape=y.shape, dtype = data_type)
out = pdpd.fluid.layers.greater_equal(x=node_x, y=node_y, name='greater_equal')
# save model does not support boolean type
out = pdpd.cast(out, data_type)

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, 'y' : y},
fetch_list=[out])

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

return outs[0]

def main():
x = np.array([0, 1, 2, 3]).astype(data_type)
y = np.array([1, 0, 2, 4]).astype(data_type)

greater_equal("greater_equal", x, y)

if __name__ == "__main__":
main()
36 changes: 36 additions & 0 deletions ngraph/test/files/paddlepaddle/gen_scripts/generate_log.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#
# log paddle model generator
#
import numpy as np
from save_model import saveModel
import paddle as pdpd

data_type = 'float32'

def log(name : str, x):
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.fluid.layers.log(node_x, name = 'log')

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():
x = np.array([0, 1, 2, -10]).astype(data_type)

log("log", x)

if __name__ == "__main__":
main()

0 comments on commit c16bcba

Please sign in to comment.