ANSHUMAN87 commented on a change in pull request #6685: URL: https://github.com/apache/incubator-tvm/pull/6685#discussion_r508585845
########## File path: python/tvm/relay/frontend/tensorflow.py ########## @@ -890,6 +890,44 @@ def _impl(inputs, attr, params, mod): return _impl +def _sparse_tensor_dense_matmul(): + # Sparse utility from Numpy + from scipy import sparse + + def _impl(inputs, attr, params, mod): + assert len(inputs) == 4, "There should be 4 input tensors" + + indices_tensor = _infer_value(inputs[0], params, mod).asnumpy() + values_tensor = _infer_value(inputs[1], params, mod).asnumpy() + dense_shape_tensor = _infer_value(inputs[2], params, mod).asnumpy() + + data = inputs[3] + + rows = [x[0] for x in indices_tensor] + cols = [x[1] for x in indices_tensor] + + # Create Numpy sparse Tensor(CSR) + weight_sp = sparse.csr_matrix( + (values_tensor, (rows, cols)), shape=tuple(dense_shape_tensor.tolist()) + ) + weight_sp = sparse.csr_matrix(weight_sp.transpose()) + + weight_data = _expr.const(weight_sp.data, weight_sp.data.dtype) + weight_indptrs = _expr.const(weight_sp.indptr, weight_sp.indptr.dtype) + weight_indices = _expr.const(weight_sp.indices, weight_sp.indices.dtype) + + ret = _op.nn.sparse_dense(data, [weight_data, weight_indices, weight_indptrs]) Review comment: I think there is slight misunderstanding here. Please allow me to put a better picture here, then we can discuss further. Tensorflow Sparse Dense Matmul: `tf.sparse.sparse_dense_matmul(A, B, adjoint_a, adjoint_b) , where both A and B can be Sparse or Dense.` So based on combination of input types, the adjoint option will be set. But internal matmul is always in order of A(Sparse) * B(Dense). This is implemented in Tensorflow in order to reuse the existing internal Op implemented. For example: When i feed D (Dense) and S (Sparse) as in our case(TVM currently support only this order), Tensorflow pseudo-code works as follows: (S^T * D^T )^T => D * S As a side note: the last transpose i added in frontend, is to counter the transpose Op added in the Tensorflow graph subsequently. I know its little confusing, but it would make sense once we workout an example based on above. Please let me know in case any further query. Thanks! ---------------------------------------------------------------- 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