Fix multithreading bug in ∇conv_filter_im2col #235
Merged
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.
Fixes issue #197.
The issue is caused by threading, and if
JULIA_NUM_THREADS
is greater than 1, it can be reproduced with the following MWE, which compares the gradients computed with∇conv_filter_im2col
to those computed with∇conv_filter_direct
.In a correct implementation,
compare_grad()
should always return zero. Since the random seed is set to zero, the result should also be deterministic. However, when using multithreading it returns seemingly random large numbers:This is caused because
∇conv_filter_im2col!
uses a threaded for-loop that attempts to accumulate the result to a single arraydw
, causing write conflicts.I fixed this issue simply by disabling threading for this loop. Another option would be to introduce another thread-specific workspace for
dw
, and accumulate over threads, but I suspect this will not be worth it.This PR also fixes some bugs involving
GC.@preserve
by removing commas from the argument list. Commas in this context cause the arguments to be treated as a tuple, as can be seen from the following example: