o
    h                     @   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: rd dl<m=Z= e>e?Z@ejABddC e7v ZD	ddee deeeEeeE f  deeE fddZFg dZGg ZHeGD ]ZIeJeIeKreHLeFdi eI qeHLeFeI qg dZMeNeOePeHeM ZQdd ZR	dddZSdd ZTdd ZUd d! ZVd"d# ZWd$d% ZXdd&d'ZYd(d) ZZdd*d+d,Z[d-d. Z\d/d0 Z]ddd*d1d2Z^ddd*d3d4Z_d5dd6d7d8Z`dd*d9d:Zad;d< Zbdd*d=d>Zcdd*d?d@Zdd5d5ddAdBdCZed5d5ddAdDdEZfdFdG ZgdHdI ZhdddJdKdLZidd*dMdNZjdOdP ZkdddQdRdSZldTdU ZmddVdWZndXdY ZodZd[ Zpd\d] Zqd^d_ Zrdd`daZsddbdcZtddde Zudfdg Zvdhdi ZwddkdlZxdmdn Zydodp Zzdqdr Z{dsdt Z|i ejj}eRejj~jeSejjeTejjeUejjeVejeWejj~jeYejjeXejeZeje[eje\eje]eje^eje_eje`ejeaejjebi ejecejedejeeejjefejegejjehejeiejenejeoejjepejejejjekejelejjemejjeqejjerejesejjetejeuejjevejewejj~jexejjeyejjezejje{eje|i	Zeeef edu< G dvdw dweZG dxdy dyeZG dzd{ d{eZd|d} Zd~d Zddededeee  fddZG dd deZdejdeeE fddZdefddZddefdedeeeE  dedee def
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)	PeftModelFX_DEBUG_MODE 
model_namesupported_tasksreturnc                 C   s   i dt dtdtdtdtdtdtdtd	td
t	dt
dtdtdtdtdtdtttd}|d u rB| }t|trJ|g}g }|D ]}|| | d }|r_|| qN|S )Ndefault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-classificationctczaudio-classification)zsemantic-segmentation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)r1   r2   task_mappingmodel_class_namestask
class_name rA   K/var/www/html/ai/venv/lib/python3.10/site-packages/transformers/utils/fx.py%_generate_supported_model_class_namesI   sb   	


rC   )*altclipalbertbartbert
blenderbotzblenderbot-smallbloomclipconvnextdebertaz
deberta-v2dinov2
distilbertz
donut-swinelectragpt2gpt_neogptjhubertlayoutlmlxmertm2m_100marianmbartzmegatron-bert
mobilebertmt5nezhaoptpegasusplbartresnetroberta	segformerspeech_to_textspeech_to_text_2swint5trocrvitxglmwav2vec2)CLIPTextModelCLIPTextModelWithProjectionCLIPVisionModelCLIPVisionModelWithProjectionAltCLIPTextModelAltCLIPVisionModelGitVisionModelGPT2DoubleHeadsModelSpeech2Text2DecoderTrOCRDecoderPeftModelForCausalLMPeftModelForSeq2SeqLMc                 C   s*   t jg |j| jjd R d| jjdS Nmeta)devicedtype)torchemptyshapeweightrz   selfinputrA   rA   rB   torch_nn_embedding   s   *r          @Fc                 C   s&   t jg | j|jd R d|jdS rv   )r{   r|   r}   rz   )r   r~   padding_idxmax_norm	norm_typescale_grad_by_freqsparserA   rA   rB   torch_nn_functional_embedding   s   &r   c                 C      |S NrA   r   rA   rA   rB   torch_nn_layernorm      r   c                 C   r   r   rA   r   rA   rA   rB   torch_nn_groupnorm   r   r   c                 C   s    t j|jd d | jf ddS )Nrw   rx   ry   )r{   r|   r}   out_featuresr   rA   rA   rB   torch_nn_linear   s    r   c                 C      | S r   rA   xrA   rA   rB   
torch_relu   r   r   c                 C   r   r   rA   )r   r   rA   rA   rB   torch_nn_relu   r   r   c                 C   s   |st d| S )Nz>Don't support in-place functional.relu for MetaTensor analysis
ValueError)r   inplacerA   rA   rB   torch_nn_functional_relu   s   r   c                 C   s$   | j dd|j dd |j dd S Nrx   r   to)	conditionr   yrA   rA   rB   torch_where   s   $r   outc                C   s   |d urt d| S )Nz2Don't support in-place abs for MetaTensor analysisr   )r   r   rA   rA   rB   	torch_abs   s   r   c                  O   s   t | }d}|dkrd}| d }n|dkr| \}}n| \}}}t|tr(t|}t|tr1t|}t|tr:t|}|d|}|d}tj|| | |ddS )N   r   r   steprz   rx   rz   ry   )lenr9   floatintr;   r{   r|   )argskwargsnr   startendrz   rA   rA   rB   torch_arange   s"   






