Tanh
产品支持情况
功能说明
按元素做逻辑回归Tanh,计算公式如下:


函数原型
-
通过sharedTmpBuffer入参传入临时空间
-
源操作数Tensor全部/部分参与计算
template <typename T, bool isReuseSource = false, const TanhConfig& config = DEFAULT_TANH_CONFIG> __aicore__ inline void Tanh(const LocalTensor<T>& dstTensor, const LocalTensor<T>& srcTensor, const LocalTensor<uint8_t>& sharedTmpBuffer, const uint32_t calCount) -
源操作数Tensor全部参与计算
template <typename T, bool isReuseSource = false, const TanhConfig& config = DEFAULT_TANH_CONFIG> __aicore__ inline void Tanh(const LocalTensor<T>& dstTensor, const LocalTensor<T>& srcTensor, const LocalTensor<uint8_t>& sharedTmpBuffer)
-
-
接口框架申请临时空间
-
源操作数Tensor全部/部分参与计算
template <typename T, bool isReuseSource = false, const TanhConfig& config = DEFAULT_TANH_CONFIG> __aicore__ inline void Tanh(const LocalTensor<T>& dstTensor, const LocalTensor<T>& srcTensor, const uint32_t calCount) -
源操作数Tensor全部参与计算
template <typename T, bool isReuseSource = false, const TanhConfig& config = DEFAULT_TANH_CONFIG> __aicore__ inline void Tanh(const LocalTensor<T>& dstTensor, const LocalTensor<T>& srcTensor)
由于该接口的内部实现中涉及复杂的数学计算,需要额外的临时空间来存储计算过程中的中间变量。临时空间支持开发者通过sharedTmpBuffer入参传入和接口框架申请两种方式。
- 通过sharedTmpBuffer入参传入,使用该tensor作为临时空间进行处理,接口框架不再申请。该方式开发者可以自行管理sharedTmpBuffer内存空间,并在接口调用完成后,复用该部分内存,内存不会反复申请释放,灵活性较高,内存利用率也较高。
- 接口框架申请临时空间,开发者无需申请,但是需要预留临时空间的大小。
通过sharedTmpBuffer传入的情况,开发者需要为tensor申请空间;接口框架申请的方式,开发者需要预留临时空间。临时空间大小BufferSize的获取方式如下:通过GetTanhMaxMinTmpSize中提供的接口获取需要预留空间范围的大小。
-
参数说明
表 1 模板参数说明
enum class TanhAlgo {
INTRINSIC = 0,
SUBSECTION_COMPENSATION,
};
struct TanhConfig {
TanhAlgo algo = TanhAlgo::INTRINSIC;
};
表 2 接口参数说明
|
类型为LocalTensor,支持的TPosition为VECIN/VECCALC/VECOUT。 |
||
|
类型为LocalTensor,支持的TPosition为VECIN/VECCALC/VECOUT。 |
||
|
类型为LocalTensor,支持的TPosition为VECIN/VECCALC/VECOUT。 临时空间大小BufferSize的获取方式请参考GetTanhMaxMinTmpSize。 |
||
返回值说明
无
约束说明
- 不支持源操作数与目的操作数地址重叠。
- 不支持sharedTmpBuffer与源操作数和目的操作数地址重叠。
- 操作数地址对齐要求请参见通用地址对齐约束。
调用示例
// dstLocal: 存放Tanh计算结果的Tensor
// srcLocal: 存放Tanh计算输入的Tensor
// sharedTmpBuffer: 存放Tanh计算过程中临时缓存的Tensor
// 接口框架申请临时空间,全部参与计算
AscendC::Tanh(dstLocal, srcLocal);
// 接口框架申请临时空间,部分参与计算, 需要参与计算的元素个数为512
AscendC::Tanh(dstLocal, srcLocal, 512);
// 通过sharedTmpBuffer入参传入临时空间,全部参与计算
AscendC::Tanh(dstLocal, srcLocal, sharedTmpBuffer);
// 通过sharedTmpBuffer入参传入临时空间,部分参与计算, 需要参与计算的元素个数为512
AscendC::Tanh(dstLocal, srcLocal, sharedTmpBuffer, 512);
static constexpr AscendC::TanhAlgo algo = AscendC::TanhAlgo::SUBSECTION_COMPENSATION;
static constexpr AscendC::TanhConfig config = { algo };
AscendC::Tanh<half, false, config>(dstLocal, srcLocal, sharedTmpBuffer, 512);
结果示例如下:
输入数据(srcLocal):
[-2.56 -2.55 -2.54 ... 0. ... 2.53 2.54 2.55]
输出数据(dstLocal):
[-0.98813187 -0.9878992 -0.98761402 ... 0. ... 0.98737127 0.98761402 0.9878992]