diff --git a/README.md b/README.md index 2109be2..fd3a258 100644 --- a/README.md +++ b/README.md @@ -1412,9 +1412,15 @@ Accuracy: against a float64 transliteration of the reference (same integer-trick approximations, public as `upscale/fsrref`), the float32 pipeline stays within `ErrorBound` = 2e-4 on a 0..1 scale on the corpus of `TestOracle` (measured 5.7e-6 at worst over 662 520 samples on arm64, scalar and NEON). The bound does -not hold everywhere: on noise of at most 3 levels of 255, about 0.9 % of the -samples are outside it, by up to 4.7e-3, under the local colour range -(`TestOracleLowContrast`; the cause is not established). Since v0.25.0 the +not hold everywhere. EASU measures an edge as |d-b| / max(|d-c|, |c-b|) on the +lumas of three neighbours, which is 0/0 where different colours share a luma, +and the reference defines no answer there: each precision answers with its +rounding noise. On noise of at most 3 levels of 255, 3.6 % of the samples have +such a neighbourhood and 0.9 % differ from the oracle by more than the bound, +by up to 4.7e-3 (since v0.21.0, on every path). The stated contract is +`ErrorBound` on every sample whose analysis is well conditioned (1.13 million +measured, worst 3.4e-5), and `ErrorBound` plus the range of the four nearest +source colours on any sample (`TestOracleLowContrast`). Since v0.25.0 the position of an output pixel in the source is computed in integers, in the package and in the oracle: computed in floats it fell on an integer for ratios such as 640 to 1920, where float32 and float64 floors chose footprints one diff --git a/upscale/adversarial_test.go b/upscale/adversarial_test.go index 8704ebc..e73dd2d 100644 --- a/upscale/adversarial_test.go +++ b/upscale/adversarial_test.go @@ -57,17 +57,63 @@ func TestPhaseTies(t *testing.T) { } } -// TestOracleLowContrast states what ErrorBound does NOT cover. On noise of at -// most 3 levels of 255 around a base, a fraction of the samples differs from -// the oracle by more than the bound: 0.9 % of 1.2 million when measured, by -// up to 4.7e-3. The cause is not established (the edge analysis divides -// differences of nearly equal lumas, which is the suspect; go-avkit/avkit#141 -// carries it); what holds, and is asserted, is that EASU clamps to the four -// nearest source pixels in both computations, so no sample is off by more -// than the local range of 3 levels, and that the fraction stays under 3 %. +// degenerateTol is the size under which a difference of two lumas is rounding +// noise rather than signal: float32 rounds a luma by up to 6e-8, and the +// smallest real step of 8-bit content is 1/510 = 2e-3. +const degenerateTol = 1e-5 + +// degenerate reports whether the edge analysis of the output sample whose "f" +// tap is (X, Y) is ill-conditioned: for one of the four bilinear corners, one +// axis, the denominator max(|d-c|, |c-b|) of FsrEasuSetF is under degenerateTol +// in one precision and not exactly zero in the other (or tiny in both). The +// ratio |d-b| / that denominator is then 0/0 and each precision answers +// differently; where the denominator is exactly zero in both, the ratio is a +// well defined 0 and the sample is not degenerate. +func degenerate(p *planes, m *fsrref.Image, X, Y int) bool { + L := func(x, y int) float64 { + c := m.At(x, y) + return c[2]*0.5 + (c[0]*0.5 + c[1]) + } + for _, c := range [4][2]int{{0, 0}, {1, 0}, {0, 1}, {1, 1}} { + x, y := X+c[0], Y+c[1] + i, s, l := p.at(x, y), p.stride, p.l + for _, v := range [2][2]float64{ + {math.Max(math.Abs(L(x+1, y)-L(x, y)), math.Abs(L(x, y)-L(x-1, y))), float64(fmax(abs32(l[i+1]-l[i]), abs32(l[i]-l[i-1])))}, + {math.Max(math.Abs(L(x, y+1)-L(x, y)), math.Abs(L(x, y)-L(x, y-1))), float64(fmax(abs32(l[i+s]-l[i]), abs32(l[i]-l[i-s])))}, + } { + if max(v[0], v[1]) > 0 && min(v[0], v[1]) < degenerateTol { + return true + } + } + } + return false +} + +// TestOracleLowContrast states, and asserts, what ErrorBound covers on noise +// of at most 3 levels of 255 around a base (go-avkit/avkit#141). The behaviour +// is present since v0.21.0, on the scalar and the vector paths alike: the cause +// is the conditioning of FSR's edge analysis, not an arithmetic defect. +// +// EASU measures an edge by lenX = sat(|d-b| * rcp(max(|d-c|, |c-b|))), from the +// lumas of three neighbours. When three different colours share a luma (the +// luma of such 8-bit colours takes about 13 values), the denominator is zero in +// exact arithmetic, and so is the numerator: 0/0. The reference defines no +// answer; a precision answers with its rounding noise. The oracle sees the +// float32 colours in float64, where a tie is 7e-9 away from zero; the float32 +// path rounds the same luma to exactly 0 or to a 6e-8 residue. Each then +// reports an edge strength anywhere in [0,1] (0 against 0.94 in the worst case +// found), which reshapes the kernel and moves the output, by at most the range +// of the colours EASU clamps to. +// +// The honest contract is therefore two parts, both asserted. A sample whose +// analysis is well conditioned is within ErrorBound: this is the guard against +// a real arithmetic defect, which a single loose bound could not see (measured +// here: 1.13 million such samples, worst 3.4e-5). Any sample is within +// ErrorBound of the range of its four nearest source colours. func TestOracleLowContrast(t *testing.T) { rng := rand.New(rand.NewSource(1)) - worst, outside, total := 0.0, 0, 0 + var well, ill, outWell, outIll int + var worstWell, worstIll float64 for it := 0; it < 200; it++ { sw, sh := 8+rng.Intn(30), 8+rng.Intn(30) ow, oh := sw+rng.Intn(2*sw), sh+rng.Intn(2*sh) @@ -80,15 +126,46 @@ func TestOracleLowContrast(t *testing.T) { } } p.finish() - w, o, n := against(p, ow, oh) - worst, outside, total = max(worst, w), outside+o, total+n + m := p.to64() + want := fsrref.Upscale(m, ow, oh, -1) + runWorkers(p, ow, oh, -1, 1, func(y int, r, g, b, _ []float32) { + Y, _, _ := fsrref.Phase(sh, oh, y) + for x := 0; x < ow; x++ { + X, _, _ := fsrref.Phase(sw, ow, x) + deg := degenerate(p, m, X, Y) + for q, v := range [3]float32{r[x], g[x], b[x]} { + d := math.Abs(float64(v) - want.Pix[y*ow+x][q]) + lo, hi := math.Inf(1), math.Inf(-1) + for _, c := range [4][2]int{{0, 0}, {1, 0}, {0, 1}, {1, 1}} { + lo, hi = min(lo, m.At(X+c[0], Y+c[1])[q]), max(hi, m.At(X+c[0], Y+c[1])[q]) + } + if d > hi-lo+ErrorBound { + t.Fatalf("(%d,%d) channel %d: error %g exceeds the range %g of the four nearest colours plus %g", x, y, q, d, hi-lo, ErrorBound) + } + if deg { + ill++ + worstIll = max(worstIll, d) + if d > ErrorBound { + outIll++ + } + } else { + well++ + worstWell = max(worstWell, d) + if d > ErrorBound { + outWell++ + } + } + } + } + }) } - t.Logf("low contrast: worst %.3g, %d of %d outside %g", worst, outside, total, ErrorBound) - if local := 3.0/255 + ErrorBound; worst > local { - t.Errorf("worst error %g exceeds the local range %g", worst, local) + t.Logf("well conditioned: %d samples, worst %.3g, %d outside %g; degenerate: %d samples, worst %.3g, %d outside", + well, worstWell, outWell, ErrorBound, ill, worstIll, outIll) + if outWell != 0 { + t.Errorf("%d of %d well-conditioned samples exceed ErrorBound (worst %g)", outWell, well, worstWell) } - if 100*outside > 3*total { - t.Errorf("%d of %d samples outside the bound, more than 3 %%", outside, total) + if ill == 0 || outIll == 0 { + t.Errorf("the corpus no longer exercises degenerate samples (%d, %d outside): the test shows nothing", ill, outIll) } } diff --git a/upscale/upscale.go b/upscale/upscale.go index b935377..da203a0 100644 --- a/upscale/upscale.go +++ b/upscale/upscale.go @@ -14,12 +14,17 @@ // Accuracy contract: against a float64 transliteration of the reference // (package fsrref, which applies the same bit-level approximations) // the float32 pipeline stays within ErrorBound of the exact result, on a -// 0..1 colour scale, on the corpus of TestOracle. It does not hold everywhere: -// on noise of at most 3 levels of 255, about 0.9 % of the samples are outside -// it, by up to 4.7e-3 (TestOracleLowContrast), which EASU's clamp to the four -// nearest pixels keeps under the local colour range. The cause is not -// established; a comparison of the reference flipping under rounding (a -// clamp, a min/max, the "direction is zero" test) is one known mechanism. +// 0..1 colour scale, on the corpus of TestOracle, and on every sample whose +// edge analysis is well conditioned. It is not a bound on every input: EASU +// measures an edge by |d-b| / max(|d-c|, |c-b|) on the lumas of three +// neighbours, which is 0/0 where different colours share a luma, and the +// reference defines no answer there; each precision answers with its rounding +// noise (the oracle's 7e-9, this package's exact 0 or 6e-8). On noise of at +// most 3 levels of 255, 3.6 % of the samples have such a neighbourhood and +// 0.9 % differ by more than ErrorBound, by up to 4.7e-3 (TestOracleLowContrast; +// since v0.21.0, every path, as in the reference's own float32). There the +// stated bound is ErrorBound plus the range of the four nearest source +// colours, which EASU's final clamp enforces. // The position of an output pixel in the source is computed in integers, by // this package and by fsrref alike: in floats it falls on an integer whenever // the two sizes, reduced, are both odd (640 to 1920), and a float32 and a