Compute Utils#
utils
#
| FUNCTION | DESCRIPTION |
|---|---|
check_random_state |
Set random seed across all random number generators. |
outer |
Compute outer product between tensors using broadcasting. |
safe_xlogy |
Compute x * log(y) safely, handling x=0 case. |
safe_broadcast |
Broadcast parameter tensor to matrix shape. |
stable_cumsum |
Compute cumulative sum with high precision and stability validation. |
Functions#
check_random_state(random_state: int = 111) -> None
#
Set random seed across all random number generators.
This function sets seeds for Python's random module, NumPy, PyTorch (CPU/CUDA), and PyTorch Lightning to ensure reproducible results across the entire stack.
| PARAMETER | DESCRIPTION |
|---|---|
random_state
|
Random seed value, by default 111.
TYPE:
|
Source code in spectre/compute/utils.py
outer(X: torch.Tensor, Y: torch.Tensor | None = None, flatten: bool = False) -> torch.Tensor
#
Compute outer product between tensors using broadcasting.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
First tensor with shape (n,).
TYPE:
|
Y
|
Second tensor with shape (m,). If None, uses X, by default None.
TYPE:
|
flatten
|
Whether the result must be flattened.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Outer product with shape (n, m). |
Examples:
>>> X = torch.tensor([1, 2, 3])
>>> Y = torch.tensor([4, 5])
>>> result = outer(X, Y)
>>> result.shape
torch.Size([3, 2])
Source code in spectre/compute/utils.py
safe_xlogy(x: torch.Tensor, y: torch.Tensor | None = None) -> torch.Tensor
#
Compute x * log(y) safely, handling x=0 case.
When x=0, returns 0 regardless of y value, avoiding NaN from 0*log(0).
| PARAMETER | DESCRIPTION |
|---|---|
x
|
First tensor.
TYPE:
|
y
|
Second tensor. If None, uses x, by default None.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Safe x * log(y) computation with same shape as inputs. |
Examples:
>>> x = torch.tensor([0.0, 1.0, 2.0])
>>> y = torch.tensor([0.0, 1.0, 2.0])
>>> result = safe_xlogy(x, y)
>>> # Returns [0.0, 0.0, log(4)] instead of [NaN, 0.0, log(4)]
Source code in spectre/compute/utils.py
safe_broadcast(param: torch.Tensor, D: torch.Tensor, param_name: str = 'parameter') -> torch.Tensor
#
Broadcast parameter tensor to matrix shape.
Supports scalar, row-wise (n, 1), column-wise (1, m), and full matrix (n, m) parameter shapes.
| PARAMETER | DESCRIPTION |
|---|---|
param
|
Parameter tensor to broadcast.
TYPE:
|
D
|
Matrix of shape (n_samples, m_samples).
TYPE:
|
param_name
|
Name of parameter for error messages.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Broadcast parameter tensor of shape (n_samples, m_samples). |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If parameter shape cannot be broadcast to matrix shape. |
Source code in spectre/compute/utils.py
stable_cumsum(arr: torch.Tensor, dim: int | None = None, rtol: float = 1e-05, atol: float = 1e-08, high_precision: bool = True) -> torch.Tensor
#
Compute cumulative sum with high precision and stability validation.
Uses high precision (float64) for cumsum computation and validates that the final value matches the sum to ensure numerical stability.
| PARAMETER | DESCRIPTION |
|---|---|
arr
|
Tensor to be cumulatively summed.
TYPE:
|
dim
|
Dimension along which cumulative sum is computed. If None, computes over flattened array, by default None.
TYPE:
|
rtol
|
Relative tolerance for stability check, by default 1e-05.
TYPE:
|
atol
|
Absolute tolerance for stability check, by default 1e-08.
TYPE:
|
high_precision
|
Whether to use float64 for cumsum computation. If False, uses input dtype, by default True.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Cumulative sum with same shape as input (or flattened if dim=None). |
| WARNS | DESCRIPTION |
|---|---|
RuntimeWarning
|
If cumsum is found to be unstable (final element does not match sum). |
Examples:
>>> arr = torch.tensor([1.0, 2.0, 3.0])
>>> cumsum = stable_cumsum(arr)
>>> cumsum
tensor([1., 3., 6.], dtype=torch.float64)