-
Notifications
You must be signed in to change notification settings - Fork 157
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
Dropout #182
Open
szagoruyko
wants to merge
1
commit into
soumith:R5
Choose a base branch
from
szagoruyko:dropout
base: R5
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
Dropout #182
Changes from all commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
local Dropout, parent = torch.class('cudnn.Dropout','nn.Dropout') | ||
|
||
local errcheck = cudnn.errcheck | ||
local ffi = require 'ffi' | ||
|
||
local function getSize(f, desc) | ||
local size = ffi.new'size_t[1]' | ||
errcheck(f, desc, size) | ||
return tonumber(size[0]) | ||
end | ||
|
||
function Dropout:createIODescriptors(input) | ||
assert(input:isContiguous(), 'Non-contiguous inputs not supported yet'); | ||
if not self.inplace then | ||
self.output:resizeAs(input) | ||
end | ||
|
||
local nElem = input:nElement() | ||
self.nElem = self.nElem or nElem -- this goes to the second branch only once | ||
if self.iDesc and nElem == self.nElem then return end | ||
self.nElem = nElem | ||
self.iDesc = cudnn.toDescriptor(input:view(1,1,1,nElem)) | ||
|
||
-- initialize RNG for dropouts lazily (per device) | ||
cudnn.dropout_rng_states = cudnn.dropout_rng_states or {} | ||
local dev = cutorch.getDevice() | ||
if not cudnn.dropout_rng_states[dev] then | ||
local states_size = getSize('cudnnDropoutGetStatesSize', cudnn.getHandle()) | ||
cudnn.dropout_rng_states[dev] = torch.CudaByteTensor(states_size) | ||
end | ||
|
||
if not self.dropDesc then | ||
self.dropDesc = ffi.new('struct cudnnDropoutStruct*[1]') | ||
errcheck('cudnnCreateDropoutDescriptor', self.dropDesc) | ||
local reserves_size = getSize('cudnnDropoutGetReserveSpaceSize', self.iDesc[0]) | ||
self.reserves = self.reserves or torch.CudaByteTensor() | ||
self.reserves = self.reserves:cudaByte():resize(reserves_size) | ||
local state = cudnn.dropout_rng_states[dev] | ||
errcheck('cudnnSetDropoutDescriptor', self.dropDesc[0], | ||
cudnn.getHandle(), self.p, | ||
state:data(), state:nElement(), torch.seed()) | ||
|
||
local function destroyADesc(a) | ||
if (a[0]) then | ||
errcheck('cudnnDestroyDropoutDescriptor', a[0]); | ||
a[0] = nil | ||
end | ||
end | ||
ffi.gc(self.dropDesc, destroyADesc) | ||
end | ||
end | ||
|
||
function Dropout:updateOutput(input) | ||
assert(self.v2) | ||
if self.inplace then | ||
self.output:set(input) | ||
else | ||
self.output:resizeAs(input) | ||
end | ||
self:createIODescriptors(input) | ||
local train = self.p > 0 or self.train | ||
if train then | ||
errcheck('cudnnDropoutForward', cudnn.getHandle(), | ||
self.dropDesc[0], | ||
self.iDesc[0], input:data(), | ||
self.iDesc[0], self.output:data(), | ||
self.reserves:data(), | ||
self.reserves:nElement()) | ||
elseif not self.inplace then | ||
self.output:copy(input) | ||
end | ||
return self.output | ||
end | ||
|
||
function Dropout:updateGradInput(input, gradOutput) | ||
assert(self.train) | ||
if self.inplace then | ||
self.gradInput:set(gradOutput) | ||
else | ||
self.gradInput:resizeAs(gradOutput) | ||
end | ||
if self.p > 0 then | ||
errcheck('cudnnDropoutBackward', cudnn.getHandle(), | ||
self.dropDesc[0], | ||
self.iDesc[0], gradOutput:data(), | ||
self.iDesc[0], self.gradInput:data(), | ||
self.reserves:data(), | ||
self.reserves:nElement()) | ||
elseif not self.inplace then | ||
self.gradInput:copy(self.gradOutput) | ||
end | ||
return self.gradInput | ||
end | ||
|
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
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
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 would reset the seed of torch RNG - this is probably not something you want to do, as it can introduce non-desirable non-determinism. Better to use torch.random() here?