aclnnConvTbc
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 : Not supported
Function
- Description: Implements one-dimensional convolution with input and output dimensions of T (time or spatial dimension), B (batch), and C (channel).
- Formula: Assume that the input (self) shape is () and the output (out) shape is (). The output is expressed as: Where represents the batch size, represents the number of channels, and represents the time or spatial dimension.
Function Prototype
Each operator uses a two-phase API. First call the aclnnConvTbcGetWorkspaceSize API to obtain the workspace size required for the computation and the executor that contains the computation process of the operator. Then call the aclnnConvTbc API to execute the computation.
aclnnStatus aclnnConvTbcGetWorkspaceSize(
const aclTensor *self,
const aclTensor *weight,
const aclTensor *bias,
const int64_t pad,
aclTensor *output,
int8_t cubeMathType,
uint64_t *workspaceSize,
aclOpExecutor **executor)aclnnStatus aclnnConvTbc(
void *workspace,
const uint64_t workspaceSize,
aclOpExecutor *executor,
aclrtStream stream)aclnnConvTbcGetWorkspaceSize
Parameters
Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor self (aclTensor*) Input self in the formula. Denotes the convolution input. - Supports empty tensors.
- The data type must comply with the data type deduction rules with weight (see deduction relationship).
- The shape is (Hin,N,Cin).
- H≥0, N≥0, Cin≥1.
FLOAT, FLOAT16, BFLOAT16, HIFLOAT8 ND, NCL 3 √ weight (aclTensor*) Input weight in the formula. Denotes the convolution weight. - Supports empty tensors.
- The data type must satisfy the data type deduction rules with the data type of self (see deduction relationship).
- The shape is (K, Cin, Cout).
- All dimensions are greater than or equal to 1.
FLOAT, FLOAT16, BFLOAT16, HIFLOAT8 ND, NCL 3 √ bias (aclTensor*) Input The bias in the formula, which denotes the convolution bias. - The data type must be the same as those of self and weight.
- Must be one-dimensional and equal to the first dimension of weight. A null pointer is not allowed.
FLOAT, FLOAT16, BFLOAT16 ND 1 √ pad (int64_t) Input Denotes the number of paddings on the left and right of the H dimension. The size must be within the range of [0,255]. INT64 - - - output (aclTensor*) Output out in the formula, denotes the convolution output. - Supports empty tensor.
- The data type is the same as self.
- The shape is (Hout,N,Cout).
- The number of channels equals the first dimension of weight, and other dimensions are ≥0.
FLOAT, FLOAT16, BFLOAT16, HIFLOAT8 ND, NCL 3 √ cubeMathType (int8_t) Input Determines the computation logic used by the Cube unit for computation. - 0 (KEEP_DTYPE): Keeps the input data type for computation.
- 1 (ALLOW_FP32_DOWN_PRECISION): Allows FLOAT down-precision computation to improve performance.
- 2 (USE_FP16): Performs computation in FLOAT16 precision.
- 3 (USE_HF32): Performs computation in HFLOAT32 (mixed precision).
INT8 - - - workspaceSize (uint64_t*) Output Returns the workspace size to be applied on the side of Device. - - - - - executor (aclOpExecutor**) Output Returns the op executor, which contains the operator computation flow. - - - - - 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 The input pointer parameter is a null pointer. ACLNN_ERR_PARAM_INVALID 161002 The data types and formats of self, weight, bias, and output are not within the range of supported values. The data types of self, weight, and output are inconsistent. The shape of output does not match the infershape result. The shape of output contains a value less than 0. The dim of self is not 3. The dim of weight is not 3. The dim of bias is not 1. The second dimension of self is not equal to the second dimension of weight. The value of bias is not equal to the first dimension of weight. ACLNN_ERR_INNER_NULLPTR 561103 API internal verification error, usually caused by input data or attributes whose specifications are out of the supported range. ACLNN_ERR_RUNTIME_ERROR 361001 The API fails to call the NPU runtime API, for example, when the SocVersion is not supported.
aclnnConvTbc
Parameters
Parameter Input/Output Description workspace Input The workspace memory address applied on the Device side. workspaceSize Input The size of the workspace applied on the Device side, obtained by calling the first-phase API aclnnConvTbcGetWorkspaceSize. executor Input Operator executor that contains the operator computation flow. stream Input Stream for executing tasks. Return Value
aclnnStatus: Return status code. For details, see aclnn return codes.
Constraints
- Deterministic computation
- aclnnConvTbc defaults to a deterministic implementation.
| Constraint Type | ||
|---|---|---|
| self and weight |
|
|
| bias | The data format is not strictly validated. ND, NCL, and other formats can be passed in, but the shape and dimension sizes must still meet the constraints. | When the data type of self is HIFLOAT8, the data type of bias is finally converted to FLOAT for computation. |
| output | output supports N, C, and L dimensions greater than or equal to 0 (the scenario where the value is 0 is supported only when the N, C, or L dimension of self is 0). |
|
| cubeMathType |
|
|
Example
The following sample code is for reference only. For details about the compilation and execution process, see Compile and Run Samples.
#include <iostream>
#include <memory>
#include <vector>
#include "acl/acl.h"
#include "aclnnop/aclnn_convolution.h"
#define CHECK_RET(cond, return_expr) \
do { \
if (!(cond)) { \
return_expr; \
} \
} while (0)
#define CHECK_FREE_RET(cond, return_expr) \
do { \
if (!(cond)) { \
Finalize(deviceId, stream); \
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, initialize resources.
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, aclFormat dataFormat) {
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);
// 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, dataFormat,
shape.data(), shape.size(), *deviceAddr);
return 0;
}
void Finalize(int32_t deviceId, aclrtStream& stream)
{
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
}
int aclnnConvTbcTest(int32_t deviceId, aclrtStream& stream)
{
auto ret = Init(deviceId, &stream);
// Handle the check as needed.
CHECK_FREE_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 interface.
std::vector<int64_t> shapeInput = {2, 2, 3};
std::vector<int64_t> shapeWeight = {3, 3, 4};
std::vector<int64_t> shapeBias = {4};
std::vector<int64_t> shapeResult = {2, 2, 4};
void* deviceDataA = nullptr;
void* deviceDataB = nullptr;
void* deviceDataC = nullptr;
void* deviceDataResult = nullptr;
aclTensor* input = nullptr;
aclTensor* weight = nullptr;
aclTensor* bias = nullptr;
aclTensor* result = nullptr;
std::vector<float> inputData(GetShapeSize(shapeInput), 1);
std::vector<float> weightData(GetShapeSize(shapeWeight), 1);
std::vector<float> biasData(GetShapeSize(shapeBias), 1);
std::vector<float> outputData(GetShapeSize(shapeResult), 1);
// Create the input aclTensor.
ret = CreateAclTensor(inputData, shapeInput, &deviceDataA, aclDataType::ACL_FLOAT, &input, aclFormat::ACL_FORMAT_NCL);
std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor *)> inputTensorPtr(input, aclDestroyTensor);
std::unique_ptr<void, aclError (*)(void *)> deviceDataAPtr(deviceDataA, aclrtFree);
CHECK_FREE_RET(ret == ACL_SUCCESS, return ret);
// Create the weight aclTensor.
ret = CreateAclTensor(weightData, shapeWeight, &deviceDataB, aclDataType::ACL_FLOAT, &weight, aclFormat::ACL_FORMAT_NCL);
std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor *)> weightTensorPtr(weight, aclDestroyTensor);
std::unique_ptr<void, aclError (*)(void *)> deviceDataBPtr(deviceDataB, aclrtFree);
CHECK_FREE_RET(ret == ACL_SUCCESS, return ret);
// Create the bias aclTensor.
ret = CreateAclTensor(biasData, shapeBias, &deviceDataC, aclDataType::ACL_FLOAT, &bias, aclFormat::ACL_FORMAT_ND);
std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor *)> biasTensorPtr(bias, aclDestroyTensor);
std::unique_ptr<void, aclError (*)(void *)> deviceDataCPtr(deviceDataC, aclrtFree);
CHECK_FREE_RET(ret == ACL_SUCCESS, return ret);
// Create the result aclTensor.
ret = CreateAclTensor(outputData, shapeResult, &deviceDataResult, aclDataType::ACL_FLOAT, &result, aclFormat::ACL_FORMAT_NCL);
std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor *)> outputTensorPtr(result, aclDestroyTensor);
std::unique_ptr<void, aclError (*)(void *)> deviceDataResultPtr(deviceDataResult, aclrtFree);
CHECK_FREE_RET(ret == ACL_SUCCESS, return ret);
// 3. Call the CANN operator library API. Replace it with the actual API name.
uint64_t workspaceSize = 0;
aclOpExecutor* executor;
// Call the first-phase API of aclnnConvTbc.
ret = aclnnConvTbcGetWorkspaceSize(input, weight, bias, 1, result, 1, &workspaceSize, &executor);
CHECK_FREE_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnConvTbcGetWorkspaceSize failed. ERROR: %d\n", ret); return ret);
// Allocate device memory based on the workspaceSize calculated by the first-phase API.
void* workspaceAddr = nullptr;
std::unique_ptr<void, aclError (*)(void *)> workspaceAddrPtr(nullptr, aclrtFree);
if (workspaceSize > 0) {
ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST);
CHECK_FREE_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret);
workspaceAddrPtr.reset(workspaceAddr);
}
// Call the second-phase API of aclnnConvTbc.
ret = aclnnConvTbc(workspaceAddr, workspaceSize, executor, stream);
CHECK_FREE_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnConvTbc failed. ERROR: %d\n", ret); return ret);
// 4. (boilerplate) Synchronize and wait for the task execution to complete.
ret = aclrtSynchronizeStream(stream);
CHECK_FREE_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret);
// 5. Obtain the output values, copy the result from device memory to host memory, and modify as required by the specific API definition.
auto size = GetShapeSize(shapeResult);
std::vector<float> resultData(size, 0);
ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), deviceDataResult,
size * sizeof(float), ACL_MEMCPY_DEVICE_TO_HOST);
CHECK_FREE_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]);
}
return ACL_SUCCESS;
}
int main() {
// 1. (Boilerplate) Initialize device/stream. For details, see the acl API manual.
// Set deviceId based on the actual device.
int32_t deviceId = 0;
aclrtStream stream;
auto ret = aclnnConvTbcTest(deviceId, stream);
CHECK_FREE_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnConvTbcTest failed. ERROR: %d\n", ret); return ret);
Finalize(deviceId, stream);
return 0;
}