trntensor 0.21.0__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 (85) hide show
  1. {trntensor-0.21.0 → trntensor-0.23.0}/CHANGELOG.md +164 -0
  2. {trntensor-0.21.0 → trntensor-0.23.0}/CLAUDE.md +7 -3
  3. {trntensor-0.21.0/trntensor.egg-info → trntensor-0.23.0}/PKG-INFO +31 -12
  4. {trntensor-0.21.0 → trntensor-0.23.0}/README.md +30 -11
  5. {trntensor-0.21.0 → trntensor-0.23.0}/docs/api/einsum.md +17 -11
  6. {trntensor-0.21.0 → trntensor-0.23.0}/docs/api/nki.md +19 -12
  7. {trntensor-0.21.0 → trntensor-0.23.0}/docs/api/plan.md +11 -5
  8. {trntensor-0.21.0 → trntensor-0.23.0}/docs/api/quantum.md +66 -16
  9. {trntensor-0.21.0 → trntensor-0.23.0}/docs/architecture.md +24 -12
  10. {trntensor-0.21.0 → trntensor-0.23.0}/docs/benchmarks.md +15 -3
  11. {trntensor-0.21.0 → trntensor-0.23.0}/docs/contraction_patterns.md +27 -17
  12. trntensor-0.23.0/docs/index.md +41 -0
  13. trntensor-0.23.0/docs/installation.md +26 -0
  14. {trntensor-0.21.0 → trntensor-0.23.0}/docs/quickstart.md +12 -8
  15. {trntensor-0.21.0 → trntensor-0.23.0}/docs/tutorial_df_mp2.md +30 -28
  16. {trntensor-0.21.0 → trntensor-0.23.0}/pyproject.toml +1 -1
  17. trntensor-0.23.0/scripts/bench_ao2mo_nocc.py +173 -0
  18. trntensor-0.23.0/tests/conftest.py +81 -0
  19. trntensor-0.23.0/tests/test_conformance.py +1156 -0
  20. {trntensor-0.21.0 → trntensor-0.23.0}/tests/test_decompose.py +32 -0
  21. {trntensor-0.21.0 → trntensor-0.23.0}/tests/test_einsum.py +257 -0
  22. {trntensor-0.21.0 → trntensor-0.23.0}/tests/test_native_dispatch.py +267 -20
  23. {trntensor-0.21.0 → trntensor-0.23.0}/tests/test_nki_compile.py +44 -0
  24. {trntensor-0.21.0 → trntensor-0.23.0}/tests/test_nki_native.py +34 -7
  25. {trntensor-0.21.0 → trntensor-0.23.0}/tests/test_nki_sim.py +27 -0
  26. trntensor-0.23.0/tests/test_nki_tail_audit.py +274 -0
  27. trntensor-0.23.0/tests/test_trace_cache_safety.py +563 -0
  28. {trntensor-0.21.0 → trntensor-0.23.0}/trntensor/__init__.py +1 -1
  29. {trntensor-0.21.0 → trntensor-0.23.0}/trntensor/decompose.py +5 -3
  30. {trntensor-0.21.0 → trntensor-0.23.0}/trntensor/einsum.py +75 -7
  31. {trntensor-0.21.0 → trntensor-0.23.0}/trntensor/nki/_kernels.py +235 -98
  32. {trntensor-0.21.0 → trntensor-0.23.0}/trntensor/nki/_native.py +14 -4
  33. {trntensor-0.21.0 → trntensor-0.23.0}/trntensor/nki/dispatch.py +177 -23
  34. {trntensor-0.21.0 → trntensor-0.23.0}/trntensor/plan.py +42 -11
  35. {trntensor-0.21.0 → trntensor-0.23.0}/trntensor/quantum.py +59 -10
  36. {trntensor-0.21.0 → trntensor-0.23.0/trntensor.egg-info}/PKG-INFO +31 -12
  37. {trntensor-0.21.0 → trntensor-0.23.0}/trntensor.egg-info/SOURCES.txt +4 -0
  38. trntensor-0.21.0/docs/index.md +0 -35
  39. trntensor-0.21.0/docs/installation.md +0 -21
  40. trntensor-0.21.0/tests/conftest.py +0 -49
  41. {trntensor-0.21.0 → trntensor-0.23.0}/.github/workflows/ci.yml +0 -0
  42. {trntensor-0.21.0 → trntensor-0.23.0}/.github/workflows/notify-umbrella.yml +0 -0
  43. {trntensor-0.21.0 → trntensor-0.23.0}/.github/workflows/publish.yml +0 -0
  44. {trntensor-0.21.0 → trntensor-0.23.0}/.gitignore +0 -0
  45. {trntensor-0.21.0 → trntensor-0.23.0}/.pre-commit-config.yaml +0 -0
  46. {trntensor-0.21.0 → trntensor-0.23.0}/CODE_OF_CONDUCT.md +0 -0
  47. {trntensor-0.21.0 → trntensor-0.23.0}/CONTRIBUTING.md +0 -0
  48. {trntensor-0.21.0 → trntensor-0.23.0}/LICENSE +0 -0
  49. {trntensor-0.21.0 → trntensor-0.23.0}/benchmarks/__init__.py +0 -0
  50. {trntensor-0.21.0 → trntensor-0.23.0}/benchmarks/bench_einsum.py +0 -0
  51. {trntensor-0.21.0 → trntensor-0.23.0}/docs/api/decompose.md +0 -0
  52. {trntensor-0.21.0 → trntensor-0.23.0}/docs/api/index.md +0 -0
  53. {trntensor-0.21.0 → trntensor-0.23.0}/docs/aws_setup.md +0 -0
  54. {trntensor-0.21.0 → trntensor-0.23.0}/docs/design/matmul_dispatch_findings.md +0 -0
  55. {trntensor-0.21.0 → trntensor-0.23.0}/docs/design/matmul_kernel_profile.md +0 -0
  56. {trntensor-0.21.0 → trntensor-0.23.0}/docs/developing_kernels.md +0 -0
  57. {trntensor-0.21.0 → trntensor-0.23.0}/examples/df_mp2_einsum.py +0 -0
  58. {trntensor-0.21.0 → trntensor-0.23.0}/infra/terraform/README.md +0 -0
  59. {trntensor-0.21.0 → trntensor-0.23.0}/infra/terraform/main.tf +0 -0
  60. {trntensor-0.21.0 → trntensor-0.23.0}/mkdocs.yml +0 -0
  61. {trntensor-0.21.0 → trntensor-0.23.0}/scripts/autotune_dispatch.py +0 -0
  62. {trntensor-0.21.0 → trntensor-0.23.0}/scripts/bench_native_einsum.py +0 -0
  63. {trntensor-0.21.0 → trntensor-0.23.0}/scripts/bench_native_kernels.py +0 -0
  64. {trntensor-0.21.0 → trntensor-0.23.0}/scripts/bench_to_md.py +0 -0
  65. {trntensor-0.21.0 → trntensor-0.23.0}/scripts/run_benchmarks.sh +0 -0
  66. {trntensor-0.21.0 → trntensor-0.23.0}/scripts/run_neuron_profile.sh +0 -0
  67. {trntensor-0.21.0 → trntensor-0.23.0}/scripts/run_neuron_tests.sh +0 -0
  68. {trntensor-0.21.0 → trntensor-0.23.0}/scripts/run_reduce_parallel_multirank.py +0 -0
  69. {trntensor-0.21.0 → trntensor-0.23.0}/scripts/run_simulator_tests.sh +0 -0
  70. {trntensor-0.21.0 → trntensor-0.23.0}/setup.cfg +0 -0
  71. {trntensor-0.21.0 → trntensor-0.23.0}/tests/test_host_output.py +0 -0
  72. {trntensor-0.21.0 → trntensor-0.23.0}/tests/test_nki.py +0 -0
  73. {trntensor-0.21.0 → trntensor-0.23.0}/tests/test_nki_kernels.py +0 -0
  74. {trntensor-0.21.0 → trntensor-0.23.0}/tests/test_parallel.py +0 -0
  75. {trntensor-0.21.0 → trntensor-0.23.0}/tests/test_plan.py +0 -0
  76. {trntensor-0.21.0 → trntensor-0.23.0}/tests/test_quantum.py +0 -0
  77. {trntensor-0.21.0 → trntensor-0.23.0}/tests/test_sr_runtime.py +0 -0
  78. {trntensor-0.21.0 → trntensor-0.23.0}/trntensor/nki/__init__.py +0 -0
  79. {trntensor-0.21.0 → trntensor-0.23.0}/trntensor/nki/collectives.py +0 -0
  80. {trntensor-0.21.0 → trntensor-0.23.0}/trntensor/parallel.py +0 -0
  81. {trntensor-0.21.0 → trntensor-0.23.0}/trntensor/py.typed +0 -0
  82. {trntensor-0.21.0 → trntensor-0.23.0}/trntensor.egg-info/dependency_links.txt +0 -0
  83. {trntensor-0.21.0 → trntensor-0.23.0}/trntensor.egg-info/requires.txt +0 -0
  84. {trntensor-0.21.0 → trntensor-0.23.0}/trntensor.egg-info/top_level.txt +0 -0
  85. {trntensor-0.21.0 → trntensor-0.23.0}/uv.lock +0 -0
