From 2824fc3ce87f76df0fd66d6b4ac851c63132eda2 Mon Sep 17 00:00:00 2001 From: Muntasir Mamun Date: Sun, 25 Oct 2026 11:55:20 +0600 Subject: [PATCH] [PyTorch Frontend] Add support for aten::deg2rad operation - Implement translate_deg2rad to convert degrees to radians via multiplication by pi / 180. - Register aten::deg2rad in PyTorch frontend op table. - Add layer tests covering float32, float64, and int32 inputs. --- src/frontends/pytorch/src/op/deg2rad.cpp | 38 +++++++++++++++++++ src/frontends/pytorch/src/op_table.cpp | 2 + .../layer_tests/pytorch_tests/test_deg2rad.py | 27 +++++++++++++ 3 files changed, 67 insertions(+) create mode 100644 src/frontends/pytorch/src/op/deg2rad.cpp create mode 100644 tests/layer_tests/pytorch_tests/test_deg2rad.py diff --git a/src/frontends/pytorch/src/op/deg2rad.cpp b/src/frontends/pytorch/src/op/deg2rad.cpp new file mode 100644 index 00000000000000..940af5a0343280 --- /dev/null +++ b/src/frontends/pytorch/src/op/deg2rad.cpp @@ -0,0 +1,38 @@ +// Copyright (C) 2018-2026 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#include + +#include "openvino/frontend/pytorch/node_context.hpp" +#include "openvino/op/constant.hpp" +#include "openvino/op/multiply.hpp" +#include "utils.hpp" + +namespace ov::frontend::pytorch::op { + +OutputVector translate_deg2rad(const NodeContext& context) { + // Ensure that the operation has exactly one input (the input tensor) + num_inputs_check(context, 1, 1); + + // Retrieve the input tensor + auto input = context.get_input(0); + + // Define the value of π + const double pi_val = std::atan(1.0) * 4; + + // Create a constant node with the conversion factor (π / 180) using fp32 type + Output conversion_factor_output = context.mark_node( + ov::op::v0::Constant::create(ov::element::f32, Shape{}, {static_cast(pi_val) / 180.0f})); + + // Align elementwise input types to handle integer cases correctly + align_eltwise_input_types(context, input, conversion_factor_output, is_python_scalar_input(context, 0), true); + + // Apply the multiplication operation to convert degrees to radians + auto result = context.mark_node(std::make_shared(input, conversion_factor_output)); + + // Return the computed result as an OutputVector + 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 df7d779d14b19b..ce2acd5d405680 100644 --- a/src/frontends/pytorch/src/op_table.cpp +++ b/src/frontends/pytorch/src/op_table.cpp @@ -76,6 +76,7 @@ OP_CONVERTER(translate_copy_); OP_CONVERTER(translate_cross); OP_CONVERTER(translate_cumsum); OP_CONVERTER(translate_deform_conv); +OP_CONVERTER(translate_deg2rad); OP_CONVERTER(translate_derive_index); OP_CONVERTER(translate_dim); OP_CONVERTER(translate_div); @@ -501,6 +502,7 @@ const std::unordered_map get_supported_ops_ts() { {"aten::cosh_", op::inplace_op>}, {"aten::cross", op::translate_cross}, {"aten::cumsum", op::translate_cumsum}, + {"aten::deg2rad", op::translate_deg2rad}, {"aten::det", op::translate_linalg_det}, {"aten::detach", op::skip_node}, {"aten::dequantize", op::skip_node}, // we convert model to fp32 using FQ, so dequantization is not needed diff --git a/tests/layer_tests/pytorch_tests/test_deg2rad.py b/tests/layer_tests/pytorch_tests/test_deg2rad.py new file mode 100644 index 00000000000000..e3717688b110e2 --- /dev/null +++ b/tests/layer_tests/pytorch_tests/test_deg2rad.py @@ -0,0 +1,27 @@ +# Copyright (C) 2018-2026 Intel Corporation +# SPDX-License-Identifier: Apache-2.0 + +import pytest +import torch +import numpy as np +from pytorch_layer_test_class import PytorchLayerTest + + +class TestDeg2Rad(PytorchLayerTest): + def _prepare_input(self, input_shape=(2, 2), dtype=np.float32): + return (self.random.uniform(-360.0, 360.0, size=input_shape, dtype=dtype),) + + def create_model(self): + class aten_deg2rad(torch.nn.Module): + def forward(self, x): + return torch.deg2rad(x) + return aten_deg2rad(), "aten::deg2rad" + + @pytest.mark.parametrize("input_shape", [(3, 3)]) + @pytest.mark.parametrize("dtype", [np.float32, np.float64, np.int32]) + @pytest.mark.nightly + @pytest.mark.precommit + @pytest.mark.precommit_torch_export + def test_deg2rad(self, input_shape, dtype, ie_device, precision, ir_version): + self._test(*self.create_model(), ie_device, precision, ir_version, + kwargs_to_prepare_input={"input_shape": input_shape, "dtype": dtype})