Skip to content

Commit

Permalink
Disable PythonCall GC during equation search
Browse files Browse the repository at this point in the history
  • Loading branch information
MilesCranmer committed Jan 26, 2024
1 parent e530637 commit 4037c2d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
13 changes: 13 additions & 0 deletions pysr/julia_helpers.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
"""Functions for initializing the Julia environment and installing deps."""
import os
import warnings

# Required to avoid segfaults (https://juliapy.github.io/PythonCall.jl/dev/faq/)
if os.environ["PYTHON_JULIACALL_HANDLE_SIGNALS"] not in {"yes", ""}:
warnings.warn(
"PYTHON_JULIACALL_HANDLE_SIGNALS environment variable is set to something other than 'yes' or ''. "
+ "You will experience segfaults if running with multithreading."
)

os.environ["PYTHON_JULIACALL_HANDLE_SIGNALS"] = "yes"

import juliacall
import juliapkg

jl = juliacall.newmodule("PySR")

from juliacall import convert as jl_convert

jl.seval("using PythonCall: PythonCall")
PythonCall = jl.PythonCall

juliainfo = None
julia_initialized = False
julia_kwargs_at_initialization = None
Expand Down
10 changes: 9 additions & 1 deletion pysr/sr.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,13 @@
from .export_sympy import assert_valid_sympy_symbol, create_sympy_symbols, pysr2sympy
from .export_torch import sympy2torch
from .feature_selection import run_feature_selection
from .julia_helpers import _escape_filename, _load_cluster_manager, jl, jl_convert
from .julia_helpers import (
PythonCall,
_escape_filename,
_load_cluster_manager,
jl,
jl_convert,
)
from .utils import (
_csv_filename_to_pkl_filename,
_preprocess_julia_floats,
Expand Down Expand Up @@ -1716,6 +1722,7 @@ def _run(self, X, y, mutated_params, weights, seed):
jl.Vector, self.display_feature_names_in_.tolist()
)

PythonCall.GC.disable()
# Call to Julia backend.
# See https://github.com/MilesCranmer/SymbolicRegression.jl/blob/master/src/SymbolicRegression.jl
self.raw_julia_state_ = SymbolicRegression.equation_search(
Expand All @@ -1738,6 +1745,7 @@ def _run(self, X, y, mutated_params, weights, seed):
progress=progress and self.verbosity > 0 and len(y.shape) == 1,
verbosity=int(self.verbosity),
)
PythonCall.GC.enable()

# Set attributes
self.equations_ = self.get_hof()
Expand Down

0 comments on commit 4037c2d

Please sign in to comment.