//  [python]
def write_prompt_predictions_into_annotations_per_clip(self, first_frame_idx, out, targets, interim_size, image_size, stride):
        """
        Write predictions per clip into annotations, which can be viewd as the pseudo ground-truth annotations
        and can be used to generate visual prompts of objects for the following frames
        Args:
            out: a Dict that stores all predicted masks, boxes
            targets: A list with [Dict, Dict, ..], which stores the annotated masks of target-objects
            first_frame_idx: the indix of the first frame in the current processing clip
        """
        if out['pred_masks'].nelement() == 0:
            # there is no prompt queries
            return

        # batch size is 1 here
        pred_logits = out['pred_logits'] # Q_pxK
        pred_masks = out['pred_masks']   # Q_pxTxHxW
        pred_embds = out['pred_embds']   # Q_pxTxC
        pred_masks = t.nn.functional.interpolate(
            pred_masks, interim_size, mode='bilinear', align_corners=False
        )

        num_frames = pred_masks.shape[1]

        targets_per_video = targets[0]
        gt_logits = targets_per_video['logits']
        gt_masks = targets_per_video['masks']
        gt_mask_logits = targets_per_video['mask_logits']
        gt_boxes = targets_per_video['boxes']
        gt_embds = targets_per_video['embds']
        gt_occurrence = targets_per_video['occurrence']
        gt_mask_quality_scores = targets_per_video["mask_quality_scores"]
        dataset_name = targets_per_video['dataset_name']
        
        # check the consistency of entity masks with thh previous frames
        temporal_consistency_threshold = self.temporal_consistency_threshold 
        if first_frame_idx < self.num_frames:
            temporal_consistency_threshold *= 0.5
        is_consistency, sim_consistency = check_consistency_with_prev_frames(
            gt_embds[:, -max(int(self.num_prev_frames_memory/stride), 3):], pred_embds, 
            sim_threshold=temporal_consistency_threshold, return_similarity=True
        )

        cur_masks = pred_masks[:, :, :image_size[0], :image_size[1]]
        mask_quality_scores = calculate_mask_quality_scores(cur_masks) 
        if 'vis' in targets[0]["sub_task"]:
            # process overlapped area by multiple masks
            cur_scores = gt_logits.mean(1).max(-1)[0] * sim_consistency * mask_quality_scores
            cur_masks = cur_masks.sigmoid().flatten(1)
            is_bg = (cur_masks < 0.5).sum(0) == len(cur_masks)
            cur_prob_masks = cur_scores.view(-1, 1) * cur_masks
            cur_mask_ids = cur_prob_masks.argmax(0)  # take argmax (t, h, w)
            cur_mask_ids[is_bg] = -1
            cur_mask_ids = (cur_mask_ids[None] - torch.arange(cur_masks.shape[0], device=cur_masks.device).view(-1,1)) == 0

            original_area = (cur_masks > 0.5).sum(1).clamp(min=1)
            mask_area = cur_mask_ids.sum(1)
            above_ratio = (mask_area / original_area) > self.overlap_threshold_entity
            mask_over = (cur_mask_ids & (cur_masks > 0.5)).sum(1) > 0
            is_consistency = is_consistency & above_ratio & mask_over 

        if is_consistency.sum():
            matched_masks = pred_masks[is_consistency]
            box_normalizer = torch.as_tensor([interim_size[1], interim_size[0], interim_size[1], interim_size[0]], device=self.device)
            
            # gt_logits[is_consistency, -1] = pred_logits[is_consistency]
            nonblank_masks = matched_masks.flatten(-2).gt(0.).any(-1)
            gt_occurrence[is_consistency, -num_frames:] += nonblank_masks.float() 
            gt_mask_logits[is_consistency, -num_frames:] += matched_masks.clone() 
            gt_boxes[is_consistency, -num_frames:] = \
                convert_mask_to_box(gt_mask_logits[is_consistency, -num_frames:] > 0) / box_normalizer.view(1,1,-1)
            nonblank_embds = (gt_embds[is_consistency, -1] != 0).any(-1)
            gt_embds[is_consistency, -1] = \
                (gt_embds[is_consistency, -1] + pred_embds[is_consistency].mean(1)) / (nonblank_embds[..., None] + 1.)

            gt_mask_quality_scores[is_consistency] += mask_quality_scores[is_consistency] 
            
        targets_per_video['logits'] = gt_logits
        targets_per_video['masks'] = gt_mask_logits.gt(0.).float()
        targets_per_video['mask_logits'] = gt_mask_logits
        targets_per_video['boxes'] = gt_boxes
        targets_per_video['embds'] = gt_embds
        targets_per_video['occurrence'] = gt_occurrence
        targets_per_video["mask_quality_scores"] = gt_mask_quality_scores