trnsparse 0.14.0__tar.gz → 0.15.0__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 (92) hide show
  1. {trnsparse-0.14.0 → trnsparse-0.15.0}/CHANGELOG.md +38 -5
  2. {trnsparse-0.14.0/trnsparse.egg-info → trnsparse-0.15.0}/PKG-INFO +1 -1
  3. {trnsparse-0.14.0 → trnsparse-0.15.0}/docs/api.md +1 -1
  4. {trnsparse-0.14.0 → trnsparse-0.15.0}/pyproject.toml +1 -1
  5. {trnsparse-0.14.0 → trnsparse-0.15.0}/tests/test_kernel_matmul.py +8 -4
  6. {trnsparse-0.14.0 → trnsparse-0.15.0}/tests/test_kernel_matmul_neuron.py +21 -1
  7. {trnsparse-0.14.0 → trnsparse-0.15.0}/tests/test_nki_kernel_matmul.py +12 -0
  8. {trnsparse-0.14.0 → trnsparse-0.15.0}/trnsparse/__init__.py +1 -1
  9. {trnsparse-0.14.0 → trnsparse-0.15.0}/trnsparse/nki/kernels.py +39 -15
  10. {trnsparse-0.14.0 → trnsparse-0.15.0/trnsparse.egg-info}/PKG-INFO +1 -1
  11. {trnsparse-0.14.0 → trnsparse-0.15.0}/.github/workflows/ci.yml +0 -0
  12. {trnsparse-0.14.0 → trnsparse-0.15.0}/.github/workflows/notify-umbrella.yml +0 -0
  13. {trnsparse-0.14.0 → trnsparse-0.15.0}/.github/workflows/publish.yml +0 -0
  14. {trnsparse-0.14.0 → trnsparse-0.15.0}/.gitignore +0 -0
  15. {trnsparse-0.14.0 → trnsparse-0.15.0}/.pre-commit-config.yaml +0 -0
  16. {trnsparse-0.14.0 → trnsparse-0.15.0}/CLAUDE.md +0 -0
  17. {trnsparse-0.14.0 → trnsparse-0.15.0}/CODE_OF_CONDUCT.md +0 -0
  18. {trnsparse-0.14.0 → trnsparse-0.15.0}/CONTRIBUTING.md +0 -0
  19. {trnsparse-0.14.0 → trnsparse-0.15.0}/LICENSE +0 -0
  20. {trnsparse-0.14.0 → trnsparse-0.15.0}/README.md +0 -0
  21. {trnsparse-0.14.0 → trnsparse-0.15.0}/benchmarks/bench_bsr_spmm.py +0 -0
  22. {trnsparse-0.14.0 → trnsparse-0.15.0}/benchmarks/bench_cg_fused_trn2.py +0 -0
  23. {trnsparse-0.14.0 → trnsparse-0.15.0}/benchmarks/bench_iterative.py +0 -0
  24. {trnsparse-0.14.0 → trnsparse-0.15.0}/benchmarks/bench_kernel_matmul_trn2.py +0 -0
  25. {trnsparse-0.14.0 → trnsparse-0.15.0}/benchmarks/bench_multicore_trn2.py +0 -0
  26. {trnsparse-0.14.0 → trnsparse-0.15.0}/benchmarks/bench_multiproc_trn2.py +0 -0
  27. {trnsparse-0.14.0 → trnsparse-0.15.0}/benchmarks/bench_native_trn2.py +0 -0
  28. {trnsparse-0.14.0 → trnsparse-0.15.0}/benchmarks/bench_power_fused_trn2.py +0 -0
  29. {trnsparse-0.14.0 → trnsparse-0.15.0}/benchmarks/bench_screened_tiles_trn2.py +0 -0
  30. {trnsparse-0.14.0 → trnsparse-0.15.0}/benchmarks/bench_screening.py +0 -0
  31. {trnsparse-0.14.0 → trnsparse-0.15.0}/benchmarks/bench_spmm.py +0 -0
  32. {trnsparse-0.14.0 → trnsparse-0.15.0}/benchmarks/bench_spmv.py +0 -0
  33. {trnsparse-0.14.0 → trnsparse-0.15.0}/benchmarks/conftest.py +0 -0
  34. {trnsparse-0.14.0 → trnsparse-0.15.0}/docs/architecture.md +0 -0
  35. {trnsparse-0.14.0 → trnsparse-0.15.0}/docs/aws_setup.md +0 -0
  36. {trnsparse-0.14.0 → trnsparse-0.15.0}/docs/benchmarks.md +0 -0
  37. {trnsparse-0.14.0 → trnsparse-0.15.0}/docs/index.md +0 -0
  38. {trnsparse-0.14.0 → trnsparse-0.15.0}/docs/installation.md +0 -0
  39. {trnsparse-0.14.0 → trnsparse-0.15.0}/docs/iterative_solvers.md +0 -0
  40. {trnsparse-0.14.0 → trnsparse-0.15.0}/docs/migration_scipy.md +0 -0
  41. {trnsparse-0.14.0 → trnsparse-0.15.0}/docs/quickstart.md +0 -0
  42. {trnsparse-0.14.0 → trnsparse-0.15.0}/docs/sparse_attention.md +0 -0
  43. {trnsparse-0.14.0 → trnsparse-0.15.0}/examples/block_sparse_attention.py +0 -0
  44. {trnsparse-0.14.0 → trnsparse-0.15.0}/examples/sparse_fock.py +0 -0
  45. {trnsparse-0.14.0 → trnsparse-0.15.0}/infra/terraform/.terraform.lock.hcl +0 -0
  46. {trnsparse-0.14.0 → trnsparse-0.15.0}/infra/terraform/README.md +0 -0
  47. {trnsparse-0.14.0 → trnsparse-0.15.0}/infra/terraform/main.tf +0 -0
  48. {trnsparse-0.14.0 → trnsparse-0.15.0}/mkdocs.yml +0 -0
  49. {trnsparse-0.14.0 → trnsparse-0.15.0}/scripts/bench_eager_handle.py +0 -0
  50. {trnsparse-0.14.0 → trnsparse-0.15.0}/scripts/bench_to_md.py +0 -0
  51. {trnsparse-0.14.0 → trnsparse-0.15.0}/scripts/run_benchmarks.sh +0 -0
  52. {trnsparse-0.14.0 → trnsparse-0.15.0}/scripts/run_neuron_tests.sh +0 -0
  53. {trnsparse-0.14.0 → trnsparse-0.15.0}/scripts/run_simulator_tests.sh +0 -0
  54. {trnsparse-0.14.0 → trnsparse-0.15.0}/setup.cfg +0 -0
  55. {trnsparse-0.14.0 → trnsparse-0.15.0}/tests/conftest.py +0 -0
  56. {trnsparse-0.14.0 → trnsparse-0.15.0}/tests/test_attention.py +0 -0
  57. {trnsparse-0.14.0 → trnsparse-0.15.0}/tests/test_attn_gather.py +0 -0
  58. {trnsparse-0.14.0 → trnsparse-0.15.0}/tests/test_bsr.py +0 -0
  59. {trnsparse-0.14.0 → trnsparse-0.15.0}/tests/test_conformance.py +0 -0
  60. {trnsparse-0.14.0 → trnsparse-0.15.0}/tests/test_core_split.py +0 -0
  61. {trnsparse-0.14.0 → trnsparse-0.15.0}/tests/test_examples.py +0 -0
  62. {trnsparse-0.14.0 → trnsparse-0.15.0}/tests/test_formats.py +0 -0
  63. {trnsparse-0.14.0 → trnsparse-0.15.0}/tests/test_iterative.py +0 -0
  64. {trnsparse-0.14.0 → trnsparse-0.15.0}/tests/test_kernel_dtype_guard.py +0 -0
  65. {trnsparse-0.14.0 → trnsparse-0.15.0}/tests/test_multicore.py +0 -0
  66. {trnsparse-0.14.0 → trnsparse-0.15.0}/tests/test_native_dispatch.py +0 -0
  67. {trnsparse-0.14.0 → trnsparse-0.15.0}/tests/test_nki_attn.py +0 -0
  68. {trnsparse-0.14.0 → trnsparse-0.15.0}/tests/test_nki_bsr.py +0 -0
  69. {trnsparse-0.14.0 → trnsparse-0.15.0}/tests/test_nki_cg_fused.py +0 -0
  70. {trnsparse-0.14.0 → trnsparse-0.15.0}/tests/test_nki_compile.py +0 -0
  71. {trnsparse-0.14.0 → trnsparse-0.15.0}/tests/test_nki_native.py +0 -0
  72. {trnsparse-0.14.0 → trnsparse-0.15.0}/tests/test_nki_screened_spmm.py +0 -0
  73. {trnsparse-0.14.0 → trnsparse-0.15.0}/tests/test_nki_sim.py +0 -0
  74. {trnsparse-0.14.0 → trnsparse-0.15.0}/tests/test_nki_spmm.py +0 -0
  75. {trnsparse-0.14.0 → trnsparse-0.15.0}/tests/test_nki_tail_audit.py +0 -0
  76. {trnsparse-0.14.0 → trnsparse-0.15.0}/tests/test_ops.py +0 -0
  77. {trnsparse-0.14.0 → trnsparse-0.15.0}/tests/test_screening.py +0 -0
  78. {trnsparse-0.14.0 → trnsparse-0.15.0}/tests/test_trace_cache_safety.py +0 -0
  79. {trnsparse-0.14.0 → trnsparse-0.15.0}/trnsparse/formats.py +0 -0
  80. {trnsparse-0.14.0 → trnsparse-0.15.0}/trnsparse/iterative.py +0 -0
  81. {trnsparse-0.14.0 → trnsparse-0.15.0}/trnsparse/kernel_matmul.py +0 -0
  82. {trnsparse-0.14.0 → trnsparse-0.15.0}/trnsparse/multicore.py +0 -0
  83. {trnsparse-0.14.0 → trnsparse-0.15.0}/trnsparse/nki/__init__.py +0 -0
  84. {trnsparse-0.14.0 → trnsparse-0.15.0}/trnsparse/nki/_native.py +0 -0
  85. {trnsparse-0.14.0 → trnsparse-0.15.0}/trnsparse/nki/_native_attn.py +0 -0
  86. {trnsparse-0.14.0 → trnsparse-0.15.0}/trnsparse/nki/dispatch.py +0 -0
  87. {trnsparse-0.14.0 → trnsparse-0.15.0}/trnsparse/ops.py +0 -0
  88. {trnsparse-0.14.0 → trnsparse-0.15.0}/trnsparse/screening.py +0 -0
  89. {trnsparse-0.14.0 → trnsparse-0.15.0}/trnsparse.egg-info/SOURCES.txt +0 -0
  90. {trnsparse-0.14.0 → trnsparse-0.15.0}/trnsparse.egg-info/dependency_links.txt +0 -0
  91. {trnsparse-0.14.0 → trnsparse-0.15.0}/trnsparse.egg-info/requires.txt +0 -0
  92. {trnsparse-0.14.0 → trnsparse-0.15.0}/trnsparse.egg-info/top_level.txt +0 -0
