
    9iӺ                        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	m
Z
mZmZmZmZmZmZ d dlZd dlmZ d dlmZmZmZmZ d dlmZ d dlmZ ddlmZmZmZ dd	l m!Z! dd
l"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5 ddl6m7Z7m8Z8m9Z9m:Z:m;Z;  e:       rd dl<m=Z=  ej|                  e?      Z@ej                  j                  dd      j                         e7v ZD	 d]dee   deeeEeeE   f      deeE   fdZFg dZGg ZHeGD ]:  ZI eJeIeK      reHj                   eFd^i eI       $eHj                   eFeI             < g dZM eN eO ePeHeMz                     ZQd ZR	 d_dZSd ZTd ZUd ZVd ZWd ZXd`dZYd ZZddd Z[d! Z\d" Z]daddd#Z^daddd$Z_d%dd&d'Z`ddd(Zad) Zbddd*Zcddd+Zdd%d%dd,d-Zed%d%dd,d.Zfd/ Zgd0 Zhddd1d2Ziddd3Zjd4 Zkddd5d6Zld7 Zmd]d8Znd9 Zod: Zpd; Zqd< Zrd]d=Zsd]d>Ztd? Zud@ ZvdA ZwdbdBZxdC ZydD ZzdE Z{dF Z|i ej$                  j                  eRej$                  j                  j                  eSej$                  j                   eTej$                  j                  eUej$                  j                  eVej                  eWej$                  j                  j                  eYej$                  j                  eXej
                  eZej                  e[ej                  e\ej                  e]ej                  e^ej                  e_ej                  e`ej                  eaej                  j                  ebi ej                  ecej                  edej                   eeej                  j                   efej"                  egej                  j$                  ehej&                  eiej(                  enej*                  eoej                  j*                  epej,                  ejej                  j,                  ekej.                  elej                  j.                  emej$                  j0                  eqej$                  j2                  erej4                  esej                  j4                  etej6                  euej                  j6                  evej8                  ewej$                  j                  j:                  exej$                  j<                  eyej$                  j>                  ezej$                  j@                  e{ejB                  e|i	Zeeef   edG<    G dH dIe      Z G dJ dKe      Z G dL dMe      ZdN ZdO ZdcdPedQedReee      fdSZ G dT dUe      ZdVejX                  dWeeE   fdXZdVefdYZddefdVedWeeeE      dZed[ee   def
d\Zy)d    N)AnyCallableDictListOptionalTypeUnion)nn)GraphGraphModuleProxyTracer)compatibilityParameterProxy   )PretrainedConfigPreTrainedModellogging)
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_MASKED_IMAGE_MODELING_MAPPING_NAMES!MODEL_FOR_MASKED_LM_MAPPING_NAMES'MODEL_FOR_MULTIPLE_CHOICE_MAPPING_NAMES0MODEL_FOR_NEXT_SENTENCE_PREDICTION_MAPPING_NAMES#MODEL_FOR_PRETRAINING_MAPPING_NAMES*MODEL_FOR_QUESTION_ANSWERING_MAPPING_NAMES-MODEL_FOR_SEMANTIC_SEGMENTATION_MAPPING_NAMES,MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING_NAMES/MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING_NAMES(MODEL_FOR_SPEECH_SEQ_2_SEQ_MAPPING_NAMES,MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING_NAMES6MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING_NAMESMODEL_MAPPING_NAMES)ENV_VARS_TRUE_VALUESTORCH_FX_REQUIRED_VERSIONget_torch_versionis_peft_availableis_torch_fx_available)	PeftModelFX_DEBUG_MODE 
model_namesupported_tasksreturnc                    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$        d}||j'                         }t)        |t*              r|g}g }|D ]+  }||   j-                  | d       }|s|j/                  |       - |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backbone)r)   r!   r    r   r   r$   r&   r   r   r"   r%   r'   r   r   r(   r   r   r#   r   keys
isinstancestrgetappend)r2   r3   task_mappingmodel_class_namestask
class_names         _/var/www/html/backtest/airagagent/rag_env/lib/python3.12/site-packages/transformers/utils/fx.py%_generate_supported_model_class_namesrD   I   s]   &: 	#$T 	6	
 	6 	B 	B 	B 	&'Z 	H 	"#R 	 L 	 !N 	 L 	)*`  	*!" 	 L#$ "O4'L, &++-/3'*+ 1!$'++J=
$$Z01
     ))altclipalbertbartbert
blenderbotzblenderbot-smallbloomclipconvnextdebertaz
deberta-v2
distilbertz
donut-swinelectragpt2gpt_neogptjhubertlayoutlmlxmertm2m_100marianmbartzmegatron-bert
mobilebertmt5nezhaoptpegasusplbartresnetroberta	segformerspeech_to_textspeech_to_text_2swint5trocrvitxglmwav2vec2)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     rC   torch_nn_embeddingr      s@    ;;ccT[[%6%6r%:c6QUQ\Q\QbQbccrE   Fc                 z    t        j                  g | j                  |j                  d   d|j                  dS rx   )r}   r~   r   r|   )r   r   padding_idxmax_norm	norm_typescale_grad_by_freqsparses          rC   torch_nn_functional_embeddingr      s3     ;;YYV\\"%5YfFLLYYrE   c                     |S N r   s     rC   torch_nn_layernormr          LrE   c                     |S r   r   r   s     rC   torch_nn_groupnormr      r   rE   c                 f    t        j                  |j                  d d | j                  fz   d      S )Nry   rz   r{   )r}   r~   r   out_featuresr   s     rC   torch_nn_linearr      s,    ;;u{{3B'4+<+<*>>vNNrE   c                     | S r   r   xs    rC   
torch_relur          HrE   c                     |S r   r   )r   r   s     rC   torch_nn_relur      r   rE   c                      |st        d      | S )Nz>Don't support in-place functional.relu for MetaTensor analysis
ValueError)r   inplaces     rC   torch_nn_functional_relur      s    YZZHrE   c                 r    | j                  d      |j                  d      z   |j                  d      z   S Nrz   r   to)	conditionr   ys      rC   torch_wherer      s5     <<v<&V)<<qtt6t?RRRrE   outc                     |t        d      | S )Nz2Don't support in-place abs for MetaTensor analysisr   )r   r   s     rC   	torch_absr      s    
MNNLrE   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 )N   r   r   stepr|   rz   r|   r{   )lenr;   floatintr=   r}   r~   )argskwargsnr   startendr|   s          rC   torch_aranger      s    D	ADAv1g	
a
ssD%E
#uC$4y::fd#DJJwE;;e,E&IIrE   c                  
   t        |       } t        | d   t        j                        r*| d   j                  t        j                  d      k(  rd| d<   t        |      }|j                  dd        t        j                  | i |S )Nr   rz   r{   )listr;   r}   Tensorr{   dictpopfull)r   r   kwargs_without_devices      rC   
torch_fullr      sm    :D$q'5<<(T!W^^u||F?S-SQ Lh-::t5455rE   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     rC   	<genexpr>ztorch_cat.<locals>.<genexpr>  s     :%5::s   r   rz   r   )r   r   r   sumr}   r~   )	tensorsr   axisr   tshapesr   concatenated_dimfinal_shapes	    `       rC   	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   r   rz   r   )r   r   r   insertr   r}   r~   )r   r   r   r   r   s        rC   torch_stackr     sw    
{t|
{t'
Qwajnn"S(!!"E	LLc'l#;;uV,,rE   r   )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 )Nrz   r   r   )r;   r}   r   
empty_likemaxr   r   r   ranger>   r~   )	r   otherr   r   
max_lengthinput_shapeother_shaper   is	            rC   	torch_addr      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,,rE   c                    t        | ||      S )Nr   )r   )r   r   r   s      rC   	torch_mulr   .  s    UEs++rE   c                     t        | |      S r   )r   )r   r   s     rC   torch_tensor_mulr   2  s    T5!!rE   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 )
Nr   r   r   ry   g        rz   r   r{   )r   sizer   r   r   r>   r   r   r}   tensorr~   )
r   r   r   d1d2r   r   shape1shape2r   s
             rC   torch_matmulr   6  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--rE   c                    |t        d      | j                  \  }}}|j                  \  }}}t        j                  |||d      S )Nz2Don't support in-place bmm for MetaTensor analysisrz   r   )r   r   r}   r~   )r   mat2r   
