eric-haibin-lin commented on a change in pull request #17569: Adding sparse 
support to MXTensor for custom operators
URL: https://github.com/apache/incubator-mxnet/pull/17569#discussion_r394642575
 
 

 ##########
 File path: example/extensions/lib_custom_op/transposecsr_lib.cc
 ##########
 @@ -0,0 +1,197 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ * Copyright (c) 2020 by Contributors
+ * \file transsparse_lib.cc
+ * \brief Sample 2D transpose custom operator.
+ */
+
+#include <iostream>
+#include "lib_api.h"
+
+void transpose(MXTensor src, MXTensor dst, OpResource res) {
+  MXSparse* A = src.data<MXSparse>();
+  MXSparse* B = dst.data<MXSparse>(); 
+  std::vector<int64_t> shape = src.shape;
+  int64_t h = shape[0];
+  int64_t w = shape[1];
+  if(src.stype == kCSRStorage) {
+    float *Aval = (float*) (A->data);
+    // Here we need one more element to help calculate index(line 57).
+    std::vector<int64_t> rowPtr(w + 2, 0);
+    // count column
+    for(int i = 0; i < A->data_len; i++) {
+      rowPtr[A->indices[i] + 2]++;
+    }
+    // Accumulated sum. After this for loop, rowPtr[1:w+2) stores the correct 
+    // result of transposed rowPtr.
+    for(int i = 2; i < rowPtr.size(); i++) {
+      rowPtr[i] += rowPtr[i - 1];
+    }
+    
+    // Alloc memory for sparse data, where 0 is the index
+    // of B in output vector.
+    res.alloc_sparse(B, 0, A->data_len, w + 1);
 
 Review comment:
   can we use enum instead of 0? 

----------------------------------------------------------------
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:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to