
    sgV                        U d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlZd dl	Z	d dl
Z
d dlmZmZmZmZmZmZmZmZmZ d dlZd dlmc mZ d dlmZ d dlmZmZmZmZm Z  d dl!m"Z" d dl#m$Z$ d dl%m&Z& dd	l'm(Z( dd
l)m*Z*m+Z+m,Z,m-Z- ddl.m/Z/m0Z0 ddl1m2Z2 ddl3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z=m>Z>m?Z?m@Z@mAZAmBZBmCZCmDZDmEZEmFZFmGZG ddlHmIZI ddlJmKZKmLZLmMZMmNZNmOZO  eN       rd dlPmQZQ  e(j                  eS      ZTej                  j                  dd      j                         eKv ZX	 dydee/   deeeYeeY   f      deeY   fdZZg dZ[ddgZ\g Z]e[D ]:  Z^ e_e^e`      re]j                   eZdzi e^       $e]j                   eZe^             < g dZb ec ed eee]ebz                     Zfdagd Zh	 d{dZid Zjd  Zkd! Zld" Zmd# Znd|d$Zod% Zpdd&d'Zqd( Zrd) Zsd}dd&d*Ztd}dd&d+Zuddd,d-Zvdd&d.Zwd/ Zxdd&d0Zydd&d1Zzdddd2d3Z{dddd2d4Z|d5 Z}d6 Z~ddd7d8Zdd&d9Zd: Zddd;d<Zd= Zdyd>Zd? Zd@ ZdA ZdB ZdydCZdydDZdE ZdF ZdG Zd~dHZ	 ddIZdJ ZdK ZdL ZdM Zi ej4                  j(                  ehej4                  j*                  j,                  eiej4                  j.                  ejej4                  j0                  ekej4                  j2                  elej4                  emej4                  j*                  j4                  eoej4                  j6                  enej8                  epej:                  eqej<                  erej>                  esej@                  etejB                  euejD                  evejF                  ewejH                  jF                  exi ejJ                  eyejL                  ezejN                  e{ejH                  jN                  e|ejP                  e}ejH                  jR                  e~ejT                  eejV                  eejX                  eejH                  jX                  eejZ                  eejH                  jZ                  eej\                  eejH                  j\                  eej4                  j^                  eej4                  j`                  eejb                  eejH                  jb                  eejd                  eejH                  jd                  eejf                  eej4                  j*                  jh                  eej4                  jj                  eej4                  jl                  eej4                  jn                  eejp                  ei	Zeeef   edN<   eIr%eeej4                  j*                  jv                  <    G dO dPe      Z G dQ dRe      Z G dS dTe      Z G dU dVe      Z	 dydWedXeedY   edZ   ed[   f   d\eeegef      defd]Z G d^ d_e      Zd`edeeef   fdaZdb Zdcee*   deegef   fddZ edee*fi  ee*      f      Z edge+fi  ee+      f      Z edhe,fi  ee,      f      Z edie-fi  ee-      f      Zddjedkedleee      fdmZ G dn doe       Zdpej                  dqeeY   fdrZddtZdduZddefdpdsdqeeeY      dvedwee   def
dxZy)    N)	AnyCallableDictListLiteralOptionalTupleTypeUnion)nn)GraphGraphModuleNodeProxyTracer)compatibility)is_fx_tracingParameterProxy   )logging)CacheDynamicCache	SinkCacheStaticCache)PretrainedConfigPreTrainedModel)
get_values),MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING_NAMES MODEL_FOR_BACKBONE_MAPPING_NAMES!MODEL_FOR_CAUSAL_LM_MAPPING_NAMESMODEL_FOR_CTC_MAPPING_NAMES3MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING_NAMES,MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING_NAMESMODEL_FOR_IMAGE_MAPPING_NAMES-MODEL_FOR_MASKED_IMAGE_MODELING_MAPPING_NAMES!MODEL_FOR_MASKED_LM_MAPPING_NAMES'MODEL_FOR_MULTIPLE_CHOICE_MAPPING_NAMES0MODEL_FOR_NEXT_SENTENCE_PREDICTION_MAPPING_NAMES#MODEL_FOR_PRETRAINING_MAPPING_NAMES*MODEL_FOR_QUESTION_ANSWERING_MAPPING_NAMES-MODEL_FOR_SEMANTIC_SEGMENTATION_MAPPING_NAMES,MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING_NAMES/MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING_NAMES(MODEL_FOR_SPEECH_SEQ_2_SEQ_MAPPING_NAMES,MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING_NAMES6MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING_NAMESMODEL_MAPPING_NAMES)"is_torch_greater_or_equal_than_2_0   )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                    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}||j)                         }t+        |t,              r|g}g }|D ]+  }||   j/                  | d       }|s|j1                  |       - |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)r2   r*   r)   r'   r!   r-   r/   r(   r#   r+   r.   r0   r&   r$   r1   r"   r   r,   r    r%   keys
isinstancestrgetappend)r=   r>   task_mappingmodel_class_namestask
class_names         H/var/www/html/venv/lib/python3.12/site-packages/transformers/utils/fx.py%_generate_supported_model_class_namesrO   Q   s`   &: 	#$T 	6	
 	6 	B 	B 	B 	&'Z 	H 	"#R 	 L 	 !N 	 L 	)*`  	*!" 	 L#$ "O4$A)L. &++-/3'*+ 1!$'++J=
$$Z01
     )2altclipalbertbartbert
