TaoLv commented on a change in pull request #17091: broadcast_axis optimization
URL: https://github.com/apache/incubator-mxnet/pull/17091#discussion_r361564897
##########
File path: src/operator/tensor/broadcast_reduce_op_value.cc
##########
@@ -32,6 +32,41 @@ DMLC_REGISTER_PARAMETER(BroadcastAxesParam);
DMLC_REGISTER_PARAMETER(BroadcastToParam);
DMLC_REGISTER_PARAMETER(BroadcastLikeParam);
+template<typename DType>
+void BroadcastAxisKer(DType* src,
+ DType* dst,
+ index_t outer,
+ index_t inner,
+ index_t size) {
+#pragma omp parallel for
num_threads(engine::OpenMP::Get()->GetRecommendedOMPThreadCount())
+ for (index_t i = 0; i < outer * size; i++) {
+ const index_t m = i / size;
+ const index_t n = i % size;
+ void* offset = reinterpret_cast<void*>(dst + m * size * inner + n * inner);
+ memcpy(offset, reinterpret_cast<void*>(src + m * inner), inner * sizeof
(DType));
+ }
+}
+
+inline void BroadcastAxisComputeCPU(const nnvm::NodeAttrs& attrs,
+ const OpContext& ctx,
+ const std::vector<TBlob>& inputs,
+ const std::vector<OpReqType>& req,
+ const std::vector<TBlob>& outputs) {
+ using namespace mshadow;
+ const BroadcastAxesParam& param =
nnvm::get<BroadcastAxesParam>(attrs.parsed);
+ if (param.axis.ndim() == 1 && inputs[0].shape_[param.axis[0]] == 1 && req[0]
== kWriteTo) {
Review comment:
It seems broadcast_axis doesn't support negative axis even before this
change. See
https://github.com/apache/incubator-mxnet/blob/master/src/operator/tensor/broadcast_reduce_op.h#L386.
```python
import mxnet as mx
a = mx.random.uniform(shape=(4, 5, 1))
b = mx.nd.broadcast_axis(a, axis=-1, size=6)
print(b)
```
Will cause error:
```bash
Traceback (most recent call last):
File "test_bcast.py", line 4, in <module>
b = mx.nd.broadcast_axis(a, axis=-1, size=6)
File "<string>", line 60, in broadcast_axis
File
"/home/lvtao/miniconda3/envs/mxnet/lib/python3.6/site-packages/mxnet/_ctypes/ndarray.py",
line 107, in _imperative_invoke
ctypes.byref(out_stypes)))
File
"/home/lvtao/miniconda3/envs/mxnet/lib/python3.6/site-packages/mxnet/base.py",
line 278, in check_call
raise MXNetError(py_str(_LIB.MXGetLastError()))
mxnet.base.MXNetError: [18:53:34] include/mxnet/./tuple.h:206: Check failed:
i >= 0 && i < ndim(): index = -1 must be in range [0, 3)
```
----------------------------------------------------------------
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