o
    Zh                     @   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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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	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is_peft_available)	PeftModelZ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defaultZpretrainingznext-sentence-predictionz	masked-lmz	causal-lmz
seq2seq-lmzspeech-seq2seqzmultiple-choicezdocument-question-answeringzquestion-answeringzsequence-classificationztoken-classificationzmasked-image-modelingzimage-classificationzzero-shot-image-classificationZctczaudio-classification)zsemantic-segmentationZ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)r3   r4   Ztask_mappingZmodel_class_namestask
class_name r>   D/var/www/auris/lib/python3.10/site-packages/transformers/utils/fx.py%_generate_supported_model_class_namesL   sd   	


r@   )5ZaltclipZalbertZbartZbertZbitnetZ
blenderbotzblenderbot-smallZbloomZclipZconvnextZdebertaz
deberta-v2Zdinov2Z
distilbertz
donut-swinZelectraZgpt2Zgpt_neoZgptjZhieraZhubertZijepaZlayoutlmllamaZcohereZlxmertZm2m_100ZmarianZmbartmegatron-bertZmistralZmixtralZ
mobilebertZmt5ZnezhaoptZpegasusZplbartZqwen2Z	qwen2_moeZqwen3Z	qwen3_moeZresnetZrobertaZ	segformerZspeech_to_textZspeech_to_text_2ZswinZt5ZtrocrZvitZxglmZwav2vec2rA   rC   )ZCLIPTextModelZCLIPTextModelWithProjectionZCLIPVisionModelZCLIPVisionModelWithProjectionZAltCLIPTextModelZAltCLIPVisionModelZGitVisionModelGPT2DoubleHeadsModelZSpeech2Text2DecoderZ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weightrK   selfinputr>   r>   r?   torch_nn_embedding   s   *rS          @Fc                 C   s&   t jg | j|jd R d|jdS rG   )rL   rM   rN   rK   )rR   rO   Zpadding_idxZmax_normZ	norm_typeZscale_grad_by_freqsparser>   r>   r?   torch_nn_functional_embedding   s   &rV   c                 C      |S Nr>   rP   r>   r>   r?   torch_nn_layernorm      rY   c                 C   rW   rX   r>   rP   r>   r>   r?   torch_nn_groupnorm   rZ   r[   c                 C   s    t j|jd d | jf ddS )NrH   rI   rJ   )rL   rM   rN   Zout_featuresrP   r>   r>   r?   torch_nn_linear   s    r]   c                 C      | S rX   r>   xr>   r>   r?   
torch_relu   rZ   ra   c                 C   rW   rX   r>   )rQ   r`   r>   r>   r?   torch_nn_relu   rZ   rb   c                 C   s   |st d| S )Nz>Don't support in-place functional.relu for MetaTensor analysis
ValueError)r`   Zinplacer>   r>   r?   torch_nn_functional_relu   s   re   c                 C   s$   | j dd|j dd |j dd S NrI   r\   to)	conditionr`   yr>   r>   r?   torch_where   s   $rk   outc                C   s   |d urt d| S )Nz2Don't support in-place abs for MetaTensor analysisrc   )rR   rm   r>   r>   r?   	torch_abs   s   rn   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   steprK   rI   rK   rJ   )lenr8   floatintr:   rL   rM   )argskwargsnro   startendrK   r>   r>   r?   torch_arange   s"   






ry   c                  O   sR   t | } t| dkrd| d< nd|d< t|}|dd  tj| i |ddiS )Nr.   Z
fill_valuerJ   rI   )listrq   dictpoprL   full)rt   ru   Zkwargs_without_devicer>   r>   r?   
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 r>   )rN   ).0tr>   r>   r?   
<listcomp>$  s    ztorch_cat.<locals>.<listcomp>c                 3   s    | ]}|  V  qd S rX   r>   )r   rN   dimr>   r?   	<genexpr>&  s    ztorch_cat.<locals>.<genexpr>r.   rI   r\   )r   rz   sumrL   rM   )tensorsr   axisrm   ZshapesrN   Zconcatenated_dimZfinal_shaper>   r   r?   	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.   rI   r\   )r   rz   rN   insertrq   rL   rM   )r   r   r   rm   rN   r>   r>   r?   torch_stack+  s   r   )alpharm   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 )NrI   r\   r.   )r8   rL   Tensor
empty_likemaxr   rz   rN   ranger;   rM   )	rR   otherr   rm   
max_lengthZinput_shapeZother_shaperN   ir>   r>   r?   	torch_add7  s   r   c                C   s   t | ||dS )Nrl   )r   )rR   r   rm   r>   r>   r?   	torch_mulE     r   c                 C   
   t | |S rX   )r   )rQ   r   r>   r>   r?   torch_tensor_mulI     
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   rH           rI   r\   rJ   )r   sizer   rz   rN   r;   r   r|   rL   tensorrM   )
rR   r   rm   Zd1Zd2rN   r   Zshape1Zshape2r   r>   r>   r?   torch_matmulM  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 analysisrI   r\   )rd   rN   rL   rM   )rR   Zmat2rm   
batch_sizerv   m_pr>   r>   r?   	torch_bmmq  s
   r   betar   rm   c                C   s   |d urt dt||S )Nz6Don't support in-place baddbmm for MetaTensor analysis)rd   r   )rR   batch1batch2r   r   rm   r>   r>   r?   torch_baddbmmy  s   
r   c                C   s   t | |||||dS )Nr   )r   )rQ   r   r   r   r   rm   r>   r>   r?   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)rL   r   )r   operandr>   r>   r?   r     s    ztorch_einsum.<locals>.<genexpr>rI   )rL   einsumrh   )ZequationZoperandsZconcrete_operandsr>   r>   r?   torch_einsum  s   r   c                 G   s:   t | j}t|D ]\}}||  |9  < q	tj|ddS rf   )rz   rN   	enumeraterL   rM   )rQ   sizesrN   r   r`   r>   r>   r?   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   rJ   rI   )	rq   r   rz   rN   r8   rs   rL   ZnumelrM   )r   r   rt   num_argsrN   Zrepeatsr>   r>   r?   torch_repeat_interleave  s   

