aclnnBlockAttnResPrepare
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: Phase one of the two-phase computation of historical residual attention for Attention Residuals: completes the inter-block attention computation for all S layers in parallel at one time; returns the output and softmax statistics (O, M, L) for phase two to fuse the current residual and form the input of the next sub-layer.
- Formula:$$ \operatorname{block\_attn\_res\_prepare} \left(V,Q,\operatorname{valid\_blocks};\epsilon\right) \longrightarrow (O,M,L). $$For the $n$-th historical source of token $t$, first compute the RMS normalization factor:$$ R_{t,n}=\sqrt{\frac{1}{D}\sum_{d=0}^{D-1}V_{t,n,d}^{2}+\epsilon}. $$Compute logits for each slot, token, and valid history source:$$ Z_{s,t,n}=R_{t,n}^{-1}\sum_{d=0}^{D-1}Q_{s,d}V_{t,n,d}. $$Let $N_v$ be the number of valid history sources specified by
valid_blocks. Then:$$ M_{s,t}=\max_{0\le n<N_v}Z_{s,t,n}, $$$$ E_{s,t,n}=\exp\left(Z_{s,t,n}-M_{s,t}\right), $$$$ L_{s,t}=\sum_{n=0}^{N_v-1}E_{s,t,n}, $$$$ O_{s,t,d}=\sum_{n=0}^{N_v-1}E_{s,t,n}V_{t,n,d}. $$WhenvalidBlocks[0] == 0, the preceding softmax computation is not performed, and the result is returned directly:$$ O=\mathbf{0}_{S\times T\times D},\qquad M=m_{\min}\mathbf{1}_{S\times T},\qquad L=\mathbf{0}_{S\times T},\qquad m_{\min}=-3.4028234663852886\times10^{38}. $$
Where:That is, `numerator` is an all-zero tensor of shape `[S, T, D]`, `logitMax` is a tensor of shape `[S, T]` with all elements set to the minimum finite value of FLOAT32, and `expSum` is an all-zero tensor of shape `[S, T]`.- $V\in\mathbb{R}^{T\times N\times D}$ denotes the input
blockRes; $V_{t,n,d}$ denotes the value of the $t$-th token and the $n$-th historical residual block at the $d$-th hidden feature. - $Q\in\mathbb{R}^{S\times D}$ denotes the input
pseudoQuery(pseudo query); each target slot corresponds to a query vector, and $Q_{s,d}$ denotes the value of the $s$-th target slot at the $d$-th hidden feature. - $N_v=\min(\texttt{validBlocks[0]},N)$, which denotes the number of historical residual blocks actually involved in the computation.
- $\epsilon$ represents input
eps, a numerical stability term used to compute the RMS normalization factor. - $T$ denotes the number of tokens, $N$ denotes the number of historical residual blocks that can be accommodated in dimension 1 of
blockRes, $S$ denotes the number of target slots, and $D$ denotes HiddenSize. - $t\in[0,T)$, $n\in[0,N_v)$, $s\in[0,S)$, and $d\in[0,D)$ denote the indices of the token, valid historical residual block, target slot, and hidden feature, respectively.
- $V\in\mathbb{R}^{T\times N\times D}$ denotes the input
Function Prototype
Each operator uses a two-phase API. You must first call the "aclnnBlockAttnResPrepareGetWorkspaceSize" API to obtain the workspace size required for computation and the executor containing the operator computation process, and then call the "aclnnBlockAttnResPrepare" API to perform the computation.
aclnnStatus aclnnBlockAttnResPrepareGetWorkspaceSize(
const aclTensor *blockRes,
const aclTensor *validBlocks,
const aclTensor *pseudoQuery,
aclTensor *numerator,
aclTensor *logitMax,
aclTensor *expSum,
double eps,
uint64_t *workspaceSize,
aclOpExecutor **executor)aclnnStatus aclnnBlockAttnResPrepare(
void *workspace,
uint64_t workspaceSize,
aclOpExecutor *executor,
aclrtStream stream)aclnnBlockAttnResPrepareGetWorkspaceSize
Parameters
Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor blockRes (const aclTensor*) Input Indicates the residual block, which is the input V in the corresponding formula. - Supports empty tensors, and T can be 0.
- Shape is [T, N, D].
FLOAT ND 3 × validBlocks (const aclTensor*) Input Indicates the current number of valid history blocks. - Empty tensors (shape [1]) are not supported.
- If the value in the Tensor is N_v, only the first N_v elements of the N dimension of blockRes participate in the computation. If the value is 0, numerator returns an all-zero Tensor, logitMax returns a Tensor whose elements are all the minimum finite value of FLOAT32, and expSum returns an all-zero Tensor. Values greater than N are treated as N.
UINT64 ND 1 × pseudoQuery (const aclTensor*) Input Indicates the pseudo query, which is the input Q in the corresponding formula; each target slot corresponds to a query vector. - Supports empty tensor, and S can be 0.
- shape is [S, D], and the last dimension must be the same as the last dimension of blockRes.
FLOAT ND 2 × numerator (aclTensor*) Output Indicates the softmax weighted numerator, which is the output O in the corresponding formula. - Supports empty tensor.
- The shape is [S, T, D].
FLOAT ND 3 × logitMax (aclTensor*) Output Indicates the maximum value of softmax, corresponding to output M in the formula. - Supports empty tensor.
- The shape is [S, T].
FLOAT ND 2 × expSum (aclTensor*) Output Indicates the sum of softmax exponents, which is output L in the corresponding formula. - Supports empty tensors.
- The shape is [S, T].
FLOAT ND 2 × eps (double) Input Indicates the stabilization term for RMS normalization, usually 1e-6. Must be a finite positive number. FLOAT - - - workspaceSize (uint64_t*) Output Returns the workspace size to be allocated on the Device side. - - - - - executor (aclOpExecutor**) Output Returns the op executor that contains the operator computation flow. - - - - - Return Value
aclnnStatus: Return status code. For details, see aclnn return code.
The first-phase API performs parameter validation and executor creation, and may return the following status codes:
Return Value Error Code Description ACLNN_SUCCESS0 The API call succeeds. ACLNN_ERR_PARAM_NULLPTR161001 One of blockRes,validBlocks,pseudoQuery,numerator,logitMax,expSum,workspaceSize, orexecutoris a null pointer.ACLNN_ERR_PARAM_INVALID161002 The data type, dimension, shape, or contiguity of the input or output does not meet the constraints, or epsis not a finite positive number.ACLNN_ERR_RUNTIME_ERROR361001 The API runs on an unsupported product model. ACLNN_ERR_INNER_CREATE_EXECUTOR561101 The API fails to create aclOpExecutorinternally.ACLNN_ERR_INNER_NULLPTR561103 A null pointer is returned when the API internally performs contiguity processing, creates the operator output, or creates the output copy node.
aclnnBlockAttnResPrepare
Parameters
Parameter Input/Output Description workspace (void*) Input Workspace memory address requested on the Device side. workspaceSize (uint64_t) Input Size of the workspace requested on the Device side, obtained by calling the first-phase API aclnnBlockAttnResPrepareGetWorkspaceSize. executor (aclOpExecutor*) Input op executor, containing the operator computation process. stream (aclrtStream) Input Stream for executing tasks. Return Value
aclnnStatus: return status code. For details, see aclnn Return Codes.
The second-phase API calls
CommonOpExecutorRunto dispatch the operator. On success, it returnsACLNN_SUCCESS(error code 0); on failure, it returns the corresponding ACLNN Runtime or internal error code.
Constraints
- Deterministic computation: aclnnBlockAttnResPrepare defaults to a deterministic implementation.
- Batch consistency: aclnnBlockAttnResPrepare defaults to a batch consistency implementation.
T >= 0,S >= 0,1 <= N <= 64,1 <= D <= 8192.- The last dimension of
blockResmust be the same as that ofpseudoQuery. - If
validBlocks[0] == 0, the empty state of online softmax is returned; ifvalidBlocks[0] > N, it is processed asN. - All input and output Tensors must be contiguous. Non-contiguous Tensors are not supported.
- If
T == 0orS == 0, an empty output of the corresponding shape is returned.
Example
The following sample code is for reference only. For details about compilation and execution, see Compile and Run Samples.
#include <cstdint>
#include <cstdio>
#include <memory>
#include <vector>
#include "acl/acl.h"
#include "aclnnop/aclnn_block_attn_res_prepare.h"
#define CHECK_RET(cond, return_expr) \
do { \
if (!(cond)) { \
return_expr; \
} \
} while (0)
#define CHECK_FREE_RET(cond, return_expr) \
do { \
if (!(cond)) { \
Finalize(deviceId, stream); \
return_expr; \
} \
} while (0)
#define LOG_PRINT(message, ...) \
do { \
std::printf(message, ##__VA_ARGS__); \
} while (0)
namespace {
constexpr int64_t ROW_MAJOR_STRIDE_START_OFFSET = 2;
constexpr float DEFAULT_EPS = 1.0e-6F;
int64_t GetShapeSize(const std::vector<int64_t> &shape)
{
int64_t shapeSize = 1;
for (const auto dim : shape) {
shapeSize *= dim;
}
return shapeSize;
}
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 ACL_SUCCESS;
}
template <typename T>
int CreateAclTensor(const std::vector<T> &hostData, const std::vector<int64_t> &shape, void **deviceAddr,
aclDataType dataType, aclTensor **tensor)
{
const size_t size = static_cast<size_t>(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 = static_cast<int64_t>(shape.size()) - ROW_MAJOR_STRIDE_START_OFFSET; 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);
CHECK_RET(*tensor != nullptr, LOG_PRINT("aclCreateTensor failed.\n"); return ACL_ERROR_INVALID_PARAM);
return ACL_SUCCESS;
}
void Finalize(int32_t deviceId, aclrtStream stream)
{
if (stream != nullptr) {
aclrtDestroyStream(stream);
}
aclrtResetDevice(deviceId);
aclFinalize();
}
int aclnnBlockAttnResPrepareTest(int32_t deviceId, aclrtStream &stream)
{
auto ret = Init(deviceId, &stream);
CHECK_RET(ret == ACL_SUCCESS,
LOG_PRINT("Init acl failed. ERROR: %d\n", ret);
return ret);
// Construct the input and output: 1 token, 2 history sources, 2 target slots, and a hidden size of 4.
const std::vector<int64_t> blockResShape = {1, 2, 4};
const std::vector<int64_t> validBlocksShape = {1};
const std::vector<int64_t> pseudoQueryShape = {2, 4};
const std::vector<int64_t> numeratorShape = {2, 1, 4};
const std::vector<int64_t> statsShape = {2, 1};
const std::vector<float> blockResHostData = {
1.0F, 2.0F, 3.0F, 4.0F, 2.0F, 0.0F, -1.0F, 1.0F,
};
const std::vector<uint64_t> validBlocksHostData = {2U};
const std::vector<float> pseudoQueryHostData = {
1.0F, 0.0F, 0.0F, 0.0F, 0.0F, 1.0F, 0.0F, 0.0F,
};
std::vector<float> numeratorHostData(GetShapeSize(numeratorShape), 0.0F);
std::vector<float> logitMaxHostData(GetShapeSize(statsShape), 0.0F);
std::vector<float> expSumHostData(GetShapeSize(statsShape), 0.0F);
void *blockResDeviceAddr = nullptr;
void *validBlocksDeviceAddr = nullptr;
void *pseudoQueryDeviceAddr = nullptr;
void *numeratorDeviceAddr = nullptr;
void *logitMaxDeviceAddr = nullptr;
void *expSumDeviceAddr = nullptr;
aclTensor *blockRes = nullptr;
aclTensor *validBlocks = nullptr;
aclTensor *pseudoQuery = nullptr;
aclTensor *numerator = nullptr;
aclTensor *logitMax = nullptr;
aclTensor *expSum = nullptr;
ret = CreateAclTensor(blockResHostData, blockResShape, &blockResDeviceAddr, aclDataType::ACL_FLOAT, &blockRes);
std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor *)> blockResTensorPtr(blockRes, aclDestroyTensor);
std::unique_ptr<void, aclError (*)(void *)> blockResDeviceAddrPtr(blockResDeviceAddr, aclrtFree);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(validBlocksHostData, validBlocksShape, &validBlocksDeviceAddr, aclDataType::ACL_UINT64,
&validBlocks);
std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor *)> validBlocksTensorPtr(validBlocks, aclDestroyTensor);
std::unique_ptr<void, aclError (*)(void *)> validBlocksDeviceAddrPtr(validBlocksDeviceAddr, aclrtFree);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(pseudoQueryHostData, pseudoQueryShape, &pseudoQueryDeviceAddr, aclDataType::ACL_FLOAT,
&pseudoQuery);
std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor *)> pseudoQueryTensorPtr(pseudoQuery, aclDestroyTensor);
std::unique_ptr<void, aclError (*)(void *)> pseudoQueryDeviceAddrPtr(pseudoQueryDeviceAddr, aclrtFree);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(numeratorHostData, numeratorShape, &numeratorDeviceAddr, aclDataType::ACL_FLOAT, &numerator);
std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor *)> numeratorTensorPtr(numerator, aclDestroyTensor);
std::unique_ptr<void, aclError (*)(void *)> numeratorDeviceAddrPtr(numeratorDeviceAddr, aclrtFree);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(logitMaxHostData, statsShape, &logitMaxDeviceAddr, aclDataType::ACL_FLOAT, &logitMax);
std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor *)> logitMaxTensorPtr(logitMax, aclDestroyTensor);
std::unique_ptr<void, aclError (*)(void *)> logitMaxDeviceAddrPtr(logitMaxDeviceAddr, aclrtFree);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(expSumHostData, statsShape, &expSumDeviceAddr, aclDataType::ACL_FLOAT, &expSum);
std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor *)> expSumTensorPtr(expSum, aclDestroyTensor);
std::unique_ptr<void, aclError (*)(void *)> expSumDeviceAddrPtr(expSumDeviceAddr, aclrtFree);
CHECK_RET(ret == ACL_SUCCESS, return ret);
uint64_t workspaceSize = 0;
aclOpExecutor *executor = nullptr;
ret = aclnnBlockAttnResPrepareGetWorkspaceSize(blockRes, validBlocks, pseudoQuery, numerator, logitMax, expSum,
DEFAULT_EPS, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS,
LOG_PRINT("aclnnBlockAttnResPrepareGetWorkspaceSize failed. ERROR: %d\n", ret);
return ret);
void *workspaceAddr = nullptr;
std::unique_ptr<void, aclError (*)(void *)> workspaceAddrPtr(nullptr, aclrtFree);
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);
workspaceAddrPtr.reset(workspaceAddr);
}
ret = aclnnBlockAttnResPrepare(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS,
LOG_PRINT("aclnnBlockAttnResPrepare 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);
ret = aclrtMemcpy(numeratorHostData.data(), numeratorHostData.size() * sizeof(numeratorHostData[0]),
numeratorDeviceAddr, numeratorHostData.size() * sizeof(numeratorHostData[0]),
ACL_MEMCPY_DEVICE_TO_HOST);
CHECK_RET(ret == ACL_SUCCESS,
LOG_PRINT("copy numerator from device to host failed. ERROR: %d\n", ret);
return ret);
ret = aclrtMemcpy(logitMaxHostData.data(), logitMaxHostData.size() * sizeof(logitMaxHostData[0]),
logitMaxDeviceAddr, logitMaxHostData.size() * sizeof(logitMaxHostData[0]),
ACL_MEMCPY_DEVICE_TO_HOST);
CHECK_RET(ret == ACL_SUCCESS,
LOG_PRINT("copy logitMax from device to host failed. ERROR: %d\n", ret);
return ret);
ret = aclrtMemcpy(expSumHostData.data(), expSumHostData.size() * sizeof(expSumHostData[0]), expSumDeviceAddr,
expSumHostData.size() * sizeof(expSumHostData[0]), ACL_MEMCPY_DEVICE_TO_HOST);
CHECK_RET(ret == ACL_SUCCESS,
LOG_PRINT("copy expSum from device to host failed. ERROR: %d\n", ret);
return ret);
for (size_t i = 0; i < numeratorHostData.size(); ++i) {
LOG_PRINT("numerator[%zu] = %.6f\n", i, numeratorHostData[i]);
}
for (size_t i = 0; i < logitMaxHostData.size(); ++i) {
LOG_PRINT("logit_max[%zu] = %.6f, exp_sum[%zu] = %.6f\n", i, logitMaxHostData[i], i, expSumHostData[i]);
}
return ACL_SUCCESS;
}
} // namespace
int main()
{
// Set the Device ID based on the actual runtime environment.
constexpr int32_t deviceId = 0;
aclrtStream stream = nullptr;
const auto ret = aclnnBlockAttnResPrepareTest(deviceId, stream);
CHECK_FREE_RET(ret == ACL_SUCCESS,
LOG_PRINT("aclnnBlockAttnResPrepareTest failed. ERROR: %d\n", ret);
return ret);
Finalize(deviceId, stream);
return ACL_SUCCESS;
}