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