Skip to content

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

[Paddle FE] argmax/argmin: respect the keepdims attribute (#37972)

siyiweige57631bc2bfae
diff --git a/src/frontends/paddle/src/op/argmax.cpp b/src/frontends/paddle/src/op/argmax.cpp
index 72ba8246ec7..8428268ffd1 100644
--- a/src/frontends/paddle/src/op/argmax.cpp
+++ b/src/frontends/paddle/src/op/argmax.cpp
@@ -14,26 +14,39 @@ NamedOutputs argmax(const NodeContext& node) {
if (!flatten) {
auto axis = node.get_attribute<int64_t>("axis");
- const auto axis_to_remove = ov::opset6::Constant::create(element::u64, Shape{}, {axis});
auto node_topk = std::make_shared<ov::opset6::TopK>(data, k, axis, "max", "index", dtype);
- const auto reshaped_indices = std::make_shared<ov::opset6::Squeeze>(node_topk->output(1), axis_to_remove);
- return node.default_single_output_mapping({std::make_shared<ov::opset6::Convert>(reshaped_indices, dtype)},
- {"Out"});
+ auto result = node_topk->output(1);
+ // Paddle drops the reduced axis only when keepdims is false; with keepdims == true the
+ // TopK index output already keeps the axis (size k==1) at the correct position, so the
+ // Squeeze must be skipped.
+ if (!node.get_attribute<bool>("keepdims", false)) {
+ const auto axis_to_remove = ov::opset6::Constant::create(element::u64, Shape{}, {axis});
+ result = std::make_shared<ov::opset6::Squeeze>(result, axis_to_remove);
+ }
+ return node.default_single_output_mapping({std::make_shared<ov::opset6::Convert>(result, dtype)}, {"Out"});
} else {
int64_t axis = 0;
const Output<ov::Node> reshape_flatten = ov::opset6::Constant::create(ov::element::i64, {1}, {-1});
auto node_reshape = std::make_shared<ov::opset6::Reshape>(data, reshape_flatten, true);
auto node_topk = std::make_shared<ov::opset6::TopK>(node_reshape, k, axis, "max", "index", dtype);
+ // TopK keeps the reduced axis as a size-1 dimension, so the index output is a 1-D tensor of
+ // one element; the shape Paddle expects is restored from the output port information.
+ Output<ov::Node> result = node_topk->output(1);
const auto output_info = node.get_output_port_infos("Out");
size_t output_size = output_info[0].second.size();
if (output_size == 0) {
- auto out = std::make_shared<ov::opset6::Squeeze>(node_topk->output(1));
- return node.default_single_output_mapping({std::make_shared<ov::opset6::Convert>(out, dtype)}, {"Out"});
- } else {
- return node.default_single_output_mapping(
- {std::make_shared<ov::opset6::Convert>(node_topk->output(1), dtype)},
- {"Out"});
+ // Paddle returns a scalar when keepdims is false.
+ result = std::make_shared<ov::opset6::Squeeze>(result);
+ } else if (output_size > 1) {
+ // With keepdims == true the flattened reduction keeps the rank of the input, every
+ // dimension being of size 1, so an (A, B, C) input yields a (1, 1, 1) output.
+ const std::vector<int64_t> keepdims_shape(output_size, 1);
+ result = std::make_shared<ov::opset6::Reshape>(
+ result,
+ ov::opset6::Constant::create(ov::element::i64, Shape{output_size}, keepdims_shape),
+ false);
}
+ return node.default_single_output_mapping({std::make_shared<ov::opset6::Convert>(result, dtype)}, {"Out"});
}
}
diff --git a/src/frontends/paddle/src/op/argmin.cpp b/src/frontends/paddle/src/op/argmin.cpp
index b81646ef924..c17dde0f41e 100644
--- a/src/frontends/paddle/src/op/argmin.cpp
+++ b/src/frontends/paddle/src/op/argmin.cpp
@@ -14,26 +14,39 @@ NamedOutputs argmin(const NodeContext& node) {
if (!flatten) {
auto axis = node.get_attribute<int64_t>("axis");
- const auto axis_to_remove = ov::opset6::Constant::create(element::u64, Shape{}, {axis});
auto node_topk = std::make_shared<ov::opset6::TopK>(data, k, axis, "min", "index", dtype);
- const auto reshaped_indices = std::make_shared<ov::opset6::Squeeze>(node_topk->output(1), axis_to_remove);
- return node.default_single_output_mapping({std::make_shared<ov::opset6::Convert>(reshaped_indices, dtype)},
- {"Out"});
+ auto result = node_topk->output(1);
+ // Paddle drops the reduced axis only when keepdims is false; with keepdims == true the
+ // TopK index output already keeps the axis (size k==1) at the correct position, so the
+ // Squeeze must be skipped.
+ if (!node.get_attribute<bool>("keepdims", false)) {
+ const auto axis_to_remove = ov::opset6::Constant::create(element::u64, Shape{}, {axis});
+ result = std::make_shared<ov::opset6::Squeeze>(result, axis_to_remove);
+ }
+ return node.default_single_output_mapping({std::make_shared<ov::opset6::Convert>(result, dtype)}, {"Out"});
} else {
int64_t axis = 0;
const Output<ov::Node> reshape_flatten = ov::opset6::Constant::create(ov::element::i64, {1}, {-1});
auto node_reshape = std::make_shared<ov::opset6::Reshape>(data, reshape_flatten, true);
auto node_topk = std::make_shared<ov::opset6::TopK>(node_reshape, k, axis, "min", "index", dtype);
+ // TopK keeps the reduced axis as a size-1 dimension, so the index output is a 1-D tensor of
+ // one element; the shape Paddle expects is restored from the output port information.
+ Output<ov::Node> result = node_topk->output(1);
const auto output_info = node.get_output_port_infos("Out");
size_t output_size = output_info[0].second.size();
if (output_size == 0) {
- auto out = std::make_shared<ov::opset6::Squeeze>(node_topk->output(1));
- return node.default_single_output_mapping({std::make_shared<ov::opset6::Convert>(out, dtype)}, {"Out"});
- } else {
- return node.default_single_output_mapping(
- {std::make_shared<ov::opset6::Convert>(node_topk->output(1), dtype)},
- {"Out"});
+ // Paddle returns a scalar when keepdims is false.
+ result = std::make_shared<ov::opset6::Squeeze>(result);
+ } else if (output_size > 1) {
+ // With keepdims == true the flattened reduction keeps the rank of the input, every
+ // dimension being of size 1, so an (A, B, C) input yields a (1, 1, 1) output.
+ const std::vector<int64_t> keepdims_shape(output_size, 1);
+ result = std::make_shared<ov::opset6::Reshape>(
+ result,
+ ov::opset6::Constant::create(ov::element::i64, Shape{output_size}, keepdims_shape),
+ false);
}
+ return node.default_single_output_mapping({std::make_shared<ov::opset6::Convert>(result, dtype)}, {"Out"});
}
}
diff --git a/src/frontends/paddle/tests/op_fuzzy.cpp b/src/frontends/paddle/tests/op_fuzzy.cpp
index f4943c8f572..1854be4b6de 100644
--- a/src/frontends/paddle/tests/op_fuzzy.cpp
+++ b/src/frontends/paddle/tests/op_fuzzy.cpp
@@ -16,6 +16,10 @@ using PaddleFuzzyOpTest = FrontEndFuzzyOpTest;
static const std::vector<std::string> models{
std::string("argmax/argmax.pdmodel"),
std::string("argmax1/argmax1.pdmodel"),
+ std::string("argmax_keepdim/argmax_keepdim.pdmodel"),
+ std::string("argmax_keepdim_flatten/argmax_keepdim_flatten.pdmodel"),
+ std::string("argmin_keepdim/argmin_keepdim.pdmodel"),
+ std::string("argmin_keepdim_flatten/argmin_keepdim_flatten.pdmodel"),
std::string("assign_none/assign_none.pdmodel"),
std::string("assign_output/assign_output.pdmodel"),
std::string("assign_value_boolean/assign_value_boolean.pdmodel"),
diff --git a/src/frontends/paddle/tests/test_models/gen_scripts/generate_argmax.py b/src/frontends/paddle/tests/test_models/gen_scripts/generate_argmax.py
index a033105d11f..07ca30e4806 100644
--- a/src/frontends/paddle/tests/test_models/gen_scripts/generate_argmax.py
+++ b/src/frontends/paddle/tests/test_models/gen_scripts/generate_argmax.py
@@ -10,13 +10,34 @@ import sys
data_type = 'float32'
-def paddle_argmax(name : str, x, axis):
+def paddle_argmax(name : str, x, axis, keepdim=False):
import paddle
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='float32')
- out = paddle.argmax(x=node_x, axis=axis)
+ out = paddle.argmax(x=node_x, axis=axis, keepdim=keepdim)
+ out = paddle.cast(out, np.float32)
+ 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())
+
+ outs = exe.run(
+ feed={'x': x},
+ fetch_list=[out])
+
+ saveModel(name, exe, feed_vars=[node_x], fetchlist=[out], inputs=[x], outputs=[outs[0]], target_dir=sys.argv[1])
+
+ return outs[0]
+
+def paddle_argmin(name : str, x, axis, keepdim=False):
+ import paddle
+ 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='float32')
+ out = paddle.argmin(x=node_x, axis=axis, keepdim=keepdim)
out = paddle.cast(out, np.float32)
cpu = paddle.static.cpu_places(1)
exe = paddle.static.Executor(cpu[0])
@@ -57,6 +78,20 @@ def main():
axis = 0
paddle_argmax("argmax", data, axis)
paddle_argmax1("argmax1", data)
+ # issue 37827: keepdims=True must keep the reduced axis (shape [2,2,1] for axis=2)
+ data2 = np.array([[[1.0, 5.0, 3.0], [2.0, 4.0, 9.0]],
+ [[3.0, 2.0, 1.0], [8.0, 7.0, 6.0]]]).astype(np.float32)
+ paddle_argmax("argmax_keepdim", data2, axis=2, keepdim=True)
+ paddle_argmin("argmin_keepdim", data2, axis=2, keepdim=True)
+ # issue 37827: axis=None is exported as flatten=True; with keepdims=True the flattened
+ # reduction keeps the rank of the input, all the dimensions being of size 1 (an (2,3,4)
+ # input gives a (1,1,1) output). The extremes are moved away from the ends so that the
+ # flattened index is not trivially the first or the last element.
+ data3 = np.linspace(-1.0, 1.0, 24, dtype=np.float32).reshape(2, 3, 4)
+ data3[0, 1, 2] = 5.0 # flattened index 6
+ data3[1, 2, 1] = -5.0 # flattened index 21
+ paddle_argmax("argmax_keepdim_flatten", data3, axis=None, keepdim=True)
+ paddle_argmin("argmin_keepdim_flatten", data3, axis=None, keepdim=True)
if __name__ == "__main__":