Skip to content

Deduction Systems

A weighted deduction system derives conclusions from weighted premises by inference rules, and combines the weights of derivations in a semiring (Shieber et al., 1995; Goodman, 1999). quivers.stochastic.deduction re-exports the model type, its abstract primitives, and the operations below: fitting adjusts rule weights, the Bayesian wrapper places priors on them, and sampling draws yields from the normalized distribution over sentences of one length.

Submodule Job
primitives Abstract building blocks (Axiom, Deduction, Goal, Schedule, DeductiveSystem), used mainly to write new deduction classes.
fit Gradient fitting of point estimates (MAP or MLE) of the learnable log weights.
bayes A MonadicProgram over the learnable log weights whose posterior No-U-Turn Sampler (NUTS) or stochastic variational inference (SVI) can target.
sample Exact forward sampling of yields of a given length from the chart's distribution.

deduction

The deduction-system surface: model type, abstract primitives, and the three independent operations on a deduction.

This package gathers everything a user needs for working with a weighted deduction system in one importable path. The contents fall into three orthogonal groups:

  • Model type (DeductionSystem): the concrete agenda-based chart deduction that compiles from a deduction block in the QVR DSL. Re-exported from quivers.stochastic.agenda.
  • Abstract primitives (.primitives): Axiom, Deduction, Goal, Schedule, DeductiveSystem: the protocol layer the agenda implementation derives from. Most users will not need these directly; they exist for custom-deduction subclasses and for the inside-algorithm framework (quivers.stochastic.inside).
  • Operations: three independent surfaces over a DeductionSystem with no overlap in purpose:
    • .fit: point-estimate gradient fitting (MAP / MLE);
    • .bayes: Bayesian wrapping for NUTS / SVI;
    • .sample: exact length-conditional forward sampling of yields.

These three live in separate submodules because they answer different questions (estimate parameters at a point vs. sample a posterior vs. generate synthetic data) and so that adding more operations in the future does not bloat any one module.

DeductionSystem didactic-model

DeductionSystem(*, rules: tuple[InferenceRule, ...] = field(opaque=True, extras=OPAQUE_BY_VALUE), semiring: ChartSemiring = field(opaque=True, extras=OPAQUE_BY_IDENTITY), axiom_injector: Callable[[HeldValue], list[tuple[Item, Tensor]]] = field(opaque=True, extras=OPAQUE_BY_IDENTITY), goal: Callable[[Item], bool] = field(opaque=True, extras=OPAQUE_BY_IDENTITY), agenda_factory: Callable[[], Agenda] = field(default=FIFOAgenda, opaque=True, extras=OPAQUE_BY_IDENTITY), chart_factory: Callable[..., Chart] = field(default=HashChart, opaque=True, extras=OPAQUE_BY_IDENTITY), max_iterations: int = 100000, tolerance: float = 0.0, mappable: bool = False, productions: ProductionWeights | None = field(default=None, opaque=True, extras=OPAQUE_BY_IDENTITY), production_refusal: str | None = None, goal_extent: Callable[[Item], tuple[int, int] | None] | None = field(default=None, opaque=True, extras=OPAQUE_BY_IDENTITY), name: str = '', modules: dict[str, Module] = field(default_factory=dict, opaque=True, extras=OPAQUE_BY_IDENTITY), item_signature: Signature | None = field(default=None, opaque=True, extras=OPAQUE_BY_IDENTITY), item_encoder: Encoder | None = field(default=None, opaque=True, extras=OPAQUE_BY_IDENTITY), losses: dict[str, LossRegistry] = field(default_factory=dict, opaque=True, extras=OPAQUE_BY_IDENTITY), presentation: RulePresentation | None = field(default=None, opaque=True, extras=OPAQUE_BY_VALUE), reveal: ChartReveal | None = field(default=None, opaque=True, extras=OPAQUE_BY_IDENTITY))

Bases: QiecModel

A weighted deductive system parameterized over its components.

The system is parameterized by:

  • An item algebra (implicit in the patterns the rules use).
  • A list of arity-n InferenceRule hyperedges.
  • A ChartSemiring for weight aggregation.
  • An axiom injector In -> [(Item, Weight)] producing the input's lexical / boundary items.
  • A goal predicate Item -> bool selecting the result items.
  • (Optional) a chart constructor and an agenda strategy.

The same data structure subsumes CKY (FIFO agenda, span items, Boolean / inside semiring), Viterbi (priority agenda with the current weight as priority, max-times semiring), A* parsing (priority agenda with an admissible heuristic, tropical semiring), MLTT type-checking (LIFO agenda, judgment items, Boolean semiring), and weighted Datalog (FIFO, atoms, any naturally-ordered semiring).

ATTRIBUTE DESCRIPTION
name

The declaration name used to look up an attached loss registry.

TYPE: str

modules

Owned parameter modules, keyed by their stable parameter-name prefixes.

TYPE: dict[str, Module]

item_signature

The signature of structured items, when declared.

TYPE: Signature | None

item_encoder

The encoder used by chart embedding queries.

TYPE: Encoder | None

losses

Shared loss registries keyed by deduction name. Compilation can attach a registry after another program has captured this system.

TYPE: dict[str, LossRegistry]

goal_extent

The positions of the input an item covers, declared for an item type whose items cover stretches of the input, None for an item that covers none. A goal of such a type has to cover the whole input. Without a declaration the goal is the predicate alone.

TYPE: Callable or None

mappable

Whether the chart can be filled at many parameter values in one pass: the order of the agenda, the rules that fire, and the axioms depend on the items alone, and the weights are computed by tensor operations. The DSL compiler declares it of the systems it builds.

TYPE: bool

presentation

The rule presentation the rules are derived from: each rule is the rule of the presentation at its position, spelled in the chart representation. None for a system built from rules directly.

TYPE: RulePresentation | None

reveal

What the run's items and applications stand for, when the system runs a deduction through an encoding with items and rules of its own; every result and every application a rule-attached loss reads goes through it. None when the run's items are the deduction's.

TYPE: ChartReveal | None

production_groups

production_groups() -> tuple[ProductionGroup, ...]

The system's productions, grouped by left-hand side.

RETURNS DESCRIPTION
tuple[ProductionGroup, ...]

One group per left-hand side, in order of its first production; rules first, then lexicon entries.

RAISES DESCRIPTION
ValueError

If the system has no fixed production groups: it was not compiled from a grammar, or a rule stands for one production per binding of a category variable.

Source code in src/quivers/stochastic/agenda.py
def production_groups(self) -> tuple[ProductionGroup, ...]:
    """The system's productions, grouped by left-hand side.

    Returns
    -------
    tuple[ProductionGroup, ...]
        One group per left-hand side, in order of its first
        production; rules first, then lexicon entries.

    Raises
    ------
    ValueError
        If the system has no fixed production groups: it was not
        compiled from a grammar, or a rule stands for one production
        per binding of a category variable.
    """
    if self.productions is None:
        raise ValueError(
            "the deduction has no fixed production groups: "
            + (self.production_refusal or "it was not compiled from a grammar")
        )
    return self.productions.groups

production_log_probs

production_log_probs(probabilities: Mapping[str, Tensor] | None = None) -> dict[str, dict[str, Tensor]]

Each production's log-probability, by left-hand side.

PARAMETER DESCRIPTION
probabilities

Production probabilities to read instead of the learned weights: one vector per left-hand side with several productions, keyed by its label, in production order.

TYPE: Mapping[str, Tensor] | None DEFAULT: None

RETURNS DESCRIPTION
dict[str, dict[str, Tensor]]

For each left-hand side, the log-probability of each of its productions (a rule by name, a lexicon entry by its word in double quotes); the probabilities of a group sum to one. The tensors carry gradients to the learned weights.

RAISES DESCRIPTION
ValueError

If the system does not normalize its weights per left-hand side (normalize=lhs), or the probabilities do not fit its groups.

Source code in src/quivers/stochastic/agenda.py
def production_log_probs(
    self, probabilities: Mapping[str, torch.Tensor] | None = None
) -> dict[str, dict[str, torch.Tensor]]:
    """Each production's log-probability, by left-hand side.

    Parameters
    ----------
    probabilities : Mapping[str, torch.Tensor] | None
        Production probabilities to read instead of the learned
        weights: one vector per left-hand side with several
        productions, keyed by its label, in production order.

    Returns
    -------
    dict[str, dict[str, torch.Tensor]]
        For each left-hand side, the log-probability of each of its
        productions (a rule by name, a lexicon entry by its word in
        double quotes); the probabilities of a group sum to one.
        The tensors carry gradients to the learned weights.

    Raises
    ------
    ValueError
        If the system does not normalize its weights per left-hand
        side (``normalize=lhs``), or the probabilities do not fit its
        groups.
    """
    return self._normalized().log_probs(probabilities)

run

run(input_value: Any, probabilities: Mapping[str, Tensor] | None = None) -> AgendaResult

Run the deduction system on an input value.

Injects the input's axioms, runs the agenda to fixed point, attaches the item encoder when one is set, and sums any rule-attached and chart-attached losses.

PARAMETER DESCRIPTION
input_value

The input the axiom injector reads.

TYPE: Any

probabilities

Production probabilities the run reads instead of the learned weights, for a grammar normalized per left-hand side: one vector per left-hand side with several productions, keyed by its label (production_groups).

TYPE: Mapping[str, Tensor] | None DEFAULT: None

RETURNS DESCRIPTION
AgendaResult

The completed chart, its goal items, and any attached loss.

RAISES DESCRIPTION
ValueError

If probabilities are given to a system that is not normalized per left-hand side, or do not fit its groups.

Source code in src/quivers/stochastic/agenda.py
def run(
    self,
    input_value: Any,
    probabilities: Mapping[str, torch.Tensor] | None = None,
) -> AgendaResult:
    """Run the deduction system on an input value.

    Injects the input's axioms, runs the agenda to fixed point,
    attaches the item encoder when one is set, and sums any
    rule-attached and chart-attached losses.

    Parameters
    ----------
    input_value : Any
        The input the axiom injector reads.
    probabilities : Mapping[str, torch.Tensor] | None
        Production probabilities the run reads instead of the
        learned weights, for a grammar normalized per left-hand
        side: one vector per left-hand side with several
        productions, keyed by its label (`production_groups`).

    Returns
    -------
    AgendaResult
        The completed chart, its goal items, and any attached
        loss.

    Raises
    ------
    ValueError
        If probabilities are given to a system that is not
        normalized per left-hand side, or do not fit its groups.
    """
    productions = self.productions
    # A normalized grammar computes its production table once per run,
    # so every axiom and firing reads one consistent table.
    if probabilities is not None:
        normalized = self._normalized()
        scope = normalized.active(normalized.table(probabilities))
    elif productions is not None and productions.normalize:
        scope = productions.active(productions.table())
    else:
        scope = nullcontext()
    with scope:
        return self._run(input_value)

__call__

__call__(input_value: Any, probabilities: Mapping[str, Tensor] | None = None) -> ChartView

