samskalicky 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_r387402708
##########
File path: include/mxnet/lib_api.h
##########
@@ -1193,19 +1312,57 @@ extern "C" {
const int64_t** outshapes, int* outdims, void**
outdata, int* outtypes,
size_t* outIDs, const char** outdev_type, int*
outdev_id, int num_out,
xpu_malloc_t cpu_malloc, void* cpu_alloc,
- xpu_malloc_t gpu_malloc, void* gpu_alloc, void*
stream) {
+ xpu_malloc_t gpu_malloc, void* gpu_alloc, void*
stream,
+ void** in_indices, void** in_indptr,
+ int64_t* in_indices_shapes, int64_t*
in_indptr_shapes,
+ std::vector<std::vector<float>>& tmp_data,
+ std::vector<std::vector<int64_t>>& col_idx,
+ std::vector<std::vector<int64_t>>& row_ptr) {
// create a vector of tensors for inputs
std::vector<MXTensor> inputs(num_in);
+ // create a vector for sparse inputs
+ std::vector<MXInSparse> in_sparse(num_in);
+
for (int i = 0; i < num_in; i++) {
- inputs[i].setTensor(indata[i], (MXDType)intypes[i], inshapes[i],
indims[i],
- inIDs[i], {indev_type[i], indev_id[i]});
+ // Dense representation.
+ if(!in_indices_shapes) {
+ inputs[i].setTensor(indata[i], (MXDType)intypes[i], inshapes[i],
indims[i],
+ inIDs[i], {indev_type[i], indev_id[i]},
kDefaultStorage);
+ }
+ // Sparse representation.
+ else {
+ MXStorageType type;
+ if(!in_indptr_shapes) {
+ type = kRowSparseStorage;
+ in_sparse[i].set(indata[i], inshapes[i], indims[i], in_indices[i],
in_indices_shapes[i]);
Review comment:
In c_api.cc you only push sparse in_indices conditionally:
```
if(inputs[i].storage_type() == mxnet::kRowSparseStorage) {
in_indices.push_back(inputs[i].aux_data(rowsparse::kIdx).dptr_);
in_indices_shapes.push_back(inputs[i].aux_shape(rowsparse::kIdx).Size());
}
```
But here you assume that the tensor index can be used to pull out entries.
For cases where some tensors are sparse and others are dense this wont work and
you get segfault possibly indexing outside of the `in_indices` range.
----------------------------------------------------------------
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