-
Notifications
You must be signed in to change notification settings - Fork 175
Expand file tree
/
Copy pathintrinsic.py
More file actions
328 lines (274 loc) · 9.62 KB
/
intrinsic.py
File metadata and controls
328 lines (274 loc) · 9.62 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
from collections.abc import Collection, Mapping, Sequence
from decimal import Decimal
from pathlib import Path
from typing import Any
import aiodocker
from ai.backend.agent import __version__
from ai.backend.agent.alloc_map import AllocationStrategy
from ai.backend.agent.resources import (
AbstractAllocMap,
AbstractComputeDevice,
AbstractComputePlugin,
DeviceAllocation,
DeviceSlotInfo,
DiscretePropertyAllocMap,
)
from ai.backend.agent.stats import (
ContainerMeasurement,
NodeMeasurement,
ProcessMeasurement,
StatContext,
)
from ai.backend.agent.types import Container, MountInfo
from ai.backend.common.types import (
AcceleratorMetadata,
DeviceId,
DeviceModelInfo,
DeviceName,
SlotName,
SlotTypes,
)
class CPUDevice(AbstractComputeDevice):
pass
class CPUPlugin(AbstractComputePlugin):
"""
Represents the CPU.
"""
resource_config: Mapping[str, Any]
config_watch_enabled = False
key = DeviceName("cpu")
slot_types = [
(SlotName("cpu"), SlotTypes.COUNT),
]
def __init__(
self,
plugin_config: Mapping[str, Any],
local_config: Mapping[str, Any],
dummy_config: Mapping[str, Any],
) -> None:
super().__init__(plugin_config, local_config)
self.resource_config = dummy_config["agent"]["resource"]
async def init(self, context: Any | None = None) -> None:
pass
async def cleanup(self) -> None:
pass
async def update_plugin_config(self, new_plugin_config: Mapping[str, Any]) -> None:
pass
def get_metadata(self) -> AcceleratorMetadata:
return {
"slot_name": "cpu",
"description": "CPU",
"human_readable_name": "CPU",
"display_unit": "Core",
"number_format": {"binary": False, "round_length": 0},
"display_icon": "cpu",
}
async def list_devices(self) -> Collection[AbstractComputeDevice]:
num_core: int = self.resource_config["cpu"]["num-core"]
return [
CPUDevice(
device_id=DeviceId(str(core_idx)),
hw_location="root",
numa_node=None,
memory_size=0,
processing_units=1,
)
for core_idx in range(num_core)
]
async def available_slots(self) -> Mapping[SlotName, Decimal]:
devices = await self.list_devices()
return {
SlotName("cpu"): Decimal(sum(dev.processing_units for dev in devices)),
}
def get_version(self) -> str:
return __version__
async def extra_info(self) -> Mapping[str, str]:
return {}
async def gather_node_measures(self, ctx: StatContext) -> Sequence[NodeMeasurement]:
return []
async def gather_container_measures(
self,
ctx: StatContext,
container_ids: Sequence[str],
) -> Sequence[ContainerMeasurement]:
return []
async def gather_process_measures(
self, ctx: StatContext, pid_map: Mapping[int, str]
) -> Sequence[ProcessMeasurement]:
return []
async def create_alloc_map(self) -> "AbstractAllocMap":
devices = await self.list_devices()
return DiscretePropertyAllocMap(
device_slots={
dev.device_id: DeviceSlotInfo(
SlotTypes.COUNT, SlotName("cpu"), Decimal(dev.processing_units)
)
for dev in devices
},
)
async def get_hooks(self, distro: str, arch: str) -> Sequence[Path]:
return []
async def generate_docker_args(
self,
docker: aiodocker.docker.Docker,
device_alloc: DeviceAllocation,
) -> Mapping[str, Any]:
# The Docker backend pins ``CpusetMems`` to the allocation's NUMA node
# when the allocation is node-local. The dummy backend intentionally
# skips that because it never actually runs containers; the output is
# only inspected by tests that exercise the plumbing, not NUMA policy.
cores = [*map(int, device_alloc[SlotName("cpu")].keys())]
sorted_core_ids = [*map(str, sorted(cores))]
return {
"HostConfig": {
"CpuPeriod": 100_000, # docker default
"CpuQuota": int(100_000 * len(cores)),
"Cpus": ",".join(sorted_core_ids),
"CpusetCpus": ",".join(sorted_core_ids),
},
}
async def restore_from_container(
self,
container: Container,
alloc_map: AbstractAllocMap,
) -> None:
return None
async def get_attached_devices(
self,
device_alloc: Mapping[SlotName, Mapping[DeviceId, Decimal]],
) -> Sequence[DeviceModelInfo]:
device_ids = [*device_alloc[SlotName("cpu")].keys()]
available_devices = await self.list_devices()
attached_devices: list[DeviceModelInfo] = []
for device in available_devices:
if device.device_id in device_ids:
attached_devices.append({
"device_id": device.device_id,
"model_name": "",
"data": {"cores": len(device_ids)},
})
return attached_devices
async def get_docker_networks(
self, device_alloc: Mapping[SlotName, Mapping[DeviceId, Decimal]]
) -> list[str]:
return []
async def generate_mounts(
self, source_path: Path, device_alloc: Mapping[SlotName, Mapping[DeviceId, Decimal]]
) -> list[MountInfo]:
return []
class MemoryDevice(AbstractComputeDevice):
pass
class MemoryPlugin(AbstractComputePlugin):
"""
Represents the main memory.
"""
resource_config: Mapping[str, Any]
config_watch_enabled = False
key = DeviceName("mem")
slot_types = [
(SlotName("mem"), SlotTypes.BYTES),
]
def __init__(
self,
plugin_config: Mapping[str, Any],
local_config: Mapping[str, Any],
dummy_config: Mapping[str, Any],
) -> None:
super().__init__(plugin_config, local_config)
self.resource_config = dummy_config["agent"]["resource"]
async def init(self, context: Any | None = None) -> None:
pass
async def cleanup(self) -> None:
pass
async def update_plugin_config(self, new_plugin_config: Mapping[str, Any]) -> None:
pass
def get_metadata(self) -> AcceleratorMetadata:
return {
"slot_name": "ram",
"description": "Memory",
"human_readable_name": "RAM",
"display_unit": "GiB",
"number_format": {"binary": True, "round_length": 0},
"display_icon": "cpu",
}
async def list_devices(self) -> Collection[AbstractComputeDevice]:
memory_size = self.resource_config["memory"]["size"]
return [
MemoryDevice(
device_id=DeviceId("root"),
device_name=self.key,
hw_location="root",
numa_node=0, # the kernel setting will do the job.
memory_size=memory_size,
processing_units=0,
),
]
async def available_slots(self) -> Mapping[SlotName, Decimal]:
devices = await self.list_devices()
return {
SlotName("mem"): Decimal(sum(dev.memory_size for dev in devices)),
}
def get_version(self) -> str:
return __version__
async def extra_info(self) -> Mapping[str, str]:
return {}
async def gather_node_measures(self, ctx: StatContext) -> Sequence[NodeMeasurement]:
return []
async def gather_container_measures(
self,
ctx: StatContext,
container_ids: Sequence[str],
) -> Sequence[ContainerMeasurement]:
return []
async def gather_process_measures(
self, ctx: StatContext, pid_map: Mapping[int, str]
) -> Sequence[ProcessMeasurement]:
return []
async def create_alloc_map(self) -> "AbstractAllocMap":
devices = await self.list_devices()
return DiscretePropertyAllocMap(
allocation_strategy=AllocationStrategy.FILL,
device_slots={
dev.device_id: DeviceSlotInfo(
SlotTypes.BYTES, SlotName("mem"), Decimal(dev.memory_size)
)
for dev in devices
},
)
async def get_hooks(self, distro: str, arch: str) -> Sequence[Path]:
return []
async def generate_docker_args(
self,
docker: aiodocker.docker.Docker,
device_alloc: DeviceAllocation,
) -> Mapping[str, Any]:
return {}
async def restore_from_container(
self,
container: Container,
alloc_map: AbstractAllocMap,
) -> None:
return None
async def get_attached_devices(
self,
device_alloc: Mapping[SlotName, Mapping[DeviceId, Decimal]],
) -> Sequence[DeviceModelInfo]:
device_ids = [*device_alloc[SlotName("mem")].keys()]
available_devices = await self.list_devices()
attached_devices: list[DeviceModelInfo] = []
for device in available_devices:
if device.device_id in device_ids:
attached_devices.append({
"device_id": device.device_id,
"model_name": "",
"data": {},
})
return attached_devices
async def get_docker_networks(
self, device_alloc: Mapping[SlotName, Mapping[DeviceId, Decimal]]
) -> list[str]:
return []
async def generate_mounts(
self, source_path: Path, device_alloc: Mapping[SlotName, Mapping[DeviceId, Decimal]]
) -> list[MountInfo]:
return []