Skip to content

Commit c0fe26e

Browse files
committed
Add serialization how_to
1 parent 95c797f commit c0fe26e

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

Diff for: docs/pages.jl

+1
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" => [

Diff for: docs/src/how_to/serialization.md

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# [How to Serialize Traces for Storage](@id how_to_serialize)
2+
3+
For emphemeral storage of Dynamic Traces, `Serialization.jl` works well. For long term storage, however, you will likely run into issues using `Serialization.jl` since traces internally track function pointers (recall `Trace`s may contain function pointers). For this, you can try the experiemental [`GenSerialization.jl`](https://github.com/probcomp/GenSerialization.jl) which still uses `Serialization.jl` but can properly discard function call data.
4+
5+
6+
7+
An example:
8+
9+
```julia
10+
using Gen
11+
using GenSerialization
12+
13+
@gen function model(p)
14+
x ~ bernoulli(p)
15+
end
16+
17+
trace = simulate(model, (0.2))
18+
serialize("coin_flip.gen", trace)
19+
```
20+
21+
This stores the trace in a file. Now to deserialize, run
22+
```julia
23+
saved_trace = deserialize("coin_flip.gen", model)
24+
```
25+
!!! note
26+
The same generative function used to save out the trace must be defined at runtime before deserialization.
27+
28+
!!! note
29+
See the repo for warnings and limitations.

0 commit comments

Comments
 (0)