Skip to content

26 overflow fallback - #126

Open
eriknw wants to merge 1 commit into
python-graphblas:22-assortativityfrom
eriknw:26-overflow-fallback
Open

eriknw wants to merge 1 commit into
python-graphblas:22-assortativityfrom
eriknw:26-overflow-fallback

Conversation

@eriknw

@eriknw eriknw commented Dec 2, 2026 •

Copy link
Copy Markdown
Member

Let NetworkX answer the calls with integer weights that this backend would answer inexactly: where a 64-bit sum could reach 2**63, and where the conversion holds integer weights as floats and the answer is an integer sum, a comparison of weights or the weights themselves. NetworkX's dispatcher tries the next backend only after NotImplementedError, so the overflow checks now raise IntegerOverflowError, an OverflowError that is also a NotImplementedError, and the names that need such ints exact decline with NotImplementedError.

  • Under backend_priority, NetworkX answers exactly, with Python integers, for a NetworkX graph whatever fallback_to_nx says, and for this backend's graphs when fallback_to_nx is on (off, NetworkX raises its own NotImplementedError)
  • With backend="graphblas", NetworkX raises its own NotImplementedError from the IntegerOverflowError, where it raised OverflowError
  • A direct call still raises OverflowError
  • A weighted call that passes a one-shot iterator of nodes to cut_size, volume, average_degree_connectivity, degree_pearson_correlation_coefficient or eccentricity is left to NetworkX, which would otherwise get it already read
  • all_pairs_bellman_ford_path_length and node_degree_xy check their bounds when called rather than at the first item, so NetworkX can still answer, and so can NetworkX's own degree mixing names, which read node_degree_xy
  • Integer weights held as floats: all ints held as floats (an np.uint64 beside a signed int, or 263 and up beside a smaller int), or an int past 253 beside a float. The conversion is kept and marked. The names that add the weights as exact ints, compare them, or give them back decline at their call, before any argument is read, on that conversion or one NetworkX serves from its cache, and NetworkX answers exactly: the path lengths, cut_size, volume, mutual_weight, the Wiener indices, betweenness, edge_boundary with data, degree_mixing_matrix, average_degree_connectivity and the rest. Before this change, a call that only read the weights was served and answered a rounded float.
  • The names with float answers are served on such weights when they are all of one sign, all at least 0 or all at most 0, 27 of them: pagerank, hits, eigenvector and Katz centrality, clustering, closeness and harmonic centrality, average_shortest_path_length, conductance and the expansions, modularity, the degree assortativity coefficients, average_neighbor_degree and the matrix names. NetworkX computes these in floats too, or from exact sums; a float sum of weights of one sign is within a few parts in 253 of the exact one. The tests call each of the 27 on such weights of either sign: 26 are served and within 1e-9 of NetworkX's largest value (hits iterates to a tolerance; the others agree far closer), and pagerank, closeness_centrality and conductance are compared on smaller cases at a relative 1e-12. group_closeness_centrality still declines weights like these, as it is unweighted here. Where the int degrees pass 231, NetworkX's degree_assortativity_coefficient squares them in int64, which wraps, so there it is compared with Pearson's r of the pairs, as NetworkX's degree_pearson_correlation_coefficient gives it. Floats are within tolerance, not NetworkX's bits.
  • Weights of both signs can cancel: with 260 + 4 and -260 on a cut, a float sum gives 0 where NetworkX's exact one gives 4, so edge_expansion would be 0.0 for 4/3, and conductance and normalized_cut_size would divide by 0. The float-answer names decline weights whose float sums can cancel, and NetworkX answers: ints held as floats of both signs, and ints held as ints of both signs whose absolute values sum past 252, which these names add as floats too. Declined, degree_pearson_correlation_coefficient is answered by NetworkX from this backend's node_degree_xy, the same exact pairs in another order, so it may differ from NetworkX's alone in its last bits. A sum of degrees counts a weight at most twice, so within that bound every float sum of the weights is exact; past it, one int past 253 or many smaller ones can round (252 + 1 added three times as floats is 3 * 252 + 4). Ints held as ints were answered wrongly this way before this change: with weights near +-260, or near +-253 and each within it, edge_expansion, mixing_expansion, conductance, normalized_cut_size, modularity, overlapping_modularity, closeness and harmonic centrality, harmonic_diameter and hits answered off, conductance and normalized_cut_size divided by 0, and pagerank failed to converge where NetworkX did not; with every weight within 2**52 + 1, normalized_cut_size summed a volume of 9 as 8. Ints of one sign, of any size, and ints of both signs within the bound stay served. The conversion records this from the matrix's least and greatest values and, for ints of both signs, their absolute sum, so a call only reads it.
  • Where the backend now answers and NetworkX raises on the same valid input: the path-based float names (average_shortest_path_length, closeness and harmonic centrality, harmonic_diameter) on positive ints past 2**53 beside floats, where NetworkX's Bellman-Ford and Dijkstra, comparing ints with floats, can raise NetworkXUnbounded or "Contradictory paths found"; and an np.uint64 beside negative ints, where NetworkX raises numpy's OverflowError ("Python integer -2 out of bounds for uint64"), now only when every np.uint64 there is 0, since otherwise the weights are of both signs and declined.
  • Any other name NetworkX declares as reading weights or giving edge data back declines such weights, so a name added later is exact unless it is listed with the float answers.
  • Keeping the conversion lets NetworkX's cache serve a float answer after an exact name has declined; a refusal during conversion left NetworkX's failed mark for every name on that graph.
  • A rebuild whose values would come back equal but as floats, such as np.uint64(2**63) beside an int, is still left to NetworkX, before conversion.
  • Speed of the names now served on such weights, a 3000-node, 15000-edge graph whose weights are half ints past 2**53, half floats, with graphblas first in the backend priority and the conversion included: pagerank 9.0 ms against NetworkX's 15.3 ms, average_neighbor_degree 9.0 against 16.7, clustering 9.9 against 74.6, closeness_centrality of one node 10.3 against 14.8. Medians of five interleaved rounds. Indicative, load 6 to 10. Reading the bounds of int weights at conversion, and their absolute sum where they have both signs, costs nothing measurable: pagerank and laplacian_matrix on a 20000-node, 100000-edge graph with int weights of one sign and of both, conversion included, within 1% of before (Indicative, load 4 to 7).
  • The additional_docs notes of the rebuild names say such weights are left to NetworkX

