Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【PaddlePaddle Hackathon 4】add paddle set_value op #15888

Merged
merged 22 commits into from
Jun 5, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions src/frontends/paddle/src/op/set_value.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright (C) 2018-2023 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

#include "default_opset.hpp"
#include "openvino/frontend/paddle/node_context.hpp"

#define MAX_VALUE(T) std::numeric_limits<T>::max()

namespace ov {
namespace frontend {
namespace paddle {
namespace op {

std::shared_ptr<default_opset::Constant> get_max_value_by_dtype(ov::element::Type dtype) {
if (dtype == element::f32)
return default_opset::Constant::create(dtype, {}, {MAX_VALUE(float)});
else
return default_opset::Constant::create(dtype, {}, {MAX_VALUE(int)});
};

NamedOutputs set_value(const NodeContext& node) {
const auto input_node = node.get_input("Input");
auto value_node = node.get_input("ValueTensor");
const auto input_shape = input_node.get_partial_shape().get_shape();
meiyang-intel marked this conversation as resolved.
Show resolved Hide resolved
const auto dims = static_cast<int64_t>(input_node.get_partial_shape().rank().get_length());
const auto input_value_dim = value_node.get_partial_shape().get_shape().size();
const auto dtype = input_node.get_element_type();

PADDLE_OP_CHECK(node, (!node.has_input("StartsTensorList")), "Slice must be interger");
PADDLE_OP_CHECK(node, (!node.has_input("StepsTensorList")), "Slice must be interger");
PADDLE_OP_CHECK(node, (!node.has_input("EndsTensorList")), "Slice must be interger");

const auto axes = node.get_attribute<std::vector<int64_t>>("axes");
const auto steps = node.get_attribute<std::vector<int64_t>>("steps");
const auto starts = node.get_attribute<std::vector<int64_t>>("starts");
const auto ends = node.get_attribute<std::vector<int64_t>>("ends");

for (size_t i = 0; i < steps.size(); i++)
PADDLE_OP_CHECK(node, (steps[i] == 1), "Elements of steps must be 1");

std::vector<int64_t> padding_starts(dims), padding_ends(dims);
Shape value_shape(input_shape);

for (size_t i = 0; i < starts.size(); i++) {
int64_t s = starts[i], e = ends[i], axis = axes[i], dim = input_shape[axis];
s += s < 0 ? dim : 0;
e += e < 0 ? dim : 0;
padding_starts[axis] = s;
padding_ends[axis] = dim - e;
value_shape[axis] -= (s + padding_ends[axis]);
}

if (input_value_dim == 1) {
const auto target_shape = default_opset::Constant::create(element::i64, {value_shape.size()}, value_shape);
value_node = std::make_shared<default_opset::Broadcast>(value_node, target_shape);
}

const auto maximum_value = get_max_value_by_dtype(dtype);

const auto p_start = default_opset::Constant::create(element::i64, {value_shape.size()}, padding_starts);
const auto p_end = default_opset::Constant::create(element::i64, {value_shape.size()}, padding_ends);

const auto padded_value = std::make_shared<default_opset::Pad>(value_node,
p_start,
p_end,
maximum_value,
ngraph::op::PadMode::CONSTANT);

const auto value_mask = std::make_shared<default_opset::Equal>(padded_value, maximum_value);

return node.default_single_output_mapping(
{std::make_shared<default_opset::Select>(value_mask, input_node, padded_value)},
{"Out"});
};

} // namespace op
} // namespace paddle
} // namespace frontend
} // namespace ov
2 changes: 2 additions & 0 deletions src/frontends/paddle/src/op_table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ OP_CONVERTER(rnn);
OP_CONVERTER(roi_align);
OP_CONVERTER(scale);
OP_CONVERTER(select_input);
OP_CONVERTER(set_value);
OP_CONVERTER(shape);
OP_CONVERTER(slice);
OP_CONVERTER(softmax);
Expand Down Expand Up @@ -201,6 +202,7 @@ std::map<std::string, CreatorFunction> get_supported_ops() {
{"roi_align", op::roi_align},
{"scale", op::scale},
{"select_input", op::select_input},
{"set_value", op::set_value},
{"shape", op::shape},
{"slice", op::slice},
{"softmax", op::softmax},
Expand Down
3 changes: 3 additions & 0 deletions src/frontends/paddle/tests/op_fuzzy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,9 @@ static const std::vector<std::string> models{
std::string("scale_bias_before_int64"),
std::string("scale_tensor_bias_after"),
std::string("scale_tensor_bias_before"),
std::string("set_value1"),
std::string("set_value2"),
std::string("set_value3"),
std::string("shape"),
std::string("sigmoid"),
std::string("slice"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# Copyright (C) 2018-2023 Intel Corporation
# SPDX-License-Identifier: Apache-2.0

import sys

#
# set_value paddle model generator
#
import numpy as np
import paddle
from save_model import saveModel


def paddle_set_value(name: str, x, value, callback, dtype):

paddle.enable_static()

with paddle.static.program_guard(paddle.static.Program(), paddle.static.Program()):
node_x = paddle.static.data(name="x", shape=x.shape, dtype=dtype)
value_shape = (0,) if isinstance(value, (int, float)) else value.shape
node_v = paddle.static.data(name="v", shape=value_shape, dtype=dtype)
cpu = paddle.static.cpu_places(1)
exe = paddle.static.Executor(cpu[0])
# startup program will call initializer to initialize the parameters.
exe.run(paddle.static.default_startup_program())
out = callback(paddle.clone(node_x), node_v)

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

saveModel(name, exe, feedkeys=['x', 'v'], fetchlist=[out], inputs=[x, value], outputs=[outs[0]], target_dir=sys.argv[1])


def main():
shape = (2, 3, 4, 5)
dtype = "float32"
data = np.random.random(shape).astype(dtype)
value = np.array([0]).astype(dtype)

def set_value1(x, value):
x[1:2, :, 2:4] = value
return x

paddle_set_value("set_value1", data, value, set_value1, dtype)

shape = (5, 3, 1)
dtype = "float32"
data = np.random.random(shape).astype("float32")
value = np.random.random((3, 3, 1)).astype(dtype)

def set_value2(x, value):
x[2:5, :] = value
return x


paddle_set_value("set_value2", data, value, set_value2, dtype)

shape = (10, 2, 5)
dtype = "int32"
data = np.random.randint(0, 5, shape).astype(dtype)
value = np.random.randint(0, 2, (3, 1, 1)).astype(dtype)
def set_value3(x, value):
x[-4:-1] = value
return x

paddle_set_value("set_value3", data, value, set_value3, dtype)

if __name__ == "__main__":
main()