Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"sphinx.ext.napoleon", # Unterstützt Google/NumPy-Style Docstrings
"sphinx.ext.mathjax",
"sphinx.ext.viewcode",
"sphinx.ext.intersphinx",
"sphinx.ext.intersphinx",
"myst_parser",
]

Expand Down Expand Up @@ -64,5 +64,5 @@
"navigation_depth": 5,
}
html_css_files = [
'custom.css',
"custom.css",
]
78 changes: 40 additions & 38 deletions src/pyGroupedTransforms/GroupedTransform.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@
"""

import numpy as np
import pykeops
import torch
from pykeops.torch import LazyTensor

from pyGroupedTransforms import *
from .NFFTtools import index_set_without_zeros

import pykeops
from pykeops.torch import LazyTensor

from .NFFTtools import index_set_without_zeros

# All code that is linked to NFMTtools or to system = "mixed" is not tested yet....

Expand Down Expand Up @@ -229,8 +228,8 @@ class GroupedTransform:
settings (list[Setting]): Pre-built settings. If empty, they are
derived from ``U``/``N`` or ``d``/``ds``/``N``.
algorithm (str, optional): Defaults to ``"nfft"``. ``"nfft"`` for the fast transforms, ``"keops"`` for implicit matrix multiplication (allow GPU or CPU computation),
or ``"direct"`` to build the matrix explicitly.
device: Device used by the ``"keops"`` algorithm, allows user to manually switch between GPU or CPU computation. Supported values are: ``"cuda"``
or ``"direct"`` to build the matrix explicitly.
device: Device used by the ``"keops"`` algorithm, allows user to manually switch between GPU or CPU computation. Supported values are: ``"cuda"``
for NVIDIA and CUDA-supporting devices, ``"mps"`` for Apple MPS-supporting devices, ``"cpu"`` for selecting CPU processing.
If ``None`` (default), the algorithm automatically selects the device based on availability, prioritizing GPU use.
parallel (bool): Run the sub-transforms threaded. Defaults to ``True``.
Expand Down Expand Up @@ -347,23 +346,22 @@ def __init__(
bandwidths=s.bandwidths, X=np.copy(X[:, u], order="C")
)
elif algorithm == "keops":
self.matrix = np.empty((0,0), dtype=object)
self.matrix = np.empty((0, 0), dtype=object)

if self.device:
device = device
else:
else:
if torch.cuda.is_available():
device = "cuda"
elif torch.mps.is_available():
device = "mps"
else:
device = "cpu"

self.transforms = [
DeferredLinearOperator()
for _ in range(len(self.settings))
DeferredLinearOperator() for _ in range(len(self.settings))
]

D = X.shape[1]

freq_list = []
Expand All @@ -372,8 +370,7 @@ def __init__(

if len(s.bandwidths) == 0:
full = np.zeros((1, D), dtype=np.int32)



local = np.atleast_2d(
s.mode.index_set_without_zeros(
np.array(s.bandwidths, dtype=np.int32)
Expand All @@ -388,13 +385,9 @@ def __init__(
freq_list.append(full)

freq = np.vstack(freq_list)

X_torch = torch.tensor(X, dtype=torch.float64, device=device)
I_torch = torch.tensor(
freq,
dtype=torch.float64,
device=device
)
I_torch = torch.tensor(freq, dtype=torch.float64, device=device)

def trafo(fhat):
if self.system == "cos" or self.system == "cheb":
Expand All @@ -403,8 +396,8 @@ def trafo(fhat):

kernel = 1.0
for i in range(D):
Xi = LazyTensor(X_torch[:, None, i:i+1].contiguous())
Ki = LazyTensor(I_torch[None, :, i:i+1].contiguous())
Xi = LazyTensor(X_torch[:, None, i : i + 1].contiguous())
Ki = LazyTensor(I_torch[None, :, i : i + 1].contiguous())
kernel = kernel * (2 * torch.pi * Xi * Ki).cos()

fhat_torch = (
Expand All @@ -428,7 +421,9 @@ def trafo(fhat):
two_pi_phase = -2 * torch.pi * phase_fwd
kernel = two_pi_phase.cos() + 1j * two_pi_phase.sin()

fhat_torch = torch.tensor(fhat, dtype=torch.complex128, device=device)
fhat_torch = torch.tensor(
fhat, dtype=torch.complex128, device=device
)
fhat_j = LazyTensor(fhat_torch[None, :, None].contiguous())

try:
Expand All @@ -443,15 +438,15 @@ def trafo(fhat):
def adjoint(f):
if self.system == "cos" or self.system == "cheb":
mult = np.sqrt(2.0) ** np.count_nonzero(freq, axis=1)

f_torch = torch.as_tensor(
f, dtype=torch.float64, device=device
).contiguous()

kernel = 1.0
for i in range(D):
Ki = LazyTensor(I_torch[:, None, i:i+1].contiguous())
Xi = LazyTensor(X_torch[None, :, i:i+1].contiguous())
Ki = LazyTensor(I_torch[:, None, i : i + 1].contiguous())
Xi = LazyTensor(X_torch[None, :, i : i + 1].contiguous())
kernel = kernel * (2 * torch.pi * Xi * Ki).cos()

f_i = LazyTensor(f_torch[None, :, None].contiguous())
Expand All @@ -465,7 +460,9 @@ def adjoint(f):
print("Error in KeOps adjoint:", e)
return None
else:
f_torch = torch.tensor(f, dtype=torch.complex128, device=device).contiguous()
f_torch = torch.tensor(
f, dtype=torch.complex128, device=device
).contiguous()

X_i = LazyTensor(X_torch[None, :, :].contiguous())
K_j = LazyTensor(I_torch[:, None, :].contiguous())
Expand All @@ -487,10 +484,15 @@ def adjoint(f):
return None

keops_dtype = np.float64 if self.system == "cos" else np.complex128
self.transforms = [DeferredLinearOperator(
dtype=keops_dtype, shape=(X.shape[0], len(freq)), mfunc=trafo, rmfunc=adjoint
)]

self.transforms = [
DeferredLinearOperator(
dtype=keops_dtype,
shape=(X.shape[0], len(freq)),
mfunc=trafo,
rmfunc=adjoint,
)
]

else:
self.transforms = []
s1 = self.settings[0]
Expand Down Expand Up @@ -576,15 +578,15 @@ def adjoint_worker(i):
adjoint_worker(i)

return fhat

elif self.algorithm == "keops":
return GroupedCoefficients(self.settings, self.transforms[0].H @ other)
return GroupedCoefficients(self.settings, self.transforms[0].H @ other)

elif self.algorithm == "direct":
return GroupedCoefficients(
self.settings, (self.matrix.conj()).T @ other
)

elif isinstance(other, GC): # `f = F*fhat` (fhat = other)
if self.settings != other.settings:
raise ValueError(
Expand Down Expand Up @@ -612,10 +614,10 @@ def worker(i):
worker(i)

return sum(results)

elif self.algorithm == "keops":
return self.transforms[0] @ other.data

elif self.algorithm == "direct":
return self.matrix @ other.data
else:
Expand Down
8 changes: 4 additions & 4 deletions src/pyGroupedTransforms/NFFTtools.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import numpy as np

from pyGroupedTransforms import *

import torch
import pykeops
import torch
from pykeops.torch import LazyTensor

from pyGroupedTransforms import *


def datalength(
bandwidths: np.ndarray,
Expand Down Expand Up @@ -173,6 +172,7 @@ def adjoint(f): # function adjoint(f::Vector{ComplexF64})::Vector{ComplexF64}
dtype=np.complex128, shape=(M, N), mfunc=trafo, rmfunc=adjoint
)


def get_matrix(
bandwidths, X
): # get_matrix(bandwidths::Vector{Int}, X::Array{Float64})::Array{ComplexF64}
Expand Down