Skip to content

Fix diagonals of Kronecker products with rectangular factors - #150

Open
ktz03 wants to merge 1 commit into
cornellius-gp:mainfrom
ktz03:fix/rectangular-kronecker-diagonal
Open

ktz03 wants to merge 1 commit into
cornellius-gp:mainfrom
ktz03:fix/rectangular-kronecker-diagonal

Conversation

@ktz03

@ktz03 ktz03 commented Oct 18, 2026

Copy link
Copy Markdown

For a square Kronecker product with rectangular factors, diagonal() currently returns too few entries and incorrect values. The product of the factors' diagonals is valid when each factor is square, but not for rectangular factors whose product is square.

Use the existing indexed diagonal implementation for nonempty products with rectangular factors, retaining the optimized path for square factors and the existing empty-product behavior. This does not materialize the full product. The regressions compare values and gradients against torch.kron, including three factors, broadcast batches, and empty-product controls.

import torch
from linear_operator.operators import KroneckerProductLinearOperator

a = torch.arange(1, 7, dtype=torch.float64).reshape(2, 3)
b = torch.arange(1, 7, dtype=torch.float64).reshape(3, 2)
op = KroneckerProductLinearOperator(a, b)
print(op.shape)       # torch.Size([6, 6])
print(op.diagonal())
# Before: tensor([1., 4., 5., 20.], dtype=torch.float64)
# After:  tensor([1., 4., 10., 10., 18., 36.], dtype=torch.float64)

Validation on Windows CPU, Python 3.12.14, PyTorch 2.14.1:

  • The three complete Kronecker test modules run 476 tests: seven failing regression subcases before the fix, all passing after it.
  • python -m unittest discover -q runs 5,049 tests. The remaining failure, test_psd_safe_cholesky_psd, also fails on the unchanged production baseline because the expected NumericalWarning is absent. The full suite is not green.
  • Full-repository Flake8 5.0.4 with flake8-print 5.0.0 and the changed-file pinned formatter checks pass. Pre-commit passes 11 hooks and skips three inapplicable hooks; its require-ascii launcher fails to resolve a Windows interpreter path. Running that same pinned ASCII-check script directly passes on both changed files.

No CUDA, other-version matrix, or performance validation was run.

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.

1 participant