trnsparse 0.12.1__tar.gz → 0.14.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.
- {trnsparse-0.12.1 → trnsparse-0.14.0}/CHANGELOG.md +398 -0
- {trnsparse-0.12.1 → trnsparse-0.14.0}/CLAUDE.md +16 -10
- {trnsparse-0.12.1 → trnsparse-0.14.0}/PKG-INFO +46 -8
- {trnsparse-0.12.1 → trnsparse-0.14.0}/README.md +45 -7
- trnsparse-0.14.0/benchmarks/bench_kernel_matmul_trn2.py +175 -0
- trnsparse-0.14.0/benchmarks/bench_screened_tiles_trn2.py +151 -0
- trnsparse-0.14.0/docs/api.md +172 -0
- {trnsparse-0.12.1 → trnsparse-0.14.0}/docs/architecture.md +104 -18
- {trnsparse-0.12.1 → trnsparse-0.14.0}/docs/benchmarks.md +175 -4
- trnsparse-0.14.0/docs/index.md +48 -0
- {trnsparse-0.12.1 → trnsparse-0.14.0}/docs/installation.md +23 -2
- {trnsparse-0.12.1 → trnsparse-0.14.0}/docs/iterative_solvers.md +10 -1
- {trnsparse-0.12.1 → trnsparse-0.14.0}/docs/migration_scipy.md +7 -7
- {trnsparse-0.12.1 → trnsparse-0.14.0}/docs/quickstart.md +2 -2
- {trnsparse-0.12.1 → trnsparse-0.14.0}/docs/sparse_attention.md +29 -4
- {trnsparse-0.12.1 → trnsparse-0.14.0}/pyproject.toml +1 -1
- trnsparse-0.14.0/scripts/bench_eager_handle.py +144 -0
- trnsparse-0.14.0/tests/conftest.py +78 -0
- {trnsparse-0.12.1 → trnsparse-0.14.0}/tests/test_attention.py +52 -0
- trnsparse-0.14.0/tests/test_attn_gather.py +140 -0
- trnsparse-0.14.0/tests/test_conformance.py +1592 -0
- trnsparse-0.14.0/tests/test_formats.py +139 -0
- {trnsparse-0.12.1 → trnsparse-0.14.0}/tests/test_iterative.py +296 -0
- trnsparse-0.14.0/tests/test_kernel_dtype_guard.py +955 -0
- trnsparse-0.14.0/tests/test_kernel_matmul.py +600 -0
- trnsparse-0.14.0/tests/test_kernel_matmul_neuron.py +262 -0
- {trnsparse-0.12.1 → trnsparse-0.14.0}/tests/test_native_dispatch.py +471 -2
- {trnsparse-0.12.1 → trnsparse-0.14.0}/tests/test_nki_compile.py +132 -0
- trnsparse-0.14.0/tests/test_nki_kernel_matmul.py +358 -0
- {trnsparse-0.12.1 → trnsparse-0.14.0}/tests/test_nki_native.py +196 -0
- trnsparse-0.14.0/tests/test_nki_screened_spmm.py +163 -0
- {trnsparse-0.12.1 → trnsparse-0.14.0}/tests/test_nki_sim.py +101 -0
- {trnsparse-0.12.1 → trnsparse-0.14.0}/tests/test_nki_tail_audit.py +3 -2
- {trnsparse-0.12.1 → trnsparse-0.14.0}/tests/test_ops.py +122 -0
- {trnsparse-0.12.1 → trnsparse-0.14.0}/tests/test_screening.py +30 -0
- trnsparse-0.14.0/tests/test_trace_cache_safety.py +583 -0
- {trnsparse-0.12.1 → trnsparse-0.14.0}/trnsparse/__init__.py +6 -1
- {trnsparse-0.12.1 → trnsparse-0.14.0}/trnsparse/formats.py +27 -4
- {trnsparse-0.12.1 → trnsparse-0.14.0}/trnsparse/iterative.py +294 -56
- trnsparse-0.14.0/trnsparse/kernel_matmul.py +399 -0
- {trnsparse-0.12.1 → trnsparse-0.14.0}/trnsparse/multicore.py +4 -6
- {trnsparse-0.12.1 → trnsparse-0.14.0}/trnsparse/nki/_native.py +143 -1
- {trnsparse-0.12.1 → trnsparse-0.14.0}/trnsparse/nki/dispatch.py +1129 -92
- {trnsparse-0.12.1 → trnsparse-0.14.0}/trnsparse/nki/kernels.py +651 -14
- {trnsparse-0.12.1 → trnsparse-0.14.0}/trnsparse/ops.py +148 -16
- {trnsparse-0.12.1 → trnsparse-0.14.0}/trnsparse.egg-info/PKG-INFO +46 -8
- {trnsparse-0.12.1 → trnsparse-0.14.0}/trnsparse.egg-info/SOURCES.txt +10 -0
- trnsparse-0.12.1/docs/api.md +0 -83
- trnsparse-0.12.1/docs/index.md +0 -34
- trnsparse-0.12.1/tests/conftest.py +0 -20
- trnsparse-0.12.1/tests/test_attn_gather.py +0 -49
- trnsparse-0.12.1/tests/test_formats.py +0 -66
- trnsparse-0.12.1/tests/test_nki_screened_spmm.py +0 -97
- {trnsparse-0.12.1 → trnsparse-0.14.0}/.github/workflows/ci.yml +0 -0
- {trnsparse-0.12.1 → trnsparse-0.14.0}/.github/workflows/notify-umbrella.yml +0 -0
- {trnsparse-0.12.1 → trnsparse-0.14.0}/.github/workflows/publish.yml +0 -0
- {trnsparse-0.12.1 → trnsparse-0.14.0}/.gitignore +0 -0
- {trnsparse-0.12.1 → trnsparse-0.14.0}/.pre-commit-config.yaml +0 -0
- {trnsparse-0.12.1 → trnsparse-0.14.0}/CODE_OF_CONDUCT.md +0 -0
- {trnsparse-0.12.1 → trnsparse-0.14.0}/CONTRIBUTING.md +0 -0
- {trnsparse-0.12.1 → trnsparse-0.14.0}/LICENSE +0 -0
- {trnsparse-0.12.1 → trnsparse-0.14.0}/benchmarks/bench_bsr_spmm.py +0 -0
- {trnsparse-0.12.1 → trnsparse-0.14.0}/benchmarks/bench_cg_fused_trn2.py +0 -0
- {trnsparse-0.12.1 → trnsparse-0.14.0}/benchmarks/bench_iterative.py +0 -0
- {trnsparse-0.12.1 → trnsparse-0.14.0}/benchmarks/bench_multicore_trn2.py +0 -0
- {trnsparse-0.12.1 → trnsparse-0.14.0}/benchmarks/bench_multiproc_trn2.py +0 -0
- {trnsparse-0.12.1 → trnsparse-0.14.0}/benchmarks/bench_native_trn2.py +0 -0
- {trnsparse-0.12.1 → trnsparse-0.14.0}/benchmarks/bench_power_fused_trn2.py +0 -0
- {trnsparse-0.12.1 → trnsparse-0.14.0}/benchmarks/bench_screening.py +0 -0
- {trnsparse-0.12.1 → trnsparse-0.14.0}/benchmarks/bench_spmm.py +0 -0
- {trnsparse-0.12.1 → trnsparse-0.14.0}/benchmarks/bench_spmv.py +0 -0
- {trnsparse-0.12.1 → trnsparse-0.14.0}/benchmarks/conftest.py +0 -0
- {trnsparse-0.12.1 → trnsparse-0.14.0}/docs/aws_setup.md +0 -0
- {trnsparse-0.12.1 → trnsparse-0.14.0}/examples/block_sparse_attention.py +0 -0
- {trnsparse-0.12.1 → trnsparse-0.14.0}/examples/sparse_fock.py +0 -0
- {trnsparse-0.12.1 → trnsparse-0.14.0}/infra/terraform/.terraform.lock.hcl +0 -0
- {trnsparse-0.12.1 → trnsparse-0.14.0}/infra/terraform/README.md +0 -0
- {trnsparse-0.12.1 → trnsparse-0.14.0}/infra/terraform/main.tf +0 -0
- {trnsparse-0.12.1 → trnsparse-0.14.0}/mkdocs.yml +0 -0
- {trnsparse-0.12.1 → trnsparse-0.14.0}/scripts/bench_to_md.py +0 -0
- {trnsparse-0.12.1 → trnsparse-0.14.0}/scripts/run_benchmarks.sh +0 -0
- {trnsparse-0.12.1 → trnsparse-0.14.0}/scripts/run_neuron_tests.sh +0 -0
- {trnsparse-0.12.1 → trnsparse-0.14.0}/scripts/run_simulator_tests.sh +0 -0
- {trnsparse-0.12.1 → trnsparse-0.14.0}/setup.cfg +0 -0
- {trnsparse-0.12.1 → trnsparse-0.14.0}/tests/test_bsr.py +0 -0
- {trnsparse-0.12.1 → trnsparse-0.14.0}/tests/test_core_split.py +0 -0
- {trnsparse-0.12.1 → trnsparse-0.14.0}/tests/test_examples.py +0 -0
- {trnsparse-0.12.1 → trnsparse-0.14.0}/tests/test_multicore.py +0 -0
- {trnsparse-0.12.1 → trnsparse-0.14.0}/tests/test_nki_attn.py +0 -0
- {trnsparse-0.12.1 → trnsparse-0.14.0}/tests/test_nki_bsr.py +0 -0
- {trnsparse-0.12.1 → trnsparse-0.14.0}/tests/test_nki_cg_fused.py +0 -0
- {trnsparse-0.12.1 → trnsparse-0.14.0}/tests/test_nki_spmm.py +0 -0
- {trnsparse-0.12.1 → trnsparse-0.14.0}/trnsparse/nki/__init__.py +0 -0
- {trnsparse-0.12.1 → trnsparse-0.14.0}/trnsparse/nki/_native_attn.py +0 -0
- {trnsparse-0.12.1 → trnsparse-0.14.0}/trnsparse/screening.py +0 -0
- {trnsparse-0.12.1 → trnsparse-0.14.0}/trnsparse.egg-info/dependency_links.txt +0 -0
- {trnsparse-0.12.1 → trnsparse-0.14.0}/trnsparse.egg-info/requires.txt +0 -0
- {trnsparse-0.12.1 → trnsparse-0.14.0}/trnsparse.egg-info/top_level.txt +0 -0
|
@@ -7,6 +7,404 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [0.14.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` / `KernelOperator`, a fused kernel-matrix product for matrix-free GP and kernel methods (#93, #102, #103), cached eager NKI handles, and the trnsci float64 policy with a dtype guard at the kernel boundary; `cg_bsr` and `power_iteration_bsr` gradients and `from_scipy` dtypes fixed.
|
|
15
|
+
|
|
16
|
+
### Performance
|
|
17
|
+
|
|
18
|
+
- **Eager NKI kernel calls skip the per-call handle rebuild and the custom-op
|
|
19
|
+
layer (same fix as trnblas#92).** On the native path a `trnsparse::`
|
|
20
|
+
custom-op call cost about 0.1 ms of host time before the kernel was queued,
|
|
21
|
+
against 0.016 ms for aten. Most of it went to nki's `Kernel.__call__`,
|
|
22
|
+
which rebuilds the torch handle on every call, plus the `torch.library`
|
|
23
|
+
layer. `dispatch._run` / `_launch` and the direct keyed-op calls (keyed
|
|
24
|
+
`bsr_spmm`, attention forward and backward, fused CG and power iteration,
|
|
25
|
+
`ShardedBSR` / `ShardedAttention`) now call a `torch_neuronx.wrap_nki`
|
|
26
|
+
handle built once per kernel, with the op body's `int()` / `tuple()` /
|
|
27
|
+
`float()` conversions of the static arguments. On trn2 (LNC=2), a
|
|
28
|
+
256x256x128 `spmm_dense` launch drops from 0.126 to 0.036 ms of issue time
|
|
29
|
+
(e2e 0.201 to 0.124 ms), a keyed `bsr_spmm` from 0.255 to 0.157 ms e2e, and
|
|
30
|
+
`cg_bsr_fused` n=256 from 1.36 to 1.19 ms. Results are bit-identical.
|
|
31
|
+
`torch.compile`, requires-grad, int64 and tensor-subclass inputs, and the
|
|
32
|
+
composite `bsr_attn` / `bsr_attn_bwd` ops keep the custom op, as does
|
|
33
|
+
the screened-tile op (`screened_tiles_keyed`, #61) for now. This is the
|
|
34
|
+
same handle cache that `kernel_matmul` launches use (keyed there by
|
|
35
|
+
operand shapes, one handle shared by every row block and core), so
|
|
36
|
+
`TRNSPARSE_NKI_EAGER_HANDLE=0` turns both off;
|
|
37
|
+
`scripts/bench_eager_handle.py` measures it.
|
|
38
|
+
|
|
39
|
+
### Added
|
|
40
|
+
|
|
41
|
+
- **`kernel_matmul` / `KernelOperator`: fused kernel-matrix product
|
|
42
|
+
K(X, Y) @ V that never forms K** (#93, for trnsci/trnsci#109). RBF and
|
|
43
|
+
Matern-1/2, 3/2, 5/2; per-dimension lengthscales; `noise * V` on the
|
|
44
|
+
square operator. One NKI launch per application: squared distances as a
|
|
45
|
+
TensorEngine matmul on augmented coordinates into PSUM, the kernel
|
|
46
|
+
function on ScalarE straight from PSUM, the product with V on the
|
|
47
|
+
TensorEngine. Coordinates are augmented and transposed on chip
|
|
48
|
+
(`nl.load` + `nisa.nc_transpose`); tile loops are rolled
|
|
49
|
+
(`nl.fori_loop`), so trace and compile time do not grow with n; ragged
|
|
50
|
+
n, m and R are handled in the kernel. Both physical cores at LNC=2 with
|
|
51
|
+
the same control flow on each, and bit-identical results at LNC=1 and
|
|
52
|
+
LNC=2. Hyperparameters are a tensor argument (aws-neuron-sdk#1454).
|
|
53
|
+
Eager launches use a cached `torch_neuronx.wrap_nki` handle (the
|
|
54
|
+
trnblas#92 pattern). CPU tests: PyTorch path against float64
|
|
55
|
+
`torch.cdist`; `nki.simulate` at LNC=1/2, MLIR verify and (with
|
|
56
|
+
neuronx-cc) NEFF compile with NKI allocation at LNC=1/2.
|
|
57
|
+
`benchmarks/bench_kernel_matmul_trn2.py` measures kernel entries/s
|
|
58
|
+
against aten on the device and the host CPU at 2 threads and all vCPUs.
|
|
59
|
+
`tile_mask` is reserved and raises for now.
|
|
60
|
+
- **`kernel_matmul`: row blocks spread across all visible logical
|
|
61
|
+
NeuronCores** (#102). On trn2.3xlarge at LNC=2 (`neuron:0..3`, with
|
|
62
|
+
`NEURON_RT_VISIBLE_CORES=0-3`) a row-blocked product issues its blocks
|
|
63
|
+
round-robin over the cores. The block count rounds up to a multiple of
|
|
64
|
+
the core count (`kmv_block_rows(..., parts=k)`; 20 blocks of 53248 rows
|
|
65
|
+
on 4 cores at n = m = 2^20), so each core gets the same number. Y, V and
|
|
66
|
+
params are staged through the host once and placed on each core before
|
|
67
|
+
any kernel is queued. Launches are issued asynchronously from the calling
|
|
68
|
+
thread (NKI tracing stays on one thread, aws-neuron-sdk#1453) and
|
|
69
|
+
gathered in row order on the operands' device, bit-identical to one
|
|
70
|
+
core. `TRNSPARSE_KMV_CORES`: `auto` (default: every visible core when
|
|
71
|
+
`n * m` exceeds the 6e10-entry block budget, else one), `all`, or an
|
|
72
|
+
int. `TRNSPARSE_KMV_THREADS=1` is a fallback that issues each core's
|
|
73
|
+
blocks from a worker thread, after the first block per core has been
|
|
74
|
+
launched on the calling thread. Target: n = 2^20 from about 42 s to about
|
|
75
|
+
11 s per apply on 4 cores. CPU stand-in tests cover block-to-core
|
|
76
|
+
assignment, per-core placement and the gather (bitwise against single
|
|
77
|
+
core). A neuron test compares multi-core with single-core bitwise and
|
|
78
|
+
skips below 2 visible cores.
|
|
79
|
+
|
|
80
|
+
### Changed
|
|
81
|
+
|
|
82
|
+
- **`kernel_matmul`: about 70% fewer instructions per launch** (#103). The
|
|
83
|
+
Trn2 profile on #93 showed the kernel issue-bound: 41% of a launch with
|
|
84
|
+
no engine active. The @V product now takes V's tile as the stationary
|
|
85
|
+
operand and the kernel tile as the moving one. That makes one
|
|
86
|
+
`nc_matmul` per 128 columns of V over all 512 X points of a tile, where
|
|
87
|
+
it was four (one per 128-row chunk, kernel tile stationary). The
|
|
88
|
+
accumulator, out^T, is now one PSUM bank per 128 columns, against four,
|
|
89
|
+
and is transposed back (`nisa.nc_transpose`) once per output block.
|
|
90
|
+
The freed PSUM holds a ring of distance tiles, so a loop body is no
|
|
91
|
+
longer capped at 4 Y tiles. Each body loads its V rows in one DMA,
|
|
92
|
+
against one per Y tile. The body size now defaults to `kmv_group(m)`:
|
|
93
|
+
about sqrt(m / 128) Y tiles, from 4 to 16 (`TRNSPARSE_KMV_GROUP` still
|
|
94
|
+
overrides it, 1 to 32).
|
|
95
|
+
- Instructions per launch, n = 131072, LNC=2, both physical cores,
|
|
96
|
+
counted from the compiled NEFF: **11.5M before, 3.5M now** at group
|
|
97
|
+
16; 6.2M with the old body of 4. That is 44 against 13 instructions per
|
|
98
|
+
128 x 512 tile per core.
|
|
99
|
+
- **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.
|
|
105
|
+
- **Error against float64** goes from about g + m/(128 g) fp32 roundings
|
|
106
|
+
to its minimum: at m = 131072 the max relative error drops from
|
|
107
|
+
9.0e-7 to 5.2e-7 (simulator, rbf and Matern-5/2, R = 1 and 33).
|
|
108
|
+
- Bit-identical at LNC=1 and LNC=2, across row blocks and across cores,
|
|
109
|
+
as before: the body size depends on m only.
|
|
110
|
+
- The simulator tests now run `nc_matmul` as a sum in contraction order
|
|
111
|
+
(numpy's BLAS rounds a column differently with the moving operand's
|
|
112
|
+
width). So the LNC=1 against LNC=2 and row-blocked checks are bitwise
|
|
113
|
+
at every d and every ragged tail. The ragged tails were tolerance-only
|
|
114
|
+
before.
|
|
115
|
+
- `benchmarks/bench_kernel_matmul_trn2.py --dump DIR` saves the results,
|
|
116
|
+
to compare two checkouts with `--compare`.
|
|
117
|
+
- **float64 under `auto` now computes in torch at float64 accuracy
|
|
118
|
+
(previously cast to fp32 on the NKI path)** — the trnsci suite fp64
|
|
119
|
+
policy (#84). Trainium computes float64 at fp32 precision, so no kernel
|
|
120
|
+
can give a float64-accurate result. For `spmm`, `bsr_spmm`,
|
|
121
|
+
`screened_spmm`, `block_sparse_attention_tiled` (forward and backward),
|
|
122
|
+
`cg_bsr` (through `bsr_spmm`), `cg_bsr_fused` and
|
|
123
|
+
`power_iteration_bsr_fused`, a float64 value operand (and complex128 in
|
|
124
|
+
`bsr_spmm`, whose complex path runs as real kernel launches):
|
|
125
|
+
- under `set_backend("auto")` (the default) takes the PyTorch path that
|
|
126
|
+
`set_backend("pytorch")` takes, at float64 accuracy, and no kernel runs
|
|
127
|
+
(the fused solvers take their host path);
|
|
128
|
+
- under an explicit `set_backend("nki")` is cast to fp32 (complex64) for
|
|
129
|
+
the kernel as before, now with a one-time `UserWarning` per op saying
|
|
130
|
+
precision is reduced to fp32;
|
|
131
|
+
- under `TRNSPARSE_REQUIRE_NKI=1` raises `NotImplementedError` (on either
|
|
132
|
+
backend; this takes precedence over the explicit-nki cast).
|
|
133
|
+
|
|
134
|
+
The result dtype always equals the pytorch backend's for the same inputs,
|
|
135
|
+
including bf16 / fp16 and mixed-dtype promotion; a kernel result computed
|
|
136
|
+
in fp32 is cast to that dtype. Callers of 0.2.0 through 0.13.0 who
|
|
137
|
+
relied on float64 inputs running on the kernels under `auto` now get
|
|
138
|
+
torch (more precise; on the host or device aten instead of the kernel):
|
|
139
|
+
pass float32 operands, or select `set_backend("nki")`, to keep the
|
|
140
|
+
kernel. float64 validation runs under REQUIRE_NKI must pass fp32 operands
|
|
141
|
+
(or call the autograd Functions directly, as the hardware gradchecks do).
|
|
142
|
+
Lands in 0.14.0. `screened_spmm` decides on `A` and `B`; a float64 bound
|
|
143
|
+
vector `diag_integrals` alone does not change the route. See
|
|
144
|
+
docs/architecture.md, "Precision and dtypes".
|
|
145
|
+
|
|
146
|
+
**float64 on the `neuron` device runs on the host under `auto`.** Device
|
|
147
|
+
aten computes float64 at fp32 precision as well (measured on Trn2), so
|
|
148
|
+
the torch path alone does not give float64 accuracy for device-resident
|
|
149
|
+
data. When a float64 / complex128 operand of `spmv`, `spmm`,
|
|
150
|
+
`spmv_symmetric`, `bsr_spmm`, `screened_spmm`,
|
|
151
|
+
`block_sparse_attention_tiled` (forward and backward), `cg_bsr`,
|
|
152
|
+
`chebyshev_bsr`, `richardson_bsr`, `power_iteration_bsr` or the
|
|
153
|
+
`jacobi_preconditioner_bsr` preconditioner lives on a `neuron` device
|
|
154
|
+
(dense, or a CSR / COO / BSR matrix's values), the operands are copied to
|
|
155
|
+
the host, host torch computes at float64, and the result returns to the
|
|
156
|
+
dense operand's device in float64, with one `UserWarning` per op ("float64
|
|
157
|
+
on the neuron device is computed on the host for float64 accuracy (device
|
|
158
|
+
arithmetic is fp32); pass float32 to stay on device").
|
|
159
|
+
`TRNSPARSE_FP64_ON_DEVICE=1` keeps device aten (fp32 precision, no
|
|
160
|
+
copies). Host-resident float64, explicit `nki` and the pytorch backend
|
|
161
|
+
are unchanged; the fused solvers already ran float64 on the host.
|
|
162
|
+
**`TRNSPARSE_REQUIRE_NKI=1` raises for float64 / complex128 on the neuron
|
|
163
|
+
device** at every public op (backends `auto` and `nki`, also with
|
|
164
|
+
`TRNSPARSE_FP64_ON_DEVICE=1`), before any host copy or kernel. Before, the
|
|
165
|
+
routes with no kernel (CSR `spmv`, `spmv_symmetric`, `spmm`, the Jacobi
|
|
166
|
+
preconditioner) ran device aten at fp32 precision with no error under
|
|
167
|
+
REQUIRE_NKI and `auto` (Trn2 conformance: `test_spmv[float64-*]` rel err
|
|
168
|
+
1e-8..2e-7 against the 1e-10 tolerance).
|
|
169
|
+
|
|
170
|
+
`kernel_matmul` / `KernelOperator` (#93, new in this release) follow the
|
|
171
|
+
same rules through `dispatch._fp64_route`: under REQUIRE_NKI float64 now
|
|
172
|
+
raises `NotImplementedError` (it raised `TypeError`), and the explicit-nki
|
|
173
|
+
fp32 warning fires once per op (it fired on every call). Integer, bool and
|
|
174
|
+
complex inputs on the NKI path take the PyTorch path, as the pytorch
|
|
175
|
+
backend does (they raised `TypeError`), or raise `NotImplementedError`
|
|
176
|
+
under REQUIRE_NKI. Device-resident float64 under `auto`: `kernel_matmul`
|
|
177
|
+
is guarded like the other ops, and a float64 `KernelOperator` bound for
|
|
178
|
+
the neuron device is built and applied on the host (one `UserWarning`),
|
|
179
|
+
returning each product on V's device.
|
|
180
|
+
|
|
181
|
+
### Fixed
|
|
182
|
+
|
|
183
|
+
- **No NKI kernel is reached with an operand dtype it does not take** (one
|
|
184
|
+
guard at the kernel boundary; trnsci/trnsci#35, the class of #78 and #79).
|
|
185
|
+
At each entry (`spmm`, `bsr_spmm`, `screened_spmm`,
|
|
186
|
+
`block_sparse_attention_tiled` forward and backward), integer and bool
|
|
187
|
+
values, and complex values except in `bsr_spmm` (real kernel launches,
|
|
188
|
+
#78), now take the PyTorch path that `set_backend("pytorch")` takes, or
|
|
189
|
+
raise `NotImplementedError` under `TRNSPARSE_REQUIRE_NKI`. Before, they went
|
|
190
|
+
into the fp32/bf16 kernels on the NKI and auto backends: complex operands of
|
|
191
|
+
`spmm` / `screened_spmm` reached the kernel unchanged, the static attention
|
|
192
|
+
kernels cast complex Q/K/V to fp32 (imaginary part dropped), the static
|
|
193
|
+
`bsr_spmm` and attention kernels cast integers to fp32 and back, and
|
|
194
|
+
REQUIRE_NKI failed with whatever error the kernel raised. float64 follows
|
|
195
|
+
the fp64 policy below (#84); fp32, bf16 and fp16 are unchanged. Every
|
|
196
|
+
launch now also checks a per-kernel, per-operand dtype table itself
|
|
197
|
+
(`_KERNEL_OPERAND_DTYPES`: the native op table, `_run` on the XLA path and
|
|
198
|
+
the simulator) and raises `TypeError` naming the op and dtypes, so a future
|
|
199
|
+
wrapper without the entry check cannot hand a kernel an unsupported dtype.
|
|
200
|
+
The eager-handle fast path (`_native_call`, and `_kmv_launch` for
|
|
201
|
+
`kernel_matmul`, whose kernels `kmv_self` / `kmv_cross` / `kmv_selfblk`
|
|
202
|
+
are now in the table as fp32-only) skips the guarded op body, so it checks
|
|
203
|
+
the table itself before the launch.
|
|
204
|
+
Affects every release with NKI kernels, through 0.13.0.
|
|
205
|
+
|
|
206
|
+
- **`spmm` with a 1-D `B` raised on the NKI path (#105).** Affected
|
|
207
|
+
0.2.0–0.13.0: under `set_backend("nki")` / `"auto"` on a Neuron host,
|
|
208
|
+
`spmm(A, b)` with a vector `b` failed in the NKI GEMM wrapper with
|
|
209
|
+
`ValueError: not enough values to unpack (expected 2, got 1)`, while the
|
|
210
|
+
pytorch backend returns `A @ b` of shape `(m,)`. The vector now goes to the
|
|
211
|
+
kernel as one column and the result is squeezed back. Matrix `B` is
|
|
212
|
+
unchanged.
|
|
213
|
+
|
|
214
|
+
- **`kernel_matmul` no longer hits the Neuron runtime's 30 s execution
|
|
215
|
+
timeout at n >= 1e6** (#100). One launch at n = 2^20, R = 33 takes about
|
|
216
|
+
40 s at LNC=2 and 80 s at LNC=1 on Trn2 and failed with `NRT status 5`.
|
|
217
|
+
Products above 6e10 kernel entries (`n * m`; the rate is flat in R, so
|
|
218
|
+
the budget is not weighted by d + R) now run as host-side row blocks of
|
|
219
|
+
X: the fewest launches the budget allows, split into equal blocks of a
|
|
220
|
+
multiple of 1024 rows (19 launches of 55296 rows at n = m = 2^20, about
|
|
221
|
+
2.2 s each at LNC=2 and 4.5 s at LNC=1), R kept whole. Large blocks keep
|
|
222
|
+
the fixed per-launch cost (~75 ms, staging Y) near 1% of the run; Y, V
|
|
223
|
+
and params are placed on the device once for all blocks. The ragged last
|
|
224
|
+
block is zero-padded (less than 1024 rows per block), so every launch has
|
|
225
|
+
one shape, one compile and one cached `wrap_nki` handle (now keyed per
|
|
226
|
+
kernel and shape). Blocks of the square operator use a new
|
|
227
|
+
`_kmv_selfblk_kernel` (custom op `trnsparse::kmv_selfblk`) that adds
|
|
228
|
+
`noise * V` for the block's rows in the same fused epilogue, so blocked
|
|
229
|
+
results are bit-identical to one launch. Below the budget nothing
|
|
230
|
+
changes. `TRNSPARSE_KMV_BLOCK_WORK` overrides the budget.
|
|
231
|
+
|
|
232
|
+
- **`cg_bsr` raised `UnboundLocalError` with `max_iter=0` or on a first-step
|
|
233
|
+
breakdown, and reported `iters=max_iter` on any breakdown** (#71, #86).
|
|
234
|
+
`r_norm` was only assigned inside the loop, so `max_iter=0`, `A p = 0`
|
|
235
|
+
(e.g. a zero matrix) or `p . A p = 0` at k=0 (an exact `x0` with `tol=0`)
|
|
236
|
+
raised. A later breakdown (`|p . A p| < 1e-30`, e.g. after exact
|
|
237
|
+
convergence with `tol=0`) returned `iters = max_iter` (500 or 1000 for a
|
|
238
|
+
1-2 step solve). `cg_bsr` now returns the number of steps actually taken
|
|
239
|
+
and the residual of the returned `x`; `max_iter=0` gives
|
|
240
|
+
`(x0, 0, ||b - A x0|| / ||b||)`. Solutions are unchanged. Affects 0.3.2
|
|
241
|
+
through 0.13.0.
|
|
242
|
+
- **`power_iteration_bsr` gave silently wrong gradients** (#96), as
|
|
243
|
+
`cg_bsr` did (#70). The norms were taken with `.item()`, so autograd
|
|
244
|
+
differentiated the iterations with frozen normalisation instead of the
|
|
245
|
+
eigenvector, and `v0` received a spurious gradient. The eigenvector now
|
|
246
|
+
differentiates as the exact unit eigenvector of `A` (implicit
|
|
247
|
+
differentiation of `A v = lam v`, `||v|| = 1`): backward solves a dense
|
|
248
|
+
bordered `(N+1) x (N+1)` system, so it costs O(N^3), and gives
|
|
249
|
+
`grad_A = -w v^T` on the stored blocks. `v0` gets no gradient, the
|
|
250
|
+
eigenvalue stays a Python float, and backward raises `NotImplementedError`
|
|
251
|
+
for complex `A`. Forward values are unchanged. Affects 0.3.2 through 0.13.0.
|
|
252
|
+
- **`from_scipy` downcast float64 and integer matrices to float32** (silent,
|
|
253
|
+
#69). Every real scipy matrix was stored as float32 (float64 rounded to
|
|
254
|
+
~3e-8 relative error; int64 exact only up to 2^24) and complex128 as
|
|
255
|
+
complex64, which contradicted the "zero-copy" description in
|
|
256
|
+
docs/migration_scipy.md. The values now keep the scipy dtype, and for CSR
|
|
257
|
+
input share memory with `sp_matrix.data`. Affects 0.1.1 through 0.13.0.
|
|
258
|
+
**Behaviour change:** float64 scipy input now gives a float64 matrix, so
|
|
259
|
+
mixing it with float32 vectors (`spmv(from_scipy(M64), x32)`) raises the
|
|
260
|
+
usual torch dtype-mismatch error; cast one side (`M.astype(np.float32)`).
|
|
261
|
+
docs/migration_scipy.md no longer refers to a nonexistent `.to_scipy()`.
|
|
262
|
+
- **`cg_bsr` gave silently wrong gradients** (#70). The CG step sizes were
|
|
263
|
+
taken with `.item()`, so autograd differentiated a frozen-coefficient
|
|
264
|
+
polynomial in `A` applied to `b`, not the solve: with `b` along an
|
|
265
|
+
eigenvector (one-step convergence) `b.grad` was ~20% off `A^{-1} g`, and
|
|
266
|
+
`A.blocks.grad` had the same problem. `cg_bsr` now differentiates as the
|
|
267
|
+
exact solve (implicit differentiation, like `torch.linalg.solve`):
|
|
268
|
+
backward runs a second CG solve `A lam = grad_x` with the same
|
|
269
|
+
`tol` / `max_iter` / `M`, giving `grad_b = lam` and
|
|
270
|
+
`grad_A = -lam x^H` on the stored blocks. `x0` and `M` no longer receive
|
|
271
|
+
gradients. Forward values are unchanged. Affects 0.3.2 through 0.13.0.
|
|
272
|
+
- **`cg_bsr_fused` / `power_iteration_bsr_fused` gave wrong results for
|
|
273
|
+
complex, integer and bool inputs** (silent, #85). Both pack A and the
|
|
274
|
+
vectors into real float tiles, so on every backend (including
|
|
275
|
+
`set_backend("pytorch")`) a complex A or b lost its imaginary part, and an
|
|
276
|
+
integer or bool b gave a truncated x of b's dtype. They (and
|
|
277
|
+
`prepare_cg_bsr`) now raise `TypeError` for a non-real-float `A.blocks`,
|
|
278
|
+
`b`, `x0` or `v0`, before any backend runs. Affects 0.11.0 through 0.13.0.
|
|
279
|
+
|
|
280
|
+
## [0.13.0] — 2026-11-02
|
|
281
|
+
|
|
282
|
+
Validated on Trn2 (trn2.3xlarge, Neuron SDK 2.33.0, native TorchNeuron) — see trnsci/trnsci#35.
|
|
283
|
+
|
|
284
|
+
Headline: **correctness fixes — upgrade recommended.** `COOMatrix.to_dense` dropped duplicate entries (#56); `from_scipy` dropped the imaginary part (#58); attention second derivatives were wrong (#65); `spmv`/`spmm` accepted a broadcasting y/C (#67); `chebyshev_coeffs` was not the Chebyshev polynomial for K ≥ 2 (#73); `bsr_diagonal` padded tall matrices (#74); on device, complex `bsr_spmm` dropped the imaginary part (#78), integer `spmv`/`spmm` returned float32 (#79), and an undersized attention mask was accepted (#82). New: bf16 and head_dim > 128 gather attention (#49), and Schwarz-screened tile skipping that beats dense aten for large screened products (#89).
|
|
285
|
+
|
|
286
|
+
### Added
|
|
287
|
+
|
|
288
|
+
- **`screened_spmm` skips screened-out tiles (#61).** On the native
|
|
289
|
+
TorchNeuron path the host classifies each 128 x 128 tile of `A` from the
|
|
290
|
+
Schwarz bounds alone (skip / full / partial, exact in fp32) and one keyed
|
|
291
|
+
kernel loads, transposes and multiplies only the kept tiles; partial
|
|
292
|
+
tiles get the element mask on chip. `A` stays dense on the device and
|
|
293
|
+
nothing is gathered. The plan (classification, key, device bounds) is
|
|
294
|
+
cached while the same bound tensor is reused. New keyword
|
|
295
|
+
`symmetric=True` (for `A == A.T`, as for the pair-index ERI matrix) skips
|
|
296
|
+
the Tensor Engine transposes. On Trn2 (LNC=2) at n = 8192, N = 512 it is
|
|
297
|
+
faster than dense aten `A @ B` up to 80% of tiles kept in fp32 (5.9x at
|
|
298
|
+
5%) and up to ~65% in bf16; at n = 4096 up to ~75% (fp32) / not in bf16;
|
|
299
|
+
at n <= 2048 the ~0.25 ms launch floor loses to aten. It beats
|
|
300
|
+
`(A * mask) @ B` and the previous fused kernel at every density for
|
|
301
|
+
n >= 4096. Results are in
|
|
302
|
+
docs/benchmarks.md. `TRNSPARSE_SCREEN_SKIP_TILES=0` restores the
|
|
303
|
+
all-tiles fused kernel. `trnsparse.nki.dispatch.screened_tile_structure`
|
|
304
|
+
exposes the classification.
|
|
305
|
+
|
|
306
|
+
- **Guard against the NKI trace-cache closure bug ([aws-neuron/aws-neuron-sdk#1454](https://github.com/aws-neuron/aws-neuron-sdk/issues/1454),
|
|
307
|
+
trnsci/trnsci#35).** `tests/test_trace_cache_safety.py` (CPU) finds every
|
|
308
|
+
`@nki.jit` kernel and fails if its body, or a helper it calls, reads a
|
|
309
|
+
closure variable or a module global that is not a constant expression
|
|
310
|
+
(reviewed exceptions are allow-listed with a reason), and checks each
|
|
311
|
+
kernel's `__closure__` at runtime against a stub `nki`.
|
|
312
|
+
- Tests: `conftest.py` no longer turns the NKI trace cache off. Neuron tests
|
|
313
|
+
run with it on, as under `import torch_neuronx`, with a fresh
|
|
314
|
+
`NKI_TRACE_CACHE_URL` directory per session (no stale editable-install
|
|
315
|
+
traces). `NKI_ENABLE_TRACE_CACHE=0` still turns it off.
|
|
316
|
+
- README: known platform issue note for #1454.
|
|
317
|
+
|
|
318
|
+
### Fixed
|
|
319
|
+
|
|
320
|
+
- `screened_spmm` with a 1-D `B` on the NKI backend raised `ValueError`
|
|
321
|
+
(pre-existing on main; found by the conformance suite on Trn2 during #61).
|
|
322
|
+
|
|
323
|
+
- **`bsr_spmm` dropped the imaginary part of complex operands on the NKI
|
|
324
|
+
backend** (silent, #78). The static-structure kernel path cast complex
|
|
325
|
+
blocks and `B` to fp32 and the result back to complex, so the imaginary
|
|
326
|
+
part was zero and the value was wrong. Affects 0.8.0 through 0.12.1 on
|
|
327
|
+
Trainium with `block_size=128`. Complex operands now run as real kernel
|
|
328
|
+
launches: one per real part of A, against `[Re B | Im B]`. Autograd works
|
|
329
|
+
through them. Found by the conformance suite on Trn2 (trnsci/trnsci#35).
|
|
330
|
+
- **`spmv` / `spmm` returned float32 for integer operands on the device**
|
|
331
|
+
(silent dtype change, #79). The default `alpha=1.0` (and `beta=1.0`) were
|
|
332
|
+
applied as `alpha * product`. A Python float times an integer tensor
|
|
333
|
+
promotes, so int32/int64 products on a `neuron` operand came back float32.
|
|
334
|
+
Affects every release with the device dense CSR path, through 0.12.1. A
|
|
335
|
+
scale of 1 is now skipped. Found by the conformance suite on Trn2
|
|
336
|
+
- **`block_sparse_attention_tiled` accepted a mask smaller than `seq_len` on
|
|
337
|
+
the NKI path** (silent, #82). With a `(256, 256)` mask and `seq_len = 384`,
|
|
338
|
+
the native `bsr_attn` op returned output rows 256..383 that no mask block
|
|
339
|
+
covers (garbage values, no error). The PyTorch path raised an opaque
|
|
340
|
+
`IndexError`. Every backend now raises `ValueError` unless
|
|
341
|
+
`mask_bsr.shape == (Q rows, K rows)` and K and V have the same length.
|
|
342
|
+
Affects 0.10.0 through 0.12.1. Found by the round-3 device harness on Trn2
|
|
343
|
+
(trnsci/trnsci#35).
|
|
344
|
+
- **`bsr_diagonal` returned length m for a tall matrix** (silent; every
|
|
345
|
+
release 0.3.2–0.12.1, every backend). For a rectangular (m, n) BSR matrix
|
|
346
|
+
with m > n the result was zero-padded to length m; it now has length
|
|
347
|
+
min(m, n), as `torch.diagonal` and scipy do. Square and wide matrices are
|
|
348
|
+
unchanged (#74).
|
|
349
|
+
- **`COOMatrix.to_dense` dropped duplicate entries** (silent; every release
|
|
350
|
+
0.1.1–0.12.1, backend-independent). Repeated `(row, col)` entries were
|
|
351
|
+
assigned rather than summed, so all but one was lost, while `to_csr()`,
|
|
352
|
+
`torch.sparse_coo_tensor` and `scipy.sparse` sum them. This also affected
|
|
353
|
+
`CSRMatrix.to_coo().to_dense()` for a CSR with a repeated column in a row.
|
|
354
|
+
Duplicates are now summed; matrices without duplicates are unchanged (#56).
|
|
355
|
+
- **`chebyshev_coeffs` / `chebyshev_bsr` did not produce the Chebyshev
|
|
356
|
+
polynomial for K >= 2** (silent; every release 0.4.3–0.12.1, every
|
|
357
|
+
backend, host-side coefficients). The conversion from the Templates
|
|
358
|
+
listing used beta_1 = (c alpha_0 / 2)^2 instead of the special-case
|
|
359
|
+
½(c alpha_0)^2, and every later coefficient inherited the error, so the
|
|
360
|
+
worst-case residual after K steps was up to ~3x the Chebyshev bound
|
|
361
|
+
(K = 7). The coefficients now follow Saad's three-term recurrence
|
|
362
|
+
(Alg. 12.1) and the residual is T_K((d - lam)/c) / T_K(d/c) (#73).
|
|
363
|
+
|
|
364
|
+
- **`from_scipy` discarded the imaginary part of complex matrices** (silent;
|
|
365
|
+
every release 0.1.1–0.12.1). Values were always cast to float32, so a
|
|
366
|
+
complex scipy matrix became its real part (one C++ `UserWarning`, no
|
|
367
|
+
error). Complex input now gives `complex64` values; real input is
|
|
368
|
+
unchanged (float32) (#58).
|
|
369
|
+
|
|
370
|
+
- **Second derivatives of `block_sparse_attention_tiled` were wrong**
|
|
371
|
+
(silent; 0.6.0–0.12.1, the releases with attention autograd, on the
|
|
372
|
+
PyTorch backend and the PyTorch/XLA / simulator paths). The backward
|
|
373
|
+
reused the forward's softmax statistics as constants, so differentiating
|
|
374
|
+
it (`create_graph=True`: `gradgradcheck`, gradient penalties, Hessian-
|
|
375
|
+
vector products) dropped their dependence on Q and K, with no error.
|
|
376
|
+
Double backward now differentiates a graph-carrying recomputation; first-
|
|
377
|
+
order gradients are unchanged (#65).
|
|
378
|
+
|
|
379
|
+
- **`spmv` / `spmm` accepted a `y` / `C` that enlarged the output** (silent;
|
|
380
|
+
every release 0.1.1–0.12.1, all backends). `beta * y` was added with plain
|
|
381
|
+
broadcasting, so a `(m, 1)` `y` turned the `(m,)` product into `(m, m)`
|
|
382
|
+
with no error, where `torch.addmv` / `addmm` raise. Such shapes now raise
|
|
383
|
+
`ValueError`; every `y` / `C` that broadcasts to the product's shape works
|
|
384
|
+
as before (#67).
|
|
385
|
+
|
|
386
|
+
|
|
387
|
+
- **bf16 Q / K / V raised on the gather attention kernels** (loud; #49).
|
|
388
|
+
Affects released versions **0.4.4 through 0.12.1** with `backend="nki"`
|
|
389
|
+
(or `"auto"` on Neuron): every bf16 call that took the gather kernels,
|
|
390
|
+
i.e. `head_dim > 128` (K-tiled, since 0.6.0), the XLA path, or the native
|
|
391
|
+
path with the static structure off. The weights @ V, dS @ K, dSᵀ @ Q and
|
|
392
|
+
Pᵀ @ dO matmuls paired fp32 P / dS with bf16 operands, and NKI rejects
|
|
393
|
+
mixed fp32 / bf16 `nc_matmul` operands ("if one input is tfloat32/float32,
|
|
394
|
+
both must be"). With `TRNSPARSE_REQUIRE_NKI=1` the call raised. Without it,
|
|
395
|
+
the call fell back to the PyTorch path, which gives correct results but
|
|
396
|
+
runs off the NKI path. The gather kernels now handle precision like the
|
|
397
|
+
static kernels: one operand dtype per call (bf16 when Q, K, V (and dO) are
|
|
398
|
+
all bf16, else fp32); fp32 scores, softmax statistics, D and PSUM; and P /
|
|
399
|
+
dS rounded to the operand dtype before their matmul. The scale and D are
|
|
400
|
+
computed on the host in fp32. fp32 and fp64 inputs give the same values as
|
|
401
|
+
before.
|
|
402
|
+
- **`head_dim` above 128 that is not a multiple of 128 (e.g. 192) raised on
|
|
403
|
+
the NKI attention path** (`AssertionError` in the gather, released 0.6.0
|
|
404
|
+
through 0.12.1). It is now zero-padded to the next multiple of 128 for the
|
|
405
|
+
K-tiled kernels and sliced back; zero columns change no score and no
|
|
406
|
+
gradient (#49).
|
|
407
|
+
|
|
10
408
|
## [0.12.1] — 2026-11-01
|
|
11
409
|
|
|
12
410
|
Validated on Trn2 (trn2.3xlarge, Neuron SDK 2.33.0, native TorchNeuron) — see trnsci/trnsci#35.
|
|
@@ -22,7 +22,9 @@ trnsparse/
|
|
|
22
22
|
│ ├── formats.py # CSRMatrix, COOMatrix, BSRMatrix, from_dense
|
|
23
23
|
│ ├── ops.py # spmv, spmm, bsr_spmm, screened_spmm, add, scale, transpose
|
|
24
24
|
│ ├── screening.py # schwarz_bounds, screen_quartets, density_screen
|
|
25
|
-
│ ├── iterative.py # cg_bsr, power_iteration_bsr, jacobi_preconditioner_bsr
|
|
25
|
+
│ ├── iterative.py # cg_bsr, cg_bsr_fused, power_iteration_bsr(_fused), jacobi_preconditioner_bsr
|
|
26
|
+
│ ├── multicore.py # ShardedBSR, ShardedAttention (logical NeuronCores of one chip)
|
|
27
|
+
│ ├── kernel_matmul.py # kernel_matmul, KernelOperator: fused K(X, Y) @ V, K never formed (#93)
|
|
26
28
|
│ └── nki/
|
|
27
29
|
│ ├── __init__.py
|
|
28
30
|
│ ├── kernels.py # _bsr_spmm_kernel, _screened_spmm_kernel, attention kernels
|
|
@@ -36,24 +38,28 @@ trnsparse/
|
|
|
36
38
|
│ └── block_sparse_attention.py # Block-sparse attention via bsr_spmm
|
|
37
39
|
```
|
|
38
40
|
|
|
39
|
-
## NKI compute strategy
|
|
41
|
+
## NKI compute strategy
|
|
40
42
|
|
|
41
43
|
**Formats:** CSR/COO are interop and PyTorch-fallback formats.
|
|
42
44
|
BSR at `block_size=128` is the NKI compute format — every nonzero block
|
|
43
|
-
is already a 128×128 Tensor Engine tile, zero gather overhead.
|
|
45
|
+
is already a 128×128 Tensor Engine tile, zero gather overhead. Other block
|
|
46
|
+
sizes go straight to the PyTorch path (raise under `TRNSPARSE_REQUIRE_NKI`).
|
|
44
47
|
|
|
45
48
|
**Dispatch hierarchy:**
|
|
46
|
-
1. `spmm(csr, B)` —
|
|
47
|
-
|
|
49
|
+
1. `spmm(csr, B)` — densify-then-GEMM kernel under `nki` / `auto` with NKI;
|
|
50
|
+
`torch.sparse_csr_tensor` on the PyTorch path (within 2× of scipy).
|
|
48
51
|
2. `bsr_spmm(bsr, B)` — NKI kernel `_bsr_spmm_kernel`; one `nc_matmul`
|
|
49
52
|
per nonzero block. `torch.autograd.Function`-wrapped; gradcheck passes.
|
|
50
53
|
3. `screened_spmm(A, diag, B, threshold)` — fused Schwarz + mask + matmul
|
|
51
54
|
in one NKI dispatch; avoids 4 host passes + CSR build + separate SpMM.
|
|
52
|
-
4. `
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
55
|
+
4. `block_sparse_attention_tiled(Q, K, V, mask_bsr, scale=None)` — two-pass
|
|
56
|
+
attention kernels (forward + backward) over the mask's stored blocks;
|
|
57
|
+
`seq_len` must be a multiple of the block size.
|
|
58
|
+
5. `cg_bsr_fused` / `power_iteration_bsr_fused` — whole solve in one launch,
|
|
59
|
+
A SBUF-resident (#22).
|
|
60
|
+
|
|
61
|
+
**Open gaps:** row-bucketing CSR (#15, NKI capability-gated), sharding
|
|
62
|
+
across chips (#16; within one chip `ShardedBSR` shipped in 0.10.0).
|
|
57
63
|
|
|
58
64
|
## Dependencies
|
|
59
65
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: trnsparse
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.14.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
|
|
@@ -53,12 +53,16 @@ trnsparse follows the [trnsci 5-phase roadmap](https://trnsci.dev/roadmap/). Act
|
|
|
53
53
|
|
|
54
54
|
- **[Phase 1 — correctness](https://github.com/trnsci/trnsparse/issues/14)** ✅ v0.2.0: NKI SpMM validated on trn1 via densify-then-GEMM (see [`trnsci/trnsci#3`](https://github.com/trnsci/trnsci/issues/3)).
|
|
55
55
|
- **v0.3.0** ✅ [`BSRMatrix`](https://trnsci.dev/trnsparse/architecture/) — Trainium-native 128×128 block-sparse format; `bsr_spmm` NKI kernel. CSR becomes interop; BSR is the compute path.
|
|
56
|
-
- **v0.3.2** ✅ `cg_bsr`, `power_iteration_bsr` — iterative solvers over BSR (Python loop; fused kernel
|
|
56
|
+
- **v0.3.2** ✅ `cg_bsr`, `power_iteration_bsr` — iterative solvers over BSR (Python loop; the fused kernel shipped in 0.11.0, below).
|
|
57
57
|
- **v0.4.0** ✅ `screened_spmm` — fused Schwarz-screened SpMM in one NKI dispatch.
|
|
58
58
|
- **v0.4.2** ✅ Block-sparse attention — `BSRMatrix` + `bsr_spmm` as the primitive; `examples/block_sparse_attention.py` + [`docs/sparse_attention.md`](https://trnsci.dev/trnsparse/sparse_attention/).
|
|
59
59
|
- **v0.4.3** ✅ Architecture-friendly alternatives: `chebyshev_bsr` / `richardson_bsr` (fixed-K solvers, no inner products); `block_sparse_attention_tiled` (two-pass, no O(seq²) intermediate).
|
|
60
|
-
- **
|
|
61
|
-
- **
|
|
60
|
+
- **v0.4.4–v0.6.0** ✅ NKI attention kernel pair, NKI backward, `head_dim` multiples of 128 (K-tiling).
|
|
61
|
+
- **v0.8.0–v0.9.0** ✅ Native TorchNeuron path (Trn2/Trn3): static-structure BSR SpMM and attention kernels, split across both LNC=2 cores, bf16 operands on fp32 PSUM.
|
|
62
|
+
- **v0.10.0** ✅ `ShardedBSR` / `ShardedAttention` across the logical NeuronCores of one chip; pre-transposed BSR SpMM beats dense aten up to ~82–98% block density on one core.
|
|
63
|
+
- **v0.11.0** ✅ `cg_bsr_fused` / `prepare_cg_bsr` — one-launch CG and block-Jacobi PCG with A in SBUF (5–56× faster than CPU on Trn2), `power_iteration_bsr_fused`.
|
|
64
|
+
- **[Phase 3 — perf](https://github.com/trnsci/trnsparse/issues/15)**: nnz-bucketing CSR, streaming large-sparse — parked on NKI indirect DMA gather.
|
|
65
|
+
- **[Phase 4 — multi-chip](https://github.com/trnsci/trnsparse/issues/16)**: sharded BSR across chips (within one chip: `ShardedBSR`, v0.10.0).
|
|
62
66
|
- **[Phase 5 — generation](https://github.com/trnsci/trnsparse/issues/17)**: trn2 DMA bandwidth exploitation.
|
|
63
67
|
|
|
64
68
|
_(No Phase 2 for trnsparse — precision inherited from trnblas.)_
|
|
@@ -97,6 +101,39 @@ to force a path. With an editable install (`pip install -e`), set
|
|
|
97
101
|
`NKI_ENABLE_TRACE_CACHE=0`: the NKI kernel cache only versions installed
|
|
98
102
|
wheels and can serve stale kernels.
|
|
99
103
|
|
|
104
|
+
### Known platform issue: NKI trace cache and kernel factories
|
|
105
|
+
|
|
106
|
+
`import torch_neuronx` turns on the NKI trace cache, and the cache key ignores
|
|
107
|
+
constants captured in a kernel's closure: two `@nki.jit` kernels built by one
|
|
108
|
+
factory, differing only in a captured value, share one trace, and the second
|
|
109
|
+
silently returns the first's results
|
|
110
|
+
([aws-neuron/aws-neuron-sdk#1454](https://github.com/aws-neuron/aws-neuron-sdk/issues/1454)).
|
|
111
|
+
trnsparse's kernels do not rely on closure constants (or on module globals that
|
|
112
|
+
can differ between processes): every value that specialises a kernel is a
|
|
113
|
+
kernel argument, and `tests/test_trace_cache_safety.py` enforces this. If you
|
|
114
|
+
write your own NKI kernels with a factory, set `NKI_ENABLE_TRACE_CACHE=0`
|
|
115
|
+
before importing `torch_neuronx`.
|
|
116
|
+
|
|
117
|
+
## Known issues in earlier releases
|
|
118
|
+
|
|
119
|
+
Silent wrong results fixed in the 2026-10-31 to 2026-11-02 releases. If you
|
|
120
|
+
use an affected version, upgrade. Details are in the [CHANGELOG](CHANGELOG.md).
|
|
121
|
+
|
|
122
|
+
| Bug | Affected | Fixed in |
|
|
123
|
+
|---|---|---|
|
|
124
|
+
| `block_sparse_attention_tiled` returned zero rows for a partial last block (PyTorch path) (#44) | 0.6.0–0.10.0 | 0.11.0 |
|
|
125
|
+
| `block_sparse_attention_tiled` ignored a custom `scale` on the NKI paths (#47) | 0.6.0–0.11.0 | 0.12.0 |
|
|
126
|
+
| `spmv_symmetric` ignored `uplo` and used both triangles (#52) | through 0.12.0 | 0.12.1 |
|
|
127
|
+
| `COOMatrix.to_dense` dropped duplicate entries instead of summing them (#56) | 0.1.1–0.12.1 | 0.13.0 |
|
|
128
|
+
| `from_scipy` dropped the imaginary part of complex matrices (#58) | 0.1.1–0.12.1 | 0.13.0 |
|
|
129
|
+
| Second derivatives of `block_sparse_attention_tiled` were wrong (#65) | 0.6.0–0.12.1 | 0.13.0 |
|
|
130
|
+
| `spmv` / `spmm` accepted a `y` / `C` that broadcast the output to a larger shape (#67) | 0.1.1–0.12.1 | 0.13.0 |
|
|
131
|
+
| `chebyshev_coeffs` / `chebyshev_bsr` were not the Chebyshev polynomial for K >= 2 (#73) | 0.4.3–0.12.1 | 0.13.0 |
|
|
132
|
+
| `bsr_diagonal` returned length m (zero-padded) for a tall matrix (#74) | 0.3.2–0.12.1 | 0.13.0 |
|
|
133
|
+
| `bsr_spmm` dropped the imaginary part of complex operands on the NKI backend (#78) | 0.8.0–0.12.1 | 0.13.0 |
|
|
134
|
+
| `spmv` / `spmm` returned float32 for integer operands on the device (#79) | through 0.12.1 (device CSR path) | 0.13.0 |
|
|
135
|
+
| `block_sparse_attention_tiled` accepted a mask smaller than `seq_len` on the NKI path (#82) | 0.10.0–0.12.1 | 0.13.0 |
|
|
136
|
+
|
|
100
137
|
## Usage
|
|
101
138
|
|
|
102
139
|
```python
|
|
@@ -125,10 +162,10 @@ stats = trnsparse.sparsity_stats(Q)
|
|
|
125
162
|
| Operation | Description |
|
|
126
163
|
|-----------|-------------|
|
|
127
164
|
| `spmv` | Sparse × dense vector (CSR) |
|
|
128
|
-
| `spmm` | Sparse × dense matrix (CSR,
|
|
129
|
-
| `bsr_spmm` | Block-sparse × dense (BSR-native, Tensor Engine) |
|
|
165
|
+
| `spmm` | Sparse × dense matrix (CSR; densify-then-GEMM on NKI, `torch.sparse` otherwise) |
|
|
166
|
+
| `bsr_spmm` | Block-sparse × dense (BSR-native, Tensor Engine; NKI at `block_size=128`, other sizes run on PyTorch) |
|
|
130
167
|
| `screened_spmm` | Fused Schwarz-screened matmul (one NKI dispatch) |
|
|
131
|
-
| `spmv_symmetric` | Symmetric SpMV (
|
|
168
|
+
| `spmv_symmetric` | Symmetric SpMV; references only the `uplo` triangle, as in BLAS `symv` (0.12.1) |
|
|
132
169
|
| `sparse_add` | C = αA + βB |
|
|
133
170
|
| `sparse_scale` | B = αA |
|
|
134
171
|
| `sparse_transpose` | Aᵀ |
|
|
@@ -139,8 +176,9 @@ stats = trnsparse.sparsity_stats(Q)
|
|
|
139
176
|
| `power_iteration_bsr` | Dominant eigenpair via power iteration |
|
|
140
177
|
| `power_iteration_bsr_fused` | Power iteration in one launch (the fused CG kernel's machinery) |
|
|
141
178
|
| `jacobi_preconditioner_bsr` | Diagonal preconditioner for `cg_bsr` |
|
|
179
|
+
| `ShardedBSR` / `ShardedAttention` | BSR SpMM / attention forward split across logical NeuronCores (`trnsparse.multicore`) |
|
|
142
180
|
| `bsr_diagonal` | Extract main diagonal from BSR matrix |
|
|
143
|
-
| `block_sparse_attention_tiled` | Two-pass sparse attention, no O(seq²) intermediate |
|
|
181
|
+
| `block_sparse_attention_tiled` | Two-pass sparse attention, no O(seq²) intermediate, autograd; `seq_len` must be a multiple of the block size |
|
|
144
182
|
| `schwarz_bounds` | Schwarz screening bounds |
|
|
145
183
|
| `screen_quartets` | Shell quartet significance mask |
|
|
146
184
|
| `density_screen` | Density-weighted screening |
|
|
@@ -18,12 +18,16 @@ trnsparse follows the [trnsci 5-phase roadmap](https://trnsci.dev/roadmap/). Act
|
|
|
18
18
|
|
|
19
19
|
- **[Phase 1 — correctness](https://github.com/trnsci/trnsparse/issues/14)** ✅ v0.2.0: NKI SpMM validated on trn1 via densify-then-GEMM (see [`trnsci/trnsci#3`](https://github.com/trnsci/trnsci/issues/3)).
|
|
20
20
|
- **v0.3.0** ✅ [`BSRMatrix`](https://trnsci.dev/trnsparse/architecture/) — Trainium-native 128×128 block-sparse format; `bsr_spmm` NKI kernel. CSR becomes interop; BSR is the compute path.
|
|
21
|
-
- **v0.3.2** ✅ `cg_bsr`, `power_iteration_bsr` — iterative solvers over BSR (Python loop; fused kernel
|
|
21
|
+
- **v0.3.2** ✅ `cg_bsr`, `power_iteration_bsr` — iterative solvers over BSR (Python loop; the fused kernel shipped in 0.11.0, below).
|
|
22
22
|
- **v0.4.0** ✅ `screened_spmm` — fused Schwarz-screened SpMM in one NKI dispatch.
|
|
23
23
|
- **v0.4.2** ✅ Block-sparse attention — `BSRMatrix` + `bsr_spmm` as the primitive; `examples/block_sparse_attention.py` + [`docs/sparse_attention.md`](https://trnsci.dev/trnsparse/sparse_attention/).
|
|
24
24
|
- **v0.4.3** ✅ Architecture-friendly alternatives: `chebyshev_bsr` / `richardson_bsr` (fixed-K solvers, no inner products); `block_sparse_attention_tiled` (two-pass, no O(seq²) intermediate).
|
|
25
|
-
- **
|
|
26
|
-
- **
|
|
25
|
+
- **v0.4.4–v0.6.0** ✅ NKI attention kernel pair, NKI backward, `head_dim` multiples of 128 (K-tiling).
|
|
26
|
+
- **v0.8.0–v0.9.0** ✅ Native TorchNeuron path (Trn2/Trn3): static-structure BSR SpMM and attention kernels, split across both LNC=2 cores, bf16 operands on fp32 PSUM.
|
|
27
|
+
- **v0.10.0** ✅ `ShardedBSR` / `ShardedAttention` across the logical NeuronCores of one chip; pre-transposed BSR SpMM beats dense aten up to ~82–98% block density on one core.
|
|
28
|
+
- **v0.11.0** ✅ `cg_bsr_fused` / `prepare_cg_bsr` — one-launch CG and block-Jacobi PCG with A in SBUF (5–56× faster than CPU on Trn2), `power_iteration_bsr_fused`.
|
|
29
|
+
- **[Phase 3 — perf](https://github.com/trnsci/trnsparse/issues/15)**: nnz-bucketing CSR, streaming large-sparse — parked on NKI indirect DMA gather.
|
|
30
|
+
- **[Phase 4 — multi-chip](https://github.com/trnsci/trnsparse/issues/16)**: sharded BSR across chips (within one chip: `ShardedBSR`, v0.10.0).
|
|
27
31
|
- **[Phase 5 — generation](https://github.com/trnsci/trnsparse/issues/17)**: trn2 DMA bandwidth exploitation.
|
|
28
32
|
|
|
29
33
|
_(No Phase 2 for trnsparse — precision inherited from trnblas.)_
|
|
@@ -62,6 +66,39 @@ to force a path. With an editable install (`pip install -e`), set
|
|
|
62
66
|
`NKI_ENABLE_TRACE_CACHE=0`: the NKI kernel cache only versions installed
|
|
63
67
|
wheels and can serve stale kernels.
|
|
64
68
|
|
|
69
|
+
### Known platform issue: NKI trace cache and kernel factories
|
|
70
|
+
|
|
71
|
+
`import torch_neuronx` turns on the NKI trace cache, and the cache key ignores
|
|
72
|
+
constants captured in a kernel's closure: two `@nki.jit` kernels built by one
|
|
73
|
+
factory, differing only in a captured value, share one trace, and the second
|
|
74
|
+
silently returns the first's results
|
|
75
|
+
([aws-neuron/aws-neuron-sdk#1454](https://github.com/aws-neuron/aws-neuron-sdk/issues/1454)).
|
|
76
|
+
trnsparse's kernels do not rely on closure constants (or on module globals that
|
|
77
|
+
can differ between processes): every value that specialises a kernel is a
|
|
78
|
+
kernel argument, and `tests/test_trace_cache_safety.py` enforces this. If you
|
|
79
|
+
write your own NKI kernels with a factory, set `NKI_ENABLE_TRACE_CACHE=0`
|
|
80
|
+
before importing `torch_neuronx`.
|
|
81
|
+
|
|
82
|
+
## Known issues in earlier releases
|
|
83
|
+
|
|
84
|
+
Silent wrong results fixed in the 2026-10-31 to 2026-11-02 releases. If you
|
|
85
|
+
use an affected version, upgrade. Details are in the [CHANGELOG](CHANGELOG.md).
|
|
86
|
+
|
|
87
|
+
| Bug | Affected | Fixed in |
|
|
88
|
+
|---|---|---|
|
|
89
|
+
| `block_sparse_attention_tiled` returned zero rows for a partial last block (PyTorch path) (#44) | 0.6.0–0.10.0 | 0.11.0 |
|
|
90
|
+
| `block_sparse_attention_tiled` ignored a custom `scale` on the NKI paths (#47) | 0.6.0–0.11.0 | 0.12.0 |
|
|
91
|
+
| `spmv_symmetric` ignored `uplo` and used both triangles (#52) | through 0.12.0 | 0.12.1 |
|
|
92
|
+
| `COOMatrix.to_dense` dropped duplicate entries instead of summing them (#56) | 0.1.1–0.12.1 | 0.13.0 |
|
|
93
|
+
| `from_scipy` dropped the imaginary part of complex matrices (#58) | 0.1.1–0.12.1 | 0.13.0 |
|
|
94
|
+
| Second derivatives of `block_sparse_attention_tiled` were wrong (#65) | 0.6.0–0.12.1 | 0.13.0 |
|
|
95
|
+
| `spmv` / `spmm` accepted a `y` / `C` that broadcast the output to a larger shape (#67) | 0.1.1–0.12.1 | 0.13.0 |
|
|
96
|
+
| `chebyshev_coeffs` / `chebyshev_bsr` were not the Chebyshev polynomial for K >= 2 (#73) | 0.4.3–0.12.1 | 0.13.0 |
|
|
97
|
+
| `bsr_diagonal` returned length m (zero-padded) for a tall matrix (#74) | 0.3.2–0.12.1 | 0.13.0 |
|
|
98
|
+
| `bsr_spmm` dropped the imaginary part of complex operands on the NKI backend (#78) | 0.8.0–0.12.1 | 0.13.0 |
|
|
99
|
+
| `spmv` / `spmm` returned float32 for integer operands on the device (#79) | through 0.12.1 (device CSR path) | 0.13.0 |
|
|
100
|
+
| `block_sparse_attention_tiled` accepted a mask smaller than `seq_len` on the NKI path (#82) | 0.10.0–0.12.1 | 0.13.0 |
|
|
101
|
+
|
|
65
102
|
## Usage
|
|
66
103
|
|
|
67
104
|
```python
|
|
@@ -90,10 +127,10 @@ stats = trnsparse.sparsity_stats(Q)
|
|
|
90
127
|
| Operation | Description |
|
|
91
128
|
|-----------|-------------|
|
|
92
129
|
| `spmv` | Sparse × dense vector (CSR) |
|
|
93
|
-
| `spmm` | Sparse × dense matrix (CSR,
|
|
94
|
-
| `bsr_spmm` | Block-sparse × dense (BSR-native, Tensor Engine) |
|
|
130
|
+
| `spmm` | Sparse × dense matrix (CSR; densify-then-GEMM on NKI, `torch.sparse` otherwise) |
|
|
131
|
+
| `bsr_spmm` | Block-sparse × dense (BSR-native, Tensor Engine; NKI at `block_size=128`, other sizes run on PyTorch) |
|
|
95
132
|
| `screened_spmm` | Fused Schwarz-screened matmul (one NKI dispatch) |
|
|
96
|
-
| `spmv_symmetric` | Symmetric SpMV (
|
|
133
|
+
| `spmv_symmetric` | Symmetric SpMV; references only the `uplo` triangle, as in BLAS `symv` (0.12.1) |
|
|
97
134
|
| `sparse_add` | C = αA + βB |
|
|
98
135
|
| `sparse_scale` | B = αA |
|
|
99
136
|
| `sparse_transpose` | Aᵀ |
|
|
@@ -104,8 +141,9 @@ stats = trnsparse.sparsity_stats(Q)
|
|
|
104
141
|
| `power_iteration_bsr` | Dominant eigenpair via power iteration |
|
|
105
142
|
| `power_iteration_bsr_fused` | Power iteration in one launch (the fused CG kernel's machinery) |
|
|
106
143
|
| `jacobi_preconditioner_bsr` | Diagonal preconditioner for `cg_bsr` |
|
|
144
|
+
| `ShardedBSR` / `ShardedAttention` | BSR SpMM / attention forward split across logical NeuronCores (`trnsparse.multicore`) |
|
|
107
145
|
| `bsr_diagonal` | Extract main diagonal from BSR matrix |
|
|
108
|
-
| `block_sparse_attention_tiled` | Two-pass sparse attention, no O(seq²) intermediate |
|
|
146
|
+
| `block_sparse_attention_tiled` | Two-pass sparse attention, no O(seq²) intermediate, autograd; `seq_len` must be a multiple of the block size |
|
|
109
147
|
| `schwarz_bounds` | Schwarz screening bounds |
|
|
110
148
|
| `screen_quartets` | Shell quartet significance mask |
|
|
111
149
|
| `density_screen` | Density-weighted screening |
|