
    ёi                        S SK Jr  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  \(       a  S SKJr  S S	KJr  \
" 1 S
kSS\4SSSS9  SSS.         SS jjj5       rg)    )annotations)TYPE_CHECKING)_C_ops)corein_dynamic_or_pir_mode)%ForbidKeywordsIgnoreOneParamDecorator   )convert_np_dtype_to_dtype_)Tensor)	DTypeLike>   xaxisname_stacklevelz#paddle.compat.nn.functional.softmaxzpaddle.nn.functional.softmaxztorch.nn.functional.softmax)illegal_keysignore_param	func_namecorrect_name
url_suffixNoutc               z   Uc#  U R                   nUS:X  d  US:X  d  US:X  a  SnOSnUbS  [        U[        R                  R                  5      (       d*  [        U[        R
                  5      (       d  [        U5      n[        5       (       a0  Uc  U O[        R                  " X5      n[        R                  " XQUS9$ g)at  
This operator implements PyTorch compatible softmax. The calculation process is as follows:

1. The dimension :attr:`dim` of ``input`` will be permuted to the last.

2. Then ``input`` will be logically flattened to a 2-D matrix. The matrix's second
dimension(row length) is the same as the dimension :attr:`axis` of ``input``,
and the first dimension(column length) is the product of all other dimensions
of ``input``. For each row of the matrix, the softmax operator squashes the
K-dimensional(K is the width of the matrix, which is also the size of ``input``'s
dimension :attr:`dim`) vector of arbitrary real values to a K-dimensional
vector of real values in the range [0, 1] that add up to 1.

3. After the softmax operation is completed, the inverse operations of steps 1 and 2
are performed to restore the two-dimensional matrix to the same dimension as the ``input`` .

It computes the exponential of the given dimension and the sum of exponential
values of all the other dimensions in the K-dimensional vector input.
Then the ratio of the exponential of the given dimension and the sum of
exponential values of all the other dimensions is the output of the softmax
operator.

For each row :math:`i` and each column :math:`j` in the matrix, we have:

.. math::

    softmax[i, j] = \frac{\exp(input[i, j])}{\sum_j(exp(input[i, j])}

Example:

.. code-block:: text

    Case 1:
      Input:
        input.shape = [2, 3, 4]
        input.data = [[[2.0, 3.0, 4.0, 5.0],
                   [3.0, 4.0, 5.0, 6.0],
                   [7.0, 8.0, 8.0, 9.0]],
                  [[1.0, 2.0, 3.0, 4.0],
                   [5.0, 6.0, 7.0, 8.0],
                   [6.0, 7.0, 8.0, 9.0]]]

      Attrs:
        dim = -1

      Output:
        out.shape = [2, 3, 4]
        out.data = [[[0.0320586 , 0.08714432, 0.23688282, 0.64391426],
                     [0.0320586 , 0.08714432, 0.23688282, 0.64391426],
                     [0.07232949, 0.19661193, 0.19661193, 0.53444665]],
                    [[0.0320586 , 0.08714432, 0.23688282, 0.64391426],
                     [0.0320586 , 0.08714432, 0.23688282, 0.64391426],
                     [0.0320586 , 0.08714432, 0.23688282, 0.64391426]]]

    Case 2:
      Input:
        input.shape = [2, 3, 4]
        input.data = [[[2.0, 3.0, 4.0, 5.0],
                   [3.0, 4.0, 5.0, 6.0],
                   [7.0, 8.0, 8.0, 9.0]],
                  [[1.0, 2.0, 3.0, 4.0],
                   [5.0, 6.0, 7.0, 8.0],
                   [6.0, 7.0, 8.0, 9.0]]]
      Attrs:
        dim = 1

      Output:
        out.shape = [2, 3, 4]
        out.data = [[[0.00657326, 0.00657326, 0.01714783, 0.01714783],
                     [0.01786798, 0.01786798, 0.04661262, 0.04661262],
                     [0.97555875, 0.97555875, 0.93623955, 0.93623955]],
                    [[0.00490169, 0.00490169, 0.00490169, 0.00490169],
                     [0.26762315, 0.26762315, 0.26762315, 0.26762315],
                     [0.72747516, 0.72747516, 0.72747516, 0.72747516]]]

Parameters:
    input (Tensor): The input Tensor with data type bfloat16, float16, float32, float64.
    dim (int, optional): The dim along which to perform softmax
        calculations. It should be in range [-D, D), where D is the
        rank of ``input`` . If ``dim`` < 0, it works the same way as
        :math:`dim + D` . Default is None.
    dtype (str, optional): The data type of the output tensor, can be bfloat16, float16, float32, float64.
    out (Tensor, optional): The output Tensor.

Returns:
    A Tensor with the same shape and data type (use ``dtype`` if it is
    specified) as input.

Examples:
    .. code-block:: python

        >>> import paddle

        >>> x = paddle.to_tensor([[[2.0, 3.0, 4.0, 5.0],
        ...                        [3.0, 4.0, 5.0, 6.0],
        ...                        [7.0, 8.0, 8.0, 9.0]],
        ...                       [[1.0, 2.0, 3.0, 4.0],
        ...                        [5.0, 6.0, 7.0, 8.0],
        ...                        [6.0, 7.0, 8.0, 9.0]]],dtype='float32')
        >>> out1 = paddle.compat.nn.functional.softmax(x, -1)
        >>> out2 = paddle.compat.nn.functional.softmax(x, -1, dtype='float64')
        >>> #out1's data type is float32; out2's data type is float64
        >>> #out1 and out2's value is as follows:
        >>> print(out1)
        >>> print(out2)
        Tensor(shape=[2, 3, 4], dtype=float32, place=Place(cpu), stop_gradient=True,
        [[[0.03205860, 0.08714432, 0.23688284, 0.64391428],
          [0.03205860, 0.08714432, 0.23688284, 0.64391428],
          [0.07232949, 0.19661194, 0.19661194, 0.53444666]],
         [[0.03205860, 0.08714432, 0.23688284, 0.64391428],
          [0.03205860, 0.08714432, 0.23688284, 0.64391428],
          [0.03205860, 0.08714432, 0.23688284, 0.64391428]]])
        Tensor(shape=[2, 3, 4], dtype=float64, place=Place(cpu), stop_gradient=True,
        [[[0.03205860, 0.08714432, 0.23688282, 0.64391426],
          [0.03205860, 0.08714432, 0.23688282, 0.64391426],
          [0.07232949, 0.19661193, 0.19661193, 0.53444665]],
         [[0.03205860, 0.08714432, 0.23688282, 0.64391426],
          [0.03205860, 0.08714432, 0.23688282, 0.64391426],
          [0.03205860, 0.08714432, 0.23688282, 0.64391426]]])
Nr         r   )ndim
isinstancer   VarDescVarTypeDataTyper
   r   r   castsoftmax)inputdimdtyper   r   	outs_casts         \/var/www/html/banglarbhumi/venv/lib/python3.13/site-packages/paddle/tensor/compat_softmax.pyr!   r!      s    L {zz19	TQYCC 
	E4<<#7#788E4==11*51"]EE0I	~~i#66      )NN)
r"   r   r#   z
int | Noner$   zDTypeLike | Noner   zTensor | Nonereturnr   )
__future__r   typingr   paddler   paddle.frameworkr   r   paddle.utils.decorator_utilsr   base.frameworkr
   r   paddle._typingr   intr!    r'   r&   <module>r2      s    #    9 N 7( '&C(3/, "N7
 N7N7	N7 N7
 
N7 N7N7r'   