Skip to content

Instantly share code, notes, and snippets.

@timholy
Last active February 18, 2022 14:41
Show Gist options
  • Save timholy/3e7185761d151a9c21072e28b8804a3d to your computer and use it in GitHub Desktop.
Save timholy/3e7185761d151a9c21072e28b8804a3d to your computer and use it in GitHub Desktop.
diff --git a/src/precompile.jl b/src/precompile.jl
index 9b72977..eba10e0 100644
--- a/src/precompile.jl
+++ b/src/precompile.jl
@@ -5,4 +5,5 @@ function _precompile_()
# CSV.Context(IOBuffer(CSV.PRECOMPILE_DATA))
# foreach(row -> row, CSV.Rows(IOBuffer(PRECOMPILE_DATA)))
CSV.Context(joinpath(dirname(pathof(CSV)), "promotions.csv"))
+ CSV.File(joinpath(pkgdir(CSV), "test", "testfiles", "precompile.csv"))
end
diff --git a/src/other/precompile.jl b/src/other/precompile.jl
index a1c2e5ec..6136709a 100644
--- a/src/other/precompile.jl
+++ b/src/other/precompile.jl
@@ -15,7 +15,7 @@
# * disabling precompilation on Julia older than 1.5
# * run @warnpcfail check for all=true and all=false both on Julia stable and nightly
-function precompile(all=false)
+function precompile(all=true)
VERSION >= v"1.5" || return nothing
all || ccall(:jl_generating_output, Cint, ()) == 1 || return nothing
diff --git a/src/Flux.jl b/src/Flux.jl
index 80d999de..fb9529f3 100644
--- a/src/Flux.jl
+++ b/src/Flux.jl
@@ -57,4 +57,15 @@ include("deprecations.jl")
include("cuda/cuda.jl")
+actual(x) = 4x + 2
+x_train, x_test = hcat(0:5...), hcat(6:10...)
+y_train, y_test = actual.(x_train), actual.(x_test)
+predict = Dense(1, 1)
+loss(x, y) = Flux.Losses.mse(predict(x), y)
+using Flux: train!
+opt = Descent()
+data = [(x_train, y_train)]
+parameters = Flux.params(predict)
+train!(loss, parameters, data, opt)
+
end # module
diff --git a/src/ImageFiltering.jl b/src/ImageFiltering.jl
index 7f10bfe..47211d4 100644
--- a/src/ImageFiltering.jl
+++ b/src/ImageFiltering.jl
@@ -107,4 +107,6 @@ if Base.VERSION >= v"1.4.2"
include("precompile.jl")
end
+imfilter(rand(Float32, 100, 100), KernelFactors.gaussian((1.5f0, 1.5f0)))
+
end # module
diff --git a/Project.toml b/Project.toml
index 5a2a0596..d2d0e9aa 100644
--- a/Project.toml
+++ b/Project.toml
@@ -7,6 +7,7 @@ version = "0.22.2"
Calculus = "49dc2e85-a5d0-5ad3-a950-438e2897f1b9"
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
+GLPK = "60bf3e95-4087-53dc-ae20-288a0d20c6a6"
JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
MathOptInterface = "b8f27783-ece8-5eb3-8dc8-9495eed66fee"
diff --git a/src/JuMP.jl b/src/JuMP.jl
index ef353cc3..93831a74 100644
--- a/src/JuMP.jl
+++ b/src/JuMP.jl
@@ -1497,4 +1497,15 @@ if Base.VERSION >= v"1.4.2"
_precompile_()
end
+# using JuMP
+using GLPK
+model = Model(GLPK.Optimizer)
+@variable(model, x >= 0)
+@variable(model, 0 <= y <= 3)
+@objective(model, Min, 12x + 20y)
+@constraint(model, c1, 6x + 8y >= 100)
+@constraint(model, c2, 7x + 12y >= 120)
+# print(model)
+optimize!(model)
+
index 04e504ff..fad962b4 100644
--- a/GLMakie/src/precompile.jl
+++ b/GLMakie/src/precompile.jl
@@ -27,4 +27,6 @@ function _precompile_()
isdefined(GLMakie, Symbol("#81#82")) && Base.precompile(Tuple{getfield(GLMakie, Symbol("#81#82")),Tuple{Symbol, Float64}}) # time: 0.001081687
isdefined(GLMakie, Symbol("#81#82")) && Base.precompile(Tuple{getfield(GLMakie, Symbol("#81#82")),Vector{Float32}}) # time: 0.001065034
isdefined(GLMakie, Symbol("#89#90")) && Base.precompile(Tuple{getfield(GLMakie, Symbol("#89#90")),Annotations{Tuple{Vector{Tuple{String, Point{2, Float32}}}}}}) # time: 0.001031033
+ plot(rand(10))
+ precompile(Tuple{GLMakie.var"#107#108"{GLMakie.Screen, Scene}, AbstractPlot})
end
diff --git a/src/Plots.jl b/src/Plots.jl
index 5717a37e..e12cf93c 100644
--- a/src/Plots.jl
+++ b/src/Plots.jl
@@ -266,6 +266,7 @@ end
const CURRENT_BACKEND = CurrentBackend(:none)
const PLOTS_SEED = 1234
-include("precompile_includer.jl")
+# include("precompile_includer.jl")
+display(plot(rand(10)))
end # module
using PyPlot: PyPlot, plt
using DataFrames, CSV
files = ("master.csv", "prune.csv", "full.csv")
groups = map(fn -> splitext(fn)[1], files)
dfs = map(files) do fn
DataFrame(CSV.File(fn; header=["package", "jisize", "load", "TTFT"]))
end
# Sort the packages by TTFT on master
p = sortperm(dfs[1].TTFT)
dfs = map(dfs) do df
df[p,:]
end
fig, axs = plt.subplots(3, 1)
labels = dfs[1].package
x = 1:length(labels)
w = 1/(length(dfs) + 1)
ax = axs[1]
barsz = [ax.bar(x .+ i*w, df.jisize/1024^2, w, label=groups[i]) for (i, df) in enumerate(dfs)]
# ax.set_yscale("log", base=2)
ax.set_ylabel("ji size (MB)")
ax.set_xticks([])
ax = axs[2]
barl = [ax.bar(x .+ i*w, df.load, w, label=groups[i]) for (i, df) in enumerate(dfs)]
# ax.set_yscale("log", base=2)
ax.set_ylabel("load time (s)")
ax.set_xticks([])
ax = axs[3]
bart = [ax.bar(x .+ i*w, df.TTFT, w, label=groups[i]) for (i, df) in enumerate(dfs)]
# ax.set_yscale("log", base=2)
ax.set_ylabel("TTFT (s)")
ax.set_xticks(x .+ 3*w/2)
ax.set_xticklabels(labels; rotation=90)
plt.legend(barsz, groups)
fig.tight_layout()
# Workflow:
# - pick a depot location
# - run create_package_folders (best to do this on the same major/minor Julia version you plan to test on, so the Manifests get resolved appropriately)
# - manually edit the devved pkgs in the depot to ensure they precompile the given workload
# - for each julia executable you want to test, pick an output file name and execute run_workload
using Pkg
const default_pkgs = String["CSV", "DataFrames", "Revise", "Plots", "GLMakie", "OrdinaryDiffEq", "ModelingToolkit", #="Flux",=# "JuMP", "ImageFiltering"]
const default_extras = ["LV"]
const default_deps = Dict("JuMP" => ["GLPK"], "Revise" => ["Example"], "DataFrames" => ["PooledArrays"], "ModelingToolkit" => ["OrdinaryDiffEq"])
const default_workloads = Dict(
"CSV" => """CSV.File(joinpath(pkgdir(CSV), "test", "testfiles", "precompile.csv"))""",
"DataFrames" =>
"""
using PooledArrays: PooledArrays
for v in ([1, 2], [2, 1], [2, 2, 1], Int32[1, 2], Int32[2, 1], Int32[2, 2, 1]),
op in (identity, x -> string.(x), x -> PooledArrays.PooledArray(string.(x))),
on in (:v1, [:v1, :v2])
df = DataFrame(v1=op(v), v2=v)
combine(groupby(df, on), identity, :v1 => identity,
:v2 => ByRow(identity), :v2 => sum)
innerjoin(df, select(df, on), on=on)
outerjoin(df, select(df, on), on=on)
end
""",
"Revise" =>
"""
using Example
sleep(0.1)
cp(joinpath(pkgdir(Example), "src/Example1.jl"), joinpath(pkgdir(Example), "src/Example.jl"); force=true)
revise()
""",
"Plots" => "using Plots; display(plot(rand(10)))",
"GLMakie" => "using GLMakie; display(plot(rand(10)))",
"OrdinaryDiffEq" =>
"""
function lorenz(du,u,p,t)
du[1] = 10.0(u[2]-u[1])
du[2] = u[1]*(28.0-u[3]) - u[2]
du[3] = u[1]*u[2] - (8/3)*u[3]
end
lorenzprob = ODEProblem(lorenz,[1.0;0.0;0.0],(0.0,1.0))
solve(lorenzprob,BS3())
""",
"ModelingToolkit" =>
"""
using ModelingToolkit, OrdinaryDiffEq
function f()
@parameters t σ ρ β
@variables x(t) y(t) z(t)
D = Differential(t)
eqs = [D(D(x)) ~ σ*(y-x),
D(y) ~ x*(ρ-z)-y,
D(z) ~ x*y - β*z]
@named sys = ODESystem(eqs)
sys = structural_simplify(sys)
u0 = [D(x) => 2.0,
x => 1.0,
y => 0.0,
z => 0.0]
p = [σ => 28.0,
ρ => 10.0,
β => 8/3]
tspan = (0.0,100.0)
prob = ODEProblem(sys,u0,tspan,p,jac=true)
end
f()
""",
"Flux" =>
"""
actual(x) = 4x + 2
x_train, x_test = hcat(0:5...), hcat(6:10...)
y_train, y_test = actual.(x_train), actual.(x_test)
predict = Dense(1, 1)
loss(x, y) = Flux.Losses.mse(predict(x), y)
using Flux: train!
opt = Descent()
data = [(x_train, y_train)]
parameters = Flux.params(predict)
train!(loss, parameters, data, opt)
""",
"ImageFiltering" => "imfilter(rand(Float32, 100, 100), KernelFactors.gaussian((1.5f0, 1.5f0)))",
"JuMP" =>
"""
using GLPK
model = Model(GLPK.Optimizer)
@variable(model, x >= 0)
@variable(model, 0 <= y <= 3)
@objective(model, Min, 12x + 20y)
@constraint(model, c1, 6x + 8y >= 100)
@constraint(model, c2, 7x + 12y >= 120)
# print(model)
optimize!(model)
""",
"LV" =>
"""
A = rand(Float64, 512, 512)
kern = [0.1 0.3 0.1;
0.3 0.5 0.3;
0.1 0.3 0.1];
filter2davx(A, kern)
"""
)
const pre_work = Dict(
"GLMakie" => "using GLMakie; display(plot(rand(10)))", # "Caching fonts..."
)
const post_work = Dict(
"Revise" =>
"""
using Example
cp(joinpath(pkgdir(Example), "src/Example0.jl"), joinpath(pkgdir(Example), "src/Example.jl"); force=true)
"""
)
"""
runner_dirs = create_package_folders(depot_path)
"""
function create_package_folders(depot_path, pkgs = default_pkgs; deps = default_deps)
mkpath(depot_path)
olddepot = copy(DEPOT_PATH)
empty!(DEPOT_PATH)
try
pushfirst!(DEPOT_PATH, depot_path)
for pkg in pkgs
p = mkpath(joinpath(depot_path, "runner_dirs", pkg))
Pkg.activate(p)
try
Pkg.develop(pkg)
for dep in get(deps, pkg, String[])
pkg == "Revise" ? Pkg.develop(dep) : Pkg.add(dep)
end
catch
end
end
finally
empty!(DEPOT_PATH)
append!(DEPOT_PATH, olddepot)
end
return joinpath(depot_path, "runner_dirs")
end
function run_workload(output, juliacmd, ver, depot_path, runner_dirs, pkgs = vcat(default_pkgs, default_extras); clear_output::Bool=true, clear_compiled::Bool=true)
clear_compiled && rm(joinpath(depot_path, "compiled", ver); force=true, recursive=true)
if clear_output
rm(output; force=true)
elseif isfile(output)
elim = String[]
for line in eachline(output)
nms = split(line, ',')
push!(elim, nms[1])
end
pkgs = setdiff(pkgs, elim)
end
for pkg in pkgs
# try
setup =
"""
using Pkg;
empty!(DEPOT_PATH)
pushfirst!(DEPOT_PATH, "$depot_path");
Pkg.activate("$(joinpath(runner_dirs, pkg))");
Pkg.precompile()
"""
run(`$juliacmd --startup=no -e $setup`)
if haskey(pre_work, pkg)
wl = pre_work[pkg]
work =
"""
using Pkg;
empty!(DEPOT_PATH)
pushfirst!(DEPOT_PATH, "$depot_path");
Pkg.activate("$(joinpath(runner_dirs, pkg))");
$wl
"""
run(`$juliacmd --startup=no -e $work`)
end
wl = default_workloads[pkg]
work =
"""
using Pkg;
empty!(DEPOT_PATH)
pushfirst!(DEPOT_PATH, "$depot_path");
Pkg.activate("$(joinpath(runner_dirs, pkg))");
tstart = time(); using $pkg; tloaded = time(); $wl; tdone = time();
id = Base.identify_package("$pkg");
origin = Base.pkgorigins[id];
open("$output", "a") do io
println(io, $pkg, ",", stat(origin.cachepath).size, ",", tloaded-tstart, ",", tdone-tloaded)
end
"""
run(`$juliacmd --startup=no -e $work`)
if haskey(post_work, pkg)
wl = post_work[pkg]
cleanup =
"""
using Pkg;
empty!(DEPOT_PATH)
pushfirst!(DEPOT_PATH, "$depot_path");
Pkg.activate("$(joinpath(runner_dirs, pkg))");
$wl
"""
run(`$juliacmd --startup=no -e $cleanup`)
end
# catch
# end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment