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 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771
|
# mypy: allow-untyped-defs
from __future__ import annotations
import collections
import dataclasses
import itertools
import pprint
from typing import Any, Dict, Iterable, List, Optional, Protocol
import sympy
import torch
from .. import config
from ..utils import _align, align, cache_on_self, CachedMethod, IndentedBuffer
from ..virtualized import V
from .wrapper import (
AllocateLine,
BufferLike,
FreeIfNotReusedLine,
MemoryPlanningLine,
NullLine,
ReuseLine,
)
@dataclasses.dataclass
class LiveRange:
"""
A range where a given tensor is live. Begin and end are both counters
representing points in the program of grouped memory operations.
Begin is inclusive, end is exclusive.
Invariant: begin <= end
"""
begin: float # int | +/-inf
end: float # int | +/-inf
def contains(self, other: LiveRange):
"""Is other entirely within self"""
return self.begin <= other.begin and other.end <= self.end
def join(self, other: LiveRange):
"""Combine two ranges using a union operation"""
return LiveRange(min(self.begin, other.begin), max(self.end, other.end))
def __len__(self):
return self.end - self.begin
class LiveRanges:
"""
A collection of LiveRange regions, allowing for non-contiguous
live regions.
Invariant: LiveRanges.ranges is in sorted order and non-overlapping
"""
def __init__(self, ranges: Iterable[LiveRange]):
ranges = [*sorted(ranges, key=lambda x: x.begin)]
self.ranges = ranges[:1]
for r in ranges[1:]:
assert self.ranges[-1].begin <= r.begin
if self.ranges[-1].end >= r.begin:
self.ranges[-1] = LiveRange.join(self.ranges[-1], r)
else:
self.ranges.append(r)
def overlaps(self, other: LiveRanges):
"""Check if any pair of ranges in self and other overlap"""
left = collections.deque(self.ranges)
right = collections.deque(other.ranges)
while left and right:
if left[0].begin > right[0].begin:
left, right = right, left
assert left[0].begin <= right[0].begin
if left[0].end > right[0].begin:
return True
left.popleft()
return False
@property
def begin(self):
return self.ranges[0].begin
@property
def end(self):
return self.ranges[-1].end
def __repr__(self):
return f"{self.__class__.__name__}([{', '.join(map(repr, self.ranges))}])"
class AllocationTreeNode:
"""
Abstract base class for nodes in allocation pool.
"""
def allocate(self, block: Allocation, is_last: bool) -> bool:
"""
Try to assign block to a memory location in this bool. Return True if
an assignment was made.
"""
return False
def get_live_ranges(self) -> LiveRanges:
"""Aggregate LiveRanges for all objects below this in tree"""
raise NotImplementedError
def get_size_hint(self) -> int:
"""Number of bytes used for example inputs"""
raise NotImplementedError
def get_symbolic_size(self) -> sympy.Expr:
"""Number of bytes needed at runtime"""
raise NotImplementedError
def finalize(self, pool, offset) -> AllocationTreeNode:
"""Called after all allocations have been made"""
return self
def is_empty(self):
return False
@dataclasses.dataclass
class Allocation(AllocationTreeNode):
"""
Represents memory allocated to a given node in the allocation pool.
"""
node: BufferLike
live_range: LiveRange
size_hint: int
symbolic_size: sympy.Expr
allocated: bool = False
pool: Optional[AllocationPool] = None
offset: Optional[sympy.Expr] = None
@property
def device(self):
return self.node.get_device()
def get_live_ranges(self):
return LiveRanges([self.live_range])
def get_size_hint(self):
return self.size_hint
def get_symbolic_size(self):
return self.symbolic_size
def mark_allocated(self):
assert not self.allocated
self.allocated = True
def finalize(self, pool, offset):
assert self.pool is None and self.offset is None
self.pool = pool
self.offset = offset
return self
def codegen_alloc_from_pool(self, wrapper):
assert self.pool
node = self.node
shape = tuple(node.get_size())
stride = tuple(node.get_stride())
return wrapper.codegen_alloc_from_pool(
self.pool.name, self.offset, node.get_dtype(), shape, stride
)
def __repr__(self):
return (
f"{self.__class__.__name__}("
f"node={self.node.get_name()}, "
f"live_range={self.live_range}, "
f"size_hint={self.size_hint}, "
f"symbolic_size={self.symbolic_size}, "
f"pool={self.pool.name if self.pool else None}, "
f"offset={self.offset})"
)
@dataclasses.dataclass
class Empty(AllocationTreeNode):
"""
Placeholder to represent empty space in the allocation pool.
Only exists to get the size_hint correct in parent nodes.
"""
size_hint: int
def get_live_ranges(self):
return LiveRanges([])
def get_size_hint(self):
return self.size_hint
def get_symbolic_size(self):
return 0
def is_empty(self):
return True
class MemorySplitProtocol(Protocol):
get_live_ranges: CachedMethod[[], LiveRanges]
get_size_hint: CachedMethod[[], int]
get_symbolic_size: CachedMethod[[], sympy.Expr]
def _allocate(self, block: Allocation, is_last: bool) -> bool:
...
class ClearCacheOnAllocateMixin(MemorySplitProtocol):
"""
Helper to assist in caching get_live_ranges, get_size_hint, and
get_symbolic_size.
"""
def allocate(self, block: Allocation, is_last: bool):
is_allocated = self._allocate(block, is_last)
if is_allocated:
self.clear_cache()
return is_allocated
def clear_cache(self):
self.get_live_ranges.clear_cache(self)
self.get_size_hint.clear_cache(self)
self.get_symbolic_size.clear_cache(self)
@dataclasses.dataclass
class TemporalSplit(ClearCacheOnAllocateMixin, AllocationTreeNode):
"""
Contains a list of allocations not overlapping in LiveRanges.
Invariant: no pair (a,b) in self.allocations will have:
a.get_live_ranges().overlaps(b.get_live_ranges())
"""
allocations: List[AllocationTreeNode]
def _allocate(self, block: Allocation, is_last: bool):
slot_size = self.get_size_hint()
block_size = block.get_size_hint()
if not is_last and block_size > slot_size:
return False # doesn't fit
block_live = block.get_live_ranges()
overlapping = [
s for s in self.allocations if s.get_live_ranges().overlaps(block_live)
]
if len(overlapping) > 1:
# TODO(jansel): we could try harder here by merging overlapping in space
return False
elif len(overlapping) == 1:
return overlapping[0].allocate(block, is_last)
else:
block.mark_allocated()
if len(self.allocations) == 1 and isinstance(self.allocations[-1], Empty):
self.allocations.pop()
if slot_size == block_size:
# perfect fit
self.allocations.append(block)
elif slot_size > block_size:
self.allocations.append(
SpatialSplit.create(block, slot_size - block_size)
)
else: # grow this allocation
assert is_last
self.allocations = [
*(
SpatialSplit.create(a, block_size - slot_size)
for a in self.allocations
),
block,
]
return True
@cache_on_self
def get_live_ranges(self) -> LiveRanges:
return LiveRanges(
itertools.chain.from_iterable(
x.get_live_ranges().ranges for x in self.allocations
)
)
@cache_on_self
def get_size_hint(self) -> int:
if not self.allocations:
return 0
return max(x.get_size_hint() for x in self.allocations)
@cache_on_self
def get_symbolic_size(self) -> sympy.Expr:
if not self.allocations:
return 0 # type: ignore[return-value]
return sympy.Max(*[x.get_symbolic_size() for x in self.allocations])
def is_empty(self):
return len(self.allocations) == 1 and self.allocations[0].is_empty()
def finalize(self, pool, offset):
self.allocations = [block.finalize(pool, offset) for block in self.allocations]
self.clear_cache()
if len(self.allocations) == 1:
return self.allocations[0]
return self
@dataclasses.dataclass
class SpatialSplit(ClearCacheOnAllocateMixin, AllocationTreeNode):
"""
Contains two allocations, left and right, that do not overlap in space.
Right will be allocated immediately after left in memory.
"""
left: TemporalSplit
right: TemporalSplit
@staticmethod
def create(left, extra_space):
assert isinstance(left, AllocationTreeNode)
assert isinstance(extra_space, int) and extra_space >= 1
return SpatialSplit(TemporalSplit([left]), TemporalSplit([Empty(extra_space)]))
def _allocate(self, block: Allocation, is_last: bool):
return self.left.allocate(block, False) or self.right.allocate(block, is_last)
@cache_on_self
def get_live_ranges(self):
return LiveRanges(
itertools.chain(
self.left.get_live_ranges().ranges, self.right.get_live_ranges().ranges
)
)
@cache_on_self
def get_size_hint(self) -> int:
return _align(self.left.get_size_hint()) + self.right.get_size_hint()
@cache_on_self
def get_symbolic_size(self) -> sympy.Expr:
return align(self.left.get_symbolic_size()) + self.right.get_symbolic_size()
def finalize(self, pool, offset):
self.left = self.left.finalize(pool, offset)
self.right = self.right.finalize(
pool, offset + align(self.left.get_symbolic_size())
)
self.clear_cache()
if self.right.is_empty():
return self.left
return self
@dataclasses.dataclass
class AllocationPool:
"""
Represents a pool of allocations that will be generated by a single
call to torch.empty.
"""
device: torch.device
root: TemporalSplit
can_expand: bool = True
restrict_live_range: Optional[LiveRange] = None
name: Optional[str] = None
names_to_del: List[str] = dataclasses.field(default_factory=list)
creation_cache: Dict[str, str] = dataclasses.field(default_factory=dict)
def allocate(self, block: Allocation, is_last: bool):
if self.restrict_live_range and not self.restrict_live_range.contains(
block.live_range
):
return False
is_last = self.can_expand and is_last
if self.root.allocate(block, is_last):
return True
if is_last:
return self.allocate_at_end(block)
return False
def allocate_at_end(self, block):
block.mark_allocated()
self.root = TemporalSplit([SpatialSplit(self.root, TemporalSplit([block]))])
return True
def finalize(self, name):
assert not self.name
self.name = name
self.names_to_del.append(name)
self.root.finalize(self, 0)
def codegen_create(self, wrapper, code: IndentedBuffer):
assert self.name
nbytes = self.root.get_symbolic_size()
for block in self.root.allocations:
if isinstance(block, Allocation) and nbytes == block.get_symbolic_size():
# optimization: fuse first allocation and pool creation
node = block.node
code.writeline(
wrapper.make_allocation(
self.name,
device=self.device,
dtype=node.get_dtype(),
shape=tuple(node.get_size()),
stride=tuple(node.get_stride()),
)
)
self.creation_cache[block.codegen_alloc_from_pool(wrapper)] = self.name
return
else:
code.writeline(
wrapper.make_allocation(
self.name,
device=self.device,
dtype=torch.uint8,
shape=(nbytes,),
stride=(1,),
)
)
def codegen_destroy(self, wrapper, code: IndentedBuffer):
code.writeline(wrapper.make_free_by_names(self.names_to_del))
def __eq__(self, other):
return self is other
def __hash__(self):
return id(self)
@dataclasses.dataclass
class AllocationPools:
"""
Collection of many AllocationPool objects grouped by device.
"""
device_to_pools: Dict[torch.device, List[AllocationPool]] = dataclasses.field(
default_factory=dict
)
def get_pools(self, block):
if block.device not in self.device_to_pools:
self.device_to_pools[block.device] = []
return self.device_to_pools[block.device]
def allocate(self, block: Allocation):
pools = self.get_pools(block)
for pool in pools:
if pool.allocate(block, is_last=pool is pools[-1]):
return
# everything is full, make a new pool
pools.append(
AllocationPool(
block.device,
TemporalSplit([block]),
can_expand=config.memory_pool != "none",
)
)
block.mark_allocated()
def allocate_output(self, block: Allocation):
"""Outputs get different pools so memory gets freed properly"""
pools = self.get_pools(block)
if pools and config.memory_pool in ("outputs", "combined"):
pools[-1].allocate_at_end(block)
else:
# create a new pool
block.mark_allocated()
pools.append(
AllocationPool(
block.device,
TemporalSplit([block]),
can_expand=config.memory_pool == "combined",
)
)
def finalize(self):
"""Called at the end of allocation process"""
for i, pool in enumerate(
itertools.chain.from_iterable(self.device_to_pools.values())
):
pool.finalize(f"pool{i}")
def pprint(self):
for pool in itertools.chain.from_iterable(self.device_to_pools.values()):
print()
print(pool.name)
print(pool.root.get_live_ranges())
pprint.pprint(pool.root)
class BufferGroup:
"""
Due to inplace reuse an allocated buffer can have many names.
This tracks these collections of buffers sharing underlying memory.
"""
def __init__(self, node: BufferLike):
self.node = node
self.names = [node.get_name()]
self.is_output = False
self.allocation: Optional[Allocation] = None
self.live_range = LiveRange(float("inf"), -float("inf"))
def update_usage(self, timestep: int):
"""Expand self.live_range to include timestep"""
self.live_range = LiveRange(
min(timestep, self.live_range.begin),
max(timestep, self.live_range.end),
)
def sym_nbytes(self):
return self.node.get_layout().storage_size() * self.node.get_dtype().itemsize
def make_allocation(self):
assert not self.allocation, "multiple allocations"
assert isinstance(self.live_range.begin, int), "live ranges not computed"
nbytes = self.sym_nbytes()
# For now, fallback value will be used if we encounter an unbacked SymInt. The longer-term plan is to have
# size_hint() use better heuristics for unbackeds, at which point the fallback value will be ignored.
size_hint = V.graph.sizevars.size_hint(nbytes, fallback=64)
self.allocation = Allocation(
self.node,
self.live_range,
size_hint=size_hint,
symbolic_size=nbytes,
)
def __repr__(self):
return (
f"{self.__class__.__name__}({self.names!r}, is_output={self.is_output}, "
f"live_range={self.live_range}"
)
@dataclasses.dataclass
class PoolMemoryPlanningLine(MemoryPlanningLine):
"""Abstract base class for {Alloc,Dealloc}FromPoolLine"""
group: BufferGroup
timestep: Optional[int] = None
@property
def node(self):
return self.group.node
@dataclasses.dataclass
class AllocFromPoolLine(PoolMemoryPlanningLine):
"""Similar to AllocationLine, but takes memory from a pool"""
is_first_pool_usage: bool = False
def codegen(self, code: IndentedBuffer):
allocation = self.group.allocation
assert allocation and allocation.pool
pool = allocation.pool
name = self.node.get_name()
if self.is_first_pool_usage:
pool.codegen_create(self.wrapper, code)
pool.names_to_del.extend(self.group.names)
alloc_from_pool = allocation.codegen_alloc_from_pool(self.wrapper)
if alloc_from_pool in pool.creation_cache:
code.writeline(
self.wrapper.make_tensor_alias(
name, pool.creation_cache[alloc_from_pool], "alloc"
)
)
else:
pool.creation_cache[alloc_from_pool] = name
code.writeline(
f"{self.wrapper.declare}{name} = {alloc_from_pool}{self.wrapper.ending}"
)
@dataclasses.dataclass
class DeallocFromPoolLine(PoolMemoryPlanningLine):
"""Similar to FreeIfNotReusedLine, but takes memory from a pool"""
is_last_pool_usage: bool = False
def codegen(self, code: IndentedBuffer):
if self.is_last_pool_usage:
assert self.group.allocation and self.group.allocation.pool
self.group.allocation.pool.codegen_destroy(self.wrapper, code)
@dataclasses.dataclass
class MemoryPlanner:
"""
Coordination object to run memory planning passes during wrapper
codegen.
"""
wrapper: Any
pools: AllocationPools = dataclasses.field(default_factory=AllocationPools)
buffer_groups: Optional[List[BufferGroup]] = None
def plan(self, lines: List[Any]) -> List[Any]:
"""Call all the memory planning passes in sequence"""
lines = [*lines]
self.drop_removed_buffers(lines)
self.convert_to_pool_lines(lines)
self.compute_live_ranges(lines)
self.allocate_groups()
self.mark_first_last_usage(lines)
return lines
def drop_removed_buffers(self, lines):
"""
Replace any memory planning lines in V.graph.removed_buffers with NullLine
"""
# drop any removed buffers
for i, line in enumerate(lines):
if isinstance(line, (AllocateLine, FreeIfNotReusedLine, ReuseLine)):
if line.node.get_name() in V.graph.removed_buffers:
lines[i] = NullLine(self.wrapper)
def compute_buffer_groups(self, lines):
"""
Populates self.buffer_groups with BufferGroup objects that join
allocations with common storage (due to inplace reuse) into a
single object.
"""
name_to_group = {}
for line in lines:
if isinstance(line, AllocateLine):
name = line.node.get_name()
assert name not in name_to_group
name_to_group[name] = BufferGroup(line.node)
elif isinstance(line, ReuseLine):
old_name = line.node.get_name()
new_name = line.reused_as.get_name()
assert new_name not in name_to_group
# TODO(jansel): we should support reusing buffers created via ExternKernelAlloc
if old_name in name_to_group:
name_to_group[old_name].names.append(new_name)
name_to_group[new_name] = name_to_group[old_name]
outputs = set(V.graph.get_output_names())
unique_groups = [*{id(g): g for g in name_to_group.values()}.values()]
for group in unique_groups:
group.is_output = any(x in outputs for x in group.names)
assert self.buffer_groups is None
self.buffer_groups = unique_groups
return name_to_group
def convert_to_pool_lines(self, lines):
"""
Convert AllocateLine/FreeIfNotReusedLine/ReuseLine into their
pool-based counterparts.
"""
name_to_group = self.compute_buffer_groups(lines)
for i, line in enumerate(lines):
if isinstance(line, AllocateLine):
if line.node.get_name() in name_to_group:
lines[i] = AllocFromPoolLine(
self.wrapper, name_to_group[line.node.get_name()]
)
elif isinstance(line, FreeIfNotReusedLine):
assert not line.is_reused
if line.node.get_name() in name_to_group:
lines[i] = DeallocFromPoolLine(
self.wrapper, name_to_group[line.node.get_name()]
)
elif isinstance(line, ReuseLine):
if line.node.get_name() in name_to_group:
line.delete_old = False
def compute_live_ranges(self, lines):
"""Populate every BufferGroup.live_ranges field based on first/last usage"""
timestep = 0
worklist = collections.deque(lines)
while worklist:
if isinstance(worklist[0], MemoryPlanningLine):
timestep += 1
while worklist and isinstance(worklist[0], MemoryPlanningLine):
line = worklist.popleft()
if isinstance(line, PoolMemoryPlanningLine):
line.group.update_usage(timestep)
line.timestep = timestep
else:
worklist.popleft()
timestep += 1
assert self.buffer_groups is not None
for group in self.buffer_groups:
if group.is_output:
group.update_usage(timestep)
def allocate_groups(self):
"""
Assign every allocation to a specific location in a specific AllocationPool.
"""
assert config.memory_pool in ("none", "intermediates", "outputs", "combined")
assert self.buffer_groups is not None
for group in self.buffer_groups:
group.make_allocation()
outputs: List[Allocation] = []
intermediates: List[Allocation] = []
for group in self.buffer_groups:
assert group.allocation
if group.is_output and config.memory_pool != "combined":
outputs.append(group.allocation)
else:
intermediates.append(group.allocation)
for block in sorted(
outputs,
key=lambda x: (
x.size_hint,
-len(x.live_range),
),
):
self.pools.allocate_output(block)
for block in sorted(
intermediates,
key=lambda x: (
-x.size_hint,
-len(x.live_range),
),
):
self.pools.allocate(block)
self.pools.finalize()
def mark_first_last_usage(self, lines):
"""
Populate the AllocFromPoolLine.is_first_pool_usage and
DeallocFromPoolLine.is_last_pool_usage fields so that pools
are created/destroyed.
"""
seen = set()
for line in lines:
if isinstance(line, AllocFromPoolLine):
assert line.group.allocation
pool = line.group.allocation.pool
assert pool is not None
if pool not in seen:
line.is_first_pool_usage = True
seen.add(pool)
seen = set()
for line in reversed(lines):
if isinstance(line, DeallocFromPoolLine):
assert line.group.allocation
pool = line.group.allocation.pool
assert pool is not None
if pool not in seen:
line.is_last_pool_usage = (
pool.root.get_live_ranges().end <= line.timestep
)
seen.add(pool)
|