MSA and pair representations
ESMFold Relative Position Buckets
Free account with email, Google or GitHub. Your code runs in an isolated PyTorch sandbox.
Your task
Turn each ordered residue pair’s signed offset into a clipped category and look up its supplied embedding. ESMFold uses residue_index[j] − residue_index[i]; invalid pairs use the learned padding bucket 0, whose embedding may be nonzero.
Function signature
esmfold_relative_position_buckets(
residue_index: torch.Tensor,
mask: torch.Tensor,
weight: torch.Tensor,
residue_radius: int = 32,
) -> dict[str, torch.Tensor]Worked example
Worked example
Offsets use j minus i. The valid 2-by-2 bucket block is [[2, 3], [1, 2]]. Token 2 is invalid, so every touching pair uses padding bucket 0. Its embedding is the supplied nonzero weight[0]=10; padding embeddings must not be zeroed.
import torch
residue_index = torch.tensor([[2, 3, 9]], dtype=torch.int64)
mask = torch.tensor([[True, True, False]], dtype=torch.bool)
weight = torch.tensor([[10.0], [20.0], [30.0], [40.0]], dtype=torch.float64, requires_grad=True)
residue_radius = 1
result = esmfold_relative_position_buckets(residue_index=residue_index, mask=mask, weight=weight, residue_radius=residue_radius)Expected return
{
'indices': torch.tensor([[[[2], [3], [0]], [[1], [2], [0]], [[0], [0], [0]]]], dtype=torch.int64),
'features': torch.tensor([[[[0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 1.0], [1.0, 0.0, 0.0, 0.0]], [[0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0], [1.0, 0.0, 0.0, 0.0]], [[1.0, 0.0, 0.0, 0.0], [1.0, 0.0, 0.0, 0.0], [1.0, 0.0, 0.0, 0.0]]]], dtype=torch.float64),
'embedding': torch.tensor([[[[30.0], [40.0], [10.0]], [[20.0], [30.0], [10.0]], [[10.0], [10.0], [10.0]]]], dtype=torch.float64),
}Parameters
| Parameter | Shape and meaning |
|---|---|
residue_indextorch.Tensor | [B, N]Supplied residue numbering, not required to start at zero. |
masktorch.Tensor | [B, N]True for valid tokens. |
weighttorch.Tensor | [2r+2,C]Learned projection from the ordered one-hot feature blocks to output channels. |
residue_radiusintDefault 32 | Clipping radius for residue-index differences. |
Return
dict[str, torch.Tensor]
Return exactly the named fields applicable to this operation.
Required behavior
- For pair(i,j), bucket = clamp(residue_index[j] − residue_index[i], −r, r) + r +1. Invalid pairs use bucket0, a REAL learned padding category. Return its one-hot vector and weight lookup even for invalid pairs; do not zero the padding embedding.
- Return indices
[B,N,N,1], one-hot features (B,N,N,2r+2), and embedding. Valid offsets occupy categories 1 through2r+1. - 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
ESMFold uses a learned lookup for clipped residue offsets and reserves a real embedding row for padding.
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 esmfold_relative_position_buckets(
residue_index: torch.Tensor, # (B, N)
mask: torch.Tensor, # (B, N)
weight: torch.Tensor, # (2r+2,C)
residue_radius: int = 32,
) -> dict[str, torch.Tensor]:
"""Return dict[str, torch.Tensor]:
indices: (B,N,N,1).
features: (B,N,N,2r+2).
embedding: (B, N, N, C)."""
# Implement the operation. Inputs and test setup are supplied by BioTorch.
raise NotImplementedError("Implement esmfold_relative_position_buckets")
Ready to implement it?
3 progressive hints, a solution walkthrough, and grading that checks values, shapes and gradients are waiting in the workspace.
Free account with email, Google or GitHub. Your code runs in an isolated PyTorch sandbox.
Related exercises
Sources and further reading
- www.nature.com
- google-deepmind/alphafold · modules.py
- www.nature.com
- google-deepmind/alphafold3 · featurization.py
- www.science.org
- facebookresearch/esm · trunk.py
- www.biorxiv.org
- huggingface/transformers · modeling_esmfold2.py
- www.science.org
- Biohub/esm · vqvae.py
- www.science.org
- dauparas/ProteinMPNN · protein_mpnn_utils.py
A primitive exercise, not an end-to-end reproduction of the named models. No affiliation or endorsement is implied.