trnsparse 0.12.0__tar.gz → 0.12.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (82) hide show
  1. {trnsparse-0.12.0 → trnsparse-0.12.1}/CHANGELOG.md +17 -0
  2. {trnsparse-0.12.0 → trnsparse-0.12.1}/PKG-INFO +1 -1
  3. {trnsparse-0.12.0 → trnsparse-0.12.1}/pyproject.toml +1 -1
  4. {trnsparse-0.12.0 → trnsparse-0.12.1}/tests/test_ops.py +52 -0
  5. {trnsparse-0.12.0 → trnsparse-0.12.1}/trnsparse/__init__.py +1 -1
  6. {trnsparse-0.12.0 → trnsparse-0.12.1}/trnsparse/ops.py +60 -13
  7. {trnsparse-0.12.0 → trnsparse-0.12.1}/trnsparse.egg-info/PKG-INFO +1 -1
  8. {trnsparse-0.12.0 → trnsparse-0.12.1}/.github/workflows/ci.yml +0 -0
  9. {trnsparse-0.12.0 → trnsparse-0.12.1}/.github/workflows/notify-umbrella.yml +0 -0
  10. {trnsparse-0.12.0 → trnsparse-0.12.1}/.github/workflows/publish.yml +0 -0
  11. {trnsparse-0.12.0 → trnsparse-0.12.1}/.gitignore +0 -0
  12. {trnsparse-0.12.0 → trnsparse-0.12.1}/.pre-commit-config.yaml +0 -0
  13. {trnsparse-0.12.0 → trnsparse-0.12.1}/CLAUDE.md +0 -0
  14. {trnsparse-0.12.0 → trnsparse-0.12.1}/CODE_OF_CONDUCT.md +0 -0
  15. {trnsparse-0.12.0 → trnsparse-0.12.1}/CONTRIBUTING.md +0 -0
  16. {trnsparse-0.12.0 → trnsparse-0.12.1}/LICENSE +0 -0
  17. {trnsparse-0.12.0 → trnsparse-0.12.1}/README.md +0 -0
  18. {trnsparse-0.12.0 → trnsparse-0.12.1}/benchmarks/bench_bsr_spmm.py +0 -0
  19. {trnsparse-0.12.0 → trnsparse-0.12.1}/benchmarks/bench_cg_fused_trn2.py +0 -0
  20. {trnsparse-0.12.0 → trnsparse-0.12.1}/benchmarks/bench_iterative.py +0 -0
  21. {trnsparse-0.12.0 → trnsparse-0.12.1}/benchmarks/bench_multicore_trn2.py +0 -0
  22. {trnsparse-0.12.0 → trnsparse-0.12.1}/benchmarks/bench_multiproc_trn2.py +0 -0
  23. {trnsparse-0.12.0 → trnsparse-0.12.1}/benchmarks/bench_native_trn2.py +0 -0
  24. {trnsparse-0.12.0 → trnsparse-0.12.1}/benchmarks/bench_power_fused_trn2.py +0 -0
  25. {trnsparse-0.12.0 → trnsparse-0.12.1}/benchmarks/bench_screening.py +0 -0
  26. {trnsparse-0.12.0 → trnsparse-0.12.1}/benchmarks/bench_spmm.py +0 -0
  27. {trnsparse-0.12.0 → trnsparse-0.12.1}/benchmarks/bench_spmv.py +0 -0
  28. {trnsparse-0.12.0 → trnsparse-0.12.1}/benchmarks/conftest.py +0 -0
  29. {trnsparse-0.12.0 → trnsparse-0.12.1}/docs/api.md +0 -0
  30. {trnsparse-0.12.0 → trnsparse-0.12.1}/docs/architecture.md +0 -0
  31. {trnsparse-0.12.0 → trnsparse-0.12.1}/docs/aws_setup.md +0 -0
  32. {trnsparse-0.12.0 → trnsparse-0.12.1}/docs/benchmarks.md +0 -0
  33. {trnsparse-0.12.0 → trnsparse-0.12.1}/docs/index.md +0 -0
  34. {trnsparse-0.12.0 → trnsparse-0.12.1}/docs/installation.md +0 -0
  35. {trnsparse-0.12.0 → trnsparse-0.12.1}/docs/iterative_solvers.md +0 -0
  36. {trnsparse-0.12.0 → trnsparse-0.12.1}/docs/migration_scipy.md +0 -0
  37. {trnsparse-0.12.0 → trnsparse-0.12.1}/docs/quickstart.md +0 -0
  38. {trnsparse-0.12.0 → trnsparse-0.12.1}/docs/sparse_attention.md +0 -0
  39. {trnsparse-0.12.0 → trnsparse-0.12.1}/examples/block_sparse_attention.py +0 -0
  40. {trnsparse-0.12.0 → trnsparse-0.12.1}/examples/sparse_fock.py +0 -0
  41. {trnsparse-0.12.0 → trnsparse-0.12.1}/infra/terraform/.terraform.lock.hcl +0 -0
  42. {trnsparse-0.12.0 → trnsparse-0.12.1}/infra/terraform/README.md +0 -0
  43. {trnsparse-0.12.0 → trnsparse-0.12.1}/infra/terraform/main.tf +0 -0
  44. {trnsparse-0.12.0 → trnsparse-0.12.1}/mkdocs.yml +0 -0
  45. {trnsparse-0.12.0 → trnsparse-0.12.1}/scripts/bench_to_md.py +0 -0
  46. {trnsparse-0.12.0 → trnsparse-0.12.1}/scripts/run_benchmarks.sh +0 -0
  47. {trnsparse-0.12.0 → trnsparse-0.12.1}/scripts/run_neuron_tests.sh +0 -0
  48. {trnsparse-0.12.0 → trnsparse-0.12.1}/scripts/run_simulator_tests.sh +0 -0
  49. {trnsparse-0.12.0 → trnsparse-0.12.1}/setup.cfg +0 -0
  50. {trnsparse-0.12.0 → trnsparse-0.12.1}/tests/conftest.py +0 -0
  51. {trnsparse-0.12.0 → trnsparse-0.12.1}/tests/test_attention.py +0 -0
  52. {trnsparse-0.12.0 → trnsparse-0.12.1}/tests/test_attn_gather.py +0 -0
  53. {trnsparse-0.12.0 → trnsparse-0.12.1}/tests/test_bsr.py +0 -0
  54. {trnsparse-0.12.0 → trnsparse-0.12.1}/tests/test_core_split.py +0 -0
  55. {trnsparse-0.12.0 → trnsparse-0.12.1}/tests/test_examples.py +0 -0
  56. {trnsparse-0.12.0 → trnsparse-0.12.1}/tests/test_formats.py +0 -0
  57. {trnsparse-0.12.0 → trnsparse-0.12.1}/tests/test_iterative.py +0 -0
  58. {trnsparse-0.12.0 → trnsparse-0.12.1}/tests/test_multicore.py +0 -0
  59. {trnsparse-0.12.0 → trnsparse-0.12.1}/tests/test_native_dispatch.py +0 -0
  60. {trnsparse-0.12.0 → trnsparse-0.12.1}/tests/test_nki_attn.py +0 -0
  61. {trnsparse-0.12.0 → trnsparse-0.12.1}/tests/test_nki_bsr.py +0 -0
  62. {trnsparse-0.12.0 → trnsparse-0.12.1}/tests/test_nki_cg_fused.py +0 -0
  63. {trnsparse-0.12.0 → trnsparse-0.12.1}/tests/test_nki_compile.py +0 -0
  64. {trnsparse-0.12.0 → trnsparse-0.12.1}/tests/test_nki_native.py +0 -0
  65. {trnsparse-0.12.0 → trnsparse-0.12.1}/tests/test_nki_screened_spmm.py +0 -0
  66. {trnsparse-0.12.0 → trnsparse-0.12.1}/tests/test_nki_sim.py +0 -0
  67. {trnsparse-0.12.0 → trnsparse-0.12.1}/tests/test_nki_spmm.py +0 -0
  68. {trnsparse-0.12.0 → trnsparse-0.12.1}/tests/test_nki_tail_audit.py +0 -0
  69. {trnsparse-0.12.0 → trnsparse-0.12.1}/tests/test_screening.py +0 -0
  70. {trnsparse-0.12.0 → trnsparse-0.12.1}/trnsparse/formats.py +0 -0
  71. {trnsparse-0.12.0 → trnsparse-0.12.1}/trnsparse/iterative.py +0 -0
  72. {trnsparse-0.12.0 → trnsparse-0.12.1}/trnsparse/multicore.py +0 -0
  73. {trnsparse-0.12.0 → trnsparse-0.12.1}/trnsparse/nki/__init__.py +0 -0
  74. {trnsparse-0.12.0 → trnsparse-0.12.1}/trnsparse/nki/_native.py +0 -0
  75. {trnsparse-0.12.0 → trnsparse-0.12.1}/trnsparse/nki/_native_attn.py +0 -0
  76. {trnsparse-0.12.0 → trnsparse-0.12.1}/trnsparse/nki/dispatch.py +0 -0
  77. {trnsparse-0.12.0 → trnsparse-0.12.1}/trnsparse/nki/kernels.py +0 -0
  78. {trnsparse-0.12.0 → trnsparse-0.12.1}/trnsparse/screening.py +0 -0
  79. {trnsparse-0.12.0 → trnsparse-0.12.1}/trnsparse.egg-info/SOURCES.txt +0 -0
  80. {trnsparse-0.12.0 → trnsparse-0.12.1}/trnsparse.egg-info/dependency_links.txt +0 -0
  81. {trnsparse-0.12.0 → trnsparse-0.12.1}/trnsparse.egg-info/requires.txt +0 -0
  82. {trnsparse-0.12.0 → trnsparse-0.12.1}/trnsparse.egg-info/top_level.txt +0 -0
@@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [0.12.1] — 2026-11-01
11
+
12
+ Validated on Trn2 (trn2.3xlarge, Neuron SDK 2.33.0, native TorchNeuron) — see trnsci/trnsci#35.
13
+
14
+ Headline: **correctness fix:** in every release through 0.12.0, `spmv_symmetric` ignored `uplo`, so a fully stored symmetric matrix gave `A@x + strict(A)ᵀ@x` (rel. err 0.85–1.9). Now only the `uplo` triangle is referenced, as in BLAS `symv`. A matrix storing only the opposite triangle still gives the old (correct) result with a `DeprecationWarning` (#53, #52).
15
+
10
16
  ## [0.12.0] — 2026-11-01
11
17
 
12
18
  Validated on Trn2 (trn2.3xlarge, Neuron SDK 2.33.0, native TorchNeuron) — see trnsci/trnsci#35.
@@ -15,6 +21,17 @@ Headline: **correctness fix — upgrade recommended if you pass `scale` to block
15
21
 
16
22
  ### Fixed
17
23
 
24
+ - **`spmv_symmetric` ignored `uplo`** (silent). Affects **every release through
25
+ 0.11.0**, on every backend. Every stored entry was used, so a fully stored
26
+ symmetric matrix (or any entry outside the intended triangle) gave
27
+ `A @ x + strict(A)ᵀ @ x` with no error (rel. err 0.85–1.9 on a 20×20 test).
28
+ When both triangles hold entries, only the `uplo` triangle is now
29
+ referenced, as in BLAS `symv`, on the host and device paths. Other `uplo`
30
+ values raise. Callers that store one triangle get the same results as
31
+ before, including those whose `uplo` names the *other* triangle (the old
32
+ code ignored it): that triangle is still used, now with a
33
+ `DeprecationWarning` asking for the matching `uplo` (#52).
34
+
18
35
  - **`block_sparse_attention_tiled` silently ignored a custom `scale` on the
19
36
  NKI paths** (released 0.6.0 through 0.11.0; #47). With `backend="nki"` or
20
37
  `"auto"` on Neuron, the forward and all three gradients always used
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: trnsparse
3
- Version: 0.12.0
3
+ Version: 0.12.1
4
4
  Summary: Sparse matrix operations for AWS Trainium via NKI
5
5
  Author-email: Scott Friedman <scttfrdmn@gmail.com>
6
6
  License-Expression: Apache-2.0
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "trnsparse"
7
- version = "0.12.0"
7
+ version = "0.12.1"
8
8
  description = "Sparse matrix operations for AWS Trainium via NKI"
9
9
  readme = "README.md"
10
10
  license = "Apache-2.0"
@@ -78,6 +78,58 @@ class TestSymmetricSpMV:
78
78
  expected = A @ x
79
79
  np.testing.assert_allclose(result.numpy(), expected.numpy(), atol=1e-6)
80
80
 
81
+ @staticmethod
82
+ def _case(store, seed=3):
83
+ g = torch.Generator().manual_seed(seed)
84
+ S = torch.randn(9, 9, generator=g, dtype=torch.float64)
85
+ S[torch.rand(9, 9, generator=g) > 0.5] = 0
86
+ S.fill_diagonal_(1.5)
87
+ stored = {"upper": torch.triu(S), "lower": torch.tril(S), "both": S}[store]
88
+ x = torch.randn(9, generator=g, dtype=torch.float64)
89
+ return S, stored, x
90
+
91
+ @staticmethod
92
+ def _sym(T):
93
+ return T + T.T - torch.diag(torch.diagonal(T))
94
+
95
+ @pytest.mark.parametrize("dense_branch", [False, True])
96
+ @pytest.mark.parametrize("uplo", ["upper", "lower"])
97
+ @pytest.mark.parametrize("store", ["upper", "lower", "both"])
98
+ def test_uplo_and_storage(self, monkeypatch, store, uplo, dense_branch):
99
+ """Both triangles stored: only `uplo` is referenced (#52). One triangle
100
+ stored: that triangle is used whatever `uplo` says, as through 0.11.0,
101
+ with a DeprecationWarning when `uplo` names the empty triangle."""
102
+ import warnings
103
+
104
+ from trnsparse import ops
105
+
106
+ if dense_branch: # the device-operand branch, on the host
107
+ monkeypatch.setattr(ops, "_torch_sparse_ok", lambda A, x: False)
108
+ S, stored, x = self._case(store)
109
+ tri = uplo if store == "both" else store
110
+ want = 0.7 * self._sym(torch.triu(S) if tri == "upper" else torch.tril(S)) @ x
111
+ csr = trnsparse.from_dense(stored)
112
+ with warnings.catch_warnings(record=True) as w:
113
+ warnings.simplefilter("always")
114
+ got = trnsparse.spmv_symmetric(csr, x, alpha=0.7, uplo=uplo)
115
+ torch.testing.assert_close(got, want)
116
+ dep = [m for m in w if issubclass(m.category, DeprecationWarning)]
117
+ assert bool(dep) == (store != "both" and store != uplo)
118
+
119
+ def test_diagonal_only_no_warning(self):
120
+ import warnings
121
+
122
+ csr = trnsparse.from_dense(torch.diag(torch.arange(1.0, 6.0)))
123
+ with warnings.catch_warnings():
124
+ warnings.simplefilter("error")
125
+ got = trnsparse.spmv_symmetric(csr, torch.ones(5), uplo="lower")
126
+ torch.testing.assert_close(got, torch.arange(1.0, 6.0))
127
+
128
+ def test_bad_uplo(self):
129
+ csr = trnsparse.from_dense(torch.eye(3))
130
+ with pytest.raises(ValueError):
131
+ trnsparse.spmv_symmetric(csr, torch.ones(3), uplo="U")
132
+
81
133
 
82
134
  class TestSparseAdd:
83
135
  def test_basic(self):
@@ -5,7 +5,7 @@ CSR/COO formats, SpMV, SpMM, and integral screening for
5
5
  sparse scientific computing. Part of the trnsci scientific computing suite.
6
6
  """
7
7
 
8
- __version__ = "0.12.0"
8
+ __version__ = "0.12.1"
9
9
 
10
10
  from .formats import BSRMatrix, COOMatrix, CSRMatrix, eye_sparse, from_dense, from_scipy
11
11
  from .iterative import (
@@ -23,6 +23,7 @@ sparse tensor) and only the dense data moves.
23
23
  from __future__ import annotations
24
24
 
25
25
  import math
26
+ import warnings
26
27
 
27
28
  import torch
28
29
 
@@ -121,40 +122,86 @@ def spmm(
121
122
  def spmv_symmetric(
122
123
  A: CSRMatrix, x: torch.Tensor, alpha: float = 1.0, uplo: str = "upper"
123
124
  ) -> torch.Tensor:
124
- """Symmetric sparse matrix × vector using only stored triangle.
125
+ """Symmetric sparse matrix × vector using only the `uplo` triangle.
125
126
 
126
127
  For symmetric matrices (like the overlap matrix S or density P),
127
- only half the non-zeros need to be stored. Computes
128
- `A @ x + (A_strict_triangle)ᵀ @ x` as two vectorized SpMVs.
128
+ only half the non-zeros need to be stored. Entries of `A` outside the
129
+ `uplo` triangle (``"upper"``: col >= row, ``"lower"``: col <= row) are
130
+ not referenced, as in BLAS ``symv``, so `A` may store one triangle or
131
+ the full matrix. Computes `T @ x + (T_strict)ᵀ @ x` for the triangle T
132
+ as two vectorized SpMVs.
133
+
134
+ Compatibility: through 0.11.0 `uplo` was ignored, so a matrix storing
135
+ only the *other* triangle gave the right answer. That case still does:
136
+ when every stored nonzero off-diagonal entry is in the triangle opposite
137
+ `uplo`, that triangle is used and a `DeprecationWarning` asks for the
138
+ matching `uplo` (#52).
129
139
  """
130
140
  m, n = A.shape
131
141
  assert m == n, "Matrix must be square for symmetric SpMV"
142
+ if uplo not in ("upper", "lower"):
143
+ raise ValueError(f"uplo must be 'upper' or 'lower'; got {uplo!r}")
144
+ uplo = _resolve_uplo(A, uplo)
132
145
 
133
146
  if not _torch_sparse_ok(A, x):
134
- return _spmv_symmetric_dense(A, x, alpha)
147
+ return _spmv_symmetric_dense(A, x, alpha, uplo)
135
148
 
136
149
  rows = torch.repeat_interleave(torch.arange(m), A.row_ptrs[1:] - A.row_ptrs[:-1])
137
150
  cols = A.col_indices
138
- strict_mask = rows != cols
139
- A_sparse = _as_torch_csr(A)
140
-
151
+ # The stored triangle only: entries in the other one were silently used
152
+ # through 0.11.0, so a fully stored A gave A @ x + strict(A)ᵀ @ x (#52).
153
+ keep = cols >= rows if uplo == "upper" else cols <= rows
154
+ r, c, v = rows[keep], cols[keep], A.values[keep]
155
+ tri = torch.sparse_coo_tensor(torch.stack([r, c]), v, size=A.shape).coalesce()
156
+ strict_mask = r != c
141
157
  strict = torch.sparse_coo_tensor(
142
- torch.stack([rows[strict_mask], cols[strict_mask]]),
143
- A.values[strict_mask],
158
+ torch.stack([r[strict_mask], c[strict_mask]]),
159
+ v[strict_mask],
144
160
  size=A.shape,
145
161
  ).coalesce()
146
162
 
147
- return alpha * (A_sparse @ x + strict.t() @ x)
163
+ return alpha * (tri @ x + strict.t() @ x)
164
+
148
165
 
166
+ def _resolve_uplo(A: CSRMatrix, uplo: str) -> str:
167
+ """`uplo`, or the opposite triangle when only that one holds entries.
149
168
 
150
- def _spmv_symmetric_dense(A: CSRMatrix, x: torch.Tensor, alpha: float) -> torch.Tensor:
169
+ Keeps one-triangle callers that relied on `uplo` being ignored (through
170
+ 0.11.0) correct: storing the lower triangle with the default
171
+ ``uplo="upper"`` would otherwise reference only the diagonal.
172
+ """
173
+ row_ptrs = A.row_ptrs.cpu()
174
+ cols = A.col_indices.cpu()
175
+ nz = A.values.detach().cpu() != 0
176
+ rows = torch.repeat_interleave(torch.arange(A.shape[0]), row_ptrs[1:] - row_ptrs[:-1])
177
+ has_upper = bool(((cols > rows) & nz).any())
178
+ has_lower = bool(((cols < rows) & nz).any())
179
+ other = "lower" if uplo == "upper" else "upper"
180
+ in_uplo, in_other = (has_upper, has_lower) if uplo == "upper" else (has_lower, has_upper)
181
+ if in_other and not in_uplo:
182
+ warnings.warn(
183
+ f"spmv_symmetric: A stores its off-diagonal entries only in the {other} "
184
+ f"triangle but uplo={uplo!r}; using the {other} triangle. Pass "
185
+ f"uplo={other!r}. Since 0.12.0 only the uplo triangle is referenced "
186
+ "when both are stored, and this fallback will be removed (#52).",
187
+ DeprecationWarning,
188
+ stacklevel=3,
189
+ )
190
+ return other
191
+ return uplo
192
+
193
+
194
+ def _spmv_symmetric_dense(
195
+ A: CSRMatrix, x: torch.Tensor, alpha: float, uplo: str = "upper"
196
+ ) -> torch.Tensor:
151
197
  """`spmv_symmetric` for a host CSR and an operand on another device.
152
198
 
153
199
  The device has no sparse tensors: build the symmetric dense view
154
- (stored triangle plus the transpose of its strict part) on the host and
155
- move it once.
200
+ (the `uplo` triangle plus the transpose of its strict part) on the host
201
+ and move it once.
156
202
  """
157
203
  D = _csr_to_dense_host(A)
204
+ D = torch.triu(D) if uplo == "upper" else torch.tril(D)
158
205
  strict = D - torch.diag(torch.diagonal(D))
159
206
  return alpha * ((D + strict.T).to(x.device) @ x)
160
207
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: trnsparse
3
- Version: 0.12.0
3
+ Version: 0.12.1
4
4
  Summary: Sparse matrix operations for AWS Trainium via NKI
5
5
  Author-email: Scott Friedman <scttfrdmn@gmail.com>
6
6
  License-Expression: Apache-2.0
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes