| @@ -55,7 +55,6 @@ constexpr int64_t ROW_SLOT_ELEM = UB_BLOCK_BYTES / DTYPE_LEN_FP32; | |||
| 55 | constexpr int64_t ACC_BUF_NUM = 8; // 4 个行标量 + 4 个低位残差 | 55 | constexpr int64_t ACC_BUF_NUM = 8; // 4 个行标量 + 4 个低位残差 |
| 56 | constexpr int64_t ACC_ALIGN_ELEM = 8; | 56 | constexpr int64_t ACC_ALIGN_ELEM = 8; |
| 57 | constexpr uint64_t SIMD_SIMT_DCACHE_SIZE = static_cast<uint64_t>(32 * 1024); | 57 | constexpr 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); | ||
| 59 | constexpr int32_t TILING_ITER = 2; | 58 | constexpr 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 | + } | ||
| 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 | ||
| 54 | private: | 56 | private: |
| 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 | ||
| 185 | private: | 190 | private: |
| 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: | |||
| 220 | template <typename T, typename TW, uint64_t hasWeight, uint64_t gammaMode> | 226 | template <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 | ||
| 301 | template <typename T, typename TW, uint64_t hasWeight, uint64_t gammaMode> | 310 | template <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>(); | ||||||||||||||||||||
🟡 Medium Priority 变更行:parallel.h:22 证据链:
触发条件: 建议:按 ExactPath() 选择源缓冲与跨步:source = ExactPath() ? dfBuf_ : accBuf_,slot 跨步 = ExactPath() ? VL_FP32*ROW_SLOT_ELEM : accStride_;并在 tiling 的 splitRow 判据中加入与 kernel 相同的 ExactPath() 条件。 ![]() ![]() Incorrect? | |||||||||||||||||||||
| 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 | + } | ||||||||||||||||||||
🟡 Medium Priority 变更行:parallel.h:62-67 的合并循环 证据链:
建议:在 MergePartials 的逐 chunk 合并循环中,每次调用 MergeRowSlots 前补 PipeBarrier<PIPE_V>(),与 vec.h:311-312 既有做法保持一致;第 47 行 Duplicate(acc,...) 之后也应先屏障再读 acc。 Suggestions
![]() ![]() Incorrect? | |||||||||||||||||||||
| 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); | ||||||||||||||||||||
【review】
问题分类:性能
位置:loss/softmax_focal_loss_grad/op_kernel/arch35/softmax_focal_loss_grad_parallel.h:111,代码:MergePartials(row);
问题: ![]() ![]() | |||||||||||||||||||||
| 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 | ||
| 29 | ASCENDC_TPL_ARGS_DECL(SoftmaxFocalLossGrad, SOFTMAX_FOCAL_LOSS_GRAD_TPL_KEY_DECL()); | 30 | ASCENDC_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_H | 38 | #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_PRED | 26 | #define DTYPE_WEIGHT DTYPE_PRED |
| 27 | #endif | 27 | #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 | } |


🟡 Medium Priority
变更行:tiling_arch35.cpp:383-387 workspaces[0] = WORKSPACE_SIZE; // 16 MiB 常量 if (splitRow) { workspaces[0] += a * chunkNum * 8 * UB_BLOCK_BYTES; // 用户区被追加在 16MiB 之后 } 证据链:
GetUserWorkspace(workspace)作为 partial 区基址(softmax_focal_loss_grad.cpp:40),该函数返回workspace + 平台系统区大小(GetLibApiWorkSpaceSize()),而不是固定偏移 16 MiB。a*chunkNum*8*UB_BLOCK_BYTES(与 kernel 的partialGm_[chunk*8*ROW_SLOT_ELEM]一一对应,已核对一致)。可用用户区实际是workspaces[0] - 系统区大小 = 16MiB + 用户需求 - 系统区大小。GetLibApiWorkSpaceSize()> 16 MiB,用户区就被系统区吃掉(系统区大小 - 16MiB)字节,kernel 的DataCopy(partialGm_[...])会写到 workspace 分配范围之外,踩坏其后的 GM(其他 tensor / 下一个算子的 workspace),表现为偶发精度错或 AIC Error。反之若系统区 < 16 MiB 则只是浪费显存,无害,所以问题被掩盖。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()。