
    ΧgX                       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,dPdZ-dQdZ.dRdZ/ G d d          Z0dSd Z1e0j2        e0j3        e0j4        e0j5        e0j6        e0j7        e0j8        d!d!e0j9        d!d"d#d$d%d&d'd(d!d)d*d+d,d-fdTdNZ:e;dOk    r e1             dS dS )U    )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_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     T/var/www/html/ai-engine/env/lib/python3.11/site-packages/torchgen/gen_lazy_tensor.py
<listcomp>z/parse_native_functions_keys.<locals>.<listcomp>l   s#    NNNL.t44NNN    c                6    g | ]}t          j        |          S r.   r/   r1   s     r4   r5   z/parse_native_functions_keys.<locals>.<listcomp>m   s#    BBB4l(..BBBr6   )openyamlloadr   
isinstancedictpoplist)	r#   r%   fyaml_valuesr"   r+   r,   full_codegen_opnamesir_gen_opnamess	            r4   parse_native_functions_keysrC   ^   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.   )r2   declshape_infr_decl_liness     r4   r5   z3validate_shape_inference_header.<locals>.<listcomp>   s+       dBW6W6W6W6W6Wr6   zGMissing shape inference function.

Please add declare this function in zD:

and implement it in the corresponding shape_inference.cpp file.

)
r8   readsetsplitOSErrorAssertionError	Exceptionoslinesepjoin)rD   rE   r?   shape_infr_declsemissing_declsrL   s         @r4   validate_shape_inference_headerrY   q   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=*/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;
}
r.   r.   r6   r4   get_ltc_helper_fnsr[      s     r6   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.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__r_   __annotations__ra   rD   rc   rd   r   rf   r   rh   rj   r.   r6   r4   r]   r]      s         I $M$$$$GGGGG1L1111;;;;;)22222 	$ % $ $ $ $ &L%%%%%%r6   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        j        j                                        }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defaultrq   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)actionrq   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_   ra   rD   rc   rd   rj   
parse_argsr   __file__parentabsoluterf   gen_ts_loweringsr   rh   run_gen_lazy_tensorsource_yaml
output_dirdry_run	impl_path)parseroptions
torch_root	aten_pathrf   rh   s         r4   mainr      s   $1UVVVF
P	     nnCUVVV
[tUAS     H     X	     &F     *J     0Q     )@     -D     ).     !!G h&-4==??JJ'%/&899I)5)G (' 	5 %  #(    r6   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   r`   r_   ra   rc   rd   rf   re   rh   rg   build_in_treeper_operator_headersrj   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    r4   make_file_managerz.run_gen_lazy_tensor.<locals>.make_file_managerE  s!    #,
 
 
 	
r6   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$   r3   )r?   r   s     r4   sort_native_functionz1run_gen_lazy_tensor.<locals>.sort_native_functionU  s7    
 %/q2F$G$GSq|  QV49>"""r6   )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   r3   )r   r   r   xfsr?   s         r4   concat_map_codegenz/run_gen_lazy_tensor.<locals>.concat_map_codegenq  s        	' 	'A(216J(K(KTakkmm$$$RSQTB ' '6;(**#tAww&&&&&&&'	' 	'r6   F)r   r   rj   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.   r2   paths     r4   r5   z9run_gen_lazy_tensor.<locals>.<lambda>.<locals>.<listcomp>  s3        %T$$$  r6   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   rh   	ns_helperr   rD   rc   rd   r   r   s   r4   <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
 r6   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     r4   r5   z9run_gen_lazy_tensor.<locals>.<lambda>.<locals>.<listcomp>  s3        %T$$$  r6   )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_objra   r   s   r4   r   z%run_gen_lazy_tensor.<locals>.<lambda>  s        ( :999::#""!9<&;P    
 #,"4"+"4-
 
 r6   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     r4   r5   z9run_gen_lazy_tensor.<locals>.<lambda>.<locals>.<listcomp>0  s;     
' 
' 
' 
'$T$$$
' 
' 
'r6   )r   r   z+torch/csrc/lazy/core/internal_ops/ltc_ops.hrb   )lazy_non_native_ir_incnon_native_ir_nodesr   r   )dest!generate_non_native_lazy_ir_nodesr   r   )r   ra   r+   r   s   r4   r   z%run_gen_lazy_tensor.<locals>.<lambda>/  sq    
' 
'   '4;M??=
' 
' 
' $(#IK$ $ #,"4"+"4!
 
 r6   )r   r$   r'   r   )r?   r   r'   r$   )r   r   r   r   r   r   r'   r   )rO   rU   r   r   rS   r   r   native_functionsr!   r   sortedr   r   r   r    rC   r   get_nop_selectornative_function_class_namer   r>   r   GenLazyShapeInferenceDefinitionrY   r   r   r   write_with_template)8r   r   r   r   r   r_   ra   rc   rd   rD   rf   rh   r   r   rj   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_namerE   dispatch_keyr!   r   r   r"   r%   r,   r   r+   r   r   s8     ``  ```` `   ` ````````` ``                 @@@@@@@@@@r4   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 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
  < 	
 	
 	
 	
 	
 	
 	
    r6   __main__)r#   r$   r%   r&   r'   r(   )rD   r$   rE   rF   r'   rG   )r'   r$   )r'   rG   )<r   r$   r   r$   r   r$   r   r|   r   r`   r_   r$   ra   r`   rc   r$   rd   r$   rD   r$   rf   re   rh   rg   r   r|   r   r|   rj   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'   rG   )<
__future__r   ry   rS   collectionsr   pathlibr   typingr   r   r   r	   r
   r9   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   rC   rY   r[   r]   r   r_   ra   rc   rd   rD   rf   rh   rj   r   rk   r.   r6   r4   <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 r6   