aclnnScatterPaCache

📄 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: Updates the key at the specified position in KCache.

  • Formula:

    • Use case 1:

      key:[batch * seq_len, num_head, k_head_size]
      keyCache:[num_blocks, block_size, num_head, k_head_size]
      slotMapping:[batch * seq_len]
      cacheMode:"Norm"
      $$ keyCache = slotMapping(key) $$
    • Scenario 2:

      key:[batch, seq_len, num_head, k_head_size]
      keyCache:[num_blocks, block_size, 1, k_head_size]
      slotMapping:[batch, num_head]
      compressLensOptional:[batch, num_head]
      compressSeqOffsetOptional:[batch * num_head]
      seqLensOptional:[batch]
      cacheMode:"Norm"
      $$ \begin{aligned} keyCache =\ & slotMapping(key[: compressSeqOffset], \\ & ReduceMean(key[compressSeqOffset : compressSeqOffset + compressLens]), \\ & key[compressSeqOffset + compressLens : seqLens]) \end{aligned} $$
    • Scenario 3:

      key:[batch, seq_len, num_head, k_head_size]
      keyCache:[num_blocks, block_size, 1, k_head_size]
      slotMapping:[batch, num_head]
      compressLensOptional:[batch * num_head]
      seqLensOptional:[batch]
      cacheMode:"Norm"
      $$ keyCache = slotMapping(key[seqLens - compressLens : seqLens]) $$

    The above scenarios are distinguished based on the constructed parameters. The first input parameter construction corresponds to Scenario 1, the second construction corresponds to Scenario 2, and the third construction corresponds to Scenario 3. Scenario 1 does not include the three optional inputs: compressLensOptional, seqLensOptional, and compressSeqOffsetOptional. Scenario 3 does not include the compressSeqOffsetOptional optional input.

Function Prototype

Each operator uses a two-phase API. First call the "aclnnScatterPaCacheGetWorkspaceSize" API to obtain the workspace size required for computation and the executor containing the operator computation flow. Then call the "aclnnScatterPaCache" API to perform the computation.

aclnnStatus aclnnScatterPaCacheGetWorkspaceSize(
  const aclTensor *key,
  aclTensor *keyCacheRef,
  const aclTensor *slotMapping,
  const aclTensor *compressLensOptional,
  const aclTensor *compressSeqOffsetOptional,
  const aclTensor *seqLensOptional,
  char            *cacheMode,
  uint64_t        *workspaceSize,
  aclOpExecutor  **executor)
aclnnStatus aclnnScatterPaCache(
  void          *workspace,
  uint64_t       workspaceSize,
  aclOpExecutor *executor,
  aclrtStream    stream)

aclnnScatterPaCacheGetWorkspaceSize

  • Parameters

    Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor
    key(aclTensor*) Input Key values to update, corresponding to key in the formula.
    • Supports Null tensors.
    • HIFLOAT8, FLOAT8_E5M2, FLOAT8_E4M3FN, FLOAT4_E2M1, and FLOAT4_E1M2 are supported only when key is 3-dimensional.
    • The shape satisfies [batch * seq_len, num_head, k_head_size] or [batch, seq_len, num_head, k_head_size]. For FLOAT4_E2M1 and FLOAT4_E1M2, k_head_size must be an even number.
    FLOAT16, FLOAT, BFLOAT16, INT8, UINT8, INT16, UINT16, INT32, UINT32, HIFLOAT8, FLOAT8_E5M2, FLOAT8_E4M3FN, FLOAT4_E2M1, FLOAT4_E1M2 ND 3-4 x
    keyCacheRef(aclTensor*) Input/Output keyCache to update, corresponding to keyCache in the formula.
    • Supports null tensor.
    • If key is 3-dimensional, the shape satisfies [num_blocks, block_size, num_head, k_head_size]. If key is 4-dimensional, the shape satisfies [num_blocks, block_size, 1, k_head_size].
    Consistent with key. ND 4 x
    slotMapping(aclTensor*) Input Storage offset of each token of key in the cache, corresponding to slotMapping in the formula.
    • Supports null tensor.
    • If key is 3-dimensional, the shape satisfies [batch * seq_len]. If key is 4-dimensional, the shape satisfies [batch, num_head].
    • Value range: [0, num_blocks * block_size-1]. Elements cannot be repeated. Correctness is not guaranteed if values are repeated.
    INT32, INT64 ND 1-2 x
    compressLensOptional(aclTensor*) Optional Input Compression length, compressLens in the formula.
    • Supports Null tensor.
    • When key is 4-dimensional and compressSeqOffsetOptional is not a null pointer, the shape satisfies [batch, num_head]. When key is 4-dimensional and compressSeqOffsetOptional is a null pointer, the shape satisfies [batch * num_head].
    • Pass a null pointer in use case 1.
    Consistent with slotMapping. ND 1-2 x
    compressSeqOffsetOptional(aclTensor*) Optional Input Start point of compression for each head in each batch, corresponding to compressSeqOffset in the formula.
    • Supports empty tensors.
    • The shape satisfies [batch * num_head].
    • A null pointer is passed in use case 1 and use case 3.
    Consistent with slotMapping. ND 1 x
    seqLensOptional(aclTensor*) Optional Input Actual seqLens of each batch, corresponding to seqLens in the formula.
    • Supports null tensor.
    • shape satisfies [batch].
    • Pass a null pointer for use case 1.
    Consistent with slotMapping. ND 1 x
    cacheMode(char*) Input Memory layout format of keyCacheRef. Reserved parameter. Only the ND data format is currently supported. This parameter does not take effect. - - - -
    workspaceSize (uint64_t*) Output Returns the size of the workspace to be requested on the Device. - - - - -
    executor(aclOpExecutor**) Output Returns the op executor, which contains the operator computation flow. - - - - -
  • Return Value

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

    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 key, keyCacheRef, or slotMapping is a null pointer.
    ACLNN_ERR_PARAM_INVALID 161002 The data type of key is not supported.
    The data types of key and keyCacheRef are inconsistent.
    The data types of slotMapping, compressLensOptional, compressSeqOffsetOptional, and seqLensOptional are inconsistent.
    ACLNN_ERR_PARAM_INVALID 561002 The dimension of key is not 3 or 4.

