165165# First process compiles; all others (same node or different) reuse cache.
166166# ---------------------------------------------------------------------------
167167_module = None
168+ _module_failed = False
168169
169170
170171def _get_module ():
171- global _module
172- if _module is None :
172+ global _module , _module_failed
173+ if _module is not None :
174+ return _module
175+ if _module_failed :
176+ return None
177+
178+ import os
179+ import sys
180+
181+ try :
173182 import fcntl
174- import os
175- import sys
183+ except ImportError :
184+ # fcntl is not available on non-POSIX platforms (e.g. Windows)
185+ _module_failed = True
186+ return None
176187
188+ try :
177189 # Ensure ninja (installed via pip) is on PATH for compute nodes
178190 bin_dir = os .path .dirname (sys .executable )
179191 if bin_dir not in os .environ .get ("PATH" , "" ):
@@ -233,6 +245,10 @@ def _get_module():
233245 finally :
234246 fcntl .lockf (lock_fd , fcntl .LOCK_UN )
235247 os .close (lock_fd )
248+ except Exception :
249+ _module_failed = True
250+ return None
251+
236252 return _module
237253
238254
@@ -257,6 +273,8 @@ def forward(ctx, density_cube, wa, wbwc, map_size):
257273 ctx .cube_shape = density_cube .shape
258274
259275 mod = _get_module ()
276+ if mod is None :
277+ raise RuntimeError ("C++ cpu_scatter module not available" )
260278 result = torch .zeros (map_size , dtype = density_cube .dtype ,
261279 device = density_cube .device )
262280 mod .structured_scatter_add (
@@ -273,6 +291,8 @@ def backward(ctx, grad_output):
273291 wa , wbwc = ctx .saved_tensors
274292 C , nx , ny , nz = ctx .cube_shape
275293 mod = _get_module ()
294+ if mod is None :
295+ raise RuntimeError ("C++ cpu_scatter module not available" )
276296 grad_cube = mod .structured_gather (
277297 grad_output .contiguous (),
278298 wa .contiguous (),
0 commit comments