MSA and pair representations

Conditioned pair-bias attention and gated transition block

HardRaw PyTorchAbout 30 min3 public examples plus private tests

AlphaFold3ESMFold2

Write and run your solution Sign in

Free account with email, Google or GitHub. Your code runs in an isolated PyTorch sandbox.

Your task

Update each token by first attending to allowed keys, then applying a conditioned feedforward transition to that updated state. Conditioning controls feature gains, shifts, and how much each update is added. Return after_attention for the first residual state, output after both residuals, and the per-head key probabilities.

Function signature

conditioned_transformer_block(
    activations: dict,
    parameters: dict,
    masks: dict,
    eps: float = 1e-05,
    style: str = 'af3',
) -> dict[str, torch.Tensor]

Worked example

Worked example: attention changes the state consumed by a nonzero transition

Use two valid tokens [-1,1], [1,-1] with both keys allowed, AF3 style, illustrative eps=3, zero shifts/pair bias, and half adaptive, value, and residual gates. Scalar q/k/v projections along [-2,2] turn the first normalized token [-0.25,0.25] into query 1, with keys and values [1,-1].

First-token attention is [p,1-p], p=\sigma(2), where \sigma(z)=1/(1+e^{-z}). Let u=2p-1. Output weights [1,-1] add [u/4,-u/4], giving after_attention [-a,a], a=1-u/4\approx0.809601.

Normalize this updated state for the transition: [-t,t], t=a/(2\sqrt{a^2+3}). Both transition input projections select t, so its gated hidden value is f=t^2\sigma(t). Half residual gating and output weights [1,2] give final output [-a+f/2,a+f]\approx[-0.797213,0.834379].

Parameters

ParameterShape and meaning
activationsdictSupplied intermediate tensors.
parametersdictSupplied model parameters, keyed as shown below.
masksdictSupplied masks. Each field below defines what True includes or permits.
epsfloatDefault 1e-05Positive stabilizer inside all population LayerNorm variance square roots; masked softmax does not add epsilon to its denominator.
stylestrDefault 'af3'Choose "af3", "esmfold2". Each choice uses the matching input subset and computational rule below.

Return

dict[str, torch.Tensor]

Return exactly the named fields applicable to this operation.

Required behavior

  • Implement one complete conditioned token block with B,N,C,S,P,H,D,V,M\ge 1. style is 'af3' (default) or 'esmfold2'; they differ here in whether normalized pairs have an affine offset. All shapes below are exact, all active data and learned parameters finite and of one dtype/device. \varepsilon=\mathrm{eps}>0 defaults 10^{-5}.
  • Define LN as population last-channel centering divided by \sqrt{\mathrm{variance}+\varepsilon}, without affine.
  • For stage j, define: A_{j}(z,c)=\operatorname{LN}(z)\cdot \operatorname{sigmoid}((\operatorname{LN}(c)\cdot \mathrm{cond\_scale}[j])\mathbin{@}w_{\mathrm{scale}}[j]+b_{\mathrm{scale}}[j])+(\operatorname{LN}(c)\cdot \mathrm{cond\_scale}[j])\mathbin{@}w_{\mathrm{shift}}[j]. Here j=0 denotes attention and j=1 denotes the transition.
  • Packed stage axes contain DISTINCT learned parameters. Before arithmetic zero x/condition at invalid tokens and pair at disallowed edges. An edge i→k is allowed iff valid_i & valid_k & pair_mask_ik.
  • Let z=A_0(x,c). Project Q_{bnhd}=\sum_c z_{bnc}w^q_{chd}+b^q_{hd}, and project K/V similarly without bias. The query projection bias is added after contraction.
  • For af3, \mathrm{Bias\_ihk}=(\operatorname{LN}(\mathrm{pair\_ik})\cdot \mathrm{pair\_scale})\mathbin{@}w_{\mathrm{pair}}. The esmfold2 source also has an affine pair_bias [P] before w_pair; its projected contribution is the same scalar for every key in a head.
  • Cancel that softmax-invariant term ALGEBRAICALLY before evaluating pair logits, using the same centered formula as af3. Adding a huge offset numerically and then subtracting a softmax maximum can already have erased meaningful pair differences. Keep pair_bias as a declared input with exactly zero VJP. The result uses [B,H,N,N] head order.
  • Softmax over key k of \operatorname{dot}(Q_{i},K_{k})/\sqrt{D}+\mathrm{Bias} uses allowed edges; no keys gives exact-zero attention, with a safe denominator and no softmax \epsilon. Average values, multiply by \operatorname{sigmoid}(z\mathbin{@}w_{\mathrm{value\_gate}}), flatten head-major, and multiply w_attention_out.
  • Multiply this update by \operatorname{sigmoid}(c\mathbin{@}w_{\mathrm{residual\_gate}}[0]+b_{\mathrm{residual\_gate}}[0]) using RAW c, then add x to get after_attention. Apply A_1 to this FIRST residual result.
  • Transition \mathrm{hidden}=\operatorname{SiLU}(A_{1}\mathbin{@}w_{\mathrm{ff\_gate}})\cdot (A_{1}\mathbin{@}w_{\mathrm{ff\_up}}), followed by w_ff_out and \operatorname{sigmoid}(c\mathbin{@}w_{\mathrm{residual\_gate}}[1]+b_{\mathrm{residual\_gate}}[1]); add to after_attention for output.
  • Return both complete residual states plus attention. Only b_q, b_scale and b_residual_gate are projection biases; the esmfold2 variant also has pair_bias in its normalization. All other projections are bias-free. Mask both returned states after residuals; invalid payloads may be NaN/Inf.
  • Valid rows with no keys still undergo the transition, while their attention update is zero. No causality, dropout, hidden weights, precomputed network update or implicit multi-block stack. Key/value widths may differ. Numerical domain: \varepsilon must be finite and remain strictly positive when represented in the input dtype.
  • Active inputs and parameters must be bounded so the required arithmetic intermediates are finite and within that dtype's representable range: normalization sums/means, centered differences, squares, variance-plus-\varepsilon, projections, residual/statistic updates, and intermediates for the graded first-order derivatives.
  • Finite inputs alone do not guarantee this; overflow-sized reductions or squares are outside the contract. Representable subnormal \operatorname{sigmoid}/\operatorname{SiLU} tails are in scope and must not be prematurely discarded.
  • Use sigmoid(z)=1/(1+e^{-z}) and SiLU(z)=z\,sigmoid(z).

Why this matters

The block has two residual updates; each uses its own conditioning and the transition reads the state already updated by attention.

Starter code

You write the body of this function in the BioTorch workspace. Your code runs in an isolated PyTorch sandbox; Run checks the public examples and Submit grades the full test suite, including gradients.

import torch


def conditioned_transformer_block(
    activations: dict,
    parameters: dict,
    masks: dict,
    eps: float = 1e-05,
    style: str = 'af3',
) -> dict[str, torch.Tensor]:
    """Return dict[str, torch.Tensor]:
    
    output: (B, N, C).
    after_attention: (B, N, C). (B,N,C), after the first residual only.
    attention: (B, H, N, N)."""
    # Implement the operation. Inputs and test setup are supplied by BioTorch.
    raise NotImplementedError("Implement conditioned_transformer_block")

Ready to implement it?

3 progressive hints, a solution walkthrough, and grading that checks values, shapes and gradients are waiting in the workspace.

Write and run your solution Sign in

Free account with email, Google or GitHub. Your code runs in an isolated PyTorch sandbox.

Sources and further reading

A primitive exercise, not an end-to-end reproduction of the named models. No affiliation or endorsement is implied.