Molecular graphs, equivariance and docking

Geometric direction-and-distance attention

HardRaw PyTorchAbout 30 min2 public examples plus private tests

ESM3

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 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

ParameterShape and meaning
activationsdictSupplied intermediate tensors.
parametersdictSupplied model parameters, keyed as shown below.
masksdictSupplied masks. Each field below defines what True includes or permits.
structuredictFields 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-attention kernel from supplied local projections. rotation[B,N,3,3] maps column local vectors to the global frame and is a proper orthogonal matrix; translation[B,N,3] is its global origin.
  • q_direction,k_direction,q_point,k_point each have [B,N,H,3]; values has [B,N,H,M,3]. B,N,H,M \ge 1. Learned direction_scale [H] and distance_scale [H] are unconstrained finite logits, not already positive scales.
  • output_weight [H\cdot M\cdot 3,\mathrm{Cout}] and optional output_bias [\mathrm{Cout}] complete the readout, \mathrm{Cout}\ge 1. All floating values share \mathrm{float32}/64 dtype/device and have representable products, squared lengths and results.
  • For residue i/head h, 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 bool True for a valid frame. An optional bool pair_mask broadcasts to [B,H,N,N]; True allows an edge. Allowed edges intersect query/key frame validity and pair_mask.
  • Normalize scores over key j with stable masked softmax; all-masked head rows return exact zeros. Rotate each local value vector with R_j only (NO translation), aggregate by attention, then rotate the result back with R_i transpose. Return vectors[B,N,H,M,3], attention[B,H,N,N] and output[B,N,\mathrm{Cout}].
  • Flatten local vectors in H,M,xyz order, multiply by output_weight, and add optional output_bias. Force output to zero when no head has an allowed key so bias cannot revive an empty query. There is no scalar residual or internal Q/K/V projection/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.

Write and run your solution Sign in

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.