Model Protocols#

Runtime-checkable Protocols for encoder/decoder extraction from models.

Overview#

The chronocratic.models.protocols module defines Protocols that allow users to check whether a model exposes its encoder (and optionally decoder) as a standalone nn.Module.

Available Protocols#

Runtime-checkable protocols for encoder/decoder extraction.

These Protocols provide a uniform, programmatically-verified interface for accessing encoder and decoder modules across all model implementations.

A model satisfies HasEncoder if it exposes a read-only .encoder property that returns an nn.Module. Similarly, HasDecoder requires a .decoder property. Both use @runtime_checkable so they work with isinstance() checks — unlike plain class attributes, which fail isinstance verification due to nn.Module.__getattr__ overriding.

class chronocratic.models.protocols.HasEncoder(*args, **kwargs)#

Bases: Protocol

Protocol for models with a publicly accessible encoder module.

The encoder property must return an nn.Module that can be used for representation extraction, checkpointing, or fine-tuning.

property encoder: Module#

Return the encoder submodule.

class chronocratic.models.protocols.HasDecoder(*args, **kwargs)#

Bases: Protocol

Protocol for models with a publicly accessible decoder module.

The decoder property must return an nn.Module that can be used for reconstruction or generation.

property decoder: Module#

Return the decoder submodule.

Usage#

from chronocratic.models.protocols import HasEncoder, HasDecoder
from chronocratic.models import TimeVAE

model = TimeVAE(seq_len=100, feat_dim=1, latent_dim=10)

# Check encoder availability
if isinstance(model, HasEncoder):
    encoder = model.encoder  # nn.Module

# Check decoder availability
if isinstance(model, HasDecoder):
    decoder = model.decoder  # nn.Module

Implementation Details#

  • HasEncoder requires a @property encoder returning nn.Module

  • HasDecoder requires a @property decoder returning nn.Module

  • Both are @runtime_checkable — use isinstance() for checks

  • The conformance test (tests/test_encoder_decoder_contract.py) verifies all 9 models satisfy these contracts