17/31
Part 4 of 6 · A Function of your ownLearn and do
Return one gradient per input, in forward's order
The engine has one more rule for what backward returns.
| Position | 0 | 1 | 2 |
|---|---|---|---|
forward(ctx, ...) takes | a | b | scale |
backward returns | grad_a | grad_b | None |
The engine matches gradients to inputs by position, not by name. Use None for an input that needs no gradient or isn't a tensor, like the float scale in this step's task.
The wrong count fails with function MatMulBackward returned an incorrect number of gradients (expected 2, got 1).
Backward gets one gradient per output of forward. Here that's only grad_out, shaped like y. Treat it as read-only: the docs say never to change it in place.
In the editor, write ScaledMatMul, a Function with three inputs: two tensors and a plain float that gets None.