xnn.common.models.ops.segment_softmax#

xnn.common.models.ops.segment_softmax(logits, index, num_segments, weight=None)[source]#

Softmax of row logits over the rows that share a segment.

Without weight this is a_r = exp(z_r) / sum_s exp(z_s) over the rows s of the segment of row r: the attention normalization of the Equiformers (rows are edges, segments their receiving atoms) and of the Set2Set readout of MEGNet (rows are atoms or bonds, segments their structures). With a per-row weight u_r (a cutoff envelope) it becomes a_r = u_r exp(z_r) / (1 + sum_s u_s exp(z_s)): a neighbor leaving the cutoff sphere fades out instead of dropping from the normalization, and the null term (a key of logit zero and zero value) lets the last neighbor fade out too, so the attention is continuous in the positions.

Parameters:
  • logits (Tensor) – Row logits (R, H).

  • index (Tensor) – Segment of every row (R,).

  • num_segments (int) – Number of segments.

  • weight (Tensor, optional) – Per-row weights (R,) in [0, 1].

Returns:

The normalized weights (R, H).

Return type:

Tensor