U
    9%eӺ                  V   @   s&  U d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlZd dl	m
Z
mZmZmZmZmZmZ d dlZd dlmZ d dlmZmZmZmZ d dlmZ d dlmZ ddlmZmZmZ dd	l m!Z! dd
l"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5 ddl6m7Z7m8Z8m9Z9m:Z:m;Z; e: rVd dl<m=Z= e>e?Z@ejABddC e7kZDdee eeeEeeE f  eeE dddZFddddddddddddddd d!d"d#d$d%d&d'd(d)d*d+d,d-d.d/d0d1d2d3d4d5d6d7d8d9d:g)ZGg ZHeGD ]2ZIeJeIeKr"eHLeFf eI neHLeFeI q d;d<d=d>d?d@dAdBdCdDdEdFgZMeNeOePeHeM ZQdGdH ZRddKdLZSdMdN ZTdOdP ZUdQdR ZVdSdT ZWdUdV ZXddWdXZYdYdZ ZZdd[d\d]Z[d^d_ Z\d`da Z]ddd[dbdcZ^ddd[dddeZ_dfddgdhdiZ`dd[djdkZadldm Zbdd[dndoZcdd[dpdqZddfdfddrdsdtZedfdfddrdudvZfdwdx Zgdydz Zhddd{d|d}Zidd[d~dZjdd ZkdJddddZldd ZmdddZndd Zodd Zpdd Zqdd ZrdddZsdddZtdd Zudd Zvdd ZwdddZxdd Zydd Zzdd Z{dd Z|ejj}eRejj~jeSejjeTejjeUejjeVejeWejj~jeYejjeXejeZeje[eje\eje]eje^eje_eje`ejeaejjebejecejedejeeejjefejegejjehejeiejenejeoejjepejejejjekejelejjemejjeqejjerejesejjetejeuejjevejewejj~jexejjeyejjezejje{eje|i+Zeeef ed< G dd deZG dd deZG dd deZdd Zdd Zdeeeee  dddZG dd deZejeeE dddZedddZddJefeeeeE  eee edddZdS )    N)AnyCallableDictListOptionalTypeUnion)nn)GraphGraphModuleProxyTracer)compatibilityParameterProxy   )PretrainedConfigPreTrainedModellogging)
get_values),MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING_NAMES MODEL_FOR_BACKBONE_MAPPING_NAMES!MODEL_FOR_CAUSAL_LM_MAPPING_NAMESMODEL_FOR_CTC_MAPPING_NAMES3MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING_NAMES,MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING_NAMES-MODEL_FOR_MASKED_IMAGE_MODELING_MAPPING_NAMES!MODEL_FOR_MASKED_LM_MAPPING_NAMES'MODEL_FOR_MULTIPLE_CHOICE_MAPPING_NAMES0MODEL_FOR_NEXT_SENTENCE_PREDICTION_MAPPING_NAMES#MODEL_FOR_PRETRAINING_MAPPING_NAMES*MODEL_FOR_QUESTION_ANSWERING_MAPPING_NAMES-MODEL_FOR_SEMANTIC_SEGMENTATION_MAPPING_NAMES,MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING_NAMES/MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING_NAMES(MODEL_FOR_SPEECH_SEQ_2_SEQ_MAPPING_NAMES,MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING_NAMES6MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING_NAMESMODEL_MAPPING_NAMES)ENV_VARS_TRUE_VALUESTORCH_FX_REQUIRED_VERSIONget_torch_versionis_peft_availableis_torch_fx_available)	PeftModelZFX_DEBUG_MODE )
model_namesupported_tasksreturnc                 C   s|   t ttttttttt	t
ttttttttd}|d kr<| }t|trL|g}g }|D ]"}|| | d }|rT|| qT|S )N)defaultZpretrainingznext-sentence-predictionz	masked-lmz	causal-lmz
seq2seq-lmzspeech-seq2seqzmultiple-choicezdocument-question-answeringzquestion-answeringzsequence-classificationztoken-classificationzmasked-image-modelingzimage-classificationzzero-shot-image-classificationZctczaudio-classificationzsemantic-segmentationZbackbone)r(   r    r   r   r   r#   r%   r   r   r!   r$   r&   r   r   r'   r   r   r"   r   keys
isinstancestrgetappend)r0   r1   Ztask_mappingZmodel_class_namestask
class_name r;   T/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/transformers/utils/fx.py%_generate_supported_model_class_namesI   s<    
r=   ZaltclipZalbertZbartZbertZ
blenderbotzblenderbot-smallZbloomZclipZconvnextZdebertaz
deberta-v2Z
distilbertz
donut-swinZelectraZgpt2Zgpt_neoZgptjZhubertZlayoutlmZlxmertZm2m_100ZmarianZmbartzmegatron-bertZ
mobilebertZmt5ZnezhaoptZpegasusZplbartZresnetZrobertaZ	segformerZspeech_to_textZspeech_to_text_2ZswinZt5ZtrocrZvitZxglmZwav2vec2ZCLIPTextModelZCLIPTextModelWithProjectionZCLIPVisionModelZCLIPVisionModelWithProjectionZAltCLIPTextModelZAltCLIPVisionModelZGitVisionModelGPT2DoubleHeadsModelZSpeech2Text2DecoderZTrOCRDecoderPeftModelForCausalLMPeftModelForSeq2SeqLMc                 C   s&   t j|j| jjd fd| jjdS Nmeta)devicedtype)torchemptyshapeweightrF   selfinputr;   r;   r<   torch_nn_embedding   s    rN          @Fc                 C   s"   t j| j|jd fd|jdS rB   )rG   rH   rI   rF   )rM   rJ   Zpadding_idxZmax_normZ	norm_typeZscale_grad_by_freqsparser;   r;   r<   torch_nn_functional_embedding   s    rQ   c                 C   s   |S Nr;   rK   r;   r;   r<   torch_nn_layernorm   s    rS   c                 C   s   |S rR   r;   rK   r;   r;   r<   torch_nn_groupnorm   s    rT   c                 C   s    t j|jd d | jf ddS )NrC   rD   rE   )rG   rH   rI   Zout_featuresrK   r;   r;   r<   torch_nn_linear   s    rV   c                 C   s   | S rR   r;   xr;   r;   r<   
torch_relu   s    rY   c                 C   s   |S rR   r;   )rL   rX   r;   r;   r<   torch_nn_relu   s    rZ   c                 C   s   |st d| S )Nz>Don't support in-place functional.relu for MetaTensor analysis
ValueError)rX   Zinplacer;   r;   r<   torch_nn_functional_relu   s    r]   c                 C   s$   | j dd|j dd |j dd S NrD   rU   to)	conditionrX   yr;   r;   r<   torch_where   s    rc   outc                C   s   |d k	rt d| S )Nz2Don't support in-place abs for MetaTensor analysisr[   )rM   re   r;   r;   r<   	torch_abs   s    rf   c                  O   s   t | }d}|dkr"d}| d }n|dkr4| \}}n
| \}}}t|trPt|}t|trbt|}t|trtt|}|d|}|d}tj|| | |ddS )N   r   r   steprF   rD   rF   rE   )lenr5   floatintr7   rG   rH   )argskwargsnrh   startendrF   r;   r;   r<   torch_arange   s"    






rr   c                  O   sT   t | } t| d tjr4| d jtdkr4d| d< t|}|dd  tj| |S )Nrg   rD   rE   )listr5   rG   TensorrE   dictpopfull)rm   rn   Zkwargs_without_devicer;   r;   r<   
torch_full   s    $rx   c                   s    d kr|d krd  d kr(|d k	r(|  dk r@| d      dd | D }t|d }t fdd|D }|d   |g | d d   }tj|ddS )	Nr   c                 S   s   g | ]
}|j qS r;   )rI   ).0tr;   r;   r<   
<listcomp>  s     ztorch_cat.<locals>.<listcomp>c                 3   s   | ]}|  V  qd S rR   r;   )ry   rI   dimr;   r<   	<genexpr>  s     ztorch_cat.<locals>.<genexpr>rg   rD   rU   )r}   rs   sumrG   rH   )tensorsr}   axisre   ZshapesrI   Zconcatenated_dimZfinal_shaper;   r|   r<   	torch_cat  s    "r   c                C   sp   |d kr|d krd}|d kr(|d k	r(|}|dk rD| d   d | }t| d j}||t|  tj|ddS Nr   rg   rD   rU   )r}   rs   rI   insertrj   rG   rH   )r   r}   r   re   rI   r;   r;   r<   torch_stack  s    r   rg   )alphare   c          	      C   s   t | tjstj|ddS t |tjs4tj| ddS t|  | }t| jdg||     }t|jdg||    }g }t|D ]}|	t|| ||  qtj
|ddS )NrD   rU   rg   )r5   rG   rt   
empty_likemaxr}   rs   rI   ranger8   rH   )	rM   otherr   re   
max_lengthZinput_shapeZother_shaperI   ir;   r;   r<   	torch_add   s    r   c                C   s   t | ||dS )Nrd   )r   )rM   r   re   r;   r;   r<   	torch_mul.  s    r   c                 C   s
   t | |S rR   )r   )rL   r   r;   r;   r<   torch_tensor_mul2  s    r   c          
      C   s  |   }|  }d }|dkr,|dkr,d }nL|dkrT|dkrT| d|df}n$|dkrt|dkrt|df}n|dkr|dkr| df}nt|   |  }t| j}t|j}|dkrdg| }|dkr|d dg||  t| j }dg||  t|j }g }t|D ]}	|t||	 ||	  q|d |d< |d |d< |dkrd|d |dkrx|d |d krtj	dddS tj
|d	diS )
Nrg   r   r   rC   g        rD   rU   rE   )r}   sizer   rs   rI   r8   r   rv   rG   tensorrH   )
rM   r   re   Zd1Zd2rI   r   Zshape1Zshape2r   r;   r;   r<   torch_matmul6  s@    








r   c                C   s:   |d k	rt d| j\}}}|j\}}}tj|||ddS )Nz2Don't support in-place bmm for MetaTensor analysisrD   rU   )r\   rI   rG   rH   )rM   Zmat2re   
batch_sizero   m_pr;   r;   r<   	torch_bmmZ  s
    r   betar   re   c                C   s   |d k	rt dt||S )Nz6Don't support in-place baddbmm for MetaTensor analysis)r\   r   )rM   batch1batch2r   r   re   r;   r;   r<   torch_baddbmmb  s    r   c                C   s   t | |||||dS )Nr   )r   )rL   r   r   r   r   re   r;   r;   r<   torch_tensor_baddbmmh  s    r   c                 G   s$   dd |D }t j| f| dS )Nc                 s   s   | ]}t j|d dV  qdS )cpurU   N)rG   r   )ry   operandr;   r;   r<   r~   n  s     ztorch_einsum.<locals>.<genexpr>rD   )rG   einsumr`   )ZequationZoperandsZconcrete_operandsr;   r;   r<   torch_einsuml  s    r   c                 G   s:   t | j}t|D ]\}}||  |9  < qtj|ddS r^   )rs   rI   	enumeraterG   rH   )rL   sizesrI   r   rX   r;   r;   r<   torch_tensor_repeatr  s    
r   )r}   output_sizec                 G   s   t |}|dkr,|d k	r|n
|d  g}nt|d j}| d krb|dkrT|d } nt|g}d} |d }t|tst|dkr||   t|9  < n|d k	r|n| || < tj|ddiS )Nrg   r   r   rE   rD   )	rj   r   rs   rI   r5   rl   rG   ZnumelrH   )r}   r   rm   num_argsrI   Zrepeatsr;   r;   r<   torch_repeat_interleavey  s    

r   c                C   s&   t | j}t|||< tj|ddiS NrE   rD   )rs   rI   rj   rG   rH   )rM   r}   indexre   rI   r;   r;   r<   torch_index_select  s    
r   c                 C   s   t | ||S rR   )r   rL   r}   r   r;   r;   r<   torch_tensor_index_select  s    r   )sparse_gradre   c                C   s(   t | j}|j| ||< tj|ddiS r   )rs   rI   rG   rH   )rM   r}   r   r   re   rI   r;   r;   r<   torch_gather  s    
r   c                 C   s   t | ||S rR   )r   r   r;   r;   r<   torch_tensor_gather  s    r   c                 C   s   | S rR   r;   )rM   Zshiftsdimsr;   r;   r<   
torch_roll  s    r   c                 C   s   | S rR   r;   )rM   r   r;   r;   r<   
torch_flip  s    r   c                 C   s   | S rR   r;   )rL   r   r;   r;   r<   torch_tensor_flip  s    r   c                 C   s   |j d }d }| j}|dkr d}|dkr2t|j }|d krt|j }t|d|d   | jd | jd d   d | jd  d }||d< | j|d< t	j
|d	d
S )NrC   validr   r   samer   r   rg   r   rD   rU   rI   paddingrs   mathfloorZdilationZkernel_sizeZstrideZout_channelsrG   rH   )rL   rM   Zl_inrI   r   Zl_outr;   r;   r<   torch_nn_conv1d  s    


8
r   c                 C   s   |j dd  \}}d }| j}|dkr(d}|dkr:t|j }|d krt|j }t|d|d   | jd | jd d   d | jd  d }t|d|d   | jd | jd d   d | jd  d }||g|dd < | j|d< t	j
|d	d
S )Nr   r   r   r   r   r   rg   rD   rU   r   )rL   rM   Zh_inZw_inrI   r   Zh_outZw_outr;   r;   r<   torch_nn_conv2d  s$    

