Skip to content

Run specific code only once (which generates randomized values) before starting DDP #9134

Discussion options

You must be logged in to vote

Hey @Gateway2745,

You could do this

from pytorch_lightning.utilities.cli import LightningCLI
from unittest import mock
import optuna

config_path = ...

class MyModel(LightningModule):

    def __init__(self, num_layers):
        ...

def objective(trial):
    num_layers = trial.suggest_uniform('num_layers', 10, 100)

    with mock.patch("sys.argv", ["any.py", "--config", str(config_path), "--trainer.accelerator", "ddp_spawn", "--trainer.gpu", "2", "--model.num_layers", str(num_layers)]):
        cli = LightningCLI(MyModel, MyDataModule)

    return cli.trainer.model_checkpoint.best_score

study = optuna.create_study()
study.optimize(objective, n_trials=100)
study.best_params

Replies: 4 comments 8 replies

Comment options

You must be logged in to vote
3 replies
@Gateway2745
Comment options

@justusschock
Comment options

@Gateway2745
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
3 replies
@tchaton
Comment options

@justusschock
Comment options

@Gateway2745
Comment options

Comment options

You must be logged in to vote
2 replies
@Gateway2745
Comment options

@tchaton
Comment options

Answer selected by Gateway2745
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants