Decoders¶
A decoder maps a vector to
a distribution over terms of a signature. Decoder is a torch.nn.Module that implements a
Kleisli arrow \(\mathrm{Vec}_D \to \mathcal{G}(T_\Sigma)\)
of the Giry monad \(\mathcal{G}\), from vectors of
dimension \(D\) to distributions over the terms \(T_\Sigma\) of the signature \(\Sigma\). It chooses a root
constructor, generates its fields, and continues through the children. It has two operations, and
reconstruction training
raises the score of the original term:
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 operation, the decoder's
factorhead for that sort and arity splits the parent vector into one vector per child, and the decoder recurses on each child. - Children of a data sort are sampled from a closed vocabulary by that sort's
primitivehead, and children of an index sort bybinder_selectover the variables in scope. - Binder ops extend Γ before recursing on their scoped arguments, as in the encoder.
Construction sets a bound on the depth of recursion. 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: Markov kernels from code vectors to terms.
A Decoder is a Markov kernel R^D -> T_Σ from code vectors to the
terms of a signature Σ, that is, a morphism R^D -> T_Σ of the Kleisli
category of the distribution monad. It 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 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. - An argument that binds variables extends Γ by them before the decoder recurses on it, 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
bound_support
¶
bound_support(limit: int) -> tuple[Term, ...]
Record the complete support at the principal sort, up to a bound.
| PARAMETER | DESCRIPTION |
|---|---|
limit
|
The most terms the support may have.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple of Term
|
The support, also kept as |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the signature binds variables or |
Source code in src/quivers/structural/decoder.py
complete_support
¶
complete_support(sort: str, limit: int) -> tuple[Term, ...]
Every closed term the decoder can produce at a sort, counted first.
Every constructor the decoder can choose at a reachable sort and
depth must be productive. Data leaves range over their decoder
vocabulary, which must be finite, nonempty and free of duplicates.
The support is counted up to limit before any term is made.
| PARAMETER | DESCRIPTION |
|---|---|
sort
|
The root object sort.
TYPE:
|
limit
|
The most terms to materialize; positive.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple of Term
|
The full root support, in constructor and vocabulary order. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the limit is not positive, a reachable branch can get stuck, a reachable sort needs a variable, or the support is larger than the limit. |
Source code in src/quivers/structural/decoder.py
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 | |
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. |
Source code in src/quivers/structural/decoder.py
decoder_draw_message
¶
The refusal of a program sample or observe from a decoder.
A decoder's values are structured terms, not the tensors a program's sites hold, so no program step draws from one; a loss scores a term under it instead.
| PARAMETER | DESCRIPTION |
|---|---|
name
|
The decoder's name.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
The message. |