Skip to content

Loss registry

A loss attachment attaches a weighted scalar training objective to a declared component, such as the reconstruction loss, which penalizes a low probability of the original tree. Each compiled module has a LossRegistry that maps attachment sites to weighted loss functions. The sites are program, deduction, encoder, decoder, and rule names, and the chart and global sites.

The training driver calls LossRegistry.evaluate(env) to sum the registered losses. evaluate_on(kind, target, env, rule_deduction) returns the weighted partial sum for one attachment site.

The QVR compiler compiles loss bodies as let-expression closures with signature (env) -> Tensor. For a global loss, env contains the compiled module's program, deduction, encoder, and decoder bindings as top-level names. For a loss attached to a rule, it also contains "rule", "deduction", "antecedents", "conclusion", and "weight" keys populated by the callback the agenda calls when a rule fires.

losses

Loss attachment registry.

Holds the table of weighted scalar losses declared in a compiled module, keyed by attachment site, so the training driver can evaluate the right ones at the right point in the training step.

LossEntry didactic-model

LossEntry(*, name: str, body: LossBody = field(opaque=True, extras=OPAQUE_BY_IDENTITY), weight: LossWeight | None = field(default=None, opaque=True, extras=OPAQUE_BY_IDENTITY), attachment_kind: AttachmentKind = 'global', target: str | None = None, rule_deduction: str | None = None)

Bases: ValueModel

One registered loss.

ATTRIBUTE DESCRIPTION
name

Diagnostic identifier (the DSL name).

TYPE: str

body

Computes the scalar loss given a training-step environment.

TYPE: callable

weight

Computes a scalar multiplier given the same environment, or None for an implicit weight of 1.

TYPE: callable | None

attachment_kind

Where this loss fires (see AttachmentKind).

TYPE: str

target

Name of the attachment target (program / deduction / encoder / decoder / chart / rule).

TYPE: str | None

rule_deduction

For rule-attached losses, the deduction the rule lives in.

TYPE: str | None

LossRegistry didactic-model

LossRegistry(*, entries: list[LossEntry] = field(default_factory=list, opaque=True, extras=OPAQUE_BY_IDENTITY))

Bases: ValueModel

All losses declared in a compiled module.

ATTRIBUTE DESCRIPTION
entries

The registered losses in registration order.

TYPE: list[LossEntry]

add

add(entry: LossEntry) -> None

Register a loss.

PARAMETER DESCRIPTION
entry

The loss to append to the registry.

TYPE: LossEntry

Source code in src/quivers/structural/losses.py
def add(self, entry: LossEntry) -> None:
    """Register a loss.

    Parameters
    ----------
    entry : LossEntry
        The loss to append to the registry.
    """
    self.entries.append(entry)

by_attachment

by_attachment(kind: AttachmentKind, target: str | None = None) -> list[LossEntry]

Return the losses attached to a kind of construct.

PARAMETER DESCRIPTION
kind

The attachment kind to select.

TYPE: AttachmentKind

target

When given, only losses attached to the target of this name are returned.

TYPE: str or None DEFAULT: None

RETURNS DESCRIPTION
list[LossEntry]

The matching losses in registration order.

Source code in src/quivers/structural/losses.py
def by_attachment(
    self,
    kind: AttachmentKind,
    target: str | None = None,
) -> list[LossEntry]:
    """Return the losses attached to a kind of construct.

    Parameters
    ----------
    kind : AttachmentKind
        The attachment kind to select.
    target : str or None
        When given, only losses attached to the target of this
        name are returned.

    Returns
    -------
    list[LossEntry]
        The matching losses in registration order.
    """
    return [
        e
        for e in self.entries
        if e.attachment_kind == kind and (target is None or e.target == target)
    ]

evaluate

evaluate(env: Mapping[str, TrainEnv] | None = None) -> Tensor

Sum all registered losses, weighted, under env.

PARAMETER DESCRIPTION
env

The training-step environment passed to every loss body and weight; empty when omitted.

