
    sgX                       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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dg d      Z,	 	 	 	 	 	 d&dZ-	 	 	 	 	 	 d'dZ.d(dZ/ G d d      Z0d)dZ1e0jd                  e0jf                  e0jh                  e0jj                  e0jl                  e0jn                  e0jp                  dde0jr                  ddddddddddd d!d"d#f	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d*d$Z:e;d%k(  r e1        yy)+    )annotationsN)
namedtuple)Path)AnyCallableIterableIteratorSequence)	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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
             K/var/www/html/venv/lib/python3.12/site-packages/torchgen/gen_lazy_tensor.pyparse_native_functions_keysr7   ^   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_declsr1   shape_infr_declsshape_infr_decl_linesedeclmissing_declss           r6   validate_shape_inference_headerrJ   q   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=*/std::make_optional(tensor.scalar_type()), /*layout=*/std::make_optional(tensor.layout()), /*device=*/std::make_optional(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;
}
 rL       r6   get_ltc_helper_fnsrN      s    rM   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.hrC   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__rS   __annotations__rU   rC   rW   rX   r   rZ   r   r\   r^   rL   rM   r6   rP   rP      sa    Is $M:$GG1L#1;c;)22 	$ % ' $ &L#%rM   rP   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defaultre   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)actionre   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boolrR   rP   rS   rU   rC   rW   rX   r^   
parse_argsr   __file__parentabsoluterZ   gen_ts_loweringsr   r\   run_gen_lazy_tensorsource_yaml
output_dirdry_run	impl_path)parseroptions
torch_root	aten_pathrZ   r\   s         r6   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 %   ##(rM   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_dirry   )r   )r   ry   r   s    r6   make_file_managerz.run_gen_lazy_tensor.<locals>.make_file_managerE  s    #,
 	
rM   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funcrR   r3   )r1   r   s     r6   sort_native_functionz1run_gen_lazy_tensor.<locals>.sort_native_functionU  s6    
 %/q2F$Gq||  QVV499>>""rM   )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   r3   )r   xsops_listxfsr1   s         r6   concat_map_codegenz/run_gen_lazy_tensor.<locals>.concat_map_codegenq  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)rN   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_tensorlistr0   lazy_tensor_ptrmetrics_counterr\   	ns_helperrx   rC   rW   rX   try_get_tensortuple_aten_from_ltc_tensorss    r6   <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"   r0   r&   lazy_ir_objrU   r   s    r6   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.hrV   r   r   )lazy_non_native_ir_incnon_native_ir_nodesr   r   )dest!generate_non_native_lazy_ir_nodesr   r   )r   r   rU   r%   r   s    r6   r   z%run_gen_lazy_tensor.<locals>.<lambda>/  sn     '4M?=
'  TF!$
' $(#I#IK$ #,"4"4"+"4"4!
 
's   A)r   rR   returnr   )r1   z%NativeFunctionsGroup | NativeFunctionr   rR   )r   z)Callable[[NativeFunction], Sequence[str]]r   z/Iterable[NativeFunctionsGroup | NativeFunction]r   zlist[OperatorName]r   zIterator[str])r<   rB   r   r   r@   r   r   native_functionsr!   r   sortedr   r   r   r    r7   r   get_nop_selectornative_function_class_namer   r-   r   GenLazyShapeInferenceDefinitionrJ   r   r   r   write_with_template)8r~   rw   rx   ry   rz   rS   rU   rW   rX   rC   rZ   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_namerD   dispatch_keyr!   r   r   r"   r0   r&   r   r%   r   r   s8     ``  ```` `   ` ````````` ``                 @@@@@@@@@@r6   rv   rv     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 	
 	
< 	
rM   __main__)r/   rR   r0   z/Sequence[NativeFunction | NativeFunctionsGroup]r   z8tuple[list[OperatorName], list[Any], list[OperatorName]])rC   rR   rD   z	list[str]r   None)r   rR   )r   r   )<r~   rR   rw   rR   rx   rR   ry   rp   rz   rT   rS   rR   rU   rT   rW   rR   rX   rR   rC   rR   rZ   rY   r\   r[   r   rp   r   rp   r^   rR   r   rp   r   rp   r   rR   r   rR   r   rR   r   rR   r   rR   r   rR   r   rp   r   rR   r   rR   r   rR   r   rR   r   rR   r   r   )<
__future__r   rm   r@   collectionsr   pathlibr   typingr   r   r   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   r   r7   rJ   rN   rP   r   rS   rU   rW   rX   rC   rZ   r\   r^   rv   r_   rL   rM   r6   <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 rM   