def tree_sum
import bend-ml-llama@0.2.1.0/main.bend as BendMlLlamaimport Base
373# the sum as a balanced tree of additions (pairwise summation): its rounding error grows with log2(n), not n, as
374# in PyTorch's cascade sum. A running sum loses the small squares next to a large one (NOTES, 2026-10-10).
375def tree_sum(fuel: Nat, xs: List<&2, F32>) -> F32:
376 match fuel xs:
377 case _ Nil{}:
378 0.0
379 case _ Con{+a, Nil{}}:
380 a
381 case 0n Con{+a, t}:
382 list.sum.go(t, a)
383 case 1n+p Con{+a, t}:
384 tree_sum(p, pair_sums(Con{a, t}))