Skip to content

Support conjugate tensor views in to_numpy - #1173

Open
ktz03 wants to merge 2 commits into
skorch-dev:masterfrom
ktz03:fix/conjugate-numpy-0648
Open

ktz03 wants to merge 2 commits into
skorch-dev:masterfrom
ktz03:fix/conjugate-numpy-0648

Conversation

@ktz03

@ktz03 ktz03 commented Oct 15, 2026

Copy link
Copy Markdown

to_numpy raises RuntimeError for a complex PyTorch tensor with its conjugate bit set, including otherwise-supported list, tuple and dictionary inputs. For example, converting torch.tensor([1 + 2j]).conj() fails instead of returning [1 - 2j].

Resolve conjugate views after the existing CPU/detach handling and before NumPy conversion. Guard the optional method so older tensors without resolve_conj retain their existing path. Add regressions covering complex64/complex128, gradients, non-contiguous views and supported containers, plus a changelog entry.

Validation on Windows, Python 3.12.14, NumPy 2.3.5 and PyTorch 2.8.0 CPU:

  • Complete test_utils.py: 114 passed, 28 skipped; the same final tests on unchanged production code produce 16 failures and 98 passes.
  • Public conversions: all 16 conjugate cases now pass; 32 ordinary real/complex controls retain exact values, dtypes and shapes. Input data, conjugate flags and gradient requirements remain unchanged.
  • pylint skorch: the same 1,246 baseline diagnostics, with no added diagnostics; git diff --check passes.

The full project suite, GPU and the currently documented PyTorch version matrix were not run locally.

@githubnemo githubnemo left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR, this is indeed an issue with the numpy conversion. I have some comments to make the code more consistent with the current code base.

Comment thread skorch/utils.py Resolved
Comment thread skorch/tests/test_utils.py Resolved
@ktz03

ktz03 commented Oct 20, 2026 •

Copy link
Copy Markdown
Author

Updated the guard to call resolve_conj only when is_conj is available and true. The conjugate case now uses a fixture and the existing list, tuple, and dict conversion tests; the fixture uses complex values so conj() actually sets the conjugate bit. The utils module passes 99 tests with 28 existing skips on CPU, including unavailable devices and pandas.

@githubnemo githubnemo left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, the changes look good.

Let's see what the CI says, otherwise it's good to go.

@githubnemo

Copy link
Copy Markdown
Collaborator

@ktz03 can you resolve the conflicts? :)

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants