已合并
fix(bounding_box_encode): fix precision tolerance mapping(extreme mismatched ranges) & add validations in geir path and inferdatatype #1260
zhangyiyi创建于 11 天前
fix(bounding_box_encode): fix precision tolerance mapping(extreme mismatched ranges) & add validations in geir path and inferdatatype #1260
已合并
zhangyiyi创建于 11 天前
1 个文件变更+83-4
Mobjdetect/bounding_box_encode/op_kernel/arch35/bounding_box_encode.h+83-4
@@ -41,6 +41,77 @@ struct BoxDeltaCalc {
41 rw = gw / pw;41 rw = gw / pw;
42 rh = gh / ph;42 rh = gh / ph;
43 }43 }
44+ 
45+ __aicore__ static inline float FixLnResult(float origInput, float npuLnResult)
46+ {
47+ if (origInput != origInput) {
48+ return MakeNan();
49+ }
50+ if (origInput == 0.0f) {
51+ return MakeNegInf();
52+ }
53+ if (origInput < 0.0f) {
54+ return MakeNan();
55+ }
56+ if (npuLnResult != npuLnResult) {
57+ return MakeNan();
58+ }
59+ float negInf = MakeNegInf();
60+ if (npuLnResult == negInf && origInput > 0.0f) {
61+ return SoftLnPositive(origInput);
62+ }
63+ return npuLnResult;
64+ }
65+ 
66+ __aicore__ static inline float SoftLnPositive(float x)
67+ {
68+ if (x <= 0.0f) {
69+ return MakeNegInf();
70+ }
71+ float m = x;
72+ int e = 0;
73+ if (m >= 2.0f) {
74+ while (m >= 2.0f) {
75+ m *= 0.5f;
76+ e++;
77+ }
78+ } else if (m < 1.0f) {
79+ while (m < 1.0f) {
80+ m *= 2.0f;
81+ e--;
82+ }
83+ }
84+ float z = (m - 1.0f) / (m + 1.0f);
85+ float z2 = z * z;
86+ float z4 = z2 * z2;
87+ float z6 = z4 * z2;
88+ float z8 = z4 * z4;
89+ float z10 = z6 * z4;
90+ float poly = 1.0f + z2 * 0.33333334f + z4 * 0.2f + z6 * 0.14285715f + z8 * 0.11111111f + z10 * 0.09090909f;
91+ float artanh_z = z * poly;
92+ float ln_m = 2.0f * artanh_z;
93+ return ln_m + static_cast<float>(e) * 0.69314718f;
94+ }
95+ 
96+ __aicore__ static inline float MakeNegInf()
97+ {
98+ union {
99+ uint32_t u;
100+ float f;
101+ } cvt;
102+ cvt.u = 0xFF800000u;
103+ return cvt.f;
104+ }
105+ 
106+ __aicore__ static inline float MakeNan()
107+ {
108+ union {
109+ uint32_t u;
110+ float f;
111+ } cvt;
112+ cvt.u = 0x7FC00000u;
113+ return cvt.f;
114+ }
44};115};
45 116 
46template <typename T>117template <typename T>
@@ -226,8 +297,12 @@ __aicore__ inline void BoundingBoxEncode<T>::ComputeFp16Path(AscendC::LocalTenso
226 int64_t base = b * 4;297 int64_t base = b * 4;
227 float v0 = (outFloat.GetValue(b * 2) - means0_) * invStds0_;298 float v0 = (outFloat.GetValue(b * 2) - means0_) * invStds0_;
228 float v1 = (outFloat.GetValue(b * 2 + 1) - means1_) * invStds1_;299 float v1 = (outFloat.GetValue(b * 2 + 1) - means1_) * invStds1_;
229- float v2 = (buf2.GetValue(base + 2) - means2_) * invStds2_;300+ float origRw = buf1.GetValue(base + 2);
230- float v3 = (buf2.GetValue(base + 3) - means3_) * invStds3_;301+ float origRh = buf1.GetValue(base + 3);
302+ float lnRw = BoxDeltaCalc::FixLnResult(origRw, buf2.GetValue(base + 2));
303+ float lnRh = BoxDeltaCalc::FixLnResult(origRh, buf2.GetValue(base + 3));
304+ float v2 = (lnRw - means2_) * invStds2_;
305+ float v3 = (lnRh - means3_) * invStds3_;
231 buf2.SetValue(base + 0, v0);306 buf2.SetValue(base + 0, v0);
232 buf2.SetValue(base + 1, v1);307 buf2.SetValue(base + 1, v1);
233 buf2.SetValue(base + 2, v2);308 buf2.SetValue(base + 2, v2);
@@ -292,8 +367,12 @@ __aicore__ inline void BoundingBoxEncode<T>::ComputeFp32Path(AscendC::LocalTenso
292 int64_t base = b * 4;367 int64_t base = b * 4;
293 float v0 = (buf1.GetValue(b * 2) - means0_) * invStds0_;368 float v0 = (buf1.GetValue(b * 2) - means0_) * invStds0_;
294 float v1 = (buf1.GetValue(b * 2 + 1) - means1_) * invStds1_;369 float v1 = (buf1.GetValue(b * 2 + 1) - means1_) * invStds1_;
295- float v2 = (buf2.GetValue(base + 2) - means2_) * invStds2_;370+ float origRw = aFp32.GetValue(base + 2);
296- float v3 = (buf2.GetValue(base + 3) - means3_) * invStds3_;371+ float origRh = aFp32.GetValue(base + 3);
372+ float lnRw = BoxDeltaCalc::FixLnResult(origRw, buf2.GetValue(base + 2));
373+ float lnRh = BoxDeltaCalc::FixLnResult(origRh, buf2.GetValue(base + 3));
374+ float v2 = (lnRw - means2_) * invStds2_;
375+ float v3 = (lnRh - means3_) * invStds3_;
297 aFp32.SetValue(base + 0, v0);376 aFp32.SetValue(base + 0, v0);
298 aFp32.SetValue(base + 1, v1);377 aFp32.SetValue(base + 1, v1);
299 aFp32.SetValue(base + 2, v2);378 aFp32.SetValue(base + 2, v2);