Skip to main content

omnibias.struct.jax.attention

0.1.0a2AlphaPython >=3.10AGPL / commercial

Differentiable dynamic programs for sequences and parses, structured distributions, and finite-temperature bounds.

import omnibias.struct.jax.attention
Module contract & mathematical conventions

Structured attention as linear-chain soft-DP marginals (jax).

Bit-identical twin of omnibias.struct.torch.attention (float64; needs jax_enable_x64). Ordinary attention is an independent per-row softmax; **structured attention** couples the rows with a transition score and reads the attention weights off the linear-chain *marginals* (omnibias.struct.jax.soft_viterbi_marginals). With zero transitions it collapses back to plain row-wise softmax(beta * scores); as beta -> inf it concentrates on the Viterbi path.

Shared formulas do not imply universal floating-point identity. Verify dtype, shape, derivative-order support, and the stated numerical assumptions for the path you use.

FUNCTION

structured_attention​

omnibias.struct.jax.structured_attentionSource ↗
structured_attention(scores: Array, transitions: Array, beta: float=1.0, *, start: Array | None=None) -> Array

Structured attention weights (T, S) -- linear-chain marginals of scores.

scores are per-position, per-key logits (T, S) and transitions (S, S) couple adjacent positions; the returned weights are P_beta(key s at position t), each row summing to 1. Zero transitions recovers independent softmax(beta * scores) attention; a non-zero transition biases attention towards structurally coherent paths.

Parameters

NameType & defaultContract
scorespositional-or-keywordArrayRequiredSee the operation contract and implementation below.
transitionspositional-or-keywordArrayRequiredSee the operation contract and implementation below.
betapositional-or-keywordfloatDefault: 1.0See the operation contract and implementation below.
startkeyword-onlyArray | NoneDefault: NoneSee the operation contract and implementation below.
ReturnsArray
Executed regression · structured attention parity and matches marginals

Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.

def test_structured_attention_parity_and_matches_marginals() -> None:
    rng = np.random.default_rng(5)
    scores = rng.standard_normal((5, 3))
    trans = rng.standard_normal((3, 3))
    beta = 1.5
    a_t = tattn(torch.tensor(scores), torch.tensor(trans), beta).numpy()
    a_j = np.asarray(jattn(jnp.asarray(scores), jnp.asarray(trans), beta))
    assert np.max(np.abs(a_t - a_j)) < 1e-9
    # It *is* the linear-chain marginal, by construction.
    marg = np.asarray(jsvm(jnp.asarray(scores), jnp.asarray(trans), beta))
    assert np.max(np.abs(a_j - marg)) < 1e-9
    _ = jsoftmax  # jax softmax import kept for symmetry with the torch oracle
python -m pytest "packages/omnibias-struct/tests/test_plan_attn.py::test_structured_attention_parity_and_matches_marginals" -q
Read the implementation
def structured_attention(
    scores: Array,
    transitions: Array,
    beta: float = 1.0,
    *,
    start: Array | None = None,
) -> Array:
    r"""Structured attention weights ``(T, S)`` -- linear-chain marginals of ``scores``.

    ``scores`` are per-position, per-key logits ``(T, S)`` and ``transitions`` ``(S, S)``
    couple adjacent positions; the returned weights are ``P_beta(key s at position t)``, each
    row summing to ``1``. Zero ``transitions`` recovers independent ``softmax(beta * scores)``
    attention; a non-zero transition biases attention towards structurally coherent paths.
    """
    return soft_viterbi_marginals(scores, transitions, beta, start=start)
Other public import paths
  • omnibias.struct.jax.structured_attention
  • omnibias.struct.jax.attention.structured_attention
FUNCTION

structured_attention_batched​

omnibias.struct.jax.structured_attention_batchedSource ↗
structured_attention_batched(scores: Array, transitions: Array, beta: float=1.0, *, start: Array | None=None) -> Array

Batched structured_attention -> (B, T, S) for scores (B, T, S).

transitions may be shared (S, S) or per-example (B, S, S); start may be None, shared (S,), or per-example (B, S). Maps the marginals over the leading batch axis with jax.vmap -- bit-identical to looping.

Parameters

NameType & defaultContract
scorespositional-or-keywordArrayRequiredSee the operation contract and implementation below.
transitionspositional-or-keywordArrayRequiredSee the operation contract and implementation below.
betapositional-or-keywordfloatDefault: 1.0See the operation contract and implementation below.
startkeyword-onlyArray | NoneDefault: NoneSee the operation contract and implementation below.
ReturnsArray
Read the implementation
def structured_attention_batched(
    scores: Array,
    transitions: Array,
    beta: float = 1.0,
    *,
    start: Array | None = None,
) -> Array:
    r"""Batched :func:`structured_attention` -> ``(B, T, S)`` for ``scores`` ``(B, T, S)``.

    ``transitions`` may be shared ``(S, S)`` or per-example ``(B, S, S)``; ``start`` may be
    ``None``, shared ``(S,)``, or per-example ``(B, S)``. Maps the marginals over the leading
    batch axis with ``jax.vmap`` -- bit-identical to looping.
    """
    import jax

    t_dim = 0 if transitions.ndim == 3 else None
    if start is None:
        out: Array = jax.vmap(
            lambda sc, tr: structured_attention(sc, tr, beta), in_axes=(0, t_dim)
        )(scores, transitions)
        return out
    s_dim = 0 if start.ndim == 2 else None
    out_start: Array = jax.vmap(
        lambda sc, tr, st: structured_attention(sc, tr, beta, start=st), in_axes=(0, t_dim, s_dim)
    )(scores, transitions, start)
    return out_start
Other public import paths
  • omnibias.struct.jax.structured_attention_batched
  • omnibias.struct.jax.attention.structured_attention_batched