Merged
perf/softmax-focal-loss-grad-a5-long-row #14428
perf/softmax-focal-loss-grad-a5-long-row #14428
Merged
zsq_888Add on 8 days ago
Total: 5 files changed+168-13
@@ -55,7 +55,6 @@ constexpr int64_t ROW_SLOT_ELEM = UB_BLOCK_BYTES / DTYPE_LEN_FP32;
55constexpr int64_t ACC_BUF_NUM = 8; // 4 个行标量 + 4 个低位残差55constexpr int64_t ACC_BUF_NUM = 8; // 4 个行标量 + 4 个低位残差
56constexpr int64_t ACC_ALIGN_ELEM = 8;56constexpr int64_t ACC_ALIGN_ELEM = 8;
57constexpr uint64_t SIMD_SIMT_DCACHE_SIZE = static_cast<uint64_t>(32 * 1024);57constexpr uint64_t SIMD_SIMT_DCACHE_SIZE = static_cast<uint64_t>(32 * 1024);
58-constexpr size_t WORKSPACE_SIZE = static_cast<size_t>(16 * 1024 * 1024);
59constexpr int32_t TILING_ITER = 2;58constexpr int32_t TILING_ITER = 2;
60// reduction 的取值集合与大小写规则对齐 A2(canndev softmax_focal_loss_grad.py:242-243):59// reduction 的取值集合与大小写规则对齐 A2(canndev softmax_focal_loss_grad.py:242-243):
61// reduction.lower() 必须落在 {"none", "mean", "sum"} 内, 非法值报错; 只有 "mean" 需要缩放。60// reduction.lower() 必须落在 {"none", "mean", "sum"} 内, 非法值报错; 只有 "mean" 需要缩放。
@@ -364,17 +363,27 @@ ge::graphStatus SoftmaxFocalLossGradTiling::DoTiling()
364 CalCoreSplit();363 CalCoreSplit();
365 OP_CHECK_IF((CalUbSplit() != ge::GRAPH_SUCCESS), OP_LOGE(context_->GetNodeName(), "CalUbSplit failed"),364 OP_CHECK_IF((CalUbSplit() != ge::GRAPH_SUCCESS), OP_LOGE(context_->GetNodeName(), "CalUbSplit failed"),
366 return ge::GRAPH_FAILED);365 return ge::GRAPH_FAILED);
366+ // Distribute the available cores evenly across a small number of long rows.
367+ const int64_t coresPerRow = tilingData_->a > 0 ? coreNum_ / tilingData_->a : 0;
368+ const bool splitRow = coresPerRow > 1 && tilingData_->chunkNum >= 2 * coresPerRow;
369+ if (splitRow) {
370+ tilingData_->realCoreNum = tilingData_->a * coresPerRow;
371+ }
367 PrintTilingData();372 PrintTilingData();
368 373 
369 const uint64_t gammaMode = tilingData_->gamma == 1.0f ? 1 : (tilingData_->gamma == 2.0f ? 2 : 0);374 const uint64_t gammaMode = tilingData_->gamma == 1.0f ? 1 : (tilingData_->gamma == 2.0f ? 2 : 0);
370- const uint64_t tilingKey = GET_TPL_TILING_KEY(hasWeight_, gammaMode);375+ const uint64_t tilingKey = GET_TPL_TILING_KEY(hasWeight_, gammaMode, static_cast<uint64_t>(splitRow));
371 context_->SetTilingKey(tilingKey);376 context_->SetTilingKey(tilingKey);
372 context_->SetBlockDim(tilingData_->realCoreNum > 0 ? tilingData_->realCoreNum : 1);377 context_->SetBlockDim(tilingData_->realCoreNum > 0 ? tilingData_->realCoreNum : 1);
373 context_->SetScheduleMode(1);378 context_->SetScheduleMode(1);
374 context_->SetLocalMemorySize(ubSize_);379 context_->SetLocalMemorySize(ubSize_);
375 size_t* workspaces = context_->GetWorkspaceSizes(1);380 size_t* workspaces = context_->GetWorkspaceSizes(1);
376 OPS_CHECK_NULL_WITH_CONTEXT(context_, workspaces);381 OPS_CHECK_NULL_WITH_CONTEXT(context_, workspaces);
377- workspaces[0] = WORKSPACE_SIZE;382+ workspaces[0] = GetWorkspaceSize(context_);
383+ if (splitRow) {
384+ workspaces[0] += static_cast<size_t>(tilingData_->a) * static_cast<size_t>(tilingData_->chunkNum) * 8 *
385+ UB_BLOCK_BYTES;
386+ }
atomgit-bot
atomgit-botatomgit-bot8 days ago

