Skip to content

Occupancy kernel gradient is about 3 times BVDOutbreakSize's hand rule #914

Backlog

Description

@seabbs-bot

The Mooncake gradient of the BVDOutbreakSize occupancy kernel (Recurrence with Depletion and Split) is about 3 times the hand rule, so BVDOutbreakSize keeps its own rule.

What is known

Done when

  • bvd_occupancy_kernel's Mooncake gradient is within about 1.3 times the hand rule.
Earlier text

Earlier title: Occupancy (Recurrence + Depletion + Split) gradient is about 3–4× BVD's hand rule

BVDOutbreakSize keeps its own occupancy rule, because the CR spelling is still several times slower.
#860 (closed) has the earlier work and profiles.

Numbers

Kernel only, Mooncake gradient, CR main after #890:

Steps CR BVD hand rule Ratio
200 24.0 µs 6.4 µs 2.7–3.8×
400 3.2–4.1×

Per step that is about 105 ns against 33 ns.
CR's plain forward pass is about 25 ns per step.

Where the time goes

Reverse profile of the Recurrence + Depletion + Split path:

Already tried, no gain

  • Computing the Split parts once: slower (11 → 18 ns).
  • Caching forward partials: saves at most 2.8 ns.
  • Stage machinery (_split_pullback!, _group_back!, _group_pullback): at most about 5% in total.
  • A fused group step needs a type-level group count, which was ruled out earlier.

What would close it

A design change rather than a local tweak.
For example, a fused pullback for a Depletion with count flows that keeps per-step partials in registers, as the hand rule does.

Done when

The BVD occupancy kernel's Mooncake gradient is within about 1.3× of the hand rule.
Then BVD can drop the rule.


Generated by Claude Code

Activity

  1. seabbs-bot commented on Dec 1, 2026

    @seabbs-bot
    CollaboratorAuthor

    Outcome: reproduced on the redesigned branch; not fixed; left open

    This one does not wait on anyone else.
    It is open because the fix is a design change that was not made here.

    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.

    Case Size Package (µs) Hand rule (µs) Ratio
    bvd_occupancy_kernel (the kernels alone) T200 42.5 11.0 3.9
    bvd_occupancy_kernel T400 77.7 26.1 3.0
    bvd_occupancy (the model's loss around them) T200 43.6 44.3 0.98
    bvd_occupancy T400 82.1 82.0 1.00
    • The kernel gap is unchanged by the redesign: the pure step did not touch a Depletion's count flows.
    • Inside a loss that Mooncake differentiates itself, the two arms are level, because the loss's own broadcasts cost about as much as the kernel. BVD's models give the loss its own rules, so the kernel row is the one that decides whether BVD can drop its rule.

    What remains

    The reverse step of a Depletion with count flows: the profile in the issue body (count-flow pullbacks 49%, group bookkeeping 10%, stage dispatch 8%) still describes it.
    The local changes listed under "Already tried" stay ruled out.
    The candidate is one reverse step per group that carries the flows' partials from the forward step, in place of one pullback per flow.
    It has to be written for any number of flows from the operator's types, with no group count in the code, and it needs timings steadier than this machine gives to judge a change of tens of nanoseconds a step.

    Done when

    As in the issue: bvd_occupancy_kernel within about 1.3× of the hand rule.


    Generated by Claude Code

  2. seabbs-bot commented on Dec 2, 2026

    @seabbs-bot
    CollaboratorAuthor

    Outcome: improved, the 1.3× bar is not met; left open

    #946 has the change, the evidence and the full tables.

    Where the time went

    • In a bare loop the flows' group step is level with BVD's hand loop: 14 to 15 ns a step each.
      The package's call took 30 ns a step.
    • The difference was the vector step around a single group: a row copy out of the buffer and back, a column copy into each modifier's record, and a gather, a scatter and a set of accumulators per modifier, at every step, forward and in reverse.
    • In the reverse pass, the Split flow's pullback divided the cotangents by the pools' total twice, on the chain each step waits on.

    What #946 changes

    • One group of a blockwise modifier runs in the series step, as a tuple through the modifiers, forward and in reverse. Any blockwise modifier takes it, not only this model.
    • The Split pullback multiplies by one reciprocal of the total. Interleaved over six rounds it took 1.3 to 6.4 ns a step off the group pullback in every round.

    Numbers

    bvd_occupancy_kernel, T = 200, benchmark/ab.jl, three interleaved rounds, main at 4ce03f8 against the branch.
    The machine was noisy: the A/A band was 12 to 31%.

    main #946 hand code #946 / hand
    Forward 11.9 µs, 30,432 B 8.5 µs, 30,256 B 4.3 µs, 18,368 B 1.9 (was 2.6)
    Mooncake reverse 41.1 µs 34.8 µs 11.7 µs (hand rule) 3.1 (was 3.6 to 4.0)
    Enzyme reverse 44.8 µs 31.0 µs 11.1 µs (Enzyme on the hand loop) 2.8 (was 3.7)

    The forward and Enzyme gains are outside the noise band.
    The Mooncake gradient was faster in each round (0.72, 0.84, 0.89) but inside a ±31% band, so the harness calls it within noise.

    What remains, and why

    The flows' arithmetic is not the gap: the bare group step with its records and its pullback come to about 8 to 9 µs, about 1.3 times the hand kernel and rule.
    A profile of the gradient on the branch:

    Part Share
    The loss's input rule, in both arms 11%
    The run's steps under the rule 13%
    Buffer, two records, the output copy and the zero output 15%
    The reverse steps 28%
    The buffer's cotangent 4%
    BVD's occupancy_outputs and its rule 15%
    Mooncake's derived code around the call 9%

    The structure the hand rule uses and the generic path does not:

    • One byte of flags a day, against a time × strata buffer, a record per modifier and a strata × time output copy, each about 5 kB.
    • The five outputs written inside the loop, against a second pass over the pools with its own rule.
    • One fused expression a day, against seven flows stepped in order.

    A strata × time buffer would let the output and its cotangent share the buffer's memory and remove two of those arrays and both transposed copies.
    That is the core change #512 asks for too; it is not made in #946.
    Writing the outputs inside the loop would need a model-specific loop in the package.

    Tried and dropped: removing additions of structural zeros from the pullback gave no consistent gain over five interleaved rounds.

    For the maintainer

    The 1.3× bar needs either the buffer layout change or a model-specific rule.
    With neither, about 3× is where the generic path sits, and BVD keeps its rule.
    Whether to close this as the cost of generality or keep it for the buffer change is your call.


    Generated by Claude Code

  3. changed the title [-]Occupancy (Recurrence + Depletion + Split) gradient is about 3–4× BVD's hand rule[/-] [+]Occupancy kernel gradient is about 3 times BVDOutbreakSize's hand rule[/+] on Dec 2, 2026
  4. seabbs-bot commented on Dec 2, 2026

    @seabbs-bot
    CollaboratorAuthor

    The occupancy and census work on branch bvd-parity (#1008) addresses this.


    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