Skip to content

Commit f1edbe6

Browse files
Integrate JournalStorage for Optuna concurrency safety
Add Journal File Storage.
1 parent 46f98be commit f1edbe6

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

generative-proof-of-concept-CPU-preprocessing-in-memory.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import optuna
2+
from optuna.storages import JournalStorage, JournalFileBackend
3+
from optuna.storages.journal import JournalFileOpenLock
24
import os
35
import mlflow
46
from datetime import datetime
@@ -22,11 +24,17 @@
2224

2325

2426
mlflow.set_tracking_uri(uri=f"http://127.0.0.1:{MLFLOW_PORT}")
27+
mlflow.set_experiment(EXPERIMENT_NAME)
2528

2629

30+
# Optuna Storage Essentials
31+
# Use JournalFileStorage to ensure concurrency safety
2732

28-
mlflow.set_experiment(EXPERIMENT_NAME)
33+
storage_file = f"./optuna_{EXPERIMENT_NAME}.log"
34+
lock_obj = JournalFileOpenLock(storage_file)
2935

36+
# Create the JournalStorage instance
37+
optuna_storage = JournalStorage(JournalFileBackend(storage_file, lock_obj=lock_obj))
3038

3139

3240
def objective(trial: optuna.Trial) -> float:
@@ -1445,8 +1453,7 @@ def main():
14451453
n_trials = N_TRIALS
14461454
# mlflow_parent = mlflow.start_run(run_name=os.getenv("MLFLOW_PARENT_RUN_NAME", "cerebros_poc_parent"), tags={"phase": "poc", "mode": "fast" if fast else "full"})
14471455
sampler = optuna.samplers.TPESampler(multivariate=True, n_startup_trials=5)
1448-
storage_name = f"sqlite:///{EXPERIMENT_NAME}.db
1449-
study = optuna.create_study(direction="minimize", sampler=sampler, storage=storage_name)
1456+
study = optuna.create_study(direction="minimize", sampler=sampler, storage=optuna_storage)
14501457
study.optimize(objective, n_trials=n_trials)
14511458
# mlflow.log_param("n_trials", n_trials)
14521459
# Log fixed (non-tunable) generation control param once at parent level

0 commit comments

Comments
 (0)