[GitHub] haojin2 commented on a change in pull request #10550: [MXNET-320] Support elemwise_add/sub between dense and csr tensors

2018-04-19 Thread GitBox
haojin2 commented on a change in pull request #10550: [MXNET-320] Support 
elemwise_add/sub between dense and csr tensors
URL: https://github.com/apache/incubator-mxnet/pull/10550#discussion_r182915603
 
 

 ##
 File path: src/operator/tensor/elemwise_binary_op.h
 ##
 @@ -305,6 +316,59 @@ class ElemwiseBinaryOp : public OpBase {
 return dispatched;
   }
 
+
+  /*!
+   * \brief Allow one of the inputs to be dense and produce a dense output
 
 Review comment:
   Done


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


[GitHub] haojin2 commented on a change in pull request #10550: [MXNET-320] Support elemwise_add/sub between dense and csr tensors

2018-04-19 Thread GitBox
haojin2 commented on a change in pull request #10550: [MXNET-320] Support 
elemwise_add/sub between dense and csr tensors
URL: https://github.com/apache/incubator-mxnet/pull/10550#discussion_r182915595
 
 

 ##
 File path: src/operator/tensor/elemwise_binary_op-inl.h
 ##
 @@ -374,6 +374,87 @@ void ElemwiseBinaryOp::CsrCsrOp(mshadow::Stream *s,
   }
 }
 
+/*!
+ * \brief Kernel for performing elemwise op between dense and csr matrix
+ * \param iglobal thread id
+ * \param req  type of request
+ * \param out  output array
+ * \param dns_data data array of dense input
+ * \param csr_data data array of csr input
+ * \param csr_indices  indices array of csr input
+ * \param csr_indptr   indptr array of csr input
+ * \param num_rows number of rows of both inputs
+ * \param num_cols number of columns of both inputs
+ */
+template
+struct ElemwiseDnsCsrDnsKernel {
+  template
+  static void inline Map(int i, OpReqType req, DType* out, DType* dns_data, 
const DType* csr_data,
+ const IType* csr_indices, const CType* csr_indptr,
+ const nnvm::dim_t num_rows, const nnvm::dim_t 
num_cols) {
+if (i < num_rows) {
+  for (int j = csr_indptr[i]; j < csr_indptr[i+1]; ++j) {
+KERNEL_ASSIGN(out[i * num_cols + csr_indices[j]], req,
+  OP::Map(dns_data[i * num_cols + csr_indices[j]], 
csr_data[j]));
+  }
+}
+  }
+};
+
+/*! \brief DNS -op- CSR binary operator for non-canonical NDArray */
+template
+void ElemwiseBinaryOp::DnsCsrDnsOp(mshadow::Stream *s,
+   const nnvm::NodeAttrs ,
+   const OpContext ,
+   const NDArray ,
+   const NDArray ,
+   const OpReqType req,
+   const NDArray ,
+   const bool reverse) {
+  using namespace mshadow;
+  using namespace mxnet_op;
+  CHECK_EQ(dns.storage_type(), kDefaultStorage);
+  CHECK_EQ(csr.storage_type(), kCSRStorage);
+  CHECK(req != kAddTo);
+  CHECK(req != kNullOp);
+  const nnvm::dim_t num_csr_rows = csr.shape()[0];
 
 Review comment:
   Done


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


[GitHub] haojin2 commented on a change in pull request #10550: [MXNET-320] Support elemwise_add/sub between dense and csr tensors

2018-04-19 Thread GitBox
haojin2 commented on a change in pull request #10550: [MXNET-320] Support 
elemwise_add/sub between dense and csr tensors
URL: https://github.com/apache/incubator-mxnet/pull/10550#discussion_r182915599
 
 

 ##
 File path: src/operator/tensor/elemwise_binary_op_basic.cc
 ##
 @@ -166,6 +168,8 @@ The storage type of ``elemwise_sub`` output depends on 
storage types of inputs
 
- elemwise_sub(row_sparse, row_sparse) = row_sparse
- elemwise_sub(csr, csr) = csr
+   - elemwise_sub(dns, csr) = dns
 
 Review comment:
   Done


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


[GitHub] haojin2 commented on a change in pull request #10550: [MXNET-320] Support elemwise_add/sub between dense and csr tensors

2018-04-19 Thread GitBox
haojin2 commented on a change in pull request #10550: [MXNET-320] Support 
elemwise_add/sub between dense and csr tensors
URL: https://github.com/apache/incubator-mxnet/pull/10550#discussion_r182837308
 
 

 ##
 File path: src/operator/elemwise_op_common.h
 ##
 @@ -102,6 +102,48 @@ inline bool ElemwiseStorageType(const nnvm::NodeAttrs& 
attrs,
  in_attrs, out_attrs);
 }
 
+template
+inline bool ElemwisePreferDenseStorageType(const nnvm::NodeAttrs& attrs,
+   const int dev_mask,
+   DispatchMode* dispatch_mode,
+   std::vector *in_attrs,
+   std::vector *out_attrs) {
+  using namespace common;
+  CHECK_EQ(in_attrs->size(), 2);
+  CHECK_EQ(out_attrs->size(), 1);
+  const auto lhs_stype = (*in_attrs)[0];
+  const auto rhs_stype = (*in_attrs)[1];
+  bool dispatched = false;
+  const bool invalid_ctx = cpu_only && dev_mask != mshadow::cpu::kDevMask;
+  const auto dispatch_ex = invalid_ctx ? DispatchMode::kFComputeFallback :
+ DispatchMode::kFComputeEx;
+  if (!dispatched && common::ContainsOnlyStorage(*in_attrs, kDefaultStorage)) {
+// dns, dns ... -> dns
+dispatched = storage_type_assign(out_attrs, kDefaultStorage,
+ dispatch_mode, DispatchMode::kFCompute);
+  }
+  if (!dispatched && rsp && ContainsOnlyStorage(*in_attrs, kRowSparseStorage)) 
{
+// rsp, rsp, ... -> rsp
+dispatched = storage_type_assign(out_attrs, kRowSparseStorage,
+ dispatch_mode, dispatch_ex);
+  }
+  if (!dispatched && csr && common::ContainsOnlyStorage(*in_attrs, 
kCSRStorage)) {
 
 Review comment:
   Done


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


[GitHub] haojin2 commented on a change in pull request #10550: [MXNET-320] Support elemwise_add/sub between dense and csr tensors

2018-04-19 Thread GitBox
haojin2 commented on a change in pull request #10550: [MXNET-320] Support 
elemwise_add/sub between dense and csr tensors
URL: https://github.com/apache/incubator-mxnet/pull/10550#discussion_r182837408
 
 

 ##
 File path: src/operator/elemwise_op_common.h
 ##
 @@ -102,6 +102,48 @@ inline bool ElemwiseStorageType(const nnvm::NodeAttrs& 
attrs,
  in_attrs, out_attrs);
 }
 
+template
+inline bool ElemwisePreferDenseStorageType(const nnvm::NodeAttrs& attrs,
 
 Review comment:
   Done


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


[GitHub] haojin2 commented on a change in pull request #10550: [MXNET-320] Support elemwise_add/sub between dense and csr tensors

2018-04-19 Thread GitBox
haojin2 commented on a change in pull request #10550: [MXNET-320] Support 
elemwise_add/sub between dense and csr tensors
URL: https://github.com/apache/incubator-mxnet/pull/10550#discussion_r182837353
 
 

 ##
 File path: src/operator/elemwise_op_common.h
 ##
 @@ -102,6 +102,48 @@ inline bool ElemwiseStorageType(const nnvm::NodeAttrs& 
attrs,
  in_attrs, out_attrs);
 }
 
+template
+inline bool ElemwisePreferDenseStorageType(const nnvm::NodeAttrs& attrs,
+   const int dev_mask,
+   DispatchMode* dispatch_mode,
+   std::vector *in_attrs,
+   std::vector *out_attrs) {
+  using namespace common;
+  CHECK_EQ(in_attrs->size(), 2);
+  CHECK_EQ(out_attrs->size(), 1);
+  const auto lhs_stype = (*in_attrs)[0];
+  const auto rhs_stype = (*in_attrs)[1];
+  bool dispatched = false;
+  const bool invalid_ctx = cpu_only && dev_mask != mshadow::cpu::kDevMask;
+  const auto dispatch_ex = invalid_ctx ? DispatchMode::kFComputeFallback :
+ DispatchMode::kFComputeEx;
+  if (!dispatched && common::ContainsOnlyStorage(*in_attrs, kDefaultStorage)) {
 
 Review comment:
   Done


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


[GitHub] haojin2 commented on a change in pull request #10550: [MXNET-320] Support elemwise_add/sub between dense and csr tensors

2018-04-19 Thread GitBox
haojin2 commented on a change in pull request #10550: [MXNET-320] Support 
elemwise_add/sub between dense and csr tensors
URL: https://github.com/apache/incubator-mxnet/pull/10550#discussion_r182837286
 
 

 ##
 File path: src/operator/elemwise_op_common.h
 ##
 @@ -102,6 +102,48 @@ inline bool ElemwiseStorageType(const nnvm::NodeAttrs& 
attrs,
  in_attrs, out_attrs);
 }
 
+template
+inline bool ElemwisePreferDenseStorageType(const nnvm::NodeAttrs& attrs,
+   const int dev_mask,
+   DispatchMode* dispatch_mode,
+   std::vector *in_attrs,
+   std::vector *out_attrs) {
+  using namespace common;
+  CHECK_EQ(in_attrs->size(), 2);
+  CHECK_EQ(out_attrs->size(), 1);
+  const auto lhs_stype = (*in_attrs)[0];
+  const auto rhs_stype = (*in_attrs)[1];
+  bool dispatched = false;
+  const bool invalid_ctx = cpu_only && dev_mask != mshadow::cpu::kDevMask;
+  const auto dispatch_ex = invalid_ctx ? DispatchMode::kFComputeFallback :
+ DispatchMode::kFComputeEx;
+  if (!dispatched && common::ContainsOnlyStorage(*in_attrs, kDefaultStorage)) {
+// dns, dns ... -> dns
+dispatched = storage_type_assign(out_attrs, kDefaultStorage,
+ dispatch_mode, DispatchMode::kFCompute);
+  }
+  if (!dispatched && rsp && ContainsOnlyStorage(*in_attrs, kRowSparseStorage)) 
{
+// rsp, rsp, ... -> rsp
+dispatched = storage_type_assign(out_attrs, kRowSparseStorage,
+ dispatch_mode, dispatch_ex);
+  }
+  if (!dispatched && csr && common::ContainsOnlyStorage(*in_attrs, 
kCSRStorage)) {
+// csr, csr, ... -> csr
+dispatched = storage_type_assign(out_attrs, kCSRStorage,
+ dispatch_mode, dispatch_ex);
+  }
+  if (!dispatched && ((lhs_stype == kDefaultStorage && rhs_stype == 
kCSRStorage) ||
+  (lhs_stype == kCSRStorage && rhs_stype == 
kDefaultStorage))) {
+// dense, csr -> csr / csr, dense -> csr
 
 Review comment:
   Was a type, done


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


[GitHub] haojin2 commented on a change in pull request #10550: [MXNET-320] Support elemwise_add/sub between dense and csr tensors

2018-04-19 Thread GitBox
haojin2 commented on a change in pull request #10550: [MXNET-320] Support 
elemwise_add/sub between dense and csr tensors
URL: https://github.com/apache/incubator-mxnet/pull/10550#discussion_r182837202
 
 

 ##
 File path: src/operator/tensor/elemwise_binary_op-inl.h
 ##
 @@ -374,6 +374,72 @@ void ElemwiseBinaryOp::CsrCsrOp(mshadow::Stream *s,
   }
 }
 
+template
+struct ElemwiseDnsZeroKernel {
+  template
+  static void inline Map(int i, const OpReqType req, DType* out, const DType* 
dns_data,
+ const nnvm::dim_t num_rows, const nnvm::dim_t 
num_cols) {
+if (i < num_rows*num_cols) {
+  KERNEL_ASSIGN(out[i], req, OP::Map(dns_data[i], DType(0.0f)));
+}
+  }
+};
+
+template
+struct ElemwiseDnsCsrDnsKernel {
+  template
+  static void inline Map(int i, const OpReqType req, DType* out, DType* 
dns_data,
+ const DType* csr_data, const IType* csr_indices, 
const CType* csr_indptr,
+ const nnvm::dim_t num_rows, const nnvm::dim_t 
num_cols) {
+if (i < num_rows) {
+  for (int j = csr_indptr[i]; j < csr_indptr[i+1]; ++j) {
+KERNEL_ASSIGN(out[i * num_cols + csr_indices[j]], req,
+  OP::Map(dns_data[i * num_cols + csr_indices[j]], 
csr_data[j]));
+  }
+}
+  }
+};
+
+/*! \brief DNS -op- CSR binary operator for non-canonical NDArray */
 
 Review comment:
   Done


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


[GitHub] haojin2 commented on a change in pull request #10550: [MXNET-320] Support elemwise_add/sub between dense and csr tensors

2018-04-19 Thread GitBox
haojin2 commented on a change in pull request #10550: [MXNET-320] Support 
elemwise_add/sub between dense and csr tensors
URL: https://github.com/apache/incubator-mxnet/pull/10550#discussion_r182837118
 
 

 ##
 File path: src/operator/tensor/elemwise_binary_op-inl.h
 ##
 @@ -374,6 +374,72 @@ void ElemwiseBinaryOp::CsrCsrOp(mshadow::Stream *s,
   }
 }
 
+template
+struct ElemwiseDnsZeroKernel {
+  template
+  static void inline Map(int i, const OpReqType req, DType* out, const DType* 
dns_data,
+ const nnvm::dim_t num_rows, const nnvm::dim_t 
num_cols) {
+if (i < num_rows*num_cols) {
+  KERNEL_ASSIGN(out[i], req, OP::Map(dns_data[i], DType(0.0f)));
+}
+  }
+};
+
+template
+struct ElemwiseDnsCsrDnsKernel {
+  template
+  static void inline Map(int i, const OpReqType req, DType* out, DType* 
dns_data,
+ const DType* csr_data, const IType* csr_indices, 
const CType* csr_indptr,
+ const nnvm::dim_t num_rows, const nnvm::dim_t 
num_cols) {
+if (i < num_rows) {
+  for (int j = csr_indptr[i]; j < csr_indptr[i+1]; ++j) {
+KERNEL_ASSIGN(out[i * num_cols + csr_indices[j]], req,
+  OP::Map(dns_data[i * num_cols + csr_indices[j]], 
csr_data[j]));
+  }
+}
+  }
+};
+
+/*! \brief DNS -op- CSR binary operator for non-canonical NDArray */
+template
+void ElemwiseBinaryOp::DnsCsrDnsOp(mshadow::Stream *s,
+   const nnvm::NodeAttrs ,
+   const OpContext ,
+   const NDArray ,
+   const NDArray ,
+   const OpReqType req,
+   const NDArray ,
+   const bool reverse) {
+  using namespace mshadow;
+  using namespace mxnet_op;
+  CHECK_EQ(dns.storage_type(), kDefaultStorage);
+  CHECK_EQ(csr.storage_type(), kCSRStorage);
+  const nnvm::dim_t num_csr_rows = csr.shape()[0];
+  const nnvm::dim_t num_csr_cols = csr.shape()[1];
+  mxnet_op::Kernel::Launch(
+s, output.data().Size(), req, output.data().dptr(), 
dns.data().dptr(),
+num_csr_rows, num_csr_cols);
+  TBlob csr_data = csr.data();
+  TBlob csr_indices = csr.aux_data(csr::kIdx);
+  TBlob csr_indptr = csr.aux_data(csr::kIndPtr);
+  MSHADOW_SGL_DBL_TYPE_SWITCH(csr_data.type_flag_, DataType, {
 
 Review comment:
   Done with this function, will discuss with you on the refactor later


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