interplane.Get Omega
Read in your language

openaccel-lab

aien-dev/openaccel-lab

Brought by drakestapleton

branch main Branches Tags History

openaccel-lab / workloads / attention / workload.json

146 lines · 3567 bytes Raw Blame History
1{
2 "name": "attention",
3 "family": "attention",
4 "summary": "single-head causal attention O = softmax(mask(Q K^T * 0.125)) V; Q, K, V are T x Dh with Dh = 64; softmax subtracts the row max (Q and K have mean 3.5, so unshifted scores near 98 overflow binary32 exp)",
5 "provenance": "authored for OpenAccel Lab (Apache-2.0)",
6 "params": {
7 "T": 48,
8 "Dh": 64
9 },
10 "cases": [
11 {},
12 {
13 "T": 1
14 },
15 {
16 "T": 17
17 }
18 ],
19 "shrink": {
20 "T": {
21 "min": 1
22 },
23 "Dh": {
24 "min": 64,
25 "multiple_of": 64
26 }
27 },
28 "inputs": [
29 {
30 "name": "q",
31 "dtype": "f32",
32 "shape": [
33 "T",
34 "Dh"
35 ],
36 "gen": {
37 "kind": "normal4",
38 "mean": 3.5,
39 "std": 1.0
40 }
41 },
42 {
43 "name": "k",
44 "dtype": "f32",
45 "shape": [
46 "T",
47 "Dh"
48 ],
49 "gen": {
50 "kind": "normal4",
51 "mean": 3.5,
52 "std": 1.0
53 }
54 },
55 {
56 "name": "v",
57 "dtype": "f32",
58 "shape": [
59 "T",
60 "Dh"
61 ],
62 "gen": {
63 "kind": "normal4",
64 "mean": 0.0,
65 "std": 1.0
66 }
67 }
68 ],
69 "outputs": [
70 {
71 "name": "o",
72 "dtype": "f32",
73 "shape": [
74 "T",
75 "Dh"
76 ]
77 }
78 ],
79 "scratch": [
80 {
81 "name": "pw",
82 "dtype": "f32",
83 "shape": [
84 "T",
85 "T"
86 ]
87 }
88 ],
89 "tolerance": {
90 "o": {
91 "kind": "bound",
92 "rtol": 1,
93 "atol": 1e-35,
94 "why": "reference.py returns the whole bound as the scale, per element: for row i with n = i+1 terms, bound = sum_j p_j |v_jc| c_j. Algorithm covered: scores, row max, exp(score - max), sum, then either p_j = e_j/sum and O = sum_j p_j v_j or O = (sum_j e_j v_j)/sum, every sum in any order, with or without FMA. Each score is within e_j = gamma(Dh) a_j, a_j = 0.125 sum_c |q_c k_jc| (Higham Thm 3.1; 0.125 exact); the row max within E = max_j e_j; the subtraction adds u|s_j - max|; so exp's argument is within tau_j = e_j + E + u(|d_j| + e_j + E), and with exp budgeted at 3 ulp <= 6u relative (OpenCL C 3.0 s7.4; CUDA expf 2 ulp = 4u is inside it) each exp(score - max) has relative error theta_j = e^tau_j (1 + 6u) - 1. The positive denominator sum has relative error K = sum_j p_j((1+theta_j)(1+gamma(n-1)) - 1); the numerator dot, including the p_j division, is within gamma(n+1) and a final division adds u, giving c_j = (1+theta_j)(1+gamma(n+1))(1+u)/(1-K) - 1. WolframLanguageEvaluator: u=2^-24; g[n_]:=n u/(1-n u); N[g[64],10] -> 3.814711818*10^-6; N[g[49],10] -> 2.920636124*10^-6; N[g[47],10] -> 2.801426152*10^-6; N[3*2^-23/u] -> 6.; N[2*2^-23/u] -> 4.; N[64*2^-24/(1-64*2^-24)*98*2,10] -> 0.0007476835162 (2 gamma(64) a with a near 8*3.5^2 = 98: the order of c_j, so a dropped term or a missing mask fails). atol covers exp, p_j and p_j v_j results flushed or rounded below 2^-126: at most 3 n of them, each < 2^-126 max|v| with max|v| <= Sqrt[12] (normal4 range) and the denominator >= 1: N[3*48*2^-126*Sqrt[12],10] -> 5.863725906*10^-36 <= 1e-35. Without the max subtraction exp overflows: N[Log[2^128],10] -> 88.72283911 is below scores near 98, so O becomes NaN."
95 }
96 },
97 "timing": {
98 "warmup": 1,
99 "repeats": 5
100 },
101 "backends": {
102 "omega": {
103 "src": "omega.om"
104 },
105 "opencl": {
106 "src": "kernel.cl",
107 "launches": [
108 {
109 "kernel": "attn_p",
110 "global": [
111 "T"
112 ],
113 "local": null,
114 "args": [
115 "q",
116 "k",
117 "pw",
118 "T",
119 "Dh"
120 ]
121 },
122 {
123 "kernel": "attn_o",
124 "global": [
125 "T",
126 "Dh"
127 ],
128 "local": null,
129 "args": [
130 "pw",
131 "v",
132 "o",
133 "T",
134 "Dh"
135 ]
136 }
137 ]
138 },
139 "triton": {
140 "src": "triton_kernel.py"
141 },
142 "cuda": {
143 "src": "kernel.cu"
144 }
145 }
146}