Loss registry

LossRegistry is the per-compiled-module table mapping attachment sites (program names, deduction names, encoder / decoder names, rule names, chart sites, global) to weighted scalar-loss callables.

The training driver calls LossRegistry.evaluate(env) to sum every registered loss; evaluate_on(kind, target, env, rule_deduction) returns the weighted partial sum filtered to a single attachment site.

Loss bodies are compiled by the QVR compiler as let-expression closures of signature (env) -> Tensor. The env for a global loss includes the compiled module's program / deduction / encoder / decoder bindings as top-level names; for rule-attached losses, the env also carries "rule", "deduction", "antecedents", "conclusion", and "weight" keys populated by the agenda's rule-firing callback.

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 dataclass

LossEntry(name: str, body: LossBody, weight: LossWeight | None = None, attachment_kind: AttachmentKind = 'global', target: str | None = None, rule_deduction: str | None = None)

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 dataclass

LossRegistry(entries: list[LossEntry] = list())

All losses declared in a compiled module.

evaluate

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

Sum all registered losses, weighted, under env.

Source code in src/quivers/structural/losses.py
90
91
92
93
94
95
def evaluate(
    self,
    env: Mapping[str, TrainEnv] | None = None,
) -> torch.Tensor:
    """Sum all registered losses, weighted, under ``env``."""
    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.

Source code in src/quivers/structural/losses.py
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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.
    """
    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 {})