Skip to content

Commit 18ba8db

Browse files
committed
Remove DEBUG (#148)
This PR removes the variables `Turing.DEBUG` and `DynamicPPL.DEBUG`. Instead one can just set the environment variable `JULIA_DEBUG` to "Turing" or "DynamicPPL", if desired (see https://docs.julialang.org/en/v1/stdlib/Logging/#Environment-variables-1). By default, debug logging is not enabled and the messages are filtered immediately without being evaluated (see https://docs.julialang.org/en/v1/stdlib/Logging/#Early-filtering-and-message-handling-1), and hence the additional check `Turing.DEBUG` or `DynamicPPL.DEBUG` is not needed even from a performance perspective.
1 parent f7eed27 commit 18ba8db

File tree

10 files changed

+29
-40
lines changed

10 files changed

+29
-40
lines changed

src/DynamicPPL.jl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,6 @@ export AbstractVarInfo,
9797
using Distributions: loglikelihood
9898
export loglikelihood
9999

100-
const DEBUG = Bool(parse(Int, get(ENV, "DEBUG_DYNAMICPPL", "0")))
101-
102100
# Used here and overloaded in Turing
103101
function getspace end
104102

src/context_implementations.jl

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -444,8 +444,7 @@ function dot_observe(
444444
vi,
445445
)
446446
increment_num_produce!(vi)
447-
DynamicPPL.DEBUG && @debug "dist = $dist"
448-
DynamicPPL.DEBUG && @debug "value = $value"
447+
@debug "dot_observe" dist value
449448
return sum(Distributions.logpdf(dist, value))
450449
end
451450
function dot_observe(
@@ -455,8 +454,7 @@ function dot_observe(
455454
vi,
456455
)
457456
increment_num_produce!(vi)
458-
DynamicPPL.DEBUG && @debug "dists = $dists"
459-
DynamicPPL.DEBUG && @debug "value = $value"
457+
@debug "dot_observe" dists value
460458
return sum(Distributions.logpdf.(dists, value))
461459
end
462460
function dot_observe(

test/Turing/Turing.jl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ function turnprogress(switch::Bool)
2323
PROGRESS[] = switch
2424
end
2525

26-
const DEBUG = Bool(parse(Int, get(ENV, "DEBUG_TURING", "0")))
27-
2826
# Random probability measures.
2927
include("stdlib/distributions.jl")
3028
include("stdlib/RandomMeasures.jl")

test/Turing/contrib/inference/AdvancedSMCExtensions.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@ function step(model, spl::Sampler{<:PMMH}, vi::VarInfo, is_first::Bool)
7979
new_likelihood_estimate = 0.0
8080
old_θ = copy(vi[spl])
8181

82-
Turing.DEBUG && @debug "Propose new parameters from proposals..."
82+
@debug "Propose new parameters from proposals..."
8383
for local_spl in spl.info[:samplers][1:end-1]
84-
Turing.DEBUG && @debug "$(typeof(local_spl)) proposing $(local_spl.alg.space)..."
84+
@debug "$(typeof(local_spl)) proposing $(local_spl.alg.space)..."
8585
propose(model, local_spl, vi)
8686
if local_spl.info[:violating_support] violating_support=true; break end
8787
new_prior_prob += local_spl.info[:prior_prob]
@@ -92,11 +92,11 @@ function step(model, spl::Sampler{<:PMMH}, vi::VarInfo, is_first::Bool)
9292
# do not run SMC if going to refuse anyway
9393
accepted = false
9494
else
95-
Turing.DEBUG && @debug "Propose new state with SMC..."
95+
@debug "Propose new state with SMC..."
9696
vi, _ = step(model, spl.info[:samplers][end], vi)
9797
new_likelihood_estimate = spl.info[:samplers][end].info[:logevidence][end]
9898

99-
Turing.DEBUG && @debug "Decide whether to accept..."
99+
@debug "Decide whether to accept..."
100100
accepted = mh_accept(
101101
spl.info[:old_likelihood_estimate] + spl.info[:old_prior_prob],
102102
new_likelihood_estimate + new_prior_prob,
@@ -150,7 +150,7 @@ function sample( model::Model,
150150
accept_his = Bool[]
151151
PROGRESS[] && (spl.info[:progress] = ProgressMeter.Progress(n, 1, "[$alg_str] Sampling...", 0))
152152
for i = 1:n
153-
Turing.DEBUG && @debug "$alg_str stepping..."
153+
@debug "$alg_str stepping..."
154154
time_elapsed = @elapsed vi, is_accept = step(model, spl, vi, i==1)
155155

156156
if is_accept # accepted => store the new predcits
@@ -305,7 +305,7 @@ function sample(model::Model, alg::IPMCMC)
305305
# IPMCMC steps
306306
if PROGRESS[] spl.info[:progress] = ProgressMeter.Progress(n, 1, "[IPMCMC] Sampling...", 0) end
307307
for i = 1:n
308-
Turing.DEBUG && @debug "IPMCMC stepping..."
308+
@debug "IPMCMC stepping..."
309309
time_elapsed = @elapsed VarInfos = step(model, spl, VarInfos, i==1)
310310

311311
# Save each CSMS retained path as a sample

test/Turing/contrib/inference/sghmc.jl

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -82,29 +82,29 @@ function step(
8282
η, α = spl.alg.learning_rate, spl.alg.momentum_decay
8383
spl.info[:eval_num] = 0
8484

85-
Turing.DEBUG && @debug "X-> R..."
85+
@debug "X-> R..."
8686
if spl.selector.tag != :default
8787
link!(vi, spl)
8888
model(vi, spl)
8989
end
9090

91-
Turing.DEBUG && @debug "recording old variables..."
91+
@debug "recording old variables..."
9292
θ, v = vi[spl], spl.info[:v]
9393
_, grad = gradient_logp(θ, vi, model, spl)
9494
verifygrad(grad)
9595

9696
# Implements the update equations from (15) of Chen et al. (2014).
97-
Turing.DEBUG && @debug "update latent variables and velocity..."
97+
@debug "update latent variables and velocity..."
9898
θ .+= v
9999
v .= (1 - α) .* v .+ η .* grad .+ rand.(Normal.(zeros(length(θ)), sqrt(2 * η * α)))
100100

101-
Turing.DEBUG && @debug "saving new latent variables..."
101+
@debug "saving new latent variables..."
102102
vi[spl] = θ
103103

104-
Turing.DEBUG && @debug "R -> X..."
104+
@debug "R -> X..."
105105
spl.selector.tag != :default && invlink!(vi, spl)
106106

107-
Turing.DEBUG && @debug "always accept..."
107+
@debug "always accept..."
108108
return vi, true
109109
end
110110

@@ -189,30 +189,30 @@ function step(
189189
spl.info[:i] += 1
190190
spl.info[:eval_num] = 0
191191

192-
Turing.DEBUG && @debug "compute current step size..."
192+
@debug "compute current step size..."
193193
γ = .35
194194
ϵ_t = spl.alg.ϵ / spl.info[:i]^γ # NOTE: Choose γ=.55 in paper
195195
mssa = spl.info[:adaptor].ssa
196196
mssa.state.ϵ = ϵ_t
197197

198-
Turing.DEBUG && @debug "X-> R..."
198+
@debug "X-> R..."
199199
if spl.selector.tag != :default
200200
link!(vi, spl)
201201
model(vi, spl)
202202
end
203203

204-
Turing.DEBUG && @debug "recording old variables..."
204+
@debug "recording old variables..."
205205
θ = vi[spl]
206206
_, grad = gradient_logp(θ, vi, model, spl)
207207
verifygrad(grad)
208208

209-
Turing.DEBUG && @debug "update latent variables..."
209+
@debug "update latent variables..."
210210
θ .+= ϵ_t .* grad ./ 2 .- rand.(Normal.(zeros(length(θ)), sqrt(ϵ_t)))
211211

212-
Turing.DEBUG && @debug "always accept..."
212+
@debug "always accept..."
213213
vi[spl] = θ
214214

215-
Turing.DEBUG && @debug "R -> X..."
215+
@debug "R -> X..."
216216
spl.selector.tag != :default && invlink!(vi, spl)
217217

218218
return vi, true

test/Turing/inference/Inference.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ using StatsFuns: logsumexp
1818
using Random: GLOBAL_RNG, AbstractRNG, randexp
1919
using DynamicPPL
2020
using AbstractMCMC: AbstractModel, AbstractSampler
21-
using Bijectors: _debug
2221
using DocStringExtensions: TYPEDEF, TYPEDFIELDS
2322

2423
import AbstractMCMC

test/Turing/inference/gibbs.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,11 +173,11 @@ function AbstractMCMC.step!(
173173
transition::Union{Nothing,GibbsTransition};
174174
kwargs...
175175
)
176-
Turing.DEBUG && @debug "Gibbs stepping..."
176+
@debug "Gibbs stepping..."
177177

178178
# Iterate through each of the samplers.
179179
transitions = map(enumerate(spl.state.samplers)) do (i, local_spl)
180-
Turing.DEBUG && @debug "$(typeof(local_spl)) stepping..."
180+
@debug "$(typeof(local_spl)) stepping..."
181181

182182
# Update the sampler's VarInfo.
183183
local_spl.state.vi = spl.state.vi

test/Turing/inference/hmc.jl

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -411,12 +411,12 @@ function AbstractMCMC.step!(
411411
spl.state.i += 1
412412
spl.state.eval_num = 0
413413

414-
Turing.DEBUG && @debug "current ϵ: "
414+
@debug "current ϵ: "
415415

416416
# When a Gibbs component
417417
if spl.selector.tag != :default
418418
# Transform the space
419-
Turing.DEBUG && @debug "X-> R..."
419+
@debug "X-> R..."
420420
link!(spl.state.vi, spl)
421421
model(spl.state.vi, spl)
422422
end
@@ -440,7 +440,7 @@ function AbstractMCMC.step!(
440440
spl.state.i, spl.alg.n_adapts, t.z.θ, t.stat.acceptance_rate)
441441
end
442442

443-
Turing.DEBUG && @debug "decide whether to accept..."
443+
@debug "decide whether to accept..."
444444

445445
# Update `vi` based on acceptance
446446
if t.stat.is_accept
@@ -453,7 +453,7 @@ function AbstractMCMC.step!(
453453

454454
# Gibbs component specified cares
455455
# Transform the space back
456-
Turing.DEBUG && @debug "R -> X..."
456+
@debug "R -> X..."
457457
spl.selector.tag != :default && invlink!(spl.state.vi, spl)
458458

459459
return HamiltonianTransition(spl, t)
@@ -514,14 +514,10 @@ function DynamicPPL.assume(
514514
vn::VarName,
515515
vi,
516516
)
517-
Turing.DEBUG && _debug("assuming...")
518517
updategid!(vi, vn, spl)
519518
r = vi[vn]
520519
# acclogp!(vi, logpdf_with_trans(dist, r, istrans(vi, vn)))
521520
# r
522-
Turing.DEBUG && _debug("dist = $dist")
523-
Turing.DEBUG && _debug("vn = $vn")
524-
Turing.DEBUG && _debug("r = $r, typeof(r)=$(typeof(r))")
525521
return r, logpdf_with_trans(dist, r, istrans(vi, vn))
526522
end
527523

test/Turing/variational/VariationalInference.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ function optimize!(
217217
Δ = apply!(optimizer, θ, Δ)
218218
@. θ = θ - Δ
219219

220-
Turing.DEBUG && @debug "Step $i" Δ DiffResults.value(diff_result)
220+
@debug "Step $i" Δ DiffResults.value(diff_result)
221221

222222
# Update the progress bar.
223223
progress && ProgressLogging.@logprogress i/max_iters

test/Turing/variational/advi.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ function vi(model::Model, alg::ADVI; optimizer = TruncatedADAGrad())
131131
end
132132

133133
function vi(model, alg::ADVI, q::TransformedDistribution{<:TuringDiagMvNormal}; optimizer = TruncatedADAGrad())
134-
Turing.DEBUG && @debug "Optimizing ADVI..."
134+
@debug "Optimizing ADVI..."
135135
# Initial parameters for mean-field approx
136136
μ, σs = params(q)
137137
θ = vcat(μ, invsoftplus.(σs))
@@ -144,7 +144,7 @@ function vi(model, alg::ADVI, q::TransformedDistribution{<:TuringDiagMvNormal};
144144
end
145145

146146
function vi(model, alg::ADVI, q, θ_init; optimizer = TruncatedADAGrad())
147-
Turing.DEBUG && @debug "Optimizing ADVI..."
147+
@debug "Optimizing ADVI..."
148148
θ = copy(θ_init)
149149
optimize!(elbo, alg, q, model, θ; optimizer = optimizer)
150150

0 commit comments

Comments
 (0)