blenderbotzblenderbot-smallbloomclipconvnextdebertaz
deberta-v2dinov2
distilbertz
donut-swinelectragpt2gpt_neogptjhierahubertijepalayoutlmllamacoherelxmertm2m_100marianmbartmegatron-bertmistralmixtral
mobilebertmt5nezhaoptpegasusplbartqwen2	qwen2_moeresnetroberta	segformerspeech_to_textspeech_to_text_2swint5trocrvitxglmwav2vec2rd   rp   )CLIPTextModelCLIPTextModelWithProjectionCLIPVisionModelCLIPVisionModelWithProjectionAltCLIPTextModelAltCLIPVisionModelGitVisionModelGPT2DoubleHeadsModelSpeech2Text2DecoderTrOCRDecoderPeftModelForCausalLMPeftModelForSeq2SeqLMc                     t        j                  g |j                  | j                  j                  d   d| j                  j                  dS Nmeta)devicedtype)torchemptyshapeweightr   selfinputs     rN   torch_nn_embeddingr      s@    ;;ccT[[%6%6r%:c6QUQ\Q\QbQbccrP   Fc                 z    t        j                  g | j                  |j                  d   d|j                  dS r   )r   r   r   r   )r   r   padding_idxmax_norm	norm_typescale_grad_by_freqsparses          rN   torch_nn_functional_embeddingr      s3     ;;YYV\\"%5YfFLLYYrP   c                     |S N r   s     rN   torch_nn_layernormr          LrP   c                     |S r   r   r   s     rN   torch_nn_groupnormr      r   rP   c                 f    t        j                  |j                  d d | j                  fz   d      S )Nr   r   r   )r   r   r   out_featuresr   s     rN   torch_nn_linearr      s,    ;;u{{3B'4+<+<*>>vNNrP   c                     | S r   r   xs    rN   
torch_relur          HrP   c                     |S r   r   )r   r   s     rN   torch_nn_relur      r   rP   c                      |st        d      | S )Nz>Don't support in-place functional.relu for MetaTensor analysis
ValueError)r   inplaces     rN   torch_nn_functional_relur      s    YZZHrP   c                 r    | j                  d      |j                  d      z   |j                  d      z   S Nr   r   to)	conditionr   ys      rN   torch_wherer      s5     <<v<&V)<<qtt6t?RRRrP   outc                     |t        d      | S )Nz2Don't support in-place abs for MetaTensor analysisr   )r   r   s     rN   	torch_absr      s    
MNNLrP   c                  |   t        |       }d}|dk(  rd}| d   }n|dk(  r| \  }}n| \  }}}t        |t              rt        |      }t        |t              rt        |      }t        |t              rt        |      }|j	                  d|      }|j	                  d      }t        j                  ||z
  |z  |d      S )Nr4   r   r   stepr   r   r   r   )lenrF   floatintrH   r   r   )argskwargsnr   startendr   s          rN   torch_aranger      s    D	ADAv1g	
a
ssD%E
#uC$4y::fd#DJJwE;;e,E&IIrP   c                      t        |       } t        |       dkD  rd| d<   nd|d<   t        |      }|j                  dd        t	        j
                  | i |ddiS )Nr4   
