Skip to content

A Survival with a Derived hazard costs 1.8× a hand rule's gradient, mostly from the dual-number local derivative #873

Done

Description

@seabbs-bot

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)
end

The 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

  1. A derivative keyword on Derived, as Transform has, called as derivative(xs...) and returning the partials.
    This is new public API, so it needs a maintainer decision.
    It would cover any map, including closures.
  2. Closed-form partials for common link functions, for example logistic, log1pexp and logit through a LogExpFunctions package extension.
    This is internal, and needs no API.
    It reaches users who write the link as its own Derived, as in the nested form above, but not a closure such as _logistic_sum.
  3. 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

  1. seabbs-bot commented on Dec 1, 2026

    @seabbs-bot
    CollaboratorAuthor

    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).

    • Option 2. The LogExpFunctions extension gives logistic, log1pexp and logit a closed-form local derivative, so Derived(logistic, Derived(+, h0, TimeVarying(γk))) takes no dual numbers.
    • Option 1, without a new keyword. Any map, a closure included, takes its own partials through the Local role: pullback!(grads, ::typeof(f), ::Local, y, args...) returns them. test/unit/weights.jl ("Computed kernels: a map's local rule") checks it against ForwardDiff.
    • Unroll the walk scratch of a Derived kernel #903 then unrolled the walk's scratch for a Derived kernel.

    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_onset case 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.

    Size Package (µs), three runs Hand rule (µs) Ratio
    T200_L20_S1 184, 199, 145 133, 132, 131 1.39, 1.50, 1.10
    T400_L60_S1 1,362, 1,041, 1,009 866, 860, 863 1.57, 1.21, 1.17

    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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions