From 2535841e4c492fdbe6d47d4eb2b812c5ffc7d1a8 Mon Sep 17 00:00:00 2001 From: Avi Fenesh Date: Sun, 29 Nov 2026 18:23:50 +0200 Subject: [PATCH 1/2] fix(ignition): seed warmup workspaces inside export storage pool Why: During native runtime-core export on unified-memory GPUs (observed on GB10 / DGX Spark), _seed_warmup_workspaces allocated its 2 MiB operand segment and 32 MiB cuBLASLt workspace inside a module-level torch.cuda.MemPool() outside CoreStorage ownership. After full forward compilation and graph capture, the native exporter's allocation-context check rejected the unowned segment. What exactly changed: - ignition/preparation/core_storage.py: added CoreStorage.warmup_pool(), which lazily creates one persistent torch.cuda.MemPool backed by self._allocator.allocator() and registers its pool ID in self._preparation_pools. - ignition/preparation/capture.py: added an optional pool parameter to _seed_warmup_workspaces(stream, device, pool=None). In capture_forwards, when warmup_storage provides warmup_pool(), pass that pool to _seed_warmup_workspaces; otherwise call _seed_warmup_workspaces(warm, device) unchanged so non-export callers and existing 2-arg stubs keep identical behavior. - tests/preparation/test_core_storage.py and tests/preparation/test_capture_cublas_workspace_cuda.py: added unit and CUDA tests verifying pool reuse, _preparation_pools registration, and that seeding lands inside storage.warmup_pool() without polluting transient storage. Claimed benefit: Keeps cuBLASLt warmup workspace allocations inside CoreStorage-owned mappings during export so native export validation succeeds on unified-memory devices without altering non-export capture behavior or weakening any native allocation check. Supporting evidence and limitations: Ran FLARE_RUN_CUDA_TESTS=1 pytest on GB10 (spark-b23d) across tests/preparation/test_core_storage.py, tests/preparation/test_capture_cublas_workspace_cuda.py, tests/preparation/test_capture_graph_metadata.py, tests/preparation/test_capture_rank_readiness.py, and tests/preparation/test_preparation_reclamation.py (35 passed, 1 skipped on Linux for Windows-only address check). Also validated end-to-end across multiple full 75- and 79-graph Qwen3.8 runtime-core exports on GB10. Signed-off-by: Avi Fenesh Design-Signed-Off-By: Antigravity (Gemini); design-review=2e5b --- ignition/preparation/capture.py | 17 ++++-- ignition/preparation/core_storage.py | 11 ++++ .../test_capture_cublas_workspace_cuda.py | 53 +++++++++++++++++++ tests/preparation/test_core_storage.py | 25 +++++++++ 4 files changed, 101 insertions(+), 5 deletions(-) diff --git a/ignition/preparation/capture.py b/ignition/preparation/capture.py index eb6585147..4e7318994 100644 --- a/ignition/preparation/capture.py +++ b/ignition/preparation/capture.py @@ -49,7 +49,7 @@ def _warmup_stream(device): return stream -def _seed_warmup_workspaces(stream, device): +def _seed_warmup_workspaces(stream, device, pool=None): """Seed the calling thread's warmup-stream workspace in a dedicated pool. Workspaces are cached per thread's cuBLAS handle, so seeding runs on @@ -58,13 +58,16 @@ def _seed_warmup_workspaces(stream, device): address in the cache after the transient backing is released, and a later call could reuse it; a dedicated pool keeps the seed's backing mapped for the life of the cache entry, outside transient storage, - any caller-owned pool, and the exported graph pool. + any caller-owned pool, and the exported graph pool. When an export + storage supplies a persistent warmup pool, seed inside it so the + allocation stays within declared core mappings. """ with torch.cuda.device(device): index = torch.cuda.current_device() - pool = _warmup_pools.get(index) if pool is None: - pool = _warmup_pools[index] = torch.cuda.MemPool() + pool = _warmup_pools.get(index) + if pool is None: + pool = _warmup_pools[index] = torch.cuda.MemPool() with torch.cuda.device(index), torch.cuda.stream(stream), torch.cuda.use_mem_pool(pool): operand = torch.empty((512, 512), dtype=torch.bfloat16, device=device) torch.mm(operand, operand, out=torch.empty_like(operand)) @@ -112,7 +115,11 @@ def capture_forwards(functions, signatures, *, session, device, stream, pool, graphs, compiled = {}, {} warmup = nullcontext if warmup_storage is None else warmup_storage.transient warm = _warmup_stream(device) - _seed_warmup_workspaces(warm, device) + warmup_pool = warmup_storage.warmup_pool() if hasattr(warmup_storage, "warmup_pool") else None + if warmup_pool is not None: + _seed_warmup_workspaces(warm, device, warmup_pool) + else: + _seed_warmup_workspaces(warm, device) if compile_decoder: cache = ForwardCache(namespace=session.namespace.to_dict(), device=device, root=cache_root, headroom_bytes=getattr(session, "headroom_bytes", 0), diff --git a/ignition/preparation/core_storage.py b/ignition/preparation/core_storage.py index 85d222c8a..933cf7748 100644 --- a/ignition/preparation/core_storage.py +++ b/ignition/preparation/core_storage.py @@ -297,6 +297,17 @@ def scratch_pool(self): self._preparation_pools.add(pool.pool.id) return pool + def warmup_pool(self): + """Own a persistent pool for cuBLAS warmup workspaces inside core mappings.""" + import torch + + pool = getattr(self, "_warmup_pool", None) + if pool is None: + pool = torch.cuda.MemPool(self._allocator.allocator()) + self._warmup_pool = pool + self._preparation_pools.add(pool.id) + return pool + def packed_weights(self): """Own aligned immutable weight storage until graph users have drained.""" from .packed_weights import PackedWeights diff --git a/tests/preparation/test_capture_cublas_workspace_cuda.py b/tests/preparation/test_capture_cublas_workspace_cuda.py index b92a602e9..976a58eb2 100644 --- a/tests/preparation/test_capture_cublas_workspace_cuda.py +++ b/tests/preparation/test_capture_cublas_workspace_cuda.py @@ -118,3 +118,56 @@ def capture(stream, before_run=None): graph.reset() finally: torch.backends.cuda.preferred_blas_library(previous_blas) + + +def test_export_warmup_pool_seeds_workspace_inside_storage_pool(): + from ignition.preparation.capture import _seed_warmup_workspaces, capture_forwards + from ignition.preparation.execution import GraphSignature + + torch.cuda.set_device(0) + previous_blas = torch.backends.cuda.preferred_blas_library() + torch.backends.cuda.preferred_blas_library("cublaslt") + try: + source = torch.randn((512, 512), device="cuda").bfloat16() + weight = torch.randn((512, 512), device="cuda").bfloat16() + output = torch.empty_like(source) + + def forward(): + torch.mm(source, weight, out=output) + + export_warmup = torch.cuda.MemPool() + transient = torch.cuda.MemPool() + storage = SimpleNamespace( + transient=lambda: torch.cuda.use_mem_pool(transient), + warmup_pool=lambda: export_warmup, + ) + pool = torch.cuda.graph_pool_handle() + session = SimpleNamespace(capture=nullcontext) + stream = torch.cuda.Stream() + graphs, _ = capture_forwards( + {"decode": forward}, + {"decode": GraphSignature("decode", 1, 1)}, + session=session, + device=0, + stream=stream, + pool=pool, + warmup_storage=storage, + compile_decoder=False, + ) + try: + assert not _active_pool_blocks(transient.id) + assert torch.cuda.memory_snapshot(export_warmup.id, include_traces=False), ( + "capture_forwards did not allocate warmup seed inside storage.warmup_pool()" + ) + # On a fresh stream with no prior cached cuBLASLt workspace, seeding must + # retain the persistent workspace block inside export_warmup. + fresh_stream = torch.cuda.Stream() + _seed_warmup_workspaces(fresh_stream, 0, export_warmup) + assert _active_pool_blocks(export_warmup.id), ( + "seeded cuBLASLt workspace did not land inside storage.warmup_pool()" + ) + finally: + for graph in graphs.values(): + graph.reset() + finally: + torch.backends.cuda.preferred_blas_library(previous_blas) diff --git a/tests/preparation/test_core_storage.py b/tests/preparation/test_core_storage.py index ed77d9c61..36eefc966 100644 --- a/tests/preparation/test_core_storage.py +++ b/tests/preparation/test_core_storage.py @@ -78,3 +78,28 @@ def test_windows_host_range_stays_below_stalling_wddm_addresses(): # RTX Spark's driver stalls on host-address hints at 16 TiB and 32 TiB. assert CoreStorage.DEVICE_BASE + CoreStorage.RESERVATION_BYTES <= CoreStorage.HOST_BASE assert CoreStorage.HOST_BASE + CoreStorage.HOST_RESERVATION_BYTES <= 16 << 40 + + +def test_warmup_pool_reuses_one_core_allocator_pool(monkeypatch): + from types import SimpleNamespace + + created = [] + + class FakeMemPool: + def __init__(self, allocator): + self.allocator = allocator + self.id = (0, 99 + len(created)) + created.append(self) + + sentinel_alloc = object() + storage = CoreStorage.__new__(CoreStorage) + storage._allocator = SimpleNamespace(allocator=lambda: sentinel_alloc) + storage._preparation_pools = set() + monkeypatch.setattr(torch.cuda, "MemPool", FakeMemPool) + + first = storage.warmup_pool() + second = storage.warmup_pool() + assert first is second + assert len(created) == 1 + assert first.allocator is sentinel_alloc + assert first.id in storage._preparation_pools From 8c1a28a1ce8fae1dff6169971b8892783f197812 Mon Sep 17 00:00:00 2001 From: Avi Fenesh Date: Sun, 29 Nov 2026 19:13:12 +0200 Subject: [PATCH 2/2] test(ignition): verify CoreStorage.warmup_pool in isolated subprocess and polish docs Why: Review of PR #606 identified that the initial CUDA test ran in-process alongside test_first_capture_binds_workspace_from_the_recording_pool (sharing module-level _warmup_streams[0] and cuBLASLt workspace cache state) and stubbed CoreStorage with SimpleNamespace instead of exercising real CoreStorage mappings and native RuntimeCore export. In addition, the updated _seed_warmup_workspaces docstring needed reconciliation with #331, self._warmup_pool was not declared in CoreStorage.__init__, and docs/preparation-development.md did not mention CoreStorage.warmup_pool(). What exactly changed: - tests/preparation/test_capture_cublas_workspace_cuda.py: replaced the in-process stub test with test_export_warmup_pool_seeds_workspace_inside_core_storage, which runs in a fresh subprocess (matching test_recorded_export_graph_cuda.py) against a real CoreStorage and ModuleImages instance, verifies _warmup_pools remains empty, asserts warmup segments lie inside CoreStorage.mappings, verifies the captured graph binds its workspace in storage.graph_pool, and writes a native RuntimeCore package. - ignition/preparation/capture.py: reconciled the _seed_warmup_workspaces docstring for both pool=None and CoreStorage.warmup_pool() paths and added a short comment explaining the 2-argument call preservation. - ignition/preparation/core_storage.py: declared self._warmup_pool = None in CoreStorage.__init__. - docs/preparation-development.md: documented CoreStorage.warmup_pool() beside transient warmup pool usage. Claimed benefit: Eliminates cross-test order dependence on _warmup_streams[0], verifies real CoreStorage mappings and native export acceptance end-to-end on CUDA, and keeps docstrings, initialization fields, and preparation docs consistent. Supporting evidence and limitations: Ran FLARE_RUN_CUDA_TESTS=1 pytest on GB10 (spark-b23d) across tests/preparation/test_core_storage.py, tests/preparation/test_capture_cublas_workspace_cuda.py, tests/preparation/test_recorded_export_graph_cuda.py, tests/preparation/test_capture_graph_metadata.py, tests/preparation/test_capture_rank_readiness.py, and tests/preparation/test_preparation_reclamation.py (36 passed, 1 skipped on Linux for Windows-only address check), plus an explicit reversed-order run of test_capture_cublas_workspace_cuda.py (2 passed). Single-GPU GB10 run only. Signed-off-by: Avi Fenesh Design-Signed-Off-By: Antigravity (Gemini); design-review=2e5b --- docs/preparation-development.md | 5 +- ignition/preparation/capture.py | 12 +- ignition/preparation/core_storage.py | 1 + .../test_capture_cublas_workspace_cuda.py | 179 +++++++++++++----- 4 files changed, 142 insertions(+), 55 deletions(-) diff --git a/docs/preparation-development.md b/docs/preparation-development.md index e877cd20a..0da40a7bd 100644 --- a/docs/preparation-development.md +++ b/docs/preparation-development.md @@ -88,7 +88,10 @@ before the session's capture guard forbids further kernel resolution. When `capture_forwards` receives `warmup_storage`, the preparation's `CoreStorage` (`ignition/preparation/core_storage.py`), eager warmups allocate from its transient pools and release them before capture, so - their workspaces do not coexist with retained graph intermediates. Ranks + their workspaces do not coexist with retained graph intermediates, while + cuBLAS warmup workspaces are seeded into `CoreStorage.warmup_pool()` so they + stay outside transient and graph pools while remaining inside declared core + mappings. Ranks synchronize before each warmup and each capture. Retain compiled kernel owners until native replay has drained. Compiled forwards retain Inductor memory-pool planning, storage reuse, and default peak-memory operation reordering; callers diff --git a/ignition/preparation/capture.py b/ignition/preparation/capture.py index 4e7318994..ff9aca6e9 100644 --- a/ignition/preparation/capture.py +++ b/ignition/preparation/capture.py @@ -56,11 +56,12 @@ def _seed_warmup_workspaces(stream, device, pool=None): each capture call under the ambient BLAS backend, the backend the warmups use. A workspace created inside transient storage keeps its address in the cache after the transient backing is released, and a - later call could reuse it; a dedicated pool keeps the seed's backing - mapped for the life of the cache entry, outside transient storage, - any caller-owned pool, and the exported graph pool. When an export - storage supplies a persistent warmup pool, seed inside it so the - allocation stays within declared core mappings. + later call could reuse it. When ``pool`` is omitted, seeding uses a + module-level pool outside transient storage, caller-owned pools, and + the exported graph pool. When an export storage supplies + ``warmup_pool()``, seeding uses that persistent pool instead so the + backing stays mapped outside transient storage, ``storage.pool``, and + ``storage.graph_pool`` while remaining inside declared core mappings. """ with torch.cuda.device(device): index = torch.cuda.current_device() @@ -119,6 +120,7 @@ def capture_forwards(functions, signatures, *, session, device, stream, pool, if warmup_pool is not None: _seed_warmup_workspaces(warm, device, warmup_pool) else: + # Keep the 2-argument call for non-export callers and existing test stubs. _seed_warmup_workspaces(warm, device) if compile_decoder: cache = ForwardCache(namespace=session.namespace.to_dict(), device=device, root=cache_root, diff --git a/ignition/preparation/core_storage.py b/ignition/preparation/core_storage.py index 933cf7748..43ff890ae 100644 --- a/ignition/preparation/core_storage.py +++ b/ignition/preparation/core_storage.py @@ -71,6 +71,7 @@ def __init__(self, *, rank=0, world=1, device=0, local_ranks=None, device_window self._persistent_total_bytes = min(properties.total_memory, self.RESERVATION_BYTES // 4) self._retention_scope = False self._retention_pool = None + self._warmup_pool = None window = (self.DEVICE_BASE, self.RESERVATION_BYTES) self.DEVICE_BASE += rank * self.RESERVATION_BYTES self.cuda = _Cuda() diff --git a/tests/preparation/test_capture_cublas_workspace_cuda.py b/tests/preparation/test_capture_cublas_workspace_cuda.py index 976a58eb2..1be31db4b 100644 --- a/tests/preparation/test_capture_cublas_workspace_cuda.py +++ b/tests/preparation/test_capture_cublas_workspace_cuda.py @@ -120,54 +120,135 @@ def capture(stream, before_run=None): torch.backends.cuda.preferred_blas_library(previous_blas) -def test_export_warmup_pool_seeds_workspace_inside_storage_pool(): - from ignition.preparation.capture import _seed_warmup_workspaces, capture_forwards - from ignition.preparation.execution import GraphSignature - - torch.cuda.set_device(0) - previous_blas = torch.backends.cuda.preferred_blas_library() - torch.backends.cuda.preferred_blas_library("cublaslt") - try: - source = torch.randn((512, 512), device="cuda").bfloat16() - weight = torch.randn((512, 512), device="cuda").bfloat16() - output = torch.empty_like(source) - - def forward(): - torch.mm(source, weight, out=output) - - export_warmup = torch.cuda.MemPool() - transient = torch.cuda.MemPool() - storage = SimpleNamespace( - transient=lambda: torch.cuda.use_mem_pool(transient), - warmup_pool=lambda: export_warmup, - ) - pool = torch.cuda.graph_pool_handle() - session = SimpleNamespace(capture=nullcontext) - stream = torch.cuda.Stream() - graphs, _ = capture_forwards( - {"decode": forward}, - {"decode": GraphSignature("decode", 1, 1)}, - session=session, - device=0, - stream=stream, - pool=pool, - warmup_storage=storage, - compile_decoder=False, - ) +def test_export_warmup_pool_seeds_workspace_inside_core_storage(tmp_path): + """Run export warmup seeding in an isolated subprocess against real CoreStorage. + + Subprocess isolation avoids both process-singleton CoreStorage arena reuse + and cross-test sharing of capture._warmup_streams[0] / cuBLASLt cache state. + """ + from pathlib import Path + import subprocess + import sys + + subprocess.run( + [ + sys.executable, + "-c", + "from pathlib import Path; import sys; " + "from tests.preparation.test_capture_cublas_workspace_cuda import check_export_warmup_pool; " + "check_export_warmup_pool(Path(sys.argv[1]))", + str(tmp_path), + ], + check=True, + timeout=120, + cwd=Path(__file__).resolve().parents[2], + ) + + +def check_export_warmup_pool(tmp_path): + from ignition.preparation import capture as _capture + from ignition.preparation.capture import capture_forwards + from ignition.preparation.core_storage import CoreStorage + from ignition.preparation.execution import GraphSignature, PreparedExecution + from ignition.preparation.module_images import ModuleImages + from ignition.preparation.runtime_core import Allocation, RuntimeCore + + with ModuleImages() as images: + torch.cuda.set_device(0) + previous_blas = torch.backends.cuda.preferred_blas_library() + torch.backends.cuda.preferred_blas_library("cublaslt") try: - assert not _active_pool_blocks(transient.id) - assert torch.cuda.memory_snapshot(export_warmup.id, include_traces=False), ( - "capture_forwards did not allocate warmup seed inside storage.warmup_pool()" - ) - # On a fresh stream with no prior cached cuBLASLt workspace, seeding must - # retain the persistent workspace block inside export_warmup. - fresh_stream = torch.cuda.Stream() - _seed_warmup_workspaces(fresh_stream, 0, export_warmup) - assert _active_pool_blocks(export_warmup.id), ( - "seeded cuBLASLt workspace did not land inside storage.warmup_pool()" - ) + storage = CoreStorage() + with storage: + source = torch.randn((512, 512), device="cuda", dtype=torch.bfloat16) + weight = torch.randn((512, 512), device="cuda", dtype=torch.bfloat16) + output = torch.empty_like(source) + frame = torch.zeros(1, device="cuda", dtype=torch.int32) + sequence = torch.zeros(1, device="cuda", dtype=torch.int64) + records = torch.zeros((2, 1, 24), device="cuda", dtype=torch.int64) + + def forward(): + torch.mm(source, weight, out=output) + + stream = torch.cuda.Stream() + graphs, _ = capture_forwards( + {"decode": forward}, + {"decode": GraphSignature("decode", 1, 1)}, + session=SimpleNamespace(capture=nullcontext), + device=0, + stream=stream, + pool=storage.graph_pool.id, + warmup_storage=storage, + retain_graphs=True, + compile_decoder=False, + ) + try: + # Export seeding must use CoreStorage.warmup_pool(), not the unmanaged module pool. + assert not _capture._warmup_pools + warmup_pool = storage.warmup_pool() + assert warmup_pool.id in storage._preparation_pools + assert _active_pool_blocks(warmup_pool.id), ( + "seeded cuBLASLt workspace did not land inside CoreStorage.warmup_pool()" + ) + warmup_segments = torch.cuda.memory_snapshot( + warmup_pool.id, include_traces=False + ) + assert warmup_segments + mappings = list(storage.mappings.values()) + for segment in warmup_segments: + addr = segment["address"] + size = segment["total_size"] + assert any( + m.address <= addr and addr + size <= m.address + m.nbytes + for m in mappings + ), f"warmup segment {addr:#x}+{size} fell outside CoreStorage.mappings" + assert _active_pool_blocks(storage.graph_pool.id), ( + "captured graph did not bind cuBLASLt workspace in storage.graph_pool" + ) + buffers = dict( + source=source, + weight=weight, + output=output, + frame_index=frame, + submission_sequence=sequence, + completion_records=records, + ) + bundle = PreparedExecution( + device=0, + graphs=graphs, + buffers=buffers, + max_active=1, + signatures={"decode": GraphSignature("decode", 1, 1, ("source",))}, + ) + allocations = [ + Allocation(name, tensor.data_ptr(), tensor.nbytes) + for name, tensor in buffers.items() + ] + allocations.extend( + Allocation( + f"graph-pool-{index}", + segment["address"], + segment["total_size"], + ) + for index, segment in enumerate( + storage.graph_pool.snapshot(include_traces=False) + ) + ) + package = RuntimeCore( + allocations=allocations, + modules={}, + kernels=(), + recorder=images.native, + owners=(bundle, storage), + ) + package.write( + tmp_path / "rank.json", + bundle.graphs, + execution=bundle.export_manifest(), + sources=True, + ) + finally: + for graph in graphs.values(): + graph.reset() finally: - for graph in graphs.values(): - graph.reset() - finally: - torch.backends.cuda.preferred_blas_library(previous_blas) + torch.backends.cuda.preferred_blas_library(previous_blas)