r   c                C   s&   t | j}t|||< tj|ddiS NrJ   rI   )rz   rN   rq   rL   rM   )rR   r   indexrm   rN   r>   r>   r?   torch_index_select  s   
r   c                 C      t | ||S rX   )r   rQ   r   r   r>   r>   r?   torch_tensor_index_select     r   )sparse_gradrm   c                C   s(   t | j}|j| ||< tj|ddiS r   )rz   rN   rL   rM   )rR   r   r   r   rm   rN   r>   r>   r?   torch_gather  s   
r   c                 C   r   rX   )r   r   r>   r>   r?   torch_tensor_gather  r   r   c                 C   r^   rX   r>   )rR   Zshiftsdimsr>   r>   r?   
torch_roll  rZ   r   c                 C   r^   rX   r>   )rR   r   r>   r>   r?   
torch_flip  rZ   r   c                 C   r^   rX   r>   )rQ   r   r>   r>   r?   torch_tensor_flip  rZ   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 )NrH   validr   r   samer   r   r.   r   rI   r\   rN   paddingrz   mathfloorZdilationZkernel_sizeZstrideZout_channelsrL   rM   )rQ   rR   Zl_inrN   r   Zl_outr>   r>   r?   torch_nn_conv1d  s   


8
r   c                 C   s   |j dd  \}}d }| j}|dkrd}|dkrt|j }|d 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.   rI   r\   r   )rQ   rR   Zh_inZw_inrN   r   Zh_outZw_outr>   r>   r?   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   )rz   rN   r   r|   r;   rL   rM   )rR   r   rN   Z	new_shapeZ	dim_valuer>   r>   r?   torch_squeeze  s   

r   c                 C   r   rX   )r   rQ   r   r>   r>   r?   torch_tensor_squeeze  r   r   c                 C   s<   t | j}|dk r|  d | }||d tj|ddS r   )rz   rN   r   r   rL   rM   )rR   r   rN   r>   r>   r?   torch_unsqueeze   s
   
