Skip to content

Commit

Permalink
Ensure julia is loaded as soon as possible
Browse files Browse the repository at this point in the history
  • Loading branch information
MilesCranmer committed Feb 7, 2024
1 parent 7aa7884 commit caf58a4
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 35 deletions.
8 changes: 7 additions & 1 deletion pysr/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
# This must be imported as early as possible to prevent
# library linking issues caused by numpy/pytorch/etc. importing
# old libraries:
from .julia_import import jl # isort:skip

from . import sklearn_monkeypatch
from .deprecated import best, best_callable, best_row, best_tex, pysr
from .export_jax import sympy2jax
from .export_torch import sympy2torch
from .sr import PySRRegressor, jl
from .sr import PySRRegressor

# This file is created by setuptools_scm during the build process:
from .version import __version__
Expand All @@ -20,4 +25,5 @@
"best_tex",
"pysr",
"__version__",
"jl",
]
35 changes: 2 additions & 33 deletions pysr/julia_helpers.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,8 @@
"""Functions for initializing the Julia environment and installing deps."""
import os
import sys
import warnings

if "juliacall" in sys.modules:
warnings.warn(
"juliacall module already imported. Make sure that you have set `PYTHON_JULIACALL_HANDLE_SIGNALS=yes` to avoid segfaults."
)

# Required to avoid segfaults (https://juliapy.github.io/PythonCall.jl/dev/faq/)
if os.environ.get("PYTHON_JULIACALL_HANDLE_SIGNALS", "yes") != "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."
)

if os.environ.get("JULIA_NUM_THREADS", "auto") != "auto":
warnings.warn(
"JULIA_NUM_THREADS environment variable is set to something other than 'auto', "
"so PySR was not able to set it. You may wish to set it to `'auto'` for full use "
"of your CPU."
)

# TODO: Remove these when juliapkg lets you specify this
for k, default in (
("PYTHON_JULIACALL_HANDLE_SIGNALS", "yes"),
("JULIA_NUM_THREADS", "auto"),
("JULIA_OPTIMIZE", "3"),
):
os.environ[k] = os.environ.get(k, default)


from juliacall import Main as jl # type: ignore
from juliacall import convert as jl_convert # type: ignore

from .julia_import import jl

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

Expand Down
32 changes: 32 additions & 0 deletions pysr/julia_import.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import os
import sys
import warnings

if "juliacall" in sys.modules:
warnings.warn(
"juliacall module already imported. Make sure that you have set `PYTHON_JULIACALL_HANDLE_SIGNALS=yes` to avoid segfaults."
)

# Required to avoid segfaults (https://juliapy.github.io/PythonCall.jl/dev/faq/)
if os.environ.get("PYTHON_JULIACALL_HANDLE_SIGNALS", "yes") != "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."
)

if os.environ.get("JULIA_NUM_THREADS", "auto") != "auto":
warnings.warn(
"JULIA_NUM_THREADS environment variable is set to something other than 'auto', "
"so PySR was not able to set it. You may wish to set it to `'auto'` for full use "
"of your CPU."
)

# TODO: Remove these when juliapkg lets you specify this
for k, default in (
("PYTHON_JULIACALL_HANDLE_SIGNALS", "yes"),
("JULIA_NUM_THREADS", "auto"),
("JULIA_OPTIMIZE", "3"),
):
os.environ[k] = os.environ.get(k, default)

from juliacall import Main as jl # type: ignore
2 changes: 1 addition & 1 deletion pysr/sr.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@
from .julia_helpers import (
_escape_filename,
_load_cluster_manager,
jl,
jl_array,
jl_deserialize_s,
)
from .julia_import import jl
from .utils import (
_csv_filename_to_pkl_filename,
_preprocess_julia_floats,
Expand Down

0 comments on commit caf58a4

Please sign in to comment.