Tensor Shapes and Broadcasting Contracts

Distinguish sample, position, and feature axes, and track what happens to a broadcast value during differentiation.

Source
Anbloq
Language
English
Created
Updated
Chapters
2
Reading time
1 min
In this article2 chapters
  1. A compatible shape can still encode the wrong meaning
  2. Shared values accumulate contributions from every use

A compatible shape can still encode the wrong meaning

For activations of shape \((B,T,D)\), a bias of shape \((D)\) can be shared across batch and positions. A mask over positions may need shape \((1,T,1)\) to align with the intended axis. If two axes happen to have equal length, broadcasting can succeed on a misplaced axis and silently compute another operation. Document what each axis denotes and test shapes with different sizes.

Shared values accumulate contributions from every use

Adding one bias coordinate to several outputs means the derivative with respect to it is the sum of those outputs’ incoming derivatives. Broadcasting often avoids physically copying the value. The gradient still reflects every logical use. Reverse-mode differentiation explains accumulation along graph edges. Attention uses separate axes for queries, keys and features, making these conventions especially important.[1]

References

  1. Ian Goodfellow, Yoshua Bengio and Aaron Courville, Deep Learning ↩