Skip to content

Backend helpers

backend

NumPy/CuPy backend selection and array-module helpers.

A thin wrapper around the array-API differences between NumPy and CuPy so the rest of the package can be written once and run on either CPU or GPU. Also exposes a few low-level helpers (memory probe, FFT-size rounding, dtype pairs) that are shared between the spectrogram and Gabor stages.

get_array_module

get_array_module(use_gpu: bool = False)

Return the active array module (numpy or cupy).

PARAMETER DESCRIPTION
use_gpu

If True and CuPy is available, return cupy. Otherwise return numpy.

TYPE: bool DEFAULT: False

RETURNS DESCRIPTION
module

Either the numpy or cupy module object.

get_signal_module

get_signal_module(use_gpu: bool = False)

Return the active signal-processing module.

PARAMETER DESCRIPTION
use_gpu

If True and CuPy is available, return cupyx.scipy.signal. Otherwise return scipy.signal.

TYPE: bool DEFAULT: False

RETURNS DESCRIPTION
module

Either scipy.signal or cupyx.scipy.signal.

next_fast_len

next_fast_len(n: int, use_gpu: bool = False) -> int

Round n up to an FFT-friendly length.

CPU uses :func:scipy.fft.next_fast_len (product of small primes). GPU uses the next power of two, which matches cuFFT's optimal path.

PARAMETER DESCRIPTION
n

Minimum length required.

TYPE: int

use_gpu

Switches between the CPU and GPU rounding rules.

TYPE: bool DEFAULT: False

RETURNS DESCRIPTION
int

The smallest fast length >= n.

get_available_memory

get_available_memory(use_gpu: bool = False) -> int

Best-effort estimate of free memory in bytes.

On GPU, reports free VRAM via the current CUDA device. On CPU, tries /proc/meminfo first, then :mod:psutil, then falls back to a hard-coded 4 GiB so callers always get a usable number.

PARAMETER DESCRIPTION
use_gpu

If True, report device memory rather than host memory.

TYPE: bool DEFAULT: False

RETURNS DESCRIPTION
int

Available memory in bytes.

get_dtypes

get_dtypes(use_float32: bool = True)

Return a matching (float, complex) dtype pair.

PARAMETER DESCRIPTION
use_float32

If True, return (np.float32, np.complex64); otherwise the double-precision pair.

TYPE: bool DEFAULT: True

RETURNS DESCRIPTION
tuple of numpy.dtype

(float_dtype, complex_dtype).

to_numpy

to_numpy(array) -> ndarray

Return array as a host-side NumPy array, copying from GPU if needed.

PARAMETER DESCRIPTION
array

Input array. May be a numpy array, cupy array, or any array-like accepted by :func:numpy.asarray.

TYPE: array_like

RETURNS DESCRIPTION
ndarray

Host-side numpy array.