Skip to content

Commit 143d386

Browse files
authored
[API Compatiblity] Support paddle.exp, paddle.expm1, paddle.diagonal, paddle.round (#76106)
* add api sink c++ * fix and add UT * add round * remove round * fix
1 parent 6ab41eb commit 143d386

File tree

7 files changed

+343
-293
lines changed

7 files changed

+343
-293
lines changed

paddle/phi/ops/yaml/python_api_info.yaml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@
1212
name : [paddle.matmul,paddle.Tensor.matmul]
1313
args_alias :
1414
use_default_mapping : True
15+
1516
- op : multiply
1617
name : [paddle.multiply,paddle.Tensor.multiply]
1718
args_alias :
1819
use_default_mapping : True
20+
1921
- op : log2
2022
name : [paddle.log2,paddle.Tensor.log2]
2123
args_alias :
@@ -88,6 +90,7 @@
8890
name : [paddle.all,paddle.Tensor.all]
8991
args_alias:
9092
use_default_mapping : True
93+
9194
- op : bmm
9295
name : [paddle.bmm, paddle.Tensor.bmm]
9396
args_alias:
@@ -194,6 +197,28 @@
194197
args_mapper :
195198
func : ArgSumMapper
196199

200+
- op : exp
201+
name : [paddle.exp, paddle.Tensor.exp]
202+
args_alias:
203+
use_default_mapping : True
204+
205+
- op : expm1
206+
name : [paddle.expm1, paddle.Tensor.expm1]
207+
args_alias:
208+
use_default_mapping : True
209+
210+
- op : diagonal
211+
name : [paddle.diagonal, paddle.Tensor.diagonal]
212+
args_alias:
213+
x : [input]
214+
axis1 : [dim1]
215+
axis2 : [dim2]
216+
217+
- op : round
218+
name : [paddle.round, paddle.Tensor.round]
219+
args_alias:
220+
use_default_mapping : True
221+
197222
- op : abs
198223
name : [paddle.abs, paddle.Tensor.abs]
199224
args_alias:

python/paddle/_paddle_docs.py

Lines changed: 206 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2314,6 +2314,212 @@ def dot(
23142314
""",
23152315
)
23162316

2317+
add_doc_and_signature(
2318+
"exp",
2319+
"""
2320+
2321+
Computes exp of x element-wise with a natural number `e` as the base.
2322+
2323+
.. math::
2324+
out = e^x
2325+
2326+
.. note::
2327+
Alias Support:
2328+
1. The parameter name ``input`` can be used as an alias for ``x``.
2329+
2330+
Args:
2331+
x (Tensor): Input of Exp operator, an N-D Tensor, with data type int32, int64, bfloat16, float16, float32, float64, complex64 or complex128.
2332+
Alias: ``input``.
2333+
name (str|None, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.
2334+
out (Tensor|None, optional): The output tensor.
2335+
2336+
Returns:
2337+
Tensor. Output of Exp operator, a Tensor with shape same as input.
2338+
2339+
Examples:
2340+
.. code-block:: python
2341+
2342+
>>> import paddle
2343+
2344+
>>> x = paddle.to_tensor([-0.4, -0.2, 0.1, 0.3])
2345+
>>> out = paddle.exp(x)
2346+
>>> print(out)
2347+
Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
2348+
[0.67032003, 0.81873077, 1.10517097, 1.34985888])
2349+
""",
2350+
"""
2351+
def exp(
2352+
x: Tensor, *, out: Tensor | None = None, name: str | None = None
2353+
) -> Tensor
2354+
""",
2355+
)
2356+
2357+
add_doc_and_signature(
2358+
"expm1",
2359+
"""
2360+
2361+
Expm1 Operator. Computes expm1 of x element-wise with a natural number :math:`e` as the base.
2362+
2363+
.. math::
2364+
out = e^x - 1
2365+
2366+
.. note::
2367+
Alias Support:
2368+
1. The parameter name ``input`` can be used as an alias for ``x``.
2369+
2370+
Args:
2371+
x (Tensor): Input of Expm1 operator, an N-D Tensor, with data type int32, int64, bfloat16, float16, float32, float64, complex64 or complex128.
2372+
Alias: ``input``.
2373+
name (str|None, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.
2374+
out (Tensor|None, optional): The output tensor.
2375+
2376+
Returns:
2377+
Tensor. Output of Expm1 operator, a Tensor with shape same as input.
2378+
2379+
Examples:
2380+
.. code-block:: python
2381+
2382+
>>> import paddle
2383+
2384+
>>> x = paddle.to_tensor([-0.4, -0.2, 0.1, 0.3])
2385+
>>> out = paddle.expm1(x)
2386+
>>> print(out)
2387+
Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
2388+
[-0.32967997, -0.18126924, 0.10517092, 0.34985882])
2389+
""",
2390+
"""
2391+
def expm1(
2392+
x: Tensor, *, out: Tensor | None = None, name: str | None = None
2393+
) -> Tensor
2394+
""",
2395+
)
2396+
2397+
add_doc_and_signature(
2398+
"diagonal",
2399+
"""
2400+
2401+
Computes the diagonals of the input tensor x.
2402+
2403+
If ``x`` is 2D, returns the diagonal.
2404+
If ``x`` has larger dimensions, diagonals be taken from the 2D planes specified by axis1 and axis2.
2405+
By default, the 2D planes formed by the first and second axis of the input tensor x.
2406+
2407+
The argument ``offset`` determines where diagonals are taken from input tensor x:
2408+
2409+
- If offset = 0, it is the main diagonal.
2410+
- If offset > 0, it is above the main diagonal.
2411+
- If offset < 0, it is below the main diagonal.
2412+
2413+
.. note::
2414+
Alias Support:
2415+
1. The parameter name ``input`` can be used as an alias for ``x``.
2416+
2. The parameter name ``dim1`` can be used as an alias for ``axis1``.
2417+
3. The parameter name ``dim2`` can be used as an alias for ``axis2``.
2418+
2419+
Args:
2420+
x (Tensor): The input tensor x. Must be at least 2-dimensional. The input data type should be bool, int32,
2421+
int64, bfloat16, float16, float32, float64. Alias: ``input``.
2422+
offset (int, optional): Which diagonals in input tensor x will be taken. Default: 0 (main diagonals).
2423+
axis1 (int, optional): The first axis with respect to take diagonal. Default: 0. Alias: ``dim1``.
2424+
axis2 (int, optional): The second axis with respect to take diagonal. Default: 1. Alias: ``dim2``.
2425+
name (str|None, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.
2426+
2427+
Returns:
2428+
Tensor: a partial view of input tensor in specify two dimensions, the output data type is the same as input data type.
2429+
2430+
Examples:
2431+
.. code-block:: python
2432+
2433+
>>> import paddle
2434+
2435+
>>> paddle.seed(2023)
2436+
>>> x = paddle.rand([2, 2, 3],'float32')
2437+
>>> print(x)
2438+
Tensor(shape=[2, 2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
2439+
[[[0.86583614, 0.52014720, 0.25960937],
2440+
[0.90525323, 0.42400089, 0.40641287]],
2441+
[[0.97020894, 0.74437362, 0.51785129],
2442+
[0.73292869, 0.97786582, 0.04315904]]])
2443+
2444+
>>> out1 = paddle.diagonal(x)
2445+
>>> print(out1)
2446+
Tensor(shape=[3, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
2447+
[[0.86583614, 0.73292869],
2448+
[0.52014720, 0.97786582],
2449+
[0.25960937, 0.04315904]])
2450+
2451+
>>> out2 = paddle.diagonal(x, offset=0, axis1=2, axis2=1)
2452+
>>> print(out2)
2453+
Tensor(shape=[2, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
2454+
[[0.86583614, 0.42400089],
2455+
[0.97020894, 0.97786582]])
2456+
2457+
>>> out3 = paddle.diagonal(x, offset=1, axis1=0, axis2=1)
2458+
>>> print(out3)
2459+
Tensor(shape=[3, 1], dtype=float32, place=Place(cpu), stop_gradient=True,
2460+
[[0.90525323],
2461+
[0.42400089],
2462+
[0.40641287]])
2463+
2464+
>>> out4 = paddle.diagonal(x, offset=0, axis1=1, axis2=2)
2465+
>>> print(out4)
2466+
Tensor(shape=[2, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
2467+
[[0.86583614, 0.42400089],
2468+
[0.97020894, 0.97786582]])
2469+
""",
2470+
"""
2471+
def diagonal(
2472+
x: Tensor,
2473+
offset: int = 0,
2474+
axis1: int = 0,
2475+
axis2: int = 1,
2476+
name: str | None = None,
2477+
) -> Tensor
2478+
""",
2479+
)
2480+
2481+
add_doc_and_signature(
2482+
"round",
2483+
"""
2484+
2485+
Round the values in the input to the nearest integer value.
2486+
2487+
.. code-block:: text
2488+
2489+
input:
2490+
x.shape = [4]
2491+
x.data = [1.2, -0.9, 3.4, 0.9]
2492+
2493+
output:
2494+
out.shape = [4]
2495+
out.data = [1., -1., 3., 1.]
2496+
2497+
Args:
2498+
x (Tensor): Input of Round operator, an N-D Tensor, with data type bfloat16, int32, int64, float32, float64, float16, complex64 or complex128.
2499+
decimals(int): Rounded decimal place (default: 0).
2500+
name (str|None, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.
2501+
2502+
Returns:
2503+
Tensor. Output of Round operator, a Tensor with shape same as input.
2504+
2505+
Examples:
2506+
.. code-block:: python
2507+
2508+
>>> import paddle
2509+
2510+
>>> x = paddle.to_tensor([-0.5, -0.2, 0.6, 1.5])
2511+
>>> out = paddle.round(x)
2512+
>>> print(out)
2513+
Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
2514+
[-0., -0., 1., 2.])
2515+
""",
2516+
"""
2517+
def round(
2518+
x: Tensor, decimals = 0, *, out: Tensor | None = None, name: str | None = None,
2519+
) -> Tensor
2520+
""",
2521+
)
2522+
23172523
add_doc_and_signature(
23182524
"abs",
23192525
"""
@@ -2350,7 +2556,6 @@ def abs(
23502556
) -> Tensor
23512557
""",
23522558
)
2353-
23542559
# lubingxin
23552560

23562561
# chenhuangrun

python/paddle/special.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414

1515
from .tensor.compat_softmax import softmax
1616
from .tensor.math import logsumexp
17+
from .tensor.ops import expm1
1718

1819
__all__ = [
1920
"logsumexp",
2021
"softmax",
22+
"expm1",
2123
]

0 commit comments

Comments
 (0)