r   c                  O   sX   t | } t| d tjr| d jtdkrd| d< t|}|dd  tj| i |S )Nr   rx   ry   )listr9   r{   Tensorry   dictpopfull)r   r   kwargs_without_devicerA   rA   rB   
torch_full   s   $r   c                   s    d u r
|d u r
d  d u r|d u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 rA   )r}   ).0trA   rA   rB   
<listcomp>  s    ztorch_cat.<locals>.<listcomp>c                 3   s    | ]}|  V  qd S r   rA   )r   r}   dimrA   rB   	<genexpr>  s    ztorch_cat.<locals>.<genexpr>r   rx   r   )r   r   sumr{   r|   )tensorsr   axisr   shapesr}   concatenated_dimfinal_shaperA   r   rB   	torch_cat  s   "r   c                C   sp   |d u r
|d u r
d}|d u r|d ur|}|dk r"| d   d | }t| d j}||t|  tj|ddS Nr   r   rx   r   )r   r   r}   insertr   r{   r|   )r   r   r   r   r}   rA   rA   rB   torch_stack  s   r   r   )alphar   c          	      C   s   t | tjstj|ddS t |tjstj| ddS t|  | }t| jdg||     }t|jdg||    }g }t|D ]}|	t|| ||  qEtj
|ddS )Nrx   r   r   )r9   r{   r   
empty_likemaxr   r   r}   ranger<   r|   )	r   otherr   r   
max_lengthinput_shapeother_shaper}   irA   rA   rB   	torch_add!  s   r   c                C   s   t | ||dS )Nr   )r   )r   r   r   rA   rA   rB   	torch_mul/     r   c                 C   
   t | |S r   )r   )r   r   rA   rA   rB   torch_tensor_mul3     
r   c          
      C   s  |   }|  }d }|dkr|dkrd }n|dkr(|dkr(| d|df}n|dkr7|dkr7|df}n|dkrF|dkrF| df}npt|   |  }t| j}t|j}|dkrbdg| }|dkrk|d dg||  t| j }dg||  t|j }g }t|D ]}	|t||	 ||	  q|d |d< |d |d< |dkr|d |dkr|d |d u rtj	dddS tj
|d	diS )
Nr   r   r   rw   g        rx   r   ry   )r   sizer   r   r}   r<   r   r   r{   tensorr|   )
r   r   r   d1d2r}   r   shape1shape2r   rA   rA   rB   torch_matmul7  s@   





r   c                C   s:   |d urt d| j\}}}|j\}}}tj|||ddS )Nz2Don't support in-place bmm for MetaTensor analysisrx   r   )r   r}   r{   r|   )r   mat2r   
batch_sizer   m_prA   rA   rB   	torch_bmm[  s
   r   betar   r   c                C   s   |d urt dt||S )Nz6Don't support in-place baddbmm for MetaTensor analysis)r   r   )r   batch1batch2r   r   r   rA   rA   rB   torch_baddbmmc  s   
