Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion amd_kernels/warm_up.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,8 @@ def _warm(spec: Entry, key, args, kwargs) -> None:
return
token = _in_warm_up.set(True)
t0 = time.perf_counter()
sizes = list(spec.sizes(key[1]))
# largest first: each size's torch buffers then fit the blocks the first size left in the caching allocator
sizes = sorted(spec.sizes(key[1]), reverse=True)
stats = {}
# a process already in compile-only mode keeps no handles: they would launch after the warm-up
load = not _flydsl_compile_only()
Expand Down
41 changes: 41 additions & 0 deletions tests/test_warm_up_order.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the amd-kernels project
"""A warm-up's torch buffers come from one cached block, not one per size."""

import pytest
import torch

from amd_kernels import warm_up

N = 8192
SIZES = list(range(256, 16384 + 1, 256))


def _fn(x):
return torch.empty(x.shape[0], N, dtype=x.dtype, device=x.device)


def test_warm_up_reuses_its_largest_buffer():
if not torch.cuda.is_available():
pytest.skip("needs a GPU")
name = "tests/warm_up_order"
fn = warm_up.entry(
name, mode=lambda x: (), sizes=lambda mode: SIZES,
inputs=lambda mode, n, x: ((torch.empty(n, 64, dtype=x.dtype, device=x.device),), {}),
)(_fn) # fmt: skip
warm_up.set_auto(True)
warm_up.set_workers(1)
try:
x = torch.empty(1, 64, dtype=torch.bfloat16, device="cuda")
torch.cuda.synchronize()
torch.cuda.empty_cache()
base = torch.cuda.memory_reserved()
torch.cuda.reset_peak_memory_stats()
fn(x)
grown = torch.cuda.max_memory_reserved() - base
finally:
warm_up.set_auto(False)
warm_up.set_workers(None)
warm_up.REGISTRY.pop(name, None)
# smallest first, the cache would hold every size: 32x the largest here
assert grown < 2 * max(SIZES) * N * 2
Loading