Exemple #1
0
  def __call__(self, sgv, dst_graph, dst_scope, src_scope=""):
    """Execute the transformation.

    Args:
      sgv: the source subgraph-view.
      dst_graph: the destination graph.
      dst_scope: the destination scope.
      src_scope: the source scope, which specify the path from which the
        relative path of the transformed nodes are computed. For instance, if
        src_scope is a/ and dst_scoped is b/, then the node a/x/y will have a
        relative path of x/y and will be transformed into b/x/y.
    Returns:
      The transformed subgraph view.
    Raises:
      ValueError: if the argumens are invalid. For instance, if the source and
        destination are the same.
    """
    src_scope = util.scope_finalize(src_scope)
    dst_scope = util.scope_finalize(dst_scope)

    sgv = subgraph.make_view(sgv)
    if sgv.graph is dst_graph and not dst_scope:
      logging.warning("The source and the destination are the same! "
                      "Beware: in-place transormation are currently "
                      "experimental.")
    self._info = Transformer._Info(self, sgv, dst_graph, dst_scope, src_scope)

    # This is a recursive call. In practice, the graph is transformed bottom-up.
    for output_t in self._info.sgv.outputs:
      self._transform_t(output_t)

    sgv_ = self._transform_sgv(sgv)

    self._info = None
    return sgv_
Exemple #2
0
  def __call__(self,
               sgv,
               dst_graph,
               dst_scope,
               src_scope="",
               reuse_dst_scope=False):
    """Execute the transformation.

    Args:
      sgv: the source subgraph-view.
      dst_graph: the destination graph.
      dst_scope: the destination scope.
      src_scope: the source scope, which specify the path from which the
        relative path of the transformed nodes are computed. For instance, if
        src_scope is a/ and dst_scoped is b/, then the node a/x/y will have a
        relative path of x/y and will be transformed into b/x/y.
      reuse_dst_scope: if True the dst_scope is re-used if it already exists.
        Otherwise, the scope is given a unique name based on the one given
        by appending an underscore followed by a digit (default).
    Returns:
      A tuple `(sgv, info)` where:
        `sgv` is the transformed subgraph view;
        `info` is an instance of Transformer.ResultInfo containing
        information about the transform, including mapping between
        original and transformed tensors and operations.
    Raises:
      ValueError: if the arguments are invalid.
    """
    sgv = subgraph.make_view(sgv)
    if not isinstance(dst_graph, tf_ops.Graph):
      raise TypeError("Expected a tf.Graph, got: {}".format(type(dst_graph)))

    src_scope = util.scope_finalize(src_scope)
    dst_scope = util.scope_finalize(dst_scope)

    # Potentially create new scope if reuse_dst_scope is False
    if dst_scope and not reuse_dst_scope:
      dst_scope = util.scope_finalize(dst_graph.unique_name(dst_scope[:-1]))

    # Create temporary info used during this transform call
    self._info = Transformer._Info(self, sgv, dst_graph, dst_scope, src_scope)

    # Transform the graph starting from the output tensors.
    for output_t in self._info.sgv.outputs:
      self._transform_t(output_t)

    # Some ops might have been missed by the previous walk, namely, the roots
    # without any outputs. So the walk is now finalized from those roots.
    remaining_ops = [op for op in self._info.sgv.ops
                     if op not in self._info.transformed_ops]
    remaining_roots = [op for op in remaining_ops if not op.outputs]
    for op in remaining_roots:
      self._transform_op(op)

    sgv_ = self._transform_sgv(sgv)

    res_info = Transformer.ResultInfo(self._info)
    self._info = None
    return sgv_, res_info
Exemple #3
0
    def __call__(self,
                 sgv,
                 dst_graph,
                 dst_scope,
                 src_scope="",
                 reuse_dst_scope=False):
        """Execute the transformation.

    Args:
      sgv: the source subgraph-view.
      dst_graph: the destination graph.
      dst_scope: the destination scope.
      src_scope: the source scope, which specify the path from which the
        relative path of the transformed nodes are computed. For instance, if
        src_scope is a/ and dst_scoped is b/, then the node a/x/y will have a
        relative path of x/y and will be transformed into b/x/y.
      reuse_dst_scope: if True the dst_scope is re-used if it already exists.
        Otherwise, the scope is given a unique name based on the one given
        by postfixing an underscore followed by a digit (default).
    Returns:
      A tuple `(sgv, ops_mapping)` where:
        `sgv` is the transformed subgraph view;
        `ops_mapping` is a dictionary mapping the name of the original ops
        to the name of the transformed ops.
    Raises:
      ValueError: if the argumens are invalid.
    """
        sgv = subgraph.make_view(sgv)
        if not isinstance(dst_graph, tf_ops.Graph):
            raise TypeError("Expected a tf.Graph, got: {}".format(
                type(dst_graph)))

        src_scope = util.scope_finalize(src_scope)
        dst_scope = util.scope_finalize(dst_scope)

        # Potentially create new scope if reuse_dst_scope is False
        if dst_scope and not reuse_dst_scope:
            dst_scope = util.scope_finalize(
                dst_graph.unique_name(dst_scope[:-1]))

        if sgv.graph is dst_graph and not dst_scope:
            logging.warning("The source and the destination are the same! "
                            "Beware: in-place transormation are currently "
                            "experimental.")
        self._info = Transformer._Info(self, sgv, dst_graph, dst_scope,
                                       src_scope)

        # This is a recursive call. In practice, the graph is transformed bottom-up.
        for output_t in self._info.sgv.outputs:
            self._transform_t(output_t)

        sgv_ = self._transform_sgv(sgv)

        ops_mapping = self._info.create_ops_mapping()
        self._info = None
        return sgv_, ops_mapping
