Repository navigation
v0.24.0 — AuxStream DF streaming, cached NKI handles, fp64 policy
Validated on Trn2 (trn2.3xlarge, Neuron SDK 2.33.0, native TorchNeuron) — see trnsci/trnsci#35.
Headline: trntensor.stream.AuxStream for DF integrals streamed over the aux index (#102), cached eager NKI handles, and the trnsci float64 policy with a dtype guard at the kernel boundary.
Fixed
mp2_energy's aten route chunks j as well as i (trnsci/trnsci#115). It
chunked only the occupied index i. Once one i row ofT(nocc · nvir²)
exceeded_MP2_CHUNK_ELEMS, every step held that whole row, 1.37 GB at
nocc 161 / nvir 1461 / naux 6064 (C40H82 / aug-cc-pVDZ), plus its temporaries.
On a 24 GiB Trn2 core, with only the 5.7 GB ofBresident, the step ran out
of device memory. Blocks are now at most_MP2_CHUNK_ELEMSelements, and the
same case ran on one core in 222 s.
Changed
AuxStreamuploads a memory-mapped chunk to a device straight from the
mapping (trnsci/trnsci#115). The sequential read into RAM, added so that
host row gathers stop faulting pages at random, now runs only for chunks
computed on the host. On Trn2, a page-cached 1.6 GB chunk went from 2.9 GB/s
(copy, then upload) to 12–14 GB/s (upload from the mapping). For water61 /
aug-cc-pVDZ (114.6 GB of B, 49.7 GB streamed per SCF iteration over 4
cores), the per-iteration upload wait went from 12.5 s to 4.3 s.
Performance
- Eager NKI kernel calls skip the per-call handle rebuild and the custom-op
layer (same fix as trnblas#92). On the native path atrntensor::
custom-op call cost about 0.1 ms of host time before the kernel was queued,
against 0.016 ms for aten. Most of it went to nki'sKernel.__call__,
which rebuilds the torch handle on every call, plus thetorch.library
layer.dispatch._launchand themp2_energy_rowloop now call a
torch_neuronx.wrap_nkihandle built once per kernel; compile-time keyword
arguments (use_sr,nub,tp,tail,banks) pass through unchanged.
On trn2 (LNC=2), a 256x128x512matmullaunch drops from 0.143 to
0.036 ms of issue time (e2e 0.222 to 0.128 ms),_nki_mp2_energy_rowswith
nocc=8 from 2.94 to 2.34 ms, and thedf_exchangekernel pair's issue time
from 0.78 to 0.36 ms. Results are bit-identical.torch.compile,
requires-grad, int64 and tensor-subclass inputs keep the custom op.
TRNTENSOR_NKI_EAGER_HANDLE=0turns this off;
scripts/bench_eager_handle.pymeasures it.
Added
trntensor.stream: DF integrals streamed over the aux index
(trnsolver#15). At nbasis 1658 the packed fp32 integrals were 33.4 GB and
only fit split over a Trn2 chip's 4 logical cores; at nbasis ≈ 3300 they
are about 270 GB.AuxStream(chunks, nbasis, devices=..., resident_bytes=..., prefetch=...)
holdsBas(npair, nq)aux chunks (tensors, numpy memmaps or lazy
loaders), keeps chunks resident up to a per-device budget and uploads the
rest on every call from a loader thread, one chunk ahead per device, so
uploads overlap compute.jk()builds packed J and K in one pass
(df_coulomb/df_exchangeper chunk, summed per device, then on the
host);ao_to_mo()writesB_ia^Pchunk by chunk into a host tensor or
memmap. Chunks are dealt round-robin overdevices(neuron:0..3).aux_chunk_width/split_aux: 128-multiple chunks under 2 GiB by
default, below the 4 GiB transposed-LHS limit (aws-neuron-sdk#1457, #97).pack_density/unpack_packed: the packed-density helpers, for use on
host tensors at large nbasis (the device gathers compile for > 40 min at
nbasis 1658, trnsolver#87).mp2_energy_blocked: DF-MP2 over occupied blocks of a hostB_ia^P,
forBlarger than one device.- Memory-mapped chunks are read into RAM with one sequential read when
loaded. Left mapped, the row gathers ofdf_exchangefault pages in at
random, which was disk-bound on a disk-backed store. - CPU-validated against dense references; device overlap and scaling are
still to be measured.
Changed
-
float64 follows the trnsci float64 policy (#92, trnsci/trnsci#35).
Trainium computes float64 at fp32 precision, so a float64 operand on a
kernel route cannot get float64 accuracy there. This applies to the matmul /
batched-matmul kernels (einsum,multi_einsum) and to the chemistry
kernels (mp2_energy,ao_to_mo_transform,df_exchange,df_coulomb):-
"auto": float64 underautonow computes in torch at float64
accuracy (previously cast to fp32 on the NKI path). For the chemistry
kernels the cast came in with #88 and was in 0.23.0. Through 0.22.0
float64 reached them unchanged and returned inf. torch runs on the
operands' own device; float64 host operands stay on the host. -
Explicit
set_backend("nki")(orTRNTENSOR_FORCE_BACKEND=nki):
float64 is cast to fp32, the kernel runs, and the result comes back as
float64. A one-timeUserWarningper op says the precision is fp32.
Through 0.23.0 the chemistry kernels cast without a warning; the matmul
kernels got float64 operands unchanged. -
TRNTENSOR_REQUIRE_NKI=1: float64 on a kernel route raises
NotImplementedError(trnblas#69's convention), under explicit"nki"
and"auto"alike. Callers who ran float64 chemistry inputs under
TRNTENSOR_REQUIRE_NKI=1(0.23.0: the kernel ran in fp32) now get an
error. Pass float32 operands to run the kernels, or unset the variable. -
Result dtype: always the pytorch backend's dtype for the same inputs,
including bf16 / fp16 and mixed-dtype promotion (torch.promote_types
of the operands). -
float64 already on the
neurondevice: under"auto", "torch on
the operands' device" would be device aten, which computes float64 at
fp32 precision (measured on Trn2). float64 / complex128 operands on the
neurondevice are now copied to the host, computed in host torch at
float64 accuracy, and the result is returned on the original device in
float64 / complex128. This coverseinsum,multi_einsum(which returns
host tensors, as before),gather(devicetorch.catrounds float64 to
fp32),einsum(dtype=float64)on fp32 / bf16 device operands, the
chemistry calls and the CP / Tucker / TT
decompositions and reconstructions. OneUserWarningper op per process:
"float64 on the neuron device is computed on the host for float64
accuracy (device arithmetic is fp32); pass float32 to stay on device".
New:TRNTENSOR_FP64_ON_DEVICE=1keeps device aten (fp32 precision,
no warning) for callers who prefer speed. Under
TRNTENSOR_REQUIRE_NKI=1, float64 / complex128 on theneurondevice
now raisesNotImplementedErrorat every guarded entry point (backends
"auto"and"nki", no host copy, ahead ofTRNTENSOR_FP64_ON_DEVICE);
before, only the kernel routes raised, and aneinsumwith no kernel
route (below the size thresholds, or not a matmul) or a decomposition
ran device aten at fp32 precision with no error. Host-resident float64 is
unchanged, andmulti_einsumno longer pins host float64 / complex128
operands to the device under"auto"; through 0.23.0 shared operands
were pinned, so device aten computed them at fp32 precision.
--backend auto --conformance-device neuronnow holds float64 and
complex128 to the float64 tolerance.
The routes that place their own compute follow the same rule:
trntensor.stream.AuxStream(..., dtype=float64)and
mp2_energy_blockedwith aneurondevice indeviceskeep the chunks
and the arithmetic on the host (results still default to the first
device given), andpack_tril/unpack_tril/pack_density/
unpack_packedgather float64 / complex128 on the host, because on the
neuron device both the gather (index_select, fancy indexing) and
torch.catround float64 to fp32 (measured on Trn2, rel err ~4e-8).
Affected versions: the old behaviour is 0.23.0 for the chemistry kernels
(the silent fp32 cast) and 0.23.0 and earlier for the matmul kernels
(float64 unchanged). The new behaviour lands in the next release (0.24.0).
Docs: "Precision and dtypes" indocs/api/nki.md.
tests/test_conformance.pynow holds float64 to the float64 tolerance
for host inputs under--backend auto, as underpytorch. -
Fixed
-
One dtype guard at the NKI kernel boundary: float64, integer, bool and
complex operands no longer reach a kernel that does not take them
(silent, trnsci/trnsci#35; the class of #87).nki.dispatchnow has a
per-kernel operand-dtype table (KERNEL_OPERAND_DTYPES). Every launch
checks it and raisesTypeErrorif a dtype is not listed: the native op
and the eager-handle fast path (_native_call), the XLA call, the
simulator, themp2_energyrow loop and the reduce-parallel all-reduce. A native op without an entry cannot be loaded.
The host wrappers decide first:einsum/multi_einsum(matmul and batched-matmul kernels; fp32, bf16,
fp16, one shared dtype): integer, bool, complex and mixed dtypes run
torch, as the pytorch backend does; float64 follows the policy above.
Through 0.23.0 they reached the kernels underset_backend("nki"), or
under"auto"above the size thresholds.mp2_energy,ao_to_mo_transform(fp32 / bf16 kernels) and
df_exchange,df_coulomb(fp32 kernels): float16 operands, and
bfloat16 operands ofdf_*, are computed in fp32 (a lossless upcast).
The result is returned in the aten route's dtype. float64 follows the
policy above. Integer, bool and complex operands take aten. On the kernel
route, bf16df_coulombraised in the host tail whenevernpairwas not
a multiple of 256, then fell back to aten. Re-allowing bf16 on thedf_*
kernels waits on a Trn2 bf16 run (#92).- reduce-parallel all-reduce: partials other than fp32 / bf16 / fp16 are
summed bytorch.distributedinstead of the NKI collective kernel.
float64 partials are summed there at full precision. - Under
TRNTENSOR_REQUIRE_NKI=1, a dtype that would run torch instead of
the kernel raisesNotImplementedError(trnblas's convention).
Thedf_half_packedop's fake (meta) output is now float32, like the
kernel's. Test:tests/test_kernel_dtype_guard.py.
-
unpack_tril/pack_trilon the neuron device at LNC=1 returned
wrong data once the gathered output reached 4 GiB (#97). Affected
0.20.0–0.23.0:unpack_tril,pack_tril, and the packed-eriaten /
legacy routes ofao_to_mo_transform, which unpack withunpack_tril: on
the neuron device at LNC=1, oneindex_selectalong dim 0 whose output
reaches 2^32 bytes can be silently wrong (about half the elements, at
times NaN) or hit the runtime's 30 s execution timeout. Measured on Trn2:
exact at 2^32 − 16 KiB, wrong at exactly 2^32, and 154 M mismatched
elements forunpack_trilat nbasis 736 / naux 2304 (4.99e9 bytes). It
is exact at LNC=2 and on the host. Device gathers are now split into row blocks of at most
2 GiB of output and concatenated in order (exact). Host gathers are one
index_selectas before. -
Complex
tucker_decomposeon the neuron device usedU^T, notU^H
(#89). Affected 0.23.0 (the release that switched the core to
factors[mode].mH, #73):.mHof a complex tensor is a lazy conj view,
and device matmul ignores the conj bit (aws-neuron/aws-neuron-sdk#1455),
so the core of a device-resident complex tensor was silently computed
with the unconjugated factors. CPU results were correct. The conjugate
transpose is now materialized withresolve_conj()before the mode
product, as trnsolver'sfactor._mHdoes; real input is unchanged (no
copy). The complexcp_decomposeadded in this release (#103) had the
same pattern (V.mH @ V,T_mode @ V.conj()) and is fixed the same way.
A CPUTorchFunctionModetest fails if any lazy conj view reaches a
matmul, einsum, solve or pinv in either decomposition. -
cp_decomposeworks in the input dtype, including float64 and complex
(#103). Affected 0.1.1–0.23.0: the random init and the Khatri-Rao buffer
were float32 on the host, so float64 or complex input raisedexpected m1 and m2 to have the same dtypeon every backend (and a float64 warm start
was truncated to float32 inside the Khatri-Rao product). The init, the
Khatri-Rao buffer and the weights now followtensor.dtypeand
tensor.device, warm-start factors are cast to them, and complex input
uses the conjugated normal equations(V^H V) A^T = (T_(k) conj(V))^T.
Weights are real (tensor.real.dtype).nonneg=Truewith complex input
raisesValueError. Real float32 results are bit-identical. -
cp_reconstructreturns the factors' dtype on the factors' device
(#78). Affected 0.1.1–0.23.0: it accumulated into a float32 host buffer
(torch.zeros(shape)), so bf16 or fp16 factors came back as float32 and
device factors raised a device mismatch. The buffer now has the promoted
dtype offactorsandweightsand the factors' device. float32 and
float64 results are unchanged. -
plan_contractionfillsContractionPlan.estimated_flops(#83).
Affected 0.1.1–0.23.0: the documented multiply-add estimate was never set
and always read 0.matmul,bmmandtorchplans now carry
estimate_flops(subscripts, *operands)(the product of all index sizes);
pathplans carry the multiply-adds of the planned binary steps, which is
what runs and is less than the dense all-index product. Plans are cached by
shape, so the field is exact for the cached shapes. -
einsum(..., out=)now writes the result intooutand returns it
(#77). Affected 0.3.0–0.23.0:outwas only read as thebetaaddend
and never written, so the documented in-place accumulation example
einsum(..., beta=1.0, out=acc)silently leftaccunchanged.outnow
behaves as intorch.einsumand cuTENSOR: it receives
alpha * contract + beta * out(old contents not read whenbeta == 0),
must have the result's shape (ValueErrorotherwise), must accept the
result dtype undertorch.can_cast(RuntimeErrorotherwise), and may
alias an operand. Liketorch.einsum(out=), it raisesRuntimeErrorwhen
grad mode is on and an operand oroutrequires grad. Callers that reused
Cafter passing it asout=Cnow see it overwritten; passout=C.clone()
to keep the old behaviour.