File size: 2,567 Bytes
26225c5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | import pyrootutils
import pytest
from hydra import compose, initialize
from hydra.core.global_hydra import GlobalHydra
from omegaconf import DictConfig, open_dict
@pytest.fixture(scope="package")
def cfg_train_global() -> DictConfig:
with initialize(version_base="1.2", config_path="../configs"):
cfg = compose(config_name="train.yaml", return_hydra_config=True, overrides=[])
# set defaults for all tests
with open_dict(cfg):
cfg.paths.root_dir = str(pyrootutils.find_root())
cfg.trainer.max_epochs = 1
cfg.trainer.limit_train_batches = 0.01
cfg.trainer.limit_val_batches = 0.1
cfg.trainer.limit_test_batches = 0.1
cfg.trainer.accelerator = "cpu"
cfg.trainer.devices = 1
cfg.datamodule.num_workers = 0
cfg.datamodule.pin_memory = False
cfg.extras.print_config = False
cfg.extras.enforce_tags = False
cfg.logger = None
return cfg
@pytest.fixture(scope="package")
def cfg_eval_global() -> DictConfig:
with initialize(version_base="1.2", config_path="../configs"):
cfg = compose(config_name="eval.yaml", return_hydra_config=True, overrides=["ckpt_path=."])
# set defaults for all tests
with open_dict(cfg):
cfg.paths.root_dir = str(pyrootutils.find_root())
cfg.trainer.max_epochs = 1
cfg.trainer.limit_test_batches = 0.1
cfg.trainer.accelerator = "cpu"
cfg.trainer.devices = 1
cfg.datamodule.num_workers = 0
cfg.datamodule.pin_memory = False
cfg.extras.print_config = False
cfg.extras.enforce_tags = False
cfg.logger = None
return cfg
# this is called by each test which uses `cfg_train` arg
# each test generates its own temporary logging path
@pytest.fixture(scope="function")
def cfg_train(cfg_train_global, tmp_path) -> DictConfig:
cfg = cfg_train_global.copy()
with open_dict(cfg):
cfg.paths.output_dir = str(tmp_path)
cfg.paths.log_dir = str(tmp_path)
yield cfg
GlobalHydra.instance().clear()
# this is called by each test which uses `cfg_eval` arg
# each test generates its own temporary logging path
@pytest.fixture(scope="function")
def cfg_eval(cfg_eval_global, tmp_path) -> DictConfig:
cfg = cfg_eval_global.copy()
with open_dict(cfg):
cfg.paths.output_dir = str(tmp_path)
cfg.paths.log_dir = str(tmp_path)
yield cfg
GlobalHydra.instance().clear()
|