o
    
sh?                     @   sn  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( ddl)m*Z*m+Z+ ddl,m-Z- ddl.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z=m>Z>m?Z?m@Z@mAZAmBZBmCZC ddlDmEZEmFZF eF rd dlGmHZH e$IeJZKejLMddN eEv ZO	ddePe* deeeQeReQ f  deReQ fddZSg dZTddgZUg ZVeTD ]ZWeXeWeYreVZeSdi eW q eVZeSeW q g dZ[e\e]e^eVe[ Z_da`dd Za	dd d!Zbd"d# Zcd$d% Zdd&d' Zed(d) Zfd*d+ Zgdd,d-Zhd.d/ Zidd0d1d2Zjd3d4 Zkd5d6 Zlddd0d7d8Zmddd0d9d:Znddd;d<d=Zodd0d>d?Zpd@dA Zqdd0dBdCZrdd0dDdEZsddddFdGdHZtddddFdIdJZudKdL ZvdMdN ZwdddOdPdQZxdd0dRdSZydTdU ZzdddVdWdXZ{dYdZ Z|dd[d\Z}d]d^ Z~d_d` Zdadb Zdcdd ZddedfZddgdhZdidj Zdkdl Zdmdn ZddpdqZ	ddsdtZdudv Zdwdx Zdydz Zd{d| Zi ejjeaejjjebejjecejjedejjeeejefejjjehejjegejeiejejejekejelejemejenejeoejepejjeqi ejerejesejetejjeuejevejjewejexeje}eje~ejjeejeyejjez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	ZeYe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 dePZdede\eef fddZdd ZdePe& deegef fddZede&fi ee&dZede'fi ee'dZede(fi ee(dZddededeeRe  fddZG dd deZdejdeReQ fddZdddZdddZddefdddeeReQ  dedePe 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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_NAMES,MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING_NAMES6MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING_NAMESMODEL_MAPPING_NAMES   )ENV_VARS_TRUE_VALUESis_peft_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)r4   r5   task_mappingmodel_class_namestask
class_name rD   S/var/www/html/alpaca_bot/venv/lib/python3.10/site-packages/transformers/utils/fx.py%_generate_supported_model_class_namesM   sd   	


rF   )8altclipalbertbartbertbitnet
blenderbotzblenderbot-smallbloomclipconvnextdebertaz
deberta-v2dinov2dinov3_convnext
dinov3_vit
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vjepa2xglmwav2vec2r]   ri   )CLIPTextModelCLIPTextModelWithProjectionCLIPVisionModelCLIPVisionModelWithProjectionAltCLIPTextModelAltCLIPVisionModelGitVisionModelGPT2DoubleHeadsModelSpeech2Text2DecoderTrOCRDecoderPeftModelForCausalLMPeftModelForSeq2SeqLMVJEPA2ForVideoClassificationc                 C   s*   t jg |j| jjd R d| jjdS Nmeta)devicedtype)torchemptyshapeweightr   selfinputrD   rD   rE   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sparserD   rD   rE   torch_nn_functional_embedding   s   &r   c                 C      |S NrD   r   rD   rD   rE   torch_nn_layernorm      r   c                 C   r   r   rD   r   rD   rD   rE   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   rD   rD   rE   torch_nn_linear   s    r   c                 C      | S r   rD   xrD   rD   rE   
torch_relu   r   r   c                 C   r   r   rD   )r   r   rD   rD   rE   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   inplacerD   rD   rE   torch_nn_functional_relu   s   r   c                 C   s$   | j dd|j dd |j dd S Nr   r   to)	conditionr   yrD   rD   rE   torch_where   s   $r   outc                C   s   |d urt d| S )Nz2Don't support in-place abs for MetaTensor analysisr   )r   r   rD   rD   rE   	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intr>   r   r   )argskwargsnr   startendr   rD   rD   rE   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_devicerD   rD   rE   
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 rD   )r   ).0trD   rD   rE   
<listcomp>)  s    ztorch_cat.<locals>.<listcomp>c                 3   s    | ]}|  V  qd S r   rD   )r   r   dimrD   rE   	<genexpr>+  s    ztorch_cat.<locals>.<genexpr>r.   r   r   )r   r   sumr   r   )tensorsr   axisr   shapesr   concatenated_dimfinal_shaperD   r   rE   	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   rD   rD   rE   torch_stack0  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   ranger?   r   )	r   otherr   r   
max_lengthinput_shapeother_shaper   irD   rD   rE   	torch_add<  s   r   c                C   s   t | ||dS )Nr   )r   )r   r   r   rD   rD   rE   	torch_mulJ     r   c                 C   
   t | |S r   )r   )r   r   rD   rD   rE   torch_tensor_mulN     
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   r?   r   r   r   tensorr   )
r   r   r   d1d2r   r   shape1shape2r   rD   rD   rE   torch_matmulR  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_prD   rD   rE   	torch_bmmv  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   rD   rD   rE   torch_baddbmm~  s   
r   c                C   s   t | |||||dS )Nr   )r   )r   r   r   r   r   r   rD   rD   rE   torch_tensor_baddbmm     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   operandrD   rD   rE   r     s    ztorch_einsum.<locals>.<genexpr>r   )r   einsumr   )equationoperandsconcrete_operandsrD   rD   rE   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   rD   rD   rE   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   repeatsrD   rD   rE   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   rD   rD   rE   torch_index_select  s   
r  c                 C      t | ||S r   )r  r   r   r  rD   rD   rE   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   rD   rD   rE   torch_gather  s   
r  c                 C   r  r   )r  r  rD   rD   rE   torch_tensor_gather  r  r  c                 C   r   r   rD   )r   shiftsdimsrD   rD   rE   
torch_roll  r   r  c                 C   r   r   rD   )r   r  rD   rD   rE   
torch_flip  r   r  c                 C   r   r   rD   )r   r  rD   rD   rE   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_outrD   rD   rE   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_outrD   rD   rE   torch_nn_conv2d  s$   