fill_valuer   r   )listr   dictpopr   full)r   r   kwargs_without_devices      rN   
torch_fullr     s]    :D
4y1}Q | Lh-::tD4DVDDrP   c                $   |d||dk  r| d   j                         z   | D cg c]  }|j                   }}t        |d         }t        fd|D              }|d  |gz   |dz   d  z   }t	        j
                  |d      S c c}w )Nr   c              3   (   K   | ]	  }|     y wr   r   ).0r   dims     rN   	<genexpr>ztorch_cat.<locals>.<genexpr>(  s     :%5::s   r4   r   r   )r   r   r   sumr   r   )	tensorsr   axisr   tshapesr   concatenated_dimfinal_shapes	    `       rN   	torch_catr     s    
{t|
{t'
Qwajnn$&'!agg'F'OE:6::+!1 22U3795EEK;;{622	 (s   Bc                    ||d}|||}|dk  r| d   j                         dz   |z   }t        | d   j                        }|j                  |t	        |              t        j                  |d      S Nr   r4   r   r   )r   r   r   insertr   r   r   )r   r   r   r   r   s        rN   torch_stackr   -  sw    
{t|
{t'
Qwajnn"S(!!"E	LLc'l#;;uV,,rP   )alphar   c                ^   t        | t        j                        st        j                  |d      S t        |t        j                        st        j                  | d      S t	        | j                         |j                               }t        | j                        dg|| j                         z
  z  z   }t        |j                        dg||j                         z
  z  z   }g }t        |      D ]#  }|j                  t	        ||   ||                % t        j                  |d      S )Nr   r   r4   )rF   r   Tensor
empty_likemaxr   r   r   rangerI   r   )	r   otherr   r   
max_lengthinput_shapeother_shaper   is	            rN   	torch_addr   9  s    eU\\*f55eU\\*f55UYY[%))+.Ju{{#qcZ%))+-E&FFKu{{#qcZ%))+-E&FFKE: :SQQ89:;;uV,,rP   c                    t        | ||      S )Nr   )r   )r   r   r   s      rN   	torch_mulr   G  s    UEs++rP   c                     t        | |      S r   )r   )r   r   s     rN   torch_tensor_mulr   K  s    T5!!rP   c                   | j                         }|j                         }d }|dk(  r	|dk(  rd }n|dk(  r)|dk(  r$| j                  d      |j                  d      f}n\|dk(  r|dk(  r|j                  d      f}n>|dk(  r|dk(  r| j                  d      f}n t        | j                         |j                               }t        | j                        }t        |j                        }|dk(  rdg|z   }|dk(  r|j                  d       dg||z
  z  t        | j                        z   }dg||z
  z  t        |j                        z   }g }t        |      D ]#  }	|j                  t        ||	   ||	                % |d   |d<   |d   |d<   |dk(  r|j                  d       |dk(  r|j                  d       |t        j                  dd      S t        j                  |d	diS )
Nr4   r   r   r           r   r   r   )r   sizer   r   r   rI   r   r   r   tensorr   )
r   r   r   d1d2r   r   shape1shape2r   s
             rN   torch_matmulr  O  s   	B	BE	Qw27	qR1WA

1.	qR1WA 	qR1WA eiik2
ekk"ekk"7S6\F7MM!b)D,==b)D,==z" 	4ALLVAYq	23	42Jb	2Jb	7IIbM7IIbM}||C//;;-f--rP   c                    |t        d      | j                  \  }}}|j                  \  }}}t        j                  |||d      S )Nz2Don't support in-place bmm for MetaTensor analysisr   r   )r   r   r   r   )r   mat2r   
batch_sizer   m_ps           rN   	torch_bmmr  s  sG    
MNN{{J1jjGAq!;;z1a77rP   betar   r   c                4    |t        d      t        ||      S )Nz6Don't support in-place baddbmm for MetaTensor analysis)r   r  )r   batch1batch2r
  r   r   s         rN   torch_baddbmmr  {  s     
QRRVV$$rP   c                $    t        | |||||      S )Nr	  )r  )r   r  r  r
  r   r   s         rN   torch_tensor_baddbmmr    s    vvD3OOrP   c                 ^    d |D        }t        j                  | g| j                  d      S )Nc              3   J   K   | ]  }t        j                  |d         yw)cpur   N)r   r   )r   operands     rN   r   ztorch_einsum.<locals>.<genexpr>  s      YW))'%@@Ys   !#r   )r   einsumr   )equationoperandsconcrete_operandss      rN   torch_einsumr    s.    YPXY<<5#4588@@rP   c                     t        | j                        }t        |      D ]  \  }}||xx   |z  cc<    t        j                  |d      S r   )r   r   	enumerater   r   )r   sizesr   r   r   s        rN   torch_tensor_repeatr    sF    E%  1aA;;uV,,rP   )r   output_sizec                    t        |      }|dk(  r||n|d   j                         g}nt        |d   j                        }| |dkD  r|d   } nt        |      g}d} |d   }t	        |t
              st        j                  |      dk(  r|| xx   t        |      z  cc<   n||n|j                         || <   t        j                  |ddiS )Nr4   r   r   r   r   )	r   r   r   r   rF   r   r   numelr   )r   r  r   num_argsr   repeatss         rN   torch_repeat_interleaver#    s    4yH1} + 7T!W[[]KT!W]]#;!|1gUq'gs#u{{7';q'@#J#g,&J(3(?W[[]E#J;;-f--rP   c                r    t        | j                        }t        |      ||<   t        j                  |ddiS Nr   r   )r   r   r   r   r   )r   r   indexr   r   s        rN   torch_index_selectr'    s1    EUE#J;;-f--rP   c                     t        | ||      S r   )r'  r   r   r&  s      rN   torch_tensor_index_selectr*    s    dC//rP   )sparse_gradr   c                z    t        | j                        }|j                  |   ||<   t        j                  |ddiS r%  )r   r   r   r   )r   r   r&  r+  r   r   s         rN   torch_gatherr-    s6    ES!E#J;;-f--rP   c                     t        | ||      S r   )r-  r)  s      rN   torch_tensor_gatherr/    s    c5))rP   c                     | S r   r   )r   shiftsdimss      rN   
torch_rollr3    r   rP   c                     | S r   r   )r   r2  s     rN   
torch_flipr5    r   rP   c                     | S r   r   )r   r2  s     rN   torch_tensor_flipr7    s    KrP   c                    |j                   d   }d }| j                  }|dk(  rd}|dk(  rt        |j                         }|qt        |j                         }t        j                  |d|d   z  z   | j
                  d   | j                  d   dz
  z  z
  dz
  | j                  d   z  dz         }||d<   | j                  |d<   t        j                  |d	
      S )Nr   validr   r   samer   r   r4   r   r   r   r   paddingr   mathfloordilationkernel_sizestrideout_channelsr   r   )r   r   l_inr   r=  l_outs         rN   torch_nn_conv1drF    s    ;;r?DEllG'&U[[!}U[[!

A
N"T]]1%59I9I!9Lq9P%QQTUUY]YdYdefYggjkk
 b	!!E"I;;uV,,rP   c                 h   |j                   dd  \  }}d }| j                  }|dk(  rd}|dk(  rt        |j                         }|t        |j                         }t        j                  |d|d   z  z   | j
                  d   | j                  d   dz
  z  z
  dz
  | j                  d   z  dz         }t        j                  |d|d   z  z   | j
                  d   | j                  d   dz
  z  z
  dz
  | j                  d   z  dz         }||g|dd  | j                  |d<   t        j                  |d	
      S )Nr   r9  r:  r;  r   r   r4   r   r   r<  )r   r   h_inw_inr   r=  h_outw_outs           rN   torch_nn_conv2drM    sK   RS!JD$EllG'&U[[!}U[[!

A
N"T]]1%59I9I!9Lq9P%QQTUUY]YdYdefYggjkk
 

A
N"T]]1%59I9I!9Lq9P%QQTUUY]YdYdefYggjkk
 U^bc
!!E"I;;uV,,rP   c                    t        | j                        }|2|dk  r| j                         |z   }||   dk(  r4|j                  |       n"g }|D ]  }|dk(  r	|j	                  |        |}t        j                  |d      S r   )r   r   r   r   rI   r   r   )r   r   r   	new_shape	dim_values        rN   torch_squeezerQ    s    E
7))+#C:?IIcN	 	(IA~Y'	( ;;uV,,rP   c                     t        | |      S r   )rQ  r   r   s     rN   torch_tensor_squeezerT    s    s##rP   c                     t        | j                        }|dk  r| j                         dz   |z   }|j                  |d       t	        j
                  |d      S r   )r   r   r   r   r   r   )r   r   r   s      rN   torch_unsqueezerV    sJ    E
QwiikAo#	LLa;;uV,,rP   c                     t        | |      S r   )rV  rS  s     rN   torch_tensor_unsqueezerX  
  s    4%%rP   c                     t        j                  t        j                  | d      fi |}t        |t         j                        r|j                  d      S t        t        |d             S )Nr  r   r   c                 $    | j                  d      S Nr   r   r   s    rN   <lambda>z*torch_unique_consecutive.<locals>.<lambda>  s    144< rP   )r   unique_consecutive
zeros_likerF   r   r   tuplemap)r   r   outputs      rN   torch_unique_consecutiverb    sT    %%e&6&6uU&KVvVF&%,,'yy  S!7899rP   c                     |dk  rt        d      t        | j                        |gz   }t        j                  |d      S )Nr   zEDon't support automatic num_classes inference for MetaTensor analysisr   r   )r   r   r   r   r   )r   num_classesr   s      rN   torch_nn_functional_one_hotre    s;    Q`aa+.E;;uV,,rP   c                     | j                   d   }|j                   d   }t        j                  g | j                   d d ||d      S )Nr   r   r   r   )r   r   r   )	querykeyvalue	attn_mask	dropout_p	is_causalscaletarget_lengthhead_dims	            rN   0torch_nn_functional_scaled_dot_product_attentionrp    sJ     KKOM{{2H;;CSb)C=C(CFSSrP   c                 l    | j                   dk(  r|j                  }nd}t        j                  |d      S Nnone)r4   r   r   	reductionr   r   r   r   r   targetr   s       rN   torch_nn_mselossrx  %  -    ~~;;uV,,rP   c                 l    | j                   dk(  r|j                  }nd}t        j                  |d      S rr  rt  rv  s       rN   torch_nn_crossentropylossr{  -  ry  rP   c                 l    | j                   dk(  r|j                  }nd}t        j                  |d      S rr  rt  rv  s       rN   torch_nn_bcewithlogitslossr}  5  ry  rP   c                 8   d }t        | t        j                        rht        |t              rt        t	        ||            }n ||      }t        j                  t        j                  | d      |      j                  d      S t        j                  | |      S )Nc                 >   t        | t        j                        rt        j                  | d      }|j                  t        j
                  t        j                  t        j                  t        j                  fv r|j                  t        j                        }|S | S )Nr  r   )rF   r   r   	ones_liker   float16float32float64int32r   int64)r   concretes     rN   to_concretez%operator_getitem.<locals>.to_concrete>  sa    a&q7H~~%--u{{![[#;;u{{3OrP   r  r   r   )	rF   r   r   r_  r`  operatorgetitemr   r   )abr  s      rN   operator_getitemr  =  sy     !U\\"ac+q)*AAA 0 05 A1EHHPPAq!!rP   _MANUAL_META_OVERRIDESc                   h     e Zd ZdZd Zed        Zed        Z fdZ fdZ	d Z
d Z fd	Z xZS )
HFProxyzI
    Proxy that uses metadata to handle data-dependent control-flow.
    c                     || _         y r   )	_metadata)r   metadatas     rN   install_metadatazHFProxy.install_metadata  s	    !rP   c                 @    | j                   j                  dd| fi       S )Ncall_methodr   )tracercreate_proxyr   s    rN   r   zHFProxy.shape  s    {{''vwKKrP   c                     t        | d      S )Nr   )MetaDeviceAttributer  s    rN   r   zHFProxy.device  s     #422rP   c                 z    t        | d      r!| j                  t        | j                        S t        |          S Nr  )hasattrr  r   super__len__r   	__class__s    rN   r  zHFProxy.__len__  s2    4%$..*Dt~~&&w  rP   c                 h    t        | d      r| j                  | j                  S t        |          S r  )r  r  r  __bool__r  s    rN   r  zHFProxy.__bool__  s.    4%$..*D>>!w!!rP   c                 F    |dk(  r| j                  |      S t        | |      S r  )__getattribute__HFAttribute)r   ks     rN   __getattr__zHFProxy.__getattr__  s*    ((++ 4##rP   c                 `    | j                   j                  dt        j                  | ||fi       S Ncall_function)r  r  r  setitem)r   indicesvaluess      rN   __setitem__zHFProxy.__setitem__  s-    {{''9I9IDRY[aKbdfggrP   c                 n    t        | d      r| j                  || j                  v S t        |   |      S r  )r  r  r  __contains__)r   rh  r  s     rN   r  zHFProxy.__contains__  s5    4%$..*D$..((w#C((rP   )__name__
