ó
àÆ÷Xc           @` sw  d  Z  d d l m Z m Z m Z d d l Z d d l Z d d l m Z d d l Z d d l	 m
 Z
 d d l m Z d d l m Z e j d ƒ Z i e j ƒ  d	 6e j ƒ  d
 6e j ƒ  d 6e j d e ƒ d 6e j j d e ƒ d 6e j j d e ƒ d 6e j j d e d e ƒ d 6e j j d e d e ƒ d 6Z d „  Z g  Z e j
 j s`d g Z n  e j d g  d e ƒ Z e j d d g d e ƒ Z e j d d g d e ƒ Z e j  d ƒ Z! e j d d d g d e ƒ Z" e j d d g d e ƒ Z# d e# _$ d e _% d e _% d e _% d  e! _% d! e" _% d" e# _% i e d 6e d# 6e d 6e d 6e! d$ 6e" d 6e# d% 6Z' d& „  Z( d' e j) f d( „  ƒ  YZ* d) e j) f d* „  ƒ  YZ+ d+ e j) f d, „  ƒ  YZ, e j- ƒ  Z. e. j/ d- e j0 ƒ  d d d d ƒ e j. j1 d. e d/ e ƒ Z2 e. j/ d0 e j. j3 e2 d1 e j4 j5 j6 ƒd2 d d ƒ e. j/ d3 e j0 ƒ  d4 d d d ƒ e. j/ d5 e j7 d6 e ƒ d7 d d d8 ƒ e. d5 j/ d e j4 j0 ƒ  d d d9 e ƒe. j/ d: e j0 ƒ  d; d d d ƒ e. j/ d< e, d= ƒ d> ƒ e. j/ d% e j7 ƒ  d? d ƒ e. j/ d@ e, dA ƒ dB ƒ e. j/ dC e j7 ƒ  dD d d ƒ e. j/ dE e j7 ƒ  dF d ƒ e. j/ dG e j7 ƒ  dH d d ƒ e. j/ dI e j0 ƒ  dJ d d ƒ e. j/ dK e* ƒ  dL d dM ƒ e. j/ dN e j0 ƒ  dO d d ƒ dP e8 f dQ „  ƒ  YZ9 e9 e j j j d e dR e ƒ d ƒ Z: e j
 j r/e9 d d ƒ Z; n e9 d d ƒ Z; i e: dS 6e; dT 6Z< d a= dU „  Z> dV „  Z? dW „  Z@ d S(X   s
   
WRITEME

i    (   t   absolute_importt   print_functiont   divisionN(   t   gof(   t   config(   t   _output_guard(   t   string_typess   theano.compile.modet   pyt   cs   c|pyt   allow_gcs	   c|py_nogct	   use_cloopt   vmt   cvmt   vm_nogct   cvm_nogcc         C` s-   |  t  k r t d |  ƒ ‚ n  | t  |  <d S(   s<   Add a `Linker` which can be referred to by `name` in `Mode`.s   Linker name already taken: %sN(   t   predefined_linkerst
   ValueError(   t   namet   linker(    (    s3   /tmp/pip-build-X4mzal/theano/theano/compile/mode.pyt   register_linker!   s    t   cxx_onlyt   includet   excludet   merget   fast_runt   stablet   fast_compilet   fast_compile_gpug›ò×  ø?t   OPT_NONEt	   OPT_MERGEt   OPT_FAST_RUNt   OPT_FAST_RUN_STABLEt   OPT_FAST_COMPILEt   OPT_STABILIZEt   Nonet   fast_run_stablet	   stabilizec         C` s-   |  t  k r t d |  ƒ ‚ n  | t  |  <d S(   s?   Add a `Optimizer` which can be referred to by `name` in `Mode`.s    Optimizer name already taken: %sN(   t   predefined_optimizersR   (   R   t   opt(    (    s3   /tmp/pip-build-X4mzal/theano/theano/compile/mode.pyt   register_optimizerN   s    t   AddDestroyHandlerc           B` s    e  Z d  Z d „  Z d „  Z RS(   s‘  
    This optimizer performs two important functions:

    1) It has a 'requirement' of the destroyhandler. This means that the fgraph
    will include it as a feature for this optimization, and keep this feature
    enabled for subsequent optimizations. All optimizations that work inplace
    on any of their inputs must run *after* this optimization to ensure that
    the DestroyHandler has been included in the fgraph.

    2) It tries to replace each output with an Op that purports to destroy it
    (but it won't I promise). If this replacement succeeds it means that
    there is a bug in theano. It should not be possible to destroy outputs.

    c         C` s`   xY | j  D]N } y1 | j | t | ƒ d d ƒt j d | ƒ Wq
 t j k
 rW q
 Xq
 Wd  S(   Nt   reasont   output_guardsn   Output variable %s required output_guard, how was this output left unprotected against destructive operations?(   t   outputst   replace_validateR   t   _loggert   infoR   t   InconsistencyError(   t   selft   fgrapht   o(    (    s3   /tmp/pip-build-X4mzal/theano/theano/compile/mode.pyt   applyd   s    	c         C` s-   t  t |  ƒ j | ƒ | j t j ƒ  ƒ d  S(   N(   t   superR(   t   add_requirementst   attach_featureR   t   DestroyHandler(   R0   R1   (    (    s3   /tmp/pip-build-X4mzal/theano/theano/compile/mode.pyR5   r   s    (   t   __name__t
   __module__t   __doc__R3   R5   (    (    (    s3   /tmp/pip-build-X4mzal/theano/theano/compile/mode.pyR(   U   s   	t   AddFeatureOptimizerc           B` s    e  Z d  Z d „  Z d „  Z RS(   sG   
    This optimizer adds a provided feature to the function graph.
    c         C` s   | |  _  d  S(   N(   t   feature(   R0   R<   (    (    s3   /tmp/pip-build-X4mzal/theano/theano/compile/mode.pyt   __init__|   s    c         C` s*   t  t |  ƒ j | ƒ | j |  j ƒ d  S(   N(   R4   R;   R5   R6   R<   (   R0   R1   (    (    s3   /tmp/pip-build-X4mzal/theano/theano/compile/mode.pyR5      s    (   R8   R9   R:   R=   R5   (    (    (    s3   /tmp/pip-build-X4mzal/theano/theano/compile/mode.pyR;   w   s   	t   PrintCurrentFunctionGraphc           B` s    e  Z d  Z d „  Z d „  Z RS(   sŠ   
    This optimizer is for debugging.

    Toss it into the optimization pipeline to see the state of things at any
    given point.

    c         C` s   | |  _  d  S(   N(   t   header(   R0   R?   (    (    s3   /tmp/pip-build-X4mzal/theano/theano/compile/mode.pyR=   Œ   s    c         C` s3   d d  l  } t d |  j ƒ | j j | j ƒ d  S(   Ni    s   PrintCurrentFunctionGraph:(   t   theano.printingt   printR?   t   printingt
   debugprintR+   (   R0   R1   t   theano(    (    s3   /tmp/pip-build-X4mzal/theano/theano/compile/mode.pyR3      s    (   R8   R9   R:   R=   R3   (    (    (    s3   /tmp/pip-build-X4mzal/theano/theano/compile/mode.pyR>   „   s   	t   merge1t   apply_all_optst   profilet   uselesst   failure_callbackg333333ã?s   merge1.1gÍÌÌÌÌÌä?t   canonicalizet   ignore_newtreesi   t   canonicalize_dbt   cleanups   merge1.2g333333ó?s	   Print1.21s   Post-canonicalizeg\Âõ(\ó?g      ø?s	   Print1.51s   Post-stabilizeg)\Âõ(ø?t
   specializei   t   uncanonicalizei   t   specialize_devicegÍÌÌÌÌLH@t   merge2i1   t   add_destroy_handlerg     ÀH@t   inplacet   merge3id   t   Modec           B` s‰   e  Z d  Z d d d „ Z d „  Z d „  Z d „  Z d „  Z e	 e ƒ Z
 d „  Z d „  Z d	 „  Z d
 „  Z d „  Z d d d „ Z RS(   sQ  
    The Mode represents a way to optimize and then link a computation graph.

    Parameters
    ----------
    optimizer : a structure of type Optimizer
        An Optimizer may simplify the math, put similar computations together,
        improve numerical stability and various other improvements.
    linker : a structure of type Linker
        A Linker decides which implementations to use (C or Python, for example)
        and how to string them together to perform the computation.

    See Also
    --------
    predefined_linkers
    predefined_optimizers
    predefined_modes

    t   defaultc         C` sJ   | d  k r t j } n  | d k r0 t j } n  t j |  | | f ƒ d  S(   NRV   (   R"   R   R   t	   optimizerRU   t   __setstate__(   R0   R   RW   (    (    s3   /tmp/pip-build-X4mzal/theano/theano/compile/mode.pyR=   ï   s
    c         C` s   |  j  |  j f S(   N(   t   provided_linkert   provided_optimizer(   R0   (    (    s3   /tmp/pip-build-X4mzal/theano/theano/compile/mode.pyt   __getstate__ÿ   s    c         C` s½   | \ } } | |  _  | |  _ t | t ƒ s9 | d  k rF t | } n  | |  _ t | t ƒ sj | d  k rw t | } n  t | t j	 ƒ r• | |  _ n  | |  _
 d |  _ d |  _ |  | _ d  S(   Ni    (   RY   RZ   t
   isinstanceR   R"   R   R   R%   R   t   Queryt
   _optimizert	   call_timet   fn_timet   mode(   R0   t   stateR   RW   (    (    s3   /tmp/pip-build-X4mzal/theano/theano/compile/mode.pyRX     s    						c         C` s   d |  j  j |  j |  j f S(   Ns   %s(linker = %s, optimizer = %s)(   t	   __class__R8   RY   RZ   (   R0   (    (    s3   /tmp/pip-build-X4mzal/theano/theano/compile/mode.pyt   __str__  s    c         C` s0   t  |  j t j ƒ r% t j |  j ƒ S|  j Sd  S(   N(   R\   R^   R   R]   t   optdbt   query(   R0   (    (    s3   /tmp/pip-build-X4mzal/theano/theano/compile/mode.pyt   __get_optimizer  s    c         C` sZ   t  | t ƒ s | d  k r( t | } n  t  | t ƒ sC | d  k rP t | } n  | | f S(   N(   R\   R   R"   R   R%   (   R0   R   RW   (    (    s3   /tmp/pip-build-X4mzal/theano/theano/compile/mode.pyt   get_linker_optimizer  s
    c         G` s7   |  j  |  j |  j ƒ \ } } |  j d | j | Œ  ƒ S(   NRW   (   Rh   RY   RZ   t   clonet	   including(   R0   t   tagst   linkR&   (    (    s3   /tmp/pip-build-X4mzal/theano/theano/compile/mode.pyRj   &  s    c         G` s7   |  j  |  j |  j ƒ \ } } |  j d | j | Œ  ƒ S(   sš  Adds new optimization instances to a mode.

        This method adds new optimization instances to a compilation mode. It
        works like the `including()` method but takes as inputs optimization
        instances to add instead of tags.

        Parameters
        ----------
        optimizations :
            Every element of `optimizations` is a tuple containing an
            optimization instance and a floating point value indicating the
            position at which to insert the optimization in the mode.

        Returns
        -------
        Mode
            Copy of the current Mode which includes the provided
            optimizations.
        RW   (   Rh   RY   RZ   Ri   t   register(   R0   t   optimizationsRl   R&   (    (    s3   /tmp/pip-build-X4mzal/theano/theano/compile/mode.pyRm   -  s    c         G` s7   |  j  |  j |  j ƒ \ } } |  j d | j | Œ  ƒ S(   NRW   (   Rh   RY   RZ   Ri   t	   excluding(   R0   Rk   Rl   R&   (    (    s3   /tmp/pip-build-X4mzal/theano/theano/compile/mode.pyRo   F  s    c         G` s7   |  j  |  j |  j ƒ \ } } |  j d | j | Œ  ƒ S(   NRW   (   Rh   RY   RZ   Ri   t	   requiring(   R0   Rk   Rl   R&   (    (    s3   /tmp/pip-build-X4mzal/theano/theano/compile/mode.pyRp   K  s    t    c         K` s^   | d k r i  } n  |  j j |   } | d k r? |  j } n  t |  ƒ d | d | ƒ } | S(   sÇ   
        Create a new instance of this Mode.

        Keyword arguments can be provided for the linker,
        in which case its `clone` method will be called with these
        arguments.

        Rq   R   RW   N(   R"   R   Ri   RZ   t   type(   R0   t   link_kwargsRW   t   kwargst
   new_linkert   new_mode(    (    s3   /tmp/pip-build-X4mzal/theano/theano/compile/mode.pyRi   P  s    			N(   R8   R9   R:   R"   R=   R[   RX   Rd   t   _Mode__get_optimizert   propertyRW   Rh   Rj   Rm   Ro   Rp   Ri   (    (    (    s3   /tmp/pip-build-X4mzal/theano/theano/compile/mode.pyRU   Ú   s   									t   c_thunkst   FAST_COMPILEt   FAST_RUNc         C` sÔ  |  d  k r t j } n |  } t | t ƒ s1 | S|  d  k r t r | t k rb t | j j } n | } t j j | k r t Sn  | d k r| d k r¾ d d l	 m
 } | d t j ƒ } q4| d k rø d d l m } | t t t d t j ƒ} q4t | d ƒ } n) | t k r$t | } n t d	 | ƒ ‚ |  d  k rÐt j j rm| j t j j j d
 ƒ Œ  } n  t j j rš| j t j j j d
 ƒ Œ  } n  t j j rÇ| j t j j j d
 ƒ Œ  } n  | a n  | S(   NRU   t	   DebugModet   NanGuardModei   (   R|   RW   (   R}   s2   (linker=config.linker, optimizer=config.optimizer)s'   No predefined mode exist for string: %st   :(   s   Modes	   DebugModes   NanGuardMode(   R"   R   Ra   R\   R   t   instantiated_default_modet   predefined_modesRc   R8   t	   debugmodeR|   RW   t   nanguardmodeR}   t   Truet   evalt	   ExceptionRD   t   optimizer_excludingRo   t   splitt   optimizer_includingRj   t   optimizer_requiringRp   (   t   orig_stringt   stringt   default_mode_classR|   t   retR}   (    (    s3   /tmp/pip-build-X4mzal/theano/theano/compile/mode.pyt   get_modev  sB    		!!!	c           C` s
   t  d  ƒ S(   N(   RŽ   R"   (    (    (    s3   /tmp/pip-build-X4mzal/theano/theano/compile/mode.pyt   get_default_mode«  s    c         C` s-   |  t  k r t d |  ƒ ‚ n  | t  |  <d S(   sI   
    Add a `Mode` which can be referred to by `name` in `function`.

    s   Mode name already taken: %sN(   R€   R   (   R   Ra   (    (    s3   /tmp/pip-build-X4mzal/theano/theano/compile/mode.pyt   register_mode¯  s    (A   R:   t
   __future__R    R   R   t   loggingRD   R   t   theano.gof.vmt   theano.configparserR   t   theano.compile.opsR   t   sixR   t	   getLoggerR-   t   PerformLinkert   CLinkert   OpWiseCLinkert   FalseR   t	   VM_LinkerRƒ   R   R   R   t   cxxR]   R   R   R   Rp   R   R    R!   t   position_cutoffR   R"   R%   R'   t	   OptimizerR(   R;   R>   t
   SequenceDBRe   Rm   t   MergeOptimizert   LocalGroupDBt   local_uselesst   TopoDBR&   t   NavigatorOptimizert   warn_inplacet   EquilibriumDBt   objectRU   Rz   R{   R€   R   RŽ   R   R   (    (    (    s3   /tmp/pip-build-X4mzal/theano/theano/compile/mode.pyt   <module>   s¾   	
								
	"

Ž	

	5	