Skip to content

Commit 61e69af

Browse files
author
HatPdotS
committed
v0.4.2 minor bug fixes
1 parent 96798de commit 61e69af

3 files changed

Lines changed: 34 additions & 7 deletions

File tree

docs/changelog.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Version 0.4.2
55
-------------
66

77
- Fixed bug where reflection data object in the refinement was not created on cuda when specified.
8+
- Fixed macos crash not catching compilation error in c++ extension for scatter add
89

910
Version 0.4.1
1011
-------------

torchref/base/electron_density/main.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,18 @@ def _get_separable_triton():
8383

8484

8585
def _get_cpp_scatter():
86-
"""Return the C++ parallel scatter_add, or None if unavailable."""
86+
"""Return the C++ parallel scatter_add, or None if unavailable.
87+
88+
Eagerly triggers the C++ compilation so that failures (missing ninja,
89+
unsupported compiler flags, etc.) are caught here rather than mid-calculation.
90+
"""
8791
global _cpp_scatter_fn, _cpp_scatter_checked
8892
if not _cpp_scatter_checked:
8993
try:
90-
from torchref.base.kernels.cpu_scatter import structured_scatter_add
91-
_cpp_scatter_fn = structured_scatter_add
94+
from torchref.base.kernels.cpu_scatter import structured_scatter_add, _get_module
95+
# Trigger compilation now — _get_module returns None on failure
96+
if _get_module() is not None:
97+
_cpp_scatter_fn = structured_scatter_add
9298
except Exception:
9399
pass
94100
_cpp_scatter_checked = True

torchref/base/kernels/cpu_scatter.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,15 +165,27 @@
165165
# First process compiles; all others (same node or different) reuse cache.
166166
# ---------------------------------------------------------------------------
167167
_module = None
168+
_module_failed = False
168169

169170

170171
def _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

Comments
 (0)