MilesCranmer commited on
Commit
7a42396
1 Parent(s): 32b2f6e

Avoid disabling GC by simply copying inside Julia

Browse files
Files changed (1) hide show
  1. pysr/sr.py +31 -7
pysr/sr.py CHANGED
@@ -7,6 +7,7 @@ import shutil
7
  import sys
8
  import tempfile
9
  import warnings
 
10
  from datetime import datetime
11
  from io import StringIO
12
  from multiprocessing import cpu_count
@@ -1718,12 +1719,31 @@ class PySRRegressor(MultiOutputMixin, RegressorMixin, BaseEstimator):
1718
  else:
1719
  jl_y_variable_names = None
1720
 
1721
- PythonCall.GC.disable()
1722
- # Call to Julia backend.
1723
- # See https://github.com/MilesCranmer/SymbolicRegression.jl/blob/master/src/SymbolicRegression.jl
1724
- self.raw_julia_state_ = SymbolicRegression.equation_search(
1725
- jl_X,
1726
- jl_y,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1727
  weights=jl_weights,
1728
  niterations=int(self.niterations),
1729
  variable_names=jl_array(self.feature_names_in_.tolist()),
@@ -1741,7 +1761,11 @@ class PySRRegressor(MultiOutputMixin, RegressorMixin, BaseEstimator):
1741
  progress=progress and self.verbosity > 0 and len(y.shape) == 1,
1742
  verbosity=int(self.verbosity),
1743
  )
1744
- PythonCall.GC.enable()
 
 
 
 
1745
 
1746
  # Set attributes
1747
  self.equations_ = self.get_hof()
 
7
  import sys
8
  import tempfile
9
  import warnings
10
+ from collections import namedtuple
11
  from datetime import datetime
12
  from io import StringIO
13
  from multiprocessing import cpu_count
 
1719
  else:
1720
  jl_y_variable_names = None
1721
 
1722
+ # Because we call some multi-threading code, we first create the arguments.
1723
+ # We do a deep copy of them **within Julia**, so that
1724
+ # Python's garbage collection is unaware of them.
1725
+ jl._equation_search_args = (jl_X, jl_y)
1726
+ jl._equation_search_kwargs = namedtuple(
1727
+ "K",
1728
+ (
1729
+ "weights",
1730
+ "niterations",
1731
+ "variable_names",
1732
+ "display_variable_names",
1733
+ "y_variable_names",
1734
+ "X_units",
1735
+ "y_units",
1736
+ "options",
1737
+ "numprocs",
1738
+ "parallelism",
1739
+ "saved_state",
1740
+ "return_state",
1741
+ "addprocs_function",
1742
+ "heap_size_hint_in_bytes",
1743
+ "progress",
1744
+ "verbosity",
1745
+ ),
1746
+ )(
1747
  weights=jl_weights,
1748
  niterations=int(self.niterations),
1749
  variable_names=jl_array(self.feature_names_in_.tolist()),
 
1761
  progress=progress and self.verbosity > 0 and len(y.shape) == 1,
1762
  verbosity=int(self.verbosity),
1763
  )
1764
+ self.raw_julia_state_ = jl.seval(
1765
+ "deepcopy(SymbolicRegression.equation_search(deepcopy(_equation_search_args)...; deepcopy(_equation_search_kwargs)...))"
1766
+ )
1767
+ jl._equation_search_args = None
1768
+ jl._equation_search_kwargs = None
1769
 
1770
  # Set attributes
1771
  self.equations_ = self.get_hof()