trntensor 0.19.0__tar.gz → 0.21.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.19.0 → trntensor-0.21.0}/CHANGELOG.md +98 -0
- {trntensor-0.19.0 → trntensor-0.21.0}/PKG-INFO +2 -2
- {trntensor-0.19.0 → trntensor-0.21.0}/README.md +1 -1
- {trntensor-0.19.0 → trntensor-0.21.0}/docs/api/index.md +1 -1
- {trntensor-0.19.0 → trntensor-0.21.0}/docs/api/nki.md +102 -1
- trntensor-0.21.0/docs/api/quantum.md +360 -0
- {trntensor-0.19.0 → trntensor-0.21.0}/pyproject.toml +1 -1
- trntensor-0.21.0/tests/test_host_output.py +139 -0
- {trntensor-0.19.0 → trntensor-0.21.0}/tests/test_native_dispatch.py +445 -0
- {trntensor-0.19.0 → trntensor-0.21.0}/tests/test_nki_compile.py +50 -0
- trntensor-0.21.0/tests/test_nki_native.py +448 -0
- {trntensor-0.19.0 → trntensor-0.21.0}/tests/test_quantum.py +121 -0
- {trntensor-0.19.0 → trntensor-0.21.0}/trntensor/__init__.py +13 -2
- {trntensor-0.19.0 → trntensor-0.21.0}/trntensor/einsum.py +89 -4
- {trntensor-0.19.0 → trntensor-0.21.0}/trntensor/nki/_kernels.py +522 -31
- {trntensor-0.19.0 → trntensor-0.21.0}/trntensor/nki/_native.py +63 -0
- {trntensor-0.19.0 → trntensor-0.21.0}/trntensor/nki/dispatch.py +469 -25
- {trntensor-0.19.0 → trntensor-0.21.0}/trntensor/plan.py +25 -0
- trntensor-0.21.0/trntensor/quantum.py +551 -0
- {trntensor-0.19.0 → trntensor-0.21.0}/trntensor.egg-info/PKG-INFO +2 -2
- {trntensor-0.19.0 → trntensor-0.21.0}/trntensor.egg-info/SOURCES.txt +1 -0
- trntensor-0.19.0/docs/api/quantum.md +0 -152
- trntensor-0.19.0/tests/test_nki_native.py +0 -238
- trntensor-0.19.0/trntensor/quantum.py +0 -236
- {trntensor-0.19.0 → trntensor-0.21.0}/.github/workflows/ci.yml +0 -0
- {trntensor-0.19.0 → trntensor-0.21.0}/.github/workflows/notify-umbrella.yml +0 -0
- {trntensor-0.19.0 → trntensor-0.21.0}/.github/workflows/publish.yml +0 -0
- {trntensor-0.19.0 → trntensor-0.21.0}/.gitignore +0 -0
- {trntensor-0.19.0 → trntensor-0.21.0}/.pre-commit-config.yaml +0 -0
- {trntensor-0.19.0 → trntensor-0.21.0}/CLAUDE.md +0 -0
- {trntensor-0.19.0 → trntensor-0.21.0}/CODE_OF_CONDUCT.md +0 -0
- {trntensor-0.19.0 → trntensor-0.21.0}/CONTRIBUTING.md +0 -0
- {trntensor-0.19.0 → trntensor-0.21.0}/LICENSE +0 -0
- {trntensor-0.19.0 → trntensor-0.21.0}/benchmarks/__init__.py +0 -0
- {trntensor-0.19.0 → trntensor-0.21.0}/benchmarks/bench_einsum.py +0 -0
- {trntensor-0.19.0 → trntensor-0.21.0}/docs/api/decompose.md +0 -0
- {trntensor-0.19.0 → trntensor-0.21.0}/docs/api/einsum.md +0 -0
- {trntensor-0.19.0 → trntensor-0.21.0}/docs/api/plan.md +0 -0
- {trntensor-0.19.0 → trntensor-0.21.0}/docs/architecture.md +0 -0
- {trntensor-0.19.0 → trntensor-0.21.0}/docs/aws_setup.md +0 -0
- {trntensor-0.19.0 → trntensor-0.21.0}/docs/benchmarks.md +0 -0
- {trntensor-0.19.0 → trntensor-0.21.0}/docs/contraction_patterns.md +0 -0
- {trntensor-0.19.0 → trntensor-0.21.0}/docs/design/matmul_dispatch_findings.md +0 -0
- {trntensor-0.19.0 → trntensor-0.21.0}/docs/design/matmul_kernel_profile.md +0 -0
- {trntensor-0.19.0 → trntensor-0.21.0}/docs/developing_kernels.md +0 -0
- {trntensor-0.19.0 → trntensor-0.21.0}/docs/index.md +0 -0
- {trntensor-0.19.0 → trntensor-0.21.0}/docs/installation.md +0 -0
- {trntensor-0.19.0 → trntensor-0.21.0}/docs/quickstart.md +0 -0
- {trntensor-0.19.0 → trntensor-0.21.0}/docs/tutorial_df_mp2.md +0 -0
- {trntensor-0.19.0 → trntensor-0.21.0}/examples/df_mp2_einsum.py +0 -0
- {trntensor-0.19.0 → trntensor-0.21.0}/infra/terraform/README.md +0 -0
- {trntensor-0.19.0 → trntensor-0.21.0}/infra/terraform/main.tf +0 -0
- {trntensor-0.19.0 → trntensor-0.21.0}/mkdocs.yml +0 -0
- {trntensor-0.19.0 → trntensor-0.21.0}/scripts/autotune_dispatch.py +0 -0
- {trntensor-0.19.0 → trntensor-0.21.0}/scripts/bench_native_einsum.py +0 -0
- {trntensor-0.19.0 → trntensor-0.21.0}/scripts/bench_native_kernels.py +0 -0
- {trntensor-0.19.0 → trntensor-0.21.0}/scripts/bench_to_md.py +0 -0
- {trntensor-0.19.0 → trntensor-0.21.0}/scripts/run_benchmarks.sh +0 -0
- {trntensor-0.19.0 → trntensor-0.21.0}/scripts/run_neuron_profile.sh +0 -0
- {trntensor-0.19.0 → trntensor-0.21.0}/scripts/run_neuron_tests.sh +0 -0
- {trntensor-0.19.0 → trntensor-0.21.0}/scripts/run_reduce_parallel_multirank.py +0 -0
- {trntensor-0.19.0 → trntensor-0.21.0}/scripts/run_simulator_tests.sh +0 -0
- {trntensor-0.19.0 → trntensor-0.21.0}/setup.cfg +0 -0
- {trntensor-0.19.0 → trntensor-0.21.0}/tests/conftest.py +0 -0
- {trntensor-0.19.0 → trntensor-0.21.0}/tests/test_decompose.py +0 -0
- {trntensor-0.19.0 → trntensor-0.21.0}/tests/test_einsum.py +0 -0
- {trntensor-0.19.0 → trntensor-0.21.0}/tests/test_nki.py +0 -0
- {trntensor-0.19.0 → trntensor-0.21.0}/tests/test_nki_kernels.py +0 -0
- {trntensor-0.19.0 → trntensor-0.21.0}/tests/test_nki_sim.py +0 -0
- {trntensor-0.19.0 → trntensor-0.21.0}/tests/test_parallel.py +0 -0
- {trntensor-0.19.0 → trntensor-0.21.0}/tests/test_plan.py +0 -0
- {trntensor-0.19.0 → trntensor-0.21.0}/tests/test_sr_runtime.py +0 -0
- {trntensor-0.19.0 → trntensor-0.21.0}/trntensor/decompose.py +0 -0
- {trntensor-0.19.0 → trntensor-0.21.0}/trntensor/nki/__init__.py +0 -0
- {trntensor-0.19.0 → trntensor-0.21.0}/trntensor/nki/collectives.py +0 -0
- {trntensor-0.19.0 → trntensor-0.21.0}/trntensor/parallel.py +0 -0
- {trntensor-0.19.0 → trntensor-0.21.0}/trntensor/py.typed +0 -0
- {trntensor-0.19.0 → trntensor-0.21.0}/trntensor.egg-info/dependency_links.txt +0 -0
- {trntensor-0.19.0 → trntensor-0.21.0}/trntensor.egg-info/requires.txt +0 -0
- {trntensor-0.19.0 → trntensor-0.21.0}/trntensor.egg-info/top_level.txt +0 -0
- {trntensor-0.19.0 → trntensor-0.21.0}/uv.lock +0 -0
|
@@ -7,6 +7,104 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [0.21.0] — 2026-11-01
|
|
11
|
+
|
|
12
|
+
Validated on Trn2 (trn2.3xlarge, Neuron SDK 2.33.0, native TorchNeuron) — see trnsci/trnsci#35.
|
|
13
|
+
|
|
14
|
+
Headline: new fused DF exchange and Coulomb builds, `trntensor.df_exchange` / `df_coulomb`, which read packed integrals in place without building the square: K at n_ao=970 is 2.6× faster (304 → 118 ms) at unchanged error, and DF-RHF SCF at n_ao=970 converges in 4.71 s instead of 9.68 s (#62, #60).
|
|
15
|
+
|
|
16
|
+
### Added
|
|
17
|
+
|
|
18
|
+
- **`trntensor.df_exchange(B, C_occ, alpha=1.0)`: the DF exchange matrix
|
|
19
|
+
straight from packed three-centre integrals (#60).**
|
|
20
|
+
`K = alpha·Σ_{i,P} X X^T` with `X[i,ν,P] = Σ_λ C_occ[λ,i] B[λν,P]`; for a
|
|
21
|
+
closed-shell SCF, `alpha=2`.
|
|
22
|
+
- On the native TorchNeuron path it is two NKI launches per group of aux
|
|
23
|
+
chunks. `df_half_packed_kernel` gathers the packed rows in place
|
|
24
|
+
(batched indirect DMA, as in `ao_to_mo_trn2_packed_kernel`) and forms
|
|
25
|
+
the half-transformed `Xt[P, i, ν]` with the aux index on partitions.
|
|
26
|
+
`df_exchange_syrk_kernel` accumulates K in PSUM over 128-row aux tiles,
|
|
27
|
+
computes only the upper-triangle strips and mirrors them on the Tensor
|
|
28
|
+
Engine. No `(n, n, naux)` square is built, `B` is not copied or padded,
|
|
29
|
+
and both physical cores work at LNC=2 (`program_id` split, no grids).
|
|
30
|
+
- `"auto"` uses it for device-resident packed `B` from
|
|
31
|
+
`nocc·nbasis²·naux` = 1e7; `TRNTENSOR_DF_EXCHANGE=auto|trn2|aten`
|
|
32
|
+
overrides. Elsewhere it is a row gather, one einsum and one GEMM.
|
|
33
|
+
- Trn2, one logical core, PySCF C_kH_2k+2 cc-pVDZ / cc-pvdz-jkfit, fp32,
|
|
34
|
+
against the unpack + einsum + `trnblas.syrk` K of trnsolver's DF-RHF
|
|
35
|
+
example: n_ao = 250 / 490 / 970 went 3.17 → 2.18, 15.5 → 9.33 and
|
|
36
|
+
304 → 118 ms at LNC=2 (1.5x / 1.7x / 2.6x), and 4.71 → 3.46, 25.9 →
|
|
37
|
+
16.9 and 424 → 230 ms at LNC=1. K error against float64 is unchanged
|
|
38
|
+
(8.1e-7 vs 8.3e-7 relative to max |K| at 970).
|
|
39
|
+
- In that example (with `df_coulomb` below, LNC=2, time to convergence)
|
|
40
|
+
the SCF went 352 → 229 ms at n_ao = 250, 756 → 587 ms at 490 and
|
|
41
|
+
9.68 → 4.71 s at 970 (J/K stage 365 → 163 ms per iteration).
|
|
42
|
+
- **`trntensor.df_coulomb(B, D)`: the DF Coulomb matrix from the same packed
|
|
43
|
+
integrals (#60).** Two NKI launches each stream `B` once in contiguous
|
|
44
|
+
128-row tiles with one fused Vector Engine instruction per tile (`d = Bᵀp`,
|
|
45
|
+
then `J = B d`). `TRNTENSOR_DF_COULOMB=auto|trn2|aten`. Against the two
|
|
46
|
+
GEMVs per aux block it replaces: 3.81 → 2.90, 12.6 → 8.83 and 86.7 →
|
|
47
|
+
46.0 ms at n_ao = 250 / 490 / 970 (LNC=2), 126 → 81.7 ms at 970 (LNC=1),
|
|
48
|
+
error 2.8e-7 vs 1.9e-7 relative to max |J|. It also runs on a single
|
|
49
|
+
8.8 GB block, where the aten GEMV needed a 26.7 GB graph. A packed
|
|
50
|
+
`(npair,)` density gives a packed J, so blocked callers pack once.
|
|
51
|
+
- Kernels: `df_half_packed_kernel`, `df_exchange_syrk_kernel`,
|
|
52
|
+
`df_coulomb_d_kernel`, `df_coulomb_j_kernel` (gridless, `num_programs()`
|
|
53
|
+
split, MLIR-verified at lnc=1 and lnc=2). Tiling helpers
|
|
54
|
+
`df_half_tiling`, `df_group_chunks`, `df_exchange_blocks`.
|
|
55
|
+
|
|
56
|
+
## [0.20.0] — 2026-10-31
|
|
57
|
+
|
|
58
|
+
Validated on Trn2 (trn2.3xlarge, Neuron SDK 2.33.0, native TorchNeuron) — see trnsci/trnsci#35.
|
|
59
|
+
|
|
60
|
+
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).
|
|
61
|
+
|
|
62
|
+
### Changed
|
|
63
|
+
|
|
64
|
+
- **Host inputs run on the device when that is faster (#56).** On the
|
|
65
|
+
native TorchNeuron path under `"auto"`, host operands are uploaded, run on
|
|
66
|
+
the device by the device-resident policy and downloaded into cached host
|
|
67
|
+
memory once the work reaches a measured threshold:
|
|
68
|
+
- `mp2_energy`: `nocc²·nvir²·naux` ≥ 1.5e8;
|
|
69
|
+
- `ao_to_mo_transform`: `nocc·nbasis²·naux` ≥ 2e8 for a full `eri`, 3e7 for a packed one;
|
|
70
|
+
- `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.
|
|
71
|
+
- These thresholds scale with `torch.get_num_threads() / 2`, as in trnblas#65. Matmul/bmm keep `TRNTENSOR_MIN_NKI_FLOPS_NATIVE` (5e8, re-measured).
|
|
72
|
+
- 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).
|
|
73
|
+
- `TRNTENSOR_HOST_ROUTE=host|device|auto` overrides the choice. `set_backend("pytorch")` and `set_backend("nki")` keep their routes.
|
|
74
|
+
- If the upload or the device call fails, the call runs on the host.
|
|
75
|
+
- **float64 and complex host matmul/bmm stay on the host** under native
|
|
76
|
+
`"auto"`. Before, they were offloaded from 5e8 multiply-adds, and the
|
|
77
|
+
device computes in at most fp32: a float64 `einsum("ij,jk->ik")` at 1024³
|
|
78
|
+
came back with 1.4e-7 relative error. It is now exact float64.
|
|
79
|
+
|
|
80
|
+
- **Host results at the host-link rate (#56).** On the native TorchNeuron
|
|
81
|
+
path, results of 1 MiB and up that come back to the host (`einsum` /
|
|
82
|
+
`multi_einsum` with host operands on the `aten`-on-device route, kernel
|
|
83
|
+
launches, `from_device`) are copied into memory from the accelerator's
|
|
84
|
+
caching host allocator (`torch.empty(..., pin_memory=True)`), the fix
|
|
85
|
+
trnblas#64 made. A copy into fresh pageable memory runs at about 4 GB/s on
|
|
86
|
+
Trn2 because every page faults; into that memory it runs at 13-14 GB/s.
|
|
87
|
+
Each call still returns a fresh tensor, and results are bit-identical.
|
|
88
|
+
`TRNTENSOR_PINNED_OUTPUT=0` turns it off.
|
|
89
|
+
- **`einsum`'s `alpha` / `beta` / `out` epilogue runs in place** on a fresh
|
|
90
|
+
result (same float ops, same bits). Out of place each step allocated and
|
|
91
|
+
page-faulted another result-sized host tensor.
|
|
92
|
+
- Trn2, host operands, warm medians: `einsum("ij,jk->ik")` 4096³ 32 -> 20 ms,
|
|
93
|
+
8192×1024×8192 80 -> 29 ms; with `alpha=2, beta=1, out=C` 69 -> 29 ms and
|
|
94
|
+
230 -> 63 ms; 16×1024³ bmm 30 -> 17 ms; `from_device` of 256 MB 68 -> 19 ms.
|
|
95
|
+
`ao_to_mo_transform` and `mp2_energy` with host inputs run on the host and
|
|
96
|
+
are unchanged.
|
|
97
|
+
|
|
98
|
+
### Added
|
|
99
|
+
|
|
100
|
+
- **Packed (μ≥ν) three-centre integrals in `ao_to_mo_transform` (#54).**
|
|
101
|
+
- 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.
|
|
102
|
+
- New `trntensor.pack_tril` / `trntensor.unpack_tril`.
|
|
103
|
+
- A host `eri` with device `C_occ` is now uploaded to that device; this used to fail on mixed devices.
|
|
104
|
+
- 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.
|
|
105
|
+
- 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).
|
|
106
|
+
- Pinned host memory does not speed up the upload (measured), so there is no pinned staging path.
|
|
107
|
+
|
|
10
108
|
## [0.19.0] — 2026-10-31
|
|
11
109
|
|
|
12
110
|
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.21.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
|
|
@@ -133,7 +133,7 @@ core, factors = trntensor.tucker_decompose(tensor, ranks=(5, 5, 5))
|
|
|
133
133
|
- [x] CP decomposition (ALS)
|
|
134
134
|
- [x] Tucker decomposition (HOSVD)
|
|
135
135
|
- [x] DF-MP2 einsum example
|
|
136
|
-
- [x] NKI fused contraction kernels (`mp2_energy`, `ao_to_mo_transform`)
|
|
136
|
+
- [x] NKI fused contraction kernels (`mp2_energy`, `ao_to_mo_transform`, `df_exchange`, `df_coulomb`)
|
|
137
137
|
- [x] Device operand residency (`to_device` / `from_device`; `to_xla` / `from_xla` kept)
|
|
138
138
|
- [x] Native TorchNeuron dispatch (Trn2/Trn3, `torch.ops.trntensor.*`) alongside PyTorch/XLA (Trn1)
|
|
139
139
|
- [x] NKI CPU simulator + `nki-simulator` CI gate
|
|
@@ -95,7 +95,7 @@ core, factors = trntensor.tucker_decompose(tensor, ranks=(5, 5, 5))
|
|
|
95
95
|
- [x] CP decomposition (ALS)
|
|
96
96
|
- [x] Tucker decomposition (HOSVD)
|
|
97
97
|
- [x] DF-MP2 einsum example
|
|
98
|
-
- [x] NKI fused contraction kernels (`mp2_energy`, `ao_to_mo_transform`)
|
|
98
|
+
- [x] NKI fused contraction kernels (`mp2_energy`, `ao_to_mo_transform`, `df_exchange`, `df_coulomb`)
|
|
99
99
|
- [x] Device operand residency (`to_device` / `from_device`; `to_xla` / `from_xla` kept)
|
|
100
100
|
- [x] Native TorchNeuron dispatch (Trn2/Trn3, `torch.ops.trntensor.*`) alongside PyTorch/XLA (Trn1)
|
|
101
101
|
- [x] NKI CPU simulator + `nki-simulator` CI gate
|
|
@@ -5,5 +5,5 @@ trntensor's public API is organized into four modules:
|
|
|
5
5
|
- **[einsum](einsum.md)** — `einsum`, `multi_einsum`
|
|
6
6
|
- **[plan](plan.md)** — `plan_contraction`, `ContractionPlan`, `estimate_flops`
|
|
7
7
|
- **[decompose](decompose.md)** — `cp_decompose`, `tucker_decompose`, and reconstructors
|
|
8
|
-
- **[quantum](quantum.md)** — fused chemistry primitives (`mp2_energy`)
|
|
8
|
+
- **[quantum](quantum.md)** — fused chemistry primitives (`mp2_energy`, `ao_to_mo_transform`, `df_exchange`, `df_coulomb`)
|
|
9
9
|
- **[nki](nki.md)** — backend dispatch (`set_backend`, `get_backend`, `HAS_NKI`)
|
|
@@ -119,7 +119,11 @@ supported in maintenance mode."
|
|
|
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
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, `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_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). |
|
|
124
|
+
| `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). |
|
|
125
|
+
| `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. |
|
|
126
|
+
| `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). |
|
|
123
127
|
| `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. |
|
|
124
128
|
| `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. |
|
|
125
129
|
|
|
@@ -148,6 +152,93 @@ NeuronCore and two host threads, with `scripts/bench_native_einsum.py` and
|
|
|
148
152
|
The default of 500 M sits between these. A host with more threads moves the
|
|
149
153
|
crossover up. Re-measure on your host with `scripts/autotune_dispatch.py`.
|
|
150
154
|
|
|
155
|
+
### Host operands: upload or stay (#56)
|
|
156
|
+
|
|
157
|
+
With every operand on the host, native `"auto"` uploads the work, runs it on
|
|
158
|
+
the device by the device-resident policy, and downloads the result into
|
|
159
|
+
memory from the caching host allocator, once the work reaches:
|
|
160
|
+
|
|
161
|
+
| entry point | work | offload from (2 host threads) |
|
|
162
|
+
|---|---|---|
|
|
163
|
+
| `mp2_energy` | `nocc²·nvir²·naux` | 1.5e8 |
|
|
164
|
+
| `ao_to_mo_transform`, full `eri` | `nocc·nbasis²·naux` | 2e8 |
|
|
165
|
+
| `ao_to_mo_transform`, packed `eri` | `nocc·nbasis²·naux` | 3e7 |
|
|
166
|
+
| `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 |
|
|
167
|
+
| `einsum` matmul / bmm | `M·K·N` | `TRNTENSOR_MIN_NKI_FLOPS_NATIVE` (5e8, above) |
|
|
168
|
+
|
|
169
|
+
- The first four scale with `torch.get_num_threads() / 2`, as trnblas#65
|
|
170
|
+
does: the host side is compute-bound and the thresholds were measured with
|
|
171
|
+
2 threads. The matmul threshold keeps its own override and is not scaled.
|
|
172
|
+
- Einsums with little reuse stay on the host at any size: under 0.5
|
|
173
|
+
multiply-adds per byte moved, such as elementwise (0.08) and outer
|
|
174
|
+
(0.25) products. They measured 1.4-2.9x faster uploaded, but only because
|
|
175
|
+
2 host threads stream memory slower than the link. Host bandwidth grows
|
|
176
|
+
with threads and the link's does not, and these patterns fill device
|
|
177
|
+
memory.
|
|
178
|
+
- float64 and complex operands stay on the host, matmul included. The
|
|
179
|
+
device computes in at most fp32.
|
|
180
|
+
- If the upload or the device call fails (for example, a full device), the
|
|
181
|
+
call runs on the host. `TRNTENSOR_REQUIRE_NKI=1` raises instead.
|
|
182
|
+
- The first call of each shape on the device pays its compile. These tables
|
|
183
|
+
are warm.
|
|
184
|
+
|
|
185
|
+
Measured on Trn2 (SDK 2.33, one logical core at LNC=2, fp32, warm medians,
|
|
186
|
+
host in to host out). Host threads were pinned to 2 CPUs on two physical
|
|
187
|
+
cores:
|
|
188
|
+
|
|
189
|
+
"Host" is the host route on two physical cores; the column after it is the
|
|
190
|
+
same on one physical core (both hyperthreads). "Device" is upload + the
|
|
191
|
+
device-resident `auto` route + download. **Bold** is what `auto` now picks.
|
|
192
|
+
|
|
193
|
+
`mp2_energy`, ms:
|
|
194
|
+
|
|
195
|
+
| nocc/nvir/naux | work | host | host, 1 core | device |
|
|
196
|
+
|---|---|---|---|---|
|
|
197
|
+
| 8/56/192 | 3.9e7 | **1.4** | 1.7 | 3.1 |
|
|
198
|
+
| 8/80/256 | 1.1e8 | **3.8** | 4.9 | 4.2 |
|
|
199
|
+
| 8/96/256 | 1.5e8 | 5.1 | 6.8 | **4.1** |
|
|
200
|
+
| 16/112/384 | 1.2e9 | 26.0 | 39.0 | **3.7** |
|
|
201
|
+
| 16/240/768 | 1.1e10 | 173 | 253 | **6.5** |
|
|
202
|
+
| 32/224/1024 | 5.3e10 | 646 | 1128 | **16.4** |
|
|
203
|
+
| 48/336/1152 | 3.0e11 | 3697 | 6457 | **47.4** |
|
|
204
|
+
| 64/448/1536 | 1.3e12 | 15849 | 28157 | **135** |
|
|
205
|
+
|
|
206
|
+
`ao_to_mo_transform`, ms (full `eri` / packed `eri`):
|
|
207
|
+
|
|
208
|
+
| nbasis/naux/nocc | work | host | host, 1 core | device |
|
|
209
|
+
|---|---|---|---|---|
|
|
210
|
+
| 64/192/8 | 6.3e6 | **1.0** / **1.3** | 1.1 / 1.7 | 5.7 / 6.7 |
|
|
211
|
+
| 128/384/16 | 1.0e8 | **4.7** / 6.7 | 7.4 / 11.0 | 7.5 / **5.3** |
|
|
212
|
+
| 128/512/16 | 1.3e8 | **6.2** / – | 10.1 / 21.7 | 8.9 / **4.1** |
|
|
213
|
+
| 160/480/20 | 2.5e8 | 9.4 / – | 16.3 / 32.9 | **7.1** / **5.1** |
|
|
214
|
+
| 256/512/8 | 2.7e8 | 16.0 / 54.2 | 26.5 / 71.0 | **13.6** / **13.0** |
|
|
215
|
+
| 256/1024/32 | 2.2e9 | 66.6 / 138 | 111 / 198 | **25.7** / **15.7** |
|
|
216
|
+
| 384/1152/48 | 8.2e9 | 211 / 397 | 355 / 571 | **62.2** / **37.9** |
|
|
217
|
+
| 512/1536/64 | 2.6e10 | 574 / 979 | 959 / 1473 | **137** / **79.9** |
|
|
218
|
+
|
|
219
|
+
`einsum` with host operands, ms:
|
|
220
|
+
|
|
221
|
+
| contraction | work | MACs per byte | host | host, 1 core | device |
|
|
222
|
+
|---|---|---|---|---|---|
|
|
223
|
+
| `mi,mnP->inP` 128/16/384 | 1.0e8 | 3.6 | **3.2** | 5.5 | 4.4 |
|
|
224
|
+
| `mi,mnP->inP` 256/32/768 | 1.6e9 | 7.1 | 30.5 | 52.4 | **19.7** |
|
|
225
|
+
| `mi,mnP->inP` 512/4/1536 | 1.6e9 | 1.0 | – | 252 | **123** |
|
|
226
|
+
| `iap,jbp->ijab` 16/112/384 | 1.2e9 | 67 | 7.5 | 13.3 | **3.8** |
|
|
227
|
+
| `iaP,PQ->iaQ` 64/448/1536 | 6.8e10 | 187 | 386 | 707 | **31.9** |
|
|
228
|
+
| `abc,cd->abd` 256 | 4.3e9 | 32 | 46.9 | 62.7 | **11.7** |
|
|
229
|
+
| `mi,na,mnP->iaP` 256/32/224/768 (path) | 3.0e9 | 13.5 | – | 90.1 | **19.0** |
|
|
230
|
+
| `ij,ij->ij` 8192² | 6.7e7 | 0.08 | – | **85.8** | 60.3 |
|
|
231
|
+
| `ab,cd->abcd` 160 | 6.6e8 | 0.25 | – | **580** | 199 |
|
|
232
|
+
| `ij,jk->ik` 640³ | 2.6e8 | 53 | **2.3** | 3.7 | 2.6 |
|
|
233
|
+
| `ij,jk->ik` 768³ | 4.5e8 | 66 | **3.4** | 5.6 | 2.7 |
|
|
234
|
+
| `ij,jk->ik` 1024³ | 1.1e9 | 85 | 6.5 | 12.1 | **3.2** |
|
|
235
|
+
|
|
236
|
+
- Results match the host route to 5e-7 relative (fp32 summation order).
|
|
237
|
+
- On one physical core the host is about 1.7x slower, so the device wins
|
|
238
|
+
earlier: from 3e7 for the full `eri`, and below 1.3e8 for matmul. The
|
|
239
|
+
thresholds were set on the faster host, so they are on the host's side.
|
|
240
|
+
- The device floor varied between runs, 1.2 to 2.6 ms for a small matmul.
|
|
241
|
+
|
|
151
242
|
Kernel status on the native path after #51:
|
|
152
243
|
|
|
153
244
|
- **matmul / bmm: retired in favour of aten.** A Trn2 rewrite used both
|
|
@@ -176,6 +267,16 @@ The original AO→MO and MP2 kernels remain the XLA-path (Trn1) kernels. See
|
|
|
176
267
|
- `batched_matmul_kernel(a, b)` — per-batch-slice matmul. Batch dim
|
|
177
268
|
iterated via `nl.affine_range`; each slice reuses the stationary-A
|
|
178
269
|
tile layout.
|
|
270
|
+
- `df_half_packed_kernel(eri_v, img, C_occ, nub)` and
|
|
271
|
+
`df_exchange_syrk_kernel(xt, kin, banks)` — the fused DF exchange build
|
|
272
|
+
behind `trntensor.df_exchange` (#60): packed-row gather and half transform
|
|
273
|
+
into `Xt[P, i, ν]` (aux on partitions), then the PSUM SYRK over aux tiles
|
|
274
|
+
with upper-triangle strips mirrored on the Tensor Engine. Tilings from
|
|
275
|
+
`df_half_tiling`, `df_group_chunks` and `df_exchange_blocks`.
|
|
276
|
+
- `df_coulomb_d_kernel(B, pw)` and `df_coulomb_j_kernel(B, d_b)` — the DF
|
|
277
|
+
Coulomb build behind `trntensor.df_coulomb` (#60): `d = Bᵀ pw` and
|
|
278
|
+
`J = B d`, each streaming packed `B` once in 128-row tiles on the Vector
|
|
279
|
+
Engine.
|
|
179
280
|
|
|
180
281
|
The DF-MP2 pair pattern `einsum("ap,bp->ab")` is served by
|
|
181
282
|
`matmul_kernel` through the planner's `transB=True` route — no
|
|
@@ -0,0 +1,360 @@
|
|
|
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.
|
|
220
|
+
|
|
221
|
+
## `trntensor.df_exchange(B, C_occ, alpha=1.0) -> Tensor`
|
|
222
|
+
|
|
223
|
+
Density-fitted exchange matrix straight from packed three-centre integrals (#60).
|
|
224
|
+
|
|
225
|
+
```
|
|
226
|
+
K[μ, ν] = alpha · Σ_{i, P} X[i, μ, P] X[i, ν, P], X[i, ν, P] = Σ_λ C_occ[λ, i] B[λν, P]
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
For a closed-shell density `D = 2 C_occ C_occᵀ`, the SCF exchange matrix is
|
|
230
|
+
`df_exchange(B, C_occ, alpha=2.0)`.
|
|
231
|
+
|
|
232
|
+
### Arguments
|
|
233
|
+
|
|
234
|
+
- `B` — the packed lower triangle `(npair, naux)` (row `μ(μ+1)/2 + ν`,
|
|
235
|
+
`pack_tril` order), PySCF's `_cderi` `(naux, npair)`, or the full
|
|
236
|
+
`(nbasis, nbasis, naux)` square (aten route).
|
|
237
|
+
- `C_occ: (nbasis, nocc)` — occupied orbital coefficients.
|
|
238
|
+
- `alpha` — scale factor.
|
|
239
|
+
|
|
240
|
+
### Returns
|
|
241
|
+
|
|
242
|
+
The symmetric `(nbasis, nbasis)` matrix, on `B`'s device.
|
|
243
|
+
|
|
244
|
+
### Example — the K build of a DF-RHF iteration
|
|
245
|
+
|
|
246
|
+
```python
|
|
247
|
+
B_d = B_packed.to("neuron") # once: (npair, naux), never unpacked
|
|
248
|
+
K = trntensor.df_exchange(B_d, C_occ_d, alpha=2.0)
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
### Backend behaviour
|
|
252
|
+
|
|
253
|
+
- **Native TorchNeuron (Trn2/Trn3): two NKI launches per group of aux chunks.**
|
|
254
|
+
- `df_half_packed_kernel` gathers the packed rows in place with batched
|
|
255
|
+
indirect DMA (the gather of `ao_to_mo_transform`'s packed kernel, #54)
|
|
256
|
+
and forms the half-transformed `Xt[P, i, ν]` with the aux index on the
|
|
257
|
+
partition axis: each gathered `(128 λ, aux)` block is the stationary
|
|
258
|
+
operand and the resident `C_occ` λ-tile the moving one. The full
|
|
259
|
+
`(n, n, naux)` square never exists.
|
|
260
|
+
- `df_exchange_syrk_kernel` accumulates `K` in PSUM over 128-row aux
|
|
261
|
+
tiles, computes only the upper-triangle 128-row strips, and mirrors them
|
|
262
|
+
on the Tensor Engine on the way out. Two running partial Ks are chained
|
|
263
|
+
through the launches, so there are no extra adds per group.
|
|
264
|
+
- Both launches split across the programs of the logical core: the first
|
|
265
|
+
by aux chunks (or ν groups), the second by occupied orbitals (padded to
|
|
266
|
+
an even count). At LNC=2 both physical cores work.
|
|
267
|
+
- The aux chunk width is a divisor of naux, so `B` is never copied or
|
|
268
|
+
padded. Traced NKI loops unroll, so the dispatcher runs the pair over
|
|
269
|
+
groups of chunks to bound the compile time (about a minute per shape at
|
|
270
|
+
n_ao = 970) and the `Xt` scratch.
|
|
271
|
+
- `"auto"` uses the kernels for device-resident `B` with `nocc ≤ 512`
|
|
272
|
+
once `nocc·nbasis²·naux` reaches 1e7 multiply-adds.
|
|
273
|
+
`TRNTENSOR_DF_EXCHANGE=auto|trn2|aten` overrides the choice.
|
|
274
|
+
- **Otherwise (CPU, small problems)**: aten on `B`'s device. Blocks of rows
|
|
275
|
+
of the square are gathered (1 GiB at a time), half-transformed with one
|
|
276
|
+
einsum into `(ν, i, P)` order, and `K` is one GEMM.
|
|
277
|
+
|
|
278
|
+
### Measured on Trn2
|
|
279
|
+
|
|
280
|
+
PySCF cc-pVDZ / cc-pvdz-jkfit integrals, fp32, warm medians, device-resident
|
|
281
|
+
inputs, one logical core. *Unpack path* is the K build of
|
|
282
|
+
`trnsolver/examples/scf_df_rhf.py` before this change: `unpack_tril` of
|
|
283
|
+
aux blocks (the square kept resident when it fits in 4 GB), the
|
|
284
|
+
half-transform `einsum`, and `trnblas.syrk`. Error is the max-abs error over
|
|
285
|
+
max |K| against float64.
|
|
286
|
+
|
|
287
|
+
| system | n_ao / naux / nocc | LNC | unpack path | `df_exchange` | speedup | error, unpack | error, `df_exchange` |
|
|
288
|
+
|---|---|---|---|---|---|---|---|
|
|
289
|
+
| water | 24 / 116 / 5 | 2 | 0.82 ms | 1.32 ms (kernels; `"auto"` runs aten, 1.16 ms) | — | 7.5e-8 | 1.9e-7 |
|
|
290
|
+
| benzene | 114 / 558 / 21 | 2 | 1.43 ms | 1.35 ms | 1.06x | 5.8e-7 | 2.6e-7 |
|
|
291
|
+
| C10H22 | 250 / 1206 / 41 | 2 | 3.17 ms | 2.18 ms | 1.46x | 3.6e-7 | 4.8e-7 |
|
|
292
|
+
| C20H42 | 490 / 2366 / 81 | 2 | 15.5 ms | 9.33 ms | 1.66x | 6.1e-7 | 6.6e-7 |
|
|
293
|
+
| C40H82 | 970 / 4686 / 161 | 2 | 304 ms | 118 ms | 2.58x | 8.3e-7 | 8.1e-7 |
|
|
294
|
+
| C10H22 | 250 / 1206 / 41 | 1 | 4.71 ms | 3.46 ms | 1.36x | 3.6e-7 | 8.1e-7 |
|
|
295
|
+
| C20H42 | 490 / 2366 / 81 | 1 | 25.9 ms | 16.9 ms | 1.53x | 6.1e-7 | 1.1e-6 |
|
|
296
|
+
| C40H82 | 970 / 4686 / 161 | 1 | 424 ms | 230 ms | 1.84x | 8.3e-7 | 1.5e-6 |
|
|
297
|
+
|
|
298
|
+
- Up to n_ao = 490 the unpack path keeps its square resident (it fits in
|
|
299
|
+
4 GB), so it pays no gather per call; at 970 it unpacks 9 aux blocks per
|
|
300
|
+
call. `df_exchange` never builds the square.
|
|
301
|
+
- `df_exchange` scales 1.59x / 1.81x / 1.95x from LNC=1 to LNC=2 at
|
|
302
|
+
n_ao = 250 / 490 / 970.
|
|
303
|
+
- At n_ao = 970 each launch pair takes 2 aux chunks of 213; there are 11
|
|
304
|
+
pairs. Padding naux to 4736 (chunks of 128, so no partial tiles) measured
|
|
305
|
+
126 ms, not faster: the narrower gathered rows cost more than the partial
|
|
306
|
+
tiles. Eight PSUM banks per SYRK pass instead of six made no difference.
|
|
307
|
+
- Without the `Xt[P, g, i, v]` store layout (ν innermost, 64-byte DMA
|
|
308
|
+
writes) the same call took 488 ms.
|
|
309
|
+
|
|
310
|
+
### Current limitations
|
|
311
|
+
|
|
312
|
+
- `nocc ≤ 512` (one PSUM bank of the stage-1 output).
|
|
313
|
+
- `naux` needs a divisor in [32, 512] (else the aten route runs).
|
|
314
|
+
- The kernels run the Tensor Engine in fp32, which is what bounds them now.
|
|
315
|
+
At n_ao = 970 the half transform runs at about 17 TFLOP/s and the SYRK at
|
|
316
|
+
about 26 TFLOP/s per LNC=2 core (each counts the padded tiles).
|
|
317
|
+
- Host `B` runs the aten route on the host; upload it once and keep it on
|
|
318
|
+
the device.
|
|
319
|
+
|
|
320
|
+
## `trntensor.df_coulomb(B, D) -> Tensor`
|
|
321
|
+
|
|
322
|
+
The density-fitted Coulomb matrix from the same packed integrals (#60).
|
|
323
|
+
|
|
324
|
+
```
|
|
325
|
+
J[μ, ν] = Σ_P B[μν, P] d[P], d[P] = Σ_{λσ} D[λ, σ] B[λσ, P]
|
|
326
|
+
```
|
|
327
|
+
|
|
328
|
+
`D` is a symmetric `(nbasis, nbasis)` density, or its weighted packed form
|
|
329
|
+
`(npair,)` (`w·D[μ, ν]` for `ν ≤ μ`, `w` = 1 on the diagonal and 2 off
|
|
330
|
+
it), in which case the packed `(npair,)` J comes back. A caller that keeps
|
|
331
|
+
the aux index in blocks then packs and unpacks once per call instead of per
|
|
332
|
+
block; on the device those are element gathers.
|
|
333
|
+
|
|
334
|
+
- **Native TorchNeuron**: two launches, `df_coulomb_d_kernel` and
|
|
335
|
+
`df_coulomb_j_kernel`. Each streams `B` once in contiguous 128-row tiles
|
|
336
|
+
(DMAs up to 4096 columns wide), rows split across the programs, with fused
|
|
337
|
+
Vector Engine instructions: a multiply-add per tile for `d`, a
|
|
338
|
+
multiply-reduce per 128 columns for `J`. The partial sums are kept short:
|
|
339
|
+
`d` in groups of 32 tiles, `J` in 128-column chunks. At n_ao = 970 one
|
|
340
|
+
fp32 chain over all 4686 columns gave J 2.4e-6 off float64 and
|
|
341
|
+
512-column chunks 5.7e-7; 128-column chunks give 2.8e-7. The < 256
|
|
342
|
+
leftover rows run as aten GEMVs. `"auto"` uses the kernels for
|
|
343
|
+
device-resident `B` from `npair·naux` = 1e7.
|
|
344
|
+
`TRNTENSOR_DF_COULOMB=auto|trn2|aten` overrides.
|
|
345
|
+
- **Otherwise**: two GEMVs.
|
|
346
|
+
|
|
347
|
+
Measured on Trn2 (fp32, warm; the J of `scf_df_rhf.py`'s `DFJK` on the
|
|
348
|
+
core-guess density, error vs float64 relative to max |J|):
|
|
349
|
+
|
|
350
|
+
| n_ao / naux | LNC | two GEMVs per aux block (before) | `df_coulomb`, one block | speedup |
|
|
351
|
+
|---|---|---|---|---|
|
|
352
|
+
| 250 / 1206 | 2 | 3.81 ms, 2.1e-7 | 2.90 ms, 2.4e-7 | 1.31x |
|
|
353
|
+
| 490 / 2366 | 2 | 12.6 ms, 1.9e-7 | 8.83 ms, 2.7e-7 | 1.43x |
|
|
354
|
+
| 970 / 4686 | 2 | 86.7 ms (9 blocks), 1.9e-7 | 46.0 ms, 2.8e-7 | 1.89x |
|
|
355
|
+
| 970 / 4686 | 1 | 126 ms (9 blocks), 2.4e-7 | 81.7 ms, 2.6e-7 | 1.55x |
|
|
356
|
+
|
|
357
|
+
It also allows a single block: at n_ao = 970 the aten GEMV on one 8.8 GB
|
|
358
|
+
block compiled to a 26.7 GB graph and did not fit in HBM. With the DMA as
|
|
359
|
+
narrow as the 128-column reduce, the kernel was descriptor-bound (76 ms at
|
|
360
|
+
970).
|