Merged
fix(l2_normalize): 恢复普通 FP32 开方精度并补齐 FP16 边界回归 #13500
raoliang_sacAdd on 18 days ago
fix(l2_normalize): 恢复普通 FP32 开方精度并补齐 FP16 边界回归 #13500
Merged
Total: 3 files changed+16-3
| @@ -72,7 +72,8 @@ constexpr AscendC::Reg::CastTrait CAST_TRAIT_FROM_FP32_FP16{ | |||
| 72 | AscendC::RoundMode::CAST_RINT}; | 72 | AscendC::RoundMode::CAST_RINT}; |
| 73 | 73 | ||
| 74 | // 高精度模式(设计文档 §7「精度处理」认可的备选:ST 实测超差时切 0-ULP,不换数据流): | 74 | // 高精度模式(设计文档 §7「精度处理」认可的备选:ST 实测超差时切 0-ULP,不换数据流): |
| 75 | -// FP16 uses 0-ULP FTZ_FALSE. FP32 uses the scaled norm helpers below. | 75 | +// FP16 and ordinary FP32 norms use 0-ULP FTZ_FALSE. FP32 high/low retries |
| 76 | +// use the scaled 1-ULP FTZ_FALSE helpers below; FP32 Div remains 1-ULP FTZ_FALSE. | ||
| 76 | // 默认 INTRINSIC Sqrt 对 subnormal 输入 FTZ | 77 | // 默认 INTRINSIC Sqrt 对 subnormal 输入 FTZ |
| 77 | // 刷 0,eps<=0 且 x² 落 fp32 subnormal 窗口(|x|<1.08e-19)时 denom 被刷成 0 → | 78 | // 刷 0,eps<=0 且 x² 落 fp32 subnormal 窗口(|x|<1.08e-19)时 denom 被刷成 0 → |
| 78 | // y=±inf,与 golden(numpy IEEE 保留 subnormal)NaN/Inf mismatch(FR-P1-001 L1_094)。 | 79 | // y=±inf,与 golden(numpy IEEE 保留 subnormal)NaN/Inf mismatch(FR-P1-001 L1_094)。 |
| @@ -439,7 +440,7 @@ __simd_vf__ inline void FinalizeSumVfImpl(__ubuf__ float* sPtr, __ubuf__ float* | |||
| 439 | } | 440 | } |
| 440 | 441 | ||
| 441 | // S6+S7 PostElewise:Maxs(eps) + Sqrt 同一 VF(全 fp32 域;eps 钳在平方和上,§9.6) | 442 | // S6+S7 PostElewise:Maxs(eps) + Sqrt 同一 VF(全 fp32 域;eps 钳在平方和上,§9.6) |
| 442 | -// Sqrt 恒 0-ULP FTZ_FALSE(IEEE 正确舍入,subnormal 输入不刷零,见 SQRT_MODE_0ULP 注释)。 | 443 | +// FP16 input path only: Sqrt uses 0-ULP FTZ_FALSE on the FP32 intermediate. |
| 443 | __simd_vf__ inline void PostElewiseVfImpl(__ubuf__ float* rootPtr, __ubuf__ float* denomPtr, uint32_t laneN, float eps, | 444 | __simd_vf__ inline void PostElewiseVfImpl(__ubuf__ float* rootPtr, __ubuf__ float* denomPtr, uint32_t laneN, float eps, |
| 444 | uint16_t repeatTime) | 445 | uint16_t repeatTime) |
| 445 | { | 446 | { |
| @@ -31,6 +31,12 @@ constexpr float SCALE_LOW_MARKER = -2.0f; | |||
| 31 | constexpr uint32_t SCALE_LANES = 64; | 31 | constexpr uint32_t SCALE_LANES = 64; |
| 32 | constexpr AscendC::Reg::DivSpecificMode SCALE_DIV_MODE{AscendC::Reg::MaskMergeMode::ZEROING, false, | 32 | constexpr AscendC::Reg::DivSpecificMode SCALE_DIV_MODE{AscendC::Reg::MaskMergeMode::ZEROING, false, |
| 33 | AscendC::DivAlgo::PRECISION_1ULP_FTZ_FALSE}; | 33 | AscendC::DivAlgo::PRECISION_1ULP_FTZ_FALSE}; |
| 34 | +// Retained finite norms in the first pass have sums in [2^-120, 2^124), | ||
| 35 | +// below the fast-inverse sqrt limit 2^126. Keep their original 0-ULP mode. | ||
| 36 | +// High/low lanes become retry markers; rescaled passes retain the full-range | ||
| 37 | +// 1-ULP FTZ_FALSE mode independently of the ordinary-input precision choice. | ||
| 38 | +constexpr AscendC::Reg::SqrtSpecificMode NORMAL_SQRT_MODE_0ULP{AscendC::Reg::MaskMergeMode::ZEROING, true, | ||
| 39 | + AscendC::SqrtAlgo::PRECISION_0ULP_FTZ_FALSE}; | ||
| 34 | constexpr AscendC::Reg::SqrtSpecificMode SCALE_SQRT_MODE{AscendC::Reg::MaskMergeMode::ZEROING, false, | 40 | constexpr AscendC::Reg::SqrtSpecificMode SCALE_SQRT_MODE{AscendC::Reg::MaskMergeMode::ZEROING, false, |
| 35 | AscendC::SqrtAlgo::PRECISION_1ULP_FTZ_FALSE}; | 41 | AscendC::SqrtAlgo::PRECISION_1ULP_FTZ_FALSE}; |
| 36 | 42 | ||
| @@ -50,7 +56,7 @@ __simd_vf__ inline void PrepareScaledNormVf(__ubuf__ float* sums, __ubuf__ float | |||
| 50 | Reg::Maxs(sum, sum, eps, mask); | 56 | Reg::Maxs(sum, sum, eps, mask); |
| 51 | Reg::Compares<float, CMPMODE::GE>(high, sum, SCALE_SUM_HIGH, mask); | 57 | Reg::Compares<float, CMPMODE::GE>(high, sum, SCALE_SUM_HIGH, mask); |
| 52 | Reg::Compares<float, CMPMODE::LT>(low, sum, SCALE_SUM_LOW, mask); | 58 | Reg::Compares<float, CMPMODE::LT>(low, sum, SCALE_SUM_LOW, mask); |
| 53 | - Reg::Sqrt<float, &SCALE_SQRT_MODE>(norm, sum, mask); | 59 | + Reg::Sqrt<float, &NORMAL_SQRT_MODE_0ULP>(norm, sum, mask); |
| 54 | Reg::Select(norm, highMarker, norm, high); | 60 | Reg::Select(norm, highMarker, norm, high); |
| 55 | Reg::Select(norm, lowMarker, norm, low); | 61 | Reg::Select(norm, lowMarker, norm, low); |
| 56 | Reg::StoreAlign(denom + off, norm, mask); | 62 | Reg::StoreAlign(denom + off, norm, mask); |
| @@ -27,3 +27,9 @@ l2_fp16_group_taila,,l2_normalize,"((65537, 2),)","('float16',)","('ND',)","((65 | |||
| 27 | l2_fp32_eps_negative,,l2_normalize,"((3,),)","('float32',)","('ND',)","((3,),)","('float32',)","('ND',)","((3,),)","('ND',)","((3,),)","('ND',)","{'axis': [0], 'eps': -1.0}","((1, 2),)",,1e-08,,,True,Small numerical stability/Group regression,,0,,(),(),"((3,),)" | 27 | l2_fp32_eps_negative,,l2_normalize,"((3,),)","('float32',)","('ND',)","((3,),)","('float32',)","('ND',)","((3,),)","('ND',)","((3,),)","('ND',)","{'axis': [0], 'eps': -1.0}","((1, 2),)",,1e-08,,,True,Small numerical stability/Group regression,,0,,(),(),"((3,),)" |
| 28 | l2_fp32_eps_max,,l2_normalize,"((3,),)","('float32',)","('ND',)","((3,),)","('float32',)","('ND',)","((3,),)","('ND',)","((3,),)","('ND',)","{'axis': [0], 'eps': 3.4028234663852886e+38}","((1e+18, 1e+18),)",,1e-08,,,True,Small numerical stability/Group regression,,0,,(),(),"((3,),)" | 28 | l2_fp32_eps_max,,l2_normalize,"((3,),)","('float32',)","('ND',)","((3,),)","('float32',)","('ND',)","((3,),)","('ND',)","((3,),)","('ND',)","{'axis': [0], 'eps': 3.4028234663852886e+38}","((1e+18, 1e+18),)",,1e-08,,,True,Small numerical stability/Group regression,,0,,(),(),"((3,),)" |
| 29 | l2_fp32_subnormal,,l2_normalize,"((3,),)","('float32',)","('ND',)","((3,),)","('float32',)","('ND',)","((3,),)","('ND',)","((3,),)","('ND',)","{'axis': [0], 'eps': 0.0}","((1.401298464324817e-45, 1.401298464324817e-45),)",,1e-08,,,True,Small numerical stability/Group regression,,0,,(),(),"((3,),)" | 29 | l2_fp32_subnormal,,l2_normalize,"((3,),)","('float32',)","('ND',)","((3,),)","('float32',)","('ND',)","((3,),)","('ND',)","((3,),)","('ND',)","{'axis': [0], 'eps': 0.0}","((1.401298464324817e-45, 1.401298464324817e-45),)",,1e-08,,,True,Small numerical stability/Group regression,,0,,(),(),"((3,),)" |
| 30 | +l2_fp16_finite_extremes,UNKNOWN,l2_normalize,"((2, 4),)","('float16',)","('ND',)","((2, 4),)","('float16',)","('ND',)","((2, 4),)","('ND',)","((2, 4),)","('ND',)","{'axis': [1], 'eps': 0.0, 'x': [[65504.0,-65504.0,0.0,2**-24],[2**-24,-2**-24,2**-14,-2**-14]]}","((-1,1),)",,,(),(),True,FP16 special values and FP32 normal sqrt precision regression,,0,,(),(),"((2, 4),)" | ||
| 31 | +l2_fp16_mixed_nonfinite,UNKNOWN,l2_normalize,"((3, 4),)","('float16',)","('ND',)","((3, 4),)","('float16',)","('ND',)","((3, 4),)","('ND',)","((3, 4),)","('ND',)","{'axis': [1], 'eps': 0.0, 'x': [[1.0,float('inf'),-2.0,0.0],[-float('inf'),1.0,-1.0,0.0],[1.0,float('nan'),0.0,-1.0]]}","((-1,1),)",,,(),(),True,FP16 special values and FP32 normal sqrt precision regression,,0,,(),(),"((3, 4),)" | ||
| 32 | +l2_fp16_empty_nonfinite,UNKNOWN,l2_normalize,"((8,),)","('float16',)","('ND',)","((8,),)","('float16',)","('ND',)","((8,),)","('ND',)","((8,),)","('ND',)","{'axis': [], 'eps': 0.0001, 'x': [float('inf'),-float('inf'),float('nan'),0.0,-0.0,2**-24,65504.0,-65504.0]}","((-1,1),)",,,(),(),True,FP16 special values and FP32 normal sqrt precision regression,,0,,(),(),"((8,),)" | ||
| 33 | +l2_fp32_identity_normal,UNKNOWN,l2_normalize,"((8,),)","('float32',)","('ND',)","((8,),)","('float32',)","('ND',)","((8,),)","('ND',)","((8,),)","('ND',)","{'axis': [], 'eps': 0.0, 'x': [-841.525634765625, -460.3163757324219, -0.10450954735279083, -0.003607991384342313, -817.0941162109375, -0.23848676681518555, -0.7906009554862976, 13.625178337097168]}","((-1,1),)",,,(),(),True,FP16 special values and FP32 normal sqrt precision regression,,0,,(),(),"((8,),)" | ||
| 34 | +l2_fp16_group_inf,UNKNOWN,l2_normalize,"((65537, 2),)","('float16',)","('ND',)","((65537, 2),)","('float16',)","('ND',)","((65537, 2),)","('ND',)","((65537, 2),)","('ND',)","{'axis': [0], 'eps': 0.0, 'x': [[float('inf'),1.0]]}","((-1,1),)",,,(),(),True,FP16 special values and FP32 normal sqrt precision regression,,0,,(),(),"((65537, 2),)" | ||
| 35 | +l2_fp16_group_nan,UNKNOWN,l2_normalize,"((65537, 2),)","('float16',)","('ND',)","((65537, 2),)","('float16',)","('ND',)","((65537, 2),)","('ND',)","((65537, 2),)","('ND',)","{'axis': [0], 'eps': 0.0, 'x': [[float('nan'),1.0]]}","((-1,1),)",,,(),(),True,FP16 special values and FP32 normal sqrt precision regression,,0,,(),(),"((65537, 2),)" | ||
🟡 Medium Priority
改动行:CSV 第 30–35 行新增 6 条用例,第 16 列 precision_tolerances 与第 17 列 absolute_precision 全为空(
,,,(),()段),而同文件其余 33 条用例(第 2–11 行 fp16 填 1.52587890625e-5,第 12–29 行填 1e-08)均显式给出 absolute_precision。本文件所有用例的 precision_tolerances 都是空的,也就是说 absolute_precision 是这套 CSV 唯一的容差来源。影响契约:第 33 行
l2_fp32_identity_normal(axis=[]、eps=0、8 个冻结元素)正是本 PR 用来锁定“普通 FP32 首遍 Sqrt 由 1-ULP 改回 0-ULP”的回归用例,其输出期望为精确的 ±1。失效模式:若容差为空、由 TTK 按 dtype 默认(fp32 通常为 1e-4 量级相对误差)判定,则把
PrepareScaledNormVf的 sqrt 模式改回SCALE_SQRT_MODE(1-ULP)后,输出相对精确 ±1 的偏差仅 1.1920928955078125e-7(issue #8024 实测最大绝对差),仍会判 PASS——即这条“回归”用例恰恰检不出它要防的回归。反之若按同文件惯例填 absolute_precision=1e-08,1.19e-7 > 1e-8,回归会被正确捕获。同理第 30/32 行 FP16 用例(含 2^-24 次正规、65504 边界)也缺少 fp16 侧 1.52587890625e-5 的量级约束。建议:为新增 6 条用例补第 17 列 absolute_precision:第 33 行(fp32)填 1e-08;第 30、31、32、34、35 行(fp16)填 1.52587890625e-5,与第 2–29 行既有写法一致,确保新回归门限严于 1 ULP。