-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
[PT FE] Added aten::logaddexp #28539
Open
itsbharatj
wants to merge
6
commits into
openvinotoolkit:master
Choose a base branch
from
itsbharatj:logaddexp
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+194
−0
Open
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1353975
added logaddexp
itsbharatj 3b610e1
#28529 aten::logaddexp
itsbharatj 34b182a
fixed unstable decomposition
itsbharatj b6cb115
Update src/frontends/pytorch/src/op/logaddexp.cpp
itsbharatj e61befd
Update src/frontends/pytorch/src/op/logaddexp.cpp
itsbharatj 08fa3d9
Merge branch 'master' into logaddexp
itsbharatj File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,62 @@ | ||||||||||
// 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/convert.hpp" | ||||||||||
#include "openvino/op/convert_like.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) -> Tensor" | ||||||||||
num_inputs_check(context, 2, 2); | ||||||||||
|
||||||||||
auto input1 = context.get_input(0); | ||||||||||
auto input2 = context.get_input(1); | ||||||||||
|
||||||||||
// Convert inputs to floating point type if needed | ||||||||||
input1 = context.mark_node(std::make_shared<v0::Convert>(input1, element::f32)); | ||||||||||
input2 = context.mark_node(std::make_shared<v0::Convert>(input2, element::f32)); | ||||||||||
|
||||||||||
// 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 the output tensor type is different, convert to match | ||||||||||
if (input1.get_element_type() != element::f32) { | ||||||||||
result = context.mark_node(std::make_shared<v1::ConvertLike>(result, input1)); | ||||||||||
} | ||||||||||
|
||||||||||
Comment on lines
+51
to
+55
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
No need for this if we do calculations in the common dtype. |
||||||||||
return {result}; | ||||||||||
}; | ||||||||||
|
||||||||||
} // namespace op | ||||||||||
} // namespace pytorch | ||||||||||
} // namespace frontend | ||||||||||
} // namespace ov |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
# Copyright (C) 2018-2023 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, dtype="float32"): | ||
"""Prepare inputs for logaddexp testing""" | ||
return (np.array(input1).astype(dtype), np.array(input2).astype(dtype)) | ||
|
||
def create_model(self, dtype=None): | ||
import torch | ||
|
||
dtype_map = { | ||
"float32": torch.float32, | ||
"float64": torch.float64, | ||
} | ||
|
||
class LogAddExpModel(torch.nn.Module): | ||
def __init__(self, dtype=None): | ||
super(LogAddExpModel, self).__init__() | ||
self.dtype = dtype_map.get(dtype) if dtype else None | ||
|
||
def forward(self, x, y): | ||
if self.dtype: | ||
x = x.to(self.dtype) | ||
y = y.to(self.dtype) | ||
itsbharatj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return torch.logaddexp(x, y) | ||
|
||
model_class = LogAddExpModel(dtype) | ||
ref_net = None | ||
|
||
return model_class, ref_net, "aten::logaddexp" | ||
|
||
@pytest.mark.nightly | ||
@pytest.mark.precommit | ||
@pytest.mark.parametrize( | ||
itsbharatj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"dtype", | ||
[ | ||
"float32", | ||
"float64", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add mixed dtypes case when one of the inputs is float and another is int |
||
], | ||
) | ||
@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 | ||
], | ||
) | ||
def test_logaddexp_basic(self, dtype, input1, input2, ie_device, precision, ir_version): | ||
self._test( | ||
*self.create_model(dtype), | ||
ie_device, | ||
precision, | ||
ir_version, | ||
kwargs_to_prepare_input={"input1": input1, "input2": input2, "dtype": dtype}, | ||
rtol=1e-5 # Relative tolerance for floating point comparisons | ||
) | ||
|
||
@pytest.mark.nightly | ||
@pytest.mark.precommit | ||
@pytest.mark.parametrize( | ||
"dtype", | ||
[ | ||
"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 | ||
], | ||
) | ||
def test_logaddexp_shapes(self, dtype, shape, 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(dtype), | ||
ie_device, | ||
precision, | ||
ir_version, | ||
kwargs_to_prepare_input={"input1": input1, "input2": input2, "dtype": dtype}, | ||
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("float32"), | ||
ie_device, | ||
precision, | ||
ir_version, | ||
kwargs_to_prepare_input={"input1": input1, "input2": input2, "dtype": "float32"}, | ||
rtol=1e-5 | ||
) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will promote inputs to the common type, since
torch.logaddexp
require one of the inputs to be of floating type, the common type will be floating type.