batch_sizer   m_ps           rC   	torch_bmmr   Z  sG    
MNN{{J1jjGAq!;;z1a77rE   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         rC   torch_baddbmmr   b  s     
QRRVV$$rE   c                $    t        | |||||      S )Nr   )r   )r   r   r   r   r   r   s         rC   torch_tensor_baddbmmr   h  s    vvD3OOrE   c                 ^    d |D        }t        j                  | g| j                  d      S )Nc              3   J   K   | ]  }t        j                  |d         yw)cpur   N)r}   r   )r   operands     rC   r   ztorch_einsum.<locals>.<genexpr>n  s      YW))'%@@Ys   !#rz   )r}   einsumr   )equationoperandsconcrete_operandss      rC   torch_einsumr  l  s.    YPXY<<5#4588@@rE   c                     t        | j                        }t        |      D ]  \  }}||xx   |z  cc<    t        j                  |d      S r   )r   r   	enumerater}   r~   )r   sizesr   r   r   s        rC   torch_tensor_repeatr  r  sF    E%  1aA;;uV,,rE   )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 )Nr   r   r   r{   rz   )	r   r   r   r   r;   r   r}   numelr~   )r   r  r   num_argsr   repeatss         rC   torch_repeat_interleaver  y  s    4yH1} + 7T!W[[]KT!W]]#;!|1gUq'gs#u{{7';q'@#J#g,&J(3(?W[[]E#J;;-f--rE   c                r    t        | j                        }t        |      ||<   t        j                  |ddiS Nr{   rz   )r   r   r   r}   r~   )r   r   indexr   r   s        rC   torch_index_selectr    s1    EUE#J;;-f--rE   c                     t        | ||      S r   )r  r   r   r  s      rC   torch_tensor_index_selectr    s    dC//rE   )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         rC   torch_gatherr    s6    ES!E#J;;-f--rE   c                     t        | ||      S r   )r  r  s      rC   torch_tensor_gatherr    s    c5))rE   c                     | S r   r   )r   shiftsdimss      rC   
torch_rollr    r   rE   c                     | S r   r   )r   r  s     rC   
torch_flipr    r   rE   c                     | S r   r   )r   r  s     rC   torch_tensor_flipr!    s    KrE   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 )Nry   validr   r   samer   r   r   r   rz   r   r   paddingr   mathfloordilationkernel_sizestrideout_channelsr}   r~   )r   r   l_inr   r'  l_outs         rC   torch_nn_conv1dr0    s    ;;r?DEllG'&U[[!}U[[!

A
N"T]]1%59I9I!9Lq9P%QQTUUY]YdYdefYggjkk
 b	!!E"I;;uV,,rE   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   r#  r$  r%  r   r   r   rz   r   r&  )r   r   h_inw_inr   r'  h_outw_outs           rC   torch_nn_conv2dr7    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,,rE   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   r>   r}   r~   )r   r   r   	new_shape	dim_values        rC   torch_squeezer;    s    E
7))+#C:?IIcN	 	(IA~Y'	( ;;uV,,rE   c                     t        | |      S r   )r;  r   r   s     rC   torch_tensor_squeezer>    s    s##rE   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      rC   torch_unsqueezer@    sJ    E
QwiikAo#	LLa;;uV,,rE   c                     t        | |      S r   )r@  r=  s     rC   torch_tensor_unsqueezerB    s    4%%rE   c                     t        j                  t        j                  | d      fi |}t        |t         j                        r|j                  d      S t        t        |d             S )Nr   r   rz   c                 $    | j                  d      S )Nrz   r   r   s    rC   <lambda>z*torch_unique_consecutive.<locals>.<lambda>  s    144< rE   )r}   unique_consecutive
zeros_liker;   r   r   tuplemap)r   r   outputs      rC   torch_unique_consecutiverK    sT    %%e&6&6uU&KVvVF&%,,'yy  S!7899rE   c                     |dk  rt        d      t        | j                        |gz   }t        j                  |d      S )Nr   zEDon't support automatic num_classes inference for MetaTensor analysisrz   r   )r   r   r   r}   r~   )r   num_classesr   s      rC   torch_nn_functional_one_hotrN    s;    Q`aa+.E;;uV,,rE   c                 l    | j                   dk(  r|j                  }nd}t        j                  |d      S Nnone)r   rz   r   	reductionr   r}   r~   r   r   targetr   s       rC   torch_nn_mselossrV    -    ~~;;uV,,rE   c                 l    | j                   dk(  r|j                  }nd}t        j                  |d      S rP  rR  rT  s       rC   torch_nn_crossentropylossrY    rW  rE   c                 l    | j                   dk(  r|j                  }nd}t        j                  |d      S rP  rR  rT  s       rC   torch_nn_bcewithlogitslossr[    rW  rE   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   )r;   r}   r   	ones_liker|   float16float32float64int32r   int64)r   concretes     rC   to_concretez%operator_getitem.<locals>.to_concrete  sa    a&q7H~~%--u{{![[#;;u{{3OrE   r   r   rz   )	r;   r}   r   rH  rI  operatorgetitemr   r   )abre  s      rC   operator_getitemrj    sy     !U\\"ac+q)*AAA 0 05 A1EHHPPAq!!rE   _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     rC   install_metadatazHFProxy.install_metadatac  s	    !rE   c                 @    | j                   j                  dd| fi       S )Ncall_methodr   )tracercreate_proxyr   s    rC   r   zHFProxy.shapef  s    {{''vwKKrE   c                     t        | d      S )Nr{   )MetaDeviceAttributerv  s    rC   r{   zHFProxy.devicej  s     #422rE   c                 z    t        | d      r!| j                  t        | j                        S t        |          S Nro  )hasattrro  r   super__len__r   	__class__s    rC   r}  zHFProxy.__len__p  s2    4%$..*Dt~~&&w  rE   c                 h    t        | d      r| j                  | j                  S t        |          S rz  )r{  ro  r|  __bool__r~  s    rC   r  zHFProxy.__bool__u  s.    4%$..*D>>!w!!rE   c                 F    |dk(  r| j                  |      S t        | |      S rz  )__getattribute__HFAttribute)r   ks     rC   __getattr__zHFProxy.__getattr__z  s*    ((++ 4##rE   c                 `    | j                   j                  dt        j                  | ||fi       S Ncall_function)rt  ru  rf  setitem)r   indicesvaluess      rC   __setitem__zHFProxy.__setitem__  s-    {{''9I9IDRY[aKbdfggrE   c                 n    t        | d      r| j                  || j                  v S t        |   |      S rz  )r{  ro  r|  __contains__)r   keyr  s     rC   r  zHFProxy.__contains__  s5    4%$..*D$..((w#C((rE   )__name__
__module____qualname____doc__rq  propertyr   r{   r}  r  r  r  r  __classcell__r  s   @rC   rm  rm  ^  sV    " L L 3 3
!
"
$h) )rE   rm  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 rz  )rootr  rt  _noder{  rq  getattrro  )r   r  r  s      rC   __init__zHFAttribute.__init__  sT    		kk
499k*!!'$))*=*=t"DE +rE   c                     | j                   Q| j                  j                  dt        j                  | j
                  | j                  fi       j                  | _         | j                   S r  )r  rt  ru  builtinsr  r  r  noderv  s    rC   r  zHFAttribute.node  sT     ::11/8CSCSVZV_V_aeajajUkmopuuDJzzrE   c                 n    | j                   j                  d| j                  | j                  f|z   |      S )Nrs  )rt  ru  r  r  )r   r   r   s      rC   __call__zHFAttribute.__call__  s.    {{''tyy499,QUBUW]^^rE   N)r  r  r  r<   r  r  r  r  r   rE   rC   r  r    s*    F3 F  _rE   r  c                       e Zd Zy)rx  N)r  r  r  r   rE   rC   rx  rx    s    rE   rx  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.rz   ro  zNo metadata was found for )	r;   rx  r}   fxr   rm  r{  RuntimeErrorro  )vs    rC   _proxies_to_metasr    sW    !()!UXX^^$1g&71k+B!;A3?@@{{HrE   c                 F     t        j                          fd       }| fS )Nc                     d fd}t         j                  j                  j                  | |       t         j                  j                  j                  ||       j                  j                  d| |      S  | i |S )Nc                 ,    t        | t              r| y y r   r;   r   )r  proxys    rC   check_has_proxyzB_gen_constructor_wrapper.<locals>.wrapper.<locals>.check_has_proxy  s    !U# $rE   r  )r}   r  r  map_aggregatert  ru  )r   r   r  r  rU  s      @rC   wrapperz)_gen_constructor_wrapper.<locals>.wrapper  sp    	
 	##D/:##FO<<<,,_fdFSS4*6**rE   )	functoolswraps)rU  r  s   ` rC   _gen_constructor_wrapperr    s*    __V+ +  F?rE   lowhighforbidden_valuesc                 x    |g }t        j                  | |      }||v rt        j                  | |      }||v r|S r   )randomrandint)r  r  r  values       rC   _generate_random_intr    sG    NN3%E
#
#sD) #
#LrE   c                   D    e Zd ZU dZdZeed<   dZeed<   g dZ e	       se
fne
efZefdf fd	Zde
d	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	 	 	 d'd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Z"dej<                  defdZ#dej<                  defdZ$dej<                  def fdZ%dej:                  j<                  d edef fd!Z& e'd"      d#d$defd%       Z( xZ)S )(HFTracerz
    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finfor   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  r.   ImportErrorr,   r+   )r   r  r  r  s      rC   r  zHFTracer.__init__  sK    *:Oab$&HIZI\H]]p,-^=  'rE   model
input_namer   r4   c                    t        |d|j                        j                  }|j                  }i }|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 rCt        j                  ||j"                  jP                  gz   t        jL                  |      ||<   |S d!|v rCt        j                  ||j"                  jR                  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 sd(|v r+t        j                  |t        j                  |      ||<   |S ||j"                  jT                  gz   }t        j                  |t        jL                  |      ||<   |S ))z4Generates 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.rr   ru   rv   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visual_feats
visual_posinputsinput_valuesi'  i N  r  r  maskids)+r  r  r  r{   r   r    r   r   r   r   r}   r  longr"   r   r%   r{  configr  r   
num_labelsr`  r!   r'   r   r   r$   r#   r   NotImplementedErrorr  r  r  r  r;   collectionsabcIterabler   input_feat_per_channelvisual_feat_dimvisual_pos_dimhidden_size)r   r  r  r   model_class_namer{   inputs_dictr   labels_shapelabels_dtyper  r  heightwidthr   
seq_lengthshape_with_hidden_sizes                    rC   _generate_dummy_inputzHFTracer._generate_dummy_input  sU    #5*EuW``GGqJ $LM$CD$ HI$ <=	$
 HI$  ).JejjY_(`H%T S " &FG&OP& ,& 
 27Zuzzbh1i-./4{{:UZZ`f/gO,F E "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%X U " 
&?@
&HI
& =>
& =>	
&
 HI
& IJ
& '
& '
& (
& 
 ).ETZ([H%~ } "%Oz2M'N%OO(-EW](^H%z w *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
#P K z!&+kk&^5&^!&^5;;W]&^K
#H G +&+kk '';;'CH;;W]'K
#D ? z)&+kkLL00 kk'K
#< - Z'&+kkLL// kk'K
#*  #&+kk5TZ&[K
#  z)!MJ-%eDJ&+kk*jPUP[P[dj&kK
#  z!Uj%8&+kk%uzzRX&YK
#
  &+ell.F.F-G%G"&+kk2HPUP[P[dj&kK
#rE   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k(  rMt        j                  ||      } ||	i |
}t        |t
        j                        rI|j                  d      }n5|dk(  r8t        |	d   j                   |      }t        j                  ||      } ||	i |
}n|dk(  r}t#        | d	      st%        |  d
      d| _        	 | j(                  j+                  |      }t-        |      }|t        v rt        |   |g|	i |
}n | j.                  |	i |
}d| _        nv|dk(  rod| _        	 | j(                  }|j1                  d      }|D ]  }t        ||      } t        |t
        j                        r|j                  d      }n|}d| _        n|S t        |t2              st5        d      |j                  |       |S # d| _        w xY w# d| _        w xY w# t6        $ r/}t8        rt;        j<                  d| d| d|        Y d }~|S d }~ww xY w)Nplaceholderr{   rz   r  r   rs  r   call_moduleorig_forwardz/ does not have an attribute called orig_forwardTFget_attr.z"Don't support composite output yetzCould not compute metadata for z target z: )r|  ru  	meta_argsrq  orig_fnsr}   r  r  r  r  rk  r=   r;   r   r   r  r  r{  AttributeError_disable_module_getattrr  get_submoduletyper  splitr   r   	Exception_IS_IN_DEBUG_MODEwarningswarn)r   kindrU  r   r   name	type_exprproxy_factory_fnrv
args_metaskwargs_metasmeta_targetmeta_outmethodmodmod_typeattr_itratomsatomer  s                       rC   ru  zHFTracer.create_proxyl  s   W!$fdIO_`= Vt~~%=v 67IT]]" 6!#)x /	]44T;LMJ 88==66v?PQL&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/3,9))11&9C#CyH#99#9(#CC#e*#eXd#e#44#4#4j#QL#Q38D0#/3,
9#yyH"LL-E % ;#*8T#:;!(ELL9#+;;f;#=#+38D0	b%( !EFF)
 	1 49D0 49D0  	]  ?vXfXUWXYWZ[\			]sQ   "DJ 4AI3 J AI? ;	J ,J 3	I<<J ?	JJ 	K$J>>K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    rC   rE  zLHFTracer._module_getattr.<locals>.maybe_get_proxy_for_attr.<locals>.<lambda>  s    nT4QRT\6] rE   r   r   )inspect	signatureru  
