You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_sparse.py 3.0 kB

4 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import numpy as np
  2. import scipy.sparse
  3. from hetu import ndarray
  4. from hetu import gpu_links as gpu_op
  5. from hetu import gpu_ops as autodiff
  6. def test_sparse_matrix_multiply():
  7. density = 1e-3
  8. ctx = ndarray.gpu(0)
  9. x = scipy.sparse.rand(500, 7000, density=density,
  10. format='coo', dtype=np.float32)
  11. y = np.random.uniform(0, 10, size=(7000, 100)).astype(np.float32)
  12. mat_x = ndarray.sparse_array(
  13. x.data, (x.row, x.col), shape=[500, 7000], ctx=ctx)
  14. mat_y = ndarray.array(y, ctx=ctx)
  15. mat_z = ndarray.empty((500, 100), ctx=ctx)
  16. gpu_op.CuSparse_Csrmm(mat_x, False, mat_y, False, mat_z)
  17. z = mat_z.asnumpy()
  18. np.testing.assert_allclose(x.dot(y), z, rtol=1e-5)
  19. # # following codes are invalid in cuda
  20. # density = 1e-3
  21. # ctx = ndarray.gpu(0)
  22. # x = scipy.sparse.rand(1000, 500 ,density=density,format='coo',dtype=np.float32)
  23. # y = np.random.uniform(0, 10, size=(2000, 500)).astype(np.float32)
  24. # mat_x = ndarray.sparse_array(x.data, (x.row, x.col), shape = [1000, 500], ctx=ctx)
  25. # mat_y = ndarray.array(y, ctx=ctx)
  26. # mat_z = ndarray.empty((1000, 2000), ctx=ctx)
  27. # gpu_op.CuSparse_Csrmm(mat_x, False, mat_y, True, mat_z)
  28. # z = mat_z.asnumpy()
  29. # np.testing.assert_allclose(x.dot(np.transpose(y)), z, rtol=1e-5)
  30. # x = scipy.sparse.rand(500, 1000, density=density,format='coo',dtype=np.float32)
  31. # y = np.random.uniform(0, 10, size=(2000, 500)).astype(np.float32)
  32. # mat_x = ndarray.sparse_array(x.data, (x.row, x.col), shape = [500, 1000], ctx=ctx)
  33. # mat_y = ndarray.array(y, ctx=ctx)
  34. # mat_z = ndarray.empty((1000, 2000), ctx=ctx)
  35. # gpu_op.CuSparse_Csrmm(mat_x, True, mat_y, True, mat_z)
  36. # z = mat_z.asnumpy()
  37. # np.testing.assert_allclose(x.T.dot(np.transpose(y)), z, rtol=1e-5)
  38. def test_sparse_array_dense_vector_multiply():
  39. density = 1e-3
  40. ctx = ndarray.gpu(0)
  41. x = scipy.sparse.rand(500, 70000, density=density,
  42. format='coo', dtype=np.float32)
  43. y = np.random.uniform(0, 10, size=(70000, 1)).astype(np.float32)
  44. mat_x = ndarray.sparse_array(
  45. x.data, (x.row, x.col), shape=[500, 70000], ctx=ctx)
  46. arr_y = ndarray.array(y, ctx=ctx)
  47. arr_z = ndarray.empty((500, 1), ctx=ctx)
  48. trans = False
  49. gpu_op.CuSparse_Csrmv(mat_x, trans, arr_y, arr_z)
  50. z = arr_z.asnumpy()
  51. np.testing.assert_allclose(x.dot(y), z, rtol=1e-5)
  52. x = scipy.sparse.rand(70000, 500, density=density,
  53. format='coo', dtype=np.float32)
  54. y = np.random.uniform(0, 10, size=(70000, 1)).astype(np.float32)
  55. mat_x = ndarray.sparse_array(
  56. x.data, (x.row, x.col), shape=[70000, 500], ctx=ctx)
  57. arr_y = ndarray.array(y, ctx=ctx)
  58. arr_z = ndarray.empty((500, 1), ctx=ctx)
  59. trans = True
  60. gpu_op.CuSparse_Csrmv(mat_x, trans, arr_y, arr_z)
  61. z = arr_z.asnumpy()
  62. np.testing.assert_allclose(x.transpose().dot(y), z, rtol=1e-5)
  63. test_sparse_matrix_multiply()
  64. test_sparse_array_dense_vector_multiply()

分布式深度学习系统