Repository navigation
36 float32 mix - #142
Closed
eriknw wants to merge 1 commit into
Closed
36 float32 mix#142eriknw wants to merge 1 commit into
eriknw wants to merge 1 commit into
Conversation
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 Report❌ Patch coverage is 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:
|
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This leaves weights of a narrow NumPy type to NetworkX wherever the backend would answer otherwise. NumPy 2 (NEP 50) adds an
np.float32and 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 weighsnp.float32(0.1), 1-3 weighs 0.2 and 0-3 weighs 0.300000005. NetworkX'sbellman_ford_pathfrom 0 to 3 is[0, 3], since it addsnp.float32(0.1) + 0.2asnp.float32(0.3), which is more than 0.300000005. The backend added them in float64, got 0.30000000149011613, and answered[0, 1, 3].np.float32,np.float16ornp.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.np.float32are held as FP32 and served by six names whose answer is NetworkX's exactly.bellman_ford_path,negative_edge_cycle,centerandperipheryadd each path's weights in float32, as NetworkX does, and only compare the sums.floyd_warshall_numpywidens to float64 first, as NetworkX's array does, andadjacency_matrixadds nothing.floyd_warshall,floyd_warshall_predecessor_and_distance,eccentricity,diameterandradius. The values match, but NetworkX's lengths arenp.float32and the backend's are Python floats, so a caller that goes on adding them gets another sum. NetworkX's ownaverage_shortest_path_length(method="floyd-warshall")adds whatfloyd_warshallgives it. Under the backend priority, withfloyd_warshallserved, it gave 0.6503448416446818, where NetworkX alone givesnp.float32(0.6503448).reverse,ego_graph, the binary operators, the k-core family and the rest) are left to NetworkX for the same reason, as isedge_boundarywithdata: they would carry the weights back as Python floats. NetworkX'scut_sizeadds the weightsedge_boundarygives it.np.float32are 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.to_scipy_sparse_arraystep 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.modularityandoverlapping_modularitymatched NetworkX on all-float32 weights in every graph probed, but they add in another order, so they are left to NetworkX as well. Returningnp.float32lengths would let the length names be served, but NetworkX's test mode rejects a NumPy scalar in a backend's result.test_narrower_floats_add_as_networkx_adds_themand thenp.float32cases of 32'stest_equal_float_weights_add_up_as_networkx_adds_them. Whichever lands second drops 32'snp.float32cases and makes 30's test expect NotImplementedError. A narrow type beside other numbers is refused during conversion, so that covers 30'sall_pairs_dijkstra_path_lengthas 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.