
    `iX                       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.dQdZ/dRdZ0dSdZ1 G d d           Z2dTd!Z3e2j4        e2j5        e2j6        e2j7        e2j8        e2j9        e2j:        d"d"e2j;        d"d#d$d%d&d'd(d)d"d*d+d,d-d.fdUdOZ<e=dPk    r e3             dS dS )V    )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_codegenbackend_yaml_pathstrgrouped_native_functions/Sequence[NativeFunction | NativeFunctionsGroup]return8tuple[list[OperatorName], list[Any], list[OperatorName]]c                   t          |           5 }t          j        |t                    }d d d            n# 1 swxY w Y   t	          |t
                    sJ |                    dg           }|                    dg           }|                    dg           }t	          |t                    sJ t	          |t                    sJ t	          |t                    sJ d |D             }d |D             }|||fS )N)Loaderr#   
non_nativeir_genc                6    g | ]}t          j        |          S  r   parse.0names     l/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/torchgen/gen_lazy_tensor.py
<listcomp>z/parse_native_functions_keys.<locals>.<listcomp>p   s#    NNNL.t44NNN    c                6    g | ]}t          j        |          S r/   r0   r2   s     r5   r6   z/parse_native_functions_keys.<locals>.<listcomp>q   s#    BBB4l(..BBBr7   )openyamlloadr   
isinstancedictpoplist)	r$   r&   fyaml_valuesr#   r,   r-   full_codegen_opnamesir_gen_opnamess	            r5   parse_native_functions_keysrD   b   s>    
	 	  6Ai*5556 6 6 6 6 6 6 6 6 6 6 6 6 6 6k4(((((??>266Lr22J__Xr**FlD)))))j$'''''fd#####NNNNNBB6BBBN^;;s   8<<shape_inference_hdrexpected_shape_infr_decls	list[str]Nonec                   	 t          |           5 }|                                }t          |                    d                    d d d            n# 1 swxY w Y   n%# t          $ r}t          d|            |d }~ww xY wfd|D             }|r2t          d|  dt          j        	                    |                     d S )N
z<Unable to read from the specified shape_inference_hdr file: c                    g | ]}|v|	S r/   r/   )r3   declshape_infr_decl_liness     r5   r6   z3validate_shape_inference_header.<locals>.<listcomp>   s+       dBW6W6W6W6W6Wr7   zGMissing shape inference function.

Please add declare this function in zD:

and implement it in the corresponding shape_inference.cpp file.

)
r9   readsetsplitOSErrorAssertionError	Exceptionoslinesepjoin)rE   rF   r@   shape_infr_declsemissing_declsrM   s         @r5   validate_shape_inference_headerrZ   u   sd   %&& 	F! vvxx$'(8(>(>t(D(D$E$E!	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F    `K^``
 
	   2  M  
$%8$ $ $ $
 
 	

 
s:   A! 7A	A! AA! AA! !
B+A>>Bc                     dS )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;
}
r/   r/   r7   r5   get_ltc_helper_fnsr\      s     r7   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<   dS )default_argsNoder%   	node_baseN
str | Nonenode_base_hdr&torch/csrc/lazy/core/shape_inference.hrE   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__r`   __annotations__rb   rE   rd   re   r   rg   r   ri   rk   r/   r7   r5   r^   r^      s         I $M$$$$GGGGG1L1111;;;;;)22222# %     &L%%%%%%r7   r^   c                    t          j        d          } |                     dddd           |                     dd	d
d           |                     ddt          dd           |                     ddt          d d           |                     dddd           |                     ddt          t
          j        d           |                     ddt          t
          j        d           |                     ddt          t
          j        d            |                     d!d"t          t
          j	        d#           |                     d$d%t          t
          j
        d&           |                     d'd(t          t
          j        d)           |                                 }t          t                                                    j        d*         }t	          |d+z  d,z  d-z            }t
          j        }|j        rt&          }t
          j        }t+          ||j        |j        |j        |j        |j        |j        |j	        |j
        |j        |||j                   d S ).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defaultrr   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)actionrr   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boolr%   r^   r`   rb   rE   rd   re   rk   
