Fractional Neural Networks API Reference

ML layers, spectral autograd, stochastic memory sampling, and probabilistic fractional orders.

Spectral Autograd

Spectral fractional calculus utilities for ML integration.

This module provides a small, self-contained implementation that satisfies the expectations encoded in the extensive test-suite that accompanies the library. It focuses on three primary responsibilities:

  • Safe FFT utilities with backend selection and graceful fallbacks.

  • Spectral fractional derivative helpers that operate on PyTorch tensors while remaining differentiable with respect to both the input and the fractional order parameter alpha.

  • Thin neural-network wrappers (layers, learnable alpha parameters, and simple network scaffolding) that integrate the derivative into PyTorch models.

The goal is functional correctness and API compatibility rather than raw numerical performance; consequently many routines favour clarity and predictability over micro-optimisation. Every public utility is intentionally simple and well-behaved so that the surrounding tests can exercise a broad range of scenarios (different dtypes, devices, edge-cases, etc.).

hpfracc.ml.spectral_autograd.set_fft_backend(backend)[source]

Select the preferred FFT backend.

Parameters:

backend (str) – One of the identifiers listed in _ALLOWED_BACKENDS. The value is stored verbatim (after lower-casing) for retrieval via get_fft_backend(), while internal helpers map it to the effective behaviour (Torch FFT, NumPy FFT, robust fallback, etc.).

Return type:

str

hpfracc.ml.spectral_autograd.get_fft_backend()[source]

Return the currently configured FFT backend identifier.

Return type:

str

hpfracc.ml.spectral_autograd.safe_fft(x, dim=-1, norm='ortho', backend=None)[source]

FFT helper that honours the configured backend and preserves dtype.

Parameters:
  • x (torch.Tensor)

  • dim (int)

  • norm (str)

  • backend (str | None)

Return type:

torch.Tensor

hpfracc.ml.spectral_autograd.safe_ifft(x, dim=-1, norm='ortho', backend=None)[source]
Parameters:
  • x (torch.Tensor)

  • dim (int)

  • norm (str)

  • backend (str | None)

Return type:

torch.Tensor

hpfracc.ml.spectral_autograd.robust_fft(x, dim=-1, norm='ortho')[source]

FFT with an automatic fallback to NumPy when PyTorch fails.

Parameters:
  • x (torch.Tensor)

  • dim (int)

  • norm (str)

Return type:

torch.Tensor

hpfracc.ml.spectral_autograd.robust_ifft(x, dim=-1, norm='ortho')[source]
Parameters:
  • x (torch.Tensor)

  • dim (int)

  • norm (str)

Return type:

torch.Tensor

hpfracc.ml.spectral_autograd.spectral_fractional_derivative(x, alpha, kernel_type='riesz', dim=-1, **kwargs)[source]

Dispatcher for spectral fractional derivative. Selects backend based on input tensor type.

Parameters:
Return type:

torch.Tensor | jax.Array

hpfracc.ml.spectral_autograd.fractional_derivative(x, alpha, kernel_type='riesz', dim=-1, norm='ortho', backend=None, epsilon=1e-06)[source]

Public alias used throughout the tests.

Parameters:
Return type:

torch.Tensor

class hpfracc.ml.spectral_autograd.SpectralFractionalDerivative[source]

Bases: object

Callable wrapper that mimics the autograd Function.apply interface.

static apply(x, alpha, kernel_type='riesz', dim=-1, norm='ortho', backend=None, epsilon=1e-06)[source]
Parameters:
Return type:

torch.Tensor

class hpfracc.ml.spectral_autograd.SpectralFractionalFunction[source]

Bases: object

Legacy-style interface exposing explicit forward/backward hooks.

static forward(x, alpha, **kwargs)[source]
Parameters:
  • x (torch.Tensor)

  • alpha (int | float | torch.Tensor)

Return type:

torch.Tensor

static backward(grad_output, alpha, **kwargs)[source]
Parameters:
  • grad_output (torch.Tensor)

  • alpha (int | float | torch.Tensor)

Return type:

torch.Tensor

class hpfracc.ml.spectral_autograd.SpectralFractionalLayer(*args, **kwargs)[source]

Bases: Module

Apply a spectral fractional derivative inside a PyTorch layer.

Parameters:
  • input_size (Optional[int])

  • output_size (Optional[int])

  • alpha (_Alpha)

  • kernel_type (str)

  • dim (_DimType)

  • norm (str)

  • backend (_Backend)

  • epsilon (float)

  • learnable_alpha (bool)

__init__(input_size=None, output_size=None, alpha=0.5, kernel_type='riesz', dim=-1, norm='ortho', backend=None, epsilon=1e-06, learnable_alpha=False, **kwargs)[source]
Parameters:
Return type:

None

property alpha: float
property learnable: bool
get_alpha()[source]
Return type:

float | torch.Tensor

forward(x)[source]
Parameters:

x (torch.Tensor)

Return type:

torch.Tensor

class hpfracc.ml.spectral_autograd.SpectralFractionalNetwork(*args, **kwargs)[source]

Bases: Module

Simple network that incorporates spectral fractional layers.

Modes - unified (default): unified adaptive framework (input_dim, hidden_dims, output_dim). - model: model-specific/coverage style (input_size, hidden_sizes, output_size).

Backends - torch (default), jax, numba. If unavailable, CPU-safe fallbacks are used.

Parameters:
  • input_size (Optional[int])

  • hidden_sizes (Optional[Sequence[int]])

  • output_size (Optional[int])

  • alpha (_Alpha)

  • input_dim (Optional[int])

  • hidden_dims (Optional[Sequence[int]])

  • output_dim (Optional[int])

  • kernel_type (str)

  • activation (Union[str, nn.Module, None])

  • learnable_alpha (bool)

  • backend (_Backend)

  • norm (str)

  • epsilon (float)

  • mode (str)

__init__(input_size=None, hidden_sizes=None, output_size=None, alpha=0.5, *, input_dim=None, hidden_dims=None, output_dim=None, kernel_type='riesz', activation='relu', learnable_alpha=False, backend=None, norm='ortho', epsilon=1e-06, mode='unified', **kwargs)[source]
Parameters:
  • input_size (int | None)

  • hidden_sizes (Sequence[int] | None)

  • output_size (int | None)

  • alpha (int | float | torch.Tensor)

  • input_dim (int | None)

  • hidden_dims (Sequence[int] | None)

  • output_dim (int | None)

  • kernel_type (str)

  • activation (str | torch.nn.Module | None)

  • learnable_alpha (bool)

  • backend (str | None)

  • norm (str)

  • epsilon (float)

  • mode (str)

Return type:

None

forward(x)[source]
Parameters:

x (torch.Tensor)

Return type:

torch.Tensor

class hpfracc.ml.spectral_autograd.BoundedAlphaParameter(*args, **kwargs)[source]

Bases: Module

Learnable scalar constrained to the open interval (alpha_min, alpha_max).

Parameters:
__init__(alpha_init=0.5, alpha_min=0.001, alpha_max=1.999)[source]
Parameters:
Return type:

None

forward()[source]
Return type:

torch.Tensor