88
r   c                 C   sr   t | j}|d k	r>|dk r&|  | }|| dkrd|| n&g }|D ]}|dkrTqF|| qF|}tj|ddS r   )rs   rI   r}   rv   r8   rG   rH   )rM   r}   rI   Z	new_shapeZ	dim_valuer;   r;   r<   torch_squeeze  s    
r   c                 C   s
   t | |S rR   )r   rL   r}   r;   r;   r<   torch_tensor_squeeze  s    r   c                 C   s<   t | j}|dk r"|  d | }||d tj|ddS r   )rs   rI   r}   r   rG   rH   )rM   r}   rI   r;   r;   r<   torch_unsqueeze  s
    
r   c                 C   s
   t | |S rR   )r   r   r;   r;   r<   torch_tensor_unsqueeze  s    r   c                 K   sD   t jt j| ddf|}t|t jr.|dS tt|dd S d S )Nr   rU   rD   c                 S   s
   |  dS )NrD   r_   rW   r;   r;   r<   <lambda>      z*torch_unique_consecutive.<locals>.<lambda>)rG   unique_consecutiveZ
zeros_liker5   rt   r`   tuplemap)rM   rn   outputr;   r;   r<   torch_unique_consecutive  s    
r   rC   c                 C   s.   |dk rt dt| j|g }tj|ddS )Nr   zEDon't support automatic num_classes inference for MetaTensor analysisrD   rU   )r\   rs   rI   rG   rH   )r   Znum_classesrI   r;   r;   r<   torch_nn_functional_one_hot  s    r   c                 C   s$   | j dkr|j}nd}tj|ddS Nnone)rg   rD   rU   Z	reductionrI   rG   rH   rL   rM   targetrI   r;   r;   r<   torch_nn_mseloss  s    
r   c                 C   s$   | j dkr|j}nd}tj|ddS r   r   r   r;   r;   r<   torch_nn_crossentropyloss  s    
r   c                 C   s$   | j dkr|j}nd}tj|ddS r   r   r   r;   r;   r<   torch_nn_bcewithlogitsloss  s    
r   c                 C   s^   dd }t | tjrRt |tr.tt||}n||}ttj| dd|dS t| |S )Nc                 S   sH   t | tjrDtj| dd}|jtjtjtjtjfkr@|	tj
}|S | S )Nr   rU   )r5   rG   rt   Z	ones_likerF   Zfloat16float32Zfloat64Zint32r`   Zint64)rz   Zconcreter;   r;   r<   to_concrete  s    z%operator_getitem.<locals>.to_concreter   rU   rD   )	r5   rG   rt   r   r   operatorgetitemr   r`   )abr   r;   r;   r<   operator_getitem  s    
r   _MANUAL_META_OVERRIDESc                       sh   e Zd ZdZdd Zedd Zedd Z fdd	Z fd
dZ	dd Z
dd Z fddZ  ZS )HFProxyzI
    Proxy that uses metadata to handle data-dependent control-flow.
    c                 C   s
   || _ d S rR   )	_metadata)rL   metadatar;   r;   r<   install_metadatac  s    zHFProxy.install_metadatac                 C   s   | j dd| fi S )Ncall_methodr   )tracercreate_proxyrL   r;   r;   r<   rI   f  s    zHFProxy.shapec                 C   s
   t | dS )NrE   )MetaDeviceAttributer   r;   r;   r<   rE   j  s    zHFProxy.devicec                    s(   t | dr| jd k	rt| jS t  S Nr   )hasattrr   rj   super__len__r   	__class__r;   r<   r   p  s    
zHFProxy.__len__c                    s$   t | dr| jd k	r| jS t  S r   )r   r   r   __bool__r   r   r;   r<   r   u  s    zHFProxy.__bool__c                 C   s   |dkr|  |S t| |S r   )__getattribute__HFAttribute)rL   kr;   r;   r<   __getattr__z  s    
zHFProxy.__getattr__c                 C   s   | j dtj| ||fi S Ncall_function)r   r   r   setitem)rL   indicesvaluesr;   r;   r<   __setitem__  s    zHFProxy.__setitem__c                    s*   t | dr| jd k	r|| jkS t |S r   )r   r   r   __contains__)rL   keyr   r;   r<   r     s    
zHFProxy.__contains__)__name__
__module____qualname____doc__r   propertyrI   rE   r   r   r   r   r   __classcell__r;   r;   r   r<   r   ^  s   

r   c                   @   s.   e Zd ZedddZedd Zdd ZdS )	r   )attrc                 C   s>   || _ || _|j| _d | _t| j dr:| t| j j| d S r   )rootr   r   _noder   r   getattrr   )rL   r   r   r;   r;   r<   __init__  s    zHFAttribute.__init__c                 C   s0   | j d kr*| jdtj| j| jfi j| _ | j S r   )r   r   r   builtinsr  r   r   noder   r;   r;   r<   r    s    
 zHFAttribute.nodec                 O   s   | j d| j| jf| |S )Nr   )r   r   r   r   )rL   rm   rn   r;   r;   r<   __call__  s    zHFAttribute.__call__N)r   r   r   r6   r  r   r  r  r;   r;   r;   r<   r     s   	
r   c                   @   s   e Zd ZdS )r   N)r   r   r   r;   r;   r;   r<   r     s   r   c                 C   sH   t | trdS t | tjjrDt | tr0t| ds>td|  | jS | S )z\Returns the underlying metadata for HFProxies, and behaves like the identity for the others.rD   r   zNo metadata was found for )	r5   r   rG   fxr   r   r   RuntimeErrorr   vr;   r;   r<   _proxies_to_metas  s    
r
  c                    s   t   fdd}| fS )Nc                     sX   d   fdd}t jj| | t jj||  d k	rJ jd| |S | |S d S )Nc                    s   t | tr|  d S rR   r5   r   r  proxyr;   r<   check_has_proxy  s    
zB_gen_constructor_wrapper.<locals>.wrapper.<locals>.check_has_proxyr   )rG   r  r  map_aggregater   r   )rm   rn   r  r   r  r<   wrapper  s    z)_gen_constructor_wrapper.<locals>.wrapper)	functoolswraps)r   r  r;   r  r<   _gen_constructor_wrapper  s    r  
      )lowhighforbidden_valuesc                 C   s2   |d krg }t | |}||kr.t | |}q|S rR   )randomrandint)r  r  r  valuer;   r;   r<   _generate_random_int  s    r  c                
       s  e Zd ZU dZdZeed< dZeed< ddddd	d
ddddg
Ze	 sJe
fne
efZefdf fdd	Ze
eee eeejf dddZd4 fdd	Zdd Zeeeeef dddZ fddZdd  Zd5eejjed!ef f e eeef  e eeef  ee!d" fd#d$Z"ejed%d&d'Z#ejed%d(d)Z$ejed% fd*d+Z%ejjeed, fd-d.Z&e'dd/d0ed1d2d3Z(  Z)S )6HFTracerz
    Tracer that is able to symbolically trace models from the library. To do that, it uses the HFProxy instead of the
    regular PyTorch torch.fx.Proxy.
    Tproxy_buffer_attributesallow_insert_stateless_modsarangezerosZonesrw   Z	full_likeeyerH   r   clampZfinfor;   c                    s2   t  j||d t s.tdt  dt dd S )N)autowrap_modulesautowrap_functionsz6Found an incompatible version of torch. Found version z, but only version z is supported.)r   r  r-   ImportErrorr+   r*   )rL   r%  r&  r   r;   r<   r    s
    zHFTracer.__init__)model
input_namerI   r2   c              
   C   s  t |d|jj}|j}i }|dkr|d }|tttttttttt	krlt
j|t
j|d|d< q|ttttd)krt
j|t
j|d|d< t
j|t
j|d|d< q|ttkrlt|jd	r|jjd
krtd|jjdkr||jjf}t
j}	nR|jjdkr|f}t
j}	n6|jjdkr@||jjf}t
j}	ntd|jj dt
j||	|d|d< n|ttttttttttttd*krt
j|t
j|d|d< n>|ttkrt
j|t
j|d|d< ntd| d| dnd|kr|d }t |jdd
}
|
d
kr`t|jdr:|jjj}
n&t|jdrT|jj j}
nt! t! f}
t |jdd}t"|
t#j$j%s|
|
f}
|
\}}t
j||||t
j|d||< n^d|krt
j|d+t
j&|d||< n4d|krt
j||jj'ft
j&|d||< nd |kr4t
j||jj(g t
j&|d||< nd!|kr`t
j||jj)g t
j&|d||< nd"|krt
j|t
j&|d||< nd#|kr|\}}t!d$d%d&}t
j||t
j&|d||< nPd'|ksd(|krt
j|t
j|d||< n$||jj*g }t
j|t
j&|d||< |S ),z4Generates dummy input for model inference recording.class_for_deserialization)labelsstart_positionsend_positionsr   ri   r+  XLNetForQuestionAnsweringr,  r-  problem_typeNzCould not retrieve the problem type for the sequence classification task, please set model.config.problem_type to one of the following values: "regression", "single_label_classification", or "multi_label_classification".Z
regressionZsingle_label_classificationZmulti_label_classificationzExpected model.config.problem_type to be either: "regression", "single_label_classification", or "multi_label_classification", but "z" was provided.r?   r@   rA   z!Generating the dummy input named z for z is not supported yet.Zpixel_values
image_sizevision_configencodernum_channels   Zbbox   Zinput_featuresZvisual_featsZ
visual_posinputsZinput_valuesi'  i N  r  r  maskZids)r.  )r?   r@   rA   )r5  )+r  r   r   rE   r   r   r   r   r   r   rG   r"  longr!   r   r$   r   configr/  r\   Z
num_labelsr   r    r&   r   r   r#   r"   r   NotImplementedErrorr1  r0  r2  r  r5   collectionsabcIterablerk   Zinput_feat_per_channelZvisual_feat_dimZvisual_pos_dimZhidden_size)rL   r(  r)  rI   Zmodel_class_namerE   Zinputs_dictr   Zlabels_shapeZlabels_dtyper0  r3  heightwidthr   Z
seq_lengthZshape_with_hidden_sizer;   r;   r<   _generate_dummy_input  s    
    


     
 
  



zHFTracer._generate_dummy_inputNc                    s8  t  |||||||}|dkr>|| jkr>|| j|  |S || jkrXd|krXd|d< ztjj|t	}	tjj|t	}
|dkrt
||}||	|
}t|tjr|jdd}n|dkrt|	d j|}t
||}||	|
}n|dkr`t| d	s
t|  d
d| _zD| j|}t|}|t
krFt
| |f|	|
}n| j|	|
}W 5 d| _X nl|dkrd| _zJ| j}|d}|D ]}t||}qt|tjr|jdd}n|}W 5 d| _X n|W S t|tstd|| W nF tk
r2 } z&tr"td| d| d|  W 5 d }~X Y nX |S )NplaceholderrE   rD   r   rU   r   r   call_moduleorig_forwardz/ does not have an attribute called orig_forwardTFget_attr.z"Don't support composite output yetzCould not compute metadata for z target z: )r   r   	meta_argsr   orig_fnsrG   r  r  r  r
  r   r7   r5   rt   r`   r  r   r   AttributeError_disable_module_getattrr   Zget_submoduletyperD  splitr   r\   	Exception_IS_IN_DEBUG_MODEwarningswarn)rL   kindr   rm   rn   nameZ	type_exprproxy_factory_fnrvZ
args_metasZkwargs_metasZmeta_targetZmeta_outmethodmodmod_typeZattr_itrZatomsZatomer   r;   r<   r   l  sb    







.zHFTracer.create_proxyc                    s   t  ddr|S  fdd}t|tjjrH|| j |}|d k	rH|S  jrxt|tjrx|| j	 |}|d k	rx|S |S d S )NrJ  Fc                    s~   |D ]t\} |kr|krli }dt jjkrPjs<d n fdd|d< jddi f|}||< |   S qd S )NrS  c                    s   t |  S rR   r   )r  )attr_valro   rL   r;   r<   r     r   zLHFTracer._module_getattr.<locals>.maybe_get_proxy_for_attr.<locals>.<lambda>rE  r;   )inspect	signaturer   
parametersZparam_shapes_constant)rY  Zcollection_to_searchparameter_proxy_cacher   rn   Z	val_proxyr   )rY  ro   r<   maybe_get_proxy_for_attr  s    z:HFTracer._module_getattr.<locals>.maybe_get_proxy_for_attr)
r  r5   rG   r	   	Parameterr   Znamed_parametersr  rt   Znamed_buffers)rL   r   rY  r]  r^  Zmaybe_parameter_proxyZmaybe_buffer_proxyr;   r   r<   _module_getattr  s(        zHFTracer._module_getattr)r   rY  r]  c                 C   s   |  |||S rR   )r`  )rL   r   rY  r]  r;   r;   r<   r    s    zHFTracer.getattrc                    s   || _ t ||||S rR   )rD  r   rC  )rL   r   forwardrm   rn   r   r;   r<   rC    s    zHFTracer.call_modulec                 C   s
   t || S rR   )r   )rL   r  r;   r;   r<   r    s    zHFTracer.proxy.)r   concrete_argsdummy_inputs6complete_concrete_args_with_inputs_not_in_dummy_inputsr2   c                    s  t t|tjjr|jn|} dkr*i  dk	r|r|j D ]0}|j	krPq@|j
