Getting started with Coinfer

Coinfer consists of three parts:
  • The Cloud Service.
  • The Coinfer.jl Julia package.
  • The Coinfer.js Javascript SDK.

Create API Token

First, let’s create a token in the Coinfer website:
  1. Open this URL with you browser: https://coinfer/bayes/.
  2. Click “Sign in with Auth0”.
  3. After successfully login to Coinfer website, click your account logo at the bottom left corner of the web page.
  4. Choose “Profile” > “Create Token”.
  5. Set you COINFER_AUTH_TOKEN environment variable to the new token created: export COINFER_AUTH_TOKEN=<newtoken>

Create Model Project

Create a new directory for our demo project and then change the current working directory to it:
mkdir coinfer-demo && cd coinfer-demo
Create a Julia source file to hold our model. I’ll use the model from the tutorial of Turing website. Create a file named main.jl and type in the following:
# main.jl
using Turing

@model function gdemo(x, y)
    s ~ InverseGamma(2, 3)
    m ~ Normal(0, sqrt(s))
    x ~ Normal(m, sqrt(s))
    return y ~ Normal(m, sqrt(s))
end
With the model defined, we need to create a function which will be used as the entrance point to run the model. By default, the name of the function is model. This function need to return the model instance and the sampler we want to use in the experiment.
# main.jl

using DynamicPPL
using Turing

function model(_input)
    _model = gdemo(1.5, 2)
    return _model, DynamicPPL.Sampler(NUTS(), _model)
end
New let’s use the APIs in Coinfer libary to run the above model in Coinfer server. Create a file named run_model.jl. First we need to create a structure which contains the information to locate the model entrypoint. We do this by call the create_model fucntion. This function takes three positional functions:
  1. The type of the model. We specify CloudFunction here. This means the model will be run in the cloud environment instead of our local environment. Other possible values are AnonymousCloudFunction, LocalFunction. The meaning of them can be found in the document.
  2. The project directory.
  3. The endpoints infomation. We create a Endpoints structure without any parameters. This means use the default server address hardcoded in the Endpoints source code. If you have your server, you can specify it’s address like this: Endpoints("https://my.endpoints.url")
# run_model.jl

using Coinfer
using StableRNGs: StableRNG
using AbstractMCMC: MCMCThreads

endpoint = Coinfer.ServerlessBayes.Endpoints()

model = Coinfer.ServerlessBayes.create_model(
    Coinfer.ServerlessBayes.CloudFunction,
    dirname(@__FILE__),
    endpoint
)
Then we can call sample to run the model.
# run_model.jl

model_id, experiment_id = Coinfer.ServerlessBayes.sample(
    StableRNG(1234567),
    model,
    10,  # the iteration count. As a demo, we set this to a small value
    MCMCThreads(),  # the parallel algorithm
    2;  # the chain number
)

Run

To run the script, we need to add the dependecies to the Julia project:
$ julia --project=.
julia> using Pkg
julia> Pkg.add("StableRNGs")
julia> Pkg.add(url="https://github.com/vectorly-ai/AbstractMCMC.jl.git", rev="multiple-chains")
julia> Pkg.add(url="https://github.com/vectorly-ai/TuringCallbacks.jl.git", rev="multiple-chains")
julia> Pkg.add(url="git@github.com:vectorly-ai/server.git", subdir="client/Coinfer.jl")
Then you can run the model like this:
julia --project run_model.jl

# ouptput
The model have been scheduled to run. You can query it with:
model_id=MDE3D93F4, experiment_id=X37E62A68
A new model and experiment will be created in the server. The model ID and experiment ID will be printed. You can query the model and the experiment running details in the Coinfer website with these IDs.

Inspect the Sample Data

To get the experiment running details, you can log in to the Coinfer website and then click the “Experiment” tab on the left panel of the page. Usually your new experiment will be the first item in the list. Click on the experiment name, and you will see the experiment detail page. You can also use the experiment ID to construct the URL as follows to directly open the experiment detail page: https://coinfer.ai/bayes/#/experiment/[experiment_id] The experiment detail page can be divided into three parts. The top-left area is the list of chains. The variable names list is under it. The large area on the right is the main content area to display various information.