|
| 1 | +using DynamicPPL |
| 2 | +using Distributions |
| 3 | +using Random |
| 4 | +using Test |
| 5 | +using StableRNGs |
| 6 | +using Mooncake: NoCache, set_to_zero!!, set_to_zero_internal!!, zero_tangent |
| 7 | +using DynamicPPL.TestUtils.AD: @be |
| 8 | +using Statistics: median |
| 9 | + |
| 10 | +# Define models globally to avoid closure issues |
| 11 | +@model function test_model1(x) |
| 12 | + s ~ InverseGamma(2, 3) |
| 13 | + m ~ Normal(0, sqrt(s)) |
| 14 | + return x .~ Normal(m, sqrt(s)) |
| 15 | +end |
| 16 | + |
| 17 | +@model function test_model2(x, y) |
| 18 | + τ ~ Gamma(1, 1) |
| 19 | + σ ~ InverseGamma(2, 3) |
| 20 | + μ ~ Normal(0, τ) |
| 21 | + x .~ Normal(μ, σ) |
| 22 | + return y .~ Normal(μ, σ) |
| 23 | +end |
| 24 | + |
1 | 25 | @testset "DynamicPPLMooncakeExt" begin
|
2 |
| - Mooncake.TestUtils.test_rule( |
3 |
| - StableRNG(123456), istrans, VarInfo(); unsafe_perturb=true, interface_only=true |
4 |
| - ) |
| 26 | + @testset "istrans rule" begin |
| 27 | + Mooncake.TestUtils.test_rule( |
| 28 | + StableRNG(123456), istrans, VarInfo(); unsafe_perturb=true, interface_only=true |
| 29 | + ) |
| 30 | + end |
| 31 | + |
| 32 | + @testset "set_to_zero!! optimization" begin |
| 33 | + # Test with a real DynamicPPL model |
| 34 | + model = test_model1([1.0, 2.0, 3.0]) |
| 35 | + vi = VarInfo(Random.default_rng(), model) |
| 36 | + ldf = LogDensityFunction(model, vi, DefaultContext()) |
| 37 | + tangent = zero_tangent(ldf) |
| 38 | + |
| 39 | + # Test that set_to_zero!! works correctly |
| 40 | + result = set_to_zero!!(deepcopy(tangent)) |
| 41 | + @test result isa typeof(tangent) |
| 42 | + |
| 43 | + # Test with metadata - verify structure exists |
| 44 | + if hasfield(typeof(tangent.fields.varinfo.fields), :metadata) |
| 45 | + metadata = tangent.fields.varinfo.fields.metadata |
| 46 | + @test !isnothing(metadata) |
| 47 | + end |
| 48 | + end |
| 49 | + |
| 50 | + @testset "NoCache optimization correctness" begin |
| 51 | + # Test that set_to_zero!! uses NoCache for DynamicPPL types |
| 52 | + model = test_model1([1.0, 2.0, 3.0]) |
| 53 | + vi = VarInfo(Random.default_rng(), model) |
| 54 | + ldf = LogDensityFunction(model, vi, DefaultContext()) |
| 55 | + tangent = zero_tangent(ldf) |
| 56 | + |
| 57 | + # Modify some values |
| 58 | + if hasfield(typeof(tangent.fields.model.fields), :args) && |
| 59 | + hasfield(typeof(tangent.fields.model.fields.args), :x) |
| 60 | + x_tangent = tangent.fields.model.fields.args.x |
| 61 | + if !isempty(x_tangent) |
| 62 | + x_tangent[1] = 5.0 |
| 63 | + end |
| 64 | + end |
| 65 | + |
| 66 | + # Call set_to_zero!! and verify it works |
| 67 | + set_to_zero!!(tangent) |
| 68 | + |
| 69 | + # Check that values are zeroed |
| 70 | + if hasfield(typeof(tangent.fields.model.fields), :args) && |
| 71 | + hasfield(typeof(tangent.fields.model.fields.args), :x) |
| 72 | + x_tangent = tangent.fields.model.fields.args.x |
| 73 | + if !isempty(x_tangent) |
| 74 | + @test x_tangent[1] == 0.0 |
| 75 | + end |
| 76 | + end |
| 77 | + end |
| 78 | + |
| 79 | + @testset "Performance improvement" begin |
| 80 | + # Test with DEMO_MODELS if available |
| 81 | + if isdefined(DynamicPPL.TestUtils, :DEMO_MODELS) && |
| 82 | + !isempty(DynamicPPL.TestUtils.DEMO_MODELS) |
| 83 | + model = DynamicPPL.TestUtils.DEMO_MODELS[1] |
| 84 | + else |
| 85 | + # Fallback to our test model |
| 86 | + model = test_model1([1.0, 2.0, 3.0, 4.0]) |
| 87 | + end |
| 88 | + |
| 89 | + vi = VarInfo(Random.default_rng(), model) |
| 90 | + ldf = LogDensityFunction(model, vi, DefaultContext()) |
| 91 | + tangent = zero_tangent(ldf) |
| 92 | + |
| 93 | + # Run benchmarks |
| 94 | + result_iddict = @be begin |
| 95 | + cache = IdDict{Any,Bool}() |
| 96 | + set_to_zero_internal!!(cache, tangent) |
| 97 | + end |
| 98 | + |
| 99 | + result_nocache = @be set_to_zero!!(tangent) |
| 100 | + |
| 101 | + # Extract median times |
| 102 | + time_iddict = median(result_iddict).time |
| 103 | + time_nocache = median(result_nocache).time |
| 104 | + |
| 105 | + # We expect NoCache to be faster |
| 106 | + speedup = time_iddict / time_nocache |
| 107 | + @test speedup > 1.5 # Conservative expectation - should be ~4x |
| 108 | + |
| 109 | + @info "Performance improvement" speedup time_iddict_μs = time_iddict / 1000 time_nocache_μs = |
| 110 | + time_nocache / 1000 |
| 111 | + end |
| 112 | + |
| 113 | + @testset "Aliasing safety" begin |
| 114 | + # Test with aliased data |
| 115 | + shared_data = [1.0, 2.0, 3.0] |
| 116 | + model = test_model2(shared_data, shared_data) # x and y are the same array |
| 117 | + vi = VarInfo(Random.default_rng(), model) |
| 118 | + ldf = LogDensityFunction(model, vi, DefaultContext()) |
| 119 | + tangent = zero_tangent(ldf) |
| 120 | + |
| 121 | + # Check that aliasing is preserved in tangent |
| 122 | + if hasfield(typeof(tangent.fields.model.fields), :args) |
| 123 | + args = tangent.fields.model.fields.args |
| 124 | + if hasfield(typeof(args), :x) && hasfield(typeof(args), :y) |
| 125 | + @test args.x === args.y # Aliasing should be preserved |
| 126 | + |
| 127 | + # Modify via x |
| 128 | + if !isempty(args.x) |
| 129 | + args.x[1] = 10.0 |
| 130 | + @test args.y[1] == 10.0 # Should also change y |
| 131 | + end |
| 132 | + |
| 133 | + # Zero and check both are zeroed |
| 134 | + # Since x and y are aliased, zeroing one zeros both |
| 135 | + set_to_zero!!(tangent) |
| 136 | + if !isempty(args.x) |
| 137 | + @test args.x[1] == 0.0 |
| 138 | + @test args.y[1] == 0.0 |
| 139 | + end |
| 140 | + end |
| 141 | + end |
| 142 | + end |
| 143 | + |
| 144 | + @testset "Closure handling" begin |
| 145 | + # Test that closure models are correctly handled |
| 146 | + |
| 147 | + # Create closure model (captures environment, has circular references) |
| 148 | + function create_closure_model() |
| 149 | + local_var = 42 |
| 150 | + @model function closure_model(x) |
| 151 | + s ~ InverseGamma(2, 3) |
| 152 | + m ~ Normal(0, sqrt(s)) |
| 153 | + return x .~ Normal(m, sqrt(s)) |
| 154 | + end |
| 155 | + return closure_model |
| 156 | + end |
| 157 | + |
| 158 | + closure_fn = create_closure_model() |
| 159 | + model_closure = closure_fn([1.0, 2.0, 3.0]) |
| 160 | + vi_closure = VarInfo(Random.default_rng(), model_closure) |
| 161 | + ldf_closure = LogDensityFunction(model_closure, vi_closure, DefaultContext()) |
| 162 | + tangent_closure = zero_tangent(ldf_closure) |
| 163 | + |
| 164 | + # Test that it works without stack overflow |
| 165 | + @test_nowarn set_to_zero!!(deepcopy(tangent_closure)) |
| 166 | + |
| 167 | + # Compare with global model (no closure) |
| 168 | + model_global = test_model1([1.0, 2.0, 3.0]) |
| 169 | + vi_global = VarInfo(Random.default_rng(), model_global) |
| 170 | + ldf_global = LogDensityFunction(model_global, vi_global, DefaultContext()) |
| 171 | + tangent_global = zero_tangent(ldf_global) |
| 172 | + |
| 173 | + # Verify model.f tangent types differ |
| 174 | + f_tangent_closure = tangent_closure.fields.model.fields.f |
| 175 | + f_tangent_global = tangent_global.fields.model.fields.f |
| 176 | + |
| 177 | + @test f_tangent_global isa Mooncake.NoTangent # Global function |
| 178 | + @test f_tangent_closure isa Mooncake.Tangent # Closure function |
| 179 | + |
| 180 | + # Performance comparison |
| 181 | + time_global = @elapsed for _ in 1:100 |
| 182 | + set_to_zero!!(tangent_global) |
| 183 | + end |
| 184 | + |
| 185 | + time_closure = @elapsed for _ in 1:100 |
| 186 | + set_to_zero!!(tangent_closure) |
| 187 | + end |
| 188 | + |
| 189 | + # Global should be faster (uses NoCache) |
| 190 | + @test time_global < time_closure |
| 191 | + |
| 192 | + @info "Closure handling" time_global_ms = time_global * 1000 time_closure_ms = |
| 193 | + time_closure * 1000 |
| 194 | + end |
5 | 195 | end
|
0 commit comments