__module____qualname____doc__r  propertyr   r   r  r  r  r  r  __classcell__r  s   @rN   r  r    sV    " L L 3 3
!
"
$h) )rP   r  c                   .    e Zd ZdefdZed        Zd Zy)r  attrc                     || _         || _        |j                  | _        d | _        t	        | j                   d      r0| j                  t        | j                   j                  |             y y r  )rootr  r  _noder  r  getattrr  )r   r  r  s      rN   __init__zHFAttribute.__init__  sT    		kk
499k*!!'$))*=*=t"DE +rP   c                     | j                   Q| j                  j                  dt        j                  | j
                  | j                  fi       j                  | _         | j                   S r  )r  r  r  builtinsr  r  r  noder  s    rN   r  zHFAttribute.node  sT     ::11/8CSCSVZV_V_aeajajUkmopuuDJzzrP   c                 n    | j                   j                  d| j                  | j                  f|z   |      S )Nr  )r  r  r  r  )r   r   r   s      rN   __call__zHFAttribute.__call__  s.    {{''tyy499,QUBUW]^^rP   N)r  r  r  rG   r  r  r  r  r   rP   rN   r  r    s*    F3 F  _rP   r  c                       e Zd Zy)r  N)r  r  r  r   rP   rN   r  r    s    rP   r  c                   2    e Zd ZdZdee   fdZed        Zy)HFCacheProxyzP
    Proxy that represents an instance of `transformers.cache_utils.Cache`.
    orig_cache_clsc                     || _         y r   )_orig_cache_cls)r   r  s     rN   install_orig_cache_clsz#HFCacheProxy.install_orig_cache_cls  s
    -rP   c                 v    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  s    rN   r  zHFCacheProxy.__class__  s4    t./`aa{{,,T-A-ABBrP   N)	r  r  r  r  r
   r   r  r  r  r   rP   rN   r  r    s-    .T%[ . C CrP   r  functionop_typer  r  get_attrproxy_factory_fnc                 J     t        j                          fd       }|S )Nc                     t               s | i |S g fd}t        j                  j                  j	                  | |       t        j                  j                  j	                  ||       t              dkD  r`d   j                  }dk(  r}n3dk(  rj                  }n!dk(  rj                  }nt        d d      |j                  || |      S  | i |S )	Nc                 J    t        | t              rj                  |        y y r   )rF   r   rI   )r  found_proxiess    rN   check_proxyz4create_wrapper.<locals>.wrapper.<locals>.check_proxy  s     !U#$$Q' $rP   r   r  r  r  zop_type z not supported.r  )
