summaryrefslogtreecommitdiff
path: root/README.md
blob: 89daa8a1d0c898e14f1ae0a26345b99e2b3dd5c2 (plain)
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
# volta-train-kernels

Hand-written CUDA and Triton kernels for **training** transformer language
models on NVIDIA Volta (V100, sm_70).

Modern attention and fusion libraries need Ampere or newer. FlashAttention 2/3
does not build for sm_70, there is no bf16, and on sm_70 Triton's `tl.dot` lowers
to plain FMA with no tensor cores. On a V100, PyTorch's only training-capable attention
backend is the CUTLASS memory-efficient kernel. These kernels fill that gap.
They were written for the Boris-2 pretraining run on a single V100, and they are
described in the paper *Outrageously Fast Training on Volta*.

## What is here

| module | what it does |
|---|---|
| `attn.cu` via `varlen_causal_attention` | Causal attention over packed variable-length documents, head_dim 64, fp16 in/out. Forward and backward run on raw `mma.sync.aligned.m8n8k4` tensor-core instructions (HMMA.884) with fp32 accumulation. Documents are launched longest-first. |
| `canon_rope_bw.py` via `canon_rope_attention` | A Canon depthwise 3-tap convolution, RoPE and the attention above, as one autograd function. The Canon/RoPE part is a bandwidth-bound CUDA kernel. |
| `canon_rope.py` | The Triton reference for Canon+RoPE. The CUDA forward is bitwise-identical to it; the CUDA backward reduces the weight gradient in a different order. |
| `head.py` via `fused_lm_loss` | Chunked cross-entropy LM head that never materializes `(N, V)` logits. dW and dx come from fp32-output GEMMs. It saves memory; it is not faster than an unchunked head that fits in memory. |
| `integ_ops.py` | Fused residual-add + RMSNorm backward, weight-gradient accumulation straight into fp32 `.grad`, and a tied-embedding gradient. |

## Requirements

- An sm_70 GPU (Tesla V100). The kernels may run on later architectures through
  PTX JIT, but they are neither tuned nor benchmarked there.
- PyTorch with CUDA and a matching `nvcc`, plus Triton.
- fp16 operands, head_dim 64, and a token stride that is a multiple of 8. Anything else
  raises an error.

The first call compiles with nvcc (about 1 minute) into `$VOLTA_KERNELS_CACHE`,
which defaults to `~/.cache/volta_kernels`.

## Usage

```python
import torch
from volta_kernels import varlen_causal_attention

# q, k, v: (N, H, 64) fp16, N tokens of several documents packed back to back
# offsets: document boundaries, e.g. tensor([0, 700, 764, 765, 2813])
o = varlen_causal_attention(q, k, v, offsets)   # same as causal SDPA per document
o.sum().backward()
```

## Precision

Every kernel keeps the operand dtypes and fp32 accumulation of the stock path it
replaces. For attention (forward and backward), the test suite holds each output's
error against an fp64 reference to at most 1.05x the stock path's own error on the
same inputs, and checks that document isolation is exact. The Canon+RoPE forward is
tested bitwise against its Triton reference. The Canon backward and the ops in
`integ_ops.py` were validated inside the Boris-2 training code but have no
standalone tests here yet. dQ uses fp32 atomics, so the attention backward is not
bitwise deterministic run to run (the difference is fp32 summation order only).

## Tests

```bash
python -m unittest discover -s tests -v   # from the repo root; VK_TEST_DEVICE=cuda:N picks the GPU
```

## Results

Speedups, precision tables and the methodology are in the paper. A link will be
added on publication.

## License

MIT