88
r0  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_valuerD   rD   rE   torch_squeeze  s   

r3  c                 C   r   r   )r3  r   r   rD   rD   rE   torch_tensor_squeeze  r   r5  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   rD   rD   rE   torch_unsqueeze  s
   
r6  c                 C   r   r   )r6  r4  rD   rD   rE   torch_tensor_unsqueeze  r   r7  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   rD   rD   rE   <lambda>  s   
 z*torch_unique_consecutive.<locals>.<lambda>)r   unique_consecutive
zeros_liker<   r   r   tuplemap)r   r   outputrD   rD   rE   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   rD   rD   rE   torch_nn_functional_one_hot  s   rA  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_dimrD   rD   rE   0torch_nn_functional_scaled_dot_product_attention   s   

&rK  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   rD   rD   rE   torch_nn_mseloss(     
rS  c                 C   rL  rM  rO  rQ  rD   rD   rE   torch_nn_crossentropyloss0  rT  rU  c                 C   rL  rM  rO  rQ  rD   rD   rE   torch_nn_bcewithlogitsloss8  rT  rV  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   concreterD   rD   rE   to_concreteA  s   z%operator_getitem.<locals>.to_concreter   r   r   )	r<   r   r   r<  r=  operatorgetitemr   r   )abr^  rD   rD   rE   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   
   || _ d S r   )	_metadata)r   metadatarD   rD   rE   install_metadata  r   zHFProxy.install_metadatac                 C   s   | j dd| fi S )Ncall_methodr   )tracercreate_proxyr   rD   rD   rE   r     s   zHFProxy.shapec                 C   s
   t | dS )Nr   )MetaDeviceAttributerm  rD   rD   rE   r     s   
zHFProxy.devicec                    s(   t | dr| jd urt| jS t  S Nrg  )hasattrrg  r   super__len__rm  	__class__rD   rE   rr    s   

zHFProxy.__len__c                    s$   t | dr| jd ur| jS t  S ro  )rp  rg  rq  __bool__rm  rs  rD   rE   ru    s   
zHFProxy.__bool__c                 C   s   |dkr	|  |S t| |S ro  )__getattribute__HFAttribute)r   krD   rD   rE   __getattr__  s   

zHFProxy.__getattr__c                 C   s   | j dtj| ||fi S Ncall_function)rk  rl  r_  setitem)r   indicesvaluesrD   rD   rE   __setitem__  s   zHFProxy.__setitem__c                    s*   t | dr| jd ur|| jv S t |S ro  )rp  rg  rq  __contains__)r   rC  rs  rD   rE   r    s   
zHFProxy.__contains__)__name__
__module____qualname____doc__ri  propertyr   r   rr  ru  ry  r  r  __classcell__rD   rD   rs  rE   re    s    

re  c                   @   s.   e Zd ZdefddZedd Zdd ZdS )	rw  attrc                 C   sB   || _ || _|j| _d | _t| j dr| t| j j| d S d S ro  )rootr  rk  _noderp  ri  getattrrg  )r   r  r  rD   rD   rE   __init__  s   zHFAttribute.__init__c                 C   s0   | j d u r| jdtj| j| jfi j| _ | j S rz  )r  rk  rl  builtinsr  r  r  noderm  rD   rD   rE   r    s   
 zHFAttribute.nodec                 O   s   | j d| j| jf| |S )Nrj  )rk  rl  r  r  )r   r   r   rD   rD   rE   __call__  s   zHFAttribute.__call__N)r  r  r  r=   r  r  r  r  rD   rD   rD   rE   rw    s
    	
rw  c                   @   s   e Zd ZdS )rn  N)r  r  r  rD   rD   rD   rE   rn    s    rn  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   rf  r   )_orig_cache_cls)r   r  rD   rD   rE   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.)rp  RuntimeErrorrk  _CLASSES_TO_PATCHr  rm  rD   rD   rE   rt    s   
zHFCacheProxy.__class__N)	r  r  r  r  typer   r  r  rt  rD   rD   rD   rE   r    s
    r  functionop_typer{  rj  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rR d j}dkr4}ndks<dkr@j}ntd d|j	|| |d	S | i |S )
Nc                    s   t | tr |  d S d S r   )r<   r   r?   )ra  found_proxiesrD   rE   check_proxy  s   
z4create_wrapper.<locals>.wrapper.<locals>.check_proxyr   r{  rj  r  zop_type z not supported.r  )
r   r   fxr  map_aggregater   rk  r  r   rl  )r   r   r  rk  rR  r  r  r  r  rE   wrapper  s   
zcreate_wrapper.<locals>.wrapper)	functoolswraps)r  r  r  r  rD   r  rE   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  r{  __rj  r  )
rq  __new__dirr  
startswithinspectismethod
isfunctionsetattrr  )clsr  r  r  r  	attr_namer  r  rs  rD   rE   r    s$   


zHFProxyableClassMeta.__new__r   )r  r  r  r  r=   r<  r  r   r   r   r   r
   r   r  r  rD   rD   rs  rE   r    s    	

r  rR  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.
    r{  )r  )rR  r  rD   rD   rE   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   rg  zNo metadata was found for )	r<   rn  r   r  r   re  rp  r  rg  )vrD   rD   rE   _proxies_to_metas%  s   
r  r  c                    s   dt dtf fdd}|S )Nr   r6   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  rD   rE   cache_proxy_factory_fn1  s
   


z=create_cache_proxy_factory_fn.<locals>.cache_proxy_factory_fn)r
   r  )r  r  rD   r  rE   create_cache_proxy_factory_fn0  s   r  ProxyableCacher  ProxyableDynamicCacheProxyableStaticCache
      lowhighforbidden_valuesc                 C   s8   |d u rg }t | |}||v rt | |}||v s|S r   )randomrandint)r  r  r  rD  rD   rD   rE   _generate_random_intM  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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,Z.d(e&j'def fd-d.Z/d/ej&j'd0edef fd1d2Z0e1dd3d4d5defd6d7Z2  Z3S ):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trilrD   c                    s   t  j||d d S )N)autowrap_modulesautowrap_functions)rq  r  )r   r  r  rs  rD   rE   r  t  r   zHFTracer.__init__modelr   
input_namer   input_namesr6   c                    s  t |d|jj}|ji }d}|dv r|d }|g tttttttttt	tt
v rAtj|tjd|d< |S |g ttttdv rgtj|tjd|d< tj|tjd|d	< |S |ttv rt|jd
ry|jjdu r}t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 rm|d }t |jdd}|du rFt|jdr3|jjj }nt|jdr@|jj!j }nt" t" f}t |jdd}t#|t$j%j&sY||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 rF|\}}t"d*d+d,}tj||tj'd||< |S d-|v rjd.|v r[|d |d$ | g}n|}tj|tjd||< |S d/|v r|tj|tjd||< |S d.|v r|jj)t/vrtd0|jj) d1|jj0}|jj+|jj0 }|d |||f t1 fd2d3t2|jj3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   rD   rE   r     s    
z1HFTracer._generate_dummy_input.<locals>.<genexpr>)4r  rt  r  r   r   r#   r"   r   r+   r   r   r   r  longr%   r   r(   rp  configr  r   
num_labelsrY  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_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_headsrJ  pkvshape_with_hidden_sizerD   r  rE   _generate_dummy_inputw  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   Tr{  r   rj  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: ) rq  rl  	meta_argsri  orig_fnsr   r  r  r  r  _disable_module_getattr_disable_call_modulerd  r>   r<   r   r   r  rt  rp  AttributeErrorr  get_submoduler  r  splitr   r   	Exception_IS_IN_DEBUG_MODEwarningswarn)r   kindrR  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ers  rD   rE   rl  '  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   rD   rE   r9  {  s    zLHFTracer._module_getattr.<locals>.maybe_get_proxy_for_attr.<locals>.<lambda>r  rD   )r  	signaturerl  
parametersparam_shapes_constant)r1  collection_to_searchparameter_proxy_cacher   r   	val_proxyrm  )r1  r   rE   maybe_get_proxy_for_attrr  s   z:HFTracer._module_getattr.<locals>.maybe_get_proxy_for_attr)
r  r<   r   r   	Parameterr  named_parametersr  r   named_buffers)r   r  r1  r6  r8  maybe_parameter_proxymaybe_buffer_proxyrD   rm  rE   _module_getattrm  s    zHFTracer._module_getattrr  r1  r6  c                 C   s   |  |||S r   )r>  )r   r  r1  r6  rD   rD   rE   r    r   zHFTracer.getattrc                    s2   t | ddr||i |S || _t ||||S )Nr  F)r  r  rq  r  )r   r   forwardr   r   rs  rD   rE   r    s   zHFTracer.call_modulec                 C   s
   t || S r   )re  )r   r  rD   rD   rE   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 rD   )r  r  r   )r   rR  rD   rD   rE   
