All-atom folding

Atom–Token Broadcast and Pool

MediumRaw PyTorchAbout 20 min2 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

Use the atom-to-token map in both directions: broadcast token features to their atoms and average atom features within each token. Return broadcast features, pooled features, and per-token counts.

Function signature

atom_token_pool(
    atom: torch.Tensor,
    token: torch.Tensor,
    atom_to_token: torch.Tensor,
    atom_mask: torch.Tensor,
    token_mask: torch.Tensor,
) -> dict[str, torch.Tensor]

Worked example

Repeated atoms and a masked token

Atoms 0 and 1 map to token 0, so their vectors [2,0] and [6,4] average to [4,2]. Token 2 is masked and excludes atom 2. Counts are [2,1,0,0]; empty tokens return zero.

import torch

atom = torch.tensor([[[2.0, 0.0], [6.0, 4.0], [10.0, 8.0], [17.0, 31.0], [101.0, 103.0]]], dtype=torch.float64, requires_grad=True)
token = torch.tensor([[[1.0, 2.0], [3.0, 4.0], [5.0, 6.0], [7.0, 8.0]]], dtype=torch.float64, requires_grad=True)
atom_to_token = torch.tensor([[0, 0, 2, -1, 1]], dtype=torch.int64)
atom_mask = torch.tensor([[True, True, True, False, True]], dtype=torch.bool)
token_mask = torch.tensor([[True, True, False, True]], dtype=torch.bool)

result = atom_token_pool(atom=atom, token=token, atom_to_token=atom_to_token, atom_mask=atom_mask, token_mask=token_mask)

Expected return

{
    'broadcast': torch.tensor([[[1.0, 2.0], [1.0, 2.0], [0.0, 0.0], [0.0, 0.0], [3.0, 4.0]]], dtype=torch.float64),
    'pooled': torch.tensor([[[4.0, 2.0], [101.0, 103.0], [0.0, 0.0], [0.0, 0.0]]], dtype=torch.float64),
    'counts': torch.tensor([[2.0, 1.0, 0.0, 0.0]], dtype=torch.float64),
}

Parameters

ParameterShape and meaning
atomtorch.Tensor[B, A, C]Atom features to average within tokens.
tokentorch.Tensor[B, N, C]Token features to broadcast back to atoms.
atom_to_tokentorch.Tensor[B, A]Token index for each atom; out-of-range values mean padding.
atom_masktorch.Tensor[B, A]True marks a usable atom.
token_masktorch.Tensor[B, N]True marks a usable token.

Return

dict[str, torch.Tensor]

Return exactly the named fields applicable to this operation.

Required behavior

  • atom has shape (B,A,C) and token has shape (B,N,C). An atom contributes only when atom_mask is True, its map lies in [0,N), and the mapped token_mask is True.
  • Broadcast the mapped token vector to each contributing atom; all other atom vectors are zero. Pool contributing atom vectors by exact sum/count; tokens with no atoms return zero.
  • counts has shape (B,N) in the floating feature dtype. Repeated token IDs accumulate contributions and gradients. Sentinel or out-of-range IDs contribute nothing. A zero-length token axis is supported.
  • Use supplied CPU torch.float32 or torch.float64 tensors with shared floating dtype/device. Do not mutate inputs or initialize new parameters; preserve arbitrary valid leading dimensions and accept noncontiguous inputs.

Why this matters

Gather copies features along a known map; pooling reverses the information flow by accumulating atoms into their parent token.

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 atom_token_pool(
    atom: torch.Tensor,  # (B, A, C)
    token: torch.Tensor,  # (B, N, C)
    atom_to_token: torch.Tensor,  # (B, A)
    atom_mask: torch.Tensor,  # (B, A)
    token_mask: torch.Tensor,  # (B, N)
) -> dict[str, torch.Tensor]:
    """Return dict[str, torch.Tensor]:
    
    broadcast: (B, A, C).
    pooled: (B, N, C).
    counts: (B, N)."""
    # Implement the operation. Inputs and test setup are supplied by BioTorch.
    raise NotImplementedError("Implement atom_token_pool")

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.