Skip to content

Add a fast path for assigning a Python scalar under a mask - #648

Open
eriknw wants to merge 3 commits into
19-scalar-fast-pathsfrom
49-masked-literal-assign
Open

eriknw wants to merge 3 commits into
19-scalar-fast-pathsfrom
49-masked-literal-assign

Conversation

@eriknw

@eriknw eriknw commented Oct 19, 2026

Copy link
Copy Markdown
Member

w(mask, replace) << x with x an exact-type Python bool, int (within INT64) or
float now makes one GrB_*assign call over all indices instead of building
the general update. A UDT target, accum, opts, input_mask, any mask the full
path would convert or reject, and an active Recorder take the full path.
Level-synchronous traversals (BFS) pay this once per level:
seen(q.S) << True goes from 6.98 to 1.9 us (best of 5), and bfs_edges on a
10k-node road ball from 5.1 to 4.25 ms (measured in graphblas-algorithms).

Two commits: the fast path, and importing lookup_dtype once at module level
(the per-call import cost 0.4 us of the 2.3).

Parity: 113,653 generated cases plus 34 edge cases compare the fast path with
the full path (forced by a Recorder): every built-in dtype including complex,
values including 300, 240, 263, 2**64 - 1, -0.0 and nan, every mask kind
and replace, Vector and both Matrix orders, wrong-size and wrong-kind masks,
UDT masks and targets, accum and opts; result, sign of zero, iso flag,
warnings, exception type and message all match, on both backends, without
numba and with C scalars.

@eriknw
eriknw added this pull request to stack #650 October 19, 2026 07:50
@eriknw
eriknw force-pushed the 49-masked-literal-assign branch from 5fb311d to 1033bd9 Compare October 19, 2026 07:55
@codecov

codecov Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
⚠️ Please upload report for BASE (19-scalar-fast-paths@be754d8). Learn more about missing BASE report.

Additional details and impacted files
@@                   Coverage Diff                   @@
##             19-scalar-fast-paths     #648   +/-   ##
=======================================================
  Coverage                        ?   98.63%           
=======================================================
  Files                           ?      106           
  Lines                           ?    31397           
  Branches                        ?     3636           
=======================================================
  Hits                            ?    30968           
  Misses                          ?      278           
  Partials                        ?      151           
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@eriknw
eriknw force-pushed the 49-masked-literal-assign branch from 1033bd9 to 246c091 Compare October 20, 2026 22:28
@eriknw
eriknw force-pushed the 49-masked-literal-assign branch from 246c091 to 0d6cb67 Compare October 21, 2026 04:14
@eriknw
eriknw force-pushed the 49-masked-literal-assign branch from 0d6cb67 to 0ab03f4 Compare October 21, 2026 08:38
@eriknw
eriknw removed this pull request from stack #650 October 28, 2026 04:29
@eriknw
eriknw force-pushed the 49-masked-literal-assign branch from 0ab03f4 to 3451a70 Compare October 28, 2026 04:29
@eriknw
eriknw added this pull request to stack #653 October 28, 2026 04:30
@eriknw
eriknw removed this pull request from stack #653 October 28, 2026 04:31
@eriknw
eriknw added this pull request to stack #655 October 28, 2026 04:32
@eriknw
eriknw removed this pull request from stack #655 October 28, 2026 04:33
@eriknw
eriknw added this pull request to stack #657 October 28, 2026 04:33
@eriknw
eriknw force-pushed the 49-masked-literal-assign branch from 3451a70 to 6e58775 Compare October 28, 2026 13:21
w(mask, replace) << x with a Python bool, int or float x goes through
_update, a second Updater, an index resolver for `...`, a Scalar built
from x and _prep_for_assign before it reaches GrB_Vector_assign_<T> (or
GrB_Matrix_assign_<T>) over all indices. That is about 7 us of Python
for a call SuiteSparse finishes in about 0.5 us, and level-synchronous
traversals make it once per level: graphblas-algorithms marks each BFS
level visited with seen(q.S) << True.

Make that call directly when nothing needs the full path: x of type
bool, int (within INT64) or float, a non-UDT target, no accum, no opts,
no input_mask, a Mask (structural or value, complemented or not) of an
object of the target's own type and shape, or no mask without replace,
and no active Recorder. x takes the dtype the full path gives its
Scalar, and GraphBLAS casts it as before. Everything else, including
masks the full path converts or rejects, ints beyond INT64 and numpy
scalars, takes the full path unchanged.

Measured with no test suite running (load average about 5.3 from other
work), mlx, SuiteSparse 10.5.0, best of 5, two rounds each:

    seen(q.S) << True, 10000-element Vector    7.7 us -> 2.3 to 2.4 us
    bfs_edges, 10k-node road ball, 134 levels  5.1 ms -> 4.2 to 4.3 ms
    bfs_edges, 50k-node road ball, 352 levels  16.5 to 16.9 ms -> 14.7 to 14.9 ms

with the same edges. The tests compare the fast path with the full path
(forced by an active Recorder) over 7 dtypes, 9 values (bool, 0, -1,
300, 2**40, 1.5, -0.0, nan), 5 mask kinds and replace for Vector, and a
smaller grid for Matrix, plus the errors of a wrong-kind or wrong-size
mask and an int beyond INT64. Breaking the fast path's replace,
complement, structure or value each fails between 216 and 423 cases.
_assign_literal imported lookup_dtype on every call. base.py can import
it at module level (literals.py, which base.py imports, already does),
and the import was about a sixth of what the fast path still costs:
seen(q.S) << True on a 10000-element Vector, best of 5, goes from
2.28 us to 1.9 us (the GrB_Vector_assign_BOOL call itself is 0.42 us).
…ull path does

_assign_literal checks the status of GrB_*_assign_<T> itself, and no test
reached that check, since Python validates the mask and value first. A
Vector whose data was exported with give_ownership=True has no GraphBLAS
object left, so the assign fails: both the fast path and the full path
(forced by a Recorder) raise NullPointer.
@eriknw
eriknw force-pushed the 49-masked-literal-assign branch from 6e58775 to df34b4c Compare October 29, 2026 02:01
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