From 7494eadca5cdb65c86d81e3704f49b1f53f4de0b Mon Sep 17 00:00:00 2001 From: Vladimir Kutuev Date: Sat, 28 Nov 2026 03:21:16 +0300 Subject: [PATCH 1/3] fix(cpu): keep LIL values counter in sync in kernels and conversion The mxm and mxmT_masked CPU kernels append entries to LIL rows directly without updating the 'values' counter, and the Coo -> Lil conversion has the same flaw. Downstream consumers size their output from the counter (e.g. Lil -> Coo in Matrix::read), so a stale counter leads to out-of-bounds writes and segfaults when reading back the result. Maintain the counter in all three places, following the pattern already used by the kron, eadd, emult and transpose kernels. Add regression tests that read the mxm/mxmT results back through MemView. --- src/cpu/cpu_format_coo.hpp | 3 ++ src/cpu/cpu_mxm.hpp | 7 +++++ src/cpu/cpu_mxmT_masked.hpp | 7 +++++ tests/test_mxm.cpp | 32 ++++++++++++++++++++ tests/test_mxmT.cpp | 60 +++++++++++++++++++++++++++++++++++++ 5 files changed, 109 insertions(+) diff --git a/src/cpu/cpu_format_coo.hpp b/src/cpu/cpu_format_coo.hpp index 96080cba0..cb5c15e0a 100644 --- a/src/cpu/cpu_format_coo.hpp +++ b/src/cpu/cpu_format_coo.hpp @@ -73,6 +73,9 @@ namespace spla { Rr[i].emplace_back(j, x); } + + // keep the LIL values counter in sync with the actual row contents + out.values = in.values; } template diff --git a/src/cpu/cpu_mxm.hpp b/src/cpu/cpu_mxm.hpp index d310817ba..97408c18c 100644 --- a/src/cpu/cpu_mxm.hpp +++ b/src/cpu/cpu_mxm.hpp @@ -84,6 +84,8 @@ namespace spla { std::vector R_tmp(DN, I); + uint values = 0; + for (uint row_R = 0; row_R < DM; row_R++) { const auto& A_lst = p_lil_A->Ar[row_R]; auto& R_lst = p_lil_R->Ar[row_R]; @@ -109,10 +111,15 @@ namespace spla { for (uint col_R = 0; col_R < DN; col_R++) { if (R_tmp[col_R] != I) { R_lst.emplace_back(col_R, R_tmp[col_R]); + values += 1; } } } + // keep the LIL values counter in sync with the actual row contents, + // otherwise conversions sized from it (e.g. Lil -> Coo) write out of bounds + p_lil_R->values = values; + return Status::Ok; } }; diff --git a/src/cpu/cpu_mxmT_masked.hpp b/src/cpu/cpu_mxmT_masked.hpp index 162bfd631..e57abdfab 100644 --- a/src/cpu/cpu_mxmT_masked.hpp +++ b/src/cpu/cpu_mxmT_masked.hpp @@ -84,6 +84,8 @@ namespace spla { auto DM = R->get_n_rows(); auto I = init->get_value(); + uint values = 0; + for (uint row_R = 0; row_R < DM; row_R++) { const auto& mask_lst = p_lil_mask->Ar[row_R]; const auto& A_lst = p_lil_A->Ar[row_R]; @@ -120,10 +122,15 @@ namespace spla { if (r != I) { R_lst.emplace_back(mask_i, r); + values += 1; } } } + // keep the LIL values counter in sync with the actual row contents, + // otherwise conversions sized from it (e.g. Lil -> Coo) write out of bounds + p_lil_R->values = values; + return Status::Ok; } }; diff --git a/tests/test_mxm.cpp b/tests/test_mxm.cpp index b727975ab..e2bbea776 100644 --- a/tests/test_mxm.cpp +++ b/tests/test_mxm.cpp @@ -129,4 +129,36 @@ TEST(mxm, perf_zero) { } } +TEST(mxm, read_after_exec) { + // Regression test: the kernel must keep the LIL `values` counter in sync + // with the actual row contents. Otherwise the subsequent conversion to COO + // (e.g. via read()) sizes the output from the stale counter and writes + // out of bounds. + spla::uint M = 3, N = 4, K = 2; + + auto R = spla::Matrix::make(M, K, spla::FLOAT); + auto A = spla::Matrix::make(M, N, spla::FLOAT); + auto B = spla::Matrix::make(N, K, spla::FLOAT); + auto init = spla::Scalar::make_float(0.0); + + A->set_float(0, 0, 1.0f); + A->set_float(0, 2, 2.0f); + A->set_float(1, 1, 3.0f); + A->set_float(1, 3, 4.0f); + A->set_float(2, 0, 5.0f); + A->set_float(2, 2, 6.0f); + + B->set_float(0, 0, 7.0f); + B->set_float(0, 1, 1.0f); + B->set_float(1, 1, 1.0f); + B->set_float(2, 1, 1.0f); + B->set_float(3, 1, 1.0f); + + spla::exec_mxm(R, A, B, spla::MULT_FLOAT, spla::PLUS_FLOAT, init); + + spla::ref_ptr vi, vj, vv; + ASSERT_EQ(R->read(vi, vj, vv), spla::Status::Ok); + EXPECT_EQ(vi->get_size() / sizeof(spla::uint), 5u); +} + SPLA_GTEST_MAIN diff --git a/tests/test_mxmT.cpp b/tests/test_mxmT.cpp index 2fa385494..82c682e5a 100644 --- a/tests/test_mxmT.cpp +++ b/tests/test_mxmT.cpp @@ -140,4 +140,64 @@ TEST(mxmT_masked, perf_zero) { } +TEST(mxmT_masked, read_after_exec) { + // Regression test: the kernel must keep the LIL `values` counter in sync + // with the actual row contents. Otherwise the subsequent conversion to COO + // (e.g. via read()) sizes the output from the stale counter and writes + // out of bounds. + spla::uint M = 3, N = 4, K = 2; + + auto R = spla::Matrix::make(M, K, spla::FLOAT); + auto mask = spla::Matrix::make(M, K, spla::FLOAT); + auto A = spla::Matrix::make(M, N, spla::FLOAT); + auto B = spla::Matrix::make(K, N, spla::FLOAT); + auto init = spla::Scalar::make_float(0.0); + + mask->set_float(0, 0, 1.0f); + mask->set_float(0, 1, 1.0f); + mask->set_float(2, 0, 1.0f); + mask->set_float(2, 1, 1.0f); + + A->set_float(0, 0, 1.0f); + A->set_float(0, 2, 2.0f); + A->set_float(1, 1, 3.0f); + A->set_float(1, 3, 4.0f); + A->set_float(2, 0, 5.0f); + A->set_float(2, 2, 6.0f); + + B->set_float(0, 0, 7.0f); + B->set_float(0, 2, 8.0f); + B->set_float(1, 1, 9.0f); + B->set_float(1, 3, 10.0f); + + spla::exec_mxmT_masked(R, mask, A, B, spla::MULT_FLOAT, spla::PLUS_FLOAT, spla::GTZERO_FLOAT, init); + + spla::ref_ptr vi, vj, vv; + ASSERT_EQ(R->read(vi, vj, vv), spla::Status::Ok); + + // some backends keep explicit zeros in the result, so verify the entries + // instead of the exact count: (0,0) = 23, (2,0) = 83, everything else 0 + const auto nnz = vi->get_size() / sizeof(spla::uint); + std::vector i(nnz), j(nnz); + std::vector x(nnz); + vi->read(0, nnz * sizeof(spla::uint), i.data()); + vj->read(0, nnz * sizeof(spla::uint), j.data()); + vv->read(0, nnz * sizeof(float), x.data()); + + bool found_00 = false, found_20 = false; + for (size_t k = 0; k < nnz; k++) { + if (i[k] == 0 && j[k] == 0) { + EXPECT_FLOAT_EQ(x[k], 23.f); + found_00 = true; + } else if (i[k] == 2 && j[k] == 0) { + EXPECT_FLOAT_EQ(x[k], 83.f); + found_20 = true; + } else { + EXPECT_FLOAT_EQ(x[k], 0.f); + } + } + EXPECT_TRUE(found_00); + EXPECT_TRUE(found_20); +} + SPLA_GTEST_MAIN From 45a8d330943921f67255cc46dd98a4424a39583f Mon Sep 17 00:00:00 2001 From: Vladimir Kutuev Date: Sat, 28 Nov 2026 03:21:16 +0300 Subject: [PATCH 2/3] fix(core): preserve vector data when setting fill value TVector::set_fill_value invalidated every storage format, so the next read rebuilt an empty representation and silently discarded all explicit entries. Only dense representations materialize the fill value; sparse ones (Coo, Dok) store explicit entries and are unaffected. Invalidate just the dense formats, converting the data to COO first when no sparse representation is valid. --- src/core/tdecoration.hpp | 1 + src/core/tvector.hpp | 12 +++++++++++- tests/test_vector.cpp | 23 +++++++++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/core/tdecoration.hpp b/src/core/tdecoration.hpp index fecbf7e90..52aba9787 100644 --- a/src/core/tdecoration.hpp +++ b/src/core/tdecoration.hpp @@ -82,6 +82,7 @@ namespace spla { [[nodiscard]] bool is_valid(F format) const { return is_valid_i(static_cast(format)); } void validate(F format) { m_is_valid.set(static_cast(format), true); } void invalidate() { m_is_valid.reset(); } + void invalidate(F format) { m_is_valid.reset(static_cast(format)); } void set_fill_value(T value) { m_fill_value = value; } T get_fill_value() const { return m_fill_value; } diff --git a/src/core/tvector.hpp b/src/core/tvector.hpp index af3e5c71f..ab0e674b3 100644 --- a/src/core/tvector.hpp +++ b/src/core/tvector.hpp @@ -130,7 +130,17 @@ namespace spla { template Status TVector::set_fill_value(const ref_ptr& value) { if (value) { - m_storage.invalidate(); + // Sparse representations (Coo, Dok) store explicit entries only and are + // unaffected by the fill value. Dense representations materialize it, so + // they must be rebuilt. If no sparse representation is valid, convert the + // data to COO first (using the old fill value) so it is not lost. + if (!m_storage.is_valid(FormatVector::CpuCoo) && !m_storage.is_valid(FormatVector::CpuDok)) { + validate_rw(FormatVector::CpuCoo); + } + m_storage.invalidate(FormatVector::CpuDense); +#if defined(SPLA_BUILD_OPENCL) + m_storage.invalidate(FormatVector::AccDense); +#endif if constexpr (std::is_same::value) m_storage.set_fill_value(value->as_int()); if constexpr (std::is_same::value) m_storage.set_fill_value(value->as_uint()); diff --git a/tests/test_vector.cpp b/tests/test_vector.cpp index 4bd310b43..49bbf7920 100644 --- a/tests/test_vector.cpp +++ b/tests/test_vector.cpp @@ -497,4 +497,27 @@ TEST(vector, map) { } } +TEST(vector, set_fill_value_preserves_data) { + // Regression test: setting the fill value must not discard explicit entries. + // Only dense representations materialize the fill value, so they are + // invalidated; sparse representations keep their data. + auto ivec = spla::Vector::make(4, spla::INT); + ivec->set_int(0, 10); + + ivec->set_fill_value(spla::Scalar::make_int(-1)); + + int x; + ivec->get_int(0, x); + EXPECT_EQ(x, 10); + ivec->get_int(1, x); + EXPECT_EQ(x, -1); + + // data must survive a round trip through the dense representation + ivec->set_format(spla::FormatVector::CpuDense); + ivec->get_int(0, x); + EXPECT_EQ(x, 10); + ivec->get_int(1, x); + EXPECT_EQ(x, -1); +} + SPLA_GTEST_MAIN From 508926ff6464f25b446877fcc6653dfa2ef5b669 Mon Sep 17 00:00:00 2001 From: Vladimir Kutuev Date: Sat, 28 Nov 2026 17:33:11 +0300 Subject: [PATCH 3/3] fix(core): preserve matrix data when setting fill value TMatrix::set_fill_value invalidated every storage format, so the next read rebuilt a representation from scratch and silently lost all explicit entries. All matrix formats are sparse and never materialize the fill value in storage (it is applied on read), so no representation needs to be invalidated. Regression test: matrix.set_fill_value_preserves_data verifies entries survive the call and round trips through Lil/Dok/Coo/Csr. --- src/core/tmatrix.hpp | 5 +++-- tests/test_matrix.cpp | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/core/tmatrix.hpp b/src/core/tmatrix.hpp index cd36e72b5..52c2f6b4e 100644 --- a/src/core/tmatrix.hpp +++ b/src/core/tmatrix.hpp @@ -130,8 +130,9 @@ namespace spla { template Status TMatrix::set_fill_value(const ref_ptr& value) { if (value) { - m_storage.invalidate(); - + // All matrix formats are sparse and never materialize the fill + // value in storage (it is applied on read), so no representation + // needs to be invalidated and no data can be lost. if constexpr (std::is_same::value) m_storage.set_fill_value(value->as_int()); if constexpr (std::is_same::value) m_storage.set_fill_value(value->as_uint()); if constexpr (std::is_same::value) m_storage.set_fill_value(value->as_float()); diff --git a/tests/test_matrix.cpp b/tests/test_matrix.cpp index 14ecc4e17..1fcb0f016 100644 --- a/tests/test_matrix.cpp +++ b/tests/test_matrix.cpp @@ -271,4 +271,38 @@ TEST(matrix, emult) { } } +TEST(matrix, set_fill_value_preserves_data) { + // Regression test: setting the fill value must not discard explicit + // entries. All matrix formats are sparse and never materialize the fill + // value in storage (it is applied on read), so the old implementation, + // which invalidated every format, silently lost all data on the next read. + const spla::uint M = 4, N = 4; + + auto imat = spla::Matrix::make(M, N, spla::INT); + imat->set_int(0, 1, 10); + imat->set_int(2, 2, 20); + + imat->set_fill_value(spla::Scalar::make_int(-1)); + + int v; + imat->get_int(0, 1, v); + EXPECT_EQ(10, v); + imat->get_int(2, 2, v); + EXPECT_EQ(20, v); + imat->get_int(0, 0, v); + EXPECT_EQ(-1, v);// missing entries report the new fill value + + // data must survive round trips through the other formats + const spla::FormatMatrix formats[] = {spla::FormatMatrix::CpuLil, spla::FormatMatrix::CpuDok, spla::FormatMatrix::CpuCoo, spla::FormatMatrix::CpuCsr}; + for (auto format : formats) { + imat->set_format(format); + imat->get_int(0, 1, v); + EXPECT_EQ(10, v); + imat->get_int(2, 2, v); + EXPECT_EQ(20, v); + imat->get_int(0, 0, v); + EXPECT_EQ(-1, v); + } +} + SPLA_GTEST_MAIN_WITH_FINALIZE