forked from SciML/DiffEqFlux.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
neural_de_gpu.jl
169 lines (139 loc) · 5.97 KB
/
neural_de_gpu.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
using OrdinaryDiffEq, StochasticDiffEq, Flux, DiffEqFlux
using Test
using CuArrays
x = Float32[2.; 0.]|>gpu
tspan = Float32.((0.0f0,25.0f0))
dudt = Chain(Dense(2,50,tanh),Dense(50,2))|>gpu
neural_ode(dudt,x,tspan,Tsit5(),save_everystep=false,save_start=false)
neural_ode(dudt,x,tspan,Tsit5(),saveat=0.1)
neural_ode_rd(dudt,x,tspan,Tsit5(),saveat=0.1)
Flux.back!(sum(neural_ode(dudt,x,tspan,Tsit5(),save_everystep=false,save_start=false)))
# Adjoint
@testset "adjoint mode" begin
Tracker.zero_grad!(dudt[1].W.grad)
Flux.back!(sum(neural_ode(dudt,x,tspan,Tsit5(),save_everystep=false,save_start=false))) #works?
@test ! iszero(Tracker.grad(dudt[1].W))
Tracker.zero_grad!(dudt[1].W.grad)
Flux.back!(sum(neural_ode(dudt,x,tspan,Tsit5(),saveat=0.0:0.1:10.0))) # broke
@test ! iszero(Tracker.grad(dudt[1].W))
Tracker.zero_grad!(dudt[1].W.grad)
@test_broken Flux.back!(sum(neural_ode(dudt,x,tspan,Tsit5(),saveat=0.1))) #broke
#@test ! iszero(Tracker.grad(dudt[1].W))
end;
#= # RD =#
@testset "reverse mode" begin
Tracker.zero_grad!(dudt[1].W.grad)
Flux.back!(sum(neural_ode_rd(dudt,x,tspan,Tsit5(),save_everystep=false,save_start=false)))
@test ! iszero(Tracker.grad(dudt[1].W))
Tracker.zero_grad!(dudt[1].W.grad)
Flux.back!(sum(neural_ode_rd(dudt,x,tspan,Tsit5(),saveat=0.0:0.1:10.0)))
@test ! iszero(Tracker.grad(dudt[1].W))
Tracker.zero_grad!(dudt[1].W.grad)
@test_broken Flux.back!(sum(neural_ode_rd(dudt,x,tspan,Tsit5(),saveat=0.1)))
#@test ! iszero(Tracker.grad(dudt[1].W))
end;
#=
mp = Float32[0.1,0.1]
Tracker.zero_grad!(dudt[1].W.grad)
neural_dmsde(dudt,x,mp,tspan,SOSRI(),saveat=0.1)
Flux.back!(sum(neural_dmsde(dudt,x,mp,tspan,SOSRI(),saveat=0.1)))
@test ! iszero(Tracker.grad(dudt[1].W))
=#
# Batch
xs = Float32.(hcat([0.; 0.], [1.; 0.], [2.; 0.])) |> gpu
tspan = Float32.((0.0f0,25.0f0))
dudt = Chain(Dense(2,50,tanh),Dense(50,2)) |> gpu
neural_ode(dudt,xs,tspan,Tsit5(),save_everystep=false,save_start=false)
neural_ode(dudt,xs,tspan,Tsit5(),saveat=0.1)
neural_ode_rd(dudt,xs,tspan,Tsit5(),saveat=0.1)
# Adjoint
@testset "adjoint mode batches" begin
Tracker.zero_grad!(dudt[1].W.grad)
Flux.back!(sum(neural_ode(dudt,xs,tspan,Tsit5(),save_everystep=false,save_start=false)))
@test ! iszero(Tracker.grad(dudt[1].W))
Tracker.zero_grad!(dudt[1].W.grad)
Flux.back!(sum(neural_ode(dudt,xs,tspan,Tsit5(),saveat=0.0:0.1:10.0)))
@test ! iszero(Tracker.grad(dudt[1].W))
Tracker.zero_grad!(dudt[1].W.grad)
@test_broken Flux.back!(sum(neural_ode(dudt,xs,tspan,Tsit5(),saveat=0.1)))
#@test ! iszero(Tracker.grad(dudt[1].W))
end;
#= # RD =#
@testset "reverse mode batches" begin
Tracker.zero_grad!(dudt[1].W.grad)
Flux.back!(sum(neural_ode_rd(dudt,xs,tspan,Tsit5(),save_everystep=false,save_start=false)))
@test ! iszero(Tracker.grad(dudt[1].W))
Tracker.zero_grad!(dudt[1].W.grad)
Flux.back!(sum(neural_ode_rd(dudt,xs,tspan,Tsit5(),saveat=0.0:0.1:10.0)))
@test ! iszero(Tracker.grad(dudt[1].W))
Tracker.zero_grad!(dudt[1].W.grad)
@test_broken Flux.back!(sum(neural_ode_rd(dudt,xs,tspan,Tsit5(),saveat=0.1)))
#@test ! iszero(Tracker.grad(dudt[1].W))
end;
# Grads w.r.t. u0
tspan = (0.0f0,25.0f0) |>gpu
dudt = Chain(Dense(2,50,tanh),Dense(50,2))|>gpu
downsample = Dense(3,2)|>gpu
x0 = Float32.(hcat([0.; 0; 0.], [1.,1.,1.],[2.,2.,2.]))|>gpu
u0 = downsample(x0)
neural_ode(dudt,u0,tspan,Tsit5(),save_everystep=false,save_start=false)
neural_ode(dudt,u0,tspan,Tsit5(),saveat=0.1)
neural_ode_rd(dudt,u0,tspan,Tsit5(),saveat=0.1)
# Adjoint
@testset "adjoint mode trackedu0" begin
@test_broken begin
Tracker.zero_grad!(dudt[1].W.grad)
Tracker.zero_grad!(downsample.W.grad)
m1 = Chain(downsample, u0->neural_ode(dudt,u0,tspan,Tsit5(),save_everystep=false,save_start=false)) #broke
Flux.back!(sum(m1(x0)))
@test ! iszero(Tracker.grad(dudt[1].W))
@test ! iszero(Tracker.grad(downsample.W))
Tracker.zero_grad!(dudt[1].W.grad)
Tracker.zero_grad!(downsample.W.grad)
m2 = Chain(downsample, u0->neural_ode(dudt,u0,tspan,Tsit5(),saveat=0.0:0.1:10.0))
Flux.back!(sum(m2(x0)))
@test ! iszero(Tracker.grad(dudt[1].W))
@test ! iszero(Tracker.grad(downsample.W))
Tracker.zero_grad!(dudt[1].W.grad)
Tracker.zero_grad!(downsample.W.grad)
m3 = Chain(downsample, u0->neural_ode(dudt,u0,tspan,Tsit5(),saveat=0.1))
@test_broken Flux.back!(sum(m3(x0)))
#@test ! iszero(Tracker.grad(dudt[1].W))
#@test ! iszero(Tracker.grad(downsample.W))
end
end;
#= # RD =#
@testset "reverse mode trackedu0" begin
Tracker.zero_grad!(dudt[1].W.grad)
u0 = downsample(x0)
Flux.back!(sum(neural_ode_rd(dudt,u0,tspan,Tsit5(),save_everystep=false,save_start=false)))
@test ! iszero(Tracker.grad(dudt[1].W))
@test ! iszero(Tracker.grad(u0))
Tracker.zero_grad!(dudt[1].W.grad)
u0 = downsample(x0)
Flux.back!(sum(neural_ode_rd(dudt,u0,tspan,Tsit5(),saveat=0.0:0.1:10.0)))
@test ! iszero(Tracker.grad(dudt[1].W))
@test ! iszero(Tracker.grad(u0))
Tracker.zero_grad!(dudt[1].W.grad)
u0 = downsample(x0)
@test_broken Flux.back!(sum(neural_ode_rd(dudt,u0,tspan,Tsit5(),saveat=0.1)))
#@test ! iszero(Tracker.grad(dudt[1].W)) =#
#@test ! iszero(Tracker.grad(downsample.W)) =#
end;
@testset "stay on gpu" begin
using CuArrays
x = gpu(randn(784,10))
down = Chain(
Dense(784,20,tanh)
)|>gpu
nn = Chain(
Dense(20,10,tanh),
Dense(10,10,tanh),
Dense(10,20,tanh)
) |>gpu
fc = Chain(
Dense(20,10)
)|>gpu
nn_ode(x) = neural_ode(nn,x,gpu((0.f0,1.f0)), Tsit5(),save_everystep=false,reltol=1e-3,abstol=1e-3, save_start=false)
@test_broken typeof(nn(down(x))) == typeof(nn_ode(down(x)))
end