Skip to content

36 float32 mix - #142

Closed
eriknw wants to merge 1 commit into
python-graphblas:26-overflow-fallbackfrom
eriknw:36-float32-mix
Closed

eriknw wants to merge 1 commit into
python-graphblas:26-overflow-fallbackfrom
eriknw:36-float32-mix

Conversation

@eriknw

@eriknw eriknw commented Dec 2, 2026

Copy link
Copy Markdown
Member

This leaves weights of a narrow NumPy type to NetworkX wherever the backend would answer otherwise. NumPy 2 (NEP 50) adds an np.float32 and a Python number in float32, so NetworkX's path sums turn float32 at the first such weight. A matrix holds every weight in the one dtype NumPy picks for all of them, float64 for a mix, and adds in that. Take the triangle where 0-1 weighs np.float32(0.1), 1-3 weighs 0.2 and 0-3 weighs 0.300000005. NetworkX's bellman_ford_path from 0 to 3 is [0, 3], since it adds np.float32(0.1) + 0.2 as np.float32(0.3), which is more than 0.300000005. The backend added them in float64, got 0.30000000149011613, and answered [0, 1, 3].

  • A narrow type (np.float32, np.float16 or np.complex64) beside any other number is refused at conversion, for every name, and NetworkX answers. A missing weight counts as the int 1, as NetworkX reads it.
  • Weights that are all np.float32 are held as FP32 and served by six names whose answer is NetworkX's exactly. bellman_ford_path, negative_edge_cycle, center and periphery add each path's weights in float32, as NetworkX does, and only compare the sums. floyd_warshall_numpy widens to float64 first, as NetworkX's array does, and adjacency_matrix adds nothing.
  • The names that return lengths are left to NetworkX for these weights: the Bellman-Ford lengths, floyd_warshall, floyd_warshall_predecessor_and_distance, eccentricity, diameter and radius. The values match, but NetworkX's lengths are np.float32 and the backend's are Python floats, so a caller that goes on adding them gets another sum. NetworkX's own average_shortest_path_length(method="floyd-warshall") adds what floyd_warshall gives it. Under the backend priority, with floyd_warshall served, it gave 0.6503448416446818, where NetworkX alone gives np.float32(0.6503448).
  • Rebuilds (reverse, ego_graph, the binary operators, the k-core family and the rest) are left to NetworkX for the same reason, as is edge_boundary with data: they would carry the weights back as Python floats. NetworkX's cut_size adds the weights edge_boundary gives it.
  • The other names that read weights add them in another order or type, so they too are left to NetworkX when all the weights are float32.
  • With NumPy before 2, NetworkX adds a float32 and a Python number in float64, and all-float32 weights go to NetworkX for every name.
  • Weights that are all np.float32 are still converted, and the conversion is marked. The names left to NetworkX decline when called: on that conversion, on one NetworkX serves from its cache, or on a graph of this backend's that came from one. A refusal during conversion would leave NetworkX's failed mark in its cache, so every later name on that graph would go to NetworkX, the six served names too. A graph built here directly from float32 values is read as its matrix holds it.
  • Each rule is in the backend notes NetworkX shows for these names.
  • Cost: the conversion reads each weight's type in its own walk of the edges, which follows NetworkX's to_scipy_sparse_array step for step, so no second walk is made. On 5e5 directed edges weighing all 1.0, halves, random fractions or ints, conversion takes the same time as without this change. Over two runs of seven to nine interleaved rounds, the median ratios were 0.93 to 1.03. Indicative, load 11 to 27.
  • A mix is still refused during conversion. NetworkX then marks the conversion failed, and every later name on that graph and weight goes to NetworkX, where each would go anyway.
  • modularity and overlapping_modularity matched NetworkX on all-float32 weights in every graph probed, but they add in another order, so they are left to NetworkX as well. Returning np.float32 lengths would let the length names be served, but NetworkX's test mode rejects a NumPy scalar in a backend's result.
  • Merging with 30-dijkstra and 32-float-path-lengths: their tests that call length names by name on all-float32 weights expect the backend to answer, and with this change it declines. These are 30's test_narrower_floats_add_as_networkx_adds_them and the np.float32 cases of 32's test_equal_float_weights_add_up_as_networkx_adds_them. Whichever lands second drops 32's np.float32 cases and makes 30's test expect NotImplementedError. A narrow type beside other numbers is refused during conversion, so that covers 30's all_pairs_dijkstra_path_length as well. All-float32 weights are declined name by name, so 30's name has to be added to the per-name lists in _nx_graphblas/core.py. Until then, the test that every name reading weights is listed fails once the two branches meet. These branches were not run together with this one.

Stacked on #126.

NumPy 2 adds an np.float32 (or np.float16, np.complex64) and a Python
number in the narrow type, so NetworkX's path sums turn float32 at the
first such weight. A matrix holds every weight in the one dtype NumPy
infers for them all, float64 for a mix, and adds in that, so lengths,
their order and the paths chosen could differ from NetworkX's.

A narrow type beside other numbers is now refused at conversion, for
every name. Weights that are all np.float32 are held as FP32 and served
only where the answer is NetworkX's exactly: Bellman-Ford's path and
negative-cycle test, center, periphery, floyd_warshall_numpy and
adjacency_matrix. Names that return the lengths are left to NetworkX:
its lengths are np.float32, which its own callers go on adding in
float32, where ours are Python floats. Rebuilds are left for the same
reason. Such weights are still converted, and marked, and the other
names decline when called: a refusal during conversion would leave
NetworkX's failed mark in its cache, for the served names too.

The weights' types are read in the conversion's own walk of the edges,
which now follows NetworkX's to_scipy_sparse_array step for step, so a
graph of plain floats costs no second walk.
@codecov

codecov Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.23926% with 9 lines in your changes missing coverage. Please review.
✅ Project coverage is 93.45%. Comparing base (9e518bb) to head (a0fe8da).
⚠️ Report is 2 commits behind head on 26-overflow-fallback.

Additional details and impacted files
@@                   Coverage Diff                    @@
##           26-overflow-fallback     #142      +/-   ##
========================================================
+ Coverage                 93.33%   93.45%   +0.11%     
========================================================
  Files                       193      194       +1     
  Lines                     12196    12520     +324     
  Branches                   1413     1449      +36     
========================================================
+ Hits                      11383    11700     +317     
- Misses                      554      558       +4     
- Partials                    259      262       +3     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@eriknw

eriknw commented Dec 2, 2026

Copy link
Copy Markdown
Member Author

Closing this one. Float results only need to agree with NetworkX's within rounding, and #126's branch already serves narrow float weights that way: mixed float32, all-float32 and float16 beside other floats all agree with the exact answer to float error, with no crash. The guard left in this PR would only refuse graphs where NetworkX's own float16 or float32 sums overflow to inf, and copying that overflow isn't a goal; the backend gives the true value or the same inf. So nothing here is needed.

@eriknw eriknw closed this Dec 2, 2026
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