@@ -275,16 +275,24 @@ def compute_filters_and_norm(refImg, norm_frames=True, spatial_smooth=1.15, spat
275275 maskMul , maskOffset , cfRefImg = rigid .compute_masks_ref_smooth_fft (refImg = rimg , maskSlope = spatial_taper ,
276276 smooth_sigma = spatial_smooth )
277277 Ly , Lx = refImg .shape
278+ # MPS backend does not support float64, convert to float32
279+ if device .type == "mps" :
280+ maskMul , maskOffset = maskMul .to (torch .float32 ), maskOffset .to (torch .float32 )
281+ cfRefImg = cfRefImg .to (torch .complex64 )
278282 maskMul , maskOffset = maskMul .to (device ), maskOffset .to (device )
279283 cfRefImg = cfRefImg .to (device )
280284 blocks = []
281285 if block_size is not None :
282286 blocks = nonrigid .make_blocks (Ly = Ly , Lx = Lx , block_size = block_size ,
283287 lpad = lpad , subpixel = subpixel )
284288 maskMulNR , maskOffsetNR , cfRefImgNR = nonrigid .compute_masks_ref_smooth_fft (
285- refImg0 = rimg , maskSlope = spatial_taper , smooth_sigma = spatial_smooth ,
289+ refImg0 = rimg , maskSlope = spatial_taper , smooth_sigma = spatial_smooth ,
286290 yblock = blocks [0 ], xblock = blocks [1 ],
287291 )
292+ # MPS backend does not support float64, convert to float32
293+ if device .type == "mps" :
294+ maskMulNR , maskOffsetNR = maskMulNR .to (torch .float32 ), maskOffsetNR .to (torch .float32 )
295+ cfRefImgNR = cfRefImgNR .to (torch .complex64 )
288296 maskMulNR , maskOffsetNR = maskMulNR .to (device ), maskOffsetNR .to (device )
289297 cfRefImgNR = cfRefImgNR .to (device )
290298
@@ -440,6 +448,10 @@ def shift_frames(fr_torch, yoff, xoff, yoff1=None, xoff1=None, blocks=None,
440448 if fr_torch .device .type == "cuda" :
441449 yoff1 = torch .from_numpy (yoff1 ).pin_memory ().to (device )
442450 xoff1 = torch .from_numpy (xoff1 ).pin_memory ().to (device )
451+ elif device .type == "mps" :
452+ # MPS backend does not support float64
453+ yoff1 = torch .from_numpy (yoff1 ).to (torch .float32 ).to (device )
454+ xoff1 = torch .from_numpy (xoff1 ).to (torch .float32 ).to (device )
443455 else :
444456 yoff1 = torch .from_numpy (yoff1 ).to (device )
445457 xoff1 = torch .from_numpy (xoff1 ).to (device )
@@ -580,7 +592,9 @@ def register_frames(f_align_in, refImg, f_align_out=None, batch_size=100,
580592 if upsample_meanImg :
581593 if not isinstance (upsample_meanImg , (np .ndarray , list , tuple )):
582594 upsample_meanImg = [upsample_meanImg , upsample_meanImg ]
583- mean_img_ups = torch .zeros ((int (Ly * upsample_meanImg [0 ]), int (Lx * upsample_meanImg [1 ])), dtype = torch .double , device = device )
595+ # MPS backend does not support float64
596+ ups_dtype = torch .float32 if device .type == "mps" else torch .double
597+ mean_img_ups = torch .zeros ((int (Ly * upsample_meanImg [0 ]), int (Lx * upsample_meanImg [1 ])), dtype = ups_dtype , device = device )
584598 counts_ups = torch .zeros ((int (Ly * upsample_meanImg [0 ]), int (Lx * upsample_meanImg [1 ])), dtype = torch .int , device = device )
585599 else :
586600 mean_img_ups , counts_ups , meanImg_ups = None , None , None
@@ -890,7 +904,10 @@ def registration_wrapper(f_reg, f_raw=None, f_reg_chan2=None, f_raw_chan2=None,
890904
891905 nchannels = 2 if f_alt_in is not None else 1
892906 logger .info (f"registering { nchannels } channels" )
893-
907+ if device .type == "mps" :
908+ logger .warning ("MPS device does not support float64, using float32 for registration. "
909+ "If you encounter registration issues, try using cuda or cpu instead." )
910+
894911 ### ----- compute reference image and bidiphase shift -------------- ###
895912 n_frames , Ly , Lx = f_align_in .shape
896913 badframes0 = np .zeros (n_frames , "bool" ) if badframes is None else badframes .copy ()
0 commit comments