@@ -189,7 +189,14 @@ def receive(self) -> int | NaType:
189
189
return self .rx
190
190
191
191
192
- _VALUE_OMITTED : str = object () # type: ignore[assignment]
192
+ # pylint: disable-next=missing-class-docstring,too-few-public-methods
193
+ class ValueOmitted :
194
+ def __repr__ (self ) -> str :
195
+ return '<VALUE OMITTED>'
196
+
197
+
198
+ __VALUE_OMITTED : str = ValueOmitted () # type: ignore[assignment]
199
+ del ValueOmitted
193
200
194
201
195
202
class Device : # pylint: disable=too-many-instance-attributes,too-many-public-methods
@@ -499,7 +506,7 @@ def from_cuda_indices(
499
506
500
507
@staticmethod
501
508
def parse_cuda_visible_devices (
502
- cuda_visible_devices : str | None = _VALUE_OMITTED ,
509
+ cuda_visible_devices : str | None = __VALUE_OMITTED ,
503
510
) -> list [int ] | list [tuple [int , int ]]:
504
511
"""Parse the given ``CUDA_VISIBLE_DEVICES`` value into a list of NVML device indices.
505
512
@@ -525,7 +532,7 @@ def parse_cuda_visible_devices(
525
532
return parse_cuda_visible_devices (cuda_visible_devices )
526
533
527
534
@staticmethod
528
- def normalize_cuda_visible_devices (cuda_visible_devices : str | None = _VALUE_OMITTED ) -> str :
535
+ def normalize_cuda_visible_devices (cuda_visible_devices : str | None = __VALUE_OMITTED ) -> str :
529
536
"""Parse the given ``CUDA_VISIBLE_DEVICES`` value and convert it into a comma-separated string of UUIDs.
530
537
531
538
This is an alias of :func:`normalize_cuda_visible_devices`.
@@ -2778,7 +2785,7 @@ def is_mig_device_uuid(uuid: str | None) -> bool:
2778
2785
2779
2786
2780
2787
def parse_cuda_visible_devices (
2781
- cuda_visible_devices : str | None = _VALUE_OMITTED ,
2788
+ cuda_visible_devices : str | None = __VALUE_OMITTED ,
2782
2789
) -> list [int ] | list [tuple [int , int ]]:
2783
2790
"""Parse the given ``CUDA_VISIBLE_DEVICES`` value into a list of NVML device indices.
2784
2791
@@ -2831,13 +2838,13 @@ def parse_cuda_visible_devices(
2831
2838
>>> parse_cuda_visible_devices('16') # invalid `CUDA_VISIBLE_DEVICES` (device ordinal out of range)
2832
2839
[]
2833
2840
""" # pylint: disable=line-too-long
2834
- if cuda_visible_devices is _VALUE_OMITTED :
2841
+ if cuda_visible_devices is __VALUE_OMITTED :
2835
2842
cuda_visible_devices = os .getenv ('CUDA_VISIBLE_DEVICES' , default = None )
2836
2843
2837
2844
return _parse_cuda_visible_devices (cuda_visible_devices , format = 'index' )
2838
2845
2839
2846
2840
- def normalize_cuda_visible_devices (cuda_visible_devices : str | None = _VALUE_OMITTED ) -> str :
2847
+ def normalize_cuda_visible_devices (cuda_visible_devices : str | None = __VALUE_OMITTED ) -> str :
2841
2848
"""Parse the given ``CUDA_VISIBLE_DEVICES`` value and convert it into a comma-separated string of UUIDs.
2842
2849
2843
2850
This function is aliased by :meth:`Device.normalize_cuda_visible_devices`.
@@ -2888,7 +2895,7 @@ def normalize_cuda_visible_devices(cuda_visible_devices: str | None = _VALUE_OMI
2888
2895
>>> normalize_cuda_visible_devices('16') # invalid `CUDA_VISIBLE_DEVICES` (device ordinal out of range)
2889
2896
''
2890
2897
""" # pylint: disable=line-too-long
2891
- if cuda_visible_devices is _VALUE_OMITTED :
2898
+ if cuda_visible_devices is __VALUE_OMITTED :
2892
2899
cuda_visible_devices = os .getenv ('CUDA_VISIBLE_DEVICES' , default = None )
2893
2900
2894
2901
return ',' .join (_parse_cuda_visible_devices (cuda_visible_devices , format = 'uuid' ))
@@ -2904,14 +2911,17 @@ class _PhysicalDeviceAttrs(NamedTuple):
2904
2911
support_mig_mode : bool
2905
2912
2906
2913
2907
- _PHYSICAL_DEVICE_ATTRS = None
2908
- _GLOBAL_PHYSICAL_DEVICE = None
2909
- _GLOBAL_PHYSICAL_DEVICE_LOCK = threading .RLock ()
2914
+ _PHYSICAL_DEVICE_ATTRS : OrderedDict [ str , _PhysicalDeviceAttrs ] | None = None
2915
+ _GLOBAL_PHYSICAL_DEVICE : PhysicalDevice | None = None
2916
+ _GLOBAL_PHYSICAL_DEVICE_LOCK : threading . RLock = threading .RLock ()
2910
2917
2911
2918
2912
- def _get_all_physical_device_attrs () -> dict [str , _PhysicalDeviceAttrs ]:
2919
+ def _get_all_physical_device_attrs () -> OrderedDict [str , _PhysicalDeviceAttrs ]:
2913
2920
global _PHYSICAL_DEVICE_ATTRS # pylint: disable=global-statement
2914
2921
2922
+ if _PHYSICAL_DEVICE_ATTRS is not None :
2923
+ return _PHYSICAL_DEVICE_ATTRS
2924
+
2915
2925
with _GLOBAL_PHYSICAL_DEVICE_LOCK :
2916
2926
if _PHYSICAL_DEVICE_ATTRS is None :
2917
2927
_PHYSICAL_DEVICE_ATTRS = OrderedDict (
@@ -3099,7 +3109,7 @@ def strip_identifier(identifier: str) -> str:
3099
3109
3100
3110
3101
3111
def _parse_cuda_visible_devices_to_uuids (
3102
- cuda_visible_devices : str | None = _VALUE_OMITTED ,
3112
+ cuda_visible_devices : str | None = __VALUE_OMITTED ,
3103
3113
verbose : bool = True ,
3104
3114
) -> list [str ]:
3105
3115
"""Parse the given ``CUDA_VISIBLE_DEVICES`` environment variable in a separate process and return a list of device UUIDs.
@@ -3121,7 +3131,7 @@ def _parse_cuda_visible_devices_to_uuids(
3121
3131
libcuda.CUDAError:
3122
3132
If failed to parse the ``CUDA_VISIBLE_DEVICES`` environment variable.
3123
3133
""" # pylint: disable=line-too-long
3124
- if cuda_visible_devices is _VALUE_OMITTED :
3134
+ if cuda_visible_devices is __VALUE_OMITTED :
3125
3135
cuda_visible_devices = os .getenv ('CUDA_VISIBLE_DEVICES' , default = None )
3126
3136
3127
3137
# Do not inherit file descriptors and handles from the parent process
@@ -3138,10 +3148,7 @@ def _parse_cuda_visible_devices_to_uuids(
3138
3148
parser .start ()
3139
3149
parser .join ()
3140
3150
finally :
3141
- try :
3142
- parser .kill () # requires Python 3.7+
3143
- except AttributeError :
3144
- pass
3151
+ parser .kill ()
3145
3152
result = queue .get ()
3146
3153
3147
3154
if isinstance (result , Exception ):
0 commit comments