Skip to content

Config schema

frogbox.config

ObjectArgument module-attribute

ObjectArgument = Union['ObjectDefinition', Any]

Argument that can be passed to an object definition.

CallbackDefinition

Bases: ObjectDefinition

Callback definition.

Attributes:

CheckpointDefinition

Bases: StrictModel

Checkpoint definition.

Attributes:

  • interval (EventStep or LogInterval) –

    Interval between saving checkpoints.

  • num_saved (int) –

    Number of checkpoints to save.

  • metric (str) –

    Name of metric to compare (optional).

  • mode (CheckpointMode) –

    Whether to priority maximum or minimum metric value.

CheckpointMode

Bases: str, Enum

Checkpoint evaluation mode.

MAX class-attribute instance-attribute

MAX = 'max'

Keep maximum value.

MIN class-attribute instance-attribute

MIN = 'min'

Keep minium value.

Config

Bases: StrictModel

Base configuration.

Attributes:

  • type (ConfigType) –

    Pipeline type.

  • project (str) –

    Project name.

  • tracker (TrackerType) –

    Tracker to use for logging.

  • tracker_kwargs (Mapping[str, ObjectArgument] | None) –

    Keyword-argument to pass to tracker during initialization.

  • log_interval (EventStep | LogInterval) –

    At which interval to log metrics.

  • batch_size (int) –

    Batch size.

  • loader_workers (int) –

    How many subprocesses to use for data loading. 0 means the data will be loaded in the main process.

  • max_epochs (int) –

    Maximum number of epochs to train for.

  • gradient_accumulation_steps (int) –

    Number of steps the gradients should be accumulated across.

  • checkpoints (Sequence[CheckpointDefinition]) –
  • datasets (Mapping[str, ObjectDefinition]) –

    Dataset definitions.

  • loaders (Mapping[str, ObjectDefinition]) –

    Data loader definitions.

  • metrics (Mapping[str, ObjectDefinition]) –

    Evaluation metrics.

  • callbacks (Sequence[CallbackDefinition]) –

    Callback functions.

ConfigType

Bases: str, Enum

Pipeline configuration type.

SUPERVISED class-attribute instance-attribute

SUPERVISED = 'supervised'

Supervised pipeline config.

EngineType

Bases: str, Enum

Engine type.

EVALUATOR class-attribute instance-attribute

EVALUATOR = 'evaluator'

Evaluator

TRAINER class-attribute instance-attribute

TRAINER = 'trainer'

Trainer

LRSchedulerDefinition

Bases: StrictModel

Learning rate scheduler definition.

Attributes:

  • type (SchedulerType) –

    Scheduler type.

  • end_value (float) –

    Final learning rate.

  • warmup_stets (int) –

    Number of steps to perform warmup. Set to 0 to disable warmup.

LogInterval

Bases: StrictModel

Logging interval.

Attributes:

  • event (Events) –

    Event trigger.

  • interval (int) –

    How often event should trigger. Defaults to every time (1).

  • first (int) –

    First step where event should trigger (zero-indexed).

  • last (int) –

    Last step where vent should trigger (zero-indexed).

LossDefinition

Bases: ObjectDefinition

Loss function definition

Attributes:

  • weight (float) –

    Loss function weight.

  • transform (ObjectDefinition | None) –

    Callable that takes in y_pred and y and outputs what is passed to the loss function.

ModelDefinition

Bases: ObjectDefinition

Model definition.

Attributes:

ObjectDefinition

Bases: StrictModel

Object definition.

Describes either an object or callable instance. Only one of object, function and lambda can provided at a time.

Attributes:

  • object (str | None) –

    Import path to object constructor.

  • function (str | None) –

    Import path to function.

  • lambda

    Lambda function definition.

    Should have format {args}: {expr}. Example: x, y: x + y.

  • args (Sequence[ObjectArgument] | None) –

    Positional arguments.

  • kwargs (Mapping[str, ObjectArgument] | None) –

    Keyword arguments.

Example

Creating an object instance:

YAML
loss_fn:
    object: torch.nn.L1Loss
    kwargs:
        reduction: mean
Equivalent Python
loss_fn = torch.nn.L1Loss(reduction="mean")

Creating a function instance:

YAML
forward:
    function: myfun.forward
    kwargs:
        clamp: true
Equivalent Python
# myfun.py
def forward(x, y, model, clamp):
    y_pred = model(x)
    if clamp:
        pred = pred.clamp(0, 1)
    return y, y_pred

loss_fn = functools.partial(
    myfun.forward,
    clamp=True,
)

Creating a lambda function:

YAML
forward:
    lambda: "x, y, model: (y, model(x))"
Equivalent Python
forward = lambda: x, y, model: (y, model(x))

OptimizerDefinition

Bases: ObjectDefinition

Optimizer definition.

Attributes:

  • scheduler (LRSchedulerDefinition) –

    Learning rate scheduler definition.

  • parameters (str | ObjectDefinition | None) –

    Which parameters in the model to optimize. Must be either a regex matching the parameter names to include or a callable that takes in the model and returns a list of parameters.

Example

Defining target parameters using regex:

config.yml
model:
  ...
  optimizers:
    encoder:
      ...
      parameters: "encoder\..*"

Defining target parameters using a function:

config.yml
model:
  ...
  optimizers:
    encoder:
      ...
      parameters:
        function: params.encoder_params
params.py
import torch

def encoder_params(model: torch.nn.Module):
    return [
        m for n, m in model.named_parameters()
        if n.startswith("encoder.")
    ]

SchedulerType

Bases: str, Enum

Parameter scheduler type.

COSINE class-attribute instance-attribute

COSINE = 'cosine'

Cosine schedule.

LINEAR class-attribute instance-attribute

LINEAR = 'linear'

Linear schedule.

SupervisedConfig

Bases: Config

Supervised pipeline configuration.

Attributes:

  • clip_grad_norm (float | None) –

    Clip gradients to norm if provided.

  • clip_grad_norm (float | None) –

    Clip gradients to value if provided.

  • model (ModelDefinition) –

    Model definition.

  • losses (Mapping[str, LossDefinition]) –

    Loss functions.

  • trainer_forward (ObjectDefinition | None) –

    Trainer custom forward function. Should be function that takes x, y and model and returns (y, y_pred).

  • evaluator_forward (ObjectDefinition | None) –

    Evaluator custom forward function. Should be function that takes x, y and model and returns (y, y_pred).

TrackerType

Bases: str, Enum

Experiment tracker.

MLFLOW class-attribute instance-attribute

MLFLOW = 'mlflow'

MLFlow

WANDB class-attribute instance-attribute

WANDB = 'wandb'

Weights & Biases

create_object_from_config

create_object_from_config(
    config, *additional_args, **additional_kwargs
)

Create object from dictionary configuration. Dictionary should have a class entry and an optional params entry.

Parameters:

  • config

    (ObjectDefinition) –

    Object definition.

  • additional_args

    (*args, default: () ) –

    Additional positional arguments. Appended to positional arguments in config.

  • additional_kwargs

    (**kwargs, default: {} ) –

    Additional keyword arguments. Keyword arguments in config take priority.

Example
config = {"class": "torch.optim.Adam", "params": {"lr": 1e-5}}
optimizer = create_object_from_config(config)

parse_log_interval

parse_log_interval(e)

Create matchable event from log interval configuration.

Returns:

read_config

read_config(path, format=None, config_vars=None)

Read and render config file using jinja2.

Parameters:

  • path

    (str or path - like) –

    Path to JSON config file.

  • format

    (str, default: None ) –

    File format to read. If not provided, format will be inferred from filename.

  • config_vars

    (str-to-str mapping, default: None ) –

    Keyword arguments to pass to jinja2.