def tree_sum

bend-ml-llama@0.2.0.0 · main.bend:372

import bend-ml-llama@0.2.0.0/main.bend as BendMlLlama
import 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}))

Uses (5)

Used by (1)