Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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"]
10 changes: 6 additions & 4 deletions src/common.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 14 additions & 0 deletions test/namedparams.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ using InteractiveUtils
using LinearAlgebra
using PDMats: PDMat, ScalMat
using Test
using Zygote: Zygote

using Distributions: namedparams

Expand Down Expand Up @@ -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 [
Expand Down
Loading