ó
    Ñ‘®iÅ  ã                  óf   • S SK Jr  S SKrS SKJr  S SKJr     S                 SS jjrg)é    )ÚannotationsN)Ú_C_ops)ÚLayerHelperc           	     ór  • [         R                  R                  5       (       a  [        R                  " U UUUUUU5      nU$ [        S0 [        5       D6n	U	R                  SS9n
U	R                  U
5      nU	R                  U
5      nU	R                  U
5      nU UUUUUUS.nUUUS.nU	R                  SXïS9  U$ )aó  
This operator sparsify the Attention matrix in Transformer module
to achieve the effect of reducing memory consumption and computation.
The sparse layout is expressed in CSR format and contains two parameters,
``offset`` and ``columns``. The equation is:

.. math::

    result=softmax(\frac{ Q * K^T }{\sqrt{d}}) * V

where : ``Q``, ``K``, and ``V`` represent the three input parameters of the attention module.
The dimensions of the three parameters are the same.
``d`` represents the size of the last dimension of the three parameters.

Warning:
    This API is only used in ``CUDA 11.3`` and above versions.

Args:
    query(Tensor): The query tensor in the Attention module.
                    4-D tensor with shape:
                    [batch_size, num_heads, seq_len, head_dim].
                    The dtype can be float32 and float64.
    key(Tensor): The key tensor in the Attention module.
                    4-D tensor with shape:
                    [batch_size, num_heads, seq_len, head_dim].
                    The dtype can be float32 and float64.
    value(Tensor): The value tensor in the Attention module.
                    4-D tensor with shape:
                    [batch_size, num_heads, seq_len, head_dim].
                    The dtype can be float32 and float64.
    sparse_csr_offset(Tensor): The sparsity feature in the Attention module
                    is expressed in the CSR format, and the offset represents
                    the number of non-zero elements in each row of the matrix.
                    3-D tensor with shape:
                    [batch_size, num_heads, seq_len + 1].
                    The dtype should be int32.
    sparse_csr_columns(Tensor): The sparsity feature in the Attention module
                    is expressed in the CSR format, and the columns represent
                    the column index values of non-zero elements in the matrix.
                    3-D tensor with shape:
                    [batch_size, num_heads, sparse_nnz].
                    The dtype should be int32.
    key_padding_mask(Tensor|None, optional):The key padding mask tensor in the Attention module.
                    2-D tensor with shape: [batch_size, seq_len].
                    The dtype can be float32 and float64.
                    A value of 0 means that the position is masked.
    attn_mask(Tensor|None, optional):The attention mask tensor in the Attention module.
                    2-D tensor with shape: [seq_len, seq_len].
                    The dtype can be float32 and float64.
                    A value of 0 means that the position is masked.
    name(str|None, optional): The default value is None. Normally there is no need for user
                    to set this property. For more information, please refer to
                    :ref:`api_guide_Name`.

Returns:
    Tensor, 4-D tensor with shape:
    [batch_size, num_heads, seq_len, head_dim].
    The dtype can be float32 or float64.

Examples:
    .. code-block:: pycon

        >>> # doctest: +SKIP('This API is only used in CUDA11.3 and above.')
        >>> import paddle

        >>> paddle.disable_static()

        >>> # `query`, `key` and `value` all have shape [1, 1, 4, 2]
        >>> query = paddle.to_tensor(
        ...     [[[[0, 1], [2, 3], [0, 1], [2, 3]]]],
        ...     dtype="float32",
        ... )
        >>> key = paddle.to_tensor([[[[0, 1], [2, 3], [0, 1], [2, 3]]]], dtype="float32")
        >>> value = paddle.to_tensor([[[[0, 1], [2, 3], [0, 1], [2, 3]]]], dtype="float32")
        >>> offset = paddle.to_tensor([[[0, 2, 4, 6, 8]]], dtype="int32")
        >>> columns = paddle.to_tensor([[[0, 1, 0, 1, 2, 3, 2, 3]]], dtype="int32")
        >>> print(offset.shape)
        paddle.Size([1, 1, 5])
        >>> print(columns.shape)
        paddle.Size([1, 1, 8])
        ...
        >>> key_padding_mask = paddle.to_tensor([[1, 1, 1, 0]], dtype="float32")
        >>> attention_mask = paddle.to_tensor(
        ...     [
        ...         [1, 0, 1, 1],
        ...         [1, 1, 1, 1],
        ...         [1, 1, 1, 1],
        ...         [1, 1, 1, 1],
        ...     ],
        ...     dtype="float32",
        ... )
        >>> output_mask = paddle.nn.functional.sparse_attention(
        ...     query,
        ...     key,
        ...     value,
        ...     offset,
        ...     columns,
        ...     key_padding_mask=key_padding_mask,
        ...     attn_mask=attention_mask,
        ... )
        >>> print(output_mask)
        Tensor(shape=[1, 1, 4, 2], dtype=float32, place=Place(cpu), stop_gradient=False,
        [[[[0.        , 1.        ],
           [1.99830270, 2.99830270],
           [0.        , 1.        ],
           [0.        , 1.        ]]]])

        >>> output = paddle.nn.functional.sparse_attention(query, key, value, offset, columns)
        >>> print(output)
        Tensor(shape=[1, 1, 4, 2], dtype=float32, place=Place(cpu), stop_gradient=False,
        [[[[1.60885942, 2.60885954],
           [1.99830270, 2.99830270],
           [1.60885942, 2.60885954],
           [1.99830270, 2.99830270]]]])
Úsparse_attentionÚQ)Úinput_param_name)r   ÚKÚVÚOffsetÚColumnsÚKeyPaddingMaskÚAttnMask)ÚOutÚSparseDotSddÚSoftmax)ÚtypeÚinputsÚoutputs)r   )
ÚpaddleÚ	frameworkÚin_dynamic_or_pir_moder   r   r   ÚlocalsÚinput_dtypeÚ"create_variable_for_type_inferenceÚ	append_op)ÚqueryÚkeyÚvalueÚsparse_csr_offsetÚsparse_csr_columnsÚkey_padding_maskÚ	attn_maskÚnameÚresÚhelperÚdtypeÚoutÚ
result_sddÚresult_softmaxr   r   s                   Úe/var/www/html/banglarbhumi/venv/lib/python3.13/site-packages/paddle/nn/functional/sparse_attention.pyr   r      sá   € ôz ×Ñ×.Ñ.×0Ñ0Ü×%Ò%ØØØØØØØó
ˆð ˆ
äÑ8¬v«xÑ8€FØ×Ñ°ÐÐ4€EØ
×
3Ñ
3°EÓ
:€CØ×:Ñ:¸5ÓA€JØ×>Ñ>¸uÓE€NàØØØ#Ø%Ø*Øñ€Fð Ø"Ø!ñ€Gð
 ×ÑÐ,°VÐÑMØ€Jó    )NNN)r   úpaddle.Tensorr   r-   r   r-   r    r-   r!   r-   r"   úpaddle.Tensor | Noner#   r.   r$   z
str | NoneÚreturnr-   )Ú
__future__r   r   r   Úpaddle.base.layer_helperr   r   © r,   r+   Ú<module>r3      sƒ   ðõ #ã Ý Ý 0ð .2Ø&*Øð]Øð]à	ð]ð ð]ð %ð	]ð
 &ð]ð +ð]ð $ð]ð ð]ð ö]r,   