ó
àÆ÷Xc           @` s  d  Z  d d l m Z m Z m Z d d l Z d d l 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 m Z d d
 l m Z m Z m Z m Z d d l m Z m Z m 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' d d l( m) Z) m* Z* d d l+ m, Z, d d l- m. Z. d d l/ m0 Z0 e j e g d e1 ƒd „  ƒ Z2 e j	 j3 d e j4 e2 d e j4 j5 ƒd d d ƒ e j e g d e1 ƒd „  ƒ Z6 e j	 j3 d e j4 e6 d e j4 j5 ƒd d d ƒ e e g ƒ d „  ƒ Z7 e e g ƒ d „  ƒ Z8 e e g ƒ d „  ƒ Z9 e e  g ƒ d  „  ƒ Z: e e g ƒ d! „  ƒ Z; e e! g ƒ d" „  ƒ Z< e e g ƒ d# „  ƒ Z= e e g ƒ d$ „  ƒ Z> e e g ƒ d% „  ƒ Z? e e  g ƒ d& „  ƒ Z@ e e g ƒ d' „  ƒ ZA e e! g ƒ d( „  ƒ ZB e j j	 jC ƒ  ZD d) eD _E e$ eD d* d ƒ eD j3 d+ e7 d, d- d* d ƒ eD j3 d. e9 d, d- d* d ƒ eD j3 d/ e; d, d- d* d ƒ eD j3 d0 e8 d, d- d* d ƒ eD j3 d1 e: d, d- d* d ƒ eD j3 d2 e< d, d- d* d ƒ eD j3 d3 e= d4 d* d ƒ eD j3 d5 e? d4 d* d ƒ eD j3 d6 eA d4 d* d ƒ eD j3 d7 e> d4 d* d ƒ eD j3 d8 e@ d4 d* d ƒ eD j3 d9 eB d4 d* d ƒ e e e e e e  e! g ƒ d: „  ƒ ZF e	 j3 d; e' jG eF d< d; ƒd= d* d ƒ d S(>   s9   
Optimizations addressing the ops in nnet root directory
i    (   t   absolute_importt   print_functiont   divisionN(   t   compilet   gof(   t   optdb(   t   local_optimizer(   t   copy_stack_trace(   t   CorrMMt   CorrMM_gradInputst   CorrMM_gradWeights(   t   Corr3dMMt   Corr3dMM_gradInputst   Corr3dMM_gradWeights(   t   SparseBlockGemvt   SparseBlockOutert   sparse_block_gemv_inplacet   sparse_block_outer_inplace(   t   AbstractConv2dt   AbstractConv2d_gradWeightst   AbstractConv2d_gradInputs(   t   AbstractConv3dt   AbstractConv3d_gradWeightst   AbstractConv3d_gradInputs(   t   get_conv_output_shape(   t   register_specialize_device(   t
   TensorType(   t   opt(   t   conv2dt   ConvOp(   t   conv3D(   t
   convGrad3D(   t   convTransp3Dt   inplacec         C` sM   t  |  j t ƒ rI |  j j rI t |  j Œ  } t |  j d | ƒ | g St S(   sM   
        SparseBlockGemv(inplace=False) -> SparseBlockGemv(inplace=True)
    i    (	   t
   isinstancet   opR   R!   R   t   inputsR   t   outputst   False(   t   nodet   new_node(    (    s6   /tmp/pip-build-X4mzal/theano/theano/tensor/nnet/opt.pyt   local_inplace_sparse_block_gemv&   s
    R)   t   failure_callbacki<   t   fast_runc         C` sM   t  |  j t ƒ rI |  j j rI t |  j Œ  } t |  j d | ƒ | g St S(   sO   
        SparseBlockOuter(inplace=False) -> SparseBlockOuter(inplace=True)
    i    (	   R"   R#   R   R!   R   R$   R   R%   R&   (   R'   R(   (    (    s6   /tmp/pip-build-X4mzal/theano/theano/tensor/nnet/opt.pyt    local_inplace_sparse_block_outer7   s
    R,   c         C` sù   t  j j d k r d  St |  j t ƒ s, d  S|  j \ } } t | j t	 ƒ sa t | j t	 ƒ re d  S|  j j
 r¨ | d  d  … d  d  … d  d  d … d  d  d … f } n  t d |  j j d |  j j d |  j j ƒ | | ƒ } t |  j d | ƒ | g S(   Nt    iÿÿÿÿt   border_modet	   subsamplet   filter_dilationi    (   t   theanot   configt   cxxR"   R#   R   t   NoneR$   t   typeR   t   filter_flipR   R.   R/   R0   R   R%   (   R'   t   imgt   kernt   rval(    (    s6   /tmp/pip-build-X4mzal/theano/theano/tensor/nnet/opt.pyt   local_abstractconv_gemmI   s    7c         C` s  t  j j d k r d  St |  j t ƒ s, d  S|  j \ } } t | j t	 ƒ sa t | j t	 ƒ re d  S|  j j
 r´ | d  d  … d  d  … d  d  d … d  d  d … d  d  d … f } n  t d |  j j d |  j j d |  j j ƒ | | ƒ } t |  j d | ƒ | g S(   NR-   iÿÿÿÿR.   R/   R0   i    (   R1   R2   R3   R"   R#   R   R4   R$   R5   R   R6   R   R.   R/   R0   R   R%   (   R'   R7   R8   R9   (    (    s6   /tmp/pip-build-X4mzal/theano/theano/tensor/nnet/opt.pyt   local_abstractconv3d_gemma   s    Cc         C` s2  t  j j d k r d  St |  j t ƒ s, d  S|  j \ } } } t | j t	 ƒ sd t | j t	 ƒ rh d  St
 d |  j j d |  j j d |  j j ƒ | | | ƒ } t |  j d | ƒ |  j j rø | d  d  … d  d  … d  d  d … d  d  d … f } n  t  j j | |  j d j ƒ } t |  j d | ƒ | g S(   NR-   R.   R/   R0   i    iÿÿÿÿ(   R1   R2   R3   R"   R#   R   R4   R$   R5   R   R
   R.   R/   R0   R   R%   R6   t   tensort   patternbroadcastt   broadcastable(   R'   R7   t   topgradt   shapeR9   (    (    s6   /tmp/pip-build-X4mzal/theano/theano/tensor/nnet/opt.pyt"   local_abstractconv_gradweight_gemmy   s"    7c         C` s>  t  j j d k r d  St |  j t ƒ s, d  S|  j \ } } } t | j t	 ƒ sd t | j t	 ƒ rh d  St
 d |  j j d |  j j d |  j j ƒ | | | ƒ } t |  j d | ƒ |  j j r| d  d  … d  d  … d  d  d … d  d  d … d  d  d … f } n  t  j j | |  j d j ƒ } t |  j d | ƒ | g S(   NR-   R.   R/   R0   i    iÿÿÿÿ(   R1   R2   R3   R"   R#   R   R4   R$   R5   R   R   R.   R/   R0   R   R%   R6   R<   R=   R>   (   R'   R7   R?   R@   R9   (    (    s6   /tmp/pip-build-X4mzal/theano/theano/tensor/nnet/opt.pyt$   local_abstractconv3d_gradweight_gemm”   s"    Cc         C` sÿ   t  j j d k r d  St |  j t ƒ s, d  S|  j \ } } } t | j t	 ƒ sd t | j t	 ƒ rh d  S|  j j
 r« | d  d  … d  d  … d  d  d … d  d  d … f } n  t d |  j j d |  j j d |  j j ƒ | | | ƒ } t |  j d | ƒ | g S(   NR-   iÿÿÿÿR.   R/   R0   i    (   R1   R2   R3   R"   R#   R   R4   R$   R5   R   R6   R	   R.   R/   R0   R   R%   (   R'   R8   R?   R@   R9   (    (    s6   /tmp/pip-build-X4mzal/theano/theano/tensor/nnet/opt.pyt"   local_abstractconv_gradinputs_gemm¯   s     7	c         C` s  t  j j d k r d  St |  j t ƒ s, d  S|  j \ } } } t | j t	 ƒ sd t | j t	 ƒ rh d  S|  j j
 r· | d  d  … d  d  … d  d  d … d  d  d … d  d  d … f } n  t d |  j j d |  j j d |  j j ƒ | | | ƒ } t |  j d | ƒ | g S(   NR-   iÿÿÿÿR.   R/   R0   i    (   R1   R2   R3   R"   R#   R   R4   R$   R5   R   R6   R   R.   R/   R0   R   R%   (   R'   R8   R?   R@   R9   (    (    s6   /tmp/pip-build-X4mzal/theano/theano/tensor/nnet/opt.pyt$   local_abstractconv3d_gradinputs_gemmÈ   s     C	c      	   C` sÉ   t  |  j t ƒ s d  S|  j \ } } t  | j t ƒ sK t  | j t ƒ rO d  S|  j j d k re d  S|  j j su d  St	 | | |  j j
 |  j j d |  j j d |  j j ƒ} t |  j d | ƒ | g S(   Nt   fullt   validR.   R/   i    (   s   fullRF   (   R"   R#   R   R4   R$   R5   R   R.   R6   R   t   imshpt   kshpR/   R   R%   (   R'   R7   R8   R9   (    (    s6   /tmp/pip-build-X4mzal/theano/theano/tensor/nnet/opt.pyt   local_conv2d_cpuá   s     	c         C` s‚  t  |  j t ƒ s d  S|  j \ } } t  | j t ƒ sK t  | j t ƒ rO d  S|  j j d d g k rk d  S|  j j d	 k r d  St	 j
 j | d  d  … d d d d f ƒ } |  j j rû | d  d  … d  d  … d  d  d … d  d  d … d  d  d … f } n  | j d d d d d ƒ } | j d d d d d ƒ } t | | | |  j j ƒ } t |  j d | ƒ | j d d d d d ƒ } | g S(
   NRF   i    i   iÿÿÿÿi   i   i   (   i    i    i    (   i   i   i   (   R"   R#   R   R4   R$   R5   R   R.   R0   R1   R<   t
   zeros_likeR6   t
   dimshuffleR   R/   R   R%   (   R'   R7   R8   t   biasR9   (    (    s6   /tmp/pip-build-X4mzal/theano/theano/tensor/nnet/opt.pyt   local_conv3d_cpuú   s&    +Cc         C` sS  t  |  j t ƒ s d  S|  j \ } } } t  | j t ƒ sN t  | j t ƒ rR d  S|  j j d k rh d  S|  j j sx d  S|  j j d k rä|  j j	 d k rä| j
 d d d d d ƒ } | j
 d d d d d ƒ } t d | d	 |  j j	 d |  j j	 d d f d
 | j d | d | d d | j d f d | ƒ } t |  j d | ƒ t j j | d ƒ } | j
 d d d d ƒ } | d  d  … d  d  … d  d  d … d  d  d … f } t j j | |  j d j ƒ } t |  j d | ƒ | g S|  j j	 \ } } | d k s| d k rd  S|  j j d  k r-d }	 n |  j j }	 |  j j d  k rTd }
 n |  j j }
 d  |	 k sxd  |
 k r‘| | f d k r‘d  Sn  t |	 ƒ d k rµt |
 ƒ d k s»t ‚ t |	 |
 |  j j |  j j	 |  j j ƒ d } t |	 |
 |  j j d ƒ d } | j
 d  ƒ } | j
 d! ƒ } |  j j d k r| | } } | } t } d  } |	 d |
 d } } |	 d |	 d |	 d f } | } n |  j j d k r| | } } d  } t } |	 d | d | d f } |
 d |	 d } } |	 d | d | d f } |	 d } n t d ƒ ‚ | d  d  … d  d  … d  d  d … d  d  d … f } t | | | | d d d d d d  d d  d d  d | d | d | d d ƒ} | | | ƒ } t |  j d | ƒ |  j j d k r| j
 d" ƒ } | d  d  … d  d  … d  d  d … d  d  d … f } n  t j j | |  j d j ƒ } t |  j d | ƒ | g S(#   NRE   RF   i   i    i   i   t   xt   Vt   dt   WShapei   t   dCdHiÿÿÿÿs0   Only [full,valid] modes are currently supported.t   output_modet   unroll_batcht   unroll_kernt   unroll_patcht   imshp_logicalt   kshp_logicalt   kshp_logical_top_alignedt   direction_hints   bprop weights(   s   fulls   valid(   i   i   (   i   i   (   i   i   (   NNNN(   NNNN(   i   i   (   i   i   (   i   i    i   i   (   i   i    i   i   (   i   i    i   i   (   R"   R#   R   R4   R$   R5   R   R.   R6   R/   RK   R   R@   R   R%   R1   R<   t   addbroadcastR=   R>   RG   RH   t   lent   AssertionErrorR   R0   R&   t   Truet   NotImplementedErrorR   (   R'   R7   R?   R@   t   shuffled_imgt   shuffled_topgradR9   t   dxt   dyt   op_imshpt   op_kshpt   outshpt
   fulloutshpt   newimgt
   newtopgradt   filtersRX   RY   RW   t   bsizet   nkernRG   RH   t   dwt   res(    (    s6   /tmp/pip-build-X4mzal/theano/theano/tensor/nnet/opt.pyt   local_conv2d_gradweight_cpu  s¨    #
	4		*						4	7c         C` s¨  t  |  j t ƒ s d  S|  j \ } } } t  | j t ƒ sN t  | j t ƒ rR d  S|  j j d d g k rn d  S|  j j d	 k r„ d  S| j	 d d d d d ƒ } | j	 d d d d d ƒ } | j
 d | d | d | d | j
 d f } t | |  j j | | ƒ } t |  j d | ƒ | j	 d d d d d ƒ } |  j j r‚| d  d  … d  d  … d  d  d … d  d  d … d  d  d … f } n  t j j | |  j d j ƒ } | g S(
   NRF   i    i   i   i   i   iÿÿÿÿ(   i    i    i    (   i   i   i   (   R"   R#   R   R4   R$   R5   R   R.   R0   RK   R@   R   R/   R   R%   R6   R1   R<   R=   R>   (   R'   R7   R?   R@   t   W_shapeR9   (    (    s6   /tmp/pip-build-X4mzal/theano/theano/tensor/nnet/opt.pyt   local_conv3d_gradweight_cpu  s*    /Cc         C` sO  t  |  j t ƒ s d  S|  j \ } } } t  | j t ƒ sN t  | j t ƒ rR d  S|  j j d k rh d  S|  j j sx d  S|  j j d k r|  j j	 d k r| d  d  … d  d  … d  d  d … d  d  d … f } | j
 d d d d d ƒ } | j
 d d d d d ƒ } t j j | d d d d d  d  … f ƒ } t d	 | d
 | d |  j j	 d |  j j	 d d f d | d | d | d d f ƒ } t |  j d | ƒ t j j | d ƒ } | j
 d d d d ƒ } t j j | |  j d j ƒ } t |  j d | ƒ | g S|  j j	 \ } }	 | d k s+|	 d k r/d  S|  j j d  k rJd }
 n |  j j }
 |  j j d  k rqd } n |  j j } d  |
 k s•d  | k r®| |	 f d k r®d  Sn  d } |  j j d k sÏd } n  | j
 d ƒ } | d  d  … d  d  … d  d  d … d  d  d … f } t |
 | |  j j |  j j	 |  j j ƒ d } t |
 | |  j j d  ƒ d } |
 d } | d | d | d f } | d | d | d f } t | | d | |
 d d d d | d d  d d  d d  d | d d  d d d d ƒ} | | | ƒ } t |  j d | ƒ t j j | |  j d j ƒ } t |  j d | ƒ | g S(!   NRE   RF   i   iÿÿÿÿi    i   i   RN   t   Wt   bRP   t   Ht   RShapei   RS   RT   RU   RV   RW   RX   t   versionRZ   s   bprop inputs(   s   fulls   valid(   i   i   (   i   i   (   i   i   (   NNNN(   NNNN(   i   i   (   i   i    i   i   (   i   i   (   R"   R#   R   R4   R$   R5   R   R.   R6   R/   RK   R1   R<   RJ   R    R   R%   R[   R=   R>   RG   RH   R   R0   R   (   R'   R8   R?   R@   t   shuffled_kernRa   Rs   R9   Rb   Rc   Rd   Re   t   modeRj   Rf   Rg   Rl   RG   RW   t   din(    (    s6   /tmp/pip-build-X4mzal/theano/theano/tensor/nnet/opt.pyt   local_conv2d_gradinputs_cpu°  sˆ    $4+#			4				
	c         C` s§  t  |  j t ƒ s d  S|  j \ } } } t  | j t ƒ sN t  | j t ƒ rR d  S|  j j d d g k rn d  S|  j j d	 k r„ d  S|  j j	 rÓ | d  d  … d  d  … d  d  d … d  d  d … d  d  d … f } n  | j
 d d d d d ƒ } | j
 d d d d d ƒ } t j j | d d d d d  d  … f ƒ } t | | |  j j | | ƒ } t |  j d | ƒ | j
 d d d d d ƒ } t j j | |  j d j ƒ } | g S(
   NRF   i    i   iÿÿÿÿi   i   i   (   i    i    i    (   i   i   i   (   R"   R#   R   R4   R$   R5   R   R.   R0   R6   RK   R1   R<   RJ   R    R/   R   R%   R=   R>   (   R'   R8   R?   R@   RL   R9   (    (    s6   /tmp/pip-build-X4mzal/theano/theano/tensor/nnet/opt.pyt   local_conv3d_gradinputs_cpu	  s*    C+t	   conv_optst   fast_compileR:   i   t	   conv_gemmRA   RC   R;   RB   RD   RI   i(   Ro   Rz   RM   Rq   R{   c         C` sD   t  |  j t t t t t t f ƒ r@ t d |  j j	 j
 ƒ ‚ n  d  S(   Ns-  %s Theano optimization failed: there is no implementation available supporting the requested options. Did you exclude both "conv_dnn" and "conv_gemm" from the optimizer? If on GPU, is cuDNN available and does the GPU support it? If on CPU, do you have a BLAS library installed Theano can link against?(   R"   R#   R   R   R   R   R   R   R]   t	   __class__t   __name__(   R'   (    (    s6   /tmp/pip-build-X4mzal/theano/theano/tensor/nnet/opt.pyt   local_abstractconv_checkT  s    t   AbstractConvCheckt   namegš™™™™YH@(H   t   __doc__t
   __future__R    R   R   R1   R   R   t   theano.compileR   t
   theano.gofR   t   theano.gof.optR   t   theano.tensor.nnet.corrR   R	   R
   t   theano.tensor.nnet.corr3dR   R   R   t   theano.tensor.nnet.blocksparseR   R   R   R   t    theano.tensor.nnet.abstract_convR   R   R   R   R   R   R   t   theano.tensor.optR   t   theano.tensorR   R   t   theano.tensor.nnet.convR   R   t   theano.tensor.nnet.Conv3DR   t   theano.tensor.nnet.ConvGrad3DR   t   theano.tensor.nnet.ConvTransp3DR    R^   R)   t   registert   TopoOptimizert   warn_inplaceR,   R:   R;   RA   RB   RC   RD   RI   RM   Ro   Rq   Rz   R{   t   LocalGroupDBt   conv_groupoptR€   R   t   in2out(    (    (    s6   /tmp/pip-build-X4mzal/theano/theano/tensor/nnet/opt.pyt   <module>   s®   "!
!
v!Y!					
	
	

	
	
	