returnn.extern.graph_editor.transform

Class to transform an subgraph into another.

returnn.extern.graph_editor.transform.replace_t_with_placeholder_handler(info, t)[source]

Transform a tensor into a placeholder tensor.

This handler is typically used to transform a subgraph input tensor into a placeholder.

Args:

info: Transform._TmpInfo instance. t: tensor whose input must be transformed into a place holder.

Returns:

The tensor generated by the newly created place holder.

returnn.extern.graph_editor.transform.keep_t_if_possible_handler(info, t)[source]

Transform a tensor into itself (identity) if possible.

This handler transform a tensor into itself if the source and destination graph are the same. Otherwise it will create a placeholder. This handler is typically used to transform a hidden input tensors.

Args:

info: Transform._TmpInfo instance. t: tensor whose input must be transformed into a place holder.

Returns:

The tensor generated by the newly created place holder.

returnn.extern.graph_editor.transform.assign_renamed_collections_handler(info, elem, elem_)[source]

Add the transformed elem to the (renamed) collections of elem.

A collection is renamed only if is not a known key, as described in tf.compat.v1.GraphKeys.

Args:

info: Transform._TmpInfo instance. elem: the original element (tf.Tensor or tf.Operation) elem_: the transformed element

returnn.extern.graph_editor.transform.transform_op_if_inside_handler(info, op, keep_if_possible=True)[source]

Transform an optional op only if it is inside the subgraph.

This handler is typically use to handle original op: it is fine to keep them if they are inside the subgraph, otherwise they are just ignored.

Args:

info: Transform._TmpInfo instance. op: the optional op to transform (or ignore). keep_if_possible: re-attach to the original op if possible, that is,

if the source graph and the destination graph are the same.

Returns:

The transformed op or None.

returnn.extern.graph_editor.transform.copy_op_handler(info, op, new_inputs)[source]

Copy a tf.Operation.

Args:

info: Transform._TmpInfo instance. op: the tf.Operation to be copied. new_inputs: The new inputs for this op.

Returns:

A (op, op_outputs) tuple containing the transformed op and its outputs.

class returnn.extern.graph_editor.transform.Transformer[source]

Transform a subgraph into another one.

By default, the constructor create a transform which copy a subgraph and replaces inputs with placeholders. This behavior can be modified by changing the handlers.

Transformer constructor.

The following members can be modified: transform_op_handler: handle the transformation of a tf.Operation.

This handler defaults to a simple copy.

assign_collections_handler: handle the assignment of collections.

This handler defaults to assigning new collections created under the given name-scope.

transform_external_input_handler: handle the transform of the inputs to

the given subgraph. This handler defaults to creating placeholders instead of the ops just before the input tensors of the subgraph.

transform_external_hidden_input_handler: handle the transform of the

hidden inputs of the subgraph, that is, the inputs which are not listed in sgv.inputs. This handler defaults to a transform which keep the same input if the source and destination graphs are the same, otherwise use placeholders.

transform_original_op_handler: handle the transform of original_op. This

handler defaults to transforming original_op only if they are in the subgraph, otherwise they are ignored.

class returnn.extern.graph_editor.transform.TransformerInfo(info)[source]

“Contains information about the result of a transform operation.

Constructor.

Args:
info: an instance of Transformer._TmpInfo containing various internal

information about the transform operation.

transformed(original, missing_fn=None)[source]

Return the transformed op/tensor corresponding to the original one.

Note that the output of this function mimics the hierarchy of its input argument original. Given an iterable, it returns a list. Given an operation or a tensor, it will return an operation or a tensor.

Args:

original: the original tensor/operation. missing_fn: function handling the case where the counterpart

cannot be found. By default, None is returned.

Returns:

the transformed tensor/operation (or None if no match is found).

original(transformed, missing_fn=None)[source]

Return the original op/tensor corresponding to the transformed one.

Note that the output of this function mimics the hierarchy of its input argument transformed. Given an iterable, it returns a list. Given an operation or a tensor, it will return an operation or a tensor.

Args:

transformed: the transformed tensor/operation. missing_fn: function handling the case where the counterpart

cannot be found. By default, None is returned.

Returns:

the original tensor/operation (or None if no match is found).

returnn.extern.graph_editor.transform.copy(sgv, dst_graph=None, dst_scope='', src_scope='', reuse_dst_scope=False)[source]

Copy a subgraph.

Args:
sgv: the source subgraph-view. This argument is converted to a subgraph

using the same rules than the function subgraph.make_view.

dst_graph: the destination graph. dst_scope: the destination scope. src_scope: the source scope. 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:

TypeError: if dst_graph is not a tf.Graph. StandardError: if sgv cannot be converted to a SubGraphView using

the same rules than the function subgraph.make_view.

returnn.extern.graph_editor.transform.copy_with_input_replacements(sgv, replacement_ts, dst_graph=None, dst_scope='', src_scope='', reuse_dst_scope=False)[source]

Copy a subgraph, replacing some of its inputs.

Note a replacement only happens if the tensor to be replaced is an input of the given subgraph. The inputs of a subgraph can be queried using sgv.inputs.

Args:
sgv: the source subgraph-view. This argument is converted to a subgraph

using the same rules as the function subgraph.make_view.

replacement_ts: dictionary mapping from original tensors to the

replaced one.

dst_graph: the destination graph. dst_scope: the destination scope. src_scope: the source scope. 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:

TypeError: if dst_graph is not a tf.Graph. StandardError: if sgv cannot be converted to a SubGraphView using

the same rules as the function subgraph.make_view.

returnn.extern.graph_editor.transform.graph_replace(target_ts, replacement_ts, dst_scope='', src_scope='', reuse_dst_scope=False)[source]

Create a new graph which compute the targets from the replaced Tensors.

Args:

target_ts: a single tf.Tensor or an iterable of tf.Tensor. replacement_ts: dictionary mapping from original tensors to replaced tensors dst_scope: the destination scope. src_scope: the source scope. 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 single tf.Tensor or a list of target tf.Tensor, depending on the type of the input argument target_ts. The returned tensors are recomputed using the tensors from replacement_ts.

Raises:

ValueError: if the targets are not connected to replacement_ts.