
Xc           @` s  d  Z  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	 j
 j Z d d l m Z m Z d d l m Z d d l Z d d l Z d d l m Z m Z d d l m Z d d l m Z m Z d d	 l m Z m Z d d
 l m Z m  Z  m! Z! m" Z" d d l# m$ Z$ d d l% m& Z& m' Z' m( Z( m) Z) m* Z* m+ Z+ d d l, m- Z- m. Z. d d l/ m0 Z0 d Z1 e j2 d  Z3 d e j4 f d     YZ5 e3 j6 e5    d e7 f d     YZ8 d e8 f d     YZ9 d e8 f d     YZ: d e8 f d     YZ; d e8 f d     YZ< d e8 f d     YZ= d e8 f d      YZ> d!   Z? d" d# e@ eA e jB eA eA e@ d$ eA e@ e@ e@ e@ e@ e@ eA d%  ZC eA d&  ZD d' f  d(     YZE eE   ZF d)   ZG eH e@ eH d*  ZI d+   ZJ d,   ZK d-   ZL d.   ZM d/   ZN d0   ZO d1   ZP eN ZQ d2   ZR d3   ZS d4 eT f d5     YZU d6 eT f d7     YZV e e j jW jX  g ZY d8 eT f d9     YZZ d: e j! j[ f d;     YZ\ d< g Z] d= e& f d>     YZ^ d?   Z_ e j` e^ e_  d@ e- f dA     YZa e. dB ea dC dD   d S(E   s   
Provides `DebugMode`, an evaluation mode for debugging theano internals.

TODO: add support for IfElse Op, LazyLinker, PureOp, etc.

i    (   t   absolute_importt   print_functiont   divisionN(   t   chaint   product(   t   izip(   t   goft   config(   t   get_unbound_function(   t	   iteritemst
   itervalues(   t   StringIOt   xrange(   t   grapht   utilst   linkt   ops_with_inner_function(   t   raise_with_op(   t   FunctionMakert   Functiont   infer_reuse_patternt   SymbolicOutputt
   Supervisort
   std_fgraph(   t   Modet   register_mode(   t   OutputGuards   restructuredtext ens   theano.compile.debugmodet   NoDuplicateOptWarningFilterc           B` s   e  Z e g   Z d    Z RS(   c         C` sI   | j    } | j d  rE | |  j k r. t S|  j j |  t Sn  t S(   Ns   Optimization Warning: (   t
   getMessaget
   startswitht	   prev_msgst   Falset   addt   True(   t   selft   recordt   msg(    (    s8   /tmp/pip-build-X4mzal/theano/theano/compile/debugmode.pyt   filter(   s    (   t   __name__t
   __module__t   setR   R%   (    (    (    s8   /tmp/pip-build-X4mzal/theano/theano/compile/debugmode.pyR   %   s   t   DebugModeErrorc           B` s   e  Z d  Z RS(   sK   
    Generic Exception raised to indicate an internal theano problem.

    (   R&   R'   t   __doc__(    (    (    s8   /tmp/pip-build-X4mzal/theano/theano/compile/debugmode.pyR)   :   s   t   BadThunkOutputc           B` sS   e  Z d  Z d Z d Z d Z d Z d Z d d  Z	 d   Z
 d   Z d   Z RS(   s<  
    Exception: Calling the same Op twice gives inconsistent outputs.

    It can be raised, for instance, if an Op's c_code and perform method
    do not agree, or if one of these methods do not give the same result
    when called twice with the same inputs (but different memory layouts
    for the output).

    t    c         C` sM   t  t |   j   | |  _ | |  _ | |  _ | |  _ | |  _ | |  _ d  S(   N(	   t   superR+   t   __init__t   rt   thunk1t   val1t   thunk2t   val2t
   inputs_val(   R"   R/   R0   R1   R2   R3   R4   (    (    s8   /tmp/pip-build-X4mzal/theano/theano/compile/debugmode.pyR.   b   s    					c         C` s   t  |  j j j  S(   se   
        Return the Op class whose c_code and perform implementations
        didn't match.

        (   t   typeR/   t   ownert   op(   R"   (    (    s8   /tmp/pip-build-X4mzal/theano/theano/compile/debugmode.pyt   offending_opk   s    c         C` s
   |  j    S(   N(   t   str_diagnostic(   R"   (    (    s8   /tmp/pip-build-X4mzal/theano/theano/compile/debugmode.pyt   __str__s   s    c         C` s3  t    } t d d | t d |  j j d | t d |  j   d | t d |  j j d | t d t |  j d d  d | t d t |  j d	 d  d | t d
 g  |  j j j	 D] } | j ^ q d | t d g  |  j
 D] } t | d d  ^ q d | t d g  |  j
 D] } t | d	 d  ^ qd | g  } xF |  j
 D]; } t | d d  d k r| j |  qT| j d  qTWt d | d | t d |  j d | t d |  j d | t d |  j d | d d l j j } t | j |  j |  j d d  d | | j   } | S(   sd   
        Return a pretty multiline string representing the cause of
        the exception.

        R+   t   files     Apply   :s     op      :s     Outputs Type:s     Outputs Shape:t   shapes     Outputs Strides:t   stridess     Inputs Type :s     Inputs Shape:s     Inputs Strides:t   sizeii
   s	   not showns     Inputs values: %ss     Bad Variable:s     thunk1  :s     thunk2  :i    N(   R   t   printR/   R6   R8   R5   t   getattrR1   t   Nonet   inputsR4   t   appendR0   R2   t   theano.tests.unittest_toolst   testst   unittest_toolsR9   R3   t   getvalue(   R"   t   siot   it   valt   scalar_valuest   iptt   uttt   ret(    (    s8   /tmp/pip-build-X4mzal/theano/theano/compile/debugmode.pyR9   v   s6    	"+	,	,(N(    (   R&   R'   R*   RA   R/   R0   R1   R2   R3   R.   R8   R:   R9   (    (    (    s8   /tmp/pip-build-X4mzal/theano/theano/compile/debugmode.pyR+   C   s   				t   BadOptimizationc           B` sS   e  Z d  Z d Z d Z d Z d Z d Z d Z	 d Z
 d   Z d   Z d   Z RS(   sU   
    Exception: some variable and its substitute take different runtime values.

    R,   c         C` sV   t  t |   j   | |  _ | |  _ | |  _ | |  _ | |  _ | |  _ | |  _	 d  S(   N(
   R-   RO   R.   t   old_rt   new_rt	   old_r_valt	   new_r_valt   reasont	   old_grapht	   new_graph(   R"   RP   RQ   RR   RS   RT   RU   RV   (    (    s8   /tmp/pip-build-X4mzal/theano/theano/compile/debugmode.pyR.      s    						c         C` s
   |  j    S(   N(   R9   (   R"   (    (    s8   /tmp/pip-build-X4mzal/theano/theano/compile/debugmode.pyR:      s    c         C` s  t    } d } t d t t |   j   d | t d t |  j  |  j d | t d |  j j d | t d t |  j	  d | y t    } t d d d	 d | t |  j
 j d d	 d | t |  j
 j d d	 d | t |  j
 j d | t | j   d | Wn t k
 r"n Xt |  j
  } t |  | k rjt d
 t |  j
  |  d d | n t d
 t |  j
  d | y t    } t d d d	 d | t |  j	 j d d	 d | t |  j	 j d d	 d | t |  j	 j d | t | j   d | Wn t k
 r n Xt |  j	  } t |  | k rht d t |  j	  |  d d | n t d t |  j	  d | yt j |  j
  } t j |  j	  } t    } t j | |  } t d t j |  d | t d t j |  d | t d t j |  d | t d t j |  d | t j |  }	 | j   |	 | j   |	 f }
 t d |
 d | | t j t j |  t j |  d  } t d t j |  d | t d t j |  d | t d t j |  d | t d t j |  d | t j |  }	 | j   |	 | j   |	 f }
 t d |
 d | t | j   d | Wn t k
 rn Xt d t |  j  d | t d d | t |  j d | t d d | t |  j d | t d d | t d d | t d d | | j   S(   sf   
        Return a pretty multiline string representating the cause
        of the exception.

        i   s   BadOptimization ErrorR;   s     Variable: ids     Ops     Value Type:s"     Old Value shape, dtype, strides:t   endt    s     Old Value: s   ...s"     New Value shape, dtype, strides:s     New Value: s     Max Abs Diff: s     Mean Abs Diff: s     Median Abs Diff: s     Std Abs Diff: s     Value at Max Diff: g:0yE>s     Max Rel Diff: s     Mean Rel Diff: s     Median Rel Diff: s     Std Rel Diff: s
     Reason: s     Old Graph:s     New Graph:R,   s8   Hint: relax the tolerance by setting tensor.cmp_sloppy=1s8     or even tensor.cmp_sloppy=2 for less-strict comparison(   R   R?   R-   RO   R:   t   idRQ   R6   R5   RS   RR   R<   t   dtypeR=   RG   t	   Exceptiont   strt   lent   npt   asarrayt   absolutet   maxt   meant   mediant   stdt   argmaxt   flattent   maximumRT   RU   RV   (   R"   RH   t   val_str_len_limitt   ssiot   str_old_r_valt   str_new_r_valt   ovt   nvt   abs_difft   arg_max_valt   values_at_maxt   reldiff(    (    s8   /tmp/pip-build-X4mzal/theano/theano/compile/debugmode.pyR9      s    	"			
		N(   R&   R'   R*   RA   RQ   RP   RR   RS   RT   RU   RV   R.   R:   R9   (    (    (    s8   /tmp/pip-build-X4mzal/theano/theano/compile/debugmode.pyRO      s   			t   BadDestroyMapc           B` s    e  Z d  Z d   Z d   Z RS(   sf   
    Exception: Some perform() or c_code() modified an input that
    wasn't in the destroy_map.

    c         C` sD   t  t |   j   | |  _ | |  _ | |  _ | |  _ | |  _ d  S(   N(   R-   Rr   R.   t   nodet   idxt   old_valt   new_valt   perform(   R"   Rs   Rt   Ru   Rv   Rw   (    (    s8   /tmp/pip-build-X4mzal/theano/theano/compile/debugmode.pyR.   C  s    				c         C` s  t    } t d |  j d | t d |  j d | t d g  |  j j D] } t |  t |  f ^ qH d | t d t |  j j d i   d | t d |  j	 d | t d |  j j |  j	 j
 d | t d	 t |  j  d | t d
 t |  j  d | yit j |  j  } t j |  j  } t d | j | j d | t d | j | j d | t d | j   | j   d | t d | j   | j   d | | | } t d | j   d | t d | j   d | t d t j | j   | j  d | t d t j | j   | j  d | t d t j t j |   d  d | t d d | Wn- t k
 r} t d t |  d | n Xt d d | | j   S(   Ns     node:R;   s
     perform:s     node.inputs:s     destroy_map:t   destroy_maps     changed input idx:s     changed input type:s     repr (old val):s     repr (new val):s      value dtype (new <space> old):s      value shape (new <space> old):s     value min (new <space> old):s     value max (new <space> old):s     value min (new-old):s     value max (new-old):s     value argmin (new-old):s     value argmax (new-old):s"     location of first 10 mismatches:i
   R,   s   (Numpy-hints failed with: %s)sz     Hint: this can also be caused by a deficient values_eq_approx() or __eq__() implementation [which compared input values](   R   R?   Rs   Rw   RB   R\   RY   R@   R7   Rt   R5   t   reprRu   Rv   R^   R_   RZ   R<   t   minRa   t   unravel_indext   argminRe   t	   transposet   nonzeroR[   RG   (   R"   RH   RI   t   npy_old_valt   npy_new_valt   deltat   e(    (    s8   /tmp/pip-build-X4mzal/theano/theano/compile/debugmode.pyR:   K  sP    		5
#	(   R&   R'   R*   R.   R:   (    (    (    s8   /tmp/pip-build-X4mzal/theano/theano/compile/debugmode.pyRr   =  s   	t
   BadViewMapc           B` s&   e  Z d  Z d d d  Z d   Z RS(   sh   
    Exception: Some perform() or c_code() created a memory alias
    that wasn't in the view_map.

    c         C` sD   t  t |   j   | |  _ | |  _ | |  _ | |  _ | |  _ d  S(   N(   R-   R   R.   Rs   t
   output_idxt   out_storaget   in_alias_idxt   out_alias_idx(   R"   Rs   R   R   R   R   (    (    s8   /tmp/pip-build-X4mzal/theano/theano/compile/debugmode.pyR.   ~  s    				c         C` s_  t    } t d |  j d | t d g  |  j j D] } t |  t |  f ^ q2 d | t d g  |  j j D] } t |  t |  f ^ qp d | t d t |  j j d i   d | t d t |  j j d i   d | t d	 |  j	 d | t d
 |  j
 d | |  j r3t d |  j d | n  |  j rUt d |  j d | n  | j   S(   Ns     node:R;   s     node.inputs:s     node.outputs:s     view_map:t   view_maps     destroy_map:Rx   s     aliased output:s     aliased output storage:s     aliased to inputs:s     aliased to outputs:(   R   R?   Rs   RB   R\   RY   t   outputsR@   R7   R   R   R   R   RG   (   R"   RH   RI   (    (    s8   /tmp/pip-build-X4mzal/theano/theano/compile/debugmode.pyR:     s     		5	5%		N(   R&   R'   R*   RA   R.   R:   (    (    (    s8   /tmp/pip-build-X4mzal/theano/theano/compile/debugmode.pyR   w  s   t   StochasticOrderc           B` s   e  Z d  Z RS(   s  
    Exception: Repeated Optimizations of the same graph do not give
    identical results.

    The most common cause is that an Optimization iterates over some
    objects in a memory-address-dependent order (such as id() or
    object.hash()).  If you see this error and you think it is related
    to optimizations within Theano, email theano-dev with the message
    attached to this exception.

    (   R&   R'   R*   (    (    (    s8   /tmp/pip-build-X4mzal/theano/theano/compile/debugmode.pyR     s   t   InvalidValueErrorc           B` s)   e  Z d  Z d d d d  Z d   Z RS(   sd   
    Exception: some Op an output value that is inconsistent with
    the Type of that output.

    t   nonec         C` sD   t  t |   j   | |  _ | |  _ | |  _ | |  _ | |  _ d  S(   N(   R-   R   R.   R/   t   vt   client_nodet   hintt   specific_hint(   R"   R/   R   R   R   R   (    (    s8   /tmp/pip-build-X4mzal/theano/theano/compile/debugmode.pyR.     s    				c         C` s   |  j  |  j } } | j } t |  } t |  d d !} d } d } d } d }	 d }
 yF | j } | j } | j   } | j   }	 t j	 t j
 |   }
 Wn t k
 r n X|  j } |  j } |  j } t | d d d d d t   j   } d	 t   S(
   Ni    id   s   N/At   prefixs     t   depthi   R;   s  InvalidValueError
        type(variable) = %(type_r)s
        variable       = %(r)s
        type(value)    = %(type_v)s
        dtype(value)   = %(v_dtype)s
        shape(value)   = %(v_shape)s
        value          = %(v_val)s
        min(value)     = %(v_min)s
        max(value)     = %(v_max)s
        isfinite       = %(v_isfinite)s
        client_node    = %(client_node)s
        hint           = %(hint)s
        specific_hint  = %(specific_hint)s
        context        = ...
%(context)s
        (   R/   R   R5   R\   R<   RZ   Rz   Ra   R^   t   allt   isfiniteR[   R   R   R   t
   debugprintR   RG   t   locals(   R"   R/   R   t   type_rt   type_vt   v_valt   v_dtypet   v_shapet   v_mint   v_maxt
   v_isfiniteR   R   R   t   context(    (    s8   /tmp/pip-build-X4mzal/theano/theano/compile/debugmode.pyR:     s.    						N(   R&   R'   R*   RA   R.   R:   (    (    (    s8   /tmp/pip-build-X4mzal/theano/theano/compile/debugmode.pyR     s   c         C` sl   d } d } |  d k r! d } n  xD |  d k rg |  | } t  t d  |  } | | } |  | }  q$ W| S(   sd   
    Converts number to string by rendering it in base 26 using
    capital letters as digits.

    i   R,   i    t   A(   t   chrt   ord(   t   numbert   baset   rvalt	   remaindert   new_char(    (    s8   /tmp/pip-build-X4mzal/theano/theano/compile/debugmode.pyt   char_from_number  s    	

R,   it   CHARc   *       ` s  | d k r d S d k r% g   n    d k r= t     n  | d k rR g  } n  | rh d  j } n d } | d k r | } n   d k r t    n  t      f d  } t  j d  r j } t  d d  } | d k r d } n  | rt t  j j d d   } n d } | rLt t  j j d	 d   } n d } | rq| d
 k rqd | } n  | r| d
 k rd | } n  d }  rt  j	  j   } n  |   k } | |  } t
 | j  d k rd } n d | j j	   } d } | r5d t | j | j d d   } n  d } | rt
 t  d g    d k r f d   } d t g   j D]' \ } }  | | t  | |  f ^ q~ } n  | d k s| | j k rt d | | j | | | | | | | | | f d | n | j | }! |! | j d }" | j   }# |# | }$ |# | | j d }% t
 | j  d k rod } n d | j j	   } t d | | j | | | | | | | | | |! |" |$ |% f d | | s|
 st  d  o j d k	 r| d }& | d }' x t | j  D] \ } }  | t
 | j  d k rK| d }' n  t |  d  rt |  j d  rt |  j j t j j j  r| j |   qn  t |  |& d | d d   d | d | d  d  d |
 d  |' d! | d" | d# | d$ | d%  d& | qWqqn | d k	 r | k r|   } |  }( t |( j d  ra| |( j  }) n | |(  }) t d' |  | | |) f d | nZ d } | rd t | j  d   } n  |   } t d( |  | | | f d | | S()   s  
    Print the graph leading to `r` to given depth.

    Parameters
    ----------
    r
        Variable instance.
    prefix
        Prefix to each line (typically some number of spaces).
    depth
        Maximum recursion depth (Default -1 for unlimited).
    done
        Internal. Used to pass information when recursing.
        Dict of Apply instances that have already been printed and their
        associated printed ids.
    print_type
        Whether to print the Variable type after the other infos.
    file
        File-like object to which to print.
    print_destroy_map
        Whether to print the op destroy_map after other info.
    print_view_map
        Whether to print the op view_map after other info.
    order
        If not empty will print the index in the toposort.
    ids
        How do we print the identifier of the variable :
        id - print the python id value,
        int - print integer character,
        CHAR - print capital character,
        "" - don't print an identifier.
    stop_on_name
        When True, if a node in the graph has a name, we don't print anything
        below it.
    scan_ops
        Scan ops in the graph will be added inside this list for later printing
        purposes.
    scan_inner_to_outer_inputs
        A dictionary mapping a scan ops inner function inputs to the scan op
        inputs (outer inputs) for printing purposes.
    smap
        None or the storage_map when printing an Theano function.
    used_ids
        Internal. Used to pass information when recursing.
        It is a dict from obj to the id used for it.
        It wasn't always printed, but at least a reference to it was printed.
    print_clients
        If True, we will print the clients of nodes when they have more then one clients.
    i    Ns    <%s>R,   c         ` s   |   k r  |  } n |  d k r. d } n  d k rS d t  t    } n_  d k rx d t  t    } n:  d k r d t t    } n  d k r d } n  | r |   |  <n  |  |  <| S(   Nt   outputRY   s   [id %s]t   intR   R,   (   R\   RY   R]   R   (   t   objt   get_printedt   id_str(   t   donet   idsR/   t   used_ids(    s8   /tmp/pip-build-X4mzal/theano/theano/compile/debugmode.pyt
   get_id_strM  s     		
R7   t   nameRx   R   s   {}s   d=s   v=i   s   .%iRX   t   clientsc         ` s*   y   j  |   SWn t k
 r% d SXd  S(   NR,   (   t   indext
   ValueError(   t   c(   t   order(    s8   /tmp/pip-build-X4mzal/theano/theano/compile/debugmode.pyt	   get_index  s    s	    clients:s   %s%s%s %s%s '%s' %s %s %s%s%sR;   id   s?   %s%s%s %s%s '%s' %s %s %s%s%s --> %8.2es %4.1f%% %8.2es %4.1f%%s    |s     R6   R   R   t
   print_typeR   R   t   stop_on_namet   prefix_childt   scan_opst   profilet   scan_inner_to_outer_inputst   smapR   t   print_clientss   %s%s %s%s -> %ss   %s%s %s%s%s(   RA   t   dictR5   R!   t   hasattrR6   R@   R\   R7   R   R]   R   t   getR   R   t
   apply_timeR?   t   fct_call_timet   compute_total_timesR   t	   enumerateRB   t
   isinstancet   theanot   scan_modulet   scan_opt   ScanRC   R   (*   R/   R   R   R   R   R;   t   print_destroy_mapt   print_view_mapR   R   R   R   R   R   R   R   R   R   t   type_strR   t   at   r_namet   destroy_map_strt   view_map_strt   ot   already_printedR   Rt   t   dataR   R   R   RI   t   op_timet   op_time_percentt   tot_time_dictt   tot_timet   tot_time_percentt
   new_prefixt   new_prefix_childt   outer_rt   outer_id_str(    (   R   R   R   R/   R   s8   /tmp/pip-build-X4mzal/theano/theano/compile/debugmode.pyR     s    8					!!	&$	;
			

! 
c   
      ` s{  g  |  D] } | j  ^ q } g  |  D] } | j r# | j ^ q# } g  | D] } | j  ^ qH | } t   } t j j | | d | g   | s x{   j D]- } t | j d d  r t
 d |   q q Wn@ x=   j D]2 } t | j d d  r   j t j    Pq q W  j t   f d   t |    j  D   x! t j D] }	   j |	    qEW  t t t |   | f S(   s  
    Create a FunctionGraph for debugging.

    Parameters
    ----------
    input_specs: WRITEME
        fgraph inputs.
    output_specs: WRITEME
        fgraph outputs.
    accept_inplace : bool
        Are inplace ops permitted in the original graph?

    Returns
    -------
    FunctionGraph
        A new FunctionGraph with a cloned graph, with debugging `Feature`
        instances already installed.

    t   featuresRx   s)   Graph must not contain inplace operationsc         3` sB   |  ]8 \ } } | j  p3 t   d   o3   j |  s | Vq d S(   t
   destroyersN(   t   mutableR   R   (   t   .0t   spect   input(   t   fgraph(    s8   /tmp/pip-build-X4mzal/theano/theano/compile/debugmode.pys	   <genexpr>  s    	N(   t   variablet   updatet   _VariableEquivalenceTrackerR   t   fgt   FunctionGrapht   apply_nodesR@   R7   RA   t	   TypeErrort   attach_featuret   DestroyHandlerR   t   zipRB   R   R   t   listt   mapR   (
   t   input_specst   output_specst   accept_inplaceR   t   orig_inputst   updatest   orig_outputst   equivalence_trackerRs   t   feature(    (   R   s8   /tmp/pip-build-X4mzal/theano/theano/compile/debugmode.pyt   _optcheck_fgraph  s(    % 	
	&t   DataDestroyedc           B` s   e  Z RS(    (   R&   R'   (    (    (    s8   /tmp/pip-build-X4mzal/theano/theano/compile/debugmode.pyR     s   c         C` s<   t  |  j d  r% |  j j | |  S|  j j | |  Sd  S(   Nt   values_eq_approx(   R   t   tagR   R5   (   t   varR1   R3   (    (    s8   /tmp/pip-build-X4mzal/theano/theano/compile/debugmode.pyt   check_eq'  s    c         C` s[  g  } t  |  j d i   }	 x' t |	  D] \ }
 } | j |  q( Wg  | D] } |  j | ^ qL } g  } t  |  j d i   } x t |  D] \ } } |  j | } | | d } | |  j | d d } t | j d  r| j j | |  r| j	 |  j |  n  | r | r t
 |  j t  r/q n  | | k	 r^t j d | d t |    q^q q Wt  |  j d i   } x t |  D] \ } } |  j | } | | d } | |  j | d d } t | j d  o| j j | |  } | r| j	 |  j |  n  | rt  | d d  d k r,qn  t
 |  j t  rDqn  | smt j d	 | d t |    qmqqWx t |  j  D] \ } } | j j | | | | d  s| | k r,|  | k rP| j | d |  f  d |  k	 rt d
   n  | r| | d |  f | | <n  t | | d <qPqSt |  | | | | | d |   qqW| S(   s  
    Raise BadDestroyMap if necessary, update dr_vals.

    Returns a list of output variables that actually worked inplace
    (their value is aliased to the value of at least one input).

    It modify the storage_map to remove node.inputs variable that have
    been destroyed.

    Rx   i    t   may_share_memorysT   Optimization Warning: input idx %d marked as destroyed was not changed for node '%s'R   R>   i   i   sY   Optimization Warning: input idx %d marked as viewed but new memory allocated by node '%s's   failure in topological ordering(   R@   R7   R	   t   extendRB   R   R   R5   R  RC   R   R   t   _loggert   warningR\   R   t	   values_eqR   R[   t   data_destroyedRr   (   Rs   t   storage_mapt   r_valst   dr_valst   active_nodest   clobber_dr_valsRw   t   warn_input_not_reusedt   destroyed_idx_listRx   t   o_post
   i_pos_listRI   t   destroyed_res_listt   actually_inplace_outputst   dmapt   oot   iiR   t   out_vart   in_vart   vmapt	   may_sharet   r_idxR/   (    (    s8   /tmp/pip-build-X4mzal/theano/theano/compile/debugmode.pyt   _check_inputs.  sb     		!"c         C` s0  x)t  |  j  D]\ } } i  i  } } | | d } t |  j d i   } t |  j d i   } x t  |  j  D] \ }	 }
 | |
 d } | t k r qq n  t |
 j d  rq |
 j j | |  rq t	 |
  } |	 | | <|	 g | j
 | d  k s|	 g | j
 | d  k r)| j |  | | <q)qq qq Wx* t |  D] \ } } | j | d  q:W| rt |  | | t | j      n  | r t |  r x t  |  j  D]z \ } } | | k rqn  | | d } t |  rt | j d  r| j j | |  rt |  | | d |  qqWq q Wd S(   s,  
    This functions raises a BadViewMap exception when it detects the
    following:
    - Output node storages aliased to input storage, with no declaration
      in view_map.
    - If not aliased to an input, check if two outputs are aliased together
      and used subsequently in the graph.

    i    R   Rx   R  R   N(   R   R   R@   R7   RB   R  R   R5   R  RY   R   RA   t   popR	   R   R   t   valuest   _is_used_in_graph(   Rs   R  t   oit   onodet
   good_aliast	   bad_aliast
   outstorageR   Rx   R  t   inodet
   in_storaget   nodeidt   keyRJ   t   other_oit   other_onodet   other_storage(    (    s8   /tmp/pip-build-X4mzal/theano/theano/compile/debugmode.pyt   _check_viewmap  s>    
$	c         C` s    |  j  d g k p |  j  g  k S(   sd   

    Returns
    -------
    bool
        True if `var` is used by another node in the graph.

    R   i   (   s   outputi   (   R   (   R   (    (    s8   /tmp/pip-build-X4mzal/theano/theano/compile/debugmode.pyR    s    	c         C` s   | d k r d Sy |  j  | j  k } Wn t k
 r: d SX| s t d |  j | j |  j  | j  t |  f  } | d k r |  q t d | d t j n  d S(   sf   

    Parameters
    ----------
    warn_err
        If 0, no warning, if 1 warning, if 2 error.

    i    Ns   Stride mismatchi   s   WARNING:R;   (   R=   R[   R   R<   R\   R?   t   syst   stderr(   R   t   bt   warn_errR7   t
   strides_eqR   (    (    s8   /tmp/pip-build-X4mzal/theano/theano/compile/debugmode.pyt   _check_strides_match  s    		c         C` s   d d l  m } t |   t j t j f k rC |  j d d  } n* t |   | k r^ |  } n t j |   } t |  t |   k s t t |  t |   f   t	 | t j  r | j
 |  j
 k s t  n  | S(   sW  

    Parameters
    ----------
    a
        Any object

    Returns
    -------
    object
        A copy of `a` that shares no internal storage with the original
        (a deep copy). This function handles numpy arrays specially, because
        copy.deepcopy() called on a 0-d array will return a numpy scalar,
        not an array.

    i    (   t   _cdata_typeR   t   K(   t   theano.gof.typeR2  R5   R^   t   ndarrayt   memmapt   copyt   deepcopyt   AssertionErrorR   RZ   (   R   R2  R   (    (    s8   /tmp/pip-build-X4mzal/theano/theano/compile/debugmode.pyt   _lessbroken_deepcopy  s    	6c         C` s!  xt  |   D]\ } } x | j D] } x | | D] \ } } } }	 | | }
 | | } | j | j k sr t  t | j d  r | j j | |
  } n9 t | d  r | j | |
  } n | j j | |
  } | s4 t d | d | d | d |
 d | d | d |	   q4 q4 Wq# Wq Wd	 S(
   sa  
    Use a simple algorithm to find broken optimizations.

    This algorithm is simple to understand, but sometimes when there's
    a problem it identifies the wrong optimization as the culprit.
    The problem stems from the fact that results are not evaluated in
    chronological order (looking at when they were introduced to the
    graph).

    R   RP   RQ   RR   RS   RT   RU   RV   N(   R   R   R5   R9  R   R   R   RO   (   R   t   reasonsR	  RI   Rs   RQ   RT   R/   t   old_graph_strt   new_graph_strRS   t   r_valt   check(    (    s8   /tmp/pip-build-X4mzal/theano/theano/compile/debugmode.pyt   _find_bad_optimizations0  s&    

c         C` s  i  } i  } x t  |   D] \ } } | | | <x | j D] } | j | t | g   xg | | D][ \ } }	 }
 } | | j | j |	 t |	 g    x  | |	 D] } | | | | <q Wqc Wq9 Wq Wi  } t } x t |  D] \ }	 } t |  | k r t | t |  <d  } xz | D]o } | r| | } | | } | j	 | j	 k sVt
  | j	 j | |  st | t |  <t } qn  | } qWq q W| rd  } xQ t  |   D]C \ } } x4 | j D]) }	 | |	 } | t |  r| } qqWqWt |  t d   n  d  S(   Nt   broken(   R   R   t
   setdefaultR(   R   R   R	   RY   RA   R5   R9  R   R!   R?   R[   (   R   R;  R	  t   equivalence_setst   program_positionRI   Rs   RQ   RT   R/   R<  R=  t   ert   equivalence_sets_brokent   there_is_a_problemt   r_equivt   re0t   reRS   R>  t   first_broken_set(    (    s8   /tmp/pip-build-X4mzal/theano/theano/compile/debugmode.pyt   _find_bad_optimizations19  sF    




c         ` sr   t       f d         f d     x8 t |   D]* \ } } x | j D] }   |  qV Wq@ Wd S(   sa  
    Use a simple algorithm to find broken optimizations.

    This algorithm is simple to understand, but sometimes when there's
    a problem it identifies the wrong optimization as the culprit.
    The problem stems from the fact that results are not evaluated in
    chronological order (looking at when they were introduced to the
    graph).

    c         ` s   x  |  D] \ } } } }   |  }   | } | j  |  j  k sY | j  j | |  r t d | d |  d | d | d | d | d |   q q Wd S(	   sQ   
        Verify that `r` has the same value as the results it replaces.

        RP   RQ   RR   RS   RT   RU   RV   N(   R5   R   RO   (   RQ   RT   R/   R<  R=  RS   R>  (   R	  R;  (    s8   /tmp/pip-build-X4mzal/theano/theano/compile/debugmode.pyt   check_variable_norecz  s    

c         ` s   |   k r d  S j  |   g   |  D] \ } } } } | ^ q( } d  |  j k	 rh | |  j j 7} n  x | D] }   |  qo W |   d  S(   N(   R    RA   R6   RB   (   R/   RT   RP   t   oldst   newst   list_of_varst   var_that_could_make_r_look_bad(   t   check_variableRM  t   checked_variablesR;  (    s8   /tmp/pip-build-X4mzal/theano/theano/compile/debugmode.pyRR    s    )N(   R(   R   R   (   R   R;  R	  RI   Rs   RQ   (    (   RR  RM  RS  R	  R;  s8   /tmp/pip-build-X4mzal/theano/theano/compile/debugmode.pyt   _find_bad_optimizations2l  s    	c   2   
   c` s  d d l  m } d d l m } m } | rO d d l m } d d l m } n  g  } x- |  j D]" } | |	 k r_ | j |  q_ q_ Wd | k s d | k r i  } x+ | D]# } | |
 k r |
 | | | <q q W| r d | f Vq n  d | k s d | k ri  } x6 | D]. } t	 | |  } | | | | <| | | <qWxO | D]G } t
 | j | | f  rFt j |  j | j j  | | d	 <qFqFW| rd | f Vn  ~ n  d
 | k sd | k rZi  } xn | D]f } t
 | j | | f  r| j j | | j  } t j |  j | j j  | d	 <| | | <qqWt |  rZd
 | f V~ qZn  d | k srd | k rLi  } x | D] } t
 | j | | f  rt j d | | j d | | j d d  } | | d	 <t
 | j |  r| | j  } | | t t t | j     } n  | | | <qqWt |  rLd | f V~ qLn  d | k spd | k spd | k rJd } g  } x | D] } t
 | j | | f  r| | j k  r| t g | j | 7} | j } n  t |  | k st  xA t | j d d d   D]  \ } } | | o"| | | <q	WqqW| d d d  } n  d | k sbd | k r[t j j } i  } x | D] } t
 | j | | f  r{g  } xg t  | | j | j  D]L \ }  } | s| j t |  | k r| j |   q| j |  d  qW| j j |  } t j |  j | j j  | d	 <| | | <q{q{Wg  }! x6 | | D]) } | r||! j d  q`|! j d  q`Wt! | |   rd g |! }! n d g |! }! xt" |!   D]}" xd D]{}# i  }$ |" d g t | |   }% |% g  |" d D] }  |  |# ^ q7}% d t# t$ |%   }& x | D] } | | k r?g  }' g  }( xW t | | j  D]B \ } }) |( j t% d |) d   |' j t% d d |% |   qqW| | }* |* j d k r|* t$ |'  t$ |(  }* n  |* j | | j k s	t  t j |  j |* j  |* d	 <|* |$ | <q?q?W|$ rM|& |$ f Vn  ~$ qWqWn  d | k ssd | k rx7t |  D]&\ }+ } | rqn  d g | }, x d D] }- |- |, |+ <i  }. d t# t$ |,   }& x | D] } t
 | j | | f  r|, | j  }/ g  t  | | j |/  D] \ }  }0 t' |  |0 d  ^ q!}1 | j j |1  } t j |  j | j j  | d	 <| |. | <qqW|. r|& |. f Vn  ~. qWqWn  d S(   s;   
    Preallocate outputs in different memory layouts.

    i    (   t
   TensorType(   t   cuda_availablet   CudaNdarrayType(   t   CudaNdarray(   t
   dimshufflet   initialt   ALLt   previous.t   c_contiguoust   f_contiguousR<   RZ   R   t   Ft   stridedt
   wrong_sizeNii   i   s	   strided%ss   wrong_size%s(   i   (   ii   (   i   (   ii   (   i   i   (   ii   ((   t   theano.tensorRU  t   theano.sandbox.cudaRV  RW  RX  RY  R   RC   R:  R   R5   R^   R_   t   astypeRZ   t   value_zerosR<   R]   t   zerost   Tt   reversedR   t   ranget   ndimR!   R9  R   t   broadcastableR   t	   DebugModet   check_preallocated_output_ndimR   R   t   itertools_productR\   t   tuplet   sliceRA   Ra   (2   Rs   t   thunkt   prealloc_modest   def_valR  R	  R
  Rw   t   active_order_sett   inplace_outst   init_outputsRU  RV  RW  RX  t   cuda_dimshufflet   considered_outputsR/   t   initial_outputst   reuse_outputsRQ   t   c_cont_outputst   new_buft   f_cont_outputst   max_ndimt   rev_out_broadcastableRI   R.  t   out_broadcastablet
   check_ndimt   init_stridedt	   buf_shapet   st   step_signs_listt
   step_signst	   step_sizeR`  t   stepsR   R=   t   shapesR>   t   r_buft   dimt
   shape_difft   diffRa  t   r_shape_difft   sdt	   out_shape(    (    s8   /tmp/pip-build-X4mzal/theano/theano/compile/debugmode.pyt   _get_preallocated_maps  s   	'"			
$	
)&"% !

,c         ` s  zt  } t t |  d d   t k rt t |  j  } t |  j | d  } | sy t | d  sy t | j d  r t j	 d |  j |  qt
 | j j t  r| j j } t j |  } t  | _ t  | _ d | _ g  | _ d | _ | | j _ t } t j d  qn  t   } t |  j d i   } t |  j d	 i   } x^ t |  j  D]M \   } t   f d
   t t |  t |   D  rW| j |  qWqWWt j d  x#t |  | | | | | | | | |	 |
  D]\ } } t j d |  d | | f } | s(t j	 d |  qn  x& | D] } t | |  | | d <q/Wx+ |  j D]  } | j  | d  | | d <q[W|   xe |  j D]Z } | j j! | | d  st" | | | d d | d | j j# | | d   qqWt$ |  | | | | d t  d | d t  t% |  |  x |  j D] } t& | | | | | d  s-g  | j' j D] } | | d ^ q^} t( | d d d | | d | d | | d d |  q-q-Wx |  j D] } d | | d <qWqWt j d  Wd | rt j d  | | j _ n  Xd S(   s=   
    Try to apply thunk() on different output storages.

    R7   t   makert   modes+   Expected theano function not found in %s.%si    i   s   changing inner modeRx   R   c         3` s   |  ] }   | k Vq d  S(   N(    (   R   R   (   RI   (    s8   /tmp/pip-build-X4mzal/theano/theano/compile/debugmode.pys	   <genexpr>  s    s%   starting preallocated output checkings     name = %ss   %s with %s outputs   %s: out_map is emptyR   R   R  Rw   R  R0   s   Reference valueR1   R2   R3   R4   s%   finished preallocated output checkingNs   changing mode back()   R   R5   R@   RA   R   R7   R   R  R  t   warnR   R  Rl  R7  t   check_py_codet   check_isfinitet   require_matching_stridest   check_preallocated_outputt   stability_patienceR!   t   infoR(   R   RB   t   anyR   R
   R    t   debugR  R:  R   R   t   is_valid_valueR   t   value_validity_msgR  R+  R  R6   R+   (   Rs   Rq  Rr  Rs  R  R	  R
  Rw   Rt  Ru  Rv  t   changed_inner_modet   fn_attr_namet   fnt   backup_modet   new_modet   aliased_inputsR  R  R/   R   t   out_mapt
   thunk_namet   inpR4   (    (   RI   s8   /tmp/pip-build-X4mzal/theano/theano/compile/debugmode.pyt   _check_preallocated_output  s    
							1	$	
t   _FunctionGraphEventc           B` sV   e  Z d  Z d Z d Z d Z d Z d Z d d d  Z	 d   Z
 d   Z d   Z RS(   s   
    A record of an event in the life of an FunctionGraph.

    The __eq__ function is important here, as it is the basis for
    comparing optimization runs.

    R,   c         C` sU   | |  _  | d k r* d |  _ d |  _ n | |  _ | j |  _ | |  _ | |  _ d  S(   NR   (   t   kindRs   R7   Rt   RT   (   R"   R  Rs   Rt   RT   (    (    s8   /tmp/pip-build-X4mzal/theano/theano/compile/debugmode.pyR.     s    				c         C` s   |  j  d k rp |  j d k r9 t t |  j j   } n d } d j d |  j t |  j  t |  j  | g  St |  j	  Sd  S(   Nt   changeR   R,   RX   (
   R  R7   R\   R]   Rs   RB   t   joinRT   Rt   t   __dict__(   R"   R$   (    (    s8   /tmp/pip-build-X4mzal/theano/theano/compile/debugmode.pyR:   $  s    	
c         C` sf   t  |   t  |  k } | rb xA d d d d g D]* } | oX t |  |  t | |  k } q1 Wn  | S(   NR  R7   Rt   RT   (   R5   R@   (   R"   t   otherR   t   attr(    (    s8   /tmp/pip-build-X4mzal/theano/theano/compile/debugmode.pyt   __eq__3  s
    +c         C` s   |  | k S(   N(    (   R"   R  (    (    s8   /tmp/pip-build-X4mzal/theano/theano/compile/debugmode.pyt   __ne__=  s    N(   R&   R'   R*   R  RA   Rs   R7   Rt   RT   R.   R:   R  R  (    (    (    s8   /tmp/pip-build-X4mzal/theano/theano/compile/debugmode.pyR    s   		
R   c           B` s   e  Z d  Z d Z d Z d Z d Z d Z d Z	 d Z
 d Z d   Z d   Z d   Z d   Z d   Z d d  Z d   Z RS(	   sh   
    A FunctionGraph Feature that keeps tabs on an FunctionGraph and
    tries to detect problems.

    c         C` s   d  |  _ d  S(   N(   RA   R   (   R"   (    (    s8   /tmp/pip-build-X4mzal/theano/theano/compile/debugmode.pyR.   `  s    c         C` s   |  j  d  k s t  i  |  _ t   |  _ t   |  _ | |  _  g  |  _ i  |  _ i  |  _	 g  |  _
 x' | j   D] } |  j | | d  qp Wd  S(   Nt	   on_attach(   R   RA   R9  t   equivR(   R  t   inactive_nodest   all_variables_everR;  t   replaced_byt
   event_listt   toposortt	   on_import(   R"   R   Rs   (    (    s8   /tmp/pip-build-X4mzal/theano/theano/compile/debugmode.pyR  c  s    						c         C` s"   | |  j  k s t  d  |  _  d  S(   N(   R   R9  RA   (   R"   R   (    (    s8   /tmp/pip-build-X4mzal/theano/theano/compile/debugmode.pyt	   on_detachp  s    c         C` sm   |  j  j t d | d |  | |  j k s4 t  | |  j k sI t  |  j j |  |  j j |  d  S(   Nt   pruneRT   (   R  RC   R  R  R9  R  t   removeR    (   R"   R   Rs   RT   (    (    s8   /tmp/pip-build-X4mzal/theano/theano/compile/debugmode.pyt   on_prunet  s    
c         C` sB  |  j  j t d | d |  | |  j k s4 t  |  j j |  | |  j k r |  j j |  x | j D] } | |  j	 k sm t  qm Wn xr | j D]g } | |  j	 k s t  t
 | g  |  j	 | <|  j j |  |  j j | g   |  j j | g   q Wx7 | j D], } |  j j | g   |  j j | g   qWd  S(   Nt   importRT   (   R  RC   R  R  R9  R    R  R  R   R  R(   R  R;  RB  R  RB   (   R"   R   Rs   RT   R/   (    (    s8   /tmp/pip-build-X4mzal/theano/theano/compile/debugmode.pyR  |  s"    
c         C` sx  |  j  j t d | d t |  d |  |  j j | g   |  j j | g   t } x> |  j | D]/ } | d | k re | d | k re t } qe qe W| rGt	   }	 t	   }
 |  j | j | | t
 | d d d d	 d
 t   d |	 d |
 j   t
 | d d d d	 d
 t   d |	 d |
 j   f  |  j | j | | f  n  | |  j k rf|  j | } n. |  j j | t | g   } |  j j |  | |  j k r|  j | } n. |  j j | t | g   } |  j j |  | | k st  | | k st  | j |  x- | D]% } | |  j | <| | k st  qW|  j | | k s[t  |  j | | k stt  d  S(   NR  RT   Rt   i    i   R   s     R   i   R;   R   R   (   R  RC   R  R\   R;  RB  R  R!   R   R   R   R   RG   R  R(   R  R9  R   (   R"   R   Rs   RI   R/   RQ   RT   t   append_reasont   tupR   R   t   r_sett	   new_r_sett
   like_new_r(    (    s8   /tmp/pip-build-X4mzal/theano/theano/compile/debugmode.pyt   on_change_input  sJ     		c         C` sG   x@ |  j  D]5 } t |  x" |  j  | D] } t d |  q( Wq
 Wd  S(   Ns     (   R  R?   (   R"   R'  R   (    (    s8   /tmp/pip-build-X4mzal/theano/theano/compile/debugmode.pyt
   printstuff  s    
N(   R&   R'   R*   RA   R   R  R  R  R  R;  R  R  R.   R  R  R  R  R  R  (    (    (    s8   /tmp/pip-build-X4mzal/theano/theano/compile/debugmode.pyR   A  s    					5t   _DummyLinkerc           B` s   e  Z d d   Z RS(   c         C` s   |  S(   N(    (   R"   t   allow_gc(    (    s8   /tmp/pip-build-X4mzal/theano/theano/compile/debugmode.pyt   clone  s    N(   R&   R'   RA   R  (    (    (    s8   /tmp/pip-build-X4mzal/theano/theano/compile/debugmode.pyR    s   t   _Linkerc           B` s>   e  Z d  Z d d  Z d d d  Z d d d d d  Z RS(   s$   
    Special debugging linker.

    c         C` s>   t  t j |   j   d  |  _ | |  _ | r: | |  _ n  d  S(   N(   R-   R   t   LocalLinkerR.   RA   R   R  t   schedule(   R"   R  R  (    (    s8   /tmp/pip-build-X4mzal/theano/theano/compile/debugmode.pyR.     s
    		c         C` s   | d  k r g  } n  |  j d  k	 rp |  j | k	 rp t |   t k sK t  t |   d |  j  j | | |  S| |  _ | |  _ |  S(   NR  (   RA   R   R5   R  R9  R  t   acceptt   no_recycling(   R"   R   R  R   (    (    s8   /tmp/pip-build-X4mzal/theano/theano/compile/debugmode.pyR    s    			c         ` so  d d l  m    j   }  }  j } t j | j  } ~ | j   t j  j	 |    j
   }	 t |	   g  }
 t j   | | 	  \   	 g   g  
 x" D]} i  } x | j	 D] } t g | | <q Wx | j D] } t g | | <q Wt } t | j j  t k r| j j | 	 | |
  } g  | j	 D] } 	 | ^ qK| _	 g  | j D] } 	 | ^ qn| _ | } n  t | j d  } y  j j j p| rt j    n  t | j t j j  rt j    n  | j j | 	 | d  | j j  | 	 | |
  } 
 j! |  Wn' t" t j f k
 r]
 j! t  n X j j j# pz
 d t k o| j j$ j% t j j& j$ j% k p| r| j j | 	 | d  | j j' | 	 | |
 d | }  j! |  n  j! t   j j j o d t k rpt( j) d | j  | j j | 	 | d  | j j  | 	 | |
  } | 
 d <n  | t k	 r d t k r|  d <n8 
 d t k r| 
 d <n t( j) d	 |  |  d <n  q W j* t k rt+ 	 j,     t j-     n/ g   j* D] } |  j	 k r7	 | ^ q t. t/ j0 j1           	 
  f d
   }    f d   } | |  } 	 | _2 t | _3 t4  j	  t4   k st5  t4  j  t4   k st5  | g  t6  j	   D]$ \ } } t j7 | | d t ^ qg  t6  j   D]$ \ } } t j7 | | d t ^ q>  f S(   Ni    (   RU  t   debug_performR   it   pyR  s9   Op %s doesn't have a perform, forcing check of the C codesZ   We won't check the perform function of node '%s' but we will check its make_thunk functionc          ` s  t  j d  t  j d 
 j j j  x  D] }  d  |  d <q- Wg   D] } | j d  k rH | ^ qH } yi  } i  } t   t   k s t  t  j d  g  } x  D] } | j d  k r | j	 j
  | d  sO | d d  k rt |  | d d d t |   n  t |  | d d d |  | d f  n   | d | | <d   | d <| j |  q q Wi  } xW  D]O } |  j k r | d d  k	 r | d | | <d   | d <qqqWxP t   D]B \ } } | d d  k	 rt | |  n  | d d  k st  qWxt t      D]\   \ }   t  j d       x  j D] } t | t j  st  | | k st  t | |   | d <| j	 j
  | d  st |  | d d	   qqW| r%t  j d
    y |   Wq%t j t f k
 rWd  } d     <q%t k
 r!}	  j j  j d }
 |
 s  n  t |
 d d  } d | d t |
 d d  d |
 d d d t |	  } |	 j |  } t j    \ } } } | } t!   | | | f  q%Xn  | rxk  j D]` } | j	 j
  | d  s5| j	 j"  | d  } t |  | d d d d |  q5q5Wt# j$ j% } t&   | |  d t' d d d | } t(    xC  j D]8 } | | k st   | d | | <d   | d <qW
 j j j r
 j j j } t  j d    t) d  d | d | d  d  d | d | d d d  d  | d! |  n  t j* j+   n   rz	t' } | r]t,  j- d" i   } t,  j- d# i   } xi t  j  D]X \   } t.   f d$   t/ | j0   | j0    D  rt | |   | d <qqWt1 } n  t  j d%    y    Wn t k
 rE}	  j j  j d }
 |
 s  n  t |
 d d  } d | d t |
 d d  d |
 d d d t |	  } |	 j |  } t j    \ } } } | } t!   | | | f  n Xx  j D] } | j	 j
  | d  st |  | d d d&  n  | rP| | k st  t2 | |  | d 
 j j j3  j-  qPqPWt# j$ j% } t&   | |  d | d d' d | } t(    x  j D] } | | k rt4 | | |  | d  sg  | j j D] }  | d ^ qd} t5 | d( d d) | | d* d+ d,  | d d- |  qn  | d | | <d   | d <q'W
 j j j rj	
 j j j }   f d.   } t  j d/    t) d  d | d | d  d  d | d | d d0 d  d  | d! |  n  t j* j+   n  x  j D] } d   | d <q	Wt  j d1    xw  j D]l } | | k r	 j6   }  | d  k s	t     | d  k s
t    t d2    q	q	WqOWt1 r=
t7 j8   n  t9   j j |  xH t   D]: \ } } t	 |  t: k s
t  | d d  k s`
t  q`
Wx. t  j 	  D] \ } } | | | d <q
Wxj | D]b } | j d  k r
|  j k r# |   j j6 |  k s#t  n  | |  | d <q
q
Wx | D]} | | d d  k	 sct  | j d  k rC|  j k st  t	 | | d  t; j< t; j= f k rB| | d j>  | d j> k rB| | d j?  | d j? k rBt | | d j?  r"| | d  | d (qX | d j@ | | d  q[| | d  | d <qCqCWWnc t k
 rxL  D]D } | | k r| | k r| |  | d <qqwd   | d <qwW  n XxO  D]G } | j d  k r| j	 j
 d   s | d d  k	 st  qqqWd  S(3   Ns   starting a DebugMode calls-   self.maker.mode.check_preallocated_output: %si    s"   DEBUGMODE: transfer initial valuesR   s   Graph Input '%s' is missings%   Graph Input '%s' has invalid value %ss   %i - starting node %i %sR   s1   %i - running thunk_py with None as output storagesI   An optimization (probably %s) inserted an apply node that raise an error.s5   
The information we have about this optimizations is:i   s   
i   s   

The original exception: 
s   perform outputR   R  Rw   R  R  s5   %i - calling _check_preallocated_output with thunk_pyRs   Rq  Rr  Rs  R  R	  R
  Rt  Ru  Rv  Rx   R   c         3` s   |  ] }   | k Vq d  S(   N(    (   R   R   (   RI   (    s8   /tmp/pip-build-X4mzal/theano/theano/compile/debugmode.pys	   <genexpr>7  s    s   %i - running thunk_cs   c outputR   R0   R1   R2   t   c_codeR3   R4   c           ` s0   y    Wn t  k
 r+ t     n Xd  S(   N(   R[   R   (    (   Rs   t   thunk_c(    s8   /tmp/pip-build-X4mzal/theano/theano/compile/debugmode.pyRq    s    s4   %i - calling _check_preallocated_output with thunk_cs   c codes   %i - done with nodes   No code run for %s(A   R  R  R  R  R  RA   R6   R]   R9  R5   R  R   R\   RC   R   R	   R?   R   R   RB   R   R   t   VariableR:  R   t   MethodNotDefinedt   NotImplementedErrorR[   R   R;  t	   __class__R,  t   exc_infoR   R  R   Rl  R  R  R!   R+  R  t   stdoutt   flushR@   R7   R  R   R  R   R1  R  R  R+   R   t   gct   collectt   _find_bad_optimizationsR   R^   R5  R6  RZ   R<   t   itemset(   t   xR/   t   original_storage_map_keysR	  R
  t   r_vals_initializedRv  R  t   thunk_pyR   RT   t   optR$   t   new_et   exc_typet	   exc_valuet	   exc_tracet   hint2t   warn_inpt   py_inplace_outsRr  t   clobberR  R  t   c_inplace_outsR  R4   Rq  Rt   R   t   storage(   Rt  Rs  R   t   input_storaget   no_recycling_mapR   t   output_storageR"   R  t   thunks_ct	   thunks_py(   RI   Rs   R  s8   /tmp/pip-build-X4mzal/theano/theano/compile/debugmode.pyt   fv  s   			>			
		>				$
		" !c         ` s       f d   } | S(   Nc          ` s7     j  }   j j j   _  z    SWd  |    _  Xd  S(   N(   t   filter_checks_isfiniteR  R  R  (   t   old_filter_checks_isfinite(   RU  R  R"   (    s8   /tmp/pip-build-X4mzal/theano/theano/compile/debugmode.pyt   deco  s
    	(    (   R  R  (   RU  R"   (   R  s8   /tmp/pip-build-X4mzal/theano/theano/compile/debugmode.pyt    run_with_tensortype_filter_check  s    t   readonly(8   Rb  RU  R   R   R7  R  t   reverseR   t   io_toposortRB   R  R(   R   t   map_storageR!   R   R   RA   R   R7   t
   make_thunkt   default_make_thunkR   R  R  t   check_c_codeR   R  R   R   t   Opt   prepare_nodet   make_c_thunkRC   R  R  Rw   t   __code__t   PureOpt   make_py_thunkR  R  R  R   R  t
   differenceR   R   t	   unittestst   rseedR  R  R]   R9  R   t	   Container(   R"   t   profilerR  R  R  t   input_storage_t   output_storage_t   fgraph_equivt   order_outputst   active_orderR  Rs   t   compute_mapt   kt   thunk_otherRq  R   R  R/   R  R  R   R  R   (    (   RU  Rt  Rs  R   R  R  R   R  R"   R  R  R  s8   /tmp/pip-build-X4mzal/theano/theano/compile/debugmode.pyt   make_all  s    		
		##		! 				- 		!!77N(   R&   R'   R*   RA   R.   R  R  (    (    (    s8   /tmp/pip-build-X4mzal/theano/theano/compile/debugmode.pyR    s
   t	   NODEFAULTt   _Makerc           B` sA   e  Z d  Z d Z e e d d d d d  Z d e d d  Z RS(   s  
    Special debugging FunctionMaker.

    Parameters
    ----------
    inputs : list of SymbolicInput instances
    outputs : list of SymbolicOutput instances
        Outputs may also be a single Variable (not a list), in which case
        the functions produced by FunctionMaker will return their output
        value directly.
    accept_inplace
        True iff it is acceptable to have inplace operations in the graph from
        the inputs to the outputs.
    on_unused_input
        What to do if a variable in the 'inputs' list is not used in the
        graph. Possible values are 'raise', 'warn' and 'ignore'.
    output_keys
        If the outputs argument for theano.function was a list, then
        output_keys is None. If the outputs argument was a dict, then
        output_keys is a sorted list of the keys from that dict.

    Notes
    -----
    The constructor sets TensorType.filter_checks_isfinite when
    `mode.check_isfinite` is True.

    i    c
      	   C` s  | |  _  | j }
 t } t } | d  k r9 t } g  } n  t | t t f  s` t } | g } n  t | t t f  s | g } n  g  | D] } |  j |  ^ q } g  | D] } |  j	 |  ^ q } t
 j j g  | D] } | j ^ q g  | D]! } t | d t  r | j ^ q  } |  j | | |  g  | D] } | g |  j | |  ^ q3} xt | j  D]|} t | | |  \ } } } | | _ t j j } zH t j j t j _ |
 |  t j j j | | t t | |    Wd  | t j _ X| d k r| } qh| j j } | j j } | | k rt   } t  d d | t  d d | t  d d | t  d d | x#t t! t" |  t" |    D] } | t" |  k rt  d | d | t  d	 t# | |  d | q| t" |  k r0t  d
 | d | d | t  d	 t# | |  d | q| | | | k rt  d | d | d | t  d	 t# | |  d | t  d	 t# | |  d | qqWt$ | j%     qh|  j& rht  d | d t" |  d d t' j( qhqhW| |  _) t* |   } g  t+ | j, | |  D] \ } } | j- s| ^ q} | r_| j. | d t/ | |  |  _0 n | j. |  |  _0 | |  _1 | |  _ | |  _2 | |  _, | |  _3 | |  _4 | |  _5 | |  _6 | |  _7 | |  _8 |	 |  _9 d  S(   NR   i    s,   WARNING: Optimization process is unstable...R;   s8     (HINT: Ops that the nodes point to must compare equal)s5   (event index)  (one event trace)  (other event trace)s5   -----------------------------------------------------s"   trailing event in optimization 0 :s      s   trailing event in optimizationt   :s   non-equal optimization eventss   OPTCHECK: optimizationt   ofs   events was stable.R  (:   R   t	   optimizerR   RA   R!   R   R   Ro  t   wrap_int   wrap_outR   R   RB   R   R@   R   t   _check_unused_inputst	   expand_inR   R  R   R   R   R   t   compute_test_valuet   compute_test_value_optt   compilet   function_modulet   insert_deepcopyR   R  R   R?   Ra   R]   R\   R   RG   t   verboseR,  R-  R   R  R   R   t   borrowR  R   t   linkert   indicest   expanded_inputst   unpack_singlet   return_noneR   t   function_builderR  t   on_unused_inputt   output_keys(   R"   RB   R   R  R   R&  R   R'  R   R(  R  R$  R%  RI   R   t   _inputsR   R"  t   additional_outputsR   t   compute_test_value_origt   fgraph0t   lit   l0t   infologt   jR!  R   R   t	   no_borrow(    (    s8   /tmp/pip-build-X4mzal/theano/theano/compile/debugmode.pyR.   ;	  s    			"""%)	
 						(   														c         C` s  | d k r% d g t |  j  } n  g  } g  } xht |  j |  D]T\ \ } } } }	 |	 }
 t |	 t j  r | d k	 r t d   n  | j	 |	 j
  d }	 n | j	 d g  | j d k	 r,|	 d k r| s t |
 t j  r| j	 t t d f  q)t d |   q| j	 t t |	 f  qD |	 d k r| sPt |
 t j  ri| j	 t t d f  q| j	 t t d f  qD | j	 t t |	 f  qD W| } |  j j d | d |  \ } } } |  j | | | |  j |  j | |  j |  j |  j |  
 } | S(   s  
        Create a function.

        Parameters
        ----------
        defaults
            A list matching the inputs list and providing default values if the
            default for an input is None, then that input is a required input.
            For an input with an update, the default acts as initialization.
        trustme
            Disables some exceptions, used internally.

        s@   Cannot take a Container instance as default for a SymbolicInput.sK   A default (initial) value is required for an input which can update itself.R  R  N(   RA   R]   RB   R   R"  R   R   R  R   RC   R  R   R   R   R!   R!  R  R&  R   R$  R%  R(  (   R"   t   defaultst   trustmeR  R  t	   _defaultsR   R"  t	   subinputst   defaultt   _Maker__defaultt   _fnt   _it   _oR  (    (    s8   /tmp/pip-build-X4mzal/theano/theano/compile/debugmode.pyt   create	  sB    	
N(	   R&   R'   R*   R  R   R   RA   R.   R;  (    (    (    s8   /tmp/pip-build-X4mzal/theano/theano/compile/debugmode.pyR  	  s   wc         C` s   t  d   d  S(   Ns    DebugMode is not picklable (yet)(   R  (   R  (    (    s8   /tmp/pip-build-X4mzal/theano/theano/compile/debugmode.pyt   _pickle_DebugMode_Maker
  s    Rl  c        	   B` s   e  Z d  Z e j j Z e j j Z e j j	 Z
 e j j Z e j j Z e j j Z e j d  Z d   Z d d d d d d d e   d  Z d   Z RS(   s  
    Evaluation Mode that detects internal theano errors.

    This mode catches several kinds of internal error:

    - Inconsistent outputs when calling the same Op twice with the same
      inputs, for instance if c_code and perform implementations, are
      inconsistent, or in case of incorrect handling of output memory
      (see `BadThunkOutput`).

    - A variable replacing another when their runtime values don't
      match.  This is a symptom of an incorrect optimization step, or
      faulty Op implementation (raises `BadOptimization`).

    - Stochastic optimization ordering (raises `StochasticOrder`).

    - Incomplete `destroy_map` specification (raises `BadDestroyMap`).

    - An op that returns an illegal value not matching the output
      Variable Type (raises InvalidValueError).

    Each of these exceptions inherits from the more generic `DebugModeError`.

    If there are no internal errors, this mode behaves like FAST_RUN
    or FAST_COMPILE, but takes a little longer and uses more memory.

    Raises
    ------
    DebugModeError
        If there are internal errors.

    Notes
    -----
    The work of debugging is implemented by the `_Maker`, `_Linker`,
    and `_VariableEquivalenceTracker` classes.

    R  c         O` s(   | |  k s t   t | | |  | |  S(   s[   
        Return an instance of `_Maker` which handles much of the debugging work.

        (   R9  R  (   R"   RI   R   t   mt   argst   kwargs(    (    s8   /tmp/pip-build-X4mzal/theano/theano/compile/debugmode.pyt   function_makerr
  s    t   fast_runc	   	      C` s   t  | t  s! t d |   n  t t |   j d | d |  | d k	 rX | |  _ n  | d k	 rp | |  _ n  | d k	 r | |  _	 n  | d k	 r | |  _
 n  | d k	 r | |  _ n  | d k	 r | |  _ n  |  j p |  j	 s t d   n  d S(   s   
        If any of these arguments (except optimizer) is not None, it overrides
        the class default. The linker argument is not used. It is set there to
        allow Mode.requiring() and some other fct to work with DebugMode too.

        sB   DebugMode can only use its own linker! You should not provide one.R  R!  s4   DebugMode has to check at least one of c and py codeN(   R   R  R[   R-   Rl  R.   RA   R  R  R  R  R  R  R   (	   R"   R  R  R  R  R  R  R  R!  (    (    s8   /tmp/pip-build-X4mzal/theano/theano/compile/debugmode.pyR.   z
  s&    c         C` s   d |  j  |  j f S(   Ns"   DebugMode(linker=%s, optimizer=%s)(   t   provided_linkert   provided_optimizer(   R"   (    (    s8   /tmp/pip-build-X4mzal/theano/theano/compile/debugmode.pyR:   
  s    N(   R&   R'   R*   R   Rl  t   patienceR  t   check_cR  t   check_pyR  t   check_finiteR  t   check_stridesR  R  t   splitR@  RA   R  R.   R:   (    (    (    s8   /tmp/pip-build-X4mzal/theano/theano/compile/debugmode.pyRl  
  s$   %		&t
   DEBUG_MODER  RA  (b   R*   t
   __future__R    R   R   R7  R,  R  t   loggingt   six.moves.copyregt   movest   copyregt	   itertoolsR   R   Rn  t   theano.compatR   t   numpyR^   R   R   R   R   t   sixR	   R
   t	   six.movesR   R   t
   theano.gofR   R   R   R   t   theano.gof.linkR   t   theano.compile.function_moduleR   R   R   R   R   R   t   theano.compile.modeR   R   t   theano.compile.opsR   t   __docformat__t	   getLoggerR  t   FilterR   t	   addFilterR[   R)   R+   RO   Rr   R   R   R   R   RA   R   R  R   R   R   R  R  R!   R  R+  R  R1  R:  R@  RL  RT  R  R  R  t   objectR  R   R  R  R  R  R  R  t
   _NODEFAULTR  R<  t   pickleRl  (    (    (    s8   /tmp/pip-build-X4mzal/theano/theano/compile/debugmode.pyt   <module>   s   ".	Y:#>			=		U	E			"	'	3	8		lL  8			