aclnnInplacePartialRotaryMulGrad
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 InplacePartialRotaryMul for partial rotary position embedding. This operator computes the rotary position embedding gradients on the [start, end) slice along dimension D of input dy, and writes back the result to dy in place.
Formula:
In the forward computation of the rotary position embedding, the list of broadcast axes is
$ dy' = dy[..., start:end] $ $ cos' = cos[..., start:end] $ $ sin' = sin[..., start:end] $dims, and the slice range along dimension D is[start, end). Let the slice data involved in the computation be:The gradient computation formula is as follows:
(1) half mode (rotary_mode equal to 0):
$$ dy1', dy2' = chunk(dy', chunks=2, dim=-1) $$$$ cos1', cos2' = chunk(cos', chunks=2, dim=-1) $$$$ sin1', sin2' = chunk(sin', chunks=2, dim=-1) $$$$ dx' = cat((cos1' * dy1' + sin2' * dy2', cos2' * dy2' - sin1' * dy1'), dim=-1) $$The result of dx' is written back in place to the [start, end) range of dy.
(2) interleave mode (rotary_mode equal to 1):
$$ dy1', dy2' = dy'[..., :: 2], dy'[..., 1 :: 2] $$$$ cos1', cos2' = cos'[..., :: 2], cos'[..., 1 :: 2] $$$$ sin1', sin2' = sin'[..., :: 2], sin'[..., 1 :: 2] $$$$ dx' = stack((cos1' * dy1' + sin2' * dy2', cos2' * dy2' - sin1' * dy1'), dim=-1).reshape(dy'.shape) $$The result of dx' is written back in place to the [start, end) range of dy.
(3) quarter mode (rotary_mode is equal to 2):
$$ dy1', dy2', dy3', dy4' = chunk(dy', chunks=4, dim=-1) $$$$ cos1', cos2', cos3', cos4' = chunk(cos', chunks=4, dim=-1) $$$$ sin1', sin2', sin3', sin4' = chunk(sin', chunks=4, dim=-1) $$$$ dx' = cat((cos1' * dy1' + sin2' * dy2', cos2' * dy2' - sin1' * dy1', cos3' * dy3' + sin4' * dy4', cos4' * dy4' - sin3' * dy3'), dim=-1) $$The result of dx' is written back in place to the [start, end) range of dy.
(4) interleave-half mode (rotary_mode is equal to 3):
$$ dy1', dy2' = chunk(dy', chunks=2, dim=-1) $$$$ cos1', cos2' = chunk(cos', chunks=2, dim=-1) $$$$ sin1', sin2' = chunk(sin', chunks=2, dim=-1) $$$$ dx' = stack((cos1' * dy1' + sin2' * dy2', cos2' * dy2' - sin1' * dy1'), dim=-1).reshape(dy'.shape) $$The result of dx' is written back in place to the [start, end) range of dy.
Function Prototype
Each operator uses a two-phase API. You must first call the "aclnnInplacePartialRotaryMulGradGetWorkspaceSize" API to obtain the input parameters and calculate the required workspace size according to the process, and then call the "aclnnInplacePartialRotaryMulGrad" API to perform the computation.
aclnnStatus aclnnInplacePartialRotaryMulGradGetWorkspaceSize(
aclTensor *dyRef,
const aclTensor *cos,
const aclTensor *sin,
int64_t rotaryMode,
const aclIntArray *partialSlice,
uint64_t *workspaceSize,
aclOpExecutor **executor)aclnnStatus aclnnInplacePartialRotaryMulGrad(
void *workspace,
uint64_t workspaceSize,
aclOpExecutor *executor,
aclrtStream stream)aclnnInplacePartialRotaryMulGradGetWorkspaceSize
Parameters
Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor dyRef Input Derivative of the forward output y, updated inplace to the derivative of the forward input x. In inplace mode, dyRef also serves as the output where the result is written back. - BFLOAT16, FLOAT16, FLOAT32 ND 4 × cos Input Forward computation input cos. Same data type as sin. BFLOAT16, FLOAT16, FLOAT32 ND 4 × sin Input Input sin of forward computation. Same data type as cos. BFLOAT16, FLOAT16, FLOAT32 ND 4 × rotaryMode Input Rotation mode. 0=half, 1=interleave, 2=quarter, 3=interleave-half. - INT64 - - - partialSlice Input Slice range on the last dimension (D dimension) of dyRef, in the format [start, end), indicating that the gradient computation of rotary position embedding is performed on dyRef[..., start:end]. - IntArray - - - workspaceSize Output Returns the workspace size to be allocated on the Device. - - - - - executor Output Returns the op executor that 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 Values Error Code Description ACLNN_ERR_PARAM_NULLPTR 161001 The required inputs dyRef, cos, and sin are null pointers. ACLNN_ERR_PARAM_INVALID 161002 The data types and formats of the inputs dyRef, cos, and sin are not in the supported range. ACLNN_ERR_INNER_TILING_ERROR 561002 The shapes of the input parameters do not meet the conditions in the Constraints section. The rotaryMode parameter is not in the range of 0, 1, 2, or 3.
aclnnInplacePartialRotaryMulGrad
Parameters
Parameter Input/Output Description workspace Input Address of the workspace memory allocated on the Device. workspaceSize Input Size of the workspace memory allocated on the Device, obtained by the first-phase API aclnnInplacePartialRotaryMulGradGetWorkspaceSize. executor Input op executor, which contains the operator computation flow. stream Input Stream used to execute the task. Return Value
aclnnStatus: return status code. For details, see aclnn Return Codes.
Constraints
This operator is supported only on Ascend 950 AI Processor.
This operator supports only contiguous tensors; non-contiguous tensors are not supported.
The current version of this operator supports only the interleave mode (
rotary_mode=1). The half (0), quarter (2), and interleave-half (3) modes are not implemented yet.Deterministic computation:
- aclnnInplacePartialRotaryMulGrad defaults to a deterministic implementation.
The input tensor dyRef supports the BSND layout and broadcast variants of its B/S/N dimensions (such as 111D, 1SND, B1ND, BS1D, 11ND, B11D, and 1S1D). The shape constraints of each parameter are as follows:
- The size of the last dimension D of the input tensor dyRef must be less than or equal to 1024.
- When the slice length (end - start) is greater than 0, the size of the last dimension of the input tensors cos and sin must be equal to the slice length.
- The shapes of input tensors cos and sin must be exactly the same. The B, S, and N dimensions of cos and sin must satisfy the broadcast relationship with dyRef, and the broadcast B, S, and N dimensions must be the same as the B, S, and N dimensions of dyRef.
- In half, interleave, and interleave-half modes, when the slice length (end - start) is greater than 0, the slice length must be divisible by 2.
- In quarter mode, when the slice length (end - start) is greater than 0, the slice length must be divisible by 4.
- The input tensors cos and sin must have the same data type.
Empty input support: When the slice length is 0 (the start of
partial_sliceis equal to the end, such as[0, 0]), or dyRef, cos, or sin contains an empty tensor (a dimension of size 0), the operator performs no-op and directly returns without rotary position embedding computation.
Example
The following sample code is for reference only. For details about the compilation and execution procedure, see Compile and Run Samples.
#include "acl/acl.h"
#include "aclnnop/aclnn_inplace_partial_rotary_mul_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)
{
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);
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); return ret);
std::vector<int64_t> strides(shape.size(), 1);
for (int64_t i = shape.size() - 2; i >= 0; i--) {
strides[i] = shape[i + 1] * strides[i + 1];
}
*tensor = aclCreateTensor(shape.data(), shape.size(), dataType, strides.data(), 0, aclFormat::ACL_FORMAT_ND,
shape.data(), shape.size(), *deviceAddr);
return 0;
}
int main()
{
int32_t deviceId = 0;
aclrtStream stream;
auto ret = Init(deviceId, &stream);
CHECK_RET(ret == 0, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret);
std::vector<int64_t> dyShape = {1, 1, 1, 128};
std::vector<int64_t> cosShape = {1, 1, 1, 128};
std::vector<int64_t> sinShape = {1, 1, 1, 128};
int64_t rotaryMode = 1;
std::vector<int64_t> partialSliceData = {0, 128};
void* dyRefDeviceAddr = nullptr;
void* cosDeviceAddr = nullptr;
void* sinDeviceAddr = nullptr;
aclTensor* dyRef = nullptr;
aclTensor* cos = nullptr;
aclTensor* sin = nullptr;
std::vector<float> dyHostData = {
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};
// Create the dyRef aclTensor (inplace operation; dyRef serves as both input and output).
ret = CreateAclTensor(dyHostData, dyShape, &dyRefDeviceAddr, aclDataType::ACL_FLOAT, &dyRef);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the cos aclTensor.
ret = CreateAclTensor(cosHostData, cosShape, &cosDeviceAddr, aclDataType::ACL_FLOAT, &cos);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the sin aclTensor.
ret = CreateAclTensor(sinHostData, sinShape, &sinDeviceAddr, aclDataType::ACL_FLOAT, &sin);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the partialSlice aclIntArray.
aclIntArray* partialSlice = aclCreateIntArray(partialSliceData.data(), partialSliceData.size());
// Call the first-phase API of aclnnInplacePartialRotaryMulGrad.
uint64_t workspaceSize = 0;
aclOpExecutor* executor;
ret = aclnnInplacePartialRotaryMulGradGetWorkspaceSize(dyRef, cos, sin, rotaryMode,partialSlice,&workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS,
LOG_PRINT("aclnnInplacePartialRotaryMulGradGetWorkspaceSize failed. ERROR: %d\n", ret);
return ret);
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 of aclnnInplacePartialRotaryMulGrad.
ret = aclnnInplacePartialRotaryMulGrad(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnInplacePartialRotaryMulGrad failed. ERROR: %d\n", ret); return ret);
ret = aclrtSynchronizeStream(stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret);
// Obtain the result (inplace; read directly from dyRefDeviceAddr).
auto size = GetShapeSize(dyShape);
std::vector<float> resultData(size, 0);
ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), dyRefDeviceAddr,
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]);
}
// Release resources.
aclDestroyIntArray(partialSlice);
aclDestroyTensor(dyRef);
aclDestroyTensor(cos);
aclDestroyTensor(sin);
aclrtFree(dyRefDeviceAddr);
aclrtFree(cosDeviceAddr);
aclrtFree(sinDeviceAddr);
if (workspaceSize > 0) {
aclrtFree(workspaceAddr);
}
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
return 0;
}