Run the deduction and return a ChartView.

The chart's weights are differentiable tensors, and the view exposes weight, enumerate, derivations, and goal_weight methods for downstream programs.

PARAMETER DESCRIPTION
input_value

The input the axiom injector reads.

TYPE: Any

probabilities

Production probabilities to read instead of the learned weights, as for run.

TYPE: Mapping[str, Tensor] | None DEFAULT: None

RETURNS DESCRIPTION
ChartView

A view onto the completed chart.

RAISES DESCRIPTION
ValueError

If the probabilities do not fit the system, as for run.

Source code in src/quivers/stochastic/agenda.py
def __call__(
    self,
    input_value: Any,
    probabilities: Mapping[str, torch.Tensor] | None = None,
) -> ChartView:
    """Run the deduction and return a `ChartView`.

    The chart's weights are differentiable tensors, and the
    view exposes ``weight``, ``enumerate``, ``derivations``,
    and ``goal_weight`` methods for downstream programs.

    Parameters
    ----------
    input_value : Any
        The input the axiom injector reads.
    probabilities : Mapping[str, torch.Tensor] | None
        Production probabilities to read instead of the learned
        weights, as for `run`.

    Returns
    -------
    ChartView
        A view onto the completed chart.

    Raises
    ------
    ValueError
        If the probabilities do not fit the system, as for `run`.
    """
    return ChartView(self.run(input_value, probabilities))

corpus

corpus(sentences: Sequence[Any], probabilities: Mapping[str, Tensor] | None = None) -> CorpusChart

Run the deduction on every sentence of a corpus.

The sentences are independent draws, so the corpus's goal weight is the semiring product of the sentences' goal weights: under LogProb, the sum of their log inside weights.

PARAMETER DESCRIPTION
sentences

The inputs, one per sentence.

TYPE: Sequence[Any]

probabilities

Production probabilities every sentence reads instead of the learned weights, as for run.

TYPE: Mapping[str, Tensor] | None DEFAULT: None

RETURNS DESCRIPTION
CorpusChart

The sentences' charts and the corpus's goal weight.

RAISES DESCRIPTION
ValueError

If the corpus is empty, or the probabilities do not fit the system.

Source code in src/quivers/stochastic/agenda.py
def corpus(
    self,
    sentences: Sequence[Any],
    probabilities: Mapping[str, torch.Tensor] | None = None,
) -> CorpusChart:
    """Run the deduction on every sentence of a corpus.

    The sentences are independent draws, so the corpus's goal weight
    is the semiring product of the sentences' goal weights: under
    ``LogProb``, the sum of their log inside weights.

    Parameters
    ----------
    sentences : Sequence[Any]
        The inputs, one per sentence.
    probabilities : Mapping[str, torch.Tensor] | None
        Production probabilities every sentence reads instead of the
        learned weights, as for `run`.

    Returns
    -------
    CorpusChart
        The sentences' charts and the corpus's goal weight.

    Raises
    ------
    ValueError
        If the corpus is empty, or the probabilities do not fit the
        system.
    """
    if len(sentences) == 0:
        raise ValueError("a corpus has at least one sentence")
    views = tuple(self(sentence, probabilities) for sentence in sentences)
    return CorpusChart(views, self.semiring)

parameters

parameters(recurse: bool = True) -> Iterable[Parameter]

Yield every learnable parameter owned by this system.

Walks the optional _axiom_module (lexicon log-weights) and _rule_module (per-rule log-weights keyed by category bindings) submodules attached by the compiler. The recurse flag is the standard torch.nn.Module.parameters signature so user code can pass a DeductionSystem anywhere a nn.Module parameter iterator is expected.

PARAMETER DESCRIPTION
recurse

Whether to include parameters of nested submodules.

TYPE: bool DEFAULT: True

RETURNS DESCRIPTION
Iterable of torch.nn.Parameter

The learnable parameters.

Source code in src/quivers/stochastic/agenda.py
def parameters(self, recurse: bool = True) -> Iterable[torch.nn.Parameter]:
    """Yield every learnable parameter owned by this system.

    Walks the optional ``_axiom_module`` (lexicon log-weights)
    and ``_rule_module`` (per-rule log-weights keyed by category bindings)
    submodules attached by the compiler. The ``recurse`` flag
    is the standard `torch.nn.Module.parameters` signature
    so user code can pass a ``DeductionSystem`` anywhere a
    ``nn.Module`` parameter iterator is expected.

    Parameters
    ----------
    recurse : bool
        Whether to include parameters of nested submodules.

    Returns
    -------
    Iterable of torch.nn.Parameter
        The learnable parameters.
    """
    for attr in ("_axiom_module", "_rule_module"):
        mod = self.modules.get(attr)
        if mod is not None and hasattr(mod, "parameters"):
            yield from mod.parameters(recurse=recurse)

named_parameters

named_parameters(prefix: str = '', recurse: bool = True) -> Iterable[tuple[str, Parameter]]

Yield (name, parameter) pairs over all learnable parameters.

PARAMETER DESCRIPTION
prefix

A prefix prepended to every parameter name.

TYPE: str DEFAULT: ''

recurse

Whether to include parameters of nested submodules.

TYPE: bool DEFAULT: True

RETURNS DESCRIPTION
Iterable of (str, torch.nn.Parameter)

The named learnable parameters.

