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	Z	d dl
Z
d dl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+m,Z, ddl-m.Z. ddl/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z=m>Z>m?Z?m@Z@mAZAmBZBmCZC ddlDmEZEmFZFmGZGmHZHmIZI eH rd dlJmKZK e$LeMZNejOPddQ eEv ZR	ddeSe+ deeeTeUeT f  deUeT fddZVg dZWddgZXg ZYeWD ]ZZe[eZe\reY]eVdi eZ qeY]eVeZ qg dZ^e_e`eaeYe^ Zbdacdd Zd	dd d!Zed"d# Zfd$d% Zgd&d' Zhd(d) Zid*d+ Zjdd,d-Zkd.d/ Zldd0d1d2Zmd3d4 Znd5d6 Zoddd0d7d8Zpddd0d9d:Zqddd;d<d=Zrdd0d>d?Zsd@dA Ztdd0dBdCZudd0dDdEZvddddFdGdHZwddddFdIdJZxdKdL ZydMdN ZzdddOdPdQZ{dd0dRdSZ|dTdU Z}dddVdWdXZ~dYdZ Zdd[d\Zd]d^ Zd_d` Zdadb Zdcdd ZddedfZddgdhZdidj Zdkdl Zdmdn ZddpdqZ	ddsdtZdudv Zdwdx Zdydz Zd{d| Zi ejjedejjjeeejjefejjegejjehejeiejjjekejjejejelejemejenejeoejepejeqejerejesejjeti ejeuejevejewejjexejeyejjez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 deSZdede_eef fddZdd ZdeSe& 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eUe  fddZG dd deZdejdeUeT fddZdddZdddZddefdddeeUeT  dedeSe def
ddZdS )    N)AnyCallableLiteralOptional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)r-   r%   r$   r"   r   r(   r*   r#   r   r&   r)   r+   r!   r   r,   r   r   r'   r   r    keys
isinstancestrgetappend)r7   r8   task_mappingmodel_class_namestask
class_name rG   i/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/transformers/utils/fx.py%_generate_supported_model_class_namesO   sd   	


rI   )4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qwen3	qwen3_moeresnetroberta	segformerspeech_to_textspeech_to_text_2swint5trocrvitxglmwav2vec2r]   ri   )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inputrG   rG   rH   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sparserG   rG   rH   torch_nn_functional_embedding   s   &r   c                 C      |S NrG   r   rG   rG   rH   torch_nn_layernorm      r   c                 C   r   r   rG   r   rG   rG   rH   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   rG   rG   rH   torch_nn_linear   s    r   c                 C      | S r   rG   xrG   rG   rH   
torch_relu   r   r   c                 C   r   r   rG   )r   r   rG   rG   rH   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   inplacerG   rG   rH   torch_nn_functional_relu   s   r   c                 C   s$   | j dd|j dd |j dd S Nr   r   to)	conditionr   yrG   rG   rH   torch_where   s   $r   outc                C   s   |d urt d| S )Nz2Don't support in-place abs for MetaTensor analysisr   )r   r   rG   rG   rH   	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 )Nr.   r   r   stepr   r   r   r   )lenr?   floatintrA   r   r   )argskwargsnr   startendr   rG   rG   rH   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 )Nr.   
fill_valuer   r   )listr   dictpopr   full)r   r   kwargs_without_devicerG   rG   rH   
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 rG   )r   ).0trG   rG   rH   
<listcomp>&  s    ztorch_cat.<locals>.<listcomp>c                 3   s    | ]}|  V  qd S r   rG   )r   r   dimrG   rH   	<genexpr>(  s    ztorch_cat.<locals>.<genexpr>r.   r   r   )r   r   sumr   r   )tensorsr   axisr   shapesr   concatenated_dimfinal_shaperG   r   rH   	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.   r   r   )r   r   r   insertr   r   r   )r   r   r   r   r   rG   rG   rH   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   r.   )r?   r   Tensor
empty_likemaxr   r   r   rangerB   r   )	r   otherr   r   
max_lengthinput_shapeother_shaper   irG   rG   rH   	torch_add9  s   r   c                C   s   t | ||dS )Nr   )r   )r   r   r   rG   rG   rH   	torch_mulG     r   c                 C   
   t | |S r   )r   )r   r   rG   rG   rH   torch_tensor_mulK     
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   r           r   r   r   )r   sizer   r   r   rB   r   r   r   tensorr   )
r   r   r   d1d2r   r   shape1shape2r   rG   rG   rH   torch_matmulO  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_prG   rG   rH   	torch_bmms  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   rG   rG   rH   torch_baddbmm{  s   
r   c                C   s   t | |||||dS )Nr   )r   )r   r   r   r   r   r   rG   rG   rH   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   operandrG   rG   rH   r     s    ztorch_einsum.<locals>.<genexpr>r   )r   einsumr   )equationoperandsconcrete_operandsrG   rG   rH   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   rG   rG   rH   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 )Nr.   r   r   r   r   )	r   r   r   r   r?   r   r   numelr   )r   r  r   num_argsr   repeatsrG   rG   rH   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   rG   rG   rH   torch_index_select  s   
r  c                 C      t | ||S r   )r  r   r   r  rG   rG   rH   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   rG   rG   rH   torch_gather  s   
r  c                 C   r  r   )r  r  rG   rG   rH   torch_tensor_gather  r  r  c                 C   r   r   rG   )r   shiftsdimsrG   rG   rH   
torch_roll  r   r  c                 C   r   r   rG   )r   r  rG   rG   rH   
torch_flip  r   r  c                 C   r   r   rG   )r   r  rG   rG   rH   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   r.   r   r   r   r   paddingr   mathfloordilationkernel_sizestrideout_channelsr   r   )r   r   l_inr   r  l_outrG   rG   rH   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.   r   r   r  )r   r   h_inw_inr   r  h_outw_outrG   rG   rH   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   rB   r   r   )r   r   r   	new_shape	dim_valuerG   rG   rH   torch_squeeze  s   

