Repository navigation
v0.14.0 — Owen-scrambled Sobol, unseeded and partition fixes, fp64 policy
Validated on Trn2 (trn2.3xlarge, Neuron SDK 2.33.0, native TorchNeuron) — see trnsci/trnsci#35.
Headline: sobol(scramble=True) is now Owen scrambling on both backends (#100; the old behaviour is scramble="shift"). Silent fixes: Generator(seed=None) drew the same stream every time (0.1.0–0.13.0, #98), and partition_rank was ignored on the PyTorch path (0.7.0–0.13.0, #99). Also the trnsci float64 policy with a dtype guard at the kernel boundary, and sobol/halton NKI limits now fall back instead of asserting.
Lands in 0.14.0. Changes the stream for float64 callers on the NKI path
(set_backend("auto") on a Neuron host): see Changed.
Changed
-
sobol(scramble=True)is now Owen scrambling, on both backends (#100).
A hash-based nested uniform scramble (Laine–Karras, with Burley's 2020
constants) of the bit-reversed Sobol integers. It keeps the (t, m, s)-net
property (every elementary interval keeps its point count; the old NKI
scramble, a random shift mod 1, did not) and its centred discrepancy
matchesscipy.stats.qmc.Sobol(scramble=True). The NKI path runs it in a
newsobol_owenkernel and gives the same points as the PyTorch path's
float32 output for the same seed.seed=0now scrambles on NKI (it gave
the plain sequence), andseed=Nonedraws the scramble seed from torch's
global RNG on both backends.
Seeded scrambled streams change:sobol(n, d, seed=s)returns different
points from 0.13.0 on every backend. Migration: to reproduce an old
stream, passscramble="shift", which is the 0.13.0 behaviour bit for bit
(PyTorch path:SobolEngine(scramble=True, seed=s); NKI path: the random
shift, unscrambled forseed=None/0).scramblealso accepts"owen"
(same asTrue); other strings raiseValueError.
Thesobol_owenNKI kernel is checked on CPU against a bit-exact host
reference (sobol_owen_reference); its device tests
(tests/test_nki_quasi.py::TestSobolOwenNKI, markedneeds_hw_validation)
have not run on Neuron hardware yet.
Owen-scrambled float64 follows the float64 policy below (#105): under
set_backend("auto")adtype=torch.float64request on the NKI path takes
the PyTorch path (full 32-bit points); underset_backend("nki")the
float32 kernel output is cast with a one-timeUserWarning; under
TRNRAND_REQUIRE_NKI=1it raisesNotImplementedError. Only unscrambled and
scramble="shift"Sobol keep the kernel for float64. -
float64 policy (trnrand#105, the trnsci suite policy). float64 under
autonow computes in torch at float64 accuracy (previously cast to fp32 on
the NKI path). Affects released versions 0.5.0 through 0.13.0, on the
NKI path only (set_backend("nki"), or"auto"on a Neuron host): a
dtype=torch.float64request (or a float64 buffer for*_into/
GeneratorProgram.stream_into) was drawn by the float32 kernels and cast up,
so the values had float32 resolution (24-bit uniforms) while
set_backend("pytorch")draws float64. From 0.14.0:- backend
"auto":uniform,normal,standard_normal,
truncated_normal,exponential,gamma,chi_squared,beta,
poisson(lam >= 20; smaller lam always used torch),halton,
normal_into/uniform_into/exponential_intodraw float64 in torch,
with the same values asset_backend("pytorch"). This changes the
stream for float64 callers on Trainium underautoand they no longer
get the on-device generator. A float64GeneratorProgrambuffer is filled
in torch at float64 precision, keyed by the program seed and the step's
counter slots; the program's other buffers and its counter are unchanged. - backend
"nki"(explicit): the float32 kernels run as before (same
values) and the result is cast to float64, with a one-timeUserWarning
per op saying the precision is float32. TRNRAND_REQUIRE_NKI=1: a float64 request raisesNotImplementedError
on either backend (before anything is drawn, for a program).- Result dtype: always the dtype the PyTorch backend returns for the same
request (the requested dtype, or the buffer's), for float32, float64,
float16 and bfloat16. trnrand takes no tensor operands that promote. - unscrambled and
scramble="shift"sobolare unchanged on every
backend: the PyTorch backend's float64 Sobol there is itself a float32
SobolEnginedraw cast to float64, so there is no float64 Sobol to route
to; they keep the kernel, without a warning or a REQUIRE_NKI error.
Owen-scrambledsobol(the default, #100) is routed likehalton. box_muller_nki(internal) warns once for float64 input.- float64 buffers on the neuron device (
*_into,GeneratorProgram):
underautothey are filled on the host at float64 precision and copied
into the device buffer (which stays float64), with oneUserWarningper
op: "float64 on the neuron device is computed on the host for float64
accuracy (device arithmetic is fp32); pass float32 to stay on device".
Device arithmetic on Trainium is float32 (measured on Trn2), so this is
the only way to keep the float64 promise for device-resident buffers. The
values equal a host buffer's with the same generator state.
TRNRAND_FP64_ON_DEVICE=1keeps such buffers on the on-device float32
kernels instead (float32 precision, no warning). Host float64 buffers and
every allocating call (they return host tensors) are unchanged.
- backend
-
normal_into/uniform_into/exponential_intoon the PyTorch path
fill a non-CPU buffer on the host and copy it over (the generator is a host
torch.Generator); CPU buffers are filled in place as before.
Added
- One dtype guard at the NKI kernel boundary (trnsci/trnsci#35).
trnrand/nki/dispatch.pyhas a per-kernel operand-dtype table
(_KERNEL_DTYPES: int32 words for every kernel, float32 for Box-Muller).
Every launch route enforces it itself (_launch, the nativenki_optable
returned by_get_native_ops, and the simulator via_simulator), raising
TypeErrornaming the op and the dtypes, and an op without a table entry
cannot be loaded.tests/test_kernel_dtype_guard.py(CPU) drives every
public API through stand-in kernels on the native, XLA and simulator routes
with float64 / bfloat16 / float16 / int32 / int64 / bool / complex64
requests, and the float64 policy in all three modes. tests/test_conformance.py: float64 is checked at float64 tolerance under
--backend pytorchand--backend auto; only--backend nkiuses the
float32 tolerance for float64.
Fixed
- NKI path: integer, bool and complex output requests no longer return
truncated or zero-imaginary values (silent). Affects released versions
through 0.13.0, on the NKI path only (set_backend("nki"), or"auto"
on a Neuron host).uniform,normal,standard_normal,
truncated_normalandnormal_into/uniform_into/exponential_into
cast a float32 draw to the requested dtype:uniform(dtype=torch.int64)
returned zeros andnormal(dtype=torch.complex64)a zero imaginary part,
where the PyTorch path raises or draws a complex variate. These requests now
take the PyTorch path (the same error or the same values as
set_backend("pytorch")), andexponential(dtype=complex)raises as there.
GeneratorProgram.stream_intoraisesNotImplementedErrorfor a non-real
buffer before drawing anything. UnderTRNRAND_REQUIRE_NKI=1all of these
raiseNotImplementedError. float32, float16 and bfloat16 requests are
unchanged and give the same values as before (float64: see Changed). uniform_into/GeneratorProgramuniform into a float16 / bfloat16
buffer could returnhigh(NKI path), asuniform()did before #94; they
now step back belowhighthe same way. Other values are unchanged.box_muller_nkireturned float32 for float64 / float16 / bfloat16 input;
it now returns the input dtype (asbox_muller_cpudoes) with the same
values, refuses integer / bool / complex input withTypeError, and refuses
float64 underTRNRAND_REQUIRE_NKI.philox4x32_nkirefuses float, bool and
complex counter / key words withTypeError(integer words still go to
int32 as before).Generator(seed=None)drew the same stream every time (silent). Affects released versions 0.1.0 through 0.13.0, on every backend. A freshtorch.Generatorstarts at torch's fixed default seed, so every unseededGenerator, in every process, and the module default generator behind un-seededtrnrand.uniform(...)etc. drew one identical stream: one unseeded generator per worker gave perfectly correlated workers.seed=Nonenow seeds from OS entropy (torch.Generator.seed());gen.torch_generator.initial_seed()reports the seed chosen, andget_state/set_statereplay as before. Code that never seeds now gets different output each run. Every seeded stream (Generator(seed=s),manual_seed(s)) is bit-identical (#98).partition_rankwas ignored on the PyTorch path: every rank drew identical samples (silent). Affects released versions 0.7.0 through 0.13.0, on the PyTorch path (set_backend("pytorch"), every CPU host) for every distribution, and on the NKI path for the draws that always go throughtorch.Generator(bernoulli,randint,randperm,poissonwithlam < 20) and the CPUGeneratorProgramfallback. A data-parallel job on CPU ranks, or P simulated ranks on a dev host, got P copies of one sample. The PyTorch path now keeps the NKI path's partition contract (#72): rank r of P draws theP × nsingle-generator sample and keeps its contiguous slice[r·n, (r+1)·n), so the ranks concatenate in order to the single-generator draw and every call moves past all P slices (P× the work on that path).randpermreturns the r-th of P consecutive permutations. Output changes for rank > 0 and, on the PyTorch path, for rank 0 of P > 1; single-rank (P = 1) streams are bit-identical (#99).sobol()on the NKI path raisedAssertionErrorforn_dims > 10or more than 2^24 points instead of falling back (loud). Affects released versions 0.5.0 through 0.13.0, on the NKI path only (set_backend("nki"), or"auto"on Neuron hardware). Only a missing kernel (ImportError) fell through to the CPU path. Calls outside the kernel's limits now usetorch.quasirandom.SobolEnginewith anNkiFallbackWarning, or raiseValueErrorunderTRNRAND_REQUIRE_NKI=1.sobol(0, d)now returns an empty(0, d)tensor on every backend (it raised on the CPU path and asserted on NKI). Calls inside the limits are unchanged (#89).halton()on the NKI path raisedAssertionErrorforn_points = 0,n_dims > 10orn_points >= 2^22instead of falling back (loud). Affects released versions 0.5.0 through 0.13.0, on the NKI path only (set_backend("nki"), or"auto"on Neuron hardware).halton(0, d)now returns an empty(0, d)tensor, as the CPU path does. Calls past the kernel's limits use the CPU path with anNkiFallbackWarning, or raiseValueErrorunderTRNRAND_REQUIRE_NKI=1. Calls inside the limits are unchanged (#104).