
    $j	                     4   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e j        de j        de j        dz  dede j        f
dZ	 	 	 d/dede j        dz  dede j        dz  de j        f
dZ	 	 d.d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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d)ed*ed+ed,e	eeeeeed-ZdS )0    N)BCEWithLogitsLossMSELoss   )DFineForObjectDetectionLoss)$DeformableDetrForObjectDetectionLoss!DeformableDetrForSegmentationLoss)Deimv2ForObjectDetectionLoss)ForObjectDetectionLossForSegmentationLoss)#GroundingDinoForObjectDetectionLoss)LwDetrForObjectDetectionLoss)RTDetrForObjectDetectionLoss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          d/home/rurouni/.hermes/hermes-agent/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"   ForCausalLMLossr0   .   s     \\^^F""66"FFc122g1133 [[Z((F$$R((L??6=11Lv|5G``Y_``DKr$   r.   r/   c                     |                                  } |                     d|          } |                    d          }|                    | j                  }t	          | |||fi |}|S Nr)   )r*   r-   r   r   r#   )r.   r/   r%   r   r   r    r!   s          r"   ForMaskedLMLossr3   G   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)r/   r4   configr    r9   loss_fcts         r"   ForSequenceClassificationLossrB   [   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"   ForQuestionAnsweringLossrP   v   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 r2   )r-   r9   r   r   r*   r#   )r.   r/   r@   r    s       r"   ForTokenClassificationrR      s\    [[V.//F[[__..F\\^^Fvv88888r$   ForCausalLMForMaskedLMForQuestionAnsweringForSequenceClassificationForImageClassificationForVideoClassificationForAudioClassificationrR   ForSegmentationForObjectDetectionForConditionalGeneration DeformableDetrForObjectDetection!ConditionalDetrForObjectDetectionDabDetrForObjectDetectionGroundingDinoForObjectDetection!MMGroundingDinoForObjectDetectionConditionalDetrForSegmentation)RTDetrForObjectDetectionRTDetrV2ForObjectDetectionDFineForObjectDetectionDeimv2ForObjectDetectionCsmForConditionalGenerationLwDetrForObjectDetection)Nr   )Nr   N)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_rt_detrr   Tensorr=   r#   r0   r3   rB   rP   rR   LOSS_MAPPING r$   r"   <module>rt      sH           / / / / / / / / 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 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?? 4  !>	
 ; ; ; 4 * 0  '(L ()M  !E &'J  ()L!" %&G#$ !=">: <#2 </  r$   