cylinbao commented on a change in pull request #4369: [Runtime] Add cusparse 
for sparse dense
URL: https://github.com/apache/incubator-tvm/pull/4369#discussion_r347792620
 
 

 ##########
 File path: topi/tests/python/test_topi_sparse.py
 ##########
 @@ -218,22 +220,42 @@ def test_dense():
 
 
 def test_sparse_dense_csr():
-    M, N, K, density = 1, 17, 47, 0.2
-    X_np = np.random.randn(M, K).astype("float32")
+    M, N, K, density = 1, 100, 128, 0.1
+    X_np = np.random.uniform(size=(M, K)).astype("float32")
     W_sp_np = sp.random(N, K, density=density, format='csr', dtype="float32")
-    W_np = W_sp_np.todense()
+    W_np = W_sp_np.todense().astype("float32")
     Y_np = X_np.dot(W_np.T)
 
-    W_data = tvm.placeholder(shape=W_sp_np.data.shape, 
dtype=str(W_sp_np.data.dtype))
-    W_indices = tvm.placeholder(shape=W_sp_np.indices.shape, 
dtype=str(W_sp_np.indices.dtype))
-    W_indptr = tvm.placeholder(shape=W_sp_np.indptr.shape, 
dtype=str(W_sp_np.indptr.dtype))
     X = tvm.placeholder(shape=X_np.shape, dtype=str(X_np.dtype))
-    Y = topi.nn.sparse_dense(X, W_data, W_indices, W_indptr)
-    s = tvm.create_schedule(Y.op)
-    func = tvm.build(s, [X, W_data, W_indices, W_indptr, Y])
-    Y_tvm = tvm.ndarray.array(np.zeros(Y_np.shape, dtype=Y_np.dtype))
-    func(tvm.ndarray.array(X_np), tvm.ndarray.array(W_sp_np.data), 
tvm.ndarray.array(W_sp_np.indices), tvm.ndarray.array(W_sp_np.indptr), Y_tvm)
-    tvm.testing.assert_allclose(Y_tvm.asnumpy(), Y_np, atol=1e-4, rtol=1e-4)
+    W_data = tvm.placeholder(shape=W_sp_np.data.shape, 
+                             dtype=str(W_sp_np.data.dtype))
+    W_indices = tvm.placeholder(shape=W_sp_np.indices.shape, 
+                                dtype=str(W_sp_np.indices.dtype))
+    W_indptr = tvm.placeholder(shape=W_sp_np.indptr.shape, 
+                               dtype=str(W_sp_np.indptr.dtype))
+    def check_device(device):
+        ctx = tvm.context(device, 0)
+        if not ctx.exist:
+            print("Skip because %s is not enabled" % device)
+            return
+        print("Running on target: %s" % device)
+
+        with tvm.target.create(device):
+            Y = topi.nn.sparse_dense(X, W_data, W_indices, W_indptr)
+            s = topi.generic.schedule_sparse_dense([Y])
+
+        func = tvm.build(s, [X, W_data, W_indices, W_indptr, Y], 
+                         device)
+        Y_tvm = tvm.ndarray.array(np.zeros(Y_np.shape, dtype=Y_np.dtype), ctx)
+        func(tvm.ndarray.array(X_np, ctx), 
+             tvm.ndarray.array(W_sp_np.data, ctx), 
+             tvm.ndarray.array(W_sp_np.indices, ctx), 
+             tvm.ndarray.array(W_sp_np.indptr, ctx), 
+             Y_tvm)
+        tvm.testing.assert_allclose(Y_tvm.asnumpy(), Y_np, atol=1e-4, 
rtol=1e-4)
+
+    for device in ["llvm", "cuda", "cuda -libs=cusparse"]:
+        check_device(device)
 
 Review comment:
   @tmoreau89 I do include the test for the cusparse in this pr. It's tested 
here and bundles with original topi.nn.sparse_dense() test.
   Do you still suggest a separate unit test in a following pr? 
   Since cuSPARSE comes with normal CUDA, I guess there is no need to update 
the CI dockerfile.

----------------------------------------------------------------
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

Reply via email to