tkonolige commented on a change in pull request #7435:
URL: https://github.com/apache/tvm/pull/7435#discussion_r579325284
##########
File path: python/tvm/relay/op/nn/nn.py
##########
@@ -2148,6 +2148,54 @@ def sparse_transpose(x):
return expr.TupleWrapper(_make.sparse_transpose(x[0], x[1], x[2]), 3)
+# pylint: disable=no-else-return,inconsistent-return-statements
+def sparse_add(dense_mat, sparse_mat):
+ r"""
+ Computes the matrix addition of `dense_mat` and `sparse_mat`, where
`dense_mat` is
+ a dense matrix and `sparse_mat` is a sparse (either BSR or CSR) namedtuple
with
+ fields `data`, `indices`, and `indptr`.
+
+ .. math::
+
+ \mbox{sparse_add}(dense_mat, sparse_mat)[m, n] =
\mbox{add}(\mbox{as_dense}(S), (D))[m, n]
+
+ where `as_dense` returns dense equivalent of the given S(sparse matrix)
+ while performing addition with given D(dense matrix).
+
+ Parameters
+ ----------
+ dense_mat : tvm.relay.Expr
+ The input dense matrix for the matrix multiplication
+
+ sparse_mat : Union[namedtuple, Tuple[ndarray, ndarray, ndarray]].
+ The input sparse matrix for the matrix multiplication.
+
+ Returns
+ -------
+ result: tvm.relay.Expr
+ The computed result.
+
+ Examples
+ -------
+ .. code-block:: python
+ dense_data = [[ 3., 4., 4. ]
+ [ 4., 2., 5. ]]
Review comment:
nit: alignment seems off here.
##########
File path: python/tvm/relay/op/nn/nn.py
##########
@@ -2148,6 +2148,54 @@ def sparse_transpose(x):
return expr.TupleWrapper(_make.sparse_transpose(x[0], x[1], x[2]), 3)
+# pylint: disable=no-else-return,inconsistent-return-statements
+def sparse_add(dense_mat, sparse_mat):
+ r"""
+ Computes the matrix addition of `dense_mat` and `sparse_mat`, where
`dense_mat` is
+ a dense matrix and `sparse_mat` is a sparse (either BSR or CSR) namedtuple
with
+ fields `data`, `indices`, and `indptr`.
+
+ .. math::
+
+ \mbox{sparse_add}(dense_mat, sparse_mat)[m, n] =
\mbox{add}(\mbox{as_dense}(S), (D))[m, n]
+
+ where `as_dense` returns dense equivalent of the given S(sparse matrix)
+ while performing addition with given D(dense matrix).
+
+ Parameters
+ ----------
+ dense_mat : tvm.relay.Expr
+ The input dense matrix for the matrix multiplication
+
+ sparse_mat : Union[namedtuple, Tuple[ndarray, ndarray, ndarray]].
+ The input sparse matrix for the matrix multiplication.
+
+ Returns
+ -------
+ result: tvm.relay.Expr
+ The computed result.
+
+ Examples
+ -------
+ .. code-block:: python
+ dense_data = [[ 3., 4., 4. ]
+ [ 4., 2., 5. ]]
+ sparse_data = [4., 8.]
+ sparse_indices =[0, 2]
+ sparse_indptr =[0, 1, 2]
+ dense_shape = [2, 3]
Review comment:
Is this dense_shape necessary?
##########
File path: src/relay/op/nn/sparse.cc
##########
@@ -196,5 +196,44 @@ RELAY_REGISTER_OP("nn.sparse_transpose")
.set_support_level(1)
.add_type_rel("SparseTranspose", SparseTransposeRel);
+// relay.nn.sparse_add
+bool SparseAddRel(const Array<Type>& types, int num_inputs, const Attrs& attrs,
+ const TypeReporter& reporter) {
+ ICHECK_EQ(types.size(), 5);
+ const auto* dense_data = types[0].as<TensorTypeNode>();
+ const auto* sparse_data = types[1].as<TensorTypeNode>();
+ ICHECK(reporter->Assert(sparse_data->dtype == dense_data->dtype));
+ ICHECK(reporter->Assert(sparse_data->shape.size() == 1));
+ const auto* sparse_indices = types[2].as<TensorTypeNode>();
+ ICHECK(reporter->Assert(sparse_indices->shape.size() == 1));
Review comment:
Can you add error messages on all the ICHECKs?
----------------------------------------------------------------
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]