r0  c                 C   r   r   )r0  r   r   rG   rG   rH   torch_tensor_squeeze  r   r2  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   rG   rG   rH   torch_unsqueeze  s
   
r3  c                 C   r   r   )r3  r1  rG   rG   rH   torch_tensor_unsqueeze
  r   r4  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   rG   rG   rH   <lambda>  s   
 z*torch_unique_consecutive.<locals>.<lambda>)r   unique_consecutive
zeros_liker?   r   r   tuplemap)r   r   outputrG   rG   rH   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   rG   rG   rH   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_dimrG   rG   rH   0torch_nn_functional_scaled_dot_product_attention  s   

&rH  c                 C   $   | j dkr	|j}nd}tj|ddS Nnone)r.   r   r   	reductionr   r   r   r   r   targetr   rG   rG   rH   torch_nn_mseloss%     
rP  c                 C   rI  rJ  rL  rN  rG   rG   rH   torch_nn_crossentropyloss-  rQ  rR  c                 C   rI  rJ  rL  rN  rG   rG   rH   torch_nn_bcewithlogitsloss5  rQ  rS  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   )r?   r   r   	ones_liker   float16float32float64int32r   int64)r   concreterG   rG   rH   to_concrete>  s   z%operator_getitem.<locals>.to_concreter   r   r   )	r?   r   r   r9  r:  operatorgetitemr   r   )abr[  rG   rG   rH   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   
   || _ d S r   )	_metadata)r   metadatarG   rG   rH   install_metadata  r   zHFProxy.install_metadatac                 C   s   | j dd| fi S )Ncall_methodr   )tracercreate_proxyr   rG   rG   rH   r     s   zHFProxy.shapec                 C   s
   t | dS )Nr   )MetaDeviceAttributerj  rG   rG   rH   r     s   
zHFProxy.devicec                    s(   t | dr| jd urt| jS t  S Nrd  )hasattrrd  r   super__len__rj  	__class__rG   rH   ro    s   

zHFProxy.__len__c                    s$   t | dr| jd ur| jS t  S rl  )rm  rd  rn  __bool__rj  rp  rG   rH   rr    s   
zHFProxy.__bool__c                 C   s   |dkr	|  |S t| |S rl  )__getattribute__HFAttribute)r   krG   rG   rH   __getattr__  s   

zHFProxy.__getattr__c                 C   s   | j dtj| ||fi S Ncall_function)rh  ri  r\  setitem)r   indicesvaluesrG   rG   rH   __setitem__  s   zHFProxy.__setitem__c                    s*   t | dr| jd ur|| jv S t |S rl  )rm  rd  rn  __contains__)r   r@  rp  rG   rH   r}    s   
zHFProxy.__contains__)__name__
__module____qualname____doc__rf  propertyr   r   ro  rr  rv  r|  r}  __classcell__rG   rG   rp  rH   rb    s    

rb  c                   @   s.   e Zd ZdefddZedd Zdd ZdS )	rt  attrc                 C   sB   || _ || _|j| _d | _t| j dr| t| j j| d S d S rl  )rootr  rh  _noderm  rf  getattrrd  )r   r  r  rG   rG   rH   __init__  s   zHFAttribute.__init__c                 C   s0   | j d u r| jdtj| j| jfi j| _ | j S rw  )r  rh  ri  builtinsr  r  r  noderj  rG   rG   rH   r    s   
 zHFAttribute.nodec                 O   s   | j d| j| jf| |S )Nrg  )rh  ri  r  r  )r   r   r   rG   rG   rH   __call__  s   zHFAttribute.__call__N)r~  r  r  r@   r  r  r  r  rG   rG   rG   rH   rt    s
    	
