Skip to content

Fix CP norm for complex weights - #656

Merged
aarmey merged 2 commits into
tensorly:mainfrom
ktz03:fix/cp-norm-complex-weights
Oct 11, 2026
Merged

aarmey merged 2 commits into
tensorly:mainfrom
ktz03:fix/cp-norm-complex-weights

Conversation

@ktz03

@ktz03 ktz03 commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

cp_norm and CPTensor.norm() can return an imaginary value for a tensor with complex CP weights. For a weight of 1j and real factors [[1], [2]] and [[3], [4]], both return 11.180339887498949j, while the represented tensor has Frobenius norm 11.180339887498949.

Conjugate the second weight axis in the weight outer product, extending the complex-factor support introduced by @aarmey in #423 to complex weights. The squared norm is real, so take its real part before the square root. This also supports Paddle CPU, whose complex sqrt kernel is unavailable.

Expose the backend real operation through the existing dispatch mechanism. Register native operations for the dense backends and NumPy sparse backend. Paddle returns an already-real input unchanged because its native real kernel accepts complex tensors only. Add scalar/vector real-part tests for float32/float64/complex64/complex128, norm output dtype checks, and float32/float64 weighted and unweighted controls.

Local validation on Python 3.12.14, with the core tensor algebra:

  • Windows, NumPy 2.3.5 / SciPy 1.16.3 / Sparse 0.19.2: complete suite 791 passed, 3 skipped, 1 xfailed. Eight additional public sparse real-part controls preserve dtype, shape, sparse structure and inputs.
  • Windows, PyTorch 2.8.0 on CPU: complete suite 786 passed, 4 skipped, 1 xfailed, with NumPy and Torch random seeds set to 1.
  • Ubuntu through WSL, Paddle 3.0.0b1 on CPU: complete suite 702 passed, 4 skipped, 1 xfailed. The new real operation covers real and complex input dtypes. This run is bound to the 12 modified core files; the later sparse-only registration is covered by the NumPy complete suite and the eight sparse controls above.
  • Keeping the old weight formula while sharing the new tests, real wrappers and real-valued norm projection gives 8 failures and 8 controls for the 16 complex-weight cases on Paddle; all 16 pass with the corrected formula. This is a controlled comparison over the exact base archive, with those explicit overlays.
  • Black 26.10.0's Python API checks all 170 tracked Python files, using the project target versions and fast=False safety checks; every file is unchanged. The CLI traversal attempts did not complete.

These validation ranges overlap. JAX, TensorFlow and CuPy bindings were inspected statically; their runtime suites were not run locally. Existing Windows test dependencies were reused, and the isolated Paddle environment uses Python 3.12.14 while official CI currently uses 3.12.15. GPU, model accuracy and performance tests were not run. Test tolerances, skip conditions and CI configuration are unchanged.

Fixes #655.

@codecov

codecov Bot commented Oct 10, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.05882% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 91.86%. Comparing base (bbd985b) to head (244cd7f).
⚠️ Report is 19 commits behind head on main.

Files with missing lines Patch % Lines
tensorly/backend/core.py 66.66% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #656      +/-   ##
==========================================
+ Coverage   90.71%   91.86%   +1.15%     
==========================================
  Files         132      132              
  Lines        8056     8446     +390     
==========================================
+ Hits         7308     7759     +451     
+ Misses        748      687      -61     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@aarmey
aarmey merged commit b75ca10 into tensorly:main Oct 11, 2026
9 checks passed
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.

cp_norm returns an imaginary norm for complex CP weights

2 participants