t jjkr@td|j	 dq@  fdd|j D  |j    }t }t }	||	g}
|jjttkrtddd}|
d	| dk	rtni }|D ]V}||krqt|| js(t|jd
r>|| |||
 qtd| dqdd | D }|j D ]2}|jt jjkrl|j	|krli |d|j	 < ql|| _ dd | j!D | _"t# | _$| j" D ]&\}\}}t%t|| | j$&| qzt' j(| d| _)W 5 | j" D ]\}\}}t%t|| qX | j)j*D ]}|j+dkr|j,|krfd|_-tj.|_n\|g}t/0 }|r|1d}d||< |t2|j3 7 }qtt4| D ]}| j)5| q|j+dkr:d|_q:| j)S )a  
        Traces `root` and returns the corresponding FX `torch.fx.Graph` representation. `root` can either be a
        `torch.nn.Module` instance or a Python callable. Note that after this call, `self.root` may be different from
        the `root` passed in here. For example, when a free function is passed to `trace()`, we will create a
        `torch.nn.Module` instance to use as the root and add embedded constants to.

        Args:
            root (`torch.nn.Module` or  `Callable`):
                Either a `torch.nn.Module`` or a function to be traced through. If root is not a
                [`~transformers.PreTrainedModel`], then `dummy_inputs` must be passed, otherwise tracing will fail.
            concrete_args (`Dict[str, Any], *optional*):
                Concrete arguments that should not be treated as Proxies
            dummy_inputs (`Dict[str, Any]`, *optional*):
                The dummy inputs needed to handle data-dependent control-flow if `root` is not a
                [`~transformers.PreTrainedModel`]. It can also be used when `root` is a
                [`~transformers.PreTrainedModel`] to specify custom dummy inputs for a subset or all the model inputs.
            complete_concrete_args_with_inputs_not_in_dummy_inputs (`bool`, *optional*, defaults to `True`):
                If `True`, and `dummy_inputs` is specified, every argument that `root` can take that is not in
                `dummy_inputs` and not in `concrete_args` will be added to `concrete_args`, otherwise does nothing.

        Returns:
            `torch.fx.Graph`:
                A FX `torch.fx.Graph` representing the semantics of the passed-in `root`.

        Nz6You need to specify a default value for the parameter rF  c                    s*   i | ]"}|j kr|j  kr|j |jqS r;   rR  r3   ry   r   rb  rc  r;   r<   
