Repository navigation
Description
Problem
kernel_weights on TimeVarying(1 - Survival(hazard), Primary()) has a hazard Derived((a, b) -> logistic(a + b), h0, TimeVarying(γ)).
Its Mooncake gradient takes 1.8 times as long as a hand-written loop and rule that compute the same CDF table.
About three quarters of that gap is the local derivative of the Derived map, which the walk takes with ForwardDiff dual numbers at every entry.
Reproducer
The table is D × nu, with D = 28 delays and nu = 101 cohorts.
Entry (j, k) is 1 - Π_{i ≤ j} (1 - h_i), where h_i = logistic(h0[i] + γk[k + i - 1]).
using ComposableRecurrences: TimeVarying, Primary, Derived, Survival, kernel_weights
using StatsFuns: logistic
_logistic_sum(a, b) = logistic(a + b)
function table(h0, γk, nu)
hazard = Derived(_logistic_sum, h0, TimeVarying(γk))
return kernel_weights(TimeVarying(1 - Survival(hazard), Primary()), nu)
endThe reference is a hand loop over the cohorts with a Mooncake rule.
The loop keeps each hazard h and survival S in the forward pass.
The pullback is one backward running sum per column, x̄_i = h_i Σ_{j ≥ i} t̄_j S_j, which uses dh/dx = h(1 - h).
Measurements
Main 21d7092, Julia 1.13.4, one thread, Mooncake value_and_pullback!! with a prepared cache, minimum µs.
Tables are identical, and the h0 gradients agree to 7e-16.
| arm | forward | gradient | bytes | allocations |
|---|---|---|---|---|
| hand loop and rule | 13.3–14.1 | 21.0–23.7 | 91,296 | 14 |
table above |
15.5–16.8 | 36.1–39.0 | 136,600 | 17 |
Where the gradient goes
Inclusive profile of the package arm's gradient:
| part | share |
|---|---|
rule forward (_weights_walk) |
77% |
of which, the hazard's value and partials (_value_and_partials → _forward_value_and_derivative) |
48% |
of which, ForwardDiff logistic on duals |
38% |
reverse walk (_walk_weights_back!) |
12% |
scratch allocation (_walk_scratch, _alloc) |
7% |
_zero_output |
6% |
Per entry, measured alone over 2,828 seeded inputs:
| evaluation | ns per entry |
|---|---|
logistic(a + b) |
2.9 |
the same, plus h(1 - h) from the value |
2.9 |
| a dual with one partial | 7.6 |
| a dual with two partials (what the walk does) | 7.4 |
The dual rule for logistic evaluates the function again for its derivative, so each entry costs about 4.5 ns more than the value alone.
Over the table's 2,828 entries that is about 13 µs of the 15–17 µs gap.
To bound the lever, I defined _value_and_partials for this one map as the closed form (h, (h(1 - h), h(1 - h))).
That took the gradient from 36.1–37.7 to 30.1 µs (0.81×), with the gradient unchanged to 8e-16.
The rest of the gap, about 7–8 µs, is the walk's machinery: scratch arrays, records, the output cotangent and the reverse walk.
No single part of it is above 7%.
Writing the hazard as Derived(logistic, Derived(+, h0, TimeVarying(γk))) uses the closed-form partials of +.
It takes the same time (36.5–38.4 µs), because logistic still goes through duals, but it uses 16% less memory (114,008 bytes).
Options
- A
derivativekeyword onDerived, asTransformhas, called asderivative(xs...)and returning the partials.
This is new public API, so it needs a maintainer decision.
It would cover any map, including closures. - Closed-form partials for common link functions, for example
logistic,log1pexpandlogitthrough aLogExpFunctionspackage extension.
This is internal, and needs no API.
It reaches users who write the link as its ownDerived, as in the nested form above, but not a closure such as_logistic_sum. - Leave the dual path. The 0.81× bound above is the most either option gains on this case.
I'd suggest option 2, plus documenting the nested spelling, unless option 1 is wanted for closures.
Activity
seabbs-bot commentedon Dec 1, 2026
Outcome: the option the issue recommends is in; closed
The issue recommends option 2, closed-form partials for the link functions, with option 1 for closures.
Both exist since #880 (4f6633d).
logistic,log1pexpandlogita closed-form local derivative, soDerived(logistic, Derived(+, h0, TimeVarying(γk)))takes no dual numbers.Localrole:pullback!(grads, ::typeof(f), ::Local, y, args...)returns them.test/unit/weights.jl("Computed kernels: a map's local rule") checks it against ForwardDiff.Derivedkernel.Measured on the redesigned branch (#907 at 21fb0bb), Julia 1.13.4, Mooncake 0.5.74, one thread,
benchmark/matrix.jl --tier=realistic --targets="Mooncake reverse", minimum times.This machine is shared and the same arm moved by up to 50% between runs, so each ratio is given for every run made.
The benchmark's
bvd_onsetcase is this table, against the hand loop with its hand rule.It still writes the hazard as the closure
_logistic_sum, so these times are with dual numbers, the slower of the two spellings.So the gap is 1.1× to 1.6× with dual numbers, against 1.8× when filed, and the issue bounds the closed form's gain at a further 0.81×.
What is then left is the generic cohort walk against a fused hand loop, which the issue measured at 7 to 8 µs with no part above 7%.
That remainder is not this issue's subject; it is tracked with the other computed kernels in #572.
Generated by Claude Code