Skip to content
23 changes: 19 additions & 4 deletions python/paddle/tensor/math.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
sign,
sin,
sum,
tan,
tanh,
)
from paddle.base.libpaddle import DataType
Expand Down Expand Up @@ -123,7 +124,6 @@
sqrt_,
square,
square_,
tan,
tan_,
)

Expand Down Expand Up @@ -5041,7 +5041,10 @@ def rad2deg(x: Tensor, name: str | None = None) -> Tensor:
return out


def deg2rad(x: Tensor, name: str | None = None) -> Tensor:
@param_one_alias(['x', 'input'])
def deg2rad(
x: Tensor, name: str | None = None, *, out: Tensor | None = None
) -> Tensor:
r"""
Convert each of the elements of input x from degrees to angles in radians.

Expand All @@ -5052,6 +5055,7 @@ def deg2rad(x: Tensor, name: str | None = None) -> Tensor:
Args:
x (Tensor): An N-D Tensor, the data type is float32, float64, int32, int64.
name (str|None, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.
out (Tensor|None, optional): The output Tensor. If set, the result will be stored in this Tensor. Default is None.

Returns:
out (Tensor): An N-D Tensor, the shape and data type is the same with input (The output data type is float32 when the input data type is int).
Expand All @@ -5078,7 +5082,15 @@ def deg2rad(x: Tensor, name: str | None = None) -> Tensor:
if in_dynamic_or_pir_mode():
if convert_dtype(x.dtype) in ['int32', 'int64']:
x = cast(x, dtype="float32")
return _C_ops.scale(x, deg2rad_scale, 0.0, True)
if in_pir_mode():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不用区分pir了,dygraph测out就行。

这里直接:return _C_ops.scale(x, deg2rad_scale, 0.0, True, out=out)

res = _C_ops.scale(x, deg2rad_scale, 0.0, True)
if out is not None:
paddle.assign(res, out)
return out
return res

res = _C_ops.scale(x, deg2rad_scale, 0.0, True, out=out)
return out if out is not None else res
else:
check_variable_and_dtype(
x, 'x', ['int32', 'int64', 'float32', 'float64'], 'deg2rad'
Expand All @@ -5095,7 +5107,10 @@ def deg2rad(x: Tensor, name: str | None = None) -> Tensor:
outputs={'Out': out_cast},
attrs={'in_dtype': x.dtype, 'out_dtype': paddle.float32},
)
out = helper.create_variable_for_type_inference(dtype=out_cast.dtype)
if out is None:
out = helper.create_variable_for_type_inference(
dtype=out_cast.dtype
)
helper.append_op(
type='scale',
inputs={'X': out_cast},
Expand Down
1 change: 1 addition & 0 deletions test/legacy_test/test_activation_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -6377,6 +6377,7 @@ class TestActivationAPI_Compatibility(unittest.TestCase):
("paddle.tanh", np.tanh, {'min_val': -1.0, 'max_val': 1.0}),
("paddle.cosh", np.cosh, {'min_val': -1.0, 'max_val': 1.0}),
("paddle.sinh", np.sinh, {'min_val': -1.0, 'max_val': 1.0}),
("paddle.tan", np.tan, {'min_val': -1.0, 'max_val': 1.0}),
]
ACTIVATION_NOT_METHOD_CONFIGS = [
(
Expand Down
32 changes: 30 additions & 2 deletions test/legacy_test/test_deg2rad.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import unittest

os.environ['FLAGS_enable_pir_api'] = '0'

import numpy as np
from op_test import get_device_place

Expand Down Expand Up @@ -63,20 +66,45 @@ def test_dygraph(self):
class TestDeg2radAPI2(TestDeg2radAPI):
# Test input data type is int
def setUp(self):
self.x_np = [180]
self.x_np = [180.0]
self.x_shape = [1]
self.out_np = np.pi
self.x_dtype = 'int64'

def test_dygraph(self):
paddle.disable_static()

x2 = paddle.to_tensor([180])
x2 = paddle.to_tensor([180.0])
result2 = paddle.deg2rad(x2)
np.testing.assert_allclose(np.pi, result2.numpy(), rtol=1e-05)

paddle.enable_static()


class TestDeg2radAliasAndOut(unittest.TestCase):
def test_alias_and_out(self):
paddle.disable_static()
x = paddle.to_tensor([180.0])
expected = np.deg2rad(180.0)

# Test alias
res = paddle.deg2rad(input=x)
np.testing.assert_allclose(res.numpy(), expected, rtol=1e-05)

# Test out
out = paddle.zeros([1], dtype="float32")
res = paddle.deg2rad(x, out=out)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PIR无需测out

np.testing.assert_allclose(out.numpy(), expected, rtol=1e-05)
self.assertTrue(res is out)

# Test int input with out
x_int = paddle.to_tensor([180.0])
out_float = paddle.zeros([1], dtype="float32")
res = paddle.deg2rad(x_int, out=out_float)
np.testing.assert_allclose(out_float.numpy(), expected, rtol=1e-05)

paddle.enable_static()


if __name__ == '__main__':
unittest.main()
Loading