@@ -38,23 +38,26 @@ function test_with_scenario(scenario)
3838 cor_ends = (P= (n_θP == 0 ? [0 ] : 1 : n_θP), M= [n_θM])
3939 ρsP = zeros (FT, get_cor_count (cor_ends. P)) .+ FT (0.02 )
4040 ρsM = zeros (FT, get_cor_count (cor_ends. M)) .+ FT (0.02 )
41+ (; transP, transM) = get_hybridproblem_transforms (prob; scenario)
4142
4243 ϕq = CA. ComponentVector (;
4344 logσ2_ζP= fill (FT (- 10.0 ), n_θP),
4445 coef_logσ2_ζMs= reduce (hcat, (FT[- 10.0 , 0.0 ] for _ in 1 : n_θM)),
4546 ρsP,
46- ρsM)
47+ ρsM,
48+ )
49+
50+ ϕq_true = CP. update_μP_by_θP (ϕq, θP_true, transP)
51+ ϕq = CA. ComponentVector (ϕq_true; μP = ϕq_true. μP .+ FT (0.01 ))
4752
4853 θ_true = θ = CA. ComponentVector (;
4954 P= θP_true,
5055 Ms= θMs_true)
51- transPMs = elementwise (exp) # all parameters on LogNormal scale
52- ζ_true = inverse (transPMs)(θ_true)
53- ϕ_true = vcat (ζ_true, CA. ComponentVector (ϕq= ϕq))
54- ϕ_cpu = vcat (ζ_true .+ FT (0.01 ), CA. ComponentVector (ϕq= ϕq))
56+ ϕ_true = CA. ComponentVector (Ms = θMs_true, ϕq= ϕq_true)
57+ ϕ_cpu = CA. ComponentVector (Ms = θMs_true .+ FT (0.01 ), ϕq= ϕq)
5558
5659 interpreters = (; pmu= ComponentArrayInterpreter (ϕ_true),
57- ϕq= ComponentArrayInterpreter (ϕ_true . ϕq)
60+ ϕq= ComponentArrayInterpreter (ϕq)
5861 ) # , M=int_θM, PMs=int_θPMs)
5962
6063 n_MC = 3
@@ -88,38 +91,37 @@ function test_with_scenario(scenario)
8891 # approx = MeanHVIApproximationMat()
8992 # approx = CP.MeanHVIApproximationDev()
9093 function test_sample_ζresid_norm (approx)
91- ϕ = CA. getdata (ϕ_cpu)
92- ϕc = interpreters. pmu (ϕ)
94+ ϕc = copy (ϕ_cpu)
9395 ϕc. ϕq. coef_logσ2_ζMs[1 ,:] .= (log ∘ abs2). ((0.1 , 100.0 ))
9496 ϕc. ϕq. ρsM .= 0.0
9597 int_ϕq = get_concrete (ComponentArrayInterpreter (ϕc. ϕq))
9698 n_MC_pred = 300 # larger n_MC to test σ2
9799 n_site_batch = size (ϕc. Ms,2 )
98100 # rng = StableRNG(111)
99101 # @inferred gives any, while Cthulhu inferres concrete type
100- # ζP_resids, ζMs_parfirst_resids, σ = @inferred CP.sample_ζresid_norm(approx, rng, ϕc.P, ϕc. Ms, ϕc.ϕq;
102+ # ζP_resids, ζMs_parfirst_resids, σ = @inferred CP.sample_ζresid_norm(approx, rng, ϕc.Ms, ϕc.ϕq;
101103 # n_MC=n_MC_pred, cor_ends, int_ϕq)
102- # @inferred first(CP.sample_ζresid_norm(approx, rng, ϕc.P, ϕc. Ms, ϕc.ϕq;
104+ # @inferred first(CP.sample_ζresid_norm(approx, rng, ϕc.Ms, ϕc.ϕq;
103105 # n_MC=n_MC_pred, cor_ends, int_ϕq))
104106 # ζP_resids, ζMs_parfirst_resids, σ = CP.sample_ζresid_norm(approx, rng, ϕc.P, ϕc.Ms, ϕc.ϕq;
105107 # n_MC=n_MC_pred, cor_ends, int_ϕq)
106- ζP_resids, ζMs_parfirst_resids, σ = @inferred CP. sample_ζresid_norm (approx, rng, ϕc. P, ϕc . Ms, ϕc. ϕq;
108+ ζP_resids, ζMs_parfirst_resids, σ = @inferred CP. sample_ζresid_norm (approx, rng, ϕc. Ms, ϕc. ϕq;
107109 n_MC= n_MC_pred, cor_ends, int_ϕq)
108- # @code_warntype CP.sample_ζresid_norm(approx, rng, ϕc.P, ϕc. Ms, ϕc.ϕq; n_MC=n_MC_pred, cor_ends, int_ϕq)
110+ # @code_warntype CP.sample_ζresid_norm(approx, rng, ϕc.Ms, ϕc.ϕq; n_MC=n_MC_pred, cor_ends, int_ϕq)
109111 # @usingany Cthulhu
110- # @descend_code_warntype CP.sample_ζresid_norm(approx, rng, ϕc.P, ϕc. Ms, ϕc.ϕq; n_MC=n_MC_pred, cor_ends, int_ϕq)
112+ # @descend_code_warntype CP.sample_ζresid_norm(approx, rng, ϕc.Ms, ϕc.ϕq; n_MC=n_MC_pred, cor_ends, int_ϕq)
111113 # @test size(ζ_resid) == (length(ϕc.P) + n_site * n_θM, n_MC)
112114 n_θM = size (ϕc. Ms,1 )
113115 @test size (ζP_resids) == (n_θP, n_MC_pred)
114116 @test size (ζMs_parfirst_resids) == (n_θM, n_site_batch, n_MC_pred)
115117 gr =
116118 Zygote. gradient (ϕc -> begin
117119 ζP_resids, ζMs_parfirst_resids, σ = CP. sample_ζresid_norm (
118- approx, rng, ϕc. P, ϕc . Ms, ϕc. ϕq;
120+ approx, rng, ϕc. Ms, ϕc. ϕq;
119121 n_MC, cor_ends, int_ϕq)
120122 sum (ζP_resids) + sum (ζMs_parfirst_resids)
121123 end , ϕc)[1 ]
122- @test length (gr) == length (ϕ )
124+ @test length (gr) == length (ϕc )
123125 #
124126 n_θM, n_site_batch = size (ϕc. Ms)
125127 # intm_PMs = ComponentArrayInterpreter(
@@ -139,32 +141,35 @@ function test_with_scenario(scenario)
139141 # ζP, ζMs, ϕq = ϕc.P, ϕc.Ms, ϕc.ϕq
140142 # urandn = CUDA.randn(length(ϕc.P) + length(ϕc.Ms), n_MC) |> gpu
141143 # include(joinpath(@__DIR__, "uncNN", "elbo.jl")) # callback_loss
142- # ζ_resid, σ = sample_ζresid_norm(urandn, ϕc.P, ϕc. Ms, ϕc.ϕq; n_MC)
143- # Zygote.gradient(ϕc -> sum(sample_ζresid_norm(urandn, ϕc.P, ϕc. Ms, ϕc.ϕq; n_MC)[1]), ϕc)[1];
144+ # ζ_resid, σ = sample_ζresid_norm(urandn, ϕc.Ms, ϕc.ϕq; n_MC)
145+ # Zygote.gradient(ϕc -> sum(sample_ζresid_norm(urandn, ϕc.Ms, ϕc.ϕq; n_MC)[1]), ϕc)[1];
144146 # @inferred first(CP.sample_ζresid_norm(
145- # approx, rng, CA.getdata(ϕcd.P), CA.getdata(ϕcd. Ms), CA.getdata(ϕcd.ϕq);
147+ # approx, rng, CA.getdata(ϕcd.Ms), CA.getdata(ϕcd.ϕq);
146148 # n_MC = n_MC_pred, cor_ends, int_ϕq))
147149 # ζP_resids, ζMs_parfirst_resids, σ = CP.sample_ζresid_norm(
148- # approx, rng, CA.getdata(ϕcd.P), CA.getdata(ϕcd. Ms), CA.getdata(ϕcd.ϕq);
150+ # approx, rng, CA.getdata(ϕcd.Ms), CA.getdata(ϕcd.ϕq);
149151 # n_MC = n_MC_pred, cor_ends, int_ϕq)
150152 ζP_resids, ζMs_parfirst_resids, σ = @inferred CP. sample_ζresid_norm (
151- approx, rng, CA. getdata (ϕcd. P), CA . getdata (ϕcd . Ms), CA. getdata (ϕcd. ϕq);
153+ approx, rng, CA. getdata (ϕcd. Ms), CA. getdata (ϕcd. ϕq);
152154 n_MC = n_MC_pred, cor_ends, int_ϕq)
153- # @descend_code_warntype CP.sample_ζresid_norm(rng, CA.getdata(ϕcd.P), CA.getdata(ϕcd. Ms), CA.getdata(ϕcd.ϕq); n_MC = n_MC_pred, cor_ends, int_ϕq)
155+ # @descend_code_warntype CP.sample_ζresid_norm(rng, CA.getdata(ϕcd.Ms), CA.getdata(ϕcd.ϕq); n_MC = n_MC_pred, cor_ends, int_ϕq)
154156 @test ζP_resids isa GPUArraysCore. AbstractGPUArray
155157 @test ζMs_parfirst_resids isa GPUArraysCore. AbstractGPUArray
156158 @test size (ζP_resids) == (n_θP, n_MC_pred)
157159 @test size (ζMs_parfirst_resids) == (n_θM, n_site_batch, n_MC_pred)
158160 # Zygote gradient for many sites, use fewer sites here
159161 n_site_few = 20
160- ϕcd_few = CA. ComponentVector (; P = ϕcd. P, Ms = ϕcd. Ms[:,1 : n_site_few], ϕq = ϕcd. ϕq);
162+ # replacing Ms in ComponentVector by different length does not work on GPU
163+ # using workaround by NamedTuples, results in ScalarIndexing, need explicit
164+ # ϕcd_few = CA.ComponentVector(ϕcd; Ms = ϕcd.Ms[:,1:n_site_few]);
165+ ϕcd_few = CA. ComponentVector (Ms = ϕcd. Ms[:,1 : n_site_few], ϕq = ϕcd. ϕq);
161166 # @usingany BenchmarkTools
162167 gr =
163168 # @profview Zygote.gradient(ϕc -> begin # type stable, most time spent in mapreduce
164169 # @benchmark Zygote.gradient(ϕc -> begin # many small allocs
165170 Zygote. gradient (ϕc -> begin
166171 ζP_resids, ζMs_parfirst_resids, σ = CP. sample_ζresid_norm (
167- approx, rng, CA. getdata (ϕc. P), CA . getdata (ϕc . Ms), CA. getdata (ϕc. ϕq);
172+ approx, rng, CA. getdata (ϕc. Ms), CA. getdata (ϕc. ϕq);
168173 n_MC, cor_ends, int_ϕq)
169174 sum (ζP_resids) + sum (ζMs_parfirst_resids)
170175 end , ϕcd_few)[1 ]; # semicolon required
@@ -175,26 +180,26 @@ function test_with_scenario(scenario)
175180 () -> begin
176181 CP. sample_ζresid_norm (
177182 # @benchmark CP.sample_ζresid_norm(
178- approx, rng, ϕc. P, ϕc . Ms, ϕc. ϕq;
183+ approx, rng, ϕc. Ms, ϕc. ϕq;
179184 n_MC, cor_ends, int_ϕq)
180185 #
181186 CP. sample_ζresid_norm (
182187 # @benchmark CP.sample_ζresid_norm(
183- approx, rng, ϕcd. P, ϕcd . Ms, ϕcd. ϕq;
188+ approx, rng, ϕcd. Ms, ϕcd. ϕq;
184189 n_MC, cor_ends, int_ϕq)
185190 #
186- ϕc_few = CA. ComponentVector (; P = ϕc . P, Ms = ϕc. Ms[:,1 : n_site_few], ϕq = ϕc . ϕq );
191+ ϕc_few = CA. ComponentVector (ϕc; Ms = ϕc. Ms[:,1 : n_site_few]);
187192 Zygote. gradient (ϕc -> begin
188193 # @benchmark Zygote.gradient(ϕc -> begin # many small allocs
189194 ζP_resids, ζMs_parfirst_resids, σ = CP. sample_ζresid_norm (
190- approx, rng, ϕc. P, ϕc . Ms, ϕc. ϕq;
195+ approx, rng, ϕc. Ms, ϕc. ϕq;
191196 n_MC, cor_ends, int_ϕq)
192197 sum (ζP_resids) + sum (ζMs_parfirst_resids)
193198 end , ϕc_few)[1 ]
194199 Zygote. gradient (ϕc -> begin # many small allocs
195200 # @benchmark Zygote.gradient(ϕc -> begin # many small allocs
196201 ζP_resids, ζMs_parfirst_resids, σ = CP. sample_ζresid_norm (
197- approx, rng, CA. getdata (ϕc. P), CA . getdata (ϕc . Ms), CA. getdata (ϕc. ϕq);
202+ approx, rng, CA. getdata (ϕc. Ms), CA. getdata (ϕc. ϕq);
198203 n_MC, cor_ends, int_ϕq)
199204 sum (ζP_resids) + sum (ζMs_parfirst_resids)
200205 end , ϕcd_few)[1 ]
0 commit comments