<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__r?   )r   r  r  r  origpatchedmodule_of_modelr+  orig_clspatched_clsr  r  r   rD   rD   rE   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 rD   r  r7   r   r   rR  rS  rD   rE   rA    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 r8  )r<   r   r   r   )rD  rD   rD   rE   to_meta  s   
zHFTracer.trace.<locals>.to_meta**rR  r  rD   r   r>  )3r  r2  r<   r   r   Moduler?  r3  r~  r  r7   r9  r   r   updater;   r  rt  r  r   r"   r   r   supported_archsr  r  r  r  r  pytreetree_mapr"  VAR_KEYWORDr  r  rQ  rq  tracegraphnodesoprR  r   r   r  OrderedDictr   r   usersreversed
erase_node)r   r  rR  rS  rT  sigparamr  r   sequence_lengthr   num_choicesr  r  r[  concrete_metasr  to_visit	to_deleter   userrs  rW  rE   rd    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  rD   rD   rE   r   H  s    zKHFTracer._stateless_mod_instanciation_depends_on_proxies.<locals>.<genexpr>)anyrK  r~  )r   r+  rD   rD   rE   /_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.
        r3   r   r   FTr.   )ru  rt  r  lowerrp  r  r  
add_module)r   r+  idxmod_namepathalready_insertedrD   rD   rE   _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)	rq  path_of_module	NameErrorr  r   r   r3  buffersr|  )r   r+  r0  rz  rs  rD   rE   r}  b  s   	.
zHFTracer.path_of_moduler   module_qualified_namec                    s   |  | ot ||S r   )ru  rq  is_leaf_module)r   r   r  rs  rD   rE   r  s  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\  )rw  r  rR  r  rg  )r   r  	attributerD   rD   rE   r;   x  s   zHFTracer.keys)NNN)NNT)4r  r  r  r  r  bool__annotations__r  rC  r   r  r   r  r   r  r  r0   r   r1   r`  r"  r  r=   r   r   r   r   r   r  rl  r>  r   r  r  r@  
contextlibcontextmanagerr   r   r^  r   rQ  r   r   rd  ru  r|  r}  r  r   r;   r  rD   rD   rs  rE   r  V  sd   
 
 1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 rD   rU  rV  rZ  rD   rE   rA    s     z%get_concrete_args.<locals>.<dictcomp>)
r  r2  r?  rE  r3  r;   r   joinr   r~  )r  r  rl  formatted_input_namesformatted_allowed_input_namesrD   rZ  rE   get_concrete_args  s   r  r   c                 C   s   | j jtv S r   )rt  r  _SUPPORTED_MODELS)r  rD   rD   rE   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  rt  r  )r  supported_model_namesrD   rD   rE   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]  )rS  r;   r   r  r  r  r  loggerwarningr  rd  r   r  r	   rt  r  r   )r  r  r  r  rR  rk  traced_graphtracedrD   rD   rE   symbolic_trace  s,   

r  r   rD   )NNr   FF)F)NN)r   )Nr   FN)r  r  N)r  r   )r  r  r  r  r  r"  r_  osr  rI  r   typingr   r   r   r   r   r   torch.utils._pytreeutils_pytreera  r   torch.fxr   r	   r
   r   r   torch.fx._compatibilityr   torch.fx._symbolic_tracer   torch.fx.proxyr   r3   r   cache_utilsr   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,   r-   import_utilsr/   r0   peftr1   
get_loggerr  r  environr>   upperr  r  r=   r   rF   (_REGULAR_SUPPORTED_MODEL_NAMES_AND_TASKSr  _REGULAR_SUPPORTED_MODELSitemr<   r   extend_SPECIAL_SUPPORTED_MODELSr<  sortedrE  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  r3  r5  r6  r7  r?  rA  rK  rS  rU  rV  rc  	Embedding
functional	embedding	LayerNorm	GroupNormLinearreluReLUwhereabsr  r   catstackrG  mulr   matmulbmmbaddbmmr  repeatrepeat_interleaverollflipindex_selectgatherConv1dConv2dsqueeze	unsqueezer:  one_hotMSELossCrossEntropyLossBCEWithLogitsLossr`  rd  r  scaled_dot_product_attentionre  rw  rn  r  r  r  r  r  r  r  r  r  r   r  r  r^  r  r  r  r  r  rD   rD   rD   rE   <module>   s  
\

)<

$






	
 !"#
/,
"   	    2



