Skip to content
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

Rad2deg #29352

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
42 changes: 42 additions & 0 deletions src/frontends/pytorch/src/op/rad2deg.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (C) 2018-2025 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

#include <cmath>

#include "openvino/frontend/pytorch/node_context.hpp"
#include "openvino/op/constant.hpp"
#include "openvino/op/multiply.hpp"
#include "utils.hpp"

namespace ov {
namespace frontend {
namespace pytorch {
namespace op {

OutputVector translate_rad2deg(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);

// Get the input element type dynamically
auto input_type = input.get_element_type();

const double pi_val = std::atan(1.0) * 4;

// Create a constant node with the conversion factor (180 / π)
auto conversion_factor = context.mark_node(ov::op::v0::Constant::create(input_type, Shape{}, {180.0 / pi_val}));

// Apply the multiplication operation to convert radians to degrees
auto result = context.mark_node(std::make_shared<ov::op::v1::Multiply>(input, conversion_factor));

// Return the computed result as an OutputVector
return {result};
}

} // namespace op
} // namespace pytorch
} // namespace frontend
} // namespace ov
2 changes: 2 additions & 0 deletions src/frontends/pytorch/src/op_table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ OP_CONVERTER(translate_quantized_add_relu);
OP_CONVERTER(translate_quantized_hardswish);
OP_CONVERTER(translate_quantized_mul);
OP_CONVERTER(translate_range_length);
OP_CONVERTER(translate_rad2deg);
OP_CONVERTER(translate_rand);
OP_CONVERTER(translate_randn);
OP_CONVERTER(translate_randint);
Expand Down Expand Up @@ -640,6 +641,7 @@ const std::unordered_map<std::string, CreatorFunction> get_supported_ops_ts() {
{"aten::prod", op::translate_prod},
{"aten::quantize_per_channel", op::translate_quantize_per_channel},
{"aten::quantize_per_tensor", op::translate_quantize_per_tensor},
{"aten::rad2deg", op::translate_rad2deg},
{"aten::rand", op::translate_rand},
{"aten::rand_like", op::translate_rand_like},
{"aten::randint", op::translate_randint},
Expand Down
26 changes: 26 additions & 0 deletions tests/layer_tests/pytorch_tests/test_rad2deg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Copyright (C) 2018-2025 Intel Corporation
# SPDX-License-Identifier: Apache-2.0

import pytest
import torch
import numpy as np
from pytorch_layer_test_class import PytorchLayerTest


class TestRad2Deg(PytorchLayerTest):
def _prepare_input(self, input_shape=(2, 2)):
return (np.random.uniform(-np.pi, np.pi, size=input_shape).astype(np.float32),)

def create_model(self):
class aten_rad2deg(torch.nn.Module):
def forward(self, x):
return x * (180.0 / np.pi)
ref_net = None
return aten_rad2deg(), ref_net, "aten::mul"

@pytest.mark.parametrize("input_shape", [(2, 2), (3, 3), (4, 5)])
@pytest.mark.nightly
@pytest.mark.precommit
def test_rad2deg(self, input_shape, ie_device, precision, ir_version):
self._test(*self.create_model(), ie_device, precision, ir_version,
kwargs_to_prepare_input={"input_shape": input_shape})
Loading