Skip to content

Instantly share code, notes, and snippets.

View torfjelde's full-sized avatar

Tor Erlend Fjelde torfjelde

View GitHub Profile
@torfjelde
torfjelde / Manifest.toml
Created May 27, 2024 16:26
Failing example model for Enzyme.
# This file is machine-generated - editing it directly is not advised
julia_version = "1.10.3"
manifest_format = "2.0"
project_hash = "85ff4462759bdfb1f5c27a8e8112fa888fa93071"
[[deps.ADTypes]]
git-tree-sha1 = "daf26bbdec60d9ca1c0003b70f389d821ddb4224"
uuid = "47edcb42-4c32-4615-8424-f2b9edc5f35b"
version = "1.2.1"
@torfjelde
torfjelde / model-gen.jl
Last active May 17, 2024 08:32
Example of how to _generate_ a Turing.jl model. This can be useful if one is working with very performance critical code where we want to unroll loops of `~` statements, etc. to improve performance.
julia> using DynamicPPL, Distributions
julia> struct NTModel{names,V}
nt::NamedTuple{names,V}
end
julia> model_template = NTModel((a=Normal(0,1), b=Normal(100, 1)))
NTModel{(:a, :b), Tuple{Normal{Float64}, Normal{Float64}}}((a = Normal{Float64}(μ=0.0, σ=1.0), b = Normal{Float64}(μ=100.0, σ=1.0)))
@torfjelde
torfjelde / benchmarking-turing.jl
Last active February 27, 2024 21:19
Determining chunksize to use in ForwardDiff.jl for a Turing.jl model
julia> using Turing, TuringBenchmarking, ADTypes
julia> @model function f_model(n)
μ ~ truncated(Normal(), lower=0)
σ² ~ InverseGamma(2, 3)
x ~ filldist(truncated(Normal(μ, sqrt(σ²)); lower=0), n)
end
f_model (generic function with 2 methods)
@torfjelde
torfjelde / Manifest.toml
Last active February 10, 2024 12:56
Quick and dirty example of a mixture sampler using some internals of MCMCTempering.jl.
# This file is machine-generated - editing it directly is not advised
julia_version = "1.10.0"
manifest_format = "2.0"
project_hash = "a0590676db40552635a5877095b47d6a64ec5413"
[[deps.ADTypes]]
git-tree-sha1 = "41c37aa88889c171f1300ceac1313c06e891d245"
uuid = "47edcb42-4c32-4615-8424-f2b9edc5f35b"
version = "0.2.6"
@torfjelde
torfjelde / hackmd-local.md
Last active December 23, 2023 11:53
Useful set up for working an HackMD file without having to edit through the browser.

Setup

Before you do anything, you'll need to install the following pieces of software:

  • pandoc for converting Markdown to HTML.
  • entr for automagic conversion of Markdown to HTML upon changes to underlying Markdown.
  • browser-sync for automagic refresh upon changes to underlying HTML.

And you need a web browser. Hopefully one is available to you.

The next step is to get yourself a local copy of MathJax v3, which can be achieved by executing

GC error (probable corruption) :
Allocations: 871149 (Pool: 870842; Big: 307); GC: 0
!!! ERROR in jl_ -- ABORTING !!!
0x7f59051d5010: Root object: 0x7f59435fc010 :: 0x7f593ed1a300 (bits: 1)
of type Task
0x7f59051d5028: Root object: 0x7f593e6e9cb0 :: 0x7f593ed1c740 (bits: 3)
of type Module
0x7f59051d5040: Root object: 0x7f593ebffff0 :: 0x7f593ed1a110 (bits: 3)
of type Array{Any, 1}
@torfjelde
torfjelde / stan-within-turing.jl
Last active September 2, 2023 11:31
Rough example of using StanDistributions.jl within Turing.jl.
julia> using PosteriorDB, StanDistributions, Turing, BridgeStan, LinearAlgebra
julia> # Necessary overloads to make it work with Turing.
function DynamicPPL.init(rng, dist::StanDistribution, ::DynamicPPL.SampleFromPrior)
# `init` uses `rand` by default but this is not supported for `StanDistribution`.
return BridgeStan.param_constrain(dist.model, randn(rng, length(dist)))
end
julia> function DynamicPPL.with_logabsdet_jacobian_and_reconstruct(f, dist::StanDistribution, x)
# HACK: This is cheating.
using DynamicPPL: OrderedDict, SamplingContext, AbstractContext, IsParent, VarName, Distribution, evaluate!!, VarInfo
import DynamicPPL: tilde_assume, dot_tilde_assume, childcontext, setchildcontext, NodeTrait
Base.@kwdef struct PriorExtractorContext{D,Ctx} <: AbstractContext
priors::D=OrderedDict{VarName,Any}()
context::Ctx=SamplingContext()
end
NodeTrait(::PriorExtractorContext) = IsParent()
childcontext(context::PriorExtractorContext) = context.context
@torfjelde
torfjelde / revise-test.jl
Last active June 21, 2023 06:33
Script for watching and running tests or certain files (using Revise.jl to avoid full re-compilation). Useful if you're taking a test-driven development. Requries the following packages to installed in the global environment: - `ArgParse` - `Revise` - `TestEnv`
#!/usr/bin/env julia
using ArgParse
# Scenarios:
# 1. Project is specified, but no files => watch project + run `runtests.jl`.
# 2. Project is specified, and files => watch project + run files.
# Command line arguments.
s = ArgParseSettings()
@add_arg_table s begin
@torfjelde
torfjelde / linearization.jl
Last active February 10, 2022 14:11
Example of (un)linearization of `VarInfo`.
using DynamicPPL
varnames_to_ranges(model::DynamicPPL.Model) = varnames_to_ranges(DynamicPPL.VarInfo(model))
varnames_to_ranges(varinfo::DynamicPPL.UntypedVarInfo) = varnames_to_ranges(varinfo.metadata)
function varnames_to_ranges(varinfo::DynamicPPL.TypedVarInfo)
offset = 0
dicts = map(varinfo.metadata) do md
vns2ranges = varnames_to_ranges(md)
vals = collect(values(vns2ranges))
vals_offset = map(r -> offset .+ r, vals)