MCPcopy Index your code
hub / github.com/dmlc/dgl / rand_coo

Function rand_coo

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

Source from the content-addressed store, hash-verified

24
25
26def rand_coo(shape, nnz, dev, nz_dim=None):
27 # Create a sparse matrix without duplicate entries.
28 nnzid = np.random.choice(shape[0] * shape[1], nnz, replace=False)
29 nnzid = torch.tensor(nnzid, device=dev).long()
30 row = torch.div(nnzid, shape[1], rounding_mode="floor")
31 col = nnzid % shape[1]
32 if nz_dim is None:
33 val = torch.randn(nnz, device=dev, requires_grad=True)
34 else:
35 val = torch.randn(nnz, nz_dim, device=dev, requires_grad=True)
36 indices = torch.stack([row, col])
37 indices = rand_stride(indices)
38 val = rand_stride(val)
39 return spmatrix(indices, val, shape)
40
41
42def rand_csr(shape, nnz, dev, nz_dim=None):

Callers 1

test_sp_broadcast_vFunction · 0.85

Calls 3

spmatrixFunction · 0.90
rand_strideFunction · 0.85
longMethod · 0.45

Tested by 1

test_sp_broadcast_vFunction · 0.68