U
    <c                     @   s  d dl Z d dlZd dlZd dlmZmZm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 edeeef d	Zee eee  d
ddZeddddZee eddddZee eddddZddddZedkredd e  dS )    N)SequenceTypeVarUnion)set_simple_logging)gen)native_function_manager)DispatchKeyNativeFunctionsGroupNativeFunctionsViewGroup)config	generatorNativeGroupT)bound)grouped_native_functionsreturnc                    sV   | sg S g }t ttf tddd  fdd| D }dd tj|dd	 d
D }|S )N)gr   c              
   S   s*   t |  t| W  5 Q R  S Q R X d S N)r   r   is_supportedr    r   R/tmp/pip-unpacked-wheel-gikjz4vx/torchgen/static_runtime/gen_static_runtime_ops.pyr      s    
z0group_functions_by_op_name.<locals>.is_supportedc                 3   s   | ]} |r|V  qd S r   r   .0r   r   r   r   	<genexpr>"   s      z-group_functions_by_op_name.<locals>.<genexpr>c                 S   s   g | ]\}}t |qS r   )list)r   kgroupr   r   r   
<listcomp>#   s   z.group_functions_by_op_name.<locals>.<listcomp>c                 S   s
   t | S r   )r   Zfunc_name_base_strr   r   r   r   <lambda>(       z,group_functions_by_op_name.<locals>.<lambda>)key)r   r	   r
   bool	itertoolsgroupby)r   groupsZeligible_opsr   r   r   group_functions_by_op_name   s    
r&   )cpp_file_pathr   c                 C   s   dd l }|dd| g d S )Nr   zclang-formatz-i)
subprocessrun)r'   r(   r   r   r   clang_format0   s    r*   )cpp_ops	file_pathr   c              	   C   sB   d | }d| d}t|d}|| W 5 Q R X t| d S )N
a  // @lint-ignore-every CLANGTIDY HOWTOEVEN
// AUTO-GENERATED FROM: torchgen/static_runtime/gen_static_runtime_ops.py
#include <torch/csrc/jit/runtime/static/ops.h>

#include <ATen/CPUFunctions.h>
#include <ATen/InferSize.h>
#include <ATen/NativeFunctions.h>
#include <ATen/Parallel.h>
#include <ATen/ScalarOps.h>
#include <ATen/TensorUtils.h>
#include <ATen/cpu/vec/functional.h>
#include <ATen/cpu/vec/vec.h>
#include <ATen/native/EmbeddingBag.h>
#include <ATen/native/Fill.h>
#include <ATen/native/IndexingUtils.h>
#include <ATen/native/Resize.h>
#include <ATen/native/SharedReduceOps.h>
#include <ATen/native/TensorAdvancedIndexing.h>
#include <ATen/native/cpu/SerialStackImpl.h>
#include <ATen/native/layer_norm.h>
#include <ATen/native/quantized/cpu/fbgemm_utils.h>
#include <ATen/native/quantized/cpu/qembeddingbag.h>
#include <ATen/native/quantized/cpu/qembeddingbag_prepack.h>
#include <ATen/quantized/QTensorImpl.h>
#include <ATen/quantized/Quantizer.h>
#include <c10/core/ScalarType.h>
#include <c10/core/WrapDimMinimal.h>
#include <c10/util/irange.h>
#include <torch/csrc/jit/ir/ir.h>
#include <torch/csrc/jit/runtime/static/impl.h>
#include <torch/csrc/jit/runtime/static/te_wrapper.h>
#include <torch/csrc/jit/runtime/vararg_functions.h>
#include <torch/csrc/jit/tensorexpr/ir.h>
#include <torch/csrc/jit/tensorexpr/ir_simplifier.h>
#include <torch/csrc/jit/tensorexpr/llvm_codegen.h>
#include <torch/csrc/jit/tensorexpr/loopnest.h>

