Skip to content

[FIX] Don't reference pykeops.torch when torch is not installed#83

Open
flohorovicic wants to merge 1 commit into
gempy-project:mainfrom
flohorovicic:claude/intelligent-cerf-22d335
Open

[FIX] Don't reference pykeops.torch when torch is not installed#83
flohorovicic wants to merge 1 commit into
gempy-project:mainfrom
flohorovicic:claude/intelligent-cerf-22d335

Conversation

@flohorovicic

Copy link
Copy Markdown
Member

Problem

gempy_engine/core/backend_tensor.py imports only pykeops.numpy at the top (guarded by is_pykeops_installed), but the wrapped pykeops functions referenced pykeops.torch.LazyTensor unconditionally in four places (_exp, _sum, _divide, _sqrt_fn).

pykeops only exposes its torch submodule when torch is importable, so running the numpy backend with pykeops enabled in an environment without torch crashed with:

AttributeError: module 'pykeops' has no attribute 'torch'

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_TYPES tuple at import time that always contains pykeops.numpy.LazyTensor and adds pykeops.torch.LazyTensor only when import pykeops.torch succeeds. All four type checks now use that tuple; the redundant import pykeops statements inside the wrapped functions were removed.

Verification

  • In a venv with numpy + pykeops and no torch: on main, a script enabling the pykeops numpy backend and calling the four wrapped functions with a pykeops.numpy.LazyTensor-typed object reproduces the exact AttributeError; on this branch all four functions dispatch correctly and plain numpy arrays still take the numpy path.
  • In an environment with torch + pykeops: _LAZY_TENSOR_TYPES contains both the numpy and torch LazyTensor classes, so behavior is unchanged.

🤖 Generated with Claude Code

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>

Leguark commented Jul 17, 2026

Copy link
Copy Markdown
Member

this was trying to use an environment Pykeops + numpy withouth torch or in an environment only with numpy did not work either?

@flohorovicic

Copy link
Copy Markdown
Member Author

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.

Leguark commented Jul 20, 2026

Copy link
Copy Markdown
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants