产品 |
是否支持 |
|---|---|
Ascend 950PR/Ascend 950DT |
√ |
√ |
|
√ |
|
x |
|
√ |
|
x |
|
x |
在神经网络中,GELU是一个重要的激活函数,其灵感来源于relu和dropout,在激活中引入了随机正则的思想。为了降低GELU的算力需求,业界提出了FastGeluV2版本。本接口实现了FastGeluV2,计算公式如下:


其中,
1 2 | template <typename T, bool highPrecision = false, bool highPerformance = false> __aicore__ inline void FasterGeluV2(const LocalTensor<T>& dstLocal, const LocalTensor<T>& srcLocal, const LocalTensor<uint8_t>& sharedTmpBuffer, const uint32_t dataSize) |
1 2 | template <typename T, bool highPrecision = false, bool highPerformance = false> __aicore__ inline void FasterGeluV2(const LocalTensor<T>& dstLocal, const LocalTensor<T>& srcLocal, const uint32_t dataSize) |
由于该接口的内部实现中涉及复杂的数学计算,需要额外的临时空间来存储计算过程中的中间变量。临时空间支持开发者通过sharedTmpBuffer入参传入和接口框架申请两种方式。
通过sharedTmpBuffer传入的情况,开发者需要为tensor申请空间;接口框架申请的方式,开发者需要预留临时空间。临时空间大小BufferSize的获取方式如下:通过FasterGeluV2 Tiling中提供的接口获取需要预留空间范围的大小。
参数名 |
描述 |
|---|---|
T |
操作数的数据类型。支持的数据类型为:half、float。 |
highPrecision |
是否开启高精度模式,以提升运算准确度。默认值为false,表示不开启高精度模式。 注意:高精度模式只在half数据类型下开启后生效,该参数的取值不影响float数据类型下的接口精度和性能。 |
highPerformance |
是否开启高性能模式,以提升运算效率。默认值为false,表示不开启高性能模式。 注意:开启高性能模式相比于默认不开启高精度和高性能模式会有精度下降,同时开启高精度和高性能模式相比于仅开启高性能模式可能会有性能下降。针对Ascend 950PR/Ascend 950DT,该参数保留但不生效,取值为true或者false,接口的精度和性能没有区别。 |
参数名 |
输入/输出 |
描述 |
|---|---|---|
dstLocal |
输出 |
目的操作数。 类型为LocalTensor,支持的TPosition为VECIN/VECCALC/VECOUT。 |
srcLocal |
输入 |
源操作数。 类型为LocalTensor,支持的TPosition为VECIN/VECCALC/VECOUT。 源操作数的数据类型需要与目的操作数保持一致。 |
sharedTmpBuffer |
输入 |
临时缓存。 类型为LocalTensor,支持的TPosition为VECIN/VECCALC/VECOUT。 用于接口内部复杂计算时存储中间变量,由开发者提供。 临时空间大小BufferSize的获取方式请参考GetGeluMaxMinTmpSize。 |
dataSize |
输入 |
实际计算数据元素个数。 |
无
API调用方式可参考gelu算子样例。
1 2 3 4 5 6 7 8 9 10 | // dstLocal:输出Tensor // srcLocal:输入Tensor // dataSize:实际参与计算的元素个数 // 默认不开启高精度和高性能模式 AscendC::FasterGeluV2(dstLocal, srcLocal, dataSize); // 开启高精度模式,高精度模式只在srcType为half时生效 AscendC::FasterGeluV2<srcType, true, false>(dstLocal, srcLocal, dataSize); // 开启高性能模式 AscendC::FasterGeluV2<srcType, false, true>(dstLocal, srcLocal, dataSize); |
结果示例如下:
1 2 3 4 5 6 7 8 9 10 | 输入数据(srcLocal): [-1.251 1.074 -6.137 -9.67 -5.066 -9.44 -3.588 -5.758 -7.484 -5.35 -9.62 -4.33 -6.66 -3.732 0.0841 -8.59 -6.3 -4.62 -3.059 -8.34 -8.24 -7.617 -7.93 -3.592 -3.268 -5.406 -9.49 5.633 -5.3 -9.36 -6.715 -5.727 ] 输出数据(dstLocal): [-0.1411 0.916 -0. -0. -0. -0. -0. -0. -0. -0. -0. -0. -0. -0. 0.0486 -0. -0. -0. -0. -0. -0. -0. -0. -0. -0. -0. -0. 5.633 -0. -0. -0. -0. ] |