
    %Vji2(                    <   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j(        e)          Z* G d d          Z+ddZ,dS )    )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                  d    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	dS )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]]
_artifactscompiled_fn	artifactsc                "    || _         || _        d S N)r   r   )selfr   r   s      \/root/voice-cloning/.venv/lib/python3.11/site-packages/torch/_inductor/standalone_compile.py__init__zCompiledArtifact.__init__6   s    
 (#    argsr   returnc                     | j         | S r!   )r   )r"   r&   s     r#   __call__zCompiledArtifact.__call__>   s     t $''r%   binary)formatpathstrr+   Literal['binary', 'unpacked']Nonec          
        t          d          5  | j        t          d          | j        \  }}t          |j                  dk    s
J |            |j        d         }|dk    rt
          j                            |          rJ ddlm	} ddl
m}  |            }|                     |                       |                    |           |                    |           t          |d          5 }	|	                    |                                           d d d            n# 1 swxY w Y   n|d	k    sJ t
          j                            |          r7t
          j                            |          sJ t%          j        |d
           ddl
m}
 t+          |          5  t,          j                            |          }|J |j        D ]}|
                    |          }t
          j                            |          sJ t7          t          j        |                    D ]}t          t
          j                            ||          d          5 }t=          j        |          }d d d            n# 1 swxY w Y   |                                 }tB          "                    d|           	 d d d            n# 1 swxY w Y   d d d            d S # 1 swxY w Y   d S )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osr,   isdir&torch.utils._appending_byte_serializerr1   	codecacher3   write_bytes	write_stropenwriteto_bytesexistsshutilrmtreer7   r   torchcompilerload_cache_artifactsinductor_artifacts_get_tmp_dir_for_keysortedlistdirjoinpickleloadwrite_to_diskloginfo)r"   r,   r+   artifact_bytes
cache_infokeyr1   r3   writerfiler7   loaded_cache_infosubdirfgraphoutput_files                   r#   savezCompiledArtifact.saveA   s    122 .	P .	P&"\   *.&NJz899Q>>>
>>>3A6C!!7==.....NNNNNN000000$""99;;///  %%%"">222$%% 2JJv001112 2 2 2 2 2 2 2 2 2 2 2 2 2 2 ++++7>>$'' <7==.....M$d;;;;333333(.. P P(-(K(K&) )% -888  1C P P!-!B!B3!G!G!w~~f55555$*2:f+=+=$>$> P PD!%bgll64&@&@$!G!G 71(.A7 7 7 7 7 7 7 7 7 7 7 7 7 7 7*/*=*=*?*?KHH%A;OOOO	PPP P P P P P P P P P P P P P P?.	P .	P .	P .	P .	P .	P .	P .	P .	P .	P .	P .	P .	P .	P .	P .	P .	P .	Ps   C$K4(D(K(D,	,K/D,	0A8K(B4KI=1K=JKJ4K:KK
	
KK
	KK"%K"c                   t          |           } t          d          5  |dk    rt          j                            |           rJ t          | d          5 }|                                }d d d            n# 1 swxY w Y   ddlm} ddl	m
}  ||          }|                                 |            k    sJ |                                }|                                }|                                sJ t          j                            |           t#                      }	n|dk    sJ t          j                            |           sJ t          j                            | d	          }
t          j                            |
          sJ t'          t          j        |
                    }t+          |          dk    sJ |d         }t-          |           }	|	5  t/          j        d
          5  t          j        j                            d
          5  ddlm} |                    |d
