File size: 4,089 Bytes
0d3c0bd
 
 
 
 
 
 
 
 
 
5d0cd55
0d3c0bd
 
 
 
 
 
1bb3ed7
 
21585ee
1bb3ed7
 
 
 
 
 
 
d190e38
1bb3ed7
d190e38
1bb3ed7
0d3c0bd
1bb3ed7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d190e38
1bb3ed7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9724394
1bb3ed7
 
 
0d3c0bd
1bb3ed7
 
 
 
 
 
 
 
 
 
 
 
 
9724394
 
 
 
 
 
 
1bb3ed7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
---
license: apache-2.0
language:
- en
tags:
- nki
- aws-neuron
- trainium
- kernel
- benchmark
- environment
pretty_name: NKIBench
size_categories:
- n<1K
viewer: false
---

# NKIBench

NKIBench is a benchmark of AWS [Neuron Kernel Interface (NKI)](https://awsdocs-neuron.readthedocs-hosted.com/en/latest/general/nki/index.html) kernels paired with NumPy reference implementations. Each task provides a specification, a ground-truth NumPy forward pass, and an optimized NKI kernel targeting AWS Trainium devices, together with tooling to compile, check numerical correctness, and measure on-device latency. [Paper](https://arxiv.org/pdf/2511.15915).

## Dataset structure

```
NKIBench/
├── seeds/               # YAML task specifications (shape-agnostic templates)
├── reference/           # NumPy reference implementations with concrete shapes
├── kernels/             # Initial NKI kernels (one per case)
├── summary.json         # Index mapping task → case → {seed, reference, kernel}
├── save_fields.json     # Useful fields of the neuron profiler
└── kernel_wrapper.py    # Profiler: compile, correctness check, latency benchmark

```

### `summary.json`

The canonical index. Each entry maps a task name to one or more parameter cases and the files that implement them:

```json
{
  "matmul": {
    "seed": "./seeds/matmul.yaml",
    "cases": {
      "3": {
        "values": {"K": 5120, "M": 4096, "N": 12288},
        "impls": [{
          "task":   "./reference/matmul_M4096_N12288_K5120_numpy_2.py",
          "kernel": "./kernels/matmul_M4096_N12288_K5120_0.py"
        }]
      }
    }
  }
}
```

### `seeds/*.yaml`

A shape-agnostic specification: the task name, its symbolic parameters, an input generator, and a NumPy `forward` implementation.

```yaml
test_name: matmul
parameters: [M, N, K]
input: |
  lhs = np.random.normal(loc=0, scale=1.0, size=(M, K)).astype(np.float32)
  rhs = np.random.normal(loc=0, scale=1.0, size=(K, N)).astype(np.float32)
  return [lhs, rhs]
impl: |
  def forward(lhs, rhs):
    return np.matmul(lhs, rhs)
```

### `reference/*.py`

A shape-concrete NumPy reference. Exposes:

- `get_inputs()` — produces randomized numpy input tensors.
- `forward(*inputs)` — ground-truth computation.
- `transform_to_nki_inputs(inputs)` — reshapes numpy inputs into the tile layout the NKI kernel expects.
- `transform_nki_outputs(k_res, ref)` — reshapes kernel outputs back to reference layout.

### `kernels/*.py`

Initial NKI kernels using `neuronxcc.nki`. Each file defines a `kernel` function decorated with `@nki.jit`.

## Usage

```bash
# Clone the dataset
hf download Genghan/NKIBench --repo-type dataset --local-dir NKIBench
cd NKIBench
```

```python
# Profile one kernel on an AWS Neuron-enabled instance (e.g. trn1 / inf2).
# Requires: neuronx-cc, neuronx-runtime, and the `neuron-profile` CLI.
import json
from kernel_wrapper import NKIKernel

summary = json.load(open("summary.json"))
save_fields = json.load(open("save_fields.json"))
case = summary["matmul"]["cases"]["3"]["impls"][0]

k = NKIKernel(program_path=case["kernel"], base_numpy_path=case["task"])
result = k.profile(save_fields=save_fields)

print("compiled:", result.compiled)
print("correct :", result.correct)
print("latency :", result.metadata.get("latency"), "ms")
```

`NKIKernel.profile()` compiles the kernel, validates numerical correctness against the NumPy reference over multiple random seeds (L2-norm relative tolerance `2e-5`), and benchmarks latency via `neuron-profile`. `float16` inside a kernel is rejected to avoid silent precision loss.

## Citation

If you use NKIBench in your work, please cite the repository.

```bibtex
@article{zhang2026accelopt, 
  title={AccelOpt: A Self-Improving LLM Agentic System for AI Accelerator Kernel Optimization}, 
  author={Zhang, Genghan and Zhu, Shaowei and Wei, Anjiang and Song, Zhenyu and Nie, Allen and Jia, Zhen and Vijaykumar, Nandita and Wang, Yida and Olukotun, Kunle}, 
  journal={Proceedings of Machine Learning and Systems}, 
  volume={9}, 
  year={2026} 
 }
```