Skip to content

Cheaper issue path for small ops on pending arrays - #5

Merged
hokb merged 3 commits into
mainfrom
cheaper-pending-items
Nov 13, 2026
Merged

hokb merged 3 commits into
mainfrom
cheaper-pending-items

Conversation

@hokb

@hokb hokb commented Nov 11, 2026 •

Copy link
Copy Markdown
Member

Port from viproc (hokb/viproc#66), O1 only. Makes the main thread's issue path cheaper for small per-iteration ops such as ES[i] = ES[i-1] + np.abs(np.sin(A[:, i])).sum(). It adds no synchronous work on the main thread.

Changes

  • Element of a pending array (ES[i-1]): a C path issues the 0-d copy as one task, instead of going through the Python hook. It is still a snapshot like NumPy's scalar, with no sync.
  • NumPy scalars as operands: the dtype comes from the scalar's type, without lookups, and the bytes are copied directly.
  • 0-d ufunc calls: when every operand is 0-d in the loop's dtypes, the layout is a constant. There is no kernel selection over layouts and no cast check.
  • Copy-on-write: no heap allocation per write.
  • Graveyard::drain(): no mutex per C ABI call.

Measurements

i7-12700K, numpy2x's 2 workers, using the README method. Results are bit-identical.

Accumulating variant of example 1:

Size main this PR README loop
500 x 500 0.94x 1.45x 1.89x
1000 x 1000 1.70x 1.80x
1500 x 1500 1.88x 1.88x

Tests

New tests:

  • elements of pending arrays (all dtypes and key forms, snapshot semantics, assert_async);
  • the accumulating loop, bit-identical to NumPy;
  • NumPy scalars as operands;
  • 0-d ops on pending operands of every dtype, including FP errors.

The full suite passes. One run had 3 failures in timing-dependent svd count checks, which pass on their own and in a rerun.

🤖 Generated with Claude Code

https://claude.ai/code/session_01JT2yHm85oFjgf1AcgGLUux

hokb and others added 3 commits November 11, 2026 10:09
…om viproc)

a[i, ...] of a pending array is issued in the C slot as one placement task
(no Python hook, no view objects), still a 0-d snapshot marked _scalar. A
NumPy scalar operand gets its dtype from its type, never defers, and its
bytes are copied as they are when the dtype matches.

i7-12700K, 2 workers: the loop ES[i] = ES[i - 1] + abs(sin(A[:, i])).sum()
at 500 x 500 0.94x -> 1.31x of NumPy, 1000 x 1000 1.70x -> 1.80x; results
bit-identical. Tests: pending elements of all dtypes and key forms against
NumPy, snapshot semantics, one task without a sync, the cumulative loop,
NumPy scalars of every dtype as operands.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…s behind the copy chain)

At SIZE elements the chain of 20 copies could finish before y[17] was
issued on a fast runner; y[17] then took the ready path and issued no
task (seen on viproc's CI, ubuntu 3.13).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JT2yHm85oFjgf1AcgGLUux
…r mutex per op (port from viproc)

- nx_ufunc_issue: 0-d inputs of the loop's dtypes and a 0-d result take the
  loop's shared kernel with a constant layout (no order-K layout,
  single-call or cast check): the same kernel and layout as before.
- Task::copies_ inline: a write into an array a pending task reads (copy-
  on-write each time) did a heap allocation per op.
- Graveyard::drain() checks an atomic flag before taking its mutex.

i7-12700K, 2 workers: the loop ES[i] = ES[i - 1] + abs(sin(A[:, i])).sum()
at 500 x 500 1.31x -> 1.45x of NumPy (0.94x before this branch), 1000 x
1000 1.80x, 1500 x 1500 1.88x; bit-identical. Test: 0-d ops of every dtype
on pending operands, FP errors.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@hokb hokb changed the title Cheaper elements of pending arrays and NumPy scalar operands Cheaper issue path for small ops on pending arrays Nov 11, 2026
@hokb
hokb merged commit a487746 into main Nov 13, 2026
5 checks passed
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.

2 participants