Decoders¶
Decoder is a torch.nn.Module that realizes the Kleisli coalgebra
Vec_D → Kern(T_Σ). Given an input vector, it defines a distribution
over terms of a signature through two operations:
sample(vec, ctx, sort)draws a singleTerm.log_prob(term, vec, ctx, sort)scores an observed term under the same distribution.
Corecursion over a signature Σ proceeds as follows:
- At each sort position, the decoder produces logits over its
choice set, every constructor and binder whose codomain is
that sort, plus the built-in
BoundVarwhenever the context contains at least one in-scope variable of that sort. - For the chosen op, the parent vector is split into per-child
sub-vectors by the per-(sort, arity)
factorfunction, and the decoder recurses on each child. - Data-sorted children are sampled from a closed vocabulary via
the per-sort
primitivehead; index-sorted children are sampled viabinder_selectover the in-scope variables. - Binder ops extend Γ before recursing on their scoped arguments, as in the encoder.
Construction sets a recursion-depth bound. At that bound, the decoder restricts the choice set to terminating ops. If a sort has no such op, the decoder raises an error that identifies the sort.
decoder
¶
Decoder runtime: Kleisli coalgebras Vec_D -> Kern(T_Σ).
A Decoder is a torch.nn.Module exposing two operations:
sample: draws a singleTermfrom the distribution induced by an input vector.log_prob: scores an observed term under the same distribution.
The corecursion structure over a signature Σ is:
- At each sort position, the decoder produces logits over its
choice set: every constructor and binder whose codomain is
that sort, plus the built-in
BOUND_VAR_OPwhenever the context Γ contains at least one in-scope variable of that sort. - For the chosen op, the parent vector is split into per-child
sub-vectors by the per-(sort, arity)
factorfunction, and the decoder recurses on each child under the same canonical form used by the encoder. - Data-sorted children are sampled from a closed vocabulary via
the per-sort
primitivehead; index-sorted children are sampled viabinder_selectover the in-scope variables. - Binder ops extend Γ before recursing on their scoped arguments, exactly mirroring the encoder.
Termination is depth-bounded at construction. At the budget limit the choice set is restricted to recursion-terminating ops (every child sort is data / index, never object); if no such op exists at a sort, the decoder raises.
No silent type coercion or sentinel value is ever emitted: an observed term whose shape doesn't match the canonical form raises.
Decoder
¶
Decoder(name: str, signature: Signature, sort_dims: dict[str, int], depth: int, structure_fns: dict[str, Callable[[Tensor], Tensor]], primitive_fns: dict[str, Callable[[Tensor], Tensor]], factor_fns: dict[str, dict[int, Callable[[Tensor], tuple[Tensor, ...]]]], binder_select_fn: Callable[[Tensor, list[Tensor]], Tensor], data_vocab: dict[str, list[DataLeaf]], modules_owned: list[Module] | None = None)
Bases: Module
A Kleisli coalgebraic decoder over a signature.
| PARAMETER | DESCRIPTION |
|---|---|
name
|
Identifier used in diagnostics.
TYPE:
|
signature
|
The Σ whose terms this decoder generates.
TYPE:
|
sort_dims
|
Per-sort embedding dimension.
TYPE:
|
depth
|
Hard upper bound on recursion depth. Sampling beyond this depth is restricted to recursion-terminating ops at each sort.
TYPE:
|
structure_fns
|
Per-sort logit producers
TYPE:
|
primitive_fns
|
Per-data-sort logit producers over the closed token vocab.
TYPE:
|
factor_fns
|
Per-sort, per-arity child-vector projections
TYPE:
|
binder_select_fn
|
TYPE:
|
data_vocab
|
Per-data-sort closed vocabulary aligned with the column
order of the corresponding
TYPE:
|
modules_owned
|
Modules holding the parameters of the logit producers and projections, registered as submodules so their parameters train.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If |
Source code in src/quivers/structural/decoder.py
sample
¶
Draw one term from the decoder's distribution.
| PARAMETER | DESCRIPTION |
|---|---|
vec
|
The embedding to decode from.
TYPE:
|
ctx
|
The scope of in-scope bound variables;
TYPE:
|
sort
|
The sort of the term to draw; the first declared object sort when omitted.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Term
|
The sampled term. |
| RAISES | DESCRIPTION |
|---|---|
RuntimeError
|
If no sort is given and the signature has no object sort, or the decoder lacks a function, vocabulary, or candidate it needs at some position. |
Source code in src/quivers/structural/decoder.py
log_prob
¶
Score an observed term under the decoder's distribution.
| PARAMETER | DESCRIPTION |
|---|---|
term
|
The observed term.
TYPE:
|
vec
|
The embedding the term is decoded from.
TYPE:
|
ctx
|
The scope of in-scope bound variables;
TYPE:
|
sort
|
The sort of
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
The scalar log-probability of |
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
If |
RuntimeError
|
If the term's shape is outside the decoder's support, for instance an operation not in the choice set of its sort or a data value outside the closed vocabulary. |