Drop per-session dict refs in close_session to fix monotonic GPU climb
Summary:
SAM3 IPNext replicas serving long-running session workloads exhibit a
monotonic climb in PyTorch's *allocated* GPU memory bucket (not the
cache bucket), to the point of OOM-ing single-call transients at
concurrency=1. Two recent client observations from the
video_product_highlight pipeline:
jiid 37154706936429064 (2026-05-09): 4 SAM3 OOMs in the first 41
seconds, AIMD shrunk concurrency 4→1, run failed at lifespan with
3,332 stranded checkpoints and 0 final detection rows.
jiid 41658306563594281 (2026-05-10): same fingerprint within 90s of
startup; 474 OOM-retry events over 8h, run averaged 3 ads/min
(vs ~7.5 ads/min on a fresh replica).
The OOM messages from these runs all show the same memory state:
this process has 94.99 GiB memory in use.
93.54 GiB allocated by PyTorch.
577.70 MiB reserved by PyTorch but unallocated.
i.e., ``empty_cache`` *was* firing (reserved-but-unallocated is tiny),
but the *allocated* bucket — backed by live tensor objects — is sitting
at ~93 GiB. That's the signature of dict-keyed references staying
alive: every closed session's tensors are still in the allocated pool
because the dict that keys them is being held by something the close
path doesn't reach.
D103081459 (2026-04-29) added ``empty_cache`` after ``gc.collect()``,
which fixed the *cache*-side leak. D103602738 (2026-05-03) gated it on
device memory pressure. Neither addressed the allocated-side leak
because they relied on ``del session`` triggering refcount-zero on the
inference_state dict — which, in practice, doesn't happen reliably
across long-lived workers (closure capture, asyncio task references,
metrics buffers, or simple cycles can keep the wrapper dict alive past
the local ``del``).
Fix: explicitly call ``.clear()`` on both ``session["state"]`` and
``session`` before ``del session``. ``.clear()`` immediately drops
every dict-keyed reference, releasing the per-session tensors
regardless of whether the wrapper dict itself survives. ``init_state``
populates ``state`` with these heavy GPU-resident keys:
- ``input_batch`` (BatchedDatapoint with ``img_batch`` = video frames
on device, plus per-frame find_inputs)
- ``constants["empty_geometric_prompt"]`` (zeroed device tensors)
- ``feature_cache`` (per-frame feature maps, populated during
propagation)
- ``cached_frame_outputs`` (per-frame outputs, populated during
propagation)
- ``tracker_inference_states``, ``tracker_metadata`` (per-track
state, populated during add_prompt + propagation)
A single ``state.clear()`` covers all of them and any future heavy
keys, so the fix doesn't need to enumerate them. Lists in the dict
hold ``None``s for per-frame slots and don't need separate handling.
___
overriding_review_checks_triggers_an_audit_and_retroactive_review
Oncall Short Name: creative_sticker_model
Differential Revision: D104607919
fbshipit-source-id: 6022802f07f51ee72c77ad008acdf94afe763433
This commit is contained in:
committed by
meta-codesync[bot]
parent
653a9609b9
commit
2530a8a7af
@@ -347,6 +347,43 @@ class Sam3BasePredictor:
|
|||||||
if session is None:
|
if session is None:
|
||||||
logger.warning(f"cannot close session {session_id} as it does not exist")
|
logger.warning(f"cannot close session {session_id} as it does not exist")
|
||||||
else:
|
else:
|
||||||
|
# Explicitly clear the per-session dicts BEFORE ``del session``.
|
||||||
|
#
|
||||||
|
# ``inference_state`` (i.e., ``session["state"]``) is the dict
|
||||||
|
# built by ``init_state`` / ``_construct_initial_input_batch``
|
||||||
|
# and grown by ``add_prompt`` / propagation. It holds heavy
|
||||||
|
# GPU-resident references — ``input_batch`` (the video frames
|
||||||
|
# as a ``BatchedDatapoint`` on device),
|
||||||
|
# ``constants["empty_geometric_prompt"]`` (zeroed device
|
||||||
|
# tensors), and the per-inference accumulators
|
||||||
|
# ``feature_cache``, ``cached_frame_outputs``,
|
||||||
|
# ``tracker_inference_states``, ``tracker_metadata``.
|
||||||
|
#
|
||||||
|
# Empirically, relying on ``del session`` + ``gc.collect()``
|
||||||
|
# alone has been insufficient in prod: across long-lived
|
||||||
|
# IPNext replicas serving thousands of sessions, PyTorch's
|
||||||
|
# *allocated* bucket monotonically climbs to ~93 GiB even
|
||||||
|
# while ``empty_cache`` keeps *reserved-but-unallocated* at
|
||||||
|
# ~600 MiB, leading to OOMs at concurrency=1 (SAM3 client
|
||||||
|
# observation 2026-05-09 / 2026-05-10, jiids
|
||||||
|
# 37154706936429064 and 41658306563594281). The fingerprint:
|
||||||
|
#
|
||||||
|
# this process has 94.99 GiB memory in use.
|
||||||
|
# 93.54 GiB allocated by PyTorch.
|
||||||
|
# 577.70 MiB reserved by PyTorch but unallocated.
|
||||||
|
#
|
||||||
|
# which is the signature of dict-keyed references staying
|
||||||
|
# alive (allocated bucket, not the cache). Calling ``clear()``
|
||||||
|
# on the nested dict immediately drops all per-session tensor
|
||||||
|
# refs the dict was keying, regardless of whether something
|
||||||
|
# else (closure, asyncio task, metrics buffer, parent
|
||||||
|
# container) is still holding the wrapper dict alive via a
|
||||||
|
# cycle. Lists in the inference state hold ``None``s for
|
||||||
|
# per-frame slots, so they don't need separate handling.
|
||||||
|
state = session.get("state")
|
||||||
|
if isinstance(state, dict):
|
||||||
|
state.clear()
|
||||||
|
session.clear()
|
||||||
del session
|
del session
|
||||||
if run_gc_collect:
|
if run_gc_collect:
|
||||||
gc.collect()
|
gc.collect()
|
||||||
|
|||||||
Reference in New Issue
Block a user