(shape, nnz, dev, nz_dim=None)
| 62 | |
| 63 | |
| 64 | def 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 | |
| 86 | def rand_diag(shape, nnz, dev, nz_dim=None): |
nothing calls this directly
no test coverage detected