Skip to content

Commit 5009998

Browse files
Merge pull request #56 from SciML/juliaformatter-update
Apply JuliaFormatter to codebase
2 parents 8941c19 + b53c6be commit 5009998

17 files changed

+405
-389
lines changed

benchmarks/benchFourierLayer.jl

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
# Stolen from Flux as well
2-
3-
for n in [2, 20, 200, 2000]
4-
x = randn(Float32, n, 2000, 100)
5-
model = FourierLayer(n, n, 2000, 100, 16)
6-
println("CPU n=$n")
7-
run_benchmark(model, x, cuda=false)
8-
println("CUDA n=$n")
9-
run_benchmark(model, x, cuda=true)
10-
end
1+
# Stolen from Flux as well
2+
3+
for n in [2, 20, 200, 2000]
4+
x = randn(Float32, n, 2000, 100)
5+
model = FourierLayer(n, n, 2000, 100, 16)
6+
println("CPU n=$n")
7+
run_benchmark(model, x, cuda = false)
8+
println("CUDA n=$n")
9+
run_benchmark(model, x, cuda = true)
10+
end

benchmarks/bench_utils.jl

Lines changed: 55 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,55 @@
1-
using BenchmarkTools
2-
using Flux
3-
using OperatorLearning
4-
using CUDA
5-
using Zygote: pullback
6-
7-
# Stolen from Flux
8-
9-
fw(m, x) = m(x)
10-
bw(back) = back(1f0)
11-
fwbw(m, ps, x) = gradient(() -> sum(m(x)), ps)
12-
13-
function run_benchmark(model, x; cuda=true)
14-
15-
if cuda
16-
model = model |> gpu
17-
x = x |> gpu
18-
end
19-
20-
ps = Flux.params(model)
21-
y, back = pullback(() -> sum(model(x)), ps)
22-
23-
24-
if cuda
25-
CUDA.allowscalar(false)
26-
# CUDA.device!(3)
27-
println(" forward")
28-
fw(model, x); GC.gc(); CUDA.reclaim(); #warmup
29-
@btime CUDA.@sync(fw($model, $x)) teardown=(GC.gc(); CUDA.reclaim())
30-
31-
println(" backward")
32-
bw(back); GC.gc(); CUDA.reclaim(); #warmup
33-
@btime CUDA.@sync(bw($back)) teardown=(GC.gc(); CUDA.reclaim())
34-
35-
println(" forw and back")
36-
fwbw(model, ps, x); GC.gc(); CUDA.reclaim(); #warmup
37-
@btime CUDA.@sync(fwbw($model, $ps, $x)) teardown=(GC.gc(); CUDA.reclaim())
38-
else
39-
println(" forward")
40-
fw(model, x) #warmup
41-
@btime fw($model, $x)
42-
43-
println(" backward")
44-
bw(back) #warmup
45-
@btime bw($back)
46-
47-
println(" forw and back")
48-
fwbw(model, ps, x) # warmup
49-
@btime fwbw($model, $ps, $x)
50-
end
51-
end
1+
using BenchmarkTools
2+
using Flux
3+
using OperatorLearning
4+
using CUDA
5+
using Zygote: pullback
6+
7+
# Stolen from Flux
8+
9+
fw(m, x) = m(x)
10+
bw(back) = back(1.0f0)
11+
fwbw(m, ps, x) = gradient(() -> sum(m(x)), ps)
12+
13+
function run_benchmark(model, x; cuda = true)
14+
if cuda
15+
model = model |> gpu
16+
x = x |> gpu
17+
end
18+
19+
ps = Flux.params(model)
20+
y, back = pullback(() -> sum(model(x)), ps)
21+
22+
if cuda
23+
CUDA.allowscalar(false)
24+
# CUDA.device!(3)
25+
println(" forward")
26+
fw(model, x);
27+
GC.gc();
28+
CUDA.reclaim(); #warmup
29+
@btime CUDA.@sync(fw($model, $x)) teardown=(GC.gc(); CUDA.reclaim())
30+
31+
println(" backward")
32+
bw(back);
33+
GC.gc();
34+
CUDA.reclaim(); #warmup
35+
@btime CUDA.@sync(bw($back)) teardown=(GC.gc(); CUDA.reclaim())
36+
37+
println(" forw and back")
38+
fwbw(model, ps, x);
39+
GC.gc();
40+
CUDA.reclaim(); #warmup
41+
@btime CUDA.@sync(fwbw($model, $ps, $x)) teardown=(GC.gc(); CUDA.reclaim())
42+
else
43+
println(" forward")
44+
fw(model, x) #warmup
45+
@btime fw($model, $x)
46+
47+
println(" backward")
48+
bw(back) #warmup
49+
@btime bw($back)
50+
51+
println(" forw and back")
52+
fwbw(model, ps, x) # warmup
53+
@btime fwbw($model, $ps, $x)
54+
end
55+
end

