@@ -2,7 +2,9 @@ istraining() = false
2
2
3
3
ChainRulesCore. rrule (:: typeof (istraining)) = true , _ -> (NoTangent (),)
4
4
5
- _isactive (m) = isnothing (m. active) ? istraining () : m. active
5
+ _isactive (m) = isnothing (m. active) ? istraining () : Bool (m. active)
6
+
7
+ ChainRulesCore. @non_differentiable _isactive (:: Any )
6
8
7
9
_dropout_shape (s, :: Colon ) = size (s)
8
10
_dropout_shape (s, dims) = tuple ((i ∉ dims ? 1 : si for (i, si) ∈ enumerate (size (s))). .. )
@@ -31,26 +33,51 @@ automatically managed using the [`Dropout`](@ref) layer instead of the
31
33
32
34
The [`Dropout`](@ref) layer is what you should use in most scenarios.
33
35
"""
34
- function dropout (rng, x, p; dims= :, active:: Bool = true )
35
- active || return x
36
- y = dropout_mask (rng, x, p, dims= dims)
37
- return x .* y
38
- end
36
+ dropout (rng, x, p; dims= :, active:: Bool = true ) = _dropout (rng, x, p, dims, active)
39
37
dropout (x, p; kwargs... ) = dropout (rng_from_array (x), x, p; kwargs... )
40
38
41
- dropout_mask (rng:: CUDA.RNG , x:: CuArray , p; kwargs... ) = _dropout_mask (rng, x, p; kwargs... )
42
- dropout_mask (rng, x:: CuArray , p; kwargs... ) =
43
- throw (ArgumentError (" x isa CuArray, but rng isa $(typeof (rng)) . dropout_mask only support CUDA.RNG for CuArrays." ))
44
- dropout_mask (rng, x, p; kwargs... ) = _dropout_mask (rng, x, p; kwargs... )
45
- function _dropout_mask (rng, x, p; dims= :)
39
+ # Internal function without kwargs to keep Zygote generated code type stable
40
+ function _dropout (rng, x, p, dims, active)
41
+ mask = active ? dropout_mask (rng, x, p, dims) : nothing
42
+ return _apply_mask (x, mask)
43
+ end
44
+
45
+ function ChainRulesCore. rrule (:: typeof (_dropout), rng, x, p, dims, active)
46
+ mask = active ? dropout_mask (rng, x, p, dims) : nothing
47
+ # Required because we don't always call dropout_mask
48
+ MT = Core. Compiler. return_type (dropout_mask, Tuple{typeof (rng),typeof (x),typeof (p),typeof (dims)})
49
+ project_x = ProjectTo (x)
50
+ return _apply_mask (x, mask), DropoutPullback {MT,typeof(project_x)} (mask, project_x)
51
+ end
52
+
53
+ # Also needed for type stability. Otherwise inference lifts the Union into a
54
+ # Union{pullback{Nothing}, pullback{AbstractArray}}
55
+ struct DropoutPullback{M<: AbstractArray ,P<: ProjectTo{AbstractArray} }
56
+ mask:: Union{Nothing,M}
57
+ project:: P
58
+ end
59
+
60
+ function (pb:: DropoutPullback )(dy)
61
+ dx = pb. project (_apply_mask (dy, pb. mask))
62
+ return (NoTangent (), NoTangent (), dx, NoTangent ())
63
+ end
64
+
65
+ _apply_mask (x, :: Nothing ) = x
66
+ _apply_mask (x, mask) = x .* mask
67
+
68
+ dropout_mask (rng:: CUDA.RNG , x:: CuArray , p, dims) = _dropout_mask (rng, x, p, dims)
69
+ dropout_mask (rng, x:: CuArray , p, dims) =
70
+ throw (ArgumentError (" x isa CuArray, but rng isa $(typeof (rng)) . dropout_mask only supports CUDA.RNG for CuArrays." ))
71
+ dropout_mask (rng, x, p, dims) = _dropout_mask (rng, x, p, dims)
72
+ function _dropout_mask (rng, x, p, dims)
46
73
realfptype = float (real (eltype (x)))
47
74
y = rand! (rng, similar (x, realfptype, _dropout_shape (x, dims)))
48
75
y .= _dropout_kernel .(y, p, 1 - p)
49
76
return y
50
77
end
51
78
52
79
# TODO move this to NNlib
53
- ChainRulesCore. @non_differentiable dropout_mask (:: Any , :: Any , :: Any )
80
+ ChainRulesCore. @non_differentiable dropout_mask (:: Any , :: Any , :: Any , :: Any )
54
81
55
82
"""
56
83
Dropout(p; dims=:, rng = rng_from_array())
82
109
@functor Dropout
83
110
trainable (a:: Dropout ) = (;)
84
111
85
- function (a:: Dropout )(x)
86
- _isactive (a) || return x
87
- return dropout (a. rng, x, a. p; dims= a. dims, active= true )
88
- end
112
+ (a:: Dropout )(x) = _dropout (a. rng, x, a. p, a. dims, _isactive (a))
89
113
90
114
testmode! (m:: Dropout , mode= true ) =
91
115
(m. active = (isnothing (mode) || mode == :auto ) ? nothing : ! mode; m)
@@ -172,7 +196,7 @@ LayerNorm(size_act...; kw...) = LayerNorm(Int.(size_act[1:end-1]), size_act[end]
172
196
173
197
@functor LayerNorm
174
198
175
- (a:: LayerNorm )(x) = a. diag (normalise (x, dims = 1 : length (a. size), ϵ = a. ϵ))
199
+ (a:: LayerNorm )(x) = a. diag (_normalize (x, 1 : length (a. size), a. ϵ))
176
200
177
201
function Base. show (io:: IO , l:: LayerNorm )
178
202
print (io, " LayerNorm(" , join (l. size, " , " ))
0 commit comments