Skip to content

MNIST Classification with SimpleChains

SimpleChains.jl is an excellent framework for training small neural networks. In this tutorial we will demonstrate how to use the same API as Lux.jl to train a model using SimpleChains.jl. We will use the tutorial from SimpleChains.jl as a reference.

Package Imports

julia
using Lux, MLUtils, Optimisers, Zygote, OneHotArrays, Random, Statistics, Printf, Reactant
using MLDatasets: MNIST
using SimpleChains: SimpleChains

Reactant.set_default_backend("cpu")
Precompiling Lux...
   3507.2 ms  ✓ ForwardDiff
    964.7 ms  ✓ ForwardDiff → ForwardDiffStaticArraysExt
    887.4 ms  ✓ NNlib → NNlibForwardDiffExt
   5795.2 ms  ✓ LuxLib
   9135.2 ms  ✓ Lux
  5 dependencies successfully precompiled in 20 seconds. 100 already precompiled.
Precompiling LuxMLUtilsExt...
   2038.3 ms  ✓ Lux → LuxMLUtilsExt
  1 dependency successfully precompiled in 2 seconds. 164 already precompiled.
Precompiling Zygote...
    370.6 ms  ✓ RealDot
    624.3 ms  ✓ SparseInverseSubset
    959.3 ms  ✓ ZygoteRules
   2081.3 ms  ✓ IRTools
   5389.8 ms  ✓ ChainRules
  33613.9 ms  ✓ Zygote
  6 dependencies successfully precompiled in 40 seconds. 59 already precompiled.
Precompiling ArrayInterfaceChainRulesExt...
    760.0 ms  ✓ ArrayInterface → ArrayInterfaceChainRulesExt
  1 dependency successfully precompiled in 1 seconds. 40 already precompiled.
Precompiling MLDataDevicesChainRulesExt...
    808.9 ms  ✓ MLDataDevices → MLDataDevicesChainRulesExt
  1 dependency successfully precompiled in 1 seconds. 41 already precompiled.
Precompiling MLDataDevicesZygoteExt...
   1531.4 ms  ✓ MLDataDevices → MLDataDevicesZygoteExt
  1 dependency successfully precompiled in 2 seconds. 71 already precompiled.
Precompiling LuxZygoteExt...
   2566.8 ms  ✓ Lux → LuxZygoteExt
  1 dependency successfully precompiled in 3 seconds. 143 already precompiled.
Precompiling LuxLibEnzymeExt...
   1301.6 ms  ✓ LuxLib → LuxLibEnzymeExt
  1 dependency successfully precompiled in 2 seconds. 132 already precompiled.
Precompiling LuxEnzymeExt...
   7620.2 ms  ✓ Lux → LuxEnzymeExt
  1 dependency successfully precompiled in 8 seconds. 148 already precompiled.
Precompiling LuxReactantExt...
  13850.7 ms  ✓ Lux → LuxReactantExt
  1 dependency successfully precompiled in 14 seconds. 179 already precompiled.
Precompiling SimpleChains...
    345.7 ms  ✓ UnPack
    799.3 ms  ✓ HostCPUFeatures
   7660.4 ms  ✓ VectorizationBase
   1010.9 ms  ✓ SLEEFPirates
   1240.7 ms  ✓ VectorizedRNG
    815.6 ms  ✓ VectorizedRNG → VectorizedRNGStaticArraysExt
  28143.9 ms  ✓ LoopVectorization
   1023.9 ms  ✓ LoopVectorization → SpecialFunctionsExt
   1213.4 ms  ✓ LoopVectorization → ForwardDiffExt
   6460.8 ms  ✓ SimpleChains
  10 dependencies successfully precompiled in 45 seconds. 59 already precompiled.
Precompiling LuxLibSLEEFPiratesExt...
   2342.8 ms  ✓ LuxLib → LuxLibSLEEFPiratesExt
  1 dependency successfully precompiled in 3 seconds. 93 already precompiled.
Precompiling LuxLibLoopVectorizationExt...
   4596.7 ms  ✓ LuxLib → LuxLibLoopVectorizationExt
  1 dependency successfully precompiled in 5 seconds. 101 already precompiled.
Precompiling LuxSimpleChainsExt...
   1880.3 ms  ✓ Lux → LuxSimpleChainsExt
  1 dependency successfully precompiled in 2 seconds. 122 already precompiled.
2025-04-16 03:35:02.886167: I external/xla/xla/service/service.cc:152] XLA service 0xc17b7d0 initialized for platform CUDA (this does not guarantee that XLA will be used). Devices:
2025-04-16 03:35:02.886552: I external/xla/xla/service/service.cc:160]   StreamExecutor device (0): NVIDIA A100-PCIE-40GB MIG 1g.5gb, Compute Capability 8.0
WARNING: All log messages before absl::InitializeLog() is called are written to STDERR
I0000 00:00:1744774502.887511 4123830 se_gpu_pjrt_client.cc:1040] Using BFC allocator.
I0000 00:00:1744774502.887618 4123830 gpu_helpers.cc:136] XLA backend allocating 3825205248 bytes on device 0 for BFCAllocator.
I0000 00:00:1744774502.887662 4123830 gpu_helpers.cc:177] XLA backend will use up to 1275068416 bytes on device 0 for CollectiveBFCAllocator.
I0000 00:00:1744774502.901992 4123830 cuda_dnn.cc:529] Loaded cuDNN version 90400

Loading MNIST

julia
function loadmnist(batchsize, train_split)
    # Load MNIST
    N = parse(Bool, get(ENV, "CI", "false")) ? 1500 : nothing
    dataset = MNIST(; split=:train)
    if N !== nothing
        imgs = dataset.features[:, :, 1:N]
        labels_raw = dataset.targets[1:N]
    else
        imgs = dataset.features
        labels_raw = dataset.targets
    end

    # Process images into (H, W, C, BS) batches
    x_data = Float32.(reshape(imgs, size(imgs, 1), size(imgs, 2), 1, size(imgs, 3)))
    y_data = onehotbatch(labels_raw, 0:9)
    (x_train, y_train), (x_test, y_test) = splitobs((x_data, y_data); at=train_split)

    return (
        # Use DataLoader to automatically minibatch and shuffle the data
        DataLoader(collect.((x_train, y_train)); batchsize, shuffle=true, partial=false),
        # Don't shuffle the test data
        DataLoader(collect.((x_test, y_test)); batchsize, shuffle=false, partial=false),
    )
end
loadmnist (generic function with 1 method)

Define the Model

julia
lux_model = Chain(
    Conv((5, 5), 1 => 6, relu),
    MaxPool((2, 2)),
    Conv((5, 5), 6 => 16, relu),
    MaxPool((2, 2)),
    FlattenLayer(3),
    Chain(Dense(256 => 128, relu), Dense(128 => 84, relu), Dense(84 => 10)),
)
Chain(
    layer_1 = Conv((5, 5), 1 => 6, relu),  # 156 parameters
    layer_2 = MaxPool((2, 2)),
    layer_3 = Conv((5, 5), 6 => 16, relu),  # 2_416 parameters
    layer_4 = MaxPool((2, 2)),
    layer_5 = Lux.FlattenLayer{Static.StaticInt{3}}(static(3)),
    layer_6 = Chain(
        layer_1 = Dense(256 => 128, relu),  # 32_896 parameters
        layer_2 = Dense(128 => 84, relu),  # 10_836 parameters
        layer_3 = Dense(84 => 10),      # 850 parameters
    ),
)         # Total: 47_154 parameters,
          #        plus 0 states.

We now need to convert the lux_model to SimpleChains.jl. We need to do this by defining the ToSimpleChainsAdaptor and providing the input dimensions.

julia
adaptor = ToSimpleChainsAdaptor((28, 28, 1))
simple_chains_model = adaptor(lux_model)
SimpleChainsLayer(
    Chain(
        layer_1 = Conv((5, 5), 1 => 6, relu),  # 156 parameters
        layer_2 = MaxPool((2, 2)),
        layer_3 = Conv((5, 5), 6 => 16, relu),  # 2_416 parameters
        layer_4 = MaxPool((2, 2)),
        layer_5 = Lux.FlattenLayer{Static.StaticInt{3}}(static(3)),
        layer_6 = Chain(
            layer_1 = Dense(256 => 128, relu),  # 32_896 parameters
            layer_2 = Dense(128 => 84, relu),  # 10_836 parameters
            layer_3 = Dense(84 => 10),  # 850 parameters
        ),
    ),
)         # Total: 47_154 parameters,
          #        plus 0 states.

Helper Functions

julia
const lossfn = CrossEntropyLoss(; logits=Val(true))

function accuracy(model, ps, st, dataloader)
    total_correct, total = 0, 0
    st = Lux.testmode(st)
    for (x, y) in dataloader
        target_class = onecold(Array(y))
        predicted_class = onecold(Array(first(model(x, ps, st))))
        total_correct += sum(target_class .== predicted_class)
        total += length(target_class)
    end
    return total_correct / total
end
accuracy (generic function with 1 method)

Define the Training Loop

julia
function train(model, dev=cpu_device(); rng=Random.default_rng(), kwargs...)
    train_dataloader, test_dataloader = dev(loadmnist(128, 0.9))
    ps, st = dev(Lux.setup(rng, model))

    vjp = dev isa ReactantDevice ? AutoEnzyme() : AutoZygote()

    train_state = Training.TrainState(model, ps, st, Adam(3.0f-4))

    if dev isa ReactantDevice
        x_ra = first(test_dataloader)[1]
        model_compiled = @compile model(x_ra, ps, Lux.testmode(st))
    else
        model_compiled = model
    end

    ### Lets train the model
    nepochs = 10
    tr_acc, te_acc = 0.0, 0.0
    for epoch in 1:nepochs
        stime = time()
        for (x, y) in train_dataloader
            _, _, _, train_state = Training.single_train_step!(
                vjp, lossfn, (x, y), train_state
            )
        end
        ttime = time() - stime

        tr_acc =
            accuracy(
                model_compiled, train_state.parameters, train_state.states, train_dataloader
            ) * 100
        te_acc =
            accuracy(
                model_compiled, train_state.parameters, train_state.states, test_dataloader
            ) * 100

        @printf "[%2d/%2d] \t Time %.2fs \t Training Accuracy: %.2f%% \t Test Accuracy: \
                 %.2f%%\n" epoch nepochs ttime tr_acc te_acc
    end

    return tr_acc, te_acc
