Skip to content

feat(hslm): sparse ternary matmul — skip zero weights#561

Merged
gHashTag merged 1 commit into
mainfrom
feat/316-sparse-ternary-matmul
Apr 30, 2026
Merged

feat(hslm): sparse ternary matmul — skip zero weights#561
gHashTag merged 1 commit into
mainfrom
feat/316-sparse-ternary-matmul

Conversation

@gHashTag
Copy link
Copy Markdown
Owner

Summary

Sparse ternary matrix multiplication that only iterates non-zero entries.

New file

  • src/b2t/sparse_ternary.zig — 203 LOC

Performance

  • Dense matmul: O(rows × cols) — iterates all entries including zeros
  • Sparse matmul: O(nnz) — only non-zero entries stored and processed
  • For typical ternary sparsity (~33% zeros): ~3x fewer operations

Features

  • SparseTernaryMatrix: CSR-like sparse storage, init from dense weight array
  • matmul(): O(nnz) sparse matrix-vector multiply
  • matmulBatch(): Batched version for sequence processing
  • SparseStats: nnz count, sparsity %, compression ratio
  • computeStats(): Statistical summary of sparsity

Tests (5)

  • Sparse output matches dense reference
  • All-zero weights → nnz=0, sparsity=100%
  • All-nonzero weights → nnz=total, sparsity=0%
  • Sparse stats accuracy
  • Batch matmul correctness

Closes #316

- Add src/b2t/sparse_ternary.zig
- CSR-like sparse storage: only non-zero entries stored
- SparseTernaryMatrix: init from dense, sparsity tracking
- matmul: iterate nnz entries only (O(nnz) vs O(rows*cols))
- matmulBatch: batched version for sequences
- SparseStats: nnz, sparsity%, compression ratio
- 5 tests: matches dense, skips zeros, all-nonzero,
  stats, batch

Closes #316
@gHashTag gHashTag merged commit af32694 into main Apr 30, 2026
9 of 19 checks passed
@gHashTag gHashTag deleted the feat/316-sparse-ternary-matmul branch April 30, 2026 00:44
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.

feat(hslm): sparse ternary matmul — skip zero weights

1 participant