trntensor 0.25.1__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.
Files changed (90) hide show
  1. {trntensor-0.25.1 → trntensor-0.25.2}/CHANGELOG.md +20 -0
  2. {trntensor-0.25.1 → trntensor-0.25.2}/CONTRIBUTING.md +11 -0
  3. {trntensor-0.25.1/trntensor.egg-info → trntensor-0.25.2}/PKG-INFO +1 -1
  4. {trntensor-0.25.1 → trntensor-0.25.2}/pyproject.toml +2 -2
  5. {trntensor-0.25.1 → trntensor-0.25.2}/tests/test_einsum.py +85 -0
  6. trntensor-0.25.2/tests/test_neuron_int_exact.py +70 -0
  7. {trntensor-0.25.1 → trntensor-0.25.2}/trntensor/__init__.py +1 -1
  8. {trntensor-0.25.1 → trntensor-0.25.2}/trntensor/einsum.py +62 -3
  9. {trntensor-0.25.1 → trntensor-0.25.2}/trntensor/nki/_kernels.py +2 -1
  10. {trntensor-0.25.1 → trntensor-0.25.2}/trntensor/nki/dispatch.py +6 -3
  11. {trntensor-0.25.1 → trntensor-0.25.2/trntensor.egg-info}/PKG-INFO +1 -1
  12. {trntensor-0.25.1 → trntensor-0.25.2}/trntensor.egg-info/SOURCES.txt +1 -0
  13. {trntensor-0.25.1 → trntensor-0.25.2}/.github/workflows/ci.yml +0 -0
  14. {trntensor-0.25.1 → trntensor-0.25.2}/.github/workflows/notify-umbrella.yml +0 -0
  15. {trntensor-0.25.1 → trntensor-0.25.2}/.github/workflows/publish.yml +0 -0
  16. {trntensor-0.25.1 → trntensor-0.25.2}/.gitignore +0 -0
  17. {trntensor-0.25.1 → trntensor-0.25.2}/.pre-commit-config.yaml +0 -0
  18. {trntensor-0.25.1 → trntensor-0.25.2}/CLAUDE.md +0 -0
  19. {trntensor-0.25.1 → trntensor-0.25.2}/CODE_OF_CONDUCT.md +0 -0
  20. {trntensor-0.25.1 → trntensor-0.25.2}/LICENSE +0 -0
  21. {trntensor-0.25.1 → trntensor-0.25.2}/README.md +0 -0
  22. {trntensor-0.25.1 → trntensor-0.25.2}/benchmarks/__init__.py +0 -0
  23. {trntensor-0.25.1 → trntensor-0.25.2}/benchmarks/bench_einsum.py +0 -0
  24. {trntensor-0.25.1 → trntensor-0.25.2}/docs/api/decompose.md +0 -0
  25. {trntensor-0.25.1 → trntensor-0.25.2}/docs/api/einsum.md +0 -0
  26. {trntensor-0.25.1 → trntensor-0.25.2}/docs/api/index.md +0 -0
  27. {trntensor-0.25.1 → trntensor-0.25.2}/docs/api/nki.md +0 -0
  28. {trntensor-0.25.1 → trntensor-0.25.2}/docs/api/plan.md +0 -0
  29. {trntensor-0.25.1 → trntensor-0.25.2}/docs/api/quantum.md +0 -0
  30. {trntensor-0.25.1 → trntensor-0.25.2}/docs/api/stream.md +0 -0
  31. {trntensor-0.25.1 → trntensor-0.25.2}/docs/architecture.md +0 -0
  32. {trntensor-0.25.1 → trntensor-0.25.2}/docs/aws_setup.md +0 -0
  33. {trntensor-0.25.1 → trntensor-0.25.2}/docs/benchmarks.md +0 -0
  34. {trntensor-0.25.1 → trntensor-0.25.2}/docs/contraction_patterns.md +0 -0
  35. {trntensor-0.25.1 → trntensor-0.25.2}/docs/design/matmul_dispatch_findings.md +0 -0
  36. {trntensor-0.25.1 → trntensor-0.25.2}/docs/design/matmul_kernel_profile.md +0 -0
  37. {trntensor-0.25.1 → trntensor-0.25.2}/docs/developing_kernels.md +0 -0
  38. {trntensor-0.25.1 → trntensor-0.25.2}/docs/index.md +0 -0
  39. {trntensor-0.25.1 → trntensor-0.25.2}/docs/installation.md +0 -0
  40. {trntensor-0.25.1 → trntensor-0.25.2}/docs/quickstart.md +0 -0
  41. {trntensor-0.25.1 → trntensor-0.25.2}/docs/tutorial_df_mp2.md +0 -0
  42. {trntensor-0.25.1 → trntensor-0.25.2}/examples/df_mp2_einsum.py +0 -0
  43. {trntensor-0.25.1 → trntensor-0.25.2}/infra/terraform/README.md +0 -0
  44. {trntensor-0.25.1 → trntensor-0.25.2}/infra/terraform/main.tf +0 -0
  45. {trntensor-0.25.1 → trntensor-0.25.2}/mkdocs.yml +0 -0
  46. {trntensor-0.25.1 → trntensor-0.25.2}/scripts/autotune_dispatch.py +0 -0
  47. {trntensor-0.25.1 → trntensor-0.25.2}/scripts/bench_ao2mo_nocc.py +0 -0
  48. {trntensor-0.25.1 → trntensor-0.25.2}/scripts/bench_eager_handle.py +0 -0
  49. {trntensor-0.25.1 → trntensor-0.25.2}/scripts/bench_native_einsum.py +0 -0
  50. {trntensor-0.25.1 → trntensor-0.25.2}/scripts/bench_native_kernels.py +0 -0
  51. {trntensor-0.25.1 → trntensor-0.25.2}/scripts/bench_to_md.py +0 -0
  52. {trntensor-0.25.1 → trntensor-0.25.2}/scripts/run_benchmarks.sh +0 -0
  53. {trntensor-0.25.1 → trntensor-0.25.2}/scripts/run_neuron_profile.sh +0 -0
  54. {trntensor-0.25.1 → trntensor-0.25.2}/scripts/run_neuron_tests.sh +0 -0
  55. {trntensor-0.25.1 → trntensor-0.25.2}/scripts/run_reduce_parallel_multirank.py +0 -0
  56. {trntensor-0.25.1 → trntensor-0.25.2}/scripts/run_simulator_tests.sh +0 -0
  57. {trntensor-0.25.1 → trntensor-0.25.2}/setup.cfg +0 -0
  58. {trntensor-0.25.1 → trntensor-0.25.2}/tests/conftest.py +0 -0
  59. {trntensor-0.25.1 → trntensor-0.25.2}/tests/test_conformance.py +0 -0
  60. {trntensor-0.25.1 → trntensor-0.25.2}/tests/test_decompose.py +0 -0
  61. {trntensor-0.25.1 → trntensor-0.25.2}/tests/test_host_output.py +0 -0
  62. {trntensor-0.25.1 → trntensor-0.25.2}/tests/test_kernel_dtype_guard.py +0 -0
  63. {trntensor-0.25.1 → trntensor-0.25.2}/tests/test_native_dispatch.py +0 -0
  64. {trntensor-0.25.1 → trntensor-0.25.2}/tests/test_neuron_span.py +0 -0
  65. {trntensor-0.25.1 → trntensor-0.25.2}/tests/test_neuron_span_device.py +0 -0
  66. {trntensor-0.25.1 → trntensor-0.25.2}/tests/test_nki.py +0 -0
  67. {trntensor-0.25.1 → trntensor-0.25.2}/tests/test_nki_compile.py +0 -0
  68. {trntensor-0.25.1 → trntensor-0.25.2}/tests/test_nki_kernels.py +0 -0
  69. {trntensor-0.25.1 → trntensor-0.25.2}/tests/test_nki_native.py +0 -0
  70. {trntensor-0.25.1 → trntensor-0.25.2}/tests/test_nki_sim.py +0 -0
  71. {trntensor-0.25.1 → trntensor-0.25.2}/tests/test_nki_tail_audit.py +0 -0
  72. {trntensor-0.25.1 → trntensor-0.25.2}/tests/test_parallel.py +0 -0
  73. {trntensor-0.25.1 → trntensor-0.25.2}/tests/test_plan.py +0 -0
  74. {trntensor-0.25.1 → trntensor-0.25.2}/tests/test_quantum.py +0 -0
  75. {trntensor-0.25.1 → trntensor-0.25.2}/tests/test_sr_runtime.py +0 -0
  76. {trntensor-0.25.1 → trntensor-0.25.2}/tests/test_stream.py +0 -0
  77. {trntensor-0.25.1 → trntensor-0.25.2}/tests/test_trace_cache_safety.py +0 -0
  78. {trntensor-0.25.1 → trntensor-0.25.2}/trntensor/decompose.py +0 -0
  79. {trntensor-0.25.1 → trntensor-0.25.2}/trntensor/nki/__init__.py +0 -0
  80. {trntensor-0.25.1 → trntensor-0.25.2}/trntensor/nki/_native.py +0 -0
  81. {trntensor-0.25.1 → trntensor-0.25.2}/trntensor/nki/collectives.py +0 -0
  82. {trntensor-0.25.1 → trntensor-0.25.2}/trntensor/parallel.py +0 -0
  83. {trntensor-0.25.1 → trntensor-0.25.2}/trntensor/plan.py +0 -0
  84. {trntensor-0.25.1 → trntensor-0.25.2}/trntensor/py.typed +0 -0
  85. {trntensor-0.25.1 → trntensor-0.25.2}/trntensor/quantum.py +0 -0
  86. {trntensor-0.25.1 → trntensor-0.25.2}/trntensor/stream.py +0 -0
  87. {trntensor-0.25.1 → trntensor-0.25.2}/trntensor.egg-info/dependency_links.txt +0 -0
  88. {trntensor-0.25.1 → trntensor-0.25.2}/trntensor.egg-info/requires.txt +0 -0
  89. {trntensor-0.25.1 → trntensor-0.25.2}/trntensor.egg-info/top_level.txt +0 -0
  90. {trntensor-0.25.1 → trntensor-0.25.2}/uv.lock +0 -0