parametersparam_shapes_constant)r   collection_to_searchparameter_proxy_cacher   r   	val_proxyr   r   s   `     @rC   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 rE   )
r  r;   r}   r
   	Parameterr  named_parametersr  r   named_buffers)r   r  r   r&  r(  maybe_parameter_proxymaybe_buffer_proxys   `      rC   _module_getattrzHFTracer._module_getattr  s    42E:O  (EHH$6$67(@dii88:<Q)% )400++
8U\\0R%=dii5579N&" &1--OrE   r  r   r&  c                 (    | j                  |||      S r   )r.  )r   r  r   r&  s       rC   r  zHFTracer.getattr  s    ##D(4IJJrE   c                 6    || _         t        | 	  ||||      S r   )r  r|  r  )r   r   forwardr   r   r  s        rC   r  zHFTracer.call_module  s!    #w"1gtV<<rE   c                     t        ||       S r   )rm  )r   r  s     rC   r  zHFTracer.proxy  s    tT""rE   r  .concrete_argsdummy_inputs6complete_concrete_args_with_inputs_not_in_dummy_inputsc           
      N
   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 ]r  }||v rt        || j.                        s$t1        |      j2                  j5                  d      r#|j                  | j7                  |||             et9        d	| d
       |j;                         D ci c]3  \  }}|t        |t        j<                        r|j?                  d      n|5 }}}|j                  j                         D ]K  }|j@                  t         j                  jB                  k(  s+|j                  |vs:i |d|j                   <   M || _"        | jF                  D ci c]  }|tI        tK        t        |             c}| _&        tO               | _(        | jL                  j;                         D ]4  \  }\  }}tS        t        ||       | jP                  jU                  |       6 	 tV        |   ||      | _-        | jL                  j;                         D ]  \  }\  }}tS        t        ||        	 | jZ                  j\                  D ]  }|j^                  dk(  r|j`                  |v rd|_1        t        j<                  |_        n|g}te        jf                         }|r?|ji                  d      }d||<   |tk        |jl                  j                               z  }|r?to        |j                               D ]  }| jZ                  jq                  |        |j^                  dk(  sd|_         | jZ                  S c c}w c c}}w c c}w # | jL                  j;                         D ]  \  }\  }}tS        t        ||        w 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 r  r      r  r   )_deserialize_graph_module_CodeOnlyModulezCould not generate input named z8 for because root is not a transformers.PreTrainedModel.rz   z**r3  r  r   r   rJ  )9r!  r"  r;   r}   r
   Moduler1  r#  r  r  r6   r)  r~   r   updater:   r  r  r  r   r   r   r   supported_archsr  r  
