2026/01/08

[Python] 自動判斷 並選定 最優 GPU : Intel GPU(Arc A350m) , nVidia GeForce RTX 4060 or CPU

手上 Notebook A 是 Intel Arc 350m , Notebook B 卻是 RTX 4060, 又有 無 GPU 的Notebook ... 
.....嗯嗯... 啥都有~ 就是沒有 AMD GPU :p 
由於 常會更換機器使用,需要一個 能 自動判定/選擇 最佳GPU 的 Python Function   
網路上找許久 都不見有人提供相似功能   
在此紀錄一下, 期待日後介面統一的到來   

另,期待能人志士添加 AMD GPU判定辦法   
暫時使用 DirectML方式偵測 AMD GPU, 功能尚未確認過
AMD使用 DirectML方式 需先安裝:
pip install torch-directml
非AMD用戶 不建議服用, 極大可能會造成身體不適,氣血失調,驚奇不斷:
torch-directml 0.2.5.dev240914 requires torch==2.4.1, but you have torch 2.8.0 which is incompatible.
torch-directml 0.2.5.dev240914 requires torchvision==0.19.1, but you have torchvision 0.23.0 which is incompatible.


--
Intel GPU 需要先安裝 Intel Extension for PyTorch (IPEX)   

```
python -m pip install torch torchvision torchaudio intel_extension_for_pytorch --extra-index-url https://pytorch-extension.intel.com/release-whl/stable/xpu/us/
```


驗證(after reboot):
```
python -c "import torch; import intel_extension_for_pytorch as ipex; print(ipex.__version__)"
```

"import intel_extension_for_pytorch as ipex" 時,可能會遇到:
```
ERROR! Intel® Extension for PyTorch* needs to work with PyTorch 2.8.*, but PyTorch 2.9.1+cpu is found. Please switch to the matching version and run again.
``` 錯誤

>
pip install torch==2.8


``` torchaudio 2.9.1 requires torch==2.9.1, but you have torch 2.8.0 which is incompatible.
torchvision 0.24.1 requires torch==2.9.1, but you have torch 2.8.0 which is incompatible.```

>
pip install torch==2.8 torchaudio==2.8 torchvision==0.23.0



或是遇到: ``` OSError: [WinError 126] The specified module could not be found.
Error loading "C:\.....\intel_extension_for_pytorch\bin\esimd_kernels.dll" or one of its dependencies. ```
Ref: https://github.com/intel/intel-extension-for-pytorch/issues/804#issuecomment-2840629788 (IE Only)
python -m pip install torch==2.8.0+xpu torchaudio torchvision --index-url https://download.pytorch.org/whl/xpu
python -m pip install intel-extension-for-pytorch==2.8.10+xpu --index-url https://pytorch-extension.intel.com/release-whl/stable/xpu/us/
And, Install Latest supported redistributable version by MS , and reboot

* nVidia 安裝 torch 即可:

pip install torch


CODE:

import torch import sys HAS_IPEX = False HAS_DML = False # 嘗試導入 Intel Extension for PyTorch # Try to import Intel Extension for PyTorch try: import intel_extension_for_pytorch as ipex HAS_IPEX = True except ImportError: HAS_IPEX = False # 嘗試導入 PyTorch DirectML (用於 AMD GPU 支援) try: import torch_directml HAS_DML = True except ImportError: HAS_DML = False def get_best_device(): """ 自動檢測並返回最佳可用設備 (Auto-detect and return best available device). 優先級 Priority: 1. NVIDIA GPU (CUDA) 2. Intel GPU (XPU) 3. AMD GPU (DirectML) 4. CPU """ # 1. 檢查 NVIDIA CUDA if torch.cuda.is_available(): print(f"[設備] 偵測到 NVIDIA GPU: {torch.cuda.get_device_name(0)}") return torch.device("cuda") # 2. 檢查 Intel XPU (Arc / Iris Xe) # PyTorch 2.4+ 可能直接支援 torch.xpu,但通常需要 IPEX if HAS_IPEX and hasattr(torch, 'xpu') and torch.xpu.is_available(): print(f"[設備] 偵測到 Intel GPU (XPU): {torch.xpu.get_device_name(0)}") return torch.device("xpu") # 用於舊版 IPEX 的備用方案 if HAS_IPEX and hasattr(ipex, 'xpu') and ipex.xpu.is_available(): print(f"[設備] 偵測到 Intel GPU (XPU via IPEX)") return torch.device("xpu") # 3. 檢查 AMD GPU (DirectML) if HAS_DML and torch_directml.is_available(): # DirectML 設備 ID 通常為 0 dml_device = torch_directml.device() print(f"[設備] 偵測到 AMD/DirectML GPU: {torch_directml.device_name(0)}") return dml_device # 4. 降級至 CPU print("[設備] 未偵測到 GPU。使用 CPU。") return torch.device("cpu") def print_system_info(): print(f"Python Version: {sys.version}") print(f"PyTorch Version: {torch.__version__}") print(f"Intel Extension (IPEX) 已安裝: {'是' if HAS_IPEX else '否'}") print(f"PyTorch DirectML (AMD) 已安裝: {'是' if HAS_DML else '否'}") if torch.cuda.is_available(): print(f"CUDA 可用: 是, 版本: {torch.version.cuda}") print(f"GPU 名稱: {torch.cuda.get_device_name(0)}") else: print("CUDA 可用: 否") if HAS_IPEX: try: # 註: IPEX 版本檢查方式可能有所不同 print(f"IPEX 版本: {ipex.__version__}") except: pass if hasattr(torch, 'xpu') and torch.xpu.is_available(): print(f"XPU 可用: 是") print(f"XPU 名稱: {torch.xpu.get_device_name(0)}") if HAS_DML and torch_directml.is_available(): print(f"DirectML 可用: 是") print(f"DirectML 設備: {torch_directml.device_name(0)}") if __name__ == "__main__": print("=== Hardware Detection Info ===") print_system_info() print("\n=== Selecting Device ===") device = get_best_device() print(f"Selected Device: {device}") # Example Usage in Training # model = MyModel().to(device) # data = data.to(device)

* Intel Arc GPU :
Python Version: 3.12.3 | packaged by conda-forge | (main, Apr 15 2024, 18:20:11) [MSC v.1938 64 bit (AMD64)] PyTorch Version: 2.8.0+xpu Intel Extension (IPEX) 已安裝: 是 PyTorch DirectML (AMD) 已安裝: 否 CUDA 可用: 否 IPEX 版本: 2.8.10+xpu XPU 可用: 是 XPU 名稱: Intel(R) Arc(TM) A350M Graphic
* nVidia RTX4060 GPU :
=== Hardware Detection Info === Python Version: 3.10.16 | packaged by Anaconda, Inc. | (main, Dec 11 2024, 16:19:12) [MSC v.1929 64 bit (AMD64)] PyTorch Version: 2.1.2+cu121 Intel Extension (IPEX) 已安裝: 否 PyTorch DirectML (AMD) 已安裝: 否 CUDA 可用: 是, 版本: 12.1 GPU 名稱: NVIDIA GeForce RTX 4060 Laptop GPU === Selecting Device === [設備] 偵測到 NVIDIA GPU: NVIDIA GeForce RTX 4060 Laptop GPU Selected Device: cuda