OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
Fix: Stable implementation for ReduceLogSumExp (ONNX frontend) (#32913)
Siddhant Shekhar97909baf9fe2
diff --git a/src/frontends/onnx/docs/supported_ops.md b/src/frontends/onnx/docs/supported_ops.mdindex 5c158457295..cafb18e0054 100644--- a/src/frontends/onnx/docs/supported_ops.md+++ b/src/frontends/onnx/docs/supported_ops.md@@ -143,7 +143,7 @@ OpenVINO provides support for operations of Default Opset (empty in table below)| |Reciprocal |1 |13, 6, 1 | || |ReduceL1 |18, 13, 1 |18, 13, 11, 1 | || |ReduceL2 |18, 13, 1 |18, 13, 11, 1 | |-| |ReduceLogSum |18, 1 |18, 13, 11, 1 | |+| |ReduceLogSum |18, 13, 1 |18, 13, 11, 1 | || |ReduceLogSumExp |18, 13, 1 |18, 13, 11, 1 | || |ReduceMax |20, 18, 13, 1 |20, 18, 13, 12, 11, 1 | || |ReduceMean |18, 13, 1 |18, 13, 11, 1 | |diff --git a/src/frontends/onnx/frontend/src/op/reduce.cpp b/src/frontends/onnx/frontend/src/op/reduce.cppindex 92acbe1754a..a80cebcde39 100644--- a/src/frontends/onnx/frontend/src/op/reduce.cpp+++ b/src/frontends/onnx/frontend/src/op/reduce.cpp@@ -5,6 +5,7 @@#include "core/operator_set.hpp"#include "exceptions.hpp"#include "openvino/frontend/exception.hpp"+#include "openvino/op/add.hpp"#include "openvino/op/constant.hpp"#include "openvino/op/convert.hpp"#include "openvino/op/exp.hpp"@@ -21,6 +22,7 @@#include "openvino/op/reduce_sum.hpp"#include "openvino/op/shape_of.hpp"#include "openvino/op/squeeze.hpp"+#include "openvino/op/subtract.hpp"#include "utils/common.hpp"using namespace ov::op;@@ -149,19 +151,61 @@ std::shared_ptr<ov::Node> onnx_reduce_sum_square(const ov::frontend::onnx::Node&const auto square_node = std::make_shared<v1::Multiply>(input, input);return make_ov_reduction_op<v1::ReduceSum>(node, square_node, supported_types, axes_as_attr);}++ov::OutputVector onnx_reduce_log_sum_exp_stable(const ov::frontend::onnx::Node& node, bool axes_as_attr = true) {+ auto input = node.get_ov_inputs().at(0);+ auto keepdims = static_cast<bool>(node.get_attribute_value<std::int64_t>("keepdims", 1));++ auto reduction_axes = axes_as_attr ? get_reduction_axes_from_attr(node) : get_reduction_axes_from_input(node);++ if (reduction_axes == nullptr) {+ return {std::make_shared<ov::op::v16::Identity>(input)};+ }++ // Numerically stable LogSumExp: k + log(sum(exp(x - k))) where k = max(x)+ // For numerical stability and avoiding exponent explosion:+ // ln(e^x1 + ... + e^xn) = ln(e^k * (e^(x1-k) + ... + e^(xn-k)))+ // = k + ln(e^(x1-k) + ... + e^(xn-k))+ // where k = max(x1, ..., xn), so all exponent degrees <= 0.+ //+ // Always compute k with keepdims=true so that (input - k) broadcasts correctly+ // regardless of which axes are being reduced. Then squeeze k at the end if the+ // original keepdims was false. This follows the pattern from the PyTorch frontend+ // (src/frontends/pytorch/src/op/log.cpp).+ std::shared_ptr<ov::Node> k = std::make_shared<ov::op::v1::ReduceMax>(input, reduction_axes, true);+ auto input_minus_k = std::make_shared<ov::op::v1::Subtract>(input, k);+ auto exp_node = std::make_shared<ov::op::v0::Exp>(input_minus_k);+ auto sum_node = std::make_shared<ov::op::v1::ReduceSum>(exp_node, reduction_axes, keepdims);+ auto log_node = std::make_shared<ov::op::v0::Log>(sum_node);++ if (!keepdims) {+ k = std::make_shared<ov::op::v0::Squeeze>(k, reduction_axes);+ }++ auto out = std::make_shared<ov::op::v1::Add>(k, log_node);+ return {out};+}} // namespacenamespace opset_1 {ov::OutputVector reduce_log_sum(const ov::frontend::onnx::Node& node) {- const ov::Output<ov::Node> sum_node =- make_ov_reduction_op<v1::ReduceSum>(node, node.get_ov_inputs().at(0), supported_types_v2);- return {std::make_shared<v0::Log>(sum_node)};+ auto input = node.get_ov_inputs().at(0);+ auto keepdims = static_cast<bool>(node.get_attribute_value<std::int64_t>("keepdims", 1));++ auto reduction_axes =+ (node.get_ov_inputs().size() > 1) ? get_reduction_axes_from_input(node) : get_reduction_axes_from_attr(node);++ if (reduction_axes == nullptr) {+ return {std::make_shared<ov::op::v16::Identity>(input)};+ }++ auto sum_node = std::make_shared<ov::op::v1::ReduceSum>(input, reduction_axes, keepdims);+ auto log_node = std::make_shared<ov::op::v0::Log>(sum_node);+ return {log_node};}ov::OutputVector reduce_log_sum_exp(const ov::frontend::onnx::Node& node) {- const auto exp_node = std::make_shared<v0::Exp>(node.get_ov_inputs().at(0));- const ov::Output<ov::Node> sum_node = make_ov_reduction_op<v1::ReduceSum>(node, exp_node, supported_types_v1);- return {std::make_shared<v0::Log>(sum_node)};+ return onnx_reduce_log_sum_exp_stable(node, true);}ov::OutputVector reduce_l1(const ov::frontend::onnx::Node& node) {@@ -233,10 +277,23 @@ ov::OutputVector reduce_l2(const Node& node) {return {make_ov_reduction_op<v4::ReduceL2>(node, node.get_ov_inputs().at(0), supported_types_v2)};}+ov::OutputVector reduce_log_sum(const ov::frontend::onnx::Node& node) {+ auto input = node.get_ov_inputs().at(0);+ auto keepdims = static_cast<bool>(node.get_attribute_value<std::int64_t>("keepdims", 1));++ auto reduction_axes = get_reduction_axes_from_input(node);++ if (reduction_axes == nullptr) {+ return {std::make_shared<ov::op::v16::Identity>(input)};+ }++ auto sum_node = std::make_shared<ov::op::v1::ReduceSum>(input, reduction_axes, keepdims);+ auto log_node = std::make_shared<ov::op::v0::Log>(sum_node);+ return {log_node};+}+ov::OutputVector reduce_log_sum_exp(const ov::frontend::onnx::Node& node) {- const auto exp_node = std::make_shared<v0::Exp>(node.get_ov_inputs().at(0));- const ov::Output<ov::Node> sum_node = make_ov_reduction_op<v1::ReduceSum>(node, exp_node, supported_types_v2);- return {std::make_shared<v0::Log>(sum_node)};+ return onnx_reduce_log_sum_exp_stable(node, false);}ov::OutputVector reduce_max(const ov::frontend::onnx::Node& node) {@@ -262,6 +319,7 @@ ov::OutputVector reduce_sum_square(const ov::frontend::onnx::Node& node) {static bool register_multiple_translators(void) {ONNX_OP_M("ReduceL1", OPSET_RANGE(13, 17), ai_onnx::opset_13::reduce_l1);ONNX_OP_M("ReduceL2", OPSET_RANGE(13, 17), ai_onnx::opset_13::reduce_l2);+ ONNX_OP_M("ReduceLogSum", OPSET_RANGE(13, 17), ai_onnx::opset_13::reduce_log_sum);ONNX_OP_M("ReduceLogSumExp", OPSET_RANGE(13, 17), ai_onnx::opset_13::reduce_log_sum_exp);ONNX_OP_M("ReduceMax", OPSET_RANGE(13, 17), ai_onnx::opset_13::reduce_max);ONNX_OP_M("ReduceMean", OPSET_RANGE(13, 17), ai_onnx::opset_13::reduce_mean);@@ -284,13 +342,6 @@ ov::OutputVector reduce_l1(const Node& node) {return {make_ov_reduction_op<v4::ReduceL1>(node, node.get_ov_inputs().at(0), supported_types_v2, false)};}-ov::OutputVector reduce_log_sum_exp(const ov::frontend::onnx::Node& node) {- const auto exp_node = std::make_shared<v0::Exp>(node.get_ov_inputs().at(0));- const ov::Output<ov::Node> sum_node =- make_ov_reduction_op<v1::ReduceSum>(node, exp_node, supported_types_v3, false);- return {std::make_shared<v0::Log>(sum_node)};-}-ov::OutputVector reduce_max(const ov::frontend::onnx::Node& node) {return {make_ov_reduction_op<v1::ReduceMax>(node, node.get_ov_inputs().at(0), supported_types_v3, false)};}@@ -304,9 +355,22 @@ ov::OutputVector reduce_min(const ov::frontend::onnx::Node& node) {}ov::OutputVector reduce_log_sum(const ov::frontend::onnx::Node& node) {- const ov::Output<ov::Node> sum_node =- make_ov_reduction_op<v1::ReduceSum>(node, node.get_ov_inputs().at(0), supported_types_v2, false);- return {std::make_shared<v0::Log>(sum_node)};+ auto input = node.get_ov_inputs().at(0);+ auto keepdims = static_cast<bool>(node.get_attribute_value<std::int64_t>("keepdims", 1));++ auto reduction_axes = get_reduction_axes_from_input(node);++ if (reduction_axes == nullptr) {+ return {std::make_shared<ov::op::v16::Identity>(input)};+ }++ auto sum_node = std::make_shared<ov::op::v1::ReduceSum>(input, reduction_axes, keepdims);+ auto log_node = std::make_shared<ov::op::v0::Log>(sum_node);+ return {log_node};+}++ov::OutputVector reduce_log_sum_exp(const ov::frontend::onnx::Node& node) {+ return onnx_reduce_log_sum_exp_stable(node, false);}ov::OutputVector reduce_prod(const ov::frontend::onnx::Node& node) {diff --git a/src/frontends/onnx/tests/onnx_import.in.cpp b/src/frontends/onnx/tests/onnx_import.in.cppindex c75c3327c2c..fbb5544db1b 100644--- a/src/frontends/onnx/tests/onnx_import.in.cpp+++ b/src/frontends/onnx/tests/onnx_import.in.cpp@@ -1106,6 +1106,49 @@ OPENVINO_TEST(${BACKEND_NAME}, onnx_model_reduce_log_sum_exp_18) {test_case.run_with_tolerance_as_fp();}+// Numerical stability tests for ReduceLogSumExp (issue #32839).+// The old naive implementation log(sum(exp(x))) overflows to Inf for inputs >= 89.+// The stable implementation k + log(sum(exp(x - k))) should produce correct finite results.++OPENVINO_TEST(${BACKEND_NAME}, onnx_model_reduce_log_sum_exp_large_values) {+ // Uses opset 1-12 model with keepdims=true, reducing all axes+ auto model = convert_model("reduce_log_sum_exp.onnx");++ // input data shape (1, 1, 4, 4) - all values 100.0, well above overflow threshold (~88.7)+ Inputs inputs{ov::test::NDArray<float, 4>(+ {{{{100, 100, 100, 100}, {100, 100, 100, 100}, {100, 100, 100, 100}, {100, 100, 100, 100}}}})+ .get_vector()};++ // Expected: 100 + log(16) = 100 + 2.77258872... = 102.77258872+ // Old naive implementation would produce Inf here+ auto expected_output = ov::test::NDArray<float, 4>({{{{102.77259f}}}}).get_vector();++ auto test_case = ov::test::TestCase(model, s_device);+ test_case.add_multiple_inputs(inputs);+ test_case.add_expected_output(expected_output);+ test_case.run_with_tolerance_as_fp();+}++OPENVINO_TEST(${BACKEND_NAME}, onnx_model_reduce_log_sum_exp_18_large_values) {+ // Uses opset 18 model with keepdims=false, reducing non-trailing axes+ // This also tests the broadcasting fix for keepdims=false+ auto model = convert_model("reduce_log_sum_exp_18.onnx");++ // input data shape (1, 1, 4, 4) - all values 100.0+ Inputs inputs{ov::test::NDArray<float, 4>(+ {{{{100, 100, 100, 100}, {100, 100, 100, 100}, {100, 100, 100, 100}, {100, 100, 100, 100}}}})+ .get_vector()};++ // output data shape (4) - each element reduces 4 values along axis 2+ // Expected: 100 + log(4) = 100 + 1.38629... = 101.38629+ auto expected_output = ov::test::NDArray<float, 1>({101.38629f, 101.38629f, 101.38629f, 101.38629f}).get_vector();++ auto test_case = ov::test::TestCase(model, s_device);+ test_case.add_multiple_inputs(inputs);+ test_case.add_expected_output(expected_output);+ test_case.run_with_tolerance_as_fp();+}+OPENVINO_TEST(${BACKEND_NAME}, onnx_model_reduce_l1) {auto model = convert_model("reduce_l1.onnx");