Skip to content

Commit b97924a

Browse files
committed
Add missing file
1 parent 884c0a7 commit b97924a

File tree

1 file changed

+175
-0
lines changed

1 file changed

+175
-0
lines changed
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
6+
from typing import Iterable
7+
8+
9+
cdef class MigInfo:
10+
cdef Device _device
11+
12+
def __init__(self, device: Device):
13+
self._device = device
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))
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+
If the device is not a MIG device, returns `False`.
40+
41+
Returns
42+
-------
43+
bool
44+
`True` if current MIG mode is enabled.
45+
"""
46+
if not self.is_mig_device:
47+
return False
48+
49+
current, _ = nvml.device_get_mig_mode(self._device._handle)
50+
return current == nvml.EnableState.FEATURE_ENABLED
51+
52+
@mode.setter
53+
def mode(self, mode: bool):
54+
"""
55+
Set the MIG mode for the device.
56+
57+
For Ampere™ or newer fully supported devices.
58+
59+
Changing MIG modes may require device unbind or reset. The "pending" MIG
60+
mode refers to the target mode following the next activation trigger.
61+
62+
Parameters
63+
----------
64+
mode: bool
65+
`True` to enable MIG mode, `False` to disable MIG mode.
66+
"""
67+
if not self.is_mig_device:
68+
raise ValueError("Device is not a MIG device")
69+
70+
nvml.device_set_mig_mode(
71+
self._device._handle,
72+
nvml.EnableState.FEATURE_ENABLED if mode else nvml.EnableState.FEATURE_DISABLED
73+
)
74+
75+
@property
76+
def pending_mode(self) -> bool:
77+
"""
78+
Get pending MIG mode for the device.
79+
80+
For Ampere™ or newer fully supported devices.
81+
82+
Changing MIG modes may require device unbind or reset. The "pending" MIG
83+
mode refers to the target mode following the next activation trigger.
84+
85+
If the device is not a MIG device, returns `False`.
86+
87+
Returns
88+
-------
89+
bool
90+
`True` if pending MIG mode is enabled.
91+
"""
92+
if not self.is_mig_device:
93+
return False
94+
95+
_, pending = nvml.device_get_mig_mode(self._device._handle)
96+
return pending == nvml.EnableState.FEATURE_ENABLED
97+
98+
def get_device_count(self) -> int:
99+
"""
100+
Get the maximum number of MIG devices that can exist under this device.
101+
102+
Returns zero if MIG is not supported or enabled.
103+
104+
For Ampere™ or newer fully supported devices.
105+
106+
Returns
107+
-------
108+
int
109+
The number of MIG devices (compute instances) on this GPU.
110+
"""
111+
return nvml.device_get_max_mig_device_count(self._device._handle)
112+
113+
def get_parent_device(self) -> Device:
114+
"""
115+
For MIG devices, get the parent GPU device.
116+
117+
For Ampere™ or newer fully supported devices.
118+
119+
Returns
120+
-------
121+
Device
122+
The parent GPU device for this MIG device.
123+
"""
124+
parent_handle = nvml.device_get_handle_from_mig_device_handle(self._handle)
125+
parent_device = Device.__new__(Device)
126+
parent_device._handle = parent_handle
127+
return parent_device
128+
129+
def get_device_by_index(self, index: int) -> Device:
130+
"""
131+
Get MIG device for the given index under its parent device.
132+
133+
If the compute instance is destroyed either explicitly or by destroying,
134+
resetting or unbinding the parent GPU instance or the GPU device itself
135+
the MIG device handle would remain invalid and must be requested again
136+
using this API. Handles may be reused and their properties can change in
137+
the process.
138+
139+
For Ampere™ or newer fully supported devices.
140+
141+
Parameters
142+
----------
143+
index: int
144+
The index of the MIG device (compute instance) to retrieve. Must be
145+
between 0 and the value returned by `get_device_count() - 1`.
146+
147+
Returns
148+
-------
149+
Device
150+
The MIG device corresponding to the given index.
151+
"""
152+
mig_device_handle = nvml.device_get_mig_device_handle_by_index(self._device._handle, index)
153+
mig_device = Device.__new__(Device)
154+
mig_device._handle = mig_device_handle
155+
return mig_device
156+
157+
def get_all_devices(self) -> Iterable[Device]:
158+
"""
159+
Get all MIG devices under its parent device.
160+
161+
If the compute instance is destroyed either explicitly or by destroying,
162+
resetting or unbinding the parent GPU instance or the GPU device itself
163+
the MIG device handle would remain invalid and must be requested again
164+
using this API. Handles may be reused and their properties can change in
165+
the process.
166+
167+
For Ampere™ or newer fully supported devices.
168+
169+
Returns
170+
-------
171+
list[Device]
172+
A list of all MIG devices corresponding to this GPU.
173+
"""
174+
for i in range(self.get_device_count()):
175+
yield self.get_device_by_index(i)

0 commit comments

Comments
 (0)