o
    @"g                     @   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	Z	d dl
Z
d dlmZmZmZmZmZmZmZmZmZ d dlZd dlm  mZ d dlmZ d dlmZmZmZmZm 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+m,Z,m-Z- ddl.m/Z/m0Z0 ddl1m2Z2 ddl3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z=m>Z>m?Z?m@Z@mAZAmBZBmCZCmDZDmEZEmFZFmGZG ddlHmIZImJZJmKZKmLZLmMZM eL rd dlNmOZO e(PeQZRejSTddU eIv ZV	ddee/ deeeWeeW f  deeW fddZXg dZYddgZZg Z[eYD ]Z\e]e\e^r"e[_eXdi e\ qe[_eXe\ qg dZ`eaebece[e` Zddaedd Zf	dd d!Zgd"d# Zhd$d% Zid&d' Zjd(d) Zkd*d+ Zldd,d-Zmd.d/ Zndd0d1d2Zod3d4 Zpd5d6 Zqddd0d7d8Zrddd0d9d:Zsddd;d<d=Ztdd0d>d?Zud@dA Zvdd0dBdCZwdd0dDdEZxddddFdGdHZyddddFdIdJZzdKdL Z{dMdN Z|dddOdPdQZ}dd0dRdSZ~dTdU ZdddVdWdXZdYdZ Zdd[d\Zd]d^ Zd_d` Zdadb Zdcdd ZddedfZddgdhZdidj Zdkdl Zdmdn ZddpdqZ	ddsdtZdudv Zdwdx Zdydz Zd{d| Zi ejjefejjjegejjehejjeiejjejejekejjjemejjelejenejeoejepejeqejerejesejetejeuejjevi ejewejexejeyejjezeje{ejje|eje}ejeejeejjeeje~ejjeejeejjeejjeejjeejeejjeejeejjeejeejjjeejjeejjeejjeejei	Zeeef ed}< eeejjj< G d~d deZG dd deZG dd deZG dd deZ	ddedeed ed ed f deeegef  defddZG dd deZdedeeef fddZdd Zdee* deegef fddZede*fi ee*dZede+fi ee+dZede,fi ee,dZede-fi ee-dZddededeee  fddZG dd de ZdejdeeW fddZdddZdddZddefdddeeeW  dedee def
ddZdS )    N)	AnyCallableDictListLiteralOptionalTupleTypeUnion)nn)GraphGraphModuleNodeProxyTracer)compatibility)is_fx_tracingParameterProxy   )logging)CacheDynamicCache	SinkCacheStaticCache)PretrainedConfigPreTrainedModel)
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_IMAGE_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td}|d u rC| }t|trK|g}g }|D ]}|| | d }|r`|| qO|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backbonezimage-feature-extraction)r1   r)   r(   r&   r    r,   r.   r'   r"   r*   r-   r/   r%   r#   r0   r!   r   r+   r   r$   keys
isinstancestrgetappend)r;   r<   task_mappingmodel_class_namestask
class_name rK   W/mnt/skqttb/ctump_chatbot/chatbot/lib/python3.10/site-packages/transformers/utils/fx.py%_generate_supported_model_class_namesP   sd   	


rM   )2altclipalbertbartbert
blenderbotzblenderbot-smallbloomclipconvnextdebertaz
deberta-v2dinov2
distilbertz
donut-swinelectragpt2gpt_neogptjhierahubertijepalayoutlmllamacoherelxmertm2m_100marianmbartmegatron-bertmistralmixtral
mobilebertmt5nezhaoptpegasusplbartqwen2	qwen2_moeresnetroberta	segformerspeech_to_textspeech_to_text_2swint5trocrvitxglmwav2vec2ra   rm   )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weightr   selfinputrK   rK   rL   torch_nn_embedding   s   *r          @Fc                 C   s&   t jg | j|jd R d|jdS r   )r   r   r   r   )r   r   padding_idxmax_norm	norm_typescale_grad_by_freqsparserK   rK   rL   torch_nn_functional_embedding   s   &r   c                 C      |S NrK   r   rK   rK   rL   torch_nn_layernorm      r   c                 C   r   r   rK   r   rK   rK   rL   torch_nn_groupnorm   r   r   c                 C   s    t j|jd d | jf ddS )Nr   r   r   )r   r   r   out_featuresr   rK   rK   rL   torch_nn_linear   s    r   c                 C      | S r   rK   xrK   rK   rL   
torch_relu   r   r   c                 C   r   r   rK   )r   r   rK   rK   rL   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   inplacerK   rK   rL   torch_nn_functional_relu   s   r   c                 C   s$   | j dd|j dd |j dd S Nr   r   to)	conditionr   yrK   rK   rL   torch_where   s   $r   outc                C   s   |d urt d| S )Nz2Don't support in-place abs for MetaTensor analysisr   )r   r   rK   rK   rL   	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 )Nr2   r   r   stepr   r   r   r   )lenrC   floatintrE   r   r   )argskwargsnr   startendr   rK   rK   rL   torch_arange   s"   






r   c                  O   sR   t | } t| dkrd| d< nd|d< t|}|dd  tj| i |ddiS )Nr2   
fill_valuer   r   )listr   dictpopr   full)r   r   kwargs_without_devicerK   rK   rL   
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 rK   )r   ).0trK   rK   rL   
<listcomp>%  s    ztorch_cat.<locals>.<listcomp>c                 3   s    | ]}|  V  qd S r   rK   )r   r   dimrK   rL   	<genexpr>'  s    ztorch_cat.<locals>.<genexpr>r2   r   r   )r   r   sumr   r   )tensorsr   axisr   shapesr   concatenated_dimfinal_shaperK   r   rL   	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   r2   r   r   )r   r   r   insertr   r   r   )r   r   r   r   r   rK   rK   rL   torch_stack,  s   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 )Nr   r   r2   )rC   r   Tensor
empty_likemaxr   r   r   rangerF   r   )	r   otherr   r   
max_lengthinput_shapeother_shaper   irK   rK   rL   	torch_add8  s   r   c                C   s   t | ||dS )Nr   )r   )r   r   r   rK   rK   rL   	torch_mulF     r   c                 C   
   t | |S r   )r   )r   r   rK   rK   rL   torch_tensor_mulJ     
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 )
Nr2   r   r   r           r   r   r   )r   sizer   r   r   rF   r   r   r   tensorr   )
r   r   r   d1d2r   r   shape1shape2r   rK   rK   rL   torch_matmulN  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 analysisr   r   )r   r   r   r   )r   mat2r   
batch_sizer   m_prK   rK   rL   	torch_bmmr  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   rK   rK   rL   torch_baddbmmz  s   
r   c                C   s   t | |||||dS )Nr   )r   )r   r   r   r   r   r   rK   rK   rL   torch_tensor_baddbmm  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   operandrK   rK   rL   r     s    ztorch_einsum.<locals>.<genexpr>r   )r   einsumr   )equationoperandsconcrete_operandsrK   rK   rL   torch_einsum  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   rK   rK   rL   torch_tensor_repeat  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 )Nr2   r   r   r   r   )	r   r   r   r   rC   r   r   numelr   )r   r  r   num_argsr   repeatsrK   rK   rL   torch_repeat_interleave  s   

r  c                C   s&   t | j}t|||< tj|ddiS Nr   r   )r   r   r   r   r   )r   r   indexr   r   rK   rK   rL   torch_index_select  s   
r  c                 C      t | ||S r   )r  r   r   r  rK   rK   rL   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   rK   rK   rL   torch_gather  s   
r  c                 C   r  r   )r  r  rK   rK   rL   torch_tensor_gather  r  r  c                 C   r   r   rK   )r   shiftsdimsrK   rK   rL   
torch_roll  r   r  c                 C   r   r   rK   )r   r  rK   rK   rL   
torch_flip  r   r  c                 C   r   r   rK   )r   r  rK   rK   rL   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 )Nr   validr   r   samer   r   r2   r   r   r   r   paddingr   mathfloordilationkernel_sizestrideout_channelsr   r   )r   r   l_inr   r   l_outrK   rK   rL   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   r2   r   r   r  )r   r   h_inw_inr   r   h_outw_outrK   rK   rL   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   rF   r   r   )r   r   r   	new_shape	dim_valuerK   rK   rL   torch_squeeze  s   

r2  c                 C   r   r   )r2  r   r   rK   rK   rL   torch_tensor_squeeze  r   r4  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   rK   rK   rL   torch_unsqueeze  s
   
r5  c                 C   r   r   )r5  r3  rK   rK   rL   torch_tensor_unsqueeze	  r   r6  c                 K   sD   t jt j| ddfi |}t|t jr|dS tt|dd S )Nr   r   r   c                 S   s
   |  dS Nr   r   r   rK   rK   rL   <lambda>  s   
 z*torch_unique_consecutive.<locals>.<lambda>)r   unique_consecutive
zeros_likerC   r   r   tuplemap)r   r   outputrK   rK   rL   torch_unique_consecutive  s   
r>  r   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 analysisr   r   )r   r   r   r   r   )r   num_classesr   rK   rK   rL   torch_nn_functional_one_hot  s   r@  r   c           	      C   s:   | j d }|j d }tjg | j d d ||R ddS )Nr   r   r   r   )r   r   r   )	querykeyvalue	attn_mask	dropout_p	is_causalscaletarget_lengthhead_dimrK   rK   rL   0torch_nn_functional_scaled_dot_product_attention  s   

&rJ  c                 C   $   | j dkr	|j}nd}tj|ddS Nnone)r2   r   r   	reductionr   r   r   r   r   targetr   rK   rK   rL   torch_nn_mseloss$     
rR  c                 C   rK  rL  rN  rP  rK   rK   rL   torch_nn_crossentropyloss,  rS  rT  c                 C   rK  rL  rN  rP  rK   rK   rL   torch_nn_bcewithlogitsloss4  rS  rU  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   )rC   r   r   	ones_liker   float16float32float64int32r   int64)r   concreterK   rK   rL   to_concrete=  s   z%operator_getitem.<locals>.to_concreter   r   r   )	rC   r   r   r;  r<  operatorgetitemr   r   )abr]  rK   rK   rL   operator_getitem<  s   
rb  _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   
   || _ d S r   )	_metadata)r   metadatarK   rK   rL   install_metadata  r   zHFProxy.install_metadatac                 C   s   | j dd| fi S )Ncall_methodr   )tracercreate_proxyr   rK   rK   rL   r     s   zHFProxy.shapec                 C   s
   t | dS )Nr   )MetaDeviceAttributerl  rK   rK   rL   r     s   
zHFProxy.devicec                    s(   t | dr| jd urt| jS t  S Nrf  )hasattrrf  r   super__len__rl  	__class__rK   rL   rq    s   

zHFProxy.__len__c                    s$   t | dr| jd ur| jS t  S rn  )ro  rf  rp  __bool__rl  rr  rK   rL   rt    s   
zHFProxy.__bool__c                 C   s   |dkr	|  |S t| |S rn  )__getattribute__HFAttribute)r   krK   rK   rL   __getattr__  s   

zHFProxy.__getattr__c                 C   s   | j dtj| ||fi S Ncall_function)rj  rk  r^  setitem)r   indicesvaluesrK   rK   rL   __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   rB  rr  rK   rL   r    s   
zHFProxy.__contains__)__name__
__module____qualname____doc__rh  propertyr   r   rq  rt  rx  r~  r  __classcell__rK   rK   rr  rL   rd    s    

rd  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  )rootr  rj  _nodero  rh  getattrrf  )r   r  r  rK   rK   rL   __init__  s   zHFAttribute.__init__c                 C   s0   | j d u r| jdtj| j| jfi j| _ | j S ry  )r  rj  rk  builtinsr  r  r  noderl  rK   rK   rL   r    s   
 zHFAttribute.nodec                 O   s   | j d| j| jf| |S )Nri  )rj  rk  r  r  )r   r   r   rK   rK   rL   __call__  s   zHFAttribute.__call__N)r  r  r  rD   r  r  r  r  rK   rK   rK   rL   rv    s
    	
rv  c                   @   s   e Zd ZdS )rm  N)r  r  r  rK   rK   rK   rL   rm    s    rm  c                   @   s.   e Zd ZdZdee fddZedd ZdS )HFCacheProxyzP
    Proxy that represents an instance of `transformers.cache_utils.Cache`.
    orig_cache_clsc                 C   re  r   )_orig_cache_cls)r   r  rK   rK   rL   install_orig_cache_cls  r   z#HFCacheProxy.install_orig_cache_clsc                 C   s    t | ds	td| jj| j S )Nr  z?The original Cache class must be installed to the HFCacheProxy.)ro  RuntimeErrorrj  _CLASSES_TO_PATCHr  rl  rK   rK   rL   rs    s   
zHFCacheProxy.__class__N)	r  r  r  r  r	   r   r  r  rs  rK   rK   rK   rL   r    s
    r  functionop_typerz  ri  get_attrproxy_factory_fnc                    s   t   fdd}|S )Nc                     s   t  s
| i |S g   fdd}tjj| | tjj|| t dkrV d j}dkr4}ndkr<j}ndkrDj}ntd d|j	|| |d	S | i |S )
Nc                    s   t | tr |  d S d S r   )rC   r   rF   )r`  found_proxiesrK   rL   check_proxy  s   
z4create_wrapper.<locals>.wrapper.<locals>.check_proxyr   rz  ri  r  zop_type z not supported.r  )
r   r   fxr  map_aggregater   rj  r  r   rk  )r   r   r  rj  rQ  r  r  r  r  rL   wrapper  s"   
zcreate_wrapper.<locals>.wrapper)	functoolswraps)r  r  r  r  rK   r  rL   create_wrapper  s   r  c                       sT   e Zd ZdZ	d
dedeedf deeef de	e
egef  f fdd	Z  ZS )HFProxyableClassMetazW
    Metaclass that creates a class with its main methods wrapped to be proxyable.
    Nnamebases.attrsr  c              	      s   t  | |||} t| D ]=}t| |d }|d u rq|dkr!d}n|dr)d }nt|r1d}n
