|
| 1 | +# Taken from https://github.com/vpuri3/KolmogorovArnold.jl/blob/0fc349813be15982365173bce0e9bf3a814a342a/examples/eg3.jl |
| 2 | +using KolmogorovArnold |
| 3 | +using Comonicon, BenchmarkTools, JSON3 |
| 4 | +using Random, LinearAlgebra |
| 5 | +using Enzyme, Zygote, Lux |
| 6 | +using OrderedCollections |
| 7 | + |
| 8 | +# configure BLAS |
| 9 | +ncores = min(Sys.CPU_THREADS, length(Sys.cpu_info())) |
| 10 | +BLAS.set_num_threads(ncores) |
| 11 | + |
| 12 | +# configure CUDA |
| 13 | +using LuxCUDA |
| 14 | +CUDA.allowscalar(false) |
| 15 | + |
| 16 | +# configure Reactant |
| 17 | +using Reactant |
| 18 | +Reactant.set_default_backend("gpu") |
| 19 | + |
| 20 | +rng = Random.default_rng() |
| 21 | +Random.seed!(rng, 0) |
| 22 | + |
| 23 | +function toy_loss_function(model, ps, st, x, y) |
| 24 | + pred, _ = model(x, ps, st) |
| 25 | + return MSELoss()(pred, y) |
| 26 | +end |
| 27 | + |
| 28 | +function setup_models(; kan_width::Int=128, grid_size::Int=32) |
| 29 | + wK, G = kan_width, grid_size |
| 30 | + |
| 31 | + basis_func = rbf # rbf, rswaf |
| 32 | + normalizer = softsign # sigmoid(_fast), tanh(_fast), softsign |
| 33 | + |
| 34 | + kan1 = Chain( |
| 35 | + KDense(1, wK, G; use_base_act=true, basis_func, normalizer), |
| 36 | + KDense(wK, wK, G; use_base_act=true, basis_func, normalizer), |
| 37 | + KDense(wK, 1, G; use_base_act=true, basis_func, normalizer), |
| 38 | + ) |
| 39 | + |
| 40 | + kan2 = Chain( |
| 41 | + KDense(1, wK, G; use_base_act=false, basis_func, normalizer), |
| 42 | + KDense(wK, wK, G; use_base_act=false, basis_func, normalizer), |
| 43 | + KDense(wK, 1, G; use_base_act=false, basis_func, normalizer), |
| 44 | + ) |
| 45 | + |
| 46 | + return [("kan_base_act", kan1), ("kan_no_base_act", kan2)] |
| 47 | +end |
| 48 | + |
| 49 | +function run_cuda_benchmarks(; batch_size::Int=128, kwargs...) |
| 50 | + dev = gpu_device(; force=true) |
| 51 | + |
| 52 | + x = rand32(rng, 1, batch_size) |
| 53 | + y = x .^ 2 |
| 54 | + |
| 55 | + models = setup_models(; kwargs...) |
| 56 | + timings = OrderedDict{String,OrderedDict{String,Float64}}() |
| 57 | + |
| 58 | + for (name, model) in models |
| 59 | + println("\nCUDA Benchmarking: $name") |
| 60 | + |
| 61 | + ps, st = Lux.setup(rng, model) |> dev |
| 62 | + x_cu = x |> dev |
| 63 | + y_cu = y |> dev |
| 64 | + |
| 65 | + println("Param count: $(Lux.parameterlength(ps))") |
| 66 | + println("State count: $(Lux.statelength(st))") |
| 67 | + |
| 68 | + # Forward pass timing |
| 69 | + fwd_time = @belapsed begin |
| 70 | + pred, _ = $(model)($(x_cu), $(ps), $(Lux.testmode(st))) |
| 71 | + CUDA.synchronize() |
| 72 | + end setup = begin |
| 73 | + GC.gc(true) |
| 74 | + CUDA.reclaim() |
| 75 | + end |
| 76 | + |
| 77 | + # Backward pass timing (using Zygote) |
| 78 | + fn = (ps, x) -> toy_loss_function(model, ps, st, x, y_cu) |
| 79 | + |
| 80 | + bwd_time = @belapsed begin |
| 81 | + Zygote.gradient($(fn), $(ps), $(x_cu)) |
| 82 | + CUDA.synchronize() |
| 83 | + end setup = begin |
| 84 | + GC.gc(true) |
| 85 | + CUDA.reclaim() |
| 86 | + end |
| 87 | + |
| 88 | + timings[name] = OrderedDict{String,Float64}( |
| 89 | + "forward" => fwd_time, "backward" => bwd_time |
| 90 | + ) |
| 91 | + |
| 92 | + display(timings[name]) |
| 93 | + end |
| 94 | + |
| 95 | + return timings |
| 96 | +end |
| 97 | + |
| 98 | +function run_xla_benchmarks(; kwargs...) |
| 99 | + return run_reactant_benchmarks(; |
| 100 | + kwargs..., compile_options=Reactant.DefaultXLACompileOptions() |
| 101 | + ) |
| 102 | +end |
| 103 | + |
| 104 | +function run_reactant_benchmarks(; |
| 105 | + batch_size::Int=128, |
| 106 | + compile_options=Reactant.CompileOptions(; optimization_passes=:all), |
| 107 | + kwargs..., |
| 108 | +) |
| 109 | + dev = reactant_device(; force=true) |
| 110 | + |
| 111 | + x = rand32(rng, 1, batch_size) |
| 112 | + y = x .^ 2 |
| 113 | + |
| 114 | + models = setup_models(; kwargs...) |
| 115 | + timings = OrderedDict{String,OrderedDict{String,Float64}}() |
| 116 | + |
| 117 | + for (name, model) in models |
| 118 | + println("\nReactant Benchmarking: $name") |
| 119 | + |
| 120 | + ps, st = Lux.setup(rng, model) |> dev |
| 121 | + x_ra = x |> dev |
| 122 | + y_ra = y |> dev |
| 123 | + |
| 124 | + println("Param count: $(Lux.parameterlength(ps))") |
| 125 | + println("State count: $(Lux.statelength(st))") |
| 126 | + |
| 127 | + # Forward pass timing |
| 128 | + fwd_time_result = Reactant.Profiler.profile_with_xprof( |
| 129 | + Lux.apply, |
| 130 | + model, |
| 131 | + x_ra, |
| 132 | + ps, |
| 133 | + Lux.testmode(st); |
| 134 | + nrepeat=10, |
| 135 | + warmup=1, |
| 136 | + compile_options, |
| 137 | + ) |
| 138 | + fwd_time = fwd_time_result.profiling_result.runtime_ns / 1e9 |
| 139 | + |
| 140 | + # Backward pass timing |
| 141 | + bwd_time_result = Reactant.Profiler.profile_with_xprof( |
| 142 | + Enzyme.gradient, |
| 143 | + Reverse, |
| 144 | + toy_loss_function, |
| 145 | + Const(model), |
| 146 | + ps, |
| 147 | + Const(st), |
| 148 | + Const(x_ra), |
| 149 | + Const(y_ra); |
| 150 | + nrepeat=10, |
| 151 | + warmup=1, |
| 152 | + compile_options, |
| 153 | + ) |
| 154 | + bwd_time = bwd_time_result.profiling_result.runtime_ns / 1e9 |
| 155 | + |
| 156 | + timings[name] = OrderedDict{String,Float64}( |
| 157 | + "forward" => fwd_time, "backward" => bwd_time |
| 158 | + ) |
| 159 | + |
| 160 | + display(timings[name]) |
| 161 | + end |
| 162 | + |
| 163 | + return timings |
| 164 | +end |
| 165 | + |
| 166 | +Comonicon.@main function main(; |
| 167 | + backend::String="all", batch_size::Int=1024, kan_width::Int=128, grid_size::Int=32 |
| 168 | +) |
| 169 | + results_path = joinpath(@__DIR__, "../results/kan/") |
| 170 | + mkpath(results_path) |
| 171 | + |
| 172 | + if backend in ("cuda", "all") |
| 173 | + println("\n" * "="^50) |
| 174 | + println("Running CUDA benchmarks...") |
| 175 | + println("="^50) |
| 176 | + |
| 177 | + cuda_timings = run_cuda_benchmarks(; batch_size, kan_width, grid_size) |
| 178 | + |
| 179 | + open(joinpath(results_path, "cudajl.json"), "w") do io |
| 180 | + JSON3.write(io, cuda_timings) |
| 181 | + end |
| 182 | + |
| 183 | + println("\nCUDA Results:") |
| 184 | + display(cuda_timings) |
| 185 | + end |
| 186 | + |
| 187 | + if backend in ("reactant", "all") |
| 188 | + println("\n" * "="^50) |
| 189 | + println("Running Reactant benchmarks...") |
| 190 | + println("="^50) |
| 191 | + |
| 192 | + reactant_timings = run_reactant_benchmarks(; batch_size, kan_width, grid_size) |
| 193 | + |
| 194 | + open(joinpath(results_path, "reactant.json"), "w") do io |
| 195 | + JSON3.write(io, reactant_timings) |
| 196 | + end |
| 197 | + |
| 198 | + println("\nReactant Results:") |
| 199 | + display(reactant_timings) |
| 200 | + end |
| 201 | + |
| 202 | + if backend in ("xla", "all") |
| 203 | + println("\n" * "="^50) |
| 204 | + println("Running XLA benchmarks...") |
| 205 | + println("="^50) |
| 206 | + |
| 207 | + xla_timings = run_xla_benchmarks(; batch_size, kan_width, grid_size) |
| 208 | + |
| 209 | + open(joinpath(results_path, "xla.json"), "w") do io |
| 210 | + JSON3.write(io, xla_timings) |
| 211 | + end |
| 212 | + |
| 213 | + println("\nXLA Results:") |
| 214 | + display(xla_timings) |
| 215 | + end |
| 216 | + |
| 217 | + return nothing |
| 218 | +end |
0 commit comments