aclnnUpsampleTrilinear3d
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: Apply trilinear interpolation to upsample an input signal composed of multiple input channels.
- Formula:
- Core algorithm logic:
- Map each point of the target image back to the original image to obtain a coordinate with a fractional part.
- Based on this floating-point coordinate, compute the adjacent original image points before and after.
- 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. For a 3D interpolation point , the following formula applies:
Therefore, for a point p(x,y,z) in a certain direction of the output, the point mapped back to the original image is denoted as q(x',y',z'), and the relationship is as follows:
Notation:
Then the following formula applies:
- Core algorithm logic:
Function Prototype
Each operator uses a two-phase API. Call the "aclnnUpsampleTrilinear3dGetWorkspaceSize" API first to obtain the workspace size required for computation and the executor containing the operator computation process. Then, call the "aclnnUpsampleTrilinear3d" API to perform the computation.
aclnnStatus aclnnUpsampleTrilinear3dGetWorkspaceSize(
const aclTensor *self,
const aclIntArray *outputSize,
bool alignCorners,
double scalesD,
double scalesH,
double scalesW,
aclTensor *out,
uint64_t *workspaceSize,
aclOpExecutor **executor)aclnnStatus aclnnUpsampleTrilinear3d(
void *workspace,
uint64_t workspaceSize,
aclOpExecutor *executor,
aclrtStream stream)aclnnUpsampleTrilinear3dGetWorkspaceSize
Parameters
Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor self (aclTensor*) Input Indicates the input tensor to be upsampled, corresponding to `self` in the corresponding formula. - Empty tensors are not supported.
- If the data format is ND, the tensor is processed in NCDHW format by default.
- The values of the C, D, H, and W dimensions of the shape must be greater than 0.
FLOAT32, FLOAT16, BFLOAT16, DOUBLE NCDHW, NDHWC, ND 5 √ outputSize (aclIntArray*) Input Indicates the spatial sizes of the output parameter `out` in the D, H, and W dimensions, corresponding to `outputSize` in the corresponding formula. The value of size is 3, and all elements are greater than 0. INT64 - - - alignCorners (bool) Input Indicates whether corner pixels are aligned, corresponding to `alignCorners` in the formula. If true, the corner pixels of the input and output tensors are aligned. Otherwise, they are not aligned. - - - - scalesD (double) Input Indicates the depth dimension multiplier of output `out`, corresponding to `scales_d` in the formula. Value less than or equal to 50. - - - - scalesH (double) Input Indicates the height dimension multiplier of output `out`, corresponding to `scales_h` in the formula. Value less than or equal to 50. - - - - scalesW (double) Input Indicates the dimension multiplier of the width of output `out`, corresponding to `scales_w` in the formula. Value less than or equal to 50. - - - - out (aclTensor*) Output Indicates the output tensor after sampling. - Empty tensors are not supported.
- The data type and data format must remain consistent with input parameter `self`.
- The N and C dimensions of shape must remain consistent with input parameter `self`.
FLOAT32, FLOAT16, BFLOAT16, DOUBLE NCDHW, NDHWC, ND 5 √ workspaceSize (uint64_t*) Output Returns the workspace size to be allocated on the Device side. - - - - - executor (aclOpExecutor**) Output Returns the op executor, which contains the operator computation process. - - - - - Atlas inference products :- The data type of the input parameter
selfsupports only FLOAT32 and FLOAT16. inf and -inf inputs are not supported. - The data type of the output parameter
outsupports only FLOAT32 and FLOAT16.
- The data type of the input parameter
Atlas training products :The data types of input parameter
selfand output parameteroutsupport FLOAT32, FLOAT16, and DOUBLE.
Ascend 950PR&950DT products :The data types of input parameter
selfand output parameteroutdo not support DOUBLE.
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 self, outputSize, or out is a null pointer. ACLNN_ERR_PARAM_INVALID 161002 The data type of self is not within the supported range, or the data type of self is different from that of out. The shape of self is not 5-dimensional. The size of outputSize is not 3. The size of self in the C, D, H, or W dimension is not greater than 0. The value of an element in outputSize is not greater than 0.
aclnnUpsampleTrilinear3d
Parameters
Parameter Input/Output Description workspace Input Workspace memory address allocated on the Device side. workspaceSize Input Workspace size allocated on the Device side, obtained by the first-phase API aclnnUpsampleTrilinear3dGetWorkspaceSize. executor Input op executor containing the operator computation process. stream Input Specifies the Stream for executing tasks. Return Value
aclnnStatus: return status code. For details, see aclnn Error Codes.
Constraints
In the input data scaling use case, the downscaling factor must be less than or equal to 50, that is:
Shape constraints for parameters
selfandout:The value of each dimension is less than or equal to 2^20.
The N and C axes of parameter
outare the same as those ofself.The memory usage must be less than 60 GB. The formula for memory usage is as follows:
Where:
- N represents the N axis of the input and output.
- C represents the C axis of the input and output.
- dtype represents the data type of the input tensor.
N * C * self_D * self_H < 2^31
out_W * out_H < 2^31
When the values of scalesD, scalesH, and scalesW are all greater than 0, parameters self, outputSize, scalesD, scalesH, and scalesW must satisfy the following constraints:
Deterministic computation:
- aclnnUpsampleTrilinear3d defaults to a deterministic implementation.
Example
The sample code is as follows and is for reference only. For details about the compilation and execution process, see Compile and Run Samples.
#include <iostream>
#include <vector>
#include "acl/acl.h"
#include "aclnnop/aclnn_upsample_trilinear_3d.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 memory on the device side.
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 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_NCDHW,
shape.data(), shape.size(), *deviceAddr);
return 0;
}
int main()
{
// 1. (Boilerplate) Initialize device/stream. For details, see the acl API manual.
// Fill in deviceId based on the actual device.
int32_t deviceId = 0;
aclrtStream stream;
auto ret = Init(deviceId, &stream);
// Handle the check as required.
CHECK_RET(ret == 0, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret);
// 2. Construct inputs and outputs. Customize the construction based on the API.
std::vector<int64_t> selfShape = {1, 1, 2, 2, 2};
std::vector<int64_t> outShape = {1, 1, 4, 4, 4};
void* selfDeviceAddr = nullptr;
void* outDeviceAddr = nullptr;
aclTensor* self = nullptr;
aclTensor* out = nullptr;
std::vector<float> selfHostData = {1, 2, 3, 4, 5, 6, 7, 8};
const size_t kSize64 = 64U;
std::vector<float> outHostData(kSize64, 0);
std::vector<int64_t> outputSizeData = {4, 4, 4};
bool alignCorners = false;
double scalesD = 0.0;
double scalesH = 0.0;
double scalesW = 0.0;
// Create self aclTensor.
ret = CreateAclTensor(selfHostData, selfShape, &selfDeviceAddr, aclDataType::ACL_FLOAT, &self);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create out aclTensor.
ret = CreateAclTensor(outHostData, outShape, &outDeviceAddr, aclDataType::ACL_FLOAT, &out);
CHECK_RET(ret == ACL_SUCCESS, return ret);
const aclIntArray* outputSize = aclCreateIntArray(outputSizeData.data(), outputSizeData.size());
CHECK_RET(outputSize != nullptr, return ACL_ERROR_INTERNAL_ERROR);
// 3. Call the CANN operator library API. Change it to the specific API.
uint64_t workspaceSize = 0;
aclOpExecutor* executor;
// Call the first-phase API of aclnnUpsampleTrilinear3d.
ret = aclnnUpsampleTrilinear3dGetWorkspaceSize(self, outputSize, alignCorners, scalesD, scalesH, scalesW, out,
&workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnUpsampleTrilinear3dGetWorkspaceSize 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 aclnnUpsampleTrilinear3d.
ret = aclnnUpsampleTrilinear3d(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnUpsampleTrilinear3d failed. ERROR: %d\n", ret); return ret);
// 4. (Boilerplate) Synchronize and wait for the 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 values and copy the result from device memory to host memory. Modify the code based on 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(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("result[%ld] is: %f\n", i, resultData[i]);
}
// 6. Release aclTensor and aclScalar. Modify the code based on the specific API definition.
aclDestroyTensor(self);
aclDestroyTensor(out);
// 7. Release device resources. Modify the code based on the specific API definition.
aclrtFree(selfDeviceAddr);
aclrtFree(outDeviceAddr);
if (workspaceSize > 0) {
aclrtFree(workspaceAddr);
}
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
return 0;
}