From 12adbb3da2f55f7a42c424abb51844e19f1247ed Mon Sep 17 00:00:00 2001 From: Oliver Schulz Date: Mon, 9 Nov 2026 23:07:54 +0100 Subject: [PATCH 1/2] Assemble generic `params` by tuple recursion instead of a fold 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 --- src/common.jl | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/common.jl b/src/common.jl index 4d0a8951e..fa63be577 100644 --- a/src/common.jl +++ b/src/common.jl @@ -494,11 +494,13 @@ function namedparams end # `params` flattens wrapped distributions, e.g. `params(truncated(Normal(), 0, 1))` is # `(0.0, 1.0, 0.0, 1.0)`. Distributions whose `params` keeps the wrapped distribution, such as # `AffineDistribution`, specialize it. -function StatsAPI.params(d::Distribution) - return reduce(_append, map(_paramvalues, values(namedparams(d))); init=()) -end +StatsAPI.params(d::Distribution) = _flatparams(values(namedparams(d))...) -_append(x::Tuple, y::Tuple) = (x..., y...) +# Splices the parameter tuples by recursion over the arguments instead of by a fold over +# them, so that the result is assembled at compile time and reverse-mode AD sees a plain +# tuple construction. +_flatparams() = () +_flatparams(x, xs...) = (_paramvalues(x)..., _flatparams(xs...)...) _paramvalues(x) = (x,) _paramvalues(d::Distribution) = params(d) From 855461a6c6ea636fce7d73e3e30b29bf4afd9379 Mon Sep 17 00:00:00 2001 From: Oliver Schulz Date: Sat, 21 Nov 2026 09:02:59 +0100 Subject: [PATCH 2/2] Add a regression test for reverse-mode AD of `params` 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 --- Project.toml | 4 +++- test/namedparams.jl | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 63b86dbe5..e74915be6 100644 --- a/Project.toml +++ b/Project.toml @@ -61,6 +61,7 @@ StatsAPI = "1.6" StatsBase = "0.32, 0.33, 0.34" StatsFuns = "1.5.2, 2.0.1" Test = "<0.0.1, 1" +Zygote = "0.7" julia = "1.10" [extras] @@ -81,9 +82,10 @@ SparseConnectivityTracer = "9f842d2f-2579-4b1d-911e-f412cf18a3f5" StableRNGs = "860ef19b-820b-49d6-a774-d7a799459cd3" StaticArrays = "90137ffa-7385-5640-81b9-e52037218182" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" +Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f" [targets] -test = ["Aqua", "StableRNGs", "Calculus", "ChainRulesCore", "ChainRulesTestUtils", "DensityInterface", "Distributed", "FiniteDifferences", "ForwardDiff", "InteractiveUtils", "JSON", "Optim", "SparseArrays", "SparseConnectivityTracer", "StaticArrays", "Test", "OffsetArrays"] +test = ["Aqua", "StableRNGs", "Calculus", "ChainRulesCore", "ChainRulesTestUtils", "DensityInterface", "Distributed", "FiniteDifferences", "ForwardDiff", "InteractiveUtils", "JSON", "Optim", "SparseArrays", "SparseConnectivityTracer", "StaticArrays", "Test", "OffsetArrays", "Zygote"] [workspace] projects = ["docs"] diff --git a/test/namedparams.jl b/test/namedparams.jl index 4aa161f1e..845a6a123 100644 --- a/test/namedparams.jl +++ b/test/namedparams.jl @@ -3,6 +3,7 @@ using InteractiveUtils using LinearAlgebra using PDMats: PDMat, ScalMat using Test +using Zygote: Zygote using Distributions: namedparams @@ -180,6 +181,19 @@ using Distributions: namedparams @test params(d) === (0.0, 1.0, 0.0, nothing, nothing, 2.0) end + @testset "reverse-mode AD" begin + # Natively, a fold over the parameters is as cheap as the recursion in `params`, + # but it made Zygote allocate about a hundred times more than for `namedparams` + allocations(f, x) = (Zygote.gradient(f, x); @allocated Zygote.gradient(f, x)) + constructors = ("Poisson" => Poisson, "Normal" => μ -> Normal(μ, 2.0)) + @testset "$name" for (name, dist) in constructors + params_sum(x) = sum(params(dist(x))) + namedparams_sum(x) = sum(values(namedparams(dist(x)))) + @test Zygote.gradient(params_sum, 1.5) == (1.0,) + @test allocations(params_sum, 1.5) <= 10 * allocations(namedparams_sum, 1.5) + end + end + @testset "reconstruction" begin # `namedparams` reports the arguments that the constructor of `d` expects @testset "$f" for (f, d) in [