U
    •Ê¹jÈZ  ã                    @   sB  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dddddgƒZ2e3eee"e#f  eee$ ee ee$ f dœdd„Z4e3ee3 ddœdd„Z5e3dœdd„Z6G dd„ dƒZ7ddœd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e3e3e3eAee3 e3ee3 e3e3e3ee ee eAeAe3eAeAe3e3e3e3e3e3eAe3e3e3e3e3dd.œd/d0„ZBeCd1k�r>e8ƒ  dS )2é    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 5 Q R X t|tƒsHt‚| dg ¡}| dg ¡}| dg ¡}t|t	ƒszt‚t|t	ƒsˆt‚t|t	ƒs–t‚d	d
„ |D ƒ}dd
„ |D ƒ}	|||	fS )Nc                 S   s   i | ]}|j j|“qS © )ÚfuncÚname)Ú.0Úfr,   r,   úW/var/www/html/bwave/python/venv/lib/python3.8/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   ó    z-parse_native_functions_keys.<locals>.<lambda>)ÚLoaderr(   Ú
non_nativeÚir_genc                 S   s   g | ]}t  |¡‘qS r,   ©r   Úparse©r/   r.   r,   r,   r1   Ú
<listcomp>€   s     z/parse_native_functions_keys.<locals>.<listcomp>c                 S   s   g | ]}t  |¡‘qS r,   r;   r=   r,   r,   r1   r>   �   s     )
r   ÚopenÚyamlÚloadr   r3   ÚdictÚAssertionErrorÚpopr4   )
r)   r*   Znative_functions_mapr0   Zyaml_valuesr(   r9   r:   Zfull_codegen_opnamesZir_gen_opnamesr,   r,   r1   Úparse_native_functions_keysj   s$    þþ
rE   )Úshape_inference_hdrÚexpected_shape_infr_declsr+   c              
      s¬   z.t | ƒ�}| ¡ }t| d¡ƒ‰ W 5 Q R X W n2 tk
r` } ztd| › �ƒ|‚W 5 d }~X Y nX d}tt ||¡ƒ}‡ fdd„|D ƒ}|r¨t	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 | ]}|ˆ kr|‘qS r,   r,   )r/   Údecl©Z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ÚOSErrorrC   r   ÚreÚfindallÚ	ExceptionÚosÚlinesepÚjoin)rF   rG   r0   Zshape_infr_declsÚeZshape_infr_regexZactual_shape_infr_name_countsZmissing_declsr,   rJ   r1   Úvalidate_shape_inference_header…   s2    
ÿþ
ÿ
ÿÿ
ýÿrV   )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    rW   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.hrF   ztorch::lazy::LazyTensorÚtensor_classztorch/csrc/lazy/core/tensor.hÚtensor_class_hdrÚlazy_ir_generatorÚ native_func_definition_generatorZTorchScriptÚbackend_name)Ú__name__Ú
__module__Ú__qualname__rZ   ÚstrÚ__annotations__r[   r
   rF   r]   r^   r   r_   r   r   r`   ra   r,   r,   r,   r1   rX   Ä   s   
þÿrX   c                  C   sn  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�r0t}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Údefaultrh   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)Úactionrh   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 generateZatenÚsrcZATen)ÚargparseÚArgumentParserÚadd_argumentÚboolre   rX   rZ   r[   rF   r]   r^   ra   Ú
parse_argsÚpathlibÚPathÚ__file__ÚparentÚabsoluter_   Zgen_ts_loweringsr   r`   Úrun_gen_lazy_tensorÚsource_yamlÚ
output_dirÚdry_runÚ	impl_path)ÚparserÚoptionsZ
torch_rootÚ	aten_pathr_   r`   r,   r,   r1   ÚmainÑ   sº    ü    ÿûüûûûûûûþór€   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::GetBackendDevice)r   ry   rz   r{   r|   rZ   r[   r]   r^   rF   r_   r`   Úbuild_in_treeÚper_operator_headersra   Ú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           .         s   |  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|ˆƒ\‰‰‰ˆfttgtt f ttttf  tt tt dœdd„‰t ¡ }*ˆd k	�sBt‚ˆ ˆ  ¡ }+|d k	�rlt|%ˆ ˆ|(|+|ˆƒ ˆd k	�ršt ˆt! "ˆ ˆ ˆ¡ˆƒƒ},t#ˆ|,ƒ |+d k	�s¨t‚t$|!|+|)ˆ ˆˆ|(|ƒ |(d k�rÎˆgnˆ|(gD ]&}-t%|!ˆ|+ˆ ˆˆ|-|*|||dd� �qÖt&|)ƒ‰|! 'ˆ› d�d‡ ‡‡‡‡‡‡‡	‡
‡‡‡‡‡‡‡‡‡‡‡‡‡fdd„¡ |
ˆ ˆ |||ƒ‰|! '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_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.yaml)r0   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.
        )r3   r   Z
functionalr-   re   r.   )r0   r-   r,   r,   r1   Úsort_native_functionm  s    z1run_gen_lazy_tensor.<locals>.sort_native_function)Úkey)r-   ÚxsÚops_listr+   c                 s   sN   |D ]D}t |tƒrt| ¡ ƒn|g}|D ]}|jj|k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‚   ra   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,   r1   r>   ì  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)rW   ÚprologueÚepiloguer4   r,   )r'   r$   r…   r›   rŒ   r‹   rŠ   rƒ   r�   r‡   r†   r*   r�   r‰   r`   Ú	ns_helperrz   rF   r]   r^   rˆ   r�   r,   r1   r6   ë  sb    ñÿìþñîÿâz%run_gen_lazy_tensor.<locals>.<lambda>zLazyIr.hc                      sD   dd„ dD ƒˆd k	r"dˆ› d�gng t ˆ ˆˆˆˆ ƒƒˆjˆjdœS )Nc                 S   s   g | ]}d |› d�‘qS rœ   r,   rž   r,   r,   r1   r>   *  s   ÿ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¢   Zvectorz
#include "ú")Zlazy_ir_sysincZlazy_ir_incZir_declarationsr¥   r¦   )r4   r§   r¨   r,   )r›   r(   r*   r:   Úlazy_ir_objr[   r©   r,   r1   r6   )  s     þÿ  ÿÿêzLazyNonNativeIr.hc                      s:   dd„ ddddgˆ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.hr\   )Zlazy_non_native_ir_incZnon_native_ir_nodesr¥   r¦   )ÚdestZ!generate_non_native_lazy_ir_nodesr§   r¨   r,   )r¬   r[   r9   r©   r,   r1   r6   G  s     üúþ ÿð)(rM   rT   r   r   rR   rŸ   re   r   r   Únative_functionsr'   r   r   r   r   Úsortedr"   r$   r%   r&   rE   r   r   r   r	   r   r   r   Zget_nop_selectorrC   Znative_function_class_namer   r4   r­   ZGenLazyShapeInferenceDefinitionrV   r!   r    r   Zwrite_with_template).r   ry   rz   r{   r|   rZ   r[   r]   r^   rF   r_   r`   r�   r‚   ra   rƒ   r„   r…   r†   r‡   rˆ   r‰   rŠ   r‹   rŒ   r�   rŽ   r�   r�   Z	lv_tokensZlv_classZlv_nsr”   ÚfmZnative_yaml_pathZtags_yaml_pathZparsed_yamlr®   r•   Zparsed_backend_yamlr%   r&   ÚselectorÚ
class_namerG   Zdispatch_keyr,   )r'   r$   r…   r›   rŒ   r‹   rŠ   r{   r(   rƒ   r�   r‡   r†   r*   r:   r¬   r�   r‰   r`   r[   r9   r©   rz   rF   r“   r]   r^   rˆ   r�   r1   rx   1  sà    &

þ ÿ  ÿ ÿ
ýü
ù
 ÿüÿ	
øÿô
4ý:   ÿýýrx   Ú__main__)Drn   rR   rs   rO   Úcollectionsr   r   Útypingr   r   r   r   r   r	   r
   r   r   r   r   r@   Ztorchgen.destr­   Ztorchgen.api.lazyr   Ztorchgen.api.typesr   Ztorchgen.dest.lazy_irr   r   r   Ztorchgen.genr   r   Ztorchgen.modelr   r   r   Z!torchgen.selective_build.selectorr   Ztorchgen.utilsr   r   r   Ztorchgen.yaml_utilsr   Zgen_backend_stubsr   r    r!   r"   r#   re   rE   rV   rW   rX   r€   rZ   r[   r]   r^   rF   r_   r`   ra   rq   rx   rb   r,   r,   r,   r1   Ú<module>   sº   4Cþý þ fÜÿÛ  -