dg i d           }d d d            n# 1 swxY w Y   |J ddlm}  |t?          d          tA          d                    }t          j!        "                    tG          tI                                          }t          j!        %                    |          5  |&                    g |j'        |          d d d            n# 1 swxY w Y   d d d            n# 1 swxY w Y   d d d            n# 1 swxY w Y   tQ          fdd           cd d d            S # 1 swxY w Y   d S )NzCompiledArtifact.loadr*   r8   r   )BytesReaderr   r2   r5   aotautogradT)&unsafe_skip_cache_dynamic_shape_guards)strict_autograd_cache)AOTAutogradCacheF)localremoter&   rV   
aot_config)_CompileFxKwargs)
cudagraphsboxed_forward_device_index	shape_envc                 4     t          |                     S r!   )list)r&   r   s    r#   <lambda>z'CompiledArtifact.load.<locals>.<lambda>   s    ++d4jj2I2I r%   ))r   r   r<   r,   r=   rB   readr>   ra   r?   r3   
read_bytesread_stris_finishedrH   rI   rJ   r   rO   ro   rN   r:   r   r   patch
_functorch-torch._functorch._aot_autograd.autograd_cachere   _lookup
compile_fxri   r   r   _guardsTracingContextr   r   tracingwrap_post_compilesanitized_aot_configr   )r,   r+   rY   r   ra   r3   readerrW   rU   cache_dir_ctxautograd_cache_dirfilesre   entryri   	fx_configcontextr   s                    @r#   rQ   zCompiledArtifact.loadt   s    (--122 ?	Q ?	Q!!7==.....$%% , $		I, , , , , , , , , , , , , , ,NNNNNN000000$Y//((**iikk9999oo''!'!2!2!4!4))+++++33NCCC>Imm++++w}}T*****%'W\\$%F%F"w}}%788888RZ(:;;<<5zzQAh 3D 9 9 ! !DIII! ! %,222NN        -44"$#%#' 5  E               (((888888,,(///?/B/B  	
  -66"XZZ888  ]**733  "'"9"9E6	# #K              =! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !D $$I$I$I$I4PP?	Q ?	Q ?	Q ?	Q ?	Q ?	Q ?	Q ?	Q ?	Q ?	Q ?	Q ?	Q ?	Q ?	Q ?	Q ?	Q ?	Q ?	Qs   9M"A:.M":A>	>M"A>	E!M"#L89&L!"I	L!IL!IBL!!L
	>L!
LL!LL!L8!L%%L8(L%)L8,M"8L<	<M"?L<	 M""M&)M&N)r   r   r   r   )r&   r   r'   r   )r,   r-   r+   r.   r'   r/   )r,   r-   r+   r.   r'   r   )
__name__
__module____qualname____doc____annotations__r$   r)   r_   staticmethodrQ    r%   r#   r   r   !   s          " %$$$1111$ $ $ $( ( ( ( EM1P 1P 1P 1P 1P 1Pf >FCQ CQ CQ CQ CQ \CQ CQ CQr%   r   gmr   example_inputsSequence[InputType]dynamic_shapesr   optionsr'   c                  ddl m} ddlm} d}|dk    r t          t	                                d}n,|d	k    r4t
          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                                      }t
          j                            |          5  |                                5  t1          j        dd          5  t5          j        |           }  || |fd|i|}t9          |          sJ t
          j                                        }|t>                               d           d d d            n# 1 swxY w Y   d d d            n# 1 swxY w Y   d d d            n# 1 swxY w Y   tC          ||          S )Nr   )CacheArtifactManagerr   )ry   Ffrom_example_inputsrl   Tfrom_tracing_context
from_graphoutputnodetorch.fx.Noder'   r/   c                    d| j         v r:| j         d         }t          |t          j        j        j                  r|j        d S d S d S )Nexample_value)meta
isinstancerH   _subclassesfake_tensor
FakeTensor	fake_mode)r   maybe_tensorr   s     r#   handle_nodez'standalone_compile.<locals>.handle_node   sU    $)++#y9lE,=,I,TUU 7 , 6III ,+7 7r%   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   r   r'   r/   )"torch.compiler._cacher   ry   r   r   rH   rz   r{   getr   nextiterreversedr]   nodesopr:   r&   r   fxNode
ValueErrorr|   with_fresh_cacher   ru   copydeepcopycallablerI   save_cache_artifactsrS   warningr   )r   r   r   r   r   ry   r   r   	last_noder   r   r   r   r   s                @r#   standalone_compiler      s    ;:::::&&&&&&..."XZZ888	  	1	1	1 -.2244 ,,,%			<	'	'"XZZ888	
 hrx~667788	|x''''9>""a''''	7 	7 	7 	7 	7 	7 inQ'77 	"K	q)****!q) " "D!!!!" jYgjjj
 
 	
 m**955Gg&& --//  	6==  ]2 j
 
1A
EL
 
 $$$$$N7799	KKY                                              & K333s[    I15IA,I7III
III1I	I1!I	"I11I58I5)
r   r   r   r   r   r   r   r   r'   r   )-
__future__r   r   loggingr<   rP   rF   
contextlibr   r   typingr   r   r   r	   r
   torch.fxrH   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   rS   r   r   r   r%   r#   <module>r      s   " " " " " "   				   : : : : : : : : B B B B B B B B B B B B B B  , , , , , , @ @ @ @ @ @ < < < < < < G G G G G G 6 6 6 6 6 6 6 6 , , , , , , : : : : : :        %((((((//////$$$$$$ g!!WQ WQ WQ WQ WQ WQ WQ WQtM4 M4 M4 M4 M4 M4r%   