Coverage for cuda / core / system / _mig.pxi: 34.78%

23 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-04-29 01:27 +0000

1# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. 

2# 

3# SPDX-License-Identifier: Apache-2.0 

4  

5  

6from typing import Iterable 

7  

8  

9cdef class MigInfo: 

10 cdef Device _device 

11  

12 def __init__(self, device: Device): 

13 self._device = device 1ba

14  

15 @property 

16 def is_mig_device(self) -> bool: 

17 """ 

18 Whether this device is a MIG (Multi-Instance GPU) device. 

19  

20 A MIG device handle is an NVML abstraction which maps to a MIG compute 

21 instance. These overloaded references can be used (with some 

22 restrictions) interchangeably with a GPU device handle to execute 

23 queries at a per-compute instance granularity. 

24  

25 For Ampere™ or newer fully supported devices. 

26 """ 

27 return bool(nvml.device_is_mig_device_handle(self._device._handle)) 1ba

28  

29 @property 

30 def mode(self) -> bool: 

31 """ 

32 Get current MIG mode for the device. 

33  

34 For Ampere™ or newer fully supported devices. 

35  

36 Changing MIG modes may require device unbind or reset. The "pending" MIG 

37 mode refers to the target mode following the next activation trigger. 

38  

39 Returns 

40 ------- 

41 bool 

42 `True` if current MIG mode is enabled. 

43 """ 

44 current, _ = nvml.device_get_mig_mode(self._device._handle) 1a

45 return current == nvml.EnableState.FEATURE_ENABLED 1a

46  

47 @mode.setter 

48 def mode(self, mode: bool): 

49 """ 

50 Set the MIG mode for the device. 

51  

52 For Ampere™ or newer fully supported devices. 

53  

54 Changing MIG modes may require device unbind or reset. The "pending" MIG 

55 mode refers to the target mode following the next activation trigger. 

56  

57 Parameters 

58 ---------- 

59 mode: bool 

60 `True` to enable MIG mode, `False` to disable MIG mode. 

61 """ 

62 nvml.device_set_mig_mode( 

63 self._device._handle, 

64 nvml.EnableState.FEATURE_ENABLED if mode else nvml.EnableState.FEATURE_DISABLED 

65 ) 

66  

67 @property 

68 def pending_mode(self) -> bool: 

69 """ 

70 Get pending MIG mode for the device. 

71  

72 For Ampere™ or newer fully supported devices. 

73  

74 Changing MIG modes may require device unbind or reset. The "pending" MIG 

75 mode refers to the target mode following the next activation trigger. 

76  

77 If the device is not a MIG device, returns `False`. 

78  

79 Returns 

80 ------- 

81 bool 

82 `True` if pending MIG mode is enabled. 

83 """ 

84 _, pending = nvml.device_get_mig_mode(self._device._handle) 1a

85 return pending == nvml.EnableState.FEATURE_ENABLED 1a

86  

87 @property 

88 def device_count(self) -> int: 

89 """ 

90 Get the maximum number of MIG devices that can exist under this device. 

91  

92 Returns zero if MIG is not supported or enabled. 

93  

94 For Ampere™ or newer fully supported devices. 

95  

96 Returns 

97 ------- 

98 int 

99 The number of MIG devices (compute instances) on this GPU. 

100 """ 

101 return nvml.device_get_max_mig_device_count(self._device._handle) 1a

102  

103 @property 

104 def parent(self) -> Device: 

105 """ 

106 For MIG devices, get the parent GPU device. 

107  

108 For Ampere™ or newer fully supported devices. 

109  

110 Returns 

111 ------- 

112 Device 

113 The parent GPU device for this MIG device. 

114 """ 

115 parent_handle = nvml.device_get_device_handle_from_mig_device_handle(self._device._handle) 

116 parent_device = Device.__new__(Device) 

117 parent_device._handle = parent_handle 

118 return parent_device 

119  

120 def get_device_by_index(self, index: int) -> Device: 

121 """ 

122 Get MIG device for the given index under its parent device. 

123  

124 If the compute instance is destroyed either explicitly or by destroying, 

125 resetting or unbinding the parent GPU instance or the GPU device itself 

126 the MIG device handle would remain invalid and must be requested again 

127 using this API. Handles may be reused and their properties can change in 

128 the process. 

129  

130 For Ampere™ or newer fully supported devices. 

131  

132 Parameters 

133 ---------- 

134 index: int 

135 The index of the MIG device (compute instance) to retrieve. Must be 

136 between 0 and the value returned by `device_count - 1`. 

137  

138 Returns 

139 ------- 

140 Device 

141 The MIG device corresponding to the given index. 

142 """ 

143 mig_device_handle = nvml.device_get_mig_device_handle_by_index(self._device._handle, index) 

144 mig_device = Device.__new__(Device) 

145 mig_device._handle = mig_device_handle 

146 return mig_device 

147  

148 def get_all_devices(self) -> Iterable[Device]: 

149 """ 

150 Get all MIG devices under its parent device. 

151  

152 If the compute instance is destroyed either explicitly or by destroying, 

153 resetting or unbinding the parent GPU instance or the GPU device itself 

154 the MIG device handle would remain invalid and must be requested again 

155 using this API. Handles may be reused and their properties can change in 

156 the process. 

157  

158 For Ampere™ or newer fully supported devices. 

159  

160 Returns 

161 ------- 

162 list[Device] 

163 A list of all MIG devices corresponding to this GPU. 

164 """ 

165 for i in range(self.device_count): 1a

166 yield self.get_device_by_index(i)