rt  c                   @   s   e Zd ZdS )rk  N)r~  r  r  rG   rG   rG   rH   rk    s    rk  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   rc  r   )_orig_cache_cls)r   r  rG   rG   rH   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.)rm  RuntimeErrorrh  _CLASSES_TO_PATCHr  rj  rG   rG   rH   rq    s   
zHFCacheProxy.__class__N)	r~  r  r  r  typer   r  r  rq  rG   rG   rG   rH   r    s
    r  functionop_typerx  rg  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   )r?   r   rB   )r^  found_proxiesrG   rH   check_proxy  s   
z4create_wrapper.<locals>.wrapper.<locals>.check_proxyr   rx  rg  r  zop_type z not supported.r  )
r   r   fxr  map_aggregater   rh  r~  r   ri  )r   r   r  rh  rO  r  r  r  r  rH   wrapper  s"   
zcreate_wrapper.<locals>.wrapper)	functoolswraps)r  r  r  r  rG   r  rH   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  rx  __rg  r  )
rn  __new__dirr  
startswithinspectismethod
isfunctionsetattrr  )clsr  r  r  r  	attr_namer  r  rp  rG   rH   r    s$   


zHFProxyableClassMeta.__new__r   )r~  r  r  r  r@   r9  r  r   r   r   r   r
   r   r  r  rG   rG   rp  rH   r    s    	

r  rO  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.
    rx  )r  )rO  r  rG   rG   rH   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   rd  zNo metadata was found for )	r?   rk  r   r  r   rb  rm  r  rd  )vrG   rG   rH   _proxies_to_metas$  s   
r  r  c                    s   dt dtf fdd}|S )Nr   r9   c                    s*   t tts	tdt| t}|  |S )NzJCannot create HFCacheProxy because there is no HFTracer currently tracing.)r?   _CURRENT_TRACERHFTracerr  r  r  )r   cache_proxyr  rG   rH   cache_proxy_factory_fn0  s
   


z=create_cache_proxy_factory_fn.<locals>.cache_proxy_factory_fn)r
   r  )r  r  rG   r  rH   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  rA  rG   rG   rH   _generate_random_intS  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trilrG   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.)rn  r  r3   ImportErrorr1   r0   )r   r  r  rp  rG   rH   r  {  s   
zHFTracer.__init__modelr   
input_namer   input_namesr9   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_sizerc   r.   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   rG   rH   r   %  s    
z1HFTracer._generate_dummy_input.<locals>.<genexpr>)3r  rq  r~  r   r   r$   r#   r   r   r   r   r  longr&   r   r)   rm  configr  r   
num_labelsrV  r%   r+   r   r"   r(   r'   r   NotImplementedErrorr  r  r  r  r?   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_headsr9  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_headsrG  pkvshape_with_hidden_sizerG   r  rH   _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   Trx  r   rg  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: ) rn  ri  	meta_argsrf  orig_fnsr   r  r  r  r  _disable_module_getattr_disable_call_modulera  rA   r?   r   r   r  rq  rm  AttributeErrorr  get_submoduler  r  splitr   r   	Exception_IS_IN_DEBUG_MODEwarningswarn)r   kindrO  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erp  rG   rH   ri  3  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   rG   rH   r6    s    zLHFTracer._module_getattr.<locals>.maybe_get_proxy_for_attr.<locals>.<lambda>r  rG   )r  	signatureri  
parametersparam_shapes_constant)r0  collection_to_searchparameter_proxy_cacher   r   	val_proxyrj  )r0  r   rH   maybe_get_proxy_for_attr~  s   z:HFTracer._module_getattr.<locals>.maybe_get_proxy_for_attr)
r  r?   r   r   	Parameterr  named_parametersr  r   named_buffers)r   r  r0  r5  r7  maybe_parameter_proxymaybe_buffer_proxyrG   rj  rH   _module_getattry  s    zHFTracer._module_getattrr  r0  r5  c                 C   s   |  |||S r   )r=  )r   r  r0  r5  rG   rG   rH   r    r   zHFTracer.getattrc                    s2   t | ddr||i |S || _t ||||S )Nr  F)r  r  rn  r  )r   r   forwardr   r   rp  rG   rH   r    s   zHFTracer.call_modulec                 C   s
   t || S r   )rb  )r   r  rG   rG   rH   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 rG   )r  r  r   )r   rO  rG   rG   rH   