r   c                 C   r   rX   )r   r   r>   r>   r?   torch_tensor_unsqueeze  r   r   c                 K   sD   t jt j| ddfi |}t|t jr|dS tt|dd S )Nr   r\   rI   c                 S   s
   |  dS NrI   rg   r_   r>   r>   r?   <lambda>  s   
 z*torch_unique_consecutive.<locals>.<lambda>)rL   unique_consecutiveZ
zeros_liker8   r   rh   tuplemap)rR   ru   outputr>   r>   r?   torch_unique_consecutive  s   
r   rH   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 analysisrI   r\   )rd   rz   rN   rL   rM   )r   Znum_classesrN   r>   r>   r?   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   rH   rI   r\   )rN   rL   rM   )	querykeyvalueZ	attn_maskZ	dropout_pZ	is_causalscaleZtarget_lengthhead_dimr>   r>   r?   0torch_nn_functional_scaled_dot_product_attention  s   

&r   c                 C   $   | j dkr	|j}nd}tj|ddS Nnone)r.   rI   r\   Z	reductionrN   rL   rM   rQ   rR   targetrN   r>   r>   r?   torch_nn_mseloss#     
r   c                 C   r   r   r   r   r>   r>   r?   torch_nn_crossentropyloss+  r   r   c                 C   r   r   r   r   r>   r>   r?   torch_nn_bcewithlogitsloss3  r   r   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\   )r8   rL   r   Z	ones_likerK   Zfloat16float32Zfloat64Zint32rh   Zint64)r   Zconcreter>   r>   r?   to_concrete<  s   z%operator_getitem.<locals>.to_concreter   r\   rI   )	r8   rL   r   r   r   operatorgetitemr   rh   )abr   r>   r>   r?   operator_getitem;  s   
r   _MANUAL_META_OVERRIDESc                       sh   e Zd ZdZdd Zedd Zedd Z fdd	Z fd
dZ	dd Z
dd Z fddZ  ZS )HFProxyzI
    Proxy that uses metadata to handle data-dependent control-flow.
    c                 C   
   || _ d S rX   )	_metadata)rQ   metadatar>   r>   r?   install_metadata  r   zHFProxy.install_metadatac                 C   s   | j dd| fi S )Ncall_methodr   )tracercreate_proxyrQ   r>   r>   r?   rN     s   zHFProxy.shapec                 C   s
   t | dS )NrJ   )MetaDeviceAttributer   r>   r>   r?   rJ     s   
zHFProxy.devicec                    s(   t | dr| jd urt| jS t  S Nr   )hasattrr   rq   super__len__r   	__class__r>   r?   r     s   

zHFProxy.__len__c                    s$   t | dr| jd ur| jS t  S r   )r   r   r   __bool__r   r   r>   r?   r    s   
zHFProxy.__bool__c                 C   s   |dkr	|  |S t| |S r   )__getattribute__HFAttribute)rQ   kr>   r>   r?   __getattr__  s   

zHFProxy.__getattr__c                 C   s   | j dtj| ||fi S Ncall_function)r   r   r   setitem)rQ   indicesvaluesr>   r>   r?   __setitem__  s   zHFProxy.__setitem__c                    s*   t | dr| jd ur|| jv S t |S r   )r   r   r   __contains__)rQ   r   r   r>   r?   r    s   
zHFProxy.__contains__)__name__
__module____qualname____doc__r   propertyrN   rJ   r   r  r  r  r  __classcell__r>   r>   r   r?   r     s    

r   c                   @   s.   e Zd ZdefddZedd Zdd ZdS )	r  attrc                 C   sB   || _ || _|j| _d | _t| j dr| t| j j| d S d S r   )rootr  r   _noder   r   getattrr   )rQ   r  r  r>   r>   r?   __init__  s   zHFAttribute.__init__c                 C   s0   | j d u r| jdtj| j| jfi j| _ | j S r  )r  r   r   builtinsr  r  r  noder   r>   r>   r?   r    s   
 zHFAttribute.nodec                 O   s   | j d| j| jf| |S )Nr   )r   r   r  r  )rQ   rt   ru   r>   r>   r?   __call__  s   zHFAttribute.__call__N)r  r  r  r9   r  r  r  r  r>   r>   r>   r?   r    s
    	
