~/bend-docscommunity

test/list.bend source

test/list.bend on the hub · documented module

# Prints one line a tensor (name, dtype, shape, data_offsets as hex) for the file given as the argument, sorted by# offset, then the metadata count and the data size; or the reason the file is refused. test/check_list.py compares# the output with Python's json reading of the same header.import Baseimport ../w64.bend as Wimport ../header.bend as Himport ../st.bend as STdef dims(xs: List<&2, U32>) -> String:  match xs:    case []:      ""    case h <> []:      U32.show(h)    case h <> t:      U32.show(h) ++ "," ++ dims(t)def line(e: H.Ent) -> String:  match e:    case H.Ent{n, d, s, b, f}:      n ++ "\t" ++ H.DType.name(d) ++ "\t" ++ dims(s) ++ "\t" ++ W.W64.show(b) ++ "\t" ++ W.W64.show(f)def show_all(es: List<&2, H.Ent>) -> IO(Unit):  match es:    case []:      IO.pure(Unit, Unit{})    case e <> t:      do IO<Unit>:        IO.print(line(e))        show_all(t)def len(xs: List<&2, H.KV>, +n: U32) -> U32:  match xs:    case []:      n    case h <> t:      len(t, (n + 1 : U32))def report(r: Result<&2, &2, String, ST.St>) -> IO(Unit):  match r:    case Done{ST.St{p, st, H.Header{es, m, n}}}:      do IO<Unit>:        show_all(es)        IO.print("meta\t" ++ U32.show(len(m, 0)) ++ "\tsize\t" ++ W.W64.show(n) ++ "\tstart\t" ++ W.W64.show(st))    case Fail{why}:      IO.print("REFUSED\t" ++ why)def arg(xs: List<String>) -> IO(Unit):  match xs:    case [prog, p]:      do IO<Unit>:        r : Result<&2, &2, String, ST.St> <- ST.St.open(p)        report(r)    case _:      IO.print("usage: list <file.safetensors>")def main() -> IO(Unit):  do IO<Unit>:    a : List<String> <- IO.args()    arg(a)