Molecular graphs, equivariance and docking
Geometric direction-and-distance attention
Free account with email, Google or GitHub. Your code runs in an isolated PyTorch sandbox.
Your task
Use the alignment of transformed directions and distances between transformed points to weight residue interactions. Return the attention weights, aggregated local vectors, and projected output features.
Function signature
geometric_reasoning_attention(
activations: dict,
parameters: dict,
masks: dict,
structure: dict,
) -> dict[str, torch.Tensor]Worked example
Rotate values back into each query frame
Use two valid residues, one head and one value vector per residue. Frame 0 is identity at (0,0,0); frame 1 is a positive z quarter-turn at (3,0,0). All local query/key directions are (1,0,0), and local query/key points are zero. Set both scale logits so softplus equals sqrt(3). Global directions are perpendicular across residues and point separation is 3, giving self logits 1 and cross logits −3 after the sqrt(3) divisor. Thus self weight is a=1/(1+exp(-4))≈0.982014. Local values (1,0,0) and (2,0,0) rotate to global (1,0,0) and (0,2,0). Query 0 returns local vector [a,2(1-a),0]. Query 1 first averages to global [1-a,2a,0], then its inverse rotation gives local [2a,-(1-a),0]. With identity output projection and zero bias, these are also the output features. Translation affects points, not these value vectors.
Parameters
| Parameter | Shape and meaning |
|---|---|
activationsdict | Supplied intermediate tensors. |
parametersdict | Supplied model parameters, keyed as shown below. |
masksdict | Supplied masks. Each field below defines what True includes or permits. |
structuredict | Fields supplied together in this dictionary. |
Return
dict[str, torch.Tensor]
Return exactly the named fields applicable to this operation.
Required behavior
- Implement the ESM3 geometric-
attentionkernel from supplied local projections.rotation[B,N,3,3] maps column localvectorsto the global frame and is a proper orthogonal matrix;translation[B,N,3] is its global origin. q_direction,k_direction,q_point,k_pointeach have [B,N,H,3];valueshas [B,N,H,M,3].B,N,H,M \ge 1. Learneddirection_scale[H]anddistance_scale[H]are unconstrained finite logits, not already positive scales.output_weight[H\cdot M\cdot 3,\mathrm{Cout}]and optionaloutput_bias[\mathrm{Cout}]complete the readout,\mathrm{Cout}\ge 1. All floatingvaluesshare\mathrm{float32}/64dtype/device and have representable products, squared lengths and results.- For residue
i/headh, rotate\mathrm{qd\_i}=R_{i}\cdot q_{\mathrm{direction\_i}}and\mathrm{kd\_j}=R_{j}\cdot k_{\mathrm{direction\_j}}; transform points\mathrm{qp\_i}=R_{i}\cdot q_{\mathrm{point\_i}}+t_{i}and\mathrm{kp\_j}=R_{j}\cdot k_{\mathrm{point\_j}}+t_{j}. - Score
a_{\mathrm{ijh}}=(\operatorname{softplus}(\mathrm{direction\_scale\_h})\cdot \operatorname{dot}(\mathrm{qd\_i},\mathrm{kd\_j})-\operatorname{softplus}(\mathrm{distance\_scale\_h})\cdot \Vert \mathrm{qp\_i}-\mathrm{kp\_j}\Vert \_2)/\sqrt{3}, where\operatorname{softplus}(x)=\operatorname{log}(1+\operatorname{exp}(x))must be evaluated stably. - The distance is not squared and has no added
\epsilon. Define its derivative at a zero displacement to be the zero vector.frame_mask[B,N] is required boolTruefor a valid frame. An optional boolpair_maskbroadcasts to [B,H,N,N];Trueallows an edge. Allowed edges intersect query/key frame validity andpair_mask. - Normalize scores over key
jwith stable masked softmax; all-masked head rows return exact zeros. Rotate each local value vector withR_jonly (NOtranslation), aggregate byattention, then rotate the result back withR_itranspose. Returnvectors[B,N,H,M,3],attention[B,H,N,N] andoutput[B,N,\mathrm{Cout}]. - Flatten local
vectorsinH,M,xyz order, multiply byoutput_weight, and add optionaloutput_bias. Forceoutputto zero when no head has an allowed key so bias cannot revive an empty query. There is no scalar residual or internalQ/K/Vprojection/LayerNorm in this kernel. - Accept noncontiguous inputs, finite masked padding, arbitrary per-head exclusion masks, and never mutate inputs. Empty batch/residue/head/message axes and nonorthogonal input rotations are outside the forward domain.
Why this matters
Each residue has its own coordinate frame. Compare geometry in the shared global frame, then express the attended vectors back in the query residue’s frame. Directions and vectors rotate; points also translate.
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 geometric_reasoning_attention(
activations: dict,
parameters: dict,
masks: dict,
structure: dict,
) -> dict[str, torch.Tensor]:
"""Return dict[str, torch.Tensor]:
output: (B, N, Cout).
attention: (B, H, N, N).
vectors: (B, N, H, M, 3)."""
# Implement the operation. Inputs and test setup are supplied by BioTorch.
raise NotImplementedError("Implement geometric_reasoning_attention")
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.
Sources and further reading
A primitive exercise, not an end-to-end reproduction of the named models. No affiliation or endorsement is implied.