Source code in src/quivers/stochastic/agenda.py
def named_parameters(
    self,
    prefix: str = "",
    recurse: bool = True,
) -> Iterable[tuple[str, torch.nn.Parameter]]:
    """Yield ``(name, parameter)`` pairs over all learnable parameters.

    Parameters
    ----------
    prefix : str
        A prefix prepended to every parameter name.
    recurse : bool
        Whether to include parameters of nested submodules.

    Returns
    -------
    Iterable of (str, torch.nn.Parameter)
        The named learnable parameters.
    """
    for attr in ("_axiom_module", "_rule_module"):
        mod = self.modules.get(attr)
        if mod is not None and hasattr(mod, "named_parameters"):
            sub_prefix = f"{prefix}.{attr}" if prefix else attr
            for n, p in mod.named_parameters(
                prefix=sub_prefix,
                recurse=recurse,
            ):
                yield n, p

Axiom

Bases: Module

Initial items in a weighted deductive system.

An axiom creates the chart tensor and populates it with initial weights derived from the input. For span-based chart parsing, this is the lexical step.

forward abstractmethod

forward(input: Tensor, semiring: ChartSemiring) -> Tensor

Create and populate the initial chart.

PARAMETER DESCRIPTION
input

Raw input (e.g. token indices).

TYPE: Tensor

semiring

The scoring semiring.

TYPE: ChartSemiring

RETURNS DESCRIPTION
Tensor

The chart tensor with initial items filled in.

Source code in src/quivers/stochastic/deduction/primitives.py
@abstractmethod
def forward(
    self,
    input: torch.Tensor,
    semiring: ChartSemiring,
) -> torch.Tensor:
    """Create and populate the initial chart.

    Parameters
    ----------
    input : torch.Tensor
        Raw input (e.g. token indices).
    semiring : ChartSemiring
        The scoring semiring.

    Returns
    -------
    torch.Tensor
        The chart tensor with initial items filled in.
    """
    ...

Deduction

Bases: Module

A weighted inference step in a deductive system.

A deduction reads from the chart, applies inference rules, and writes updated weights. This is a morphism in the V-enriched category of chart states.

forward abstractmethod

forward(chart: Tensor, semiring: ChartSemiring, **context) -> Tensor

Apply this deduction step.

PARAMETER DESCRIPTION
chart

Current chart state.

TYPE: Tensor

semiring

The scoring semiring.

TYPE: ChartSemiring

**context

Schedule-provided context (e.g. span_length, span_start, split for CKY).

DEFAULT: {}

RETURNS DESCRIPTION
Tensor

Updated chart.

Source code in src/quivers/stochastic/deduction/primitives.py
@abstractmethod
def forward(
    self,
    chart: torch.Tensor,
    semiring: ChartSemiring,
    **context,
) -> torch.Tensor:
    """Apply this deduction step.

    Parameters
    ----------
    chart : torch.Tensor
        Current chart state.
    semiring : ChartSemiring
        The scoring semiring.
    **context
        Schedule-provided context (e.g. ``span_length``,
        ``span_start``, ``split`` for CKY).

    Returns
    -------
    torch.Tensor
        Updated chart.
    """
    ...

DeductiveSystem

DeductiveSystem(axiom: Axiom, deductions: list[Deduction], goal: Goal, schedule: Schedule, semiring: ChartSemiring | None = None)

Bases: Module

A weighted deductive system evaluated to fixpoint.

This is the abstract core of all parsing algorithms::

input -> axiom -> schedule(deductions) -> goal -> output

The system is an nn.Module whose learnable parameters come from the axiom (e.g. lexical weights) and deductions (e.g. rule weights).

PARAMETER DESCRIPTION
axiom

Initial item population.

TYPE: Axiom

deductions

Inference rules.

TYPE: list[Deduction]

goal

Result extraction.

TYPE: Goal

schedule

Evaluation strategy.

TYPE: Schedule

semiring

Scoring algebra (default: LOG_PROB).

TYPE: ChartSemiring DEFAULT: None

Source code in src/quivers/stochastic/deduction/primitives.py
def __init__(
    self,
    axiom: Axiom,
    deductions: list[Deduction],
    goal: Goal,
    schedule: Schedule,
    semiring: ChartSemiring | None = None,
) -> None:
    super().__init__()
    self.axiom = axiom
    self.deductions = nn.ModuleList(deductions)
    self.goal = goal
    self._schedule = schedule
    self._semiring = semiring or LOG_PROB

semiring property

semiring: ChartSemiring

The scoring algebra.

forward

forward(input: Tensor) -> Tensor

Run the deductive system on input.

PARAMETER DESCRIPTION
input

Raw input (e.g. token indices).

TYPE: Tensor

RETURNS DESCRIPTION
Tensor

Goal item weights.

Source code in src/quivers/stochastic/deduction/primitives.py
def forward(self, input: torch.Tensor) -> torch.Tensor:
    """Run the deductive system on input.

    Parameters
    ----------
    input : torch.Tensor
        Raw input (e.g. token indices).

    Returns
    -------
    torch.Tensor
        Goal item weights.
    """
    chart = self.axiom(input, self._semiring)
    chart = self._schedule.run(chart, self.deductions, self._semiring)
    return self.goal(chart)

Goal

Bases: Module

Extract the result from a completed chart.

A goal identifies which chart items constitute the answer and extracts their weights.

forward abstractmethod

forward(chart: Tensor) -> Tensor

Extract goal items from the chart.

PARAMETER DESCRIPTION
chart

Completed chart.

TYPE: Tensor

RETURNS DESCRIPTION
Tensor

Goal item weights.