t|r9d}nd }|d urJt| |t	|||d q| S )Nr  rz  __ri  r  )
rp  __new__dirr  
startswithinspectismethod
isfunctionsetattrr  )clsr  r  r  r  	attr_namer  r  rr  rK   rL   r     s$   


zHFProxyableClassMeta.__new__r   )r  r  r  r  rD   r   r	   r   r   r   r   r   r   r  r  rK   rK   rr  rL   r    s    	

r  rQ  c                 C   s   t | d}|| fS )zo
    Wraps `target` to be proxyable. Used for tensor creators like `torch.ones`, `torch.arange` and so on.
    rz  )r  )rQ  r  rK   rK   rL   gen_constructor_wrapper  s   
r  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.r   rf  zNo metadata was found for )	rC   rm  r   r  r   rd  ro  r  rf  )vrK   rK   rL   _proxies_to_metas#  s   
r  r  c                    s   dt dtf fdd}|S )Nr   r=   c                    s*   t tts	tdt| t}|  |S )NzJCannot create HFCacheProxy because there is no HFTracer currently tracing.)rC   _CURRENT_TRACERHFTracerr  r  r  )r   cache_proxyr  rK   rL   cache_proxy_factory_fn/  s
   


z=create_cache_proxy_factory_fn.<locals>.cache_proxy_factory_fn)r   r  )r  r  rK   r  rL   create_cache_proxy_factory_fn.  s   r  ProxyableCacher  ProxyableDynamicCacheProxyableSinkCacheProxyableStaticCache
      lowhighforbidden_valuesc                 C   s8   |d u rg }t | |}||v rt | |}||v s|S r   )randomrandint)r  r  r  rC  rK   rK   rL   _generate_random_intR  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	e
eeeeeeiZe s'efneefZefdf fdd	Zd	d
dedee dee deeejf f
ddZd8 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$e%j&de'ej(j)e*d e!f f fd!d"Z+			d9d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,Z0d(e(j)def fd-d.Z1d/ej(j)d0edef fd1d2Z2e3dd3d4d5de!fd6d7Z4  Z5S ):r  z
    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finfotrilrK   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  r  r7   ImportErrorr5   r4   )r   r  r  rr  rK   rL   r  z  s   
zHFTracer.__init__modelr   
input_namer   input_namesr=   c                    s  t |d|jj}|ji }d}|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 rct
j|t
jd|d< t
j|t
jd|d	< |S |ttv rt|jd
ru|jjdu ryt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 rt
j|t
jd|d< |S td| d| dd|v ri|d }t |jdd}|du rBt|jdr/|jjj}nt|jdr<|jj j}nt! t! f}t |jdd}t"|t#j$j%sU||f}|\}}t
j||||t
jd||< |S d|v rt
jg |dR t
j&d||< |S d |v rt
jg ||jj'R t
j&d||< |S d!|v r|d }t |jd"ddur|jj(d#kr|jj)}n|jj*}t+|dkr||d$ |d% |f}n||d$ |f}t
j|t
j&d||< |S d&|v rt
j||jj,g t
j&d||< |S d'|v rt
j||jj-g t
j&d||< |S d(|v r%t
j|t
j&d||< |S d)|v rB|\}}t!d*d+d,}t
j||t
j&d||< |S d-|v rfd.|v rW|d |d$ | g}n|}t
j|t
jd||< |S d/|v rxt
j|t
jd||< |S d.|v r|jj(t.vrtd0|jj( d1|jj/}|jj*|jj/ }|d |||f t0 fd2d3t1|jj2D }|||< |S ||jj*g }t
j|t
j&d||< |S )4z4Generates 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.r   r   r   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inputs_embedsembedding_sizerg   r2   r   visual_feats
visual_posinputsinput_valuesi'  i N  r  r  maskpast_key_valuesidszMSymbolic trace with past_key_values input is not supported yet for the model ze. Please open an issue or a PR in Transformers repository if you would like to see the support added.c                 3   s4    | ]}t j t jd t j t jd fV  qdS )r   N)r   randr   )r   r   cache_shaper   rK   rL   r   $  s    
z1HFTracer._generate_dummy_input.<locals>.<genexpr>)3r  rs  r  r   r   r(   r'   r#   r   r   r   r  longr*   r"   r-   ro  configr  r   
num_labelsrX  r)   r/   r    r&   r,   r+   r!   NotImplementedErrorr  r  r  r  rC   collectionsabcIterabler   input_feat_per_channel
model_typer  hidden_sizer   visual_feat_dimvisual_pos_dim"_FX_SUPPORTED_MODELS_WITH_KV_CACHEnum_attention_headsr;  r   num_hidden_layers)r   r  r  r   r  model_class_nameinputs_dictkv_cache_lengthr   labels_shapelabels_dtyper  r  heightwidthr  embedding_shaper   
seq_length
mask_shape	num_headsrI  pkvshape_with_hidden_sizerK   r  rL   _generate_dummy_input  s\  
    w	jh


S
"O
M

8

6

-
$





zHFTracer._generate_dummy_inputNc                    s0  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}d| _
d| _|dkrct||}||	i |
}t|tjrb|jdd}nw|dkr}t|	d j|}t||}||	i |
}n]|d	krt| d
st|  d| j|}t|}|tv rt| |g|	R i |
}n1| j|	i |
}n(|dkr| j}|d}|D ]}t||}qt|tjr|jdd}n|}nd}|rt|tstd|| W n$ ty } ztrtd| d| d|  W Y d }~nd }~ww d| _
d| _|S )Nplaceholderr   r   Trz  r   ri  r   call_moduleorig_forwardz/ does not have an attribute called orig_forwardr  .Fz"Don't support composite output yetzCould not compute metadata for z target z: ) rp  rk  	meta_argsrh  orig_fnsr   r  r  r  r  _disable_module_getattr_disable_call_modulerc  rE   rC   r   r   r  rs  ro  AttributeErrorr  get_submoduletyper  splitr   r   	Exception_IS_IN_DEBUG_MODEwarningswarn)r   kindrQ  r   r   r  	type_exprr  rv
args_metaskwargs_metasshould_install_metadatameta_targetmeta_outmethodmodmod_typeattr_itratomsatomerr  rK   rL   rk  2  sj   




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   )r  )attr_valr   r   rK   rL   r8    s    zLHFTracer._module_getattr.<locals>.maybe_get_proxy_for_attr.<locals>.<lambda>r  rK   )r  	signaturerk  
parametersparam_shapes_constant)r2  collection_to_searchparameter_proxy_cacher   r   	val_proxyrl  )r2  r   rL   maybe_get_proxy_for_attr}  s   z:HFTracer._module_getattr.<locals>.maybe_get_proxy_for_attr)
r  rC   r   r   	Parameterr  named_parametersr  r   named_buffers)r   r  r2  r7  r9  maybe_parameter_proxymaybe_buffer_proxyrK   rl  rL   _module_getattrx  s    zHFTracer._module_getattrr  r2  r7  c                 C   s   |  |||S r   )r?  )r   r  r2  r7  rK   rK   rL   r    r   zHFTracer.getattrc                    s2   t | ddr||i |S || _t ||||S )Nr  F)r  r  rp  r  )r   r   forwardr   r   rr  rK   rL   r    s   zHFTracer.call_modulec                 C   s
   t || S r   )rd  )r   r  rK   rK   rL   proxy  r   zHFTracer.proxyr  .c                 c   s0   dd | j D | _t | _| j D ]\}\}}tt|| | j| qg }t	|}t
j D ]9\}}|d ur?||ur?q2|dsEq2| j D ] \}}	|j D ]\}
}||u ri|||
|f t||
|	 qSqJq2d V  | j D ]\}\}}tt|| qti | _t | _|D ]\}}
}t||
| qd S )Nc                 S   s   i | ]
}|t tt|qS rK   )r  r  r   )r   rQ  rK   rK   rL   
<dictcomp>  s    z.HFTracer.patch_for_tracing.<locals>.<dictcomp>transformers)_TORCH_METHODS_TO_PATCHpatched_torch_methodssetr  itemsr  r   addr  	getmodulesysmodulesr  r  __dict__rF   )r   r  r  r  origpatchedmodule_of_modelr,  orig_clspatched_clsr  r  r   rK   rK   rL   patch_for_tracing  s>   

zHFTracer.patch_for_tracing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|| j|||
|d q|td| ddd }t||}|j D ]}|jt jj kr|j	|vri |d|j	 < q|| _!| a"| #| zt$ j%| d| _&W da"nda"w W d   n1 sw   Y  | j&j'D ]N}|j(dkr@|j)|v rd|_*tj+|_n/|g}t,- }|r0|.d}d||< |t/|j0 7 }|st1| D ]	}| j&2| q6|j(dkrId|_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 rK   r  r>   r   r   rS  rT  rK   rL   rB    s
    z"HFTracer.trace.<locals>.<dictcomp>r   r  r  r2   )_deserialize_graph_module_CodeOnlyModuler  zCould not generate input named z8 for because root is not a transformers.PreTrainedModel.c                 S   s   t | tjr| dS | S r7  )rC   r   r   r   )rC  rK   rK   rL   to_meta  s   
zHFTracer.trace.<locals>.to_meta**rS  r  rK   r   r=  )3r  r3  rC   r   r   Moduler@  r4  r}  r  r>   r:  r   r   updaterB   r  rs  r  r   r'   r   r   supported_archsr  r  r  r  r  pytreetree_mapr#  VAR_KEYWORDr  r  rR  rp  tracegraphnodesoprQ  r   r   r  OrderedDictr   r   usersreversed
erase_node)r   r  rS  rT  rU  sigparamr  r   sequence_lengthr   num_choicesr  r  r\  concrete_metasr  to_visit	to_deleter   userrr  rX  rL   re    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   )rC   r   )r   r  rK   rK   rL   r   S  s    zKHFTracer._stateless_mod_instanciation_depends_on_proxies.<locals>.<genexpr>)anyrL  r}  )r   r,  rK   rK   rL   /_stateless_mod_instanciation_depends_on_proxiesN  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.
        r:   r   r   FTr2   )rv  rs  r  lowerro  r  r  
add_module)r   r,  idxmod_namepathalready_insertedrK   rK   rL   _insert_module_as_submoduleU  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   r4  buffersr}  )r   r,  r1  r{  rr  rK   rL   r~  m  s   	.
zHFTracer.path_of_moduler   module_qualified_namec                    s   |  | ot ||S r   )rv  rp  is_leaf_module)r   r   r  rr  rK   rL   r  ~  s   zHFTracer.is_leaf_module)is_backward_compatibleobjr   c                 C   s$   t |d }|jjd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.
        rB   r]  )rv  r  rQ  r  rf  )r   r  	attributerK   rK   rL   rB     s   zHFTracer.keys)NNN)NNT)6r  r  r  r  r  bool__annotations__r  rD  r   r  r   r  r   r  r   r  r  r6   r   r8   ra  r!  r  rD   r   r   r   r   r   r  rk  r?  r   r  r  rA  