r   r   fxr  map_aggregater   r  r  r   r  )	r   r   r  r  rw  r  r  r  r  s	        @rN   wrapperzcreate_wrapper.<locals>.wrapper  s    T,V,,	( 	##D+6##FK8}!"1%,,F/)!M)!**J&!** 8G9O!DEE&&wfWg&hhT,V,,rP   )	functoolswraps)r  r  r  r  s   ``` rN   create_wrapperr    s'    
 __X- -6 NrP   c                   ^     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Z xZS )	HFProxyableClassMetazW
    Metaclass that creates a class with its main methods wrapped to be proxyable.
    namebases.attrsr  c           
      @   t         |   | |||      } t        |       D ]|  }t        | |d       }||dk(  rd}nF|j	                  d      rd }n2t        j                  |      rd}nt        j                  |      rd}nd }|dt        | |t        |||             ~ | S )Nr  r  __r  r  )
r  __new__dirr  
startswithinspectismethod
isfunctionsetattrr  )	clsr  r  r  r  	attr_namer  r  r  s	           rN   r  zHFProxyableClassMeta.__new__  s     goc46S 	jI3	40D|J&)%%d+!!$')##D)'"YtWWg(hi	j  
rP   r   )r  r  r  r  rG   r	   r
   r   r   r   r   r   r   r  r  r  s   @rN   r  r    s_     ?C T3Y CH~	
 #8TFEM#:; rP   r  rw  c                 "    t        | d      }|| fS )zo
    Wraps `target` to be proxyable. Used for tensor creators like `torch.ones`, `torch.arange` and so on.
    r  )r  )rw  r  s     rN   gen_constructor_wrapperr    s     V_5GF?rP   c                     t        | t              ryt        | t        j                  j                        r6t        | 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   r  zNo metadata was found for )	rF   r  r   r  r   r  r  r  r  )vs    rN   _proxies_to_metasr  %  sW    !()!UXX^^$1g&71k+B!;A3?@@{{HrP   r  c                 ,     dt         dt        f fd}|S )Nr   r?   c                     t        t        t              st        d      t	        | t              }|j                         |S )NzJCannot create HFCacheProxy because there is no HFTracer currently tracing.)rF   _CURRENT_TRACERHFTracerr  r  r  )r   cache_proxyr  s     rN   cache_proxy_factory_fnz=create_cache_proxy_factory_fn.<locals>.cache_proxy_factory_fn1  s9    /84kll"1o6**>:rP   )r   r  )r  r  s   ` rN   create_cache_proxy_factory_fnr  0  s    $ <  "!rP   ProxyableCacher  ProxyableDynamicCacheProxyableSinkCacheProxyableStaticCachelowhighforbidden_valuesc                 x    |g }t        j                  | |      }||v rt        j                  | |      }||v r|S r   )randomrandint)r  r   r  ri  s       rN   _generate_random_intr  T  sG    NN3%E
#
#sD) #
#LrP   c                       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	Zdd	d
edee   dee   deeej:                  f   f
dZd) fd	Zd Z dede!deee!f   fdZ" fdZ#d Z$e%jL                  de'ejP                  jR                  e*de!f   f   fd       Z+	 	 	 d*de'ejP                  jR                  e*de!f   f   de,eee!f      de,eee!f      dede-f
 fdZ.de(jR                  defdZ/de(jR                  defd Z0de(jR                  def fd!Z1d"ejP                  jR                  d#edef fd$Z2 e3d%      d&d'de!fd(       Z4 x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trilr   c                 x    t         |   ||       t               st        dt	                dt
         d      y )N)autowrap_modulesautowrap_functionsz6Found an incompatible version of torch. Found version z, but only version z is supported.)r  r  r9   ImportErrorr7   r6   )r   r  r  r  s      rN   r  zHFTracer.__init__|  sK    *:Oab$&HIZI\H]]p,-^=  'rP   modelr   
input_namer   input_namesr?   c                    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<   |S |g t	        t              t	        t              dv rTt        j                  |t        j                        |d<   t        j                  |t        j                        |d	<   |S |t	        t              v r&t!        |j"                  d
      r|j"                  j$                  t'        d      |j"                  j$                  dk(  r)||j"                  j(                  f}	t        j*                  }
