From 653a9609b9cb153373f182914ba126668b642008 Mon Sep 17 00:00:00 2001 From: Minh Vu Date: Sun, 10 May 2026 19:12:26 -0700 Subject: [PATCH] Return gpu_mem snapshot in close_session response MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: Enriches the existing ``Sam3BasePredictor.close_session`` response with a ``gpu_mem`` dict so clients can decide whether the device has headroom for their next session — no separate admission RPC needed. Every session-close naturally exposes the post-cleanup HBM state (snapshot taken AFTER any ``empty_cache`` call), which is exactly what the next session will face. Why piggyback on close_session: clients already round-trip to the server at session boundaries; adding a separate admission-check endpoint would double the RPC traffic and require new dispatch logic on both sides. Folding the snapshot into close_session means the client gets a fresh GPU state read every time it finishes a session, free, with no new endpoint. Wire shape (additive to the existing response, backward-compatible at the JSON layer): ``` close_session response: { "is_success": True, "gpu_mem": { "free_bytes": int, # raw torch.cuda.mem_get_info() "total_bytes": int, # raw torch.cuda.mem_get_info() "allocated_bytes": int, # torch.cuda.memory_allocated() — live tensor footprint "reserved_bytes": int, # torch.cuda.memory_reserved() — caching-allocator pool "free_pct": float, # convenience: free_bytes / total_bytes * 100 "active_session_count": int, # sessions still resident after this close } } ``` Old callers that only read ``is_success`` work unchanged — extra dict keys are ignored at the JSON layer. How clients will use it (companion diff): each worker stores the snapshot from its most recent ``close_session`` response in a small per-process structure (e.g., a ``_HbmGate`` with an ``asyncio.Lock``). Before issuing the next ``start_session``, the worker checks the snapshot's ``free_pct``; if it's below a threshold, sleep briefly and re-check, bounded by a max wait. The throttle gate naturally happens at session boundaries instead of via a separate admission RPC. Fail-open semantics: any error reading the device returns zero stats so a broken CUDA context (e.g., CPU-only test env) NEVER breaks the session-close response. ``memory_allocated`` and ``memory_reserved`` are cheap host-side bookkeeping reads (no CUDA sync) and complement ``mem_get_info`` by exposing the caching-allocator pool size vs the live tensor footprint. Reference: T262964602 (SAM3 VH daily DataSwarm pipeline). Differential Revision: D104592033 fbshipit-source-id: 495cb1c28ac634c882be2fd468eff7d1a75a8801 --- sam3/model/sam3_base_predictor.py | 98 ++++++++++++++++++++++++++----- 1 file changed, 82 insertions(+), 16 deletions(-) diff --git a/sam3/model/sam3_base_predictor.py b/sam3/model/sam3_base_predictor.py index 2d699fe..8d83896 100644 --- a/sam3/model/sam3_base_predictor.py +++ b/sam3/model/sam3_base_predictor.py @@ -328,40 +328,106 @@ class Sam3BasePredictor: long-running workloads even though the Python-level objects are gone. ``empty_cache()`` itself triggers a CUDA sync, so it is gated on - device memory pressure — see ``_should_empty_cache``. Callers can + device memory pressure via the ``gpu_mem`` snapshot. Callers can override the threshold per-call via ``clear_cache_threshold``. + + When ``run_gc_collect=True``, the response includes a ``gpu_mem`` + snapshot (free / total / allocated / reserved bytes, plus active + session count) so clients can decide whether the device has + headroom for their next session — no separate admission RPC + needed. See ``_gpu_mem_snapshot`` for the field shape. The + first snapshot (after ``gc.collect()``) drives the cache-eviction + decision; if ``empty_cache()`` fires, a second snapshot is taken + so the response reflects the post-cleanup state and the freed + bytes are logged. Old callers that only read ``is_success`` work + unchanged — additional dict keys are ignored at the JSON layer. """ session = self._all_inference_states.pop(session_id, None) + result = {"is_success": True} if session is None: logger.warning(f"cannot close session {session_id} as it does not exist") else: del session if run_gc_collect: gc.collect() - if torch.cuda.is_available() and self._should_empty_cache( - clear_cache_threshold + gpu_mem = self._gpu_mem_snapshot() + if ( + torch.cuda.is_available() + and gpu_mem["total_bytes"] > 0 + and (100.0 - gpu_mem["free_pct"]) >= clear_cache_threshold ): torch.cuda.empty_cache() + post_gpu_mem = self._gpu_mem_snapshot() + logger.info( + f"empty_cache freed " + f"{post_gpu_mem['free_bytes'] - gpu_mem['free_bytes']} bytes " + f"(free_pct {gpu_mem['free_pct']:.1f}% -> " + f"{post_gpu_mem['free_pct']:.1f}%, reserved " + f"{gpu_mem['reserved_bytes']} -> " + f"{post_gpu_mem['reserved_bytes']} bytes)" + ) + gpu_mem = post_gpu_mem + result["gpu_mem"] = gpu_mem logger.info(f"removed session {session_id}") - return {"is_success": True} + return result - def _should_empty_cache(self, clear_cache_threshold: int) -> bool: - """Whether close_session should call ``torch.cuda.empty_cache()``. + def _gpu_mem_snapshot(self) -> dict: + """Snapshot of current GPU memory state for inclusion in + session-close responses. - Fires only when device memory usage is at or above - ``clear_cache_threshold``. Below that threshold, the caching - allocator's freed blocks remain available for the next session - and the CUDA sync from empty_cache is avoided. + Lets clients track free HBM across the fleet without a separate + admission RPC: every ``close_session`` naturally exposes the + post-cleanup state (taken AFTER any ``empty_cache`` call), which + is exactly what the next session will face. + + Fields: + - ``free_bytes`` / ``total_bytes`` — raw + ``torch.cuda.mem_get_info()``. + - ``allocated_bytes`` — ``torch.cuda.memory_allocated()``, + live tensor footprint (no caching pool overhead). + - ``reserved_bytes`` — ``torch.cuda.memory_reserved()``, + caching-allocator pool size. + - ``free_pct`` — ``free_bytes / total_bytes * 100`` for + convenient % thresholds. + - ``active_session_count`` — sessions still resident on this + predictor instance after the close. + + Fail-open: any error reading the device returns zero stats so + a broken CUDA context (e.g., CPU-only test env) NEVER breaks + the session-close response. """ + active_count = len(self._all_inference_states) try: - free, total = torch.cuda.mem_get_info() + free_bytes, total_bytes = torch.cuda.mem_get_info() except RuntimeError: # No active CUDA context (e.g., CPU-only test env). - return False - if total <= 0: - return False - used_pct = (1.0 - free / total) * 100 - return used_pct >= clear_cache_threshold + return { + "free_bytes": 0, + "total_bytes": 0, + "allocated_bytes": 0, + "reserved_bytes": 0, + "free_pct": 0.0, + "active_session_count": active_count, + } + free_pct = (free_bytes / total_bytes) * 100 if total_bytes > 0 else 0.0 + # ``memory_allocated`` / ``memory_reserved`` are cheap host-side + # bookkeeping reads (no CUDA sync) and complement + # ``mem_get_info`` by exposing the caching-allocator pool size + # vs the actual live tensor footprint. + allocated_bytes = ( + torch.cuda.memory_allocated() if torch.cuda.is_available() else 0 + ) + reserved_bytes = ( + torch.cuda.memory_reserved() if torch.cuda.is_available() else 0 + ) + return { + "free_bytes": free_bytes, + "total_bytes": total_bytes, + "allocated_bytes": allocated_bytes, + "reserved_bytes": reserved_bytes, + "free_pct": free_pct, + "active_session_count": active_count, + } def _get_session(self, session_id): session = self._all_inference_states.get(session_id, None)