CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-mmengine

Engine of OpenMMLab projects for training deep learning models based on PyTorch with large-scale training frameworks, configuration management, and monitoring capabilities

Pending
Overview
Eval results
Files

registry.mddocs/

Registry System

Comprehensive component registry system enabling modular architecture with automatic discovery, registration, and instantiation of models, datasets, optimizers, and other components. The registry system is central to MMEngine's plugin architecture and configuration-driven development.

Capabilities

Registry Class

Core registry class for mapping strings to classes with hierarchical organization and scope management.

class Registry:
    def __init__(self, name: str, build_func: callable = None, parent: 'Registry' = None, scope: str = None, locations: list = None):
        """
        Initialize Registry.
        
        Parameters:
        - name: Registry name
        - build_func: Function to build objects from config
        - parent: Parent registry for hierarchy
        - scope: Registry scope
        - locations: Import locations for automatic discovery
        """

    def register_module(self, name: str = None, force: bool = False, module: type = None) -> callable:
        """
        Register module in registry.
        
        Parameters:
        - name: Module name (uses class name if None)
        - force: Whether to override existing registration
        - module: Module class to register
        
        Returns:
        Decorator function or registered module
        """

    def build(self, cfg: dict) -> object:
        """
        Build object from configuration.
        
        Parameters:
        - cfg: Configuration dictionary with 'type' key
        
        Returns:
        Built object instance
        """

    def get(self, key: str) -> type:
        """
        Get registered module class.
        
        Parameters:
        - key: Module name
        
        Returns:
        Registered module class
        """

    def __contains__(self, key: str) -> bool:
        """
        Check if key is registered.
        
        Parameters:
        - key: Module name
        
        Returns:
        True if module is registered
        """

    def __len__(self) -> int:
        """Get number of registered modules."""

    @property
    def name(self) -> str:
        """Registry name."""

    @property
    def scope(self) -> str:
        """Registry scope."""

    @property
    def module_dict(self) -> dict:
        """Dictionary of registered modules."""

Default Scope Management

System for managing registry scopes and hierarchies across the codebase.

class DefaultScope:
    def __init__(self, scope_name: str, overrides: list = None):
        """
        Initialize default scope context.
        
        Parameters:
        - scope_name: Name of the scope
        - overrides: List of scope overrides
        """

    def __enter__(self) -> 'DefaultScope':
        """Enter scope context."""

    def __exit__(self, exc_type, exc_val, exc_tb):
        """Exit scope context."""

    @classmethod
    def get_current_instance(cls) -> 'DefaultScope':
        """
        Get current default scope instance.
        
        Returns:
        Current DefaultScope instance
        """

def init_default_scope(scope: str):
    """
    Initialize default scope.
    
    Parameters:
    - scope: Scope name to set as default
    """

Build Functions

High-level functions for building objects from configurations using registries.

def build_from_cfg(cfg: dict, registry: Registry, default_args: dict = None) -> object:
    """
    Build object from configuration using registry.
    
    Parameters:
    - cfg: Configuration dictionary
    - registry: Registry to use for building
    - default_args: Default arguments to merge
    
    Returns:
    Built object instance
    """

def build_model_from_cfg(cfg: dict, registry: Registry = None, default_args: dict = None):
    """
    Build model from configuration.
    
    Parameters:
    - cfg: Model configuration
    - registry: Model registry (uses MODELS if None)
    - default_args: Default arguments
    
    Returns:
    Built model instance
    """

def build_runner_from_cfg(cfg: dict) -> 'Runner':
    """
    Build runner from configuration.
    
    Parameters:
    - cfg: Runner configuration
    
    Returns:
    Built runner instance
    """

def build_scheduler_from_cfg(cfg: dict, default_args: dict = None):
    """
    Build scheduler from configuration.
    
    Parameters:
    - cfg: Scheduler configuration
    - default_args: Default arguments
    
    Returns:
    Built scheduler instance
    """

Global Registry Instances

Pre-defined global registries for different component types used throughout MMEngine.

# Core component registries
RUNNERS: Registry  # Registry for runner classes
RUNNER_CONSTRUCTORS: Registry  # Registry for runner constructors
HOOKS: Registry  # Registry for hook classes
LOOPS: Registry  # Registry for training loop classes

# Data-related registries
DATASETS: Registry  # Registry for dataset classes
DATA_SAMPLERS: Registry  # Registry for data sampler classes
TRANSFORMS: Registry  # Registry for data transform classes

# Model-related registries  
MODELS: Registry  # Registry for model classes
WEIGHT_INITIALIZERS: Registry  # Registry for weight initializers
MODEL_WRAPPERS: Registry  # Registry for model wrapper classes

# Optimization registries
OPTIMIZERS: Registry  # Registry for optimizer classes
OPTIM_WRAPPERS: Registry  # Registry for optimizer wrappers
OPTIM_WRAPPER_CONSTRUCTORS: Registry  # Registry for optimizer wrapper constructors
PARAM_SCHEDULERS: Registry  # Registry for parameter schedulers

