aclnnTransQuantParamV2
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 : Supported
Atlas training products : Not supported
Function
- Description: Converts the data type of the quantization calculation parameter scale from FLOAT32 to UINT64 or INT64, as required by the hardware.
- Formula:
outis in 64-bit format and is initialized to 0.Retains the high 19 bits of
scalebit-wise (that is, bits 13-31, and clears the low 13 bits), writes the result to the low 32 bits ofout, and sets bit 46 to 1.Perform subsequent calculations based on the
offsetvalue:- If
offsetdoes not exist, no further computation is performed. - If
offsetexists:Process the
offsetvalue as int, with a range of [-256, 255].Retain the lower 9 bits of
offsetand store them in bits 37 to 45 ofout.
- If
Function Prototype
Each operator uses a two-phase API. First call the "aclnnTransQuantParamV2GetWorkspaceSize" API to obtain the workspace size required for computation and the executor that contains the operator computation flow. Then call the "aclnnTransQuantParamV2" API to execute the computation.
aclnnStatus aclnnTransQuantParamV2GetWorkspaceSize(
const aclTensor* scale,
const aclTensor* offset,
const aclTensor* out,
uint64_t* workspaceSize,
aclOpExecutor** executor)aclnnStatus aclnnTransQuantParamV2(
void *workspace,
uint64_t workspaceSize,
aclOpExecutor *executor,
const aclrtStream stream)aclnnTransQuantParamV2GetWorkspaceSize
Parameters:
Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor scale (aclTensor*) Input Scale value in quantization. Corresponds to `scale` in the formula. - Empty tensors are not supported.
- Ensure that the scale data contains no NaN or Inf.
- For shape constraints, see Constraints.
FLOAT32 ND 1-2 × offset (aclTensor*) Input Optional parameter, offset value in quantization. Corresponds to `offset` in the formula. - Empty tensors are not supported.
- Ensure that the offset data does not contain NaN or Inf.
- For shape constraints, see Constraints.
FLOAT32 ND 1-2 × out (aclTensor*) Output Calculation output of quantization. Corresponds to `out` in the formula. - Empty tensors are not supported.
- For shape constraints, see Constraints.
UINT64, INT64 ND 1-2 √ workspaceSize(uint64_t*) Output Returns the size of the workspace to be allocated on the device. - - - - - executor(aclOpExecutor**) Output Returns the op executor containing the operator computation process. - - - - - 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:
Error Code Error Code Description ACLNN_ERR_PARAM_NULLPTR 161001 scale or out is a null pointer. ACLNN_ERR_PARAM_INVALID 161002 The data type or data format of scale, offset, or out is not within the supported range. An error is reported if the shape of offset is not (t,) or (1, n), where t=1 or n. n is the same as the n of x2 in the matmul computation (or the shape n of the right matrix (corresponding parameter weight) in the grouped matmul computation). If scale is not of the 1-D shape (t,) or (g,) or the 2-D shape (1, n), (g, 1), or (g, n), an error is reported. Where: t = 1 or n; n is the same as the n dimension of x2 in the matmul computation (or the same as the n dimension of the shape of the right matrix (corresponding parameter: weight) in the grouped matmul computation); g is the same as the number of groups in the grouped matmul computation (the size of the shape of corresponding parameter: groupListOptional).
aclnnTransQuantParamV2
Parameters:
Parameter Input/Output Description workspace Input Workspace memory address allocated on the Device side. workspaceSize Input Workspace size allocated on the Device side, obtained by calling the first-phase API aclnnTransQuantParamV2GetWorkspaceSize. executor Input Op executor, which contains the operator computation flow. stream Input Stream used to execute tasks. Return Value
aclnnStatus: return status code. For details, see aclnn Error Codes.
Constraints
Atlas inference products ,Atlas A2 products ,Atlas A3 products ,Ascend 950PR&950DT products : This API can be used together with matmul operators (such as aclnnQuantMatmulV4).
Atlas inference products ,Atlas A2 products ,Atlas A3 products : This API cannot be used together with grouped matmul operators (such as aclnnGroupedMatmulV4).
- The shapes of scale, offset, and out are described as follows:
- When there is no offset, the out shape is the same as the scale shape.
- If out is used as the input of a matmul operator (such as aclnnQuantMatmulV4), the supported shapes are 1D (1,), (n,), or 2D (1, n), where n is the same as the shape n of the right matrix (corresponding parameter x2) in the matmul computation.
- If out is used as a grouped matmul class operator input (such as aclnnGroupedMatmulV4), it is only used when the grouping mode is m-axis grouping (corresponding parameter groupType is 0). Its shape supports 1D (g,) or 2D (g, 1), (g, n), where n is consistent with the shape n of the right matrix (corresponding parameter x2) in the grouped matmul computation, and g is consistent with the number of groups (corresponding parameter groupListOptional's shape size) in the grouped matmul computation.
- When offset is available, use it only as input of matmul operators (such as aclnnQuantMatmulV4):
- The shapes of offset, scale, and out support 1D (1,), (n,) or 2D (1, n), where n is consistent with the shape n of the right matrix (corresponding parameter x2) in the matmul computation.
- When the input scale's shape is 1D, the out shape is also 1D, and its shape size is the maximum of the single-dimension shape sizes of scale and offset.
- When the shape of the input scale is 2D, the shape of out has exactly the same dimensions and size as the shape of the input scale.
- When there is no offset, the out shape is the same as the scale shape.
- Deterministic computation:
- aclnnTransQuantParamV2 defaults to a deterministic implementation.
Example
The following sample code is for reference only. For details about compiling and running, see Compile and Run Samples.
#include <memory>
#include <iostream>
#include <vector>
#include "acl/acl.h"
#include "aclnnop/aclnn_trans_quant_param_v2.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 shapeSize = 1;
for (auto i : shape) {
shapeSize *= i;
}
return shapeSize;
}
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 memory on the device.
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 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 aclCreateTensor 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;
}
void Finalize(int32_t deviceId, aclrtStream stream)
{
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
}
int aclnnTransQuantParamV2Test(int32_t deviceId, 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 inputs and outputs. Perform custom construction based on the API.
std::vector<int64_t> offsetShape = {3};
std::vector<int64_t> scaleShape = {3};
std::vector<int64_t> outShape = {3};
void* scaleDeviceAddr = nullptr;
void* offsetDeviceAddr = nullptr;
void* outDeviceAddr = nullptr;
aclTensor* scale = nullptr;
aclTensor* offset = nullptr;
aclTensor* out = nullptr;
std::vector<float> scaleHostData = {1, 1, 1};
std::vector<float> offsetHostData = {1, 1, 1};
std::vector<uint64_t> outHostData = {1, 1, 1};
// Create the scale aclTensor.
ret = CreateAclTensor(scaleHostData, scaleShape, &scaleDeviceAddr, aclDataType::ACL_FLOAT, &scale);
std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor*)> scaleTensorPtr(scale, aclDestroyTensor);
std::unique_ptr<void, aclError (*)(void*)> scaleDeviceAddrPtr(scaleDeviceAddr, aclrtFree);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the offset aclTensor.
ret = CreateAclTensor(offsetHostData, offsetShape, &offsetDeviceAddr, aclDataType::ACL_FLOAT, &offset);
std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor*)> offsetTensorPtr(offset, aclDestroyTensor);
std::unique_ptr<void, aclError (*)(void*)> offsetDeviceAddrPtr(offsetDeviceAddr, aclrtFree);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the out aclTensor.
ret = CreateAclTensor(outHostData, outShape, &outDeviceAddr, aclDataType::ACL_UINT64, &out);
std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor*)> outTensorPtr(out, aclDestroyTensor);
std::unique_ptr<void, aclError (*)(void*)> outDeviceAddrPtr(outDeviceAddr, aclrtFree);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// 3. Call the CANN operator library API. Change it to the actual API name.
uint64_t workspaceSize = 0;
aclOpExecutor* executor = nullptr;
// Call the aclnnTransQuantParamV2 first-phase API.
ret = aclnnTransQuantParamV2GetWorkspaceSize(scale, offset, out, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnTransQuantParamV2GetWorkspaceSize 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_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret);
workspaceAddrPtr.reset(workspaceAddr);
}
// Call the aclnnTransQuantParamV2 second-phase API.
ret = aclnnTransQuantParamV2(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnTransQuantParamV2 failed. ERROR: %d\n", ret); return ret);
// 4. (Boilerplate) Synchronize and wait for the task execution 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 device memory to host memory. Modify it based on the definition of the specific API.
auto size = GetShapeSize(outShape);
std::vector<uint64_t> resultData(size, 0);
ret = aclrtMemcpy(
resultData.data(), resultData.size() * sizeof(resultData[0]), outDeviceAddr, 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: %lu\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 = aclnnTransQuantParamV2Test(deviceId, stream);
CHECK_FREE_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnTransQuantParamV2Test failed. ERROR: %d\n", ret); return ret);
Finalize(deviceId, stream);
return 0;
}