nki(histogramming): NKI (Trainium) implementation - #280
bowencui123 wants to merge 6 commits into
Conversation
Split out of the consolidated NKI branch cecilia/feature/nki-vector-add (nki-all-operators, PR #259) so each operator can be reviewed on its own. Supersedes PR #222 (older per-operator branch). - also carries the operator's `impl_torch.py` change from the NKI branch Co-Authored-By: Cecilia123li <68335867+Cecilia123li@users.noreply.github.com> Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012Q38kGmXvyoeM1qtCbheSL
Tunables mirror the Triton search space (`BLOCK_SIZE`/`BLOCK_ROWS`/`BLOCK_BINS`); defaults are the previous constants, so autotune=False is unchanged. Triton's bins/rows split has no NKI analog (histogram accumulates per partition) Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012Q38kGmXvyoeM1qtCbheSL
This branch's impl_torch.py already builds a CPU-safe reference (no CUDA-only assert), so this was just never actually benchmarked before. Adds results/csv/histogramming_default.csv: 20/20 cases pass correctness verification. Performance is notably poor (~0.02x avg vs. the torch-on-Neuron baseline, i.e. NKI is ~45-100x slower) -- expected given the kernel counts via broadcast-compare-per-bin rather than atomics (no atomic histogram primitive on this hardware), which is O(N * num_bins) work instead of O(N). Correct, just not fast. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AQseF7nyesBh8KZAp8g7Cm
… LNC2) Merges NKI backend timing into results/csv/histogramming_autotune.csv, run with --autotune against this branch's impl_nki.py on trn2.3xlarge, LNC2 execution contract. 19/20 cases pass correctness verification and are included. One case (N=67108864, num_bins=4096 int32) is excluded: neuronx-cc's backend scheduler/register-allocator (walrus_driver) took multiple hours to compile a single candidate for this shape and was killed rather than let run indefinitely. This is the operator's largest input combined with autotune re-compiling per candidate; the same shape compiles and runs correctly (just slowly, ~45-100x under the torch-on-Neuron baseline) under the default (non-autotune) config, where it's covered by results/csv/histogramming_default.csv. Root cause is very likely the fully-unrolled Python loop over ~524k partition tiles for this N generating an extremely large static program for the compiler backend, not a correctness defect -- every case that did complete verified correctly. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AQseF7nyesBh8KZAp8g7Cm
| "N=67108864, num_bins=64",int32,0.7712,0.9464,1.8305,0.81,0.42,1.9342,7.8280,111.8305,0.07 | ||
| "N=67108864, num_bins=256",int32,0.6610,0.9612,1.8510,0.69,0.36,1.9257,7.8234,217.1039,0.04 | ||
| "N=67108864, num_bins=1024",int32,0.6350,0.9350,1.7902,0.68,0.35,1.9147,7.8260,870.1608,0.01 | ||
| "N=67108864, num_bins=4096",int32,0.6246,0.8009,1.4265,0.78,0.44,1.7810,,, |
There was a problem hiding this comment.
Lost result for N=67108864, num_bins=4096
There was a problem hiding this comment.
I think the main performance issue here is algorithmic rather than an autotuning issue.
The current NKI implementation iterates over ceil(num_bins / 128) bin blocks. For each bin block, it scans the entire N-element input again, broadcasts/materializes each input chunk across up to 128 partitions, compares every value against the bin ID owned by each partition, and then reduces along the free axis. Therefore, the effective number of full input passes scales with ceil(num_bins / 128).
This is quite different from the Triton/cuTile implementation. Triton and cuTile shard the input across up to 256 partial workers, so each input element is read once and performs one atomic add into a per-worker partial histogram. A second kernel then reduces the partial histograms across workers. The main work is therefore approximately:
O(N) + O(num_partials * num_bins)
instead of repeatedly scanning N for every 128-bin block.
The measurements are consistent with this. For N=67,108,864, autotuned NKI goes from:
- 111.8 ms for 64 bins
- 217.1 ms for 256 bins
- 870.2 ms for 1024 bins
This almost exactly follows the number of 128-bin blocks:
1 -> 2 -> 8
while Triton stays around 0.8-1.0 ms.
Could we rework the NKI implementation to follow the Triton/cuTile algorithm more closely? In particular, it may be worth investigating input sharding plus nl.atomic_rmw(..., op=nl.add) into per-worker partial histograms, followed by a reduction kernel.
I don't think tuning block_size alone can fix the current scaling, because the repeated full-input scan is structural.
That's the reason why autotune take more than 3 hours.
In N=67108864, num_bins=1024, nki takes at least 870ms, while triton only takes 0.935ms
# Conflicts: # results/csv/histogramming_autotune.csv # results/csv/histogramming_default.csv
… packed matmuls, exact int32 accumulation, per-core partials); on-device XLA torch baseline (bincount/histc run on the host); rerun benchmarks Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01ScXYNjrrKGgDUVNHxv7HJt
NKI (AWS Trainium) implementation of histogramming, split out of the consolidated NKI branch
cecilia/feature/nki-vector-add(nki-all-operators, #259) so each operator can be reviewed independently. Supersedes #222 (older per-operator branch: legacyneuronxcc.nkiimports; this is the migratedimport nkiversion).Files: A benchmarks/operators/histogramming/impl_nki.py, M benchmarks/operators/histogramming/impl_torch.py
Status: imports and exposes run()/get_last_config() on trn2 (nki 0.6.0); not individually re-benchmarked in this split
impl_torch.pychange from the NKI branchImplementation by @Cecilia123li. Timing/identity infrastructure: #261; Trainium peak/roofline infra: #262.
🤖 Generated with Claude Code
https://claude.ai/code/session_012Q38kGmXvyoeM1qtCbheSL
Autotune (47d2823)
block_size(values chunk per pass)BLOCK_SIZE/BLOCK_ROWS/BLOCK_BINSautotune=Falsekeeps the previous constants (default numbers unchanged). Validation on trn2, case 0 (default run + autotune code path with the candidate timer stubbed — no sweep;--autotuneruns a real sweep):