spd_learn.functional.get_epsilon#

spd_learn.functional.get_epsilon(dtype: dtype, name: Literal['eigval_clamp', 'eigval_log', 'eigval_sqrt', 'eigval_inv_sqrt', 'eigval_power', 'loewner_equal', 'batchnorm_var', 'dropout', 'trace_norm', 'stiefel_init', 'division_safe'] = 'eigval_clamp', *, config: NumericalConfig | None = None) → float[source]#

Get a dtype-aware epsilon value for numerical stability.

This function returns an appropriate epsilon value based on the data type and the intended use case. It scales the machine epsilon by a factor that ensures numerical stability for the specific operation.

Parameters:
  • dtype (torch.dtype) – The PyTorch dtype to compute epsilon for.

  • name (ThresholdName, default="eigval_clamp") –

    The type of threshold to compute. Options are:

    • "eigval_clamp": General eigenvalue clamping (ReEig layer)

    • "eigval_log": Eigenvalue clamping before log operation

    • "eigval_sqrt": Eigenvalue clamping before sqrt operation

    • "eigval_inv_sqrt": Eigenvalue clamping before inverse sqrt

    • "eigval_power": Eigenvalue clamping before power operation

    • "loewner_equal": Detection of equal eigenvalues in Loewner matrix

    • "batchnorm_var": Batch normalization variance epsilon

    • "dropout": Dropout diagonal epsilon

    • "trace_norm": Trace normalization epsilon

    • "stiefel_init": Stiefel manifold initialization

    • "division_safe": Safe division operations

  • config (NumericalConfig, optional) – Configuration to use. If None, uses the global numerical_config.

Returns:

The computed epsilon value.

Return type:

float

Examples

>>> import torch
>>> from spd_learn.functional.numerical import get_epsilon
>>> # Get epsilon for float32 eigenvalue clamping
>>> eps32 = get_epsilon(torch.float32, "eigval_clamp")
>>> print(f"float32 eigval_clamp: {eps32:.2e}")
float32 eigval_clamp: 1.19e-03
>>> # Get epsilon for float64 (more precise)
>>> eps64 = get_epsilon(torch.float64, "eigval_clamp")
>>> print(f"float64 eigval_clamp: {eps64:.2e}")
float64 eigval_clamp: 2.22e-12
>>> # float16 needs larger epsilon
>>> eps16 = get_epsilon(torch.float16, "eigval_clamp")
>>> print(f"float16 eigval_clamp: {eps16:.2e}")
float16 eigval_clamp: 9.77e+00

See also

get_epsilon_tensor

Returns epsilon as a tensor on the correct device.

numerical_config

Global configuration for threshold scales.