a
    hX                      @  s   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ddddZ/ddddddZ0ddddZ1G d d! d!Z2ddd"d#Z3e2j4e2j5e2j6e2j7e2j8e2j9e2j:d$d$e2j;d$d%d&d'd(d)d*d+d$d,d-d.d/d0fdddd1d2dd2dddd3d4d1d1dd1d1ddddddd1ddddddd5d6d7Z<e=d8kre3  dS )9    )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strz/Sequence[NativeFunction | NativeFunctionsGroup]z8tuple[list[OperatorName], list[Any], list[OperatorName]])backend_yaml_pathgrouped_native_functionsreturnc           	      C  s   t | }tj|td}W d    n1 s,0    Y  t|tsDJ |dg }|dg }|dg }t|tsvJ t|tsJ t|tsJ dd |D }dd |D }|||fS )N)Loaderr"   
non_nativeir_genc                 S  s   g | ]}t |qS  r   parse.0namer*   r*   F/var/www/auris/lib/python3.9/site-packages/torchgen/gen_lazy_tensor.py
<listcomp>p       z/parse_native_functions_keys.<locals>.<listcomp>c                 S  s   g | ]}t |qS r*   r+   r-   r*   r*   r0   r1   q   r2   )openyamlloadr   
isinstancedictpoplist)	r$   r%   fZyaml_valuesr"   r(   r)   Zfull_codegen_opnamesZir_gen_opnamesr*   r*   r0   parse_native_functions_keysb   s    
,r;   z	list[str]None)shape_inference_hdrexpected_shape_infr_declsr&   c              
     s   zBt | &}| }t|d W d    n1 s60    Y  W n4 tyv } ztd|  |W Y d }~n
d }~0 0  fdd|D }|rtd|  dtj	| d S )N
z<Unable to read from the specified shape_inference_hdr file: c                   s   g | ]}| vr|qS r*   r*   )r.   declZshape_infr_decl_linesr*   r0   r1      s   z3validate_shape_inference_header.<locals>.<listcomp>zGMissing shape inference function.

Please add declare this function in zD:

and implement it in the corresponding shape_inference.cpp file.

)
r3   readsetsplitOSErrorAssertionError	Exceptionoslinesepjoin)r=   r>   r:   Zshape_infr_declseZmissing_declsr*   rA   r0   validate_shape_inference_headeru   s*    
0

rL   )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=*/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*   r*   r*   r0   get_ltc_helper_fns   s    rM   c                   @  sn   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.hr=   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_generatorZTorchScriptbackend_name)__name__
__module____qualname__rP   __annotations__rR   r=   rT   rU   r
   rW   r   rY   rZ   r*   r*   r*   r0   rN      s   

rN   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d* }t|d+ d, d- }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defaultr`   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)actionr`   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   ZatensrcZATen)argparseArgumentParseradd_argumentboolr#   rN   rP   rR   r=   rT   rU   rZ   
parse_argsr   __file__absoluteparentsrW   Zgen_ts_loweringsr   rY   run_gen_lazy_tensorsource_yaml
output_dirdry_run	impl_path)parseroptionsZ
torch_root	aten_pathrW   rY   r*   r*   r0   main   s    
rw   FTztorch::lazyZGetTensorListZ$GetLtcTensorOrCreateForWrappedNumberZTryGetLtcTensorzTORCH_LAZY_FN_COUNTER("lazy::")zLazyTensor::Createz$torch::lazy::CreateAtenFromLtcTensorz$torch::lazy::TupleAtenFromLtcTensorsztorch::lazy::ValueZLazyTensorPtrztorch::lazy::GetBackendDevicerj   rQ   rV   rX   )rv   rp   rq   rr   rs   rP   rR   rT   rU   r=   rW   rY   build_in_treeper_operator_headersrZ   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_fnr&   c           .        sv  | d}|d }d|d d }tt|| tj| ddddfdd} | }!tj| d	}"tj| d
}#t|"|#}$|$j|$j }% t	|%ddddd}&t
|&dt| }'|'j|'j}(|'j})|'j t|\fdddddddt }*d usJ    }+|d urBt|% |(|+| d urptt  },t|, |+d us~J 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::Z	templatesr#   r   )install_dirr&   c                   s   t |  dS )N)r   template_dirrr   )r   )r   )rr   r   r*   r0   make_file_managerI  s    z.run_gen_lazy_tensor.<locals>.make_file_managerznative/native_functions.yamlznative/tags.yamlz%NativeFunctionsGroup | NativeFunction)r:   r&   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.
        )r6   r   Z
functionalfuncr#   r/   )r:   r   r*   r*   r0   sort_native_functionY  s    z1run_gen_lazy_tensor.<locals>.sort_native_function)keyz)Callable[[NativeFunction], Sequence[str]]z/Iterable[NativeFunctionsGroup | NativeFunction]zlist[OperatorName]zIterator[str])r   xsops_listr&   c                 s  sN   |D ]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)r6   r   r9   Z	functionsr   r/   )r   r   r   xfsr:   r*   r*   r0   concat_map_codegenu  s
    
z/run_gen_lazy_tensor.<locals>.concat_map_codegenF)rx   ry   rZ   Zeager_registrationzNativeFunctions.cppzDispatchKeyNativeFunctions.cppc                     s   dd ddddddd	d
ddd d d dgrBdgng  D t  djjt d  
	dS )Nc                 S  s   g | ]}d | dqS z
#include <>r*   r.   pathr*   r*   r0   r1     s   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 ZNativeFunctions)ZincludesZ
helper_fnsZnative_functions_includenamespace_prologuenamespace_epilogueZnative_function_definitions)rM   prologueepiloguer9   r*   )r!   r   r|   r   r   r   r   rz   r   r~   r}   r%   r   r   rY   	ns_helperrq   r=   rT   rU   r   r   r*   r0   <lambda>  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  s   g | ]}d | dqS r   r*   r   r*   r*   r0   r1     s   r   )zATen/core/Formatting.hzc10/core/ScalarType.hztorch/csrc/lazy/core/hash.htorch/csrc/lazy/core/ir.hr   optionalZvectorz
#include "")Zlazy_ir_sysincZlazy_ir_incZir_declarationsr   r   )r9   r   r   r*   )r   r"   r%   r)   lazy_ir_objrR   r   r*   r0   r     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*   r0   r1   4  s   r   )r   r   z+torch/csrc/lazy/core/internal_ops/ltc_ops.hrS   )Zlazy_non_native_ir_incZnon_native_ir_nodesr   r   )destZ!generate_non_native_lazy_ir_nodesr   r   r*   )r   rR   r(   r   r*   r0   r   3  s    )rD   rJ   r   r	   rH   r   r   native_functionsr!   r   sortedr   r   r   r    r;   r   Zget_nop_selectorZnative_function_class_namer   r9   r   ZGenLazyShapeInferenceDefinitionrL   r   r   r   Zwrite_with_template).rv   rp   rq   rr   rs   rP   rR   rT   rU   r=   rW   rY   rx   ry   rZ   rz   r{   r|   r}   r~   r   r   r   r   r   r   r   r   r   Z	lv_tokensZlv_classZlv_nsr   ZfmZnative_yaml_pathZtags_yaml_pathZparsed_yamlr   r   Zparsed_backend_yamlr   r    selector
class_namer>   Zdispatch_keyr*   )r!   r   r|   r   r   r   r   rr   r"   rz   r   r~   r}   r%   r)   r   r   r   rY   rR   r(   r   rq   r=   r   rT   rU   r   r   r0   ro     s    &




	

4:ro   __main__)>
__future__r   rg   rH   collectionsr   pathlibr   typingr   r   r   r4   Ztorchgen.destr   Ztorchgen.api.lazyr   Ztorchgen.api.typesr	   Ztorchgen.dest.lazy_irr
   r   r   Ztorchgen.genr   r   Ztorchgen.gen_backend_stubsr   r   r   r   Ztorchgen.modelr   r   r   Z!torchgen.selective_build.selectorr   Ztorchgen.utilsr   r   Ztorchgen.yaml_utilsr   collections.abcr   r   r   r   r;   rL   rM   rN   rw   rP   rR   rT   rU   r=   rW   rY   rZ   ro   r[   r*   r*   r*   r0   <module>   sp   ? fJ  -