startswithr  r  itemsr   r   r  VAR_KEYWORDr  _TORCH_METHODS_TO_PATCHr  r  patched_torch_methodssetr  setattraddr|  tracegraphnodesoprU  r   r  OrderedDictr   r   usersreversed
erase_node)r   r  r3  r4  r5  sigparamr   input_namesr   sequence_lengthr   num_choicesr  r  input_concrete_metasrU  r  r  origr   r  to_visit	to_deleter   userr  s                              rC   rF  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QR"5j\ B5 5 	" '-lln
"
F Z-M		&)SYY
 
 ^^**, 	7EzzW..:::uzzQ\?\46EJJ<01	7 (SWSoSo&
IOF,WUF-CDD&
" %)%?%?%E%E%G 	$!D/7DE4)MMd#	$	+t=IDJ#'#=#=#C#C#E +iq$tT*+
 JJ$$ 	!Dww-';;+- "DI !&DI !%vH + 7 7 9I"$LLO'+	! D$88 #
 !))9 : 4

--d34
 ww(" 	/	!2 zziD
&
 $(#=#=#C#C#E +iq$tT*+s   #:S8S!<!S'S, ,8T$r  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   r  )r   r  s     rC   r   zKHFTracer._stateless_mod_instanciation_depends_on_proxies.<locals>.<genexpr>g  s     Mt:dE*Ms   )any__dict__r  )r   r  s     rC   /_stateless_mod_instanciation_depends_on_proxiesz8HFTracer._stateless_mod_instanciation_depends_on_proxiesb  s"    
 Ms||7J7J7LMMMrE   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.
        r1   r   r   FTr   )r]  r  r  lowerr{  r  r  
add_module)r   r  idxmod_namepathalready_inserteds         rC   _insert_module_as_submodulez$HFTracer._insert_module_as_submodulei  s     ??D==))//11SE" dii&tyy$'3.#' Zq&D1HC dii&  II  s+rE   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#  buffersre  )r   r  r  rc  r  s       rC   rg  zHFTracer.path_of_module  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   rj  r  s      rC   rl  zHFTracer.is_leaf_module  s3    HHKK 
QVQXQg$R
 	
