import dataclasses from typing import Callable, Generator import torch import torch.nn as nn Batch = tuple[torch.Tensor, torch.Tensor, torch.Tensor] BatchGenerator = Generator[Batch, None, None] @dataclasses.dataclass class ModelEntry: name: str make_model: Callable[[int, int, int], nn.Module] make_optimizer: Callable[[nn.Module], torch.optim.Optimizer] | None = None train_step: Callable[ [nn.Module, torch.optim.Optimizer, torch.Tensor, torch.Tensor, torch.Tensor], float] | None = None config_tester: Callable | None = None config_defaults: dict | None = None _model_registry: dict[str, ModelEntry] = {} def _get_or_create_arch(name: str) -> ModelEntry: if name not in _model_registry: # noinspection PyTypeChecker _model_registry[name] = ModelEntry(name=name, make_model=None) return _model_registry[name] def model_init(model_name: str): """ Register a model factory function. The decorated function must have signature: (n_params: int, vocab_size: int, seq_len: int) -> nn.Module :param model_name: The name of the model to register. :return: None """ def decorator(func: Callable) -> Callable: entry = _get_or_create_arch(model_name) entry.make_model = func return func return decorator def optimizer(model_name: str): """ Optionally override the default optimizer for a model. If not provided, the default optimizer (AdamW) will be used. The decorated function must have signature: (model: nn.Module) -> torch.optim.Optimizer :param model_name: The name of the model to register the optimizer for. :return: None """ def decorator(func: Callable) -> Callable: entry = _get_or_create_arch(model_name) entry.make_optimizer = func return func return decorator def train_step(model_name: str): """ Optionally override the default training step for a model. The decorated function must have signature: (model, optimizer, input_ids, target_ids, loss_mask) -> float :param model_name: The name of the model to register the training step for. :return: None """ def decorator(func: Callable) -> Callable: entry = _get_or_create_arch(model_name) entry.train_step = func return func return decorator def config_tester(model_name: str, **defaults): """ Register a param counter for a simple config UI. Decorator kwargs are the architecture-specific fields and their defaults. The decorated function must have signature: (vocab_size: int, seq_len: int, **kwargs) -> int where **kwargs matches the decorator kwargs. :param model_name: The name of the model to register the param counter for. :param defaults: Params with default values for all params that the config UI should vary. :return: None """ def decorator(func: Callable) -> Callable: entry = _get_or_create_arch(model_name) entry.config_tester = func entry.config_defaults = defaults return func return decorator @dataclasses.dataclass class TaskEntry: name: str vocab_size: int | Callable[[int], int] batch_generator: Callable[[int, int, int | None], BatchGenerator] seq_len: int _task_registry: dict[str, TaskEntry] = {} def task(name: str, vocab_size: int | Callable[[int], int], seq_len: int = 64): """ Register a task generator. The decorated function must have signature: (seq_len: int, batch_size: int, seed: int | None = None) -> Generator[Batch, None, None] where Batch = (input_ids, target_ids, loss_mask), all (B, T) tensors. input_ids and target_ids are torch.long; loss_mask is torch.bool. vocab_size may be a plain int or a callable (seq_len) -> int for tasks whose vocab grows with sequence length. :param name: The name of the task to register. :param vocab_size: The plain int or callable (seq_len) -> int for tasks. :param seq_len: The sequence length to use for this task. :return: None """ def decorator(func: Callable) -> Callable: _task_registry[name] = TaskEntry( name=name, vocab_size=vocab_size, batch_generator=func, seq_len=seq_len, ) return func return decorator def get_model(model_name: str) -> ModelEntry: if model_name not in _model_registry: raise KeyError(f"No arch registered under name '{model_name}'.") if _model_registry[model_name].make_model is None: raise KeyError(f"Model '{model_name}' has no @model_init function.") return _model_registry[model_name] def get_task(task_name: str) -> TaskEntry: if task_name not in _task_registry: raise KeyError(f"No task registered under name '{task_name}'.") return _task_registry[task_name] def all_models() -> dict[str, ModelEntry]: return dict(_model_registry) def all_tasks() -> dict[str, TaskEntry]: return dict(_task_registry)