eric-haibin-lin closed pull request #10452: [MXNET-294] Fix element wise 
multiply for csr ndarrays
URL: https://github.com/apache/incubator-mxnet/pull/10452
 
 
   

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/elemwise_binary_op-inl.h 
b/src/operator/tensor/elemwise_binary_op-inl.h
index 5cd33145316..15b1f0e286e 100644
--- a/src/operator/tensor/elemwise_binary_op-inl.h
+++ b/src/operator/tensor/elemwise_binary_op-inl.h
@@ -288,12 +288,16 @@ void ElemwiseBinaryOp::CsrCsrOp(mshadow::Stream<cpu> *s,
   mshadow::Tensor<cpu, 1, DType> lhs_row(reinterpret_cast<DType 
*>(workspace.dptr_
                                                                    + nr_cols * 
sizeof(IType)),
                                          Shape1(nr_cols));
-  mshadow::Tensor<cpu, 1, DType> rhs_row(lhs_row.dptr_ + nr_cols, 
Shape1(nr_cols));
+  mshadow::Tensor<cpu, 1, DType> rhs_row;
 
   OpBase::FillDense<IType>(s, next.shape_.Size(), IType(-1), req, next.dptr_);
   OpBase::FillDense<DType>(s, lhs_row.shape_.Size(), DType(0),  req, 
lhs_row.dptr_);
+
   if (!same_lhs_rhs) {
+    rhs_row = Tensor<cpu, 1, DType>(lhs_row.dptr_ + nr_cols, Shape1(nr_cols));
     OpBase::FillDense<DType>(s, rhs_row.shape_.Size(), DType(0), req, 
rhs_row.dptr_);
+  } else {
+    rhs_row = lhs_row;
   }
 
   // Column indices
@@ -331,17 +335,19 @@ void ElemwiseBinaryOp::CsrCsrOp(mshadow::Stream<cpu> *s,
       }
     }
 
-    // add a row of B to rhs_row
-    const IType i_start_r = row_ptr_r[i];
-    const IType i_end_r = row_ptr_r[i + 1];
-    for (IType jj = i_start_r; jj < i_end_r; jj++) {
-      const IType col = col_indices_r[jj];
-      rhs_row[col] += data_r[jj];
-
-      if (next[col] == -1) {
-        next[col] = head;
-        head = col;
-        ++length;
+    if (!same_lhs_rhs) {
+      // add a row of B to rhs_row
+      const IType i_start_r = row_ptr_r[i];
+      const IType i_end_r = row_ptr_r[i + 1];
+      for (IType jj = i_start_r; jj < i_end_r; jj++) {
+        const IType col = col_indices_r[jj];
+        rhs_row[col] += data_r[jj];
+
+        if (next[col] == -1) {
+          next[col] = head;
+          head = col;
+          ++length;
+        }
       }
     }
 
@@ -361,7 +367,7 @@ void ElemwiseBinaryOp::CsrCsrOp(mshadow::Stream<cpu> *s,
 
       next[temp] = -1;
       lhs_row[temp] = 0;
-      rhs_row[temp] = 0;
+      if (!same_lhs_rhs) rhs_row[temp] = 0;
     }
 
     row_ptr_out[i + 1] = nnz;
diff --git a/tests/python/unittest/test_sparse_ndarray.py 
b/tests/python/unittest/test_sparse_ndarray.py
index 25eaf4291c9..4a72660a388 100644
--- a/tests/python/unittest/test_sparse_ndarray.py
+++ b/tests/python/unittest/test_sparse_ndarray.py
@@ -261,6 +261,7 @@ def check_binary(fn, stype):
             lhs_nd = mx.nd.array(lhs).tostype(stype)
             rhs_nd = mx.nd.array(rhs).tostype(stype)
             assert_allclose(fn(lhs, rhs), fn(lhs_nd, rhs_nd).asnumpy(), 
rtol=1e-4, atol=1e-4)
+            assert_allclose(fn(lhs, lhs), fn(lhs_nd, lhs_nd).asnumpy(), 
rtol=1e-4, atol=1e-4)
 
     stypes = ['row_sparse', 'csr']
     for stype in stypes:


 

----------------------------------------------------------------
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

Reply via email to