wkcn commented on a change in pull request #16218: Improving performance of
argmax operator
URL: https://github.com/apache/incubator-mxnet/pull/16218#discussion_r326427720
##########
File path: src/operator/tensor/broadcast_reduce_op.h
##########
@@ -556,6 +556,162 @@ inline bool ReduceAxesOpForwardStorage(const
nnvm::NodeAttrs& attrs,
return dispatched;
}
+struct argmax {
+ template<typename DType>
+ MSHADOW_XINLINE static void Map(int i, const int nWorkers, const DType
*in_data, DType *out_data,
+ size_t nSteps, size_t step, size_t shift, void *pIdxStorage,
bool use_uint16) {
+ // i - index of launched thread
+ // nWorkers - number of threads, assigned to work on one row/column
+ // iw - index of current thread among workers assigned to the same vector
+ int iw = 0;
+ const DType *pCurr = in_data;
+ if (nWorkers > 1) {
+ // in - the vector number which current thread is assigned to
+ const auto in = i / nWorkers;
+ iw = i % nWorkers;
+ pCurr += in % step + shift * (in / step) + iw * step;
+ nSteps = (nSteps + nWorkers - 1 - iw) / nWorkers;
+ step *= nWorkers;
+ } else {
+ pCurr += i % step + shift * (i / step);
+ }
+
+ int maxIdx = 0;
+ DType maxVal = *pCurr;
+ for (size_t j = 1; j < nSteps; ++j) {
+ if (maxVal < *(pCurr += step)) {
+ maxVal = *pCurr;
+ maxIdx = j;
+ }
+ }
+
+ if (nWorkers > 1) {
+ // saving index of best element found by current thread
+ if (use_uint16) {
+ *(static_cast<uint16_t *>(pIdxStorage) + i) = maxIdx * nWorkers + iw;
+ } else {
+ *(static_cast<uint32_t *>(pIdxStorage) + i) = maxIdx * nWorkers + iw;
+ }
+ } else {
+ out_data[i] = maxIdx; // output of argmax
+ }
+ }
+};
+
+struct argmax_reduce {
+ template<typename DType>
+ MSHADOW_XINLINE static void Map(int i, const int nWorkers, const DType
*in_data, DType *out_data,
+ const size_t step, const size_t shift, void *pIdx, const bool
use_uin16) {
+ const DType *pCurr = in_data + i % step + shift * (i / step);
+ int maxIdx;
+ if (use_uin16) {
Review comment:
It may be better to use template classes to represent the implementation of
uint16 and uint32.
----------------------------------------------------------------
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:
[email protected]
With regards,
Apache Git Services