|
1 | 1 |
|
2 |
| -println("RNN") |
3 |
| -for n in [2, 20, 200, 1000], T in [1, 8, 16, 64] |
4 |
| - x = [randn(Float32, n, n) for t in 1:T] |
5 |
| - model = RNN(n, n) |
6 |
| - println("CPU n=$n, t=$T") |
7 |
| - run_benchmark(model, x, cuda=false) |
8 |
| - println("CUDA n=$n, t=$T") |
9 |
| - try |
10 |
| - run_benchmark(model, x, cuda=true) |
11 |
| - catch ex |
12 |
| - @show typeof(ex) |
13 |
| - if ex isa OutOfGPUMemoryError |
14 |
| - @warn "Not enough GPU memory to run test" |
15 |
| - else |
16 |
| - rethrow(ex) |
17 |
| - end |
18 |
| - end |
| 2 | + |
| 3 | +struct RNNWrapper{T} |
| 4 | + rnn::T |
| 5 | +end |
| 6 | +Flux.@functor RNNWrapper |
| 7 | + |
| 8 | +# Need to specialize for RNNWrapper. |
| 9 | +fw(r::RNNWrapper, X::Vector{<:AbstractArray}) = begin |
| 10 | + Flux.reset!(r.rnn) |
| 11 | + [r.rnn(x) for x in X] |
| 12 | +end |
| 13 | + |
| 14 | +fw(r::RNNWrapper, X) = begin |
| 15 | + Flux.reset!(r.rnn) |
| 16 | + r.rnn(X) |
| 17 | +end |
| 18 | + |
| 19 | +fwbw(r::RNNWrapper, ps, X::Vector{<:AbstractArray}) = gradient(ps) do |
| 20 | + y = fw(r, X) |
| 21 | + sum(sum(y)) |
| 22 | +end |
| 23 | + |
| 24 | +pb(r::RNNWrapper, ps, X::Vector{<:AbstractArray}) = pullback(ps) do |
| 25 | + y = fw(r, X) |
| 26 | + sum(sum(y)) |
19 | 27 | end
|
20 | 28 |
|
21 |
| -println("RNN-3d") |
22 |
| -for n in [2, 20, 200, 1000], T in [1, 8, 16, 64] |
23 |
| - x = randn(Float32, n, n, T) |
24 |
| - model = RNN(n, n) |
25 |
| - println("CPU n=$n, t=$T") |
26 |
| - run_benchmark(model, x, cuda=false) |
27 |
| - println("CUDA n=$n, t=$T") |
28 |
| - try |
| 29 | +function rnn_benchmark_sweep(data_creator::Function, rnn_type) |
| 30 | + for n in [2, 20, 200, 1000], ts in [1, 4, 16, 64] |
| 31 | + x, x_n = data_creator(n, ts) |
| 32 | + model = RNNWrapper(rnn_type(n, n)) |
| 33 | + |
| 34 | + println("$rnn_type $x_n CPU n=$n, ts=$ts") |
| 35 | + run_benchmark(model, x, cuda=false) |
| 36 | + |
| 37 | + println("$rnn_type $x_n CUDA n=$n, ts=$ts") |
| 38 | + try |
29 | 39 | run_benchmark(model, x, cuda=true)
|
30 |
| - catch ex |
| 40 | + catch ex |
31 | 41 | @show typeof(ex)
|
32 | 42 | if ex isa OutOfGPUMemoryError
|
33 |
| - @warn "Not enough GPU memory to run test" |
| 43 | + @warn "Not enough GPU memory to run test" |
34 | 44 | else
|
35 |
| - rethrow(ex) |
| 45 | + rethrow(ex) |
36 | 46 | end
|
37 |
| - end |
| 47 | + end |
| 48 | + end |
38 | 49 | end
|
39 | 50 |
|
| 51 | +for rnn_type in [Flux.RNN, Flux.GRU, Flux.LSTM] |
| 52 | + rnn_benchmark_sweep(rnn_type) do n, ts |
| 53 | + [randn(Float32, n, n) for _ in 1:ts], "Vec" |
| 54 | + end |
| 55 | +end |
40 | 56 |
|
| 57 | +for rnn_type in [Flux.RNN, Flux.GRU, Flux.LSTM] |
| 58 | + rnn_benchmark_sweep(rnn_type) do n, ts |
| 59 | + randn(Float32, n, n, ts), "Block" |
| 60 | + end |
| 61 | +end |
41 | 62 |
|
0 commit comments