
    OgX                       d dl mZ d dlZd dlZd dlmZ d dlmZ d dl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mZ d d	lmZmZ d d
lmZmZmZmZ d dlm Z m!Z!m"Z" d dl#m$Z$ d dl%m&Z&m'Z' d dl(m)Z) er
d dl*m+Z+m,Z,m-Z-  edg d      Z.	 	 	 	 	 	 d'dZ/	 	 	 	 	 	 d(dZ0d)dZ1 G d d      Z2d*dZ3e2jh                  e2jj                  e2jl                  e2jn                  e2jp                  e2jr                  e2jt                  dde2jv                  dddddddddd d!d"d#d$f	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d+d%Z<e=d&k(  r e3        yy),    )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                   t        |       5 }t        j                  |t              }d d d        t	        t
              sJ |j                  dg       }|j                  dg       }|j                  dg       }t	        |t              sJ t	        |t              sJ t	        |t              sJ |D cg c]  }t        j                  |       }}|D cg c]  }t        j                  |       }	}|||	fS # 1 sw Y   xY wc c}w c c}w )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
             _/var/www/html/suriana-translation/venv/lib/python3.12/site-packages/torchgen/gen_lazy_tensor.pyparse_native_functions_keysr8   b   s     
	  6Aii*56k4(((??>26Lr2J__Xr*FlD)))j$'''fd###AMNL..t4NN;AB4l((.BNB^;;6 6 OBs   C72DD7D c                l   	 t        |       5 }|j                         }t        |j                  d            }d d d        |D cg c]	  }|vs| }}|r.t        d|  dt        j                  j                  |             y # 1 sw Y   NxY w# t        $ r}t        d|        |d }~ww xY wc c}w )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    %& 	F! vvx$'(8(>(>t(D$E!	F 3dBW6WM  %%8$9 :  $
 	
 	F 	F  JK^J_`
	s9   B +BB 	B1B1B
B 	B.B))B.c                      y)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                  ~    e Zd ZU dZded<   dZded<   dZded<   d	Zded
<   dZded<   e	Z
ded<   eZded<   dZded<   y)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_name)__name__
__module____qualname__rT   __annotations__rV   rD   rX   rY   r   r[   r   r]   r_   rM   rN   r7   rQ   rQ      s]    Is $M:$GG1L#1;c;)22# %&G  &L#%rN   rQ   c                    t        j                  d      } | j                  dddd       | j                  dd	d
d       | j                  ddt        dd       | j                  ddt        d d       | j                  dddd       | j                  ddt        t
        j                  d       | j                  ddt        t
        j                  d       | j                  ddt        t
        j                  d        | j                  d!d"t        t
        j                  d#       | j                  d$d%t        t
        j                  d&       | j                  d'd(t        t
        j                  d)       | j                         }t        t              j                  j                  j                  j!                         }t	        |d*z  d+z  d,z        }t
        j"                  }|j$                  rt&        }t
        j(                  }t+        ||j,                  |j.                  |j0                  |j2                  |j                  |j                  |j                  |j                  |j                  |||j                         y )-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defaultrf   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)actionrf   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boolrS   rQ   rT   rV   rD   rX   rY   r_   
parse_argsr   __file__parentabsoluter[   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&&--44==?J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                *  	./01234567 |j                  d      }|d   }dj                  |d d       }t        t        ||             t        j
                  j                  | d      7d7fd}  |       }!t        j
                  j                  | d      }"t        j
                  j                  | d      }#t        |"|#      }$|$j                  |$j                  c}%.t        |%      2dd}&t        2|&      2t        |2.      }'|'j                  /|'j                  }(|'j                  })|'j                  .t        |2      \  1531f	 	 	 	 	 	 	 dd	0t!        j"                         }*/J ./   j%                         }+|t'        |%./|(|+|1       	 	5t)         0t+        j,                  ./         2            },t/        	|,       |+J t1        |!|+|).2/|(|       |(/gn/|(gD ]  }-t3        |!|+.2/|-|*|||d
        t5        |)      6|!j7                  / dd./026	fd        |
./   |||      4|!j7                  dd012346fd       |!j7                  dd456fd       y )Nz::	templatesc                     t        |       S )N)install_dirtemplate_dirrz   )r   )r   rz   r   s    r7   make_file_managerz.run_gen_lazy_tensor.<locals>.make_file_managerI  s    #,
 	
rN   znative/native_functions.yamlznative/tags.yamlc                    t        | t              r| j                  j                  n| j                  }t	        |j
                  j
                        S )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funcrS   r4   )r2   r   s     r7   sort_native_functionz1run_gen_lazy_tensor.<locals>.sort_native_functionY  s6    
 %/q2F$Gq||  QVV499>>""rN   )keyc              3     K   |D ]^  }t        |t              rt        |j                               n|g}|D ]+  }|j                  j
                  |v s | |      E d{    - ` y7 	w)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_codegenz/run_gen_lazy_tensor.<locals>.concat_map_codegenu  sh       	'A(216J(Kakkm$RSQTB '66;;(*#Aw&&'	' 's   AA)A)A'
A)F)build_in_treeper_operator_headersr_   eager_registrationzNativeFunctions.cppzDispatchKeyNativeFunctions.cppc                    ddddddddd	d
d d d dgrdgng z   D  cg c]  } d|  d
 c} t               dj                  j                  t           d   
	                  dS c c} w )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_helperry   rD   rX   rY   try_get_tensortuple_aten_from_ltc_tensorss    r7   <lambda>z%run_gen_lazy_tensor.<locals>.<lambda>  s     %'&5,L*&/7@42!l!K=0AB!l),& 0 FF) TF!$2 -.(*"+"4"4"+"4"4+/"4&-7'4$0)&1&'%033'%" -%,=4
 s   BzLazyIr.hc            
         dD  cg c]  } d|  d
 c} d dgng t         z               j                  j                  dS c c} w )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_objrV   r   s    r7   r   z%run_gen_lazy_tensor.<locals>.<lambda>  s}     TF!$ ( )q9:#"!9<&;P 
 #,"4"4"+"4"4-
 s   AzLazyNonNativeIr.hc                     g drgng z   D  cg c]
  } | rd|  d c} t        j                        j                  j                  dS c c} w )N)r   r   z+torch/csrc/lazy/core/internal_ops/ltc_ops.hrW   r   r   )lazy_non_native_ir_incnon_native_ir_nodesr   r   )dest!generate_non_native_lazy_ir_nodesr   r   )r   r   rV   r&   r   s    r7   r   z%run_gen_lazy_tensor.<locals>.<lambda>3  sn     '4M?=
'  TF!$
' $(#I#IK$ #,"4"4"+"4"4!
 
's   A)r   rS   returnr   )r2   z%NativeFunctionsGroup | NativeFunctionr   rS   )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   rx   ry   rz   r{   rT   rV   rX   rY   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   rw   rw     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
 	%$'!5%$	

&  .I-*+(4	
 4	
 4	
 4	
8t $$lI~K 	
 	
< 	
rN   __main__)r0   rS   r1   z/Sequence[NativeFunction | NativeFunctionsGroup]r   z8tuple[list[OperatorName], list[Any], list[OperatorName]])rD   rS   rE   z	list[str]r   None)r   rS   )r   r   )<r   rS   rx   rS   ry   rS   rz   rq   r{   rU   rT   rS   rV   rU   rX   rS   rY   rS   rD   rS   r[   rZ   r]   r\   r   rq   r   rq   r_   rS   r   rq   r   rq   r   rS   r   rS   r   rS   r   rS   r   rS   r   rS   r   rq   r   rS   r   rS   r   rS   r   rS   r   rS   r   r   )>
__future__r   rn   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   rT   rV   rX   rY   rD   r[   r]   r_   rw   r`   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   