src/tensor/contracts/broadcast.bend source
src/tensor/contracts/broadcast.bend on the hub · documented module
import Baseimport ../../shape/core.bend as Shapeimport ../../storage/LAWS.bend as StorageLawsimport ../core.bend as Coreimport ../elementwise.bend as Elementwiseimport ./replayed.bend as Replayedimport ./pointwise.bend as Pointwisedef left_walk(-left: List<&2, Nat>, -right: List<&2, Nat>, -output: List<&2, Nat>, map: Shape.Index<left, right, output>, +index: Nat) -> Nat: match map: case Shape.Indexed{rows, exact}: Shape.walk_left(rows, index) case Shape.Unindexed{zero}: 0ndef right_walk(-left: List<&2, Nat>, -right: List<&2, Nat>, -output: List<&2, Nat>, map: Shape.Index<left, right, output>, +index: Nat) -> Nat: match map: case Shape.Indexed{rows, exact}: Shape.walk_right(rows, index) case Shape.Unindexed{zero}: 0ndef left_map(plan: Shape.BroadcastPlan, index: Nat) -> Nat: +plan = plan left_walk(Shape.extents(Shape.left_shape(plan)), Shape.extents(Shape.right_shape(plan)), Shape.extents(Shape.output_shape(plan)), Shape.index_map(plan), index)def right_map(plan: Shape.BroadcastPlan, index: Nat) -> Nat: +plan = plan right_walk(Shape.extents(Shape.left_shape(plan)), Shape.extents(Shape.right_shape(plan)), Shape.extents(Shape.output_shape(plan)), Shape.index_map(plan), index)def binary(+operation: Elementwise.Operation, +plan: Shape.BroadcastPlan, left: Core.Dynamic, right: Core.Dynamic) -> (Core.Dynamic & Core.Dynamic) & Result<&2, &1, Core.Error, Core.Dynamic>: Pointwise.run(~Elementwise.Operation, ~Elementwise.scalar, ~Shape.BroadcastPlan, ~left_map, ~right_map, operation, plan, Shape.output_shape(plan), left, right)def cell(+operation: Elementwise.Operation, +plan: Shape.BroadcastPlan, +left_fill: F32, +left_ops: List<&2, StorageLaws.Op>, +right_fill: F32, +right_ops: List<&2, StorageLaws.Op>, +index: Nat) -> F32: Elementwise.scalar(operation, Replayed.value(Shape.left_shape(plan), left_fill, left_ops, U32.from_nat(left_map(plan, index))), Replayed.value(Shape.right_shape(plan), right_fill, right_ops, U32.from_nat(right_map(plan, index))))