trntensor 0.25.0__tar.gz → 0.25.2__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.25.0 → trntensor-0.25.2}/.github/workflows/ci.yml +2 -2
- {trntensor-0.25.0 → trntensor-0.25.2}/.pre-commit-config.yaml +4 -2
- {trntensor-0.25.0 → trntensor-0.25.2}/CHANGELOG.md +48 -0
- {trntensor-0.25.0 → trntensor-0.25.2}/CONTRIBUTING.md +11 -0
- {trntensor-0.25.0/trntensor.egg-info → trntensor-0.25.2}/PKG-INFO +11 -4
- {trntensor-0.25.0 → trntensor-0.25.2}/README.md +9 -2
- {trntensor-0.25.0 → trntensor-0.25.2}/pyproject.toml +3 -3
- {trntensor-0.25.0 → trntensor-0.25.2}/tests/test_einsum.py +85 -0
- {trntensor-0.25.0 → trntensor-0.25.2}/tests/test_native_dispatch.py +1 -1
- trntensor-0.25.2/tests/test_neuron_int_exact.py +70 -0
- trntensor-0.25.2/tests/test_neuron_span.py +119 -0
- trntensor-0.25.2/tests/test_neuron_span_device.py +99 -0
- {trntensor-0.25.0 → trntensor-0.25.2}/trntensor/__init__.py +1 -1
- {trntensor-0.25.0 → trntensor-0.25.2}/trntensor/einsum.py +62 -3
- {trntensor-0.25.0 → trntensor-0.25.2}/trntensor/nki/_kernels.py +2 -1
- {trntensor-0.25.0 → trntensor-0.25.2}/trntensor/nki/dispatch.py +227 -15
- {trntensor-0.25.0 → trntensor-0.25.2}/trntensor/quantum.py +7 -2
- {trntensor-0.25.0 → trntensor-0.25.2/trntensor.egg-info}/PKG-INFO +11 -4
- {trntensor-0.25.0 → trntensor-0.25.2}/trntensor.egg-info/SOURCES.txt +3 -0
- {trntensor-0.25.0 → trntensor-0.25.2}/trntensor.egg-info/requires.txt +1 -1
- {trntensor-0.25.0 → trntensor-0.25.2}/.github/workflows/notify-umbrella.yml +0 -0
- {trntensor-0.25.0 → trntensor-0.25.2}/.github/workflows/publish.yml +0 -0
- {trntensor-0.25.0 → trntensor-0.25.2}/.gitignore +0 -0
- {trntensor-0.25.0 → trntensor-0.25.2}/CLAUDE.md +0 -0
- {trntensor-0.25.0 → trntensor-0.25.2}/CODE_OF_CONDUCT.md +0 -0
- {trntensor-0.25.0 → trntensor-0.25.2}/LICENSE +0 -0
- {trntensor-0.25.0 → trntensor-0.25.2}/benchmarks/__init__.py +0 -0
- {trntensor-0.25.0 → trntensor-0.25.2}/benchmarks/bench_einsum.py +0 -0
- {trntensor-0.25.0 → trntensor-0.25.2}/docs/api/decompose.md +0 -0
- {trntensor-0.25.0 → trntensor-0.25.2}/docs/api/einsum.md +0 -0
- {trntensor-0.25.0 → trntensor-0.25.2}/docs/api/index.md +0 -0
- {trntensor-0.25.0 → trntensor-0.25.2}/docs/api/nki.md +0 -0
- {trntensor-0.25.0 → trntensor-0.25.2}/docs/api/plan.md +0 -0
- {trntensor-0.25.0 → trntensor-0.25.2}/docs/api/quantum.md +0 -0
- {trntensor-0.25.0 → trntensor-0.25.2}/docs/api/stream.md +0 -0
- {trntensor-0.25.0 → trntensor-0.25.2}/docs/architecture.md +0 -0
- {trntensor-0.25.0 → trntensor-0.25.2}/docs/aws_setup.md +0 -0
- {trntensor-0.25.0 → trntensor-0.25.2}/docs/benchmarks.md +0 -0
- {trntensor-0.25.0 → trntensor-0.25.2}/docs/contraction_patterns.md +0 -0
- {trntensor-0.25.0 → trntensor-0.25.2}/docs/design/matmul_dispatch_findings.md +0 -0
- {trntensor-0.25.0 → trntensor-0.25.2}/docs/design/matmul_kernel_profile.md +0 -0
- {trntensor-0.25.0 → trntensor-0.25.2}/docs/developing_kernels.md +0 -0
- {trntensor-0.25.0 → trntensor-0.25.2}/docs/index.md +0 -0
- {trntensor-0.25.0 → trntensor-0.25.2}/docs/installation.md +0 -0
- {trntensor-0.25.0 → trntensor-0.25.2}/docs/quickstart.md +0 -0
- {trntensor-0.25.0 → trntensor-0.25.2}/docs/tutorial_df_mp2.md +0 -0
- {trntensor-0.25.0 → trntensor-0.25.2}/examples/df_mp2_einsum.py +0 -0
- {trntensor-0.25.0 → trntensor-0.25.2}/infra/terraform/README.md +0 -0
- {trntensor-0.25.0 → trntensor-0.25.2}/infra/terraform/main.tf +0 -0
- {trntensor-0.25.0 → trntensor-0.25.2}/mkdocs.yml +0 -0
- {trntensor-0.25.0 → trntensor-0.25.2}/scripts/autotune_dispatch.py +0 -0
- {trntensor-0.25.0 → trntensor-0.25.2}/scripts/bench_ao2mo_nocc.py +0 -0
- {trntensor-0.25.0 → trntensor-0.25.2}/scripts/bench_eager_handle.py +0 -0
- {trntensor-0.25.0 → trntensor-0.25.2}/scripts/bench_native_einsum.py +0 -0
- {trntensor-0.25.0 → trntensor-0.25.2}/scripts/bench_native_kernels.py +0 -0
- {trntensor-0.25.0 → trntensor-0.25.2}/scripts/bench_to_md.py +0 -0
- {trntensor-0.25.0 → trntensor-0.25.2}/scripts/run_benchmarks.sh +0 -0
- {trntensor-0.25.0 → trntensor-0.25.2}/scripts/run_neuron_profile.sh +0 -0
- {trntensor-0.25.0 → trntensor-0.25.2}/scripts/run_neuron_tests.sh +0 -0
- {trntensor-0.25.0 → trntensor-0.25.2}/scripts/run_reduce_parallel_multirank.py +0 -0
- {trntensor-0.25.0 → trntensor-0.25.2}/scripts/run_simulator_tests.sh +0 -0
- {trntensor-0.25.0 → trntensor-0.25.2}/setup.cfg +0 -0
- {trntensor-0.25.0 → trntensor-0.25.2}/tests/conftest.py +0 -0
- {trntensor-0.25.0 → trntensor-0.25.2}/tests/test_conformance.py +0 -0
- {trntensor-0.25.0 → trntensor-0.25.2}/tests/test_decompose.py +0 -0
- {trntensor-0.25.0 → trntensor-0.25.2}/tests/test_host_output.py +0 -0
- {trntensor-0.25.0 → trntensor-0.25.2}/tests/test_kernel_dtype_guard.py +0 -0
- {trntensor-0.25.0 → trntensor-0.25.2}/tests/test_nki.py +0 -0
- {trntensor-0.25.0 → trntensor-0.25.2}/tests/test_nki_compile.py +0 -0
- {trntensor-0.25.0 → trntensor-0.25.2}/tests/test_nki_kernels.py +0 -0
- {trntensor-0.25.0 → trntensor-0.25.2}/tests/test_nki_native.py +0 -0
- {trntensor-0.25.0 → trntensor-0.25.2}/tests/test_nki_sim.py +0 -0
- {trntensor-0.25.0 → trntensor-0.25.2}/tests/test_nki_tail_audit.py +0 -0
- {trntensor-0.25.0 → trntensor-0.25.2}/tests/test_parallel.py +0 -0
- {trntensor-0.25.0 → trntensor-0.25.2}/tests/test_plan.py +0 -0
- {trntensor-0.25.0 → trntensor-0.25.2}/tests/test_quantum.py +0 -0
- {trntensor-0.25.0 → trntensor-0.25.2}/tests/test_sr_runtime.py +0 -0
- {trntensor-0.25.0 → trntensor-0.25.2}/tests/test_stream.py +0 -0
- {trntensor-0.25.0 → trntensor-0.25.2}/tests/test_trace_cache_safety.py +0 -0
- {trntensor-0.25.0 → trntensor-0.25.2}/trntensor/decompose.py +0 -0
- {trntensor-0.25.0 → trntensor-0.25.2}/trntensor/nki/__init__.py +0 -0
- {trntensor-0.25.0 → trntensor-0.25.2}/trntensor/nki/_native.py +0 -0
- {trntensor-0.25.0 → trntensor-0.25.2}/trntensor/nki/collectives.py +0 -0
- {trntensor-0.25.0 → trntensor-0.25.2}/trntensor/parallel.py +0 -0
- {trntensor-0.25.0 → trntensor-0.25.2}/trntensor/plan.py +0 -0
- {trntensor-0.25.0 → trntensor-0.25.2}/trntensor/py.typed +0 -0
- {trntensor-0.25.0 → trntensor-0.25.2}/trntensor/stream.py +0 -0
- {trntensor-0.25.0 → trntensor-0.25.2}/trntensor.egg-info/dependency_links.txt +0 -0
- {trntensor-0.25.0 → trntensor-0.25.2}/trntensor.egg-info/top_level.txt +0 -0
- {trntensor-0.25.0 → trntensor-0.25.2}/uv.lock +0 -0
|
@@ -17,8 +17,8 @@ jobs:
|
|
|
17
17
|
steps:
|
|
18
18
|
- uses: actions/checkout@v6
|
|
19
19
|
- uses: astral-sh/setup-uv@v6
|
|
20
|
-
- run: uvx ruff check .
|
|
21
|
-
- run: uvx ruff format --check .
|
|
20
|
+
- run: uvx ruff@0.17.3 check .
|
|
21
|
+
- run: uvx ruff@0.17.3 format --check .
|
|
22
22
|
|
|
23
23
|
test:
|
|
24
24
|
runs-on: ubuntu-latest
|
|
@@ -3,9 +3,11 @@
|
|
|
3
3
|
# CI re-checks the same rules in the lint job — pre-commit keeps CI green.
|
|
4
4
|
|
|
5
5
|
repos:
|
|
6
|
+
# Keep rev in lockstep with the ruff pin in .github/workflows/ci.yml
|
|
7
|
+
# (and the [dev] extra in pyproject.toml) so local and CI agree.
|
|
6
8
|
- repo: https://github.com/astral-sh/ruff-pre-commit
|
|
7
|
-
rev: v0.
|
|
9
|
+
rev: v0.17.3
|
|
8
10
|
hooks:
|
|
9
|
-
- id: ruff
|
|
11
|
+
- id: ruff-check
|
|
10
12
|
args: [--fix]
|
|
11
13
|
- id: ruff-format
|
|
@@ -7,6 +7,54 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [0.25.2] — 2026-11-10
|
|
11
|
+
|
|
12
|
+
Validated on Trn2 (trn2.3xlarge, Neuron SDK 2.33.0, native TorchNeuron) — see trnsci/trnsci#35.
|
|
13
|
+
|
|
14
|
+
Headline: integer einsum on the neuron device is exact above 2^24, and int32/int64 out= copies are exact (silent, #132)
|
|
15
|
+
|
|
16
|
+
### Fixed
|
|
17
|
+
|
|
18
|
+
- **Integer einsum on the neuron device: `precision="fast"` rounded int32
|
|
19
|
+
above 2^24, and `out=` rounded transposed int32 / int64 results** (silent,
|
|
20
|
+
#132, trnsci/trnsci#135, aws-neuron/aws-neuron-sdk#1466). Integer and bool
|
|
21
|
+
contractions with an operand on a device without exact integer arithmetic
|
|
22
|
+
now contract on the host in every precision mode (the #90 route, which
|
|
23
|
+
covered `"kahan"` only), and the result is made contiguous before it is
|
|
24
|
+
uploaded (an upload of a transposed int32 host view is rounded too).
|
|
25
|
+
`out=` copies int32 through `uint32` views and int64 through `float64`
|
|
26
|
+
views, which the device moves bit-exactly. On Trn2, `einsum("ij->ji", x,
|
|
27
|
+
out=buf)` on an int32 (64, 96) tensor had 5928 of 6144 entries wrong, and
|
|
28
|
+
`einsum("ij,jk->ik")` of `[[2^24 + 1, 2], [3, 4]]` with I gave 16777216.
|
|
29
|
+
|
|
30
|
+
## [0.25.1] — 2026-11-07
|
|
31
|
+
|
|
32
|
+
Validated on Trn2 (trn2.3xlarge, Neuron SDK 2.33.0, native TorchNeuron) — see trnsci/trnsci#35.
|
|
33
|
+
|
|
34
|
+
Headline: device matmuls with 32 MiB or longer rows (an operand's 128 partitions spanning 4 GiB) are no longer silently wrong (#123, aws-neuron-sdk#1457).
|
|
35
|
+
|
|
36
|
+
### Fixed
|
|
37
|
+
|
|
38
|
+
- **Device matmuls with 32 MiB or longer rows were silently wrong (#123,
|
|
39
|
+
trnblas#112, aws-neuron-sdk#1457).** On the native TorchNeuron path (Trn2,
|
|
40
|
+
SDK 2.33), a device matmul returns wrong values with no error (or aborts
|
|
41
|
+
with NRT status 1203) once an operand's 128 DMA partitions span 4 GiB or
|
|
42
|
+
more, i.e. `min(rows, 128) * row_bytes >= 2**32`, and that includes a
|
|
43
|
+
contiguous left operand. Example: fp32 `(96, 128) @ (128, 8,389,632)` gave
|
|
44
|
+
rel err 0.8. Affected: the contiguous-LHS `ao_to_mo_transform` aten route
|
|
45
|
+
of #98 (0.23.0–0.25.0) when `nbasis * naux >= 8,388,608` (fp32), and
|
|
46
|
+
device `einsum` matmul / bmm plans (`nki_matmul`, `nki_batched_matmul`)
|
|
47
|
+
with such operands (0.17.0–0.25.0). These products are now cut into
|
|
48
|
+
contiguous row blocks of the long operand, each block spanning at most 2 GiB:
|
|
49
|
+
K is split (partial sums), or M / N (concatenated results). No operand is
|
|
50
|
+
copied. Splitting K changes the fp32 summation order of the affected products
|
|
51
|
+
only. Every other product is the same aten call as before (bitwise
|
|
52
|
+
unchanged). Standard DF ratios (naux about 3 × nbasis) on one core do not
|
|
53
|
+
reach it. The `_ATEN_TRANSPOSED_LHS_MAX_BYTES` comment said exactly 2**32
|
|
54
|
+
bytes was correct. That was a lucky measurement (the operand sat above an
|
|
55
|
+
identical copy), and the comment is corrected; the guard already started
|
|
56
|
+
at 2**32.
|
|
57
|
+
|
|
10
58
|
## [0.25.0] — 2026-11-04
|
|
11
59
|
|
|
12
60
|
Validated on Trn2 (trn2.3xlarge, Neuron SDK 2.33.0, native TorchNeuron) — see trnsci/trnsci#35.
|
|
@@ -57,6 +57,17 @@ Before opening a PR:
|
|
|
57
57
|
- [ ] If you added a public symbol, the project's `docs/api.md` lists it
|
|
58
58
|
- [ ] PR description says *what* changed and *why*, not just *what*
|
|
59
59
|
|
|
60
|
+
## Releasing
|
|
61
|
+
|
|
62
|
+
`.github/workflows/publish.yml` publishes to PyPI (trusted publishing) when a GitHub Release is published. A tag containing `-` (`vX.Y.Z-rc1`) goes to TestPyPI.
|
|
63
|
+
|
|
64
|
+
- [ ] Release PR (`release: vX.Y.Z`): bump `version` in `pyproject.toml` and `__version__` in `trntensor/__init__.py`, and move the `[Unreleased]` CHANGELOG entries under `## [X.Y.Z] - YYYY-MM-DD`.
|
|
65
|
+
- [ ] After the merge, run `gh release create vX.Y.Z --target <merge sha>`, then confirm the publish run and the new version on PyPI.
|
|
66
|
+
- [ ] **Update umbrella docs/extras** in one [trnsci/trnsci](https://github.com/trnsci/trnsci) PR:
|
|
67
|
+
- if users should require this release (always for a fix to silent wrong results), bump `trntensor` in the umbrella `pyproject.toml` `[tensor]` and `[all]` extras, and cut a `trnsci` meta-package release;
|
|
68
|
+
- update the `[[described:trntensor=X.Y.Z]]` rows in `docs/roadmap.md` and `docs/nki_validation_status.md`, with the [#35](https://github.com/trnsci/trnsci/issues/35) Trn2 validation link;
|
|
69
|
+
- check that `python scripts/check_docs_drift.py` exits 0. The weekly docs-drift workflow opens a `docs-stale` issue for anything missed.
|
|
70
|
+
|
|
60
71
|
## Issues
|
|
61
72
|
|
|
62
73
|
Label each issue one of: `bug`, `feature`, `question`, `docs`, `infra`. Bugs include repro steps and expected-vs-actual output. Features describe the use case.
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: trntensor
|
|
3
|
-
Version: 0.25.
|
|
3
|
+
Version: 0.25.2
|
|
4
4
|
Summary: Tensor contractions for AWS Trainium via NKI
|
|
5
5
|
Author-email: Scott Friedman <scttfrdmn@gmail.com>
|
|
6
6
|
License-Expression: Apache-2.0
|
|
@@ -29,7 +29,7 @@ Provides-Extra: dev
|
|
|
29
29
|
Requires-Dist: pytest<9,>=7.0; extra == "dev"
|
|
30
30
|
Requires-Dist: pytest-benchmark>=4.0; extra == "dev"
|
|
31
31
|
Requires-Dist: pytest-cov>=4.0; extra == "dev"
|
|
32
|
-
Requires-Dist: ruff
|
|
32
|
+
Requires-Dist: ruff==0.17.3; extra == "dev"
|
|
33
33
|
Requires-Dist: pre-commit>=3.7; extra == "dev"
|
|
34
34
|
Requires-Dist: scipy>=1.11; extra == "dev"
|
|
35
35
|
Requires-Dist: mkdocs>=1.5; extra == "dev"
|
|
@@ -169,7 +169,7 @@ core, factors = trntensor.tucker_decompose(tensor, ranks=(5, 5, 5))
|
|
|
169
169
|
|
|
170
170
|
## Known issues in earlier releases
|
|
171
171
|
|
|
172
|
-
Silent wrong-result bugs fixed in the 2026-10-31 to 2026-11-
|
|
172
|
+
Silent wrong-result bugs fixed in the 2026-10-31 to 2026-11-04 releases. Details are in the [CHANGELOG](CHANGELOG.md).
|
|
173
173
|
|
|
174
174
|
| Bug | Affected | Fixed in |
|
|
175
175
|
|-----|----------|----------|
|
|
@@ -180,7 +180,14 @@ Silent wrong-result bugs fixed in the 2026-10-31 to 2026-11-02 releases. Details
|
|
|
180
180
|
| `einsum(precision="dd")` on float32/float64 was less accurate than `"fast"` (CPU emulation, #84) | 0.17.0–0.22.0 | 0.23.0 |
|
|
181
181
|
| `multi_einsum` returned float32 results as float64 in mixed-dtype batches (#75) | 0.17.0–0.22.0 | 0.23.0 |
|
|
182
182
|
| NKI chemistry kernels returned inf for float64 operands, and some kernel routes changed the result dtype (#87) | `df_*` 0.21.0–0.22.0; `ao_to_mo_transform` every release with its kernels | 0.23.0 |
|
|
183
|
-
| `ao_to_mo_transform` aten route on the TorchNeuron device was wrong once `eri` exceeded 4 GiB (#97;
|
|
183
|
+
| `ao_to_mo_transform` aten route on the TorchNeuron device was wrong once `eri` exceeded 4 GiB (#97; the LNC=1 gather case is the 0.24.0 row below) | through 0.22.0 | 0.23.0 |
|
|
184
|
+
| `unpack_tril` / `pack_tril` (and the packed-`eri` aten / legacy routes of `ao_to_mo_transform`) on the neuron device at LNC=1 returned wrong data, at times NaN, once one gathered output reached 4 GiB (154 M mismatched elements at 4.99e9 bytes) (#97) | 0.20.0–0.23.0 | 0.24.0 |
|
|
185
|
+
| Complex `tucker_decompose` on the neuron device used `Uᵀ`, not `Uᴴ`: device matmul ignores the lazy conj bit of `.mH` (#89) | 0.23.0 | 0.24.0 |
|
|
186
|
+
| float64 on the neuron device rounded to fp32 in `gather` (device `torch.cat`) and in `pack_tril` / `unpack_tril` / `pack_density` / `unpack_packed` (device `index_select` / fancy indexing), rel. err ~4e-8 (#92) | through 0.23.0 | 0.24.0 |
|
|
187
|
+
| `multi_einsum` under `"auto"` pinned shared host float64 / complex128 operands to the device, where device aten computed them at fp32 precision (#92) | through 0.23.0 | 0.24.0 |
|
|
188
|
+
| Integer, bool, complex and mixed-dtype operands of `einsum` / `multi_einsum` reached the fp32/bf16/fp16 matmul kernels under `set_backend("nki")`, or under `"auto"` above the size thresholds (trnsci/trnsci#35) | through 0.23.0 | 0.24.0 |
|
|
189
|
+
| `einsum(..., out=)` never wrote `out`, so the documented `einsum(..., beta=1.0, out=acc)` accumulation left `acc` unchanged (#77) | 0.3.0–0.23.0 | 0.24.0 |
|
|
190
|
+
| `cp_reconstruct` returned float32 for bf16 / fp16 factors (#78) | 0.1.1–0.23.0 | 0.24.0 |
|
|
184
191
|
|
|
185
192
|
## Related Projects
|
|
186
193
|
|
|
@@ -131,7 +131,7 @@ core, factors = trntensor.tucker_decompose(tensor, ranks=(5, 5, 5))
|
|
|
131
131
|
|
|
132
132
|
## Known issues in earlier releases
|
|
133
133
|
|
|
134
|
-
Silent wrong-result bugs fixed in the 2026-10-31 to 2026-11-
|
|
134
|
+
Silent wrong-result bugs fixed in the 2026-10-31 to 2026-11-04 releases. Details are in the [CHANGELOG](CHANGELOG.md).
|
|
135
135
|
|
|
136
136
|
| Bug | Affected | Fixed in |
|
|
137
137
|
|-----|----------|----------|
|
|
@@ -142,7 +142,14 @@ Silent wrong-result bugs fixed in the 2026-10-31 to 2026-11-02 releases. Details
|
|
|
142
142
|
| `einsum(precision="dd")` on float32/float64 was less accurate than `"fast"` (CPU emulation, #84) | 0.17.0–0.22.0 | 0.23.0 |
|
|
143
143
|
| `multi_einsum` returned float32 results as float64 in mixed-dtype batches (#75) | 0.17.0–0.22.0 | 0.23.0 |
|
|
144
144
|
| NKI chemistry kernels returned inf for float64 operands, and some kernel routes changed the result dtype (#87) | `df_*` 0.21.0–0.22.0; `ao_to_mo_transform` every release with its kernels | 0.23.0 |
|
|
145
|
-
| `ao_to_mo_transform` aten route on the TorchNeuron device was wrong once `eri` exceeded 4 GiB (#97;
|
|
145
|
+
| `ao_to_mo_transform` aten route on the TorchNeuron device was wrong once `eri` exceeded 4 GiB (#97; the LNC=1 gather case is the 0.24.0 row below) | through 0.22.0 | 0.23.0 |
|
|
146
|
+
| `unpack_tril` / `pack_tril` (and the packed-`eri` aten / legacy routes of `ao_to_mo_transform`) on the neuron device at LNC=1 returned wrong data, at times NaN, once one gathered output reached 4 GiB (154 M mismatched elements at 4.99e9 bytes) (#97) | 0.20.0–0.23.0 | 0.24.0 |
|
|
147
|
+
| Complex `tucker_decompose` on the neuron device used `Uᵀ`, not `Uᴴ`: device matmul ignores the lazy conj bit of `.mH` (#89) | 0.23.0 | 0.24.0 |
|
|
148
|
+
| float64 on the neuron device rounded to fp32 in `gather` (device `torch.cat`) and in `pack_tril` / `unpack_tril` / `pack_density` / `unpack_packed` (device `index_select` / fancy indexing), rel. err ~4e-8 (#92) | through 0.23.0 | 0.24.0 |
|
|
149
|
+
| `multi_einsum` under `"auto"` pinned shared host float64 / complex128 operands to the device, where device aten computed them at fp32 precision (#92) | through 0.23.0 | 0.24.0 |
|
|
150
|
+
| Integer, bool, complex and mixed-dtype operands of `einsum` / `multi_einsum` reached the fp32/bf16/fp16 matmul kernels under `set_backend("nki")`, or under `"auto"` above the size thresholds (trnsci/trnsci#35) | through 0.23.0 | 0.24.0 |
|
|
151
|
+
| `einsum(..., out=)` never wrote `out`, so the documented `einsum(..., beta=1.0, out=acc)` accumulation left `acc` unchanged (#77) | 0.3.0–0.23.0 | 0.24.0 |
|
|
152
|
+
| `cp_reconstruct` returned float32 for bf16 / fp16 factors (#78) | 0.1.1–0.23.0 | 0.24.0 |
|
|
146
153
|
|
|
147
154
|
## Related Projects
|
|
148
155
|
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
[build-system]
|
|
2
|
-
requires = ["setuptools>=
|
|
2
|
+
requires = ["setuptools>=77.0", "setuptools-scm>=8.0"]
|
|
3
3
|
build-backend = "setuptools.build_meta"
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "trntensor"
|
|
7
|
-
version = "0.25.
|
|
7
|
+
version = "0.25.2"
|
|
8
8
|
description = "Tensor contractions for AWS Trainium via NKI"
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
license = "Apache-2.0"
|
|
@@ -45,7 +45,7 @@ dev = [
|
|
|
45
45
|
"pytest>=7.0,<9", # pytest-benchmark 5.3 breaks on pytest 9 (Function._nodeid)
|
|
46
46
|
"pytest-benchmark>=4.0",
|
|
47
47
|
"pytest-cov>=4.0",
|
|
48
|
-
"ruff
|
|
48
|
+
"ruff==0.17.3",
|
|
49
49
|
"pre-commit>=3.7",
|
|
50
50
|
"scipy>=1.11",
|
|
51
51
|
"mkdocs>=1.5",
|
|
@@ -1093,3 +1093,88 @@ class TestBetaZeroIgnoresOut:
|
|
|
1093
1093
|
got = trntensor.einsum(subscripts, *ops, alpha=alpha, beta=0.0, out=out)
|
|
1094
1094
|
torch.testing.assert_close(got, ref)
|
|
1095
1095
|
assert got is out # old contents not read, result written (#77)
|
|
1096
|
+
|
|
1097
|
+
|
|
1098
|
+
class TestIntegerOnDeviceAnyPrecision:
|
|
1099
|
+
"""trnsci/trnsci#135: on the neuron device an integer contraction in
|
|
1100
|
+
"fast" mode ran there in fp32 (int32 above 2^24 rounded), and an int32 /
|
|
1101
|
+
int64 result written through ``out=`` was rounded by the device transpose
|
|
1102
|
+
(aws-neuron/aws-neuron-sdk#1466). CPU stands in for the neuron device
|
|
1103
|
+
(``_FP64_DEVICE_TYPES`` without "cpu")."""
|
|
1104
|
+
|
|
1105
|
+
@pytest.fixture
|
|
1106
|
+
def as_device(self, monkeypatch):
|
|
1107
|
+
E = sys.modules["trntensor.einsum"]
|
|
1108
|
+
calls = []
|
|
1109
|
+
real = E._host_int_einsum
|
|
1110
|
+
|
|
1111
|
+
def spy(subscripts, operands):
|
|
1112
|
+
calls.append(subscripts)
|
|
1113
|
+
return real(subscripts, operands)
|
|
1114
|
+
|
|
1115
|
+
monkeypatch.setattr(E, "_FP64_DEVICE_TYPES", ("cuda",))
|
|
1116
|
+
monkeypatch.setattr(E, "_host_int_einsum", spy)
|
|
1117
|
+
return calls
|
|
1118
|
+
|
|
1119
|
+
@pytest.mark.parametrize("dtype", [torch.int32, torch.int64])
|
|
1120
|
+
@pytest.mark.parametrize(
|
|
1121
|
+
"subscripts,n_ops",
|
|
1122
|
+
[("ij,jk->ik", 2), ("ij,jk->ki", 2), ("ji,jk->ik", 2), ("ij,jk,kl->il", 3), ("ij->ji", 1)],
|
|
1123
|
+
)
|
|
1124
|
+
def test_fast_integer_routes_to_host(self, as_device, dtype, subscripts, n_ops):
|
|
1125
|
+
big = 2**24 + 1 if dtype == torch.int32 else 2**53 + 1
|
|
1126
|
+
a = torch.tensor([[big, 2], [3, 4]], dtype=dtype)
|
|
1127
|
+
eye = torch.eye(2, dtype=dtype)
|
|
1128
|
+
ops = (a, eye, eye)[:n_ops]
|
|
1129
|
+
got = trntensor.einsum(subscripts, *ops)
|
|
1130
|
+
assert as_device == [subscripts]
|
|
1131
|
+
assert got.dtype == dtype and got.is_contiguous()
|
|
1132
|
+
assert torch.equal(got, torch.einsum(subscripts, *ops))
|
|
1133
|
+
|
|
1134
|
+
def test_bmm_integer_routes_to_host(self, as_device):
|
|
1135
|
+
a = torch.tensor([[[2**24 + 1, 2]]], dtype=torch.int32)
|
|
1136
|
+
b = torch.tensor([[[1], [1]]], dtype=torch.int32)
|
|
1137
|
+
assert trntensor.einsum("bij,bjk->bik", a, b).item() == 2**24 + 3
|
|
1138
|
+
assert as_device == ["bij,bjk->bik"]
|
|
1139
|
+
|
|
1140
|
+
def test_float_operands_unaffected(self, as_device):
|
|
1141
|
+
a, b = torch.randn(3, 4), torch.randn(4, 2)
|
|
1142
|
+
torch.testing.assert_close(trntensor.einsum("ij,jk->ik", a, b), a @ b)
|
|
1143
|
+
assert as_device == []
|
|
1144
|
+
|
|
1145
|
+
def test_multi_einsum_integer_not_batched(self, as_device):
|
|
1146
|
+
a = torch.tensor([[2**24 + 1, 2]], dtype=torch.int32)
|
|
1147
|
+
b = torch.tensor([[1], [1]], dtype=torch.int32)
|
|
1148
|
+
got = trntensor.multi_einsum(("ij,jk->ik", a, b), ("ij,jk->ik", a, b))
|
|
1149
|
+
assert [g.item() for g in got] == [2**24 + 3, 2**24 + 3]
|
|
1150
|
+
assert as_device == ["ij,jk->ik", "ij,jk->ik"]
|
|
1151
|
+
|
|
1152
|
+
@pytest.mark.parametrize("dtype", [torch.int32, torch.int64])
|
|
1153
|
+
@pytest.mark.parametrize("out_layout", ["contiguous", "transposed"])
|
|
1154
|
+
def test_out_integer_exact(self, as_device, dtype, out_layout):
|
|
1155
|
+
g = torch.Generator().manual_seed(0)
|
|
1156
|
+
x = torch.randint(-(2**31), 2**31 - 1, (6, 5), generator=g, dtype=torch.int32).to(dtype)
|
|
1157
|
+
x = x * (2**31 + 3) if dtype == torch.int64 else x
|
|
1158
|
+
out = (
|
|
1159
|
+
torch.empty(5, 6, dtype=dtype)
|
|
1160
|
+
if out_layout == "contiguous"
|
|
1161
|
+
else torch.empty(6, 5, dtype=dtype).t()
|
|
1162
|
+
)
|
|
1163
|
+
assert trntensor.einsum("ij->ji", x, out=out) is out
|
|
1164
|
+
assert torch.equal(out, x.t())
|
|
1165
|
+
|
|
1166
|
+
def test_exact_copy_uses_bit_views(self, monkeypatch):
|
|
1167
|
+
E = sys.modules["trntensor.einsum"]
|
|
1168
|
+
monkeypatch.setattr(E, "_FP64_DEVICE_TYPES", ("cuda",))
|
|
1169
|
+
assert E._exact_bits(torch.zeros(2, dtype=torch.int32)).dtype == torch.uint32
|
|
1170
|
+
assert E._exact_bits(torch.zeros(2, dtype=torch.int64)).dtype == torch.float64
|
|
1171
|
+
assert E._exact_bits(torch.zeros(2)).dtype == torch.float32
|
|
1172
|
+
x = torch.tensor([[2**24 + 1, -(2**31)], [2**31 - 1, 7]], dtype=torch.int32)
|
|
1173
|
+
out = torch.empty(2, 2, dtype=torch.int64)
|
|
1174
|
+
E._exact_copy_(out, x.t()) # a cast goes through the host
|
|
1175
|
+
assert torch.equal(out, x.t().to(torch.int64))
|
|
1176
|
+
|
|
1177
|
+
def test_to_device_exact_is_contiguous(self):
|
|
1178
|
+
E = sys.modules["trntensor.einsum"]
|
|
1179
|
+
t = torch.arange(6, dtype=torch.int32).reshape(2, 3).t()
|
|
1180
|
+
assert E._to_device_exact(t, torch.device("cpu")).is_contiguous()
|
|
@@ -1303,7 +1303,7 @@ class TestNativeRoute:
|
|
|
1303
1303
|
torch.testing.assert_close(
|
|
1304
1304
|
dispatch.nki_batched_matmul(A[None], B[None]), torch.bmm(A[None], B[None])
|
|
1305
1305
|
)
|
|
1306
|
-
assert moved == [
|
|
1306
|
+
assert moved == [dispatch._neuron_matmul, dispatch._neuron_bmm]
|
|
1307
1307
|
|
|
1308
1308
|
def test_fused_quantum_kernels_off_under_auto(self, monkeypatch):
|
|
1309
1309
|
monkeypatch.setattr(dispatch, "HAS_NKI", True)
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
"""On-device regression tests for trnsci/trnsci#135 (aws-neuron/aws-neuron-sdk#1466).
|
|
2
|
+
|
|
3
|
+
On Trn2 (SDK 2.33), every device op that materializes a transposed int32 /
|
|
4
|
+
int64 layout returns int32(float32(x)) (per 32-bit half for int64), and
|
|
5
|
+
integer matmul runs in fp32. Integer einsum on the device therefore
|
|
6
|
+
contracts on the host, and ``out=`` writes go through bit-exact views.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
import pytest
|
|
10
|
+
import torch
|
|
11
|
+
|
|
12
|
+
import trntensor
|
|
13
|
+
from trntensor.nki import HAS_NKI
|
|
14
|
+
from trntensor.nki import dispatch as D
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def _native() -> bool:
|
|
18
|
+
if not HAS_NKI or D._use_simulator():
|
|
19
|
+
return False
|
|
20
|
+
try:
|
|
21
|
+
return D._get_dispatch_path() == "native"
|
|
22
|
+
except Exception:
|
|
23
|
+
return False
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
pytestmark = [
|
|
27
|
+
pytest.mark.neuron,
|
|
28
|
+
pytest.mark.skipif(not _native(), reason="requires TorchNeuron (native) dispatch path"),
|
|
29
|
+
]
|
|
30
|
+
|
|
31
|
+
DEV = "neuron"
|
|
32
|
+
BIG = 2**24 + 1
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def _rand_int(shape, dtype, seed=0):
|
|
36
|
+
g = torch.Generator().manual_seed(seed)
|
|
37
|
+
lo = torch.randint(-(2**31), 2**31 - 1, (*shape, 2), generator=g, dtype=torch.int32)
|
|
38
|
+
return lo[..., 0].contiguous() if dtype == torch.int32 else lo.view(torch.int64).reshape(shape)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
@pytest.mark.parametrize("subscripts", ["ij,jk->ik", "ij,jk->ki", "ji,jk->ik", "ij,jk,kl->il"])
|
|
42
|
+
@pytest.mark.parametrize("precision", ["fast", "kahan"])
|
|
43
|
+
def test_int32_contraction_exact_on_device(subscripts, precision):
|
|
44
|
+
a = torch.tensor([[BIG, 2], [3, 4]], dtype=torch.int32)
|
|
45
|
+
eye = torch.eye(2, dtype=torch.int32)
|
|
46
|
+
ops = (a, eye, eye)[: subscripts.count(",") + 1]
|
|
47
|
+
got = trntensor.einsum(subscripts, *(o.to(DEV) for o in ops), precision=precision)
|
|
48
|
+
assert got.device.type == DEV
|
|
49
|
+
assert torch.equal(got.cpu(), torch.einsum(subscripts, *ops))
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
@pytest.mark.parametrize("dtype", [torch.int32, torch.int64])
|
|
53
|
+
@pytest.mark.parametrize("shape", [(64, 96), (256, 384), (1000, 600)])
|
|
54
|
+
@pytest.mark.parametrize("out_layout", ["contiguous", "transposed"])
|
|
55
|
+
def test_transpose_out_exact_on_device(dtype, shape, out_layout):
|
|
56
|
+
x = _rand_int(shape, dtype)
|
|
57
|
+
if out_layout == "contiguous":
|
|
58
|
+
out = torch.empty(shape[::-1], dtype=dtype, device=DEV)
|
|
59
|
+
else:
|
|
60
|
+
out = torch.empty(shape, dtype=dtype, device=DEV).t()
|
|
61
|
+
trntensor.einsum("ij->ji", x.to(DEV), out=out)
|
|
62
|
+
assert torch.equal(out.cpu(), x.t())
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
@pytest.mark.parametrize("dtype", [torch.int32, torch.int64])
|
|
66
|
+
def test_transpose_contiguous_result_on_device(dtype):
|
|
67
|
+
x = _rand_int((256, 384), dtype, seed=1)
|
|
68
|
+
got = trntensor.einsum("ij->ji", x.to(DEV))
|
|
69
|
+
assert got.is_contiguous()
|
|
70
|
+
assert torch.equal(got.cpu(), x.t())
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
"""Row-block matmul for the >= 4 GiB partition-span bug on the neuron device (#123).
|
|
2
|
+
|
|
3
|
+
CPU tests: the thresholds are lowered so that small host tensors stand in for
|
|
4
|
+
the 32 MiB-row operands, and `_on_neuron_device` is patched so the host tensors
|
|
5
|
+
take the device logic. A spy on `torch.matmul` checks that no product the
|
|
6
|
+
blocking issues still has an unsafe operand or result. The on-device
|
|
7
|
+
regression tests at the real sizes are in test_native_device.py
|
|
8
|
+
(`TestSpan4GiB`).
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
import pytest
|
|
12
|
+
import torch
|
|
13
|
+
|
|
14
|
+
import trntensor
|
|
15
|
+
from trntensor import quantum
|
|
16
|
+
from trntensor.nki import dispatch as D
|
|
17
|
+
|
|
18
|
+
SPAN = 1 << 14 # stands in for 2**32
|
|
19
|
+
BLOCK = 1 << 13 # stands in for 2**31
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
@pytest.fixture
|
|
23
|
+
def lowered(monkeypatch):
|
|
24
|
+
monkeypatch.setattr(D, "_NEURON_SPAN_BYTES", SPAN)
|
|
25
|
+
monkeypatch.setattr(D, "_NEURON_BLOCK_BYTES", BLOCK)
|
|
26
|
+
monkeypatch.setattr(D, "_on_neuron_device", lambda t: True)
|
|
27
|
+
calls = []
|
|
28
|
+
real = torch.matmul
|
|
29
|
+
|
|
30
|
+
def spy(a, b, *args, **kwargs):
|
|
31
|
+
calls.append((tuple(a.shape), tuple(b.shape)))
|
|
32
|
+
if a.dim() == 2 and b.dim() == 2:
|
|
33
|
+
assert not D._span_block(a), f"unsafe lhs {tuple(a.shape)}"
|
|
34
|
+
assert not D._span_block(b), f"unsafe rhs {tuple(b.shape)}"
|
|
35
|
+
assert not D._out_block(a.shape[0], b.shape[1], a.element_size()), "unsafe result"
|
|
36
|
+
return real(a, b, *args, **kwargs)
|
|
37
|
+
|
|
38
|
+
monkeypatch.setattr(torch, "matmul", spy)
|
|
39
|
+
return calls
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def _r(*shape, seed=0):
|
|
43
|
+
g = torch.Generator().manual_seed(seed)
|
|
44
|
+
return torch.randn(*shape, generator=g, dtype=torch.float64).float()
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def _ref(a, b):
|
|
48
|
+
return (a.double() @ b.double()).float()
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def test_real_thresholds():
|
|
52
|
+
assert D._NEURON_SPAN_BYTES == 1 << 32
|
|
53
|
+
assert D._out_block(128, 8_389_632, 4) == 64
|
|
54
|
+
assert D._out_block(128, 8_388_480, 4) == 0
|
|
55
|
+
assert D._out_block(64, 8_389_632, 4) == 0
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
@pytest.mark.parametrize("M,K,N", [(12, 128, 40), (12, 300, 33), (128, 40, 6), (130, 128, 40)])
|
|
59
|
+
def test_blocked_matches_plain(lowered, M, K, N):
|
|
60
|
+
a, b = _r(M, K), _r(K, N, seed=1)
|
|
61
|
+
assert D._neuron_mm_unsafe(a, b)
|
|
62
|
+
torch.testing.assert_close(D._neuron_matmul(a, b), _ref(a, b), rtol=1e-5, atol=1e-4)
|
|
63
|
+
assert len(lowered) > 1
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def test_batched_and_bmm(lowered):
|
|
67
|
+
a, b = _r(3, 12, 128), _r(3, 128, 40, seed=1)
|
|
68
|
+
torch.testing.assert_close(D._neuron_bmm(a, b), _ref(a, b), rtol=1e-5, atol=1e-4)
|
|
69
|
+
assert len(lowered) > 1
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def test_short_row_batches_stay_one_call(monkeypatch):
|
|
73
|
+
monkeypatch.setattr(D, "_NEURON_SPAN_BYTES", SPAN)
|
|
74
|
+
monkeypatch.setattr(D, "_on_neuron_device", lambda t: True)
|
|
75
|
+
# (nocc, nbasis, naux) halves with short rows: no blocking even when large.
|
|
76
|
+
a, b = _r(4, 6, 32), _r(4, 32, 8, seed=1)
|
|
77
|
+
assert not D._neuron_mm_unsafe(a, b)
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def test_safe_and_host_products_untouched(monkeypatch):
|
|
81
|
+
a, b = _r(12, 128), _r(128, 40, seed=1)
|
|
82
|
+
assert torch.equal(D._neuron_matmul(a, b), torch.matmul(a, b)) # host
|
|
83
|
+
monkeypatch.setattr(D, "_on_neuron_device", lambda t: True)
|
|
84
|
+
calls = []
|
|
85
|
+
real = torch.matmul
|
|
86
|
+
monkeypatch.setattr(torch, "matmul", lambda x, y: calls.append((x, y)) or real(x, y))
|
|
87
|
+
out = D._neuron_matmul(a, b) # below the real 4 GiB rule
|
|
88
|
+
assert len(calls) == 1 and calls[0][0] is a and calls[0][1] is b
|
|
89
|
+
assert torch.equal(out, real(a, b))
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
@pytest.mark.parametrize("backend", ["auto", "pytorch"])
|
|
93
|
+
def test_einsum_matmul_and_bmm_routes(lowered, backend):
|
|
94
|
+
old = trntensor.get_backend()
|
|
95
|
+
trntensor.set_backend(backend)
|
|
96
|
+
try:
|
|
97
|
+
a, b = _r(12, 128), _r(128, 40, seed=1)
|
|
98
|
+
out = trntensor.einsum("ij,jk->ik", a, b)
|
|
99
|
+
torch.testing.assert_close(out, _ref(a, b), rtol=1e-5, atol=1e-4)
|
|
100
|
+
assert len(lowered) > 1
|
|
101
|
+
lowered.clear()
|
|
102
|
+
a3, b3 = _r(2, 12, 128), _r(2, 128, 40, seed=1)
|
|
103
|
+
out = trntensor.einsum("bij,bjk->bik", a3, b3)
|
|
104
|
+
torch.testing.assert_close(out, _ref(a3, b3), rtol=1e-5, atol=1e-4)
|
|
105
|
+
finally:
|
|
106
|
+
trntensor.set_backend(old)
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def test_ao_to_mo_contiguous_route_blocks(lowered, monkeypatch):
|
|
110
|
+
# nbasis 128, nbasis * naux * 4 B = 2 KiB rows: the #123 layout at this scale.
|
|
111
|
+
nb, naux, nocc, nvir = 128, 4, 5, 7
|
|
112
|
+
eri = _r(nb, nb, naux)
|
|
113
|
+
C_occ, C_vir = _r(nb, nocc, seed=1), _r(nb, nvir, seed=2)
|
|
114
|
+
ref = torch.einsum(
|
|
115
|
+
"na,inP->iaP", C_vir.double(), torch.einsum("mi,mnP->inP", C_occ.double(), eri.double())
|
|
116
|
+
)
|
|
117
|
+
out = quantum._ao_to_mo_contiguous_lhs(eri, C_occ, C_vir)
|
|
118
|
+
torch.testing.assert_close(out.double(), ref, rtol=1e-5, atol=1e-5)
|
|
119
|
+
assert len(lowered) > 2 # the half-transform was blocked over K
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
"""On-device regression tests for #123 (trnblas#112, aws-neuron-sdk#1457) at real sizes.
|
|
2
|
+
|
|
3
|
+
A device matmul is silently wrong once an operand's (or the result's) 128 DMA
|
|
4
|
+
partitions span 4 GiB or more: fp32 rows of 32 MiB or more. Operands are
|
|
5
|
+
uploaded above a same-size decoy holding different data, or produced on the
|
|
6
|
+
device by `clone`, and every result is compared with a host reference. Each
|
|
7
|
+
case needs about 9-14 GB of one logical core's device memory.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
import gc
|
|
11
|
+
|
|
12
|
+
import pytest
|
|
13
|
+
import torch
|
|
14
|
+
|
|
15
|
+
import trntensor
|
|
16
|
+
from trntensor import quantum
|
|
17
|
+
from trntensor.nki import HAS_NKI
|
|
18
|
+
from trntensor.nki import dispatch as D
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def _native() -> bool:
|
|
22
|
+
if not HAS_NKI or D._use_simulator():
|
|
23
|
+
return False
|
|
24
|
+
try:
|
|
25
|
+
return D._get_dispatch_path() == "native"
|
|
26
|
+
except Exception:
|
|
27
|
+
return False
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
pytestmark = [
|
|
31
|
+
pytest.mark.neuron,
|
|
32
|
+
pytest.mark.skipif(not _native(), reason="requires TorchNeuron (native) dispatch path"),
|
|
33
|
+
]
|
|
34
|
+
|
|
35
|
+
DEV = "neuron"
|
|
36
|
+
N_BAD = 8_389_632 # 32 MiB + 4 KiB fp32 rows
|
|
37
|
+
N_OK = 8_388_480 # 32 MiB - 512 B
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def _host(*shape, seed=0):
|
|
41
|
+
g = torch.Generator().manual_seed(seed)
|
|
42
|
+
return torch.randn(*shape, generator=g)
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def _rel(out, ref):
|
|
46
|
+
out, ref = out.cpu().double(), ref.double()
|
|
47
|
+
return ((out - ref).norm() / ref.norm()).item()
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
@pytest.fixture(autouse=True)
|
|
51
|
+
def _free():
|
|
52
|
+
yield
|
|
53
|
+
gc.collect()
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
@pytest.fixture(params=["upload", "device"])
|
|
57
|
+
def source(request):
|
|
58
|
+
return request.param
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def _device_operand(X, source):
|
|
62
|
+
decoy = (-X).to(DEV) # different data directly below
|
|
63
|
+
return decoy, (X.to(DEV) if source == "upload" else X.to(DEV).clone())
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
class TestSpan4GiB:
|
|
67
|
+
def test_einsum_matmul(self, source):
|
|
68
|
+
A, B = _host(96, 128), _host(128, N_BAD, seed=1)
|
|
69
|
+
decoy, Bd = _device_operand(B, source)
|
|
70
|
+
out = trntensor.einsum("ij,jk->ik", A.to(DEV), Bd)
|
|
71
|
+
assert _rel(out, A @ B) < 1e-5
|
|
72
|
+
|
|
73
|
+
def test_einsum_bmm(self, source):
|
|
74
|
+
A, B = _host(1, 96, 128), _host(1, 128, N_BAD, seed=1)
|
|
75
|
+
decoy, Bd = _device_operand(B, source)
|
|
76
|
+
out = trntensor.einsum("bij,bjk->bik", A.to(DEV), Bd)
|
|
77
|
+
assert _rel(out, A @ B) < 1e-5
|
|
78
|
+
|
|
79
|
+
def test_ao_to_mo_contiguous_route(self, source):
|
|
80
|
+
# nbasis * naux = 8,389,632: the #98 contiguous route's B rows are 32 MiB + 4 KiB.
|
|
81
|
+
nb, naux, nocc, nvir = 128, 65_544, 8, 16
|
|
82
|
+
eri = _host(nb, nb, naux)
|
|
83
|
+
C_occ, C_vir = _host(nb, nocc, seed=1), _host(nb, nvir, seed=2)
|
|
84
|
+
decoy, eri_d = _device_operand(eri, source)
|
|
85
|
+
half = (C_occ.T @ eri.reshape(nb, nb * naux)).reshape(nocc, nb, naux)
|
|
86
|
+
ref = torch.matmul(C_vir.T, half)
|
|
87
|
+
del half
|
|
88
|
+
out = quantum._cpu_ao_to_mo_transform(eri_d, C_occ.to(DEV), C_vir.to(DEV))
|
|
89
|
+
assert out.shape == (nocc, nvir, naux)
|
|
90
|
+
assert _rel(out, ref) < 1e-5
|
|
91
|
+
|
|
92
|
+
def test_below_rule_is_the_plain_call(self):
|
|
93
|
+
A, B = _host(96, 128), _host(128, N_OK, seed=1)
|
|
94
|
+
decoy, Bd = _device_operand(B, "upload")
|
|
95
|
+
Ad = A.to(DEV)
|
|
96
|
+
assert not D._neuron_mm_unsafe(Ad, Bd)
|
|
97
|
+
out = trntensor.einsum("ij,jk->ik", Ad, Bd)
|
|
98
|
+
assert torch.equal(out.cpu(), torch.matmul(Ad, Bd).cpu())
|
|
99
|
+
assert _rel(out, A @ B) < 1e-5
|