-
-
Notifications
You must be signed in to change notification settings - Fork 612
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reduce allocations in Dropout #1791
base: master
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,14 +30,14 @@ The [`Dropout`](@ref) layer is what you should use in most scenarios. | |
""" | ||
function dropout(x, p; dims=:, active::Bool=true) | ||
active || return x | ||
y = dropout_mask(x, p, dims=dims) | ||
return x .* y | ||
y = rand!(similar(x, _dropout_shape(x, dims))) | ||
@inbounds @. y = x * _dropout_kernel(y, p, 1-p) | ||
end | ||
|
||
@adjoint function dropout(x, p; dims=:, active::Bool=true) | ||
active || return x, Δ -> (Δ, nothing) | ||
y = dropout_mask(x, p, dims=dims) | ||
return x .* y, Δ -> (Δ .* y, nothing) | ||
y = rand!(similar(x, _dropout_shape(x, dims))) | ||
return x .* _dropout_kernel.(y, p, 1-p), Δ -> (Δ .* _dropout_kernel.(y, p, 1-p), nothing) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This makes me wonder if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That, I believe, would be equivalent to the change here (but perhaps with neater packaging). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe it would also save a multiplication per element (assuming There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it'd be equivalent in the end. Maybe check the generated code to verify. |
||
end | ||
|
||
function dropout_mask(x, p; dims=:) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note BTW that this re-use of
That's another reason to avoid this, in favour of the fusion proposed here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, I think deleting an internal function like this should be fine. If anyone was overloading this for some reason, better they find out sooner than later. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would this correctly not trigger for GPU arrays? The type of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure this issue exists for GPU, nor whether it calls the method which I pirate here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IDK, but the definition appears specific enough to not cause major problems: https://github.com/JuliaGPU/GPUArrays.jl/blob/master/src/host/broadcast.jl#L50 |
||
|
@@ -56,7 +56,7 @@ e.g. `Dropout(p; dims = 3)` will randomly zero out entire channels on WHCN input | |
(also called 2D dropout). | ||
|
||
Does nothing to the input once [`Flux.testmode!`](@ref) is `true`. | ||
""" | ||
"""` | ||
mutable struct Dropout{F,D} | ||
p::F | ||
dims::D | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to replace
dropout_mask
here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would probably be easier to just remove the call to
dropout_kernel
indropout_mask
and keep the first line here. That also gives a reason fordropout_mask
's continued existence (I can see it coming in handy in the future if we ever think of more efficient ways to generate or store the mask depending on input type).Edit: a slimmed down
dropout_mask
could also be used byFlux.jl/src/layers/normalise.jl
Line 114 in 1242c20