n|j"                  j$                  dk(  r|f}	t        j                  }
ne|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	        t0              t	        t2              t	        t4              t	        t6              dddv r+t        j                  |t        j                        |d<   |S |g t	        t8              v r+t        j                  |t        j*                        |d<   |S t;        d| d| d      d|v r|d   }t        |j"                  dd      }|t!        |j"                  d      r!|j"                  j<                  j>                  }nKt!        |j"                  d      r!|j"                  j@                  j>                  }ntC               tC               f}t        |j"                  dd      }tE        |tF        jH                  jJ                        s||f}|\  }}t        j                  ||||t        j*                        ||<   |S d|v r.t        j                  g |dt        jL                  d||<   |S d |v rBt        j                  g ||j"                  jN                  t        jL                  d||<   |S d!|v r|d   }t        |j"                  d"d      0|j"                  jP                  d#k7  r|j"                  jR                  }n|j"                  jT                  }tW        |      dk(  r||d$   |d%   |f}n||d$   |f}t        j                  |t        jL                        ||<   |S d&|v rCt        j                  ||j"                  jX                  gz   t        jL                        ||<   |S d'|v rCt        j                  ||j"                  jZ                  gz   t        jL                        ||<   |S d(|v r)t        j                  |t        jL                  d||<   |S d)|v r>|\  }}tC        d*d+,      }t        j                  ||t        jL                        ||<   |S d-|v r?d.|v r|d   |d$   |z   g}n|}t        j                  |t        j                        ||<   |S d/|v r+t        j                  |t        j                        ||<   |S d.|v r|j"                  jP                  t\        vr#t;        d0|j"                  jP                   d1      |j"                  j^                  }|j"                  jT                  |j"                  j^                  z  }|d   |||fta        fd2tc        |j"                  jd                        D              }|||<   |S ||j"                  jT                  gz   }t        j                  |t        jL                        ||<   |S )3z4Generates 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_sizerj   r4   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      K   | ]P  }t        j                  t         j                         t        j                  t         j                         f R yw)r   N)r   randr   )r   r   cache_shaper   s     rN   r   z1HFTracer._generate_dummy_input.<locals>.<genexpr>&  sE      
  JJ{%++fMJJ{%++fMs   AA)3r  r  r  r   r   r)   r(   r$   r    r   r   r
  longr+   r#   r.   r  configr  r   
num_labelsr  r*   r0   r!   r'   r-   r,   r"   NotImplementedErrorr%  r$  r&  r  rF   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_headsro  pkvshape_with_hidden_sizer8  r   s                           @@rN   _generate_dummy_inputzHFTracer._generate_dummy_input  s*    #5*EuW``
 GGqJ $LM$CD$ HI$ <=	$
 HI$  ).JejjY_(`H%j i " &FG&OP& ,& 
 27Zuzzbh1i-./4{{:UZZ`f/gO,\ [ "Z0_%``u||^<@Y@Y@a$Z  <<,,<$.0G0G#HL#(==L\\..2OO$.=L#(::L\\..2NN$.0G0G#HL#(==L$CCH<<C\C\B]]ln  ).\^d(eH%n k " 
&?@
&HI
& =>
& =>	
&
 HI
& IJ
& '
& '
& (
& 
 ).ETZ([H%T S "%Oz2M'N%OO(-EW](^H%P M *7
|5IYHZZpq  z)qJ |TBJ!5<<9!&!;!;!F!FJU\\95!&!5!5!@!@J"6"8:N:P!QJ #5<<CLj+//*B*BC(*5
&MFE&+kkL&%u}}U['K
#f a z!&+kk&^5&^!&^5;;W]&^K
#^ ] +&+kk '';;'CH;;W]'K
#Z U 
*qJ &6=ILL++>!&!<!<!&!9!95zQ#-uQxq>"R $.uQx"H&+kk/]c&dK
#p o z)&+kkLL00 kk'K
#l ] Z'&+kkLL// kk'K
#Z K #&+kk5TZ&[K
#H G z)!MJ-%eDJ&+kk*jPUP[P[dj&kK
#> = z! K/#Aha?(BC
"
&+kk*EJJW]&^K
#0 / j &+kk%uzzRX&YK
#, + *,||&&.PP)cdidpdpd{d{c|  }b  c  88I||//5<<3S3SSH 8YJK 
 u||==> C '*K
#
  &+ell.F.F-G%G"&+kk2HPUP[P[dj&kK
#rP   c           	      ^   t         |   |||||||      }|dk(  r.|| j                  v r |j                  | j                  |          |S || j                  v r	d|v rd|d<   	 t
        j                  j                  j                  |t              }	t
        j                  j                  j                  |t              }
d}d| _
        d| _        |dk(  rMt        j                  ||      } ||	i |
}t        |t
        j                        r+|j!                  d      }n|dk(  r8t#        |	d   j$                  |      }t        j                  ||      } ||	i |
}n|d	k(  rnt'        | d
      st)        |  d      | j*                  j-                  |      }t/        |      }|t        v rt        |   |g|	i |
}nz | j0                  |	i |
}ng|dk(  r`| j*                  }|j3                  d      }|D ]  }t#        ||      } t        |t
        j                        r|j!                  d      }n|}nd}|r,t        |t4              st7        d      |j                         d| _
        d| _        |S # t8        $ r.}t:        rt=        j>                  d| d| d|        Y d }~Bd }~ww xY w)Nplaceholderr   r   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_fnsr   r  r  r  r  _disable_module_getattr_disable_call_moduler  rH   rF   r   r   r  r  r  AttributeErrorr  get_submoduletyperY  splitr   r   	Exception_IS_IN_DEBUG_MODEwarningswarn)r   kindrw  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er  s                        rN   r  zHFTracer.create_proxy4  s   W!$fdIO_`= Vt~~%=v 67IT]]" 6!#)x .	]44T;LMJ 88==66v?PQL&*#+/D((,D%&488H&
ClCh5'{{&{9H& A!8!8&A488H&
ClC&t^4(D61`)abbii--f59555h?ajaT`aH0t00*MMH#99S)! 7D&x6H7h5'{{&{9H'H*/'&!"e,$%IJJ##H- (-$$)!	  	]  ?vXfXUWXYWZ[\	]s   "HI5 5	J,>$J''J,c                 \    t         dd      r|S  fd}t        |t        j                  j                        r& || j
                  j                         |      }||S  j                  r@t        |t        j                        r& || j
                  j                         |      }||S |S )Nr]  Fc                      |D ]t  \  } |u s|vr`i }dt        j                  j                        j                  v rj                  sd n fd|d<    j                  ddi fi |}||<   |   c S  y )Nr  c                      t        |       S r   r   )r  attr_valr   r   s    rN   r\  zLHFTracer._module_getattr.<locals>.maybe_get_proxy_for_attr.<locals>.<lambda>  s    nT4QRT\6] rP   r  r   )r  	signaturer  
parametersparam_shapes_constant)ry  collection_to_searchparameter_proxy_cacher  r   	val_proxyr   r   s   `     @rN   maybe_get_proxy_for_attrz:HFTracer._module_getattr.<locals>.maybe_get_proxy_for_attr  s    0 8DAq1}$99%'F1W5F5FtGXGX5Y5d5dd ,0+E+E %))] !''9 :
 ):(9(9*aR(ZSY(ZI7@1!44Q778 rP   )
r  rF   r   r   	Parameterr  named_parametersr  r   named_buffers)r   r  ry  r~  r  maybe_parameter_proxymaybe_buffer_proxys   `      rN   _module_getattrzHFTracer._module_getattrz  s    42E:O  (EHH$6$67(@dii88:<Q)% )400++
8U\\0R%=dii5579N&" &1--OrP   r  ry  r~  c                 (    | j                  |||      S r   )r  )r   r  ry  r~  s       rN   r  zHFTracer.getattr  s    ##D(4IJJrP   c                 `    t        | dd      r ||i |S || _        t        |   ||||      S )Nr^  F)r  rY  r  rX  )r   r  forwardr   r   r  s        rN   rX  zHFTracer.call_module  s>    4/7D+F++#w"1gtV<<rP   c                     t        ||       S r   )r  )r   r  s     rN   proxyzHFTracer.proxy  s    tT""rP   r  .c           
   #     K   | j                   D ci c]  }|t        t        t        |             c}| _        t               | _        | j                  j                         D ]4  \  }\  }}t        t        ||       | j                  j                  |       6 g }t        j                  |      }t        j                  j                         D ]  \  }}|||ur|j                  d      s| j                  j                         D ]M  \  }	}
