| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- //go:build cuda
- package matmul
- import (
- "testing"
- "makarna/pkg/backend/cpu"
- "makarna/pkg/tensor"
- )
- func TestLinearCudaMatchesCPU(t *testing.T) {
- // Small matrix to compare CPU vs CUDA paths.
- M, K, N := 4, 8, 3
- aCPU := cpu.NewTensor(tensor.Shape{M, K}, nil)
- wCPU := cpu.NewTensor(tensor.Shape{N, K}, nil)
- outCPU := cpu.NewTensor(tensor.Shape{M, N}, nil)
- outCUDA := cpu.NewTensor(tensor.Shape{M, N}, nil)
- fillSeq(aCPU.DataFloat32())
- fillSeq(wCPU.DataFloat32())
- if err := linearCPU(aCPU, wCPU, outCPU); err != nil {
- t.Fatalf("cpu linear failed: %v", err)
- }
- if err := Linear(aCPU, wCPU, outCUDA); err != nil {
- t.Fatalf("cuda linear failed: %v", err)
- }
- for i, v := range outCPU.DataFloat32() {
- got := outCUDA.DataFloat32()[i]
- if diff := abs32(v - got); diff > 1e-4 {
- t.Fatalf("mismatch at %d: cpu=%f cuda=%f", i, v, got)
- }
- }
- }
- func fillSeq(dst []float32) {
- for i := range dst {
- dst[i] = float32(i%7 + 1)
- }
- }
- func abs32(v float32) float32 {
- if v < 0 {
- return -v
- }
- return v
- }
|