Skip to content

Commit

Permalink
dev: autofind available gpu in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
CharlesGaydon committed Feb 6, 2024
1 parent 72c2195 commit 03d4921
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
8 changes: 4 additions & 4 deletions tests/myria3d/test_train_and_predict.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

import numpy as np
import pytest
from lightning.pytorch.accelerators import find_usable_cuda_devices


from myria3d.pctl.dataset.toy_dataset import TOY_LAS_DATA
from myria3d.pctl.dataset.utils import pdal_read_las_array
Expand Down Expand Up @@ -59,9 +61,7 @@ def test_FrenchLidar_RandLaNetDebug_with_gpu(toy_dataset_hdf5_path, tmpdir_facto
tmp_paths_overrides = _make_list_of_necesary_hydra_overrides_with_tmp_paths(
toy_dataset_hdf5_path, tmpdir
)
# We will always use the first GPU id for tests, because it always exists if there are some GPUs.
# Attention to concurrency with other processes using the GPU when running tests.
gpu_id = 0
gpu_id = find_usable_cuda_devices(1)
cfg_one_epoch = make_default_hydra_cfg(
overrides=[
"experiment=RandLaNetDebug",
Expand Down Expand Up @@ -216,7 +216,7 @@ def _run_test_right_after_training(
tmp_paths_overrides = _make_list_of_necesary_hydra_overrides_with_tmp_paths(
toy_dataset_hdf5_path, tmpdir
)
devices = "[0]" if accelerator == "gpu" else 1
devices = find_usable_cuda_devices(1) if accelerator == "gpu" else 1
cfg_test_using_trained_model = make_default_hydra_cfg(
overrides=[
"experiment=test", # sets task.task_name to "test"
Expand Down
9 changes: 7 additions & 2 deletions tests/runif.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pytest
import torch
from lightning.pytorch.accelerators import find_usable_cuda_devices

"""
Simplified from:
Expand Down Expand Up @@ -35,8 +36,12 @@ def __new__(
reasons = []

if min_gpus:
conditions.append(torch.cuda.device_count() < min_gpus)
reasons.append(f"GPUs>={min_gpus}")
try:
find_usable_cuda_devices(min_gpus)
conditions.append(False)
except (ValueError, RuntimeError) as _:
conditions.append(True)
reasons.append(f"GPUs>={min_gpus}")

reasons = [rs for cond, rs in zip(conditions, reasons) if cond]
return pytest.mark.skipif(
Expand Down

0 comments on commit 03d4921

Please sign in to comment.