SUQ Documentation
Base Functions
Classes
SUQ_Base
Bases: Module
Base class for SUQ models.
Provides core functionality for:
- Managing likelihood type (regression or classification)
- Probit-based approximation for classification
- NLPD-based fitting of the scale factor
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
likelihood
|
str
|
Either |
required |
scale_init
|
float
|
Initial value for the scale factor parameter. |
required |
Source code in suq/base_suq.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 | |
Functions
probit_approximation(out_mean, out_var)
Applies a probit approximation to compute class probabilities from the latent Gaussian distribution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
out_mean
|
Tensor
|
Latent function mean, shape [B, num_classes] |
required |
out_var
|
Tensor
|
Latent function variance, shape [B, num_classes] or [B, num_classes, num_classes] |
required |
Returns:
| Name | Type | Description |
|---|---|---|
posterior_predict_mean |
Tensor
|
Predicted class probabilities, shape [B, num_classes] |
Source code in suq/base_suq.py
fit_scale_factor(data_loader, n_epoches, lr, speedup=True, verbose=False)
Fits the scale factor for predictive variance using negative log predictive density (NLPD).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_loader
|
DataLoader
|
Dataloader containing (input, target) pairs |
required |
n_epoches
|
int
|
Number of epochs for optimization |
required |
lr
|
float
|
Learning rate for scale optimizer |
required |
speedup
|
bool
|
If True (classification only), caches forward pass outputs to accelerate fitting |
True
|
verbose
|
bool
|
If True, prints NLPD at each epoch |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
total_train_nlpd |
List[float]
|
Average NLPD per epoch over training data |
Source code in suq/base_suq.py
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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 | |
MLP Functions with Diagonal Covariance
Classes
SUQ_Linear_Diag
Bases: Module
Linear layer with uncertainty propagation under SUQ, with a diagonal Gaussian posterior.
Wraps a standard nn.Linear layer and applies closed-form mean and variance propagation. See the SUQ paper for theoretical background and assumptions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
org_linear
|
Linear
|
The original linear layer to wrap |
required |
w_var
|
Tensor
|
Element-wise variance of the weights |
required |
b_var
|
Tensor
|
Element-wise variance of the bias |
required |
Source code in suq/diag_suq_mlp.py
Functions
forward(a_mean, a_var)
Forward pass with uncertainty propagation through a SUQ linear layer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a_mean
|
Tensor
|
Input mean. Shape: |
required |
a_var
|
Tensor
|
Input element-wise variance. Shape: |
required |
Returns:
| Name | Type | Description |
|---|---|---|
h_mean |
Tensor
|
Mean of the output |
h_var |
Tensor
|
Element-wise variance of output |
Source code in suq/diag_suq_mlp.py
SUQ_Activation_Diag
Bases: Module
Activation layer with closed-form uncertainty propagation under SUQ, with a diagonal Gaussian posterior.
Wraps a standard activation function and applies a first-order approximation to propagate input variance through the nonlinearity. See the SUQ paper for theoretical background and assumptions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
afun
|
Callable
|
A PyTorch activation function (e.g. |
required |
Source code in suq/diag_suq_mlp.py
Functions
forward(h_mean, h_var)
Forward pass with uncertainty propagation through a SUQ activation layer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
h_mean
|
Tensor
|
Mean of the pre-activations |
required |
h_var
|
Tensor
|
Element-wise variance of the pre-activation |
required |
Returns:
| Name | Type | Description |
|---|---|---|
a_mean |
Tensor
|
Mean of the activation |
a_var |
Tensor
|
Element-wise variance of the activation |
Source code in suq/diag_suq_mlp.py
SUQ_BatchNorm_Diag
Bases: Module
BatchNorm layer with closed-form uncertainty propagation under SUQ, with a diagonal Gaussian posterior.
Wraps nn.BatchNorm1d and adjusts input variance using batch normalization statistics and scale parameters. See the SUQ paper for theoretical background and assumptions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
BatchNorm
|
BatchNorm1d
|
The original batch norm layer |
required |
Source code in suq/diag_suq_mlp.py
Functions
forward(x_mean, x_var)
Forward pass with uncertainty propagation through a SUQ BatchNorm layer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x_mean
|
Tensor
|
Input mean. Shape: [B, D] |
required |
x_var
|
Tensor
|
Input element-wise variance. Shape: [B, D] |
required |
Returns:
| Name | Type | Description |
|---|---|---|
out_mean |
Tensor
|
Output mean after batch normalization. Shape: [B, D] |
out_var |
Tensor
|
Output element-wise variance after batch normalization. Shape: [B, D] |
Source code in suq/diag_suq_mlp.py
SUQ_MLP_Diag
Bases: SUQ_Base
Multilayer perceptron model with closed-form uncertainty propagation under SUQ, with a diagonal Gaussian posterior.
Wraps a standard MLP, converting its layers into SUQ-compatible components. Supports both classification and regression via predictive Gaussian approximation.
Note
The input model should correspond to the latent function only: - For regression, this is the full model (including final output layer). - For classification, exclude the softmax layer and pass only the logit-producing part.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
org_model
|
Module
|
The original MLP model to convert |
required |
posterior_variance
|
Tensor
|
Flattened posterior variance vector |
required |
likelihood
|
str
|
Either 'classification' or 'regression' |
required |
scale_init
|
float
|
Initial scale factor |
1.0
|
sigma_noise
|
float
|
noise level (for regression) |
None
|
Source code in suq/diag_suq_mlp.py
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 222 223 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 302 303 304 305 306 307 308 309 310 311 312 | |
Functions
forward_latent(data, out_var=None)
Compute the predictive mean and variance of the latent function before applying the likelihood.
Traverses the model layer by layer, propagating mean and variance through each SUQ-wrapped layer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Tensor
|
Input data. Shape: [B, D_in] |
required |
out_var
|
Tensor or None
|
Optional input variance. Shape: [B, D_in] |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
out_mean |
Tensor
|
Output mean after final layer. Shape: [B, D_out] |
out_var |
Tensor
|
Output element-wise variance after final layer. Shape: [B, D_out] |
Source code in suq/diag_suq_mlp.py
forward(data)
Compute the predictive distribution based on the model's likelihood setting.
For classification, use probit-approximation. For regression, returns the latent mean and total predictive variance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Tensor
|
Input data. Shape: [B, D] |
required |
Returns:
| Type | Description |
|---|---|
|
If classification: Tensor: Class probabilities. Shape: [B, num_classes] |
|
|
If regression: Tuple[Tensor, Tensor]: Output mean and element-wise variance. Shape: [B, D_out] |
Source code in suq/diag_suq_mlp.py
convert_model(org_model, posterior_variance)
Converts a deterministic MLP into a SUQ-compatible model with diagonal posterior.
Each layer is replaced with its corresponding SUQ module (e.g. linear, activation, batchnorm), using the provided flattened posterior variance vector.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
org_model
|
Module
|
The original model to convert (latent function only) |
required |
posterior_variance
|
Tensor
|
Flattened posterior variance for Bayesian parameters |
required |
Source code in suq/diag_suq_mlp.py
Functions
forward_aW_diag(a_mean, a_var, weight, bias, w_var, b_var)
Compute the mean and element-wise variance of h = a @ W^T + b when the posterior has diagonal covariance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a_mean
|
Tensor
|
Mean of the input |
required |
a_var
|
Tensor
|
Variance of the input |
required |
weight
|
Tensor
|
Mean of the weights |
required |
bias
|
Tensor
|
Mean of the bias |
required |
b_var
|
Tensor
|
Element-wise variance of the bias |
required |
w_var
|
Tensor
|
Element-wise variance of the weights |
required |
Returns:
| Name | Type | Description |
|---|---|---|
h_mean |
Tensor
|
Mean of the pre-activations |
h_var |
Tensor
|
Element-wise variance of the pre-activations |
Source code in suq/diag_suq_mlp.py
forward_activation_implicit_diag(activation_func, h_mean, h_var)
Approximate the distribution of a = g(h) given h ~ N(h_mean, h_var), where h_var
is the element-wise variance of pre-activation h.
Uses a first-order Taylor expansion: a ~ N(g(h_mean), g'(h_mean)^T @ h_var @ g'(h_mean)).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
activation_func
|
Callable
|
A PyTorch activation function |
required |
h_mean
|
Tensor
|
Mean of the pre-activations |
required |
h_var
|
Tensor
|
Element-wise variance of the pre-activations |
required |
Returns:
| Name | Type | Description |
|---|---|---|
a_mean |
Tensor
|
Mean of the activations |
a_var |
Tensor
|
Element-wise variance of the activations |
Source code in suq/diag_suq_mlp.py
forward_batch_norm_diag(h_var, bn_weight, bn_running_var, bn_eps)
Compute the output variance when a distribution h ~ N(h_mean, h_var)
is passed through a BatchNorm layer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
h_var
|
Tensor
|
Element-wise variance of the input |
required |
bn_weight
|
Tensor
|
Batch normalization scale factor (gamma). Shape: |
required |
bn_running_var
|
Tensor
|
Running variance used in batch normalization. Shape: |
required |
bn_eps
|
float
|
Small constant added to the denominator for numerical stability. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
output_var |
Tensor
|
Element-wise variance of the output after batch normalization. Shape: |
Source code in suq/diag_suq_mlp.py
Transformer Functions with Diagonal Covariance
Classes
SUQ_LayerNorm_Diag
Bases: Module
LayerNorm module with uncertainty propagation under SUQ.
Wraps nn.LayerNorm and propagates input variance analytically using running statistics. See the SUQ paper for theoretical background and assumptions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
LayerNorm
|
LayerNorm
|
The original layer norm module to wrap |
required |
Source code in suq/diag_suq_transformer.py
Functions
forward(x_mean, x_var)
Forward pass with uncertainty propagation through a SUQ LayerNorm layer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x_mean
|
Tensor
|
Input mean. Shape: [B, T, D] |
required |
x_var
|
Tensor
|
Input element-wise variance. Shape: [B, T, D] |
required |
Returns:
| Name | Type | Description |
|---|---|---|
out_mean |
Tensor
|
Output mean after layer normalization. Shape: [B, T, D] |
out_var |
Tensor
|
Output element-wise variance after layer normalization. Shape: [B, T, D] |
Source code in suq/diag_suq_transformer.py
SUQ_Classifier_Diag
Bases: Module
Classifier head with uncertainty propagation under SUQ, with a diagonal Gaussian posterior.
Wraps a standard linear classifier and applies closed-form mean and variance propagation. See the SUQ paper for theoretical background and assumptions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
classifier
|
Linear
|
The final classification head |
required |
w_var
|
Tensor
|
Element-wise variance of weight. Shape: |
required |
b_var
|
Tensor
|
Element-wise variance of bias. Shape: |
required |
Source code in suq/diag_suq_transformer.py
Functions
forward(x_mean, x_var)
Forward pass with uncertainty propagation through a SUQ linear layer.
Args.
x_mean (Tensor): Input mean. Shape: [B, D_in]
x_var (Tensor): Input element-wise variance. Shape: [B, D_in]
Returns:
| Name | Type | Description |
|---|---|---|
h_mean |
Tensor
|
Output mean. Shape: |
h_var |
Tensor
|
Output element-wise variance. Shape: |
Source code in suq/diag_suq_transformer.py
SUQ_TransformerMLP_Diag
Bases: Module
MLP submodule of a transformer block with uncertainty propagation under SUQ.
Supports both deterministic and Bayesian forward modes with closed-form variance propagation.
Used internally in SUQ_Transformer_Block_Diag.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
MLP
|
Module
|
Original MLP submodule |
required |
determinstic
|
bool
|
Whether to treat the MLP weights as deterministic |
True
|
w_fc_var
|
Tensor
|
Variance of the first linear layer in MLP(if Bayesian) |
None
|
w_proj_var
|
Tensor
|
Variance of the second linear layer in MLP (if Bayesian) |
None
|
Source code in suq/diag_suq_transformer.py
Functions
forward(x_mean, x_var)
Forward pass with uncertainty propagation through a SUQ Transformer MLP layer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x_mean
|
Tensor
|
Input mean. Shape [B, T, D] |
required |
x_var
|
Tensor
|
Input element-wise variance. Shape [B, T, D] |
required |
Returns:
| Name | Type | Description |
|---|---|---|
h_mean |
Tensor
|
Output mean. Shape [B, T, D] |
h_var |
Tensor
|
Output element-wise variance. Shape [B, T, D] |
Source code in suq/diag_suq_transformer.py
SUQ_Attention_Diag
Bases: Module
Self-attention module with uncertainty propagation under SUQ.
Supports deterministic and Bayesian value projections, with optional diagonal covariance assumptions. For details see SUQ paper section A.6
Used internally in SUQ_Transformer_Block_Diag.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
Attention
|
Module
|
The original attention module |
required |
determinstic
|
bool
|
Whether to treat value projections as deterministic |
True
|
diag_cov
|
bool
|
If True, only compute the diagoanl covariance for value |
False
|
W_v_var
|
Tensor
|
Posterior variance for value matrix (if Bayesian) |
None
|
Source code in suq/diag_suq_transformer.py
Functions
forward(x_mean, x_var)
Forward pass with uncertainty propagation through a SUQ Attention layer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x_mean
|
Tensor
|
Input mean. Shape [B, T, D] |
required |
x_var
|
Tensor
|
Input element-wise variance. Shape [B, T, D] |
required |
Returns:
| Name | Type | Description |
|---|---|---|
output_mean |
Tensor
|
Output mean. Shape [B, T, D] |
output_var |
Tensor
|
Output element-wise variance. Shape [B, T, D] |
Source code in suq/diag_suq_transformer.py
SUQ_Transformer_Block_Diag
Bases: Module
Single transformer block with uncertainty propagation under SUQ.
Wraps LayerNorm, attention, and MLP submodules with uncertainty-aware versions.
Used in SUQ_ViT_Diag to form a full transformer stack.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
MLP
|
Module
|
Original MLP submodule |
required |
Attention
|
Module
|
Original attention submodule |
required |
LN_1
|
LayerNorm
|
Pre-attention layer norm |
required |
LN_2
|
LayerNorm
|
Pre-MLP layer norm |
required |
MLP_determinstic
|
bool
|
Whether to treat MLP as deterministic |
required |
Attn_determinstic
|
bool
|
Whether to treat attention as deterministic |
required |
diag_cov
|
bool
|
If True, only compute the diagoanl covariance for value |
False
|
w_fc_var
|
Tensor or None
|
Posterior variance of MLP input projection (if Bayesian) |
None
|
w_proj_var
|
Tensor or None
|
Posterior variance of MLP output projection (if Bayesian) |
None
|
W_v_var
|
Tensor or None
|
Posterior variance of value matrix (if Bayesian) |
None
|
Source code in suq/diag_suq_transformer.py
Functions
forward(x_mean, x_var)
Forward pass with uncertainty propagation through a SUQ Transformer block.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x_mean
|
Tensor
|
Input mean. Shape [B, T, D] |
required |
x_var
|
Tensor
|
Input element-wise variance. Shape [B, T, D] |
required |
Returns:
| Name | Type | Description |
|---|---|---|
h_mean |
Tensor
|
Output mean. Shape [B, T, D] |
h_var |
Tensor
|
Output element-wise variance. Shape [B, T, D] |
Source code in suq/diag_suq_transformer.py
SUQ_ViT_Diag
Bases: SUQ_Base
Vision Transformer model with uncertainty propagation under SUQ, with a diagonal Gaussian posterior.
Wraps a ViT architecture into a structured uncertainty-aware model by replacing parts of the network with SUQ-compatible blocks. Allows selective Bayesian treatment of MLP and attention modules within each transformer block.
Currently supports classification only. See the SUQ paper for theoretical background and assumptions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ViT
|
Module
|
A Vision Transformer model structured like |
required |
posterior_variance
|
Tensor
|
Flattened posterior variance vector |
required |
MLP_determinstic
|
bool
|
Whether MLP submodules are treated as deterministic |
required |
Attn_determinstic
|
bool
|
Whether attention submodules are treated as deterministic |
required |
scale_init
|
float
|
Initial value for the scale factor |
1.0
|
attention_diag_cov
|
bool
|
If True, only compute the diagoanl covariance for value |
False
|
likelihood
|
str
|
Currently only support 'Classification' |
'clasification'
|
num_det_blocks
|
int
|
Number of transformer blocks to leave deterministic (from the bottom up) |
10
|
Source code in suq/diag_suq_transformer.py
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 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 | |
Functions
forward_latent(pixel_values, interpolate_pos_encoding=None)
Compute the predictive mean and variance of the ViT's latent output before applying the final likelihood layer.
Traverses the full transformer stack with uncertainty propagation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pixel_values
|
Tensor
|
Input image tensor, shape [B, C, H, W] |
required |
interpolate_pos_encoding
|
optional
|
Optional positional embedding interpolation |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
x_mean |
Tensor
|
Predicted latent mean at the [CLS] token, shape [B, D] |
x_var |
Tensor
|
Predicted latent variance at the [CLS] token, shape [B, D] |
Source code in suq/diag_suq_transformer.py
forward(pixel_values, interpolate_pos_encoding=None)
Compute predictive class probabilities using a probit approximation.
Performs a full forward pass through the ViT with uncertainty propagation, and produces softmax-normalized class probabilities for classification.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pixel_values
|
Tensor
|
Input image tensor, shape [B, C, H, W] |
required |
interpolate_pos_encoding
|
optional
|
Optional positional embedding interpolation |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
Tensor |
Predicted class probabilities, shape [B, num_classes] |
Source code in suq/diag_suq_transformer.py
Functions
forward_linear_diag_Bayesian_weight(e_mean, e_var, w_mean, w_var, bias=None)
Compute the mean and element-wise variance of h = e @ W^T + b when e ~ N(e_mean, e_var) and W ~ N(w_mean, w_var)
Note
- We only make the weight Bayesian, bias is treated determinstically
- We always assume the input to next layer has diagonal covariance, so we only compute the variance over
hhere.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
e_mean
|
Tensor
|
Mean of the input embeddings |
required |
e_var
|
Tensor
|
Element-wise variance of the input embeddings |
required |
w_mean
|
Tensor
|
Mean of the weights |
required |
w_var
|
Tensor
|
Element-wise variance of the weights |
required |
bias
|
Tensor
|
Bias term |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
h_mean |
Tensor
|
Mean of the output |
h_var |
Tensor
|
Element-wise variance of the output |
Source code in suq/diag_suq_transformer.py
forward_linear_diag_determinstic_weight(e_mean, e_var, weight, bias=None)
Compute the mean and element-wise variance of h = e @ W^T + b when e ~ N(e_mean, e_var), W and b are both determinstic
Note
- We always assume the input to next layer has diagonal covariance, so we only compute the variance over
hhere.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
e_mean
|
Tensor
|
Mean of the input embeddings |
required |
e_var
|
Tensor
|
Element-wise variance of the input embeddings |
required |
weight
|
Tensor
|
Weights |
required |
bias
|
Tensor
|
Bias term |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
h_mean |
Tensor
|
Mean of the output |
h_var |
Tensor
|
Element-wise variance of the output |
Source code in suq/diag_suq_transformer.py
forward_activation_diag(activation_func, h_mean, h_var)
Approximate the distribution of a = g(h) given h ~ N(h_mean, h_var), where h_var
is the element-wise variance of pre-activation h.
Uses a first-order Taylor expansion: a ~ N(g(h_mean), g'(h_mean)^T @ h_var @ g'(h_mean)).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
activation_func
|
Callable
|
A PyTorch activation function |
required |
h_mean
|
Tensor
|
Mean of the pre-activations |
required |
h_var
|
Tensor
|
Element-wise variance of the pre-activations |
required |
Returns:
| Name | Type | Description |
|---|---|---|
a_mean |
Tensor
|
Mean of the activations |
a_var |
Tensor
|
Element-wise variance of the activations |
Source code in suq/diag_suq_transformer.py
forward_layer_norm_diag(e_mean, e_var, ln_weight, ln_eps)
Compute the output variance when a distribution e ~ N(e_mean, e_var)
is passed through a LayerhNorm layer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
e_mean
|
Tensor
|
Mean of the input distribution. Shape: |
required |
e_var
|
Tensor
|
Element-wise variance of the input distribution. Shape: |
required |
ln_weight
|
Tensor
|
LayerNorm scale factor (gamma). Shape: |
required |
ln_eps
|
float
|
Small constant added for numerical stability. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
output_var |
Tensor
|
Element-wise variance after LayerNorm. Shape: |
Source code in suq/diag_suq_transformer.py
forward_value_cov_Bayesian_W(W_v, W_v_var, e_mean, e_var, n_h, D_v, diag_cov=False)
Given value weight W_v ~ N(W_v, W_v_var) and input E ~ N(e_mean, e_var)
Compute the covariance of output v = W_v @ E
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_h
|
int
|
Number of attention heads. |
required |
D_v
|
int
|
Dimension per head. Must satisfy |
required |
W_v
|
Tensor
|
Mean of the value weight |
required |
W_v_var
|
Tensor
|
Element-wise variance of the value weight |
required |
e_mean
|
Tensor
|
Mean of the input embeddings |
required |
e_var
|
Tensor
|
Element-wise variance of the input embeddings |
required |
diag_cov
|
bool
|
If |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
v_var |
Tensor
|
Returned if |
v_cov |
Tensor
|
Returned if |
Source code in suq/diag_suq_transformer.py
forward_value_cov_determinstic_W(W_v, e_var, n_h, D_v, diag_cov=False)
Given determinstic value weight W_v and input E ~ N(e_mean, e_var)
Compute the covariance of output v = W_v @ E
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_h
|
int
|
Number of attention heads. |
required |
D_v
|
int
|
Dimension per head. Must satisfy |
required |
W_v
|
Tensor
|
Value weight |
required |
e_var
|
Tensor
|
Element-wise variance of the input embeddings |
required |
diag_cov
|
bool
|
If |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
v_var |
Tensor
|
Returned if |
v_cov |
Tensor
|
Returned if |
Source code in suq/diag_suq_transformer.py
forward_QKV_cov(attention_score, v_cov, diag_cov=False)
Given attention score (QK^T) and V ~ N(v_mean, v_cov)
Compute the covariance of output E = (QK^T) V
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
attention_score
|
Tensor
|
Attention weights |
required |
v_cov
|
Tensor
|
Covariance of the value |
required |
diag_cov
|
bool
|
If |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
QKV_var |
Tensor
|
Returned if |
QKV_cov |
Tensor
|
Returned if |
Source code in suq/diag_suq_transformer.py
forward_fuse_multi_head_cov(QKV_cov, project_W, diag_cov=False)
Given concatanated multi-head embedding E ~ N(e_mean, e_cov) and the determinstic projection weight matrix W
Compute variance of each output dimenison
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
QKV_cov
|
Tensor
|
Covariance of the concatenated multi-head output |
required |
project_W
|
Tensor
|
Projection weight matrix |
required |
diag_cov
|
bool
|
If |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
output_var |
Tensor
|
Element-wise variance of the projected output. Shape: |