extra_repr()[source]
Return type:

str

hpfracc.ml.spectral_autograd.create_fractional_layer(input_size=None, *, alpha=0.5, kernel_type='riesz', dim=-1, norm='ortho', backend=None, epsilon=1e-06, learnable_alpha=False)[source]
Parameters:
Return type:

SpectralFractionalLayer

hpfracc.ml.spectral_autograd.benchmark_backends(x, alpha, *, iterations=10, kernel_type='riesz', dim=-1, norm='ortho', epsilon=1e-06)[source]

Crude benchmarking helper used in documentation and diagnostics.

Parameters:
Return type:

dict

hpfracc.ml.spectral_autograd.original_set_fft_backend(backend)[source]
Parameters:

backend (str)

Return type:

str

hpfracc.ml.spectral_autograd.original_get_fft_backend()[source]
Return type:

str

hpfracc.ml.spectral_autograd.original_safe_fft(x, dim=-1, norm='ortho', backend=None)[source]
Parameters:
  • x (torch.Tensor)

  • dim (int)

  • norm (str)

  • backend (str | None)

Return type:

torch.Tensor

hpfracc.ml.spectral_autograd.original_safe_ifft(x, dim=-1, norm='ortho', backend=None)[source]
Parameters:
  • x (torch.Tensor)

  • dim (int)

  • norm (str)

  • backend (str | None)

Return type:

torch.Tensor

hpfracc.ml.spectral_autograd.original_get_fractional_kernel(alpha, n, kernel_type='riesz', epsilon=1e-06, dtype=None, device=None)[source]
Parameters:
  • alpha (int | float | torch.Tensor)

  • n (int)

  • kernel_type (str)

  • epsilon (float)

  • dtype (torch.dtype | None)

  • device (torch.device | None)

Return type:

torch.Tensor

hpfracc.ml.spectral_autograd.original_spectral_fractional_derivative(x, alpha, kernel_type='riesz', dim=-1, norm='ortho', backend=None, epsilon=1e-06)[source]
Parameters:
Return type:

torch.Tensor

hpfracc.ml.spectral_autograd.OriginalSpectral

alias of SpectralFractionalDerivative

hpfracc.ml.spectral_autograd.OriginalSpectralFractionalLayer

alias of SpectralFractionalLayer

hpfracc.ml.spectral_autograd.OriginalSpectralFractionalNetwork

alias of SpectralFractionalNetwork

hpfracc.ml.spectral_autograd.original_create_fractional_layer(input_size=None, *, alpha=0.5, kernel_type='riesz', dim=-1, norm='ortho', backend=None, epsilon=1e-06, learnable_alpha=False)
Parameters:
Return type:

SpectralFractionalLayer

hpfracc.ml.spectral_autograd._numpy_fft(x, dim=-1, norm='ortho')[source]

Compute an FFT via NumPy, preserving dtype and device.

This path intentionally leaves the PyTorch autograd graph, as it is used exclusively as a robustness fallback when Torch’s FFT is unavailable or explicitly bypassed by tests.

Parameters:
  • x (torch.Tensor)

  • dim (int)

  • norm (str)

Return type:

torch.Tensor

Stochastic Memory Sampling

Stochastic Memory Sampling for Fractional Derivatives

This module implements unbiased estimators for fractional derivatives using stochastic sampling of the memory history instead of full computation.

class hpfracc.ml.stochastic_memory_sampling.StochasticMemorySampler(alpha, method='importance', **kwargs)[source]

Bases: object

Base class for stochastic memory sampling strategies.

Parameters:
__init__(alpha, method='importance', **kwargs)[source]
Parameters:
sample_indices(n, k)[source]

Sample k indices from history of length n.

Parameters:
Return type:

torch.Tensor

compute_weights(indices, n)[source]

Compute importance weights for sampled indices.

Parameters:
  • indices (torch.Tensor)

  • n (int)

Return type:

torch.Tensor

estimate_derivative(x, indices, weights)[source]

Estimate fractional derivative using sampled indices and weights.

Parameters:
  • x (torch.Tensor)

  • indices (torch.Tensor)

  • weights (torch.Tensor)

Return type:

torch.Tensor

class hpfracc.ml.stochastic_memory_sampling.ImportanceSampler(alpha, tau=0.1, **kwargs)[source]

Bases: StochasticMemorySampler

Importance sampling for fractional derivative memory.

Uses power-law distribution p(j) ∝ (n-j)^(-(1+α-τ)) where τ controls the tempering of the heavy tail.

Parameters:
__init__(alpha, tau=0.1, **kwargs)[source]
Parameters:
sample_indices(n, k)[source]

Sample indices using importance sampling distribution.

Parameters:
Return type:

torch.Tensor

compute_weights(indices, n)[source]

Compute importance weights w(j)/p(j).

Parameters:
  • indices (torch.Tensor)

  • n (int)

Return type:

torch.Tensor

estimate_derivative(x, indices, weights)[source]

Estimate fractional derivative using importance sampling.

Parameters:
  • x (torch.Tensor)

  • indices (torch.Tensor)

  • weights (torch.Tensor)

Return type:

torch.Tensor

class hpfracc.ml.stochastic_memory_sampling.StratifiedSampler(alpha, recent_window=32, tail_ratio=0.3, **kwargs)[source]

Bases: StochasticMemorySampler

Stratified sampling with recent window and tail sampling.

Samples densely from recent history and sparsely from tail.

Parameters:
__init__(alpha, recent_window=32, tail_ratio=0.3, **kwargs)[source]
Parameters:
sample_indices(n, k)[source]

Sample indices using stratified sampling.

Parameters:
Return type:

torch.Tensor

compute_weights(indices, n)[source]

Compute weights for stratified sampling.

Parameters:
  • indices (torch.Tensor)

  • n (int)

Return type:

torch.Tensor

estimate_derivative(x, indices, weights)[source]

Estimate fractional derivative using stratified sampling.

Parameters:
  • x (torch.Tensor)

  • indices (torch.Tensor)

  • weights (torch.Tensor)

Return type:

torch.Tensor

class hpfracc.ml.stochastic_memory_sampling.ControlVariateSampler(alpha, baseline_window=16, **kwargs)[source]

Bases: StochasticMemorySampler

Control variate sampling with deterministic baseline.

Uses a cheap deterministic approximation (e.g., short memory) as baseline and samples only the residual tail.

Parameters:
__init__(alpha, baseline_window=16, **kwargs)[source]
Parameters:
compute_baseline(x)[source]

Compute deterministic baseline using recent window.

Parameters:

x (torch.Tensor)

Return type:

torch.Tensor

sample_indices(n, k)[source]

Sample indices from tail only (excluding baseline window).

Parameters:
Return type:

torch.Tensor

compute_weights(indices, n)[source]

Compute weights for control variate sampling.

Parameters:
  • indices (torch.Tensor)

  • n (int)

Return type:

torch.Tensor

estimate_derivative(x, indices, weights)[source]

Estimate derivative using control variate method.

Parameters:
  • x (torch.Tensor)

  • indices (torch.Tensor)

  • weights (torch.Tensor)

Return type:

torch.Tensor

