ó
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 l m	 Z	 d d	 l
 m Z d
 d d d d d d g Z d „  Z d „  Z e d „ Z d d „ Z e d d d „ Z e d „ Z d „  Z d S(   s#   Various function for graph editing.i    (   t   absolute_import(   t   division(   t   print_function(   t   reroute(   t   select(   t   subgraph(   t   util(   t	   array_opst   detach_control_inputst   detach_control_outputst   detach_inputst   detach_outputst   detacht   connectt   bypassc         C` sb   t  j |  ƒ }  xL |  j D]A } g  | j D] } | |  j k r) | ^ q) } t j | | ƒ q Wd S(   sÚ   Detach all the external control inputs of the subgraph sgv.

  Args:
    sgv: the subgraph view to be detached. This argument is converted to a
      subgraph using the same rules as the function subgraph.make_view.
  N(   R   t	   make_viewt   opst   control_inputsR   t   remove_control_inputs(   t   sgvt   opt   copt   cops(    (    sf   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/graph_editor/edit.pyR   &   s    +c         C` s—   t  | t j ƒ s* t d t | ƒ ƒ ‚ n  | j ƒ  t j |  ƒ }  xM |  j D]B } x9 | j	 | ƒ D]( } | |  j k rc t
 j | | ƒ qc qc WqM Wd S(   s  Detach all the external control outputs of the subgraph sgv.

  Args:
    sgv: the subgraph view to be detached. This argument is converted to a
      subgraph using the same rules as the function subgraph.make_view.
    control_outputs: a util.ControlOutputs instance.
  s'   Expected a util.ControlOutputs, got: {}N(   t
   isinstanceR   t   ControlOutputst	   TypeErrort   typet   updateR   R   R   t   getR   R   (   R   t   control_outputsR   R   (    (    sf   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/graph_editor/edit.pyR	   3   s    
c         C` sŒ   t  j |  ƒ }  |  j j ƒ  ? g  |  j D]* } t j d | j d t j	 | ƒ ƒ ^ q) } Wd QXt
 j |  | ƒ | r‚ t |  ƒ n  |  | f S(   su  Detach the inputs of a subgraph view.

  Args:
    sgv: the subgraph view to be detached. This argument is converted to a
      subgraph using the same rules as the function subgraph.make_view.
      Note that sgv is modified in place.
    control_inputs: if True control_inputs are also detached.
  Returns:
    A tuple `(sgv, input_placeholders)` where
      `sgv` is a new subgraph view of the detached subgraph;
      `input_placeholders` is a list of the created input placeholders.
  Raises:
    StandardError: if sgv cannot be converted to a SubGraphView using
      the same rules than the function subgraph.make_view.
  t   dtypet   nameN(   R   R   t   grapht
   as_defaultt   inputst   tf_array_opst   placeholderR   R   t   placeholder_nameR   t   swap_inputsR   (   R   R   t   input_tt   input_placeholders(    (    sf   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/graph_editor/edit.pyR
   F   s    =c   	      C` s  t  j |  ƒ }  |  j g  t |  j ƒ D] \ } } | j ƒ  r% | ^ q% ƒ } t  j | j ƒ  ƒ } | j g  t | j ƒ D]! \ } } | | j k rw | ^ qw ƒ } | j	 j
 ƒ  * g  | j D] } t j | ƒ ^ q» } Wd QXt j | | ƒ | d k	 rt | | ƒ n  | | f S(   s°  Detach the output of a subgraph view.

  Args:
    sgv: the subgraph view to be detached. This argument is converted to a
      subgraph using the same rules as the function subgraph.make_view.
      Note that sgv is modified in place.
    control_outputs: a util.ControlOutputs instance or None. If not None the
      control outputs are also detached.
  Returns:
    A tuple `(sgv, output_placeholders)` where
      `sgv` is a new subgraph view of the detached subgraph;
      `output_placeholders` is a list of the created output placeholders.
  Raises:
    StandardError: if sgv cannot be converted to a SubGraphView using
      the same rules than the function subgraph.make_view.
  N(   R   R   t   remap_outputst	   enumeratet   outputst	   consumerst   SubGraphViewt   remap_inputsR"   R    R!   R   t   make_placeholder_from_tensorR   t   swap_outputst   NoneR	   (	   R   R   t	   output_idt   output_tt   sgv_t   consumers_sgvt   input_idR'   t   output_placeholders(    (    sf   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/graph_editor/edit.pyR   e   s    	(c         C` sR   t  j | | | ƒ \ } } t |  | ƒ \ } } t |  | ƒ \ } } |  | | f S(   sP  Detach both the inputs and the outputs of a subgraph view.

  Args:
    sgv: the subgraph view to be detached. This argument is converted to a
      subgraph using the same rules as the function subgraph.make_view.
      Note that sgv is modified in place.
    control_inputs: A boolean indicating whether control inputs are enabled.
    control_outputs: An instance of util.ControlOutputs or None. If not None,
      control outputs are enabled.
    control_ios:  An instance of util.ControlOutputs or None. If not None, both
      control inputs and control outputs are enabled. This is equivalent to set
      control_inputs to True and control_outputs to the util.ControlOutputs
      instance.
  Returns:
    A tuple `(sgv, detached_inputs, detached_outputs)` where:
    `sgv` is a new subgraph view of the detached subgraph;
    `detach_inputs` is a list of the created input placeholders;
    `detach_outputs` is a list of the created output placeholders.
  Raises:
    StandardError: if sgv cannot be converted to a SubGraphView using
      the same rules than the function subgraph.make_view.
  (   R   t
   check_ciosR
   R   (   R   R   R   t   control_iost   _t   detached_inputst   detached_outputs(    (    sf   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/graph_editor/edit.pyR      s    	c         C` sp   t  j |  ƒ }  t  j | ƒ } t j |  | ƒ | rA t |  ƒ n  t  j d |  j ƒ } t j | | ƒ |  | f S(   s  Connect the outputs of sgv0 to the inputs of sgv1.

  Args:
    sgv0: the first subgraph to have its outputs swapped. This argument is
      converted to a subgraph using the same rules as the function
      subgraph.make_view.
      Note that sgv0 is modified in place.
    sgv1: the second subgraph to have its outputs swapped. This argument is
      converted to a subgraph using the same rules as the function
      subgraph.make_view.
      Note that sgv1 is modified in place.
    disconnect_first: if True the current outputs of sgv0 are disconnected.
  Returns:
    A tuple `(sgv0, sgv1)` of the now connected subgraphs.
  Raises:
    StandardError: if sgv0 or sgv1 cannot be converted to a SubGraphView using
      the same rules than the function subgraph.make_view.
  t   passthrough_ts(	   R   R   R   t   check_graphsR   R-   R+   R   t   reroute_inputs(   t   sgv0t   sgv1t   disconnect_firstt   sgv0_outputs(    (    sf   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/graph_editor/edit.pyR   ¬   s    c         C` sM   t  j |  ƒ }  t |  j ƒ } t |  ƒ \ }  } t j | |  j ƒ |  | f S(   sQ  Bypass the given subgraph by connecting its inputs to its outputs.

  Args:
    sgv: the subgraph view to be bypassed. This argument is converted to a
      subgraph using the same rules than the function subgraph.make_view.
      Note that sgv is modified in place.
  Returns:
    A tuple `(sgv, detached_inputs)` where:
      `sgv` is a new subgraph view of the bypassed subgraph;
      `detached_inputs` is a list of the created input placeholders.
  Raises:
    StandardError: if sgv cannot be converted to a SubGraphView using
      the same rules than the function subgraph.make_view.
  (   R   R   t   listR"   R
   R   t
   reroute_tsR+   (   R   t
   sgv_inputsR;   (    (    sf   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/graph_editor/edit.pyR   É   s
    N(   t   __doc__t
   __future__R    R   R   t   tensorflow.contrib.graph_editorR   R   R   R   t   tensorflow.python.opsR   R#   t   __all__R   R	   t   FalseR
   R1   R   R   R   R   (    (    (    sf   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/contrib/graph_editor/edit.pyt   <module>   s,   			(