Skip to content

Commit 6a97162

Browse files
authored
Add CUDA tests for dropout (#60)
* add dropout * bump
1 parent a42c7e3 commit 6a97162

File tree

4 files changed

+41
-3
lines changed

4 files changed

+41
-3
lines changed

ext/NNlibCUDA/Project.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "NNlibCUDA"
22
uuid = "a00861dc-f156-4864-bf3c-e6376f28a68d"
3-
version = "0.2.5"
3+
version = "0.2.7"
44

55
[deps]
66
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
@@ -13,14 +13,15 @@ Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
1313
[compat]
1414
Adapt = "3.3"
1515
CUDA = "3.11"
16-
NNlib = "0.8.11"
16+
NNlib = "0.8.14"
1717
julia = "1.6"
1818

1919
[extras]
2020
CUDA = "052768ef-5323-5732-b1bb-66c8b64840ba"
21+
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
2122
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
2223
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
2324
Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f"
2425

2526
[targets]
26-
test = ["CUDA", "ForwardDiff", "Test", "Zygote"]
27+
test = ["CUDA", "ChainRulesCore", "ForwardDiff", "Test", "Zygote"]

ext/NNlibCUDA/src/utils.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
NNlib._rng_from_array(::CuArray) = CUDA.default_rng()
2+
13
function divide_kernel!(xs, ys, max_idx)
24
index = threadIdx().x + (blockIdx().x - 1) * blockDim().x
35

ext/NNlibCUDA/test/dropout.jl

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using NNlib, NNlibCUDA, CUDA, Test
2+
using Zygote, ChainRulesCore
3+
4+
@testset "dropout + CUDA" begin
5+
# Basics
6+
x1 = CUDA.randn(3, 4)
7+
@test size(@inferred dropout(x1, 0.1)) == (3, 4)
8+
@test size(@inferred dropout(x1, 0.2; dims=2)) == (3, 4)
9+
@test size(@inferred dropout(x1, 0.3; dims=(1,2))) == (3, 4)
10+
11+
rng = CUDA.default_rng()
12+
@test size(@inferred dropout(rng, x1, 0.1)) == (3, 4)
13+
@test size(@inferred dropout(rng, x1, 0.1; dims=2)) == (3, 4)
14+
15+
# Values
16+
d45 = dropout(CUDA.ones(100, 100, 100), 0.45)
17+
@test mean(d45) 1 atol=1e-2
18+
dpi2 = dropout(CUDA.fill(1f0 * pi, 1000), 0.2)
19+
@test sort(unique(Array(dpi2))) [0, 5pi/4]
20+
d33 = dropout(CUDA.fill(3f0, 10, 1000), 0.3, dims=2)
21+
@test sort(unique(vec(Array(d33)))) [0, 3/(1-0.3)]
22+
23+
# Gradient rule
24+
y, back = rrule(dropout, rng, hcat(CUDA.ones(1000), CUDA.zeros(1000)), 0.45)
25+
dx = back(CUDA.fill(3f0, 1000, 2))[3]
26+
@test !all(iszero, dx[:,2]) # this is why we save the random choices
27+
@test sort(unique(vec(Array(dx)))) [0, 3/(1-0.45)]
28+
29+
@testset "Zygote" begin
30+
@test Zygote.gradient(x -> sum(dropout(x, 0.3)), x1)[1] isa CuArray{Float32}
31+
@test Zygote.gradient(x -> sum(dropout(rng, x, 0.3)), x1)[1] isa CuArray{Float32}
32+
@test Zygote.gradient(x -> sum(dropout(x, 0.3, dims=1)), x1)[1] isa CuArray{Float32}
33+
end
34+
end

ext/NNlibCUDA/test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ CUDA.allowscalar(false)
1010
@testset "NNlibCUDA" begin
1111
include("test_utils.jl")
1212
include("activations.jl")
13+
include("dropout.jl")
1314
include("batchedadjtrans.jl")
1415
include("batchedmul.jl")
1516
include("upsample.jl")

0 commit comments

Comments
 (0)