【CANN训练营】+开源之星+NotEqual算子Ascend C开发
收藏回复举报
【CANN训练营】+开源之星+NotEqual算子Ascend C开发
发表于2026-01-29 16:11:11
0 查看

背景信息

使用AscendC对TBE实现的NotEqual算子进行重构,实现了NotEqual算子,对Atlas A2 训练系列产品/Atlas 800I A2 推理产品/A200I A2 Box 异构组件的适配。

价值/作用

NotEqual算子功能:逐元素比较两个输入tensor,判断对应元素是否不相等。返回一个BOOL型tensor。

计算公式:$out_i = (self_i \neq other_i)$。

实现了NotEqual算子的AscendC实现,替代原有TBE算子在昇腾硬件上的适配。

设计方案

使能方式

Aclnn直调

总体设计

算子支持的数据类型

bfloat16/bool/float16/float32/int8/int32/int64/uint8

host侧设计

  1. tiling策略:当不需要广播的情况下,算子计算过程不涉及数据的维度信息,故在host侧将数据视为一维向量,仅考虑数据个数,不考虑数据维度信息。
  2. 分核策略:输入数据以512B为单位均分,优先使用满核的原则,当数据量较小时依实际性能降低核数优化头开销。
  3. 核内切分策略:开启double buffer,tile块最大大小在均分UB空间且满足对齐约束的条件下设到最大,尾块可一并处理,无需额外参数。
  4. tiling key规划策略:无需多分支,tiling key固定为0。

kernel侧设计

  1. 根据DTYPE_X1自动选择对应类型的模板函数。
  2. 循环执行数据搬入(CopyIn)、计算(Compute)、搬出(CopyOut)三个阶段,Compute阶段根据模板数据类型调用相应的NotEqual函数。
  3. Ascend C的NotEqual算子流程见下图。
graph TD
    A(input) --> B{"if dtype == 'bool':"};
    B -- 满足 --> C["dtype = uint8"] --> D;
    B -- 不满足 --> D{"if dtype in ('float16', 'int32'):"};
    D -- 满足 --> E["Compare<dtype, uint8>(CMPMODE::EQ)"]
    E --> F["Select<float16, uint8>(zeros, ones)"] --> P;
    D -- 不满足 --> G{"if dtype in ('float', 'bfloat16'):"};
    G -- 满足 --> H{"if dtype == 'bfloat16':"};
    H -- 满足 --> I["Cast<float, bfloat16>"] --> J;
    H -- 不满足 --> J["Compare(结果存入寄存器)<float>(CMPMODE::EQ)"];
    J --> K["Select<float>(zeros, ones)"]
    K --> L{"还有数据吗?"} -- 满足 --> J;
    L -- 不满足 --> M["Cast<int8, float>"] --> Q;
    G -- 不满足 --> N["Cast<float16, dtype>"];
    N --> O["res = min(|x1 - x2|, 2**(-24)) * 2**(12) * 2**(12)"];
    O --> P["Cast<int8, float16>"]
    P --> Q(return);

支持硬件

Atlas A2 训练系列产品/Atlas 800I A2 推理产品/A200I A2 Box 异构组件

算子约束限制

不支持广播,不支持int64。

核心代码

以float16、float、int8类型为例,展示三种计算流程的具体实现。

float16

template<typename T, typename U>
__aicore__ inline std::enable_if_t<std::is_same_v<T, half>> NotEqual(LocalTensor<int8_t> &y, LocalTensor<U> &x1, LocalTensor<U> &x2, int _)
{
    Compare(y, x1, x2, CMPMODE::EQ, ceil_round(_, static_cast<int>(256 / sizeof(U))));
    Duplicate(x1, static_cast<U>(0), _);
    Duplicate(x2, static_cast<U>(1), _);
    Select(x1, y, x1, x2, SELMODE::VSEL_TENSOR_TENSOR_MODE, _);
    Cast(y, x1, RoundMode::CAST_TRUNC, _);
}

float

template<typename T, typename U>
__aicore__ inline std::enable_if_t<std::is_same_v<T, float>> NotEqual(LocalTensor<int8_t> &y, LocalTensor<U> &x1, LocalTensor<U> &x2, int _)
{
    LocalTensor<U> y_U = y.ReinterpretCast<U>();
    LocalTensor<half> y_half = y.ReinterpretCast<half>();
    PipeBarrier<PIPE_V>();
    for (int i = 0; i < _; i += 256 / sizeof(U))
    {
        int mask = min(_ - i, static_cast<int>(256 / sizeof(U)));
        Compare<U, false>(x1[i], x2[i], CMPMODE::EQ, MASK_PLACEHOLDER, {});
        if (i == 0)
        {
            PipeBarrier<PIPE_V>();
            Duplicate(x1, static_cast<U>(0), mask);
            PipeBarrier<PIPE_V>();
            Duplicate(x2, static_cast<U>(1), mask);
        }
        PipeBarrier<PIPE_V>();
        Select<U, SELMODE::VSEL_CMPMASK_SPR>(y_U[i], x1, x2, 1, {});
    }
    PipeBarrier<PIPE_V>();
    Cast(y_half, y_U, RoundMode::CAST_RINT, _);
    PipeBarrier<PIPE_V>();
    Cast(y, y_half, RoundMode::CAST_TRUNC, _);
}

int8

template<typename T, typename U>
__aicore__ inline std::enable_if_t<is_one_of_v<T, int8_t, uint8_t>> NotEqual(LocalTensor<int8_t> &y, LocalTensor<U> &x1, LocalTensor<U> &x2, int _)
{
    LocalTensor<U> y_U = y.ReinterpretCast<U>();
    Cast(y_U, x1.template ReinterpretCast<T>(), RoundMode::CAST_NONE, _);
    Cast(x1, x2.template ReinterpretCast<T>(), RoundMode::CAST_NONE, _);
    Sub(y_U, y_U, x1, _);
    Abs(y_U, y_U, _);
    Mins(y_U, y_U, static_cast<U>(0x1p-24), _);
    Muls(y_U, y_U, static_cast<U>(0x1p12), _);
    Muls(y_U, y_U, static_cast<U>(0x1p12), _);
    Cast(y, y_U, RoundMode::CAST_TRUNC, _);
}

我要发帖子