From 90723a1fca4aa0a4ebf18fa4609b77204ec755f2 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Mon, 30 Nov 2026 21:01:06 +0100 Subject: [PATCH] test: assert FuzzOps MatMul against a stated error bound The GEMM sums in its own order and fuses multiply-add, so it is not bit-equal to the naive float64 loop the fuzz oracle used (1 ulp apart on amd64, run 46298156668: At[3 1] 2.6035494484318307e+23 vs 2.603549448431831e+23). MatMul now owes |C_ij - exact_ij| <= gamma_k * sum|a_ik||b_kj| with exact_ij summed in math/big; At vs Copy stay bit-equal, elementwise ops unchanged. Commits the crash input as a regression seed. Refs go-ndarray/ndarray#137 Co-Authored-By: Claude Sonnet 5.5 --- fuzz_test.go | 74 ++++++++++++++++++++++++++ testdata/fuzz/FuzzOps/3f595a556cba79cb | 2 + 2 files changed, 76 insertions(+) create mode 100644 testdata/fuzz/FuzzOps/3f595a556cba79cb diff --git a/fuzz_test.go b/fuzz_test.go index 30d9815..4e72651 100644 --- a/fuzz_test.go +++ b/fuzz_test.go @@ -2,6 +2,7 @@ package ndarray import ( "math" + "math/big" "testing" ) @@ -146,10 +147,83 @@ func applyChecked(t *testing.T, r *byteReader, a, b *Array) *Array { case want == nil: return nil } + if name == "MatMul" { + checkMatMul(t, got, a, b, want) + return got + } checkAgainst(t, name, got, want) return got } +// checkMatMul asserts MatMul's result against the exact product rather than +// against another float64 loop. The GEMM may sum in any order and fuse +// multiply-add, so it is not bit-equal to the naive loop (1 ulp apart on +// amd64); what it owes is the standard inner-product bound +// +// |fl(xᵀy) − xᵀy| ≤ γ_k Σ|x_i||y_i|, γ_k = ku/(1−ku), u = 2⁻⁵³, +// +// valid for every summation order and with FMA (go-blas docs/design.md +// Decision 4). The exact value is summed in math/big, wide enough to hold +// every float64 product without rounding. The bound ignores underflow, so +// k·2⁻¹⁰⁷⁴ is added. A dot product with a non-finite term, or whose exact +// magnitude sum exceeds MaxFloat64 (intermediate overflow depends on the +// order), keeps the oracle's value for NaN/Inf and is otherwise not +// bounded. At and Copy must agree exactly with each other. +func checkMatMul(t *testing.T, got, a, b *Array, want *ref) { + t.Helper() + if !equalInts(got.Shape(), want.shape) { + t.Fatalf("MatMul: shape %v, oracle %v", got.Shape(), want.shape) + } + flat := got.Copy().contiguousData() + k := a.Shape()[1] + const prec = 5000 // products span 2^-2148..2^2046: exact sums fit + ku := new(big.Float).SetPrec(prec).SetMantExp(big.NewFloat(float64(k)), -53) + one := new(big.Float).SetPrec(prec).SetInt64(1) + gamma := new(big.Float).SetPrec(prec).Quo(ku, new(big.Float).SetPrec(prec).Sub(one, ku)) + slack := new(big.Float).SetPrec(prec).SetMantExp(big.NewFloat(float64(k)), -1074) + maxF := new(big.Float).SetPrec(prec).SetFloat64(math.MaxFloat64) + pos := 0 + eachIndex(want.shape, func(idx []int) { + v := got.At(idx...) + if !same(v, flat[pos]) { + t.Fatalf("MatMul: At%v = %v, Copy = %v", idx, v, flat[pos]) + } + exact := new(big.Float).SetPrec(prec) + sumAbs := new(big.Float).SetPrec(prec) + finite := true + for j := 0; j < k; j++ { + x, y := a.At(idx[0], j), b.At(j, idx[1]) + if math.IsNaN(x) || math.IsInf(x, 0) || math.IsNaN(y) || math.IsInf(y, 0) { + finite = false + break + } + p := new(big.Float).SetPrec(prec).Mul(big.NewFloat(x), big.NewFloat(y)) + exact.Add(exact, p) + sumAbs.Add(sumAbs, p.Abs(p)) + } + switch { + case !finite: + if !same(v, want.vals[pos]) { + t.Fatalf("MatMul: At%v = %v, oracle %v (non-finite operand)", idx, v, want.vals[pos]) + } + case sumAbs.Cmp(maxF) > 0: + // intermediate overflow is order-dependent: not bounded + default: + if math.IsNaN(v) || math.IsInf(v, 0) { + t.Fatalf("MatMul: At%v = %v, exact %.17g is finite", idx, v, exact) + } + diff := new(big.Float).SetPrec(prec).Sub(big.NewFloat(v), exact) + limit := new(big.Float).SetPrec(prec).Mul(gamma, sumAbs) + limit.Add(limit, slack) + if diff.Abs(diff).Cmp(limit) > 0 { + t.Fatalf("MatMul: At%v = %v, exact %.17g, error %.3g exceeds γ_%d·Σ|a||b| = %.3g (shape %v)", + idx, v, exact, diff, k, limit, want.shape) + } + } + pos++ + }) +} + // ref is an oracle result: a shape and its row-major values. type ref struct { shape []int diff --git a/testdata/fuzz/FuzzOps/3f595a556cba79cb b/testdata/fuzz/FuzzOps/3f595a556cba79cb new file mode 100644 index 0000000..340494f --- /dev/null +++ b/testdata/fuzz/FuzzOps/3f595a556cba79cb @@ -0,0 +1,2 @@ +go test fuzz v1 +[]byte("2002AA000000000000100000C01C22C00000CAAC")