o
    ?"g                     @   s   d dl 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	m
Z
 ddlmZ ddlmZ dd	ed
efddZ			dded	ed
efddZ	dded	ed
efddZdd Zdd Zdd Zeeeeee
e	eeeeeeedZdS )    N)BCEWithLogitsLossMSELoss   )$DeformableDetrForObjectDetectionLoss!DeformableDetrForSegmentationLoss)ForObjectDetectionLossForSegmentationLoss)#GroundingDinoForObjectDetectionLoss)RTDetrForObjectDetectionLossnum_items_in_batchignore_indexc                 K   s8   |d urdnd}t jj| |||d}|dkr|| }|S )Nsummean)r   	reduction)nn
functionalcross_entropy)sourcetargetr   r   kwargsr   loss r   ^/mnt/skqttb/ctump_chatbot/chatbot/lib/python3.10/site-packages/transformers/loss/loss_utils.pyfixed_cross_entropy   s
   r   
vocab_sizec                 K   s~   |   } |d u r!|| j}tjj|d|d}|ddd f  }| d|} |d}|| j}t| |||fi |}|S )N)r   r   )value.r   )	floattodevicer   r   pad
contiguousviewr   )logitslabelsr   r   r   shift_labelsr   r   r   r   r   ForCausalLMLoss!   s   

r'   c                 K   sP   |   } || j}| d|} |d}|| j}t| |||fi |}|S Nr   )r   r   r    r#   r   )r$   r%   r   r   r   r   r   r   r   r   ForMaskedLMLoss<   s   
r)   c                 K   s   |j }|jd u r'|dkrd|_n|dkr$| jtjks | jtjkr$d|_nd|_| |j} |jdkrKt }|dkrD||	 | 	 }|S ||| }|S |jdkrbt
|d|| dfi |}|S |jdkrot }||| }|S )Nr   
regressionsingle_label_classificationmulti_label_classificationr   )
num_labelsproblem_typedtypetorchlongintr   r    r   squeezer   r#   r   )r%   pooled_logitsconfigr   r-   loss_fctr   r   r   r   ForSequenceClassificationLossM   s,   
 


 

r7   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sizer3   r   r    clampr   )	start_logits
end_logitsstart_positionsend_positionsr   
total_lossignored_index
start_lossend_lossr   r   r   ForQuestionAnsweringLossf   s   
rD   c                 K   s:   |  d|j} | d| j}|  } t| |fi |S r(   )r#   r-   r   r    r   r   )r$   r%   r5   r   r   r   r   ForTokenClassificationy   s   rE   )ForCausalLMForMaskedLMForQuestionAnsweringForSequenceClassificationrE   ForSegmentationForObjectDetection DeformableDetrForObjectDetection!ConditionalDetrForObjectDetectionDabDetrForObjectDetectionGroundingDinoForObjectDetectionConditionalDetrForSegmentationRTDetrForObjectDetectionRTDetrV2ForObjectDetection)Nr   )Nr   N)r0   torch.nnr   r   r   loss_deformable_detrr   r   loss_for_object_detectionr   r   loss_grounding_dinor	   loss_rt_detrr
   r2   r   r'   r)   r7   rD   rE   LOSS_MAPPINGr   r   r   r   <module>   sX   



