Skip to content

[Bug] Use correct num_triplets - #8502

Open
junwha wants to merge 1 commit into
taichi-dev:masterfrom
junwha:fix-bof-sparse-matrix
Open

junwha wants to merge 1 commit into
taichi-dev:masterfrom
junwha:fix-bof-sparse-matrix

Conversation

@junwha

@junwha junwha commented Mar 31, 2024 •

Copy link
Copy Markdown

Fixes #8501.

build_from_ndarray divided the buffer size in bytes by three, which could make it read past the end of the array. Count one triplet for every three scalars and check that the array and matrix have the same scalar dtype before reading.

I ran tests/python/test_sparse_matrix.py on CPU. All 57 CPU tests passed, including the new regression cases. The two CUDA tests were skipped.


Note

Low Risk
Localized fix to sparse-matrix construction from ndarrays with added validation and expanded tests; no auth or infrastructure changes.

Overview
Fixes incorrect num_triplets calculation in make_sparse_matrix_from_ndarray, which previously used ndarray.get_nelement() * ndarray.get_element_size() / 3 and broke when each ndarray “element” is a vector or matrix (multiple scalars per entry). The code now derives scalars per element from the scalar dtype size and computes num_triplets as nelement * scalars_per_element / 3.

Adds a dtype guard so building fails with a clear error when the sparse matrix scalar type and the ndarray’s scalar type differ. The triplet-build loop index type is updated to size_t to match num_triplets.

Tests for build_from_ndarray now cover scalar, vector, and matrix element layouts via from_numpy, assert concrete off-diagonal values, fix the test name typo, and add a dtype mismatch case.

Reviewed by Cursor Bugbot for commit d79c5f0. Bugbot is set up for automated code reviews on this repo. Configure here.

@CLAassistant

CLAassistant commented Mar 31, 2024 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@netlify

netlify Bot commented Mar 31, 2024 •

Copy link
Copy Markdown

✅ Deploy Preview for docsite-preview canceled.

Name Link
🔨 Latest commit c1fb92a
🔍 Latest deploy log https://app.netlify.com/sites/docsite-preview/deploys/6609b8e152af6f0008feca27

@junwha junwha changed the title [Bugfix] Use correct num_triplets [Bug] Use correct num_triplets Mar 31, 2024
…rray

Use the scalar width to count triplets and check the input dtype before
reading the buffer. Cover scalar, vector and matrix ndarrays on CPU.

Fixes taichi-dev#8501.
@junwha
junwha force-pushed the fix-bof-sparse-matrix branch from c1fb92a to d79c5f0 Compare November 29, 2026 17:05

This branch has not been deployed

No deployments
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.

Incorrect calculation of num_triplets at SparseMatrix construction

2 participants