|
| 1 | +import graphblas as gb |
| 2 | +from typing import TYPE_CHECKING, Optional, Union, Hashable |
| 3 | +import functools as ft |
| 4 | +import torch |
| 5 | +from torch_sparse import SparseTensor |
| 6 | +import warnings |
| 7 | + |
| 8 | +if TYPE_CHECKING: |
| 9 | + from graph_diffusers._typing import _GraphblasModule as gb |
| 10 | + from numpy.typing import DTypeLike |
| 11 | + |
| 12 | +# TODO check if torch_sparse installed |
| 13 | + |
| 14 | + |
| 15 | +def torch_to_graphblas( |
| 16 | + edge_index: torch.Tensor, |
| 17 | + *, |
| 18 | + num_nodes: Optional[int] = None, |
| 19 | + weighted: bool = False, |
| 20 | + dtype: "Optional[DTypeLike]" = None, |
| 21 | +) -> gb.Matrix: |
| 22 | + if isinstance(edge_index, SparseTensor): |
| 23 | + return torch_sparse_tensor_to_graphblas(edge_index, weighted=weighted, dtype=dtype) |
| 24 | + if edge_index.is_sparse_csr: |
| 25 | + return torch_sparse_csr_to_graphblas(edge_index, weighted=weighted, dtype=dtype) |
| 26 | + return torch_edge_index_to_graphblas(edge_index, num_nodes=num_nodes, dtype=dtype) |
| 27 | + |
| 28 | + |
| 29 | +def torch_sparse_csr_to_graphblas( |
| 30 | + adj_t: torch.Tensor, *, weighted: bool = False, dtype: "Optional[DTypeLike]" = None |
| 31 | +) -> gb.Matrix: |
| 32 | + if not isinstance(dtype, Hashable): |
| 33 | + warnings.warn( |
| 34 | + f"Unhashable dtype {dtype} passed when converting from torch to graphblas." "The result will not be cached." |
| 35 | + ) |
| 36 | + return _torch_edge_index_to_graphblas.__wrapped__(adj_t, weighted=weighted, dtype=dtype) |
| 37 | + return _torch_sparse_csr_to_graphblas(adj_t, weighted=weighted, dtype=dtype) |
| 38 | + |
| 39 | + |
| 40 | +def torch_sparse_tensor_to_graphblas( |
| 41 | + adj_t: SparseTensor, *, weighted: bool = False, dtype: "Optional[DTypeLike]" = None |
| 42 | +) -> gb.Matrix: |
| 43 | + return torch_sparse_csr_to_graphblas( |
| 44 | + adj_t.to_torch_sparse_csr_tensor(), |
| 45 | + weighted=weighted, |
| 46 | + dtype=dtype, |
| 47 | + ) |
| 48 | + |
| 49 | + |
| 50 | +def torch_edge_index_to_graphblas( |
| 51 | + edge_index: Union[torch.Tensor, SparseTensor], |
| 52 | + *, |
| 53 | + num_nodes: Optional[int] = None, |
| 54 | + dtype: "Optional[DTypeLike]" = None, |
| 55 | +) -> gb.Matrix: |
| 56 | + if not isinstance(dtype, Hashable): |
| 57 | + warnings.warn( |
| 58 | + f"Unhashable dtype {dtype} passed when converting from torch to graphblas." "The result will not be cached." |
| 59 | + ) |
| 60 | + return _torch_edge_index_to_graphblas.__wrapped__(edge_index, num_nodes=num_nodes, dtype=dtype) |
| 61 | + return _torch_edge_index_to_graphblas(edge_index, num_nodes=num_nodes, dtype=dtype) |
| 62 | + |
| 63 | + |
| 64 | +@ft.lru_cache(maxsize=1) |
| 65 | +def _torch_sparse_csr_to_graphblas( |
| 66 | + adj_t: torch.Tensor, |
| 67 | + weighted: bool, |
| 68 | + dtype: "Optional[DTypeLike]", |
| 69 | +) -> gb.Matrix: |
| 70 | + if not adj_t.is_sparse_csr: |
| 71 | + adj_t = adj_t.to_sparse_csr() |
| 72 | + return gb.Matrix.from_csr( |
| 73 | + indptr=adj_t.crow_indices().detach().cpu().numpy(), |
| 74 | + col_indices=adj_t.col_indices().detach().cpu().numpy(), |
| 75 | + values=1.0 if not weighted else adj_t.values().detach().cpu().numpy(), |
| 76 | + nrows=adj_t.shape[0], |
| 77 | + ncols=adj_t.shape[0], |
| 78 | + dtype=dtype, |
| 79 | + ) |
| 80 | + |
| 81 | + |
| 82 | +@ft.lru_cache(maxsize=1) |
| 83 | +def _torch_edge_index_to_graphblas( |
| 84 | + edge_index: torch.Tensor, |
| 85 | + num_nodes: Optional[int], |
| 86 | + dtype: "Optional[DTypeLike]", |
| 87 | +) -> gb.Matrix: |
| 88 | + return gb.Matrix.from_coo(*edge_index, dtype=dtype, nrows=num_nodes, ncols=num_nodes) |
0 commit comments