🟡 Medium Priority

变更行:tiling_arch35.cpp:383-387 workspaces[0] = WORKSPACE_SIZE; // 16 MiB 常量 if (splitRow) { workspaces[0] += a * chunkNum * 8 * UB_BLOCK_BYTES; // 用户区被追加在 16MiB 之后 } 证据链:

  1. kernel 侧用 GetUserWorkspace(workspace) 作为 partial 区基址(softmax_focal_loss_grad.cpp:40),该函数返回 workspace + 平台系统区大小(GetLibApiWorkSpaceSize()),而不是固定偏移 16 MiB。
  2. 本改动首次让用户数据依赖这段布局:partial 区需要 a*chunkNum*8*UB_BLOCK_BYTES(与 kernel 的 partialGm_[chunk*8*ROW_SLOT_ELEM] 一一对应,已核对一致)。可用用户区实际是 workspaces[0] - 系统区大小 = 16MiB + 用户需求 - 系统区大小。
  3. 只要平台 GetLibApiWorkSpaceSize() > 16 MiB,用户区就被系统区吃掉 (系统区大小 - 16MiB) 字节,kernel 的 DataCopy(partialGm_[...]) 会写到 workspace 分配范围之外,踩坏其后的 GM(其他 tensor / 下一个算子的 workspace),表现为偶发精度错或 AIC Error。反之若系统区 < 16 MiB 则只是浪费显存,无害,所以问题被掩盖。
  4. 兄弟实现正是显式相加平台系统区大小:loss/softmax_focal_loss/op_host/arch35/softmax_focal_loss_tiling_arch35.cpp:362-364(chunkNum*4*UB_BLOCK_BYTES + platform.GetLibApiWorkSpaceSize()),仓内 mse_loss_v2 / multilabel_margin_loss / nll_loss / cosine_embedding_loss 的 tiling 也一律 用户区 + GetLibApiWorkSpaceSize()。
