From 17fd9bacd6fe84fef5181f4419551224de9a54dd Mon Sep 17 00:00:00 2001 From: Kayla Meyer Date: Tue, 19 Nov 2024 14:15:11 +0100 Subject: [PATCH 1/4] update deeprvat environment file; -update pytorch, pytorch-lightening packages --- deeprvat_env.yaml | 17 +++++++++-------- deeprvat_env_no_gpu.yml | 7 ++++--- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/deeprvat_env.yaml b/deeprvat_env.yaml index a50346da..f83fe236 100644 --- a/deeprvat_env.yaml +++ b/deeprvat_env.yaml @@ -7,20 +7,24 @@ channels: dependencies: - click=8.1 - cudatoolkit=11.8 + - cython=0.29 - dask=2023.5 - fastparquet=0.5 - h5py=3.1 - mkl==2022.1.0 - numcodecs=0.11 - - numpy=1.21 + - numpy=1.24 - optuna=2.10 - pandas=1.5 + - parallel=20230922 + - pip=23.0.1 + - plotnine=0.10.1 - pyarrow=11.0 - pyranges=0.0.129 - python=3.8 - - pytorch=1.13 - - pytorch-cuda=11 - - pytorch-lightning=1.5 + - pytorch=2.4 + - pytorch-cuda=11.8 + - pytorch-lightning=2.4 - pyyaml=5.4 - regenie=3.4.1 - scikit-learn=1.1 @@ -29,12 +33,9 @@ dependencies: - snakemake=7.17 - sqlalchemy=1.4 - statsmodels=0.13 + - tensorboard=2.14 - tqdm=4.59 - zarr=2.13 - - Cython=0.29 - - parallel=20230922 - - pip=23.0.1 - - plotnine=0.10.1 - pip: - git+https://github.com/HealthML/seak@v0.4.3 - bgen==1.6.3 diff --git a/deeprvat_env_no_gpu.yml b/deeprvat_env_no_gpu.yml index bfefd719..6c6d65c9 100644 --- a/deeprvat_env_no_gpu.yml +++ b/deeprvat_env_no_gpu.yml @@ -10,14 +10,14 @@ dependencies: - h5py=3.1 - mkl==2022.1.0 - numcodecs=0.11 - - numpy=1.21 + - numpy=1.24 - optuna=2.10 - pandas=1.5 - pyarrow=11.0 - pyranges=0.0.129 - python=3.8 - - pytorch=1.13 - - pytorch-lightning=1.5 + - pytorch=2.4 + - pytorch-lightning=2.4 - pyyaml=5.4 - regenie=3.4.1 - scikit-learn=1.1 @@ -26,6 +26,7 @@ dependencies: - snakemake=7.17 - sqlalchemy=1.4 - statsmodels=0.13 + - tensorboard=2.14 - tqdm=4.59 - zarr=2.13 - Cython=0.29 From a5dae24b81e0f9e0ecb3b3d7c56daec0dea38581 Mon Sep 17 00:00:00 2001 From: Kayla Meyer Date: Tue, 19 Nov 2024 14:16:41 +0100 Subject: [PATCH 2/4] update pl_trainer and BaseModel to reflect pytorch-lightning 2.x updates --- deeprvat/deeprvat/models.py | 23 ++++++++++++++----- example/config/deeprvat_input_config.yaml | 3 ++- .../config/deeprvat_input_config_regenie.yaml | 3 ++- .../deeprvat_input_training_config.yaml | 3 ++- .../deeprvat_config.yaml | 2 +- .../expected/deeprvat_config.yaml | 3 ++- .../input/deeprvat_input_config.yaml | 3 ++- .../expected/deeprvat_config.yaml | 3 ++- .../input/deeprvat_input_config.yaml | 3 ++- .../expected/deeprvat_config.yaml | 3 ++- .../input/deeprvat_input_training_config.yaml | 3 ++- .../test_data/training/deeprvat_config.yaml | 3 ++- .../deeprvat_config.yaml | 2 +- 13 files changed, 39 insertions(+), 18 deletions(-) diff --git a/deeprvat/deeprvat/models.py b/deeprvat/deeprvat/models.py index 6fc189d4..2e66cc76 100644 --- a/deeprvat/deeprvat/models.py +++ b/deeprvat/deeprvat/models.py @@ -86,6 +86,9 @@ def __init__( name: METRICS[name]() for name in self.hparams.metrics["all"] } + self.test_step_outputs = [] + self.validation_step_outputs = [] + self.objective_mode = self.hparams.metrics.get("objective_mode", "min") if self.objective_mode == "max": self.best_objective = float("-inf") @@ -174,11 +177,11 @@ def validation_step(self, batch: dict, batch_idx: int): and corresponding ground truth values ("y_by_pheno"). """ y_by_pheno = {pheno: pheno_batch["y"] for pheno, pheno_batch in batch.items()} - return {"y_pred_by_pheno": self(batch), "y_by_pheno": y_by_pheno} + pred = {"y_pred_by_pheno": self(batch), "y_by_pheno": y_by_pheno} + self.validation_step_outputs.append(pred) + return pred - def validation_epoch_end( - self, prediction_y: List[Dict[str, Dict[str, torch.Tensor]]] - ): + def on_validation_epoch_end(self): """ Evaluate accumulated phenotype predictions at the end of the validation epoch. @@ -193,6 +196,7 @@ def validation_epoch_end( :return: None :rtype: None """ + prediction_y = self.validation_step_outputs y_pred_by_pheno = dict() y_by_pheno = dict() for result in prediction_y: @@ -233,6 +237,8 @@ def validation_epoch_end( self.best_objective, results[self.hparams.metrics["objective"]].item() ) + self.validation_step_outputs.clear() #free memory + def test_step(self, batch: dict, batch_idx: int): """ During testing, we do not compute backward passes, such that we can accumulate @@ -247,9 +253,11 @@ def test_step(self, batch: dict, batch_idx: int): and corresponding ground truth values ("y"). :rtype: dict """ - return {"y_pred": self(batch), "y": batch["y"]} + pred = {"y_pred": self(batch), "y": batch["y"]} + self.test_step_outputs.append(pred) + return pred - def test_epoch_end(self, prediction_y: List[Dict[str, torch.Tensor]]): + def on_test_epoch_end(self): """ Evaluate accumulated phenotype predictions at the end of the testing epoch. @@ -257,6 +265,7 @@ def test_epoch_end(self, prediction_y: List[Dict[str, torch.Tensor]]): and corresponding ground truth values obtained during the testing process. :type prediction_y: List[Dict[str, Dict[str, torch.Tensor]]] """ + prediction_y = self.test_step_outputs y_pred = torch.cat([p["y_pred"] for p in prediction_y]) y = torch.cat([p["y"] for p in prediction_y]) @@ -269,6 +278,8 @@ def test_epoch_end(self, prediction_y: List[Dict[str, torch.Tensor]]): self.best_objective, results[self.hparams.metrics["objective"]].item() ) + self.test_step_outputs.clear() #free memory + def configure_callbacks(self): return [ModelSummary()] diff --git a/example/config/deeprvat_input_config.yaml b/example/config/deeprvat_input_config.yaml index 397a7afe..83b7e932 100644 --- a/example/config/deeprvat_input_config.yaml +++ b/example/config/deeprvat_input_config.yaml @@ -109,7 +109,8 @@ seed_gene_results: #baseline_results # DeepRVAT training settings training: pl_trainer: #PyTorch Lightening trainer settings - gpus: 1 + accelerator: gpu + devices: 1 precision: 16 min_epochs: 50 max_epochs: 1000 diff --git a/example/config/deeprvat_input_config_regenie.yaml b/example/config/deeprvat_input_config_regenie.yaml index f74814fb..dafb9af2 100644 --- a/example/config/deeprvat_input_config_regenie.yaml +++ b/example/config/deeprvat_input_config_regenie.yaml @@ -109,7 +109,8 @@ seed_gene_results: #baseline_results # DeepRVAT training settings training: pl_trainer: #PyTorch Lightening trainer settings - gpus: 1 + accelerator: gpu + devices: 1 precision: 16 min_epochs: 50 max_epochs: 1000 diff --git a/example/config/deeprvat_input_training_config.yaml b/example/config/deeprvat_input_training_config.yaml index be04cbf2..4fe8b12f 100644 --- a/example/config/deeprvat_input_training_config.yaml +++ b/example/config/deeprvat_input_training_config.yaml @@ -100,7 +100,8 @@ seed_gene_results: #baseline_results # DeepRVAT training settings training: pl_trainer: #PyTorch Lightening trainer settings - gpus: 1 + accelerator: gpu + devices: 1 precision: 16 min_epochs: 50 max_epochs: 1000 diff --git a/tests/deeprvat/regenie/training_association_testing/deeprvat_config.yaml b/tests/deeprvat/regenie/training_association_testing/deeprvat_config.yaml index 2c4d3c83..a327bc3f 100644 --- a/tests/deeprvat/regenie/training_association_testing/deeprvat_config.yaml +++ b/tests/deeprvat/regenie/training_association_testing/deeprvat_config.yaml @@ -196,7 +196,7 @@ training: Platelet_count: {} pl_trainer: check_val_every_n_epoch: 1 - gpus: 0 + accelerator: cpu log_every_n_steps: 1 max_epochs: 1000 min_epochs: 50 diff --git a/tests/deeprvat/test_data/config/training_association_testing/expected/deeprvat_config.yaml b/tests/deeprvat/test_data/config/training_association_testing/expected/deeprvat_config.yaml index d338833b..e3215877 100644 --- a/tests/deeprvat/test_data/config/training_association_testing/expected/deeprvat_config.yaml +++ b/tests/deeprvat/test_data/config/training_association_testing/expected/deeprvat_config.yaml @@ -198,7 +198,8 @@ training: Apolipoprotein_B: {} pl_trainer: check_val_every_n_epoch: 1 - gpus: 1 + accelerator: gpu + devices: 1 log_every_n_steps: 1 max_epochs: 1000 min_epochs: 50 diff --git a/tests/deeprvat/test_data/config/training_association_testing/input/deeprvat_input_config.yaml b/tests/deeprvat/test_data/config/training_association_testing/input/deeprvat_input_config.yaml index 19d6858e..394d6413 100644 --- a/tests/deeprvat/test_data/config/training_association_testing/input/deeprvat_input_config.yaml +++ b/tests/deeprvat/test_data/config/training_association_testing/input/deeprvat_input_config.yaml @@ -109,7 +109,8 @@ seed_gene_results: #baseline_results # DeepRVAT training settings training: pl_trainer: #PyTorch Lightening trainer settings - gpus: 1 + accelerator: gpu + devices: 1 precision: 16 min_epochs: 50 max_epochs: 1000 diff --git a/tests/deeprvat/test_data/config/training_association_testing_cv/expected/deeprvat_config.yaml b/tests/deeprvat/test_data/config/training_association_testing_cv/expected/deeprvat_config.yaml index 3cf29e41..22c126a1 100644 --- a/tests/deeprvat/test_data/config/training_association_testing_cv/expected/deeprvat_config.yaml +++ b/tests/deeprvat/test_data/config/training_association_testing_cv/expected/deeprvat_config.yaml @@ -201,7 +201,8 @@ training: Apolipoprotein_B: {} pl_trainer: check_val_every_n_epoch: 1 - gpus: 1 + accelerator: gpu + devices: 1 log_every_n_steps: 1 max_epochs: 1000 min_epochs: 50 diff --git a/tests/deeprvat/test_data/config/training_association_testing_cv/input/deeprvat_input_config.yaml b/tests/deeprvat/test_data/config/training_association_testing_cv/input/deeprvat_input_config.yaml index 88a9d124..8abaea5f 100644 --- a/tests/deeprvat/test_data/config/training_association_testing_cv/input/deeprvat_input_config.yaml +++ b/tests/deeprvat/test_data/config/training_association_testing_cv/input/deeprvat_input_config.yaml @@ -109,7 +109,8 @@ seed_gene_results: #baseline_results # DeepRVAT training settings training: pl_trainer: #PyTorch Lightening trainer settings - gpus: 1 + accelerator: gpu + devices: 1 precision: 16 min_epochs: 50 max_epochs: 1000 diff --git a/tests/deeprvat/test_data/config/training_only/expected/deeprvat_config.yaml b/tests/deeprvat/test_data/config/training_only/expected/deeprvat_config.yaml index 46003763..98e8c589 100644 --- a/tests/deeprvat/test_data/config/training_only/expected/deeprvat_config.yaml +++ b/tests/deeprvat/test_data/config/training_only/expected/deeprvat_config.yaml @@ -85,7 +85,8 @@ training: Apolipoprotein_B: {} pl_trainer: check_val_every_n_epoch: 1 - gpus: 1 + accelerator: gpu + devices: 1 log_every_n_steps: 1 max_epochs: 1000 min_epochs: 50 diff --git a/tests/deeprvat/test_data/config/training_only/input/deeprvat_input_training_config.yaml b/tests/deeprvat/test_data/config/training_only/input/deeprvat_input_training_config.yaml index 48227520..b3c105d4 100644 --- a/tests/deeprvat/test_data/config/training_only/input/deeprvat_input_training_config.yaml +++ b/tests/deeprvat/test_data/config/training_only/input/deeprvat_input_training_config.yaml @@ -100,7 +100,8 @@ seed_gene_results: #baseline_results # DeepRVAT training settings training: pl_trainer: #PyTorch Lightening trainer settings - gpus: 1 + accelerator: gpu + devices: 1 precision: 16 min_epochs: 50 max_epochs: 1000 diff --git a/tests/deeprvat/test_data/training/deeprvat_config.yaml b/tests/deeprvat/test_data/training/deeprvat_config.yaml index 37da651c..35be534c 100644 --- a/tests/deeprvat/test_data/training/deeprvat_config.yaml +++ b/tests/deeprvat/test_data/training/deeprvat_config.yaml @@ -218,7 +218,8 @@ training: Urate: {} pl_trainer: check_val_every_n_epoch: 1 - gpus: 1 + accelerator: gpu + devices: 1 log_every_n_steps: 1 max_epochs: 1000 min_epochs: 50 diff --git a/tests/deeprvat/training_association_testing/deeprvat_config.yaml b/tests/deeprvat/training_association_testing/deeprvat_config.yaml index f1420bf6..d5c3ac97 100644 --- a/tests/deeprvat/training_association_testing/deeprvat_config.yaml +++ b/tests/deeprvat/training_association_testing/deeprvat_config.yaml @@ -197,7 +197,7 @@ training: Platelet_count: {} pl_trainer: check_val_every_n_epoch: 1 - gpus: 0 + accelerator: cpu log_every_n_steps: 1 max_epochs: 1000 min_epochs: 50 From a80932fc42d9e003907f7463e14c93c1fafe9145 Mon Sep 17 00:00:00 2001 From: PMBio Date: Tue, 19 Nov 2024 14:40:09 +0000 Subject: [PATCH 3/4] fixup! Format Python code with psf/black pull_request --- deeprvat/deeprvat/models.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/deeprvat/deeprvat/models.py b/deeprvat/deeprvat/models.py index 2e66cc76..ae3e4dc1 100644 --- a/deeprvat/deeprvat/models.py +++ b/deeprvat/deeprvat/models.py @@ -237,7 +237,7 @@ def on_validation_epoch_end(self): self.best_objective, results[self.hparams.metrics["objective"]].item() ) - self.validation_step_outputs.clear() #free memory + self.validation_step_outputs.clear() # free memory def test_step(self, batch: dict, batch_idx: int): """ @@ -278,7 +278,7 @@ def on_test_epoch_end(self): self.best_objective, results[self.hparams.metrics["objective"]].item() ) - self.test_step_outputs.clear() #free memory + self.test_step_outputs.clear() # free memory def configure_callbacks(self): return [ModelSummary()] From 5cd873eeaa20b547ef96e8ecfa98a9750e837c68 Mon Sep 17 00:00:00 2001 From: Kayla Meyer Date: Tue, 19 Nov 2024 15:44:54 +0100 Subject: [PATCH 4/4] update docs, as deeprvat env can support cuda when installed w/ conda or mamba --- docs/installation.md | 1 - 1 file changed, 1 deletion(-) diff --git a/docs/installation.md b/docs/installation.md index 66c2f91c..55151747 100644 --- a/docs/installation.md +++ b/docs/installation.md @@ -7,7 +7,6 @@ git clone git@github.com:PMBio/deeprvat.git 1. Change directory to the repository: `cd deeprvat` 1. Install the conda environment. We recommend using [mamba](https://mamba.readthedocs.io/en/latest/index.html), though you may also replace `mamba` with `conda` - *Note: [the current deeprvat env does not support cuda when installed with conda](https://github.com/PMBio/deeprvat/issues/16). Install using mamba for cuda support.* ```shell mamba env create -n deeprvat -f deeprvat_env.yaml ```