Skip to content

Commit c4a6db9

Browse files
authored
[API compatibility] add tril and triu out parameter (#74624)
* add tril and triu out parameter * change the place of parameter name and out * tril and triu api sink into C++ * add import tril and triu * fix testcase * fix testcase * fix testcase * fix testcase * change position * add compatibility test for tril and triu * fix conflict * fix testcase
1 parent 352419a commit c4a6db9

File tree

7 files changed

+373
-2048
lines changed

7 files changed

+373
-2048
lines changed

paddle/phi/ops/yaml/ops.yaml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5501,7 +5501,11 @@
55015501
interfaces : paddle::dialect::InferSymbolicShapeInterface
55025502

55035503
- op : tril
5504-
args : (Tensor x, int diagonal)
5504+
args : (Tensor x, int diagonal=0)
5505+
python_api :
5506+
name : [paddle.tril, paddle.Tensor.tril]
5507+
args_alias:
5508+
x : [input]
55055509
output : Tensor(out)
55065510
infer_meta :
55075511
func : TrilInferMeta
@@ -5540,7 +5544,11 @@
55405544
interfaces : paddle::dialect::InferSymbolicShapeInterface
55415545

55425546
- op : triu
5543-
args : (Tensor x, int diagonal)
5547+
args : (Tensor x, int diagonal=0)
5548+
python_api :
5549+
name : [paddle.triu, paddle.Tensor.triu]
5550+
args_alias:
5551+
x : [input]
55445552
output : Tensor(out)
55455553
infer_meta :
55465554
func : TriuInferMeta

python/paddle/_paddle_docs.py

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,150 @@ def any(
597597
# lousiyu
598598

599599
# zhengshijie
600+
add_doc_and_signature(
601+
"tril",
602+
r"""
603+
Returns the lower triangular part of a matrix (2-D tensor) or batch
604+
of matrices :attr:`x`, the other elements of the result tensor are set
605+
to 0. The lower triangular part of the matrix is defined as the elements
606+
on and below the diagonal.
607+
608+
Args:
609+
x (Tensor): The input x which is a Tensor.
610+
Support data types: ``bool``, ``float64``, ``float32``, ``int32``, ``int64``, ``complex64``, ``complex128``.
611+
diagonal (int, optional): The diagonal to consider, default value is 0.
612+
If :attr:`diagonal` = 0, all elements on and below the main diagonal are
613+
retained. A positive value includes just as many diagonals above the main
614+
diagonal, and similarly a negative value excludes just as many diagonals below
615+
the main diagonal. The main diagonal are the set of indices
616+
:math:`\{(i, i)\}` for :math:`i \in [0, \min\{d_{1}, d_{2}\} - 1]` where
617+
:math:`d_{1}, d_{2}` are the dimensions of the matrix.
618+
name(str|None, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None.
619+
out(Tensor, optional): The output tensor.
620+
621+
Returns:
622+
Tensor: Results of lower triangular operation by the specified diagonal of input tensor x,
623+
it's data type is the same as x's Tensor.
624+
625+
Examples:
626+
.. code-block:: python
627+
628+
>>> import paddle
629+
630+
>>> data = paddle.arange(1, 13, dtype="int64").reshape([3,-1])
631+
>>> print(data)
632+
Tensor(shape=[3, 4], dtype=int64, place=Place(cpu), stop_gradient=True,
633+
[[1 , 2 , 3 , 4 ],
634+
[5 , 6 , 7 , 8 ],
635+
[9 , 10, 11, 12]])
636+
637+
>>> tril1 = paddle.tril(data)
638+
>>> print(tril1)
639+
Tensor(shape=[3, 4], dtype=int64, place=Place(cpu), stop_gradient=True,
640+
[[1 , 0 , 0 , 0 ],
641+
[5 , 6 , 0 , 0 ],
642+
[9 , 10, 11, 0 ]])
643+
644+
>>> # example 2, positive diagonal value
645+
>>> tril2 = paddle.tril(data, diagonal=2)
646+
>>> print(tril2)
647+
Tensor(shape=[3, 4], dtype=int64, place=Place(cpu), stop_gradient=True,
648+
[[1 , 2 , 3 , 0 ],
649+
[5 , 6 , 7 , 8 ],
650+
[9 , 10, 11, 12]])
651+
652+
>>> # example 3, negative diagonal value
653+
>>> tril3 = paddle.tril(data, diagonal=-1)
654+
>>> print(tril3)
655+
Tensor(shape=[3, 4], dtype=int64, place=Place(cpu), stop_gradient=True,
656+
[[0 , 0 , 0 , 0 ],
657+
[5 , 0 , 0 , 0 ],
658+
[9 , 10, 0 , 0 ]])
659+
""",
660+
"""
661+
def tril(
662+
x: Tensor,
663+
diagonal: int = 0,
664+
name: str | None = None,
665+
*,
666+
out: Tensor | None = None,
667+
) -> Tensor
668+
""",
669+
)
670+
600671

672+
add_doc_and_signature(
673+
"triu",
674+
r"""
675+
Return the upper triangular part of a matrix (2-D tensor) or batch of matrices
676+
:attr:`x`, the other elements of the result tensor are set to 0.
677+
The upper triangular part of the matrix is defined as the elements on and
678+
above the diagonal.
679+
680+
Args:
681+
x (Tensor): The input x which is a Tensor.
682+
Support data types: ``float64``, ``float32``, ``int32``, ``int64``, ``complex64``, ``complex128``.
683+
diagonal (int, optional): The diagonal to consider, default value is 0.
684+
If :attr:`diagonal` = 0, all elements on and above the main diagonal are
685+
retained. A positive value excludes just as many diagonals above the main
686+
diagonal, and similarly a negative value includes just as many diagonals below
687+
the main diagonal. The main diagonal are the set of indices
688+
:math:`\{(i, i)\}` for :math:`i \in [0, \min\{d_{1}, d_{2}\} - 1]` where
689+
:math:`d_{1}, d_{2}` are the dimensions of the matrix.
690+
name(str|None, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None.
691+
out(Tensor, optional): The output tensor.
692+
693+
Returns:
694+
Tensor: Results of upper triangular operation by the specified diagonal of input tensor x,
695+
it's data type is the same as x's Tensor.
696+
697+
Examples:
698+
.. code-block:: python
699+
700+
>>> import paddle
701+
702+
>>> x = paddle.arange(1, 13, dtype="int64").reshape([3,-1])
703+
>>> print(x)
704+
Tensor(shape=[3, 4], dtype=int64, place=Place(cpu), stop_gradient=True,
705+
[[1 , 2 , 3 , 4 ],
706+
[5 , 6 , 7 , 8 ],
707+
[9 , 10, 11, 12]])
708+
709+
>>> # example 1, default diagonal
710+
>>> triu1 = paddle.tensor.triu(x)
711+
>>> print(triu1)
712+
Tensor(shape=[3, 4], dtype=int64, place=Place(cpu), stop_gradient=True,
713+
[[1 , 2 , 3 , 4 ],
714+
[0 , 6 , 7 , 8 ],
715+
[0 , 0 , 11, 12]])
716+
717+
>>> # example 2, positive diagonal value
718+
>>> triu2 = paddle.tensor.triu(x, diagonal=2)
719+
>>> print(triu2)
720+
Tensor(shape=[3, 4], dtype=int64, place=Place(cpu), stop_gradient=True,
721+
[[0, 0, 3, 4],
722+
[0, 0, 0, 8],
723+
[0, 0, 0, 0]])
724+
725+
>>> # example 3, negative diagonal value
726+
>>> triu3 = paddle.tensor.triu(x, diagonal=-1)
727+
>>> print(triu3)
728+
Tensor(shape=[3, 4], dtype=int64, place=Place(cpu), stop_gradient=True,
729+
[[1 , 2 , 3 , 4 ],
730+
[5 , 6 , 7 , 8 ],
731+
[0 , 10, 11, 12]])
732+
733+
""",
734+
"""
735+
def triu(
736+
x: Tensor,
737+
diagonal: int = 0,
738+
name: str | None = None,
739+
*,
740+
out: Tensor | None = None,
741+
) -> Tensor
742+
""",
743+
)
601744
# lihaoyang
602745

603746
# lubingxin

python/paddle/tensor/creation.py

Lines changed: 1 addition & 182 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import paddle
2727
from paddle import _C_ops
28+
from paddle._C_ops import tril, triu # noqa: F401
2829
from paddle.utils import deprecated
2930
from paddle.utils.decorator_utils import (
3031
ParamAliasDecorator,
@@ -2269,96 +2270,6 @@ def _tril_triu_op(helper: LayerHelper) -> paddle.Tensor:
22692270
return out
22702271

22712272

2272-
def tril(
2273-
x: paddle.Tensor, diagonal: int = 0, name: str | None = None
2274-
) -> paddle.Tensor:
2275-
r"""
2276-
Returns the lower triangular part of a matrix (2-D tensor) or batch
2277-
of matrices :attr:`x`, the other elements of the result tensor are set
2278-
to 0. The lower triangular part of the matrix is defined as the elements
2279-
on and below the diagonal.
2280-
2281-
Args:
2282-
x (Tensor): The input x which is a Tensor.
2283-
Support data types: ``bool``, ``float64``, ``float32``, ``int32``, ``int64``, ``complex64``, ``complex128``.
2284-
diagonal (int, optional): The diagonal to consider, default value is 0.
2285-
If :attr:`diagonal` = 0, all elements on and below the main diagonal are
2286-
retained. A positive value includes just as many diagonals above the main
2287-
diagonal, and similarly a negative value excludes just as many diagonals below
2288-
the main diagonal. The main diagonal are the set of indices
2289-
:math:`\{(i, i)\}` for :math:`i \in [0, \min\{d_{1}, d_{2}\} - 1]` where
2290-
:math:`d_{1}, d_{2}` are the dimensions of the matrix.
2291-
name(str|None, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None.
2292-
2293-
Returns:
2294-
Tensor: Results of lower triangular operation by the specified diagonal of input tensor x,
2295-
it's data type is the same as x's Tensor.
2296-
2297-
Examples:
2298-
.. code-block:: python
2299-
2300-
>>> import paddle
2301-
2302-
>>> data = paddle.arange(1, 13, dtype="int64").reshape([3,-1])
2303-
>>> print(data)
2304-
Tensor(shape=[3, 4], dtype=int64, place=Place(cpu), stop_gradient=True,
2305-
[[1 , 2 , 3 , 4 ],
2306-
[5 , 6 , 7 , 8 ],
2307-
[9 , 10, 11, 12]])
2308-
2309-
>>> tril1 = paddle.tril(data)
2310-
>>> print(tril1)
2311-
Tensor(shape=[3, 4], dtype=int64, place=Place(cpu), stop_gradient=True,
2312-
[[1 , 0 , 0 , 0 ],
2313-
[5 , 6 , 0 , 0 ],
2314-
[9 , 10, 11, 0 ]])
2315-
2316-
>>> # example 2, positive diagonal value
2317-
>>> tril2 = paddle.tril(data, diagonal=2)
2318-
>>> print(tril2)
2319-
Tensor(shape=[3, 4], dtype=int64, place=Place(cpu), stop_gradient=True,
2320-
[[1 , 2 , 3 , 0 ],
2321-
[5 , 6 , 7 , 8 ],
2322-
[9 , 10, 11, 12]])
2323-
2324-
>>> # example 3, negative diagonal value
2325-
>>> tril3 = paddle.tril(data, diagonal=-1)
2326-
>>> print(tril3)
2327-
Tensor(shape=[3, 4], dtype=int64, place=Place(cpu), stop_gradient=True,
2328-
[[0 , 0 , 0 , 0 ],
2329-
[5 , 0 , 0 , 0 ],
2330-
[9 , 10, 0 , 0 ]])
2331-
"""
2332-
if in_dynamic_mode():
2333-
return _C_ops.tril(x, diagonal)
2334-
elif in_pir_mode():
2335-
op_type = 'tril'
2336-
assert x is not None, f'x cannot be None in {op_type}'
2337-
check_variable_and_dtype(
2338-
x,
2339-
'x',
2340-
[
2341-
'float16',
2342-
'uint16',
2343-
'float32',
2344-
'float64',
2345-
'int32',
2346-
'int64',
2347-
'bool',
2348-
'complex64',
2349-
'complex128',
2350-
],
2351-
op_type,
2352-
)
2353-
if len(x.shape) < 2:
2354-
raise ValueError(f"x shape in {op_type} must be at least 2-D")
2355-
if not isinstance(diagonal, (int,)):
2356-
raise TypeError(f"diagonal in {op_type} must be a python Int")
2357-
return _C_ops.tril(x, diagonal)
2358-
else:
2359-
return _tril_triu_op(LayerHelper('tril', **locals()))
2360-
2361-
23622273
@inplace_apis_in_dygraph_only
23632274
def tril_(
23642275
x: paddle.Tensor, diagonal: int = 0, name: str | None = None
@@ -2372,98 +2283,6 @@ def tril_(
23722283
return _C_ops.tril_(x, diagonal)
23732284

23742285

2375-
def triu(
2376-
x: paddle.Tensor, diagonal: int = 0, name: str | None = None
2377-
) -> paddle.Tensor:
2378-
r"""
2379-
Return the upper triangular part of a matrix (2-D tensor) or batch of matrices
2380-
:attr:`x`, the other elements of the result tensor are set to 0.
2381-
The upper triangular part of the matrix is defined as the elements on and
2382-
above the diagonal.
2383-
2384-
Args:
2385-
x (Tensor): The input x which is a Tensor.
2386-
Support data types: ``float64``, ``float32``, ``int32``, ``int64``, ``complex64``, ``complex128``.
2387-
diagonal (int, optional): The diagonal to consider, default value is 0.
2388-
If :attr:`diagonal` = 0, all elements on and above the main diagonal are
2389-
retained. A positive value excludes just as many diagonals above the main
2390-
diagonal, and similarly a negative value includes just as many diagonals below
2391-
the main diagonal. The main diagonal are the set of indices
2392-
:math:`\{(i, i)\}` for :math:`i \in [0, \min\{d_{1}, d_{2}\} - 1]` where
2393-
:math:`d_{1}, d_{2}` are the dimensions of the matrix.
2394-
name(str|None, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None.
2395-
2396-
Returns:
2397-
Tensor: Results of upper triangular operation by the specified diagonal of input tensor x,
2398-
it's data type is the same as x's Tensor.
2399-
2400-
Examples:
2401-
.. code-block:: python
2402-
2403-
>>> import paddle
2404-
2405-
>>> x = paddle.arange(1, 13, dtype="int64").reshape([3,-1])
2406-
>>> print(x)
2407-
Tensor(shape=[3, 4], dtype=int64, place=Place(cpu), stop_gradient=True,
2408-
[[1 , 2 , 3 , 4 ],
2409-
[5 , 6 , 7 , 8 ],
2410-
[9 , 10, 11, 12]])
2411-
2412-
>>> # example 1, default diagonal
2413-
>>> triu1 = paddle.tensor.triu(x)
2414-
>>> print(triu1)
2415-
Tensor(shape=[3, 4], dtype=int64, place=Place(cpu), stop_gradient=True,
2416-
[[1 , 2 , 3 , 4 ],
2417-
[0 , 6 , 7 , 8 ],
2418-
[0 , 0 , 11, 12]])
2419-
2420-
>>> # example 2, positive diagonal value
2421-
>>> triu2 = paddle.tensor.triu(x, diagonal=2)
2422-
>>> print(triu2)
2423-
Tensor(shape=[3, 4], dtype=int64, place=Place(cpu), stop_gradient=True,
2424-
[[0, 0, 3, 4],
2425-
[0, 0, 0, 8],
2426-
[0, 0, 0, 0]])
2427-
2428-
>>> # example 3, negative diagonal value
2429-
>>> triu3 = paddle.tensor.triu(x, diagonal=-1)
2430-
>>> print(triu3)
2431-
Tensor(shape=[3, 4], dtype=int64, place=Place(cpu), stop_gradient=True,
2432-
[[1 , 2 , 3 , 4 ],
2433-
[5 , 6 , 7 , 8 ],
2434-
[0 , 10, 11, 12]])
2435-
2436-
"""
2437-
if in_dynamic_mode():
2438-
return _C_ops.triu(x, diagonal)
2439-
elif in_pir_mode():
2440-
op_type = 'triu'
2441-
assert x is not None, f'x cannot be None in {op_type}'
2442-
check_variable_and_dtype(
2443-
x,
2444-
'x',
2445-
[
2446-
'float16',
2447-
'uint16',
2448-
'float32',
2449-
'float64',
2450-
'int32',
2451-
'int64',
2452-
'bool',
2453-
'complex64',
2454-
'complex128',
2455-
],
2456-
op_type,
2457-
)
2458-
if len(x.shape) < 2:
2459-
raise ValueError(f"x shape in {op_type} must be at least 2-D")
2460-
if not isinstance(diagonal, (int,)):
2461-
raise TypeError(f"diagonal in {op_type} must be a python Int")
2462-
return _C_ops.triu(x, diagonal)
2463-
else:
2464-
return _tril_triu_op(LayerHelper('triu', **locals()))
2465-
2466-
24672286
@inplace_apis_in_dygraph_only
24682287
def triu_(
24692288
x: paddle.Tensor, diagonal: int = 0, name: str | None = None

0 commit comments

Comments
 (0)