benchmarks/runbenchmarks.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
using OperatorLearning, Flux
2-
3-
versioninfo()
4-
include("bench_utils.jl")
5-
6-
@info "Benchmark FourierLayer"
7-
include("benchFourierLayer.jl")
1+
using OperatorLearning, Flux
2+
3+
versioninfo()
4+
include("bench_utils.jl")
5+
6+
@info "Benchmark FourierLayer"
7+
include("benchFourierLayer.jl")

docs/make.jl

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,28 @@
11
using OperatorLearning
22
using Documenter, DocumenterTools
33

4-
DocMeta.setdocmeta!(OperatorLearning, :DocTestSetup, :(using OperatorLearning); recursive=true)
4+
DocMeta.setdocmeta!(OperatorLearning, :DocTestSetup, :(using OperatorLearning); recursive = true)
55

66
makedocs(;
7-
modules=[OperatorLearning],
8-
authors="Patrick Zimbrod <patrick.zimbrod@gmail.com> and contributors",
9-
repo="https://github.com/pzimbrod/OperatorLearning.jl/blob/{commit}{path}#{line}",
10-
sitename="OperatorLearning.jl",
11-
format=Documenter.HTML(;
12-
prettyurls=get(ENV, "CI", "false") == "true",
13-
canonical="https://pzimbrod.github.io/OperatorLearning.jl",
14-
assets=String[],
7+
modules = [OperatorLearning],
8+
authors = "Patrick Zimbrod <patrick.zimbrod@gmail.com> and contributors",
9+
repo = "https://github.com/pzimbrod/OperatorLearning.jl/blob/{commit}{path}#{line}",
10+
sitename = "OperatorLearning.jl",
11+
format = Documenter.HTML(;
12+
prettyurls = get(ENV, "CI", "false") == "true",
13+
canonical = "https://pzimbrod.github.io/OperatorLearning.jl",
14+
assets = String[]
1515
),
16-
pages=[
16+
pages = [
1717
"Home" => "index.md",
18-
"Examples" =>
19-
["Burgers Equation with FNO" => "examples/burgers_FNO.md",
20-
"Burgers Equation with DeepONet" => "examples/burgers_DeepONet.md"],
18+
"Examples" => ["Burgers Equation with FNO" => "examples/burgers_FNO.md",
19+
"Burgers Equation with DeepONet" => "examples/burgers_DeepONet.md"],
2120
"Frequently Asked Questions" => "faq.md",
22-
"Module Reference" => "reference.md",
23-
],
21+
"Module Reference" => "reference.md"
22+
]
2423
)
2524

2625
deploydocs(;
27-
repo="github.com/pzimbrod/OperatorLearning.jl",
28-
#devbranch="main",
26+
repo = "github.com/pzimbrod/OperatorLearning.jl",
27+
#devbranch="main",
2928
)

docs/src/reference.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
```@autodocs
22
Modules = [OperatorLearning]
3-
```
3+
```

examples/burgers_DeepONet.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,22 @@ subsample = 2^3;
2121
# create the x training array, according to our desired grid size
2222
xtrain = vars["a"][1:1000, 1:subsample:end]' |> device;
2323
# create the x test array
24-
xtest = vars["a"][end-99:end, 1:subsample:end]' |> device;
24+
xtest = vars["a"][(end - 99):end, 1:subsample:end]' |> device;
2525

2626
# Create the y training array
2727
ytrain = vars["u"][1:1000, 1:subsample:end] |> device;
2828
# Create the y test array
29-
ytest = vars["u"][end-99:end, 1:subsample:end] |> device;
29+
ytest = vars["u"][(end - 99):end, 1:subsample:end] |> device;
3030

3131
# The data is missing grid data, so we create it
3232
# `collect` converts data type `range` into an array
33-
grid = collect(range(0, 1, length=1024))' |> device
33+
grid = collect(range(0, 1, length = 1024))' |> device
3434

3535
# Create the DeepONet:
3636
# IC is given on grid of 1024 points, and we solve for a fixed time t in one
3737
# spatial dimension x, making the branch input of size 1024 and trunk size 1
3838
# We choose GeLU activation for both subnets
39-
model = DeepONet((1024,1024,1024),(1,1024,1024),gelu,gelu) |> device
39+
model = DeepONet((1024, 1024, 1024), (1, 1024, 1024), gelu, gelu) |> device
4040

4141
# We use the ADAM optimizer for training
4242
learning_rate = 0.001
@@ -48,12 +48,12 @@ parameters = params(model)
4848
# The loss function
4949
# We can't use the "vanilla" implementation of the mse here since we have
5050
# two distinct inputs to our DeepONet, so we wrap them into a tuple
51-
loss(xtrain,ytrain,sensor) = Flux.Losses.mse(model(xtrain,sensor),ytrain)
51+
loss(xtrain, ytrain, sensor) = Flux.Losses.mse(model(xtrain, sensor), ytrain)
5252

5353
# Define a callback function that gives some output during training
54-
evalcb() = @show(loss(xtest,ytest,grid))
54+
evalcb() = @show(loss(xtest, ytest, grid))
5555
# Print the callback only every 5 seconds
5656
throttled_cb = throttle(evalcb, 5)
5757

5858
# Do the training loop
59-
Flux.@epochs 500 train!(loss, parameters, [(xtrain,ytrain,grid)], opt, cb = evalcb)
59+
Flux.@epochs 500 train!(loss, parameters, [(xtrain, ytrain, grid)], opt, cb = evalcb)

examples/burgers_FNO.jl

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,55 +13,57 @@ subsample = 2^3;
1313
# create the x training array, according to our desired grid size
1414
xtrain = vars["a"][1:1000, 1:subsample:end] |> device;
1515
# create the x test array
16-
xtest = vars["a"][end-99:end, 1:subsample:end] |> device;
16+
xtest = vars["a"][(end - 99):end, 1:subsample:end] |> device;
1717

1818
# Create the y training array
1919
ytrain = vars["u"][1:1000, 1:subsample:end] |> device;
2020
# Create the y test array
21-
ytest = vars["u"][end-99:end, 1:subsample:end] |> device;
21+
ytest = vars["u"][(end - 99):end, 1:subsample:end] |> device;
2222

2323
# The data is missing grid data, so we create it
2424
# `collect` converts data type `range` into an array
25-
grid = collect(range(0, 1, length=length(xtrain[1,:]))) |> device
25+
grid = collect(range(0, 1, length = length(xtrain[1, :]))) |> device
2626

2727
# Merge the created grid with the data
2828
# Output has the dims: batch x grid points x 2 (a(x), x)
2929
# First, reshape the data to a 3D tensor,
3030
# Then, create a 3D tensor from the synthetic grid data
3131
# and concatenate them along the newly created 3rd dim
32-
xtrain = cat(reshape(xtrain,(1000,1024,1)),
33-
reshape(repeat(grid,1000),(1000,1024,1));
34-
dims=3) |> device
35-
ytrain = cat(reshape(ytrain,(1000,1024,1)),
36-
reshape(repeat(grid,1000),(1000,1024,1));
37-
dims=3) |> device
32+
xtrain = cat(reshape(xtrain, (1000, 1024, 1)),
33+
reshape(repeat(grid, 1000), (1000, 1024, 1));
34+
dims = 3) |> device
35+
ytrain = cat(reshape(ytrain, (1000, 1024, 1)),
36+
reshape(repeat(grid, 1000), (1000, 1024, 1));
37+
dims = 3) |> device
3838
# Same treatment with the test data
39-
xtest = cat(reshape(xtest,(100,1024,1)),
40-
reshape(repeat(grid,100),(100,1024,1));
41-
dims=3) |> device
42-
ytest = cat(reshape(ytest,(100,1024,1)),
43-
reshape(repeat(grid,100),(100,1024,1));
44-
dims=3) |> device
39+
xtest = cat(reshape(xtest, (100, 1024, 1)),
40+
reshape(repeat(grid, 100), (100, 1024, 1));
41+
dims = 3) |> device
42+
ytest = cat(reshape(ytest, (100, 1024, 1)),
43+
reshape(repeat(grid, 100), (100, 1024, 1));
44+
dims = 3) |> device
4545

4646
# Our net wants the input in the form (2,grid,batch), though,
4747
# So we permute
48-
xtrain, xtest = permutedims(xtrain,(3,2,1)), permutedims(xtest,(3,2,1)) |> device
49-
ytrain, ytest = permutedims(ytrain,(3,2,1)), permutedims(ytest,(3,2,1)) |> device
48+
xtrain, xtest = permutedims(xtrain, (3, 2, 1)), permutedims(xtest, (3, 2, 1)) |> device
49+
ytrain, ytest = permutedims(ytrain, (3, 2, 1)), permutedims(ytest, (3, 2, 1)) |> device
5050

5151
# Pass the data to the Flux DataLoader and give it a batch of 20
52-
train_loader = Flux.Data.DataLoader((xtrain, ytrain), batchsize=20, shuffle=true) |> device
53-
test_loader = Flux.Data.DataLoader((xtest, ytest), batchsize=20, shuffle=false) |> device
52+
train_loader = Flux.Data.DataLoader((xtrain, ytrain), batchsize = 20, shuffle = true) |>
53+
device
54+
test_loader = Flux.Data.DataLoader((xtest, ytest), batchsize = 20, shuffle = false) |>
55+
device
5456

5557
# Set up the Fourier Layer
5658
# 128 in- and outputs, batch size 20 as given above, grid size 1024
5759
# 16 modes to keep, σ activation on the gpu
58-
layer = FourierLayer(128,128,1024,16,gelu,bias_fourier=false) |> device
60+
layer = FourierLayer(128, 128, 1024, 16, gelu, bias_fourier = false) |> device
5961

6062
# The whole architecture
6163
# linear transform into the latent space, 4 Fourier Layers,
6264
# then transform it back
63-
model = Chain(Dense(2,128;bias=false), layer, layer, layer, layer,
64-
Dense(128,2;bias=false)) |> device
65+
model = Chain(Dense(2, 128; bias = false), layer, layer, layer, layer,
66+
Dense(128, 2; bias = false)) |> device
6567

6668
# We use the ADAM optimizer for training
6769
learning_rate = 0.001
@@ -71,21 +73,21 @@ opt = ADAM(learning_rate)
7173
parameters = params(model)
7274

7375
# The loss function
74-
loss(x,y) = Flux.Losses.mse(model(x),y)
76+
loss(x, y) = Flux.Losses.mse(model(x), y)
7577

7678
# Define a callback function that gives some output during training
77-
evalcb() = @show(loss(xtest,ytest))
79+
evalcb() = @show(loss(xtest, ytest))
7880
# Print the callback only every 5 seconds,
7981
throttled_cb = throttle(evalcb, 5)
8082

8183
# Do the training loop
8284
Flux.@epochs 500 train!(loss, parameters, train_loader, opt, cb = throttled_cb)
8385

8486
# Accuracy metrics
85-
val_loader = Flux.Data.DataLoader((xtest, ytest), batchsize=1, shuffle=false) |> device
87+
val_loader = Flux.Data.DataLoader((xtest, ytest), batchsize = 1, shuffle = false) |> device
8688
loss = 0.0 |> device
8789

88-
for (x,y) in val_loader
90+
for (x, y) in val_loader
8991
= model(x)
90-
loss += Flux.Losses.mse(ŷ,y)
92+
loss += Flux.Losses.mse(ŷ, y)
9193
end

src/ComplexWeights.jl

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ cglorot_uniform([rng=GLOBAL_RNG], dims...)
44
A modification of the `glorot_uniform` function provided by `Flux` to accommodate Complex numbers.
55
This is necessary since the parameters of the global convolution operator in the Fourier Layer generally has complex weights.
66
"""
7-
cglorot_uniform(rng::AbstractRNG, dims...) = (rand(rng, ComplexF32, dims...) .- 0.5f0) .* sqrt(24.0f0 / sum(nfan(dims...)))
7+
function cglorot_uniform(rng::AbstractRNG, dims...)
8+
(rand(rng, ComplexF32, dims...) .- 0.5f0) .* sqrt(24.0f0 / sum(nfan(dims...)))
9+
end
810
cglorot_uniform(dims...) = cglorot_uniform(Random.GLOBAL_RNG, dims...)
911
cglorot_uniform(rng::AbstractRNG) = (dims...) -> cglorot_uniform(rng, dims...)
1012

@@ -14,6 +16,8 @@ cglorot_normal([rng=GLOBAL_RNG], dims...)
1416
A modification of the `glorot_normal` function provided by `Flux` to accommodate Complex numbers.
1517
This is necessary since the parameters of the global convolution operator in the Fourier Layer generally has complex weights.
1618
"""
17-
cglorot_normal(rng::AbstractRNG, dims...) = randn(rng, ComplexF32, dims...) .* sqrt(2.0f0 / sum(nfan(dims...)))
19+
function cglorot_normal(rng::AbstractRNG, dims...)
20+
randn(rng, ComplexF32, dims...) .* sqrt(2.0f0 / sum(nfan(dims...)))
21+
end
1822
cglorot_normal(dims...) = cglorot_normal(Random.GLOBAL_RNG, dims...)
19-
cglorot_normal(rng::AbstractRNG) = (dims...) -> cglorot_normal(rng, dims...)
23+
cglorot_normal(rng::AbstractRNG) = (dims...) -> cglorot_normal(rng, dims...)

0 commit comments

Comments
 (0)