Performs matrix dtype and/or device conversion. If the target device and dtype are already in use, the original matrix will be returned. Parameters ---------- device : torch.device, optional The target device of the matrix if provided, otherwise the curre
(self, device=None, dtype=None)
| 247 | return SparseMatrix(self.c_sparse_matrix.transpose()) |
| 248 | |
| 249 | def to(self, device=None, dtype=None): |
| 250 | """Performs matrix dtype and/or device conversion. If the target device |
| 251 | and dtype are already in use, the original matrix will be returned. |
| 252 | |
| 253 | Parameters |
| 254 | ---------- |
| 255 | device : torch.device, optional |
| 256 | The target device of the matrix if provided, otherwise the current |
| 257 | device will be used |
| 258 | dtype : torch.dtype, optional |
| 259 | The target data type of the matrix values if provided, otherwise the |
| 260 | current data type will be used |
| 261 | |
| 262 | Returns |
| 263 | ------- |
| 264 | SparseMatrix |
| 265 | The converted matrix |
| 266 | |
| 267 | Examples |
| 268 | -------- |
| 269 | |
| 270 | >>> indices = torch.tensor([[1, 1, 2], [1, 2, 0]]) |
| 271 | >>> A = dglsp.spmatrix(indices, shape=(3, 4)) |
| 272 | >>> A.to(device="cuda:0", dtype=torch.int32) |
| 273 | SparseMatrix(indices=tensor([[1, 1, 2], |
| 274 | [1, 2, 0]], device='cuda:0'), |
| 275 | values=tensor([1, 1, 1], device='cuda:0', |
| 276 | dtype=torch.int32), |
| 277 | shape=(3, 4), nnz=3) |
| 278 | """ |
| 279 | if device is None: |
| 280 | device = self.device |
| 281 | if dtype is None: |
| 282 | dtype = self.dtype |
| 283 | |
| 284 | if device == self.device and dtype == self.dtype: |
| 285 | return self |
| 286 | elif device == self.device: |
| 287 | return val_like(self, self.val.to(dtype=dtype)) |
| 288 | else: |
| 289 | # TODO(#5119): Find a better moving strategy instead of always |
| 290 | # convert to COO format. |
| 291 | row, col = self.coo() |
| 292 | row = row.to(device=device) |
| 293 | col = col.to(device=device) |
| 294 | val = self.val.to(device=device, dtype=dtype) |
| 295 | return from_coo(row, col, val, self.shape) |
| 296 | |
| 297 | def cuda(self): |
| 298 | """Moves the matrix to GPU. If the matrix is already on GPU, the |
no test coverage detected