
    qiA                       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 d dl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 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(m)Z) d dl*m+Z+ d dlm,Z,  ejZ                  e.      Z/ G d de      Z0 G d de0      Z1 G d de0      Z2dd	 	 	 	 	 	 	 	 	 	 	 ddZ3y)    )annotationsN)ABCabstractmethod)AbstractContextManagernullcontext)AnyLiteralOptionalTYPE_CHECKING)&BundledAOTAutogradSerializableCallable)dynamo_timed)normalize_path_separator)BoxedDeviceIndex)temporary_cache_dir)	BoxedBool	InputType)FakeTensorMode)ShapeEnv   )config)CallableSequence)	CacheInfo)GraphModulec                  v    e Zd ZdZ	 	 	 	 d	dZed
d       Zedd	 	 	 	 	 dd       Zedd	 	 	 	 	 dd       Z	y)CompiledArtifacta  
    CompiledArtifact class represents the inductor cache artifacts 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 cached artifact.
    c                     || _         || _        y N_compiled_fn
_artifactsselfcompiled_fn	artifactss      h/home/ubuntu/crypto_trading_bot/.venv/lib/python3.12/site-packages/torch/_inductor/standalone_compile.py__init__zCompiledArtifact.__init__5       
 (#    c                     y r    r#   argss     r&   __call__zCompiledArtifact.__call__=   s    +.r)   binaryformatc                    y r   r+   )r#   pathr1   s      r&   savezCompiledArtifact.save@   s     r)   c                @   |dk(  rt         j                  | |      S |dk(  sJ t        | d      5 }ddlm} ddlm} |j                         } ||      }|j                         }|t        j                  k(  rZ|j                          |       k(  sJ |j                         }|j                         sJ t        j                  |      cd d d        S |t         j                  k(  r|j                          |       k(  sJ |j                         }	|j                         }
|j                         sJ t        j                   j#                  |
       t         j%                  t'               |	      cd d d        S t)        d	|j+                  d
      z         # 1 sw Y   y xY w)Nunpackedr3   r1   r/   rbr   BytesReaderr   	torch_keyzLInvalid header, expected CacheCompiledArtifact or AOTCompiledArtifact, got: utf-8)CacheCompiledArtifactloadopen&torch.utils._appending_byte_serializerr:   	codecacher<   read
read_bytesAOTCompiledArtifact
AOT_HEADERis_finisheddeserializeCACHE_HEADERread_strtorchcompilerload_cache_artifacts
_load_implr   RuntimeErrordecode)r3   r1   filer:   r<   result_bytesreaderheaderartifactkeyartifact_bytess              r&   r?   zCompiledArtifact.loadE   s|    Z(--4-GG!!!$ 	J,99;L .F&&(F,777((*ik999!,,.))+++*66x@	 	 0===((*ik999oo'!'!2!2!4))+++33NC,77sK)	 	, #bmmG,- -	 	s   BFBF7FFNr$   Callable[..., Any]r%   z!Optional[tuple[bytes, CacheInfo]]r-   r   returnr   r3   strr1   Literal['binary', 'unpacked']r[   Noner3   r]   r1   r^   r[   r   )
__name__
__module____qualname____doc__r'   r   r.   r4   staticmethodr?   r+   r)   r&   r   r   #   s    "$'$ 5$ . .DL$A	  >F!!;!	! !r)   r   c                      e Zd ZdZ ed d      Z	 	 	 	 ddZddZdd	 	 	 	 	 ddZe		 	 	 	 	 	 dd       Z
e	dd	 	 	 	 	 dd	       Ze	dd	 	 	 	 	 dd
       Zy)r>   zN
    CompiledArtifact that depends on torch.compiler.save_cache_artifacts
    r=   c                     || _         || _        y r   r   r"   s      r&   r'   zCacheCompiledArtifact.__init__q   r(   r)   c                      | j                   | S r   )r    r,   s     r&   r.   zCacheCompiledArtifact.__call__y   s     t  $''r)   r/   r0   c          
     Z   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                  t        j                         |j                   |              |j                  |       |j                  |       ddlm}	  |	||j%                                np|d	k(  sJ t
        j                  j'                  |      r8t
        j                  j                  |      sJ t)        j*                  |d
       ddl
m}
 t/        |      5  t0        j2                  j5                  |      }|J |j6                  D ]  }|
j9                  |      }t
        j                  j'                  |      sJ t;        t        j<                  |            D ]p  }t?        t
        j                  jA                  ||      d      5 }tC        jD                  |      }d d d        jG                         }tH        jK                  d|       r  	 d d d        d d d        y # 1 sw Y   Ex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/   BytesWriterr;   write_atomicr6   T)ignore_errors)FxGraphCacher8   zOutput code written to: %s)&r   r!   rO   lenaot_autograd_artifactsosr3   isdirrA   rk   rB   r<   write_bytesr>   rI   	write_strtorch._inductor.codecacherm   to_bytesexistsshutilrmtreero   r   rK   rL   rM   inductor_artifacts_get_tmp_dir_for_keysortedlistdirr@   joinpickler?   write_to_diskloginfo)r#   r3   r1   rW   
