Shapes¶
Ready-made signatures, encoders, and decoders for the three principal
compressible shapes: sequences, trees, and graphs. Each factory builds
on the generic Encoder and Decoder runtimes, so the objects it
returns can be inspected and composed like any hand-built one.
seq
¶
Sequence-shaped signatures and canonical encoders / decoders.
Surface algebra::
Seq[A] = Nil | Cons(A, Seq[A])
The principal shape behind autoregressive LMs (the "compress a
prefix to a vector, decode next-token from the vector" loop). All
encoders and decoders below are realised on top of the generic
quivers.structural.Encoder and
quivers.structural.Decoder runtimes, with sequence-
specific per-op functions.
seq_signature
¶
seq_signature(name: str = 'Seq', dim: int = 64) -> Signature
Return a cons-list signature with a data element sort.
The object sort Seq has the constructors Nil and Cons,
the latter over an element of data sort A and a tail.
| PARAMETER | DESCRIPTION |
|---|---|
name
|
The signature's name.
TYPE:
|
dim
|
The embedding dimension of every sort.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Signature
|
The sequence signature. |
Source code in src/quivers/structural/shapes/seq.py
rnn_encoder
¶
A GRU-cell encoder: Cons(head, tail) updates a hidden
state from the tail's compressed vector and the head's element
embedding.
| PARAMETER | DESCRIPTION |
|---|---|
sig
|
A sequence signature with object sort
TYPE:
|
dim
|
The embedding dimension of both sorts.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Encoder
|
The sequence encoder. |
Source code in src/quivers/structural/shapes/seq.py
transformer_encoder
¶
A transformer-style encoder: each Cons step combines
the head's element embedding with the tail's running compressed
vector through a learned MLP over the concatenation.
| PARAMETER | DESCRIPTION |
|---|---|
sig
|
A sequence signature with object sort
TYPE:
|
dim
|
The embedding dimension of both sorts.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Encoder
|
The sequence encoder. |
Source code in src/quivers/structural/shapes/seq.py
bow_encoder
¶
An order-independent sum-of-element-embeddings encoder.
Each Cons(head, tail) step adds the head's element embedding
to the running tail embedding; Nil is a learnable zero.
Equivalent to a bag-of-tokens fixed-length representation.
| PARAMETER | DESCRIPTION |
|---|---|
sig
|
A sequence signature with object sort
TYPE:
|
dim
|
The embedding dimension of both sorts.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Encoder
|
The sequence encoder. |
Source code in src/quivers/structural/shapes/seq.py
ar_decoder
¶
ar_decoder(sig: Signature | None = None, dim: int = 64, vocab: list[DataLeaf] | None = None, depth: int = 64) -> Decoder
An autoregressive decoder over a sequence signature.
vocab is the closed token set the primitive head selects
from. Required; the runtime raises on an empty vocabulary.
| PARAMETER | DESCRIPTION |
|---|---|
sig
|
A sequence signature with object sort
TYPE:
|
dim
|
The embedding dimension of both sorts.
TYPE:
|
vocab
|
The closed set of element values the decoder emits.
TYPE:
|
depth
|
The recursion-depth bound, which caps the sampled length.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Decoder
|
The sequence decoder. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If |
Source code in src/quivers/structural/shapes/seq.py
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 | |
list_to_term
¶
Convert a Python list to a right-nested Cons(..., Nil) term.
| PARAMETER | DESCRIPTION |
|---|---|
elements
|
The sequence elements, in order.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Term
|
The cons-list term over |
Source code in src/quivers/structural/shapes/seq.py
term_to_list
¶
Inverse of list_to_term.
| PARAMETER | DESCRIPTION |
|---|---|
term
|
A cons-list term.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[DataLeaf]
|
The heads of the |
Source code in src/quivers/structural/shapes/seq.py
tree
¶
Tree-shaped signatures and canonical encoders / decoders.
Surface algebra::
Tree[L, B] = Leaf(L) | Node(B, Tree, Tree)
tree_signature
¶
tree_signature(name: str = 'Tree', dim: int = 64) -> Signature
Return a binary-tree signature with leaf and branch data sorts.
The object sort Tree has two constructors: Leaf over the
data sort L and Node over a branch label of data sort
B and two subtrees.
| PARAMETER | DESCRIPTION |
|---|---|
name
|
The signature's name.
TYPE:
|
dim
|
The embedding dimension of every sort.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Signature
|
The tree signature. |
Source code in src/quivers/structural/shapes/tree.py
tree_lstm_encoder
¶
A binary-tree LSTM encoder (Tai et al. 2015 child-sum variant).
A Leaf projects its token embedding. A Node gates between
an MLP over its label and both children and the mean of the two
children's embeddings.
| PARAMETER | DESCRIPTION |
|---|---|
sig
|
A tree signature with object sort
TYPE:
|
dim
|
The embedding dimension of every sort.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Encoder
|
The tree encoder. |
Source code in src/quivers/structural/shapes/tree.py
tree_decoder
¶
tree_decoder(sig: Signature | None = None, dim: int = 64, leaf_vocab: list[DataLeaf] | None = None, label_vocab: list[DataLeaf] | None = None, depth: int = 12) -> Decoder
Top-down structural decoder over a tree signature.
| PARAMETER | DESCRIPTION |
|---|---|
sig
|
A tree signature with object sort
TYPE:
|
dim
|
The embedding dimension of every sort.
TYPE:
|
leaf_vocab
|
The closed set of leaf values the decoder emits.
TYPE:
|
label_vocab
|
The closed set of branch labels the decoder emits.
TYPE:
|
depth
|
The recursion-depth bound on sampled trees.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Decoder
|
The tree decoder. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If |
Source code in src/quivers/structural/shapes/tree.py
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 | |
graph
¶
Graph-shaped signatures and canonical encoder.
A graph signature is declared with vertex kinds and edge kinds; the
canonical GNN encoder runs iterations rounds of
message passing with per-edge-kind message functions and
per-vertex-kind update functions.
graph_signature
¶
graph_signature(name: str, vertex_kinds: dict[str, int], edge_kinds: dict[str, tuple[str, str, bool]]) -> Signature
Construct a graph signature.
| PARAMETER | DESCRIPTION |
|---|---|
name
|
The signature's name.
TYPE:
|
vertex_kinds
|
Each vertex kind's embedding dimension, keyed by kind name. Every vertex kind is data-kinded.
TYPE:
|
edge_kinds
|
Each edge kind's
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Signature
|
The graph signature. |
Source code in src/quivers/structural/shapes/graph.py
gnn_encoder
¶
A GNN encoder: per-edge-kind message MLP, per-vertex-kind GRU update, mean / sum / max readout.
Each vertex kind embeds its payload through a lazily grown table of learned vectors keyed by the payload's string form.
| PARAMETER | DESCRIPTION |
|---|---|
sig
|
A graph signature, as built by
TYPE:
|
iterations
|
The number of message-passing rounds.
TYPE:
|
dim
|
The embedding dimension of a vertex kind that declares none.
TYPE:
|
readout
|
How the final vertex embeddings are reduced:
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Encoder
|
The graph encoder; call its |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
Raised by the encoder's readout when |
Source code in src/quivers/structural/shapes/graph.py
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | |