aclnnTransQuantParamV2
Applicable Products
| Product | Supported |
|---|---|
| √ | |
| √ | |
| √ | |
| × | |
| √ | |
| × |
Function
Description: Converts the data type of the quantization parameter scale from FLOAT32 to UINT64 or INT64 as required by the hardware.
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
Each operator is divided into a two-phase API. The "aclnnTransQuantParamV2GetWorkspaceSize" API must be called first to obtain the workspace size required for computation and the executor that encapsulates the operator computation flow. Then, the "aclnnTransQuantParamV2" API is called to perform 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.
- The user must ensure that the scale data contains no NaN or Inf.
- For shape constraints, see Constraints.
FLOAT32 ND 1-2 × offset (aclTensor*) Input Optional parameter, the offset value in quantization. Corresponds to `offset` in the formula. - Empty tensors are not supported.
- The user must ensure that the offset data contains no NaN or Inf.
- For shape constraints, see Constraints.
FLOAT32 ND 1-2 × out (aclTensor*) Output Quantization computation output. 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 Size of the workspace to be applied for on the Device side. - - - - - executor (aclOpExecutor**) Output Returns the op executor, which contains the operator computation flow. - - - - - Return value
aclnnStatus: return code. For details, see aclnn Return Code.
The first-phase API performs input parameter validation and returns an error in the following scenarios:
Return Code Error Code Description ACLNN_ERR_PARAM_NULLPTR 161001 The input 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. If the shape of offset is not (t,) or (1, n), an error is reported, where t = 1 or n, and n is consistent with n of x2 in the matmul computation (or consistent with shape n of the right matrix (corresponding to the weight parameter) in the grouped matmul computation). If the shape of scale is not one-dimensional (t,), (g,), or two-dimensional (1, n), (g, 1), (g, n), an error is reported. Where: t = 1 or n; n is consistent with n of x2 in the matmul computation (or consistent with shape n of the right matrix (corresponding to the weight parameter) in the grouped matmul computation); g is consistent with the number of groups in the grouped matmul computation (corresponding to the shape size of the groupListOptional parameter).
aclnnTransQuantParamV2
Parameters:
Parameter Input/Output Description Workspace Input Specifies the workspace memory address applied for on the device. workspaceSize Input Specifies the workspace size applied for on the device, obtained from the first-phase API aclnnTransQuantParamV2GetWorkspaceSize. executor Input Operator executor that contains the operator computation flow. stream Input Specifies the Stream for task execution. Return value
aclnnStatus: return code. For details, see aclnn Return Code.
Constraints
Atlas inference products ,Atlas A2 training products/Atlas A2 inference products ,Atlas A3 training products/Atlas A3 inference products ,Ascend 950PR/Ascend 950DT : This API supports use in conjunction with matmul operators (such as aclnnQuantMatmulV4).Atlas inference products ,Atlas A2 training products/Atlas A2 inference products ,Atlas A3 training products/Atlas A3 inference products : This API does not support use in conjunction with grouped matmul operators (such as aclnnGroupedMatmulV4).The shape descriptions for scale, offset, and out are as follows:
When offset is absent, the out shape is consistent with the scale shape.
If out is used as the input of a matmul operator (such as aclnnQuantMatmulV4), its shape supports 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.
If out is used as the input of a grouped matmul operator (such as aclnnGroupedMatmulV4), it is used only 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 to the shape size of parameter groupListOptional) in the grouped matmul computation.
When offset is present, it is used only as the input of a matmul operator (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 shape of the input scale is 1D, the shape of out 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.
Deterministic computation:
- aclnnTransQuantParamV2 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 <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);
// Allocate device memory by calling aclrtMalloc.
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);
// Copy host-side data to device-side memory by calling aclrtMemcpy.
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];
}
// Create an aclTensor by calling the aclCreateTensor API.
*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. Custom construction is required 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. Replace with the actual API name.
uint64_t workspaceSize = 0;
aclOpExecutor* executor = nullptr;
// Call the first-phase API of aclnnTransQuantParamV2.
ret = aclnnTransQuantParamV2GetWorkspaceSize(scale, offset, out, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnTransQuantParamV2GetWorkspaceSize failed. ERROR: %d\n", ret);
return ret);
// Apply for 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 second-phase API of aclnnTransQuantParamV2.
ret = aclnnTransQuantParamV2(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnTransQuantParamV2 failed. ERROR: %d\n", ret); return ret);
// 4. (Boilerplate) Synchronously wait for 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 the device memory to the host memory. Modify this based on the specific API definition.
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 the device/stream. 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;
}