| @@ -116,6 +116,13 @@ private: |
| const uint64_t baseDo, const uint64_t baseHo, const uint64_t baseWo, const uint64_t baseDi, | const uint64_t baseDo, const uint64_t baseHo, const uint64_t baseWo, const uint64_t baseDi, |
| const uint64_t baseHi, const uint64_t baseWi, const ge::DataType& dtype); | const uint64_t baseHi, const uint64_t baseWi, const ge::DataType& dtype); |
| | |
| + ge::graphStatus InitBaseParams(gert::Shape& gradShape, ge::DataType& dtype); |
| + void CalcShapeVars(const gert::Shape& gradShape, int64_t& ncShape, int64_t& ndhwShape, int64_t& cShape); |
| + void SetWorkspace(ge::DataType dtype, int64_t ncShape); |
| + void CalcCoreNumAndDetermineFlag(const AvgPool3DGradCubeCompileInfo* compileInfo, |
| + int64_t ncShape, int64_t ndhwShape, int64_t gradDim0); |
| + void DispatchTilingStrategy(uint64_t ubSizePlatform, ge::DataType dtype, |
| + int64_t cShape, int64_t ndhwShape); |
| private: | private: |
| gert::TilingContext* tilingContext_ = nullptr; | gert::TilingContext* tilingContext_ = nullptr; |
| AvgPool3dGradTilingParam tilingData_; | AvgPool3dGradTilingParam tilingData_; |
| @@ -542,16 +549,53 @@ ge::graphStatus AvgPool3dGradTiling::Init() |
| { | { |
| OP_LOGD(tilingContext_->GetNodeName(), "Tiling initing"); | OP_LOGD(tilingContext_->GetNodeName(), "Tiling initing"); |
| auto compileInfo = static_cast<const AvgPool3DGradCubeCompileInfo*>(tilingContext_->GetCompileInfo()); | auto compileInfo = static_cast<const AvgPool3DGradCubeCompileInfo*>(tilingContext_->GetCompileInfo()); |
| - if (compileInfo == nullptr) { | + OP_CHECK_IF(compileInfo == nullptr, |
| - OP_LOGE(tilingContext_->GetNodeName(), "compile info is nullptr"); | + OP_LOGE(tilingContext_->GetNodeName(), "compile info is nullptr"), |
| - return ge::GRAPH_FAILED; | + return ge::GRAPH_FAILED); |
| + |
| + gert::Shape gradShape; |
| + ge::DataType dtype; |
| + OP_CHECK_IF(InitBaseParams(gradShape, dtype) != ge::GRAPH_SUCCESS, |
| + OP_LOGE(tilingContext_->GetNodeName(), "InitBaseParams failed"), |
| + return ge::GRAPH_FAILED); |
| + |
| + int64_t ndhwShape = 1; |
| + int64_t ncShape = 1; |
| + int64_t cShape = 1; |
| + CalcShapeVars(gradShape, ncShape, ndhwShape, cShape); |
| + |
| + auto ret = InitDHW(); |
| + if (ret != ge::GRAPH_SUCCESS) { |
| + return ret; |
| } | } |
| - auto gradShape = tilingContext_->GetInputShape(1)->GetStorageShape(); | + |
| - auto dtype = tilingContext_->GetInputDesc(1)->GetDataType(); | + SetWorkspace(dtype, ncShape); |
| + CalcCoreNumAndDetermineFlag(compileInfo, ncShape, ndhwShape, gradShape.GetDim(0)); |
| + |
| + OP_CHECK_IF(coreNum_ == 0UL, |
| + OP_LOGE(tilingContext_->GetNodeName(), "CoreNum is zero, error."), |
| + return ge::GRAPH_FAILED); |
| + uint64_t ubSizePlatform = compileInfo->ub_size; |
| + DispatchTilingStrategy(ubSizePlatform, dtype, cShape, ndhwShape); |
| + if (dtype == ge::DT_BF16 || dtype == ge::DT_FLOAT16) { |
| + int64_t inDhwShape = static_cast<int64_t>(inDHW_.d * inDHW_.h * inDHW_.w); |
| + Tiling4CastCopyOut(static_cast<int64_t>(ubSizePlatform), ncShape, inDhwShape); |
| + } |
| + |
| + SetTilingKey(dtype); |
| + return ge::GRAPH_SUCCESS; |
| +} |
| + |
| +ge::graphStatus AvgPool3dGradTiling::InitBaseParams(gert::Shape& gradShape, ge::DataType& dtype) |
| +{ |
| + gradShape = tilingContext_->GetInputShape(1)->GetStorageShape(); |
| + dtype = tilingContext_->GetInputDesc(1)->GetDataType(); |
| auto attrs = tilingContext_->GetAttrs(); | auto attrs = tilingContext_->GetAttrs(); |
| + |
| countIncludePad_ = static_cast<uint64_t>(*attrs->GetAttrPointer<bool>(COUNT_IDX)); | countIncludePad_ = static_cast<uint64_t>(*attrs->GetAttrPointer<bool>(COUNT_IDX)); |
| divisorOverride_ = static_cast<int64_t>(*attrs->GetAttrPointer<int>(DIVISOR_IDX)); | divisorOverride_ = static_cast<int64_t>(*attrs->GetAttrPointer<int>(DIVISOR_IDX)); |
| dataFormat_ = attrs->GetStr(FORMAT_IDX); | dataFormat_ = attrs->GetStr(FORMAT_IDX); |
| + |
| if (dataFormat_ != "NDHWC" && dataFormat_ != "NCDHW") { | if (dataFormat_ != "NDHWC" && dataFormat_ != "NCDHW") { |
| OP_LOGE(tilingContext_->GetNodeName(), "invalid data_format, should be NCDHW or NDHWC"); | OP_LOGE(tilingContext_->GetNodeName(), "invalid data_format, should be NCDHW or NDHWC"); |
| return ge::GRAPH_FAILED; | return ge::GRAPH_FAILED; |
| @@ -561,76 +605,87 @@ ge::graphStatus AvgPool3dGradTiling::Init() |
| OP_LOGE(tilingContext_->GetNodeName(), "gradShape dim num is not 5"); | OP_LOGE(tilingContext_->GetNodeName(), "gradShape dim num is not 5"); |
| return ge::GRAPH_FAILED; | return ge::GRAPH_FAILED; |
| } | } |
| + return ge::GRAPH_SUCCESS; |
| +} |
| | |
| - int64_t ndhwShape = 1; | +void AvgPool3dGradTiling::CalcShapeVars(const gert::Shape& gradShape, int64_t& ncShape, int64_t& ndhwShape, int64_t& cShape) |
| - int64_t ncShape = 1; | +{ |
| - int64_t cShape = 1; | |
| if (dataFormat_ == "NDHWC") { | if (dataFormat_ == "NDHWC") { |
| cShape = gradShape.GetDim(GRAD_SHAPE - 1); | cShape = gradShape.GetDim(GRAD_SHAPE - 1); |
| + ndhwShape = 1; |
| for (int i = NDHWC_N_DIM; i < NDHWC_D_DIM + ATTR_SIZE; i++) { | for (int i = NDHWC_N_DIM; i < NDHWC_D_DIM + ATTR_SIZE; i++) { |
| ndhwShape *= gradShape.GetDim(i); | ndhwShape *= gradShape.GetDim(i); |
| } | } |
| ncShape = gradShape.GetDim(0) * cShape; | ncShape = gradShape.GetDim(0) * cShape; |
| } else { | } else { |
| + |
| N_ = gradShape.GetDim(0); | N_ = gradShape.GetDim(0); |
| C_ = gradShape.GetDim(1); | C_ = gradShape.GetDim(1); |
| ncShape = static_cast<int64_t>(N_ * C_); | ncShape = static_cast<int64_t>(N_ * C_); |
| } | } |
| +} |
| | |
| - auto ret = InitDHW(); | +void AvgPool3dGradTiling::SetWorkspace(ge::DataType dtype, int64_t ncShape) |
| - if (ret != ge::GRAPH_SUCCESS) { | +{ |
| - return ret; | |
| - } | |
| size_t sysWorkspaceSize = 16UL * 1024UL * 1024UL; | size_t sysWorkspaceSize = 16UL * 1024UL * 1024UL; |
| size_t castWorkspaceSize = 0; | size_t castWorkspaceSize = 0; |
| - size_t* currentWorkSpace = tilingContext_->GetWorkspaceSizes(1); | + |
| if (dtype == ge::DT_BF16 || dtype == ge::DT_FLOAT16) { | if (dtype == ge::DT_BF16 || dtype == ge::DT_FLOAT16) { |
| - castWorkspaceSize = static_cast<size_t>(inDHW_.d) * static_cast<size_t>(inDHW_.h) * | + size_t totalElements = static_cast<size_t>(inDHW_.d) * static_cast<size_t>(inDHW_.h) * |
| - static_cast<size_t>(inDHW_.w) * static_cast<size_t>(ncShape) * sizeof(float); | + static_cast<size_t>(inDHW_.w) * static_cast<size_t>(ncShape); |
| + castWorkspaceSize = totalElements * sizeof(float); |
| } | } |
| - sysWorkspaceSize += castWorkspaceSize; | + |
| - currentWorkSpace[0] = sysWorkspaceSize; | + size_t* currentWorkSpace = tilingContext_->GetWorkspaceSizes(1); |
| - // compute corenum + normalCoreNCNum + lastCoreNCNum | + currentWorkSpace[0] = sysWorkspaceSize + castWorkspaceSize; |
| +} |
| + |
| +void AvgPool3dGradTiling::CalcCoreNumAndDetermineFlag(const AvgPool3DGradCubeCompileInfo* compileInfo, |
| + int64_t ncShape, int64_t ndhwShape, int64_t gradDim0) |
| +{ |
| if (dataFormat_ == "NDHWC") { | if (dataFormat_ == "NDHWC") { |
| coreNum_ = std::min(compileInfo->core_num, static_cast<uint32_t>(ndhwShape)); | coreNum_ = std::min(compileInfo->core_num, static_cast<uint32_t>(ndhwShape)); |
| } else { | } else { |
| - if (isOnlyT_ != static_cast<uint64_t>(0)) { | + // NCDHW |
| - coreNum_ = std::min(compileInfo->core_num, static_cast<uint32_t>(ncShape * outDHW_.d)); | + int64_t taskCount = (isOnlyT_ != 0) ? (ncShape * outDHW_.d) : (ncShape * outDHW_.d * outDHW_.h * outDHW_.w); |
| - } else { | + coreNum_ = std::min(compileInfo->core_num, static_cast<uint32_t>(taskCount)); |
| - coreNum_ = | |
| - std::min(compileInfo->core_num, static_cast<uint32_t>(ncShape * outDHW_.d * outDHW_.h * outDHW_.w)); | |
| - } | |
| - } | |
| - if (dataFormat_ == "NCDHW" && isOnlyT_ == static_cast<uint64_t>(0)) { | |
| - isDetermine_ = 1UL; | |
| - } else if (tilingContext_->GetDeterministic() == 1) { | |
| - coreNum_ = 1UL; | |
| - isDetermine_ = 1UL; | |
| - } | |
| - if (coreNum_ == 0UL) { | |
| - OP_LOGE(tilingContext_->GetNodeName(), "coreNum is zero, error."); | |
| - return ge::GRAPH_FAILED; | |
| } | } |
| | |
| - // tiling for HW or C | + bool isDeterministic = (tilingContext_->GetDeterministic() == 1); |
| - uint64_t ubSizePlatform = compileInfo->ub_size; | + if (!isDeterministic || isOverlap_ != 1) { |
| - if ((isOnlyT_ == static_cast<uint64_t>(0)) && dataFormat_ == "NCDHW") { | + return; |
| - Tiling4Block(ubSizePlatform, dtype); | + } |
| - } else if ((isOnlyT_ != static_cast<uint64_t>(0)) && dataFormat_ == "NCDHW") { | + |
| - Tiling4HWParam(ubSizePlatform, dtype); | + // 进入确定性计算逻辑 |
| + if (dataFormat_ == "NCDHW" && isOnlyT_ == 0) { |
| + isDetermine_ = 1UL; |
| } else { | } else { |
| + |
| + if (isOnlyT_ != 0) { |
| + coreNum_ = std::min(compileInfo->core_num, static_cast<uint32_t>(ncShape)); |
| + } else { |
| + coreNum_ = std::min(compileInfo->core_num, static_cast<uint32_t>(gradDim0)); |
| + } |
| + isDetermine_ = 1UL; |
| + } |
| +} |
| + |
| +void AvgPool3dGradTiling::DispatchTilingStrategy(uint64_t ubSizePlatform, ge::DataType dtype, |
| + int64_t cShape, int64_t ndhwShape) |
| +{ |
| + if (dataFormat_ == "NCDHW") { |
| + if (isOnlyT_ == 0) { |
| + Tiling4Block(ubSizePlatform, dtype); |
| + } else { |
| + Tiling4HWParam(ubSizePlatform, dtype); |
| + } |
| + } else { |
| + |
| Tiling4CParam(ubSizePlatform, cShape, ndhwShape, dtype); | Tiling4CParam(ubSizePlatform, cShape, ndhwShape, dtype); |
| } | } |
| - | |
| - int64_t inDhwShape = static_cast<int64_t>(inDHW_.d * inDHW_.h * inDHW_.w); | |
| - if (dtype == ge::DT_BF16 || dtype == ge::DT_FLOAT16) { | |
| - Tiling4CastCopyOut(static_cast<int64_t>(ubSizePlatform), ncShape, inDhwShape); | |
| - } | |
| - | |
| - SetTilingKey(dtype); | |
| - return ge::GRAPH_SUCCESS; | |
| } | } |
| | |
| + |
| ge::graphStatus AvgPool3dGradTiling::SetKernelTiling() | ge::graphStatus AvgPool3dGradTiling::SetKernelTiling() |
| { | { |
| tilingContext_->SetBlockDim(coreNum_); | tilingContext_->SetBlockDim(coreNum_); |
| |
代码结构与可维护性: 函数TransGrad2CDHW中新增的确定性逻辑(第319-333行)与原始功能(reshape梯度tensor)耦合在一起,降低了代码的可读性和可维护性。该逻辑根据系统确定性配置和核心数调整reshape维度,属于性能优化策略,应单独提取为辅助函数。
问题类型: 代码结构与可维护性 文件路径:
pooling/avg_pool3_d_grad/op_host/op_api/aclnn_avgpool3d_backward.cpp行号: 319 问题代码:uint32_t coreNum = GetCurrentPlatformInfo().GetVectorCoreNum(); int64_t usedCoreNum = 1; int64_t deterministicValue = 0; rtError_t retRts = rtCtxGetSysParamOpt(SYS_OPT_DETERMINISTIC, &deterministicValue); if (retRts != RT_ERROR_NONE) { deterministicValue = 0; } if (deterministicValue != 0) { for (size_t i = coreNum;i >= 1;i--) { if (mergeNC % i == 0) { usedCoreNum = i; break; } } }修改建议:
此评论由代码审查工具自动生成