Skip to content

Event Graph Mamba model

The active model uses continuous message history and independent recording streams on one GPU. See event architecture.

event_graph_mamba

Message-resolution Mamba and learned causal CAN-ID graph readout.

Every observed frame enters Mamba before any learned compression. Graph keys are latest preceding per-ID temporal states, with learned attention rather than count/change correlation edges. This is a separate, incompatible checkpoint type.

EventGraphMamba

EventGraphMamba(*, width: int = 128, layers: int = 2, d_state: int = 16, d_conv: int = 4, expand: int = 2, dropout: float = 0.1, backend: str = 'official', learning_rate: float = 0.001, weight_decay: float = 0.01, payload_weight: float = 1.0, positive_weight: float = 1.0, node_capacity: int = 512, max_graph_tokens: int = defaults.MAX_GRAPH_TOKENS, use_id_gap: bool | None = None, timing_min_support: int = 32, schema_version: int = 2, **legacy_options)

Bases: LightningModule

Source code in graphids/models/event_graph_mamba.py
def __init__(
    self,
    *,
    width: int = 128,
    layers: int = 2,
    d_state: int = 16,
    d_conv: int = 4,
    expand: int = 2,
    dropout: float = 0.1,
    backend: str = "official",
    learning_rate: float = 1e-3,
    weight_decay: float = 0.01,
    payload_weight: float = 1.0,
    positive_weight: float = 1.0,
    node_capacity: int = 512,
    max_graph_tokens: int = defaults.MAX_GRAPH_TOKENS,
    use_id_gap: bool | None = None,
    timing_min_support: int = 32,
    schema_version: int = 2,
    **legacy_options,
):
    super().__init__()
    cfg = EventGraphMambaConfig(
        width=width,
        layers=layers,
        d_state=d_state,
        d_conv=d_conv,
        expand=expand,
        dropout=dropout,
        backend=backend,
        learning_rate=learning_rate,
        weight_decay=weight_decay,
        payload_weight=payload_weight,
        positive_weight=positive_weight,
        node_capacity=node_capacity,
        max_graph_tokens=max_graph_tokens,
        use_id_gap=schema_version == 2 if use_id_gap is None else use_id_gap,
        timing_min_support=timing_min_support,
        schema_version=schema_version,
        **legacy_options,
    )
    self.save_hyperparameters(cfg.model_dump(exclude={"type"}))
    # Exact frame fields encoded numerically: bytes, byte-presence masks,
    # all 29 ID bits, byte length, elapsed time, preceding interarrival time.
    header_width = 32 + 3 * int(cfg.use_id_gap)
    self.input_proj = nn.Linear(16 + header_width, cfg.width)
    self.norms = nn.ModuleList(nn.LayerNorm(cfg.width) for _ in range(cfg.layers))
    if cfg.backend == "official":
        from graphids.runtime_checks import assert_mamba_backend

        assert_mamba_backend()
        from mamba_ssm import Mamba

        self.temporal = nn.ModuleList(
            Mamba(
                d_model=cfg.width,
                d_state=cfg.d_state,
                d_conv=cfg.d_conv,
                expand=cfg.expand,
                layer_idx=i,
            )
            for i in range(cfg.layers)
        )
    else:
        from .mamba_reference import ReferenceMamba

        self.temporal = nn.ModuleList(
            ReferenceMamba(cfg.width, cfg.d_state, cfg.d_conv, cfg.expand)
            for _ in range(cfg.layers)
        )
    self.dropout = nn.Dropout(cfg.dropout)
    self.query, self.key, self.value = (
        nn.Linear(cfg.width, cfg.width) for _ in range(3)
    )
    self.graph_gate = nn.Linear(cfg.width * 2, cfg.width)
    self.graph_norm = nn.LayerNorm(cfg.width)
    self.classifier = nn.Linear(cfg.width, 1)
    # Header/current arrival are known; current payload is absent from every
    # path feeding this forecast. Predictions precede the frame-state update.
    self.payload_head = nn.Sequential(
        nn.Linear(cfg.width * 2 + header_width, cfg.width),
        nn.SiLU(),
        nn.Linear(cfg.width, 8 * 256),
    )
    if cfg.use_id_gap:
        self.register_buffer("timing_ids", torch.empty(0, dtype=torch.long))
        self.register_buffer(
            "timing_statistics", torch.empty((0, 4), dtype=torch.float64)
        )
    self.automatic_optimization = False
    self._training_states = {}
    self._epoch_complete = True
    self._stream_state = None
    self._scores = None
    self._source_manifest_digest = None
    self._resume_contract = None
    self._resume_rng = None

batched_loss_sums

batched_loss_sums(outputs, batches)

One classification reduction and one eligible normal-byte reduction.

Source code in graphids/models/event_graph_mamba.py
def batched_loss_sums(self, outputs, batches):
    """One classification reduction and one eligible normal-byte reduction."""
    return self._loss_sums(outputs, batches)

scan

scan(batches, states=None, *, detach=False)

Differentiable equal-length independent streams, with explicit state.

Detach only returned learned history for truncated backpropagation; current chunk outputs retain their complete parameter gradients.

Source code in graphids/models/event_graph_mamba.py
def scan(self, batches, states=None, *, detach=False):
    """Differentiable equal-length independent streams, with explicit state.

    Detach only returned learned history for truncated backpropagation;
    current chunk outputs retain their complete parameter gradients.
    """
    states = (
        states if states is not None else [EventGraphMambaState() for _ in batches]
    )
    if (
        not batches
        or any(len(b.ids) == 0 for b in batches)
        or len(states) != len(batches)
        or len({len(b.ids) for b in batches}) != 1
    ):
        raise ValueError("scan requires one state per equal-length stream")
    if len({(b.recording, b.source_digest) for b in batches}) != len(batches):
        raise ValueError("independent streams must name distinct recordings")
    for batch, state in zip(batches, states, strict=True):
        self._validate_state(batch, state)
    return self._execute(batches, states, detach=detach)

config

Typed configuration for the shared graph-Mamba detector.

EventGraphMambaConfig

Bases: BaseModel

Validated settings shared by YAML construction and checkpoint loading.

read_selected_and_legacy_checkpoints classmethod

read_selected_and_legacy_checkpoints(value)

Strip retired neutral flags; reject checkpoints for removed variants.

Source code in graphids/models/config.py
@model_validator(mode="before")
@classmethod
def read_selected_and_legacy_checkpoints(cls, value):
    """Strip retired neutral flags; reject checkpoints for removed variants."""
    if not isinstance(value, Mapping):
        return value
    value = dict(value)
    for name, neutral in (
        ("use_elapsed_time", True),
        ("timing_mode", "raw"),
        ("payload_context", "none"),
        ("graph_age", False),
        ("graph_age_scale", 1.0),
    ):
        if name in value and value.pop(name) != neutral:
            raise ValueError(
                f"{name} variant is retired; use its frozen source snapshot"
            )
    # Schema 1 predates per-ID context. Preserve historical checkpoint identity.
    if value.get("schema_version") == 1:
        value.setdefault("use_id_gap", False)
    return value