2525
2626import paddle
2727from paddle import _C_ops
28+ from paddle ._C_ops import tril , triu # noqa: F401
2829from paddle .utils import deprecated
2930from 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
23632274def 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
24682287def triu_ (
24692288 x : paddle .Tensor , diagonal : int = 0 , name : str | None = None
0 commit comments