Make clear_cache_threshold a per-call param on close_session
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
This commit is contained in:
committed by
meta-codesync[bot]
parent
847e1a3b15
commit
cb1ff794d3
@@ -26,7 +26,7 @@ logger = get_logger(__name__)
|
|||||||
# threshold. The allocator's caching pool already covers the common case
|
# threshold. The allocator's caching pool already covers the common case
|
||||||
# where freed blocks get reused by the next session — empty_cache is only
|
# where freed blocks get reused by the next session — empty_cache is only
|
||||||
# needed to keep memory from growing unbounded.
|
# needed to keep memory from growing unbounded.
|
||||||
_MAX_MEMORY_PCT = 85
|
_CLEAR_CACHE_THRESHOLD = 80
|
||||||
|
|
||||||
|
|
||||||
class Sam3BasePredictor:
|
class Sam3BasePredictor:
|
||||||
@@ -89,6 +89,9 @@ class Sam3BasePredictor:
|
|||||||
return self.close_session(
|
return self.close_session(
|
||||||
session_id=request["session_id"],
|
session_id=request["session_id"],
|
||||||
run_gc_collect=request.get("run_gc_collect", True),
|
run_gc_collect=request.get("run_gc_collect", True),
|
||||||
|
clear_cache_threshold=int(
|
||||||
|
request.get("clear_cache_threshold", _CLEAR_CACHE_THRESHOLD)
|
||||||
|
),
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
raise RuntimeError(f"invalid request type: {request_type}")
|
raise RuntimeError(f"invalid request type: {request_type}")
|
||||||
@@ -309,7 +312,12 @@ class Sam3BasePredictor:
|
|||||||
self.model.reset_state(inference_state)
|
self.model.reset_state(inference_state)
|
||||||
return {"is_success": True}
|
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.
|
"""Close a session. Idempotent.
|
||||||
|
|
||||||
``run_gc_collect=True`` (the default) also returns the session's
|
``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.
|
long-running workloads even though the Python-level objects are gone.
|
||||||
|
|
||||||
``empty_cache()`` itself triggers a CUDA sync, so it is gated on
|
``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)
|
session = self._all_inference_states.pop(session_id, None)
|
||||||
if session is None:
|
if session is None:
|
||||||
@@ -329,18 +338,20 @@ class Sam3BasePredictor:
|
|||||||
del session
|
del session
|
||||||
if run_gc_collect:
|
if run_gc_collect:
|
||||||
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()
|
torch.cuda.empty_cache()
|
||||||
logger.info(f"removed session {session_id}")
|
logger.info(f"removed session {session_id}")
|
||||||
return {"is_success": True}
|
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()``.
|
"""Whether close_session should call ``torch.cuda.empty_cache()``.
|
||||||
|
|
||||||
Fires only when device memory usage is at or above
|
Fires only when device memory usage is at or above
|
||||||
``_MAX_MEMORY_PCT``. Below that threshold, the caching allocator's
|
``clear_cache_threshold``. Below that threshold, the caching
|
||||||
freed blocks remain available for the next session and the CUDA
|
allocator's freed blocks remain available for the next session
|
||||||
sync from empty_cache is avoided.
|
and the CUDA sync from empty_cache is avoided.
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
free, total = torch.cuda.mem_get_info()
|
free, total = torch.cuda.mem_get_info()
|
||||||
@@ -350,7 +361,7 @@ class Sam3BasePredictor:
|
|||||||
if total <= 0:
|
if total <= 0:
|
||||||
return False
|
return False
|
||||||
used_pct = (1.0 - free / total) * 100
|
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):
|
def _get_session(self, session_id):
|
||||||
session = self._all_inference_states.get(session_id, None)
|
session = self._all_inference_states.get(session_id, None)
|
||||||
|
|||||||
Reference in New Issue
Block a user