aclnnNpuFormatCast
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:
Ascend 950PR&950DT products :- Converts the ND data format to the FRACTAL_NZ data format with the specified C0 size. C0 is the size of the last dimension of the FRACTAL_NZ data format and is determined by
additionalDtype. - Converts a tensor in FRACTAL_NZ data format with a specified C0 size to a tensor in ND data format. The supported NZ formats include: FRACTAL_NZ, FRACTAL_NZ_C0_2, FRACTAL_NZ_C0_4, FRACTAL_NZ_C0_16, and FRACTAL_NZ_C0_32.
- When the data type of srcTensor is FLOAT8_E8M0, the format of srcTensor is ND/NCL/NCHW, and dstFormat is FRACTAL_NZ, conversion of the MX scale to the Ascend-affine storage shape is supported.
- Converts the ND data format to the FRACTAL_NZ data format with the specified C0 size. C0 is the size of the last dimension of the FRACTAL_NZ data format and is determined by
Atlas A3 products andAtlas A2 products :- Converts between ND and NZ, between NCL and NZ, between NCHW and NZ, and between NCDHW and NZ. C0 is the size of the last dimension of the NZ data format. Calculation method: C0 = 32B /
ge::GetSizeByDataType(static_cast<op::DataType>(additionalDtype)). - Converts between NCDHW and NDC1HWC0, between NCDHW and FRACTAL_Z_3D, between NCHW and NC1HWC0, between NHWC and NC1HWC0, between NCHW and FRACTAL_Z, between HWCN and FRACTAL_Z, between NDHWC and NDC1HWC0, and between DHWCN and FRACTAL_Z_3D. C0 is strongly related to the microarchitecture. Its value equals the size of the cube unit, for example, 16. C1 is obtained by splitting dimension C by C0: C1=C/C0. If the result is not an integer, the last portion of data must be padded to C0. Calculation method: C0 = 32B /
ge::GetSizeByDataType(static_cast<op::DataType>(additionalDtype))(for example, the enumeration value of additionalDtype for FP16 is 1, and the corresponding FP16 data is 2 bytes).
- Converts between ND and NZ, between NCL and NZ, between NCHW and NZ, and between NCDHW and NZ. C0 is the size of the last dimension of the NZ data format. Calculation method: C0 = 32B /
Calculation Process:
aclnnNpuFormatCastCalculateSizeAndFormatcalculates the shape and actual data format of the destination tensor dstTensor after conversion based on the source tensor srcTensor, the data typeadditionalDtype, and the data format dstFormat of the destination tensor. This information is used to construct dstTensor. Then, callaclnnNpuFormatCastto convert srcTensor into the destination tensor dstTensor with the actual data format.
Function Prototype
Calculate the shape and actual data format of dstTensor by calling aclnnNpuFormatCastCalculateSizeAndFormat first, and then call the two-phase API. The two-phase API first calls the aclnnNpuFormatCastGetWorkspaceSize API to obtain the workspace size required for computation and the executor that contains the operator computation process, and then calls the aclnnNpuFormatCast API to perform computation.
aclnnStatus aclnnNpuFormatCastCalculateSizeAndFormat(
const aclTensor* srcTensor,
const int dstFormat,
int additionalDtype,
int64_t** dstShape,
uint64_t* dstShapeSize,
int* actualFormat)aclnnStatus aclnnNpuFormatCastGetWorkspaceSize(
const aclTensor* srcTensor,
aclTensor* dstTensor,
uint64_t* workspaceSize,
aclOpExecutor** executor)aclnnStatus aclnnNpuFormatCast(
void* workspace,
uint64_t workspaceSize,
aclOpExecutor* executor,
aclrtStream stream)aclnnNpuFormatCastCalculateSizeAndFormat
Parameters
Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor srcTensor (aclTensor*) Input Source tensor to be converted. - INT8 (2), UINT8 (4), INT32 (3), UINT32 (8), FLOAT (0), FLOAT16 (1), BFLOAT16 (27), FLOAT8_E4M3FN2 (36), FLOAT4_E2M12 (40), FLOAT4_E1M22 (41), HIFLOAT82 (34) ACL_FORMAT_ND (2), ACL_FORMAT_NZ (29), ACL_FORMAT_NCL (47), ACL_FORMAT_NCHW1 (0), ACL_FORMAT_NHWC1 (1), ACL_FORMAT_HWCN1 (16), ACL_FORMAT_NCDHW1 (30), ACL_FORMAT_NDHWC1 (27), ACL_FORMAT_DHWCN1 (31), ACL_FORMAT_NDC1HWC01 (32), ACL_FORMAT_FRACTAL_Z_3D1 (33), ACL_FORMAT_NC1HWC01 (3), ACL_FORMAT_FRACTAL_Z1 (4). 2-6 - dstFormat (int) Input Enumeration value corresponding to the data format of the output tensor. The value in brackets in the Instruction section is the corresponding enumeration value, which is used as the actual input. ACL_FORMAT_ND (2), ACL_FORMAT_NZ (29), ACL_FORMAT_NCL (47), ACL_FORMAT_NCHW1 (0), ACL_FORMAT_NHWC1 (1), ACL_FORMAT_HWCN1 (16), ACL_FORMAT_NCDHW1 (30), ACL_FORMAT_NDHWC1 (27), ACL_FORMAT_DHWCN1 (31), ACL_FORMAT_NDC1HWC01 (32), ACL_FORMAT_FRACTAL_Z_3D1 (33), ACL_FORMAT_NC1HWC01 (3), ACL_FORMAT_FRACTAL_Z1 (4). INT64 - - - additionalDtype (int) Input Base data type used to infer the C0 size when converting to a private data format. The content in brackets in the Instruction section is the enumeration value corresponding to each data type. The actual input is the corresponding enumeration value. If -1 is passed, additionalDtype defaults to the data type of srcTensor. INT8 (2), UINT8 (4), INT32 (3), UINT32 (8), FLOAT (0), FLOAT16 (1), BFLOAT16 (27), FLOAT8_E4M3FN2 (36), FLOAT4_E2M12 (40), HIFLOAT82 (34), -1 - - - dstShape (int64_t**) Output Pointer to the array of the shapes of dstTensor for output. The memory pointed to by this pointer is allocated by this API and released by the caller. - None None 4-8 - dstShapeSize (uint64_t*) Output Pointer used to output the size of the dstTensor shape array. - None None None - actualFormat (int*) Output Pointer used to output the actual data format of dstTensor. The value in parentheses is the corresponding enumeration value. None ACL_FORMAT_ND(2), ACL_FORMAT_NZ(29), ACL_FORMAT_NCL(47), ACL_FORMAT_NCHW1(0), ACL_FORMAT_NHWC1(1), ACL_FORMAT_HWCN1(16), ACL_FORMAT_NCDHW1(30), ACL_FORMAT_NDHWC1(27), ACL_FORMAT_DHWCN1(31), ACL_FORMAT_NDC1HWC01(32), ACL_FORMAT_FRACTAL_Z_3D1(33), ACL_FORMAT_NC1HWC01(3), ACL_FORMAT_FRACTAL_Z1(4). None - Ascend 950PR&950DT products : The superscript "1" in the data type column of the preceding table indicates the data types or data formats not supported by this products.
Atlas A2 products ,Atlas A3 products : The superscript "2" in the data type column of the preceding table indicates the data types or data formats not supported by these products.
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:
Return Value Error Code Description ACLNN_ERR_PARAM_NULLPTR 161001 The input srcTensor is a null pointer. ACLNN_ERR_PARAM_INVALID 161002 The data format of srcTensor is out of the specified range. The data format of dstFormat is out of the specified range. The data type of additionalDtype is not in the specified range. The view shape dimension of srcTensor is not in the range [2, 6] ACLNN_ERR_RUNTIME_ERROR 361001 The product model is not supported. The conversion format is not supported.
aclnnNpuFormatCastGetWorkspaceSize
Parameters
Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor srcTensor (aclTensor*) Input Input tensor. The input data supports only contiguous tensors. The values in parentheses indicate the corresponding enumeration values. INT8 (2), UINT8 (4), INT32 (3), UINT32 (8), FLOAT (0), FLOAT16 (1), BFLOAT16 (27), FLOAT8_E4M3FN2 (36), FLOAT4_E2M12 (40), FLOAT4_E1M22 (41), HIFLOAT82 (34) ND, NZ, NCDHW, NDC1HWC0, FRACTAL_Z_3D, NCL2 2-6 - dstTensor (aclTensor*) Input Target tensor after conversion. Only contiguous tensors are supported. The corresponding enumeration value is given in parentheses. INT8 (2), UINT8 (4), INT32 (3), UINT32 (8), FLOAT (0), FLOAT16 (1), BFLOAT16 (27), FLOAT8_E4M3FN2 (36), FLOAT4_E2M12 (40), HIFLOAT82 (34) ACL_FORMAT_ND (2), ACL_FORMAT_NZ (29), ACL_FORMAT_NCL (47), ACL_FORMAT_NCHW1 (0), ACL_FORMAT_NHWC1 (1), ACL_FORMAT_HWCN1 (16), ACL_FORMAT_NCDHW1 (30), ACL_FORMAT_NDHWC1 (27), ACL_FORMAT_DHWCN1 (31), ACL_FORMAT_NDC1HWC01 (32), ACL_FORMAT_FRACTAL_Z_3D1 (33), ACL_FORMAT_NC1HWC01 (3), ACL_FORMAT_FRACTAL_Z1 (4). 4-8 - workspaceSize (uint64_t*) Input Size of the workspace to allocate on the device side. - None None None - executor (aclOpExecutor**) Input Op executor containing the operator calculation flow. - None None None - Ascend 950PR&950DT products : Superscript "1" in the data type column of the preceding table indicates data types or data formats not supported by this series.
Atlas A2 products andAtlas A3 products : Superscript "2" in the data type column of the preceding table indicates data types or data formats not supported by these series.
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:
Return Values Error Code Description ACLNN_ERR_PARAM_NULLPTR 161001 srcTensor or dstTensor is a null pointer. ACLNN_ERR_PARAM_INVALID 161002 The data type or data format of srcTensor is not in the supported range. The data type or data format of dstTensor is not in the supported range. srcTensor or dstTensor is a non-contiguous tensor. The dimension of the view shape of srcTensor is not in the range [2, 6], or the dimension of the storage shape of dstTensor is not in the range [4, 8].2 ACLNN_ERR_RUNTIME_ERROR 361001 The product model is not supported. Ascend 950PR&950DT products : The superscript "1" in the data type column of the preceding table indicates the interception types not supported by this products.
Atlas A2 products ,Atlas A3 products : The superscript "2" in the data type column of the preceding table indicates the data types intercepted because they are not supported by the products.
aclnnNpuFormatCast
Parameters
Parameter Input/Output Description workspace Input Workspace memory address allocated on the device. workspaceSize Input Size of the workspace allocated on the device, obtained by the first-phase API aclnnNpuFormatCastGetWorkspaceSize. executor Input 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
Deterministic computation: aclnnNpuFormatCast defaults to a deterministic implementation.
The input and output support the following data type combinations:
Ascend 950PR&950DT products - aclnnNpuFormatCastCalculateSizeAndFormat API:
| srcTensor | srcTensor Data Format | dstFormat | additionalDtype | actualFormat | | --------- | -------------------------------------------------------- | --------- | ---------------------------- | ------------------------------- | | INT8 | ACL_FORMAT_ND(2) | ACL_FORMAT_FRACTAL_NZ(29) | ACL_INT8(2) | ACL_FORMAT_FRACTAL_NZ(29) | | INT32 | ACL_FORMAT_ND(2) | ACL_FORMAT_FRACTAL_NZ(29) | ACL_FLOAT16(1), ACL_BF16(27) | ACL_FORMAT_FRACTAL_NZ_C0_16(50) | | FLOAT | ACL_FORMAT_ND(2) | ACL_FORMAT_FRACTAL_NZ(29) | ACL_FLOAT16(1), ACL_BF16(27) | ACL_FORMAT_FRACTAL_NZ_C0_16(50) | | FLOAT | ACL_FORMAT_ND(2) | ACL_FORMAT_FRACTAL_NZ(29) | ACL_FLOAT8_E4M3FN(36) | ACL_FORMAT_FRACTAL_NZ_C0_32(51) | | FLOAT16 | ACL_FORMAT_ND(2) | ACL_FORMAT_FRACTAL_NZ(29) | ACL_FLOAT16(1) | ACL_FORMAT_FRACTAL_NZ(29) | | BFLOAT16 | ACL_FORMAT_ND(2) | ACL_FORMAT_FRACTAL_NZ(29) | ACL_BF16(27) | ACL_FORMAT_FRACTAL_NZ(29) | | FLOAT8_E4M3FN | ACL_FORMAT_ND(2) | ACL_FORMAT_FRACTAL_NZ(29) | ACL_FLOAT8_E4M3FN(36) | ACL_FORMAT_FRACTAL_NZ(29) | | FLOAT8_E8M0 | ACL_FORMAT_ND(2)/ACL_FORMAT_NCL(47)/ACL_FORMAT_NCHW(0) | ACL_FORMAT_FRACTAL_NZ(29) | ACL_FLOAT8_E8M0 | ACL_FORMAT_FRACTAL_NZ(29) | | FLOAT4_E2M1 | ACL_FORMAT_ND(2) | ACL_FORMAT_FRACTAL_NZ(29) | ACL_FLOAT8_E4M3FN(36) | ACL_FORMAT_FRACTAL_NZ(29) | | FLOAT4_E2M1 | ACL_FORMAT_ND(2) | ACL_FORMAT_FRACTAL_NZ(29) | ACL_FLOAT4_E2M1(40) | ACL_FORMAT_FRACTAL_NZ(29) | | FLOAT4_E1M2 | ACL_FORMAT_ND(2) | ACL_FORMAT_FRACTAL_NZ(29) | ACL_FLOAT4_E1M2(41) | ACL_FORMAT_FRACTAL_NZ(29) | | HIFLOAT8 | ACL_FORMAT_ND(2) | ACL_FORMAT_FRACTAL_NZ(29) | HIFLOAT8(34) | ACL_FORMAT_FRACTAL_NZ(29) | | FLOAT4_E2M1 | ACL_FORMAT_FRACTAL_NZ(29) | ACL_FORMAT_ND(2) | ACL_FLOAT4_E2M1(40) | ACL_FORMAT_ND(2) | | FLOAT4_E2M1 | ACL_FORMAT_FRACTAL_NZ_C0_32(51) | ACL_FORMAT_ND(2) | ACL_FLOAT4_E2M1(40) | ACL_FORMAT_ND(2) | | INT8 | ACL_FORMAT_FRACTAL_NZ(29) | ACL_FORMAT_ND(2) | ACL_INT8(2) | ACL_FORMAT_ND(2) | | INT8 | ACL_FORMAT_FRACTAL_NZ_C0_16(50) | ACL_FORMAT_ND(2) | ACL_INT8(2) | ACL_FORMAT_ND(2) | | INT8 | ACL_FORMAT_FRACTAL_NZ_C0_32(51) | ACL_FORMAT_ND(2) | ACL_INT8(2) | ACL_FORMAT_ND(2) | | UINT8 | ACL_FORMAT_FRACTAL_NZ(29) | ACL_FORMAT_ND(2) | ACL_UINT8(4) | ACL_FORMAT_ND(2) | | UINT8 | ACL_FORMAT_FRACTAL_NZ_C0_16(50) | ACL_FORMAT_ND(2) | ACL_UINT8(4) | ACL_FORMAT_ND(2) | | UINT8 | ACL_FORMAT_FRACTAL_NZ_C0_32(51) | ACL_FORMAT_ND(2) | ACL_UINT8(4) | ACL_FORMAT_ND(2) | | FLOAT8_E4M3FN | ACL_FORMAT_FRACTAL_NZ(29) | ACL_FORMAT_ND(2) | ACL_FLOAT8_E4M3FN(36) | ACL_FORMAT_ND(2) | | FLOAT16 | ACL_FORMAT_FRACTAL_NZ(29) | ACL_FORMAT_ND(2) | ACL_FLOAT16(1) | ACL_FORMAT_ND(2) | | BFLOAT16 | ACL_FORMAT_FRACTAL_NZ(29) | ACL_FORMAT_ND(2) | ACL_BF16(27) | ACL_FORMAT_ND(2) | | INT32 | ACL_FORMAT_FRACTAL_NZ(29) | ACL_FORMAT_ND(2) | ACL_INT32(3) | ACL_FORMAT_ND(2) | | INT32 | ACL_FORMAT_FRACTAL_NZ_C0_2(52) | ACL_FORMAT_ND(2) | ACL_INT32(3) | ACL_FORMAT_ND(2) | | INT32 | ACL_FORMAT_FRACTAL_NZ_C0_4(53) | ACL_FORMAT_ND(2) | ACL_INT32(3) | ACL_FORMAT_ND(2) | | INT32 | ACL_FORMAT_FRACTAL_NZ_C0_16(50) | ACL_FORMAT_ND(2) | ACL_INT32(3) | ACL_FORMAT_ND(2) | | INT32 | ACL_FORMAT_FRACTAL_NZ_C0_32(51) | ACL_FORMAT_ND(2) | ACL_INT32(3) | ACL_FORMAT_ND(2) | | FLOAT | ACL_FORMAT_FRACTAL_NZ(29) | ACL_FORMAT_ND(2) | ACL_FLOAT(0) | ACL_FORMAT_ND(2) | | FLOAT | ACL_FORMAT_FRACTAL_NZ_C0_2(52) | ACL_FORMAT_ND(2) | ACL_FLOAT(0) | ACL_FORMAT_ND(2) | | FLOAT | ACL_FORMAT_FRACTAL_NZ_C0_4(53) | ACL_FORMAT_ND(2) | ACL_FLOAT(0) | ACL_FORMAT_ND(2) | | FLOAT | ACL_FORMAT_FRACTAL_NZ_C0_16(50) | ACL_FORMAT_ND(2) | ACL_FLOAT(0) | ACL_FORMAT_ND(2) | | FLOAT | ACL_FORMAT_FRACTAL_NZ_C0_32(51) | ACL_FORMAT_ND(2) | ACL_FLOAT(0) | ACL_FORMAT_ND(2) |
- aclnnNpuFormatCastGetWorkspaceSize API:
| srcTensor | dstTensor Data Type | srcTensor Data Format | dstTensor Data Format | | --------- | ----------------- | ------------------------------------------------------- | ------------------------------------------------------------------ | | INT8 | INT8 | ACL_FORMAT_ND(2) | ACL_FORMAT_FRACTAL_NZ(29) | | INT32 | INT32 | ACL_FORMAT_ND(2) | ACL_FORMAT_FRACTAL_NZ_C0_16(50) | | FLOAT | FLOAT | ACL_FORMAT_ND(2) | ACL_FORMAT_FRACTAL_NZ_C0_16(50)/ACL_FORMAT_FRACTAL_NZ_C0_32(51) | | FLOAT16 | FLOAT16 | ACL_FORMAT_ND(2) | ACL_FORMAT_FRACTAL_NZ(29) | | BFLOAT16 | BFLOAT16 | ACL_FORMAT_ND(2) | ACL_FORMAT_FRACTAL_NZ(29) | | FLOAT8_E4M3FN | FLOAT8_E4M3FN | ACL_FORMAT_ND(2) | ACL_FORMAT_FRACTAL_NZ(29) | | FLOAT8_E8M0 | FLOAT8_E8M0 | ACL_FORMAT_ND(2)/ACL_FORMAT_NCL(47)/ACL_FORMAT_NCHW(0) | ACL_FORMAT_FRACTAL_NZ(29) | | FLOAT4_E2M1 | FLOAT4_E2M1 | ACL_FORMAT_ND(2) | ACL_FORMAT_FRACTAL_NZ_C0_32(51) | | FLOAT4_E2M1 | FLOAT4_E2M1 | ACL_FORMAT_ND(2) | ACL_FORMAT_FRACTAL_NZ(29) | | FLOAT4_E1M2 | FLOAT4_E1M2 | ACL_FORMAT_ND(2) | ACL_FORMAT_FRACTAL_NZ(29) | | HIFLOAT8 | HIFLOAT8 | ACL_FORMAT_ND(2) | ACL_FORMAT_FRACTAL_NZ(29) | | INT8 | INT8 | ACL_FORMAT_FRACTAL_NZ(29) | ACL_FORMAT_ND(2) | | INT8 | INT8 | ACL_FORMAT_FRACTAL_NZ_C0_16(50) | ACL_FORMAT_ND(2) | | INT8 | INT8 | ACL_FORMAT_FRACTAL_NZ_C0_32(51) | ACL_FORMAT_ND(2) | | UINT8 | UINT8 | ACL_FORMAT_FRACTAL_NZ(29) | ACL_FORMAT_ND(2) | | UINT8 | UINT8 | ACL_FORMAT_FRACTAL_NZ_C0_16(50) | ACL_FORMAT_ND(2) | | UINT8 | UINT8 | ACL_FORMAT_FRACTAL_NZ_C0_32(51) | ACL_FORMAT_ND(2) | | INT32 | INT32 | ACL_FORMAT_FRACTAL_NZ(29) | ACL_FORMAT_ND(2) | | INT32 | INT32 | ACL_FORMAT_FRACTAL_NZ_C0_2(52) | ACL_FORMAT_ND(2) | | INT32 | INT32 | ACL_FORMAT_FRACTAL_NZ_C0_4(53) | ACL_FORMAT_ND(2) | | INT32 | INT32 | ACL_FORMAT_FRACTAL_NZ_C0_16(50) | ACL_FORMAT_ND(2) | | INT32 | INT32 | ACL_FORMAT_FRACTAL_NZ_C0_32(51) | ACL_FORMAT_ND(2) | | FLOAT | FLOAT | ACL_FORMAT_FRACTAL_NZ(29) | ACL_FORMAT_ND(2) | | FLOAT | FLOAT | ACL_FORMAT_FRACTAL_NZ_C0_2(52) | ACL_FORMAT_ND(2) | | FLOAT | FLOAT | ACL_FORMAT_FRACTAL_NZ_C0_4(53) | ACL_FORMAT_ND(2) | | FLOAT | FLOAT | ACL_FORMAT_FRACTAL_NZ_C0_16(50) | ACL_FORMAT_ND(2) | | FLOAT | FLOAT | ACL_FORMAT_FRACTAL_NZ_C0_32(51) | ACL_FORMAT_ND(2) | | FLOAT16 | FLOAT16 | ACL_FORMAT_FRACTAL_NZ(29) | ACL_FORMAT_ND(2) | | BFLOAT16 | BFLOAT16 | ACL_FORMAT_FRACTAL_NZ(29) | ACL_FORMAT_ND(2) | | FLOAT8_E4M3FN | FLOAT8_E4M3FN | ACL_FORMAT_FRACTAL_NZ(29) | ACL_FORMAT_ND(2) | | FLOAT4_E2M1 | FLOAT4_E2M1 | ACL_FORMAT_FRACTAL_NZ(29) | ACL_FORMAT_ND(2) | | FLOAT4_E2M1 | FLOAT4_E2M1 | ACL_FORMAT_FRACTAL_NZ_C0_32(51) | ACL_FORMAT_ND(2) |
C0 calculation method:
additionalDtype C0 ACL_INT8(2) 32 ACL_FLOAT16(1) 16 ACL_BF16(27) 16 ACL_FLOAT8_E4M3FN(36) 32 ACL_HIFLOAT8(34) 32 FLOAT8_E8M0 special scenarios:
- When the data type of srcTensor is FLOAT8_E8M0, the format of srcTensor is ND/NCL/NCHW, and dstFormat is FRACTAL_NZ, only 3-dimensional or 4-dimensional scale inputs are supported. The storage shapes after conversion are
(ceil(N/16), ceil(K/64), 16, 2)or(g, ceil(N/16), ceil(K/64), 16, 2), respectively. - The capability of converting FLOAT8_E8M0 scale to the Ascend-affine storage shape does not support graph mode. Only the single-operator mode and direct aclnn call scenario are supported.
- When the data type of srcTensor is FLOAT8_E8M0, the format of srcTensor is ND/NCL/NCHW, and dstFormat is FRACTAL_NZ, only 3-dimensional or 4-dimensional scale inputs are supported. The storage shapes after conversion are
Currently unsupported special scenarios:
- When the data type of srcTensor is the same as additionalDtype, the format of srcTensor is ND, and the type is FLOAT16 or BFLOAT16, if the dimensions are represented as [k, n], the scenario where k is 1 is not yet supported.
- Calling this API to convert to the Ascend-affine data format FRACTAL_NZ and then performing any operations that modify the tensor, such as contiguous, pad, and slice, is not supported;
- If the shape of either of the last two dimensions of srcTensor is 1, after converting to the Ascend-affine data format FRACTAL_NZ, operations that modify the tensor, including transpose, are also not allowed.
Atlas A3 products , Atlas A2 products
aclnnNpuFormatCastCalculateSizeAndFormat API parameters:
srcTensor dstFormat additionalDtype actualFormat Data type: INT8, UINT8, FLOAT, FLOAT16, BF16, INT32, UINT32; data format: ACL_FORMAT_ND(2) ACL_FORMAT_FRACTAL_NZ(29) INT8, UINT8, FLOAT, FLOAT16, BF16, INT32, UINT32 ACL_FORMAT_FRACTAL_NZ(29) Data type: INT8, UINT8, FLOAT, FLOAT16, BF16, INT32, UINT32; data format: ACL_FORMAT_NCL(47) ACL_FORMAT_FRACTAL_NZ(29) INT8, UINT8, FLOAT, FLOAT16, BF16, INT32, UINT32 ACL_FORMAT_FRACTAL_NZ(29) Data type: INT8, UINT8, FLOAT, FLOAT16, BF16, INT32, UINT32; data format: ACL_FORMAT_NCHW(0) ACL_FORMAT_FRACTAL_NZ(29) INT8, UINT8, FLOAT, FLOAT16, BF16, INT32, UINT32 ACL_FORMAT_FRACTAL_NZ(29) Data type: INT8, UINT8, FLOAT, FLOAT16, BF16, INT32, UINT32; data format: ACL_FORMAT_NCDHW(30) ACL_FORMAT_FRACTAL_NZ(29) INT8, UINT8, FLOAT, FLOAT16, BF16, INT32, UINT32 ACL_FORMAT_FRACTAL_NZ(29) Data type: INT8, UINT8, FLOAT, FLOAT16, BF16, INT32, UINT32; data format: ACL_FORMAT_FRACTAL_NZ(29) ACL_FORMAT_ND(2) INT8, UINT8, FLOAT, FLOAT16, BF16, INT32, UINT32 ACL_FORMAT_ND(2) Data type: INT8, UINT8, FLOAT, FLOAT16, BF16, INT32, UINT32; data format: ACL_FORMAT_FRACTAL_NZ(29) ACL_FORMAT_NCL(47) INT8, UINT8, FLOAT, FLOAT16, BF16, INT32, UINT32 ACL_FORMAT_NCL(47) Data type: INT8, UINT8, FLOAT, FLOAT16, BF16, INT32, UINT32; data format: ACL_FORMAT_FRACTAL_NZ(29) ACL_FORMAT_NCHW(0) INT8, UINT8, FLOAT, FLOAT16, BF16, INT32, UINT32 ACL_FORMAT_NCHW(0) Data type: INT8, UINT8, FLOAT, FLOAT16, BF16, INT32, UINT32; data format: ACL_FORMAT_FRACTAL_NZ(29) ACL_FORMAT_NCDHW(30) INT8, UINT8, FLOAT, FLOAT16, BF16, INT32, UINT32 ACL_FORMAT_NCDHW(30) Data type: INT8, UINT8, FLOAT, FLOAT16, BF16, INT32, UINT32; data format: ACL_FORMAT_NCDHW(30) ACL_FORMAT_NDC1HWC0(32) INT8, UINT8, FLOAT, FLOAT16, BF16, INT32, UINT32 ACL_FORMAT_NDC1HWC0(32) Data type: INT8, UINT8, FLOAT, FLOAT16, BF16, INT32, UINT32; data format: ACL_FORMAT_NDC1HWC0(32) ACL_FORMAT_NCDHW(30) INT8, UINT8, FLOAT, FLOAT16, BF16, INT32, UINT32 ACL_FORMAT_NCDHW(30) Data type: INT8, UINT8, FLOAT, FLOAT16, BF16, INT32, UINT32; data format: ACL_FORMAT_NCDHW(30) ACL_FORMAT_FRACTAL_Z_3D(33) INT8, UINT8, FLOAT, FLOAT16, BF16, INT32, UINT32 ACL_FORMAT_FRACTAL_Z_3D(33) Data type: INT8, UINT8, FLOAT, FLOAT16, BF16, INT32, UINT32; data format: ACL_FORMAT_FRACTAL_Z_3D(33) ACL_FORMAT_NCDHW(30) INT8, UINT8, FLOAT, FLOAT16, BF16, INT32, UINT32 ACL_FORMAT_NCDHW(30) Data type: INT8, UINT8, FLOAT, FLOAT16, BF16, INT32, UINT32; data format: ACL_FORMAT_DHWCN(31) ACL_FORMAT_FRACTAL_Z_3D(33) INT8, UINT8, FLOAT, FLOAT16, BF16, INT32, UINT32 ACL_FORMAT_FRACTAL_Z_3D(33) Data type: INT8, UINT8, FLOAT, FLOAT16, BF16, INT32, UINT32; data format: ACL_FORMAT_NCHW(0) ACL_FORMAT_NC1HWC0(3) INT8, UINT8, FLOAT, FLOAT16, BF16, INT32, UINT32 ACL_FORMAT_NC1HWC0(3) Data type: INT8, UINT8, FLOAT, FLOAT16, BF16, INT32, UINT32; data format: ACL_FORMAT_NC1HWC0(3) ACL_FORMAT_NCHW(0) INT8, UINT8, FLOAT, FLOAT16, BF16, INT32, UINT32 ACL_FORMAT_NCHW(0) Data type: INT8, UINT8, FLOAT, FLOAT16, BF16, INT32, UINT32; data format: ACL_FORMAT_NHWC(1) ACL_FORMAT_NC1HWC0(3) INT8, UINT8, FLOAT, FLOAT16, BF16, INT32, UINT32 ACL_FORMAT_NC1HWC0(3) Data type: INT8, UINT8, FLOAT, FLOAT16, BF16, INT32, UINT32; data format: ACL_FORMAT_NC1HWC0(3) ACL_FORMAT_NHWC(1) INT8, UINT8, FLOAT, FLOAT16, BF16, INT32, UINT32 ACL_FORMAT_NHWC(1) Data type: INT8, UINT8, FLOAT, FLOAT16, BF16, INT32, UINT32; data format: ACL_FORMAT_NCHW(0) ACL_FORMAT_FRACTAL_Z(4) INT8, UINT8, FLOAT, FLOAT16, BF16, INT32, UINT32 ACL_FORMAT_FRACTAL_Z(4) Data type: INT8, UINT8, FLOAT, FLOAT16, BF16, INT32, UINT32; data format: ACL_FORMAT_FRACTAL_Z(4) ACL_FORMAT_NCHW(0) INT8, UINT8, FLOAT, FLOAT16, BF16, INT32, UINT32 ACL_FORMAT_NCHW(0) Data type: INT8, UINT8, FLOAT, FLOAT16, BF16, INT32, UINT32; data format: ACL_FORMAT_HWCN(16) ACL_FORMAT_FRACTAL_Z(4) INT8, UINT8, FLOAT, FLOAT16, BF16, INT32, UINT32 ACL_FORMAT_FRACTAL_Z(4) Data type: INT8, UINT8, FLOAT, FLOAT16, BF16, INT32, UINT32; data format: ACL_FORMAT_FRACTAL_Z(4) ACL_FORMAT_HWCN(16) INT8, UINT8, FLOAT, FLOAT16, BF16, INT32, UINT32 ACL_FORMAT_HWCN(16) Data type: INT8, UINT8, FLOAT, FLOAT16, BF16, INT32, UINT32; data format: ACL_FORMAT_NDHWC(27) ACL_FORMAT_NDC1HWC0(32) INT8, UINT8, FLOAT, FLOAT16, BF16, INT32, UINT32 ACL_FORMAT_NDC1HWC0(32) Data type: INT8, UINT8, FLOAT, FLOAT16, BF16, INT32, UINT32; data format: ACL_FORMAT_NDC1HWC0(32) ACL_FORMAT_NDHWC(27) INT8, UINT8, FLOAT, FLOAT16, BF16, INT32, UINT32 ACL_FORMAT_NDHWC(27) aclnnNpuFormatCastGetWorkspaceSize API:
srcTensor dstTensor Data Type dstTensor Data Format INT8, UINT8, FLOAT, FLOAT16, BF16, INT32, UINT32 INT8, UINT8, FLOAT, FLOAT16, BF16, INT32, UINT32 ACL_FORMAT_FRACTAL_NZ(29) INT8, UINT8, FLOAT, FLOAT16, BF16, INT32, UINT32 INT8, UINT8, FLOAT, FLOAT16, BF16, INT32, UINT32 ACL_FORMAT_ND(2) INT8, UINT8, FLOAT, FLOAT16, BF16, INT32, UINT32 INT8, UINT8, FLOAT, FLOAT16, BF16, INT32, UINT32 ACL_FORMAT_NCDHW(30) INT8, UINT8, FLOAT, FLOAT16, BF16, INT32, UINT32 INT8, UINT8, FLOAT, FLOAT16, BF16, INT32, UINT32 ACL_FORMAT_NDC1HWC0(32) INT8, UINT8, FLOAT, FLOAT16, BF16, INT32, UINT32 INT8, UINT8, FLOAT, FLOAT16, BF16, INT32, UINT32 ACL_FORMAT_FRACTAL_Z_3D(33) INT8, UINT8, FLOAT, FLOAT16, BF16, INT32, UINT32 INT8, UINT8, FLOAT, FLOAT16, BF16, INT32, UINT32 ACL_FORMAT_NCL(47) INT8, UINT8, FLOAT, FLOAT16, BF16, INT32, UINT32 INT8, UINT8, FLOAT, FLOAT16, BF16, INT32, UINT32 ACL_FORMAT_NCHW(0) INT8, UINT8, FLOAT, FLOAT16, BF16, INT32, UINT32 INT8, UINT8, FLOAT, FLOAT16, BF16, INT32, UINT32 ACL_FORMAT_NC1HWC0(3) INT8, UINT8, FLOAT, FLOAT16, BF16, INT32, UINT32 INT8, UINT8, FLOAT, FLOAT16, BF16, INT32, UINT32 ACL_FORMAT_NHWC(1) INT8, UINT8, FLOAT, FLOAT16, BF16, INT32, UINT32 INT8, UINT8, FLOAT, FLOAT16, BF16, INT32, UINT32 ACL_FORMAT_FRACTAL_Z(4) INT8, UINT8, FLOAT, FLOAT16, BF16, INT32, UINT32 INT8, UINT8, FLOAT, FLOAT16, BF16, INT32, UINT32 ACL_FORMAT_HWCN(16) INT8, UINT8, FLOAT, FLOAT16, BF16, INT32, UINT32 INT8, UINT8, FLOAT, FLOAT16, BF16, INT32, UINT32 ACL_FORMAT_NDHWC(27) C0 calculation method:
C0 = 32B / ge::GetSizeByDataType(static_cast<op::DataType>(additionalDtype))srcTensor Base Type C0 ACL_FLOAT(0), ACL_INT32(3), ACL_UINT32(8) 8 ACL_FLOAT16(1), ACL_BF16(27) 16 ACL_INT8(2), ACL_UINT8(4), ACL_HIFLOAT8(34) 32 Currently unsupported special scenarios:
- Calling this API to convert to the Ascend-affine data format FRACTAL_NZ and then performing any operations that modify the tensor, such as contiguous, pad, and slice, is not supported;
- After converting to the Ascend-affine data format FRACTAL_NZ, performing any operations that modify the tensor, including transpose, is not allowed.
Example
Ascend 950PR&950DT products :The following sample code is for reference only. For the compilation and execution procedure, see Compile and Run Samples.
#include <iostream> #include <vector> #include "acl/acl.h" #include "aclnnop/aclnn_npu_format_cast.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) #define CEIL_DIV(x, y) ((((x) + (y)) - 1) / (y)) #define CEIL_ALIGN(x, y) ((((x) + (y)) - 1) / (y) * (y)) int64_t GetShapeSize(const std::vector<int64_t>& shape) { int64_t shapeSize = 1; for (auto i : shape) { shapeSize *= i; } return shapeSize; } extern "C" aclnnStatus aclnnNpuFormatCastCalculateSizeAndFormat(const aclTensor* srcTensor, const int dstFormat, const int additionalDtype, int64_t** dstShape, uint64_t* dstShapeSize, int* actualFormat); extern "C" aclnnStatus aclnnNpuFormatCastGetWorkspaceSize(const aclTensor* srcTensor, aclTensor* dstTensor,uint64_t* workspaceSize, aclOpExecutor** executor); extern "C" aclnnStatus aclnnNpuFormatCast(void* workspace, uint64_t workspaceSize, aclOpExecutor* executor, aclrtStream stream); 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 a 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; } template <typename T> int CreateAclTensorWithFormat(const std::vector<T>& hostData, const std::vector<int64_t>& shape, int64_t** storageShape, uint64_t* storageShapeSize, void** deviceAddr, aclDataType dataType, aclTensor** tensor, aclFormat format) { auto size = hostData.size() * 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 a 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]; } *tensor = aclCreateTensor(shape.data(), shape.size(), dataType, strides.data(), 0, format, *storageShape, *storageShapeSize, *deviceAddr); return 0; } 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 = 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 as required by the API. int64_t k = 64; int64_t n = 128; int64_t srcDim0 = k; int64_t srcDim1 = n; int dstFormat = 29; aclDataType srcDtype = aclDataType::ACL_INT32; aclDataType additionalDtype = aclDataType::ACL_FLOAT16; std::vector<int64_t> srcShape = {srcDim0, srcDim1}; void* srcDeviceAddr = nullptr; void* dstDeviceAddr = nullptr; aclTensor* srcTensor = nullptr; aclTensor* dstTensor= nullptr; std::vector<int32_t> srcHostData(k * n, 1); for (size_t i = 0; i < k; i++) { for (size_t j = 0; j < n; j++) { srcHostData[i * n + j] = (j + 1) % 128; } } std::vector<int32_t> dstTensorHostData(k * n, 1); int64_t* dstShape = nullptr; uint64_t dstShapeSize = 0; int actualFormat; // Create src aclTensor. ret = CreateAclTensor(srcHostData, srcShape, &srcDeviceAddr, srcDtype, &srcTensor); CHECK_RET(ret == ACL_SUCCESS, return ret); // 3. Call the CANN operator library API. uint64_t workspaceSize = 0; aclOpExecutor* executor; void* workspaceAddr = nullptr; // Calculate the shape and format of the target tensor. ret = aclnnNpuFormatCastCalculateSizeAndFormat(srcTensor, 29, additionalDtype, &dstShape, &dstShapeSize, &actualFormat); CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnNpuFormatCastCalculateSizeAndFormat failed. ERROR: %d\n", ret); return ret); ret = CreateAclTensorWithFormat(dstTensorHostData, srcShape, &dstShape, &dstShapeSize, &dstDeviceAddr, srcDtype, &dstTensor, static_cast<aclFormat>(actualFormat)); CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("CreateAclTensorWithFormat failed. ERROR: %d\n", ret); return ret); // Call the first-phase API aclnnNpuFormatCastGetWorkspaceSize. ret = aclnnNpuFormatCastGetWorkspaceSize(srcTensor, dstTensor, &workspaceSize, &executor); CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnNpuFormatCastGetWorkspaceSize failed. ERROR: %d\n", ret); return ret); // Allocate device memory based on the workspaceSize calculated by the first-phase API. 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 aclnnNpuFormatCast. ret = aclnnNpuFormatCast(workspaceAddr, workspaceSize, executor, stream); CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnNpuFormatCast failed. ERROR: %d\n", ret); return ret); // 4. (boilerplate) Wait synchronously 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 the device to the host. auto size = 1; for (size_t i = 0; i < dstShapeSize; i++) { size *= dstShape[i]; } std::vector<int32_t> resultData(size, 0); ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), dstDeviceAddr, 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: %d\n", i, resultData[i]); } // 6. Release dstShape, aclTensor, and aclScalar. delete[] dstShape; aclDestroyTensor(srcTensor); aclDestroyTensor(dstTensor); // 7. Release device resources. aclrtFree(srcDeviceAddr); aclrtFree(dstDeviceAddr); if (workspaceSize > 0) { aclrtFree(workspaceAddr); } aclrtDestroyStream(stream); aclrtResetDevice(deviceId); aclFinalize(); return 0; }
Atlas A3 products ,Atlas A2 products :The sample code is for reference only. For details about compiling and running, see Compile and Run Samples.
#include <iostream> #include <vector> #include "acl/acl.h" #include "aclnnop/aclnn_npu_format_cast.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) #define CEIL_DIV(x, y) ((((x) + (y)) - 1) / (y)) #define CEIL_ALIGN(x, y) ((((x) + (y)) - 1) / (y) * (y)) int64_t GetShapeSize(const std::vector<int64_t>& shape) { int64_t shapeSize = 1; for (auto i : shape) { shapeSize *= i; } return shapeSize; } extern "C" aclnnStatus aclnnNpuFormatCastCalculateSizeAndFormat(const aclTensor* srcTensor, const int dstFormat, const int additionalDtype, int64_t** dstShape, uint64_t* dstShapeSize, int* actualFormat); extern "C" aclnnStatus aclnnNpuFormatCastGetWorkspaceSize(const aclTensor* srcTensor, aclTensor* dstTensor,uint64_t* workspaceSize, aclOpExecutor** executor); extern "C" aclnnStatus aclnnNpuFormatCast(void* workspace, uint64_t workspaceSize, aclOpExecutor* executor, aclrtStream stream); 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 host-side data to 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. // Modify the format of src here. *tensor = aclCreateTensor(shape.data(), shape.size(), dataType, strides.data(), 0, aclFormat::ACL_FORMAT_NCDHW, shape.data(), shape.size(), *deviceAddr); return 0; } template <typename T> int CreateAclTensorWithFormat(const std::vector<T>& hostData, const std::vector<int64_t>& shape, int64_t** storageShape, uint64_t* storageShapeSize, void** deviceAddr, aclDataType dataType, aclTensor** tensor, aclFormat format) { auto size = hostData.size() * 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 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]; } *tensor = aclCreateTensor(shape.data(), shape.size(), dataType, strides.data(), 0, format, *storageShape, *storageShapeSize, *deviceAddr); return 0; } int main() { // 1. (Boilerplate) device/stream initialization. Refer to the acl API manual. // Set deviceId based on 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. The construction is customized based on the API. int dstFormat = 32; // Modify the target format here. aclDataType srcDtype = aclDataType::ACL_INT32; int additionalDtype = -1; // std::vector<int64_t> srcShape = {srcDim0 , srcDim1}; int64_t N = 1; int64_t C = 17; int64_t D = 1; int64_t H = 2; int64_t W = 2; std::vector<int64_t> srcShape = {N, C, D, H, W}; void* srcDeviceAddr = nullptr; void* dstDeviceAddr = nullptr; aclTensor* srcTensor = nullptr; aclTensor* dstTensor= nullptr; std::vector<int32_t> srcHostData(N * C * D * H * W, 1); int num = 0; for (int n = 0; n < N; ++n) { for (int c = 0; c < C; ++c) { for (int d = 0; d < D; ++d) { for (int h = 0; h < H; ++h) { for (int w = 0; w < W; ++w) { // Arrange in row-major order and calculate the linear index. int index = (((n * C + c) * D + d) * H + h) * W + w; srcHostData[index] = num; num++; } } } } } std::vector<int32_t> dstTensorHostData(N * C * D * H * W, 1); int64_t* dstShape = nullptr; uint64_t dstShapeSize = 0; int actualFormat; // Create src aclTensor. ret = CreateAclTensor(srcHostData, srcShape, &srcDeviceAddr, srcDtype, &srcTensor); CHECK_RET(ret == ACL_SUCCESS, return ret); // 3. Call the CANN operator library API. uint64_t workspaceSize = 0; aclOpExecutor* executor; void* workspaceAddr = nullptr; std::cout << "init actualFormat = " << actualFormat << std::endl; // Calculate the shape and format of the target tensor. ret = aclnnNpuFormatCastCalculateSizeAndFormat(srcTensor, dstFormat, additionalDtype, &dstShape, &dstShapeSize, &actualFormat); CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnNpuFormatCastCalculateSizeAndFormat failed. ERROR: %d\n", ret); return ret); std::cout << "actualFormat = " << actualFormat << std::endl; std::cout << "&dstShape = " << &dstShape << std::endl; std::cout << "dstShape = [ "; for (int64_t i = 0; i < dstShapeSize; ++i) { std::cout << dstShape[i] << " "; } std::cout << "]" << std::endl; ret = CreateAclTensorWithFormat(dstTensorHostData, srcShape, &dstShape, &dstShapeSize, &dstDeviceAddr, srcDtype, &dstTensor, static_cast<aclFormat>(actualFormat)); CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("CreateAclTensorWithFormat failed. ERROR: %d\n", ret); return ret); // Call the first-phase API aclnnNpuFormatCastGetWorkspaceSize. ret = aclnnNpuFormatCastGetWorkspaceSize(srcTensor, dstTensor, &workspaceSize, &executor); CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnNpuFormatCastGetWorkspaceSize failed. ERROR: %d\n", ret); return ret); // Allocate device memory based on the workspaceSize calculated by the first-phase API. 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 aclnnNpuFormatCast. ret = aclnnNpuFormatCast(workspaceAddr, workspaceSize, executor, stream); CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnNpuFormatCast failed. ERROR: %d\n", ret); return ret); // 4. (boilerplate) Wait synchronously until the task is 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. auto size = 1; for (size_t i = 0; i < dstShapeSize; i++) { size *= dstShape[i]; } std::vector<int32_t> resultData(size, 0); ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), dstDeviceAddr, 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: %d\n", i, resultData[i]); } // 6. Release dstShape, aclTensor, and aclScalar. delete[] dstShape; aclDestroyTensor(srcTensor); aclDestroyTensor(dstTensor); // 7. Release device resources. aclrtFree(srcDeviceAddr); aclrtFree(dstDeviceAddr); if (workspaceSize > 0) { aclrtFree(workspaceAddr); } aclrtDestroyStream(stream); aclrtResetDevice(deviceId); aclFinalize(); return 0; }