CoolFace
Apppublic

Aluode/PerceptionLabPortable

sourceHugging Faceupdated 9mo agoView on Hugging Face
0likes
fx.cpython-310.pyc320 linesDownload Raw Back to __pycache__
1o

20�Yib��@snUddlZddlZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl3Z4ddlmZm
Z
mZmZmZddlZddlmmZddlmZddlmZmZmZmZmZddlmZddlm Z ddl!m"Z"dd	l#m$Z$dd5l%m&Z&m'Z'm(Z(ddl)m*Z*m+Z+ddl,m-Z-dd
l.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z=m>Z>m?Z?m@Z@mAZAmBZBmCZCddlDmEZEmFZFeF�r�ddlGmHZHe$�IeJ�ZKejL�Mdd��N�eEvZO	d�dePe*deeeQeReQfdeReQfdd�ZSgd�ZTddgZUgZVeTD]ZWeXeWeY��reV�ZeSd�ieW����qeV�ZeSeW���qgd�Z[e\e]e^eVe[���Z_da`dd�Za	d�d d!�Zbd"d#�Zcd$d%�Zdd&d'�Zed(d)�Zfd*d+�Zgd�d,d-�Zhd.d/�Zidd0�d1d2�Zjd3d4�Zkd5d6�Zld�dd0�d7d8�Zmd�dd0�d9d:�Znddd;�d<d=�Zodd0�d>d?�Zpd@dA�Zqdd0�dBdC�Zrdd0�dDdE�ZsddddF�dGdH�ZtddddF�dIdJ�ZudKdL�ZvdMdN�ZwdddO�dPdQ�Zxdd0�dRdS�ZydTdU�ZzdddV�dWdX�Z{dYdZ�Z|d�d[d\�Z}d]d^�Z~d_d`�Zdadb�Z�dcdd�Z�d�dedf�Z�d�dgdh�Z�didj�Z�dkdl�Z�dmdn�Z�d�dpdq�Z�	d�dsdt�Z�dudv�Z�dwdx�Z�dydz�Z�d{d|�Z�iejj�ea�ejj�j�eb�ejj�ec�ejj�ed�ejj�ee�ej�ef�ejj�j�eh�ejj�eg�ej�ei�ej�ej�ej�ek�ej�el�ej�em�ej�en�ej�eo�ej�ep�ej�j�eq�iej�er�ej�es�ej�et�ej�j�eu�ej�ev�ej�j�ew�ej�ex�ej�e}�ej�e~�ej�j�e�ej�ey�ej�j�ez�ej�e{�ej�j�e|�ejj�e��ejj�e��ej�e���ej�j�e�ej�e�ej�j�e�ej�e�ejj�j�e�ejj�e�ejj�e�ejj�e�ej�e�i	�Z�eYe
e
fe�d}<e�e�ejj�j�<Gd~d�de�Z�Gd�d��d�e��Z�Gd�d��d�e��Z�Gd�d��d�e��Z�	d�d�e
d�eed�ed�ed�fd�ee
egefde
fd�d��Z�Gd�d��d�eP�Z�d�e
de\e
e
ffd�d��Z�d�d��Z�d�ePe&de
ege�ffd�d��Z�e�d�e&fie�e&�d��Z�e�d�e'fie�e'�d��Z�e�d�e(fie�e(�d��Z�d�d�e�d�e�d�eeRe�fd�d��Z�Gd�d��d�e�Z�d�ej�d�eReQfd�d��Z�d�d�d��Z�d�d�d��Z�dde�fd�d�d�eeReQd�e�d�ePe�def6d�d��Z�dS)��N)�Any�Callable�Literal�Optional�Union)�nn)�Graph�GraphModule�Node�Proxy�Tracer)�
compatibility)�
is_fx_tracing��ParameterProxy�)�logging)�Cache�DynamicCache�StaticCache)�PretrainedConfig�PreTrainedModel)�7get_values)�,MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING_NAMES� MODEL_FOR_BACKBONE_MAPPING_NAMES�!MODEL_FOR_CAUSAL_LM_MAPPING_NAMES�MODEL_FOR_CTC_MAPPING_NAMES�3MODEL_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_NAMES�0MODEL_FOR_NEXT_SENTENCE_PREDICTION_MAPPING_NAMES�#MODEL_FOR_PRETRAINING_MAPPING_NAMES�*MODEL_FOR_QUESTION_ANSWERING_MAPPING_NAMES�-MODEL_FOR_SEMANTIC_SEGMENTATION_MAPPING_NAMES�,MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING_NAMES�/MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING_NAMES�(MODEL_FOR_SPEECH_SEQ_2_SEQ_MAPPING_NAMES�,MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING_NAMES�,MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING_NAMES�6MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING_NAMES�MODEL_MAPPING_NAMES�)�ENV_VARS_TRUE_VALUES�is_peft_available)�	PeftModelZ
FX_DEBUG_MODE��8model_name�supported_tasks�returncCs�idt�dt�dt�dt�dt�dt�dt�dt�d	t�d9t	�dt10�dt�d
t�dt
�dt�dt�dt�tttd��}|durC|��}t|t�rK|g}g}|D]}||�|d�}|r`|�|�qO|S)N�defaultZpretrainingznext-sentence-predictionz	masked-lmz	causal-lmz11seq2seq-lmzspeech-seq2seqzmultiple-choicezdocument-question-answeringzquestion-answeringzsequence-classificationztoken-classificationzmasked-image-modelingzimage-classificationzzero-shot-image-classificationZctczaudio-classification)zsemantic-segmentationZbackbonezimage-feature-extraction)r-r$r#r!rr'r)r"rr%r(r*r rr,rrr&rr�keys�12isinstance�str�get�append)r3r4Ztask_mappingZmodel_class_names�task�13class_name�r>�E:\DocsHouse\542 percep lab latest\PerceptionLab\PerceptionLab_Portable\python_embed\Lib\site-packages\transformers/utils/fx.py�%_generate_supported_model_class_namesMsd��������	�14���
������1516�r@):ZaltclipZalbertZbartZbertZbitnetZ17blenderbotzblenderbot-smallZbloomZclipZconvnextZdebertaz18deberta-v2Zdinov2Zdinov3_convnextZ19dinov3_vitZ20distilbertz21donut-swinZelectraZgpt2Zgpt_neoZgptjZhieraZhubertZijepaZlayoutlm�llamaZcohereZlxmertZm2m_100ZmarianZmbart�
megatron-bertZ	ministralZmistralZmixtralZ22mobilebertZmt5Znezha�optZpegasusZplbartZqwen2Z	qwen2_moeZqwen3Z23qwen3_nextZ	qwen3_moeZresnetZrobertaZ	segformerZspeech_to_textZspeech_to_text_2ZswinZt5ZtrocrZvitZvjepa2ZxglmZwav2vec2rArC)
Z
CLIPTextModelZCLIPTextModelWithProjectionZCLIPVisionModelZCLIPVisionModelWithProjectionZAltCLIPTextModelZAltCLIPVisionModelZGitVisionModel�GPT2DoubleHeadsModelZSpeech2Text2DecoderZTrOCRDecoder�PeftModelForCausalLM�PeftModelForSeq2SeqLMZVJEPA2ForVideoClassificationcCs*tjg|j�|jjd�Rd|jjd��S�N������meta)�device�dtype)�torch�empty�shape�weightrK��self�inputr>r>r?�torch_nn_embedding�s*rS�@FcCs&tjg|j�|jd�Rd|jd��SrG)rLrMrNrK)rRrOZpadding_idxZmax_normZ	norm_typeZscale_grad_by_freq�sparser>r>r?�torch_nn_functional_embedding�s&rVcC�|S�Nr>rPr>r>r?�torch_nn_layernorm��rYcCrWrXr>rPr>r>r?�torch_nn_groupnorm�rZr[cCs tj|jdd�|jfdd�S)NrHrI�rJ)rLrMrNZout_featuresrPr>r>r?�torch_nn_linear�s r]cC�|SrXr>��xr>r>r?�24torch_relu�rZracCrWrXr>)rQr`r>r>r?�
torch_nn_relu�rZrbcCs|std��|S)Nz>Don't support in-place functional.relu for MetaTensor analysis��25ValueError)r`Zinplacer>r>r?�torch_nn_functional_relu�srecCs$|jdd�|jdd�|jdd�S�NrIr\��to)�	conditionr`�yr>r>r?�torch_where�s$rk��outcCs|durtd��|S)Nz2Don't support in-place abs for MetaTensor analysisrc)rRrmr>r>r?�	torch_abs�srncOs�t|�}d}|dkrd}|d}n|dkr|\}}n|\}}}t|t�r(t|�}t|t�r1t|�}t|t�r:t|�}|�d|�}|�d�}tj||||dd�S)Nr.rr�steprKrI�rKrJ)�lenr8�float�intr:rLrM)�args�kwargs�nro�start�endrKr>r>r?�torch_aranges"26272829303132rycOsRt|�}t|�dkrd|d<nd|d<t|�}|�dd�tj|i|�ddi��S)Nr.Z33fill_valuerJrI)�listrq�dict�poprL�full)rtruZkwargs_without_devicer>r>r?�34torch_fulls35r~cs��dur36|dur37d��dur|dur|��dkr |d����dd�|D�}t|d�}t�fdd�|D��}|d��|g|�dd�}tj|dd�S)	NrcSsg|]}|j�qSr>)rN)�.0�tr>r>r?�38<listcomp>+sztorch_cat.<locals>.<listcomp>c3s�|]}|�VqdSrXr>)rrN��dimr>r?�	<genexpr>-s�ztorch_cat.<locals>.<genexpr>r.rIr\)r�rz�sumrLrM)�tensorsr��axisrmZshapesrNZconcatenated_dimZfinal_shaper>r�r?�	torch_cat$s"r�cCsp|dur39|dur40d}|dur|dur|}|dkr"|d��d|}t|dj�}|�|t|��tj|dd�S�Nrr.rIr\)r�rzrN�insertrqrLrM)r�r�r�rmrNr>r>r?�torch_stack2sr�)�alpharmc	Cs�t|tj�s
tj|dd�St|tj�stj|dd�St|��|���}t|j�dg||��}t|j�dg||��}g}t|�D]}|�	t||||��qEtj41|dd�S)NrIr\r.)r8rL�Tensor�42empty_like�maxr�rzrN�ranger;rM)	rR�otherr�rm�43max_lengthZinput_shapeZother_shaperN�ir>r>r?�	torch_add>sr�cCst|||d�S)Nrl)r�)rRr�rmr>r>r?�	torch_mulL�r�cC�44t||�SrX)r�)rQr�r>r>r?�torch_tensor_mulP�45r�c46Cs�|��}|��}d}|dkr|dkrd}n�|dkr(|dkr(|�d�|�d�f}n�|dkr7|dkr7|�d�f}n|dkrF|dkrF|�d�f}npt|��|���}t|j�}t|j�}|dkrbdg|}|dkrk|�d�dg||t|j�}dg||t|j�}g}t|�D]}	|�t||	||	��q�|d|d<|d|d<|dkr�|�d�|dkr�|�d�|dur�tj	ddd�Stj47|d	di�S)48Nr.rrrH������rIr\rJ)r��sizer�rzrNr;r�r|rL�tensorrM)49rRr�rmZd1Zd2rNr�Zshape1Zshape2r�r>r>r?�torch_matmulTs@505152535455r�cCs:|durtd��|j\}}}|j\}}}tj|||dd�S)Nz2Don't support in-place bmm for MetaTensor analysisrIr\)rdrNrLrM)rRZmat2rm�56batch_sizerv�m�_�pr>r>r?�	torch_bmmxs57r���betar�rmcCs|durtd��t||�S)Nz6Don't support in-place baddbmm for MetaTensor analysis)rdr�)rR�batch1�batch2r�r�rmr>r>r?�
torch_baddbmm�s58r�cCst||||||d�S)Nr�)r�)rQr�r�r�r�rmr>r>r?�torch_tensor_baddbmm��r�cGs&dd�|D�}tj|g|�R��d�S)Ncss�|]59}tj|dd�VqdS)�cpur\N)rLr�)r�operandr>r>r?r��s�ztorch_einsum.<locals>.<genexpr>rI)rL�einsumrh)ZequationZoperandsZconcrete_operandsr>r>r?�torch_einsum�sr�cGs:t|j�}t|�D]\}}|||9<q	tj|dd�Srf)rzrN�	enumeraterLrM)rQ�sizesrNr�r`r>r>r?�torch_tensor_repeat�s60r�)r��output_sizecGs�t|�}|dkr|dur|n|d��g}nBt|dj�}|dur1|dkr*|d}nt|�g}d}|d}t|t�sAt�|�dkrL||t|�9<n|durR|n|��||<tj|ddi�S)Nr.rrrJrI)	rqr�rzrNr8rsrLZnumelrM)r�r�rt�num_argsrNZrepeatsr>r>r?�torch_repeat_interleave�s6162r�cCs&t|j�}t|�||<tj|ddi�S�NrJrI)rzrNrqrLrM)rRr��indexrmrNr>r>r?�torch_index_select�s63r�cC�t|||�SrX)r��rQr�r�r>r>r?�torch_tensor_index_select��r�)�sparse_gradrmcCs(t|j�}|j|||<tj|ddi�Sr�)rzrNrLrM)rRr�r�r�rmrNr>r>r?�torch_gather�s64r�cCr�rX)r�r�r>r>r?�torch_tensor_gather�r�r�cCr^rXr>)rRZshifts�dimsr>r>r?�65torch_roll�rZr�cCr^rXr>)rRr�r>r>r?�66torch_flip�rZr�cCr^rXr>)rQr�r>r>r?�torch_tensor_flip�rZr�cCs�|jd}d}|j}|dkrd}|dkrt|j�}|durFt|j�}t�|d|d|jd|jddd|jdd�}||d<|j|d<t	j67|d	d68�S)NrH�valid�rr�samerrr.r�rIr\�rN�paddingrz�math�floorZdilationZkernel_sizeZstrideZout_channelsrLrM)rQrRZl_inrNr�Zl_outr>r>r?�torch_nn_conv1d�s6970718�72r�cCs�|jdd�\}}d}|j}|dkrd}|dkrt|j�}|durnt|j�}t�|d|d|jd|jddd|jdd�}t�|d|d|jd|jddd|jdd�}||g|dd�<|j|d<t	j73|d	d74�S)Nr�r�r�r�rrr.�����rIr\r�)rQrRZh_inZw_inrNr�Zh_outZw_outr>r>r?�torch_nn_conv2d�s$75768�8�77r�cCsrt|j�}|dur|dkr|��|}||dkr|�|�ng}|D]}|dkr*q#|�|�q#|}tj|dd�Sr�)rzrNr�r|r;rLrM)rRr�rNZ	new_shapeZ	dim_valuer>r>r?�
torch_squeeze�s7879�r�cCr�rX)r��rQr�r>r>r?�torch_tensor_squeezer�r�cCs<t|j�}|dkr|��d|}|�|d�tj|dd�Sr�)rzrNr�r�rLrM)rRr�rNr>r>r?�torch_unsqueezes8081r�cCr�rX)r�r�r>r>r?�torch_tensor_unsqueezer�r�cKsDtjtj|dd�fi|��}t|tj�r|�d�Stt|dd���S)Nr�r\rIcSs82|�d�S�NrIrgr_r>r>r?�<lambda>s83z*torch_unique_consecutive.<locals>.<lambda>)rL�unique_consecutiveZ84zeros_liker8r�rh�tuple�map)rRru�outputr>r>r?�torch_unique_consecutives85r�rHcCs.|dkrtd��t|j�|g}tj|dd�S)NrzEDon't support automatic num_classes inference for MetaTensor analysisrIr\)rdrzrNrLrM)r�Znum_classesrNr>r>r?�torch_nn_functional_one_hotsr�r�c	Cs:|jd}|jd}tjg|jdd��|�|�Rdd�S)Nr�rHrIr\)rNrLrM)	�query�key�valueZ	attn_maskZ	dropout_pZ	is_causal�scaleZ
target_length�head_dimr>r>r?�0torch_nn_functional_scaled_dot_product_attention"s8687&r�cC�$|jdkr	|j}nd}tj|dd�S�N�none)r.rIr\�Z	reductionrNrLrM�rQrR�targetrNr>r>r?�torch_nn_mseloss*�88r�cCr�r�r�r�r>r>r?�torch_nn_crossentropyloss2r�r�cCr�r�r�r�r>r>r?�torch_nn_bcewithlogitsloss:r�r�cCs^dd�}t|tj�r)t|t�rtt||��}n||�}t�tj|dd�|��d�St�||�S)NcSsHt|tj�r"tj|dd�}|jtjtjtjtjfvr |�	tj89�}|S|S)Nr�r\)r8rLr�Z	ones_likerKZfloat16�float32Zfloat64Zint32rhZint64)r�Zconcreter>r>r?�to_concreteCsz%operator_getitem.<locals>.to_concreter�r\rI)	r8rLr�r�r��operator�getitemr�rh)�a�br�r>r>r?�operator_getitemBs90r��_MANUAL_META_OVERRIDEScsheZdZdZdd�Zedd��Zedd��Z�fdd	�Z�fd91d�Z	dd
�Z92dd�Z�fdd�Z�Z
S)�HFProxyzI93    Proxy that uses metadata to handle data-dependent control-flow.94    cC�95||_dSrX)�	_metadata)rQ�metadatar>r>r?�install_metadata�r�zHFProxy.install_metadatacCs|j�dd|fi�S)N�call_methodr�)�tracer�create_proxy�rQr>r>r?rN�sz
HFProxy.shapecCs96t|d�S)NrJ)�MetaDeviceAttributer�r>r>r?rJ�s97zHFProxy.devicecs(t|d�r|jdurt|j�St���S�Nr�)�hasattrr�rq�super�__len__r���	__class__r>r?r��s9899zHFProxy.__len__cs$t|d�r
|jdur
|jSt���Sr�)r�r�r��__bool__r�rr>r?r�s100zHFProxy.__bool__cCs|dkr	|�|�St||�Sr�)�__getattribute__�HFAttribute)rQ�kr>r>r?�__getattr__�s101102zHFProxy.__getattr__cCs|j�dtj|||fi�S�N�
call_function)r�r�r��setitem)rQ�indices�valuesr>r>r?�__setitem__�szHFProxy.__setitem__cs*t|d�r|jdur||jvSt��|�Sr�)r�r�r��__contains__)rQr�rr>r?r
�s103zHFProxy.__contains__)�__name__�104__module__�__qualname__�__doc__r��propertyrNrJr�rrrr
�
__classcell__r>r>rr?r��s105106r�c@s.eZdZdefdd�Zedd��Zdd�ZdS)	r�attrcCsB||_||_|j|_d|_t|jd�r|�t|jj|��dSdSr�)�rootrr��_noder�r��getattrr�)rQrrr>r>r?�__init__�s�zHFAttribute.__init__cCs0|jdur|j�dtj|j|jfi�j|_|jSr)rr�r��builtinsrrr�noder�r>r>r?r�s107 zHFAttribute.nodecOs|j�d|j|jf||�S)Nr�)r�r�rr)rQrtrur>r>r?�__call__�szHFAttribute.__call__N)rrrr9rrrrr>r>r>r?r�s108	109rc@seZdZdS)r�N)rrrr>r>r>r?r��sr�c@s.eZdZdZdeefdd�Zedd��ZdS)�HFCacheProxyzP110    Proxy that represents an instance of `transformers.cache_utils.Cache`.111    �orig_cache_clscCr�rX)�_orig_cache_cls)rQrr>r>r?�install_orig_cache_cls�r�z#HFCacheProxy.install_orig_cache_clscCs t|d�s	td��|jj|jS)Nrz?The original Cache class must be installed to the HFCacheProxy.)r��RuntimeErrorr��_CLASSES_TO_PATCHrr�r>r>r?r�s112zHFCacheProxy.__class__N)	rrrr�typerrrrr>r>r>r?r�s113r�function�op_typerr��get_attr�proxy_factory_fncst������fdd��}|S)Ncs�t�s114�|i|��Sg��fdd�}tjj�||�tjj�||�t��dkrR�dj}�dkr4�}n�dks<�dkr@�j}ntd��d���|j	�|||�d	�S�|i|��S)115Ncst|t�r��|�dSdSrX)r8rr;)r��Z
found_proxiesr>r?�check_proxy�s116�z4create_wrapper.<locals>.wrapper.<locals>.check_proxyrrr�r%zop_type z not supported.�r&)117rrL�fxr�
map_aggregaterqr�rrdr�)rtrur(r�r��r#r$r&r'r?�wrapper�s118zcreate_wrapper.<locals>.wrapper)�	functools�wraps)r#r$r&r-r>r,r?�create_wrapper�sr0csTeZdZdZ	d119dedeedfdeeefde	e120egeff�fdd	�
Z
�ZS)�HFProxyableClassMetazW121    Metaclass that creates a class with its main methods wrapped to be proxyable.122    N�name�bases.�attrsr&c	s�t��||||�}t|�D]=}t||d�}|durq
|dkr!d}n|�d�r)d}nt�|�r1d}n123t�|�r9d}nd}|durJt||t	|||d��q
|S)Nrr�__r�r))124r��__new__�dirr�125startswith�inspect�ismethod�126isfunction�setattrr0)�clsr2r3r4r&�	attr_namerr$rr>r?r6s$127128129�zHFProxyableClassMeta.__new__rX)rrrrr9r�r"r{rrrr130rr6rr>r>rr?r1�s	��131�132��r1r�cCst|d�}||fS)zo133    Wraps `target` to be proxyable. Used for tensor creators like `torch.ones`, `torch.arange` and so on.134    r)r0)r�r-r>r>r?�gen_constructor_wrappers135r?cCsHt|t�rdSt|tjj�r"t|t�rt|d�std|����|jS|S)z\Returns the underlying metadata for HFProxies, and behaves like the identity for the others.rIr�zNo metadata was found for )	r8r�rLr*rr�r�r r�)�vr>r>r?�_proxies_to_metas's136rArcsdtdtf�fdd�}|S)Nrvr5cs*ttt�s	td��t|t�}|���|S)NzJCannot create HFCacheProxy because there is no HFTracer currently tracing.)r8�_CURRENT_TRACER�HFTracerr rr)rvZcache_proxy�rr>r?�cache_proxy_factory_fn3s137138139140z=create_cache_proxy_factory_fn.<locals>.cache_proxy_factory_fn)r141r)rrEr>rDr?�create_cache_proxy_factory_fn2srF�ProxyableCacher)�ProxyableDynamicCache�ProxyableStaticCache�142��low�high�forbidden_valuescCs8|durg}t�||�}||vrt�||�}||vs|SrX)�random�randint)rLrMrNr�r>r>r?�_generate_random_intOs�rQcs�eZdZUdZdZeed<dZeed<gd�Ze	e143eee
eiZe�s%efneefZefdf�fdd�	Zd	d144dedeed
eedeeejff145dd�Zd8�fdd�	Zdd�Zdededeeeffdd�Z �fdd�Z!dd�Z"e#j$de%ej&j'e(d efffd!d"��Z)			d9de%ej&j'e(d effd#e*eeefd$e*eeefd%ede+f146�fd&d'�
Z,d(e&j'defd)d*�Z-d(e&j'defd+d,�Z.d(e&j'def�fd-d.�Z/d/ej&j'd0edef�fd1d2�Z0e1dd3�d4d5defd6d7��Z2�Z3S):rCz�147    Tracer that is able to symbolically trace models from the library. To do that, it uses the HFProxy instead of the148    regular PyTorch torch.fx.Proxy.149    T�proxy_buffer_attributes�allow_insert_stateless_mods)�arange�zerosZonesr}Z	full_like�eyerMr��clampZfinfoZtrilr>cst�j||d�dS)N)�autowrap_modules�autowrap_functions)r�r)rQrXrYrr>r?rvr�zHFTracer.__init__�modelr�150input_namerN�input_namesr5cs�t|d|j�j}|j�i}d}|dv�r|d}|gtt��tt��tt��tt��tt	��tt151��vrAtj|tj
�d�|d<|S|gtt��tt��d�vrgtj|tj
�d�|d<tj|tj
�d�|d	<|S|tt�vr�t|jd152�ry|jjdur}td��|jjd
kr�||jjf}	tj}153n'|jjdkr�|f}	tj
}154n|jjdkr�||jjf}	tj}155n156td|jj�d���tj|	|157�d��|d<|S|gtt��tt��tt��tt��tt��tt��d�d�d�vr�tj|tj
�d�|d<|S|gtt��v�rtj|tj�d�|d<|Std|�d|�d���d|v�rm|d}t|jdd�}|du�rFt|jd��r3|jjj }nt|jd��r@|jj!j }nt"�t"�f}t|jdd�}t#|t$j%j&��sY||f}|\}
}tj|||
|tj�d�||<|Sd|v�r�tjg|�d�Rtj'�d��||<|Sd |v�r�tjg|�|jj(�Rtj'�d��||<|Sd!|v�r�|d}t|jd"d�du�r�|jj)d#k�r�|jj*}n|jj+}t,|�dk�r�||d$|d%|f}n||d$|f}tj|tj'�d�||<|Sd&|v�r�tj||jj-gtj'�d�||<|Sd'|v�rtj||jj.gtj'�d�||<|Sd(|v�r)tj|tj'�d��||<|Sd)|v�rF|\}}t"d*d+d,�}tj||tj'�d�||<|Sd-|v�rjd.|v�r[|d|d$|g}n|}tj|tj
�d�||<|Sd/|v�r|tj|tj
�d�||<|Sd.|v�r�|jj)t/v�r�td0|jj)�d1���|jj0}|jj+|jj0}|d|||f�t1��fd2d3�t2|jj3�D��}|||<|S||jj+g}tj|tj'�d�||<|S)4z4Generates dummy input for model inference recording.�class_for_deserialization�)�labels�start_positions�
end_positionsrrpr_ZXLNetForQuestionAnsweringr`ra�problem_typeNz�Could 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".Z158regressionZsingle_label_classificationZmulti_label_classificationz�Expected model.config.problem_type to be either: "regression", "single_label_classification", or "multi_label_classification", but "z" was provided.rDrErFz!Generating the dummy input named z for z is not supported yet.Zpixel_values�159image_size�
vision_config�encoder�num_channels�Zbbox�Zinput_featuresZ
inputs_embeds�embedding_sizerBr.rZvisual_featsZ160visual_pos�inputsZinput_valuesi'i N�rLrM�mask�past_key_valuesZidszMSymbolic 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.c3s4�|]}tj�tj�d�tj�tj�d�fVqdS)rpN)rLZrandrr)rr��Zcache_shaperJr>r?r�s���161�z1HFTracer._generate_dummy_input.<locals>.<genexpr>)4rrrrJrr#r"rr+rrrLrU�longr%rr(r��configrbrdZ162num_labelsr�r$r*rr!r'r&r�NotImplementedErrorrdrcrerQr8�collections�abc�IterablerrZinput_feat_per_channelZ163model_typeriZhidden_sizerqZvisual_feat_dimZvisual_pos_dim�"_FX_SUPPORTED_MODELS_WITH_KV_CACHEZnum_attention_headsr�r�Znum_hidden_layers)rQrZr[rNr\Zmodel_class_nameZinputs_dictZkv_cache_lengthr�Zlabels_shapeZlabels_dtypercrf�height�widthriZembedding_shaper�Z164seq_lengthZ165mask_shapeZ	num_headsr�ZpkvZshape_with_hidden_sizer>rnr?�_generate_dummy_inputys`166����������������w���������	�j�h��167168169�S170�"O171����M172�1738174���175�6176���177�-178�$179�180181182�183��184��zHFTracer._generate_dummy_inputNcs0t��|||||||�}|dkr||jvr|�|j|�|S||jvr,d|vr,d|d<z�tjj�|t	�}	tjj�|t	�}185d}d|_186d|_|dkrct�
||�}||	i|187��}
t|
tj�rb|
jdd�}
nw|dkr}t|	dj|�}t�
||�}||	i|188��}
n]|d	kr�t|d189�s�t|�d���|j�|�}t|�}|tvr�t||g|	�Ri|190��}
n1|j|	i|191��}
n(|dkr�|j}|�d
�}|D]}t||�}q�t|tj�r�|jdd�}
n|}
nd}|r�t|t�s�td��|�|
�Wn$t�y}zt�rt�d|�d|�d|���WYd}~nd}~wwd|_192d|_|S)N�placeholderrJrITrr\r�r�call_module�orig_forwardz/ does not have an attribute called orig_forwardr%�.Fz"Don't support composite output yetzCould not compute metadata for z target z: ) r�r��	meta_argsr��orig_fnsrLr*rr+rA�_disable_module_getattr�_disable_call_moduler�r:r8r�rhrrr��AttributeErrorrZ
get_submoduler"r{�splitrrd�	Exception�_IS_IN_DEBUG_MODE�warnings�warn)rQ�kindr�rtrur2Z	type_exprr&�rvZ193args_metasZkwargs_metasZshould_install_metadataZmeta_targetZmeta_out�method�mod�mod_typeZattr_itrZatomsZatom�err>r?r�)sj194�195196197198���zHFTracer.create_proxycs|t�dd�r|S�fdd�}t|tjj�r$||�j��|�}|dur$|S�jr<t|tj�r<||�j�	�|�}|dur<|S|S)NrFcs�|D]<\�}�|ur>�|vr8i}dt��j�jvr(�jsdn���fdd�|d<�jd�difi|��}||�<|�SqdS)Nr&cst�|���SrXr)r)�attr_valrvrQr>r?r�}szLHFTracer._module_getattr.<locals>.maybe_get_proxy_for_attr.<locals>.<lambda>r%r>)r9�	signaturer��199parametersZparam_shapes_constant)r�Zcollection_to_search�parameter_proxy_cacher�ruZ	val_proxyr�)r�rvr?�maybe_get_proxy_for_attrts���z:HFTracer._module_getattr.<locals>.maybe_get_proxy_for_attr)200rr8rLr�	ParameterrZnamed_parametersrRr�Z
named_buffers)rQrr�r�r�Zmaybe_parameter_proxyZmaybe_buffer_proxyr>r�r?�_module_getattros ��zHFTracer._module_getattrrr�r�cCs|�|||�SrX)r�)rQrr�r�r>r>r?r�r�zHFTracer.getattrcs2t|dd�r
||i|��S||_t��||||�S)Nr�F)rr{r�rz)rQr��forwardrtrurr>r?rz�szHFTracer.call_modulecCs201t||�SrX)r�)rQrr>r>r?�proxy�r�zHFTracer.proxyr.c
cs0�dd�|jD�|_t�|_|j��D]\}\}}tt||�|j�|�qg}t�	|�}t202j��D]9\}}|dur?||ur?q2|�d�sEq2|j
��D] \}}	|j��D]\}203}||uri|�||204|f�t||205|	�qSqJq2dV|j��D]\}\}}tt||�qti|_t�|_|D]\}}206}t||207|�q�dS)NcSsi|]208}|ttt|���qSr>)r?rrL)rr�r>r>r?�209<dictcomp>�s�z.HFTracer.patch_for_tracing.<locals>.<dictcomp>Ztransformers)�_TORCH_METHODS_TO_PATCHZpatched_torch_methods�setr~�itemsr<rL�addr9�	getmodule�sys�modulesr8r!�__dict__r;)
rQrr2r-�origZpatchedZmodule_of_modelr�Zorig_clsZpatched_clsr>rr�r>r>r?�patch_for_tracing�s>��210211����zHFTracer.patch_for_tracing�
concrete_args�dummy_inputs�6complete_concrete_args_with_inputs_not_in_dummy_inputsc	s�t�t|tjj�r|jn|�}�duri��durI|rI|j��D]}|j	�vr(q |j212tjjur8t
d|j	�d���q ����fdd�|j��D��|j�����}t�}t�}	||	g}213|jjtt�vrptddd�}|214�d	|��durxt��ni}|D])}
|
|vr�q|t||j�s�t|�j�d215�r�|�|j||
|216|d��q|td|
�d
���dd�}t�||�}|j��D]}|jtjj kr�|j	|vr�i|d|j	��<q�||_!|a"|�#|��z
t$�j%|�d�|_&Wda"nda"wWd�n1s�wY|j&j'D]N}|j(dk�r@|j)|v�rd|_*tj+|_n/|g}t,�-�}|�r0|�.d�}d||<|t/|j0���7}|�st1|���D]	}|j&�2|��q6|j(dk�rId|_q�|j&S)a�217        Traces `root` and returns the corresponding FX `torch.fx.Graph` representation. `root` can either be a218        `torch.nn.Module` instance or a Python callable. Note that after this call, `self.root` may be different from219        the `root` passed in here. For example, when a free function is passed to `trace()`, we will create a220        `torch.nn.Module` instance to use as the root and add embedded constants to.221 222        Args:223            root (`torch.nn.Module` or  `Callable`):224                Either a `torch.nn.Module`` or a function to be traced through. If root is not a225                [`~transformers.PreTrainedModel`], then `dummy_inputs` must be passed, otherwise tracing will fail.226            concrete_args (`dict[str, Any], *optional*):227                Concrete arguments that should not be treated as Proxies228            dummy_inputs (`dict[str, Any]`, *optional*):229                The dummy inputs needed to handle data-dependent control-flow if `root` is not a230                [`~transformers.PreTrainedModel`]. It can also be used when `root` is a231                [`~transformers.PreTrainedModel`] to specify custom dummy inputs for a subset or all the model inputs.232            complete_concrete_args_with_inputs_not_in_dummy_inputs (`bool`, *optional*, defaults to `True`):233                If `True`, and `dummy_inputs` is specified, every argument that `root` can take that is not in234                `dummy_inputs` and not in `concrete_args` will be added to `concrete_args`, otherwise does nothing.235 236        Returns:237            `torch.fx.Graph`:238                A FX `torch.fx.Graph` representing the semantics of the passed-in `root`.239 240        Nz6You need to specify a default value for the parameter r|cs*i|]}|j�vr|j�vr|j|j�qSr>�r2r6�rr��r�r�r>r?r��s241�z"HFTracer.trace.<locals>.<dictcomp>rr^rkr.)Z_deserialize_graph_moduleZ_CodeOnlyModule�r\zCould not generate input named z8 for because root is not a transformers.PreTrainedModel.cSst|tj�r|�d�S|Sr�)r8rLr�rh)r�r>r>r?�to_metas242zHFTracer.trace.<locals>.to_meta�**�r�ryr>rr�)3r9r�r8rLr�Moduler�r�rr2r6r�rMrd�updater7rQrrrr"r�r{�supported_archsr"rr8rxr �pytreeZtree_mapr��VAR_KEYWORDr}rBr�r��trace�graph�nodes�opr�rtr�rr�OrderedDictr|rzZusers�reversedZ243erase_node)rQrr�r�r��sig�paramr\r�Zsequence_lengthrNZnum_choicesrjr[r�Zconcrete_metasrZto_visitZ	to_deleterv�userrr�r?r��s� 244����245���246247��zHFTracer.tracer�cCstdd�|j��D��S)z�248        Whether the module was instantiated with Proxies. If that is the case, such module cannot be a leaf module249        because its attributes are input-dependent.250        css�|]}t|t�VqdSrX)r8r)rrr>r>r?r�Js�zKHFTracer._stateless_mod_instantiation_depends_on_proxies.<locals>.<genexpr>)�anyr�r)rQr�r>r>r?�/_stateless_mod_instantiation_depends_on_proxiesEsz8HFTracer._stateless_mod_instantiation_depends_on_proxiescCs�|�|�rdSd}|jj��}|�d|��}d}t|j|�r:t|j|�|ur)d}n|�d|��}|d7}t|j|�s|sC|j�||�|S)zb251        Helper method which tries to insert a module that was not declared as submodule.252        r2rr�FTr.)r�rr�lowerr�rrZ253add_module)rQr��idx�mod_name�pathZalready_insertedr>r>r?�_insert_module_as_submoduleLs 254�z$HFTracer._insert_module_as_submodulec255stzt��|�WSty9}z&|jr3tt|����dkr3tt|����dkr3|�|�}|WYd}~S|�d}~ww)ag256        Helper method to find the qualified name of `mod` in the Module hierarchy of `root`. For example, if `root` has257        a submodule named `foo`, which has a submodule named `bar`, passing `bar` into this function will return the258        string "foo.bar".259 260        Args:261            mod (str): The `Module` to retrieve the qualified name for.262        rN)	r��path_of_module�	NameErrorrSrqrzr��buffersr�)rQr�r�r�rr>r?r�ds	.263��zHFTracer.path_of_moduler��module_qualified_namecs|�|�ot��||�SrX)r�r��is_leaf_module)rQr�r�rr>r?r�us�zHFTracer.is_leaf_module)Zis_backward_compatible�objrcCs$t|d��}|jj�d�r|jS|S)z�Called when a proxy object is has the keys() method called.264        This is what happens when ** is called on a proxy. This should return an iterator if ** is supposed to work in265        your custom tracer.266        r7r�)rrr�r8r�)rQr��	attributer>r>r?r7zsz
HFTracer.keys)NNN)NNT)4rrrrrR�bool�__annotations__rSr�rrGrrHrrIr!r0rr1r�r�rr9rzrsr{rLr�rxr�r�rrrzr��267contextlib�contextmanagerrrr�rr�rrr�r�r�r�r�r
r7rr>r>rr?rCXsd268�����269�1F&$'������rCrZr\cs|t�|j�}t��t|j���ks2t��dkr�dnd���}d�|j���}td|�d|�����fdd�|j�	�D�S)Nr.r�, z(The model does not have input(s) named: z&, expected a subset of the following: cs i|]}|j�vr|j|j�qSr>r�r�r�r>r?r��s z%get_concrete_args.<locals>.<dictcomp>)270r9r�r�r�r�r7rq�joinrdr)rZr\r�Zformatted_input_namesZformatted_allowed_input_namesr>r�r?�get_concrete_args�s��r�rcCs|jjtvSrX)rr�_SUPPORTED_MODELS)rZr>r>r?�is_model_supported�r�r�cCs.t|�sd�t�}td|jj�d|����dS)Nr�zModel z) is not supported yet, supported models: )r�r�r�rqrr)rZZsupported_model_namesr>r>r?�check_if_model_is_supported�s271��r��
disable_check�272tracer_clscCs�|dur	|j��}t|�}t||�}|st|�d|vr(t|jdd�s(t�d�d|vr<t|jdd�r<t�d�d|j_	|�}|j273||d�}tj�
||�}|j|_|j|_|j|_|S)a�274    Performs symbolic tracing on the model.275 276    Args:277        model ([`PretrainedModel`]):278            The model to trace.279        input_names (`list[str]`, *optional*):280            The names of the inputs of the traced model. If unset, model.dummy_inputs.keys() are used instead.281        disable_check (`bool`, *optional*, defaults to `False`):282            If `True`, no check is done before trying to trace the model, this is mostly usesul for debugging purposes.283        tracer_cls (`Type[HFTracer]`, *optional*, defaults to `HFTracer`):284            The tracer class to use for instantiating the tracer. If unset, `HFTracer` is used instead.285 286    Returns:287        `torch.fx.GraphModule`: A GraphModule constructed by recording operations seen while tracing the model.288 289    Example:290 291        ```python292        from transformers.utils.fx import symbolic_trace293 294        traced_model = symbolic_trace(model, input_names=["input_ids", "attention_mask", "token_type_ids"])295        ```296    Nrm�	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�r7rzr�r�rrp�logger�warningr�r�rLr*r	rr]rJ)rZr\r�r�r�r�Ztraced_graphZtracedr>r>r?�symbolic_trace�s,297298��r�rXr>)NNrTFF)F)NN)rH)Nr�FN)rJrKN)rZr)�rrrr�r.r9r�r��osrOr�r��typingrrrrrrLZtorch.utils._pytree�utilsZ_pytreer�rZtorch.fxrr	r299rrZtorch.fx._compatibilityr
Ztorch.fx._symbolic_tracerZtorch.fx.proxyrr2rZcache_utilsrrrZmodeling_utilsrrZmodels.autorZmodels.auto.modeling_autorrrrrrrr r!r"r#r$r%r&r'r(r)r*r+r,r-Zimport_utilsr/r0Zpeftr1Z300get_loggerrr��environr:�upperr�r"r9rzr@Z(_REGULAR_SUPPORTED_MODEL_NAMES_AND_TASKSruZ_REGULAR_SUPPORTED_MODELS�itemr8r{�extendZ_SPECIAL_SUPPORTED_MODELSr��sortedr�r�rBrSrVrYr[r]rarbrerkrnryr~r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�Z	EmbeddingZ301functionalZ	embeddingZ	LayerNormZ	GroupNormZLinearZreluZReLU�where�absrTr}�cat�stackr��mulr��matmulZbmmZbaddbmmr��repeatZrepeat_interleaveZroll�flipZindex_selectZgatherZConv1dZConv2dZsqueezeZ	unsqueezer�Zone_hotZMSELossZCrossEntropyLossZBCEWithLogitsLossr�r�r�Zscaled_dot_product_attentionr�rr�rr0r1r?rArFrGrHrIrsrQrCr�r�r�r�r�r�r>r>r>r?�<module>s�302\303���304�)>305�306$307308309310311��312�����313��	�314���
������������������� �!�"�#315�/�,����316�"  ��� 	2317318319��320����
Aluode/PerceptionLabPortable · CoolFace