Shortcuts

common.registry

Registry is central source of truth in MMF. Inspired from Redux’s concept of global store, Registry maintains mappings of various information to unique keys. Special functions in registry can be used as decorators to register different kind of classes.

Import the global registry object using

from mmf.common.registry import registry

Various decorators for registry different kind of classes with unique keys

  • Register a trainer: @registry.register_trainer

  • Register a dataset builder: @registry.register_builder

  • Register a callback function: @registry.register_callback

  • Register a metric: @registry.register_metric

  • Register a loss: @registry.register_loss

  • Register a fusion technique: @registery.register_fusion

  • Register a model: @registry.register_model

  • Register a processor: @registry.register_processor

  • Register a optimizer: @registry.register_optimizer

  • Register a scheduler: @registry.register_scheduler

  • Register a encoder: @registry.register_encoder

  • Register a decoder: @registry.register_decoder

  • Register a transformer backend: @registry.register_transformer_backend

  • Register a transformer head: @registry.register_transformer_head

  • Register a test reporter: @registry.register_test_reporter

  • Register a pl datamodule: @registry.register_datamodule

class mmf.common.registry.Registry[source]

Class for registry object which acts as central source of truth for MMF

classmethod get(name, default=None, no_warning=False)[source]

Get an item from registry with key ‘name’

Parameters
  • name (string) – Key whose value needs to be retrieved.

  • default – If passed and key is not in registry, default value will be returned with a warning. Default: None

  • no_warning (bool) – If passed as True, warning when key doesn’t exist will not be generated. Useful for MMF’s internal operations. Default: False

Usage:

from mmf.common.registry import registry

config = registry.get("config")
classmethod register(name, obj)[source]

Register an item to registry with key ‘name’

Parameters

name – Key with which the item will be registered.

Usage:

from mmf.common.registry import registry

registry.register("config", {})
classmethod register_builder(name)[source]

Register a dataset builder to registry with key ‘name’

Parameters

name – Key with which the metric will be registered.

Usage:

from mmf.common.registry import registry
from mmf.datasets.base_dataset_builder import BaseDatasetBuilder


@registry.register_builder("vqa2")
class VQA2Builder(BaseDatasetBuilder):
    ...
classmethod register_callback(name)[source]

Register a callback to registry with key ‘name’

Parameters

name – Key with which the callback will be registered.

Usage:

from mmf.common.registry import registry
from mmf.trainers.callbacks.base import Callback


@registry.register_callback("logistic")
class LogisticCallback(Callback):
    ...
classmethod register_datamodule(name)[source]

Register a datamodule to registry with key ‘name’

Parameters

name – Key with which the datamodule will be registered.

Usage:

from mmf.common.registry import registry
import pytorch_lightning as pl


@registry.register_datamodule("my_datamodule")
class MyDataModule(pl.LightningDataModule):
    ...
classmethod register_decoder(name)[source]

Register a decoder to registry with key ‘name’

Parameters

name – Key with which the decoder will be registered.

Usage:

from mmf.common.registry import registry
from mmf.utils.text import TextDecoder


@registry.register_decoder("nucleus_sampling")
class NucleusSampling(TextDecoder):
    ...
classmethod register_encoder(name)[source]

Register a encoder to registry with key ‘name’

Parameters

name – Key with which the encoder will be registered.

Usage:

from mmf.common.registry import registry
from mmf.modules.encoders import Encoder


@registry.register_encoder("transformer")
class TransformerEncoder(Encoder):
    ...
classmethod register_fusion(name)[source]

Register a fusion technique to registry with key ‘name’

Parameters

name – Key with which the fusion technique will be registered

Usage:

from mmf.common.registry import registry
from torch import nn

@registry.register_fusion("linear_sum")
class LinearSum():
    ...
classmethod register_iteration_strategy(name)[source]

Register an iteration_strategy to registry with key ‘name’

Parameters

name – Key with which the iteration_strategy will be registered.

Usage:

from dataclasses import dataclass
from mmf.common.registry import registry
from mmf.datasets.iterators import IterationStrategy


@registry.register_iteration_strategy("my_iteration_strategy")
class MyStrategy(IterationStrategy):
    @dataclass
    class Config:
        name: str = "my_strategy"
    def __init__(self, config, dataloader):
        ...
classmethod register_loss(name)[source]

Register a loss to registry with key ‘name’

Parameters

name – Key with which the loss will be registered.

Usage:

from mmf.common.registry import registry
from torch import nn

@registry.register_task("logit_bce")
class LogitBCE(nn.Module):
    ...
classmethod register_metric(name)[source]

Register a metric to registry with key ‘name’

Parameters

name – Key with which the metric will be registered.

Usage:

from mmf.common.registry import registry
from mmf.modules.metrics import BaseMetric


@registry.register_metric("r@1")
class RecallAt1(BaseMetric):
    ...
classmethod register_model(name)[source]

Register a model to registry with key ‘name’

Parameters

name – Key with which the model will be registered.

Usage:

from mmf.common.registry import registry
from mmf.models.base_model import BaseModel

@registry.register_task("pythia")
class Pythia(BaseModel):
    ...
classmethod register_pooler(name)[source]

Register a modality pooling method to registry with key ‘name’

Parameters

name – Key with which the pooling method will be registered.

Usage:

from mmf.common.registry import registry
from torch import nn

@registry.register_pool("average_pool")
class average_pool(nn.Module):
    ...
classmethod register_processor(name)[source]

Register a processor to registry with key ‘name’

Parameters

name – Key with which the processor will be registered.

Usage:

from mmf.common.registry import registry
from mmf.datasets.processors import BaseProcessor

@registry.register_task("glove")
class GloVe(BaseProcessor):
    ...
classmethod register_trainer(name)[source]

Register a trainer to registry with key ‘name’

Parameters

name – Key with which the trainer will be registered.

Usage:

from mmf.common.registry import registry
from mmf.trainers.custom_trainer import CustomTrainer


@registry.register_trainer("custom_trainer")
class CustomTrainer():
    ...
classmethod unregister(name)[source]

Remove an item from registry with key ‘name’

Parameters

name – Key which needs to be removed.

Usage:

from mmf.common.registry import registry

config = registry.unregister("config")