Zha0q1 commented on a change in pull request #19015:
URL: https://github.com/apache/incubator-mxnet/pull/19015#discussion_r478598552
##########
File path: src/operator/numpy/np_nonzero_op.cc
##########
@@ -88,17 +88,16 @@ void NonzeroForwardCPU(const nnvm::NodeAttrs& attrs,
const_cast<NDArray &>(out).Init(s);
return;
}
- std::vector<int32_t> prefix_sum(in_size, 0);
+ std::vector<index_t> prefix_sum(in_size, 0);
size_t valid_num = 0;
// Calculate prefix sum
MSHADOW_TYPE_SWITCH_WITH_BOOL(in.dtype(), DType, {
DType* in_dptr = in.data().dptr<DType>();
for (size_t i = 0; i < in_size; i++) {
- prefix_sum[i] = (i == 0) ? 0 : prefix_sum[i - 1];
- prefix_sum[i] += (in_dptr[i]) ? 1 : 0;
+ valid_num += (in_dptr[i] != 0);
Review comment:
I will add a comment there explaining the logic. This way of writing it
is faster than branching
----------------------------------------------------------------
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]