aclnnSparseFlashMlaMetadata

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: aclnnSparseFlashMlaMetadata is the preceding operator of the aclnnSparseFlashMla operator and generates a load balancing task partitioning scheme for subsequent Attention computation. This operator does not perform actual Attention computation. Instead, it uses the input parameters to compute in AI CPU the start and end ranges of Attention computation to be processed by each AI Core, maximizing the utilization of computing resources and avoiding load imbalance across Cores.

    This operator is not recommended for standalone use. It is recommended to use it together with the aclnnSparseFlashMla operator to form a complete workflow.

  • Use case abbreviations: SWA (Sliding Window Attention), CSA (Compressed Sparse Attention), HCA (Heavily Compressed Attention).

  • Formula:

    This operator is an AICPU-scheduled operator and does not involve numerical computation. The core workflow is: parse the Q/KV sequence lengths of each batch → calculate the valid S2 range of each S1G block based on the mask mode → perform load balancing core partitioning based on the cost model → output the core partitioning metadata.

    The output metadata tensor has a shape of (1024,) and a data type of INT32. Its internal structure is as follows:

    • FA metadata area (AIC_CORE_NUM × 9 INT32 values), containing the FA-stage task information of each AICore:

      IndexMeaning
      0core_enable, Whether the core is enabled.
      1bn2_start, BN2 start index.
      2m_start, M (S1G) start index.
      3s2_start, S2 start index.
      4bn2_end, BN2 end index.
      5m_end, M end index.
      6s2_end, S2 end index.
      7first_fd_data_workspace_idx, workspace offset of the first FD reduction data.
      8max_s2_block_num, maximum number of s2 blocks allocated to a single core.
    • FD metadata area (AIV_CORE_NUM × 8 INT32 values), containing the FD reduction task information of each AIVCore:

      IndexMeaning
      0core_enable, Whether the core is enabled.
      1bn2_idx, BN2 index of the reduction task.
      2m_idx, M index of the reduction task.
      3workspace_idx, storage location of the reduction data in the workspace.
      4workspace_num, number of S2 splits across cores.
      5m_start, M axis start.
      6m_num, number of rows in the M axis.
  • Symbol Description

    SymbolMeaning
    BBatch Size
    N1/N2Number of Query/KV heads.
    DDimension of each attention head.
    GGQA grouping ratio, G=N1/N2.
    S1/S2Query/KV sequence length.
    S1GBlock index along the S1×G direction.
    mBaseSizeBase block size of the M axis, equal to G.
    s2BaseSizeBase block size of the S2 axis, fixed to 512.

Function Prototype

Each operator uses a two-phase API. Call the aclnnSparseFlashMlaMetadataGetWorkspaceSize API first to obtain the workspace size required for the computation and the executor that contains the operator computation process. Then call the aclnnSparseFlashMlaMetadata API to perform the actual computation.

