o �J�hW}�@s�ddlmZddlZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl m Z ddlmZddlmZmZmZmZmZmZmZmZmZmZmZddlmZddlmZddl m!Z!dd l"m#Z#e�$e%�Z&erwddl'Z'ddl(Z( Gd d �d e�Z)Gd d �d e�Z*Gdd�dej+�Z, Gdd�d�Z-Gdd�de�Z.ej/Gdd�d��Z0ed�Z1 ej/Gdd�d��Z2 ej/Gdd�de2��Z3 Gdd�dee1�Z4Gdd�d�Z5Gdd �d �Z6Gd!d"�d"e4e6�Z7Gd#d$�d$�Z8Gd%d&�d&e4e8�Z9 Gd'd(�d(�Z:Gd)d*�d*e4e5�Z;e�<�Z= Gd+d,�d,�Z>Gd-d.�d.�Z?edBd1d2��Z@edCd4d5��ZAej/d6d7�Gd8d9�d9��ZBej/d6d7�Gd:d;�d;eB��ZCdDdEd>d?�ZDd@dA�ZEdS)F�)� annotationsN)�abstractmethod)�contextmanager) �Any�Callable�Dict�Generic�List� NamedTuple�Optional�Set�Tuple� TYPE_CHECKING�TypeVar)�set_context_frame)�_pytree)�CapturedTraceback)�WeakTensorKeyDictionaryc@�&eZdZUded<ded<dd�ZdS)� CompileId�int�frame_id�frame_compile_idcCs|j�d|j��S)N�/)rr��self�r�KC:\pinokio\api\whisper-webui.git\app\env\lib\site-packages\torch\_guards.py�__str__@szCompileId.__str__N��__name__� __module__� __qualname__�__annotations__rrrrrr7s  rc@s&eZdZUded<ded<dd�ZdS)�TraceIdr� compile_idr�attemptcCs&|jdkr t|j�S|j�d|j��S)Nr�_)r&�strr%rrrrrJs  zTraceId.__str__Nrrrrrr$Ds  r$c@s|eZdZdZdZdZdZdZdZdZ dZ d Z d Z d Z d Zd ZdZdZdZddd�Zddd�Zddd�Zddd�Zdd�ZdS)� GuardSourcer��������� � � � � ���return�boolcC�|tjtjfvS�N)r)�GLOBAL_FSDP_MODULE�LOCAL_FSDP_MODULErrrr�is_fsdp_modulec�zGuardSource.is_fsdp_modulecCs|tjtjfvp |��Sr<)r)�GLOBAL_SPECIALIZED_NN_MODULE�LOCAL_SPECIALIZED_NN_MODULEr?rrrr�is_specialized_nn_modulefs���z$GuardSource.is_specialized_nn_modulecCs|tjtjtjtjfvSr<)r)�GLOBAL_UNSPECIALIZED_NN_MODULE�LOCAL_UNSPECIALIZED_NN_MODULE�&GLOBAL_UNSPECIALIZED_BUILTIN_NN_MODULE�%LOCAL_UNSPECIALIZED_BUILTIN_NN_MODULErrrr�is_unspecialized_nn_moduleqs �z&GuardSource.is_unspecialized_nn_modulecCr;r<)r)rFrGrrrr�"is_unspecialized_builtin_nn_moduleys�z.GuardSource.is_unspecialized_builtin_nn_modulecCs|tjtjtjtjtjfvSr<)r)�LOCALrBr>rErGrrrr�is_locals�zGuardSource.is_localN�r9r:)r r!r"rJ�GLOBALrBrA�CONSTANTZ RANDOM_VALUEZ SHAPE_ENVr>r=ZBACKWARD_STATEZ EPHEMERAL�SYNTHETIC_LOCALrErDrGrFr?rCrHrIrKrrrrr)Qs,    r)c@� eZdZdS)�GuardBuilderBaseN�r r!r"rrrrrQ�srQc@seZdZUded<ded<dS)� ShapeGuardz sympy.Expr�exprr�stackN)r r!r"r#rrrrrS�s  rSc@s�eZdZUded<ded<dZded<dZded<dZd ed <dZd ed <dZd ed<dZ ded<dZ ded<dd�Z dd�Z dd�Z dd�Zed4dd��Zed5d d!��Zed"d#��Zd$d%�Zd&d'�Zd6d*d+�Zd,d-�Zd.d/�Zd0d1�Zd2d3�ZdS)7�Guard�Source�originating_sourcez)Callable[[GuardBuilderBase, Guard], None]� create_fnNzOptional[List[str]]� guard_types� code_listzOptional[object]� obj_weakrefzOptional[type]�guarded_class_weakrefzOptional[CapturedTraceback]rUz Optional[traceback.StackSummary]� user_stackz Optional[int]�_hashcCs*|jdurt|j|jt|j�f�|_|jSr<)r_�hash�name�source�idrYrrrr�__hash__�s zGuard.__hash__cCsTddlm}t|jtj�o|jj|ju}||jr|jj ndt |j �|j |� �j jfS)Nr)� GuardBuilder�����)Ztorch._dynamo.guardsre� isinstancerY� functools�partial�funcZDUPLICATE_INPUTrb�value�lenra�inner_create_fn�__code__�co_firstlineno)rreZis_duplicate_inputrrr�sort_key�s  � �zGuard.sort_keycCs|��|��kSr<)rp�r�otherrrr�__lt__�r@z Guard.__lt__cCst|jtj�r |jjS|jSr<)rgrYrhrirjrrrrrm�szGuard.inner_create_fnr9r(cC� |j��Sr<)rXrarrrrra�� z Guard.namer)cCrtr<)rX� guard_sourcerrrrrb�ruz Guard.sourcecCsbt|tj�r-|�}|dur#dtt|���d|jj�dtt|���d�Sdtt|���d�St|�S)a� This is a workaround of a Python weakref bug. `obj_weakref` is instance returned by `weakref.ref`, `str(obj_weakref)` is buggy if the original obj overrides __getattr__, e.g: class MyConfig(dict): def __getattr__(self, x): return self[x] obj = MyConfig(offset=5) obj_weakref = weakref.ref(obj) str(obj_weakref) # raise error: KeyError: '__name__' Nz <weakref at z; to 'z' at �>z; dead>)rg�weakref� ReferenceType�hexrc� __class__r r()r\�objrrr�weakref_to_str�s ,zGuard.weakref_to_strcCs`d|jr |jj��nd�dt|j��d|��j�d|j�d|j�d|�|j ��d|j �d�}|S) Nz �� z& { 'guard_types': z, 'code': z, 'obj_weakref': z 'guarded_class': z } ) rbra�lower�reprrmr rZr[r}r\r])r�srrr�__repr__s ����� �� zGuard.__repr__cCs�dt|j��d�}|jr|jj��nd}|d|�d�7}|d|��j�d�7}|d|j�d�7}|d|j�d�7}|d|�|j ��d�7}|d |j �d�7}|S) NzName: � r~z Source: z Create Function: z Guard Types: z Code List: z Object Weakref: z Guarded Class Weakref: ) r�rarbr�rmr rZr[r}r\r])r�outputrbrrrr sz Guard.__str__�builderrQc Cs^z|�||�WSty.t�dt|����|jr-t�dd�|j� �dd������w)NzError while creating guard: %szCreated at: %sr~�����) rY� Exception�log� exceptionr(�rstriprU�error�join�format)rr�rrr�creates $�z Guard.createcCrtr<)rbrCrrrrrC � zGuard.is_specialized_nn_modulecCrtr<)rbr?rrrrr?#r�zGuard.is_fsdp_modulecCrtr<)rbrKrrrrrK&r�zGuard.is_localcCs�|jsg|_|j�|�|j|dfvsJd��||_|js!||_n|j�|�|j|dfvs=t|j�r9|��dus=Jd��||_dS)Nz+Guarded class id must be identical, or NonezBGuarded object must be identical, None or ephemeral (dead weakref))rZ�appendr]r[�extendr\�callable)rZ guard_typeZ guarded_classr[r\rrr�set_export_info)s( �� �� zGuard.set_export_info�r9r(�r9r))r�rQ)r r!r"r#rZr[r\r]rUr^r_rdrprsrm�propertyrarb� staticmethodr}r�rr�rCr?rKr�rrrrrV�s6              rV�Tc@rP)� GuardEnvExprNrRrrrrr�Psr�c@r)�DuplicateInputsrW�input_source_a�input_source_bcCs|j|jksJ�dSr<)r�r�rrrr� __post_init__`szDuplicateInputs.__post_init__N)r r!r"r#r�rrrrr�[s  r�c@s(eZdZed dd��Zed dd��ZdS) �Checkpointabler9r�cC�dSr<rrrrr�copy_graphstater�zCheckpointable.copy_graphstate�statecCr�r<r�rr�rrr�restore_graphstateur�z!Checkpointable.restore_graphstateN)r9r�)r�r�)r r!r"rr�r�rrrrr�qs  r�c@s8eZdZUdZe�Zded<dd�Zdd�Zdd �Z d S) �GuardsCheckpointStatezW The GuardCheckpointState - it is the T of Checkpointable[T] for GuardsContext � Set[Guard]� dynamo_guardscC� ||_dSr<)r�)rr�rrr�__init__�r�zGuardsCheckpointState.__init__cCs"|j�|j�}t|�dkrdS|S)z� Produces a delta against another GuardsCheckpointState. Returns None if no delta is found, otherwise, return a set() of mismatched Guard type objects. rN)r�� differencerl�rrr�rrrr�diff�s zGuardsCheckpointState.diffcC�|�|�duSr<�r�rqrrr�__eq__��zGuardsCheckpointState.__eq__N) r r!r"�__doc__�setr�r#r�r�r�rrrrr�ys  r�c@�2eZdZUiZded<dd�Zdd�Zdd�Zd S) �ModuleContextCheckpointStatezDict[str, torch.nn.Module]� nn_modulescCr�r<�r�)rr�rrrr��r�z%ModuleContextCheckpointState.__init__cC�2t|j����t|j����}t|�dkrdS|S)z� Produces a delta against another ModuleContextCheckpointState. Returns None if no delta is found, otherwise, return a set() of mismatched module key names. rN)r�r��keysr�rlr�rrrr��� z!ModuleContextCheckpointState.diffcCr�r<r�rqrrrr��r�z#ModuleContextCheckpointState.__eq__N)r r!r"r�r#r�r�r�rrrrr���  r�c@�&eZdZd dd�Zdd�Zdd�Zd S) � ModuleContextr9�NonecC� i|_dSr<r�rrrrr��r�zModuleContext.__init__cC�tt|j��Sr<)r��dictr�rrrrr��r�zModuleContext.copy_graphstatecCst|t�sJ�|j|_dSr<)rgr�r�r�rrrr��s z ModuleContext.restore_graphstateN�r9r��r r!r"r�r�r�rrrrr��s  r�c@r�) �GlobalContextCheckpointStatezDict[str, Tuple[Callable, ...]]� global_statecCr�r<�r�)rZ global_statesrrrr��r�z%GlobalContextCheckpointState.__init__cCr�)z� Produces a delta against another GlobalContextCheckpointState. Returns None if no delta is found, otherwise, return a set() of mismatched global key names. rN)r�r�r�r�rlr�rrrr��r�z!GlobalContextCheckpointState.diffcCr�r<r�rqrrrr��r�z#GlobalContextCheckpointState.__eq__N)r r!r"r�r#r�r�r�rrrrr��r�r�c@s2eZdZdZhd�Zd dd�Zdd�Zd d �Zd S) � GlobalContextzz This keeps track of the global torch state during tracing of a function. For example, torch.is_grad_enabled. >Zautocast_gpu_dtype� grad_enabledZautocast_cpu_dtypeZautocast_cache_enabled�autocast_enabledZtorch_function_enabledZautocast_cpu_enabledr9r�cCr�r<r�rrrrr��r�zGlobalContext.__init__cCr�r<)r�r�r�rrrrr��r�zGlobalContext.copy_graphstatecCsft|t�sJ�|j|_t|j�t|j�krt|j���|jks#Jd��|j��D]\}}||�q(dS)NzGlobal state mismatch)rgr�r�rl�_supported_global_statesr�r��values)rr�rj�argsrrrr��s� �z GlobalContext.restore_graphstateNr�)r r!r"r�r�r�r�r�rrrrr��s   r�c@sZeZdZddd�Zdd�Zdd�Zdd �Zd d �Zd d d�ddd�Zddd�Z dd�Z dS)� GuardsSetNcCs|durt�}||_dSr<)r��inner)rr�rrrr��s zGuardsSet.__init__cC� t|j�Sr<)�iterr�rrrr�__iter__�r�zGuardsSet.__iter__cCr�r<)rlr�rrrr�__len__�r�zGuardsSet.__len__cCst|j|j�Sr<)r�r�rqrrr�__sub__r@zGuardsSet.__sub__cCr�r<)r:r�rrrr�__bool__r�zGuardsSet.__bool__Tr)�collect_debug_stack�skip�guardrVcCsR||jvrdS|r!|jdurtjd|d�|_|jdur!t��|_|j�|�dS�Nr*)r�)r�rUr�extractr^�TracingContext� extract_stack�add)rr�r�r�rrrr� s    z GuardsSet.add�othersr�cGs&|D]}|D] }|j|dd�qqdSr�)r�)rr��o�grrr�updates ��zGuardsSet.updatecs�fdd�|jD�|_dS)z%Delete all guards with a given sourcecsh|] }|j�kr|�qSr)rX)�.0r��rbrr� <setcomp>sz6GuardsSet.remove_guards_with_source.<locals>.<setcomp>N)r�)rrbrr�r�remove_guards_with_sourcesz#GuardsSet.remove_guards_with_sourcer<)r�rV)r�r�) r r!r"r�r�r�r�r�r�r�r�rrrrr��s  r�c@r�) � GuardsContextr9r�cCst�|_g|_dSr<)r�r�Zaotautograd_guardsrrrrr�s zGuardsContext.__init__cCstt|jj��Sr<)r�r�r�r�rrrrr�#r@zGuardsContext.copy_graphstatecCst|t�sJ�t|j�|_dSr<)rgr�r�r�r�rrrr�&sz GuardsContext.restore_graphstateNr�r�rrrrr�s  r�c@sHeZdZeddd��Zeddd��Zdd�Zed d ��Zed d ��Zd S)�CompileContextr9cCstjdusJ�tjSr<)�_TLS�compile_contextrrrr�getBszCompileContext.get�Optional[CompileContext]cC� ttdd�S�Nr���getattrr�rrrr�try_getG� zCompileContext.try_getcCs&|dus t|t�s J�||_d|_dS)Nr)rgrr%r&)rr%rrrr�Ks zCompileContext.__init__cCst��}|dur dS|jSr<)r�r�r%rrrr�current_compile_idPsz!CompileContext.current_compile_idcCs0t��}|dur dS|jdurdSt|j|j�Sr<)r�r�r%r$r&rrrr�current_trace_idWs  zCompileContext.current_trace_idN)r9r�)r9r�) r r!r"r�r�r�r�r�r�rrrrr�As   r�c@s�eZdZdZeddd��Zeddd��Zdd �Zd d �Zee d d ���Z edd��Z ee j dd���Z ee j dd���Zee j dd���Zedd��ZdS)r�z� Provides the currently installed TracingContext, or None. Note that it is a staticmethod, and invocations outside of `with tracing()` (see below), are valid but will return None. r9�Optional[TracingContext]cCr�)N�tracing_contextr�rrrrr�ir�zTracingContext.try_getcCst��}r|Std��)Nz<TracingContext.get() must be called within an ongoing trace.)r�r�� RuntimeError)�ctxrrrr�ms �zTracingContext.getcCsZt�|_t�|_t�|_||_g|_d|_d|_ d|_ d|_ d|_ d|_ t�|_d|_dS�NF)r�Zguards_contextr�Zmodule_contextr��global_context� fake_mode�frame_summary_stack� loc_in_frameZ fw_metadataZaot_graph_nameZ params_flat�output_stridesZ#force_unspec_int_unbacked_size_likerZtensor_to_contextZfakify_first_call)rr�rrrr�us  zTracingContext.__init__cCs i|j_dSr<)r�r�rrrr�clear�s zTracingContext.clearc ks��i}t��}|��D] }t||�||<q |��D] \}}t|||�qzdVW|��D] \}}t|||�q-dS|��D] \}}t|||�q>wr<)r�r�r�r��items�setattr)�kwargs�priorr��key�valrrr�patch�s� ��zTracingContext.patchcCs@t��}|dur t��S|j}|jdur||jg}tj�|�Sr<)r�r�� traceback� StackSummaryr�r�� from_list)rrUrrrr��s   zTracingContext.extract_stackc cs��t��}tjj�|dg��Etjj�|dd��#zdVWnty3}z t|d�s.d|_�d}~wwWd�n1s>wYWd�dSWd�dS1sVwYdS)Nr�r�� real_stack) r�r��unittest�mockr�objectr��hasattrr )�tc�errr� clear_frame�s"���  ���P�zTracingContext.clear_framec cs��t��}|dur|j�|�|j}d|_z,zdVWnty2}z t|d�s-|��|_�d}~wwW|dur=|j� �||_dS|durK|j� �||_w)Nr ) r�r�r�r�r�r�r r�r �pop)� frame_summaryr�oldrrrr� current_frame�s*�    ���  � zTracingContext.current_frameccsF�t��}|durdVdS|j}g|_z |jVW||_dS||_wr<)r�r�r�)rZold_output_stridesrrr�report_output_strides�s� z$TracingContext.report_output_stridescCstj|||dd�t��_dS)NF)� lookup_line)r� FrameSummaryr�r�r�)�filename�lineno� frame_namerrr�set_current_locs�zTracingContext.set_current_locN)r9r�)r9r�)r r!r"r�r�r�r�r�r�rrr�� contextlibrrrrrrrrr�as0  *       r��contextr�c cs��ttdd�}|t_z|VW|dur$|jdur$t|jj|jj|jf�|t_dS|dur>|jdur>t|jj|jj|jf�|t_wr�)r�r�r�r%rrrr&)r� old_contextrrrr� s.�  �� � ��r�r�c cs��ttdd�}|t_z<z|VWnty+}zt|d�s&|dur&|��|_�d}~wwW|durB|jdurB|jjdurB|jj� �|t_dS|dur\|jdur\|jjdur\|jj� �|t_w)z� This function installs the passed in tracing context as a dynamic scoped global variable. Calls to TracingContext.get() while not under a `with tracing()` context will return None. r�Nr ) r�r�r�r�r r�r r�� shape_env�cleanup)rrrrrr�tracings,�   ���    �   r!T)�frozenc@sTeZdZdd�Zdd�Zdd�Zdd d �Zdd d �Zddd�Zddd�Z dd�Z dS)rWcC�dSr�rrrrr� is_dict_key<r�zSource.is_dict_keycCr#r�rrrrr� is_ephemeral?r�zSource.is_ephemeralcC�t�r<��NotImplementedError)r�codegenrrr� reconstructBr�zSource.reconstructr9r)cCr&r<r'rrrrrvEr�zSource.guard_sourcer(cCr&r<r'rrrrraHr�z Source.namerVcCs|��tjur t�t||�Sr<)rvr)rNr(rV)r�fnrrr� make_guardKs zSource.make_guardr:cCs |����Sr<)rvrCrrrrrCPs zSource.is_specialized_nn_modulecCs|��tjkS)z+True if you can guard on attributes of this)rvr)rOrrrr�subguards_allowedSszSource.subguards_allowedNr�r�)r9rVrL) r r!r"r$r%r*rvrar,rCr-rrrrrW:s     rWc@s&eZdZUded<dd�Zdd�ZdS)� ChainedSourcerW�basecCrtr<)r/r$rrrrr$]ruzChainedSource.is_dict_keycCrtr<)r/r%rrrrr%ar�zChainedSource.is_ephemeralN)r r!r"r#r$r%rrrrr.Ys  r.�inputsrcCsBddlm}m}g}t��}r|j}|dur|�|ddf�ddlm}t t |���D]\}}t ||�r=|�|d|f�q,t � |�} t | �D]\}} t | |�rY|�| jd|f�qG|r�|d\}} } |dd�D]3\}} }||us�Jd |�d | �d | �d |�d | �d |�d | �d | �d|j�d| �d |�d|j����qi|SdS)a� Attempts to "detect" what the current fake mode is. If there is one ambiently available from TracingContext, we preferentially use that. Otherwise, we heuristically detect the fake mode via the following sources, in order of priority: - Currently active fake mode on stack - Fake mode associated with passed in tensors (inputs does not have to be flattened) r)� FakeTensor�FakeTensorModeNztracing context�� _get_current_dispatch_mode_stackzactive fake modezfake tensor inputr*z fake mode (z) from rz doesn't match mode (z fake mode from z allocated at: z fake mode from )�torch._subclasses.fake_tensorr1r2r�r�r�r��torch.utils._python_dispatchr4� enumerate�reversedrg�pytree� tree_leavesrU)r0r1r2Z fake_modesrr�r4�i�mZ flat_inputs� flat_inputZdesc1�i1Zdesc2�i2rrr�detect_fake_modeesH    �  � &�������r@cCsFddlm}ddlm}tt|���D] \}}t||�r |SqdS)z~ Inspects the dispatch mode stack for an active fake mode and returns it. Returns None if no fake mode is active. r)r2r3N)r5r2r6r4r7r8rg)r2r4r'r<rrr�active_fake_mode�s   �rA)rr�)rr�r<)r0r)F� __future__rr� dataclasses�enumrh�logging� threadingr� unittest.mockr rx�abcrr�typingrrrrr r r r r rrZtorch._C._dynamo.eval_framerZ torch.utilsrr9�torch.utils._tracebackrZtorch.utils.weakr� getLoggerr r��sympy�torchrr$�Enumr)rQrS� dataclassrVr�r�r�r�r�r�r�r�r�r�r��localr�r�r�r�r!rWr.r@rArrrr�<module>s~   4       8&  ! ( *     ,
Memory