o
    Zh                     @   s  d dl mZ 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 		
d1dejdejdee dedejf
ddZ		
	d2dedee dedeej dejf
ddZ		
d1dejdejdedee 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ZdS )3    )OptionalN)BCEWithLogitsLossMSELoss   )DFineForObjectDetectionLoss)$DeformableDetrForObjectDetectionLoss!DeformableDetrForSegmentationLoss)ForObjectDetectionLossForSegmentationLoss)#GroundingDinoForObjectDetectionLoss)RTDetrForObjectDetectionLosssourcetargetnum_items_in_batchignore_indexreturnc                 K   s8   |d urdnd}t jj| |||d}|dkr|| }|S )Nsummean)r   	reduction)nn
functionalZcross_entropy)r   r   r   r   kwargsr   loss r   K/var/www/auris/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viewtodevicer   )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   ForMaskedLMLossD   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   Z
regressionZsingle_label_classificationZmulti_label_classificationr    zInvalid problem type: )
num_labelsZproblem_typeZdtypetorchlongintr%   r&   r   squeezer   r$   r   RuntimeError)r(   r,   configr   r-   Zloss_fctr   r   r   ForSequenceClassificationLossY   s&   



 

r4   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sizer1   r%   r&   clampr   )	Zstart_logitsZ
end_logitsZstart_positionsZend_positionsr   Z
total_lossZignored_indexZ
start_lossZend_lossr   r   r   ForQuestionAnsweringLosst   s   
r9   c                 K   s:   |  d|j} | d| j}|  } t| |fi |S r*   )r$   r-   r%   r&   r!   r   )r'   r(   r3   r   r   r   r   ForTokenClassification   s   r:   ZForCausalLMZForMaskedLMZForQuestionAnsweringZForSequenceClassificationZForImageClassificationZForSegmentationZForObjectDetectionZ DeformableDetrForObjectDetectionZ!ConditionalDetrForObjectDetectionZDabDetrForObjectDetectionZGroundingDinoForObjectDetectionZConditionalDetrForSegmentationZRTDetrForObjectDetectionZRTDetrV2ForObjectDetectionZDFineForObjectDetectionZCsmForConditionalGeneration)Nr   )Nr   N)typingr   r.   Ztorch.nnr   r   r   Zloss_d_finer   Zloss_deformable_detrr   r   Zloss_for_object_detectionr	   r
   Zloss_grounding_dinor   Zloss_rt_detrr   ZTensorr0   r   r)   r+   r4   r9   r:   ZLOSS_MAPPINGr   r   r   r   <module>   s   


		
