Skip to main content

omnibias.torch.fastpath.hermite

0.5.0rc1Release candidatePython >=3.10Apache-2.0

PyTorch activation derivatives, trainable operator layers, network jets, and direct differential-operator contractions.

import omnibias.torch.fastpath.hermite
Module contract & mathematical conventions

Closed-form derivatives of the Gaussian activation via probabilist's Hermite polynomials.

Let g(z) = e^{-z^2/2} be the (unnormalised) Gaussian. Then

g^(n)(z) = (-1)^n * He_n(z) * g(z),

where He_n is the probabilist's Hermite polynomial obeying the three-term recurrence

He_0(z) = 1, He_1(z) = z, He_{n+1}(z) = z * He_n(z) - n * He_{n-1}(z).

The coefficient generator lives in the canonical omnibias.core.polynomials module shared by torch and JAX. The cost per call is one exp plus O(n) multiply-adds in Horner form.

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.

Imports & aliases​

These names share a definition with another public module. Follow a name for its full contract, parameters, and examples.

Import from this moduleCanonical reference
hermite_coeffsomnibias.core.hermite_coeffs
FUNCTION

gaussian_forward​

omnibias.torch.fastpath.hermite.gaussian_forwardSource ↗
gaussian_forward(z: Tensor) -> Tensor

g(z) = exp(-z^2/2); provided here so callers do not need to duplicate the formula.

Parameters

NameType & defaultContract
zpositional-or-keywordTensorRequiredSee the operation contract and implementation below.
ReturnsTensor
Executed regression · every registered activation can be constructed

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_every_registered_activation_can_be_constructed(name: str) -> None:
    ombu = OperatorMultiBiasUnit(num_channels=2, K=2, base=name)
    z = torch.randn(3, 2)
    out = ombu(z)
    assert out.shape == (3, 2)
    assert torch.isfinite(out).all()
python -m pytest "packages/omnibias-torch/tests/test_unit.py::test_every_registered_activation_can_be_constructed[gaussian]" -q
Read the implementation
def gaussian_forward(z: Tensor) -> Tensor:
    """``g(z) = exp(-z^2/2)``; provided here so callers do not need to
    duplicate the formula."""
    return torch.exp(-0.5 * z * z)
FUNCTION

gaussian_nth_derivative​

omnibias.torch.fastpath.hermite.gaussian_nth_derivativeSource ↗
gaussian_nth_derivative(z: Tensor, n: int) -> Tensor

g^(n)(z) where g(z) = exp(-z^2/2).

Uses the identity g^(n)(z) = (-1)^n * He_n(z) * g(z).

Parameters

NameType & defaultContract
zpositional-or-keywordTensorRequiredSee the operation contract and implementation below.
npositional-or-keywordintRequiredSee the operation contract and implementation below.
ReturnsTensor
Explicit validation & failure cases
  • When n < 0, raises ValueError(f'order n must be >= 0, got {n}').

These are explicit source guards. Backend operations may also raise for invalid shapes, dtypes, or unsupported devices.

Executed regression · order1 is antisymmetric zero mean

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_order1_is_antisymmetric_zero_mean() -> None:
    taps = analytic_gaussian_taps(21, torch.tensor([1.3], dtype=DT), 1)[0]
    torch.testing.assert_close(taps, -taps.flip(0), rtol=1e-12, atol=1e-12)  # odd
    assert float(taps.sum()) == pytest.approx(0.0, abs=1e-12)
python -m pytest "packages/omnibias-torch/tests/test_analytic_gaussian_conv.py::test_order1_is_antisymmetric_zero_mean" -q
Regression example · dog equals closed form gaussian tower

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_dog_equals_closed_form_gaussian_tower() -> None:
    """Derivative-of-Gaussian taps are the closed-form sigma tower (no sampling)."""
    from omnibias.torch.fastpath.hermite import gaussian_nth_derivative

    K, sigma = 11, 1.4
    half = K // 2
    offs = torch.arange(-half, half + 1, dtype=DT)
    a = (offs - 0.5) / sigma
    b = (offs + 0.5) / sigma
    for order in (1, 2, 3, 4):
        taps = analytic_gaussian_taps(K, torch.tensor([sigma], dtype=DT), order)[0]
        tower = (
            gaussian_nth_derivative(b, order - 1) - gaussian_nth_derivative(a, order - 1)
        ) / (sigma**order * _SQRT_2PI)
        torch.testing.assert_close(taps, tower, rtol=1e-12, atol=1e-12)
python -m pytest packages/omnibias-torch/tests/test_analytic_gaussian_conv.py -k test_dog_equals_closed_form_gaussian_tower -q
Read the implementation
def gaussian_nth_derivative(z: Tensor, n: int) -> Tensor:
    """``g^(n)(z)`` where ``g(z) = exp(-z^2/2)``.

    Uses the identity ``g^(n)(z) = (-1)^n * He_n(z) * g(z)``.
    """
    if n < 0:
        raise ValueError(f"order n must be >= 0, got {n}")
    g = torch.exp(-0.5 * z * z)
    if n == 0:
        return g
    coeffs = hermite_coeffs(n)
    poly = _horner(coeffs, z)
    sign = -1.0 if (n & 1) else 1.0
    return sign * poly * g