@@ -7,6 +7,26 @@ 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
+
10
30
  ## [0.25.1] — 2026-11-07
11
31
 
12
32
  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.1
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
@@ -1,10 +1,10 @@
1
1
  [build-system]
2
- requires = ["setuptools>=68.0", "setuptools-scm>=8.0"]
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.1"
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"
@@ -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()
@@ -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())
@@ -5,7 +5,7 @@ Einstein summation, contraction planning, and tensor decompositions
5
5
  for scientific computing. Part of the trnsci scientific computing suite.
6
6
  """
7
7
 
8
- __version__ = "0.25.1"
8
+ __version__ = "0.25.2"
9
9
 
10
10
  from .decompose import (
11
11
  cp_decompose,
@@ -205,11 +205,44 @@ def _write_out(result: torch.Tensor, out: torch.Tensor) -> torch.Tensor:
205
205
  if result.untyped_storage().data_ptr() == out.untyped_storage().data_ptr():
206
206
  if result.data_ptr() == out.data_ptr() and result.stride() == out.stride():
207
207
  return out
208
- result = result.clone()
209
- out.copy_(result)
208
+ result = _exact_bits(result).clone().view(result.dtype)
209
+ _exact_copy_(out, result)
210
210
  return out
211
211
 
212
212
 
213
+ # On the TorchNeuron device every op that materializes a transposed int32 or
214
+ # int64 layout (copy_ / clone / .contiguous() of a transposed view, an upload
215
+ # of a transposed host view) returns int32(float32(x)), per 32-bit half for
216
+ # int64: values above 2^24 are rounded, silently (Trn2, SDK 2.33,
217
+ # aws-neuron/aws-neuron-sdk#1466, trnsci/trnsci#135). The same bytes viewed
218
+ # as uint32 / float64 move exactly, so integer copies go through those views.
219
+ _BITS_VIEW = {torch.int32: torch.uint32, torch.int64: torch.float64}
220
+
221
+
222
+ def _exact_bits(t: torch.Tensor) -> torch.Tensor:
223
+ """``t`` viewed as a dtype the device moves bit-exactly (int32 -> uint32,
224
+ int64 -> float64); ``t`` itself on CPU/CUDA or for other dtypes."""
225
+ bits = _BITS_VIEW.get(t.dtype)
226
+ if bits is None or t.device.type in _FP64_DEVICE_TYPES:
227
+ return t
228
+ return t.view(bits)
229
+
230
+
231
+ def _exact_copy_(out: torch.Tensor, src: torch.Tensor) -> torch.Tensor:
232
+ """``out.copy_(src)``, bit-exact for int32 / int64 on the neuron device."""
233
+ if src.dtype == out.dtype and src.dtype in _BITS_VIEW and src.device == out.device:
234
+ _exact_bits(out).copy_(_exact_bits(src))
235
+ return out
236
+ if src.dtype in _BITS_VIEW or out.dtype in _BITS_VIEW:
237
+ # A cast or a cross-device integer copy: cast on the host (a download
238
+ # of any view is exact), then move with the same-dtype bit view.
239
+ host = src.cpu().to(out.dtype).contiguous()
240
+ if out.device.type == "cpu":
241
+ return out.copy_(host)
242
+ return _exact_copy_(out, host.to(out.device))
243
+ return out.copy_(src)
244
+
245
+
213
246
  def _owns(result: torch.Tensor, others) -> bool:
214
247
  """True when ``result`` may be overwritten: no autograd history to keep and
