mirror of
https://github.com/pytorch/pytorch.git
synced 2025-10-20 21:14:14 +08:00
Revert "Fix logdet returning finite values for singular matrices on CUDA (#157910)"
This reverts commit 7d4228dbfd13d1ac8fac2c78c042dbb8314f042d. Reverted https://github.com/pytorch/pytorch/pull/157910 on behalf of https://github.com/huydhn due to Sorry for reverting your change but this seems to fail some internal tests accuracy ([comment](https://github.com/pytorch/pytorch/pull/157910#issuecomment-3064368647))
This commit is contained in:
@ -8943,166 +8943,6 @@ scipy_lobpcg | {eq_err_scipy:10.2e} | {eq_err_general_scipy:10.2e} | {iters2:
|
||||
s[-1] = 0
|
||||
test(u.mm(s.diag()).mm(v))
|
||||
|
||||
# Test case from PyTorch issue #154312: numerically singular matrix
|
||||
# This matrix is mathematically singular but has tiny non-zero diagonal elements
|
||||
# in LU factorization, requiring threshold-based singularity detection
|
||||
issue_154312_matrix = torch.tensor([[1.0, 2.0, 3.0],
|
||||
[2.0, 5.0, 6.0],
|
||||
[3.0, 6.0, 9.0]], dtype=dtype, device=device)
|
||||
test_single_det(issue_154312_matrix,
|
||||
(torch.zeros((), dtype=dtype, device=device),
|
||||
torch.full((), -inf, dtype=dtype, device=device)),
|
||||
'issue #154312 numerically singular matrix')
|
||||
|
||||
# Additional edge cases
|
||||
|
||||
# Test 1: Exact zero matrix (should be detected by both tiers)
|
||||
zero_matrix = torch.zeros(3, 3, dtype=dtype, device=device)
|
||||
test_single_det(zero_matrix,
|
||||
(torch.zeros((), dtype=dtype, device=device),
|
||||
torch.full((), -inf, dtype=dtype, device=device)),
|
||||
'exact zero matrix')
|
||||
|
||||
# Test 2: Matrix with one zero row (rank deficient)
|
||||
zero_row_matrix = torch.tensor([[1.0, 2.0, 3.0],
|
||||
[0.0, 0.0, 0.0],
|
||||
[4.0, 5.0, 6.0]], dtype=dtype, device=device)
|
||||
test_single_det(zero_row_matrix,
|
||||
(torch.zeros((), dtype=dtype, device=device),
|
||||
torch.full((), -inf, dtype=dtype, device=device)),
|
||||
'matrix with zero row')
|
||||
|
||||
# Test 3: Matrix with linearly dependent rows (rank deficient)
|
||||
dependent_rows_matrix = torch.tensor([[1.0, 2.0, 3.0],
|
||||
[4.0, 5.0, 6.0],
|
||||
[5.0, 7.0, 9.0]], dtype=dtype, device=device) # row3 = row1 + row2
|
||||
test_single_det(dependent_rows_matrix,
|
||||
(torch.zeros((), dtype=dtype, device=device),
|
||||
torch.full((), -inf, dtype=dtype, device=device)),
|
||||
'matrix with linearly dependent rows')
|
||||
|
||||
# Test 4: Nearly singular matrix (very small determinant)
|
||||
nearly_singular = torch.tensor([[1.0, 2.0, 3.0],
|
||||
[4.0, 5.0, 6.0],
|
||||
[7.0, 8.0, 9.0 + 1e-10]], dtype=dtype, device=device)
|
||||
# This should be detected as singular by our threshold
|
||||
test_single_det(nearly_singular,
|
||||
(torch.zeros((), dtype=dtype, device=device),
|
||||
torch.full((), -inf, dtype=dtype, device=device)),
|
||||
'nearly singular matrix')
|
||||
|
||||
# Test 5: Well-conditioned matrix (should not be singular)
|
||||
well_conditioned = torch.tensor([[1.0, 0.0, 0.0],
|
||||
[0.0, 2.0, 0.0],
|
||||
[0.0, 0.0, 3.0]], dtype=dtype, device=device)
|
||||
expected_det = 6.0
|
||||
expected_logdet = torch.log(torch.tensor(expected_det, dtype=dtype, device=device))
|
||||
test_single_det(well_conditioned,
|
||||
(torch.ones((), dtype=dtype, device=device),
|
||||
expected_logdet),
|
||||
'well conditioned diagonal matrix')
|
||||
|
||||
# Test 6: Negative determinant matrix
|
||||
negative_det_matrix = torch.tensor([[1.0, 2.0],
|
||||
[3.0, 4.0]], dtype=dtype, device=device)
|
||||
# det = 1*4 - 2*3 = -2
|
||||
expected_logdet = torch.log(torch.tensor(2.0, dtype=dtype, device=device))
|
||||
test_single_det(negative_det_matrix,
|
||||
(-torch.ones((), dtype=dtype, device=device),
|
||||
expected_logdet),
|
||||
'negative determinant matrix')
|
||||
|
||||
# Test 7: Batched singular matrices (mix of singular and non-singular)
|
||||
# Use fixed 3x3 matrices for batching test
|
||||
batch_singular = torch.stack([
|
||||
issue_154312_matrix, # singular (3x3)
|
||||
well_conditioned, # non-singular (3x3)
|
||||
zero_matrix, # singular (3x3)
|
||||
])
|
||||
|
||||
|
||||
expected_signs = torch.tensor([0.0, 1.0, 0.0], dtype=dtype, device=device)
|
||||
expected_logdets = torch.tensor([-float('inf'),
|
||||
torch.log(torch.tensor(6.0, dtype=dtype, device=device)).item(),
|
||||
-float('inf')],
|
||||
dtype=dtype, device=device)
|
||||
|
||||
batch_result = torch.linalg.slogdet(batch_singular)
|
||||
# Test signs
|
||||
self.assertEqual(batch_result[0], expected_signs,
|
||||
msg='batched singular detection failed - signs mismatch')
|
||||
# Test logdets (allowing for inf values)
|
||||
for i in range(len(expected_logdets)):
|
||||
if torch.isfinite(expected_logdets[i]):
|
||||
self.assertLess(abs(batch_result[1][i] - expected_logdets[i]), 1e-5,
|
||||
msg=f'batched logdet mismatch at index {i}')
|
||||
else:
|
||||
self.assertTrue(torch.isneginf(batch_result[1][i]),
|
||||
msg=f'expected -inf but got {batch_result[1][i]} at index {i}')
|
||||
|
||||
# Test 8: Identity matrix (should always work)
|
||||
identity_matrix = torch.eye(3, dtype=dtype, device=device)
|
||||
test_single_det(identity_matrix,
|
||||
(torch.ones((), dtype=dtype, device=device),
|
||||
torch.zeros((), dtype=dtype, device=device)),
|
||||
'identity matrix')
|
||||
|
||||
# Test 9: Scaled identity (determinant = scale^n)
|
||||
scale = 2.0
|
||||
scaled_identity = scale * torch.eye(3, dtype=dtype, device=device)
|
||||
expected_det = scale ** 3
|
||||
expected_logdet = torch.log(torch.tensor(expected_det, dtype=dtype, device=device))
|
||||
test_single_det(scaled_identity,
|
||||
(torch.ones((), dtype=dtype, device=device),
|
||||
expected_logdet),
|
||||
f'scaled identity matrix (scale={scale})')
|
||||
|
||||
# Test 10: Large values (test numerical stability)
|
||||
large_scale = 10.0 # Use smaller scale to avoid precision issues: 10^3 = 1000
|
||||
large_scaled_identity = large_scale * torch.eye(3, dtype=dtype, device=device)
|
||||
expected_det = large_scale ** 3
|
||||
expected_logdet = torch.log(torch.tensor(expected_det, dtype=dtype, device=device))
|
||||
test_single_det(large_scaled_identity,
|
||||
(torch.ones((), dtype=dtype, device=device),
|
||||
expected_logdet),
|
||||
f'large scaled identity matrix (scale={large_scale})')
|
||||
|
||||
# Test 11: Small but reasonable values (test numerical stability)
|
||||
# Use 0.1 instead of very small values to avoid being caught by our conservative threshold
|
||||
small_scale = 0.1
|
||||
small_scaled_identity = small_scale * torch.eye(3, dtype=dtype, device=device)
|
||||
expected_det = small_scale ** 3
|
||||
expected_logdet = torch.log(torch.tensor(expected_det, dtype=dtype, device=device))
|
||||
test_single_det(small_scaled_identity,
|
||||
(torch.ones((), dtype=dtype, device=device),
|
||||
expected_logdet),
|
||||
f'small scaled identity matrix (scale={small_scale})')
|
||||
|
||||
# Test 12: Empty matrices (0x0) - determinant should be 1 by convention
|
||||
empty_matrix = torch.zeros((0, 0), dtype=dtype, device=device)
|
||||
test_single_det(empty_matrix,
|
||||
(torch.ones((), dtype=dtype, device=device),
|
||||
torch.zeros((), dtype=dtype, device=device)),
|
||||
'empty 0x0 matrix')
|
||||
|
||||
# Test 13: Batched empty matrices
|
||||
batched_empty = torch.zeros((3, 0, 0), dtype=dtype, device=device)
|
||||
batch_result = torch.linalg.slogdet(batched_empty)
|
||||
expected_signs = torch.ones(3, dtype=dtype, device=device)
|
||||
expected_logdets = torch.zeros(3, dtype=dtype, device=device)
|
||||
self.assertEqual(batch_result[0], expected_signs,
|
||||
msg='batched empty matrices - signs should be 1')
|
||||
self.assertEqual(batch_result[1], expected_logdets,
|
||||
msg='batched empty matrices - logdets should be 0')
|
||||
|
||||
# Test 14: Zero batch dimension with 0x0 matrices
|
||||
zero_batch_empty = torch.zeros((0, 0, 0), dtype=dtype, device=device)
|
||||
zero_batch_result = torch.linalg.slogdet(zero_batch_empty)
|
||||
self.assertEqual(zero_batch_result[0].shape, torch.Size([0]),
|
||||
msg='zero batch empty matrices - sign shape')
|
||||
self.assertEqual(zero_batch_result[1].shape, torch.Size([0]),
|
||||
msg='zero batch empty matrices - logdet shape')
|
||||
|
||||
# Small values to test numerical stability. Note that we don't scale
|
||||
# this matrix.
|
||||
r = torch.randn(512, 512, dtype=dtype, device=device)
|
||||
@ -9161,7 +9001,6 @@ scipy_lobpcg | {eq_err_scipy:10.2e} | {eq_err_general_scipy:10.2e} | {iters2:
|
||||
run_test(matsize, batchdims, mat_chars=['sym', 'sym_pd', 'sym_psd'])
|
||||
run_test(matsize, batchdims, mat_chars=['sing', 'non_sing'])
|
||||
|
||||
|
||||
@skipCUDAIfNoMagma
|
||||
@skipCPUIfNoLapack
|
||||
@dtypes(*floating_and_complex_types())
|
||||
|
Reference in New Issue
Block a user