-
-
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
Open
mcognetta
wants to merge
4
commits into
FluxML:master
Choose a base branch
from
mcognetta:dropout_opt
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This allocates a new vector rather than reusing
y
. I tried this variant and it produced the same lowered code as the original.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.
I see, it's a compelling change, but I don't think it works when dims is set, because then the size of y is actually smaller than x. The current code relies on broadcasting to inflate size-1 dims in the mask to the equivalent full dim size in x.
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.
Something here breaks type-stability, which isn't visible on the benchmarks of large arrays:
Also, it is odd that the calculation of
1-p
is pulled out of the kernel, but the more expensive1/q
is not. IMO this should be written_dropout_kernel(y, p, invq) = ifelse(y > p, invq, zero(invq))
, although in examples I tried the compiler does seem to figure this out. But explicit is better than magic.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.
Seems to infer fine for me, perhaps
Random.rand!
wasn't imported beforehand? I get 100.5ns fordropout_pr
and 108.5ns forFlux.dropout
.Riffing of an earlier comment, I wonder if
x
should also be an arg todropout_kernel
. Local benchmarking didn't show much of a difference, but as long as it doesn't hurt codegen it could help to eliminate some redundancy.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.
OK, I can't reproduce this on a restart, not sure what was wrong, sorry.
I doubt it matters much whether you write
x .* _dropout_kernel.(y, p, invq)
or_dropout_kernel.(x, y, p, invq)
, but not opposed. Your hope is roughly that it'll compile onebroadcast
for forwards & back, instead of two?Pulling out the division and avoiding a branch seems like a good idea, although likewise I can't prove it causes issues.
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.
Cool. It may not work at all, not sure. It's also possible that this should be
y .= rand.(Float32) .> p
.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.
I get:
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.
But confusingly:
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.
What does
CUDA.@sync @btime
do? It seems like that would sync once after the benchmark has run, but perhaps it's not like that? I am never sure about GPU benchmarks.The CPU result is sturprising. Note that your
pr_y
is different to mine, it makes a second pass overy
, and broadcasts back to the same array in-place, so it might hit JuliaLang/julia#43153 . I was assuming that, if you materialise an array of random numbers, you should still fuse the.> p
loop with thex
one. These should if anything make it slower, though.In the GPU case, this 2nd pass (over a small array, 50x2?) might mean one more kernel launch, and it's possible this is the entire timing here, 2 vs 1?
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.
The
@sync @btime
was just crossed wires on my end, these should be accurate:AFAICT from
@device_code_llvm
, the GPU path doesn't include a similar aliasing check.Edit: another factor to consider is that
rand!
may be quite a bit faster on CPU with 1.7+ because of the new SIMD-friendly Xoshiro implementation.