215
248
  no storage shared with an operand or ``out`` (``torch.einsum`` can return a
@@ -457,6 +490,26 @@ def _host_int_einsum(subscripts: str, operands: tuple) -> torch.Tensor:
457
490
  return torch.einsum(subscripts, *(op.cpu() for op in operands))
458
491
 
459
492
 
493
+ def _to_device_exact(t: torch.Tensor, device: torch.device) -> torch.Tensor:
494
+ """Host result ``t`` on ``device``, contiguous first: an upload of a
495
+ transposed int32 / int64 host view is rounded through fp32 on the neuron
496
+ device (aws-neuron/aws-neuron-sdk#1466, trnsci/trnsci#135), and a host
497
+ einsum can return one (e.g. ``"ij,jk->ki"``)."""
498
+ return t.contiguous().to(device)
499
+
500
+
501
+ def _integer_off_host(operands: tuple) -> bool:
502
+ """True for an integer / bool contraction with an operand on a device
503
+ without exact integer arithmetic. The neuron device computes integer
504
+ matmul in fp32 (int32 above 2^24 is rounded, #90) and rounds int32 /
505
+ int64 transposes (aws-neuron/aws-neuron-sdk#1466), so these contract on
506
+ the host in every precision mode."""
507
+ if not operands or all(op.device.type in _FP64_DEVICE_TYPES for op in operands):
508
+ return False
509
+ promoted = functools.reduce(torch.promote_types, (op.dtype for op in operands))
510
+ return not (promoted.is_floating_point or promoted.is_complex)
511
+
512
+
460
513
  def _einsum_fp64(subscripts: str, operands: tuple) -> torch.Tensor:
461
514
  """Contract in true fp64 and round once to the operand dtype.
462
515
 
@@ -481,7 +534,7 @@ def _einsum_fp64(subscripts: str, operands: tuple) -> torch.Tensor:
481
534
  return torch.einsum(subscripts, *operands)
482
535
  # On the neuron device an integer contraction runs in fp32 and rounds
483
536
  # values above 2**24 (#90): contract natively on the host instead.
484
- return _host_int_einsum(subscripts, operands).to(orig_device)
537
+ return _to_device_exact(_host_int_einsum(subscripts, operands), orig_device)
485
538
  if all(op.device.type in _FP64_DEVICE_TYPES for op in operands):
486
539
  result = torch.einsum(subscripts, *(op.to(wide) for op in operands))
487
540
  return result.to(orig_dtype)
@@ -537,6 +590,10 @@ def _execute_contraction(subscripts: str, operands: tuple, plan: ContractionPlan
537
590
  if plan.strategy == "path" and real_float:
538
591
  return _execute_path(subscripts, operands, plan)
539
592
  return _einsum_fp64(subscripts, operands)
593
+ if _integer_off_host(operands):
594
+ # "fast" / "sr": integer contractions on the neuron device were
595
+ # contracted there, in fp32 (int32 above 2^24 rounded, trnsci#135).
596
+ return _to_device_exact(_host_int_einsum(subscripts, operands), operands[0].device)
540
597
  if plan.strategy in ("torch", "path"):
541
598
  offloaded = _offload_host_einsum(subscripts, operands, plan)
542
599
  if offloaded is not None:
@@ -715,6 +772,8 @@ def _try_batched_multi_einsum(
715
772
  meta_key = tuple((op.dtype, op.device) for op in contractions[0][1:])
716
773
  if not all(tuple((op.dtype, op.device) for op in c[1:]) == meta_key for c in contractions):
717
774
  return None
775
+ if _integer_off_host(contractions[0][1:]):
776
+ return None # integer bmm on the device is fp32: einsum contracts on the host
718
777
 
719
778
  from .nki.dispatch import nki_batched_matmul
720
779
 
@@ -1004,7 +1004,8 @@ if HAS_NKI:
1004
1004
  # loops unroll, so the dispatcher runs the pair over groups of aux chunks
1005
1005
  # (``df_group_chunks``) to bound compile time and the ``Xt`` scratch.
1006
1006
 
1007
- # Bytes per partition for launch 1's buffers (of 192 KiB). 176 KiB admits
1007
+ # Bytes per partition for launch 1's buffers (of 224 KiB on Trn2, about
1008
+ # 207 KiB allocatable by NKI; 192 KiB on Trn1). 176 KiB admits
1008
1009
  # TP = 256, NUB = 8, NVC = 16 at n_ao = 970 (175 KiB), the fastest tiling
1009
1010
  # measured there (#60).
1010
1011
  DF_K_SBUF_BUDGET = 176 * 1024
@@ -1850,8 +1850,10 @@ def _nki_mp2_energy(
1850
1850
  # ── Native MP2 energy: per-row symmetric kernel, both cores at LNC=2 (#51) ──
1851
1851
  #
1852
1852
  # SBUF bytes per partition `mp2_energy_row_kernel` keeps live: B[i].T and
1853
- # B[j].T (NK * NVIR each) plus the T strip block (NA * NVIR), all fp32. Trn2
1854
- # has 192 KiB per partition; leave room for the compiler's temporaries.
1853
+ # B[j].T (NK * NVIR each) plus the T strip block (NA * NVIR), all fp32.
1854
+ # Conservative: SBUF is 192 KiB per partition on Trn1 and 224 KiB on Trn2
1855
+ # (about 207 KiB of it allocatable by NKI); leave room for the compiler's
1856
+ # temporaries.
1855
1857
  _MP2_ROW_SBUF_BUDGET = 144 * 1024
1856
1858
 
1857
1859
 
@@ -2147,7 +2149,8 @@ def _nki_ao_to_mo_transform(
2147
2149
  _AO_TO_MO_CHOICES = ("auto", "trn2", "aten", "legacy")
2148
2150
  # SBUF bytes per partition `ao_to_mo_trn2_kernel` keeps live (C_occ, C_vir,
2149
2151
  # one stage-1 eri block and X block, one stage-2 X group and B block); same
2150
- # budget as df_exchange's launch 1 (of 192 KiB per partition). It was 144 KiB;
2152
+ # budget as df_exchange's launch 1 (of 224 KiB per partition on Trn2, about
2153
+ # 207 KiB allocatable by NKI; 192 KiB on Trn1). It was 144 KiB;
2151
2154
  # n_ao = 970, nocc = 161, packed naux = 4608 needs 158 KiB and compiles and
2152
2155
  # runs at 118 ms, where aten runs out of device memory (#80).
2153
2156
  _AO_TO_MO_SBUF_BUDGET = 176 * 1024
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: trntensor
3
- Version: 0.25.1
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
@@ -53,6 +53,7 @@ tests/test_einsum.py
53
53
  tests/test_host_output.py
54
54
  tests/test_kernel_dtype_guard.py
55
55
  tests/test_native_dispatch.py
56
+ tests/test_neuron_int_exact.py
56
57
  tests/test_neuron_span.py
57
58
  tests/test_neuron_span_device.py
58
59
  tests/test_nki.py
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