@@ -274,6 +274,13 @@ def __init__(
274274 elif algorithm == "keops" :
275275 self .matrix = np .empty ((0 ,0 ), dtype = object )
276276
277+ if torch .cuda .is_available ():
278+ device = "cuda"
279+ elif torch .mps .is_available ():
280+ device = "mps"
281+ else :
282+ device = "cpu"
283+
277284 self .transforms = [
278285 DeferredLinearOperator ()
279286 for _ in range (len (self .settings ))
@@ -288,44 +295,33 @@ def __init__(
288295 if len (s .bandwidths ) == 0 :
289296 full = np .zeros ((1 , D ), dtype = np .int32 )
290297
291- elif self .system == "cos" :
292-
293- local = np .atleast_2d (
294- s .mode .index_set_without_zeros (
295- np .array (s .bandwidths , dtype = np .int32 )
296- )
297- ).T
298-
299- full = np .zeros ((local .shape [0 ], D ), dtype = np .int32 )
300-
301- for i , dim in enumerate (s .u ):
302- full [:, dim ] = local [:, i ]
303-
304- else :
305- local = index_set_without_zeros (
298+
299+ local = np .atleast_2d (
300+ s .mode .index_set_without_zeros (
306301 np .array (s .bandwidths , dtype = np .int32 )
307- ).T
302+ )
303+ ).T
308304
309- full = np .zeros ((local .shape [0 ], D ), dtype = np .int32 )
305+ full = np .zeros ((local .shape [0 ], D ), dtype = np .int32 )
310306
311- for i , dim in enumerate (s .u ):
312- full [:, dim ] = local [:, i ]
307+ for i , dim in enumerate (s .u ):
308+ full [:, dim ] = local [:, i ]
313309
314310 freq_list .append (full )
315311
316312 freq = np .vstack (freq_list )
317313
318- X_torch = torch .tensor (X , dtype = torch .float64 , device = "cuda" )
314+ X_torch = torch .tensor (X , dtype = torch .float64 , device = device )
319315 I_torch = torch .tensor (
320316 freq ,
321317 dtype = torch .float64 ,
322- device = "cuda"
318+ device = device
323319 )
324320
325321 def trafo (fhat ):
326322 if self .system == "cos" or self .system == "cheb" :
327323 mult = np .sqrt (2.0 ) ** np .count_nonzero (freq , axis = 1 )
328- mult_torch = torch .tensor (mult , dtype = torch .float64 , device = "cuda" )
324+ mult_torch = torch .tensor (mult , dtype = torch .float64 , device = device )
329325
330326 kernel = 1.0
331327 for i in range (D ):
@@ -334,7 +330,7 @@ def trafo(fhat):
334330 kernel = kernel * (2 * torch .pi * Xi * Ki ).cos ()
335331
336332 fhat_torch = (
337- torch .as_tensor (fhat , dtype = torch .float64 , device = "cuda" )
333+ torch .as_tensor (fhat , dtype = torch .float64 , device = device )
338334 * mult_torch
339335 )
340336 fhat_j = LazyTensor (fhat_torch [None , :, None ].contiguous ())
@@ -354,7 +350,7 @@ def trafo(fhat):
354350 two_pi_phase = - 2 * torch .pi * phase_fwd
355351 kernel = two_pi_phase .cos () + 1j * two_pi_phase .sin ()
356352
357- fhat_torch = torch .tensor (fhat , dtype = torch .complex128 , device = "cuda" )
353+ fhat_torch = torch .tensor (fhat , dtype = torch .complex128 , device = device )
358354 fhat_j = LazyTensor (fhat_torch [None , :, None ].contiguous ())
359355
360356 try :
@@ -369,11 +365,9 @@ def trafo(fhat):
369365 def adjoint (f ):
370366 if self .system == "cos" or self .system == "cheb" :
371367 mult = np .sqrt (2.0 ) ** np .count_nonzero (freq , axis = 1 )
372- mult_torch = torch .tensor (mult , dtype = torch .float64 , device = "cuda" )
373368
374- f_torch = (
375- torch .as_tensor (f , dtype = torch .float64 , device = "cuda" )
376- * mult_torch
369+ f_torch = torch .as_tensor (
370+ f , dtype = torch .float64 , device = device
377371 ).contiguous ()
378372
379373 kernel = 1.0
@@ -388,12 +382,12 @@ def adjoint(f):
388382 result = (kernel * f_i ).sum (dim = 1 )
389383 torch .cuda .synchronize ()
390384 result = result .squeeze ().cpu ().numpy ()
391- return result
385+ return result * mult
392386 except Exception as e :
393387 print ("Error in KeOps adjoint:" , e )
394388 return None
395389 else :
396- f_torch = torch .tensor (f , dtype = torch .complex128 , device = "cuda" ).contiguous ()
390+ f_torch = torch .tensor (f , dtype = torch .complex128 , device = device ).contiguous ()
397391
398392 X_i = LazyTensor (X_torch [None , :, :].contiguous ())
399393 K_j = LazyTensor (I_torch [:, None , :].contiguous ())
0 commit comments