Skip to main content

omnibias.struct.torch.tropical

0.1.0a2AlphaPython >=3.10AGPL / commercial

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

import omnibias.struct.torch.tropical
Module contract & mathematical conventions

Tropical homotopy twins (torch; theory 01-08).

beta -> inf is temperature collapse, not founding delta -> 0. Do not conflate the two.

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

relaxed_value​

omnibias.struct.torch.tropical.relaxed_valueSource ↗
relaxed_value(poly: TropicalLinear, x: Tensor, *, beta: float) -> Tensor

The module contract describes the context for this operation. Its signature, explicit validation, return expressions, and implementation are recorded below.

Parameters

NameType & defaultContract
polypositional-or-keywordTropicalLinearRequiredSee the operation contract and implementation below.
xpositional-or-keywordTensorRequiredSee the operation contract and implementation below.
betakeyword-onlyfloatRequiredSee the operation contract and implementation below.
ReturnsTensor
Executed regression · g4 parity

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_g4_parity() -> None:
    pytest.importorskip("jax")
    import jax
    import jax.numpy as jnp
    import torch
    from omnibias.struct.jax.tropical import relaxed_value as rv_jax
    from omnibias.struct.torch.tropical import relaxed_value as rv_torch

    jax.config.update("jax_enable_x64", True)
    poly = _poly()
    x = np.array([[0.1, -0.2], [0.3, 0.4]])
    t = rv_torch(poly, torch.as_tensor(x, dtype=torch.float64), beta=2.0)
    j = rv_jax(poly, jnp.asarray(x, dtype=jnp.float64), beta=2.0)
    np.testing.assert_allclose(t.detach().cpu().numpy(), np.asarray(j), rtol=0.0, atol=1e-12)
python -m pytest "packages/omnibias-struct/tests/test_tropical.py::test_g4_parity" -q
Read the implementation
def relaxed_value(poly: TropicalLinear, x: Tensor, *, beta: float) -> Tensor:
    return logsumexp_beta(scores(poly, x), beta, axis=-1)
FUNCTION

relaxed_weights​

omnibias.struct.torch.tropical.relaxed_weightsSource ↗
relaxed_weights(poly: TropicalLinear, x: Tensor, *, beta: float) -> Tensor

The module contract describes the context for this operation. Its signature, explicit validation, return expressions, and implementation are recorded below.

Parameters

NameType & defaultContract
polypositional-or-keywordTropicalLinearRequiredSee the operation contract and implementation below.
xpositional-or-keywordTensorRequiredSee the operation contract and implementation below.
betakeyword-onlyfloatRequiredSee the operation contract and implementation below.
ReturnsTensor
Read the implementation
def relaxed_weights(poly: TropicalLinear, x: Tensor, *, beta: float) -> Tensor:
    return softmax_beta(scores(poly, x), beta, axis=-1)
FUNCTION

scores​

omnibias.struct.torch.tropical.scoresSource ↗
scores(poly: TropicalLinear, x: Tensor) -> Tensor

The module contract describes the context for this operation. Its signature, explicit validation, return expressions, and implementation are recorded below.

Parameters

NameType & defaultContract
polypositional-or-keywordTropicalLinearRequiredSee the operation contract and implementation below.
xpositional-or-keywordTensorRequiredSee the operation contract and implementation below.
ReturnsTensor
Executed regression · g4 parity

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_g4_parity() -> None:
    pytest.importorskip("jax")
    import jax
    import jax.numpy as jnp
    import torch
    from omnibias.struct.jax.tropical import relaxed_value as rv_jax
    from omnibias.struct.torch.tropical import relaxed_value as rv_torch

    jax.config.update("jax_enable_x64", True)
    poly = _poly()
    x = np.array([[0.1, -0.2], [0.3, 0.4]])
    t = rv_torch(poly, torch.as_tensor(x, dtype=torch.float64), beta=2.0)
    j = rv_jax(poly, jnp.asarray(x, dtype=jnp.float64), beta=2.0)
    np.testing.assert_allclose(t.detach().cpu().numpy(), np.asarray(j), rtol=0.0, atol=1e-12)
python -m pytest "packages/omnibias-struct/tests/test_tropical.py::test_g4_parity" -q
Read the implementation
def scores(poly: TropicalLinear, x: Tensor) -> Tensor:
    m = torch.as_tensor(poly.exponents, dtype=x.dtype, device=x.device)
    a = torch.as_tensor(poly.coeffs, dtype=x.dtype, device=x.device)
    xv = x if x.ndim == 2 else x.unsqueeze(0)
    return a + xv @ m.T