aclnnScatterPaCache

  • Parameters

    Parameter Input/Output Description
    workspace Input Workspace memory address allocated on the Device.
    workspaceSize Input Size of the workspace allocated on the Device, obtained by the first-phase API aclnnScatterPaCacheGetWorkspaceSize.
    executor Input Op executor, which contains the operator computation flow.
    stream Input Stream for task execution.
  • Return Value

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

Constraints

  • Deterministic computation: aclnnScatterPaCache defaults to a deterministic implementation.
  • The variables used in shape descriptions in Parameters are as follows:
    • batch: the number of sequences in the current input (the number of samples processed at a time), which is a positive integer.
    • seq_len: the length of the sequence, which is a positive integer.
    • num_head: Number of "heads" in multi-head attention, which is a positive integer.
    • k_head_size: Feature dimension of key per attention head (length of a single-head key), which is a positive integer.
    • num_blocks: Total number of pre-allocated blocks in keyCache for storing key data of all sequences, which is a positive integer.
    • block_size: Number of tokens contained in each cache block, which is a positive integer.
  • Input value range restriction: Each element value in seqLensOptional and compressLensOptional must satisfy the formula: reduceSum(seqLensOptional[i] - compressLensOptional[i] + 1) <= num_blocks * block_size (corresponding to scenarios 2 and 3).

