aclnnSparseFlashAttentionGrad
Applicable Products
Ascend 950PR&950DT products : 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: Rearranges data of size selectedBlockSize selected from key and value based on topkIndices, and then computes the backward output of attention in training scenarios.
Formula: Rearranges data of selectedBlockCount blocks, each of size selectedBlockSize, selected from keyIn and value based on the input topkIndices. The formula is as follows:
$$ selectedKey\text{ }=\text{ }Gather \left( key,topkIndices \left[ i \left] \left) ,\text{ }0\text{ } < =i < \text{ }selectBlockCount\right. \right. \right. \right. $$$$ selectedValue\text{ }=\text{ }Gather \left( value,topkIndices \left[ i \left] \left) ,\text{ }0\text{ } < =i < \text{ }selectBlockCount\right. \right. \right. \right. $$In the KV merge use case, if value is a null pointer, value in the preceding formula is processed as key, and selectedValue is equivalent to selectedKey. In this use case, the backward output does not return dValue separately; dKeyOut indicates the merged gradient:
$$ dKeyOut = dK + dV $$
Phase 1: Compute $dP$ and $dV$ based on the matrix multiplication derivative rule:
Phase 2: Compute $dS$:
Phase 3: Compute $dQ$ and $dK$:
Function Prototype
Each operator uses a two-phase API. Call "aclnnSparseFlashAttentionGradGetWorkspaceSize" to obtain the workspace size required for computation and the executor containing the computation flow of the operator, and then call "aclnnSparseFlashAttentionGrad" to execute the computation.
aclnnStatus aclnnSparseFlashAttentionGradGetWorkspaceSize(
const aclTensor *query,
const aclTensor *key,
const aclTensor *value,
const aclTensor *sparseIndices,
const aclTensor *dOut,
const aclTensor *out,
const aclTensor *softmaxMax,
const aclTensor *softmaxSum,
const aclTensor *actualSeqLengthsQueryOptional,
const aclTensor *actualSeqLengthsKvOptional,
const aclTensor *queryRopeOptional,
const aclTensor *keyRopeOptional,
double scaleValue,
int64_t sparseBlockSize,
char *layoutOptional,
int64_t sparseMode,
int64_t preTokens,
int64_t nextTokens,
bool deterministic,
const aclTensor *dQueryOut,
const aclTensor *dKeyOut,
const aclTensor *dValueOut,
const aclTensor *dQueryRopeOutOptional,
const aclTensor *dKeyRopeOutOptional,
uint64_t *workspaceSize,
aclOpExecutor **executor)aclnnStatus aclnnSparseFlashAttentionGrad(
void *workspace,
uint64_t workspaceSize,
aclOpExecutor *executor,
aclrtStream stream)aclnnSparseFlashAttentionGradGetWorkspaceSize
Parameters
Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor query Input Input Q of the attention structure. The Shape dimensions of query, key, value, sparseIndices, dOut, out, softmaxMax, and softmaxSum must be consistent. BFLOAT16, FLOAT16 ND (B,S1,N1,D), (T1,N1,D)
B: generalized; S1: generalized; N1: 128, 64, 32, 16, 8, 4, 2, 1; D: 512; T1: B × S1
ForAscend 950PR&950DT products , N1 additionally supports 48, 24, 12, 6, 3√ key Input Input K of the attention structure. - BFLOAT16, FLOAT16 ND (B,S2,N2,D), (T2,N2,D)
N2: 1; T2: B × S2√ value Input Input v of the attention structure. Optional. Passing a null pointer enables KV merge, and value is processed as key internally. In this case, dValueOut must also be a null pointer, and dKeyOut returns the merged result of dK+dV. BFLOAT16, FLOAT16 ND (B,S2,N2,D), (T2,N2,D) √ sparseIndices Input Attention indices with higher weights selected in sparse use cases. - INT32 ND (B,S1,N2,K), (T1,N2,K)
K: 1024, 2048, 3072, 4096, 5120, 6144, 7168, 8192√ dOut Input Gradient of the attention output matrix. - BFLOAT16, FLOAT16 ND (B,S1,N1,D), (T1,N1,D) √ out Input Attention output matrix. - BFLOAT16, FLOAT16 ND (B,S1,N1,D), (T1,N1,D) √ softmaxMax Input Intermediate output of the forward attention computation. - FLOAT32 ND (B,N2,S1,G), (N2,T1,G)
G: N1/N2√ softmaxSum Input Intermediate output of forward attention computation. - FLOAT32 ND (B,N2,S1,G), (N2,T1,G) √ actualSeqLengthsQueryOptional Input Number of valid Query tokens in each batch. - Optional: exists when layout is TND.
- Length must be the same as B.
- The cumulative sum must be the same as T1 (can be less than T1 in the EOD use case).
- The value must be non-negative. Passing a negative value may trigger a chip alarm.
INT32 ND (B,) √ actualSeqLengthsKvOptional Input Number of valid Key and value tokens in each batch. - Optional: available when layout is TND.
- The length must be the same as B.
- The cumulative sum must be the same as T2 (the cumulative sum can be less than T2 in EOD use cases).
INT32 ND (B,) √ queryRopeOptional Input MLA rope part: output of Query position encoding. Optional. queryRopeOptional and keyRopeOptional must be passed in together or both null pointers. BFLOAT16, FLOAT16 ND (B,S1,N1,Dr),(T1,N1,Dr)
Dr: 64√ keyRopeOptional Input MLA rope part: output of Key position encoding. Optional. queryRopeOptional and keyRopeOptional must be passed in together or both set to null pointers. BFLOAT16, FLOAT16 ND (B,S2,N2,Dr), (T2,N2,Dr) √ scaleValue Input Scale factor. Recommended value: reciprocal of the square root of d in the formula. FLOAT32 - - - sparseBlockSize Input Size of the selected block. Atlas A2 products andAtlas A3 products support 1, 8, 16, 32, and 64
Ascend 950PR&950DT products support 1INT64 - - - layout Input layout format. Supports BSND and TND. STRING - - - sparseMode Input sparse mode. - Represents the sparse mode. For details about different sparse modes, see Constraints.
- Only modes 0 and 3 are supported.
INT64 - - - preTokens Input Start position of the sliding window over the S matrix in the Attention operator. - pre_tokens takes effect when sparseMode=4.
- Only value 2147483647 is supported.
INT64 - - - nextTokens Input End position of the sliding window over the S matrix in the Attention operator. - next_tokens takes effect when sparseMode=4.
- Only value 2147483647 is supported.
INT64 - - - deterministic Input Deterministic computation. Must be consistent with the network-level deterministic parameter use_deterministic_algorithms. BOOL - - - dQuery Output Gradient of query. Same shape dimensions as the input query. BFLOAT16, FLOAT16 ND (B,S1,N1,D), (T1,N1,D) x dKey Output Indicates the gradient of key. In the KV merge use case, indicates the merged gradient of key and value, that is, dK+dV. BFLOAT16, FLOAT16 ND (B,S2,N2,D), (T2,N2,D) x dValue Output Indicates the gradient of value. Optional. In normal use cases, the shape is the same as that of input value; in the KV merge use case, a null pointer must be passed in, dValue is not output separately, and its gradient is merged into dKey. BFLOAT16, FLOAT16 ND (B,S2,N2,D), (T2,N2,D) x dQueryRopeOptional Output Indicates the gradient of queryRope. - This variable is output only when input queryRope exists.
- The shape is the same as that of input query.
BFLOAT16, FLOAT16 ND (B,S1,N1,Dr), (T1,N1,Dr) x dKeyRopeOptional Output Gradient of keyRope. Output only when input keyRope exists. BFLOAT16, FLOAT16 ND (B,S2,N2,Dr), (T2,N2,Dr) x 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 A required parameter or output is a null pointer. In the KV merge use case, value and dValueOut are allowed to be null pointers. ACLNN_ERR_PARAM_INVALID 161002 The data types and data formats of input variables, such as query, key, value, sparseIndices, ..., are out of the supported range. ACLNN_ERR_RUNTIME_ERROR 361001 An exception occurs when the API calls the npu runtime API for memory operations. ACLNN_ERR_INNER_TILING_ERROR 561002 The value of an input parameter (such as layout or sparseMode) is out of the supported range, or the shape dimensions of the input Tensor do not meet the constraints.
aclnnSparseFlashAttentionGrad
Parameters
Parameter Input/Output Description workspace Input Workspace memory address allocated on the Device. workspaceSize Input Workspace size allocated on the Device, obtained by the first-phase API aclnnSparseFlashAttentionGradGetWorkspaceSize. executor Input op executor containing the operator computation flow. stream Input Stream where the task is executed. Return Value
aclnnStatus: return status code. For details, see aclnn Return Codes.
Constraints
Deterministic computation:
- aclnnSparseFlashAttentionGrad defaults to a non-deterministic implementation. Deterministic computation can be enabled through aclrtCtxSetSysParamOpt.
General Constraints
- Handling of empty input parameters:
- query is an empty tensor: return directly.
- Currently, only the scenario where value and key are identical is supported.
- In the KV merge use case, pass null pointers for value and dValueOut. dKeyOut returns dK+dV, and dValue is no longer returned separately.
- Handling of empty input parameters:
Mask
sparseMode Meaning Remarks 0 No mask operation is performed. Supported 1 allMask, the complete attenmask matrix must be passed in. Not supported 2 leftUpCausal mode mask, the optimized attenmask matrix must be passed in. Not supported 3 Mask of the rightDownCausal mode, corresponding to the lower triangular use case partitioned by the right vertex. Supported 4 Mask of the band mode, requiring the optimized attenmask matrix as input. Not supported 5 prefix Not supported 6 global Not supported 7 dilated Not supported 8 block_local Not supported Specification constraints
Item Specification Description deterministic bool Supports deterministic computation. B 1-256 - S1, S2 ~~S1: 1-128K~~, S2: 1-1M. S1 and S2 support unequal lengths. N1 1, 2, 4, 8, 16, 32, 64, 128
Ascend 950PR&950DT products additionally support 48, 24, 12, 6, and 3SparseFA is MQA. N2 1 SparseFA is MQA and Nidx2=1. D 512 - Drope 64 - K 1024, 2048, 3072, 4096, 5120, 6144, 7168, 8192 K * sparseBlockSize exceeding 100k is not recommended. Otherwise, the internal algorithm hardware limit may cause OOM. layout BSND/TND -
Example
The following sample code is for reference only (taking
#include <iostream>
#include <vector>
#include <numeric>
#include "acl/acl.h"
#include "aclnnop/aclnn_sparse_flash_attention_grad.h"
#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;
}
void PrintOutResult(std::vector<int64_t> &shape, void** deviceAddr) {
auto size = GetShapeSize(shape);
std::vector<aclFloat16> resultData(size, 0);
auto ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]),
*deviceAddr, size * sizeof(resultData[0]), ACL_MEMCPY_DEVICE_TO_HOST);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy result from device to host failed. ERROR: %d\n", ret); return);
for (int64_t i = 0; i < size; i++) {
LOG_PRINT("mean result[%ld] is: %f\n", i, aclFloat16ToFloat(resultData[i]));
}
}
int Init(int32_t deviceId, aclrtContext* context, aclrtStream* stream) {
// Boilerplate: 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 = aclrtCreateContext(context, deviceId);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtCreateContext failed. ERROR: %d\n", ret); return ret);
ret = aclrtSetCurrentContext(*context);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSetCurrentContext 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) {
auto size = GetShapeSize(shape) * sizeof(T);
// Call aclrtMalloc to allocate device-side memory.
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);
// Call aclrtMemcpy to copy host-side data to device-side memory.
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); return ret);
// Compute the strides of the contiguous tensor.
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];
}
// Call the aclCreateTensor API to create an aclTensor.
*tensor = aclCreateTensor(shape.data(), shape.size(), dataType, strides.data(), 0, aclFormat::ACL_FORMAT_ND,
shape.data(), shape.size(), *deviceAddr);
return 0;
}
int main() {
// 1. (Boilerplate) Initialize device/context/stream. For details, see the AscendCL external API list.
// Fill in deviceId based on the actual device.
int32_t deviceId = 0;
aclrtContext context;
aclrtStream stream;
auto ret = Init(deviceId, &context, &stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret);
// 2. Construct the inputs and outputs. Customize the construction based on the API definitions.
std::vector<int64_t> qShape = {1, 16, 512}; // T1, N1, D
std::vector<int64_t> kShape = {2048, 1, 512}; // T2, N2, D
std::vector<int64_t> vShape = {2048, 1, 512}; // T2, N2, D
std::vector<int64_t> sparseIndicesShape = {1, 1, 2048}; // T1, N2, K
std::vector<int64_t> outShape = {1, 16, 512}; // T1, N1, D
std::vector<int64_t> dOutShape = {1, 16, 512}; // T1, N1, D
std::vector<int64_t> softmaxMaxShape = {1, 1, 16}; // N2, T1, G
std::vector<int64_t> softmaxSumShape = {1, 1, 16}; // N2, T1, G
std::vector<int64_t> actSeqQLenshape = {1}; // B
std::vector<int64_t> actSeqKvLenshape = {1}; // B
std::vector<int64_t> qRopeShape = {1, 16, 64}; // T1, N1, Drope
std::vector<int64_t> kRopeShape = {2048, 1, 64}; // T2, N2, Drope
void* qDeviceAddr = nullptr;
void* kDeviceAddr = nullptr;
void* vDeviceAddr = nullptr;
void* sparseIndicesDeviceAddr = nullptr;
void* outDeviceAddr = nullptr;
void* dOutDeviceAddr = nullptr;
void* softmaxMaxDeviceAddr = nullptr;
void* softmaxSumDeviceAddr = nullptr;
void* actSeqQLenDeviceAddr = nullptr;
void* actSeqKvLenDeviceAddr = nullptr;
void* qRopeDeviceAddr = nullptr;
void* kRopeDeviceAddr = nullptr;
void* dqDeviceAddr = nullptr;
void* dkDeviceAddr = nullptr;
void* dvDeviceAddr = nullptr;
void* dqRopeDeviceAddr = nullptr;
void* dkRopeDeviceAddr = nullptr;
aclTensor* q = nullptr;
aclTensor* k = nullptr;
aclTensor* v = nullptr;
aclTensor* sparseIndices = nullptr;
aclTensor* out = nullptr;
aclTensor* dOut = nullptr;
aclTensor* softmaxMax = nullptr;
aclTensor* softmaxSum = nullptr;
aclTensor* actSeqQLen = nullptr;
aclTensor* actSeqKvLen = nullptr;
aclTensor* qRope = nullptr;
aclTensor* kRope = nullptr;
aclTensor* dq = nullptr;
aclTensor* dk = nullptr;
aclTensor* dv = nullptr;
aclTensor* dqRope = nullptr;
aclTensor* dkRope = nullptr;
std::vector<short> qHostData(1 * 16 * 512, 1.0);
std::vector<short> kHostData(2048 * 1 * 512, 1.0);
std::vector<short> vHostData(2048 * 1 * 512, 1.0);
std::vector<int32_t> sparseIndicesHostData(2048);
std::iota(sparseIndicesHostData.begin(), sparseIndicesHostData.end(), 0);
std::vector<short> outHostData(1 * 16 * 512, 1.0);
std::vector<short> dOutHostData(1 * 16 * 512, 1.0);
std::vector<float> softmaxMaxHostData(16, 3.0);
std::vector<float> softmaxSumHostData(16, 3.0);
std::vector<int32_t> actSeqQLenHostData(1, 1);
std::vector<int32_t> actSeqKvLenHostData(1, 2048);
std::vector<short> qRopeHostData(1 * 16 * 64, 1.0);
std::vector<short> kRopeHostData(2048 * 1 * 64, 1.0);
std::vector<short> dqHostData(1 * 16 * 512, 0);
std::vector<short> dkHostData(2048 * 1 * 512, 0);
std::vector<short> dvHostData(2048 * 1 * 512, 0);
std::vector<short> dqRopeHostData(1 * 16 * 64, 0);
std::vector<short> dkRopeHostData(2048 * 1 * 64, 0);
ret = CreateAclTensor(qHostData, qShape, &qDeviceAddr, aclDataType::ACL_FLOAT16, &q);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(kHostData, kShape, &kDeviceAddr, aclDataType::ACL_FLOAT16, &k);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(vHostData, vShape, &vDeviceAddr, aclDataType::ACL_FLOAT16, &v);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(sparseIndicesHostData, sparseIndicesShape, &sparseIndicesDeviceAddr, aclDataType::ACL_INT32, &sparseIndices);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(outHostData, outShape, &outDeviceAddr, aclDataType::ACL_FLOAT16, &out);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(dOutHostData, dOutShape, &dOutDeviceAddr, aclDataType::ACL_FLOAT16, &dOut);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(softmaxMaxHostData, softmaxMaxShape, &softmaxMaxDeviceAddr, aclDataType::ACL_FLOAT, &softmaxMax);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(softmaxSumHostData, softmaxSumShape, &softmaxSumDeviceAddr, aclDataType::ACL_FLOAT, &softmaxSum);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(actSeqQLenHostData, actSeqQLenshape, &actSeqQLenDeviceAddr, aclDataType::ACL_INT32, &actSeqQLen);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(actSeqKvLenHostData, actSeqKvLenshape, &actSeqKvLenDeviceAddr, aclDataType::ACL_INT32, &actSeqKvLen);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(qRopeHostData, qRopeShape, &qRopeDeviceAddr, aclDataType::ACL_FLOAT16, &qRope);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(kRopeHostData, kRopeShape, &kRopeDeviceAddr, aclDataType::ACL_FLOAT16, &kRope);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(dqHostData, qShape, &dqDeviceAddr, aclDataType::ACL_FLOAT16, &dq);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(dkHostData, kShape, &dkDeviceAddr, aclDataType::ACL_FLOAT16, &dk);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(dvHostData, vShape, &dvDeviceAddr, aclDataType::ACL_FLOAT16, &dv);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(dqRopeHostData, qRopeShape, &dqRopeDeviceAddr, aclDataType::ACL_FLOAT16, &dqRope);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(dkRopeHostData, kRopeShape, &dkRopeDeviceAddr, aclDataType::ACL_FLOAT16, &dkRope);
CHECK_RET(ret == ACL_SUCCESS, return ret);
double scaleValue = 0.044194;
int64_t sparseBlockSize = 1;
int64_t sparseMode = 0;
int64_t preTokens = 2147483647;
int64_t nextTokens = 2147483647;
bool deterministic = false;
char layout[5] = {'T', 'N', 'D', 0};
// 3. Call the CANN operator library API. Replace it with the specific API name.
uint64_t workspaceSize = 0;
aclOpExecutor* executor;
// Call the first-phase API of aclnnSparseFlashAttentionGrad.
ret = aclnnSparseFlashAttentionGradGetWorkspaceSize(q, k, v, sparseIndices, dOut, out, softmaxMax, softmaxSum,
actSeqQLen, actSeqKvLen, qRope, kRope, scaleValue, sparseBlockSize, layout, sparseMode, preTokens, nextTokens,
deterministic, dq, dk, dv, dqRope, dkRope, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnSparseFlashAttentionGradGetWorkspaceSize failed. ERROR: %d\n", ret); return ret);
// Allocate device memory based on the workspaceSize calculated by the first-phase API.
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);
}
// Call the second-phase API aclnnSparseFlashAttentionGrad.
ret = aclnnSparseFlashAttentionGrad(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnSparseFlashAttentionGrad failed. ERROR: %d\n", ret); return ret);
// 4. (Boilerplate) 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);
// 5. Obtain the output values, copy the results from device memory to host memory. Modify the code based on the specific API definition.
PrintOutResult(qShape, &dqDeviceAddr);
PrintOutResult(kShape, &dkDeviceAddr);
PrintOutResult(vShape, &dvDeviceAddr);
PrintOutResult(qRopeShape, &dqRopeDeviceAddr);
PrintOutResult(kRopeShape, &dkRopeDeviceAddr);
// 6. Release aclTensor and aclScalar. Modify the code based on the specific API definition.
aclDestroyTensor(q);
aclDestroyTensor(k);
aclDestroyTensor(v);
aclDestroyTensor(sparseIndices);
aclDestroyTensor(out);
aclDestroyTensor(dOut);
aclDestroyTensor(softmaxMax);
aclDestroyTensor(softmaxSum);
aclDestroyTensor(actSeqQLen);
aclDestroyTensor(actSeqKvLen);
aclDestroyTensor(qRope);
aclDestroyTensor(kRope);
aclDestroyTensor(dq);
aclDestroyTensor(dk);
aclDestroyTensor(dv);
aclDestroyTensor(dqRope);
aclDestroyTensor(dkRope);
// 7. Release device resources.
aclrtFree(qDeviceAddr);
aclrtFree(kDeviceAddr);
aclrtFree(vDeviceAddr);
aclrtFree(sparseIndicesDeviceAddr);
aclrtFree(softmaxMaxDeviceAddr);
aclrtFree(softmaxSumDeviceAddr);
aclrtFree(outDeviceAddr);
aclrtFree(dOutDeviceAddr);
aclrtFree(qRopeDeviceAddr);
aclrtFree(kRopeDeviceAddr);
aclrtFree(dqDeviceAddr);
aclrtFree(dkDeviceAddr);
aclrtFree(dvDeviceAddr);
aclrtFree(dqRopeDeviceAddr);
aclrtFree(dkRopeDeviceAddr);
aclrtFree(actSeqQLenDeviceAddr);
aclrtFree(actSeqKvLenDeviceAddr);
if (workspaceSize > 0) {
aclrtFree(workspaceAddr);
}
aclrtDestroyStream(stream);
aclrtDestroyContext(context);
aclrtResetDevice(deviceId);
aclFinalize();
return 0;
}