Exemple #4
0
  def __call__(self,
               sgv,
               dst_graph,
               dst_scope,
               src_scope="",
               reuse_dst_scope=False):
    """Execute the transformation.

    Args:
      sgv: the source subgraph-view.
      dst_graph: the destination graph.
      dst_scope: the destination scope.
      src_scope: the source scope, which specify the path from which the
        relative path of the transformed nodes are computed. For instance, if
        src_scope is a/ and dst_scoped is b/, then the node a/x/y will have a
        relative path of x/y and will be transformed into b/x/y.
      reuse_dst_scope: if True the dst_scope is re-used if it already exists.
        Otherwise, the scope is given a unique name based on the one given
        by postfixing an underscore followed by a digit (default).
    Returns:
      A tuple `(sgv, ops_mapping)` where:
        `sgv` is the transformed subgraph view;
        `ops_mapping` is a dictionary mapping the name of the original ops
        to the name of the transformed ops.
    Raises:
      ValueError: if the argumens are invalid.
    """
    sgv = subgraph.make_view(sgv)
    if not isinstance(dst_graph, tf_ops.Graph):
      raise TypeError("Expected a tf.Graph, got: {}".format(type(dst_graph)))

    src_scope = util.scope_finalize(src_scope)
    dst_scope = util.scope_finalize(dst_scope)

    # Potentially create new scope if reuse_dst_scope is False
    if dst_scope and not reuse_dst_scope:
      dst_scope = util.scope_finalize(dst_graph.unique_name(dst_scope[:-1]))

    if sgv.graph is dst_graph and not dst_scope:
      logging.warning("The source and the destination are the same! "
                      "Beware: in-place transormation are currently "
                      "experimental.")
    self._info = Transformer._Info(self, sgv, dst_graph, dst_scope, src_scope)

    # This is a recursive call. In practice, the graph is transformed bottom-up.
    for output_t in self._info.sgv.outputs:
      self._transform_t(output_t)

    sgv_ = self._transform_sgv(sgv)

    ops_mapping = self._info.create_ops_mapping()
    self._info = None
    return sgv_, ops_mapping
Exemple #5
0
    def __call__(self,
                 sgv,
                 dst_graph,
                 dst_scope,
                 src_scope="",
                 reuse_dst_scope=False):
        """Execute the transformation.

    Args:
      sgv: the source subgraph-view.
      dst_graph: the destination graph.
      dst_scope: the destination scope.
      src_scope: the source scope, which specify the path from which the
        relative path of the transformed nodes are computed. For instance, if
        src_scope is a/ and dst_scoped is b/, then the node a/x/y will have a
        relative path of x/y and will be transformed into b/x/y.
      reuse_dst_scope: if True the dst_scope is re-used if it already exists.
        Otherwise, the scope is given a unique name based on the one given
        by appending an underscore followed by a digit (default).
    Returns:
      A tuple `(sgv, info)` where:
        `sgv` is the transformed subgraph view;
        `info` is an instance of TransformerInfo containing
        information about the transform, including mapping between
        original and transformed tensors and operations.
    Raises:
      ValueError: if the arguments are invalid.
    """
        sgv = subgraph.make_view(sgv)
        if not isinstance(dst_graph, tf_ops.Graph):
            raise TypeError("Expected a tf.Graph, got: {}".format(
                type(dst_graph)))

        src_scope = util.scope_finalize(src_scope)
        dst_scope = util.scope_finalize(dst_scope)

        # Potentially create new scope if reuse_dst_scope is False
        if dst_scope and not reuse_dst_scope:
            dst_scope = util.scope_finalize(
                dst_graph.unique_name(dst_scope[:-1]))

        # Create temporary info used during this transform call
        info = _TmpInfo(sgv, dst_graph, dst_scope, src_scope)
        info.transform_original_op_handler = self.transform_original_op_handler

        self._copy_ops(info)
        self._connect_ops(info)

        # Compute information about the transformation
        res_info = TransformerInfo(info)
        sgv_ = self._transform_sgv(info, sgv)
        return sgv_, res_info
