aclnnPromptFlashAttentionV3
Applicable Products
Ascend 950PR&950DT products : Not supported
Atlas A3 products : Supported
Atlas A2 products : Supported
Atlas 200I/500 A2 inference products : Not supported
Atlas inference products : Supported
Atlas training products : Not supported
Function
Description: FlashAttention operator for the prompt inference task. Supports sparse optimization, actualSeqLengthsKv optimization, int8 quantization, and the innerPrecise parameter (for selecting the high-precision or high-performance mode).
Formula:
Self-attention builds an attention model using the relationships within the input samples themselves. The principle is as follows: assume there is an input sample sequence $x$ of length $n$, where each element of $x$ is a $d$-dimensional vector. Each $d$-dimensional vector can be regarded as a token embedding. After passing such a sequence through three weight matrices, three matrices of dimension $n*d$ are obtained.
The calculation formula of self-attention is generally defined as follows, where $Q$, $K$, and $V$ are important property elements of the input sample, obtained through spatial transformation of the input sample and can be unified into a feature space. "Attention" in the formula and operator name is an abbreviation of "self-attention".
$$ Attention(Q,K,V)=Score(Q,K)V $$In this operator, the Score function uses the Softmax function, and the self-attention calculation formula is:
$$ Attention(Q,K,V)=Softmax(\frac{QK^T}{\sqrt{d}})V $$Where: the product of $Q$ and $K^T$ represents the attention of the input $x$. To prevent this value from becoming too large, it is usually divided by the square root of $d$ for scaling, and each row is normalized by softmax. After multiplying with $V$, an $n*d$ matrix is obtained.
Function Prototype
The operator execution API is a two-phase API. You must first call the "aclnnPromptFlashAttentionV3GetWorkspaceSize" API to obtain the workspace size required for computation and the executor containing the operator computation process, and then call the "aclnnPromptFlashAttentionV3" API to perform the computation.
aclnnStatus aclnnPromptFlashAttentionV3GetWorkspaceSize(
const aclTensor *query,
const aclTensor *key,
const aclTensor *value,
const aclTensor *pseShift,
const aclTensor *attenMask,
const aclIntArray *actualSeqLengths,
const aclIntArray *actualSeqLengthsKv,
const aclTensor *deqScale1,
const aclTensor *quantScale1,
const aclTensor *deqScale2,
const aclTensor *quantScale2,
const aclTensor *quantOffset2,
int64_t numHeads,
double scaleValue,
int64_t preTokens,
int64_t nextTokens,
char *inputLayout,
int64_t numKeyValueHeads,
int64_t sparseMode,
int64_t innerPrecise,
const aclTensor *attentionOut,
uint64_t *workspaceSize,
aclOpExecutor **executor)aclnnStatus aclnnPromptFlashAttentionV3(
void *workspace,
uint64_t workspaceSize,
aclOpExecutor *executor,
const aclrtStream stream)aclnnPromptFlashAttentionV3GetWorkspaceSize
Parameters
Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor query Input Input Q in the formula. Must be consistent with the data types of key and value. FLOAT16, BFLOAT16, INT8 ND 3-4 × key Input Input K in the formula. Must be the same as the data type of query and value. FLOAT16, BFLOAT16, INT8 ND 3-4 × value Input Input V in the formula. Must be the same as the data type of query and key. FLOAT16, BFLOAT16, INT8 ND 3-4 × pseShift Input Position encoding. - Pass in nullptr if this function is not used.
- For details, see Constraints.
FLOAT16, BFLOAT16 ND 4 × attenMask Input Mask matrix. - Pass in nullptr if this function is not used.
- For details about all constraints, see Constraints.
BOOL, INT8, UINT8 ND 2-4 × actualSeqLengths Input Valid sequence length of query in each batch. - Pass nullptr if the sequence length is not specified.
- For comprehensive constraints, see Constraints.
INT64 ND 1 - actualSeqLengthsKv Input Valid sequence lengths of key/value in different batches. - Pass nullptr if the sequence length is not specified.
- For combined constraints, see Constraints.
INT64 ND 1 - deqScale1 Input Dequantization factor following BMM1. - Supports per-tensor.
- Pass nullptr if this function is not used.
UINT64, FLOAT32 ND 1 - quantScale1 Input Quantization factor before BMM2. - Supports per-tensor.
- Pass in nullptr if this function is not used.
FLOAT32 ND 1 - deqScale2 Input Dequantization factor after BMM2. - Supports per-tensor.
- If this function is not used, pass nullptr.
UINT64, FLOAT32 ND 1 - quantScale2 Input Quantization factor of the output. - Supports per-tensor and per-channel.
- Set this parameter to nullptr if this function is not used.
FLOAT32, BFLOAT16 ND 1 - quantOffset2 Input Quantization offset of the output. - Supports per-tensor and per-channel.
- Pass nullptr if this function is not used.
FLOAT32, BFLOAT16 ND 1 - numHeads Input Number of query heads. - INT64 ND 1 - scaleValue Input Reciprocal of the square root of d in the formula. - The data type must comply with the data type deduction rule with the data type of query.
- If not specified, 1.0 is recommended.
DOUBLE - 1 - preTokens Input Indicates the number of previous tokens that attention needs to compute association with. If not specified, 2147483647 is recommended. INT64 - 1 - nextTokens Input Indicates the number of following Tokens associated with the attention computation. If not specified, 0 is recommended. INT64 - 1 - inputLayout Input Identifies the data layout format of the input query, key, and value. - It is recommended to pass in "BSH" if not specified.
- For details about all constraints, see Constraints
STRING - - - numKeyValueHeads Input Number of heads in key and value. - It is recommended to pass in 0 if not specified, indicating that key/value and query have the same number of heads.
- For details about all constraints, see Constraints
INT64 - - - sparseMode Input Sparse mode. For comprehensive constraints, see Constraints. INT64 - 1 - innerPrecise Input Specifies high-precision or high-performance mode. For all constraints, see Constraints. INT8 - 1 - attentionOut Output Output in the formula. - FLOAT16, BFLOAT16, INT8 ND 3-4 - workspaceSize Output Returns the workspace size that needs to be allocated on the Device by the user. - - - 1 - executor Output Returns the op executor, which contains the operator computation process. - - - 1 - Return Value
aclnnStatus: return status code. For details, see aclnn Return Codes.
The first-phase API validates input parameters. If the following error codes are returned, the causes are:
Return Values Error Code Description ACLNN_ERR_PARAM_NULLPTR 161001 161001 is returned if a passed parameter is a required input, output, or required attribute and is a null pointer. ACLNN_ERR_PARAM_INVALID 161002 The data types and data formats of query, key, value, pseShift, attenMask, and attentionOut are not within the supported range. ACLNN_ERR_RUNTIME_ERROR 361001 An exception occurs when the API calls the NPU runtime API for memory allocation.
aclnnPromptFlashAttentionV3
Parameters
Parameter Input/Output Description workspace Input Workspace memory address requested on the Device side. workspaceSize Input Workspace size requested on the Device side, obtained by the first-phase API aclnnPromptFlashAttentionV3GetWorkspaceSize. executor Input op executor, containing the operator computation flow. stream Input Specifies the Stream for executing tasks. Return Value
aclnnStatus: return status code. For details, see aclnn Return Codes.
Constraints
Deterministic computation:
- aclnnPromptFlashAttentionV3 defaults to a deterministic implementation.
When using this API with PyTorch, ensure that the versions of CANN-related packages and PyTorch-related packages are compatible.
Handling of null input parameters: The operator internally checks whether the query parameter is null. If it is null, the operator returns directly. If the query parameter is a non-null tensor and the key and value parameters are null tensors (i.e., S2 is 0), attentionOut is filled with all zeros. When attentionOut is a null tensor, the AscendCLNN framework handles it. For other input parameters marked as "nullable" in the parameter descriptions above, no processing is performed when they are null pointers.
The query, key, and value data layouts can be interpreted from multiple dimensions, where B (Batch) indicates the input sample batch size, S (Seq-Length) indicates the input sample sequence length, H (Head-Size) indicates the hidden layer size, N (Head-Num) indicates the number of heads, D (Head-Dim) indicates the smallest unit size of the hidden layer with D=H/N, and T indicates the accumulated sequence length of input samples across all batches.
The function usage restrictions for query, key, and value inputs are as follows:
Atlas A2 products :Supports B axis less than or equal to 65536 (64k). If the input type includes INT8 and the D axis is not 32-aligned, or the input type is FLOAT16 or BFLOAT16 and the D axis is not 16-aligned, the B axis only supports up to 128.
Supports N axis less than or equal to 256.
S must be less than or equal to 20971520 (20M). In some long sequence scenarios, if the computation load is too large, the PFA operator may time out (aicore error type, with errorStr: timeout or trap error). In such scenarios, S-axis splitting is recommended. Note: The computation load is affected by B, S, N, D, etc. Larger values result in greater computation load. Typical long sequence scenarios that may cause timeouts (i.e., where the product of B, S, N, and D is large) include but are not limited to:
B Q_N Q_S D KV_N KV_S 1 20 2097152 256 1 2097152 1 2 20971520 256 2 20971520 20 1 2097152 256 1 2097152 1 10 2097152 512 1 2097152 The D axis must be less than or equal to 512. When inputLayout is BSH or BSND, N*D must be less than 65535.
Atlas A2 products : Overall constraints on the query, key, and value inputs in TND use cases:- T is less than or equal to 65536.
- N is equal to 8/16/32/64/128, and Q_N, K_N, and V_N are equal.
- Q_D and K_D are equal to 192, and V_D is equal to 128/192.
- The data type only supports BFLOAT16.
- The sparse mode only supports sparse=0 without mask input, or sparse=3 with mask input.
- When sparse=3, actualSeqLengths must be less than actualSeqLengthsKv for each individual batch.
Atlas inference products :- When inputLayout is BSH, the B axis is less than or equal to 300; otherwise, the B axis is less than or equal to 128. The N axis is less than or equal to 256. The S axis is less than or equal to 65535 (64k). If Q_S or KV_S is not 128-aligned and Q_S and KV_S are of different lengths, configuring atten_mask is not supported. The D axis is less than or equal to 512.
Atlas A2 products : data types FLOAT16, BFLOAT16, and INT8 are supported.
Atlas inference products : only the FLOAT16 data type is supported.
pseShift function usage restrictions as follows:
- The input shape must be (B, N, Q_S, KV_S) or (1, N, Q_S, KV_S), where Q_S is the S in the shape of query and KV_S is the S in the shapes of key and value.
- Q_S must be greater than or equal to the S length of query, and KV_S must be greater than or equal to the S length of key.
- When the KV_S of pseShift is not 32-aligned, it is recommended to pad it to 32 bytes to improve performance. The values of the extra part are not restricted. If this feature is not used, nullptr can be input.
Atlas A2 products : data types FLOAT16 and BFLOAT16 are supported. If pseShift is of the FLOAT16 type, query must be of the FLOAT16 or INT8 type. If pseShift is of the BFLOAT16 type, query must be of the BFLOAT16 type. If query, key, and value are of the FLOAT16 type and pseShift exists, the high-precision mode is used by default, and the corresponding constraints inherit from those of the high-precision mode.
Atlas inference products : only nullptr is supported.
Restrictions on the attenMask function are as follows:
- When the KV_S of attenMask is not 32-aligned, it is recommended to pad it to 32-byte alignment to improve performance. The extra part is padded with 1.
- Typically, the recommended input shapes are Q_S, KV_S; B, Q_S, KV_S; 1, Q_S, KV_S; B, 1, Q_S, KV_S; and 1, 1, Q_S, KV_S, where Q_S is the S in the shape of query and KV_S is the S in the shapes of key and value.
Atlas A2 products : data types BOOL, INT8, and UINT8 are supported.
Atlas inference products : only BOOL is supported.
actualSeqLengths and actualSeqLengthsKv inputs have the following function usage restrictions:
- For actualSeqLengths, if the sequence length is not specified, nullptr can be passed, indicating that the valid sequence length is the same as the S length in the query shape. Note that the valid sequence length of each batch in this parameter must not exceed the sequence length of the corresponding batch in the query.
- For actualSeqLengthsKv, if the sequence length is not specified, nullptr can be passed, indicating that the valid sequence length is the same as the S length in the key/value shape. Note that the valid sequence length of each batch in this parameter must not exceed the sequence length of the corresponding batch in the key/value.
- The input length of seqlen is subject to the following rules: If the input length is 1, all batches use the same seqlen; if the input length is greater than or equal to the number of batches, the first batch values of seqlen are used; input of other lengths is not supported.
- When the inputLayout of query is TND, this input is required, and the number of elements in this input is used as the batch value. Each element value indicates the sum of the Sequence Length values of the current batch and all preceding batches. Therefore, the value of an element must be greater than or equal to that of the preceding element, and negative values are not allowed.
Atlas A2 products : data type INT64 is supported, and the TND format is supported.
Atlas inference products : data type INT64 is supported.
deqScale1 and deqScale2 inputs, function usage restrictions as follows:
Atlas A2 products : data types UINT64 and FLOAT32 are supported.
Atlas inference products : Only nullptr is supported.
quantScale1 input, function usage restrictions as follows:
Atlas A2 products : The supported data type is FLOAT32.
Atlas inference products : Only nullptr is supported.
The restrictions on the use of the quantScale2 and quantOffset2 inputs are as follows:
Atlas A2 products : The supported data types are FLOAT32 and BFLOAT16.
Atlas inference products : Only nullptr is supported.
The preTokens input has the following function usage restrictions:
Atlas A2 products : The supported data type is INT64.
Atlas inference products : Only the value 2147483647 is supported.
The nextTokens input has the following function usage restrictions:
Atlas A2 products : The supported data type is INT64.
Atlas inference products : Only the values 0 and 2147483647 are supported.
The inputLayout input has the following function usage restrictions:
- Currently, BSH, BSND, BNSD, and BNSD_BSND are supported (when the input is BNSD, the output format is BSND). It is recommended to pass "BSH" when the user does not specify a format.
Atlas A2 products : Besides the preceding formats, TND is also supported (pse, full quantization, and post-quantization are not supported).
Restrictions on the numKeyValueHeads input and function usage are as follows:
- numHeads must be divisible by numKeyValueHeads, and in the BSND, BNSD, and BNSD_BSND use cases, it must be the same as the N-axis shape value of key/value in shape; otherwise, an error is reported.
Atlas A2 products : The data type supports INT64.
Atlas inference products : Only 0 is supported.
sparseMode input, function usage restrictions as follows:
Atlas A2 products :- When sparseMode is 0, it represents the defaultMask mode. If attenmask is not passed, no mask operation is performed, and preTokens and nextTokens are ignored (internally assigned to INT_MAX). If attenmask is passed, the complete attenmask matrix (S1 * S2) must be provided, indicating that the portion between preTokens and nextTokens needs to be computed.
- When sparseMode is 1, it represents allMask, and the complete attenmask matrix (S1 * S2) must be passed in.
- When sparseMode is 2, it represents the leftUpCausal mode mask, and the optimized attenmask matrix (2048 * 2048) must be passed in.
- When sparseMode is 3, it represents the rightDownCausal mode mask, corresponding to the lower triangular scenario partitioned by the right vertex, and the optimized attenmask matrix (2048 * 2048) must be passed in.
- When sparseMode is 4, it represents the band mode mask, and the optimized attenmask matrix (2048 * 2048) must be passed in.
- When sparseMode is 5, 6, 7, or 8, they represent prefix, global, dilated, and block_local respectively, and none of them are currently supported. If the user does not specify a value, 0 is recommended.
Atlas A2 products : When inputLayout is TND, sparseMode supports only 0 and 3.
Atlas inference products : Only 0 is supported.
Feature usage restrictions for the innerPrecise input are as follows:
There are four modes: 0, 1, 2, and 3. They occupy two bits in total. Bit 0 indicates the selection between high precision and high performance, and bit 1 indicates whether row invalidation correction is performed.
innerPrecise Mode Row invalid correction 0 High precision × 1 High performance × 2 High precision √ 3 High performance √ When Q_S>1, sparse_mode is 0 or 1, and a user-defined mask is input, enabling row invalid correction is recommended.
BFLOAT16 and INT8 do not distinguish between High Precision and High Performance. The invalid row correction takes effect on FLOAT16, BFLOAT16, and INT8. Currently, 0 and 1 are reserved configuration values. When the "mask portion that participates in the computation" contains an entire row of all 1s, precision loss may occur. In this case, set this parameter to 2 or 3 to enable the invalid row function to improve precision. However, this configuration degrades performance. If the operator can identify invalid row use cases, invalid row computation is automatically enabled. For example, sparse_mode is 3 and Sq > Skv.
The attentionOut output has the following function usage restrictions:
- When inputLayout is BNSD_BSND, the shape of the input query is BNSD and the output shape is BSND. In other cases, the shape of this parameter must keep consistent with the shape of the input query.
Atlas A2 products : Supported data types are FLOAT16, BFLOAT16, and INT8.
Atlas inference products : only FLOAT16 is supported.
Comprehensive constraints on the number of int8 quantization-related input parameters and the input/output data formats:
- When both input and output are INT8: The input parameters deqScale1, quantScale1, deqScale2, and quantScale2 must all be present. quantOffset2 is optional and defaults to 0 if not passed.
- Use case where the input is INT8 and the output is FLOAT16: input parameters deqScale1, quantScale1, and deqScale2 must be input at the same time. If input parameter quantOffset2 or quantScale2 exists (that is, not nullptr), an error is reported and returned.
- Use case where the input is FLOAT16 or BFLOAT16 and the output is INT8: the quantScale2 parameter is required, and quantOffset2 is optional, defaulting to 0 if not passed. If the parameters deqScale1, quantScale1, or deqScale2 exist (that is, are not nullptr), an error is reported and returned.
- The parameters quantScale2 and quantOffset2 support two formats (per-tensor/per-channel) and two data types (FLOAT32/BFLOAT16). If quantOffset2 is passed, its type and shape information must be consistent with those of quantScale2. When the input is BFLOAT16, both FLOAT32 and BFLOAT16 are supported; otherwise, only FLOAT32 is supported. In per-channel format, when the output layout is BSH, the product of all dimensions of quantScale2 must equal H; for other layouts, the product must equal N*D. (It is recommended to pass a quantScale2 shape of [1,1,H] or [H] when the output layout is BSH, [1,N,1,D] or [N,D] when the output is BNSD, and [1,1,N,D] or [N,D] when the output is BSND)
- When the output is int8 and quantScale2 and quantOffset2 are per-channel, left padding, Ring Attention, or the use case where D is not 32-byte aligned is not supported currently.
- When the output is INT8, the scenario where sparse is band and preTokens/nextTokens are negative is not supported.
When the output is INT8, the quantOffset2 parameter is passed with a non-null pointer and a non-empty tensor value, and sparseMode, preTokens, and nextTokens satisfy the following conditions, some rows of the matrix do not participate in computation, resulting in computation result errors. This use case is intercepted (solution: if this use case should not be intercepted, perform post-quantization outside the PFA API and do not enable it inside the PFA API):
- sparseMode = 0. If attenMask is not a null pointer and actualSeqLengths - actualSeqLengthsKV - preTokens > 0 or nextTokens < 0 for each batch, the interception condition is satisfied.
- For sparseMode = 1 or 2, the interception condition is never satisfied.
- When sparseMode = 3, if each batch satisfies actualSeqLengthsKV - actualSeqLengths < 0, the interception condition is met.
- For sparseMode = 4, the interception condition is satisfied when preTokens < 0 or nextTokens + actualSeqLengthsKV - actualSeqLengths < 0 for each batch.
Example
The following sample code is for reference only. For details about the compilation and execution, see Compile and Run Samples.
#include <iostream>
#include <vector>
#include <cmath>
#include <cstring>
#include "acl/acl.h"
#include "aclnn/opdev/fp16_t.h"
#include "aclnnop/aclnn_prompt_flash_attention_v3.h"
#include "securec.h"
using namespace std;
namespace {
#define CHECK_RET(cond) ((cond) ? true :(false))
#define LOG_PRINT(message, ...) \
do { \
(void)printf(message, ##__VA_ARGS__); \
} while (0)
int64_t GetShapeSize(const std::vector<int64_t>& shape) {
int64_t shapeSize = 1;
for (auto i : shape) {
shapeSize *= i;
}
return shapeSize;
}
int Init(int32_t deviceId, aclrtStream* stream) {
auto ret = aclInit(nullptr);
if (!CHECK_RET(ret == ACL_SUCCESS)) {
LOG_PRINT("aclInit failed. ERROR: %d\n", ret);
return ret;
}
ret = aclrtSetDevice(deviceId);
if (!CHECK_RET(ret == ACL_SUCCESS)) {
LOG_PRINT("aclrtSetDevice failed. ERROR: %d\n", ret);
return ret;
}
ret = aclrtCreateStream(stream);
if (!CHECK_RET(ret == ACL_SUCCESS)) {
LOG_PRINT("aclrtCreateStream failed. ERROR: %d\n", ret);
return ret;
}
return 0;
}
template <typename T>
int CreateAclTensor(const std::vector<T>& hostData, const std::vector<int64_t>& shape, void** deviceAddr,
aclDataType dataType, aclTensor** tensor) {
auto size = GetShapeSize(shape) * sizeof(T);
auto ret = aclrtMalloc(deviceAddr, size, ACL_MEM_MALLOC_HUGE_FIRST);
if (!CHECK_RET(ret == ACL_SUCCESS)) {
LOG_PRINT("aclrtMalloc failed. ERROR: %d\n", ret);
return ret;
}
ret = aclrtMemcpy(*deviceAddr, size, hostData.data(), size, ACL_MEMCPY_HOST_TO_DEVICE);
if (!CHECK_RET(ret == ACL_SUCCESS)) {
LOG_PRINT("aclrtMemcpy failed. ERROR: %d\n", ret);
return ret;
}
std::vector<int64_t> strides(shape.size(), 1);
for (int64_t i = shape.size() - 2; i >= 0; i--) {
strides[i] = shape[i + 1] * strides[i + 1];
}
*tensor = aclCreateTensor(shape.data(), shape.size(), dataType, strides.data(), 0, aclFormat::ACL_FORMAT_ND,
shape.data(), shape.size(), *deviceAddr);
return 0;
}
struct TensorResources {
void* queryDeviceAddr = nullptr;
void* keyDeviceAddr = nullptr;
void* valueDeviceAddr = nullptr;
void* attenDeviceAddr = nullptr;
void* outDeviceAddr = nullptr;
aclTensor* queryTensor = nullptr;
aclTensor* keyTensor = nullptr;
aclTensor* valueTensor = nullptr;
aclTensor* attenTensor = nullptr;
aclTensor* outTensor = nullptr;
aclIntArray* actualSeqLengths = nullptr;
};
int InitializeTensors(TensorResources& resources) {
std::vector<int64_t> queryShape = {1, 2, 1, 16};
std::vector<int64_t> keyShape = {1, 2, 2, 16};
std::vector<int64_t> valueShape = {1, 2, 2, 16};
std::vector<int64_t> attenShape = {1, 1, 1, 2};
std::vector<int64_t> outShape = {1, 2, 1, 16};
int64_t queryShapeSize = GetShapeSize(queryShape);
int64_t keyShapeSize = GetShapeSize(keyShape);
int64_t valueShapeSize = GetShapeSize(valueShape);
int64_t attenShapeSize = GetShapeSize(attenShape);
int64_t outShapeSize = GetShapeSize(outShape);
std::vector<op::fp16_t> queryHostData(queryShapeSize, 1);
std::vector<op::fp16_t> keyHostData(keyShapeSize, 1);
std::vector<op::fp16_t> valueHostData(valueShapeSize, 1);
std::vector<int8_t> attenHostData(attenShapeSize, 1);
std::vector<op::fp16_t> outHostData(outShapeSize, 1);
int ret = CreateAclTensor(queryHostData, queryShape, &resources.queryDeviceAddr,
aclDataType::ACL_FLOAT16, &resources.queryTensor);
if (!CHECK_RET(ret == ACL_SUCCESS)) {
return ret;
}
ret = CreateAclTensor(keyHostData, keyShape, &resources.keyDeviceAddr,
aclDataType::ACL_FLOAT16, &resources.keyTensor);
if (!CHECK_RET(ret == ACL_SUCCESS)) {
return ret;
}
ret = CreateAclTensor(valueHostData, valueShape, &resources.valueDeviceAddr,
aclDataType::ACL_FLOAT16, &resources.valueTensor);
if (!CHECK_RET(ret == ACL_SUCCESS)) {
return ret;
}
ret = CreateAclTensor(attenHostData, attenShape, &resources.attenDeviceAddr,
aclDataType::ACL_BOOL, &resources.attenTensor);
if (!CHECK_RET(ret == ACL_SUCCESS)) {
return ret;
}
ret = CreateAclTensor(outHostData, outShape, &resources.outDeviceAddr,
aclDataType::ACL_FLOAT16, &resources.outTensor);
if (!CHECK_RET(ret == ACL_SUCCESS)) {
return ret;
}
std::vector<int64_t> actualSeqlenVector = {2};
resources.actualSeqLengths = aclCreateIntArray(actualSeqlenVector.data(),
actualSeqlenVector.size());
return ACL_SUCCESS;
}
int ExecutePromptFlashAttention(TensorResources& resources, aclrtStream stream,
void** workspaceAddr, uint64_t* workspaceSize) {
int64_t numHeads = 2;
int64_t numKeyValueHeads = numHeads;
double scaleValue = 1 / sqrt(2);
int64_t preTokens = 2147483647;
int64_t nextTokens = 0;
int64_t sparseMode = 0;
int64_t innerPrecise = 1;
constexpr const char LAYER_OUT_STR[] = "BNSD";
constexpr size_t LAYER_OUT_LEN = sizeof(LAYER_OUT_STR);
char layerOut[LAYER_OUT_LEN];
memcpy(layerOut, LAYER_OUT_STR, LAYER_OUT_LEN);
aclOpExecutor* executor;
int ret = aclnnPromptFlashAttentionV3GetWorkspaceSize(
resources.queryTensor, resources.keyTensor, resources.valueTensor,
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
numHeads, scaleValue, preTokens, nextTokens, layerOut, numKeyValueHeads,
sparseMode, innerPrecise, resources.outTensor, workspaceSize, &executor);
if (!CHECK_RET(ret == ACL_SUCCESS)) {
LOG_PRINT("aclnnPromptFlashAttentionV3GetWorkspaceSize failed. ERROR: %d\n", ret);
return ret;
}
if (*workspaceSize > 0ULL) {
ret = aclrtMalloc(workspaceAddr, *workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST);
if (!CHECK_RET(ret == ACL_SUCCESS)) {
LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret);
return ret;
}
}
ret = aclnnPromptFlashAttentionV3(*workspaceAddr, *workspaceSize, executor, stream);
if (!CHECK_RET(ret == ACL_SUCCESS)) {
LOG_PRINT("aclnnPromptFlashAttentionV3 failed. ERROR: %d\n", ret);
return ret;
}
return ACL_SUCCESS;
}
int ProcessResults(TensorResources& resources, const std::vector<int64_t>& outShape) {
auto size = GetShapeSize(outShape);
std::vector<op::fp16_t> resultData(size, 0);
int ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]),
resources.outDeviceAddr, size * sizeof(resultData[0]),
ACL_MEMCPY_DEVICE_TO_HOST);
if (!CHECK_RET(ret == ACL_SUCCESS)) {
LOG_PRINT("copy result from device to host failed. ERROR: %d\n", ret);
return ret;
}
for (int64_t i = 0; i < size; i++) {
std::cout << "index: " << i << ": " << static_cast<float>(resultData[i]) << std::endl;
}
return ACL_SUCCESS;
}
void CleanupResources(TensorResources& resources, void* workspaceAddr,
aclrtStream stream, int32_t deviceId) {
if (resources.queryTensor) {
aclDestroyTensor(resources.queryTensor);
}
if (resources.keyTensor) {
aclDestroyTensor(resources.keyTensor);
}
if (resources.valueTensor) {
aclDestroyTensor(resources.valueTensor);
}
if (resources.attenTensor) {
aclDestroyTensor(resources.attenTensor);
}
if (resources.outTensor) {
aclDestroyTensor(resources.outTensor);
}
if (resources.actualSeqLengths) {
aclDestroyIntArray(resources.actualSeqLengths);
}
if (resources.queryDeviceAddr) {
aclrtFree(resources.queryDeviceAddr);
}
if (resources.keyDeviceAddr) {
aclrtFree(resources.keyDeviceAddr);
}
if (resources.valueDeviceAddr) {
aclrtFree(resources.valueDeviceAddr);
}
if (resources.attenDeviceAddr) {
aclrtFree(resources.attenDeviceAddr);
}
if (resources.outDeviceAddr) {
aclrtFree(resources.outDeviceAddr);
}
if (workspaceAddr) {
aclrtFree(workspaceAddr);
}
if (stream) {
aclrtDestroyStream(stream);
}
aclrtResetDevice(deviceId);
aclFinalize();
}
} // namespace
int main() {
int32_t deviceId = 0;
aclrtStream stream = nullptr;
TensorResources resources = {};
void* workspaceAddr = nullptr;
uint64_t workspaceSize = 0;
std::vector<int64_t> outShape = {1, 2, 1, 16};
int ret = ACL_SUCCESS;
// 1. Initialize device and stream
ret = Init(deviceId, &stream);
if (!CHECK_RET(ret == ACL_SUCCESS)) {
LOG_PRINT("Init acl failed. ERROR: %d\n", ret);
return ret;
}
// 2. Initialize tensors
ret = InitializeTensors(resources);
if (!CHECK_RET(ret == ACL_SUCCESS)) {
CleanupResources(resources, workspaceAddr, stream, deviceId);
return ret;
}
// 3. Execute the operation
ret = ExecutePromptFlashAttention(resources, stream, &workspaceAddr, &workspaceSize);
if (!CHECK_RET(ret == ACL_SUCCESS)) {
CleanupResources(resources, workspaceAddr, stream, deviceId);
return ret;
}
// 4. Synchronize stream
ret = aclrtSynchronizeStream(stream);
if (!CHECK_RET(ret == ACL_SUCCESS)) {
LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret);
CleanupResources(resources, workspaceAddr, stream, deviceId);
return ret;
}
// 5. Process results
ret = ProcessResults(resources, outShape);
if (!CHECK_RET(ret == ACL_SUCCESS)) {
CleanupResources(resources, workspaceAddr, stream, deviceId);
return ret;
}
// 6. Cleanup resources
CleanupResources(resources, workspaceAddr, stream, deviceId);
return 0;
}