returnn.tf.frontend_layers.layer

Layer (NameCtx earlier)

class returnn.tf.frontend_layers.layer.Layer(*, module: ~returnn.frontend.module.Module | None = None, suggested_name: str | None = None, name: str | None = None, virtual: bool = False, can_access_children: bool = True, new_control_flow_ctx: ~returnn.tensor.control_flow_ctx.ControlFlowContext | None = None, parent: ~returnn.tf.frontend_layers.layer.Layer | None = <class 'returnn.util.basic.NotSpecified'>)[source]

This is a helper class to keep track of the current name context when creating layers. Usually you do not need to access this directly except for creating the root name ctx and getting out the final RETURNN config or net dict.

A name ctx represents one absolute layer name in the RETURNN layer hierarchy, except for the root name ctx.

A name ctx thus can have a parent name ctx (if it is not the root), and potentially child name contexts.

See the documentation on name hierarchies for RETURNN and RETURNN-common in the module docstring at the top.

(Note: This class was previously called NameCtx in RETURNN-common.)

You are not supposed to call this directly. Use NameCtx.new_root() or scoped().

classmethod reset_default_root()[source]

Resets the default root name ctx.

classmethod top() Layer[source]

Return the top of the stack. Assumes that it exists.

classmethod recent_subnet() Layer[source]

Return the most recent subnet.

classmethod current_ctx(*, ignore_top_stack_frames: int = 0) Layer[source]

Return the current context. This is the top from the stack with is_subnet_ctx, and additionally using the Python stack trace to automatically infer further subnets.

Parameters:

ignore_top_stack_frames (int)

classmethod new_root() Layer[source]

Create new root name context

classmethod inner_loop() ControlFlowContext | None[source]
Returns:

the most inner loop in the current context, if there is one E.g. you can use it to access the outer spatial dim.

classmethod inner_control_flow() ControlFlowContext | None[source]
Returns:

the most inner loop in the current context, if there is one E.g. you can use it to access the outer spatial dim.

assign_parent(parent: Layer, suggested_name: str | None = None)[source]

Assign or reassign parent to this name context.

move_tensor_here(tensor: Tensor)[source]

Moves an existing layer ref (with assigned name ctx) to another name ctx (without assigned layer or layer ref).

This assumes that there are no other references to tensor.raw_tensor because those would become invalid. References to tensor itself should be fine.

property root: Layer[source]
Returns:

root name ctx

property is_root: bool[source]
Returns:

whether this is a root ctx

property can_access_children_from_root[source]
Returns:

whether can_access_children for self and all parents

control_flow_ctx() ControlFlowContext | None[source]
Returns:

control flow context of this name ctx

extend_reserved_names(names: Set[str])[source]

Extend reserved child names.

get_returnn_config(*, root_module: Module) _ReturnnConfigSerializer[source]
Parameters:

root_module – there must be one root module such that all params have a well-defined name

Returns:

config serializer

make_net() Net[source]

Create new (sub) net, an instance of Net.

make_default_output(ref: Tensor) Tensor[source]

Assume this is a subnet, or the root net, and make a default output.

get_abs_name_ctx_list() List[Layer][source]

Return list [root name ctx, …, self].

get_abs_name(*, join_str: str = '/') str[source]
Returns:

absolute RETURNN layer name starting from root context.

get_abs_name_repr() str[source]
Returns:

Some repr for our absolute name.

get_name_in_ctx(ctx: Layer, *, middle_prefix: str = '', shorten_subnet: bool = True) str[source]

Get layer name valid in given scope.

get_child(name: str) Layer[source]

Makes sure the child exists.

get_new_child(suggested_name: str) Layer[source]

New child.

get_child_with_tensor(name: str, *, data: Tensor) Layer[source]

Makes sure the child exists, including a corresponding layer ref. Creates the child together with a layer ref if it does not exist yet.

get_child_tensor(name: str, *, data: Tensor) Tensor[Layer][source]

Get child layer ref. Makes sure it exists.

get_recent_tensor(*, only_same_control_flow: bool = False) Tensor | None[source]

Get recent tensor if it exists. Can go deeply through children.

get_tensor_dependencies(*, _extra_layer_dict=None) List[Layer][source]
Returns:

list of tensors this tensor depends on

make_all_sub_networks_and_optimize()[source]

Go up all parents and create subnetworks which are not initialized yet. Also optimize by removing obsolete subnetworks (which just consist of one child).

optimize_move_up()[source]

If the parent is a (non-initialized) subnet where we are the only child, move us up.

class returnn.tf.frontend_layers.layer.Net(*, name_ctx: Layer)[source]

Represents a RETURNN (sub) network.

class returnn.tf.frontend_layers.layer.ReturnnDimTagsProxy(*, reserved_names: Set[str] | None = None)[source]

When serialized via __repr__, this represents a dict unique_name -> dim tag. All usages in the network and extern_data will also get proxies when serialized point to this dict.

class DimRefProxy(*, dim: Dim | MarkedDim, name: str | None, path: Tuple[Any, ...], parent: ReturnnDimTagsProxy)[source]

This will be a reference to the global dim_tags __repr__.

property dim: Dim[source]

Dim

ref_repr() str[source]

ref repr

py_id_name() str[source]
Returns:

valid Python identifier

dim_repr()[source]

Dim repr, used for serialization of all registered dim tags. Any derived dims or special dims will not be registered and instead be represented with the same derivation referencing other registered dim tags. See ReturnnDimTagsProxy.dim_ref_repr().

class SetProxy(values: Sequence[Any])[source]

This represents a set but with a predefined order. We want a deterministic order in the repr such that the generated code stays deterministic.

copy() ReturnnDimTagsProxy[source]
Returns:

creates a shallow copy

py_code_str(exclude_dims: Collection[DimRefProxy] = ())[source]
Parameters:

exclude_dims – dim tags to exclude from serializing

Returns:

Python code

dim_ref_repr(dim: Dim | MarkedDim, *, brackets: bool = True, prefer_ref: bool = True) str[source]
Returns:

for the given dim, Python code which refers to it, via dim_tags

collect_dim_tags_and_transform_config(config: T) T[source]

Go through the config and collect all dim tags, replace them by proxies (DimRefProxy or SetProxy).

Returns:

new config

returnn.tf.frontend_layers.layer.auto_setup_name_ctx_ignore_func(func: LambdaType | Callable)[source]

Registers the func in the blacklist.