aclnnApplyRotaryPosEmbGrad
Applicable Products
Ascend 950PR&950DT products : Supported
Atlas A3 products : Not supported
Atlas A2 products : Not supported
Atlas 200I/500 A2 inference products : Not supported
Atlas inference products : Not supported
Atlas training products : Not supported
Function
Description: Performs the backward computation of the dual-path rotary position embedding aclnnApplyRotaryPosEmb. Computes the rope backward gradients of query and key at the same time, fused into a single kernel call.
Formula:
In the forward computation of the rotary position embedding, let
dimsbe the list of broadcast axes, that is, the axes with value 1 incos/sinand a corresponding dimension greater than 1 ingradQueryEmbed/gradKeyEmbed(including the N axis and the optional B axis in BSND and SBND layouts). The formula for half mode is as follows:$$ grad\_q_1, grad\_q_2 = chunk(grad\_query\_embed, chunks=2, dim=-1) $$$$ grad\_k_1, grad\_k_2 = chunk(grad\_key\_embed, chunks=2, dim=-1) $$$$ cos_1, cos_2 = chunk(cos, chunks=2, dim=-1) $$$$ sin_1, sin_2 = chunk(sin, chunks=2, dim=-1) $$$$ query\_rotate = cat((-query_2, query_1), dim=-1) $$$$ key\_rotate = cat((-key_2, key_1), dim=-1) $$$$ grad\_query = cat(cos_1 * grad\_q_1 + sin_2 * grad\_q_2, cos_2 * grad\_q_2 - sin_1 * grad\_q_1, dim=-1) $$$$ grad\_key = cat(cos_1 * grad\_k_1 + sin_2 * grad\_k_2, cos_2 * grad\_k_2 - sin_1 * grad\_k_1, dim=-1) $$$$ grad\_cos = sum(grad\_query\_embed * query + grad\_key\_embed * key, dims) $$$$ grad\_sin = sum(grad\_query\_embed * query\_rotate + grad\_key\_embed * key\_rotate, dims) $$
Function Prototype
Each operator uses a two-phase API. You must call the "aclnnApplyRotaryPosEmbGradGetWorkspaceSize" API first to obtain input parameters and calculate the required workspace size based on the process. Then call the "aclnnApplyRotaryPosEmbGrad" API to perform the computation.
aclnnStatus aclnnApplyRotaryPosEmbGradGetWorkspaceSize(
const aclTensor *gradQueryEmbed,
const aclTensor *gradKeyEmbed,
const aclTensor *cos,
const aclTensor *sin,
const aclTensor *queryOptional,
const aclTensor *keyOptional,
char *rotaryModeOptional,
int64_t layout,
const aclTensor *gradQueryOut,
const aclTensor *gradKeyOut,
const aclTensor *gradCosOut,
const aclTensor *gradSinOut,
uint64_t *workspaceSize,
aclOpExecutor **executor)aclnnStatus aclnnApplyRotaryPosEmbGrad(
void *workspace,
uint64_t workspaceSize,
aclOpExecutor *executor,
aclrtStream stream)aclnnApplyRotaryPosEmbGradGetWorkspaceSize
Parameters
Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor gradQueryEmbed Input Derivative of the forward output query, corresponding to grad_q_embed in the formula. Empty tensors are not supported. BFLOAT16, FLOAT16, FLOAT32 ND 4 (layout is 1 or 2) or 3 (layout is 4) √ gradKeyEmbed Input Derivative of the forward output key, corresponding to grad_k_embed in the formula. Same data type and dimensions as gradQueryEmbed. Empty tensors are not supported. BFLOAT16, FLOAT16, FLOAT32 ND 4 (layout is 1 or 2) or 3 (layout is 4) √ cos Input cos input for forward computation. Same data type and dimensions as gradQueryEmbed. The N dimension must be 1. Empty tensors are not supported. BFLOAT16, FLOAT16, FLOAT32 ND 4 (layout is 1 or 2) or 3 (layout is 4) √ sin Input Forward computation input sin. Same data type and dimensions as gradQueryEmbed. The N dimension must be 1. Empty tensors are not supported. BFLOAT16, FLOAT16, FLOAT32 ND 4 (layout is 1 or 2) or 3 (layout is 4) √ queryOptional Optional input Forward computation input query. When the pointer is null, gradCosOutOptional and gradSinOutOptional are not computed. Same data type and dimensions as gradQueryEmbed. Must be passed together with keyOptional or omitted altogether. BFLOAT16, FLOAT16, FLOAT32 ND 4 (layout is 1 or 2) or 3 (layout is 4) √ keyOptional Optional input Forward computation input key. If it is a null pointer, gradCosOutOptional and gradSinOutOptional are not calculated. Its data type and dimensions must be the same as those of gradQueryEmbed. It must be passed in together with queryOptional or not passed in at all. BFLOAT16, FLOAT16, FLOAT32 ND 4 (layout is 1 or 2) or 3 (layout is 4) √ rotaryModeOptional Input Rotation mode. Only "half" is supported. STRING - - - layout Input Layout format of the input tensor. Value range: 1-BSND, 2-SBND, 4-TND, 3-BNSD (reserved). INT64 - - - gradQueryOut Output Derivative of query, the input of forward computation. Same data type and dimensions as gradQueryEmbed. BFLOAT16, FLOAT16, FLOAT32 ND 4 (layout: 1 or 2) or 3 (layout: 4) × gradKeyOut Output Derivative of the input key in forward computation. Same data type and dimensions as gradQueryEmbed. BFLOAT16, FLOAT16, FLOAT32 ND 4 (layout is 1 or 2) or 3 (layout is 4) × gradCosOut Output Derivative of the forward computation input cos. Valid when queryOptional and keyOptional are not empty. Same data type and dimension as gradQueryEmbed. BFLOAT16, FLOAT16, FLOAT32 ND 4 (layout is 1 or 2) or 3 (layout is 4) × gradSinOut Output Derivative of the forward computation input sin. Valid when queryOptional and keyOptional are not empty. Same data type and dimensions as gradQueryEmbed. BFLOAT16, FLOAT16, FLOAT32 ND 4 (layout is 1 or 2) or 3 (layout is 4) × workspaceSize Output Returns the workspace size to be allocated on the Device side. - - - - - 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 Values Error Code Description ACLNN_ERR_PARAM_NULLPTR 161001 The required inputs gradQueryEmbed, gradKeyEmbed, cos, and sin, and the required outputs gradQueryOut and gradKeyOut are null pointers. ACLNN_ERR_PARAM_INVALID 161002 The input and output data types and formats are not in the supported range, the shape does not meet the verification conditions, the dimensions are not in the supported range, queryOptional and keyOptional are not both passed in or both omitted, or rotaryMode/layout is not a supported value.
aclnnApplyRotaryPosEmbGrad
Parameters
Parameter Input/Output Description workspace Input Address of the workspace memory allocated on the Device. workspaceSize Input Size of the workspace allocated on the Device, obtained by calling the first-phase API aclnnApplyRotaryPosEmbGradGetWorkspaceSize. executor Input Op executor that contains the operator computation process. stream Input Stream for executing tasks. Return Value
aclnnStatus: return status code. For details, see aclnn Return Codes.
Constraints
- Deterministic computation:
- aclnnApplyRotaryPosEmbGrad defaults to a deterministic implementation.
- Constraints on each parameter are as follows:
- Input and output tensors only support 3D or 4D: 4D when layout is 1 or 2, and 3D when layout is 4.
- The dtype of input and output tensors must be the same.
- Input and output tensors do not support empty tensors (each dimension must be greater than 0).
- The layout of input and output tensors must be the same.
- The D dimension of input and output tensors must be the same. In half mode, it must be ≤ 1024 and divisible by 2.
- The shape of
gradQueryEmbedandgradQueryOutmust be the same, and the shape ofgradKeyEmbedandgradKeyOutmust be the same. - For any
layout, all dimensions except N must be the same betweengradQueryEmbedandgradKeyEmbed. - The N dimension of
cosandsinmust be equal to 1. Iflayoutis 1 (BSND) or 2 (SBND), the B dimension ofcosandsincan be 1 or consistent with the B dimension ofgradQueryEmbed. Iflayoutis 4 (TND), the T dimension ofcosandsinmust be consistent with the T dimension ofgradQueryEmbed. All dimensions except N (and the optionally broadcast B dimension under BSND and SBND layouts) must be consistent with those ofgradQueryEmbed. - The shapes of
cos,sin,gradCosOutOptional, andgradSinOutOptionalmust be the same. - The dimensions of
querymust be consistent withgradQueryEmbed, and the dimensions ofkeymust be consistent withgradKeyEmbed.queryandkeymust be both passed in or both omitted. rotaryModeOptionalonly supports "half".layoutonly supports {1, 2, 4}, corresponding to {BSND, SBND, TND}.
Example
The following sample code is for reference only. For details about compilation and execution, see Compile and Run Samples.
#include "acl/acl.h"
#include "aclnnop/aclnn_apply_rotary_pos_emb_grad.h"
#include <iostream>
#include <vector>
#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 shape_size = 1;
for (auto i : shape) {
shape_size *= i;
}
return shape_size;
}
int Init(int32_t deviceId, aclrtStream* stream)
{
// Boilerplate for resource 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)
{
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 the 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);
// Calculate 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 for device/stream initialization. For details, see the acl API documentation.
// Set deviceId based on the actual device.
int32_t deviceId = 0;
aclrtStream stream;
auto ret = Init(deviceId, &stream);
// Handle check as needed.
CHECK_RET(ret == 0, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret);
// 2. Construct inputs and outputs according to the API definition.
std::vector<int64_t> gradQEmbedShape = {1, 1, 1, 128};
std::vector<int64_t> gradKEmbedShape = {1, 1, 1, 128};
std::vector<int64_t> cosShape = {1, 1, 1, 128};
std::vector<int64_t> sinShape = {1, 1, 1, 128};
std::vector<int64_t> queryShape = {1, 1, 1, 128};
std::vector<int64_t> keyShape = {1, 1, 1, 128};
std::vector<int64_t> gradQueryOutShape = {1, 1, 1, 128};
std::vector<int64_t> gradKeyOutShape = {1, 1, 1, 128};
std::vector<int64_t> gradCosOutShape = {1, 1, 1, 128};
std::vector<int64_t> gradSinOutShape = {1, 1, 1, 128};
int64_t layout = 1;
const char* rotaryModeOptional = "half";
void* gradQEmbedDeviceAddr = nullptr;
void* gradKEmbedDeviceAddr = nullptr;
void* cosDeviceAddr = nullptr;
void* sinDeviceAddr = nullptr;
void* queryDeviceAddr = nullptr;
void* keyDeviceAddr = nullptr;
void* gradQueryOutDeviceAddr = nullptr;
void* gradKeyOutDeviceAddr = nullptr;
void* gradCosOutDeviceAddr = nullptr;
void* gradSinOutDeviceAddr = nullptr;
aclTensor* gradQueryEmbed = nullptr;
aclTensor* gradKeyEmbed = nullptr;
aclTensor* cos = nullptr;
aclTensor* sin = nullptr;
aclTensor* query = nullptr;
aclTensor* key = nullptr;
aclTensor* gradQueryOut = nullptr;
aclTensor* gradKeyOut = nullptr;
aclTensor* gradCosOut = nullptr;
aclTensor* gradSinOut = nullptr;
std::vector<float> gradQEmbedHostData = {
74, 54, 84, 125, 23, 78, 37, 72, 27, 98, 34, 107, 29, 23, 54, 60, 70, 49, 119, 54, 29, 54,
41, 99, 27, 62, 5, 46, 108, 39, 24, 123, 33, 82, 6, 40, 88, 24, 6, 116, 38, 119, 110, 5,
30, 79, 87, 18, 29, 100, 90, 24, 21, 93, 63, 68, 34, 112, 119, 48, 74, 43, 85, 64, 14, 49,
128, 59, 18, 37, 123, 76, 14, 63, 10, 39, 107, 124, 79, 16, 17, 76, 80, 47, 90, 41, 58, 82,
75, 80, 69, 37, 74, 36, 54, 26, 32, 54, 13, 100, 105, 15, 13, 69, 122, 26, 94, 59, 29, 14,
60, 8, 24, 17, 45, 33, 107, 122, 63, 111, 75, 128, 68, 31, 105, 6, 82, 99};
std::vector<float> gradKEmbedHostData = {
74, 54, 84, 125, 23, 78, 37, 72, 27, 98, 34, 107, 29, 23, 54, 60, 70, 49, 119, 54, 29, 54,
41, 99, 27, 62, 5, 46, 108, 39, 24, 123, 33, 82, 6, 40, 88, 24, 6, 116, 38, 119, 110, 5,
30, 79, 87, 18, 29, 100, 90, 24, 21, 93, 63, 68, 34, 112, 119, 48, 74, 43, 85, 64, 14, 49,
128, 59, 18, 37, 123, 76, 14, 63, 10, 39, 107, 124, 79, 16, 17, 76, 80, 47, 90, 41, 58, 82,
75, 80, 69, 37, 74, 36, 54, 26, 32, 54, 13, 100, 105, 15, 13, 69, 122, 26, 94, 59, 29, 14,
60, 8, 24, 17, 45, 33, 107, 122, 63, 111, 75, 128, 68, 31, 105, 6, 82, 99};
std::vector<float> cosHostData = {
41, 37, 17, 25, 49, 25, 22, 24, 110, 120, 107, 3, 82, 66, 75, 86, 85, 115, 110, 56, 52, 39,
86, 23, 36, 71, 20, 73, 113, 25, 114, 56, 125, 80, 95, 82, 31, 63, 99, 62, 23, 55, 30, 99,
42, 121, 15, 24, 97, 87, 81, 67, 43, 21, 13, 9, 33, 29, 117, 10, 114, 61, 98, 15, 78, 108,
48, 97, 1, 3, 78, 109, 57, 46, 47, 56, 50, 66, 81, 77, 17, 128, 68, 121, 47, 91, 114, 125,
51, 108, 31, 15, 47, 78, 109, 115, 113, 26, 53, 97, 1, 111, 103, 58, 106, 68, 11, 104, 22, 79,
61, 127, 86, 39, 33, 123, 102, 39, 64, 41, 119, 120, 61, 29, 94, 68, 36, 12};
std::vector<float> sinHostData = {
46, 56, 56, 101, 66, 10, 96, 16, 86, 57, 102, 66, 12, 105, 76, 58, 90, 6, 79, 128, 126, 82,
41, 3, 45, 7, 66, 4, 46, 22, 31, 26, 37, 63, 97, 84, 91, 90, 47, 77, 90, 34, 41, 83,
91, 108, 120, 13, 90, 32, 85, 37, 119, 31, 51, 82, 122, 125, 7, 116, 121, 108, 38, 56, 100, 20,
97, 119, 10, 4, 53, 13, 46, 82, 103, 119, 124, 80, 23, 67, 78, 56, 119, 122, 40, 58, 128, 27,
30, 52, 71, 42, 123, 69, 4, 5, 116, 97, 38, 107, 8, 4, 65, 120, 40, 22, 60, 44, 48, 66,
68, 125, 4, 93, 112, 112, 113, 90, 94, 23, 104, 39, 85, 84, 64, 128, 96, 119};
std::vector<float> queryHostData = {
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44,
45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66,
67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88,
89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108,
109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128};
std::vector<float> keyHostData = {
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44,
45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66,
67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88,
89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108,
109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128};
std::vector<float> gradQueryOutHostData(128, 0.0f);
std::vector<float> gradKeyOutHostData(128, 0.0f);
// Create the gradQueryEmbed aclTensor.
ret = CreateAclTensor(gradQEmbedHostData, gradQEmbedShape, &gradQEmbedDeviceAddr, aclDataType::ACL_FLOAT, &gradQueryEmbed);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the gradKeyEmbed aclTensor.
ret = CreateAclTensor(gradKEmbedHostData, gradKEmbedShape, &gradKEmbedDeviceAddr, aclDataType::ACL_FLOAT, &gradKeyEmbed);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create cos aclTensor.
ret = CreateAclTensor(cosHostData, cosShape, &cosDeviceAddr, aclDataType::ACL_FLOAT, &cos);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create sin aclTensor.
ret = CreateAclTensor(sinHostData, sinShape, &sinDeviceAddr, aclDataType::ACL_FLOAT, &sin);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create query aclTensor (used to compute grad_cos/grad_sin).
ret = CreateAclTensor(queryHostData, queryShape, &queryDeviceAddr, aclDataType::ACL_FLOAT, &query);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create key aclTensor (used to compute grad_cos/grad_sin).
ret = CreateAclTensor(keyHostData, keyShape, &keyDeviceAddr, aclDataType::ACL_FLOAT, &key);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create gradQueryOut aclTensor.
ret = CreateAclTensor(gradQueryOutHostData, gradQueryOutShape, &gradQueryOutDeviceAddr, aclDataType::ACL_FLOAT, &gradQueryOut);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create gradKeyOut aclTensor.
ret = CreateAclTensor(gradKeyOutHostData, gradKeyOutShape, &gradKeyOutDeviceAddr, aclDataType::ACL_FLOAT, &gradKeyOut);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create gradCosOut aclTensor.
ret = CreateAclTensor(std::vector<float>(128, 0.0f), gradCosOutShape, &gradCosOutDeviceAddr, aclDataType::ACL_FLOAT, &gradCosOut);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create gradSinOut aclTensor.
ret = CreateAclTensor(std::vector<float>(128, 0.0f), gradSinOutShape, &gradSinOutDeviceAddr, aclDataType::ACL_FLOAT, &gradSinOut);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// 3. Call the CANN operator library API, and replace it with the specific API.
uint64_t workspaceSize = 0;
aclOpExecutor* executor;
// Call the first-phase API of aclnnApplyRotaryPosEmbGrad.
ret = aclnnApplyRotaryPosEmbGradGetWorkspaceSize(gradQueryEmbed, gradKeyEmbed, cos, sin, query, key,
const_cast<char*>(rotaryModeOptional), layout,
gradQueryOut, gradKeyOut, gradCosOut, gradSinOut,
&workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS,
LOG_PRINT("aclnnApplyRotaryPosEmbGradGetWorkspaceSize failed. ERROR: %d\n", ret);
return ret);
// Allocate device memory for the workspaceSize computed 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 aclnnApplyRotaryPosEmbGrad.
ret = aclnnApplyRotaryPosEmbGrad(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnApplyRotaryPosEmbGrad failed. ERROR: %d\n", ret); return ret);
// 4. Boilerplate: wait synchronously for the task execution to finish.
ret = aclrtSynchronizeStream(stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret);
// 5. Obtain the output value and copy the result from the device memory to the host.
auto size = GetShapeSize(gradQueryOutShape);
std::vector<float> resultData(size, 0);
ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), gradQueryOutDeviceAddr,
size * sizeof(float), 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);
for (int64_t i = 0; i < size; i++) {
LOG_PRINT("result[%ld] is: %f\n", i, resultData[i]);
}
// 6. Release aclTensor and aclScalar.
aclDestroyTensor(gradQueryEmbed);
aclDestroyTensor(gradKeyEmbed);
aclDestroyTensor(cos);
aclDestroyTensor(sin);
aclDestroyTensor(query);
aclDestroyTensor(key);
aclDestroyTensor(gradQueryOut);
aclDestroyTensor(gradKeyOut);
aclDestroyTensor(gradCosOut);
aclDestroyTensor(gradSinOut);
// 7. Release device resources.
aclrtFree(gradQEmbedDeviceAddr);
aclrtFree(gradKEmbedDeviceAddr);
aclrtFree(cosDeviceAddr);
aclrtFree(sinDeviceAddr);
aclrtFree(queryDeviceAddr);
aclrtFree(keyDeviceAddr);
aclrtFree(gradQueryOutDeviceAddr);
aclrtFree(gradKeyOutDeviceAddr);
aclrtFree(gradCosOutDeviceAddr);
aclrtFree(gradSinOutDeviceAddr);
if (workspaceSize > 0) {
aclrtFree(workspaceAddr);
}
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
return 0;
}