r"""Query visible devices.
()
| 36 | |
| 37 | |
| 38 | def _query_visible_devices(): |
| 39 | r"""Query visible devices.""" |
| 40 | visible_devices_str = os.getenv("CUDA_VISIBLE_DEVICES", "") |
| 41 | if not visible_devices_str: |
| 42 | visible_devices_str = os.getenv("NVIDIA_VISIBLE_DEVICES", "") |
| 43 | if not visible_devices_str or visible_devices_str == "void": |
| 44 | return [] |
| 45 | if visible_devices_str != "all": |
| 46 | try: |
| 47 | return visible_devices_str.split(",") |
| 48 | except: # pylint: disable=bare-except |
| 49 | logging.exception("Parse NVIDIA_VISIBLE_DEVICES failed:") |
| 50 | return [] |
| 51 | query_devices_command = ( |
| 52 | "nvidia-smi --query-gpu=uuid --format=csv,noheader 2>/dev/null" |
| 53 | ) |
| 54 | try: |
| 55 | with subprocess.Popen( |
| 56 | query_devices_command, |
| 57 | shell=True, |
| 58 | stderr=subprocess.STDOUT, |
| 59 | stdout=subprocess.PIPE, |
| 60 | ) as proc: |
| 61 | return [d for d in iter(proc.stdout.readline, b"") if d] |
| 62 | except (OSError, ValueError): |
| 63 | return [] |
| 64 | |
| 65 | |
| 66 | def launch(command): |