Skip to content

perf(numpy): speed up metadata -- 15.7% faster forward pass - #884

Open
dexhunter wants to merge 1 commit into
HIPS:masterfrom
dexhunter:perf/numpy-metadata
Open

dexhunter wants to merge 1 commit into
HIPS:masterfrom
dexhunter:perf/numpy-metadata

Conversation

@dexhunter

Copy link
Copy Markdown

TL;DR

  • Speed up metadata during the 10,000-iteration fan-out/fan-in forward pass.
  • Read native NumPy properties directly, while keeping NumPy dtype inference for swapped-endian arrays.
  • Median runtime: 143.51 ms → 121.03 ms, saving 22.48 ms per forward pass.
  • Native tests: python -m pytest -o addopts='' — 646 passed, with 14 warnings.

What & Why

Previously, every metadata call used four general NumPy queries, even for arrays and scalars that already store these properties.
The change reads those properties directly for exact NumPy arrays and common scalar types.

NumPy still computes the dtype for non-native byte order, so the output stays the same.
Other inputs keep the existing queries, and one shared return builds the result.

Validation

Measurements used Python 3.12 and NumPy 2.5.4 on an isolated CPU core.
All situations use the repository's 10,000-iteration fan_out_fan_in workload.

Situation Before After Change
Forward trace with metadata checks, median of 27 samples 143.51 ms 121.03 ms 22.48 ms saved
Forward trace with garbage collection, median of 7 repeats 291 ms 301 ms 10 ms slower; ranges overlap
Full gradient, median of 7 repeats 365 ms 347 ms 18 ms saved; ranges overlap
Plain function evaluation, best of 7 repeats 0.307 ms 0.308 ms No material change (+0.001 ms)

The forward-trace time ratios have the following summary:

Statistic Value
Median new/old time ratio 0.843 (15.7% faster)
95% CI of the median (bootstrap, 10,000 resamples) 0.835–0.857 (14.3–16.5% faster)
Mean ± SD of the ratio 0.848 ± 0.017
P95 ratio (slowest 5% of pairs) 0.877
Number of old/new pairs (n) 27 (3 runs)

The first row measures tracing with garbage collection disabled.
The other rows use the unchanged repository benchmarks through timeit, with garbage collection enabled.
Their repeat ranges overlap, so they do not establish a reliable speedup or slowdown.
Plain evaluation does not use the changed metadata path.

Run the repository's full-gradient benchmark from either checkout:

python -m timeit -v -r 7 -n 1 -u msec \
  -s 'import gc; gc.enable(); from benchmarks.bench_core import time_fan_out_fan_in_grad' \
  'time_fan_out_fan_in_grad()'

Use time_fan_out_fan_in_forward_pass for forward tracing, or time_fan_out_fan_in_fun with -n 10 for plain evaluation.
Nine additional regression checks passed for swapped-endian float64, complex128, and int32 arrays across scalar, vector, and matrix shapes.
These checks passed on baseline and failed on the earlier dtype shortcut.
Ruff 0.17.3 lint and formatting checks pass on both checkouts.

Why this is safe and easy to maintain

The change stays inside one short function and builds its result in one place.
Exact type checks keep the general path for subclasses and custom array protocols.
tests/test_numpy.py, tests/test_complex.py, and the byte-order regression checks verify unchanged outputs and gradients.

AI assistance

This PR was prepared with AI assistance from Aiden. Candidates were tracked with Weco Observe.

Weco share link: https://dashboard.weco.ai/share/dImyhY89leTtdHie5cd2HIVt_O5_ViFJ

Read native NumPy properties directly and build one shared metadata result.
Keep NumPy dtype inference for non-native byte order and generic inputs.

Co-Authored-By: Aiden <aiden@weco.ai>

This branch has not been deployed

No deployments
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