昇腾社区首页
中文
注册
开发者
下载

尾核&尾块

对于不同shape的输入进行数据切分时,可能会发生数据无法平均分配到多个核、同时每个核内的数据无法均分的情况。参考核间均分场景下的尾块处理核间不均分场景下的尾核处理的处理方式,将两者结合起来考虑整核的尾块、尾核的尾块的处理方式。

Tiling实现

由于本场景中核间、核内的数据均无法均分,在核间不均分场景下的尾核处理定义的Tiling结构体的基础上增加两个成员变量:

  • formerLastTileLength:数据量多的核最后一个分块大小,即整核的尾块大小。

    计算时,先按尾核Tiling中提到的分核策略,切分数据量多的核。

    1
    2
    3
    4
    5
    6
    // shape需要对齐到的datablock,
    uint32_t totalLengthAligned = ((totalLength + ALIGN_NUM - 1) / ALIGN_NUM) * ALIGN_NUM;
    // 计算整核数量
    uint32_t formerNum = (totalLengthAligned / ALIGN_NUM) % BLOCK_DIM;
    // 计算整核的数据量
    uint32_t formerLength = ((totalLengthAligned / BLOCK_DIM + ALIGN_NUM - 1) / ALIGN_NUM) * ALIGN_NUM;
    

    再按尾块Tiling中的切分策略,计算尾块长度。

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    uint32_t formerTileNum = formerLength / ALIGN_NUM/ UB_BLOCK_NUM;
    if (formerTileNum == 0) {
        formerTileLength = 0;
        formerLastTileLength = formerLength / ALIGN_NUM * ALIGN_NUM;
    } else if ((formerLength / ALIGN_NUM) % UB_BLOCK_NUM == 0) {
        formerTileLength = UB_BLOCK_NUM * ALIGN_NUM;
        lastTileLength = 0;
    } else {
        formerTileLength = UB_BLOCK_NUM * ALIGN_NUM;
        formerLastTileLength = formerLength - formerTileNum * formerTileLength;
    }
    
  • tailLastTileLength:数据量少的核最后一个分块大小,即尾核的尾块大小。

    计算时,先按尾核Tiling中提到的分核策略,切分数据量少的核。

    1
    2
    3
    4
    // 计算尾核数量
    uint32_t tailNum = BLOCK_DIM - formerNum;
    // 计算尾核的数据量
    uint32_t tailLength = (totalLengthAligned / BLOCK_DIM / ALIGN_NUM) * ALIGN_NUM;
    

    再按尾块Tiling中的切分策略,计算尾块长度。

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    uint32_t tailTileNum = tailLength / ALIGN_NUM/ UB_BLOCK_NUM;
    if (tailTileNum == 0) {
        tailTileLength = 0;
        tailLastTileLength = tailLength / ALIGN_NUM * ALIGN_NUM;
    } else if ((tailLength / ALIGN_NUM) % UB_BLOCK_NUM == 0) {
        tailTileLength = UB_BLOCK_NUM * ALIGN_NUM;
        tailLastTileLength = 0;
    } else {
        tailTileLength = UB_BLOCK_NUM * ALIGN_NUM;
        tailLastTileLength = tailLength - tailTileNum * tailTileLength ;
    }
    

算子类实现

Kernel侧Init函数和Process函数的实现需将核间均分场景下的尾块处理核间不均分场景下的尾核处理的实现结合起来。

Init函数中由于整核和尾核对应的tileLength和lastTileLength不同。因此需按照核间不均分场景下的尾核处理中提到的分别处理整核和尾核。后续对主块和尾块的CopyIn、Compute、CopyOut函数的处理方式与核间均分场景下的处理方式相同。

Init函数实现代码如下:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
__aicore__ inline void Init(GM_ADDR x, GM_ADDR y, GM_ADDR z, AddCustomTilingData tiling)
{
    if (AscendC::GetBlockIdx() < formerNum) {
        this->tileNum = tiling.formerTileNum;
        this->tileLength = tiling.formerTileLength;
        this->lastTileLength = tiling.formerLastTileLength;

        xGm.SetGlobalBuffer((__gm__ half *)x + tiling.formerLength * AscendC::GetBlockIdx(),
            tiling.formerLength);
        yGm.SetGlobalBuffer((__gm__ half *)y + tiling.formerLength * AscendC::GetBlockIdx(),
            tiling.formerLength);
        zGm.SetGlobalBuffer((__gm__ half *)z + tiling.formerLength * AscendC::GetBlockIdx(),
            tiling.formerLength);
    } else {
        this->tileNum = tiling.tailTileNum;
        this->tileLength = tiling.tailTileLength;
        this->lastTileLength = tiling.tailLastTileLength;

        xGm.SetGlobalBuffer((__gm__ half *)x + tiling.formerLength * tiling.formerNum +
            tiling.tailLength * (AscendC::GetBlockIdx() - tiling.formerNum), tiling.tailLength);
        yGm.SetGlobalBuffer((__gm__ half *)y + tiling.formerLength * tiling.formerNum +
            tiling.tailLength * (AscendC::GetBlockIdx() - tiling.formerNum), tiling.tailLength);
        zGm.SetGlobalBuffer((__gm__ half *)z + tiling.formerLength * tiling.formerNum +
            tiling.tailLength * (AscendC::GetBlockIdx() - tiling.formerNum), tiling.tailLength);
    }
    
    // 只有尾块的场景下,tileLength为0,因此取tileLength和lastTileLength的最大值来初始化
    uint32_t initBufferLength = AscendC::Std::max(this->tileLength, this->lastTileLength);
    pipe.InitBuffer(inQueueX, 1, initBufferLength * sizeof(half));
    pipe.InitBuffer(inQueueY, 1, initBufferLength * sizeof(half));
    pipe.InitBuffer(outQueueZ, 1, initBufferLength * sizeof(half));
}