Skip to content

Commit 56147a6

Browse files
throw runtime error when SVD on no block tensors
1 parent 7ffff5b commit 56147a6

3 files changed

Lines changed: 40 additions & 2 deletions

File tree

include/qlten/mpi_tensor_manipulation/ten_decomp/mpi_svd.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,16 @@ void MPITensorSVDExecutor<TenElemT, QNT>::Execute(void) {
7575
mpi_comm_
7676
);
7777
auto kept_sv_info = this->CalcTruncedSVInfo_(idx_raw_data_svd_res);
78+
79+
// Empty SVD result: input tensor has no valid blocks for this QN configuration
80+
if (kept_sv_info.empty()) {
81+
DeleteDataBlkMatSvdResMap(idx_raw_data_svd_res);
82+
throw std::runtime_error(
83+
"MPI SVD failed: empty result. The input tensor has no data blocks "
84+
"compatible with the specified quantum number divergence (lqndiv)."
85+
);
86+
}
87+
7888
this->ConstructSVDResTens_(kept_sv_info, idx_raw_data_svd_res);
7989
DeleteDataBlkMatSvdResMap(idx_raw_data_svd_res);
8090

include/qlten/tensor_manipulation/ten_decomp/ten_svd.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@
1313
#ifndef QLTEN_TENSOR_MANIPULATION_TEN_DECOMP_TEN_SVD_H
1414
#define QLTEN_TENSOR_MANIPULATION_TEN_DECOMP_TEN_SVD_H
1515

16-
#include <cassert> // assert
17-
#include <utility> // pair
16+
#include <cassert> // assert
17+
#include <utility> // pair
18+
#include <stdexcept> // runtime_error
1819

1920
#include "qlten/framework/bases/executor.h" // Executor
2021
#include "qlten/qltensor_all.h"
@@ -192,6 +193,16 @@ void TensorSVDExecutor<TenElemT, QNT>::Execute(void) {
192193
idx_ten_decomp_data_blk_mat_map_
193194
);
194195
auto kept_sv_info = CalcTruncedSVInfo_(idx_raw_data_svd_res);
196+
197+
// Empty SVD result: input tensor has no valid blocks for this QN configuration
198+
if (kept_sv_info.empty()) {
199+
DeleteDataBlkMatSvdResMap(idx_raw_data_svd_res);
200+
throw std::runtime_error(
201+
"SVD failed: empty result. The input tensor has no data blocks "
202+
"compatible with the specified quantum number divergence (lqndiv)."
203+
);
204+
}
205+
195206
ConstructSVDResTens_(kept_sv_info, idx_raw_data_svd_res);
196207
DeleteDataBlkMatSvdResMap(idx_raw_data_svd_res);
197208

tests/test_tensor_manipulation/test_ten_svd.cc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,23 @@ TEST_F(TestSvd, 3DCaseFloat) {
697697
&qn0);
698698
}
699699

700+
// Test that SVD throws when input tensor has no data blocks
701+
TEST_F(TestSvd, EmptySVDResultThrows) {
702+
// Create a tensor without any data blocks (not initialized with Random)
703+
DQLTensor empty_tensor({idx_in_s, idx_out_s});
704+
// Don't call Random() - tensor has structure but no actual data blocks
705+
706+
DQLTensor u, vt;
707+
QLTensor<QLTEN_Double, U1QN> s;
708+
QLTEN_Double trunc_err;
709+
size_t D;
710+
711+
EXPECT_THROW(
712+
SVD(&empty_tensor, 1, qn0, 0.0, 1, 100, &u, &s, &vt, &trunc_err, &D),
713+
std::runtime_error
714+
);
715+
}
716+
700717
struct TestSvdOmpParallel : public testing::Test {
701718
std::string qn_nm = "qn";
702719
};

0 commit comments

Comments
 (0)