namespace torch {
namespace jit {

z*

} // namespace jit
} // namespace torch
wjoinopenwriter*   r+   r,   code	generatedfr   r   r   	write_cpp6   s    
(-r7   c              	   C   sB   d | }d| d}t|d}|| W 5 Q R X t| d S )Nr-   aq  // @lint-ignore-every CLANGTIDY HOWTOEVEN
// AUTO-GENERATED FROM: torchgen/static_runtime/gen_static_runtime_ops.py
#include <gtest/gtest.h>
#include <torch/csrc/jit/runtime/static/impl.h>
#include <torch/torch.h>

#include "test_utils.h"

using namespace caffe2;
using namespace torch;
using namespace torch::jit;
using namespace torch::jit::test;
using c10::IValue;



r.   r/   r3   r   r   r   write_test_cppj   s    
r9   )r   c                     s  t jdd} | jddddd | jdd	d
dd | jddd
dd |  }tj|jd}tj|jd}t	||}|j
|j } t t dd t|D }t|} fdd|D }fdd|D }	dd t|D }
t|
} fdd|D }fdd|D }|dg | }|	dg | }t||j t||j tdtt|  tdt|  tdd |D }td|  tdt|
  tdd |D }td|  td ||   d S )!NzGenerate ATen source files)descriptionz-sz--source-pathz!path to source directory for ATenzcaffe2/aten/src/ATen)helpdefaultz-pz--generated-ops-cpp-pathz5path to directory to generate op dispatcher .cpp filez6caffe2/torch/csrc/jit/runtime/static/generated_ops.cppz-tz--generated-ops-test-cpp-pathz6caffe2/benchmarks/static_runtime/test_generated_ops.ccznative/native_functions.yamlznative/tags.yamlc                 S   s   g | ]}t |tr|qS r   )
isinstancer	   r   r   r   r   r      s   
zmain.<locals>.<listcomp>c                    s   g | ]} | tj qS r   )out_variantr   CPUr   r%   backend_indicesop_generatorr   r   r      s   c                    s   g | ]}  |qS r   )r>   r@   test_case_generatorr   r   r      s    c                 S   s   g | ]}t |tr|qS r   )r=   r
   r   r   r   r   r      s   
c                    s   g | ]} | tj qS r   )viewr   r?   r@   rA   r   r   r      s   c                    s   g | ]}  |qS r   )rF   r@   rD   r   r   r      s    r8   z
total grouped native ops: %dz'grouped native ops with out variant: %dc                 S   s   g | ]}t |qS r   lenr@   r   r   r   r      s     z/generated functions groups with out variant: %dz
view grouped native ops: %dc                 S   s   g | ]}t |qS r   rG   r@   r   r   r   r      s     z#generated functions view groups: %dz
overall generated : %d)argparseArgumentParseradd_argument
parse_argsospathr0   source_pathr   Zparse_native_yamlnative_functionsrB   r   ZGenOpDispatcherZGenOpTestCaseZget_grouped_native_functionsr&   Z$get_grouped_by_view_native_functionsr7   Zgenerated_ops_cpp_pathr9   Zgenerated_ops_test_cpp_pathprintrH   sum)parseroptionsZnative_yaml_pathZtags_yaml_pathZparsed_yamlrP   Znative_functions_groupsZsupported_functions_groupsZout_variant_op_resultZout_variant_test_resultZnative_functions_view_groupsZsupported_functions_view_groupsZview_op_resultZview_test_result	op_resultZtest_resultZsupported_functions_numZsupported_view_functions_numr   )rB   rC   rE   r   main   s    

rV   __main__F)Zescape_newlines)rI   r#   rM   typingr   r   r   Zlibfb.py.logr   Ztorchgenr   Ztorchgen.contextr   Ztorchgen.modelr   r	   r
   Ztorchgen.static_runtimer   r   r   r&   strr*   r7   r9   rV   __name__r   r   r   r   <module>   s,   

4_

