
    VhS                    ,   d dl m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 d dlmZ d dlmZmZmZmZ d dlmZmZmZ d dlZd dlZd dlmZmZ d dlmZ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) ddl*m+Z+m,Z,m-Z-m.Z. ddl/m0Z0 ddl1m2Z2 ddl3m4Z4 ddl5m6Z6 erd dl7Z7d dl8m9Z9m:Z:m;Z; d dl<m=Z= d dlm>Z> e G d d             Z? G d dej                        ZAd6dZB G d d      ZC G d dej                        ZDe G d d              ZEe G d! d"             ZFd7d#ZGe G d$ d%             ZHe G d& d'             ZIe G d( d)             ZJ G d* d+      ZK G d, d-      ZL G d. d/e-      ZM G d0 d1eM      ZN G d2 d3eM      ZO G d4 d5eO      ZPy)8    )annotationsN)abstractmethod)	dataclass)AnyOptionalTYPE_CHECKINGUnion)overrideSelf	TypeGuard)BypassFxGraphCacheFxGraphCache)CachedMetricsDeltasCachedMetricsHelper)CompiledFxGraphCompiledFxGraphConstantsCompiledFxGraphConstantsWithGm
OutputCode)FakeTensorMode)
OrderedSet   )config)_CompileFxKwargs_InProcessFxCompile	FxCompilelog)DebugContext)GraphLowering)complex_memory_overlap)V)	GeneratorMappingSequence)	InputType)GraphModulec                      e Zd ZU dZdZded<   dZded<   dZded<   dZded<   dZ	ded<   dZ
ded	<   edd
       ZddZy)_VirtualizedSerializerz<
    This handles the data for serializing Virtualized.
    Nr   aot_compilationchoiceslocal_buffer_contextopskernelcurrent_nodec                    i }t        j                  |       D ])  }t        t        |j                        ||j                  <   + t        di |S )zp
        Turn the current state of torch._inductor.virtualized.V into a
        serializable structure.
         )dataclassesfieldsgetattrr    namer'   )clskwargsfs      N/home/dcms/DCMS/lib/python3.12/site-packages/torch/_inductor/compile_fx_ext.py	serializez _VirtualizedSerializer.serialize<   sI     ##C( 	0A$Q/F166N	0%///    c                    t        |       S )z
        Returns a context manager which patches the saved values into the
        current environment. While patched, any value not listed above will be
        poisoned so that reads will raise an error.
        )$_VirtualizedSerializerContextManagerselfs    r7   patchz_VirtualizedSerializer.patchG   s     4D99r9   )returnr'   )r?   r;   )__name__
__module____qualname____doc__r(   __annotations__r)   r*   r+   r,   r-   classmethodr8   r>   r/   r9   r7   r'   r'   -   s\      OSGS $#$COFCL#0 0:r9   r'   c                  :     e Zd ZdZd fdZed fd       Z xZS )r;   z3
    Helper for _VirtualizedSerializer.patch()
    c                0    t         |           || _        y N)super__init__virtualized)r=   rK   	__class__s     r7   rJ   z-_VirtualizedSerializerContextManager.__init__U   s    &r9   c                   t         |           t        t              D ]  }|j	                  d      s|dd  }|j                  d      }t        t        |      }t        | j                  |      rt        | j                  |      }n$t        j                  j                  j                  }| j                   ||              | S )Nset_   _handler)rI   	__enter__dirr    
startswithremovesuffixr2   hasattrrK   torch	_inductor_PoisonedVirtualenter_context)r=   set_namer3   set_handlervaluerL   s        r7   rQ   z._VirtualizedSerializerContextManager.__enter__Y   s    A 	3H&&v.AB<D$$Z0D!!X.Kt''. 0 0$7 33DD{512	3 r9   )rK   r'   r?   Noner?   r   r@   rA   rB   rC   rJ   r
   rQ   __classcell__rL   s   @r7   r;   r;   P   s!    '  r9   r;   c                :    	 | j                   S # t        $ r Y yw xY w)NF)_is_fallback_handlerAttributeError)ops    r7   rc   rc   n   s%    &&& s    	c                  ,    e Zd ZU dZded<   ddZddZy)	_LoweringSerializerzA
    This handles the data for serializing lowering.lowering
    zOrderedSet[str]	fallbacksc                n    ddl m} t        d |j                  j	                         D              | _        y )Nr   loweringc              3  P   K   | ]  \  }}t        |      st        |         y wrH   )rc   str).0kvs      r7   	<genexpr>z/_LoweringSerializer.__init__.<locals>.<genexpr>   s&      $
q!=QRS=TCF$
s   &&) rk   r   	loweringsitemsrh   )r=   rk   s     r7   rJ   z_LoweringSerializer.__init__   s-    # $
'11779$
 
r9   c                    t        |       S rH   )!_LoweringSerializerContextManagerr<   s    r7   r>   z_LoweringSerializer.patch   s    066r9   Nr?   r]   )r?   rv   )r@   rA   rB   rC   rD   rJ   r>   r/   r9   r7   rg   rg   u   s     
7r9   rg   c                  :     e Zd ZdZd fdZed fd       Z xZS )rv   z0
    Helper for _LoweringSerializer.patch()
    c                0    t         |           || _        y rH   )rI   rJ   rk   )r=   rk   rL   s     r7   rJ   z*_LoweringSerializerContextManager.__init__   s     r9   c                   t         |           ddlm} |j                  j                         D ]U  \  }}t        |      }|| j                  j                  v s*t        |      r6| j                  |j                  |             W | S )Nr   rj   )rI   rQ   rr   rk   rs   rt   rm   rh   rc   rY   force_fallback)r=   rk   ro   rp   r3   rL   s        r7   rQ   z+_LoweringSerializerContextManager.__enter__   sx    &&,,. 	CDAqq6Dt}}...+A.&&x'>'>q'AB		C r9   )rk   rg   r?   r]   r^   r_   ra   s   @r7   rv   rv      s!    !  r9   rv   c                  F    e Zd ZU ded<   ddZej                  dd       Zy)_FakeTensorModeSerializerboolallow_non_fake_inputsc                &    |j                   | _         y rH   r   )r=   	fake_modes     r7   rJ   z"_FakeTensorModeSerializer.__init__   s    %.%D%D"r9   c              #  \   K   |j                   }| j                   |_         d  ||_         y wrH   r   )r=   r   saved_allow_non_fake_inputss      r7   r>   z_FakeTensorModeSerializer.patch   s+     &/&E&E#*.*D*D	'*E	's   *,N)r   r   r?   r]   )r   r   r?   zGenerator[None, None, None])r@   rA   rB   rD   rJ   
