agentleFS
90184479018447/julia-pro-p/skills/julia-model-deployment/SKILL.md

julia-model-deployment

Skill0 starsChanged 3 days agoMITView on GitHub

Deploy Julia models with JLD2, ONNX, PackageCompiler.jl, Genie.jl, Oxygen.jl, and PythonCall.jl. Use for Julia model export, API serving, containers, and startup latency.

The file

We split this skill into the rules it gives an agent

2 rules. About 0 are common, 2 are distinctive, and 0 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈2

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 0

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Run with the system image:

    line 128
  2. Distinctive:

    When the serving stack is Julia and a Python library has to be reached, use PythonCall.jl:

    line 189

≈ “Common” is an estimate: at least 1 in 5 of the 408,460 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Julia Model Deployment

Model Serialization with JLD2

Save and load trained model parameters:

using JLD2, Lux

model = Chain(Dense(784, 256, relu), Dense(256, 10))
ps, st = Lux.setup(Random.default_rng(), model)

# Train model...

# Save
jldsave("model.jld2"; ps=ps, st=st)

# Load
saved = load("model.jld2")
ps_loaded = saved["ps"]
st_loaded = saved["st"]

ONNX Export with ONNXRunTime

Export models for cross-platform inference:

using ONNXRunTime

# Load an ONNX model for inference
model = ONNXRunTime.load("model.onnx")

# Run inference
input = Dict("input" => randn(Float32, 784, 1))
output = model(input)

To export Julia models to ONNX, trace through the model and serialize:

using Flux, ONNXExporter

flux_model = Chain(Dense(784, 256, relu), Dense(256, 10))
dummy_input = randn(Float32, 784, 1)

ONNXExporter.export_onnx("model.onnx", flux_model, dummy_input)

Genie.jl REST API

Serve predictions via HTTP:

using Genie, Genie.Router, Genie.Renderer.Json
using JLD2, Lux

# Load model at startup
const MODEL = Chain(Dense(784, 256, relu), Dense(256, 10))
const PARAMS = load("model.jld2")

route("/predict", method=POST) do
    payload = jsonpayload()
    x = Float32.(payload["input"])
    y, _ = MODEL(x, PARAMS["ps"], PARAMS["st"])
    json(Dict("prediction" => Array(y)))
end

route("/health") do
    json(Dict("status" => "healthy", "version" => "1.0.0"))
end

up(8080; async=false)

Oxygen.jl Lightweight Alternative

Minimal API server for simple deployments:

using Oxygen, HTTP

@post "/predict" function(req::HTTP.Request)
    data = json(req)
    x = Float32.(data["input"])
    y, _ = MODEL(x, PARAMS["ps"], PARAMS["st"])
    return Dict("prediction" => Array(y))
end

@get "/health" function()
    return Dict("status" => "healthy")
end

serve(; host="0.0.0.0", port=8080)

PackageCompiler.jl System Image

Eliminate startup latency with a custom system image:

using PackageCompiler

# Create sysimage with precompiled packages
create_sysimage(
    [:Lux, :Genie, :JLD2];
    sysimage_path="serving.so",
    precompile_execution_file="precompile_script.jl"
)

The precompile script exercises key code paths:

# precompile_script.jl
using Lux, JLD2, Genie
model = Chain(Dense(10, 5, relu), Dense(5, 2))
ps, st = Lux.setup(Random.default_rng(), model)
x = randn(Float32, 10, 1)
model(x, ps, st)

Run with the system image:

julia --sysimage=serving.so serve.jl

Standalone Application

Build a fully standalone executable:

create_app(
    ".",                        # Source directory
    "build/serve_app";          # Output directory
    precompile_execution_file="precompile_script.jl",
    force=true
)

Docker Multi-Stage Build

# Stage 1: Build sysimage
FROM julia:1.11 AS builder

WORKDIR /app
COPY Project.toml Manifest.toml ./
RUN julia --project=. -e 'using Pkg; Pkg.instantiate()'

COPY . .
RUN julia --project=. -e '
    using PackageCompiler
    create_sysimage(
        [:Lux, :Genie, :JLD2];
        sysimage_path="serving.so",
        precompile_execution_file="precompile_script.jl"
    )'

# Stage 2: Runtime
FROM julia:1.11-slim

WORKDIR /app
COPY --from=builder /app/serving.so .
COPY --from=builder /app/serve.jl .
COPY --from=builder /app/model.jld2 .

EXPOSE 8080
HEALTHCHECK CMD curl -f http://localhost:8080/health || exit 1

CMD ["julia", "--sysimage=serving.so", "serve.jl"]

Build and run:

docker build -t julia-model-server .
docker run -p 8080:8080 julia-model-server

Call Python from Julia

When the serving stack is Julia and a Python library has to be reached, use PythonCall.jl:

using PythonCall

np = pyimport("numpy")
flask = pyimport("flask")

# Bridge Julia inference into a Python web framework
function julia_predict(input_array)
    x = pyconvert(Matrix{Float32}, input_array)
    y, _ = MODEL(x, PARAMS["ps"], PARAMS["st"])
    return Py(Array(y))
end

Latency Optimization

StrategyStartup TimeBinary SizeUse Case
Default Julia~30s (TTFX)N/ADevelopment
Sysimage~2s~500MBProduction servers
Standalone app~0.5s~1GBEdge / containerized
PrecompileTools.jl~5s (cached)N/APackage development

Production Checklist

  • Serialize models with JLD2 (include both ps and st)
  • Add /health endpoint for load balancer probes
  • Build a sysimage to eliminate first-call latency
  • Use multi-stage Docker builds to minimize image size
  • Pin Julia version and Manifest.toml for reproducibility
  • Add request validation and error handling middleware
  • Set up structured logging (JSON format for log aggregators)
  • Load test with realistic payloads before deploying

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.