aclnnCtcLoss

📄 View Source Code

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: Supported
  • Atlas training products: Supported

Function

  • Description: Computes the connectionist temporal classification loss value.

  • Formula: Define ykty_{k}^{t} as the probability that the true character is kk at time tt. (Generally, ykty_{k}^{t} is an element in the output matrix after softmax.) The set of all sequences that can be formed from the character set L′L^{'} is called L′TL^{'T}, and any sequence in L′TL^{'T} is called a path, denoted as ππ. The distribution of ππ is given by formula (1):

    p(π∣x)=∏t=1Tyπtt,∀π∈L′T.(1)p(π|x)=\prod_{t=1}^{T}y^{t}_{π_{t}} , \forall π \in L'^{T}. \tag{1}

    Define a many-to-one mapping B: L′T→L≤TL^{'T} \to L^{\leq T}. The conditional probability of l∈L≤Tl \in L^{\leq T} obtained through mapping B is equal to the sum of probabilities of all possible paths corresponding to ll, as shown in formula (2):

    p(l∣x)=∑π∈B−1(l)p(π∣x).(2)p(l|x)=\sum_{π \in B^{-1}(l)}p(π|x).\tag{2}

    The task of finding the path of ll that maximizes p(l∣x)p(l|x) is called decoding, as shown in formula (3):

    h(x)=l∈L≤Targ max p(l∣x).(3)h(x)=^{arg \ max}_{l \in L^{ \leq T}} \ p(l|x).\tag{3}

    When zeroInfinity is True

    h(x)={0,h(x)==Inf or h(x)==−Infh(x), else h(x)=\begin{cases}0,&h(x) == Inf \text{ or } h(x) == -Inf \\h(x),&\text { else }\end{cases}

Function Prototype

Each operator uses a two-phase API. First call the "aclnnCtcLossGetWorkspaceSize" API to obtain the workspace size required for the computation and the executor containing the operator computation flow, and then call the "aclnnCtcLoss" API to perform the computation.

aclnnStatus aclnnCtcLossGetWorkspaceSize(
    const aclTensor*     logProbs,
    const aclTensor*     targets,
    const aclIntArray*   inputLengths,
    const aclIntArray*   targetlengths,
    int64_t              blank,
    bool                 zeroInfinity,
    aclTensor*           negLogLikelihoodOut,
    aclTensor*           logAlphaOut,
    uint64_t*            workspaceSize,
    aclOpExecutor**      executor)
aclnnStatus aclnnCtcLoss(
    void*          workspace,
    uint64_t       workspaceSize,
    aclOpExecutor* executor,
    aclrtStream    stream)

aclnnCtcLossGetWorkspaceSize

  • Parameters:

    Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor
    logProbs (aclTensor*) Input Represents the log probabilities of the output, y in the formula. - FLOAT16, FLOAT, BFLOAT16, DOUBLE ND (T,N,C) or (T,C)
    T is the input length, N is the batch size, and C is the number of classes. C must be greater than 0, including the blank symbol.
    √
    targets (aclTensor*) Input Represents the labels containing the target sequences, π in the formula. The shape is (N,S), where S is not less than the maximum value in targetlengths; or the shape is (SUM(targetlengths)), assuming that targets are unpadded and concatenated in 1 dimension. When logProbs is 2-dimensional, N=1. INT64, INT32, BOOL, FLOAT, FLOAT16 ND - √
    inputLengths (aclIntArray*) Input Indicates the actual length of the input sequence. T in the formula is an element of inputLengths. The array length is N, and each value in the array must be less than or equal to T. When logProbs is 2-dimensional, N=1. INT64, INT32 - - -
    targetlengths (aclIntArray*) Input Indicates the actual length of the target sequence. The length of l in the formula is an element in targetlengths. The array length is N. When the shape of targets is (N,S), each value in the array must be less than or equal to S. When logProbs is 2D, N=1. INT64, INT32 - - -
    blank (int64_t) Input Blank label. The value must be greater than or equal to 0 and less than C. INT64 - - -
    zeroInfinity (bool) Input Indicates whether to zero infinite losses and associated gradients, corresponding to zeroInfinity in the formula. - BOOL - - -
    negLogLikelihoodOut (aclTensor*) Output Indicates the output loss value, corresponding to h in the formula. The data type must be consistent with that of logProbs. When logProbs has 3 dimensions, negLogLikelihoodOut is a Tensor with the (N) shape; otherwise, negLogLikelihoodOut is a 0-dimension Tensor. Consistent with logProbs ND - √
    logAlphaOut(aclTensor*) Output Probability of the possible paths from the input to the target, that is, p(l|x) in the formula. The data type must be consistent with that of logProbs. When logProbs has 2 dimensions, N=1. Consistent with logProbs ND - √
    workspaceSize (uint64_t*) Output Returns the workspace size to be allocated on the Device. - - - - -
    executor (aclOpExecutor**) Output Returns the op executor, which contains the computation flow of the operator. - - - - -
    • logAlphaOut:

      • Atlas A2 products, Atlas A3 products: shape is (N,T,(2∗max(targetlengths)+8)/8∗8N, T, (2*max(targetlengths)+8)/8 * 8).
      • Ascend 950PR&950DT products: shape is (N,T,(2∗max(targetlengths)+1)N, T, (2*max(targetlengths)+1)).
  • Return value

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

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

    Return value Error code Description
    ACLNN_ERR_PARAM_NULLPTR 161001 When the passed-in logProbs, targets, inputLengths, targetlengths, negLogLikelihoodOut, or logAlphaOut is a null pointer.
    ACLNN_ERR_PARAM_INVALID 161002 The data types of logProbs, targets, inputLengths, and targetlengths are not within the supported range.
    The tensors of logProbs, targets, inputLengths, targetlengths, negLogLikelihoodOut, and logAlphaOut do not meet the corresponding shape requirements, or the ArrayList length of inputLengths or targetlengths does not meet the requirement.
    blank is not within the value range.

aclnnCtcLoss

  • Parameters:

    Parameter Input/Output Description
    workspace Input Memory address of the workspace requested on the Device.
    workspaceSize Input Size of the workspace requested on the Device, obtained by the first-phase API aclnnCtcLossGetWorkspaceSize.
    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

  • Value Range Constraints:

    • The value range of targets must be [0,C−1][0, C - 1] and exclude the value corresponding to blank, where CC represents the last dimension of logProbs, i.e., the number of classes.
    • The value range of inputLengths must be [1,T][1, T], where TT represents the 0th dimension of logProbs, i.e., the input length.
    • The value range of targetlengths must be greater than or equal to 1.
    • Each element in targetlengths must be less than or equal to the corresponding element in inputLengths.

    If the first three value range constraints are not met, out-of-bounds behavior may occur on CPU/GPU, causing the computation results of negLogLikelihoodOut and logAlphaOut to potentially differ from those on CPU/GPU. If the fourth value range constraint is not met, the computation result of logAlphaOut on the corresponding batch will differ from that on CPU/GPU.

  • Deterministic computation:

    • aclnnCtcLoss 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.

  • Atlas A2 products, Atlas A3 products:

    #include <iostream>
    #include <vector>
    #include "acl/acl.h"
    #include "aclnnop/aclnn_ctc_loss.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: 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-side 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 data from the host to the 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) 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);
      // Handle the check as needed.
      CHECK_RET(ret == 0, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret);
      // 2. Construct the inputs and outputs, which need to be custom-built based on the API.
      std::vector<int64_t> logProbsShape = {12, 4, 5};
      std::vector<int64_t> targetsShape = {4, 7};
      std::vector<int64_t> negLoglikelihoodOutShape = {4};
      std::vector<int64_t> logAlphaOutShape = {4, 12, 16};
      void* logProbsDeviceAddr = nullptr;
      void* targetsDeviceAddr = nullptr;
      void* negLoglikelihoodOutDeviceAddr = nullptr;
      void* logAlphaOutDeviceAddr = nullptr;
      aclTensor* logProbs = nullptr;
      aclTensor* targets = nullptr;
      aclIntArray* inputLengths = nullptr;
      aclIntArray* targetLengths = nullptr;
      aclTensor* negLoglikelihoodOut = nullptr;
      aclTensor* logAlphaOut = nullptr;
      std::vector<float> logProbsHostData = {
        -1.0894, -2.7162, -0.9764, -1.9126, -2.6162,
        -2.0684, -2.4871, -2.0866, -1.7205, -0.7187,
        -2.4423, -1.2017, -1.4653, -1.1821, -2.5942,
        -2.4670, -2.7257, -1.4135, -2.1042, -0.7248,
    
        -3.7759, -1.3742, -1.2549, -1.5807, -1.4562,
        -1.3826, -1.8995, -1.8527, -0.9493, -2.8895,
        -1.6316, -2.6603, -2.5014, -0.6992, -1.8609,
        -1.9269, -2.2350, -0.8073, -1.8906, -1.8947,
    
        -0.3468, -2.5855, -2.0723, -2.7147, -3.6668,
        -0.9541, -1.7258, -2.0693, -1.6378, -2.1531,
        -3.5386, -3.4830, -0.2532, -2.0557, -3.3261,
        -1.1480, -1.8080, -0.8244, -3.2414, -3.1909,
    
        -0.8866, -0.7540, -4.4312, -3.4634, -2.6000,
        -1.2785, -1.8347, -3.3122, -0.7620, -2.8349,
        -1.4975, -1.3865, -0.9645, -3.8171, -2.0939,
        -2.3536, -2.0773, -1.4981, -0.8372, -2.0938,
    
        -1.2186, -0.8285, -2.9399, -2.1159, -2.3620,
        -2.3139, -0.6503, -2.7249, -1.2340, -3.7927,
        -0.7143, -2.5084, -3.2826, -2.6651, -1.1334,
        -1.6965, -1.9728, -2.3849, -1.6052, -0.9554,
    
        -1.6384, -1.2596, -2.1680, -1.8476, -1.3866,
        -3.0455, -0.5737, -2.5339, -2.1118, -1.6681,
        -2.4675, -2.8842, -0.4329, -3.6266, -1.6925,
        -3.1023, -2.7696, -1.2755, -0.6470, -2.4143,
    
        -2.0107, -2.0912, -1.3053, -0.8557, -3.0683,
        -1.2872, -3.6523, -1.6703, -2.7596, -0.8063,
        -2.4633, -1.2959, -1.6153, -2.3072, -1.0705,
        -3.0543, -0.6473, -1.1650, -2.9025, -2.7710,
    
        -3.5519, -2.0400, -1.8667, -1.4289, -0.8050,
        -1.4602, -0.7452, -1.5754, -3.1624, -3.1247,
        -1.4677, -1.2725, -2.9575, -1.8883, -1.2513,
        -1.2164, -1.5894, -2.2217, -2.3714, -1.2110,
    
        -2.0843, -0.6515, -1.4252, -2.9402, -2.7964,
        -1.5261, -2.5471, -1.7167, -1.9846, -0.9488,
        -1.4847, -1.7093, -1.4095, -1.7293, -1.7675,
        -0.9203, -4.2299, -1.8740, -1.4076, -1.6671,
    
        -1.9052, -0.8330, -2.1839, -2.2459, -1.6193,
        -2.9108, -1.2114, -1.4616, -1.7297, -1.4330,
        -2.2656, -0.7878, -1.8533, -1.8711, -2.0349,
        -2.2457, -2.1395, -1.4509, -0.7538, -2.6381,
    
        -0.8078, -2.1054, -2.6703, -1.1108, -3.3867,
        -1.7774, -1.8426, -1.9473, -1.3293, -1.3273,
        -1.3490, -1.9842, -2.5357, -2.2161, -0.8800,
        -1.5412, -1.8003, -2.7603, -0.8606, -2.0066,
    
        -1.8342, -2.2741, -1.8348, -1.5833, -0.9877,
        -3.5196, -2.3361, -0.9124, -0.9307, -2.5531,
        -1.4862, -1.2153, -1.4453, -3.4462, -1.5625,
        -2.6455, -1.4153, -1.3079, -1.1568, -2.2897};
      std::vector<int64_t> targetsHostData = {
        1, 2, 1, 1, 2, 4, 1,
        2, 2, 2, 2, 2, 2, 3,
        4, 2, 1, 4, 3, 1, 4,
        4, 1, 4, 2, 2, 2, 3};
    
      std::vector<float> negLoglikelihoodOutHostData = {0, 0, 0, 0};
      std::vector<float> logAlphaOutHostData = {
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
      // Create the logProbs aclTensor.
      ret = CreateAclTensor(logProbsHostData, logProbsShape, &logProbsDeviceAddr, aclDataType::ACL_FLOAT, &logProbs);
      CHECK_RET(ret == ACL_SUCCESS, return ret);
      // Create the targets aclTensor.
      ret = CreateAclTensor(targetsHostData, targetsShape, &targetsDeviceAddr, aclDataType::ACL_INT64, &targets);
      CHECK_RET(ret == ACL_SUCCESS, return ret);
    
      std::vector<int64_t> inputLengthsSizeData = {10,10,10,10};
      inputLengths = aclCreateIntArray(inputLengthsSizeData.data(), 4);
      CHECK_RET(inputLengths != nullptr, return ACL_ERROR_BAD_ALLOC);
      std::vector<int64_t> targetLengthsSizeData = {2, 3, 1, 5};
      targetLengths = aclCreateIntArray(targetLengthsSizeData.data(), 4);
      CHECK_RET(targetLengths != nullptr, return ACL_ERROR_BAD_ALLOC);
    
      // Create the negLoglikelihoodOut aclTensor.
      ret = CreateAclTensor(negLoglikelihoodOutHostData, negLoglikelihoodOutShape, &negLoglikelihoodOutDeviceAddr, aclDataType::ACL_FLOAT, &negLoglikelihoodOut);
      CHECK_RET(ret == ACL_SUCCESS, return ret);
      // Create the logAlphaOut aclTensor.
      ret = CreateAclTensor(logAlphaOutHostData, logAlphaOutShape, &logAlphaOutDeviceAddr, aclDataType::ACL_FLOAT, &logAlphaOut);
      CHECK_RET(ret == ACL_SUCCESS, return ret);
    
      // 3. Call the CANN operator library API, which needs to be changed to the specific API.
      uint64_t workspaceSize = 0;
      aclOpExecutor* executor;
      // Call the first-phase API of aclnnCtcLoss.
      ret = aclnnCtcLossGetWorkspaceSize(logProbs, targets, inputLengths, targetLengths, 0, false, negLoglikelihoodOut, logAlphaOut, &workspaceSize, &executor);
      CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnCtcLossGetWorkspaceSize 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 second-phase API of aclnnCtcLoss.
      ret = aclnnCtcLoss(workspaceAddr, workspaceSize, executor, stream);
      CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnCtcLoss failed. ERROR: %d\n", ret); return ret);
      // 4. (boilerplate) Synchronously 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 of negLoglikelihoodOut and copy the result from the device-side memory to the host side, which needs to be modified based on the specific API definition.
      auto size = GetShapeSize(negLoglikelihoodOutShape);
      std::vector<float> resultData(size, 0);
      ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), negLoglikelihoodOutDeviceAddr, size * sizeof(float),
                        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("negLoglikelihoodOut result[%ld] is: %f\n", i, resultData[i]);
      }
    
      // 6. Obtain the output value of logAlphaOut and copy the result from the device-side memory to the host side, which needs to be modified based on the specific API definition.
      auto size1 = GetShapeSize(logAlphaOutShape);
      std::vector<float> resultData1(size1, 0);
      ret = aclrtMemcpy(resultData1.data(), resultData1.size() * sizeof(resultData1[0]), logAlphaOutDeviceAddr, size1 * sizeof(float),
                        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 < size1; i++) {
        LOG_PRINT("logAlphaOut result[%ld] is: %f\n", i, resultData1[i]);
      }
    
      // 7. Release aclTensor and IntArray. Modify based on the specific API definition.
      aclDestroyTensor(logProbs);
      aclDestroyTensor(targets);
      aclDestroyIntArray(inputLengths);
      aclDestroyIntArray(targetLengths);
      aclDestroyTensor(negLoglikelihoodOut);
      aclDestroyTensor(logAlphaOut);
    
      // 8. Release device resources. Modify based on the specific API definition.
      aclrtFree(logProbsDeviceAddr);
      aclrtFree(targetsDeviceAddr);
      aclrtFree(negLoglikelihoodOutDeviceAddr);
      aclrtFree(logAlphaOutDeviceAddr);
      if (workspaceSize > 0) {
        aclrtFree(workspaceAddr);
      }
      aclrtDestroyStream(stream);
      aclrtResetDevice(deviceId);
      aclFinalize();
      return 0;
    }
  • Ascend 950PR&950DT products:

    #include <iostream>
    #include <vector>
    #include "acl/acl.h"
    #include "aclnnop/aclnn_ctc_loss.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: 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 data from the host 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 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. For details, refer to the acl API manual.
      // Fill in deviceId based on the actual device.
      int32_t deviceId = 0;
      aclrtStream stream;
      auto ret = Init(deviceId, &stream);
      // Process the check as required.
      CHECK_RET(ret == 0, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret);
      // 2. Construct the inputs and outputs. Custom construction based on the API is required.
      std::vector<int64_t> logProbsShape = {12, 4, 5};
      std::vector<int64_t> targetsShape = {4, 7};
      std::vector<int64_t> negLoglikelihoodOutShape = {4};
      std::vector<int64_t> logAlphaOutShape = {4, 12, 11};
      void* logProbsDeviceAddr = nullptr;
      void* targetsDeviceAddr = nullptr;
      void* negLoglikelihoodOutDeviceAddr = nullptr;
      void* logAlphaOutDeviceAddr = nullptr;
      aclTensor* logProbs = nullptr;
      aclTensor* targets = nullptr;
      aclIntArray* inputLengths = nullptr;
      aclIntArray* targetLengths = nullptr;
      aclTensor* negLoglikelihoodOut = nullptr;
      aclTensor* logAlphaOut = nullptr;
      std::vector<float> logProbsHostData = {
        -1.0894, -2.7162, -0.9764, -1.9126, -2.6162,
        -2.0684, -2.4871, -2.0866, -1.7205, -0.7187,
        -2.4423, -1.2017, -1.4653, -1.1821, -2.5942,
        -2.4670, -2.7257, -1.4135, -2.1042, -0.7248,
    
        -3.7759, -1.3742, -1.2549, -1.5807, -1.4562,
        -1.3826, -1.8995, -1.8527, -0.9493, -2.8895,
        -1.6316, -2.6603, -2.5014, -0.6992, -1.8609,
        -1.9269, -2.2350, -0.8073, -1.8906, -1.8947,
    
        -0.3468, -2.5855, -2.0723, -2.7147, -3.6668,
        -0.9541, -1.7258, -2.0693, -1.6378, -2.1531,
        -3.5386, -3.4830, -0.2532, -2.0557, -3.3261,
        -1.1480, -1.8080, -0.8244, -3.2414, -3.1909,
    
        -0.8866, -0.7540, -4.4312, -3.4634, -2.6000,
        -1.2785, -1.8347, -3.3122, -0.7620, -2.8349,
        -1.4975, -1.3865, -0.9645, -3.8171, -2.0939,
        -2.3536, -2.0773, -1.4981, -0.8372, -2.0938,
    
        -1.2186, -0.8285, -2.9399, -2.1159, -2.3620,
        -2.3139, -0.6503, -2.7249, -1.2340, -3.7927,
        -0.7143, -2.5084, -3.2826, -2.6651, -1.1334,
        -1.6965, -1.9728, -2.3849, -1.6052, -0.9554,
    
        -1.6384, -1.2596, -2.1680, -1.8476, -1.3866,
        -3.0455, -0.5737, -2.5339, -2.1118, -1.6681,
        -2.4675, -2.8842, -0.4329, -3.6266, -1.6925,
        -3.1023, -2.7696, -1.2755, -0.6470, -2.4143,
    
        -2.0107, -2.0912, -1.3053, -0.8557, -3.0683,
        -1.2872, -3.6523, -1.6703, -2.7596, -0.8063,
        -2.4633, -1.2959, -1.6153, -2.3072, -1.0705,
        -3.0543, -0.6473, -1.1650, -2.9025, -2.7710,
    
        -3.5519, -2.0400, -1.8667, -1.4289, -0.8050,
        -1.4602, -0.7452, -1.5754, -3.1624, -3.1247,
        -1.4677, -1.2725, -2.9575, -1.8883, -1.2513,
        -1.2164, -1.5894, -2.2217, -2.3714, -1.2110,
    
        -2.0843, -0.6515, -1.4252, -2.9402, -2.7964,
        -1.5261, -2.5471, -1.7167, -1.9846, -0.9488,
        -1.4847, -1.7093, -1.4095, -1.7293, -1.7675,
        -0.9203, -4.2299, -1.8740, -1.4076, -1.6671,
    
        -1.9052, -0.8330, -2.1839, -2.2459, -1.6193,
        -2.9108, -1.2114, -1.4616, -1.7297, -1.4330,
        -2.2656, -0.7878, -1.8533, -1.8711, -2.0349,
        -2.2457, -2.1395, -1.4509, -0.7538, -2.6381,
    
        -0.8078, -2.1054, -2.6703, -1.1108, -3.3867,
        -1.7774, -1.8426, -1.9473, -1.3293, -1.3273,
        -1.3490, -1.9842, -2.5357, -2.2161, -0.8800,
        -1.5412, -1.8003, -2.7603, -0.8606, -2.0066,
    
        -1.8342, -2.2741, -1.8348, -1.5833, -0.9877,
        -3.5196, -2.3361, -0.9124, -0.9307, -2.5531,
        -1.4862, -1.2153, -1.4453, -3.4462, -1.5625,
        -2.6455, -1.4153, -1.3079, -1.1568, -2.2897};
      std::vector<int64_t> targetsHostData = {
        1, 2, 1, 1, 2, 4, 1,
        2, 2, 2, 2, 2, 2, 3,
        4, 2, 1, 4, 3, 1, 4,
        4, 1, 4, 2, 2, 2, 3};
    
      std::vector<float> negLoglikelihoodOutHostData = {0, 0, 0, 0};
      std::vector<float> logAlphaOutHostData = {
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
      // Create the logProbs aclTensor.
      ret = CreateAclTensor(logProbsHostData, logProbsShape, &logProbsDeviceAddr, aclDataType::ACL_FLOAT, &logProbs);
      CHECK_RET(ret == ACL_SUCCESS, return ret);
      // Create the targets aclTensor.
      ret = CreateAclTensor(targetsHostData, targetsShape, &targetsDeviceAddr, aclDataType::ACL_INT64, &targets);
      CHECK_RET(ret == ACL_SUCCESS, return ret);
    
      std::vector<int64_t> inputLengthsSizeData = {10,10,10,10};
      inputLengths = aclCreateIntArray(inputLengthsSizeData.data(), 4);
      CHECK_RET(inputLengths != nullptr, return ACL_ERROR_BAD_ALLOC);
      std::vector<int64_t> targetLengthsSizeData = {2, 3, 1, 5};
      targetLengths = aclCreateIntArray(targetLengthsSizeData.data(), 4);
      CHECK_RET(targetLengths != nullptr, return ACL_ERROR_BAD_ALLOC);
    
      // Create the negLoglikelihoodOut aclTensor.
      ret = CreateAclTensor(negLoglikelihoodOutHostData, negLoglikelihoodOutShape, &negLoglikelihoodOutDeviceAddr, aclDataType::ACL_FLOAT, &negLoglikelihoodOut);
      CHECK_RET(ret == ACL_SUCCESS, return ret);
      // Create the logAlphaOut aclTensor.
      ret = CreateAclTensor(logAlphaOutHostData, logAlphaOutShape, &logAlphaOutDeviceAddr, aclDataType::ACL_FLOAT, &logAlphaOut);
      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 aclnnCtcLoss.
      ret = aclnnCtcLossGetWorkspaceSize(logProbs, targets, inputLengths, targetLengths, 0, false, negLoglikelihoodOut, logAlphaOut, &workspaceSize, &executor);
      CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnCtcLossGetWorkspaceSize 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 aclnnCtcLoss.
      ret = aclnnCtcLoss(workspaceAddr, workspaceSize, executor, stream);
      CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnCtcLoss failed. ERROR: %d\n", ret); return ret);
      // 4. (Boilerplate) Synchronize and wait for task execution to finish.
      ret = aclrtSynchronizeStream(stream);
      CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret);
      // 5. Obtain the output negLoglikelihoodOut value and copy the result from device memory to host memory. Modify the code based on the specific API definition.
      auto size = GetShapeSize(negLoglikelihoodOutShape);
      std::vector<float> resultData(size, 0);
      ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), negLoglikelihoodOutDeviceAddr, size * sizeof(float),
                        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("negLoglikelihoodOut result[%ld] is: %f\n", i, resultData[i]);
      }
    
      // 6. Obtain the output logAlphaOut value and copy the result from device memory to host memory. Modify the code based on the specific API definition.
      auto size1 = GetShapeSize(logAlphaOutShape);
      std::vector<float> resultData1(size1, 0);
      ret = aclrtMemcpy(resultData1.data(), resultData1.size() * sizeof(resultData1[0]), logAlphaOutDeviceAddr, size1 * sizeof(float),
                        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 < size1; i++) {
        LOG_PRINT("logAlphaOut result[%ld] is: %f\n", i, resultData1[i]);
      }
    
      // 7. Release aclTensor and IntArray. Modify the code based on the specific API definition.
      aclDestroyTensor(logProbs);
      aclDestroyTensor(targets);
      aclDestroyIntArray(inputLengths);
      aclDestroyIntArray(targetLengths);
      aclDestroyTensor(negLoglikelihoodOut);
      aclDestroyTensor(logAlphaOut);
    
      // 8. Release device resources. Modify the code based on the specific API definition.
      aclrtFree(logProbsDeviceAddr);
      aclrtFree(targetsDeviceAddr);
      aclrtFree(negLoglikelihoodOutDeviceAddr);
      aclrtFree(logAlphaOutDeviceAddr);
      if (workspaceSize > 0) {
        aclrtFree(workspaceAddr);
      }
      aclrtDestroyStream(stream);
      aclrtResetDevice(deviceId);
      aclFinalize();
      return 0;
    }