Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

[PT FE] Support aten::constant_pad_nd (#31629)

Maxim Vafinf14d058ed703
diff --git a/src/frontends/pytorch/src/op/pad.cpp b/src/frontends/pytorch/src/op/pad.cpp
index 9250fdf6a50..115f6e6253d 100644
--- a/src/frontends/pytorch/src/op/pad.cpp
+++ b/src/frontends/pytorch/src/op/pad.cpp
@@ -118,11 +118,14 @@ OutputVector translate_pad(const NodeContext& context) {
return translate_pad_common(context, data, paddings, pad_value, mode);
}
-OutputVector translate_constant_pad_nd_fx(const NodeContext& context) {
- num_inputs_check(context, 3, 3);
+OutputVector translate_constant_pad_nd(const NodeContext& context) {
+ num_inputs_check(context, 2, 3);
auto data = context.get_input(0);
auto paddings = context.const_input<std::vector<int64_t>>(1);
- auto pad_value = context.get_input(2);
+ Output<Node> pad_value = context.mark_node(v0::Constant::create(element::f32, Shape{}, {0}));
+ if (!context.input_is_none(2)) {
+ pad_value = context.get_input(2);
+ }
return translate_pad_common(context, data, paddings, pad_value);
}
diff --git a/src/frontends/pytorch/src/op_table.cpp b/src/frontends/pytorch/src/op_table.cpp
index 89fd27dce9a..9b63dd9e61f 100644
--- a/src/frontends/pytorch/src/op_table.cpp
+++ b/src/frontends/pytorch/src/op_table.cpp
@@ -71,6 +71,7 @@ OP_CONVERTER(translate_conv_transposend);
OP_CONVERTER(translate_convnd);
OP_CONVERTER(translate_convolution);
OP_CONVERTER(translate_convolution_mode);
+OP_CONVERTER(translate_constant_pad_nd);
OP_CONVERTER(translate_copy_);
OP_CONVERTER(translate_cross);
OP_CONVERTER(translate_cumsum);
@@ -298,7 +299,6 @@ OP_CONVERTER(translate_batch_norm_legit_fx);
OP_CONVERTER(translate_batch_norm_legit_no_training_fx);
OP_CONVERTER(translate_batch_norm_legit_no_stats_fx);
OP_CONVERTER(translate_cat_fx);
-OP_CONVERTER(translate_constant_pad_nd_fx);
OP_CONVERTER(translate_copy_fx);
OP_CONVERTER(translate_cumsum_fx);
OP_CONVERTER(translate_chunk_fx);
@@ -474,6 +474,7 @@ const std::unordered_map<std::string, CreatorFunction> get_supported_ops_ts() {
{"aten::conv2d", op::translate_convnd},
{"aten::conv3d", op::translate_convnd},
{"aten::convolution", op::translate_convolution},
+ {"aten::constant_pad_nd", op::translate_constant_pad_nd},
{"aten::copy", op::skip_node},
{"aten::copy_", op::translate_copy_},
{"aten::cos", op::optional_out<op::translate_1to1_match_1_inputs_with_fp32_type_alignment<opset10::Cos>, 1>},
@@ -891,7 +892,7 @@ const std::unordered_map<std::string, CreatorFunction> get_supported_ops_fx() {
{"aten.clamp_min.Tensor", op::translate_1to1_match_2_inputs_align_types<opset10::Maximum>},
{"aten.clone.default", op::skip_node}, // ignore clone operators that are inserted by PyTorch autograd
{"aten.col2im.default", op::translate_col2im},
- {"aten.constant_pad_nd.default", op::translate_constant_pad_nd_fx},
+ {"aten.constant_pad_nd.default", op::translate_constant_pad_nd},
{"aten.convolution.default", op::translate_convolution},
{"aten.copy.default", op::translate_copy_fx},
{"aten.copy_.default", op::translate_copy_},
diff --git a/src/frontends/pytorch/src/transforms/prim_list_construct_pad.cpp b/src/frontends/pytorch/src/transforms/prim_list_construct_pad.cpp
index dcc525b4a52..ef74783a6bf 100644
--- a/src/frontends/pytorch/src/transforms/prim_list_construct_pad.cpp
+++ b/src/frontends/pytorch/src/transforms/prim_list_construct_pad.cpp
@@ -61,7 +61,7 @@ const std::unordered_map<std::string, PadMode> PAD_MODES = {{"constant", PadMode
PrimListConstructPadReplacer::PrimListConstructPadReplacer() {
// transformation for case aten::pad + prim::ListConstruct as paddings
const auto& pad_op = ov::pass::pattern::wrap_type<ov::op::util::FrameworkNode>(
- fw_node_predicate({"aten::pad", "aten::reflection_pad2d"}));
+ fw_node_predicate({"aten::pad", "aten::constant_pad_nd", "aten::reflection_pad2d"}));
ov::matcher_pass_callback callback = [](ov::pass::pattern::Matcher& m) {
Output<Node> input_node;
Output<Node> padding;
@@ -92,6 +92,15 @@ PrimListConstructPadReplacer::PrimListConstructPadReplacer() {
padding = pad_op->input_value(1);
// Pad value is used only for constant pad, fill with 0 as placeholder.
pad_value = v0::Constant::create(element::f32, Shape{}, {0});
+ } else if ((pad_op = cast_fw_node(m.get_match_root(), "aten::constant_pad_nd"))) {
+ mode = "constant";
+ input_node = pad_op->input_value(0);
+ padding = pad_op->input_value(1);
+ if (pad_op->get_input_size() > 2) {
+ pad_value = pad_op->input_value(2);
+ } else {
+ pad_value = v0::Constant::create(element::f32, Shape{}, {0});
+ }
} else {
return false;
}
diff --git a/tests/layer_tests/pytorch_tests/test_constant_pad_nd.py b/tests/layer_tests/pytorch_tests/test_constant_pad_nd.py
index 56c2417e7df..a3108e3fc62 100644
--- a/tests/layer_tests/pytorch_tests/test_constant_pad_nd.py
+++ b/tests/layer_tests/pytorch_tests/test_constant_pad_nd.py
@@ -21,16 +21,15 @@ class TestConstantPadND(PytorchLayerTest):
self.value = value
def forward(self, x):
- return torch.constant_pad_nd(x, self.pad, self.value);
+ return torch.constant_pad_nd(x, self.pad, self.value)
-
- ref_net = None
-
- return aten_constant_pad_nd(pad, value), ref_net, "aten::constant_pad_nd"
+ return aten_constant_pad_nd(pad, value), None, "aten::constant_pad_nd"
@pytest.mark.parametrize(("pad", "value"),
[((1,1,1,1), 0),((0,2,0,2), -1.0),((3,1,5,2), 0.5),((0,0,0,0), 0),])
+ @pytest.mark.precommit
+ @pytest.mark.precommit_torch_export
@pytest.mark.precommit_fx_backend
def test_constant_pad_nd(self, pad, value, ie_device, precision, ir_version):
self._test(*self.create_model(pad, value),