ó
í˜^c           @   s~  d  Z  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 d d l m	 Z	 m
 Z
 i d d 6d d 6d d	 6d d
 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6Z e e j ƒ  ƒ j d d h ƒ Z d „  Z d „  Z d  „  Z d! „  Z e d" „ Z d# „  Z d$ „  Z e d% k rze ƒ  n  d S(&   sS  
To run this file by hand from the root of the PyTorch
repository, run:

python -m tools.autograd.gen_autograd        build/aten/src/ATen/Declarations.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/csrc/autograd/generated/
iÿÿÿÿN(   t   defaultdicti   (   t
   YamlLoadert   split_name_paramst   selft   numpy_Tt   aliast
   as_stridedt   diagonalt   expandt   narrowt   permutet   selectt   slicet   squeezet   tt	   transposet   unfoldt	   unsqueezet   viewt   unbindt   _indicest   _valuest   indicest   valuest'   sparse_coo_tensor_with_dims_and_tensorst   chunkt   splitc         C   sk   t  |  ƒ d k r d St  |  ƒ d k r4 |  d d Sg  |  D] } | d ^ q; } d j d j | ƒ ƒ Sd  S(   Ni    t   voidi   t   types   std::tuple<{}>t   ,(   t   lent   formatt   join(   t   returnst   rt   return_types(    (    s-   /torch/pytorch/tools/autograd/gen_autograd.pyt   format_return_typeG   s    c         C   sq   |  d } | j  d d ƒ j  d d ƒ } | j  d d ƒ } t j d | ƒ } | rm d j | j d	 ƒ ƒ } n  | S(
   NR   s    &t    s   const s   Generator *t	   Generators   c10::optional<(.+)>s   {}?i   (   t   replacet   ret   matchR   t   group(   t   argt   simple_typet	   opt_match(    (    s-   /torch/pytorch/tools/autograd/gen_autograd.pyt   get_simple_typeQ   s    
c         C   s�  t  |  d ƒ � } t j | d t ƒ} Wd  QXg  } xV| D]N} | j d ƒ rU q: n  x" | d D] } t | ƒ | d <q` Wx" | d D] } t | ƒ | d <q… Wg  | d D] } | d d | d	 ^ qª | d
 <g  | d D] } | d	 ^ qÛ | d <g  | d D] } | d d | d	 ^ q | d <g  | d D] } | d	 ^ q1| d <| d	 | d <t | d ƒ | d <| d	 | d <| j | ƒ q: W| S(   NR"   t   Loadert
   deprecatedt	   argumentsR,   R!   R   t    t   namet   formalst   argst   type_method_formalst   type_method_argst   api_namet   return_typet	   base_name(   t   opent   yamlt   loadR   t   getR.   R$   t   append(   t   patht   ft   declarationst   selected_declarationst   declarationR+   t   ret(    (    s-   /torch/pytorch/tools/autograd/gen_autograd.pyt   load_aten_declarations\   s*    .%.%c      	      s½  ‡  f d †  } t  | d ƒ � } t j | d t ƒ} Wd  QXg  } | ƒ  } d „  } xb| D]Z} t | d ƒ \ }	 }
 t | d ƒ \ } } | |	 | |
 ƒ } x| | D]} t j | ƒ } t | d <|
 | d <d	 „  t |
 ƒ Dƒ } | d
 } g  } t	 } x˜ | D]� } | d k r$t } qn  | j
 d ƒ \ } } | | | } | j i | d 6| d 6| d d 6| d d 6| d d 6| j d t	 ƒ d 6ƒ qW| | d
 <| j | ƒ qª Wq[ W| S(   Nc             s’   t  t ƒ }  x ˆ  D]w } | d } | d r7 | d  n | } g  | d D] } | d ^ qH } d j | d j | ƒ ƒ } |  | j | ƒ q W|  S(   NR3   t   inplaceiÿÿÿÿR1   R,   s   {}({})s   , (   R    t   listR   R    R?   (   t   dRD   R3   R:   R+   t   simple_typest	   signature(   t
   aten_decls(    s-   /torch/pytorch/tools/autograd/gen_autograd.pyt   group_declarations_by_signature{   s    
!R"   R/   c         S   s   t  g  | D]. } | d k r
 | j d ƒ d  d  d … ^ q
 ƒ } g  | D] } | j | d ƒ ^ qH } d j |  d j | ƒ ƒ S(   Nt   *R2   iÿÿÿÿt   Scalars   {}({})s   , (   t   dictR   R>   R   R    (   R3   t   paramst	   call_argst   paramt   typesR+   t   rearranged_types(    (    s-   /torch/pytorch/tools/autograd/gen_autograd.pyt   get_signatureŠ   s    A%t   atenR3   R0   RR   c         S   s   i  |  ] \ } } | | “ q S(    (    (   t   .0t   iR+   (    (    s-   /torch/pytorch/tools/autograd/gen_autograd.pys
   <dictcomp>œ   s   	 R1   RN   R2   t
   kwarg_onlyR   R,   t   dynamic_typet   output(   R;   R<   R=   R   R   t   copyt   deepcopyt   Truet	   enumeratet   FalseR   R?   R>   (   RL   t   deprecated_pathRM   RA   t   deprecated_defsRB   t   declarations_by_signatureRV   R0   t	   aten_nameRR   R3   RQ   RK   RD   t   call_arg_to_idxt   original_argsR1   RZ   RS   t   _t
   param_namet   original(    (   RL   s-   /torch/pytorch/tools/autograd/gen_autograd.pyt   load_deprecated_signaturesz   sD    
		


	
c         C   sÂ   t  |  ƒ } d d l m } | t j j | d ƒ | ƒ } t j j | d ƒ } | sx d d l m } | | | | ƒ n  d d l m }	 |	 | | | ƒ d d l m }
 |
 | | | d | ƒd  S(	   Ni   (   t   load_derivativess   derivatives.yamlt	   templates(   t   gen_variable_type(   t   gen_autograd_functions_lib(   t   gen_variable_factoriest   disable_autograd(	   RF   Rl   t   osR@   R    Rn   t   gen_autograd_functionsRo   Rp   (   t	   aten_patht   outt   autograd_dirRq   RL   Rl   t   autograd_functionst   template_pathRn   Ro   Rp   (    (    s-   /torch/pytorch/tools/autograd/gen_autograd.pyt   gen_autograd¸   s    c   
      C   sâ   t  |  ƒ } d d l m } | t j j | d ƒ | ƒ } t j j | d ƒ } t | t j j | d ƒ ƒ } d d l m } | | | | ƒ d d l m	 }	 |	 j
 | | | | ƒ |	 j | | | | ƒ |	 j | | | ƒ d  S(   Ni   (   Rl   s   derivatives.yamlRm   s   deprecated.yaml(   t   gen_autograd_functions_python(   t   gen_python_functions(   RF   Rl   Rr   R@   R    Rk   Rs   Rz   R%   R{   t   gen_py_variable_methodst   gen_py_torch_functionst   gen_py_nn_functions(
   Rt   Ru   Rv   RL   Rl   Rw   Rx   R0   Rz   R{   (    (    s-   /torch/pytorch/tools/autograd/gen_autograd.pyt   gen_autograd_pythonÒ   s"    c          C   s†   t  j d d ƒ }  |  j d d d d d ƒ|  j d d d	 d d
 ƒ|  j d d d d d ƒ|  j ƒ  } t | j | j | j ƒ d  S(   Nt   descriptions"   Generate autograd C++ files scriptRB   t   metavart   DECLt   helps   path to Declarations.yamlRu   t   OUTs   path to output directoryt   autogradt   AUTOGRADs   path to autograd directory(   t   argparset   ArgumentParsert   add_argumentt
   parse_argsRy   RB   Ru   R…   (   t   parserR5   (    (    s-   /torch/pytorch/tools/autograd/gen_autograd.pyt   mainò   s    		t   __main__(   t   __doc__R‡   R]   Rr   R<   R(   t   collectionsR    t   utilsR   R   t   VIEW_FUNCTIONSt   sett   keyst   uniont   RETURNS_VIEWS_OF_INPUTR$   R.   RF   Rk   Ra   Ry   R   RŒ   t   __name__(    (    (    s-   /torch/pytorch/tools/autograd/gen_autograd.pyt   <module>   sN   
!	
			>	 	