o
    i                     @   s  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mZ ddlmZ ddlmZ dd	lmZ 		
d2de jde jde jdB dede jf
ddZ		
	d3dede jdB dede jdB de jf
ddZ		
d2de jde jdede jdB def
ddZde jde jde jfddZdd Zde jfd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d0e	eeeeed1ZdS )4    N)BCEWithLogitsLossMSELoss   )DFineForObjectDetectionLoss)$DeformableDetrForObjectDetectionLoss!DeformableDetrForSegmentationLoss)ForObjectDetectionLossForSegmentationLoss)#GroundingDinoForObjectDetectionLoss)LwDetrForObjectDetectionLoss)RTDetrForObjectDetectionLosssourcetargetnum_items_in_batchignore_indexreturnc                 K   sN   |d urdnd}t jj| |||d}|dkr%t|r!||j}|| }|S )Nsummean)r   	reduction)nn
functionalcross_entropytorch	is_tensortodevice)r   r   r   r   kwargsr   loss r   ^/sda-disk/www/egybert/egybert_env/lib/python3.10/site-packages/transformers/loss/loss_utils.pyfixed_cross_entropy   s   
r!   
vocab_sizeshift_labelsc                 K   sr   |   } |d u rtjj|d|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   r   r   r    ForCausalLMLoss-   s   

r,   r*   r+   c                 K   sD   |   } | d|} |d}|| j}t| |||fi |}|S Nr%   )r&   r)   r   r   r!   )r*   r+   r"   r   r   r   r   r   r   r    ForMaskedLMLossF   s   	
r.   pooled_logitsc                 K   s   |j }|jd u r$|dkrd|_n|dkr!| jtjtjfv r!d|_nd|_| |j} |jdkrDt }|dkr?||	 | 	 S ||| S |jdkrYt
|d|| dfi |S |jdkrft }||| 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+   r/   configr   r3   loss_fctr   r   r    ForSequenceClassificationLossZ   s&   



 

r<   c           	      K   s   d }|d urY|d urYt | dkr|d| j}t | dkr,|d|j}| d}|d|}|d|}t| |fd|i|}t||fd|i|}|| d }|S )Nr   r%   r   r      )lensizer8   r   r   clampr!   )	start_logits
end_logitsstart_positionsend_positionsr   
total_lossignored_index
start_lossend_lossr   r   r    ForQuestionAnsweringLossu   s   
rI   c                 K   s:   |  d|j} | d| j}|  } t| |fi |S r-   )r)   r3   r   r   r&   r!   )r*   r+   r:   r   r   r   r    ForTokenClassification   s   rJ   ForCausalLMForMaskedLMForQuestionAnsweringForSequenceClassificationForImageClassificationForVideoClassificationForAudioClassificationForSegmentationForObjectDetectionForConditionalGeneration DeformableDetrForObjectDetection!ConditionalDetrForObjectDetectionDabDetrForObjectDetectionGroundingDinoForObjectDetection!MMGroundingDinoForObjectDetectionConditionalDetrForSegmentation)RTDetrForObjectDetectionRTDetrV2ForObjectDetectionDFineForObjectDetectionCsmForConditionalGenerationLwDetrForObjectDetection)Nr   )Nr   N)r   torch.nnr   r   r   loss_d_finer   loss_deformable_detrr   r   loss_for_object_detectionr   r	   loss_grounding_dinor
   loss_lw_detrr   loss_rt_detrr   Tensorr7   r!   r,   r.   r<   rI   rJ   LOSS_MAPPINGr   r   r   r    <module>   s   


		