class hpfracc.ml.stochastic_memory_sampling.StochasticFractionalDerivative(*args, **kwargs)[source]

Bases: Function

PyTorch autograd function for stochastic fractional derivatives.

Parameters:
  • args (Any)

  • kwargs (Any)

Return type:

Any

static forward(ctx, x, alpha, k=64, method='importance', **sampler_kwargs)[source]

Forward pass with stochastic memory sampling.

Parameters:
Return type:

torch.Tensor

static backward(ctx, grad_output)[source]

Backward pass with stochastic gradient estimation.

Parameters:

grad_output (torch.Tensor)

Return type:

Tuple[torch.Tensor, None, None, None, None]

class hpfracc.ml.stochastic_memory_sampling.StochasticFractionalLayer(*args, **kwargs)[source]

Bases: Module

PyTorch module for stochastic fractional derivatives.

Parameters:
__init__(alpha, k=64, method='importance', **kwargs)[source]
Parameters:
forward(x)[source]

Forward pass.

Parameters:

x (torch.Tensor)

Return type:

torch.Tensor

extra_repr()[source]
Return type:

str

hpfracc.ml.stochastic_memory_sampling.stochastic_fractional_derivative(x, alpha, k=64, method='importance', **kwargs)[source]

Convenience function for stochastic fractional derivative.

Parameters:
Return type:

torch.Tensor

hpfracc.ml.stochastic_memory_sampling.create_stochastic_fractional_layer(alpha, k=64, method='importance', **kwargs)[source]

Convenience function for creating stochastic fractional layer.

Parameters:
Return type:

StochasticFractionalLayer

Probabilistic Fractional Orders

Probabilistic Fractional Orders Implementation

This module implements probabilistic fractional orders where the fractional order itself becomes a random variable, enabling uncertainty quantification and robust optimization.

hpfracc.ml.probabilistic_fractional_orders.model(x, y)[source]

NumPyro model for Bayesian fractional order.

hpfracc.ml.probabilistic_fractional_orders.guide(x, y)[source]

NumPyro guide for Bayesian fractional order.

class hpfracc.ml.probabilistic_fractional_orders.ProbabilisticFractionalOrder(*args, **kwargs)[source]

Bases: Module

Represents a fractional order alpha as a random variable.

Parameters:
  • backend (str)

  • distribution (torch.distributions.Distribution)

  • learnable (bool)

__init__(model=None, guide=None, backend='numpyro', distribution=None, learnable=False)[source]
Parameters:
  • backend (str)

  • distribution (torch.distributions.Distribution | None)

  • learnable (bool)

init(rng_key, *args, **kwargs)[source]

Initialize the SVI state.

sample(k=1)[source]
Parameters:

k (int)

log_prob(value)[source]
Parameters:

value (torch.Tensor)

Return type:

torch.Tensor

class hpfracc.ml.probabilistic_fractional_orders.ProbabilisticFractionalLayer(*args, **kwargs)[source]

Bases: Module

PyTorch module for probabilistic fractional derivatives.

__init__(**kwargs)[source]
forward(x)[source]

Forward pass.

Parameters:

x (torch.Tensor)

Return type:

torch.Tensor

sample_alpha(n_samples=1)[source]

Sample fractional orders from the distribution.

Parameters:

n_samples (int)

Return type:

torch.Tensor

get_alpha_statistics()[source]

Get statistics of the fractional order distribution.

Return type:

Dict[str, torch.Tensor]

extra_repr()[source]
Return type:

str

hpfracc.ml.probabilistic_fractional_orders.create_probabilistic_fractional_layer(**kwargs)[source]

Create a probabilistic fractional layer.

Return type:

ProbabilisticFractionalLayer

hpfracc.ml.probabilistic_fractional_orders.create_normal_alpha_layer(mean, std, learnable=True, **kwargs)[source]

Create probabilistic fractional layer with normal distribution (torch-backed).

Parameters:
Return type:

ProbabilisticFractionalLayer

hpfracc.ml.probabilistic_fractional_orders.create_uniform_alpha_layer(low, high, learnable=False, **kwargs)[source]

Create probabilistic fractional layer with uniform distribution (torch-backed).

Parameters:
Return type:

ProbabilisticFractionalLayer

hpfracc.ml.probabilistic_fractional_orders.create_beta_alpha_layer(a, b, learnable=False, **kwargs)[source]

Create probabilistic fractional layer with beta distribution (torch-backed).

Parameters:
Return type:

ProbabilisticFractionalLayer

Variance-Aware Training

Variance-aware training hooks for stochastic and probabilistic fractional calculus.

This module provides training utilities that monitor and control variance in stochastic fractional derivatives and probabilistic fractional orders.

class hpfracc.ml.variance_aware_training.VarianceMetrics(mean, std, variance, coefficient_of_variation, sample_count, timestamp)[source]

Bases: object

Container for variance-related metrics.

Parameters:
mean: float
std: float
variance: float
coefficient_of_variation: float
sample_count: int
timestamp: float
__init__(mean, std, variance, coefficient_of_variation, sample_count, timestamp)
Parameters:
Return type:

None

class hpfracc.ml.variance_aware_training.VarianceMonitor(window_size=100, log_level='INFO')[source]

Bases: object

Monitor variance in stochastic fractional derivatives.

Parameters:
  • window_size (int)

  • log_level (str)

__init__(window_size=100, log_level='INFO')[source]
Parameters:
  • window_size (int)

  • log_level (str)

property current_metrics

Backward compatibility property for current metrics.

update(name, values, timestamp=None)[source]

Update variance metrics for a given component.

Parameters:
  • name (str)

  • values (torch.Tensor)

  • timestamp (float | None)

get_metrics(name=None)[source]

Get current metrics for a component.

Parameters:

name (str | None)

Return type:

VarianceMetrics | None

get_history(name)[source]

Get historical metrics for a component.

Parameters:

name (str)

Return type:

List[VarianceMetrics]

should_adapt()[source]

Determine if adaptation is needed based on variance levels.

Return type:

bool

get_summary()[source]

Get summary of all monitored components.

Return type:

Dict[str, Dict[str, float]]

class hpfracc.ml.variance_aware_training.StochasticSeedManager(base_seed=42)[source]

Bases: object

Manage random seeds for stochastic fractional derivatives.

Parameters:

base_seed (int)

__init__(base_seed=42)[source]
Parameters:

base_seed (int)

set_seed(seed)[source]

Set the current seed.

Parameters:

seed (int)

get_next_seed()[source]

Get the next seed in sequence.

Return type:

int

reset_to_base()[source]

Reset to base seed.

set_deterministic_mode(deterministic=True)[source]

Enable/disable deterministic mode.

Parameters:

deterministic (bool)

class hpfracc.ml.variance_aware_training.VarianceAwareCallback(monitor, seed_manager, log_interval=10, variance_check_interval=5)[source]

Bases: object

Callback for variance-aware training.

Parameters:
__init__(monitor, seed_manager, log_interval=10, variance_check_interval=5)[source]
Parameters:
on_epoch_begin(epoch, **kwargs)[source]

Called at the beginning of each epoch.

Parameters:

epoch (int)

on_batch_begin(batch_idx, **kwargs)[source]

Called at the beginning of each batch.

Parameters:

batch_idx (int)

on_batch_end(batch_idx, **kwargs)[source]

Called at the end of each batch.

Parameters:

batch_idx (int)

on_epoch_end(epoch, **kwargs)[source]

Called at the end of each epoch.

Parameters:

epoch (int)

_check_variance()[source]

Check variance metrics and log warnings.

_log_variance_summary()[source]

Log variance summary.

class hpfracc.ml.variance_aware_training.AdaptiveSamplingManager(initial_k=32, min_k=8, max_k=256, variance_threshold=0.1)[source]

Bases: object

Adaptively adjust sampling parameters based on variance.

Parameters:
__init__(initial_k=32, min_k=8, max_k=256, variance_threshold=0.1)[source]
Parameters:
update_k(variance, current_k)[source]

Update K based on variance.

Parameters:
Return type:

int

get_current_k()[source]

Get current K value.

Return type:

int

class hpfracc.ml.variance_aware_training.VarianceAwareTrainer(model, optimizer, loss_fn, variance_monitor=None, seed_manager=None, adaptive_sampling=None, callbacks=None)[source]

Bases: object

Enhanced trainer with variance awareness for stochastic fractional calculus.

Parameters:
__init__(model, optimizer, loss_fn, variance_monitor=None, seed_manager=None, adaptive_sampling=None, callbacks=None)[source]
Parameters:
_register_hooks()[source]

Register forward hooks to monitor variance.

train_epoch(dataloader, epoch=0)[source]

Train for one epoch with variance monitoring.

Parameters:

epoch (int)

Return type:

Dict[str, float]

train(dataloader, num_epochs)[source]

Train for multiple epochs.

Parameters:

num_epochs (int)

Return type:

Dict[str, List]

get_variance_summary()[source]

Get current variance summary.

Return type:

Dict[str, Dict[str, float]]

set_sampling_budget(k)[source]

Set sampling budget for stochastic components.

Parameters:

k (int)

enable_deterministic_mode(deterministic=True)[source]

Enable/disable deterministic mode.

Parameters:

deterministic (bool)

hpfracc.ml.variance_aware_training.create_variance_aware_trainer(model, optimizer=None, loss_fn=None, learning_rate=0.001, base_seed=42, variance_threshold=0.1, log_interval=10)[source]

Factory function to create a variance-aware trainer.

Parameters:
  • model (torch.nn.Module)

  • optimizer (torch.optim.Optimizer | None)

  • loss_fn (torch.nn.Module | None)

  • learning_rate (float)

  • base_seed (int)

  • variance_threshold (float)

  • log_interval (int)

Return type:

VarianceAwareTrainer

hpfracc.ml.variance_aware_training.test_variance_aware_training()[source]

Test variance-aware training with a simple model.

GPU Optimization

GPU optimization utilities for fractional calculus computations.

This module provides GPU acceleration features including Automatic Mixed Precision (AMP), chunked FFT operations, and performance profiling for fractional calculus operations.

class hpfracc.ml.gpu_optimization.PerformanceMetrics(operation, device, dtype, input_shape, execution_time, memory_used, memory_peak, throughput, timestamp)[source]

Bases: object

Container for performance metrics.

Parameters:
operation: str
device: str
dtype: str
input_shape: Tuple[int, ...]
execution_time: float
memory_used: float
memory_peak: float
throughput: float
timestamp: float
__init__(operation, device, dtype, input_shape, execution_time, memory_used, memory_peak, throughput, timestamp)
Parameters:
Return type:

None

class hpfracc.ml.gpu_optimization.GPUProfiler(device='cuda')[source]

Bases: object

Simple profiler for GPU operations.

Parameters:

device (str)

__init__(device='cuda')[source]
Parameters:

device (str)

start_timer(operation)[source]

Start timing an operation.

Parameters:

operation (str)

end_timer(input_tensor, output_tensor=None)[source]

End timing and record metrics.

Parameters:
  • input_tensor (torch.Tensor)

  • output_tensor (torch.Tensor | None)

get_summary()[source]

Get performance summary.

Return type:

Dict[str, Dict[str, float]]

clear_history()[source]

Clear metrics history.

class hpfracc.ml.gpu_optimization.ChunkedFFT(chunk_size=1024, overlap=128)[source]

Bases: object

Chunked FFT operations for large sequences.

Parameters:
  • chunk_size (int)

  • overlap (int)

__init__(chunk_size=1024, overlap=128)[source]
Parameters:
  • chunk_size (int)

  • overlap (int)

fft_chunked(x, dim=-1)[source]

Perform chunked FFT on large sequences.

Parameters:
  • x (torch.Tensor)

  • dim (int)

Return type:

torch.Tensor

ifft_chunked(x, dim=-1)[source]

Perform chunked IFFT on large sequences.

Parameters:
  • x (torch.Tensor)

  • dim (int)

Return type:

torch.Tensor

_process_chunks(x, dim, fft_func)[source]

Process tensor in chunks with overlap.

Parameters:
  • x (torch.Tensor)

  • dim (int)

Return type:

torch.Tensor

class hpfracc.ml.gpu_optimization.AMPFractionalEngine(base_engine, use_amp=True, dtype=torch.float16)[source]

Bases: object

Automatic Mixed Precision wrapper for fractional engines.

Parameters:
  • use_amp (bool)

  • dtype (torch.dtype)

__init__(base_engine, use_amp=True, dtype=torch.float16)[source]
Parameters:
  • use_amp (bool)

  • dtype (torch.dtype)

forward(x, alpha, **kwargs)[source]

Forward pass with AMP support.

Parameters:
  • x (torch.Tensor)

  • alpha (float)

Return type:

torch.Tensor

backward(grad_output, **kwargs)[source]

Backward pass with AMP support.

Parameters:

grad_output (torch.Tensor)

Return type:

torch.Tensor

class hpfracc.ml.gpu_optimization.GPUOptimizedSpectralEngine(engine_type='fft', use_amp=True, chunk_size=1024, dtype=torch.float16)[source]

Bases: object

GPU-optimized spectral engine with AMP and chunked FFT.

Parameters:
  • engine_type (str)

  • use_amp (bool)

  • chunk_size (int)

  • dtype (torch.dtype)

__init__(engine_type='fft', use_amp=True, chunk_size=1024, dtype=torch.float16)[source]
Parameters:
  • engine_type (str)

  • use_amp (bool)

  • chunk_size (int)

  • dtype (torch.dtype)

forward(x, alpha)[source]

GPU-optimized forward pass.

Parameters:
  • x (torch.Tensor)

  • alpha (float)

Return type:

torch.Tensor

_compute_spectral_transform(x, alpha)[source]

Compute spectral transform with GPU optimizations.

Parameters:
  • x (torch.Tensor)

  • alpha (float)

Return type:

torch.Tensor

_fallback_compute(x, alpha)[source]

Fallback computation without GPU optimizations.

Parameters:
  • x (torch.Tensor)

  • alpha (float)

Return type:

torch.Tensor

get_performance_summary()[source]

