Merged
engram训练算子kernel实现 #9179
liuyibinAdd on Jul 27
engram训练算子kernel实现 #9179
Merged
Merged fromliuyibin/ops-transformerengramintocann/ops-transformermaster
Total: 7 files changed+2358-19
| @@ -22,6 +22,7 @@ | |||
| 22 | #include "../engram_fetch_tiling_data.h" | 22 | #include "../engram_fetch_tiling_data.h" |
| 23 | #include "../engram_fetch_tiling_key.h" | 23 | #include "../engram_fetch_tiling_key.h" |
| 24 | #include "engram_fetch_arch35.h" | 24 | #include "engram_fetch_arch35.h" |
| 25 | +#include "engram_fetch_train_arch35.h" | ||
| 25 | 26 | ||
| 26 | using namespace Mc2Kernel; | 27 | using namespace Mc2Kernel; |
| 27 | 28 | ||
| @@ -40,6 +41,10 @@ __global__ __aicore__ void engram_fetch(GM_ADDR commContext, GM_ADDR indices, GM | |||
| 40 | op.Init(commContext, indices, fetched, workspaceGM, &pipe, &tilingData); | 41 | op.Init(commContext, indices, fetched, workspaceGM, &pipe, &tilingData); |
| 41 | op.Process(); | 42 | op.Process(); |
| 42 | } else if constexpr (EngramFetchMode == ENGRAM_FETCH_TRAIN_MODE) { | 43 | } else if constexpr (EngramFetchMode == ENGRAM_FETCH_TRAIN_MODE) { |
| 43 | - printf("hello engram_fetch train mode\n"); | 44 | + AscendC::TPipe pipe; |
| 45 | + EngramFetchTrainArch35 op; | ||
| 46 | + op.Init(commContext, indices, fetched, permOut, sendCountsOut, recvCountsOut, recvLocalEntryOut, numRecvOut, | ||
| 47 | + workspaceGM, localStorageAddr, &pipe, &tilingData); | ||
| 48 | + op.Process(); | ||
| 44 | } | 49 | } |
| 45 | } | 50 | } |
| @@ -33,6 +33,14 @@ constexpr int32_t BITS_PER_BYTE = 8; | |||
| 33 | constexpr uint32_t ALIGNED_LEN_256 = 256U; | 33 | constexpr uint32_t ALIGNED_LEN_256 = 256U; |
| 34 | constexpr uint32_t RELAY_BUFFER_NUM = 2U; | 34 | constexpr uint32_t RELAY_BUFFER_NUM = 2U; |
| 35 | 35 | ||
| 36 | +constexpr uint32_t STATE_OFFSET = 32U; | ||
| 37 | +constexpr uint32_t WIN_REGION_COUNT = 8U; | ||
| 38 | +constexpr uint32_t NUM_SLOTS = 4U; | ||
| 39 | +constexpr uint32_t INDICES_RATIO = 50U; | ||
| 40 | +constexpr uint32_t UB_RESERVED_SIZE = 8U * 1024U; | ||
| 41 | +constexpr uint32_t SENDER_CHANNEL_IDX = 0U; | ||
| 42 | +constexpr uint32_t RECEIVER_CHANNEL_IDX = 1U; | ||
| 43 | + | ||
| 36 | struct EngramCommContext { | 44 | struct EngramCommContext { |
| 37 | uint32_t rankId; | 45 | uint32_t rankId; |
| 38 | uint32_t rankSize; | 46 | uint32_t rankSize; |
| @@ -288,12 +288,6 @@ static ge::graphStatus CheckTensorDim(const gert::TilingContext *context, int64_ | |||
| 288 | (std::string("dim0=") + std::to_string(recvCountsShape->GetStorageShape().GetDim(0))).c_str(), | 288 | (std::string("dim0=") + std::to_string(recvCountsShape->GetStorageShape().GetDim(0))).c_str(), |
| 289 | "> 0"), | 289 | "> 0"), |
| 290 | return ge::GRAPH_FAILED); | 290 | return ge::GRAPH_FAILED); |
| 291 | - OP_TILING_CHECK(recvCountsShape->GetStorageShape().GetDim(0) != static_cast<int64_t>(rankSize), | ||
| 292 | - OP_LOGE_FOR_INVALID_SHAPE_WITH_REASON( | ||
| 293 | - nodeName, "recvCounts", | ||
| 294 | - (std::string("dim0=") + std::to_string(recvCountsShape->GetStorageShape().GetDim(0))).c_str(), | ||
| 295 | - (std::string("dim0 must equal sendCounts dim0(rankSize)=") + std::to_string(rankSize)).c_str()), | ||
| 296 | - return ge::GRAPH_FAILED); | ||
| 297 | 291 | ||
| 298 | // recvLocalEntry: 1D (R,) | 292 | // recvLocalEntry: 1D (R,) |
| 299 | const gert::StorageShape *recvLocalEntryShape = context->GetInputShape(IN_RECV_LOCAL_ENTRY); | 293 | const gert::StorageShape *recvLocalEntryShape = context->GetInputShape(IN_RECV_LOCAL_ENTRY); |
| @@ -577,6 +571,10 @@ static ge::graphStatus SetWorkSpace(gert::TilingContext *context, const EngramFe | |||
| 577 | OP_TILING_CHECK(totalRecv > 0 && hiddenBytes > INT64_MAX / totalRecv, | 571 | OP_TILING_CHECK(totalRecv > 0 && hiddenBytes > INT64_MAX / totalRecv, |
| 578 | OP_LOGE(nodeName, "workspace overflow: totalRecv=%ld, hiddenBytes=%ld", totalRecv, hiddenBytes), | 572 | OP_LOGE(nodeName, "workspace overflow: totalRecv=%ld, hiddenBytes=%ld", totalRecv, hiddenBytes), |
| 579 | return ge::GRAPH_FAILED); | 573 | return ge::GRAPH_FAILED); |
| 574 | + OP_TILING_CHECK(totalRecv > 0 && tilingData.hiddenDim > INT64_MAX / totalRecv, | ||
| 575 | + OP_LOGE(nodeName, "workspace overflow: totalRecv=%ld, hiddenDim=%ld", | ||
| 576 | + totalRecv, tilingData.hiddenDim), | ||
| 577 | + return ge::GRAPH_FAILED); | ||
| 580 | 578 | ||
| 581 | int64_t wsGradSorted = numTokens * hiddenBytes; | 579 | int64_t wsGradSorted = numTokens * hiddenBytes; |
| 582 | int64_t wsRecvGrad = totalRecv * hiddenBytes; | 580 | int64_t wsRecvGrad = totalRecv * hiddenBytes; |
| @@ -584,8 +582,14 @@ static ge::graphStatus SetWorkSpace(gert::TilingContext *context, const EngramFe | |||
| 584 | int64_t wsRdispls = numRanks * UB_ALIGN; | 582 | int64_t wsRdispls = numRanks * UB_ALIGN; |
| 585 | int64_t wsCounterScratch = static_cast<int64_t>(tilingData.aivNum) * UB_ALIGN; | 583 | int64_t wsCounterScratch = static_cast<int64_t>(tilingData.aivNum) * UB_ALIGN; |
| 586 | int64_t wsFlagScratch = 32; | 584 | int64_t wsFlagScratch = 32; |
| 585 | + uint32_t segLen = (static_cast<uint32_t>(tilingData.numEntriesPerRank) + tilingData.aivNum - 1U) / | ||
| 586 | + tilingData.aivNum; | ||
| 587 | + uint32_t segLenAlign = (segLen * sizeof(int32_t) + UB_ALIGN - 1) / UB_ALIGN * UB_ALIGN / sizeof(int32_t); | ||
| 588 | + int64_t wsSegUniqueEntry = static_cast<int64_t>(tilingData.aivNum) * segLenAlign * sizeof(int32_t); | ||
| 589 | + int64_t wsSegCount = static_cast<int64_t>(tilingData.aivNum) * sizeof(int32_t); | ||
| 587 | 590 | ||
| 588 | - int64_t wsTotal = wsGradSorted + wsRecvGrad + wsSdispls + wsRdispls + wsCounterScratch + wsFlagScratch; | 591 | + int64_t wsTotal = wsGradSorted + wsRecvGrad + wsSdispls + wsRdispls + wsCounterScratch + wsFlagScratch + |
| 592 | + wsSegUniqueEntry + wsSegCount; | ||
| 589 | wsTotal = AlignTo(wsTotal, BUFFER_ALIGNMENT); | 593 | wsTotal = AlignTo(wsTotal, BUFFER_ALIGNMENT); |
| 590 | wsTotal += SYSTEM_NEED_WORKSPACE; | 594 | wsTotal += SYSTEM_NEED_WORKSPACE; |
| 591 | 595 | ||
| @@ -11,16 +11,6 @@ | |||
| 11 | /*! | 11 | /*! |
| 12 | * \file engram_fetch_grad.cpp | 12 | * \file engram_fetch_grad.cpp |
| 13 | * \brief EngramFetchGrad 算子 kernel 入口 | 13 | * \brief EngramFetchGrad 算子 kernel 入口 |
| 14 | - * | ||
| 15 | - * 单融合 kernel,3 步: | ||
| 16 | - * ① grad_sorted = gradFetched[perm] | ||
| 17 | - * ② a2a(grad_sorted; sendCounts↔recvCounts) → recvGrad (用 commBuffer[peer] = a2a GM) | ||
| 18 | - * ③ owner unique + fp32 scatter-add (recvLocalEntry, recvGrad) | ||
| 19 | - * → gradUniqueOut (fp32) + uniqueLocalEntryOut (int32) + numUniqueOut (int32) | ||
| 20 | - * | ||
| 21 | - * kernel 签名按 op 定义顺序: input0~5, output0~2, workspace, tiling | ||
| 22 | - * input: commContext(0), gradFetched(1), perm(2), sendCounts(3), recvCounts(4), recvLocalEntry(5), numRecv(6) | ||
| 23 | - * output: gradUniqueOut(0), uniqueLocalEntryOut(1), numUniqueOut(2) | ||
| 24 | */ | 14 | */ |
| 25 | 15 | ||
| 26 | #if ASC_DEVKIT_MAJOR >= 9 | 16 | #if ASC_DEVKIT_MAJOR >= 9 |
| @@ -31,7 +21,8 @@ | |||
| 31 | #include "kernel_tiling/kernel_tiling.h" | 21 | #include "kernel_tiling/kernel_tiling.h" |
| 32 | #include "engram_fetch_grad_tiling_data.h" | 22 | #include "engram_fetch_grad_tiling_data.h" |
| 33 | #include "engram_fetch_grad_tiling_key.h" | 23 | #include "engram_fetch_grad_tiling_key.h" |
| 34 | -#include "../../engram_fetch/op_kernel/engram_fetch_utils.h" | 24 | +#include "arch35/engram_fetch_grad_arch35.h" |
| 25 | +#include "engram_fetch_grad_utils.h" | ||
| 35 | 26 | ||
| 36 | using namespace Mc2Kernel; | 27 | using namespace Mc2Kernel; |
| 37 | 28 | ||
| @@ -44,4 +35,10 @@ __global__ __aicore__ void engram_fetch_grad(GM_ADDR commContext, GM_ADDR gradFe | |||
| 44 | REGISTER_TILING_DEFAULT(EngramFetchGradTilingData); | 35 | REGISTER_TILING_DEFAULT(EngramFetchGradTilingData); |
| 45 | GET_TILING_DATA_WITH_STRUCT(EngramFetchGradTilingData, tilingData, tilingGM); | 36 | GET_TILING_DATA_WITH_STRUCT(EngramFetchGradTilingData, tilingData, tilingGM); |
| 46 | KERNEL_TASK_TYPE_DEFAULT(KERNEL_TYPE_AIV_ONLY); | 37 | KERNEL_TASK_TYPE_DEFAULT(KERNEL_TYPE_AIV_ONLY); |
| 38 | + | ||
| 39 | + TPipe pipe; | ||
| 40 | + EngramFetchGradArch35 op; | ||
| 41 | + op.Init(commContext, gradFetched, perm, sendCounts, recvCounts, recvLocalEntry, numRecv, | ||
| 42 | + gradUniqueOut, uniqueLocalEntryOut, numUniqueOut, workspaceGM, &pipe, &tilingData); | ||
| 43 | + op.Process(); | ||
| 47 | } | 44 | } |
| @@ -0,0 +1,50 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 3 | + * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | + * CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | + * Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 6 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 7 | + * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 8 | + * See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | + */ | ||
| 10 | + | ||
| 11 | +#ifndef ENGRAM_FETCH_GRAD_UTILS_H | ||
| 12 | +#define ENGRAM_FETCH_GRAD_UTILS_H | ||
| 13 | + | ||
| 14 | +#if ASC_DEVKIT_MAJOR >= 9 | ||
| 15 | +#include "basic_api/kernel_basic_intf.h" | ||
| 16 | +#else | ||
| 17 | +#include "kernel_operator.h" | ||
| 18 | +#endif | ||
| 19 | + | ||
| 20 | +namespace Mc2Kernel { | ||
| 21 | +constexpr uint32_t MAX_QP_SIZE = 1024U; | ||
| 22 | +constexpr uint32_t UB_ALIGN = 32U; | ||
| 23 | +constexpr uint32_t TILE_BYTES = 32U * 1024U; | ||
| 24 | +constexpr uint32_t HCOMM_INIT_SIZE = 512U; | ||
| 25 | +constexpr int32_t BITS_PER_BYTE = 8; | ||
| 26 | +constexpr uint32_t ALIGNED_LEN_256 = 256U; | ||
| 27 | + | ||
| 28 | +constexpr uint32_t STATE_OFFSET = 32U; | ||
| 29 | +constexpr uint32_t NUM_SLOTS = 4U; | ||
| 30 | +constexpr uint32_t SENDER_CHANNEL_IDX = 0U; | ||
| 31 | +constexpr uint32_t RECEIVER_CHANNEL_IDX = 1U; | ||
| 32 | + | ||
| 33 | +constexpr int32_t ENGRAM_DT_BFLOAT16 = 27; | ||
| 34 | +constexpr int32_t ENGRAM_DT_FLOAT16 = 1; | ||
| 35 | +constexpr int32_t ENGRAM_DT_FLOAT = 0; | ||
| 36 | + | ||
| 37 | +constexpr uint32_t ENTRY_BATCH_CAP = 1024U; | ||
| 38 | +constexpr uint32_t SENDCOUNT_STRIDE_RATIO = 8U; | ||
| 39 | + | ||
| 40 | +struct EngramCommContext { | ||
| 41 | + uint32_t rankId; | ||
| 42 | + uint32_t rankSize; | ||
| 43 | + uint64_t commBuffer[MAX_QP_SIZE]; | ||
| 44 | + uint64_t hcommHandle[MAX_QP_SIZE]; | ||
| 45 | + uint32_t channelsPerRank; | ||
| 46 | +}; | ||
| 47 | + | ||
| 48 | +} // namespace Mc2Kernel | ||
| 49 | + | ||
| 50 | +#endif | ||
🟡 Medium Priority
changed line:
engram_fetch_grad_utils.h第 38–43 行新增了struct EngramCommContext定义(位于namespace Mc2Kernel)。受影响行为/合约:
mc2/engram_fetch/op_kernel/engram_fetch_utils.h(第 42–47 行)中已存在完全相同的struct EngramCommContext,同样位于namespace Mc2Kernel,成员字段完全一致(rankId,rankSize,commBuffer[MAX_QP_SIZE],hcommHandle[MAX_QP_SIZE])。mc2/engram_fetch_grad/op_kernel/engram_fetch_grad.cpp第 34 行直接 include 了../../engram_fetch/op_kernel/engram_fetch_utils.h。虽然当前该.cpp尚未 includeengram_fetch_grad_arch35.h(后者会间接引入engram_fetch_grad_utils.h),但一旦后续集成 grad arch kernel 时在同一翻译单元中同时包含两个 utils 头文件,编译器将在同一命名空间下看到两个相同的 struct 定义,触发 ODR(One Definition Rule)违规,导致编译错误。触发条件:当
engram_fetch_grad.cpp(或任何其他 TU)同时(直接或间接)包含engram_fetch_utils.h和engram_fetch_grad_utils.h时。建议:将
EngramCommContext定义从两个 utils 头文件中移除,提取到一个共享基头文件中(如engram_fetch_common.h);或者在新文件engram_fetch_grad_utils.h中改为#include "../../engram_fetch/op_kernel/engram_fetch_utils.h"以复用已有定义,而非重新定义该结构体。