r   c                C   s   t | |||||dS )Nr   )r   )r   r   r   r   r   r   rA   rA   rB   torch_tensor_baddbmmi  s   r   c                 G   s&   dd |D }t j| g|R  dS )Nc                 s   s    | ]
}t j|d dV  qdS )cpur   N)r{   r   )r   operandrA   rA   rB   r   o  s    ztorch_einsum.<locals>.<genexpr>rx   )r{   einsumr   )equationoperandsconcrete_operandsrA   rA   rB   torch_einsumm  s   r   c                 G   s:   t | j}t|D ]\}}||  |9  < q	tj|ddS r   )r   r}   	enumerater{   r|   )r   sizesr}   r   r   rA   rA   rB   torch_tensor_repeats  s   
r   )r   output_sizec                 G   s   t |}|dkr|d ur|n|d  g}nBt|d j}| d u r1|dkr*|d } nt|g}d} |d }t|tsAt|dkrL||   t|9  < n|d urR|n| || < tj|ddiS )Nr   r   r   ry   rx   )	r   r   r   r}   r9   r   r{   numelr|   )r   r   r   num_argsr}   repeatsrA   rA   rB   torch_repeat_interleavez  s   

r   c                C   s&   t | j}t|||< tj|ddiS Nry   rx   )r   r}   r   r{   r|   )r   r   indexr   r}   rA   rA   rB   torch_index_select  s   
r   c                 C      t | ||S r   )r   r   r   r   rA   rA   rB   torch_tensor_index_select     r   )sparse_gradr   c                C   s(   t | j}|j| ||< tj|ddiS r   )r   r}   r{   r|   )r   r   r   r   r   r}   rA   rA   rB   torch_gather  s   
r  c                 C   r   r   )r  r   rA   rA   rB   torch_tensor_gather  r   r  c                 C   r   r   rA   )r   shiftsdimsrA   rA   rB   
torch_roll  r   r  c                 C   r   r   rA   )r   r  rA   rA   rB   
torch_flip  r   r  c                 C   r   r   rA   )r   r  rA   rA   rB   torch_tensor_flip  r   r  c                 C   s   |j d }d }| j}|dkrd}|dkrt|j }|d u rFt|j }t|d|d   | jd | jd d   d | jd  d }||d< | j|d< t	j
|d	d
S )Nrw   validr   r   samer   r   r   r   rx   r   r}   paddingr   mathfloordilationkernel_sizestrideout_channelsr{   r|   )r   r   l_inr}   r  l_outrA   rA   rB   torch_nn_conv1d  s   


8
r  c                 C   s   |j dd  \}}d }| j}|dkrd}|dkrt|j }|d u rnt|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   r   rx   r   r  )r   r   h_inw_inr}   r  h_outw_outrA   rA   rB   torch_nn_conv2d  s$   

88
r  c                 C   sr   t | j}|d ur|dk r|  | }|| dkr|| ng }|D ]}|dkr*q#|| q#|}tj|ddS r   )r   r}   r   r   r<   r{   r|   )r   r   r}   	new_shape	dim_valuerA   rA   rB   torch_squeeze  s   

r  c                 C   r   r   )r  r   r   rA   rA   rB   torch_tensor_squeeze  r   r   c                 C   s<   t | j}|dk r|  d | }||d tj|ddS r   )r   r}   r   r   r{   r|   )r   r   r}   rA   rA   rB   torch_unsqueeze  s
   
r!  c                 C   r   r   )r!  r  rA   rA   rB   torch_tensor_unsqueeze  r   r"  c                 K   sD   t jt j| ddfi |}t|t jr|dS tt|dd S )Nr   r   rx   c                 S   s
   |  dS )Nrx   r   r   rA   rA   rB   <lambda>  s   
 z*torch_unique_consecutive.<locals>.<lambda>)r{   unique_consecutive
zeros_liker9   r   r   tuplemap)r   r   outputrA   rA   rB   torch_unique_consecutive  s   
r)  rw   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 analysisrx   r   )r   r   r}   r{   r|   )r   num_classesr}   rA   rA   rB   torch_nn_functional_one_hot  s   r+  c                 C   $   | j dkr	|j}nd}tj|ddS Nnone)r   rx   r   	reductionr}   r{   r|   r   r   targetr}   rA   rA   rB   torch_nn_mseloss     
r3  c                 C   r,  r-  r/  r1  rA   rA   rB   torch_nn_crossentropyloss  r4  r5  c                 C   r,  r-  r/  r1  rA   rA   rB   torch_nn_bcewithlogitsloss  r4  r6  c                 C   s^   dd }t | tjr)t |trtt||}n||}ttj| dd|dS t| |S )Nc                 S   sH   t | tjr"tj| dd}|jtjtjtjtjfv r |	tj
}|S | S )Nr   r   )r9   r{   r   	ones_likerz   float16float32float64int32r   int64)r   concreterA   rA   rB   to_concrete  s   z%operator_getitem.<locals>.to_concreter   r   rx   )	r9   r{   r   r&  r'  operatorgetitemr   r   )abr>  rA   rA   rB   operator_getitem  s   
rC  _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 r   )	_metadata)r   metadatarA   rA   rB   install_metadatad  r   zHFProxy.install_metadatac                 C   s   | j dd| fi S )Ncall_methodr   )tracercreate_proxyr   rA   rA   rB   r}   g  s   zHFProxy.shapec                 C   s
   t | dS )Nry   )MetaDeviceAttributerL  rA   rA   rB   ry   k  s   
