o
    hZ                  =   @   s~  d dl Z d dlZd dlZd dlZd dlmZmZ d dlmZm	Z	m
Z
mZmZmZ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$ d d	l%m&Z& d d
l'm(Z(m)Z)m*Z* d dl+m,Z, ddl-m.Z.m/Z/m0Z0m1Z1 edg dZ2de3deee"e#f  deee$ ee ee$ f fddZ4de3dee3 ddfddZ5de3fddZ6G dd dZ7dKddZ8e7j9e7j:e7j;e7j<e7j=e7j>e7j?dde7j@dd d!d"d#d$d%d&dd'd(d)d*d+fd,e3d-e3d.e3d/eAd0ee3 d1e3d2ee3 d3e3d4e3de3d5ee d6ee d7eAd8eAd9e3d:eAd;eAd<e3d=e3d>e3d?e3d@e3dAe3dBeAdCe3dDe3dEe3dFe3dGe3ddf<dHdIZBeCdJkr=e8  dS dS )L    N)Counter
namedtuple)AnyCallableDictIterableIteratorListOptionalSequenceTupleTypeUnion)	setValueT)BaseCppType)	GenLazyIRGenLazyNativeFuncDefinitionGenTSLazyIR)get_grouped_native_functionsparse_native_yaml)NativeFunctionNativeFunctionsGroupOperatorName)SelectiveBuilder)	concatMapFileManagerNamespaceHelper)
YamlLoader   )error_on_missing_kernelsgen_dispatcher_registrations"gen_dispatchkey_nativefunc_headersparse_backend_yamlParsedExternalYaml)backend_keyautograd_keycpp_namespacebackend_indicesfull_codegenbackend_yaml_pathgrouped_native_functionsreturnc           
      C   s   dd t dd |D }t| }tj|td}W d    n1 s"w   Y  t|ts.J |dg }|dg }|dg }t|tsGJ t|tsNJ t|tsUJ d	d
 |D }dd
 |D }	|||	fS )Nc                 S   s   i | ]}|j j|qS  )funcname).0fr,   r,   N/var/www/html/ai/venv/lib/python3.10/site-packages/torchgen/gen_lazy_tensor.py
<dictcomp>n   s    z/parse_native_functions_keys.<locals>.<dictcomp>c                 S   s   t | tr| gS t|  S )N)
isinstancer   list	functions)r0   r,   r,   r1   <lambda>q   s    z-parse_native_functions_keys.<locals>.<lambda>)Loaderr(   
non_nativeir_genc                 S      g | ]}t |qS r,   r   parser/   r.   r,   r,   r1   
<listcomp>       z/parse_native_functions_keys.<locals>.<listcomp>c                 S   r:   r,   r;   r=   r,   r,   r1   r>      r?   )	r   openyamlloadr   r3   dictpopr4   )
r)   r*   native_functions_mapr0   yaml_valuesr(   r8   r9   full_codegen_opnamesir_gen_opnamesr,   r,   r1   parse_native_functions_keysj   s&   

rI   shape_inference_hdrexpected_shape_infr_declsc              
      s   z!t | }| }t|d W d    n1 sw   Y  W n ty5 } ztd|  |d }~ww d}tt||} fdd|D }|rYt	d|  dt
j| d S )N
z<Unable to read from the specified shape_inference_hdr file: zcompute_shape_(\w+)c                    s   g | ]}| vr|qS r,   r,   )r/   declshape_infr_decl_linesr,   r1   r>      s    z3validate_shape_inference_header.<locals>.<listcomp>zGMissing shape inference function.

Please add declare this function in zH:

and implement it in the the corresponding shape_inference.cpp file.

)r@   readsetsplitOSErrorAssertionErrorr   refindall	Exceptionoslinesepjoin)rJ   rK   r0   shape_infr_declseshape_infr_regexactual_shape_infr_name_countsmissing_declsr,   rN   r1   validate_shape_inference_header   s<   



r`   c                   C   s   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=*/c10::make_optional(tensor.scalar_type()), /*layout=*/c10::make_optional(tensor.layout()), /*device=*/c10::make_optional(c10::Device(c10::kMeta)), /*pin_memory=*/c10::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;
}
c10::optional<at::Tensor> to_meta(const c10::optional<at::Tensor>& tensor) {
  if (tensor.has_value()) {
    return to_meta(*tensor);
  }
  return c10::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,   r,   r,   r1   get_ltc_helper_fns   s   ra   c                   @   sz   e Zd ZU dZeed< dZee ed< dZeed< dZ	eed< d	Z
eed
< eZee ed< eZee ed< dZeed< dS )default_argsNode	node_baseNnode_base_hdr&torch/csrc/lazy/core/shape_inference.hrJ   ztorch::lazy::LazyTensortensor_classztorch/csrc/lazy/core/tensor.htensor_class_hdrlazy_ir_generator native_func_definition_generatorTorchScriptbackend_name)__name__
__module____qualname__rd   str__annotations__re   r
   rJ   rg   rh   r   ri   r   r   rj   rl   r,   r,   r,   r1   rb      s   
 rb   c                  C   sl  t jdd} | jddddd | jdd	d
dd | jddtddd | jddtd dd | jddddd | jddttjdd | jddttjdd | jddttjd d | jd!d"ttj	d#d | jd$d%ttj
d&d | jd'd(ttjd)d |  }ttjjj }t|d* d+ d, }tj}|jrt}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defaultrs   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)actionrs   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boolrp   rb   rd   re   rJ   rg   rh   rl   
parse_argspathlibPath__file__parentabsoluteri   gen_ts_loweringsr   rj   run_gen_lazy_tensorsource_yaml
output_dirdry_run	impl_path)parseroptions
torch_root	aten_pathri   rj   r,   r,   r1   main   s   
r   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   rd   re   rg   rh   ri   rj   build_in_treeper_operator_headersrl   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           .         s  | d}|d }d|d d }tt|| tj| ddtdtffdd} | }!tj| d}"tj| d	}#t|"|#}$|$j	|$j
}% t|%d
tttf dtfdd}&t|&dt| }'|'j|'j}(|'j})|'j
 t|\fdttgtt f dttttf  dtt dtt fddt }*d usJ    }+|d urt|% |(|+| 	 d urtt !  },t"|, |+d usJ t#|!|+|) |(| |(d u rgn|(gD ]}-t$|!|+ |-|*|||dd qt%|)|!& dd 	
fdd |
  ||||!&ddfdd |!&ddfdd d S )Nz::	templatesinstall_dirr+   c                    s   t |  dS )N)r   template_dirr   )r   )r   )r   r   r,   r1   make_file_manager]  s   z.run_gen_lazy_tensor.<locals>.make_file_managerznative/native_functions.yamlznative/tags.yamlr0   c                 S   s$   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.
        )r3   r   
functionalr-   rp   r.   )r0   r-   r,   r,   r1   sort_native_functionm  s   z1run_gen_lazy_tensor.<locals>.sort_native_function)keyr-   xsops_listc                 s   sP    |D ]"}t |trt| n|g}|D ]}|jj|v r$| |E dH  qq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)r3   r   r4   r5   r-   r.   )r-   r   r   xfsr0   r,   r,   r1   concat_map_codegen  s   
z/run_gen_lazy_tensor.<locals>.concat_map_codegenF)r   r   rl   eager_registrationzNativeFunctions.cppzDispatchKeyNativeFunctions.cppc                      s   dd ddddddd	d
ddd d d dgr!dgng  D t  djjt d  
	dS )Nc                 S      g | ]}d | dqS z
#include <>r,   r/   pathr,   r,   r1   r>         
9run_gen_lazy_tensor.<locals>.<lambda>.<locals>.<listcomp>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)ra   prologueepiloguer4   r,   )r'   r$   r   r   r   r   r   r   r   r   r   r*   r   r   rj   	ns_helperr   rJ   rg   rh   r   r   r,   r1   r6     sb   z%run_gen_lazy_tensor.<locals>.<lambda>zLazyIr.hc                      sD   dd dD d urd dgng t   jjdS )Nc                 S   r   r   r,   r   r,   r,   r1   r>   *  r   r   )zATen/core/Formatting.hzc10/core/ScalarType.hzc10/util/Optional.hztorch/csrc/lazy/core/hash.htorch/csrc/lazy/core/ir.hr   vectorz
#include "")lazy_ir_sysinclazy_ir_incir_declarationsr   r   )r4   r   r   r,   )r   r(   r*   r9   lazy_ir_objre   r   r,   r1   r6   )  s   
zLazyNonNativeIr.hc                      s6   dd g drgng  D t  jjdS )Nc                 S   s   g | ]
}|rd | dqS r   r,   r   r,   r,   r1   r>   H  s    
r   )r   r   z+torch/csrc/lazy/core/internal_ops/ltc_ops.hrf   )lazy_non_native_ir_incnon_native_ir_nodesr   r   )dest!generate_non_native_lazy_ir_nodesr   r   r,   )r   re   r8   r   r,   r1   r6   G  s   )'rR   rZ   r   r   rX   r   rp   r   r   native_functionsr'   r   r   r   r   sortedr"   r$   r%   r&   rI   r   r   r   r	   r   r   r   get_nop_selectornative_function_class_namer   r4   r   GenLazyShapeInferenceDefinitionr`   r!   r    r   write_with_template).r   r   r   r   r   rd   re   rg   rh   rJ   ri   rj   r   r   rl   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_namerK   dispatch_keyr,   )r'   r$   r   r   r   r   r   r   r(   r   r   r   r   r*   r9   r   r   r   rj   re   r8   r   r   rJ   r   rg   rh   r   r   r1   r   1  s   
&




	4:r   __main__)r+   N)Dr{   rX   r   rU   collectionsr   r   typingr   r   r   r   r   r	   r
   r   r   r   r   rA   torchgen.destr   torchgen.api.lazyr   torchgen.api.typesr   torchgen.dest.lazy_irr   r   r   torchgen.genr   r   torchgen.modelr   r   r   !torchgen.selective_build.selectorr   torchgen.utilsr   r   r   torchgen.yaml_utilsr   gen_backend_stubsr   r    r!   r"   r#   rp   rI   r`   ra   rb   r   rd   re   rg   rh   rJ   ri   rj   rl   r~   r   rm   r,   r,   r,   r1   <module>   s   4C

 
f	
 !"#$%
  
-
