1- import logging
1+ import torch
2+
23from .platform import Platform
34from .cuda import CudaPlatform
4- from .npu import NPUPlatform
5+ from .npu import NpuPlatform
6+ from .rocm import RocmPlatform
7+ from .unknown import UnknownPlatform
8+ from .cpu import CpuPlatform
9+
10+ from roll .utils .logging import get_logger
11+
12+
13+ logger = get_logger ()
514
6- logger = logging .getLogger (__name__ )
715
816def _init_platform () -> Platform :
9- """Initialize and return the current platform instance.
17+ """
18+ Detect and initialize the appropriate platform based on available devices.
19+
20+ Priority:
21+ 1. CUDA (NVIDIA / AMD ROCm)
22+ 2. NPU (if torch_npu is installed)
23+ 3. CPU (fallback)
1024
11- Automatically selects the platform based on environment and availability:
12- - If torch_npu is installed, use NPUPlatform.
13- - Otherwise, fall back to CudaPlatform.
25+ Returns:
26+ An instance of a subclass of Platform corresponding to the detected hardware.
1427 """
15- try :
16- import torch_npu # noqa: F401
17- logger .info ("Detected torch_npu. Initializing NPU platform." )
18- return NPUPlatform ()
19- except ImportError :
20- logger .info ("Initializing ROLL default device backend: Cuda platform." )
21- return CudaPlatform ()
28+ if torch .cuda .is_available ():
29+ device_name = torch .cuda .get_device_name ().upper ()
30+ logger .info (f"Detected CUDA device: { device_name } " )
31+ if "NVIDIA" in device_name :
32+ logger .info ("Initializing CUDA platform (NVIDIA)." )
33+ return CudaPlatform ()
34+ elif "AMD" in device_name :
35+ logger .info ("Initializing ROCm platform (AMD)." )
36+ return RocmPlatform ()
37+ logger .warning ("Unrecognized CUDA device. Falling back to UnknownPlatform." )
38+ return UnknownPlatform ()
39+ else :
40+ try :
41+ import torch_npu # noqa: F401
42+
43+ logger .info ("Detected torch_npu. Initializing NPU platform." )
44+ return NpuPlatform ()
45+ except ImportError :
46+ logger .info ("No supported accelerator detected. Initializing CPU platform." )
47+ return CpuPlatform ()
48+
2249
2350# Global singleton representing the current platform in use.
2451current_platform : Platform = _init_platform ()
2552
2653__all__ = [
2754 "Platform" ,
2855 "current_platform" ,
29- ]
56+ ]
0 commit comments