From 2e0009e23f0ad0fbcbd0488df893d30d5c8c2565 Mon Sep 17 00:00:00 2001 From: Quan Tran Date: Tue, 21 Apr 2026 22:27:16 -0700 Subject: [PATCH] v3 code Summary: better design between LLM and SAM3 due to the embedding issue of SAM3 Reviewed By: mchorton Differential Revision: D101818594 fbshipit-source-id: b51c36defc6ed4adae5406e8eba31280e05cac9a --- sam3/model/sam3_base_predictor.py | 3 +++ sam3/model/sam3_video_inference.py | 16 ++++++++-------- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/sam3/model/sam3_base_predictor.py b/sam3/model/sam3_base_predictor.py index a02d213..08a15b8 100644 --- a/sam3/model/sam3_base_predictor.py +++ b/sam3/model/sam3_base_predictor.py @@ -46,6 +46,7 @@ class Sam3BasePredictor: resource_path=request["resource_path"], session_id=request.get("session_id", None), offload_video_to_cpu=request.get("offload_video_to_cpu", False), + offload_state_to_cpu=request.get("offload_state_to_cpu", False), ) elif request_type == "add_prompt": return self.add_prompt( @@ -107,11 +108,13 @@ class Sam3BasePredictor: resource_path, session_id=None, offload_video_to_cpu=False, + offload_state_to_cpu=False, ): """Start a new inference session on a video directory or path.""" init_kwargs = dict( resource_path=resource_path, offload_video_to_cpu=offload_video_to_cpu, + offload_state_to_cpu=offload_state_to_cpu, ) if hasattr(self, "async_loading_frames"): init_kwargs["async_loading_frames"] = self.async_loading_frames diff --git a/sam3/model/sam3_video_inference.py b/sam3/model/sam3_video_inference.py index 18cd387..375d9ec 100644 --- a/sam3/model/sam3_video_inference.py +++ b/sam3/model/sam3_video_inference.py @@ -56,6 +56,7 @@ class Sam3VideoInference(Sam3VideoBase): self, resource_path, offload_video_to_cpu=False, + offload_state_to_cpu=False, async_loading_frames=False, video_loader_type="cv2", ): @@ -71,6 +72,7 @@ class Sam3VideoInference(Sam3VideoBase): ) inference_state = {} inference_state["image_size"] = self.image_size + inference_state["offload_state_to_cpu"] = offload_state_to_cpu inference_state["num_frames"] = len(images) # the original video height and width, used for resizing final output scores inference_state["orig_height"] = orig_height @@ -551,17 +553,14 @@ class Sam3VideoInference(Sam3VideoBase): def _build_tracker_output( self, inference_state, frame_idx, refined_obj_id_to_mask=None ): - assert ( + if ( "cached_frame_outputs" in inference_state and frame_idx in inference_state["cached_frame_outputs"] - ), ( - "No cached outputs found. Ensure normal propagation has run first to populate the cache." - ) - cached_outputs = inference_state["cached_frame_outputs"][frame_idx] + ): + obj_id_to_mask = inference_state["cached_frame_outputs"][frame_idx].copy() + else: + obj_id_to_mask = {} - obj_id_to_mask = cached_outputs.copy() - - # Update with refined masks if provided if refined_obj_id_to_mask is not None: for obj_id, refined_mask in refined_obj_id_to_mask.items(): assert refined_mask is not None, ( @@ -990,6 +989,7 @@ class Sam3VideoInferenceWithInstanceInteractivity(Sam3VideoInference): video_height=inference_state["orig_height"], video_width=inference_state["orig_width"], num_frames=inference_state["num_frames"], + offload_state_to_cpu=inference_state.get("offload_state_to_cpu", False), ) @torch.inference_mode()