Skip to content

Commit 6f857be

Browse files
committed
fix(BA-2851): Ensure new resource allocation format is used
1 parent d3095f3 commit 6f857be

File tree

2 files changed

+24
-23
lines changed

2 files changed

+24
-23
lines changed

src/ai/backend/agent/resources.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -584,9 +584,9 @@ def _calculate_total_slots(self) -> SlotsMap:
584584

585585
def _calculate_available_total_slots(self) -> SlotsMap:
586586
reserved_resources = {
587-
SlotName("cpu"): Decimal(self.local_config.resource_common.reserved_cpu),
588-
SlotName("mem"): Decimal(self.local_config.resource_common.reserved_mem),
589-
SlotName("disk"): Decimal(self.local_config.resource_common.reserved_disk),
587+
SlotName("cpu"): Decimal(self.local_config.resource.reserved_cpu),
588+
SlotName("mem"): Decimal(self.local_config.resource.reserved_mem),
589+
SlotName("disk"): Decimal(self.local_config.resource.reserved_disk),
590590
}
591591

592592
available_slots: dict[SlotName, Decimal] = {}
@@ -671,19 +671,18 @@ def _calculate_device_slot(
671671
f"Unrecognized AbstractAllocMap type {alloc_map_type}"
672672
)
673673
case ResourceAllocationMode.MANUAL:
674+
assert resource_config.allocations is not None
674675
match slot_name:
675676
case SlotName(device_name="cpu"):
676-
assert resource_config.allocated_cpu is not None
677-
return Decimal(resource_config.allocated_cpu)
677+
return Decimal(resource_config.allocations.cpu)
678678
case SlotName(device_name="mem"):
679-
assert resource_config.allocated_mem is not None
680-
return Decimal(resource_config.allocated_mem)
679+
return Decimal(resource_config.allocations.mem)
681680
case slot_name:
682-
if slot_name not in resource_config.allocated_devices:
681+
if slot_name not in resource_config.allocations.devices:
683682
raise ValueError(
684-
f"{slot_name=} not found in config {resource_config.allocated_devices!r}"
683+
f"{slot_name=} not found in config {resource_config.allocations.devices!r}"
685684
)
686-
return resource_config.allocated_devices[slot_name]
685+
return resource_config.allocations.devices[slot_name]
687686

688687
def _calculate_reserved_slots(self, device_slots: SlotsMap) -> SlotsMap:
689688
reserved_slots: dict[SlotName, Decimal] = {}
@@ -693,7 +692,7 @@ def _calculate_reserved_slots(self, device_slots: SlotsMap) -> SlotsMap:
693692
return reserved_slots
694693

695694
def _calculate_resource_scaling_factor(self, allocated_slots: SlotsMap) -> SlotsMap:
696-
match self.local_config.resource_common.allocation_mode:
695+
match self.local_config.resource.allocation_mode:
697696
case ResourceAllocationMode.SHARED:
698697
return defaultdict(lambda: Decimal(1.0))
699698
case ResourceAllocationMode.AUTO_SPLIT:
@@ -748,7 +747,7 @@ async def _scan_available_resources(self) -> Mapping[SlotName, Decimal]:
748747
return await dummy_scan(compute_device_types)
749748

750749
def _ensure_slots_are_not_overallocated(self) -> None:
751-
if self.local_config.resource_common.allocation_mode != ResourceAllocationMode.MANUAL:
750+
if self.local_config.resource.allocation_mode != ResourceAllocationMode.MANUAL:
752751
return
753752

754753
allocated_slots: dict[SlotName, Decimal] = defaultdict(lambda: Decimal("0"))

tests/agent/test_resource_allocation.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,16 @@ def create_test_config(
7676
}
7777

7878
# Add manual allocation fields if provided
79-
if allocated_cpu is not None:
80-
base_config["resource"]["allocated_cpu"] = allocated_cpu
81-
if allocated_mem is not None:
82-
base_config["resource"]["allocated_mem"] = BinarySize.finite_from_str(allocated_mem)
83-
if allocated_devices is not None:
84-
base_config["resource"]["allocated_devices"] = allocated_devices
79+
if allocated_cpu is not None or allocated_mem is not None or allocated_devices is not None:
80+
base_config["resource"]["allocations"] = {}
81+
if allocated_cpu is not None:
82+
base_config["resource"]["allocations"]["cpu"] = allocated_cpu
83+
if allocated_mem is not None:
84+
base_config["resource"]["allocations"]["mem"] = BinarySize.finite_from_str(
85+
allocated_mem
86+
)
87+
if allocated_devices is not None:
88+
base_config["resource"]["allocations"]["devices"] = allocated_devices
8589

8690
# Add multiple agents if requested
8791
if num_agents > 1:
@@ -93,13 +97,11 @@ def create_test_config(
9397
# In MANUAL mode, each agent needs allocation config
9498
if allocation_mode == ResourceAllocationMode.MANUAL:
9599
agent_override["resource"] = {
96-
"allocated_cpu": allocated_cpu,
97-
"allocated_mem": BinarySize.finite_from_str(allocated_mem)
98-
if allocated_mem
99-
else None,
100+
"cpu": allocated_cpu,
101+
"mem": BinarySize.finite_from_str(allocated_mem) if allocated_mem else None,
100102
}
101103
if allocated_devices:
102-
agent_override["resource"]["allocated_devices"] = allocated_devices
104+
agent_override["resource"]["devices"] = allocated_devices
103105
agents_list.append(agent_override)
104106
base_config["agents"] = agents_list
105107

0 commit comments

Comments
 (0)