|
产品 |
是否支持 |
|---|---|
|
Ascend 950PR/Ascend 950DT |
√ |
|
|
√ |
|
|
√ |
|
|
x |
|
|
√ |
|
|
x |
|
|
√ |
按元素做量化计算,比如将half/float数据类型量化为int8_t数据类型。计算公式如下,round表示四舍六入五成双取整,cast表示舍入模式:

如上图所示是AscendQuant内部算法框图,计算过程大致描述为如下几步,均在Vector上进行:
PER_TOKEN/PER_GROUP场景的计算逻辑如下:
1 2 |
template <typename T, bool isReuseSource = false, const AscendQuantConfig& config = ASCEND_QUANT_DEFAULT_CFG> __aicore__ inline void AscendQuant(const LocalTensor<int8_t>& dstTensor, const LocalTensor<T>& srcTensor, const LocalTensor<uint8_t>& sharedTmpBuffer, const float scale, const float offset, const uint32_t calCount) |
1 2 |
template <typename T, bool isReuseSource = false, const AscendQuantConfig& config = ASCEND_QUANT_DEFAULT_CFG> __aicore__ inline void AscendQuant(const LocalTensor<int8_t>& dstTensor, const LocalTensor<T>& srcTensor, const LocalTensor<uint8_t>& sharedTmpBuffer, const float scale, const float offset) |
1 2 |
template <typename T, bool isReuseSource = false, const AscendQuantConfig& config = ASCEND_QUANT_DEFAULT_CFG> __aicore__ inline void AscendQuant(const LocalTensor<int8_t>& dstTensor, const LocalTensor<T>& srcTensor, const float scale, const float offset, const uint32_t calCount) |
1 2 |
template <typename T, bool isReuseSource = false, const AscendQuantConfig& config = ASCEND_QUANT_DEFAULT_CFG> __aicore__ inline void AscendQuant(const LocalTensor<int8_t>& dstTensor, const LocalTensor<T>& srcTensor, const float scale, const float offset) |
1 2 |
template <typename T, bool isReuseSource = false, const AscendQuantConfig& config = ASCEND_QUANT_DEFAULT_CFG> __aicore__ inline void AscendQuant(const LocalTensor<int8_t>& dstTensor, const LocalTensor<T>& srcTensor, const LocalTensor<uint8_t>& sharedTmpBuffer, const LocalTensor<T>& scaleTensor, const T offset, const uint32_t scaleCount, const uint32_t calCount) |
1 2 |
template <typename T, bool isReuseSource = false, const AscendQuantConfig& config = ASCEND_QUANT_DEFAULT_CFG> __aicore__ inline void AscendQuant(const LocalTensor<int8_t>& dstTensor, const LocalTensor<T>& srcTensor, const LocalTensor<uint8_t>& sharedTmpBuffer, const LocalTensor<T>& scaleTensor, const LocalTensor<T>& offsetTensor, const uint32_t scaleCount, const uint32_t offsetCount, const uint32_t calCount) |
1 2 |
template <typename T, bool isReuseSource = false, const AscendQuantConfig& config = ASCEND_QUANT_DEFAULT_CFG> __aicore__ inline void AscendQuant(const LocalTensor<int8_t>& dstTensor, const LocalTensor<T>& srcTensor, const LocalTensor<uint8_t>& sharedTmpBuffer, const LocalTensor<T>& scaleTensor, const T offset) |
1 2 |
template <typename T, bool isReuseSource = false, const AscendQuantConfig& config = ASCEND_QUANT_DEFAULT_CFG> __aicore__ inline void AscendQuant(const LocalTensor<int8_t>& dstTensor, const LocalTensor<T>& srcTensor, const LocalTensor<uint8_t>& sharedTmpBuffer, const LocalTensor<T>& scaleTensor, const LocalTensor<T>& offsetTensor) |
1 2 |
template <typename T, bool isReuseSource = false, const AscendQuantConfig& config = ASCEND_QUANT_DEFAULT_CFG> __aicore__ inline void AscendQuant(const LocalTensor<int8_t>& dstTensor, const LocalTensor<T>& srcTensor, const LocalTensor<T>& scaleTensor, const T offset, const uint32_t scaleCount, const uint32_t calCount) |
1 2 |
template <typename T, bool isReuseSource = false, const AscendQuantConfig& config = ASCEND_QUANT_DEFAULT_CFG> __aicore__ inline void AscendQuant(const LocalTensor<int8_t>& dstTensor, const LocalTensor<T>& srcTensor, const LocalTensor<T>& scaleTensor, const LocalTensor<T>& offsetTensor, const uint32_t scaleCount, const uint32_t offsetCount, const uint32_t calCount) |
1 2 |
template <typename T, bool isReuseSource = false, const AscendQuantConfig& config = ASCEND_QUANT_DEFAULT_CFG> __aicore__ inline void AscendQuant(const LocalTensor<int8_t>& dstTensor, const LocalTensor<T>& srcTensor, const LocalTensor<T>& scaleTensor, const T offset) |
1 2 |
template <typename T, bool isReuseSource = false, const AscendQuantConfig& config = ASCEND_QUANT_DEFAULT_CFG> __aicore__ inline void AscendQuant(const LocalTensor<int8_t>& dstTensor, const LocalTensor<T>& srcTensor, const LocalTensor<T>& scaleTensor, const LocalTensor<T>& offsetTensor) |
1 2 |
template <typename dstT, typename srcT, bool isReuseSource = false> __aicore__ inline void AscendQuant(const LocalTensor<dstT>& dstTensor, const LocalTensor<srcT>& srcTensor, const LocalTensor<uint8_t>& sharedTmpBuffer, const float scale, const float offset, const uint32_t calCount) |
1 2 |
template <typename dstT, typename srcT, bool isReuseSource = false> __aicore__ inline void AscendQuant(const LocalTensor<dstT>& dstTensor, const LocalTensor<srcT>& srcTensor, const LocalTensor<uint8_t>& sharedTmpBuffer, const float scale, const float offset) |
1 2 |
template <typename dstT, typename srcT, bool isReuseSource = false> __aicore__ inline void AscendQuant(const LocalTensor<dstT>& dstTensor, const LocalTensor<srcT>& srcTensor, const float scale, const float offset, const uint32_t calCount) |
1 2 |
template <typename dstT, typename srcT, bool isReuseSource = false> __aicore__ inline void AscendQuant(const LocalTensor<dstT>& dstTensor, const LocalTensor<srcT>& srcTensor, const float scale, const float offset) |
1 2 |
template <typename dstT, typename srcT, bool isReuseSource = false> __aicore__ inline void AscendQuant(const LocalTensor<dstT>& dstTensor, const LocalTensor<srcT>& srcTensor, const LocalTensor<uint8_t>& sharedTmpBuffer, const LocalTensor<srcT>& scaleTensor, const srcT offset, const uint32_t scaleCount, const uint32_t calCount) |
1 2 |
template <typename dstT, typename srcT, bool isReuseSource = false> __aicore__ inline void AscendQuant(const LocalTensor<dstT>& dstTensor, const LocalTensor<srcT>& srcTensor, const LocalTensor<uint8_t>& sharedTmpBuffer, const LocalTensor<srcT>& scaleTensor, const LocalTensor<srcT>& offsetTensor, const uint32_t scaleCount, const uint32_t offsetCount, const uint32_t calCount) |
1 2 |
template <typename dstT, typename srcT, bool isReuseSource = false> __aicore__ inline void AscendQuant(const LocalTensor<dstT>& dstTensor, const LocalTensor<srcT>& srcTensor, const LocalTensor<uint8_t>& sharedTmpBuffer, const LocalTensor<srcT>& scaleTensor, const srcT offset) |
1 2 |
template <typename dstT, typename srcT, bool isReuseSource = false> __aicore__ inline void AscendQuant(const LocalTensor<dstT>& dstTensor, const LocalTensor<srcT>& srcTensor, const LocalTensor<uint8_t>& sharedTmpBuffer, const LocalTensor<srcT>& scaleTensor, const LocalTensor<srcT>& offsetTensor) |
1 2 |
template <typename dstT, typename srcT, bool isReuseSource = false> __aicore__ inline void AscendQuant(const LocalTensor<dstT>& dstTensor, const LocalTensor<srcT>& srcTensor, const LocalTensor<srcT>& scaleTensor, const srcT offset, const uint32_t scaleCount, const uint32_t calCount) |
1 2 |
template <typename dstT, typename srcT, bool isReuseSource = false> __aicore__ inline void AscendQuant(const LocalTensor<dstT>& dstTensor, const LocalTensor<srcT>& srcTensor, const LocalTensor<srcT>& scaleTensor, const LocalTensor<srcT>& offsetTensor, const uint32_t scaleCount, const uint32_t offsetCount, const uint32_t calCount) |
1 2 |
template <typename dstT, typename srcT, bool isReuseSource = false> __aicore__ inline void AscendQuant(const LocalTensor<dstT>& dstTensor, const LocalTensor<srcT>& srcTensor, const LocalTensor<srcT>& scaleTensor, const srcT offset) |
1 2 |
template <typename dstT, typename srcT, bool isReuseSource = false> __aicore__ inline void AscendQuant(const LocalTensor<dstT>& dstTensor, const LocalTensor<srcT>& srcTensor, const LocalTensor<srcT>& scaleTensor, const LocalTensor<srcT>& offsetTensor) |
1 2 |
template <typename dstT, typename srcT, typename scaleT, bool isReuseSource = false, const AscendQuantConfig& config, const AscendQuantPolicy& policy> __aicore__ inline void AscendQuant(const LocalTensor<dstT>& dstTensor, const LocalTensor<srcT>& srcTensor, const LocalTensor<uint8_t>& sharedTmpBuffer, const LocalTensor<scaleT>& scaleTensor, const LocalTensor<scaleT>& offsetTensor, const AscendQuantParam& para) |
1 2 |
template <typename dstT, typename srcT, typename scaleT, bool isReuseSource = false, const AscendQuantConfig& config, const AscendQuantPolicy& policy> __aicore__ inline void AscendQuant(const LocalTensor<dstT>& dstTensor, const LocalTensor<srcT>& srcTensor, const LocalTensor<uint8_t>& sharedTmpBuffer, const LocalTensor<scaleT>& scaleTensor,const scaleT offset, const AscendQuantParam& para) |
1 2 |
template <typename dstT, typename srcT, typename scaleT, bool isReuseSource = false, const AscendQuantConfig& config, const AscendQuantPolicy& policy> __aicore__ inline void AscendQuant(const LocalTensor<dstT>& dstTensor, const LocalTensor<srcT>& srcTensor, const LocalTensor<scaleT>& scaleTensor, const LocalTensor<scaleT>& offsetTensor, const AscendQuantParam& para) |
1 2 |
template <typename dstT, typename srcT, typename scaleT, bool isReuseSource = false, const AscendQuantConfig& config, const AscendQuantPolicy& policy> __aicore__ inline void AscendQuant(const LocalTensor<dstT>& dstTensor, const LocalTensor<srcT>& srcTensor, const LocalTensor<scaleT>& scaleTensor, const scaleT offset, const AscendQuantParam& para) |
由于该接口的内部实现中涉及复杂的数学计算,需要额外的临时空间来存储计算过程中的中间变量。临时空间支持接口框架申请和开发者通过sharedTmpBuffer入参传入两种方式。
接口框架申请的方式,开发者需要预留临时空间;通过sharedTmpBuffer传入的情况,开发者需要为sharedTmpBuffer申请空间。临时空间大小BufferSize的获取方式如下:通过GetAscendQuantMaxMinTmpSize中提供的GetAscendQuantMaxMinTmpSize接口获取需要预留空间的范围大小。
需要注意的是,在PER_TOKEN/PER_GROUP量化场景,内部实现不需要临时空间Buffer,对应的接口中sharedTmpBuffer为预留参数。
|
参数名 |
描述 |
|---|---|
|
T |
操作数的数据类型。支持的数据类型为:half、float。 |
|
isReuseSource |
是否允许修改源操作数。该参数预留,传入默认值false即可。 |
|
config |
结构体模板参数,此参数可选配,AscendQuantConfig类型,定义如下方代码所示,其中参数的含义如下。
当上述参数的取值满足如下任一种场景,将开启参数常量化,即编译过程中使用常量化的相关参数,从而减少Scalar计算。
|
1 2 3 4 5 6 |
struct AscendQuantConfig{ uint32_t calcCount = 0; uint32_t offsetCount = 0; uint32_t scaleCount = 0; uint32_t workLocalSize = 0; }; |
默认参数的配置示例如下。
1
|
constexpr AscendQuantConfig ASCEND_QUANT_DEFAULT_CFG = {0, 0, 0, 0}; |
|
参数名 |
描述 |
|---|---|
|
dstT |
目的操作数的数据类型。支持的数据类型为:int8_t、fp8_e4m3fn_t、fp8_e5m2_t、hifloat8_t、fp4x2_e1m2_t、fp4x2_e2m1_t。注意,对于fp4x2_e1m2_t、fp4x2_e2m1_t数据类型,仅在PER_GROUP场景下支持。 |
|
srcT |
源操作数的数据类型。支持的数据类型为:half、bfloat16_t、float。 |
|
isReuseSource |
是否允许修改源操作数。该参数预留,传入默认值false即可。 |
|
参数名 |
描述 |
|---|---|
|
scaleT |
量化参数scale和offset的数据类型。支持的数据类型为:half、bfloat16_t、float。 |
|
config |
量化接口配置参数,AscendQuantConfig类型,定义如下方代码所示,其中参数的含义如下。
|
|
policy |
量化策略配置参数,AscendQuantPolicy枚举类型,可取值如下:
|
1 2 3 4 5 |
struct AscendQuantConfig { bool hasOffset; int32_t kDim = 1; RoundMode roundMode = RoundMode::CAST_RINT; } |
|
srcDtype |
scaleDtype/offsetDtype |
dstDtype |
roundMode |
|---|---|---|---|
|
half |
half |
fp8_e5m2_t/fp8_e4m3fn_t |
|
|
bfloat16_t |
bfloat16_t |
||
|
float |
float |
||
|
half |
float |
||
|
bfloat16_t |
float |
||
|
half |
half |
hifloat8_t |
|
|
bfloat16_t |
bfloat16_t |
||
|
float |
float |
||
|
half |
float |
||
|
bfloat16_t |
float |
||
|
half |
half |
int8_t |
|
|
bfloat16_t |
bfloat16_t |
||
|
float |
float |
||
|
half |
float |
||
|
bfloat16_t |
float |
||
|
half |
half |
fp4x2_e1m2_t/fp4x2_e2m1_t (当前均只支持PER_GROUP场景) |
|
|
half |
float |
||
|
bfloat16_t |
bfloat16_t |
||
|
bfloat16_t |
float |
|
参数名 |
输入/输出 |
描述 |
|---|---|---|
|
dstTensor |
输出 |
目的操作数。 类型为LocalTensor,支持的TPosition为VECIN/VECCALC/VECOUT。 |
|
srcTensor |
输入 |
源操作数。 类型为LocalTensor,支持的TPosition为VECIN/VECCALC/VECOUT。 |
|
sharedTmpBuffer |
输入 |
临时缓存。 类型为LocalTensor,支持的TPosition为VECIN/VECCALC/VECOUT。 临时空间大小BufferSize的获取方式请参考GetAscendQuantMaxMinTmpSize。 |
|
scale |
输入 |
量化参数。 类型为Scalar,支持的数据类型为float。 |
|
offset |
输入 |
量化参数。 类型为Scalar,支持的数据类型为float。 |
|
calCount |
输入 |
参与计算的元素个数。 |
|
参数名 |
输入/输出 |
描述 |
|---|---|---|
|
dstTensor |
输出 |
目的操作数。 类型为LocalTensor,支持的TPosition为VECIN/VECCALC/VECOUT。 |
|
srcTensor |
输入 |
源操作数。 类型为LocalTensor,支持的TPosition为VECIN/VECCALC/VECOUT。 |
|
sharedTmpBuffer |
输入 |
临时缓存。 类型为LocalTensor,支持的TPosition为VECIN/VECCALC/VECOUT。 临时空间大小BufferSize的获取方式请参考GetAscendQuantMaxMinTmpSize。 |
|
scaleTensor |
输入 |
量化参数。 类型为LocalTensor,支持的TPosition为VECIN/VECCALC/VECOUT。 |
|
offsetTensor |
输入 |
量化参数。 类型为LocalTensor,支持的TPosition为VECIN/VECCALC/VECOUT。 |
|
scaleCount |
输入 |
实际量化参数元素个数,且scaleCount∈[0, min(scaleTensor.GetSize(),dstTensor.GetSize())],要求是32的整数倍。 |
|
offsetCount |
输入 |
实际量化参数元素个数,且offsetCount∈[0, min(offsetTensor.GetSize(),dstTensor.GetSize())],并且和scaleCount必须相等,要求是32的整数倍。 |
|
calCount |
输入 |
参与计算的元素个数。calCount必须是scaleCount的整数倍。 |
|
参数名 |
输入/输出 |
描述 |
|---|---|---|
|
dstTensor |
输出 |
目的操作数。 类型为LocalTensor,支持的TPosition为VECIN/VECCALC/VECOUT。 |
|
srcTensor |
输入 |
源操作数。 类型为LocalTensor,支持的TPosition为VECIN/VECCALC/VECOUT。 |
|
sharedTmpBuffer |
输入 |
临时缓存。 类型为LocalTensor,支持的TPosition为VECIN/VECCALC/VECOUT。 临时空间大小BufferSize的获取方式请参考GetAscendQuantMaxMinTmpSize。 |
|
scaleTensor |
输入 |
量化参数scale。 类型为LocalTensor,支持的TPosition为VECIN/VECCALC/VECOUT。 |
|
offsetTensor/offset |
输入 |
量化参数offset。
数据类型和scaleTensor保持一致。对于float4场景,offsetTensor/offset不生效。 |
|
para |
输入 |
量化接口的参数,AscendQuantParam类型,定义如下方代码所示,其中参数的含义如下。
|
1 2 3 4 5 6 |
struct AscendQuantParam { uint32_t m; uint32_t n; uint32_t calCount; uint32_t groupSize = 0; } |
无
完整的算子样例请参考quant算子样例。
1 2 3 4 5 6 7 8 9 10 11 12 13 |
// dstLocal: 存放量化计算的结果Tensor,shape为1024 // srcLocal: 存放量化计算的输入Tensor,shape为1024,类型为float/half // sharedTmpBuffer: 存放量化计算过程中临时缓存的Tensor const float scale = 0.02; // 量化参数 const float offset = 0.9; // 量化参数,dstLocal[i] = srcLocal[i] * scale + offset uint32_t calCount = 1022; // srcTensor的前calCount个于元素参与计算 // dstTensor为int8_t数据类型,通过sharedTmpBuffer入参传入临时空间 AscendC::AscendQuant<srcType>(dstLocal, srcLocal, sharedTmpBuffer, scale, offset, calCount); // dstTensor非固定数据类型 AscendC::AscendQuant<dstType, srcType>(dstLocal, srcLocal, scale, offset, calCount); |
结果示例如下:
1 2 3 4 |
输入数据(srcLocal): [-512. -511. -510. ... 509. 510. 511.] 输入量化参数(scale): 0.02 输入量化参数(offset): 0.9 输出数据(dstLocal): [-9 -9 -9 ... 11 51. 51.1] |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
// dstLocal: 存放量化计算的结果Tensor,shape为1024 // srcLocal: 存放量化计算的输入Tensor,shape为1024,类型为float/half // scaleLocal:存放量化参数的输入Tensor // offsetLocal:存放量化参数的输入Tensor // sharedTmpBuffer: 存放量化计算过程中临时缓存的Tensor uint32_t scaleCount = 64; // 量化参数,要求是32的整数倍 uint32_t offsetCount = 64; // 量化参数,要求与scaleCount相等 uint32_t calCount = 1022; // srcTensor的前calCount个于元素参与计算 // dstTensor为int8_t数据类型,通过sharedTmpBuffer入参传入临时空间 AscendC::AscendQuant<srcType>(dstLocal, srcLocal, sharedTmpBuffer, scaleLocal, offsetLocal, scaleCount, offsetCount, calCount); // dstTensor非固定数据类型 AscendC::AscendQuant<dstType, srcType>(dstLocal, srcLocal, scaleLocal, offsetLocal, scaleCount, offsetCount, calCount); |
结果示例如下:
1 2 3 4 |
输入数据(srcLocal): [-512. -511. -510. ... 509. 510. 511.] 输入量化参数(scale): [0.02 0.02 0.02 ... 0.02] 输入量化参数(offset): [1.01 1.02 1.03 ... 1.32] 输出数据(dstLocal): [-9 -9 -9 ... 11 510. 511.] |
PER_TOKEN/PER_GROUP场景调用示例如下。
1 2 3 4 5 6 7 8 9 10 |
// 注意m,n需从外部传入 constexpr static bool isReuseSource = false; constexpr static AscendQuantConfig config = {has_offset, 1}; constexpr static AscendQuantPolicy policy = AscendQuantPolicy::PER_TOKEN; // 可修改枚举值以开启PER_GROUP LocalTensor<uint8_t> sharedTmpBuffer = inQueue.AllocTensor<uint8_t>(); AscendQuantParam para; para.m = m; para.n = n; para.calCount = calCount; AscendQuant<dstType, srcType, scaleType, isReuseSource, config, policy>(dstLocal, srcLocal, sharedTmpBuffer, scaleLocal, offsetLocal, para); |
1 2 3 4 5 6 7 8 9 10 |
// 注意m,n需从外部传入 constexpr static bool isReuseSource = false; constexpr static AscendQuantConfig config = {has_offset, 1, RoundMode::CAST_ROUND}; constexpr static AscendQuantPolicy policy = AscendQuantPolicy::PER_TOKEN; // 可修改枚举值以开启PER_GROUP LocalTensor<uint8_t> sharedTmpBuffer = inQueue.AllocTensor<uint8_t>(); AscendQuantParam para; para.m = m; para.n = n; para.calCount = calCount; AscendQuant<dstType, srcType, scaleType, isReuseSource, config, policy>(dstLocal, srcLocal, sharedTmpBuffer, scaleLocal, offsetLocal, para); |