
Xc           @` s  d  Z  d d l m Z m Z m Z d d l Z d d l m Z m Z m	 Z	 d d l
 m Z d d l j j Z d d l j j Z d d l m Z d d l Z d d l 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 d d l Z d d l  Z d d
 l! m" Z" m# Z# m$ Z$ d d l% m& Z& m' Z' d d l( m) Z) d d l* m+ Z+ d d l, Z, e, j- d  Z. d Z/ d e0 f d     YZ1 d   Z2 d   Z3 d   Z4 d   Z5 d f  d     YZ6 e7 d  Z8 e j9 j: g e8 _; d e0 f d     YZ< d g Z= d e> f d     YZ? d   Z@ d   ZA e j e? e@  d    ZB d! g ZC d" e> f d#     YZD d$   ZE d%   ZF e j eD eE  g  ZG d&   ZH d'   ZI eJ e7 eJ eJ eJ eJ d(  ZK d)   ZL d*   ZM d S(+   s;   
Driver of graph construction, optimization, and linking.

i    (   t   absolute_importt   print_functiont   divisionN(   t   string_typest	   iteritemst   iterkeys(   t   xrange(   t   chain(   t   configt   gof(   t   izip(   t   graph(   t   Int   SymbolicInputt   SymbolicOutput(   t   deep_copy_opt   view_op(   t   is_same_graph(   t   ops_with_inner_functions   theano.compile.function_modules   restructuredtext ent   UnusedInputErrorc           B` s   e  Z d  Z RS(   s=   
    A symbolic input passed to function is not needed.

    (   t   __name__t
   __module__t   __doc__(    (    (    s>   /tmp/pip-build-X4mzal/theano/theano/compile/function_module.pyR   #   s   c         C` s   |  j  d k r |  St |  j  j d i   } t |  j  j d i   } |  j  j j |   } | j | g   | j | g   } t |  d k r t t	 |   d   n" | r t
 |  j  j | d  S|  Sd S(   sS   
    Return the variable to which v is aliased by view_maps and destroy_maps.

    t   view_mapt   destroy_mapi   s    is a view/destroyed version of more then one inputs. Currently, we only support the case where an output is a view or a destroyed version of one input.i    N(   t   ownert   Nonet   getattrt   opt   outputst   indext   gett   lent   NotImplementedErrort   strt
   alias_roott   inputs(   t   vt   vmapt   dmapt   outpost   v_views(    (    s>   /tmp/pip-build-X4mzal/theano/theano/compile/function_module.pyR#   ,   s    "c         C` s   | j  |   x |  j D] \ } } | d k r5 q n  t | j d i   } t | j d i   } xb t t |  t |   D]E \ } } | | k r{ | j | | k r t | j | |  q q{ q{ Wq Wd S(   s^   
    Add to `treeset` all variables that are views of v, given that v is
    not a view.

    t   outputR   R   N(   t   addt   clientsR   R   R   R   R   t   view_tree_set(   R%   t   treesett   clt   v_input_pos_to_clR&   R'   t   opost   iposlist(    (    s>   /tmp/pip-build-X4mzal/theano/theano/compile/function_module.pyR-   B   s    (c         C` sG   t    } x! | D] } t t |  |  q Wt  d   | D  } | S(   sM  
    Given an fgraph and a list of variables, returns the list or set
    of all variables which may share the same underlying data storage
    as any of the specified variables. Used internally by function,
    FunctionMaker.

    This list (or set) is also refered to as no_recycling sometimes,
    especially by linker code.

    c         s` s$   |  ] } | j  d  k	 r | Vq d  S(   N(   R   R   (   t   .0t   r(    (    s>   /tmp/pip-build-X4mzal/theano/theano/compile/function_module.pys	   <genexpr>c   s    (   t   setR-   R#   (   t   fgrapht   outputs_to_disownt   rvalt   o(    (    s>   /tmp/pip-build-X4mzal/theano/theano/compile/function_module.pyt   infer_reuse_patternT   s
    	c         C` s   i  } t  |  j  } t |  t |  j  k r? t d   n  xN t t  t | |  j    D]. \ } } | j d k	 r^ | j	   | | <q^ q^ W| S(   s   
    Reconstruct the full "updates" dictionary, mapping from FunctionGraph input
    variables to the fgraph outputs that will replace their values.

    Returns
    -------
    dict variable -> variable

    s-   expanded_inputs must match len(fgraph.inputs)N(
   t   listR   R    R$   t
   ValueErrort   reversedt   zipt   updateR   t   pop(   R6   t   expanded_inputst   updated_varst   potential_valuest   e_inputt   ivar(    (    s>   /tmp/pip-build-X4mzal/theano/theano/compile/function_module.pyt   fgraph_updated_varsh   s    
+t
   Supervisorc           B` s    e  Z d  Z d   Z d   Z RS(   s   
    Listener for FunctionGraph events which makes sure that no
    operation overwrites the contents of protected Variables. The
    outputs of the FunctionGraph are protected by default.

    c         C` s   t  |  |  _ d  S(   N(   R;   t	   protected(   t   selfRH   (    (    s>   /tmp/pip-build-X4mzal/theano/theano/compile/function_module.pyt   __init__   s    c         C` s\   t  | d  s t SxB |  j t | j  D]* } | j |  r* t j d |   q* q* Wd  S(   Nt
   destroyerss&   Trying to destroy a protectedVariable.(   t   hasattrt   TrueRH   R;   R   RK   R	   t   InconsistencyError(   RI   R6   R4   (    (    s>   /tmp/pip-build-X4mzal/theano/theano/compile/function_module.pyt   validate   s    	(   R   R   R   RJ   RO   (    (    (    s>   /tmp/pip-build-X4mzal/theano/theano/compile/function_module.pyRG   |   s   	c         ` s  g  |  D] } | j  ^ q } g  } i  } t |  } xR t t |    D]> } |  | j rG | j |  | j  | | | <| d 7} qG qG Wg  | D] } | j  ^ q | }	 t j j | |	 d |   x[   j D]P }
 t	 |
 j
 d d  r | st d |
 |
 j
   q  j t j    Pq q W  j t   f d   t |    j  D   x! t j D] }   j |    q[W  t t t |   f S(   s  
    Makes an FunctionGraph corresponding to the input specs and the output
    specs.  Any SymbolicInput in the input_specs, if its update field
    is not None, will add an output to the FunctionGraph corresponding to that
    update. The return value is the FunctionGraph as well as a list of
    SymbolicOutput instances corresponding to the updates.

    If accept_inplace is False, the graph will be checked for inplace
    operations and an exception will be raised if it has any. If
    accept_inplace is True, a DestroyHandler will be added to the FunctionGraph
    if there are any inplace operations.

    The returned FunctionGraph is a clone of the graph between the provided
    inputs and outputs.

    i   t   update_mappingR   s)   Graph must not contain inplace operationsc         3` sB   |  ]8 \ } } | j  p3 t   d   o3   j |  s | Vq d S(   RK   N(   t   mutableRL   RK   (   R3   t   spect   input(   R6   (    s>   /tmp/pip-build-X4mzal/theano/theano/compile/function_module.pys	   <genexpr>   s   		N(   t   variableR    t   rangeR?   t   appendR	   t   fgt   FunctionGrapht   apply_nodesR   R   R   t	   TypeErrort   attach_featuret   DestroyHandlerRG   R>   R$   t
   std_fgrapht   featuresR;   t   mapR   (   t   input_specst   output_specst   accept_inplaceRR   t   orig_inputst   updatesRP   t   out_idxt   inp_idxt   orig_outputst   nodet   feature(    (   R6   s>   /tmp/pip-build-X4mzal/theano/theano/compile/function_module.pyR]      s2    
 	t   AliasedMemoryErrorc           B` s   e  Z d  Z RS(   s0   
    Memory is aliased that should not be.

    (   R   R   R   (    (    (    s>   /tmp/pip-build-X4mzal/theano/theano/compile/function_module.pyRj      s   t	   DUPLICATEt   Functionc           B` s   e  Z d  Z d Z e Z e Z e Z e Z e Z	 e Z
 e Z e Z e Z e Z d   Z d   Z d   Z d   Z d   Z e e e e e d  Z d   Z e d	   e d
 d Z e d   e d
 d Z d   Z d   Z RS(   s  
    Type of the functions returned by theano.function or
    theano.FunctionMaker.create.

    `Function` is the callable object that does computation.  It has the storage
    of inputs and outputs, performs the packing and unpacking of inputs and
    return values. It implements the square-bracket indexing so that you can
    look up the value of a symbolic node.

    Functions are copyable via {{{fn.copy()}}} and {{{copy.copy(fn)}}}.
    When a function is copied, this instance is duplicated. Contrast with
    self.maker (instance of `FunctionMaker`) that is shared between copies.
    The meaning of copying a function is that the containers and their current
    values will all be duplicated. This requires that mutable inputs be
    copied, whereas immutable inputs may be shared between copies.

    A Function instance is hashable, on the basis of its memory
    address (its id).

    A Function instance is only equal to itself.

    A Function instance may be serialized using the `pickle` or
    `cPickle` modules.  This will save all default inputs, the graph,
    and WRITEME to the pickle file.

    A Function instance have a ``trust_input`` field that default to
    False. When True, we don't do extra check of the input to give
    better error message. In some case, python code will still return
    the good results if you pass a python or numpy scalar instead of a
    numpy tensor.  C code should raise an error if you pass an object
    of the wrong type.

    Attributes
    ----------
    finder
    inv_finder

    t   warnc         ` s/  | |  _  | |  _ | |  _ | |  _ | |  _ | |  _ | |  _ | |  _ |
 |  _ d  |  _
 t |  _ d  |  _ g  |  _ |	 |  _ t |  j  } i    i  }  f d   } g   d  x\t t |  j |   D]B\ } \ \  } } \ } } } | d  k r | d } t  d t  | _ t  d d   | _ | d  k	 rkt | t j  r_| sht  qk| | _ n  | | _  j | _ d | _ |   | <|    j < j   k r|    j <n t    j < j d  k r d 7 n  j   j   | | <g  | d *q q W  |  _! | |  _" d t# f     f d     Y} d t# f   f d	     Y} |   |  _$ |   |  _% t& |  j j'  t& |  j  k st  t& |  j  |  _( x5 |  j j' D]'   j) d  k	 r|  j( d 8_( qqWx< |  j j* j+ D]+ } | j, t- k r|  j j  | j,  qqWd  S(
   Nc         ` s7     j  | |  |  x | D] } | j d 7_ q Wd  S(   Ni   (   t
   distributet   provided(   t   indicest   cst   valuet   c(   RS   (    s>   /tmp/pip-build-X4mzal/theano/theano/compile/function_module.pyRn     s    i    t   strictt   allow_downcasti   t   ValueAttributec           ` s;   e  Z   f d    Z     f d   Z   f d   Z RS(   c         ` s   y   | } Wn' t  k
 r7 t d t |    n X| t k r] t d t |    n  t | t j  rv | j St  d  S(   Ns   Unknown input or state: %ssZ   Ambiguous name: %s - please check the names of the inputs of your function for duplicates.(	   t   KeyErrorRZ   R"   Rk   t
   isinstanceR	   t	   ContainerRr   R!   (   RI   t   itemt   s(   t   finder(    s>   /tmp/pip-build-X4mzal/theano/theano/compile/function_module.pyt   __getitem__  s    c         ` s   y   | } Wn< t  k
 rL t    } t d t |  | f   n X| t k rr t d t |    n  t | t j  r | | _ | j	 d 7_	 n
 | |  d  S(   Ns   Unknown input or state: %s. %ssZ   Ambiguous name: %s - please check the names of the inputs of your function for duplicates.i   (
   Rw   t   get_info_on_inputsRZ   R"   Rk   Rx   R	   Ry   Rr   Ro   (   RI   Rz   Rr   R{   t   msg(   R|   t   n_unnamed_inputst   named_inputs(    s>   /tmp/pip-build-X4mzal/theano/theano/compile/function_module.pyt   __setitem__  s    	c         ` s     j  |  S(   N(   t   __contains__(   RI   Rz   (   R|   (    s>   /tmp/pip-build-X4mzal/theano/theano/compile/function_module.pyR     s    (   R   R   R}   R   R   (    (   R|   R   R   (    s>   /tmp/pip-build-X4mzal/theano/theano/compile/function_module.pyRv     s   t   ContainerAttributec           ` s&   e  Z   f d    Z   f d   Z RS(   c         ` s     | S(   N(    (   RI   Rz   (   R|   (    s>   /tmp/pip-build-X4mzal/theano/theano/compile/function_module.pyR}     s    c         ` s     j  |  S(   N(   R   (   RI   Rz   (   R|   (    s>   /tmp/pip-build-X4mzal/theano/theano/compile/function_module.pyR     s    (   R   R   R}   R   (    (   R|   (    s>   /tmp/pip-build-X4mzal/theano/theano/compile/function_module.pyR     s   (.   t   fnt   input_storaget   output_storageRp   R   t   defaultst   unpack_singlet   return_nonet   makerR   t   profilet   Falset   trust_inputt   namet   nodes_with_inner_functiont   output_keysR;   t	   enumerateR>   R   Rt   Ru   Rx   R	   Ry   t   AssertionErrorRr   t   requiredt   implicitRo   RT   Rk   RV   R|   t
   inv_findert   objectt   _valuet
   _containerR    RA   t   n_returned_outputsR?   R6   RY   R   R   (   RI   R   R   R   Rp   R   R   R   R   R   R   t
   containersR   Rn   t   it   sinputsR   t   refeedRr   Rs   Rv   R   Rh   (    (   R|   RS   R   R   s>   /tmp/pip-build-X4mzal/theano/theano/compile/function_module.pyRJ   j  sr    														:
		

		"''c         C` s   |  j  j |  S(   N(   Rr   R   (   RI   Rz   (    (    s>   /tmp/pip-build-X4mzal/theano/theano/compile/function_module.pyR     s    c         C` s   |  j  | S(   N(   Rr   (   RI   Rz   (    (    s>   /tmp/pip-build-X4mzal/theano/theano/compile/function_module.pyR}     s    c         C` s   | |  j  | <d  S(   N(   Rr   (   RI   Rz   Rr   (    (    s>   /tmp/pip-build-X4mzal/theano/theano/compile/function_module.pyR     s    c         C` s
   |  j    S(   s   
        Copy a function. Copied function have separate intermediate
        storages and output storages with original function
        (   t   copy(   RI   (    (    s>   /tmp/pip-build-X4mzal/theano/theano/compile/function_module.pyt   __copy__  s    c   '      C` s3  d   } |  j  } g  | j D] } t j |  ^ q }	 | rY | j j t | j   }
 n | j j }
 t j | j j |
  } t j	 j
 g  | j j D] } | | ^ q g  |
 D] } | | ^ q d t } t t t | j t | j     } x, t | j |  D] \ } } | j | _ qW| d k	 r3g  | j D] } | j ^ q6} x6 t |  D]( } | | k rXt d | j   qXqXWx t t |	 | j   D] \ } \ } } | j | j } | | k r| | } | | j |  | | _ | j | _ | j   } | | j | <| j | | d d qqWn  t |  } xe t |	 | j  D]Q \ } } | | _ | r| j d k	 r| j | | _ | d 7} qRd | _ qRW|  j j } i  } | r| j j | j j } x8 | j   D]' } | | k r| | | | | <qqWn  | r0|  j r0|  j d } n  g  |	 D] } | j ^ q7} | d k rmt j  pgt j! } n  | t" k r| r| } n t# | j  j$  d } t% j& j' j( d |  } n- t) |  t# k rt% j& j' j( d |  } n  | j* d	 |	 d
 | d | d | j+ d | d d d | j, d t"  j- | d | }  x t | j |  j  j |  j. |  j.  D] \ }! }" }# }$ | d k	 o|! j | k }% |! j/ r|% r|# j0 |$ _0 |! j |" _ n  |  j1 j2 |" j  }& |% s|& |  j1 |! j <|! j |" _3 q\|& |  j1 | |! j <| |! j |" _ q\W| |  _ | |  j  j _ |  S(   s  
        Copy this function. Copied function will have separated maker and
        fgraph with original function. User can choose whether to separate
        storage by changing the share_memory arguments.

        Parameters
        ----------
        share_memory : boolean
            When True, two function share intermediate storages(storages except input and
            output storages). Otherwise two functions will only share partial
            storages and same maker. If two functions share memory and
            allow_gc=False, this will increase executing speed and save memory.

        swap : dict
            Dictionary that map old SharedVariables to new
            SharedVariables. Default is None.
            NOTE: The shared variable swap in only done in the new returned
            function, not in the user graph.

        delete_updates : boolean
            If True, Copied function will not have updates.
        name : string
            If provided, will be the name of the new
            Function. Otherwise, it will be old + " copy"

        profile :
            as theano.function profile parameter

        Returns
        -------
        theano.Function
            Copied theano.Function
        c         S` s   t  j j j } t |  |  s< t d |  d t |   f   t | |  si t d | d t |   f   |  j | j k s t d d | j d |  j f   d S(	   s   
            Assert two SharedVariable follow some restirctions:
                1. same type
                2. same shape or dim?
            s,   Key of swap should be SharedVariable, given:s    types.   Value of swap should be SharedVariable, given:t   types8   Type of given SharedVariable conflicts with original ones   Type of given SharedVariable:s    Type of original SharedVariable:N(   t   theanot   tensort	   sharedvart   SharedVariableRx   R   R   (   t   sv_orit   sv_rplR   (    (    s>   /tmp/pip-build-X4mzal/theano/theano/compile/function_module.pyt   checkSV-  s    	t   clones   SharedVariable: %s not foundt   reasons   Swap SVi   s    copyt   messageR$   R   R6   t   modeR   t   on_unused_inputt   ignoret   function_builderRb   t   storage_mapN(4   R   R$   R   R6   R   R    R   t   clone_get_equivR	   RW   RX   R   R;   R_   R   R>   t   borrowR   RT   R   R<   R   R   t	   containerRr   R   t   replaceR?   R   R   t   keysR   R   t   print_global_statsRM   R"   R   R   t   compilet	   profilingt   ProfileStatsR   t	   __class__R   R   t   createR   RQ   t   dataR|   R@   t   vairable('   RI   t   share_memoryt   swapt   delete_updatesR   R   R   R   RS   t   inst   out_varst   memoR   R9   t   fg_cpyt   outst   out_orit   out_cpyt	   exist_svst   svR   t   in_vt   vart   swap_svt   update_it   in_varR   t   new_storage_mapt   i_o_varst   keyR   R   t   f_cpyt   in_orit   in_cpyt   orit   cpyt   swappedR   (    (    s>   /tmp/pip-build-X4mzal/theano/theano/compile/function_module.pyR   	  s    $		%)	%+
	 						c   $   
   ` s	   f d   }  j  } t j   } | j d d  } | d k	 ry  j d k	 ry g  | D] }  j j |  ^ qX } n   j r d } x#| D]* }	  j | }
 |	 |
 j d <| d 7} q Wnx  j D] } d | _	 q Wt
 |  t
 |  t
  j  k rt d   n  d } x| D]}	  j | }
 |	 d k rM|	 |
 j d <nDy/ |
 j j |	 d |
 j d |
 j |
 j d <Wnt k
 r} d } d	 }  j r| d
  j d 7} n  t |	 d  r|	 j r| d
 |	 j d 7} n  t j j j  j j | j  } t
 | j  d k rVd | d | d | | f | j d f | _ n- d | d | d | | f f | j | _ |     n X|
 j	 d 7_	 | d 7} qW| rx' t |  D] \ } }	 |	  | <qWn   j rt  d t  rg  } xt t
  j   D]}  j j | j }  j | j d } t | j d  rPt } x t t
 |   D] } t  g  | | D] }  j j | j ^ qg  | | D] }  j | j d ^ q } t! j" g  | D]3 \ } } | j | j k o	| j j# | |  ^ q rnt } | | j$ |  PqnqnW| sP| j$ | g  qPn  xc | D][ } t
 |  d k rWx@ | d D]1 } t% j%  j | j d   j | j d <qzWqWqWWqWn   j sx  j D] } | j& r| j	 r|   t d t  j' | d  j' |    n  | j	 d k r`|   t d t  j' | d  j' |    n  | j( r| j	 d k r|   t d t  j' | d  j' |    qqWn  t j   } y. | d k r j)   n  j) d |    Wn t k
 r|   t  j) d  rd } t  j) d  rG j) j*  j) j+ } n  t j, j- d  j) j.  j) j+ d | d t  j) d d   q  n Xt j   | }  j j/ j0 | 7_0 | r| j1 | 7_1 n    d k rg   j2 D] } | j3 ^ q  n  t
    t
  j2  k st4  x*  j D] } | j& r%d | j d <q%q%Wt  j) d t  rt
  j2  t
  j j5 j6  k st4  xK t7  j2  j j5 j6  D]+ \ } }  |  j8 d k	 rd | j d <qqWn  t  j) d t  rCxc t9 t: t7  j j;  j    D]- \ }! }" |! j< d k	 r  j   |" _3 qqWn    j=    |   t j   | }# t j> j? j@ |# 7_@  j j/ jA |# 7_A | r| jB d 7_B | jC |# 7_C t  j) d  r j) jD |  n  | jE r| jF   t | _E qn   jG r		d S jH r8	t
    d k r8	| d k r8	  d S j d k	 r	t
  j  t
    k sh	t4  | d k r	tI t   j     StI    f d   | D  Sn  | d k r	  Sg  | D] }   | ^ q	Sd S(    sS  
        Evaluates value of a function on given arguments.

        Parameters
        ----------
        args : list
            List of inputs to the function. All inputs are required, even when
            some of them are not necessary to calculate requested subset of
            outputs.

        kwargs : dict
            The function inputs can be passed as keyword argument. For this, use
            the name of the input or the input instance as the key.

            Keyword argument ``output_subset`` is a list of either indices of the
            function's outputs or the keys belonging to the `output_keys` dict
            and represent outputs that are requested to be calculated. Regardless
            of the presence of ``output_subset``, the updates are always calculated
            and processed. To disable the updates, you should use the ``copy``
            method with ``delete_updates=True``.

        Returns
        -------
        list
            List of outputs on indices/keys from ``output_subset`` or all of them,
            if ``output_subset`` is not passed.
        c          ` sb   x[ t    j  D]J \ }  \ } } } | r t | t j  rM | j d } n  |   |  <q q Wd  S(   Ni    (   R   R   Rx   R	   Ry   t   storage(   R   R   R   Rr   (   RI   (    s>   /tmp/pip-build-X4mzal/theano/theano/compile/function_module.pyt   restore_defaults  s
    %t   output_subseti    i   s,   Too many parameter passed to theano functionRt   Ru   s   theano functiont   arguments    with name "t   "R   s
   Bad input s    to s    at index %d (0-based). %st   _check_for_aliased_inputst   may_share_memorys   Missing required input: %sRT   s   Multiple values for input: %ss-   Tried to provide value for implicit input: %st   position_of_errort   thunksRh   t   thunkR   t   allow_gct   need_update_inputst   update_profilec         3` s&   |  ] }  j  |   | f Vq d  S(   N(   R   (   R3   R   (   R   RI   (    s>   /tmp/pip-build-X4mzal/theano/theano/compile/function_module.pys	   <genexpr>  s   N(J   R   t   timeR@   R   R   R   R   R   R   Ro   R    RZ   R   t   filterRt   Ru   t	   ExceptionR   RL   R   R	   t   utilst   get_variable_trace_stringR   R$   RT   t   argsR   R   RM   R   R   R
   t   npt   anyR   RV   R   R   R   R   R   R   R   t   linkt   raise_with_opt   nodesR   t   fn_timet   vm_call_timeR   R   R   R6   R   R>   R   R=   R;   RA   R?   R   R   R   t   total_fct_exec_timet	   call_timet   fct_callcountt   fct_call_timeR   t   ignore_first_callt   resetR   R   t   dict($   RI   R   t   kwargsR   R   t   t0R   R   R   t   argR{   Rs   t   et   function_namet   argument_namet   wheret   kt   args_share_memoryt   i_vart   i_valt
   is_aliasedt   jt   group_jR   t   valt   groupt   idxt   t0_fnR   t   dt_fnt   xt   o_containert
   o_variableRS   R   t   dt_call(    (   R   RI   s>   /tmp/pip-build-X4mzal/theano/theano/compile/function_module.pyt   __call__  s0   	(	%		4
$+	@7	"!	*	
	!c         C` s   |  j  S(   N(   R   (   RI   (    (    s>   /tmp/pip-build-X4mzal/theano/theano/compile/function_module.pyt   <lambda>  s    t   docs>   dictionary-like access to the values associated with Variablesc         C` s   |  j  S(   N(   R   (   RI   (    (    s>   /tmp/pip-build-X4mzal/theano/theano/compile/function_module.pyR    s    sB   dictionary-like access to the containers associated with Variablesc         C` s   t  |  j d t  s x@ |  j j D]2 } t | t j j  s" d |  j j | d <q" q" Wx% |  j	 D] } t
 | j j   qb Wn  d S(   sK   
        When allow_gc = False, clear the Variables in storage_map
        R   i    N(   R   R   RM   R   Rx   R   R	   t   ConstantR   R   R   R   t   free(   RI   R   Rh   (    (    s>   /tmp/pip-build-X4mzal/theano/theano/compile/function_module.pyR    s    c         C` s)   g  |  j  j D] } | j r | j ^ q S(   sQ   
        Return the shared variable read or updated by by this function.
        (   R   R$   R   RT   (   RI   R   (    (    s>   /tmp/pip-build-X4mzal/theano/theano/compile/function_module.pyt
   get_shared  s    (   R   R   R   t   pickle_aliased_memory_strategyR   R   R   Rp   R   R   R   R   R   R|   R   RJ   R   R}   R   R   R   R   R  t   propertyRr   R   R  R  (    (    (    s>   /tmp/pip-build-X4mzal/theano/theano/compile/function_module.pyRl      s<   &										c         C` s  t  |  j  } g  } xM t |  j |  j  D]6 \ \ } } } \ } } } | j | d  | d =q+ Wg  |  j D] }	 |	 j ^ qo }
 |  j d k sf| |
 } x t |  D] \ } } x t |  D] \ } } | | k  r t	 | t
 j  r t	 | t
 j  r t
 j | |  r[|  j d k rFt j d t |  t |  f  qXt | |   q[q q Wq Wn  t |  j | |
 f f } | S(   Ni    R   Rm   se   aliased relationship between Function arguments %s, %s will not be preserved by un-pickling operation(   R;   R   R>   Rp   R   RV   R   R  R   Rx   R   t   ndarrayR   t   _loggert   warningR"   Rj   t   _constructor_FunctionR   (   t   fR   R   RS   Rp   R$   R   R   t   defaultR  t   inputs_datat   all_dataR   t   d_iR  t   d_jR8   (    (    s>   /tmp/pip-build-X4mzal/theano/theano/compile/function_module.pyt   _pickle_Function  s(    1
	 c         C` s   t  j j s d  S|  j | d t } t | j  t |  k sF t  xk t	 | j |  D]W \ } } | j
 | k sY t | t j  r | j
 | k j   sY | j
 | k sY t  qY W| S(   Nt   trustme(   R   R   t   unpickle_functionR   R   RM   R    R   R   R>   R   Rx   R   R  t   all(   R   R   R  R  R   R  (    (    s>   /tmp/pip-build-X4mzal/theano/theano/compile/function_module.pyR    s    !'c         C` s  t  |  t  |  j  k s! t  t  |  t  |  j  k sB t  d } t g  t | |  j  D]$ \ } } t | d t  r^ | ^ q^  } t j	 j |  j  } t
 |  d  } x1t t  |  j   D]} t   }	 t t |  j |  |	  t }
 x t | d t  |  j   D] } |  j | |	 k r| | j rm| | j rm|  j d | t |  j |  d | n& |  j d | t |  j |  d | t }
 PqqW|
 s x5| D]*} | | k rqn  | |	 k r| o|  j |  r| |  j k rw|  j j |  } | | j rM| | j rM|  j d | t |  j |  d d Pq|  j d | t |  j |  d d Pq| | j r|  j d | t |  j |  d d Pq|  j d | t |  j |  d d PqqWq q Wd S(   sD   
    Insert deepcopy in the fgraph to break aliasing of outputs
    t   insert_deepcopyR?   t   get_destroyers_ofi   R*   R   N(   R    R$   R   R   R5   R>   R   R   R	   R   RL   R   R-   R#   R   t   change_inputR   R   RM   R'  R   (   R6   t   wrapped_inputst   wrapped_outputsR   R   t   fgraph_it   updated_fgraph_inputst   all_graph_inputst   has_destroyerst   views_of_output_it   copiedR  t   input_j(    (    s>   /tmp/pip-build-X4mzal/theano/theano/compile/function_module.pyR&  '  sd    !!!	#
"t	   NODEFAULTt   FunctionMakerc           B` s}   e  Z d  Z e d    Z e d    Z e d    Z d   Z d e	 e
 d d d d d  Z d   Z d e	 d d  Z RS(	   s  
    `FunctionMaker` is the class to `create` `Function` instances.

    This class has the fgraph, the optimizer, and the linker. When
    copying a `Function`, there is no need to duplicate the
    `FunctionMaker` instance. Deepcopy still copies both, which can
    variable in re-compilation.

    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.
    mode : Mode instance
        Telling FunctionMaker how to optimize and link. None means to use the
        `config.mode`.
    accept_inplace : bool
        True iff it is acceptable to have inplace operations in the graph from
        the inputs to the outputs.
    on_unused_input : {'raise', 'warn', 'ignore', None}
        What to do if a variable in the 'inputs' list is not used in the graph.
        Possible values are:
        - 'raise': raise an error
        - 'warn': log a warning
        - 'ignore': do not do anything
        - None: Use the value in the Theano flags on_unused_input.

    c         C` s   t  |  t  r |  St  |  t j  r/ t |   St  |  t t f  r t |   d k rn t |  d d |  d St d |    n t d t |   |    d  S(   Ni   i    R?   i   s+   Expected two elements in the list or tuple.s7   Unknown input type: %s (%s), expected Variable instance(	   Rx   R   R	   t   VariableR;   t   tupleR    RZ   R   (   RS   (    (    s>   /tmp/pip-build-X4mzal/theano/theano/compile/function_module.pyt   wrap_in  s    
c         C` s    t  |  t  r d  |  g g Sd  S(   N(   Rx   R   R   (   t   sinputt   rinputs(    (    s>   /tmp/pip-build-X4mzal/theano/theano/compile/function_module.pyt	   expand_in  s    c         C` sK   t  |  t  r |  St  |  t j  r/ t |   St d t |   |    d  S(   Ns   Unknown output type: %s (%s)(   Rx   R   R	   R4  RZ   R   (   R*   (    (    s>   /tmp/pip-build-X4mzal/theano/theano/compile/function_module.pyt   wrap_out  s    
c         ` s  d d l  m } m } d d  l   j j t j j d    g  | D] } | j	 ^ qD  g  | D] } | j	 ^ q`  t
 |  j j   |      f d   }    f d   }	 |   }
 t d   t
 |
  f  |	 |
  } | r | |  _ d  } n t d  t t j j |  j j |  j j   |  j _ |  j j d t  } | |  j  } |
 j i |  j | 6 t   d	   } t j |
 | d
  Wd  QXt d  |   | S(   Ni    (   t   get_lockt   release_locks   optimized_graphs.pklc          ` s    j  j    r t d  n& t   d   }  t d    Wd  QXz yM t   d  . }  t j j } t t j _ t j	 |   } Wd  QXt d  Wn- t
 k
 r } t |  t d  i  } n XWd  | t j _ X| S(   Ns   graph_db already existst   wbs   create new graph_db in %st   rbs#   graph_db loaded and it is not emptys   graph_db loaded and it is empty(   t   patht   isfilet   printt   openR   R   R$  R   t   picklet   loadt   EOFError(   R  t   tmpt   graph_dbR   (   t   graph_db_filet   os(    s>   /tmp/pip-build-X4mzal/theano/theano/compile/function_module.pyt   load_graph_db  s"     

c         ` s  d  } xt |   D]~\ } } | j } | j } t | j  } t   t |  k rh t d  q q t   t |  k r t d  q q t d   t  |  D  s t d  q q t d   t  |  D  s t d  q q |  k s
t d  q q g  } xXt	 t  |   D]A\ } \ }	 }
 t d  t
 t j j | j | j   | _ | j d	 t  } |	 } | j | }   f d
       |  } t t t j j | g  t j j | g    } t t t j j | g  t j j | g    } x6 t |  D]( \ } } | j | j k r| | =qqWt | | d | } | j |  q&Wt |  } | r t d  | } Pq q W| S(   Ns1   need to optimize, because input size is differents2   need to optimize, because output size is differentc         s` s'   |  ] \ } } | j  | j  k Vq d  S(   N(   R   (   R3   t	   input_newt	   input_old(    (    s>   /tmp/pip-build-X4mzal/theano/theano/compile/function_module.pys	   <genexpr>  s   s7   need to optimize, because inputs are of different typesc         s` s'   |  ] \ } } | j  | j  k Vq d  S(   N(   R   (   R3   t
   output_newt
   output_old(    (    s>   /tmp/pip-build-X4mzal/theano/theano/compile/function_module.pys	   <genexpr>  s   s8   need to optimize, because outputs are of different typessA   need to optimize, because numbers of nodes in graph are differents)   loop through outputs node for both graphst   check_integrityc         ` s   t  |  d  r |  ` n  t  |  d  r |  j d  k r9 q t  |  j d  rW |  j ` n  t  |  j d  r g  |  j j D] }   |  ^ qv |  j _ x/ |  j j D] } t  | d  r | ` q q Wq n  |  S(   NR6   R   R$   (   RL   R6   R   R   R$   R   (   t   removeR   R9   (   t   removeAllFgraph(    s>   /tmp/pip-build-X4mzal/theano/theano/compile/function_module.pyRQ  *  s    	(t   givenss"   found a match, no need to optimize(   R   R   R$   R   R    RY   RA  R%  R>   R   R5   R	   R   t	   variablesR   R   R   R
   R   R   RV   (   RG  t   found_graph_in_dbt	   graph_oldt   graph_optimizedt
   inputs_oldt   outputs_oldt   size_oldt   flagsR   RM  RN  t   f2t   t1t   t2RR  t   tempR   Rr   t   flagt   is_same(   t
   inputs_newt   outputs_newt   size_new(   RQ  s>   /tmp/pip-build-X4mzal/theano/theano/compile/function_module.pyt   find_same_graph_in_db  s`    		

	
	

"

s    loaded graph_db from %s, size=%ds1   graph not found in graph_db, optimizing the graphRO  R=  is   new graph saved into graph_db(   t   theano.gof.compilelockR;  R<  t   os.pathR?  t   joinR   R   t
   compiledirRT   R    R6   RY   RA  R   R5   R	   R   RS  R$   R   R   R   R?   RB  RC  t   dump(   RI   t	   optimizerR$   R   R;  R<  t   inpt   outRJ  Rd  RG  t   found_grapht   optimizer_profilet
   before_optR  (    (   RH  Ra  RI  Rb  Rc  s>   /tmp/pip-build-X4mzal/theano/theano/compile/function_module.pyt   optimize_graph_with_cache  s8    	b				
!
c
         C` s  t  j j j |  } t | d d   r6 t d   n  | |  _ | r^ t  j j	 j
   j   n  | |  _ t }
 t } | d  k r 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  rC| j ^ qC } |  j | | |  g  | D] } | g |  j | |  ^ q} | d  k rt } t | | |  \ } } | | _ n@ t } g  | D] } | j r| j ^ q} t t t |   } | |  _ | j t j | j   } } | rt  j! j" } t  j! j# j$ } zt  j! j% t  j! _" t  j! j# j& t  j! j# _$ t' j'   } t  j! j( r|  j) | | |  } n | |  } t' j'   } | | } t  j j* j+ | 7_+ | r3| j, | 7_, t  j! j- rU| | f | _. qUn" t  j! j- rUt/ j0 d d d n  t1 j2 d |  t3 | | | |  Wd  | t  j! _" | t  j! j# _$ Xn  t4 | d  st5 d	 t t  j j j6 j7      n  t8 | j9  t8 | |  k st:  g  t; | j9 | |  D] \ } } | j< s| ^ q} | r`| j= | d
 t> | |  d | |  _  n | j= | d | |  _  t4 | d  r|  j  j? t@ | |   n  | |  _A | |  _ | |  _B | |  _9 |
 |  _C | |  _D | |  _ | |  _E | |  _F | |  _G |	 |  _H g  |  j D] } | jI d  k ^ q|  _J g  |  j D]: } | jI d  k	 oot | jI t jK  oo| j d  k ^ q8|  _L d  S(   NR   s7   profile passed via 'mode'. This isn't supported anymoreR?   sK   config.profile_optimizer requires config.profile to  be set to True as wellt
   stackleveli   s   Optimizing took %f secondst   acceptsY   'linker' parameter of FunctionMaker should be a Linker with an accept method or one of %st   no_recyclingt   accept_var_updates(M   R   R   R   t   get_modeR   R   RZ   R   R	   t   cct   get_module_cachet   refreshRg   R   RM   Rx   R;   R5  R6  R:  R   R$   RT   R?   t   _check_unused_inputsR9  R]   R_   R   R6   Rj  R   t   linkerR   t   compute_test_valuet	   tracebackt   limitt   compute_test_value_optt   compile_limitR   t   cache_optimizationsRp  R   t   total_graph_opt_timet   optimizer_timet   profile_optimizerRn  t   warningsRm   R  t   debugR&  RL   R<   t   predefined_linkersR   R    R   R   R>   R   Rr  R:   Rt  RF   Rp   RA   R   R   Rb   R   R   R   Rr   R   Ry   R   (   RI   R$   R   R   Rb   R   R   R   R6   R   R   R   R   R9   t   _inputsRS   Rp   t   need_optt   additional_outputsRR   Rd   Rj  Rz  t   compute_test_value_origt
   limit_origt   start_optimizerRn  t   end_optimizert   opt_timeR*   t	   no_borrow(    (    s>   /tmp/pip-build-X4mzal/theano/theano/compile/function_module.pyRJ   o  s    			"""%)	%	

"% 												(c   
      C` sb  | d  k r t j j } n  | d k r+ d  St j j g  | D] } | j ^ q; g  | D]! } t | d t	  rT | j
 ^ qT d g  | D] } | j ^ q } d } d } d }	 x | D] } | j | k r | j
 d  k r | d k rt j | | j |  | j | f d d	 qZ| d
 k rGt | | j |  | j |	 f   qZt d |   q q Wd  S(   NR   R?   t   blockerss   theano.function was asked to create a function computing outputs given certain inputs, but the provided input variable at index %i is not part of the computational graph needed to compute the outputs: %s.
%ss   To make this warning into an error, you can pass the parameter on_unused_input='raise' to theano.function. To disable it completely, use on_unused_input='ignore'.s   To make this error into a warning, you can pass the parameter on_unused_input='warn' to theano.function. To disable it completely, use on_unused_input='ignore'.Rm   Rq  i   t   raisess   Invalid value for keyword on_unused_input of theano.function: '%s'.
Valid values are 'raise', 'warn', and 'ignore'.(   R   R   R   R   R	   R   t	   ancestorsRT   R   R   R?   R  Rm   R   R   R<   (
   RI   R$   R   R   R9   R   t   used_inputsR   t   warn_msgt   err_msg(    (    s>   /tmp/pip-build-X4mzal/theano/theano/compile/function_module.pyRy    s*    	/c         C` s  | d k r% d g t |  j  } n  g  } g  } t |  j  t |  k sR t  xZt t |  j |   D]@\ } \ \ } } }	 }
 t |
 t j	  r |
 j
 }
 n  t |
 t j  r | d k	 r t d   n  | j |
 j  | | j d } n | j |
 g  |
 } |  j | } |  j | } | o.| s8t  | j rd| sNt  | s[t  d } n  | rsd } n  | d k	 r| s| st  n  | j | | | f  qk Wt j   } t j j j } t j j j } z@ t j j j t j j _ |  j j d | d |  \ } } } Wd | t j j _ Xt j   } | | } t j j j | 7_ t j  d |  |  j! r|  j! j" | 7_" |  j! j# | _$ t j j j | } |  j! j | 7_ n  |  j% | | | |  j |  j& | |  j' |  j( |  j) |  
 } |  j! | _! | S(   s  
        Create a function.

        Parameters
        ----------
        input_storage
            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.

        sC   Cannot take a Container instance as default for a SymbolicInputKit.i    R   R   Ns   Linker took %f seconds(*   R   R    R$   Rp   R   R   R>   Rx   R	   R4  R   Ry   RZ   RV   R   R   R   t   sharedR   R   t   cmodulet   import_timeR   R|  R}  R  Rz  t
   make_thunkR   R   t   total_time_linkerR  R  R   t   linker_timet   flag_time_thunkst   time_thunksR   R   R   R   R   (   RI   R   R#  R   t   input_storage_listsR   R   RS   Rp   t	   subinputst   input_storage_iR   R   R   t   start_linkert   start_import_timeR  t   _fnt   _it   _ot
   end_linkerR  R  R   (    (    s>   /tmp/pip-build-X4mzal/theano/theano/compile/function_module.pyR   ,  sd    !1			
		N(   R   R   R   t   staticmethodR6  R9  R:  Rp  R   R   Rl   RJ   Ry  R   (    (    (    s>   /tmp/pip-build-X4mzal/theano/theano/compile/function_module.pyR3    s   
				(c         C` s^   t  d |  j d |  j d |  j d |  j d |  j d |  j d |  j d |  j  } t	 | f f S(	   NR$   R   R6   R   Rb   R   R   R   (
   R   R$   Rg   R6   R   Rb   R   R   R   t   _constructor_FunctionMaker(   RI   R   (    (    s>   /tmp/pip-build-X4mzal/theano/theano/compile/function_module.pyt   _pickle_FunctionMaker  s    							c         C` s4   t  j j r, t  j j r" |  d =n  t |    Sd  Sd  S(   NR6   (   R   R   R$  t   reoptimize_unpickled_functionR3  R   (   R   (    (    s>   /tmp/pip-build-X4mzal/theano/theano/compile/function_module.pyR    s
    

c         C` sC   x6 t  D]. } y | |  |  SWq t k
 r4 q q Xq W|  | k S(   N(   t
   __checkersR   (   R  t   yt   checker(    (    s>   /tmp/pip-build-X4mzal/theano/theano/compile/function_module.pyt   check_equal  s    c         C` s   t  j d |   d  S(   Ni    (   R  t   insert(   R  (    (    s>   /tmp/pip-build-X4mzal/theano/theano/compile/function_module.pyt   register_checker  s    c         C` sn  t  j    } t j j j |  } t t t |    }  | d k	 r t	 | t t
 f  rr t t t j |   } q t j |  } n  g  |  D] }	 t |	 d d  ^ q }
 t	 | t t
 f  r t d   nE t | d t  } | |  | | d | d | d | d | j |
  } t  j    } | rR| j | | 7_ t | j j j  | _ n  | | _ | | j j _ | S(	   s  
    Return a Function that will calculate the outputs from the inputs.

    Parameters
    ----------
    inputs : list of `SymbolicInput` or `In` instances
    outputs : a SymbolicOutput or a list of `SymbolicOutput` or `Out` instances
        The return value of the returned function will match the format of this
        argument (either the value itself or a list of one or more return
        values).
    mode : descriptive string or Mode instance
        Default of None means to use `config.mode` (see below for descriptive
        string list).
    name : str
        An optional name for this function. If used, the profile mode will print the
        time spent in this function.
    accept_inplace : bool
        True iff the graph can contain inplace operations prior to the
        optimization phase (default is False).
    profile : None or ProfileStats instance
    on_unused_input : {'raise', 'warn', 'ignore', None}
        What to do if a variable in the 'inputs' list is not used in the graph.
    output_keys :
        If the outputs were provided to theano.function as a list, then
        output_keys is None. Otherwise, if outputs were provided as a dict,
        output_keys is the sorted list of keys from the outputs.

    Notes
    -----
    Currently, the library provides the following mode strings:

    - FAST_RUN (default) (optimize without too much time)

    - FAST_COMPILE (minimal optimization)

    - DebugMode: verify many internal conditions that are normally assumed
      (slow)

    Rr   s/   We do not support the passing of multiple modest   function_makerRb   R   R   R   N(   R   R   R   R   Ru  R;   R_   t   convert_function_inputR   Rx   R5  R3  R:  R   R   R   t   compile_timeR    R   R6   RY   t   nb_nodesR   (   R$   R   R   Rb   R   R   R   R   R\  RS   R   t   MakerR   R]  (    (    s>   /tmp/pip-build-X4mzal/theano/theano/compile/function_module.pyt   orig_function  s4    2%			c         C` s   t  |  t  r |  St  |  t j  r7 t d |    nt  |  t j  rS t |   St  |  t t f  r|  } |  s t d |    n  t  |  d t	  r |  d } |  d }  n d } t  |  d t t f  r t |  d  d k s t |   d k rt d |   n  |  \ \ } } } nt  |  d t j  rt |   d k rc|  d d d } } } q!t |   d k r|  d \ } } } q!t d |   n t  |  d t  rt |   d k r|  d St |   d k r!|  \ }  } | d k	 r| |  _ n  | |  _ |  Sn t d |    t  | t j  sOt d t |  |   n  | d k	 rt  | t j  rt d	 t |  |   n  | d k	 rt  | t j t f  rt d
 | | f   n  t | d | d | d | St d t |   |    d S(   s!  
    Upgrade a input shortcut to an In instance.

    The rules for upgrading are as follows:

    - a `Variable` instance r will be upgraded like `In`(r)

    - a tuple (name, r) will be `In`(r, name=name)

    - a tuple (r, val) will be `In`(r, value=value, autoname=True)

    - a tuple ((r,up), val) will be
      `In`(r, value=value, update=up, autoname=True)

    - a tuple (name, r, val) will be `In`(r, name=name, value=value)

    - a tuple (name, (r,up), val) will be
      `In`(r, name=name, value=val, update=up, autoname=True)

    s1   A Constant instance is not a legal function inputs#   Nonsensical input specification: %si    i   i   sD   Invalid input syntax: %s (check documentation or use an In instance)s(   The input specification is not valid: %ss2   Unknown input type: %s, expected Variable instances3   Unknown update type: %s, expected Variable instancesS   The value for input %s should not be a Variable or SymbolicInput instance (got: %s)R   Rr   R?   N(   Rx   R   R	   R  RZ   R4  R   R;   R5  R   R   R    R   Rr   R   (   RS   t   origR   RT   R?   Rr   (    (    s>   /tmp/pip-build-X4mzal/theano/theano/compile/function_module.pyR    sd    

(	c         C` s   t  |   } d   } | d k rX | d k r6 d } q | d k rK d } q d | } nh | d k r d | | |  d j |   f } n7 d	 | | |  d j |   | | |  | |  f } | S(
   sL   
    Return a human-readable description of named and un-named inputs.

    c         S` s   |  d k r d Sd Sd  S(   Ni   R{   t    (    (   t   n(    (    s>   /tmp/pip-build-X4mzal/theano/theano/compile/function_module.pyt
   get_pluralf  s    i    s*   The function is supposed to have no input.i   s   The function has a single input variable which has no name, and thus cannot be assigned through a keyword argument (use 'name=...' in a Variable's constructor to give it a name).s   The function has %s inputs, but none of them is named, and thus they cannot be assigned through keyword arguments (use 'name=...' in a Variable's constructor to give it a name).s'   The function has %s named input%s (%s).s   , s   The function has %s named input%s (%s), and %s unnamed input%s which thus cannot be accessed through keyword argument%s (use 'name=...' in a variable's constructor to give it a name).(   R    Rg  (   R   R   t   n_named_inputsR  R   (    (    s>   /tmp/pip-build-X4mzal/theano/theano/compile/function_module.pyR~   _  s&    			
	(N   R   t
   __future__R    R   R   R   t   sixR   R   R   t	   six.movesR   t   six.moves.copyregt   movest   copyregt   six.moves.cPicklet   cPickleRC  t	   itertoolsR   R   R  t   numpyR   R   R   R	   t   theano.compatR
   t
   theano.gofR   t   theano.compile.modet   theano.compile.profilingt   theano.compile.ioR   R   R   t   theano.compile.opsR   R   t   theano.gof.graphR   t   theano.gof.opR   t   loggingt	   getLoggerR  t   __docformat__R   R   R#   R-   R:   RF   RG   R   R]   t   toolboxt   PreserveVariableAttributesR^   Rj   Rk   R   Rl   R"  R  R&  R2  R3  R  R  R  R  R  R   R  R  R~   (    (    (    s>   /tmp/pip-build-X4mzal/theano/theano/compile/function_module.pyt   <module>   sp   					:	   	"		U	  						S	P