Stacked on #122.

NetworkX adds integer weights as Python ints, exactly. Here they are
added in 64 bits, which wrap past 2**63, and a matrix holds every
weight in the one dtype numpy infers for them all, so an int past 2**53
beside a float, or an np.uint64 beside a signed int, is held as a float
and rounds: 2**53 - 1 and np.uint64(4) add to 2**53 + 4.

Where a sum could reach 2**63 the kernels raise IntegerOverflowError,
an OverflowError that is also a NotImplementedError, so NetworkX's
dispatcher tries the next backend: under backend_priority NetworkX
answers for a NetworkX graph, and for one of this backend's graphs when
fallback_to_nx is on. With backend="graphblas" NetworkX raises its own
NotImplementedError from it, and a direct call gets an OverflowError.
NetworkX answers with the arguments it was given, so a one-shot
iterator of nodes, read before the sums are bounded, would reach it
spent: cut_size, volume, average_degree_connectivity,
degree_pearson_correlation_coefficient and eccentricity leave a
weighted call with one to NetworkX. all_pairs_bellman_ford_path_length
and node_degree_xy bound their sums when called, while NetworkX can
still answer, rather than when the first item is read.

A conversion that holds ints as floats is kept and marked. The names
that add the weights as exact ints, compare them, or give them back
decline at their call, on that conversion or one NetworkX serves from
its cache, and NetworkX answers; a name NetworkX declares as reading
weights is one of them unless it is listed with a float answer. Those
names, such as pagerank, NetworkX computes in floats too, or from
exact sums that a float sum of weights of one sign matches to a few
parts in 2**53. They are served unless the weights can cancel in a
float sum: ints held as floats of both signs, or ints of both signs
large enough that their sums pass 2**53, which they add as floats too.
The conversion records which, so a call reads one attribute. An int
below 2**53 beside a float is served, as NetworkX adds it in floats
too. The additional_docs notes say which weights are left to NetworkX.
@codecov

codecov Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 99.18033% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 93.33%. Comparing base (4f35ea8) to head (9e518bb).

Additional details and impacted files
@@                 Coverage Diff                  @@
##           22-assortativity     #126      +/-   ##
====================================================
+ Coverage             93.24%   93.33%   +0.08%     
====================================================
  Files                   192      193       +1     
  Lines                 12022    12196     +174     
  Branches               1395     1413      +18     
====================================================
+ Hits                  11210    11383     +173     
- Misses                  553      554       +1     
  Partials                259      259              
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

This was referenced Dec 2, 2026
@eriknw
eriknw force-pushed the 26-overflow-fallback branch from 9e518bb to 4dca4dc Compare December 3, 2026 00:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant