-
Notifications
You must be signed in to change notification settings - Fork 854
Expand file tree
/
Copy pathdevice_map.go
More file actions
355 lines (312 loc) · 10.7 KB
/
Copy pathdevice_map.go
File metadata and controls
355 lines (312 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/**
# Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
**/
package rm
import (
"fmt"
"github.com/NVIDIA/go-nvlib/pkg/nvlib/device"
"github.com/NVIDIA/go-nvlib/pkg/nvlib/info"
"github.com/NVIDIA/go-nvml/pkg/nvml"
"k8s.io/klog/v2"
pluginapi "k8s.io/kubelet/pkg/apis/deviceplugin/v1beta1"
spec "github.com/NVIDIA/k8s-device-plugin/api/config/v1"
)
type deviceMapBuilder struct {
device.Interface
migStrategy *string
resources *spec.Resources
replicatedResources *spec.ReplicatedResources
newGPUDevice func(i int, gpu nvml.Device) (string, deviceInfo)
newMigDevice func(i int, j int, mig nvml.Device) (string, nvmlMigDevice)
}
// DeviceMap stores a set of devices per resource name.
type DeviceMap map[spec.ResourceName]Devices
// NewDeviceMap creates a device map for the specified NVML library and config.
func NewDeviceMap(devicelib device.Interface, config *spec.Config, platform info.Platform) (DeviceMap, error) {
sysfsRoot := spec.DefaultSysfsRoot
if config.Flags.SysfsRoot != nil && *config.Flags.SysfsRoot != "" {
sysfsRoot = *config.Flags.SysfsRoot
}
newGPUDevice := newNvmlGPUDevice(sysfsRoot)
if platform == info.PlatformWSL {
newGPUDevice = newWslAllGPUsDevice
}
b := deviceMapBuilder{
Interface: devicelib,
migStrategy: config.Flags.MigStrategy,
resources: &config.Resources,
replicatedResources: config.Sharing.ReplicatedResources(),
newGPUDevice: newGPUDevice,
newMigDevice: newMigDevice(sysfsRoot),
}
return b.build()
}
// build builds a map of resource names to devices.
func (b *deviceMapBuilder) build() (DeviceMap, error) {
devices, err := b.buildDeviceMapFromConfigResources()
if err != nil {
return nil, fmt.Errorf("error building device map from config.resources: %v", err)
}
devices, err = updateDeviceMapWithReplicas(b.replicatedResources, devices)
if err != nil {
return nil, fmt.Errorf("error updating device map with replicas from replicatedResources config: %v", err)
}
return devices, nil
}
// buildDeviceMapFromConfigResources builds a map of resource names to devices from spec.Config.Resources
func (b *deviceMapBuilder) buildDeviceMapFromConfigResources() (DeviceMap, error) {
deviceMap, err := b.buildGPUDeviceMap()
if err != nil {
return nil, fmt.Errorf("error building GPU device map: %v", err)
}
if *b.migStrategy == spec.MigStrategyNone {
return deviceMap, nil
}
migDeviceMap, err := b.buildMigDeviceMap()
if err != nil {
return nil, fmt.Errorf("error building MIG device map: %v", err)
}
var requireUniformMIGDevices bool
if *b.migStrategy == spec.MigStrategySingle {
requireUniformMIGDevices = true
}
err = b.assertAllMigDevicesAreValid(requireUniformMIGDevices)
if err != nil {
return nil, fmt.Errorf("invalid MIG configuration: %v", err)
}
if requireUniformMIGDevices && !deviceMap.isEmpty() && !migDeviceMap.isEmpty() {
return nil, fmt.Errorf("all devices on the node must be configured with the same migEnabled value")
}
deviceMap.merge(migDeviceMap)
return deviceMap, nil
}
// buildGPUDeviceMap builds a map of resource names to GPU devices
func (b *deviceMapBuilder) buildGPUDeviceMap() (DeviceMap, error) {
devices := make(DeviceMap)
err := b.VisitDevices(func(i int, gpu device.Device) error {
name, ret := gpu.GetName()
if ret != nvml.SUCCESS {
return fmt.Errorf("error getting product name for GPU: %v", ret)
}
migEnabled, err := gpu.IsMigEnabled()
if err != nil {
return fmt.Errorf("error checking if MIG is enabled on GPU: %v", err)
}
if migEnabled && *b.migStrategy != spec.MigStrategyNone {
return nil
}
for _, resource := range b.resources.GPUs {
if resource.Pattern.Matches(name) {
index, info := b.newGPUDevice(i, gpu)
return devices.setEntry(resource.Name, index, info)
}
}
return fmt.Errorf("GPU name '%v' does not match any resource patterns", name)
})
return devices, err
}
// buildMigDeviceMap builds a map of resource names to MIG devices
func (b *deviceMapBuilder) buildMigDeviceMap() (DeviceMap, error) {
devices := make(DeviceMap)
err := b.VisitMigDevices(func(i int, d device.Device, j int, mig device.MigDevice) error {
migProfile, err := mig.GetProfile()
if err != nil {
return fmt.Errorf("error getting MIG profile for MIG device at index '(%v, %v)': %v", i, j, err)
}
for _, resource := range b.resources.MIGs {
if resource.Pattern.Matches(migProfile.String()) {
index, info := b.newMigDevice(i, j, mig)
return devices.setEntry(resource.Name, index, info)
}
}
return fmt.Errorf("MIG profile '%v' does not match any resource patterns", migProfile)
})
return devices, err
}
// assertAllMigDevicesAreValid ensures that each MIG-enabled device has at least one MIG device
// associated with it.
func (b *deviceMapBuilder) assertAllMigDevicesAreValid(uniform bool) error {
err := b.VisitDevices(func(i int, d device.Device) error {
isMigEnabled, err := d.IsMigEnabled()
if err != nil {
return err
}
if !isMigEnabled {
return nil
}
migDevices, err := d.GetMigDevices()
if err != nil {
return err
}
if uniform && len(migDevices) == 0 {
return fmt.Errorf("device %v has no MIG devices configured", i)
}
if !uniform && len(migDevices) == 0 {
klog.Warningf("device %v has no MIG devices configured", i)
}
return nil
})
if err != nil {
return fmt.Errorf("at least one device with migEnabled=true was not configured correctly: %v", err)
}
if !uniform {
return nil
}
var previousAttributes *nvml.DeviceAttributes
return b.VisitMigDevices(func(i int, d device.Device, j int, m device.MigDevice) error {
attrs, ret := m.GetAttributes()
if ret != nvml.SUCCESS {
return fmt.Errorf("error getting device attributes: %v", ret)
}
if previousAttributes == nil {
previousAttributes = &attrs
} else if attrs != *previousAttributes {
return fmt.Errorf("more than one MIG device type present on node")
}
return nil
})
}
// setEntry sets the DeviceMap entry for the specified resource
func (d DeviceMap) setEntry(name spec.ResourceName, index string, device deviceInfo) error {
dev, err := BuildDevice(index, device)
if err != nil {
return fmt.Errorf("error building Device: %v", err)
}
d.insert(name, dev)
return nil
}
// insert adds the specified device to the device map
func (d DeviceMap) insert(name spec.ResourceName, dev *Device) {
if d[name] == nil {
d[name] = make(Devices)
}
d[name][dev.ID] = dev
}
// merge merges two devices maps
func (d DeviceMap) merge(o DeviceMap) {
for name, devices := range o {
for _, device := range devices {
d.insert(name, device)
}
}
}
// isEmpty checks whether a device map is empty
func (d DeviceMap) isEmpty() bool {
for _, devices := range d {
if len(devices) > 0 {
return false
}
}
return true
}
// getIDsOfDevicesToReplicate returns a list of dervice IDs that we want to replicate.
func (d DeviceMap) getIDsOfDevicesToReplicate(r *spec.ReplicatedResource) ([]string, error) {
devices, exists := d[r.Name]
if !exists {
return nil, nil
}
// If all devices for this resource type are to be replicated.
if r.Devices.All {
return devices.GetIDs(), nil
}
// If a specific number of devices for this resource type are to be replicated.
if r.Devices.Count > 0 {
if r.Devices.Count > len(devices) {
return nil, fmt.Errorf("requested %d devices to be replicated, but only %d devices available", r.Devices.Count, len(devices))
}
return devices.GetIDs()[:r.Devices.Count], nil
}
// If a specific set of devices for this resource type are to be replicated.
if len(r.Devices.List) > 0 {
var ids []string
for _, ref := range r.Devices.List {
if ref.IsUUID() {
d := devices.GetByID(string(ref))
if d == nil {
return nil, fmt.Errorf("no matching device with UUID: %v", ref)
}
ids = append(ids, d.ID)
}
if ref.IsGPUIndex() || ref.IsMigIndex() {
d := devices.GetByIndex(string(ref))
if d == nil {
return nil, fmt.Errorf("no matching device at index: %v", ref)
}
ids = append(ids, d.ID)
}
}
return ids, nil
}
return nil, fmt.Errorf("unexpected error")
}
// updateDeviceMapWithReplicas returns an updated map of resource names to devices with replica
// information from the active replicated resources config.
func updateDeviceMapWithReplicas(replicatedResources *spec.ReplicatedResources, oDevices DeviceMap) (DeviceMap, error) {
devices := make(DeviceMap)
// Begin by walking replicatedResources.Resources and building a map of just the resource names.
names := make(map[spec.ResourceName]bool)
for _, r := range replicatedResources.Resources {
names[r.Name] = true
}
// Copy over all devices from oDevices without a resource reference in TimeSlicing.Resources.
for r, ds := range oDevices {
if !names[r] {
devices[r] = ds
}
}
// Walk shared Resources and update devices in the device map as appropriate.
for _, resource := range replicatedResources.Resources {
r := resource
// Get the IDs of the devices we want to replicate from oDevices
ids, err := oDevices.getIDsOfDevicesToReplicate(&r)
if err != nil {
return nil, fmt.Errorf("unable to get IDs of devices to replicate for '%v' resource: %v", r.Name, err)
}
// Skip any resources not matched in oDevices
if len(ids) == 0 {
continue
}
// Add any devices we don't want replicated directly into the device map.
for _, d := range oDevices[r.Name].Difference(oDevices[r.Name].Subset(ids)) {
devices.insert(r.Name, d)
}
// Create replicated devices add them to the device map.
// Rename the resource for replicated devices as requested.
name := r.Name
if r.Rename != "" {
name = r.Rename
}
for _, id := range ids {
for i := 0; i < r.Replicas; i++ {
annotatedID := string(NewAnnotatedID(id, i))
original := oDevices[r.Name][id]
replicatedDevice := Device{
Device: pluginapi.Device{
ID: annotatedID,
Health: original.Health,
Topology: original.Topology,
},
Paths: original.Paths,
Index: original.Index,
TotalMemory: original.TotalMemory,
ComputeCapability: original.ComputeCapability,
Replicas: r.Replicas,
}
devices.insert(name, &replicatedDevice)
}
}
}
return devices, nil
}