MSA and pair representations

ESMFold 2 Single-to-Pair Features

HardRaw PyTorchAbout 30 min1 public example plus private tests

ESMFold2

Write and run your solution Sign in

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

Your task

Build ESMFold2 ordered product/difference pair features from projected token vectors, then apply the supplied GELU pair head.

Function signature

esmfold2_single_to_pair(
    activations: dict,
    parameters: dict,
    masks: dict,
    eps: float = 1e-05,
    output_norm: bool = False,
) -> tuple[torch.Tensor, torch.Tensor]

Worked example

Products and differences have different symmetry

At the pair-construction stage, suppose the initial projection gives token vectors z₀=[1,2] and z₁=[3,5], with both tokens valid. Pair (0,1) is [z₀*z₁, z₀−z₁]=[3,10,-2,-3]; pair (1,0) is [3,10,2,3]. Self-pair (0,0) is [1,4,0,0]. These are componentwise products, not a 2×2 outer product. Each complete vector then passes through the supplied GELU pair head.

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-05Used only if output_norm=True, inside the final population LayerNorm variance square root.
output_normboolDefault FalseApply the supplied final affine normalization when True.

Return

tuple[torch.Tensor, torch.Tensor]

Return these tensors in the stated order.

Required behavior

  • x is [B,N,C]; mask is [B,N], with True meaning present. Sanitize invalid token features before normalization/projection, and zero both returned pair tensors unless both residue endpoints are present.
  • Do not normalize x before the initial weight/bias projection.
  • Do not split the projection into q/k. Pair(i,j) is the concatenation [z_i\odot z_j,\;z_i-z_j], where z=xW+b.
  • Project pair_features with hidden_weight/hidden_bias and apply exact erf GELU. Then apply out_weight and out_bias.
  • output_norm defaults to False. When True, apply affine population LayerNorm to the final output using output_scale/output_bias.
  • Every LayerNorm uses eps (default 1e-5) inside the population-variance square root. Return pair_features before the learned pair head and output after it.
  • Use CPU float32/float64 tensors with one shared floating dtype/device. Preserve strided layouts and do not mutate supplied tensors.
  • Use raw tensor operations, not a ready-made implementation of the requested primitive. Supplied parameters and random variates must not be reinitialized.

Why this matters

An ESMFold2 token vector is projected once. Pair products are symmetric in the two tokens, while differences preserve their order before the learned pair head.

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 esmfold2_single_to_pair(
    activations: dict,
    parameters: dict,
    masks: dict,
    eps: float = 1e-05,
    output_norm: bool = False,
) -> tuple[torch.Tensor, torch.Tensor]:
    """Return tuple[torch.Tensor, torch.Tensor]:
    
    pair_features: (B,N,N,2H).
    output: (B,N,N,D)."""
    # Implement the operation. Inputs and test setup are supplied by BioTorch.
    raise NotImplementedError("Implement esmfold2_single_to_pair")

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.