Get specified CUDA device property values without initializing CUDA in the current process.
(device, names: Sequence[str], init_cuda=False)
| 83 | |
| 84 | |
| 85 | def cuda_get_device_properties(device, names: Sequence[str], init_cuda=False) -> tuple[Any, ...]: |
| 86 | """Get specified CUDA device property values without initializing CUDA in |
| 87 | the current process.""" |
| 88 | if init_cuda or cuda_is_initialized(): |
| 89 | try: |
| 90 | props = paddle.device.cuda.get_device_properties(device) |
| 91 | result = [] |
| 92 | for name in names: |
| 93 | if name == "major": |
| 94 | value = props.major |
| 95 | elif name == "minor": |
| 96 | value = props.minor |
| 97 | elif name == "name": |
| 98 | value = props.name |
| 99 | elif name == "total_memory": |
| 100 | value = props.total_memory |
| 101 | elif name == "multi_processor_count": |
| 102 | value = props.multi_processor_count |
| 103 | else: |
| 104 | value = getattr(props, name) |
| 105 | result.append(value) |
| 106 | return tuple(result) |
| 107 | except Exception as e: |
| 108 | api_server_logger.debug(f"Warning: Failed to get CUDA properties: {e}") |
| 109 | return tuple([None] * len(names)) |
| 110 | |
| 111 | # Run in subprocess to avoid initializing CUDA as a side effect. |
| 112 | try: |
| 113 | mp_ctx = multiprocessing.get_context("spawn") |
| 114 | except ValueError: |
| 115 | mp_ctx = multiprocessing.get_context() |
| 116 | with ProcessPoolExecutor(max_workers=1, mp_context=mp_ctx) as executor: |
| 117 | return executor.submit(cuda_get_device_properties, device, names, True).result() |
| 118 | |
| 119 | |
| 120 | def get_xpu_model(): |