Skip to content

perf(fit): pass operators and constraints to Julia as typed values - #1394

Merged
MilesCranmer merged 2 commits into
astroautomata:masterfrom
MilesCranmerBot:perf/typed-operator-setup
Sep 28, 2026
Merged

MilesCranmer merged 2 commits into
astroautomata:masterfrom
MilesCranmerBot:perf/typed-operator-setup

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

Two setup steps in _run compiled new Julia code in every session:

  • OperatorEnum was built by evaluating a new Julia closure with jl.seval and splatting a list of pairs.
  • Nested constraints were built by converting a Python list of jl.Pair values into a Dict with inferred types.

Passing typed values instead reuses code compiled at install time.

Scope

  • pysr/sr.py: jl_operator_enum = SymbolicRegression.OperatorEnum(tuple(jl_operators_dict.values())).
  • pysr/sr.py: jl_constraints_dict is a Dict{Int,Vector} filled per arity.

Tradeoffs

The tuple constructor does not register DynamicExpressions' global helper functions. A custom loss_function that evaluates a bare Node without operators, as tree(dataset.X), now fails with DynamicExpressions' own message: "Please use the tree(X, operators; kws...) syntax instead." tree(dataset.X, options) still works, and so does anything that receives an Expression, such as loss_function_expression, because an Expression carries its operators. Operator-free Node calls are deprecated, so this PR does not keep the helpers. Keeping them would cost about 0.45 s on every first fit.

Verification

Zero-iteration first fit, one change at a time: constraints dictionary 3.37 s to 2.88 s on a laptop; OperatorEnum construction 4.21 s to 3.68 s on the Slurm node.

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.

Build the nested-constraints dictionary with concrete key and value types
instead of converting a Python list of pairs, and construct `OperatorEnum`
from a tuple of operator tuples instead of evaluating a new Julia closure
each session. Both paths avoided code compiled at install time.

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 18:09
@MilesCranmer
MilesCranmer merged commit 81cb12b into astroautomata:master Sep 28, 2026
73 of 81 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