eric-haibin-lin commented on a change in pull request #9625: sparse regression
operators
URL: https://github.com/apache/incubator-mxnet/pull/9625#discussion_r170439526
##########
File path: src/operator/regression_output-inl.h
##########
@@ -121,6 +228,67 @@ void RegressionBackward(const nnvm::NodeAttrs& attrs,
});
}
+
+template<typename xpu, typename BackwardOp>
+inline void RegressionBackwardCSRImpl(mshadow::Stream<xpu> *s,
+ const RegressionOutputParam& param,
+ const OpReqType req,
+ const NDArray &data, const NDArray
&label,
+ const NDArray &data_grad, const bool
sparse_kernel) {
+ using namespace mshadow;
+ using namespace mxnet_op;
+ using namespace csr;
+ const TShape dshape = data.shape();
+ const nnvm::dim_t num_rows = dshape[0];
+ const nnvm::dim_t row_length = dshape[1];
+ CHECK_EQ(label.aux_type(kIndPtr), label.aux_type(kIdx))
+ << "Type of indices array and index pointer array of the label should be
the same";
+ MSHADOW_IDX_TYPE_SWITCH(label.aux_type(kIdx), IType, {
+ MSHADOW_REAL_TYPE_SWITCH(label.dtype(), DType, {
+ MXNET_ASSIGN_REQ_SWITCH(req, Req, {
+ const IType* label_indptr = label.aux_data(kIndPtr).dptr<IType>();
+ const IType* label_idx = label.aux_data(kIdx).dptr<IType>();
+ const DType* label_data = label.data().dptr<DType>();
+ const DType* data_ptr = data.data().dptr<DType>();
+ DType* grad_ptr = data_grad.data().dptr<DType>();
+ if (sparse_kernel) {
+ if (req != kWriteInplace) {
+ Kernel<op_with_req<mshadow_op::identity, Req>, xpu>::Launch(s,
+ dshape.Size(), grad_ptr, data_ptr);
+ }
+ Kernel<DnsCsrSparseKernel<BackwardOp, Req>, xpu>::Launch(s, num_rows,
+ grad_ptr, data_ptr, label_data, label_idx, label_indptr,
row_length);
+ } else {
+ Kernel<DnsCsrKernel<BackwardOp, Req>, xpu>::Launch(s, num_rows,
+ grad_ptr, data_ptr, label_data, label_idx, label_indptr,
row_length);
+ }
+ Kernel<op_with_req<mshadow_op::mul, Req>, xpu>::Launch(s,
dshape.Size(),
+ grad_ptr, grad_ptr, static_cast<DType>(param.grad_scale/row_length));
+ });
+ });
+ });
+}
+
+
+template<typename xpu, typename BackwardOP, bool sparse_kernel>
+void RegressionBackwardEx(const nnvm::NodeAttrs& attrs,
+ const OpContext& ctx,
+ const std::vector<NDArray>& inputs,
+ const std::vector<OpReqType>& req,
+ const std::vector<NDArray>& outputs) {
+ CHECK_EQ(inputs.size(), 2U);
+ CHECK_EQ(outputs.size(), 2U);
+ const RegressionOutputParam& param =
nnvm::get<RegressionOutputParam>(attrs.parsed);
+ auto label_stype = inputs[0].storage_type();
+ mshadow::Stream<xpu> *s = ctx.get_stream<xpu>();
+ if (label_stype == kCSRStorage) {
Review comment:
Also check the stype of other inputs?
----------------------------------------------------------------
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:
[email protected]
With regards,
Apache Git Services