aclnnKvCompressEpilog

📄 View Source Code

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: In the epilog phase of the KV Cache, performs in-place compressed updates on the KV Cache. Quantizes and compresses the bfloat16 activations, and then scatters them into the cache based on slotMapping. Tokens with the value -1 are skipped. The last 64 columns of the last dimension d of x form the rope segment, and the first d-64 columns form the nope segment. Supports three quantization modes: group(bf16 scale), group(e8m0 scale), and rope hifloat8 static + nope FLOAT4_E2M1 dynamic.

  • Formula:

    Computes the amax of each group along the last dimension (d axis) of x and quantizes each group to the target dtype. Group g is denoted as $x_g$:

    • Scenario 1 (quantMode=0): group(64) is quantized to FP8, scale is stored as bfloat16; the rope part remains bfloat16.

      $$ scale_g = \frac{\max(|x_g|)}{FP8\_MAX}, \quad q_i = \mathrm{round}\left(\frac{x_i}{scale_g}\right) $$
    • Scenario 2 (quantMode=1): Same as scenario 1, but scale is stored as float8_e8m0. When roundScale=true, scale is rounded up to a power of 2.

    • Scenario 3 (quantMode=2): The rope part (last 64 columns) applies hifloat8 static quantization, and the nope part (first d-64 columns) applies per-group FLOAT4_E2M1 dynamic quantization (FP4\_MAX=6.0), scale is written as bfloat16, and roundScale does not take effect:

      $$ rope_i = \mathrm{hifloat8}(x_i \cdot xScale), \qquad scale_g = \frac{\max(|x_g|)}{FP4\_MAX}, \quad nope_i = \mathrm{FLOAT4\_E2M1}\left(\frac{x_i}{scale_g}\right) $$
  • Example:

    cache shape:        [128, 16, 1, 384]   # 4D [blockNum, blockSize, 1, headDim], num_slots=2048, headDim≥kvCacheCol(=323)
    x shape:            [1024, 256]
    slot_mapping shape: [1024]
    quantGroupSize = 64
    quantMode = 1
    roundScale = true
    xScale = 1.0
    cache out shape:    [128, 16, 1, 384]   (in-place update)

Function Prototype

Each operator uses a two-phase API. Call the "aclnnKvCompressEpilogGetWorkspaceSize" API first to obtain the workspace size required for computation and the executor that contains the operator computation process, and then call the "aclnnKvCompressEpilog" API to perform the computation.

aclnnStatus aclnnKvCompressEpilogGetWorkspaceSize(
  aclTensor       *cacheRef,
  const aclTensor *x,
  const aclTensor *slotMapping,
  int64_t          quantGroupSize,
  int64_t          quantMode,
  bool             roundScale,
  float            xScale,
  uint64_t        *workspaceSize,
  aclOpExecutor  **executor)
aclnnStatus aclnnKvCompressEpilog(
  void          *workspace,
  uint64_t       workspaceSize,
  aclOpExecutor *executor,
  aclrtStream    stream)

aclnnKvCompressEpilogGetWorkspaceSize

  • Parameters

    Parameter Input/Output Description Instruction Data Type Data Format Shape Non-contiguous Tensor
    cacheRef (aclTensor*) Input/Output Indicates the KV Cache vector cache of the current layer. It is updated in place and corresponds to the quantization target in the formula.
  • Empty tensors are not supported.
  • Serves as both input and output.
  • Only the four-dimensional shape [blockNum, blockSize, 1, headDim] is supported, and dim2 is fixed to 1.
  • UINT8 ND [blockNum, blockSize, 1, headDim] ×
    x (const aclTensor*) Input Activation input to be quantized, corresponding to x in the formula.
  • Empty tensors are not supported.
  • The last dimension d must satisfy d % 64 == 0 and 64 < d ≤ 8192.
  • BFLOAT16 ND [bs, d] ×
    slotMapping (const aclTensor*) Input Index mapping from token to cache slot. The value -1 indicates that the token is skipped.
  • Empty tensors are not supported.
  • The dimension must equal the dimension of x minus 1.
  • The value range of elements is [-1, num_slots - 1].
  • INT32, INT64 ND [bs] ×
    quantGroupSize(int64_t) Input Quantization group size. The default value is 64. When quantMode=2, only 16/32/64 are supported, and (d-64) % quantGroupSize == 0 is required. - - - -
    quantMode (int64_t) Input Quantization mode. Enumerated values 0, 1, and 2 are supported. 0 indicates group quantization (scale is stored as bfloat16), 1 indicates group quantization (scale is stored as float8_e8m0), and 2 indicates hifloat8 static quantization for the rope part + FLOAT4_E2M1 dynamic quantization for the nope part (scale is stored as bfloat16). The default value is 1. - - - -
    roundScale (bool) Input Specifies whether to round up the scale of each group to a power of 2 in group mode. The default value is true. This parameter does not take effect when quantMode=2. - - - -
    xScale (float) Input Scaling factor for hifloat8 static quantization in the rope stage when quantMode=2; reserved and unused when quantMode=0/1. The default value is 1.0. - - - -
    workspaceSize (uint64_t*) Output Returns the size of the workspace to be allocated on the Device. - - - - -
    executor(aclOpExecutor**) Output Returns the op executor containing the operator computation process. - - - - -
  • Return Value

    aclnnStatus: Indicates the 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_PARAM_NULLPTR 161001 cacheRef, x, or slotMapping is a null pointer.
    ACLNN_ERR_PARAM_INVALID 161002 cacheRef, x, or slotMapping is an empty tensor.

