已合并
modify offsetElemCount logic & clang format update #3939
renshuojian创建于 7月9日
modify offsetElemCount logic & clang format update #3939
已合并
共 2 个文件变更+43-36
Mrandom/stateless_drop_out_gen_mask/op_host/arch35/stateless_drop_out_gen_mask_tiling_arch35.cpp+21-17
| @@ -3,7 +3,7 @@ | |||
| 3 | * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | 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"). | 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. | 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, | 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. | 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. | 8 | * See LICENSE in the root of the software repository for the full text of the License. |
| 9 | */ | 9 | */ |
| @@ -15,7 +15,7 @@ | |||
| 15 | 15 | ||
| 16 | 16 | ||
| 17 | 17 | ||
| 18 | #include "../../../random_common/op_host/arch35/random_tiling_base.h" | 18 | #include "../../../random_common/op_host/arch35/random_tiling_base.h" |
| 19 | 19 | ||
| 20 | 20 | ||
| 21 | 21 | ||
| @@ -35,7 +35,7 @@ static constexpr int64_t IN_OFFSET_IDX = 4; | |||
| 35 | static constexpr int64_t OUT_Y_IDX = 0; | 35 | static constexpr int64_t OUT_Y_IDX = 0; |
| 36 | static constexpr uint64_t BUFFER_NUM = 2; | 36 | static constexpr uint64_t BUFFER_NUM = 2; |
| 37 | static constexpr uint64_t EXIST_NODE_NUM = 3; | 37 | static constexpr uint64_t EXIST_NODE_NUM = 3; |
| 38 | static constexpr uint64_t CORE_ALIGN_SIZE =256; | 38 | static constexpr uint64_t CORE_ALIGN_SIZE = 256; |
| 39 | static constexpr uint64_t UB_ALIGN_SIZE = 256; | 39 | static constexpr uint64_t UB_ALIGN_SIZE = 256; |
| 40 | static constexpr uint64_t REGBASE_CCEC_CACHE_SIZE = 8 * 1024; | 40 | static constexpr uint64_t REGBASE_CCEC_CACHE_SIZE = 8 * 1024; |
| 41 | static constexpr uint32_t RIGHT_SHIFT_NUM = 32; | 41 | static constexpr uint32_t RIGHT_SHIFT_NUM = 32; |
| @@ -48,24 +48,22 @@ OpTilingConfig StatelessDropOutGenMaskTiling::BuildOpConfig() | |||
| 48 | OpTilingConfig config; | 48 | OpTilingConfig config; |
| 49 | config.inputCheckRules = { | 49 | config.inputCheckRules = { |
| 50 | // 输入索引: dtype列表,shapeSize,dim_num | 50 | // 输入索引: dtype列表,shapeSize,dim_num |
| 51 | {0, {{ge::DT_INT32, ge::DT_INT64}, -1, {1}, nullptr}}, // shape | 51 | {0, {{ge::DT_INT32, ge::DT_INT64}, -1, {1}, nullptr}}, // shape |
| 52 | {1, {{ge::DT_FLOAT16, ge::DT_FLOAT, ge::DT_BF16}, 1, {}, nullptr}}, // prob | 52 | {1, {{ge::DT_FLOAT16, ge::DT_FLOAT, ge::DT_BF16}, 1, {}, nullptr}}, // prob |
| 53 | {2, {{ge::DT_INT32, ge::DT_INT64}, 1, {}, nullptr}}, // seed | 53 | {2, {{ge::DT_INT32, ge::DT_INT64}, 1, {}, nullptr}}, // seed |
| 54 | {3, {{ge::DT_INT32, ge::DT_INT64}, 1, {}, nullptr}}, // seed1 | 54 | {3, {{ge::DT_INT32, ge::DT_INT64}, 1, {}, nullptr}}, // seed1 |
| 55 | }; | 55 | }; |
| 56 | 56 | ||
| 57 | config.optionalInputCheckRules = { | 57 | config.optionalInputCheckRules = { |
| 58 | {4, {{ge::DT_INT64}, -1, {}, nullptr}}, // offset (可选输入) | 58 | {4, {{ge::DT_INT64}, -1, {}, nullptr}}, // offset (可选输入) |
| 59 | }; | 59 | }; |
| 60 | 60 | ||
| 61 | config.outputCheckRules = { | 61 | config.outputCheckRules = {// 输出索引: dtype列表,shapeSize,dim_num |
| 62 | // 输出索引: dtype列表,shapeSize,dim_num | 62 | {0, {{ge::DT_UINT8}, -1, {1, 2, 3, 4, 5, 6, 7, 8}, nullptr}}}; // y |
| 63 | {0, {{ge::DT_UINT8}, -1, {1,2,3,4,5,6,7,8}, nullptr}} | ||
| 64 | }; // y | ||
| 65 | 63 | ||
| 66 | // 获取output_size:输入0(shape)的shapeSize | 64 | // 获取output_size:输入0(shape)的shapeSize |
| 67 | config.getOutputSize = [](gert::TilingContext* ctx, int64_t& shapeSize) -> ge::graphStatus { | 65 | config.getOutputSize = [](gert::TilingContext* ctx, int64_t& shapeSize) -> ge::graphStatus { |
| 68 | //获取Input ShapeSize | 66 | // 获取Input ShapeSize |
| 69 | gert::Shape constShape; | 67 | gert::Shape constShape; |
| 70 | auto ret = ExtractTensorValue(ctx, 0, constShape); | 68 | auto ret = ExtractTensorValue(ctx, 0, constShape); |
| 71 | if (ret != ge::GRAPH_SUCCESS) { | 69 | if (ret != ge::GRAPH_SUCCESS) { |
| @@ -80,7 +78,8 @@ OpTilingConfig StatelessDropOutGenMaskTiling::BuildOpConfig() | |||
| 80 | if (shapeSize == 0) { | 78 | if (shapeSize == 0) { |
| 81 | std::string valueStr = std::to_string(shapeSize); | 79 | std::string valueStr = std::to_string(shapeSize); |
| 82 | std::string reasonMsg = "input shape should not be empty tensor"; | 80 | std::string reasonMsg = "input shape should not be empty tensor"; |
| 83 | OP_LOGE_FOR_INVALID_SHAPESIZE_WITH_REASON(ctx->GetNodeName(), "input shape", valueStr.c_str(), reasonMsg.c_str()); | 81 | OP_LOGE_FOR_INVALID_SHAPESIZE_WITH_REASON(ctx->GetNodeName(), "input shape", valueStr.c_str(), |
| 82 | reasonMsg.c_str()); | ||
| 84 | return ge::GRAPH_FAILED; | 83 | return ge::GRAPH_FAILED; |
| 85 | } | 84 | } |
| 86 | 85 | ||
| @@ -91,11 +90,14 @@ OpTilingConfig StatelessDropOutGenMaskTiling::BuildOpConfig() | |||
| 91 | // 通过 key[] 传递元数据给 kernel:key[0]=offsetElemCount, key[1]=seedByteSize | 90 | // 通过 key[] 传递元数据给 kernel:key[0]=offsetElemCount, key[1]=seedByteSize |
| 92 | config.getKeyAndCounter = []([[maybe_unused]] gert::TilingContext* ctx, uint32_t key[2], | 91 | config.getKeyAndCounter = []([[maybe_unused]] gert::TilingContext* ctx, uint32_t key[2], |
| 93 | uint32_t counter[4]) -> ge::graphStatus { | 92 | uint32_t counter[4]) -> ge::graphStatus { |
| 94 | counter[0] = 0; counter[1] = 0; counter[2] = 0; counter[3] = 0; | 93 | counter[0] = 0; |
| 94 | counter[1] = 0; | ||
| 95 | counter[2] = 0; | ||
| 96 | counter[3] = 0; | ||
| 95 | 97 | ||
| 96 | // offset element count (shape info, no D2H needed) | 98 | // offset element count (shape info, no D2H needed) |
| 97 | auto offsetTensor = ctx->GetOptionalInputTensor(IN_OFFSET_IDX); | 99 | auto offsetTensor = ctx->GetOptionalInputTensor(IN_OFFSET_IDX); |
| 98 | uint32_t offsetElemCount = 2; | 100 | uint32_t offsetElemCount = 0; |
| 99 | if (offsetTensor != nullptr) { | 101 | if (offsetTensor != nullptr) { |
| 100 | offsetElemCount = static_cast<uint32_t>(offsetTensor->GetShapeSize()); | 102 | offsetElemCount = static_cast<uint32_t>(offsetTensor->GetShapeSize()); |
| 101 | } | 103 | } |
| @@ -113,7 +115,7 @@ OpTilingConfig StatelessDropOutGenMaskTiling::BuildOpConfig() | |||
| 113 | return ge::GRAPH_SUCCESS; | 115 | return ge::GRAPH_SUCCESS; |
| 114 | }; | 116 | }; |
| 115 | 117 | ||
| 116 | config.ubAlignSize = UB_ALIGN_SIZE; | 118 | config.ubAlignSize = UB_ALIGN_SIZE; |
| 117 | 119 | ||
| 118 | config.getBufferNum = [](gert::TilingContext* ctx, int64_t& bufNum) -> ge::graphStatus { | 120 | config.getBufferNum = [](gert::TilingContext* ctx, int64_t& bufNum) -> ge::graphStatus { |
| 119 | auto outDesc = ctx->GetOutputDesc(0); | 121 | auto outDesc = ctx->GetOutputDesc(0); |
| @@ -127,7 +129,9 @@ OpTilingConfig StatelessDropOutGenMaskTiling::BuildOpConfig() | |||
| 127 | return config; | 129 | return config; |
| 128 | } | 130 | } |
| 129 | 131 | ||
| 130 | StatelessDropOutGenMaskTiling::StatelessDropOutGenMaskTiling(gert::TilingContext* context) : RandomTilingArch35(context, BuildOpConfig()){} | 132 | StatelessDropOutGenMaskTiling::StatelessDropOutGenMaskTiling(gert::TilingContext* context) |
| 133 | : RandomTilingArch35(context, BuildOpConfig()) | ||
| 134 | {} | ||
| 131 | 135 | ||
| 132 | static ge::graphStatus TilingPrepare4StatelessDropOutGenMaskTiling(gert::TilingParseContext* context) | 136 | static ge::graphStatus TilingPrepare4StatelessDropOutGenMaskTiling(gert::TilingParseContext* context) |
| 133 | { | 137 | { |
| @@ -144,5 +148,5 @@ static ge::graphStatus TilingStatelessDropOutGenMask(gert::TilingContext* tiling | |||
| 144 | IMPL_OP_OPTILING(StatelessDropOutGenMask) | 148 | IMPL_OP_OPTILING(StatelessDropOutGenMask) |
| 145 | .Tiling(TilingStatelessDropOutGenMask) | 149 | .Tiling(TilingStatelessDropOutGenMask) |
| 146 | .TilingParse<RandomOperatorCompileInfo>(TilingPrepare4StatelessDropOutGenMaskTiling) | 150 | .TilingParse<RandomOperatorCompileInfo>(TilingPrepare4StatelessDropOutGenMaskTiling) |
| 147 | .TilingInputsDataDependency({ IN_SHAPE_IDX, IN_PROB_IDX }); | 151 | .TilingInputsDataDependency({IN_SHAPE_IDX, IN_PROB_IDX}); |
| 148 | } // namespace optiling | 152 | } // namespace optiling |
| @@ -34,9 +34,10 @@ constexpr static uint32_t RoundUpByte256 = 256; | |||
| 34 | constexpr static int64_t BUFFER_NUM = 2; | 34 | constexpr static int64_t BUFFER_NUM = 2; |
| 35 | 35 | ||
| 36 | template <typename T> | 36 | template <typename T> |
| 37 | class StatelessDropOutGenMaskPt : public RandomKernelBaseOp{ | 37 | class StatelessDropOutGenMaskPt : public RandomKernelBaseOp { |
| 38 | public: | 38 | public: |
| 39 | __aicore__ inline StatelessDropOutGenMaskPt(TPipe* pipe, const RandomUnifiedTilingDataStruct* __restrict tilingData) : RandomKernelBaseOp(tilingData),pipe_(pipe){}; | 39 | __aicore__ inline StatelessDropOutGenMaskPt(TPipe* pipe, const RandomUnifiedTilingDataStruct* __restrict tilingData) |
| 40 | : RandomKernelBaseOp(tilingData), pipe_(pipe){}; | ||
| 40 | __aicore__ inline void Init(GM_ADDR shape, GM_ADDR prob, GM_ADDR seed, GM_ADDR offset, GM_ADDR y); | 41 | __aicore__ inline void Init(GM_ADDR shape, GM_ADDR prob, GM_ADDR seed, GM_ADDR offset, GM_ADDR y); |
| 41 | __aicore__ inline void Process(); | 42 | __aicore__ inline void Process(); |
| 42 | 43 | ||
| @@ -56,7 +57,7 @@ private: | |||
| 56 | TPipe* pipe_; | 57 | TPipe* pipe_; |
| 57 | GlobalTensor<T> probInputGm_; | 58 | GlobalTensor<T> probInputGm_; |
| 58 | GlobalTensor<uint8_t> outputGm_; | 59 | GlobalTensor<uint8_t> outputGm_; |
| 59 | GlobalTensor<uint64_t> counterGm_; // offset tensor 的 GM 访问句柄 | 60 | GlobalTensor<uint64_t> counterGm_; // offset tensor 的 GM 访问句柄 |
| 60 | 61 | ||
| 61 | TQue<QuePosition::VECOUT, BUFFER_NUM> outQueY_; | 62 | TQue<QuePosition::VECOUT, BUFFER_NUM> outQueY_; |
| 62 | TBuf<QuePosition::VECCALC> philoxQueBuf_; | 63 | TBuf<QuePosition::VECCALC> philoxQueBuf_; |
| @@ -68,14 +69,14 @@ private: | |||
| 68 | uint32_t blockOffset_ = 0; | 69 | uint32_t blockOffset_ = 0; |
| 69 | uint32_t singleBufferProNum = 0; | 70 | uint32_t singleBufferProNum = 0; |
| 70 | 71 | ||
| 71 | static constexpr MicroAPI::CastTrait castTraitPt = { | 72 | static constexpr MicroAPI::CastTrait castTraitPt = {MicroAPI::RegLayout::ZERO, MicroAPI::SatMode::UNKNOWN, |
| 72 | MicroAPI::RegLayout::ZERO, MicroAPI::SatMode::UNKNOWN, MicroAPI::MaskMergeMode::ZEROING, | 73 | MicroAPI::MaskMergeMode::ZEROING, |
| 73 | AscendC::RoundMode::CAST_RINT}; | 74 | AscendC::RoundMode::CAST_RINT}; |
| 74 | }; | 75 | }; |
| 75 | 76 | ||
| 76 | template <typename T> | 77 | template <typename T> |
| 77 | __aicore__ inline void StatelessDropOutGenMaskPt<T>::Init( | 78 | __aicore__ inline void StatelessDropOutGenMaskPt<T>::Init(GM_ADDR shape, GM_ADDR prob, GM_ADDR seed, GM_ADDR offset, |
| 78 | GM_ADDR shape, GM_ADDR prob, GM_ADDR seed, GM_ADDR offset, GM_ADDR y) | 79 | GM_ADDR y) |
| 79 | { | 80 | { |
| 80 | // VarsInit 从 tiling_ 拷贝 key/counter(此时 tiling 中 key 存元数据,counter 已置零) | 81 | // VarsInit 从 tiling_ 拷贝 key/counter(此时 tiling 中 key 存元数据,counter 已置零) |
| 81 | VarsInit(); | 82 | VarsInit(); |
| @@ -96,7 +97,7 @@ __aicore__ inline void StatelessDropOutGenMaskPt<T>::Init( | |||
| 96 | seedGm.SetGlobalBuffer(reinterpret_cast<__gm__ int64_t*>(seed), 1); | 97 | seedGm.SetGlobalBuffer(reinterpret_cast<__gm__ int64_t*>(seed), 1); |
| 97 | keyVal = seedGm(0); | 98 | keyVal = seedGm(0); |
| 98 | } | 99 | } |
| 99 | key_[0] = static_cast<uint32_t>(keyVal); // 截断取低32位,符号位不影响 | 100 | key_[0] = static_cast<uint32_t>(keyVal); // 截断取低32位,符号位不影响 |
| 100 | // 先转 uint64_t 再右移:keyVal 是有符号 int64_t,负数直接右移是算术右移(高位补1), | 101 | // 先转 uint64_t 再右移:keyVal 是有符号 int64_t,负数直接右移是算术右移(高位补1), |
| 101 | // 转成无符号后右移才是逻辑右移(高位补0),保证拿到原始数据的高32位 | 102 | // 转成无符号后右移才是逻辑右移(高位补0),保证拿到原始数据的高32位 |
| 102 | key_[1] = static_cast<uint32_t>(static_cast<uint64_t>(keyVal) >> SHIFT_BITS); | 103 | key_[1] = static_cast<uint32_t>(static_cast<uint64_t>(keyVal) >> SHIFT_BITS); |
| @@ -104,12 +105,14 @@ __aicore__ inline void StatelessDropOutGenMaskPt<T>::Init( | |||
| 104 | // 从 GM 直接读取 offset → counter_[] | 105 | // 从 GM 直接读取 offset → counter_[] |
| 105 | uint64_t counterVal0 = 0; | 106 | uint64_t counterVal0 = 0; |
| 106 | uint64_t counterVal1 = 0; | 107 | uint64_t counterVal1 = 0; |
| 107 | counterGm_.SetGlobalBuffer(reinterpret_cast<__gm__ uint64_t*>(offset), offsetElemCount); | 108 | if (offsetElemCount > 0) { |
| 108 | if (offsetElemCount == 1) { | 109 | counterGm_.SetGlobalBuffer(reinterpret_cast<__gm__ uint64_t*>(offset), offsetElemCount); |
| 109 | counterVal1 = counterGm_(0); | 110 | if (offsetElemCount == 1) { |
| 110 | } else { | 111 | counterVal1 = counterGm_(0); |
| 111 | counterVal0 = counterGm_(0); | 112 | } else { |
| 112 | counterVal1 = counterGm_(1); | 113 | counterVal0 = counterGm_(0); |
| 114 | counterVal1 = counterGm_(1); | ||
| 115 | } | ||
| 113 | } | 116 | } |
| 114 | counter_[0] = static_cast<uint32_t>(counterVal0); | 117 | counter_[0] = static_cast<uint32_t>(counterVal0); |
| 115 | counter_[1] = static_cast<uint32_t>(counterVal0 >> SHIFT_BITS); | 118 | counter_[1] = static_cast<uint32_t>(counterVal0 >> SHIFT_BITS); |
| @@ -204,8 +207,8 @@ template <typename T> | |||
| 204 | __aicore__ inline void StatelessDropOutGenMaskPt<T>::Compute(uint32_t loopIdx, uint32_t calCount) | 207 | __aicore__ inline void StatelessDropOutGenMaskPt<T>::Compute(uint32_t loopIdx, uint32_t calCount) |
| 205 | { | 208 | { |
| 206 | LocalTensor<uint32_t> philoxRes = philoxQueBuf_.Get<uint32_t>(); | 209 | LocalTensor<uint32_t> philoxRes = philoxQueBuf_.Get<uint32_t>(); |
| 207 | AscendC::PhiloxRandom<10>( | 210 | AscendC::PhiloxRandom<10>(philoxRes, {key_[0], key_[1]}, {counter_[0], counter_[1], counter_[2], counter_[3]}, |
| 208 | philoxRes, {key_[0], key_[1]}, {counter_[0], counter_[1], counter_[2], counter_[3]}, calCount); | 211 | calCount); |
| 209 | Uint32ToFloat(calCount); | 212 | Uint32ToFloat(calCount); |
| 210 | CompareMask(calCount); | 213 | CompareMask(calCount); |
| 211 | } | 214 | } |
| @@ -225,7 +228,7 @@ __aicore__ inline void StatelessDropOutGenMaskPt<T>::Process() | |||
| 225 | 228 | ||
| 226 | LocalTensor<uint8_t> yOutput = outQueY_.DeQue<uint8_t>(); | 229 | LocalTensor<uint8_t> yOutput = outQueY_.DeQue<uint8_t>(); |
| 227 | int64_t yOffset = (idx * singleBufferProNum) / byteBitRatio; | 230 | int64_t yOffset = (idx * singleBufferProNum) / byteBitRatio; |
| 228 | uint32_t copyLength = static_cast<uint32_t>(Ops::Base::CeilDiv(curUbProNum,byteBitRatio) * sizeof(uint8_t)); | 231 | uint32_t copyLength = static_cast<uint32_t>(Ops::Base::CeilDiv(curUbProNum, byteBitRatio) * sizeof(uint8_t)); |
| 229 | CopyOut(yOutput, outputGm_, 1, copyLength, yOffset); | 232 | CopyOut(yOutput, outputGm_, 1, copyLength, yOffset); |
| 230 | outQueY_.FreeTensor(yOutput); | 233 | outQueY_.FreeTensor(yOutput); |
| 231 | 234 | ||
| @@ -234,4 +237,4 @@ __aicore__ inline void StatelessDropOutGenMaskPt<T>::Process() | |||
| 234 | } | 237 | } |
| 235 | } | 238 | } |
| 236 | } // namespace StatelessDropOutGenMask | 239 | } // namespace StatelessDropOutGenMask |
| 237 | #endif // STATELESS_DROP_OUT_GEN_MASK_PT_H | 240 | #endif // STATELESS_DROP_OUT_GEN_MASK_PT_H |