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
add aten::logaddexp (#30044)
Ekaterina Aidovae635d987c51c
diff --git a/src/frontends/pytorch/src/op/logaddexp.cpp b/src/frontends/pytorch/src/op/logaddexp.cppnew file mode 100644index 00000000000..590ffa40c8f--- /dev/null+++ b/src/frontends/pytorch/src/op/logaddexp.cpp@@ -0,0 +1,54 @@+// Copyright (C) 2018-2025 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include "openvino/frontend/pytorch/node_context.hpp"+#include "openvino/op/add.hpp"+#include "openvino/op/exp.hpp"+#include "openvino/op/log.hpp"+#include "openvino/op/maximum.hpp"+#include "openvino/op/subtract.hpp"+#include "utils.hpp"++namespace ov {+namespace frontend {+namespace pytorch {+namespace op {++using namespace ov::op;++OutputVector translate_logaddexp(const NodeContext& context) {+ // "aten::logaddexp(Tensor self, Tensor other, out=None) -> Tensor"+ num_inputs_check(context, 2, 3);+ Output<Node> input1;+ Output<Node> input2;+ std::tie(input1, input2) = get_inputs_with_promoted_types(context, 0, 1);+ // Find maximum of inputs+ auto max_val = context.mark_node(std::make_shared<v1::Maximum>(input1, input2));++ // Calculate x1 - max and x2 - max+ auto diff1 = context.mark_node(std::make_shared<v1::Subtract>(input1, max_val));+ auto diff2 = context.mark_node(std::make_shared<v1::Subtract>(input2, max_val));++ // Calculate exp(x1 - max) and exp(x2 - max)+ auto exp1 = context.mark_node(std::make_shared<v0::Exp>(diff1));+ auto exp2 = context.mark_node(std::make_shared<v0::Exp>(diff2));++ // Add the scaled exponentials+ auto sum = context.mark_node(std::make_shared<v1::Add>(exp1, exp2));++ // Take the log and add back the maximum+ auto log_sum = context.mark_node(std::make_shared<v0::Log>(sum));+ auto result = context.mark_node(std::make_shared<v1::Add>(log_sum, max_val));++ if (!context.input_is_none(2)) {+ context.mutate_input(2, result);+ }++ return {result};+};++} // namespace op+} // namespace pytorch+} // namespace frontend+} // namespace ov\ No newline at end of filediff --git a/src/frontends/pytorch/src/op_table.cpp b/src/frontends/pytorch/src/op_table.cppindex 3ccada7289a..f71535fb178 100644--- a/src/frontends/pytorch/src/op_table.cpp+++ b/src/frontends/pytorch/src/op_table.cpp@@ -151,6 +151,7 @@ OP_CONVERTER(translate_linear);OP_CONVERTER(translate_linspace);OP_CONVERTER(translate_list_construct);OP_CONVERTER(translate_list_unpack);+OP_CONVERTER(translate_logaddexp);OP_CONVERTER(translate_log1p);OP_CONVERTER(translate_log_sigmoid);OP_CONVERTER(translate_log_softmax);@@ -580,6 +581,7 @@ const std::unordered_map<std::string, CreatorFunction> get_supported_ops_ts() {{"aten::linspace", op::translate_linspace},{"aten::log", op::optional_out<op::translate_1to1_match_1_inputs_with_fp32_type_alignment<opset10::Log>, 1>},{"aten::log_", op::inplace_op<op::translate_1to1_match_1_inputs<opset10::Log>>},+ {"aten::logaddexp", op::translate_logaddexp},{"aten::logical_and", op::translate_and},{"aten::logical_or", op::translate_or},{"aten::logical_not", op::translate_not},diff --git a/tests/layer_tests/pytorch_tests/test_logaddexp.py b/tests/layer_tests/pytorch_tests/test_logaddexp.pynew file mode 100644index 00000000000..74e0ae14bb0--- /dev/null+++ b/tests/layer_tests/pytorch_tests/test_logaddexp.py@@ -0,0 +1,149 @@+# Copyright (C) 2018-2025 Intel Corporation+# SPDX-License-Identifier: Apache-2.0++import pytest+import numpy as np+from pytorch_layer_test_class import PytorchLayerTest+++class TestLogAddExp(PytorchLayerTest):+ def _prepare_input(self, input1, input2, dtype1="float32", dtype2="float32", out=False):+ """Prepare inputs for logaddexp testing"""+ inp1 = np.array(input1).astype(dtype1)+ inp2 = np.array(input2).astype(dtype2)+ if not out:+ return (inp1, inp2)+ return (inp1, inp2, np.zeros_like(np.logaddexp(inp1, inp2)))++ def create_model(self, out=False):+ import torch+++ class LogAddExpModel(torch.nn.Module):+ def __init__(self, out):+ super(LogAddExpModel, self).__init__()+ if out:+ self.forward = self.forward_out++ def forward(self, x, y):+ return torch.logaddexp(x, y)++ def forward_out(self, x, y, out):+ return torch.logaddexp(x, y, out=out), out++ model_class = LogAddExpModel(out)+ ref_net = None++ return model_class, ref_net, "aten::logaddexp"++ @pytest.mark.parametrize(+ "dtype1",+ [+ "float32",+ "float64",+ ],+ )+ @pytest.mark.parametrize(+ "dtype2",+ [+ "float32",+ "float64",+ ],+ )+ @pytest.mark.parametrize(+ "input1,input2",+ [+ # Basic cases+ (0.0, 0.0), # log(exp(0) + exp(0)) = log(2)+ (1.0, 1.0), # log(exp(1) + exp(1)) = log(2*e)+ (-1.0, -1.0), # log(exp(-1) + exp(-1)) = log(2/e)++ # One large, one small number+ (100.0, 0.0), # Tests handling of large differences+ (-100.0, 0.0), # Tests handling of negative large differences++ # Both large numbers+ (100.0, 100.0), # Tests numerical stability with large numbers+ (-100.0, -100.0), # Tests numerical stability with large negative numbers++ # Numbers with different signs+ (1.0, -1.0), # Tests mixed positive/negative+ (-1.0, 1.0), # Tests mixed negative/positive++ # Near-zero cases+ (1e-7, 1e-7), # Tests handling of very small numbers+ (-1e-7, -1e-7), # Tests handling of very small negative numbers+ ],+ )+ @pytest.mark.parametrize(+ "out", (False, True)+ )+ def test_logaddexp_basic(self, dtype1, dtype2, input1, input2, out, ie_device, precision, ir_version):+ self._test(+ *self.create_model(out),+ ie_device,+ precision,+ ir_version,+ kwargs_to_prepare_input={"input1": input1, "input2": input2, "dtype1": dtype1, "dtype2": dtype2, "out": out},+ rtol=1e-5 # Relative tolerance for floating point comparisons+ )++ @pytest.mark.nightly+ @pytest.mark.precommit+ @pytest.mark.parametrize(+ "dtype1",+ [+ "float32",+ "float64",+ ],+ )+ @pytest.mark.parametrize(+ "dtype2",+ [+ "float32",+ "float64",+ ],+ )+ @pytest.mark.parametrize(+ "shape",+ [+ (3,), # 1D array+ (2, 3), # 2D array+ (2, 2, 2), # 3D array+ (1, 1), # Broadcasting test+ (3, 1), # Broadcasting test+ (1, 3), # Broadcasting test+ ],+ )+ @pytest.mark.parametrize(+ "out", (False, True)+ )+ def test_logaddexp_shapes(self, dtype1, dtype2, shape, out, ie_device, precision, ir_version):+ # Generate random inputs within a reasonable range+ input1 = np.random.uniform(-10, 10, shape)+ input2 = np.random.uniform(-10, 10, shape)++ self._test(+ *self.create_model(out),+ ie_device,+ precision,+ ir_version,+ kwargs_to_prepare_input={"input1": input1, "input2": input2, "dtype1": dtype1, "dtype2": dtype2, "out": out},+ rtol=1e-5+ )++ @pytest.mark.nightly+ @pytest.mark.precommit+ def test_logaddexp_broadcasting(self, ie_device, precision, ir_version):+ # Test broadcasting with different shapes+ input1 = np.array([[1.0, 2.0, 3.0]], dtype=np.float32) # Shape (1, 3)+ input2 = np.array([[1.0], [2.0]], dtype=np.float32) # Shape (2, 1)++ self._test(+ *self.create_model(),+ ie_device,+ precision,+ ir_version,+ kwargs_to_prepare_input={"input1": input1, "input2": input2},+ rtol=1e-5+ )\ No newline at end of file