<dictcomp>  s
   
 
 z"HFTracer.trace.<locals>.<dictcomp>r      r7  rg   )Z_deserialize_graph_moduleZ_CodeOnlyModulezCould not generate input named z8 for because root is not a transformers.PreTrainedModel.c                 S   s,   i | ]$\}}|t |tjr$|d n|qS )rD   )r5   rG   rt   r`   )ry   r)  Zinput_r;   r;   r<   rh  .  s    z**c                 S   s   i | ]}|t tt|qS r;   )r  r  rG   )ry   r   r;   r;   r<   rh  6  s     rb  rB  r;   r   r   )6rZ  r[  r5   rG   r	   Modulera  r\  r   rR  r3   r_  rH   r\   updater4   r  r   r   r   r   r   ru   supported_archsrK  r   
startswithrA  r  itemsrQ  VAR_KEYWORDrG  _TORCH_METHODS_TO_PATCHZpatched_torch_methodssetrH  setattraddr   tracegraphZnodesopr   rm   rt   r<  OrderedDictrv   rs   ZusersreversedZ
erase_node)rL   r   rb  rc  rd  sigparaminput_namesr   Zsequence_lengthrI   Znum_choicesr6  r)  Zconcrete_metasrR  r  origr   r  to_visitZ	to_deletero   userr   rg  r<   ru    s     





