prophet icon indicating copy to clipboard operation
prophet copied to clipboard

fixed parallel cmdstanpy execution

Open lucidfrontier45 opened this issue 3 years ago • 4 comments

This PR fixes parallel cmdstanpy execution during cross validation with threads or processes mode. cmdstanpy has a global tmp directory and it outputs all the result files to the same destination, which causes corrupted result. To fix this issue, I simply create temp directory every time the single_cutoff_forecast is called.

Without this fix, I got the following error during parallel cross validatoin with cmdstanpy backend

Importing plotly failed. Interactive plots will not work.
INFO:cmdstanpy:Chain [1] start processing
INFO:cmdstanpy:Chain [1] done processing
INFO:cmdstanpy:Chain [1] start processing
INFO:cmdstanpy:Chain [1] start processing
INFO:cmdstanpy:Chain [1] start processing
INFO:cmdstanpy:Chain [1] start processing
INFO:cmdstanpy:Chain [1] done processing
INFO:cmdstanpy:Chain [1] done processing
concurrent.futures.process._RemoteTraceback: 
"""
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/concurrent/futures/process.py", line 243, in _process_worker
    r = call_item.fn(*call_item.args, **call_item.kwargs)
  File "/usr/local/lib/python3.10/concurrent/futures/process.py", line 202, in _process_chunk
    return [fn(*args) for args in chunk]
  File "/usr/local/lib/python3.10/concurrent/futures/process.py", line 202, in <listcomp>
    return [fn(*args) for args in chunk]
  File "/home/du/workspace/numpyro_test/.venv/lib/python3.10/site-packages/prophet/diagnostics.py", line 236, in single_cutoff_forecast
    m.fit(history_c, **model.fit_kwargs)
  File "/home/du/workspace/numpyro_test/.venv/lib/python3.10/site-packages/prophet/forecaster.py", line 1171, in fit
    self.params = self.stan_backend.fit(stan_init, dat, **kwargs)
  File "/home/du/workspace/numpyro_test/.venv/lib/python3.10/site-packages/prophet/models.py", line 143, in fit
    self.stan_fit = self.model.optimize(**args)
  File "/home/du/workspace/numpyro_test/.venv/lib/python3.10/site-packages/cmdstanpy/model.py", line 636, in optimize
    mle = CmdStanMLE(runset)
  File "/home/du/workspace/numpyro_test/.venv/lib/python3.10/site-packages/cmdstanpy/stanfit/mle.py", line 37, in __init__
    self._set_mle_attrs(runset.csv_files[0])
  File "/home/du/workspace/numpyro_test/.venv/lib/python3.10/site-packages/cmdstanpy/stanfit/mle.py", line 55, in _set_mle_attrs
    self._metadata = InferenceMetadata(meta)
  File "/home/du/workspace/numpyro_test/.venv/lib/python3.10/site-packages/cmdstanpy/stanfit/metadata.py", line 20, in __init__
    stan_vars_dims, stan_vars_cols, stan_vars_types = parse_stan_vars(
  File "/home/du/workspace/numpyro_test/.venv/lib/python3.10/site-packages/cmdstanpy/utils.py", line 886, in parse_stan_vars
    dims = [int(x) for x in coords]
  File "/home/du/workspace/numpyro_test/.venv/lib/python3.10/site-packages/cmdstanpy/utils.py", line 886, in <listcomp>
    dims = [int(x) for x in coords]
ValueError: invalid literal for int() with base 10: '99584e-10'
"""

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/du/workspace/numpyro_test/parallel_cv.py", line 23, in <module>
    cv_df = diagnostics.cross_validation(m, "15 days", "10 days", parallel=parallel)
  File "/home/du/workspace/numpyro_test/.venv/lib/python3.10/site-packages/prophet/diagnostics.py", line 202, in cross_validation
    return pd.concat(predicts, axis=0).reset_index(drop=True)
  File "/home/du/workspace/numpyro_test/.venv/lib/python3.10/site-packages/pandas/util/_decorators.py", line 311, in wrapper
    return func(*args, **kwargs)
  File "/home/du/workspace/numpyro_test/.venv/lib/python3.10/site-packages/pandas/core/reshape/concat.py", line 347, in concat
    op = _Concatenator(
  File "/home/du/workspace/numpyro_test/.venv/lib/python3.10/site-packages/pandas/core/reshape/concat.py", line 401, in __init__
    objs = list(objs)
  File "/usr/local/lib/python3.10/concurrent/futures/process.py", line 567, in _chain_from_iterable_of_lists
    for element in iterable:
  File "/usr/local/lib/python3.10/concurrent/futures/_base.py", line 609, in result_iterator
    yield fs.pop().result()
  File "/usr/local/lib/python3.10/concurrent/futures/_base.py", line 446, in result
    return self.__get_result()
  File "/usr/local/lib/python3.10/concurrent/futures/_base.py", line 391, in __get_result
    raise self._exception
ValueError: invalid literal for int() with base 10: '99584e-10'
INFO:cmdstanpy:Chain [1] done processing
INFO:cmdstanpy:Chain [1] done processing
INFO:cmdstanpy:deleting tmpfiles dir: /tmp/tmpr47zodde
INFO:cmdstanpy:done

lucidfrontier45 avatar Apr 28 '22 15:04 lucidfrontier45

Hi @lucidfrontier45!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at [email protected]. Thanks!

facebook-github-bot avatar Apr 28 '22 15:04 facebook-github-bot

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

facebook-github-bot avatar Apr 28 '22 16:04 facebook-github-bot

@tcuongd and @lucidfrontier45 , this is the same bug that was fixed in the tests in this PR

https://github.com/facebook/prophet/pull/2088/files#diff-aaffc6762dd18b0eefb63cab26fd7555cfe89e04655e8b391bd9217f39209db6

akosfurton avatar May 12 '22 14:05 akosfurton

Thanks! To confirm, were you running Prophet 1.1 (i.e. with the fixes from #2088 applied)? I'm wondering if the fix to load_model() in that PR already addresses this issue.

No, I was using v1.0 at that time. The fix which @akosfurton mentioned looks fine. I can close this PR.

lucidfrontier45 avatar Jul 22 '22 00:07 lucidfrontier45