Merged
AscendRequant质量加固 #13098
renshuojianAdd on 23 days ago
AscendRequant质量加固 #13098
Merged
renshuojianAdd on 23 days ago
Total: 5 files changed+306-26
@@ -127,6 +127,52 @@ int64_t CheckBroadcastShape(const std::vector<std::vector<int64_t>>& paddedIn,
127 return kOk;127 return kOk;
128}128}
129 129 
130+int64_t ValidateBroadcastShape(const std::vector<int64_t>& xShape, const std::vector<int64_t>& scaleShape,
131+ const std::vector<std::vector<int64_t>>& outputShapes)
132+{
133+ const int64_t xRank = static_cast<int64_t>(xShape.size());
134+ const int64_t scaleRank = static_cast<int64_t>(scaleShape.size());
135+ if (xRank < 1 || scaleRank > xRank) {
136+ return kErrRankExceedsX;
137+ }
138+ for (int64_t i = 0; i < scaleRank; i++) {
139+ const int64_t scaleDim = scaleShape[i];
140+ const int64_t xDim = xShape[xRank - scaleRank + i];
yuning_chen
yuning_chenyuning_chen17 days ago

请问这里的下标在 scaleRank 大于 xRank 时是否还能保证落在 xShape 范围内?

  • 现在代码看来 xRank 和 scaleRank 分别来自两个独立传进来的 vector,循环从 i = 0 走到 scaleRank - 1,取的是 xShape[xRank - scaleRank + i];只有当 scaleRank <= xRank 时这个下标才一定非负,函数自身没有做这个前提的判断,xShape 也没有做 empty 判断。
  • 可能存在有越界读的问题,这个 PR 在 infershape 里确实加了 req_scale_shape->GetDimNum() > input_shape->GetDimNum() 的拦截,但那是另一个阶段的检查;ValidateBroadcastShape 是在 tiling 的 CheckShapeInfo 里用 raw_input_shapes_[0] 和 raw_input_shapes_[1] 直接调的,如果走到这里时两者 rank 关系不满足(比如 unknown rank 场景被前面的 xUnknownRank / scaleUnknownRank 分支跳过了 infershape 的那条判断),xRank - scaleRank + i 会是负数,std::vector 的 operator[] 不做检查,直接就是未定义行为。
  • 建议可以在循环前补一句 scaleRank 大于 xRank 时直接返回 kErrShapeMismatch;考虑到这个函数本身的定位就是给 kernel 兜底防越界写,内部自己把前提条件判全会更稳妥,也不用依赖调用方的顺序。
likedislike
renshuojian
renshuojian
16 days ago Comments:
likedislike
141+ if (scaleDim != 1 && scaleDim != xDim) {
142+ return kErrShapeMismatch;
143+ }
144+ }
145+ // 输出 shape 须逐维等于 x 的 shape(README 约束),防止 Kernel 按广播 shape 写入更小的输出。
146+ for (size_t o = 0; o < outputShapes.size(); o++) {
147+ if (static_cast<int64_t>(outputShapes[o].size()) != xRank) {
148+ return kErrShapeMismatch;
149+ }
150+ for (int64_t d = 0; d < xRank; d++) {
151+ if (outputShapes[o][d] != xShape[d]) {
152+ return kErrShapeMismatch;
153+ }
154+ }
155+ }
156+ return kOk;
157+}
158+ 
159+int64_t ValidateTotalElements(const std::vector<int64_t>& maxBroShape)
160+{
161+ __int128 total = 1;
162+ for (size_t d = 0; d < maxBroShape.size(); d++) {
163+ if (maxBroShape[d] < 0) {
164+ return kErrShapeOverflow;
165+ }
166+ total *= static_cast<__int128>(maxBroShape[d]);
167+ if (total > static_cast<__int128>(INT64_MAX)) {
168+ return kErrShapeOverflow;
169+ }
170+ }
171+ return kOk;
172+}
173+ 
174+constexpr int64_t TAIL_LOAD_MARGIN_BYTES = 512;
175+ 
130int64_t FindSplitAxis(const std::vector<int64_t>& maxBroShape, int64_t maxDtypeSize, int64_t ubPerCore,176int64_t FindSplitAxis(const std::vector<int64_t>& maxBroShape, int64_t maxDtypeSize, int64_t ubPerCore,
131 int64_t physNodes, SplitResult& out)177 int64_t physNodes, SplitResult& out)
132{178{
@@ -142,12 +188,20 @@ int64_t FindSplitAxis(const std::vector<int64_t>& maxBroShape, int64_t maxDtypeS
142 188 
143 constexpr int64_t UB_ALIGN_MASK = ~31LL;189 constexpr int64_t UB_ALIGN_MASK = ~31LL;
144 int64_t perBufBytes = (ubPerCore / physNodes) & UB_ALIGN_MASK;190 int64_t perBufBytes = (ubPerCore / physNodes) & UB_ALIGN_MASK;
145- int64_t perBufElems = perBufBytes / maxDtypeSize;191+ int64_t usableBytes = perBufBytes - TAIL_LOAD_MARGIN_BYTES;
192+ if (usableBytes < maxDtypeSize) {
193+ usableBytes = maxDtypeSize;
194+ }
195+ int64_t perBufElems = usableBytes / maxDtypeSize;
196+ if (perBufElems < 1) {
197+ perBufElems = 1;
198+ }
146 int64_t rank = (int64_t)maxBroShape.size();199 int64_t rank = (int64_t)maxBroShape.size();
147- int64_t inner = 1;200+ __int128 inner = 1;
148 for (int64_t k = rank - 1; k >= 0; k--) {201 for (int64_t k = rank - 1; k >= 0; k--) {
149- if (maxBroShape[k] * inner > perBufElems) {202+ if (static_cast<__int128>(maxBroShape[k]) * inner > perBufElems) {
150- out.aI = perBufElems / inner;203+ int64_t inner64 = static_cast<int64_t>(inner);
204+ out.aI = perBufElems / inner64;
151 if (out.aI < 1)205 if (out.aI < 1)
152 out.aI = 1;206 out.aI = 1;
153 out.aO = (maxBroShape[k] + out.aI - 1) / out.aI;207 out.aO = (maxBroShape[k] + out.aI - 1) / out.aI;
@@ -171,10 +225,15 @@ int64_t FindSplitAxis(const std::vector<int64_t>& maxBroShape, int64_t maxDtypeS
171int64_t MultiCoreSplit(const std::vector<int64_t>& maxBroShape, const SplitResult& ubSplit, int64_t maxCores,225int64_t MultiCoreSplit(const std::vector<int64_t>& maxBroShape, const SplitResult& ubSplit, int64_t maxCores,
172 MultiCoreResult& out)226 MultiCoreResult& out)
173{227{
174- int64_t k = ubSplit.axis, outerProd = 1;228+ int64_t k = ubSplit.axis;
229+ __int128 outerProd = 1;
175 for (int64_t j = 0; j < k; j++)230 for (int64_t j = 0; j < k; j++)
176 outerProd *= maxBroShape[j];231 outerProd *= maxBroShape[j];
177- out.totalTiles = outerProd * ubSplit.aO;232+ __int128 totalTiles = outerProd * static_cast<__int128>(ubSplit.aO);
233+ if (totalTiles > static_cast<__int128>(INT64_MAX)) {
234+ return kErrShapeOverflow;
235+ }
236+ out.totalTiles = static_cast<int64_t>(totalTiles);
178 237 
179 if (out.totalTiles == 0) {238 if (out.totalTiles == 0) {
180 out.numCores = 0;239 out.numCores = 0;
@@ -238,17 +297,26 @@ int64_t ComputeBranchTiling(const BranchInputs<RANK>& in, AscendRequantTilingDat
238 int64_t delta = RANK - rank;297 int64_t delta = RANK - rank;
239 298 
240 int64_t perBufBytes = (in.ubPerCore / PHYS_NODES) & UB_ALIGN_MASK;299 int64_t perBufBytes = (in.ubPerCore / PHYS_NODES) & UB_ALIGN_MASK;
241- int64_t perBufElems = perBufBytes / MAX_DTYPE_SIZE;300+ // 与 FindSplitAxis 一致:预留尾块整宽加载余量(A5 VL(uint32)=64,按 uint64 计 512B)。
301+ int64_t usableBytes = perBufBytes - ascend_requant::public_api::TAIL_LOAD_MARGIN_BYTES;
302+ if (usableBytes < MAX_DTYPE_SIZE) {
303+ usableBytes = MAX_DTYPE_SIZE;
304+ }
305+ int64_t perBufElems = usableBytes / MAX_DTYPE_SIZE;
306+ if (perBufElems < 1) {
307+ perBufElems = 1;
308+ }
242 309 
243 if constexpr (RANK > 4) {310 if constexpr (RANK > 4) {
244 }311 }
245 312 
246 SplitResult split{};313 SplitResult split{};
247 {314 {
248- int64_t inner = 1;315+ __int128 inner = 1;
249 for (int64_t k = rank - 1; k >= 0; k--) {316 for (int64_t k = rank - 1; k >= 0; k--) {
250- if (in.maxBroShape[k] * inner > perBufElems) {317+ if (static_cast<__int128>(in.maxBroShape[k]) * inner > perBufElems) {
251- split.aI = perBufElems / inner;318+ int64_t inner64 = static_cast<int64_t>(inner);
319+ split.aI = perBufElems / inner64;
252 if (split.aI < 1)320 if (split.aI < 1)
253 split.aI = 1;321 split.aI = 1;
254 split.aO = (in.maxBroShape[k] + split.aI - 1) / split.aI;322 split.aO = (in.maxBroShape[k] + split.aI - 1) / split.aI;
@@ -270,10 +338,14 @@ int64_t ComputeBranchTiling(const BranchInputs<RANK>& in, AscendRequantTilingDat
270 338 
271 MultiCoreResult mc{};339 MultiCoreResult mc{};
272 {340 {
273- int64_t outerProd = 1;341+ __int128 outerProd = 1;
274 for (int64_t j = 0; j < split.axis; j++)342 for (int64_t j = 0; j < split.axis; j++)
275 outerProd *= in.maxBroShape[j];343 outerProd *= in.maxBroShape[j];
276- mc.totalTiles = outerProd * split.aO;344+ __int128 totalTiles = outerProd * static_cast<__int128>(split.aO);
345+ if (totalTiles > static_cast<__int128>(INT64_MAX)) {
346+ return static_cast<int64_t>(ascend_requant::public_api::kErrShapeOverflow);
347+ }
348+ mc.totalTiles = static_cast<int64_t>(totalTiles);
277 if (mc.totalTiles == 0) {349 if (mc.totalTiles == 0) {
278 mc.numCores = 0;350 mc.numCores = 0;
279 mc.tilesMain = 0;351 mc.tilesMain = 0;
@@ -295,10 +367,10 @@ int64_t ComputeBranchTiling(const BranchInputs<RANK>& in, AscendRequantTilingDat
295 st[d] = 0;367 st[d] = 0;
296 continue;368 continue;
297 }369 }
298- int64_t prod = 1;370+ __int128 prod = 1;
299 for (int64_t j = d + 1; j < r; j++)371 for (int64_t j = d + 1; j < r; j++)
300 prod *= s[j];372 prod *= s[j];
301- st[d] = prod;373+ st[d] = static_cast<int64_t>(prod);
302 }374 }
303 return st;375 return st;
304 };376 };
@@ -386,10 +458,14 @@ static bool PrecomputeStrides(const std::vector<int64_t>& s, std::vector<int64_t
386 strides[d] = 0;458 strides[d] = 0;
387 continue;459 continue;
388 }460 }
389- int64_t prod = 1;461+ // 后缀维度乘积以 __int128 累计并限制在 int64 内,超出则步长无法表示。
462+ __int128 prod = 1;
390 for (int64_t j = d + 1; j < rank; j++)463 for (int64_t j = d + 1; j < rank; j++)
391 prod *= s[j];464 prod *= s[j];
392- strides[d] = prod;465+ if (prod > static_cast<__int128>(INT64_MAX)) {
466+ return false;
467+ }
468+ strides[d] = static_cast<int64_t>(prod);
393 }469 }
394 return true;470 return true;
395}471}
@@ -479,6 +555,13 @@ private:
479 return ge::GRAPH_FAILED;555 return ge::GRAPH_FAILED;
480 }556 }
481 557 
558+ if (ascend_requant::public_api::ValidateBroadcastShape(raw_input_shapes_[0], raw_input_shapes_[1],
559+ raw_output_shapes_) != ascend_requant::public_api::kOk) {
560+ OP_LOGE(ctx_->GetNodeName(),
561+ "req_scale must broadcast to x (each dim 1 or equal to x) and output shape must equal x");
562+ return ge::GRAPH_FAILED;
563+ }
564+ 
482 return ge::GRAPH_SUCCESS;565 return ge::GRAPH_SUCCESS;
483 }566 }
484 567 
@@ -535,6 +618,12 @@ private:
535 return ge::GRAPH_FAILED;618 return ge::GRAPH_FAILED;
536 }619 }
537 620 
621+ // 总元素数超出 int64 可表示范围时,分块数与步长累乘必然溢出,拒绝而非产生错误 tiling。
622+ if (ascend_requant::public_api::ValidateTotalElements(max_bro_shape_) != ascend_requant::public_api::kOk) {
623+ OP_LOGE(ctx_->GetNodeName(), "total elements of broadcast shape exceed int64 range");
624+ return ge::GRAPH_FAILED;
625+ }
626+ 
538 OP_LOGI(ctx_->GetNodeName(), "GetShapeInfo done rank %lld dtype %lld ub %llu core %llu relu_flag %lld", rank_,627 OP_LOGI(ctx_->GetNodeName(), "GetShapeInfo done rank %lld dtype %lld ub %llu core %llu relu_flag %lld", rank_,
539 dtype_size_, ubSize_, coreNum_, reluFlag_);628 dtype_size_, ubSize_, coreNum_, reluFlag_);
540 629 
@@ -574,9 +663,18 @@ private:
574 int64_t per_buf_bytes = (ub_per_core / PHYS_NODES) & ~31LL;663 int64_t per_buf_bytes = (ub_per_core / PHYS_NODES) & ~31LL;
575 664 
576 constexpr int64_t MAX_DTYPE_SIZE = 8;665 constexpr int64_t MAX_DTYPE_SIZE = 8;
577- ascend_requant::public_api::FindSplitAxis(max_bro_shape_, MAX_DTYPE_SIZE, ub_per_core, PHYS_NODES,666+ int64_t splitRet = ascend_requant::public_api::FindSplitAxis(max_bro_shape_, MAX_DTYPE_SIZE, ub_per_core,
578- tiling->split);667+ PHYS_NODES, tiling->split);
579- ascend_requant::public_api::MultiCoreSplit(max_bro_shape_, tiling->split, (int64_t)coreNum_, tiling->multicore);668+ if (splitRet != ascend_requant::public_api::kOk) {
669+ OP_LOGE(ctx_->GetNodeName(), "FindSplitAxis failed: %lld", splitRet);
670+ return ge::GRAPH_FAILED;
671+ }
672+ int64_t mcRet = ascend_requant::public_api::MultiCoreSplit(max_bro_shape_, tiling->split,
673+ static_cast<int64_t>(coreNum_), tiling->multicore);
674+ if (mcRet != ascend_requant::public_api::kOk) {
675+ OP_LOGE(ctx_->GetNodeName(), "MultiCoreSplit failed: %lld", mcRet);
676+ return ge::GRAPH_FAILED;
677+ }
580 if (tiling->multicore.numCores < 1) {678 if (tiling->multicore.numCores < 1) {
581 tiling->multicore.numCores = 1;679 tiling->multicore.numCores = 1;
582 }680 }
@@ -585,10 +683,18 @@ private:
585 int64_t num_in = (int64_t)normal_input_shapes_.size();683 int64_t num_in = (int64_t)normal_input_shapes_.size();
586 int64_t num_out = (int64_t)normal_output_shapes_.size();684 int64_t num_out = (int64_t)normal_output_shapes_.size();
587 std::vector<std::vector<int64_t>> in_strides(num_in), out_strides(num_out);685 std::vector<std::vector<int64_t>> in_strides(num_in), out_strides(num_out);
588- for (int64_t i = 0; i < num_in; i++)686+ for (int64_t i = 0; i < num_in; i++) {
589- PrecomputeStrides(normal_input_shapes_[i], in_strides[i]);687+ if (!PrecomputeStrides(normal_input_shapes_[i], in_strides[i])) {
590- for (int64_t i = 0; i < num_out; i++)688+ OP_LOGE(ctx_->GetNodeName(), "input strides exceed int64 range");
591- PrecomputeStrides(normal_output_shapes_[i], out_strides[i]);689+ return ge::GRAPH_FAILED;
690+ }
691+ }
692+ for (int64_t i = 0; i < num_out; i++) {
693+ if (!PrecomputeStrides(normal_output_shapes_[i], out_strides[i])) {
694+ OP_LOGE(ctx_->GetNodeName(), "output strides exceed int64 range");
695+ return ge::GRAPH_FAILED;
696+ }
697+ }
592 698 
593 tiling->rank = rank_;699 tiling->rank = rank_;
594 tiling->reluFlag = reluFlag_;700 tiling->reluFlag = reluFlag_;
@@ -33,6 +33,7 @@ constexpr int64_t kErrDtypeNotSupported = -2;
33constexpr int64_t kErrFormatNotSupported = -3;33constexpr int64_t kErrFormatNotSupported = -3;
34constexpr int64_t kErrDimOutOfRange = -4;34constexpr int64_t kErrDimOutOfRange = -4;
35constexpr int64_t kErrRankExceedsX = -5;35constexpr int64_t kErrRankExceedsX = -5;
36+constexpr int64_t kErrShapeOverflow = -6;
36constexpr int64_t kOk = 0;37constexpr int64_t kOk = 0;
37 38 
38int64_t PadAndSqueeze(const std::vector<std::vector<int64_t>>& inputShapes,39int64_t PadAndSqueeze(const std::vector<std::vector<int64_t>>& inputShapes,
@@ -43,6 +44,11 @@ int64_t PadAndSqueeze(const std::vector<std::vector<int64_t>>& inputShapes,
43int64_t CheckBroadcastShape(const std::vector<std::vector<int64_t>>& paddedIn,44int64_t CheckBroadcastShape(const std::vector<std::vector<int64_t>>& paddedIn,
44 const std::vector<std::vector<int64_t>>& paddedOut, int64_t maxRank);45 const std::vector<std::vector<int64_t>>& paddedOut, int64_t maxRank);
45 46 
47+int64_t ValidateBroadcastShape(const std::vector<int64_t>& xShape, const std::vector<int64_t>& scaleShape,
48+ const std::vector<std::vector<int64_t>>& outputShapes);
49+ 
50+int64_t ValidateTotalElements(const std::vector<int64_t>& maxBroShape);
51+ 
46int64_t FindSplitAxis(const std::vector<int64_t>& maxBroShape, int64_t maxDtypeSize, int64_t ubPerCore,52int64_t FindSplitAxis(const std::vector<int64_t>& maxBroShape, int64_t maxDtypeSize, int64_t ubPerCore,
47 int64_t physNodes, SplitResult& out);53 int64_t physNodes, SplitResult& out);
48 54 
@@ -30,8 +30,19 @@ static ge::graphStatus InferShape4AscendRequant(gert::InferShapeContext* context
30 const auto is_unknown_rank = [](const gert::Shape& s) -> bool {30 const auto is_unknown_rank = [](const gert::Shape& s) -> bool {
31 return s.GetDimNum() == 1U && s.GetDim(0U) == ge::UNKNOWN_DIM_NUM;31 return s.GetDimNum() == 1U && s.GetDim(0U) == ge::UNKNOWN_DIM_NUM;
32 };32 };
33- if (!is_unknown_rank(*input_shape) && !is_unknown_rank(*req_scale_shape) &&33+ const bool xUnknownRank = is_unknown_rank(*input_shape);
34- req_scale_shape->GetDimNum() > input_shape->GetDimNum()) {34+ const bool scaleUnknownRank = is_unknown_rank(*req_scale_shape);
35+ 
36+ if (!xUnknownRank && (input_shape->GetDimNum() < 1U || input_shape->GetDimNum() > 8U)) {
37+ OP_LOGE(context->GetNodeName(), "rank(x)[%zu] must be in [1, 8].", input_shape->GetDimNum());
38+ return ge::GRAPH_FAILED;
39+ }
40+ if (!scaleUnknownRank && req_scale_shape->GetDimNum() > 8U) {
41+ OP_LOGE(context->GetNodeName(), "rank(req_scale)[%zu] must be in [0, 8].", req_scale_shape->GetDimNum());
42+ return ge::GRAPH_FAILED;
43+ }
44+ 
45+ if (!xUnknownRank && !scaleUnknownRank && req_scale_shape->GetDimNum() > input_shape->GetDimNum()) {
35 OP_LOGE(context->GetNodeName(),46 OP_LOGE(context->GetNodeName(),
36 "rank(req_scale)[%zu] must be <= rank(x)[%zu], "47 "rank(req_scale)[%zu] must be <= rank(x)[%zu], "
37 "req_scale is not broadcastable to x (shape_mismatch).",48 "req_scale is not broadcastable to x (shape_mismatch).",
@@ -39,6 +50,32 @@ static ge::graphStatus InferShape4AscendRequant(gert::InferShapeContext* context
39 return ge::GRAPH_FAILED;50 return ge::GRAPH_FAILED;
40 }51 }
41 52 
53+ if (!xUnknownRank && !scaleUnknownRank) {
54+ const size_t xRank = input_shape->GetDimNum();
55+ const size_t scaleRank = req_scale_shape->GetDimNum();
56+ if (xRank < 1U || scaleRank > xRank) {
57+ OP_LOGE(context->GetNodeName(),
58+ "rank(req_scale)[%zu] must be in [0, rank(x)[%zu]], req_scale is not broadcastable to x.",
59+ scaleRank, xRank);
60+ return ge::GRAPH_FAILED;
61+ }
62+ const auto is_unknown_dim = [](int64_t dim) -> bool { return dim == ge::UNKNOWN_DIM; };
63+ for (size_t i = 0; i < scaleRank; i++) {
64+ const int64_t xDim = input_shape->GetDim(xRank - scaleRank + i);
65+ const int64_t scaleDim = req_scale_shape->GetDim(i);
66+ if (is_unknown_dim(xDim) || is_unknown_dim(scaleDim)) {
67+ continue;
68+ }
69+ if (scaleDim != 1 && scaleDim != xDim) {
70+ OP_LOGE(context->GetNodeName(),
71+ "req_scale dim[%zu]=%lld is neither 1 nor equal to x dim[%zu]=%lld; "
72+ "req_scale must broadcast to x (broadcast_result_must_equal_x).",
73+ i, scaleDim, xRank - scaleRank + i, xDim);
74+ return ge::GRAPH_FAILED;
75+ }
76+ }
77+ }
78+ 
42 *output_shape = *input_shape;79 *output_shape = *input_shape;
43 80 
44 return ge::GRAPH_SUCCESS;81 return ge::GRAPH_SUCCESS;
@@ -291,9 +291,12 @@ __simd_vf__ inline void RequantVF(__ubuf__ YT* dstAddr, __ubuf__ XT* src0Addr, _
291 AscendC::Reg::RegTensor<YT> yReg;291 AscendC::Reg::RegTensor<YT> yReg;
292 AscendC::Reg::MaskReg mask;292 AscendC::Reg::MaskReg mask;
293 AscendC::Reg::MaskReg negMask;293 AscendC::Reg::MaskReg negMask;
294+ AscendC::Reg::MaskReg storeMask;
294 295 
295 static constexpr float kUint64AsFloat = static_cast<float>(uint64_t(1) << 63) * 2.0f;296 static constexpr float kUint64AsFloat = static_cast<float>(uint64_t(1) << 63) * 2.0f;
296 297 
298+ constexpr uint32_t kCopyOutAlignElems = 32U / sizeof(YT);
299+ 
297 uint32_t sreg = count;300 uint32_t sreg = count;
298 for (uint16_t i = 0; i < repeatTimes; ++i) {301 for (uint16_t i = 0; i < repeatTimes; ++i) {
299 mask = AscendC::Reg::UpdateMask<uint32_t>(sreg);302 mask = AscendC::Reg::UpdateMask<uint32_t>(sreg);
@@ -313,6 +316,15 @@ __simd_vf__ inline void RequantVF(__ubuf__ YT* dstAddr, __ubuf__ XT* src0Addr, _
313 if constexpr (kApplyMaxZero) {316 if constexpr (kApplyMaxZero) {
314 AscendC::Reg::Maxs(yReg, yReg, YT(0), mask);317 AscendC::Reg::Maxs(yReg, yReg, YT(0), mask);
315 }318 }
316- AscendC::Reg::StoreAlign<YT, AscendC::Reg::StoreDist::DIST_PACK4_B32>(dstAddr + i * oneRepeatSize, yReg, mask);319+ uint32_t storeCount = static_cast<uint32_t>(i + 1) * oneRepeatSize;
320+ uint32_t storeElems = (storeCount > count) ? (count - static_cast<uint32_t>(i) * oneRepeatSize) : oneRepeatSize;
321+ if (i + 1 == repeatTimes) {
322+ uint32_t aligned = (storeElems + kCopyOutAlignElems - 1U) / kCopyOutAlignElems * kCopyOutAlignElems;
323+ storeElems = (aligned < oneRepeatSize) ? aligned : oneRepeatSize;
324+ }
325+ uint32_t storeCnt = storeElems;
326+ storeMask = AscendC::Reg::UpdateMask<uint32_t>(storeCnt);
327+ AscendC::Reg::StoreAlign<YT, AscendC::Reg::StoreDist::DIST_PACK4_B32>(dstAddr + i * oneRepeatSize, yReg,
328+ storeMask);
317 }329 }
318}330}
@@ -544,3 +544,122 @@ TEST_F(AscendRequantTilingTest, tiling_shape_mismatch)
544 .Build();544 .Build();
545 EXPECT_NE(tilingFunc(holder.GetContext<gert::TilingContext>()), ge::GRAPH_SUCCESS);545 EXPECT_NE(tilingFunc(holder.GetContext<gert::TilingContext>()), ge::GRAPH_SUCCESS);
546}546}
547+ 
548+TEST_F(AscendRequantTilingTest, tiling_broadcast_amplification_rejected)
549+{
550+ fe::PlatFormInfos platformInfo;
551+ map<string, string> socInfos, aicoreSpec, intrinsics;
552+ SetupPlatform(platformInfo, socInfos, aicoreSpec, intrinsics);
553+ AscendRequantCompileInfoForTest compileInfo;
554+ gert::KernelRunContextHolder kernelHolder;
555+ SetupKernelHolder(platformInfo, compileInfo, socInfos, aicoreSpec, intrinsics, kernelHolder);
556+ 
557+ gert::StorageShape xShape = {{1, 4}, {1, 4}};
558+ gert::StorageShape sShape = {{3, 1}, {3, 1}};
559+ gert::StorageShape yShape = {{1, 4}, {1, 4}};
560+ 
561+ auto tilingFunc = gert::OpImplRegistry::GetInstance().GetOpImpl("AscendRequant")->tiling;
562+ ASSERT_NE(tilingFunc, nullptr);
563+ 
564+ auto param = gert::TilingData::CreateCap(4096);
565+ auto wsHolder = gert::ContinuousVector::Create<size_t>(4096);
566+ auto* wsSize = reinterpret_cast<gert::ContinuousVector*>(wsHolder.get());
567+ 
568+ bool reluFlag = false;
569+ auto holder = gert::TilingContextFaker()
570+ .SetOpType("AscendRequant")
571+ .NodeIoNum(2, 1)
572+ .IrInstanceNum({1, 1})
573+ .InputShapes({&xShape, &sShape})
574+ .OutputShapes({&yShape})
575+ .CompileInfo(&compileInfo)
576+ .PlatformInfo(reinterpret_cast<char*>(&platformInfo))
577+ .NodeInputTd(0, ge::DT_INT32, ge::FORMAT_ND, ge::FORMAT_ND)
578+ .NodeInputTd(1, ge::DT_UINT64, ge::FORMAT_ND, ge::FORMAT_ND)
579+ .NodeOutputTd(0, ge::DT_INT8, ge::FORMAT_ND, ge::FORMAT_ND)
580+ .NodeAttrs({{"relu_flag", Ops::NN::AnyValue::CreateFrom<bool>(reluFlag)}})
581+ .TilingData(param.get())
582+ .Workspace(wsSize)
583+ .Build();
584+ EXPECT_NE(tilingFunc(holder.GetContext<gert::TilingContext>()), ge::GRAPH_SUCCESS);
585+}
586+ 
587+TEST_F(AscendRequantTilingTest, tiling_output_shape_mismatch_rejected)
588+{
589+ fe::PlatFormInfos platformInfo;
590+ map<string, string> socInfos, aicoreSpec, intrinsics;
591+ SetupPlatform(platformInfo, socInfos, aicoreSpec, intrinsics);
592+ AscendRequantCompileInfoForTest compileInfo;
593+ gert::KernelRunContextHolder kernelHolder;
594+ SetupKernelHolder(platformInfo, compileInfo, socInfos, aicoreSpec, intrinsics, kernelHolder);
595+ 
596+ gert::StorageShape xShape = {{2, 3}, {2, 3}};
597+ gert::StorageShape sShape = {{2, 3}, {2, 3}};
598+ gert::StorageShape yShape = {{2, 4}, {2, 4}};
atomgit-bot
atomgit-botatomgit-bot23 days ago

🟡 Medium Priority

变更行:新增用例 tiling_output_shape_mismatch_rejected(x=[2,3]、req_scale=[2,3]、y=[2,4]),注释声称其回归验证“输出 shape 须逐维等于 x 的 shape”。

问题链路:tiling 流程为 ValidateInputs()(内含新增的 ValidateBroadcastShape)→ 之后才 PadAndSqueeze()。该用例的 y 维 4 与 x 维 3 已构成双向 numpy 广播冲突,会在 PadAndSqueeze 内部的 CheckBroadcastShape(ascend_requant_tiling_arch35.cpp:59、118-125)就返回 kErrShapeMismatch 并先行失败,因此执行流根本走不到新增的 ValidateBroadcastShape 输出校验(ascend_requant_tiling_arch35.cpp:146-155)。

失败模式:把新增的输出 shape 校验整段删掉,本用例依然 EXPECT_NE(..., GRAPH_SUCCESS) 通过——即该 PR 最关键的安全修复之一(防止 kernel 按广播后的更大 shape 写更小的输出 buffer 越界写)没有任何用例能捕获其回归。现有其它成功用例的 y 均等于 x,也无法覆盖该分支。

建议:改为“广播本身合法、但输出 shape 与 x 不等”的输入,使 PadAndSqueeze/CheckBroadcastShape 通过、只由新增的输出校验拒绝,例如 x=[2,3]、req_scale=[1,1]、y=[1,3](y 维 1 与 x 维 2 广播兼容,但输出不等于 x,能被 ValidateBroadcastShape 的 outputShapes 分支捕获)。

Suggestions
598
- gert::StorageShape yShape = {{2, 4}, {2, 4}};
598
+ gert::StorageShape xShape = {{2, 3}, {2, 3}};
599
+ gert::StorageShape sShape = {{1, 1}, {1, 1}};
600
+ gert::StorageShape yShape = {{1, 3}, {1, 3}};
Apply suggestion
likedislike
Incorrect?
599+ 
600+ auto tilingFunc = gert::OpImplRegistry::GetInstance().GetOpImpl("AscendRequant")->tiling;
601+ ASSERT_NE(tilingFunc, nullptr);
602+ 
603+ auto param = gert::TilingData::CreateCap(4096);
604+ auto wsHolder = gert::ContinuousVector::Create<size_t>(4096);
605+ auto* wsSize = reinterpret_cast<gert::ContinuousVector*>(wsHolder.get());
606+ 
607+ bool reluFlag = false;
608+ auto holder = gert::TilingContextFaker()
609+ .SetOpType("AscendRequant")
610+ .NodeIoNum(2, 1)
611+ .IrInstanceNum({1, 1})
612+ .InputShapes({&xShape, &sShape})
613+ .OutputShapes({&yShape})
614+ .CompileInfo(&compileInfo)
615+ .PlatformInfo(reinterpret_cast<char*>(&platformInfo))
616+ .NodeInputTd(0, ge::DT_INT32, ge::FORMAT_ND, ge::FORMAT_ND)
617+ .NodeInputTd(1, ge::DT_UINT64, ge::FORMAT_ND, ge::FORMAT_ND)
618+ .NodeOutputTd(0, ge::DT_INT8, ge::FORMAT_ND, ge::FORMAT_ND)
619+ .NodeAttrs({{"relu_flag", Ops::NN::AnyValue::CreateFrom<bool>(reluFlag)}})
620+ .TilingData(param.get())
621+ .Workspace(wsSize)
622+ .Build();
623+ EXPECT_NE(tilingFunc(holder.GetContext<gert::TilingContext>()), ge::GRAPH_SUCCESS);
624+}
625+ 
626+TEST_F(AscendRequantTilingTest, tiling_total_elements_overflow_rejected)
627+{
628+ fe::PlatFormInfos platformInfo;
629+ map<string, string> socInfos, aicoreSpec, intrinsics;
630+ SetupPlatform(platformInfo, socInfos, aicoreSpec, intrinsics);
631+ AscendRequantCompileInfoForTest compileInfo;
632+ gert::KernelRunContextHolder kernelHolder;
633+ SetupKernelHolder(platformInfo, compileInfo, socInfos, aicoreSpec, intrinsics, kernelHolder);
634+ 
635+ gert::StorageShape xShape = {{4294967296LL, 4294967296LL, 4294967296LL, 10},
636+ {4294967296LL, 4294967296LL, 4294967296LL, 10}};
637+ gert::StorageShape sShape = {{1}, {1}};
638+ gert::StorageShape yShape = {{4294967296LL, 4294967296LL, 4294967296LL, 10},
639+ {4294967296LL, 4294967296LL, 4294967296LL, 10}};
640+ 
641+ auto tilingFunc = gert::OpImplRegistry::GetInstance().GetOpImpl("AscendRequant")->tiling;
642+ ASSERT_NE(tilingFunc, nullptr);
643+ 
644+ auto param = gert::TilingData::CreateCap(4096);
645+ auto wsHolder = gert::ContinuousVector::Create<size_t>(4096);
646+ auto* wsSize = reinterpret_cast<gert::ContinuousVector*>(wsHolder.get());
647+ 
648+ bool reluFlag = false;
649+ auto holder = gert::TilingContextFaker()
650+ .SetOpType("AscendRequant")
651+ .NodeIoNum(2, 1)
652+ .IrInstanceNum({1, 1})
653+ .InputShapes({&xShape, &sShape})
654+ .OutputShapes({&yShape})
655+ .CompileInfo(&compileInfo)
656+ .PlatformInfo(reinterpret_cast<char*>(&platformInfo))
657+ .NodeInputTd(0, ge::DT_INT32, ge::FORMAT_ND, ge::FORMAT_ND)
658+ .NodeInputTd(1, ge::DT_UINT64, ge::FORMAT_ND, ge::FORMAT_ND)
659+ .NodeOutputTd(0, ge::DT_INT8, ge::FORMAT_ND, ge::FORMAT_ND)
660+ .NodeAttrs({{"relu_flag", Ops::NN::AnyValue::CreateFrom<bool>(reluFlag)}})
661+ .TilingData(param.get())
662+ .Workspace(wsSize)
663+ .Build();
664+ EXPECT_NE(tilingFunc(holder.GetContext<gert::TilingContext>()), ge::GRAPH_SUCCESS);
665+}