zHFTracer.trace)rV  r2   c                 C   s   t dd |j D S )z
        Whether the module was instantiated with Proxies. If that is the case, such module cannot be a leaf module
        because its attributes are input-dependent.
        c                 s   s   | ]}t |tV  qd S rR   r  )ry   r   r;   r;   r<   r~   g  s     zKHFTracer._stateless_mod_instanciation_depends_on_proxies.<locals>.<genexpr>)any__dict__r   )rL   rV  r;   r;   r<   /_stateless_mod_instanciation_depends_on_proxiesb  s    z8HFTracer._stateless_mod_instanciation_depends_on_proxiesc                 C   s   |  |rdS d}|jj }| d| }d}t| j|rjt| j||krRd}qj| d| }|d7 }q0|s|| j|| |S )zb
        Helper method which tries to insert a module that was not declared as submodule.
        r/   r   r   FTrg   )r  r   r   lowerr   r   r  Z
add_module)rL   rV  idxmod_namepathZalready_insertedr;   r;   r<   _insert_module_as_submodulei  s    

z$HFTracer._insert_module_as_submodulec              
      s~   zt  |W S  tk
rx } zL| jrdtt| dkrdtt| dkrd| |}| W Y 
S |W 5 d}~X Y nX dS )ag  
        Helper method to find the qualified name of `mod` in the Module hierarchy of `root`. For example, if `root` has
        a submodule named `foo`, which has a submodule named `bar`, passing `bar` into this function will return the
        string "foo.bar".

        Args:
            mod (str): The `Module` to retrieve the qualified name for.
        r   N)	r   path_of_module	NameErrorr   rj   rs   r\  buffersr  )rL   rV  rX  r  r   r;   r<   r    s    	.
