σ
ΰΖχXc           @` s‘  d  d l  m Z m Z m Z d  d l Z d  d l Z d  d l m Z d  d l Z	 d  d l
 Z
 d  d l m Z d  d l j Z d  d l j j Z d  d l m Z d d l m Z y0 d  d l m Z m Z d  d	 l m Z e Z Wn e k
 rφ e Z n Xe j  d
  Z! d   Z" d   Z# d d d  Z% d d d  Z& d a' d a( d a) d   Z* d   Z+ e+ e j,  Z- e+ e j.  Z/ e+ d    Z0 d e f d     YZ1 d S(   i    (   t   absolute_importt   print_functiont   divisionN(   t   StringIO(   t   config(   t   Modei   (   t   get_mode(   t   GpuArrayTypet   _name_for_ctx(   t   GpuArrays   theano.compile.nanguardmodec         C` s   t  |  t j j j  r t St  |  t j j j	  r8 t S| rW t
 | j d t  rW t St  |  t  rj t S|  d k rz t S|  j d k r t St S(   s~  
    Checks a variable against non-numeric types such as types, slices,
    empty arrays, and None, that need not be checked for NaN and Inf values.

    Parameters
    ----------
    arr : the data of that correspond to any Theano Variable
    var : The corresponding Theano variable

    Returns
    -------
    is_non_numeric : bool
        `True` the value is non-numeric.

    t   is_rngi    N(   t
   isinstancet   theanot   goft   typet   _cdata_typet   Falset   npt   randomt   mtrandt   RandomStatet   getattrt   tagt   slicet   Nonet   sizet   True(   t   arrt   var(    (    s;   /tmp/pip-build-X4mzal/theano/theano/compile/nanguardmode.pyt   _is_numeric_value   s    c         C` sx   t  |  t t t j f  rm g  } xP |  D]> } t  | t t f  rY | j t |   q( | j |  q( Wn |  g S| S(   sχ   
    Turns a nested graph of lists/tuples/other objects into a list of objects.

    Parameters
    ----------
    l : list/tuple/other objects
        Might be nested.

    Returns
    -------
    object
        A flattened list of objects.

    (   R   t   listt   tuplet   collectionst
   ValuesViewt   extendt   flattent   append(   t   lt   rvalt   elem(    (    s;   /tmp/pip-build-X4mzal/theano/theano/compile/nanguardmode.pyR#   9   s    c         C` sα   t  |  |  s t St j r t |  t j  r | re t t j d  re t | j	 t j j
 j  re t St t t t  t j t |  j |  j    Sn4 t rΛ t |  t  rΛ t j t |  j |  j    St j t j |    S(   sε  
    Test whether a numpy.ndarray contains any `np.nan` values.

    Parameters
    ----------
    arr : np.ndarray or output of any Theano op
    node : None or an Apply instance.
        If arr is the output of a Theano op, the node associated to it.
    var : The Theano symbolic variable.

    Returns
    -------
    contains_nan : bool
        `True` if the array contains any `np.nan` values, `False` otherwise.

    Notes
    -----
    Tests for the presence of `np.nan`'s using `np.isnan(np.min(ndarray))`.
    This approach is faster and more memory efficient than the obvious
    alternative, calling `np.any(np.isnan(ndarray))`, which requires the
    construction of a boolean array with the same shape as the input array.

    t   rng_mrg(   R   R   t   cudat   cuda_availableR   t   CudaNdarrayt   hasattrR   t   sandboxt   opR(   t   GPU_mrg_uniformt   compile_gpu_funcR   R   t   isnant   f_gpumint   reshapeR   t   pygpu_availableR	   t
   f_gpua_mint   min(   R   t   nodeR   (    (    s;   /tmp/pip-build-X4mzal/theano/theano/compile/nanguardmode.pyt   contains_nanT   s    "c         C` s;  t  |  |  s t St j rΈ t |  t j  rΈ | re t t j d  re t | j	 t j j
 j  re t St t t t  t j t |  j |  j    p΄ t j t |  j |  j    SnU t rt |  t  rt j t |  j |  j    pt j t |  j |  j    St j t j |    p:t j t j |    S(   s  
    Test whether a numpy.ndarray contains any `np.inf` values.

    Parameters
    ----------
    arr : np.ndarray or output of any Theano op
    node : None or an Apply instance.
        If the output of a Theano op, the node associated to it.
    var : The Theano symbolic variable.

    Returns
    -------
    contains_inf : bool
        `True` if the array contains any `np.inf` values, `False` otherwise.

    Notes
    -----
    Tests for the presence of `np.inf`'s by determining whether the
    values returned by `np.nanmin(arr)` and `np.nanmax(arr)` are finite.
    This approach is more memory efficient than the obvious alternative,
    calling `np.any(np.isinf(ndarray))`, which requires the construction of a
    boolean array with the same shape as the input array.

    R(   (   R   R   R)   R*   R   R+   R,   R   R-   R.   R(   R/   R0   R   R   t   isinfR2   R3   R   t   f_gpumaxR4   R	   R5   t
   f_gpua_maxt   nanmaxt   nanmin(   R   R7   R   (    (    s;   /tmp/pip-build-X4mzal/theano/theano/compile/nanguardmode.pyt   contains_inf~   s    !"!c         C` sH  t  j s d St  j d  } t } |  s. | r t d k r y( t j | g t j	 |  d d a Wq t
 k
 r{ t } q Xn  | rέ | rέ t d k rέ y( t j | g t j |  d d a Wqέ t
 k
 rΩ t } qέ Xn  | rD| rDt d k rDy1 t j | g t j t j |   d d a WqDt
 k
 r@t } qDXn  d S(   sD    compile utility function used by contains_nan and contains_inf
    Nt	   nan_guardt   modet   FAST_RUN(   R)   R*   t   fvectorR   R2   R   R   t   functiont   TR6   t   RuntimeErrorR   R:   t   maxt   f_gpuabsmaxt   abs_(   t   nan_is_errort   inf_is_errort   big_is_errort   guard_inputt   cuda_compile_failed(    (    s;   /tmp/pip-build-X4mzal/theano/theano/compile/nanguardmode.pyR0   ―   s2    	c         ` s"      f d    t     _  S(   Nc         ` sΌ   |  j  } t |  j  } | | f }  j j | d   } | d  k r² t t |  t f d |   } t	 d  j
 d  } t j | g   |  d | d t } |  j | <n  | |   S(   Nt   context_nameRA   t   gpuarrayR@   t   profile(   t   dtypeR   t   contextt   cachet   getR   R   t   strR   R   t	   includingR   RC   (   t   inpRQ   t   ctx_namet   keyt   ft   guard_inR@   (   R.   t   result(    s;   /tmp/pip-build-X4mzal/theano/theano/compile/nanguardmode.pyR\   Ϋ   s    	!(   t   dictRS   (   R.   (    (   R.   R\   s;   /tmp/pip-build-X4mzal/theano/theano/compile/nanguardmode.pyt	   f_computeΪ   s    c         C` s   t  j t  j |    S(   N(   RD   RF   RH   (   t   x(    (    s;   /tmp/pip-build-X4mzal/theano/theano/compile/nanguardmode.pyt   <lambda>μ   s    t   NanGuardModec           B` s&   e  Z d  Z d d d d d d  Z RS(   s  
    A Theano compilation Mode that makes the compiled function automatically
    detect NaNs and Infs and detect an error if they occur.

    Parameters
    ----------
    nan_is_error : bool
        If True, raise an error anytime a NaN is encountered.
    inf_is_error : bool
        If True, raise an error anytime an Inf is encountered.  Note that some
        pylearn2 modules currently use np.inf as a default value (e.g.
        mlp.max_pool) and these will cause an error if inf_is_error is True.
    big_is_error : bool
        If True, raise an error when a value greater than 1e10 is encountered.

    Note
    ----
        We ignore the linker parameter
    t   defaultc   	      ` s  | |  _   d  k r$ t j j  n   d  k r? t j j  n    d  k rZ t j j   n   sr  sr   sr t  t j	 r t
      n  d      f d    f d   }  f d   } t j j j d | d |  } t t |   j | d |  j  d  S(   Nc         ` sk  t  } t   }  rC t |  | |  rC t d d | t } qC n   rw t |  | |  rw t d d | t } qw n    rZt  } t |  |  s t  } n t j rη t	 |  t j
  rη t t  t  t  t |  j |  j   d k } nQ t rt	 |  t  rt |  j |  j   d k } n t j |   j   d k } | rZt d d | t } qZn  | rg| rt d d | t t j j | d d d | n t d d | | rΑ| j d	 } n  t t j j j |  d | | j   } t j j d
 k rt  |   qgt j j d k rBt |  d	 d l! } | j"   qgt j j d k rgt# j$ |  qgn  d S(   sW  
            Checks `value` for NaNs / Infs. If detected, raises an exception
            and / or prints information about `nd`, `f`, and `is_input` to
            help the user determine the cause of the invalid values.

            Parameters
            ----------
            value : numpy.ndarray
                The value to be checked.
            nd : theano.gof.Apply
                The Apply node being executed.
            var : theano.gof.Variable
                Not used if nd is there. Otherwise, used to print the stack
                trace for inputs of the graph.

            s   NaN detectedt   files   Inf detectedg    _ Bs   Big value detectedsE   NanGuardMode found an error in the output of a node in this variable:RU   s5   NanGuardMode found an error in an input of the graph.i    t   raiset   pdbNt   warn(%   R   R   R8   t   printR   R>   R   R)   R*   R   R+   R0   RG   R3   R   R4   R	   t   f_gpua_absmaxR   t   absRF   R   t   printingt
   debugprintt   outputsR   t   utilst   get_variable_trace_stringt   getvalueR   Ra   t   actiont   AssertionErrorRe   t	   set_tracet   loggert   error(   t   valuet   ndR   Rt   t   siot   errt   msgRe   (   RK   RJ   RI   (    s;   /tmp/pip-build-X4mzal/theano/theano/compile/nanguardmode.pyt   do_check_on  sV    		!!	%	
c         ` sS   xL |  j  D]A } | | d r
 t | j d t  r
   | | d |   q
 q
 Wd  S(   Ni    t   nan_guard_mode_check(   Rl   R   R   R   (   R7   t   thunkt   storage_mapt   compute_mapR   (   Rz   (    s;   /tmp/pip-build-X4mzal/theano/theano/compile/nanguardmode.pyt	   nan_checkT  s    c         ` s/   t  |  j d t  r+   | d  d |  n  d  S(   NR{   R   (   R   R   R   R   (   R   Ru   (   Rz   (    s;   /tmp/pip-build-X4mzal/theano/theano/compile/nanguardmode.pyt   nan_check_inputZ  s    t   callbackt   callback_inputt	   optimizer(   t   provided_optimizerR   R   Ra   RI   RJ   RK   Rq   R)   t   cuda_enabledR0   R   R   t   vmt	   VM_Linkert   supert   __init__(	   t   selfRI   RJ   RK   R   t   linkerR   R   t   wrap_linker(    (   RK   Rz   RJ   RI   s;   /tmp/pip-build-X4mzal/theano/theano/compile/nanguardmode.pyR     s"    		@	N(   t   __name__t
   __module__t   __doc__R   R   (    (    (    s;   /tmp/pip-build-X4mzal/theano/theano/compile/nanguardmode.pyRa   ο   s   	(2   t
   __future__R    R   R   R    t   loggingt	   six.movesR   t   numpyR   R   t   theano.configparserR   t   theano.tensort   tensorRD   t   theano.sandbox.cudaR-   R)   t   theano.compileR   R@   R   t   theano.gpuarray.typeR   R   t   pygpu.gpuarrayR	   R   R4   t   ImportErrorR   t	   getLoggerRs   R   R#   R   R8   R>   R2   R:   RG   R0   R^   R6   R5   RF   R;   Rh   Ra   (    (    (    s;   /tmp/pip-build-X4mzal/theano/theano/compile/nanguardmode.pyt   <module>   s<   

		*,	+	