aclnnStatus aclnnSparseFlashMlaMetadataGetWorkspaceSize(
    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          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 aclnnSparseFlashMlaMetadata(
    void          *workspace,
    uint64_t       workspaceSize,
    aclOpExecutor *executor,
    aclrtStream    stream)

aclnnSparseFlashMlaMetadataGetWorkspaceSize

  • Parameters

    Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor
    cuSeqlensQOptional (aclTensor*) Input Represents the number of valid tokens of q in different batches (prefix sum form).
    • Supports empty tensors.
    • The shape is fixed to (B+1, ).
    INT32 ND 1D √
    cuSeqlensOriKvOptional (aclTensor*) Input Represents the number of valid tokens of oriKv in different batches (prefix sum form).
    • Supports empty tensor.
    • shape is fixed to (B+1, ).
    INT32 ND 1-D √
    cuSeqlensCmpKvOptional(aclTensor*) Input Represents the number of valid tokens of cmpKv in different batches (prefix sum form).
    • Supports empty tensor.
    • shape is fixed to (B+1, ).
    INT32 ND 1D √
    sequsedQOptional(aclTensor*) Input Indicates the number of q tokens actually participating in computation in different batches.
    • Supports empty tensors.
    • Shape is fixed to (B, ).
    INT32 ND (B,) √
    sequsedOriKvOptional (aclTensor*) Input Indicates the number of tokens in oriKv actually participating in the computation in different batches.
    • Supports empty tensors.
    • The shape is fixed to (B, ).
    INT32 ND 1D √
    sequsedCmpKvOptional (aclTensor*) Input Indicates the number of tokens in cmpKv actually participating in the computation in different Batch values.
    • Supports empty tensors.
    • The shape is fixed to (B, ).
    INT32 ND 1-D √
    cmpResidualKvOptional (aclTensor*) Input Compressed KV residual, used to restore the pre-compression KV length used by the cmp-side mask based on cmp_len * cmpRatio + residual. Mandatory in the use case where CSA or HCA is used, cmpRatio is not equal to 1, and cmpMaskMode is 3. Usable when layoutKvOptional is BSND, TND, or PA_BBND. INT32 ND 1 dimension √
    oriTopkLengthOptional (aclTensor*) Input Represents the number of key sparse tokens in the oriKvOptional part corresponding to different q tokens.
    • Must be passed in for the SWA sparse ori_kv use case. Supports an empty Tensor in other scenarios.
    • The shape is (B, S1, N2) or (T1, N2).
    INT32 ND 2D, 3D √
    cmpTopkLengthOptional (aclTensor*) Input Represents the number of key sparse tokens in the cmpKvOptional part corresponding to different q tokens.
    • Supports empty tensors.
    • Shape is (B, S1, N2) or (T1, N2).
    INT32 ND 2D, 3D √
    numHeadsQ (int64_t) Input Number of Query heads. Value range: [1, 128]. - - - -
    numHeadsKv (int64_t) Input Number of KV heads. Only supports 1. - - - -
    headDim (int64_t) Input Dimension of each attention head. Only 512 is supported. - - - -
    batchSize (int64_t) Input Batch size of input samples. No manual specification is required when layoutQOptional is TND. The recommended value is 0. - - - -
    maxSeqlenQ (int64_t) Input Maximum number of valid q tokens in all batches. If 0 is passed in, the value is determined through interface deduction. The recommended value is 0. - - - -
    maxSeqlenOriKv (int64_t) Input Maximum number of valid oriKv tokens across all batches. If 0 is passed in, the value is derived through interface deduction. The recommended value is 0. - - - -
    maxSeqlenCmpKv (int64_t) Input Maximum number of valid cmpKv tokens in all batches. If 0 is passed in, interface deduction is used. The recommended value is 0. - - - -
    oriTopk (int64_t) Input Number of sparse tokens selected from oriKv. In the SWA sparse ori_kv scenario, this is the last dimension K of oriSparseIndicesOptional of the main operator and must be greater than 0; the recommended value is 0 in other scenarios. - - - -
    cmpTopk (int64_t) Input Number of sparse tokens selected from cmpKv. In the CSA scenario of Atlas A2/A3 products, any integer within the range [1, 8192] is supported. It is 0 in the SWA and HCA scenarios, and the recommended value is 0. - - - -
    cmpRatio (int64_t) Input Compression ratio of cmpKv relative to the KV length before compression, used to restore the KV length before compression used by the mask on the cmp side. Supported range: [1, 128]. This parameter does not participate in compressed KV computation when only oriKv is passed in. Pass 4 for CSA use cases and 128 for HCA use cases. The recommended value is 0. - - - -
    oriMaskMode (int64_t) Input Mask mode for q and oriKv computation. 0: No Mask.
    3: RightDownCausal mode.
    4: Band mode.
    Recommended value: 0.
    - - - -
    cmpMaskMode (int64_t) Input Mask mode for q and cmpKv computation. 0: No Mask.
    3: RightDownCausal mode.
    The recommended value is 0.
    - - - -
    oriWinLeft(int64_t) Input Number of tokens to extend the sliding window to the left. Supports -1 or a non-negative number. -1 indicates that the window is unrestricted. The recommended value is -1. - - - -
    oriWinRight (int64_t) Input Number of tokens the sliding window extends to the right. Supports -1 or a non-negative number. -1 indicates that the window is unrestricted. The recommended value is -1. - - - -
    layoutQOptional (char*) Input Indicates the data layout format of input q. Supports "BSND" and "TND". The recommended value is "BSND". - - - -
    layoutKvOptional (char*) Input Indicates the data layout format of the input KV. Supports "PA_BBND", "BSND", and "TND". The recommended value is "BSND". - - - -
    hasOriKv (bool) Input Whether oriKv is passed in. Set based on whether oriKv is passed in. The recommended value is true. - - - -
    hasCmpKv (bool) Input Whether cmpKv is passed in. Set to false for SWA use cases and true for CSA and HCA use cases. The recommended value is true, depending on whether cmpKv is passed in. - - - -
    metaData (aclTensor*) Output Core partitioning metadata output, used by the SparseFlashMla API. The shape is fixed to (1024, ). INT32 ND 1D ×
    workspaceSize (uint64_t*) Output Returns the workspace size to be requested on the Device side. - - - - -
    executor(aclOpExecutor**) Output Returns the op executor, which contains the operator computation flow. - - - - -
    • Atlas A3 products: sequsedQOptional and cmpTopkLengthOptional are not supported, and numHeadsQ/numHeadsKv only support 1, 2, 4, 8, 16, 32, 64, and 128; the SWA sparse ori_kv scenario supports oriTopkLengthOptional, oriTopk greater than 0, and oriMaskMode 0, and oriWinLeft and oriWinRight support non-negative values; in other SWA scenarios, oriTopk is 0, oriMaskMode is 4, oriWinLeft is 127, and oriWinRight is 0; cmpTopk supports [0, 8192], where 0 indicates non-sparse compressed KV, cmpMaskMode only supports 3, and cmpRatio only supports 1, 4, and 128.
    • Atlas A2 products: sequsedQOptional and cmpTopkLengthOptional are not supported, and numHeadsQ/numHeadsKv only support 1, 2, 4, 8, 16, 32, 64, and 128; the SWA sparse ori_kv scenario supports oriTopkLengthOptional, oriTopk greater than 0, and oriMaskMode 0, and oriWinLeft and oriWinRight support non-negative values; in other SWA scenarios, oriTopk is 0, oriMaskMode is 4, oriWinLeft is 127, and oriWinRight is 0; cmpTopk supports [0, 8192], where 0 indicates non-sparse compressed KV, cmpMaskMode only supports 3, and cmpRatio only supports 1, 4, and 128.
  • Return Value

    aclnnStatus: Returns a status code. For details, see aclnn return codes.

    The first-phase API performs input parameter validation and returns an error in the following scenarios:

    • Ascend 950PR&950DT products:

      Return Value Error Code Description
      ACLNN_ERR_INNER_CREATE_EXECUTOR 561101 Failed to create aclOpExecutor.
      ACLNN_ERR_INNER_NULLPTR 561103 workspaceSize or executor is a null pointer; optional input is a null pointer after make contiguous processing; or task failure when adding the SparseFlashMlaMetadata AICPU task.
      ACLNN_ERR_PARAM_INVALID 161002 batchSize or maxSeqlenQ is negative.
      numHeadsQ is not within the range of [1,128], numHeadsKv is not 1, numHeadsQ is not divisible by numHeadsKv, or numHeadsQ/numHeadsKv is not within the range of [1,128].
      headDim is not 512.
      oriMaskMode is not 0, 3, or 4, or cmpMaskMode is not 0 or 3.
      oriWinLeft or oriWinRight is less than -1.
      When hasCmpKv is true, cmpRatio is not within the range [1,128].
      When hasCmpKv is true, cmpTopk is negative.
      The shape, data type, or mandatory relationship of layoutQOptional, layoutKvOptional, cuSeqlens, seqused, or metaData is not within the supported range.
    • Atlas A3 products:

      Return Value Error Code Description
      ACLNN_ERR_INNER_CREATE_EXECUTOR 561101 Failed to create aclOpExecutor.
      ACLNN_ERR_INNER_NULLPTR 561103 workspaceSize or executor is a null pointer; optional input is a null pointer after making it contiguous; or task failure when adding the SparseFlashMlaMetadata AICPU task.
      ACLNN_ERR_PARAM_INVALID 161002 batchSize or maxSeqlenQ is negative.
      numHeadsQ is not within the range [1,128], numHeadsKv is not 1, numHeadsQ is not divisible by numHeadsKv, or numHeadsQ/numHeadsKv is not a power of 2 within the range [1,128].
      headDim is not 512.
      oriMaskMode is not 4 in the non-SWA sparse ori_kv use case, oriMaskMode is not 0 in the SWA sparse ori_kv use case, or cmpMaskMode is not 3.
      In non-SWA sparse ori_kv scenarios, oriWinLeft is not 127 or oriWinRight is not 0; in SWA sparse ori_kv scenarios, oriWinLeft or oriWinRight is negative.
      In SWA scenarios, cmpRatio is not 1, or cmpRatio does not match the CSA or HCA scenarios.
      cmpTopk is not within the range [0, 8192].
      In SWA sparse ori_kv scenarios, oriTopkLengthOptional is not passed in, or the shape or data type of oriTopkLengthOptional does not meet the specifications; a non-empty Tensor is passed in for cmpTopkLengthOptional.
      The shape, data type, or mandatory relationship of layoutQOptional, layoutKvOptional, cuSeqlens, seqused, or metaData is not within the supported range.
    • Atlas A2 products:

      Return Value Error Code Description
      ACLNN_ERR_INNER_CREATE_EXECUTOR 561101 Failed to create aclOpExecutor.
      ACLNN_ERR_INNER_NULLPTR 561103 workspaceSize or executor is a null pointer; the optional input is a null pointer after being made contiguous; or the SparseFlashMlaMetadata AICPU task fails to be added.
      ACLNN_ERR_PARAM_INVALID 161002 batchSize or maxSeqlenQ is negative.
      numHeadsQ is not within the range [1,128], numHeadsKv is not 1, numHeadsQ is not divisible by numHeadsKv, or numHeadsQ/numHeadsKv is not a power of 2 within the range [1,128].
      headDim is not 512.
      In the non-SWA sparse ori_kv scenario, oriMaskMode is not 4; in the SWA sparse ori_kv scenario, oriMaskMode is not 0 or cmpMaskMode is not 3.
      In the non-SWA sparse ori_kv scenario, oriWinLeft is not 127 or oriWinRight is not 0; in the SWA sparse ori_kv scenario, oriWinLeft or oriWinRight is negative.
      In the SWA scenario, cmpRatio is not 1, or cmpRatio does not match the CSA or HCA scenarios.
      cmpTopk is not within the range [0, 8192].
      In the SWA sparse ori_kv scenario, oriTopkLengthOptional is not passed in, or the shape or data type of oriTopkLengthOptional does not conform to the specification; a non-empty tensor is passed in for cmpTopkLengthOptional.
      The shape, data type, or mandatory relationship of layoutQOptional, layoutKvOptional, cuSeqlens, seqused, or metaData is not within the supported range.

aclnnSparseFlashMlaMetadata

  • 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 aclnnSparseFlashMlaMetadataGetWorkspaceSize.
    executor Input Op executor, containing the operator computation flow.
    stream Input Stream for task execution.
  • Return Value

    aclnnStatus: return status code. For details, see aclnn Return Codes.

Constraints

  • Deterministic computation

    • aclnnSparseFlashMlaMetadata defaults to a deterministic implementation. The same inputs produce the same results across multiple calls.
  • General specification constraints

    • B (Batch) indicates the batch size of input samples. q, oriKvOptional, and cmpKvOptional are input parameters of the paired aclnnSparseFlashMla operator. S1 indicates the S axis size in the q shape when layoutQOptional=BSND. T1 indicates the T axis size in the q shape when layoutQOptional=TND. S2 indicates the S axis size in the oriKvOptional shape when layoutKvOptional=BSND. S3 indicates the S axis size in the cmpKvOptional shape when layoutKvOptional=BSND. N2 indicates the N axis size in the oriKvOptional and cmpKvOptional shapes.
    • The values of cuSeqlensQOptional, cuSeqlensOriKvOptional, and cuSeqlensCmpKvOptional must be the cumulative number of valid tokens in the current and preceding batches. The first element is fixed to 0, and each subsequent element must be greater than or equal to the preceding element.
    • The values of sequsedQOptional, sequsedOriKvOptional, and sequsedCmpKvOptional must indicate the number of valid tokens in each batch.
    • Only the following layoutQOptional and layoutKvOptional combinations are supported: "BSND"/"BSND", "TND"/"TND", "BSND"/"PA_BBND", and "TND"/"PA_BBND". In non-PA_BBND use cases, layoutQOptional and layoutKvOptional must be consistent.
    • cmpResidualKvOptional must satisfy cmpResidualKvOptional[i] < cmpRatio.
  • Ascend 950PR&950DT product constraints:
    • When hasOriKv is true, the oriKvOptional part is considered sparse if oriTopk is greater than 0, and non-sparse if oriTopk is 0.
    • When hasCmpKv is true, the cmpKvOptional part is considered sparse if cmpTopk is greater than 0, and non-sparse if cmpTopk is 0.
    • When hasOriKv is true, oriTopk is not 0, and oriMaskMode is 0, oriTopkLengthOptional must be passed in. In this case, the valid seqlen of oriKvOptional corresponding to the current q token is the minimum of the oriMaskMode rule and the elements in oriTopkLengthOptional. In other oriKvOptional sparse scenarios, the valid seqlen of oriKvOptional corresponding to the current q token is the minimum of the oriMaskMode rule and oriTopk.
    • When hasCmpKv is true, cmpTopk is not 0, and cmpMaskMode is 0, cmpTopkLengthOptional must be passed in. In this case, the valid seqlen of cmpKvOptional corresponding to the current q token is the minimum of the cmpMaskMode rule and the elements in cmpTopkLengthOptional. In other cmpKvOptional sparse scenarios, the valid seqlen of cmpKvOptional corresponding to the current q token is the minimum of the cmpMaskMode rule and cmpTopk.
    • layoutQOptional=BSND scenario
      • The value of S1 must be passed to maxSeqlenQ.
    • layoutKvOptional=BSND use case
      • When hasOriKv is true, maxSeqlenOriKv must be passed in with the value of S2.
      • When hasCmpKv is true, maxSeqlenCmpKv must be passed in with the value of S3.
    • layoutQOptional=TND scenario
      • cuSeqlensQOptional is mandatory.
    • layoutKvOptional=TND use case
      • When hasOriKv is true, cuSeqlensOriKvOptional must be passed in.
      • When hasCmpKv is true, cuSeqlensCmpKvOptional must be passed in.
    • layoutKvOptional=PA_BBND use case
      • When hasOriKv is true, oriTopk is not 0, and oriMaskMode is 0 (oriTopkLengthOptional is mandatory in this scenario), sequsedOriKvOptional is optional. In other scenarios, sequsedOriKvOptional must be passed in.
      • When hasCmpKv is true, cmpTopk is not 0, and cmpMaskMode is 0 (cmpTopkLengthOptional is mandatory in this scenario), sequsedCmpKvOptional is optional. In other scenarios, sequsedCmpKvOptional must be passed in.
    • Batch value rules
      • When layoutQOptional is BSND, prefer to use the shape of sequsedQOptional to derive the batch; if sequsedQOptional is not passed, obtain the number of batches from batch_size.
      • When layoutQOptional is TND, prefer to use the shape of sequsedQOptional to derive the batch; if sequsedQOptional is not passed, derive the batch from the shape of cuSeqlensQOptional.
    • q Seqlen value rules
      • When layoutQOptional is BSND, prefer to use the elements in sequsedQOptional to obtain the seqlen; if sequsedQOptional is not passed, obtain the seqlen from maxSeqlenQ.
      • When layoutQOptional is TND, prefer to use the elements in sequsedQOptional to obtain the seqlen; if sequsedQOptional is not passed, obtain the seqlen from the elements in cuSeqlensQOptional.
    • oriKvOptional Seqlen value rule
      • When layoutKvOptional is BSND, seqlen is obtained preferentially from elements in sequsedOriKvOptional. If sequsedOriKvOptional is not passed in, seqlen is obtained from maxSeqlenOriKv.
      • When layoutKvOptional is TND, seqlen is obtained preferentially from elements in sequsedOriKvOptional. If sequsedOriKvOptional is not passed in, seqlen is obtained from elements in cuSeqlensOriKvOptional.
      • When layoutKvOptional is PA_BBND, seqlen is obtained preferentially from elements in sequsedOriKvOptional. If sequsedOriKvOptional is not passed in, seqlen is obtained from oriTopkLengthOptional.
    • cmpKvOptional Seqlen value rule
      • When layoutKvOptional is BSND, seqlen is obtained preferentially from elements in sequsedCmpKvOptional. If sequsedCmpKvOptional is not passed in, seqlen is obtained from maxSeqlenCmpKv.
      • When layoutKvOptional is TND, seqlen is obtained preferentially from elements in sequsedCmpKvOptional. If sequsedCmpKvOptional is not passed in, seqlen is obtained from elements in cuSeqlensCmpKvOptional.
      • When layoutKvOptional is PA_BBND, seqlen is obtained preferentially from elements in sequsedCmpKvOptional. If sequsedCmpKvOptional is not passed in, seqlen is obtained from cmpTopkLengthOptional.
  • Atlas A3 product constraints:
    • In the SWA sparse ori_kv use case, only the SWA template is supported. hasOriKv must be true, hasCmpKv must be false, oriTopk must be greater than 0, oriMaskMode must be 0, oriWinLeft and oriWinRight must be non-negative, and oriTopkLengthOptional must be passed in. oriTopk must be consistent with K, the last dimension of oriSparseIndicesOptional of the accompanying main operator. oriTopkLengthOptional indicates the number of left-aligned valid index entries for each q token and KV head, and its value must be within the range of [0, K]. Metadata uses only oriTopkLengthOptional to generate task partitioning. In the PA_BBND use case, the accompanying main operator still requires sequsedOriKvOptional to be passed in.
    • When layoutQOptional is TND, cuSeqlensQOptional must be passed in.
    • When layoutKvOptional is PA_BBND, sequsedOriKvOptional must be passed in. In the BSND use case, sequsedOriKvOptional is optional and overrides the valid oriKv length of each batch; in the TND use case, cuSeqlensOriKvOptional is used to express the oriKv sequence boundary.
    • When layoutKvOptional is TND, cuSeqlensOriKvOptional must be passed in; if hasCmpKv is true, cuSeqlensCmpKvOptional must also be passed in.
    • sequsedCmpKvOptional is an optional input for all layoutKvOptional values. When explicitly passed in, it is used to override the logical valid length on the cmp side.
    • cmpResidualKvOptional is an optional input of aclnnSparseFlashMlaMetadata and aclnnSparseFlashMla. It is mandatory in the CSA, HCA, cmpRatio not equal to 1, or cmpMaskMode equal to 3 use cases, and is used for recovery of the pre-compression length used by the cmp-side mask.
  • Atlas A2 product constraints:
    • In the SWA sparse ori_kv use case, only the SWA template is supported. hasOriKv must be true, hasCmpKv must be false, oriTopk must be greater than 0, oriMaskMode must be 0, oriWinLeft and oriWinRight must be non-negative, and oriTopkLengthOptional must be passed in. oriTopk must be consistent with K, the last dimension of oriSparseIndicesOptional of the accompanying main operator. oriTopkLengthOptional indicates the number of left-aligned valid index entries for each q token and KV head, and its value must be within the range of [0, K]. Metadata uses only oriTopkLengthOptional to generate task partitioning. In the PA_BBND use case, the accompanying main operator still requires sequsedOriKvOptional to be passed in.
    • When layoutQOptional is TND, cuSeqlensQOptional must be passed in.
    • When layoutKvOptional is PA_BBND, sequsedOriKvOptional must be passed in. In the BSND use case, sequsedOriKvOptional is optional and overrides the valid oriKv length of each batch; in the TND use case, cuSeqlensOriKvOptional is used to express the oriKv sequence boundary.
    • When layoutKvOptional is TND, cuSeqlensOriKvOptional must be passed in; if hasCmpKv is true, cuSeqlensCmpKvOptional must also be passed in.
    • sequsedCmpKvOptional is an optional input for all layoutKvOptional values. When explicitly passed in, it is used to override the logical valid length on the cmp side.
    • cmpResidualKvOptional is an optional input of aclnnSparseFlashMlaMetadata and aclnnSparseFlashMla. It is mandatory in the CSA, HCA, cmpRatio not equal to 1, or cmpMaskMode equal to 3 use cases, and is used for recovery of the pre-compression length used by the cmp-side mask.

Example

The following sample code is for reference only. For details about compilation and execution, see Compile and Run Samples.

/**
 * @file test_aclnn_sparse_flash_mla_metadata.cpp
 */
#include <iostream>
#include <vector>
#include <cmath>
#include <cstring>
#include <limits>
#include <functional>
#include <utility>
#include "acl/acl.h"
#include "aclnnop/aclnn_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 sparse_flash_mla_metadata.h.
constexpr uint32_t AIC_CORE_MAX_NUM = 36;
constexpr uint32_t AIV_CORE_MAX_NUM = 72;
constexpr uint32_t SMLA_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 SmlaMetadata {
    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) {}
    // Do not copy.
    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 };
    // 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 for 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 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 memory.
    ret = aclrtMalloc(&(tensor.deviceAddr), size, ACL_MEM_MALLOC_HUGE_FIRST);
    CHECK_LOG_RET(ret == ACL_SUCCESS, ret, "aclrtMalloc failed. ERROR: %d", ret);
    // Call the aclCreateTensor API to create an 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 data to device 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;
    ret = CreateTensor(aclDataType::ACL_INT32, { SMLA_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.
    // Fill in the deviceId based on your 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 according to 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 aclnnSparseFlashMlaMetadata.
    uint64_t workspaceSize = 0;
    aclOpExecutor *executor = nullptr;
    void *workspaceAddr = nullptr;
    ret = aclnnSparseFlashMlaMetadataGetWorkspaceSize(
        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.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,
        "aclnnSparseFlashMlaMetadataGetWorkspaceSize 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 aclnnSparseFlashMlaMetadata.
    ret = aclnnSparseFlashMlaMetadata(workspaceAddr, workspaceSize, executor, stream);
    CHECK_LOG_RET(ret == ACL_SUCCESS, ret, "aclnnSparseFlashMlaMetadata failed. ERROR: %d\n", ret);

    // 4. (boilerplate) Wait for the task execution to finish.
    ret = aclrtSynchronizeStream(stream);
    CHECK_LOG_RET(ret == ACL_SUCCESS, ret, "aclrtSynchronizeStream failed. ERROR: %d\n", ret);

    // 5. Print the output.
    SmlaMetadata 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 Workspace 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;
}