@@ -7,6 +7,38 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [0.15.0] — 2026-11-04
11
+
12
+ Validated on Trn2 (trn2.3xlarge, Neuron SDK 2.33.0, native TorchNeuron) — see trnsci/trnsci#35.
13
+
14
+ Headline: `kernel_matmul` auto loop body up to 32 Y tiles at m >= 131072 (#108, #111): rbf R = 128 runs 18% faster, and fp32 error drops from 7.0e-7 to 5.3e-7 at m = 524288. A 2-slot SBUF scratch ring keeps the Matern kernels on NKI allocation at 32 tiles.
15
+
16
+ ### Performance
17
+
18
+ - **`kernel_matmul`: the default loop body grows to 32 Y tiles at
19
+ m >= 131072** (`_KMV_AUTO_GROUP_MAX` 16 -> 32, #108). Trn2, one logical
20
+ core, n = m = 131072 and 524288, `rbf`: R = 128 goes from 4.3e10 to
21
+ 5.1e10 kernel entries/s (+18%, also at LNC=1), R = 768 +19%, R = 33
22
+ +15% to +55% (it varies between runs), R = 1 +2%. Matern-5/2 gains
23
+ 12%, Matern-3/2 4-6%; Matern-1/2 loses 3% at R = 1 and gains 4% at
24
+ R = 128 (both groups with the scratch ring below). The fp32 error falls
25
+ with it, as the g + m/(128 g) model predicts: 7.0e-7 -> 5.3e-7 against
26
+ float64 at m = 524288 (summation-bound case, positive V, lengthscale
27
+ 0.5). `kmv_group(m)` is still about sqrt(m / 128); smaller m and
28
+ `TRNSPARSE_KMV_GROUP` are unchanged.
29
+ - **`kernel_matmul`: Matern intermediates share a 2-slot SBUF ring per
30
+ loop body** (#108). NKI allocations live for their enclosing region, so
31
+ the per-Y-tile temporaries of the Matern kernels (one to three 2 KiB
32
+ tiles each) piled up across a body. Bodies of 24 Matern-5/2 tiles (20 at
33
+ R >= 384), 32 Matern-3/2 tiles (24 at R >= 512) and 32 Matern-1/2 tiles
34
+ at R >= 512 overflowed SBUF and silently fell back to neuronx-cc
35
+ allocation (correct, but without NKI scheduling). Now every kernel keeps
36
+ NKI's allocation at 32 tiles for every R checked (1 to 1500, rbf to
37
+ 3000) at LNC=1 and LNC=2. The
38
+ instructions are the same, so the results are bit-identical at a given
39
+ group, and at group 16 Matern-1/2 and Matern-3/2 run 23% and 14% faster
40
+ (2.83e10 -> 3.49e10 and 2.78e10 -> 3.16e10 entries/s at n = 131072).
41
+
10
42
  ## [0.14.0] — 2026-11-04
11
43
 
12
44
  Validated on Trn2 (trn2.3xlarge, Neuron SDK 2.33.0, native TorchNeuron) — see trnsci/trnsci#35.
@@ -97,11 +129,12 @@ Headline: `kernel_matmul` / `KernelOperator`, a fused kernel-matrix product for
97
129
  16; 6.2M with the old body of 4. That is 44 against 13 instructions per
98
130
  128 x 512 tile per core.
99
131
  - **Summation order:** with a body of 4 Y tiles (m < 3200 by default,
100
- or `TRNSPARSE_KMV_GROUP=4`) the result is bit-identical to 0.13.0.
101
- That is checked in the simulator, with its matmul summed in
102
- contraction order like the TensorEngine; the device check is on the
103
- PR. Larger bodies change where the PSUM partial sums are split, so the
104
- bits change.
132
+ or `TRNSPARSE_KMV_GROUP=4`) the summation order matches 0.13.0.
133
+ That holds in the simulator, with its matmul summed in contraction
134
+ order like the TensorEngine. On Trn2 hardware it does not: group 4
135
+ differs from 0.13.0 by up to about 1.5e-5, with unchanged error against
136
+ float64 (#107). Larger bodies change where the PSUM partial sums are
137
+ split, so the bits change.
105
138
  - **Error against float64** goes from about g + m/(128 g) fp32 roundings
106
139
  to its minimum: at m = 131072 the max relative error drops from
107
140
  9.0e-7 to 5.2e-7 (simulator, rbf and Matern-5/2, R = 1 and 33).
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: trnsparse
3
- Version: 0.14.0
3
+ Version: 0.15.0
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
@@ -104,7 +104,7 @@ Matern-1/2 (a cusp at 0) as `sqrt(u |x|^2) / l`. float64 follows the suite
104
104
  policy: `auto` computes in torch float64, explicit `nki` casts with a
105
105
  warning, `TRNSPARSE_REQUIRE_NKI` raises. Env: `TRNSPARSE_KMV_GROUP` (Y tiles
106
106
  per loop body, 1 to 32; default `kmv_group(m)`: about `sqrt(m / 128)`, from 4
107
- to 16, which also minimises the fp32 summation error), `TRNSPARSE_KMV_BLOCK_WORK` (kernel entries
107
+ to 32 (32 from m = 131072), which also minimises the fp32 summation error), `TRNSPARSE_KMV_BLOCK_WORK` (kernel entries
108
108
  per launch, default `6e10`), `TRNSPARSE_NKI_EAGER_HANDLE=0` (custom op instead
109
109
  of the cached `wrap_nki` handle).
110
110
 
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "trnsparse"
7
- version = "0.14.0"
7
+ version = "0.15.0"
8
8
  description = "Sparse matrix operations for AWS Trainium via NKI"
9
9
  readme = "README.md"
10
10
  license = "Apache-2.0"
@@ -264,13 +264,17 @@ def test_kmv_group_rule(monkeypatch):
264
264
  # Small m: the fixed 4-tile body of earlier releases (same summation, same bits).
265
265
  for m in (1, 128, 1000, 3199):
266
266
  assert K.kmv_group(m) == 4
267
- # About sqrt(m / 128) Y tiles in between, capped at 16.
267
+ # About sqrt(m / 128) Y tiles in between, capped at 32 (#108).
268
268
  assert K.kmv_group(3200) == 5
269
269
  assert K.kmv_group(8192) == 8
270
- assert [K.kmv_group(m) for m in (32768, 131072, 1 << 20)] == [16, 16, 16]
271
- assert all(K.kmv_group(m) <= K.kmv_group(m + 128) for m in range(0, 40000, 128))
270
+ assert K.kmv_group(32768) == 16
271
+ assert K.kmv_group(131072 - 128) == 31
272
+ assert [K.kmv_group(m) for m in (131072, 524288, 1 << 20)] == [32, 32, 32]
273
+ assert K._KMV_AUTO_GROUP_MAX == K._KMV_MAX_GROUP == 32
274
+ assert all(K.kmv_group(m) <= K.kmv_group(m + 128) for m in range(0, 140000, 128))
272
275
  monkeypatch.delenv("TRNSPARSE_KMV_GROUP", raising=False)
273
- assert _kmv_group(131072) == 16
276
+ assert _kmv_group(131072) == 32
277
+ assert _kmv_group(32768) == 16
274
278
  monkeypatch.setenv("TRNSPARSE_KMV_GROUP", "7")
275
279
  assert _kmv_group(131072) == 7
276
280
  monkeypatch.setenv("TRNSPARSE_KMV_GROUP", "100")
@@ -94,7 +94,7 @@ def test_square_operator_on_device(kernel, n):
94
94
  @pytest.mark.parametrize("R", [1, 33, 130])
95
95
  @pytest.mark.parametrize("group", [4, 8, 16, 32])
96
96
  def test_group_sizes_on_device(monkeypatch, group, R):
97
- """Every loop-body size (#103: default `kmv_group(m)`, 16 here) is accurate on the device."""
97
+ """Every loop-body size (#103: default `kmv_group(m)`, 8 here) is accurate on the device."""
98
98
  monkeypatch.setenv("TRNSPARSE_KMV_GROUP", str(group))
99
99
  g = torch.Generator().manual_seed(R)
100
100
  n = 8192 + 77 # a ragged Y tile and ragged rows
@@ -104,6 +104,26 @@ def test_group_sizes_on_device(monkeypatch, group, R):
104
104
  assert _rel(op(V.to(DEV)), ref) < 2e-5
105
105
 
106
106
 
107
+ @pytest.mark.parametrize("R", [1, 128, 1500])
108
+ @pytest.mark.parametrize("kernel", ["rbf", "matern12", "matern32", "matern52"])
109
+ def test_auto_group_32_on_device(monkeypatch, kernel, R):
110
+ """m >= 131072 picks the 32-tile body (#108): accurate for every kernel and V width."""
111
+ from trnsparse.nki.kernels import kmv_group
112
+
113
+ monkeypatch.delenv("TRNSPARSE_KMV_GROUP", raising=False)
114
+ g = torch.Generator().manual_seed(R)
115
+ m, q = 131072 + 77, 600
116
+ assert kmv_group(m) == 32
117
+ X, Y = torch.rand(m, 3, generator=g), torch.rand(q, 3, generator=g)
118
+ W = torch.randn(m, R, generator=g)
119
+ op = trnsparse.KernelOperator(X, kernel, 0.05, 1.0, device=DEV)
120
+ ref = trnsparse.kernel_matmul_reference(Y, X, W, kernel, 0.05, 1.0)
121
+ # Matern-1/2's cusp at 0 errs like sqrt(u |x|^2) / l whatever the group
122
+ # (about 5e-5 here at group 16 or 32); the smooth kernels stay near 1e-5.
123
+ tol = 2e-4 if kernel == "matern12" else 3e-5
124
+ assert _rel(op.cross(Y, W.to(DEV)), ref) < tol
125
+
126
+
107
127
  def test_handle_and_custom_op_agree(monkeypatch):
108
128
  g = torch.Generator().manual_seed(3)
109
129
  X, V = torch.rand(3000, 3, generator=g), torch.randn(3000, 17, generator=g)
@@ -271,6 +271,18 @@ def _compile_cases():
271
271
  (131072, 128, 3, 16),
272
272
  (8192, 768, 0, 32),
273
273
  (8192, 769, 0, 16),
274
+ # The auto group reaches 32 at m >= 131072 (#108): every V pass width
275
+ # (one tile, the 64 KiB batched-V limit at 512 columns, a full
276
+ # 6-tile pass, two passes) with the most SBUF temporaries (Matern-5/2).
277
+ # Without the scratch ring, Matern-5/2 fell back to neuronx-cc
278
+ # allocation from 24 tiles (20 at R >= 384).
279
+ (131072, 1, 3, 32),
280
+ (131072, 128, 3, 32),
281
+ (131072, 512, 3, 32),
282
+ (131072, 513, 3, 32),
283
+ (131072, 768, 3, 32),
284
+ (131072, 1500, 3, 32),
285
+ (131072, 1500, 0, 32),
274
286
  ]:
275
287
  cases[f"self-n{n}-R{R}-k{kind}-g{g}"] = (
276
288
  "_kmv_self_kernel",
@@ -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.14.0"
8
+ __version__ = "0.15.0"
9
9
 
10
10
  from .formats import BSRMatrix, COOMatrix, CSRMatrix, eye_sparse, from_dense, from_scipy
11
11
  from .iterative import (
@@ -2622,9 +2622,11 @@ _KMV_RT = 128 # V columns per accumulator (the stationary operand's free size)
2622
2622
  _KMV_PASS_RT = 6 # accumulators (V column tiles) per pass over Y; leaves >= 2 PSUM banks
2623
2623
  _KMV_GROUP = 4 # Y tiles per rolled-loop body (one SBUF partial-sum add per body)
2624
2624
  _KMV_MAX_GROUP = 32 # cap on the body size (SBUF for the body's K^T and V tiles)
2625
- _KMV_AUTO_GROUP_MAX = 16 # largest body `kmv_group` picks
2625
+ _KMV_AUTO_GROUP_MAX = 32 # largest body `kmv_group` picks (#108)
2626
2626
  _KMV_V_BATCH_BYTES = 64 * 1024 # SBUF per partition for one body's V rows in one DMA
2627
2627
  _KMV_PSUM_BANKS = 8 # 16 KiB of PSUM per partition in 2 KiB banks
2628
+ _KMV_SCRATCH = (0, 1, 2, 3) # SBUF intermediates per kernel tile, by kind (rbf, matern12/32/52)
2629
+ _KMV_SCRATCH_RING = 2 # slots of them per loop body, shared round-robin by its Y tiles
2628
2630
 
2629
2631
 
2630
2632
  def kmv_plan(n: int, R: int, n_prog: int = 1):
@@ -2645,17 +2647,19 @@ def kmv_plan(n: int, R: int, n_prog: int = 1):
2645
2647
 
2646
2648
 
2647
2649
  def kmv_group(m: int) -> int:
2648
- """Y tiles per rolled-loop body for m points of Y (#103): about sqrt(m / 128), in [4, 16].
2650
+ """Y tiles per rolled-loop body for m points of Y (#103): about sqrt(m / 128), in [4, 32].
2649
2651
 
2650
2652
  Each body sums its tiles in PSUM and adds that partial to a running sum
2651
2653
  in SBUF, so an output entry is a sum of `g` terms then of `nt / g`
2652
2654
  partials (nt = m / 128 Y tiles): its fp32 rounding error grows like
2653
2655
  g + nt / g, smallest at g = sqrt(nt). Bigger bodies also pay the loop's
2654
- per-trip overhead less often. At most 16 (SBUF for the body's K^T and V
2655
- tiles); at least 4, so a small problem (m < 3200) runs exactly the
2656
- summation, and gives exactly the bits, of the fixed 4-tile body before.
2657
- The group depends on m only, so row blocks, cores and LNC=1/2 all get
2658
- the same summation order.
2656
+ per-trip overhead less often. At most 32 (m >= 131072), the largest
2657
+ body whose K^T tiles, V rows and Matern scratch ring fit SBUF with
2658
+ NKI's own allocation for every kernel and R (#108; on Trn2 group 32
2659
+ gave +18% over 16 at R = 128); at least 4, so a small problem
2660
+ (m < 3200) runs exactly the summation, and gives exactly the bits, of
2661
+ the fixed 4-tile body before. The group depends on m only, so row
2662
+ blocks, cores and LNC=1/2 all get the same summation order.
2659
2663
  """
2660
2664
  nt = max(1, m // _TILE_M)
2661
2665
  return max(_KMV_GROUP, min(_KMV_AUTO_GROUP_MAX, math.isqrt(nt)))
@@ -2780,31 +2784,41 @@ if HAS_NKI:
2780
2784
  if rem:
2781
2785
  tile(r_lo + n_full * _TILE_M, rem)
2782
2786
 
2783
- def _kmv_entries(d_ps, prm, kind, jr, ni):
2784
- """Kernel tile (jr, ni) in SBUF from the squared-distance tile in PSUM."""
2785
- k = nl.ndarray((jr, ni), dtype=nl.float32, buffer=nl.sbuf)
2787
+ def _kmv_entries(d_ps, prm, kind, jr, ni, scratch):
2788
+ """Kernel tile (jr, ni) in SBUF from the squared-distance tile in PSUM.
2789
+
2790
+ `scratch`: `_KMV_SCRATCH[kind]` full-size SBUF tiles for the Matern
2791
+ intermediates, a slot of the body's ring (#108). Only the returned
2792
+ tile is allocated here: allocations live for their enclosing region
2793
+ (the loop body), so fresh intermediates per Y tile would grow the
2794
+ body's SBUF with its size, past 192 KiB at 24 Matern-5/2 tiles.
2795
+ """
2786
2796
  a = prm[0:jr, 0:1]
2787
2797
  log_s = prm[0:jr, 1:2]
2788
2798
  if kind == 0:
2799
+ k = nl.ndarray((jr, ni), dtype=nl.float32, buffer=nl.sbuf)
2789
2800
  # Cancellation can leave D slightly negative for close points;
2790
2801
  # exp of it errs by the same relative amount as D itself, so no
2791
2802
  # clamp (it would cost a VectorE pass over every tile).
2792
2803
  nisa.activation(k, nl.exp, d_ps, bias=log_s, scale=a)
2793
2804
  return k
2794
- t = nl.ndarray((jr, ni), dtype=nl.float32, buffer=nl.sbuf)
2805
+ t = scratch[0][0:jr, 0:ni]
2795
2806
  # max(a * D, 2^-110) on VectorE straight from PSUM: sqrt needs a
2796
2807
  # non-negative input, and ScalarE sqrt is accurate only above 2^-116
2797
2808
  # (sqrt(2^-110) = 2^-55 stands in for an exact zero distance).
2798
2809
  nisa.tensor_scalar(t, d_ps, op0=nl.multiply, operand0=a, op1=nl.maximum, operand1=2.0**-110)
2799
2810
  nisa.activation(t, nl.sqrt, t)
2800
- e = nl.ndarray((jr, ni), dtype=nl.float32, buffer=nl.sbuf)
2801
- nisa.activation(e, nl.exp, t, bias=log_s, scale=-1.0)
2802
2811
  if kind == 1:
2812
+ e = nl.ndarray((jr, ni), dtype=nl.float32, buffer=nl.sbuf)
2813
+ nisa.activation(e, nl.exp, t, bias=log_s, scale=-1.0)
2803
2814
  return e
2815
+ e = scratch[1][0:jr, 0:ni]
2816
+ nisa.activation(e, nl.exp, t, bias=log_s, scale=-1.0)
2817
+ k = nl.ndarray((jr, ni), dtype=nl.float32, buffer=nl.sbuf)
2804
2818
  if kind == 2:
2805
2819
  nisa.scalar_tensor_tensor(k, t, nl.add, 1.0, nl.multiply, e)
2806
2820
  return k
2807
- q = nl.ndarray((jr, ni), dtype=nl.float32, buffer=nl.sbuf)
2821
+ q = scratch[2][0:jr, 0:ni]
2808
2822
  nisa.tensor_scalar(q, t, op0=nl.multiply, operand0=1.0 / 3.0, op1=nl.add, operand1=1.0)
2809
2823
  nisa.tensor_tensor(q, q, t, op=nl.multiply)
2810
2824
  nisa.scalar_tensor_tensor(k, q, nl.add, 1.0, nl.multiply, e)
@@ -2881,6 +2895,16 @@ if HAS_NKI:
2881
2895
  nl.ndarray((_TILE_M, NI), dtype=nl.float32, buffer=nl.psum)
2882
2896
  for _ in range(min(n_dist, len(sizes)))
2883
2897
  ]
2898
+ # Matern intermediates: a ring of `_KMV_SCRATCH_RING` slots
2899
+ # shared by the body's Y tiles (#108), so the body's SBUF
2900
+ # does not grow with its size.
2901
+ scr = [
2902
+ [
2903
+ nl.ndarray((_TILE_M, NI), dtype=nl.float32, buffer=nl.sbuf)
2904
+ for _ in range(_KMV_SCRATCH[kind])
2905
+ ]
2906
+ for _ in range(min(_KMV_SCRATCH_RING, len(sizes)) if _KMV_SCRATCH[kind] else 0)
2907
+ ]
2884
2908
  # The body's Y coordinates in one DMA: (d + 2, sum(sizes)).
2885
2909
  y_all = nl.load(yt[:, nl.ds(j0, sum(sizes))])
2886
2910
  # One DMA when every tile is full and the rows fit the budget.
@@ -2897,7 +2921,7 @@ if HAS_NKI:
2897
2921
  yb = y_all[:, off : off + jr] # (d + 2, jr): stationary
2898
2922
  d_ps = ring[k % len(ring)][0:jr, 0:ni]
2899
2923
  nisa.nc_matmul(d_ps, yb, xb, accumulate=False) # D^T, Y on partitions
2900
- kt = _kmv_entries(d_ps, prm, kind, jr, ni)
2924
+ kt = _kmv_entries(d_ps, prm, kind, jr, ni, scr[k % len(scr)] if scr else None)
2901
2925
  vk = v_all[:, k, :] if full else nl.load(v[nl.ds(j, jr), p0 : p0 + pr])
2902
2926
  for ri, (r0, rr) in enumerate(rg):
2903
2927
  # out^T += V_j^T K^T; the body's first Y tile opens
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: trnsparse
3
- Version: 0.14.0
3
+ Version: 0.15.0
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