chickadee » nanograd » matmul-op

matmul-op a bprocedure

Matrix multiplication using BLAS GEMM/GEMV operations with batch support. Supports:

  • Matrix × Matrix
  • Matrix × Vector
  • Vector × Matrix
  • Vector × Vector (dot product)
  • Batched operations (implicit batching over first dimension)
; Standard matrix-vector multiplication
(define A (make-tensor32 (f32vector 1.0 2.0 3.0 4.0) '(2 2)))
(define b (make-tensor32 (f32vector 5.0 6.0) '(2)))
(define c (matmul-op A b))  ; 2×2 matrix times 2×1 vector = 2×1 vector

; Batch matrix multiplication
(define batch-A (make-tensor32 (make-f32vector 80) '(10 2 4)))  ; 10 samples
(define W (make-tensor32 (make-f32vector 12) '(4 3)))
(define batch-result (matmul-op batch-A W))  ; Shape: (10, 2, 3)

Gradient: dL/dA = dL/dC · B^T, dL/dB = A^T · dL/dC