
    pi2(                    L   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mZm	Z	 d dl
mZmZmZmZmZ d dlZd dlmZ d dlmZ d dlmZ d dlmZ d d	lmZmZ d d
lmZ d dlm Z  ddl!m"Z" erd dl#m$Z$ d dl%m&Z& d dlm'Z'  ejP                  e)      Z* G d d      Z+	 	 	 	 	 	 	 	 	 	 ddZ,y)    )annotationsN)AbstractContextManagernullcontext)AnyCallableLiteralOptionalTYPE_CHECKING)dynamo_timed)normalize_path_separator)BoxedDeviceIndex)temporary_cache_dir)	BoxedBool	InputType)FakeTensorMode)ShapeEnv   )config)Sequence)	CacheInfo)GraphModulec                  x    e Zd ZU dZded<   ded<   	 	 	 	 ddZddZdd		 	 	 	 	 dd
Zedd		 	 	 	 	 dd       Z	y)CompiledArtifacta  
    CompiledArtifact class represents the precompiled inductor artifact that
    can be invoked in order to avoid repeated compilation.

    CompiledArtifact can be obtained by calling standalone_compile(gm, example_inputs)
    to create a fresh CompiledArtifact from a GraphModule and example inputs.

    Later this CompiledArtifact can be saved to disk, either as a binary or unpacked
    into the provided folder via the CompiledArtifact.save function.

    CompiledArtifact.load provides a way to create a CompiledArtifact from the
    binary or unpacked data.

    Finally, the CompiledArtifact can be invoked via the __call__ method
    to execute the precompiled artifact.
    Callable[..., Any]_compiled_fn!Optional[tuple[bytes, CacheInfo]]
_artifactsc                     || _         || _        y N)r   r   )selfcompiled_fn	artifactss      d/opt/services/ai/voice_agent/venv/lib/python3.12/site-packages/torch/_inductor/standalone_compile.py__init__zCompiledArtifact.__init__6   s    
 (#    c                      | j                   | S r   )r   )r    argss     r#   __call__zCompiledArtifact.__call__>   s     t  $''r%   binary)formatc          
     b   t        d      5  | j                  t        d      | j                  \  }}t        |j                        dk(  sJ |       |j                  d   }|dk(  rt
        j                  j                  |      rJ ddlm	} ddl
m}  |       }|j                   |              |j                  |       |j                  |       t        |d      5 }	|	j                  |j!                                d d d        np|d	k(  sJ t
        j                  j#                  |      r8t
        j                  j                  |      sJ t%        j&                  |d
       ddl
m}
 t+        |      5  t,        j.                  j1                  |      }|J |j2                  D ]  }|
j5                  |      }t
        j                  j#                  |      sJ t7        t        j8                  |            D ]p  }t        t
        j                  j;                  ||      d      5 }t=        j>                  |      }d d d        jA                         }tB        jE                  d|       r  	 d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   QxY w# 1 sw Y   *xY w# 1 sw Y   y xY w)NzCompiledArtifact.savezFCompiledArtifact.save failed to save since there's no artifact to saver   r   r)   )BytesWriter	torch_keywbunpackedT)ignore_errors)FxGraphCacherbzOutput code written to: %s)#r   r   RuntimeErrorlenaot_autograd_artifactsospathisdir&torch.utils._appending_byte_serializerr,   	codecacher.   write_bytes	write_stropenwriteto_bytesexistsshutilrmtreer2   r   torchcompilerload_cache_artifactsinductor_artifacts_get_tmp_dir_for_keysortedlistdirjoinpickleloadwrite_to_diskloginfo)r    r8   r*   artifact_bytes
cache_infokeyr,   r.   writerfiler2   loaded_cache_infosubdirfgraphoutput_files                   r#   savezCompiledArtifact.saveA   sZ    12 .	P&"\  *.&NJz889Q>J
J>33A6C!77==...N0$""9;/  %"">2$% 2JJv012 2 +++77>>$'77==...MM$d;3(. P(-(K(K&)% -888  1CC P!-!B!B3!G!ww~~f555$*2::f+=$> PD!%bggll64&@$!G 71(.A7*/*=*=*?KHH%A;O	PPP?.	P .	P*2 2,7 7P P?.	P .	PsV   CJ% J>A9J%7B0J'J=2J0J%J
	J%JJJ"	J%%J.c                   t        |       } t        d      5  |dk(  rt        j                  j	                  |       rJ t        | d      5 }|j                         }d d d        ddlm} ddl	m
}  |      }|j                          |       k(  sJ |j                         }|j                         }|j                         sJ t        j                  j!                  |       t#               }	n|dk(  sJ t        j                  j	                  |       sJ t        j                  j%                  | d	      }
t        j                  j	                  |
      sJ t'        t        j(                  |
            }t+        |      dk(  sJ |d   }t-        |       }	|	5  t/        j0                  d
      5  t        j2                  j.                  j1                  d
      5  ddlm} |j9                  |d
