libai.config

class libai.config.LazyCall(target)[source]

Wrap a callable so that when it’s called, the call will not be executed, but returns a dict that describes the call.

LazyCall object has to be called with only keyword arguments. Positional arguments are not yet supported.

Examples:

from libai.config import instantiate, LazyCall
layer_cfg = LazyCall(nn.Conv2d)(in_channels=32, out_channels=32)
layer_cfg.out_channels = 64   # can edit it afterwards
layer = instantiate(layer_cfg)
class libai.config.LazyConfig[source]

Provide methods to save, load, and overrides an omegaconf config object which may contain definition of lazily-constructed objects.

static apply_overrides(cfg, overrides: List[str])[source]

In-place override contents of cfg.

Parameters
  • cfg – an omegaconf config object

  • overrides – list of strings in the format of “a=b” to override configs.

See https://hydra.cc/docs/next/advanced/override_grammar/basic/ for syntax.

Returns

the cfg object

static load(filename: str, keys: Union[None, str, Tuple[str, ]] = None)[source]

Load a config file.

Parameters
  • filename – absolute path or relative path w.r.t. the current working directory

  • keys – keys to load and return. If not given, return all keys (whose values are config objects) in a dict.

static load_rel(filename: str, keys: Union[None, str, Tuple[str, ]] = None)[source]

Similar to load(), but load path relative to the caller’s source file. This has the same functionality as a relative import, except that this method accepts filename as a string, so more characters are allowed in the filename.

static save(cfg, filename: str)[source]

Save a config object to a yaml file. Note that when the config dictionary contains complex objects (e.g. lambda), it can’t be saved to yaml. In that case we will print an error and attempt to save to a pkl file instead.

Parameters
  • cfg – an omegaconf config object

  • filename – yaml file name to save the config file

static to_py(cfg, prefix: str = 'cfg.')[source]

Try to convert a config object into Python-like pseudo code. Note that perfect conversion is not always possible. So the returned results are mainly meant to be human-readable, and not meant to be executed.

Parameters
  • cfg – an omegaconf config object

  • prefix – root name for the resulting code (default: “cfg.”)

Returns

str of formatted Python code

libai.config.configurable(init_func=None, *, from_config=None)[source]

Decorate a function or a class’s __init__ method so that it can be called with a CfgNode object using a from_config() function that translates CfgNode to arguments.

Examples:

# Usage 1: Decorator on __init__:
class A:
    @configurable
    def __init__(self, a, b=2, c=3):
        pass

    @classmethod
    def from_config(cls, cfg):   # 'cfg' must be the first argument
        # Returns kwargs to be passed to __init__
        return {"a": cfg.A, "b": cfg.B}

a1 = A(a=1, b=2)  # regular construction
a2 = A(cfg)       # construct with a cfg
a3 = A(cfg, b=3, c=4)  # construct with extra overwrite

# Usage 2: Decorator on any function. Needs an extra from_config argument:
@configurable(from_config=lambda cfg: {"a: cfg.A, "b": cfg.B})
def a_func(a, b=2, c=3):
    pass

a1 = a_func(a=1, b=2)  # regular call
a2 = a_func(cfg)       # call with a cfg
a3 = a_func(cfg, b=3, c=4)  # call with extra overwrite
Parameters
  • init_func (callable) – a class’s __init__ method in usage 1. The class must have a from_config classmethod which takes cfg as the first argument.

  • from_config (callable) – the from_config function in usage 2. It must take cfg as its first argument.

libai.config.get_config(config_path)[source]

Returns a config object from a config_path.

Parameters

config_path (str) – config file name relative to libai’s “configs/” directory, e.g., “common/models/bert.py”

Returns

a config object

Return type

omegaconf.DictConfig

libai.config.instantiate(cfg, **kwargs: Any)Any[source]

Recursively instantiate objects defined in dictionaries by “_target_” and arguments.

Parameters

cfg – a dict-like object with “_target_” that defines the caller, and other keys that define the arguments

Returns

object instantiated by cfg

libai.config.try_get_key(cfg, *keys, default=None)[source]

Try select keys from cfg until the first key that exists. Otherwise return default.