Broadcasting #
Spec-level broadcasting and broadcasted binary maps.
Broadcasting is defined in terms of the shape proposition Shape.CanBroadcastTo: pick an explicit
target shape and provide evidence that each operand broadcasts to it. The proposition depends only
on the two shapes, so the result never depends on how the evidence was obtained.
Execution goes through the internal Rep.broadcast: the source is padded with leading singleton
axes by a zero-copy reshape and then pulled back along the coordinate map that fixes expanded axes
at index zero. This is one buffer fill, independent of the rank.
TorchLean standardizes on this explicit target style throughout core. There is intentionally no
second "implicit" API that infers a common output shape from two operands, because that would split
the codebase into two parallel styles. The backward pass of a broadcast is the sum-reduction over
the broadcast axes (reduceFromBroadcastTo in the reductions module).
Shape.CanBroadcastTo in the list form used by the internal representation: after padding the
source with leading ones to the target rank, extents agree pointwise or the source extent is one.
Broadcast a source padded with k leading singleton axes.
Separating the padding count from the shapes lets the structural lemmas below be stated for
literal counts 0 and k + 1, independently of the rank arithmetic in broadcastTo.
Instances For
Transport the padding count along an equality.
Broadcasting a scalar to the scalar shape is the identity.
One more padding axis stacks the broadcast of the remaining padding along the leading target axis.
With no padding and equal leading extents, broadcasting acts slice by slice.
With no padding and a leading source extent of one, every target slice reads slice zero.
Broadcast a tensor to a compatible target shape (spec-level analogue of
torch.broadcast_to).
The source is padded with leading singleton axes to the target rank and every singleton axis is
replicated. The result depends only on the shapes, not on how h was proved.
Instances For
Broadcasting a scalar tensor to the scalar shape is the identity.
Equal leading extents broadcast slice by slice.
A leading source extent of one is replicated along the leading target axis.
A target axis that only raises the rank replicates the whole source.
Broadcasting a tensor to its own shape changes nothing.
Broadcasting across one new leading target axis replicates the source.
Broadcasted maps #
Helper: map a scalar on the left over any tensor shape.
Instances For
Helper: map a scalar on the right over any tensor shape.
Instances For
Binary element-wise operation with broadcasting to an explicit target shape.
This is the helper you typically want in spec code:
- pick the output shape
t, - broadcast each operand to
t, - then
map2Specthe pointwise operation.
PyTorch analogy: f(x, y) where x and/or y are broadcastable to a common shape.
The common shape is explicit rather than discovered at runtime, which makes the result type
predictable and fixes the intended output shape at the call site.