attention_pair_bias#

cuequivariance_torch.attention_pair_bias(
single_repr,
pair_repr,
mask,
num_heads,
w_ln_a,
b_ln_a,
w_proj_q,
b_proj_q,
w_proj_k,
w_proj_v,
w_proj_g,
w_proj_o,
w_proj_z=None,
b_proj_o=None,
b_proj_z=None,
w_ln_z=None,
b_ln_z=None,
inf=1000000.0,
eps=1e-05,
attn_scale=None,
return_z_proj=False,
is_cached_z_proj=False,
*,
b_proj_k=None,
b_proj_v=None,
w_ln_q=None,
b_ln_q=None,
w_ln_k=None,
b_ln_k=None,
b_proj_g=None,
norm_kind='layer_norm',
)#

Compute attention with a pairwise bias.

Normalizes the single and pair representations according to norm_kind, then computes the Q/K/V and gating projections, pair-bias term, biased and masked softmax attention, output gate, and output projection. The backend selects a supported, profitable optimized route for the current inputs and otherwise uses the native PyTorch implementation (see Notes). Q, K, V and the gate are all projected from the same normalized single_repr (there is no separate s input).

Dimensions: B batch, M multiplicity (single-rep replicas), N sequence length, D single-rep feature dim, H heads, DH head dim (so the attention inner dim is H * DH), z_dim pair feature dim.

Parameters:
  • single_repr (Tensor) – Single/token representation of shape (B * M, N, D). Normalization and the Q/K/V/gate projections are applied to this tensor inside the op (the gate is computed from the normalized representation).

  • pair_repr (Tensor) – Pairwise tensor of shape (B, N, N, z_dim). When is_cached_z_proj is True, pair_repr is instead the already-projected bias of shape (B, H, N, N).

  • mask (Tensor | None) – Attention mask of shape (B, N) or (B * M, N) (0 = masked, 1 = valid). If None, all positions are treated as valid.

  • num_heads (int) – Number of attention heads.

  • w_ln_a (Tensor) – Normalization weight for single_repr of shape (D,).

  • b_ln_a (Tensor | None) – Normalization bias for single_repr of shape (D,). May be None.

  • w_proj_q (Tensor) – Weight for the query projection of shape (H * DH, D).

  • b_proj_q (Tensor | None) – Bias for the query projection of shape (H * DH,). May be None.

  • w_proj_k (Tensor) – Weight for the key projection of shape (H * DH, D).

  • w_proj_v (Tensor) – Weight for the value projection of shape (H * DH, D).

  • w_proj_g (Tensor) – Weight for the gating projection of shape (H * DH, D).

  • w_proj_o (Tensor) – Weight for the output projection of shape (D, H * DH).

  • w_proj_z (Tensor | None) – Weight for the pair projection of shape (H, z_dim). May be None when is_cached_z_proj is True because pair_repr is already projected. Defaults to None.

  • b_proj_o (Tensor | None) – Bias for the output projection of shape (D,). Defaults to None.

  • b_proj_z (Tensor | None) – Bias for the pair projection of shape (H,). Defaults to None.

  • w_ln_z (Tensor | None) – Normalization weight for pair_repr of shape (z_dim,). May be None.

  • b_ln_z (Tensor | None) – Normalization bias for pair_repr of shape (z_dim,). May be None.

  • inf (float) – Large value used for masking invalid attention positions. Defaults to 1e6.

  • eps (float) – Epsilon value for normalization. Defaults to 1e-5.

  • attn_scale (float | None) – Scaling factor for attention scores. If None, uses 1/sqrt(head_dim). Defaults to None.

  • return_z_proj (bool) – Whether to return the projected pair tensor as the second output. Defaults to False.

  • is_cached_z_proj (bool) – Whether pair_repr is already projected and cached. If True, pair_repr should be of shape (B, H, N, N). Defaults to False.

  • b_proj_k (Tensor | None) – Optional key-projection bias of shape (H * DH,). Defaults to None.

  • b_proj_v (Tensor | None) – Optional value-projection bias of shape (H * DH,). Defaults to None.

  • w_ln_q (Tensor | None) – Optional weight for projected-query LayerNorm of shape (H * DH,). The norm is applied over H * DH before splitting heads. Defaults to None.

  • b_ln_q (Tensor | None) – Optional bias for projected-query LayerNorm of shape (H * DH,). Weight and bias are independently optional. Defaults to None.

  • w_ln_k (Tensor | None) – Optional weight for projected-key LayerNorm of shape (H * DH,). The norm is applied over H * DH before splitting heads. Defaults to None.

  • b_ln_k (Tensor | None) – Optional bias for projected-key LayerNorm of shape (H * DH,). Weight and bias are independently optional. Defaults to None.

  • b_proj_g (Tensor | None) – Optional gating-projection bias of shape (H * DH,). Defaults to None.

  • norm_kind (str) – Normalization applied to the single, optional Q/K, and pair representations. "layer_norm" preserves the existing behavior; "rms_norm" requires every normalization bias to be None.

Returns:

Attention output of shape (B * M, N, D) with the

pairwise bias applied.

  • z_proj (torch.Tensor | None): Projected pair tensor of shape (B, H, N, N) reusable via is_cached_z_proj, or None when return_z_proj is False.

Return type:

  • output (torch.Tensor)

Notes

  • The backend chooses among its supported optimized routes according to the device, dtype, shape, gradient mode, and projection options. It uses the native PyTorch implementation whenever no optimized route is supported and profitable. The exact routing policy is an implementation detail.

  • Multiplicity (M) is inferred from the shapes so multiple single-rep replicas can share one pair representation in a single forward pass.