zHFProxy.devicec                    s(   t | dr| jd urt| jS t  S NrF  )hasattrrF  r   super__len__rL  	__class__rA   rB   rQ  q  s   

zHFProxy.__len__c                    s$   t | dr| jd ur| jS t  S rN  )rO  rF  rP  __bool__rL  rR  rA   rB   rT  v  s   
zHFProxy.__bool__c                 C   s   |dkr	|  |S t| |S rN  )__getattribute__HFAttribute)r   krA   rA   rB   __getattr__{  s   

zHFProxy.__getattr__c                 C   s   | j dtj| ||fi S Ncall_function)rJ  rK  r?  setitem)r   indicesvaluesrA   rA   rB   __setitem__  s   zHFProxy.__setitem__c                    s*   t | dr| jd ur|| jv S t |S rN  )rO  rF  rP  __contains__)r   keyrR  rA   rB   r_    s   
zHFProxy.__contains__)__name__
__module____qualname____doc__rH  propertyr}   ry   rQ  rT  rX  r^  r_  __classcell__rA   rA   rR  rB   rE  _  s    

rE  c                   @   s.   e Zd ZdefddZedd Zdd ZdS )	rV  attrc                 C   sB   || _ || _|j| _d | _t| j dr| t| j j| d S d S rN  )rootrg  rJ  _noderO  rH  getattrrF  )r   rh  rg  rA   rA   rB   __init__  s   zHFAttribute.__init__c                 C   s0   | j d u r| jdtj| j| jfi j| _ | j S rY  )ri  rJ  rK  builtinsrj  rh  rg  noderL  rA   rA   rB   rm    s   
 zHFAttribute.nodec                 O   s   | j d| j| jf| |S )NrI  )rJ  rK  rg  rh  )r   r   r   rA   rA   rB   __call__  s   zHFAttribute.__call__N)ra  rb  rc  r:   rk  re  rm  rn  rA   rA   rA   rB   rV    s
    	
rV  c                   @   s   e Zd ZdS )rM  N)ra  rb  rc  rA   rA   rA   rB   rM    s    rM  c                 C   sH   t | trdS t | tjjr"t | trt| dstd|  | jS | S )z\Returns the underlying metadata for HFProxies, and behaves like the identity for the others.rx   rF  zNo metadata was found for )	r9   rM  r{   fxr   rE  rO  RuntimeErrorrF  vrA   rA   rB   _proxies_to_metas  s   
rs  c                    s   t   fdd}| fS )Nc                     sX   d   fdd}t jj| | t jj||  d ur% jd| |S | i |S )Nc                    s   t | tr	|  d S d S r   r9   r   rq  proxyrA   rB   check_has_proxy  s   
zB_gen_constructor_wrapper.<locals>.wrapper.<locals>.check_has_proxyrZ  )r{   ro  rm  map_aggregaterJ  rK  )r   r   rw  r2  ru  rB   wrapper  s   z)_gen_constructor_wrapper.<locals>.wrapper)	functoolswraps)r2  rz  rA   ry  rB   _gen_constructor_wrapper  s   r}  
      lowhighforbidden_valuesc                 C   s8   |d u rg }t | |}||v rt | |}||v s|S r   )randomrandint)r  r  r  valuerA   rA   rB   _generate_random_int  s   r  c                       s  e Zd ZU dZdZeed< dZeed< g dZe	 se
fne
efZefdf fdd	Zd	e
d
edee deeejf fddZd4 fdd	Zdd Zdededeeef fddZ fddZdd Z			d5deejjedef f de eeef  d e eeef  d!ede!f
 fd"d#Z"d$ejdefd%d&Z#d$ejdefd'd(Z$d$ejdef fd)d*Z%d+ejjd,edef fd-d.Z&e'dd/d0d1def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zerosonesr   	full_likeeyer|   r   clampfinforA   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.)rP  rk  r-   ImportErrorr+   r*   )r   r  r  rR  rA   rB   rk    s   
zHFTracer.__init__model
input_namer}   r3   c                 C   sB  t |d|jj}|j}i }|dv r
|d }|g tttttttttt	v r;t
j|t
j|d|d< |S |g ttttdv rat
j|t
j|d|d< t
j|t
j|d|d< |S |ttv rt|jd	rs|jjd
u rwtd|jjdkr||jjf}t
j}	n'|jjdkr|f}t
j}	n|jjdkr||jjf}t
j}	n
td|jj dt
j||	|d|d< |S |g ttttttttttttdddv rt
j|t
j|d|d< |S |g ttv rt
j|t
j|d|d< |S td| d| dd|v rf|d }t |jdd
}
|
d
u r?t|jdr,|jjj}
nt|jdr9|jj j}
nt! t! f}
t |jdd}t"|
t#j$j%sR|
|
f}
|
\}}t
j||||t
j|d||< |S d|v r~t
jg |dR t
j&|d||< |S d|v rt
jg ||jj'R t
j&|d||< |S d |v rt
j||jj(g t
j&|d||< |S d!|v rt
j||jj)g t
j&|d||< |S d"|v rt
j|t
j&|d||< |S d#|v r|\}}t!d$d%d&}t
j||t
j&|d||< |S d'|v s d(|v rt
j|t
j|d||< |S ||jj*g }t
j|t
j&|d||< |S ))z4Generates dummy input for model inference recording.class_for_deserialization)labelsstart_positionsend_positionsr   r   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".
regressionsingle_label_classificationmulti_label_classificationzExpected model.config.problem_type to be either: "regression", "single_label_classification", or "multi_label_classification", but "z" was provided.rq   rt   ru   z!Generating the dummy input named z for z is not supported yet.pixel_values
image_sizevision_configencodernum_channels   bbox   input_featuresvisual_feats
visual_posinputsinput_valuesi'  i N  r  r  maskids)+rj  rS  ra  ry   r   r   r   r   r   r   r{   r  longr!   r   r$   rO  configr  r   
num_labelsr9  r    r&   r   r   r#   r"   r   NotImplementedErrorr  r  r  r  r9   collectionsabcIterabler   input_feat_per_channelvisual_feat_dimvisual_pos_dimhidden_size)r   r  r  r}   model_class_namery   inputs_dictr   labels_shapelabels_dtyper  r  heightwidthr   
seq_lengthshape_with_hidden_sizerA   rA   rB   _generate_dummy_input  s  
jcL	?=


(
"$
"





zHFTracer._generate_dummy_inputNc                    sP  t  |||||||}|dkr|| jv r|| j|  |S || jv r,d|v r,d|d< ztjj|t	}	tjj|t	}
|dkr[t
||}||	i |
}t|tjrZ|jdd}n|dkrut|	d j|}t
||}||	i |
}nv|dkrt| d	st|  d
d| _z)| j|}t|}|t
v rt
| |g|	R i |
}n| j|	i |
}W d| _n9d| _w |dkrd| _z&| j}|d}|D ]}t||}qt|tjr|jdd}n|}W d| _nd| _w |W S t|tstd|| W |S  ty' } ztrtd| d| d|  W Y d }~|S W Y d }~|S d }~ww )Nplaceholderry   rx   rZ  r   rI  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: )rP  rK  	meta_argsrH  orig_fnsr{   ro  rm  rx  rs  rD  r;   r9   r   r   rj  rS  rO  AttributeError_disable_module_getattrrh  get_submoduletyper  splitr   r   	Exception_IS_IN_DEBUG_MODEwarningswarn)r   kindr2  r   r   name	type_exprproxy_factory_fnrv
args_metaskwargs_metasmeta_targetmeta_outmethodmodmod_typeattr_itratomsatomerR  rA   rB   rK  m  sr   



&
zHFTracer.create_proxyc                    s|   t  ddr|S  fdd}t|tjjr$|| j |}|d ur$|S  jr<t|tjr<|| j	 |}|d ur<|S |S )Nr  Fc                    s   |D ]<\} |u r>|vr8i }dt jjv r(jsd n fdd|d< jddi fi |}||< |   S qd S )Nr  c                    s   t |  S r   r   )rm  )attr_valr   r   rA   rB   r#    s    zLHFTracer._module_getattr.<locals>.maybe_get_proxy_for_attr.<locals>.<lambda>r  rA   )inspect	signaturerK  
parametersparam_shapes_constant)r  collection_to_searchparameter_proxy_cacher   r   	val_proxyrL  )r  r   rB   maybe_get_proxy_for_attr  s   z:HFTracer._module_getattr.<locals>.maybe_get_proxy_for_attr)
rj  r9   r{   r	   	Parameterrh  named_parametersr  r   named_buffers)r   rg  r  r  r  maybe_parameter_proxymaybe_buffer_proxyrA   rL  rB   _module_getattr  s    zHFTracer._module_getattrrg  r  r  c                 C   s   |  |||S r   )r  )r   rg  r  r  rA   rA   rB   rj    r   zHFTracer.getattrc                    s   || _ t ||||S r   )r  rP  r  )r   r   forwardr   r   rR  rA   rB   r    s   zHFTracer.call_modulec                 C   s
   t || S r   )rE  )r   rm  rA   rA   rB   rv    r   zHFTracer.proxyrh  .concrete_argsdummy_inputs6complete_concrete_args_with_inputs_not_in_dummy_inputsc                    s  t t|tjjr|jn|} du ri  durI|rI|j D ]}|j	v r(q |j
t jju r8td|j	 dq   fdd|j D  |j    }t }t }	||	g}
|jjttv rptddd}|
d	| durxtni }|D ]'}||v rq|t|| jst|jd
r|| |||
 q|td| ddd | D }|j D ]}|jt jjkr|j	|vri |d|j	 < q|| _ dd | j!D | _"t# | _$| j" D ]\}\}}t%t|| | j$&| qzt' j(| d| _)W | j" D ]\}\}}t%t|| qn| j" D ]\}\}}t%t|| qw | j)j*D ]O}|j+dkrm|j,|v r>d|_-tj.|_n/|g}t/0 }|r]|1d}d||< |t2|j3 7 }|sHt4| D ]	}| j)5| qc|j+dkrvd|_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 r  c                    s*   i | ]}|j vr|j  vr|j |jqS rA   r  r4   r   r   r  r  rA   rB   
<dictcomp>  s
    z"HFTracer.trace.<locals>.<dictcomp>r      r  r   )_deserialize_graph_module_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 )rx   )r9   r{   r   r   )r   r  input_rA   rA   rB   r  /  s    z**c                 S   s   i | ]
}|t tt|qS rA   )r}  rj  r{   )r   r2  rA   rA   rB   r  7  s    r  r  rA   r   r(  )6r  r  r9   r{   r	   Moduler  r  r]  r  r4   r  r|   r   updater8   r  rS  ra  r   r   r   r   supported_archsr  rc  
startswithr  rp  itemsr  VAR_KEYWORDr  _TORCH_METHODS_TO_PATCHpatched_torch_methodssetr  setattraddrP  tracegraphnodesopr2  r   r   r  OrderedDictr   r   usersreversed
erase_node)r   rh  r  r  r  sigparaminput_namesr   sequence_lengthr}   num_choicesr  r  concrete_metasr  rz  origr   rm  to_visit	to_deleter   userrR  r  rB   r
    s    



