Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions fuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ndarray

import (
"math"
"math/big"
"testing"
)

Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions testdata/fuzz/FuzzOps/3f595a556cba79cb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("2002AA000000000000100000C01C22C00000CAAC")
Loading