o
    Zh[$                     @   s  d dl Z d dlZd dlmZmZ d dlmZ d dlmZm	Z	m
Z
mZ d dlZd dlmZ d dlmZ ddlmZ dd	lmZ dd
lmZmZ ddlmZmZmZmZ eeZe e! Z"e#dd e"D Z$eG dd dZ%G dd deZ&G dd deZ'dS )    N)	dataclassfield)Enum)DictListOptionalUnion)FileLock)Dataset   )$MODEL_FOR_QUESTION_ANSWERING_MAPPING)PreTrainedTokenizer)check_torch_load_is_safelogging   )SquadFeaturesSquadV1ProcessorSquadV2Processor"squad_convert_examples_to_featuresc                 c   s    | ]}|j V  qd S N)
model_type).0conf r   O/var/www/auris/lib/python3.10/site-packages/transformers/data/datasets/squad.py	<genexpr>"   s    r   c                   @   s<  e Zd ZU dZedddde idZee	d< edddidZ
ee	d	< ed
ddidZee	d< ed
ddidZee	d< edddidZee	d< edddidZee	d< edddidZee	d< edddidZee	d< edddidZee	d< edddidZee	d< eddd idZee	d!< ed"dd#idZee	d$< dS )%SquadDataTrainingArgumentszb
    Arguments pertaining to what data we are going to input our model for training and eval.
    Nhelpz!Model type selected in the list: z, )defaultmetadatar   zFThe input data dir. Should contain the .json files for the SQuAD task.data_dir   zThe maximum total input sequence length after tokenization. Sequences longer than this will be truncated, sequences shorter will be padded.max_seq_lengthzVWhen splitting up a long document into chunks, how much stride to take between chunks.
doc_stride@   zkThe maximum number of tokens for the question. Questions longer than this will be truncated to this length.max_query_length   zThe maximum length of an answer that can be generated. This is needed because the start and end predictions are not conditioned on one another.max_answer_lengthFz1Overwrite the cached training and evaluation setsoverwrite_cachezDIf true, the SQuAD examples contain some that do not have an answer.version_2_with_negativeg        zIIf null_score - best_non_null is greater than the threshold predict null.null_score_diff_threshold   n_best_sizer   zjlanguage id of input for language-specific xlm models (see tokenization_xlm.PRETRAINED_INIT_CONFIGURATION)lang_id   z3multiple threads for converting example to featuresthreads)__name__
__module____qualname____doc__r   joinMODEL_TYPESr   str__annotations__r    r"   intr#   r%   r'   r(   boolr)   r*   floatr,   r-   r/   r   r   r   r   r   %   sb   
 				r   c                   @   s   e Zd ZdZdZdS )SplittraindevN)r0   r1   r2   r<   r=   r   r   r   r   r;   h   s    r;   c                   @   s   e Zd ZU dZeed< ee ed< eed< e	ed< dej
dddfded	ed
ee deeef dee	 dee dee fddZdd Zdeeejf fddZdS )SquadDatasetzH
    This will be superseded by a framework-agnostic approach soon.
    argsfeaturesmodeis_language_sensitiveNFpt	tokenizerlimit_length	cache_dirdataset_formatc                 C   s&  || _ || _|jrt nt | _t|tr'zt| }W n t	y&   t	dw || _
|jr/dnd}tj|d ur:|n|jd|j d|jj d|j d| }	|	d }
t|
 tj|	r|jst }t  tj|	dd| _| jd	 | _| jd
d | _| jdd | _t d|	 dt |  | jd u s| jd u rt!d|	 d nX|tj"kr| j#|j| _n| j$|j| _t%| j||j|j&|j'|tj(k|j)|d\| _| _t }t*| j| j| jd|	 t d|	 dt | dd W d    d S W d    d S 1 sw   Y  d S )Nzmode is not a valid split namev2v1Zcached__z.lockT)Zweights_onlyr@   datasetexamplesz"Loading features from cached file z [took %.3f s]zDeleting cached file z; will allow dataset and examples to be cached in future run)rL   rD   r"   r#   r%   Zis_trainingr/   Zreturn_dataset)r@   rK   rL   z!Saving features into cached file z [took z.3fz s])+r?   rB   r)   r   r   	processor
isinstancer6   r;   KeyErrorrA   ospathr4   r    value	__class__r0   r"   r	   existsr(   timer   torchloadZold_featuresr@   getrK   rL   loggerinfowarningr=   Zget_dev_examplesZget_train_examplesr   r#   r%   r<   r/   save)selfr?   rD   rE   rA   rB   rF   rG   Zversion_tagZcached_features_fileZ	lock_pathstartr   r   r   __init__w   sp   

"


$ zSquadDataset.__init__c                 C   s
   t | jS r   )lenr@   )r]   r   r   r   __len__   s   
zSquadDataset.__len__returnc                 C   s4  | j | }tj|jtjd}tj|jtjd}tj|jtjd}tj|jtjd}tj|jtj	d}tj|j
tj	d}|||d}	| jjdv rJ|	d= | jjdv rx|	||d | jjrc|	d|i | jrx|	dtj|jtjd| jj i | jtjkrtj|jtjd}
tj|jtjd}|	|
|d	 |	S )
N)Zdtype)	input_idsattention_masktoken_type_ids)xlmZrobertaZ
distilbertZ	camembertre   )Zxlnetrf   )	cls_indexp_maskis_impossibleZlangs)start_positionsend_positions)r@   rV   Ztensorrc   longrd   re   rg   rh   r:   ri   r?   r   updater)   rB   ZonesshapeZint64r-   rA   r;   r<   Zstart_positionZend_position)r]   ifeaturerc   rd   re   rg   rh   ri   Zinputsrj   rk   r   r   r   __getitem__   s0   
$zSquadDataset.__getitem__)r0   r1   r2   r3   r   r7   r   r   r;   r9   r<   r   r   r8   r   r6   r_   ra   r   rV   ZTensorrq   r   r   r   r   r>   m   s8   
 

Lr>   )(rP   rU   dataclassesr   r   enumr   typingr   r   r   r   rV   Zfilelockr	   Ztorch.utils.datar
   Zmodels.auto.modeling_autor   Ztokenization_utilsr   utilsr   r   Zprocessors.squadr   r   r   r   Z
get_loggerr0   rY   listkeysZMODEL_CONFIG_CLASSEStupler5   r   r;   r>   r   r   r   r   <module>   s&   
B