You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mxnet.apache.org by GitBox <gi...@apache.org> on 2020/02/07 20:17:31 UTC
[GitHub] [incubator-mxnet] roywei opened a new pull request #17547: Fix
cudnn Dropout reproducibility
roywei opened a new pull request #17547: Fix cudnn Dropout reproducibility
URL: https://github.com/apache/incubator-mxnet/pull/17547
Fix https://github.com/apache/incubator-mxnet/issues/15662
This will replace #16532 with an alternative solution.
Please refer to the discussion in https://github.com/apache/incubator-mxnet/pull/16532#discussion_r339268855
1. Added `GetSeed()` in GPU random resource, it's similar to CPU version.
2. During cudnn dropout, check whether random seed has been changed and reset cudnn dropout descriptor's seed if random seed changed.
Benefit:
1. This avoided generating a random int (tensor on gpu) on GPU, copy to CPU and and pass it to `cudnnDropoutGetStatesSize`.
2. get the seed from gpu random resource is fast, we can afford to do it every forward.
Drawback:
Will be affected by https://github.com/apache/incubator-mxnet/issues/7410, by default mxnet's random seed is fixed.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
With regards,
Apache Git Services
[GitHub] [incubator-mxnet] apeforest commented on a change in pull request
#17547: Fix cudnn Dropout reproducibility
Posted by GitBox <gi...@apache.org>.
apeforest commented on a change in pull request #17547: Fix cudnn Dropout reproducibility
URL: https://github.com/apache/incubator-mxnet/pull/17547#discussion_r382967982
##########
File path: 3rdparty/mshadow/mshadow/random.h
##########
@@ -466,6 +474,7 @@ class Random<gpu, DType> {
uniform(Shape<dim> shape);
private:
+ unsigned rseed_;
Review comment:
use uint64_t?
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
With regards,
Apache Git Services
[GitHub] [incubator-mxnet] apeforest commented on a change in pull request
#17547: Fix cudnn Dropout reproducibility
Posted by GitBox <gi...@apache.org>.
apeforest commented on a change in pull request #17547: Fix cudnn Dropout reproducibility
URL: https://github.com/apache/incubator-mxnet/pull/17547#discussion_r382968151
##########
File path: src/operator/rnn.cc
##########
@@ -180,6 +180,7 @@ static std::vector<ResourceRequest> RNNResourceEx(const NodeAttrs& attrs, const
const RNNParam& param = nnvm::get<RNNParam>(attrs.parsed);
if (param.p != 0 && 1.0f - param.p > 0) {
request.emplace_back(ResourceRequest::kCuDNNDropoutDesc);
+ request.emplace_back(ResourceRequest::kRandom);
Review comment:
nit: indent here not consistent.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
With regards,
Apache Git Services
[GitHub] [incubator-mxnet] ChaiBapchya commented on issue #17547: Fix cudnn
Dropout reproducibility
Posted by GitBox <gi...@apache.org>.
ChaiBapchya commented on issue #17547: Fix cudnn Dropout reproducibility
URL: https://github.com/apache/incubator-mxnet/pull/17547#issuecomment-609977294
@roywei with CI issue fixed. let's rebase to include the fix. Thanks!
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
With regards,
Apache Git Services
[GitHub] [incubator-mxnet] leezu merged pull request #17547: Fix cudnn
Dropout reproducibility
Posted by GitBox <gi...@apache.org>.
leezu merged pull request #17547: Fix cudnn Dropout reproducibility
URL: https://github.com/apache/incubator-mxnet/pull/17547
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
With regards,
Apache Git Services
[GitHub] [incubator-mxnet] access2rohit commented on issue #17547: Fix cudnn
Dropout reproducibility
Posted by GitBox <gi...@apache.org>.
access2rohit commented on issue #17547: Fix cudnn Dropout reproducibility
URL: https://github.com/apache/incubator-mxnet/pull/17547#issuecomment-610124065
restarted CI jobs. @roywei can you get your PR merged once they pass CI
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
With regards,
Apache Git Services
[GitHub] [incubator-mxnet] roywei commented on issue #17547: Fix cudnn
Dropout reproducibility
Posted by GitBox <gi...@apache.org>.
roywei commented on issue #17547: Fix cudnn Dropout reproducibility
URL: https://github.com/apache/incubator-mxnet/pull/17547#issuecomment-610147543
@ChaiBapchya @access2rohit Rebased! Thanks!
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
With regards,
Apache Git Services
[GitHub] [incubator-mxnet] roywei commented on issue #17547: Fix cudnn
Dropout reproducibility
Posted by GitBox <gi...@apache.org>.
roywei commented on issue #17547: Fix cudnn Dropout reproducibility
URL: https://github.com/apache/incubator-mxnet/pull/17547#issuecomment-587118629
@DickJC123 @ptrendx could you help take a look again? Thanks
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
With regards,
Apache Git Services
[GitHub] [incubator-mxnet] ChaiBapchya commented on issue #17547: Fix cudnn
Dropout reproducibility
Posted by GitBox <gi...@apache.org>.
ChaiBapchya commented on issue #17547: Fix cudnn Dropout reproducibility
URL: https://github.com/apache/incubator-mxnet/pull/17547#issuecomment-610132949
@access2rohit without rebasing, the fix #17962 won't be included [for windows-gpu]
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
With regards,
Apache Git Services
[GitHub] [incubator-mxnet] apeforest commented on a change in pull request
#17547: Fix cudnn Dropout reproducibility
Posted by GitBox <gi...@apache.org>.
apeforest commented on a change in pull request #17547: Fix cudnn Dropout reproducibility
URL: https://github.com/apache/incubator-mxnet/pull/17547#discussion_r382967938
##########
File path: 3rdparty/mshadow/mshadow/random.h
##########
@@ -404,8 +404,16 @@ class Random<gpu, DType> {
// Now set the seed.
curandStatus_t status;
status = curandSetPseudoRandomGeneratorSeed(gen_, static_cast<uint64_t>(seed));
+ this->rseed_ = static_cast<unsigned>(seed);
CHECK_EQ(status, CURAND_STATUS_SUCCESS) << "Set CURAND seed failed.";
}
+ /*!
+ * \brief get random seed used in random generator
+ * \return seed in unsigned
+ */
+ inline unsigned GetSeed() const {
Review comment:
Why can't we just use uint64_t as the data type for rseed_?
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
With regards,
Apache Git Services
[GitHub] [incubator-mxnet] apeforest commented on issue #17547: Fix cudnn
Dropout reproducibility
Posted by GitBox <gi...@apache.org>.
apeforest commented on issue #17547: Fix cudnn Dropout reproducibility
URL: https://github.com/apache/incubator-mxnet/pull/17547#issuecomment-606884157
@roywei Could you please rebase and update this PR? Thanks!
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
With regards,
Apache Git Services
[GitHub] [incubator-mxnet] leezu commented on a change in pull request
#17547: Fix cudnn Dropout reproducibility
Posted by GitBox <gi...@apache.org>.
leezu commented on a change in pull request #17547: Fix cudnn Dropout reproducibility
URL: https://github.com/apache/incubator-mxnet/pull/17547#discussion_r406867814
##########
File path: src/operator/rnn-inl.h
##########
@@ -1495,7 +1500,7 @@ class RNNOp {
cudnnRNNInputMode_t input_mode_;
cudnnDropoutDescriptor_t dropout_desc_;
Storage::Handle reserve_space_;
- uint64_t seed_ = 17 + rand() % 4096; // NOLINT(runtime/threadsafe_fn)
Review comment:
This `seed_` is used for `rand_r` in the forward implementations. That's very bad practice, not portable and should be fixed.
As this PR already passes CI, it's fine to fix it in a follow-up PR. In fac, https://github.com/apache/incubator-mxnet/pull/17984 is blocked by the usage of `rand_r` so I may fix it there.
More info: https://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
With regards,
Apache Git Services
[GitHub] [incubator-mxnet] karan6181 commented on issue #17547: Fix cudnn
Dropout reproducibility
Posted by GitBox <gi...@apache.org>.
karan6181 commented on issue #17547: Fix cudnn Dropout reproducibility
URL: https://github.com/apache/incubator-mxnet/pull/17547#issuecomment-596784507
@DickJC123 @ptrendx Can you please review this PR once again? Thanks!
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
With regards,
Apache Git Services
[GitHub] [incubator-mxnet] apeforest commented on a change in pull request
#17547: Fix cudnn Dropout reproducibility
Posted by GitBox <gi...@apache.org>.
apeforest commented on a change in pull request #17547: Fix cudnn Dropout reproducibility
URL: https://github.com/apache/incubator-mxnet/pull/17547#discussion_r382968025
##########
File path: src/operator/nn/dropout-inl.h
##########
@@ -255,8 +255,13 @@ class DropoutOp {
Stream<xpu> *s = ctx.get_stream<xpu>();
// set dropout state.
- ctx.requested[0].get_cudnn_dropout_desc(&dropout_desc_, s, 1.0f - this->pkeep_, seed_);
-
+ Random<xpu, unsigned> *prnd = ctx.requested[1].get_random<xpu, unsigned>(s);
+ uint64_t rng_seed = (uint64_t) prnd->GetSeed();
Review comment:
If GetSeed() returns uint64_t you don't need the cast here
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
With regards,
Apache Git Services
[GitHub] [incubator-mxnet] szha commented on a change in pull request #17547: Fix cudnn Dropout reproducibility
Posted by GitBox <gi...@apache.org>.
szha commented on a change in pull request #17547:
URL: https://github.com/apache/incubator-mxnet/pull/17547#discussion_r413972469
##########
File path: src/operator/nn/dropout-inl.h
##########
@@ -255,8 +255,13 @@ class DropoutOp {
Stream<xpu> *s = ctx.get_stream<xpu>();
// set dropout state.
- ctx.requested[0].get_cudnn_dropout_desc(&dropout_desc_, s, 1.0f - this->pkeep_, seed_);
-
+ Random<xpu, unsigned> *prnd = ctx.requested[1].get_random<xpu, unsigned>(s);
+ uint64_t rng_seed = prnd->GetSeed();
+ // reset dropout descriptor if rng seed changed.
+ bool reset = seed_ != rng_seed;
+ seed_ = rng_seed;
+ ctx.requested[0].get_cudnn_dropout_desc(&dropout_desc_, s, 1.0f - this->pkeep_,
+ seed_, reset);
Review comment:
@roywei the links in your comment don't seem to point to what they refer to so I'm a bit confused. Where did you insert the 're-init dropout desc' comment?
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [incubator-mxnet] roywei commented on a change in pull request #17547: Fix cudnn Dropout reproducibility
Posted by GitBox <gi...@apache.org>.
roywei commented on a change in pull request #17547:
URL: https://github.com/apache/incubator-mxnet/pull/17547#discussion_r413521892
##########
File path: src/operator/nn/dropout-inl.h
##########
@@ -255,8 +255,13 @@ class DropoutOp {
Stream<xpu> *s = ctx.get_stream<xpu>();
// set dropout state.
- ctx.requested[0].get_cudnn_dropout_desc(&dropout_desc_, s, 1.0f - this->pkeep_, seed_);
-
+ Random<xpu, unsigned> *prnd = ctx.requested[1].get_random<xpu, unsigned>(s);
+ uint64_t rng_seed = prnd->GetSeed();
+ // reset dropout descriptor if rng seed changed.
+ bool reset = seed_ != rng_seed;
+ seed_ = rng_seed;
+ ctx.requested[0].get_cudnn_dropout_desc(&dropout_desc_, s, 1.0f - this->pkeep_,
+ seed_, reset);
Review comment:
I spent some time looking into it and actually the problem is dropout's forward is entering the [re-init cudnn dropout desc logic](https://github.com/apache/incubator-mxnet/pull/17547/files#diff-cc7bb408eba92cdd6fad0e590b76fabeL438) every time during forward even without my change. This is true for nd/np and gluon, false for symbol dropout. So originally it was already reinitializing in every forward. The reason it does not cause any performance regression is because it's always using the `seed_` defined in `uint64_t seed_ = 17 + rand() % 4096; ` and never changed, it won't listen to MXNet's PRNG (which is the problem this PR is trying to fix). So if the seed didn't change, event if you re-init cudnn dropout descriptor, it won't take any time. My PR changed the seed, so it was actually re-init every forward, thus the regression.
However, it works fine under symbol case. I guess is somehow for symbolic, every forward is using the same Dropout node and the state check took effect the didn't go into re-init logic. For imperative case the state check is always empty and went into the re-init logic.
Compare the following two code, one is gluon and one is symbol, if I print some log during reinitialization in the original code without my change [here](https://github.com/apache/incubator-mxnet/pull/17547/files#diff-cc7bb408eba92cdd6fad0e590b76fabeL438). It will print out every forward in ND/NP/Gluon, not the case in Symbol.
```
import mxnet as mx
data = mx.nd.ones((10, 200, 300, 500), ctx=mx.gpu(0))
dropout = mx.gluon.nn.Dropout(0.5)
# with or without hybridize is the same result
dropout.hybridize()
with mx.autograd.record():
result1 = dropout(data)
result2 = dropout(result1)
```
print 2 times
```
re-init dropout desc
re-init dropout desc
```
Symbol:
```
import mxnet as mx
data = mx.nd.ones((10, 200, 300, 500), ctx=mx.gpu(0))
net = mx.sym.Variable("data")
net = mx.sym.Dropout(data=net, p=0.5, cudnn_off=False)
exe = net.simple_bind(mx.gpu(0), data=data.shape)
result1 = exe.forward(is_train=True, data=data)
result2 = exe.forward(is_train=True, data=result1[0])
```
print 1 time
```
re-init dropout desc
```
Given this situation, I don't have a good solution as checking the state handle size does not work in imperative mode, checking PRNG seed will also not work. I will revert this PR for now as it's causing regressions for models using dropout.
cc @szha @sxjscience @apeforest
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [incubator-mxnet] roywei commented on a change in pull request #17547: Fix cudnn Dropout reproducibility
Posted by GitBox <gi...@apache.org>.
roywei commented on a change in pull request #17547:
URL: https://github.com/apache/incubator-mxnet/pull/17547#discussion_r413521892
##########
File path: src/operator/nn/dropout-inl.h
##########
@@ -255,8 +255,13 @@ class DropoutOp {
Stream<xpu> *s = ctx.get_stream<xpu>();
// set dropout state.
- ctx.requested[0].get_cudnn_dropout_desc(&dropout_desc_, s, 1.0f - this->pkeep_, seed_);
-
+ Random<xpu, unsigned> *prnd = ctx.requested[1].get_random<xpu, unsigned>(s);
+ uint64_t rng_seed = prnd->GetSeed();
+ // reset dropout descriptor if rng seed changed.
+ bool reset = seed_ != rng_seed;
+ seed_ = rng_seed;
+ ctx.requested[0].get_cudnn_dropout_desc(&dropout_desc_, s, 1.0f - this->pkeep_,
+ seed_, reset);
Review comment:
I spent some time looking into it and actually the problem is dropout's forward is entering the [re-init cudnn dropout desc logic](https://github.com/apache/incubator-mxnet/pull/17547/files#diff-cc7bb408eba92cdd6fad0e590b76fabeL438) every time during forward even without my change. This is true for nd/np and gluon, false for symbol dropout. So originally it was already reinitializing in every forward. The reason it does not cause any performance regression is because it's always using the `seed_` defined in `uint64_t seed_ = 17 + rand() % 4096; ` and never changed, it won't listen to MXNet's PRNG (which is the problem this PR is trying to fix). So if the seed didn't change, event if you re-init cudnn dropout descriptor, it won't take any time. My PR changed the seed, so it was actually re-init every forward, thus the regression.
However, it works fine under symbol case. My guess is somehow for symbolic, every forward is using the same Dropout node and the state check took effect then didn't go into re-init logic. For imperative case the state check is always empty and went into the re-init logic.
Compare the following two code, one is gluon and one is symbol, if I print some log during reinitialization in the original code without my change [here](https://github.com/apache/incubator-mxnet/pull/17547/files#diff-cc7bb408eba92cdd6fad0e590b76fabeL438). It will print out every forward in ND/NP/Gluon, not the case in Symbol.
```
import mxnet as mx
data = mx.nd.ones((10, 200, 300, 500), ctx=mx.gpu(0))
dropout = mx.gluon.nn.Dropout(0.5)
# with or without hybridize is the same result
dropout.hybridize()
with mx.autograd.record():
result1 = dropout(data)
result2 = dropout(result1)
```
print 2 times
```
re-init dropout desc
re-init dropout desc
```
Symbol:
```
import mxnet as mx
data = mx.nd.ones((10, 200, 300, 500), ctx=mx.gpu(0))
net = mx.sym.Variable("data")
net = mx.sym.Dropout(data=net, p=0.5, cudnn_off=False)
exe = net.simple_bind(mx.gpu(0), data=data.shape)
result1 = exe.forward(is_train=True, data=data)
result2 = exe.forward(is_train=True, data=result1[0])
```
print 1 time
```
re-init dropout desc
```
Given this situation, I don't have a good solution as checking the state handle size does not work in imperative mode, checking PRNG seed will also not work. I will revert this PR for now as it's causing regressions for models using dropout.
cc @szha @sxjscience @apeforest
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [incubator-mxnet] rondogency commented on a change in pull request #17547: Fix cudnn Dropout reproducibility
Posted by GitBox <gi...@apache.org>.
rondogency commented on a change in pull request #17547:
URL: https://github.com/apache/incubator-mxnet/pull/17547#discussion_r413358071
##########
File path: src/operator/nn/dropout-inl.h
##########
@@ -255,8 +255,13 @@ class DropoutOp {
Stream<xpu> *s = ctx.get_stream<xpu>();
// set dropout state.
- ctx.requested[0].get_cudnn_dropout_desc(&dropout_desc_, s, 1.0f - this->pkeep_, seed_);
-
+ Random<xpu, unsigned> *prnd = ctx.requested[1].get_random<xpu, unsigned>(s);
+ uint64_t rng_seed = prnd->GetSeed();
+ // reset dropout descriptor if rng seed changed.
+ bool reset = seed_ != rng_seed;
+ seed_ = rng_seed;
+ ctx.requested[0].get_cudnn_dropout_desc(&dropout_desc_, s, 1.0f - this->pkeep_,
+ seed_, reset);
Review comment:
I believe @roywei is already working on this issue, since this regression has been caught by our benchmark last week
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [incubator-mxnet] szha commented on a change in pull request #17547: Fix cudnn Dropout reproducibility
Posted by GitBox <gi...@apache.org>.
szha commented on a change in pull request #17547:
URL: https://github.com/apache/incubator-mxnet/pull/17547#discussion_r413213237
##########
File path: src/operator/nn/dropout-inl.h
##########
@@ -255,8 +255,13 @@ class DropoutOp {
Stream<xpu> *s = ctx.get_stream<xpu>();
// set dropout state.
- ctx.requested[0].get_cudnn_dropout_desc(&dropout_desc_, s, 1.0f - this->pkeep_, seed_);
-
+ Random<xpu, unsigned> *prnd = ctx.requested[1].get_random<xpu, unsigned>(s);
+ uint64_t rng_seed = prnd->GetSeed();
+ // reset dropout descriptor if rng seed changed.
+ bool reset = seed_ != rng_seed;
+ seed_ = rng_seed;
+ ctx.requested[0].get_cudnn_dropout_desc(&dropout_desc_, s, 1.0f - this->pkeep_,
+ seed_, reset);
Review comment:
@roywei there are more than 1 random number generators in the resources (4 by default) and they could have different seeds. The result of rotating random number generator seeds here is that cudnn dropout state is reinitialized very often. @sxjscience observed that there's significant performance impact because of this.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org