contextlibcontextmanagerr
   r   r_  r   rR  r   r   re  rv  r}  r~  r  r   rB   r  rK   rK   rr  rL   r  [  sf   
 	
 0F&$'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 )Nr2   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 rK   rV  rW  r[  rK   rL   rB    s     z%get_concrete_args.<locals>.<dictcomp>)
r  r3  r@  rF  r4  rB   r   joinr   r}  )r  r  rm  formatted_input_namesformatted_allowed_input_namesrK   r[  rL   get_concrete_args  s   r  r   c                 C   s   | j jtv S r   )rs  r  _SUPPORTED_MODELS)r  rK   rK   rL   is_model_supported  r  r  c                 C   s.   t | sdt}td| jj d| d S )Nr  zModel z) is not supported yet, supported models: )r  r  r  r  rs  r  )r  supported_model_namesrK   rK   rL   check_if_model_is_supported  s   
r  disable_check
tracer_clsc                 C   s   |du r	| j  }t|}t| |}|st|  d|v r(t| jdds(td d|vr<t| jddr<td d| j_	| }|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  	use_cacheFz|`past_key_values` were specified as input names, but model.config.use_cache = False, this might lead to unexpected behavior.z`past_key_values` were not specified as input names, but model.config.use_cache = True. Setting model.config.use_cache = False.r^  )rT  rB   r   r  r  r  r  loggerwarningr  re  r   r  r   rs  r  r   )r  r  r  r  rS  rj  traced_graphtracedrK   rK   rL   symbolic_trace  s,   

r  r   rK   )NNr   FF)F)NN)r   )Nr   FN)r  r  N)r  r   )r  r  r  r  r  r!  r^  osr  rJ  r!  typingr   r   r   r   r   r   r   r	   r
   r   torch.utils._pytreeutils_pytreerb  r   torch.fxr   r   r   r   r   torch.fx._compatibilityr   torch.fx._symbolic_tracer   torch.fx.proxyr   r:   r   cache_utilsr   r   r   r   modeling_utilsr   r   models.autor   models.auto.modeling_autor   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   import_utilsr3   r4   r5   r6   r7   peftr8   
get_loggerr  r  environrE   upperr   rD   rM   (_REGULAR_SUPPORTED_MODEL_NAMES_AND_TASKSr  _REGULAR_SUPPORTED_MODELSitemrC   r   extend_SPECIAL_SUPPORTED_MODELSr;  sortedrF  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/  r2  r4  r5  r6  r>  r@  rJ  rR  rT  rU  rb  	Embedding
functional	embedding	LayerNorm	GroupNormLinearreluReLUwhereabsr  r   catstackrH  mulr   matmulbmmbaddbmmr   repeatrepeat_interleaverollflipindex_selectgatherConv1dConv2dsqueeze	unsqueezer9  one_hotMSELossCrossEntropyLossBCEWithLogitsLossr_  rc  r  scaled_dot_product_attentionrd  rv  rm  r  r  r  r  r  r  r  r  r  r  r  r   r  r  r_  r  r  r  r  r  rK   rK   rK   rL   <module>   s  
,X	

)6

$






	
 !"#
/,
$   	    8



