Copyright (c) 2026 Huawei Technologies Co., Ltd.
This program is free software, you can redistribute it and/or modify it under the terms and conditions of
CANN Open Software License Agreement Version 2.0 (the "License").
Please refer to the License for details. You may not use this file except in compliance with the License.
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
See LICENSE in the root of the software repository for the full text of the License.
*/
#include <pto/pto-inst.hpp>
#include <pto/common/constants.hpp>
#include "acl/acl.h"
using namespace pto;
template <typename T, int dstTileH, int dstTileW, int src0TileH, int src0TileW, int src1TileH, int src1TileW>
__global__ AICORE void runTFmod(__gm__ T __out__ *out, __gm__ T __in__ *src0, __gm__ T __in__ *src1, int vRows,
int vCols)
{
using DynShape = pto::Shape<-1, -1, -1, -1, -1>;
using DynStride = pto::Stride<-1, -1, -1, -1, -1>;
using GlobalData = GlobalTensor<T, DynShape, DynStride>;
GlobalData dstGlobal(out, pto::Shape(1, 1, 1, vRows, vCols), pto::Stride(1, 1, 1, dstTileW, 1));
GlobalData src0Global(src0, pto::Shape(1, 1, 1, vRows, vCols), pto::Stride(1, 1, 1, src0TileW, 1));
GlobalData src1Global(src1, pto::Shape(1, 1, 1, vRows, vCols), pto::Stride(1, 1, 1, src1TileW, 1));
TASSIGN(dstGlobal, out);
TASSIGN(src0Global, src0);
TASSIGN(src1Global, src1);
using TileDataDst = Tile<TileType::Vec, T, dstTileH, dstTileW, BLayout::RowMajor, -1, -1>;
using TileDataSrc0 = Tile<TileType::Vec, T, src0TileH, src0TileW, BLayout::RowMajor, -1, -1>;
using TileDataSrc1 = Tile<TileType::Vec, T, src1TileH, src1TileW, BLayout::RowMajor, -1, -1>;
TileDataDst dstTile(vRows, vCols);
TileDataSrc0 src0Tile(vRows, vCols);
TileDataSrc1 src1Tile(vRows, vCols);
TASSIGN(src0Tile, 0x0);
TASSIGN(src1Tile, src0TileH * src0TileW * sizeof(T));
TASSIGN(dstTile, src0TileH * src0TileW * sizeof(T) + src1TileH * src1TileW * sizeof(T));
TLOAD(src0Tile, src0Global);
TLOAD(src1Tile, src1Global);
#ifndef __PTO_AUTO__
set_flag(PIPE_MTE2, PIPE_V, EVENT_ID0);
wait_flag(PIPE_MTE2, PIPE_V, EVENT_ID0);
#endif
TFMOD<FmodAlgorithm::DEFAULT, TileDataDst, TileDataSrc0, TileDataSrc1>(dstTile, src0Tile, src1Tile);
#ifndef __PTO_AUTO__
set_flag(PIPE_V, PIPE_MTE3, EVENT_ID0);
wait_flag(PIPE_V, PIPE_MTE3, EVENT_ID0);
#endif
TSTORE(dstGlobal, dstTile);
out = dstGlobal.data();
}
template <typename T, int dstTileH, int dstTileW, int src0TileH, int src0TileW, int src1TileH, int src1TileW, int vRows,
int vCols>
void LaunchTFMOD(T *out, T *src0, T *src1, void *stream)
{
runTFmod<T, dstTileH, dstTileW, src0TileH, src0TileW, src1TileH, src1TileW>
<<<1, nullptr, stream>>>(out, src0, src1, vRows, vCols);
}
template void LaunchTFMOD<float, 16, 64, 16, 128, 16, 128, 16, 64>(float *out, float *src0, float *src1, void *stream);
template void LaunchTFMOD<float, 16, 32, 16, 64, 16, 32, 16, 32>(float *out, float *src0, float *src1, void *stream);
template void LaunchTFMOD<float, 16, 64, 16, 128, 16, 128, 16, 63>(float *out, float *src0, float *src1, void *stream);
template void LaunchTFMOD<float, 2, 32, 2, 64, 2, 32, 2, 31>(float *out, float *src0, float *src1, void *stream);
template void LaunchTFMOD<float, 1, 8192, 1, 8192, 1, 8192, 1, 8192>(float *out, float *src0, float *src1,
void *stream);