Skip to content

Commit 8bef40f

Browse files
carmoccaBorda
authored andcommitted
Fix tests on single-GPU machine (#16911)
(cherry picked from commit 888686e)
1 parent f6cb47a commit 8bef40f

File tree

3 files changed

+13
-12
lines changed

3 files changed

+13
-12
lines changed

tests/tests_fabric/plugins/precision/test_native_amp_integration.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ def after_backward(self, model):
6363
[
6464
("cpu", 16, torch.bfloat16),
6565
("cpu", "bf16", torch.bfloat16),
66-
pytest.param("cuda", 16, torch.float16, marks=RunIf(min_cuda_gpus=1)),
67-
pytest.param("cuda", "bf16", torch.bfloat16, marks=RunIf(min_cuda_gpus=1, bf16_cuda=True)),
66+
pytest.param("cuda", 16, torch.float16, marks=RunIf(min_cuda_gpus=2)),
67+
pytest.param("cuda", "bf16", torch.bfloat16, marks=RunIf(min_cuda_gpus=2, bf16_cuda=True)),
6868
],
6969
)
7070
def test_native_mixed_precision(accelerator, precision, expected_dtype):

tests/tests_fabric/test_connector.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ def test_set_devices_if_none_cpu():
380380
@RunIf(mps=False)
381381
def test_unsupported_strategy_types_on_cpu_and_fallback():
382382
with pytest.warns(UserWarning, match="is not supported on CPUs, hence setting `strategy='ddp"):
383-
connector = _Connector(strategy="dp", devices=2)
383+
connector = _Connector(accelerator="cpu", strategy="dp", devices=2)
384384
assert isinstance(connector.strategy, DDPStrategy)
385385

386386

@@ -462,9 +462,9 @@ def test_strategy_choice_ddp_spawn_cpu():
462462
@RunIf(skip_windows=True)
463463
@mock.patch("lightning_fabric.connector._IS_INTERACTIVE", True)
464464
def test_strategy_choice_ddp_fork_in_interactive():
465-
"""Test that when accelerator and strategy are unspecified, the connector chooses DDP Fork in interactive
466-
environments by default."""
467-
connector = _Connector(devices=2)
465+
"""Test that when strategy is unspecified, the connector chooses DDP Fork in interactive environments by
466+
default."""
467+
connector = _Connector(accelerator="cpu", devices=2)
468468
assert isinstance(connector.accelerator, CPUAccelerator)
469469
assert isinstance(connector.strategy, DDPStrategy)
470470
assert isinstance(connector.strategy.cluster_environment, LightningEnvironment)
@@ -766,7 +766,7 @@ def test_precision_selection_16_on_cpu_warns():
766766
with pytest.warns(
767767
UserWarning, match=r"precision=16\)` but native AMP is not supported on CPU. Using `precision='bf16"
768768
):
769-
_Connector(precision=16)
769+
_Connector(accelerator="cpu", precision=16)
770770

771771

772772
class MyNativeAMP(MixedPrecision):
@@ -784,6 +784,7 @@ def test_precision_selection_amp_ddp(strategy, devices, is_custom_plugin, plugin
784784
if is_custom_plugin:
785785
plugin = plugin_cls(16, "cpu")
786786
connector = _Connector(
787+
accelerator="cpu",
787788
precision=16,
788789
devices=devices,
789790
strategy=strategy,
@@ -833,7 +834,7 @@ def test_accelerator_strategy_from_environment(accelerator, strategy, expected_a
833834
env_vars["LT_STRATEGY"] = strategy
834835

835836
with mock.patch.dict(os.environ, env_vars):
836-
connector = _Connector()
837+
connector = _Connector(accelerator="cpu" if accelerator is None else "auto")
837838
assert isinstance(connector.accelerator, expected_accelerator)
838839
assert isinstance(connector.strategy, expected_strategy)
839840

@@ -877,7 +878,7 @@ def test_arguments_from_environment_collision():
877878
def test_fsdp_unsupported_on_cpu(_):
878879
"""Test that we raise an error if attempting to run FSDP without GPU."""
879880
with pytest.raises(ValueError, match="You selected the FSDP strategy but FSDP is only available on GPU"):
880-
_Connector(strategy="fsdp")
881+
_Connector(accelerator="cpu", strategy="fsdp")
881882

882883

883884
def test_connector_defaults_match_fabric_defaults():

tests/tests_pytorch/trainer/connectors/test_accelerator_connector.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -514,9 +514,9 @@ def test_strategy_choice_ddp_spawn_cpu():
514514
@RunIf(skip_windows=True)
515515
@mock.patch("pytorch_lightning.trainer.connectors.accelerator_connector._IS_INTERACTIVE", True)
516516
def test_strategy_choice_ddp_fork_in_interactive():
517-
"""Test that when accelerator and strategy are unspecified, the connector chooses DDP Fork in interactive
518-
environments by default."""
519-
trainer = Trainer(devices=2)
517+
"""Test that when strategy is unspecified, the connector chooses DDP Fork in interactive environments by
518+
default."""
519+
trainer = Trainer(accelerator="cpu", devices=2)
520520
assert isinstance(trainer.accelerator, CPUAccelerator)
521521
assert isinstance(trainer.strategy, DDPSpawnStrategy)
522522
assert isinstance(trainer.strategy.cluster_environment, LightningEnvironment)

0 commit comments

Comments
 (0)