
    ёi
,                    :   S SK Jr  S SKJr  S SKJrJr  S SKJr  SSK	J
r
JrJr  SSKJr  SSKJr  SS	KJrJrJr  \(       a  S S
KJr  S SKJrJr  / r\" SSSSS9 S         SS jj5       r   S         SS jjrSS jr   S           SS jjrg)    )annotations)TYPE_CHECKING)_C_opstensor)
deprecated   )check_dtype
check_typecheck_variable_and_dtype)LayerHelper)Variable)convert_np_dtype_to_dtype_corein_dynamic_or_pir_mode)Tensor)DataLayout2D	DTypeLikez2.5.2zpaddle.diag_embed   z<diag_embed in paddle.nn.functional will be removed in future)since	update_tolevelreasonc                0    [         R                  " XX#5      $ )N)r   
diag_embed)inputoffsetdim1dim2s       ^/var/www/html/banglarbhumi/venv/lib/python3.13/site-packages/paddle/nn/functional/extension.pyr   r   ,   s     UD77    Nc                   [        5       (       ai  [        U[        R                  R                  [        R
                  45      (       d  [        U5      nUc  Sn[        R                  " XU5      nSUl	        U$ [        S0 [        5       D6nUR                  US9nSU /0nSUR                  0nUb  [        U[        5      (       a  XS'   OXS'   UR                  SUS	U0US
9  SUl	        U$ )a  
**SequenceMask Layer**

This layer outputs a mask according to the input :code:`x` and
:code:`maxlen` with data type of :code:`dtype`.

Supposing :code:`x` is a Tensor with shape [d_1, d_2, ..., d_n], the
:code:`y` is a mask with shape [d_1, d_2, ..., d_n, maxlen], where:

.. math::

    y(i_1, i_2,..., i_n, j) = (j < x(i_1, i_2,..., i_n))

.. code-block:: text

    Case:

    Consider input:
        x = [3, 1, 1, 0]    max_len = 4

    then we get out:
        mask = [[1, 1, 1, 0],
                [1, 0, 0, 0],
                [1, 0, 0, 0],
                [0, 0, 0, 0]]

Args:
    x (Variable): Input tensor of sequence_mask layer, \
        whose elements are integers less than :code:`maxlen`. \
        Tensor with shape [d_1, d_2, ..., d_n].
    maxlen (int|None, optional): Maximum length of the sequence. If :code:`maxlen` \
                       is None, it would be replace with :math:`max(x)`.
    dtype (np.dtype|paddle.dtype|str, optional): Data type of the output, \
         ``int64`` by default.
    name(str|None, optional): For detailed information, please refer \
        to :ref:`api_guide_Name`. Usually name is no need to set and \
        None by default.

Returns:
        Tensor, The output sequence mask. Tensor with shape [d_1, d_2, ..., d_n, maxlen] \
        and data type of :code:`dtype`. The data type should be bool, float32, float64, int8, \
        int32 or int64.

Examples:
    .. code-block:: python

        >>> import paddle

        >>> lengths = paddle.to_tensor([10, 9, 8])
        >>> mask = paddle.nn.functional.sequence_mask(lengths)

        >>> print(mask)
        Tensor(shape=[3, 10], dtype=int64, place=Place(cpu), stop_gradient=True,
        [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
         [1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
         [1, 1, 1, 1, 1, 1, 1, 1, 0, 0]])

Tsequence_maskdtypeX	out_dtypeMaxLenTensormaxlenYtypeinputsoutputsattrs)r#   )r   
isinstancer   VarDescVarTypeDataTyper   r   r#   stop_gradientr   locals"create_variable_for_type_inferencer%   r   	append_op)xr)   r%   nameouthelperr-   r/   s           r   r#   r#   8   s    B %$,,"6"6!FGG.u5E>F""1e4 
5FH5F

3
3%
3
@CA3ZF#))$Efh''%+>"$(O
Vc3Zu   CJr    c                   U R                   S:w  a  [        S5      eU R                   UR                   :w  a  [        S5      e[        5       (       a0  [        UR                  SSS/S5        [
        R                  " X5      $ [        S0 [        5       D6n[        U SSS/S5        [        USSS/S5        UR                  U R                  S	9nUR                  SXS
.SU0S9  U$ )a  
To be used after beam search. After beam search, we get selected ids at
each time step and the corresponding parents in the search tree. Both ids
and parents have the layout :attr:`[max_time, batch_size, beam_size]`. Then
:attr:`gather_tree` is used to backtrace from the last time step and
generate the full sequences by collecting selected ids.

Here is an example:

.. code-block:: text

        Given:
            ids = [[[2 2]
                    [6 1]]
                   [[3 9]
                    [6 1]]
                   [[0 1]
                    [9 0]]]
            parents = [[[0 0]
                        [1 1]]
                       [[1 0]
                        [1 0]]
                       [[0 0]
                        [0 1]]]

        Then:
            gather_tree(ids, parents)
                     = [[[2 2]
                         [1 6]]
                        [[3 3]
                         [6 1]]
                        [[0 1]
                         [9 0]]]

Args:
    ids(Tensor): A Tensor with shape :attr:`[length, batch_size, beam_size]`
        and data type :attr:`int32` or :attr:`int64`. It contains the selected
        ids of all time steps.
    parents(Tensor): A Tensor with the same shape and data type as :attr:`ids`,
        It contains the parents corresponding to selected ids when searching
        among beams.

Returns:
        A Tensor with the same shape and data type as :attr:`ids`. \
        It contains the full sequences. The sequences are collected from \
        :attr:`ids` by backtracing according to :attr:`parents`.

Examples:
    .. code-block:: python

        >>> import paddle

        >>> ids = paddle.to_tensor([[[2, 2], [6, 1]], [[3, 9], [6, 1]], [[0, 1], [9, 0]]])

        >>> parents = paddle.to_tensor([[[0, 0], [1, 1]], [[1, 0], [1, 0]], [[0, 0], [0, 1]]])

        >>> final_sequences = paddle.nn.functional.gather_tree(ids, parents)
        >>> [[[2, 2], [1, 6]], [[3, 3], [6, 1]], [[0, 1], [9, 0]]]
        >>> final_sequences = paddle.nn.functional.gather_tree(ids, parents)
        >>> print(final_sequences)
        Tensor(shape=[3, 2, 2], dtype=int64, place=Place(cpu), stop_gradient=True,
        [[[2, 2],
          [1, 6]],
         [[3, 3],
          [6, 1]],
         [[0, 1],
          [9, 0]]])


r   zLThe input ids must be a 3D tensor with shape [length, batch_size, beam_size]z4The ids's shape must be the same as parents' shape. parentsint32int64gather_treeidsr$   )IdsParentsOut)r,   r-   r.   )r@   )ndim
ValueErrorr   r	   r%   r   r@   r   r5   r   r6   r7   )rA   r=   r;   r:   s       r   r@   r@      s    N xx1}Z
 	
 xx7<<OPPGMM9w.@-P!!#//7fh7 egw-?O Y' 2M	
 77cii7H3CL 	 	
 
r    c           	        US;  a  [        SU S35      e[        5       (       a  [        R                  " XX$5      $ [	        S0 [        5       D6n[        U S/ SQS5        [        US[        S5        [        US[        S5        UR                  U R                  S	9n[        U[        5      (       d  [        S
5      eUR                  SSU 0SU0UUUS.S9  U$ )a  

**Temporal Shift Operator**

Calculate the temporal shifting features for Input(X).

Input(X) should be in shape of [N*T, C, H, W] or [N*T, H, W, C], while
N is the batch size, T is the temporal segment number specified by
:attr:`seg_num`, C is the channel number, H and W is the height and
width of features.

Temporal Shifting is calculated as follows when data format is NCHW:

Step 1: Reshape Input(X) to [N, T, C, H, W].

Step 2: Pad 0 to reshaping result in the 2nd(T) dimension with
padding width as 1 on each side, padding result will be in shape
of [N, T+2, C, H, W].

Step 3: Assume :attr:`shift_ratio` is :math:`1/4`, slice padding
result as follows:

$$
slice1 = x[:, :T, :C/4, :, :]
$$
$$
slice2 = x[:, 2:T+2, C/4:C/2, :, :]
$$
$$
slice3 = x[:, 1:T+1, C/2:, :, :]
$$

Step 4: Concatenate three slices along the 3rd(C) dimension and
reshape result to [N*T, C, H, W].

For details of temporal shifting, please refer to paper:
`Temporal Shift Module <http://arxiv.org/abs/1811.08383>`_ .

Args:
    x(Tensor): ${x_comment}
    seg_num(int): ${seg_num_comment}
    shift_ratio(float): ${shift_ratio_comment}
    name(str|None, optional): For detailed information, please refer
                         to :ref:`api_guide_Name`. Usually name is no need to set and
                         None by default.
    data_format(str, optional): Data format that specifies the layout of input.
        It can be "NCHW" or "NHWC". Default: "NCHW".

Returns:
    out(Tensor): The temporal shifting result is a tensor with the
    same shape and same data type as the input.

Examples:
    .. code-block:: python

        >>> import paddle
        >>> import paddle.nn.functional as F

        >>> input = paddle.randn([6, 4, 2, 2])
        >>> out = F.temporal_shift(x=input, seg_num=2, shift_ratio=0.2)
)NCHWNHWCzJAttr(data_format) should be 'NCHW' or 'NHWC'. Received Attr(data_format): .temporal_shiftr8   )float16uint16float32float64seg_numshift_ratior$   zseg_num must be int type.r&   rD   )rP   rQ   data_formatr+   )rK   )rF   r   r   rK   r   r5   r   r
   intfloatr6   r%   r0   	TypeErrorr7   )r8   rP   rQ   r9   rR   r;   r:   s          r   rK   rK      s    H **++6-q:
 	
 $$QJJ:: 7		
 	7Is,<=;u6FG77agg7F'3''788!8CL"**	 	 		
 
r    )r   r"   )
r   r   r   rS   r   rS   r   rS   returnr   )Nr?   N)
r8   r   r)   z
int | Noner%   r   r9   
str | NonerW   r   )rA   r   r=   r   rW   r   )g      ?NrH   )r8   r   rP   rS   rQ   rT   r9   rX   rR   zDataLayout2D | strrW   r   )
__future__r   typingr   paddler   r   paddle.utilsr   base.data_feederr	   r
   r   base.layer_helperr   common_ops_importr   	frameworkr   r   r   r   paddle._typingr   r   __all__r   r#   r@   rK    r    r   <module>rd      s2   # " # 
 - )  6
 
!
I	 AC888*-8:=888 	ZZZ Z 	Z
 Zz_J &,eee e 	e
 $e er    