You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: JuliaBUGS/History.md
+10Lines changed: 10 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,15 @@
1
1
# JuliaBUGS Changelog
2
2
3
+
## 0.10.4
4
+
5
+
-**DifferentiationInterface.jl integration**: JuliaBUGS now uses [DifferentiationInterface.jl](https://github.com/JuliaDiff/DifferentiationInterface.jl) for automatic differentiation, providing a unified interface to multiple AD backends.
6
+
- Add `adtype` parameter to `compile()` function for specifying AD backends
7
+
- Support convenient symbol shortcuts: `:ReverseDiff`, `:ForwardDiff`, `:Zygote`, `:Enzyme`
8
+
- Gradient computation is prepared during compilation for optimal performance
`LogDensityProblemsAD.jl` defined some extensions that support automatic differentiation packages.
194
-
For example, with `ReverseDiff.jl`
193
+
### Automatic Differentiation
194
+
195
+
JuliaBUGS integrates with automatic differentiation (AD) through [DifferentiationInterface.jl](https://github.com/JuliaDiff/DifferentiationInterface.jl), enabling gradient-based inference methods like Hamiltonian Monte Carlo (HMC) and No-U-Turn Sampler (NUTS).
196
+
197
+
#### Specifying an AD Backend
198
+
199
+
To compile a model with gradient support, pass the `adtype` parameter to `compile`:
195
200
196
201
```julia
197
-
using LogDensityProblemsAD, ReverseDiff
202
+
# Using explicit ADType from ADTypes.jl
203
+
using ADTypes
204
+
model =compile(model_def, data; adtype=AutoReverseDiff(compile=true))
205
+
206
+
# Using convenient symbol shortcuts
207
+
model =compile(model_def, data; adtype=:ReverseDiff) # Equivalent to above
For fine-grained control, use explicit `ADTypes` constructors:
217
+
218
+
```julia
219
+
# ReverseDiff without compilation
220
+
model =compile(model_def, data; adtype=AutoReverseDiff(compile=false))
200
221
```
201
222
202
-
Here `ad_model` will also implement all the interfaces of [`LogDensityProblems.jl`](https://www.tamaspapp.eu/LogDensityProblems.jl/dev/).
203
-
`LogDensityProblemsAD.jl` will automatically add the interface function [`logdensity_and_gradient`](https://www.tamaspapp.eu/LogDensityProblems.jl/dev/#LogDensityProblems.logdensity_and_gradient) to the model, which will return the log density and gradient of the model.
204
-
And `ad_model` can be used in the same way as `model` in the example below.
223
+
The compiled model with gradient support implements the [`LogDensityProblems.jl`](https://www.tamaspapp.eu/LogDensityProblems.jl/dev/) interface, including [`logdensity_and_gradient`](https://www.tamaspapp.eu/LogDensityProblems.jl/dev/#LogDensityProblems.logdensity_and_gradient), which returns both the log density and its gradient.
205
224
206
225
### Inference
207
226
208
-
For a differentiable model, we can use [`AdvancedHMC.jl`](https://github.com/TuringLang/AdvancedHMC.jl) to perform inference.
209
-
For instance,
227
+
For gradient-based inference, we use [`AdvancedHMC.jl`](https://github.com/TuringLang/AdvancedHMC.jl) with models compiled with an `adtype`:
210
228
211
229
```julia
212
-
using AdvancedHMC, AbstractMCMC, LogDensityProblems, MCMCChains
230
+
using AdvancedHMC, AbstractMCMC, LogDensityProblems, MCMCChains, ReverseDiff
231
+
232
+
# Compile with gradient support
233
+
model =compile(model_def, data; adtype=:ReverseDiff)
213
234
214
235
n_samples, n_adapts =2000, 1000
215
236
216
237
D = LogDensityProblems.dimension(model); initial_θ =rand(D)
This is consistent with the result in the [OpenBUGS seeds example](https://chjackson.github.io/openbugsdoc/Examples/Seeds.html).
@@ -283,7 +338,7 @@ The model compilation code remains the same, and we can sample multiple chains i
283
338
```julia
284
339
n_chains =4
285
340
samples_and_stats = AbstractMCMC.sample(
286
-
ad_model,
341
+
model,
287
342
AdvancedHMC.NUTS(0.65),
288
343
AbstractMCMC.MCMCThreads(),
289
344
n_samples,
@@ -311,7 +366,7 @@ For example:
311
366
312
367
```julia
313
368
@everywherebegin
314
-
using JuliaBUGS, LogDensityProblems, LogDensityProblemsAD, AbstractMCMC, AdvancedHMC, MCMCChains, ReverseDiff # also other packages one may need
369
+
using JuliaBUGS, LogDensityProblems, AbstractMCMC, AdvancedHMC, MCMCChains, ADTypes, ReverseDiff
315
370
316
371
# Define the functions to use
317
372
# Use `@bugs_primitive` to register the functions to use in the model
@@ -322,7 +377,7 @@ end
322
377
323
378
n_chains =nprocs() -1# use all the processes except the parent process
324
379
samples_and_stats = AbstractMCMC.sample(
325
-
ad_model,
380
+
model,
326
381
AdvancedHMC.NUTS(0.65),
327
382
AbstractMCMC.MCMCDistributed(),
328
383
n_samples,
@@ -342,6 +397,67 @@ In this case, we pass two additional arguments to `AbstractMCMC.sample`:
342
397
Note that the `init_params` argument is now a vector of initial parameters for each chain.
343
398
Sometimes the progress logger can cause problems in distributed setting, so we can disable it by setting `progress = false`.
344
399
400
+
## Choosing an Automatic Differentiation Backend
401
+
402
+
JuliaBUGS integrates with multiple automatic differentiation (AD) backends through [DifferentiationInterface.jl](https://github.com/JuliaDiff/DifferentiationInterface.jl), providing flexibility to choose the most suitable backend for your model.
403
+
404
+
### Available Backends
405
+
406
+
The following AD backends are supported via convenient symbol shortcuts:
407
+
408
+
-**`:ReverseDiff`** (recommended) — Tape-based reverse-mode AD, highly efficient for models with many parameters. Uses compilation by default for optimal performance.
409
+
-**`:ForwardDiff`** — Forward-mode AD, efficient for models with few parameters (typically < 20).
410
+
-**`:Zygote`** — Source-to-source reverse-mode AD, general-purpose but may be slower than ReverseDiff for many models.
411
+
-**`:Enzyme`** — Experimental high-performance AD backend with LLVM-level transformations.
412
+
413
+
### Usage Examples
414
+
415
+
#### Basic Usage with Symbol Shortcuts
416
+
417
+
The simplest way to specify an AD backend is using symbol shortcuts:
418
+
419
+
```julia
420
+
# ReverseDiff with tape compilation (recommended for most models)
421
+
model =compile(model_def, data; adtype=:ReverseDiff)
422
+
423
+
# ForwardDiff (good for small models with few parameters)
424
+
model =compile(model_def, data; adtype=:ForwardDiff)
425
+
426
+
# Zygote (source-to-source AD)
427
+
model =compile(model_def, data; adtype=:Zygote)
428
+
```
429
+
430
+
#### Advanced Configuration
431
+
432
+
For fine-grained control, use explicit `ADTypes` constructors:
433
+
434
+
```julia
435
+
using ADTypes
436
+
437
+
# ReverseDiff without tape compilation
438
+
model =compile(model_def, data; adtype=AutoReverseDiff(compile=false))
439
+
440
+
# ReverseDiff with compilation (equivalent to :ReverseDiff)
441
+
model =compile(model_def, data; adtype=AutoReverseDiff(compile=true))
442
+
```
443
+
444
+
### Performance Considerations
445
+
446
+
-**ReverseDiff with compilation** (`:ReverseDiff`) is recommended for most models, especially those with many parameters. Compilation adds a one-time overhead but significantly speeds up subsequent gradient evaluations.
447
+
448
+
-**ForwardDiff** (`:ForwardDiff`) is often faster for models with few parameters (< 20), as it avoids tape construction overhead.
449
+
450
+
-**Tape compilation trade-off**: While `AutoReverseDiff(compile=true)` has higher initial compilation time, it provides faster gradient evaluations during sampling. For quick prototyping or models that will only be sampled a few times, `AutoReverseDiff(compile=false)` may be preferable.
451
+
452
+
### Compatibility
453
+
454
+
All models compiled with an `adtype` implement the full [`LogDensityProblems.jl`](https://www.tamaspapp.eu/LogDensityProblems.jl/dev/) interface, making them compatible with:
455
+
456
+
-[`AdvancedHMC.jl`](https://github.com/TuringLang/AdvancedHMC.jl) — NUTS and HMC samplers
457
+
- Any other sampler that works with the LogDensityProblems interface
458
+
459
+
The gradient computation is prepared during model compilation for optimal performance during sampling.
460
+
345
461
## More Examples
346
462
347
463
We have transcribed all the examples from the first volume of the BUGS Examples ([original](https://www.multibugs.org/examples/latest/VolumeI.html) and [transcribed](https://github.com/TuringLang/JuliaBUGS.jl/tree/main/JuliaBUGS/src/BUGSExamples/Volume_1)). All programs and data are included, and can be compiled using the steps described in the tutorial above.
0 commit comments