Source code in src/quivers/stochastic/deduction/primitives.py
@abstractmethod
def forward(self, chart: torch.Tensor) -> torch.Tensor:
    """Extract goal items from the chart.

    Parameters
    ----------
    chart : torch.Tensor
        Completed chart.

    Returns
    -------
    torch.Tensor
        Goal item weights.
    """
    ...

Schedule

Bases: ABC

Evaluation strategy for a deductive system.

Different schedules compute the same fixpoint in different orders. CKY processes spans bottom-up; an agenda schedule uses a priority queue. The schedule is independent of the deduction rules.

run abstractmethod

run(chart: Tensor, deductions: ModuleList, semiring: ChartSemiring) -> Tensor

Execute deductions on chart to fixpoint.

PARAMETER DESCRIPTION
chart

Chart with axioms filled in.

TYPE: Tensor

deductions

The deduction steps.

TYPE: ModuleList

semiring

The scoring semiring.

TYPE: ChartSemiring

RETURNS DESCRIPTION
Tensor

Completed chart.

Source code in src/quivers/stochastic/deduction/primitives.py
@abstractmethod
def run(
    self,
    chart: torch.Tensor,
    deductions: nn.ModuleList,
    semiring: ChartSemiring,
) -> torch.Tensor:
    """Execute deductions on chart to fixpoint.

    Parameters
    ----------
    chart : torch.Tensor
        Chart with axioms filled in.
    deductions : nn.ModuleList
        The deduction steps.
    semiring : ChartSemiring
        The scoring semiring.

    Returns
    -------
    torch.Tensor
        Completed chart.
    """
    ...

adam_fit_deduction

adam_fit_deduction(ded: DeductionSystem, corpus: Sequence[Sequence[str]], *, steps: int = 300, lr: float = 0.05, prior_scale: float | None = None) -> list[float]

Maximise the corpus log-marginal under an optional Normal prior on the parameters.

PARAMETER DESCRIPTION
ded

Deduction whose _axiom_module and _rule_module parameters are optimised.

TYPE: DeductionSystem

corpus

Each sentence is a sequence of token strings the deduction's axiom injector accepts.

TYPE: sequence of sentences

steps

Adam steps.

TYPE: int DEFAULT: 300

lr

Adam learning rate.

TYPE: float DEFAULT: 0.05

prior_scale

If supplied, adds a Gaussian regulariser :math:\tfrac{1}{2\sigma^2}\lVert \mathbf{w} \rVert^2 to the loss (MAP). Defaults to None (MLE).

TYPE: float DEFAULT: None

RETURNS DESCRIPTION
list[float]

The loss trajectory; length == steps.

Source code in src/quivers/stochastic/deduction/fit.py
def adam_fit_deduction(
    ded: DeductionSystem,
    corpus: Sequence[Sequence[str]],
    *,
    steps: int = 300,
    lr: float = 5e-2,
    prior_scale: float | None = None,
) -> list[float]:
    """Maximise the corpus log-marginal under an optional Normal
    prior on the parameters.

    Parameters
    ----------
    ded : DeductionSystem
        Deduction whose ``_axiom_module`` and ``_rule_module``
        parameters are optimised.
    corpus : sequence of sentences
        Each sentence is a sequence of token strings the
        deduction's axiom injector accepts.
    steps : int
        Adam steps.
    lr : float
        Adam learning rate.
    prior_scale : float, optional
        If supplied, adds a Gaussian regulariser
        :math:`\\tfrac{1}{2\\sigma^2}\\lVert \\mathbf{w} \\rVert^2`
        to the loss (MAP). Defaults to ``None`` (MLE).

    Returns
    -------
    list[float]
        The loss trajectory; length == ``steps``.
    """
    materialise_parameters(ded, corpus)
    params = list(ded.parameters())
    if not params:
        return []
    optim = torch.optim.Adam(params, lr=lr)
    history: list[float] = []
    for _ in range(steps):
        optim.zero_grad()
        log_z = torch.zeros(())
        for sentence in corpus:
            log_z = log_z + ded(list(sentence)).goal_weight()
        loss = -log_z
        if prior_scale is not None:
            inv_var = 1.0 / (prior_scale**2)
            for p in params:
                loss = loss + 0.5 * inv_var * (p**2).sum()
        loss.backward()
        optim.step()
        history.append(float(loss.detach()))
    return history

sample_corpus

sample_corpus(ded: DeductionSystem, *, length: int, n_samples: int, seed: int | None = None) -> list[list[str]]

Sample n_samples yields of length length from the chart's length-conditional distribution under the deduction's current parameters.

PARAMETER DESCRIPTION
ded

The deduction with materialised parameters.

TYPE: DeductionSystem

length

Length of yields to enumerate.

TYPE: int

n_samples

Number of sentences to draw.

TYPE: int

seed

Seed for the multinomial draws.

TYPE: int DEFAULT: None

RETURNS DESCRIPTION
list of list of str

n_samples yields, each a list of length words, drawn with replacement in proportion to their goal weights.

RAISES DESCRIPTION
ValueError

If the deduction's vocabulary cannot be determined, or if no yield of length length parses.

