Files
tensor/signal/conv_bench_extra_test.go
T

75 lines
1.8 KiB
Go
Raw Normal View History

2026-09-03 10:00:00 +02:00
// Copyright (c) 2026 Petr Balvín <opensource@petrbalvin.org> (https://petrbalvin.org)
// SPDX-License-Identifier: MIT
package signal
import (
"testing"
core "sourcedock.dev/petrbalvin/tensor/internal/core"
)
// Convolution benchmarks guard the direct kernels; the 2-D pair
// already lives in bench_test.go, these add the 1-D path and pooling
// at inference-like sizes.
func benchSig(b *testing.B, seed, n int, shape ...int) *core.Array {
b.Helper()
v := make([]float64, n)
for i := range v {
v[i] = float64(i%23)*float64(seed%7)*0.5 + float64(i%11) - 5
}
a, err := core.FromFloats(v, shape...)
if err != nil {
b.Fatal(err)
}
return a
}
// BenchmarkConv1D measures a 1×16×4096 input with 8 kernels of width
// 7, stride 1.
func BenchmarkConv1D(b *testing.B) {
input := benchSig(b, 1, 16*4096, 1, 16, 4096)
kernel := benchSig(b, 2, 8*16*7, 8, 16, 7)
b.ReportAllocs()
for b.Loop() {
if _, err := Conv1D(input, kernel, nil, 1, 0, 1); err != nil {
b.Fatal(err)
}
}
}
// BenchmarkMaxPool2D measures a 8×64×28×28 input through 2×2 pooling.
func BenchmarkMaxPool2D(b *testing.B) {
input := benchSig(b, 3, 8*64*28*28, 8, 64, 28, 28)
b.ReportAllocs()
for b.Loop() {
if _, err := MaxPool2D(input, 2, 2, 0); err != nil {
b.Fatal(err)
}
}
}
// BenchmarkAvgPool2D is the averaging twin.
func BenchmarkAvgPool2D(b *testing.B) {
input := benchSig(b, 4, 8*64*28*28, 8, 64, 28, 28)
b.ReportAllocs()
for b.Loop() {
if _, err := AvgPool2D(input, 2, 2, 0, false); err != nil {
b.Fatal(err)
}
}
}
// BenchmarkAutocorrelate measures the FFT-based 1-D correlation at a
// speech-like length.
func BenchmarkAutocorrelate(b *testing.B) {
x := benchSig(b, 5, 8192, 8192)
b.ReportAllocs()
for b.Loop() {
if _, err := Autocorrelate(x, 512); err != nil {
b.Fatal(err)
}
}
}