aclnnRainFusionAttention
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 : Not supported
Atlas training products : Not supported
Function
Description: RainFusionAttention performs sparse attention computation, supporting flexible block-level sparse patterns. It specifies the KV blocks selected for each Q block through selectIdx, enabling efficient sparse attention computation.
Formula: Sparse block size: $blockShapeX \times blockShapeY$, selectIdx specifies the sparse pattern
$$ attentionOut = Softmax(scale \cdot query \cdot key^T + atten\_mask) \cdot value $$The data layout format of the query, key, and value inputs for RainFusionAttention supports interpretation from multiple dimensional layouts, which can be passed in through qInputLayout and kvInputLayout.
- B: Indicates the batch size of the input samples (Batch).
- T: Indicates the length of B and S concatenated along the axis and tightly packed (Total tokens).
- S: Indicates the sequence length of the input samples (Seq-Length).
- H: Indicates the size of the hidden layer (Head-Size).
- N: Number of heads (Head-Num).
- D: Minimum unit size of the hidden layer, satisfying D = H/N (Head-Dim).
Currently supported layouts:
- qInputLayout: "TND" "BNSD"
- kvInputLayout: "TND" "BNSD"
Function Prototype
Each operator uses a two-phase API. First, call "aclnnRainFusionAttentionGetWorkspaceSize" to obtain the workspace size required for computation and the executor containing the operator computation process, and then call "aclnnRainFusionAttention" to execute the computation.
aclnnStatus aclnnRainFusionAttentionGetWorkspaceSize(
const aclTensor *query,
const aclTensor *key,
const aclTensor *value,
const aclTensor *selectIdx,
const aclTensor *selectNumIdx,
const aclIntArray *blockShape,
const aclTensor *attenMaskOptional,
const aclIntArray *actualSeqLengthsOptional,
const aclIntArray *actualSeqLengthsKvOptional,
const aclTensor *blockTableOptional,
char *qInputLayout,
char *kvInputLayout,
int64_t numKeyValueHeads,
int64_t maskType,
double scaleValue,
int64_t innerPrecise,
int64_t blockSize,
const aclTensor *attentionOut,
const aclTensor *softmaxLseOptional,
uint64_t *workspaceSize,
aclOpExecutor **executor)aclnnStatus aclnnRainFusionAttention(
void *workspace,
uint64_t workspaceSize,
aclOpExecutor *executor,
const aclrtStream stream)aclnnRainFusionAttentionGetWorkspaceSize
Parameters:
Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor query Input The query in the formula. - FLOAT16, BFLOAT16 ND 3 √ key Input The key in the formula. - FLOAT16, BFLOAT16 ND 3 √ value Input value in the formula. - FLOAT16, BFLOAT16 ND 3 √ selectIdx Input Sparse block index array, specifying the block-selected KV block index for each Q block. - The shape is [QBlockNum, headNum, maxKvBlockNum].
- QBlockNum is the total number of Q-direction blocks across all batches.
- Stores the block-selected KV block index for each Q block. Valid indices are placed at the front of the array in ascending order, and invalid positions are placed at the back and filled with -1.
INT64 ND 3 √ selectNumIdx Input Number of KV blocks actually selected by each Q block. - shape is [QBlockNum, headNum].
- Stores the number of KV blocks actually selected by each Q block.
INT64 ND 2 √ blockShape Input Sparse block shape array. - Must contain at least two elements [blockShapeX, blockShapeY].
- blockShapeX: block size in the Q direction.
- blockShapeY: block size in the KV direction.
INT64 - 1 - attenMaskOptional Input atten_mask in the formula. Currently not supported. Passed in as nullptr. BOOL ND 2 √ actualSeqLengthsOptional Input Describes the query sequence length corresponding to each Batch. - If not used, can be passed as nullptr.
- Used for variable-length sequence use cases.
INT64 - 1 - actualSeqLengthsKvOptional Input Describes the key/value sequence length corresponding to each batch. - If not used, nullptr can be passed.
- Used for variable-length sequence use cases.
INT64 - 1 - blockTableOptional Input Block table used for PagedAttention. Currently not supported. Pass in nullptr. INT32 ND 2 √ qInputLayout Input Represents the data arrangement format of the input query. Currently only supports "TND" and "BNSD". String - - - kvInputLayout Input Represents the data arrangement format of the input key and value. Currently only supports "TND" and "BNSD". String - - - numKeyValueHeads Input Represents the number of key/value heads. - INT64 - - - maskType Input Mask type. 0 indicates no mask; other values indicate different mask types. INT64 - - - scaleValue Input scale in the formula, representing the scaling factor. Generally set to D^-0.5. DOUBLE - - - innerPrecise Input Softmax precision control. 0 indicates float32 softmax, and 1 indicates fp16 softmax. INT64 - - - blockSize Input PagedAttention block size. Used for the PagedAttention use case. If not used, 0 can be passed. INT64 - - - attentionOut Output attentionOut in the formula. The data type and shape must be the same as those of query. FLOAT16, BFLOAT16 ND 3 √ softmaxLseOptional Output Intermediate log-sum-exp result of the Softmax computation. Currently not supported. Pass in nullptr. FLOAT ND 3 √ workspaceSize Output Returns the size of the Device-side-applied workspace. - - - - - executor Output Returns the op executor, which contains the operator computation flow. - - - - - Return value
aclnnStatus: return status code. For details, see aclnn Return Codes.
The first-phase API performs input parameter validation and returns an error in the following scenarios:
Return Value Error Code Description ACLNN_ERR_PARAM_NULLPTR 161001 The input query, key, value, selectIdx, or selectNumIdx passed in is a null pointer. ACLNN_ERR_PARAM_INVALID 161002 The data type of query, key, or value is not supported. qInputLayout or kvInputLayout is invalid. blockShape is invalid (the number of elements is less than 2 or the value is less than or equal to 0). innerPrecise is invalid (it must be 0 or 1).
aclnnRainFusionAttention
Parameters:
Parameter Input/Output Description workspace Input The Device-side-applied workspace memory address. workspaceSize Input The size of the Device-side-applied workspace, obtained through the first-phase API aclnnRainFusionAttentionGetWorkspaceSize. executor Input Executor of the op, containing the operator computation flow. stream Input Stream on which tasks are executed. Return value
Returns an aclnnStatus status code. For details, see aclnn Return Codes.
Constraints
- Deterministic computation:
- aclnnRainFusionAttention 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.
- qInputLayout currently supports only "TND" and "BNSD".
- kvInputLayout currently only supports "TND" and "BNSD".
- The data types of the input query, key, and value must be consistent, supporting FLOAT16 and BFLOAT16.
- blockShape must contain at least two elements [blockShapeX, blockShapeY], and their values must be greater than 0.
- The shape of selectIdx must be [T, headNum, maxKvBlockNum], where T is the total number of Q-direction blocks across all batches.
- The shape of selectNumIdx must be [T, headNum].
- innerPrecise must be 0 (float32 softmax) or 1 (fp16 softmax). When the query input is BFLOAT16, only 0 can be configured.
- qSeqlen and kvSeqlen do not need to be divisible by blockShape. Non-aligned scenarios are supported, and the actual number of blocks is calculated by rounding up.
- qSeqlen is mandatory when qInputLayout is "TND" or "BNSD". kvSeqlen is mandatory when kvInputLayout is "TND" or "BNSD".
- Sparse block indices must be within the valid range. Invalid positions are filled with -1.
- Let the headNum of the input query be N1 and the headNum of the input key and value be N2. Then N1 >= N2 && N1 % N2 == 0.
- Let G = N1 / N2. G must satisfy the following constraints: G < 128 && 128 % G == 0.
Example
The following sample code is for reference only. For details about compilation and execution, see Compile and Run Samples.
#include <iostream>
#include <vector>
#include <cstring>
#include <cmath>
#include <cstdint>
#include "acl/acl.h"
#include "aclnn/opdev/fp16_t.h"
#include "aclnnop/aclnn_rain_fusion_attention.h"
using namespace std;
#define CHECK_RET(cond, return_expr) \
do { \
if (!(cond)) { \
return_expr; \
} \
} while (0)
#define LOG_PRINT(message, ...) \
do { \
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) {
// Boilerplate code: AscendCL initialization.
auto ret = aclInit(nullptr);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclInit failed. ERROR: %d\n", ret); return ret);
ret = aclrtSetDevice(deviceId);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSetDevice failed. ERROR: %d\n", ret); return ret);
ret = aclrtCreateStream(stream);
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) {
// Check whether the shape is valid.
if (shape.empty()) {
LOG_PRINT("CreateAclTensor: ERROR - shape is empty\n");
return -1;
}
for (size_t i = 0; i < shape.size(); ++i) {
if (shape[i] <= 0) {
LOG_PRINT("CreateAclTensor: ERROR - shape[%zu]=%ld is invalid\n", i, shape[i]);
return -1;
}
}
auto size = GetShapeSize(shape) * sizeof(T);
// Check whether the size of hostData matches.
if (hostData.size() != static_cast<size_t>(GetShapeSize(shape))) {
LOG_PRINT("CreateAclTensor: ERROR - hostData size mismatch: %zu vs %ld\n",
hostData.size(), GetShapeSize(shape));
return -1;
}
// Call aclrtMalloc to allocate device-side memory.
*deviceAddr = nullptr;
auto ret = aclrtMalloc(deviceAddr, size, ACL_MEM_MALLOC_HUGE_FIRST);
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);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtMemcpy failed. ERROR: %d\n", ret);
aclrtFree(*deviceAddr); *deviceAddr = nullptr; return ret);
// Calculate the strides of the contiguous tensor.
std::vector<int64_t> strides(shape.size(), 1);
if (shape.size() > 1) {
for (int64_t i = static_cast<int64_t>(shape.size()) - 2; i >= 0; i--) {
strides[i] = shape[i + 1] * strides[i + 1];
}
}
*tensor = nullptr;
*tensor = aclCreateTensor(shape.data(), shape.size(), dataType, strides.data(), 0, aclFormat::ACL_FORMAT_ND,
shape.data(), shape.size(), *deviceAddr);
CHECK_RET(*tensor != nullptr, LOG_PRINT("aclCreateTensor failed - returned nullptr\n");
aclrtFree(*deviceAddr); *deviceAddr = nullptr; return -1);
return 0;
}
int main() {
// 1. (boilerplate) device/stream initialization
int32_t deviceId = 0;
aclrtStream stream;
auto ret = Init(deviceId, &stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret);
// 2. Set parameters.
int32_t batch = 1;
int32_t qSeqlen = 128;
int32_t kvSeqlen = 128;
int32_t numHeads = 1;
int32_t numKvHeads = 1;
int32_t headDim = 128;
int32_t blockShapeX = 128;
int32_t blockShapeY = 128;
// Calculate the dimensions in TND format.
int64_t totalQTokens = batch * qSeqlen;
int64_t totalKvTokens = batch * kvSeqlen;
int32_t qBlockNum = (qSeqlen + blockShapeX - 1) / blockShapeX; // Number of Q blocks in the X dimension
int32_t kvBlockNum = (kvSeqlen + blockShapeY - 1) / blockShapeY; // Number of KV blocks in the Y dimension
// totalQBlocks = qBlockNum * numHeads (each Q block corresponds to one head)
int32_t totalQBlocks = qBlockNum * batch;
int32_t maxKvBlockNum = kvBlockNum;
// 3. Create the Query tensor (TND format: [totalQTokens, numHeads, headDim]).
void *queryDeviceAddr = nullptr;
std::vector<int64_t> queryShape = {totalQTokens, numHeads, headDim};
std::vector<op::fp16_t> queryHostData(totalQTokens * numHeads * headDim, 1.0f);
aclTensor *queryTensor = nullptr;
ret = CreateAclTensor(queryHostData, queryShape, &queryDeviceAddr, aclDataType::ACL_FLOAT16, &queryTensor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("Failed to create query tensor\n"); return ret);
// 4. Create the Key/Value tensor (TND format: [totalKvTokens, numKvHeads, headDim]).
void *keyDeviceAddr = nullptr;
void *valueDeviceAddr = nullptr;
std::vector<int64_t> kvShape = {totalKvTokens, numKvHeads, headDim};
std::vector<op::fp16_t> keyHostData(totalKvTokens * numKvHeads * headDim, 1.0f);
std::vector<op::fp16_t> valueHostData(totalKvTokens * numKvHeads * headDim, 1.0f);
aclTensor *keyTensor = nullptr;
aclTensor *valueTensor = nullptr;
ret = CreateAclTensor(keyHostData, kvShape, &keyDeviceAddr, aclDataType::ACL_FLOAT16, &keyTensor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("Failed to create key tensor\n"); return ret);
ret = CreateAclTensor(valueHostData, kvShape, &valueDeviceAddr, aclDataType::ACL_FLOAT16, &valueTensor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("Failed to create value tensor\n"); return ret);
// 5. Generate the sparse indices selectIdx and selectNumIdx.
// selectIdx: [totalQBlocks, numHeads, maxKvBlockNum] - 3D tensor
// selectNumIdx: [totalQBlocks, numHeads] - 2D tensor
// Sparsity ratio is 1, meaning no sparsity is applied and each Q block selects all KV blocks.
std::vector<int64_t> selectIdxHostData(totalQBlocks * numHeads * maxKvBlockNum, -1);
std::vector<int64_t> selectNumIdxHostData(totalQBlocks * numHeads, 0);
// Sparsity ratio is 1: each Q block selects all KV blocks. Directly assign the indices 0 to maxKvBlockNum-1.
for (int32_t qb = 0; qb < totalQBlocks; ++qb) {
for (int32_t h = 0; h < numHeads; ++h) {
// selectNumIdx[qb, h] = maxKvBlockNum (each Q block selects all KV blocks)
selectNumIdxHostData[qb * numHeads + h] = static_cast<int64_t>(maxKvBlockNum);
// selectIdx[qb, h, k] = k (indices assigned directly, from 0 to maxKvBlockNum-1)
int64_t baseIdx = static_cast<int64_t>((qb * numHeads + h) * maxKvBlockNum);
for (int32_t k = 0; k < maxKvBlockNum; ++k) {
selectIdxHostData[baseIdx + k] = static_cast<int64_t>(k);
}
}
}
void *selectIdxDeviceAddr = nullptr;
void *selectNumIdxDeviceAddr = nullptr;
std::vector<int64_t> selectIdxShape = {totalQBlocks, numHeads, maxKvBlockNum};
std::vector<int64_t> selectNumIdxShape = {totalQBlocks, numHeads};
aclTensor *selectIdxTensor = nullptr;
aclTensor *selectNumIdxTensor = nullptr;
ret = CreateAclTensor(selectIdxHostData, selectIdxShape, &selectIdxDeviceAddr, aclDataType::ACL_INT64, &selectIdxTensor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("Failed to create selectIdx tensor\n"); return ret);
ret = CreateAclTensor(selectNumIdxHostData, selectNumIdxShape, &selectNumIdxDeviceAddr, aclDataType::ACL_INT64, &selectNumIdxTensor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("Failed to create selectNumIdx tensor\n"); return ret);
// 6. Create the output tensor.
void *outputDeviceAddr = nullptr;
std::vector<int64_t> outputShape = {totalQTokens, numHeads, headDim};
int64_t outputElementCount = totalQTokens * numHeads * headDim;
std::vector<op::fp16_t> outputHostData(outputElementCount, 0.0f);
aclTensor *outputTensor = nullptr;
ret = CreateAclTensor(outputHostData, outputShape, &outputDeviceAddr, aclDataType::ACL_FLOAT16, &outputTensor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("Failed to create output tensor\n"); return ret);
// 7. Create the blockShape array.
std::vector<int64_t> blockShapeData = {blockShapeX, blockShapeY};
aclIntArray *blockShape = aclCreateIntArray(blockShapeData.data(), blockShapeData.size());
CHECK_RET(blockShape != nullptr, LOG_PRINT("Failed to create blockShape array\n"); return -1);
// 8. Create actualSeqLengths and actualSeqLengthsKv (required parameters).
std::vector<int64_t> actualSeqLengthsHost(batch, static_cast<int64_t>(qSeqlen));
std::vector<int64_t> actualSeqLengthsKvHost(batch, static_cast<int64_t>(kvSeqlen));
void *actualSeqLengthsDevice = nullptr;
void *actualSeqLengthsKvDevice = nullptr;
size_t seqLengthsSize = batch * sizeof(int64_t);
ret = aclrtMalloc(&actualSeqLengthsDevice, seqLengthsSize, ACL_MEM_MALLOC_HUGE_FIRST);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("Failed to allocate actualSeqLengths memory\n"); return ret);
ret = aclrtMalloc(&actualSeqLengthsKvDevice, seqLengthsSize, ACL_MEM_MALLOC_HUGE_FIRST);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("Failed to allocate actualSeqLengthsKv memory\n");
aclrtFree(actualSeqLengthsDevice); return ret);
ret = aclrtMemcpy(actualSeqLengthsDevice, seqLengthsSize, actualSeqLengthsHost.data(),
seqLengthsSize, ACL_MEMCPY_HOST_TO_DEVICE);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("Failed to copy actualSeqLengths to device\n");
aclrtFree(actualSeqLengthsDevice); aclrtFree(actualSeqLengthsKvDevice); return ret);
ret = aclrtMemcpy(actualSeqLengthsKvDevice, seqLengthsSize, actualSeqLengthsKvHost.data(),
seqLengthsSize, ACL_MEMCPY_HOST_TO_DEVICE);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("Failed to copy actualSeqLengthsKv to device\n");
aclrtFree(actualSeqLengthsDevice); aclrtFree(actualSeqLengthsKvDevice); return ret);
// aclCreateIntArray expects a host-side data pointer instead of device-side data.
aclIntArray *actualSeqLengths = aclCreateIntArray(actualSeqLengthsHost.data(), batch);
aclIntArray *actualSeqLengthsKv = aclCreateIntArray(actualSeqLengthsKvHost.data(), batch);
CHECK_RET(actualSeqLengths != nullptr && actualSeqLengthsKv != nullptr,
LOG_PRINT("Failed to create actualSeqLengths arrays\n");
if (actualSeqLengthsDevice) aclrtFree(actualSeqLengthsDevice);
if (actualSeqLengthsKvDevice) aclrtFree(actualSeqLengthsKvDevice); return -1);
// 9. Prepare string parameters (ensure the buffer is large enough to contain the null terminator).
const char* qLayoutStr = "TND";
const char* kvLayoutStr = "TND";
char qLayoutBuffer[16] = {0};
char kvLayoutBuffer[16] = {0};
strncpy(qLayoutBuffer, qLayoutStr, sizeof(qLayoutBuffer) - 1);
strncpy(kvLayoutBuffer, kvLayoutStr, sizeof(kvLayoutBuffer) - 1);
// 10. Calculate scaleValue.
float scaleValue = 1.0f / std::sqrt(static_cast<float>(headDim));
// 11. Call the first-phase API.
uint64_t workspaceSize = 0;
aclOpExecutor* executor = nullptr;
ret = aclnnRainFusionAttentionGetWorkspaceSize(
queryTensor, // query
keyTensor, // key
valueTensor, // value
selectIdxTensor, // selectIdx
selectNumIdxTensor, // selectNumIdx
blockShape, // blockShape
nullptr, // attenMaskOptional
actualSeqLengths, // actualSeqLengthsOptional
actualSeqLengthsKv, // actualSeqLengthsKvOptional
nullptr, // blockTableOptional
qLayoutBuffer, // qInputLayout
kvLayoutBuffer, // kvInputLayout
numKvHeads, // numKeyValueHeads
0, // maskType
scaleValue, // scaleValue
0, // innerPrecise (1=fp16 softmax)
128, // blockSize
outputTensor, // attentionOut
nullptr, // softmaxLseOptional
&workspaceSize, // workspaceSize (out)
&executor); // executor (out)
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnRainFusionAttentionGetWorkspaceSize failed. ERROR: %d\n", ret); return ret);
CHECK_RET(executor != nullptr, LOG_PRINT("executor is null after GetWorkspaceSize\n"); return -1);
// 12. Allocate workspace.
void* workspaceAddr = nullptr;
if (workspaceSize > 0) {
ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret);
}
// 12. Call the second-phase API.
ret = aclnnRainFusionAttention(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnRainFusionAttention failed. ERROR: %d\n", ret); return ret);
// 13. Synchronize and wait for the task execution to complete.
ret = aclrtSynchronizeStream(stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret);
// 14. Obtain the output value and copy the result from device-side memory to the host side.
int64_t outputSize = GetShapeSize(outputShape);
std::vector<op::fp16_t> resultData(outputSize, 0);
ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(op::fp16_t), outputDeviceAddr,
outputSize * sizeof(op::fp16_t), ACL_MEMCPY_DEVICE_TO_HOST);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy result from device to host failed. ERROR: %d\n", ret); return ret);
// 15. Print partial results.
uint64_t printNum = 10;
LOG_PRINT("Output results (first %lu elements):\n", printNum);
for (uint64_t i = 0; i < printNum && i < resultData.size(); i++) {
LOG_PRINT(" index %lu: %f\n", i, static_cast<float>(resultData[i]));
}
// 16. Release resources.
if (workspaceAddr) aclrtFree(workspaceAddr);
if (queryDeviceAddr) aclrtFree(queryDeviceAddr);
if (keyDeviceAddr) aclrtFree(keyDeviceAddr);
if (valueDeviceAddr) aclrtFree(valueDeviceAddr);
if (outputDeviceAddr) aclrtFree(outputDeviceAddr);
if (selectIdxDeviceAddr) aclrtFree(selectIdxDeviceAddr);
if (selectNumIdxDeviceAddr) aclrtFree(selectNumIdxDeviceAddr);
if (actualSeqLengthsDevice) aclrtFree(actualSeqLengthsDevice);
if (actualSeqLengthsKvDevice) aclrtFree(actualSeqLengthsKvDevice);
if (queryTensor) aclDestroyTensor(queryTensor);
if (keyTensor) aclDestroyTensor(keyTensor);
if (valueTensor) aclDestroyTensor(valueTensor);
if (outputTensor) aclDestroyTensor(outputTensor);
if (selectIdxTensor) aclDestroyTensor(selectIdxTensor);
if (selectNumIdxTensor) aclDestroyTensor(selectNumIdxTensor);
if (blockShape) aclDestroyIntArray(blockShape);
if (actualSeqLengths) aclDestroyIntArray(actualSeqLengths);
if (actualSeqLengthsKv) aclDestroyIntArray(actualSeqLengthsKv);
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
LOG_PRINT("Test completed successfully!\n");
return 0;
}