
    JThX                       S SK Jr  S SKrS SKrS SKJr  S SKJr  S SKJ	r	J
r
Jr  S SKrS SKJr  S SKJr  S SKJr  S SKJrJrJr  S S	KJrJr  S S
KJrJrJrJr  S SKJ r J!r!J"r"  S SK#J$r$  S SK%J&r&J'r'  S SK(J)r)  \(       a
  S SK*J+r+J,r,J-r-  \" S/ SQ5      r.      S'S jr/      S(S jr0S)S jr1 " S S5      r2S*S jr3\2Rh                  \2Rj                  \2Rl                  \2Rn                  \2Rp                  \2Rr                  \2Rt                  SS\2Rv                  SSSSSSSSSS S!S"S#S$4                                                           S+S% jjr<\=S&:X  a  \3" 5         gg),    )annotationsN)
namedtuple)Path)AnyCallableTYPE_CHECKING)	setValueT)BaseCppType)	GenLazyIRGenLazyNativeFuncDefinitionGenTSLazyIR)get_grouped_native_functionsparse_native_yaml)error_on_missing_kernelsgen_dispatcher_registrations"gen_dispatchkey_nativefunc_headersparse_backend_yaml)NativeFunctionNativeFunctionsGroupOperatorName)SelectiveBuilder)FileManagerNamespaceHelper)
YamlLoader)IterableIteratorSequenceParsedExternalYaml)backend_keyautograd_keycpp_namespacebackend_indicesfull_codegenc                V   [        U 5       n[        R                  " U[        S9nS S S 5        [	        W[
        5      (       d   eUR                  S/ 5      nUR                  S/ 5      nUR                  S/ 5      n[	        U[        5      (       d   e[	        U[        5      (       d   e[	        U[        5      (       d   eU Vs/ s H  n[        R                  " U5      PM     nnU Vs/ s H  n[        R                  " U5      PM     n	nXU	4$ ! , (       d  f       N= fs  snf s  snf )N)Loaderr#   
non_nativeir_gen)
openyamlloadr   
isinstancedictpoplistr   parse)
backend_yaml_pathgrouped_native_functionsfyaml_valuesr#   r&   r'   namefull_codegen_opnamesir_gen_opnamess
             P/var/www/auris/envauris/lib/python3.13/site-packages/torchgen/gen_lazy_tensor.pyparse_native_functions_keysr8   b   s     
	 Aii*5 
!k4((((??>26Lr2J__Xr*FlD))))j$''''fd####AMNL..t4N;AB64l((.6NB^;; 
!	  OBs   D D!* D&
Dc                    [        U 5       nUR                  5       n[        UR                  S5      5      nS S S 5        U Vs/ s H  ofW;  d  M
  UPM     nnU(       a.  [        SU  S[        R                  R                  U5       35      eg ! , (       d  f       N]= f! [         a  n[        SU  35      UeS nAff = fs  snf )N
z<Unable to read from the specified shape_inference_hdr file: zGMissing shape inference function.

Please add declare this function in zD:

and implement it in the corresponding shape_inference.cpp file.

)
r(   readsetsplitOSErrorAssertionError	Exceptionoslinesepjoin)shape_inference_hdrexpected_shape_infr_declsr2   shape_infr_declsshape_infr_decl_linesedeclmissing_declss           r7   validate_shape_inference_headerrK   u   s    %&! vvx$'(8(>(>t(D$E! ' 32BW6W2   %%8$9 :  $
 	
  '&  JK^J_`
	s?   B  +BB  	CC
BB  B   
B>*B99B>c                     g)Na  at::Tensor to_meta(const at::Tensor& tensor) {
  // undefined tensors can't be converted to the meta device, since they don't have sizes/strides
  if (!tensor.defined()) return tensor;
  auto out = at::native::empty_strided_meta_symint(tensor.sym_sizes(), tensor.sym_strides(), /*dtype=*/tensor.scalar_type(), /*layout=*/tensor.layout(), /*device=*/c10::Device(c10::kMeta), /*pin_memory=*/std::nullopt);
  // needs to handle wrapped numbers, so dtype promotion works properly.
  if (tensor.unsafeGetTensorImpl()->is_wrapped_number()) {
    out.unsafeGetTensorImpl()->set_wrapped_number(true);
  }
  return out;
}
std::optional<at::Tensor> to_meta(const std::optional<at::Tensor>& tensor) {
  if (tensor.has_value()) {
    return to_meta(*tensor);
  }
  return std::nullopt;
}

std::vector<at::Tensor> to_meta(at::ITensorListRef t_list) {
  std::vector<at::Tensor> outs;
  outs.reserve(t_list.size());
  for (const auto& tensor : t_list) {
    outs.push_back(to_meta(tensor));
  }
  return outs;
}
 rM       r7   get_ltc_helper_fnsrO      s    rN   c                      \ rS rSr% SrS\S'   SrS\S'   SrS\S	'   S
rS\S'   Sr	S\S'   \
rS\S'   \rS\S'   SrS\S'   Srg)default_args   Nodestr	node_baseN
str | Nonenode_base_hdr&torch/csrc/lazy/core/shape_inference.hrD   ztorch::lazy::LazyTensortensor_classztorch/csrc/lazy/core/tensor.htensor_class_hdrtype[GenLazyIR]lazy_ir_generator!type[GenLazyNativeFuncDefinition] native_func_definition_generatorTorchScriptbackend_namerM   )__name__
__module____qualname____firstlineno__rU   __annotations__rW   rD   rY   rZ   r   r\   r   r^   r`   __static_attributes__rM   rN   r7   rQ   rQ      s]    Is $M:$GG1L#1;c;)22# %&G  &L#%rN   rQ   c                    [         R                  " SS9n U R                  SSSSS9  U R                  SS	S
SS9  U R                  SS[        SSS9  U R                  SS[        S SS9  U R                  SSSSS9  U R                  SS[        [
        R                  SS9  U R                  SS[        [
        R                  SS9  U R                  SS[        [
        R                  S S9  U R                  S!S"[        [
        R                  S#S9  U R                  S$S%[        [
        R                  S&S9  U R                  S'S([        [
        R                  S)S9  U R                  5       n[        [        5      R                  5       R                   S*   n[	        US+-  S,-  S--  5      n[
        R"                  nUR$                  (       a  [&        n[
        R(                  n[+        UUR,                  UR.                  UR0                  UR2                  UR                  UR                  UR                  UR                  UR                  UUUR                  5        g ).Nz"Generate Lazy Tensor backend files)descriptionz-sz--source-yamlz--source_yamlzApath to source yaml file containing operator external definitions)helpz-oz--output-dirz--output_dirzoutput directoryz	--dry-runz	--dry_runF)typedefaultri   z--impl-pathz--impl_pathz9path to the source C++ file containing kernel definitionsz--gen-ts-loweringsz--gen_ts_lowerings
store_truezIGenerate TorchScript lowerings in addition to Lazy IR and NativeFunctions)actionri   z--node-basez--node_basez7Name of backend specific custom Lazy IR Node base classz--node-base-hdrz--node_base_hdrz;Path to header file defining custom Lazy IR Node base classz--shape-inference-hdrz--shape_inference_hdrzBPath to header file defining custom Lazy shape inference functionsz--tensor-classz--tensor_classz1Name of backend specific custom Lazy Tensor classz--tensor-class-hdrz--tensor_class_hdrz5Path to header file defining custom Lazy Tensor classz--backend-namez--backend_namezName of the backend to generate   atensrcATen)argparseArgumentParseradd_argumentboolrT   rQ   rU   rW   rD   rY   rZ   r`   
parse_argsr   __file__absoluteparentsr\   gen_ts_loweringsr   r^   run_gen_lazy_tensorsource_yaml
output_dirdry_run	impl_path)parseroptions
torch_root	aten_pathr\   r^   s         r7   mainr      s   $$1UVF
P	   nnCUV
[tUAS   H   X	   &&F   **J   00Q   ))@   --D   )).   !G h((*2215JJ'%/&89I)5)G)G'55 %   ##(rN   FTztorch::lazyGetTensorList$GetLtcTensorOrCreateForWrappedNumberTryGetLtcTensorzTORCH_LAZY_FN_COUNTER("lazy::")zLazyTensor::Createz$torch::lazy::CreateAtenFromLtcTensorz$torch::lazy::TupleAtenFromLtcTensorsztorch::lazy::ValueLazyTensorPtrztorch::lazy::GetBackendDevicec                >  ^^^^^^	^^^^^^^^^^^^^^.^/^0^1^2^3^4^5^6^7 UR                  S5      nUS   nSR                  US S 5      n[        [        UU5      5        [        R
                  R                  U S5      m7SUU74S jjn U " T5      n![        R
                  R                  U S5      n"[        R
                  R                  U S5      n#[        U"U#5      n$U$R                  U$R                  sn%m.[        U%5      m2SS jn&[        T2U&S9m2[        UT2T.5      n'U'R                  m/U'R                  n(U'R                  n)U'R                  m.[        UT25      u  m1m5m3T14       SS	 jjm0[         R"                  " 5       n*T/c   eT.T/   R%                  5       n+Ub  ['        U%T.T/U(U+UT15         T	b6  [)        T0" [*        R,                  " T.T/   T5      T25      5      n,[/        T	U,5        U+c   e[1        U!U+U)T.T2T/U(U5        U(c  T//OT/U(/ H  n-[3        U!TU+T.T2T/U-U*UUUS
S9  M     [5        U)5      m6U!R7                  T/ S3SU.U/UU0UUUUUUUU2UUUU6UU	UUUU4S j5        U
" T.T/   XU5      m4U!R7                  SSU0U1U2U3U4UU64S j5        U!R7                  SSU4UU5U64S j5        g )Nz::	templatesc                   > [        U TTS9$ )N)install_dirtemplate_dirr~   )r   )r   r~   r   s    r7   make_file_manager.run_gen_lazy_tensor.<locals>.make_file_managerI  s    #,
 	
rN   znative/native_functions.yamlznative/tags.yamlc                    [        U [        5      (       a  U R                  R                  OU R                  n[	        UR
                  R
                  5      $ )z
We sort the native function because of the note in concat_map_codegen.
TODO(alanwaketan): Remove this sorting hack once all ops are grouped properly.
)r+   r   
functionalfuncrT   r4   )r2   r   s     r7   sort_native_function1run_gen_lazy_tensor.<locals>.sort_native_functionY  s9    
 %/q2F$G$Gq||  QVV499>>""rN   )keyc              3     #    U Hi  n[        U[        5      (       a  [        UR                  5       5      OU/nU H/  nUR                  R
                  U;   d  M  U " U5       Sh  vN   M1     Mk     g N7f)z
We code-gen for the functional variant, which is all we need for IR classes/lowerings/shape inferences, but we
only code-gen additional entries for the inplace variant for the native functions.
N)r+   r   r.   	functionsr   r4   )r   xsops_listxfsr2   s         r7   concat_map_codegen/run_gen_lazy_tensor.<locals>.concat_map_codegenu  s`      A(216J(K(Kakkm$RSQTB66;;(*#Aw&&   's   AA5A5%A3&A5F)build_in_treeper_operator_headersr`   eager_registrationzNativeFunctions.cppzDispatchKeyNativeFunctions.cppc                    > TTSSSSSSSSS	S
ST ST S3T S3/T(       a  S/O/ -    V s/ s H	  n SU  S3PM     sn [        5       STR                  TR                  [        T" T" T S3TT   TTTTT
TTTTTTTT	5      T5      5      S.$ s  sn f )NzATen/Functions.hzATen/native/TensorConversions.hzATen/NativeFunctions.hz6ATen/CompositeExplicitAutogradNonFunctionalFunctions.hzATen/MetaFunctions.hzATen/Operators.hzATen/native/CPUFallback.h!torch/csrc/lazy/core/ir_builder.hz*torch/csrc/lazy/core/lazy_graph_executor.hztorch/csrc/lazy/core/metrics.htorch/csrc/lazy/core/shape.h/zNativeFunctions.hz	/LazyIr.hz.torch/csrc/lazy/ts_backend/ts_eager_fallback.h
#include <> NativeFunctions)includes
helper_fnsnative_functions_includenamespace_prologuenamespace_epiloguenative_function_definitions)rO   prologueepiloguer.   )pathr"   r   backend_namespacer   create_aten_from_ltc_tensorcreate_from_first_tensorcreate_tensorgen_forced_fallback_codeget_device_fnget_tensor_or_wrap_numberget_tensorlistr1   lazy_tensor_ptrmetrics_counterr^   	ns_helperr}   rD   rY   rZ   try_get_tensortuple_aten_from_ltc_tensorss    r7   <lambda>%run_gen_lazy_tensor.<locals>.<lambda>  s    %'&5,L*&/7@42!l!K=0AB!l),& 0 FF)D TF!$2 -.(*"+"4"4"+"4"4+/"4&-7'4$0)&1&'%033'%" -%,=4
s   BzLazyIr.hc            
        > S V s/ s H	  n SU  S3PM     sn Tb  ST S3/O/ [        T" TTTT-   5      5      TR                  TR                  S.$ s  sn f )N)zATen/core/Formatting.hzc10/core/ScalarType.hztorch/csrc/lazy/core/hash.htorch/csrc/lazy/core/ir.hr   optionalvectorr   r   z
#include "")lazy_ir_sysinclazy_ir_incir_declarationsr   r   )r.   r   r   )r   r   r#   r1   r'   lazy_ir_objrW   r   s    r7   r   r     s    D TF!$ ( )q9:#"!9<&;P 
 #,"4"4"+"4"4-
s   AzLazyNonNativeIr.hc                    > / SQT(       a  T/O/ -    V s/ s H  n U (       d  M  SU  S3PM     sn [         R                  " TT5      TR                  TR                  S.$ s  sn f )N)r   r   z+torch/csrc/lazy/core/internal_ops/ltc_ops.hrX   r   r   )lazy_non_native_ir_incnon_native_ir_nodesr   r   )dest!generate_non_native_lazy_ir_nodesr   r   )r   r   rW   r&   r   s    r7   r   r   3  sw     '4M?=
'=D  %*TF!$=
' $(#I#IK$ #,"4"4"+"4"4!

's
   A
A)r   rT   returnr   )r2   z%NativeFunctionsGroup | NativeFunctionr   rT   )r   z)Callable[[NativeFunction], Sequence[str]]r   z/Iterable[NativeFunctionsGroup | NativeFunction]r   zlist[OperatorName]r   zIterator[str])r=   rC   r	   r
   rA   r   r   native_functionsr"   r   sortedr   r   r    r!   r8   r   get_nop_selectornative_function_class_namer   r.   r   GenLazyShapeInferenceDefinitionrK   r   r   r   write_with_template)8r   r|   r}   r~   r   rU   rW   rY   rZ   rD   r\   r^   r   r   r`   r   use_lazy_shaper   r   r   r   r   r   r   r   r   lazy_value_classr   r   	lv_tokenslv_classlv_nsr   fmnative_yaml_pathtags_yaml_pathparsed_yamlr   r   parsed_backend_yamlr    r!   selector
class_namerE   dispatch_keyr"   r   r   r#   r1   r'   r   r&   r   r   s8     ``  ```` `   ` ````````` ``                 @@@@@@@@@@r7   r{   r{     s   L !&&t,I}HIIin%Ek%*+77<<	;7L
 

 
:	&Bww||I/MNWW\\)-?@N#$4nEK$$## &o  <<LM#  & &:  -- &11K&33L'55M)99O (C-($L*f (4'7';' %' 
	'   002H""" -HHJJ 	
 &$(44#K0, )	%
! 	((;=VW!!! '
 	 &-K3NN 	 	%$'!5%$	
 	O$  .I-*+(4	
 4	
 4	
 4	
8t $$l~K 	
 	
< 	
rN   __main__)r0   rT   r1   z/Sequence[NativeFunction | NativeFunctionsGroup]r   z8tuple[list[OperatorName], list[Any], list[OperatorName]])rD   rT   rE   z	list[str]r   None)r   rT   )r   r   )<r   rT   r|   rT   r}   rT   r~   ru   r   rV   rU   rT   rW   rV   rY   rT   rZ   rT   rD   rT   r\   r[   r^   r]   r   ru   r   ru   r`   rT   r   ru   r   ru   r   rT   r   rT   r   rT   r   rT   r   rT   r   rT   r   ru   r   rT   r   rT   r   rT   r   rT   r   rT   r   r   )>
__future__r   rr   rA   collectionsr   pathlibr   typingr   r   r   r)   torchgen.destr   torchgen.api.lazyr	   torchgen.api.typesr
   torchgen.dest.lazy_irr   r   r   torchgen.genr   r   torchgen.gen_backend_stubsr   r   r   r   torchgen.modelr   r   r   !torchgen.selective_build.selectorr   torchgen.utilsr   r   torchgen.yaml_utilsr   collections.abcr   r   r   r   r8   rK   rO   rQ   r   rU   rW   rY   rZ   rD   r\   r^   r`   r{   ra   rM   rN   r7   <module>r      s   "  	 "  / /   ' * U U H  N M > 7 * <<~  W <<M< ><&

9B
	
6@
& 
&]L "++ , : :$11(99+??)5)G)G 	55 "'$11%* +)%K+<-%*'M'M0*8Ihhh h 	h
 h h h h h h 'h'h  !h& 'h( )h* #+h, -h2 3h4 5h6  #7h8 9h: ;h< =h> #?h@ "%AhB "%ChD EhF GhH IhJ 
KhV	 zF rN   