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 grid_sampler op #15869

Merged
merged 21 commits into from
May 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
29 changes: 29 additions & 0 deletions src/frontends/paddle/src/op/grid_sampler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (C) 2018-2023 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

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

namespace ov {
namespace frontend {
namespace paddle {
namespace op {
NamedOutputs grid_sampler(const NodeContext& node) {
auto data = node.get_input("X");
auto grid = node.get_input("Grid");
default_opset::GridSample::Attributes attributes{};

attributes.align_corners = node.get_attribute<bool>("align_corners", 1);
attributes.mode = ov::EnumNames<default_opset::GridSample::InterpolationMode>::as_enum(
node.get_attribute<std::string>("mode", "bilinear"));
attributes.padding_mode = ov::EnumNames<default_opset::GridSample::PaddingMode>::as_enum(
node.get_attribute<std::string>("padding_mode", "zeros"));

return node.default_single_output_mapping({std::make_shared<default_opset::GridSample>(data, grid, attributes)},
{"Output"});
}
} // 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 @@ -50,6 +50,7 @@ OP_CONVERTER(gather);
OP_CONVERTER(gather_nd);
OP_CONVERTER(gelu);
OP_CONVERTER(greater_than);
OP_CONVERTER(grid_sampler);
OP_CONVERTER(group_norm);
OP_CONVERTER(hard_sigmoid);
OP_CONVERTER(hard_swish);
Expand Down Expand Up @@ -163,6 +164,7 @@ std::map<std::string, CreatorFunction> get_supported_ops() {
{"generate_proposals_v2", op::generate_proposals_v2},
{"greater_equal", op::elementwise_greater_equal},
{"greater_than", op::greater_than},
{"grid_sampler", op::grid_sampler},
{"group_norm", op::group_norm},
{"hard_sigmoid", op::hard_sigmoid},
{"hard_swish", op::hard_swish},
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 @@ -235,6 +235,9 @@ static const std::vector<std::string> models{
std::string("greater_than_float32"),
std::string("greater_than_int32"),
std::string("greater_than_int64"),
std::string("grid_sampler_1"),
std::string("grid_sampler_2"),
std::string("grid_sampler_dyn"),
std::string("group_norm_1/group_norm_1.pdmodel"),
std::string("group_norm_2/group_norm_2.pdmodel"),
std::string("group_norm_3/group_norm_3.pdmodel"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Copyright (C) 2018-2023 Intel Corporation
# SPDX-License-Identifier: Apache-2.0

#
# grid_sampler paddle model generator
#
import paddle
import numpy as np
from save_model import saveModel
import sys


def grid_sampler(name: str, x, grid, mode="bilinear", padding_mode="zeros", align_corners=True, not_empty=True,
is_dynamic=False):
paddle.enable_static()

with paddle.static.program_guard(paddle.static.Program(), paddle.static.Program()):
if is_dynamic:
x_node = paddle.static.data(name="x", shape=(-1, -1, -1, -1), dtype=x.dtype)
grid_node = paddle.static.data(name="grid", shape=(-1, -1, -1, 2), dtype=grid.dtype)
else:
x_node = paddle.static.data(name="x", shape=x.shape, dtype=x.dtype)
grid_node = paddle.static.data(name="grid", shape=grid.shape, dtype=grid.dtype)
out = paddle.nn.functional.grid_sample(x_node, grid_node, mode=mode, padding_mode=padding_mode,
align_corners=align_corners) if not_empty else paddle.nn.functional.grid_sample(
x_node, grid_node)
place = paddle.CPUPlace()
exe = paddle.static.Executor(place)
exe.run(paddle.static.default_startup_program())
outs = exe.run(feed={"x": x, "grid": grid}, fetch_list=[out])
saveModel(name, exe, feedkeys=['x', 'grid'], fetchlist=[out], inputs=[x, grid], outputs=[outs[0]],
target_dir=sys.argv[1])

return outs[0]


def main():
dtype = np.float32
x = np.random.randn(2, 2, 3, 3).astype(dtype)
grid = np.random.uniform(-1, 1, [2, 3, 3, 2]).astype(dtype)
grid_sampler(name='grid_sampler_1', x=x, grid=grid, not_empty=False)

x = np.random.randn(2, 3, 5, 6).astype(dtype)
grid = np.random.uniform(-1, 1, [2, 8, 9, 2]).astype(dtype)
mode = "nearest"
padding_mode = "reflection"
align_corners = False
grid_sampler(name='grid_sampler_2', x=x, grid=grid, mode=mode, padding_mode=padding_mode,
align_corners=align_corners)

x = np.random.randn(2, 3, 128, 128).astype(dtype)
grid = np.random.uniform(-1, 1, [2, 130, 130, 2]).astype(dtype)
padding_mode = "border"
grid_sampler(name='grid_sampler_dyn', x=x, grid=grid, mode=mode, padding_mode=padding_mode,
align_corners=align_corners, is_dynamic=True)


if __name__ == "__main__":
main()