aclnnFusedLinearOnlineMaxSum

Applicable Products

  • Ascend 950PR&950DT products: Not 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:

    The implementation is functionally equivalent to Megatron's matmul and fused_vocab_parallel_cross_entropy. It supports fusing matmul and celoss with splitting across devices along the vocabulary_size dimension. The computation is split into aclnnFusedLinearOnlineMaxSum and aclnnFusedCrossEntropyLossWithMaxSum, with communication in between. The two APIs need to be called in sequence to implement the complete function.

  • Formula:

    1. Perform matrix multiplication of inputinput and weightTweight^T to obtain:vocabParallelLogitsOutOptional=input@weightTvocabParallelLogitsOutOptional = input @ weight^T
    2. Calculate the maximum value of each row of vocabParallelLogitsOutOptionalvocabParallelLogitsOutOptional:logitsMaxLocalOut=max(vocabParallelLogitsOutOptional,dim=−1)logitsMaxLocalOut = max(vocabParallelLogitsOutOptional, dim=-1)
    3. Calculate the difference between vocabParallelLogitsOutOptionalvocabParallelLogitsOutOptional and logitsMaxLocalOutlogitsMaxLocalOut:subRes[b][n]=vocabParallelLogitsOutOptional[b][n]−logitsMaxLocalOut[b]subRes[b][n] = vocabParallelLogitsOutOptional[b][n] - logitsMaxLocalOut[b]
    4. Calculate the sum of each row of subRessubRes after exponential operation.sumExpLogitsLocalOut=sum(exp(subRes),dim=−1)sumExpLogitsLocalOut = sum(exp(subRes), dim=-1)
    5. Compute the mask where targettarget is less than vocabStartIndexvocabStartIndex or targettarget is greater than vocabEndIndexvocabEndIndextargetMask=(target<vocabStartIndex)∣(target>vocabEndIndex)targetMask = (target < vocabStartIndex) | (target > vocabEndIndex)
    6. Compute maskedTargetOutmaskedTargetOutmaskedTargetOut[b]={0targetMask[b]=truetarget[b]−vocabStartIndextargetMask[b]=falsemaskedTargetOut[b] = \begin{cases} 0 & \text{targetMask[b]=true}\\ target[b] - vocabStartIndex & \text{targetMask[b]=false} \end{cases}
    7. Compute predictedLogitsLocalOutpredictedLogitsLocalOutpredictedLogitsLocalOut[b]={0targetMask[b]=truesubRes[b][maskedTargetOut[b]]targetMask[b]=falsepredictedLogitsLocalOut[b] = \begin{cases} 0 & \text{targetMask[b]=true}\\ subRes[b][maskedTargetOut[b]] & \text{targetMask[b]=false} \end{cases}
    8. Compute targetMaskOuttargetMaskOutalignNum=(input.size(0)+7)/8∗8maskBit[p]={uint8(targetMask[p])p < input.size(0)1input.size(0) <= p < alignNumtargetMaskOut[k]=0b(maskBit[8∗k:8∗k+8])alignNum = (input.size(0) + 7) / 8 * 8\\ maskBit[p] = \begin{cases} uint8(targetMask[p]) & \text{p < input.size(0)}\\ 1 & \text{input.size(0) <= p < alignNum} \end{cases} \\ targetMaskOut[k] = 0b(maskBit[8*k:8*k+8])

    Where 0≤b<input.size(0),0≤n<weight.size(0),0≤p<alignNum,0≤k<alignNum/80 \le b \lt input.size(0), 0 \le n \lt weight.size(0), 0 \le p \lt alignNum, 0 \le k \lt alignNum / 8.

Function Prototype

Each operator uses a two-phase API. First call the "aclnnFusedLinearOnlineMaxSumGetWorkspaceSize" API to obtain the workspace size required for the computation and the executor that contains the operator computation process. Then call the "aclnnFusedLinearOnlineMaxSum" API to perform the computation.

aclnnStatus aclnnFusedLinearOnlineMaxSumGetWorkspaceSize(
  const aclTensor* input,
  const aclTensor* weight,
  const aclTensor* target,
  int64_t          vocabStartIndex,
  int64_t          vocabEndIndex,
  aclTensor*       logitsMaxLocalOut,
  aclTensor*       sumExpLogitsLocalOut,
  aclTensor*       predictedLogitsLocalOut,
  aclTensor*       targetMaskOut,
  aclTensor*       maskedTargetOut,
  aclTensor*       vocabParallelLogitsOutOptional,
  uint64_t*        workspaceSize,
  aclOpExecutor**  executor)
aclnnStatus aclnnFusedLinearOnlineMaxSum(
  void*           workspace,
  uint64_t        workspaceSize,
  aclOpExecutor*  executor,
  aclrtStream     stream)

aclnnFusedLinearOnlineMaxSumGetWorkspaceSize

  • Parameters

    Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor
    Input Input Indicates the left matrix of the matmul computation, that is, input in the formula.
    • Supports empty tensors.
    • input.size(1) must be less than or equal to 65534.
    BFLOAT16, FLOAT16 ND 2 √
    weight Input Indicates the right matrix of the matmul computation, that is, weight in the formula.
    • Supports empty tensors.
    • The data type must be consistent with that of input.
    • weight.size(0) needs to be greater than 0.
    • weight.size(1) needs to be consistent with input.size(1).
    BFLOAT16, FLOAT16 ND 2 √
    target Input Indicates the target index, that is, target in the formula.
    • Supports empty tensors.
    • target.size(0) needs to be consistent with input.size(0).
    INT32, INT64 ND 1 √
    vocabStartIndex Input Indicates the start index allocated to this device, i.e., vocabStartIndex in the formula.
    • Value range: [0, max(target) - 1].
    INT64 - - -
    vocabEndIndex Input Indicates the end index allocated to the current device, which is vocabEndIndex in the formula.
    • Value range: [vocabStartIndex, min(vocabStartIndex + weight.size(0) - 1, max(target) - 1)].
    INT64 - - -
    logitsMaxLocalOut Output Indicates the maximum value of each row after matmul computation, corresponding to logitsMaxLocalOut in the formula.
    • logitsMaxLocalOut.size(0) needs to be consistent with input.size(0).
    FLOAT32 ND 1 √
    sumExpLogitsLocalOut Output Indicates the accumulation result within each row after exp computation on the difference between the matmul computation result and the maximum value of each row, corresponding to sumExpLogitsLocalOut in the formula.
    • sumExpLogitsLocalOut.size(0) needs to be consistent with input.size(0).
    FLOAT32 ND 1 √
    predictedLogitsLocalOut Output Indicates the result of subtracting the maximum value of each row from the matmul computation result and then filtering the result by maskedTargetOut, that is, predictedLogitsLocalOut in the formula.
    • predictedLogitsLocalOut.size(0) needs to be consistent with input.size(0).
    FLOAT32 ND 1 √
    targetMaskOut Output Indicates the mask used to filter the vocabulary, that is, targetMaskOut in the formula.
    • The shape is [(input.size(0) + 7) / 8].
    UINT8 ND 1 √
    maskedTargetOut Output Indicates the result of target filtered by targetMaskOut, that is, maskedTargetOut in the formula.
    • The data type needs to be consistent with that of target.
    • maskedTargetOut.size(0) needs to be consistent with input.size(0).
    INT32, INT64 ND 1 √
    vocabParallelLogitsOutOptional Output Indicates the matmul computation result, an optional output, corresponding to vocabParallelLogitsOutOptional in the formula.
    • The data type needs to be consistent with input.
    • The shape is [input.size(0), weight.size(0)].
    • If vocabParallelLogitsOutOptional is nullptr, the memory-saving branch is used. Otherwise, the high-performance branch is used.
    BFLOAT16, FLOAT16 ND 2 √
    workspaceSize Output Indicates the workspace size to allocate on the Device. - - - - -
    executor Output Returns the op executor, which contains the operator computation flow. - - - - -
  • Return Value

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

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

    Error Code Error Code Description
    ACLNN_ERR_PARAM_NULLPTR 161001 The input, weight, target, logitsMaxLocalOut, sumExpLogitsLocalOut, predictedLogitsLocalOut, targetMaskOut, or maskedTargetOut passed in is a null pointer.
    ACLNN_ERR_PARAM_INVALID 161002 The data types of input, weight, target, logitsMaxLocalOut, sumExpLogitsLocalOut, predictedLogitsLocalOut, targetMaskOut, and maskedTargetOut are not within the supported range.
    vocabParallelLogitsOutOptional is not empty, and its data type is not within the supported range.
    The data formats of input, weight, target, logitsMaxLocalOut, sumExpLogitsLocalOut, predictedLogitsLocalOut, targetMaskOut, and maskedTargetOut are not within the supported range.
    vocabParallelLogitsOutOptional is not empty, and its data format is not within the supported range.
    The shapes of input, weight, target, logitsMaxLocalOut, sumExpLogitsLocalOut, predictedLogitsLocalOut, targetMaskOut, and maskedTargetOut do not meet the constraints.
    vocabParallelLogitsOutOptional is not empty, and its shape does not meet the constraints.
    The data types of input and weight are not consistent.
    The data types of target and maskedTargetOut are not consistent.
    vocabParallelLogitsOutOptional is not a null pointer and the data type of vocabParallelLogitsOutOptional is inconsistent with that of input.
    vocabStartIndex is less than 0.
    vocabEndIndex is less than vocabStartIndex.

aclnnFusedLinearOnlineMaxSum

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

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

Constraints

  • Determinism:

    • Atlas A2 products, Atlas A3 products: aclnnFusedLinearOnlineMaxSum defaults to a deterministic implementation.

Example

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

#include <iostream>
#include <vector>
#include "acl/acl.h"
#include "aclnn/opdev/fp16_t.h"
#include "aclnnop/aclnn_fused_linear_online_max_sum.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 shape_size = 1;
  for (auto i : shape) {
    shape_size *= i;
  }
  return shape_size;
}

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);

  // Compute the strides of a 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. 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);
  // Handle the check as needed.
  CHECK_RET(ret == 0, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret);
  // 2. Construct the input and output. Custom construction is required based on the API.
  int64_t mSize = 128;
  int64_t kSize = 64;
  int64_t nSize = 256;
  std::vector<int64_t> inputShape = {mSize, kSize};
  std::vector<int64_t> weightShape = {nSize, kSize};
  std::vector<int64_t> targetShape = {mSize};
  std::vector<int64_t> logitsMaxLocalOutShape = {mSize};
  std::vector<int64_t> sumExpLogitsLocalOutShape = {mSize};
  std::vector<int64_t> predictedLogitsLocalOutShape = {mSize};
  std::vector<int64_t> targetMaskOutShape = {(mSize + 7) / 8};
  std::vector<int64_t> maskedTargetOutShape = {mSize};
  std::vector<int64_t> vocabParallelLogitsOutOptionalShape = {mSize, nSize};
  void* inputDeviceAddr = nullptr;
  void* weightDeviceAddr = nullptr;
  void* targetDeviceAddr = nullptr;
  void* logitsMaxLocalOutDeviceAddr = nullptr;
  void* sumExpLogitsLocalOutDeviceAddr = nullptr;
  void* predictedLogitsLocalOutDeviceAddr = nullptr;
  void* targetMaskOutDeviceAddr = nullptr;
  void* maskedTargetOutDeviceAddr = nullptr;
  void* vocabParallelLogitsOutOptionalDeviceAddr = nullptr;
  aclTensor* input = nullptr;
  aclTensor* weight = nullptr;
  aclTensor* target = nullptr;
  aclTensor* logitsMaxLocalOut = nullptr;
  aclTensor* sumExpLogitsLocalOut = nullptr;
  aclTensor* predictedLogitsLocalOut = nullptr;
  aclTensor* targetMaskOut = nullptr;
  aclTensor* maskedTargetOut = nullptr;
  aclTensor* vocabParallelLogitsOutOptional = nullptr;
  std::vector<op::fp16_t> inputHostData(mSize * kSize, 1.0);
  std::vector<op::fp16_t> weightHostData(nSize * kSize, 1.0);
  std::vector<int32_t> targetHostData(mSize, 1);
  std::vector<float> logitsMaxLocalOutHostData(mSize, 0);
  std::vector<float> sumExpLogitsLocalOutHostData(mSize, 0);
  std::vector<float> predictedLogitsLocalOutHostData(mSize, 0);
  std::vector<uint8_t> targetMaskOutHostData((mSize + 7) / 8, 0);
  std::vector<int32_t> maskedTargetOutHostData(mSize, 0);
  std::vector<op::fp16_t> vocabParallelLogitsOutOptionalHostData(mSize * nSize, 0);
  int64_t vocabStartIndex = 0;
  int64_t vocabEndIndex = 64;
  // Create the input aclTensor.
  ret = CreateAclTensor(inputHostData, inputShape, &inputDeviceAddr, aclDataType::ACL_FLOAT16, &input);
  CHECK_RET(ret == ACL_SUCCESS, return ret);
  // Create the weight aclTensor.
  ret = CreateAclTensor(weightHostData, weightShape, &weightDeviceAddr, aclDataType::ACL_FLOAT16, &weight);
  CHECK_RET(ret == ACL_SUCCESS, return ret);
  // Create the target aclTensor.
  ret = CreateAclTensor(targetHostData, targetShape, &targetDeviceAddr, aclDataType::ACL_INT32, &target);
  CHECK_RET(ret == ACL_SUCCESS, return ret);
  // Create the logitsMaxLocalOut aclTensor.
  ret = CreateAclTensor(logitsMaxLocalOutHostData, logitsMaxLocalOutShape, &logitsMaxLocalOutDeviceAddr, aclDataType::ACL_FLOAT, &logitsMaxLocalOut);
  CHECK_RET(ret == ACL_SUCCESS, return ret);
  // Create the sumExpLogitsLocalOut aclTensor.
  ret = CreateAclTensor(sumExpLogitsLocalOutHostData, sumExpLogitsLocalOutShape, &sumExpLogitsLocalOutDeviceAddr, aclDataType::ACL_FLOAT, &sumExpLogitsLocalOut);
  CHECK_RET(ret == ACL_SUCCESS, return ret);
  // Create the predictedLogitsLocalOut aclTensor.
  ret = CreateAclTensor(predictedLogitsLocalOutHostData, predictedLogitsLocalOutShape, &predictedLogitsLocalOutDeviceAddr, aclDataType::ACL_FLOAT, &predictedLogitsLocalOut);
  CHECK_RET(ret == ACL_SUCCESS, return ret);
  // Create the targetMaskOut aclTensor.
  ret = CreateAclTensor(targetMaskOutHostData, targetMaskOutShape, &targetMaskOutDeviceAddr, aclDataType::ACL_UINT8, &targetMaskOut);
  CHECK_RET(ret == ACL_SUCCESS, return ret);
  // Create the maskedTargetOut aclTensor.
  ret = CreateAclTensor(maskedTargetOutHostData, maskedTargetOutShape, &maskedTargetOutDeviceAddr, aclDataType::ACL_INT32, &maskedTargetOut);
  CHECK_RET(ret == ACL_SUCCESS, return ret);
  // Create the vocabParallelLogitsOutOptional aclTensor.
  ret = CreateAclTensor(vocabParallelLogitsOutOptionalHostData, vocabParallelLogitsOutOptionalShape, &vocabParallelLogitsOutOptionalDeviceAddr, aclDataType::ACL_FLOAT16, &vocabParallelLogitsOutOptional);
  CHECK_RET(ret == ACL_SUCCESS, return ret);

  // 3. Call the CANN operator library API. Replace it with the actual API.
  uint64_t workspaceSize = 0;
  aclOpExecutor* executor;
  // Call the first-phase API of aclnnFusedLinearOnlineMaxSum.
  ret = aclnnFusedLinearOnlineMaxSumGetWorkspaceSize(input, weight, target, vocabStartIndex, vocabEndIndex, logitsMaxLocalOut, sumExpLogitsLocalOut, predictedLogitsLocalOut, targetMaskOut, maskedTargetOut, vocabParallelLogitsOutOptional, &workspaceSize, &executor);
  CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnFusedLinearOnlineMaxSumGetWorkspaceSize failed. ERROR: %d\n", ret); return ret);
  // Allocate device memory based on the workspaceSize computed 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 aclnnFusedLinearOnlineMaxSum second-phase API.
  ret = aclnnFusedLinearOnlineMaxSum(workspaceAddr, workspaceSize, executor, stream);
  CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnFusedLinearOnlineMaxSum failed. ERROR: %d\n", ret); return ret);
  // 4. (boilerplate) Synchronize and wait for the task execution to complete.
  ret = aclrtSynchronizeStream(stream);
  CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret);
  // 5. Obtain the output value and copy the result on device memory to host memory. Modify the code based on the specific API definition.
  auto size = GetShapeSize(logitsMaxLocalOutShape);
  std::vector<float> logitsMaxLocalOutResultData(size, 0);
  ret = aclrtMemcpy(logitsMaxLocalOutResultData.data(), logitsMaxLocalOutResultData.size() * sizeof(logitsMaxLocalOutResultData[0]), logitsMaxLocalOutDeviceAddr, size * sizeof(logitsMaxLocalOutResultData[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("logitsMaxLocalOut[%ld] is: %f\n", i, logitsMaxLocalOutResultData[i]);
  }

  size = GetShapeSize(sumExpLogitsLocalOutShape);
  std::vector<float> sumExpLogitsLocalOutResultData(size, 0);
  ret = aclrtMemcpy(sumExpLogitsLocalOutResultData.data(), sumExpLogitsLocalOutResultData.size() * sizeof(sumExpLogitsLocalOutResultData[0]), sumExpLogitsLocalOutDeviceAddr, size * sizeof(sumExpLogitsLocalOutResultData[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("sumExpLogitsLocalOut[%ld] is: %f\n", i, sumExpLogitsLocalOutResultData[i]);
  }

  size = GetShapeSize(predictedLogitsLocalOutShape);
  std::vector<float> predictedLogitsLocalOutResultData(size, 0);
  ret = aclrtMemcpy(predictedLogitsLocalOutResultData.data(), predictedLogitsLocalOutResultData.size() * sizeof(predictedLogitsLocalOutResultData[0]), predictedLogitsLocalOutDeviceAddr, size * sizeof(predictedLogitsLocalOutResultData[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("predictedLogitsLocalOut[%ld] is: %f\n", i, predictedLogitsLocalOutResultData[i]);
  }

  size = GetShapeSize(targetMaskOutShape);
  std::vector<uint8_t> targetMaskOutResultData(size, 0);
  ret = aclrtMemcpy(targetMaskOutResultData.data(), targetMaskOutResultData.size() * sizeof(targetMaskOutResultData[0]), targetMaskOutDeviceAddr, size * sizeof(targetMaskOutResultData[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("targetMaskOut[%ld] is: %hhu\n", i, targetMaskOutResultData[i]);
  }

  size = GetShapeSize(maskedTargetOutShape);
  std::vector<int32_t> maskedTargetOutResultData(size, 0);
  ret = aclrtMemcpy(maskedTargetOutResultData.data(), maskedTargetOutResultData.size() * sizeof(maskedTargetOutResultData[0]), maskedTargetOutDeviceAddr, size * sizeof(maskedTargetOutResultData[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("maskedTargetOut[%ld] is: %d\n", i, maskedTargetOutResultData[i]);
  }

  size = GetShapeSize(vocabParallelLogitsOutOptionalShape);
  std::vector<op::fp16_t> vocabParallelLogitsOutOptionalResultData(size, 0);
  ret = aclrtMemcpy(vocabParallelLogitsOutOptionalResultData.data(), vocabParallelLogitsOutOptionalResultData.size() * sizeof(vocabParallelLogitsOutOptionalResultData[0]), vocabParallelLogitsOutOptionalDeviceAddr, size * sizeof(vocabParallelLogitsOutOptionalResultData[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("vocabParallelLogitsOutOptional[%ld] is: %f\n", i, static_cast<float>(vocabParallelLogitsOutOptionalResultData[i]));
  }

  // 6. Release aclTensor and aclScalar. Modify the code based on the specific API definition.
  aclDestroyTensor(input);
  aclDestroyTensor(weight);
  aclDestroyTensor(target);
  aclDestroyTensor(logitsMaxLocalOut);
  aclDestroyTensor(sumExpLogitsLocalOut);
  aclDestroyTensor(predictedLogitsLocalOut);
  aclDestroyTensor(targetMaskOut);
  aclDestroyTensor(maskedTargetOut);
  aclDestroyTensor(vocabParallelLogitsOutOptional);

  // 7. Release device resources. Modify the code based on the specific API definition.
  aclrtFree(inputDeviceAddr);
  aclrtFree(weightDeviceAddr);
  aclrtFree(targetDeviceAddr);
  aclrtFree(logitsMaxLocalOutDeviceAddr);
  aclrtFree(sumExpLogitsLocalOutDeviceAddr);
  aclrtFree(predictedLogitsLocalOutDeviceAddr);
  aclrtFree(targetMaskOutDeviceAddr);
  aclrtFree(maskedTargetOutDeviceAddr);
  aclrtFree(vocabParallelLogitsOutOptionalDeviceAddr);

  if (workspaceSize > 0) {
    aclrtFree(workspaceAddr);
  }
  aclrtDestroyStream(stream);
  aclrtResetDevice(deviceId);
  aclFinalize();
  return 0;
}