IsFinite

产品支持情况

产品

是否支持

Ascend 950PR/Ascend 950DT

Atlas A3 训练系列产品/Atlas A3 推理系列产品

x

Atlas A2 训练系列产品/Atlas A2 推理系列产品

x

功能说明

按元素判断输入的浮点数是否非NAN、非±INF,输出结果为浮点数或者布尔值。对于非NAN或者非±INF的输入数据,当输出为浮点类型时,对应位置的结果为该浮点类型的1,反之为0;当输出为bool类型时,对应位置的结果为true,反之为false。计算公式如下:

  • 当输出为浮点类型时:

  • 当输出为bool类型时:

函数原型

template<typename T, typename U>
__aicore__ inline void IsFinite(const LocalTensor<U>& dst, const LocalTensor<T>& src, uint32_t calCount)

参数说明

表 1 模板参数说明

参数名

描述

T

源操作数的数据类型。

Ascend 950PR/Ascend 950DT,支持的数据类型为:half、bfloat16_t、float。

U

目的操作数的数据类型。

Ascend 950PR/Ascend 950DT,支持的数据类型为:bool、half、bfloat16_t、float。

表 2 接口参数说明

参数名称

输入/输出

含义

dst

输出

目的操作数。

类型为LocalTensor,支持的TPosition为VECIN/VECCALC/VECOUT。

目的操作数的数据类型和源操作数保持一致,或者目的操作数的数据类型为bool类型。当前支持的数据类型组合请见表3

src

输入

源操作数。

类型为LocalTensor,支持的TPosition为VECIN/VECCALC/VECOUT。

calCount

输入

参与计算的元素个数。

表 3 输入输出支持的数据类型组合

srcDtype

dstDtype

half

half

half

bool

float

float

float

bool

bfloat16_t

bfloat16_t

bfloat16_t

bool

返回值说明

约束说明

  • 不支持源操作数与目的操作数地址重叠。
  • 操作数地址偏移对齐要求请参见通用说明和约束

调用示例

#include "kernel_operator.h"
 
AscendC::LocalTensor<SrcT> xLocal = inQueueX.DeQue<SrcT>();
if constexpr (std::is_same_v<DstT, bool>) {
    AscendC::LocalTensor<int8_t> yLocal = outQueueY.AllocTensor<int8_t>();
    AscendC::Duplicate(yLocal, (int8_t)0, dataSize);
    AscendC::IsFinite(yLocal, xLocal, calCount);
    outQueueY.EnQue(yLocal);
} else {
    AscendC::LocalTensor<DstT> yLocal = outQueueY.AllocTensor<DstT>();
    AscendC::Duplicate(yLocal, (DstT)0, dataSize);
    AscendC::IsFinite(yLocal, xLocal, calCount);
    outQueueY.EnQue(yLocal);
}
inQueueX.FreeTensor(xLocal);

结果示例如下:

输入的数据类型为float,输出的数据类型为bool
输入数据(src):
[1.0,+inf,3.0,4.0,nan,6.0,-inf,8.0]
输出数据(dst):
[true,false,true,true,false,true,false,true]