Spaces:
Paused
Paused
Zhen Ye Claude Opus 4.6 commited on
Commit Β·
64f68de
1
Parent(s): fc9835a
perf: eliminate CUDA sync points in SAM2 video propagation hot-path
Browse filesReplace per-object-per-frame torch.nonzero + .item() calls (7 CUDA
syncs each) with batched GPU-native argmax reductions (2 syncs per
segment). Move deepcopy from per-frame to once per segment on last
frame only. Reduces total CUDA sync points from ~28,700 to ~82 per
pipeline run.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
37 unmodified lines383940414243444546474849505152535455406 unmodified lines462463464465466467468469470471472473474475476477478479480481482483188 unmodified lines67267367467567667767867968068168268368468568668768868969069169269369469569669769869937 unmodified linesy2: int = 0def update_box(self):"""Derive bounding box from mask."""if self.mask is None:returnnonzero = torch.nonzero(self.mask)if nonzero.size(0) == 0:returny_min, x_min = torch.min(nonzero, dim=0)[0]y_max, x_max = torch.max(nonzero, dim=0)[0]self.x1 = x_min.item()self.y1 = y_min.item()self.x2 = x_max.item()self.y2 = y_max.item()@dataclass406 unmodified lines)segment_results: Dict[int, Dict[int, ObjectInfo]] = {}for out_frame_idx, out_obj_ids, out_mask_logits in self._video_predictor.propagate_in_video(inference_state,max_frame_num_to_track=step,start_frame_idx=start_idx,):frame_objects: Dict[int, ObjectInfo] = {}for i, out_obj_id in enumerate(out_obj_ids):out_mask = (out_mask_logits[i] > 0.0)info = ObjectInfo(instance_id=out_obj_id,mask=out_mask[0],class_name=mask_dict.get_target_class_name(out_obj_id),)info.update_box()frame_objects[out_obj_id] = infosegment_results[out_frame_idx] = frame_objectsif _pm is not None:_pl = getattr(self, '_perf_lock', None)188 unmodified linesobj_info.mask,)for out_frame_idx, out_obj_ids, out_mask_logits in self._video_predictor.propagate_in_video(inference_state,max_frame_num_to_track=step,start_frame_idx=start_idx,):frame_objects: Dict[int, ObjectInfo] = {}for i, out_obj_id in enumerate(out_obj_ids):out_mask = (out_mask_logits[i] > 0.0)info = ObjectInfo(instance_id=out_obj_id,mask=out_mask[0],class_name=mask_dict.get_target_class_name(out_obj_id),)info.update_box()frame_objects[out_obj_id] = infoall_results[out_frame_idx] = frame_objects# Keep latest frame masks for next segment's IoU matchingsam2_masks = MaskDictionary()sam2_masks.labels = copy.deepcopy(frame_objects)if frame_objects:first_info = next(iter(frame_objects.values()))if first_info.mask is not None:sam2_masks.mask_height = first_info.mask.shape[-2] if first_info.mask.ndim >= 2 else 0sam2_masks.mask_width = first_info.mask.shape[-1] if first_info.mask.ndim >= 2 else 037 unmodified lines3839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990406 unmodified lines497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542188 unmodified lines73173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278337 unmodified linesy2: int = 0def update_box(self):"""Derive bounding box from mask (GPU-native, minimal sync)."""if self.mask is None:returnmask = self.maskif not torch.is_tensor(mask):mask = torch.as_tensor(mask)rows = mask.any(dim=1) # (H,) β which rows have any Truecols = mask.any(dim=0) # (W,) β which cols have any Trueif not rows.any():returnrows_f = rows.float()cols_f = cols.float()H, W = mask.shape[-2], mask.shape[-1]bbox = torch.stack([cols_f.argmax(),rows_f.argmax(),W - 1 - cols_f.flip(0).argmax(),H - 1 - rows_f.flip(0).argmax(),])x1, y1, x2, y2 = bbox.tolist()self.x1 = int(x1)self.y1 = int(y1)self.x2 = int(x2)self.y2 = int(y2)@staticmethoddef batch_bbox(masks: torch.Tensor):"""Compute bboxes for (N, H, W) bool masks. Returns (N,4) cpu int, (N,) cpu bool."""N, H, W = masks.shaperows = masks.any(dim=2) # (N, H)cols = masks.any(dim=1) # (N, W)valid = rows.any(dim=1) # (N,)rows_f = rows.float()cols_f = cols.float()y_mins = rows_f.argmax(dim=1)y_maxs = H - 1 - rows_f.flip(1).argmax(dim=1)x_mins = cols_f.argmax(dim=1)x_maxs = W - 1 - cols_f.flip(1).argmax(dim=1)bboxes = torch.stack([x_mins, y_mins, x_maxs, y_maxs], dim=1)return bboxes.cpu(), valid.cpu()@dataclass406 unmodified lines)segment_results: Dict[int, Dict[int, ObjectInfo]] = {}# Phase A: Drain generator β GPU ops only, zero CUDA syncsraw_frames: list = []for out_frame_idx, out_obj_ids, out_mask_logits in self._video_predictor.propagate_in_video(inference_state,max_frame_num_to_track=step,start_frame_idx=start_idx,):bool_masks = (out_mask_logits[:, 0] > 0.0) # (N_obj, H, W) bool, GPUraw_frames.append((out_frame_idx, list(out_obj_ids), bool_masks))# Phase B: Batched bbox + ObjectInfo construction β 2 CUDA syncs totalif raw_frames:entries: list = []all_masks: list = []for frame_idx, obj_ids, bool_masks in raw_frames:for i, obj_id in enumerate(obj_ids):entries.append((frame_idx, obj_id, mask_dict.get_target_class_name(obj_id)))all_masks.append(bool_masks[i])if all_masks:stacked = torch.stack(all_masks)bboxes_cpu, valid_cpu = ObjectInfo.batch_bbox(stacked)del stackedbboxes_list = bboxes_cpu.tolist()valid_list = valid_cpu.tolist()for idx, (frame_idx, obj_id, class_name) in enumerate(entries):if valid_list[idx]:x1, y1, x2, y2 = int(bboxes_list[idx][0]), int(bboxes_list[idx][1]), int(bboxes_list[idx][2]), int(bboxes_list[idx][3])else:x1 = y1 = x2 = y2 = 0info = ObjectInfo(instance_id=obj_id,mask=all_masks[idx],class_name=class_name,x1=x1, y1=y1, x2=x2, y2=y2,)segment_results.setdefault(frame_idx, {})[obj_id] = infoif _pm is not None:_pl = getattr(self, '_perf_lock', None)188 unmodified linesobj_info.mask,)# Phase A: Drain generator β GPU ops only, zero CUDA syncsraw_frames: list = []for out_frame_idx, out_obj_ids, out_mask_logits in self._video_predictor.propagate_in_video(inference_state,max_frame_num_to_track=step,start_frame_idx=start_idx,):bool_masks = (out_mask_logits[:, 0] > 0.0) # (N_obj, H, W) bool, GPUraw_frames.append((out_frame_idx, list(out_obj_ids), bool_masks))# Phase B: Batched bbox + ObjectInfo construction β 2 CUDA syncs totalif raw_frames:entries: list = []all_masks: list = []for frame_idx, obj_ids, bool_masks in raw_frames:for i, obj_id in enumerate(obj_ids):entries.append((frame_idx, obj_id, mask_dict.get_target_class_name(obj_id)))all_masks.append(bool_masks[i])if all_masks:stacked = torch.stack(all_masks)bboxes_cpu, valid_cpu = ObjectInfo.batch_bbox(stacked)del stackedbboxes_list = bboxes_cpu.tolist()valid_list = valid_cpu.tolist()for idx, (frame_idx, obj_id, class_name) in enumerate(entries):if valid_list[idx]:x1, y1, x2, y2 = int(bboxes_list[idx][0]), int(bboxes_list[idx][1]), int(bboxes_list[idx][2]), int(bboxes_list[idx][3])else:x1 = y1 = x2 = y2 = 0info = ObjectInfo(instance_id=obj_id,mask=all_masks[idx],class_name=class_name,x1=x1, y1=y1, x2=x2, y2=y2,)all_results.setdefault(frame_idx, {})[obj_id] = info# deepcopy ONLY the last frame (was running every frame before)last_frame_idx = raw_frames[-1][0]last_frame_objects = all_results.get(last_frame_idx, {})sam2_masks = MaskDictionary()sam2_masks.labels = copy.deepcopy(last_frame_objects)if last_frame_objects:first_info = next(iter(last_frame_objects.values()))if first_info.mask is not None:sam2_masks.mask_height = first_info.mask.shape[-2] if first_info.mask.ndim >= 2 else 0sam2_masks.mask_width = first_info.mask.shape[-1] if first_info.mask.ndim >= 2 else 0
models/segmenters/grounded_sam2.py+120-36