end
train (generic function with 2 methods)

Finally Training the Model

First we will train the Lux model

julia
tr_acc, te_acc = train(lux_model, reactant_device())
[ 1/10] 	 Time 248.90s 	 Training Accuracy: 12.97% 	 Test Accuracy: 7.81%
[ 2/10] 	 Time 0.22s 	 Training Accuracy: 19.69% 	 Test Accuracy: 17.19%
[ 3/10] 	 Time 0.19s 	 Training Accuracy: 35.31% 	 Test Accuracy: 26.56%
[ 4/10] 	 Time 0.20s 	 Training Accuracy: 46.64% 	 Test Accuracy: 43.75%
[ 5/10] 	 Time 0.22s 	 Training Accuracy: 55.31% 	 Test Accuracy: 50.00%
[ 6/10] 	 Time 0.20s 	 Training Accuracy: 64.06% 	 Test Accuracy: 58.59%
[ 7/10] 	 Time 0.21s 	 Training Accuracy: 68.28% 	 Test Accuracy: 67.97%
[ 8/10] 	 Time 0.20s 	 Training Accuracy: 72.34% 	 Test Accuracy: 68.75%
[ 9/10] 	 Time 0.24s 	 Training Accuracy: 76.64% 	 Test Accuracy: 71.09%
[10/10] 	 Time 0.21s 	 Training Accuracy: 78.59% 	 Test Accuracy: 74.22%

Now we will train the SimpleChains model

julia
tr_acc, te_acc = train(simple_chains_model)
[ 1/10] 	 Time 871.91s 	 Training Accuracy: 16.56% 	 Test Accuracy: 17.19%
[ 2/10] 	 Time 12.09s 	 Training Accuracy: 55.70% 	 Test Accuracy: 51.56%
[ 3/10] 	 Time 12.16s 	 Training Accuracy: 67.11% 	 Test Accuracy: 64.84%
[ 4/10] 	 Time 12.12s 	 Training Accuracy: 73.83% 	 Test Accuracy: 72.66%
[ 5/10] 	 Time 12.13s 	 Training Accuracy: 77.11% 	 Test Accuracy: 77.34%
[ 6/10] 	 Time 12.10s 	 Training Accuracy: 80.39% 	 Test Accuracy: 75.00%
[ 7/10] 	 Time 12.09s 	 Training Accuracy: 81.02% 	 Test Accuracy: 82.81%
[ 8/10] 	 Time 12.07s 	 Training Accuracy: 84.61% 	 Test Accuracy: 84.38%
[ 9/10] 	 Time 12.07s 	 Training Accuracy: 86.80% 	 Test Accuracy: 81.25%
[10/10] 	 Time 12.07s 	 Training Accuracy: 87.58% 	 Test Accuracy: 89.06%

On my local machine we see a 3-4x speedup when using SimpleChains.jl. The conditions of the server this documentation is being built on is not ideal for CPU benchmarking hence, the speedup may not be as significant and even there might be regressions.

Appendix

julia
using InteractiveUtils
InteractiveUtils.versioninfo()

if @isdefined(MLDataDevices)
    if @isdefined(CUDA) && MLDataDevices.functional(CUDADevice)
        println()
        CUDA.versioninfo()
    end

    if @isdefined(AMDGPU) && MLDataDevices.functional(AMDGPUDevice)
        println()
        AMDGPU.versioninfo()
    end
end
Julia Version 1.11.5
Commit 760b2e5b739 (2025-04-14 06:53 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 48 × AMD EPYC 7402 24-Core Processor
  WORD_SIZE: 64
  LLVM: libLLVM-16.0.6 (ORCJIT, znver2)
Threads: 48 default, 0 interactive, 24 GC (on 2 virtual cores)
Environment:
  JULIA_CPU_THREADS = 2
  LD_LIBRARY_PATH = /usr/local/nvidia/lib:/usr/local/nvidia/lib64
  JULIA_PKG_SERVER = 
  JULIA_NUM_THREADS = 48
  JULIA_CUDA_HARD_MEMORY_LIMIT = 100%
  JULIA_PKG_PRECOMPILE_AUTO = 0
  JULIA_DEBUG = Literate
  JULIA_DEPOT_PATH = /root/.cache/julia-buildkite-plugin/depots/01872db4-8c79-43af-ab7d-12abac4f24f6

This page was generated using Literate.jl.