# For each tensor name given after the file, reads the tensor with St.read and (for F32, BF16, F16) St.read_f32, and # prints the cell count, the wrapping sum and the xor of the cells of each. test/check_read.py computes the same from # the file with NumPy. import Base import ../w64.bend as W import ../header.bend as H import ../st.bend as ST type Sum is Data: Sum{s: U32, x: U32} @unsafe def fold(done: Bool, +k: U32, +n: U32, a: Array, +s: U32, +x: U32) -> Sum: match done: case True{}: Sum{s, x} case False{}: fold_get(k, n, s, x, Array.get(U32, a, k)) @unsafe def fold_get(+k: U32, +n: U32, +s: U32, +x: U32, r: Array & U32) -> Sum: match r: case (a, +v): fold(U32.is_eq((k + 1 : U32), n), (k + 1 : U32), n, a, (s + v : U32), U32.xor(x, v)) def sum_show(+n: U32, r: Sum) -> String: match r: case Sum{+s, +x}: U32.show(n) ++ " " ++ U32.show(s) ++ " " ++ U32.show(x) @unsafe def sums(+n: U32, a: Array) -> String: sum_show(n, fold(U32.is_eq(n, 0), 0, n, a, 0, 0)) @unsafe def f32_part(ok: Bool, +s: ST.St, +e: H.Ent) -> IO(String): match ok: case True{}: do IO: a : Array <- ST.St.read_f32(s, e, 0, Array.new(U32, ST.depth(ST.Ent.count(e)), 0)) return " f32 " ++ sums(ST.Ent.count(e), a) case False{}: IO.pure(String, "") @unsafe def one(+s: ST.St, +name: String, m: Maybe<&2, H.Ent>) -> IO(Unit): match m: case None{}: IO.print(name ++ " MISSING") case Some{+e}: do IO: a : Array <- ST.St.read(s, e, 0, Array.new(U32, ST.depth(ST.Ent.cells(e)), 0)) f : String <- f32_part(ST.St.can_f32(e), s, e) IO.print(name ++ " raw " ++ sums(ST.Ent.cells(e), a) ++ f) @unsafe def each(+s: ST.St, names: List) -> IO(Unit): match names: case []: IO.pure(Unit, Unit{}) case +n <> t: do IO: one(s, n, ST.St.find(s, n)) each(s, t) @unsafe def opened(names: List, r: Result<&2, &2, String, ST.St>) -> IO(Unit): match r: case Done{s}: each(s, names) case Fail{why}: IO.print("REFUSED " ++ why) @unsafe def arg(xs: List) -> IO(Unit): match xs: case prog <> p <> names: do IO: r : Result<&2, &2, String, ST.St> <- ST.St.open(p) opened(names, r) case _: IO.print("usage: read NAME...") @unsafe def main() -> IO(Unit): do IO: a : List <- IO.args() arg(a)