trntensor 0.22.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.
- {trntensor-0.22.0 → trntensor-0.23.0}/CHANGELOG.md +121 -0
- {trntensor-0.22.0 → trntensor-0.23.0}/CLAUDE.md +7 -3
- {trntensor-0.22.0/trntensor.egg-info → trntensor-0.23.0}/PKG-INFO +31 -12
- {trntensor-0.22.0 → trntensor-0.23.0}/README.md +30 -11
- {trntensor-0.22.0 → trntensor-0.23.0}/docs/api/einsum.md +17 -11
- {trntensor-0.22.0 → trntensor-0.23.0}/docs/api/nki.md +16 -10
- {trntensor-0.22.0 → trntensor-0.23.0}/docs/api/plan.md +11 -5
- {trntensor-0.22.0 → trntensor-0.23.0}/docs/api/quantum.md +36 -7
- {trntensor-0.22.0 → trntensor-0.23.0}/docs/architecture.md +24 -12
- {trntensor-0.22.0 → trntensor-0.23.0}/docs/benchmarks.md +15 -3
- {trntensor-0.22.0 → trntensor-0.23.0}/docs/contraction_patterns.md +27 -17
- trntensor-0.23.0/docs/index.md +41 -0
- trntensor-0.23.0/docs/installation.md +26 -0
- {trntensor-0.22.0 → trntensor-0.23.0}/docs/quickstart.md +12 -8
- {trntensor-0.22.0 → trntensor-0.23.0}/docs/tutorial_df_mp2.md +30 -28
- {trntensor-0.22.0 → trntensor-0.23.0}/pyproject.toml +1 -1
- trntensor-0.23.0/scripts/bench_ao2mo_nocc.py +173 -0
- trntensor-0.23.0/tests/conftest.py +81 -0
- trntensor-0.23.0/tests/test_conformance.py +1156 -0
- {trntensor-0.22.0 → trntensor-0.23.0}/tests/test_decompose.py +32 -0
- {trntensor-0.22.0 → trntensor-0.23.0}/tests/test_einsum.py +204 -0
- {trntensor-0.22.0 → trntensor-0.23.0}/tests/test_native_dispatch.py +196 -1
- {trntensor-0.22.0 → trntensor-0.23.0}/tests/test_nki_compile.py +28 -0
- {trntensor-0.22.0 → trntensor-0.23.0}/tests/test_nki_native.py +19 -2
- {trntensor-0.22.0 → trntensor-0.23.0}/tests/test_nki_sim.py +27 -0
- trntensor-0.23.0/tests/test_trace_cache_safety.py +563 -0
- {trntensor-0.22.0 → trntensor-0.23.0}/trntensor/__init__.py +1 -1
- {trntensor-0.22.0 → trntensor-0.23.0}/trntensor/decompose.py +5 -3
- {trntensor-0.22.0 → trntensor-0.23.0}/trntensor/einsum.py +75 -7
- {trntensor-0.22.0 → trntensor-0.23.0}/trntensor/nki/_kernels.py +89 -28
- {trntensor-0.22.0 → trntensor-0.23.0}/trntensor/nki/dispatch.py +150 -7
- {trntensor-0.22.0 → trntensor-0.23.0}/trntensor/plan.py +15 -7
- {trntensor-0.22.0 → trntensor-0.23.0}/trntensor/quantum.py +59 -10
- {trntensor-0.22.0 → trntensor-0.23.0/trntensor.egg-info}/PKG-INFO +31 -12
- {trntensor-0.22.0 → trntensor-0.23.0}/trntensor.egg-info/SOURCES.txt +3 -0
- trntensor-0.22.0/docs/index.md +0 -35
- trntensor-0.22.0/docs/installation.md +0 -21
- trntensor-0.22.0/tests/conftest.py +0 -49
- {trntensor-0.22.0 → trntensor-0.23.0}/.github/workflows/ci.yml +0 -0
- {trntensor-0.22.0 → trntensor-0.23.0}/.github/workflows/notify-umbrella.yml +0 -0
- {trntensor-0.22.0 → trntensor-0.23.0}/.github/workflows/publish.yml +0 -0
- {trntensor-0.22.0 → trntensor-0.23.0}/.gitignore +0 -0
- {trntensor-0.22.0 → trntensor-0.23.0}/.pre-commit-config.yaml +0 -0
- {trntensor-0.22.0 → trntensor-0.23.0}/CODE_OF_CONDUCT.md +0 -0
- {trntensor-0.22.0 → trntensor-0.23.0}/CONTRIBUTING.md +0 -0
- {trntensor-0.22.0 → trntensor-0.23.0}/LICENSE +0 -0
- {trntensor-0.22.0 → trntensor-0.23.0}/benchmarks/__init__.py +0 -0
- {trntensor-0.22.0 → trntensor-0.23.0}/benchmarks/bench_einsum.py +0 -0
- {trntensor-0.22.0 → trntensor-0.23.0}/docs/api/decompose.md +0 -0
- {trntensor-0.22.0 → trntensor-0.23.0}/docs/api/index.md +0 -0
- {trntensor-0.22.0 → trntensor-0.23.0}/docs/aws_setup.md +0 -0
- {trntensor-0.22.0 → trntensor-0.23.0}/docs/design/matmul_dispatch_findings.md +0 -0
- {trntensor-0.22.0 → trntensor-0.23.0}/docs/design/matmul_kernel_profile.md +0 -0
- {trntensor-0.22.0 → trntensor-0.23.0}/docs/developing_kernels.md +0 -0
- {trntensor-0.22.0 → trntensor-0.23.0}/examples/df_mp2_einsum.py +0 -0
- {trntensor-0.22.0 → trntensor-0.23.0}/infra/terraform/README.md +0 -0
- {trntensor-0.22.0 → trntensor-0.23.0}/infra/terraform/main.tf +0 -0
- {trntensor-0.22.0 → trntensor-0.23.0}/mkdocs.yml +0 -0
- {trntensor-0.22.0 → trntensor-0.23.0}/scripts/autotune_dispatch.py +0 -0
- {trntensor-0.22.0 → trntensor-0.23.0}/scripts/bench_native_einsum.py +0 -0
- {trntensor-0.22.0 → trntensor-0.23.0}/scripts/bench_native_kernels.py +0 -0
- {trntensor-0.22.0 → trntensor-0.23.0}/scripts/bench_to_md.py +0 -0
- {trntensor-0.22.0 → trntensor-0.23.0}/scripts/run_benchmarks.sh +0 -0
- {trntensor-0.22.0 → trntensor-0.23.0}/scripts/run_neuron_profile.sh +0 -0
- {trntensor-0.22.0 → trntensor-0.23.0}/scripts/run_neuron_tests.sh +0 -0
- {trntensor-0.22.0 → trntensor-0.23.0}/scripts/run_reduce_parallel_multirank.py +0 -0
- {trntensor-0.22.0 → trntensor-0.23.0}/scripts/run_simulator_tests.sh +0 -0
- {trntensor-0.22.0 → trntensor-0.23.0}/setup.cfg +0 -0
- {trntensor-0.22.0 → trntensor-0.23.0}/tests/test_host_output.py +0 -0
- {trntensor-0.22.0 → trntensor-0.23.0}/tests/test_nki.py +0 -0
- {trntensor-0.22.0 → trntensor-0.23.0}/tests/test_nki_kernels.py +0 -0
- {trntensor-0.22.0 → trntensor-0.23.0}/tests/test_nki_tail_audit.py +0 -0
- {trntensor-0.22.0 → trntensor-0.23.0}/tests/test_parallel.py +0 -0
- {trntensor-0.22.0 → trntensor-0.23.0}/tests/test_plan.py +0 -0
- {trntensor-0.22.0 → trntensor-0.23.0}/tests/test_quantum.py +0 -0
- {trntensor-0.22.0 → trntensor-0.23.0}/tests/test_sr_runtime.py +0 -0
- {trntensor-0.22.0 → trntensor-0.23.0}/trntensor/nki/__init__.py +0 -0
- {trntensor-0.22.0 → trntensor-0.23.0}/trntensor/nki/_native.py +0 -0
- {trntensor-0.22.0 → trntensor-0.23.0}/trntensor/nki/collectives.py +0 -0
- {trntensor-0.22.0 → trntensor-0.23.0}/trntensor/parallel.py +0 -0
- {trntensor-0.22.0 → trntensor-0.23.0}/trntensor/py.typed +0 -0
- {trntensor-0.22.0 → trntensor-0.23.0}/trntensor.egg-info/dependency_links.txt +0 -0
- {trntensor-0.22.0 → trntensor-0.23.0}/trntensor.egg-info/requires.txt +0 -0
- {trntensor-0.22.0 → trntensor-0.23.0}/trntensor.egg-info/top_level.txt +0 -0
- {trntensor-0.22.0 → trntensor-0.23.0}/uv.lock +0 -0
|
@@ -7,6 +7,127 @@ 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
|
+
|
|
10
131
|
## [0.22.0] — 2026-11-01
|
|
11
132
|
|
|
12
133
|
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**:
|
|
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.
|
|
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)** (
|
|
58
|
-
- **[Phase 2 — precision](https://github.com/trnsci/trntensor/issues/28)
|
|
59
|
-
- **[Phase 3 — perf](https://github.com/trnsci/trntensor/issues/29)
|
|
60
|
-
- **[Phase 4 — multi-chip](https://github.com/trnsci/trntensor/issues/30)
|
|
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 / `"
|
|
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/
|
|
156
|
-
| [trnblas](https://github.com/
|
|
157
|
-
| [trnsolver](https://github.com/
|
|
158
|
-
| [trnrand](https://github.com/
|
|
159
|
-
| [trnsparse](https://github.com/
|
|
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)** (
|
|
20
|
-
- **[Phase 2 — precision](https://github.com/trnsci/trntensor/issues/28)
|
|
21
|
-
- **[Phase 3 — perf](https://github.com/trnsci/trntensor/issues/29)
|
|
22
|
-
- **[Phase 4 — multi-chip](https://github.com/trnsci/trntensor/issues/30)
|
|
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 / `"
|
|
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/
|
|
118
|
-
| [trnblas](https://github.com/
|
|
119
|
-
| [trnsolver](https://github.com/
|
|
120
|
-
| [trnrand](https://github.com/
|
|
121
|
-
| [trnsparse](https://github.com/
|
|
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.
|
|
25
|
-
|
|
26
|
-
`
|
|
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
|
|
74
|
-
|
|
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
|
-
#
|
|
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
|
|
160
|
-
before executing the loop,
|
|
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"` —
|
|
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
|
|
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
|
|
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.
|
|
@@ -279,11 +287,9 @@ The original AO→MO and MP2 kernels remain the XLA-path (Trn1) kernels. See
|
|
|
279
287
|
`J = B d`, each streaming packed `B` once in 128-row tiles on the Vector
|
|
280
288
|
Engine.
|
|
281
289
|
|
|
282
|
-
The DF-MP2 pair pattern `einsum("ap,bp->ab")` is
|
|
283
|
-
`
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
[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`.
|
|
288
294
|
|
|
289
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"` |
|
|
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"`** —
|
|
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
|
|
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
|
|
|
@@ -176,12 +176,19 @@ Kernel alone, device-resident, warm (fp32; padded `nvir` included):
|
|
|
176
176
|
no DMA transposes.
|
|
177
177
|
- Stage 1: `X[i, ν, P] = Σ_μ C_occ[μ, i] eri[μ, ν, P]`.
|
|
178
178
|
- Stage 2: `B[i, a, P] = Σ_ν C_vir[ν, a] X[i, ν, P]`.
|
|
179
|
-
- The auxiliary index is split across both physical cores at LNC=2.
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
179
|
+
- The auxiliary index is split across both physical cores at LNC=2. With
|
|
180
|
+
an odd aux-chunk count and `nocc > 128`, the occupied index is split
|
|
181
|
+
instead.
|
|
182
|
+
- Any `nocc` (#80): stage 1 runs occupied blocks of ≤ 128 over each eri
|
|
183
|
+
block, so `eri` is read once per program.
|
|
184
|
+
- `"auto"` uses the kernel for device-resident `eri` when its SBUF
|
|
185
|
+
working set fits and the stage-1 work `nocc·nbasis²·naux` is at least
|
|
186
|
+
5e8 multiply-adds (about nbasis ≥ 256). Smaller problems lose to the
|
|
187
|
+
launch floor. Two exceptions (#80):
|
|
188
|
+
- A full `eri` whose nbasis or naux is not a 128 multiple runs aten. The
|
|
189
|
+
kernel would pad a copy of the whole square, which costs more than
|
|
190
|
+
aten. Pass the packed form to keep the kernel there.
|
|
191
|
+
- A packed `eri` with nbasis not a 128 multiple needs 2e9 multiply-adds.
|
|
185
192
|
`TRNTENSOR_AO_TO_MO=auto|trn2|aten|legacy` overrides the choice.
|
|
186
193
|
|
|
187
194
|
Measured on Trn2 (fp32, one LNC=2 core, device-resident, warm, #51):
|
|
@@ -199,6 +206,26 @@ Kernel alone, device-resident, warm (fp32; padded `nvir` included):
|
|
|
199
206
|
nbasis 64 and 128 it measured 0.79 and 0.57 ms with the kernel forced,
|
|
200
207
|
which is why `"auto"` keeps aten there.
|
|
201
208
|
|
|
209
|
+
Above `nocc = 128` (#80; Trn2, one logical core, fp32, warm medians in ms,
|
|
210
|
+
`nvir = nbasis − nocc`). "Per-128 launches" is the `nocc ≤ 128` kernel run
|
|
211
|
+
once per 128 orbitals, which re-reads `eri` each time:
|
|
212
|
+
|
|
213
|
+
| nbasis, naux | LNC | nocc | aten, full / packed | per-128 launches, full / packed | Trn2 kernel, full / packed |
|
|
214
|
+
|---|---|---|---|---|---|
|
|
215
|
+
| 512, 1536 | 2 | 160 | 17.2 / 25.1 | 14.1 / 13.5 | **9.4 / 9.2** |
|
|
216
|
+
| 512, 1536 | 2 | 256 | 24.8 / 34.8 | 15.3 / 14.6 | **10.4 / 9.9** |
|
|
217
|
+
| 512, 1536 | 2 | 384 | 29.5 / 37.0 | 21.5 / 19.7 | **12.6 / 12.7** |
|
|
218
|
+
| 512, 1536 | 1 | 160 | 24.8 / 43.1 | 26.7 / 27.6 | **18.7 / 18.7** |
|
|
219
|
+
| 512, 1536 | 1 | 256 | 33.1 / 52.1 | 28.2 / 29.6 | **20.3 / 20.4** |
|
|
220
|
+
| 512, 1536 | 1 | 384 | 41.0 / 60.2 | 38.7 / 40.3 | **24.3 / 24.7** |
|
|
221
|
+
| 768, 2304 | 2 | 160 | 37.5* / 65.0* | 41.7 / 47.6 | **30.2 / 30.6** |
|
|
222
|
+
| 768, 2304 | 2 | 256 | 84.9* / 112.9* | 44.3 / 51.8 | **33.7 / 33.6** |
|
|
223
|
+
| 768, 2304 | 2 | 384 | 112.9* / 140.6* | 59.2 / 70.7 | **45.3 / 45.4** |
|
|
224
|
+
|
|
225
|
+
\* At nbasis 768 / naux 2304 the aten result was wrong on the device
|
|
226
|
+
(max error ~1 against float64), while the kernel matched float64 to 2e-7;
|
|
227
|
+
aten was correct at nbasis 736. Its timings there are for reference only.
|
|
228
|
+
|
|
202
229
|
- **Host inputs on native TorchNeuron (#56).** Under `"auto"`, with `eri`,
|
|
203
230
|
`C_occ` and `C_vir` all on the host, the inputs are uploaded (`eri` in the
|
|
204
231
|
layout given), the device route above runs, and the result is downloaded,
|
|
@@ -215,7 +242,9 @@ Kernel alone, device-resident, warm (fp32; padded `nvir` included):
|
|
|
215
242
|
|
|
216
243
|
### Current limitations
|
|
217
244
|
|
|
218
|
-
-
|
|
245
|
+
- The original (XLA-path) kernel needs `nocc ≤ 128` and raises `NotImplementedError` above it. The Trn2 kernels take any `nocc`.
|
|
246
|
+
- At `nocc` just above a multiple of 128 the Trn2 kernel pays for a whole extra block in stage 1 (the occupied block is the stationary operand, and a pass costs the same at any width). For example, nocc 160 at nbasis 768 costs 30 ms against 21 ms at 128.
|
|
247
|
+
- The first call per shape compiles for minutes at large sizes (traced loops unroll): 4–11 min at nbasis 768 / naux 2304 for a full `eri`, 1–4.5 min packed. The NEFF cache keeps that to one compile per shape.
|
|
219
248
|
- The `(nocc, nbasis, naux)` intermediate goes through kernel-scratch HBM between the two stages, as it does between the two aten einsums. Stage 1 leaves `i` on the partition axis, and stage 2 needs `ν` there.
|
|
220
249
|
|
|
221
250
|
## `trntensor.df_exchange(B, C_occ, alpha=1.0) -> Tensor`
|