trntensor 0.24.0__tar.gz → 0.25.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.24.0 → trntensor-0.25.0}/CHANGELOG.md +41 -0
- {trntensor-0.24.0 → trntensor-0.25.0}/PKG-INFO +1 -1
- {trntensor-0.24.0 → trntensor-0.25.0}/docs/api/nki.md +1 -0
- {trntensor-0.24.0 → trntensor-0.25.0}/docs/api/quantum.md +35 -0
- {trntensor-0.24.0 → trntensor-0.25.0}/pyproject.toml +1 -1
- {trntensor-0.24.0 → trntensor-0.25.0}/tests/test_kernel_dtype_guard.py +24 -3
- {trntensor-0.24.0 → trntensor-0.25.0}/tests/test_native_dispatch.py +110 -1
- {trntensor-0.24.0 → trntensor-0.25.0}/tests/test_nki_compile.py +21 -0
- {trntensor-0.24.0 → trntensor-0.25.0}/tests/test_nki_native.py +41 -0
- {trntensor-0.24.0 → trntensor-0.25.0}/trntensor/__init__.py +1 -1
- {trntensor-0.24.0 → trntensor-0.25.0}/trntensor/nki/_kernels.py +253 -0
- {trntensor-0.24.0 → trntensor-0.25.0}/trntensor/nki/_native.py +15 -0
- {trntensor-0.24.0 → trntensor-0.25.0}/trntensor/nki/dispatch.py +138 -1
- {trntensor-0.24.0 → trntensor-0.25.0}/trntensor/quantum.py +7 -1
- {trntensor-0.24.0 → trntensor-0.25.0}/trntensor.egg-info/PKG-INFO +1 -1
- {trntensor-0.24.0 → trntensor-0.25.0}/.github/workflows/ci.yml +0 -0
- {trntensor-0.24.0 → trntensor-0.25.0}/.github/workflows/notify-umbrella.yml +0 -0
- {trntensor-0.24.0 → trntensor-0.25.0}/.github/workflows/publish.yml +0 -0
- {trntensor-0.24.0 → trntensor-0.25.0}/.gitignore +0 -0
- {trntensor-0.24.0 → trntensor-0.25.0}/.pre-commit-config.yaml +0 -0
- {trntensor-0.24.0 → trntensor-0.25.0}/CLAUDE.md +0 -0
- {trntensor-0.24.0 → trntensor-0.25.0}/CODE_OF_CONDUCT.md +0 -0
- {trntensor-0.24.0 → trntensor-0.25.0}/CONTRIBUTING.md +0 -0
- {trntensor-0.24.0 → trntensor-0.25.0}/LICENSE +0 -0
- {trntensor-0.24.0 → trntensor-0.25.0}/README.md +0 -0
- {trntensor-0.24.0 → trntensor-0.25.0}/benchmarks/__init__.py +0 -0
- {trntensor-0.24.0 → trntensor-0.25.0}/benchmarks/bench_einsum.py +0 -0
- {trntensor-0.24.0 → trntensor-0.25.0}/docs/api/decompose.md +0 -0
- {trntensor-0.24.0 → trntensor-0.25.0}/docs/api/einsum.md +0 -0
- {trntensor-0.24.0 → trntensor-0.25.0}/docs/api/index.md +0 -0
- {trntensor-0.24.0 → trntensor-0.25.0}/docs/api/plan.md +0 -0
- {trntensor-0.24.0 → trntensor-0.25.0}/docs/api/stream.md +0 -0
- {trntensor-0.24.0 → trntensor-0.25.0}/docs/architecture.md +0 -0
- {trntensor-0.24.0 → trntensor-0.25.0}/docs/aws_setup.md +0 -0
- {trntensor-0.24.0 → trntensor-0.25.0}/docs/benchmarks.md +0 -0
- {trntensor-0.24.0 → trntensor-0.25.0}/docs/contraction_patterns.md +0 -0
- {trntensor-0.24.0 → trntensor-0.25.0}/docs/design/matmul_dispatch_findings.md +0 -0
- {trntensor-0.24.0 → trntensor-0.25.0}/docs/design/matmul_kernel_profile.md +0 -0
- {trntensor-0.24.0 → trntensor-0.25.0}/docs/developing_kernels.md +0 -0
- {trntensor-0.24.0 → trntensor-0.25.0}/docs/index.md +0 -0
- {trntensor-0.24.0 → trntensor-0.25.0}/docs/installation.md +0 -0
- {trntensor-0.24.0 → trntensor-0.25.0}/docs/quickstart.md +0 -0
- {trntensor-0.24.0 → trntensor-0.25.0}/docs/tutorial_df_mp2.md +0 -0
- {trntensor-0.24.0 → trntensor-0.25.0}/examples/df_mp2_einsum.py +0 -0
- {trntensor-0.24.0 → trntensor-0.25.0}/infra/terraform/README.md +0 -0
- {trntensor-0.24.0 → trntensor-0.25.0}/infra/terraform/main.tf +0 -0
- {trntensor-0.24.0 → trntensor-0.25.0}/mkdocs.yml +0 -0
- {trntensor-0.24.0 → trntensor-0.25.0}/scripts/autotune_dispatch.py +0 -0
- {trntensor-0.24.0 → trntensor-0.25.0}/scripts/bench_ao2mo_nocc.py +0 -0
- {trntensor-0.24.0 → trntensor-0.25.0}/scripts/bench_eager_handle.py +0 -0
- {trntensor-0.24.0 → trntensor-0.25.0}/scripts/bench_native_einsum.py +0 -0
- {trntensor-0.24.0 → trntensor-0.25.0}/scripts/bench_native_kernels.py +0 -0
- {trntensor-0.24.0 → trntensor-0.25.0}/scripts/bench_to_md.py +0 -0
- {trntensor-0.24.0 → trntensor-0.25.0}/scripts/run_benchmarks.sh +0 -0
- {trntensor-0.24.0 → trntensor-0.25.0}/scripts/run_neuron_profile.sh +0 -0
- {trntensor-0.24.0 → trntensor-0.25.0}/scripts/run_neuron_tests.sh +0 -0
- {trntensor-0.24.0 → trntensor-0.25.0}/scripts/run_reduce_parallel_multirank.py +0 -0
- {trntensor-0.24.0 → trntensor-0.25.0}/scripts/run_simulator_tests.sh +0 -0
- {trntensor-0.24.0 → trntensor-0.25.0}/setup.cfg +0 -0
- {trntensor-0.24.0 → trntensor-0.25.0}/tests/conftest.py +0 -0
- {trntensor-0.24.0 → trntensor-0.25.0}/tests/test_conformance.py +0 -0
- {trntensor-0.24.0 → trntensor-0.25.0}/tests/test_decompose.py +0 -0
- {trntensor-0.24.0 → trntensor-0.25.0}/tests/test_einsum.py +0 -0
- {trntensor-0.24.0 → trntensor-0.25.0}/tests/test_host_output.py +0 -0
- {trntensor-0.24.0 → trntensor-0.25.0}/tests/test_nki.py +0 -0
- {trntensor-0.24.0 → trntensor-0.25.0}/tests/test_nki_kernels.py +0 -0
- {trntensor-0.24.0 → trntensor-0.25.0}/tests/test_nki_sim.py +0 -0
- {trntensor-0.24.0 → trntensor-0.25.0}/tests/test_nki_tail_audit.py +0 -0
- {trntensor-0.24.0 → trntensor-0.25.0}/tests/test_parallel.py +0 -0
- {trntensor-0.24.0 → trntensor-0.25.0}/tests/test_plan.py +0 -0
- {trntensor-0.24.0 → trntensor-0.25.0}/tests/test_quantum.py +0 -0
- {trntensor-0.24.0 → trntensor-0.25.0}/tests/test_sr_runtime.py +0 -0
- {trntensor-0.24.0 → trntensor-0.25.0}/tests/test_stream.py +0 -0
- {trntensor-0.24.0 → trntensor-0.25.0}/tests/test_trace_cache_safety.py +0 -0
- {trntensor-0.24.0 → trntensor-0.25.0}/trntensor/decompose.py +0 -0
- {trntensor-0.24.0 → trntensor-0.25.0}/trntensor/einsum.py +0 -0
- {trntensor-0.24.0 → trntensor-0.25.0}/trntensor/nki/__init__.py +0 -0
- {trntensor-0.24.0 → trntensor-0.25.0}/trntensor/nki/collectives.py +0 -0
- {trntensor-0.24.0 → trntensor-0.25.0}/trntensor/parallel.py +0 -0
- {trntensor-0.24.0 → trntensor-0.25.0}/trntensor/plan.py +0 -0
- {trntensor-0.24.0 → trntensor-0.25.0}/trntensor/py.typed +0 -0
- {trntensor-0.24.0 → trntensor-0.25.0}/trntensor/stream.py +0 -0
- {trntensor-0.24.0 → trntensor-0.25.0}/trntensor.egg-info/SOURCES.txt +0 -0
- {trntensor-0.24.0 → trntensor-0.25.0}/trntensor.egg-info/dependency_links.txt +0 -0
- {trntensor-0.24.0 → trntensor-0.25.0}/trntensor.egg-info/requires.txt +0 -0
- {trntensor-0.24.0 → trntensor-0.25.0}/trntensor.egg-info/top_level.txt +0 -0
- {trntensor-0.24.0 → trntensor-0.25.0}/uv.lock +0 -0
|
@@ -7,6 +7,47 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [0.25.0] — 2026-11-04
|
|
11
|
+
|
|
12
|
+
Validated on Trn2 (trn2.3xlarge, Neuron SDK 2.33.0, native TorchNeuron) — see trnsci/trnsci#35.
|
|
13
|
+
|
|
14
|
+
Headline: the blocked packed AO→MO kernel (#112, #113): `ao_to_mo_transform` on packed fp32 integrals stays on the device past the old SBUF budget (n_ao 3508 at 85 ms per chunk vs 8.6 s on the host), and `auto` picks it where the resident kernel does not fit.
|
|
15
|
+
|
|
16
|
+
### Fixed
|
|
17
|
+
|
|
18
|
+
- **Packed `ao_to_mo_transform` on the device past n_ao ≈ 1100–1300 (#112,
|
|
19
|
+
trnsci/trnsci#115).** `ao_to_mo_trn2_packed_kernel` keeps C_occ, C_vir, one
|
|
20
|
+
chunk's gather indices and a whole μ column of eri in SBUF. At n_ao 3508
|
|
21
|
+
(alkane60 / cc-pVTZ, 87-column aux chunks) that needs 979 KiB per
|
|
22
|
+
partition against a 176 KiB budget, and at n_ao 1658 it needs 282 KiB. Both
|
|
23
|
+
cases fell back to aten, which unpacks a full square per chunk (4.3 GB at
|
|
24
|
+
3508). That first call did not finish compiling in 19 minutes. The new
|
|
25
|
+
`ao_to_mo_trn2_packed_blocked_kernel` streams every operand over blocks of
|
|
26
|
+
up to 8 μ/ν tiles, and its PSUM accumulators live across the blocks. Its
|
|
27
|
+
SBUF footprint (71–78 KiB at the measured shapes) does not depend on n_ao.
|
|
28
|
+
The aux chunk width is the chunk itself up to 512 columns, otherwise a
|
|
29
|
+
divisor in [64, 512]. So an 87-column chunk goes in without the
|
|
30
|
+
128-column padding copy (3.1 GB at n_ao 3508). Under `"auto"`, fp32 packed
|
|
31
|
+
integrals take it wherever the resident kernel does not fit, and
|
|
32
|
+
`TRNTENSOR_AO_TO_MO_PACKED=auto|resident|blocked` overrides. Measured on
|
|
33
|
+
Trn2 with one core, fp32 and synthetic packed chunks:
|
|
34
|
+
- At n_ao 3508 / nocc 241 / 87 columns the kernel takes 85 ms per chunk at
|
|
35
|
+
LNC=2 and 94 ms at LNC=1. The host fp32 route on 2 threads takes 8.6 s.
|
|
36
|
+
The cold compile takes 162 s, and 26 s with the NEFF cache.
|
|
37
|
+
- At n_ao 1658 / 161 / 384 it takes 56 ms against 6.1 s on the host, with
|
|
38
|
+
a 128 s cold compile. At 2522 / 215 / 168 it takes 73 ms against 7.9 s,
|
|
39
|
+
with a 195 s cold compile.
|
|
40
|
+
- Its maximum error against an fp64 host reference is 3.2–4.4e-7 relative
|
|
41
|
+
(3.9e-7 at 3508). The host fp32 route gets 5.4–6.6e-7.
|
|
42
|
+
- A full alkane60-shaped pass through `AuxStream.ao_to_mo` took 43 s warm
|
|
43
|
+
and 102 s including the first-call compiles (NEFF cache warm from the
|
|
44
|
+
single-chunk runs). That is 97 chunks, 8400 aux and 207 GB uploaded. The
|
|
45
|
+
96 full chunks reuse one host tensor, so no disk reads are included.
|
|
46
|
+
The trnsci#115 run did this pass on the host in 673 s, reading the store
|
|
47
|
+
from NVMe.
|
|
48
|
+
- Where the resident kernel fits, the two kernels give bit-identical
|
|
49
|
+
results, because they run the same per-element PSUM chains.
|
|
50
|
+
|
|
10
51
|
## [0.24.0] — 2026-11-04
|
|
11
52
|
|
|
12
53
|
Validated on Trn2 (trn2.3xlarge, Neuron SDK 2.33.0, native TorchNeuron) — see trnsci/trnsci#35.
|
|
@@ -186,6 +186,7 @@ supported in maintenance mode."
|
|
|
186
186
|
| `NKI_ENABLE_TRACE_CACHE=0` | TorchNeuron setting (not trntensor's). Turns off the cross-process NKI kernel cache, which cannot version editable installs and may serve a stale kernel after a source edit. Set it with `pip install -e`, or if you write your own NKI kernel factories ([aws-neuron/aws-neuron-sdk#1454](https://github.com/aws-neuron/aws-neuron-sdk/issues/1454)). The test suite keeps the cache on, with a fresh cache directory per session. |
|
|
187
187
|
| `TRNTENSOR_MP2_ENERGY=auto\|rows\|aten\|legacy` | Native path: force the `mp2_energy` executor (`rows` = the Trn2 row kernel, `legacy` = the original kernel, deprecated there). |
|
|
188
188
|
| `TRNTENSOR_AO_TO_MO=auto\|trn2\|aten\|legacy` | Native path: force the `ao_to_mo_transform` executor (`trn2` = the Trn2 kernel, or `ao_to_mo_trn2_packed_kernel` for a packed 2-D `eri`; `legacy` = the original kernel, deprecated there; `aten` and `legacy` unpack a packed `eri` first). |
|
|
189
|
+
| `TRNTENSOR_AO_TO_MO_PACKED=auto\|resident\|blocked` | Native path, packed `eri` on the Trn2 route: pick the kernel (`resident` = `ao_to_mo_trn2_packed_kernel`; `blocked` = `ao_to_mo_trn2_packed_blocked_kernel`, fp32 only, with an SBUF footprint independent of n_ao). `auto` keeps the resident kernel wherever its SBUF working set fits and takes the blocked one beyond it (#112). |
|
|
189
190
|
| `TRNTENSOR_DF_EXCHANGE=auto\|trn2\|aten` | Native path: force the `df_exchange` executor (`trn2` = `df_half_packed_kernel` + `df_exchange_syrk_kernel` on packed integrals; `aten` = row gather + einsum + one GEMM). `auto` uses the kernels for device-resident packed `B` from `nocc·nbasis²·naux` = 1e7 (#60). |
|
|
190
191
|
| `TRNTENSOR_DF_COULOMB=auto\|trn2\|aten` | Native path: force the `df_coulomb` executor (`trn2` = `df_coulomb_d_kernel` + `df_coulomb_j_kernel`; `aten` = two GEMVs). `auto` uses the kernels for device-resident packed `B` from `npair·naux` = 1e7 (#60). |
|
|
191
192
|
| `TRNTENSOR_HOST_ROUTE=auto\|host\|device` | Native path, `"auto"` backend, all operands on the host: where the work runs (#56). `auto` (default) uploads it once it reaches the measured thresholds below; `host` keeps it on the host; `device` always uploads. Covers `mp2_energy`, `ao_to_mo_transform`, `einsum` / `multi_einsum` (matmul, bmm, generic and multi-operand plans). Only float32 / bfloat16 / float16 operands are uploaded. `set_backend("pytorch")` and `set_backend("nki")` ignore it. |
|
|
@@ -226,6 +226,41 @@ Kernel alone, device-resident, warm (fp32; padded `nvir` included):
|
|
|
226
226
|
(max error ~1 against float64), while the kernel matched float64 to 2e-7;
|
|
227
227
|
aten was correct at nbasis 736. Its timings there are for reference only.
|
|
228
228
|
|
|
229
|
+
- **Large packed bases (#112).** The packed kernel above keeps C_occ,
|
|
230
|
+
C_vir, one chunk's gather indices and a whole μ column of eri in SBUF. That
|
|
231
|
+
outgrows the budget from n_ao ≈ 1100–1300 (979 KiB per partition at
|
|
232
|
+
n_ao 3508). From there on, fp32 packed integrals run
|
|
233
|
+
`ao_to_mo_trn2_packed_blocked_kernel`. It streams every operand over
|
|
234
|
+
blocks of up to 8 μ/ν tiles, keeps its PSUM accumulators across the
|
|
235
|
+
blocks, and has an SBUF footprint (≈ 71–78 KiB) that does not grow with
|
|
236
|
+
n_ao. Any aux chunk width up to 512 goes in without a padding copy. Where
|
|
237
|
+
both kernels fit, they agree bit for bit.
|
|
238
|
+
`TRNTENSOR_AO_TO_MO_PACKED=auto|resident|blocked` picks the kernel.
|
|
239
|
+
|
|
240
|
+
Measured on Trn2 with one core, fp32, synthetic packed chunks of the
|
|
241
|
+
trnsci#115 ladder's widths. The fp64 reference is computed on the host.
|
|
242
|
+
The host column is the aten route on 2 pinned threads, including the
|
|
243
|
+
unpack. The kernel column is the device time, synchronised, without the
|
|
244
|
+
download:
|
|
245
|
+
|
|
246
|
+
| n_ao, nocc, chunk naux | LNC | cold compile | kernel | host fp32 | max rel. error, kernel / host |
|
|
247
|
+
|---|---|---|---|---|---|
|
|
248
|
+
| 1658, 161, 384 | 2 | 128 s | 56 ms¹ | 6.1 s | 3.2e-7 / 6.6e-7 |
|
|
249
|
+
| 2522, 215, 168 | 2 | 195 s | 73 ms¹ | 7.9 s | 3.7e-7 / 6.0e-7 |
|
|
250
|
+
| 3508, 241, 87 | 2 | 162 s | 85 ms | 8.6 s | 3.9e-7 / 5.4e-7 |
|
|
251
|
+
| 3508, 241, 87 | 1 | 140 s | 94 ms | – | 3.9e-7 |
|
|
252
|
+
| 3508, 241, 48 (last chunk) | 2 | 56 s | 53 ms | 4.8 s | 4.4e-7 / 5.7e-7 |
|
|
253
|
+
|
|
254
|
+
¹ One chunk of up to 512 columns stays one chunk, so at LNC=2 the two
|
|
255
|
+
programs split the occupied index and both gather every eri block.
|
|
256
|
+
Splitting it in two takes 1658 to 35 ms at the same compile time, but
|
|
257
|
+
2522 to 54 ms at a 552 s compile, so the wider tiling is kept.
|
|
258
|
+
|
|
259
|
+
With the NEFF cache the first call at n_ao 3508 takes 26 s. A whole
|
|
260
|
+
alkane60 / cc-pVTZ-shaped pass through `AuxStream.ao_to_mo` (97 chunks,
|
|
261
|
+
8400 aux, 207 GB uploaded from host RAM) took 43 s warm. The trnsci#115
|
|
262
|
+
run did this pass on the host in 673 s, reading the store from NVMe.
|
|
263
|
+
|
|
229
264
|
- **Host inputs on native TorchNeuron (#56).** Under `"auto"`, with `eri`,
|
|
230
265
|
`C_occ` and `C_vir` all on the host, the inputs are uploaded (`eri` in the
|
|
231
266
|
layout given), the device route above runs, and the result is downloaded,
|
|
@@ -53,6 +53,7 @@ KERNEL_TAKES = {
|
|
|
53
53
|
"ao_to_mo_transform": (_CHEM,) * 3,
|
|
54
54
|
"ao_to_mo_trn2": (_CHEM,) * 3,
|
|
55
55
|
"ao_to_mo_trn2_packed": (_CHEM, _IDX, _CHEM, _CHEM),
|
|
56
|
+
"ao_to_mo_trn2_packed_blocked": ({F32}, _IDX, {F32}, {F32}),
|
|
56
57
|
"df_half_packed": ({F32}, _IDX, {F32}),
|
|
57
58
|
"df_exchange_syrk": ({F32},) * 2,
|
|
58
59
|
"df_coulomb_d": ({F32},) * 2,
|
|
@@ -80,6 +81,7 @@ _RAW_KERNELS = [
|
|
|
80
81
|
"mp2_energy_row_kernel",
|
|
81
82
|
"ao_to_mo_trn2_kernel",
|
|
82
83
|
"ao_to_mo_trn2_packed_kernel",
|
|
84
|
+
"ao_to_mo_trn2_packed_blocked_kernel",
|
|
83
85
|
"df_half_packed_kernel",
|
|
84
86
|
"df_exchange_syrk_kernel",
|
|
85
87
|
"df_coulomb_d_kernel",
|
|
@@ -274,6 +276,7 @@ def _tt(dt, g):
|
|
|
274
276
|
return trntensor.tt_decompose(_mk((4, 5, 6), dt, g), max_rank=2)
|
|
275
277
|
|
|
276
278
|
|
|
279
|
+
_BLOCKED = {"TRNTENSOR_AO_TO_MO": "trn2", "TRNTENSOR_AO_TO_MO_PACKED": "blocked"}
|
|
277
280
|
CALLS = [
|
|
278
281
|
("einsum-matmul", {}, _einsum("ij,jk->ik", [(6, 5), (5, 4)])),
|
|
279
282
|
("einsum-matmul-T", {}, _einsum("ji,jk->ik", [(5, 6), (5, 4)])),
|
|
@@ -289,6 +292,7 @@ CALLS = [
|
|
|
289
292
|
("mp2-legacy", {"TRNTENSOR_MP2_ENERGY": "legacy"}, _mp2),
|
|
290
293
|
("ao2mo-trn2", {"TRNTENSOR_AO_TO_MO": "trn2"}, _ao2mo(False)),
|
|
291
294
|
("ao2mo-trn2-packed", {"TRNTENSOR_AO_TO_MO": "trn2"}, _ao2mo(True)),
|
|
295
|
+
("ao2mo-trn2-packed-blocked", _BLOCKED, _ao2mo(True)),
|
|
292
296
|
("ao2mo-legacy", {"TRNTENSOR_AO_TO_MO": "legacy"}, _ao2mo(False)),
|
|
293
297
|
("df_exchange", {"TRNTENSOR_DF_EXCHANGE": "trn2"}, _dfk),
|
|
294
298
|
("df_coulomb", {"TRNTENSOR_DF_COULOMB": "trn2"}, _dfj),
|
|
@@ -301,7 +305,7 @@ CALLS = [
|
|
|
301
305
|
def _leaves(x):
|
|
302
306
|
if isinstance(x, torch.Tensor):
|
|
303
307
|
return [x]
|
|
304
|
-
if isinstance(x,
|
|
308
|
+
if isinstance(x, list | tuple):
|
|
305
309
|
return [t for v in x for t in _leaves(v)]
|
|
306
310
|
if isinstance(x, parallel.ShardedTensor):
|
|
307
311
|
return _leaves(list(x.chunks))
|
|
@@ -381,7 +385,15 @@ def test_require_nki_refuses_matmul_dtypes(guarded, monkeypatch, dtype, name):
|
|
|
381
385
|
@pytest.mark.parametrize("dtype", [torch.int32, torch.int64, torch.bool, torch.complex64])
|
|
382
386
|
@pytest.mark.parametrize(
|
|
383
387
|
"name",
|
|
384
|
-
[
|
|
388
|
+
[
|
|
389
|
+
"mp2-rows",
|
|
390
|
+
"mp2-legacy",
|
|
391
|
+
"ao2mo-trn2",
|
|
392
|
+
"ao2mo-trn2-packed",
|
|
393
|
+
"ao2mo-trn2-packed-blocked",
|
|
394
|
+
"df_exchange",
|
|
395
|
+
"df_coulomb",
|
|
396
|
+
],
|
|
385
397
|
)
|
|
386
398
|
def test_require_nki_refuses_chemistry_dtypes(guarded, monkeypatch, dtype, name):
|
|
387
399
|
_require_nki(monkeypatch)
|
|
@@ -397,7 +409,15 @@ def test_require_nki_refuses_chemistry_dtypes(guarded, monkeypatch, dtype, name)
|
|
|
397
409
|
@pytest.mark.parametrize("dtype", [torch.float16, torch.bfloat16])
|
|
398
410
|
@pytest.mark.parametrize(
|
|
399
411
|
"name",
|
|
400
|
-
[
|
|
412
|
+
[
|
|
413
|
+
"mp2-rows",
|
|
414
|
+
"mp2-legacy",
|
|
415
|
+
"ao2mo-trn2",
|
|
416
|
+
"ao2mo-trn2-packed",
|
|
417
|
+
"ao2mo-trn2-packed-blocked",
|
|
418
|
+
"df_exchange",
|
|
419
|
+
"df_coulomb",
|
|
420
|
+
],
|
|
401
421
|
)
|
|
402
422
|
def test_require_nki_chemistry_floats_run_the_kernel(guarded, monkeypatch, dtype, name):
|
|
403
423
|
"""float16 (and, on the fp32-only df_* kernels, bfloat16) chemistry operands
|
|
@@ -552,6 +572,7 @@ _MIXED_CALLS = [
|
|
|
552
572
|
("mp2-rows", {"TRNTENSOR_MP2_ENERGY": "rows"}, _mp2_mixed),
|
|
553
573
|
("ao2mo-trn2", {"TRNTENSOR_AO_TO_MO": "trn2"}, _ao2mo_mixed),
|
|
554
574
|
("ao2mo-trn2-packed", {"TRNTENSOR_AO_TO_MO": "trn2"}, lambda a, b: _ao2mo_mixed(a, b, True)),
|
|
575
|
+
("ao2mo-trn2-packed-blocked", _BLOCKED, lambda a, b: _ao2mo_mixed(a, b, True)),
|
|
555
576
|
("df_exchange", {"TRNTENSOR_DF_EXCHANGE": "trn2"}, _dfk_mixed),
|
|
556
577
|
("df_coulomb", {"TRNTENSOR_DF_COULOMB": "trn2"}, _dfj_mixed),
|
|
557
578
|
("einsum-matmul", {}, _matmul_mixed),
|
|
@@ -34,6 +34,7 @@ EXPECTED_OPS = {
|
|
|
34
34
|
"mp2_energy_row",
|
|
35
35
|
"ao_to_mo_trn2",
|
|
36
36
|
"ao_to_mo_trn2_packed",
|
|
37
|
+
"ao_to_mo_trn2_packed_blocked",
|
|
37
38
|
"df_half_packed",
|
|
38
39
|
"df_exchange_syrk",
|
|
39
40
|
"df_coulomb_d",
|
|
@@ -140,6 +141,7 @@ def native_host(monkeypatch):
|
|
|
140
141
|
"mp2_energy_row_kernel",
|
|
141
142
|
"ao_to_mo_trn2_kernel",
|
|
142
143
|
"ao_to_mo_trn2_packed_kernel",
|
|
144
|
+
"ao_to_mo_trn2_packed_blocked_kernel",
|
|
143
145
|
"df_half_packed_kernel",
|
|
144
146
|
"df_exchange_syrk_kernel",
|
|
145
147
|
"df_coulomb_d_kernel",
|
|
@@ -517,7 +519,7 @@ class TestEagerKernelTable:
|
|
|
517
519
|
if not isinstance(node, ast.FunctionDef):
|
|
518
520
|
continue
|
|
519
521
|
*pre, last = node.body
|
|
520
|
-
if not all(isinstance(n,
|
|
522
|
+
if not all(isinstance(n, ast.Import | ast.ImportFrom) for n in pre):
|
|
521
523
|
continue
|
|
522
524
|
call = getattr(last, "value", None)
|
|
523
525
|
if not (
|
|
@@ -736,6 +738,113 @@ class TestPublicApiOnNativePath:
|
|
|
736
738
|
)
|
|
737
739
|
assert [c[0] for c in native_host] == ["ao_to_mo_trn2_packed"]
|
|
738
740
|
|
|
741
|
+
@pytest.mark.parametrize(
|
|
742
|
+
"nbasis,naux,nocc,nvir",
|
|
743
|
+
[(40, 6, 5, 9), (130, 87, 131, 20), (200, 768, 7, 140), (60, 1031, 3, 50)],
|
|
744
|
+
)
|
|
745
|
+
def test_packed_blocked_ao_to_mo_replays_to_the_full_transform(
|
|
746
|
+
self, native_host, monkeypatch, nbasis, naux, nocc, nvir
|
|
747
|
+
):
|
|
748
|
+
"""The blocked op (#112) gets (npair·NPC, TP) rows with TP dividing naux
|
|
749
|
+
(no padding copy unless naux has no divisor in [64, 512]), NB/NUB index
|
|
750
|
+
blocks of NKB·NUB columns and padded C_occ/C_vir; replaying its gathers
|
|
751
|
+
on CPU rebuilds the square, so the result matches the full transform."""
|
|
752
|
+
from trntensor.nki._kernels import ao_to_mo_blocked_nub, ao_to_mo_blocked_tp
|
|
753
|
+
|
|
754
|
+
def blocked_op(eri_v, img, C_occ, C_vir):
|
|
755
|
+
npc, nvb, tp = img.shape[0], img.shape[1], eri_v.shape[1]
|
|
756
|
+
nbp, nocp = C_occ.shape
|
|
757
|
+
nub = nbp // nvb
|
|
758
|
+
assert img.dtype == torch.uint32 and img.shape == (npc, nvb, 128, nbp // 128 * nub)
|
|
759
|
+
assert nub == ao_to_mo_blocked_nub(tp, nocp)
|
|
760
|
+
assert nbp % 128 == 0 and C_vir.shape[1] % 128 == 0
|
|
761
|
+
if nocp > 128 and npc % 2:
|
|
762
|
+
assert nocp % 2 == 0 # the programs split the occupied index
|
|
763
|
+
idx = img.view(torch.int32).long()
|
|
764
|
+
full = torch.empty(nbp, nbp, npc * tp)
|
|
765
|
+
for c in range(npc):
|
|
766
|
+
for vb in range(nvb):
|
|
767
|
+
for k in range(nbp // 128):
|
|
768
|
+
blk = idx[c, vb, :, k * nub : (k + 1) * nub]
|
|
769
|
+
rows = eri_v[blk.T.flatten()].reshape(128, nub, tp)
|
|
770
|
+
full[
|
|
771
|
+
k * 128 : (k + 1) * 128,
|
|
772
|
+
vb * nub : (vb + 1) * nub,
|
|
773
|
+
c * tp : (c + 1) * tp,
|
|
774
|
+
] = rows
|
|
775
|
+
return torch.einsum("mi,na,mnP->iaP", C_occ, C_vir, full)
|
|
776
|
+
|
|
777
|
+
monkeypatch.setenv("TRNTENSOR_AO_TO_MO_PACKED", "blocked")
|
|
778
|
+
_install_ops(monkeypatch, native_host, ao_to_mo_trn2_packed_blocked=blocked_op)
|
|
779
|
+
g = torch.Generator().manual_seed(nbasis)
|
|
780
|
+
eri = torch.randn(nbasis, nbasis, naux, generator=g)
|
|
781
|
+
eri = 0.5 * (eri + eri.transpose(0, 1))
|
|
782
|
+
C_occ = torch.randn(nbasis, nocc, generator=g)
|
|
783
|
+
C_vir = torch.randn(nbasis, nvir, generator=g)
|
|
784
|
+
out = trntensor.ao_to_mo_transform(trntensor.pack_tril(eri), C_occ, C_vir)
|
|
785
|
+
assert out.shape == (nocc, nvir, naux)
|
|
786
|
+
want = torch.einsum("mi,na,mnP->iaP", C_occ.double(), C_vir.double(), eri.double())
|
|
787
|
+
assert (out.double() - want).abs().max() <= 1e-5 * want.abs().max()
|
|
788
|
+
assert [c[0] for c in native_host] == ["ao_to_mo_trn2_packed_blocked"]
|
|
789
|
+
tp = ao_to_mo_blocked_tp(naux)
|
|
790
|
+
assert tp == 0 or naux % tp == 0
|
|
791
|
+
|
|
792
|
+
def test_packed_variant_choice(self, monkeypatch):
|
|
793
|
+
"""'auto' keeps the resident kernel where it fits and takes the blocked
|
|
794
|
+
one beyond (n_ao 3508 needs 979 KiB resident, #112); fp32 only."""
|
|
795
|
+
f32 = torch.float32
|
|
796
|
+
assert dispatch._ao_to_mo_packed_variant(256, 32, 224, 512, f32) == "resident"
|
|
797
|
+
assert dispatch._ao_to_mo_packed_variant(3508, 241, 3267, 87, f32) == "blocked"
|
|
798
|
+
assert dispatch._ao_to_mo_packed_variant(1658, 161, 1497, 384, f32) == "blocked"
|
|
799
|
+
assert dispatch._ao_to_mo_packed_variant(3508, 241, 3267, 87, torch.bfloat16) is None
|
|
800
|
+
monkeypatch.setenv("TRNTENSOR_AO_TO_MO_PACKED", "blocked")
|
|
801
|
+
assert dispatch._ao_to_mo_packed_variant(256, 32, 224, 512, f32) == "blocked"
|
|
802
|
+
monkeypatch.setenv("TRNTENSOR_AO_TO_MO_PACKED", "resident")
|
|
803
|
+
assert dispatch._ao_to_mo_packed_variant(3508, 241, 3267, 87, f32) == "resident"
|
|
804
|
+
monkeypatch.setenv("TRNTENSOR_AO_TO_MO_PACKED", "nope")
|
|
805
|
+
with pytest.raises(ValueError, match="TRNTENSOR_AO_TO_MO_PACKED"):
|
|
806
|
+
dispatch._ao_to_mo_packed_variant(256, 32, 224, 512, f32)
|
|
807
|
+
|
|
808
|
+
def test_packed_route_takes_the_blocked_kernel_past_the_resident_budget(self, monkeypatch):
|
|
809
|
+
monkeypatch.setattr(dispatch, "_use_nki", lambda: True)
|
|
810
|
+
monkeypatch.setattr(dispatch, "_NEURON_DEVICE", "meta")
|
|
811
|
+
monkeypatch.setattr(dispatch, "_on_native_path", lambda: True)
|
|
812
|
+
monkeypatch.setattr(dispatch, "HAS_NKI", True)
|
|
813
|
+
n = 3508
|
|
814
|
+
big = torch.empty(n * (n + 1) // 2, 87, device="meta")
|
|
815
|
+
assert dispatch._ao_to_mo_route(big, 241, n - 241, n) == "trn2"
|
|
816
|
+
assert dispatch._ao_to_mo_route(big.to(torch.bfloat16), 241, n - 241, n) == "aten"
|
|
817
|
+
|
|
818
|
+
@pytest.mark.parametrize("nocc", [16, 161, 241, 600])
|
|
819
|
+
@pytest.mark.parametrize("naux", [48, 87, 96, 384, 512, 768, 6064])
|
|
820
|
+
def test_blocked_sbuf_does_not_grow_with_nao(self, nocc, naux):
|
|
821
|
+
from trntensor.nki._kernels import ao_to_mo_blocked_sbuf_bytes, ao_to_mo_blocked_tp
|
|
822
|
+
|
|
823
|
+
tp = ao_to_mo_blocked_tp(naux) or ao_to_mo_blocked_tp(-(-naux // 128) * 128)
|
|
824
|
+
sizes = {ao_to_mo_blocked_sbuf_bytes(nocc, tp, nb) for nb in (1024, 2048, 3584, 8192)}
|
|
825
|
+
assert len(sizes) == 1
|
|
826
|
+
assert sizes.pop() <= dispatch._AO_TO_MO_SBUF_BUDGET
|
|
827
|
+
|
|
828
|
+
@pytest.mark.parametrize(
|
|
829
|
+
"nbasis,nbp,naux,tp,nub", [(200, 256, 87, 87, 8), (256, 256, 768, 384, 2)]
|
|
830
|
+
)
|
|
831
|
+
def test_blocked_gather_images_address_the_packed_rows(self, nbasis, nbp, naux, tp, nub):
|
|
832
|
+
img = dispatch._tril_gather_images_blocked(nbasis, nbp, naux, tp, nub)
|
|
833
|
+
idx = img.view(torch.int32).long()
|
|
834
|
+
npc = naux // tp
|
|
835
|
+
rows = dispatch._tril_rows(nbasis)
|
|
836
|
+
assert idx.shape == (npc, nbp // nub, 128, nbp // 128 * nub)
|
|
837
|
+
for c in (0, npc - 1):
|
|
838
|
+
for vb in (0, nbp // nub - 1):
|
|
839
|
+
for k in range(nbp // 128):
|
|
840
|
+
got = idx[c, vb, :, k * nub : (k + 1) * nub].T.flatten().reshape(128, nub)
|
|
841
|
+
mu = torch.arange(k * 128, (k + 1) * 128)[:, None].expand(128, nub)
|
|
842
|
+
nu = torch.arange(vb * nub, (vb + 1) * nub)[None, :].expand(128, nub)
|
|
843
|
+
ok = (mu < nbasis) & (nu < nbasis)
|
|
844
|
+
want = rows[mu.clamp(max=nbasis - 1), nu.clamp(max=nbasis - 1)] * npc + c
|
|
845
|
+
assert torch.equal(got[ok], want[ok])
|
|
846
|
+
assert torch.equal(got[~ok], torch.full_like(got[~ok], c))
|
|
847
|
+
|
|
739
848
|
def test_packed_ao_to_mo_aten_route_unpacks(self, native_host, monkeypatch):
|
|
740
849
|
monkeypatch.setenv("TRNTENSOR_AO_TO_MO", "aten")
|
|
741
850
|
_install_ops(monkeypatch, native_host) # any kernel launch would KeyError
|
|
@@ -33,6 +33,17 @@ def _u32(*shape):
|
|
|
33
33
|
return np.zeros(shape, dtype=np.uint32)
|
|
34
34
|
|
|
35
35
|
|
|
36
|
+
def _blocked(nb, nocc, nvir, naux, rows=1000):
|
|
37
|
+
"""Inputs for ``ao_to_mo_trn2_packed_blocked_kernel`` as the dispatcher pads them."""
|
|
38
|
+
from trntensor.nki._kernels import ao_to_mo_blocked_nub, ao_to_mo_blocked_tp
|
|
39
|
+
|
|
40
|
+
tp = ao_to_mo_blocked_tp(naux)
|
|
41
|
+
nub = ao_to_mo_blocked_nub(tp, nocc)
|
|
42
|
+
npc = naux // tp
|
|
43
|
+
img = _u32(npc, nb // nub, 128, nb // 128 * nub)
|
|
44
|
+
return (_r(rows * npc, tp), img, _r(nb, nocc), _r(nb, nvir)), {}
|
|
45
|
+
|
|
46
|
+
|
|
36
47
|
# kernel name -> [(case label, () -> (args, kwargs))]
|
|
37
48
|
KERNEL_INPUTS = {
|
|
38
49
|
"matmul_kernel": [
|
|
@@ -111,6 +122,16 @@ KERNEL_INPUTS = {
|
|
|
111
122
|
),
|
|
112
123
|
),
|
|
113
124
|
],
|
|
125
|
+
# Blocked packed AO→MO (#112): (rows, TP), (NPC, NB/NUB, 128, NKB·NUB)
|
|
126
|
+
# indices, C_occ, C_vir. Row counts are cut (shapes only).
|
|
127
|
+
"ao_to_mo_trn2_packed_blocked_kernel": [
|
|
128
|
+
("nb256_nocc32_nvir256_naux512", lambda: _blocked(256, 32, 256, 512)),
|
|
129
|
+
("nb384_nocc200_nvir256_naux87_occsplit", lambda: _blocked(384, 200, 256, 87)),
|
|
130
|
+
("nb1280_nocc242_nvir384_naux96_kbtail", lambda: _blocked(1280, 242, 384, 96)),
|
|
131
|
+
("nb256_nocc129_nvir640_naux768", lambda: _blocked(256, 129, 640, 768)),
|
|
132
|
+
("nb256_nocc600_nvir128_naux128_passes", lambda: _blocked(256, 600, 128, 128)),
|
|
133
|
+
("nb256_nocc16_nvir128_naux48", lambda: _blocked(256, 16, 128, 48)),
|
|
134
|
+
],
|
|
114
135
|
"df_half_packed_kernel": [
|
|
115
136
|
# (packed (npair·NPC, TP) rows, (NPC, NVG, 128, NKB·NVC) uint32 indices, C_occ)
|
|
116
137
|
(
|
|
@@ -104,6 +104,7 @@ class TestNativeDispatch:
|
|
|
104
104
|
"mp2_energy_row",
|
|
105
105
|
"ao_to_mo_trn2",
|
|
106
106
|
"ao_to_mo_trn2_packed",
|
|
107
|
+
"ao_to_mo_trn2_packed_blocked",
|
|
107
108
|
"df_half_packed",
|
|
108
109
|
"df_exchange_syrk",
|
|
109
110
|
"df_coulomb_d",
|
|
@@ -327,6 +328,46 @@ class TestNativeTrn2Kernels:
|
|
|
327
328
|
ref = torch.einsum("na,inP->iaP", C_vir, torch.einsum("mi,mnP->inP", C_occ, eri))
|
|
328
329
|
torch.testing.assert_close(out.cpu(), ref, atol=1e-3, rtol=1e-4)
|
|
329
330
|
|
|
331
|
+
@pytest.mark.parametrize(
|
|
332
|
+
"nbasis,nocc,nvir,naux",
|
|
333
|
+
[
|
|
334
|
+
(128, 8, 120, 128),
|
|
335
|
+
(200, 20, 180, 300),
|
|
336
|
+
(300, 200, 100, 87), # odd chunk count: the programs split nocc
|
|
337
|
+
(256, 129, 127, 768), # two 384-wide chunks
|
|
338
|
+
(1100, 40, 300, 96), # 9 μ tiles: blocks of 8 + 1
|
|
339
|
+
(256, 600, 100, 128), # five occupied blocks: two passes
|
|
340
|
+
(130, 7, 60, 1031), # no divisor in [64, 512]: naux padded
|
|
341
|
+
],
|
|
342
|
+
)
|
|
343
|
+
def test_ao_to_mo_trn2_packed_blocked_matches_reference(
|
|
344
|
+
self, monkeypatch, nbasis, nocc, nvir, naux
|
|
345
|
+
):
|
|
346
|
+
"""#112: the blocked packed kernel against an fp64 reference, and bit for
|
|
347
|
+
bit against the resident kernel (same per-element PSUM chains)."""
|
|
348
|
+
import trntensor
|
|
349
|
+
from trntensor.nki import dispatch
|
|
350
|
+
|
|
351
|
+
g = torch.Generator().manual_seed(nbasis + naux)
|
|
352
|
+
eri = torch.randn(nbasis, nbasis, naux, generator=g) * 0.1
|
|
353
|
+
eri = 0.5 * (eri + eri.transpose(0, 1))
|
|
354
|
+
C_occ = torch.randn(nbasis, nocc, generator=g)
|
|
355
|
+
C_vir = torch.randn(nbasis, nvir, generator=g)
|
|
356
|
+
co, cv = _dev(C_occ, C_vir)
|
|
357
|
+
pk = trntensor.pack_tril(eri).to("neuron")
|
|
358
|
+
monkeypatch.setenv("TRNTENSOR_AO_TO_MO_PACKED", "blocked")
|
|
359
|
+
out = dispatch._nki_ao_to_mo_trn2_packed(pk, co, cv)
|
|
360
|
+
assert out.device.type == "neuron" and out.shape == (nocc, nvir, naux)
|
|
361
|
+
ref = torch.einsum(
|
|
362
|
+
"na,inP->iaP", C_vir.double(), torch.einsum("mi,mnP->inP", C_occ.double(), eri.double())
|
|
363
|
+
)
|
|
364
|
+
err = (out.cpu().double() - ref).abs().max() / ref.abs().max()
|
|
365
|
+
assert err < 1e-5, err
|
|
366
|
+
if naux % 128 == 0:
|
|
367
|
+
monkeypatch.setenv("TRNTENSOR_AO_TO_MO_PACKED", "resident")
|
|
368
|
+
res = dispatch._nki_ao_to_mo_trn2_packed(pk, co, cv)
|
|
369
|
+
assert torch.equal(out.cpu(), res.cpu())
|
|
370
|
+
|
|
330
371
|
@pytest.mark.parametrize(
|
|
331
372
|
"nbasis,nocc,naux,group,tp",
|
|
332
373
|
[
|
|
@@ -89,6 +89,96 @@ def ao_to_mo_trn2_nub(tp: int) -> int:
|
|
|
89
89
|
return nub
|
|
90
90
|
|
|
91
91
|
|
|
92
|
+
# ── Blocked packed AO→MO (#112) ─────────────────────────────────────────────
|
|
93
|
+
#
|
|
94
|
+
# ``ao_to_mo_trn2_packed_kernel`` keeps C_occ, C_vir, one chunk's gather
|
|
95
|
+
# indices and a whole μ column of the stage-1 eri block in SBUF, all of which
|
|
96
|
+
# grow with n_ao: 979 KiB per partition at n_ao = 3508 against 176 KiB. The
|
|
97
|
+
# blocked kernel streams every operand over blocks of ``KB`` μ (stage 1) or ν
|
|
98
|
+
# (stage 2) tiles and keeps the partial sums in PSUM across the blocks, so its
|
|
99
|
+
# SBUF footprint depends on the tile sizes only (and on nocc through one
|
|
100
|
+
# C_occ block), not on n_ao.
|
|
101
|
+
|
|
102
|
+
AO2MO_BLOCKED_PSUM_BANKS = 4 # PSUM accumulators live across a block loop
|
|
103
|
+
AO2MO_BLOCKED_E_ELEMS = 8192 # eri block elements per partition (32 KiB fp32)
|
|
104
|
+
AO2MO_BLOCKED_KB_MAX = 8
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def ao_to_mo_blocked_num(tp: int) -> int:
|
|
108
|
+
"""ν per stage-1 matmul: the largest power of two ≤ 8 with ``NUM·tp ≤ 512``."""
|
|
109
|
+
num = 1
|
|
110
|
+
while num < 8 and 2 * num * tp <= TILE_N:
|
|
111
|
+
num *= 2
|
|
112
|
+
return num
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
def ao_to_mo_blocked_nub(tp: int, nocc: int) -> int:
|
|
116
|
+
"""ν per stage-1 gather for the blocked kernel: ``NUM·NUG``, with ``NUG``
|
|
117
|
+
the ν matmul groups per occupied block, so that the occupied blocks of one
|
|
118
|
+
pass hold at most ``AO2MO_BLOCKED_PSUM_BANKS`` accumulators (a power of
|
|
119
|
+
two, so it divides 128 and every padded nbasis)."""
|
|
120
|
+
nob = max(1, -(-nocc // TILE_M))
|
|
121
|
+
nug = 1
|
|
122
|
+
while nug * 2 * nob <= AO2MO_BLOCKED_PSUM_BANKS:
|
|
123
|
+
nug *= 2
|
|
124
|
+
return ao_to_mo_blocked_num(tp) * nug
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
def ao_to_mo_blocked_kb(nub: int, tp: int, nkb: int) -> int:
|
|
128
|
+
"""128-row tiles per contraction block: ``KB·NUB·tp ≤ AO2MO_BLOCKED_E_ELEMS``."""
|
|
129
|
+
return max(1, min(nkb, AO2MO_BLOCKED_KB_MAX, AO2MO_BLOCKED_E_ELEMS // (nub * tp)))
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
def ao_to_mo_blocked_avb(nav: int) -> int:
|
|
133
|
+
"""Virtual 128-tiles per stage-2 pass: the largest divisor of ``nav`` ≤ the bank budget."""
|
|
134
|
+
for d in range(AO2MO_BLOCKED_PSUM_BANKS, 0, -1):
|
|
135
|
+
if nav % d == 0:
|
|
136
|
+
return d
|
|
137
|
+
return 1
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
def ao_to_mo_blocked_tp(naux: int) -> int:
|
|
141
|
+
"""Aux chunk width for the blocked kernel, or 0 when ``naux`` needs padding.
|
|
142
|
+
|
|
143
|
+
A divisor of ``naux`` in ``[64, 512]`` (or ``naux`` itself up to 512), so
|
|
144
|
+
the packed tensor is viewed as ``(npair·NPC, TP)`` rows without a padding
|
|
145
|
+
copy (at n_ao = 3508 a 128-wide padded copy of one 87-column chunk is
|
|
146
|
+
3.1 GB). The widest wins, which keeps the traced instruction count, and
|
|
147
|
+
with it the compile time, down; among divisors of a wider naux an even
|
|
148
|
+
chunk count (the programs split the chunks at LNC=2) comes first.
|
|
149
|
+
|
|
150
|
+
Splitting a chunk of ≤ 512 in two for an even count was measured and
|
|
151
|
+
not taken (Trn2, LNC=2, kernel time / cold compile): n_ao 2522 / nocc
|
|
152
|
+
215 / naux 168 ran 54 ms / 552 s as 2 × 84 against 73 ms / 195 s;
|
|
153
|
+
1658 / 161 / 384 35 ms / 128 s as 2 × 192 against 56 ms / 128 s;
|
|
154
|
+
970 / 161 / 512 18 ms / 67 s as 2 × 256 against 27 ms / 39 s.
|
|
155
|
+
"""
|
|
156
|
+
if naux <= TILE_N:
|
|
157
|
+
return naux
|
|
158
|
+
cands = [tp for tp in range(TILE_N, 63, -1) if naux % tp == 0]
|
|
159
|
+
if not cands:
|
|
160
|
+
return 0
|
|
161
|
+
even = [tp for tp in cands if (naux // tp) % 2 == 0]
|
|
162
|
+
return (even or cands)[0]
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
def ao_to_mo_blocked_sbuf_bytes(nocc: int, tp: int, nb: int = 1 << 30) -> int:
|
|
166
|
+
"""Upper bound of the blocked kernel's SBUF bytes per partition (fp32, before
|
|
167
|
+
the compiler's double buffering): independent of n_ao (``nb`` only caps
|
|
168
|
+
``KB``)."""
|
|
169
|
+
nub = ao_to_mo_blocked_nub(tp, nocc)
|
|
170
|
+
kb = ao_to_mo_blocked_kb(nub, tp, max(1, nb // TILE_K))
|
|
171
|
+
num = ao_to_mo_blocked_num(tp)
|
|
172
|
+
nug = nub // num
|
|
173
|
+
obp = max(1, AO2MO_BLOCKED_PSUM_BANKS // nug)
|
|
174
|
+
pw = min(nocc, obp * TILE_M)
|
|
175
|
+
ni = max(1, min(nocc, TILE_N // tp))
|
|
176
|
+
avb = AO2MO_BLOCKED_PSUM_BANKS
|
|
177
|
+
stage1 = kb * nub + kb * nub * tp + kb * pw + obp * nub * tp
|
|
178
|
+
stage2 = kb * ni * tp + kb * avb * TILE_M + avb * ni * tp
|
|
179
|
+
return 4 * (stage1 + stage2)
|
|
180
|
+
|
|
181
|
+
|
|
92
182
|
if HAS_NKI:
|
|
93
183
|
|
|
94
184
|
@nki.jit
|
|
@@ -728,6 +818,169 @@ if HAS_NKI:
|
|
|
728
818
|
NUB = ao_to_mo_trn2_nub(TP)
|
|
729
819
|
return _ao_to_mo_trn2_body(eri_v, img, C_occ, C_vir, NB, NPC * TP, TP, NUB)
|
|
730
820
|
|
|
821
|
+
@nki.jit
|
|
822
|
+
def ao_to_mo_trn2_packed_blocked_kernel(eri_v, img, C_occ, C_vir):
|
|
823
|
+
""":func:`ao_to_mo_trn2_packed_kernel` with SBUF independent of n_ao (#112).
|
|
824
|
+
|
|
825
|
+
``eri_v``: the packed lower triangle viewed as ``(npair·NPC, TP)``
|
|
826
|
+
rows (row ``NPC·pair + c`` = aux chunk ``c`` of packed row ``pair``);
|
|
827
|
+
``TP`` need not be a 128 multiple. ``img``: ``(NPC, NB/NUB, 128,
|
|
828
|
+
NKB·NUB)`` uint32 gather indices from
|
|
829
|
+
``dispatch._tril_gather_images_blocked``: for chunk ``c`` and ν block
|
|
830
|
+
``vb`` the ``(128, NUB)`` block at columns ``k·NUB`` gathers
|
|
831
|
+
``eri[k·128 + p, vb·NUB + u, chunk c]`` into ``(p, u)``. ``C_occ``
|
|
832
|
+
``(NB, NOCC)``, ``C_vir`` ``(NB, NVIR)``, NB and NVIR multiples of 128.
|
|
833
|
+
|
|
834
|
+
Same two GEMM stages as the resident kernels, but nothing of size
|
|
835
|
+
n_ao stays in SBUF:
|
|
836
|
+
|
|
837
|
+
1. per ν block of ``NUB`` and pass of occupied blocks, the μ tiles are
|
|
838
|
+
taken ``KB`` at a time: their gather indices, eri rows (one batched
|
|
839
|
+
indirect DMA per tile) and ``C_occ`` rows are loaded, and every
|
|
840
|
+
matmul accumulates into PSUM tiles that live across the μ blocks.
|
|
841
|
+
Each μ sum is the full PSUM chain, in the resident kernel's order.
|
|
842
|
+
2. per group of ``NI`` occupied orbitals and pass of ``AVB`` virtual
|
|
843
|
+
tiles, ``X`` and ``C_vir`` are streamed over ν blocks of ``KB``
|
|
844
|
+
tiles into ``AVB`` PSUM accumulators. ``X`` is re-read once per
|
|
845
|
+
pass (``NVIR / (128·AVB)`` times) from kernel scratch HBM.
|
|
846
|
+
|
|
847
|
+
The aux chunks (or, with an odd chunk count and NOCC > 128, the
|
|
848
|
+
occupied index; NOCC even) are split across the programs as before.
|
|
849
|
+
"""
|
|
850
|
+
NB, NOCC = C_occ.shape
|
|
851
|
+
_, NVIR = C_vir.shape
|
|
852
|
+
NPC, NVB = img.shape[0], img.shape[1]
|
|
853
|
+
TP = eri_v.shape[1]
|
|
854
|
+
NAUX = NPC * TP
|
|
855
|
+
TILE = 128
|
|
856
|
+
NKB = NB // TILE
|
|
857
|
+
NUB = NB // NVB
|
|
858
|
+
NAV = NVIR // TILE
|
|
859
|
+
n_prog, pid, axis = _programs(NPC, NOCC if NOCC > TILE else 1)
|
|
860
|
+
if axis == 1:
|
|
861
|
+
NPC_per, c_base = NPC, 0
|
|
862
|
+
OCC_per = NOCC // n_prog
|
|
863
|
+
o_base = pid * OCC_per
|
|
864
|
+
else:
|
|
865
|
+
NPC_per = NPC // n_prog
|
|
866
|
+
c_base = pid * NPC_per
|
|
867
|
+
OCC_per, o_base = NOCC, 0
|
|
868
|
+
NUM = min(NUB, ao_to_mo_blocked_num(TP))
|
|
869
|
+
NUG = NUB // NUM
|
|
870
|
+
KB = ao_to_mo_blocked_kb(NUB, TP, NKB)
|
|
871
|
+
kblocks = ao_to_mo_trn2_groups(NKB, KB)
|
|
872
|
+
obs = ao_to_mo_trn2_occ_blocks(OCC_per)
|
|
873
|
+
OBP = max(1, AO2MO_BLOCKED_PSUM_BANKS // NUG)
|
|
874
|
+
passes = [obs[i : i + OBP] for i in range(0, len(obs), OBP)]
|
|
875
|
+
NI = max(1, min(OCC_per, TILE_N // TP))
|
|
876
|
+
NGF = OCC_per // NI
|
|
877
|
+
NIT = OCC_per - NGF * NI
|
|
878
|
+
AVB = ao_to_mo_blocked_avb(NAV)
|
|
879
|
+
AVW = AVB * TILE
|
|
880
|
+
|
|
881
|
+
B_out = nl.ndarray((NOCC, NVIR, NAUX), dtype=eri_v.dtype, buffer=nl.shared_hbm)
|
|
882
|
+
x_hbm = nl.ndarray((NOCC, NB, NAUX), dtype=eri_v.dtype, buffer=nl.shared_hbm)
|
|
883
|
+
|
|
884
|
+
def stage1(c, p_off, v0, vb, pas):
|
|
885
|
+
po0 = o_base + pas[0][0]
|
|
886
|
+
pw = pas[-1][0] + pas[-1][1] - pas[0][0]
|
|
887
|
+
accs = [
|
|
888
|
+
[nl.zeros((osz, NUM, TP), dtype=nl.float32, buffer=nl.psum) for _ in range(NUG)]
|
|
889
|
+
for _, osz in pas
|
|
890
|
+
]
|
|
891
|
+
for k0, kbs in kblocks:
|
|
892
|
+
idx_sb = nl.ndarray((TILE, kbs * NUB), dtype=nl.uint32, buffer=nl.sbuf)
|
|
893
|
+
nisa.dma_copy(dst=idx_sb, src=img[c, vb, 0:TILE, k0 * NUB : (k0 + kbs) * NUB])
|
|
894
|
+
e_blk = nl.ndarray((TILE, kbs, NUB, TP), dtype=eri_v.dtype, buffer=nl.sbuf)
|
|
895
|
+
for k in nl.affine_range(kbs):
|
|
896
|
+
nisa.dma_copy(
|
|
897
|
+
dst=e_blk[0:TILE, k, 0:NUB, 0:TP],
|
|
898
|
+
src=eri_v.ap(
|
|
899
|
+
pattern=[[TP, TILE * NUB], [1, TP]],
|
|
900
|
+
vector_offset=idx_sb[0:TILE, k * NUB : (k + 1) * NUB],
|
|
901
|
+
indirect_dim=0,
|
|
902
|
+
),
|
|
903
|
+
)
|
|
904
|
+
co_blk = nl.ndarray((TILE, kbs, pw), dtype=C_occ.dtype, buffer=nl.sbuf)
|
|
905
|
+
nisa.dma_copy(
|
|
906
|
+
dst=co_blk,
|
|
907
|
+
src=C_occ[k0 * TILE : k0 * TILE + 1, po0 : po0 + 1].ap(
|
|
908
|
+
pattern=[[NOCC, TILE], [TILE * NOCC, kbs], [1, pw]]
|
|
909
|
+
),
|
|
910
|
+
)
|
|
911
|
+
for j, (b0, osz) in enumerate(pas):
|
|
912
|
+
ob = b0 - pas[0][0]
|
|
913
|
+
for ug in range(NUG):
|
|
914
|
+
for k in nl.affine_range(kbs):
|
|
915
|
+
nisa.nc_matmul(
|
|
916
|
+
dst=accs[j][ug],
|
|
917
|
+
stationary=co_blk[0:TILE, k, ob : ob + osz],
|
|
918
|
+
moving=e_blk[0:TILE, k, ug * NUM : (ug + 1) * NUM, 0:TP],
|
|
919
|
+
accumulate=True,
|
|
920
|
+
)
|
|
921
|
+
for j, (b0, osz) in enumerate(pas):
|
|
922
|
+
x_sb = nl.ndarray((osz, NUB, TP), dtype=eri_v.dtype, buffer=nl.sbuf)
|
|
923
|
+
for ug in range(NUG):
|
|
924
|
+
nisa.tensor_copy(
|
|
925
|
+
src=accs[j][ug], dst=x_sb[0:osz, ug * NUM : (ug + 1) * NUM, 0:TP]
|
|
926
|
+
)
|
|
927
|
+
o0 = o_base + b0
|
|
928
|
+
nl.store(x_hbm[o0 : o0 + osz, v0 : v0 + NUB, p_off : p_off + TP], value=x_sb)
|
|
929
|
+
|
|
930
|
+
def stage2(p_off, i0, ni):
|
|
931
|
+
for ab in nl.affine_range(NAV // AVB):
|
|
932
|
+
a0 = ab * AVW
|
|
933
|
+
accs = [
|
|
934
|
+
nl.zeros((TILE, ni, TP), dtype=nl.float32, buffer=nl.psum) for _ in range(AVB)
|
|
935
|
+
]
|
|
936
|
+
for k0, kbs in kblocks:
|
|
937
|
+
x_g = nl.ndarray((TILE, kbs, ni, TP), dtype=eri_v.dtype, buffer=nl.sbuf)
|
|
938
|
+
for k in nl.affine_range(kbs):
|
|
939
|
+
r0 = (k0 + k) * TILE
|
|
940
|
+
nisa.dma_copy(
|
|
941
|
+
dst=x_g[0:TILE, k, 0:ni, 0:TP],
|
|
942
|
+
src=x_hbm[i0 : i0 + 1, r0 : r0 + 1, p_off : p_off + 1].ap(
|
|
943
|
+
pattern=[[NAUX, TILE], [NB * NAUX, ni], [1, TP]]
|
|
944
|
+
),
|
|
945
|
+
)
|
|
946
|
+
cv_blk = nl.ndarray((TILE, kbs, AVW), dtype=C_vir.dtype, buffer=nl.sbuf)
|
|
947
|
+
nisa.dma_copy(
|
|
948
|
+
dst=cv_blk,
|
|
949
|
+
src=C_vir[k0 * TILE : k0 * TILE + 1, a0 : a0 + 1].ap(
|
|
950
|
+
pattern=[[NVIR, TILE], [TILE * NVIR, kbs], [1, AVW]]
|
|
951
|
+
),
|
|
952
|
+
)
|
|
953
|
+
for j in range(AVB):
|
|
954
|
+
for k in nl.affine_range(kbs):
|
|
955
|
+
nisa.nc_matmul(
|
|
956
|
+
dst=accs[j],
|
|
957
|
+
stationary=cv_blk[0:TILE, k, j * TILE : (j + 1) * TILE],
|
|
958
|
+
moving=x_g[0:TILE, k, 0:ni, 0:TP],
|
|
959
|
+
accumulate=True,
|
|
960
|
+
)
|
|
961
|
+
for j in range(AVB):
|
|
962
|
+
b_sb = nl.ndarray((TILE, ni, TP), dtype=eri_v.dtype, buffer=nl.sbuf)
|
|
963
|
+
nisa.tensor_copy(src=accs[j], dst=b_sb)
|
|
964
|
+
a1 = a0 + j * TILE
|
|
965
|
+
nisa.dma_copy(
|
|
966
|
+
dst=B_out[i0 : i0 + 1, a1 : a1 + 1, p_off : p_off + 1].ap(
|
|
967
|
+
pattern=[[NAUX, TILE], [NVIR * NAUX, ni], [1, TP]]
|
|
968
|
+
),
|
|
969
|
+
src=b_sb,
|
|
970
|
+
)
|
|
971
|
+
|
|
972
|
+
for pc in nl.affine_range(NPC_per):
|
|
973
|
+
c = c_base + pc
|
|
974
|
+
p_off = c * TP
|
|
975
|
+
for vb in nl.affine_range(NVB):
|
|
976
|
+
for pas in passes:
|
|
977
|
+
stage1(c, p_off, vb * NUB, vb, pas)
|
|
978
|
+
for g in nl.affine_range(NGF):
|
|
979
|
+
stage2(p_off, o_base + g * NI, NI)
|
|
980
|
+
if NIT:
|
|
981
|
+
stage2(p_off, o_base + NGF * NI, NIT)
|
|
982
|
+
return B_out
|
|
983
|
+
|
|
731
984
|
|
|
732
985
|
# ---------------------------------------------------------------------------
|
|
733
986
|
# DF exchange (K) from packed integrals (#60)
|
|
@@ -116,6 +116,19 @@ def ao_to_mo_trn2_packed(
|
|
|
116
116
|
return _k.ao_to_mo_trn2_packed_kernel(eri_v, img, C_occ, C_vir)
|
|
117
117
|
|
|
118
118
|
|
|
119
|
+
@nki_op(
|
|
120
|
+
"trntensor::ao_to_mo_trn2_packed_blocked",
|
|
121
|
+
mutates_args={},
|
|
122
|
+
fake_fn=lambda eri_v, img, C_occ, C_vir: eri_v.new_empty(
|
|
123
|
+
(C_occ.shape[1], C_vir.shape[1], img.shape[0] * eri_v.shape[1])
|
|
124
|
+
),
|
|
125
|
+
)
|
|
126
|
+
def ao_to_mo_trn2_packed_blocked(
|
|
127
|
+
eri_v: torch.Tensor, img: torch.Tensor, C_occ: torch.Tensor, C_vir: torch.Tensor
|
|
128
|
+
) -> torch.Tensor:
|
|
129
|
+
return _k.ao_to_mo_trn2_packed_blocked_kernel(eri_v, img, C_occ, C_vir)
|
|
130
|
+
|
|
131
|
+
|
|
119
132
|
def _df_half_rows(eri_v, img, tp, tail):
|
|
120
133
|
tp = tp or eri_v.shape[1]
|
|
121
134
|
return (img.shape[0] - 1) * tp + tail if tail else img.shape[0] * tp
|
|
@@ -182,6 +195,7 @@ KERNELS = {
|
|
|
182
195
|
"mp2_energy_row": (_k, "mp2_energy_row_kernel"),
|
|
183
196
|
"ao_to_mo_trn2": (_k, "ao_to_mo_trn2_kernel"),
|
|
184
197
|
"ao_to_mo_trn2_packed": (_k, "ao_to_mo_trn2_packed_kernel"),
|
|
198
|
+
"ao_to_mo_trn2_packed_blocked": (_k, "ao_to_mo_trn2_packed_blocked_kernel"),
|
|
185
199
|
"df_half_packed": (_k, "df_half_packed_kernel"),
|
|
186
200
|
"df_exchange_syrk": (_k, "df_exchange_syrk_kernel"),
|
|
187
201
|
"df_coulomb_d": (_k, "df_coulomb_d_kernel"),
|
|
@@ -197,6 +211,7 @@ OPS = {
|
|
|
197
211
|
"mp2_energy_row": mp2_energy_row,
|
|
198
212
|
"ao_to_mo_trn2": ao_to_mo_trn2,
|
|
199
213
|
"ao_to_mo_trn2_packed": ao_to_mo_trn2_packed,
|
|
214
|
+
"ao_to_mo_trn2_packed_blocked": ao_to_mo_trn2_packed_blocked,
|
|
200
215
|
"df_half_packed": df_half_packed,
|
|
201
216
|
"df_exchange_syrk": df_exchange_syrk,
|
|
202
217
|
"df_coulomb_d": df_coulomb_d,
|
|
@@ -34,6 +34,9 @@ from ._kernels import (
|
|
|
34
34
|
TILE_K,
|
|
35
35
|
TILE_M,
|
|
36
36
|
TILE_N,
|
|
37
|
+
ao_to_mo_blocked_nub,
|
|
38
|
+
ao_to_mo_blocked_sbuf_bytes,
|
|
39
|
+
ao_to_mo_blocked_tp,
|
|
37
40
|
ao_to_mo_trn2_nub,
|
|
38
41
|
ao_to_mo_trn2_tiling,
|
|
39
42
|
df_group_chunks,
|
|
@@ -694,6 +697,8 @@ KERNEL_OPERAND_DTYPES: dict[str, tuple[frozenset, ...]] = {
|
|
|
694
697
|
"ao_to_mo_transform": (_CHEM_DTYPES,) * 3,
|
|
695
698
|
"ao_to_mo_trn2": (_CHEM_DTYPES,) * 3,
|
|
696
699
|
"ao_to_mo_trn2_packed": (_CHEM_DTYPES, _INDEX_DTYPES, _CHEM_DTYPES, _CHEM_DTYPES),
|
|
700
|
+
# fp32 only: the blocked kernel (#112) was validated on device in fp32.
|
|
701
|
+
"ao_to_mo_trn2_packed_blocked": (_F32_DTYPES, _INDEX_DTYPES, _F32_DTYPES, _F32_DTYPES),
|
|
697
702
|
# The df_* kernels run fp32 only: bf16 was never validated on device, and
|
|
698
703
|
# the df_coulomb host tail mixed a bf16 B with the kernel's fp32 d.
|
|
699
704
|
"df_half_packed": (_F32_DTYPES, _INDEX_DTYPES, _F32_DTYPES),
|
|
@@ -2001,6 +2006,9 @@ def _ao_to_mo_route(eri: torch.Tensor, nocc: int, nvir: int, nbasis: int | None
|
|
|
2001
2006
|
_ao_to_mo_trn2_sbuf_bytes(nb, nocc, nvir, naux, eri.element_size(), packed)
|
|
2002
2007
|
<= _AO_TO_MO_SBUF_BUDGET
|
|
2003
2008
|
)
|
|
2009
|
+
if not fits and packed:
|
|
2010
|
+
# The blocked kernel's SBUF does not grow with n_ao (#112).
|
|
2011
|
+
fits = _ao_to_mo_packed_variant(nb, nocc, nvir, naux, eri.dtype) == "blocked"
|
|
2004
2012
|
if not fits:
|
|
2005
2013
|
return "aten"
|
|
2006
2014
|
if _nki_forced():
|
|
@@ -2015,6 +2023,43 @@ def _ao_to_mo_route(eri: torch.Tensor, nocc: int, nvir: int, nbasis: int | None
|
|
|
2015
2023
|
return "trn2" if big and eri.device.type == _NEURON_DEVICE else "aten"
|
|
2016
2024
|
|
|
2017
2025
|
|
|
2026
|
+
_AO_TO_MO_PACKED_CHOICES = ("auto", "resident", "blocked")
|
|
2027
|
+
|
|
2028
|
+
|
|
2029
|
+
def _ao_to_mo_packed_variant(
|
|
2030
|
+
nbasis: int, nocc: int, nvir: int, naux: int, dtype: torch.dtype
|
|
2031
|
+
) -> str | None:
|
|
2032
|
+
"""Which packed Trn2 AO→MO kernel runs: 'resident' (``ao_to_mo_trn2_packed_kernel``),
|
|
2033
|
+
'blocked' (``ao_to_mo_trn2_packed_blocked_kernel``, #112) or None (neither).
|
|
2034
|
+
|
|
2035
|
+
``"auto"`` keeps the resident kernel wherever its SBUF working set fits
|
|
2036
|
+
(it was measured there, #54/#80, and is up to ~2x faster at mid sizes:
|
|
2037
|
+
14 against 18-27 ms at n_ao 970 / nocc 161 / naux 512) and takes the
|
|
2038
|
+
blocked one beyond, from n_ao ≈ 1100-1300 at nocc ≈ n_ao/10, where
|
|
2039
|
+
C_vir, the gather indices and the stage-1 eri column pass the budget.
|
|
2040
|
+
The blocked kernel runs fp32 only.
|
|
2041
|
+
``TRNTENSOR_AO_TO_MO_PACKED=auto|resident|blocked`` overrides.
|
|
2042
|
+
"""
|
|
2043
|
+
choice = os.environ.get("TRNTENSOR_AO_TO_MO_PACKED", "auto").strip().lower()
|
|
2044
|
+
if choice not in _AO_TO_MO_PACKED_CHOICES:
|
|
2045
|
+
raise ValueError(
|
|
2046
|
+
f"TRNTENSOR_AO_TO_MO_PACKED must be one of {_AO_TO_MO_PACKED_CHOICES}; got {choice!r}"
|
|
2047
|
+
)
|
|
2048
|
+
itemsize = torch.empty((), dtype=dtype).element_size()
|
|
2049
|
+
resident = (
|
|
2050
|
+
_ao_to_mo_trn2_sbuf_bytes(nbasis, nocc, nvir, naux, itemsize, True) <= _AO_TO_MO_SBUF_BUDGET
|
|
2051
|
+
)
|
|
2052
|
+
tp = ao_to_mo_blocked_tp(naux) or ao_to_mo_blocked_tp(_round_up(naux, TILE_K))
|
|
2053
|
+
blocked = (
|
|
2054
|
+
dtype == torch.float32
|
|
2055
|
+
and ao_to_mo_blocked_sbuf_bytes(nocc, tp, _round_up(nbasis, TILE_K))
|
|
2056
|
+
<= _AO_TO_MO_SBUF_BUDGET
|
|
2057
|
+
)
|
|
2058
|
+
if choice == "resident" or (choice == "auto" and resident):
|
|
2059
|
+
return "resident"
|
|
2060
|
+
return "blocked" if blocked else None
|
|
2061
|
+
|
|
2062
|
+
|
|
2018
2063
|
def _ao_to_mo_pad_occ(C_occ: torch.Tensor, naux_pad: int, packed: bool) -> torch.Tensor:
|
|
2019
2064
|
"""``C_occ`` with a zero orbital appended when the kernel splits the
|
|
2020
2065
|
occupied index across programs and ``nocc`` is odd.
|
|
@@ -2122,6 +2167,47 @@ def _tril_gather_images(nbasis: int, nbp: int, naux: int, tp: int, device=None)
|
|
|
2122
2167
|
_gather_image_cache: dict[tuple, torch.Tensor] = {}
|
|
2123
2168
|
|
|
2124
2169
|
|
|
2170
|
+
def _tril_gather_images_blocked(
|
|
2171
|
+
nbasis: int, nbp: int, naux: int, tp: int, nub: int, device=None
|
|
2172
|
+
) -> torch.Tensor:
|
|
2173
|
+
"""Gather indices for ``ao_to_mo_trn2_packed_blocked_kernel``:
|
|
2174
|
+
``(NPC, nbp/NUB, 128, NKB·NUB)`` uint32 (#112).
|
|
2175
|
+
|
|
2176
|
+
The same ``(128, NUB)`` index blocks as :func:`_tril_gather_images`
|
|
2177
|
+
(rows ``NPC·pair + c`` of the ``(npair·NPC, tp)`` view, read column-major
|
|
2178
|
+
and written row-major), regrouped so that for chunk ``c`` and ν block
|
|
2179
|
+
``vb`` the blocks of consecutive μ tiles ``k`` are contiguous (columns
|
|
2180
|
+
``k·NUB``): one small DMA brings a whole μ block's indices. Cached per
|
|
2181
|
+
shape and device (``4·NPC·nbp²`` bytes).
|
|
2182
|
+
"""
|
|
2183
|
+
key = ("blk", nbasis, nbp, naux, tp, nub, str(device))
|
|
2184
|
+
hit = _gather_image_cache.get(key)
|
|
2185
|
+
if hit is not None:
|
|
2186
|
+
return hit
|
|
2187
|
+
npc = naux // tp
|
|
2188
|
+
nkb = nbp // TILE_K
|
|
2189
|
+
nvb = nbp // nub
|
|
2190
|
+
if (nbasis * (nbasis + 1) // 2) * npc >= 2**31:
|
|
2191
|
+
raise ValueError("packed eri too large for 32-bit gather indices")
|
|
2192
|
+
rows = torch.zeros(nbp, nbp, dtype=torch.int64)
|
|
2193
|
+
rows[:nbasis, :nbasis] = _tril_rows(nbasis)
|
|
2194
|
+
q = torch.arange(TILE_K)[:, None]
|
|
2195
|
+
m = torch.arange(nub)[None, :]
|
|
2196
|
+
j = m * TILE_K + q # (128, NUB)
|
|
2197
|
+
dp, du = j // nub, j % nub
|
|
2198
|
+
k = torch.arange(nkb)[None, :, None, None]
|
|
2199
|
+
v0 = (torch.arange(nvb) * nub)[:, None, None, None]
|
|
2200
|
+
mu = k * TILE_K + dp[None, None] # (NVB, NKB, 128, NUB)
|
|
2201
|
+
nu = v0 + du[None, None]
|
|
2202
|
+
base = rows[mu, nu].permute(0, 2, 1, 3).reshape(nvb, TILE_K, nkb * nub)
|
|
2203
|
+
hit = (base[None] * npc + torch.arange(npc)[:, None, None, None]).to(torch.int32)
|
|
2204
|
+
hit = hit.view(torch.uint32).contiguous()
|
|
2205
|
+
if device is not None:
|
|
2206
|
+
hit = hit.to(device)
|
|
2207
|
+
_gather_image_cache[key] = hit
|
|
2208
|
+
return hit
|
|
2209
|
+
|
|
2210
|
+
|
|
2125
2211
|
def _packed_tp(naux: int) -> int:
|
|
2126
2212
|
return ao_to_mo_trn2_tiling(naux, packed=True)[0]
|
|
2127
2213
|
|
|
@@ -2142,6 +2228,8 @@ def _nki_ao_to_mo_trn2_packed(
|
|
|
2142
2228
|
npair, naux = eri_p.shape
|
|
2143
2229
|
nbasis, nocc = C_occ.shape
|
|
2144
2230
|
nvir = C_vir.shape[1]
|
|
2231
|
+
if _ao_to_mo_packed_variant(nbasis, nocc, nvir, naux, eri_p.dtype) == "blocked":
|
|
2232
|
+
return _nki_ao_to_mo_trn2_packed_blocked(eri_p, C_occ, C_vir)
|
|
2145
2233
|
nbp, nvp, nxp = _round_up(nbasis, TILE_K), _round_up(nvir, TILE_M), _round_up(naux, TILE_K)
|
|
2146
2234
|
if nxp != naux:
|
|
2147
2235
|
ep = torch.zeros(npair, nxp, dtype=eri_p.dtype, device=eri_p.device)
|
|
@@ -2155,8 +2243,8 @@ def _nki_ao_to_mo_trn2_packed(
|
|
|
2155
2243
|
cv = torch.zeros(nbp, nvp, dtype=C_vir.dtype, device=C_vir.device)
|
|
2156
2244
|
cv[:nbasis, :nvir] = C_vir
|
|
2157
2245
|
C_vir = cv
|
|
2158
|
-
tp = _packed_tp(nxp)
|
|
2159
2246
|
dev = eri_p.device if eri_p.device.type == _NEURON_DEVICE else None
|
|
2247
|
+
tp = _packed_tp(nxp)
|
|
2160
2248
|
img = _tril_gather_images(nbasis, nbp, nxp, tp, dev)
|
|
2161
2249
|
C_occ = _ao_to_mo_pad_occ(C_occ, nxp, packed=True)
|
|
2162
2250
|
out = _launch(
|
|
@@ -2172,6 +2260,55 @@ def _nki_ao_to_mo_trn2_packed(
|
|
|
2172
2260
|
return out[:, :nvir, :naux] if (nvp, nxp) != (nvir, naux) else out
|
|
2173
2261
|
|
|
2174
2262
|
|
|
2263
|
+
def _nki_ao_to_mo_trn2_packed_blocked(
|
|
2264
|
+
eri_p: torch.Tensor,
|
|
2265
|
+
C_occ: torch.Tensor,
|
|
2266
|
+
C_vir: torch.Tensor,
|
|
2267
|
+
) -> torch.Tensor:
|
|
2268
|
+
"""Dispatch ``ao_to_mo_trn2_packed_blocked_kernel`` (#112).
|
|
2269
|
+
|
|
2270
|
+
The aux chunk width is a divisor of naux (:func:`ao_to_mo_blocked_tp`),
|
|
2271
|
+
so the packed tensor goes in without a padding copy unless naux has no
|
|
2272
|
+
divisor in ``[64, 512]``. nbasis and nvir pad through ``C_occ``/``C_vir``
|
|
2273
|
+
and the gather indices, and an odd nocc above 128 gets a zero orbital
|
|
2274
|
+
when the programs split the occupied index (odd chunk count).
|
|
2275
|
+
"""
|
|
2276
|
+
npair, naux = eri_p.shape
|
|
2277
|
+
nbasis, nocc = C_occ.shape
|
|
2278
|
+
nvir = C_vir.shape[1]
|
|
2279
|
+
nbp, nvp = _round_up(nbasis, TILE_K), _round_up(nvir, TILE_M)
|
|
2280
|
+
tp, nxp = ao_to_mo_blocked_tp(naux), naux
|
|
2281
|
+
if not tp:
|
|
2282
|
+
nxp = _round_up(naux, TILE_K)
|
|
2283
|
+
tp = ao_to_mo_blocked_tp(nxp)
|
|
2284
|
+
ep = torch.zeros(npair, nxp, dtype=eri_p.dtype, device=eri_p.device)
|
|
2285
|
+
ep[:, :naux] = eri_p
|
|
2286
|
+
eri_p = ep
|
|
2287
|
+
nocp = nocc + 1 if nocc > TILE_M and nocc % 2 and (nxp // tp) % 2 else nocc
|
|
2288
|
+
if (nbp, nocp) != (nbasis, nocc):
|
|
2289
|
+
co = torch.zeros(nbp, nocp, dtype=C_occ.dtype, device=C_occ.device)
|
|
2290
|
+
co[:nbasis, :nocc] = C_occ
|
|
2291
|
+
C_occ = co
|
|
2292
|
+
if (nbp, nvp) != (nbasis, nvir):
|
|
2293
|
+
cv = torch.zeros(nbp, nvp, dtype=C_vir.dtype, device=C_vir.device)
|
|
2294
|
+
cv[:nbasis, :nvir] = C_vir
|
|
2295
|
+
C_vir = cv
|
|
2296
|
+
dev = eri_p.device if eri_p.device.type == _NEURON_DEVICE else None
|
|
2297
|
+
nub = ao_to_mo_blocked_nub(tp, nocp)
|
|
2298
|
+
img = _tril_gather_images_blocked(nbasis, nbp, nxp, tp, nub, dev)
|
|
2299
|
+
out = _launch(
|
|
2300
|
+
None,
|
|
2301
|
+
"ao_to_mo_trn2_packed_blocked",
|
|
2302
|
+
eri_p.contiguous().view(npair * (nxp // tp), tp),
|
|
2303
|
+
img,
|
|
2304
|
+
C_occ.contiguous(),
|
|
2305
|
+
C_vir.contiguous(),
|
|
2306
|
+
)
|
|
2307
|
+
if out.shape[0] != nocc:
|
|
2308
|
+
out = out[:nocc]
|
|
2309
|
+
return out[:, :nvir, :naux] if (nvp, nxp) != (nvir, naux) else out
|
|
2310
|
+
|
|
2311
|
+
|
|
2175
2312
|
def _df_gather_images(
|
|
2176
2313
|
nbasis: int, nbp: int, naux: int, tp: int, nub: int, nvc: int, device=None
|
|
2177
2314
|
) -> torch.Tensor:
|
|
@@ -303,7 +303,13 @@ def ao_to_mo_transform(
|
|
|
303
303
|
``naux == npair``). On the native path the packed tensor feeds
|
|
304
304
|
``ao_to_mo_trn2_packed_kernel``, which gathers its rows in place: the
|
|
305
305
|
full square never exists in device memory, and only half the bytes
|
|
306
|
-
cross the host link.
|
|
306
|
+
cross the host link. Where that kernel's SBUF working set (which grows
|
|
307
|
+
with n_ao: 979 KiB per partition at n_ao = 3508) passes the budget,
|
|
308
|
+
fp32 integrals take ``ao_to_mo_trn2_packed_blocked_kernel`` instead,
|
|
309
|
+
which streams every operand over blocks of μ/ν tiles and keeps an SBUF
|
|
310
|
+
footprint independent of n_ao (#112);
|
|
311
|
+
``TRNTENSOR_AO_TO_MO_PACKED=auto|resident|blocked`` overrides. The
|
|
312
|
+
aten and legacy routes unpack it with one
|
|
307
313
|
``index_select`` (exact for fp32; float64 operands on the neuron device
|
|
308
314
|
already run on the host under ``"auto"``).
|
|
309
315
|
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|