trnsparse 0.16.0__tar.gz → 0.16.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 (95) hide show
  1. {trnsparse-0.16.0 → trnsparse-0.16.1}/CHANGELOG.md +18 -0
  2. {trnsparse-0.16.0/trnsparse.egg-info → trnsparse-0.16.1}/PKG-INFO +1 -1
  3. {trnsparse-0.16.0 → trnsparse-0.16.1}/pyproject.toml +1 -1
  4. {trnsparse-0.16.0 → trnsparse-0.16.1}/tests/test_formats.py +94 -0
  5. {trnsparse-0.16.0 → trnsparse-0.16.1}/trnsparse/__init__.py +1 -1
  6. {trnsparse-0.16.0 → trnsparse-0.16.1}/trnsparse/formats.py +54 -4
  7. {trnsparse-0.16.0 → trnsparse-0.16.1/trnsparse.egg-info}/PKG-INFO +1 -1
  8. {trnsparse-0.16.0 → trnsparse-0.16.1}/.github/workflows/ci.yml +0 -0
  9. {trnsparse-0.16.0 → trnsparse-0.16.1}/.github/workflows/notify-umbrella.yml +0 -0
  10. {trnsparse-0.16.0 → trnsparse-0.16.1}/.github/workflows/publish.yml +0 -0
  11. {trnsparse-0.16.0 → trnsparse-0.16.1}/.gitignore +0 -0
  12. {trnsparse-0.16.0 → trnsparse-0.16.1}/.pre-commit-config.yaml +0 -0
  13. {trnsparse-0.16.0 → trnsparse-0.16.1}/CLAUDE.md +0 -0
  14. {trnsparse-0.16.0 → trnsparse-0.16.1}/CODE_OF_CONDUCT.md +0 -0
  15. {trnsparse-0.16.0 → trnsparse-0.16.1}/CONTRIBUTING.md +0 -0
  16. {trnsparse-0.16.0 → trnsparse-0.16.1}/LICENSE +0 -0
  17. {trnsparse-0.16.0 → trnsparse-0.16.1}/README.md +0 -0
  18. {trnsparse-0.16.0 → trnsparse-0.16.1}/benchmarks/bench_bsr_spmm.py +0 -0
  19. {trnsparse-0.16.0 → trnsparse-0.16.1}/benchmarks/bench_cg_fused_trn2.py +0 -0
  20. {trnsparse-0.16.0 → trnsparse-0.16.1}/benchmarks/bench_iterative.py +0 -0
  21. {trnsparse-0.16.0 → trnsparse-0.16.1}/benchmarks/bench_kernel_matmul_trn2.py +0 -0
  22. {trnsparse-0.16.0 → trnsparse-0.16.1}/benchmarks/bench_multicore_trn2.py +0 -0
  23. {trnsparse-0.16.0 → trnsparse-0.16.1}/benchmarks/bench_multiproc_trn2.py +0 -0
  24. {trnsparse-0.16.0 → trnsparse-0.16.1}/benchmarks/bench_native_trn2.py +0 -0
  25. {trnsparse-0.16.0 → trnsparse-0.16.1}/benchmarks/bench_power_fused_trn2.py +0 -0
  26. {trnsparse-0.16.0 → trnsparse-0.16.1}/benchmarks/bench_screened_tiles_trn2.py +0 -0
  27. {trnsparse-0.16.0 → trnsparse-0.16.1}/benchmarks/bench_screening.py +0 -0
  28. {trnsparse-0.16.0 → trnsparse-0.16.1}/benchmarks/bench_spmm.py +0 -0
  29. {trnsparse-0.16.0 → trnsparse-0.16.1}/benchmarks/bench_spmv.py +0 -0
  30. {trnsparse-0.16.0 → trnsparse-0.16.1}/benchmarks/conftest.py +0 -0
  31. {trnsparse-0.16.0 → trnsparse-0.16.1}/docs/api.md +0 -0
  32. {trnsparse-0.16.0 → trnsparse-0.16.1}/docs/architecture.md +0 -0
  33. {trnsparse-0.16.0 → trnsparse-0.16.1}/docs/aws_setup.md +0 -0
  34. {trnsparse-0.16.0 → trnsparse-0.16.1}/docs/benchmarks.md +0 -0
  35. {trnsparse-0.16.0 → trnsparse-0.16.1}/docs/index.md +0 -0
  36. {trnsparse-0.16.0 → trnsparse-0.16.1}/docs/installation.md +0 -0
  37. {trnsparse-0.16.0 → trnsparse-0.16.1}/docs/iterative_solvers.md +0 -0
  38. {trnsparse-0.16.0 → trnsparse-0.16.1}/docs/migration_scipy.md +0 -0
  39. {trnsparse-0.16.0 → trnsparse-0.16.1}/docs/quickstart.md +0 -0
  40. {trnsparse-0.16.0 → trnsparse-0.16.1}/docs/sparse_attention.md +0 -0
  41. {trnsparse-0.16.0 → trnsparse-0.16.1}/examples/block_sparse_attention.py +0 -0
  42. {trnsparse-0.16.0 → trnsparse-0.16.1}/examples/sparse_fock.py +0 -0
  43. {trnsparse-0.16.0 → trnsparse-0.16.1}/infra/terraform/.terraform.lock.hcl +0 -0
  44. {trnsparse-0.16.0 → trnsparse-0.16.1}/infra/terraform/README.md +0 -0
  45. {trnsparse-0.16.0 → trnsparse-0.16.1}/infra/terraform/main.tf +0 -0
  46. {trnsparse-0.16.0 → trnsparse-0.16.1}/mkdocs.yml +0 -0
  47. {trnsparse-0.16.0 → trnsparse-0.16.1}/scripts/bench_eager_handle.py +0 -0
  48. {trnsparse-0.16.0 → trnsparse-0.16.1}/scripts/bench_to_md.py +0 -0
  49. {trnsparse-0.16.0 → trnsparse-0.16.1}/scripts/run_benchmarks.sh +0 -0
  50. {trnsparse-0.16.0 → trnsparse-0.16.1}/scripts/run_neuron_tests.sh +0 -0
  51. {trnsparse-0.16.0 → trnsparse-0.16.1}/scripts/run_simulator_tests.sh +0 -0
  52. {trnsparse-0.16.0 → trnsparse-0.16.1}/setup.cfg +0 -0
  53. {trnsparse-0.16.0 → trnsparse-0.16.1}/tests/conftest.py +0 -0
  54. {trnsparse-0.16.0 → trnsparse-0.16.1}/tests/test_attention.py +0 -0
  55. {trnsparse-0.16.0 → trnsparse-0.16.1}/tests/test_attn_gather.py +0 -0
  56. {trnsparse-0.16.0 → trnsparse-0.16.1}/tests/test_bsr.py +0 -0
  57. {trnsparse-0.16.0 → trnsparse-0.16.1}/tests/test_cg_bsr_reduction.py +0 -0
  58. {trnsparse-0.16.0 → trnsparse-0.16.1}/tests/test_cg_span.py +0 -0
  59. {trnsparse-0.16.0 → trnsparse-0.16.1}/tests/test_conformance.py +0 -0
  60. {trnsparse-0.16.0 → trnsparse-0.16.1}/tests/test_core_split.py +0 -0
  61. {trnsparse-0.16.0 → trnsparse-0.16.1}/tests/test_examples.py +0 -0
  62. {trnsparse-0.16.0 → trnsparse-0.16.1}/tests/test_iterative.py +0 -0
  63. {trnsparse-0.16.0 → trnsparse-0.16.1}/tests/test_kernel_dtype_guard.py +0 -0
  64. {trnsparse-0.16.0 → trnsparse-0.16.1}/tests/test_kernel_matmul.py +0 -0
  65. {trnsparse-0.16.0 → trnsparse-0.16.1}/tests/test_kernel_matmul_neuron.py +0 -0
  66. {trnsparse-0.16.0 → trnsparse-0.16.1}/tests/test_multicore.py +0 -0
  67. {trnsparse-0.16.0 → trnsparse-0.16.1}/tests/test_native_dispatch.py +0 -0
  68. {trnsparse-0.16.0 → trnsparse-0.16.1}/tests/test_nki_attn.py +0 -0
  69. {trnsparse-0.16.0 → trnsparse-0.16.1}/tests/test_nki_bsr.py +0 -0
  70. {trnsparse-0.16.0 → trnsparse-0.16.1}/tests/test_nki_cg_fused.py +0 -0
  71. {trnsparse-0.16.0 → trnsparse-0.16.1}/tests/test_nki_compile.py +0 -0
  72. {trnsparse-0.16.0 → trnsparse-0.16.1}/tests/test_nki_kernel_matmul.py +0 -0
  73. {trnsparse-0.16.0 → trnsparse-0.16.1}/tests/test_nki_native.py +0 -0
  74. {trnsparse-0.16.0 → trnsparse-0.16.1}/tests/test_nki_screened_spmm.py +0 -0
  75. {trnsparse-0.16.0 → trnsparse-0.16.1}/tests/test_nki_sim.py +0 -0
  76. {trnsparse-0.16.0 → trnsparse-0.16.1}/tests/test_nki_spmm.py +0 -0
  77. {trnsparse-0.16.0 → trnsparse-0.16.1}/tests/test_nki_tail_audit.py +0 -0
  78. {trnsparse-0.16.0 → trnsparse-0.16.1}/tests/test_ops.py +0 -0
  79. {trnsparse-0.16.0 → trnsparse-0.16.1}/tests/test_screening.py +0 -0
  80. {trnsparse-0.16.0 → trnsparse-0.16.1}/tests/test_trace_cache_safety.py +0 -0
  81. {trnsparse-0.16.0 → trnsparse-0.16.1}/trnsparse/_reduce.py +0 -0
  82. {trnsparse-0.16.0 → trnsparse-0.16.1}/trnsparse/iterative.py +0 -0
  83. {trnsparse-0.16.0 → trnsparse-0.16.1}/trnsparse/kernel_matmul.py +0 -0
  84. {trnsparse-0.16.0 → trnsparse-0.16.1}/trnsparse/multicore.py +0 -0
  85. {trnsparse-0.16.0 → trnsparse-0.16.1}/trnsparse/nki/__init__.py +0 -0
  86. {trnsparse-0.16.0 → trnsparse-0.16.1}/trnsparse/nki/_native.py +0 -0
  87. {trnsparse-0.16.0 → trnsparse-0.16.1}/trnsparse/nki/_native_attn.py +0 -0
  88. {trnsparse-0.16.0 → trnsparse-0.16.1}/trnsparse/nki/dispatch.py +0 -0
  89. {trnsparse-0.16.0 → trnsparse-0.16.1}/trnsparse/nki/kernels.py +0 -0
  90. {trnsparse-0.16.0 → trnsparse-0.16.1}/trnsparse/ops.py +0 -0
  91. {trnsparse-0.16.0 → trnsparse-0.16.1}/trnsparse/screening.py +0 -0
  92. {trnsparse-0.16.0 → trnsparse-0.16.1}/trnsparse.egg-info/SOURCES.txt +0 -0
  93. {trnsparse-0.16.0 → trnsparse-0.16.1}/trnsparse.egg-info/dependency_links.txt +0 -0
  94. {trnsparse-0.16.0 → trnsparse-0.16.1}/trnsparse.egg-info/requires.txt +0 -0
  95. {trnsparse-0.16.0 → trnsparse-0.16.1}/trnsparse.egg-info/top_level.txt +0 -0
