|
21 | 21 | from lightning_utilities.core.imports import RequirementCache |
22 | 22 | from typing_extensions import get_args |
23 | 23 |
|
24 | | -from lightning.fabric.accelerators import CPUAccelerator, CUDAAccelerator, MPSAccelerator |
| 24 | +from lightning.fabric.accelerators import CPUAccelerator, CUDAAccelerator, MPSAccelerator, XLAAccelerator |
25 | 25 | from lightning.fabric.plugins.precision.precision import _PRECISION_INPUT_STR, _PRECISION_INPUT_STR_ALIAS |
26 | 26 | from lightning.fabric.strategies import STRATEGY_REGISTRY |
27 | 27 | from lightning.fabric.utilities.consolidate_checkpoint import _process_cli_args |
|
37 | 37 | _SUPPORTED_ACCELERATORS = ("cpu", "gpu", "cuda", "mps", "tpu", "auto") |
38 | 38 |
|
39 | 39 |
|
| 40 | +def _choose_auto_accelerator() -> str: |
| 41 | + """Choose the best available accelerator for the current environment.""" |
| 42 | + if CUDAAccelerator.is_available(): |
| 43 | + return "cuda" |
| 44 | + if MPSAccelerator.is_available(): |
| 45 | + return "mps" |
| 46 | + if XLAAccelerator.is_available(): |
| 47 | + return "tpu" |
| 48 | + return "cpu" |
| 49 | + |
| 50 | + |
40 | 51 | def _get_supported_strategies() -> list[str]: |
41 | 52 | """Returns strategy choices from the registry, with the ones removed that are incompatible to be launched from the |
42 | 53 | CLI or ones that require further configuration by the user.""" |
@@ -187,10 +198,9 @@ def _set_env_variables(args: Namespace) -> None: |
187 | 198 |
|
188 | 199 | def _get_num_processes(accelerator: str, devices: str) -> int: |
189 | 200 | """Parse the `devices` argument to determine how many processes need to be launched on the current machine.""" |
190 | | - from lightning.pytorch.trainer.connectors.accelerator_connector import _AcceleratorConnector |
191 | 201 |
|
192 | 202 | if accelerator == "auto" or accelerator is None: |
193 | | - accelerator = _AcceleratorConnector._choose_auto_accelerator() |
| 203 | + accelerator = _choose_auto_accelerator() |
194 | 204 | if devices == "auto": |
195 | 205 | if accelerator == "cuda" or accelerator == "mps" or accelerator == "cpu": |
196 | 206 | devices = "1" |
|
0 commit comments