Skip to content

perf(fit): copy NumPy fit data through PyArray - #1393

Merged
MilesCranmer merged 1 commit into
astroautomata:masterfrom
MilesCranmerBot:perf/pyarray-fit-data
Sep 28, 2026
Merged

MilesCranmer merged 1 commit into
astroautomata:masterfrom
MilesCranmerBot:perf/pyarray-fit-data

Conversation

@MilesCranmerBot

Copy link
Copy Markdown
Contributor

These PySR changes pair with the SymbolicRegression.jl PRs astroautomata/SymbolicRegression.jl#739 through #742, which compile PySR's search and PythonCall conversions at install time. Each PySR PR works on its own with the current SymbolicRegression.jl. The full speedup arrives once those PRs are released and PySR's version bound is raised.

Why

jl_array converts NumPy arrays with pyconvert(Array{T}, x), which compiles PythonCall's generic conversion rules on every first fit. Wrapping the array in PythonCall.PyArray and copying it gives the same owned Julia Array through a narrow, typed path that SymbolicRegression.jl can precompile.

Scope

  • pysr/julia_helpers.py: new jl_numpy_array(x), which returns jl.copy(PythonCall.PyArray(x)).
  • pysr/sr.py: X, y, and weights use jl_numpy_array in _run. Type-spec inputs still go through type_spec_to_julia_array.

Blast Radius

The data Julia receives is an owned copy, as before, so later changes to the NumPy array do not affect a running search. Fixed-seed fits give byte-identical halls of fame.

Verification

Zero-iteration first fit, measured with the matching SymbolicRegression.jl precompile statement: 3.68 s to 3.33 s.

With all seven PRs applied, on a 12-core Slurm node (Julia 1.13.1, Python 3.13), the README example's first fit went from 17.4 s to 6.95 s, and a zero-iteration first fit from 12.6 s to 2.71 s. pysr/test/test_main.py and pysr/test/test_type_specs.py pass (254 passed, 1 skipped). The pre-commit hooks pass.

Wrap X, y and weights in `PythonCall.PyArray` and copy them into an owned
Julia array. This skips PythonCall's generic array conversion, which
compiled fresh code on every first fit.

Co-authored-by: Miles Cranmer <miles.cranmer@gmail.com>
@codecov

codecov Bot commented Sep 28, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@MilesCranmer
MilesCranmer enabled auto-merge (squash) September 28, 2026 09:02
@MilesCranmer
MilesCranmer merged commit 415700d into astroautomata:master Sep 28, 2026
47 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