o
    ]i|                     @  s   d Z ddlmZ ddlZddlZddlZddlmZ ddlm	Z	m
Z
 ddlm  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mZmZmZmZ e
raddlmZ ddlmZm Z m!Z! dddZ"edddZ#dddZ$e%dkr{e$  dS dS ) a  
For procedural tests needed for __torch_function__, we use this function
to export method names and signatures as needed by the tests in
test/test_overrides.py.

python -m tools.autograd.gen_annotated_fn_args        aten/src/ATen/native/native_functions.yaml        aten/src/ATen/native/tags.yaml        $OUTPUT_DIR        tools/autograd

Where $OUTPUT_DIR is where you would like the files to be
generated.  In the full build system, OUTPUT_DIR is
torch/testing/_internal/generated
    )annotationsN)defaultdict)AnyTYPE_CHECKING)with_native_function)parse_native_yaml)FileManager   )is_py_fft_functionis_py_linalg_functionis_py_nn_functionis_py_special_functionis_py_torch_functionis_py_variable_methodshould_generate_py_binding)Sequence)ArgumentBaseOperatorNameNativeFunctionnative_yaml_pathstrtags_yaml_pathoutautograd_dirreturnNonec              	     s   t | |j}tdftdftdftdftdftdff}g  |D ]8\}}tt	}|D ]}	t
|	r2||	s3q(||	jjj |	 q(| D ]}
|
D ]}	 | dt|	  qFqBqtj|d}t||d	d
}|dd fdd d S )Nztorch._C._VariableFunctionsztorch._C._nnztorch._C._linalgztorch._C._specialztorch._C._fftztorch.Tensor.Z	templatesF)install_dirZtemplate_dirdry_runzannotated_fn_args.pyzannotated_fn_args.py.inc                     s   dt d diS )Nannotated_args
z    )textwrapindentjoin r   r$   F/home/app/PyTorch/test/pytorch/tools/autograd/gen_annotated_fn_args.py<lambda>M   s   zgen_annotated.<locals>.<lambda>)r   native_functionsr   r   r   r   r
   r   r   listr   funcnameappendvaluesgen_annotated_argsospathr#   r   Zwrite_with_template)r   r   r   r   r(   ZmappingsZpred	namespacegroupsfgroupZtemplate_pathZfmr$   r%   r&   gen_annotated/   s>   
r5   r3   r   c                 C  sl   ddd}ddd}g }||| j jjdd | j jj | vr)||| j jjdd | j jj dt| dS )Nr   	list[str]c                   S  s   g dS )N)ZdiagonalZround_roundZscatter_r$   r$   r$   r$   r&   _get_kwargs_func_exclusion_listU   s   z;gen_annotated_args.<locals>._get_kwargs_func_exclusion_listout_argslist[dict[str, Any]]argsSequence[Argument]is_kwarg_onlyboolr   c                S  sj   |D ]0}|j d ur
qi }t||d< |j|d< tj|jdd|d< t|j}|r-||d< | | qd S )Nr=   r+   T)simple_typer?   size)defaultr   r+   pythonZargument_type_strtypeZargument_type_sizer,   )r9   r;   r=   argZout_argZsize_tr$   r$   r&   _add_out_arg^   s   


z(gen_annotated_args.<locals>._add_out_argF)r=   Tz: ,)r   r6   )r9   r:   r;   r<   r=   r>   r   r   )r*   	argumentsZflat_positionalr+   Zflat_kwarg_onlyrepr)r3   r8   rE   r9   r$   r$   r&   r.   S   s   

	r.   c                  C  sn   t jdd} | jdddd | jddd	d | jd
ddd | jdddd |  }t|j|j|j|j d S )Nz!Generate annotated_fn_args script)descriptionr(   ZNATIVEzpath to native_functions.yaml)metavarhelptagsZTAGSzpath to tags.yamlr   ZOUTzpath to output directoryautogradZAUTOGRADzpath to template directory)	argparseArgumentParseradd_argument
parse_argsr5   r(   rL   r   rM   )parserr;   r$   r$   r&   mainw   s   rS   __main__)
r   r   r   r   r   r   r   r   r   r   )r3   r   r   r   )r   r   )&__doc__
__future__r   rN   r/   r!   collectionsr   typingr   r   Ztorchgen.api.pythonZapirB   Ztorchgen.contextr   Ztorchgen.genr   Ztorchgen.utilsr   Zgen_python_functionsr
   r   r   r   r   r   r   collections.abcr   Ztorchgen.modelr   r   r   r5   r.   rS   __name__r$   r$   r$   r&   <module>   s,    $
$
#
