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 2019/10/25 03:54:12 UTC

[GitHub] [incubator-mxnet] roywei commented on a change in pull request #16605: Fix rnn dropout on CPU

roywei commented on a change in pull request #16605: Fix rnn dropout on CPU
URL: https://github.com/apache/incubator-mxnet/pull/16605#discussion_r338877010
 
 

 ##########
 File path: tests/nightly/test_fixed_seed.py
 ##########
 @@ -0,0 +1,69 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+import sys
+import os
+curr_path = os.path.dirname(os.path.abspath(os.path.expanduser(__file__)))
+sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', 'python', 'unittest'))
+from common import with_seed
+
+import mxnet as mx
+import numpy as np
+from mxnet.test_utils import assert_almost_equal
+
+@with_seed()
+def test_dropout_with_seed():
+    info = np.iinfo(np.int32)
+    seed = np.random.randint(info.min, info.max)
+    _test_dropout(seed, mx.cpu())
+    _test_dropout(seed, mx.gpu())
+
+def _test_dropout(seed, ctx):
+    data = mx.nd.ones((100, 100), ctx=ctx)
+    dropout = mx.gluon.nn.Dropout(0.5)
+
+    mx.random.seed(seed)
+    with mx.autograd.record():
+        result1 = dropout(data)
+
+    mx.random.seed(seed)
+    with mx.autograd.record():
+        result2 = dropout(data)
+    # dropout on gpu should return same result with fixed seed
+    assert_almost_equal(result1.asnumpy(), result2.asnumpy())
 
 Review comment:
   According to the discussion [here](https://github.com/apache/incubator-mxnet/pull/16532#discussion_r337794095), we choose to generate a random seed from MXNet CPU Random module  to avoid seeding every time during forward. The benefit is we only seed once during cuddn dropout initialization, and use CPU random module will avoid calling cuda mem copy. Drawback is we can not fix seed on different device. Note that the original code also cannot seed on different device. Currently we didn't see any usage of such functionality, so we chose this solution for performance.

----------------------------------------------------------------
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