-
Notifications
You must be signed in to change notification settings - Fork 17
TreePPL bridge #374
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
ErikDanielsson
wants to merge
1
commit into
Julia-Tempering:main
Choose a base branch
from
ErikDanielsson:treeppl
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
TreePPL bridge #374
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| """ | ||
| A [`StreamTarget`](@ref) delegating exploration to a | ||
| [TreePPL](https://www.treeppl.org/) worker processes. | ||
|
|
||
| For installation help, see the official [TreePPL installation instructions](https://treeppl.org/getting-started/getting-started) | ||
|
|
||
| ```@example TreePPL_Pigeons | ||
| using Pigeons | ||
|
|
||
| model_path = <a>/<treeppl>/<model> | ||
| data_path = <a>/<treeppl>/<input-file> | ||
| result_dir = <a>/<sample>/<directory> | ||
|
|
||
| tppl_bin = tppl_compile_model(model_path) # Compile the TreePPL model with the correct flags | ||
| tppl_target = Pigeons.TreePPLTarget(Pigeons.tppl_construct_command(tppl_bin, data_path), result_dir) | ||
| pigeons(target = tppl_target)); | ||
| ``` | ||
| """ | ||
|
|
||
| struct TreePPLTarget <: StreamTarget | ||
| command::Cmd | ||
| record_samples::Bool | ||
| output_dir::AbstractString | ||
| end | ||
|
|
||
| java_seed_32bit(rng::AbstractRNG) = rand(rng, UInt32) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need to redefined it if it's identical to the one in the blang file. |
||
|
|
||
| function initialization(target::TreePPLTarget, rng::AbstractRNG, replica_index::Int64) | ||
| # Set the seed of the TreePPL process | ||
| envs = Pair{String, Any}["PPL_SEED" => java_seed(rng)] | ||
| if target.record_samples | ||
| # Ensure that the output directory exists | ||
| mkpath(target.output_dir) | ||
| # Instruct TreePPL to save samples to file | ||
| push!(envs, "PPL_OUTPUT" => "$(target.output_dir)/tppl-replica-$replica_index.json") | ||
| elseif target.output_dir | ||
| @warn "You have specified an TreePPL output directory but record_samples is set to false. No samples will be recorded." | ||
| end | ||
| cmd_with_env = addenv(target.command, envs...) | ||
| StreamState(cmd_with_env, replica_index) | ||
| end | ||
|
|
||
| # Store the binary path and metadata about how we compiled it | ||
| Base.@kwdef struct TreePPLBinary | ||
| model_name::AbstractString | ||
| path::AbstractString | ||
| local_exploration_steps::Int | ||
| use_global::Bool | ||
| record_samples::Bool | ||
| sampling_period::Int | ||
| cps::String | ||
| align::Bool | ||
| kernel::Bool | ||
| drift::Float64 | ||
| globalProb::Float64 | ||
| end | ||
|
|
||
| function tppl_construct_target(binary::TreePPLBinary, data_path::AbstractString, result_dir::AbstractString)::TreePPLTarget | ||
| cmd = Cmd([binary.path, data_path]) | ||
| return TreePPLTarget(cmd, binary.record_samples, result_dir) | ||
| end | ||
|
|
||
| function tppl_compile_model( | ||
| model_path::AbstractString, bin::AbstractString="out"; | ||
| local_exploration_steps::Int=1, use_global::Bool=true, | ||
| record_samples::Bool=true, sampling_period::Int=1, | ||
| cps::String="full", align::Bool=true, | ||
| kernel::Bool=true, drift::Float64=1.0, | ||
| globalProb::Float64=0.0, | ||
| tpplc="tpplc" | ||
| )::TreePPLBinary | ||
| if !(cps in ["none", "full", "partial"]) | ||
| error("Only `--cps none`, `--cps full` and `--cps partial` are allowed.") | ||
| end | ||
|
|
||
| # TreePPL only supports Pigeons when running lightweight MCMC. | ||
| # Please see `tpplc --help` for explanations of what each argument does | ||
| args = [ | ||
| "-m", "mcmc-lightweight", | ||
| "--pigeons", | ||
| "--pigeons-explore-steps", local_exploration_steps, | ||
| "--cps", cps, | ||
| "--mcmc-lw-gprob", globalProb, | ||
| "--drift", drift, | ||
| "--sampling-period", sampling_period, | ||
| "--output", bin, | ||
| ] | ||
| flags = [ | ||
| (!use_global, "--pigeons-no-global"), | ||
| (kernel, "--kernel"), | ||
| (align, "--align"), | ||
| (record_samples, "--incremental-printing"), | ||
| ] | ||
| args = vcat(args, [flag for (cond, flag) in flags if cond]) | ||
|
|
||
| # Compile the model | ||
| run(`$tpplc $args $model_path`) | ||
| return TreePPLBinary( | ||
| model_name=basename(model_path), | ||
| path=abspath(bin), | ||
| local_exploration_steps=local_exploration_steps, | ||
| use_global=use_global, | ||
| record_samples=record_samples, | ||
| sampling_period=sampling_period, | ||
| cps=cps, | ||
| align=align, | ||
| kernel=kernel, | ||
| drift=drift, | ||
| globalProb=globalProb | ||
| ) | ||
| end | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should crash on CI, maybe remove new line after?