omnibias.torch.fastpath.legendre
PyTorch activation derivatives, trainable operator layers, network jets, and direct differential-operator contractions.
import omnibias.torch.fastpath.legendreModule contract & mathematical conventions
Closed-form derivatives of tanh via the Legendre-style recursion.
Let t = tanh(z). Then t' = 1 - t^2 (a Riccati identity), and
each higher derivative of tanh is a polynomial in t:
tanh^(n)(z) = T_n(t), T_0(t) = t, T_{n+1}(t) = (1 - t^2) * T_n'(t).
Each step of the recursion factors out (1 - t^2), the same weight
appearing in the Legendre orthogonality on [-1, 1] -- hence the
module name. The coefficient generator lives in the canonical
omnibias.core.polynomials module shared by torch and JAX.
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 module | Canonical reference |
|---|---|
tanh_polynomial_coeffs | omnibias.core.tanh_polynomial_coeffs |
tanh_nth_derivative
omnibias.torch.fastpath.legendre.tanh_nth_derivativeSource ↗tanh_nth_derivative(z: Tensor, n: int) -> Tensortanh^(n)(z) evaluated in closed form.
Parameters
| Name | Type & default | Contract |
|---|---|---|
zpositional-or-keyword | TensorRequired | See the operation contract and implementation below. |
npositional-or-keyword | intRequired | See the operation contract and implementation below. |
TensorExplicit validation & failure cases
- When
n < 0, raisesValueError(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 · cmbconv1d shape
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_cmbconv1d_shape() -> None:
layer = cmbConv1d(2, 4, kernel_size=3, padding=1, op="grad", base="tanh")
out = layer(torch.randn(1, 2, 16))
assert out.shape == (1, 4, 16)python -m pytest "packages/omnibias-torch/tests/test_blocks.py::test_cmbconv1d_shape" -qRead the implementation
def tanh_nth_derivative(z: Tensor, n: int) -> Tensor:
"""``tanh^(n)(z)`` evaluated in closed form."""
if n < 0:
raise ValueError(f"order n must be >= 0, got {n}")
t = torch.tanh(z)
if n == 0:
return t
coeffs = tanh_polynomial_coeffs(n)
return _horner(coeffs, t)tanh_tower
omnibias.torch.fastpath.legendre.tanh_towerSource ↗tanh_tower(z: Tensor, max_order: int) -> TensorAll derivatives through max_order from one native tanh evaluation.
Parameters
| Name | Type & default | Contract |
|---|---|---|
zpositional-or-keyword | TensorRequired | See the operation contract and implementation below. |
max_orderpositional-or-keyword | intRequired | See the operation contract and implementation below. |
TensorExplicit validation & failure cases
- When
max_order < 0, raisesValueError('max_order must be nonnegative').
These are explicit source guards. Backend operations may also raise for invalid shapes, dtypes, or unsupported devices.
Executed regression · hessian requires order two
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_hessian_requires_order_two() -> None:
layers, x0 = _build_mlp(seed=6)
jet = mlp_jet_mv(x0, layers, 1)
with pytest.raises(ValueError, match="hessian needs order >= 2"):
jet_hessian(jet, int(x0.shape[0]), 1)python -m pytest "packages/omnibias-torch/tests/test_jet_mv.py::test_hessian_requires_order_two" -qRead the implementation
def tanh_tower(z: Tensor, max_order: int) -> Tensor:
"""All derivatives through max_order from one native tanh evaluation."""
if max_order < 0:
raise ValueError("max_order must be nonnegative")
value = torch.tanh(z)
rows = [value] + [_horner(tanh_polynomial_coeffs(n), value) for n in range(1, max_order + 1)]
return torch.stack(rows, dim=0)