optuna: Storage slowing down execution

Hi,

I have some troubles to make Optuna runs in parallel, and even in sequential but with a local storage. My test code is really simple:

import optuna

def objectif(trial):
    x = trial.suggest_float("x", -10., 10.)
    y = trial.suggest_float("y", -10., 10.)

    return (x - 5)**2 + (y + 4)**2

study = optuna.create_study(direction = "minimize", study_name = "TestOptuna",\
                            load_if_exists = True)
study.optimize(objectif, n_trials = 1000)

Executing the previous code takes : 8,83s. Now if I add some local storage with :

study = optuna.create_study(direction = "minimize", study_name = "TestOptuna",\
                            load_if_exists = True, storage = "mysql://root:#password@localhost/TestOptuna")

The execution takes : 2min6,6s. Same problem with sqlite.

Any idea from where the problem is coming from? Thank you for helping me!

  • Optuna version: 2.9.1
  • Python version: 3.8.11
  • OS: Linux-5.11.0-38-generic-x86_64-with-glibc2.17

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 24 (10 by maintainers)

Most upvoted comments

You can see throughout optuna/storages/_rdb/storage.py with _create_scoped_session(self.scoped_session) as session is being used whenever DB state is being mutated. Therefore each operation under the scoped_session is going to generate a DB commit, which is incredibly inefficient.

Here is the speed benchmark result. https://github.com/optuna/optuna/runs/4062350310?check_suite_focus=true#step:6:17 The slowing down ratio (inmemory, random, 1000 v.s. sqlite, random, 1000) is about x50 in the GitHub Actions’ environment.

For future development, please let me know your request? 1000 trials in 1 second?

Thank you for trying the code! I hope the optimisation with Redis performs not too slowly.

On my side with your code (using sqlite and removing the db):

%timeit in_memory_optimisation()
6.88 s ± 171 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

%timeit db_optimisation()
1min 42s ± 300 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

I’ll have a look at redis, thank you!

Thank you for your reply!

In my environment, the difference was smaller, 5.75 s vs 21.4 s.

import optuna
import os

optuna.logging.set_verbosity(100)

def objectif(trial):
    x = trial.suggest_float("x", -10., 10.)
    y = trial.suggest_float("y", -10., 10.)

    return (x - 5)**2 + (y + 4)**2
    
def in_memory_optimisation():
    study = optuna.create_study()
    study.optimize(objectif, n_trials=1000)

def db_optimisation():
    os.remove("example.db")    
    study = optuna.create_study(storage="sqlite:///example.db", study_name="my_study")
    study.optimize(objectif, n_trials=1000)

# On jupyter notebook or ipython
%timeit in_memory_optimisation()
# 5.75 s ± 271 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
%timeit db_optimisation()
# 21.4 s ± 161 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

My env is as follows:

  • Optuna version: master
  • Python version: 3.8.11
  • OS: M1 Mac book Air + latest MacOS

I suppose that accessing a database is more expensive than the main memory. So the down performance sounds natural. However, the reported difference is too large.

Practically, Redis storage might be faster than MySQL and SQLite.