sparse_attention

paddle.nn.functional. sparse_attention ( query, key, value, sparse_csr_offset, sparse_csr_columns, key_padding_mask=None, attn_mask=None, name=None ) [source]

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:

\[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.

Parameters
  • 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, 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, 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, optional) – The default value is None. Normally there is no need for user to set this property. For more information, please refer to Name.

Returns

[batch_size, num_heads, seq_len, head_dim]. The dtype can be float32 or float64.

Return type

4-D tensor with shape

Examples

# required: skiptest
import paddle
import numpy as np

query_data = np.array([[[[0, 1,], [2, 3],
        [ 0, 1], [2, 3]]]]).astype("float32")
key_data = np.array([[[[0, 1,], [2, 3],
                [ 0, 1], [2, 3]]]]).astype("float32")
value_data = np.array([[[[0, 1,], [2, 3],
                [ 0, 1], [2, 3]]]]).astype("float32")
sparse_csr_offset_data = np.array([[[0, 2,
                4, 6, 8]]]).astype("int32")
sparse_csr_columns_data = np.array([[[0, 1,
                0, 1, 2, 3, 2, 3]]]).astype("int32")
key_padding_mask_data = np.array([[1,1,1,0]]).astype("float32")
attention_mask_data = np.array([[1,0,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1]]).astype("float32")
print(query_data.shape)
# (1, 1, 4, 2)
print(sparse_csr_offset_data.shape)
# (1, 1, 5)
print(sparse_csr_columns_data.shape)
# (1, 1, 8)
paddle.disable_static()
query = paddle.to_tensor(query_data, stop_gradient=False,
                place=paddle.CUDAPlace(0))
key = paddle.to_tensor(key_data, stop_gradient=False,
                place=paddle.CUDAPlace(0))
value = paddle.to_tensor(value_data, stop_gradient=False,
                place=paddle.CUDAPlace(0))
offset = paddle.to_tensor(sparse_csr_offset_data, stop_gradient=False,
                place=paddle.CUDAPlace(0))
columns = paddle.to_tensor(sparse_csr_columns_data, stop_gradient=False,
                place=paddle.CUDAPlace(0))
key_padding_mask = paddle.to_tensor(key_padding_mask_data, stop_gradient=False,
                place=paddle.CUDAPlace(0))
attention_mask = paddle.to_tensor(attention_mask_data, stop_gradient=False,
                place=paddle.CUDAPlace(0))
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)
# [[[[0.        , 1.        ],
#    [1.99830270, 2.99830270],
#    [0.        , 1.        ],
#    [0.        , 1.        ]]]]
output = paddle.nn.functional.sparse_attention(query, key,
                value, offset, columns)
print(output)
# [[[[1.60885942, 2.60885954],
#       [1.99830270, 2.99830270],
#       [1.60885942, 2.60885954],
#       [1.99830270, 2.99830270]]]]