# Evaluation registries
METRICS: Registry  # Registry for metric classes
EVALUATOR: Registry  # Registry for evaluator classes

# Visualization registries
VISBACKENDS: Registry  # Registry for visualization backends
VISUALIZERS: Registry  # Registry for visualizer classes

# Utility registries
LOG_PROCESSORS: Registry  # Registry for log processors
INFERENCERS: Registry  # Registry for inferencer classes
FUNCTIONS: Registry  # Registry for utility functions
TASK_UTILS: Registry  # Registry for task utility functions
STRATEGIES: Registry  # Registry for training strategies

Registry Utilities

Utility functions for working with registries and registry hierarchies.

def traverse_registry_tree(registry: Registry, verbose: bool = False) -> dict:
    """
    Traverse registry hierarchy tree.
    
    Parameters:
    - registry: Root registry to traverse
    - verbose: Whether to include detailed information
    
    Returns:
    Dictionary representing registry tree structure
    """

def count_registered_modules(registry: Registry, verbose: bool = False) -> int:
    """
    Count registered modules in registry.
    
    Parameters:
    - registry: Registry to count
    - verbose: Whether to print detailed count
    
    Returns:
    Number of registered modules
    """

Usage Examples

Basic Module Registration

from mmengine import Registry

# Create custom registry
MODELS = Registry('model')

# Register using decorator
@MODELS.register_module()
class MyModel:
    def __init__(self, param1, param2):
        self.param1 = param1
        self.param2 = param2

# Register with custom name
@MODELS.register_module(name='CustomModel')
class AnotherModel:
    pass

# Register programmatically
MODELS.register_module(name='ThirdModel', module=SomeModelClass)

Building from Configuration

from mmengine import Registry, build_from_cfg

# Configuration dictionary
cfg = {
    'type': 'MyModel',
    'param1': 'value1',
    'param2': 42
}

# Build object from config
model = build_from_cfg(cfg, MODELS)

# Build with default arguments
default_args = {'param3': 'default_value'}
model = build_from_cfg(cfg, MODELS, default_args)

Using Global Registries

from mmengine import MODELS, OPTIMIZERS, build_from_cfg

# Register model in global registry
@MODELS.register_module()
class ResNet:
    def __init__(self, depth, num_classes):
        self.depth = depth
        self.num_classes = num_classes

# Register optimizer
@OPTIMIZERS.register_module()
class AdamW:
    def __init__(self, lr, weight_decay):
        self.lr = lr
        self.weight_decay = weight_decay

# Build from configs
model_cfg = {'type': 'ResNet', 'depth': 50, 'num_classes': 1000}
model = build_from_cfg(model_cfg, MODELS)

optim_cfg = {'type': 'AdamW', 'lr': 0.001, 'weight_decay': 0.01}
optimizer = build_from_cfg(optim_cfg, OPTIMIZERS)

Scope Management

from mmengine import DefaultScope, Registry

# Create registry with scope
MODELS = Registry('models', scope='myproject')

# Use scope context
with DefaultScope('myproject'):
    @MODELS.register_module()
    class ScopedModel:
        pass

# Initialize default scope
init_default_scope('myproject')

Registry Hierarchy

from mmengine import Registry

# Create parent registry
BASE_MODELS = Registry('base_models')

# Create child registry
SPECIFIC_MODELS = Registry('specific_models', parent=BASE_MODELS)

# Register in parent
@BASE_MODELS.register_module()
class BaseModel:
    pass

# Register in child
@SPECIFIC_MODELS.register_module()
class SpecificModel:
    pass

# Child can access parent modules
assert 'BaseModel' in SPECIFIC_MODELS
assert 'SpecificModel' in SPECIFIC_MODELS

Custom Build Function

from mmengine import Registry

def custom_build_func(cfg, registry, default_args=None):
    """Custom build function with preprocessing."""
    # Preprocess config
    cfg = preprocess_config(cfg)
    
    # Get class from registry
    cls = registry.get(cfg['type'])
    
    # Build with custom logic
    return cls(**cfg)

# Create registry with custom build function
CUSTOM_REGISTRY = Registry('custom', build_func=custom_build_func)

Registry Inspection

from mmengine import MODELS, traverse_registry_tree, count_registered_modules

# Count registered modules
num_models = count_registered_modules(MODELS, verbose=True)

# Traverse registry tree
tree = traverse_registry_tree(MODELS, verbose=True)

# Check registration
if 'ResNet' in MODELS:
    resnet_cls = MODELS.get('ResNet')
    
# List all registered modules
all_models = MODELS.module_dict.keys()

Install with Tessl CLI

npx tessl i tessl/pypi-mmengine

docs

configuration.md

dataset.md

distributed.md

fileio.md

index.md

logging.md

models.md

optimization.md

registry.md

training.md

visualization.md

tile.json