Skip to content

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

[PyTorch FE] Support aten::hypot operator (#37825)

Cekaru147beec5a446
diff --git a/src/frontends/pytorch/src/op/hypot.cpp b/src/frontends/pytorch/src/op/hypot.cpp
new file mode 100644
index 00000000000..6489bf57284
--- /dev/null
+++ b/src/frontends/pytorch/src/op/hypot.cpp
@@ -0,0 +1,76 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include <limits>
+
+#include "openvino/frontend/pytorch/node_context.hpp"
+#include "openvino/op/abs.hpp"
+#include "openvino/op/add.hpp"
+#include "openvino/op/constant.hpp"
+#include "openvino/op/convert_like.hpp"
+#include "openvino/op/divide.hpp"
+#include "openvino/op/equal.hpp"
+#include "openvino/op/is_inf.hpp"
+#include "openvino/op/is_nan.hpp"
+#include "openvino/op/logical_or.hpp"
+#include "openvino/op/maximum.hpp"
+#include "openvino/op/minimum.hpp"
+#include "openvino/op/multiply.hpp"
+#include "openvino/op/select.hpp"
+#include "openvino/op/sqrt.hpp"
+#include "utils.hpp"
+
+namespace ov::frontend::pytorch::op {
+
+using namespace ov::op;
+
+OutputVector translate_hypot(const NodeContext& context) {
+ // hypot(input, other, *) -> Tensor : elementwise sqrt(input^2 + other^2), with broadcasting.
+ num_inputs_check(context, 2, 2);
+ Output<Node> lhs;
+ Output<Node> rhs;
+ std::tie(lhs, rhs) = get_inputs_with_promoted_types(context, 0, 1);
+
+ // Squaring the inputs directly overflows (or underflows to zero) long before the result does,
+ // so compute it as max * sqrt(1 + (min / max)^2), where min / max is in [0, 1].
+ const auto lhs_abs = context.mark_node(std::make_shared<v0::Abs>(lhs));
+ const auto rhs_abs = context.mark_node(std::make_shared<v0::Abs>(rhs));
+ const auto max_abs = context.mark_node(std::make_shared<v1::Maximum>(lhs_abs, rhs_abs));
+ const auto min_abs = context.mark_node(std::make_shared<v1::Minimum>(lhs_abs, rhs_abs));
+
+ auto zero = context.mark_node(v0::Constant::create(element::f32, Shape{}, {0}));
+ zero = context.mark_node(std::make_shared<v1::ConvertLike>(zero, lhs));
+ auto one = context.mark_node(v0::Constant::create(element::f32, Shape{}, {1}));
+ one = context.mark_node(std::make_shared<v1::ConvertLike>(one, lhs));
+ auto inf = context.mark_node(v0::Constant::create(element::f32, Shape{}, {std::numeric_limits<float>::infinity()}));
+ inf = context.mark_node(std::make_shared<v1::ConvertLike>(inf, lhs));
+ auto nan =
+ context.mark_node(v0::Constant::create(element::f32, Shape{}, {std::numeric_limits<float>::quiet_NaN()}));
+ nan = context.mark_node(std::make_shared<v1::ConvertLike>(nan, lhs));
+
+ // Both inputs zero: divide by 1 instead of 0, the result is then 0 * sqrt(1) = 0.
+ const auto max_is_zero = context.mark_node(std::make_shared<v1::Equal>(max_abs, zero));
+ const auto safe_max = context.mark_node(std::make_shared<v1::Select>(max_is_zero, one, max_abs));
+ const auto ratio = context.mark_node(std::make_shared<v1::Divide>(min_abs, safe_max));
+ const auto ratio_squared = context.mark_node(std::make_shared<v1::Multiply>(ratio, ratio));
+ const auto one_plus = context.mark_node(std::make_shared<v1::Add>(one, ratio_squared));
+ const auto root = context.mark_node(std::make_shared<v0::Sqrt>(one_plus));
+ Output<Node> result = context.mark_node(std::make_shared<v1::Multiply>(max_abs, root));
+
+ // Maximum/Minimum do not reliably propagate NaN, so restore it explicitly.
+ const auto lhs_is_nan = context.mark_node(std::make_shared<v10::IsNaN>(lhs));
+ const auto rhs_is_nan = context.mark_node(std::make_shared<v10::IsNaN>(rhs));
+ const auto any_nan = context.mark_node(std::make_shared<v1::LogicalOr>(lhs_is_nan, rhs_is_nan));
+ result = context.mark_node(std::make_shared<v1::Select>(any_nan, nan, result));
+
+ // As in std::hypot, an infinite input gives +inf even when the other input is NaN.
+ const auto lhs_is_inf = context.mark_node(std::make_shared<v10::IsInf>(lhs));
+ const auto rhs_is_inf = context.mark_node(std::make_shared<v10::IsInf>(rhs));
+ const auto any_inf = context.mark_node(std::make_shared<v1::LogicalOr>(lhs_is_inf, rhs_is_inf));
+ result = context.mark_node(std::make_shared<v1::Select>(any_inf, inf, result));
+
+ return {result};
+}
+
+} // namespace ov::frontend::pytorch::op
diff --git a/src/frontends/pytorch/src/op_table.cpp b/src/frontends/pytorch/src/op_table.cpp
index 5ab5a7931a0..df7d779d14b 100644
--- a/src/frontends/pytorch/src/op_table.cpp
+++ b/src/frontends/pytorch/src/op_table.cpp
@@ -132,6 +132,7 @@ OP_CONVERTER(translate_gru);
OP_CONVERTER(translate_hann_window);
OP_CONVERTER(translate_hardtanh);
OP_CONVERTER(translate_hstack);
+OP_CONVERTER(translate_hypot);
OP_CONVERTER(translate_if);
OP_CONVERTER(translate_cond_fx);
OP_CONVERTER(translate_im2col);
@@ -571,6 +572,7 @@ const std::unordered_map<std::string, CreatorFunction> get_supported_ops_ts() {
{"aten::hardswish", op::quantizable_op<op::translate_1to1_match_1_inputs<opset10::HSwish>>},
{"aten::hardtanh", op::quantizable_op<op::translate_hardtanh>},
{"aten::hstack", op::translate_hstack},
+ {"aten::hypot", op::translate_hypot},
{"aten::im2col", op::translate_im2col},
{"aten::imag", common_translators::translate_imag},
// aten::index - Supported in limited set of patterns
diff --git a/tests/layer_tests/pytorch_tests/test_hypot.py b/tests/layer_tests/pytorch_tests/test_hypot.py
new file mode 100644
index 00000000000..8f917d02819
--- /dev/null
+++ b/tests/layer_tests/pytorch_tests/test_hypot.py
@@ -0,0 +1,158 @@
+# Copyright (C) 2018-2026 Intel Corporation
+# SPDX-License-Identifier: Apache-2.0
+
+import numpy as np
+import pytest
+import torch
+
+from pytorch_layer_test_class import PytorchLayerTest
+
+
[email protected]("input_shape_rhs", [
+ [2, 5, 3, 4],
+ [1, 5, 3, 4],
+ [1]
+])
+class TestHypot(PytorchLayerTest):
+
+ def _prepare_input(self):
+ return (self.random.randn(2, 5, 3, 4), self.input_rhs)
+
+ def create_model(self):
+
+ class aten_hypot(torch.nn.Module):
+ def forward(self, lhs, rhs):
+ return torch.hypot(lhs, rhs)
+
+ return aten_hypot(), "aten::hypot"
+
+ @pytest.mark.nightly
+ @pytest.mark.precommit
+ @pytest.mark.precommit_torch_export
+ @pytest.mark.precommit_fx_backend
+ def test_hypot(self, ie_device, precision, ir_version, input_shape_rhs):
+ self.input_rhs = self.random.randn(*input_shape_rhs)
+ self._test(*self.create_model(), ie_device, precision, ir_version,
+ use_convert_model=True, fx_kind="aten.hypot")
+
+
+class TestHypotTypes(PytorchLayerTest):
+
+ def _prepare_input(self):
+ return (self.random.randn(*self.lhs_shape, dtype=self.lhs_type),
+ self.random.randn(*self.rhs_shape, dtype=self.rhs_type))
+
+ def create_model(self, lhs_type, rhs_type):
+
+ class aten_hypot(torch.nn.Module):
+ def __init__(self, lhs_type, rhs_type):
+ super().__init__()
+ self.lhs_type = lhs_type
+ self.rhs_type = rhs_type
+
+ def forward(self, lhs, rhs):
+ return torch.hypot(lhs.to(self.lhs_type), rhs.to(self.rhs_type))
+
+ return aten_hypot(lhs_type, rhs_type), "aten::hypot"
+
+ @pytest.mark.parametrize(("lhs_type", "rhs_type"),
+ [[torch.float16, torch.float32],
+ [torch.float32, torch.float64],
+ [torch.float16, torch.float64],
+ [torch.float32, torch.float32]])
+ @pytest.mark.parametrize(("lhs_shape", "rhs_shape"), [([2, 3], [2, 3]),
+ ([2, 3], [1, 3]),
+ ([3, 2, 3], [2, 3])])
+ @pytest.mark.nightly
+ @pytest.mark.precommit
+ @pytest.mark.precommit_torch_export
+ def test_hypot_types(self, ie_device, precision, ir_version, lhs_type, lhs_shape, rhs_type, rhs_shape):
+ self.lhs_type = lhs_type
+ self.lhs_shape = lhs_shape
+ self.rhs_type = rhs_type
+ self.rhs_shape = rhs_shape
+ self._test(*self.create_model(lhs_type, rhs_type),
+ ie_device, precision, ir_version, freeze_model=False, trace_model=True, fx_kind="aten.hypot")
+
+
+class TestHypotValues(PytorchLayerTest):
+ # Pythagorean triples plus negative and zero legs give exact, easy-to-verify results:
+ # (3,4)->5, (5,12)->13, (8,15)->17, (-6,8)->10, (0,0)->0.
+ def _prepare_input(self):
+ return (np.array([3.0, 5.0, 8.0, -6.0, 0.0], dtype=np.float32),
+ np.array([4.0, 12.0, 15.0, 8.0, 0.0], dtype=np.float32))
+
+ def create_model(self):
+
+ class aten_hypot(torch.nn.Module):
+ def forward(self, lhs, rhs):
+ return torch.hypot(lhs, rhs)
+
+ return aten_hypot(), "aten::hypot"
+
+ @pytest.mark.nightly
+ @pytest.mark.precommit
+ @pytest.mark.precommit_torch_export
+ def test_hypot_values(self, ie_device, precision, ir_version):
+ self._test(*self.create_model(), ie_device, precision, ir_version, fx_kind="aten.hypot")
+
+
+class TestHypotExtremeValues(PytorchLayerTest):
+ # Inputs whose squares overflow to inf or underflow to 0 in f32, while hypot itself is
+ # representable. The result is divided by max(|input|, |other|) so every finite output is
+ # in [1, sqrt(2)] and the absolute tolerance of the comparison cannot hide an inf or a 0.
+ def _prepare_input(self):
+ lhs = np.array(self.lhs, dtype=np.float32)
+ rhs = np.array(self.rhs, dtype=np.float32)
+ norm = np.maximum(np.abs(lhs), np.abs(rhs))
+ norm = np.where(np.isfinite(norm) & (norm > 0), norm, 1).astype(np.float32)
+ return (lhs, rhs, norm)
+
+ def create_model(self):
+
+ class aten_hypot(torch.nn.Module):
+ def forward(self, lhs, rhs, norm):
+ return torch.hypot(lhs, rhs) / norm
+
+ return aten_hypot(), "aten::hypot"
+
+ @pytest.mark.parametrize(("lhs", "rhs"), [
+ # squares overflow f32
+ ([1e20, 3e19, -1e30, 1e38, 1e38], [1e20, 4e19, 1e30, 1e38, 1.0]),
+ # squares underflow to 0 in f32
+ ([1e-25, 3e-30, -1e-37, 1e-30], [1e-25, 4e-30, 1e-37, 0.0]),
+ # inf dominates nan, nan propagates otherwise
+ ([np.inf, -np.inf, np.nan, np.nan, np.inf, 3.0, 0.0], [np.nan, 5.0, -np.inf, 2.0, np.inf, np.nan, 0.0]),
+ ])
+ @pytest.mark.nightly
+ @pytest.mark.precommit
+ @pytest.mark.precommit_torch_export
+ def test_hypot_extreme_values(self, ie_device, precision, ir_version, lhs, rhs):
+ if precision == "FP16":
+ pytest.skip("Values exceed the f16 range")
+ self.lhs = lhs
+ self.rhs = rhs
+ self._test(*self.create_model(), ie_device, precision, ir_version, fx_kind="aten.hypot")
+
+
+class TestHypotF16Range(PytorchLayerTest):
+ # In f16 (max 65504) the squares of inputs above ~256 overflow, though hypot itself fits.
+ def _prepare_input(self):
+ return (np.array([300.0, -1000.0, 255.0, 40000.0, 3.0], dtype=np.float32),
+ np.array([400.0, 0.5, 256.0, 30000.0, 4.0], dtype=np.float32))
+
+ def create_model(self):
+
+ class aten_hypot(torch.nn.Module):
+ def forward(self, lhs, rhs):
+ return torch.hypot(lhs.to(torch.float16), rhs.to(torch.float16))
+
+ return aten_hypot(), "aten::hypot"
+
+ @pytest.mark.nightly
+ @pytest.mark.precommit
+ @pytest.mark.precommit_torch_export
+ def test_hypot_f16_range(self, ie_device, precision, ir_version):
+ # f16 resolution is ~1e-3 relative, results may differ from torch by an ulp.
+ self._test(*self.create_model(), ie_device, precision, ir_version,
+ freeze_model=False, trace_model=True, fx_kind="aten.hypot", custom_eps=1e-2)