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 2018/06/04 08:58:03 UTC

[GitHub] sxjscience opened a new pull request #11134: [MXNET-507] Set dtype of index to be int32 for ordering ops

sxjscience opened a new pull request #11134: [MXNET-507] Set dtype of index to be int32 for ordering ops
URL: https://github.com/apache/incubator-mxnet/pull/11134
 
 
   ## Description ##
   
   There are two problems in the ordering operators, i.e, topk, sort, argsort:
   
   1) Only real_t is supported.
   
   2) The indices are stored as real_t. This will cause error in the backward pass where the gradient are passed to the wrong locations.
   
   For example, we can not run the following code in the previous version:
   
   ```python
   
   import mxnet as mx
   import numpy as np
   import mxnet.ndarray as nd
   
   ctx = mx.cpu()
   
   a = mx.nd.arange(54686454, ctx=ctx, dtype=np.int32)
   a.attach_grad()
   
   k = 10
   with mx.autograd.record():
   b = mx.nd.topk(a, k=k, ret_typ='value')
   b.backward(mx.nd.ones((k,), ctx=ctx, dtype=np.int32))
   a_grad = a.grad.asnumpy()
   for i in range(-1, - k - 1, -1):
   assert a_grad[i] == 1
   
   ```
   I propose to fix this bug by changing the dtype of indices to int32. This will make the code **backward incompatible**. However, it only breaks some rare usages. Normally we will directly output the indices or use them to slice a tensor. The current change do not break these common usages.
   
   This PR also fixes the issue reported in https://github.com/apache/incubator-mxnet/issues/10085 that ordering ops do not support kNullOp.
   
   ## Checklist ##
   ### Essentials ###
   Please feel free to remove inapplicable items for your PR.
   - [x] The PR title starts with [MXNET-$JIRA_ID], where $JIRA_ID refers to the relevant [JIRA issue](https://issues.apache.org/jira/projects/MXNET/issues) created (except PRs with tiny changes)
   - [x] Changes are complete (i.e. I finished coding on this PR)
   - [x] All changes have test coverage:
   - Unit tests are added for small changes to verify correctness (e.g. adding a new operator)
   - Nightly tests are added for complicated/long-running ones (e.g. changing distributed kvstore)
   - Build tests will be added for build configuration changes (e.g. adding a new build option with NCCL)
   - [x] Code is well-documented: 
   - For user-facing API changes, API doc string has been updated. 
   - For new C++ functions in header files, their functionalities and arguments are documented. 
   - For new examples, README.md is added to explain the what the example does, the source of the dataset, expected performance on test set and reference to the original paper if applicable
   - Check the API doc at http://mxnet-ci-doc.s3-accelerate.dualstack.amazonaws.com/PR-$PR_ID/$BUILD_ID/index.html
   - [x] To the my best knowledge, examples are either not affected by this change, or have been fixed to be compatible with this change
   
   ### Changes ###
   - [ ] Arbitrary dtype (exclude float16) for ordering operators, tests
   - [ ] Use int32 as the dtype for indices, tests
   - [ ] Fix the bug that ordering ops do not support kNullOp, tests
   
   ## Comments ##
   - This change is backward incompatible. However, I think it's reasonable to set the dtype of indices to int32 because it does not break the common usage where we use these indices to slice a tensor. 
   

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services