Skip to content

Commit

Permalink
Disable pyrootutils changing work dir by default (ashleve#337)
Browse files Browse the repository at this point in the history
  • Loading branch information
ashleve authored Jun 8, 2022
1 parent e0b2407 commit 6804b9c
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 17 deletions.
5 changes: 0 additions & 5 deletions configs/hydra/default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@ defaults:
- override hydra_logging: colorlog
- override job_logging: colorlog

# save cfg to pickle for reproducibility
# callbacks:
# save_job_info:
# _target_: hydra.experimental.callbacks.PickleJobInfoCallback

# output directory, generated dynamically on each run
run:
dir: ${paths.log_dir}/${task_name}/runs/${name}/${now:%Y-%m-%d}_${now:%H-%M-%S}
Expand Down
13 changes: 9 additions & 4 deletions configs/paths/default.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
# path to folder with data
data_dir: data/
# path to root directory
# this requires PROJECT_ROOT environment variable to exist
# PROJECT_ROOT is inferred and set by pyrootutils package in `train.py` and `eval.py`
root_dir: ${oc.env:PROJECT_ROOT}

# path to folder with logs
log_dir: logs/
# path to data directory
data_dir: ${paths.root_dir}/data/

# path to logging directory
log_dir: ${paths.root_dir}/logs/

# path to output directory, created dynamically by hydra
# path generation pattern is specified in `configs/hydra/default.yaml`
Expand Down
2 changes: 1 addition & 1 deletion configs/trainer/ddp_sim.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ defaults:

# simulate DDP on CPU, useful for debugging
accelerator: cpu
devices: 4
devices: 2
strategy: ddp_spawn
2 changes: 1 addition & 1 deletion src/eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import pyrootutils
from omegaconf import DictConfig

root = pyrootutils.setup_root(__file__)
root = pyrootutils.setup_root(__file__, cwd=False)


@hydra.main(version_base="1.2", config_path=root / "configs", config_name="eval.yaml")
Expand Down
14 changes: 10 additions & 4 deletions src/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@
from omegaconf import DictConfig

# project root setup (allows for calling this script from any place)
# searches for root indicators in parent directories, like ".git", "pyproject.toml", "setup.py", etc.
# adds project root directory to the PYTHONPATH
# sets current working directory to the root directory
# searches for root indicator in parent directories, like ".git", "pyproject.toml", "setup.py", etc.
# sets PROJECT_ROOT environment variable (used for specifying paths in hydra config)
# loads environment variables from ".env" file if exists
# adds project root directory to the PYTHONPATH (so imports work correctly)
# https://github.com/ashleve/pyrootutils
root = pyrootutils.setup_root(__file__)
root = pyrootutils.setup_root(
search_from=__file__,
project_root_env_var=True,
dotenv=True,
pythonpath=True,
cwd=False, # do NOT change working directory to root (would cause problems in DDP mode)
)


@hydra.main(version_base="1.2", config_path=root / "configs", config_name="train.yaml")
Expand Down
7 changes: 5 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import pyrootutils
import pytest
from hydra import compose, initialize
from hydra.core.global_hydra import GlobalHydra
Expand All @@ -14,6 +15,7 @@ def cfg_train_global() -> DictConfig:

# 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
Expand All @@ -36,6 +38,7 @@ def cfg_eval_global() -> DictConfig:

# 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.gpus = 0
Expand All @@ -47,7 +50,7 @@ def cfg_eval_global() -> DictConfig:


# this is called by each test which uses `cfg_train` arg
# each test uses it's own temporary logging path
# each test generates it's own temporary logging path
@pytest.fixture(scope="function")
def cfg_train(cfg_train_global, tmp_path) -> DictConfig:
cfg = cfg_train_global.copy()
Expand All @@ -62,7 +65,7 @@ def cfg_train(cfg_train_global, tmp_path) -> DictConfig:


# this is called by each test which uses `cfg_eval` arg
# each test uses it's own temporary logging path
# each test generates it's own temporary logging path
@pytest.fixture(scope="function")
def cfg_eval(cfg_eval_global, tmp_path) -> DictConfig:
cfg = cfg_eval_global.copy()
Expand Down

0 comments on commit 6804b9c

Please sign in to comment.