Structure module, templates and recycling · INTEGRATION

Template-axis attention and mean aggregation

HardRaw PyTorchAbout 30 min2 public examples plus private tests

AlphaFold2OpenFoldAlphaFold3RFdiffusion

Write and run your solution Sign in

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

Your task

Combine the available templates separately for each residue pair. In attention mode, the query determines each template’s share of a weighted value mixture; in mean mode, average the valid template vectors before ReLU and projection. Return the pair update and the corresponding template coefficients.

Function signature

template_pair_attention(
    activations: dict,
    parameters: dict,
    masks: dict,
    mode: str = 'attention',
    mean_eps: float = 1e-07,
) -> tuple[torch.Tensor, torch.Tensor]

Worked example

A query favors one template

Consider one valid residue pair, one head and D=V=1 in attention mode, after the supplied projections. Let the projected query be 1, template keys [0,log(3)], and template values [2,4]; both templates are valid. Scores are [0,log(3)], giving weights [1/4,3/4] over templates. The weighted value is (1/4)·2 + (3/4)·4 = 3.5. With output weight 1 and bias 1, the pair update is 4.5. These are update features; no input residual is added.

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.
modestrDefault 'attention'Choose "attention" for query-based template softmax or "mean" for a masked mean followed by ReLU and a separate affine readout.
mean_epsfloatDefault 1e-07Nonnegative offset added to the usable-template count in mean mode. It affects both the average and returned mean weights; empty pairs stay zero.

Return

tuple[torch.Tensor, torch.Tensor]

Return these tensors in the stated order.

Required behavior

  • Return a template-derived pair UPDATE, without residual addition. All dimensions are positive (T\ge 1; missing templates use False masks), all floats finite and same dtype/device. template[B,T,N,N,C_t] contains already pair-processed template features.
  • template_mask[B,T,N,N] and pair_mask[B,N,N] are Boolean True means usable; effective mask is their conjunction. Invalid template payloads are ignored and invalid/empty query pairs return exact zero, including output bias.
  • mode='attention' (default): query[B,N,N,C_q], w_q[C_q,H,D], w_k[C_t,H,D], w_v[C_t,H,V] are bias-free projections.
  • For each b,i,j,h, compute \mathrm{logits}_{t}=\operatorname{dot}(\mathrm{query}_{ij}\,\mathrm{w\_q}_{h},\mathrm{template}_{tij}\,\mathrm{w\_k}_{h})/\sqrt{D}. Apply numerically stable masked softmax over T ONLY. Expose weights[B,N,N,H,T]; all-masked weights are zero.
  • Average projected template values, flatten H,V in head-major order, apply w_out[H\cdot V,C_q] and b_out[C_q], then zero empty/invalid pairs. There is no gate or LayerNorm inside this attention variant; callers supply the desired preprocessed template/query representations.
  • mode='mean': query and attention weights are absent. Average usable template vectors with denominator \operatorname{count}+\mathrm{mean\_eps} (\mathrm{mean\_eps}\ge 0 default 1\times10^{-7}); if \operatorname{count}=0 use an exact zero average. Apply elementwise ReLU, then w_mean[C_t,C_q] and b_mean[C_q].
  • Return weights[B,T,N,N] equal to \mathrm{effective\_mask}/(\operatorname{count}+\mathrm{mean\_eps}), zero for empty pairs. This is AF3's mean/ReLU/readout algebra; with all templates present the denominator is T+1\times10^{-7}, as in the pinned code.
  • A pair-specific mask is an educational extension, not AF3's total-template denominator for a partially invalid pair. ReLU inputs exactly zero are excluded from smooth-gradient fixtures unless a structural all-masked zero. No dropout, pair-trunk iteration, or hidden parameters.

Why this matters

Templates offer alternative descriptions of the same residue pair. Attention lets the query choose among them; mean mode treats usable templates equally before a ReLU and learned readout.

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 template_pair_attention(
    activations: dict,
    parameters: dict,
    masks: dict,
    mode: str = 'attention',
    mean_eps: float = 1e-07,
) -> tuple[torch.Tensor, torch.Tensor]:
    """Return tuple[torch.Tensor, torch.Tensor]:
    
    update: (B, N, N, Cq).
    weights: Attention mode: (B, N, N, H, T). Mean mode: (B, T, N, N)."""
    # Implement the operation. Inputs and test setup are supplied by BioTorch.
    raise NotImplementedError("Implement template_pair_attention")

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.

Related exercises

Sources and further reading

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