@@ -7,6 +7,170 @@ 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-02
11
+
12
+ Validated on Trn2 (trn2.3xlarge, Neuron SDK 2.33.0, native TorchNeuron) — see trnsci/trnsci#35.
13
+
14
+ Headline: **correctness fixes — upgrade recommended.** `kahan`/`dd` dropped the imaginary part of complex operands and rounded large integers (#69, #90); `beta=0` read `out=` (#71); complex `tucker_decompose` used Uᵀ (#73); `multi_einsum` returned fp32 results as fp64 (#75); `einsum(precision="dd")` missed its documented bound (#84); float64 chemistry kernels returned inf on device (#87); `ao_to_mo_transform` on device with operands over 4 GiB was silently wrong and is now guarded (#97). New: native AO→MO for any nocc, 1.8–3.5× faster than aten at nocc 160–384 (#94).
15
+
16
+ ### Changed
17
+
18
+ - **`ao_to_mo_transform`'s Trn2 kernels now run any `nocc` (#80).** They
19
+ needed `nocc ≤ 128`, so larger systems fell back to aten under `"auto"`.
20
+ - Stage 1 runs occupied blocks of ≤ 128 over each eri block, so `eri` is
21
+ read once whatever `nocc` is. `C_occ` and `C_vir` stay resident in SBUF.
22
+ - Both stages move 512 columns per Tensor Engine pass (`512 // TP` ν in
23
+ stage 1, `512 // TP` orbitals in stage 2), and stage 2 fetches and
24
+ stores each group with one strided DMA.
25
+ - With an odd aux-chunk count and `nocc > 128`, the two programs of an
26
+ LNC=2 core split the occupied index instead of the aux chunks (the
27
+ dispatcher appends one zero orbital when `nocc` is odd), so both
28
+ physical cores still work.
29
+ - The packed kernel gets the same treatment and keeps its in-place gather.
30
+ - Trn2, LNC=2, fp32, warm: at nbasis 512 / naux 1536 the kernel is
31
+ 1.8–2.4× faster than aten at nocc 160–384 (full `eri`) and 2.7–3.5×
32
+ with packed `eri`. It is 1.3–1.7× faster than launching the old kernel
33
+ once per 128 orbitals. At LNC=1 it is 1.3–1.7× (full) and 2.3–2.6×
34
+ (packed) faster than aten. Tables on #80.
35
+ - At `nocc ≤ 128` it is 0.99–1.08× the speed of `main` (the 512-column
36
+ passes), and the first-call compile is shorter.
37
+ - With an odd aux-chunk count and `nocc > 128` the occupied split is
38
+ 1.3–1.6× faster than one program (e.g. 512/257/1152: 17.8 → 11.7 ms).
39
+ - **`"auto"` routing for `ao_to_mo_transform` (#80).**
40
+ - The `nocc ≤ 128` condition is gone.
41
+ - A full `eri` whose nbasis or naux is not a 128 multiple now stays on
42
+ aten. The kernel pads a copy of the whole square first, and that copy
43
+ costs more than aten (300/100/700: 5.6 ms against 2.0 ms). This also
44
+ changes routing at `nocc ≤ 128`. Pass packed `eri` to keep the kernel:
45
+ it pads nbasis only in its gather indices.
46
+ - A packed `eri` with a padded nbasis needs 2e9 multiply-adds of stage-1
47
+ work instead of 5e8.
48
+ - The kernel's SBUF budget goes from 144 to 176 KiB (df_exchange's). At
49
+ n_ao 970, nocc 161, packed naux 4608 it needs 158 KiB and runs in
50
+ 118 ms on Trn2 (LNC=2); aten ran out of device memory there.
51
+
52
+ ### Added
53
+
54
+ - **Guard against the NKI trace-cache closure bug ([aws-neuron/aws-neuron-sdk#1454](https://github.com/aws-neuron/aws-neuron-sdk/issues/1454),
55
+ trnsci/trnsci#35).** `tests/test_trace_cache_safety.py` (CPU) finds every
56
+ `@nki.jit` kernel and fails if its body, or a helper it calls, reads a
57
+ closure variable or a module global that is not a constant expression
58
+ (reviewed exceptions are allow-listed with a reason), and checks each
59
+ kernel's `__closure__` at runtime against a stub `nki`.
60
+ - Tests: `conftest.py` no longer turns the NKI trace cache off. Neuron tests
61
+ run with it on, as under `import torch_neuronx`, with a fresh
62
+ `NKI_TRACE_CACHE_URL` directory per session (no stale editable-install
63
+ traces). `NKI_ENABLE_TRACE_CACHE=0` still turns it off.
64
+ - README: known platform issue note for #1454.
65
+
66
+ ### Fixed
67
+
68
+ - **`ao_to_mo_transform` aten route on the device: wrong results from 4 GiB of
69
+ `eri` (#97).** On TorchNeuron (torch-neuronx 2.14.3), a matmul whose left
70
+ operand is a transposed view returns wrong values, without an error, once the
71
+ right operand exceeds 2^32 bytes and the contraction dim is a multiple of 128.
72
+ If the right operand came straight from the host it aborts instead
73
+ (aws-neuron/aws-neuron-sdk#1448). Both `torch.einsum` steps of the aten route
74
+ lower to that matmul, so at nbasis 768 / naux 2304 the result was off by
75
+ about 1 relative. On the device, from 2^32 bytes of `eri` or of the
76
+ half-transformed tensor, the two steps now run as `mm` and `bmm` with
77
+ contiguous orbital transposes. At 768/64/2304 the error against float64 is
78
+ 2.2e-7 (was 1.06), and a warm call takes 16.1 ms (the wrong route took
79
+ 25 ms). Not fixed here: at LNC=1, the device `index_select` that builds a
80
+ square `eri` over 4 GiB is itself wrong (it unpacks packed input for the aten
81
+ route), so that case still returns wrong values or NaN (#97).
82
+
83
+ - **`einsum(precision="kahan")` rounded int32 values above 2**24 on the
84
+ neuron device** (silent, #90). The device computes integer matmul in fp32.
85
+ Since #70 integer operands contracted natively on any device. They now
86
+ contract natively only on CPU/CUDA, and on the host for other devices.
87
+ Unreleased (`main` after #70 only). Found by the conformance suite on Trn2
88
+ (trnsci/trnsci#35).
89
+ - **NKI chemistry kernels on Trainium: float64 operands gave inf, and kernel
90
+ routes changed the result dtype** (silent, #87). float64 tensors reached the
91
+ fp32 kernels as is. `df_coulomb` (≥ 256 packed rows), `ao_to_mo_transform`
92
+ and `df_exchange` came back inf under `set_backend("nki")`, or under
93
+ `"auto"` at the sizes where it picks the kernels. float64 operands are now
94
+ computed in fp32 and returned as float64, the documented fp64-on-Trainium
95
+ behaviour (`df_*` 0.21.0–0.22.0, `ao_to_mo_transform` every release with
96
+ its kernels). `mp2_energy` (row and legacy kernels), `ao_to_mo_transform`
97
+ and `df_exchange` also returned float32 for float64 or bfloat16 callers.
98
+ They now return the aten route's dtype. `df_exchange` with `naux == 0` (or
99
+ `nocc == 0`) raised `ZeroDivisionError` in the route's tiling and now takes
100
+ aten. Found by the conformance suite on Trn2 (trnsci/trnsci#35).
101
+ - **`einsum(precision="dd")` on float32 / float64 operands was less accurate
102
+ than `"fast"`** (silent; 0.17.0 through 0.22.0, the CPU emulation; Trainium
103
+ raises `NotImplementedError`). The CPU route split every operand into two
104
+ bf16-precision pieces (~16 mantissa bits) and rounded float64 operands to
105
+ float32, so the error was ~1e-5 instead of the documented K·u² + u_out
106
+ (6e-8 fp32, 1.1e-16 fp64). float32 operands now accumulate in fp64 (exact
107
+ products, inside K·u²). float64 operands form exact TwoProduct products and
108
+ sum them in double-word arithmetic on `matmul` / `bmm` plans and `path`
109
+ steps; generic `torch` plans accumulate in fp64. bf16 / fp16 `"dd"` and
110
+ every `"fast"` result are unchanged (#84).
111
+ - **`einsum(..., out=C)` with `beta=0` (the default) read `C`**, so NaN or
112
+ inf in the buffer (e.g. `torch.empty`) made the result NaN (silent, every
113
+ backend). Affects 0.3.0 through 0.22.0. `beta == 0` now ignores `out`, as
114
+ in BLAS, cuTENSOR and trnblas (#71).
115
+ - **`tucker_decompose` returned a wrong core for complex tensors** (silent,
116
+ every backend): it projected with `Uᵀ` instead of `Uᴴ`, so even a
117
+ full-rank reconstruction was off by ~140 %. Affects every release through
118
+ 0.22.0. `tt_decompose` now also accepts complex tensors instead of raising
119
+ (#73). Real tensors are unchanged.
120
+ - **`precision="kahan"` / `"dd"` / `target_forward_error` dropped the imaginary
121
+ part of complex operands and rounded large integers** (silent, every
122
+ backend). Affects `"kahan"` in 0.4.0 through 0.22.0, and `"dd"` and
123
+ `target_forward_error` in 0.17.0 through 0.22.0. Complex operands now
124
+ accumulate in complex128, and integer operands contract natively, which is
125
+ exact (#69).
126
+ - **`multi_einsum` changed result dtypes for mixed-dtype batches** (silent,
127
+ every backend). Same-shape contractions were stacked without checking
128
+ dtype, so float32 results came back float64. Affects 0.17.0 through 0.22.0.
129
+ Batching now requires matching operand dtypes and devices (#75).
130
+
131
+ ## [0.22.0] — 2026-11-01
132
+
133
+ Validated on Trn2 (trn2.3xlarge, Neuron SDK 2.33.0, native TorchNeuron) — see trnsci/trnsci#35.
134
+
135
+ Headline: **correctness fix:** in every release through 0.21.0, on every backend, `einsum` planned matmul/bmm without checking index order, so transposed or permuted outputs came back in the wrong layout (`ij,jk->ki` returned `A@B`, silently for square operands) and non-canonical batched operands raised. 228 of 364 two-operand cases were wrong or raised (#65, #64). Also: `df_exchange` tiling minimises partial SYRK tiles, so K at n_ao=970 drops from 118 to 94 ms (#66).
136
+
137
+ ### Fixed
138
+
139
+ - **`einsum` returned the wrong layout for transposed or permuted outputs**
140
+ (silent). Affects **every release through 0.21.0**, on every backend
141
+ (PyTorch included). The planner chose the `matmul` / `bmm` strategy without
142
+ checking index order, so `einsum("ij,jk->ki", A, B)` returned `A @ B`
143
+ (shape `(i, k)`, and for square operands the transpose with no error), the
144
+ permuted batched outputs (`bij,bjk->bki` etc.) returned `(b, i, k)`, and
145
+ non-canonical batched operands (`bji,...`, `...,bkj`, `ibj,...`) raised.
146
+ `matmul` is now planned only for outputs `[A free, B free]` and `bmm` only
147
+ for exactly `bij,bjk->bik`; the rest run through `torch.einsum` (#64).
148
+
149
+ ### Changed
150
+
151
+ - **`df_exchange` tiles naux to minimise the SYRK's partial aux tiles (#66, #60).**
152
+ K at n_ao = 970 takes 93.8 ms instead of 118 ms at LNC=2, and 187 ms
153
+ instead of 230 ms at LNC=1. The error against float64 is 6.2e-7 (was
154
+ 8.1e-7) relative to max |K|.
155
+ - Aux chunks can now be 128 multiples with a short last chunk of
156
+ `naux mod width`. Before, the width had to divide naux.
157
+ `df_half_packed_kernel` takes `tp` and `tail` and gathers from the packed
158
+ rows viewed as `(npair·naux / W, W)`, `W = gcd(naux, tp)`. `B` is still
159
+ not copied.
160
+ - `df_half_tiling` first minimises the 128-row tiles over the SYRK
161
+ launches (`df_syrk_tiles`), because each partial tile there costs a full
162
+ one. Next it prefers a divisor of naux, then the old order.
163
+ - At naux = 4686 that means chunks of 256 with a 78 tail, 37 tiles
164
+ instead of 44.
165
+ - n_ao = 250 and 490 keep their previous tilings, which already reach
166
+ the minimum.
167
+ - `df_groups` folds a lone short last chunk into the previous launch group.
168
+ - The launch-1 SBUF budget is now 176 KiB (was 160 KiB), so that
169
+ TP = 256, NUB = 8, NVC = 16 fits at n_ao = 970. This tiling is 7 ms
170
+ faster than NVC = 8.
171
+ - naux with no divisor in [32, 512] now runs the kernels, not aten.
172
+ - `TRNTENSOR_DF_K_TP` forces the chunk width.
173
+
10
174
  ## [0.21.0] — 2026-11-01
11
175
 
12
176
  Validated on Trn2 (trn2.3xlarge, Neuron SDK 2.33.0, native TorchNeuron) — see trnsci/trnsci#35.
@@ -21,7 +21,9 @@ trntensor/
21
21
  │ ├── __init__.py
22
22
  │ ├── einsum.py # einsum(), multi_einsum()
23
23
  │ ├── plan.py # ContractionPlan, plan_contraction(), estimate_flops()
24
- │ ├── decompose.py # cp_decompose, tucker_decompose (HOSVD)
24
+ │ ├── decompose.py # cp_decompose, tucker_decompose (HOSVD), tt_decompose
25
+ │ ├── quantum.py # mp2_energy, ao_to_mo_transform, df_exchange, df_coulomb
26
+ │ ├── parallel.py # ShardedTensor, scatter, gather
25
27
  │ └── nki/
26
28
  │ ├── __init__.py
27
29
  │ ├── _kernels.py # @nki.jit kernels
@@ -38,8 +40,10 @@ trntensor/
38
40
  ## Contraction Planning
39
41
 
40
42
  The planner analyzes einsum subscripts and selects:
41
- - **matmul**: 2D contraction over single index → `torch.matmul`
42
- - **bmm**: batched 2D contraction → `torch.bmm`
43
+ - **matmul**: 2D contraction over single index, output `[A free, B free]` → `torch.matmul`
44
+ - **bmm**: exactly `bij,bjk->bik` → `torch.bmm`
45
+ (other index orders go to `torch.einsum`; before 0.22.0 they silently
46
+ returned the wrong layout, #64)
43
47
  - **torch**: complex patterns → `torch.einsum`
44
48
  - **(future) nki**: fused multi-index contractions on Tensor Engine
45
49
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: trntensor
3
- Version: 0.21.0
3
+ Version: 0.23.0
4
4
  Summary: Tensor contractions for AWS Trainium via NKI
5
5
  Author-email: Scott Friedman <scttfrdmn@gmail.com>
6
6
  License-Expression: Apache-2.0
@@ -54,10 +54,10 @@ Einstein summation with contraction planning, CP and Tucker decompositions. Expr
54
54
 
55
55
  trntensor follows the [trnsci 5-phase roadmap](https://trnsci.dev/roadmap/). Active work is tracked in phase-labeled GitHub issues:
56
56
 
57
- - **[Phase 1 — correctness](https://github.com/trnsci/trntensor/issues/27)** (active): matmul + batched-matmul NKI kernels in place; awaiting hardware validation + additional `@pytest.mark.neuron` coverage.
58
- - **[Phase 2 — precision](https://github.com/trnsci/trntensor/issues/28)**: precision-aware contraction path selection (depends on [trnblas#22](https://github.com/trnsci/trnblas/issues/22) double-double GEMM).
59
- - **[Phase 3 — perf](https://github.com/trnsci/trntensor/issues/29)**: opt_einsum-style path planner, plan cache reuse.
60
- - **[Phase 4 — multi-chip](https://github.com/trnsci/trntensor/issues/30)**: sharded tensor contractions.
57
+ - **[Phase 1 — correctness](https://github.com/trnsci/trntensor/issues/27)** (closed): NKI kernels validated on hardware. On the native TorchNeuron path (Trn2/Trn3) the fused `mp2_energy`, `ao_to_mo_transform`, `df_exchange` and `df_coulomb` kernels beat the aten routes they replace; the NKI matmul/bmm kernels never did and are deprecated there in favour of aten (0.19.0).
58
+ - **[Phase 2 — precision](https://github.com/trnsci/trntensor/issues/28)** (closed): precision-aware mode selection (`precision=`, `target_forward_error=`). `precision="dd"` on Trainium still waits on [trnblas#22](https://github.com/trnsci/trnblas/issues/22) double-double GEMM.
59
+ - **[Phase 3 — perf](https://github.com/trnsci/trntensor/issues/29)** (closed): greedy multi-operand path planner, plan cache.
60
+ - **[Phase 4 — multi-chip](https://github.com/trnsci/trntensor/issues/30)** (active): sharded tensor contractions (`ShardedTensor`, reduce-parallel all-reduce).
61
61
  - **[Phase 5 — generation](https://github.com/trnsci/trntensor/issues/31)**: trn2 fused multi-contraction paths.
62
62
 
63
63
  Suite-wide tracker: [trnsci/trnsci#1](https://github.com/trnsci/trnsci/issues/1).
@@ -92,6 +92,19 @@ the device with `to_device` / `from_device`; the older `to_xla` /
92
92
  editable install (`pip install -e`), set `NKI_ENABLE_TRACE_CACHE=0`: the NKI
93
93
  kernel cache only versions installed wheels and can serve stale kernels.
94
94
 
95
+ ### Known platform issue: NKI trace cache and kernel factories
96
+
97
+ `import torch_neuronx` turns on the NKI trace cache, and the cache key ignores
98
+ constants captured in a kernel's closure: two `@nki.jit` kernels built by one
99
+ factory, differing only in a captured value, share one trace, and the second
100
+ silently returns the first's results
101
+ ([aws-neuron/aws-neuron-sdk#1454](https://github.com/aws-neuron/aws-neuron-sdk/issues/1454)).
102
+ trntensor's kernels do not rely on closure constants (or on module globals that
103
+ can differ between processes): every value that specialises a kernel is a
104
+ kernel argument, and `tests/test_trace_cache_safety.py` enforces this. If you
105
+ write your own NKI kernels with a factory, set `NKI_ENABLE_TRACE_CACHE=0`
106
+ before importing `torch_neuronx`.
107
+
95
108
  ## Usage
96
109
 
97
110
  ```python
@@ -125,10 +138,16 @@ core, factors = trntensor.tucker_decompose(tensor, ranks=(5, 5, 5))
125
138
  | Planning | `estimate_flops` | FLOPs for a contraction |
126
139
  | Decomposition | `cp_decompose` | CP/PARAFAC via ALS |
127
140
  | Decomposition | `tucker_decompose` | Tucker via HOSVD |
141
+ | Decomposition | `tt_decompose` | Tensor Train via TT-SVD |
142
+ | Chemistry | `mp2_energy` | Fused DF-MP2 correlation energy |
143
+ | Chemistry | `ao_to_mo_transform` | Fused AO→MO transform (full or packed integrals) |
144
+ | Chemistry | `df_exchange` / `df_coulomb` | Fused DF exchange (K) / Coulomb (J) from packed integrals |
145
+ | Chemistry | `pack_tril` / `unpack_tril` | Packed (μ≥ν) three-centre integral layout |
146
+ | Sharding | `ShardedTensor`, `scatter`, `gather` | Output- and reduce-parallel sharded contractions |
128
147
 
129
148
  ## Status
130
149
 
131
- - [x] Einsum with matmul/bmm/torch dispatch
150
+ - [x] Einsum with matmul/bmm/torch dispatch (output index order honoured since 0.22.0; earlier releases returned the wrong layout for transposed/permuted outputs, #64)
132
151
  - [x] Contraction planner
133
152
  - [x] CP decomposition (ALS)
134
153
  - [x] Tucker decomposition (HOSVD)
@@ -146,17 +165,17 @@ core, factors = trntensor.tucker_decompose(tensor, ranks=(5, 5, 5))
146
165
  - [x] Non-negative CP + warm-start CP
147
166
  - [x] PEP 561 `py.typed` marker
148
167
  - [x] Mixed precision / `dtype` override for bf16/fp16
149
- - [x] `precision=` kwarg (`"fast"` / `"kahan"` fp64 promotion / `"dd"` stub)
168
+ - [x] `precision=` kwarg (`"fast"` / `"kahan"` fp64 promotion / `"sr"` / `"dd"`: CPU emulation only, raises on Trainium until trnblas#22)
150
169
 
151
170
  ## Related Projects
152
171
 
153
172
  | Project | What |
154
173
  |---------|------|
155
- | [trnfft](https://github.com/scttfrdmn/trnfft) | FFT + complex ops |
156
- | [trnblas](https://github.com/scttfrdmn/trnblas) | BLAS operations |
157
- | [trnsolver](https://github.com/scttfrdmn/trnsolver) | Linear solvers |
158
- | [trnrand](https://github.com/scttfrdmn/trnrand) | Random number generation |
159
- | [trnsparse](https://github.com/scttfrdmn/trnsparse) | Sparse operations |
174
+ | [trnfft](https://github.com/trnsci/trnfft) | FFT + complex ops |
175
+ | [trnblas](https://github.com/trnsci/trnblas) | BLAS operations |
176
+ | [trnsolver](https://github.com/trnsci/trnsolver) | Linear solvers |
177
+ | [trnrand](https://github.com/trnsci/trnrand) | Random number generation |
178
+ | [trnsparse](https://github.com/trnsci/trnsparse) | Sparse operations |
160
179
 
161
180
  ## License
162
181
 
@@ -16,10 +16,10 @@ Einstein summation with contraction planning, CP and Tucker decompositions. Expr
16
16
 
17
17
  trntensor follows the [trnsci 5-phase roadmap](https://trnsci.dev/roadmap/). Active work is tracked in phase-labeled GitHub issues:
18
18
 
19
- - **[Phase 1 — correctness](https://github.com/trnsci/trntensor/issues/27)** (active): matmul + batched-matmul NKI kernels in place; awaiting hardware validation + additional `@pytest.mark.neuron` coverage.
20
- - **[Phase 2 — precision](https://github.com/trnsci/trntensor/issues/28)**: precision-aware contraction path selection (depends on [trnblas#22](https://github.com/trnsci/trnblas/issues/22) double-double GEMM).
21
- - **[Phase 3 — perf](https://github.com/trnsci/trntensor/issues/29)**: opt_einsum-style path planner, plan cache reuse.
22
- - **[Phase 4 — multi-chip](https://github.com/trnsci/trntensor/issues/30)**: sharded tensor contractions.
19
+ - **[Phase 1 — correctness](https://github.com/trnsci/trntensor/issues/27)** (closed): NKI kernels validated on hardware. On the native TorchNeuron path (Trn2/Trn3) the fused `mp2_energy`, `ao_to_mo_transform`, `df_exchange` and `df_coulomb` kernels beat the aten routes they replace; the NKI matmul/bmm kernels never did and are deprecated there in favour of aten (0.19.0).
20
+ - **[Phase 2 — precision](https://github.com/trnsci/trntensor/issues/28)** (closed): precision-aware mode selection (`precision=`, `target_forward_error=`). `precision="dd"` on Trainium still waits on [trnblas#22](https://github.com/trnsci/trnblas/issues/22) double-double GEMM.
21
+ - **[Phase 3 — perf](https://github.com/trnsci/trntensor/issues/29)** (closed): greedy multi-operand path planner, plan cache.
22
+ - **[Phase 4 — multi-chip](https://github.com/trnsci/trntensor/issues/30)** (active): sharded tensor contractions (`ShardedTensor`, reduce-parallel all-reduce).
23
23
  - **[Phase 5 — generation](https://github.com/trnsci/trntensor/issues/31)**: trn2 fused multi-contraction paths.
24
24
 
25
25
  Suite-wide tracker: [trnsci/trnsci#1](https://github.com/trnsci/trnsci/issues/1).
@@ -54,6 +54,19 @@ the device with `to_device` / `from_device`; the older `to_xla` /
54
54
  editable install (`pip install -e`), set `NKI_ENABLE_TRACE_CACHE=0`: the NKI
55
55
  kernel cache only versions installed wheels and can serve stale kernels.
56
56
 
57
+ ### Known platform issue: NKI trace cache and kernel factories
58
+
59
+ `import torch_neuronx` turns on the NKI trace cache, and the cache key ignores
60
+ constants captured in a kernel's closure: two `@nki.jit` kernels built by one
61
+ factory, differing only in a captured value, share one trace, and the second
62
+ silently returns the first's results
63
+ ([aws-neuron/aws-neuron-sdk#1454](https://github.com/aws-neuron/aws-neuron-sdk/issues/1454)).
64
+ trntensor's kernels do not rely on closure constants (or on module globals that
65
+ can differ between processes): every value that specialises a kernel is a
66
+ kernel argument, and `tests/test_trace_cache_safety.py` enforces this. If you
67
+ write your own NKI kernels with a factory, set `NKI_ENABLE_TRACE_CACHE=0`
68
+ before importing `torch_neuronx`.
69
+
57
70
  ## Usage
58
71
 
59
72
  ```python
@@ -87,10 +100,16 @@ core, factors = trntensor.tucker_decompose(tensor, ranks=(5, 5, 5))
87
100
  | Planning | `estimate_flops` | FLOPs for a contraction |
88
101
  | Decomposition | `cp_decompose` | CP/PARAFAC via ALS |
89
102
  | Decomposition | `tucker_decompose` | Tucker via HOSVD |
103
+ | Decomposition | `tt_decompose` | Tensor Train via TT-SVD |
104
+ | Chemistry | `mp2_energy` | Fused DF-MP2 correlation energy |
105
+ | Chemistry | `ao_to_mo_transform` | Fused AO→MO transform (full or packed integrals) |
106
+ | Chemistry | `df_exchange` / `df_coulomb` | Fused DF exchange (K) / Coulomb (J) from packed integrals |
107
+ | Chemistry | `pack_tril` / `unpack_tril` | Packed (μ≥ν) three-centre integral layout |
108
+ | Sharding | `ShardedTensor`, `scatter`, `gather` | Output- and reduce-parallel sharded contractions |
90
109
 
91
110
  ## Status
92
111
 
93
- - [x] Einsum with matmul/bmm/torch dispatch
112
+ - [x] Einsum with matmul/bmm/torch dispatch (output index order honoured since 0.22.0; earlier releases returned the wrong layout for transposed/permuted outputs, #64)
94
113
  - [x] Contraction planner
95
114
  - [x] CP decomposition (ALS)
96
115
  - [x] Tucker decomposition (HOSVD)
@@ -108,17 +127,17 @@ core, factors = trntensor.tucker_decompose(tensor, ranks=(5, 5, 5))
108
127
  - [x] Non-negative CP + warm-start CP
109
128
  - [x] PEP 561 `py.typed` marker
110
129
  - [x] Mixed precision / `dtype` override for bf16/fp16
111
- - [x] `precision=` kwarg (`"fast"` / `"kahan"` fp64 promotion / `"dd"` stub)
130
+ - [x] `precision=` kwarg (`"fast"` / `"kahan"` fp64 promotion / `"sr"` / `"dd"`: CPU emulation only, raises on Trainium until trnblas#22)
112
131
 
113
132
  ## Related Projects
114
133
 
115
134
  | Project | What |
116
135
  |---------|------|
117
- | [trnfft](https://github.com/scttfrdmn/trnfft) | FFT + complex ops |
118
- | [trnblas](https://github.com/scttfrdmn/trnblas) | BLAS operations |
119
- | [trnsolver](https://github.com/scttfrdmn/trnsolver) | Linear solvers |
120
- | [trnrand](https://github.com/scttfrdmn/trnrand) | Random number generation |
121
- | [trnsparse](https://github.com/scttfrdmn/trnsparse) | Sparse operations |
136
+ | [trnfft](https://github.com/trnsci/trnfft) | FFT + complex ops |
137
+ | [trnblas](https://github.com/trnsci/trnblas) | BLAS operations |
138
+ | [trnsolver](https://github.com/trnsci/trnsolver) | Linear solvers |
139
+ | [trnrand](https://github.com/trnsci/trnrand) | Random number generation |
140
+ | [trnsparse](https://github.com/trnsci/trnsparse) | Sparse operations |
122
141
 
123
142
  ## License
124
143
 
@@ -21,9 +21,14 @@ einsum(
21
21
  ```
22
22
 
23
23
  The planner selects `matmul`, `bmm`, `path`, or `torch.einsum` based on
24
- the subscript pattern and operand shapes. When a NKI backend is active and
25
- the pattern maps to a 2-index matmul, the kernel in
26
- `trntensor.nki._kernels.matmul_kernel` is used.
24
+ the subscript pattern and operand shapes. `matmul` is planned only when the
25
+ output lists A's free index then B's, and `bmm` only for exactly
26
+ `bij,bjk->bik`; transposed or permuted outputs run through `torch.einsum`.
27
+ Releases through 0.21.0 returned the wrong layout for those outputs on every
28
+ backend (#64, fixed in 0.22.0). On the native TorchNeuron path, matmul/bmm
29
+ run on aten on the `neuron` device (the NKI matmul kernels are deprecated
30
+ there); on the XLA path (Trn1), `trntensor.nki._kernels.matmul_kernel` is used
31
+ above the FLOP threshold.
27
32
 
28
33
  ### Arguments
29
34
 
@@ -70,12 +75,12 @@ trntensor.einsum("ij,jk->ik", A, B, beta=1.0, out=acc)
70
75
 
71
76
  ### `dtype` — mixed-precision compute
72
77
 
73
- Cast operands to a lower dtype to hit the NKI bf16 matmul path without
74
- changing the model's weight dtype. The result is returned in the requested
75
- dtype.
78
+ Cast operands to a lower dtype to compute in bf16/fp16 (aten on the native
79
+ device, the NKI bf16 matmul kernel on the XLA path) without changing the
80
+ model's weight dtype. The result is returned in the requested dtype.
76
81
 
77
82
  ```python
78
- # Route through NKI bf16 kernel even if weights are fp32
83
+ # Compute in bf16 even if weights are fp32
79
84
  result_bf16 = trntensor.einsum("ij,jk->ik", A, B, dtype="bf16")
80
85
 
81
86
  # Use torch.dtype directly
@@ -89,7 +94,7 @@ result = trntensor.einsum("ij,jk->ik", A, B, dtype=torch.float16)
89
94
  | `"fast"` | Native operand dtype; NKI kernels accumulate in fp32 via PSUM |
90
95
  | `"kahan"` | Contracts in fp64 and rounds once back to the operand dtype; bypasses NKI dispatch. Operands on a device without native fp64 (the Neuron device, XLA) are contracted on the host and the result moved back. Error ≤ K·2⁻⁵³ + u_out, so the result is as accurate as its dtype allows (~1 ulp), not ~16 digits, unless the operands are float64 |
91
96
  | `"sr"` | Stochastic rounding at the PSUM→SBUF downcast (BF16/FP16); mean-zero √K·u error instead of K·u. On Trainium requires `NEURON_RT_STOCHASTIC_ROUNDING_EN=1` at process start (see below) |
92
- | `"dd"` | Double-double accumulation via trnblas Phase 2 — raises `NotImplementedError` until trnblas#22 lands |
97
+ | `"dd"` | Double-double accumulation via trnblas Phase 2 — raises `NotImplementedError` on Trainium hosts until trnblas#22 lands; on CPU, by operand dtype: bf16/fp16 run a software Ozaki-2 emulation for `matmul` plans (`"kahan"` for `bmm` / `torch` plans); fp32 runs as `"kahan"` (fp32 products are exact in fp64, so fp64 accumulation, K·2⁻⁵³, is inside K·u²); fp64 forms each product exactly (TwoProduct) and sums in double-word arithmetic for `matmul` / `bmm` plans and `path` steps, while generic `torch` plans accumulate in plain fp64 (the `"kahan"` bound) |
93
98
 
94
99
  ```python
95
100
  # High-precision DF-MP2 energy accumulation
@@ -107,7 +112,7 @@ operand dtype, so `u_out = u`):
107
112
  |------|-------|
108
113
  | `"fast"` | K·u + u_out |
109
114
  | `"sr"` | √K·u + u_out (expected value) |
110
- | `"dd"` | K·u² + u_out |
115
+ | `"dd"` | K·u² + u_out (fp64 `torch` plans: the `"kahan"` bound) |
111
116
  | `"kahan"` | K·2⁻⁵³ + u_out |
112
117
 
113
118
  `trntensor.forward_error_bound(mode, dtype, K)` returns these values.
@@ -156,8 +161,9 @@ Execute several contractions in one call. Each contraction is a tuple
156
161
  of `(subscripts, *operands)`. Results are returned in input order.
157
162
 
158
163
  When NKI dispatch is active, operand tensors that appear in more than one
159
- contraction (by object identity) are pre-pinned to the XLA device once
160
- before executing the loop, eliminating redundant host↔device transfers.
164
+ contraction (by object identity) are pre-pinned to the device (`neuron` on
165
+ TorchNeuron, the XLA device on Trn1) once before executing the loop,
166
+ eliminating redundant host↔device transfers.
161
167
 
162
168
  ```python
163
169
  results = trntensor.multi_einsum(
@@ -6,7 +6,10 @@ public-facing dispatch is in `trntensor.nki.dispatch`.
6
6
 
7
7
  ## `set_backend(backend: str)`
8
8
 
9
- - `"auto"` — use NKI if `nki` is importable, else PyTorch
9
+ - `"auto"` — the default. When `nki` is importable, each call is routed by
10
+ measured thresholds (see [Environment variables](#environment-variables)
11
+ and the tables below); on the native path matmul/bmm run on aten, and the
12
+ fused chemistry kernels run where they beat it. Otherwise PyTorch
10
13
  - `"pytorch"` — always use `torch.matmul` / `torch.einsum`
11
14
  - `"nki"` — require NKI; raises `RuntimeError` on non-Neuron hosts
12
15
 
@@ -29,7 +32,10 @@ detected on the first kernel launch (never at import):
29
32
  (`torch_neuronx.device_count() > 0`). Each kernel is a PyTorch custom op
30
33
  on `device="neuron"`, also callable directly and inside
31
34
  `torch.compile(backend="neuron")`: `torch.ops.trntensor.matmul`,
32
- `batched_matmul`, `mp2_energy`, `ao_to_mo_transform`. The ops are
35
+ `batched_matmul`, `mp2_energy`, `ao_to_mo_transform`, `mp2_energy_row`,
36
+ `ao_to_mo_trn2`, `ao_to_mo_trn2_packed`, `df_half_packed`,
37
+ `df_exchange_syrk`, `df_coulomb_d`, `df_coulomb_j` (`matmul` / `batched_matmul` are
38
+ deprecated there, see below). The ops are
33
39
  registered only once this path is selected, and this path never imports
34
40
  `torch_xla`.
35
41
  - **XLA (Trn1)** — chosen when `torch_xla` is installed and the native
@@ -117,7 +123,7 @@ supported in maintenance mode."
117
123
  | `TRNTENSOR_MIN_NKI_FLOPS_NATIVE_PINNED=<int>\|never` | Native path: M·K·N at which the NKI kernel replaces aten on the device. Below it, device-resident operands run `torch.matmul` / `torch.bmm` (aten) on `neuron`. Default: `never`, because on Trn2 aten beat the NKI kernels at every measured size. |
118
124
  | `TRNSCI_NKI_DISPATCH=auto\|native\|xla` | Suite-wide. Force the native or XLA dispatch path; `auto` (default) detects. Forcing an unavailable path raises. |
119
125
  | `TRNSCI_XLA_DEPRECATION=1` | Suite-wide, off by default. Emit the one-time XLA-path and `to_xla` / `from_xla` deprecation warnings. |
120
- | `NKI_ENABLE_TRACE_CACHE=0` | TorchNeuron setting (not trntensor's). Turns off the cross-process NKI kernel cache, which cannot version editable installs and may serve a stale kernel after a source edit. Set it with `pip install -e`; the test suite sets it by default. |
126
+ | `NKI_ENABLE_TRACE_CACHE=0` | TorchNeuron setting (not trntensor's). Turns off the cross-process NKI kernel cache, which cannot version editable installs and may serve a stale kernel after a source edit. Set it with `pip install -e`, or if you write your own NKI kernel factories ([aws-neuron/aws-neuron-sdk#1454](https://github.com/aws-neuron/aws-neuron-sdk/issues/1454)). The test suite keeps the cache on, with a fresh cache directory per session. |
121
127
  | `TRNTENSOR_MP2_ENERGY=auto\|rows\|aten\|legacy` | Native path: force the `mp2_energy` executor (`rows` = the Trn2 row kernel, `legacy` = the original kernel, deprecated there). |
122
128
  | `TRNTENSOR_AO_TO_MO=auto\|trn2\|aten\|legacy` | Native path: force the `ao_to_mo_transform` executor (`trn2` = the Trn2 kernel, or `ao_to_mo_trn2_packed_kernel` for a packed 2-D `eri`; `legacy` = the original kernel, deprecated there; `aten` and `legacy` unpack a packed `eri` first). |
123
129
  | `TRNTENSOR_DF_EXCHANGE=auto\|trn2\|aten` | Native path: force the `df_exchange` executor (`trn2` = `df_half_packed_kernel` + `df_exchange_syrk_kernel` on packed integrals; `aten` = row gather + einsum + one GEMM). `auto` uses the kernels for device-resident packed `B` from `nocc·nbasis²·naux` = 1e7 (#60). |
@@ -252,7 +258,9 @@ Kernel status on the native path after #51:
252
258
  - **AO→MO: kept, rewritten for Trn2** (`ao_to_mo_trn2_kernel`). It is
253
259
  1.3–1.6× faster than the two aten einsums at nbasis 256–768, and native
254
260
  `"auto"` uses it there for device-resident `eri`. Below nbasis ≈ 256
255
- `"auto"` keeps aten.
261
+ `"auto"` keeps aten. Since #80 it takes any `nocc` (occupied blocks of
262
+ ≤ 128 in stage 1); at nocc 160–384 it is 1.8–2.4× faster than aten (full
263
+ `eri`) and 2.7–3.5× (packed), at nbasis 512.
256
264
  - **MP2 energy: kept, rewritten for Trn2** (`mp2_energy_row_kernel`). It is
257
265
  1.25–1.9× faster than aten once each row launch carries at least 3 GFLOP,
258
266
  and native `"auto"` uses it there.
@@ -267,22 +275,21 @@ The original AO→MO and MP2 kernels remain the XLA-path (Trn1) kernels. See
267
275
  - `batched_matmul_kernel(a, b)` — per-batch-slice matmul. Batch dim
268
276
  iterated via `nl.affine_range`; each slice reuses the stationary-A
269
277
  tile layout.
270
- - `df_half_packed_kernel(eri_v, img, C_occ, nub)` and
278
+ - `df_half_packed_kernel(eri_v, img, C_occ, nub, tp, tail)` and
271
279
  `df_exchange_syrk_kernel(xt, kin, banks)` — the fused DF exchange build
272
280
  behind `trntensor.df_exchange` (#60): packed-row gather and half transform
273
281
  into `Xt[P, i, ν]` (aux on partitions), then the PSUM SYRK over aux tiles
274
282
  with upper-triangle strips mirrored on the Tensor Engine. Tilings from
275
- `df_half_tiling`, `df_group_chunks` and `df_exchange_blocks`.
283
+ `df_half_tiling`, `df_group_chunks`, `df_groups`, `df_syrk_tiles` and
284
+ `df_exchange_blocks`.
276
285
  - `df_coulomb_d_kernel(B, pw)` and `df_coulomb_j_kernel(B, d_b)` — the DF
277
286
  Coulomb build behind `trntensor.df_coulomb` (#60): `d = Bᵀ pw` and
278
287
  `J = B d`, each streaming packed `B` once in 128-row tiles on the Vector
279
288
  Engine.
280
289
 
281
- The DF-MP2 pair pattern `einsum("ap,bp->ab")` is served by
282
- `matmul_kernel` through the planner's `transB=True` route — no
283
- dedicated kernel needed. Fused energy-denominator kernel is tracked
284
- in [#13][i13].
285
-
286
- [i13]: https://github.com/trnsci/trntensor/issues/13
290
+ The DF-MP2 pair pattern `einsum("ap,bp->ab")` is planned as `matmul` with
291
+ `transB=True`: aten on the native path, `matmul_kernel` on the XLA path
292
+ above the FLOP threshold. The fused contraction + energy-denominator
293
+ kernel (#13) is `trntensor.mp2_energy`.
287
294
 
288
295
  [m2]: https://github.com/trnsci/trntensor/milestone/2
@@ -28,7 +28,7 @@ Call `clear_plan_cache()` to invalidate (e.g. after a backend change).
28
28
  |-------|--------------------|
29
29
  | `"fast"` | Default; NKI kernels eligible for matmul/bmm strategies |
30
30
  | `"kahan"` | Signals fp64 promotion at execution time; plan is still computed but `einsum` will use `torch.einsum` in fp64 regardless of strategy |
31
- | `"dd"` | Raises `NotImplementedError` at execution time (trnblas#22 pending) |
31
+ | `"dd"` | On Trainium hosts raises `NotImplementedError` at execution time (trnblas#22 pending); on CPU, Ozaki-2 emulation for `matmul` plans, `"kahan"` otherwise |
32
32
  | `"sr"` | Stochastic rounding at the PSUM→SBUF downcast for matmul/bmm strategies. On Trainium requires `NEURON_RT_STOCHASTIC_ROUNDING_EN=1` at process start, otherwise one-time `RuntimeWarning` and round-to-nearest; see [einsum](einsum.md#stochastic-rounding-on-trainium-is-a-runtime-setting) |
33
33
 
34
34
  ```python
@@ -58,10 +58,16 @@ the planner returns.
58
58
 
59
59
  ### Strategy selection
60
60
 
61
- - **`"matmul"`** — 2-operand, 2D tensors, single contracted index → `torch.matmul` (or NKI kernel when backend is `"nki"`)
62
- - **`"bmm"`** — 2-operand, 3D tensors, single contracted index + single batch index → `torch.bmm` (or NKI batched kernel)
63
- - **`"torch"`** — 2-operand fallback for patterns that don't fit matmul/bmm → `torch.einsum`
64
- - **`"path"`** — 3+ operands → greedy binary contraction ordering; each binary step is dispatched through the full backend-selection stack so large sub-contractions still reach NKI
61
+ - **`"matmul"`** — 2-operand, 2D tensors, single contracted index, output in the order `[A free, B free]` (e.g. `ij,jk->ik`, `ap,bp->ab`) → `torch.matmul` (or NKI kernel when backend is `"nki"`)
62
+ - **`"bmm"`** — exactly `bij,bjk->bik` → `torch.bmm` (or NKI batched kernel)
63
+ - **`"torch"`** — 2-operand fallback for patterns that don't fit matmul/bmm, including transposed or permuted outputs such as `ij,jk->ki` → `torch.einsum`
64
+ - **`"path"`** — 3+ operands → greedy binary contraction ordering; each binary step is dispatched through the full backend-selection stack
65
+
66
+ Before 0.22.0 the index-order conditions above were not checked, so
67
+ transposed or permuted outputs came back in the wrong layout on every backend
68
+ (#64). On the native TorchNeuron path `backend` reports `"pytorch"` for
69
+ matmul/bmm by default: they run on aten on the device (the NKI matmul kernels are
70
+ deprecated there).
65
71
 
66
72
  ## `estimate_flops(subscripts, *operands) -> int`
67
73