ó
í˜^c           @   sÓ   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 „  Z	 d „  Z
 d „  Z d	 „  Z d
 „  Z d „  Z e d „ Z d Z d „  Z d „  Z d „  Z d „  Z d „  Z d S(   iÿÿÿÿ(   t   defaultdictNi   (   t
   YamlLoader(   t   IDENT_REGEXt   split_name_paramsc         C   s  t  |  d ƒ � } t j | d t ƒ} Wd  QXt t ƒ } t ƒ  } xV | D]N } | t | ƒ j | ƒ | d rI | d | k s† t	 ‚ | | | d <qI qI Wg  | D] } t
 | | | ƒ ^ q¢ } g  | D]  }	 |	 d d  k	 rÇ |	 d ^ qÇ }
 t |
 ƒ t | | ƒ |
 S(   Nt   rt   Loadert   schema_stringt   autograd_fn(   t   opent   yamlt   loadR   R    t   listt   dictt   get_signaturet   appendt   AssertionErrort   process_definitiont   Nonet   ensure_unique_namest.   match_declarations_with_differentiability_info(   t   patht   declarationst   ft   definitionst   declarations_by_signaturet   declarations_by_schemat   declarationt   defnt   differentiability_infost   dt   autograd_functions(    (    s1   /torch/pytorch/tools/autograd/load_derivatives.pyt   load_derivatives   s    	
"-
c         C   s    i |  d 6| d 6| d 6| d 6S(   Nt	   signaturet   non_differentiable_arg_namest   output_differentiabilityR   (    (   R    R!   R"   R   (    (    s1   /torch/pytorch/tools/autograd/load_derivatives.pyt   create_differentiability_info%   s
    c         C   si   t  |  ƒ d } | j d d ƒ } i |  d 6| d 6| d 6| d 6| d 6t | d ƒ d 6t | d	 ƒ d	 6S(
   Nt   Backwardt   ForwardBackwardt   namet   opR   t   args_with_derivativest   derivativest   saved_inputst   saved_outputs(   t   to_camel_caset   replacet   all_saved_variables(   R&   R)   R(   R   R'   (    (    s1   /torch/pytorch/tools/autograd/load_derivatives.pyt   create_autograd_function1   s    c   
      C   s¿   d „  } g  | D] } | | ƒ ^ q } t  | |  ƒ \ } } t  | | ƒ \ } } xJ t | ƒ D]< }	 |	 t | ƒ k r_ t d j | |	 t | ƒ ƒ ƒ ‚ q_ q_ Wi | d 6| d 6| d 6| d 6S(   Nc         S   s0   |  d d k r, t  j |  ƒ }  d |  d <n  |  S(   NR&   t   selft   result(   t   copyt   deepcopy(   R   (    (    s1   /torch/pytorch/tools/autograd/load_derivatives.pyt   transform_returnA   s    sn   Out of bounds grads access: derivative formula for {} used grads[{}], but the forward only returns {} outputs.t   formulaR*   R+   t	   var_names(   t   saved_variablest   used_gradient_indicest   lent   RuntimeErrort   format(
   t	   argumentst   returnsR&   R5   R6   R4   R   R*   R+   t   i(    (    s1   /torch/pytorch/tools/autograd/load_derivatives.pyt   create_derivative@   s    	c            sI  d „  } d „  ‰ d „  } d „  ‰  ‡  ‡ f d †  } d „  } |  j  d ƒ } t | ƒ \ } }	 |  j  d d ƒ }
 | j d	 | ƒ } | sg  | j ƒ  D]O \ } } | j d	 d
 ƒ j | d ƒ r˜ t | ƒ d k r˜ | j d	 d
 ƒ ^ q˜ } t d j	 | d j
 | ƒ ƒ ƒ ‚ n  t | ƒ } | | } t | ƒ d k rªg  | j ƒ  D]7 \ } } | j | d ƒ rFt | ƒ d k rF| ^ qF} t d j	 | | d j
 | ƒ ƒ ƒ ‚ n  | | | ƒ } d d „  | d Dƒ k rët d j	 | ƒ ƒ ‚ n  | | |  | ƒ \ } } } d } t | ƒ d k r6t | | | | ƒ } n  t | | |
 | ƒ S(   s3   Processes a single entry `defn` in derivatives.yamlc         S   sK   x" |  D] } | d | k r | Sq W| d |  d d k sC t  ‚ |  d S(   NR&   t   _i    (   R   (   R   R&   R   (    (    s1   /torch/pytorch/tools/autograd/load_derivatives.pyt   canonical_declaration`   s
    c         S   s&   g  |  j  d ƒ D] } | j ƒ  ^ q S(   s(   Given "foo, bar", return ["foo", "bar"].t   ,(   t   splitt   strip(   t	   raw_namest   x(    (    s1   /torch/pytorch/tools/autograd/load_derivatives.pyt   split_namesh   s    c            s    t  ‡  f d †  t | ƒ Dƒ ƒ S(   s:   Return the index of the first element of xs matching pred.c         3   s-   |  ]# \ } } ˆ  | ƒ r | | f Vq d  S(   N(    (   t   .0R>   RF   (   t   pred(    s1   /torch/pytorch/tools/autograd/load_derivatives.pys	   <genexpr>n   s    (   t   nextt	   enumerate(   RI   t   xs(    (   RI   s1   /torch/pytorch/tools/autograd/load_derivatives.pyt   lookup_predl   s    c   
      S   s7  d } d } t  } g  } x— | D]� } | d } | t t j t j d ƒ | ƒ ƒ 7} | t t j t j d ƒ | ƒ ƒ 7} | o˜ t j t j d ƒ | ƒ } | j t | ƒ ƒ q W| t | ƒ k sÊ t	 ‚ | t | ƒ k }	 | r | r t
 d j |  ƒ ƒ ‚ n  |	 r3t | ƒ d h k r3t
 d j |  ƒ ƒ ‚ n  d S(	   sÉ   
        Check for some subtle mistakes one might make when writing derivatives.
        These mistakes will compile, but will be latent until a function is
        used with double backwards.
        i    R5   t   gradt   gradst   not_implementeds“   Derivative definition of {} in derivatives.yaml illegally mixes use of 'grad' and 'grads'. Consider replacing occurrences of 'grad' with 'grads[0]'sï   Derivative definition of {} in derivatives.yaml solely refers to 'grads[0]'.  If the first output is indeed the only differentiable output, replace 'grads[0]' with 'grad'; otherwise, there is a likely error in your derivatives declaration.N(   t   TrueR9   t   ret   findallR   R;   t   searcht   extendR8   R   R:   t   set(
   t	   defn_nameR   R)   t	   used_gradt
   used_gradst   fully_implementedt   used_grads_indicesR   R5   t   only_used_grads_indices(    (    s1   /torch/pytorch/tools/autograd/load_derivatives.pyt   check_grad_usagep   s&    
%%		c            s~  t  ƒ  } x$ | D] } | t  ˆ | ƒ ƒ O} q Wg  } x5 | d D]) } | d | k r] qA n  | j | ƒ qA Wg  } g  ‰  xÆ t | j ƒ  ƒ D]² } | | } ˆ | ƒ }	 t | d | d | d | |	 ƒ }
 | j ƒ  j ƒ  d k r2t g  |
 d D] } t | ƒ t	 k ^ qó ƒ s!t
 d ƒ ‚ ˆ  |
 d 7‰  q� | j |
 ƒ q� Wt	 t ‡  f d †  | ƒ ƒ } ˆ |  | | ƒ | | ˆ  f S(   NR<   R&   R=   t   non_differentiableR6   s<   Variable names associated to a formula should be a flat listc            s   |  d ˆ  k S(   NR&   (    (   RF   (   R!   (    s1   /torch/pytorch/tools/autograd/load_derivatives.pyt   <lambda>®   t    (   RV   R   t   sortedt   keysR?   t   lowerRD   t   sumt   typeR   R   t   filter(   RW   R   R   t   args_with_derivatives_setRE   R(   t   argR)   R5   t   namest
   derivativet   var_name(   R]   RG   (   R!   s1   /torch/pytorch/tools/autograd/load_derivatives.pyt   set_up_derivatives’   s2    	
-	c         S   s
   t  |  Œ  S(   N(   t   zip(   RL   (    (    s1   /torch/pytorch/tools/autograd/load_derivatives.pyt   unzip¶   s    R&   R"   s   aten::R`   t   (i    sJ   could not find ATen declaration for schema: {} .  Available signatures:
{}s   
sŽ   could not find ATen declaration for legacy signature: {} corresponding to schema {}.  Please report a bug to PyTorch. Available signatures: {}s   , t   grad_input_maskc         s   s   |  ] } | d  Vq d S(   R&   N(    (   RH   t   a(    (    s1   /torch/pytorch/tools/autograd/load_derivatives.pys	   <genexpr>Ó   s    R<   sš   Schema for {} has an argument named grad_input_mask, but this name would be shadowed by our codegen. Please use a different name in native_functions.yaml.N(   t   popR   R   t   gett   itemsR-   t
   startswithR9   R:   R;   t   joinR   R/   R#   (   R   R   R   RA   RM   Rl   Rn   t   specificationRW   t   paramsR"   t   schema_declarationt   kt   vt   availR    R   t	   canonicalR)   R(   R!   R   (    (   R]   RG   s1   /torch/pytorch/tools/autograd/load_derivatives.pyR   ]   sB    				"$	I	
1			c         C   sŸ   t  t ƒ } x# |  D] } | | d j | ƒ q Wxf | j ƒ  D]X } | | } t | ƒ d k r? x3 t | ƒ D]" \ } } | d c t | ƒ 7<qn Wq? q? Wd  S(   NR'   i   (   R    R   R   Rb   R9   RK   t   str(   R   t   functions_by_namet   funcR'   t	   overloadsR>   (    (    s1   /torch/pytorch/tools/autograd/load_derivatives.pyR   å   s    
c         C   sÆ   |  d } |  d } | r� |  d rF | j  d ƒ s9 t ‚ | d  } q� | j  d ƒ r� | d  } g  | D] } | j d t ƒ sf | ^ qf } q� n  g  | D] } | d	 ^ q— } d
 j | d j | ƒ ƒ S(   NR&   R<   t   inplaceR@   iÿÿÿÿt   _outiüÿÿÿt   outputt   simple_types   {}({})s   , (   t   endswithR   Rs   t   FalseR;   Rv   (   R   t   use_base_variantR&   R<   Rh   t   simple_types(    (    s1   /torch/pytorch/tools/autograd/load_derivatives.pyR   õ   s    



1s   (?:^|\W)grads\[(\d+)\]c         C   s)   g  t  j t |  ƒ D] } t | ƒ ^ q S(   s¤   Determine a list of gradient indices (the i in grads[i]) that
    are used by the formula.

    >>> used_gradient_indices("foo(grads[0], grads[1])")
    [0, 1]
    (   RR   RS   t   GRAD_INDEX_REGEXt   int(   R5   R>   (    (    s1   /torch/pytorch/tools/autograd/load_derivatives.pyR8     s    c      
      s¥  g  ‰ d i d d 6d d 6f d i d d 6d d 6d	 „  d
 6d „  d 6f d i d „  d 6d d 6f d i d d 6d d 6f d i d d 6d d 6f d i d d 6d d 6f d i d d 6d d 6f g } xÔ | D]Ì } d | k rã qË n  | d ‰ xG | D]? \ } ‰  ‡  ‡ ‡ f d †  } t  j | j ˆ ƒ | |  ƒ }  qô Wt  j t j ˆ ƒ |  ƒ rË t j | ƒ } | d j d d ƒ j d d ƒ | d <ˆ j | ƒ qË qË W|  ˆ f S(    Ns   {}.sizes\(\)t   _sizest   suffixt   IntArrayRefRe   s   zeros_like\({}\)t   _infot   TypeAndSizec         S   s   |  S(   N(    (   R&   (    (    s1   /torch/pytorch/tools/autograd/load_derivatives.pyR_     R`   t   exprc         S   s   |  d S(   Ns   _info.zeros()(    (   R&   (    (    s1   /torch/pytorch/tools/autograd/load_derivatives.pyR_     R`   t   ress   {}.size\((\w+)\)c         S   s   d j  |  j ƒ  Œ  S(   Ns   _argsize_{}(   R;   t   groups(   t   m(    (    s1   /torch/pytorch/tools/autograd/load_derivatives.pyR_   #  R`   t   int64_ts   {}.numel\(\)t   _numels   to_args_sizes\({}\)t   _args_sizess!   std::vector<std::vector<int64_t>>s   TensorGeometry\({}\)t	   _geometryt   TensorGeometrys   {}.scalar_type\(\)t   _scalar_typet
   ScalarTypeR&   c            sŸ   ˆ  d } t  | ƒ r" | |  ƒ n | } d ˆ  k rD ˆ  d ˆ ƒ n |  j d ƒ } ˆ j i ˆ | d 6ˆ  d d 6| d 6ƒ d ˆ  k r— ˆ  d ˆ ƒ Sˆ | S(   NR�   R‘   i    R&   Re   R’   (   t   callablet   groupR   (   R”   R�   R‘   (   t   infoR&   t   saved(    s1   /torch/pytorch/tools/autograd/load_derivatives.pyt   replE  s    
+	s   const R`   s    &(	   RR   t   subR;   RT   R   R2   R3   R-   R   (   R5   t   argst   REPLACEMENTSRh   t   regexR    (    (   Rž   R&   RŸ   s1   /torch/pytorch/tools/autograd/load_derivatives.pyR7     sH    







"&c         C   sm   t  ƒ  } g  } xW |  D]O } xF | | D]: } | d | k rC q' n  | j | d ƒ | j | ƒ q' Wq W| S(   NR&   (   RV   t   addR   (   R)   t   keyt   seenRŸ   R   t	   saved_arg(    (    s1   /torch/pytorch/tools/autograd/load_derivatives.pyR.   ]  s    	c         C   s/   d j  g  |  j d ƒ D] } | j ƒ  ^ q ƒ S(   NR`   R@   (   Rv   RC   t   title(   R&   t   p(    (    s1   /torch/pytorch/tools/autograd/load_derivatives.pyR,   i  s    c            sŽ   d „  | Dƒ ‰  ‡  f d †  } xh |  D]` } | | ƒ } | rH | d n d | d <| rb | d n g  | d <| r| | d n d | d <q& Wd S(   sí   Sets the "derivative" and "output_differentiability" key on declarations
    to matching differentiability info

    In-place functions will use the out-of-place derivative definition if there
    is no in-place specific derivative.
    c         S   s   i  |  ] } | | d  “ q S(   R    (    (   RH   R   (    (    s1   /torch/pytorch/tools/autograd/load_derivatives.pys
   <dictcomp>u  s   	 c            s?   t  |  ƒ } | ˆ  k r  ˆ  | St  |  d t ƒ} ˆ  j | ƒ S(   NRˆ   (   R   RQ   Rs   (   R   R    (   t   infos_by_signature(    s1   /torch/pytorch/tools/autograd/load_derivatives.pyt	   find_infow  s
    R   Rj   R!   R"   N(   R   (   R   R   R¬   R   Rž   (    (   R«   s1   /torch/pytorch/tools/autograd/load_derivatives.pyR   m  s    
(   t   collectionsR    R2   RR   R	   t   utilsR   R   R   R   R#   R/   R?   R   R   R‡   R   RŠ   R8   R7   R.   R,   R   (    (    (    s1   /torch/pytorch/tools/autograd/load_derivatives.pyt   <module>   s$   					ˆ		
	M		