likedislike
Incorrect?
zsq_888
zsq_888
7 days ago Comments:
likedislike
378 return ge::GRAPH_SUCCESS;387 return ge::GRAPH_SUCCESS;
379}388}
380 389 
@@ -48,10 +48,15 @@ public:
48 __aicore__ inline SoftmaxFocalLossGradND() {}48 __aicore__ inline SoftmaxFocalLossGradND() {}
49 49 
50 __aicore__ inline void Init(GM_ADDR pred, GM_ADDR target, GM_ADDR dout, GM_ADDR weight, GM_ADDR grad,50 __aicore__ inline void Init(GM_ADDR pred, GM_ADDR target, GM_ADDR dout, GM_ADDR weight, GM_ADDR grad,
51- const SoftmaxFocalLossGradArch35TilingData* tiling, TPipe* pipe);51+ const SoftmaxFocalLossGradArch35TilingData* tiling, TPipe* pipe,
52+ GM_ADDR workspace = nullptr);
53+ template <bool splitRow = false>
52 __aicore__ inline void Process();54 __aicore__ inline void Process();
53 55 
54private:56private:
57+ __aicore__ inline void StorePartial(int64_t chunk);
58+ __aicore__ inline void MergePartials(int64_t row);
59+ __aicore__ inline void ProcessSplitRow();
55 __aicore__ inline void ProcessTile(int64_t rowGm, int64_t rows);60 __aicore__ inline void ProcessTile(int64_t rowGm, int64_t rows);
56 __aicore__ inline void CopyIn(int64_t rowGm, int64_t colOff, int64_t rows, int64_t len);61 __aicore__ inline void CopyIn(int64_t rowGm, int64_t colOff, int64_t rows, int64_t len);
57 // 搬运参数按 dtype 组装(blockLen/srcStride 走各自 sizeof, rightPadding 走各自行对齐)62 // 搬运参数按 dtype 组装(blockLen/srcStride 走各自 sizeof, rightPadding 走各自行对齐)
@@ -183,6 +188,7 @@ protected:
183 constexpr static float PRED_PAD_VALUE = 0.5f;188 constexpr static float PRED_PAD_VALUE = 0.5f;
184 189 
185private:190private:
191+ GlobalTensor<float> partialGm_;
186 GlobalTensor<T> predGm_;192 GlobalTensor<T> predGm_;
187 GlobalTensor<int32_t> targetGm_;193 GlobalTensor<int32_t> targetGm_;
188 GlobalTensor<T> doutGm_;194 GlobalTensor<T> doutGm_;
@@ -220,7 +226,7 @@ private:
220template <typename T, typename TW, uint64_t hasWeight, uint64_t gammaMode>226template <typename T, typename TW, uint64_t hasWeight, uint64_t gammaMode>
221__aicore__ inline void SoftmaxFocalLossGradND<T, TW, hasWeight, gammaMode>::Init(227__aicore__ inline void SoftmaxFocalLossGradND<T, TW, hasWeight, gammaMode>::Init(
222 GM_ADDR pred, GM_ADDR target, GM_ADDR dout, GM_ADDR weight, GM_ADDR grad,228 GM_ADDR pred, GM_ADDR target, GM_ADDR dout, GM_ADDR weight, GM_ADDR grad,
223- const SoftmaxFocalLossGradArch35TilingData* tiling, TPipe* pipe)229+ const SoftmaxFocalLossGradArch35TilingData* tiling, TPipe* pipe, GM_ADDR workspace)
224{230{
225 a_ = tiling->a;231 a_ = tiling->a;
226 r_ = tiling->r;232 r_ = tiling->r;
@@ -234,6 +240,9 @@ __aicore__ inline void SoftmaxFocalLossGradND<T, TW, hasWeight, gammaMode>::Init
234 alpha_ = tiling->alpha;240 alpha_ = tiling->alpha;
235 coef_ = tiling->reductionCoef;241 coef_ = tiling->reductionCoef;
236 pipe_ = pipe;242 pipe_ = pipe;
243+ if (workspace != nullptr) {
244+ partialGm_.SetGlobalBuffer((__gm__ float*)workspace);
245+ }
237 246 
238 predGm_.SetGlobalBuffer((__gm__ T*)pred);247 predGm_.SetGlobalBuffer((__gm__ T*)pred);
239 targetGm_.SetGlobalBuffer((__gm__ int32_t*)target);248 targetGm_.SetGlobalBuffer((__gm__ int32_t*)target);
@@ -299,8 +308,13 @@ __aicore__ inline void SoftmaxFocalLossGradND<T, TW, hasWeight, gammaMode>::Init
299}308}
300 309 
301template <typename T, typename TW, uint64_t hasWeight, uint64_t gammaMode>310template <typename T, typename TW, uint64_t hasWeight, uint64_t gammaMode>
311+template <bool splitRow>
302__aicore__ inline void SoftmaxFocalLossGradND<T, TW, hasWeight, gammaMode>::Process()312__aicore__ inline void SoftmaxFocalLossGradND<T, TW, hasWeight, gammaMode>::Process()
303{313{
314+ if constexpr (splitRow) {
315+ ProcessSplitRow();
316+ return;
317+ }
304 int64_t blockIdx = static_cast<int64_t>(GetBlockIdx());318 int64_t blockIdx = static_cast<int64_t>(GetBlockIdx());
305 if (blockIdx >= realCoreNum_) {319 if (blockIdx >= realCoreNum_) {
306 return;320 return;
@@ -0,0 +1,122 @@
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 SOFTMAX_FOCAL_LOSS_GRAD_PARALLEL_H
12+#define SOFTMAX_FOCAL_LOSS_GRAD_PARALLEL_H
13+ 
14+#include "softmax_focal_loss_grad_nd_vec.h"
15+ 
16+namespace SoftmaxFocalLossGrad {
17+ 
18+template <typename T, typename TW, uint64_t hasWeight, uint64_t gammaMode>
19+__aicore__ inline void SoftmaxFocalLossGradND<T, TW, hasWeight, gammaMode>::StorePartial(int64_t chunk)
20+{
21+ // Publish the raw chunk expansion before its merge into the temporary accumulator.
22+ const bool exact = ExactPath();
23+ LocalTensor<float> source = exact ? dfBuf_.template Get<float>() : accBuf_.template Get<float>();
24+ const int64_t sourceStride = exact ? VL_FP32 * ROW_SLOT_ELEM : accStride_;
25+ LocalTensor<float> stage = gradQue_.template AllocTensor<float>();
atomgit-bot
atomgit-botatomgit-bot8 days ago

🟡 Medium Priority

变更行:parallel.h:22 LocalTensor<float> source = dfBuf_.template Get<float>();(配合 33 行 src + slot * VL_FP32 * ROW_SLOT_ELEM 的固定跨步)把「本 chunk 的行和 hi/lo 中间量」一律当作 dfBuf_ 的内容发布到 workspace。

证据链:

  1. ExactRowSum4(softmax_focal_loss_grad_nd_vec.h:231-237)在 if (!ExactPath()) 分支里直接把四路结果写进传入的 accBase(ExactRowSum(src0, accBase, accBase + 4*accStride_, ...)),然后 return,根本不写 dfBuf_;只有精确路径(行组循环 + StoreAlign(rowHi/rowLo...),vec.h:301-308)才把 hi/lo 落到 dfBuf_。
  2. 因此当 ExactPath() 为 false 时,StorePartial 读到的是 dfBuf_ 里上一次 chunk 留下的旧值/未初始化值,并被当作该 chunk 的 partial 通过 DataCopy 写进 GM;MergePartials 随后把它合并成行标量,第一趟得到的 WF/WB/CE/W 全错,第二趟 grad 全错——属于静默的数值错误,无任何报错。
  3. 兄弟实现已经处理了这个分支:loss/softmax_focal_loss/op_kernel/arch35/softmax_focal_loss_parallel.h:33-34 的 StagePartial(chunk, exact) 用 exact ? dfBuf_ : accBuf_ 选源缓冲、sourceStride = exact ? VL_FP32*ROW_SLOT_ELEM : accStride_;本文件是照该实现移植的,但把 exact 分支判断丢了。

触发条件:splitRow=1 且 ExactPath()==false。当前 tiling 判据(chunkNum >= 2*coresPerRow,需 r 远大于 colsPerChunk)下大概率恒有 ExactPath()==true(r≫1024 时 blocks>log2(r)),所以目前多半不可达,但这是 kernel 与 tiling 之间一条未被校验的隐式契约:一旦 UB 预算/colsPerChunk 或 dtype 组合变化使 colsPerChunk 变小、r 落在 blocks<=log2(r) 的区间(r ≲ 700),该路径立刻产出错误结果。

建议:按 ExactPath() 选择源缓冲与跨步:source = ExactPath() ? dfBuf_ : accBuf_,slot 跨步 = ExactPath() ? VL_FP32*ROW_SLOT_ELEM : accStride_;并在 tiling 的 splitRow 判据中加入与 kernel 相同的 ExactPath() 条件。

likedislike
Incorrect?
zsq_888
zsq_888
7 days ago Comments:
likedislike
26+ auto src = (__ubuf__ float*)source.GetPhyAddr();
27+ auto dst = (__ubuf__ float*)stage.GetPhyAddr();
28+ uint32_t slotLanes = ROW_SLOT_ELEM;
29+ __VEC_SCOPE__
30+ {
31+ RegTensor<float> value;
32+ MaskReg mask = UpdateMask<float>(slotLanes);
33+ // Four high components followed by four low components, one 32B slot each.
34+ for (uint16_t slot = 0; slot < 8; ++slot) {
35+ AscendC::Reg::LoadAlign<float, LoadDist::DIST_BRC_B32>(value, src + slot * sourceStride);
36+ AscendC::Reg::StoreAlign(dst + slot * ROW_SLOT_ELEM, value, mask);
37+ }
38+ }
39+ gradQue_.template EnQue<float>(stage);
40+ stage = gradQue_.template DeQue<float>();
41+ DataCopy(partialGm_[chunk * 8 * ROW_SLOT_ELEM], stage, 8 * ROW_SLOT_ELEM);
42+ gradQue_.FreeTensor(stage);
43+}
44+ 
45+template <typename T, typename TW, uint64_t hasWeight, uint64_t gammaMode>
46+__aicore__ inline void SoftmaxFocalLossGradND<T, TW, hasWeight, gammaMode>::MergePartials(int64_t row)
47+{
48+ LocalTensor<float> acc = accBuf_.template Get<float>();
49+ Duplicate(acc, 0.0f, static_cast<int32_t>(8 * accStride_));
50+ PipeBarrier<PIPE_V>();
51+ // The first pass has released the input queue, which can hold a batch of partials.
52+ int64_t queueBytes = (rowsPerTile_ * colsPerChunk_ + VL_FP32) * sizeof(T);
53+ int64_t batchChunks = queueBytes / (8 * BLOCK_SIZE);
54+ constexpr int64_t MAX_BATCH_CHUNKS = 64;
55+ batchChunks = batchChunks > MAX_BATCH_CHUNKS ? MAX_BATCH_CHUNKS : batchChunks;
56+ for (int64_t first = 0; first < chunkNum_; first += batchChunks) {
57+ int64_t count = chunkNum_ - first;
58+ count = count > batchChunks ? batchChunks : count;
59+ LocalTensor<float> parts = predQue_.template AllocTensor<float>();
60+ DataCopy(parts, partialGm_[(row * chunkNum_ + first) * 8 * ROW_SLOT_ELEM],
61+ static_cast<uint32_t>(count * 8 * ROW_SLOT_ELEM));
62+ predQue_.template EnQue<float>(parts);
63+ parts = predQue_.template DeQue<float>();
64+ auto data = (__ubuf__ float*)parts.GetPhyAddr();
65+ for (int64_t offset = 0; offset < count; ++offset) {
66+ auto rowHi = data + offset * 8 * ROW_SLOT_ELEM;
67+ auto rowLo = rowHi + 4 * ROW_SLOT_ELEM;
68+ // Consume chunks in their original order using the existing pair merge.
69+ MergeRowSlots((__ubuf__ float*)acc.GetPhyAddr(), rowHi, rowLo, ROW_SLOT_ELEM, 0, 1);
70+ }
atomgit-bot
atomgit-botatomgit-bot8 days ago

🟡 Medium Priority

变更行:parallel.h:62-67 的合并循环 证据链:

  1. MergeRowSlots(softmax_focal_loss_grad_nd_vec.h:317-...) 的函数体整体是一个 __VEC_SCOPE__ 块,内部对 accBase 做 Gather→LoadAlign(accBase + slot*accStride_ + g)→精确两和→再写回同一 acc 槽(读-改-写同一片 UB)。
  2. 仓内其它所有 MergeRowSlots 调用点都先显式插 PipeBarrier<PIPE_V>():vec.h:311-312(注释明确写「行槽在上一个作用域里写入, 这里要读回: 同一矢量流水上的写后读必须显式设屏障, 否则是潜在竞态(偶发红)」)。本循环里连续多轮调用(每轮是独立的 __VEC_SCOPE__)对同一 acc 做写→读,却没有任何 PipeBarrier<PIPE_V>();同时循环前的 Duplicate(acc, 0.0f, 8*accStride_)(第 47 行)与首个 MergeRowSlots 的 LoadAlign 之间也没有屏障。
  3. 失败模式:跨 VEC_SCOPE 的 acc 写后读没有同步点,chunk 累加可能读到未落地的旧值,导致行标量(WF/WB/CE/W)偶发偏差 → grad 偶发精度不一致(非确定性结果),且难以复现定位。属于「新路径与同仓既有约定的分歧」+ 真实竞态风险。

建议:在 MergePartials 的逐 chunk 合并循环中,每次调用 MergeRowSlots 前补 PipeBarrier<PIPE_V>(),与 vec.h:311-312 既有做法保持一致;第 47 行 Duplicate(acc,...) 之后也应先屏障再读 acc。

Suggestions
70
+ for (int64_t offset = 0; offset < count; ++offset) {
71
+ auto rowHi = data + offset * 8 * ROW_SLOT_ELEM;
72
+ auto rowLo = rowHi + 4 * ROW_SLOT_ELEM;
73
+ // Consume chunks in their original order using the existing pair merge.
74
+ // acc is read-modified-written by each call; each call is its own __VEC_SCOPE__,
75
+ // so the write->read across scopes needs an explicit barrier (same as ExactRowSum4).
76
+ PipeBarrier<PIPE_V>();
77
+ MergeRowSlots((__ubuf__ float*)acc.GetPhyAddr(), rowHi, rowLo, ROW_SLOT_ELEM, 0, 1);
70
78
  }
Apply suggestion
likedislike
Incorrect?
zsq_888
zsq_888
7 days ago Comments:
likedislike
71+ predQue_.FreeTensor(parts);
72+ }
73+ if (sizeof(T) != sizeof(half) || r_ <= static_cast<int64_t>(VL_FP32)) {
74+ for (uint16_t slot = 0; slot < 4; ++slot) {
75+ Add(acc[slot * accStride_], acc[slot * accStride_], acc[(slot + 4) * accStride_], 1);
76+ }
77+ }
78+ PipeBarrier<PIPE_V>();
79+}
80+ 
81+template <typename T, typename TW, uint64_t hasWeight, uint64_t gammaMode>
82+__aicore__ inline void SoftmaxFocalLossGradND<T, TW, hasWeight, gammaMode>::ProcessSplitRow()
83+{
84+ int64_t block = static_cast<int64_t>(GetBlockIdx());
85+ int64_t coresPerRow = realCoreNum_ / a_;
86+ int64_t row = block / coresPerRow;
87+ int64_t rowCore = block % coresPerRow;
88+ int64_t baseChunks = chunkNum_ / coresPerRow;
89+ int64_t extraCores = chunkNum_ % coresPerRow;
90+ int64_t first = rowCore * baseChunks + (rowCore < extraCores ? rowCore : extraCores);
91+ int64_t count = baseChunks + (rowCore < extraCores ? 1 : 0);
92+ LocalTensor<float> acc = accBuf_.template Get<float>();
93+ LocalTensor<float> wf = wfBuf_.template Get<float>();
94+ LocalTensor<float> wd = wdBuf_.template Get<float>();
95+ LocalTensor<float> ce = ceBuf_.template Get<float>();
96+ LocalTensor<float> wt = wtBuf_.template Get<float>();
97+ for (int64_t chunk = first; chunk < first + count; ++chunk) {
98+ int64_t len = r_ - chunk * colsPerChunk_;
99+ len = len > colsPerChunk_ ? colsPerChunk_ : len;
100+ Duplicate(acc, 0.0f, static_cast<int32_t>(8 * accStride_));
101+ CopyIn(row, chunk * colsPerChunk_, 1, len);
102+ ComputeSums(1, len);
103+ ExactRowSum4((__ubuf__ float*)wf.GetPhyAddr(), (__ubuf__ float*)wd.GetPhyAddr(),
104+ (__ubuf__ float*)ce.GetPhyAddr(), (__ubuf__ float*)wt.GetPhyAddr(),
105+ (__ubuf__ float*)acc.GetPhyAddr(), 1, len);
106+ StorePartial(row * chunkNum_ + chunk);
107+ }
108+ // All launched cores publish disjoint chunks before any core reads the row sums.
109+ PipeBarrier<PIPE_MTE3>();
110+ SyncAll();
111+ MergePartials(row);
johan_lee
johan_leejohan_lee6 days ago

【review】 问题分类:性能 位置:loss/softmax_focal_loss_grad/op_kernel/arch35/softmax_focal_loss_grad_parallel.h:111,代码:MergePartials(row); 问题:ProcessSplitRow 将每行的 chunk 分给 coresPerRow 个核计算,但同步后每个核都调用 MergePartials(row)。该函数从第 0 块读到 chunkNum_,每个核都把该行全部 partial 回读到 UB 并按原顺序重新合并。 可能导致的结果:例如单行由 32 核处理且有 64 个 chunk 时,每个核回读 64×256 字节,合计 512 KiB 搬运,相同归约也执行 32 次。第一趟已由分核摊薄,这段全行串行合并仍落在每个核的第二趟关键路径上。 建议的修改方法:由每行一个核按既有顺序合并并发布最终行系数,其他核在同步后只读取该行系数;按同一长行用例比较节省的 partial 搬运与新增同步成本。

likedislike
112+ for (int64_t chunk = first; chunk < first + count; ++chunk) {
113+ int64_t len = r_ - chunk * colsPerChunk_;
114+ len = len > colsPerChunk_ ? colsPerChunk_ : len;
115+ CopyIn(row, chunk * colsPerChunk_, 1, len);
116+ ComputeGrad(1, len);
117+ CopyOut(row, chunk * colsPerChunk_, 1, len);
118+ }
119+}
120+ 
121+} // namespace SoftmaxFocalLossGrad
122+#endif
@@ -18,9 +18,10 @@
18#include "ascendc/host_api/tiling/template_argument.h"18#include "ascendc/host_api/tiling/template_argument.h"
19 19 
20// hasWeight: 0 = weight 缺省(按全 1 语义, A5 相对 A2 的功能补齐), 1 = weight 传入20// hasWeight: 0 = weight 缺省(按全 1 语义, A5 相对 A2 的功能补齐), 1 = weight 传入
21-#define SOFTMAX_FOCAL_LOSS_GRAD_TPL_KEY_DECL() \21+#define SOFTMAX_FOCAL_LOSS_GRAD_TPL_KEY_DECL() \
22- ASCENDC_TPL_UINT_DECL(hasWeight, ASCENDC_TPL_8_BW, ASCENDC_TPL_UI_LIST, 0, 1), \22+ ASCENDC_TPL_UINT_DECL(hasWeight, ASCENDC_TPL_8_BW, ASCENDC_TPL_UI_LIST, 0, 1), \
23- ASCENDC_TPL_UINT_DECL(gammaMode, ASCENDC_TPL_8_BW, ASCENDC_TPL_UI_LIST, 0, 1, 2)23+ ASCENDC_TPL_UINT_DECL(gammaMode, ASCENDC_TPL_8_BW, ASCENDC_TPL_UI_LIST, 0, 1, 2), \
24+ ASCENDC_TPL_UINT_DECL(splitRow, ASCENDC_TPL_8_BW, ASCENDC_TPL_UI_LIST, 0, 1)
24 25 
25#define SOFTMAX_FOCAL_LOSS_GRAD_TPL_KEY_SEL() \26#define SOFTMAX_FOCAL_LOSS_GRAD_TPL_KEY_SEL() \
26 ASCENDC_TPL_UINT_SEL(hasWeight, ASCENDC_TPL_UI_LIST, 0, 1), \27 ASCENDC_TPL_UINT_SEL(hasWeight, ASCENDC_TPL_UI_LIST, 0, 1), \
@@ -28,5 +29,10 @@
28 29 
29ASCENDC_TPL_ARGS_DECL(SoftmaxFocalLossGrad, SOFTMAX_FOCAL_LOSS_GRAD_TPL_KEY_DECL());30ASCENDC_TPL_ARGS_DECL(SoftmaxFocalLossGrad, SOFTMAX_FOCAL_LOSS_GRAD_TPL_KEY_DECL());
30 31 
31-ASCENDC_TPL_SEL(ASCENDC_TPL_ARGS_SEL(SOFTMAX_FOCAL_LOSS_GRAD_TPL_KEY_SEL()));32+ASCENDC_TPL_SEL(ASCENDC_TPL_ARGS_SEL(ASCENDC_TPL_KERNEL_TYPE_SEL(ASCENDC_TPL_AIV_ONLY),
33+ SOFTMAX_FOCAL_LOSS_GRAD_TPL_KEY_SEL(),
34+ ASCENDC_TPL_UINT_SEL(splitRow, ASCENDC_TPL_UI_LIST, 0)),
35+ ASCENDC_TPL_ARGS_SEL(ASCENDC_TPL_KERNEL_TYPE_SEL(ASCENDC_TPL_MIX_AIV_1_0),
36+ SOFTMAX_FOCAL_LOSS_GRAD_TPL_KEY_SEL(),
37+ ASCENDC_TPL_UINT_SEL(splitRow, ASCENDC_TPL_UI_LIST, 1)));
32#endif // SOFTMAX_FOCAL_LOSS_GRAD_TILING_KEY_H38#endif // SOFTMAX_FOCAL_LOSS_GRAD_TILING_KEY_H
@@ -14,7 +14,7 @@
14 */14 */
15#include "kernel_tiling/kernel_tiling.h"15#include "kernel_tiling/kernel_tiling.h"
16#include "kernel_operator.h"16#include "kernel_operator.h"
17-#include "arch35/softmax_focal_loss_grad_nd_vec.h"17+#include "arch35/softmax_focal_loss_grad_parallel.h"
18#include "arch35/softmax_focal_loss_grad_tiling_data.h"18#include "arch35/softmax_focal_loss_grad_tiling_data.h"
19#include "arch35/softmax_focal_loss_grad_tiling_key.h"19#include "arch35/softmax_focal_loss_grad_tiling_key.h"
20 20 
@@ -26,7 +26,7 @@ using namespace AscendC;
26#define DTYPE_WEIGHT DTYPE_PRED26#define DTYPE_WEIGHT DTYPE_PRED
27#endif27#endif
28 28 
29-template <uint64_t hasWeight, uint64_t gammaMode>29+template <uint64_t hasWeight, uint64_t gammaMode, uint64_t splitRow>
30__global__ __aicore__ void softmax_focal_loss_grad(GM_ADDR pred, GM_ADDR target, GM_ADDR dout, GM_ADDR weight,30__global__ __aicore__ void softmax_focal_loss_grad(GM_ADDR pred, GM_ADDR target, GM_ADDR dout, GM_ADDR weight,
31 GM_ADDR grad, GM_ADDR workspace, GM_ADDR tiling)31 GM_ADDR grad, GM_ADDR workspace, GM_ADDR tiling)
32{32{
@@ -35,6 +35,10 @@ __global__ __aicore__ void softmax_focal_loss_grad(GM_ADDR pred, GM_ADDR target,
35 35 
36 TPipe pipe;36 TPipe pipe;
37 SoftmaxFocalLossGrad::SoftmaxFocalLossGradND<DTYPE_PRED, DTYPE_WEIGHT, hasWeight, gammaMode> op;37 SoftmaxFocalLossGrad::SoftmaxFocalLossGradND<DTYPE_PRED, DTYPE_WEIGHT, hasWeight, gammaMode> op;
38- op.Init(pred, target, dout, weight, grad, &tilingData, &pipe);38+ GM_ADDR userWorkspace = nullptr;
39- op.Process();39+ if constexpr (splitRow == 1) {
40+ userWorkspace = GetUserWorkspace(workspace);
41+ }
42+ op.Init(pred, target, dout, weight, grad, &tilingData, &pipe, userWorkspace);
43+ op.template Process<(splitRow == 1)>();
40}44}