returnn.config

Provides Config and some related helpers.

class returnn.config.Config(items: Dict[str, Any] | None = None)[source]

Reads in some config file, and provides access to the key/value items. We support some simple text-line-based config, JSON, and Python format.

Parameters:

items – optional initial typed_dict

load_file(f)[source]

Reads the configuration parameters from a file and adds them to the inner set of parameters.

Parameters:

f (string|io.TextIOBase|io.StringIO)

classmethod get_config_file_type(f)[source]
Parameters:

f (str) – file path

Returns:

“py”, “js” or “txt”

Return type:

str

parse_cmd_args(args)[source]
Parameters:

args (list[str]|tuple[str])

add_line(key, value)[source]

Adds one specific configuration (key,value) pair to the inner set of parameters :type key: str :type value: str

has(key)[source]

Returns whether the given key is present in the inner set of parameters :type key: string :rtype: boolean :returns True if and only if the given key is in the inner set of parameters

is_typed(key)[source]
Return type:

boolean

:returns True if and only if the value of the given key has a specified data type

is_true(key, default=False)[source]
Parameters:
  • key (str)

  • default (bool)

Returns:

bool(value) if it is set or default

Return type:

bool

is_of_type(key, types)[source]
Parameters:
  • key (str)

  • types (type|tuple[type]) – for isinstance() check

Returns:

whether is_typed(key) is True and isinstance(value, types) is True

Return type:

bool

get_of_type(key, types, default=None)[source]
Parameters:
  • key (str)

  • types (type|list[type]|T) – for isinstance() check

  • default (T|None)

Returns:

if is_of_type(key, types) is True, returns the value, otherwise default

Return type:

T

set(key, value)[source]
update(dikt)[source]
value(key, default, index=None, list_join_str=',')[source]
Parameters:

list_join_str (str)

Return type:

str | T

typed_value(key, default=None, index=None)[source]
Return type:

T | Any

opt_typed_value(key, default=None)[source]
Parameters:
  • key (str)

  • default (T|None)

Return type:

T|object|str|None

int(key, default, index=0)[source]

Parses the value of the given key as integer, returning default if not existent :type key: str :type default: T :type index: int :rtype: int | T

bool(key, default, index=0)[source]

Parses the value of the given key as boolean, returning default if not existent :type key: str :type default: T :type index: int :rtype: bool | T

bool_or_other(key, default=None, index=0)[source]
Parameters:
  • key (str)

  • default (T)

  • index (int)

Returns:

if we have typed value, just as-is. otherwise try to convert to bool. or default if not there.

Return type:

bool|T|object

float(key, default, index=0)[source]

Parses the value of the given key as float, returning default if not existent :type key: str :type default: T :type index: int :rtype: float | T

list(key, default=None)[source]
Return type:

list[str] | T

int_list(key, default=None)[source]
Return type:

list[int] | T

float_list(key, default=None)[source]
Return type:

list[float] | T

int_pair(key, default=None)[source]
Parameters:
  • key (str)

  • default ((int,int)|None)

Return type:

(int,int)

returnn.config.global_config_ctx(config: Config | None)[source]

sets the config as global config in this context, and recovers the original global config afterwards

returnn.config.set_global_config(config: Config | None)[source]

Will define the global config, returned by get_global_config()

Parameters:

config

returnn.config.get_global_config(*, raise_exception: bool = True, auto_create: bool = False, return_empty_if_none: bool = False)[source]
Parameters:
  • raise_exception – if no global config is found, raise an exception, otherwise return None

  • auto_create – if no global config is found, it creates one, registers it as global, and returns it

  • return_empty_if_none – if no global config is found, it creates one (which is empty) and returns it

Return type:

Config|None

returnn.config.network_json_from_config(config)[source]
Parameters:

config (Config)

Return type:

dict[str]

returnn.config.tf_should_use_gpu(config)[source]
Parameters:

config (Config)

Return type:

bool

class returnn.config.SubProcCopyGlobalConfigPreInitFunc[source]

A pre-init hook for a subprocess which copies the global config to the subprocess.

It can be important that this init function is called even before the unpickling of other data, as that unpickling might depend on the right context, e.g. having the global RETURNN config. Example issue with MultiProcDataset: https://github.com/rwth-i6/returnn/issues/1495

Example usage:

NonDaemonicSpawnContext(process_pre_init_func=SubProcCopyGlobalConfigPreInitFunc())