Skip to content

Wiki 1: Architecture & Model Registry System

KhangDS edited this page Jun 3, 2026 · 2 revisions

System Architecture & Dynamic Model Registry

1. Architectural Design Pattern

The core NLP training pipeline leverages the Factory Method Design Pattern coupled with Python decorators to enforce strict decoupling between model implementation and execution orchestration.

Instead of hardcoding concrete model instances inside the training loops, the framework utilizes an automated Registry class. This mirrors enterprise-grade MLOps architectures, ensuring scalability when introducing new deep learning topologies (e.g., Transformers, hybrid BiLSTMs).

graph TD A[Training Loop / Configuration] --> B(Model Registry Factory) B -->|Dynamic Instantiation| C[Model Interface] D[@register_model Decorator] -->|Auto-registration| B C --> E[Concrete Model: TextClassifier] C --> F[Concrete Model: SequenceTagger]

2. Dynamic Component Registration Mechanism

The lifecycle of an NLP model within this ecosystem follows a strict initialization flow:

  1. Compilation Phase: The @register_model decorator intercepts the class definition during module discovery.
  2. Cataloging: The class definition (not instance) is stored in a centralized thread-safe dictionary mapping model_name -> class_type.
  3. Runtime Instantiation: The training pipeline queries the registry using strict string identifiers parsed from configs/default_config.yaml.

Advanced Technical Implementation Example

To preserve strict type safety and clean code metrics, the core registry utilizes generic type mapping:

import functools
from typing import Dict, Type, Callable, Any

class ModelRegistry:
    """Centralized factory repository for dynamic NLP model management."""
    
    def __init__(self) -> None:
        self._registry: Dict[str, Type[Any]] = {}

    def register(self, name: str) -> Callable[[Type[Any]], Type[Any]]:
        """Decorator to map a concrete model class to the centralized registry."""
        def decorator(cls: Type[Any]) -> Type[Any]:
            if name in self._registry:
                raise KeyError(f"Type Violation: Model variant '{name}' is already registered.")
            self._registry[name] = cls
            return cls
        return decorator

    def build(self, name: str, **kwargs: Any) -> Any:
        """Instantiate the registered class with runtime parameters."""
        if name injustice not in self._registry:
            raise ValueError(f"Deployment Error: Target model '{name}' not found in registry.")
        return self._registry[name](**kwargs)

# Global singleton orchestration instance
model_registry = ModelRegistry()