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: str) str[source]
Parameters:

f – file path

Returns:

“py”, “js” or “txt”

parse_cmd_args(args: Sequence[str])[source]
Parameters:

args

add_line(key: str, value: str)[source]

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

Parameters:
  • key

  • value

has(key: str) bool[source]

Returns whether the given key is present in the inner set of parameters

Parameters:

key

Returns:

True if and only if the given key is in the inner set of parameters

is_typed(key: str) bool[source]
Parameters:

key

Returns:

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

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

  • default

Returns:

bool(value) if it is set or default

is_of_type(key: str, types: type | Tuple[type, ...]) bool[source]
Parameters:
  • key

  • types – for isinstance() check

Returns:

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

get_of_type(key: str, types: type | Tuple[type, ...] | Type[T], default: T | None = None) T[source]
Parameters:
  • key (str)

  • types – for isinstance() check

  • default

Returns:

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

set(key: str, value: Any)[source]
Parameters:
  • key

  • value

update(dikt: Dict[str, Any])[source]
Parameters:

dikt – dict

value(key: str, default: T, index: int | None = None, list_join_str: str = ',') T | str[source]
Parameters:
  • key

  • default

  • index

  • list_join_str

typed_value(key: str, default: T | None = None, index: int | None = None) T | Any[source]
Parameters:
  • key

  • default

  • index

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

  • default

int(key: str, default: T, index: int = 0) int | T[source]

Parses the value of the given key as integer, returning default if not existent

Parameters:
  • key

  • default

  • index

bool(key: str, default: T, index: int = 0) bool | T[source]

Parses the value of the given key as boolean, returning default if not existent

Parameters:
  • key

  • default

  • index

bool_or_other(key: str, default: T | None = None, index: int = 0) bool | T | Any[source]
Parameters:
  • key

  • default

  • index

Returns:

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

float(key: str, default: T, index: int = 0) float | T[source]

Parses the value of the given key as float, returning default if not existent

Parameters:
  • key

  • default

  • index

list(key: str, default: T | None = None) List[str] | T[source]
Parameters:
  • key

  • default

int_list(key: str, default: T | None = None) List[int] | T[source]
Parameters:
  • key

  • default

float_list(key: str, default: T | None = None) List[float] | T[source]
Parameters:
  • key

  • default

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

  • default

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())