Skip to content

Commit 22833e7

Browse files
authored
Merge pull request #1781 from Shashank-Tripathi-07/fix/tinytorch-audit-20260518
Correct MatmulBackward gradients: use np.outer for 1D vector inputs instead of .T which is a no-op on 1D arrays.
2 parents fccfad0 + b4fae46 commit 22833e7

1 file changed

Lines changed: 10 additions & 6 deletions

File tree

tinytorch/src/06_autograd/06_autograd.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -966,21 +966,25 @@ def apply(self, grad_output):
966966

967967
# Gradient for first input: grad_output @ b.T
968968
if isinstance(a, Tensor) and a.requires_grad:
969-
# For batched tensors, transpose only last two dims
970969
if b.data.ndim >= 2:
970+
# Batched: transpose only the last two dims
971971
b_T = np.swapaxes(b.data, -2, -1)
972+
grad_a = np.matmul(grad_output, b_T)
972973
else:
973-
b_T = b.data.T
974-
grad_a = np.matmul(grad_output, b_T)
974+
# 1D b: A(m,k) @ b(k,) -> out(m,)
975+
# grad_A = outer(grad_output, b): (m,) x (k,) -> (m, k)
976+
grad_a = np.outer(grad_output, b.data)
975977

976978
# Gradient for second input: a.T @ grad_output
977979
if isinstance(b, Tensor) and b.requires_grad:
978-
# For batched tensors, transpose only last two dims
979980
if a.data.ndim >= 2:
981+
# Batched: transpose only the last two dims
980982
a_T = np.swapaxes(a.data, -2, -1)
983+
grad_b = np.matmul(a_T, grad_output)
981984
else:
982-
a_T = a.data.T
983-
grad_b = np.matmul(a_T, grad_output)
985+
# 1D a: a(k,) @ B(k,n) -> out(n,)
986+
# grad_B = outer(a, grad_output): (k,) x (n,) -> (k, n)
987+
grad_b = np.outer(a.data, grad_output)
984988

985989
return grad_a, grad_b
986990
### END SOLUTION

0 commit comments

Comments
 (0)