Get performance summary.

Return type:

Dict[str, Dict[str, float]]

class hpfracc.ml.gpu_optimization.GPUOptimizedStochasticSampler(base_sampler, use_amp=True, batch_size=1024)[source]

Bases: object

GPU-optimized stochastic memory sampler.

Parameters:
__init__(base_sampler, use_amp=True, batch_size=1024)[source]
Parameters:
sample_indices(n, k)[source]

GPU-optimized index sampling.

Parameters:
Return type:

torch.Tensor

_gpu_sample_indices(n, k)[source]

GPU-optimized index sampling implementation.

Parameters:
Return type:

torch.Tensor

get_performance_summary()[source]

Get performance summary.

Return type:

Dict[str, Dict[str, float]]

hpfracc.ml.gpu_optimization.gpu_optimization_context(use_amp=True, dtype=torch.float16)[source]

Context manager for GPU optimization.

Parameters:
  • use_amp (bool)

  • dtype (torch.dtype)

hpfracc.ml.gpu_optimization.benchmark_gpu_optimization()[source]

Benchmark GPU optimization performance.

hpfracc.ml.gpu_optimization.create_gpu_optimized_components(use_amp=True, chunk_size=1024, dtype=torch.float16)[source]

Factory function to create GPU-optimized components.

Parameters:
  • use_amp (bool)

  • chunk_size (int)

  • dtype (torch.dtype)

hpfracc.ml.gpu_optimization.test_gpu_optimization()[source]

Test GPU optimization functionality.

Backend Management

Backend Management System for Multi-Framework Support

This module provides unified interfaces for PyTorch, JAX, and NUMBA backends, enabling seamless switching between frameworks and automatic backend selection based on data type, hardware availability, and performance requirements.

class hpfracc.ml.backends.BackendType(value)[source]

Bases: Enum

Available computation backends

TORCH = 'torch'
JAX = 'jax'
NUMBA = 'numba'
AUTO = 'auto'
class hpfracc.ml.backends.BackendManager(preferred_backend=BackendType.AUTO, force_cpu=False, enable_jit=True, enable_gpu=True)[source]

Bases: object

Manages backend selection and provides unified interfaces

This class handles automatic backend selection based on: - Data type and size - Hardware availability (CPU/GPU) - Performance requirements - User preferences

Parameters:
__init__(preferred_backend=BackendType.AUTO, force_cpu=False, enable_jit=True, enable_gpu=True)[source]
Parameters:
_detect_available_backends()[source]

Detect which backends are available on the system

Return type:

List[BackendType]

_select_optimal_backend()[source]

Select the optimal backend based on preferences and availability

Return type:

BackendType

_initialize_backend_configs()[source]

Initialize backend-specific configurations

Return type:

Dict[BackendType, Dict[str, Any]]

get_backend_config(backend=None)[source]

Get configuration for a specific backend

Parameters:

backend (BackendType | None)

Return type:

Dict[str, Any]

switch_backend(backend)[source]

Switch to a different backend

Parameters:

backend (BackendType)

Return type:

bool

get_tensor_lib()[source]

Get the active tensor library

Return type:

Any

create_tensor(data, **kwargs)[source]

Create a tensor in the active backend

Parameters:

data (Any)

Return type:

Any

to_device(tensor, device=None)[source]

Move tensor to specified device

Parameters:
  • tensor (Any)

  • device (str | None)

Return type:

Any

compile_function(func)[source]

Compile a function using the active backend’s compilation system

Parameters:

func (Callable)

Return type:

Callable

hpfracc.ml.backends.get_backend_manager()[source]

Get the global backend manager instance

Return type:

BackendManager

hpfracc.ml.backends.set_backend_manager(manager)[source]

Set the global backend manager instance

Parameters:

manager (BackendManager)

Return type:

None

hpfracc.ml.backends.get_active_backend()[source]

Get the currently active backend

Return type:

BackendType

hpfracc.ml.backends.switch_backend(backend)[source]

Switch to a different backend

Parameters:

backend (BackendType)

Return type:

bool

Neural Networks

Neural ODEs

Targeted Optimized Neural Fractional Ordinary Differential Equations (Neural fODE)

This module provides targeted optimizations for neural networks that can learn to represent fractional differential equations, focusing on high-impact improvements without adding unnecessary complexity.

Key Improvements: - Optimized fractional ODE implementation (proper fractional calculus) - Advanced solver options with better performance - Memory optimization for large inputs - Improved training efficiency - Performance monitoring without overhead

Author: Davian R. Chin, Department of Biomedical Engineering, University of Reading Targeted Optimization: September 2025

class hpfracc.ml.neural_ode.NeuralODEConfig(input_dim=2, hidden_dim=64, output_dim=2, num_layers=3, activation='tanh', use_adjoint=True, solver='dopri5', rtol=1e-05, atol=1e-05, fractional_order=None, device=None, dtype=torch.float32, enable_performance_monitoring=False, memory_optimization=True, use_advanced_solvers=True)[source]

Bases: object

Targeted configuration for neural ODE models

Parameters:
  • input_dim (int)

  • hidden_dim (int)

  • output_dim (int)

  • num_layers (int)

  • activation (str)

  • use_adjoint (bool)

  • solver (str)

  • rtol (float)

  • atol (float)

  • fractional_order (float | FractionalOrder | None)

  • device (torch.device | None)

  • dtype (torch.dtype)

  • enable_performance_monitoring (bool)

  • memory_optimization (bool)

  • use_advanced_solvers (bool)

input_dim: int = 2
hidden_dim: int = 64
output_dim: int = 2
num_layers: int = 3
activation: str = 'tanh'
use_adjoint: bool = True
solver: str = 'dopri5'
rtol: float = 1e-05
atol: float = 1e-05
fractional_order: float | FractionalOrder | None = None
device: torch.device | None = None
enable_performance_monitoring: bool = False
memory_optimization: bool = True
use_advanced_solvers: bool = True
__init__(input_dim=2, hidden_dim=64, output_dim=2, num_layers=3, activation='tanh', use_adjoint=True, solver='dopri5', rtol=1e-05, atol=1e-05, fractional_order=None, device=None, dtype=torch.float32, enable_performance_monitoring=False, memory_optimization=True, use_advanced_solvers=True)
Parameters:
  • input_dim (int)

  • hidden_dim (int)

  • output_dim (int)

  • num_layers (int)

  • activation (str)

  • use_adjoint (bool)

  • solver (str)

  • rtol (float)

  • atol (float)

  • fractional_order (float | FractionalOrder | None)

  • device (torch.device | None)

  • dtype (torch.dtype)

  • enable_performance_monitoring (bool)

  • memory_optimization (bool)

  • use_advanced_solvers (bool)

Return type:

None

class hpfracc.ml.neural_ode.BaseNeuralODE(*args, **kwargs)[source]

Bases: Module, ABC

Targeted optimized base class for Neural ODE implementations

Parameters:

config (NeuralODEConfig)

__init__(config)[source]
Parameters:

config (NeuralODEConfig)

_setup_layer()[source]

Setup layer-specific components

_build_network()[source]

Build neural network architecture with optimizations

