Skip to content

Commit 27c1af0

Browse files
committed
Add serialization how_to
1 parent 95c797f commit 27c1af0

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-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: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# [How to Serialize Traces for Storage](@id how_to_serialize)
2+
3+
For emphemeral storage, `Serialization.jl` works well. For long term storage, however, you will likely run into issues serializing function pointers (recall `Trace`s may contain function pointers). For this, you can try the experiemental [`GenSerialization.jl`](https://github.com/probcomp/GenSerialization.jl).
4+
5+
6+
An example:
7+
8+
```julia
9+
using Gen
10+
using GenSerialization
11+
12+
@gen function model(p)
13+
x ~ bernoulli(p)
14+
end
15+
16+
trace = simulate(model, (0.2))
17+
serialize("coin_flip.gen", trace)
18+
```
19+
20+
This stores the trace in a file. Now to deserialize, run
21+
```julia
22+
saved_trace = deserialize("coin_flip.gen", model)
23+
```
24+
25+
!!! note
26+
See the repo for warnings and limitations.

0 commit comments

Comments
 (0)