aclnnInterleaveRope
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: Applies rotary position encoding to the single input x.
Formula:
$$ q = \text{reshape}(x, [B, N, S, D//2, 2]) \cdot \text{transpose}(-1, -2) \cdot \text{reshape}([B, N, S, D]) $$$$ q_{\text{embed}} = q \cdot \text{cos} + \text{RotateHalf}(q) \cdot \sin $$where RotateHalf(q) indicates that the elements in the second half of the D dimension of q are moved to the first half and multiplied by -1, and the second half is replaced with the values of the first half.
$$ \text{RotateHalf}(q)_{\text{i}} = \begin{cases} -q_{i+D//2} & \text{if } i < D//2 \\ q_{i-D//2} & \text{otherwise} \end{cases} $$
Function Prototype
Each operator uses a two-phase API. You must first call the "aclnnInterleaveRopeGetWorkspaceSize" API to obtain the input parameters and calculate the required workspace size based on the procedure, and then call the "aclnnInterleaveRope" API to perform the computation.
aclnnStatus aclnnInterleaveRopeGetWorkspaceSize(
const aclTensor *x,
const aclTensor *cos,
const aclTensor *sin,
aclTensor *out,
uint64_t *workspaceSize,
aclOpExecutor **executor)aclnnStatus aclnnInterleaveRope(
void *workspace,
uint64_t workspaceSize,
aclOpExecutor *executor,
aclrtStream stream)aclnnInterleaveRopeGetWorkspaceSize
Parameters
Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor x Input aclTensor on the device, the tensor to which the rotated position encoding is applied, corresponding to x in the formula. - FLOAT32, FLOAT16, BFLOAT16 ND 4 √ cos Input aclTensor on the device, position encoding tensor, corresponding to cos in the formula. The shape satisfies the broadcast relationship with x. Same as x ND 4 √ sin Input aclTensor on the device, the position encoding tensor, corresponding to sin in the formula. The shape is the same as cos. Same as x ND 4 √ out Output aclTensor on the device, rotary position encoding computation result, corresponding to y in the formula. The shape is the same as that of x. Same as x ND 4 x workspaceSize Output Returns the size of the workspace that the user needs to allocate on the NPU device. - - - - - 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:
Error Code Error code Description ACLNN_ERR_PARAM_NULLPTR 161001 x, cos, sin, or out passed in is a null pointer. ACLNN_ERR_PARAM_INVALID 161002 The data type or format of the passed-in x, cos, sin, or out is not within the supported range. ACLNN_ERR_INNER_TILING_ERROR 561002 The shapes of the passed-in x, cos, sin, or out do not match.
aclnnInterleaveRope
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 aclnnInterleaveRopeGetWorkspaceSize. executor Input Executor of the op, which contains the computation flow of the operator. stream Input Specifies the stream for executing tasks. Return Value
aclnnStatus: return code. For details, see aclnn return codes.
Constraints
- Deterministic computation:
- aclnnInterleaveRope defaults to a deterministic implementation.
- This API supports use in inference scenarios.
- x, cos, sin, and out must be 4D tensors with the shape (B, N, S, D).
- The S dimension of cos and sin can be 1 or the same as the S dimension of x, and the N dimension must be equal to 1.
- The D dimension of the input x, cos, and sin must be equal to 64.
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_interleave_rope.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;
}
std::vector<aclFloat16> ConvertToFloat16(const std::vector<float>& data) {
std::vector<aclFloat16> converted;
converted.reserve(data.size());
for (float value : data) {
converted.push_back(aclFloatToFloat16(value));
}
return converted;
}
int Init(int32_t deviceId, aclrtStream* stream) {
// Boilerplate, 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: device/stream initialization. For details, see the acl API reference.
// Set deviceId based on the actual device.
int32_t deviceId = 0;
aclrtStream stream;
auto ret = Init(deviceId, &stream);
// Handle the check as required.
CHECK_RET(ret == 0, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret);
// 2. Construct the inputs and outputs based on the API definition.
// interleave_rope constraints: D=64, N=1
std::vector<int64_t> xShape = {1, 1, 1, 64};
std::vector<int64_t> cosShape = {1, 1, 1, 64};
std::vector<int64_t> sinShape = {1, 1, 1, 64};
std::vector<int64_t> outShape = {1, 1, 1, 64};
void* xDeviceAddr = nullptr;
void* cosDeviceAddr = nullptr;
void* sinDeviceAddr = nullptr;
void* outDeviceAddr = nullptr;
aclTensor* x = nullptr;
aclTensor* cos = nullptr;
aclTensor* sin = nullptr;
aclTensor* out = nullptr;
std::vector<aclFloat16> xHostData = ConvertToFloat16({
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
});
std::vector<aclFloat16> cosHostData = ConvertToFloat16({
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
});
std::vector<aclFloat16> sinHostData = ConvertToFloat16({
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
});
std::vector<aclFloat16> outHostData(64, aclFloatToFloat16(0.0f));
// Create the x aclTensor.
ret = CreateAclTensor(xHostData, xShape, &xDeviceAddr, aclDataType::ACL_FLOAT16, &x);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the cos aclTensor.
ret = CreateAclTensor(cosHostData, cosShape, &cosDeviceAddr, aclDataType::ACL_FLOAT16, &cos);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the sin aclTensor.
ret = CreateAclTensor(sinHostData, sinShape, &sinDeviceAddr, aclDataType::ACL_FLOAT16, &sin);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the out aclTensor.
ret = CreateAclTensor(outHostData, outShape, &outDeviceAddr, aclDataType::ACL_FLOAT16, &out);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// 3. Call the CANN operator library API. Replace it with the specific API.
uint64_t workspaceSize = 0;
aclOpExecutor* executor;
// Call the first-phase API of aclnnInterleaveRope.
ret = aclnnInterleaveRopeGetWorkspaceSize(x, cos, sin, out, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnInterleaveRopeGetWorkspaceSize failed. ERROR: %d\n", ret); return ret);
// Allocate device memory based on 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 of aclnnInterleaveRope.
ret = aclnnInterleaveRope(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnInterleaveRope failed. ERROR: %d\n", ret); return ret);
// 4. Boilerplate: synchronously wait for the task to complete.
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 side to the host side. Modify it according to the specific API definition.
auto size = GetShapeSize(outShape);
std::vector<aclFloat16> resultData(size, aclFloatToFloat16(0.0f));
ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), outDeviceAddr,
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 ret);
for (int64_t i = 0; i < size; i++) {
LOG_PRINT("result[%ld] is: %f\n", i, aclFloat16ToFloat(resultData[i]));
}
// 6. Release the aclTensor and aclScalar. Modify it according to the specific API definition.
aclDestroyTensor(x);
aclDestroyTensor(cos);
aclDestroyTensor(sin);
aclDestroyTensor(out);
// 7. Release device resources
aclrtFree(xDeviceAddr);
aclrtFree(cosDeviceAddr);
aclrtFree(sinDeviceAddr);
aclrtFree(outDeviceAddr);
if (workspaceSize > 0) {
aclrtFree(workspaceAddr);
}
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
return 0;
}