parse_argsr   __file__absoluteparentsrg   gen_ts_loweringsr   ri   run_gen_lazy_tensorsource_yaml
output_dirdry_run	impl_path)parseroptions
torch_root	aten_pathrg   ri   s         r5   mainr      s   $1UVVVF
P	     nnCUVVV
[tUAS     H     X	     &F     *J     0Q     )@     -D     ).     !!G h((**215JJ'%/&899I)5)G ('5 %  #(    r7   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::GetBackendDevicer   r   r   r   r~   r   ra   r`   rb   rd   re   rg   rf   ri   rh   build_in_treeper_operator_headersrk   gen_forced_fallback_codeuse_lazy_shapebackend_namespaceget_tensorlistget_tensor_or_wrap_numbertry_get_tensormetrics_countercreate_tensorcreate_from_first_tensorcreate_aten_from_ltc_tensortuple_aten_from_ltc_tensorslazy_value_classlazy_tensor_ptrget_device_fnc                  	./01234567 |                     d          }|d         }d                    |d d                   }t          t          ||                     t          j                            | d          7d 7fd}  |           }!t          j                            | d	          }"t          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 ./                                         }+|t'          |%./|(|+|1           	 	Bt)           0t+          j        ./                   2                    },t/          	|,           |+J t1          |!|+|).2/|(|           |(/gn/|(gD ]}-t3          |!|+.2/|-|*|||d           t5          |)          6|!                    / dd./026	fd            |
./         |||          4|!                    dd012346fd           |!                    dd456fd           d S )#Nz::	templatesinstall_dirr%   r(   r   c                (    t          |           S )N)r   template_dirr   )r   )r   r   r   s    r5   make_file_managerz.run_gen_lazy_tensor.<locals>.make_file_managerI  s!    #,
 
 
 	
r7   znative/native_functions.yamlznative/tags.yamlr@   %NativeFunctionsGroup | NativeFunctionc                    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funcr%   r4   )r@   r   s     r5   sort_native_functionz1run_gen_lazy_tensor.<locals>.sort_native_functionY  s7    
 %/q2F$G$GSq|  QV49>"""r7   )keyr   )Callable[[NativeFunction], Sequence[str]]xs/Iterable[NativeFunctionsGroup | NativeFunction]ops_listlist[OperatorName]Iterator[str]c              3     K   |D ]_}t          |t                    r!t          |                                          n|g}|D ]!}|j        j        |v r | |          E d{V  "`dS )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   r   r   xfsr@   s         r5   concat_map_codegenz/run_gen_lazy_tensor.<locals>.concat_map_codegenu  s        	' 	'A(216J(K(KTakkmm$$$RSQTB ' '6;(**#tAww&&&&&&&'	' 	'r7   F)r   r   rk   eager_registrationzNativeFunctions.cppzDispatchKeyNativeFunctions.cppc                    d dddddddd	d
dd d d dgrdgng z   D             t                      dj        j        t             d          
	                              dS )Nc                    g | ]}d | d	S z
#include <>r/   r3   paths     r5   r6   z9run_gen_lazy_tensor.<locals>.<lambda>.<locals>.<listcomp>  s3        %T$$$  r7   z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 NativeFunctions)includes
helper_fnsnative_functions_includenamespace_prologuenamespace_epiloguenative_function_definitions)r\   prologueepiloguer?   )r"   r   r   r   r   r   r   r   r   r   r   r&   r   r   ri   	ns_helperr   rE   rd   re   r   r   s   r5   <lambda>z%run_gen_lazy_tensor.<locals>.<lambda>  s     %'&5,L*&/7@42!BBKBBB!,,,& 0EFF)  2 -..(*"+"4"+"4+/""44&777'4$0)&1&'%033'% " -% , ,=4
 4
 r7   zLazyIr.hc            
         d dD             d dgng t            z                       j        j        dS )Nc                    g | ]}d | d	S r   r/   r   s     r5   r6   z9run_gen_lazy_tensor.<locals>.<lambda>.<locals>.<listcomp>  s3        %T$$$  r7   )zATen/core/Formatting.hzc10/core/ScalarType.hztorch/csrc/lazy/core/hash.htorch/csrc/lazy/core/ir.hr   optionalvectorz
#include "")lazy_ir_sysinclazy_ir_incir_declarationsr   r   )r?   r   r   )r   r#   r&   r-   lazy_ir_objrb   r   s   r5   r   z%run_gen_lazy_tensor.<locals>.<lambda>  s        ( :999::#""!9<&;P    
 #,"4"+"4-
 
 r7   zLazyNonNativeIr.hc                 t    d g drgng z   D             t          j                   j        j        dS )Nc                    g | ]
}|d | dS r   r/   r   s     r5   r6   z9run_gen_lazy_tensor.<locals>.<lambda>.<locals>.<listcomp>4  s;     
' 
' 
' 
'$T$$$
' 
' 
'r7   )r   r   z+torch/csrc/lazy/core/internal_ops/ltc_ops.hrc   )lazy_non_native_ir_incnon_native_ir_nodesr   r   )dest!generate_non_native_lazy_ir_nodesr   r   )r   rb   r,   r   s   r5   r   z%run_gen_lazy_tensor.<locals>.<lambda>3  sq    
' 
'   '4;M??=
' 
' 
' $(#IK$ $ #,"4"+"4!
 
 r7   )r   r%   r(   r   )r@   r   r(   r%   )r   r   r   r   r   r   r(   r   )rP   rV   r	   r
   rT   r   r   native_functionsr"   r   sortedr   r   r    r!   rD   r   get_nop_selectornative_function_class_namer   r?   r   GenLazyShapeInferenceDefinitionrZ   r   r   r   write_with_template)8r   r   r   r   r   r`   rb   rd   re   rE   rg   ri   r   r   rk   r   r   r   r   r   r   r   r   r   r   r   r   r   r   	lv_tokenslv_classlv_nsr   fmnative_yaml_pathtags_yaml_pathparsed_yamlr   r   parsed_backend_yamlr    r!   selector
class_namerF   dispatch_keyr"   r   r   r#   r&   r-   r   r,   r   r   s8     ``  ```` `   ` ````````` ``                 @@@@@@@@@@r5   r   r     s   L !&&t,,I}HIIin%%Ek%**+++7<<	;77L
 
 
 
 
 
 

 
	:	&	&Bw||I/MNNW\\)-?@@N#$4nEEK$# &o  <<LMM# # # #  & &:      --  &1K&3L'5M)9O (C-( ($L*f (4' ' ' ' '   022H""" -HHJJJ 	
 	
 	
 &$(4#K0,  )	 %
 %
! 	((;=VWWW!!! '
 	 	 	 &-K3N
 
 	%$'!5%$	
 	
 	
 	
 	
   ..I+++(4	
 4	
 4	
 4	
 4	
 4	
 4	
 4	
 4	
 4	
 4	
 4	
 4	
 4	
 4	
 4	
 4	
 4	
 4	
 4	
 4	
 4	
 4	
 4	
 4	
8 8 8t $#$lI~ K 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
  < 	
 	
 	
 	
 	
 	
 	
    r7   __main__)r$   r%   r&   r'   r(   r)   )rE   r%   rF   rG   r(   rH   )r(   r%   )r(   rH   )<r   r%   r   r%   r   r%   r   r~   r   ra   r`   r%   rb   ra   rd   r%   re   r%   rE   r%   rg   rf   ri   rh   r   r~   r   r~   rk   r%   r   r~   r   r~   r   r%   r   r%   r   r%   r   r%   r   r%   r   r%   r   r~   r   r%   r   r%   r   r%   r   r%   r   r%   r(   rH   )>
__future__r   r{   rT   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   rD   rZ   r\   r^   r   r`   rb   rd   re   rE   rg   ri   rk   r   rl   r/   r7   r5   <module>r     s   " " " " " "  				 " " " " " "       / / / / / / / / / /        ' ' ' ' ' ' * * * * * * U U U U U U U U U U H H H H H H H H            N M M M M M M M M M > > > > > > 7 7 7 7 7 7 7 7 * * * * * *  =<<<<<<<<<<~  ZWWW  < < < <&
 
 
 
6   @
& 
& 
& 
& 
& 
& 
& 
&] ] ] ]L "+ , :$1(9+?)5)G 	5 "'$1%* +)%K+<-%*'M'M0*8Ih h h h hV	 zDFFFFF r7   