Merged
feat(RecurrentGatedDeltaRule): 支持 float32 类型的 ssm_state 输入输出 #5750
feat(RecurrentGatedDeltaRule): 支持 float32 类型的 ssm_state 输入输出 #5750
Merged
Vi1taAdd on May 22
Total: 11 files changed+211-85
@@ -64,7 +64,8 @@ struct RecurrentGatedDeltaRuleParams {
64 64 
65// support dtype65// support dtype
66static const std::initializer_list<op::DataType> QKV_TYPE_SUPPORT_LIST = {op::DataType::DT_BF16};66static const std::initializer_list<op::DataType> QKV_TYPE_SUPPORT_LIST = {op::DataType::DT_BF16};
67-static const std::initializer_list<op::DataType> STATE_TYPE_SUPPORT_LIST = {op::DataType::DT_BF16};67+static const std::initializer_list<op::DataType> STATE_TYPE_SUPPORT_LIST = {op::DataType::DT_BF16,
68+ op::DataType::DT_FLOAT};
68static const std::initializer_list<op::DataType> BETA_TYPE_SUPPORT_LIST = {op::DataType::DT_BF16};69static const std::initializer_list<op::DataType> BETA_TYPE_SUPPORT_LIST = {op::DataType::DT_BF16};
69static const std::initializer_list<op::DataType> SEQ_LENS_TYPE_SUPPORT_LIST = {op::DataType::DT_INT32};70static const std::initializer_list<op::DataType> SEQ_LENS_TYPE_SUPPORT_LIST = {op::DataType::DT_INT32};
70static const std::initializer_list<op::DataType> SSM_TYPE_SUPPORT_LIST = {op::DataType::DT_INT32};71static const std::initializer_list<op::DataType> SSM_TYPE_SUPPORT_LIST = {op::DataType::DT_INT32};
@@ -21,64 +21,64 @@ public:
21 {21 {
22 this->Input("query")22 this->Input("query")
23 .ParamType(REQUIRED)23 .ParamType(REQUIRED)
24- .DataType({ge::DT_BF16})24+ .DataType({ge::DT_BF16, ge::DT_BF16})
25- .Format({ge::FORMAT_ND})25+ .Format({ge::FORMAT_ND, ge::FORMAT_ND})
26- .UnknownShapeFormat({ge::FORMAT_ND});26+ .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND});
27 this->Input("key")27 this->Input("key")
28 .ParamType(REQUIRED)28 .ParamType(REQUIRED)
29- .DataType({ge::DT_BF16})29+ .DataType({ge::DT_BF16, ge::DT_BF16})
30- .Format({ge::FORMAT_ND})30+ .Format({ge::FORMAT_ND, ge::FORMAT_ND})
31- .UnknownShapeFormat({ge::FORMAT_ND});31+ .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND});
32 this->Input("value")32 this->Input("value")
33 .ParamType(REQUIRED)33 .ParamType(REQUIRED)
34- .DataType({ge::DT_BF16})34+ .DataType({ge::DT_BF16, ge::DT_BF16})
35- .Format({ge::FORMAT_ND})35+ .Format({ge::FORMAT_ND, ge::FORMAT_ND})
36- .UnknownShapeFormat({ge::FORMAT_ND});36+ .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND});
37 this->Input("beta")37 this->Input("beta")
38 .ParamType(REQUIRED)38 .ParamType(REQUIRED)
39- .DataType({ge::DT_BF16})39+ .DataType({ge::DT_BF16, ge::DT_BF16})
40- .Format({ge::FORMAT_ND})40+ .Format({ge::FORMAT_ND, ge::FORMAT_ND})
41- .UnknownShapeFormat({ge::FORMAT_ND});41+ .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND});
42 this->Input("state")42 this->Input("state")
43 .ParamType(REQUIRED)43 .ParamType(REQUIRED)
44- .DataType({ge::DT_BF16})44+ .DataType({ge::DT_BF16, ge::DT_FLOAT})
45- .Format({ge::FORMAT_ND})45+ .Format({ge::FORMAT_ND, ge::FORMAT_ND})
46- .UnknownShapeFormat({ge::FORMAT_ND});46+ .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND});
47 this->Input("actual_seq_lengths")47 this->Input("actual_seq_lengths")
48 .ParamType(REQUIRED)48 .ParamType(REQUIRED)
49- .DataType({ge::DT_INT32})49+ .DataType({ge::DT_INT32, ge::DT_INT32})
50- .Format({ge::FORMAT_ND})50+ .Format({ge::FORMAT_ND, ge::FORMAT_ND})
51- .UnknownShapeFormat({ge::FORMAT_ND});51+ .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND});
52 this->Input("ssm_state_indices")52 this->Input("ssm_state_indices")
53 .ParamType(REQUIRED)53 .ParamType(REQUIRED)
54- .DataType({ge::DT_INT32})54+ .DataType({ge::DT_INT32, ge::DT_INT32})
55- .Format({ge::FORMAT_ND})55+ .Format({ge::FORMAT_ND, ge::FORMAT_ND})
56- .UnknownShapeFormat({ge::FORMAT_ND});56+ .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND});
57 this->Input("g")57 this->Input("g")
58 .ParamType(OPTIONAL)58 .ParamType(OPTIONAL)
59- .DataType({ge::DT_FLOAT})59+ .DataType({ge::DT_FLOAT, ge::DT_FLOAT})
60- .Format({ge::FORMAT_ND})60+ .Format({ge::FORMAT_ND, ge::FORMAT_ND})
61- .UnknownShapeFormat({ge::FORMAT_ND});61+ .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND});
62 this->Input("gk")62 this->Input("gk")
63 .ParamType(OPTIONAL)63 .ParamType(OPTIONAL)
64- .DataType({ge::DT_FLOAT})64+ .DataType({ge::DT_FLOAT, ge::DT_FLOAT})
65- .Format({ge::FORMAT_ND})65+ .Format({ge::FORMAT_ND, ge::FORMAT_ND})
66- .UnknownShapeFormat({ge::FORMAT_ND});66+ .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND});
67 this->Input("num_accepted_tokens")67 this->Input("num_accepted_tokens")
68 .ParamType(OPTIONAL)68 .ParamType(OPTIONAL)
69- .DataType({ge::DT_INT32})69+ .DataType({ge::DT_INT32, ge::DT_INT32})
70- .Format({ge::FORMAT_ND})70+ .Format({ge::FORMAT_ND, ge::FORMAT_ND})
71- .UnknownShapeFormat({ge::FORMAT_ND});71+ .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND});
72 this->Output("out")72 this->Output("out")
73 .ParamType(REQUIRED)73 .ParamType(REQUIRED)
74- .DataType({ge::DT_BF16})74+ .DataType({ge::DT_BF16, ge::DT_BF16})
75- .Format({ge::FORMAT_ND})75+ .Format({ge::FORMAT_ND, ge::FORMAT_ND})
76- .UnknownShapeFormat({ge::FORMAT_ND});76+ .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND});
77 this->Output("state")77 this->Output("state")
78 .ParamType(REQUIRED)78 .ParamType(REQUIRED)
79- .DataType({ge::DT_BF16})79+ .DataType({ge::DT_BF16, ge::DT_FLOAT})
80- .Format({ge::FORMAT_ND})80+ .Format({ge::FORMAT_ND, ge::FORMAT_ND})
81- .UnknownShapeFormat({ge::FORMAT_ND});81+ .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND});
82 this->Attr("scale_value").AttrType(OPTIONAL).Float(1.0);82 this->Attr("scale_value").AttrType(OPTIONAL).Float(1.0);
83 83
84 OpAICoreConfig aicConfig;84 OpAICoreConfig aicConfig;
@@ -82,8 +82,9 @@ static ge::graphStatus InferShapeRecurrentGatedDeltaRule(InferShapeContext *cont
82 82 
83static ge::graphStatus InferDataTypeRecurrentGatedDeltaRule(gert::InferDataTypeContext *context)83static ge::graphStatus InferDataTypeRecurrentGatedDeltaRule(gert::InferDataTypeContext *context)
84{84{
85+ auto stateDtype = context->GetInputDataType(STATE_INDEX);
85 context->SetOutputDataType(0, ge::DT_BF16);86 context->SetOutputDataType(0, ge::DT_BF16);
86- context->SetOutputDataType(1, ge::DT_BF16);87+ context->SetOutputDataType(1, stateDtype);
87 return ge::GRAPH_SUCCESS;88 return ge::GRAPH_SUCCESS;
88}89}
89 90 
@@ -115,7 +115,7 @@ ge::graphStatus RecurrentGatedDeltaRuleTiling::DoOpTiling()
115 115 
116ge::graphStatus RecurrentGatedDeltaRuleTiling::DoLibApiTiling()116ge::graphStatus RecurrentGatedDeltaRuleTiling::DoLibApiTiling()
117{117{
118- tilingKey_ = 0;118+ tilingKey_ = (stateDtype_ == ge::DT_FLOAT) ? 1UL : 0UL;
119 return ge::GRAPH_SUCCESS;119 return ge::GRAPH_SUCCESS;
120};120};
121 121 
@@ -212,9 +212,13 @@ ge::graphStatus RecurrentGatedDeltaRuleTiling::AnalyzeDtype()
212 212 
213 auto betaDtype = context_->GetInputDesc(BETA_INDEX)->GetDataType();213 auto betaDtype = context_->GetInputDesc(BETA_INDEX)->GetDataType();
214 auto stateDtype = context_->GetInputDesc(STATE_INDEX)->GetDataType();214 auto stateDtype = context_->GetInputDesc(STATE_INDEX)->GetDataType();
215- OP_CHECK_IF(betaDtype != ge::DT_BF16 || stateDtype != ge::DT_BF16,215+ OP_CHECK_IF(betaDtype != ge::DT_BF16,
216- OP_LOGE(context_->GetNodeName(), "beta dtype and state dtype should be bfloat16."),216+ OP_LOGE(context_->GetNodeName(), "beta dtype should be bfloat16."),
217 return ge::GRAPH_FAILED);217 return ge::GRAPH_FAILED);
218+ OP_CHECK_IF(stateDtype != ge::DT_BF16 && stateDtype != ge::DT_FLOAT,
219+ OP_LOGE(context_->GetNodeName(), "state dtype should be bfloat16 or float32."),
220+ return ge::GRAPH_FAILED);
221+ stateDtype_ = stateDtype;
218 222 
219 auto actSeqlensDtype = context_->GetInputDesc(ACTUAL_SEQ_LENGTHS_INDEX)->GetDataType();223 auto actSeqlensDtype = context_->GetInputDesc(ACTUAL_SEQ_LENGTHS_INDEX)->GetDataType();
220 auto ssmStateIndicesDtype = context_->GetInputDesc(SSM_STATE_INDICES_INDEX)->GetDataType();224 auto ssmStateIndicesDtype = context_->GetInputDesc(SSM_STATE_INDICES_INDEX)->GetDataType();
@@ -608,8 +612,10 @@ ge::graphStatus RecurrentGatedDeltaRuleTiling::CalUbSize()
608 usedUbBytes += MAX_MTP * 2 * aNv; // 2 for betaInQueue_612 usedUbBytes += MAX_MTP * 2 * aNv; // 2 for betaInQueue_
609 tilingData_.ubRestBytes = ubSize - usedUbBytes;613 tilingData_.ubRestBytes = ubSize - usedUbBytes;
610 usedUbBytes += MAX_MTP * (8 * aDk + 4 * aDv + 4 * aNv); // 8 for qk in ub, 4 for v in ub, 4 for beta in ub614 usedUbBytes += MAX_MTP * (8 * aDk + 4 * aDv + 4 * aNv); // 8 for qk in ub, 4 for v in ub, 4 for beta in ub
611- int64_t coeff = (2 + 2) * aDk + 4; // 2 for stateInQueue_, stateOutQueue_, 4 for attnOutQueue_615+ int64_t stateInElemBytes = (stateDtype_ == ge::DT_FLOAT) ? sizeof(float) : sizeof(uint16_t);
612- coeff += (4 + 4) * aDk + 4 + 4; // 4 for qInUb, kInUb, vInUb, deltaInUb, attnInUb616+ int64_t stateOutElemBytes = stateInElemBytes; // stateOut has the same dtype as stateIn
617+ int64_t coeff = (stateInElemBytes + stateOutElemBytes) * aDk + 4; // stateInQueue_, stateOutQueue_, attnOutQueue_
618+ coeff += (4 + 4) * aDk + 4 + 4; // 4 for qInUb, kInUb, vInUb, deltaInUb, attnInUb
613 int64_t vStep = (ubSize - usedUbBytes) / coeff / 8 * 8; // 8 * sizeof(float) = 32619 int64_t vStep = (ubSize - usedUbBytes) / coeff / 8 * 8; // 8 * sizeof(float) = 32
614 if (vStep < 8) { // vStep不小于8620 if (vStep < 8) { // vStep不小于8
615 OP_LOGE(context_->GetNodeName(), "vStep should be bigger than 8, shape is too big.");621 OP_LOGE(context_->GetNodeName(), "vStep should be bigger than 8, shape is too big.");
@@ -620,7 +626,7 @@ ge::graphStatus RecurrentGatedDeltaRuleTiling::CalUbSize()
620 static_cast<uint32_t>(8)); // 8 * sizeof(float) = 32626 static_cast<uint32_t>(8)); // 8 * sizeof(float) = 32
621 tilingData_.ubCalSize = compileInfo_.ubSize;627 tilingData_.ubCalSize = compileInfo_.ubSize;
622 tilingData_.vStep = vStep;628 tilingData_.vStep = vStep;
623- tilingData_.ubRestBytes -= ((2 + 2) * aDk + 4) * vStep; // 2 for stateInQueue_, stateOutQueue_, 4 for attnOutQueue_629+ tilingData_.ubRestBytes -= ((stateInElemBytes + stateOutElemBytes) * aDk + 4) * vStep;
624 630 
625 return ge::GRAPH_SUCCESS;631 return ge::GRAPH_SUCCESS;
626}632}
@@ -85,6 +85,7 @@ protected:
85 RecurrentGatedDeltaRuleCompileInfo compileInfo_;85 RecurrentGatedDeltaRuleCompileInfo compileInfo_;
86 RecurrentGatedDeltaRuleTilingData tilingData_;86 RecurrentGatedDeltaRuleTilingData tilingData_;
87 RecurrentGatedDeltaRuleInfo inputParams_;87 RecurrentGatedDeltaRuleInfo inputParams_;
88+ ge::DataType stateDtype_ = ge::DT_BF16;
88};89};
89 90 
90} // namespace optiling91} // namespace optiling
@@ -46,7 +46,7 @@ struct RGDRInitParams {
46 GM_ADDR finalState;46 GM_ADDR finalState;
47};47};
48 48 
49-template <typename inType, typename outType>49+template <typename inType, typename outType, typename stateType = inType>
50class RGDR {50class RGDR {
51public:51public:
52 __aicore__ inline RGDR(const RecurrentGatedDeltaRuleTilingData *tilingData)52 __aicore__ inline RGDR(const RecurrentGatedDeltaRuleTilingData *tilingData)
@@ -91,11 +91,11 @@ public:
91 gamaGm_.SetGlobalBuffer((__gm__ float *)initParams.gama);91 gamaGm_.SetGlobalBuffer((__gm__ float *)initParams.gama);
92 gamaKGm_.SetGlobalBuffer((__gm__ float *)initParams.gamaK);92 gamaKGm_.SetGlobalBuffer((__gm__ float *)initParams.gamaK);
93 betaGm_.SetGlobalBuffer((__gm__ inType *)initParams.beta);93 betaGm_.SetGlobalBuffer((__gm__ inType *)initParams.beta);
94- initStateGm_.SetGlobalBuffer((__gm__ inType *)initParams.initState);94+ initStateGm_.SetGlobalBuffer((__gm__ stateType *)initParams.initState);
95 cuSeqlensGm_.SetGlobalBuffer((__gm__ int32_t *)initParams.cuSeqlens);95 cuSeqlensGm_.SetGlobalBuffer((__gm__ int32_t *)initParams.cuSeqlens);
96 ssmStateIndicesGm_.SetGlobalBuffer((__gm__ int32_t *)initParams.ssmStateIndices);96 ssmStateIndicesGm_.SetGlobalBuffer((__gm__ int32_t *)initParams.ssmStateIndices);
97 numAcceptedTokensGm_.SetGlobalBuffer((__gm__ int32_t *)initParams.numAcceptedTokens);97 numAcceptedTokensGm_.SetGlobalBuffer((__gm__ int32_t *)initParams.numAcceptedTokens);
98- finalStateGm_.SetGlobalBuffer((__gm__ outType *)initParams.finalState);98+ finalStateGm_.SetGlobalBuffer((__gm__ stateType *)initParams.finalState);
99 attnOutGm_.SetGlobalBuffer((__gm__ outType *)initParams.attnOut);99 attnOutGm_.SetGlobalBuffer((__gm__ outType *)initParams.attnOut);
100 }100 }
101 101 
@@ -110,7 +110,7 @@ public:
110 pipe_->InitBuffer(qInQueue_, BUFFER_NUM, MAX_MTP * alignK_ * sizeof(inType));110 pipe_->InitBuffer(qInQueue_, BUFFER_NUM, MAX_MTP * alignK_ * sizeof(inType));
111 pipe_->InitBuffer(kInQueue_, BUFFER_NUM, MAX_MTP * alignK_ * sizeof(inType));111 pipe_->InitBuffer(kInQueue_, BUFFER_NUM, MAX_MTP * alignK_ * sizeof(inType));
112 pipe_->InitBuffer(vInQueue_, BUFFER_NUM, MAX_MTP * alignV_ * sizeof(inType));112 pipe_->InitBuffer(vInQueue_, BUFFER_NUM, MAX_MTP * alignV_ * sizeof(inType));
113- pipe_->InitBuffer(stateInQueue_, BUFFER_NUM, alignK_ * vStep_ * sizeof(inType));113+ pipe_->InitBuffer(stateInQueue_, BUFFER_NUM, alignK_ * vStep_ * sizeof(stateType));
114 if (hasGama_) {114 if (hasGama_) {
115 pipe_->InitBuffer(gamaInQueue_, BUFFER_NUM, MAX_MTP * NV_ * sizeof(float));115 pipe_->InitBuffer(gamaInQueue_, BUFFER_NUM, MAX_MTP * NV_ * sizeof(float));
116 }116 }
@@ -118,7 +118,7 @@ public:
118 pipe_->InitBuffer(gamaKInQueue_, BUFFER_NUM, MAX_MTP * alignK_ * sizeof(float));118 pipe_->InitBuffer(gamaKInQueue_, BUFFER_NUM, MAX_MTP * alignK_ * sizeof(float));
119 }119 }
120 pipe_->InitBuffer(betaInQueue_, BUFFER_NUM, MAX_MTP * NV_ * sizeof(inType));120 pipe_->InitBuffer(betaInQueue_, BUFFER_NUM, MAX_MTP * NV_ * sizeof(inType));
121- pipe_->InitBuffer(stateOutQueue_, BUFFER_NUM, alignK_ * vStep_ * sizeof(outType));121+ pipe_->InitBuffer(stateOutQueue_, BUFFER_NUM, alignK_ * vStep_ * sizeof(stateType));
122 pipe_->InitBuffer(attnOutQueue_, BUFFER_NUM, vStep_ * sizeof(outType));122 pipe_->InitBuffer(attnOutQueue_, BUFFER_NUM, vStep_ * sizeof(outType));
123 pipe_->InitBuffer(tmpBuff, restUbSize_);123 pipe_->InitBuffer(tmpBuff, restUbSize_);
124 uint32_t buffOffset = 0;124 uint32_t buffOffset = 0;
@@ -226,14 +226,25 @@ private:
226 226 
227 __aicore__ inline void CopyInState(uint64_t stateOffest, uint32_t curSingleV)227 __aicore__ inline void CopyInState(uint64_t stateOffest, uint32_t curSingleV)
228 {228 {
229- LocalTensor<inType> stateLocal = stateInQueue_.AllocTensor<inType>();229+ LocalTensor<stateType> stateLocal = stateInQueue_.AllocTensor<stateType>();
230+ // DataCopyPad rightPadding may only fill within one 32B block; align to the state dtype's
231+ // block (8 elems for fp32, 16 for bf16) so padding stays legal on unaligned dk. The gap up
232+ // to alignK_ is bridged by dstStride and pre-zeroed via Duplicate (mirrors the gamaK path).
233+ constexpr uint32_t blockElems = std::is_same_v<stateType, float> ? FP32_NUM_PER_BLOCK : BF16_NUM_PER_BLOCK;
234+ uint32_t alignKState = Ceil(realK_, blockElems) * blockElems;
235+ uint32_t dstStride = (alignK_ - alignKState) / blockElems;
230 DataCopyExtParams stateInParams{static_cast<uint16_t>(curSingleV),236 DataCopyExtParams stateInParams{static_cast<uint16_t>(curSingleV),
231- static_cast<uint16_t>(realK_ * sizeof(inType)), 0, 0, 0};237+ static_cast<uint16_t>(realK_ * sizeof(stateType)), 0,
232- DataCopyPadExtParams<inType> padParams{true, 0, static_cast<uint8_t>(alignK_ - realK_), 0};238+ static_cast<uint16_t>(dstStride), 0};
239+ DataCopyPadExtParams<stateType> padParams{true, 0, static_cast<uint8_t>(alignKState - realK_), 0};
233 DataCopyPad(stateLocal, initStateGm_[stateOffest], stateInParams, padParams);240 DataCopyPad(stateLocal, initStateGm_[stateOffest], stateInParams, padParams);
234- stateInQueue_.EnQue<inType>(stateLocal);241+ stateInQueue_.EnQue<stateType>(stateLocal);
235- stateLocal = stateInQueue_.DeQue<inType>();242+ stateLocal = stateInQueue_.DeQue<stateType>();
236- Cast(stateInUb, stateLocal, AscendC::RoundMode::CAST_NONE, alignK_ * curSingleV);243+ if constexpr (!std::is_same_v<stateType, float>) {
244+ Cast(stateInUb, stateLocal, AscendC::RoundMode::CAST_NONE, alignK_ * curSingleV);
245+ } else {
246+ DataCopy(stateInUb, stateLocal, alignK_ * curSingleV);
247+ }
237 stateInQueue_.FreeTensor(stateLocal);248 stateInQueue_.FreeTensor(stateLocal);
238 }249 }
239 250 
@@ -284,10 +295,14 @@ private:
284 MatVecMul(stateInUb, qInUb[curQKOffset], broadTmpInUb, curSingleV, false);295 MatVecMul(stateInUb, qInUb[curQKOffset], broadTmpInUb, curSingleV, false);
285 AscendC::PipeBarrier<PIPE_V>();296 AscendC::PipeBarrier<PIPE_V>();
286 ReduceSum<float, Pattern::Reduce::AR, true>(attnInUb, broadTmpInUb, stateShape, true);297 ReduceSum<float, Pattern::Reduce::AR, true>(attnInUb, broadTmpInUb, stateShape, true);
287- LocalTensor<outType> stateOutLocal = stateOutQueue_.AllocTensor<outType>();298+ LocalTensor<stateType> stateOutLocal = stateOutQueue_.AllocTensor<stateType>();
288 LocalTensor<outType> attnOutLocal = attnOutQueue_.AllocTensor<outType>();299 LocalTensor<outType> attnOutLocal = attnOutQueue_.AllocTensor<outType>();
289- Cast(stateOutLocal, stateInUb, AscendC::RoundMode::CAST_RINT, alignK_ * curSingleV);300+ if constexpr (!std::is_same_v<stateType, float>) {
290- stateOutQueue_.EnQue<outType>(stateOutLocal);301+ Cast(stateOutLocal, stateInUb, AscendC::RoundMode::CAST_RINT, alignK_ * curSingleV);
302+ } else {
303+ DataCopy(stateOutLocal, stateInUb, alignK_ * curSingleV);
304+ }
305+ stateOutQueue_.EnQue<stateType>(stateOutLocal);
291 AscendC::PipeBarrier<PIPE_V>();306 AscendC::PipeBarrier<PIPE_V>();
292 Cast(attnOutLocal, attnInUb, AscendC::RoundMode::CAST_RINT, curSingleV);307 Cast(attnOutLocal, attnInUb, AscendC::RoundMode::CAST_RINT, curSingleV);
293 attnOutQueue_.EnQue<outType>(attnOutLocal);308 attnOutQueue_.EnQue<outType>(attnOutLocal);
@@ -303,9 +318,14 @@ private:
303 318 
304 __aicore__ inline void CopyOutState(uint64_t stateOffset, uint32_t curSingleV)319 __aicore__ inline void CopyOutState(uint64_t stateOffset, uint32_t curSingleV)
305 {320 {
306- LocalTensor<outType> stateOutLocal = stateOutQueue_.DeQue<outType>();321+ LocalTensor<stateType> stateOutLocal = stateOutQueue_.DeQue<stateType>();
307- DataCopyParams stateOutParams{static_cast<uint16_t>(curSingleV),322+ // UB rows are alignK_-strided while GM is dense realK_-strided. DataCopyExtParams splits the
308- static_cast<uint16_t>(realK_ * sizeof(outType)), 0, 0};323+ // strides (src/UB in 32B datablocks, dst/GM in bytes), so srcStride bridges the gap per row
324+ // for both dtypes in one MTE op (mirrors arch35). blockElems = 32B / sizeof(stateType).
325+ constexpr uint32_t blockElems = std::is_same_v<stateType, float> ? FP32_NUM_PER_BLOCK : BF16_NUM_PER_BLOCK;
326+ DataCopyExtParams stateOutParams{static_cast<uint16_t>(curSingleV),
327+ static_cast<uint32_t>(realK_ * sizeof(stateType)),
328+ static_cast<uint32_t>((alignK_ - realK_) / blockElems), 0, 0};
309 DataCopyPad(finalStateGm_[stateOffset], stateOutLocal, stateOutParams);329 DataCopyPad(finalStateGm_[stateOffset], stateOutLocal, stateOutParams);
310 stateOutQueue_.FreeTensor(stateOutLocal);330 stateOutQueue_.FreeTensor(stateOutLocal);
311 }331 }
@@ -379,11 +399,11 @@ private:
379 GlobalTensor<inType> betaGm_;399 GlobalTensor<inType> betaGm_;
380 GlobalTensor<float> gamaGm_;400 GlobalTensor<float> gamaGm_;
381 GlobalTensor<float> gamaKGm_;401 GlobalTensor<float> gamaKGm_;
382- GlobalTensor<inType> initStateGm_;402+ GlobalTensor<stateType> initStateGm_;
383 GlobalTensor<int32_t> cuSeqlensGm_;403 GlobalTensor<int32_t> cuSeqlensGm_;
384 GlobalTensor<int32_t> ssmStateIndicesGm_;404 GlobalTensor<int32_t> ssmStateIndicesGm_;
385 GlobalTensor<int32_t> numAcceptedTokensGm_;405 GlobalTensor<int32_t> numAcceptedTokensGm_;
386- GlobalTensor<outType> finalStateGm_;406+ GlobalTensor<stateType> finalStateGm_;
387 GlobalTensor<outType> attnOutGm_;407 GlobalTensor<outType> attnOutGm_;
388 TPipe *pipe_;408 TPipe *pipe_;
389 TQue<QuePosition::VECIN, 1> qInQueue_;409 TQue<QuePosition::VECIN, 1> qInQueue_;
@@ -128,7 +128,7 @@ struct RGDRInitParams {
128 GM_ADDR finalState;128 GM_ADDR finalState;
129};129};
130 130 
131-template <typename inType, typename outType>131+template <typename inType, typename outType, typename stateType = inType>
132class RGDR {132class RGDR {
133public:133public:
134 __aicore__ inline RGDR(const RecurrentGatedDeltaRuleTilingData *tilingData)134 __aicore__ inline RGDR(const RecurrentGatedDeltaRuleTilingData *tilingData)
@@ -146,7 +146,7 @@ public:
146 alignDK_ = Ceil(tilingData->dk, BF16_NUM_PER_BLOCK) * BF16_NUM_PER_BLOCK;146 alignDK_ = Ceil(tilingData->dk, BF16_NUM_PER_BLOCK) * BF16_NUM_PER_BLOCK;
147 alignDV_ = Ceil(tilingData->dv, BF16_NUM_PER_BLOCK) * BF16_NUM_PER_BLOCK;147 alignDV_ = Ceil(tilingData->dv, BF16_NUM_PER_BLOCK) * BF16_NUM_PER_BLOCK;
148 vStep_ = 8192 / alignDK_;148 vStep_ = 8192 / alignDK_;
149- if (vStep_ > 256) { 149+ if (vStep_ > 256) {
150 vStep_ = 255;150 vStep_ = 255;
151 }151 }
152 vStep_ = vStep_ / BF16_NUM_PER_BLOCK * BF16_NUM_PER_BLOCK;152 vStep_ = vStep_ / BF16_NUM_PER_BLOCK * BF16_NUM_PER_BLOCK;
@@ -174,18 +174,20 @@ public:
174 gamaGm_.SetGlobalBuffer((__gm__ float *)initParams.gama);174 gamaGm_.SetGlobalBuffer((__gm__ float *)initParams.gama);
175 gamaKGm_.SetGlobalBuffer((__gm__ float *)initParams.gamaK);175 gamaKGm_.SetGlobalBuffer((__gm__ float *)initParams.gamaK);
176 betaGm_.SetGlobalBuffer((__gm__ inType *)initParams.beta);176 betaGm_.SetGlobalBuffer((__gm__ inType *)initParams.beta);
177- initStateGm_.SetGlobalBuffer((__gm__ inType *)initParams.initState);177+ initStateGm_.SetGlobalBuffer((__gm__ stateType *)initParams.initState);
178 cuSeqlensGm_.SetGlobalBuffer((__gm__ int32_t *)initParams.cuSeqlens);178 cuSeqlensGm_.SetGlobalBuffer((__gm__ int32_t *)initParams.cuSeqlens);
179 ssmStateIndicesGm_.SetGlobalBuffer((__gm__ int32_t *)initParams.ssmStateIndices);179 ssmStateIndicesGm_.SetGlobalBuffer((__gm__ int32_t *)initParams.ssmStateIndices);
180 numAcceptedTokensGm_.SetGlobalBuffer((__gm__ int32_t *)initParams.numAcceptedTokens);180 numAcceptedTokensGm_.SetGlobalBuffer((__gm__ int32_t *)initParams.numAcceptedTokens);
181- finalStateGm_.SetGlobalBuffer((__gm__ outType *)initParams.finalState);181+ finalStateGm_.SetGlobalBuffer((__gm__ stateType *)initParams.finalState);
182 attnOutGm_.SetGlobalBuffer((__gm__ outType *)initParams.attnOut);182 attnOutGm_.SetGlobalBuffer((__gm__ outType *)initParams.attnOut);
183 }183 }
184 184 
185 __aicore__ inline void InitLocalBuffers()185 __aicore__ inline void InitLocalBuffers()
186 {186 {
187- pipe_->InitBuffer(inputQueue1_, DOUBLE_BUFFER, BUFFER_SIZE_BYTE_16K);187+ constexpr uint32_t stateQueueSize =
188- pipe_->InitBuffer(stateOutQueue_, SINGLE_BUFFER, BUFFER_SIZE_BYTE_16K);188+ std::is_same_v<stateType, float> ? BUFFER_SIZE_BYTE_32K : BUFFER_SIZE_BYTE_16K;
189+ pipe_->InitBuffer(inputQueue1_, DOUBLE_BUFFER, stateQueueSize);
190+ pipe_->InitBuffer(stateOutQueue_, SINGLE_BUFFER, stateQueueSize);
189 pipe_->InitBuffer(attnOutQueue_, SINGLE_BUFFER, BUFFER_SIZE_BYTE_16K);191 pipe_->InitBuffer(attnOutQueue_, SINGLE_BUFFER, BUFFER_SIZE_BYTE_16K);
190 pipe_->InitBuffer(gamaBuff_, BUFFER_SIZE_BYTE_16K);192 pipe_->InitBuffer(gamaBuff_, BUFFER_SIZE_BYTE_16K);
191 pipe_->InitBuffer(stateBuff_, BUFFER_SIZE_BYTE_32K);193 pipe_->InitBuffer(stateBuff_, BUFFER_SIZE_BYTE_32K);
@@ -327,11 +329,15 @@ private:
327 uint32_t actDataLen = DK_;329 uint32_t actDataLen = DK_;
328 uint64_t srcRowStride = DK_;330 uint64_t srcRowStride = DK_;
329 uint64_t dstRowStride = alignDK_;331 uint64_t dstRowStride = alignDK_;
330- LocalTensor<inType> stateInUb = inputQueue1_.AllocTensor<inType>();332+ LocalTensor<stateType> stateInUb = inputQueue1_.AllocTensor<stateType>();
331 CopySingleMatrixNDToND(stateInUb, initStateGm_[stateGmOffset], dealRowCount, actDataLen, srcRowStride, dstRowStride);333 CopySingleMatrixNDToND(stateInUb, initStateGm_[stateGmOffset], dealRowCount, actDataLen, srcRowStride, dstRowStride);
332- inputQueue1_.EnQue<inType>(stateInUb);334+ inputQueue1_.EnQue<stateType>(stateInUb);
333- inputQueue1_.DeQue<inType>();335+ inputQueue1_.DeQue<stateType>();
334- Cast(stateUb_, stateInUb, RoundMode::CAST_NONE, dealRowCount * dstRowStride);336+ if constexpr (!std::is_same_v<stateType, float>) {
337+ Cast(stateUb_, stateInUb, RoundMode::CAST_NONE, dealRowCount * dstRowStride);
338+ } else {
339+ DataCopy(stateUb_, stateInUb, dealRowCount * dstRowStride);
340+ }
335 inputQueue1_.FreeTensor(stateInUb);341 inputQueue1_.FreeTensor(stateInUb);
336 }342 }
337 343 
@@ -352,10 +358,14 @@ private:
352 358 
353 __aicore__ inline void CopyOutState(LocalTensor<float> &outStateFp32Ub, uint32_t tIdx, uint32_t head_i, uint32_t dvIdx, uint32_t curSingleV)359 __aicore__ inline void CopyOutState(LocalTensor<float> &outStateFp32Ub, uint32_t tIdx, uint32_t head_i, uint32_t dvIdx, uint32_t curSingleV)
354 {360 {
355- LocalTensor<outType> stateOutLocal = stateOutQueue_.AllocTensor<outType>();361+ LocalTensor<stateType> stateOutLocal = stateOutQueue_.AllocTensor<stateType>();
356- Cast(stateOutLocal, outStateFp32Ub, AscendC::RoundMode::CAST_RINT, alignDK_ * curSingleV);362+ if constexpr (!std::is_same_v<stateType, float>) {
357- stateOutQueue_.EnQue<outType>(stateOutLocal);363+ Cast(stateOutLocal, outStateFp32Ub, AscendC::RoundMode::CAST_RINT, alignDK_ * curSingleV);
358- stateOutQueue_.DeQue<outType>();364+ } else {
365+ DataCopy(stateOutLocal, outStateFp32Ub, alignDK_ * curSingleV);
366+ }
367+ stateOutQueue_.EnQue<stateType>(stateOutLocal);
368+ stateOutQueue_.DeQue<stateType>();
359 DataCopyExtParams stateOutParams;369 DataCopyExtParams stateOutParams;
360 stateOutParams.blockCount = curSingleV;370 stateOutParams.blockCount = curSingleV;
361 stateOutParams.blockLen = DK_ * sizeof(outType);371 stateOutParams.blockLen = DK_ * sizeof(outType);
@@ -465,11 +475,11 @@ private:
465 GlobalTensor<int32_t> cuSeqlensGm_;475 GlobalTensor<int32_t> cuSeqlensGm_;
466 GlobalTensor<int32_t> ssmStateIndicesGm_;476 GlobalTensor<int32_t> ssmStateIndicesGm_;
467 GlobalTensor<inType> betaGm_;477 GlobalTensor<inType> betaGm_;
468- GlobalTensor<inType> initStateGm_;478+ GlobalTensor<stateType> initStateGm_;
469 GlobalTensor<float> gamaGm_;479 GlobalTensor<float> gamaGm_;
470 GlobalTensor<float> gamaKGm_;480 GlobalTensor<float> gamaKGm_;
471 GlobalTensor<int32_t> numAcceptedTokensGm_;481 GlobalTensor<int32_t> numAcceptedTokensGm_;
472- GlobalTensor<outType> finalStateGm_;482+ GlobalTensor<stateType> finalStateGm_;
473 483 
474 TQue<QuePosition::VECIN, 1> inputQueue1_;484 TQue<QuePosition::VECIN, 1> inputQueue1_;
475 TQue<QuePosition::VECOUT, 1> attnOutQueue_;485 TQue<QuePosition::VECOUT, 1> attnOutQueue_;
@@ -14,6 +14,7 @@
14 */14 */
15#include "arch22/recurrent_gated_delta_rule.h"15#include "arch22/recurrent_gated_delta_rule.h"
16#include "recurrent_gated_delta_rule_tiling_data.h"16#include "recurrent_gated_delta_rule_tiling_data.h"
17+#include "recurrent_gated_delta_rule_tiling_key.h"
17 18 
18 19 
19using namespace AscendC;20using namespace AscendC;
@@ -30,9 +31,15 @@ recurrent_gated_delta_rule(GM_ADDR query, GM_ADDR key, GM_ADDR value, GM_ADDR be
30 GET_TILING_DATA(tilingData, tilingGM);31 GET_TILING_DATA(tilingData, tilingGM);
31 KERNEL_TASK_TYPE_DEFAULT(KERNEL_TYPE_AIV_ONLY);32 KERNEL_TASK_TYPE_DEFAULT(KERNEL_TYPE_AIV_ONLY);
32 TPipe pipe;33 TPipe pipe;
33- RGDR<bfloat16_t, bfloat16_t> op(&tilingData);
34 RGDRInitParams initParams{query, key, value, g, gk, beta, state, cuSeqlens,34 RGDRInitParams initParams{query, key, value, g, gk, beta, state, cuSeqlens,
35 ssmStateIndices, numAcceptedTokens, out, stateOut};35 ssmStateIndices, numAcceptedTokens, out, stateOut};
36- op.Init(initParams, &pipe);36+ if (TILING_KEY_IS(TILING_KEY_RGDR_FP32_STATE)) {
37- op.Process();37+ RGDR<bfloat16_t, bfloat16_t, float> op(&tilingData);
38+ op.Init(initParams, &pipe);
39+ op.Process();
40+ } else if (TILING_KEY_IS(TILING_KEY_RGDR_BF16_STATE)) {
41+ RGDR<bfloat16_t, bfloat16_t, bfloat16_t> op(&tilingData);
42+ op.Init(initParams, &pipe);
43+ op.Process();
44+ }
38}45}
@@ -14,6 +14,7 @@
14 */14 */
15#include "arch35/recurrent_gated_delta_rule.h"15#include "arch35/recurrent_gated_delta_rule.h"
16#include "recurrent_gated_delta_rule_tiling_data.h"16#include "recurrent_gated_delta_rule_tiling_data.h"
17+#include "recurrent_gated_delta_rule_tiling_key.h"
17 18 
18 19 
19using namespace AscendC;20using namespace AscendC;
@@ -30,9 +31,15 @@ recurrent_gated_delta_rule(GM_ADDR query, GM_ADDR key, GM_ADDR value, GM_ADDR be
30 GET_TILING_DATA(tilingData, tilingGM);31 GET_TILING_DATA(tilingData, tilingGM);
31 KERNEL_TASK_TYPE_DEFAULT(KERNEL_TYPE_AIV_ONLY);32 KERNEL_TASK_TYPE_DEFAULT(KERNEL_TYPE_AIV_ONLY);
32 TPipe pipe;33 TPipe pipe;
33- RGDR<bfloat16_t, bfloat16_t> op(&tilingData);
34 RGDRInitParams initParams{query, key, value, g, gk, beta, state, cuSeqlens,34 RGDRInitParams initParams{query, key, value, g, gk, beta, state, cuSeqlens,
35 ssmStateIndices, numAcceptedTokens, out, stateOut};35 ssmStateIndices, numAcceptedTokens, out, stateOut};
36- op.Init(initParams, &pipe);36+ if (TILING_KEY_IS(TILING_KEY_RGDR_FP32_STATE)) {
37- op.Process();37+ RGDR<bfloat16_t, bfloat16_t, float> op(&tilingData);
38+ op.Init(initParams, &pipe);
39+ op.Process();
40+ } else if (TILING_KEY_IS(TILING_KEY_RGDR_BF16_STATE)) {
41+ RGDR<bfloat16_t, bfloat16_t, bfloat16_t> op(&tilingData);
42+ op.Init(initParams, &pipe);
43+ op.Process();
44+ }
38}45}
@@ -0,0 +1,22 @@
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+/*!
12+ * \file recurrent_gated_delta_rule_tiling_key.h
13+ * \brief recurrent_gated_delta_rule tiling key definitions
14+ */
15+ 
16+#ifndef RECURRENT_GATED_DELTA_RULE_TILING_KEY_H
17+#define RECURRENT_GATED_DELTA_RULE_TILING_KEY_H
18+ 
19+#define TILING_KEY_RGDR_BF16_STATE 0UL
20+#define TILING_KEY_RGDR_FP32_STATE 1UL
21+ 
22+#endif // RECURRENT_GATED_DELTA_RULE_TILING_KEY_H
@@ -90,3 +90,54 @@ TEST_F(RecurrentGatedDeltaRuleTilingTest, Test0)
90 ExecuteTiling(tilingContextPara, tilingInfo);90 ExecuteTiling(tilingContextPara, tilingInfo);
91 EXPECT_EQ(tilingInfo.tilingKey, expectTilingKey);91 EXPECT_EQ(tilingInfo.tilingKey, expectTilingKey);
92}92}
93+ 
94+TEST_F(RecurrentGatedDeltaRuleTilingTest, Test1)
95+{
96+ optiling::RecurrentGatedDeltaRuleCompileInfo compileinfo = {40, 196608}; // aivNum、ubSize
97+ 
98+ int t = 128;
99+ int nk = 4;
100+ int dk = 8;
101+ int nv = 128;
102+ int dv = 128;
103+ int sBlockNum = 128;
104+ int b = 64;
105+ 
106+ gert::StorageShape queryShape = {{t, nk, dk}, {t, nk, dk}};
107+ gert::StorageShape keyShape = {{t, nk, dk}, {t, nk, dk}};
108+ gert::StorageShape valueShape = {{t, nv, dv}, {t, nv, dv}};
109+ gert::StorageShape betaShape = {{t, nv}, {t, nv}};
110+ gert::StorageShape stateShape = {{sBlockNum, nv, dv, dk}, {sBlockNum, nv, dv, dk}};
111+ gert::StorageShape seqLengthsShape = {{b}, {b}};
112+ gert::StorageShape ssmStateIndicesShape = {{t}, {t}};
113+ gert::StorageShape gShape = {{t, nv}, {t, nv}};
114+ gert::StorageShape outShape = {{t, nv, dv}, {t, nv, dv}};
115+ gert::StorageShape stateOutShape = {{sBlockNum, nv, dv, dk}, {sBlockNum, nv, dv, dk}};
116+ 
117+ gert::TilingContextPara tilingContextPara("RecurrentGatedDeltaRule",
118+ {
119+ {queryShape, ge::DT_BF16, ge::FORMAT_ND},
120+ {keyShape, ge::DT_BF16, ge::FORMAT_ND},
121+ {valueShape, ge::DT_BF16, ge::FORMAT_ND},
122+ {betaShape, ge::DT_BF16, ge::FORMAT_ND},
123+ {stateShape, ge::DT_FLOAT, ge::FORMAT_ND},
124+ {seqLengthsShape, ge::DT_INT32, ge::FORMAT_ND},
125+ {ssmStateIndicesShape, ge::DT_INT32, ge::FORMAT_ND},
126+ {gShape, ge::DT_FLOAT, ge::FORMAT_ND},
127+ },
128+ {
129+ {outShape, ge::DT_BF16, ge::FORMAT_ND},
130+ {stateOutShape, ge::DT_FLOAT, ge::FORMAT_ND},
131+ },
132+ {
133+ {"sacle_value", Ops::Transformer::AnyValue::CreateFrom<float>(1.0)},
134+ },
135+ &compileinfo
136+ );
137+ 
138+ int64_t expectTilingKey = 1UL;
139+ 
140+ TilingInfo tilingInfo;
141+ ExecuteTiling(tilingContextPara, tilingInfo);
142+ EXPECT_EQ(tilingInfo.tilingKey, expectTilingKey);
143+}