Replace sgemm() panics with Result-based error handling#997
Conversation
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #997 +/- ##
==========================================
+ Coverage 89.48% 89.58% +0.09%
==========================================
Files 448 459 +11
Lines 84095 85196 +1101
==========================================
+ Hits 75250 76320 +1070
- Misses 8845 8876 +31
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull request overview
This PR refactors diskann_linalg::sgemm() to return Result<(), SgemmError> instead of panicking, adds overflow-safe dimension validation via checked_mul(), and updates downstream call sites/tests to handle the new error path.
Changes:
- Introduces
SgemmErrorand convertssgemm()’s dimension checks fromassert_eq!toResult-based validation with overflow protection. - Updates callers (notably
diskann-diskanddiskann-quantization’s linalg-backed transform) to handlesgemm()errors. - Adds unit tests in
diskann-linalgcovering invalid dimensions and overflow cases.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| diskann-linalg/src/lib.rs | Adds SgemmError, changes sgemm() to return Result, adds overflow checks and new unit tests. |
| diskann-disk/src/utils/math_util.rs | Converts 3 sgemm() call sites to propagate errors as ANNError. |
| diskann-quantization/src/algorithms/transforms/utils.rs | Adds TransformFailed::SgemmError (cfg’d on linalg) and removes unconditional Copy derive. |
| diskann-quantization/src/algorithms/transforms/random_rotation.rs | Propagates sgemm() errors via ? from transform_into(). |
| diskann-quantization/src/spherical/quantizer.rs | Adds match arms for TransformFailed::SgemmError, but currently handles it with panic!. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
hildebrandmw
left a comment
There was a problem hiding this comment.
Thanks Alex - just a few general error handling comments from my end.
Summary
Refactors
diskann_linalg::sgemm()to returnResult<(), SgemmError>instead of panicking on invalid inputs. This change improves error handling and adds overflow protection for matrix dimension calculations.Principle: DiskANN must not panic in production for recoverable errors; panics are reserved for catastrophic failures indicating undefined behavior.
Changes
1. New Error Type (
SgemmError)SgemmErrorenum with two variants:InvalidMatrixDimensions: Matrix has incorrect dimensionsDimensionOverflow: Multiplication would overflowusizeDisplayandstd::error::Errortraits for proper error reporting2. Updated
sgemm()Function SignatureBefore:
After:
3. Overflow Protection
Replaced unsafe multiplication with
checked_mul()for all dimension calculations:m * kvalidation (matrix a)k * nvalidation (matrix b)m * nvalidation (matrix c)This prevents integer overflow vulnerabilities that could lead to undefined behavior.
4. Updated Call Sites
Updated all callers of
sgemm()to handle theResult:TransformFailed::SgemmErrorvariantANNErrorwith descriptive messages5. Comprehensive Test Coverage
Added 6 new unit tests:
test_sgemm_invalid_matrix_a_dimensions: Validates dimension check for matrix atest_sgemm_invalid_matrix_b_dimensions: Validates dimension check for matrix btest_sgemm_invalid_matrix_c_dimensions: Validates dimension check for matrix ctest_sgemm_m_times_k_overflow: Tests overflow detection form * ktest_sgemm_k_times_n_overflow: Tests overflow detection fork * ntest_sgemm_m_times_n_overflow: Tests overflow detection form * nReview Order
📁 diskann-linalg/src/lib.rs
SgemmErrortypechecked_mul()📁 diskann-linalg/src/lib.rs (lines 318-461)
📁 diskann-disk/src/utils/math_util.rs (lines 164-204)