trntensor 0.24.0__tar.gz → 0.25.1__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.24.0 → trntensor-0.25.1}/.github/workflows/ci.yml +2 -2
- {trntensor-0.24.0 → trntensor-0.25.1}/.pre-commit-config.yaml +4 -2
- {trntensor-0.24.0 → trntensor-0.25.1}/CHANGELOG.md +69 -0
- {trntensor-0.24.0/trntensor.egg-info → trntensor-0.25.1}/PKG-INFO +11 -4
- {trntensor-0.24.0 → trntensor-0.25.1}/README.md +9 -2
- {trntensor-0.24.0 → trntensor-0.25.1}/docs/api/nki.md +1 -0
- {trntensor-0.24.0 → trntensor-0.25.1}/docs/api/quantum.md +35 -0
- {trntensor-0.24.0 → trntensor-0.25.1}/pyproject.toml +2 -2
- {trntensor-0.24.0 → trntensor-0.25.1}/tests/test_kernel_dtype_guard.py +24 -3
- {trntensor-0.24.0 → trntensor-0.25.1}/tests/test_native_dispatch.py +111 -2
- trntensor-0.25.1/tests/test_neuron_span.py +119 -0
- trntensor-0.25.1/tests/test_neuron_span_device.py +99 -0
- {trntensor-0.24.0 → trntensor-0.25.1}/tests/test_nki_compile.py +21 -0
- {trntensor-0.24.0 → trntensor-0.25.1}/tests/test_nki_native.py +41 -0
- {trntensor-0.24.0 → trntensor-0.25.1}/trntensor/__init__.py +1 -1
- {trntensor-0.24.0 → trntensor-0.25.1}/trntensor/nki/_kernels.py +253 -0
- {trntensor-0.24.0 → trntensor-0.25.1}/trntensor/nki/_native.py +15 -0
- {trntensor-0.24.0 → trntensor-0.25.1}/trntensor/nki/dispatch.py +359 -13
- {trntensor-0.24.0 → trntensor-0.25.1}/trntensor/quantum.py +14 -3
- {trntensor-0.24.0 → trntensor-0.25.1/trntensor.egg-info}/PKG-INFO +11 -4
- {trntensor-0.24.0 → trntensor-0.25.1}/trntensor.egg-info/SOURCES.txt +2 -0
- {trntensor-0.24.0 → trntensor-0.25.1}/trntensor.egg-info/requires.txt +1 -1
- {trntensor-0.24.0 → trntensor-0.25.1}/.github/workflows/notify-umbrella.yml +0 -0
- {trntensor-0.24.0 → trntensor-0.25.1}/.github/workflows/publish.yml +0 -0
- {trntensor-0.24.0 → trntensor-0.25.1}/.gitignore +0 -0
- {trntensor-0.24.0 → trntensor-0.25.1}/CLAUDE.md +0 -0
- {trntensor-0.24.0 → trntensor-0.25.1}/CODE_OF_CONDUCT.md +0 -0
- {trntensor-0.24.0 → trntensor-0.25.1}/CONTRIBUTING.md +0 -0
- {trntensor-0.24.0 → trntensor-0.25.1}/LICENSE +0 -0
- {trntensor-0.24.0 → trntensor-0.25.1}/benchmarks/__init__.py +0 -0
- {trntensor-0.24.0 → trntensor-0.25.1}/benchmarks/bench_einsum.py +0 -0
- {trntensor-0.24.0 → trntensor-0.25.1}/docs/api/decompose.md +0 -0
- {trntensor-0.24.0 → trntensor-0.25.1}/docs/api/einsum.md +0 -0
- {trntensor-0.24.0 → trntensor-0.25.1}/docs/api/index.md +0 -0
- {trntensor-0.24.0 → trntensor-0.25.1}/docs/api/plan.md +0 -0
- {trntensor-0.24.0 → trntensor-0.25.1}/docs/api/stream.md +0 -0
- {trntensor-0.24.0 → trntensor-0.25.1}/docs/architecture.md +0 -0
- {trntensor-0.24.0 → trntensor-0.25.1}/docs/aws_setup.md +0 -0
- {trntensor-0.24.0 → trntensor-0.25.1}/docs/benchmarks.md +0 -0
- {trntensor-0.24.0 → trntensor-0.25.1}/docs/contraction_patterns.md +0 -0
- {trntensor-0.24.0 → trntensor-0.25.1}/docs/design/matmul_dispatch_findings.md +0 -0
- {trntensor-0.24.0 → trntensor-0.25.1}/docs/design/matmul_kernel_profile.md +0 -0
- {trntensor-0.24.0 → trntensor-0.25.1}/docs/developing_kernels.md +0 -0
- {trntensor-0.24.0 → trntensor-0.25.1}/docs/index.md +0 -0
- {trntensor-0.24.0 → trntensor-0.25.1}/docs/installation.md +0 -0
- {trntensor-0.24.0 → trntensor-0.25.1}/docs/quickstart.md +0 -0
- {trntensor-0.24.0 → trntensor-0.25.1}/docs/tutorial_df_mp2.md +0 -0
- {trntensor-0.24.0 → trntensor-0.25.1}/examples/df_mp2_einsum.py +0 -0
- {trntensor-0.24.0 → trntensor-0.25.1}/infra/terraform/README.md +0 -0
- {trntensor-0.24.0 → trntensor-0.25.1}/infra/terraform/main.tf +0 -0
- {trntensor-0.24.0 → trntensor-0.25.1}/mkdocs.yml +0 -0
- {trntensor-0.24.0 → trntensor-0.25.1}/scripts/autotune_dispatch.py +0 -0
- {trntensor-0.24.0 → trntensor-0.25.1}/scripts/bench_ao2mo_nocc.py +0 -0
- {trntensor-0.24.0 → trntensor-0.25.1}/scripts/bench_eager_handle.py +0 -0
- {trntensor-0.24.0 → trntensor-0.25.1}/scripts/bench_native_einsum.py +0 -0
- {trntensor-0.24.0 → trntensor-0.25.1}/scripts/bench_native_kernels.py +0 -0
- {trntensor-0.24.0 → trntensor-0.25.1}/scripts/bench_to_md.py +0 -0
- {trntensor-0.24.0 → trntensor-0.25.1}/scripts/run_benchmarks.sh +0 -0
- {trntensor-0.24.0 → trntensor-0.25.1}/scripts/run_neuron_profile.sh +0 -0
- {trntensor-0.24.0 → trntensor-0.25.1}/scripts/run_neuron_tests.sh +0 -0
- {trntensor-0.24.0 → trntensor-0.25.1}/scripts/run_reduce_parallel_multirank.py +0 -0
- {trntensor-0.24.0 → trntensor-0.25.1}/scripts/run_simulator_tests.sh +0 -0
- {trntensor-0.24.0 → trntensor-0.25.1}/setup.cfg +0 -0
- {trntensor-0.24.0 → trntensor-0.25.1}/tests/conftest.py +0 -0
- {trntensor-0.24.0 → trntensor-0.25.1}/tests/test_conformance.py +0 -0
- {trntensor-0.24.0 → trntensor-0.25.1}/tests/test_decompose.py +0 -0
- {trntensor-0.24.0 → trntensor-0.25.1}/tests/test_einsum.py +0 -0
- {trntensor-0.24.0 → trntensor-0.25.1}/tests/test_host_output.py +0 -0
- {trntensor-0.24.0 → trntensor-0.25.1}/tests/test_nki.py +0 -0
- {trntensor-0.24.0 → trntensor-0.25.1}/tests/test_nki_kernels.py +0 -0
- {trntensor-0.24.0 → trntensor-0.25.1}/tests/test_nki_sim.py +0 -0
- {trntensor-0.24.0 → trntensor-0.25.1}/tests/test_nki_tail_audit.py +0 -0
- {trntensor-0.24.0 → trntensor-0.25.1}/tests/test_parallel.py +0 -0
- {trntensor-0.24.0 → trntensor-0.25.1}/tests/test_plan.py +0 -0
- {trntensor-0.24.0 → trntensor-0.25.1}/tests/test_quantum.py +0 -0
- {trntensor-0.24.0 → trntensor-0.25.1}/tests/test_sr_runtime.py +0 -0
- {trntensor-0.24.0 → trntensor-0.25.1}/tests/test_stream.py +0 -0
- {trntensor-0.24.0 → trntensor-0.25.1}/tests/test_trace_cache_safety.py +0 -0
- {trntensor-0.24.0 → trntensor-0.25.1}/trntensor/decompose.py +0 -0
- {trntensor-0.24.0 → trntensor-0.25.1}/trntensor/einsum.py +0 -0
- {trntensor-0.24.0 → trntensor-0.25.1}/trntensor/nki/__init__.py +0 -0
- {trntensor-0.24.0 → trntensor-0.25.1}/trntensor/nki/collectives.py +0 -0
- {trntensor-0.24.0 → trntensor-0.25.1}/trntensor/parallel.py +0 -0
- {trntensor-0.24.0 → trntensor-0.25.1}/trntensor/plan.py +0 -0
- {trntensor-0.24.0 → trntensor-0.25.1}/trntensor/py.typed +0 -0
- {trntensor-0.24.0 → trntensor-0.25.1}/trntensor/stream.py +0 -0
- {trntensor-0.24.0 → trntensor-0.25.1}/trntensor.egg-info/dependency_links.txt +0 -0
- {trntensor-0.24.0 → trntensor-0.25.1}/trntensor.egg-info/top_level.txt +0 -0
- {trntensor-0.24.0 → trntensor-0.25.1}/uv.lock +0 -0
|
@@ -17,8 +17,8 @@ jobs:
|
|
|
17
17
|
steps:
|
|
18
18
|
- uses: actions/checkout@v6
|
|
19
19
|
- uses: astral-sh/setup-uv@v6
|
|
20
|
-
- run: uvx ruff check .
|
|
21
|
-
- run: uvx ruff format --check .
|
|
20
|
+
- run: uvx ruff@0.17.3 check .
|
|
21
|
+
- run: uvx ruff@0.17.3 format --check .
|
|
22
22
|
|
|
23
23
|
test:
|
|
24
24
|
runs-on: ubuntu-latest
|
|
@@ -3,9 +3,11 @@
|
|
|
3
3
|
# CI re-checks the same rules in the lint job — pre-commit keeps CI green.
|
|
4
4
|
|
|
5
5
|
repos:
|
|
6
|
+
# Keep rev in lockstep with the ruff pin in .github/workflows/ci.yml
|
|
7
|
+
# (and the [dev] extra in pyproject.toml) so local and CI agree.
|
|
6
8
|
- repo: https://github.com/astral-sh/ruff-pre-commit
|
|
7
|
-
rev: v0.
|
|
9
|
+
rev: v0.17.3
|
|
8
10
|
hooks:
|
|
9
|
-
- id: ruff
|
|
11
|
+
- id: ruff-check
|
|
10
12
|
args: [--fix]
|
|
11
13
|
- id: ruff-format
|
|
@@ -7,6 +7,75 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [0.25.1] — 2026-11-07
|
|
11
|
+
|
|
12
|
+
Validated on Trn2 (trn2.3xlarge, Neuron SDK 2.33.0, native TorchNeuron) — see trnsci/trnsci#35.
|
|
13
|
+
|
|
14
|
+
Headline: device matmuls with 32 MiB or longer rows (an operand's 128 partitions spanning 4 GiB) are no longer silently wrong (#123, aws-neuron-sdk#1457).
|
|
15
|
+
|
|
16
|
+
### Fixed
|
|
17
|
+
|
|
18
|
+
- **Device matmuls with 32 MiB or longer rows were silently wrong (#123,
|
|
19
|
+
trnblas#112, aws-neuron-sdk#1457).** On the native TorchNeuron path (Trn2,
|
|
20
|
+
SDK 2.33), a device matmul returns wrong values with no error (or aborts
|
|
21
|
+
with NRT status 1203) once an operand's 128 DMA partitions span 4 GiB or
|
|
22
|
+
more, i.e. `min(rows, 128) * row_bytes >= 2**32`, and that includes a
|
|
23
|
+
contiguous left operand. Example: fp32 `(96, 128) @ (128, 8,389,632)` gave
|
|
24
|
+
rel err 0.8. Affected: the contiguous-LHS `ao_to_mo_transform` aten route
|
|
25
|
+
of #98 (0.23.0–0.25.0) when `nbasis * naux >= 8,388,608` (fp32), and
|
|
26
|
+
device `einsum` matmul / bmm plans (`nki_matmul`, `nki_batched_matmul`)
|
|
27
|
+
with such operands (0.17.0–0.25.0). These products are now cut into
|
|
28
|
+
contiguous row blocks of the long operand, each block spanning at most 2 GiB:
|
|
29
|
+
K is split (partial sums), or M / N (concatenated results). No operand is
|
|
30
|
+
copied. Splitting K changes the fp32 summation order of the affected products
|
|
31
|
+
only. Every other product is the same aten call as before (bitwise
|
|
32
|
+
unchanged). Standard DF ratios (naux about 3 × nbasis) on one core do not
|
|
33
|
+
reach it. The `_ATEN_TRANSPOSED_LHS_MAX_BYTES` comment said exactly 2**32
|
|
34
|
+
bytes was correct. That was a lucky measurement (the operand sat above an
|
|
35
|
+
identical copy), and the comment is corrected; the guard already started
|
|
36
|
+
at 2**32.
|
|
37
|
+
|
|
38
|
+
## [0.25.0] — 2026-11-04
|
|
39
|
+
|
|
40
|
+
Validated on Trn2 (trn2.3xlarge, Neuron SDK 2.33.0, native TorchNeuron) — see trnsci/trnsci#35.
|
|
41
|
+
|
|
42
|
+
Headline: the blocked packed AO→MO kernel (#112, #113): `ao_to_mo_transform` on packed fp32 integrals stays on the device past the old SBUF budget (n_ao 3508 at 85 ms per chunk vs 8.6 s on the host), and `auto` picks it where the resident kernel does not fit.
|
|
43
|
+
|
|
44
|
+
### Fixed
|
|
45
|
+
|
|
46
|
+
- **Packed `ao_to_mo_transform` on the device past n_ao ≈ 1100–1300 (#112,
|
|
47
|
+
trnsci/trnsci#115).** `ao_to_mo_trn2_packed_kernel` keeps C_occ, C_vir, one
|
|
48
|
+
chunk's gather indices and a whole μ column of eri in SBUF. At n_ao 3508
|
|
49
|
+
(alkane60 / cc-pVTZ, 87-column aux chunks) that needs 979 KiB per
|
|
50
|
+
partition against a 176 KiB budget, and at n_ao 1658 it needs 282 KiB. Both
|
|
51
|
+
cases fell back to aten, which unpacks a full square per chunk (4.3 GB at
|
|
52
|
+
3508). That first call did not finish compiling in 19 minutes. The new
|
|
53
|
+
`ao_to_mo_trn2_packed_blocked_kernel` streams every operand over blocks of
|
|
54
|
+
up to 8 μ/ν tiles, and its PSUM accumulators live across the blocks. Its
|
|
55
|
+
SBUF footprint (71–78 KiB at the measured shapes) does not depend on n_ao.
|
|
56
|
+
The aux chunk width is the chunk itself up to 512 columns, otherwise a
|
|
57
|
+
divisor in [64, 512]. So an 87-column chunk goes in without the
|
|
58
|
+
128-column padding copy (3.1 GB at n_ao 3508). Under `"auto"`, fp32 packed
|
|
59
|
+
integrals take it wherever the resident kernel does not fit, and
|
|
60
|
+
`TRNTENSOR_AO_TO_MO_PACKED=auto|resident|blocked` overrides. Measured on
|
|
61
|
+
Trn2 with one core, fp32 and synthetic packed chunks:
|
|
62
|
+
- At n_ao 3508 / nocc 241 / 87 columns the kernel takes 85 ms per chunk at
|
|
63
|
+
LNC=2 and 94 ms at LNC=1. The host fp32 route on 2 threads takes 8.6 s.
|
|
64
|
+
The cold compile takes 162 s, and 26 s with the NEFF cache.
|
|
65
|
+
- At n_ao 1658 / 161 / 384 it takes 56 ms against 6.1 s on the host, with
|
|
66
|
+
a 128 s cold compile. At 2522 / 215 / 168 it takes 73 ms against 7.9 s,
|
|
67
|
+
with a 195 s cold compile.
|
|
68
|
+
- Its maximum error against an fp64 host reference is 3.2–4.4e-7 relative
|
|
69
|
+
(3.9e-7 at 3508). The host fp32 route gets 5.4–6.6e-7.
|
|
70
|
+
- A full alkane60-shaped pass through `AuxStream.ao_to_mo` took 43 s warm
|
|
71
|
+
and 102 s including the first-call compiles (NEFF cache warm from the
|
|
72
|
+
single-chunk runs). That is 97 chunks, 8400 aux and 207 GB uploaded. The
|
|
73
|
+
96 full chunks reuse one host tensor, so no disk reads are included.
|
|
74
|
+
The trnsci#115 run did this pass on the host in 673 s, reading the store
|
|
75
|
+
from NVMe.
|
|
76
|
+
- Where the resident kernel fits, the two kernels give bit-identical
|
|
77
|
+
results, because they run the same per-element PSUM chains.
|
|
78
|
+
|
|
10
79
|
## [0.24.0] — 2026-11-04
|
|
11
80
|
|
|
12
81
|
Validated on Trn2 (trn2.3xlarge, Neuron SDK 2.33.0, native TorchNeuron) — see trnsci/trnsci#35.
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: trntensor
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.25.1
|
|
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
|
|
@@ -29,7 +29,7 @@ Provides-Extra: dev
|
|
|
29
29
|
Requires-Dist: pytest<9,>=7.0; extra == "dev"
|
|
30
30
|
Requires-Dist: pytest-benchmark>=4.0; extra == "dev"
|
|
31
31
|
Requires-Dist: pytest-cov>=4.0; extra == "dev"
|
|
32
|
-
Requires-Dist: ruff
|
|
32
|
+
Requires-Dist: ruff==0.17.3; extra == "dev"
|
|
33
33
|
Requires-Dist: pre-commit>=3.7; extra == "dev"
|
|
34
34
|
Requires-Dist: scipy>=1.11; extra == "dev"
|
|
35
35
|
Requires-Dist: mkdocs>=1.5; extra == "dev"
|
|
@@ -169,7 +169,7 @@ core, factors = trntensor.tucker_decompose(tensor, ranks=(5, 5, 5))
|
|
|
169
169
|
|
|
170
170
|
## Known issues in earlier releases
|
|
171
171
|
|
|
172
|
-
Silent wrong-result bugs fixed in the 2026-10-31 to 2026-11-
|
|
172
|
+
Silent wrong-result bugs fixed in the 2026-10-31 to 2026-11-04 releases. Details are in the [CHANGELOG](CHANGELOG.md).
|
|
173
173
|
|
|
174
174
|
| Bug | Affected | Fixed in |
|
|
175
175
|
|-----|----------|----------|
|
|
@@ -180,7 +180,14 @@ Silent wrong-result bugs fixed in the 2026-10-31 to 2026-11-02 releases. Details
|
|
|
180
180
|
| `einsum(precision="dd")` on float32/float64 was less accurate than `"fast"` (CPU emulation, #84) | 0.17.0–0.22.0 | 0.23.0 |
|
|
181
181
|
| `multi_einsum` returned float32 results as float64 in mixed-dtype batches (#75) | 0.17.0–0.22.0 | 0.23.0 |
|
|
182
182
|
| NKI chemistry kernels returned inf for float64 operands, and some kernel routes changed the result dtype (#87) | `df_*` 0.21.0–0.22.0; `ao_to_mo_transform` every release with its kernels | 0.23.0 |
|
|
183
|
-
| `ao_to_mo_transform` aten route on the TorchNeuron device was wrong once `eri` exceeded 4 GiB (#97;
|
|
183
|
+
| `ao_to_mo_transform` aten route on the TorchNeuron device was wrong once `eri` exceeded 4 GiB (#97; the LNC=1 gather case is the 0.24.0 row below) | through 0.22.0 | 0.23.0 |
|
|
184
|
+
| `unpack_tril` / `pack_tril` (and the packed-`eri` aten / legacy routes of `ao_to_mo_transform`) on the neuron device at LNC=1 returned wrong data, at times NaN, once one gathered output reached 4 GiB (154 M mismatched elements at 4.99e9 bytes) (#97) | 0.20.0–0.23.0 | 0.24.0 |
|
|
185
|
+
| Complex `tucker_decompose` on the neuron device used `Uᵀ`, not `Uᴴ`: device matmul ignores the lazy conj bit of `.mH` (#89) | 0.23.0 | 0.24.0 |
|
|
186
|
+
| float64 on the neuron device rounded to fp32 in `gather` (device `torch.cat`) and in `pack_tril` / `unpack_tril` / `pack_density` / `unpack_packed` (device `index_select` / fancy indexing), rel. err ~4e-8 (#92) | through 0.23.0 | 0.24.0 |
|
|
187
|
+
| `multi_einsum` under `"auto"` pinned shared host float64 / complex128 operands to the device, where device aten computed them at fp32 precision (#92) | through 0.23.0 | 0.24.0 |
|
|
188
|
+
| Integer, bool, complex and mixed-dtype operands of `einsum` / `multi_einsum` reached the fp32/bf16/fp16 matmul kernels under `set_backend("nki")`, or under `"auto"` above the size thresholds (trnsci/trnsci#35) | through 0.23.0 | 0.24.0 |
|
|
189
|
+
| `einsum(..., out=)` never wrote `out`, so the documented `einsum(..., beta=1.0, out=acc)` accumulation left `acc` unchanged (#77) | 0.3.0–0.23.0 | 0.24.0 |
|
|
190
|
+
| `cp_reconstruct` returned float32 for bf16 / fp16 factors (#78) | 0.1.1–0.23.0 | 0.24.0 |
|
|
184
191
|
|
|
185
192
|
## Related Projects
|
|
186
193
|
|
|
@@ -131,7 +131,7 @@ core, factors = trntensor.tucker_decompose(tensor, ranks=(5, 5, 5))
|
|
|
131
131
|
|
|
132
132
|
## Known issues in earlier releases
|
|
133
133
|
|
|
134
|
-
Silent wrong-result bugs fixed in the 2026-10-31 to 2026-11-
|
|
134
|
+
Silent wrong-result bugs fixed in the 2026-10-31 to 2026-11-04 releases. Details are in the [CHANGELOG](CHANGELOG.md).
|
|
135
135
|
|
|
136
136
|
| Bug | Affected | Fixed in |
|
|
137
137
|
|-----|----------|----------|
|
|
@@ -142,7 +142,14 @@ Silent wrong-result bugs fixed in the 2026-10-31 to 2026-11-02 releases. Details
|
|
|
142
142
|
| `einsum(precision="dd")` on float32/float64 was less accurate than `"fast"` (CPU emulation, #84) | 0.17.0–0.22.0 | 0.23.0 |
|
|
143
143
|
| `multi_einsum` returned float32 results as float64 in mixed-dtype batches (#75) | 0.17.0–0.22.0 | 0.23.0 |
|
|
144
144
|
| NKI chemistry kernels returned inf for float64 operands, and some kernel routes changed the result dtype (#87) | `df_*` 0.21.0–0.22.0; `ao_to_mo_transform` every release with its kernels | 0.23.0 |
|
|
145
|
-
| `ao_to_mo_transform` aten route on the TorchNeuron device was wrong once `eri` exceeded 4 GiB (#97;
|
|
145
|
+
| `ao_to_mo_transform` aten route on the TorchNeuron device was wrong once `eri` exceeded 4 GiB (#97; the LNC=1 gather case is the 0.24.0 row below) | through 0.22.0 | 0.23.0 |
|
|
146
|
+
| `unpack_tril` / `pack_tril` (and the packed-`eri` aten / legacy routes of `ao_to_mo_transform`) on the neuron device at LNC=1 returned wrong data, at times NaN, once one gathered output reached 4 GiB (154 M mismatched elements at 4.99e9 bytes) (#97) | 0.20.0–0.23.0 | 0.24.0 |
|
|
147
|
+
| Complex `tucker_decompose` on the neuron device used `Uᵀ`, not `Uᴴ`: device matmul ignores the lazy conj bit of `.mH` (#89) | 0.23.0 | 0.24.0 |
|
|
148
|
+
| float64 on the neuron device rounded to fp32 in `gather` (device `torch.cat`) and in `pack_tril` / `unpack_tril` / `pack_density` / `unpack_packed` (device `index_select` / fancy indexing), rel. err ~4e-8 (#92) | through 0.23.0 | 0.24.0 |
|
|
149
|
+
| `multi_einsum` under `"auto"` pinned shared host float64 / complex128 operands to the device, where device aten computed them at fp32 precision (#92) | through 0.23.0 | 0.24.0 |
|
|
150
|
+
| Integer, bool, complex and mixed-dtype operands of `einsum` / `multi_einsum` reached the fp32/bf16/fp16 matmul kernels under `set_backend("nki")`, or under `"auto"` above the size thresholds (trnsci/trnsci#35) | through 0.23.0 | 0.24.0 |
|
|
151
|
+
| `einsum(..., out=)` never wrote `out`, so the documented `einsum(..., beta=1.0, out=acc)` accumulation left `acc` unchanged (#77) | 0.3.0–0.23.0 | 0.24.0 |
|
|
152
|
+
| `cp_reconstruct` returned float32 for bf16 / fp16 factors (#78) | 0.1.1–0.23.0 | 0.24.0 |
|
|
146
153
|
|
|
147
154
|
## Related Projects
|
|
148
155
|
|
|
@@ -186,6 +186,7 @@ supported in maintenance mode."
|
|
|
186
186
|
| `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. |
|
|
187
187
|
| `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). |
|
|
188
188
|
| `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). |
|
|
189
|
+
| `TRNTENSOR_AO_TO_MO_PACKED=auto\|resident\|blocked` | Native path, packed `eri` on the Trn2 route: pick the kernel (`resident` = `ao_to_mo_trn2_packed_kernel`; `blocked` = `ao_to_mo_trn2_packed_blocked_kernel`, fp32 only, with an SBUF footprint independent of n_ao). `auto` keeps the resident kernel wherever its SBUF working set fits and takes the blocked one beyond it (#112). |
|
|
189
190
|
| `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). |
|
|
190
191
|
| `TRNTENSOR_DF_COULOMB=auto\|trn2\|aten` | Native path: force the `df_coulomb` executor (`trn2` = `df_coulomb_d_kernel` + `df_coulomb_j_kernel`; `aten` = two GEMVs). `auto` uses the kernels for device-resident packed `B` from `npair·naux` = 1e7 (#60). |
|
|
191
192
|
| `TRNTENSOR_HOST_ROUTE=auto\|host\|device` | Native path, `"auto"` backend, all operands on the host: where the work runs (#56). `auto` (default) uploads it once it reaches the measured thresholds below; `host` keeps it on the host; `device` always uploads. Covers `mp2_energy`, `ao_to_mo_transform`, `einsum` / `multi_einsum` (matmul, bmm, generic and multi-operand plans). Only float32 / bfloat16 / float16 operands are uploaded. `set_backend("pytorch")` and `set_backend("nki")` ignore it. |
|
|
@@ -226,6 +226,41 @@ Kernel alone, device-resident, warm (fp32; padded `nvir` included):
|
|
|
226
226
|
(max error ~1 against float64), while the kernel matched float64 to 2e-7;
|
|
227
227
|
aten was correct at nbasis 736. Its timings there are for reference only.
|
|
228
228
|
|
|
229
|
+
- **Large packed bases (#112).** The packed kernel above keeps C_occ,
|
|
230
|
+
C_vir, one chunk's gather indices and a whole μ column of eri in SBUF. That
|
|
231
|
+
outgrows the budget from n_ao ≈ 1100–1300 (979 KiB per partition at
|
|
232
|
+
n_ao 3508). From there on, fp32 packed integrals run
|
|
233
|
+
`ao_to_mo_trn2_packed_blocked_kernel`. It streams every operand over
|
|
234
|
+
blocks of up to 8 μ/ν tiles, keeps its PSUM accumulators across the
|
|
235
|
+
blocks, and has an SBUF footprint (≈ 71–78 KiB) that does not grow with
|
|
236
|
+
n_ao. Any aux chunk width up to 512 goes in without a padding copy. Where
|
|
237
|
+
both kernels fit, they agree bit for bit.
|
|
238
|
+
`TRNTENSOR_AO_TO_MO_PACKED=auto|resident|blocked` picks the kernel.
|
|
239
|
+
|
|
240
|
+
Measured on Trn2 with one core, fp32, synthetic packed chunks of the
|
|
241
|
+
trnsci#115 ladder's widths. The fp64 reference is computed on the host.
|
|
242
|
+
The host column is the aten route on 2 pinned threads, including the
|
|
243
|
+
unpack. The kernel column is the device time, synchronised, without the
|
|
244
|
+
download:
|
|
245
|
+
|
|
246
|
+
| n_ao, nocc, chunk naux | LNC | cold compile | kernel | host fp32 | max rel. error, kernel / host |
|
|
247
|
+
|---|---|---|---|---|---|
|
|
248
|
+
| 1658, 161, 384 | 2 | 128 s | 56 ms¹ | 6.1 s | 3.2e-7 / 6.6e-7 |
|
|
249
|
+
| 2522, 215, 168 | 2 | 195 s | 73 ms¹ | 7.9 s | 3.7e-7 / 6.0e-7 |
|
|
250
|
+
| 3508, 241, 87 | 2 | 162 s | 85 ms | 8.6 s | 3.9e-7 / 5.4e-7 |
|
|
251
|
+
| 3508, 241, 87 | 1 | 140 s | 94 ms | – | 3.9e-7 |
|
|
252
|
+
| 3508, 241, 48 (last chunk) | 2 | 56 s | 53 ms | 4.8 s | 4.4e-7 / 5.7e-7 |
|
|
253
|
+
|
|
254
|
+
¹ One chunk of up to 512 columns stays one chunk, so at LNC=2 the two
|
|
255
|
+
programs split the occupied index and both gather every eri block.
|
|
256
|
+
Splitting it in two takes 1658 to 35 ms at the same compile time, but
|
|
257
|
+
2522 to 54 ms at a 552 s compile, so the wider tiling is kept.
|
|
258
|
+
|
|
259
|
+
With the NEFF cache the first call at n_ao 3508 takes 26 s. A whole
|
|
260
|
+
alkane60 / cc-pVTZ-shaped pass through `AuxStream.ao_to_mo` (97 chunks,
|
|
261
|
+
8400 aux, 207 GB uploaded from host RAM) took 43 s warm. The trnsci#115
|
|
262
|
+
run did this pass on the host in 673 s, reading the store from NVMe.
|
|
263
|
+
|
|
229
264
|
- **Host inputs on native TorchNeuron (#56).** Under `"auto"`, with `eri`,
|
|
230
265
|
`C_occ` and `C_vir` all on the host, the inputs are uploaded (`eri` in the
|
|
231
266
|
layout given), the device route above runs, and the result is downloaded,
|
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "trntensor"
|
|
7
|
-
version = "0.
|
|
7
|
+
version = "0.25.1"
|
|
8
8
|
description = "Tensor contractions for AWS Trainium via NKI"
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
license = "Apache-2.0"
|
|
@@ -45,7 +45,7 @@ dev = [
|
|
|
45
45
|
"pytest>=7.0,<9", # pytest-benchmark 5.3 breaks on pytest 9 (Function._nodeid)
|
|
46
46
|
"pytest-benchmark>=4.0",
|
|
47
47
|
"pytest-cov>=4.0",
|
|
48
|
-
"ruff
|
|
48
|
+
"ruff==0.17.3",
|
|
49
49
|
"pre-commit>=3.7",
|
|
50
50
|
"scipy>=1.11",
|
|
51
51
|
"mkdocs>=1.5",
|
|
@@ -53,6 +53,7 @@ KERNEL_TAKES = {
|
|
|
53
53
|
"ao_to_mo_transform": (_CHEM,) * 3,
|
|
54
54
|
"ao_to_mo_trn2": (_CHEM,) * 3,
|
|
55
55
|
"ao_to_mo_trn2_packed": (_CHEM, _IDX, _CHEM, _CHEM),
|
|
56
|
+
"ao_to_mo_trn2_packed_blocked": ({F32}, _IDX, {F32}, {F32}),
|
|
56
57
|
"df_half_packed": ({F32}, _IDX, {F32}),
|
|
57
58
|
"df_exchange_syrk": ({F32},) * 2,
|
|
58
59
|
"df_coulomb_d": ({F32},) * 2,
|
|
@@ -80,6 +81,7 @@ _RAW_KERNELS = [
|
|
|
80
81
|
"mp2_energy_row_kernel",
|
|
81
82
|
"ao_to_mo_trn2_kernel",
|
|
82
83
|
"ao_to_mo_trn2_packed_kernel",
|
|
84
|
+
"ao_to_mo_trn2_packed_blocked_kernel",
|
|
83
85
|
"df_half_packed_kernel",
|
|
84
86
|
"df_exchange_syrk_kernel",
|
|
85
87
|
"df_coulomb_d_kernel",
|
|
@@ -274,6 +276,7 @@ def _tt(dt, g):
|
|
|
274
276
|
return trntensor.tt_decompose(_mk((4, 5, 6), dt, g), max_rank=2)
|
|
275
277
|
|
|
276
278
|
|
|
279
|
+
_BLOCKED = {"TRNTENSOR_AO_TO_MO": "trn2", "TRNTENSOR_AO_TO_MO_PACKED": "blocked"}
|
|
277
280
|
CALLS = [
|
|
278
281
|
("einsum-matmul", {}, _einsum("ij,jk->ik", [(6, 5), (5, 4)])),
|
|
279
282
|
("einsum-matmul-T", {}, _einsum("ji,jk->ik", [(5, 6), (5, 4)])),
|
|
@@ -289,6 +292,7 @@ CALLS = [
|
|
|
289
292
|
("mp2-legacy", {"TRNTENSOR_MP2_ENERGY": "legacy"}, _mp2),
|
|
290
293
|
("ao2mo-trn2", {"TRNTENSOR_AO_TO_MO": "trn2"}, _ao2mo(False)),
|
|
291
294
|
("ao2mo-trn2-packed", {"TRNTENSOR_AO_TO_MO": "trn2"}, _ao2mo(True)),
|
|
295
|
+
("ao2mo-trn2-packed-blocked", _BLOCKED, _ao2mo(True)),
|
|
292
296
|
("ao2mo-legacy", {"TRNTENSOR_AO_TO_MO": "legacy"}, _ao2mo(False)),
|
|
293
297
|
("df_exchange", {"TRNTENSOR_DF_EXCHANGE": "trn2"}, _dfk),
|
|
294
298
|
("df_coulomb", {"TRNTENSOR_DF_COULOMB": "trn2"}, _dfj),
|
|
@@ -301,7 +305,7 @@ CALLS = [
|
|
|
301
305
|
def _leaves(x):
|
|
302
306
|
if isinstance(x, torch.Tensor):
|
|
303
307
|
return [x]
|
|
304
|
-
if isinstance(x,
|
|
308
|
+
if isinstance(x, list | tuple):
|
|
305
309
|
return [t for v in x for t in _leaves(v)]
|
|
306
310
|
if isinstance(x, parallel.ShardedTensor):
|
|
307
311
|
return _leaves(list(x.chunks))
|
|
@@ -381,7 +385,15 @@ def test_require_nki_refuses_matmul_dtypes(guarded, monkeypatch, dtype, name):
|
|
|
381
385
|
@pytest.mark.parametrize("dtype", [torch.int32, torch.int64, torch.bool, torch.complex64])
|
|
382
386
|
@pytest.mark.parametrize(
|
|
383
387
|
"name",
|
|
384
|
-
[
|
|
388
|
+
[
|
|
389
|
+
"mp2-rows",
|
|
390
|
+
"mp2-legacy",
|
|
391
|
+
"ao2mo-trn2",
|
|
392
|
+
"ao2mo-trn2-packed",
|
|
393
|
+
"ao2mo-trn2-packed-blocked",
|
|
394
|
+
"df_exchange",
|
|
395
|
+
"df_coulomb",
|
|
396
|
+
],
|
|
385
397
|
)
|
|
386
398
|
def test_require_nki_refuses_chemistry_dtypes(guarded, monkeypatch, dtype, name):
|
|
387
399
|
_require_nki(monkeypatch)
|
|
@@ -397,7 +409,15 @@ def test_require_nki_refuses_chemistry_dtypes(guarded, monkeypatch, dtype, name)
|
|
|
397
409
|
@pytest.mark.parametrize("dtype", [torch.float16, torch.bfloat16])
|
|
398
410
|
@pytest.mark.parametrize(
|
|
399
411
|
"name",
|
|
400
|
-
[
|
|
412
|
+
[
|
|
413
|
+
"mp2-rows",
|
|
414
|
+
"mp2-legacy",
|
|
415
|
+
"ao2mo-trn2",
|
|
416
|
+
"ao2mo-trn2-packed",
|
|
417
|
+
"ao2mo-trn2-packed-blocked",
|
|
418
|
+
"df_exchange",
|
|
419
|
+
"df_coulomb",
|
|
420
|
+
],
|
|
401
421
|
)
|
|
402
422
|
def test_require_nki_chemistry_floats_run_the_kernel(guarded, monkeypatch, dtype, name):
|
|
403
423
|
"""float16 (and, on the fp32-only df_* kernels, bfloat16) chemistry operands
|
|
@@ -552,6 +572,7 @@ _MIXED_CALLS = [
|
|
|
552
572
|
("mp2-rows", {"TRNTENSOR_MP2_ENERGY": "rows"}, _mp2_mixed),
|
|
553
573
|
("ao2mo-trn2", {"TRNTENSOR_AO_TO_MO": "trn2"}, _ao2mo_mixed),
|
|
554
574
|
("ao2mo-trn2-packed", {"TRNTENSOR_AO_TO_MO": "trn2"}, lambda a, b: _ao2mo_mixed(a, b, True)),
|
|
575
|
+
("ao2mo-trn2-packed-blocked", _BLOCKED, lambda a, b: _ao2mo_mixed(a, b, True)),
|
|
555
576
|
("df_exchange", {"TRNTENSOR_DF_EXCHANGE": "trn2"}, _dfk_mixed),
|
|
556
577
|
("df_coulomb", {"TRNTENSOR_DF_COULOMB": "trn2"}, _dfj_mixed),
|
|
557
578
|
("einsum-matmul", {}, _matmul_mixed),
|
|
@@ -34,6 +34,7 @@ EXPECTED_OPS = {
|
|
|
34
34
|
"mp2_energy_row",
|
|
35
35
|
"ao_to_mo_trn2",
|
|
36
36
|
"ao_to_mo_trn2_packed",
|
|
37
|
+
"ao_to_mo_trn2_packed_blocked",
|
|
37
38
|
"df_half_packed",
|
|
38
39
|
"df_exchange_syrk",
|
|
39
40
|
"df_coulomb_d",
|
|
@@ -140,6 +141,7 @@ def native_host(monkeypatch):
|
|
|
140
141
|
"mp2_energy_row_kernel",
|
|
141
142
|
"ao_to_mo_trn2_kernel",
|
|
142
143
|
"ao_to_mo_trn2_packed_kernel",
|
|
144
|
+
"ao_to_mo_trn2_packed_blocked_kernel",
|
|
143
145
|
"df_half_packed_kernel",
|
|
144
146
|
"df_exchange_syrk_kernel",
|
|
145
147
|
"df_coulomb_d_kernel",
|
|
@@ -517,7 +519,7 @@ class TestEagerKernelTable:
|
|
|
517
519
|
if not isinstance(node, ast.FunctionDef):
|
|
518
520
|
continue
|
|
519
521
|
*pre, last = node.body
|
|
520
|
-
if not all(isinstance(n,
|
|
522
|
+
if not all(isinstance(n, ast.Import | ast.ImportFrom) for n in pre):
|
|
521
523
|
continue
|
|
522
524
|
call = getattr(last, "value", None)
|
|
523
525
|
if not (
|
|
@@ -736,6 +738,113 @@ class TestPublicApiOnNativePath:
|
|
|
736
738
|
)
|
|
737
739
|
assert [c[0] for c in native_host] == ["ao_to_mo_trn2_packed"]
|
|
738
740
|
|
|
741
|
+
@pytest.mark.parametrize(
|
|
742
|
+
"nbasis,naux,nocc,nvir",
|
|
743
|
+
[(40, 6, 5, 9), (130, 87, 131, 20), (200, 768, 7, 140), (60, 1031, 3, 50)],
|
|
744
|
+
)
|
|
745
|
+
def test_packed_blocked_ao_to_mo_replays_to_the_full_transform(
|
|
746
|
+
self, native_host, monkeypatch, nbasis, naux, nocc, nvir
|
|
747
|
+
):
|
|
748
|
+
"""The blocked op (#112) gets (npair·NPC, TP) rows with TP dividing naux
|
|
749
|
+
(no padding copy unless naux has no divisor in [64, 512]), NB/NUB index
|
|
750
|
+
blocks of NKB·NUB columns and padded C_occ/C_vir; replaying its gathers
|
|
751
|
+
on CPU rebuilds the square, so the result matches the full transform."""
|
|
752
|
+
from trntensor.nki._kernels import ao_to_mo_blocked_nub, ao_to_mo_blocked_tp
|
|
753
|
+
|
|
754
|
+
def blocked_op(eri_v, img, C_occ, C_vir):
|
|
755
|
+
npc, nvb, tp = img.shape[0], img.shape[1], eri_v.shape[1]
|
|
756
|
+
nbp, nocp = C_occ.shape
|
|
757
|
+
nub = nbp // nvb
|
|
758
|
+
assert img.dtype == torch.uint32 and img.shape == (npc, nvb, 128, nbp // 128 * nub)
|
|
759
|
+
assert nub == ao_to_mo_blocked_nub(tp, nocp)
|
|
760
|
+
assert nbp % 128 == 0 and C_vir.shape[1] % 128 == 0
|
|
761
|
+
if nocp > 128 and npc % 2:
|
|
762
|
+
assert nocp % 2 == 0 # the programs split the occupied index
|
|
763
|
+
idx = img.view(torch.int32).long()
|
|
764
|
+
full = torch.empty(nbp, nbp, npc * tp)
|
|
765
|
+
for c in range(npc):
|
|
766
|
+
for vb in range(nvb):
|
|
767
|
+
for k in range(nbp // 128):
|
|
768
|
+
blk = idx[c, vb, :, k * nub : (k + 1) * nub]
|
|
769
|
+
rows = eri_v[blk.T.flatten()].reshape(128, nub, tp)
|
|
770
|
+
full[
|
|
771
|
+
k * 128 : (k + 1) * 128,
|
|
772
|
+
vb * nub : (vb + 1) * nub,
|
|
773
|
+
c * tp : (c + 1) * tp,
|
|
774
|
+
] = rows
|
|
775
|
+
return torch.einsum("mi,na,mnP->iaP", C_occ, C_vir, full)
|
|
776
|
+
|
|
777
|
+
monkeypatch.setenv("TRNTENSOR_AO_TO_MO_PACKED", "blocked")
|
|
778
|
+
_install_ops(monkeypatch, native_host, ao_to_mo_trn2_packed_blocked=blocked_op)
|
|
779
|
+
g = torch.Generator().manual_seed(nbasis)
|
|
780
|
+
eri = torch.randn(nbasis, nbasis, naux, generator=g)
|
|
781
|
+
eri = 0.5 * (eri + eri.transpose(0, 1))
|
|
782
|
+
C_occ = torch.randn(nbasis, nocc, generator=g)
|
|
783
|
+
C_vir = torch.randn(nbasis, nvir, generator=g)
|
|
784
|
+
out = trntensor.ao_to_mo_transform(trntensor.pack_tril(eri), C_occ, C_vir)
|
|
785
|
+
assert out.shape == (nocc, nvir, naux)
|
|
786
|
+
want = torch.einsum("mi,na,mnP->iaP", C_occ.double(), C_vir.double(), eri.double())
|
|
787
|
+
assert (out.double() - want).abs().max() <= 1e-5 * want.abs().max()
|
|
788
|
+
assert [c[0] for c in native_host] == ["ao_to_mo_trn2_packed_blocked"]
|
|
789
|
+
tp = ao_to_mo_blocked_tp(naux)
|
|
790
|
+
assert tp == 0 or naux % tp == 0
|
|
791
|
+
|
|
792
|
+
def test_packed_variant_choice(self, monkeypatch):
|
|
793
|
+
"""'auto' keeps the resident kernel where it fits and takes the blocked
|
|
794
|
+
one beyond (n_ao 3508 needs 979 KiB resident, #112); fp32 only."""
|
|
795
|
+
f32 = torch.float32
|
|
796
|
+
assert dispatch._ao_to_mo_packed_variant(256, 32, 224, 512, f32) == "resident"
|
|
797
|
+
assert dispatch._ao_to_mo_packed_variant(3508, 241, 3267, 87, f32) == "blocked"
|
|
798
|
+
assert dispatch._ao_to_mo_packed_variant(1658, 161, 1497, 384, f32) == "blocked"
|
|
799
|
+
assert dispatch._ao_to_mo_packed_variant(3508, 241, 3267, 87, torch.bfloat16) is None
|
|
800
|
+
monkeypatch.setenv("TRNTENSOR_AO_TO_MO_PACKED", "blocked")
|
|
801
|
+
assert dispatch._ao_to_mo_packed_variant(256, 32, 224, 512, f32) == "blocked"
|
|
802
|
+
monkeypatch.setenv("TRNTENSOR_AO_TO_MO_PACKED", "resident")
|
|
803
|
+
assert dispatch._ao_to_mo_packed_variant(3508, 241, 3267, 87, f32) == "resident"
|
|
804
|
+
monkeypatch.setenv("TRNTENSOR_AO_TO_MO_PACKED", "nope")
|
|
805
|
+
with pytest.raises(ValueError, match="TRNTENSOR_AO_TO_MO_PACKED"):
|
|
806
|
+
dispatch._ao_to_mo_packed_variant(256, 32, 224, 512, f32)
|
|
807
|
+
|
|
808
|
+
def test_packed_route_takes_the_blocked_kernel_past_the_resident_budget(self, monkeypatch):
|
|
809
|
+
monkeypatch.setattr(dispatch, "_use_nki", lambda: True)
|
|
810
|
+
monkeypatch.setattr(dispatch, "_NEURON_DEVICE", "meta")
|
|
811
|
+
monkeypatch.setattr(dispatch, "_on_native_path", lambda: True)
|
|
812
|
+
monkeypatch.setattr(dispatch, "HAS_NKI", True)
|
|
813
|
+
n = 3508
|
|
814
|
+
big = torch.empty(n * (n + 1) // 2, 87, device="meta")
|
|
815
|
+
assert dispatch._ao_to_mo_route(big, 241, n - 241, n) == "trn2"
|
|
816
|
+
assert dispatch._ao_to_mo_route(big.to(torch.bfloat16), 241, n - 241, n) == "aten"
|
|
817
|
+
|
|
818
|
+
@pytest.mark.parametrize("nocc", [16, 161, 241, 600])
|
|
819
|
+
@pytest.mark.parametrize("naux", [48, 87, 96, 384, 512, 768, 6064])
|
|
820
|
+
def test_blocked_sbuf_does_not_grow_with_nao(self, nocc, naux):
|
|
821
|
+
from trntensor.nki._kernels import ao_to_mo_blocked_sbuf_bytes, ao_to_mo_blocked_tp
|
|
822
|
+
|
|
823
|
+
tp = ao_to_mo_blocked_tp(naux) or ao_to_mo_blocked_tp(-(-naux // 128) * 128)
|
|
824
|
+
sizes = {ao_to_mo_blocked_sbuf_bytes(nocc, tp, nb) for nb in (1024, 2048, 3584, 8192)}
|
|
825
|
+
assert len(sizes) == 1
|
|
826
|
+
assert sizes.pop() <= dispatch._AO_TO_MO_SBUF_BUDGET
|
|
827
|
+
|
|
828
|
+
@pytest.mark.parametrize(
|
|
829
|
+
"nbasis,nbp,naux,tp,nub", [(200, 256, 87, 87, 8), (256, 256, 768, 384, 2)]
|
|
830
|
+
)
|
|
831
|
+
def test_blocked_gather_images_address_the_packed_rows(self, nbasis, nbp, naux, tp, nub):
|
|
832
|
+
img = dispatch._tril_gather_images_blocked(nbasis, nbp, naux, tp, nub)
|
|
833
|
+
idx = img.view(torch.int32).long()
|
|
834
|
+
npc = naux // tp
|
|
835
|
+
rows = dispatch._tril_rows(nbasis)
|
|
836
|
+
assert idx.shape == (npc, nbp // nub, 128, nbp // 128 * nub)
|
|
837
|
+
for c in (0, npc - 1):
|
|
838
|
+
for vb in (0, nbp // nub - 1):
|
|
839
|
+
for k in range(nbp // 128):
|
|
840
|
+
got = idx[c, vb, :, k * nub : (k + 1) * nub].T.flatten().reshape(128, nub)
|
|
841
|
+
mu = torch.arange(k * 128, (k + 1) * 128)[:, None].expand(128, nub)
|
|
842
|
+
nu = torch.arange(vb * nub, (vb + 1) * nub)[None, :].expand(128, nub)
|
|
843
|
+
ok = (mu < nbasis) & (nu < nbasis)
|
|
844
|
+
want = rows[mu.clamp(max=nbasis - 1), nu.clamp(max=nbasis - 1)] * npc + c
|
|
845
|
+
assert torch.equal(got[ok], want[ok])
|
|
846
|
+
assert torch.equal(got[~ok], torch.full_like(got[~ok], c))
|
|
847
|
+
|
|
739
848
|
def test_packed_ao_to_mo_aten_route_unpacks(self, native_host, monkeypatch):
|
|
740
849
|
monkeypatch.setenv("TRNTENSOR_AO_TO_MO", "aten")
|
|
741
850
|
_install_ops(monkeypatch, native_host) # any kernel launch would KeyError
|
|
@@ -1194,7 +1303,7 @@ class TestNativeRoute:
|
|
|
1194
1303
|
torch.testing.assert_close(
|
|
1195
1304
|
dispatch.nki_batched_matmul(A[None], B[None]), torch.bmm(A[None], B[None])
|
|
1196
1305
|
)
|
|
1197
|
-
assert moved == [
|
|
1306
|
+
assert moved == [dispatch._neuron_matmul, dispatch._neuron_bmm]
|
|
1198
1307
|
|
|
1199
1308
|
def test_fused_quantum_kernels_off_under_auto(self, monkeypatch):
|
|
1200
1309
|
monkeypatch.setattr(dispatch, "HAS_NKI", True)
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
"""Row-block matmul for the >= 4 GiB partition-span bug on the neuron device (#123).
|
|
2
|
+
|
|
3
|
+
CPU tests: the thresholds are lowered so that small host tensors stand in for
|
|
4
|
+
the 32 MiB-row operands, and `_on_neuron_device` is patched so the host tensors
|
|
5
|
+
take the device logic. A spy on `torch.matmul` checks that no product the
|
|
6
|
+
blocking issues still has an unsafe operand or result. The on-device
|
|
7
|
+
regression tests at the real sizes are in test_native_device.py
|
|
8
|
+
(`TestSpan4GiB`).
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
import pytest
|
|
12
|
+
import torch
|
|
13
|
+
|
|
14
|
+
import trntensor
|
|
15
|
+
from trntensor import quantum
|
|
16
|
+
from trntensor.nki import dispatch as D
|
|
17
|
+
|
|
18
|
+
SPAN = 1 << 14 # stands in for 2**32
|
|
19
|
+
BLOCK = 1 << 13 # stands in for 2**31
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
@pytest.fixture
|
|
23
|
+
def lowered(monkeypatch):
|
|
24
|
+
monkeypatch.setattr(D, "_NEURON_SPAN_BYTES", SPAN)
|
|
25
|
+
monkeypatch.setattr(D, "_NEURON_BLOCK_BYTES", BLOCK)
|
|
26
|
+
monkeypatch.setattr(D, "_on_neuron_device", lambda t: True)
|
|
27
|
+
calls = []
|
|
28
|
+
real = torch.matmul
|
|
29
|
+
|
|
30
|
+
def spy(a, b, *args, **kwargs):
|
|
31
|
+
calls.append((tuple(a.shape), tuple(b.shape)))
|
|
32
|
+
if a.dim() == 2 and b.dim() == 2:
|
|
33
|
+
assert not D._span_block(a), f"unsafe lhs {tuple(a.shape)}"
|
|
34
|
+
assert not D._span_block(b), f"unsafe rhs {tuple(b.shape)}"
|
|
35
|
+
assert not D._out_block(a.shape[0], b.shape[1], a.element_size()), "unsafe result"
|
|
36
|
+
return real(a, b, *args, **kwargs)
|
|
37
|
+
|
|
38
|
+
monkeypatch.setattr(torch, "matmul", spy)
|
|
39
|
+
return calls
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def _r(*shape, seed=0):
|
|
43
|
+
g = torch.Generator().manual_seed(seed)
|
|
44
|
+
return torch.randn(*shape, generator=g, dtype=torch.float64).float()
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def _ref(a, b):
|
|
48
|
+
return (a.double() @ b.double()).float()
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def test_real_thresholds():
|
|
52
|
+
assert D._NEURON_SPAN_BYTES == 1 << 32
|
|
53
|
+
assert D._out_block(128, 8_389_632, 4) == 64
|
|
54
|
+
assert D._out_block(128, 8_388_480, 4) == 0
|
|
55
|
+
assert D._out_block(64, 8_389_632, 4) == 0
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
@pytest.mark.parametrize("M,K,N", [(12, 128, 40), (12, 300, 33), (128, 40, 6), (130, 128, 40)])
|
|
59
|
+
def test_blocked_matches_plain(lowered, M, K, N):
|
|
60
|
+
a, b = _r(M, K), _r(K, N, seed=1)
|
|
61
|
+
assert D._neuron_mm_unsafe(a, b)
|
|
62
|
+
torch.testing.assert_close(D._neuron_matmul(a, b), _ref(a, b), rtol=1e-5, atol=1e-4)
|
|
63
|
+
assert len(lowered) > 1
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def test_batched_and_bmm(lowered):
|
|
67
|
+
a, b = _r(3, 12, 128), _r(3, 128, 40, seed=1)
|
|
68
|
+
torch.testing.assert_close(D._neuron_bmm(a, b), _ref(a, b), rtol=1e-5, atol=1e-4)
|
|
69
|
+
assert len(lowered) > 1
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def test_short_row_batches_stay_one_call(monkeypatch):
|
|
73
|
+
monkeypatch.setattr(D, "_NEURON_SPAN_BYTES", SPAN)
|
|
74
|
+
monkeypatch.setattr(D, "_on_neuron_device", lambda t: True)
|
|
75
|
+
# (nocc, nbasis, naux) halves with short rows: no blocking even when large.
|
|
76
|
+
a, b = _r(4, 6, 32), _r(4, 32, 8, seed=1)
|
|
77
|
+
assert not D._neuron_mm_unsafe(a, b)
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def test_safe_and_host_products_untouched(monkeypatch):
|
|
81
|
+
a, b = _r(12, 128), _r(128, 40, seed=1)
|
|
82
|
+
assert torch.equal(D._neuron_matmul(a, b), torch.matmul(a, b)) # host
|
|
83
|
+
monkeypatch.setattr(D, "_on_neuron_device", lambda t: True)
|
|
84
|
+
calls = []
|
|
85
|
+
real = torch.matmul
|
|
86
|
+
monkeypatch.setattr(torch, "matmul", lambda x, y: calls.append((x, y)) or real(x, y))
|
|
87
|
+
out = D._neuron_matmul(a, b) # below the real 4 GiB rule
|
|
88
|
+
assert len(calls) == 1 and calls[0][0] is a and calls[0][1] is b
|
|
89
|
+
assert torch.equal(out, real(a, b))
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
@pytest.mark.parametrize("backend", ["auto", "pytorch"])
|
|
93
|
+
def test_einsum_matmul_and_bmm_routes(lowered, backend):
|
|
94
|
+
old = trntensor.get_backend()
|
|
95
|
+
trntensor.set_backend(backend)
|
|
96
|
+
try:
|
|
97
|
+
a, b = _r(12, 128), _r(128, 40, seed=1)
|
|
98
|
+
out = trntensor.einsum("ij,jk->ik", a, b)
|
|
99
|
+
torch.testing.assert_close(out, _ref(a, b), rtol=1e-5, atol=1e-4)
|
|
100
|
+
assert len(lowered) > 1
|
|
101
|
+
lowered.clear()
|
|
102
|
+
a3, b3 = _r(2, 12, 128), _r(2, 128, 40, seed=1)
|
|
103
|
+
out = trntensor.einsum("bij,bjk->bik", a3, b3)
|
|
104
|
+
torch.testing.assert_close(out, _ref(a3, b3), rtol=1e-5, atol=1e-4)
|
|
105
|
+
finally:
|
|
106
|
+
trntensor.set_backend(old)
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def test_ao_to_mo_contiguous_route_blocks(lowered, monkeypatch):
|
|
110
|
+
# nbasis 128, nbasis * naux * 4 B = 2 KiB rows: the #123 layout at this scale.
|
|
111
|
+
nb, naux, nocc, nvir = 128, 4, 5, 7
|
|
112
|
+
eri = _r(nb, nb, naux)
|
|
113
|
+
C_occ, C_vir = _r(nb, nocc, seed=1), _r(nb, nvir, seed=2)
|
|
114
|
+
ref = torch.einsum(
|
|
115
|
+
"na,inP->iaP", C_vir.double(), torch.einsum("mi,mnP->inP", C_occ.double(), eri.double())
|
|
116
|
+
)
|
|
117
|
+
out = quantum._ao_to_mo_contiguous_lhs(eri, C_occ, C_vir)
|
|
118
|
+
torch.testing.assert_close(out.double(), ref, rtol=1e-5, atol=1e-5)
|
|
119
|
+
assert len(lowered) > 2 # the half-transform was blocked over K
|