szha closed pull request #11231: Fix a bug in sparse embedding operator URL: https://github.com/apache/incubator-mxnet/pull/11231
This is a PR merged from a forked repository. As GitHub hides the original diff on merge, it is displayed below for the sake of provenance: As this is a foreign pull request (from a fork), the diff is supplied below (as it won't show otherwise due to GitHub magic): diff --git a/src/operator/tensor/indexing_op.cu b/src/operator/tensor/indexing_op.cu index 593593ae4e3..b0ee05ea139 100644 --- a/src/operator/tensor/indexing_op.cu +++ b/src/operator/tensor/indexing_op.cu @@ -188,7 +188,8 @@ void SparseEmbeddingDeterministicKernelLaunch(const OpContext& ctx, // estimate unique temp space IType* data_ptr = data.dptr<IType>(); size_t *null_ptr = nullptr; - cub::DeviceSelect::Unique(NULL, unique_workspace_bytes, data_ptr, data_ptr, + // unique operations will be applied on sorted data + cub::DeviceSelect::Unique(NULL, unique_workspace_bytes, sorted_data, sorted_data, null_ptr, data_size, Stream<gpu>::GetStream(s)); // One more space reserved for unique count size_t temp_workspace_bytes = std::max(unique_workspace_bytes, diff --git a/tests/python/unittest/test_gluon.py b/tests/python/unittest/test_gluon.py index bf1e0deb200..ced3063448b 100644 --- a/tests/python/unittest/test_gluon.py +++ b/tests/python/unittest/test_gluon.py @@ -753,8 +753,22 @@ def check_embedding(sparse_grad): y.backward() assert (layer.weight.grad().asnumpy()[:5] == 1).all() assert (layer.weight.grad().asnumpy()[5:] == 0).all() + + def check_embedding_large_input(sparse_grad): + embedding = mx.gluon.nn.Embedding(10, 1, sparse_grad=True) + embedding.initialize() + embedding.hybridize() + shape = (20481,) + with mx.autograd.record(): + emb_in = embedding(mx.nd.ones(shape)) + loss = emb_in.sum() + loss.backward() + assert embedding.weight.grad().data.sum().asscalar() == 20481 + check_embedding(True) check_embedding(False) + check_embedding_large_input(True) + check_embedding_large_input(False) @with_seed() def test_export(): ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services