trntensor 0.20.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.20.0 → trntensor-0.21.0}/CHANGELOG.md +46 -0
- {trntensor-0.20.0 → trntensor-0.21.0}/PKG-INFO +2 -2
- {trntensor-0.20.0 → trntensor-0.21.0}/README.md +1 -1
- {trntensor-0.20.0 → trntensor-0.21.0}/docs/api/index.md +1 -1
- {trntensor-0.20.0 → trntensor-0.21.0}/docs/api/nki.md +12 -0
- {trntensor-0.20.0 → trntensor-0.21.0}/docs/api/quantum.md +141 -0
- {trntensor-0.20.0 → trntensor-0.21.0}/pyproject.toml +1 -1
- {trntensor-0.20.0 → trntensor-0.21.0}/tests/test_native_dispatch.py +158 -0
- {trntensor-0.20.0 → trntensor-0.21.0}/tests/test_nki_compile.py +29 -0
- {trntensor-0.20.0 → trntensor-0.21.0}/tests/test_nki_native.py +70 -0
- {trntensor-0.20.0 → trntensor-0.21.0}/tests/test_quantum.py +76 -0
- {trntensor-0.20.0 → trntensor-0.21.0}/trntensor/__init__.py +11 -2
- {trntensor-0.20.0 → trntensor-0.21.0}/trntensor/nki/_kernels.py +409 -0
- {trntensor-0.20.0 → trntensor-0.21.0}/trntensor/nki/_native.py +49 -0
- {trntensor-0.20.0 → trntensor-0.21.0}/trntensor/nki/dispatch.py +180 -0
- {trntensor-0.20.0 → trntensor-0.21.0}/trntensor/quantum.py +173 -0
- {trntensor-0.20.0 → trntensor-0.21.0}/trntensor.egg-info/PKG-INFO +2 -2
- {trntensor-0.20.0 → trntensor-0.21.0}/.github/workflows/ci.yml +0 -0
- {trntensor-0.20.0 → trntensor-0.21.0}/.github/workflows/notify-umbrella.yml +0 -0
- {trntensor-0.20.0 → trntensor-0.21.0}/.github/workflows/publish.yml +0 -0
- {trntensor-0.20.0 → trntensor-0.21.0}/.gitignore +0 -0
- {trntensor-0.20.0 → trntensor-0.21.0}/.pre-commit-config.yaml +0 -0
- {trntensor-0.20.0 → trntensor-0.21.0}/CLAUDE.md +0 -0
- {trntensor-0.20.0 → trntensor-0.21.0}/CODE_OF_CONDUCT.md +0 -0
- {trntensor-0.20.0 → trntensor-0.21.0}/CONTRIBUTING.md +0 -0
- {trntensor-0.20.0 → trntensor-0.21.0}/LICENSE +0 -0
- {trntensor-0.20.0 → trntensor-0.21.0}/benchmarks/__init__.py +0 -0
- {trntensor-0.20.0 → trntensor-0.21.0}/benchmarks/bench_einsum.py +0 -0
- {trntensor-0.20.0 → trntensor-0.21.0}/docs/api/decompose.md +0 -0
- {trntensor-0.20.0 → trntensor-0.21.0}/docs/api/einsum.md +0 -0
- {trntensor-0.20.0 → trntensor-0.21.0}/docs/api/plan.md +0 -0
- {trntensor-0.20.0 → trntensor-0.21.0}/docs/architecture.md +0 -0
- {trntensor-0.20.0 → trntensor-0.21.0}/docs/aws_setup.md +0 -0
- {trntensor-0.20.0 → trntensor-0.21.0}/docs/benchmarks.md +0 -0
- {trntensor-0.20.0 → trntensor-0.21.0}/docs/contraction_patterns.md +0 -0
- {trntensor-0.20.0 → trntensor-0.21.0}/docs/design/matmul_dispatch_findings.md +0 -0
- {trntensor-0.20.0 → trntensor-0.21.0}/docs/design/matmul_kernel_profile.md +0 -0
- {trntensor-0.20.0 → trntensor-0.21.0}/docs/developing_kernels.md +0 -0
- {trntensor-0.20.0 → trntensor-0.21.0}/docs/index.md +0 -0
- {trntensor-0.20.0 → trntensor-0.21.0}/docs/installation.md +0 -0
- {trntensor-0.20.0 → trntensor-0.21.0}/docs/quickstart.md +0 -0
- {trntensor-0.20.0 → trntensor-0.21.0}/docs/tutorial_df_mp2.md +0 -0
- {trntensor-0.20.0 → trntensor-0.21.0}/examples/df_mp2_einsum.py +0 -0
- {trntensor-0.20.0 → trntensor-0.21.0}/infra/terraform/README.md +0 -0
- {trntensor-0.20.0 → trntensor-0.21.0}/infra/terraform/main.tf +0 -0
- {trntensor-0.20.0 → trntensor-0.21.0}/mkdocs.yml +0 -0
- {trntensor-0.20.0 → trntensor-0.21.0}/scripts/autotune_dispatch.py +0 -0
- {trntensor-0.20.0 → trntensor-0.21.0}/scripts/bench_native_einsum.py +0 -0
- {trntensor-0.20.0 → trntensor-0.21.0}/scripts/bench_native_kernels.py +0 -0
- {trntensor-0.20.0 → trntensor-0.21.0}/scripts/bench_to_md.py +0 -0
- {trntensor-0.20.0 → trntensor-0.21.0}/scripts/run_benchmarks.sh +0 -0
- {trntensor-0.20.0 → trntensor-0.21.0}/scripts/run_neuron_profile.sh +0 -0
- {trntensor-0.20.0 → trntensor-0.21.0}/scripts/run_neuron_tests.sh +0 -0
- {trntensor-0.20.0 → trntensor-0.21.0}/scripts/run_reduce_parallel_multirank.py +0 -0
- {trntensor-0.20.0 → trntensor-0.21.0}/scripts/run_simulator_tests.sh +0 -0
- {trntensor-0.20.0 → trntensor-0.21.0}/setup.cfg +0 -0
- {trntensor-0.20.0 → trntensor-0.21.0}/tests/conftest.py +0 -0
- {trntensor-0.20.0 → trntensor-0.21.0}/tests/test_decompose.py +0 -0
- {trntensor-0.20.0 → trntensor-0.21.0}/tests/test_einsum.py +0 -0
- {trntensor-0.20.0 → trntensor-0.21.0}/tests/test_host_output.py +0 -0
- {trntensor-0.20.0 → trntensor-0.21.0}/tests/test_nki.py +0 -0
- {trntensor-0.20.0 → trntensor-0.21.0}/tests/test_nki_kernels.py +0 -0
- {trntensor-0.20.0 → trntensor-0.21.0}/tests/test_nki_sim.py +0 -0
- {trntensor-0.20.0 → trntensor-0.21.0}/tests/test_parallel.py +0 -0
- {trntensor-0.20.0 → trntensor-0.21.0}/tests/test_plan.py +0 -0
- {trntensor-0.20.0 → trntensor-0.21.0}/tests/test_sr_runtime.py +0 -0
- {trntensor-0.20.0 → trntensor-0.21.0}/trntensor/decompose.py +0 -0
- {trntensor-0.20.0 → trntensor-0.21.0}/trntensor/einsum.py +0 -0
- {trntensor-0.20.0 → trntensor-0.21.0}/trntensor/nki/__init__.py +0 -0
- {trntensor-0.20.0 → trntensor-0.21.0}/trntensor/nki/collectives.py +0 -0
- {trntensor-0.20.0 → trntensor-0.21.0}/trntensor/parallel.py +0 -0
- {trntensor-0.20.0 → trntensor-0.21.0}/trntensor/plan.py +0 -0
- {trntensor-0.20.0 → trntensor-0.21.0}/trntensor/py.typed +0 -0
- {trntensor-0.20.0 → trntensor-0.21.0}/trntensor.egg-info/SOURCES.txt +0 -0
- {trntensor-0.20.0 → trntensor-0.21.0}/trntensor.egg-info/dependency_links.txt +0 -0
- {trntensor-0.20.0 → trntensor-0.21.0}/trntensor.egg-info/requires.txt +0 -0
- {trntensor-0.20.0 → trntensor-0.21.0}/trntensor.egg-info/top_level.txt +0 -0
- {trntensor-0.20.0 → trntensor-0.21.0}/uv.lock +0 -0
|
@@ -7,6 +7,52 @@ 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
|
+
|
|
10
56
|
## [0.20.0] — 2026-10-31
|
|
11
57
|
|
|
12
58
|
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`)
|
|
@@ -120,6 +120,8 @@ supported in maintenance mode."
|
|
|
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
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). |
|
|
123
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. |
|
|
124
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). |
|
|
125
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. |
|
|
@@ -265,6 +267,16 @@ The original AO→MO and MP2 kernels remain the XLA-path (Trn1) kernels. See
|
|
|
265
267
|
- `batched_matmul_kernel(a, b)` — per-batch-slice matmul. Batch dim
|
|
266
268
|
iterated via `nl.affine_range`; each slice reuses the stationary-A
|
|
267
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.
|
|
268
280
|
|
|
269
281
|
The DF-MP2 pair pattern `einsum("ap,bp->ab")` is served by
|
|
270
282
|
`matmul_kernel` through the planner's `transB=True` route — no
|
|
@@ -217,3 +217,144 @@ Kernel alone, device-resident, warm (fp32; padded `nvir` included):
|
|
|
217
217
|
|
|
218
218
|
- Both NKI kernels need `nocc ≤ 128`; larger `nocc` runs the aten steps (native) or raises `NotImplementedError` (XLA).
|
|
219
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).
|
|
@@ -31,6 +31,10 @@ EXPECTED_OPS = {
|
|
|
31
31
|
"mp2_energy_row",
|
|
32
32
|
"ao_to_mo_trn2",
|
|
33
33
|
"ao_to_mo_trn2_packed",
|
|
34
|
+
"df_half_packed",
|
|
35
|
+
"df_exchange_syrk",
|
|
36
|
+
"df_coulomb_d",
|
|
37
|
+
"df_coulomb_j",
|
|
34
38
|
}
|
|
35
39
|
|
|
36
40
|
|
|
@@ -133,6 +137,10 @@ def native_host(monkeypatch):
|
|
|
133
137
|
"mp2_energy_row_kernel",
|
|
134
138
|
"ao_to_mo_trn2_kernel",
|
|
135
139
|
"ao_to_mo_trn2_packed_kernel",
|
|
140
|
+
"df_half_packed_kernel",
|
|
141
|
+
"df_exchange_syrk_kernel",
|
|
142
|
+
"df_coulomb_d_kernel",
|
|
143
|
+
"df_coulomb_j_kernel",
|
|
136
144
|
):
|
|
137
145
|
monkeypatch.setattr(_kernels, kname, no_raw_kernel, raising=False)
|
|
138
146
|
# Force NKI: under "auto" the native path runs the fused quantum
|
|
@@ -437,6 +445,149 @@ class TestPublicApiOnNativePath:
|
|
|
437
445
|
torch.testing.assert_close(out, trntensor.ao_to_mo_transform(eri, C_occ, C_vir))
|
|
438
446
|
assert native_host == []
|
|
439
447
|
|
|
448
|
+
@pytest.mark.parametrize(
|
|
449
|
+
"nbasis,naux,nocc,group", [(40, 6, 5, 0), (130, 30, 7, 0), (40, 600, 6, 1)]
|
|
450
|
+
)
|
|
451
|
+
def test_df_exchange_routes_to_the_two_ops(
|
|
452
|
+
self, native_host, monkeypatch, nbasis, naux, nocc, group
|
|
453
|
+
):
|
|
454
|
+
"""#60: replaying the half kernel's batched gather from the index
|
|
455
|
+
images on CPU rebuilds Xt, and the SYRK stand-in's two partials add
|
|
456
|
+
up to K."""
|
|
457
|
+
|
|
458
|
+
def half_op(eri_v, img, C_occ, nub=8):
|
|
459
|
+
npc, nvg = img.shape[0], img.shape[1]
|
|
460
|
+
tp, nbp = eri_v.shape[1], C_occ.shape[0]
|
|
461
|
+
nvc, nkb = nbp // nvg, nbp // 128
|
|
462
|
+
assert img.dtype == torch.uint32 and img.shape == (npc, nvg, 128, nkb * nvc)
|
|
463
|
+
idx = img.view(torch.int32).long()
|
|
464
|
+
full = torch.empty(nbp, nbp, npc * tp)
|
|
465
|
+
for c in range(npc):
|
|
466
|
+
for g in range(nvg):
|
|
467
|
+
for k in range(nkb):
|
|
468
|
+
for b in range(nvc // nub):
|
|
469
|
+
blk = idx[c, g, :, k * nvc + b * nub : k * nvc + (b + 1) * nub]
|
|
470
|
+
rows = eri_v[blk.T.flatten()].reshape(128, nub, tp)
|
|
471
|
+
v0 = g * nvc + b * nub
|
|
472
|
+
full[k * 128 : (k + 1) * 128, v0 : v0 + nub, c * tp : (c + 1) * tp] = (
|
|
473
|
+
rows
|
|
474
|
+
)
|
|
475
|
+
x = torch.einsum("li,lnP->Pin", C_occ, full) # (P, i, ν)
|
|
476
|
+
return x.reshape(npc * tp, -1, nvg, nvc).permute(0, 2, 1, 3).contiguous()
|
|
477
|
+
|
|
478
|
+
def syrk_op(xt, kin, banks=6):
|
|
479
|
+
nr, nvg, nop, nvc = xt.shape
|
|
480
|
+
assert nop % 2 == 0 # nocc padded even for the program split
|
|
481
|
+
xm = xt.permute(1, 3, 0, 2).reshape(nvg * nvc, -1)
|
|
482
|
+
k = xm @ xm.T
|
|
483
|
+
return kin + torch.stack([0.25 * k, 0.75 * k])
|
|
484
|
+
|
|
485
|
+
_install_ops(monkeypatch, native_host, df_half_packed=half_op, df_exchange_syrk=syrk_op)
|
|
486
|
+
if group: # one aux chunk per launch pair
|
|
487
|
+
monkeypatch.setattr(dispatch, "df_group_chunks", lambda *a: group)
|
|
488
|
+
g = torch.Generator().manual_seed(nbasis)
|
|
489
|
+
eri = torch.randn(nbasis, nbasis, naux, generator=g)
|
|
490
|
+
eri = 0.5 * (eri + eri.transpose(0, 1))
|
|
491
|
+
C = torch.randn(nbasis, nocc, generator=g)
|
|
492
|
+
out = trntensor.df_exchange(trntensor.pack_tril(eri), C, alpha=2.0)
|
|
493
|
+
X = torch.einsum("li,lnP->inP", C, eri)
|
|
494
|
+
ref = 2.0 * torch.einsum("imP,inP->mn", X, X)
|
|
495
|
+
# fp32 sums of up to 600·nocc terms: compare against max |K|.
|
|
496
|
+
assert float((out - ref).abs().max()) <= 1e-5 * float(ref.abs().max())
|
|
497
|
+
tp, nub, _nvc = _kernels.df_half_tiling(naux, -(-nbasis // 128) * 128, nocc + nocc % 2)
|
|
498
|
+
launches = naux // tp if group else 1
|
|
499
|
+
assert [c[0] for c in native_host] == ["df_half_packed", "df_exchange_syrk"] * launches
|
|
500
|
+
assert native_host[0][1] == {"nub": nub} and naux % tp == 0
|
|
501
|
+
|
|
502
|
+
@pytest.mark.parametrize("nbasis", [30, 40])
|
|
503
|
+
def test_df_coulomb_routes_to_the_two_ops(self, native_host, monkeypatch, nbasis):
|
|
504
|
+
"""The kernels take 2·(npair // 256) full row tiles; the leftover rows
|
|
505
|
+
go through aten, and the result matches the reference J."""
|
|
506
|
+
|
|
507
|
+
def d_op(B, pw):
|
|
508
|
+
rows = (B.shape[0] // 256) * 256
|
|
509
|
+
d = B[:rows].T @ pw[:rows, 0]
|
|
510
|
+
return torch.stack([0.5 * d, 0.5 * d])[:, None, :]
|
|
511
|
+
|
|
512
|
+
def j_op(B, d_b):
|
|
513
|
+
rows = (B.shape[0] // 256) * 256
|
|
514
|
+
assert d_b.shape == (128, B.shape[1])
|
|
515
|
+
return (B[:rows] @ d_b[0]).reshape(-1, 128).T
|
|
516
|
+
|
|
517
|
+
_install_ops(monkeypatch, native_host, df_coulomb_d=d_op, df_coulomb_j=j_op)
|
|
518
|
+
g = torch.Generator().manual_seed(nbasis)
|
|
519
|
+
eri = torch.randn(nbasis, nbasis, 9, generator=g)
|
|
520
|
+
eri = 0.5 * (eri + eri.transpose(0, 1))
|
|
521
|
+
D = torch.randn(nbasis, nbasis, generator=g)
|
|
522
|
+
D = D + D.T
|
|
523
|
+
J = trntensor.df_coulomb(trntensor.pack_tril(eri), D)
|
|
524
|
+
ref = torch.einsum("mnP,P->mn", eri, torch.einsum("lsP,ls->P", eri, D))
|
|
525
|
+
torch.testing.assert_close(J, ref, atol=1e-3, rtol=1e-4)
|
|
526
|
+
assert [c[0] for c in native_host] == ["df_coulomb_d", "df_coulomb_j"]
|
|
527
|
+
|
|
528
|
+
def test_df_coulomb_route(self, monkeypatch):
|
|
529
|
+
monkeypatch.setattr(dispatch, "HAS_NKI", True)
|
|
530
|
+
monkeypatch.setattr(dispatch, "_use_simulator", lambda: False)
|
|
531
|
+
monkeypatch.setattr(dispatch, "_use_nki", lambda: True)
|
|
532
|
+
monkeypatch.setattr(dispatch, "_dispatch_path", "native")
|
|
533
|
+
monkeypatch.setattr(dispatch, "_NEURON_DEVICE", "meta")
|
|
534
|
+
big = torch.empty(31375, 1206, device="meta")
|
|
535
|
+
assert dispatch._df_coulomb_route(big) == "trn2"
|
|
536
|
+
assert dispatch._df_coulomb_route(torch.empty(31375, 1206)) == "aten"
|
|
537
|
+
assert dispatch._df_coulomb_route(torch.empty(300, 116, device="meta")) == "aten"
|
|
538
|
+
assert dispatch._df_coulomb_route(torch.empty(200, 116, device="meta")) == "aten"
|
|
539
|
+
monkeypatch.setenv("TRNTENSOR_DF_COULOMB", "trn2")
|
|
540
|
+
assert dispatch._df_coulomb_route(torch.empty(300, 116, device="meta")) == "trn2"
|
|
541
|
+
|
|
542
|
+
def test_df_exchange_aten_route_skips_the_kernels(self, native_host, monkeypatch):
|
|
543
|
+
monkeypatch.setenv("TRNTENSOR_DF_EXCHANGE", "aten")
|
|
544
|
+
_install_ops(monkeypatch, native_host)
|
|
545
|
+
eri = torch.randn(12, 12, 5)
|
|
546
|
+
eri = 0.5 * (eri + eri.transpose(0, 1))
|
|
547
|
+
C = torch.randn(12, 3)
|
|
548
|
+
X = torch.einsum("li,lnP->inP", C, eri)
|
|
549
|
+
out = trntensor.df_exchange(trntensor.pack_tril(eri), C)
|
|
550
|
+
torch.testing.assert_close(out, torch.einsum("imP,inP->mn", X, X), atol=1e-4, rtol=1e-4)
|
|
551
|
+
assert native_host == []
|
|
552
|
+
|
|
553
|
+
def test_df_exchange_route(self, monkeypatch):
|
|
554
|
+
monkeypatch.setattr(dispatch, "HAS_NKI", True)
|
|
555
|
+
monkeypatch.setattr(dispatch, "_use_simulator", lambda: False)
|
|
556
|
+
monkeypatch.setattr(dispatch, "_use_nki", lambda: True)
|
|
557
|
+
monkeypatch.setattr(dispatch, "_dispatch_path", "native")
|
|
558
|
+
monkeypatch.setattr(dispatch, "_NEURON_DEVICE", "meta")
|
|
559
|
+
npair = 256 * 257 // 2
|
|
560
|
+
dev = torch.empty(npair, 1206, device="meta")
|
|
561
|
+
assert dispatch._df_exchange_route(dev, 41, 256) == "trn2"
|
|
562
|
+
assert dispatch._df_exchange_route(torch.empty(npair, 1206), 41, 256) == "aten"
|
|
563
|
+
assert dispatch._df_exchange_route(dev, 513, 256) == "aten"
|
|
564
|
+
monkeypatch.setenv("TRNTENSOR_DF_EXCHANGE", "aten")
|
|
565
|
+
assert dispatch._df_exchange_route(dev, 41, 256) == "aten"
|
|
566
|
+
monkeypatch.setenv("TRNTENSOR_DF_EXCHANGE", "bogus")
|
|
567
|
+
with pytest.raises(ValueError):
|
|
568
|
+
dispatch._df_exchange_route(dev, 41, 256)
|
|
569
|
+
|
|
570
|
+
@pytest.mark.parametrize(
|
|
571
|
+
"naux,nb,nocc", [(1206, 256, 41), (2366, 512, 81), (4686, 1024, 161), (768, 256, 32)]
|
|
572
|
+
)
|
|
573
|
+
def test_df_half_tiling_divides_and_fits(self, naux, nb, nocc):
|
|
574
|
+
tp, nub, nvc = _kernels.df_half_tiling(naux, nb, nocc)
|
|
575
|
+
assert naux % tp == 0 and tp <= 512 and nb % nvc == 0 and nvc % nub == 0
|
|
576
|
+
assert _kernels.df_half_sbuf_bytes(nb, nocc, tp, nub, nvc) <= _kernels.DF_K_SBUF_BUDGET
|
|
577
|
+
g = _kernels.df_group_chunks(naux // tp, nb, tp, nub, nvc)
|
|
578
|
+
assert 1 <= g <= naux // tp
|
|
579
|
+
|
|
580
|
+
@pytest.mark.parametrize("nb,banks", [(128, 6), (512, 6), (1024, 6), (1024, 2), (2048, 8)])
|
|
581
|
+
def test_df_exchange_blocks_cover_the_upper_triangle(self, nb, banks):
|
|
582
|
+
passes = _kernels.df_exchange_blocks(nb, banks)
|
|
583
|
+
cover = torch.zeros(nb // 128, nb // 128, dtype=torch.int64)
|
|
584
|
+
for blocks in passes:
|
|
585
|
+
assert 1 <= len(blocks) <= banks
|
|
586
|
+
for m, v0, w in blocks:
|
|
587
|
+
assert v0 >= 128 * m and w <= 512 and v0 + w <= nb
|
|
588
|
+
cover[m, v0 // 128 : (v0 + w) // 128] += 1
|
|
589
|
+
assert torch.equal(cover, torch.triu(torch.ones_like(cover)))
|
|
590
|
+
|
|
440
591
|
def test_ao_to_mo_transform_legacy_routes_and_slices(self, native_host, monkeypatch):
|
|
441
592
|
monkeypatch.setenv("TRNTENSOR_AO_TO_MO", "legacy")
|
|
442
593
|
monkeypatch.setattr(dispatch, "_native_kernel_deprecations_emitted", set())
|
|
@@ -1231,3 +1382,10 @@ class TestLazyRegistration:
|
|
|
1231
1382
|
m(32896 * 3, 256), m(3, 128, 512, dtype=torch.int32), m(256, 16), m(256, 512)
|
|
1232
1383
|
)
|
|
1233
1384
|
assert bp.shape == (16, 512, 768)
|
|
1385
|
+
xt = fake("df_half_packed")(
|
|
1386
|
+
m(32896 * 6, 201), m(2, 4, 128, 128, dtype=torch.int32), m(256, 42)
|
|
1387
|
+
)
|
|
1388
|
+
assert xt.shape == (402, 4, 42, 64)
|
|
1389
|
+
assert fake("df_exchange_syrk")(xt, m(2, 256, 256)).shape == (2, 256, 256)
|
|
1390
|
+
assert fake("df_coulomb_d")(m(31375, 1206), m(31375, 1)).shape == (2, 1, 1206)
|
|
1391
|
+
assert fake("df_coulomb_j")(m(31375, 1206), m(128, 1206)).shape == (128, 244)
|
|
@@ -97,6 +97,35 @@ KERNEL_INPUTS = {
|
|
|
97
97
|
),
|
|
98
98
|
),
|
|
99
99
|
],
|
|
100
|
+
"df_half_packed_kernel": [
|
|
101
|
+
# (packed (npair·NPC, TP) rows, (NPC, NVG, 128, NKB·NVC) uint32 indices, C_occ)
|
|
102
|
+
(
|
|
103
|
+
"nb256_nocc41_naux402",
|
|
104
|
+
lambda: ((_r(32896, 402), _u32(1, 4, 128, 128), _r(256, 41)), {"nub": 2}),
|
|
105
|
+
),
|
|
106
|
+
(
|
|
107
|
+
"nb256_nocc20_naux390_split_chunks",
|
|
108
|
+
lambda: ((_r(32896 * 2, 195), _u32(2, 2, 128, 256), _r(256, 20)), {"nub": 4}),
|
|
109
|
+
),
|
|
110
|
+
],
|
|
111
|
+
"df_exchange_syrk_kernel": [
|
|
112
|
+
# (Xt (rows, NVG, nocc, NVC), running partial Ks (2, NB, NB)); partial last tile
|
|
113
|
+
("rows402_nocc6_nb256", lambda: ((_r(402, 2, 6, 128), _r(2, 256, 256)), {})),
|
|
114
|
+
(
|
|
115
|
+
"rows256_nocc34_nb640_banks2",
|
|
116
|
+
lambda: ((_r(256, 40, 34, 16), _r(2, 640, 640)), {"banks": 2}),
|
|
117
|
+
),
|
|
118
|
+
],
|
|
119
|
+
"df_coulomb_d_kernel": [
|
|
120
|
+
# (packed B (npair, naux), weighted packed density (npair, 1))
|
|
121
|
+
("npair1000_naux450", lambda: ((_r(1000, 450), _r(1000, 1)), {})),
|
|
122
|
+
("npair600_naux4500", lambda: ((_r(600, 4500), _r(600, 1)), {})),
|
|
123
|
+
],
|
|
124
|
+
"df_coulomb_j_kernel": [
|
|
125
|
+
# (packed B (npair, naux), d replicated on 128 partitions)
|
|
126
|
+
("npair1000_naux450", lambda: ((_r(1000, 450), _r(128, 450)), {})),
|
|
127
|
+
("npair600_naux4500", lambda: ((_r(600, 4500), _r(128, 4500)), {})),
|
|
128
|
+
],
|
|
100
129
|
"ao_to_mo_trn2_kernel": [
|
|
101
130
|
(
|
|
102
131
|
"nb256_nocc32_nvir256_naux512",
|
|
@@ -104,6 +104,10 @@ class TestNativeDispatch:
|
|
|
104
104
|
"mp2_energy_row",
|
|
105
105
|
"ao_to_mo_trn2",
|
|
106
106
|
"ao_to_mo_trn2_packed",
|
|
107
|
+
"df_half_packed",
|
|
108
|
+
"df_exchange_syrk",
|
|
109
|
+
"df_coulomb_d",
|
|
110
|
+
"df_coulomb_j",
|
|
107
111
|
):
|
|
108
112
|
assert hasattr(torch.ops.trntensor, name)
|
|
109
113
|
|
|
@@ -223,6 +227,72 @@ class TestNativeTrn2Kernels:
|
|
|
223
227
|
ref = torch.einsum("na,inP->iaP", C_vir, torch.einsum("mi,mnP->inP", C_occ, eri))
|
|
224
228
|
torch.testing.assert_close(out.cpu(), ref, atol=1e-3, rtol=1e-4)
|
|
225
229
|
|
|
230
|
+
@pytest.mark.parametrize(
|
|
231
|
+
"nbasis,nocc,naux,group",
|
|
232
|
+
[(128, 8, 128, 0), (200, 21, 558, 0), (256, 41, 1206, 0), (300, 33, 600, 1)],
|
|
233
|
+
)
|
|
234
|
+
def test_df_exchange_kernels_match_reference(self, monkeypatch, nbasis, nocc, naux, group):
|
|
235
|
+
"""#60: gather + half transform + PSUM SYRK against the fp64 K,
|
|
236
|
+
including odd nocc (padded), non-128 nbasis, partial aux tiles and
|
|
237
|
+
several launch groups."""
|
|
238
|
+
import trntensor
|
|
239
|
+
from trntensor.nki import dispatch
|
|
240
|
+
|
|
241
|
+
if group:
|
|
242
|
+
monkeypatch.setattr(dispatch, "df_group_chunks", lambda *a: group)
|
|
243
|
+
g = torch.Generator().manual_seed(nbasis + naux)
|
|
244
|
+
eri = torch.randn(nbasis, nbasis, naux, generator=g, dtype=torch.float64) * 0.1
|
|
245
|
+
eri = 0.5 * (eri + eri.transpose(0, 1))
|
|
246
|
+
C = torch.randn(nbasis, nocc, generator=g, dtype=torch.float64) / nbasis**0.5
|
|
247
|
+
pk = trntensor.pack_tril(eri)
|
|
248
|
+
out = dispatch._nki_df_exchange(pk.float().to("neuron"), C.float().to("neuron"))
|
|
249
|
+
assert out.device.type == "neuron" and out.shape == (nbasis, nbasis)
|
|
250
|
+
X = torch.einsum("li,lnP->inP", C, eri)
|
|
251
|
+
ref = torch.einsum("imP,inP->mn", X, X)
|
|
252
|
+
K = out.cpu().double()
|
|
253
|
+
assert torch.equal(K, K.T)
|
|
254
|
+
assert float((K - ref).abs().max() / ref.abs().max()) < 2e-6
|
|
255
|
+
|
|
256
|
+
@pytest.mark.parametrize("nbasis,naux", [(64, 300), (250, 1206)])
|
|
257
|
+
def test_df_coulomb_kernels_match_reference(self, nbasis, naux):
|
|
258
|
+
import trntensor
|
|
259
|
+
from trntensor.nki import dispatch
|
|
260
|
+
|
|
261
|
+
g = torch.Generator().manual_seed(nbasis)
|
|
262
|
+
eri = torch.randn(nbasis, nbasis, naux, generator=g, dtype=torch.float64) * 0.1
|
|
263
|
+
eri = 0.5 * (eri + eri.transpose(0, 1))
|
|
264
|
+
D = torch.randn(nbasis, nbasis, generator=g, dtype=torch.float64)
|
|
265
|
+
D = D + D.T
|
|
266
|
+
pk = trntensor.pack_tril(eri)
|
|
267
|
+
r = torch.tril_indices(nbasis, nbasis)
|
|
268
|
+
pw = D[r[0], r[1]] * torch.where(r[0] == r[1], 1.0, 2.0).double()
|
|
269
|
+
jp = dispatch._nki_df_coulomb(pk.float().to("neuron"), pw.float().to("neuron"))
|
|
270
|
+
ref = pk @ (pk.T @ pw)
|
|
271
|
+
assert float((jp.cpu().double() - ref).abs().max() / ref.abs().max()) < 2e-6
|
|
272
|
+
|
|
273
|
+
def test_df_exchange_and_coulomb_auto_on_device(self):
|
|
274
|
+
"""Under "auto", device-resident packed B above the thresholds runs
|
|
275
|
+
the kernels and the results stay on the device."""
|
|
276
|
+
import trntensor
|
|
277
|
+
|
|
278
|
+
prev = trntensor.get_backend()
|
|
279
|
+
trntensor.set_backend("auto")
|
|
280
|
+
try:
|
|
281
|
+
g = torch.Generator().manual_seed(11)
|
|
282
|
+
eri = torch.randn(200, 200, 558, generator=g) * 0.1
|
|
283
|
+
eri = 0.5 * (eri + eri.transpose(0, 1))
|
|
284
|
+
C = torch.randn(200, 21, generator=g) / 200**0.5
|
|
285
|
+
B = trntensor.pack_tril(eri).to("neuron")
|
|
286
|
+
K = trntensor.df_exchange(B, C.to("neuron"), alpha=2.0)
|
|
287
|
+
J = trntensor.df_coulomb(B, (2 * C @ C.T).to("neuron"))
|
|
288
|
+
finally:
|
|
289
|
+
trntensor.set_backend(prev)
|
|
290
|
+
assert K.device.type == "neuron" and J.device.type == "neuron"
|
|
291
|
+
Kr = trntensor.df_exchange(trntensor.pack_tril(eri), C, alpha=2.0)
|
|
292
|
+
Jr = trntensor.df_coulomb(trntensor.pack_tril(eri), 2 * C @ C.T)
|
|
293
|
+
torch.testing.assert_close(K.cpu(), Kr, atol=1e-4, rtol=1e-4)
|
|
294
|
+
torch.testing.assert_close(J.cpu(), Jr, atol=1e-3, rtol=1e-4)
|
|
295
|
+
|
|
226
296
|
def test_packed_host_cderi_uploads_and_routes_to_packed_kernel(self, monkeypatch):
|
|
227
297
|
"""PySCF-layout (naux, npair) host integrals with device orbitals:
|
|
228
298
|
uploaded as is, transposed on the device, packed kernel under auto."""
|
|
@@ -177,3 +177,79 @@ class TestPackedAoToMo:
|
|
|
177
177
|
trntensor.ao_to_mo_transform(torch.randn(21), C, C)
|
|
178
178
|
with pytest.raises(ValueError, match="C_occ must have shape"):
|
|
179
179
|
trntensor.ao_to_mo_transform(torch.randn(6, 6, 4), torch.randn(5, 2), C)
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
class TestDfExchange:
|
|
183
|
+
"""#60: K = Σ_{iP} X X^T from packed (μ≥ν) integrals."""
|
|
184
|
+
|
|
185
|
+
@staticmethod
|
|
186
|
+
def _case(n=13, naux=7, nocc=4, seed=0):
|
|
187
|
+
g = torch.Generator().manual_seed(seed)
|
|
188
|
+
eri = torch.randn(n, n, naux, generator=g, dtype=torch.float64)
|
|
189
|
+
eri = 0.5 * (eri + eri.transpose(0, 1))
|
|
190
|
+
C = torch.randn(n, nocc, generator=g, dtype=torch.float64)
|
|
191
|
+
X = torch.einsum("li,lnP->inP", C, eri)
|
|
192
|
+
return eri, C, torch.einsum("imP,inP->mn", X, X)
|
|
193
|
+
|
|
194
|
+
@pytest.mark.parametrize("layout", ["packed", "cderi", "full"])
|
|
195
|
+
def test_matches_reference(self, layout):
|
|
196
|
+
import trntensor
|
|
197
|
+
|
|
198
|
+
eri, C, K = self._case()
|
|
199
|
+
pk = trntensor.pack_tril(eri)
|
|
200
|
+
B = {"packed": pk, "cderi": pk.T.contiguous(), "full": eri}[layout]
|
|
201
|
+
out = trntensor.df_exchange(B, C, alpha=2.0)
|
|
202
|
+
assert out.shape == (13, 13)
|
|
203
|
+
torch.testing.assert_close(out, 2.0 * K)
|
|
204
|
+
|
|
205
|
+
def test_gather_blocks(self, monkeypatch):
|
|
206
|
+
"""Several ν blocks of gathered rows give the same K."""
|
|
207
|
+
import trntensor
|
|
208
|
+
import trntensor.quantum as q
|
|
209
|
+
|
|
210
|
+
eri, C, K = self._case(n=20, naux=9, nocc=3, seed=1)
|
|
211
|
+
monkeypatch.setattr(q, "_DF_K_GATHER_BYTES", 20 * 9 * 8 * 3)
|
|
212
|
+
torch.testing.assert_close(trntensor.df_exchange(trntensor.pack_tril(eri), C), K)
|
|
213
|
+
|
|
214
|
+
def test_shape_errors(self):
|
|
215
|
+
import trntensor
|
|
216
|
+
|
|
217
|
+
eri, C, _ = self._case()
|
|
218
|
+
with pytest.raises(ValueError):
|
|
219
|
+
trntensor.df_exchange(torch.zeros(10, 7), C)
|
|
220
|
+
with pytest.raises(ValueError):
|
|
221
|
+
trntensor.df_exchange(eri[:, :5], C)
|
|
222
|
+
with pytest.raises(ValueError):
|
|
223
|
+
trntensor.df_exchange(trntensor.pack_tril(eri), C[:, 0])
|
|
224
|
+
|
|
225
|
+
|
|
226
|
+
class TestDfCoulomb:
|
|
227
|
+
"""#60: J = Σ_P B[:, P] (Σ_{λσ} D_λσ B[λσ, P]) from packed integrals."""
|
|
228
|
+
|
|
229
|
+
@pytest.mark.parametrize("cderi", [False, True])
|
|
230
|
+
def test_matches_reference(self, cderi):
|
|
231
|
+
g = torch.Generator().manual_seed(5)
|
|
232
|
+
n = 11
|
|
233
|
+
eri = torch.randn(n, n, 6, generator=g, dtype=torch.float64)
|
|
234
|
+
eri = 0.5 * (eri + eri.transpose(0, 1))
|
|
235
|
+
D = torch.randn(n, n, generator=g, dtype=torch.float64)
|
|
236
|
+
D = D + D.T
|
|
237
|
+
pk = trntensor.pack_tril(eri)
|
|
238
|
+
J = trntensor.df_coulomb(pk.T.contiguous() if cderi else pk, D)
|
|
239
|
+
ref = torch.einsum("mnP,P->mn", eri, torch.einsum("lsP,ls->P", eri, D))
|
|
240
|
+
torch.testing.assert_close(J, ref)
|
|
241
|
+
# Packed weighted density in, packed J out.
|
|
242
|
+
w = 2.0 * trntensor.pack_tril(D[:, :, None])[:, 0]
|
|
243
|
+
r = torch.tril_indices(n, n)
|
|
244
|
+
w[r[0] == r[1]] *= 0.5
|
|
245
|
+
torch.testing.assert_close(
|
|
246
|
+
trntensor.df_coulomb(pk, w), trntensor.pack_tril(ref[:, :, None])[:, 0]
|
|
247
|
+
)
|
|
248
|
+
|
|
249
|
+
def test_shape_errors(self):
|
|
250
|
+
with pytest.raises(ValueError):
|
|
251
|
+
trntensor.df_coulomb(torch.zeros(10, 3), torch.zeros(4, 5))
|
|
252
|
+
with pytest.raises(ValueError):
|
|
253
|
+
trntensor.df_coulomb(torch.zeros(10, 3), torch.zeros(11))
|
|
254
|
+
with pytest.raises(ValueError):
|
|
255
|
+
trntensor.df_coulomb(torch.zeros(4, 4, 3), torch.zeros(4, 4))
|