PySR icon indicating copy to clipboard operation
PySR copied to clipboard

[BUG]: Unhandled Task ERROR when running in multiprocessing

Open ibengtsson opened this issue 1 year ago • 4 comments

What happened?

Trying out multiprocessing now when it's compatible with TemplateExpressionSpec, I ran into a new problem when running a larger dataset without batching or with a larger batch size. Regardless if I stop the SR search, let it run its intended iterations or use a timeout, I get a long error message related to the distributed processes. It doesn't crash the code execution on the Python side though, but it does mess up the saved checkpoint file and creates deserialization errors when I try to load it again.

If I use a smaller dataset or a small batch size, I don't get error messages. Is it perhaps related to the chosen heap size on the Julia processes?

Version

1.5.8

Operating System

macOS

Package Manager

pip

Interface

Script (i.e., python my_script.py)

Relevant log output

Unhandled Task ERROR: Distributed.ProcessExitedException(3)
Stacktrace:
 [1] take!(c::Channel{Any})
   @ Base ./channels.jl:471
 [2] take!(::Distributed.RemoteValue)
   @ Distributed /opt/homebrew/Cellar/julia/1.10.4/share/julia/stdlib/v1.10/Distributed/src/remotecall.jl:726
 [3] remotecall_fetch(f::Function, w::Distributed.Worker, args::Distributed.RRID; kwargs::@Kwargs{})
   @ Distributed /opt/homebrew/Cellar/julia/1.10.4/share/julia/stdlib/v1.10/Distributed/src/remotecall.jl:461
 [4] remotecall_fetch(f::Function, w::Distributed.Worker, args::Distributed.RRID)
   @ Distributed /opt/homebrew/Cellar/julia/1.10.4/share/julia/stdlib/v1.10/Distributed/src/remotecall.jl:454
 [5] remotecall_fetch
   @ /opt/homebrew/Cellar/julia/1.10.4/share/julia/stdlib/v1.10/Distributed/src/remotecall.jl:492 [inlined]
 [6] call_on_owner
   @ /opt/homebrew/Cellar/julia/1.10.4/share/julia/stdlib/v1.10/Distributed/src/remotecall.jl:565 [inlined]
 [7] fetch(r::Distributed.Future)
   @ Distributed /opt/homebrew/Cellar/julia/1.10.4/share/julia/stdlib/v1.10/Distributed/src/remotecall.jl:619
 [8] (::SymbolicRegression.var"#63#68"{SymbolicRegression.SearchUtilsModule.SearchState{Float32, Float32, Expression{Float32, Node{Float32}, @NamedTuple{operators::Nothing, variable_names::Nothing}}, Distributed.Future, Distributed.RemoteChannel}, Int64, Int64})()
   @ SymbolicRegression ~/Dev/modelling/SRCustom/SymbolicRegression.jl/src/SymbolicRegression.jl:980

Extra Info

The error messages are usually repeated, so I guess they appear on most processes. Code example:

from pysr import PySRRegressor
import numpy as np
from sklearn.metrics import r2_score

def main():

    x = np.random.uniform(low=[-1, 3, 10], high=[6, 10, 100], size=(3000, 3))
    y = np.sin(x[:, 0] - x[:, 2] ** 3) * np.exp(x[:, 1] / 5 + x[:, 0]) 
    

    model = PySRRegressor(
        binary_operators=["+", "-", "*", "/", "^"],
        unary_operators=["sin", "exp"],
        constraints={"^": (1, 9)},
        parallelism="multiprocessing",
    )

    
    model.fit(x, y)
    
    preds = model.predict(x)
    score = r2_score(y, preds)
    print(score)

if __name__ == "__main__":
    main()

Changing the size of the input features array or using batching makes it not crash.

ibengtsson avatar May 23 '25 14:05 ibengtsson

These are harmless. When you use multiprocessing and exit early, the processes are killed before they have a chance to finish what they were doing. So it results in these lengthy error messages for each process that was killed.

Admittedly this is a bit annoying though! I'd like to have some way of avoiding these messages when the processes exit intentionally vs unintentionally.

The effect of a small batch size is probably just that the process is able to finish its processing. With a larger dataset it takes too long, so gets killed.

MilesCranmer avatar May 23 '25 20:05 MilesCranmer

Ah, thank you for pointing that out. It appears that the from_file()-method doesn't work at all for me when trying to load from a pickle-file. I thought it was the killed processes that caused this, but upon further tests it seems that this always happens now, regardless if I run in a distributed setting or not...

I have a minimal example here:

from pysr import PySRRegressor
import numpy as np

def main():

    x = np.random.uniform(low=[-1, 3, 10], high=[6, 10, 100], size=(3000, 3))
    y = np.sin(x[:, 0] - x[:, 2] ** 3) * np.exp(x[:, 1] / 5 + x[:, 0]) 
    

    model = PySRRegressor(
        binary_operators=["+", "-", "*", "/", "^"],
        unary_operators=["sin", "exp"],
        constraints={"^": (1, 9)},
        run_id="test",
    )

    
    model.fit(x, y)

    # load model
    loaded_model = PySRRegressor().from_file(run_directory="./outputs/test")

if __name__ == "__main__":
    main()

with an error message:

[ Info: Results saved to:
  - outputs/test/hall_of_fame.csv
Attempting to load model from outputs/test/checkpoint.pkl...
Traceback (most recent call last):
  File "/Users/isakbe/Dev/modelling/il-sr/il_sr/scripts/minimal_example.py", line 24, in <module>
    main()
  File "/Users/isakbe/Dev/modelling/il-sr/il_sr/scripts/minimal_example.py", line 21, in main
    loaded_model = PySRRegressor().from_file(run_directory="./outputs/test")
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/isakbe/Library/Caches/pypoetry/virtualenvs/il-sr-9TFUWRsR-py3.11/lib/python3.11/site-packages/pysr/sr.py", line 1140, in from_file
    model = cast("PySRRegressor", pkl.load(f))
                                  ^^^^^^^^^^^
EOFError: Ran out of input
``

ibengtsson avatar May 24 '25 13:05 ibengtsson

This is a different unrelated issue; is it okay if you make a new bug report?

MilesCranmer avatar May 25 '25 06:05 MilesCranmer

Of course!

ibengtsson avatar May 26 '25 13:05 ibengtsson