Skip to content

feat: optional Rust backend - #1429

Draft
MilesCranmerBot wants to merge 4 commits into
astroautomata:masterfrom
MilesCranmerBot:rust-backend
Draft

MilesCranmerBot wants to merge 4 commits into
astroautomata:masterfrom
MilesCranmerBot:rust-backend

Conversation

@MilesCranmerBot

Copy link
Copy Markdown
Contributor

Adds an optional Rust backend, PySRRegressor(backend="rust"), which runs the search with symbolic_regression.rs through its Python package symbolic-regression-rs. The Julia backend stays the default, and nothing changes for existing users.

This depends on the Python package landing in symbolic_regression.rs (astroautomata/symbolic_regression.rs#57) and being published to PyPI as symbolic-regression-rs 0.1.0. Until then, CI builds it from a pinned commit.

What changes

  • backend="julia" (default) or "rust". A new pysr[rust] extra installs symbolic-regression-rs.
  • Importing is unchanged by default: import pysr still loads Julia exactly as before, and backend="rust" works with that normal import. Setting PYSR_BACKEND=rust before import pysr skips Julia entirely (juliacall is never imported, so Julia is never installed or started), makes "rust" the default backend, and makes Julia-only objects such as pysr.jl raise an error that says to unset the variable.
  • With the Rust backend, the regressor's settings are translated to SymbolicRegression.jl's option names and passed to the Rust engine. Parameters that only make sense in Julia (loss_function, loss_function_expression, complexity_mapping, turbo, bumper, autodiff_backend, logger_spec, cluster_manager/procs and related multiprocessing options, fast_cycle, type specs, precision=16, custom Julia mutations or plugins) raise ValueError naming the parameter when set to non-default values. The Rust engine takes real float32/float64 data.
  • Results go through the existing code: the engine writes SymbolicRegression.jl-format CSV files, so equations_ has the same columns and dtypes, scores, SymPy/JAX/PyTorch exports and from_file behavior as the Julia path. Ordinary expressions predict through the same SymPy-lambdified functions; template expressions predict with the engine's evaluator, since their equation strings are not SymPy.
  • warm_start resumes from the engine's state, kept in rust_state_stream_; pickled regressors predict after unpickling.
  • Separate fix: from_file now loads CSV-only multi-output runs, which raised FileNotFoundError before (first commit, with a test).
  • Docs: the backend choice, installation, PYSR_BACKEND, and the options the Rust backend does not support.
  • CI: a new job installs Rust, builds the engine's Python package, and runs the Rust-backend tests with and without PYSR_BACKEND=rust.

Verification

  • Rust-backend tests: 61 passed with the normal import; 59 passed and 2 skipped (Julia-only checks) with PYSR_BACKEND=rust. They cover fits and predictions, equations_ columns and dtypes against the Julia path, exports, multi-output, units, guesses, templates, string losses and early stops, warm start, pickling, options taking effect (operator complexity, nested constraints, seeds, maxsize), errors for Julia-only parameters, and the missing-package message.
  • The existing test suite with the Julia backend: 189 tests, OK.
  • In a fresh environment with only PySR and the Rust package installed, PYSR_BACKEND=rust imports every PySR module, fits and predicts without importing juliacall, and never creates a Julia installation.
  • With PYSR_BACKEND unset, an independent check found the import order, environment variables, __all__, and pysr.jl unchanged.

@codecov

codecov Bot commented Oct 14, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 38.42365% with 125 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
pysr/rust_backend.py 0.00% 89 Missing ⚠️
pysr/julia_import.py 59.18% 20 Missing ⚠️
pysr/sr.py 65.62% 11 Missing ⚠️
pysr/__init__.py 80.00% 2 Missing ⚠️
pysr/julia_helpers.py 81.81% 2 Missing ⚠️
pysr/_backend.py 85.71% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@MilesCranmerBot
MilesCranmerBot force-pushed the rust-backend branch 5 times, most recently from 1684edc to 17103f9 Compare October 18, 2026 00:26
MilesCranmerBot and others added 3 commits October 18, 2026 20:31
Co-authored-by: Miles Cranmer <miles.cranmer@gmail.com>
Co-authored-by: Miles Cranmer <miles.cranmer@gmail.com>
Co-authored-by: Miles Cranmer <miles.cranmer@gmail.com>
Co-authored-by: Miles Cranmer <miles.cranmer@gmail.com>

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