Skip to content

compose_config

allennlp_hydra.commands.compose_config

[SOURCE]


The compose command creates an AllenNLP config by composing a set of yaml files with Hydra's Compose API. Overriding using Hydra's Override Grammar is also supported.

Parameters

  • config_path : Union[str, PathLike]
    Path to the root config directory.

  • config_name : str
    The name of the root config file. Do NOT include the .yaml.

  • job_name : str
    The job name. This is passed to Hydra and is not used here.

  • -s/--serialization-dir : Union[str, PathLike]
    The directory where the new AllenNLP config will be saved to. The name used for saving will be the config_name and it will be a .json file.

  • -o/--overrides : List[str], optional (default = [])
    Keyword arguments passed will be used as a list of overrides using Hydra's override grammar for the config.

    Example usage:

    allennlp compose conf config example -s example --overrides A=B C="D"
    
    Will be interpreted as overrides ['A=B', 'C="D"']

Example

Say you have the following directory structure:

project
+-- conf
|   +-- dataset_readers
|   |   +-- A.yaml
|   |   +-- B.yaml
|   +-- models
|   |   +-- C.yaml
|   |   +-- D.yaml
|   +-- config.yaml
+-- experiments

conf/dataset_readers/A.yaml:

type: A
start_token: <s>
end_token: </s>

conf/dataset_readers/B.yaml:

type: B
start_token: [CLS]
end_token: [SEP]

conf/models/C.yaml:

type: C
layers: 5

conf/models/D.yaml:

type: D
input_dim: 10

config.yaml

defaults:
    - dataset_reader: A
    - model: C

debug: false

Then running the command

allennlp compose conf config example -s experiments
Produces the file project/experiments/config.json
{
    "dataset_reader":{
        "type": "A",
        "start_token": "<s>",
        "end_token": "</s>"
    },
    "model": {
        "type": "C",
        "layers": 5
    },
    "debug": false
}

If you want to override the config and use the B dataset reader with the D model, you would modify the previous command:

allennlp compose conf config example -s experiments -o model=D dataset_reader=B
Produces the file project/experiments/config.json
{
    "dataset_reader":{
        "type": "B",
        "start_token": "[CLS]",
        "end_token": "[SEP]"
    },
    "model": {
        "type": "D",
        "input_dim": 10
    },
    "debug": false
}

And if you wanted to change input_dim of model D to 25:

allennlp compose conf config example -s experiments -o model=D dataset_reader=B model.input_dim=25

Produces the file project/experiments/config.json

{
    "dataset_reader":{
        "type": "B",
        "start_token": "[CLS]",
        "end_token": "[SEP]"
    },
    "model": {
        "type": "D",
        "input_dim": 25
    },
    "debug": false
}

ComposeConfig#

@Subcommand.register("compose")
class ComposeConfig(Subcommand)

add_subparser#

class ComposeConfig(Subcommand):
 | ...
 | @overrides
 | def add_subparser(
 |     self,
 |     parser: argparse._SubParsersAction
 | ) -> argparse.ArgumentParser

compose_config_from_args#

def compose_config_from_args(args: argparse.Namespace) -> Dict

Wrapper for compose so that it can be called with argparse arguments from the CLI.

Parameters

  • args : argparse.Namespace
    The parsed args from argparse.

Returns

The composed config.

compose_config#

def compose_config(
    config_path: Union[str, PathLike],
    config_name: str,
    job_name: str,
    serialization_dir: Optional[Union[str, PathLike]] = None,
    config_overrides: List[str] = None,
    fill_defaults: bool = False
) -> Dict

Create an AllenNLP config by composing a set of yaml files with Hydra's Compose API. Overriding using Hydra's Override Grammar is also supported.

Parameters

  • config_path : Union[str, PathLike]
    Path to the root config directory.

  • config_name : str
    The name of the root config file.

  • job_name : str
    The job name. This is passed to Hydra and is not used here.

  • serialization_dir : Optional[Union[str, PathLike]], optional (default = None)
    If this is passed, it is the directory where the new AllenNLP config will be saved to. The name used for saving will be the config_name and it will be a .json file.

  • config_overrides : List[str], optional (default = [])
    List of overrides using Hydra's override grammar for the config.

  • fill_defaults : bool, optional (default = False)
    Add arguments and their default values to the config if they are not specified.

Returns

  • Dict
    The dictionary config generated by Hydra.