o
    h                     @   sN  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m	Z	 ddl
mZmZ ddlmZ ddlmZ 			d!d
ejdejdee dedejf
ddZ				d"dedee dedeej dejf
ddZ			d!d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eeeeeeeeeeee	eed ZdS )#    )OptionalN)BCEWithLogitsLossMSELoss   )$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
functionalcross_entropy)r   r   r   r   kwargsr   loss r   p/var/www/html/construction_image-detection-poc/venv/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   ForMaskedLMLossC   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dtypetorchlongintr%   r&   r   squeezer   r$   r   RuntimeError)r(   r,   configr   r0   loss_fctr   r   r   ForSequenceClassificationLossX   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sizer6   r%   r&   clampr   )	start_logits
end_logitsstart_positionsend_positionsr   
total_lossignored_index
start_lossend_lossr   r   r   ForQuestionAnsweringLosss   s   
rG   c                 K   s:   |  d|j} | d| j}|  } t| |fi |S r*   )r$   r0   r%   r&   r!   r   )r'   r(   r8   r   r   r   r   ForTokenClassification   s   rH   )ForCausalLMForMaskedLMForQuestionAnsweringForSequenceClassificationrH   ForSegmentationForObjectDetection DeformableDetrForObjectDetection!ConditionalDetrForObjectDetectionDabDetrForObjectDetectionGroundingDinoForObjectDetectionConditionalDetrForSegmentationRTDetrForObjectDetectionRTDetrV2ForObjectDetection)Nr   )Nr   N)typingr   r3   torch.nnr   r   r   loss_deformable_detrr   r   loss_for_object_detectionr   r	   loss_grounding_dinor
   loss_rt_detrr   Tensorr5   r   r)   r+   r:   rG   rH   LOSS_MAPPINGr   r   r   r   <module>   s   




