Skip to content

Commit 48dc9bd

Browse files
committed
Add serialization how_to
1 parent 95c797f commit 48dc9bd

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

docs/pages.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ pages = [
2424
"Custom Modeling Languages" => "how_to/custom_dsl.md",
2525
"Custom Gradients" => "how_to/custom_derivatives.md",
2626
"Incremental Computation" => "how_to/custom_incremental_computation.md",
27+
"Serializing Dynamic DSL Traces" => "how_to/serialization.md"
2728
],
2829
"API Reference" => [
2930
"Modeling Library" => [

docs/src/how_to/serialization.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# [Saving Traces between REPL Sessions](@id how_to_serialize)
2+
3+
When saving a trace to disk, it is likely to run into issues using `Serialization.jl` since traces internally track function pointers (recall `Trace`s may contain function pointers). Instead, you can try the experiemental [`GenSerialization.jl`](https://github.com/probcomp/GenSerialization.jl) which uses can discard function call data. This is most useful for check-pointing work (e.g. inference) between different REPL sessions on the same machine.
4+
5+
Since `Serialization.jl` is used underneath, similar restrictions apply (see [`serialize`](https://docs.julialang.org/en/v1/stdlib/Serialization/#Serialization.serialize)). Please not we do not guarantee portability between different machines and have yet extensively test this.
6+
7+
!!! note
8+
See the repo for warnings and limitations.
9+
10+
11+
An example:
12+
13+
```julia
14+
using Gen
15+
using GenSerialization
16+
17+
@gen function model(p)
18+
x ~ bernoulli(p)
19+
end
20+
21+
trace = simulate(model, (0.2))
22+
serialize("coin_flip.gen", trace)
23+
```
24+
25+
This stores the trace in a file. Now to deserialize, run
26+
```julia
27+
saved_trace = deserialize("coin_flip.gen", model)
28+
```
29+
!!! note
30+
The same generative function used to save out the trace must be defined at runtime before deserialization.
31+

0 commit comments

Comments
 (0)