torch.BroadcastShape
export type BroadcastShape<A extends Shape, B extends Shape> =
// If either is dynamic, result is dynamic (can't statically check)
number extends A[number]
? DynamicShape
: number extends B[number]
? DynamicShape
: // Propagate error types - errors in either operand propagate to result
A extends { readonly __isShapeError: true }
? A
: B extends { readonly __isShapeError: true }
? B
: // Handle empty shapes
A extends readonly []
? B
: B extends readonly []
? A
: // Same length - broadcast element-wise
A['length'] extends B['length']
? BroadcastSameLength<A, B>
: // Different lengths - pad shorter one with 1s
A['length'] extends number
? B['length'] extends number
? MaxLength<A, B> extends infer L extends number
? BroadcastSameLength<PadLeft<A, L>, PadLeft<B, L>>
: DynamicShape
: DynamicShape
: DynamicShape;Aextends ShapeBextends ShapeCompute broadcast shape for binary operations.
Broadcasting rules (from right to left):
- Dimensions must be equal OR one of them is 1
- Missing dimensions are treated as 1 (left-padded)