[FIX] Don't reference pykeops.torch when torch is not installed#83
[FIX] Don't reference pykeops.torch when torch is not installed#83flohorovicic wants to merge 1 commit into
Conversation
pykeops only exposes its torch submodule when torch is importable, so the unconditional pykeops.torch.LazyTensor references in the wrapped pykeops functions crashed with AttributeError when running the numpy backend with pykeops but without torch. Build a module-level _LAZY_TENSOR_TYPES tuple that includes the torch LazyTensor only when pykeops.torch imports, and use it in _exp, _sum, _divide and _sqrt_fn. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
|
this was trying to use an environment Pykeops + numpy withouth torch or in an environment only with numpy did not work either? |
|
Only the first one. An environment with just numpy (no pykeops) works fine both before and after this PR — the pykeops import at module level is guarded by is_pykeops_installed, and _wrap_pykeops_functions() is only called when pykeops is enabled, so the bad pykeops.torch references were never reached. I re-verified this in a numpy-only venv against main: the numpy backend works, and requesting pykeops there raises the existing clear "library is not installed: pykeops" error. The crash was specific to: pykeops installed + pykeops enabled (numpy backend) + torch not installed. In that case pykeops loads but never creates its torch submodule, so the unconditional pykeops.torch.LazyTensor checks raised AttributeError as soon as a wrapped function ran. |
|
okay good. So I do not have to hotfix the pip release. Are you able to run Keops on MacOS? I thought it is Linux only |
Problem
gempy_engine/core/backend_tensor.pyimports onlypykeops.numpyat the top (guarded byis_pykeops_installed), but the wrapped pykeops functions referencedpykeops.torch.LazyTensorunconditionally in four places (_exp,_sum,_divide,_sqrt_fn).pykeops only exposes its
torchsubmodule when torch is importable, so running the numpy backend with pykeops enabled in an environment without torch crashed with:inside
_sum(reached from_kernels_assembler._compute_distances_generic). Reproduced with gempy_engine 2026.0.3 running the Weisweiler model on macOS CPU (numpy backend +DEFAULT_PYKEOPS=True).Fix
Build a module-level
_LAZY_TENSOR_TYPEStuple at import time that always containspykeops.numpy.LazyTensorand addspykeops.torch.LazyTensoronly whenimport pykeops.torchsucceeds. All four type checks now use that tuple; the redundantimport pykeopsstatements inside the wrapped functions were removed.Verification
main, a script enabling the pykeops numpy backend and calling the four wrapped functions with apykeops.numpy.LazyTensor-typed object reproduces the exactAttributeError; on this branch all four functions dispatch correctly and plain numpy arrays still take the numpy path._LAZY_TENSOR_TYPEScontains both the numpy and torch LazyTensor classes, so behavior is unchanged.🤖 Generated with Claude Code