|
40 | 40 | InvalidResourceConfigError, |
41 | 41 | ResourceOverAllocatedError, |
42 | 42 | ) |
43 | | -from ai.backend.common.data.kernel.types import KernelResourceSpecData, MountData |
44 | 43 | from ai.backend.common.etcd import AsyncEtcd |
45 | | -from ai.backend.common.identifier.resource_slot import ResourceSlotName |
46 | 44 | from ai.backend.common.json import dump_json_str, load_json |
47 | 45 | from ai.backend.common.plugin import AbstractPlugin, BasePluginContext |
48 | 46 | from ai.backend.common.types import ( |
|
57 | 55 | MountPermission, |
58 | 56 | MountTypes, |
59 | 57 | ResourceSlot, |
60 | | - ResourceSlotEntry, |
61 | 58 | SlotName, |
62 | 59 | SlotTypes, |
63 | 60 | aobject, |
@@ -126,7 +123,7 @@ class KernelResourceSpec: |
126 | 123 | slots: ResourceSlot |
127 | 124 | """Stores the original user-requested resource slots.""" |
128 | 125 |
|
129 | | - allocations: MutableMapping[DeviceName, Mapping[SlotName, Mapping[DeviceId, Decimal]]] |
| 126 | + allocations: MutableMapping[DeviceName, DeviceAllocation] |
130 | 127 | """ |
131 | 128 | Represents the resource allocations for each slot (device) type and devices. |
132 | 129 | """ |
@@ -265,34 +262,35 @@ async def aread_from_file(cls, file: AsyncTextIOWrapper) -> Self: |
265 | 262 | text = "\n".join(await file.readlines()) |
266 | 263 | return cls.read_from_string(text) |
267 | 264 |
|
268 | | - def to_data(self) -> KernelResourceSpecData: |
269 | | - """ |
270 | | - Render this spec as the value events and logs carry it as. |
271 | | - """ |
272 | | - return KernelResourceSpecData( |
273 | | - slots=ResourceSlotEntry.from_resource_slot(self.slots), |
274 | | - allocations={ |
275 | | - dev_name: { |
276 | | - ResourceSlotName(str(slot_name)): dict(per_device_alloc) |
277 | | - for slot_name, per_device_alloc in dev_alloc.items() |
278 | | - } |
279 | | - for dev_name, dev_alloc in self.allocations.items() |
280 | | - }, |
281 | | - scratch_disk_size=self.scratch_disk_size, |
282 | | - mounts=[ |
283 | | - MountData( |
284 | | - type=mount.type, |
285 | | - source=mount.source, |
286 | | - target=mount.target, |
287 | | - permission=mount.permission, |
288 | | - ) |
289 | | - for mount in self.mounts |
290 | | - ], |
291 | | - unified_devices=[ |
292 | | - (device_name, ResourceSlotName(str(slot_name))) |
293 | | - for device_name, slot_name in self.unified_devices |
294 | | - ], |
295 | | - ) |
| 265 | + def to_json_serializable_dict(self) -> Mapping[str, Any]: |
| 266 | + o = attrs.asdict(self) |
| 267 | + for slot_name, alloc in o["slots"].items(): |
| 268 | + if known_slot_types.get(slot_name, "count") == "bytes": |
| 269 | + o["slots"] = f"{BinarySize(alloc):s}" |
| 270 | + else: |
| 271 | + o["slots"] = str(alloc) |
| 272 | + serialized_allocations = {} |
| 273 | + for dev_name, dev_alloc in o["allocations"].items(): |
| 274 | + serialized_dev_alloc = {} |
| 275 | + for slot_name, per_device_alloc in dev_alloc.items(): |
| 276 | + serialized_per_device_alloc = {} |
| 277 | + for dev_id, alloc in per_device_alloc.items(): |
| 278 | + if known_slot_types.get(slot_name, "count") == "bytes": |
| 279 | + serialized_alloc = f"{BinarySize(alloc):s}" |
| 280 | + else: |
| 281 | + serialized_alloc = str(alloc) |
| 282 | + serialized_per_device_alloc[str(dev_id)] = serialized_alloc |
| 283 | + serialized_dev_alloc[str(slot_name)] = serialized_per_device_alloc |
| 284 | + serialized_allocations[str(dev_name)] = serialized_dev_alloc |
| 285 | + o["allocations"] = serialized_allocations |
| 286 | + o["mounts"] = list(map(str, self.mounts)) |
| 287 | + o["unified_devices"] = [ |
| 288 | + (str(device_name), str(slot_name)) for device_name, slot_name in self.unified_devices |
| 289 | + ] |
| 290 | + return o |
| 291 | + |
| 292 | + def to_json(self) -> str: |
| 293 | + return dump_json_str(self.to_json_serializable_dict()) |
296 | 294 |
|
297 | 295 | @classmethod |
298 | 296 | def __get_pydantic_core_schema__( |
|
0 commit comments