You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Follow-ups from #536: five items left after the BVD port's API friction #891
The occupancy balance runs a Depletion through Recurrence([true]) with history = zeros(T, 3, 1), only to give it a carrier, and floors the pools with Transform(clamp, 0, Inf).
It then sums pool rows into stocks by hand.
Needed: a Depletion that runs on its own from given pools, with a floor option, and named sums of pools as outputs.
2. Time-varying parameters at absolute time (#536 item 10)
Every time-varying input to the zone run is sliced (and copied) to the run's window.
Needed: start and stop that index time-varying parameters at absolute time, as they already do for x, gain and add, or views without a copy under AD.
Check first whether this already holds for Recurrence and is missing only for Allocate.
kernel_weights(TimeVarying(1 - Survival(hazard), Primary()), n) returns each cohort's CDF, but the name does not say so.
Needed: a decision on whether a cdf_table(hazard, n) / survival_table(hazard, n) pair is worth two names, or a docs recipe.
#889 names the flows of a Depletion without a draw in its trace.
A Depletion with a draw records its pools, the values passed on and the overflows, but not the amount each flow moves.
Needed: that trace to record each flow's amount, then the same naming.
5. Allocation shares and per-patch inputs for the zone split
From the 23 November table on #536: the zone model reads each zone's share of its patch's infections, and maps each patch's force to its zones by hand. :force (#877) and imports by name (#883) cover the rest of that glue.
Needed: Allocate's shares recorded, and a per-patch input mapped to zones by a stratum index.
Item 2 (time-varying parameters at absolute time) already holds on main (9891fc0), for Allocate as for Recurrence.
With full-length inputs and start = t0, Allocate's TimeVarying totals and import fractions are read at absolute time.
The output matches the zone run that slices every input to t0:n and starts at 1.
It also matches with history = zeros(nz, 1) or no history.
r =Recurrence(g; modifiers = (Allocate(groups, TimeVarying(PerStratum(I_bar));
imports = (TimeVarying(PerStratum(frac)), P), spill),))
r(gain; add, start = t0) # equals the run on view(…, :, t0:n) with no start
Only gain and add must then span times 1 to n, as they too are read at absolute time.
A model that builds them for the window alone can keep the view slices, which do not copy.
So item 2 needs no change; it can be ticked.
Item 3 proposal, for a decision (not built): one name, cdf_table(hazard, n), defined as kernel_weights(TimeVarying(1 - Survival(hazard), Primary()), n).
Rationale: the call then says it returns each cohort's delay CDF, and the survival table is 1 .- cdf_table(hazard, n), so a second name is not needed.
The alternative is no new name: a docs recipe under kernel_weights showing this spelling.
Proposal: gain and add as parameters (being built)
The BVD port timed the zone split's inputs and outputs written as plain broadcasts, with no hand rules.
Under Mooncake they cost 5–9× its rules: the inputs exp.(δ'), w0 .* force_pre[zp, days] and gain .* pre take 1515 µs, against 164 µs for the rule.
The time is Mooncake's derived rules for the broadcasts and gathers, not this package's operators.
Change: r(gain; add) accepts a parameter for gain and add, such as PerStratum, TimeVarying or Derived, as well as the arrays it takes now.
The step loop reads it with param(x, k, t), and the rule's reverse pass sends its cotangent through add_param!.
This is the same generic dispatch every modifier parameter uses, so there is no second code path.
So gain = TimeVarying(Derived(exp, PerStratum(δ))) needs no broadcast, and its cotangent comes from the recurrence's own rule.
Arrays keep their current path, and the A/A against main checks that it is unchanged.
Gates: default paths level with main; AD on every backend; the zone inputs case timed against BVD's rule.
The per-zone read of a per-patch input (force_pre[zp, t]) is a gather by group.
If it still costs much as a Derived over a gathered matrix, that will be measured and raised separately.
#894 (a Recurrence's gain and add as parameters) and #896 (Gather(p, rows), a parameter read at another row) let the BVD port's zone split build its inputs with no broadcasts:
D =TimeVarying(PerStratum(δ)) # zones × days
gain =Derived(exp, D)
add =Derived(f, D, PerStratum(w0), Gather(TimeVarying(PerStratum(force)), zone_patch))
Mooncake gradient, 62 zones in 9 patches, T = 220, Allocate with imports.
Each value is the extra time over the run differentiated in its array inputs (about 1100 µs).
zone inputs
extra
broadcasts with force[zone_patch, :] (the port's rule-free spelling)
+1097 µs
parameters with Gather
+496 µs
the port's hand rule, scaled from its own timings
about +120 µs
The generic path halves the broadcasts' cost but stays about 4× the hand rule.
The remainder is per entry and inherent to a generic parameter path: one evaluation of each map and one cotangent store per argument, about 12 ns, type-stable with no allocations.
Evaluating the parameters into arrays once per call gave no speed-up and cost more memory, so it was dropped.
So the port keeps zone_split_inputs' rule, and this part of item 5 is closed.
A keyword for a gain that also scales add (option (ii)) is held.
Cost of traced imports in BVD's health-zone renewal, for tracking.
Setup: BVDOutbreakSize zone renewal kernel, 62 zones in 3 patches, 220 days, mixing on.
Mooncake value_and_gradient!! of sum(abs2, forces) with respect to all five inputs, Julia 1.11, one thread, minimum time.
CR is main with #909, #910 and #911 merged locally.
Variant
Gradient
BVD main: the plain two-row Allocate trace, imports computed in a BVD rule
1.95–2.06 ms
trace_imports = Val(true), imports read from the trace
2.63–2.66 ms
trace_imports = Val(true), imports not read (timing only)
2.16 ms
trace_imports = Val(false), imports not read (timing only)
2.25 ms
trace_imports = Val(true), whole trace converted and then sliced
2.46 ms
The single runs differ by about 0.1–0.2 ms from run to run.
Reading the traced imports back through the named view, and its pullback into the trace, cost about 0.4 ms, beyond the extra trace row itself.
So BVD #1282 keeps main's read-out rules and does not use trace_imports for now.
A cheaper pullback for reading a traced view would let it switch.
In a CR-only copy of the zone case (Mooncake, 1 thread), the Derived gain and add then cost 1.7–2.1 ms, level with the materialised S×T inputs.
Still open: BVD's own zone_share_renewal_kernel with the Derived gain and add reports 3.15–3.32 ms, against 2.42–2.49 ms for its hand rule. Around the call it also has a fancy-index view broadcast for pre and zone_outputs; the CR-only copy has neither. BVD keeps its rules for the zone part for now, so this is not being pursued.
Outcome: three items resolved, two declined; closed
Item
Outcome
1. Stocks
Resolved by the redesign (0a9026f on #907): Recurrence(; modifiers = (Depletion(...; pools = (...)),)) runs a Depletion on its own from its named pools, with no borrowed kernel, and without a history the pools start empty. The occupancy tutorial and modifiers.md use it. A stock is a sum of pool rows, sum(y[rows, :]; dims = 1), which Base already has
2. Time-varying parameters at absolute time
Already held (30 November comment): with start = t0, Allocate's and Recurrence's TimeVarying parameters are read at absolute time
3. A name for a CDF table
Declined as a name, done as a recipe: #925 adds to the Survival docstring that kernel_weights(TimeVarying(1 - Survival(hazard), Primary()), n) is each cohort's cumulative distribution. A second name for one line would be a second way to say the same thing
4. Flows of a Depletion with a draw
Declined for now. A Depletion without a draw records each flow's amount by name (#889), which covers the occupancy model that asked. No model reads the flows of one with a draw, and its trace layout is being settled in #916
5. Allocation shares and per-patch inputs
Resolved: Gather reads a per-patch parameter at each zone (#896), gain and add take parameters (#894), and an Allocate's traced imports are read back from its kept states (#910). The share is the output over its group total. The measured outcome, that BVD keeps its zone-input rule at about 4× the generic path, is in the 30 November comments
Reopen item 4 with the call site if a model needs the flows of a Depletion with a draw.
Activity
seabbs-bot commentedon Nov 30, 2026
Item 2 (time-varying parameters at absolute time) already holds on
main(9891fc0), forAllocateas forRecurrence.With full-length inputs and
start = t0,Allocate'sTimeVaryingtotals and import fractions are read at absolute time.The output matches the zone run that slices every input to
t0:nand starts at 1.It also matches with
history = zeros(nz, 1)or no history.Only
gainandaddmust then span times 1 ton, as they too are read at absolute time.A model that builds them for the window alone can keep the
viewslices, which do not copy.So item 2 needs no change; it can be ticked.
Generated by Claude Code
seabbs-bot commentedon Nov 30, 2026
Item 3 proposal, for a decision (not built): one name,
cdf_table(hazard, n), defined askernel_weights(TimeVarying(1 - Survival(hazard), Primary()), n).Rationale: the call then says it returns each cohort's delay CDF, and the survival table is
1 .- cdf_table(hazard, n), so a second name is not needed.The alternative is no new name: a docs recipe under
kernel_weightsshowing this spelling.Generated by Claude Code
seabbs-bot commentedon Nov 30, 2026
Proposal:
gainandaddas parameters (being built)The BVD port timed the zone split's inputs and outputs written as plain broadcasts, with no hand rules.
Under Mooncake they cost 5–9× its rules: the inputs
exp.(δ'),w0 .* force_pre[zp, days]andgain .* pretake 1515 µs, against 164 µs for the rule.The time is Mooncake's derived rules for the broadcasts and gathers, not this package's operators.
Change:
r(gain; add)accepts a parameter forgainandadd, such asPerStratum,TimeVaryingorDerived, as well as the arrays it takes now.The step loop reads it with
param(x, k, t), and the rule's reverse pass sends its cotangent throughadd_param!.This is the same generic dispatch every modifier parameter uses, so there is no second code path.
So
gain = TimeVarying(Derived(exp, PerStratum(δ)))needs no broadcast, and its cotangent comes from the recurrence's own rule.Arrays keep their current path, and the A/A against
mainchecks that it is unchanged.Gates: default paths level with
main; AD on every backend; the zone inputs case timed against BVD's rule.The per-zone read of a per-patch input (
force_pre[zp, t]) is a gather by group.If it still costs much as a
Derivedover a gathered matrix, that will be measured and raised separately.Generated by Claude Code
seabbs-bot commentedon Nov 30, 2026
Zone inputs: outcome
#894 (a
Recurrence'sgainandaddas parameters) and #896 (Gather(p, rows), a parameter read at another row) let the BVD port's zone split build its inputs with no broadcasts:Mooncake gradient, 62 zones in 9 patches, T = 220,
Allocatewith imports.Each value is the extra time over the run differentiated in its array inputs (about 1100 µs).
force[zone_patch, :](the port's rule-free spelling)GatherThe generic path halves the broadcasts' cost but stays about 4× the hand rule.
The remainder is per entry and inherent to a generic parameter path: one evaluation of each map and one cotangent store per argument, about 12 ns, type-stable with no allocations.
Evaluating the parameters into arrays once per call gave no speed-up and cost more memory, so it was dropped.
So the port keeps
zone_split_inputs' rule, and this part of item 5 is closed.A keyword for a gain that also scales
add(option (ii)) is held.Generated by Claude Code
seabbs-bot commentedon Nov 30, 2026
Cost of traced imports in BVD's health-zone renewal, for tracking.
Setup: BVDOutbreakSize zone renewal kernel, 62 zones in 3 patches, 220 days, mixing on.
Mooncake
value_and_gradient!!ofsum(abs2, forces)with respect to all five inputs, Julia 1.11, one thread, minimum time.CR is main with #909, #910 and #911 merged locally.
trace_imports = Val(true), imports read from the tracetrace_imports = Val(true), imports not read (timing only)trace_imports = Val(false), imports not read (timing only)trace_imports = Val(true), whole trace converted and then slicedThe single runs differ by about 0.1–0.2 ms from run to run.
Reading the traced imports back through the named view, and its pullback into the trace, cost about 0.4 ms, beyond the extra trace row itself.
So BVD #1282 keeps main's read-out rules and does not use
trace_importsfor now.A cheaper pullback for reading a traced view would let it switch.
Generated by Claude Code
seabbs-bot commentedon Nov 30, 2026
Zone inputs: open item after #909–#911
Transpose/Adjoint, soDerived(… TimeVarying(PerStratum(transpose(δ))))gain/add take the rule.Allocate's traced imports read the kept states.In a CR-only copy of the zone case (Mooncake, 1 thread), the
Derivedgain and add then cost 1.7–2.1 ms, level with the materialised S×T inputs.Still open: BVD's own
zone_share_renewal_kernelwith theDerivedgain and add reports 3.15–3.32 ms, against 2.42–2.49 ms for its hand rule. Around the call it also has a fancy-indexviewbroadcast forpreandzone_outputs; the CR-only copy has neither. BVD keeps its rules for the zone part for now, so this is not being pursued.Generated by Claude Code
seabbs-bot commentedon Dec 1, 2026
Outcome: three items resolved, two declined; closed
Recurrence(; modifiers = (Depletion(...; pools = (...)),))runs aDepletionon its own from its named pools, with no borrowed kernel, and without ahistorythe pools start empty. The occupancy tutorial andmodifiers.mduse it. A stock is a sum of pool rows,sum(y[rows, :]; dims = 1), which Base already hasstart = t0,Allocate's andRecurrence'sTimeVaryingparameters are read at absolute timeSurvivaldocstring thatkernel_weights(TimeVarying(1 - Survival(hazard), Primary()), n)is each cohort's cumulative distribution. A second name for one line would be a second way to say the same thingDepletionwith a drawDepletionwithout a draw records each flow's amount by name (#889), which covers the occupancy model that asked. No model reads the flows of one with a draw, and its trace layout is being settled in #916Gatherreads a per-patch parameter at each zone (#896),gainandaddtake parameters (#894), and anAllocate's traced imports are read back from its kept states (#910). The share is the output over its group total. The measured outcome, that BVD keeps its zone-input rule at about 4× the generic path, is in the 30 November commentsReopen item 4 with the call site if a model needs the flows of a
Depletionwith a draw.Generated by Claude Code