aclnnTransQuantParam

📄 View Source Code

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: 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 and is initialized to 0.

    2. For scale, retain the upper 19 bits (that is, bit 13-31; clear the lower 13 bits to zero), write to the lower 32 bits of out, and set bit 46 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 computation 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 the lower 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 Dim (Shape) Non-contiguous Tensor
    scaleArray (float*) Input Indicates pointing to the memory that stores scale data, corresponding to `scale` in the formula. Ensure that no NaN or inf exists in the scale data. - - - -
    scaleSize (uint64_t) Input Indicates the scale data count. You need to ensure that `scaleSize` is the same as the element count contained in `scaleArray`. - - - -
    offsetArray (float*) Input Indicates pointing to the memory storing offset data. It corresponds to `offset` in the corresponding formula. Ensure that no NaN or inf exists in the offset data. - - - -
    offsetSize (uint64_t) Input Indicates the count of offset data. You need to ensure that `offsetSize` is the same as the element count contained in `offsetArray`. - - - -
    quantParam (uint64_t**) Output Indicates pointing to the address of the memory that stores the converted quantParam data, corresponding to `out` in the formula. - - - - -
    quantParamSize (uint64_t*) Output Indicates the data count of the stored quantParam. You need to ensure that `quantParamSize` is the same as the element count contained in `quantParam`. - - - -
  • Return Value

    aclnnStatus: return status code. For details, see aclnn Error Codes.

    The API performs input parameter validation and returns an error in the following scenarios:

    Error Code Error Code Description
    ACLNN_ERR_PARAM_NULLPTR 161001 The parameter quantParam is a null pointer.
    The parameter scaleArray is a null pointer.
    The parameter quantParamSize is a null pointer.
    ACLNN_ERR_PARAM_INVALID 161002 When scaleArray is not a null pointer, scaleSize < 1.
    When offsetArray is not a null pointer, offsetSize < 1.
    When offsetArray is a null pointer, the parameter offsetSize is not equal to 0.
    ACLNN_ERR_INNER_NULLPTR 561103 Memory allocation for the output buffer quantParam fails (*quantParam is a null pointer).

Constraints

  • Deterministic computation:
    • aclnnTransQuantParam defaults to a deterministic implementation.

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