Repository navigation
v0.8.0
Validated on Trn2 (trn2.3xlarge, Neuron SDK 2.33.0, native TorchNeuron device) — see trnsci/trnsci#35.
Added
Native PyTorch dispatch on TorchNeuron (#54)
On TorchNeuron (Neuron SDK 2.33+, torch-neuronx 2.14, PyTorch 2.14,
Trn2/Trn3), every NKI kernel is now registered as a PyTorch custom op with
torch_neuronx.nki_op and dispatched on device="neuron", with no
torch_xla import. Ops: trnrand::philox4x32, box_muller,
threefry4x32, threefry_normal, sobol_gray_code, halton,
threefry_streaming_normal, threefry_streaming_uniform. Kernel bodies
are unchanged.
- The dispatch path is detected on the first kernel launch: native when
torch_neuronxexportsnki_opand aneurondevice is available, XLA
whentorch_xlais installed, otherwise none. The XLA path (Trn1) is
unchanged, as are thetorch.Generatorfallback and the
TRNRAND_USE_SIMULATORpath. - Custom ops are registered lazily (
trnrand/nki/_native.pyis imported
only when the native path is selected), so CPU-only installs and the XLA
lineage see no import-time side effects. - All host wrappers,
GeneratorProgram.stream_into, and the partitioned
multi-chip paths share one launch helper, so they follow the same path. TRNSCI_NKI_DISPATCH=auto|native|xlaforces the dispatch path (a
suite-wide variable shared by all trnsci libraries). Forcing a path that
isn't available raises instead of falling back.- New
[torchneuron]extra (torch>=2.14,torch-neuronx>=2.14,
nki>=0.6);[neuron]stays the PyTorch/XLA lineage. - Docs: install notes cover both extras and lineages and recommend
NKI_ENABLE_TRACE_CACHE=0for editable installs. - Tests:
conftest.pysetsNKI_ENABLE_TRACE_CACHE=0by default so test
runs from a checkout never hit a stale cached kernel. - CI: the scheduled run is weekly (Mondays) instead of daily.
- Tests: CPU-runnable backend-selection and lazy-registration tests;
@pytest.mark.neuronnative-path tests for Trn2 (not yet run on
hardware).
Changed
Bulk uniform/normal use multi-tile launches (#60)
threefry_uniform_nki and threefry_normal_nki (behind trnrand.uniform,
normal, exponential, and the gamma/beta/chi-squared/Poisson/
truncated-normal NKI paths) launched one kernel per 512 values, so
normal(4_194_304) was 8,192 launches (about 9 s on Trn2 at the native
dispatch floor). They now go through the streaming kernels
(threefry_stream_*, 16,384 values per launch): 256 launches for 4M values,
ceil(N / 16,384) in general. Output values and order are unchanged for every
seed and counter_offset (#58). Tests: CPU launch-count and bit-identity
tests with a stand-in launcher (tests/test_launch_granularity.py), and
simulator bit-identity against the per-tile path.
- On the native path the streaming launches keep their inputs and outputs
on the device. The lane-index and key inputs are copied to the device
once per call, the start-batch inputs for all launches are copied once as
one(launches, 128, 1)tensor, and the per-launch outputs are stacked on
the device and copied back in one transfer: 6 host-to-device copies and 1
device-to-host copy per call instead of 6 and 1 per launch. Measured on
Trn2 before this step:normal(4M)went from 9.2 s to 1.6 s with
multi-tile launches alone. - The per-tile wrappers are kept as
_threefry_uniform_per_tile/
_threefry_normal_per_tile. They are still used on the XLA path when
neuronx-ccis older than 2.28, where the streaming kernels hit SBUF OOM
at compile (#57).TRNRAND_BULK_STREAMING=0|1forces the choice. - The Threefry host wrappers now live at module level in
trnrand/nki/dispatch.py, outsideif HAS_NKI:, so they import and can
be tested on hosts without NKI.
Bulk uniform/normal run as one wide-tile kernel launch (#60)
The streaming kernels held every byte of every Threefry word as a (128, 1)
tile, so each NKI instruction did 128 elements of work and a 16,384-value
launch was about 55k instructions. On Trn2 that made normal() linear at
about 0.23 ms per 1k values (934 ms for 4M) no matter how launches were
grouped. On the native path and the simulator, threefry_stream_* (and so
trnrand.uniform, normal and the distributions built on them) now use
new bulk kernels, threefry_bulk_normal_kernel and
threefry_bulk_uniform_kernel (ops trnrand::threefry_bulk_normal and
trnrand::threefry_bulk_uniform). They run the same byte-tile arithmetic on
(128, F) tiles, with F up to 512 Threefry batches per chunk and up to 16
unrolled chunks per launch. One launch covers up to 4,194,304 values.
- Counters are built in SBUF: the column index comes from
nisa.iotaand is
added byte-wise to each chunk's start batch. Key words are added as float32
(128, 1)tensor_scalaroperands (byte plus byte is at most 510, exact),
so the key schedule is never broadcast to(128, F). Box-Muller runs in
the same kernel, and each chunk's four output words leave in one DMA. - Values and order are bit-identical to the streaming and per-tile kernels
for every seed,counter_offsetand size. The counter for each element is
unchanged, including the 24-bit batch wrap. F and C are rounded up to
powers of two (F from 32 to 512, C from 1 to 16), so at most 9 NEFF shapes
are compiled. TRNRAND_BULK_COLSlowers the column cap if a compiler reports SBUF
pressure.TRNRAND_BULK_KERNEL=0goes back to the 16,384-value streaming
kernels. The XLA path (Trn1) still uses the streaming kernels.- Tests:
tests/test_bulk_kernel.pychecks the wrapper against an
independent numpy Threefry4x32-20 plus Box-Muller reference on CPU for
sizes that are and aren't multiples of the tile, for multi-chunk and
multi-launch plans, and for partitioned generators. On the simulator it
checks the real kernels against the streaming kernels. Both bulk kernels
are added to the MLIR verifier check for trn1 and trn2
(tests/test_nki_compile.py).
Bulk kernels run on full uint32 words by default (#60)
On the native TorchNeuron path (Trn2+), the bulk Threefry kernels now hold
each word as one uint32 tile instead of four byte tiles. TRNRAND_LIMB_BITS
selects the width: 32 (default), or 8 for the byte-limb kernels as a
fallback. Values are bit-identical for every width. The XLA/Trn1 path never
uses the bulk kernels (it streams on the byte kernels), so it is unchanged.
-
Bytes were used because integer
nl.add/tensor_scalararithmetic runs
in float32 (exact only below 2^24, aws-neuron-sdk#1308), and the byte
form was carried over from the Philox multiply, which needs 8-bit
sub-products. NKI 0.7 documentsnisa.tensor_tensorwith all-int32/uint32
operands as native integer arithmetic on GpSimd. The full-word kernels
(threefry_bulk32_{normal,uniform}_kernel, ops
trnrand::threefry_bulk32_*) pin their adds there and do xor/rotate as
bitvec ops. That is about 510 compute instructions per (128, 512) chunk
in trn2 MLIR, against about 1,950 for bytes. On Trn2 (SDK 2.33.0) the add
wraps mod 2^32: results matched the 8-bit kernels exactly in
test_limb_variants_bit_identical(all 4 cases), the full-m neuron
suite (88 passed, 1 xpassed) and the 17-case edge sweep. -
normallatency on Trn2, 32-bit vs 8-bit:n 32-bit 8-bit 16k 0.78 ms 0.90 ms 262k 1.25 ms 2.46 ms 1M 5.0 ms 10.6 ms 4M 15.9 ms 37.1 ms -
TRNRAND_LIMB_BITS=16(two 16-bit limbs, every add below 2^17) is kept
for diagnostics only. It is also bit-identical, but slower than 32 at
every size. -
Plan buckets: the chunk count C no longer rounds up to a power of two.
Launches split the chunks evenly, and C rounds up to one of
1-8, 10, 12, 14 or 16, so a request computes at most about 25% more than it
needs. Before, 2^20 + 1 values ran 8 chunks (1.7x the 2^20 latency); now
they run 5. That makes at most 12 C shapes. -
Tests: a numpy port of the 16-bit limb kernel that does every add in
float32 is checked against the full-word reference, including all
rotations, carry edge cases and the counter wrap at 2^24. The new ops are
checked for routing and values on CPU, including partitioned generators,
and checked against the 8-bit kernels on the simulator (including
multi-chunk/multi-launch plans and 2^24 - 5 offsets). They are added to
the trn1/trn2 MLIR verifier check.tests/test_nki_native.pyadds a
device bit-identity test for 16 and 32 against 8. The CPU tests check that
32 is the default and that the XLA path does not select the bulk kernels.
Streaming output is in per-tile order (#58)
Reproducibility break. For a given seed and counter, the streaming
generators (threefry_stream_normal, threefry_stream_uniform, and
everything built on them: GeneratorProgram.stream_into, the in-place
*_into distributions on the NKI path, and the partitioned multi-chip
paths) now return values in a different order than before. The values
drawn are unchanged; only their order changes.
- Streams now match the per-tile path (
threefry_normal_nki,
threefry_uniform_nki) element for element for the same seed and
counter offset: all 128 lanes of counter batch 0, then batch 1, and so
on. Before, each 16,384-element launch was flattened lane-major
((P, tiles, 4)) instead of tile-major ((tiles, P, 4)). - The fix is a host-side permute after each launch
(_stream_tile_orderintrnrand/nki/dispatch.py). The kernels are
unchanged. - Partitioned streams still concatenate to the single-chip result.
- Checked on the NKI simulator. The Trn2 run of
TestHardwareBitExactand the native-path program test is pending.
Fixed
torch.compile(fullgraph=True) graph-breaks on normal() / uniform() (#61)
torch.compile(lambda: trnrand.normal(n), fullgraph=True) (and uniform,
standard_normal) failed with Unsupported: Failed to convert args/kwargs to proxy on every backend (neuron on Trn2, eager on CPU). Root cause: the
draw passed a torch.Generator to a traced call (Tensor.normal_ /
uniform_(generator=...) on the PyTorch path, torch.randint(generator=...)
for the 24-bit kernel seed on the NKI path), and Dynamo cannot proxy a
Generator. torch.compiler.disable is not a fix under fullgraph=True.
- While compiling, the generator-state step runs in opaque custom ops that
take the generator's integer handle (newtrnrand/_compile.py):
trnrand::gen_fill(PyTorch-path fill),trnrand::gen_nki_state(seed
draw + partition counter offset + advance, returned as an int64 tensor) and
trnrand::gen_nki_draw(whole draw, for the XLA / simulator / per-tile
routes). Each declares a mutation of a per-generator token tensor so
AOT/Inductor keep every draw, in order, even if its output is unused. - On the native TorchNeuron path the kernel launches stay in the graph:
dispatch.threefry_stream_from_statebuilds the kernel inputs with torch
ops from the state tensor (seed and offset are not baked in as constants,
so repeated calls neither recompile nor repeat values) and shares the
launch/copy code (_native_launches) with the eager wrapper. It takes the
eager route: the bulk Threefry kernels (#60) with the same_bulk_plan
launches andTRNRAND_LIMB_BITSvariant, or the streaming kernels under
TRNRAND_BULK_KERNEL=0. The plan and variant are fixed at trace time. - Eager calls are unchanged. Compiled output is bit-identical to eager for
every seed, and the generator ends in the same state. backend="neuron"lowers the whole graph to StableHLO and rejects any
custom op that is not annki_opkernel, so thegen_*ops only serve the
CPU compile backends (eager / aot_eager / inductor). The default per-call
NKI key is atorch.randinton the Generator's Mersenne Twister, which has
no in-graph aten equivalent, so it cannot lower without changing the stream.
New opt-inGenerator(key_mode="counter")(orTRNRAND_KEY_MODE=counter):
the Threefry key is fixed by the seed (64-bit seed folded to 48 bits,
k0/k1) and only the counter advances between calls. The counter is now a
0-d tensor on the Generator (_counterstays as the int view), so on the
native path the compiled draw is plain aten ops (the bulk-kernel lane,
key and start tiles built from the seed's key words and the_ctrtensor)
plus thenki_oplaunches (dispatch.threefry_stream_from_key),
advancing the counter with an
in-placeadd_.key_mode="counter"gives a different stream from the
default"draw"(same seed, different values) and does not consume the
hosttorch.Generator; the default is unchanged. It applies to
uniform/normal/standard_normal; other distributions still draw
their key. The PyTorch-backend path (set_backend("pytorch")) remains
non-lowerable onneuron.Generatorgains a publickey_modeand private_handle(registry
lookup),_compile_token,_ctrand_key_words. Needs PyTorch 2.4+
(torch.library.custom_op); older PyTorch keeps the previous behaviour.- Tests:
tests/test_torch_compile.py(CPU:backend="eager", zero graph
breaks viatorch._dynamo.explain, compiled == eager for fixed seeds on the
PyTorch, native-NKI (CPU stand-in kernels: every bulk limb variant,
multi-chunk / multi-launch plans and the streaming opt-out) and per-tile
NKI paths, in both key modes; the AOT ATen graph in counter mode contains only aten ops and
the kernel ops; plus aneuron-markedbackend="neuron"check using
key_mode="counter").
NKI kernels fail to compile on neuronxcc 2.26 / NKI 0.5+ (#56)
Hardware compilation failed with 'nisa.tensor_scalar_arith' op 'operand0' must be float32, got 'ui32' for the Philox, Threefry, Sobol and streaming
kernels. nl.add / nl.multiply lower any (P, 1) operand (a Python
scalar or a (P, 1) tile, which is every tile in these kernels) to
nisa.tensor_scalar, whose arithmetic form now requires a float32
operand. Integer adds and multiplies now call nisa.tensor_tensor
directly. Bitwise ops and shifts lower to tensor_scalar_bitvec, which
accepts integer operands, so they are unchanged. The arithmetic and
outputs are unchanged.
- Every kernel now generates MLIR that passes verification on CPU
(nki.compiler.frontend.source_to_mlir, trn1 and trn2 targets, NKI
0.5.0 / 0.6.0 / 0.7.0). Before the fix, 6 of 8 kernels failed
verification there with the same error as on hardware. - Tests:
tests/test_nki_compile.py(nki_simulatormarker) runs that
frontend check for every kernel on trn1 and trn2, so the simulator CI job
now catches MLIR verification failures thatnki.simulatemisses. It
does not run the backend compiler. - Full CPU compile to NEFF (frontend, then
neuronx-cc, no device): all 8
kernels compile for trn1 and trn2 withneuronx-cc2.28 / NKI 0.7.0. With
neuronx-cc2.26 / NKI 0.5.0, 6 of 8 compile. The two streaming kernels
then fail with an unrelated SBUF out-of-memory error in NKI 0.5.0's pass
pipeline: 32 unrolled tiles leave too many allocations live at once
(2 tiles fit). That failure is not fixed here. - Not yet re-run on hardware.
Philox output corrupted by saturating int32/uint32 casts (#56)
On Trn2 hardware (Neuron SDK 2.33, NKI 0.7.0, neuronx-cc 2.28) every
philox4x32_kernel output word was wrong: many were exactly 0x7FFFFFFF
and the uniform mean was about 0.33. The kernels crossed between int32 (the
kernel input/output dtype) and uint32 (the arithmetic dtype) with
nl.copy(x, dtype=...), which is a value conversion, not a bit
reinterpretation: uint32 values >= 2^31 saturate to 0x7FFFFFFF and
negative int32 values to 0. Every crossing now uses tile.view(dtype), a
bit reinterpretation with no data copied. The arithmetic is unchanged.
- Philox: inputs are reinterpreted as uint32 after
nl.load, the state and
key bump stay uint32 through all 10 rounds (_mul32_hi_lonow returns
uint32), and the outputs are reinterpreted as int32 at the store. This
removes 6 casts per round. - Threefry, Threefry-normal and both streaming kernels: the int32 -> uint32
cast in_b_split(counter and key words) is now a reinterpretation, so
words >= 2^31 are no longer read as 0. The host wrappers keep these words
below 2^24, so existing outputs are unchanged. - Streaming kernels:
nl.load(start_batch_ref, dtype=nl.uint32)is now a
plain load followed by.view(nl.uint32). - Sobol: the Gray-code load is reinterpreted rather than converted. Gray
codes are below 2^24, so outputs are unchanged. - The simulator (
nki.simulate, NKI 0.7.0) saturated the same way, so the
Philox simulator tests that were xfailed as aws-neuron-sdk#1308 were in
fact failing on this bug. They now pass and are no longer xfailed. New
tests feed full-range counters and keys (top bit set) to Philox and
Threefry and require bit-exact output, with about half the Philox words= 2^31. They fail on the previous kernels.
- Checked with
neuronx-cc2.28's BIR simulator (no device), trn2 target:
the previous Philox kernel matches 0 of 512 words against
philox4x32_reference, with 153 words equal to0x7FFFFFFF(the
hardware symptom), and the fixed kernel matches 512 of 512. - Not yet re-run on hardware.
NKI Sobol differs from SobolEngine in dimensions 2-5 (#59)
The unscrambled NKI Sobol output (sobol_nki, sobol() with the NKI
backend) did not match torch.quasirandom.SobolEngine in dimension
indices 2-5 (Joe-Kuo dimensions 3-6). The initial direction numbers
m_init for those four rows of the table in _init_sobol_directions were
wrong (for example [1, 1] instead of [1, 3] for index 2). The primitive
polynomials and the Gray-code recurrence were correct. The rows now match
Joe-Kuo new-joe-kuo-6.21201, the table SobolEngine uses, and all 10
dimensions equal the top 24 bits of SobolEngine's output. Dimensions 0, 1
and 6-9 are unchanged.
- The CPU
sobol()path was not affected: it calls SobolEngine. - The direction-vector table and a new CPU reference,
sobol_reference,
are now at module level intrnrand/nki/dispatch.py, so CPU-only
installs can check them.tests/test_quasi.pycompares the table with
SobolEngine.sobolstateandsobol_referencewith SobolEngine (after
fast_forward) for every dimension. Both tests fail on the old table. - Simulator tests (
tests/test_nki_sim.py) check the kernel against
sobol_referenceandsobol_nkiagainst SobolEngine for all 10
dimensions, including a nonzerostart_index. TestSobolNKI.test_start_index_advances_sequencecalled the public
sobol()withstart_index, whichsobol()has never accepted. It now
callssobol_nki, which has the parameter.test_conformance_vs_cpunow
checks all 10 dimensions instead of 3.