def loss_groundings(self, outputs, targets, indices, num_masks, layer_id, extra):
        """Compute the losses related to the masks: the focal loss and the dice loss.
        targets dicts must contain the key "masks" containing a tensor of dim [nb_target_boxes, h, w]
        """
        assert "pred_gmasks" in outputs
        assert "pred_gtexts" in outputs
        
        if layer_id >= self.top_x_layers['grounding']:
            return {"loss_grounding_bce_0": 0, "loss_grounding_dice_0": 0, "loss_grounding_ce_0": 0}

        masks = [t["grounding_masks"] for t in targets]
        if indices is None or None in masks:
            loss = outputs['pred_gmasks'].sum() * 0.0
            return {"loss_grounding_bce_0": loss, "loss_grounding_dice_0": loss, "loss_grounding_ce_0": loss}

        pred_logits = []
        for b in range(len(indices)):
            t_emb = targets[b]['grounding_class_embs']
            v_emb = outputs["pred_gtexts"][b]
            
            t_emb = t_emb / (t_emb.norm(dim=-1, keepdim=True) + 1e-7)
            v_emb = v_emb / (v_emb.norm(dim=-1, keepdim=True) + 1e-7)            

            out_prob = vl_similarity(v_emb, t_emb, temperature=extra['lang_logit'])
            pred_logits += [out_prob]            
        outputs['pred_logits'] = pred_logits

        indices = self.matcher(outputs, targets, mode='grounding', extra={'temperature':extra['lang_logit']})
        src_idx = self._get_src_permutation_idx(indices)
        tgt_idx = self._get_tgt_permutation_idx(indices)

        src_masks = outputs["pred_gmasks"]
        src_masks = src_masks[src_idx]
        # TODO use valid to mask invalid areas due to padding in loss
        target_masks, valid = nested_tensor_from_tensor_list(masks).decompose()
        target_masks = target_masks.to(src_masks)
        target_masks = target_masks[tgt_idx]
        # No need to upsample predictions as we are using normalized coordinates :)
        # N x 1 x H x W
        src_masks = src_masks[:, None]
        target_masks = target_masks[:, None]
        
        with torch.no_grad():
            # sample point_coords
            point_coords = get_uncertain_point_coords_with_randomness(
                src_masks,
                lambda logits: calculate_uncertainty(logits),
                self.num_points,
                self.oversample_ratio,
                self.importance_sample_ratio,
            ).type(src_masks.dtype)
            # get gt labels
            point_labels = point_sample(
                target_masks,
                point_coords,
                align_corners=False,
            ).squeeze(1)

        point_logits = point_sample(
            src_masks,
            point_coords,
            align_corners=False,
        ).squeeze(1)

        losses = {
            "loss_grounding_bce_0": sigmoid_ce_loss_jit(point_logits, point_labels, len(src_masks)),
            "loss_grounding_dice_0": dice_loss_jit(point_logits, point_labels, len(src_masks)),
        }

        # compute query-token contrastive loss
        # ttk_emb = torch.cat([x['caption_tokens'] for x in targets], dim=0)
        # ttk_mask = torch.cat([x['caption_mask'] for x in targets], dim=0).float()
        # ttk_mask = ttk_mask * torch.cumsum(ttk_mask, dim=1)
        # vtk_emb = outputs['pred_captions'][:,:-1]
        # keep = torch.cat([x['caption_mask'] for x in targets], dim=0).bool()

        # ttk_emb = ttk_emb / (ttk_emb.norm(dim=-1, keepdim=True) + 1e-7)
        # vtk_emb = vtk_emb / (vtk_emb.norm(dim=-1, keepdim=True) + 1e-7)
        # logit_scale = extra['lang_encoder'].logit_scale.exp().clamp(max=100)

        # # prepare gt
        # gt = (torch.eye(vtk_emb.shape[0]).type_as(ttk_mask).unsqueeze(-1) * ttk_mask.unsqueeze(0).repeat(vtk_emb.shape[0], 1, 1))[:,keep].flatten(1)
        # gt = gt / (gt.sum(1, keepdim=True) + 1e-7)
        # # compute i2t loss
        # logits = logit_scale * (vtk_emb @ ttk_emb[keep].transpose(0, 1)).mean(1)
        # loss_contrast_fine_vt = SoftTargetCrossEntropy()(logits, gt)
        # # loss_contrast_fine = loss_contrast_fine_vt # i2t only

        # # compute t2i loss
        # bs, nq, _ = vtk_emb.shape
        # logits = logit_scale * (ttk_emb @ vtk_emb.flatten(0,1).transpose(0, 1)).reshape(bs,-1,bs,nq).mean(dim=-1)[keep,:]
        # loss_contrast_fine_tv = SoftTargetCrossEntropy()(logits, gt.t())
        # # compute loss
        # loss_contrast_fine = (loss_contrast_fine_vt * 0.7 + loss_contrast_fine_tv * 0.3)

        # compute t2i loss
        loss_grd_ce = 0
        for b in range(len(indices)):
            task = targets[b]['grounding_task']
            pred_logit = outputs["pred_logits"][b]
            gt_logit = torch.zeros_like(pred_logit)
            select_idx = torch.stack((indices[b][0], indices[b][1])).tolist()
            gt_logit[select_idx] = 1
            t_hash = torch.tensor(targets[b]['grounding_hash'], device=gt_logit.device)
            hash_table = torch.zeros((len(t_hash), len(t_hash)), device=gt_logit.device)
            for idx in range(0, len(hash_table)):
                hash_table[idx][t_hash==t_hash[idx]] = 1
            hash_table = hash_table / hash_table.sum(-1, keepdim=True)
            gt_logit = gt_logit @ hash_table
            loss_grd_ce += self.grounding_weight[task]*torch.sum(-gt_logit.t() * F.log_softmax(pred_logit.t(), dim=-1), dim=-1).mean()
        loss_grd_ce = loss_grd_ce / len(indices)
        losses.update({"loss_grounding_ce_0": loss_grd_ce})
        del src_masks
        del target_masks
        return losses
Compute the losses related to the masks: the focal loss and the dice loss.
        targets dicts must contain the key "masks" containing a tensor of dim [nb_target_boxes, h, w]
        
modeling/modules/criterion.py