Repository navigation
Conversation
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 Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
|
@devmotion Is this Ok like this? |
|
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
|
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 If you would rather not have Zygote among the test dependencies, I can take the test out again. |
|
Is the regression test Ok like this @devmotion ? |
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: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 differentiatesparamsof many distributions became about a hundred times slower.params(d::Poisson) = (d.λ,))(Julia 1.13, Zygote 0.7.16, after compilation.)
Downstream this showed up in MGVI.jl, which flattens
paramsof 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
paramssplices the parameter tuples by recursion over the arguments, so the result is a plain tuple construction that the compiler assembles statically:The results are unchanged, including the flattening of nested wrappers.
_appendhad no other users and is removed.Tests
The existing
namedparamstest set covers values and inferability ofparamsfor 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.