Skip to content

Assemble generic params by tuple recursion instead of a fold - #2113

Open
oschulz wants to merge 2 commits into
JuliaStats:masterfrom
oschulz:params-compile-time-splice
Open

oschulz wants to merge 2 commits into
JuliaStats:masterfrom
oschulz:params-compile-time-splice

Conversation

@oschulz

@oschulz oschulz commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

Written by Claude (Fable 5.2, effort xhigh), working under @oschulz's guidance.

Why

Since 0.25.133 (#2092), the generic params(d::Distribution) is a runtime fold over the named parameters:

reduce(_append, map(_paramvalues, values(namedparams(d))); init=())

The primal is as cheap as the previous per-type methods. Source-transforming reverse-mode AD (Zygote) however now differentiates mapfoldl, keyword handling and tuple iteration instead of a field access, and records about 200 heap objects per call. Code that differentiates params of many distributions became about a hundred times slower.

using Distributions, Zygote
λ = rand(10_000) .+ 0.5
f(λ) = sum(first.(params.(Poisson.(λ))))
Zygote.gradient(f, λ)
@time Zygote.gradient(f, λ)
Distributions time allocations
0.25.131 (params(d::Poisson) = (d.λ,)) 0.012 s 12 MiB
0.25.134 1.35 s 1.7 GiB
this PR 0.014 s 15 MiB

(Julia 1.13, Zygote 0.7.16, after compilation.)

Downstream this showed up in MGVI.jl, which flattens params of about 10^4 Poisson distributions inside its forward models: one optimization step went from 2 s to 260 s (JuliaBayes/MGVI.jl#80 works around it on the MGVI side).

What

params splices the parameter tuples by recursion over the arguments, so the result is a plain tuple construction that the compiler assembles statically:

StatsAPI.params(d::Distribution) = _flatparams(values(namedparams(d))...)
_flatparams() = ()
_flatparams(x, xs...) = (_paramvalues(x)..., _flatparams(xs...)...)

The results are unchanged, including the flattening of nested wrappers. _append had no other users and is removed.

Tests

The existing namedparams test set covers values and inferability of params for all distributions and passes unchanged. I did not add an AD regression test since Zygote is not a test dependency here. I can add one if that is wanted.

The generic `params` reduced over the values of `namedparams` at
runtime. Reverse-mode AD by source transformation (Zygote)
differentiates that fold step by step, which made a gradient through
`params` of many distributions about a hundred times slower than with
the per-type methods before 0.25.133. Splicing the parameter tuples by
recursion over the arguments gives the same result as a plain tuple
construction.

Assisted-By: generative AI
@codecov

codecov Bot commented Nov 9, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 87.35%. Comparing base (dd5ad8e) to head (855461a).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2113      +/-   ##
==========================================
- Coverage   87.37%   87.35%   -0.02%     
==========================================
  Files         149      150       +1     
  Lines        8948     8937      -11     
==========================================
- Hits         7818     7807      -11     
  Misses       1130     1130              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@oschulz
oschulz marked this pull request as ready for review November 20, 2026 16:02
@oschulz

oschulz commented Nov 20, 2026

Copy link
Copy Markdown
Contributor Author

@devmotion Is this Ok like this?

@devmotion

Copy link
Copy Markdown
Member

Interesting, I thought some draft version was already quite similar to this PR. Not a problem at all, but it would be good with a regression test. Can you add one?

Natively, a fold over the parameters is as cheap as the recursion, so
the regression only shows under source-to-source reverse-mode AD. The
test compares the allocations of a Zygote gradient through `params`
with those of a gradient through `namedparams`. This makes Zygote a
test dependency.

Assisted-By: generative AI
@oschulz

oschulz commented Nov 21, 2026

Copy link
Copy Markdown
Contributor Author

Posted by Claude (Fable 5.2, effort xhigh), working under @oschulz's guidance.

Added in 855461a. Natively the fold and the recursion behave the same, both are inferred and allocate nothing, so the regression only shows under source-to-source reverse-mode AD. The test therefore uses Zygote, which becomes a test dependency.

It compares the allocations of a Zygote gradient through params with those of a gradient through namedparams, for Poisson and Normal, and allows a factor of 10. On Julia 1.10 and 1.13 the ratio is between 1.1 and 1.7 with the recursion and above 40 with the fold. I checked that the test fails with the reduce version and passes with this PR on both Julia versions.

If you would rather not have Zygote among the test dependencies, I can take the test out again.

@oschulz

oschulz commented Nov 21, 2026

Copy link
Copy Markdown
Contributor Author

Is the regression test Ok like this @devmotion ?

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants