ó
V¾÷Xc           @` s¡   d  Z  d d l m Z d d l m Z d d l m Z d d l m Z d d l m Z d d l	 m
 Z d d	 g Z d
 „  Z d d „ Z d	 e f d „  ƒ  YZ d S(   s    Simple graph matching functions.i    (   t   absolute_import(   t   division(   t   print_function(   t   string_types(   t   select(   t   opst   op_typet	   OpMatcherc         C` s2   |  d k r d St |  t ƒ s. t |  ƒ }  n  |  S(   s    Convert to a OpMatcher instance.N(   t   Nonet
   isinstanceR   (   t   graph_match(    (    sg   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/graph_editor/match.pyt   _make_graph_match    s
    c         ` sE   t  ˆ  t ƒ r ˆ  ‰  n  | d k r1 ‡  f d †  S| j j ˆ  k Sd S(   s_  Check if an op is of the given type.

  Args:
    op_types: tuple of strings containing the types to check against.
      For instance: ("Add", "Const")
    op: the operation to check (or None).
  Returns:
    if op is not None, return True if the op is of the correct type.
    if op is None, return a lambda function which does the type checking.
  c         ` s   |  j  j ˆ  k S(   N(   t   node_deft   op(   R   (   t   op_types(    sg   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/graph_editor/match.pyt   <lambda>7   s    N(   R	   R   R   R   R   (   R   R   (    (   R   sg   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/graph_editor/match.pyR   )   s
    	c           B` sD   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   s   Graph match class.c         C` sG   g  |  _  d |  _ d |  _ d |  _ |  j | ƒ } |  j  j | ƒ d S(   s   Graph match constructor.N(   t   positive_filtersR   t   input_op_matchest   control_input_op_matchest   output_op_matchest   _finalize_positive_filtert   append(   t   selft   positive_filter(    (    sg   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/graph_editor/match.pyt   __init__?   s    				c         C` s€   t  j | ƒ r( t  j | ƒ } | d „ St | t j ƒ rD | d „ St | ƒ rT | S| t k rg d „  St d j	 | ƒ ƒ ‚ d S(   s   Convert to a filter function.c         S` s   | j  |  j ƒ d  k	 S(   N(   t   searcht   nameR   (   R   t   regex(    (    sg   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/graph_editor/match.pyR   L   s    c         S` s
   |  | k S(   N(    (   R   t   match_op(    (    sg   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/graph_editor/match.pyR   N   s    c         S` s   t  S(   N(   t   True(   R   (    (    sg   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/graph_editor/match.pyR   R   s    s'   Cannot finalize the positive filter: {}N(
   R   t   can_be_regext
   make_regexR	   t   tf_opst	   Operationt   callableR   t
   ValueErrort   format(   R   t   elemt   regex_(    (    sg   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/graph_editor/match.pyR   H   s    

c         C` s)  t  | t j ƒ s0 t d j t | ƒ ƒ ƒ ‚ n  x! |  j D] } | | ƒ s: t Sq: W|  j d k	 rÓ t
 | j ƒ t
 |  j ƒ k r… t SxK t | j |  j ƒ D]1 \ } } | d k r¹ q› n  | | j ƒ s› t Sq› Wn  |  j d k	 rOt
 | j ƒ t
 |  j ƒ k rt SxH t | j |  j ƒ D]. \ } } | d k r8qn  | | ƒ st SqWn  |  j d k	 r%t
 | j ƒ t
 |  j ƒ k r€t Sx¢ t | j |  j ƒ D]ˆ \ } } | d k r´q–n  t
 | j ƒ  ƒ t
 | ƒ k rÖt SxE t | j ƒ  | ƒ D]. \ }	 }
 |
 d k r
qìn  |
 |	 ƒ sìt SqìWq–Wn  t S(   s"   Evaluate if the op matches or not.s   Expect tf.Operation, got: {}N(   R	   R    R!   t	   TypeErrorR$   t   typeR   t   FalseR   R   t   lent   inputst   zipR   R   t   control_inputsR   t   outputst	   consumersR   (   R   R   R   t   input_tt   input_op_matcht	   cinput_opt   cinput_op_matcht   output_tR   t   consumer_opt   consumer_op_match(    (    sg   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/graph_editor/match.pyt   __call__V   sL    "c         G` sR   |  j  d k	 r t d ƒ ‚ n  g  |  _  x$ | D] } |  j  j t | ƒ ƒ q. W|  S(   s   Add input matches.s    input_op_matches is already set.N(   R   R   R#   R   R   (   R   t   argst   input_match(    (    sg   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/graph_editor/match.pyt	   input_ops   s    	c         G` sR   |  j  d k	 r t d ƒ ‚ n  g  |  _  x$ | D] } |  j  j t | ƒ ƒ q. W|  S(   s   Add input matches.s(   control_input_op_matches is already set.N(   R   R   R#   R   R   (   R   R8   R9   (    (    sg   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/graph_editor/match.pyt   control_input_opsˆ   s    	c         G` s¥   |  j  d k	 r t d ƒ ‚ n  g  |  _  xw | D]o } | d k rS |  j  j d ƒ n  t | t ƒ sn | g } n  g  | D] } t | ƒ ^ qu } |  j  j | ƒ q. W|  S(   s   Add output matches.s!   output_op_matches is already set.N(   R   R   R#   R   R	   t   listR   (   R   R8   t   consumer_op_matchesR6   (    (    sg   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/graph_editor/match.pyt
   output_ops‘   s    	(	   t   __name__t
   __module__t   __doc__R   R   R7   R:   R;   R>   (    (    (    sg   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/graph_editor/match.pyR   <   s   				)				N(   RA   t
   __future__R    R   R   t   sixR   t   tensorflow.contrib.graph_editorR   t   tensorflow.python.frameworkR   R    t   __all__R   R   R   t   objectR   (    (    (    sg   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/graph_editor/match.pyt   <module>   s   			