|j                   j                         D ]+  \  }}||	u s|j#                  |||	f       t        |||
       - O  d  | j                  j                         D ]  \  }\  }}t        t        ||        i | _        t               | _        |D ]  \  }}}	t        |||	        y c c}w w)Ntransformers)_TORCH_METHODS_TO_PATCHr  r  r   patched_torch_methodssetr\  itemsr  addr  	getmodulesysmodulesr  r  __dict__rI   )r   r  rw  r  r  origpatchedmodule_of_modelrp  orig_clspatched_clsr  r  r  s                 rN   patch_for_tracingzHFTracer.patch_for_tracing  s     SWRnRn&
HNF+GE6,BCC&
" %)%?%?%E%E%G 	$!D/7DE4)MMd#	$
 !++D1**, 		=ID#*s//I??>2)-)?)?)E)E)G =%+'*||'9'9'; =OItx'Y'ABY<==		= 	  $99??A 	'OD)1dE4&	'%'"(/ 	.$CHCH-	.?&
s   G!GD G3BGconcrete_argsdummy_inputs6complete_concrete_args_with_inputs_not_in_dummy_inputsc           	      &   t        j                  t        |t        j                  j
                        r|j                  n|      }|i }||r|j                  j                         D ]P  }|j                  |v r|j                  t         j                  j                  u s9t        d|j                   d       |j                  |j                  j                         D ci c]5  }|j                  |vr%|j                  |vr|j                  |j                  7 c}       |j                  j                         |j                         z
  }t!               }	t!               }
|	|
g}|j"                  j$                  t'        t(              v rt!        dd      }|j+                  d|       |t-        |      ni }|D ]t  }||v rt        || j.                        s$t1        |      j2                  j5                  d      r%|j                  | j7                  ||||	             gt9        d
| d       d }t;        j<                  ||      }|j                  j                         D ]K  }|j>                  t         j                  j@                  k(  s+|j                  |vs:i |d|j                   <   M || _!        | a"| jG                  |      5  	 tH        |   ||      | _&        da"	 ddd       | jL                  jN                  D ]  }|jP                  dk(  r|jR                  |v rd|_*        t        jV                  |_        n|g}tY        jZ                         }|r?|j]                  d      }d||<   |t_        |j`                  j                               z  }|r?tc        |j                               D ]  }| jL                  je                  |        |jP                  dk(  sd|_         | jL                  S c c}w # da"w xY w# 1 sw Y   xY w)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 rZ  r   r  r2  r4   )_deserialize_graph_module_CodeOnlyModule)r  zCould not generate input named z8 for because root is not a transformers.PreTrainedModel.c                 \    t        | t        j                        r| j                  d      S | S r[  )rF   r   r   r   )ri  s    rN   to_metazHFTracer.trace.<locals>.to_meta  s$    %.xx''LrP   **r  rW  r   r   ra  )3r  rz  rF   r   r   Moduler  r{  r  r  rA   r  r   r   updaterE   r  r  r  r   r(   r   r   supported_archsra  r  r  rU  r  pytreetree_maprg  VAR_KEYWORDr[  r  r  r  tracegraphnodesoprw  r   r   r=  OrderedDictr   r   usersreversed
erase_node)r   r  r  r  r  sigparamr  r  r  sequence_lengthr   num_choicesr0  r  r  concrete_metasr  to_visit	to_deleter   userr  s                         rN   r  zHFTracer.trace  s   @ 
40QW[\ M#(^..0 m::-==G$5$5$;$;;$']^c^h^h]iij%kll	m
    !^^224l2qvv]7R FFAII% nn))+m.@.@.BB *+
.0_->>""j1X&YY.11=KLLK('3'?l#R% 	JV# $ 4 45d9P9P9[9[@: d88z5^i8jk"5j\ B5 5 		
  &9^^**, 	7EzzW..:::uzzQ\?\46EJJ<01	7 ( ##D) 	''"W]4}]M
"&		' JJ$$ 	!Dww-';;+- "DI !&DI !%vH + 7 7 9I"$LLO'+	! D$88 #
 !))9 : 4

--d34
 ww(" 	/	!2 zzch #'		' 	's*   #:O:PO?&P?PPPrp  c                 V    t        d |j                  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              3   <   K   | ]  }t        |t                y wr   )rF   r   )r   r  s     rN   r   zKHFTracer._stateless_mod_instanciation_depends_on_proxies.<locals>.<genexpr>U  s     Mt:dE*Ms   )anyr  r  )r   rp  s     rN   /_stateless_mod_instanciation_depends_on_proxiesz8HFTracer._stateless_mod_instanciation_depends_on_proxiesP  s"    
 Ms||7J7J7LMMMrP   c                 l   | j                  |      ryd}|j                  j                  j                         }| d| }d}t	        | j
                  |      r>t        | j
                  |      |u rd}n#| d| }|dz  }t	        | j
                  |      r>|s| j
                  j                  ||       |S )zb
        Helper method which tries to insert a module that was not declared as submodule.
        r<   r   r  FTr4   )r  r  r  lowerr  r  r  
add_module)r   rp  idxmod_namepathalready_inserteds         rN   _insert_module_as_submodulez$HFTracer._insert_module_as_submoduleW  s     ??D==))//11SE" dii&tyy$'3.#' Zq&D1HC dii&  II  s+rP   c                 &   	 t         |   |      S # t        $ ru}| j                  rbt	        t        |j                                     dk(  r=t	        t        |j                                     dk(  r| j                  |      }|cY d}~S |d}~ww xY 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	NameErrorr  r   r   r{  buffersr  )r   rp  ru  r  r  s       rN   r  zHFTracer.path_of_moduleo  s    	7)#.. 	//CS^^=M8N4OST4TY\]abebmbmbo]pYquvYv77<G		s!    	BA(BB	BBr  module_qualified_namec                 L    | j                  |       xr t        | 	  ||      S r   )r  r  is_leaf_module)r   r  r  r  s      rN   r  zHFTracer.is_leaf_module  s3    HHKK 
QVQXQg$R
 	
rP   )is_backward_compatibleobjr   c                      t        |d             }|j                  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.
        rE   r  )r  r  rw  r  r  )r   r  	attributes      rN   rE   zHFTracer.keys  s=     -KV,.	88??%%d+&&&rP   )NNN)NNT)6r  r  r  r  r  bool__annotations__r  r  r   r  r   r  r   r  r   r  r  r8   r   r:   r  r>  r  rG   r   r   r   r   r   rU  r  r  r   r  rX  r  
contextlibcontextmanagerr   r   r  r   r  r   r   r  r  r  r  r  r   rE   r  r  s   @rN   r  r  ]  sE    %)T((,, 	~+%)	 1B0C(/[dIeO)-B m&m47m@DS	mX\]`Xam	c5<<	 m^CL#LKC K3 KtCQTH~ K=# ".eEHHOOXc3h=O,O&P ". ".N 3715GK}EHHOOXc3h%778}  S#X/} tCH~.	}
 AE} 
}~N299 NQU Nryy S 0"))  "
 
 
PT 

 $/ C  0rP   r  r  r  c                    t        j                  | j                        }t        |      t        |j                  j                               k  s^t        |      dk(  r|d   ndj                  |      }dj                  |j                  j                               }t        d| d|       |j                  j                         D ci c](  }|j                  |vs|j                  |j                  * c}S c c}w )Nr4   r   , z(The model does not have input(s) named: z&, expected a subset of the following: )r  rz  r  r  r{  rE   r   joinr   r  r  rA   )r  r  r  formatted_input_namesformatted_allowed_input_namesr  s         rN   get_concrete_argsr    s    


