codebook_prior
codebook_prior
¶
Learned prior over discrete codebook indices for VQ latent variable models.
Predicts a categorical distribution over codebook entries conditioned on observations. At inference, samples codebook indices and returns the corresponding quantized embedding for the decoder. Shares the codebook with the VQ posterior encoder — the codebook is set after construction via wire_posterior().
CodebookPrior
¶
CodebookPrior(latent_dimension, num_codes, num_residual_layers, embedding_dimension, observation_horizon, device, number_of_heads=4, feedforward_dimension=128, number_of_encoder_layers=1, activation=value, dropout_rate=0.0, attention_dropout=0.0, normalization_type=value, attention_type=value, positional_encoding_type=None, exclude_keys=None, temperature=1.0)
Bases: PriorLatentEncoder
Learned categorical prior over VQ codebook indices.
Encodes observations via a transformer, then predicts logits over the K codebook entries for each residual VQ layer. At inference, samples indices from the predicted categorical and decodes them to a quantized embedding via the shared codebook.
The codebook is owned by the VQ posterior encoder and shared via wire_posterior(). This must be called before the first forward pass.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
latent_dimension
|
int
|
Dimension of each codebook vector. Must match the posterior encoder's latent dimension. |
required |
num_codes
|
int
|
Number of codebook entries per layer (K). |
required |
num_residual_layers
|
int
|
Number of residual VQ layers. |
required |
embedding_dimension
|
int
|
Transformer hidden dimension. |
required |
observation_horizon
|
int
|
Number of observation timesteps. |
required |
device
|
str
|
Device string. |
required |
number_of_heads
|
int
|
Number of attention heads. |
4
|
feedforward_dimension
|
int
|
Feedforward network dimension. |
128
|
number_of_encoder_layers
|
int
|
Number of transformer encoder layers. |
1
|
activation
|
str
|
Activation function name. |
value
|
dropout_rate
|
float
|
Dropout probability. |
0.0
|
attention_type
|
str
|
Attention mechanism type (use AttentionType enum values). |
value
|
exclude_keys
|
list[str] | None
|
Observation keys to exclude from encoding. |
None
|
temperature
|
float
|
Softmax temperature for sampling. Lower values produce sharper categorical distributions. |
1.0
|
Source code in src/versatil/models/decoding/latent/prior/codebook_prior.py
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 | |
residual_vq
property
¶
Return the posterior-owned VQ module without registering it as a child.
__getstate__
¶
Return copy-safe state without the posterior VQ reference.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Module state with runtime wiring cleared. The owning |
dict[str, Any]
|
VariationalAlgorithm reconnects the copied prior to the copied |
dict[str, Any]
|
posterior after deepcopy or unpickling. |
Source code in src/versatil/models/decoding/latent/prior/codebook_prior.py
get_auxiliary_output_keys
¶
Codebook prior outputs quantized latent, sampled indices, and logits.
Source code in src/versatil/models/decoding/latent/prior/codebook_prior.py
wire_posterior
¶
Wire shared codebook from the VQ posterior encoder.
Extracts the ResidualVQ reference needed to decode sampled indices into quantized embeddings at inference.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
posterior
|
VQPosteriorEncoder
|
VQ posterior encoder with a residual_vq attribute. |
required |
Raises:
| Type | Description |
|---|---|
AttributeError
|
If the posterior does not expose ResidualVQ state. |
ValueError
|
If the posterior's VQ configuration does not match this prior's configuration. |
Source code in src/versatil/models/decoding/latent/prior/codebook_prior.py
forward
¶
Predict codebook index distribution from observations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target_latents
|
Tensor | None
|
Unused (batch size derived from observations). |
required |
observations
|
dict[str, Tensor]
|
Dictionary of observation features. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Tensor]
|
Dictionary containing: - LatentKey.PRIOR_LATENT: Quantized embedding from sampled indices, shape (B, code_dim). - LatentKey.VQ_PRIOR_INDICES: List of sampled per-layer indices, each shape (B,). Emitted under a distinct key from the posterior's VQ_INDICES to avoid collision. - LatentKey.PRIOR_CODE_LOGITS: List of per-layer logits over the K codebook entries, each shape (B, K). Consumed by VQPriorCrossEntropyLoss. |
Source code in src/versatil/models/decoding/latent/prior/codebook_prior.py
224 225 226 227 228 229 230 231 232 233 234 235 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 | |
sample_prior
¶
Sample latent from learned categorical prior p(k|s).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch_size
|
int
|
Number of samples. |
required |
observations
|
dict[str, Tensor] | None
|
Observation features for conditioning. |
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Quantized latent embedding, shape (batch_size, code_dim). |