contextlibcontextmanagerr>   r/   r9   r7   r}   r}      s*    E F Fr9   r}   c                      e Zd ZU dZded<   ded<   ded<   ded	<   d
ed<   ded<   ded<   ded<   ded<   ded<   ded<   ddZy)_WireProtocolInputzy
    For _SerializedFxCompile - encapsulates all the data being transferred
    (sent) from the parent to the child.
    ztorch.fx.GraphModulegmSequence[InputType]example_inputsSequence[int]inputs_to_checkr   graph_kwargsz&Optional[torch._guards.TracingContext]tracing_contextzdict[str, object]r   r'   rK   zAOptional[torch.testing._internal.common_utils.DeterministicGuard]deterministic_guard_for_testing_LoggerStatelogger_staterg   rk   r}   fake_tensor_modec                B    ddl m} t        |j                  |             S )z
        Turns this object into a _WireProtocolPickledInput which can be
        directly transferred across a stream.
        r   GraphPickler)torch.fx._graph_picklerr   _WireProtocolPickledInputdumpsr=   r   s     r7   r8   z_WireProtocolInput.serialize   s    
 	9(););D)ABBr9   N)r?   r   r@   rA   rB   rC   rD   r8   r/   r9   r7   r   r      sW    
 	''"""";;''&  !!//Cr9   r   c                     d } t         j                  j                  j                         x}r|j                  } | | S t         j
                  j                  j                  j                         }t        |      S )N)	shape_env)
rV   _guardsTracingContexttry_getr   fxexperimentalsymbolic_shapesShapeEnvr   )r   contextr   s      r7   _current_fake_moder      se    I--..6688w8%%	%%55>>@II..r9   c                       e Zd ZU ded<   ddZy)r   bytesr\   c                    ddl m} t               }|j                  | j                  |      }t        |t              sJ |S )zM
        Turn this streamable object back into a _WireProtocolInput.
        r   r   )r   r   r   loadsr\   
isinstancer   )r=   r   r   results       r7   deserializez%_WireProtocolPickledInput.deserialize   s;     	9&(	##DJJ	:&"4555r9   N)r?   r   r@   rA   rB   rD   r   r/   r9   r7   r   r      s    L	r9   r   c                  B    e Zd ZU dZded<   ded<   ded<   ded	<   dd
Zy)_WireProtocolOutputz
    For _SerializedFxCompile - encapsulates all the data being transferred
    (returned) back from the child to the parent.
    r   graphr   metricslist[logging.LogRecord]logsz'Optional[list[warnings.WarningMessage]]warning_replayc                    ddl m} t        | j                  t              r| j                  j                          t        |j                  |             S )z
        Turns this object into a _WireProtocolPickledOutput which can be
        directly transferred across a stream.
        r   r   )r   r   r   r   r   prepare_for_serialization_WireProtocolPickledOutputr   r   s     r7   r8   z_WireProtocolOutput.serialize   s;    
 	9djj/2JJ002),*<*<T*BCCr9   N)r?   r   r   r/   r9   r7   r   r      s'    
   
!!;;	Dr9   r   c                       e Zd ZU ded<   ddZy)r   r   r\   c                    ddl m} t               }|j                  | j                  |      }t        |t              sJ t        |j                  t              r|j                  j                  |       |S )zN
        Turn this streamable object back into a _WireProtocolOutput.
        r   r   )
r   r   r   r   r\   r   r   r   r   after_deserialization)r=   	constantsr   r   r   s        r7   r   z&_WireProtocolPickledOutput.deserialize
  s[     	9&(	##DJJ	:&"5666fllO4LL..y9r9   N)r   r   r?   r   r   r/   r9   r7   r   r     s    Lr9   r   c                  R    e Zd ZU dZded<   dZded<   d
dZddZ	 	 	 	 	 	 	 	 dd	Zy)r   z
    This class is for tracking logging that happens during an out-of-process
    compile so we can "replay" those messages when the compile is done. Used as
    a context manager which returns the captured logs (object).
    zdict[str, int]loggersNzOptional[_CapturedLogs]captured_logsc                j   i | _         	 	 	 	 dd}t        j                  d      }t        j                  dk  rt        j
                          	 |j                  j                  j                         D ].  } ||      s|j                  | j                   |j                  <   0 	 t        j                          y |g}|r^|j                         } ||      r#|j                  | j                   |j                  <   |j                  |j                                |r]y y # t        j                          w xY w)Nc                    t        | t        j                        sy| j                  j	                  d      sy| j
                  ryy)NFtorch._inductorT)r   loggingLoggerr3   rS   	propagate)loggers    r7   filterz%_LoggerState.__init__.<locals>.filter(  s:     fgnn5;;))*;<r9   r   )      )r   z*Union[logging.Logger, logging.PlaceHolder]r?   zTypeGuard[logging.Logger])r   r   	getLoggersysversion_info_acquireLockmanager
loggerDictvalueslevelr3   _releaseLockpopextendgetChildren)r=   r   rootr   qs        r7   rJ   z_LoggerState.__init__$  s    	>	&	   !23g%  "'"ll55<<> AFf~4:LLV[[1A $$&A&>06DLL-++-.	  $$&s   /D =%D D2c                    | j                   J t        |       | _         | j                   j                          | j                   S rH   )r   _CapturedLogsapplyr<   s    r7   rQ   z_LoggerState.__enter__H  s?    !!)))*40  "!!!r9   c                T    | j                   J | j                   j                          y rH   )r   remove)r=   exc_type	exc_value	tracebacks       r7   __exit__z_LoggerState.__exit__N  s(     !!---!!#r9   rw   )r?   r   )r   zOptional[type[BaseException]]r   zOptional[BaseException]r   zOptional[types.TracebackType]r?   r]   )	r@   rA   rB   rC   rD   r   rJ   rQ   r   r/   r9   r7   r   r     sR      .2M*1"/H"$/$ +$ 1	$
 
$r9   r   c                  P    e Zd ZU dZded<   ded<   ded<   ddZdd	Zdd
ZddZy)r   z
    Helper for _LoggerState - this class actually attaches to the logger in
    the child process and grabs the log messages themselves.
    r   statezqueue.Queue[logging.LogRecord]queuez$Optional[dict[str, logging.Handler]]handlersc                T    || _         t        j                  d      | _        d | _        y )N)r   r   Queuer   )r=   r   s     r7   rJ   z_CapturedLogs.__init__b  s!    
 [[_
r9   c                    | j                   J g }	 	 |j                  | j                  j                                *# t        j                  $ r Y |S w xY wrH   )r   appendr   
get_nowaitEmpty)r=   r   s     r7   finishz_CapturedLogs.finishj  sX    }}$$$	DJJ1134 {{ 		s   += AAc                    | j                   J | j                   d c}| _         |j                         D ]+  \  }}t        j                  |      }|j	                  |       - y rH   )r   rt   r   r   removeHandler)r=   r   r3   handlerr   s        r7   r   z_CapturedLogs.removet  s\    }}((("&--$-%^^- 	*MD'&&t,F  )	*r9   c                j   ddl m} | j                  J i | _        | j                  j                  j                         D ]q  \  }}t        j                  |      } || j                        }|| j                  |<   |j                  |       |t        j                  k7  sa|j                  |       s y )Nr   )QueueHandler)logging.handlersr   r   r   r   rt   r   r   r   
addHandlerNOTSETsetLevel)r=   r   r3   r   r   r   s         r7   r   z_CapturedLogs.apply{  s    1}}$$$::--335 	'KD%&&t,F"4::.G")DMM$g&&&	'r9   N)r   r   r?   r]   )r?   r   rw   )	r@   rA   rB   rC   rD   rJ   r   r   r   r/   r9   r7   r   r   X  s/    
 ))22*'r9   r   c                  x    e Zd ZdZe	 	 	 	 	 	 	 	 	 	 dd       Ze	 	 	 	 dd       Zd	dZe		 d
	 	 	 	 	 dd       Z
y)_SerializedFxCompilez_
    This is used to represent an FxCompile which occurs across a serialized
    boundary.
    c                   dfd}	 t        j                         t        j                  j                  j                         }t              }t               }	t               }
d }	 t        j                  j                  j                  j                   j#                         }t'               }t)        |      }	 t+        |t-        j.                         t0        j3                         ||	|
|      j3                         }| j5                  |      j7                  |      }| j9                  |       | j:                  t        |          xj<                  dz  c_        |j>                  S # t        $ r1}t        j                  dt        |       |        |       cY d }~S d }~ww xY w# t$        $ r Y w xY w# t$        t        f$ r! t        j                  dd        |       cY S w xY w)Nc                 <    t               j                         S rH   )r   codegen_and_compile)r   r   r   r   s   r7   fallbackz:_SerializedFxCompile.codegen_and_compile.<locals>.fallback  s"    &(<<NO\ r9   zSkipping %s compile: %sz.Unable to pickle input graph or example inputsT)exc_infor   )r?   r   ) r   _check_for_hopr   r   debugtyperV   r   r   r   r   r   rg   testing	_internalcommon_utilsDeterministicGuard_current_staterd   r   r}   r   r   save_config_portabler'   r8   _send_to_childr   _postprocess_compile_statsr   r   )r=   r   r   r   r   r   er   r   r   rk   r   r   r   inputoutputs    ````           r7   r   z(_SerializedFxCompile.codegen_and_compile  s   	 	
	 ''+
 --..668226	#~&(  	(	''44GGVVX , '(	4Y?	&++-&002/  ik , $$U+77	B&!DJ';;q@;
 ||o " 	II/dQ?:	$  		(  23 	
 IIFQUV:	sB   E/ .<F,  AF< /	F)8&F$F)$F),	F98F9<-G,+G,c                     y rH   r/   r=   pickled_inputs     r7   r  z#_SerializedFxCompile._send_to_child  s     	r9   c                     y rH   r/   )r=   r  s     r7   r  z!_SerializedFxCompile._postprocess  s    r9   Nc                   t               }t        j                         5 }|9dd l}|j	                  |j
                  j                  j                  d|             |j	                  t        j                  d            }|j                         }|j	                  |j                  j                                |j	                  |j                  j                                |j	                  t        j                  |j                               |j	                  |j                        }|j                  r|j	                  |j                         |j	                  t         j"                  j%                  |j&                               |j	                  t)                      t+               }	|j	                  |j,                  j                  |	             t/               j1                  |j2                  |j4                  |j6                  |j8                        }
d d d        j;                         }t=        
|j?                         |      jA                         S # 1 sw Y   CxY w)Nr   z
os.environT)record)!r   r   	ExitStackunittestrY   mockr>   dictwarningscatch_warningsr   rK   rk   r   r   r   rV   r   tracingr   r   r   r   r   r   r   r   r   r   r   r   
get_deltasr8   )r4   r  	extra_envr   stackr  r   r
  r   r   output_graphr   s               r7   _run_in_childz"_SerializedFxCompile._run_in_child  s    &'!!#  	u$##HMM$7$7$<$<\9$UV #001H1HPT1UVN "--/E 1 1 7 7 9: 4 4 67U\\ :;!//0B0BCM44##E$I$IJ 5 5e6K6K LM/*,I 6 6 < <Y GH.0DD$$%%""	L7 	D ##%"',,.n

)+	I 	  	s   G<II&)
r   r%   r   r   r   r   r   r   r?   r   r  r   r?   r   r  r   r?   r]   rH   )r  r   r  zOptional[Mapping[str, str]]r?   r   )r@   rA   rB   rC   r
   r   r   r  r  rE   r  r/   r9   r7   r   r     s    
 GG ,G '	G
 'G 
G GR 6	#   26-0- /- 
$	- -r9   r   c                  &    e Zd Ze	 	 	 	 dd       Zy)_DebugSerdeFxCompilec                $    | j                  |      S rH   )r  r  s     r7   r  z#_DebugSerdeFxCompile._send_to_child  s     !!-00r9   Nr  )r@   rA   rB   r
   r  r/   r9   r7   r!  r!    s$    161	#1 1r9   r!  c                      e Zd ZdZddZy)_OutOfProcessFxCompilez
    Represents an FxCompile which is run outside the current process (in
    either a subprocess or possibly even a separate machine).
    c                &   t        j                  |j                         t        j                  )t        j                  |j
                  j                         t        j                  d       dd       }|j                  r]|j                  D ]N  }t        j                  |j                  |j                  |j                  |j                  |j                          P |j"                  D ]%  } ||j$                        }|j'                  |       ' y )Nc                ,    t        j                  |       S rH   )r   r   )r3   s    r7   r   z6_OutOfProcessFxCompile._postprocess.<locals>.getLogger7  s    $$T**r9   )messagecategoryfilenamelinenosource)r3   rm   r?   zlogging.Logger)r   apply_deltasr   r   save_output_coder   source_code	functools	lru_cacher   r  warn_explicitr'  r(  r)  r*  r+  r   r3   handle)r=   r  r   wr  r   s         r7   r  z#_OutOfProcessFxCompile._postprocess'  s     	((8 ))5**6<<+C+CD 
		T	"	+ 
#	+   ** &&IIZZZZ8888 kk 	"Fv{{+FMM&!	"r9   Nr  )r@   rA   rB   rC   r  r/   r9   r7   r$  r$  !  s    
 "r9   r$  c                  *    e Zd ZdZe	 	 	 	 dd       Zy)_DebugFileFxCompiler   c                $   t         j                  }t         xj                  dz  c_        d| d}t        |d      5 }|j                  |j                         d d d        t        d|        	 	 t        j                  d       y # 1 sw Y   /xY w)Nr   z+/tmp/aorenste/pytorch_compile_fx_tmp_input_z.binwbz	Wrote to r   )r5  
file_indexopenwriter\   printr  r   reados_exit)r=   r  idxr3   r6   actualr   s          r7   r  z"_DebugFileFxCompile._send_to_child]  s     ",,&&!+&<SEF$ 	)GGM''(	)	$ !  HHRL#	) 	)s   BBNr  )r@   rA   rB   r8  r
   r  r/   r9   r7   r5  r5  Z  s)    J6	# r9   r5  )re   objectr?   r~   )r?   r   )Q
__future__r   r   r0   r/  r   r=  r   r   r  abcr   r   typingr   r   r   r	   typing_extensionsr
   r   r   torch._inductor.async_compilerV   torch.fxtorch._inductor.codecacher   r   torch._inductor.metricsr   r   torch._inductor.output_coder   r   r   r   torch._subclassesr   torch.utils._ordered_setr   rr   r   
compile_fxr   r   r   r   r   r   r   r   output_coder   rK   r    typescollections.abcr!   r"   r#   torch._inductor.utilsr$   r%   r'   r  r;   rc   rg   rv   r}   r   r   r   r   r   r   r   r   r!  r$  r5  r/   r9   r7   <module>rR     s   "     	  
   ! 6 6 7 7 $  F L  - /  M M    I  <</$ : : :D:+?+? <7 7,
(<(< 0 F F F  C C C</    D D D.   "=$ =$@.' .'bI9 I\1/ 1&"1 &"r0 r9   