Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions python/paddle/tensor/math.py
Original file line number Diff line number Diff line change
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,8 @@ 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)
_C_ops.scale(x, deg2rad_scale, 0.0, True, out=out)
return out
Copy link
Contributor

Choose a reason for hiding this comment

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

这个return看起来不太对?直接return _C_ops.scale(x, deg2rad_scale, 0.0, True, out=out)

else:
check_variable_and_dtype(
x, 'x', ['int32', 'int64', 'float32', 'float64'], 'deg2rad'
Expand All @@ -5095,7 +5100,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
29 changes: 27 additions & 2 deletions test/legacy_test/test_deg2rad.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,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