
    |-j!                        d dl Z d dlmZ d dlmZmZ ddlmZ ddlmZm	Z	 ddl
mZ ddlmZmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ 	 	 d5de j        de j        de j        dz  dede j        f
dZ	 	 	 d6dede j        dz  dede j        dz  de j        f
dZ	 	 d5de j        de j        dede j        dz  def
dZde j        de j        de j        fdZd Zde j        fdZ 	 	 	 	 d7de j        de j        dede j        dz  d e j        dz  d!e!de j        fd"Z"i d#e"d$ed%ed&ed'ed(ed)ed*ed+e d,ed-ed.ed/ed0ed1ed2ed3ee	eeeeeeeeed4
Z#dS )8    N)BCEWithLogitsLossMSELoss   )DFineForObjectDetectionLoss)$DeformableDetrForObjectDetectionLoss!DeformableDetrForSegmentationLoss)Deimv2ForObjectDetectionLoss)ForObjectDetectionLossForSegmentationLoss)#GroundingDinoForObjectDetectionLoss)LwDetrForObjectDetectionLoss)RfDetrForSegmentationLoss)RTDetrForObjectDetectionLoss)ParakeetForTDTLosssourcetargetnum_items_in_batchignore_indexreturnc                     |dnd}t           j                            | |||          }|dk    r3t          j        |          r|                    |j                  }||z  }|S )Nsummean)r   	reduction)nn
functionalcross_entropytorch	is_tensortodevice)r   r   r   r   kwargsr   losss          \/var/www/html/banglarbhumi/venv/lib/python3.11/site-packages/transformers/loss/loss_utils.pyfixed_cross_entropyr%      sv     ,7VI=&&vvL\e&ffDE?-.. 	D!3!6!6t{!C!C((K    
vocab_sizeshift_labelsc                 \   |                                  } |@t          j                            |d|          }|ddd f                                         }|                     d|          } |                    d          }|                    | j                  }t          | |||fi |}|S )N)r   r   )value.r   )	floatr   r   pad
contiguousviewr    r!   r%   )logitslabelsr'   r   r   r(   r"   r#   s           r$   ForCausalLMLossr2   0   s     \\^^F""66"FFc122g1133 [[Z((F$$R((L??6=11Lv|5G``Y_``DKr&   r0   r1   c                     |                                  } |                     d|          } |                    d          }|                    | j                  }t	          | |||fi |}|S Nr+   )r,   r/   r    r!   r%   )r0   r1   r'   r   r   r"   r#   s          r$   ForMaskedLMLossr5   I   sj     \\^^F [[Z((F[[__FYYv}%%Fvv/A<ZZSYZZDKr&   pooled_logitsc                    |j         }|j        B|dk    rd|_        n4|dk    r'| j        t          j        t          j        fv rd|_        nd|_        |                     |j                  } |j        dk    rPt                      }|dk    r0 ||	                                | 	                                          S  |||           S |j        dk    r5t          |                    d|          |                     d          fi |S |j        dk    rt                      } |||           S t          d|j                   )Nr   
regressionsingle_label_classificationmulti_label_classificationr+   zInvalid problem type: )
num_labelsproblem_typedtyper   longintr    r!   r   squeezer%   r/   r   RuntimeError)r1   r6   configr"   r;   loss_fcts         r$   ForSequenceClassificationLossrD   ]   sO   "J"??".F!^^%*ei1H!H!H"?F">FYY}+,,Fl**99??8M1133V^^5E5EFFF8M6222;;;"=#5#5b*#E#Ev{{SUaaZ`aaa:::$&&xv...
E0CEE
F
FFr&   c                 0   d }||t          |                                          dk    r-|                    d                              | j                  }t          |                                          dk    r-|                    d                              |j                  }|                     d          }|                    d|          }|                    d|          }t          | |fd|i|}t          ||fd|i|}||z   dz  }|S )Nr   r+   r   r      )lensizer@   r    r!   clampr%   )	start_logits
end_logitsstart_positionsend_positionsr"   
total_lossignored_index
start_lossend_losss	            r$   ForQuestionAnsweringLossrR   x   s%   J"}'@##%%&&**-55b99<<\=PQQO}!!##$$q(()11"55889JKKM$))!,,)//=AA%++A}==(mmUbmflmm
&z=gg}g`fgg 8+q0
r&   c                     |                      d|j                  } |                     d                              | j                  }|                                 } t          | |fi |S r4   )r/   r;   r    r!   r,   r%   )r0   r1   rB   r"   s       r$   ForTokenClassificationrT      s\    [[V.//F[[__..F\\^^Fvv88888r&      皙?auxiliary_logitsauxiliary_loss_weightc                 "   t           j                            | |j        dd          dd          }t	          ||||          }|It           j                            ||j        dd          dd          }	||t	          |	|||          z  z   }|S )NbilinearF)rH   modealign_corners)r   r   )r   r   interpolateshaper%   )
r0   r1   r   r   rW   rX   r"   upsampled_logitsr#   upsampled_auxiliary_logitss
             r$   ForSemanticSegmentationLossrb      s     }00fl233>OV`pu0vv&5GVb  D #%']%>%>6<#4:UZ &? &
 &
" +.A&CUdp/
 /
 /
 
 
 Kr&   ForSemanticSegmentationForCausalLMForMaskedLMForQuestionAnsweringForSequenceClassificationForImageClassificationForVideoClassificationForAudioClassificationrT   ForSegmentationForObjectDetectionForConditionalGeneration DeformableDetrForObjectDetection!ConditionalDetrForObjectDetectionDabDetrForObjectDetectionGroundingDinoForObjectDetection!MMGroundingDinoForObjectDetection)
ConditionalDetrForSegmentationRTDetrForObjectDetectionRTDetrV2ForObjectDetectionDFineForObjectDetectionDeimv2ForObjectDetectionCsmForConditionalGenerationLwDetrForObjectDetectionParakeetForTDTRfDetrForObjectDetectionRfDetrForInstanceSegmentation)Nr   )Nr   N)rU   NNrV   )$r   torch.nnr   r   r   loss_d_finer   loss_deformable_detrr   r   loss_deimv2r	   loss_for_object_detectionr
   r   loss_grounding_dinor   loss_lw_detrr   loss_rf_detrr   loss_rt_detrr   loss_tdtr   Tensorr?   r%   r2   r5   rD   rR   rT   r,   rb   LOSS_MAPPING r&   r$   <module>r      s           / / / / / / / / 4 4 4 4 4 4 i i i i i i i i 5 5 5 5 5 5 R R R R R R R R D D D D D D 6 6 6 6 6 6 3 3 3 3 3 3 6 6 6 6 6 6 ( ( ( ( ( ( /3	 LL t+ 	 \   * /3(,   t+	
  ,% \   : /3 LL  t+	
    (G%, Gu| Gjojv G G G G6  &95< 9 9 9 9 .2,0#& LL  t+	
 lT) ! \   .:? ? 4	
  !> ; ; ; 4 * 0  '(L ()M  !E  &'J!" ()L#$ 'H <">: <#2 <( <%>7  r&   