Repository navigation
ty_holds walks a type family's body, so a bang over a pair no longer puts every closure on the device - #1336
Conversation
045cab3 to
1b74da2
Compare
|
Measured this branch against main on a real program: a Llama inference runner (llama2.c's stories15M) written in Bend. Its only bang is a matmul fork tree that takes Array & Array pairs. M5, macOS, Metal.
|
6e66b08 to
c996bfd
Compare
|
Thanks, the diagnosis is right, and the change to open a family's body is needed. I pushed one commit on top (d3141f9) that turns it into a more general fix of the same root cause. The root cause is wider than pairs
The fix
The polarity is deliberately conservative. Any kind it can't resolve, such as a variable, a stuck family or a def that doesn't reduce, is still walked. A missed closure fails on the device ("a function the device does not hold"), while an extra one only costs device code, which is the cost this PR removes. Sigma's second half has kind The same rule applies to Test
The device overreach itself doesn't show up in any program's output, so the table above is measured rather than pinned by a test. Checks (Apple M4, on this branch)
🤖 Generated with Claude Code |
…puts every closure on the device A & B is Sigma<A, _ => B>. ty_clo answered true for that Lam, so a bang with a pair parameter put every closure of the program on the device. ty_holds now walks a family's body, under the same seen set, before asking its predicate, as type_adts does. Answering false for the Lam instead loses real closures: ty_holds stops at a datatype it has seen, so in U32 & U32 & (U32 -> U32) the inner Sigma's second half went unwalked. The marshal walk had the same hole: U32 & U32 & Nat's Nat crossed the effect seam as a number, U32 & Nat's as a BigInt. compile/gpu_reach_closure_pair applies a closure from a nested pair on the device; io/marshal_nat_pair hands JS effects a Nat at both depths.
…s a value, so an index or a dependent pair no longer puts every closure on the device ty_holds handed every argument of a datatype to its predicate as a type. A pair's family was one argument that is no type; a value index (Box<3n>) and the DUMMY a dependent family opens with (Exists(Nat, n => Box<n>)) are others, and ty_clo took each for a closure: a bang over any of them still put every closure of the program on the device (19 of 20 segments, where 5 is the closure-free baseline). ty_holds now skips an argument only when its parameter's kind is surely no sort: a quantity, a datatype, an equality, or a function into one (ty_value). Any kind it cannot resolve is still walked, since a missed closure fails on the device where an extra one only costs code. A family into a sort, as Sigma's second half, is walked and opened as before. compile/gpu_reach_closure_indexed applies, on the device, a closure that only the walk of a seen pair's family argument reaches, through a value-indexed datatype: it prints 43n on the GPU, the CPU, JS and the interpreter, and fails with "a function the device does not hold" under a walk that skips every argument or one that also skips families. The emitted C, JS and .mjs are byte-identical to the parent's for every file under tests/, demos/ and bench/. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
ce3dc7a to
bf755ce
Compare
…, so a datatype value that carries a type keeps its closures on the device Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
bf755ce to
811715c
Compare
Cause
A & BisSigma<A, _ => B>, so a pair's second half is a type family: aLam.ty_cloanswered true for anyLam, so a bang with a pair parameterset
wide. The device then took every closure infl.clos, plus everythingthose closures reach. Any bang over an array pair hits this.
A bang
head!overArray<U32> & Array<U32>, in amainwith a few host-onlyIO.bindcontinuations (device segments: those the C does not wrap in#if !DEVICE):p: Array<U32> & Array<U32>a: Array<U32>, b: Array<U32>Fix
The change has two parts, both in
ty_holdsincomp.ts:ty_holdswalks aLam's body, astype_adtsalready does, so its predicates no longer see aLam.unless its parameter's kind holds no sort (
ty_value): a quantity, anequality, a datatype that holds none, or a function into one. A datatype
kind is walked with
ty_holdsto tell, because a datatype value can carrya type: in
Hold<b: TyB>withTyB{T: Type}, the argument ofHold<TyB{U32 -> U32}>is what names the function.js_marshalruns a new Nat walk at each level of an effect's type, and eachwalk now reaches into a pair's second half. So a right-nested tuple with a
Nat at the bottom costs O(n²) in its length. This was already so for
nesting through datatype arguments (
Maybe<Maybe<…<Nat>>>). The walk runsonce per foreign def and
.mjsexport, on its signature, not per call, soit is left uncached.
ty_clois unchanged: a closure in either half of a pair, at any depth,still puts the program's closures on the device, and a pair of closure-free
halves no longer does.
The other two
ty_holdswalks had the same blind spot, and the change closesit for both:
js_marshal's Nat walk foundU32 & Nat's Nat throughthe
Sigma's fields. InU32 & U32 & Natit had already seenSigma, sothat Nat crossed the effect seam, and left a
.mjsexport, as anumber,not the
BigIntthatio/marshal_nat_bigintpins. It's aBigIntnow.lay_of's check for a field that re-enters its own datatypenow also sees one through a nested pair's second half. No layout in the
repository changes.
Alternatives
"Lam"toty_clo's list fixes the array pair but loses realclosures.
ty_holdsstops at a datatype it has seen, so inU32 & U32 & (U32 -> U32)nothing reaches the innerSigma's second half,and the lambda minted on the host has no device code.
compile/gpu_reach_closure_pairfails on the GPU with that patch:bend: a function the device does not hold.Lamonly forty_clo, by having it answernullandty_holdsopen it only then, emits the same C everywhere. But it leaves themarshal hole open, and it splits one rule about types between the walk and
one of its predicates. It is also +45 ttok against this PR's +28.
seenby the instantiated type rather than its name also reachesthe inner
Sigma. But polymorphic recursion (Nest<A>holding aNest<List<A>>) has unboundedly many instances; the name is what ends thewalk.
Checks
The branch is rebased on
main(7b7559c).For the whole branch (on one Mac):
compile/gpu_reach_closure_typedis a bang overHold<TyB{U32}> & Hold<TyB{U32 -> U32}>. It prints43non the GPU, theCPU and JS. With the earlier rule, which counted every datatype kind as a
value, it fails on the GPU with
bend: a function the device does not hold.gpu_reach*tests andio/marshal_nat_pairpass on the GPU, the CPUand JS.
tests/that importBase, theemitted
.cand.jsmatch the earlier, larger version of this fixbyte for byte.
(
def Stream(-A: Type) -> Type: A & Stream(A)) makesty_holdswalkwithout end. On
main,type_adtsandfacts_hotalready overflow thestack on such a family, so that bound belongs in its own change, with a
test that builds end to end.
comp.tsis 63,718 ttok, under the 64k cap.bun gates/repo.tspasses (50/50).tsc -p bend2/packreports no errors in
comp.ts.For the first commit only, measured before the later commits:
.bendfiles undertests/,demos/andbench/, 1,169 check. For each, the emitted.c,.jsand.mjsarebyte-identical to
main's except:reg/spin_array_hold, whosefan!takes anArray<U32> & Array<U32>. Its device segments go from 27 of 28 to 8.io/marshal_nat_pair..mjs:run/literal_sugar,show/literal_readbackandio/readback_sugars. Each exports a Nat in a nested pair's second half,which is now a
BigInt.compile/gpu_reach_closure_pairhands the device a closure in the secondhalf of a pair's second half and applies it there. It prints
43non theGPU, the CPU, JS and the interpreter. It also passes on
main, which putsevery closure on the device; it guards the first alternative above.
io/marshal_nat_pairhands two JS effectsU32 & NatandU32 & U32 & Natand prints the Nat'stypeof.mainprintsbigint,number.gates/test.ts's own shard script on one Mac, withthe ssh hop swapped for a local shell. Result: PASS: 1568 / 1571. The 3
failures fail the same way on
mainthere:gfx_clicksandgfx_windowexpect no display, but this Mac has one.io_process_runfails its inherited-pipe cases.PAR-GPU columns cannot move. Each prints its pinned OUTPUT in all three
modes.
-o main.c, hyperfine, 15 runs each): branch/maingeomean 1.008 with
mainrun first and 0.985 with the branch first. Nobench moves more than 4%.
-o main, median of 5, interleaved): 0.98–1.04×.ALL PROOFS CHECK, and the emitted code is identical.A median of 3 gave 0.97–1.06×. I re-measured
generics_3200's 1.06 withhyperfine, 30 runs in each order: 0.988 with
mainfirst and 1.040 withthe branch first. The ratio follows the run order, not the code.
gates/perf.tsagainst the apple_m4 pins,and
gates/test.tson the minis), and CUDA, Linux and x86.🤖 Generated with Claude Code