trnsolver 0.22.1__tar.gz → 0.23.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (104) hide show
  1. {trnsolver-0.22.1 → trnsolver-0.23.0}/CHANGELOG.md +87 -0
  2. {trnsolver-0.22.1 → trnsolver-0.23.0}/CONTRIBUTING.md +11 -0
  3. {trnsolver-0.22.1 → trnsolver-0.23.0}/PKG-INFO +1 -1
  4. {trnsolver-0.22.1 → trnsolver-0.23.0}/docs/api/factor.md +11 -13
  5. {trnsolver-0.22.1 → trnsolver-0.23.0}/docs/api/iterative.md +28 -5
  6. {trnsolver-0.22.1 → trnsolver-0.23.0}/docs/api/nki.md +1 -0
  7. {trnsolver-0.22.1 → trnsolver-0.23.0}/pyproject.toml +2 -2
  8. {trnsolver-0.22.1 → trnsolver-0.23.0}/scripts/bench_gp_precond.py +104 -15
  9. trnsolver-0.23.0/scripts/probe_cg_reduction.py +219 -0
  10. trnsolver-0.23.0/tests/test_block_neuron.py +246 -0
  11. {trnsolver-0.22.1 → trnsolver-0.23.0}/tests/test_cg_block.py +29 -0
  12. trnsolver-0.23.0/tests/test_cg_reduction.py +520 -0
  13. {trnsolver-0.22.1 → trnsolver-0.23.0}/tests/test_conformance.py +15 -9
  14. {trnsolver-0.22.1 → trnsolver-0.23.0}/tests/test_factor.py +40 -38
  15. {trnsolver-0.22.1 → trnsolver-0.23.0}/tests/test_kernel_dtype_guard.py +0 -2
  16. {trnsolver-0.22.1 → trnsolver-0.23.0}/tests/test_subspace.py +11 -0
  17. {trnsolver-0.22.1 → trnsolver-0.23.0}/trnsolver/__init__.py +1 -1
  18. {trnsolver-0.22.1 → trnsolver-0.23.0}/trnsolver/block.py +254 -31
  19. {trnsolver-0.22.1 → trnsolver-0.23.0}/trnsolver/factor.py +12 -51
  20. {trnsolver-0.22.1 → trnsolver-0.23.0}/trnsolver/iterative.py +91 -21
  21. {trnsolver-0.22.1 → trnsolver-0.23.0}/trnsolver/nki/subspace.py +3 -2
  22. {trnsolver-0.22.1 → trnsolver-0.23.0}/trnsolver/subspace.py +11 -1
  23. {trnsolver-0.22.1 → trnsolver-0.23.0}/trnsolver.egg-info/PKG-INFO +1 -1
  24. {trnsolver-0.22.1 → trnsolver-0.23.0}/trnsolver.egg-info/SOURCES.txt +2 -0
  25. trnsolver-0.22.1/tests/test_block_neuron.py +0 -135
  26. {trnsolver-0.22.1 → trnsolver-0.23.0}/.github/ISSUE_TEMPLATE/bug_report.md +0 -0
  27. {trnsolver-0.22.1 → trnsolver-0.23.0}/.github/ISSUE_TEMPLATE/feature_request.md +0 -0
  28. {trnsolver-0.22.1 → trnsolver-0.23.0}/.github/pull_request_template.md +0 -0
  29. {trnsolver-0.22.1 → trnsolver-0.23.0}/.github/workflows/ci.yml +0 -0
  30. {trnsolver-0.22.1 → trnsolver-0.23.0}/.github/workflows/neuron.yml +0 -0
  31. {trnsolver-0.22.1 → trnsolver-0.23.0}/.github/workflows/notify-umbrella.yml +0 -0
  32. {trnsolver-0.22.1 → trnsolver-0.23.0}/.github/workflows/publish.yml +0 -0
  33. {trnsolver-0.22.1 → trnsolver-0.23.0}/.gitignore +0 -0
  34. {trnsolver-0.22.1 → trnsolver-0.23.0}/.pre-commit-config.yaml +0 -0
  35. {trnsolver-0.22.1 → trnsolver-0.23.0}/CLAUDE.md +0 -0
  36. {trnsolver-0.22.1 → trnsolver-0.23.0}/CODE_OF_CONDUCT.md +0 -0
  37. {trnsolver-0.22.1 → trnsolver-0.23.0}/LICENSE +0 -0
  38. {trnsolver-0.22.1 → trnsolver-0.23.0}/README.md +0 -0
  39. {trnsolver-0.22.1 → trnsolver-0.23.0}/benchmarks/bench_cuda.py +0 -0
  40. {trnsolver-0.22.1 → trnsolver-0.23.0}/benchmarks/bench_solver.py +0 -0
  41. {trnsolver-0.22.1 → trnsolver-0.23.0}/benchmarks/conftest.py +0 -0
  42. {trnsolver-0.22.1 → trnsolver-0.23.0}/docs/api/eigen.md +0 -0
  43. {trnsolver-0.22.1 → trnsolver-0.23.0}/docs/architecture.md +0 -0
  44. {trnsolver-0.22.1 → trnsolver-0.23.0}/docs/aws_setup.md +0 -0
  45. {trnsolver-0.22.1 → trnsolver-0.23.0}/docs/benchmarks.md +0 -0
  46. {trnsolver-0.22.1 → trnsolver-0.23.0}/docs/developing_kernels.md +0 -0
  47. {trnsolver-0.22.1 → trnsolver-0.23.0}/docs/index.md +0 -0
  48. {trnsolver-0.22.1 → trnsolver-0.23.0}/docs/installation.md +0 -0
  49. {trnsolver-0.22.1 → trnsolver-0.23.0}/docs/quickstart.md +0 -0
  50. {trnsolver-0.22.1 → trnsolver-0.23.0}/examples/df_store.py +0 -0
  51. {trnsolver-0.22.1 → trnsolver-0.23.0}/examples/scf_df_mp2.py +0 -0
  52. {trnsolver-0.22.1 → trnsolver-0.23.0}/examples/scf_df_rhf.py +0 -0
  53. {trnsolver-0.22.1 → trnsolver-0.23.0}/examples/scf_eigen.py +0 -0
  54. {trnsolver-0.22.1 → trnsolver-0.23.0}/infra/terraform/README.md +0 -0
  55. {trnsolver-0.22.1 → trnsolver-0.23.0}/infra/terraform/gpu.tf +0 -0
  56. {trnsolver-0.22.1 → trnsolver-0.23.0}/infra/terraform/main.tf +0 -0
  57. {trnsolver-0.22.1 → trnsolver-0.23.0}/mkdocs.yml +0 -0
  58. {trnsolver-0.22.1 → trnsolver-0.23.0}/scripts/bench_eager_handle.py +0 -0
  59. {trnsolver-0.22.1 → trnsolver-0.23.0}/scripts/bench_jacobi.py +0 -0
  60. {trnsolver-0.22.1 → trnsolver-0.23.0}/scripts/bench_jacobi_multicore.py +0 -0
  61. {trnsolver-0.22.1 → trnsolver-0.23.0}/scripts/bench_occupied_subspace.py +0 -0
  62. {trnsolver-0.22.1 → trnsolver-0.23.0}/scripts/bench_scf.py +0 -0
  63. {trnsolver-0.22.1 → trnsolver-0.23.0}/scripts/bench_scf_ladder.py +0 -0
  64. {trnsolver-0.22.1 → trnsolver-0.23.0}/scripts/probe_large_n_split.py +0 -0
  65. {trnsolver-0.22.1 → trnsolver-0.23.0}/scripts/run_cuda_tests.sh +0 -0
  66. {trnsolver-0.22.1 → trnsolver-0.23.0}/scripts/run_neuron_tests.sh +0 -0
  67. {trnsolver-0.22.1 → trnsolver-0.23.0}/scripts/run_simulator_tests.sh +0 -0
  68. {trnsolver-0.22.1 → trnsolver-0.23.0}/scripts/stop_neuron_ci.sh +0 -0
  69. {trnsolver-0.22.1 → trnsolver-0.23.0}/setup.cfg +0 -0
  70. {trnsolver-0.22.1 → trnsolver-0.23.0}/tests/conftest.py +0 -0
  71. {trnsolver-0.22.1 → trnsolver-0.23.0}/tests/test_bench_scf_ladder.py +0 -0
  72. {trnsolver-0.22.1 → trnsolver-0.23.0}/tests/test_compile.py +0 -0
  73. {trnsolver-0.22.1 → trnsolver-0.23.0}/tests/test_complex_rhs.py +0 -0
  74. {trnsolver-0.22.1 → trnsolver-0.23.0}/tests/test_dtype.py +0 -0
  75. {trnsolver-0.22.1 → trnsolver-0.23.0}/tests/test_eigen.py +0 -0
  76. {trnsolver-0.22.1 → trnsolver-0.23.0}/tests/test_eigh_cutover.py +0 -0
  77. {trnsolver-0.22.1 → trnsolver-0.23.0}/tests/test_eigh_neuron.py +0 -0
  78. {trnsolver-0.22.1 → trnsolver-0.23.0}/tests/test_hermitian_batch.py +0 -0
  79. {trnsolver-0.22.1 → trnsolver-0.23.0}/tests/test_iterative.py +0 -0
  80. {trnsolver-0.22.1 → trnsolver-0.23.0}/tests/test_iterative_autograd.py +0 -0
  81. {trnsolver-0.22.1 → trnsolver-0.23.0}/tests/test_iterative_rhs_scale.py +0 -0
  82. {trnsolver-0.22.1 → trnsolver-0.23.0}/tests/test_jacobi.py +0 -0
  83. {trnsolver-0.22.1 → trnsolver-0.23.0}/tests/test_jacobi_onezero_arg.py +0 -0
  84. {trnsolver-0.22.1 → trnsolver-0.23.0}/tests/test_native_dispatch.py +0 -0
  85. {trnsolver-0.22.1 → trnsolver-0.23.0}/tests/test_nki_compile.py +0 -0
  86. {trnsolver-0.22.1 → trnsolver-0.23.0}/tests/test_nki_native.py +0 -0
  87. {trnsolver-0.22.1 → trnsolver-0.23.0}/tests/test_nki_sim.py +0 -0
  88. {trnsolver-0.22.1 → trnsolver-0.23.0}/tests/test_nki_tail_audit.py +0 -0
  89. {trnsolver-0.22.1 → trnsolver-0.23.0}/tests/test_orthogonalize.py +0 -0
  90. {trnsolver-0.22.1 → trnsolver-0.23.0}/tests/test_scf_df_mp2.py +0 -0
  91. {trnsolver-0.22.1 → trnsolver-0.23.0}/tests/test_scf_df_rhf.py +0 -0
  92. {trnsolver-0.22.1 → trnsolver-0.23.0}/tests/test_trace_cache_safety.py +0 -0
  93. {trnsolver-0.22.1 → trnsolver-0.23.0}/trnsolver/_jacobi.py +0 -0
  94. {trnsolver-0.22.1 → trnsolver-0.23.0}/trnsolver/eigen.py +0 -0
  95. {trnsolver-0.22.1 → trnsolver-0.23.0}/trnsolver/nki/__init__.py +0 -0
  96. {trnsolver-0.22.1 → trnsolver-0.23.0}/trnsolver/nki/_native.py +0 -0
  97. {trnsolver-0.22.1 → trnsolver-0.23.0}/trnsolver/nki/dispatch.py +0 -0
  98. {trnsolver-0.22.1 → trnsolver-0.23.0}/trnsolver/nki/eigh_pool.py +0 -0
  99. {trnsolver-0.22.1 → trnsolver-0.23.0}/trnsolver/nki/jacobi.py +0 -0
  100. {trnsolver-0.22.1 → trnsolver-0.23.0}/trnsolver/orthogonalize.py +0 -0
  101. {trnsolver-0.22.1 → trnsolver-0.23.0}/trnsolver.egg-info/dependency_links.txt +0 -0
  102. {trnsolver-0.22.1 → trnsolver-0.23.0}/trnsolver.egg-info/requires.txt +0 -0
  103. {trnsolver-0.22.1 → trnsolver-0.23.0}/trnsolver.egg-info/top_level.txt +0 -0
  104. {trnsolver-0.22.1 → trnsolver-0.23.0}/uv.lock +0 -0
