trnsparse 0.15.0__tar.gz → 0.16.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.15.0 → trnsparse-0.16.0}/.github/workflows/ci.yml +1 -1
- trnsparse-0.16.0/.pre-commit-config.yaml +9 -0
- {trnsparse-0.15.0 → trnsparse-0.16.0}/CHANGELOG.md +47 -0
- {trnsparse-0.15.0 → trnsparse-0.16.0}/CONTRIBUTING.md +11 -0
- {trnsparse-0.15.0/trnsparse.egg-info → trnsparse-0.16.0}/PKG-INFO +9 -2
- {trnsparse-0.15.0 → trnsparse-0.16.0}/README.md +7 -1
- {trnsparse-0.15.0 → trnsparse-0.16.0}/docs/iterative_solvers.md +39 -4
- {trnsparse-0.15.0 → trnsparse-0.16.0}/pyproject.toml +3 -3
- trnsparse-0.16.0/tests/test_cg_bsr_reduction.py +190 -0
- trnsparse-0.16.0/tests/test_cg_span.py +93 -0
- {trnsparse-0.15.0 → trnsparse-0.16.0}/tests/test_kernel_dtype_guard.py +9 -2
- {trnsparse-0.15.0 → trnsparse-0.16.0}/tests/test_kernel_matmul.py +112 -0
- {trnsparse-0.15.0 → trnsparse-0.16.0}/tests/test_kernel_matmul_neuron.py +33 -0
- {trnsparse-0.15.0 → trnsparse-0.16.0}/tests/test_nki_cg_fused.py +87 -0
- {trnsparse-0.15.0 → trnsparse-0.16.0}/tests/test_nki_compile.py +29 -0
- {trnsparse-0.15.0 → trnsparse-0.16.0}/trnsparse/__init__.py +1 -1
- trnsparse-0.16.0/trnsparse/_reduce.py +88 -0
- {trnsparse-0.15.0 → trnsparse-0.16.0}/trnsparse/iterative.py +37 -12
- {trnsparse-0.15.0 → trnsparse-0.16.0}/trnsparse/kernel_matmul.py +48 -7
- {trnsparse-0.15.0 → trnsparse-0.16.0}/trnsparse/nki/kernels.py +55 -8
- {trnsparse-0.15.0 → trnsparse-0.16.0/trnsparse.egg-info}/PKG-INFO +9 -2
- {trnsparse-0.15.0 → trnsparse-0.16.0}/trnsparse.egg-info/SOURCES.txt +3 -0
- {trnsparse-0.15.0 → trnsparse-0.16.0}/trnsparse.egg-info/requires.txt +1 -0
- trnsparse-0.15.0/.pre-commit-config.yaml +0 -7
- {trnsparse-0.15.0 → trnsparse-0.16.0}/.github/workflows/notify-umbrella.yml +0 -0
- {trnsparse-0.15.0 → trnsparse-0.16.0}/.github/workflows/publish.yml +0 -0
- {trnsparse-0.15.0 → trnsparse-0.16.0}/.gitignore +0 -0
- {trnsparse-0.15.0 → trnsparse-0.16.0}/CLAUDE.md +0 -0
- {trnsparse-0.15.0 → trnsparse-0.16.0}/CODE_OF_CONDUCT.md +0 -0
- {trnsparse-0.15.0 → trnsparse-0.16.0}/LICENSE +0 -0
- {trnsparse-0.15.0 → trnsparse-0.16.0}/benchmarks/bench_bsr_spmm.py +0 -0
- {trnsparse-0.15.0 → trnsparse-0.16.0}/benchmarks/bench_cg_fused_trn2.py +0 -0
- {trnsparse-0.15.0 → trnsparse-0.16.0}/benchmarks/bench_iterative.py +0 -0
- {trnsparse-0.15.0 → trnsparse-0.16.0}/benchmarks/bench_kernel_matmul_trn2.py +0 -0
- {trnsparse-0.15.0 → trnsparse-0.16.0}/benchmarks/bench_multicore_trn2.py +0 -0
- {trnsparse-0.15.0 → trnsparse-0.16.0}/benchmarks/bench_multiproc_trn2.py +0 -0
- {trnsparse-0.15.0 → trnsparse-0.16.0}/benchmarks/bench_native_trn2.py +0 -0
- {trnsparse-0.15.0 → trnsparse-0.16.0}/benchmarks/bench_power_fused_trn2.py +0 -0
- {trnsparse-0.15.0 → trnsparse-0.16.0}/benchmarks/bench_screened_tiles_trn2.py +0 -0
- {trnsparse-0.15.0 → trnsparse-0.16.0}/benchmarks/bench_screening.py +0 -0
- {trnsparse-0.15.0 → trnsparse-0.16.0}/benchmarks/bench_spmm.py +0 -0
- {trnsparse-0.15.0 → trnsparse-0.16.0}/benchmarks/bench_spmv.py +0 -0
- {trnsparse-0.15.0 → trnsparse-0.16.0}/benchmarks/conftest.py +0 -0
- {trnsparse-0.15.0 → trnsparse-0.16.0}/docs/api.md +0 -0
- {trnsparse-0.15.0 → trnsparse-0.16.0}/docs/architecture.md +0 -0
- {trnsparse-0.15.0 → trnsparse-0.16.0}/docs/aws_setup.md +0 -0
- {trnsparse-0.15.0 → trnsparse-0.16.0}/docs/benchmarks.md +0 -0
- {trnsparse-0.15.0 → trnsparse-0.16.0}/docs/index.md +0 -0
- {trnsparse-0.15.0 → trnsparse-0.16.0}/docs/installation.md +0 -0
- {trnsparse-0.15.0 → trnsparse-0.16.0}/docs/migration_scipy.md +0 -0
- {trnsparse-0.15.0 → trnsparse-0.16.0}/docs/quickstart.md +0 -0
- {trnsparse-0.15.0 → trnsparse-0.16.0}/docs/sparse_attention.md +0 -0
- {trnsparse-0.15.0 → trnsparse-0.16.0}/examples/block_sparse_attention.py +0 -0
- {trnsparse-0.15.0 → trnsparse-0.16.0}/examples/sparse_fock.py +0 -0
- {trnsparse-0.15.0 → trnsparse-0.16.0}/infra/terraform/.terraform.lock.hcl +0 -0
- {trnsparse-0.15.0 → trnsparse-0.16.0}/infra/terraform/README.md +0 -0
- {trnsparse-0.15.0 → trnsparse-0.16.0}/infra/terraform/main.tf +0 -0
- {trnsparse-0.15.0 → trnsparse-0.16.0}/mkdocs.yml +0 -0
- {trnsparse-0.15.0 → trnsparse-0.16.0}/scripts/bench_eager_handle.py +0 -0
- {trnsparse-0.15.0 → trnsparse-0.16.0}/scripts/bench_to_md.py +0 -0
- {trnsparse-0.15.0 → trnsparse-0.16.0}/scripts/run_benchmarks.sh +0 -0
- {trnsparse-0.15.0 → trnsparse-0.16.0}/scripts/run_neuron_tests.sh +0 -0
- {trnsparse-0.15.0 → trnsparse-0.16.0}/scripts/run_simulator_tests.sh +0 -0
- {trnsparse-0.15.0 → trnsparse-0.16.0}/setup.cfg +0 -0
- {trnsparse-0.15.0 → trnsparse-0.16.0}/tests/conftest.py +0 -0
- {trnsparse-0.15.0 → trnsparse-0.16.0}/tests/test_attention.py +0 -0
- {trnsparse-0.15.0 → trnsparse-0.16.0}/tests/test_attn_gather.py +0 -0
- {trnsparse-0.15.0 → trnsparse-0.16.0}/tests/test_bsr.py +0 -0
- {trnsparse-0.15.0 → trnsparse-0.16.0}/tests/test_conformance.py +0 -0
- {trnsparse-0.15.0 → trnsparse-0.16.0}/tests/test_core_split.py +0 -0
- {trnsparse-0.15.0 → trnsparse-0.16.0}/tests/test_examples.py +0 -0
- {trnsparse-0.15.0 → trnsparse-0.16.0}/tests/test_formats.py +0 -0
- {trnsparse-0.15.0 → trnsparse-0.16.0}/tests/test_iterative.py +0 -0
- {trnsparse-0.15.0 → trnsparse-0.16.0}/tests/test_multicore.py +0 -0
- {trnsparse-0.15.0 → trnsparse-0.16.0}/tests/test_native_dispatch.py +0 -0
- {trnsparse-0.15.0 → trnsparse-0.16.0}/tests/test_nki_attn.py +0 -0
- {trnsparse-0.15.0 → trnsparse-0.16.0}/tests/test_nki_bsr.py +0 -0
- {trnsparse-0.15.0 → trnsparse-0.16.0}/tests/test_nki_kernel_matmul.py +0 -0
- {trnsparse-0.15.0 → trnsparse-0.16.0}/tests/test_nki_native.py +0 -0
- {trnsparse-0.15.0 → trnsparse-0.16.0}/tests/test_nki_screened_spmm.py +0 -0
- {trnsparse-0.15.0 → trnsparse-0.16.0}/tests/test_nki_sim.py +0 -0
- {trnsparse-0.15.0 → trnsparse-0.16.0}/tests/test_nki_spmm.py +0 -0
- {trnsparse-0.15.0 → trnsparse-0.16.0}/tests/test_nki_tail_audit.py +0 -0
- {trnsparse-0.15.0 → trnsparse-0.16.0}/tests/test_ops.py +0 -0
- {trnsparse-0.15.0 → trnsparse-0.16.0}/tests/test_screening.py +0 -0
- {trnsparse-0.15.0 → trnsparse-0.16.0}/tests/test_trace_cache_safety.py +0 -0
- {trnsparse-0.15.0 → trnsparse-0.16.0}/trnsparse/formats.py +0 -0
- {trnsparse-0.15.0 → trnsparse-0.16.0}/trnsparse/multicore.py +0 -0
- {trnsparse-0.15.0 → trnsparse-0.16.0}/trnsparse/nki/__init__.py +0 -0
- {trnsparse-0.15.0 → trnsparse-0.16.0}/trnsparse/nki/_native.py +0 -0
- {trnsparse-0.15.0 → trnsparse-0.16.0}/trnsparse/nki/_native_attn.py +0 -0
- {trnsparse-0.15.0 → trnsparse-0.16.0}/trnsparse/nki/dispatch.py +0 -0
- {trnsparse-0.15.0 → trnsparse-0.16.0}/trnsparse/ops.py +0 -0
- {trnsparse-0.15.0 → trnsparse-0.16.0}/trnsparse/screening.py +0 -0
- {trnsparse-0.15.0 → trnsparse-0.16.0}/trnsparse.egg-info/dependency_links.txt +0 -0
- {trnsparse-0.15.0 → trnsparse-0.16.0}/trnsparse.egg-info/top_level.txt +0 -0
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
repos:
|
|
2
|
+
# Keep rev in lockstep with the ruff pin in .github/workflows/ci.yml
|
|
3
|
+
# (and the [dev] extra in pyproject.toml) so local and CI agree.
|
|
4
|
+
- repo: https://github.com/astral-sh/ruff-pre-commit
|
|
5
|
+
rev: v0.17.3
|
|
6
|
+
hooks:
|
|
7
|
+
- id: ruff-check
|
|
8
|
+
args: [--fix]
|
|
9
|
+
- id: ruff-format
|
|
@@ -7,6 +7,53 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [0.16.0] — 2026-11-10
|
|
11
|
+
|
|
12
|
+
Validated on Trn2 (trn2.3xlarge, Neuron SDK 2.33.0, native TorchNeuron) — see trnsci/trnsci#35.
|
|
13
|
+
|
|
14
|
+
Headline: `cg_bsr(..., reduction=)` for LNC-invariant inner products and norms (opt-in, trnsci/trnsolver#128), and two silent fixes: `kernel_matmul` with a complex V dropped its imaginary part (#120), and the fused BSR CG / power kernels misread A at 65,536 fp32 blocks or more (#121, aws-neuron-sdk#1457).
|
|
15
|
+
|
|
16
|
+
### Added
|
|
17
|
+
|
|
18
|
+
- **`cg_bsr(..., reduction=None)`: LNC-invariant inner products and norms
|
|
19
|
+
(trnsci/trnsolver#128).** `"pairwise"` folds the elementwise products with
|
|
20
|
+
a fixed pairwise tree of device adds and finishes in float64 on the host
|
|
21
|
+
(the same tree as `trnsolver.cg_block`, kept local in
|
|
22
|
+
`trnsparse/_reduce.py`, so there is no trnsolver dependency). `"host64"`
|
|
23
|
+
sums the product on the host in float64. `"device"` (the default, the
|
|
24
|
+
previous behaviour) uses `torch.dot` / `torch.linalg.norm`. `None` reads
|
|
25
|
+
`TRNSPARSE_CG_REDUCTION`. The folded modes are opt-in because a BSR
|
|
26
|
+
matvec is cheap. Trn2, LNC=2, N = 100352 block-tridiagonal: 3.46 ms per
|
|
27
|
+
iteration with `"device"`, 6.57 ms with `"pairwise"` (+90%) and 3.90 ms
|
|
28
|
+
with `"host64"` (+13%).
|
|
29
|
+
|
|
30
|
+
### Fixed
|
|
31
|
+
|
|
32
|
+
- **`kernel_matmul` / `KernelOperator` with a complex V dropped its imaginary
|
|
33
|
+
part** (silent, #120). 0.14.0 through 0.15.0 cast V to the real working
|
|
34
|
+
dtype and returned `K @ Re(V)` as a real tensor, with no error (relative
|
|
35
|
+
error 0.90 at n = 40). The kernel is real, so a complex V now runs as one
|
|
36
|
+
real product on `[Re V, Im V]` (one launch on the NKI route, as `bsr_spmm`
|
|
37
|
+
does, #78) and returns V's complex dtype. complex128 V follows the float64
|
|
38
|
+
policy. Complex points `X` / `Y` (which lost their imaginary part the same
|
|
39
|
+
way) raise `TypeError`, also in `kernel_matmul_reference`, whose complex V
|
|
40
|
+
is now split the same way.
|
|
41
|
+
- **`cg_bsr_fused` / `power_iteration_bsr_fused` read A's blocks over 128
|
|
42
|
+
partitions at a 32 MiB partition step once fp32 A stores 65,536 blocks or
|
|
43
|
+
more** (#121, aws-neuron-sdk#1457). The kernels keep A's blocks side by side
|
|
44
|
+
in one `(128, n_blocks * 128)` operand; with 65,536 or more fp32 (or
|
|
45
|
+
bf16x2) blocks, or 131,072 or more bf16 ones, its memory rows are 32 MiB or
|
|
46
|
+
more, the layout whose device reads are misdirected on Trn2 (the
|
|
47
|
+
trnsci/trnblas#112 / trnsci/trntensor#123 rule, min(rows, 128) * row_bytes
|
|
48
|
+
>= 2**32). Such reads are now DMAs of row blocks of at most 64 partitions
|
|
49
|
+
(views of the same operand, no copies), each spanning at most 2 GiB + 1 MiB
|
|
50
|
+
(`nki.kernels.dma_row_block`). Below the rule every read is the one
|
|
51
|
+
full-partition DMA it was. On Trn2 at T = 256 dense (65,536 blocks), 0.11.0
|
|
52
|
+
through 0.15.0 ran CG to its iteration cap with no error and returned x
|
|
53
|
+
with true residual 3.9e6 (column matvec) / 1.7e7 (row); now 9 iterations,
|
|
54
|
+
5e-7. A probe DMA of such an operand read partitions 64-127 wrong (max abs
|
|
55
|
+
error 11); 64-row reads were bit-exact.
|
|
56
|
+
|
|
10
57
|
## [0.15.0] — 2026-11-04
|
|
11
58
|
|
|
12
59
|
Validated on Trn2 (trn2.3xlarge, Neuron SDK 2.33.0, native TorchNeuron) — see trnsci/trnsci#35.
|
|
@@ -56,6 +56,17 @@ Before opening a PR:
|
|
|
56
56
|
- [ ] If you added a public symbol, the project's `docs/api.md` lists it
|
|
57
57
|
- [ ] PR description says *what* changed and *why*, not just *what*
|
|
58
58
|
|
|
59
|
+
## Releasing
|
|
60
|
+
|
|
61
|
+
`.github/workflows/publish.yml` publishes to PyPI (trusted publishing) when a GitHub Release is published. A tag containing `-` (`vX.Y.Z-rc1`) goes to TestPyPI.
|
|
62
|
+
|
|
63
|
+
- [ ] Release PR (`release: vX.Y.Z`): bump `version` in `pyproject.toml` and `__version__` in `trnsparse/__init__.py`, and move the `[Unreleased]` CHANGELOG entries under `## [X.Y.Z] - YYYY-MM-DD`.
|
|
64
|
+
- [ ] After the merge, run `gh release create vX.Y.Z --target <merge sha>`, then confirm the publish run and the new version on PyPI.
|
|
65
|
+
- [ ] **Update umbrella docs/extras** in one [trnsci/trnsci](https://github.com/trnsci/trnsci) PR:
|
|
66
|
+
- if users should require this release (always for a fix to silent wrong results), bump `trnsparse` in the umbrella `pyproject.toml` `[sparse]` and `[all]` extras, and cut a `trnsci` meta-package release;
|
|
67
|
+
- update the `[[described:trnsparse=X.Y.Z]]` rows in `docs/roadmap.md` and `docs/nki_validation_status.md`, with the [#35](https://github.com/trnsci/trnsci/issues/35) Trn2 validation link;
|
|
68
|
+
- check that `python scripts/check_docs_drift.py` exits 0. The weekly docs-drift workflow opens a `docs-stale` issue for anything missed.
|
|
69
|
+
|
|
59
70
|
## Issues
|
|
60
71
|
|
|
61
72
|
Label each issue one of: `bug`, `feature`, `question`, `docs`, `infra`. Bugs include repro steps and expected-vs-actual output. Features describe the use case.
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: trnsparse
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.16.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
|
|
@@ -28,6 +28,7 @@ Requires-Dist: torch-neuronx>=2.14; extra == "torchneuron"
|
|
|
28
28
|
Requires-Dist: nki>=0.6; extra == "torchneuron"
|
|
29
29
|
Provides-Extra: dev
|
|
30
30
|
Requires-Dist: pytest>=7.0; extra == "dev"
|
|
31
|
+
Requires-Dist: ruff==0.17.3; extra == "dev"
|
|
31
32
|
Requires-Dist: pytest-benchmark>=4.0; extra == "dev"
|
|
32
33
|
Requires-Dist: pytest-cov>=4.1; extra == "dev"
|
|
33
34
|
Requires-Dist: scipy>=1.11; extra == "dev"
|
|
@@ -116,7 +117,7 @@ before importing `torch_neuronx`.
|
|
|
116
117
|
|
|
117
118
|
## Known issues in earlier releases
|
|
118
119
|
|
|
119
|
-
Silent wrong results fixed in the 2026-10-31 to 2026-11-
|
|
120
|
+
Silent wrong results fixed in the 2026-10-31 to 2026-11-04 releases. If you
|
|
120
121
|
use an affected version, upgrade. Details are in the [CHANGELOG](CHANGELOG.md).
|
|
121
122
|
|
|
122
123
|
| Bug | Affected | Fixed in |
|
|
@@ -133,6 +134,12 @@ use an affected version, upgrade. Details are in the [CHANGELOG](CHANGELOG.md).
|
|
|
133
134
|
| `bsr_spmm` dropped the imaginary part of complex operands on the NKI backend (#78) | 0.8.0–0.12.1 | 0.13.0 |
|
|
134
135
|
| `spmv` / `spmm` returned float32 for integer operands on the device (#79) | through 0.12.1 (device CSR path) | 0.13.0 |
|
|
135
136
|
| `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 |
|
|
137
|
+
| Integer, bool and complex operands reached the fp32/bf16 NKI kernels: the static attention kernels dropped the imaginary part of complex Q/K/V, and the static `bsr_spmm` and attention kernels cast integers to fp32 and back (trnsci/trnsci#35) | every release with NKI kernels through 0.13.0, NKI / `auto` backends | 0.14.0 |
|
|
138
|
+
| `from_scipy` downcast float64 to float32 (~3e-8 rel. err), int64 to float32 (exact only up to 2^24) and complex128 to complex64 (#69) | 0.1.1–0.13.0 | 0.14.0 |
|
|
139
|
+
| `cg_bsr` gave wrong gradients: autograd differentiated a frozen-coefficient polynomial, not the solve (`b.grad` ~20% off) (#70) | 0.3.2–0.13.0 | 0.14.0 |
|
|
140
|
+
| `power_iteration_bsr` gave wrong gradients (frozen normalisation; `v0` got a spurious gradient) (#96) | 0.3.2–0.13.0 | 0.14.0 |
|
|
141
|
+
| `cg_bsr` reported `iters = max_iter` after a breakdown, e.g. 500 for a one-step solve (#71) | 0.3.2–0.13.0 | 0.14.0 |
|
|
142
|
+
| `cg_bsr_fused` / `power_iteration_bsr_fused` dropped the imaginary part of complex input and truncated integer / bool `b`, on every backend (#85) | 0.11.0–0.13.0 | 0.14.0 |
|
|
136
143
|
|
|
137
144
|
## Usage
|
|
138
145
|
|
|
@@ -81,7 +81,7 @@ before importing `torch_neuronx`.
|
|
|
81
81
|
|
|
82
82
|
## Known issues in earlier releases
|
|
83
83
|
|
|
84
|
-
Silent wrong results fixed in the 2026-10-31 to 2026-11-
|
|
84
|
+
Silent wrong results fixed in the 2026-10-31 to 2026-11-04 releases. If you
|
|
85
85
|
use an affected version, upgrade. Details are in the [CHANGELOG](CHANGELOG.md).
|
|
86
86
|
|
|
87
87
|
| Bug | Affected | Fixed in |
|
|
@@ -98,6 +98,12 @@ use an affected version, upgrade. Details are in the [CHANGELOG](CHANGELOG.md).
|
|
|
98
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
99
|
| `spmv` / `spmm` returned float32 for integer operands on the device (#79) | through 0.12.1 (device CSR path) | 0.13.0 |
|
|
100
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
|
+
| Integer, bool and complex operands reached the fp32/bf16 NKI kernels: the static attention kernels dropped the imaginary part of complex Q/K/V, and the static `bsr_spmm` and attention kernels cast integers to fp32 and back (trnsci/trnsci#35) | every release with NKI kernels through 0.13.0, NKI / `auto` backends | 0.14.0 |
|
|
102
|
+
| `from_scipy` downcast float64 to float32 (~3e-8 rel. err), int64 to float32 (exact only up to 2^24) and complex128 to complex64 (#69) | 0.1.1–0.13.0 | 0.14.0 |
|
|
103
|
+
| `cg_bsr` gave wrong gradients: autograd differentiated a frozen-coefficient polynomial, not the solve (`b.grad` ~20% off) (#70) | 0.3.2–0.13.0 | 0.14.0 |
|
|
104
|
+
| `power_iteration_bsr` gave wrong gradients (frozen normalisation; `v0` got a spurious gradient) (#96) | 0.3.2–0.13.0 | 0.14.0 |
|
|
105
|
+
| `cg_bsr` reported `iters = max_iter` after a breakdown, e.g. 500 for a one-step solve (#71) | 0.3.2–0.13.0 | 0.14.0 |
|
|
106
|
+
| `cg_bsr_fused` / `power_iteration_bsr_fused` dropped the imaginary part of complex input and truncated integer / bool `b`, on every backend (#85) | 0.11.0–0.13.0 | 0.14.0 |
|
|
101
107
|
|
|
102
108
|
## Usage
|
|
103
109
|
|
|
@@ -42,6 +42,40 @@ round-trip per iteration. On CPU it's `torch.sparse`-backed and
|
|
|
42
42
|
roughly on par with `scipy.sparse.linalg.cg` (benchmarked: 369 μs vs
|
|
43
43
|
310 μs at 128×128 SPD, 1.19× slower).
|
|
44
44
|
|
|
45
|
+
### `cg_bsr(..., reduction=None)`: LNC-invariant reductions (trnsci/trnsolver#128)
|
|
46
|
+
|
|
47
|
+
On Trn2, device `dot` / `norm` add in an order that depends on how the
|
|
48
|
+
compiler splits the work across logical cores, so LNC=1 and LNC=2 give
|
|
49
|
+
different bits (bitwise reproducible within one LNC). `reduction=` picks how
|
|
50
|
+
`cg_bsr`'s inner products and norms reduce over N:
|
|
51
|
+
|
|
52
|
+
| `reduction` | How | LNC-invariant |
|
|
53
|
+
|---|---|---|
|
|
54
|
+
| `"device"` (default) | `torch.dot` / `torch.linalg.norm` on b's device | no |
|
|
55
|
+
| `"pairwise"` | elementwise products, a fixed pairwise tree of elementwise adds on the device (pad to m · 2^L, m ≤ 64), then the m leaves in float64 on the host; the same tree as `trnsolver.cg_block` | yes, given an LNC-invariant matvec |
|
|
56
|
+
| `"host64"` | the product is copied to the host and summed in float64 | yes |
|
|
57
|
+
|
|
58
|
+
`None` reads `TRNSPARSE_CG_REDUCTION`. Host tensors are multiplied in
|
|
59
|
+
float64 in the folded modes.
|
|
60
|
+
|
|
61
|
+
**Why `"device"` stays the default:** a BSR matvec is cheap, so the extra
|
|
62
|
+
small device ops and host syncs are a large fraction of an iteration. Trn2,
|
|
63
|
+
LNC=2, one logical core, N = 100352, block-tridiagonal compact-support
|
|
64
|
+
kernel (b = 128, 2350 blocks), 60 iterations:
|
|
65
|
+
|
|
66
|
+
| `reduction` | ms / iteration | vs `"device"` |
|
|
67
|
+
|---|---|---|
|
|
68
|
+
| `"device"` | 3.46 | |
|
|
69
|
+
| `"pairwise"` | 6.57 | +90% |
|
|
70
|
+
| `"host64"` | 3.90 | +13% |
|
|
71
|
+
|
|
72
|
+
The matvec alone is 1.55 ms. For one vector at this size, `"host64"` (one
|
|
73
|
+
400 KB copy) is cheaper than the 11-level fold, so it is the better choice
|
|
74
|
+
when you need LNC invariance here. trnsolver's `cg` and `gmres` with an
|
|
75
|
+
expensive operator (the GP `KernelOperator`, 289 ms per call) pay +0.1% (cg) or less (gmres) for
|
|
76
|
+
`"pairwise"`, which is why trnsolver defaults to it. `cg_bsr_fused` reduces
|
|
77
|
+
inside the kernel and does not take `reduction=`.
|
|
78
|
+
|
|
45
79
|
## Fused CG: `cg_bsr_fused` (#22)
|
|
46
80
|
|
|
47
81
|
`cg_bsr_fused` runs the whole CG (or block-Jacobi PCG) solve in **one
|
|
@@ -62,10 +96,11 @@ What the kernel does on the NeuronCore:
|
|
|
62
96
|
|
|
63
97
|
- **A's stored blocks are DMA'd into SBUF once** and every iteration
|
|
64
98
|
reads them from there. The budget is `TRNSPARSE_CG_SBUF_KIB` (default
|
|
65
|
-
160
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
99
|
+
160 KiB per partition, i.e. 20 MiB of SBUF), so about 320 fp32 128x128
|
|
100
|
+
blocks per physical core. Trn2 SBUF is 224 KiB per partition (28 MiB),
|
|
101
|
+
of which NKI's allocator gets about 207 KiB; 192 KiB (24 MiB) is the
|
|
102
|
+
Trn1 figure. Blocks beyond the budget stream from HBM in 16-block DMAs
|
|
103
|
+
on every iteration, still inside the launch.
|
|
69
104
|
- x, r, p (and z) stay in SBUF in a column layout (element `t*128 + p`
|
|
70
105
|
at `[p, t]`). The matvec is one `nc_matmul` per stored block; see
|
|
71
106
|
"Matvec orientation" below.
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
[build-system]
|
|
2
|
-
requires = ["setuptools>=
|
|
2
|
+
requires = ["setuptools>=77.0", "setuptools-scm>=8.0"]
|
|
3
3
|
build-backend = "setuptools.build_meta"
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "trnsparse"
|
|
7
|
-
version = "0.
|
|
7
|
+
version = "0.16.0"
|
|
8
8
|
description = "Sparse matrix operations for AWS Trainium via NKI"
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
license = "Apache-2.0"
|
|
@@ -47,7 +47,7 @@ torchneuron = [
|
|
|
47
47
|
"torch-neuronx>=2.14",
|
|
48
48
|
"nki>=0.6",
|
|
49
49
|
]
|
|
50
|
-
dev = ["pytest>=7.0", "pytest-benchmark>=4.0", "pytest-cov>=4.1", "scipy>=1.11"]
|
|
50
|
+
dev = ["pytest>=7.0", "ruff==0.17.3", "pytest-benchmark>=4.0", "pytest-cov>=4.1", "scipy>=1.11"]
|
|
51
51
|
|
|
52
52
|
[project.urls]
|
|
53
53
|
Homepage = "https://github.com/trnsci/trnsparse"
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
"""cg_bsr reductions over N that do not depend on the LNC split (trnsci/trnsolver#128).
|
|
2
|
+
|
|
3
|
+
The CPU stand-in follows trnsolver's tests/test_cg_reduction.py: ``_Dev``
|
|
4
|
+
marks "device" vectors whose full reductions (``dot``, ``vdot``, a 1-D
|
|
5
|
+
``linalg.norm``) add in an order set by a layout. Layout 1 reduces all N
|
|
6
|
+
entries at once; layout 2 reduces the two halves separately and adds them,
|
|
7
|
+
as two cores would. Elementwise ops and the BSR matvec are unchanged (the
|
|
8
|
+
matvec stands in for an LNC-invariant kernel), and a copy to the host
|
|
9
|
+
returns a plain tensor. Results that are bitwise equal across the layouts
|
|
10
|
+
do not depend on the device's reduction order.
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
from __future__ import annotations
|
|
14
|
+
|
|
15
|
+
import contextlib
|
|
16
|
+
|
|
17
|
+
import numpy as np
|
|
18
|
+
import pytest
|
|
19
|
+
import torch
|
|
20
|
+
|
|
21
|
+
import trnsparse
|
|
22
|
+
from trnsparse import _reduce
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class _Dev(torch.Tensor):
|
|
26
|
+
layout = 1
|
|
27
|
+
|
|
28
|
+
@classmethod
|
|
29
|
+
def __torch_function__(cls, func, types, args=(), kwargs=None):
|
|
30
|
+
kwargs = kwargs or {}
|
|
31
|
+
name = getattr(func, "__name__", "")
|
|
32
|
+
with torch._C.DisableTorchFunctionSubclass():
|
|
33
|
+
out = _split(name, args, kwargs) if cls.layout == 2 else None
|
|
34
|
+
if out is None:
|
|
35
|
+
out = func(*args, **kwargs)
|
|
36
|
+
if not isinstance(out, torch.Tensor):
|
|
37
|
+
return out
|
|
38
|
+
host = _to_host(name, args, kwargs)
|
|
39
|
+
return out if host else out.as_subclass(_Dev)
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def _to_host(name, args, kwargs):
|
|
43
|
+
if name == "cpu":
|
|
44
|
+
return True
|
|
45
|
+
if name != "to":
|
|
46
|
+
return False
|
|
47
|
+
dev = kwargs.get("device")
|
|
48
|
+
for a in args[1:]:
|
|
49
|
+
if isinstance(a, (torch.device, str)):
|
|
50
|
+
dev = a
|
|
51
|
+
return dev is not None and torch.device(dev).type == "cpu"
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def _split(name, args, kwargs):
|
|
55
|
+
if name in ("dot", "vdot") and len(args) == 2 and args[0].dim() == 1 and args[0].shape[0] > 1:
|
|
56
|
+
u, v = args
|
|
57
|
+
h = u.shape[0] // 2
|
|
58
|
+
f = torch.vdot if name == "vdot" else torch.dot
|
|
59
|
+
return f(u[:h], v[:h]) + f(u[h:], v[h:])
|
|
60
|
+
if name == "linalg_norm" and len(args) == 1 and not kwargs and args[0].dim() == 1:
|
|
61
|
+
t = args[0]
|
|
62
|
+
if t.shape[0] > 1:
|
|
63
|
+
h = t.shape[0] // 2
|
|
64
|
+
return torch.sqrt(t[:h].square().sum() + t[h:].square().sum())
|
|
65
|
+
return None
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
@contextlib.contextmanager
|
|
69
|
+
def _layout(k):
|
|
70
|
+
old, _Dev.layout = _Dev.layout, k
|
|
71
|
+
try:
|
|
72
|
+
yield
|
|
73
|
+
finally:
|
|
74
|
+
_Dev.layout = old
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def _plain(t):
|
|
78
|
+
return t.as_subclass(torch.Tensor)
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def _banded_spd_bsr(n, bs=32, seed=0):
|
|
82
|
+
"""A banded SPD kernel matrix (compact-support stand-in for a GP) in BSR."""
|
|
83
|
+
g = torch.Generator().manual_seed(seed)
|
|
84
|
+
x = torch.sort(torch.rand(n, generator=g)).values * (n / 50)
|
|
85
|
+
d = (x[:, None] - x[None, :]).abs()
|
|
86
|
+
K = torch.clamp(1 - d / 3, min=0) ** 2 # Wendland-like, compact support
|
|
87
|
+
A = K + 0.1 * torch.eye(n)
|
|
88
|
+
return A, trnsparse.BSRMatrix.from_dense(A.float(), block_size=bs)
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def _solve(red, n=2048, M=None):
|
|
92
|
+
A, A_bsr = _banded_spd_bsr(n)
|
|
93
|
+
b = torch.randn(n, generator=torch.Generator().manual_seed(1)).as_subclass(_Dev)
|
|
94
|
+
x, it, rel = trnsparse.cg_bsr(A_bsr, b, tol=1e-6, max_iter=500, M=M, reduction=red)
|
|
95
|
+
return _plain(x), it, rel, A
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def _both(fn):
|
|
99
|
+
with _layout(1):
|
|
100
|
+
a = fn()
|
|
101
|
+
with _layout(2):
|
|
102
|
+
b = fn()
|
|
103
|
+
return a, b
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
class TestLayoutInvariance:
|
|
107
|
+
@pytest.mark.parametrize("red", ["pairwise", "host64"])
|
|
108
|
+
def test_bitwise(self, red):
|
|
109
|
+
(xa, ia, ra, A), (xb, ib, rb, _) = _both(lambda: _solve(red))
|
|
110
|
+
assert torch.equal(xa, xb) and ia == ib and ra == rb
|
|
111
|
+
b = torch.randn(xa.shape[0], generator=torch.Generator().manual_seed(1))
|
|
112
|
+
x64 = torch.linalg.solve(A.double(), b.double())
|
|
113
|
+
assert float((xa.double() - x64).norm() / x64.norm()) < 1e-3
|
|
114
|
+
assert ra < 1e-6
|
|
115
|
+
|
|
116
|
+
def test_device_mode_differs(self):
|
|
117
|
+
(xa, ia, ra, _), (xb, ib, rb, _) = _both(lambda: _solve("device"))
|
|
118
|
+
assert not (torch.equal(xa, xb) and ra == rb)
|
|
119
|
+
|
|
120
|
+
def test_jacobi_preconditioned_bitwise(self):
|
|
121
|
+
_, A_bsr = _banded_spd_bsr(2048)
|
|
122
|
+
dinv = (1.0 / trnsparse.bsr_diagonal(A_bsr)).as_subclass(_Dev)
|
|
123
|
+
(xa, ia, *_), (xb, ib, *_) = _both(lambda: _solve("pairwise", M=lambda r: dinv * r))
|
|
124
|
+
assert torch.equal(xa, xb) and ia == ib
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
class TestModes:
|
|
128
|
+
@pytest.mark.parametrize("red", _reduce.REDUCTIONS)
|
|
129
|
+
def test_modes_agree(self, red):
|
|
130
|
+
A, A_bsr = _banded_spd_bsr(1000, seed=2)
|
|
131
|
+
b = torch.randn(1000, generator=torch.Generator().manual_seed(2))
|
|
132
|
+
x, it, rel = trnsparse.cg_bsr(A_bsr, b, tol=1e-6, reduction=red)
|
|
133
|
+
x0, it0, _ = trnsparse.cg_bsr(A_bsr, b, tol=1e-6, reduction="device")
|
|
134
|
+
# On host tensors the folded modes form float64 products while
|
|
135
|
+
# "device" is an fp32 torch.dot, so the modes do different
|
|
136
|
+
# arithmetic here. For this system (cond ~1e3) fp32 CG converges
|
|
137
|
+
# late by an amount that moves with the rounding of the dots and of
|
|
138
|
+
# the fp32 matvec: pairwise / device take 145 / 153 iterations on
|
|
139
|
+
# Linux (MKL) and 151 / 152 on macOS (Accelerate); fp64 CG takes
|
|
140
|
+
# ~108. The counts agree only to a few percent, so the solutions
|
|
141
|
+
# are checked against float64 instead.
|
|
142
|
+
assert rel < 1e-6 and abs(it - it0) <= max(2, it0 // 10), (it, it0)
|
|
143
|
+
x64 = torch.linalg.solve(A.double(), b.double())
|
|
144
|
+
for xs in (x, x0):
|
|
145
|
+
assert float((xs.double() - x64).norm() / x64.norm()) < 1e-4
|
|
146
|
+
assert torch.allclose(x, x0, rtol=1e-3, atol=1e-3)
|
|
147
|
+
|
|
148
|
+
@pytest.mark.parametrize("n", [1, 7, 64, 65, 1000, 4097, 30011])
|
|
149
|
+
def test_fold_matches_numpy_tree(self, n):
|
|
150
|
+
g = torch.Generator().manual_seed(n)
|
|
151
|
+
p = torch.randn(n, generator=g)
|
|
152
|
+
lev, m = _reduce.fold_plan(n)
|
|
153
|
+
t = np.zeros(m << lev, dtype=np.float32)
|
|
154
|
+
t[:n] = p.numpy()
|
|
155
|
+
for _ in range(lev):
|
|
156
|
+
h = t.shape[0] // 2
|
|
157
|
+
t = t[:h] + t[h:]
|
|
158
|
+
assert float(_reduce.fold_host64(p)) == float(t.astype(np.float64).sum())
|
|
159
|
+
|
|
160
|
+
def test_helpers_accuracy(self):
|
|
161
|
+
g = torch.Generator().manual_seed(3)
|
|
162
|
+
u, v = torch.randn(100003, generator=g), torch.randn(100003, generator=g)
|
|
163
|
+
ref = float((u.double() * v.double()).sum())
|
|
164
|
+
scale = float((u.double() * v.double()).abs().sum())
|
|
165
|
+
nref = float(u.double().norm())
|
|
166
|
+
for red in ("pairwise", "host64"):
|
|
167
|
+
assert abs(_reduce.dot(u, v, red) - ref) <= 1e-12 * scale
|
|
168
|
+
assert abs(_reduce.norm(u, red) - nref) <= 1e-12 * nref
|
|
169
|
+
assert abs(_reduce.dot(u, v, "device") - ref) <= 1e-5 * scale
|
|
170
|
+
|
|
171
|
+
def test_grad_path_takes_reduction(self):
|
|
172
|
+
A, A_bsr = _banded_spd_bsr(512, seed=4)
|
|
173
|
+
b = torch.randn(512, generator=torch.Generator().manual_seed(4), requires_grad=True)
|
|
174
|
+
x, _, _ = trnsparse.cg_bsr(A_bsr, b, tol=1e-10, max_iter=500, reduction="pairwise")
|
|
175
|
+
x.sum().backward()
|
|
176
|
+
ref = torch.linalg.solve(A.T.double(), torch.ones(512, dtype=torch.float64))
|
|
177
|
+
assert torch.allclose(b.grad.double(), ref, rtol=1e-3, atol=1e-4)
|
|
178
|
+
|
|
179
|
+
def test_env_and_validation(self, monkeypatch):
|
|
180
|
+
monkeypatch.delenv("TRNSPARSE_CG_REDUCTION", raising=False)
|
|
181
|
+
assert _reduce.reduction_mode(None, "TRNSPARSE_CG_REDUCTION", "device") == "device"
|
|
182
|
+
monkeypatch.setenv("TRNSPARSE_CG_REDUCTION", "Pairwise")
|
|
183
|
+
assert _reduce.reduction_mode(None, "TRNSPARSE_CG_REDUCTION", "device") == "pairwise"
|
|
184
|
+
monkeypatch.setenv("TRNSPARSE_CG_REDUCTION", "fast")
|
|
185
|
+
A_bsr = trnsparse.BSRMatrix.from_dense(torch.eye(32) * 2, block_size=32)
|
|
186
|
+
with pytest.raises(ValueError, match="reduction"):
|
|
187
|
+
trnsparse.cg_bsr(A_bsr, torch.ones(32))
|
|
188
|
+
monkeypatch.delenv("TRNSPARSE_CG_REDUCTION")
|
|
189
|
+
with pytest.raises(ValueError, match="reduction"):
|
|
190
|
+
trnsparse.cg_bsr(A_bsr, torch.ones(32), reduction="exact")
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
"""The fused CG / power kernels' reads of A's blocks at the 4 GiB rule (#121).
|
|
2
|
+
|
|
3
|
+
`blocks_t` (128, n_blocks * 128 * parts) has memory rows of 32 MiB or more
|
|
4
|
+
once fp32 A stores 65,536 blocks. Device reads over 128 partitions with that
|
|
5
|
+
step are misdirected on Trn2 (aws-neuron-sdk#1457), so the kernels read such
|
|
6
|
+
a blocks_t in row blocks of at most 64 partitions. CPU checks of the rule and
|
|
7
|
+
of the kernel source; the device checks are in tests/test_nki_cg_fused.py.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
import ast
|
|
13
|
+
import inspect
|
|
14
|
+
import textwrap
|
|
15
|
+
|
|
16
|
+
import pytest
|
|
17
|
+
|
|
18
|
+
from trnsparse.nki import kernels as K
|
|
19
|
+
|
|
20
|
+
MIB = 1 << 20
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
@pytest.mark.parametrize(
|
|
24
|
+
"row_bytes,rows",
|
|
25
|
+
[
|
|
26
|
+
(512, 128), # one fp32 block
|
|
27
|
+
(65535 * 512, 128), # 65,535 fp32 blocks: 32 MiB - 512 B
|
|
28
|
+
(65536 * 512, 64), # 65,536 fp32 blocks: 32 MiB, 128 * row = 2**32
|
|
29
|
+
(32 * MIB + 16 * 1024, 64), # 64 rows span exactly 2 GiB + 1 MiB
|
|
30
|
+
(32 * MIB + 16 * 1024 + 4, 32),
|
|
31
|
+
(64 * MIB, 32),
|
|
32
|
+
(64 * MIB + 32 * 1024 + 1, 16),
|
|
33
|
+
(262144 * 512, 16), # T = 512 dense: every block stored (128 MiB rows)
|
|
34
|
+
(131071 * 256, 128), # bf16 A: 131,071 blocks
|
|
35
|
+
(131072 * 256, 64), # bf16 A: 131,072 blocks
|
|
36
|
+
(3 << 31, 1), # a 6 GiB row: one partition per read
|
|
37
|
+
],
|
|
38
|
+
)
|
|
39
|
+
def test_dma_row_block_rule(row_bytes, rows):
|
|
40
|
+
got = K.dma_row_block(128, row_bytes)
|
|
41
|
+
assert got == rows
|
|
42
|
+
if got < 128:
|
|
43
|
+
assert 128 % got == 0 and got <= 64
|
|
44
|
+
assert got == 1 or got * row_bytes <= K._DMA_BLOCK_BYTES
|
|
45
|
+
assert 128 * row_bytes >= 1 << 32
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def test_dma_row_block_fewer_rows():
|
|
49
|
+
# The rule counts min(rows, 128) partitions.
|
|
50
|
+
assert K.dma_row_block(32, 64 * MIB) == 32
|
|
51
|
+
assert K.dma_row_block(64, 64 * MIB) == 32 # 64 rows of 64 MiB span 4 GiB
|
|
52
|
+
assert K.dma_row_block(128, 64 * MIB) == 32
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def _cg_body_tree():
|
|
56
|
+
src = K.__file__
|
|
57
|
+
with open(src) as f:
|
|
58
|
+
mod = ast.parse(f.read())
|
|
59
|
+
for node in ast.walk(mod):
|
|
60
|
+
if isinstance(node, ast.FunctionDef) and node.name == "_cg_bsr_body":
|
|
61
|
+
return node
|
|
62
|
+
raise AssertionError("_cg_bsr_body not found")
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def test_cg_kernel_reads_blocks_only_in_row_blocks():
|
|
66
|
+
"""Every read of blocks_t in the CG / power body goes through load_blocks,
|
|
67
|
+
which splits it into row blocks above the rule (#121). Through 0.15.0 the
|
|
68
|
+
body read blocks_t over all 128 partitions at every size."""
|
|
69
|
+
body = _cg_body_tree()
|
|
70
|
+
loader = next(
|
|
71
|
+
(n for n in ast.walk(body) if isinstance(n, ast.FunctionDef) and n.name == "load_blocks"),
|
|
72
|
+
None,
|
|
73
|
+
)
|
|
74
|
+
assert loader is not None, "_cg_bsr_body has no load_blocks"
|
|
75
|
+
inside = {id(n) for n in ast.walk(loader)}
|
|
76
|
+
reads = [
|
|
77
|
+
n
|
|
78
|
+
for n in ast.walk(body)
|
|
79
|
+
if isinstance(n, ast.Subscript)
|
|
80
|
+
and isinstance(n.value, ast.Name)
|
|
81
|
+
and n.value.id == "blocks_t"
|
|
82
|
+
]
|
|
83
|
+
assert reads, "no blocks_t reads found"
|
|
84
|
+
outside = [ast.unparse(n) for n in reads if id(n) not in inside]
|
|
85
|
+
assert not outside, f"blocks_t read outside load_blocks: {outside}"
|
|
86
|
+
# The split reads slice the partition axis by the row block.
|
|
87
|
+
split = [ast.unparse(n) for n in reads if "a_rows" in ast.unparse(n.slice)]
|
|
88
|
+
assert split, "load_blocks never reads a row block of blocks_t"
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def test_dma_row_block_is_plain_python():
|
|
92
|
+
"""The rule runs on the host at trace time, so it must not need NKI."""
|
|
93
|
+
assert "nl." not in textwrap.dedent(inspect.getsource(K.dma_row_block))
|
|
@@ -305,11 +305,14 @@ def _case(api, dtype):
|
|
|
305
305
|
A = trnsparse.BSRMatrix.from_dense(Ad, block_size=P)
|
|
306
306
|
return lambda: trnsparse.power_iteration_bsr_fused(A, max_iter=3)[1]
|
|
307
307
|
if api in ("kernel_matmul", "kernel_matmul_cross"):
|
|
308
|
-
|
|
308
|
+
# Points are real (complex points raise TypeError, #120); a complex
|
|
309
|
+
# dtype is a complex V on real points of the matching real dtype.
|
|
310
|
+
pdt = dtype.to_real() if dtype.is_complex else dtype
|
|
311
|
+
X = _rnd((40, 3), pdt, g)
|
|
309
312
|
V = _rnd((40, 2), dtype, g)
|
|
310
313
|
if api == "kernel_matmul":
|
|
311
314
|
return lambda: trnsparse.kernel_matmul(X, None, V, noise=0.1)
|
|
312
|
-
Y = _rnd((30, 3),
|
|
315
|
+
Y = _rnd((30, 3), pdt, g)
|
|
313
316
|
return lambda: trnsparse.kernel_matmul(Y, X, V)
|
|
314
317
|
raise AssertionError(api)
|
|
315
318
|
|
|
@@ -335,6 +338,8 @@ def _outcome(fn):
|
|
|
335
338
|
# float64 only (complex is #85).
|
|
336
339
|
FP64_APIS = {api: {torch.float64} for api in APIS}
|
|
337
340
|
FP64_APIS["bsr_spmm"] = FP64_APIS["cg_bsr"] = {torch.float64, torch.complex128}
|
|
341
|
+
# complex128 V is two float64 products (#120), so the float64 policy decides it.
|
|
342
|
+
FP64_APIS["kernel_matmul"] = FP64_APIS["kernel_matmul_cross"] = {torch.float64, torch.complex128}
|
|
338
343
|
FUSED = {"cg_bsr_fused", "power_fused"}
|
|
339
344
|
|
|
340
345
|
|
|
@@ -367,6 +372,8 @@ def test_no_unsupported_dtype_reaches_a_kernel(stubs, monkeypatch, api, dtype, r
|
|
|
367
372
|
routed = api in ROUTED_APIS and dtype in NO_KERNEL
|
|
368
373
|
if api == "bsr_spmm" and dtype.is_complex and hasattr(dispatch, "_nki_bsr_spmm_complex"):
|
|
369
374
|
routed = False # #80: complex BSR SpMM as real kernel launches
|
|
375
|
+
if api.startswith("kernel_matmul") and dtype.is_complex:
|
|
376
|
+
routed = False # #120: complex V as one real launch on [Re V, Im V]
|
|
370
377
|
if require and (routed or _fp64_policy(api, dtype, path)):
|
|
371
378
|
assert isinstance(err, NotImplementedError), (err, got)
|
|
372
379
|
return
|