dg i d       }d d d        J ddlm}  |t?        d      tA        d            }t        jB                  jE                  tG        tI                           }t        jB                  jK                  |      5  |jM                  g |jN                  |      d d d        d d d        d d d        tQ        fdd       cd d d        S # 1 sw Y   ixY w# 1 sw Y   xY w# 1 sw Y   JxY w# 1 sw Y   NxY w# 1 sw Y   RxY w# 1 sw Y   y xY w)NzCompiledArtifact.loadr)   r3   r   )BytesReaderr   r-   r0   aotautogradT)&unsafe_skip_cache_dynamic_shape_guards)strict_autograd_cache)AOTAutogradCacheF)localremoter'   rR   
aot_config)_CompileFxKwargs)
cudagraphsboxed_forward_device_index	shape_envc                 &     t        |             S r   )list)r'   r!   s    r#   <lambda>z'CompiledArtifact.load.<locals>.<lambda>   s    +d4j2I r%   ))r   r   r7   r8   r9   r>   readr:   r]   r;   r.   
read_bytesread_stris_finishedrD   rE   rF   r   rK   rk   rJ   r5   r   r   patch
_functorch-torch._functorch._aot_autograd.autograd_cachera   _lookup
compile_fxre   r   r   _guardsTracingContextr   r   tracingwrap_post_compilesanitized_aot_configr   )r8   r*   rU   r"   r]   r.   readerrS   rQ   cache_dir_ctxautograd_cache_dirfilesra   entryre   	fx_configcontextr!   s                    @r#   rM   zCompiledArtifact.loadt   s    (-12 ?	Q!77==...$% , $		I,N0$Y/((*ik999oo'!'!2!2!4))+++33NC>Im+++ww}}T***%'WW\\$%F"ww}}%7888RZZ(:;<5zQ&Ah 3D 9 !DI! %%,,222N  -44"$#%#' 5 E (((8,(//?/B	
  --66"XZ8 ]]**73 "'"9"9E66	#K=! !D $$I4P?	Q ?	Q, ,: 4 =! ! ! !;?	Q ?	Qs   3L KD;L K4.+K(K	7A>K(5K	K(K4#L K	L KK(K%!K((K1-K44K=	9L  L	N)r!   r   r"   r   )r'   r   returnr   )r8   strr*   Literal['binary', 'unpacked']r   None)r8   r   r*   r   r   r   )
__name__
__module____qualname____doc____annotations__r$   r(   r[   staticmethodrM    r%   r#   r   r   !   s    " %$11$'$ 5$( EM1P1P$A1P	1Pf >FCQCQ;CQ	CQ CQr%   r   c                  ddl m} ddlm} d}|dk(  rt        t	                     d}n6|d	k(  rCt
        j                  j                  j                         }|j                  J |j                  n|d
k(  rt        t	                     t        t        t        | j                  j                                    }|j                  dk(  sJ t!        |j"                        dk(  sJ dfd}	t%        |j"                  d   t
        j&                  j(                        r |	|j"                  d          n,|j"                  d   D ]
  }
 |	|
        nt+        d| d      t
        j                  j                        }t
        j                  j-                  |      5  |j/                         5  t1        j2                  dd      5  t5        j6                  |       }  || |fd|i|}t9        |      sJ t
        j:                  j=                         }|t>        jA                  d       d d d        d d d        d d d        tC              S # 1 sw Y   %xY w# 1 sw Y   )xY w# 1 sw Y   -xY w)Nr   )CacheArtifactManagerr   )ru   Ffrom_example_inputsrh   Tfrom_tracing_context
from_graphoutputc                    d| j                   v rK| j                   d   }t        |t        j                  j                  j
                        r|j                  y y y )Nexample_value)meta
isinstancerD   _subclassesfake_tensor
FakeTensor	fake_mode)nodemaybe_tensorr   s     r#   handle_nodez'standalone_compile.<locals>.handle_node   sO    $))+#yy9lE,=,=,I,I,T,TU , 6 6I V ,r%   zJstandalone_compile got unsupported `dynamic_shapes` value: dynamic_shapes=.ztriton.autotune_at_compile_timeignore_shape_envzstandalone_compile artifact generation failed, cannot save. Run with TORCH_LOGS=+torch._inductor.codecache to identify the problem)r   ztorch.fx.Noder   r   )"torch.compiler._cacher   ru   r   r   rD   rv   rw   getr   nextiterreversedrY   nodesopr5   r'   r   fxNode
ValueErrorrx   with_fresh_cacher   rq   copydeepcopycallablerE   save_cache_artifactsrO   warningr   )gmexample_inputsdynamic_shapesoptionsr   ru   r   r   	last_noder   r   r!   r"   r   s                @r#   standalone_compiler      s6    ;&.."XZ8	  	1	1 --..224  ,,,%%		<	'"XZ8	
 hrxx~~678	||x'''9>>"a'''	7 innQ'7	q)*!q) "D!" XYgXhhij
 	
 mm**95Gg&--/ 	6= ]]2 
1A
EL
 $$$NN779	KKY  & K33'     s=   !I)2I	A$I-I5I)III&	"I))I2)
r   r   r   zSequence[InputType]r   r   r   r   r   r   )-
__future__r   r   loggingr7   rL   rB   
contextlibr   r   typingr   r   r   r	   r
   torch.fxrD   torch._dynamo.utilsr   torch._inductor.cpp_builderr   torch._inductor.cudagraph_utilsr   'torch._inductor.runtime.cache_dir_utilsr   torch._inductor.utilsr   r   torch._subclassesr   %torch.fx.experimental.symbolic_shapesr    r   collections.abcr   r   r   r   	getLoggerr   rO   r   r   r   r%   r#   <module>r      s    "   	   : B B  , @ < G 6 , :  (/$ g!WQ WQtM4M4'M4 	M4
 M4 M4r%   