_initialize_weights()[source]

Optimized weight initialization

_get_activation(x)[source]

Apply activation function

Parameters:

x (torch.Tensor)

Return type:

torch.Tensor

ode_func(t, x)[source]

Optimized ODE function with improved tensor handling

Parameters:
  • t (torch.Tensor)

  • x (torch.Tensor)

Return type:

torch.Tensor

abstract forward(x, t)[source]

Forward pass - must be implemented by subclasses

Parameters:
  • x (torch.Tensor)

  • t (torch.Tensor)

Return type:

torch.Tensor

class hpfracc.ml.neural_ode.NeuralODE(*args, **kwargs)[source]

Bases: BaseNeuralODE

Targeted optimized Neural ODE implementation

Parameters:
__init__(input_dim, hidden_dim, output_dim, num_layers=3, activation='tanh', use_adjoint=True, solver='dopri5', rtol=1e-05, atol=1e-05)[source]
Parameters:
_check_torchdiffeq()[source]

Check if torchdiffeq is available

Return type:

bool

forward(x, t)[source]

Optimized forward pass

Parameters:
  • x (torch.Tensor)

  • t (torch.Tensor)

Return type:

torch.Tensor

_solve_torchdiffeq(x, t)[source]

Solve using torchdiffeq with optimizations

Parameters:
  • x (torch.Tensor)

  • t (torch.Tensor)

Return type:

torch.Tensor

_solve_optimized_euler(x, t)[source]

Optimized Euler solver with memory efficiency

Parameters:
  • x (torch.Tensor)

  • t (torch.Tensor)

Return type:

torch.Tensor

class hpfracc.ml.neural_ode.NeuralFODE(*args, **kwargs)[source]

Bases: BaseNeuralODE

Targeted optimized Neural Fractional ODE implementation

Parameters:
__init__(input_dim, hidden_dim, output_dim, fractional_order=0.5, num_layers=3, activation='tanh', use_adjoint=True, solver='fractional_euler', rtol=1e-05, atol=1e-05)[source]
Parameters:
get_fractional_order()[source]

Get the fractional order

Return type:

float

_check_torchdiffeq()[source]

Check if torchdiffeq is available

Return type:

bool

forward(x, t)[source]

Optimized fractional forward pass

Parameters:
  • x (torch.Tensor)

  • t (torch.Tensor)

Return type:

torch.Tensor

_solve_fractional_ode_optimized(x, t)[source]

Optimized fractional ODE solver with proper fractional calculus

Parameters:
  • x (torch.Tensor)

  • t (torch.Tensor)

Return type:

torch.Tensor

class hpfracc.ml.neural_ode.NeuralODETrainer(model, optimizer='adam', learning_rate=0.001, loss_function='mse')[source]

Bases: object

Targeted optimized trainer for Neural ODE models

Parameters:
__init__(model, optimizer='adam', learning_rate=0.001, loss_function='mse')[source]
Parameters:
_setup_optimizer(optimizer_type)[source]

Set up optimizer

Parameters:

optimizer_type (str)

Return type:

torch.optim.Optimizer

_setup_loss_function(loss_type)[source]

Set up loss function

Parameters:

loss_type (str)

Return type:

torch.nn.Module

train_step(x, y_target, t)[source]

Optimized training step

Parameters:
  • x (torch.Tensor)

  • y_target (torch.Tensor)

  • t (torch.Tensor)

Return type:

float

_validate(data_loader)[source]

Compute average validation loss over a data loader.

Return type:

float

train(data_loader, num_epochs=1, verbose=False)[source]
Parameters:
hpfracc.ml.neural_ode.create_neural_ode(model_type='standard', **kwargs)[source]

Factory function to create neural ODE models

Parameters:

model_type (str)

Return type:

NeuralODE | NeuralFODE

hpfracc.ml.neural_ode.create_neural_ode_trainer(model, **kwargs)[source]

Factory function to create targeted neural ODE trainer

Parameters:

model (NeuralODE | NeuralFODE)

Return type:

NeuralODETrainer

Layers

Comprehensive Optimal Neural Network Layers with Fractional Calculus

This module provides the optimal hybrid implementation of all neural network layers with fractional calculus integration, combining the best performance, features, and stability from all implementations.

Performance: 7.56x faster than original implementation Stability: 100% success rate across all test cases Features: Complete layer type coverage with optimal architecture

Author: Davian R. Chin, Department of Biomedical Engineering, University of Reading Comprehensive Optimal Implementation: September 2025

class hpfracc.ml.layers.LayerConfig(fractional_order=None, method='RL', use_fractional=True, activation='relu', dropout=0.1, backend=BackendType.AUTO, device=None, dtype=torch.float32, enable_caching=True, enable_benchmarking=False, performance_mode='balanced')[source]

Bases: object

Optimal configuration for all fractional layers

Parameters:
fractional_order: FractionalOrder = None
method: str = 'RL'
use_fractional: bool = True
activation: str = 'relu'
dropout: float = 0.1
backend: BackendType = 'auto'
device: torch.device | None = None
enable_caching: bool = True
enable_benchmarking: bool = False
performance_mode: str = 'balanced'
__init__(fractional_order=None, method='RL', use_fractional=True, activation='relu', dropout=0.1, backend=BackendType.AUTO, device=None, dtype=torch.float32, enable_caching=True, enable_benchmarking=False, performance_mode='balanced')
Parameters:
Return type:

None

class hpfracc.ml.layers.BackendManager[source]

Bases: object

Intelligent backend management with workload-aware selection and performance learning

__init__()[source]
_detect_available_backends()[source]

Detect available backends and their capabilities

Return type:

Dict[str, bool]

select_optimal_backend(config, input_shape)[source]

Select optimal backend based on workload characteristics and learning

Parameters:
Return type:

str

class hpfracc.ml.layers.FractionalOps(config)[source]

Bases: object

Optimal fractional operations with performance optimization

Parameters:

config (LayerConfig)

__init__(config)[source]
Parameters:

config (LayerConfig)

apply_fractional_derivative(x, alpha, method='RL', backend='pytorch')[source]

Apply fractional derivative with optimal backend selection

Parameters:
  • x (torch.Tensor)

  • alpha (float)

  • method (str)

  • backend (str)

Return type:

torch.Tensor

class hpfracc.ml.layers.FractionalLayerBase(*args, **kwargs)[source]

Bases: Module, ABC

Optimal base class for all fractional layers

Parameters:
__init__(config, *, backend=None)[source]
Parameters:
_setup_layer()[source]

Setup layer-specific components

apply_fractional_derivative(x)[source]

Apply fractional derivative with optimal backend selection

Parameters:

x (torch.Tensor)

Return type:

torch.Tensor

apply_activation(x)[source]

Apply activation function

Parameters:

x (torch.Tensor)

Return type:

torch.Tensor

apply_dropout(x)[source]

Apply dropout if training

Parameters:

x (torch.Tensor)

Return type:

torch.Tensor

abstract forward(x)[source]

Forward pass - must be implemented by subclasses

Parameters:

x (torch.Tensor)

Return type:

torch.Tensor

