Merged
fix(moe): 修复MoeInitRoutingV3/V4动态量化及V3标杆比较 #15813
GuoqhAdd on 17 days ago
fix(moe): 修复MoeInitRoutingV3/V4动态量化及V3标杆比较 #15813
Merged
Merged fromguoqiuhao/ops-transformerfixintocann/ops-transformermaster
Total: 4 files changed+53-5
| @@ -434,6 +434,10 @@ static ge::graphStatus CheckScaleShapeForHif8PerTensor(const gert::InferShapeCon | |||
| 434 | static ge::graphStatus CheckScaleShapeForInt4DynamicQuant(const gert::InferShapeContext* context, | 434 | static ge::graphStatus CheckScaleShapeForInt4DynamicQuant(const gert::InferShapeContext* context, |
| 435 | const gert::Shape* xShape, const gert::Shape* scaleShape) | 435 | const gert::Shape* xShape, const gert::Shape* scaleShape) |
| 436 | { | 436 | { |
| 437 | + // Unknown rank is represented by {-2}; validate once the rank is known. | ||
| 438 | + if (scaleShape->GetDimNum() == DIM_ONE && scaleShape->GetDim(0) == ge::UNKNOWN_DIM_NUM) { | ||
| 439 | + return ge::GRAPH_SUCCESS; | ||
| 440 | + } | ||
| 437 | if (scaleShape->GetDimNum() != DIM_TWO) { | 441 | if (scaleShape->GetDimNum() != DIM_TWO) { |
| 438 | OP_LOGE_FOR_INVALID_SHAPEDIM(context->GetNodeName(), "scale", std::to_string(scaleShape->GetDimNum()), "2"); | 442 | OP_LOGE_FOR_INVALID_SHAPEDIM(context->GetNodeName(), "scale", std::to_string(scaleShape->GetDimNum()), "2"); |
| 439 | return ge::GRAPH_FAILED; | 443 | return ge::GRAPH_FAILED; |
| @@ -405,7 +405,8 @@ __aicore__ inline float MoeGatherOutDynamicQuant<T, QuantT>::ComputeMax(LocalTen | |||
| 405 | inLocal = inputXInQueue_.DeQue<float>(); | 405 | inLocal = inputXInQueue_.DeQue<float>(); |
| 406 | 406 | ||
| 407 | if constexpr (IS_INPUT_SCALE) { | 407 | if constexpr (IS_INPUT_SCALE) { |
| 408 | - DataCopyPad(smoothLocal, quantSmoothGm_[j * perLoopCols_], intriParamsFp32, {false, 0, 0, 0}); | 408 | + DataCopyPad(smoothLocal, quantSmoothGm_[expertIdx * cols_ + j * perLoopCols_], intriParamsFp32, |
| 409 | + {false, 0, 0, 0}); | ||
| 409 | smoothInQueue_.EnQue(smoothLocal); | 410 | smoothInQueue_.EnQue(smoothLocal); |
| 410 | smoothLocal = smoothInQueue_.DeQue<float>(); | 411 | smoothLocal = smoothInQueue_.DeQue<float>(); |
| 411 | } | 412 | } |
| @@ -295,6 +295,14 @@ def _moe_init_routing_v3_numpy( | |||
| 295 | expanded_x = numpy.clip(expanded_x, -128, 127) | 295 | expanded_x = numpy.clip(expanded_x, -128, 127) |
| 296 | expanded_x = expanded_x.astype(numpy.int8) | 296 | expanded_x = expanded_x.astype(numpy.int8) |
| 297 | 297 | ||
| 298 | + elif quant_mode == 1 and h == 0: | ||
| 299 | + # Zero-width inputs still route indices/counts, but the kernel skips | ||
| 300 | + # quantization and leaves expanded_scale unwritten. | ||
| 301 | + expanded_x = x[sorted_expert_indices[:actual_expert_total_num] // k, :].astype( | ||
| 302 | + numpy.int8 | ||
| 303 | + ) | ||
| 304 | + expanded_scale = None | ||
| 305 | + | ||
| 298 | elif quant_mode == 1: | 306 | elif quant_mode == 1: |
| 299 | expanded_x = x[sorted_expert_indices // k, :] | 307 | expanded_x = x[sorted_expert_indices // k, :] |
| 300 | expanded_x = expanded_x.astype(numpy.float32) | 308 | expanded_x = expanded_x.astype(numpy.float32) |
| @@ -1212,6 +1220,32 @@ class E2eMoeInitRoutingV3Spec: | |||
| 1212 | continue | 1220 | continue |
| 1213 | npu_arr = _torch_to_numpy(npu_out) | 1221 | npu_arr = _torch_to_numpy(npu_out) |
| 1214 | golden_arr = _torch_to_numpy(golden_out) | 1222 | golden_arr = _torch_to_numpy(golden_out) |
| 1223 | + if quant_mode == -1 and i in (0, 3): | ||
| 1224 | + # Non-quantized outputs retain the input x/scale FP8 type, | ||
| 1225 | + # even when TTK supplies a uint8 carrier or promotes Golden. | ||
| 1226 | + dtype_ref = npu_arr | ||
| 1227 | + inputs = getattr(compare_context, "input_tensors", None) | ||
| 1228 | + source_idx = 0 if i == 0 else 2 | ||
| 1229 | + if inputs is not None and len(inputs) > source_idx: | ||
| 1230 | + source = inputs[source_idx] | ||
| 1231 | + if any( | ||
| 1232 | + name in str(getattr(source, "dtype", "")).lower() | ||
| 1233 | + for name in ("e4m3", "e5m2", "e8m0") | ||
| 1234 | + ): | ||
| 1235 | + dtype_ref = source | ||
| 1236 | + golden_arr = _restore_fp8_golden_dtype(dtype_ref, golden_arr) | ||
| 1237 | + if any( | ||
| 1238 | + name in str(golden_arr.dtype).lower() | ||
| 1239 | + for name in ("e4m3", "e5m2", "e8m0") | ||
| 1240 | + ): | ||
| 1241 | + if npu_arr.dtype.itemsize == 1 and npu_arr.dtype.kind in ( | ||
| 1242 | + "i", | ||
| 1243 | + "u", | ||
| 1244 | + "V", | ||
| 1245 | + ): | ||
| 1246 | + npu_arr = npu_arr.view(golden_arr.dtype) | ||
| 1247 | + else: | ||
| 1248 | + npu_arr = npu_arr.astype(golden_arr.dtype, copy=False) | ||
| 1215 | npu_flat = numpy.asarray(npu_arr).reshape(-1) | 1249 | npu_flat = numpy.asarray(npu_arr).reshape(-1) |
| 1216 | golden_flat = numpy.asarray(golden_arr).reshape(-1) | 1250 | golden_flat = numpy.asarray(golden_arr).reshape(-1) |
| 1217 | min_len = min(npu_flat.size, golden_flat.size) | 1251 | min_len = min(npu_flat.size, golden_flat.size) |
| @@ -1256,10 +1290,15 @@ class E2eMoeInitRoutingV3Spec: | |||
| 1256 | or "hif8" in str(npu_arr.dtype).lower() | 1290 | or "hif8" in str(npu_arr.dtype).lower() |
| 1257 | ) | 1291 | ) |
| 1258 | 1292 | ||
| 1293 | + is_4bit = any( | ||
| 1294 | + name in dtype_name or name in str(npu_arr.dtype).lower() | ||
| 1295 | + for name in ("float4", "e2m1", "e1m2", "int4") | ||
| 1296 | + ) | ||
| 1297 | + | ||
| 1259 | def _to_raw_uint8(arr): | 1298 | def _to_raw_uint8(arr): |
| 1260 | if arr.dtype.kind in ("i", "u"): | 1299 | if arr.dtype.kind in ("i", "u"): |
| 1261 | vals = arr.reshape(-1).astype(numpy.uint8) | 1300 | vals = arr.reshape(-1).astype(numpy.uint8) |
| 1262 | - if vals.size > 0 and numpy.max(vals) <= 15: | 1301 | + if is_4bit and vals.size > 0 and numpy.max(vals) <= 15: |
| 1263 | n = vals.size - vals.size % 2 | 1302 | n = vals.size - vals.size % 2 |
| 1264 | lo = vals[:n:2] | 1303 | lo = vals[:n:2] |
| 1265 | hi = vals[1:n:2] | 1304 | hi = vals[1:n:2] |
| @@ -1269,15 +1308,15 @@ class E2eMoeInitRoutingV3Spec: | |||
| 1269 | raw = arr.view(numpy.uint8).reshape(-1).astype(numpy.uint8) | 1308 | raw = arr.view(numpy.uint8).reshape(-1).astype(numpy.uint8) |
| 1270 | except (ValueError, TypeError): | 1309 | except (ValueError, TypeError): |
| 1271 | raw = numpy.frombuffer(arr.tobytes(), dtype=numpy.uint8) | 1310 | raw = numpy.frombuffer(arr.tobytes(), dtype=numpy.uint8) |
| 1272 | - if raw.size > 0 and numpy.max(raw) <= 15: | 1311 | + if is_4bit and raw.size > 0 and numpy.max(raw) <= 15: |
| 1273 | n = raw.size - raw.size % 2 | 1312 | n = raw.size - raw.size % 2 |
| 1274 | lo = raw[:n:2] | 1313 | lo = raw[:n:2] |
| 1275 | hi = raw[1:n:2] | 1314 | hi = raw[1:n:2] |
| 1276 | return ((hi << 4) | lo).astype(numpy.uint8) | 1315 | return ((hi << 4) | lo).astype(numpy.uint8) |
| 1277 | return raw | 1316 | return raw |
| 1278 | 1317 | ||
| 1279 | - npu_raw = _to_raw_uint8(numpy.asarray(npu_arr)) | 1318 | + npu_raw = _to_raw_uint8(numpy.asarray(npu_cmp)) |
| 1280 | - golden_raw = _to_raw_uint8(numpy.asarray(golden_arr)) | 1319 | + golden_raw = _to_raw_uint8(numpy.asarray(golden_cmp)) |
| 1281 | min_len_raw = min(npu_raw.size, golden_raw.size) | 1320 | min_len_raw = min(npu_raw.size, golden_raw.size) |
| 1282 | if is_hifloat8: | 1321 | if is_hifloat8: |
| 1283 | # hifloat8: 按 int8 解释,ULP 差 > 1 计为错误,NaN 位置双方一致视为相等 | 1322 | # hifloat8: 按 int8 解释,ULP 差 > 1 计为错误,NaN 位置双方一致视为相等 |
| @@ -415,6 +415,10 @@ static ge::graphStatus CheckScaleShapeForHif8PerTensor(const gert::InferShapeCon | |||
| 415 | static ge::graphStatus CheckScaleShapeForInt4DynamicQuant(const gert::InferShapeContext* context, | 415 | static ge::graphStatus CheckScaleShapeForInt4DynamicQuant(const gert::InferShapeContext* context, |
| 416 | const gert::Shape* xShape, const gert::Shape* scaleShape) | 416 | const gert::Shape* xShape, const gert::Shape* scaleShape) |
| 417 | { | 417 | { |
| 418 | + // Unknown rank is represented by {-2}; validate once the rank is known. | ||
| 419 | + if (scaleShape->GetDimNum() == DIM_ONE && scaleShape->GetDim(0) == ge::UNKNOWN_DIM_NUM) { | ||
| 420 | + return ge::GRAPH_SUCCESS; | ||
| 421 | + } | ||
| 418 | if (scaleShape->GetDimNum() != DIM_TWO) { | 422 | if (scaleShape->GetDimNum() != DIM_TWO) { |
| 419 | OP_LOGE_FOR_INVALID_SHAPEDIM(context->GetNodeName(), "scale", std::to_string(scaleShape->GetDimNum()), "2"); | 423 | OP_LOGE_FOR_INVALID_SHAPEDIM(context->GetNodeName(), "scale", std::to_string(scaleShape->GetDimNum()), "2"); |
| 420 | return ge::GRAPH_FAILED; | 424 | return ge::GRAPH_FAILED; |
【review】 问题分类:可测 位置:moe/moe_init_routing_v3/tests/assets/golden.py:1318,代码:npu_raw = _to_raw_uint8(numpy.asarray(npu_cmp)) 问题:这里改为对 npu_cmp/golden_cmp 做 raw-byte 转换,但二者已经按转换前的 min_len 截断。当 INT4 的设备结果使用 packed uint8 载体、golden 为 ml_dtypes.int4 的逐元素数组时,一个设备字节对应两个 golden 元素,直接取两边元素数的最小值会先丢掉一半 golden,再打包和取 min_len_raw,最终只比较设备结果的前半段。CPU 复现:golden=[1,2,3,4,5,6,7,0],正确 packed 字节为 [0x21,0x43,0x65,0x07];将后两字节改为 [0xFF,0xEE] 后,当前 E2eMoeInitRoutingV3Spec.compare 仍返回 pass=true、precision=100,基线版本则返回失败。 可能导致的结果:INT4 输出后半部分的错误被新增截断隐藏,量化精度回归会被误报为通过。 建议的修改方法:先将两侧统一到相同的逻辑元素或 packed 字节表示,再按有效逻辑长度裁剪和比较;若需忽略输出填充,应显式换算每字节两个元素。增加只破坏后半段 packed 输出的负向自检,断言比较失败。