aclnnWeightQuantBatchMatmul
Note: This API will be deprecated in later versions. Use the latest aclnnWeightQuantBatchMatmulV3 API instead.
Applicable Products
Ascend 950PR&950DT products : Not 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: Pseudo-quantization is used to quantize mat2 in self * mat2 (matmul/batchmatmul).
- Formula:
Function Prototype
- two-phase API. Call the aclnnWeightQuantBatchMatmulGetWorkspaceSize API first to obtain the input parameters and calculate the required workspace size based on the calculation process, and then call the aclnnWeightQuantBatchMatmul API to perform the computation.
aclnnStatus aclnnWeightQuantBatchMatmulGetWorkspaceSize(
const aclTensor *x1,
const aclTensor *x2,
const aclTensor *diagonalMatrix,
const aclTensor *deqOffset,
const aclTensor *deqScale,
const aclTensor *addOffset,
const aclTensor *mulScale,
const aclTensor *bias,
bool transposeX1,
bool transposeX2,
float antiquantScale,
float antiquantOffset,
aclTensor *out,
uint64_t *workspaceSize,
aclOpExecutor **executor)aclnnStatus aclnnWeightQuantBatchMatmul(
void *workspace,
uint64_t workspaceSize,
aclOpExecutor *executor,
const aclrtStream stream)aclnnWeightQuantBatchMatmulGetWorkspaceSize
Parameters
Return value
aclnnStatus: Return value. For details, see aclnn return codes.
The first-phase API performs input parameter validation and returns an error in the following scenarios:
Return Value Error Code Description ACLNN_ERR_PARAM_NULLPTR 161001 x1 or x2, diagonalMatrix (when m < 64), deqOffset (when m < 64), or deqScale (when m < 64) is a null pointer. ACLNN_ERR_PARAM_INVALID 161002 The data type of the input non-empty tensor is not supported.
aclnnWeightQuantBatchMatmul
Parameters
Parameter Input/Output Description workspace Input The workspace memory address applied on the Device side. workspaceSize Input The size of the workspace applied on the Device side, obtained by the first-phase API aclnnWeightQuantBatchMatmulGetWorkspaceSize. executor Input opExecutor that contains the operator computation process. stream Input Specifies the stream for executing tasks. Return value
aclnnStatus: return status code. For details, see aclnn Return Codes.
Constraints
Deterministic computation:
Atlas A3 products ,Atlas A2 products : aclnnWeightQuantBatchMatmul defaults to a deterministic implementation.
Example
The following sample code is for reference only. For details about compilation and execution, see Compile and Run Samples.
#include <iostream>
#include <vector>
#include <cmath>
#include "acl/acl.h"
#include "aclnnop/aclnn_trans_quant_param.h"
#include "aclnnop/aclnn_cast.h"
#include "aclnnop/aclnn_weight_quant_batch_matmul.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)
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 device-side memory.
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 host-side data to device-side 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);
// Compute the strides of the 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 the aclCreateTensor API 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;
}
int main() {
// 1. (Boilerplate) Initialize the device/stream. Refer to the acl API manual.
// Set deviceId based on the actual device.
int32_t deviceId = 0;
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 the inputs and outputs. Perform custom construction based on the API.
std::vector<int64_t> x1Shape = {128, 4};
std::vector<int64_t> x2Shape = {4, 4};
std::vector<int64_t> addOffsetShape = {4};
std::vector<int64_t> mulScaleShape = {4};
std::vector<int64_t> diagonalMatrixShape = {32, 32};
std::vector<int64_t> deqOffsetShape = {4};
std::vector<int64_t> deqScaleShape = {4};
std::vector<int64_t> outShape = {128, 4};
void* x1DeviceAddr = nullptr;
void* x2DeviceAddr = nullptr;
void* addOffsetDeviceAddr = nullptr;
void* mulScaleDeviceAddr = nullptr;
void* diagonalMatrixDeviceAddr = nullptr;
void* deqOffsetDeviceAddr = nullptr;
void* deqScaleDeviceAddr = nullptr;
void* outDeviceAddr = nullptr;
void* x1Fp16DeviceAddr = nullptr;
void* addOffsetFp16DeviceAddr = nullptr;
void* mulScaleFp16DeviceAddr = nullptr;
void* outFp16DeviceAddr = nullptr;
std::vector<float> x1HostData = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,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 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
bool transposeX1 = false;
bool transposeX2 = false;
float antiquantOffset = 0;
float antiquantScale = 1;
std::vector<float> addOffsetHostData = {1.0, 1.0, 1.0, 0.0};
float* addOffsetData = addOffsetHostData.data();
uint64_t addOffsetSize = 4;
std::vector<float> mulScaleHostData = {2.0, 2.0, 1.0, 1.0};
float* mulScaleData = mulScaleHostData.data();
uint64_t mulScaleSize = 4;
// diagonalMatrixData
uint64_t n = 32;
uint64_t diagonalMatrixSize = n*n;
int8_t *diagonalMatrixData = (int8_t *)calloc(diagonalMatrixSize, sizeof(int32_t));
for (int64_t i = 0; i < n; i++) {
diagonalMatrixData[i * n + i] = 1;
}
std::vector<int8_t> diagonalMatrixHostData(diagonalMatrixData, diagonalMatrixData + diagonalMatrixSize);
// Get deqOffset
uint64_t deqOffsetSize = addOffsetSize;
int32_t *deqOffsetData = (int32_t *)calloc(deqOffsetSize, sizeof(int32_t));
for (int64_t i = 0; i < deqOffsetSize; i++) {
deqOffsetData[i] = static_cast<int32_t>(round(addOffsetData[i] / antiquantScale - antiquantOffset));
}
std::vector<int32_t> deqOffsetHostData(deqOffsetData, deqOffsetData + deqOffsetSize);
// Get deqScale
uint64_t deqScaleSize = 0;
uint64_t *deqScaleData = nullptr;
ret = aclnnTransQuantParam(mulScaleData, mulScaleSize, addOffsetData, addOffsetSize, &deqScaleData, &deqScaleSize);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnTransQuantParam failed. ERROR: %d\n", ret); return ret);
std::vector<uint64_t> deqScaleHostData(deqScaleData, deqScaleData + deqScaleSize);
// create aclTensor
aclTensor* x1 = nullptr;
aclTensor* x2 = nullptr;
aclTensor* addOffset = nullptr;
aclTensor* mulScale = nullptr;
aclTensor* diagonalMatrix = nullptr;
aclTensor* deqOffset = nullptr;
aclTensor* deqScale = nullptr;
aclTensor* out = nullptr;
aclTensor* x1Fp16 = nullptr;
aclTensor* addOffsetFp16 = nullptr;
aclTensor* mulScaleFp16 = nullptr;
aclTensor* outFp16 = nullptr;
// Create the x1 aclTensor.
ret = CreateAclTensor(x1HostData, x1Shape, &x1DeviceAddr, aclDataType::ACL_FLOAT, &x1);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the x1Fp16 aclTensor.
ret = CreateAclTensor(x1HostData, x1Shape, &x1Fp16DeviceAddr, aclDataType::ACL_FLOAT16, &x1Fp16);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the x2 aclTensor.
ret = CreateAclTensor(x2HostData, x2Shape, &x2DeviceAddr, aclDataType::ACL_INT8, &x2);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the addOffset aclTensor.
ret = CreateAclTensor(addOffsetHostData, addOffsetShape, &addOffsetDeviceAddr, aclDataType::ACL_FLOAT, &addOffset);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the addOffsetFp16 aclTensor.
ret = CreateAclTensor(addOffsetHostData, addOffsetShape, &addOffsetFp16DeviceAddr, aclDataType::ACL_FLOAT16, &addOffsetFp16);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the mulScale aclTensor.
ret = CreateAclTensor(mulScaleHostData, mulScaleShape, &mulScaleDeviceAddr, aclDataType::ACL_FLOAT, &mulScale);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the mulScaleFp16 aclTensor.
ret = CreateAclTensor(mulScaleHostData, mulScaleShape, &mulScaleFp16DeviceAddr, aclDataType::ACL_FLOAT16, &mulScaleFp16);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the diagonalMatrix aclTensor.
ret = CreateAclTensor(diagonalMatrixHostData, diagonalMatrixShape, &diagonalMatrixDeviceAddr, aclDataType::ACL_INT8, &diagonalMatrix);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the deqOffset aclTensor.
ret = CreateAclTensor(deqOffsetHostData, deqOffsetShape, &deqOffsetDeviceAddr, aclDataType::ACL_INT32, &deqOffset);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the deqScale aclTensor.
ret = CreateAclTensor(deqScaleHostData, deqScaleShape, &deqScaleDeviceAddr, aclDataType::ACL_UINT64, &deqScale);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the out aclTensor.
ret = CreateAclTensor(outHostData, outShape, &outDeviceAddr, aclDataType::ACL_FLOAT, &out);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the outFp16 aclTensor.
ret = CreateAclTensor(outHostData, outShape, &outFp16DeviceAddr, aclDataType::ACL_FLOAT16, &outFp16);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// 3. Call the CANN operator library API and change it to the specific API name.
// aclnnWeightQuantBatchMatmul API call example
uint64_t workspaceSize = 0;
aclOpExecutor* executor;
// aclnn cast fp16
//x1
ret = aclnnCastGetWorkspaceSize(x1, aclDataType::ACL_FLOAT16, x1Fp16, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnCastGetWorkspaceSize failed. ERROR: %d\n", ret); return ret);
void* workspaceAddr = nullptr;
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);
}
ret = aclnnCast(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnCast failed. ERROR: %d\n", ret); return ret);
ret = aclrtSynchronizeStream(stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret);
// addOffset
ret = aclnnCastGetWorkspaceSize(addOffset, aclDataType::ACL_FLOAT16, addOffsetFp16, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnCastGetWorkspaceSize failed. ERROR: %d\n", ret); return ret);
workspaceAddr = nullptr;
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);
}
ret = aclnnCast(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnCast failed. ERROR: %d\n", ret); return ret);
ret = aclrtSynchronizeStream(stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret);
// mulScale
ret = aclnnCastGetWorkspaceSize(mulScale, aclDataType::ACL_FLOAT16, mulScaleFp16, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnCastGetWorkspaceSize failed. ERROR: %d\n", ret); return ret);
workspaceAddr = nullptr;
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);
}
ret = aclnnCast(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnCast failed. ERROR: %d\n", ret); return ret);
ret = aclrtSynchronizeStream(stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret);
// Call the first-phase API of aclnnWeightQuantBatchMatmul.
ret = aclnnWeightQuantBatchMatmulGetWorkspaceSize(x1Fp16, x2, diagonalMatrix, deqOffset, deqScale, addOffsetFp16, mulScaleFp16, nullptr, transposeX1, transposeX2, antiquantScale, antiquantOffset, outFp16, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnWeightQuantBatchMatmulGetWorkspaceSize failed. ERROR: %d\n", ret); return ret);
// Allocate device memory based on the workspaceSize calculated by the first-phase API.
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);
}
// Call the second-phase API of aclnnWeightQuantBatchMatmul.
ret = aclnnWeightQuantBatchMatmul(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnWeightQuantBatchMatmul failed. ERROR: %d\n", ret); return ret);
// 4. (boilerplate) Wait synchronously for the task execution to end.
ret = aclrtSynchronizeStream(stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret);
// fp16 to fp 32
ret = aclnnCastGetWorkspaceSize(outFp16, aclDataType::ACL_FLOAT, out, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnCastGetWorkspaceSize failed. ERROR: %d\n", ret); return ret);
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);
}
ret = aclnnCast(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnCast failed. ERROR: %d\n", ret); return ret);
ret = aclrtSynchronizeStream(stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret);
// 5. Obtain the output value, copy the result from device memory to the host side, and modify them according to the specific API definition.
auto size = GetShapeSize(outShape);
std::vector<float> 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: %f\n", i, resultData[i]);
}
// 6. Release aclTensor and aclScalar, and modify them according to the specific API definition.
aclDestroyTensor(x1);
aclDestroyTensor(x1Fp16);
aclDestroyTensor(x2);
aclDestroyTensor(addOffset);
aclDestroyTensor(addOffsetFp16);
aclDestroyTensor(mulScaleFp16);
aclDestroyTensor(mulScale);
aclDestroyTensor(out);
aclDestroyTensor(outFp16);
// 7. Release device resources, and modify them according to the specific API definition.
aclrtFree(x1DeviceAddr);
aclrtFree(x2DeviceAddr);
aclrtFree(addOffsetDeviceAddr);
aclrtFree(deqScaleDeviceAddr);
aclrtFree(mulScaleDeviceAddr);
aclrtFree(diagonalMatrixDeviceAddr);
aclrtFree(deqOffsetDeviceAddr);
aclrtFree(outDeviceAddr);
aclrtFree(x1Fp16DeviceAddr);
aclrtFree(addOffsetFp16DeviceAddr);
aclrtFree(mulScaleFp16DeviceAddr);
aclrtFree(outFp16DeviceAddr);
if (workspaceSize > 0) {
aclrtFree(workspaceAddr);
}
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
free(diagonalMatrixData);
free(deqOffsetData);
free(deqScaleData);
return 0;
}