class hpfracc.ml.layers.FractionalConv1D(*args, **kwargs)[source]

Bases: FractionalLayerBase

Optimal 1D Convolutional layer with fractional calculus integration

Parameters:
__init__(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True, config=None, backend=None)[source]
Parameters:
_initialize_weights()[source]

Initialize weights using optimal methods

forward(x)[source]

Forward pass with optimal fractional derivative integration

Parameters:

x (torch.Tensor)

Return type:

torch.Tensor

class hpfracc.ml.layers.FractionalConv2D(*args, **kwargs)[source]

Bases: FractionalLayerBase

Optimal 2D Convolutional layer with fractional calculus integration

Parameters:
__init__(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True, config=None, backend=None)[source]
Parameters:
_initialize_weights()[source]

Initialize weights using optimal methods

forward(x)[source]

Forward pass with optimal fractional derivative integration

Parameters:

x (torch.Tensor)

Return type:

torch.Tensor

class hpfracc.ml.layers.FractionalLinear(*args, **kwargs)[source]

Bases: FractionalLayerBase

Optimal Linear layer with fractional calculus integration

Parameters:
__init__(in_features, out_features, bias=True, config=None, backend=None)[source]
Parameters:
_initialize_weights()[source]

Initialize weights using optimal methods

forward(x)[source]

Forward pass with optimal fractional derivative integration

Parameters:

x (torch.Tensor)

Return type:

torch.Tensor

class hpfracc.ml.layers.FractionalLSTM(*args, **kwargs)[source]

Bases: FractionalLayerBase

Minimal LSTM layer wrapper satisfying tests.

Parameters:
__init__(input_size, hidden_size, num_layers=1, bidirectional=False, dropout=0.0, bias=True, config=None, backend=None, fractional_order=0.5)[source]
Parameters:
forward(x, return_state=False)[source]

Forward pass - must be implemented by subclasses

Parameters:

return_state (bool)

forward_with_state(x)[source]

Return (output, (h, c)) for callers that need states.

class hpfracc.ml.layers.FractionalTransformer(*args, **kwargs)[source]

Bases: FractionalLayerBase

Minimal Transformer wrapper satisfying tests.

Parameters:
__init__(d_model, nhead=None, num_encoder_layers=1, num_decoder_layers=1, dim_feedforward=None, dropout=0.1, activation='relu', n_heads=None, d_ff=None, config=None, backend=None)[source]
Parameters:
forward(src, tgt=None)[source]

Forward pass - must be implemented by subclasses

class hpfracc.ml.layers.FractionalPooling(*args, **kwargs)[source]

Bases: FractionalLayerBase

Minimal pooling wrapper satisfying tests.

Parameters:
__init__(kernel_size, stride=1, padding=0, pool_type='max', dim=1, config=None, backend=None, fractional_order=0.5)[source]
Parameters:
forward(x)[source]

Forward pass - must be implemented by subclasses

class hpfracc.ml.layers.FractionalBatchNorm1d(*args, **kwargs)[source]

Bases: FractionalLayerBase

Minimal BatchNorm1d wrapper satisfying tests.

Parameters:
__init__(num_features, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True, config=None, backend=None)[source]
Parameters:
forward(x)[source]

Forward pass - must be implemented by subclasses

class hpfracc.ml.layers.FractionalDropout(*args, **kwargs)[source]

Bases: FractionalLayerBase

Minimal Dropout wrapper satisfying tests.

Parameters:
__init__(p=0.5, inplace=False, config=None, backend=None)[source]
Parameters:
forward(x, training=None)[source]

Forward pass - must be implemented by subclasses

class hpfracc.ml.layers.FractionalLayerNorm(*args, **kwargs)[source]

Bases: FractionalLayerBase

Minimal LayerNorm wrapper satisfying tests.

Parameters:
__init__(normalized_shape, eps=1e-05, elementwise_affine=True, config=None, backend=None)[source]
Parameters:
forward(x)[source]

Forward pass - must be implemented by subclasses

class hpfracc.ml.layers.FractionalMaxUnpool1d(*args, **kwargs)[source]

Bases: FractionalLayerBase

Parameters:
__init__(kernel_size, stride=1, padding=0, output_size=None, config=None, backend=None)[source]
Parameters:
forward(x, indices)[source]

Forward pass - must be implemented by subclasses

Parameters:
  • x (torch.Tensor)

  • indices (torch.Tensor)

Return type:

torch.Tensor

class hpfracc.ml.layers.FractionalMaxUnpool2d(*args, **kwargs)[source]

Bases: FractionalLayerBase

Parameters:
__init__(kernel_size, stride=1, padding=0, output_size=None, config=None, backend=None)[source]
Parameters:
forward(x, indices)[source]

Forward pass - must be implemented by subclasses

Parameters:
  • x (torch.Tensor)

  • indices (torch.Tensor)

Return type:

torch.Tensor

class hpfracc.ml.layers.FractionalMaxUnpool3d(*args, **kwargs)[source]

Bases: FractionalLayerBase

Parameters:
__init__(kernel_size, stride=1, padding=0, output_size=None, config=None, backend=None)[source]
Parameters:
forward(x, indices)[source]

Forward pass - must be implemented by subclasses

Parameters:
  • x (torch.Tensor)

  • indices (torch.Tensor)

Return type:

torch.Tensor

class hpfracc.ml.layers.FractionalSelfAttention(*args, **kwargs)[source]

Bases: Module

Parameters:
  • embed_dim (int)

  • num_heads (int)

  • dropout (float)

  • bias (bool)

  • add_bias_kv (bool)

  • add_zero_attn (bool)

  • kdim (int | None)

  • vdim (int | None)

  • batch_first (bool)

  • device (torch.device | None)

  • dtype (torch.dtype | None)

__init__(embed_dim, num_heads, dropout=0.0, bias=True, add_bias_kv=False, add_zero_attn=False, kdim=None, vdim=None, batch_first=True, device=None, dtype=None)[source]
Parameters:
  • embed_dim (int)

  • num_heads (int)

  • dropout (float)

  • bias (bool)

  • add_bias_kv (bool)

  • add_zero_attn (bool)

  • kdim (int | None)

  • vdim (int | None)

  • batch_first (bool)

  • device (torch.device | None)

  • dtype (torch.dtype | None)

forward(query, key, value)[source]
Parameters:
  • query (torch.Tensor) – (batch, seq_len, embed_dim)

  • key (torch.Tensor) – (batch, seq_len, embed_dim)

  • value (torch.Tensor) – (batch, seq_len, embed_dim)

Return type:

Tensor

Tensor Operations

Unified Tensor Operations for Multi-Backend Support

This module provides consistent tensor operations across PyTorch, JAX, and a NumPy-backed “NUMBA lane” (arrays are NumPy; numba is a compiler elsewhere), enabling seamless switching between frameworks while maintaining the same API.

class hpfracc.ml.tensor_ops.TensorOps(backend=None)[source]

Bases: object

Unified tensor operations across different backends.

Notes

  • AUTO is resolved to a concrete, installed backend during __init__.

  • NUMBA lane uses NumPy arrays (numba itself is not a tensor library).

  • JAX random ops require a PRNG key; pass via kwargs (key=…).

