
    rhP%                    @   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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jL                  e'      Z( G d d      Z)	 	 	 	 	 	 	 	 	 	 ddZ*y)    )annotationsN)AbstractContextManagernullcontext)AnyCallableLiteralOptionalTYPE_CHECKING)dynamo_timed)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      u/var/www/html/ai-insurance-compliance-backend/venv/lib/python3.12/site-packages/torch/_inductor/standalone_compile.py__init__zCompiledArtifact.__init__5   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rmtreer1   r   torchcompilerload_cache_artifactsinductor_artifacts_get_tmp_dir_for_keysortedlistdirjoinpickleloadwrite_to_diskloginfo)r   r7   r)   artifact_bytes
cache_infokeyr+   r-   writerfiler1   loaded_cache_infosubdirfgraphoutput_files                   r"   savezCompiledArtifact.save@   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        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-        j.                  d
      5  t        j0                  j,                  j/                  d
      5  ddlm} |j7                  |d
dg i d       }d d d        J ddlm}  |t=        d      t?        d            }t        j@                  jC                  tE        tG                           }t        j@                  jI                  |      5  |jK                  g |jL                  |      d d d        d d d        d d d        tO        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(   r2   r   )BytesReaderr   r,   r/   aotautogradT)&unsafe_skip_cache_dynamic_shape_guards)strict_autograd_cache)AOTAutogradCacheF)localremoter&   rQ   
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   r6   r7   r8   r=   readr9   r\   r:   r-   
read_bytesread_stris_finishedrC   rD   rE   r   rJ   rj   rI   r4   r   r   patch
_functorch-torch._functorch._aot_autograd.autograd_cacher`   _lookup
compile_fxrd   r   r   _guardsTracingContextr   r   tracingwrap_post_compilesanitized_aot_configr   )r7   r)   rT   r!   r\   r-   readerrR   rP   cache_dir_ctxautograd_cache_dirfilesr`   entryrd   	fx_configcontextr    s                    @r"   rL   zCompiledArtifact.loads   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   3K5 J8D;K5K)#+KK	,A>K*K	KK)K58K	=K5K
KKKK&"K))K2	.K55K>N)r    r   r!   r   )r&   r   returnr   )r7   strr)   Literal['binary', 'unpacked']r   None)r7   r   r)   r   r   r   )
__name__
__module____qualname____doc____annotations__r#   r'   rZ   staticmethodrL    r$   r"   r   r       s    " %$11$'$ 5$( EM1P1P$A1P	1Pf >FBQBQ;BQ	BQ BQr$   r   c                  ddl m} ddlm} d}|dk(  rt        t	                     }d}n,|d	k(  r5t
        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 |	j"                  d   D ][  }
d|
j$                  v s|
j$                  d   }t'        |t
        j(                  j*                  j,                        sP|j                  }] nt/        d| d      t
        j                  j                  |      }t
        j                  j1                  |      5  |j3                         5  t5        j6                  dd      5  t9        j:                  |       }  || |fd|i|}t=        |      sJ t
        j>                  jA                         }|tB        jE                  d       d d d        d d d        d d d        tG              S # 1 sw Y   %xY w# 1 sw Y   )xY w# 1 sw Y   -xY w)Nr   )CacheArtifactManagerr   )rt   Ffrom_example_inputsrg   Tfrom_tracing_context
from_graphoutputexample_valuezJstandalone_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)$torch.compiler._cacher   rt   r   r   rC   ru   rv   get	fake_modenextiterreversedrX   nodesopr4   r&   meta
isinstance_subclassesfake_tensor
FakeTensor
ValueErrorrw   with_fresh_cacher   rp   copydeepcopycallablerD   save_cache_artifactsrN   warningr   )gmexample_inputsdynamic_shapesoptionsr   rt   r   r   r   	last_nodenodemaybe_tensorr    r!   s                 r"   standalone_compiler      s)    ;&.."XZ8	  	1	1 --..224%%		<	'"XZ8	
 hrxx~~678	||x'''9>>"a'''NN1% 	7D$))+#yy9lE,=,=,I,I,T,TU , 6 6I		7 XYgXhhij
 	
 mm**95Gg&--/ 	6= ]]2 
1A
EL
 $$$NN779	KKY  & K33'     s=   I'I>A$I"I*IIII	II')
r   r   r   zSequence[InputType]r   r   r   r   r   r   )+
__future__r   r   loggingr6   rK   rA   
contextlibr   r   typingr   r   r   r	   r
   torch.fxrC   torch._dynamo.utilsr   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   rN   r   r   r   r$   r"   <module>r      s    "   	   : B B  , < G 6 , :  (/$ g!VQ VQr=4=4'=4 	=4
 =4 =4r$   