aclnnUpsampleLinear1d

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

Function

  • Description: Apply linear interpolation to upsample an input signal composed of multiple input channels. If the input shape is (N, C, L), the output shape is (N, C, outputSize).

  • Formula:

    • Core algorithm logic:
      1. Map each point of the target image back to the original image to obtain a coordinate with a fractional part.
      2. Based on this floating-point coordinate, compute the adjacent original image points before and after.
      3. Compute the weights of the adjacent points to the corresponding target point respectively, and multiply and accumulate them according to the weights to obtain the target point value.
    • Specific computation logic: The scaling method is divided into corner alignment and edge alignment. Corner alignment (alignCorners = true) means aligning according to the pixel center point at the top-left corner of the original image; edge alignment (alignCorners = false) means aligning according to the top-left vertex and two edges of the original image. There are differences in computing the scaling coefficient and coordinate position. Then the following formula applies:scale={(self.dim[2]−1)/(outputSize[0]−1)alignCorners=true1/scalesalignCorners=false&scales>0self.dim[2]/outputSize[0]alignCorners=falsescale =\begin{cases} (self.dim[2]-1) / (outputSize[0]-1) & alignCorners=true \\ 1 / scales & alignCorners=false\&scales>0\\ self.dim[2] / outputSize[0] & alignCorners=false \end{cases} Therefore, for a point p(x) in a certain direction of output, the point mapped back to the original image is denoted as q(x'), and the relationship is:x′={x∗scalealignCorners=trueMAX(0,(x+0.5)∗scale−0.5)alignCorners=falsex' =\begin{cases} x * scale & alignCorners=true \\ MAX(0,{(x+0.5)*scale-0.5}) & alignCorners=false \end{cases}
      • Notation:

        x0=int(x′),x1=int(x′)+1,lambda0=x1−x′,lambda1=1−lambda0x_{0} =int(x'),x_{1} =int(x')+1, lambda_{0} = x_{1}-x', lambda_{1} = 1-lambda_{0}
      • Then the following formula applies:

        V(px)=V(px0)∗lambda0+V(px1)∗lambda1{V(p_{x})} = {V(p_{x0})} * {lambda_{0}} + {V(p_{x1})} * {lambda_{1}}

Function Prototype

Each operator uses a two-phase API. You must first call the "aclnnUpsampleLinear1dGetWorkspaceSize" API to obtain the input parameters and calculate the required workspace size based on the computation flow, and then call the "aclnnUpsampleLinear1d" API to perform the computation.

aclnnStatus aclnnUpsampleLinear1dGetWorkspaceSize(
  const aclTensor   *self,
  const aclIntArray *outputSize,
  const bool         alignCorners,
  const double       scales,
  aclTensor         *out,
  uint64_t          *workspaceSize,
  aclOpExecutor    **executor)
aclnnStatus aclnnUpsampleLinear1d(
  void          *workspace,
  uint64_t       workspaceSize,
  aclOpExecutor *executor,
  aclrtStream    stream)

aclnnUpsampleLinear1dGetWorkspaceSize

  • Parameters

    Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor
    self (aclTensor*) Input Input tensor for upsampling, corresponding to `self` in the formula.
    • Empty tensors are not supported.
    • When the data format is ND, it is processed as NCL by default.
    FLOAT32, FLOAT16, BFLOAT16 ND, NCL 3 √
    outputSize (aclIntArray*) Input Spatial size of output out along the L dimension, corresponding to `outputSize` in the formula. The size is 1 and the value is greater than 0. INT64 - - -
    alignCorners (bool) Input A bool parameter that determines whether to align corner pixels. It corresponds to `alignCorners` in the formula.
    • If set to true, the input and output tensors are aligned by the center of their corner pixels, and the values at the corner pixels are preserved.
    • If set to false, the input and output tensors are aligned by the corner points of their corner pixels, and the interpolation uses edge value padding for out-of-boundary values, making this operation independent of the input size when scales remains unchanged.
    - - - -
    scales (double) Input Multiplier of the L dimension of output out, corresponding to `scales` in the formula. - - - - -
    out (aclTensor*) Output Represents the output tensor after sampling.
    • Empty tensors are not supported.
    • The output must be 3-dimensional. The data type and data format must be the same as those of the input parameter `self`.
    FLOAT32, FLOAT16, BFLOAT16 ND, NCL 3 √
    workspaceSize (uint64_t*) Output Returns the workspace size to be requested on the device. - - - - -
    executor (aclOpExecutor**) Output Returns the op executor, which contains the computation process of the operator. - - - - -
    • Atlas training products:

      The data types of input parameter self and output parameter out do not support BFLOAT16.

  • 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 A required input, output, or attribute parameter is a null pointer.
    ACLNN_ERR_PARAM_INVALID 161002
    The data type of self is not within the supported range.
    The data types of self and out are inconsistent.
    self and out are not 3-dimensional.
    The size of outputSize is not 1.
    The value of an element in outputSize is less than 1.
    The size of out on the L dimension is inconsistent with outputSize[0].
    self, outputSize, or scales does not satisfy the constraints.

aclnnUpsampleLinear1d

  • Parameters

    Parameter Input/Output Description
    workspace Input The workspace memory address allocated on the device.
    workspaceSize Input The workspace size allocated on the device, obtained by the first-phase API aclnnUpsampleLinear1dGetWorkspaceSize.
    executor Input Op executor that contains the operator computation flow.
    stream Input Stream for task execution.
  • Return Value

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

Constraints

  • When the data format of input parameter self and output parameter out is neither ND nor NCL, other input data formats are processed as NCL by default.

  • Parameters self, outputSize, and scales must satisfy the following constraints:

    outputSize=floor(self_L∗scales)outputSize = floor(self\_L * scales)
  • Deterministic computation:

    • aclnnUpsampleLinear1d 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 "aclnnop/aclnn_upsample_linear_1d.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 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 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) Initialize the device/stream. For details, see the acl API documentation.
    // Set deviceId according to 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 the input and output. Customize the construction based on the API.
    std::vector<int64_t> selfShape = {1, 1, 2};
    std::vector<int64_t> outShape = {1, 1, 3};
    void* selfDeviceAddr = nullptr;
    void* outDeviceAddr = nullptr;
    aclTensor* self = nullptr;
    aclTensor* out = nullptr;
    std::vector<float> selfHostData = {1, 1};
    std::vector<float> outHostData = {0, 0, 0};

    // Create the self aclTensor.
    ret = CreateAclTensor(selfHostData, selfShape, &selfDeviceAddr, aclDataType::ACL_FLOAT, &self);
    CHECK_RET(ret == ACL_SUCCESS, return ret);

    // Create the out aclTensor.
    ret = CreateAclTensor(outHostData, outShape, &outDeviceAddr, aclDataType::ACL_FLOAT, &out);
    CHECK_RET(ret == ACL_SUCCESS, return ret);

    std::vector<int64_t> outArraySize = {3};
    const aclIntArray* outputSize = aclCreateIntArray(outArraySize.data(), outArraySize.size());
    CHECK_RET(outputSize != nullptr, return ACL_ERROR_INTERNAL_ERROR);

    // 3. Call the CANN operator library API. Replace it with the specific API name
    uint64_t workspaceSize = 0;
    aclOpExecutor* executor;
    // Call the first-phase API of aclnnUpsampleLinear1d
    ret = aclnnUpsampleLinear1dGetWorkspaceSize(self, outputSize, false, -1, out, &workspaceSize, &executor);
    CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnUpsampleLinear1dGetWorkspaceSize 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 aclnnUpsampleLinear1d
    ret = aclnnUpsampleLinear1d(workspaceAddr, workspaceSize, executor, stream);
    CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnUpsampleLinear1d 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 values and copy the result on the device to the host. Modify the code according to the specific API definition
    auto size = GetShapeSize(outShape);
    std::vector<float> resultData(size, 0);
    ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), outDeviceAddr,
                      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 aclTensor and aclScalar. Modify the code according to the specific API definition
    aclDestroyTensor(self);
    aclDestroyTensor(out);
    aclDestroyIntArray(outputSize);

    // 7. Release device resources. Modify the code according to the specific API definition
    aclrtFree(selfDeviceAddr);
    aclrtFree(outDeviceAddr);
    if (workspaceSize > 0) {
        aclrtFree(workspaceAddr);
    }
    aclrtDestroyStream(stream);
    aclrtResetDevice(deviceId);
    aclFinalize();
    return 0;
}