Skip to content

Commit

Permalink
Fix crash in random.shuffle operator (apache#15041)
Browse files Browse the repository at this point in the history
* fix crash in random_shuffle caused by int overflow

* add unit test

* add comment

* remove small random test to avoid CI failure
  • Loading branch information
apeforest authored and haohuw committed Jun 23, 2019
1 parent 5798abf commit 5abe119
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/operator/random/shuffle_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,13 @@ namespace {
template<typename DType, typename Rand>
void Shuffle1D(DType* const out, const index_t size, Rand* const prnd) {
#ifdef USE_GNU_PARALLEL_SHUFFLE
auto rand_n = [prnd](index_t n) {
std::uniform_int_distribution<index_t> dist(0, n - 1);
/*
* See issue #15029: the data type of n needs to be compatible with
* the gcc library: https://github.com/gcc-mirror/gcc/blob/master/libstdc%2B%2B\
* -v3/include/parallel/random_shuffle.h#L384
*/
auto rand_n = [prnd](uint32_t n) {
std::uniform_int_distribution<uint32_t> dist(0, n - 1);
return dist(*prnd);
};
__gnu_parallel::random_shuffle(out, out + size, rand_n);
Expand Down
2 changes: 2 additions & 0 deletions tests/python/unittest/test_random.py
Original file line number Diff line number Diff line change
Expand Up @@ -867,6 +867,8 @@ def testLarge(data, repeat):
# Test larger arrays
testLarge(mx.nd.arange(0, 100000).reshape((10, 10000)), 10)
testLarge(mx.nd.arange(0, 100000).reshape((10000, 10)), 10)
testLarge(mx.nd.arange(0, 100000), 10)


@with_seed()
def test_randint():
Expand Down

0 comments on commit 5abe119

Please sign in to comment.