ó
àÆ÷Xc           @` s®  d  d l  m Z m Z m 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
 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 d  d	 l m Z d  d
 l m Z d  d l m Z d  d l Z d e j f d „  ƒ  YZ d „  Z d „  Z d „  Z d „  Z d „  Z  d „  Z! d „  Z" e# Z$ e$ rd „  Z% d „  Z& d „  Z' d „  Z( n  d e j) f d „  ƒ  YZ* d „  Z+ d „  Z, d S(   i    (   t   absolute_importt   print_functiont   divisionN(   t   SkipTest(   t
   itervalues(   t   function(   t   vm(   t   OpWiseCLinker(   t   xrange(   t   Mode(   t   tensor(   t   ifelset   TestCallbacksc           B` s2   e  Z d  Z d „  Z d „  Z d „  Z d „  Z RS(   sT   
    Test the VM_Linker's callback argument, which can be useful for debugging.
    c         C` s   i  |  _  d  S(   N(   t   n_callbacks(   t   self(    (    s8   /tmp/pip-build-X4mzal/theano/theano/gof/tests/test_vm.pyt   setUp   s    c         C` s9   | j  j j } |  j j | d ƒ |  j | c d 7<d  S(   Ni    i   (   t   opt	   __class__t   __name__R   t
   setdefault(   R   t   nodet   thunkt   storage_mapt   compute_mapt   key(    (    s8   /tmp/pip-build-X4mzal/theano/theano/gof/tests/test_vm.pyt   callback   s    c         C` sê   t  j d ƒ \ } } } t | | | g | | | d t d d  d t j d |  j ƒ ƒ ƒ} | d d d ƒ t |  j	 j
 ƒ  ƒ t | j j j ƒ  ƒ k sŸ t ‚ | d d d ƒ t |  j	 j
 ƒ  ƒ t | j j j ƒ  ƒ d k sæ t ‚ d  S(	   Nt   abct   modet	   optimizert   linkerR   i   i   i   (   R
   t   scalarsR   R	   t   NoneR   t	   VM_LinkerR   t   sumR   t   valuest   lent   makert   fgrapht   toposortt   AssertionError(   R   t   at   bt   ct   f(    (    s8   /tmp/pip-build-X4mzal/theano/theano/gof/tests/test_vm.pyt   test_callback"   s    3c         C` s•   t  j d ƒ \ } } } t | | | g t | d | d | ƒ d t d d  d t j d |  j ƒ ƒ ƒ} | d d d ƒ |  j	 d	 d k s‘ t
 ‚ d  S(
   NR   i   R   R   R   R   i   i   t   IfElse(   R
   R   R   R   R	   R   R   R    R   R   R'   (   R   R(   R)   R*   R+   (    (    s8   /tmp/pip-build-X4mzal/theano/theano/gof/tests/test_vm.pyt   test_callback_with_ifelse/   s    )(   R   t
   __module__t   __doc__R   R   R,   R.   (    (    (    s8   /tmp/pip-build-X4mzal/theano/theano/gof/tests/test_vm.pyR      s
   			c          C` s3  t  j d ƒ }  t  j d ƒ \ } } t g } t j j rI | j t ƒ n  xã | D]Û } t	 |  | | g t
 |  |  | | | ƒ d t d d  d t j d | d t ƒ ƒ ƒ} | d d	 g d
 d	 g ƒ d d l m } | t | d d	 g d
 d g ƒ t g  | j j D] } t | d ƒ ^ qƒ | k sP t ‚ qP Wd  S(   NR(   t   bcR   R   R   t   c_thunkst	   use_cloopi   i   i   i    (   t   assert_raisesi   t   cthunk(   R
   R   t   vectorst   Falset   theanot   configt   cxxt   appendt   TrueR   R   R	   R   R   R    t
   nose.toolsR4   t
   ValueErrort   anyt   fnt   thunkst   hasattrR'   (   R(   R)   R*   t   casesR2   R+   R4   t   t(    (    s8   /tmp/pip-build-X4mzal/theano/theano/gof/tests/test_vm.pyt   test_c_thunks:   s    	)c          ` s¥   t  j j s t d ƒ ‚ n  d d „ ‰  d „  ‰ ‡ f d †  }  ‡  f d †  } | d t ƒ | d t j ƒ | d	 d
 „  ƒ t  j j rš | d d „  ƒ n  |  ƒ  d  S(   Ns0   G++ not available, so we need to skip this test.i   c         S` s+   |  } x t  | ƒ D] } | | } q W| S(   N(   t   range(   t   xt   deptht   zt   d(    (    s8   /tmp/pip-build-X4mzal/theano/theano/gof/tests/test_vm.pyt   build_graphP   s    c         S` s+   |  } x t  | ƒ D] } | | } q W| S(   N(   R   (   RG   RH   RI   RJ   (    (    s8   /tmp/pip-build-X4mzal/theano/theano/gof/tests/test_vm.pyt   numpy_versionV   s    c    	      ` s¦   d }  d } t  j d d g d t j j ƒ} ˆ  | |  ƒ t j ƒ  } t j ƒ  } t j ƒ  } t j ƒ  } | | } | | } t d d d | | | |  f ƒ d  S(	   Ni   id   g       @g      @t   dtypes   %s takes %f s/Kopt   numpyiè  (   t   npt   asarrayR8   R9   t   floatXt   timet   print(	   t   steps_at   steps_bRG   t   t0t   t1t   t2t   t3t   t_at   t_b(   RL   (    s8   /tmp/pip-build-X4mzal/theano/theano/gof/tests/test_vm.pyt
   time_numpy\   s    !

c      	   ` s6  d } d } t  j ƒ  } ˆ  | | ƒ } ˆ  | | ƒ } t | g | d t d d  d | ƒ  ƒ ƒ} t | g | d t d d  d | ƒ  ƒ ƒ} | d d g ƒ t j ƒ  }	 | d d g ƒ t j ƒ  }
 | d d g ƒ t j ƒ  } | d d g ƒ t j ƒ  } |
 |	 } | | } t d |  d	 | | | | f ƒ d  S(
   Ni   id   R   R   R   g       @g      @s   %s takes %f s/Kopiè  (   R
   t   vectorR   R	   R   RR   RS   (   t   nameR   RT   RU   RG   R(   R)   t   f_at   f_bRV   RW   RX   RY   RZ   R[   (   RK   (    s8   /tmp/pip-build-X4mzal/theano/theano/gof/tests/test_vm.pyt   time_linkero   s,    

s   c|pyt   vmLinkert   vmLinker_nogcc           S` s   t  j d t ƒ S(   Nt   allow_gc(   R   R    R7   (    (    (    s8   /tmp/pip-build-X4mzal/theano/theano/gof/tests/test_vm.pyt   <lambda>   s    t   vmLinker_CLOOPc           S` s   t  j d t d t ƒ S(   NRd   R3   (   R   R    R7   R<   (    (    (    s8   /tmp/pip-build-X4mzal/theano/theano/gof/tests/test_vm.pyRe   ‘   s   (   R8   R9   R:   R   R   R   R    (   R\   Ra   (    (   RK   RL   s8   /tmp/pip-build-X4mzal/theano/theano/gof/tests/test_vm.pyt
   test_speedL   s    	c          ` s^   d d „ ‰  ‡  f d †  }  |  d t  j ƒ |  d d „  ƒ t j j rZ |  d d „  ƒ n  d  S(	   Ni   c         S` s>   |  } x1 t  | ƒ D]# } t | d d k | | ƒ } q W| S(   Ni    (   RF   R   (   RG   RH   RI   RJ   (    (    s8   /tmp/pip-build-X4mzal/theano/theano/gof/tests/test_vm.pyRK   ˜   s    !c      	   ` s*  d } d } t  j ƒ  } ˆ  | | ƒ } ˆ  | | ƒ } t | g | d t d d  d | ƒ  ƒ ƒ} t | g | d t d d  d | ƒ  ƒ ƒ} | d g ƒ t j ƒ  }	 | d g ƒ t j ƒ  }
 | d g ƒ t j ƒ  } | d g ƒ t j ƒ  } |
 |	 } | | } t d |  d | | | | f ƒ d  S(	   Ni
   id   R   R   R   g       @s   %s takes %f s/Kopiè  (   R
   R]   R   R	   R   RR   RS   (   R^   R   RT   RU   RG   R(   R)   R_   R`   RV   RW   RX   RY   RZ   R[   (   RK   (    s8   /tmp/pip-build-X4mzal/theano/theano/gof/tests/test_vm.pyRa   ž   s0    

Rb   Rc   c           S` s   t  j d t ƒ S(   NRd   (   R   R    R7   (    (    (    s8   /tmp/pip-build-X4mzal/theano/theano/gof/tests/test_vm.pyRe   ¿   s    t
   vmLinker_Cc           S` s   t  j d t d t ƒ S(   NRd   R3   (   R   R    R7   R<   (    (    (    s8   /tmp/pip-build-X4mzal/theano/theano/gof/tests/test_vm.pyRe   Á   s   (   R   R    R8   R9   R:   (   Ra   (    (   RK   s8   /tmp/pip-build-X4mzal/theano/theano/gof/tests/test_vm.pyt   test_speed_lazy–   s     c          ` sd   d d l  m ‰  ‡  f d †  }  |  t j d t d t ƒ ƒ t j j sV t	 d ƒ ‚ n  |  d ƒ d  S(   Ni    (   t   unittest_toolsc      	   ` sï   t  j d ƒ } | d } t j | g | d | d | d g d t d d  d |  ƒ ƒ} | d	 d
 d d d g ƒ| d	 ƒ k s… t ‚ | d d
 d d g ƒ| d ƒ d | d ƒ d g k sÃ t ‚ ˆ  j | d ƒ t j	 d d d g ƒ ƒ d  S(   Nt   inputi   i   i	   g      ,@R   R   R   i   t   output_subseti    i   i   i   g      @@g      0@g%I’$I’ü?(
   R
   t   scalarR8   R   R	   R   R'   t   assert_allcloseRO   t   array(   t   linker_nameRG   t   yR+   (   t   utt(    s8   /tmp/pip-build-X4mzal/theano/theano/gof/tests/test_vm.pyt   check_partial_functionÈ   s    
-->t   allow_partial_evalR3   s   Need cxx for this testt   cvm(
   t   theano.testsRj   R   R    R<   R7   R8   R9   R:   R   (   Rs   (    (   Rr   s8   /tmp/pip-build-X4mzal/theano/theano/gof/tests/test_vm.pyt   test_partial_functionÅ   s    
c          C` sN   d „  }  |  t  j d t d t ƒ ƒ t j j s@ t d ƒ ‚ n  |  d ƒ d  S(   Nc      	   S` sŒ   t  j d ƒ } d | } t j | g i | d d 6| d d 6d t d d  d	 |  ƒ ƒ} | d d
 d g ƒd | d ƒ d k sˆ t ‚ d  S(   NRk   i   i   R(   i   R)   R   R   R   Rl   (   R
   Rm   R8   R   R	   R   R'   (   Rp   RG   Rq   R+   (    (    s8   /tmp/pip-build-X4mzal/theano/theano/gof/tests/test_vm.pyt"   check_partial_function_output_keysÚ   s
    
.Rt   R3   s   Need cxx for this testRu   (   R   R    R<   R7   R8   R9   R:   R   (   Rx   (    (    s8   /tmp/pip-build-X4mzal/theano/theano/gof/tests/test_vm.pyt&   test_partial_function_with_output_keysØ   s
    	c          C` sN   d „  }  |  t  j d t d t ƒ ƒ t j j s@ t d ƒ ‚ n  |  d ƒ d  S(   Nc         S` sO  t  j d ƒ } t j t j d d ƒ d d ƒ} t j | g | | d g d | | d f g d t d	 d  d
 |  ƒ ƒ} t j | g | d g d | | d f g d t d	 d  d
 |  ƒ ƒ} | d d g  ƒg  k sÙ t	 ‚ | j
 ƒ  d k sñ t	 ‚ | d d d g ƒd g k st	 ‚ | d d g  ƒg  k s3t	 ‚ | j
 ƒ  d k sKt	 ‚ d  S(   NRk   i   t   int64R^   t   globali"   t   updatesR   R   R   i   i   Rl   i   i   i    i   i(   i
   (   R
   t   lscalarR8   t   sharedRO   RP   R   R	   R   R'   t	   get_value(   Rp   RG   Rq   R+   t   g(    (    s8   /tmp/pip-build-X4mzal/theano/theano/gof/tests/test_vm.pyt   check_updatesê   s    !52$Rt   R3   s   Need cxx for this testRu   (   R   R    R<   R7   R8   R9   R:   R   (   R   (    (    s8   /tmp/pip-build-X4mzal/theano/theano/gof/tests/test_vm.pyt"   test_partial_function_with_updatesè   s
    	c          C` sZ  t  j j }  |  d k r! d }  n  t  j j ƒ  } t  j | g | d d |  ƒ} | d g ƒ t | j j j	 ƒ d j
 d } | j j | d d  k sœ t ‚ | j j t k s´ t ‚ t | j _ | j j t k sØ t ‚ | d g ƒ | j j | d d  k	 st ‚ t | j _ | j j t k s)t ‚ | d g ƒ | j j | d d  k sVt ‚ d  S(   Nt
   DEBUG_MODEt	   DebugModet   FAST_RUNi   R   i    (   Rƒ   R„   (   R8   R9   R   R
   R]   R   t   listR$   R%   t   apply_nodest   outputsR@   R   R   R'   Rd   R<   R7   (   R   t   vR+   t   n(    (    s8   /tmp/pip-build-X4mzal/theano/theano/gof/tests/test_vm.pyt   test_allow_gc_cvmþ   s"    	   c          C` sÞ   d d  l  j j }  xÅ t d ƒ D]· } t j d d g d d ƒ} t j | ƒ } |  j | ƒ } | j	 ƒ  | t j | ƒ k sƒ t
 ‚ ~ | d sÉ t d d	 d
 ƒt t j ƒ  d	 d
 ƒt t j ƒ  ƒ n  t j j ƒ  q Wd  S(   Ni    i@B gffffff@g      @RM   R+   iè  t   .t   endt    (   t   theano.sandbox.cudat   sandboxt   cudaR   RO   RP   t   syst   getrefcountt   CudaNdarrayR!   R'   RS   t   gct   collectt   stdoutt   flush(   R‘   t   iRŠ   R*   R(   (    (    s8   /tmp/pip-build-X4mzal/theano/theano/gof/tests/test_vm.pyt
   test_leak2  s    

c       
   C` sç   xà t  d ƒ D]Ò }  t j ƒ  } | } x( t d ƒ D] } t j | d ƒ } q2 Wt | g | d t d d  d d ƒ ƒ} |  d s” t t	 j
 ƒ  ƒ n  t j j ƒ  t	 j
 ƒ  | d	 g ƒ | d
 g ƒ | d g ƒ | d g ƒ q Wd  S(   Ni'  i
   i   R   R   R   Ru   id   g       @g      @g      @g      @(   R   R
   R]   RF   t   sinR   R	   R   RS   R•   R–   R’   R—   R˜   (   R™   RG   RI   RJ   R+   (    (    s8   /tmp/pip-build-X4mzal/theano/theano/gof/tests/test_vm.pyt   test_no_leak_many_graphs)  s    '

c          ` sS   d d „ ‰  ‡  f d †  }  t  d ƒ |  d d „  ƒ t  d ƒ |  d d	 „  ƒ d  S(
   Ni   c         S` s@   |  } x3 t  | ƒ D]% } t | j ƒ  d k | | ƒ } q W| S(   Ng      à?(   RF   R   t   mean(   RG   RH   RI   RJ   (    (    s8   /tmp/pip-build-X4mzal/theano/theano/gof/tests/test_vm.pyRK   F  s    #c      	   ` s‚   d } t  j ƒ  } ˆ  | | ƒ } t | g | d t d d  d | ƒ  ƒ ƒ} t j j d ƒ } x t d ƒ D] } | | ƒ qj Wd  S(   Ni
   R   R   R   i@B id   (	   R
   t   dvectorR   R	   R   RO   t   randomt   randR   (   R^   R   RT   RG   R(   R_   t   inpR™   (   RK   (    s8   /tmp/pip-build-X4mzal/theano/theano/gof/tests/test_vm.pyRa   L  s    i   Rh   c           S` s   t  j d t d t ƒ S(   NRd   R3   (   R   R    R7   R<   (    (    (    s8   /tmp/pip-build-X4mzal/theano/theano/gof/tests/test_vm.pyRe   `  s    i   Rb   c           S` s   t  j d t d t ƒ S(   NRd   R3   (   R   R    R7   (    (    (    s8   /tmp/pip-build-X4mzal/theano/theano/gof/tests/test_vm.pyRe   c  s    (   RS   (   Ra   (    (   RK   s8   /tmp/pip-build-X4mzal/theano/theano/gof/tests/test_vm.pyt   test_no_leak_many_call_lazy@  s    


c          ` sS   d d „ ‰  ‡  f d †  }  t  d ƒ |  d d „  ƒ t  d ƒ |  d d	 „  ƒ d  S(
   Ni   c         S` s5   |  } x( t  | ƒ D] } t j | d ƒ } q W| S(   Ni   (   RF   R
   R›   (   RG   RH   RI   RJ   (    (    s8   /tmp/pip-build-X4mzal/theano/theano/gof/tests/test_vm.pyRK   k  s    c      	   ` s‚   d } t  j ƒ  } ˆ  | | ƒ } t | g | d t d d  d | ƒ  ƒ ƒ} t j j d ƒ } x t d ƒ D] } | | ƒ qj Wd  S(   Ni
   R   R   R   i@B iô  (	   R
   Rž   R   R	   R   RO   RŸ   R    R   (   R^   R   RT   RG   R(   R_   R¡   R™   (   RK   (    s8   /tmp/pip-build-X4mzal/theano/theano/gof/tests/test_vm.pyRa   q  s    i   Rh   c           S` s   t  j d t d t ƒ S(   NRd   R3   (   R   R    R7   R<   (    (    (    s8   /tmp/pip-build-X4mzal/theano/theano/gof/tests/test_vm.pyRe   ~  s    i   Rb   c           S` s   t  j d t d t ƒ S(   NRd   R3   (   R   R    R7   (    (    (    s8   /tmp/pip-build-X4mzal/theano/theano/gof/tests/test_vm.pyRe     s    (   RS   (   Ra   (    (   RK   s8   /tmp/pip-build-X4mzal/theano/theano/gof/tests/test_vm.pyt   test_no_leak_many_call_nonlazye  s    


t   RunOncec           B` s)   e  Z d Z d „  Z d „  Z d „  Z RS(   t   nb_runc         C` s   d |  _  d  S(   Ni    (   R¥   (   R   (    (    s8   /tmp/pip-build-X4mzal/theano/theano/gof/tests/test_vm.pyt   __init__ˆ  s    c         C` s   t  j |  | g | j ƒ  g ƒ S(   N(   R8   t   Applyt   type(   R   RG   (    (    s8   /tmp/pip-build-X4mzal/theano/theano/gof/tests/test_vm.pyt	   make_node‹  s    c         C` s@   |  j  d k s t ‚ |  j  d 7_  | d j ƒ  | d d <d  S(   Ni    i   (   R¥   R'   t   copy(   R   R   t   inputsRˆ   (    (    s8   /tmp/pip-build-X4mzal/theano/theano/gof/tests/test_vm.pyt   performŽ  s    (   s   nb_run(   R   R/   t	   __props__R¦   R©   R¬   (    (    (    s8   /tmp/pip-build-X4mzal/theano/theano/gof/tests/test_vm.pyR¤   „  s   		c          C` sß   t  j j ƒ  }  t ƒ  |  ƒ } t  j d t  j j j d t ƒ ƒ } t  j	 t  j
 |  d t ƒg | d | d g d | ƒ} | d d d g ƒ t ƒ  |  ƒ } | | } t  j	 |  g | | g d | ƒ} | d d d g ƒ d S(	   sŽ   This already caused a bug in the trunk of Theano.

    The bug was introduced in the trunk on July 5th, 2012 and fixed on
    July 30th.

    R   t   lazyt   mutablei   i   R   i   N(   R8   R
   R]   R¤   R	   t   gofR   R    R<   R   t   In(   RG   t   pR   R+   t   pp(    (    s8   /tmp/pip-build-X4mzal/theano/theano/gof/tests/test_vm.pyt
   test_vm_gc”  s    $/	
	c    	      C` sc  t  j d ƒ }  t  j d ƒ } t  j d |  | ƒ t  j |  d | ƒ } xt j d t d t d t ƒ t j d t d t d t ƒ g D]Ô } t j	 j
 t j d | ƒ ƒ } | j d	 d
 ƒ } t j |  | g | d d d | ƒ} | d d ƒ } | sü t ‚ | j j } d „  } | | ƒ d s't ‚ t t d „  t | ƒ Dƒ ƒ ƒ t | ƒ k  s‡ t ‚ q‡ Wd  S(   NRG   Rq   i   i   Rd   R®   R3   R   t   fusiont   inplaceR^   t   test_reduce_memoryR   i   i   c         S` sª   d d l  m } x |  D]… } t | | ƒ s t |  j ƒ  ƒ } | j | ƒ xM | D]B } |  | d rS |  | d |  | d k rS t |  | d g SqS Wq q Wt d  g S(   Ni    (   t   TensorConstant(	   t   theano.tensor.varR¸   t
   isinstanceR†   t   keyst   removeR<   R7   R   (   R   R¸   R™   t	   keys_copyt   o(    (    s8   /tmp/pip-build-X4mzal/theano/theano/gof/tests/test_vm.pyt   check_storage¹  s    i    c         s` s   |  ] } t  | ƒ Vq d  S(   N(   t   id(   t   .0R‰   (    (    s8   /tmp/pip-build-X4mzal/theano/theano/gof/tests/test_vm.pys	   <genexpr>Æ  s    (   R
   Rm   t   tanht   coshR   R    R7   R<   R8   t   compilet   get_modeR	   t	   excludingR   R'   R@   R   R#   t   setR   (	   RG   Rq   RI   t   lt   mR+   t   outputR   R¿   (    (    s8   /tmp/pip-build-X4mzal/theano/theano/gof/tests/test_vm.pyt   test_reallocation©  s     ,%		(-   t
   __future__R    R   R   R•   R’   RR   t   unittestt   nose.plugins.skipR   RN   RO   t   sixR   R8   R   t
   theano.gofR   R   t	   six.movesR   t   theano.compileR	   R
   t   theano.ifelseR   t   TestCaseR   RE   Rg   Ri   Rw   Ry   R‚   R‹   R7   t   run_memory_usage_testsRš   Rœ   R¢   R£   t   OpR¤   R´   RË   (    (    (    s8   /tmp/pip-build-X4mzal/theano/theano/gof/tests/test_vm.pyt   <module>   s@   $		J	/							%	