@@ -7,6 +7,93 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [0.23.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: `solve_spd` reads `B.shape == A.shape[:-1]` as a batch of vectors in every case, as `torch.linalg.solve` does (#130, completes #79; breaking for the formerly ambiguous shape). `cg`, `gmres` and `cg_block` reductions over n no longer depend on the LNC split (#128). `pivoted_cholesky(pad_rows=True)` keeps a device `rows_fn` to a few shapes (trnsci/trnsci#109). `occupied_subspace` raises on complex F (#132).
15
+
16
+ ### Added
17
+
18
+ - **`pivoted_cholesky(..., pad_rows=True)`: a few data-independent `rows_fn`
19
+ shapes (trnsci/trnsci#109).** With `block` > 1, the number of pivots a
20
+ round accepts depends on the values of K, so a device `rows_fn` (one NEFF
21
+ per shape) compiled in almost every round. A compile-only pre-warm could
22
+ not reach those shapes, because the pivots depend on the data. The GP n = 1e6
23
+ build hit 57 compiles in 63 calls and went from 143 s to 207 s.
24
+ `pad_rows=True` pads each call to a power of two, capped at `block`, by
25
+ repeating the last index, and drops the extra rows. That leaves at most
26
+ ⌈log₂ block⌉ + 1 shapes. The factor is bitwise unchanged. The default is
27
+ False, so `rows_fn` callers that need distinct indices are unaffected.
28
+ Trn2, n = 1e5, cold NEFF cache, one logical core: 21 → 3 shapes, rows
29
+ 83.6 → 16.5 s, build 100.3 → 29.1 s.
30
+ - **`scripts/bench_gp_precond.py`:** `--pad-rows` (on by default), `--prewarm`
31
+ (compiles every padded shape first, which also works in compile-only mode),
32
+ and `host_s`, the host fp64 part of `cholesky_s`. `cholesky_s` includes the
33
+ device rows (`rows_s`). Read that way, the window-5 "cholesky 150 s against
34
+ 90 s" was 82 s against 80 s on the host, plus the cold compiles.
35
+
36
+ ### Changed
37
+
38
+ - **`solve_spd` reads an ambiguous `B` as a batch of vectors, as
39
+ `torch.linalg.solve` does (#130, completes #79). Breaking for callers who
40
+ pass that shape.** With A `(*, n, n, n)` and B `(*, n, n)` (`B.shape ==
41
+ A.shape[:-1]` and A's last batch dim equals n), `B` is now `(*, n)`
42
+ vectors and the result is `(*, n, n)`. 0.22.1 and earlier read it as
43
+ `(*, n, k)` matrices broadcast over A's batch and returned `(*, n, n, n)`;
44
+ 0.22.1 warned with a `FutureWarning` (#131), which is removed. For the old
45
+ result, pass `B.unsqueeze(-3)` for a 3-D A, or `B.unsqueeze(0)` in
46
+ general; both mean the same in every release. The same holds with
47
+ `iterative_refinement=True`. The conformance case `solve_spd_vec-batch{N}`
48
+ passes (it was `xfail(strict=True)`).
49
+
50
+ ### Fixed
51
+
52
+ - **`cg` and `gmres` no longer depend on the LNC split (#128).** They take
53
+ `reduction=` as `cg_block` does: `"pairwise"` (the default for both),
54
+ `"host64"` or `"device"` (the previous code). `None` reads
55
+ `TRNSOLVER_CG_REDUCTION` for `cg` and `TRNSOLVER_GMRES_REDUCTION` for
56
+ `gmres`. On the device, `rᴴz`, `pᴴAp`, the norms and the Gram-Schmidt
57
+ dots are now elementwise products, folded by the same fixed tree and
58
+ finished in float64 on the host. `gmres` also forms `x + V y` as
59
+ fixed-order axpys. Host tensors keep their float64 products. Trn2, LNC=2,
60
+ n = 1e5, the GP `KernelOperator` on one vector (289 ms per call):
61
+ `cg` goes from 299.7 to 299.9 ms per iteration (+0.1%), and `gmres` from
62
+ 375.9 to 337.6 ms (−10%, because the old path cast to float64 on the
63
+ device for every dot).
64
+ - **`cg_block` no longer depends on the LNC split (#128).** Device aten
65
+ reductions over n (`sum`, `vector_norm`, matmul) add in an order that
66
+ depends on how the work is split across logical cores. On Trn2 at
67
+ n = 1e5, R = 33, `cg_block`'s inner products differed by 4.9e-4 between
68
+ LNC=1 and LNC=2, and the GP posterior mean by 4.5e-4. The new
69
+ `reduction=` argument (or `TRNSOLVER_CG_REDUCTION`) chooses how the inner
70
+ products and norms reduce over n:
71
+ - `"pairwise"` (the new default): zero-pad the rows to m · 2^L (m ≤ 64),
72
+ fold with L elementwise adds of contiguous halves on the device (a fixed
73
+ tree that depends on n only), then sum the m leaves in float64 on the
74
+ host. It is about 2x more accurate than the device sum.
75
+ - `"host64"`: copy the whole (n, R) product to the host and reduce it in
76
+ float64. This is the most accurate option, at one (n, R) D2H per
77
+ reduction.
78
+ - `"device"`: the previous device sums. They are fastest, but
79
+ LNC-dependent; this mode is kept for performance comparison.
80
+
81
+ The residual norm and rᵀz now share one fold, so the default costs two
82
+ small host syncs per iteration. `PivotedLowRankPreconditioner(apply=
83
+ "projected")` takes the same `reduction=` for its two Qᵀ V reductions
84
+ (#117). With `"pairwise"`, it forms them as 128-row block products in one
85
+ bmm, folded in the same fixed order. `pivoted_lowrank_preconditioner` and
86
+ `slq_logdet` pass `reduction` through. The float64 policy and
87
+ `TRNSOLVER_REQUIRE_NKI` behaviour are unchanged.
88
+
89
+ - **`occupied_subspace` with a complex Hermitian F raises `TypeError` (#132).**
90
+ 0.21.0 through 0.22.1 cast a complex64 / complex128 F to float32, dropping
91
+ its imaginary part, and returned the occupied subspace of `F.real` with no
92
+ error or warning (at n = 12: lowest eigenvalue -7.50 against the right
93
+ -7.14). The filtered subspace iteration is real, so complex F now raises;
94
+ use `eigh` for a complex Hermitian F. Integer and half-precision F are
95
+ still upcast to float32.
96
+
10
97
  ## [0.22.1] — 2026-11-07
11
98
 
12
99
  Validated on Trn2 (trn2.3xlarge, Neuron SDK 2.33.0, native TorchNeuron) — see trnsci/trnsci#35.
@@ -85,6 +85,17 @@ Before opening a PR:
85
85
  - [ ] If you added a public symbol, the project's `docs/api.md` lists it
86
86
  - [ ] PR description says *what* changed and *why*, not just *what*
87
87
 
88
+ ## Releasing
89
+
90
+ `.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.
91
+
92
+ - [ ] Release PR (`release: vX.Y.Z`): bump `version` in `pyproject.toml` and `__version__` in `trnsolver/__init__.py`, and move the `[Unreleased]` CHANGELOG entries under `## [X.Y.Z] - YYYY-MM-DD`.
93
+ - [ ] After the merge, run `gh release create vX.Y.Z --target <merge sha>`, then confirm the publish run and the new version on PyPI.
94
+ - [ ] **Update umbrella docs/extras** in one [trnsci/trnsci](https://github.com/trnsci/trnsci) PR:
95
+ - if users should require this release (always for a fix to silent wrong results), bump `trnsolver` in the umbrella `pyproject.toml` `[solver]` and `[all]` extras, and cut a `trnsci` meta-package release;
96
+ - update the `[[described:trnsolver=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;
97
+ - check that `python scripts/check_docs_drift.py` exits 0. The weekly docs-drift workflow opens a `docs-stale` issue for anything missed.
98
+
88
99
  ## Issues
89
100
 
90
101
  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: trnsolver
3
- Version: 0.22.1
3
+ Version: 0.23.0
4
4
  Summary: Linear solvers and eigendecomposition for AWS Trainium via NKI
5
5
  Author-email: Scott Friedman <scttfrdmn@gmail.com>
6
6
  License-Expression: Apache-2.0
@@ -28,23 +28,21 @@ Solves `A X = B` via Cholesky — faster than `solve` when `A` is SPD.
28
28
  `B` is `(n,)`, `(*, n, k)`, or a batch of vectors `(*, n)` with
29
29
  `B.shape == A.shape[:-1]`, as `torch.linalg.solve` reads it.
30
30
 
31
- !!! warning "Deprecated in 0.22.1: an ambiguous `B` shape (#79)"
32
- When A's last batch dim equals `n` (A `(*, n, n, n)`, B `(*, n, n)`), `B`
33
- can be read either way. `solve_spd` still reads it as `(*, n, k)`
34
- right-hand-side matrices broadcast over A's batch (result
35
- `(*, n, n, n)`), and now emits a `FutureWarning`. From 0.23.0 it reads
36
- it as a batch of vectors, as `torch.linalg.solve` does (result
37
- `(*, n, n)`; [#130](https://github.com/trnsci/trnsolver/issues/130)).
38
- Be explicit; both forms mean the same in every release:
31
+ This includes A `(*, n, n, n)` with B `(*, n, n)`, where A's last batch dim
32
+ equals `n`: `B` is a batch of vectors and `X` is `(*, n, n)`, as in
33
+ `torch.linalg.solve`.
34
+
35
+ !!! note "Changed in 0.23.0 (#130)"
36
+ trnsolver 0.22.1 and earlier read that `B` as `(*, n, k)` right-hand-side
37
+ matrices broadcast over A's batch (result `(*, n, n, n)`; 0.22.1 warned,
38
+ #79). For that result, pass the explicit form, which means the same in
39
+ every release:
39
40
 
40
41
  ```python
41
- X = trnsolver.solve_spd(A, B.unsqueeze(-1)) # batch of vectors -> (*, n, n, 1)
42
- X = trnsolver.solve_spd(A, B.unsqueeze(-3)) # one shared (n, k) right-hand side
42
+ X = trnsolver.solve_spd(A, B.unsqueeze(-3)) # 3-D A: one shared (n, k) right-hand side
43
+ X = trnsolver.solve_spd(A, B.unsqueeze(0)) # any A: the 0.22.1 reading
43
44
  ```
44
45
 
45
- For a 3-D `A`, `B.unsqueeze(-3)` is the current result; with more batch
46
- dims, `B.unsqueeze(0)` is.
47
-
48
46
  ## `inv_spd(A)`
49
47
 
50
48
  Returns `A⁻¹` for SPD `A` via Cholesky.
@@ -2,7 +2,7 @@
2
2
 
3
3
  Both solvers operate on the host (PyTorch). The matvec / inner-product hot path would benefit from NKI Level 1/2 kernels in `trnblas`, but the iteration loop itself stays on CPU.
4
4
 
5
- ## `cg(A, b, x0=None, tol=1e-6, maxiter=1000, M=None)`
5
+ ## `cg(A, b, x0=None, tol=1e-6, maxiter=1000, M=None, *, reduction=None)`
6
6
 
7
7
  Conjugate Gradient for SPD `A`. `A` may be a tensor or a callable `A(x) → A @ x`.
8
8
 
@@ -10,6 +10,7 @@ Conjugate Gradient for SPD `A`. `A` may be a tensor or a callable `A(x) → A @
10
10
  - **`tol`** — relative residual tolerance
11
11
  - **`maxiter`** — iteration cap
12
12
  - **`M`** — optional preconditioner (tensor or callable applying `M⁻¹`)
13
+ - **`reduction`** — how `rᴴz`, `pᴴAp` and `‖r‖` reduce over n (#128): `"pairwise"` (default), `"host64"` or `"device"`, as in [`cg_block`](#cg_blocka-b-mnone--tol1e-6-maxiternone-check_every10-x0none-return_coeffsfalse-reductionnone). `None` reads `TRNSOLVER_CG_REDUCTION`. Host tensors are multiplied in float64 in every mode, as before. On the device the product is in the working dtype, then folded and finished in float64 on the host.
13
14
 
14
15
  Returns `(x, iters, residual)`.
15
16
 
@@ -17,10 +18,21 @@ Returns `(x, iters, residual)`.
17
18
  x, iters, residual = trnsolver.cg(A_spd, b, tol=1e-8)
18
19
  ```
19
20
 
20
- ## `gmres(A, b, x0=None, tol=1e-6, maxiter=200, restart=30)`
21
+ ## `gmres(A, b, x0=None, tol=1e-6, maxiter=200, restart=30, *, reduction=None)`
21
22
 
22
23
  GMRES for general (non-symmetric) systems. Same calling convention as `cg`. `restart` controls the Krylov subspace size before restart.
23
24
 
25
+ `reduction` (#128) applies to the modified Gram-Schmidt inner products and the norms: `"pairwise"` (default), `"host64"` or `"device"`. With `"pairwise"` / `"host64"`, the update `x + V y` is formed as fixed-order axpys instead of a matmul. `None` reads `TRNSOLVER_GMRES_REDUCTION`.
26
+
27
+ Cost on Trn2 (LNC=2, one logical core, n = 1e5, the GP exemplar's `KernelOperator` applied to one vector, 289 ms per call, 60 iterations):
28
+
29
+ | solver | `device` ms/iter | `pairwise` ms/iter | `host64` ms/iter |
30
+ |---|---|---|---|
31
+ | `cg` | 299.7 | 299.9 (+0.1%) | 296.7 |
32
+ | `gmres` (restart 30) | 375.9 | 337.6 (−10%) | 324.6 |
33
+
34
+ `gmres` gets faster because the old path cast both vectors to float64 on the device for every Gram-Schmidt dot.
35
+
24
36
  ```python
25
37
  x, iters, residual = trnsolver.gmres(A, b, tol=1e-6)
26
38
  ```
@@ -38,9 +50,19 @@ Raises `ValueError` if any diagonal entry is within `1e-15` of zero. More-capabl
38
50
 
39
51
  ## Block CG on matrix-free operators (#97)
40
52
 
41
- ### `cg_block(A, B, M=None, *, tol=1e-6, maxiter=None, check_every=10, X0=None, return_coeffs=False)`
53
+ ### `cg_block(A, B, M=None, *, tol=1e-6, maxiter=None, check_every=10, X0=None, return_coeffs=False, reduction=None)`
54
+
55
+ Column-batched preconditioned CG for SPD `A` with an `(n, R)` right-hand side (or `(n,)`). `A` and `M` are callables on `(n, R)` blocks, or tensors. Each iteration makes **one** call `A(P)` on the whole block. Converged columns freeze, but the full block is still passed in, so every call has the same shape. Convergence is per column, `‖rⱼ‖ ≤ tol·‖bⱼ‖`. The stopping test reads back to the host only every `check_every` iterations.
56
+
57
+ `reduction` sets how the inner products and norms reduce over n (#128). `None` reads `TRNSOLVER_CG_REDUCTION`.
58
+
59
+ | `reduction` | How | LNC=1 vs LNC=2 | Cost |
60
+ |---|---|---|---|
61
+ | `"pairwise"` (default) | zero-pad to m · 2^L rows (m ≤ 64), L elementwise adds of contiguous halves on the device, then the m leaves summed in float64 on the host | bitwise equal | two small D2H syncs per iteration |
62
+ | `"host64"` | the whole (n, R) product is copied to the host and reduced in float64 | bitwise equal; most accurate | one (n, R) D2H per reduction |
63
+ | `"device"` | device `sum` / `vector_norm` (the previous behaviour) | differs (5e-4 relative at n = 1e5) | fastest; for comparison |
42
64
 
43
- Column-batched preconditioned CG for SPD `A` with an `(n, R)` right-hand side (or `(n,)`). `A` and `M` are callables on `(n, R)` blocks, or tensors. Each iteration makes **one** call `A(P)` on the whole block. Converged columns freeze, but the full block is still passed in, so every call has the same shape. Convergence is per column, `‖rⱼ‖ ≤ tol·‖bⱼ‖`. The host syncs only every `check_every` iterations.
65
+ Device reductions over n add in an order that depends on how the compiler splits the work across logical cores. Elementwise ops do not. So `"pairwise"` and `"host64"` give the same bits at any LNC, provided the operator `A` (and `M`) does too.
44
66
 
45
67
  Returns `(X, info)`. `info` has these keys:
46
68
 
@@ -115,7 +137,8 @@ How `"projected"` works:
115
137
  - It applies `Q (Σ² + sI)⁻¹ QᵀV + (I − QQᵀ)V/s` with the projection done twice.
116
138
  - One pass leaves an error of about u·√k·‖V‖ along Q, which the 1/s weight amplifies. That is the `"native"` failure. The second pass removes that error.
117
139
  - On CPU fp32 it takes the same CG iteration counts as `"host64"` on the GP cases that stall `"native"`.
140
+ - Its two QᵀV products reduce over n, so they take `reduction=` as `cg_block` does (`PivotedLowRankPreconditioner(L, shift, apply="projected", reduction=None)`; `None` reads `TRNSOLVER_CG_REDUCTION`). With `"pairwise"`, QᵀV is formed as 128-row block products `Q_iᵀ V_i` in one bmm, then the blocks are folded as above and finished in float64 on the host (#128).
118
141
 
119
- ### `lanczos_tridiag_from_cg(alpha, beta, col_iterations=None)`, `slq_logdet(A, n, num_probes=32, num_iters=50, generator=None, M=None, *, dtype=torch.float32, device=None, tol=1e-10)`
142
+ ### `lanczos_tridiag_from_cg(alpha, beta, col_iterations=None)`, `slq_logdet(A, n, num_probes=32, num_iters=50, generator=None, M=None, *, dtype=torch.float32, device=None, tol=1e-10, reduction=None)`
120
143
 
121
144
  `lanczos_tridiag_from_cg` builds the per-column Lanczos tridiagonals from CG coefficients. `slq_logdet` gives the stochastic Lanczos quadrature estimate of `log det(A)` and returns `(estimate, info)`. `info` has `std_error`, `quadratures` and `cg_info`.
@@ -81,6 +81,7 @@ The same rule holds across the trnsci suite (trnsci/trnsolver#89).
81
81
  | `TRNSOLVER_REQUIRE_NKI=1` | Kernel-path failures re-raise instead of silently falling back to PyTorch, and inputs no kernel takes (float64, integer, bool) raise `NotImplementedError` (see [Precision and dtypes](#precision-and-dtypes)). Used by the validation suite to catch silent kernel breakage. |
82
82
  | `TRNSOLVER_FP64_ON_DEVICE=1` | Under backend `"auto"`, float64 / complex128 operands on the `neuron` device stay there and compute with device aten at fp32 precision, instead of on the host at float64 (see [Precision and dtypes](#precision-and-dtypes)). For callers who prefer speed. Read at import. |
83
83
  | `TRNSOLVER_EIGH_NKI_MIN_N=<n>` | On the native `neuron` device (and for host tensors on a TorchNeuron host), `eigh` uses `torch.linalg.eigh` for matrices with fewer than `n` rows and the NKI path otherwise. The NKI path is single-tile (n ≤ 128) and falls back to `torch.linalg.eigh` above that. Default 512; `0` always uses NKI. Ignored when `TRNSOLVER_REQUIRE_NKI=1`. (#42) |
84
+ | `TRNSOLVER_CG_REDUCTION=pairwise\|host64\|device` | How `cg_block` and the projected preconditioner reduce over n when `reduction=` is not passed (#128). `pairwise` (default) and `host64` give the same bits at LNC=1 and LNC=2; `device` is the previous device-sum path, which is faster but LNC-dependent. See [`cg_block`](iterative.md). |
84
85
  | `TRNSOLVER_EIGH_METHOD=auto\|jacobi` | `eigh` method when `method=` is not passed. `jacobi` selects the batched Jacobi solver (#48). Default `auto`. |
85
86
  | `TRNSOLVER_JACOBI_GROUP=<g>` | Jacobi kernel: tiles per launch (rounded down to a power of two). Default 128 (n ≤ 32: scalar kernel; 33 ≤ n ≤ 128: block kernel), split across the two physical cores of a logical NeuronCore. |
86
87
  | `TRNSOLVER_JACOBI_CHAINS=<c>` | Jacobi kernel: independent dependency chains per step. Default 8 for n ≤ 32, 2 for the block kernel. |
@@ -1,10 +1,10 @@
1
1
  [build-system]
2
- requires = ["setuptools>=68.0", "setuptools-scm>=8.0"]
2
+ requires = ["setuptools>=77.0", "setuptools-scm>=8.0"]
3
3
  build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "trnsolver"
7
- version = "0.22.1"
7
+ version = "0.23.0"
8
8
  description = "Linear solvers and eigendecomposition for AWS Trainium via NKI"
9
9
  readme = "README.md"
10
10
  license = "Apache-2.0"
@@ -6,6 +6,8 @@ Needs trnsparse (>= the #104 multicore kernel_matmul).
6
6
 
7
7
  python scripts/bench_gp_precond.py build --n 1e5 [--seq] # preconditioner build time
8
8
  python scripts/bench_gp_precond.py conv --n 5e4 # CG: host64 vs projected
9
+ python scripts/bench_gp_precond.py conv --n 1e5 --applies projected \
10
+ --reductions device,pairwise,host64 --L-cache L.pt --save-dir DIR # #128
9
11
  python scripts/bench_gp_precond.py gp --n 1e6 # GP posterior mean (C3)
10
12
 
11
13
  --cpu runs the same on the CPU (trnsparse pytorch backend).
@@ -16,8 +18,10 @@ Output is one JSON line per measurement (prefix RESULT) plus human-readable line
16
18
  from __future__ import annotations
17
19
 
18
20
  import argparse
21
+ import hashlib
19
22
  import json
20
23
  import math
24
+ import os
21
25
  import time
22
26
 
23
27
  import torch
@@ -95,6 +99,7 @@ class DeviceRows:
95
99
  self.device = device
96
100
  self.calls = self.rows = 0
97
101
  self.t_rows = 0.0
102
+ self.shapes = set()
98
103
 
99
104
  def rows_fn(self, idx):
100
105
  t = time.perf_counter()
@@ -104,6 +109,7 @@ class DeviceRows:
104
109
  self.t_rows += time.perf_counter() - t
105
110
  self.calls += 1
106
111
  self.rows += idx.numel()
112
+ self.shapes.add(idx.numel())
107
113
  return out
108
114
 
109
115
  def sub_fn(self, idx):
@@ -112,7 +118,21 @@ class DeviceRows:
112
118
  return trnsparse.kernel_matmul_reference(Xi, Xi, eye, "rbf", LS, OS)
113
119
 
114
120
 
115
- def build(X, op, device, *, rank, block, relax, apply, seq=False):
121
+ def prewarm(X, device, block):
122
+ """Compile every rows_fn shape pad_rows can produce (powers of two up to block).
123
+
124
+ The shapes don't depend on the data, so this also works in TorchNeuron
125
+ compile-only mode, unlike a pre-warm build (trnsci/trnsci#109).
126
+ """
127
+ dr = DeviceRows(X, device)
128
+ t0 = time.perf_counter()
129
+ sizes = sorted({min(1 << b, block) for b in range(max(1, block).bit_length())})
130
+ for j in sizes:
131
+ dr.rows_fn(torch.arange(j))
132
+ return time.perf_counter() - t0, sizes
133
+
134
+
135
+ def build(X, op, device, *, rank, block, relax, apply, seq=False, pad_rows=True):
116
136
  t = {}
117
137
  t0 = time.perf_counter()
118
138
  if seq:
@@ -121,10 +141,19 @@ def build(X, op, device, *, rank, block, relax, apply, seq=False):
121
141
  else:
122
142
  dr = DeviceRows(X, device)
123
143
  L = trnsolver.pivoted_cholesky(
124
- op.diag, dr.rows_fn, rank, block=block, relax=relax, sub_fn=dr.sub_fn
144
+ op.diag,
145
+ dr.rows_fn,
146
+ rank,
147
+ block=block,
148
+ relax=relax,
149
+ sub_fn=dr.sub_fn,
150
+ pad_rows=pad_rows,
125
151
  )
126
- info = dict(calls=dr.calls, rows=dr.rows, rows_s=round(dr.t_rows, 2))
152
+ info = dict(calls=dr.calls, rows=dr.rows, shapes=len(dr.shapes), rows_s=round(dr.t_rows, 2))
153
+ # cholesky_s includes the device rows (rows_s); host_s is the host fp64 part alone.
127
154
  t["cholesky_s"] = time.perf_counter() - t0
155
+ if info["rows_s"] is not None:
156
+ t["host_s"] = t["cholesky_s"] - info["rows_s"]
128
157
  t0 = time.perf_counter()
129
158
  Ld = L.to(device=device, dtype=torch.float32)
130
159
  M = trnsolver.PivotedLowRankPreconditioner(Ld, op.noise, apply=apply)
@@ -152,12 +181,21 @@ def cmd_build(a):
152
181
  op = trnsparse.KernelOperator(X, "rbf", LS, OS, a.noise, device=a.device)
153
182
  for apply in a.applies.split(","):
154
183
  L, M, t, info = build(
155
- X, op, a.device, rank=a.rank, block=a.block, relax=a.relax, apply=apply, seq=a.seq
184
+ X,
185
+ op,
186
+ a.device,
187
+ rank=a.rank,
188
+ block=a.block,
189
+ relax=a.relax,
190
+ apply=apply,
191
+ seq=a.seq,
192
+ pad_rows=a.pad_rows,
156
193
  )
157
194
  ta = time_apply(M, n, a.device)
158
195
  print(
159
196
  f"n={n} rank={info['rank']} seq={a.seq} block={a.block} relax={a.relax} apply={apply}: "
160
- f"cholesky {t['cholesky_s']:.1f}s (rows on device {info['rows_s']}s, {info['calls']} calls) "
197
+ f"pad_rows={a.pad_rows}: cholesky {t['cholesky_s']:.1f}s (rows on device {info['rows_s']}s, "
198
+ f"{info['calls']} calls, {info.get('shapes')} shapes; host {t.get('host_s', 0):.1f}s) "
161
199
  f"factor {t['factor_s']:.1f}s total {t['precond_s']:.1f}s; apply(n,33) {ta * 1e3:.0f} ms; "
162
200
  f"trace_rem {info['trace_rem']:.4e}",
163
201
  flush=True,
@@ -178,10 +216,10 @@ def cmd_build(a):
178
216
  break
179
217
 
180
218
 
181
- def solve(op, B, M, a):
219
+ def solve(op, B, M, a, reduction=None):
182
220
  t0 = time.perf_counter()
183
221
  sol, info = trnsolver.cg_block(
184
- op, B, M=M, tol=a.tol, maxiter=a.maxiter, check_every=a.check_every
222
+ op, B, M=M, tol=a.tol, maxiter=a.maxiter, check_every=a.check_every, reduction=reduction
185
223
  )
186
224
  sync()
187
225
  return sol, info, time.perf_counter() - t0
@@ -199,16 +237,25 @@ def cmd_conv(a):
199
237
  X, y, Xt, ft = synthetic_field(n, 3, noise=a.noise)
200
238
  op = trnsparse.KernelOperator(X, "rbf", LS, OS, a.noise, device=a.device)
201
239
  B = torch.cat([y[:, None], _rademacher(n, 32, 1)], 1).to(device=a.device, dtype=torch.float32)
202
- dr = DeviceRows(X, a.device)
203
- L = trnsolver.pivoted_cholesky(
204
- op.diag, dr.rows_fn, a.rank, block=a.block, relax=a.relax, sub_fn=dr.sub_fn
205
- )
240
+ if a.L_cache and os.path.exists(a.L_cache):
241
+ L = torch.load(a.L_cache) # float64 host factor from an earlier run
242
+ else:
243
+ dr = DeviceRows(X, a.device)
244
+ L = trnsolver.pivoted_cholesky(
245
+ op.diag, dr.rows_fn, a.rank, block=a.block, relax=a.relax, sub_fn=dr.sub_fn
246
+ )
247
+ if a.L_cache:
248
+ torch.save(L, a.L_cache)
206
249
  Ld = L.to(device=a.device, dtype=torch.float32)
207
250
  first = None
208
- for apply in a.applies.split(","):
209
- M = trnsolver.PivotedLowRankPreconditioner(Ld, a.noise, apply=apply)
251
+ runs = [(ap, red) for ap in a.applies.split(",") for red in a.reductions.split(",")]
252
+ for apply, red in runs:
253
+ red = red or None
254
+ M = trnsolver.PivotedLowRankPreconditioner(Ld, a.noise, apply=apply, reduction=red)
210
255
  ta = time_apply(M, n, a.device)
211
- sol, info, ts = solve(op, B, M, a)
256
+ if a.warm:
257
+ solve(op, B, M, a, red) # first call compiles; time the second
258
+ sol, info, ts = solve(op, B, M, a, red)
212
259
  alpha = sol[:, 0].detach().cpu().double()
213
260
  mean = trnsparse.kernel_matmul_reference(Xt, X, alpha, "rbf", LS, OS)
214
261
  r = dict(
@@ -216,6 +263,8 @@ def cmd_conv(a):
216
263
  n=n,
217
264
  noise=a.noise,
218
265
  apply=apply,
266
+ reduction=M.reduction,
267
+ lnc=os.environ.get("NEURON_LOGICAL_NC_CONFIG", "2"),
219
268
  iterations=info["iterations"],
220
269
  col_iters_max=int(info["col_iterations"].max()),
221
270
  converged=bool(info["converged"].all()),
@@ -224,9 +273,18 @@ def cmd_conv(a):
224
273
  sampled_res=sampled_residual(X, y, alpha, a.noise, n),
225
274
  solve_s=round(ts, 1),
226
275
  s_per_iter=round(ts / max(1, info["iterations"] + 1), 2),
276
+ ms_per_iter=round(1e3 * ts / max(1, info["iterations"] + 1), 1),
227
277
  apply_ms=round(ta * 1e3, 1),
228
278
  rmse=float((mean - ft).pow(2).mean().sqrt()),
279
+ sha_X=hashlib.sha256(sol.detach().cpu().numpy().tobytes()).hexdigest()[:16],
280
+ sha_mean=hashlib.sha256(mean.numpy().tobytes()).hexdigest()[:16],
229
281
  )
282
+ if a.save_dir:
283
+ os.makedirs(a.save_dir, exist_ok=True)
284
+ torch.save(
285
+ dict(X=sol.detach().cpu(), mean=mean, info=r),
286
+ f"{a.save_dir}/conv_{apply}_{M.reduction}.pt",
287
+ )
230
288
  if first is None:
231
289
  first = (mean, sol.detach().cpu())
232
290
  else:
@@ -249,7 +307,14 @@ def cmd_gp(a):
249
307
  B = torch.cat([y[:, None], _rademacher(n, a.probes, 1)], 1).to(device=a.device, dtype=op.dtype)
250
308
  t["setup_s"] = time.perf_counter() - t0
251
309
  L, M, tb, binfo = build(
252
- X, op, a.device, rank=min(a.rank, n), block=a.block, relax=a.relax, apply=a.apply
310
+ X,
311
+ op,
312
+ a.device,
313
+ rank=min(a.rank, n),
314
+ block=a.block,
315
+ relax=a.relax,
316
+ apply=a.apply,
317
+ pad_rows=a.pad_rows,
253
318
  )
254
319
  t.update(tb)
255
320
  t["precond_rank"] = binfo["rank"]
@@ -300,8 +365,27 @@ def main():
300
365
  ap.add_argument(
301
366
  "--seq", action="store_true", help="build: the one-row-per-step host build (baseline)"
302
367
  )
368
+ ap.add_argument(
369
+ "--pad-rows",
370
+ action=argparse.BooleanOptionalAction,
371
+ default=True,
372
+ help="pad device row calls to power-of-two shapes (trnsci/trnsci#109)",
373
+ )
374
+ ap.add_argument(
375
+ "--prewarm",
376
+ action="store_true",
377
+ help="compile the pad_rows shapes first (timed separately as prewarm_s)",
378
+ )
303
379
  ap.add_argument("--apply", default="projected")
304
380
  ap.add_argument("--applies", default="host64,projected")
381
+ ap.add_argument(
382
+ "--reductions",
383
+ default="",
384
+ help="conv: cg_block reduction modes, comma-separated (default: the library default)",
385
+ )
386
+ ap.add_argument("--L-cache", default=None, help="conv: load / save the factor L here")
387
+ ap.add_argument("--save-dir", default=None, help="conv: save X and the mean per run")
388
+ ap.add_argument("--warm", action="store_true", help="conv: run each solve twice, time the 2nd")
305
389
  ap.add_argument("--probes", type=int, default=32)
306
390
  ap.add_argument("--tol", type=float, default=1e-6)
307
391
  ap.add_argument("--maxiter", type=int, default=1000)
@@ -314,6 +398,11 @@ def main():
314
398
  torch.set_num_threads(a.threads)
315
399
  a.device = "cpu" if a.cpu else "neuron"
316
400
  trnsparse.set_backend("pytorch" if a.cpu else "nki")
401
+ if a.prewarm and a.cmd != "conv":
402
+ X = synthetic_field(int(a.n), 3, noise=a.noise)[0]
403
+ tp, sizes = prewarm(X, a.device, a.block)
404
+ print(f"prewarm: {len(sizes)} shapes {sizes} in {tp:.1f}s", flush=True)
405
+ emit(kind="prewarm", n=int(a.n), prewarm_s=round(tp, 2), shapes=sizes)
317
406
  {"build": cmd_build, "conv": cmd_conv, "gp": cmd_gp}[a.cmd](a)
318
407
 
319
408