rE   )is_backward_compatibleobjr   c                 r     t        |d             }|j                  j                  dk(  r|j                  S |S )zCalled when a proxy object is has the keys() method called.
        This is what happens when ** is called on a proxy. This should return an iterator if ** is supposed to work in
        your custom tracer.
        r:   z**kwargs)r  r  rU  ro  )r   rn  	attributes      rC   r:   zHFTracer.keys  s7     -KV,.	88??j(&&&rE   )NNN)NNT)*r  r  r  r  r  bool__annotations__r  rA  r-   r   r/   r=  r(  r  r<   r   r   r   r}   r   r  ru  r.  r   r  r  r  r	   r
   r;  r   r   r   rF  r]  re  rg  rl  r   r:   r  r  s   @rC   r  r    s    %)T((,, 1B0C(/[dIeO)-B }$}25}>B3i}	c5<<	 }~AH#LKC K3 KtCQTH~ K=# 3715GK@EHHOOXc3h%778@  S#X/@ tCH~.	@
 AE@ 
@DN299 NQU Nryy S 0"))  "
 
 
PT 

 $/ C  0rE   r  r  rP  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 )Nr   r   , z(The model does not have input(s) named: z&, expected a subset of the following: )r!  r"  r1  rC  r#  r:   r   joinr   r  r  r6   )r  rP  rN  formatted_input_namesformatted_allowed_input_namesr   s         rC   get_concrete_argsrx    s    