zHFTracer.tracer  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 r   rt  )r   rg  rA   rA   rB   r   h  s    zKHFTracer._stateless_mod_instanciation_depends_on_proxies.<locals>.<genexpr>)any__dict__r]  )r   r  rA   rA   rB   /_stateless_mod_instanciation_depends_on_proxiesc  s   z8HFTracer._stateless_mod_instanciation_depends_on_proxiesc                 C   s   |  |rdS d}|jj }| d| }d}t| j|r:t| j||u r)d}n| d| }|d7 }t| j|s|sC| j|| |S )zb
        Helper method which tries to insert a module that was not declared as submodule.
        r0   r   r   FTr   )r  rS  ra  lowerrO  rh  rj  
add_module)r   r  idxmod_namepathalready_insertedrA   rA   rB   _insert_module_as_submodulej  s    
z$HFTracer._insert_module_as_submodulec              
      st   zt  |W S  ty9 } z&| jr3tt| dkr3tt| dkr3| |}|W  Y d}~S |d}~ww )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)	rP  path_of_module	NameErrorr  r   r   r  buffersr%  )r   r  r  r#  rR  rA   rB   r&    s   	.
zHFTracer.path_of_moduler   module_qualified_namec                    s   |  | ot ||S r   )r  rP  is_leaf_module)r   r   r)  rR  rA   rB   r*    s   zHFTracer.is_leaf_module)is_backward_compatibleobjr   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.
        r8   z**kwargs)rV  rm  r2  rF  )r   r,  	attributerA   rA   rB   r8     s   zHFTracer.keys)NNN)NNT)*ra  rb  rc  rd  r  bool__annotations__r  r  r,   r   r.   r  r  rk  r:   r   r   r   r{   r   r  rK  r  r   rj  r  rv  r   r	   r  r   r   r
   r
  r  r%  r&  r*  r   r8   rf  rA   rA   rR  rB   r    sT   
 	
D& r  r  r  c                    s|   t | j}t t|j ks2t dkr d nd }d|j }td| d|  fdd|j	 D S )Nr   r   , z(The model does not have input(s) named: z&, expected a subset of the following: c                    s    i | ]}|j  vr|j |jqS rA   r  r  r  rA   rB   r    s     z%get_concrete_args.<locals>.<dictcomp>)
r  r  r  r  r  r8   r   joinr   r]  )r  r  r  formatted_input_namesformatted_allowed_input_namesrA   r1  rB   get_concrete_args  s   r5  c                 C   s2   | j jtvrdt}td| j j d| d S )Nr0  zModel z) is not supported yet, supported models: )rS  ra  _SUPPORTED_MODELSr2  r  )r  supported_model_namesrA   rA   rB   check_if_model_is_supported  s   
r8  disable_check
tracer_clsc                 C   sn   |du r	| j  }t|}t| |}|st|  | }|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"])
        ```
    Nr  )r  r8   r   r5  r8  r
  r{   ro  r   r  rS  r  ry   )r  r  r9  r:  r  rJ  traced_graphtracedrA   rA   rB   symbolic_trace  s   

r=  r   rA   )NNr   FF)F)NN)rw   )r~  r  N)rl  r  r{  r  r  r?  osr  r  typingr   r   r   r   r   r   r   r{   r	   torch.fxr
   r   r   r   torch.fx._compatibilityr   torch.fx.proxyr   r0   r   r   r   models.autor   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-   peftr.   
get_loggerra  loggerenvironr;   upperr  r:   rC   (_REGULAR_SUPPORTED_MODEL_NAMES_AND_TASKS_REGULAR_SUPPORTED_MODELSitemr9   r   extend_SPECIAL_SUPPORTED_MODELSr&  sortedr  r6  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  r  r  r  r   r!  r"  r)  r+  r3  r5  r6  rC  	Embedding
functional	embedding	LayerNorm	GroupNormLinearreluReLUwhereabsr  r   catstackr	  mulr   matmulbmmbaddbmmr   repeatrepeat_interleaverollflipindex_selectgatherConv1dConv2dsqueeze	unsqueezer$  one_hotMSELossCrossEntropyLossBCEWithLogitsLossr@  rD  r/  rE  rV  rM  rs  r}  r   r  r  r  r5  r8  r.  r=  rA   rA   rA   rB   <module>   sx  
$T	

(.


	$





	
 !"#
/, 	   Y

