a
    h                     @   sD  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 dejejeej eejdddZdeeej eeej ejdddZdejejeeej edddZejejejdddZdd ZejdddZeeeeeeeeeeee
e
e
eeeeeeedZdS )     )OptionalN)BCEWithLogitsLossMSELoss   )DFineForObjectDetectionLoss)$DeformableDetrForObjectDetectionLoss!DeformableDetrForSegmentationLoss)ForObjectDetectionLossForSegmentationLoss)#GroundingDinoForObjectDetectionLoss)RTDetrForObjectDetectionLoss)sourcetargetnum_items_in_batchignore_indexreturnc                 K   sN   |d urdnd}t jj| |||d}|dkrJt|rB||j}|| }|S )Nsummean)r   	reduction)nn
functionalZcross_entropytorchZ	is_tensortodevice)r   r   r   r   kwargsr   loss r   X/var/www/html/assistant/venv/lib/python3.9/site-packages/transformers/loss/loss_utils.pyfixed_cross_entropy   s    
r   )
vocab_sizer   r   shift_labelsr   c                 K   sr   |   } |d u r6t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)   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   ForMaskedLMLossG   s    	
r,   )r)   pooled_logitsr   c                 K   s   |j }|jd u rH|dkr d|_n(|dkrB| jtjtjfv rBd|_nd|_| |j} |jdkrt }|dkr~||	 | 	 S ||| S |jdkrt
|d|| dfi |S |jdkrt }||| S td|j d S )Nr   Z
regressionZsingle_label_classificationZmulti_label_classificationr#   zInvalid problem type: )
num_labelsZproblem_typeZdtyper   longintr   r   r   squeezer   r'   r   RuntimeError)r)   r-   configr   r.   Zloss_fctr   r   r   ForSequenceClassificationLoss\   s&    



 

r4   c           	      K   s   d }|d ur|d urt | dkr6|d| j}t | dkrX|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   ForQuestionAnsweringLossw   s    
r9   )r(   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ForVideoClassificationZForAudioClassificationr:   ZForSegmentationZForObjectDetectionZForConditionalGenerationZ DeformableDetrForObjectDetectionZ!ConditionalDetrForObjectDetectionZDabDetrForObjectDetectionZGroundingDinoForObjectDetectionZ!MMGroundingDinoForObjectDetectionZConditionalDetrForSegmentationZRTDetrForObjectDetectionZRTDetrV2ForObjectDetectionZDFineForObjectDetectionZCsmForConditionalGeneration)Nr   )Nr   N)Nr   )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>   sz          