EMM
*CCNN$7$7$9 ::25k2Ba2GATYYWbMc(,		#..2E2E2G(H%67L6M N-.0
 	

 (+~~'<'<'>\!!&&P[B[AFFAII\\\s   	C9C9c                     | j                   j                  t        vr:dj                  t              }t	        d| j                   j                   d|       y )Nrt  zModel z) is not supported yet, supported models: )r  r  _SUPPORTED_MODELSru  r  )r  supported_model_namess     rC   check_if_model_is_supportedr|    sU    '88 $		*; <!U__--..WXmWno
 	
 9rE   disable_check
tracer_clsc                 `   || j                   j                         }t        |      }t        | |      }|st	        |         |       }|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"])
        ```
    r:  )r4  r:   r   rx  r|  rF  r}   r  r   r  r  r  r{   )r  rP  r}  r~  r3  rt  traced_graphtraceds           rC   symbolic_tracer    s    < ((--/{#K%e[9M#E* \F<<]<CLXX!!%6FLLFM (-F$LLFMMrE   r   r   )NNg       @FF)F)NN)ry   )
      N)r  r  r  r!  r(  rf  osr  r  typingr   r   r   r   r   r   r	   r}   r
   torch.fxr   r   r   r   torch.fx._compatibilityr   torch.fx.proxyr   r1   r   r   r   models.autor   models.auto.modeling_autor   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   utilsr*   r+   r,   r-   r.   peftr/   
get_loggerr  loggerenvironr=   upperr
  r<   rD   (_REGULAR_SUPPORTED_MODEL_NAMES_AND_TASKS_REGULAR_SUPPORTED_MODELSitemr;   r   extend_SPECIAL_SUPPORTED_MODELSrH  sortedrC  rz  r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r  r  r  r  r  r!  r0  r7  r;  r>  r@  rB  rK  rN  rV  rY  r[  rj  	Embedding
functional	embedding	LayerNorm	GroupNormLinearreluReLUwhereabsr  r   catstackrE  mulr   matmulbmmbaddbmmr   repeatrepeat_interleaverollflipindex_selectgatherConv1dConv2dsqueeze	unsqueezerF  one_hotMSELossCrossEntropyLossBCEWithLogitsLossrg  rk  rr  rm  r  rx  r  r  r   r  r  r;  rx  r|  rq  r  r   rE   rC   <module>r     s          	   C C C   6 6 1 ) 9 9 $     *   
		H	%JJNN?B7==?CWW 
 8<%%&%eCcN34% 
#Y%P+, (Z  4 VD$!(()N)VQU)VW!(()Nt)TU	V   &%>AZ%Z![\] d
 ejZOS ! J*634 3	-T 	- &'D - $( ," '+ !.H #' 8 23! % 89t PA- (, .( 26 .0 49d .*-$-*-"$-&:----"&,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 ,^))e ))X_' _,	+ 	*c c (SWX[S\J] Sv Sl]RYY ]T#Y ]
 
 (,!)	22$s)$2 2 X	2
 2rE   