chickadee » nanograd » matmul-op

matmul-op a bprocedure

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

  • Matrix x Matrix
  • Matrix x Vector
  • Vector x Matrix
  • Vector x Vector (dot product)
  • Batched operations (implicit batching over the first dimension)
; 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))  ; 2x2 matrix times 2-vector = 2-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