r  c                   @   s   e Zd ZdS )r   N)r  r  r  r>   r>   r>   r?   r     s    r   c                   @   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   r   rX   )_orig_cache_cls)rQ   r  r>   r>   r?   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.)r   RuntimeErrorr   _CLASSES_TO_PATCHr  r   r>   r>   r?   r    s   
zHFCacheProxy.__class__N)	r  r  r  r  typer   r  r  r  r>   r>   r>   r?   r    s
    r  functionop_typer  r   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 rX   )r8   r   r;   )r   Zfound_proxiesr>   r?   check_proxy  s   
z4create_wrapper.<locals>.wrapper.<locals>.check_proxyr   r  r   r%  zop_type z not supported.r&  )
r   rL   fxr  map_aggregaterq   r   r  rd   r   )rt   ru   r(  r   r   r#  r$  r&  r'  r?   wrapper  s"   
zcreate_wrapper.<locals>.wrapper)	functoolswraps)r#  r$  r&  r-  r>   r,  r?   create_wrapper  s   r0  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  __r   r)  )
r   __new__dirr  
startswithinspectismethod
isfunctionsetattrr0  )clsr2  r3  r4  r&  	attr_namer  r$  r   r>   r?   r6    s$   


zHFProxyableClassMeta.__new__rX   )r  r  r  r  r9   r   r"  r{   r   r   r   r
   r   r6  r  r>   r>   r   r?   r1    s    	

r1  r   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  )r0  )r   r-  r>   r>   r?   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.rI   r   zNo metadata was found for )	r8   r   rL   r*  r   r   r   r   r   )vr>   r>   r?   _proxies_to_metas"  s   
rA  r  c                    s   dt dtf fdd}|S )Nrv   r5   c                    s*   t tts	tdt| t}|  |S )NzJCannot create HFCacheProxy because there is no HFTracer currently tracing.)r8   _CURRENT_TRACERHFTracerr   r  r  )rv   Zcache_proxyr  r>   r?   cache_proxy_factory_fn.  s
   


z=create_cache_proxy_factory_fn.<locals>.cache_proxy_factory_fn)r
   r  )r  rE  r>   rD  r?   create_cache_proxy_factory_fn-  s   rF  ProxyableCacher)  ProxyableDynamicCacheProxyableSinkCacheProxyableStaticCache
      lowhighforbidden_valuesc                 C   s8   |d u rg }t | |}||v rt | |}||v s|S rX   )randomrandint)rM  rN  rO  r   r>   r>   r?   _generate_random_intQ  s   rR  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 ):rC  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zerosZonesr}   Z	full_likeeyerM   r   clampZfinfoZtrilr>   c                    s   t  j||d d S )N)autowrap_modulesautowrap_functions)r   r  )rQ   rY  rZ  r   r>   r?   r  y  r   zHFTracer.__init__modelr   
input_namerN   input_namesr5   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   rp   r`  ZXLNetForQuestionAnsweringra  rb  problem_typeNzCould not retrieve the problem type for the sequence classification task, please set model.config.problem_type to one of the following values: "regression", "single_label_classification", or "multi_label_classification".Z
regressionZsingle_label_classificationZmulti_label_classificationzExpected model.config.problem_type to be either: "regression", "single_label_classification", or "multi_label_classification", but "z" was provided.rD   rE   rF   z!Generating the dummy input named z for z is not supported yet.Zpixel_values
image_sizevision_configencodernum_channels   Zbbox   Zinput_featuresZinputs_embedsembedding_sizerB   r.   r   Zvisual_featsZ
visual_posinputsZinput_valuesi'  i N  rM  rN  maskpast_key_valuesZ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 )rp   N)rL   Zrandrr   )r   r   Zcache_shaperJ   r>   r?   r     s    
z1HFTracer._generate_dummy_input.<locals>.<genexpr>)3r  r  r  rJ   r   r$   r#   r   r   r   rL   rV  longr&   r   r)   r   configrc  rd   Z
num_labelsr   r%   r+   r   r"   r(   r'   r   NotImplementedErrorre  rd  rf  rR  r8   collectionsabcIterablerr   Zinput_feat_per_channelZ
model_typerj  Zhidden_sizerq   Zvisual_feat_dimZvisual_pos_dim"_FX_SUPPORTED_MODELS_WITH_KV_CACHEZnum_attention_headsr   r   Znum_hidden_layers)rQ   r[  r\  rN   r]  Zmodel_class_nameZinputs_dictZkv_cache_lengthr   Zlabels_shapeZlabels_dtyperd  rg  heightwidthrj  Zembedding_shaper   Z
seq_lengthZ
mask_shapeZ	num_headsr   ZpkvZshape_with_hidden_sizer>   ro  r?   _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placeholderrJ   rI   Tr  r\   r   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: ) r   r   	meta_argsr   orig_fnsrL   r*  r  r+  rA  _disable_module_getattr_disable_call_moduler   r:   r8   r   rh   r  r  r   AttributeErrorr  Zget_submoduler"  r|  splitr   rd   	Exception_IS_IN_DEBUG_MODEwarningswarn)rQ   kindr   rt   ru   r2  Z	type_exprr&  rvZ
args_metasZkwargs_metasZshould_install_metadataZmeta_targetZmeta_outmethodmodmod_typeZattr_itrZatomsZatomer   r>   r?   r   +  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 rX   r   )r  )attr_valrv   rQ   r>   r?   r     s    zLHFTracer._module_getattr.<locals>.maybe_get_proxy_for_attr.<locals>.<lambda>r%  r>   )r9  	signaturer   
parametersZparam_shapes_constant)r  Zcollection_to_searchparameter_proxy_cacher   ru   Z	val_proxyr   )r  rv   r?   maybe_get_proxy_for_attrv  s   z:HFTracer._module_getattr.<locals>.maybe_get_proxy_for_attr)
r  r8   rL   r   	Parameterr  Znamed_parametersrS  r   Znamed_buffers)rQ   r  r  r  r  Zmaybe_parameter_proxyZmaybe_buffer_proxyr>   r   r?   _module_getattrq  s    zHFTracer._module_getattrr  r  r  c                 C   s   |  |||S rX   )r  )rQ   r  r  r  r>   r>   r?   r    r   zHFTracer.getattrc                    s2   t | ddr||i |S || _t ||||S )Nr  F)r  r|  r   r{  )rQ   r   forwardrt   ru   r   r>   r?   r{    s   zHFTracer.call_modulec                 C   s
   t || S rX   )r   )rQ   r  r>   r>   r?   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 r>   )r?  r  rL   )r   r   r>   r>   r?   
<dictcomp>  s    z.HFTracer.patch_for_tracing.<locals>.<dictcomp>Ztransformers)_TORCH_METHODS_TO_PATCHZpatched_torch_methodssetr  itemsr<  rL   addr9  	getmodulesysmodulesr8  r!  __dict__r;   )rQ   r  r2  r-  origZpatchedZmodule_of_modelr  Zorig_clsZpatched_clsr>  r  r   r>   r>   r?   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 r>   r2  r6   r   r   r  r  r>   r?   r    s
    z"HFTracer.trace.<locals>.<dictcomp>r   r_  rl  r.   )Z_deserialize_graph_moduleZ_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 r   )r8   rL   r   rh   )r   r>   r>   r?   to_meta  s   
zHFTracer.trace.<locals>.to_meta**r  rz  r>   r   r   )3r9  r  r8   rL   r   Moduler  r  r  r2  r6   r  rM   rd   updater7   rR  r  r  r   r#   r   r{   supported_archsr"  r  r8  ry  r   pytreeZtree_mapr  VAR_KEYWORDr~  rB  r  r   tracegraphnodesopr   rt   r   rs  OrderedDictr|   rz   ZusersreversedZ
erase_node)rQ   r  r  r  r  sigparamr]  r   Zsequence_lengthrN   Znum_choicesrk  r\  r  Zconcrete_metasr  Zto_visitZ	to_deleterv   userr   r  r?   r    s    



zHFTracer.tracer  c                 C   s   t dd |j D S )z
        Whether the module was instantiated with Proxies. If that is the case, such module cannot be a leaf module
        because its attributes are input-dependent.
        c                 s   s    | ]}t |tV  qd S rX   )r8   r   )r   r  r>   r>   r?   r   L  s    zKHFTracer._stateless_mod_instanciation_depends_on_proxies.<locals>.<genexpr>)anyr  r  )rQ   r  r>   r>   r?   /_stateless_mod_instanciation_depends_on_proxiesG  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.
        r2   r   r   FTr.   )r  r  r  lowerr   r  r  Z
add_module)rQ   r  idxmod_namepathZalready_insertedr>   r>   r?   _insert_module_as_submoduleN  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)	r   path_of_module	NameErrorrT  rq   rz   r  buffersr  )rQ   r  r  r  r   r>   r?   r  f  s   	.
zHFTracer.path_of_moduler   module_qualified_namec                    s   |  | ot ||S rX   )r  r   is_leaf_module)rQ   r   r  r   r>   r?   r  w  s   zHFTracer.is_leaf_module)Z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.
        r7   r  )r  r  r   r8  r   )rQ   r  	attributer>   r>   r?   r7   |  s   zHFTracer.keys)NNN)NNT)6r  r  r  r  rS  bool__annotations__rT  r  r   rG  r   rH  r   rI  r   rJ  r!  r0   r   r1   r  r   r  r9   rz   rs   r{   rL   r   ry  r   r  r   r  r{  r  
contextlibcontextmanagerr   r   r  r   r  r   r   r  r  r  r  r  r   r7   r  r>   r>   r   r?   rC  Z  sf   
 
 0F&$'rC  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 r>   r  r  r  r>   r?   r    s     z%get_concrete_args.<locals>.<dictcomp>)
r9  r  r  r  r  r7   rq   joinrd   r  )r[  r]  r  Zformatted_input_namesZformatted_allowed_input_namesr>   r  r?   get_concrete_args  s   r  r   c                 C   s   | j jtv S rX   )r  r  _SUPPORTED_MODELS)r[  r>   r>   r?   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  rr  r  r  )r[  Zsupported_model_namesr>   r>   r?   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"])
        ```
    Nrn  	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  )r  r7   rz   r  r  r  rq  loggerwarningr  r  rL   r*  r	   r  r^  rJ   )r[  r]  r  r  r  r   Ztraced_graphZtracedr>   r>   r?   symbolic_trace  s,   