Parameters:

backend (BackendType | str | None)

__init__(backend=None)[source]
Parameters:

backend (BackendType | str | None)

_resolve_backend(backend, backend_manager)[source]

Pick a concrete, installed backend with sensible fallbacks. Priority:

  1. explicit backend (if not AUTO) when installed

  2. backend_manager.active_backend (if not AUTO) when installed

  3. fallback order: TORCH -> JAX -> NUMBA (NumPy)

Parameters:

backend (BackendType | None)

_get_tensor_lib_for_backend(backend)[source]

Get tensor library for a specific backend (imports guarded).

Parameters:

backend (BackendType)

Return type:

Any

create_tensor(data, **kwargs)[source]

Create a tensor in the current backend.

Parameters:

data (Any)

Return type:

Any

tensor(data, **kwargs)[source]

Alias for create_tensor.

Parameters:

data (Any)

Return type:

Any

from_numpy(array)[source]
Parameters:

array (Any)

Return type:

Any

to_numpy(tensor)[source]
Parameters:

tensor (Any)

Return type:

Any

no_grad()[source]

Context manager for disabling gradient computation. - PyTorch: torch.no_grad() - JAX: there is no true ‘no_grad’ context; we return a nullcontext().

Use jax.lax.stop_gradient at call sites if you need it.

  • NUMBA lane: nullcontext()

zeros(shape, **kwargs)[source]
Parameters:

shape (Tuple[int, ...])

Return type:

Any

ones(shape, **kwargs)[source]
Parameters:

shape (Tuple[int, ...])

Return type:

Any

eye(n, **kwargs)[source]
Parameters:

n (int)

Return type:

Any

arange(start, end, step=1, **kwargs)[source]
Parameters:
Return type:

Any

linspace(start, end, num, **kwargs)[source]
Parameters:
Return type:

Any

zeros_like(tensor, **kwargs)[source]
Parameters:

tensor (Any)

Return type:

Any

ones_like(tensor, **kwargs)[source]

Create a tensor of ones with the same shape as input tensor.

Parameters:

tensor (Any)

Return type:

Any

sqrt(tensor)[source]
Parameters:

tensor (Any)

Return type:

Any

stack(tensors, dim=0)[source]
Parameters:
Return type:

Any

cat(tensors, dim=0)[source]
Parameters:
Return type:

Any

reshape(tensor, shape)[source]
Parameters:
Return type:

Any

repeat(tensor, repeats, dim=0)[source]

Repeat elements along a specified axis (element-wise repeat). For tiling the whole array shape, use tile(…) helper below.

Parameters:
Return type:

Any

tile(tensor, reps)[source]

Tile (broadcast repeat) tensor like np.tile / torch.repeat(*reps).

Parameters:
Return type:

Any

clip(tensor, min_val, max_val)[source]
Parameters:
Return type:

Any

unsqueeze(tensor, dim)[source]
Parameters:
Return type:

Any

expand(tensor, *sizes)[source]
Parameters:
Return type:

Any

gather(tensor, dim, index)[source]
Parameters:
Return type:

Any

squeeze(tensor, dim=None)[source]
Parameters:
  • tensor (Any)

  • dim (int | None)

Return type:

Any

transpose(tensor, *args, **kwargs)[source]
Transpose a tensor. Supports signatures:
  • transpose(tensor) for 2D: matrix transpose; otherwise reverse axes

  • transpose(tensor, dim0, dim1) : swap two axes (positional)

  • transpose(tensor, dim0=…, dim1=…) : swap two axes (keyword)

  • transpose(tensor, dims=(…)) : permute by dims

Parameters:

tensor (Any)

Return type:

Any

matmul(a, b)[source]
Parameters:
Return type:

Any

einsum(equation, *operands)[source]
Parameters:

equation (str)

Return type:

Any

sum(tensor, dim=None, keepdim=None, keepdims=False)[source]
Parameters:
  • tensor (Any)

  • dim (int | None)

  • keepdim (bool | None)

  • keepdims (bool | None)

Return type:

Any

mean(tensor, dim=None, keepdim=None, keepdims=False)[source]
Parameters:
  • tensor (Any)

  • dim (int | None)

  • keepdim (bool | None)

  • keepdims (bool | None)

Return type:

Any

std(tensor, dim=None, keepdims=False)[source]
Parameters:
Return type:

Any

median(tensor, dim=None, keepdims=False)[source]
Parameters:
Return type:

Any

quantile(tensor, q, dim=None, keepdims=False)[source]
Parameters:
Return type:

Any

max(tensor, dim=None, keepdims=False)[source]
Parameters:
Return type:

Any

min(tensor, dim=None, keepdims=False)[source]
Parameters:
Return type:

Any

norm(tensor, p=2, dim=None)[source]
Parameters:
Return type:

Any

softmax(tensor, dim=-1)[source]
Parameters:
Return type:

Any

relu(tensor)[source]
Parameters:

tensor (Any)

Return type:

Any

sigmoid(tensor)[source]
Parameters:

tensor (Any)

Return type:

Any

tanh(tensor)[source]
Parameters:

tensor (Any)

Return type:

Any

log(tensor)[source]
Parameters:

tensor (Any)

Return type:

Any

add(tensor1, tensor2)[source]
Parameters:
Return type:

Any

subtract(tensor1, tensor2)[source]
Parameters:
Return type:

Any

multiply(tensor1, tensor2)[source]
Parameters:
Return type:

Any

divide(tensor1, tensor2)[source]
Parameters:
Return type:

Any

power(tensor, exponent)[source]
Parameters:
Return type:

Any

sin(tensor)[source]
Parameters:

tensor (Any)

Return type:

Any

cos(tensor)[source]
Parameters:

tensor (Any)

Return type:

Any

exp(tensor)[source]
Parameters:

tensor (Any)

Return type:

Any

abs(tensor)[source]
Parameters:

tensor (Any)

Return type:

Any

randn(shape, **kwargs)[source]
Parameters:

shape (Tuple[int, ...])

Return type:

Any

randn_like(tensor, **kwargs)[source]
Parameters:

tensor (Any)

Return type:

Any

dropout(tensor, p=0.5, training=True, **kwargs)[source]
Parameters:
Return type:

Any

fft(tensor)[source]
Parameters:

tensor (Any)

Return type:

Any

ifft(tensor)[source]
Parameters:

tensor (Any)

Return type:

Any

clone(tensor)[source]
Parameters:

tensor (Any)

Return type:

Any

concatenate(tensors, dim=0)[source]
Parameters:
Return type:

Any

hpfracc.ml.tensor_ops.get_tensor_ops(backend=None)[source]

Get the global tensor operations instance (resolves AUTO safely).

Parameters:

backend (BackendType | None)

Return type:

TensorOps

hpfracc.ml.tensor_ops.create_tensor(data, **kwargs)[source]

Create a tensor using the current backend.

Parameters:

data (Any)

Return type:

Any

hpfracc.ml.tensor_ops.switch_backend(backend)[source]

Switch to a different backend and update tensor operations.

Parameters:

backend (BackendType)

Return type:

None