From cb1ff794d3052cfc7af74254048df6a3f7443149 Mon Sep 17 00:00:00 2001 From: Minh Vu Date: Tue, 5 May 2026 22:14:01 -0700 Subject: [PATCH] Make clear_cache_threshold a per-call param on close_session MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: - Add `clear_cache_threshold: int = 80` keyword param to `Sam3BasePredictor.close_session`. Lowered the module-level `_CLEAR_CACHE_THRESHOLD` constant (renamed from `_MAX_MEMORY_PCT = 85`, used as the default-of-default) to 80 so request-omitted calls match. - `_should_empty_cache` now takes `clear_cache_threshold` as an explicit argument instead of reading the module constant — lets callers pick a tighter or looser threshold per session-close. - `handle_request` forwards `request.get("clear_cache_threshold", _CLEAR_CACHE_THRESHOLD)` to `close_session`, so RPC clients can tune the empty_cache threshold via the request payload without redeploying the predictor. Differential Revision: D103978013 fbshipit-source-id: 5f0db11b6a26b88a9be2ea4643c90c9b71338393 --- sam3/model/sam3_base_predictor.py | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/sam3/model/sam3_base_predictor.py b/sam3/model/sam3_base_predictor.py index b2435a3..2d699fe 100644 --- a/sam3/model/sam3_base_predictor.py +++ b/sam3/model/sam3_base_predictor.py @@ -26,7 +26,7 @@ logger = get_logger(__name__) # threshold. The allocator's caching pool already covers the common case # where freed blocks get reused by the next session — empty_cache is only # needed to keep memory from growing unbounded. -_MAX_MEMORY_PCT = 85 +_CLEAR_CACHE_THRESHOLD = 80 class Sam3BasePredictor: @@ -89,6 +89,9 @@ class Sam3BasePredictor: return self.close_session( session_id=request["session_id"], run_gc_collect=request.get("run_gc_collect", True), + clear_cache_threshold=int( + request.get("clear_cache_threshold", _CLEAR_CACHE_THRESHOLD) + ), ) else: raise RuntimeError(f"invalid request type: {request_type}") @@ -309,7 +312,12 @@ class Sam3BasePredictor: self.model.reset_state(inference_state) return {"is_success": True} - def close_session(self, session_id, run_gc_collect=True): + def close_session( + self, + session_id, + run_gc_collect=True, + clear_cache_threshold: int = _CLEAR_CACHE_THRESHOLD, + ): """Close a session. Idempotent. ``run_gc_collect=True`` (the default) also returns the session's @@ -320,7 +328,8 @@ 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``. + device memory pressure — see ``_should_empty_cache``. Callers can + override the threshold per-call via ``clear_cache_threshold``. """ session = self._all_inference_states.pop(session_id, None) if session is None: @@ -329,18 +338,20 @@ class Sam3BasePredictor: del session if run_gc_collect: gc.collect() - if torch.cuda.is_available() and self._should_empty_cache(): + if torch.cuda.is_available() and self._should_empty_cache( + clear_cache_threshold + ): torch.cuda.empty_cache() logger.info(f"removed session {session_id}") return {"is_success": True} - def _should_empty_cache(self) -> bool: + def _should_empty_cache(self, clear_cache_threshold: int) -> bool: """Whether close_session should call ``torch.cuda.empty_cache()``. Fires only when device memory usage is at or above - ``_MAX_MEMORY_PCT``. Below that threshold, the caching allocator's - freed blocks remain available for the next session and the CUDA - sync from empty_cache is avoided. + ``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. """ try: free, total = torch.cuda.mem_get_info() @@ -350,7 +361,7 @@ class Sam3BasePredictor: if total <= 0: return False used_pct = (1.0 - free / total) * 100 - return used_pct >= _MAX_MEMORY_PCT + return used_pct >= clear_cache_threshold def _get_session(self, session_id): session = self._all_inference_states.get(session_id, None)