Skip to content

Commit 41db862

Browse files
authored
Restore the banked memory planner after the storage_base rename (pytorch#22231)
### Summary pytorch#21840 renamed _resolve_inplace_specs to _resolve_storage_base_specs and TensorSpec.inplace_base to storage_base, and deleted materialize_buffer. exir/banked_memory_planning.py imported the first of those, so the module stopped importing and exir/tests/test_banked_memory_planning.py failed at collection. That single collection error red-lighted every unittest, unittest-editable and unittest-release job on main. Moving the banked planner onto the new names is the whole fix. _resolve_storage_base_specs already applies storage_base_offset when it places a deferred spec inside its base's shared object, so the planner gains offset support without further change. The banked planner had no coverage of a non-zero offset, so a test pins down that a storage-backed spec lands at its offset inside the base, in the base's bank, without costing the bank anything. The packed_size test helper wanted only the total, so it sums the shared object sizes instead of reviving materialize_buffer. Authored with Claude Code.
1 parent 6755ea3 commit 41db862

2 files changed

Lines changed: 25 additions & 10 deletions

File tree

‎exir/banked_memory_planning.py‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
_compute_total_sizes,
3535
_does_not_overlap,
3636
_find_max_overlapping_allocations_offset,
37-
_resolve_inplace_specs,
37+
_resolve_storage_base_specs,
3838
AllocationSpec,
3939
get_node_tensor_specs,
4040
MemoryAlgoResult,
@@ -181,11 +181,13 @@ def __call__(
181181
self._check_bank_alignments(alignment)
182182
all_specs = list(specs)
183183

184-
# In-place outputs share their base's storage, so they inherit its bank
185-
# rather than being placed or counted against capacity.
186-
planned = [spec for spec in all_specs if spec.inplace_base is None]
187-
deferred_inplace = [spec for spec in all_specs if spec.inplace_base is not None]
188-
for spec in deferred_inplace:
184+
# Storage-backed specs live inside another spec's allocation, so they
185+
# inherit its bank rather than being placed or counted against capacity.
186+
planned = [spec for spec in all_specs if spec.storage_base is None]
187+
deferred_storage_base = [
188+
spec for spec in all_specs if spec.storage_base is not None
189+
]
190+
for spec in deferred_storage_base:
189191
spec.realign(alignment)
190192

191193
self._reject_non_cpu(all_specs)
@@ -236,9 +238,9 @@ def __call__(
236238
result.spec_dict[spec] = SpecAllocResult(mem_id, 0, 0)
237239
spec2obj[spec] = sobj
238240

239-
for spec in deferred_inplace:
241+
for spec in deferred_storage_base:
240242
result.spec_dict[spec] = SpecAllocResult(0, 0, 0)
241-
_resolve_inplace_specs(deferred_inplace, spec2obj, result)
243+
_resolve_storage_base_specs(deferred_storage_base, spec2obj, result)
242244

243245
result.bufsizes = _compute_total_sizes(
244246
objects, graph_module, 0, result, len(spec2obj)

‎exir/tests/test_banked_memory_planning.py‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
from executorch.exir.memory_planning import (
2323
collect_specs_from_nodes,
2424
greedy,
25-
materialize_buffer,
2625
MemoryPlanningAlgorithmSuite,
2726
pick_shared_obj,
2827
update_all_tensors_lifetime,
@@ -118,7 +117,7 @@ def packed_size(specs: Sequence[TensorSpec]) -> int:
118117
ordered = sorted(specs, key=lambda s: s.allocated_memory, reverse=True)
119118
for spec in ordered:
120119
pick_shared_obj(objects, spec, True)
121-
return materialize_buffer(objects)
120+
return sum(sobj.size for sobj in objects)
122121

123122

124123
def assert_no_free_readmission(
@@ -661,6 +660,20 @@ def test_inplace_spec_does_not_double_count_against_capacity(self) -> None:
661660
self.assertEqual(bufsizes[1], 1024)
662661
self.assertEqual(bufsizes[2], 0)
663662

663+
def test_storage_backed_spec_sits_at_its_offset_inside_the_base(self) -> None:
664+
base = make_spec(1024, (0, 10))
665+
aliased = make_spec(256, (0, 10))
666+
aliased.storage_base = base
667+
aliased.storage_base_offset = 512
668+
669+
bufsizes, alloc = plan(
670+
BankedGreedy(two_banks(1 * KiB, 64 * KiB)), [base, aliased]
671+
)
672+
self.assertEqual(alloc[id(aliased)][0], alloc[id(base)][0])
673+
self.assertEqual(alloc[id(aliased)][1], alloc[id(base)][1] + 512)
674+
self.assertEqual(bufsizes[1], 1024)
675+
self.assertEqual(bufsizes[2], 0)
676+
664677

665678
class TestPlanningPassIntegration(unittest.TestCase):
666679
class Net(torch.nn.Module):

0 commit comments

Comments
 (0)