Merged
修复issue:7976/7997问题 #13786
林泽辉Add on 16 days ago
修复issue:7976/7997问题 #13786
Merged
林泽辉Add on 16 days ago
Total: 4 files changed+92-5
@@ -149,8 +149,10 @@ static bool CheckFormat(const aclTensor* selfTensor, [[maybe_unused]] const aclT
149 149 
150static inline bool CheckBmmResIsEmpty(const aclTensor* self, const aclTensor* mat2)150static inline bool CheckBmmResIsEmpty(const aclTensor* self, const aclTensor* mat2)
151{151{
152- return self->GetViewShape().GetDim(FIRST_DIM) == 0 || self->GetViewShape().GetDim(SECOND_DIM) == 0 ||152+ // batch 维允许单侧为 1 广播,结果 batch = max(self[0], mat2[0]),
153- mat2->GetViewShape().GetDim(THIRD_DIM) == 0;153+ // 仅当 self[0] 与 mat2[0] 同时为 0 时结果才为空,可按空 tensor 早退
yuning_chen
yuning_chenyuning_chen11 days ago

请问 self batch=0 且 mat2 batch>1 时,新的空 tensor 早退是否会漏拦非法广播?

  • 现在代码看来旧逻辑 self[0]==0 就会早退;新逻辑要求 self[0] 与 mat2[0] 同时为 0 才按空处理,注释按 max(self[0], mat2[0]) 理解 batch
  • 可能存在有 0 与大于 1 的 batch 并不满足广播规则(0 也不是 1),却不再早退、继续往下构图或计算的问题
  • 建议可以明确 batch 维合法集合(相等或一侧为 1),对 0 与 N(N>1) 组合直接参数错误,而不是只放宽早退
likedislike
林泽辉
林泽辉
11 days ago Comments:
likedislike
sxb154714
sxb154714
11 days ago Comments:
likedislike
林泽辉
林泽辉
10 days ago Comments:
likedislike
154+ return (self->GetViewShape().GetDim(FIRST_DIM) == 0 && mat2->GetViewShape().GetDim(FIRST_DIM) == 0) ||
155+ self->GetViewShape().GetDim(SECOND_DIM) == 0 || mat2->GetViewShape().GetDim(THIRD_DIM) == 0;
154}156}
155 157 
156static aclnnStatus CheckParamsV2(const aclTensor* self, const aclTensor* mat2, const aclTensor* out,158static aclnnStatus CheckParamsV2(const aclTensor* self, const aclTensor* mat2, const aclTensor* out,
@@ -81,8 +81,9 @@ static inline bool CheckNpuArchIsSupportBf16(void)
81 81 
82static const aclTensor* ProcessEmptyTensor(const aclTensor* self, const aclTensor* mat2, aclOpExecutor* executor)82static const aclTensor* ProcessEmptyTensor(const aclTensor* self, const aclTensor* mat2, aclOpExecutor* executor)
83{83{
84- // 获取shape信息84+ // 获取shape信息:batch 维允许单侧为 1 广播,结果 batch = max(self[0], mat2[0])
85- op::Shape bmmEmptyShape = {(self->GetViewShape())[0], (self->GetViewShape())[1], (mat2->GetViewShape())[2]};85+ op::Shape bmmEmptyShape = {std::max((self->GetViewShape())[0], (mat2->GetViewShape())[0]),
86+ (self->GetViewShape())[1], (mat2->GetViewShape())[2]};
86 auto output = executor->AllocTensor(bmmEmptyShape, self->GetDataType());87 auto output = executor->AllocTensor(bmmEmptyShape, self->GetDataType());
87 OP_CHECK_NULL(output, return nullptr);88 OP_CHECK_NULL(output, return nullptr);
88 if (output->IsEmpty()) {89 if (output->IsEmpty()) {
@@ -175,6 +175,22 @@ static bool CheckShapeValid(const aclTensor* self, const aclTensor* mat2)
175 OP_CHECK_MAX_DIM(self, maxDims, return false);175 OP_CHECK_MAX_DIM(self, maxDims, return false);
176 OP_CHECK_MAX_DIM(mat2, maxDims, return false);176 OP_CHECK_MAX_DIM(mat2, maxDims, return false);
177 177 
178+ // 维度值不能为负
179+ for (size_t i = 0; i < selfShape.GetDimNum(); i++) {
180+ if (selfShape.GetDim(i) < 0) {
181+ OP_LOGE(ACLNN_ERR_PARAM_INVALID, "Dim value of self is negative, shape is [%s].",
182+ op::ToString(selfShape).GetString());
183+ return false;
184+ }
185+ }
186+ for (size_t i = 0; i < mat2Shape.GetDimNum(); i++) {
187+ if (mat2Shape.GetDim(i) < 0) {
188+ OP_LOGE(ACLNN_ERR_PARAM_INVALID, "Dim value of mat2 is negative, shape is [%s].",
189+ op::ToString(mat2Shape).GetString());
190+ return false;
191+ }
192+ }
193+ 
178 // Tensor1 dims number is 0 OR error dims number is 0194 // Tensor1 dims number is 0 OR error dims number is 0
179 if (dimTensor1 == 0 || dimTensor2 == 0) {195 if (dimTensor1 == 0 || dimTensor2 == 0) {
180 OP_LOGE(ACLNN_ERR_PARAM_INVALID, "Matmul does not support %s, %s", op::ToString(selfShape).GetString(),196 OP_LOGE(ACLNN_ERR_PARAM_INVALID, "Matmul does not support %s, %s", op::ToString(selfShape).GetString(),
@@ -226,6 +242,71 @@ static bool CheckFormat(const aclTensor* selfTensor, [[maybe_unused]] const aclT
226 return true;242 return true;
227}243}
228 244 
245+// 校验 out 与 matmul 推导结果的元素数契约:PostProcess 为 Reshape(convOut, out),
246+// 允许 out 的 shape 与推导 shape 不同(如 batch 折叠),但元素总数必须一致
247+static bool CheckOutputShapeValid(const aclTensor* self, const aclTensor* mat2, const aclTensor* out)
248+{
249+ // 维度值不能为负,负维度属非法参数,应在入参阶段拦截
250+ op::Shape outShape = out->GetViewShape();
251+ for (size_t i = 0; i < outShape.GetDimNum(); i++) {
252+ if (outShape.GetDim(i) < 0) {
253+ OP_LOGE(ACLNN_ERR_PARAM_INVALID, "Dim value of out is negative, shape is [%s].",
254+ op::ToString(outShape).GetString());
255+ return false;
256+ }
257+ }
258+ 
259+ // 空输入场景由 MatMulEmptyTensorGraph 按给定 out shape 补零填充(或走空输出早退),不比对元素数
260+ if (self->IsEmpty() || mat2->IsEmpty()) {
261+ return true;
262+ }
263+ 
264+ // 按广播语义推导输出元素个数
265+ op::Shape selfShape = self->GetViewShape();
266+ op::Shape mat2Shape = mat2->GetViewShape();
267+ auto dimTensor1 = selfShape.GetDimNum();
268+ auto dimTensor2 = mat2Shape.GetDimNum();
269+ int64_t expectNumel = 0;
270+ if (dimTensor1 == 1 && dimTensor2 == 1) {
271+ // 1D @ 1D -> dot 输出标量
272+ expectNumel = 1;
273+ } else if (dimTensor1 == 1) {
274+ // 1D @ nD -> (..., n):batch 维取 mat2,移除 self 的维度
275+ int64_t batchNum = 1;
276+ for (size_t idx = 0; idx + 2 < dimTensor2; idx++) {
277+ batchNum *= mat2Shape.GetDim(idx);
278+ }
279+ expectNumel = batchNum * mat2Shape.GetDim(dimTensor2 - 1);
280+ } else if (dimTensor2 == 1) {
281+ // nD @ 1D -> (..., m):batch 维取 self,移除 mat2 的维度
282+ int64_t batchNum = 1;
283+ for (size_t idx = 0; idx + 2 < dimTensor1; idx++) {
284+ batchNum *= selfShape.GetDim(idx);
285+ }
286+ expectNumel = batchNum * selfShape.GetDim(dimTensor1 - 2);
287+ } else {
288+ // nD @ nD -> broadcast(batch 维) + (m, n)
289+ op::Shape broadcastShape;
290+ auto selfBroadcastShape = GetBroadcastShape(self);
291+ auto mat2BroadcastShape = GetBroadcastShape(mat2);
292+ if (!BroadcastInferShape(selfBroadcastShape, mat2BroadcastShape, broadcastShape)) {
293+ OP_LOGE(ACLNN_ERR_PARAM_INVALID, "Broadcast [%s] and [%s] failed.", op::ToString(selfShape).GetString(),
294+ op::ToString(mat2Shape).GetString());
295+ return false;
296+ }
297+ expectNumel = broadcastShape.GetShapeSize() * selfShape.GetDim(dimTensor1 - 2) *
298+ mat2Shape.GetDim(dimTensor2 - 1);
299+ }
300+ 
301+ if (outShape.GetShapeSize() != expectNumel) {
sxb154714
sxb154714sxb15471411 days ago

【检视意见 P2|类型匹配】GetShapeSize() 与 %ld 的类型匹配性建议核实:仓内其他位置对该接口的格式化采用 static_cast + %lld(如 weight_quant_batch_matmul_v2/op_host/op_tiling/arch35/weight_quant_batch_matmul_v2_checker_for_920r1.cpp:333-335),提示其返回类型可能为 uint64_t/size_t。本处 outShape.GetShapeSize() != expectNumel 为与 int64_t 的符号性混合比较,日志 %ld 直接格式化存在 -Wformat 告警风险(LP64 上功能无损)。建议确认 op::Shape::GetShapeSize 返回类型后显式 static_cast<int64_t> 再比较与打印。

likedislike
林泽辉
林泽辉
11 days ago Comments:
likedislike
302+ OP_LOGE(ACLNN_ERR_PARAM_INVALID,
303+ "output's shape [%s] does not match input, element number of out [%ld] should be [%ld].",
304+ op::ToString(outShape).GetString(), outShape.GetShapeSize(), expectNumel);
305+ return false;
306+ }
307+ return true;
308+}
309+ 
229inline static aclnnStatus CheckInputParams(const aclTensor* self, const aclTensor* mat2, const aclTensor* out,310inline static aclnnStatus CheckInputParams(const aclTensor* self, const aclTensor* mat2, const aclTensor* out,
230 int8_t cubeMathType)311 int8_t cubeMathType)
231{312{
@@ -245,6 +326,9 @@ inline static aclnnStatus CheckInputParams(const aclTensor* self, const aclTenso
245 // 4. 检查self和mat2的format是否符合要求326 // 4. 检查self和mat2的format是否符合要求
246 CHECK_RET(CheckFormat(self, mat2, out), ACLNN_ERR_PARAM_INVALID);327 CHECK_RET(CheckFormat(self, mat2, out), ACLNN_ERR_PARAM_INVALID);
247 328 
329+ // 5. 检查out的shape契约:元素数需与推导结果一致(与 PostProcess 的 Reshape 契约对齐)
330+ CHECK_RET(CheckOutputShapeValid(self, mat2, out), ACLNN_ERR_PARAM_INVALID);
331+ 
248 return ACLNN_SUCCESS;332 return ACLNN_SUCCESS;
249}333}
250 334 
@@ -772,7 +772,7 @@ TEST_F(l2_matmul_test, dav3510_test_mm_slice_invalid)
772 772 
773 uint64_t workspace_size = 0;773 uint64_t workspace_size = 0;
774 aclnnStatus aclRet = ut.TestGetWorkspaceSize(&workspace_size);774 aclnnStatus aclRet = ut.TestGetWorkspaceSize(&workspace_size);
775- EXPECT_EQ(aclRet, ACLNN_ERR_INNER_NULLPTR);775+ EXPECT_EQ(aclRet, ACLNN_ERR_PARAM_INVALID);
776}776}
777 777 
778TEST_F(l2_matmul_test, dav3510_test_mm_slice_valid)778TEST_F(l2_matmul_test, dav3510_test_mm_slice_valid)