aclnnTransQuantParam

📄 View Source Code

Applicable Products

ProductSupported
Ascend 950PR/Ascend 950DT√
Atlas A3 training products/Atlas A3 inference products√
Atlas A2 training products/Atlas A2 inference products√
Atlas 200I/500 A2 inference products×
Atlas inference products×
Atlas training products×

Function

  • Description: Converts the input scale data from FLOAT32 type to UINT64 type required by the hardware, and stores it in quantParam.

  • Formula:

    1. out is in 64-bit format, initialized to 0.

    2. The scale value is truncated to the upper 19 bits, stored at bit 32 of out, and bit 46 is set to 1.

      out=out ∣ (scale & 0xFFFFE000) ∣ (1≪46)out = out\ |\ (scale\ \&\ 0xFFFFE000)\ |\ (1\ll46)
    3. Perform subsequent calculations based on the offset value:

      • If offset does not exist, no further calculation is performed.

      • If offset exists:

        1. Process the offset value as int, with a range of [-256, 255].

          offset=Max(Min(INT(Round(offset)),255),−256)offset = Max(Min(INT(Round(offset)),255),-256)
        2. Retain 9 bits of offset and store them in bits 37 to 45 of out.

          out=(out & 0x4000FFFFFFFF) ∣ ((offset & 0x1FF)≪37)out = (out\ \&\ 0x4000FFFFFFFF)\ |\ ((offset\ \&\ 0x1FF)\ll37)

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;
}