MCPcopy
hub / github.com/dmlc/dgl / rand_csc

Function rand_csc

tests/python/pytorch/sparse/utils.py:64–83  ·  view source on GitHub ↗
(shape, nnz, dev, nz_dim=None)

Source from the content-addressed store, hash-verified

62
63
64def rand_csc(shape, nnz, dev, nz_dim=None):
65 # Create a sparse matrix without duplicate entries.
66 nnzid = np.random.choice(shape[0] * shape[1], nnz, replace=False)
67 nnzid = torch.tensor(nnzid, device=dev).long()
68 row = torch.div(nnzid, shape[1], rounding_mode="floor")
69 col = nnzid % shape[1]
70 if nz_dim is None:
71 val = torch.randn(nnz, device=dev, requires_grad=True)
72 else:
73 val = torch.randn(nnz, nz_dim, device=dev, requires_grad=True)
74 indptr = torch.zeros(shape[1] + 1, device=dev, dtype=torch.int64)
75 for c in col.tolist():
76 indptr[c + 1] += 1
77 indptr = torch.cumsum(indptr, 0)
78 col_sorted, col_sorted_idx = torch.sort(col)
79 indices = row[col_sorted_idx]
80 indptr = rand_stride(indptr)
81 indices = rand_stride(indices)
82 val = rand_stride(val)
83 return from_csc(indptr, indices, val, shape=shape)
84
85
86def rand_diag(shape, nnz, dev, nz_dim=None):

Callers

nothing calls this directly

Calls 3

from_cscFunction · 0.90
rand_strideFunction · 0.85
longMethod · 0.45

Tested by

no test coverage detected