zHFTracer.path_of_module)r   module_qualified_namer2   c                    s   |  | ot ||S rR   )r  r   is_leaf_module)rL   r   r  r   r;   r<   r    s     zHFTracer.is_leaf_module)Zis_backward_compatibler   )objr2   c                 C   s"   t |d }|jjdkr|jS |S )zCalled when a proxy object is has the keys() method called.
        This is what happens when ** is called on a proxy. This should return an iterator if ** is supposed to work in
        your custom tracer.
        r4   z**kwargs)r   r  r   r   )rL   r  	attributer;   r;   r<   r4     s    zHFTracer.keys)NNN)NNT)*r   r   r   r   r  bool__annotations__r   rq  r,   r   r.   rm  r   r  r6   r   rl   r   rG   rt   rA  r   r`  r   r  rC  r  r   r	   rk  r   r   r
   ru  r  r  r  r  r   r4   r   r;   r;   r   r<   r    sV   

  D&    r  )r(  r|  c                    s|   t | j}t t|j ksdt dkr6 d nd }d|j }td| d|  fdd|j	 D S )Nrg   r   , z(The model does not have input(s) named: z&, expected a subset of the following: c                    s    i | ]}|j  kr|j |jqS r;   re  rf  r|  r;   r<   rh    s     
  z%get_concrete_args.<locals>.<dictcomp>)
rZ  r[  ra  rr  r\  r4   rj   joinr\   r   )r(  r|  rz  Zformatted_input_namesZformatted_allowed_input_namesr;   r  r<   get_concrete_args  s    r  )r(  c                 C   s2   | j jtkr.dt}td| j j d| d S )Nr  zModel z) is not supported yet, supported models: )r   r   _SUPPORTED_MODELSr  r;  )r(  Zsupported_model_namesr;   r;   r<   check_if_model_is_supported  s
    
r  )r(  r|  disable_check
tracer_clsr2   c                 C   sn   |dkr| j  }t|}t| |}|s0t|  | }|j| |d}tj| |}| j	|_	| j
|_| j|_|S )a  
    Performs symbolic tracing on the model.

    Args:
        model ([`PretrainedModel`]):
            The model to trace.
        input_names (`List[str]`, *optional*):
            The names of the inputs of the traced model. If unset, model.dummy_inputs.keys() are used instead.
        disable_check (`bool`, *optional*, defaults to `False`):
            If `True`, no check is done before trying to trace the model, this is mostly usesul for debugging purposes.
        tracer_cls (`Type[HFTracer]`, *optional*, defaults to `HFTracer`):
            The tracer class to use for instantiating the tracer. If unset, `HFTracer` is used instead.

    Returns:
        `torch.fx.GraphModule`: A GraphModule constructed by recording operations seen while tracing the model.

    Example:

        ```python
        from transformers.utils.fx import symbolic_trace

        traced_model = symbolic_trace(model, input_names=["input_ids", "attention_mask", "token_type_ids"])
        ```
    Nrj  )rc  r4   rs   r  r  ru  rG   r  r   r:  r   r*  rE   )r(  r|  r  r  rb  r   Ztraced_graphZtracedr;   r;   r<   symbolic_trace  s    

r  )N)NNrO   FF)F)NN)NN)N)N)N)rC   )r  r  N)r  r<  r  rZ  r   r   osr  rO  typingr   r   r   r   r   r   r   rG   r	   Ztorch.fxr
   r   r   r   Ztorch.fx._compatibilityr   Ztorch.fx.proxyr   r/   r   r   r   Zmodels.autor   Zmodels.auto.modeling_autor   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   utilsr)   r*   r+   r,   r-   Zpeftr.   Z
get_loggerr   loggerenvironr7   upperrN  r6   r=   Z(_REGULAR_SUPPORTED_MODEL_NAMES_AND_TASKSZ_REGULAR_SUPPORTED_MODELSitemr5   ru   extendZ_SPECIAL_SUPPORTED_MODELSr   sortedrr  r  rN   rQ   rS   rT   rV   rY   rZ   r]   rc   rf   rr   rx   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   Z	EmbeddingZ
functionalZ	embeddingZ	LayerNormZ	GroupNormZLinearZreluZReLUwhereabsr!  rw   catstackrt  mulrt   matmulZbmmZbaddbmmr   repeatZrepeat_interleaveZrollflipZindex_selectgatherZConv1dZConv2dZsqueezeZ	unsqueezer   Zone_hotZMSELossZCrossEntropyLossZBCEWithLogitsLossr   r   r  r   r   r   r
  r  rl   r  r  rk  r  r  r  r  r;   r;   r;   r<   <module>   s   $T	
 )-         

	$



                                           /,	   Y