Source code in src/quivers/stochastic/deduction/sample.py
def sample_corpus(
    ded: DeductionSystem,
    *,
    length: int,
    n_samples: int,
    seed: int | None = None,
) -> list[list[str]]:
    """Sample ``n_samples`` yields of length ``length`` from the
    chart's length-conditional distribution under the deduction's
    current parameters.

    Parameters
    ----------
    ded : DeductionSystem
        The deduction with materialised parameters.
    length : int
        Length of yields to enumerate.
    n_samples : int
        Number of sentences to draw.
    seed : int, optional
        Seed for the multinomial draws.

    Returns
    -------
    list of list of str
        ``n_samples`` yields, each a list of ``length`` words, drawn
        with replacement in proportion to their goal weights.

    Raises
    ------
    ValueError
        If the deduction's vocabulary cannot be determined, or if no
        yield of length ``length`` parses.
    """
    vocab = _vocabulary(ded)
    if not vocab:
        raise ValueError(
            "sample_corpus: cannot determine the deduction's "
            "vocabulary; set ``ded._vocabulary`` explicitly or "
            "call ``materialise_parameters`` first"
        )

    yields: list[list[str]] = []
    log_weights: list[torch.Tensor] = []
    for combo in itertools.product(vocab, repeat=length):
        chart = ded(list(combo))
        w = chart.goal_weight()
        if torch.isfinite(w):
            yields.append(list(combo))
            log_weights.append(w)
    if not yields:
        raise ValueError(
            f"sample_corpus: no yield of length {length} parses under "
            f"the deduction's current parameters"
        )
    logw = torch.stack([w.detach() for w in log_weights])
    probs = torch.softmax(logw, dim=0)
    gen = torch.Generator()
    if seed is not None:
        gen.manual_seed(seed)
    idxs = torch.multinomial(probs, n_samples, replacement=True, generator=gen)
    return [yields[int(i.item())] for i in idxs]

bayesian_lift_parameters

bayesian_lift_parameters(inner_model: Module, x: Tensor, observations: dict[str, Tensor], *, prior_scale: float = 1.0, site_prefix: str = 'theta', additional_latents: dict[str, tuple[int, ...]] | None = None, latent_placeholder_scale: float = 10.0) -> tuple[MonadicProgram, Tensor, dict[str, Tensor]]

Lift model parameters and selected latents into sample sites.

Each learnable parameter receives an independent :math:\mathcal{N}(0, \sigma_\theta^2) prior. Entries in additional_latents receive placeholder Normal priors. The score term subtracts those placeholder log densities from inner_model.log_joint, so they cancel pointwise; latent_placeholder_scale affects initialization and adaptation, not the represented density.

Learnable parameters must use unconstrained real coordinates. Use explicit priors when an independent zero-centered Normal does not represent the intended parameter prior.

PARAMETER DESCRIPTION
inner_model

Module exposing log_joint(x, observations) -> Tensor.

TYPE: Module

x

Input passed to inner_model.log_joint. Its B rows are B independent runs of the inner program, whose joints are summed; a program with a plate observes its rows through the plate and takes one row.

TYPE: Tensor

observations

Observations passed to inner_model.log_joint. Names listed in additional_latents must not also occur here.

TYPE: dict[str, Tensor]

prior_scale

Standard deviation of each parameter prior.

TYPE: float DEFAULT: 1.0

site_prefix

Prefix for parameter sample-site names.

TYPE: str DEFAULT: 'theta'

additional_latents

Latent names and tensor shapes, excluding any batch dimension.

TYPE: dict[str, tuple[int, ...]] | None DEFAULT: None

latent_placeholder_scale

Standard deviation of each placeholder latent prior.

TYPE: float DEFAULT: 10.0

RETURNS DESCRIPTION
tuple[MonadicProgram, Tensor, dict[str, Tensor]]

Lifted program, placeholder input, and empty observation mapping.

RAISES DESCRIPTION
ValueError

If inner_model has no log_joint, a name is both observed and an additional latent, there is nothing to lift, or a program with a plate is given more than one input row.

