Skip to content

Commit

Permalink
Add serialization how_to
Browse files Browse the repository at this point in the history
  • Loading branch information
limarta committed May 9, 2024
1 parent 95c797f commit 6f011b9
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/pages.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pages = [
"Custom Modeling Languages" => "how_to/custom_dsl.md",
"Custom Gradients" => "how_to/custom_derivatives.md",
"Incremental Computation" => "how_to/custom_incremental_computation.md",
"Serializing Dynamic DSL Traces" => "how_to/serialization.md"
],
"API Reference" => [
"Modeling Library" => [
Expand Down
31 changes: 31 additions & 0 deletions docs/src/how_to/serialization.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# [Saving Traces between REPL Sessions](@id how_to_serialize)

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 experimental [`GenSerialization.jl`](https://github.com/probcomp/GenSerialization.jl) which can discard function call data. This is most useful for check-pointing work (e.g. inference) between different REPL sessions on the same machine.

Since `Serialization.jl` is used underneath, similar restrictions apply (see [`serialize`](https://docs.julialang.org/en/v1/stdlib/Serialization/#Serialization.serialize)). Please note we do not guarantee portability between different machines and have yet to extensively test this.

!!! note
See the repository for warnings and limitations.


An example:

```julia
using Gen
using GenSerialization

@gen function model(p)
x ~ bernoulli(p)
end

trace = simulate(model, (0.2))
serialize("coin_flip.gen", trace)
```

This stores the trace in a file. Now to deserialize, run
```julia
saved_trace = deserialize("coin_flip.gen", model)
```
!!! note
The same generative function used to save out the trace must be defined at runtime before deserialization.

0 comments on commit 6f011b9

Please sign in to comment.