-
Notifications
You must be signed in to change notification settings - Fork 288
Package compiler #230
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Package compiler #230
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
0fddf6f
Add PackageCompiler.jl to PySR project
MilesCranmer c865ccb
Add function to create simple sysimage
MilesCranmer bc50655
Add `create_sysimage` to `__init__`
MilesCranmer 9124934
Put `pysr.so` into correct directory
MilesCranmer f7a8f6c
Add option to create sysimage to `install()`
MilesCranmer 0e57673
Clean up sysimage creation
MilesCranmer 494d282
Move installation utilities to compilation module
MilesCranmer 8616793
Separate install and compile steps
MilesCranmer b2d5380
Working precompilation
MilesCranmer 65f77f7
Only try loading sysimage if Julia uninitialized
MilesCranmer 2e22926
Clean up sysimage check
MilesCranmer a3be3c1
Ensure that we compile an optimized image
MilesCranmer f8c0522
Format code with black
deepsource-autofix[bot] 3cb46f0
Improve time to check project dir
MilesCranmer b438421
Ensure that compiling twice works
MilesCranmer ded8eb0
Allow forced init without sysimage
MilesCranmer 6dbda5c
Fix re-install
MilesCranmer f183a23
Compile all packages into sysimage
MilesCranmer b348287
Format code with black
deepsource-autofix[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
"""Functions to create a sysimage for PySR.""" | ||
|
||
import os | ||
from pathlib import Path | ||
import warnings | ||
from multiprocessing import cpu_count | ||
|
||
import numpy as np | ||
from julia.api import JuliaError | ||
|
||
from .version import __symbolic_regression_jl_version__ | ||
from .julia_helpers import ( | ||
init_julia, | ||
_julia_version_assertion, | ||
_set_julia_project_env, | ||
_get_io_arg, | ||
_process_julia_project, | ||
_import_error, | ||
) | ||
|
||
|
||
def _add_sr_to_julia_project(Main, io_arg): | ||
Main.eval("using Pkg") | ||
Main.sr_spec = Main.PackageSpec( | ||
name="SymbolicRegression", | ||
url="https://github.com/MilesCranmer/SymbolicRegression.jl", | ||
rev="v" + __symbolic_regression_jl_version__, | ||
) | ||
Main.clustermanagers_spec = Main.PackageSpec( | ||
name="ClusterManagers", | ||
rev="v0.4.2", | ||
) | ||
Main.packagecompiler_spec = Main.PackageSpec( | ||
name="PackageCompiler", | ||
rev="v2.1.0", | ||
) | ||
Main.pycall_spec = Main.PackageSpec( | ||
name="PyCall", | ||
rev="v1.94.1", | ||
) | ||
Main.eval( | ||
"Pkg.add([" | ||
+ ", ".join( | ||
["sr_spec", "clustermanagers_spec", "packagecompiler_spec", "pycall_spec"] | ||
) | ||
+ f"], {io_arg})" | ||
) | ||
Main.eval(f'Pkg.build("PyCall", {io_arg})') | ||
|
||
|
||
def _update_julia_project(Main, is_shared, io_arg): | ||
try: | ||
if is_shared: | ||
_add_sr_to_julia_project(Main, io_arg) | ||
Main.eval("using Pkg") | ||
Main.eval(f"Pkg.resolve({io_arg})") | ||
except (JuliaError, RuntimeError) as e: | ||
raise ImportError(_import_error()) from e | ||
|
||
|
||
def install(julia_project=None, quiet=False, precompile=None): # pragma: no cover | ||
""" | ||
Install PyCall.jl and all required dependencies for SymbolicRegression.jl. | ||
|
||
Also updates the local Julia registry. | ||
""" | ||
import julia | ||
|
||
_julia_version_assertion() | ||
# Set JULIA_PROJECT so that we install in the pysr environment | ||
processed_julia_project, is_shared = _process_julia_project(julia_project) | ||
_set_julia_project_env(processed_julia_project, is_shared) | ||
|
||
if precompile == False: | ||
os.environ["JULIA_PKG_PRECOMPILE_AUTO"] = "0" | ||
|
||
julia.install(quiet=quiet) | ||
Main, init_log = init_julia( | ||
julia_project, quiet=quiet, use_sysimage=False, return_aux=True | ||
) | ||
io_arg = _get_io_arg(quiet) | ||
|
||
if precompile is None: | ||
precompile = init_log["compiled_modules"] | ||
|
||
if not precompile: | ||
Main.eval('ENV["JULIA_PKG_PRECOMPILE_AUTO"] = 0') | ||
|
||
if is_shared: | ||
# Install SymbolicRegression.jl: | ||
_add_sr_to_julia_project(Main, io_arg) | ||
|
||
Main.eval("using Pkg") | ||
Main.eval(f"Pkg.instantiate({io_arg})") | ||
if precompile: | ||
Main.eval(f"Pkg.precompile({io_arg})") | ||
|
||
if not quiet: | ||
warnings.warn( | ||
"It is recommended to restart Python after installing PySR's dependencies," | ||
" so that the Julia environment is properly initialized." | ||
) | ||
|
||
|
||
def compile( | ||
julia_project=None, | ||
quiet=False, | ||
sysimage_name="pysr.so", | ||
): | ||
"""Create a PackageCompiler.jl sysimage for SymbolicRegression.jl.""" | ||
Main = init_julia( | ||
julia_project=julia_project, | ||
quiet=quiet, | ||
use_sysimage=False, | ||
julia_kwargs={ | ||
"compiled_modules": False, | ||
"optimize": 3, | ||
"threads": cpu_count(), | ||
"compile": "all", | ||
}, | ||
) | ||
cur_project_dir = Main.eval("dirname(Base.active_project())") | ||
sysimage_path = str(Path(cur_project_dir) / sysimage_name) | ||
from julia import PackageCompiler | ||
|
||
Main.eval("using SymbolicRegression") | ||
|
||
PackageCompiler.create_sysimage(sysimage_path=sysimage_path) |
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could add a call here:
just so that the PyCall interface specific to PySR gets compiled too.