EMM
*CCNN$7$7$9 ::25k2Ba2GATYYWbMc(,		#..2E2E2G(H%67L6M N-.0
 	

 (+~~'<'<'>\!!&&P[B[AFFAII\\\s   	C9C9r   c                 :    | j                   j                  t        v S r   )r  r  _SUPPORTED_MODELS)r  s    rN   is_model_supportedr    s    ??##'888rP   c                     t        |       s:dj                  t              }t        d| j                  j
                   d|       y )Nr  zModel z) is not supported yet, supported models: )r  r  r  r<  r  r  )r  supported_model_namess     rN   check_if_model_is_supportedr    sJ    e$ $		*; <!U__--..WXmWno
 	
 %rP   disable_check
tracer_clsc                 B   || j                   j                         }t        |      }t        | |      }|st	        |        d|v r,t        | j                  dd      st        j                  d       d|vr=t        | j                  dd      r&t        j                  d       d| j                  _	         |       }|j                  | |      }t        j                  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"])
        ```
    r4  	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  rE   r   r  r  r  r:  loggerwarningr  r  r   r  r   r  r  r   )r  r  r  r  r  r  traced_graphtraceds           rN   symbolic_tracer    s    < ((--/{#K%e[9M#E*K'kSX0Y#	
 +kSX0Y.	
 "' \F<<]<CLXX!!%6FLLFM (-F$LLFMMrP   r   r   )NNg       @FF)F)NN)r   )Nr   FN)
      N)r  r   )r  r=  r  r  r  r>  r  osr  r  re  typingr   r   r   r   r   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   r<   r   cache_utilsr   r   r   r   modeling_utilsr   r   models.autor   models.auto.modeling_autor   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   pytorch_utilsr3   import_utilsr5   r6   r7   r8   r9   peftr:   
get_loggerr  r  environrH   upperrd  rG   rO   (_REGULAR_SUPPORTED_MODEL_NAMES_AND_TASKSrE  _REGULAR_SUPPORTED_MODELSitemrF   r   extend_SPECIAL_SUPPORTED_MODELSr_  sortedr  r  r  r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r  r  r#  r'  r*  r-  r/  r3  r5  r7  rF  rM  rQ  rT  rV  rX  rb  re  rp  rx  r{  r}  r  	Embedding
functional	embedding	LayerNorm	GroupNormLinearreluReLUwhereabsr	  r   catstackr  mulr   matmulbmmbaddbmmr  repeatrepeat_interleaverollflipindex_selectgatherConv1dConv2dsqueeze	unsqueezer]  one_hotMSELossCrossEntropyLossBCEWithLogitsLossr  r  r  scaled_dot_product_attentionr  r  r  r  r  ra  r  r  r  r  r  r  r  r  r   r  r  r  r  r  r  r  r  r   rP   rN   <module>r'     sA           	  
  S S S  $ $  < < 1 2 )  E E > $     , ?   
		H	%JJNN?B7==?CWW 
 8<&%&&eCcN34& 
#Y&R4, (l '.u%5 " 4 VD$!(()N)VQU)VW!(()Nt)TU	V   &%>AZ%Z![\] d
 ejZOS ! J*	E34 3	-T 	- &'D - $( ," '+ !.H #' 8 23! % 89t PA- (, .( 26 .0 49d .*-$-*-"$-&:- NRT---"&,4	HH*,4	HH!!#@,4 
HH*,4 
HH*	,4
 
HHOO_,4 
JJ
,4 
HH6,4 
HHMM=,4 
KK,4 
IIy,4 
LL,,4 
JJ
,4 
IIy,4 
KK,4 
IIy,4  
IIy!,4" 
LL&#,4$ 
LL,%,4& 
IIy',4( 
MM=),4* 
LL.+,4, 
LL,-,4. 
LL,/,40 
41,42 
JJ
3,44 
JJ
5,46 
LL(7,48 
*9,4: 
LL8;,4< 
LL,=,4> 
LL,?,4@ 
HHOO_A,4B 
HHOO_C,4D 
MM=E,4F 
LL.	OO_	LL2	6	HH!<	HH&	HH8	HH :&W,4 Xx/0 ,\ &8 588..KKL
))e ))X_' _,	+ 	C7 C$ ;?!!7?+W]-CWZEXXY! x67! 	!H4 @H x7I1J 	"$u+ 	"(D6S_K_B` 	" &uh5RSX5Y -O2<@	  *L29=	  ,N2;?	 c c (SWX[S\J] qv qh]RYY ]T#Y ]9
 (,!)	>>$s)$> > X	>
 >rP   