question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

parallelisation is not working in optuna

See original GitHub issue

How can we execute the code in parallel in optuna. The documentation is so naive about that. Suppose I have a test.py file which includes the following code:

 study = optuna.create_study(
        direction="maximize",
        study_name="ali", #for parallel execution you need these couple of lines 
        storage="sqlite:///mydb.db", 
        load_if_exists= True, #to skip creating a new study if it already exists. 
        pruner = SuccessiveHalvingPruner(),
        #pruner=HyperbandPruner(min_resource=1, max_resource=100, reduction_factor=3),
    )

and I have another test2.py file which calls the test.py file like that:

import multiprocessing as mp
from concurrent.futures import ProcessPoolExecutor
import os
from multiprocessing import Pool

def run_process (process):
    os.system('python {}'.format(process))

if __name__ == "__main__":
    processes = ['TestDriver.py']
    pool = Pool()
    pool.map(run_process, processes)
    pool.close    

But the code:

  1. is not executed in parallel. I cannot see for example in the console: Trial 0 finsihed … Trial 1…
  2. The code creates two experiments in neptune.

Note to the questioner

If you are more comfortable with Stack Overflow, you may consider posting your question with the “optuna” tag there instead. Alternatively, for issues that would benefit from more of an interactive session with the developers, you may refer to the optuna/optuna chat on Gitter.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:16 (6 by maintainers)

github_iconTop GitHub Comments

7reactions
koh-joshuacommented, Feb 22, 2021

I think what ali3assi may be asking for is the good old n_jobs=-1: study.optimize(objective, n_trials=n_trials, n_jobs=-1)

This will utilize all the cores on your computer for the single study. However, depending on the complexity of the objective and number of trials, you will typically not see all cores utilized or 100% CPU usage. Also, since Optuna > 2.0, the n_jobs=-1 don’t really seem to be efficient in using multiple cores. Up until version 2.0, I can set n_jobs=-1 and launch multiple instances of Optuna and see my CPU usage hit 100% for 32 cores sustained. After 2.0, it seems that each study is locked to a process or there’s some limitations and this prevents my CPU usage scaling to 100%. The speed penalty is extreme.

For now, version 2.0 is gold to me.

What about distributed parallelism in Optuna using RDB? That is ok if your trial run is up to 100,000 or less, split or distributed between multiple instances of Optuna for the same study. It gets extremely slow after that. But due to the random seed initialization in the optimizers, you’d be better running multiple instances of Optuna to get a better tuned model or objective. At least in my own experience, I recover the best result from 5 instances/5 studies of 20,00 trials each versus one instance/study at 100,000 trials.

Hope this helps.

1reaction
ali3assicommented, Feb 16, 2021

@HideakiImamura Thanks for your reply. I am on windows. But I need to parallelize the code on a pc of 48 cpus, I need to open 48 terminales?

Read more comments on GitHub >

github_iconTop Results From Across the Web

4. Easy Parallelization — Optuna 3.0.4 documentation
It's straightforward to parallelize optuna.study.Study.optimize() . If you want to manually execute Optuna optimization: start an RDB server (this example ...
Read more >
optuna - How to set n_trials for multiple processes when using ...
When I execute code without parallel computation, n_trials in the optimize function means how many trials the program runs.
Read more >
Parallel Hyperparameter Tuning With Optuna and Kubeflow ...
Parallelize hyperparameter searches over multiple threads or processes without modifying code · Automated search for optimal hyperparameters ...
Read more >
optuna/optuna - Gitter
When using optuna in parallel (e.g. 4 GPUs running on different terminals with the same common database), how does n_trials and n_startup_trials behave?...
Read more >
A Guide To Parallelism and Resources - the Ray documentation
Parallelism is determined by per trial resources (defaulting to 1 CPU, ... Troubleshooting: Occasionally, you may run into GPU memory issues when running...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found