Skip to content

Instantly share code, notes, and snippets.

View xukai92's full-sized avatar
😼
Feeling cool

Kai Xu xukai92

😼
Feeling cool
View GitHub Profile
@xukai92
xukai92 / mwe.jl
Last active February 24, 2021 13:51
`invokelatest` vs `mk_function`
using InteractiveUtils
versioninfo()
using DrWatson
@quickactivate
using Comonicon, Test, BenchmarkTools, ExprOptimization
using ExprOptimization.ProbabilisticExprRules: RuleNode, mindepth_map, ProbabilisticGrammar
using GeneralizedGenerated: mk_function
@xukai92
xukai92 / takeuntil.jl
Created November 24, 2020 11:09
A variant of `takeuntil` from IterTools.jl
struct TakeUntil{I}
cond::Function
xs::I
end
"""
takeuntil(cond, xs)
An iterator that yields values from the iterator `xs` as long as the
predicate `cond` is true. Unlike `takewhile`, it also take the last
value for which the predicate `cond` is false.
@xukai92
xukai92 / ahmc_gpu.jl
Created October 26, 2020 10:14
AHMC GPU improvements
using Random: AbstractRNG, GLOBAL_RNG
using AdvancedHMC: PhasePoint, phasepoint
function AdvancedHMC.refresh(
rng::Union{AbstractRNG, AbstractVector{<:AbstractRNG}},
z::PhasePoint{T},
h::Hamiltonian
) where {T<:CuArray}
r = CuArray{Float32, 2}(undef, size(h.metric)...)
CUDA.CURAND.randn!(r)
@xukai92
xukai92 / generated_quantile.jl
Last active June 26, 2020 23:30
Generated quantiles with Turing.jl
# Modifed from https://github.com/cambridge-mlg/Covid19/blob/master/src/utils.jl#L79
function generated_quantities(m::Turing.Model, c::MCMCChains.Chains)
# If `c` is multiple chains, we convert them into a single chain
chain = length(chains(c)) == 1 ? c : MCMCChains.pool_chain(c)
vi = Turing.DynamicPPL.VarInfo(m)
return map(1:length(chain)) do i
Turing.DynamicPPL._setval!(vi, chain[i])
m(vi)
end
end
@xukai92
xukai92 / set_ssl.jl
Last active June 26, 2020 23:27
Resolve the SSL issue with Julia's Pkg
using LibGit2
LibGit2.set_ssl_cert_locations("/etc/ssl/certs/ca-certificates.crt")