Exemplo n.º 1
0
def connect(sgv0, sgv1, disconnect_first=False):
    """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.
  """
    sgv0 = subgraph.make_view(sgv0)
    sgv1 = subgraph.make_view(sgv1)
    util.check_graphs(sgv0, sgv1)
    if disconnect_first:
        detach_outputs(sgv0)
    sgv0_outputs = subgraph.SubGraphView(passthrough_ts=sgv0.outputs)
    reroute.reroute_inputs(sgv0_outputs, sgv1)
    return sgv0, sgv1
Exemplo n.º 2
0
def _reroute_sgv_inputs(sgv0, sgv1, mode):
  """Re-route all the inputs of two subgraphs.

  Args:
    sgv0: the first subgraph to have its inputs swapped. This argument is
      converted to a subgraph using the same rules than the function
      subgraph.make_view.
    sgv1: the second subgraph to have its inputs swapped. This argument is
      converted to a subgraph using the same rules than the function
      subgraph.make_view.
    mode: reroute mode, see _reroute_ts(...).
  Returns:
    A tuple `(sgv0, sgv1)` of subgraph views with their inputs swapped.
      Note that the function argument sgv0 and sgv1 are also modified in place.
  Raises:
    StandardError: if sgv0 or sgv1 cannot be converted to a SubGraphView using
      the same rules than the function subgraph.make_view.
  """
  sgv0 = _subgraph.make_view(sgv0)
  sgv1 = _subgraph.make_view(sgv1)
  _util.check_graphs(sgv0, sgv1)
  can_modify = sgv0.ops + sgv1.ops
  # also allow consumers of passthrough to be modified:
  can_modify += _util.get_consuming_ops(sgv0.passthroughs)
  can_modify += _util.get_consuming_ops(sgv1.passthroughs)
  _reroute_ts(sgv0.inputs, sgv1.inputs, mode, can_modify=can_modify)
  _reroute_sgv_remap(sgv0, sgv1, mode)
  return sgv0, sgv1
Exemplo n.º 3
0
def _reroute_sgv_remap(sgv0, sgv1, mode):
    """Remap in place the inputs of two subgraph views to mimic the reroute.

  This function is meant to used by reroute_inputs only.

  Args:
    sgv0: the first subgraph to have its inputs remapped.
    sgv1: the second subgraph to have its inputs remapped.
    mode: reroute mode, see _reroute_ts(...).
  Raises:
    TypeError: if svg0 or svg1 are not SubGraphView.
    ValueError: if sgv0 and sgv1 do not belong to the same graph.
  """
    a2b, b2a = _RerouteMode.check(mode)
    if not isinstance(sgv0, _subgraph.SubGraphView):
        raise TypeError("Expected a SubGraphView, got {}".format(type(sgv0)))
    if not isinstance(sgv1, _subgraph.SubGraphView):
        raise TypeError("Expected a SubGraphView, got {}".format(type(sgv1)))
    _util.check_graphs(sgv0, sgv1)
    sgv0_ = sgv0.copy()
    sgv1_ = sgv1.copy()
    # pylint: disable=protected-access
    if a2b and b2a:
        (sgv0_._input_ts, sgv1_._input_ts) = (sgv1_._input_ts, sgv0_._input_ts)
        (sgv0_._passthrough_ts,
         sgv1_._passthrough_ts) = (sgv1_._passthrough_ts,
                                   sgv0_._passthrough_ts)
    elif a2b:
        sgv1_._input_ts = sgv0_._input_ts[:]
        sgv1_._passthrough_ts = sgv0_._passthrough_ts[:]
    elif b2a:
        sgv0_._input_ts = sgv1_._input_ts[:]
        sgv0_._passthrough_ts = sgv1_._passthrough_ts[:]
    # pylint: enable=protected-access

    # Update the passthrough outputs as well.
    def update_passthrough_outputs(a, b):
        # pylint: disable=protected-access
        for i, t in enumerate(b._output_ts):
            if t in a._passthrough_ts:
                ii = a._input_ts.index(t)
                b._output_ts[i] = b._input_ts[ii]
        # pylint: enable=protected-access

    if a2b:
        update_passthrough_outputs(sgv0_, sgv1_)
    if b2a:
        update_passthrough_outputs(sgv1_, sgv0_)

    # in-place
    # pylint: disable=protected-access
    sgv0._assign_from(sgv0_)
    sgv1._assign_from(sgv1_)
Exemplo n.º 4
0
def _reroute_sgv_outputs(sgv0, sgv1, mode):
  """Re-route all the outputs of two operations.

  Args:
    sgv0: the first subgraph to have its outputs swapped. This argument is
      converted to a subgraph using the same rules than the function
      subgraph.make_view.
    sgv1: the second subgraph to have its outputs swapped. This argument is
      converted to a subgraph using the same rules than the function
      subgraph.make_view.
    mode: reroute mode, see _reroute_ts(...).
  Returns:
    A tuple `(sgv0, sgv1)` of subgraph views with their outputs swapped.
      Note that the function argument sgv0 and sgv1 are also modified in place.
  Raises:
    StandardError: if sgv0 or sgv1 cannot be converted to a SubGraphView using
      the same rules than the function subgraph.make_view.
  """
  sgv0 = _subgraph.make_view(sgv0)
  sgv1 = _subgraph.make_view(sgv1)
  _util.check_graphs(sgv0, sgv1)
  cannot_modify = sgv0.ops + sgv1.ops
  _reroute_ts(sgv0.outputs, sgv1.outputs, mode, cannot_modify=cannot_modify)
  return sgv0, sgv1