Repository: Lightning-AI/pytorch-lightning
File: tests/tuner/test_auto_gpu_select.py (Python)

Code before:
        pytest.param(False, 0, None),
        pytest.param(False, -1, None),
    ],
)
def test_combination_gpus_options(auto_select_gpus, gpus, expected_error):
    model = BoringModel()

    if expected_error:
        with pytest.raises(
            expected_error,
            match=r"auto_select_gpus=True, gpus=0 is not a valid configuration. Please select a valid number of GPU resources when using auto_select_gpus.",
        ):
            trainer = Trainer(auto_select_gpus=auto_select_gpus, gpus=gpus)
    else:
        trainer = Trainer(auto_select_gpus=auto_select_gpus, gpus=gpus)


@pytest.mark.skipif(
    torch.cuda.device_count() < 2, reason="test requires a number of GPU machine greater than 1"
)
@pytest.mark.parametrize(
    ["nb", "expected_gpu_idxs", "expected_error"],
    [
        pytest.param(0, [], MisconfigurationException),
        pytest.param(-1, [i for i in range(torch.cuda.device_count())], None),
        pytest.param(1, [0], None),
    ],
)
def test_pick_multiple_gpus(nb, expected_gpu_idxs, expected_error):
    if expected_error:
        with pytest.raises(
            expected_error,
            match=r"auto_select_gpus=True, gpus=0 is not a valid configuration. Please select a valid number of GPU resources when using auto_select_gpus.",
        ):
            pick_multiple_gpus(nb)
    else:
        assert expected_gpu_idxs == pick_multiple_gpus(nb)


Review comment (suggestion):
```suggestion
        (0, [], MisconfigurationException),
        (-1, [i for i in range(torch.cuda.device_count())], None),
        (1, [0], None),
```

Code after:
        (True, 0, MisconfigurationException),
        (True, -1, None),
        (False, 0, None),
        (False, -1, None),
    ],
)
def test_trainer_with_gpus_options_combination_at_available_gpus_env(
    auto_select_gpus, gpus, expected_error
):
    if expected_error:
        with pytest.raises(
            expected_error,
            match=re.escape(
                r"auto_select_gpus=True, gpus=0 is not a valid configuration.\
            Please select a valid number of GPU resources when using auto_select_gpus."
            ),
        ):
            trainer = Trainer(auto_select_gpus=auto_select_gpus, gpus=gpus)
    else:
        trainer = Trainer(auto_select_gpus=auto_select_gpus, gpus=gpus)


@pytest.mark.skipif(
    torch.cuda.device_count() < 2, reason="test requires a number of GPU machine greater than 1"
)
@pytest.mark.parametrize(
    ["nb", "expected_gpu_idxs", "expected_error"],
    [
        (0, [], MisconfigurationException),
        (-1, [i for i in range(torch.cuda.device_count())], None),
        (1, [0], None),
    ],
)
def test_pick_multiple_gpus(nb, expected_gpu_idxs, expected_error):
    if expected_error:
        with pytest.raises(
            expected_error,
            match=re.escape(
                r"auto_select_gpus=True, gpus=0 is not a valid configuration.\
            Please select a valid number of GPU resources when using auto_select_gpus."
            ),
        ):
            pick_multiple_gpus(nb)
    else:
        assert expected_gpu_idxs == pick_multiple_gpus(nb)