Skip to content

Commit 214b443

Browse files
benedikt-voelkelBenedikt Volkel
andauthored
Enable SQLite support (#32)
* make sure DB files are stored in optimisation cwd * fix issue when creating/loading study when multiprocessing Co-authored-by: Benedikt Volkel <[email protected]>
1 parent e53de3d commit 214b443

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

src/o2tuner/backends.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,28 +36,44 @@ def make_trial_directory(trial):
3636
return cwd
3737

3838

39+
def adjust_path_sqlite(storage, workdir):
40+
"""
41+
Make sure the path for SQLite is located either at an absolute path or relative to the specified workdir
42+
"""
43+
if not storage:
44+
return None
45+
if storage.find("sqlite:///") != 0 or not workdir:
46+
return storage
47+
path = storage[10:]
48+
if path[0] == "/":
49+
# Absolute path
50+
return storage
51+
return "sqlite:///" + join(workdir, path)
52+
53+
3954
def load_or_create_study(study_name=None, storage=None, sampler=None, workdir=None):
4055
"""
4156
Helper to load or create a study
4257
Returns tuple of whether it can run via DB interface and optuna.study.study.Study
4358
"""
59+
storage = adjust_path_sqlite(storage, workdir)
4460
if study_name and storage:
4561
# there is a database we can connect to for multiprocessing
4662
# Although optuna would come up with a unique name when study_name is None,
4763
# we force a name to be given by the user for those cases
4864
try:
4965
study = optuna.load_study(study_name=study_name, storage=storage, sampler=sampler)
5066
LOG.info(f"Loading existing study {study_name} from storage {storage}")
67+
return True, study
5168
except KeyError:
5269
study = optuna.create_study(study_name=study_name, storage=storage, sampler=sampler)
5370
LOG.info(f"Creating new study {study_name} at storage {storage}")
71+
return True, study
5472
except ImportError as exc:
5573
# Probably cannot import MySQL stuff
5674
LOG.info("Probably cannot import what is needed for database access. Will try to attempt a serial run.")
5775
LOG.info(exc)
58-
else:
59-
return True, study
60-
# This is a "one-time" in-memory study so we don't care so much for the name honestly, could be None
76+
6177
if study_name and workdir:
6278
# Try to see if there is a study saved here
6379
# Pickling is unsafe, we should try to find another way eventually

src/o2tuner/run.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def run_optimisation(cwd, name, config):
8585
this_dir = getcwd()
8686
# change to this cwd and afterwards back
8787
chdir(cwd)
88-
ret = optimise(func, optuna_config, work_dir=cwd, user_config=user_config)
88+
ret = optimise(func, optuna_config, work_dir="./", user_config=user_config)
8989
chdir(this_dir)
9090
return ret
9191

0 commit comments

Comments
 (0)