ó
àÆ÷Xc           @` sû  d  d l  m Z m Z m Z d  d l m Z d  d l Z d  d l Z d  d l	 m
 Z
 d  d l	 m Z d  d l	 Z	 d  d l Z	 d  d l m Z d  d l m Z d  d l Z	 d  d l m Z d	 „  Z d
 e j f d „  ƒ  YZ e e ƒ Z e e ƒ Z d e j f d „  ƒ  YZ e d ƒ Z e d ƒ Z e d ƒ Z e d ƒ Z d „  Z  d „  Z! d „  Z" d „  Z# d „  Z$ d „  Z% d „  Z& d e j' f d „  ƒ  YZ( d e j' f d „  ƒ  YZ) d e j f d „  ƒ  YZ* d e j f d  „  ƒ  YZ+ d! e j' f d" „  ƒ  YZ, d S(#   i    (   t   absolute_importt   print_functiont   division(   t   SkipTestN(   t   config(   t   gof(   t   exc_message(   t	   debugmode(   t   unittest_toolsc          C` sP   t  j j ƒ  }  t  j |  g d |  d d d t j ƒ  ƒ} | d d g ƒ d  S(   Ng       @i   t   modei   i   (   t   theanot   tensort   dvectort   functionR   t	   DebugMode(   t   xt   f(    (    sC   /tmp/pip-build-X4mzal/theano/theano/compile/tests/test_debugmode.pyt   test0   s    -t   BROKEN_ON_PURPOSE_Addc           B` s;   e  Z d Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   t	   py_offsetc         C` s   t  j j |  ƒ | |  _ d  S(   N(   R   t   Opt   __init__R   (   t   selfR   (    (    sC   /tmp/pip-build-X4mzal/theano/theano/compile/tests/test_debugmode.pyR      s    c         C` sš   t  j j | ƒ } t  j j | ƒ } | j j d k s< t ‚ | j j | j j k sZ t ‚ | j j d k sr t ‚ t j |  | | g | j ƒ  g ƒ } | S(   Nt   float64i   (	   R
   R   t   as_tensor_variablet   typet   dtypet   AssertionErrort   ndimR   t   Apply(   R   t   at   bt   r(    (    sC   /tmp/pip-build-X4mzal/theano/theano/compile/tests/test_debugmode.pyt	   make_node   s    $c         C` sG   | \ } } | \ } | | } |  j  r9 | d | d <n
 | | d <d  S(   Ng      à?i    (   R   (   R   t   nodet   inpt   out_R   R   t   outt   z(    (    sC   /tmp/pip-build-X4mzal/theano/theano/compile/tests/test_debugmode.pyt   perform(   s    	
	c         C` s   d S(   Ni   (   i   (    (   R   (    (    sC   /tmp/pip-build-X4mzal/theano/theano/compile/tests/test_debugmode.pyt   c_code_cache_version2   s    c   	      C` s)   | \ } } | \ } d t  t ƒ  |  S(   Ns6  
        if (PyArray_NDIM(%(a)s) != 1) {PyErr_SetString(PyExc_NotImplementedError, "rank(a) != 1"); %(fail)s;}
        if (PyArray_NDIM(%(b)s) != 1) {PyErr_SetString(PyExc_NotImplementedError, "rank(b) != 1"); %(fail)s;}

        if (PyArray_DESCR(%(a)s)->type_num != NPY_DOUBLE)
        {PyErr_SetString(PyExc_NotImplementedError, "a dtype not NPY_DOUBLE"); %(fail)s;}

        if (PyArray_DESCR(%(b)s)->type_num != NPY_DOUBLE)
        {PyErr_SetString(PyExc_NotImplementedError, "b's dtype not NPY_DOUBLE"); %(fail)s;}

        if (PyArray_DIMS(%(a)s)[0] != PyArray_DIMS(%(b)s)[0])
        {PyErr_SetString(PyExc_NotImplementedError, "a and b have different lengths"); %(fail)s;}

        if ((!%(z)s)
            || (PyArray_DIMS(%(z)s)[0] != PyArray_DIMS(%(b)s)[0])
            )
        {
            {Py_XDECREF(%(z)s);}
            npy_intp dims[] = {0};
            dims[0] = PyArray_DIMS(%(b)s)[0];
            %(z)s = (PyArrayObject*) PyArray_SimpleNew(1, dims, PyArray_DESCR(%(b)s)->type_num);
        }

        {
            for (npy_intp m = 0; m < PyArray_DIMS(%(z)s)[0]; ++m)
            {
                ((double*)PyArray_GETPTR1(%(z)s, m))[0]
                = 0.5
                + ((double*)PyArray_GETPTR1(%(a)s, m))[0]
                + ((double*)PyArray_GETPTR1(%(b)s, m))[0] ;
            }
        }
        (   t   dictt   locals(	   R   R"   t   nameR#   R%   t   subR   R   R&   (    (    sC   /tmp/pip-build-X4mzal/theano/theano/compile/tests/test_debugmode.pyt   c_code5   s    	!(   s	   py_offset(   t   __name__t
   __module__t	   __props__R   R!   R'   R(   R-   (    (    (    sC   /tmp/pip-build-X4mzal/theano/theano/compile/tests/test_debugmode.pyR      s   				
	t   WeirdBrokenOpc           B` sA   e  Z d  Z d Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   sù   
    This op can be inplace if behaviour is 'times1_inplace'
    This op can be destructive if behaviour is 'times2_inplace'

    In both cases, it does not set the destroy_map or view_map correctly so
    it should raise an error in DebugMode.
    t	   behaviourc         C` s   t  j j |  ƒ | |  _ d  S(   N(   R   R   R   R2   (   R   R2   (    (    sC   /tmp/pip-build-X4mzal/theano/theano/compile/tests/test_debugmode.pyR   k   s    c         C` s7   t  j j | ƒ } t j |  | g | j ƒ  g ƒ } | S(   N(   R
   R   R   R   R   R   (   R   R   t   a_R    (    (    sC   /tmp/pip-build-X4mzal/theano/theano/compile/tests/test_debugmode.pyR!   o   s    !c         C` s­   | \ } | \ } |  j  d k r2 | d | d <nw |  j  d k r^ | | d <| d c d 9<nK |  j  d k r~ | d | d <n+ |  j  d k rš | | d <n t |  j  ƒ ‚ d  S(   Nt   times2i   i    t   times2_inplacet   times1i   t   times1_inplace(   R2   t
   ValueError(   R   R"   R#   R$   R   R%   (    (    sC   /tmp/pip-build-X4mzal/theano/theano/compile/tests/test_debugmode.pyt   dontuse_performt   s    		
c         C` s   d S(   Ni   (   i   (    (   R   (    (    sC   /tmp/pip-build-X4mzal/theano/theano/compile/tests/test_debugmode.pyR(   ƒ   s    c         C` sÑ   | \ } | \ } d |  j  k r* d } n d } d }	 |  j  d k rN d }
 nW |  j  d k rf d }
 n? |  j  d k r~ d	 }
 n' |  j  d
 k r– d }
 n t |  j  ƒ ‚ d } | |	 |
 | t t ƒ  |  } | S(   Nt   inplacesg   
            {Py_XDECREF(%(z)s);}
            Py_INCREF(%(a)s);
            %(z)s = %(a)s;
            sž   
            {Py_XDECREF(%(z)s);}
            %(z)s = (PyArrayObject*) PyArray_SimpleNew(1, PyArray_DIMS(%(a)s), PyArray_DESCR(%(a)s)->type_num);
            sô  
            //the output array has size M x N
            npy_intp M = PyArray_DIMS(%(a)s)[0];
            npy_intp Sa = PyArray_STRIDES(%(a)s)[0] / PyArray_DESCR(%(a)s)->elsize;
            npy_intp Sz = PyArray_STRIDES(%(z)s)[0] / PyArray_DESCR(%(z)s)->elsize;

            npy_double * Da = (npy_double*)PyArray_BYTES(%(a)s);
            npy_double * Dz = (npy_double*)PyArray_BYTES(%(z)s);

            //clear the output array
            for (npy_intp m = 0; m < M; ++m)
            {
        R4   s"        Dz[m * Sz] = 2 * Da[m * Sa]; R5   R6   s        Dz[m * Sz] = Da[m * Sa]; R7   t    s   
            }
        (   R2   R8   R)   R*   (   R   R"   R+   R#   R%   R,   R   R&   t   z_codet	   prep_varsR2   t
   prep_vars2t   total(    (    sC   /tmp/pip-build-X4mzal/theano/theano/compile/tests/test_debugmode.pyR-   †   s&    							(   s	   behaviour(	   R.   R/   t   __doc__R0   R   R!   R9   R(   R-   (    (    (    sC   /tmp/pip-build-X4mzal/theano/theano/compile/tests/test_debugmode.pyR1   a   s   				R5   R4   R7   R6   c          C` s2  t  j j ƒ  }  t  j j ƒ  } t  j |  | g t |  | ƒ d t j d t  j j ƒ ƒ} t  j |  | g t	 |  | ƒ d t j d t  j j ƒ ƒ} | d d d g d d d g ƒ t  j j sÊ t
 d	 ƒ ‚ n  y# | d d d g d d d g ƒ Wn2 t j k
 r!} | j j j t	 k st ‚ d  SXt s.t ‚ d  S(
   NR	   t   check_c_codeg      ð?g       @g      @i   i   i   s0   G++ not available, so we need to skip this test.(   R
   R   R   R   t   off_by_halfR   R   R   t   cxxt   inconsistentR   t   BadThunkOutputR    t   ownert   opR   t   False(   R   R   t   f_goodt   f_inconsistentt   e(    (    sC   /tmp/pip-build-X4mzal/theano/theano/compile/tests/test_debugmode.pyt   test_badthunkoutputÀ   s"    #c          C` s  t  j t j j g ƒ d „  ƒ }  t  j ƒ  } | j d |  d ƒ | j d ƒ } t j j ƒ  } t j j ƒ  } t j	 | | g | | d t
 j d | ƒ ƒ} y# | d d d	 g d
 d d g ƒ Wn2 t
 j k
 rò } t | j ƒ d k sî t ‚ d  SXt sÿ t ‚ d  S(   Nc         S` s)   |  j  t j j k r% t |  j Œ  g St S(   N(   RG   R
   R   t   addRB   t   inputsRH   (   R"   (    (    sC   /tmp/pip-build-X4mzal/theano/theano/compile/tests/test_debugmode.pyt   insert_broken_addÜ   s    RO   t   alls   +allR	   t	   optimizerg      ð?g       @g      @i   i   i   (   R   t   local_optimizerR
   R   RM   t   EquilibriumDBt   registert   queryR   R   R   R   t   BadOptimizationt   strt   reasonR   RH   (   RO   t   edbt   optR   R   R   RK   (    (    sC   /tmp/pip-build-X4mzal/theano/theano/compile/tests/test_debugmode.pyt   test_badoptimizationÛ   s    !#c          C` sý   t  j t j j g ƒ d „  ƒ }  t  j ƒ  } | j d |  d ƒ | j d ƒ } t j j ƒ  } t j j ƒ  } t j	 | | g | | d t
 j d | ƒ ƒ} y# | d d d	 g d
 d d g ƒ Wn, t k
 rì } d t | ƒ k sè t ‚ d SXt sù t ‚ d S(   sy   This variant of test_badoptimization() replace the working code
    with a new apply node that will raise an error.

    c         S` sr   |  j  t j j k rn t |  j ƒ } | d j d  k rn t j j | d | d f ƒ | d <|  j  | Œ  g Sn  t	 S(   Niÿÿÿÿ(
   RG   R
   R   RM   t   listRN   RF   t   Nonet   concatenateRH   (   R"   RN   (    (    sC   /tmp/pip-build-X4mzal/theano/theano/compile/tests/test_debugmode.pyt   insert_bigger_b_addù   s    R_   RP   s   +allR	   RQ   g      ð?g       @g      @i   i   i   N(   R   RR   R
   R   RM   RS   RT   RU   R   R   R   R   t	   ExceptionR   R   RH   (   R_   RY   RZ   R   R   R   RK   (    (    sC   /tmp/pip-build-X4mzal/theano/theano/compile/tests/test_debugmode.pyt   test_badoptimization_opt_errô   s    !	#c          ` sú   t  g ‰  t j t j j g ƒ ‡  f d †  ƒ }  t j ƒ  } | j d |  d ƒ | j d ƒ } t j j	 ƒ  } t j j	 ƒ  } yV t j
 | | g t j j | | ƒ d t j d | d t d t d	 t j j ƒ ƒ ƒWn t j k
 ré d  SXt  sö t ‚ d  S(
   Nc         ` sE   |  j  t j j k rA ˆ  d ˆ  d <ˆ  d rA t |  j Œ  g Sn  t S(   Ni    (   RG   R
   R   RM   RB   RN   RH   (   R"   (   t   last_time_replaced(    sC   /tmp/pip-build-X4mzal/theano/theano/compile/tests/test_debugmode.pyt   insert_broken_add_sometimes  s
    
Rc   RP   s   +allR	   RQ   RA   t   stability_patiencei   (   RH   R   RR   R
   R   RM   RS   RT   RU   R   R   R   R   t   Truet   maxR   t   patiencet   StochasticOrderR   (   Rc   RY   RZ   R   R   (    (   Rb   sC   /tmp/pip-build-X4mzal/theano/theano/compile/tests/test_debugmode.pyt   test_stochasticoptimization  s(    	'	c          C` sˆ   t  j j s t d ƒ ‚ n  t  j j ƒ  }  t  j |  g t |  ƒ d t j	 d t
 ƒ ƒ} t j | d d g ƒ d d g k ƒ s„ t ‚ d  S(   Ns0   G++ not available, so we need to skip this test.R	   t   check_py_codei   i   i   (   R
   R   RC   R   R   R   R   t   wb2R   R   RH   t   npRP   R   (   R   R   (    (    sC   /tmp/pip-build-X4mzal/theano/theano/compile/tests/test_debugmode.pyt   test_just_c_code9  s    c          C` s¥   d t  j f d „  ƒ  Y}  t j j ƒ  } t j j ƒ  } t j | | g |  ƒ  | | ƒ d d ƒ} y) | d d g d d g ƒ t s‰ t ‚ Wn t j	 k
 r  n Xd  S(	   Nt   BadAddc           B` s   e  Z d  „  Z d „  Z RS(   c         S` s(   | j  ƒ  } t j |  | | g | g ƒ S(   N(   R   R   R   (   R   R   R   t   c(    (    sC   /tmp/pip-build-X4mzal/theano/theano/compile/tests/test_debugmode.pyR!   D  s    c         S` s3   | \ } } | \ } | | d <| d c | 7<d  S(   Ni    (    (   R   R"   R#   R%   R   R   Ro   (    (    sC   /tmp/pip-build-X4mzal/theano/theano/compile/tests/test_debugmode.pyR'   H  s    	
(   R.   R/   R!   R'   (    (    (    sC   /tmp/pip-build-X4mzal/theano/theano/compile/tests/test_debugmode.pyRn   C  s   	R	   t
   DEBUG_MODEi   i   i   i   (
   R   R   R
   R   R   R   RH   R   R   t   BadDestroyMap(   Rn   R   t   yR   (    (    sC   /tmp/pip-build-X4mzal/theano/theano/compile/tests/test_debugmode.pyt   test_baddestroymapB  s    *c          C` s¯   t  j j s t d ƒ ‚ n  t  j j ƒ  }  t  j |  g t |  ƒ d t j	 d t
 ƒ ƒ} y= t j | d d g ƒ d d g k ƒ s‡ t ‚ t
 s“ t ‚ Wn t j k
 rª n Xd  S(   Ns0   G++ not available, so we need to skip this test.R	   Rj   i   i   i   (   R
   R   RC   R   R   R   R   t   wb2iR   R   RH   Rl   RP   R   Rq   (   R   R   (    (    sC   /tmp/pip-build-X4mzal/theano/theano/compile/tests/test_debugmode.pyt   test_baddestroymap_cY  s    -t   Test_ViewMapc           B` s‚   e  Z d  e j f d „  ƒ  YZ d e j f d „  ƒ  YZ d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d	 „  Z d
 „  Z d „  Z RS(   t	   BadAddRefc           B` s   e  Z d  „  Z d „  Z RS(   c         C` s(   | j  ƒ  } t j |  | | g | g ƒ S(   N(   R   R   R   (   R   R   R   Ro   (    (    sC   /tmp/pip-build-X4mzal/theano/theano/compile/tests/test_debugmode.pyR!   i  s    c         C` s#   | \ } } | \ } | | d <d  S(   Ni    (    (   R   R"   R#   R%   R   R   Ro   (    (    sC   /tmp/pip-build-X4mzal/theano/theano/compile/tests/test_debugmode.pyR'   m  s    	(   R.   R/   R!   R'   (    (    (    sC   /tmp/pip-build-X4mzal/theano/theano/compile/tests/test_debugmode.pyRw   h  s   	t   BadAddSlicec           B` s   e  Z d  „  Z d „  Z RS(   c         C` s(   | j  ƒ  } t j |  | | g | g ƒ S(   N(   R   R   R   (   R   R   R   Ro   (    (    sC   /tmp/pip-build-X4mzal/theano/theano/compile/tests/test_debugmode.pyR!   s  s    c         C` s*   | \ } } | \ } | d d !| d <d  S(   Ni   i   i    (    (   R   R"   R#   R%   R   R   Ro   (    (    sC   /tmp/pip-build-X4mzal/theano/theano/compile/tests/test_debugmode.pyR'   w  s    	(   R.   R/   R!   R'   (    (    (    sC   /tmp/pip-build-X4mzal/theano/theano/compile/tests/test_debugmode.pyRx   r  s   	c         C` s   t  j j ƒ  } t  j j ƒ  } t  j | | g |  j ƒ  | | ƒ d d ƒ} y) | d d g d d g ƒ t ss t ‚ Wn t j k
 r‹ d  SXd  S(   NR	   Rp   i   i   i   i   (	   R
   R   R   R   Rw   RH   R   R   t
   BadViewMap(   R   R   Rr   R   (    (    sC   /tmp/pip-build-X4mzal/theano/theano/compile/tests/test_debugmode.pyt   test_badviewmap_ref|  s    -c         C` s   t  j j ƒ  } t  j j ƒ  } t  j | | g |  j ƒ  | | ƒ d d ƒ} y) | d d g d d g ƒ t ss t ‚ Wn t j k
 r‹ d  SXd  S(   NR	   Rp   i   i   i   i   (	   R
   R   R   R   Rx   RH   R   R   Ry   (   R   R   Rr   R   (    (    sC   /tmp/pip-build-X4mzal/theano/theano/compile/tests/test_debugmode.pyt   test_badviewmap_slice†  s    $	c         C` s¸   |  j  ƒ  } i d g d 6| _ t j j ƒ  } t j j ƒ  } t j | | g | | | ƒ d d ƒ} y- | d d d g d d d d d g ƒ d  SWn  t j k
 r³ t s´ t	 ‚ n Xd  S(	   Ni   i    R	   Rp   i   i   i   i   (
   Rw   t   view_mapR
   R   R   R   R   Ry   RH   R   (   R   t   goodopR   Rr   R   (    (    sC   /tmp/pip-build-X4mzal/theano/theano/compile/tests/test_debugmode.pyt   test_goodviewmap‘  s    '%c         C` s’   t  j j s t d ƒ ‚ n  t  j j ƒ  } t  j | g t | ƒ d t j	 d t
 ƒ ƒ} y  | d d g ƒ t
 sv t ‚ Wn t j k
 r n Xd  S(   Ns0   G++ not available, so we need to skip this test.R	   Rj   i   i   (   R
   R   RC   R   R   R   R   t   wb1iR   R   RH   R   Ry   (   R   R   R   (    (    sC   /tmp/pip-build-X4mzal/theano/theano/compile/tests/test_debugmode.pyt   test_badviewmap_c  s    c         C` sã   d t  j f d „  ƒ  Y} t j j d ƒ } t j j d ƒ } t j | | g | ƒ  | | ƒ d d ƒ} | d d d	 d
 g d d d d g ƒ \ } } t j | d d d	 d
 g k ƒ s» t ‚ t j | d d	 d
 g k ƒ sß t ‚ d  S(   Nt   CustomOpc           B` s4   e  Z i d  g d  6d  g d 6Z d „  Z d „  Z RS(   i    i   c         S` s7   | j  ƒ  } | j  ƒ  } t j |  | | g | | g ƒ S(   N(   R   R   R   (   R   R   R   Ro   t   d(    (    sC   /tmp/pip-build-X4mzal/theano/theano/compile/tests/test_debugmode.pyR!   ¯  s    c         S` s4   | \ } } | \ } } | | d <| d | d <d  S(   Ni    i   (    (   R   R"   R#   R%   R   R   Ro   R‚   (    (    sC   /tmp/pip-build-X4mzal/theano/theano/compile/tests/test_debugmode.pyR'   ´  s    
(   R.   R/   R|   R!   R'   (    (    (    sC   /tmp/pip-build-X4mzal/theano/theano/compile/tests/test_debugmode.pyR   ¬  s   	R   Rr   R	   Rp   i   i   i   i   i   i   i   i   (	   R   R   R
   R   R   R   Rl   RP   R   (   R   R   R   Rr   R   t   r0t   r1(    (    sC   /tmp/pip-build-X4mzal/theano/theano/compile/tests/test_debugmode.pyt   test_aliased_outputs_ok©  s    *-'c         C` sÝ   d t  j f d „  ƒ  Y} t j j ƒ  } t j j ƒ  } t j | | g | ƒ  | | ƒ d d ƒ} | d d d d g d	 d
 d d g ƒ \ } } t j | d d d
 d g k ƒ sµ t ‚ t j | d d
 d g k ƒ sÙ t ‚ d  S(   NR   c           B` s   e  Z d  „  Z d „  Z RS(   c         S` s7   | j  ƒ  } | j  ƒ  } t j |  | | g | | g ƒ S(   N(   R   R   R   (   R   R   R   Ro   R‚   (    (    sC   /tmp/pip-build-X4mzal/theano/theano/compile/tests/test_debugmode.pyR!   Ç  s    c   	      S` s>   | \ } } | \ } } | d } | | d <| d | d <d  S(   Ni   i    i   (    (	   R   R"   R#   R%   R   R   Ro   R‚   R    (    (    sC   /tmp/pip-build-X4mzal/theano/theano/compile/tests/test_debugmode.pyR'   Ì  s
    

(   R.   R/   R!   R'   (    (    (    sC   /tmp/pip-build-X4mzal/theano/theano/compile/tests/test_debugmode.pyR   Æ  s   	R	   Rp   i   i   i   i   i   i   i   i   (	   R   R   R
   R   R   R   Rl   RP   R   (   R   R   R   Rr   R   Rƒ   R„   (    (    sC   /tmp/pip-build-X4mzal/theano/theano/compile/tests/test_debugmode.pyt   test_aliased_outputs_ok_outputÃ  s    *-'c         C` sÁ   d t  j f d „  ƒ  Y} t j j d ƒ } t j j d ƒ } t j | | g | ƒ  | | ƒ d d d d ƒ} | d	 d d
 d g d d d d g ƒ } t j | d d d d g k ƒ s½ t ‚ d  S(   NR   c           B` s   e  Z d  „  Z d „  Z RS(   c         S` s7   | j  ƒ  } | j  ƒ  } t j |  | | g | | g ƒ S(   N(   R   R   R   (   R   R   R   Ro   R‚   (    (    sC   /tmp/pip-build-X4mzal/theano/theano/compile/tests/test_debugmode.pyR!   á  s    c   	      S` s>   | \ } } | \ } } | d } | | d <| d | d <d  S(   Ni   i    (    (	   R   R"   R#   R%   R   R   Ro   R‚   R    (    (    sC   /tmp/pip-build-X4mzal/theano/theano/compile/tests/test_debugmode.pyR'   æ  s
    

(   R.   R/   R!   R'   (    (    (    sC   /tmp/pip-build-X4mzal/theano/theano/compile/tests/test_debugmode.pyR   à  s   	R   Rr   i    i   R	   Rp   i   i   i   i   i   i   i   (	   R   R   R
   R   R   R   Rl   RP   R   (   R   R   R   Rr   R   Rƒ   (    (    sC   /tmp/pip-build-X4mzal/theano/theano/compile/tests/test_debugmode.pyt   test_aliased_outputs_ok_shadowÜ  s    2'c   	      C` sÕ   d t  j f d „  ƒ  Y} | ƒ  } t j j ƒ  } t j j ƒ  } | | | ƒ \ } } | d | d } t j | | g | d d ƒ} y5 | d d d d g d	 d
 d d g ƒ t s¹ t ‚ Wn t j	 k
 rÐ n Xd  S(   NR   c           B` s   e  Z d  „  Z d „  Z RS(   c         S` s7   | j  ƒ  } | j  ƒ  } t j |  | | g | | g ƒ S(   N(   R   R   R   (   R   R   R   Ro   R‚   (    (    sC   /tmp/pip-build-X4mzal/theano/theano/compile/tests/test_debugmode.pyR!   ú  s    c   	      S` sB   | \ } } | \ } } | d } | d  | d <| d | d <d  S(   Ni   iÿÿÿÿi    (    (	   R   R"   R#   R%   R   R   Ro   R‚   R    (    (    sC   /tmp/pip-build-X4mzal/theano/theano/compile/tests/test_debugmode.pyR'   ÿ  s
    
(   R.   R/   R!   R'   (    (    (    sC   /tmp/pip-build-X4mzal/theano/theano/compile/tests/test_debugmode.pyR   ù  s   	i   R	   Rp   i   i   i   i   i   i   i   (
   R   R   R
   R   R   R   RH   R   R   Ry   (	   R   R   t	   custom_opR   Rr   t   bad_xy0t   bad_xy1R%   R   (    (    sC   /tmp/pip-build-X4mzal/theano/theano/compile/tests/test_debugmode.pyt   test_aliased_outputs_badõ  s    	%(   R.   R/   R   R   Rw   Rx   Rz   R{   R~   R€   R…   R†   R‡   R‹   (    (    (    sC   /tmp/pip-build-X4mzal/theano/theano/compile/tests/test_debugmode.pyRv   f  s   

	
						t   Test_check_isfinitec           B` s,   e  Z d  „  Z d „  Z d „  Z d „  Z RS(   c         C` s/   t  j j j |  _ t  j j j d j |  _	 d  S(   NRp   (
   R
   R   t
   TensorTypet   filter_checks_isfinitet   old_tst   compileR	   t   predefined_modest   check_isfinitet   old_dm(   R   (    (    sC   /tmp/pip-build-X4mzal/theano/theano/compile/tests/test_debugmode.pyt   setUp  s    c         C` s/   |  j  t j j _ |  j t j j j d _	 d  S(   NRp   (
   R   R
   R   R   RŽ   R“   R   R	   R‘   R’   (   R   (    (    sC   /tmp/pip-build-X4mzal/theano/theano/compile/tests/test_debugmode.pyt   tearDown#  s    c         C` s±  t  j j ƒ  } t  j | g | d d d d ƒ} t  j | g t  j j | ƒ d d ƒ} | t j d d d g ƒ j t j ƒ ƒ |  j	 t
 j | t j d d d g ƒ j t j ƒ ƒ |  j	 t
 j | t j d d	 d g ƒ d j t j ƒ ƒ |  j	 t
 j | t j d	 d	 d	 g ƒ d j t j ƒ ƒ |  j	 t
 j | t j d d d g d
 t j ƒƒ t t  j j _ t t  j j j d _ | t j t j d	 d	 d	 g ƒ d d
 t j ƒƒ d  S(   Ni   i   R	   Rp   i   i   iüÿÿÿi    g      ð?R   (   R
   R   t   vectorR   t   logRl   t   astypeR   t   floatXt   assertRaisesR   t   InvalidValueErrort   asarrayRH   R   RŽ   R   R	   R‘   R’   (   R   R   R   t   g(    (    sC   /tmp/pip-build-X4mzal/theano/theano/compile/tests/test_debugmode.pyt   test_check_isfinite(  s     #'(%))"%c         C` s„   t  j j ƒ  } t  j | g | d d d t j d t ƒ ƒ} | t j d d d g ƒ ƒ t j	 d d d g ƒ d } | | ƒ d  S(	   Ni   i   R	   R’   i   iüÿÿÿg      ð?i    (
   R
   R   R   R   R   R   RH   Rl   R—   Rœ   (   R   R   R   t   infs(    (    sC   /tmp/pip-build-X4mzal/theano/theano/compile/tests/test_debugmode.pyt   test_check_isfinite_disabledH  s    
(   R.   R/   R”   R•   Rž   R    (    (    (    sC   /tmp/pip-build-X4mzal/theano/theano/compile/tests/test_debugmode.pyRŒ     s   			 t   BrokenCImplementationAddc           B` s2   e  Z d Z d  „  Z d „  Z d „  Z d „  Z RS(   c         C` sš   t  j j | ƒ } t  j j | ƒ } | j j d k s< t ‚ | j j | j j k sZ t ‚ | j j d k sr t ‚ t j |  | | g | j ƒ  g ƒ } | S(   Nt   float32i   (	   R
   R   R   R   R   R   R   R   R   (   R   R   R   R    (    (    sC   /tmp/pip-build-X4mzal/theano/theano/compile/tests/test_debugmode.pyR!   [  s    $c         C` s-   | \ } } | \ } | | } | | d <d  S(   Ni    (    (   R   R"   R#   R$   R   R   R%   R&   (    (    sC   /tmp/pip-build-X4mzal/theano/theano/compile/tests/test_debugmode.pyR'   d  s    	
c         C` s   d S(   Ni   (   i   (    (   R   (    (    sC   /tmp/pip-build-X4mzal/theano/theano/compile/tests/test_debugmode.pyR(   l  s    c   
      C` s/   | \ } } | \ } d }	 d t  t ƒ  |  S(   Ni    sº	  
        //printf("executing c_code\n");
        if (PyArray_NDIM(%(a)s) != 2) {PyErr_SetString(PyExc_NotImplementedError, "rank(a) != 2"); %(fail)s;}
        if (PyArray_NDIM(%(b)s) != 2) {PyErr_SetString(PyExc_NotImplementedError, "rank(b) != 2"); %(fail)s;}

        if (PyArray_DESCR(%(a)s)->type_num != NPY_FLOAT)
        {PyErr_SetString(PyExc_NotImplementedError, "a dtype not NPY_FLOAT"); %(fail)s;}

        if (PyArray_DESCR(%(b)s)->type_num != NPY_FLOAT)
        {PyErr_SetString(PyExc_NotImplementedError, "b's dtype not NPY_FLOAT"); %(fail)s;}

        if (PyArray_DIMS(%(a)s)[0] != PyArray_DIMS(%(a)s)[1])
        {PyErr_SetString(PyExc_NotImplementedError, "a is not square"); %(fail)s;}

        if (PyArray_DIMS(%(b)s)[0] != PyArray_DIMS(%(b)s)[1])
        {PyErr_SetString(PyExc_NotImplementedError, "b is not square"); %(fail)s;}

        if (PyArray_DIMS(%(a)s)[0] != PyArray_DIMS(%(b)s)[0])
        {PyErr_SetString(PyExc_NotImplementedError, "a and b have different dimensions"); %(fail)s;}

        // We do not check for c_contiguous property here
        if (%(debug)s)
        {
            if (!%(z)s)
                printf("%(z)s is not there, %%p \n", %(z)s);
            else if (PyArray_DIMS(%(z)s)[0] != PyArray_DIMS(%(b)s)[0])
                printf("Dimension 0 mismatch for %(z)s and %(b)s\n");
            else if (PyArray_DIMS(%(z)s)[1] != PyArray_DIMS(%(b)s)[1])
                printf("Dimension 1 mismatch for %(z)s and %(b)s\n");
            else
                printf("Reusing %(z)s\n");
        }

        if ((!%(z)s)
            || (PyArray_DIMS(%(z)s)[0] != PyArray_DIMS(%(b)s)[0])
            || (PyArray_DIMS(%(z)s)[1] != PyArray_DIMS(%(b)s)[1])
            )
        {
            Py_XDECREF(%(z)s);
            npy_intp dims[] = {0, 0};
            dims[0] = PyArray_DIMS(%(b)s)[0];
            dims[1] = PyArray_DIMS(%(b)s)[1];
            %(z)s = (PyArrayObject*) PyArray_SimpleNew(2, dims, PyArray_DESCR(%(b)s)->type_num);
        }

        // Let us assume that %(z)s is c_contiguous
        {
            dtype_%(z)s * z = ((dtype_%(z)s*)(PyArray_GETPTR2(%(z)s,0,0)));
            for (int i=0; i<PyArray_DIMS(%(b)s)[0]; i++)
            {
                for (int j=0; j<PyArray_DIMS(%(b)s)[1]; j++)
                {
                    *z = ((float*)PyArray_GETPTR2(%(a)s, i, j))[0] +
                         ((float*)PyArray_GETPTR2(%(b)s, i, j))[0] ;
                    z++;
                }
            }
        }
        (   R)   R*   (
   R   R"   R+   R#   R%   R,   R   R   R&   t   debug(    (    sC   /tmp/pip-build-X4mzal/theano/theano/compile/tests/test_debugmode.pyR-   o  s    	;(    (   R.   R/   R0   R!   R'   R(   R-   (    (    (    sC   /tmp/pip-build-X4mzal/theano/theano/compile/tests/test_debugmode.pyR¡   W  s
   				t   VecAsRowAndColc           B` s&   e  Z d  Z d Z d „  Z d „  Z RS(   s®   
    Transforms a vector into a row and a column.

    This Op exists to check everything is correct when an Op has
    two outputs with different broadcasting patterns.
    c         C` s¬   t  | t j ƒ s' t j j | ƒ } n  | j j d k s? t ‚ t | j ƒ } | d | j	 d t
 t f ƒ } | d | j	 d t t
 f ƒ } t j |  | g | ƒ  | ƒ  g ƒ S(   Ni   R   t   broadcastable(   t
   isinstanceR   t   VariableR
   R   R   R   R   R   R   Re   RH   R   (   R   t   vt
   type_classt
   out_r_typet
   out_c_type(    (    sC   /tmp/pip-build-X4mzal/theano/theano/compile/tests/test_debugmode.pyR!   ¹  s    c   	      C` s  | \ } | \ } } | j  d } | d d  k sK | d j  d | f k rq | j d j j d | f ƒ | d <n  | d d  k sš | d j  | d f k rÀ | j d j j | d f ƒ | d <n  xD t | ƒ D]6 } | | | d d | f <| | | d | d f <qÍ Wd  S(   Ni    i   (   t   shapeR]   t   outputsR   t   value_zerost   range(	   R   R"   R#   R%   R¨   R    Ro   t   lvt   i(    (    sC   /tmp/pip-build-X4mzal/theano/theano/compile/tests/test_debugmode.pyR'   Â  s    	)&)&(    (   R.   R/   R@   R0   R!   R'   (    (    (    sC   /tmp/pip-build-X4mzal/theano/theano/compile/tests/test_debugmode.pyR¤   °  s   		t   Test_preallocated_outputc           B` s5   e  Z d  „  Z d „  Z d „  Z d „  Z d „  Z RS(   c         C` s"   t  j j d t j ƒ  ƒ |  _ d  S(   Nt   seed(   Rl   t   randomt   RandomStatet   uttt
   fetch_seedt   rng(   R   (    (    sC   /tmp/pip-build-X4mzal/theano/theano/compile/tests/test_debugmode.pyR”   Ó  s    c   	      C` s<  t  j j d ƒ } t  j j d ƒ } t ƒ  | | ƒ } t  j j | t j d ƒ ƒ } |  j j d d ƒ j	 d ƒ } |  j j d d ƒ j	 d ƒ } t
 j d d g ƒ } t  j | | g | d | ƒ} | | | ƒ t
 j d d g ƒ } t  j | | g | d | ƒ} t  j j r+|  j t
 j | | | ƒ n | | | ƒ d  S(	   NR   R   i   R¢   t   check_preallocated_outputt   c_contiguousR	   t   f_contiguous(   R
   R   t   fmatrixR¡   t   dotRl   t   eyeR¸   t   randnR˜   R   R   R   R   RC   Rš   RE   (	   R   R   R   R&   R%   t   a_valt   b_valR	   R   (    (    sC   /tmp/pip-build-X4mzal/theano/theano/compile/tests/test_debugmode.pyt   test_f_contiguousÖ  s     		c         C` s  t  j j d ƒ } t  j j d ƒ } t ƒ  | | ƒ } |  j j d d ƒ j d ƒ } |  j j d d ƒ j d ƒ } t j d d g ƒ } t  j	 | | g | d | ƒ} | | | ƒ t j d d g ƒ } t  j	 | | g | d | ƒ} t  j
 j r|  j t j | | | ƒ n | | | ƒ d  S(	   NR   R   i   R¢   R¹   Rº   R	   R»   (   R
   R   R¼   R¡   R¸   R¿   R˜   R   R   R   R   RC   Rš   RE   (   R   R   R   R%   RÀ   RÁ   R	   R   (    (    sC   /tmp/pip-build-X4mzal/theano/theano/compile/tests/test_debugmode.pyt   test_f_contiguous_outö  s    		c         C` sk   t  j j d ƒ } t ƒ  | ƒ \ } } t  j | g | | g ƒ } |  j j d ƒ j d ƒ } | | ƒ d  S(   NR¨   i   R¢   (   R
   R   t   fvectorR¤   R   R¸   R¿   R˜   (   R   R¨   Ro   R    R   t   v_val(    (    sC   /tmp/pip-build-X4mzal/theano/theano/compile/tests/test_debugmode.pyt   test_output_broadcast_tensor  s
    c      
   C` sÓ   d d l  m } | j s( t d ƒ ‚ n  | j j d  k rb | j d d t d t d t d t ƒn  | j	 d	 ƒ } t
 ƒ  | ƒ \ } } t j | g | | g ƒ } | j |  j j d
 ƒ j d ƒ ƒ } | | ƒ d  S(   Ni    (   t   cudas   Optional package Cuda disabledt   gput   forcet"   default_to_move_computation_to_gput   move_shared_float32_to_gput   enable_cudaR¨   i   R¢   (   t   theano.sandboxRÇ   t   cuda_availableR   t   uset   device_numberR]   Re   RH   RÄ   R¤   R
   R   t   CudaNdarrayR¸   R¿   R˜   (   R   RÇ   R¨   Ro   R    R   RÅ   (    (    sC   /tmp/pip-build-X4mzal/theano/theano/compile/tests/test_debugmode.pyt   test_output_broadcast_cuda  s    	
$(   R.   R/   R”   RÂ   RÃ   RÆ   RÒ   (    (    (    sC   /tmp/pip-build-X4mzal/theano/theano/compile/tests/test_debugmode.pyR²   Ò  s
   		 	 	(-   t
   __future__R    R   R   t   nose.plugins.skipR   t   unittestt   numpyRl   R
   R   R   t   theano.tensort   theano.compatR   t   theano.compileR   t   theano.testsR   R¶   R   R   R   RH   RD   Re   RB   R1   Rt   Rk   R   t   wb1RL   R[   Ra   Ri   Rm   Rs   Ru   t   TestCaseRv   RŒ   R¡   R¤   R²   (    (    (    sC   /tmp/pip-build-X4mzal/theano/theano/compile/tests/test_debugmode.pyt   <module>   s@   	DY			!	$				·:Y"