Source code in src/quivers/inference/lifts.py
def bayesian_lift_parameters(
    inner_model: nn.Module,
    x: torch.Tensor,
    observations: dict[str, torch.Tensor],
    *,
    prior_scale: float = 1.0,
    site_prefix: str = "theta",
    additional_latents: dict[str, tuple[int, ...]] | None = None,
    latent_placeholder_scale: float = 10.0,
) -> tuple[MonadicProgram, torch.Tensor, dict[str, torch.Tensor]]:
    """Lift model parameters and selected latents into sample sites.

    Each learnable parameter receives an independent
    :math:`\\mathcal{N}(0, \\sigma_\\theta^2)` prior. Entries in
    ``additional_latents`` receive placeholder Normal priors. The score term
    subtracts those placeholder log densities from ``inner_model.log_joint``,
    so they cancel pointwise; ``latent_placeholder_scale`` affects
    initialization and adaptation, not the represented density.

    Learnable parameters must use unconstrained real coordinates. Use explicit
    priors when an independent zero-centered Normal does not represent the
    intended parameter prior.

    Parameters
    ----------
    inner_model : nn.Module
        Module exposing ``log_joint(x, observations) -> Tensor``.
    x : torch.Tensor
        Input passed to ``inner_model.log_joint``. Its ``B`` rows are
        ``B`` independent runs of the inner program, whose joints are
        summed; a program with a plate observes its rows through the
        plate and takes one row.
    observations : dict[str, torch.Tensor]
        Observations passed to ``inner_model.log_joint``. Names listed in
        ``additional_latents`` must not also occur here.
    prior_scale : float
        Standard deviation of each parameter prior.
    site_prefix : str
        Prefix for parameter sample-site names.
    additional_latents : dict[str, tuple[int, ...]] | None
        Latent names and tensor shapes, excluding any batch dimension.
    latent_placeholder_scale : float
        Standard deviation of each placeholder latent prior.

    Returns
    -------
    tuple[MonadicProgram, torch.Tensor, dict[str, torch.Tensor]]
        Lifted program, placeholder input, and empty observation mapping.

    Raises
    ------
    ValueError
        If ``inner_model`` has no ``log_joint``, a name is both observed
        and an additional latent, there is nothing to lift, or a program
        with a plate is given more than one input row.
    """
    if not hasattr(inner_model, "log_joint"):
        raise ValueError(
            "bayesian_lift_parameters: inner_model must expose "
            "``log_joint(x, observations)``"
        )
    if additional_latents and any(k in observations for k in additional_latents):
        bad = sorted(k for k in additional_latents if k in observations)
        raise ValueError(
            "bayesian_lift_parameters: keys "
            f"{bad!r} appear in both ``observations`` and "
            "``additional_latents``; a latent cannot be both "
            "observed and sampled"
        )

    if isinstance(inner_model, MonadicProgram) and _has_plates(inner_model):
        _require_one_run(x, "bayesian_lift_parameters")
    inner_log_joint = inner_model.log_joint

    params = list(inner_model.named_parameters())
    if not params and not additional_latents:
        raise ValueError(
            "bayesian_lift_parameters: inner_model has no parameters "
            "and no additional_latents were declared"
        )

    safe_paths = [n.replace(".", "_") for n, _ in params]
    param_sites = [f"{site_prefix}__{p}" for p in safe_paths]
    locator, _paths, _ = _build_locator(inner_model)
    flat_dims = [int(p.numel()) for _, p in params]
    param_shapes = [tuple(p.shape) for _, p in params]
    param_paths = [n for n, _ in params]

    latent_names: list[str] = []
    latent_shapes: list[tuple[int, ...]] = []
    latent_flat_dims: list[int] = []
    latent_sites: list[str] = []
    if additional_latents:
        for name, shape in additional_latents.items():
            latent_names.append(name)
            latent_shapes.append(tuple(int(s) for s in shape))
            flat = 1
            for s in shape:
                flat *= int(s)
            latent_flat_dims.append(flat)
            latent_sites.append(f"latent__{name}")

    steps: list[Step] = []
    for site, dim in zip(param_sites, flat_dims):
        steps.append(
            Draw(
                names=(site,),
                morphism=_make_normal_prior_morphism(prior_scale, dim=dim),
            )
        )
    for site, dim in zip(latent_sites, latent_flat_dims):
        steps.append(
            Draw(
                names=(site,),
                morphism=_make_normal_prior_morphism(latent_placeholder_scale, dim=dim),
            )
        )

    placeholder_scale_f = float(latent_placeholder_scale)

    def _score_fn(
        env: dict[str, torch.Tensor],
        _param_paths: list[str] = list(param_paths),
        _param_shapes: list[tuple[int, ...]] = list(param_shapes),
        _param_sites: list[str] = list(param_sites),
        _latent_names: list[str] = list(latent_names),
        _latent_shapes: list[tuple[int, ...]] = list(latent_shapes),
        _latent_sites: list[str] = list(latent_sites),
        _locator: Callable[[str], tuple[nn.Module, str]] = locator,
        _inner_x: torch.Tensor = x,
        _inner_obs: dict[str, torch.Tensor] = dict(observations),
        _placeholder_scale: float = placeholder_scale_f,
    ) -> torch.Tensor:
        # The score runs once per run of the lifted program, on that
        # run's draws of the parameters and latents.
        overrides: dict[str, torch.Tensor] = {}
        for path, shape, name in zip(_param_paths, _param_shapes, _param_sites):
            v = env[name]
            overrides[path] = v.reshape(shape) if shape else v.reshape(())
        latent_dict: dict[str, torch.Tensor] = {}
        placeholder_log_prior = torch.zeros((), dtype=torch.get_default_dtype())
        for name, shape, site in zip(_latent_names, _latent_shapes, _latent_sites):
            flat = env[site]
            latent_dict[name] = flat.reshape(shape) if shape else flat.reshape(())
            placeholder_log_prior = (
                placeholder_log_prior
                + D.Normal(
                    torch.zeros_like(flat),
                    torch.full_like(flat, _placeholder_scale),
                )
                .log_prob(flat)
                .sum()
            )
        with _swap_named_parameters(_locator, overrides):
            merged_obs = {**_inner_obs, **latent_dict}
            # The inner joint of each of the inner input's runs, summed:
            # the inner program's runs are independent replications.
            ll = inner_log_joint(_inner_x, merged_obs).sum()
        # Cancel the placeholder priors on lifted latents so the net
        # log-density is exactly ``log p(theta) + log p_inner(z, y | x,
        # theta)``.
        return ll - placeholder_log_prior

    steps.append(
        Score(
            name="log_lik",
            score=mappable(
                reading(_score_fn, [*param_sites, *latent_sites]),
                joint_obstacle(inner_model),
            ),
        )
    )
    lifted = MonadicProgram(
        domain=Unit,
        codomain=Unit,
        steps=steps,
        return_vars=("log_lik",),
    )
    return lifted, torch.zeros(1, 1), {}

nuts_program_from_deduction

nuts_program_from_deduction(ded: DeductionSystem, corpus: Sequence[Sequence[str]], *, prior_scale: float = 1.0, site_prefix: str = 'log_w') -> tuple[MonadicProgram, Tensor, dict[str, Tensor]]

Lift a deduction system's learnable parameters to a MonadicProgram suitable for NUTS / SVI.

The returned program has one torch.distributions.Normal sample site per learnable parameter (lexicon entries and rule bindings alike) plus one score step that substitutes the sampled values into the deduction's parameter slots and adds :math:\sum_n \log Z(s_n; \mathbf{w}) to the joint.

