Repository navigation
Update to SS:GB 10.5.3 - #163
Merged
Merged
Conversation
- cibuildwheel v4.3.5, setup-python v7.1.0, create-pull-request v8.1.3 - test.yml: add 3.15t now that conda-forge builds numpy, cffi and cython for it (coverage there is pure Python, so the existing fallback runs the tests without it); replace setup-miniconda's deprecated auto-activate-base and nodefaults channel with auto-activate and conda-remove-defaults - lint.yml: run pre-commit's steps directly, since pre-commit/action's latest release uses actions/cache@v4, which runs on Node.js 20 - pre-commit: validate-pyproject v0.27, flake8 7.5.0, flake8-bugbear 26.11.9, zizmor v1.32.0, check-jsonschema 0.39.0
10.5.3 fixes one crash (changing the integer bit-width of a matrix with pending zombies). The regenerated headers and source.c are unchanged, and GB_control.h is the same as in 10.5.2, so suitesparse.sh's comment about which types lack factory kernels still holds.
The preprocessor ran as `gcc ... | sed ... > out` in a shell, whose exit status is sed's. When gcc failed, the script went on to write empty headers and source.c and reported success. It does that with pycparser's PyPI wheels, which leave out the fake libc headers that conda-forge's pycparser ships. Run gcc without a shell and fail on its exit status, do the " complex " -> " _Complex " substitution in Python, and say clearly when the fake libc headers are missing. With conda-forge's pycparser the output is byte-identical. AGENTS.md notes which pycparser to use.
With a subarray dtype such as ("<i8", (4,)), NumPy appends the element
axes to the array's shape, and claim_buffer_2d got two cases wrong:
- exact size in column-major order: NPY_ARRAY_F_CONTIGUOUS made the
element axes the slowest, so values came out scrambled without error;
- spare values: reshaping claim_buffer's result to (nrows, ncols) raised
ValueError after GraphBLAS had already handed over the buffer.
Give the column-major case explicit strides so the element axes stay
last and contiguous, and keep them last when trimming spare values. The
result is (nrows, ncols) + dtype.shape in every case; exact-size results
still own their data. Built-in dtypes behave as before. New tests cover
built-in and subarray dtypes by row/column order and exact/spare size.
The aarch64 job has skipped musllinux since GraphBLAS#106 (2023-10), when NumPy had no musllinux aarch64 wheels and that job ran under QEMU. NumPy has had them since 1.26.2 (2023-11), and the job runs on a native arm runner since GraphBLAS#142, so build them in a job of their own, as for x86_64: CIBW_BEFORE_ALL builds GraphBLAS once per container image, and the musl build needs its own.
GraphBLAS has handed the memory over by the time it is claimed, so if claiming raises (an array too big for NumPy, or a buffer smaller than nrows * ncols), nothing else would free it. Free it with the arena's free function instead. An invalid arena still leaves it alone, since then there is no free function to use.
GraphBLAS reads the bytes of the buffer it is given, so a copy must hold them in the order the array has them in memory. The copy was in C order unless the array was F-contiguous, which reordered other layouts that fill their data without gaps, such as the column order claim_buffer_2d gives a subarray dtype. Arrays with gaps or a backwards axis are still copied in C order.
Resizing a matrix to get spare values relied on GraphBLAS keeping the buffer. Instead, pack a buffer of exactly the size to test and export it again, which hands it back unchanged. Also check that giving the claimed array back, as itself or as a copy, gives GraphBLAS the layout it exported.
When the base of a subarray dtype is a subarray dtype too, as with
(("<i2", (3,)), (2,)), NumPy appends its axes after dtype.shape. With
spare values, claim_buffer_2d reshaped to (nrows, ncols) + dtype.shape,
which left them out: it raised ValueError (the claimed buffer was still
freed) or, for an empty matrix, returned the wrong shape. Take the
element axes from the claimed array instead. An exact fit was right
already, since NumPy expands the dtype itself there.
The test's arena allocates and frees with Python cffi callbacks, which utils.pyx calls directly. PyPy runs C extension code holding the GIL, and a cffi callback takes the GIL on entry without checking whether its own thread holds it, so the first call (from `empty`) waits forever.
PyPy runs extension modules holding the GIL, and a cffi callback takes the GIL on entry without checking whether its own thread holds it, so a Python callback that psg or NumPy calls as an arena's function waits forever.
…ions At -O2, wasm-opt inlines every function that has one caller, whatever its size, so it inlined the FactoryKernels into the switches that call them. The module had functions of 3.4 MB (GB_AxB_saxpy3), 2.9 MB (GB_add_phase2), 1.2 MB (GB_AxB_saxbit), 1.1 MB (GB_AxB_dot3) and 1.0 MB (GB_AxB_dot2). Once they were hot, Chrome's optimizing compiler spent 4 to 5 GB of memory on them, and the kernels ran on baseline code meanwhile. With --one-caller-inline-max-function-size=1000 the largest GraphBLAS function is about 0.2 MB. In headless Chrome (Pyodide 314.0.8, python-graphblas with graphblas-algorithms), a page with a 1e6-edge graph peaks at 1.2 GB instead of 4.9 to 5.0 GB, PageRank takes 4.1 to 4.3 ms instead of 7.5 to 8.1 ms, and a first BFS takes 0.02 s instead of 0.21 s. The wheel grows from 3.10 MB to 3.84 MB, and the link takes 20 s instead of 99 s. It is a link setting, so it goes in LDFLAGS: in EMCC_CFLAGS, emcc warns at each of the 3,102 GraphBLAS compiles that it is ignored there. The cp313 wheels' Emscripten 4.0.9 (wasm-opt 123) supports the option too. PR builds use GRAPHBLAS_COMPACT, which has no FactoryKernels, so only release builds change.
A PyPy wheel job hung in a test for over an hour before its run was cancelled; without a timeout, GitHub lets a job run for 6 hours. The limits leave room: full release builds take up to 35 minutes, PR builds up to 25, and the conda test jobs a few minutes.
pytest-run-parallel (`--parallel-threads=N`) runs each test in N threads at once, which finds races on free-threaded Python. Two tests failed under it: - test_claim_buffer_frees_on_error: threads raced to initialize the recording arena (GxB_arena_init returns GrB_INVALID_VALUE for an arena that is already initialized), and shared one list of freed addresses, which one thread cleared while another checked it. Initialize the arena under a lock, and record frees per thread. Fixing the test, rather than running it in one thread, keeps it racing the first use of an arena's memory handler in utils.pyx: without the lock in `_handler`, it fails in 4 threads. - test_run_doctests: doctest replaces sys.stdout and linecache.getlines for the whole process while it runs, so mark it thread_unsafe, which pytest-run-parallel runs in one thread. conftest.py registers the marker when the plugin isn't installed, since --strict-markers and -W error reject an unknown marker.
Without a GIL, threads run Python code in parallel, and a race in psg, GraphBLAS, or a test shows up only when tests run in threads. On 3.14t and 3.15t, install pytest-run-parallel and run the suite once more with each test in 4 threads at once, 10 times each (locally, 5.5 seconds against 1.3 for the usual run). This adds to the usual run rather than replacing it: pytest-run-parallel ignores warnings that a test raises in its threads, and the usual run turns warnings into errors.
The comment at the top of utils.pyx and AGENTS.md said the package does nothing special for free-threading, but making each arena's NumPy memory handler on first use takes a lock, so threads racing that first use share one handler.
Each thread's list of freed addresses existed only once the test had set it, so a free in another thread would raise AttributeError in the cffi callback, which is only printed, and the free would go unseen. Every thread now starts with an empty list.
The allocator table, the NumPy handler structs and the handler capsules are all filled once under _handlers_lock and only read after that; the comment and AGENTS.md now say so instead of naming just the handler.
Member
Author
|
Seems okay to me. I wouldn't mind if an agent other than Opus/Sonnet reviewed this and recent psg changes (CC @michelp). I plan to enable code coverage in a new PR before the next release so our agents can improve coverage. Heh, it's kind of amusing to watch agents discover that uncovered code is often buggy code! YOLO, merging 🚀 |
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.
Update to SuiteSparse:GraphBLAS 10.5.3, along with some fixes my agents recommend.
claim_buffer_2dsupports subarray dtypes (also nested ones), with or without spare values; the result is(nrows, ncols)followed by the element axes. Before, column order scrambled the values, and spare values raised after GraphBLAS had handed over the buffer.claim_bufferandclaim_buffer_2dfree the memory when they raise (it leaked before).give_buffercopies in the array's memory order, so a copy hands GraphBLAS the same bytes as the array itself would.create_headers.pystops when the preprocessor fails (with PyPI's pycparser it wrote empty headers and reported success); AGENTS.md says to use conda-forge's pycparser.test_claim_buffer_frees_on_erroron PyPy: its arena allocates with Python cffi callbacks, and on PyPy a cffi callback called from a C extension waits forever for the GIL its own thread holds. Theutilsmodule docstring now says an arena's functions must be C functions on PyPy.--one-caller-inline-max-function-size=1000. Without it, wasm-opt inlined the FactoryKernels into functions of up to 3.4 MB, Chrome used 4 to 5 GB of memory compiling them, and a page using python-graphblas on a 1e6-edge graph peaked at 4.9 GB (now 1.2 GB; PageRank 7.8 ms to 4.1 ms). The wheel is 0.7 MB larger. PR builds are compact (no FactoryKernels), so only release builds change.test_claim_buffer_frees_on_errorwas made thread-safe rather than single-threaded, so it still races the first use of an arena's memory handler (it fails in threads ifutils.pyxdrops its lock there).Risk:
claim_bufferandclaim_buffer_2dnow free the buffer when they raise, so a caller that catches the error and still uses or frees the pointer would hit a use-after-free or double free. python-graphblas, the only other user on GitHub, doesn't do that.