trntensor 0.18.0__tar.gz → 0.20.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.18.0 → trntensor-0.20.0}/CHANGELOG.md +88 -0
- {trntensor-0.18.0 → trntensor-0.20.0}/PKG-INFO +1 -1
- {trntensor-0.18.0 → trntensor-0.20.0}/docs/api/nki.md +111 -9
- trntensor-0.20.0/docs/api/quantum.md +219 -0
- {trntensor-0.18.0 → trntensor-0.20.0}/pyproject.toml +1 -1
- trntensor-0.20.0/scripts/bench_native_kernels.py +199 -0
- trntensor-0.20.0/tests/test_host_output.py +139 -0
- {trntensor-0.18.0 → trntensor-0.20.0}/tests/test_native_dispatch.py +506 -5
- {trntensor-0.18.0 → trntensor-0.20.0}/tests/test_nki_compile.py +49 -0
- trntensor-0.20.0/tests/test_nki_native.py +378 -0
- {trntensor-0.18.0 → trntensor-0.20.0}/tests/test_quantum.py +45 -0
- {trntensor-0.18.0 → trntensor-0.20.0}/trntensor/__init__.py +4 -2
- {trntensor-0.18.0 → trntensor-0.20.0}/trntensor/einsum.py +89 -4
- {trntensor-0.18.0 → trntensor-0.20.0}/trntensor/nki/_kernels.py +306 -0
- {trntensor-0.18.0 → trntensor-0.20.0}/trntensor/nki/_native.py +43 -0
- {trntensor-0.18.0 → trntensor-0.20.0}/trntensor/nki/dispatch.py +641 -15
- {trntensor-0.18.0 → trntensor-0.20.0}/trntensor/plan.py +25 -0
- trntensor-0.20.0/trntensor/quantum.py +378 -0
- {trntensor-0.18.0 → trntensor-0.20.0}/trntensor.egg-info/PKG-INFO +1 -1
- {trntensor-0.18.0 → trntensor-0.20.0}/trntensor.egg-info/SOURCES.txt +2 -0
- trntensor-0.18.0/docs/api/quantum.md +0 -109
- trntensor-0.18.0/tests/test_nki_native.py +0 -176
- trntensor-0.18.0/trntensor/quantum.py +0 -201
- {trntensor-0.18.0 → trntensor-0.20.0}/.github/workflows/ci.yml +0 -0
- {trntensor-0.18.0 → trntensor-0.20.0}/.github/workflows/notify-umbrella.yml +0 -0
- {trntensor-0.18.0 → trntensor-0.20.0}/.github/workflows/publish.yml +0 -0
- {trntensor-0.18.0 → trntensor-0.20.0}/.gitignore +0 -0
- {trntensor-0.18.0 → trntensor-0.20.0}/.pre-commit-config.yaml +0 -0
- {trntensor-0.18.0 → trntensor-0.20.0}/CLAUDE.md +0 -0
- {trntensor-0.18.0 → trntensor-0.20.0}/CODE_OF_CONDUCT.md +0 -0
- {trntensor-0.18.0 → trntensor-0.20.0}/CONTRIBUTING.md +0 -0
- {trntensor-0.18.0 → trntensor-0.20.0}/LICENSE +0 -0
- {trntensor-0.18.0 → trntensor-0.20.0}/README.md +0 -0
- {trntensor-0.18.0 → trntensor-0.20.0}/benchmarks/__init__.py +0 -0
- {trntensor-0.18.0 → trntensor-0.20.0}/benchmarks/bench_einsum.py +0 -0
- {trntensor-0.18.0 → trntensor-0.20.0}/docs/api/decompose.md +0 -0
- {trntensor-0.18.0 → trntensor-0.20.0}/docs/api/einsum.md +0 -0
- {trntensor-0.18.0 → trntensor-0.20.0}/docs/api/index.md +0 -0
- {trntensor-0.18.0 → trntensor-0.20.0}/docs/api/plan.md +0 -0
- {trntensor-0.18.0 → trntensor-0.20.0}/docs/architecture.md +0 -0
- {trntensor-0.18.0 → trntensor-0.20.0}/docs/aws_setup.md +0 -0
- {trntensor-0.18.0 → trntensor-0.20.0}/docs/benchmarks.md +0 -0
- {trntensor-0.18.0 → trntensor-0.20.0}/docs/contraction_patterns.md +0 -0
- {trntensor-0.18.0 → trntensor-0.20.0}/docs/design/matmul_dispatch_findings.md +0 -0
- {trntensor-0.18.0 → trntensor-0.20.0}/docs/design/matmul_kernel_profile.md +0 -0
- {trntensor-0.18.0 → trntensor-0.20.0}/docs/developing_kernels.md +0 -0
- {trntensor-0.18.0 → trntensor-0.20.0}/docs/index.md +0 -0
- {trntensor-0.18.0 → trntensor-0.20.0}/docs/installation.md +0 -0
- {trntensor-0.18.0 → trntensor-0.20.0}/docs/quickstart.md +0 -0
- {trntensor-0.18.0 → trntensor-0.20.0}/docs/tutorial_df_mp2.md +0 -0
- {trntensor-0.18.0 → trntensor-0.20.0}/examples/df_mp2_einsum.py +0 -0
- {trntensor-0.18.0 → trntensor-0.20.0}/infra/terraform/README.md +0 -0
- {trntensor-0.18.0 → trntensor-0.20.0}/infra/terraform/main.tf +0 -0
- {trntensor-0.18.0 → trntensor-0.20.0}/mkdocs.yml +0 -0
- {trntensor-0.18.0 → trntensor-0.20.0}/scripts/autotune_dispatch.py +0 -0
- {trntensor-0.18.0 → trntensor-0.20.0}/scripts/bench_native_einsum.py +0 -0
- {trntensor-0.18.0 → trntensor-0.20.0}/scripts/bench_to_md.py +0 -0
- {trntensor-0.18.0 → trntensor-0.20.0}/scripts/run_benchmarks.sh +0 -0
- {trntensor-0.18.0 → trntensor-0.20.0}/scripts/run_neuron_profile.sh +0 -0
- {trntensor-0.18.0 → trntensor-0.20.0}/scripts/run_neuron_tests.sh +0 -0
- {trntensor-0.18.0 → trntensor-0.20.0}/scripts/run_reduce_parallel_multirank.py +0 -0
- {trntensor-0.18.0 → trntensor-0.20.0}/scripts/run_simulator_tests.sh +0 -0
- {trntensor-0.18.0 → trntensor-0.20.0}/setup.cfg +0 -0
- {trntensor-0.18.0 → trntensor-0.20.0}/tests/conftest.py +0 -0
- {trntensor-0.18.0 → trntensor-0.20.0}/tests/test_decompose.py +0 -0
- {trntensor-0.18.0 → trntensor-0.20.0}/tests/test_einsum.py +0 -0
- {trntensor-0.18.0 → trntensor-0.20.0}/tests/test_nki.py +0 -0
- {trntensor-0.18.0 → trntensor-0.20.0}/tests/test_nki_kernels.py +0 -0
- {trntensor-0.18.0 → trntensor-0.20.0}/tests/test_nki_sim.py +0 -0
- {trntensor-0.18.0 → trntensor-0.20.0}/tests/test_parallel.py +0 -0
- {trntensor-0.18.0 → trntensor-0.20.0}/tests/test_plan.py +0 -0
- {trntensor-0.18.0 → trntensor-0.20.0}/tests/test_sr_runtime.py +0 -0
- {trntensor-0.18.0 → trntensor-0.20.0}/trntensor/decompose.py +0 -0
- {trntensor-0.18.0 → trntensor-0.20.0}/trntensor/nki/__init__.py +0 -0
- {trntensor-0.18.0 → trntensor-0.20.0}/trntensor/nki/collectives.py +0 -0
- {trntensor-0.18.0 → trntensor-0.20.0}/trntensor/parallel.py +0 -0
- {trntensor-0.18.0 → trntensor-0.20.0}/trntensor/py.typed +0 -0
- {trntensor-0.18.0 → trntensor-0.20.0}/trntensor.egg-info/dependency_links.txt +0 -0
- {trntensor-0.18.0 → trntensor-0.20.0}/trntensor.egg-info/requires.txt +0 -0
- {trntensor-0.18.0 → trntensor-0.20.0}/trntensor.egg-info/top_level.txt +0 -0
- {trntensor-0.18.0 → trntensor-0.20.0}/uv.lock +0 -0
|
@@ -7,6 +7,94 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [0.20.0] — 2026-10-31
|
|
11
|
+
|
|
12
|
+
Validated on Trn2 (trn2.3xlarge, Neuron SDK 2.33.0, native TorchNeuron) — see trnsci/trnsci#35.
|
|
13
|
+
|
|
14
|
+
Headline: packed ERI input for `ao_to_mo_transform` (DF-MP2 14–18% faster, #55); pinned-memory downloads (1.6–3.7×, #57); large host-input calls offload to the device above measured thresholds (umbrella DF-MP2 at xxl 29 s → 0.4 s, #58).
|
|
15
|
+
|
|
16
|
+
### Changed
|
|
17
|
+
|
|
18
|
+
- **Host inputs run on the device when that is faster (#56).** On the
|
|
19
|
+
native TorchNeuron path under `"auto"`, host operands are uploaded, run on
|
|
20
|
+
the device by the device-resident policy and downloaded into cached host
|
|
21
|
+
memory once the work reaches a measured threshold:
|
|
22
|
+
- `mp2_energy`: `nocc²·nvir²·naux` ≥ 1.5e8;
|
|
23
|
+
- `ao_to_mo_transform`: `nocc·nbasis²·naux` ≥ 2e8 for a full `eri`, 3e7 for a packed one;
|
|
24
|
+
- `einsum` generic and multi-operand plans: ≥ 5e8 multiply-adds and at least 0.5 multiply-adds per byte moved. A path uploads once and keeps its intermediates on the device.
|
|
25
|
+
- These thresholds scale with `torch.get_num_threads() / 2`, as in trnblas#65. Matmul/bmm keep `TRNTENSOR_MIN_NKI_FLOPS_NATIVE` (5e8, re-measured).
|
|
26
|
+
- Trn2, 2 host threads, warm: `mp2_energy` 646 -> 16 ms at 32/224/1024 and 15.8 s -> 135 ms at 64/448/1536. `ao_to_mo_transform` at 512/1536/64 went 574 -> 137 ms full and 979 -> 80 ms packed. The umbrella DF-MP2 profile in host-auto mode at 512/1536/64 went 29.9 s -> 0.37 s (fused pipeline) and 28.9 s -> 0.41 s (example pipeline, with trnblas 0.9 routing).
|
|
27
|
+
- `TRNTENSOR_HOST_ROUTE=host|device|auto` overrides the choice. `set_backend("pytorch")` and `set_backend("nki")` keep their routes.
|
|
28
|
+
- If the upload or the device call fails, the call runs on the host.
|
|
29
|
+
- **float64 and complex host matmul/bmm stay on the host** under native
|
|
30
|
+
`"auto"`. Before, they were offloaded from 5e8 multiply-adds, and the
|
|
31
|
+
device computes in at most fp32: a float64 `einsum("ij,jk->ik")` at 1024³
|
|
32
|
+
came back with 1.4e-7 relative error. It is now exact float64.
|
|
33
|
+
|
|
34
|
+
- **Host results at the host-link rate (#56).** On the native TorchNeuron
|
|
35
|
+
path, results of 1 MiB and up that come back to the host (`einsum` /
|
|
36
|
+
`multi_einsum` with host operands on the `aten`-on-device route, kernel
|
|
37
|
+
launches, `from_device`) are copied into memory from the accelerator's
|
|
38
|
+
caching host allocator (`torch.empty(..., pin_memory=True)`), the fix
|
|
39
|
+
trnblas#64 made. A copy into fresh pageable memory runs at about 4 GB/s on
|
|
40
|
+
Trn2 because every page faults; into that memory it runs at 13-14 GB/s.
|
|
41
|
+
Each call still returns a fresh tensor, and results are bit-identical.
|
|
42
|
+
`TRNTENSOR_PINNED_OUTPUT=0` turns it off.
|
|
43
|
+
- **`einsum`'s `alpha` / `beta` / `out` epilogue runs in place** on a fresh
|
|
44
|
+
result (same float ops, same bits). Out of place each step allocated and
|
|
45
|
+
page-faulted another result-sized host tensor.
|
|
46
|
+
- Trn2, host operands, warm medians: `einsum("ij,jk->ik")` 4096³ 32 -> 20 ms,
|
|
47
|
+
8192×1024×8192 80 -> 29 ms; with `alpha=2, beta=1, out=C` 69 -> 29 ms and
|
|
48
|
+
230 -> 63 ms; 16×1024³ bmm 30 -> 17 ms; `from_device` of 256 MB 68 -> 19 ms.
|
|
49
|
+
`ao_to_mo_transform` and `mp2_energy` with host inputs run on the host and
|
|
50
|
+
are unchanged.
|
|
51
|
+
|
|
52
|
+
### Added
|
|
53
|
+
|
|
54
|
+
- **Packed (μ≥ν) three-centre integrals in `ao_to_mo_transform` (#54).**
|
|
55
|
+
- A 2-D `eri` is read as the packed lower triangle: `(npair, naux)` (row `μ(μ+1)/2 + ν`, the `trnblas.parallel.pack_tril` layout), or PySCF's `_cderi` `(naux, npair)` as is. 3-D calls are unchanged.
|
|
56
|
+
- New `trntensor.pack_tril` / `trntensor.unpack_tril`.
|
|
57
|
+
- A host `eri` with device `C_occ` is now uploaded to that device; this used to fail on mixed devices.
|
|
58
|
+
- New native kernel `ao_to_mo_trn2_packed_kernel`. It gathers its stage-1 blocks from the packed rows with batched indirect DMA, so the full square is never written to device memory. Bit-identical to `ao_to_mo_trn2_kernel`, and as fast at LNC=1 and LNC=2.
|
|
59
|
+
- DF-MP2 end to end on Trn2 (umbrella `df_mp2_profile.py`, one LNC=2 core): 342 → 281 ms at ao/aux/occ 512/1536/64, 157 → 135 ms at 384/1152/48, with identical energies. The upload of `eri` halves (119 → 62 ms).
|
|
60
|
+
- Pinned host memory does not speed up the upload (measured), so there is no pinned staging path.
|
|
61
|
+
|
|
62
|
+
## [0.19.0] — 2026-10-31
|
|
63
|
+
|
|
64
|
+
Validated on Trn2 (trn2.3xlarge, Neuron SDK 2.33.0, native TorchNeuron) — see trnsci/trnsci#35.
|
|
65
|
+
|
|
66
|
+
Headline: new native MP2-energy and AO→MO kernels beat aten by 1.25–1.9× and 1.3–1.6× in the `auto` region; native NKI matmul/bmm kernels deprecated (#52).
|
|
67
|
+
|
|
68
|
+
Resolves #51: on the native TorchNeuron path (Trn2/Trn3), the AO→MO and MP2-energy kernels are rewritten for Trn2 and now beat aten, and the NKI matmul/bmm kernels are retired in favour of aten.
|
|
69
|
+
|
|
70
|
+
### Added
|
|
71
|
+
|
|
72
|
+
- **`mp2_energy_row_kernel`, the native MP2-energy kernel (#51).**
|
|
73
|
+
- One launch per occupied row, using the pair symmetry `e_ij = e_ji`.
|
|
74
|
+
- Per pair, one GEMM fills `T` in SBUF; the `Tᵀ` blocks are Tensor Engine transposes. `T` never reaches device memory.
|
|
75
|
+
- Gridless launch: at LNC=2 each physical core takes half of the `j` window (`num_programs()` / `program_id`), and it also runs at LNC=1.
|
|
76
|
+
- The design follows trnblas `pair_energy_sym` (trnblas#55, #58).
|
|
77
|
+
- Native `"auto"` uses it for device-resident `B` once each row launch carries at least 3 GFLOP. On Trn2 it measured 1.25–1.9× faster than aten: for example 0.69 s against 1.30 s at `nocc=96, nvir=672, naux=2304`.
|
|
78
|
+
- Near a full device the kernel's input staging (about 1.5× the padded `B`) needs more memory than aten's 64 MiB chunks. `"auto"` keeps aten when live allocations plus that staging would leave less than 3 GiB of the core free, and remembers shapes whose launch failed.
|
|
79
|
+
- `TRNTENSOR_MP2_ENERGY=auto|rows|aten|legacy` overrides the choice.
|
|
80
|
+
- **`ao_to_mo_trn2_kernel`, the native AO→MO kernel (#51).**
|
|
81
|
+
- Two plain GEMM stages with the contraction index on the partition axis, so there are no DMA transposes.
|
|
82
|
+
- The auxiliary index is split across both physical cores at LNC=2.
|
|
83
|
+
- Native `"auto"` uses it for device-resident `eri` with `nocc ≤ 128` and stage-1 work of at least 5e8 multiply-adds (about nbasis ≥ 256). There it measured 1.3–1.6× faster than the two aten einsums on Trn2, at nbasis 256–768. Smaller problems stay on aten.
|
|
84
|
+
- `TRNTENSOR_AO_TO_MO=auto|trn2|aten|legacy` overrides the choice.
|
|
85
|
+
- `scripts/bench_native_kernels.py`: a kernel-level A/B harness (aten against the original and the Trn2 kernels), with `--hold-gb` for the memory-pressure regime of aws-neuron-sdk#1447.
|
|
86
|
+
|
|
87
|
+
### Deprecated
|
|
88
|
+
|
|
89
|
+
- **On the native path, the NKI `matmul` / `batched_matmul` kernels and the original `mp2_energy` / `ao_to_mo_transform` kernels (#51).** `set_backend("nki")` still runs the matmul/bmm kernels there, with a one-time `FutureWarning`; a later release will route them to aten.
|
|
90
|
+
- A Trn2 rewrite of matmul used both cores, Tensor Engine transposes and B-tile reuse. It was 1.2–4.3× faster than the original kernel, but still slower than aten at every size (4096³ fp32: 4.10 ms against 3.80 ms; bf16: 1.61 ms against 1.43 ms). It was not kept.
|
|
91
|
+
- Native `"auto"` already used aten for these.
|
|
92
|
+
- The XLA path (Trn1) keeps all four original kernels unchanged and does not warn.
|
|
93
|
+
|
|
94
|
+
### Changed
|
|
95
|
+
|
|
96
|
+
- Under `set_backend("nki")` on the native path, `mp2_energy` and `ao_to_mo_transform` run the Trn2 kernels instead of the original ones.
|
|
97
|
+
|
|
10
98
|
## [0.18.0] — 2026-10-30
|
|
11
99
|
|
|
12
100
|
Native TorchNeuron dispatch (Trn2/Trn3) alongside the PyTorch/XLA path (Trn1, maintenance); validated on Trn2 (trn2.3xlarge, Neuron SDK 2.33.0) — see trnsci/trnsci#35.
|
|
@@ -118,6 +118,10 @@ supported in maintenance mode."
|
|
|
118
118
|
| `TRNSCI_NKI_DISPATCH=auto\|native\|xla` | Suite-wide. Force the native or XLA dispatch path; `auto` (default) detects. Forcing an unavailable path raises. |
|
|
119
119
|
| `TRNSCI_XLA_DEPRECATION=1` | Suite-wide, off by default. Emit the one-time XLA-path and `to_xla` / `from_xla` deprecation warnings. |
|
|
120
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. |
|
|
121
|
+
| `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
|
+
| `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
|
+
| `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. |
|
|
124
|
+
| `TRNTENSOR_PINNED_OUTPUT=0` | Native path: download results with plain `.to("cpu")` instead of into memory from the caching host allocator (`torch.empty(..., pin_memory=True)`, results of 1 MiB and up; #56). |
|
|
121
125
|
| `TRNTENSOR_FORCE_BACKEND=pytorch\|nki\|auto` | Override the backend selection at runtime without calling `set_backend()`. Used by benchmarks to sweep both paths on the same machine. |
|
|
122
126
|
| `TRNTENSOR_USE_SIMULATOR=1` | Route kernel dispatch through `nki.simulate(kernel)(numpy_args)` on CPU instead of XLA → NEFF → hardware. Catches Python-trace-level errors without AWS round-trips. MLIR verifier errors remain hardware-only. |
|
|
123
127
|
|
|
@@ -146,15 +150,113 @@ NeuronCore and two host threads, with `scripts/bench_native_einsum.py` and
|
|
|
146
150
|
The default of 500 M sits between these. A host with more threads moves the
|
|
147
151
|
crossover up. Re-measure on your host with `scripts/autotune_dispatch.py`.
|
|
148
152
|
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
153
|
+
### Host operands: upload or stay (#56)
|
|
154
|
+
|
|
155
|
+
With every operand on the host, native `"auto"` uploads the work, runs it on
|
|
156
|
+
the device by the device-resident policy, and downloads the result into
|
|
157
|
+
memory from the caching host allocator, once the work reaches:
|
|
158
|
+
|
|
159
|
+
| entry point | work | offload from (2 host threads) |
|
|
160
|
+
|---|---|---|
|
|
161
|
+
| `mp2_energy` | `nocc²·nvir²·naux` | 1.5e8 |
|
|
162
|
+
| `ao_to_mo_transform`, full `eri` | `nocc·nbasis²·naux` | 2e8 |
|
|
163
|
+
| `ao_to_mo_transform`, packed `eri` | `nocc·nbasis²·naux` | 3e7 |
|
|
164
|
+
| `einsum`, generic or multi-operand plan | contraction multiply-adds (a path: the sum of its steps) | 5e8, and at least 0.5 multiply-adds per byte moved |
|
|
165
|
+
| `einsum` matmul / bmm | `M·K·N` | `TRNTENSOR_MIN_NKI_FLOPS_NATIVE` (5e8, above) |
|
|
166
|
+
|
|
167
|
+
- The first four scale with `torch.get_num_threads() / 2`, as trnblas#65
|
|
168
|
+
does: the host side is compute-bound and the thresholds were measured with
|
|
169
|
+
2 threads. The matmul threshold keeps its own override and is not scaled.
|
|
170
|
+
- Einsums with little reuse stay on the host at any size: under 0.5
|
|
171
|
+
multiply-adds per byte moved, such as elementwise (0.08) and outer
|
|
172
|
+
(0.25) products. They measured 1.4-2.9x faster uploaded, but only because
|
|
173
|
+
2 host threads stream memory slower than the link. Host bandwidth grows
|
|
174
|
+
with threads and the link's does not, and these patterns fill device
|
|
175
|
+
memory.
|
|
176
|
+
- float64 and complex operands stay on the host, matmul included. The
|
|
177
|
+
device computes in at most fp32.
|
|
178
|
+
- If the upload or the device call fails (for example, a full device), the
|
|
179
|
+
call runs on the host. `TRNTENSOR_REQUIRE_NKI=1` raises instead.
|
|
180
|
+
- The first call of each shape on the device pays its compile. These tables
|
|
181
|
+
are warm.
|
|
182
|
+
|
|
183
|
+
Measured on Trn2 (SDK 2.33, one logical core at LNC=2, fp32, warm medians,
|
|
184
|
+
host in to host out). Host threads were pinned to 2 CPUs on two physical
|
|
185
|
+
cores:
|
|
186
|
+
|
|
187
|
+
"Host" is the host route on two physical cores; the column after it is the
|
|
188
|
+
same on one physical core (both hyperthreads). "Device" is upload + the
|
|
189
|
+
device-resident `auto` route + download. **Bold** is what `auto` now picks.
|
|
190
|
+
|
|
191
|
+
`mp2_energy`, ms:
|
|
192
|
+
|
|
193
|
+
| nocc/nvir/naux | work | host | host, 1 core | device |
|
|
194
|
+
|---|---|---|---|---|
|
|
195
|
+
| 8/56/192 | 3.9e7 | **1.4** | 1.7 | 3.1 |
|
|
196
|
+
| 8/80/256 | 1.1e8 | **3.8** | 4.9 | 4.2 |
|
|
197
|
+
| 8/96/256 | 1.5e8 | 5.1 | 6.8 | **4.1** |
|
|
198
|
+
| 16/112/384 | 1.2e9 | 26.0 | 39.0 | **3.7** |
|
|
199
|
+
| 16/240/768 | 1.1e10 | 173 | 253 | **6.5** |
|
|
200
|
+
| 32/224/1024 | 5.3e10 | 646 | 1128 | **16.4** |
|
|
201
|
+
| 48/336/1152 | 3.0e11 | 3697 | 6457 | **47.4** |
|
|
202
|
+
| 64/448/1536 | 1.3e12 | 15849 | 28157 | **135** |
|
|
203
|
+
|
|
204
|
+
`ao_to_mo_transform`, ms (full `eri` / packed `eri`):
|
|
205
|
+
|
|
206
|
+
| nbasis/naux/nocc | work | host | host, 1 core | device |
|
|
207
|
+
|---|---|---|---|---|
|
|
208
|
+
| 64/192/8 | 6.3e6 | **1.0** / **1.3** | 1.1 / 1.7 | 5.7 / 6.7 |
|
|
209
|
+
| 128/384/16 | 1.0e8 | **4.7** / 6.7 | 7.4 / 11.0 | 7.5 / **5.3** |
|
|
210
|
+
| 128/512/16 | 1.3e8 | **6.2** / – | 10.1 / 21.7 | 8.9 / **4.1** |
|
|
211
|
+
| 160/480/20 | 2.5e8 | 9.4 / – | 16.3 / 32.9 | **7.1** / **5.1** |
|
|
212
|
+
| 256/512/8 | 2.7e8 | 16.0 / 54.2 | 26.5 / 71.0 | **13.6** / **13.0** |
|
|
213
|
+
| 256/1024/32 | 2.2e9 | 66.6 / 138 | 111 / 198 | **25.7** / **15.7** |
|
|
214
|
+
| 384/1152/48 | 8.2e9 | 211 / 397 | 355 / 571 | **62.2** / **37.9** |
|
|
215
|
+
| 512/1536/64 | 2.6e10 | 574 / 979 | 959 / 1473 | **137** / **79.9** |
|
|
216
|
+
|
|
217
|
+
`einsum` with host operands, ms:
|
|
218
|
+
|
|
219
|
+
| contraction | work | MACs per byte | host | host, 1 core | device |
|
|
220
|
+
|---|---|---|---|---|---|
|
|
221
|
+
| `mi,mnP->inP` 128/16/384 | 1.0e8 | 3.6 | **3.2** | 5.5 | 4.4 |
|
|
222
|
+
| `mi,mnP->inP` 256/32/768 | 1.6e9 | 7.1 | 30.5 | 52.4 | **19.7** |
|
|
223
|
+
| `mi,mnP->inP` 512/4/1536 | 1.6e9 | 1.0 | – | 252 | **123** |
|
|
224
|
+
| `iap,jbp->ijab` 16/112/384 | 1.2e9 | 67 | 7.5 | 13.3 | **3.8** |
|
|
225
|
+
| `iaP,PQ->iaQ` 64/448/1536 | 6.8e10 | 187 | 386 | 707 | **31.9** |
|
|
226
|
+
| `abc,cd->abd` 256 | 4.3e9 | 32 | 46.9 | 62.7 | **11.7** |
|
|
227
|
+
| `mi,na,mnP->iaP` 256/32/224/768 (path) | 3.0e9 | 13.5 | – | 90.1 | **19.0** |
|
|
228
|
+
| `ij,ij->ij` 8192² | 6.7e7 | 0.08 | – | **85.8** | 60.3 |
|
|
229
|
+
| `ab,cd->abcd` 160 | 6.6e8 | 0.25 | – | **580** | 199 |
|
|
230
|
+
| `ij,jk->ik` 640³ | 2.6e8 | 53 | **2.3** | 3.7 | 2.6 |
|
|
231
|
+
| `ij,jk->ik` 768³ | 4.5e8 | 66 | **3.4** | 5.6 | 2.7 |
|
|
232
|
+
| `ij,jk->ik` 1024³ | 1.1e9 | 85 | 6.5 | 12.1 | **3.2** |
|
|
233
|
+
|
|
234
|
+
- Results match the host route to 5e-7 relative (fp32 summation order).
|
|
235
|
+
- On one physical core the host is about 1.7x slower, so the device wins
|
|
236
|
+
earlier: from 3e7 for the full `eri`, and below 1.3e8 for matmul. The
|
|
237
|
+
thresholds were set on the faster host, so they are on the host's side.
|
|
238
|
+
- The device floor varied between runs, 1.2 to 2.6 ms for a small matmul.
|
|
239
|
+
|
|
240
|
+
Kernel status on the native path after #51:
|
|
241
|
+
|
|
242
|
+
- **matmul / bmm: retired in favour of aten.** A Trn2 rewrite used both
|
|
243
|
+
physical cores through the `program_id` split, Tensor Engine transposes of
|
|
244
|
+
A and B-tile reuse across four stationaries. It was 1.2–4.3× faster than
|
|
245
|
+
the original kernel and still slower than aten at every size: 4096³ fp32
|
|
246
|
+
took 4.10 ms against 3.80 ms, and 4096³ bf16 1.61 ms against 1.43 ms.
|
|
247
|
+
It was not kept. `set_backend("nki")` still runs the original kernels on
|
|
248
|
+
the native path, with a one-time `FutureWarning`. A later release will
|
|
249
|
+
route them to aten.
|
|
250
|
+
- **AO→MO: kept, rewritten for Trn2** (`ao_to_mo_trn2_kernel`). It is
|
|
251
|
+
1.3–1.6× faster than the two aten einsums at nbasis 256–768, and native
|
|
252
|
+
`"auto"` uses it there for device-resident `eri`. Below nbasis ≈ 256
|
|
253
|
+
`"auto"` keeps aten.
|
|
254
|
+
- **MP2 energy: kept, rewritten for Trn2** (`mp2_energy_row_kernel`). It is
|
|
255
|
+
1.25–1.9× faster than aten once each row launch carries at least 3 GFLOP,
|
|
256
|
+
and native `"auto"` uses it there.
|
|
257
|
+
|
|
258
|
+
The original AO→MO and MP2 kernels remain the XLA-path (Trn1) kernels. See
|
|
259
|
+
[Quantum-chemistry primitives](quantum.md) for the tables.
|
|
158
260
|
|
|
159
261
|
## Kernels (internal)
|
|
160
262
|
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
# Quantum-chemistry primitives
|
|
2
|
+
|
|
3
|
+
Domain-specific fused kernels where the whole computation — contraction, elementwise shaping, reduction — lives in a single NKI program. The architectural core of trntensor (see [Architecture](../architecture.md)).
|
|
4
|
+
|
|
5
|
+
## `trntensor.mp2_energy(B, eps_occ, eps_vir) -> scalar`
|
|
6
|
+
|
|
7
|
+
Density-fitted second-order Møller–Plesset correlation energy.
|
|
8
|
+
|
|
9
|
+
```
|
|
10
|
+
E_MP2 = Σ_{i,j,a,b} T_{i,j,a,b} (2 T_{i,j,a,b} - T_{i,j,b,a}) / Δ_{i,j,a,b}
|
|
11
|
+
|
|
12
|
+
T_{i,j,a,b} = Σ_P B[i, a, P] B[j, b, P]
|
|
13
|
+
Δ_{i,j,a,b} = ε_i + ε_j - ε_a - ε_b
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
### Arguments
|
|
17
|
+
|
|
18
|
+
- `B: (nocc, nvir, naux) tensor` — density-fitted ERI coefficients
|
|
19
|
+
- `eps_occ: (nocc,) tensor` — occupied orbital energies
|
|
20
|
+
- `eps_vir: (nvir,) tensor` — virtual orbital energies
|
|
21
|
+
|
|
22
|
+
### Returns
|
|
23
|
+
|
|
24
|
+
A 0-D tensor containing the correlation energy.
|
|
25
|
+
|
|
26
|
+
### Example
|
|
27
|
+
|
|
28
|
+
```python
|
|
29
|
+
import torch
|
|
30
|
+
import trntensor
|
|
31
|
+
|
|
32
|
+
nocc, nvir, naux = 5, 19, 72
|
|
33
|
+
B = torch.randn(nocc, nvir, naux) * 0.1
|
|
34
|
+
eps_occ = -torch.sort(torch.rand(nocc))[0] - 0.5
|
|
35
|
+
eps_vir = torch.sort(torch.rand(nvir))[0] + 0.1
|
|
36
|
+
|
|
37
|
+
E = trntensor.mp2_energy(B, eps_occ, eps_vir)
|
|
38
|
+
print(f"E_MP2 = {E.item():.6f}")
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
### Backend behaviour
|
|
42
|
+
|
|
43
|
+
- **CPU, and small problems on native TorchNeuron**: aten ops on the
|
|
44
|
+
operands' device. `T[i, j, a, b]` is contracted with `torch.einsum` in
|
|
45
|
+
chunks over `i`, which bounds peak memory to about 64 MiB of `T` at a time.
|
|
46
|
+
The `T`-weighted sum is vectorized.
|
|
47
|
+
- **Native TorchNeuron (Trn2/Trn3): the row kernel.** `mp2_energy_row_kernel`
|
|
48
|
+
is launched once per occupied row `i`. It covers `j = i + d (mod nocc)` for
|
|
49
|
+
`d = 0..nocc/2` and uses the pair symmetry `e_ij = e_ji`. Per pair, one
|
|
50
|
+
GEMM fills `T = B[i] B[j]ᵀ` in SBUF, and the `Tᵀ` blocks come from Tensor
|
|
51
|
+
Engine transposes. `T` never reaches device memory. At LNC=2 each physical
|
|
52
|
+
core takes half of the `j` window. The design follows trnblas
|
|
53
|
+
`pair_energy_sym` (trnblas#55, #58).
|
|
54
|
+
- `"auto"` uses the row kernel when `B` is on the device and each row
|
|
55
|
+
launch carries at least 3 GFLOP, counted on the padded sizes as
|
|
56
|
+
`2·(nocc//2+1)·nvir²·naux`. Below that, the ~nocc custom-op launch
|
|
57
|
+
floors lose to aten.
|
|
58
|
+
- `set_backend("nki")` uses it whenever its SBUF working set fits.
|
|
59
|
+
- `TRNTENSOR_MP2_ENERGY=auto|rows|aten|legacy` overrides the choice.
|
|
60
|
+
`legacy` is the original single-launch kernel, which is deprecated on
|
|
61
|
+
the native path.
|
|
62
|
+
|
|
63
|
+
Measured on Trn2 (fp32, one LNC=2 core, device-resident, warm, #51):
|
|
64
|
+
|
|
65
|
+
| nocc, nvir, naux | aten | row kernel |
|
|
66
|
+
|---|---|---|
|
|
67
|
+
| 16, 128, 512 | 1.5 ms | 5.1 ms |
|
|
68
|
+
| 32, 256, 768 | 12.1 ms | 11.6 ms |
|
|
69
|
+
| 32, 320, 1024 | 24.9 ms | 19.8 ms |
|
|
70
|
+
| 48, 384, 1152 | 75.7 ms | 39.2 ms |
|
|
71
|
+
| 64, 448, 1536 | 240 ms | 120 ms |
|
|
72
|
+
| 96, 672, 2304 | 1.30 s | 0.69 s |
|
|
73
|
+
|
|
74
|
+
Holding 12 or 18 GB of the core's 24 GB on the device did not slow either
|
|
75
|
+
path. The aten path's 64 MiB `T` chunks stay clear of the slowdown in
|
|
76
|
+
aws-neuron-sdk#1447. The row kernel's input staging is about 1.5× the
|
|
77
|
+
padded `B`, so near a full device it needs more memory than aten does.
|
|
78
|
+
`"auto"` therefore keeps aten when this process's live device allocations
|
|
79
|
+
plus that staging would leave less than 3 GiB free. A shape whose kernel
|
|
80
|
+
launch fails anyway falls back to aten and is remembered for the rest of
|
|
81
|
+
the process.
|
|
82
|
+
- **Host `B` on native TorchNeuron (#56).** Under `"auto"`, a host `B` with
|
|
83
|
+
`nocc²·nvir²·naux` of at least 1.5e8 multiply-adds (scaled by
|
|
84
|
+
`torch.get_num_threads() / 2`) is uploaded with the orbital energies, the
|
|
85
|
+
device route above runs, and the energy comes back as a host scalar.
|
|
86
|
+
Smaller problems stay on the host. On Trn2 with 2 host threads this took
|
|
87
|
+
646 ms -> 16 ms at 32/224/1024 and 15.8 s -> 135 ms at 64/448/1536. See
|
|
88
|
+
[host operands](nki.md#host-operands-upload-or-stay-56) for the table and
|
|
89
|
+
`TRNTENSOR_HOST_ROUTE`.
|
|
90
|
+
- **PyTorch/XLA path (Trn1)**: the original single-launch `mp2_energy_kernel`.
|
|
91
|
+
It accumulates `T` in PSUM, builds `Δ` on the Vector Engine, and writes one
|
|
92
|
+
partial per `(i, j)` pair.
|
|
93
|
+
|
|
94
|
+
## `trntensor.ao_to_mo_transform(eri, C_occ, C_vir) -> Tensor`
|
|
95
|
+
|
|
96
|
+
Fused 4-index AO→MO integral transform. The canonical quantum-chemistry primitive.
|
|
97
|
+
|
|
98
|
+
```
|
|
99
|
+
B[i, a, P] = Σ_{μ, ν} C_occ[μ, i] · C_vir[ν, a] · eri[μ, ν, P]
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
### Arguments
|
|
103
|
+
|
|
104
|
+
- `eri` — density-fitted three-centre integrals in the AO basis, one of:
|
|
105
|
+
- `(nbasis, nbasis, naux)`: the full square;
|
|
106
|
+
- `(npair, naux)`, `npair = nbasis(nbasis+1)/2`: the packed lower triangle, row `μ(μ+1)/2 + ν` for `ν ≤ μ` (see [Packed integrals](#packed-integrals));
|
|
107
|
+
- `(naux, npair)`: PySCF's `_cderi`, as is. When `naux == npair` the shape is ambiguous and is read as `(npair, naux)`; pass `cderi.T` then.
|
|
108
|
+
- `C_occ: (nbasis, nocc) tensor` — occupied molecular orbital coefficients
|
|
109
|
+
- `C_vir: (nbasis, nvir) tensor` — virtual molecular orbital coefficients
|
|
110
|
+
|
|
111
|
+
### Returns
|
|
112
|
+
|
|
113
|
+
A `(nocc, nvir, naux)` tensor of transformed DF coefficients B_ia^P.
|
|
114
|
+
|
|
115
|
+
### Example — full DF-MP2 pipeline
|
|
116
|
+
|
|
117
|
+
```python
|
|
118
|
+
import trntensor
|
|
119
|
+
|
|
120
|
+
B = trntensor.ao_to_mo_transform(eri, C_occ, C_vir)
|
|
121
|
+
E = trntensor.mp2_energy(B, eps_occ, eps_vir)
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
### Packed integrals
|
|
125
|
+
|
|
126
|
+
Real three-centre integrals are symmetric in μ↔ν, and drivers store only the
|
|
127
|
+
lower triangle (PySCF `_cderi`, `trnblas.parallel.pack_tril`). Pass it packed
|
|
128
|
+
(#54):
|
|
129
|
+
|
|
130
|
+
```python
|
|
131
|
+
B = trntensor.ao_to_mo_transform(cderi, C_occ_d, C_vir_d) # cderi: host (naux, npair)
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
- `trntensor.pack_tril(eri)` and `trntensor.unpack_tril(eri_packed, nbasis)`
|
|
135
|
+
convert between the layouts (one `index_select` each, exact).
|
|
136
|
+
- **A host `eri` with device `C_occ` is uploaded to `C_occ`'s device**, in the
|
|
137
|
+
layout given. The result stays on the device.
|
|
138
|
+
- On the native path, `"auto"` sends packed device integrals to
|
|
139
|
+
`ao_to_mo_trn2_packed_kernel` under the same conditions as the full kernel.
|
|
140
|
+
Its stage 1 fetches each `(128 μ, NUB ν, TP aux)` block from the packed
|
|
141
|
+
rows with one batched indirect DMA. The full square never exists in device
|
|
142
|
+
memory. Its result is bit-identical to the full kernel's.
|
|
143
|
+
- The aten and legacy routes unpack on the tensor's device first.
|
|
144
|
+
|
|
145
|
+
Why: on Trn2 the host link carries about 13.8 GB/s for the whole chip, so the
|
|
146
|
+
upload of `eri` was DF-MP2's largest stage. Packed, it is half the bytes.
|
|
147
|
+
|
|
148
|
+
Upload of `eri` alone (Trn2, one logical core; host threads pinned to 2 CPUs):
|
|
149
|
+
|
|
150
|
+
| nbasis/naux | full | packed | packed, from pinned host memory |
|
|
151
|
+
|---|---|---|---|
|
|
152
|
+
| 384/1152 | 49.2 ms | 25.1 ms | 24.9 ms |
|
|
153
|
+
| 512/1536 | 115.7 ms | 58.5 ms | 58.4 ms |
|
|
154
|
+
|
|
155
|
+
Pinned host memory and `copy_` into a preallocated device tensor upload no
|
|
156
|
+
faster: H2D already runs at the link rate from ordinary host memory.
|
|
157
|
+
Packing a full host tensor costs more than it saves: 180 ms on 2 host
|
|
158
|
+
threads at 512/1536, against a 57 ms saving. Neither does uploading the
|
|
159
|
+
triangle row by row (281 ms). So a 3-D host `eri` is uploaded as is.
|
|
160
|
+
Packing pays only when the integrals already arrive packed.
|
|
161
|
+
|
|
162
|
+
Kernel alone, device-resident, warm (fp32; padded `nvir` included):
|
|
163
|
+
|
|
164
|
+
| nbasis/naux/nocc | LNC=2 full | LNC=2 packed | LNC=1 full | LNC=1 packed | device `unpack_tril` |
|
|
165
|
+
|---|---|---|---|---|---|
|
|
166
|
+
| 256/768/32 | 1.09 ms | 0.91 ms | 1.86 ms | 1.93 ms | — |
|
|
167
|
+
| 384/1152/48 | 5.38 ms | 5.81 ms | 5.72 ms | 5.95 ms | 3.1 ms |
|
|
168
|
+
| 512/1536/64 | 6.28 ms | 6.27 ms | 12.87 ms | 12.75 ms | 5.5 ms |
|
|
169
|
+
|
|
170
|
+
### Backend behaviour
|
|
171
|
+
|
|
172
|
+
- **CPU**: two explicit aten steps on the operands' device:
|
|
173
|
+
`einsum("mi,mnP->inP")` then `einsum("na,inP->iaP")`.
|
|
174
|
+
- **Native TorchNeuron (Trn2/Trn3): `ao_to_mo_trn2_kernel`.** Both stages are
|
|
175
|
+
plain GEMMs with the contraction index on the partition axis, so there are
|
|
176
|
+
no DMA transposes.
|
|
177
|
+
- Stage 1: `X[i, ν, P] = Σ_μ C_occ[μ, i] eri[μ, ν, P]`.
|
|
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
|
+
- `"auto"` uses the kernel for device-resident `eri` with `nocc ≤ 128` when
|
|
181
|
+
its SBUF working set fits and the stage-1 work `nocc·nbasis²·naux` is at
|
|
182
|
+
least 5e8 multiply-adds (about nbasis ≥ 256). Smaller problems lose to
|
|
183
|
+
the launch floor and the padding copies; otherwise it runs the two aten
|
|
184
|
+
steps.
|
|
185
|
+
`TRNTENSOR_AO_TO_MO=auto|trn2|aten|legacy` overrides the choice.
|
|
186
|
+
|
|
187
|
+
Measured on Trn2 (fp32, one LNC=2 core, device-resident, warm, #51):
|
|
188
|
+
|
|
189
|
+
| nbasis, nocc, naux | two aten einsums | Trn2 kernel (op) | `ao_to_mo_transform`, `"auto"` | original kernel |
|
|
190
|
+
|---|---|---|---|---|
|
|
191
|
+
| 64, 8, 128 | 0.41 ms | — | 0.41 ms (aten) | 6.0 ms (#49) |
|
|
192
|
+
| 128, 16, 256 | 0.49 ms | 0.43 ms | 0.49 ms (aten) | 16.1 ms |
|
|
193
|
+
| 256, 32, 512 | 1.23 ms | 1.11 ms | 0.92 ms | 112 ms (#49) |
|
|
194
|
+
| 512, 32, 1024 | 7.30 ms | 4.56 ms | 4.72 ms | — |
|
|
195
|
+
| 768, 48, 1536 | 18.3 ms | 11.8 ms | 12.0 ms | — |
|
|
196
|
+
|
|
197
|
+
The op column takes tile-multiple inputs (the raw kernel). The `"auto"`
|
|
198
|
+
column includes the padding of `nvir` to 128 and is the shipped route. At
|
|
199
|
+
nbasis 64 and 128 it measured 0.79 and 0.57 ms with the kernel forced,
|
|
200
|
+
which is why `"auto"` keeps aten there.
|
|
201
|
+
|
|
202
|
+
- **Host inputs on native TorchNeuron (#56).** Under `"auto"`, with `eri`,
|
|
203
|
+
`C_occ` and `C_vir` all on the host, the inputs are uploaded (`eri` in the
|
|
204
|
+
layout given), the device route above runs, and the result is downloaded,
|
|
205
|
+
once `nocc·nbasis²·naux` reaches 2e8 for a full `eri` or 3e7 for a packed
|
|
206
|
+
one (both scaled by `torch.get_num_threads() / 2`). A packed host `eri`
|
|
207
|
+
otherwise pays a host unpack, so its threshold is lower. On Trn2 with 2
|
|
208
|
+
host threads at 512/1536/64 this took 574 -> 137 ms full and 979 -> 80 ms
|
|
209
|
+
packed. PySCF's `(naux, npair)` layout went from 5.2 s to 83 ms on one
|
|
210
|
+
physical core. See [host operands](nki.md#host-operands-upload-or-stay-56)
|
|
211
|
+
for the table and `TRNTENSOR_HOST_ROUTE`.
|
|
212
|
+
- **PyTorch/XLA path (Trn1)**: the original kernel. It loops over P, with the
|
|
213
|
+
`(i, ν)` intermediate round-tripping through kernel-scratch HBM. On the
|
|
214
|
+
native path it is deprecated.
|
|
215
|
+
|
|
216
|
+
### Current limitations
|
|
217
|
+
|
|
218
|
+
- Both NKI kernels need `nocc ≤ 128`; larger `nocc` runs the aten steps (native) or raises `NotImplementedError` (XLA).
|
|
219
|
+
- 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.
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
"""Kernel-level A/B on TorchNeuron: trntensor NKI kernels vs aten (#51).
|
|
2
|
+
|
|
3
|
+
Times, on device-resident ``neuron`` tensors created on the host and copied
|
|
4
|
+
over (aws-neuron-sdk#1439), the warm median latency of:
|
|
5
|
+
|
|
6
|
+
aten the aten formulation trntensor's native ``auto`` uses
|
|
7
|
+
nki_trn1 the original kernel (the XLA-path kernel, as on main)
|
|
8
|
+
tt_auto the public trntensor entry point under set_backend("auto")
|
|
9
|
+
nki_trn2 the native Trn2 kernel (program_id split, no DMA transposes);
|
|
10
|
+
AO→MO and MP2 only. The Trn2 matmul candidate measured in #51
|
|
11
|
+
lost to aten at every size and was not kept.
|
|
12
|
+
|
|
13
|
+
Families: matmul, ao2mo, mp2. ``--hold-gb G`` keeps G GB allocated on the
|
|
14
|
+
device during the run (the memory-pressure regime of aws-neuron-sdk#1447).
|
|
15
|
+
|
|
16
|
+
python scripts/bench_native_kernels.py --family mp2 --hold-gb 12
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
from __future__ import annotations
|
|
20
|
+
|
|
21
|
+
import argparse
|
|
22
|
+
import json
|
|
23
|
+
import os
|
|
24
|
+
import statistics
|
|
25
|
+
import time
|
|
26
|
+
|
|
27
|
+
os.environ.setdefault("NKI_ENABLE_TRACE_CACHE", "0")
|
|
28
|
+
|
|
29
|
+
import torch # noqa: E402
|
|
30
|
+
|
|
31
|
+
from trntensor import quantum # noqa: E402
|
|
32
|
+
from trntensor.nki import dispatch # noqa: E402
|
|
33
|
+
|
|
34
|
+
DEV = "neuron"
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def _sync():
|
|
38
|
+
torch.neuron.synchronize()
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def _time(fn, warm, reps, budget_s=20.0):
|
|
42
|
+
for _ in range(warm):
|
|
43
|
+
fn()
|
|
44
|
+
_sync()
|
|
45
|
+
t0 = time.perf_counter()
|
|
46
|
+
fn()
|
|
47
|
+
_sync()
|
|
48
|
+
one = time.perf_counter() - t0
|
|
49
|
+
if one > 0.05:
|
|
50
|
+
reps = max(3, min(reps, int(budget_s / one)))
|
|
51
|
+
ts = []
|
|
52
|
+
for _ in range(reps):
|
|
53
|
+
t0 = time.perf_counter()
|
|
54
|
+
fn()
|
|
55
|
+
_sync()
|
|
56
|
+
ts.append(time.perf_counter() - t0)
|
|
57
|
+
return statistics.median(ts) * 1e3
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def _relerr(x, ref):
|
|
61
|
+
x = x.detach().cpu().double()
|
|
62
|
+
ref = ref.double()
|
|
63
|
+
return float((x - ref).norm() / ref.norm().clamp_min(1e-300))
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def _ops():
|
|
67
|
+
return dispatch._get_native_ops()
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def cases_matmul(dtype, sizes):
|
|
71
|
+
sizes = sizes or [(n, n, n) for n in (512, 1024, 2048, 4096)] + [(512, 2048, 512)]
|
|
72
|
+
for M, K, N in sizes:
|
|
73
|
+
|
|
74
|
+
def make(M=M, K=K, N=N):
|
|
75
|
+
g = torch.Generator().manual_seed(0)
|
|
76
|
+
a = torch.randn(M, K, generator=g).to(dtype)
|
|
77
|
+
b = torch.randn(K, N, generator=g).to(dtype)
|
|
78
|
+
return (a, b), (a.double() @ b.double())
|
|
79
|
+
|
|
80
|
+
yield (
|
|
81
|
+
f"matmul {M}x{K}x{N} {str(dtype)[6:]}",
|
|
82
|
+
make,
|
|
83
|
+
{
|
|
84
|
+
"aten": lambda a, b: torch.matmul(a, b),
|
|
85
|
+
"nki_trn1": lambda a, b: _ops()["matmul"](a, b),
|
|
86
|
+
},
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def cases_ao2mo(dtype, sizes):
|
|
91
|
+
# (nbasis, nocc, naux), nvir = nbasis - nocc; nbasis/nvir/naux tile-padded below.
|
|
92
|
+
sizes = sizes or [(128, 16, 256), (256, 32, 512), (512, 32, 1024), (768, 48, 1536)]
|
|
93
|
+
for nb, no, nx in sizes:
|
|
94
|
+
nv = nb - no
|
|
95
|
+
|
|
96
|
+
def make(nb=nb, no=no, nv=nv, nx=nx):
|
|
97
|
+
g = torch.Generator().manual_seed(0)
|
|
98
|
+
co = torch.randn(nb, no, generator=g).to(dtype)
|
|
99
|
+
cv = torch.randn(nb, nv, generator=g).to(dtype)
|
|
100
|
+
eri = torch.randn(nb, nb, nx, generator=g).to(dtype)
|
|
101
|
+
ref = torch.einsum(
|
|
102
|
+
"na,inP->iaP", cv.double(), torch.einsum("mi,mnP->inP", co.double(), eri.double())
|
|
103
|
+
)
|
|
104
|
+
return (eri, co, cv), ref
|
|
105
|
+
|
|
106
|
+
modes = {
|
|
107
|
+
"aten": lambda eri, co, cv: quantum._cpu_ao_to_mo_transform(eri, co, cv),
|
|
108
|
+
"nki_trn2": lambda eri, co, cv: dispatch._nki_ao_to_mo_trn2(eri, co, cv),
|
|
109
|
+
"tt_auto": lambda eri, co, cv: quantum.ao_to_mo_transform(eri, co, cv),
|
|
110
|
+
}
|
|
111
|
+
if nb <= 128: # the original kernel compiles for many minutes beyond this
|
|
112
|
+
modes["nki_trn1"] = lambda eri, co, cv: dispatch._nki_ao_to_mo_transform(eri, co, cv)
|
|
113
|
+
yield (f"ao2mo nb={nb} no={no} nv={nv} nx={nx}", make, modes)
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def cases_mp2(dtype, sizes):
|
|
117
|
+
sizes = sizes or [
|
|
118
|
+
(16, 128, 512),
|
|
119
|
+
(32, 128, 512),
|
|
120
|
+
(32, 256, 768),
|
|
121
|
+
(48, 384, 1152),
|
|
122
|
+
(64, 448, 1536),
|
|
123
|
+
(96, 672, 2304),
|
|
124
|
+
]
|
|
125
|
+
for no, nv, nx in sizes:
|
|
126
|
+
|
|
127
|
+
def make(no=no, nv=nv, nx=nx):
|
|
128
|
+
g = torch.Generator().manual_seed(0)
|
|
129
|
+
B = (torch.randn(no, nv, nx, generator=g) * 0.05).to(dtype)
|
|
130
|
+
eo = -torch.rand(no, generator=g) - 1.0
|
|
131
|
+
ev = torch.rand(nv, generator=g) + 0.5
|
|
132
|
+
ref = quantum._cpu_mp2_energy(B.double(), eo.double(), ev.double())
|
|
133
|
+
return (B, eo, ev), ref
|
|
134
|
+
|
|
135
|
+
modes = {
|
|
136
|
+
"aten": lambda B, eo, ev: quantum._cpu_mp2_energy(B, eo, ev),
|
|
137
|
+
"nki_trn2": lambda B, eo, ev: dispatch._nki_mp2_energy_rows(B, eo, ev),
|
|
138
|
+
"tt_auto": lambda B, eo, ev: quantum.mp2_energy(B, eo, ev),
|
|
139
|
+
}
|
|
140
|
+
if no <= 32:
|
|
141
|
+
modes["nki_trn1"] = lambda B, eo, ev: dispatch._nki_mp2_energy(B, eo, ev)
|
|
142
|
+
yield (f"mp2 no={no} nv={nv} nx={nx}", make, modes)
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
FAMILIES = {"matmul": cases_matmul, "ao2mo": cases_ao2mo, "mp2": cases_mp2}
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
def main():
|
|
149
|
+
ap = argparse.ArgumentParser()
|
|
150
|
+
ap.add_argument("--family", required=True, choices=list(FAMILIES))
|
|
151
|
+
ap.add_argument("--dtype", default="float32")
|
|
152
|
+
ap.add_argument("--modes", default="aten,nki_trn1,nki_trn2")
|
|
153
|
+
ap.add_argument("--sizes", default="", help="semicolon list of comma tuples")
|
|
154
|
+
ap.add_argument("--hold-gb", type=float, default=0.0)
|
|
155
|
+
ap.add_argument("--warm", type=int, default=2)
|
|
156
|
+
ap.add_argument("--reps", type=int, default=10)
|
|
157
|
+
ap.add_argument("--json", default="")
|
|
158
|
+
args = ap.parse_args()
|
|
159
|
+
dtype = getattr(torch, args.dtype)
|
|
160
|
+
sizes = [tuple(int(x) for x in s.split(",")) for s in args.sizes.split(";") if s]
|
|
161
|
+
want = args.modes.split(",")
|
|
162
|
+
|
|
163
|
+
held = []
|
|
164
|
+
if args.hold_gb > 0:
|
|
165
|
+
# Hold the working set in 1 GiB host-made blocks (device randn is
|
|
166
|
+
# shape-sensitive, #1439; zeros are enough to occupy the memory).
|
|
167
|
+
for _ in range(int(args.hold_gb)):
|
|
168
|
+
held.append(torch.ones(256 * 1024 * 1024, dtype=torch.float32).to(DEV))
|
|
169
|
+
_sync()
|
|
170
|
+
print(f"# family={args.family} dtype={args.dtype} hold_gb={args.hold_gb}", flush=True)
|
|
171
|
+
for label, make, modes in FAMILIES[args.family](dtype, sizes):
|
|
172
|
+
host, ref = make()
|
|
173
|
+
dev = tuple(t.to(DEV) for t in host)
|
|
174
|
+
_sync()
|
|
175
|
+
row = {"case": label, "hold_gb": args.hold_gb}
|
|
176
|
+
for m in want:
|
|
177
|
+
if m not in modes:
|
|
178
|
+
continue
|
|
179
|
+
fn = lambda f=modes[m], o=dev: f(*o) # noqa: E731
|
|
180
|
+
try:
|
|
181
|
+
t0 = time.perf_counter()
|
|
182
|
+
out = fn()
|
|
183
|
+
_sync()
|
|
184
|
+
row[f"{m}_first_s"] = round(time.perf_counter() - t0, 2)
|
|
185
|
+
row[f"{m}_relerr"] = _relerr(out, ref)
|
|
186
|
+
del out
|
|
187
|
+
row[m] = _time(fn, args.warm, args.reps)
|
|
188
|
+
except Exception as exc: # noqa: BLE001
|
|
189
|
+
row[m] = None
|
|
190
|
+
row[f"{m}_err"] = repr(exc)[:400]
|
|
191
|
+
del dev
|
|
192
|
+
print(json.dumps(row), flush=True)
|
|
193
|
+
if args.json:
|
|
194
|
+
with open(args.json, "a") as f:
|
|
195
|
+
f.write(json.dumps(row) + "\n")
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
if __name__ == "__main__":
|
|
199
|
+
main()
|