PARAMETER DESCRIPTION
ded

Deduction whose parameters are lifted.

TYPE: DeductionSystem

corpus

Corpus the score step closes over.

TYPE: sequence of sentences

prior_scale

Standard deviation of the Normal prior on each parameter.

TYPE: float DEFAULT: 1.0

site_prefix

Stem of each sample-site's name (the parameter's path is appended for round-trip mapping).

TYPE: str DEFAULT: 'log_w'

RETURNS DESCRIPTION
(model, x, observations)

The lifted program plus a (1, 1) placeholder input and an empty observation dict, ready to feed to quivers.inference.MCMC .run.

RAISES DESCRIPTION
ValueError

If the deduction has no learnable parameters.

Source code in src/quivers/stochastic/deduction/bayes.py
def nuts_program_from_deduction(
    ded: DeductionSystem,
    corpus: Sequence[Sequence[str]],
    *,
    prior_scale: float = 1.0,
    site_prefix: str = "log_w",
) -> tuple[MonadicProgram, torch.Tensor, dict[str, torch.Tensor]]:
    """Lift a deduction system's learnable parameters to a
    `MonadicProgram` suitable for NUTS / SVI.

    The returned program has one
    `torch.distributions.Normal` sample site per learnable
    parameter (lexicon entries and rule bindings alike) plus one
    score step that substitutes the sampled values into the
    deduction's parameter slots and adds
    :math:`\\sum_n \\log Z(s_n; \\mathbf{w})` to the joint.

    Parameters
    ----------
    ded : DeductionSystem
        Deduction whose parameters are lifted.
    corpus : sequence of sentences
        Corpus the score step closes over.
    prior_scale : float
        Standard deviation of the Normal prior on each parameter.
    site_prefix : str
        Stem of each sample-site's name (the parameter's path is
        appended for round-trip mapping).

    Returns
    -------
    (model, x, observations)
        The lifted program plus a ``(1, 1)`` placeholder input and
        an empty observation dict, ready to feed to
        [`quivers.inference.MCMC`][quivers.inference.MCMC] ``.run``.

    Raises
    ------
    ValueError
        If the deduction has no learnable parameters.
    """
    materialise_parameters(ded, corpus)
    locator, paths, _ = build_locator(ded)
    if not paths:
        raise ValueError(
            "nuts_program_from_deduction: the deduction has no "
            "learnable parameters (neither lexicon nor rules are "
            "marked #[learnable])"
        )
    site_names: list[str] = []
    for path in paths:
        safe = path.replace("/", "__").replace(".", "_")
        site_names.append(f"{site_prefix}__{safe}")

    prior_morph = _make_normal_prior_morphism(prior_scale)
    steps: list[Step] = [
        Draw(names=(site,), morphism=prior_morph) for site in site_names
    ]

    def _score_fn(
        env: dict[str, torch.Tensor],
        _ded: DeductionSystem = ded,
        _corpus: list[list[str]] = [list(s) for s in corpus],
        _site_names: list[str] = list(site_names),
        _paths: list[str] = list(paths),
        _locator: Callable[[str], tuple[torch.nn.Module, str]] = locator,
    ) -> torch.Tensor:
        # A score step reads one run's values, each site a single draw.
        overrides = {
            path: env[name].reshape(()) for path, name in zip(_paths, _site_names)
        }
        with _swap_named_parameters(_locator, overrides):
            log_z = torch.zeros(())
            for sentence in _corpus:
                log_z = log_z + _ded(sentence).goal_weight()
        return log_z.reshape(1)

    obstacle = (
        None
        if ded.mappable
        else f"deduction {ded.name!r} does not declare its chart mappable"
    )
    steps.append(
        Score(
            name="log_Z",
            score=mappable(reading(_score_fn, site_names), obstacle),
        )
    )
    model = MonadicProgram(
        domain=Unit,
        codomain=Unit,
        steps=steps,
        return_vars=("log_Z",),
    )
    return model, torch.zeros(1, 1), {}

__getattr__

__getattr__(name: str) -> FunctionType

Load inference operations when their public names are requested.

The deduction primitives are independent of inference. Their package can initialize while the compiler is importing chart parsers.

PARAMETER DESCRIPTION
name

The requested public attribute.

TYPE: str

RETURNS DESCRIPTION
FunctionType

The operation from its defining module.

RAISES DESCRIPTION
AttributeError

If the name is not a deferred inference operation.

Source code in src/quivers/stochastic/deduction/__init__.py
def __getattr__(name: str) -> FunctionType:
    """Load inference operations when their public names are requested.

    The deduction primitives are independent of inference. Their package
    can initialize while the compiler is importing chart parsers.

    Parameters
    ----------
    name : str
        The requested public attribute.

    Returns
    -------
    FunctionType
        The operation from its defining module.

    Raises
    ------
    AttributeError
        If the name is not a deferred inference operation.
    """
    module_name = _LAZY_EXPORTS.get(name)
    if module_name is None:
        raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
    value: FunctionType = getattr(importlib.import_module(module_name), name)
    globals()[name] = value
    return value

Cited sources

Stuart M. Shieber, Yves Schabes, and Fernando C. N. Pereira. Principles and Implementation of Deductive Parsing. The Journal of Logic Programming, 24(1–2):3–36, 1995. URL: https://www.sciencedirect.com/science/article/pii/074310669500035I, doi:10.1016/0743-1066(95)00035-I.

Joshua Goodman. Semiring Parsing. Computational Linguistics, 25(4):573–606, 1999. URL: https://aclanthology.org/J99-4004/.