aclnnQuantSparseFlashMlaMetadata
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:
aclnnQuantSparseFlashMlaMetadatais the preceding operator ofaclnnQuantSparseFlashMla. It generates a load balancing task partition scheme for the subsequent Attention computation. This operator does not perform the actual Attention computation. Instead, it calculates in the AI CPU the start and end ranges of the Attention computation to be processed by each AI Core based on the input parameters. This maximizes the utilization of computing resources and avoids load imbalance across Cores.This operator is not recommended for standalone use. Use it with the aclnnQuantSparseFlashMla operator to form a complete workflow.
- Accepts the input data shape information of the aclnnQuantSparseFlashMla operator API, including batchSize, qSeqen, kvSeqlen, and mask. The input is divided into blocks, and the computation time is simulated to evenly distribute the blocks across the available cores. This reduces the overall computation time of the aclnnQuantSparseFlashMla operator and improves hardware utilization.
- After the allocation result is output, it is used as input for the aclnnQuantSparseFlashMla operator.
- The allocation result includes the start and end points of the basic blocks of each AIC core, and the FD task information of each AIV core. For details, see Example.
Function Prototype
Each operator uses a two-phase API. Call "aclnnQuantSparseFlashMlaMetadataGetWorkspaceSize" first to obtain the workspace size, and then call "aclnnQuantSparseFlashMlaMetadata" to execute the computation.
aclnnStatus aclnnQuantSparseFlashMlaMetadataGetWorkspaceSize(
const aclTensor *cuSeqlensQOptional,
const aclTensor *cuSeqlensOriKvOptional,
const aclTensor *cuSeqlensCmpKvOptional,
const aclTensor *sequsedQOptional,
const aclTensor *sequsedOriKvOptional,
const aclTensor *sequsedCmpKvOptional,
const aclTensor *cmpResidualKvOptional,
const aclTensor *oriTopkLengthOptional,
const aclTensor *cmpTopkLengthOptional,
int64_t numHeadsQ,
int64_t numHeadsKv,
int64_t headDim,
int64_t quantMode,
int64_t batchSize,
int64_t maxSeqlenQ,
int64_t maxSeqlenOriKv,
int64_t maxSeqlenCmpKv,
int64_t oriTopk,
int64_t cmpTopk,
int64_t cmpRatio,
int64_t oriMaskMode,
int64_t cmpMaskMode,
int64_t oriWinLeft,
int64_t oriWinRight,
const char *layoutQOptional,
const char *layoutKvOptional,
bool hasOriKv,
bool hasCmpKv,
const aclTensor *metadata,
uint64_t *workspaceSize,
aclOpExecutor **executor)aclnnStatus aclnnQuantSparseFlashMlaMetadata(
void *workspace,
uint64_t workspaceSize,
aclOpExecutor *executor,
aclrtStream stream)aclnnQuantSparseFlashMlaMetadataGetWorkspaceSize
Parameters
Parameter Input/Output Description Instruction Data Type Data Format Shape Non-contiguous Tensor cuSeqlensQOptional (aclTensor*) Input Indicates the valid Sequence Length of Query in different Batches. - Supports empty tensors.
- Required in scenarios where layoutQOptional is TND.
- The first value is an additional value and fixed to 0.
- The shape is fixed to (B+1, ).
INT32 ND 1D √ cuSeqlensOriKvOptional (aclTensor*) Input Indicates the valid Sequence Length of oriKvOptional in different Batches. - Supports empty tensor.
- Mandatory when layoutKvOptional is TND.
- The first value is an additional value and fixed to 0.
- The shape is fixed to (B+1, ).
INT32 ND 1D √ cuSeqlensCmpKvOptional(aclTensor*) Input Indicates the valid Sequence Length of cmpKvOptional in different batches. - Supports empty tensor.
- Mandatory when layoutKvOptional is TND.
- The first value is an additional value and fixed to 0.
- The shape is fixed to (B+1, ).
INT32 ND 1D √ sequsedQOptional (aclTensor*) Input Indicates the sequence length of query actually involved in the operation in different batches. - Supports an empty tensor.
- The shape is fixed to (B, ).
INT32 ND 1D √ sequsedOriKvOptional (aclTensor*) Input Indicates the Sequence Length of oriKvOptional actually involved in the operation in different batches. - Supports empty tensor.
- Shape is fixed to (B, ).
INT32 ND 1D √ sequsedCmpKvOptional (aclTensor*) Input Indicates different Sequence Lengths of cmpKvOptional actually involved in the operation across batches. - Supports empty tensor.
- Shape is fixed to (B, ).
INT32 ND 1D √ cmpResidualKvOptional (aclTensor*) Input Indicates the remainder of the compressed Sequence Length of cmpKvOptional in different batches. Used with cmpRatio to compute the mask and load of the cmpKvOptional part. - Supports empty tensors.
- Required in scenarios where cmpRatio is not 1.
- Shape fixed to (B, ).
INT32 ND 1D √ oriTopkLengthOptional (aclTensor*) Input Used to identify the length of oriSparseIndicesOptional actually involved in the calculation. - Supports empty tensors.
- Required in scenarios where oriTopk!=0 and oriMaskMode=0.
- When layoutQ is BSND: (B, S1, N2)
- When layoutQ is TND: (T1, N2)
INT32 ND 2D, 3D √ cmpTopkLengthOptional (aclTensor*) Input Used to identify the length of cmpSparseIndicesOptional actually involved in the calculation. - Supports empty tensor.
- Required in scenarios where cmpTopk!=0 and cmpMaskMode=0.
- When layoutQ is BSND: (B, S1, N2)
- When layoutQ is TND: (T1, N2)
INT32 ND 2D, 3D √ numHeadsQ (int64_t) Input Indicates the number of Query heads. Supported range: 1 to 128. - - - - numHeadsKv (int64_t) Input Number of heads corresponding to Key and Value. Currently only supports 1. - - - - headDim (int64_t) Input Dimension of the attention head. Currently only supports 512. - - - - quantMode (int64_t) Input Indicates the quantization mode. - 1: Q, K, and V use per-tensor quantization, and the data type of Q, K, and V is HIFLOAT8.
- - - - batchSize (int64_t) Input Indicates the number of batches. - Supports non-negative values.
- The recommended value is 0.
- - - - maxSeqlenQ (int64_t) Input Indicates the maximum Sequence Length of Query. - Supports non-negative values.
- The recommended value is 0.
- - - - maxSeqlenOriKv (int64_t) Input Indicates the maximum sequence length of oriKvOptional. - Supports non-negative values.
- The recommended value is 0.
- - - - maxSeqlenCmpKv (int64_t) Input Indicates the maximum sequence length of cmpKvOptional. - Supports non-negative numbers.
- The recommended value is 0.
- - - - oriTopk (int64_t) Input Indicates the number of key sparse tokens filtered out from oriKvOptional. 0 indicates a non-sparse task. - Currently only supports 0.
- The recommended value is 0.
- - - - cmpTopk (int64_t) Input Indicates the number of key sparse tokens filtered out from cmpKvOptional. 0 indicates a non-sparse use case. - Supports non-negative numbers.
- The recommended value is 0.
- - - - cmpRatio (int64_t) Input Indicates the compression ratio of cmpKvOptional. - Currently supports 1 to 128.
- The recommended value is 1, indicating no compression.
- - - - oriMaskMode (int64_t) Input Indicates the mask mode for calculation of q and oriKvOptional. - Currently supports:
0: No Mask.
3: RightDownCausal mode.
4: Band mode. - The recommended value is 0.
- - - - cmpMaskMode (int64_t) Input Indicates the mask mode for calculation of q and cmpKvOptional. - Currently supports:
0: No Mask.
3: RightDownCausal mode. - The recommended value is 0.
- - - - oriWinLeft (int64_t) Input Indicates the number of past tokens calculated by q in the computation of q and oriKvOptional. - Value range: ≥-1. -1 indicates infinity.
- The recommended value is -1.
- - - - oriWinRight (int64_t) Input Indicates the number of future tokens that q attends to in the computation of q and oriKvOptional. - Value range: ≥-1. -1 indicates infinity.
- The recommended value is -1.
- - - - layoutQOptional (char*) Input Indicates the layout of Query. - Supports BSND and TND.
- The recommended value is BSND.
- - - - layoutKvOptional (char*) Input Indicates the layout format of Key. - Supports BSND, TND, and PA_BBND.
- The recommended value is BSND.
- - - - hasOriKv (bool) Input Used to identify whether oriKvOptional is present. - true: contains oriKvOptional.
- false: does not contain oriKvOptional.
- The recommended value is true.
- - - - hasCmpKv (bool) Input Used to identify whether cmpKvOptional is contained. - true: contains cmpKvOptional.
- false: does not contain cmpKvOptional.
- The recommended value is true.
- - - - metadata (aclTensor*) Output Indicates the load balancing result output. - INT32 ND 1D, shape fixed to (1024) × workspaceSize (uint64_t*) Output Returns the workspace size to be requested by the device. - - - - - executor (aclOpExecutor**) Output Returns the op executor containing the operator calculation 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 Value Error Code Description ACLNN_ERR_INNER_CREATE_EXECUTOR 561101 Failed to create aclOpExecutor. ACLNN_ERR_INNER_NULLPTR 561103 Parameter workspaceSize or executor is a null pointer, or parameters cuSeqlensQOptional, cuSeqlensOriKvOptional, cuSeqlensCmpKvOptional, sequsedQOptional, sequsedOriKvOptional, sequsedCmpKvOptional, cmpResidualKvOptional, oriTopkLengthOptional, or cmpTopkLengthOptional are null pointers after Contiguous conversion. ACLNN_ERR_PARAM_INVALID 161002 The specifications of parameters cuSeqlensQOptional, cuSeqlensOriKvOptional, cuSeqlensCmpKvOptional, sequsedQOptional, sequsedOriKvOptional, sequsedCmpKvOptional, cmpResidualKvOptional, oriTopkLengthOptional, cmpTopkLengthOptional, numHeadsQ, numHeadsKv, headDim, quantMode, batchSize, maxSeqlenQ, maxSeqlenOriKv, maxSeqlenCmpKv, oriTopk, cmpTopk, cmpRatio, oriMaskMode, cmpMaskMode, oriWinLeft, oriWinRight, layoutQOptional, layoutKvOptional, hasOriKv, and hasCmpKv are out of the supported range.
aclnnQuantSparseFlashMlaMetadata
Parameters:
Parameter Input/Output Description workspace Input Workspace memory address requested by the Device. workspaceSize Input Size of the workspace requested by the Device, obtained by calling the first-phase API aclnnQuantSparseFlashMlaMetadataGetWorkspaceSize. executor Input op executor, containing the operator computation flow. stream Input Stream used to execute tasks. Return value
aclnnStatus: return status code. For details, see aclnn Return Codes.
Constraints
- aclnnQuantSparseFlashMlaMetadata defaults to a deterministic implementation.
- B (Batch) indicates the batch size of input samples.
- Batch value rules
- Obtain the Batch information in sequsedQOptional first.
- When sequsedQOptional is not passed, layoutQOptional is TND, and cuSeqlensQOptional is passed, obtain the Batch information in cuSeqlensQOptional.
- In all other cases, use batchSize.
- Query Sequence Length value rules
- Obtain the Sequence Length information in sequsedQOptional first.
- When sequsedQOptional is not passed, layoutQOptional is TND, and cuSeqlensQOptional is passed, obtain the Sequence Length information in cuSeqlensQOptional.
- In all other cases, use maxSeqlenQ.
- The rules for obtaining the Sequence Length of oriKvOptional and cmpKvOptional are the same as those for Query.
- BSND scenario
- When the passed layoutQOptional is "BSND", the maxSeqlenQ parameter is mandatory when sequsedQOptional is not passed.
- When the passed layoutKvOptional is "BSND", if hasOriKv is true, maxSeqlenOriKv is mandatory when sequsedOriKvOptional is not passed; if hasCmpKv is true, maxSeqlenCmpKv is mandatory when sequsedCmpKvOptional is not passed.
- TND scenario
- When the passed layoutQOptional is "TND", cuSeqlensQOptional is mandatory.
- When the passed layoutKvOptional is "TND", if hasOriKv is true, cuSeqlensOriKvOptional is mandatory; if hasCmpKv is true, cuSeqlensCmpKvOptional is mandatory.
- TopkLength parameter
- oriTopkLengthOptional and cmpTopkLengthOptional indicate the length actually involved in calculation of ori/cmp sparseIndices. The value cannot be greater than the size of the last dimension of sparseIndices. When sequsedQOptional is passed, the values of the valid part corresponding to topkLength must be greater than or equal to 0.
- When oriMaskMode=0 and oriSparseIndicesOptional is not empty, oriTopkLengthOptional is mandatory.
- When cmpMaskMode=0 and cmpSparseIndicesOptional is not empty, cmpTopkLengthOptional is mandatory.
Example
The following is sample code for reference only. For details about compilation and execution, see Compile and Run Samples.
#include <iostream>
#include <vector>
#include <cmath>
#include <cstring>
#include <limits>
#include <functional>
#include <utility>
#include "acl/acl.h"
#include "aclnnop/aclnn_quant_sparse_flash_mla_metadata.h"
#define CHECK_LOG_RET(cond, ret_val, fmt, ...) \
do { \
if (!(cond)) { \
printf(fmt "\n", ##__VA_ARGS__); \
return (ret_val); \
} \
} while (0)
// Refer to quant_sparse_flash_mla_metadata.h.
constexpr uint32_t AIC_CORE_MAX_NUM = 36;
constexpr uint32_t AIV_CORE_MAX_NUM = 72;
constexpr uint32_t MQSMLA_METADATA_TOTAL_SIZE = 1024;
constexpr uint32_t FA_METADATA_SIZE = 9;
constexpr uint32_t FD_METADATA_SIZE = 8;
// FA Metadata Index Definitions
constexpr uint32_t FA_CORE_ENABLE_INDEX = 0;
constexpr uint32_t FA_BN2_START_INDEX = 1;
constexpr uint32_t FA_M_START_INDEX = 2;
constexpr uint32_t FA_S2_START_INDEX = 3;
constexpr uint32_t FA_BN2_END_INDEX = 4;
constexpr uint32_t FA_M_END_INDEX = 5;
constexpr uint32_t FA_S2_END_INDEX = 6;
constexpr uint32_t FA_FIRST_FD_DATA_WORKSPACE_IDX_INDEX = 7;
constexpr uint32_t FA_S2_MAX_NUM = 8;
// FD Metadata Index Definitions
constexpr uint32_t FD_CORE_ENABLE_INDEX = 0;
constexpr uint32_t FD_BN2_IDX_INDEX = 1;
constexpr uint32_t FD_M_IDX_INDEX = 2;
constexpr uint32_t FD_WORKSPACE_IDX_INDEX = 3;
constexpr uint32_t FD_WORKSPACE_NUM_INDEX = 4;
constexpr uint32_t FD_M_START_INDEX = 5;
constexpr uint32_t FD_M_NUM_INDEX = 6;
struct MqsmlaMetadata {
uint32_t faMetadata[AIC_CORE_MAX_NUM][FA_METADATA_SIZE];
uint32_t fdMetadata[AIV_CORE_MAX_NUM][FD_METADATA_SIZE];
};
struct ScopeGuard {
explicit ScopeGuard(std::function<void()> onExitScope) : m_exitFunc(std::move(onExitScope)), m_isDismissed(false)
{
}
// Copying is prohibited.
ScopeGuard(const ScopeGuard &) = delete;
ScopeGuard &operator=(const ScopeGuard &) = delete;
~ScopeGuard()
{
if (!m_isDismissed) {
m_exitFunc();
}
}
void Dismiss()
{
m_isDismissed = true;
}
std::function<void()> m_exitFunc;
bool m_isDismissed;
};
struct Tensor {
void *hostAddr{nullptr};
void *deviceAddr{nullptr};
aclTensor *data{nullptr};
};
struct ArgScenario {
bool hasCuSeq{false};
bool hasSeqused{false};
};
struct ArgContext {
// required input
int64_t numHeadsQ{0};
int64_t numHeadsKv{0};
int64_t headDim{0};
int64_t quantMode{1};
// optional input
Tensor cuSeqlensQOptional{};
Tensor cuSeqlensOriKvOptional{};
Tensor cuSeqlensCmpKvOptional{};
Tensor sequsedQOptional{};
Tensor sequsedOriKvOptional{};
Tensor sequsedCmpKvOptional{};
Tensor cmpResidualKvOptional{};
Tensor oriTopkLengthOptional{};
Tensor cmpTopkLengthOptional{};
int64_t batchSize{0};
int64_t maxSeqlenQ{0};
int64_t maxSeqlenOriKv{0};
int64_t maxSeqlenCmpKv{0};
int64_t oriTopk{0};
int64_t cmpTopk{0};
int64_t cmpRatio{0};
int64_t oriMaskMode{0};
int64_t cmpMaskMode{0};
int64_t oriWinLeft{-1};
int64_t oriWinRight{-1};
char *layoutQOptional{nullptr};
char *layoutKvOptional{nullptr};
bool hasOriKv{true};
bool hasCmpKv{true};
// output
Tensor metadata{};
};
int64_t GetShapeSize(const std::vector<int64_t> &shape)
{
int64_t shapeSize = 1;
for (auto i : shape) {
shapeSize *= i;
}
return shapeSize;
}
aclnnStatus Init(int32_t deviceId, aclrtStream *stream)
{
// Boilerplate, initialization.
auto ret = aclInit(nullptr);
CHECK_LOG_RET(ret == ACL_SUCCESS, ret, "aclInit failed. ERROR: %d", ret);
ret = aclrtSetDevice(deviceId);
CHECK_LOG_RET(ret == ACL_SUCCESS, ret, "aclrtSetDevice failed. ERROR: %d", ret);
ret = aclrtCreateStream(stream);
CHECK_LOG_RET(ret == ACL_SUCCESS, ret, "aclrtCreateStream failed. ERROR: %d", ret);
return ACL_SUCCESS;
}
void Finalize(int32_t deviceId, aclrtStream stream)
{
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
}
aclnnStatus CreateTensor(aclDataType dataType, const std::vector<int64_t> &shape, Tensor &tensor)
{
auto size = GetShapeSize(shape) * aclDataTypeSize(dataType);
// Call aclrtMallocHost to allocate host-side memory.
auto ret = aclrtMallocHost(&(tensor.hostAddr), size);
CHECK_LOG_RET(ret == ACL_SUCCESS, ret, "aclrtMallocHost failed. ERROR: %d", ret);
memset(tensor.hostAddr, 0, size);
// Call aclrtMalloc to allocate device-side memory.
ret = aclrtMalloc(&(tensor.deviceAddr), size, ACL_MEM_MALLOC_HUGE_FIRST);
CHECK_LOG_RET(ret == ACL_SUCCESS, ret, "aclrtMalloc failed. ERROR: %d", ret);
// Call aclCreateTensor to create aclTensor.
tensor.data = aclCreateTensor(shape.data(), shape.size(), dataType, nullptr, 0, aclFormat::ACL_FORMAT_ND,
shape.data(), shape.size(), tensor.deviceAddr);
CHECK_LOG_RET(tensor.data != nullptr, ACL_ERROR_FAILURE, "aclCreateTensor failed");
// Call aclrtMemcpy to copy host-side data to device-side memory.
ret = aclrtMemcpy(tensor.deviceAddr, size, tensor.hostAddr, size, ACL_MEMCPY_HOST_TO_DEVICE);
CHECK_LOG_RET(ret == ACL_SUCCESS, ret, "aclrtMemcpy failed. ERROR: %d", ret);
return ACL_SUCCESS;
}
void DestroyTensor(Tensor &tensor)
{
if (tensor.data != nullptr) {
aclDestroyTensor(tensor.data);
tensor.data = nullptr;
}
if (tensor.deviceAddr != nullptr) {
aclrtFree(tensor.deviceAddr);
tensor.deviceAddr = nullptr;
}
if (tensor.hostAddr != nullptr) {
aclrtFreeHost(tensor.hostAddr);
tensor.hostAddr = nullptr;
}
}
void DestroyArgs(ArgContext &context)
{
DestroyTensor(context.metadata);
DestroyTensor(context.cuSeqlensQOptional);
DestroyTensor(context.cuSeqlensOriKvOptional);
DestroyTensor(context.cuSeqlensCmpKvOptional);
DestroyTensor(context.sequsedQOptional);
DestroyTensor(context.sequsedOriKvOptional);
DestroyTensor(context.sequsedCmpKvOptional);
DestroyTensor(context.cmpResidualKvOptional);
DestroyTensor(context.oriTopkLengthOptional);
DestroyTensor(context.cmpTopkLengthOptional);
if (context.layoutQOptional != nullptr) {
free(context.layoutQOptional);
context.layoutQOptional = nullptr;
}
if (context.layoutKvOptional != nullptr) {
free(context.layoutKvOptional);
context.layoutKvOptional = nullptr;
}
}
aclnnStatus CreateArgs(const ArgScenario &scenario, ArgContext &context)
{
ScopeGuard argsGuard([&] { DestroyArgs(context); });
aclnnStatus ret;
context.numHeadsQ = 64;
context.numHeadsKv = 1;
context.headDim = 512;
context.quantMode = 1;
ret = CreateTensor(aclDataType::ACL_INT32, {MQSMLA_METADATA_TOTAL_SIZE}, context.metadata); // 1024: Fix size
CHECK_LOG_RET(ret == ACL_SUCCESS, ret, "Create metadata failed. Error: %d", ret);
context.oriTopk = 0;
context.cmpTopk = 0;
context.cmpRatio = 128;
context.oriMaskMode = 4;
context.cmpMaskMode = 3;
context.oriWinLeft = 127;
context.oriWinRight = 0;
context.layoutQOptional = (char *)malloc(sizeof(char) * 16);
context.layoutKvOptional = (char *)malloc(sizeof(char) * 16);
CHECK_LOG_RET(context.layoutQOptional != nullptr, ACL_ERROR_FAILURE, "Create layoutQOptional failed");
CHECK_LOG_RET(context.layoutKvOptional != nullptr, ACL_ERROR_FAILURE, "Create layoutKvOptional failed");
strcpy(context.layoutQOptional, "BSND"); // BSND,TND
strcpy(context.layoutKvOptional, "BSND"); // BSND,TND,PA_BBND
context.hasOriKv = true;
context.hasCmpKv = true;
context.batchSize = 4;
context.maxSeqlenOriKv = 1024;
context.maxSeqlenCmpKv = 1024;
context.maxSeqlenQ = 1024;
if (scenario.hasCuSeq) {
// (B+1,), first element is always 0
ret = CreateTensor(aclDataType::ACL_INT32, {context.batchSize + 1}, context.cuSeqlensQOptional);
CHECK_LOG_RET(ret == ACL_SUCCESS, ret, "Create cuSeqlensQOptional failed. Error: %d", ret);
ret = CreateTensor(aclDataType::ACL_INT32, {context.batchSize + 1}, context.cuSeqlensOriKvOptional);
CHECK_LOG_RET(ret == ACL_SUCCESS, ret, "Create cuSeqlensOriKvOptional failed. Error: %d", ret);
ret = CreateTensor(aclDataType::ACL_INT32, {context.batchSize + 1}, context.cuSeqlensCmpKvOptional);
CHECK_LOG_RET(ret == ACL_SUCCESS, ret, "Create cuSeqlensCmpKvOptional failed. Error: %d", ret);
}
if (scenario.hasSeqused) {
// (B,)
ret = CreateTensor(aclDataType::ACL_INT32, {context.batchSize}, context.sequsedQOptional);
CHECK_LOG_RET(ret == ACL_SUCCESS, ret, "Create sequsedQOptional failed. Error: %d", ret);
ret = CreateTensor(aclDataType::ACL_INT32, {context.batchSize}, context.sequsedOriKvOptional);
CHECK_LOG_RET(ret == ACL_SUCCESS, ret, "Create sequsedOriKvOptional failed. Error: %d", ret);
ret = CreateTensor(aclDataType::ACL_INT32, {context.batchSize}, context.sequsedCmpKvOptional);
CHECK_LOG_RET(ret == ACL_SUCCESS, ret, "Create sequsedCmpKvOptional failed. Error: %d", ret);
}
if (context.hasCmpKv && context.cmpRatio != 1 && context.cmpMaskMode == 3) {
// (B,)
ret = CreateTensor(aclDataType::ACL_INT32, {context.batchSize}, context.cmpResidualKvOptional);
CHECK_LOG_RET(ret == ACL_SUCCESS, ret, "Create cmpResidualKvOptional failed. Error: %d", ret);
}
argsGuard.Dismiss();
return ACL_SUCCESS;
}
int main()
{
// 1. (Boilerplate) Initialize the device/stream. For details, see the external API list.
// Set deviceId based on the actual device.
int32_t deviceId = 0;
aclrtStream stream;
auto ret = Init(deviceId, &stream);
CHECK_LOG_RET(ret == ACL_SUCCESS, ret, "Init acl failed. ERROR: %d", ret);
ScopeGuard sysGuard([&] { Finalize(deviceId, stream); });
// 2. Construct the input and output based on the API definition.
ArgScenario scenario{};
scenario.hasCuSeq = false;
scenario.hasSeqused = false;
ArgContext context{};
ret = CreateArgs(scenario, context);
CHECK_LOG_RET(ret == ACL_SUCCESS, ret, "Create input arguments failed. ERROR: %d", ret);
ScopeGuard argsGuard([&] { DestroyArgs(context); });
// 3. Call the CANN operator library API. Replace it with the specific API.
// Call the first-phase API of aclnnQuantSparseFlashMlaMetadata.
uint64_t workspaceSize = 0;
aclOpExecutor *executor = nullptr;
void *workspaceAddr = nullptr;
ret = aclnnQuantSparseFlashMlaMetadataGetWorkspaceSize(
context.cuSeqlensQOptional.data, context.cuSeqlensOriKvOptional.data, context.cuSeqlensCmpKvOptional.data,
context.sequsedQOptional.data, context.sequsedOriKvOptional.data, context.sequsedCmpKvOptional.data,
context.cmpResidualKvOptional.data, context.oriTopkLengthOptional.data, context.cmpTopkLengthOptional.data,
context.numHeadsQ, context.numHeadsKv, context.headDim, context.quantMode, context.batchSize,
context.maxSeqlenQ, context.maxSeqlenOriKv, context.maxSeqlenCmpKv, context.oriTopk, context.cmpTopk,
context.cmpRatio, context.oriMaskMode, context.cmpMaskMode, context.oriWinLeft, context.oriWinRight,
context.layoutQOptional, context.layoutKvOptional, context.hasOriKv, context.hasCmpKv, context.metadata.data,
&workspaceSize, &executor);
CHECK_LOG_RET(ret == ACL_SUCCESS, ret, "aclnnQuantSparseFlashMlaMetadataGetWorkspaceSize failed. ERROR: %d\n", ret);
if (workspaceSize > static_cast<uint64_t>(0)) {
ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST);
CHECK_LOG_RET(ret == ACL_SUCCESS, ret, "allocate workspace failed. ERROR: %d\n", ret);
}
ScopeGuard workspaceGuard([&] {
if (workspaceAddr != nullptr) {
aclrtFree(workspaceAddr);
workspaceAddr = nullptr;
}
});
// Call the second-phase API of aclnnQuantSparseFlashMlaMetadata.
ret = aclnnQuantSparseFlashMlaMetadata(workspaceAddr, workspaceSize, executor, stream);
CHECK_LOG_RET(ret == ACL_SUCCESS, ret, "aclnnQuantSparseFlashMlaMetadata failed. ERROR: %d\n", ret);
// 4. (boilerplate) Synchronize and wait for the task execution to complete.
ret = aclrtSynchronizeStream(stream);
CHECK_LOG_RET(ret == ACL_SUCCESS, ret, "aclrtSynchronizeStream failed. ERROR: %d\n", ret);
// 5. Print the output.
MqsmlaMetadata result{};
ret = aclrtMemcpy(&result, sizeof(result), context.metadata.deviceAddr, sizeof(result), ACL_MEMCPY_DEVICE_TO_HOST);
CHECK_LOG_RET(ret == ACL_SUCCESS, ret, "aclrtMemcpy failed. ERROR: %d\n", ret);
for (uint32_t i = 0; i < AIC_CORE_MAX_NUM; ++i) {
printf("AIC Core%u\n", i);
printf(" Core Enable : %u\n", result.faMetadata[i][FA_CORE_ENABLE_INDEX]);
printf(" Start BN2 : %u\n", result.faMetadata[i][FA_BN2_START_INDEX]);
printf(" Start M : %u\n", result.faMetadata[i][FA_M_START_INDEX]);
printf(" Start S2 : %u\n", result.faMetadata[i][FA_S2_START_INDEX]);
printf(" End BN2 : %u\n", result.faMetadata[i][FA_BN2_END_INDEX]);
printf(" End M : %u\n", result.faMetadata[i][FA_M_END_INDEX]);
printf(" End S2 : %u\n", result.faMetadata[i][FA_S2_END_INDEX]);
printf(" First Workspace Index : %u\n", result.faMetadata[i][FA_FIRST_FD_DATA_WORKSPACE_IDX_INDEX]);
printf(" Max S2 Block Num : %u\n", result.faMetadata[i][FA_S2_MAX_NUM]);
}
for (uint32_t i = 0; i < AIV_CORE_MAX_NUM; ++i) {
printf("AIV Core%u\n", i);
printf(" Core Enable : %u\n", result.fdMetadata[i][FD_CORE_ENABLE_INDEX]);
printf(" FD Task BN2 Idx : %u\n", result.fdMetadata[i][FD_BN2_IDX_INDEX]);
printf(" FD Task M Idx : %u\n", result.fdMetadata[i][FD_M_IDX_INDEX]);
printf(" FD Task S2 Idx : %u\n", result.fdMetadata[i][FD_WORKSPACE_IDX_INDEX]);
printf(" FD Task Workspace Num : %u\n", result.fdMetadata[i][FD_WORKSPACE_NUM_INDEX]);
printf(" FD Subtask M Start : %u\n", result.fdMetadata[i][FD_M_START_INDEX]);
printf(" FD Subtask M Num : %u\n", result.fdMetadata[i][FD_M_NUM_INDEX]);
}
return 0;
}