Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 5 additions & 0 deletions paddle/phi/ops/yaml/python_api_info.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,11 @@
args_mapper :
func : ArgSumMapper

- op : tan
Copy link
Contributor

Choose a reason for hiding this comment

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

tan已经下沉了,这个PR不用再改了。只用修改deg2rad即可。

name : [paddle.tan, paddle.Tensor.tan]
args_alias:
use_default_mapping : True

- op : tanh
name : [paddle.tanh, paddle.Tensor.tanh, paddle.nn.functional.tanh]
args_alias:
Expand Down
40 changes: 40 additions & 0 deletions python/paddle/_paddle_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3413,3 +3413,43 @@ def baddbmm(
) -> Tensor
""",
)


add_doc_and_signature(
"tan",
"""
Tangent Operator. Computes tangent of x element-wise.

Input range is `(k*pi-pi/2, k*pi+pi/2)` and output range is `(-inf, inf)`.

.. math::
out = tan(x)

Args:
x (Tensor): Input of Tan operator, an N-D Tensor, with data type float32, float64, float16,
bfloat16, uint8, int8, int16, int32, int64, complex64 or complex128.
Alias: ``input``.
name (str|None, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.

Returns:
Tensor. Output of Tan operator, a Tensor with shape same as input
(integer types are autocasted into float32).

Examples:
.. code-block:: python

>>> import paddle

>>> x = paddle.to_tensor([-0.4, -0.2, 0.1, 0.3])
>>> out = paddle.tan(x)
>>> print(out)
Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
[-0.42279324, -0.20271003, 0.10033467, 0.30933627])
""",
"""
def tan(
x: Tensor,
name: str | None = None,
) -> Tensor
""",
)
14 changes: 12 additions & 2 deletions python/paddle/tensor/math.py
Original file line number Diff line number Diff line change
Expand Up @@ -5219,7 +5219,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 @@ -5230,6 +5233,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 @@ -5256,6 +5260,9 @@ 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")
if out is not None:
Copy link
Contributor

Choose a reason for hiding this comment

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

_C_ops会自动处理out=None的情况,无需再额外判断,直接透传。

_C_ops.scale(x, deg2rad_scale, 0.0, True, out=out)
return out
return _C_ops.scale(x, deg2rad_scale, 0.0, True)
else:
check_variable_and_dtype(
Expand All @@ -5273,7 +5280,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
57 changes: 1 addition & 56 deletions python/paddle/tensor/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
sin,
sinh,
sqrt,
tan,
)
from paddle.utils.inplace_utils import inplace_apis_in_dygraph_only

Expand Down Expand Up @@ -307,62 +308,6 @@ def square(x: Tensor, name: str | None = None) -> Tensor:
return out


def tan(x: Tensor, name: str | None = None) -> Tensor:
"""
Tangent Operator. Computes tangent of x element-wise.

Input range is `(k*pi-pi/2, k*pi+pi/2)` and output range is `(-inf, inf)`.

.. math::
out = tan(x)

Args:
x (Tensor): Input of Tan operator, an N-D Tensor, with data type float32, float64, float16,
bfloat16, uint8, int8, int16, int32, int64, complex64 or complex128.
name (str|None, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.

Returns:
Tensor. Output of Tan operator, a Tensor with shape same as input
(integer types are autocasted into float32).

Examples:
.. code-block:: python

>>> import paddle

>>> x = paddle.to_tensor([-0.4, -0.2, 0.1, 0.3])
>>> out = paddle.tan(x)
>>> print(out)
Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
[-0.42279324, -0.20271003, 0.10033467, 0.30933627])
"""
if in_dynamic_or_pir_mode():
return _C_ops.tan(x)
else:
check_variable_and_dtype(
x,
'x',
[
'float16',
'uint16',
'float32',
'float64',
'uint8',
'int8',
'int16',
'int32',
'int64',
'complex64',
'complex128',
],
'tan',
)
helper = LayerHelper('tan', **locals())
out = helper.create_variable_for_type_inference(dtype=x.dtype)
helper.append_op(type='tan', inputs={"X": x}, outputs={"Out": out})
return out


def erf(x: Tensor, name: str | None = None) -> Tensor:
r"""
The error function.
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