@@ -7,6 +7,24 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [0.16.1] — 2026-11-10
11
+
12
+ Validated on Trn2 (trn2.3xlarge, Neuron SDK 2.33.0, native TorchNeuron) — see trnsci/trnsci#35.
13
+
14
+ Headline: from_dense on the neuron device returns correct indices and values above 2^24 elements (silent, #134)
15
+
16
+ ### Fixed
17
+
18
+ - **`from_dense` on the neuron device: wrong column indices and values for
19
+ matrices of more than 2^24 elements** (silent, #134, trnsci/trnsci#135).
20
+ The device `nonzero` / `torch.where` / `A[mask]` round the flat index
21
+ `row * n + col` through fp32 above 2^24: on Trn2 a (2100, 8100) matrix
22
+ with 4002 nonzeros came back with 19 wrong column indices. The indices now
23
+ come from a host `nonzero` of the mask, and the values are gathered on the
24
+ device in row blocks whose flat indices stay below 2^23 (a device
25
+ `index_select` at indices above 2^24 is rounded as well).
26
+ `BSRMatrix.from_dense` takes its block indices from the host mask too.
27
+
10
28
  ## [0.16.0] — 2026-11-10
11
29
 
12
30
  Validated on Trn2 (trn2.3xlarge, Neuron SDK 2.33.0, native TorchNeuron) — see trnsci/trnsci#35.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: trnsparse
3
- Version: 0.16.0
3
+ Version: 0.16.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.16.0"
7
+ version = "0.16.1"
8
8
  description = "Sparse matrix operations for AWS Trainium via NKI"
9
9
  readme = "README.md"
10
10
  license = "Apache-2.0"
@@ -137,3 +137,97 @@ class TestFromScipy:
137
137
  A = trnsparse.from_scipy(M)
138
138
  assert A.dtype == torch.float64
139
139
  np.testing.assert_array_equal(A.to_dense().numpy(), M.toarray())
140
+
141
+
142
+ # from_dense on the device (trnsci/trnsci#135): TorchNeuron's nonzero /
143
+ # torch.where / A[mask] round flat indices above 2^24 through fp32, so a
144
+ # device dense matrix with more than 2^24 elements got wrong column indices
145
+ # (and the values at those wrong positions). The device path takes the
146
+ # indices from a host nonzero instead.
147
+ _BIG_SHAPE = (2100, 8100) # 17.0M elements > 2^24
148
+
149
+
150
+ def _big_sparse_dense(seed=2, nnz=4000):
151
+ g = torch.Generator().manual_seed(seed)
152
+ m, n = _BIG_SHAPE
153
+ D = torch.zeros(m, n)
154
+ r = torch.randint(0, m, (nnz,), generator=g)
155
+ c = torch.randint(0, n, (nnz,), generator=g)
156
+ D[r, c] = torch.randn(nnz, generator=g)
157
+ D[m - 1, n - 3 :] = torch.tensor([1.5, 2.5, 3.5]) # flat indices above 2^24
158
+ return D
159
+
160
+
161
+ def _assert_same_csr(got, ref):
162
+ assert got.shape == ref.shape
163
+ assert torch.equal(got.col_indices.cpu(), ref.col_indices)
164
+ assert torch.equal(got.row_ptrs.cpu(), ref.row_ptrs)
165
+ assert torch.equal(got.values.cpu(), ref.values)
166
+
167
+
168
+ class TestFromDenseHostIndex:
169
+ def test_host_index_path_matches_where(self):
170
+ from trnsparse.formats import _dense_nonzeros
171
+
172
+ D = _big_sparse_dense()
173
+ mask = D != 0
174
+ r0, c0, v0 = _dense_nonzeros(D, mask, host_index=False)
175
+ r1, c1, v1 = _dense_nonzeros(D, mask, host_index=True)
176
+ assert torch.equal(r0, r1) and torch.equal(c0, c1) and torch.equal(v0, v1)
177
+ assert int(c1[-1]) == _BIG_SHAPE[1] - 1 and float(v1[-1]) == 3.5
178
+
179
+ @pytest.mark.parametrize("span", [1, 4, 6, 1 << 23])
180
+ def test_host_index_path_row_blocks(self, monkeypatch, span):
181
+ from trnsparse import formats
182
+ from trnsparse.formats import _dense_nonzeros
183
+
184
+ monkeypatch.setattr(formats, "_GATHER_SPAN", span) # span 1: every row on the host
185
+ g = torch.Generator().manual_seed(3)
186
+ A = torch.randn(7, 5, generator=g).masked_fill(torch.rand(7, 5, generator=g) < 0.5, 0.0)
187
+ A[3] = 0.0 # an empty row block
188
+ A.requires_grad_(span == 1 << 23)
189
+ mask = A != 0
190
+ r1, c1, v1 = _dense_nonzeros(A, mask, host_index=True)
191
+ r0, c0 = torch.where(mask)
192
+ assert torch.equal(r0, r1) and torch.equal(c0, c1) and torch.equal(v1, A[mask])
193
+ if A.requires_grad:
194
+ v1.sum().backward()
195
+ assert torch.equal(A.grad, mask.to(A.dtype))
196
+
197
+ def test_host_index_path_threshold_and_dtype(self):
198
+ from trnsparse.formats import _dense_nonzeros
199
+
200
+ A = torch.tensor([[0.5, -2.0, 0.0], [3.0, 0.1, -0.2]], dtype=torch.float64)
201
+ mask = A.abs() > 0.3
202
+ r, c, v = _dense_nonzeros(A, mask, host_index=True)
203
+ assert r.tolist() == [0, 0, 1] and c.tolist() == [0, 1, 0]
204
+ assert v.dtype == torch.float64 and v.tolist() == [0.5, -2.0, 3.0]
205
+
206
+
207
+ def _neuron_device():
208
+ try:
209
+ import torch_neuronx # noqa: F401
210
+ except ImportError:
211
+ pytest.skip("requires torch-neuronx")
212
+ return "neuron"
213
+
214
+
215
+ @pytest.mark.neuron
216
+ def test_from_dense_on_device_above_2_24_elements():
217
+ dev = _neuron_device()
218
+ D = _big_sparse_dense()
219
+ _assert_same_csr(trnsparse.from_dense(D.to(dev)), trnsparse.from_dense(D))
220
+
221
+
222
+ @pytest.mark.neuron
223
+ def test_bsr_from_dense_on_device():
224
+ dev = _neuron_device()
225
+ g = torch.Generator().manual_seed(7)
226
+ D = torch.zeros(1024, 1024)
227
+ D[:128, 896:] = torch.randn(128, 128, generator=g)
228
+ D[512:640, 128:256] = torch.randn(128, 128, generator=g)
229
+ ref = trnsparse.BSRMatrix.from_dense(D, block_size=128)
230
+ got = trnsparse.BSRMatrix.from_dense(D.to(dev), block_size=128)
231
+ assert torch.equal(got.block_col_indices.cpu(), ref.block_col_indices)
232
+ assert torch.equal(got.block_row_ptrs.cpu(), ref.block_row_ptrs)
233
+ assert torch.equal(got.blocks.cpu(), ref.blocks)
@@ -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.16.0"
8
+ __version__ = "0.16.1"
9
9
 
10
10
  from .formats import BSRMatrix, COOMatrix, CSRMatrix, eye_sparse, from_dense, from_scipy
11
11
  from .iterative import (
@@ -228,8 +228,13 @@ class BSRMatrix:
228
228
  block_row_ptrs = torch.zeros(mb + 1, dtype=torch.long)
229
229
  block_row_ptrs[1:] = nonzero_mask.sum(dim=1).cumsum(dim=0)
230
230
 
231
- block_col_indices = nonzero_mask.nonzero(as_tuple=False)[:, 1].to(torch.long)
232
- kept_blocks = block_grid[nonzero_mask] # (n_blocks, block_size, block_size)
231
+ # Host nonzero: on the device, nonzero rounds flat indices above 2^24
232
+ # through fp32 (see `_dense_nonzeros`); the block mask is small.
233
+ mask_h = nonzero_mask.cpu()
234
+ block_col_indices = mask_h.nonzero(as_tuple=False)[:, 1].contiguous()
235
+ block_col_indices = block_col_indices.to(nonzero_mask.device)
236
+ kept = mask_h.reshape(-1).nonzero(as_tuple=False)[:, 0].to(block_grid.device)
237
+ kept_blocks = block_grid.reshape(-1, block_size, block_size).index_select(0, kept)
233
238
 
234
239
  return cls(
235
240
  blocks=kept_blocks.contiguous(),
@@ -306,12 +311,57 @@ def _csr_to_dense_host(A: CSRMatrix) -> torch.Tensor:
306
311
  # --- Construction helpers ---
307
312
 
308
313
 
314
+ # Value gathers in `_dense_nonzeros` use blocks of rows whose flat indices
315
+ # stay below 2^23: the device's int64 index_select is rounded through fp32
316
+ # above 2^24 as well (an out-of-bounds gather on a 17M-element matrix, Trn2).
317
+ _GATHER_SPAN = 1 << 23
318
+
319
+
320
+ def _dense_nonzeros(
321
+ A: torch.Tensor, mask: torch.Tensor, host_index: bool
322
+ ) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
323
+ """(rows, cols, values) of the True entries of the 2-D `mask`, row-major.
324
+
325
+ On the TorchNeuron ``neuron`` device, ``nonzero`` / ``torch.where`` /
326
+ ``argwhere`` / ``masked_select`` / boolean-mask indexing of a tensor with
327
+ more than 2^24 elements round the flat index ``row * n + col`` through
328
+ fp32: the column of an entry whose flat index is above 2^24 comes back as
329
+ ``fp32(row * n + col) - row * n``, and ``A[mask]`` returns the values at
330
+ those positions, silently (Trn2, SDK 2.33, trnsci/trnsci#135; the same
331
+ fp32 rounding of integers as aws-neuron/aws-neuron-sdk#1466).
332
+
333
+ `host_index` (any device other than the CPU) therefore takes the indices
334
+ from a host ``nonzero`` of the mask (one byte per element) and gathers the
335
+ values on A's device in blocks of rows, each with flat indices below 2^23.
336
+ """
337
+ if not host_index:
338
+ rows, cols = torch.where(mask)
339
+ return rows, cols, A[mask]
340
+ rows_h, cols_h = torch.where(mask.cpu())
341
+ m, n = A.shape
342
+ step = max(1, _GATHER_SPAN // max(n, 1))
343
+ starts = list(range(0, m, step))
344
+ bounds = torch.searchsorted(rows_h, torch.tensor([*starts, m])).tolist()
345
+ parts = []
346
+ for b, r0 in enumerate(starts):
347
+ lo, hi = bounds[b], bounds[b + 1]
348
+ if lo == hi:
349
+ continue
350
+ local = ((rows_h[lo:hi] - r0) * n + cols_h[lo:hi]).contiguous()
351
+ block = A[r0 : r0 + step].reshape(-1)
352
+ if block.numel() > 2 * _GATHER_SPAN: # a single row above 2^24: gather on the host
353
+ parts.append(block.detach().cpu().index_select(0, local).to(A.device))
354
+ else:
355
+ parts.append(block.index_select(0, local.to(A.device)))
356
+ vals = torch.cat(parts) if parts else A.new_zeros(0)
357
+ return rows_h.contiguous().to(A.device), cols_h.contiguous().to(A.device), vals
358
+
359
+
309
360
  def from_dense(A: torch.Tensor, threshold: float = 0.0) -> CSRMatrix:
310
361
  """Convert dense matrix to CSR, dropping values with |v| <= threshold."""
311
362
  m, n = A.shape
312
363
  mask = torch.abs(A) > threshold
313
- rows, cols = torch.where(mask)
314
- vals = A[mask]
364
+ rows, cols, vals = _dense_nonzeros(A, mask, host_index=A.device.type != "cpu")
315
365
 
316
366
  # Build CSR
317
367
  coo = COOMatrix(values=vals, row_indices=rows, col_indices=cols, shape=(m, n))
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: trnsparse
3
- Version: 0.16.0
3
+ Version: 0.16.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
File without changes
File without changes