Merged
fix: 修复GroupNormSilu校验与NaN、GeluBackward出参校验、SwiGluGrad错误码表 #13166
shiluluAdd on 22 days ago
fix: 修复GroupNormSilu校验与NaN、GeluBackward出参校验、SwiGluGrad错误码表 #13166
Merged
shiluluAdd on 22 days ago
Total: 12 files changed+101-27
@@ -57,12 +57,14 @@ static bool CheckShape(const aclTensor* gradOutput, const aclTensor* self, const
57}57}
58 58 
59namespace {59namespace {
60-static bool CheckPromoteType(const aclTensor* gradOutput, const aclTensor* self, const aclTensor* /* gradInput */)60+static bool CheckPromoteType(const aclTensor* gradOutput, const aclTensor* self, const aclTensor* gradInput)
61{61{
62 op::DataType promoteType;62 op::DataType promoteType;
63 if (!CheckPromoteTypeGeluBackward(gradOutput, self, promoteType, DTYPE_SUPPORT_LIST)) {63 if (!CheckPromoteTypeGeluBackward(gradOutput, self, promoteType, DTYPE_SUPPORT_LIST)) {
64 return false;64 return false;
65 }65 }
66+ // 检查出参gradInput的数据类型在支持列表内
67+ OP_CHECK_DTYPE_NOT_SUPPORT(gradInput, DTYPE_SUPPORT_LIST, return false);
66 return true;68 return true;
67}69}
68} // namespace70} // namespace
@@ -83,7 +85,7 @@ static aclnnStatus CheckParams(const aclTensor* gradOutput, const aclTensor* sel
83 // 1. 检查参数是否为空指针85 // 1. 检查参数是否为空指针
84 CHECK_RET(CheckNotNull(gradOutput, self, gradInput), ACLNN_ERR_PARAM_NULLPTR);86 CHECK_RET(CheckNotNull(gradOutput, self, gradInput), ACLNN_ERR_PARAM_NULLPTR);
85 87 
86- // 2. 检查gradOutput和self的dtype能否作类型推导,类型推导后的结果需要与gradInput一致88+ // 2. 检查gradOutput和self的dtype能否作类型推导,并检查gradInput的数据类型在支持列表内
87 CHECK_RET(CheckPromoteType(gradOutput, self, gradInput), ACLNN_ERR_PARAM_INVALID);89 CHECK_RET(CheckPromoteType(gradOutput, self, gradInput), ACLNN_ERR_PARAM_INVALID);
88 90 
89 // 3. 检查gradOutput和self以及gradInput的维度匹配关系91 // 3. 检查gradOutput和self以及gradInput的维度匹配关系
@@ -175,12 +175,20 @@ aclnnStatus aclnnSwiGluGrad(
175 <td>传入的yGrad或x或out是空指针。</td>175 <td>传入的yGrad或x或out是空指针。</td>
176 </tr>176 </tr>
177 <tr>177 <tr>
178- <td rowspan="2">ACLNN_ERR_PARAM_INVALID</td>178+ <td>ACLNN_ERR_PARAM_INVALID</td>
179- <td rowspan="2">161002</td>179+ <td>161002</td>
180 <td>yGrad或x或out的数据类型不在支持的范围之内。</td>180 <td>yGrad或x或out的数据类型不在支持的范围之内。</td>
181 </tr>181 </tr>
182 <tr>182 <tr>
183- <td>dim不在取值范围内。</td>183+ <td rowspan="3">ACLNN_ERR_INNER_TILING_ERROR</td>
184+ <td rowspan="3">561002</td>
185+ <td>dim不在取值范围内,或x在dim维的大小不能被2整除。</td>
186+ </tr>
187+ <tr>
188+ <td>yGrad与x的维度数不一致。</td>
189+ </tr>
190+ <tr>
191+ <td>yGrad与x在非dim维的大小不一致,或yGrad在dim维的大小不等于x在该维大小的一半。</td>
184 </tr>192 </tr>
185 </tbody></table>193 </tbody></table>
186 194 
@@ -100,6 +100,25 @@ static bool CheckDtypeValid(const aclTensor* self, const aclTensor* gamma, const
100 "The dtype of gamma must be the same as self or FLOAT"}));100 "The dtype of gamma must be the same as self or FLOAT"}));
101 return false;101 return false;
102 }102 }
103+ } else if (!Ops::NN::AclnnUtil::IsRegbase(npuArch) && (gamma != nullptr || beta != nullptr)) {
104+ // 非RegBase平台单侧为空时,空侧会按self的dtype补齐(OnesLike/ZerosLike),
105+ // 等效dtype组合仍需保持gamma与beta一致,否则会在kernel匹配阶段以内部错误码失败,此处提前按参数错误返回
106+ auto gammaDtype = (gamma != nullptr) ? gamma->GetDataType() : self->GetDataType();
107+ auto betaDtype = (beta != nullptr) ? beta->GetDataType() : self->GetDataType();
108+ if (gammaDtype != betaDtype) {
109+ std::string reason = "The dtype of gamma must be the same as the dtype of beta, because when one of gamma "
110+ "and beta is "
111+ "null, it will be filled with the dtype of self";
112+ OP_LOGE_WITHOUT_REPORT(ACLNN_ERR_PARAM_INVALID, "%s", reason.c_str());
113+ REPORT_PREDEFINED_ERR_MSG(
114+ "EZ0021", (std::vector<const char*>{"param_names", "op_name", "incorrect_dtypes", "reason"}),
115+ (std::vector<const char*>{
116+ "gamma,beta", "aclnnGroupNormSilu",
117+ (std::string(op::ToString(gammaDtype).GetString()) + "," + op::ToString(betaDtype).GetString())
118+ .c_str(),
119+ reason.c_str()}));
120+ return false;
121+ }
103 }122 }
104 // 检查out, meanOut, rstdOut的数据类型是否与self一致123 // 检查out, meanOut, rstdOut的数据类型是否与self一致
105 OP_CHECK_DTYPE_NOT_MATCH(out, self->GetDataType(), return false);124 OP_CHECK_DTYPE_NOT_MATCH(out, self->GetDataType(), return false);
@@ -209,7 +209,10 @@ __aicore__ inline void GroupNormSiluB16<T1, T2>::ComputeOneLoop(const int64_t& l
209 float mean = tmpMean.GetValue(0);209 float mean = tmpMean.GetValue(0);
210 float rstd = x2Ub32.GetValue(0);210 float rstd = x2Ub32.GetValue(0);
211 mean = mean * numRec;211 mean = mean * numRec;
212- rstd = float(1.0) / sqrt(rstd * numRec - mean * mean + tiling->epsilon);212+ // 方差因浮点抵消可能为负,钳位至0避免sqrt产生NaN
213+ float var = rstd * numRec - mean * mean;
214+ var = (var > 0.0f) ? var : 0.0f;
215+ rstd = float(1.0) / sqrt(var + tiling->epsilon);
213 meanUb.SetValue(outIdx, mean);216 meanUb.SetValue(outIdx, mean);
214 rstdUb.SetValue(outIdx, rstd);217 rstdUb.SetValue(outIdx, rstd);
215 event_t eventS2V = static_cast<event_t>(GetTPipePtr()->FetchEventID(HardEvent::S_V));218 event_t eventS2V = static_cast<event_t>(GetTPipePtr()->FetchEventID(HardEvent::S_V));
@@ -253,7 +256,10 @@ __aicore__ inline void GroupNormSiluB16<T1, T2>::ComputeMultipleLoop(const int64
253 float mean = tmpTensor.Get<float>().GetValue(0);256 float mean = tmpTensor.Get<float>().GetValue(0);
254 float rstd = x2Buf32.Get<float>().GetValue(0);257 float rstd = x2Buf32.Get<float>().GetValue(0);
255 mean = mean * numRec;258 mean = mean * numRec;
256- rstd = float(1.0) / sqrt(rstd * numRec - mean * mean + tiling->epsilon);259+ // 方差因浮点抵消可能为负,钳位至0避免sqrt产生NaN
260+ float var = rstd * numRec - mean * mean;
261+ var = (var > 0.0f) ? var : 0.0f;
262+ rstd = float(1.0) / sqrt(var + tiling->epsilon);
257 meanBuf32.Get<float>().SetValue(outIdx, mean);263 meanBuf32.Get<float>().SetValue(outIdx, mean);
258 rstdBuf32.Get<float>().SetValue(outIdx, rstd);264 rstdBuf32.Get<float>().SetValue(outIdx, rstd);
259 // normalize x and calc silu265 // normalize x and calc silu
@@ -459,4 +465,4 @@ __aicore__ inline void GroupNormSiluB16<T1, T2>::CopyOutMeanAndRstd(const int64_
459}465}
460} // namespace GroupNormSilu466} // namespace GroupNormSilu
461 467 
462-#endif // GROUP_NORM_SILU_B16_H_468+#endif // GROUP_NORM_SILU_B16_H_
@@ -174,7 +174,10 @@ __aicore__ inline void GroupNormSiluB32<T>::ComputeOneLoop(const int64_t& loopNu
174 float mean = tmpTensor.Get<float>().GetValue(0);174 float mean = tmpTensor.Get<float>().GetValue(0);
175 float rstd = x1Buf32.Get<float>().GetValue(0);175 float rstd = x1Buf32.Get<float>().GetValue(0);
176 mean = mean * numRec;176 mean = mean * numRec;
177- rstd = float(1.0) / sqrt(rstd * numRec - mean * mean + tiling->epsilon);177+ // 方差因浮点抵消可能为负,钳位至0避免sqrt产生NaN
178+ float var = rstd * numRec - mean * mean;
179+ var = (var > 0.0f) ? var : 0.0f;
180+ rstd = float(1.0) / sqrt(var + tiling->epsilon);
178 meanUb.SetValue(outIdx, mean);181 meanUb.SetValue(outIdx, mean);
179 rstdUb.SetValue(outIdx, rstd);182 rstdUb.SetValue(outIdx, rstd);
180 // normalize x and calc silu183 // normalize x and calc silu
@@ -211,7 +214,10 @@ __aicore__ inline void GroupNormSiluB32<T>::ComputeMultipleLoop(const int64_t& l
211 float mean = tmpTensor.Get<float>().GetValue(0);214 float mean = tmpTensor.Get<float>().GetValue(0);
212 float rstd = x1Buf32.Get<float>().GetValue(0);215 float rstd = x1Buf32.Get<float>().GetValue(0);
213 mean = mean * numRec;216 mean = mean * numRec;
214- rstd = float(1.0) / sqrt(rstd * numRec - mean * mean + tiling->epsilon);217+ // 方差因浮点抵消可能为负,钳位至0避免sqrt产生NaN
218+ float var = rstd * numRec - mean * mean;
219+ var = (var > 0.0f) ? var : 0.0f;
220+ rstd = float(1.0) / sqrt(var + tiling->epsilon);
215 meanUb.SetValue(outIdx, mean);221 meanUb.SetValue(outIdx, mean);
216 rstdUb.SetValue(outIdx, rstd);222 rstdUb.SetValue(outIdx, rstd);
217 // normalize x and calc silu223 // normalize x and calc silu
@@ -406,4 +412,4 @@ __aicore__ inline void GroupNormSiluB32<T>::CopyOutMeanAndRstd(const int64_t& co
406 412 
407} // namespace GroupNormSilu413} // namespace GroupNormSilu
408 414 
409-#endif // GROUP_NORM_SILU_B32_H_415+#endif // GROUP_NORM_SILU_B32_H_
@@ -251,7 +251,10 @@ __aicore__ inline void GroupNormSiluG16<T1, T2>::ComputeGroupNormSilu(const int6
251 float mean = tmpMean.GetValue(0);251 float mean = tmpMean.GetValue(0);
252 float rstd = x2Ub32.GetValue(0);252 float rstd = x2Ub32.GetValue(0);
253 mean = mean * numRec;253 mean = mean * numRec;
254- rstd = float(1.0) / sqrt(rstd * numRec - mean * mean + tiling->epsilon);254+ // 方差因浮点抵消可能为负,钳位至0避免sqrt产生NaN
255+ float var = rstd * numRec - mean * mean;
256+ var = (var > 0.0f) ? var : 0.0f;
257+ rstd = float(1.0) / sqrt(var + tiling->epsilon);
255 meanUb.SetValue(groupIdx % innerNumPerCore, mean);258 meanUb.SetValue(groupIdx % innerNumPerCore, mean);
256 rstdUb.SetValue(groupIdx % innerNumPerCore, rstd);259 rstdUb.SetValue(groupIdx % innerNumPerCore, rstd);
257 // calculating y y = (x- mean) * rstd * gamma + beta260 // calculating y y = (x- mean) * rstd * gamma + beta
@@ -221,7 +221,10 @@ __aicore__ inline void GroupNormSiluG32<T>::ComputeGroupNormSilu(const int64_t&
221 float mean = tmpMean.GetValue(0);221 float mean = tmpMean.GetValue(0);
222 float rstd = x2Ub.GetValue(0);222 float rstd = x2Ub.GetValue(0);
223 mean = mean * numRec;223 mean = mean * numRec;
224- rstd = float(1.0) / sqrt(rstd * numRec - mean * mean + tiling->epsilon);224+ // 方差因浮点抵消可能为负,钳位至0避免sqrt产生NaN
225+ float var = rstd * numRec - mean * mean;
226+ var = (var > 0.0f) ? var : 0.0f;
227+ rstd = float(1.0) / sqrt(var + tiling->epsilon);
225 meanUb.SetValue(groupIdx % innerNumPerCore, mean);228 meanUb.SetValue(groupIdx % innerNumPerCore, mean);
226 rstdUb.SetValue(groupIdx % innerNumPerCore, rstd);229 rstdUb.SetValue(groupIdx % innerNumPerCore, rstd);
227 // calculating y y = (x- mean) * rstd * gamma + beta230 // calculating y y = (x- mean) * rstd * gamma + beta
@@ -371,4 +374,4 @@ __aicore__ inline void GroupNormSiluG32<T>::CopyOutMeanAndRstdAlign(const int64_
371}374}
372} // namespace GroupNormSilu375} // namespace GroupNormSilu
373 376 
374-#endif // GROUP_NORM_SILU_G32_H_377+#endif // GROUP_NORM_SILU_G32_H_
@@ -409,7 +409,10 @@ __aicore__ inline void GroupNormSiluHW1B16<T1, T2>::ComputeForMeanAlign(LocalTen
409 float mean = tmpMean.GetValue(0);409 float mean = tmpMean.GetValue(0);
410 float rstd = x2Ub32.GetValue(0);410 float rstd = x2Ub32.GetValue(0);
411 mean = mean * numRec;411 mean = mean * numRec;
412- rstd = float(1.0) / sqrt(rstd * numRec - mean * mean + tiling->epsilon);412+ // 方差因浮点抵消可能为负,钳位至0避免sqrt产生NaN
413+ float var = rstd * numRec - mean * mean;
414+ var = (var > 0.0f) ? var : 0.0f;
415+ rstd = float(1.0) / sqrt(var + tiling->epsilon);
413 meanUb.SetValue(groups + groupIdx, mean);416 meanUb.SetValue(groups + groupIdx, mean);
414 rstdUb.SetValue(groups + groupIdx, rstd);417 rstdUb.SetValue(groups + groupIdx, rstd);
415 castNum++;418 castNum++;
@@ -501,7 +504,10 @@ __aicore__ inline void GroupNormSiluHW1B16<T1, T2>::ComputeGroupNormSilu(const i
501 float mean = tmpMean.GetValue(0);504 float mean = tmpMean.GetValue(0);
502 float rstd = x2Ub32.GetValue(0);505 float rstd = x2Ub32.GetValue(0);
503 mean = mean * numRec;506 mean = mean * numRec;
504- rstd = float(1.0) / sqrt(rstd * numRec - mean * mean + tiling->epsilon);507+ // 方差因浮点抵消可能为负,钳位至0避免sqrt产生NaN
508+ float var = rstd * numRec - mean * mean;
509+ var = (var > 0.0f) ? var : 0.0f;
510+ rstd = float(1.0) / sqrt(var + tiling->epsilon);
505 meanUb.SetValue(groupIdx % innerNumPerCore, mean);511 meanUb.SetValue(groupIdx % innerNumPerCore, mean);
506 rstdUb.SetValue(groupIdx % innerNumPerCore, rstd);512 rstdUb.SetValue(groupIdx % innerNumPerCore, rstd);
507 513 
@@ -299,7 +299,10 @@ __aicore__ inline void GroupNormSiluHW1B32<T>::ComputeForMeanAlign(LocalTensor<T
299 float mean = tmpMean.GetValue(0);299 float mean = tmpMean.GetValue(0);
300 float rstd = x2Ub.GetValue(0);300 float rstd = x2Ub.GetValue(0);
301 mean = mean * numRec;301 mean = mean * numRec;
302- rstd = float(1.0) / sqrt(rstd * numRec - mean * mean + tiling->epsilon);302+ // 方差因浮点抵消可能为负,钳位至0避免sqrt产生NaN
303+ float var = rstd * numRec - mean * mean;
304+ var = (var > 0.0f) ? var : 0.0f;
305+ rstd = float(1.0) / sqrt(var + tiling->epsilon);
303 meanOut.SetValue(groups + groupIdx, mean);306 meanOut.SetValue(groups + groupIdx, mean);
304 rstdOut.SetValue(groups + groupIdx, rstd);307 rstdOut.SetValue(groups + groupIdx, rstd);
305 }308 }
@@ -365,7 +368,10 @@ __aicore__ inline void GroupNormSiluHW1B32<T>::ComputeGroupNormSilu(const int64_
365 float mean = tmpMean.GetValue(0);368 float mean = tmpMean.GetValue(0);
366 float rstd = x2Ub.GetValue(0);369 float rstd = x2Ub.GetValue(0);
367 mean = mean * numRec;370 mean = mean * numRec;
368- rstd = float(1.0) / sqrt(rstd * numRec - mean * mean + tiling->epsilon);371+ // 方差因浮点抵消可能为负,钳位至0避免sqrt产生NaN
372+ float var = rstd * numRec - mean * mean;
373+ var = (var > 0.0f) ? var : 0.0f;
374+ rstd = float(1.0) / sqrt(var + tiling->epsilon);
369 meanUb.SetValue(groupIdx % innerNumPerCore, mean);375 meanUb.SetValue(groupIdx % innerNumPerCore, mean);
370 rstdUb.SetValue(groupIdx % innerNumPerCore, rstd);376 rstdUb.SetValue(groupIdx % innerNumPerCore, rstd);
371 int64_t StartAddr = gmOffset + groupIdx * tiling->elemNum;377 int64_t StartAddr = gmOffset + groupIdx * tiling->elemNum;
@@ -536,4 +542,4 @@ __aicore__ inline void GroupNormSiluHW1B32<T>::CopyOutMeanAndRstdAlign(const int
536}542}
537} // namespace GroupNormSilu543} // namespace GroupNormSilu
538 544 
539-#endif // GROUP_NORM_SILU_HW1_B32_H_545+#endif // GROUP_NORM_SILU_HW1_B32_H_
@@ -145,7 +145,10 @@ __aicore__ inline void GroupNormSiluSD<T1, T2>::ComputeSilu(const int64_t& group
145 float mean = tmpTensor.Get<float>().GetValue(0);145 float mean = tmpTensor.Get<float>().GetValue(0);
146 float rstd = x2Buf32.Get<float>().GetValue(0);146 float rstd = x2Buf32.Get<float>().GetValue(0);
147 mean = mean * numRec;147 mean = mean * numRec;
148- rstd = float(1.0) / sqrt(rstd * numRec - mean * mean + tiling->epsilon);148+ // 方差因浮点抵消可能为负,钳位至0避免sqrt产生NaN
149+ float var = rstd * numRec - mean * mean;
150+ var = (var > 0.0f) ? var : 0.0f;
151+ rstd = float(1.0) / sqrt(var + tiling->epsilon);
149 // normalize x and calc silu152 // normalize x and calc silu
150 int64_t gammaIdx = group * tiling->shapeD;153 int64_t gammaIdx = group * tiling->shapeD;
151 if (tiling->processSize >= tiling->hwNum) {154 if (tiling->processSize >= tiling->hwNum) {
@@ -337,4 +340,4 @@ __aicore__ inline void GroupNormSiluSD<T1, T2>::CopyOutY(const int64_t& startAdd
337 340 
338} // namespace GroupNormSilu341} // namespace GroupNormSilu
339 342 
340-#endif // GROUP_NORM_SILU_SD_H_343+#endif // GROUP_NORM_SILU_SD_H_
@@ -210,7 +210,10 @@ __aicore__ inline void GroupNormSiluSmallB16<T1, T2>::ComputeWithCopyOnce(const
210 float mean = tmpTensor.Get<float>().GetValue(0);210 float mean = tmpTensor.Get<float>().GetValue(0);
211 float rstd = x2Buf32.Get<float>().GetValue(0);211 float rstd = x2Buf32.Get<float>().GetValue(0);
212 mean = mean * numRec;212 mean = mean * numRec;
213- rstd = float(1.0) / sqrt(rstd * numRec - mean * mean + tiling->epsilon);213+ // 方差因浮点抵消可能为负,钳位至0避免sqrt产生NaN
214+ float var = rstd * numRec - mean * mean;
215+ var = (var > 0.0f) ? var : 0.0f;
216+ rstd = float(1.0) / sqrt(var + tiling->epsilon);
214 meanBuf32.Get<float>().SetValue(outIdx, mean);217 meanBuf32.Get<float>().SetValue(outIdx, mean);
215 rstdBuf32.Get<float>().SetValue(outIdx, rstd);218 rstdBuf32.Get<float>().SetValue(outIdx, rstd);
216 event_t eventS2V = static_cast<event_t>(GetTPipePtr()->FetchEventID(HardEvent::S_V));219 event_t eventS2V = static_cast<event_t>(GetTPipePtr()->FetchEventID(HardEvent::S_V));
@@ -257,7 +260,10 @@ __aicore__ inline void GroupNormSiluSmallB16<T1, T2>::ComputeAlign(const int64_t
257 float mean = tmpTensor.Get<float>().GetValue(0);260 float mean = tmpTensor.Get<float>().GetValue(0);
258 float rstd = x2Buf32.Get<float>().GetValue(0);261 float rstd = x2Buf32.Get<float>().GetValue(0);
259 mean = mean * numRec;262 mean = mean * numRec;
260- rstd = float(1.0) / sqrt(rstd * numRec - mean * mean + tiling->epsilon);263+ // 方差因浮点抵消可能为负,钳位至0避免sqrt产生NaN
264+ float var = rstd * numRec - mean * mean;
265+ var = (var > 0.0f) ? var : 0.0f;
266+ rstd = float(1.0) / sqrt(var + tiling->epsilon);
261 meanBuf32.Get<float>().SetValue(outIdx, mean);267 meanBuf32.Get<float>().SetValue(outIdx, mean);
262 rstdBuf32.Get<float>().SetValue(outIdx, rstd);268 rstdBuf32.Get<float>().SetValue(outIdx, rstd);
263 // normalize x and calc silu269 // normalize x and calc silu
@@ -484,4 +490,4 @@ __aicore__ inline void GroupNormSiluSmallB16<T1, T2>::CopyOutMeanAndRstd(const i
484}490}
485} // namespace GroupNormSilu491} // namespace GroupNormSilu
486 492 
487-#endif // GROUP_NORM_SILU_SMALL_B16_H_493+#endif // GROUP_NORM_SILU_SMALL_B16_H_
@@ -178,7 +178,10 @@ __aicore__ inline void GroupNormSiluSmallB32<T>::ComputeWithCopyOnce(const int64
178 float mean = tmpMean.GetValue(0);178 float mean = tmpMean.GetValue(0);
179 float rstd = x1Ub32.GetValue(0);179 float rstd = x1Ub32.GetValue(0);
180 mean = mean * numRec;180 mean = mean * numRec;
181- rstd = float(1.0) / sqrt(rstd * numRec - mean * mean + tiling->epsilon);181+ // 方差因浮点抵消可能为负,钳位至0避免sqrt产生NaN
182+ float var = rstd * numRec - mean * mean;
183+ var = (var > 0.0f) ? var : 0.0f;
184+ rstd = float(1.0) / sqrt(var + tiling->epsilon);
182 meanUb.SetValue(outIdx, mean);185 meanUb.SetValue(outIdx, mean);
183 rstdUb.SetValue(outIdx, rstd);186 rstdUb.SetValue(outIdx, rstd);
184 // normalize x and calc silu187 // normalize x and calc silu
@@ -218,7 +221,10 @@ __aicore__ inline void GroupNormSiluSmallB32<T>::ComputeAlign(const int64_t& loo
218 float mean = tmpTensor.Get<float>().GetValue(0);221 float mean = tmpTensor.Get<float>().GetValue(0);
219 float rstd = x1Buf32.Get<float>().GetValue(0);222 float rstd = x1Buf32.Get<float>().GetValue(0);
220 mean = mean * numRec;223 mean = mean * numRec;
221- rstd = float(1.0) / sqrt(rstd * numRec - mean * mean + tiling->epsilon);224+ // 方差因浮点抵消可能为负,钳位至0避免sqrt产生NaN
225+ float var = rstd * numRec - mean * mean;
226+ var = (var > 0.0f) ? var : 0.0f;
227+ rstd = float(1.0) / sqrt(var + tiling->epsilon);
222 meanUb.SetValue(outIdx, mean);228 meanUb.SetValue(outIdx, mean);
223 rstdUb.SetValue(outIdx, rstd);229 rstdUb.SetValue(outIdx, rstd);
224 // normalize x and calc silu230 // normalize x and calc silu
@@ -425,4 +431,4 @@ __aicore__ inline void GroupNormSiluSmallB32<T>::CopyOutMeanAndRstd(const int64_
425 431 
426} // namespace GroupNormSilu432} // namespace GroupNormSilu
427 433 
428-#endif // GROUP_NORM_SILU_SMALL_B32_H_434+#endif // GROUP_NORM_SILU_SMALL_B32_H_