r  rX   r>   )NNrT   FF)F)NN)rH   )Nr   FN)rK  rL  N)r[  r   )r  rs  r  r.  r9  r   r   osrP  r  r  typingr   r   r   r   r   rL   Ztorch.utils._pytreeutilsZ_pytreer  r   Ztorch.fxr   r	   r
   r   r   Ztorch.fx._compatibilityr   Ztorch.fx._symbolic_tracer   Ztorch.fx.proxyr   r2   r   Zcache_utilsr   r   r   r   Zmodeling_utilsr   r   Zmodels.autor   Zmodels.auto.modeling_autor   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   Zimport_utilsr/   r0   Zpeftr1   Z
get_loggerr  r  environr:   upperr  r"  r9   rz   r@   Z(_REGULAR_SUPPORTED_MODEL_NAMES_AND_TASKSrv  Z_REGULAR_SUPPORTED_MODELSitemr8   r{   extendZ_SPECIAL_SUPPORTED_MODELSr   sortedr  r  rB  rS   rV   rY   r[   r]   ra   rb   re   rk   rn   ry   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   Z	EmbeddingZ
functionalZ	embeddingZ	LayerNormZ	GroupNormZLinearZreluZReLUwhereabsrU  r}   catstackr  mulr   matmulZbmmZbaddbmmr   repeatZrepeat_interleaveZrollflipZindex_selectgatherZConv1dZConv2dZsqueezeZ	unsqueezer   Zone_hotZMSELossZCrossEntropyLossZBCEWithLogitsLossr   r   r  Zscaled_dot_product_attentionr   r  r   r  r0  r1  r?  rA  rF  rG  rH  rI  rJ  rs   rR  rC  r  r  r  r  r  r  r>   r>   r>   r?   <module>   s  
X

)9

$






	
 !"#
/,
$   	    2