cache_inforV   rk   r<   writerrm   ro   loaded_cache_infosubdirfgraphoutput_files                   r&   r4   zCacheCompiledArtifact.save|   sK    12 1	P&"\  *.&NJz889Q>J
J>33A6C!77==...N0$""#8#E#EF""9;/  %"">2BT6??#45+++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E1	P 1	P\7 7P PE1	P 1	Ps=   E3J!?B0J/J	2J8J!	JJJ	J!!J*c                  	 | 5  t        j                  d      5  t        j                  j                   j                  d      5  ddlm} |j                  |ddg i d       }d d d        J |\  }}dd	lm}  |t        d      t        d      
      }t        j                  j                  t        t                           }t        j                  j                  |      5  |j!                  g |j"                  |      	d d d        d d d        d d d        t%        	fdd       S # 1 sw Y   xY w# 1 sw Y   4xY w# 1 sw Y   8xY w# 1 sw Y   <xY w)NT)&unsafe_skip_cache_dynamic_shape_guards)strict_autograd_cacher   )AOTAutogradCacheF)localremoter-   r   
aot_configr   )_CompileFxKwargs)
cudagraphsboxed_forward_device_index	shape_envc                 &     t        |             S r   )list)r-   r$   s    r&   <lambda>z2CacheCompiledArtifact._load_impl.<locals>.<lambda>   s    ;tDz3J r)   )r   patchrK   
_functorch-torch._functorch._aot_autograd.autograd_cacher   _lookup
compile_fxr   r   r   _guardsTracingContextr   r   tracingwrap_post_compilesanitized_aot_configr>   )
cache_dir_ctxrV   r   resultentry_r   	fx_configcontextr$   s
            @r&   rN   z CacheCompiledArtifact._load_impl   sF   
  	LLE 	 !!((..T.J  *11 !# 2  %%%JUA4($U++;A+>I
 mm22>HJ3WXG&&w/ #5522I; 	  	B %%JDQQ; 2 ; 	  	  	  	sS   E+ED,$BE'D8EE,D51E8E=EE		EE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#               fcddd       S |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-        |       }||fcddd       S # 1 sw Y   RxY w# 1 sw Y   yxY w)zU
        Do format specific prep and loads, return a context manager and key
        zCompiledArtifact.loadr/   r8   Nr   r9   r   r;   r6   aotautograd)r   r   rr   r3   rs   r@   rC   rA   r:   rB   r<   rD   rJ   rG   rK   rL   rM   r   r   r   r~   rp   r   )r3   r1   rQ   r%   r:   r<   rS   rV   rW   autograd_cache_dirfilesr   s               r&   _prepare_loadz#CacheCompiledArtifact._prepare_load   s    (-12 	*!77==...$% , $		I,N0$Y/((*ik999oo'!'!2!2!4))+++33NCKM)#	* 	*& +++ww}}T***%'WW\\$%F"ww}}%7888RZZ(:;<5zQ&Ah 3D 9M)7	* 	*, ,		* 	*s+   3F8
F+BF87B*F8+F5	0F88Gc                b    t         j                  | |      \  }}t         j                  ||      S )Nr7   )r>   r   rN   )r3   r1   rV   r   s       r&   r?   zCacheCompiledArtifact.load   s9     3@@f A 
] %//sCCr)   NrX   rZ   r\   )r   zAbstractContextManager[Any]rV   r]   r[   r   )r3   r]   r1   r^   r[   z'tuple[str, AbstractContextManager[Any]]r`   )ra   rb   rc   rd   bytesrI   r'   r.   r4   re   rN   r   r?   r+   r)   r&   r>   r>   j   s     0':L$'$ 5$( EM4P4P$A4P	4Pl $R2$R9<$R	$R $RL >F"*"*;"*	0"* "*H >FDD;D	D Dr)   r>   c                      e Zd ZdZ ed d      Z	 	 ddZe	 	 	 	 dd       ZddZ	dd	 	 	 	 	 ddZ
dd	Zedd
       Zedd	 	 	 	 	 dd       Zy)rE   a9  
    Similar to CompiledArtifact, but the object is a single, bundled precompiled function.
    This object is always a serializable callable function.

    This object is essentially a wrapper for BundledAOTAutogradSerializableCallable, which
    is used by torch._dynamo.aot_compile for AOT Precompilation.
    r=   c                2    t        |      | _        d | _        y r   )r   inner_fnr!   )r#   r$   s     r&   r'   zAOTCompiledArtifact.__init__  s     ?{K 	r)   c                ,    t        | j                        S r   )rE   r$   )
bundled_fns    r&   from_bundled_callablez)AOTCompiledArtifact.from_bundled_callable  s     #:#9#9::r)   c                      | j                   | S r   )r   r,   s     r&   r.   zAOTCompiledArtifact.__call__"  s    t}}d##r)   r/   r0   c               0   |dk(  rt        d      | j                         }ddlm} ddlm}  |       }|j                  t        j                         |j                   |              |j                  |       ddl	m
}  |||j                                y )Nr6   8AOTCompiledArtifact does not support unpacked format yetr   rj   r   r;   rl   )rO   	serializerA   rk   rB   r<   rt   rE   rF   rv   rm   rw   )r#   r3   r1   rR   rk   r<   r   rm   s           r&   r4   zAOTCompiledArtifact.save%  s     ZJ  ~~'F(.99:9;'<(: 	T6??,-r)   c                @    t        j                  | j                        S r   )r   serialize_compile_artifactsr   )r#   s    r&   r   zAOTCompiledArtifact.serialize;  s    5QQMM
 	
r)   c                z    t        j                  |       }t        |t               sJ t        j	                  |      S r   )r   deserialize_compile_artifacts
isinstancerE   r   )rR   deserializeds     r&   rH   zAOTCompiledArtifact.deserialize@  s>     3PP 	
 ,(NOOO"88FFr)   c                   |dk(  rt        d      t        | d      5 }ddlm} ddlm} |j                         } ||      }|j                         }|t        j                  k(  sJ |j                          |       k(  sJ |j                         }|j                         sJ t        j                  |      cd d d        S # 1 sw Y   y xY w)Nr6   r   r8   r   r9   r   r;   )rO   r@   rA   r:   rB   r<   rC   rD   rE   rF   rG   rH   )	r3   r1   rQ   r:   r<   rR   rS   rT   rU   s	            r&   r?   zAOTCompiledArtifact.loadJ  s     ZJ  $ 	=J,99;L .F&&(F0;;;;;$$&)+555((*H%%'''&228<	= 	= 	=s   BCC
N)r$   rY   )r   r   r[   rE   rZ   r\   )r[   r   )rR   r   r[   rE   r`   )ra   rb   rc   rd   r   rF   r'   re   r   r.   r4   r   rH   r?   r+   r)   r&   rE   rE     s     ,g6J
'
 ;:;	; ;
$ EM..$A.	.,

 G G >F==;=	= =r)   rE   F)aotc          	        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  t
        j4                  j0                  j3                  d|      5  t7        j8                  |       }  || |fd|i|}t;        |      sJ |rFt=        |d      st?        d      tA        |      cd
d
d
       cd
d
d
       cd
d
d
       cd
d
d
       S t
        jB                  jE                         }|tF        jI                  d       d
d
d
       d
d
d
       d
d
d
       d
d
d
       tK              S # 1 sw Y   -xY w# 1 sw Y   1xY w# 1 sw Y   5xY w# 1 sw Y   9xY w)z=
    Implementation of torch.inductor.standalone_compile
    r   )CacheArtifactManagerr   )r   Ffrom_example_inputsr   Tfrom_tracing_contextN
from_graphoutputc                    d| j                   v rK| j                   d   }t        |t        j                  j                  j
                        r|j                  y y y )Nexample_value)metar   rK   _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bundled_autograd_cacheignore_shape_envr   z<Compiled function should have serialize method when aot=Truezstandalone_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   r   r   r   rK   r   r   getr   nextiterreversedr   nodesoprp   r-   r   fxNode
ValueErrorr   with_fresh_cacher   r   r   copydeepcopycallablehasattrrO   rE   rL   save_cache_artifactsr   warningr>   )gmexample_inputsdynamic_shapesoptionsr   r   r   r   r   	last_noder   r   r$   r%   r   s                 @r&   standalone_compiler   a  s    ;&.."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= 	%%&>D	 ]]2 
1A
EL
 $$$;4"R  '{3#    $ NN779	KKY)   2 !i883       sm   !K02K$	+K4AK		K	K$	K0+5K	 K(K$0K0KKK!K$$K-	)K00K9)r   r   r   zSequence[InputType]r   r   r   r   r   boolr[   r   )4
__future__r   r   loggingrr   r   ry   abcr   r   
contextlibr   r   typingr   r	   r
   r   torch.fxrK   torch._dynamo.aot_compile_typesr   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   r   	getLoggerra   r   r   r>   rE   r   r+   r)   r&   <module>r      s    "   	   # : 8 8  R , @ < G 6 , :  2/$ g!Ds DN[D, [D|V=* V=~ W9W9'W9 	W9
 W9 
W9 W9r)   