perf(fit): pass operators and constraints to Julia as typed values - #1394
Merged
MilesCranmer merged 2 commits intoSep 28, 2026
Merged
MilesCranmer merged 2 commits into
MilesCranmer merged 2 commits into
Conversation
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 Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
MilesCranmer
approved these changes
Sep 28, 2026
MilesCranmer
approved these changes
Sep 28, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
_runcompiled new Julia code in every session:OperatorEnumwas built by evaluating a new Julia closure withjl.sevaland splatting a list of pairs.jl.Pairvalues into aDictwith 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_dictis aDict{Int,Vector}filled per arity.Tradeoffs
The tuple constructor does not register DynamicExpressions' global helper functions. A custom
loss_functionthat evaluates a bareNodewithout operators, astree(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 anExpression, such asloss_function_expression, because anExpressioncarries its operators. Operator-freeNodecalls 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;
OperatorEnumconstruction 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.pyandpysr/test/test_type_specs.pypass (254 passed, 1 skipped). The pre-commit hooks pass.