<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__rB   )r   r  r  r  origpatchedmodule_of_modelr*  orig_clspatched_clsr  r  r   rG   rG   rH   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 rG   r  r:   r   r   rQ  rR  rG   rH   r@    s
    z"HFTracer.trace.<locals>.<dictcomp>r   r  r  r.   )_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 r5  )r?   r   r   r   )rA  rG   rG   rH   to_meta  s   
zHFTracer.trace.<locals>.to_meta**rQ  r  rG   r   r;  )3r  r1  r?   r   r   Moduler>  r2  r{  r  r:   r8  r   r   updater>   r  rq  r~  r   r#   r   r   supported_archsr  r  r  r  r  pytreetree_mapr!  VAR_KEYWORDr  r  rP  rn  tracegraphnodesoprO  r   r   r  OrderedDictr   r   usersreversed
erase_node)r   r  rQ  rR  rS  sigparamr  r   sequence_lengthr   num_choicesr  r  rZ  concrete_metasr  to_visit	to_deleter   userrp  rV  rH   rc    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   )r?   r   )r   r  rG   rG   rH   r   T  s    zKHFTracer._stateless_mod_instanciation_depends_on_proxies.<locals>.<genexpr>)anyrJ  r{  )r   r*  rG   rG   rH   /_stateless_mod_instanciation_depends_on_proxiesO  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.
        r6   r   r   FTr.   )rt  rq  r~  lowerrm  r  r  
add_module)r   r*  idxmod_namepathalready_insertedrG   rG   rH   _insert_module_as_submoduleV  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)	rn  path_of_module	NameErrorr  r   r   r2  buffersr{  )r   r*  r/  ry  rp  rG   rH   r|  n  s   	.
zHFTracer.path_of_moduler   module_qualified_namec                    s   |  | ot ||S r   )rt  rn  is_leaf_module)r   r   r  rp  rG   rH   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.
        r>   r[  )rt  r  rO  r  rd  )r   r  	attributerG   rG   rH   r>     s   zHFTracer.keys)NNN)NNT)6r~  r  r  r  r  bool__annotations__r  rB  r   r  r   r  r   r  r   r  r  r2   r   r4   r_  r  r  r@   r   r   r   r   r   r  ri  r=  r   r  r  r?  
contextlibcontextmanagerr   r   r]  r   rP  r   r   rc  rt  r{  r|  r  r   r>   r  rG   rG   rp  rH   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 )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 rG   rT  rU  rY  rG   rH   r@    s     z%get_concrete_args.<locals>.<dictcomp>)
r  r1  r>  rD  r2  r>   r   joinr   r{  )r  r  rk  formatted_input_namesformatted_allowed_input_namesrG   rY  rH   get_concrete_args  s   r  r   c                 C   s   | j jtv S r   )rq  r~  _SUPPORTED_MODELS)r  rG   rG   rH   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  rq  r~  )r  supported_model_namesrG   rG   rH   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\  )rR  r>   r   r  r  r  r  loggerwarningr  rc  r   r  r	   rq  r  r   )r  r  r  r  rQ  rh  traced_graphtracedrG   rG   rH   symbolic_trace  s,   

r  r   rG   )NNr   FF)F)NN)r   )Nr   FN)r  r  N)r  r   )r  r  r  r  r  r  r\  osr  rH  r  typingr   r   r   r   r   r   torch.utils._pytreeutils_pytreer`  r   torch.fxr   r	   r
   r   r   torch.fx._compatibilityr   torch.fx._symbolic_tracer   torch.fx.proxyr   r6   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+   r,   r-   import_utilsr/   r0   r1   r2   r3   peftr4   
get_loggerr~  r  environrA   upperr  r  r@   r   rI   (_REGULAR_SUPPORTED_MODEL_NAMES_AND_TASKSr  _REGULAR_SUPPORTED_MODELSitemr?   r   extend_SPECIAL_SUPPORTED_MODELSr9  sortedrD  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-  r0  r2  r3  r4  r<  r>  rH  rP  rR  rS  r`  	Embedding
functional	embedding	LayerNorm	GroupNormLinearreluReLUwhereabsr  r   catstackrF  mulr   matmulbmmbaddbmmr   repeatrepeat_interleaverollflipindex_selectgatherConv1dConv2dsqueeze	unsqueezer7  one_hotMSELossCrossEntropyLossBCEWithLogitsLossr]  ra  r  scaled_dot_product_attentionrb  rt  rk  r  r  r  r  r  r  r  r  r  r  r   r  r  r]  r  r  r  r  r  rG   rG   rG   rH   <module>   s  
X	

)8

$






	
 !"#
/,
$   	    8