aclnnKvCompressEpilog

  • Parameters

    ParameterInput/OutputDescription
    workspaceInputWorkspace memory address allocated on the Device.
    workspaceSizeInputSize of the workspace allocated on the Device, obtained by the first-phase API aclnnKvCompressEpilogGetWorkspaceSize.
    executorInputopExecutor that contains the computation flow of the operator.
    streamInputStream for task execution.
  • Return Value

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

Constraints

  • Deterministic computation: aclnnKvCompressEpilog defaults to a deterministic implementation.
  • cache supports only the 4D shape [blockNum, blockSize, 1, headDim] (num_slots = blockNum × blockSize). The second-to-last dimension is fixed to 1, and other dimension counts are not supported; non-contiguous tensors are supported only along the blockNum dimension.
  • headDim constraint: The last dimension of cache, headDim, must be ≥ the number of bytes written per row, kvCacheCol = alignment(concatCol). concatCol: (d-64)+128+⌈(d-64)/64⌉×scaleBytes for quantMode=0/1 (mode0=2, mode1=1); 64+(d-64)/2+((d-64)/quantGroupSize)×2 for quantMode=2. Alignment: no padding for quantMode=1, and 32B alignment for quantMode=0/2. Example: d=256, quantMode=1 → kvCacheCol=323 → headDim ≥ 323.
  • The dimension of slotMapping must be the dimension of x minus 1, indicating that slotMapping is obtained by flattening all dimensions of x except the last one.
  • The last dimension of x (the d axis) must satisfy d % 64 == 0 and 64 < d ≤ 8192. Per-group quantization is performed with every 64 elements as a group.
  • When quantMode=2, quantGroupSize supports only 16/32/64, the length of the nope segment (d-64) must be divisible by quantGroupSize, and x must be bfloat16.
  • Tokens with a value of -1 in slotMapping are skipped and not processed. Other valid elements must be in the range [0, num_slots - 1] and must not be duplicated; otherwise, result correctness is not guaranteed.

Example

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

#include <iostream>
#include <vector>
#include "acl/acl.h"
#include "aclnnop/aclnn_kv_compress_epilog.h"

#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 shapeSize = 1;
    for (auto i : shape) {
        shapeSize *= i;
    }
    return shapeSize;
}

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 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 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) Initialize the device/stream, see the acl API manual.
    int32_t deviceId = 0;
    aclrtStream stream;
    auto ret = Init(deviceId, &stream);
    CHECK_RET(ret == 0, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret);

    // 2. Construct the input and output. Customize the construction based on the API.
    std::vector<int64_t> cacheShape = {128, 16, 1, 384};  // 4D KV Cache [blockNum,blockSize,1,headDim], num_slots=2048
    std::vector<int64_t> xShape = {1024, 256};      // Activation to be quantized
    std::vector<int64_t> slotShape = {1024};        // Slot index mapping
    void* cacheDeviceAddr = nullptr;
    void* xDeviceAddr = nullptr;
    void* slotDeviceAddr = nullptr;
    aclTensor* cache = nullptr;
    aclTensor* x = nullptr;
    aclTensor* slotMapping = nullptr;
    std::vector<uint8_t> cacheHostData(GetShapeSize(cacheShape), 0);
    std::vector<uint16_t> xHostData(GetShapeSize(xShape), 0);  // bfloat16 stored as uint16
    std::vector<int32_t> slotHostData(GetShapeSize(slotShape), 0);
    // Create the cache aclTensor (in-place input/output).
    ret = CreateAclTensor(cacheHostData, cacheShape, &cacheDeviceAddr, aclDataType::ACL_UINT8, &cache);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    // Create the x aclTensor.
    ret = CreateAclTensor(xHostData, xShape, &xDeviceAddr, aclDataType::ACL_BF16, &x);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    // Create the slotMapping aclTensor.
    ret = CreateAclTensor(slotHostData, slotShape, &slotDeviceAddr, aclDataType::ACL_INT32, &slotMapping);
    CHECK_RET(ret == ACL_SUCCESS, return ret);

    // Attributes.
    int64_t quantGroupSize = 64;
    int64_t quantMode = 1;   // group quantization; scale is stored as float8_e8m0
    bool roundScale = true;
    float xScale = 1.0f;

    // 3. Call the CANN operator library API.
    uint64_t workspaceSize = 0;
    aclOpExecutor* executor;
    // Call the first-phase API of aclnnKvCompressEpilog.
    ret = aclnnKvCompressEpilogGetWorkspaceSize(cache, x, slotMapping, quantGroupSize, quantMode, roundScale, xScale,
                                                &workspaceSize, &executor);
    CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnKvCompressEpilogGetWorkspaceSize failed. ERROR: %d\n", ret); return ret);
    // Allocate device memory based on the workspaceSize calculated 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 aclnnKvCompressEpilog.
    ret = aclnnKvCompressEpilog(workspaceAddr, workspaceSize, executor, stream);
    CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnKvCompressEpilog failed. ERROR: %d\n", ret); return ret);
    // 4. (boilerplate) Synchronize and 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. cache is updated in place, and the result has been written back to cacheDeviceAddr. Copy it back to the host as needed for viewing.

    // 6. Release aclTensor. Modify it based on the specific API definition.
    aclDestroyTensor(cache);
    aclDestroyTensor(x);
    aclDestroyTensor(slotMapping);

    // 7. Release device resources.
    aclrtFree(cacheDeviceAddr);
    aclrtFree(xDeviceAddr);
    aclrtFree(slotDeviceAddr);
    if (workspaceSize > 0) {
        aclrtFree(workspaceAddr);
    }
    aclrtDestroyStream(stream);
    aclrtResetDevice(deviceId);
    aclFinalize();
    return 0;
}