Kernels
File size: 5,869 Bytes
44e9845
 
8f89ce2
e677f62
f72121c
 
 
 
 
 
 
 
f3b99fb
e5e2eeb
 
 
 
 
 
 
a1e5ca8
 
e5e2eeb
 
 
 
 
a1e5ca8
 
e5e2eeb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f72121c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d14fd4d
 
e5e2eeb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d14fd4d
 
 
e5e2eeb
97825b8
e5e2eeb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d14fd4d
e5e2eeb
d14fd4d
97825b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
---
tags:
- kernels
license: apache-2.0
---

# Activation

Activation is a python package that contains custom CUDA-based activation kernels, primarily targeting AMD GPUs.

- Currently implemented
  - [PolyNorm](https://arxiv.org/html/2411.03884v1)
  - [RMSNorm](https://docs.pytorch.org/docs/stable/generated/torch.nn.RMSNorm.html)
  - **FusedAddRMSNorm**

    A fused operator that combines **residual addition** (`x + residual`) with **RMSNorm** in a single kernel.
    - Instead of:

      ```python
      y = x + residual
      hidden_state = rms_norm(y, weight, eps)
      out = y + some_op(hidden_state) 
      ```

    - Fused as:

      ```python
      hidden_state, y = fused_add_rms_norm(x, residual, weight, eps)
      out = y + some_op(hidden_state)
      ```

  - **FusedMulPolyNorm**

    A fused operator that combines **PolyNorm** with an **element-wise multiplication** by a Tensor.
    - Instead of:

      ```python
      y = poly_norm(x, weight, bias, eps)
      out = y * a
      ```

    - Fused as:

      ```python
      out = fused_mul_poly_norm(x, a, weight, bias, eps)
      ```

## Usage

```python
import torch
from kernels import get_kernel

activation = get_kernel("motif-technologies/activation")

torch.set_default_device("cuda")
poly_norm = activation.layers.PolyNorm(eps=1e-6)
x = torch.randn(10, 10)

print(poly_norm(x))
```

## Performance
- Test cases are from the Motif LLM
- The results can be reproduced using the provided benchmarking tools.
- For details on how to use the benchmarking tools, please refer to the [benchmarks README](./benchmarks/README.md).
- The benchmark results may show fluctuations, especially in the backward pass and when the dimension size is small.

### RMSNorm

#### H100 Results

<details>
<summary>Forward Performance</summary>

![RMSNorm Forward Performance](./benchmarks/plots/h100/rms/plot_rms-fwd-perf.png)

</details>

<details>
<summary>Backward Performance</summary>

![RMSNorm Backward Performance](./benchmarks/plots/h100/rms/plot_rms-bwd-perf.png)

</details>

#### MI250 Results

<details>
<summary>Forward Performance</summary>

![RMSNorm Forward Performance](./benchmarks/plots/mi250/rms/plot_rms-fwd-perf.png)

</details>

<details>
<summary>Backward Performance</summary>

![RMSNorm Backward Performance](./benchmarks/plots/mi250/rms/plot_rms-bwd-perf.png)

</details>

---

### FusedAddRMSNorm

> [!NOTE]  
> For fusion case performance, the **non-fused baseline** was implemented with our **custom kernels**.

#### H100 Results

<details>
<summary>Forward Performance</summary>

![FusedAddRMSNorm Forward Performance](./benchmarks/plots/h100/add_rms/plot_add_rms-fwd-perf.png)

</details>

<details>
<summary>Backward Performance</summary>

![FusedAddRMSNorm Backward Performance](./benchmarks/plots/h100/add_rms/plot_add_rms-bwd-perf.png)

</details>

#### MI250 Results

<details>
<summary>Forward Performance</summary>

![FusedAddRMSNorm Forward Performance](./benchmarks/plots/mi250/add_rms/plot_add_rms-fwd-perf.png)

</details>

<details>
<summary>Backward Performance</summary>

![FusedAddRMSNorm Backward Performance](./benchmarks/plots/mi250/add_rms/plot_add_rms-bwd-perf.png)

</details>

---

### PolyNorm

#### H100 Results

<details>
<summary>Forward Performance</summary>

![PolyNorm Forward Performance](./benchmarks/plots/h100/poly/plot_poly-fwd-perf.png)

</details>

<details>
<summary>Backward Performance</summary>

![PolyNorm Backward Performance](./benchmarks/plots/h100/poly/plot_poly-bwd-perf.png)

</details>

#### MI250 Results

<details>
<summary>Forward Performance</summary>

![PolyNorm Forward Performance](./benchmarks/plots/mi250/poly/plot_poly-fwd-perf.png)

</details>

<details>
<summary>Backward Performance</summary>

![PolyNorm Backward Performance](./benchmarks/plots/mi250/poly/plot_poly-bwd-perf.png)

</details>

---

### FusedMulPolyNorm

> [!NOTE]  
> For fusion case performance, the **non-fused baseline** was implemented with our **custom kernels**.

#### H100 Results

<details>
<summary>Forward Performance</summary>

![FusedMulPolyNorm Forward Performance](./benchmarks/plots/h100/mul_poly/plot_mul_poly-fwd-perf.png)

</details>

<details>
<summary>Backward Performance</summary>

![FusedMulPolyNorm Backward Performance](./benchmarks/plots/h100/mul_poly/plot_mul_poly-bwd-perf.png)

</details>

#### MI250 Results

<details>
<summary>Forward Performance</summary>

![FusedMulPolyNorm Forward Performance](./benchmarks/plots/mi250/mul_poly/plot_mul_poly-fwd-perf.png)

</details>

<details>
<summary>Backward Performance</summary>

![FusedMulPolyNorm Backward Performance](./benchmarks/plots/mi250/mul_poly/plot_mul_poly-bwd-perf.png)

</details>

## Pre-commit Hooks

This project uses [pre-commit](https://pre-commit.com/) to automatically check and format code before commits.

### Setup

1. Install pre-commit:

   ```bash
   pip install pre-commit
   ```

2. Install the git hooks:
  
```bash
   pre-commit install
   ```

Once installed, the configured hooks will run automatically on each commit.

### Included Hooks

The following tools are run via pre-commit:

- **[yapf](https://github.com/google/yapf)** – Python code formatter  
- **[typos](https://github.com/crate-ci/typos)** – Spell checker for common typos  
- **[isort](https://github.com/PyCQA/isort)** – Organizes and sorts Python imports  
- **[clang-format](https://clang.llvm.org/docs/ClangFormat.html)** – Formats C++/CUDA code (`--style=file`)  
- **[pymarkdown](https://github.com/jackdewinter/pymarkdown)** – Lints and auto-fixes Markdown files  
- **[actionlint](https://github.com/rhysd/actionlint)** – Validates GitHub Actions workflows  

### Usage

- Run all checks on the entire codebase:

   ```bash
   pre-commit run --all-files
   ```

- Run a specific hook (example: isort):
  
 ```bash
   pre-commit run isort --all-files
   ```