diff --git a/amd_kernels/warm_up.py b/amd_kernels/warm_up.py index 1e11c4c..23645f0 100644 --- a/amd_kernels/warm_up.py +++ b/amd_kernels/warm_up.py @@ -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() diff --git a/tests/test_warm_up_order.py b/tests/test_warm_up_order.py new file mode 100644 index 0000000..1144a84 --- /dev/null +++ b/tests/test_warm_up_order.py @@ -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