Exemple #6
0
  def __call__(self,
               sgv,
               dst_graph,
               dst_scope,
               src_scope="",
               reuse_dst_scope=False):
    """Execute the transformation.

    Args:
      sgv: the source subgraph-view.
      dst_graph: the destination graph.
      dst_scope: the destination scope.
      src_scope: the source scope, which specify the path from which the
        relative path of the transformed nodes are computed. For instance, if
        src_scope is a/ and dst_scoped is b/, then the node a/x/y will have a
        relative path of x/y and will be transformed into b/x/y.
      reuse_dst_scope: if True the dst_scope is re-used if it already exists.
        Otherwise, the scope is given a unique name based on the one given
        by appending an underscore followed by a digit (default).
    Returns:
      A tuple `(sgv, info)` where:
        `sgv` is the transformed subgraph view;
        `info` is an instance of TransformerInfo containing
        information about the transform, including mapping between
        original and transformed tensors and operations.
    Raises:
      ValueError: if the arguments are invalid.
    """
    sgv = subgraph.make_view(sgv)
    if not isinstance(dst_graph, tf_ops.Graph):
      raise TypeError("Expected a tf.Graph, got: {}".format(type(dst_graph)))

    src_scope = util.scope_finalize(src_scope)
    dst_scope = util.scope_finalize(dst_scope)

    # Potentially create new scope if reuse_dst_scope is False
    if dst_scope and not reuse_dst_scope:
      dst_scope = util.scope_finalize(dst_graph.unique_name(dst_scope[:-1]))

    # Create temporary info used during this transform call
    info = _TmpInfo(sgv, dst_graph, dst_scope, src_scope)

    self._copy_ops(info)
    self._finalize_cycles(info)
    self._connect_control_inputs(info)

    # Compute information about the transformation
    res_info = TransformerInfo(info)
    sgv_ = self._transform_sgv(info, sgv)
    return sgv_, res_info
Exemple #7
0
    def __call__(self, sgv, dst_graph, dst_scope, src_scope=""):
        """Execute the transformation.

    Args:
      sgv: the source subgraph-view.
      dst_graph: the destination graph.
      dst_scope: the destination scope.
      src_scope: the source scope, which specify the path from which the
        relative path of the transformed nodes are computed. For instance, if
        src_scope is a/ and dst_scoped is b/, then the node a/x/y will have a
        relative path of x/y and will be transformed into b/x/y.
    Returns:
      The transformed subgraph view.
    Raises:
      ValueError: if the argumens are invalid. For instance, if the source and
        destination are the same.
    """
        src_scope = util.scope_finalize(src_scope)
        dst_scope = util.scope_finalize(dst_scope)

        sgv = subgraph.make_view(sgv)
        if sgv.graph is dst_graph and not dst_scope:
            logging.warning("The source and the destination are the same! "
                            "Beware: in-place transormation are currently "
                            "experimental.")
        self._info = Transformer._Info(self, sgv, dst_graph, dst_scope,
                                       src_scope)

        # This is a recursive call. In practice, the graph is transformed bottom-up.
        for output_t in self._info.sgv.outputs:
            self._transform_t(output_t)

        sgv_ = self._transform_sgv(sgv)

        self._info = None
        return sgv_
Exemple #8
0
    def __call__(self,
                 sgv,
                 dst_graph,
                 dst_scope,
                 src_scope="",
                 reuse_dst_scope=False):
        """Execute the transformation.

    Args:
      sgv: the source subgraph-view.
      dst_graph: the destination graph.
      dst_scope: the destination scope.
      src_scope: the source scope, which specify the path from which the
        relative path of the transformed nodes are computed. For instance, if
        src_scope is a/ and dst_scoped is b/, then the node a/x/y will have a
        relative path of x/y and will be transformed into b/x/y.
      reuse_dst_scope: if True the dst_scope is re-used if it already exists.
        Otherwise, the scope is given a unique name based on the one given
        by appending an underscore followed by a digit (default).
    Returns:
      A tuple `(sgv, info)` where:
        `sgv` is the transformed subgraph view;
        `info` is an instance of Transformer.ResultInfo containing
        information about the transform, including mapping between
        original and transformed tensors and operations.
    Raises:
      ValueError: if the arguments are invalid.
    """
        sgv = subgraph.make_view(sgv)
        if not isinstance(dst_graph, tf_ops.Graph):
            raise TypeError("Expected a tf.Graph, got: {}".format(
                type(dst_graph)))

        src_scope = util.scope_finalize(src_scope)
        dst_scope = util.scope_finalize(dst_scope)

        # Potentially create new scope if reuse_dst_scope is False
        if dst_scope and not reuse_dst_scope:
            dst_scope = util.scope_finalize(
                dst_graph.unique_name(dst_scope[:-1]))

        # Create temporary info used during this transform call
        self._info = Transformer._Info(self, sgv, dst_graph, dst_scope,
                                       src_scope)

        # Transform the graph starting from the output tensors.
        for output_t in self._info.sgv.outputs:
            self._transform_t(output_t)

        # Some ops might have been missed by the previous walk, namely, the roots
        # without any outputs. So the walk is now finalized from those roots.
        remaining_ops = [
            op for op in self._info.sgv.ops
            if op not in self._info.transformed_ops
        ]
        remaining_roots = [
            op for op in remaining_ops
            if not op.outputs and not self._info.control_outputs.get(op)
        ]
        for op in remaining_roots:
            self._transform_op(op)

        sgv_ = self._transform_sgv(sgv)

        res_info = Transformer.ResultInfo(self._info)
        self._info = None
        return sgv_, res_info