import Base import ../storage/model.bend as Model import ../storage/refinement.bend as Refinement import ./matmul.bend as Matrix import ./matrix_index_proof.bend as MatrixIndex import ./dot_fold.bend as DotFold import ./elementwise.bend as Elementwise import ./elementwise_parallel_proof.bend as Parallel import ./parallel_proofs.bend as Generic # Proofs that the parallel matmul build writes, at every cell of its output # tree, the value the cell's dot names: for two balanced arrays A and B, a # cell below `count` holds `DotFold.dot_fold` of the words A and B hold at # the cell's row and column, in increasing k from +0.0, and a cell above it # holds +0.0. Every read keeps the array it reads and every clone is two # copies of it, so the whole build reads A and B. # The factor pairs the terms `cursor .. cursor + fuel` of cell (row, column) # read: left[row * inner + k] and right[k * columns + column]. def LogicalArr(+fuel: Nat, +dl: Nat, +dr: Nat, -A: Array, -B: Array, +inner: Nat, +columns: Nat, +row: Nat, +column: Nat, +cursor: Nat, values: List<&2, DotFold.Factors>) -> Data: match fuel values: case 0n Nil{}: Unit case 1n+tail DotFold.Factors{lv, rv} <> rest: ArrStep case 0n DotFold.Factors{lv, rv} <> rest: Empty case 1n+tail Nil{}: Empty type ArrStep<-fuel: Nat, -dl: Nat, -dr: Nat, -A: Array, -B: Array, -inner: Nat, -columns: Nat, -row: Nat, -column: Nat, -cursor: Nat, -lv: F32, -rv: F32, -values: List<&2, DotFold.Factors>> is Data: ArrStep{-left_observed: {Model.observe(F32, dl, A, Refinement.route(dl, Matrix.left_index(inner, row, cursor))) == Some{lv} : Maybe<&2, F32>}, -right_observed: {Model.observe(F32, dr, B, Refinement.route(dr, Matrix.right_index(columns, column, cursor))) == Some{rv} : Maybe<&2, F32>}, -remaining: LogicalArr(fuel, dl, dr, A, B, inner, columns, row, column, 1n+cursor, values)} # A run of `cell_dot`: its total folds the factor pairs it read, and both # arrays come back unchanged. type DotRun<-fuel: Nat, -dl: Nat, -dr: Nat, -A: Array, -B: Array, -inner: Nat, -columns: Nat, -row: Nat, -column: Nat, -cursor: Nat, -accumulator: F32, -result: F32 & (Array & Array)> is Data: DotRun{-values: List<&2, DotFold.Factors>, -logical: LogicalArr(fuel, dl, dr, A, B, inner, columns, row, column, cursor, values), -fold: {Pair.fst(F32, Array & Array, result) == DotFold.dot_fold(values, accumulator) : F32}, -left_kept: {Pair.fst(Array, Array, Pair.snd(F32, Array & Array, result)) == A : Array}, -right_kept: {Pair.snd(Array, Array, Pair.snd(F32, Array & Array, result)) == B : Array}} def dot_extend(+rest: Nat, +dl: Nat, +dr: Nat, -A: Array, -B: Array, +inner: Nat, +columns: Nat, +row: Nat, +column: Nat, +cursor: Nat, -accumulator: F32, -x: F32, -y: F32, -ex: {Model.observe(F32, dl, A, Refinement.route(dl, Matrix.left_index(inner, row, cursor))) == Some{x} : Maybe<&2, F32>}, -ey: {Model.observe(F32, dr, B, Refinement.route(dr, Matrix.right_index(columns, column, cursor))) == Some{y} : Maybe<&2, F32>}, -result: F32 & (Array & Array), -run: Parallel.Fact>) -> Parallel.Fact>: Parallel.Fact{{\{DotRun: values => logical => fold => left_kept => right_kept => DotRun{DotFold.Factors{x, y} <> values, ArrStep{ex, ey, logical}, fold, left_kept, right_kept}} : DotRun -> DotRun<1n+rest, dl, dr, A, B, inner, columns, row, column, cursor, accumulator, result>}( {\{Parallel.Fact: p => p} : Parallel.Fact> -> DotRun}(run))} # The last term of a run: the reads hold the words of A and B, and both # arrays come back. def run_last(+dl: Nat, +dr: Nat, -A: Array, -B: Array, +inner: Nat, +columns: Nat, +row: Nat, +column: Nat, +cursor: Nat, -accumulator: F32, -la: Array, -ra: Array, -el: {la == A : Array}, -er: {ra == B : Array}, -left_valid: Model.Balanced(F32, dl, A), -right_valid: Model.Balanced(F32, dr, B), pl: Array & F32, -exact_l: {pl == Array.get(F32, la, Matrix.left_index(inner, row, cursor)) : Array & F32}, pr: Array & F32, -exact_r: {pr == Array.get(F32, ra, Matrix.right_index(columns, column, cursor)) : Array & F32}) -> Parallel.Fact>: (kl, x) = pl (kr, y) = pr -ex = Equal.sym(Maybe<&2, F32>, Some{x}, Model.observe(F32, dl, A, Refinement.route(dl, Matrix.left_index(inner, row, cursor))), {\{Parallel.Fact: p => p} : Parallel.Fact<({Some{Refinement.second(F32, (kl, x))} == Model.observe(F32, dl, A, Refinement.route(dl, Matrix.left_index(inner, row, cursor))) : Maybe<&2, F32>})> -> {Some{Refinement.second(F32, (kl, x))} == Model.observe(F32, dl, A, Refinement.route(dl, Matrix.left_index(inner, row, cursor))) : Maybe<&2, F32>}}( Parallel.read_value(dl, la, A, Matrix.left_index(inner, row, cursor), (kl, x), exact_l, el, left_valid))) -ey = Equal.sym(Maybe<&2, F32>, Some{y}, Model.observe(F32, dr, B, Refinement.route(dr, Matrix.right_index(columns, column, cursor))), {\{Parallel.Fact: p => p} : Parallel.Fact<({Some{Refinement.second(F32, (kr, y))} == Model.observe(F32, dr, B, Refinement.route(dr, Matrix.right_index(columns, column, cursor))) : Maybe<&2, F32>})> -> {Some{Refinement.second(F32, (kr, y))} == Model.observe(F32, dr, B, Refinement.route(dr, Matrix.right_index(columns, column, cursor))) : Maybe<&2, F32>}}( Parallel.read_value(dr, ra, B, Matrix.right_index(columns, column, cursor), (kr, y), exact_r, er, right_valid))) -kl_kept = {\{Parallel.Fact: p => p} : Parallel.Fact<({Refinement.first(F32, (kl, x)) == A : Array})> -> {Refinement.first(F32, (kl, x)) == A : Array}}( Parallel.read_keeps(dl, la, A, Matrix.left_index(inner, row, cursor), (kl, x), exact_l, el, left_valid)) -kr_kept = {\{Parallel.Fact: p => p} : Parallel.Fact<({Refinement.first(F32, (kr, y)) == B : Array})> -> {Refinement.first(F32, (kr, y)) == B : Array}}( Parallel.read_keeps(dr, ra, B, Matrix.right_index(columns, column, cursor), (kr, y), exact_r, er, right_valid)) dot_extend(0n, dl, dr, A, B, inner, columns, row, column, cursor, accumulator, x, y, ex, ey, (F32.add(accumulator, F32.mul(x, y)), (kl, kr)), Parallel.Fact{DotRun{Nil{}, Unit{}, {==}, kl_kept, kr_kept}}) # The offsets of the next term are the stepped ones. def run_stepped(+rest: Nat, +dl: Nat, +dr: Nat, -A: Array, -B: Array, +inner: Nat, +columns: Nat, +row: Nat, +column: Nat, +cursor: Nat, -value: F32, -l: Array, -r: Array, run: Parallel.Fact>) -> Parallel.Fact>: %Equal.sym(Nat, Nat.add(1n, Nat.add(Nat.mul(row, inner), cursor)), Nat.add(Nat.mul(row, inner), 1n+cursor), MatrixIndex.stepped_left(inner, row, cursor)) : Parallel.Fact> %Equal.sym(Nat, Nat.add(columns, Nat.add(Nat.mul(cursor, columns), column)), Nat.add(Nat.mul(1n+cursor, columns), column), MatrixIndex.stepped_right(columns, column, cursor)) : Parallel.Fact> run # A term with more after it: its reads hold the words of A and B, and the # run goes on from the next term's reads of the arrays they hand back. def run_next(+rest: Nat, +dl: Nat, +dr: Nat, -A: Array, -B: Array, +inner: Nat, +columns: Nat, +row: Nat, +column: Nat, +cursor: Nat, -accumulator: F32, -la: Array, -ra: Array, -el: {la == A : Array}, -er: {ra == B : Array}, -left_valid: Model.Balanced(F32, dl, A), -right_valid: Model.Balanced(F32, dr, B), pl: Array & F32, -exact_l: {pl == Array.get(F32, la, Matrix.left_index(inner, row, cursor)) : Array & F32}, pr: Array & F32, -exact_r: {pr == Array.get(F32, ra, Matrix.right_index(columns, column, cursor)) : Array & F32}, -next: @value: F32 -> @-l: Array -> @-r: Array -> @-el: {l == A : Array} -> @-er: {r == B : Array} -> Parallel.Fact>) -> Parallel.Fact>: (kl, x) = pl (kr, y) = pr -ex = Equal.sym(Maybe<&2, F32>, Some{x}, Model.observe(F32, dl, A, Refinement.route(dl, Matrix.left_index(inner, row, cursor))), {\{Parallel.Fact: p => p} : Parallel.Fact<({Some{Refinement.second(F32, (kl, x))} == Model.observe(F32, dl, A, Refinement.route(dl, Matrix.left_index(inner, row, cursor))) : Maybe<&2, F32>})> -> {Some{Refinement.second(F32, (kl, x))} == Model.observe(F32, dl, A, Refinement.route(dl, Matrix.left_index(inner, row, cursor))) : Maybe<&2, F32>}}( Parallel.read_value(dl, la, A, Matrix.left_index(inner, row, cursor), (kl, x), exact_l, el, left_valid))) -ey = Equal.sym(Maybe<&2, F32>, Some{y}, Model.observe(F32, dr, B, Refinement.route(dr, Matrix.right_index(columns, column, cursor))), {\{Parallel.Fact: p => p} : Parallel.Fact<({Some{Refinement.second(F32, (kr, y))} == Model.observe(F32, dr, B, Refinement.route(dr, Matrix.right_index(columns, column, cursor))) : Maybe<&2, F32>})> -> {Some{Refinement.second(F32, (kr, y))} == Model.observe(F32, dr, B, Refinement.route(dr, Matrix.right_index(columns, column, cursor))) : Maybe<&2, F32>}}( Parallel.read_value(dr, ra, B, Matrix.right_index(columns, column, cursor), (kr, y), exact_r, er, right_valid))) -kl_kept = {\{Parallel.Fact: p => p} : Parallel.Fact<({Refinement.first(F32, (kl, x)) == A : Array})> -> {Refinement.first(F32, (kl, x)) == A : Array}}( Parallel.read_keeps(dl, la, A, Matrix.left_index(inner, row, cursor), (kl, x), exact_l, el, left_valid)) -kr_kept = {\{Parallel.Fact: p => p} : Parallel.Fact<({Refinement.first(F32, (kr, y)) == B : Array})> -> {Refinement.first(F32, (kr, y)) == B : Array}}( Parallel.read_keeps(dr, ra, B, Matrix.right_index(columns, column, cursor), (kr, y), exact_r, er, right_valid)) dot_extend(1n+rest, dl, dr, A, B, inner, columns, row, column, cursor, accumulator, x, y, ex, ey, Matrix.product_run(rest, 1n, columns, Nat.add(1n, Nat.add(Nat.mul(row, inner), cursor)), Nat.add(columns, Nat.add(Nat.mul(cursor, columns), column)), F32.add(accumulator, F32.mul(x, y)), Array.get(F32, kl, U32.from_nat(Nat.add(1n, Nat.add(Nat.mul(row, inner), cursor)))), Array.get(F32, kr, U32.from_nat(Nat.add(columns, Nat.add(Nat.mul(cursor, columns), column))))), run_stepped(rest, dl, dr, A, B, inner, columns, row, column, cursor, F32.add(accumulator, F32.mul(x, y)), kl, kr, next(F32.add(accumulator, F32.mul(x, y)))(kl)(kr)(kl_kept)(kr_kept))) # A run of fuel + 1 terms from cursor folds the factor pairs it read, and # both arrays come back. def run_eq(+fuel: Nat, +dl: Nat, +dr: Nat, -A: Array, -B: Array, +inner: Nat, +columns: Nat, +row: Nat, +column: Nat, +cursor: Nat, -accumulator: F32, -la: Array, -ra: Array, -el: {la == A : Array}, -er: {ra == B : Array}, -left_valid: Model.Balanced(F32, dl, A), -right_valid: Model.Balanced(F32, dr, B), pl: Array & F32, -exact_l: {pl == Array.get(F32, la, Matrix.left_index(inner, row, cursor)) : Array & F32}, pr: Array & F32, -exact_r: {pr == Array.get(F32, ra, Matrix.right_index(columns, column, cursor)) : Array & F32}) -> Parallel.Fact>: match fuel: case 0n: run_last(dl, dr, A, B, inner, columns, row, column, cursor, accumulator, la, ra, el, er, left_valid, right_valid, pl, exact_l, pr, exact_r) case 1n+rest: run_next(rest, dl, dr, A, B, inner, columns, row, column, cursor, accumulator, la, ra, el, er, left_valid, right_valid, pl, exact_l, pr, exact_r, value => l => r => el => er => run_eq(rest, dl, dr, A, B, inner, columns, row, column, 1n+cursor, value, l, r, el, er, left_valid, right_valid, Array.get(F32, l, Matrix.left_index(inner, row, 1n+cursor)), {==}, Array.get(F32, r, Matrix.right_index(columns, column, 1n+cursor)), {==})) def dot_run(fuel: Nat, +dl: Nat, +dr: Nat, -A: Array, -B: Array, +inner: Nat, +columns: Nat, +row: Nat, +column: Nat, +cursor: Nat, -accumulator: F32, la: Array, ra: Array, -el: {la == A : Array}, -er: {ra == B : Array}, -left_valid: Model.Balanced(F32, dl, A), -right_valid: Model.Balanced(F32, dr, B)) -> Parallel.Fact>: match fuel: case 0n: Parallel.Fact{DotRun{Nil{}, Unit{}, {==}, el, er}} case 1n+rest: run_eq(rest, dl, dr, A, B, inner, columns, row, column, cursor, accumulator, la, ra, el, er, left_valid, right_valid, Array.get(F32, la, Matrix.left_index(inner, row, cursor)), {==}, Array.get(F32, ra, Matrix.right_index(columns, column, cursor)), {==}) # What output cell `position` holds: +0.0 at or above `count`; below it, a # word that folds the factor pairs of row `position / columns` and column # `position % columns`, in increasing k from +0.0. type CellAt<-dl: Nat, -dr: Nat, -A: Array, -B: Array, -inner: Nat, -columns: Nat, -position: Nat, -value: Maybe<&2, F32>> is Data: CellAt{-word: F32, -observed: {value == Some{word} : Maybe<&2, F32>}, -values: List<&2, DotFold.Factors>, -logical: LogicalArr(inner, dl, dr, A, B, inner, columns, Matrix.row_of(columns, position), Matrix.column_of(columns, position), 0n, values), -fold: {word == DotFold.dot_fold(values, 0.0) : F32}} def CellSpec(+dl: Nat, +dr: Nat, -A: Array, -B: Array, +inner: Nat, +columns: Nat, +position: Nat, inside: Bool, -value: Maybe<&2, F32>) -> Type: match inside: case False{}: {value == Some{0.0} : Maybe<&2, F32>} case True{}: CellAt def CellIs(+dl: Nat, +dr: Nat, -A: Array, -B: Array, +inner: Nat, +columns: Nat, +count: Nat, +position: Nat, -value: Maybe<&2, F32>) -> Type: CellSpec(dl, dr, A, B, inner, columns, position, Nat.is_lt(position, count), value) # Every slot of `out`, at index `base + offset`, holds what CellIs names. def Cells(+depth: Nat, +dl: Nat, +dr: Nat, -A: Array, -B: Array, +inner: Nat, +columns: Nat, +count: Nat, +base: Nat, -out: Array) -> Type: @+path: Model.Path(depth) -> CellIs(dl, dr, A, B, inner, columns, count, Nat.add(Model.offset(depth, path), base), Model.observe(F32, depth, out, path)) # The matmul cell function of `Parallel.build`, from the input arrays. def cell_of(+inner: Nat, +columns: Nat, +position: Nat, A: Array, B: Array, inside: Bool) -> F32 & (Array & Array): Matrix.cell_at(Matrix.dims(Matrix.Dims{inner, columns}), position, (A, B), inside) def eta(-X: Type, -Y: Type, pair: X & Y) -> {pair == (Pair.fst(X, Y, pair), Pair.snd(X, Y, pair)) : X & Y}: (x, y) = pair {==} # Every cell hands both input arrays back. def cell_keeps(+dl: Nat, +dr: Nat, A: Array, B: Array, +inner: Nat, +columns: Nat, +position: Nat, inside: Bool, -left_valid: Model.Balanced(F32, dl, A), -right_valid: Model.Balanced(F32, dr, B)) -> Parallel.Fact<{Pair.snd(F32, Array & Array, cell_of(inner, columns, position, A, B, inside)) == (A, B) : Array & Array}>: match inside: case False{}: Parallel.Fact{{==}} case True{}: Parallel.Fact{{\{DotRun: values => logical => fold => left_kept => right_kept => Equal.trans(Array & Array, Pair.snd(F32, Array & Array, cell_of(inner, columns, position, A, B, True{})), (Pair.fst(Array, Array, Pair.snd(F32, Array & Array, cell_of(inner, columns, position, A, B, True{}))), Pair.snd(Array, Array, Pair.snd(F32, Array & Array, cell_of(inner, columns, position, A, B, True{})))), (A, B), eta(Array, Array, Pair.snd(F32, Array & Array, cell_of(inner, columns, position, A, B, True{}))), Parallel.pair_equal(Pair.fst(Array, Array, Pair.snd(F32, Array & Array, cell_of(inner, columns, position, A, B, True{}))), Pair.snd(Array, Array, Pair.snd(F32, Array & Array, cell_of(inner, columns, position, A, B, True{}))), A, B, left_kept, right_kept))} : DotRun -> {Pair.snd(F32, Array & Array, cell_of(inner, columns, position, A, B, True{})) == (A, B) : Array & Array}}( {\{Parallel.Fact: p => p} : Parallel.Fact> -> DotRun}( dot_run(inner, dl, dr, A, B, inner, columns, Matrix.row_of(columns, position), Matrix.column_of(columns, position), 0n, 0.0, A, B, {==}, {==}, left_valid, right_valid)))} # What the cell function computes is what CellSpec names. def cell_spec(+dl: Nat, +dr: Nat, A: Array, B: Array, +inner: Nat, +columns: Nat, +position: Nat, inside: Bool, -observed: Maybe<&2, F32>, -computed: {observed == Some{Pair.fst(F32, Array & Array, cell_of(inner, columns, position, A, B, inside))} : Maybe<&2, F32>}, -left_valid: Model.Balanced(F32, dl, A), -right_valid: Model.Balanced(F32, dr, B)) -> Parallel.Fact: match inside: case False{}: Parallel.Fact{computed} case True{}: Parallel.Fact{{\{DotRun: values => logical => fold => left_kept => right_kept => CellAt{Pair.fst(F32, Array & Array, cell_of(inner, columns, position, A, B, True{})), computed, values, logical, fold}} : DotRun -> CellAt}( {\{Parallel.Fact: p => p} : Parallel.Fact> -> DotRun}( dot_run(inner, dl, dr, A, B, inner, columns, Matrix.row_of(columns, position), Matrix.column_of(columns, position), 0n, 0.0, A, B, {==}, {==}, left_valid, right_valid)))} # Whatever the split, every cell of the parallel build holds what CellIs # names: the generic law of `Parallel.build`, with the matmul cell. def build_cells(+depth: Nat, +splits: Nat, +dl: Nat, +dr: Nat, -A: Array, -B: Array, +inner: Nat, +columns: Nat, +count: Nat, +base: Nat, la: Array, ra: Array, -el: {la == A : Array}, -er: {ra == B : Array}, -left_valid: Model.Balanced(F32, dl, A), -right_valid: Model.Balanced(F32, dr, B)) -> Parallel.Fact: -split = {\{Parallel.Fact: q => q} : Parallel.Fact<({Pair.fst(Array & Array, Array & Array, Elementwise.split((A, B))) == (A, B) : Array & Array} & {Pair.snd(Array & Array, Array & Array, Elementwise.split((A, B))) == (A, B) : Array & Array})> -> {Pair.fst(Array & Array, Array & Array, Elementwise.split((A, B))) == (A, B) : Array & Array} & {Pair.snd(Array & Array, Array & Array, Elementwise.split((A, B))) == (A, B) : Array & Array}}( Parallel.split_equal(dl, dr, A, B, left_valid, right_valid)) -cells = {\{Generic.Fact: q => q} : Generic.Fact, ~Array, ~Matrix.cell_at, Matrix.dims(Matrix.Dims{inner, columns}), (A, B), depth, count, base, Matrix.build(depth, splits, inner, columns, count, base, la, ra))> -> Generic.Cells(~Matrix.Dims, ~Array, ~Array, ~Matrix.cell_at, Matrix.dims(Matrix.Dims{inner, columns}), (A, B), depth, count, base, Matrix.build(depth, splits, inner, columns, count, base, la, ra))}( Generic.build_cells(~Matrix.Dims, ~Matrix.Dims, ~Array, ~Array, ~Matrix.dims, ~Elementwise.split, ~Matrix.cell_at, Matrix.Dims{inner, columns}, (A, B), depth, splits, count, base, (la, ra), Parallel.pair_equal(la, ra, A, B, el, er), at => inside => {\{Parallel.Fact: q => q} : Parallel.Fact<({Pair.snd(F32, Array & Array, cell_of(inner, columns, at, A, B, inside)) == (A, B) : Array & Array})> -> {Pair.snd(F32, Array & Array, cell_of(inner, columns, at, A, B, inside)) == (A, B) : Array & Array}}( cell_keeps(dl, dr, A, B, inner, columns, at, inside, left_valid, right_valid)), Pair.fst({Pair.fst(Array & Array, Array & Array, Elementwise.split((A, B))) == (A, B) : Array & Array}, {Pair.snd(Array & Array, Array & Array, Elementwise.split((A, B))) == (A, B) : Array & Array}, split), Pair.snd({Pair.fst(Array & Array, Array & Array, Elementwise.split((A, B))) == (A, B) : Array & Array}, {Pair.snd(Array & Array, Array & Array, Elementwise.split((A, B))) == (A, B) : Array & Array}, split))) Parallel.Fact{path => {\{Parallel.Fact: q => q} : Parallel.Fact -> CellSpec(dl, dr, A, B, inner, columns, Nat.add(Model.offset(depth, path), base), Nat.is_lt(Nat.add(Model.offset(depth, path), base), count), Model.observe(F32, depth, Matrix.build(depth, splits, inner, columns, count, base, la, ra), path))}( cell_spec(dl, dr, A, B, inner, columns, Nat.add(Model.offset(depth, path), base), Nat.is_lt(Nat.add(Model.offset(depth, path), base), count), Model.observe(F32, depth, Matrix.build(depth, splits, inner, columns, count, base, la, ra), path), cells(path), left_valid, right_valid))} # A cell named at an equal index, and a CellIs below the count is a CellAt. def cell_shift(+dl: Nat, +dr: Nat, -A: Array, -B: Array, +inner: Nat, +columns: Nat, +count: Nat, -position: Nat, -other: Nat, -value: Maybe<&2, F32>, -same: {position == other : Nat}, -at: CellIs(dl, dr, A, B, inner, columns, count, position, value)) -> Parallel.Fact: Parallel.Fact{ %same : CellIs(dl, dr, A, B, inner, columns, count, _, value) at} def cell_below(+dl: Nat, +dr: Nat, -A: Array, -B: Array, +inner: Nat, +columns: Nat, +count: Nat, -position: Nat, -value: Maybe<&2, F32>, -below: {Nat.is_lt(position, count) == True{} : Bool}, -at: CellIs(dl, dr, A, B, inner, columns, count, position, value)) -> Parallel.Fact>: Parallel.Fact{ %below : CellSpec(dl, dr, A, B, inner, columns, position, _, value) at}