aclnnTransQuantParam
Applicable Products
| Product | Supported |
|---|---|
| √ | |
| √ | |
| √ | |
| × | |
| × | |
| × |
Function
Description: Converts the input scale data from FLOAT32 type to UINT64 type required by the hardware, and stores it in quantParam.
Formula:
outis in 64-bit format, initialized to 0.The
scalevalue is truncated to the upper 19 bits, stored at bit 32 ofout, and bit 46 is set to 1.Perform subsequent calculations based on the
offsetvalue:If
offsetdoes not exist, no further calculation is performed.If
offsetexists:Process the
offsetvalue as int, with a range of [-256, 255].Retain 9 bits of
offsetand store them in bits 37 to 45 of out.
Function Prototype
aclnnStatus aclnnTransQuantParam(
const float *scaleArray,
uint64_t scaleSize,
const float *offsetArray,
uint64_t offsetSize,
uint64_t **quantParam,
uint64_t *quantParamSize)aclnnTransQuantParam
Parameters:
Parameter Input/Output Description Instruction Data Type Data Format Dimension (Shape) Non-contiguous Tensor scaleArray (float*) Input Represents the memory pointer to the scale data, corresponding to `scale` in the formula. Ensure that the scale data contains no NaN or inf values. - - - - scaleSize (uint64_t) Input Indicates the number of scale data. Ensure that scaleSize and scaleArray contain the same number of elements. - - - - offsetArray (float*) Input Indicates the memory that stores the offset data, corresponding to `offset` in the formula. Ensure that the offset data contains no NaN or inf values. - - - - offsetSize (uint64_t) Input Indicates the number of offset data. You need to ensure that `offsetSize` and `offsetArray` contain the same number of elements. - - - - quantParam (uint64_t**) Output Indicates the address pointing to the memory that stores the converted quantParam data, corresponding to `out` in the formula. - - - - - quantParamSize (uint64_t*) Output Indicates the number of elements storing quantParam data. You need to ensure that `quantParamSize` matches the number of elements contained in `quantParam`. - - - - Return value
aclnnStatus: return code. For details, see aclnn Return Code.
The API performs input parameter validation and returns an error in the following scenarios:
| Return Code | Error Code | Description |
|---|---|---|
| ACLNN_ERR_PARAM_NULLPTR | 161001 | The quantParam parameter is a null pointer. |
| The scaleArray parameter is a null pointer. | ||
| The parameter quantParamSize is a null pointer. | ||
| ACLNN_ERR_PARAM_INVALID | 161002 | When scaleArray is not a null pointer, the parameter scaleSize < 1. |
| When offsetArray is not a null pointer, the parameter offsetSize < 1. | ||
| When offsetArray is a null pointer, the offsetSize parameter is not 0. | ||
| ACLNN_ERR_INNER_NULLPTR | 561103 | quantParam is a null pointer. |
Constraints
Deterministic computation:
- aclnnTransQuantParam defaults to a deterministic implementation.
Example
The following provides example code for reference only. For details about compilation and running, see Compile and Run Samples.
#include <iostream>
#include "acl/acl.h"
#include "aclnnop/aclnn_trans_quant_param.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)
int main()
{
float scaleArray[3] = {1.0, 1.0, 1.0};
uint64_t scaleSize = 3;
float offsetArray[3] = {1.0, 1.0, 1.0};
uint64_t offsetSize = 3;
uint64_t* result = nullptr;
uint64_t resultSize = 0;
auto ret = aclnnTransQuantParam(scaleArray, scaleSize, offsetArray, offsetSize, &result, &resultSize);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnTransQuantParam failed. ERROR: %d\n", ret); return ret);
for (auto i = 0; i < resultSize; i++) {
LOG_PRINT("result[%d] is: %ld\n", i, result[i]);
}
free(result);
return 0;
}