boofun.core.gpu

GPU Acceleration for Boolean Function Operations.

This module provides GPU-accelerated implementations of computationally intensive operations using CuPy. Falls back to NumPy when CuPy is unavailable.

Key accelerated operations: - Walsh-Hadamard Transform (WHT) - Influence computation - Noise stability computation - Large truth table operations

Usage:

from boofun.core.gpu import gpu_wht, is_gpu_available

if is_gpu_available():

fourier = gpu_wht(truth_table_pm)

else:

# Fallback to CPU fourier = cpu_wht(truth_table_pm)

Functions

auto_accelerate(func)

Decorator to automatically use GPU when beneficial.

enable_gpu([enable])

Enable or disable GPU acceleration.

get_array_module(arr)

Get the array module (numpy or cupy) for the given array.

gpu_influences(fourier_coeffs[, n_vars])

GPU-accelerated influence computation from Fourier coefficients.

gpu_noise_stability(fourier_coeffs, rho)

GPU-accelerated noise stability computation.

gpu_spectral_weight_by_degree(fourier_coeffs)

GPU-accelerated spectral weight computation by degree.

gpu_walsh_hadamard(values[, in_place])

GPU-accelerated Walsh-Hadamard Transform.

is_gpu_available()

Check if GPU acceleration is available.

is_gpu_enabled()

Check if GPU acceleration is currently enabled.

to_cpu(arr)

Move array to CPU.

to_gpu(arr)

Move array to GPU if available and enabled.

Classes

GPUBooleanFunctionOps(truth_table)

GPU-accelerated operations for Boolean functions.

boofun.core.gpu.is_gpu_available() bool[source]

Check if GPU acceleration is available.

boofun.core.gpu.is_gpu_enabled() bool[source]

Check if GPU acceleration is currently enabled.

boofun.core.gpu.enable_gpu(enable: bool = True) None[source]

Enable or disable GPU acceleration.

boofun.core.gpu.get_array_module(arr: ndarray | cp.ndarray) type[source]

Get the array module (numpy or cupy) for the given array.

boofun.core.gpu.to_gpu(arr: ndarray) ndarray | cp.ndarray[source]

Move array to GPU if available and enabled.

boofun.core.gpu.to_cpu(arr: ndarray | cp.ndarray) ndarray[source]

Move array to CPU.

boofun.core.gpu.gpu_walsh_hadamard(values: ndarray, in_place: bool = False) ndarray[source]

GPU-accelerated Walsh-Hadamard Transform.

Computes the WHT in O(n * 2^n) time using GPU parallelism.

Parameters:
  • values – Array of 2^n values in ±1 representation

  • in_place – If True, modify input array (saves memory)

Returns:

WHT result (unnormalized, on CPU)

boofun.core.gpu.gpu_influences(fourier_coeffs: ndarray, n_vars: int = None) ndarray[source]

GPU-accelerated influence computation from Fourier coefficients.

Inf_i[f] = Σ_{S∋i} f̂(S)²

Parameters:
  • fourier_coeffs – Fourier coefficients array

  • n_vars – Number of variables (inferred from array size if not provided)

Returns:

Array of influences (one per variable)

boofun.core.gpu.gpu_noise_stability(fourier_coeffs: ndarray, rho: float) float[source]

GPU-accelerated noise stability computation.

Stab_ρ[f] = Σ_S ρ^|S| · f̂(S)²

Parameters:
  • fourier_coeffs – Fourier coefficients array

  • rho – Correlation parameter

Returns:

Noise stability value

boofun.core.gpu.gpu_spectral_weight_by_degree(fourier_coeffs: ndarray) ndarray[source]

GPU-accelerated spectral weight computation by degree.

W^{=k}[f] = Σ_{|S|=k} f̂(S)²

Parameters:

fourier_coeffs – Fourier coefficients array

Returns:

Array of weights W^{=0}, W^{=1}, …, W^{=n}

class boofun.core.gpu.GPUBooleanFunctionOps(truth_table: ndarray)[source]

GPU-accelerated operations for Boolean functions.

Provides a high-level interface for GPU acceleration.

__init__(truth_table: ndarray)[source]

Initialize with a truth table.

Parameters:

truth_table – Boolean truth table (0/1 values)

property pm_values: ndarray

Convert to ±1 representation.

O’Donnell convention (Analysis of Boolean Functions, Chapter 1): Boolean 0 → +1, Boolean 1 → -1 This matches the library’s SpectralAnalyzer convention.

fourier() ndarray[source]

Compute Fourier coefficients using GPU if available.

influences() ndarray[source]

Compute influences using GPU if available.

total_influence() float[source]

Compute total influence.

noise_stability(rho: float) float[source]

Compute noise stability using GPU if available.

spectral_weights() ndarray[source]

Compute spectral weights by degree using GPU if available.

boofun.core.gpu.auto_accelerate(func)[source]

Decorator to automatically use GPU when beneficial.

Uses GPU for arrays larger than a threshold (2^14 = 16384 elements).