gigl.utils.csr#
Memory-lean COO -> CSR/CSC conversion.
graphlearn_torch.utils.coo_to_csr delegates to torch_sparse.SparseTensor, which builds a
composite sort key, sorts it, and gathers row and col through the permutation while the caller’s
originals are still live. Seven full-size int64 arrays exist at the peak, measured at 7.25x one
int64 array on 400M edges. At a multi-billion-edge partition that is hundreds of GiB and the
conversion, not the graph, is what exhausts the host.
This implementation is a two-pass counting sort: degrees give the exact output layout up front, so the output is allocated once and written in place, and the input is consumed in chunks so transients are bounded by chunk size rather than by edge count:
peak = row + col + indices + optional edge_ids + 2 x (num_rows + 1)
+ O(chunk + max_degree)
which is 1.5x one int64 array with int32 inputs when edge IDs are omitted, since indices
then stays int32, or 2.5x when the int64 edge IDs are retained. The max_degree term comes
from within-row sorting; it matters when one row owns a meaningful fraction of the edges.
CompactTopology wraps the result as a GLT Topology.
Attributes#
Classes#
A GLT |
|
CSR arrays and optional COO-position edge IDs in CSR order. |
Functions#
|
Convert a COO edge index to CSR with bounded chunk and sort temporaries. |
Module Contents#
- class gigl.utils.csr.CompactTopology(edge_index, layout, num_rows=None, *, retain_edge_ids=True)[source]#
Bases:
graphlearn_torch.data.TopologyA GLT
Topologybuilt bybuild_csr_from_coo().Topology.__init__is not called: it would fabricate a fulltorch.arange(num_edges)and convert withcoo_to_csr. Instead, the counting sort writes original COO positions directly into final CSR order when retained. Edge features need explicit IDs and cannot use this class.- Parameters:
edge_index (torch.Tensor) –
[2, num_edges]COO. int32 and int64 are used as they are; anything else is cast to int64, as GLT’sTopologydoes.layout (Literal["CSR", "CSC"]) – The layout GLT samples from.
num_rows (Optional[int]) – Size of the compressed dimension. Defaults to the largest compressed id + 1, as GLT’s
Topologydoes.retain_edge_ids (bool) – Retain COO-position IDs for edge sampling by default, at a cost of 8 bytes per edge. Use
Falseonly when callers sample without edge IDs.
- class gigl.utils.csr.CsrBuildResult[source]#
CSR arrays and optional COO-position edge IDs in CSR order.
- gigl.utils.csr.build_csr_from_coo(row, col, num_rows=None, chunk_size=DEFAULT_CHUNK_SIZE, sort_block_edges=DEFAULT_SORT_BLOCK_EDGES, band_bytes=DEFAULT_BAND_BYTES, retain_edge_ids=True)[source]#
Convert a COO edge index to CSR with bounded chunk and sort temporaries.
Produces the same CSR arrays as
graphlearn_torch.utils.coo_to_csrwithout its full-size sorting temporaries.When
retain_edge_idsis true, write original COO positions in CSR order alongside columns. The final int64 vector is allocated directly; no full-size input ID vector is needed. Explicit edge IDs and edge weights are unsupported.To build CSC instead, pass the column indices as
rowand the row indices ascol.- Parameters:
row (torch.Tensor) – 1-D row indices, int32 or int64.
col (torch.Tensor) – 1-D column indices, same length as
row.num_rows (Optional[int]) – Size of the row dimension;
indptrhasnum_rows + 1entries. Defaults tomax(row) + 1, ascoo_to_csrdoes.chunk_size (int) – Edges per scatter chunk. Bounds the transient working set.
sort_block_edges (int) – Edges per block of the within-row sort, which orders each row’s columns ascending as
coo_to_csrdoes.band_bytes (int) – Destination window for the scatter, used only when
indicescould not be placed in memory.retain_edge_ids (bool) – Retain original COO positions in CSR order by default. The int64 IDs cost 8 bytes per edge. Set to
Falsewhen callers do not sample edge IDs.
- Returns:
indptrof shape[num_rows + 1], always int64, andindicesof shape[num_edges], int32 whencolis int32 and int64 otherwise, plus optional int64edge_idsof shape[num_edges]in CSR order. Empty input returns an empty ID tensor when retention is enabled andNoneotherwise.- Return type:
- Raises:
ValueError – If
rowandcoldisagree in length, a row id is out of range, or the scatter does not exactly fill every row.