TYPE: Mapping[str, TrainEnv] or None DEFAULT: None

RETURNS DESCRIPTION
Tensor

The scalar weighted sum of the losses.

Source code in src/quivers/structural/losses.py
def evaluate(
    self,
    env: Mapping[str, TrainEnv] | None = None,
) -> torch.Tensor:
    """Sum all registered losses, weighted, under ``env``.

    Parameters
    ----------
    env : Mapping[str, TrainEnv] or None
        The training-step environment passed to every loss body
        and weight; empty when omitted.

    Returns
    -------
    torch.Tensor
        The scalar weighted sum of the losses.
    """
    return self._weighted_sum(self.entries, env or {})

evaluate_on

evaluate_on(kind: AttachmentKind, target: str | None = None, env: Mapping[str, TrainEnv] | None = None, rule_deduction: str | None = None) -> Tensor

Sum only the losses whose attachment matches the filter.

kind selects the attachment kind; target filters by attachment target (the program / deduction / encoder / decoder / rule name); rule_deduction further narrows the "rule" kind to a specific enclosing deduction.

PARAMETER DESCRIPTION
kind

The attachment kind to select.

TYPE: AttachmentKind

target

When given, the attachment target the losses must name.

TYPE: str or None DEFAULT: None

env

The training-step environment passed to every loss body and weight; empty when omitted.

TYPE: Mapping[str, TrainEnv] or None DEFAULT: None

rule_deduction

When given, the enclosing deduction a "rule" loss must name.

TYPE: str or None DEFAULT: None

RETURNS DESCRIPTION
Tensor

The scalar weighted sum of the matching losses.

Source code in src/quivers/structural/losses.py
def evaluate_on(
    self,
    kind: AttachmentKind,
    target: str | None = None,
    env: Mapping[str, TrainEnv] | None = None,
    rule_deduction: str | None = None,
) -> torch.Tensor:
    """Sum only the losses whose attachment matches the filter.

    ``kind`` selects the attachment kind; ``target`` filters by
    attachment target (the program / deduction / encoder /
    decoder / rule name); ``rule_deduction`` further narrows the
    ``"rule"`` kind to a specific enclosing deduction.

    Parameters
    ----------
    kind : AttachmentKind
        The attachment kind to select.
    target : str or None
        When given, the attachment target the losses must name.
    env : Mapping[str, TrainEnv] or None
        The training-step environment passed to every loss body
        and weight; empty when omitted.
    rule_deduction : str or None
        When given, the enclosing deduction a ``"rule"`` loss must
        name.

    Returns
    -------
    torch.Tensor
        The scalar weighted sum of the matching losses.
    """
    matching = []
    for e in self.entries:
        if e.attachment_kind != kind:
            continue
        if target is not None and e.target != target:
            continue
        if rule_deduction is not None and e.rule_deduction != rule_deduction:
            continue
        matching.append(e)
    return self._weighted_sum(matching, env or {})

Type aliases

Structural compression: signatures, encoders, decoders, losses.

Runtime substrate for QVR signatures, encoders, decoders, and attached losses: second-order binding signatures with their generated term operations, and a uniform algebraic interface for compressing structured objects to fixed-length vectors and decoding them under a learned distribution.

LossBody

LossBody = Callable[[Mapping[str, 'TrainEnv']], Tensor]

A loss term: maps the training environment to a scalar loss.

LossWeight

LossWeight = Callable[[Mapping[str, 'TrainEnv']], Tensor]

A loss weight: maps the training environment to a scalar multiplier.

TrainEnv

TrainEnv = Module | Tensor | int | float | str | list | tuple | dict

A value of the training environment a loss body reads.

The environment holds compiled encoders, decoders, and deductions (each an nn.Module), input tensors, structured-term observations, and raw scalar or tensor targets.

AttachmentKind

AttachmentKind = Literal['global', 'program', 'deduction', 'encoder', 'decoder', 'rule', 'chart']

The kind of construct a registered loss is attached to.