aclnnNormRopeConcatBackward
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: In the transformer attention mechanism (multimodal), implements the backward deduction of the fused operator that performs normalization (Norm), rotary positional encoding (Rope), and feature concatenation (Concat) on query, key, and Value:
- Normalization (Norm) currently supports layer normalization (LayerNorm) and layer normalization with affine transformation parameters (AFFINE LayerNorm) types.
- Rotary position encoding (Rope) supports Interleave and Half types.
Formula:
LayerNorm backward deduction:
$$ \frac{\partial L}{\partial x} = \text{rstd} \cdot \Bigg( \frac{\partial L}{\partial y} - \text{Mean}\left( \frac{\partial L}{\partial y} \right) - \hat{x} \cdot \text{Mean}\left( \frac{\partial L}{\partial y} \odot \hat{x} \right) \Bigg) \quad \quad \quad \quad \quad \quad \quad \text{[Mean over headDim dimension]} $$LayerNorm (with affine transformation parameters) backward deduction:
$$ \left\{ \begin{aligned} \frac{\partial L}{\partial \beta} &= \sum_{B, S, H} \frac{\partial L}{\partial y}, &\quad \text{[Sum over batch, seq, headNum dimensions]} \\ \frac{\partial L}{\partial \gamma} &= \sum_{B, S, H} \frac{\partial L}{\partial y} \odot \hat{x}, &\quad \text{[Element-wise product accumulation]} \\ \frac{\partial L}{\partial x} &= \text{rstd} \cdot \Bigg( \frac{\partial L}{\partial \hat{x}} - \text{Mean}\left( \frac{\partial L}{\partial \hat{x}} \right) - \hat{x} \cdot \text{Mean}\left( \frac{\partial L}{\partial \hat{x}} \odot \hat{x} \right) \Bigg) &\quad \text{[Mean over headDim dimension]} \\ \end{aligned} \right\} $$Where (μ is the mean, σ^2 is the variance):
$$ \hat{x} = \frac{x - \mu}{\sqrt{\sigma^2 + \epsilon}}, \quad \quad \frac{\partial L}{\partial \hat{x}} = \frac{\partial L}{\partial y} \odot \gamma, \quad \quad \text{rstd} = \frac{1}{\sqrt{\sigma^2 + \epsilon}} $$Rope-Interleave backward deduction:
$$ \frac{\partial L}{\partial x} = \frac{\partial L}{\partial y} \cdot \text{cos} + Interleave({\frac{\partial L}{\partial y} \cdot \text{sin}}) \odot \text{negMask} $$Rope-Half backward deduction:
$$ \frac{\partial L}{\partial x} = \frac{\partial L}{\partial y} \cdot \text{cos} + Half({\frac{\partial L}{\partial y} \cdot \text{sin}}) \odot \text{negMask} $$Where Interleave() denotes alternating the odd and even positions in the headDim dimension, and Half() denotes alternating the second half and first half elements in the headDim dimension. For example, if x = [0,1,2,3,4,5,6,7], then Interleave(x) = [1,0,3,2,5,4,7,6] and Half(x)=[4,0,5,1,6,2,7,3]. negMask has a length of headDim, with even positions set to 1 and odd positions set to -1, that is, (1, -1, 1, -1, 1, ...)
Function Prototype
Each operator uses a two-phase API. You must first call the "aclnnNormRopeConcatBackwardGetWorkspaceSize" API to obtain the input parameters and calculate the required workspace size based on the computation process, and then call the "aclnnNormRopeConcatBackward" API to perform the computation.
aclnnStatus aclnnNormRopeConcatBackwardGetWorkspaceSize(
const aclTensor *gradQueryOutput,
const aclTensor *gradKeyOutput,
const aclTensor *gradValueOutput,
const aclTensor *query,
const aclTensor *key,
const aclTensor *encoderQuery,
const aclTensor *encoderKey,
const aclTensor *normQueryWeight,
const aclTensor *normQueryMean,
const aclTensor *normQueryRstd,
const aclTensor *normKeyWeight,
const aclTensor *normKeyMean,
const aclTensor *normKeyRstd,
const aclTensor *normAddedQueryWeight,
const aclTensor *normAddedQueryMean,
const aclTensor *normAddedQueryRstd,
const aclTensor *normAddedKeyWeight,
const aclTensor *normAddedKeyMean,
const aclTensor *normAddedKeyRstd,
const aclTensor *ropeSin,
const aclTensor *ropeCos,
int64_t normType,
int64_t normAddedType,
int64_t ropeType,
int64_t concatOrder,
const aclTensor *gradQuery,
const aclTensor *gradKey,
const aclTensor *gradValue,
const aclTensor *gradEncoderQuery,
const aclTensor *gradEncoderKey,
const aclTensor *gradEncoderValue,
const aclTensor *gradNormQueryWeight,
const aclTensor *gradNormQueryBias,
const aclTensor *gradNormKeyWeight,
const aclTensor *gradNormKeyBias,
const aclTensor *gradNormAddedQueryWeight,
const aclTensor *gradNormAddedQueryBias,
const aclTensor *gradNormAddedKeyWeight,
const aclTensor *gradNormAddedKeyBias,
uint64_t *workspaceSize,
aclOpExecutor **executor)aclnnStatus aclnnNormRopeConcatBackward(
void *workspace,
uint64_t workspaceSize,
aclOpExecutor *executor,
aclrtStream stream)aclnnNormRopeConcatBackwardGetWorkspaceSize
Parameters
Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor gradQueryOutput Input Backward gradient of the forward outputs of query and encoderQuery, corresponding to y in the formula. If encoderQuery is nullptr, seqEncoderQuery is 0. headDim must be an even number in the range of [1-1024]. FLOAT32, FLOAT16, BFLOAT16 ND [batch, headNum, seqQuery+seqEncoderQuery, headDim] √ gradKeyOutput Input Backward gradient of the forward outputs of key and encoderKey, corresponding to y in the formula. When encoderKey is nullptr, seqEncoderKey is 0. The data type is the same as that of gradQueryOutput. FLOAT32, FLOAT16, BFLOAT16 ND [batch, headNum, seqKey+seqEncoderKey, headDim] √ gradValueOutput Input Backward gradient of the forward outputs of value and encoderValue, corresponding to y in the formula. When encoderValue is nullptr, seqEncoderValue is 0. The length of seqValue is the same as that of seqKey. The length of seqEncoderValue is the same as that of seqEncoderKey. The data type is the same as that of gradQueryOutput. FLOAT32, FLOAT16, BFLOAT16 ND [batch, headNum, seqValue+seqEncoderValue, headDim] √ query Input query in the forward input (image Query in multimodal tasks), corresponding to x in the formula. Data type must be the same as that of gradQueryOutput. FLOAT32, FLOAT16, BFLOAT16 ND [batch, seqQuery, headNum, headDim] √ key Input Key in the forward input (image Key in the multimodal use case), corresponding to x in the formula. Data type is the same as that of the parameter gradQueryOutput. FLOAT32, FLOAT16, BFLOAT16 ND [batch, seqKey, headNum,headDim] √ encoderQuery Optional input encoderQuery in the forward input (text Query in the multi-modal), corresponding to x in the formula. Passed in when the text Query participates in training. The data type is the same as that of parameter gradQueryOutput. FLOAT32, FLOAT16, BFLOAT16 ND [batch, seqEncoderQuery, headNum, headDim] √ encoderKey Optional input encoderKey in the forward input (text Key in the multi-modal), corresponding to x in the formula. Passed when the text Key participates in training. The data type must be the same as that of the gradQueryOutput parameter. FLOAT32, FLOAT16, BFLOAT16 ND [batch, seqEncoderKey, headNum, headDim] √ normQueryWeight Optional input Weight value for the normalization operation on the query in the forward pass, corresponding to γ in the formula. Passed when LayerNorm normalization with affine is performed on the image Query and Key. The data type must be the same as that of the gradQueryOutput parameter. FLOAT32, FLOAT16, BFLOAT16 ND [headDim] √ normQueryMean Optional input Mean value output when the forward query is normalized, corresponding to μ in the formula. Pass in when image Query and Key are normalized. FLOAT32 ND [batch, seqQuery, headNum, 1] √ normQueryRstd Optional input Variance-related term output when the forward query is normalized, corresponding to rstd in the formula. Passed in when the image Query and Key undergo normalization. FLOAT32 ND [batch, seqQuery, headNum, 1] √ normKeyWeight Optional input Weight value of the key normalization operation in the forward pass, corresponding to γ in the formula. Passed when the image Query and Key undergo LayerNorm with affine. Data type must be the same as that of parameter gradQueryOutput. FLOAT32, FLOAT16, BFLOAT16 ND [headDim] √ normKeyMean Optional input Mean output of the key normalization operation in the forward pass, corresponding to μ in the formula. Passed in when normalization is performed on the image Query and Key. FLOAT32 ND [batch, seqKey, headNum, 1] √ normKeyRstd Optional input Variance-related term rstd output when the forward key is normalized. It corresponds to rstd in the formula. Passed in when normalization is performed on the image Query and Key. FLOAT32 ND [batch, seqKey, headNum, 1] √ normAddedQueryWeight Optional input Weight value for the normalization operation on the forward encoderQuery, corresponding to γ in the formula. Passed in when the text Query and Key undergo LayerNorm with affine. The data type must be the same as that of parameter gradQueryOutput. FLOAT32, FLOAT16, BFLOAT16 ND [headDim] √ normAddedQueryMean Optional input Mean output when encoderQuery is normalized in forward propagation, corresponding to μ in the formula. Required when the text Query and Key are normalized. FLOAT32 ND [batch, seqEncoderQuery, headNum, 1] √ normAddedQueryRstd Optional input Variance-related term output during normalization of encoderQuery in forward propagation, corresponding to rstd in the formula. Passed in when normalization is performed on the text Query and Key. FLOAT32 ND [batch, seqEncoderQuery, headNum, 1] √ normAddedKeyWeight Optional input Weight value for the normalization of encoderKey in forward propagation, corresponding to γ in the formula. Passed when the text Query and Key are normalized by LayerNorm with affine. The data type must be the same as that of parameter gradQueryOutput. FLOAT32, FLOAT16, BFLOAT16 ND [headDim] √ normAddedKeyMean Optional input Mean value output when the forward encoderKey is normalized, corresponding to μ in the formula. Passed when the text Query and Key are normalized. FLOAT32 ND [batch, seqEncoderKey, headNum, 1] √ normAddedKeyRstd Optional input Variance-related term output during the forward normalization of encoderKey, corresponding to rstd in the formula. Passed in when the text Query and Key are normalized. FLOAT32 ND [batch, seqEncoderKey, headNum, 1] √ ropeSin Optional input The sin value for the rotary position encoding operation on the forward input in the formula. Passed in when the rotary position encoding operation is performed on the image or text Query and Key. The seqRope length must be within [1-min(seqQuery+seqEncoderQuery, seqKey+seqEncoderKey)], and the data type must be the same as that of the parameter gradQueryOutput. FLOAT32, FLOAT16, BFLOAT16 ND [seqRope, headDim] √ ropeCos Optional input The cos value used in the rotary position encoding operation on the forward input in the formula. Passed when the rotary position encoding operation is performed on the Query and Key of images or text. The data type must be the same as that of gradQueryOutput. FLOAT32, FLOAT16, BFLOAT16 ND [seqRope, headDim] √ normType Optional input Specifies the normalization operation type of query and key. 0: no normalization. 1: layer normalization. 2: layer normalization with affine parameters. It is recommended to set this parameter to 0 if not specifically specified. INT64 - - - normAddedType Optional input Specifies the normalization type of encoderQuery and encoderKey. 0: no normalization. 1: layer normalization. 2: layer normalization with affine parameters. It is recommended to set this parameter to 0 if not specifically specified. INT64 - - - ropeType Optional input Specifies the type of the rotary position encoding operation performed on query and encoderQuery, and key and encoderKey after Concat. 0: no rotary position encoding operation, 1: Interleave-type rotary position encoding, 2: Half-type rotary position encoding. It is recommended to pass in 0 if not specifically specified. INT64 - - - concatOrder Optional input Specifies the stacking order of query and encoderQuery, key and encoderKey, value and encoderValue in the Concat operation. Taking query as an example, 0: [query, encoderQuery], 1: [encoderQuery, query]. If not specified, 0 is recommended. INT64 - - - gradQuery Output Backward gradient value of the network layer in the formula for the forward input query. Data type is the same as that of parameter gradQueryOutput. FLOAT32, FLOAT16, BFLOAT16 ND [batch, seqQuery, headNum, headDim] x gradKey Output Backward gradient value of the network layer in the formula for the forward input key. Data type is the same as that of parameter gradQueryOutput. FLOAT32, FLOAT16, BFLOAT16 ND [batch, seqKey, headNum, headDim] x gradValue Output Backward gradient value of the forward input value in the formula. Data type is the same as that of parameter gradQueryOutput. FLOAT32, FLOAT16, BFLOAT16 ND [batch, seqValue, headNum, headDim] x gradEncoderQuery Optional output Backward gradient value of the forward input encoderQuery computed by the network layer in the formula. Output when the text Query is involved in training. The data type is the same as that of parameter gradQueryOutput. FLOAT32, FLOAT16, BFLOAT16 ND [batch, seqEncoderQuery, headNum, headDim] x gradEncoderKey Optional output Backward gradient value of the forward input encoderKey in the formula. Output when the text Key participates in training. The data type is the same as that of parameter gradQueryOutput. FLOAT32, FLOAT16, BFLOAT16 ND [batch, seqEncoderKey, headNum, headDim] x gradEncoderValue Optional output Backward gradient value of the forward input encoderValue computed by the network layer in the formula. Output when the text Value participates in training. The data type is the same as that of gradQueryOutput. FLOAT32, FLOAT16, BFLOAT16 ND [batch, seqEncoderValue, headNum, headDim] x gradNormQueryWeight Optional output Backward gradient value of the γ weight used for normalizing the forward input query, computed by the network layer in the formula. Output when the image Query and Key are normalized using affine LayerNorm. The data type must be the same as that of gradQueryOutput. FLOAT32, FLOAT16, BFLOAT16 ND [headDim] x gradNormQueryBias Optional output Backward gradient of the β offset when the network layer in the formula normalizes the forward input query. Output when the image Query and Key are normalized using affine LayerNorm. The data type must be the same as that of gradQueryOutput. FLOAT32, FLOAT16, BFLOAT16 ND [headDim] x gradNormKeyWeight Optional output Backward gradient value of the γ weight for the normalization operation on the forward input key in the formula. Output when the image Query and Key are normalized using LayerNorm with affine. The data type must be the same as that of gradQueryOutput. FLOAT32, FLOAT16, BFLOAT16 ND [headDim] x gradNormKeyBias Optional output Backward gradient value of the β offset for the normalization operation performed by the network layer in the formula on the forward input key. Output when LayerNorm normalization with affine is performed on the image Query and Key. The data type is the same as that of gradQueryOutput. FLOAT32, FLOAT16, BFLOAT16 ND [headDim] x gradNormAddedQueryWeight Optional output Backward gradient of the γ weight for the normalization operation applied to the forward input encoderQuery in the formula. Output when LayerNorm normalization with affine is applied to the text Query and Key. The data type is the same as that of parameter gradQueryOutput. FLOAT32, FLOAT16, BFLOAT16 ND [headDim] x gradNormAddedQueryBias Optional output Backward gradient of the β offset for the normalization operation applied to the forward input encoderQuery in the formula. Output when LayerNorm normalization with affine is performed on the text Query and Key. The data type must be the same as that of gradQueryOutput. FLOAT32, FLOAT16, BFLOAT16 ND [headDim] x gradNormAddedKeyWeight Optional output Backward gradient of the γ weight used by the network layer in the formula to normalize the forward input encoderKey. Output when LayerNorm normalization with affine is performed on the text Query and Key. The data type must be the same as that of gradQueryOutput. FLOAT32, FLOAT16, BFLOAT16 ND [headDim] x gradNormAddedKeyBias Optional output Backward gradient of the β offset for the normalization operation that the network layer in the formula performs on the forward input encoderKey. Output when LayerNorm normalization with affine is performed on the text Query and Key. The data type is the same as that of gradQueryOutput. FLOAT32, FLOAT16, BFLOAT16 ND [headDim] x workspaceSize Output Returns the workspace size to be allocated on the Device. - - - - - executor Output Returns the op executor. It contains the operator computation process. - - - - - 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 null pointer exists in required inputs or required outputs of the computation. ACLNN_ERR_PARAM_INVALID 161002 The data types of computation inputs and outputs are out of the supported range.
aclnnNormRopeConcatBackward
Parameters
Parameter Input/Output Description workspace Input Workspace memory address allocated on the Device. workspaceSize Input Size of the workspace allocated on the Device, obtained by calling the first-phase API aclnnNormRopeConcatBackwardGetWorkspaceSize. executor Input op executor, containing the operator computation flow. stream Input Specifies the Stream for task execution. Return Value
aclnnStatus: return status code. For details, see aclnn Return Codes.
Constraints
- Deterministic computation:
- aclnnNormRopeConcatBackward defaults to a non-deterministic implementation. Deterministic computation can be enabled through aclrtCtxSetSysParamOpt.
Example
aclnn single-operator call method
The following is the sample code for calling a single operator through aclnn, for reference only. For details about compilation and execution, see Compile and Run Samples.
#include <algorithm> #include <cstdint> #include <iostream> #include <vector> #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> #include <fstream> #include <fcntl.h> #include "acl/acl.h" #include "aclnnop/aclnn_norm_rope_concat_grad.h" #define SUCCESS 0 #define FAILED 1 #define INFO_LOG(fmt, args...) fprintf(stdout, "[INFO] " fmt "\n", ##args) #define WARN_LOG(fmt, args...) fprintf(stdout, "[WARN] " fmt "\n", ##args) #define ERROR_LOG(fmt, args...) fprintf(stderr, "[ERROR] " fmt "\n", ##args) #define CHECK_RET(cond, return_expr) \ do { \ if (!(cond)) { \ return_expr; \ } \ } while (0) #define LOG_PRINT(message, ...) \ do { \ printf(message, ##__VA_ARGS__); \ } while (0) bool ReadFile(const std::string &filePath, size_t &fileSize, void *buffer, size_t bufferSize) { struct stat sBuf; int fileStatus = stat(filePath.data(), &sBuf); if (fileStatus == -1) { ERROR_LOG("failed to get file %s", filePath.c_str()); return false; } if (S_ISREG(sBuf.st_mode) == 0) { ERROR_LOG("%s is not a file, please enter a file", filePath.c_str()); return false; } std::ifstream file; file.open(filePath, std::ios::binary); if (!file.is_open()) { ERROR_LOG("Open file failed. path = %s", filePath.c_str()); return false; } std::filebuf *buf = file.rdbuf(); size_t size = buf->pubseekoff(0, std::ios::end, std::ios::in); if (size == 0) { ERROR_LOG("file size is 0"); file.close(); return false; } if (size > bufferSize) { ERROR_LOG("file size is larger than buffer size"); file.close(); return false; } buf->pubseekpos(0, std::ios::in); buf->sgetn(static_cast<char *>(buffer), size); fileSize = size; file.close(); return true; } 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, aclrtContext* context, aclrtStream* stream) { // Boilerplate. acl 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** xOrResult) { auto size = GetShapeSize(shape) * sizeof(T); // Call aclrtMalloc to allocate device 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 contiguous xOrResult. 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. *xOrResult = aclCreateTensor(shape.data(), shape.size(), dataType, strides.data(), 0, aclFormat::ACL_FORMAT_ND, shape.data(), shape.size(), *deviceAddr); return 0; } int main() { // 1. (boilerplate) Device/context/stream initialization. For details, see the ACL external API list. // Set deviceId based on your 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 input and output. Customize the construction based on the API. uint32_t batchSize = 1; uint32_t headNum = 10; uint32_t headDim = 6; uint32_t querySeq = 5; uint32_t keySeq = 5; uint32_t valueSeq = 5; uint32_t encoderQuerySeq = 3; uint32_t encoderKeySeq = 3; uint32_t encoderValueSeq = 3; uint32_t ropeSeq = 5; uint32_t ropeDim = 6; uint32_t normType = 2; uint32_t normAddedType = 2; uint32_t ropeType = 1; uint32_t concatOrder = 0; std::vector<int64_t> gradQueryOutputShape = {batchSize, headNum, querySeq + encoderQuerySeq, headDim}; std::vector<int64_t> gradKeyOutputShape = {batchSize, headNum, keySeq + encoderKeySeq, headDim}; std::vector<int64_t> gradValueOutputShape = {batchSize, headNum, valueSeq + encoderValueSeq, headDim}; std::vector<int64_t> queryShape = {batchSize, querySeq, headNum, headDim}; std::vector<int64_t> keyShape = {batchSize, keySeq, headNum, headDim}; std::vector<int64_t> encoderQueryShape = {batchSize, encoderQuerySeq, headNum, headDim}; std::vector<int64_t> encoderKeyShape = {batchSize, encoderKeySeq, headNum, headDim}; std::vector<int64_t> normQueryWeightShape = {headDim}; std::vector<int64_t> normQueryMeanShape = {batchSize, querySeq, headNum, 1}; std::vector<int64_t> normQueryRstdShape = {batchSize, querySeq, headNum, 1}; std::vector<int64_t> normKeyWeightShape = {headDim}; std::vector<int64_t> normKeyMeanShape = {batchSize, keySeq, headNum, 1}; std::vector<int64_t> normKeyRstdShape = {batchSize, keySeq, headNum, 1}; std::vector<int64_t> normAddedQueryWeightShape = {headDim}; std::vector<int64_t> normAddedQueryMeanShape = {batchSize, encoderQuerySeq, headNum, 1}; std::vector<int64_t> normAddedQueryRstdShape = {batchSize, encoderQuerySeq, headNum, 1}; std::vector<int64_t> normAddedKeyWeightShape = {headDim}; std::vector<int64_t> normAddedKeyMeanShape = {batchSize, encoderKeySeq, headNum, 1}; std::vector<int64_t> normAddedKeyRstdShape = {batchSize, encoderKeySeq, headNum, 1}; std::vector<int64_t> ropeSinShape = {ropeSeq, headDim}; std::vector<int64_t> ropeCosShape = {ropeSeq, headDim}; std::vector<int64_t> gradQueryShape = {batchSize, querySeq, headNum, headDim}; std::vector<int64_t> gradKeyShape = {batchSize, keySeq, headNum, headDim}; std::vector<int64_t> gradValueShape = {batchSize, valueSeq, headNum, headDim}; std::vector<int64_t> gradEncoderQueryShape = {batchSize, encoderQuerySeq, headNum, headDim}; std::vector<int64_t> gradEncoderKeyShape = {batchSize, encoderKeySeq, headNum, headDim}; std::vector<int64_t> gradEncoderValueShape = {batchSize, encoderValueSeq, headNum, headDim}; std::vector<int64_t> gradNormQueryWeightShape = {headDim}; std::vector<int64_t> gradNormQueryBiasShape = {headDim}; std::vector<int64_t> gradNormKeyWeightShape = {headDim}; std::vector<int64_t> gradNormKeyBiasShape = {headDim}; std::vector<int64_t> gradNormAddedQueryWeightShape = {headDim}; std::vector<int64_t> gradNormAddedQueryBiasShape = {headDim}; std::vector<int64_t> gradNormAddedKeyWeightShape = {headDim}; std::vector<int64_t> gradNormAddedKeyBiasShape = {headDim}; void* gradQueryOutputDeviceAddr = nullptr; void* gradKeyOutputDeviceAddr = nullptr; void* gradValueOutputDeviceAddr = nullptr; void* queryDeviceAddr = nullptr; void* keyDeviceAddr = nullptr; void* encoderQueryDeviceAddr = nullptr; void* encoderKeyDeviceAddr = nullptr; void* normQueryWeightDeviceAddr = nullptr; void* normQueryMeanDeviceAddr = nullptr; void* normQueryRstdDeviceAddr = nullptr; void* normKeyWeightDeviceAddr = nullptr; void* normKeyMeanDeviceAddr = nullptr; void* normKeyRstdDeviceAddr = nullptr; void* normAddedQueryWeightDeviceAddr = nullptr; void* normAddedQueryMeanDeviceAddr = nullptr; void* normAddedQueryRstdDeviceAddr = nullptr; void* normAddedKeyWeightDeviceAddr = nullptr; void* normAddedKeyMeanDeviceAddr = nullptr; void* normAddedKeyRstdDeviceAddr = nullptr; void* ropeSinDeviceAddr = nullptr; void* ropeCosDeviceAddr = nullptr; void* gradQueryDeviceAddr = nullptr; void* gradKeyDeviceAddr = nullptr; void* gradValueDeviceAddr = nullptr; void* gradEncoderQueryDeviceAddr = nullptr; void* gradEncoderKeyDeviceAddr = nullptr; void* gradEncoderValueDeviceAddr = nullptr; void* gradNormQueryWeightDeviceAddr = nullptr; void* gradNormQueryBiasDeviceAddr = nullptr; void* gradNormKeyWeightDeviceAddr = nullptr; void* gradNormKeyBiasDeviceAddr = nullptr; void* gradNormAddedQueryWeightDeviceAddr = nullptr; void* gradNormAddedQueryBiasDeviceAddr = nullptr; void* gradNormAddedKeyWeightDeviceAddr = nullptr; void* gradNormAddedKeyBiasDeviceAddr = nullptr; aclTensor* gradQueryOutput = nullptr; aclTensor* gradKeyOutput = nullptr; aclTensor* gradValueOutput = nullptr; aclTensor* query = nullptr; aclTensor* key = nullptr; aclTensor* encoderQuery = nullptr; aclTensor* encoderKey = nullptr; aclTensor* normQueryWeight = nullptr; aclTensor* normQueryMean = nullptr; aclTensor* normQueryRstd = nullptr; aclTensor* normKeyWeight = nullptr; aclTensor* normKeyMean = nullptr; aclTensor* normKeyRstd = nullptr; aclTensor* normAddedQueryWeight = nullptr; aclTensor* normAddedQueryMean = nullptr; aclTensor* normAddedQueryRstd = nullptr; aclTensor* normAddedKeyWeight = nullptr; aclTensor* normAddedKeyMean = nullptr; aclTensor* normAddedKeyRstd = nullptr; aclTensor* ropeSin = nullptr; aclTensor* ropeCos = nullptr; aclTensor* gradQuery = nullptr; aclTensor* gradKey = nullptr; aclTensor* gradValue = nullptr; aclTensor* gradEncoderQuery = nullptr; aclTensor* gradEncoderKey = nullptr; aclTensor* gradEncoderValue = nullptr; aclTensor* gradNormQueryWeight = nullptr; aclTensor* gradNormQueryBias = nullptr; aclTensor* gradNormKeyWeight = nullptr; aclTensor* gradNormKeyBias = nullptr; aclTensor* gradNormAddedQueryWeight = nullptr; aclTensor* gradNormAddedQueryBias = nullptr; aclTensor* gradNormAddedKeyWeight = nullptr; aclTensor* gradNormAddedKeyBias = nullptr; std::vector<float> gradQueryOutputHostData(batchSize * headNum * (querySeq + encoderQuerySeq) * headDim, 1.0); std::vector<float> gradKeyOutputHostData(batchSize * headNum * (keySeq + encoderKeySeq) * headDim, 1.0); std::vector<float> gradValueOutputHostData(batchSize * headNum * (valueSeq + encoderValueSeq) * headDim, 1.0); std::vector<float> queryHostData(batchSize * headNum * querySeq * headDim, 1.0); std::vector<float> keyHostData(batchSize * headNum * keySeq * headDim, 1.0); std::vector<float> encoderQueryHostData(batchSize * headNum * encoderQuerySeq * headDim, 1.0); std::vector<float> encoderKeyHostData(batchSize * headNum * encoderKeySeq * headDim, 1.0); std::vector<float> normQueryWeightHostData(headDim, 1.0); std::vector<float> normQueryMeanHostData(batchSize * headNum * querySeq * 1, 0.0); std::vector<float> normQueryRstdHostData(batchSize * headNum * querySeq * 1, 1.0); std::vector<float> normKeyWeightHostData(headDim, 1.0); std::vector<float> normKeyMeanHostData(batchSize * headNum * keySeq * 1, 0.0); std::vector<float> normKeyRstdHostData(batchSize * headNum * keySeq * 1, 1.0); std::vector<float> normAddedQueryWeightHostData(headDim, 1.0); std::vector<float> normAddedQueryMeanHostData(batchSize * headNum * encoderQuerySeq * 1, 1.0); std::vector<float> normAddedQueryRstdHostData(batchSize * headNum * encoderQuerySeq * 1, 1.0); std::vector<float> normAddedKeyWeightHostData(headDim, 1.0); std::vector<float> normAddedKeyMeanHostData(batchSize * headNum * encoderKeySeq * 1, 0.0); std::vector<float> normAddedKeyRstdHostData(batchSize * headNum * encoderKeySeq * 1, 1.0); std::vector<float> ropeSinHostData(ropeSeq * headDim, 1.0); std::vector<float> ropeCosHostData(ropeSeq * headDim, 1.0); std::vector<float> gradQueryHostData(batchSize * headNum * querySeq * headDim, 0.0); std::vector<float> gradKeyHostData(batchSize * headNum * keySeq * headDim, 0.0); std::vector<float> gradValueHostData(batchSize * headNum * valueSeq * headDim, 0.0); std::vector<float> gradEncoderQueryHostData(batchSize * headNum * encoderQuerySeq * headDim, 0.0); std::vector<float> gradEncoderKeyHostData(batchSize * headNum * encoderKeySeq * headDim, 0.0); std::vector<float> gradEncoderValueHostData(batchSize * headNum * encoderValueSeq * headDim, 0.0); std::vector<float> gradNormQueryWeightHostData(headDim, 0.0); std::vector<float> gradNormQueryBiasHostData(headDim, 0.0); std::vector<float> gradNormKeyWeightHostData(headDim, 0.0); std::vector<float> gradNormKeyBiasHostData(headDim, 0.0); std::vector<float> gradNormAddedQueryWeightHostData(headDim, 0.0); std::vector<float> gradNormAddedQueryBiasHostData(headDim, 0.0); std::vector<float> gradNormAddedKeyWeightHostData(headDim, 0.0); std::vector<float> gradNormAddedKeyBiasHostData(headDim, 0.0); ret = CreateAclTensor(gradQueryOutputHostData, gradQueryOutputShape, &gradQueryOutputDeviceAddr, aclDataType::ACL_FLOAT, &gradQueryOutput); CHECK_RET(ret == ACL_SUCCESS, return ret); ret = CreateAclTensor(gradKeyOutputHostData, gradKeyOutputShape, &gradKeyOutputDeviceAddr, aclDataType::ACL_FLOAT, &gradKeyOutput); CHECK_RET(ret == ACL_SUCCESS, return ret); ret = CreateAclTensor(gradValueOutputHostData, gradValueOutputShape, &gradValueOutputDeviceAddr, aclDataType::ACL_FLOAT, &gradValueOutput); CHECK_RET(ret == ACL_SUCCESS, return ret); ret = CreateAclTensor(queryHostData, queryShape, &queryDeviceAddr, aclDataType::ACL_FLOAT, &query); CHECK_RET(ret == ACL_SUCCESS, return ret); ret = CreateAclTensor(keyHostData, keyShape, &keyDeviceAddr, aclDataType::ACL_FLOAT, &key); CHECK_RET(ret == ACL_SUCCESS, return ret); ret = CreateAclTensor(encoderQueryHostData, encoderQueryShape, &encoderQueryDeviceAddr, aclDataType::ACL_FLOAT, &encoderQuery); CHECK_RET(ret == ACL_SUCCESS, return ret); ret = CreateAclTensor(encoderKeyHostData, encoderKeyShape, &encoderKeyDeviceAddr, aclDataType::ACL_FLOAT, &encoderKey); CHECK_RET(ret == ACL_SUCCESS, return ret); ret = CreateAclTensor(normQueryWeightHostData, normQueryWeightShape, &normQueryWeightDeviceAddr, aclDataType::ACL_FLOAT, &normQueryWeight); CHECK_RET(ret == ACL_SUCCESS, return ret); ret = CreateAclTensor(normQueryMeanHostData, normQueryMeanShape, &normQueryMeanDeviceAddr, aclDataType::ACL_FLOAT, &normQueryMean); CHECK_RET(ret == ACL_SUCCESS, return ret); ret = CreateAclTensor(normQueryRstdHostData, normQueryRstdShape, &normQueryRstdDeviceAddr, aclDataType::ACL_FLOAT, &normQueryRstd); CHECK_RET(ret == ACL_SUCCESS, return ret); ret = CreateAclTensor(normKeyWeightHostData, normKeyWeightShape, &normKeyWeightDeviceAddr, aclDataType::ACL_FLOAT, &normKeyWeight); CHECK_RET(ret == ACL_SUCCESS, return ret); ret = CreateAclTensor(normKeyMeanHostData, normKeyMeanShape, &normKeyMeanDeviceAddr, aclDataType::ACL_FLOAT, &normKeyMean); CHECK_RET(ret == ACL_SUCCESS, return ret); ret = CreateAclTensor(normKeyRstdHostData, normKeyRstdShape, &normKeyRstdDeviceAddr, aclDataType::ACL_FLOAT, &normKeyRstd); CHECK_RET(ret == ACL_SUCCESS, return ret); ret = CreateAclTensor(normAddedQueryWeightHostData, normAddedQueryWeightShape, &normAddedQueryWeightDeviceAddr, aclDataType::ACL_FLOAT, &normAddedQueryWeight); CHECK_RET(ret == ACL_SUCCESS, return ret); ret = CreateAclTensor(normAddedQueryMeanHostData, normAddedQueryMeanShape, &normAddedQueryMeanDeviceAddr, aclDataType::ACL_FLOAT, &normAddedQueryMean); CHECK_RET(ret == ACL_SUCCESS, return ret); ret = CreateAclTensor(normAddedQueryRstdHostData, normAddedQueryRstdShape, &normAddedQueryRstdDeviceAddr, aclDataType::ACL_FLOAT, &normAddedQueryRstd); CHECK_RET(ret == ACL_SUCCESS, return ret); ret = CreateAclTensor(normAddedKeyWeightHostData, normAddedKeyWeightShape, &normAddedKeyWeightDeviceAddr, aclDataType::ACL_FLOAT, &normAddedKeyWeight); CHECK_RET(ret == ACL_SUCCESS, return ret); ret = CreateAclTensor(normAddedKeyMeanHostData, normAddedKeyMeanShape, &normAddedKeyMeanDeviceAddr, aclDataType::ACL_FLOAT, &normAddedKeyMean); CHECK_RET(ret == ACL_SUCCESS, return ret); ret = CreateAclTensor(normAddedKeyRstdHostData, normAddedKeyRstdShape, &normAddedKeyRstdDeviceAddr, aclDataType::ACL_FLOAT, &normAddedKeyRstd); CHECK_RET(ret == ACL_SUCCESS, return ret); ret = CreateAclTensor(ropeSinHostData, ropeSinShape, &ropeSinDeviceAddr, aclDataType::ACL_FLOAT, &ropeSin); CHECK_RET(ret == ACL_SUCCESS, return ret); ret = CreateAclTensor(ropeCosHostData, ropeCosShape, &ropeCosDeviceAddr, aclDataType::ACL_FLOAT, &ropeCos); CHECK_RET(ret == ACL_SUCCESS, return ret); ret = CreateAclTensor(gradQueryHostData, gradQueryShape, &gradQueryDeviceAddr, aclDataType::ACL_FLOAT, &gradQuery); CHECK_RET(ret == ACL_SUCCESS, return ret); ret = CreateAclTensor(gradKeyHostData, gradKeyShape, &gradKeyDeviceAddr, aclDataType::ACL_FLOAT, &gradKey); CHECK_RET(ret == ACL_SUCCESS, return ret); ret = CreateAclTensor(gradValueHostData, gradValueShape, &gradValueDeviceAddr, aclDataType::ACL_FLOAT, &gradValue); CHECK_RET(ret == ACL_SUCCESS, return ret); ret = CreateAclTensor(gradEncoderQueryHostData, gradEncoderQueryShape, &gradEncoderQueryDeviceAddr, aclDataType::ACL_FLOAT, &gradEncoderQuery); CHECK_RET(ret == ACL_SUCCESS, return ret); ret = CreateAclTensor(gradEncoderKeyHostData, gradEncoderKeyShape, &gradEncoderKeyDeviceAddr, aclDataType::ACL_FLOAT, &gradEncoderKey); CHECK_RET(ret == ACL_SUCCESS, return ret); ret = CreateAclTensor(gradEncoderValueHostData, gradEncoderValueShape, &gradEncoderValueDeviceAddr, aclDataType::ACL_FLOAT, &gradEncoderValue); CHECK_RET(ret == ACL_SUCCESS, return ret); ret = CreateAclTensor(gradNormQueryWeightHostData, gradNormQueryWeightShape, &gradNormQueryWeightDeviceAddr, aclDataType::ACL_FLOAT, &gradNormQueryWeight); CHECK_RET(ret == ACL_SUCCESS, return ret); ret = CreateAclTensor(gradNormQueryBiasHostData, gradNormQueryBiasShape, &gradNormQueryBiasDeviceAddr, aclDataType::ACL_FLOAT, &gradNormQueryBias); CHECK_RET(ret == ACL_SUCCESS, return ret); ret = CreateAclTensor(gradNormKeyWeightHostData, gradNormKeyWeightShape, &gradNormKeyWeightDeviceAddr, aclDataType::ACL_FLOAT, &gradNormKeyWeight); CHECK_RET(ret == ACL_SUCCESS, return ret); ret = CreateAclTensor(gradNormKeyBiasHostData, gradNormKeyBiasShape, &gradNormKeyBiasDeviceAddr, aclDataType::ACL_FLOAT, &gradNormKeyBias); CHECK_RET(ret == ACL_SUCCESS, return ret); ret = CreateAclTensor(gradNormAddedQueryWeightHostData, gradNormAddedQueryWeightShape, &gradNormAddedQueryWeightDeviceAddr, aclDataType::ACL_FLOAT, &gradNormAddedQueryWeight); CHECK_RET(ret == ACL_SUCCESS, return ret); ret = CreateAclTensor(gradNormAddedQueryBiasHostData, gradNormAddedQueryBiasShape, &gradNormAddedQueryBiasDeviceAddr, aclDataType::ACL_FLOAT, &gradNormAddedQueryBias); CHECK_RET(ret == ACL_SUCCESS, return ret); ret = CreateAclTensor(gradNormAddedKeyWeightHostData, gradNormAddedKeyWeightShape, &gradNormAddedKeyWeightDeviceAddr, aclDataType::ACL_FLOAT, &gradNormAddedKeyWeight); CHECK_RET(ret == ACL_SUCCESS, return ret); ret = CreateAclTensor(gradNormAddedKeyBiasHostData, gradNormAddedKeyBiasShape, &gradNormAddedKeyBiasDeviceAddr, aclDataType::ACL_FLOAT, &gradNormAddedKeyBias); CHECK_RET(ret == ACL_SUCCESS, return ret); // 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 aclnnGeGluBackward. ret = aclnnNormRopeConcatBackwardGetWorkspaceSize( gradQueryOutput, gradKeyOutput, gradValueOutput, query, key, encoderQuery, encoderKey, normQueryWeight, normQueryMean, normQueryRstd, normKeyWeight, normKeyMean, normKeyRstd, normAddedQueryWeight, normAddedQueryMean, normAddedQueryRstd, normAddedKeyWeight, normAddedKeyMean, normAddedKeyRstd, ropeSin, ropeCos, normType, normAddedType, ropeType, concatOrder, gradQuery, gradKey, gradValue, gradEncoderQuery, gradEncoderKey, gradEncoderValue, gradNormQueryWeight, gradNormQueryBias, gradNormKeyWeight, gradNormKeyBias, gradNormAddedQueryWeight, gradNormAddedQueryBias, gradNormAddedKeyWeight, gradNormAddedKeyBias, & workspaceSize, & executor); CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnNormRopeConcatBackwardGetWorkspaceSize failed. ERROR: %d\n", ret); return ret); // Allocate device memory using the workspaceSize calculated by the first-phase API. void* workspaceAddr = nullptr; if (workspaceSize > static_cast<uint64_t>(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 of aclnnGeGluBackward. ret = aclnnNormRopeConcatBackward(workspaceAddr, workspaceSize, executor, stream); CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnNormRopeConcatBackward failed. ERROR: %d\n", ret); return ret); // 4. (boilerplate) 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); // 5. Obtain the output values and copy the results from device memory to host memory. Modify it according to the API definition. auto size = GetShapeSize(gradQueryShape); std::vector<float> gradQueryData(size, 0); ret = aclrtMemcpy(gradQueryData.data(), gradQueryData.size() * sizeof(gradQueryData[0]), gradQueryDeviceAddr, size * sizeof(gradQueryData[0]), ACL_MEMCPY_DEVICE_TO_HOST); CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy gradQuery result from device to host failed. ERROR: %d\n", ret); return ret); for (int64_t i = 0; i < size; i++) { LOG_PRINT("gradQuery result[%ld] is: %f\n", i, gradQueryData[i]); } size = GetShapeSize(gradKeyShape); std::vector<float> gradKeyData(size, 0); ret = aclrtMemcpy(gradKeyData.data(), gradKeyData.size() * sizeof(gradKeyData[0]), gradKeyDeviceAddr, size * sizeof(gradKeyData[0]), ACL_MEMCPY_DEVICE_TO_HOST); CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy gradKey result from device to host failed. ERROR: %d\n", ret); return ret); for (int64_t i = 0; i < size; i++) { LOG_PRINT("gradKey result[%ld] is: %f\n", i, gradKeyData[i]); } size = GetShapeSize(gradValueShape); std::vector<float> gradValueData(size, 0); ret = aclrtMemcpy(gradValueData.data(), gradValueData.size() * sizeof(gradValueData[0]), gradValueDeviceAddr, size * sizeof(gradValueData[0]), ACL_MEMCPY_DEVICE_TO_HOST); CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy gradValue result from device to host failed. ERROR: %d\n", ret); return ret); for (int64_t i = 0; i < size; i++) { LOG_PRINT("gradValue result[%ld] is: %f\n", i, gradValueData[i]); } size = GetShapeSize(gradEncoderQueryShape); std::vector<float> gradEncoderQueryData(size, 0); ret = aclrtMemcpy(gradEncoderQueryData.data(), gradEncoderQueryData.size() * sizeof(gradEncoderQueryData[0]), gradEncoderQueryDeviceAddr, size * sizeof(gradEncoderQueryData[0]), ACL_MEMCPY_DEVICE_TO_HOST); CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy gradEncoderQuery result from device to host failed. ERROR: %d\n", ret); return ret); for (int64_t i = 0; i < size; i++) { LOG_PRINT("gradEncoderQuery result[%ld] is: %f\n", i, gradEncoderQueryData[i]); } size = GetShapeSize(gradEncoderKeyShape); std::vector<float> gradEncoderKeyData(size, 0); ret = aclrtMemcpy(gradEncoderKeyData.data(), gradEncoderKeyData.size() * sizeof(gradEncoderKeyData[0]), gradEncoderKeyDeviceAddr, size * sizeof(gradEncoderKeyData[0]), ACL_MEMCPY_DEVICE_TO_HOST); CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy gradEncoderKey result from device to host failed. ERROR: %d\n", ret); return ret); for (int64_t i = 0; i < size; i++) { LOG_PRINT("gradEncoderKey result[%ld] is: %f\n", i, gradEncoderKeyData[i]); } size = GetShapeSize(gradEncoderValueShape); std::vector<float> gradEncoderValueData(size, 0); ret = aclrtMemcpy(gradEncoderValueData.data(), gradEncoderValueData.size() * sizeof(gradEncoderValueData[0]), gradEncoderValueDeviceAddr, size * sizeof(gradEncoderValueData[0]), ACL_MEMCPY_DEVICE_TO_HOST); CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy gradEncoderValue result from device to host failed. ERROR: %d\n", ret); return ret); for (int64_t i = 0; i < size; i++) { LOG_PRINT("gradEncoderValue result[%ld] is: %f\n", i, gradEncoderValueData[i]); } size = GetShapeSize(gradNormQueryWeightShape); std::vector<float> gradNormQueryWeightData(size, 0); ret = aclrtMemcpy(gradNormQueryWeightData.data(), gradNormQueryWeightData.size() * sizeof(gradNormQueryWeightData[0]), gradNormQueryWeightDeviceAddr, size * sizeof(gradNormQueryWeightData[0]), ACL_MEMCPY_DEVICE_TO_HOST); CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy gradNormQueryWeight result from device to host failed. ERROR: %d\n", ret); return ret); for (int64_t i = 0; i < size; i++) { LOG_PRINT("gradNormQueryWeight result[%ld] is: %f\n", i, gradNormQueryWeightData[i]); } size = GetShapeSize(gradNormQueryBiasShape); std::vector<float> gradNormQueryBiasData(size, 0); ret = aclrtMemcpy(gradNormQueryBiasData.data(), gradNormQueryBiasData.size() * sizeof(gradNormQueryBiasData[0]), gradNormQueryBiasDeviceAddr, size * sizeof(gradNormQueryBiasData[0]), ACL_MEMCPY_DEVICE_TO_HOST); CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy gradNormQueryBias result from device to host failed. ERROR: %d\n", ret); return ret); for (int64_t i = 0; i < size; i++) { LOG_PRINT("gradNormQueryBias result[%ld] is: %f\n", i, gradNormQueryBiasData[i]); } size = GetShapeSize(gradNormKeyWeightShape); std::vector<float> gradNormKeyWeightData(size, 0); ret = aclrtMemcpy(gradNormKeyWeightData.data(), gradNormKeyWeightData.size() * sizeof(gradNormKeyWeightData[0]), gradNormKeyWeightDeviceAddr, size * sizeof(gradNormKeyWeightData[0]), ACL_MEMCPY_DEVICE_TO_HOST); CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy gradNormKeyWeight result from device to host failed. ERROR: %d\n", ret); return ret); for (int64_t i = 0; i < size; i++) { LOG_PRINT("gradNormKeyWeight result[%ld] is: %f\n", i, gradNormKeyWeightData[i]); } size = GetShapeSize(gradNormKeyBiasShape); std::vector<float> gradNormKeyBiasData(size, 0); ret = aclrtMemcpy(gradNormKeyBiasData.data(), gradNormKeyBiasData.size() * sizeof(gradNormKeyBiasData[0]), gradNormKeyBiasDeviceAddr, size * sizeof(gradNormKeyBiasData[0]), ACL_MEMCPY_DEVICE_TO_HOST); CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy gradNormKeyBias result from device to host failed. ERROR: %d\n", ret); return ret); for (int64_t i = 0; i < size; i++) { LOG_PRINT("gradNormKeyBias result[%ld] is: %f\n", i, gradNormKeyBiasData[i]); } size = GetShapeSize(gradNormAddedQueryWeightShape); std::vector<float> gradNormAddedQueryWeightData(size, 0); ret = aclrtMemcpy(gradNormAddedQueryWeightData.data(), gradNormAddedQueryWeightData.size() * sizeof(gradNormAddedQueryWeightData[0]), gradNormAddedQueryWeightDeviceAddr, size * sizeof(gradNormAddedQueryWeightData[0]), ACL_MEMCPY_DEVICE_TO_HOST); CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy gradNormAddedQueryWeight result from device to host failed. ERROR: %d\n", ret); return ret); for (int64_t i = 0; i < size; i++) { LOG_PRINT("gradNormAddedQueryWeight result[%ld] is: %f\n", i, gradNormAddedQueryWeightData[i]); } size = GetShapeSize(gradNormAddedQueryBiasShape); std::vector<float> gradNormAddedQueryBiasData(size, 0); ret = aclrtMemcpy(gradNormAddedQueryBiasData.data(), gradNormAddedQueryBiasData.size() * sizeof(gradNormAddedQueryBiasData[0]), gradNormAddedQueryBiasDeviceAddr, size * sizeof(gradNormAddedQueryBiasData[0]), ACL_MEMCPY_DEVICE_TO_HOST); CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy gradNormAddedQueryBias result from device to host failed. ERROR: %d\n", ret); return ret); for (int64_t i = 0; i < size; i++) { LOG_PRINT("gradNormAddedQueryBias result[%ld] is: %f\n", i, gradNormAddedQueryBiasData[i]); } size = GetShapeSize(gradNormAddedKeyWeightShape); std::vector<float> gradNormAddedKeyWeightData(size, 0); ret = aclrtMemcpy(gradNormAddedKeyWeightData.data(), gradNormAddedKeyWeightData.size() * sizeof(gradNormAddedKeyWeightData[0]), gradNormAddedKeyWeightDeviceAddr, size * sizeof(gradNormAddedKeyWeightData[0]), ACL_MEMCPY_DEVICE_TO_HOST); CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy gradNormAddedKeyWeight result from device to host failed. ERROR: %d\n", ret); return ret); for (int64_t i = 0; i < size; i++) { LOG_PRINT("gradNormAddedKeyWeight result[%ld] is: %f\n", i, gradNormAddedKeyWeightData[i]); } size = GetShapeSize(gradNormAddedKeyBiasShape); std::vector<float> gradNormAddedKeyBiasData(size, 0); ret = aclrtMemcpy(gradNormAddedKeyBiasData.data(), gradNormAddedKeyBiasData.size() * sizeof(gradNormAddedKeyBiasData[0]), gradNormAddedKeyBiasDeviceAddr, size * sizeof(gradNormAddedKeyBiasData[0]), ACL_MEMCPY_DEVICE_TO_HOST); CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy gradNormAddedKeyBias result from device to host failed. ERROR: %d\n", ret); return ret); for (int64_t i = 0; i < size; i++) { LOG_PRINT("gradNormAddedKeyBias result[%ld] is: %f\n", i, gradNormAddedKeyBiasData[i]); } // 6. Release aclTensor and aclScalar. Modify it according to the API definition. aclDestroyTensor(gradQueryOutput); aclDestroyTensor(gradKeyOutput); aclDestroyTensor(gradValueOutput); aclDestroyTensor(query); aclDestroyTensor(key); aclDestroyTensor(encoderQuery); aclDestroyTensor(encoderKey); aclDestroyTensor(normQueryWeight); aclDestroyTensor(normQueryMean); aclDestroyTensor(normQueryRstd); aclDestroyTensor(normKeyWeight); aclDestroyTensor(normKeyMean); aclDestroyTensor(normKeyRstd); aclDestroyTensor(normAddedQueryWeight); aclDestroyTensor(normAddedQueryMean); aclDestroyTensor(normAddedQueryRstd); aclDestroyTensor(normAddedKeyWeight); aclDestroyTensor(normAddedKeyMean); aclDestroyTensor(normAddedKeyRstd); aclDestroyTensor(ropeSin); aclDestroyTensor(ropeCos); aclDestroyTensor(gradQuery); aclDestroyTensor(gradKey); aclDestroyTensor(gradValue); aclDestroyTensor(gradEncoderQuery); aclDestroyTensor(gradEncoderKey); aclDestroyTensor(gradEncoderValue); aclDestroyTensor(gradNormQueryWeight); aclDestroyTensor(gradNormQueryBias); aclDestroyTensor(gradNormKeyWeight); aclDestroyTensor(gradNormKeyBias); aclDestroyTensor(gradNormAddedQueryWeight); aclDestroyTensor(gradNormAddedQueryBias); aclDestroyTensor(gradNormAddedKeyWeight); aclDestroyTensor(gradNormAddedKeyBias); // 7. Release device resources. Modify it according to the API definition. aclrtFree(gradQueryOutputDeviceAddr); aclrtFree(gradKeyOutputDeviceAddr); aclrtFree(gradValueOutputDeviceAddr); aclrtFree(queryDeviceAddr); aclrtFree(keyDeviceAddr); aclrtFree(encoderQueryDeviceAddr); aclrtFree(encoderKeyDeviceAddr); aclrtFree(normQueryWeightDeviceAddr); aclrtFree(normQueryMeanDeviceAddr); aclrtFree(normQueryRstdDeviceAddr); aclrtFree(normKeyWeightDeviceAddr); aclrtFree(normKeyMeanDeviceAddr); aclrtFree(normKeyRstdDeviceAddr); aclrtFree(normAddedQueryWeightDeviceAddr); aclrtFree(normAddedQueryMeanDeviceAddr); aclrtFree(normAddedQueryRstdDeviceAddr); aclrtFree(normAddedKeyWeightDeviceAddr); aclrtFree(normAddedKeyMeanDeviceAddr); aclrtFree(normAddedKeyRstdDeviceAddr); aclrtFree(ropeSinDeviceAddr); aclrtFree(ropeCosDeviceAddr); aclrtFree(gradQueryDeviceAddr); aclrtFree(gradKeyDeviceAddr); aclrtFree(gradValueDeviceAddr); aclrtFree(gradEncoderQueryDeviceAddr); aclrtFree(gradEncoderKeyDeviceAddr); aclrtFree(gradEncoderValueDeviceAddr); aclrtFree(gradNormQueryWeightDeviceAddr); aclrtFree(gradNormQueryBiasDeviceAddr); aclrtFree(gradNormKeyWeightDeviceAddr); aclrtFree(gradNormKeyBiasDeviceAddr); aclrtFree(gradNormAddedQueryWeightDeviceAddr); aclrtFree(gradNormAddedQueryBiasDeviceAddr); aclrtFree(gradNormAddedKeyWeightDeviceAddr); aclrtFree(gradNormAddedKeyBiasDeviceAddr); if (workspaceSize > static_cast<uint64_t>(0)) { aclrtFree(workspaceAddr); } aclrtDestroyStream(stream); aclrtDestroyContext(context); aclrtResetDevice(deviceId); aclFinalize(); return 0; }