|
| 1 | +using Test |
| 2 | +using Adapt |
| 3 | +using Lux |
| 4 | +using LuxCUDA |
| 5 | +using JLD2 |
| 6 | +using ConvolutionalNeuralOperators: create_CNOdownsampler, create_CNO |
| 7 | +using ComponentArrays: ComponentArray |
| 8 | +using Optimisers: Adam, ClipGrad, OptimiserChain |
| 9 | +using Optimization |
| 10 | +using Random |
| 11 | +using Zygote: Zygote |
| 12 | +using CUDA |
| 13 | +using CoupledNODE |
| 14 | +using IncompressibleNavierStokes |
| 15 | +using NeuralClosure |
| 16 | +using OrdinaryDiffEqTsit5 |
| 17 | + |
| 18 | +rng = Random.Xoshiro(123) |
| 19 | +T = Float32 |
| 20 | +N = 16 |
| 21 | +nles = 16 |
| 22 | +D = 2 |
| 23 | +ch_ = [2, 2] |
| 24 | +act = [tanh_fast, identity] |
| 25 | +df = [2, 2] |
| 26 | +k_rad = [3, 3] |
| 27 | +bd = [2, 2, 2] |
| 28 | +cutoff = 10 |
| 29 | +batch = 4 |
| 30 | + |
| 31 | +@testset "CoupledNODE integration (CPU)" begin |
| 32 | + # Create the model |
| 33 | + closure, θ_start, st = cno( |
| 34 | + T = T, |
| 35 | + N = N, |
| 36 | + D = D, |
| 37 | + cutoff = cutoff, |
| 38 | + ch_sizes = ch_, |
| 39 | + activations = act, |
| 40 | + down_factors = df, |
| 41 | + k_radii = k_rad, |
| 42 | + bottleneck_depths = bd, |
| 43 | + rng = rng, |
| 44 | + use_cuda = false, |
| 45 | + ) |
| 46 | + |
| 47 | + # Define input tensor and pass through model |
| 48 | + input_tensor = rand(T, N, N, D, batch) |
| 49 | + output = Lux.apply(closure, input_tensor, θ_start, st)[1] |
| 50 | + @test size(output) == size(input_tensor) |
| 51 | + |
| 52 | + # Read conf |
| 53 | + NS = Base.get_extension(CoupledNODE, :NavierStokes) |
| 54 | + conf = NS.read_config("./config.yaml") |
| 55 | + conf["params"]["backend"] = CPU() |
| 56 | + |
| 57 | + # get params |
| 58 | + params = NS.load_params(conf) |
| 59 | + device(x) = x |
| 60 | + |
| 61 | + # Get the setup in the format expected by the CoupledNODE |
| 62 | + function getsetup(; params, nles) |
| 63 | + Setup(; |
| 64 | + x = ntuple(α -> range(params.lims..., nles + 1), params.D), |
| 65 | + params.Re, |
| 66 | + params.backend, |
| 67 | + params.bodyforce, |
| 68 | + params.issteadybodyforce, |
| 69 | + ) |
| 70 | + end |
| 71 | + setup = getsetup(; params, nles) |
| 72 | + psolver = default_psolver(setup) |
| 73 | + setup = [] |
| 74 | + for nl in nles |
| 75 | + x = ntuple(α -> LinRange(T(0.0), T(1.0), nl + 1), params.D) |
| 76 | + push!(setup, Setup(; x = x, Re = params.Re, params.backend)) |
| 77 | + end |
| 78 | + |
| 79 | + # Load data |
| 80 | + function namedtupleload(file) |
| 81 | + dict = load(file) |
| 82 | + k, v = keys(dict), values(dict) |
| 83 | + pairs = @. Symbol(k) => v |
| 84 | + (; pairs...) |
| 85 | + end |
| 86 | + data_train = [] |
| 87 | + data_i = namedtupleload("data_train.jld2") |
| 88 | + push!(data_train, hcat(data_i)) |
| 89 | + |
| 90 | + # Create the io array |
| 91 | + NS = Base.get_extension(CoupledNODE, :NavierStokes) |
| 92 | + io_train = NS.create_io_arrays_priori(data_train, setup) |
| 93 | + |
| 94 | + # Create the dataloader |
| 95 | + θ = device(copy(θ_start)) |
| 96 | + dataloader_prior = NS.create_dataloader_prior( |
| 97 | + io_train[1]; |
| 98 | + batchsize = 4, |
| 99 | + rng = Random.Xoshiro(24), |
| 100 | + device = device, |
| 101 | + ) |
| 102 | + train_data_priori = dataloader_prior() |
| 103 | + |
| 104 | + l0 = CoupledNODE.loss_priori_lux(closure, θ, st, train_data_priori)[1] |
| 105 | + @test isnan(l0) == false |
| 106 | + loss = CoupledNODE.loss_priori_lux |
| 107 | + |
| 108 | + # Final integration test of the entire train interface |
| 109 | + l, trainstate = CoupledNODE.train( |
| 110 | + closure, |
| 111 | + θ, |
| 112 | + st, |
| 113 | + dataloader_prior, |
| 114 | + loss; |
| 115 | + nepochs = 20, |
| 116 | + alg = OptimiserChain(Adam(T(1.0e-3)), ClipGrad(0.1)), |
| 117 | + cpu = true, |
| 118 | + ) |
| 119 | + @test isnan(l) == false |
| 120 | + @test l < l0 |
| 121 | + @test trainstate.step == 20 |
| 122 | + @test any(isnan, trainstate.parameters) == false |
| 123 | + |
| 124 | +end |
| 125 | + |
| 126 | +if !CUDA.functional() |
| 127 | + @test "CUDA not functional, skipping GPU tests" |
| 128 | + return |
| 129 | +end |
| 130 | +CUDA.allowscalar(false) |
| 131 | + |
| 132 | +@testset "CoupledNODE integration (GPU)" begin |
| 133 | + # Create the model |
| 134 | + closure, θ_start, st = cno( |
| 135 | + T = T, |
| 136 | + N = N, |
| 137 | + D = D, |
| 138 | + cutoff = cutoff, |
| 139 | + ch_sizes = ch_, |
| 140 | + activations = act, |
| 141 | + down_factors = df, |
| 142 | + k_radii = k_rad, |
| 143 | + bottleneck_depths = bd, |
| 144 | + rng = rng, |
| 145 | + use_cuda = true, |
| 146 | + ) |
| 147 | + |
| 148 | + # Define input tensor and pass through model |
| 149 | + input_tensor = CUDA.rand(T, N, N, D, batch) |
| 150 | + output = Lux.apply(closure, input_tensor, θ_start, st)[1] |
| 151 | + @test size(output) == size(input_tensor) |
| 152 | + @test isa(output, CuArray) |
| 153 | + |
| 154 | + # Read conf |
| 155 | + NS = Base.get_extension(CoupledNODE, :NavierStokes) |
| 156 | + conf = NS.read_config("./config.yaml") |
| 157 | + conf["params"]["backend"] = CUDABackend() |
| 158 | + |
| 159 | + # get params |
| 160 | + params = NS.load_params(conf) |
| 161 | + device(x) = adapt(params.backend, x) |
| 162 | + |
| 163 | + # Get the setup in the format expected by the CoupledNODE |
| 164 | + function getsetup(; params, nles) |
| 165 | + Setup(; |
| 166 | + x = ntuple(α -> range(params.lims..., nles + 1), params.D), |
| 167 | + params.Re, |
| 168 | + params.backend, |
| 169 | + params.bodyforce, |
| 170 | + params.issteadybodyforce, |
| 171 | + ) |
| 172 | + end |
| 173 | + setup = getsetup(; params, nles) |
| 174 | + psolver = default_psolver(setup) |
| 175 | + setup = [] |
| 176 | + for nl in nles |
| 177 | + x = ntuple(α -> LinRange(T(0.0), T(1.0), nl + 1), params.D) |
| 178 | + push!(setup, Setup(; x = x, Re = params.Re, params.backend)) |
| 179 | + end |
| 180 | + |
| 181 | + # Load data |
| 182 | + function namedtupleload(file) |
| 183 | + dict = load(file) |
| 184 | + k, v = keys(dict), values(dict) |
| 185 | + pairs = @. Symbol(k) => v |
| 186 | + (; pairs...) |
| 187 | + end |
| 188 | + data_train = [] |
| 189 | + data_i = namedtupleload("data_train.jld2") |
| 190 | + push!(data_train, hcat(data_i)) |
| 191 | + |
| 192 | + # Create the io array |
| 193 | + NS = Base.get_extension(CoupledNODE, :NavierStokes) |
| 194 | + io_train = NS.create_io_arrays_priori(data_train, setup) |
| 195 | + |
| 196 | + # Create the dataloader |
| 197 | + θ = device(copy(θ_start)) |
| 198 | + dataloader_prior = NS.create_dataloader_prior( |
| 199 | + io_train[1]; |
| 200 | + batchsize = 4, |
| 201 | + rng = Random.Xoshiro(24), |
| 202 | + device = device, |
| 203 | + ) |
| 204 | + train_data_priori = dataloader_prior() |
| 205 | + @test isa(train_data_priori[1], CuArray) |
| 206 | + @test isa(train_data_priori[2], CuArray) |
| 207 | + |
| 208 | + l0 = CoupledNODE.loss_priori_lux(closure, θ, st, train_data_priori)[1] |
| 209 | + @test isnan(l0) == false |
| 210 | + loss = CoupledNODE.loss_priori_lux |
| 211 | + |
| 212 | + function loss_pb(model, ps, st, (x, y), device = identity) |
| 213 | + y_pred, st_ = Lux.apply(model, x, ps, st)[1:2] |
| 214 | + return sum(abs2, y_pred - y) / sum(abs2, y) |
| 215 | + end |
| 216 | + y, back = Zygote.pullback(loss_pb, closure, θ, st, train_data_priori) |
| 217 | + @test y == l0 |
| 218 | + y_bar = 1 |
| 219 | + _, θ_bar, _, _ = back(y_bar) |
| 220 | + @test size(θ_bar) == size(θ) |
| 221 | + @test sum(θ_bar) !== 0.0 |
| 222 | + |
| 223 | + |
| 224 | + tstate = Lux.Training.TrainState(closure, θ, st, Adam(T(1.0e-3))) |> Lux.gpu_device() |
| 225 | + data = dataloader_prior() |
| 226 | + _, l, _, tstate = |
| 227 | + Lux.Training.single_train_step!(Optimization.AutoZygote(), loss, data, tstate) |> |
| 228 | + Lux.gpu_device() |
| 229 | + @test isnan(l) == false |
| 230 | + @test l < 2 * l0 |
| 231 | + @test tstate.step == 1 |
| 232 | + |
| 233 | + # Final integration test of the entire train interface |
| 234 | + l, trainstate = CoupledNODE.train( |
| 235 | + closure, |
| 236 | + θ, |
| 237 | + st, |
| 238 | + dataloader_prior, |
| 239 | + loss; |
| 240 | + nepochs = 20, |
| 241 | + alg = Adam(T(1.0e-3)), |
| 242 | + ) |
| 243 | + @test isnan(l) == false |
| 244 | + @test l < 2 * l0 |
| 245 | + @test trainstate.step == 20 |
| 246 | + @test any(isnan, trainstate.parameters) == false |
| 247 | + |
| 248 | +end |
0 commit comments