U
    dL                     @   s   d dl Z 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 d dl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%m&Z& d dl'm(Z( ddl)m*Z*m+Z+m,Z,m-Z- eddddddgZ.e/eeeef  e	e  dddZ0e/e	e/ ddddZ1G dd dZ2ddddZ3e2j4e2j5e2j6e2j7e2j8e2j9dde2j:ddd d!d"d#d$dd%d&d'd(d)fe/e/e/e;ee/ e/ee/ e/e/e/ee e;e;e/e;e/e/e/e/e/e/e;e/e/e/e/e/dd*d+d,Z<e=d-kre3  dS ).    N)
namedtupleCounter)	ListDictUnionSequenceOptionalCallableIterableIteratorType)BaseCppType)	GenLazyIRGenTSLazyIR)get_grouped_native_functionsparse_native_yamlNamespaceHelper)	setValueT)NativeFunctionNativeFunctionsGroupOperatorName)SelectiveBuilder)	concatMap
YamlLoaderFileManager   )parse_backend_yamlerror_on_missing_kernels"gen_dispatchkey_nativefunc_headersgen_dispatcher_registrations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| d}tj|td}W 5 Q R X t|tsJt|dg }t|t	sntd| d	d
 |D }|S )Nc                 S   s   i | ]}|j j|qS  )funcname).0fr)   r)   </tmp/pip-unpacked-wheel-ua33x9lu/torchgen/gen_lazy_tensor.py
<dictcomp>r   s    z*parse_full_codegen_ops.<locals>.<dictcomp>c                 S   s   t | tr| gS t|  S )N)
isinstancer   list	functions)r-   r)   r)   r.   <lambda>u       z(parse_full_codegen_ops.<locals>.<lambda>r)Loaderr%   z/expected "full_codegen" to be a list, but got: c                 S   s   g | ]}t |qS r)   )r   parse)r,   r+   r)   r)   r.   
<listcomp>   s     z*parse_full_codegen_ops.<locals>.<listcomp>)
r   openyamlloadr   r0   dictAssertionErrorpopr1   )r&   r'   Znative_functions_mapr-   Zyaml_valuesr%   r)   r)   r.   parse_full_codegen_opsm   s$     r?   )shape_inference_hdrexpected_shape_infr_declsr(   c              	   C   s   z0t | d}| }t|d}W 5 Q R X W n" tk
rR   td|  Y nX d}tt||}|D ] }||ksltd|  d| qld S )Nr5   
z<Unable to read from the specified shape_inference_hdr file: zcompute_shape_(\w+)zGMissing shape inference function.

Please add declare this function in zH:

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

)	r9   readsetsplitIOErrorr=   r   refindall)r@   rA   r-   Zshape_infr_declsZshape_infr_decl_linesZshape_infr_regexZactual_shape_infr_name_countsdeclr)   r)   r.   validate_shape_inference_header   s*    

rJ   c                   @   sj   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< dZeed< dS )default_argsNode	node_baseNnode_base_hdrz&torch/csrc/lazy/core/shape_inference.hr@   ztorch::lazy::LazyTensortensor_classztorch/csrc/lazy/core/tensor.htensor_class_hdrlazy_ir_generatorZTorchScriptbackend_name)__name__
__module____qualname__rM   str__annotations__rN   r   r@   rO   rP   r   rQ   r   rR   r)   r)   r)   r.   rK      s   
rK   )r(   c                  C   sP  t jdd} | jdddd | jddd	d | jd
tdd	d | jdtd dd | jdddd | jdttjdd | jdttjdd | jdttjdd | jdttj	dd | jdttj
dd | jdttjdd |  }ttjjj }t|d d  d! }tj}|jrt}t||j|j|j|j|j|j|j	|j
|j||j d S )"Nz"Generate Lazy Tensor backend files)descriptionz-sz--source_yamlzApath to source yaml file containing operator external definitions)helpz-oz--output_dirzoutput directoryz	--dry_runF)typedefaultrY   z--impl_pathz9path to the source C++ file containing kernel definitionsz--gen_ts_lowerings
store_truezIGenerate TorchScript lowerings in addition to Lazy IR and NativeFunctions)actionrY   z--node_basez7Name of backend specific custom Lazy IR Node base classz--node_base_hdrz;Path to header file defining custom Lazy IR Node base classz--shape_inference_hdrzBPath to header file defining custom Lazy shape inference functionsz--tensor_classz1Name of backend specific custom Lazy Tensor classz--tensor_class_hdrz5Path to header file defining custom Lazy Tensor classz--backend_namezName of the backend to generateZatensrcZATen)argparseArgumentParseradd_argumentboolrV   rK   rM   rN   r@   rO   rP   rR   
parse_argspathlibPath__file__parentabsoluterQ   Zgen_ts_loweringsr   run_gen_lazy_tensorsource_yaml
output_dirdry_run	impl_path)parseroptionsZ
torch_root	aten_pathrQ   r)   r)   r.   main   s    rq   Fz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::GetBackendDevice)rp   rj   rk   rl   rm   rM   rN   rO   rP   r@   rQ   build_in_treeper_operator_headersrR   gen_forced_fallback_code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           ,         sh  | d}|d }d|d d }tt|| tj| dttdfdd}|}tj| d} tj| d}!t| |!}"|"j	|"j
 }# t|#tttf td	d
d}$t|$dt| }%|%j|%j}&|%j}'|%j
 t|ttgtt f ttttf  tt dfddt }(d k	s6t   })|d k	r`t|# |&|)| d k	rtt   }*t!|* |)d k	stt"||)|' |&| |&d krgn|&gD ](}+t#||)|' |+|(|||dd qt$|'|% dd 	
fdd |%dd fdd d S )Nz::Z	templates)install_dirr(   c                    s   t |  dS )N)r   template_dirrl   )r   )r   )rl   r   r)   r.   make_file_manager%  s
      z.run_gen_lazy_tensor.<locals>.make_file_managerznative/native_functions.yamlznative/tags.yaml)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.
        )r0   r   Z
functionalr*   rV   r+   )r-   r*   r)   r)   r.   sort_native_function5  s    z1run_gen_lazy_tensor.<locals>.sort_native_function)key)r*   xsr(   c                 3   sT   |D ]J}t |trt| n|g}|D ]$}|jj kr(| |D ]
}|V  q@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)r0   r   r1   r2   r*   r+   )r*   r   xfsr-   r5   )r%   r)   r.   concat_map_codegenI  s    	z/run_gen_lazy_tensor.<locals>.concat_map_codegenF)rr   rs   rR   Zeager_registrationzNativeFunctions.cppzDispatchKeyNativeFunctions.cppc                      s   dd ddddddd	d
 d d dgr<dgng  D dj jtt d  
	dS )Nc                 S   s   g | ]}d | dqS z
#include <>r)   r,   pathr)   r)   r.   r8     s   9run_gen_lazy_tensor.<locals>.<lambda>.<locals>.<listcomp>zATen/Functions.hzATen/MetaFunctions.hzATen/Operators.hzATen/native/CPUFallback.hz!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native_functions_includenamespace_prologuenamespace_epilogueZnative_function_definitions)prologueepiloguer1   destZGenLazyNativeFuncDefinitionr)   )r$   r!   ru   r   r|   r{   rz   rt   r   rw   rv   r'   r   ry   	ns_helperrk   r@   rO   rP   rx   r}   r)   r.   r3     sZ    z%run_gen_lazy_tensor.<locals>.<lambda>zLazyIr.hc                      sJ   dd dD dd d k	rnd fD t   jjdS )Nc                 S   s   g | ]}d | dqS r   r)   r   r)   r)   r.   r8     s   r   )zATen/core/Formatting.hzc10/core/ScalarType.hzc10/util/Optional.hztorch/csrc/lazy/core/hash.hztorch/csrc/lazy/core/ir.hr   Zvectorc                 S   s    g | ]}|d k	rd| dqS )Nz
#include ""r)   r   r)   r)   r.   r8     s   )Zlazy_ir_sysincZlazy_ir_incZir_declarationsr   r   )r1   r   r   r)   )r$   r!   r   r'   rQ   rM   rN   r   r)   r.   r3     s    )&rE   joinr   r   osr   rV   r   r   native_functionsr$   r   r   r   r   sortedr   r!   r"   r#   r?   r	   r   r
   r   r   Zget_nop_selectorr=   Znative_function_class_namer   r1   r   ZGenLazyShapeInferenceDefinitionrJ   r   r   r   Zwrite_with_template),rp   rj   rk   rl   rm   rM   rN   rO   rP   r@   rQ   rr   rs   rR   rt   ru   rv   rw   rx   ry   rz   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_namerA   Zdispatch_keyr)   )r$   r!   ru   r   r|   r{   rz   rl   r%   rt   r   rw   rv   r'   rQ   r   ry   rM   rN   r   rk   r@   r   rO   rP   rx   r}   r.   ri      s    "

   


 	

26ri   __main__)>rd   r_   r   rG   r:   collectionsr   r   typingr   r   r   r   r   r	   r
   r   r   Ztorchgen.api.typesr   Ztorchgen.dest.lazy_irr   r   Ztorchgen.genr   r   r   Ztorchgen.api.lazyr   Ztorchgen.modelr   r   r   Z!torchgen.selective_build.selectorr   Ztorchgen.utilsr   r   r   Ztorchgen.destr   Zgen_backend_stubsr   r   r   r   r    rV   r?   rJ   rK   rq   rM   rN   rO   rP   r@   rQ   rR   rb   ri   rS   r)   r)   r)   r.   <module>   s   ,C 
W  
