Repository: microsoft/DeepSpeed
File: accelerator/real_accelerator.py (Python)

Code before:
        # 2. Some scenario like install on login node (without CUDA device)
        #    and run on compute node (with CUDA device) may cause mismatch
        #    between installation time and runtime.

        try:
            from intel_extension_for_deepspeed import XPU_Accelerator  # noqa: F401,F811 # type: ignore
            accelerator_name = "xpu.external"
        except ImportError as e:
            pass
        if accelerator_name is None:
            try:
                import intel_extension_for_pytorch as ipex

                if ipex._C._has_xpu():
                    accelerator_name = "xpu"
            except ImportError as e:
                pass
        if accelerator_name is None:
            try:
                import torch

                # torch.xpu supported in upstream pytorch.
                # Currently we can run on xpu device only using pytorch,
                # also reserve the old path using ipex when the torch version is old.
                if hasattr(torch, 'xpu'):
                    if torch.xpu.device_count() > 0 and torch.xpu.is_available():
                        accelerator_name = "xpu"
                    else:
                        pass
            except ImportError as e:
                pass
        if accelerator_name is None:
            try:
                import torch_npu  # noqa: F401,F811 # type: ignore

                accelerator_name = "npu"
            except ImportError as e:
                pass
        if accelerator_name is None:
            try:
                import torch_sdaa  # noqa: F401,F811 # type: ignore

                accelerator_name = "sdaa"
            except ImportError as e:
                pass
        if accelerator_name is None:
            try:
                import torch.mps

                # should use torch.mps.is_available() if it exists someday but this is used as proxy
                torch.mps.current_allocated_memory()

Review comment (suggestion):
I'm thinking about the following scenario: A desktop/laptop with an integrated XPU device and a discrete CUDA device.  In this case following the logic xpu will be selected.  But intuitively, CUDA device should be selected in this case.

To be more friendly to desktop users, could add one more check `torch.cuda.device_count == 0`?  This would be more safe for desktop CUDA device users.

Code after:
        # 2. Some scenario like install on login node (without CUDA device)
        #    and run on compute node (with CUDA device) may cause mismatch
        #    between installation time and runtime.

        try:
            from intel_extension_for_deepspeed import XPU_Accelerator  # noqa: F401,F811 # type: ignore
            accelerator_name = "xpu.external"
        except ImportError as e:
            pass
        if accelerator_name is None:
            try:
                import intel_extension_for_pytorch as ipex

                if ipex._C._has_xpu():
                    accelerator_name = "xpu"
            except ImportError as e:
                pass
        if accelerator_name is None:
            try:
                import torch

                # torch.xpu will be supported in upstream pytorch-2.8.
                # Currently we can run on xpu device only using pytorch,
                # also reserve the old path using ipex when the torch version is old.
                if hasattr(torch, 'xpu'):
                    if torch.cuda.device_count() == 0:  #ignore-cuda
                        if torch.xpu.device_count() > 0 and torch.xpu.is_available():
                            accelerator_name = "xpu"
                else:
                    pass
            except ImportError as e:
                pass
        if accelerator_name is None:
            try:
                import torch_npu  # noqa: F401,F811 # type: ignore

                accelerator_name = "npu"
            except ImportError as e:
                pass
        if accelerator_name is None:
            try:
                import torch_sdaa  # noqa: F401,F811 # type: ignore

                accelerator_name = "sdaa"
            except ImportError as e:
                pass
        if accelerator_name is None:
            try:
                import torch.mps

                # should use torch.mps.is_available() if it exists someday but this is used as proxy