aclnnUpsampleNearestExact3d
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 : Not supported
Function
- Description: Apply nearest-neighbor interpolation to upsample an input signal composed of multiple input channels. If the input shape is (N, C, D, H, W), the output shape is (N, C, outputSize[0], outputSize[1], outputSize[2]).
- Formula:
Function Prototype
Each operator uses a two-phase API. First, call the "aclnnUpsampleNearestExact3dGetWorkspaceSize" API to obtain the input parameters and calculate the required workspace size based on the computation process. Then, call the "aclnnUpsampleNearestExact3d" API to perform the computation.
aclnnStatus aclnnUpsampleNearestExact3dGetWorkspaceSize(
const aclTensor *self,
const aclIntArray *outputSize,
double scalesD,
double scalesH,
double scalesW,
aclTensor *out,
uint64_t *workspaceSize,
aclOpExecutor **executor)aclnnStatus aclnnUpsampleNearestExact3d(
void *workspace,
uint64_t workspaceSize,
aclOpExecutor *executor,
aclrtStream stream)aclnnUpsampleNearestExact3dGetWorkspaceSize
Parameters
Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor self(aclTensor*) Input Indicates the input tensor for upsampling, corresponding to `self` in the formula. - Empty tensors are not supported.
- When the data format is ND, the tensor is processed as NCDHW by default.
- All dimension values of self are less than or equal to (2^31-1).
FLOAT32, FLOAT16, BFLOAT16 NCDHW, NDHWC, ND 5 √ outputSize (aclIntArray*) Input Indicates the spatial size of output out along the D, H, and W dimensions, corresponding to `outputSize` in the formula. size is 3, and each element is greater than zero. INT64 - - - scalesD (double) Input Indicates the specified depth dimension multiplier of output `out`, corresponding to `scalesD` in the formula. Takes effect only when the value is positive. - - - - scalesH (double) Input Indicates the height dimension multiplier of the specified output `out`, corresponding to `scalesH` in the formula. Takes effect only when the value is positive. - - - - scalesW (double) Input Indicates the specified width dimension multiplier of output `out`, corresponding to `scalesW` in the formula. Takes effect only when the value is positive. - - - - out (aclTensor*) Output Indicates the output tensor after sampling, corresponding to output `out` in the formula. - Empty tensors are not supported.
- The data type and data format must be the same as those of input `self`.
- All dimension values of out are less than or equal to (2^31-1).
- The N and C dimensions of shape are the same as those of input `self`.
FLOAT32, FLOAT16, BFLOAT16 NCDHW, NDHWC, ND 5 √ workspaceSize (uint64_t*) Output Returns the size of the workspace to be applied on the side of Device. - - - - - executor (aclOpExecutor**) Output Returns the op executor containing the computation process of the operator. - - - - - Atlas inference products :The data types of parameters
selfandoutonly support FLOAT32 and FLOAT16.
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 in the supported range, or the data types of self and out are different. 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. The shape of out is not equal to the shape derived from self and outputSize.
aclnnUpsampleNearestExact3d
Parameters
Parameter Input/Output Description workspace Input Workspace memory address applied on the side of Device. workspaceSize Input Workspace size applied on the side of Device, obtained by the first-phase API aclnnUpsampleNearestExact3dGetWorkspaceSize. executor Input Op executor that contains the operator computation process. stream Input Stream for task execution. Return Value
aclnnStatus: return status code. For details, see aclnn Error Codes.
Constraints
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:
- aclnnUpsampleNearestExact3d 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_nearest_exact3d.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 memory on the device side.
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_NCDHW,
shape.data(), shape.size(), *deviceAddr);
return 0;
}
int main()
{
// 1. (Boilerplate) Device/stream initialization. For details, see the acl API manual.
// Set 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. Define the construction based on the API.
std::vector<int64_t> selfShape = {2, 2, 2, 2, 3};
std::vector<int64_t> outShape = {2, 2, 1, 1, 1};
void* selfDeviceAddr = nullptr;
void* outDeviceAddr = nullptr;
aclTensor* self = nullptr;
aclTensor* out = nullptr;
std::vector<float> selfHostData = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,
33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48};
std::vector<float> outHostData = {1, 2, 3, 4};
std::vector<int64_t> outputSizeData = {1, 1, 1};
double scalesD = 0.0;
double scalesH = 0.0;
double scalesW = 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);
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 aclnnUpsampleNearestExact3d.
ret = aclnnUpsampleNearestExact3dGetWorkspaceSize(self, outputSize, scalesD, scalesH, scalesW, out, &workspaceSize,
&executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnUpsampleNearestExact3dGetWorkspaceSize failed. ERROR: %d\n", ret);
return ret);
// Allocate device memory using 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 aclnnUpsampleNearestExact3d.
ret = aclnnUpsampleNearestExact3d(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnUpsampleNearestExact3d 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 value and copy the result from device memory to host memory. Modify the code based on the 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 the aclTensor and aclScalar. Modify the code based on the API definition.
aclDestroyTensor(self);
aclDestroyTensor(out);
// 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;
}