Example

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

  • Ascend 950PR&950DT products:

    #include <iostream>
    #include <vector>
    #include "acl/acl.h"
    #include "aclnnop/aclnn_scatter_pa_cache.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 for 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 data to device 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 aclCreateTensor to create an aclTensor.
      *tensor = aclCreateTensor(shape.data(), shape.size(), dataType, strides.data(), 0, aclFormat::ACL_FORMAT_ND,
                                shape.data(), shape.size(), *deviceAddr);
      return 0;
    }
    
    int main() {
      // 1. (Boilerplate) Device/stream initialization. For details, see the acl API documentation.
      // Set deviceId based on the actual device.
      int32_t deviceId = 0;
      aclrtStream stream;
      auto ret = Init(deviceId, &stream);
      CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret);
    
      // 2. Construct inputs and outputs. Define them based on the API.
      std::vector<int64_t> keyShape = {2, 2, 3, 7};
      std::vector<int64_t> keyCacheShape = {2, 2, 1, 7};
      std::vector<int64_t> slotMappingShape = {2, 3};
    
      std::vector<int64_t> compressLensShape = {2, 3};
      std::vector<int64_t> compressSeqOffsetShape = {6};
      std::vector<int64_t> seqLensShape = {2};
      void* keyDeviceAddr = nullptr;
      void* slotMappingDeviceAddr = nullptr;
      void* keyCacheDeviceAddr = nullptr;
      void* compressLensDeviceAddr = nullptr;
      void* compressSeqOffsetDeviceAddr = nullptr;
      void* seqLensDeviceAddr = nullptr;
    
      aclTensor* key = nullptr;
      aclTensor* slotMapping = nullptr;
      aclTensor* keyCache = nullptr;
      aclTensor* compressLens = nullptr;
      aclTensor* compressSeqOffset = nullptr;
      aclTensor* seqLens = nullptr;
      char* cacheMode = const_cast<char*>("Norm");
    
      std::vector<float> hostKey = {1};
      std::vector<int32_t> hostSlotMapping = {0, 3, 6, 9, 12, 15};
      std::vector<float> hostKeyCacheRef = {1};
      std::vector<int32_t> hostCompressLens = {1, 0, 0, 0, 1, 0};
      std::vector<int32_t> hostCompressSeqOffset = {0, 0, 1, 0, 1, 1};
      std::vector<int32_t> hostSeqLens = {2, 1};
    
      // Create the key aclTensor.
      ret = CreateAclTensor(hostKey, keyShape, &keyDeviceAddr, aclDataType::ACL_FLOAT, &key);
      CHECK_RET(ret == ACL_SUCCESS, return ret);
      // Create the slotMapping aclTensor.
      ret = CreateAclTensor(hostSlotMapping, slotMappingShape, &slotMappingDeviceAddr, aclDataType::ACL_INT32, &slotMapping);
      CHECK_RET(ret == ACL_SUCCESS, return ret);
      // Create the keyCache aclTensor.
      ret = CreateAclTensor(hostKeyCacheRef, keyCacheShape, &keyCacheDeviceAddr, aclDataType::ACL_FLOAT, &keyCache);
      CHECK_RET(ret == ACL_SUCCESS, return ret);
      // Create the compressLens aclTensor.
      ret = CreateAclTensor(hostCompressLens, compressLensShape, &compressLensDeviceAddr, aclDataType::ACL_INT32, &compressLens);
      CHECK_RET(ret == ACL_SUCCESS, return ret);
      // Create the compressSeqOffset aclTensor.
      ret = CreateAclTensor(hostCompressSeqOffset, compressSeqOffsetShape, &compressSeqOffsetDeviceAddr, aclDataType::ACL_INT32, &compressSeqOffset);
      CHECK_RET(ret == ACL_SUCCESS, return ret);
      // Create the seqLens aclTensor.
      ret = CreateAclTensor(hostSeqLens, seqLensShape, &seqLensDeviceAddr, aclDataType::ACL_INT32, &seqLens);
      CHECK_RET(ret == ACL_SUCCESS, return ret);
    
      // 3. Call the CANN operator library API. Replace it with the actual API name.
      uint64_t workspaceSize = 0;
      aclOpExecutor* executor;
      // Call the first-phase API of aclnnScatterPaCache.
      ret = aclnnScatterPaCacheGetWorkspaceSize(key, keyCache, slotMapping, compressLens, compressSeqOffset, seqLens, cacheMode, &workspaceSize, &executor);
      CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnScatterPaCacheGetWorkspaceSize 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 aclnnScatterPaCache.
      ret = aclnnScatterPaCache(workspaceAddr, workspaceSize, executor, stream);
      CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnScatterPaCache 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. Obtain the output values. Copy the result from device memory to host memory. Modify according to the API definition.
      auto size = GetShapeSize(keyShape);
      std::vector<float> resultData(size, 0);
      ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), keyDeviceAddr,
                        size * sizeof(resultData[0]), ACL_MEMCPY_DEVICE_TO_HOST);
      CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy result from device to host failed. ERROR: %d\n", ret); return ret);
      for (int64_t i = 0; i < size; i++) {
        LOG_PRINT("result[%ld] is: %f\n", i, resultData[i]);
      }
    
      // 6. Release the aclTensor and aclScalar. Modify according to the API definition.
      aclDestroyTensor(key);
      aclDestroyTensor(slotMapping);
      aclDestroyTensor(keyCache);
      aclDestroyTensor(compressLens);
      aclDestroyTensor(compressSeqOffset);
      aclDestroyTensor(seqLens);
      // 7. Release device resources according to the parameter definitions of the specific API.
      aclrtFree(keyDeviceAddr);
      aclrtFree(slotMappingDeviceAddr);
      aclrtFree(keyCacheDeviceAddr);
      aclrtFree(compressLensDeviceAddr);
      aclrtFree(compressSeqOffsetDeviceAddr);
      aclrtFree(seqLensDeviceAddr);
      if (workspaceSize > 0) {
        aclrtFree(workspaceAddr);
      }
      aclrtDestroyStream(stream);
      aclrtResetDevice(deviceId);
      aclFinalize();
      return 0;
    }