openaccel-lab
aien-dev/openaccel-lab
Brought by drakestapleton
openaccel-lab / docs / AUTHORING.md
| 1 | # Writing a workload |
| 2 | |
| 3 | A workload is one directory `workloads/<name>/` (or `controls/<name>/` for a deliberately wrong |
| 4 | kernel). The directory name, the `name` field and the reference module agree. Everything an entrant |
| 5 | needs is in the directory; nothing outside it is workload-specific. |
| 6 | |
| 7 | ``` |
| 8 | workloads/<name>/ |
| 9 | workload.json manifest: shapes, generators, tolerance contract, entrant sources |
| 10 | reference.py reference(p, x): float64 numpy oracle, never an entrant |
| 11 | omega.om Omega entrant (kernel + main), compiled after lib/omega/oal.om |
| 12 | kernel.cl OpenCL C entrant (one or more __kernel functions) |
| 13 | triton_kernel.py Triton entrant: launch(p, t) |
| 14 | kernel.cu CUDA entrant: oal_launch(p, buf); the HIP entrant is this file through hipify-perl |
| 15 | ``` |
| 16 | |
| 17 | ## workload.json |
| 18 | |
| 19 | | field | meaning | |
| 20 | |---|---| |
| 21 | | `name`, `family`, `summary`, `provenance` | identity; provenance says who wrote it and under what licence | |
| 22 | | `params` | integer parameters with their default values, in container order | |
| 23 | | `derived` | optional `{name: expression}` computed from params after overrides, appended in order | |
| 24 | | `cases` | list of parameter overrides run by `oal-matrix`; `{}` is the default case. Include an edge shape (a size of 1, a size that is not a multiple of any block). | |
| 25 | | `shrink` | per parameter `{"min": m, "multiple_of": k}`: how far `oal-shrink` may reduce it | |
| 26 | | `inputs` | tensors `{name, dtype, shape, gen}`; dtype one of f32 f64 i32 i8 u8; shape a list of expressions | |
| 27 | | `outputs` | tensors `{name, dtype, shape}` | |
| 28 | | `tolerance` | per output: `{"kind": "exact"}`, `{"kind": "bound", "rtol": expr, "atol": expr, "why": text}` or `{"kind": "int", "atol": n}` | |
| 29 | | `timing` | `{"warmup": w, "repeats": r}` | |
| 30 | | `backends` | `omega: {src}`, `opencl: {src, launches, scratch?, options?}`, `triton: {src}`, `cuda: {src}` | |
| 31 | | `scratch` | optional top-level list of tensors `{name, dtype, shape}` that OpenCL/Triton/CUDA entrants may use as device temporaries (zeroed for Triton/CUDA, undefined for OpenCL) | |
| 32 | | `expect` | controls only: `"fail"` | |
| 33 | | `reference_from` | controls only: the workload whose reference judges this control | |
| 34 | |
| 35 | Expressions are Python arithmetic over parameter names plus `gamma(n)`, `u` (2^-24), `max`, `min`, |
| 36 | `log2`, `ceil`. `gamma(n)` is n·u/(1−n·u), Higham's bound on n roundings in binary32. |
| 37 | |
| 38 | Generators (`gen`), all from splitmix64 with stream seed `seed ^ fnv1a64("<workload>/<input>")`: |
| 39 | |
| 40 | - `{"kind":"uniform","lo":a,"hi":b}`: a + (b−a)·k·2^-24 |
| 41 | - `{"kind":"normal4","mean":m,"std":s}`: Irwin–Hall sum of four uniforms, unit variance, then scaled |
| 42 | - `{"kind":"grid","klo":i,"khi":j,"shift":s}`: integer k in [i, j] times 2^-s, exact in f16 when |k| < 2048 |
| 43 | - `{"kind":"int","lo":i,"hi":j}`: integers; required for i32, i8, u8 inputs |
| 44 | - `{"kind":"const","value":v}`, `{"kind":"iota","mod":m}` |
| 45 | |
| 46 | Float scalars (alpha, eps) are written as constants in every source or passed as one-element f32 |
| 47 | inputs; integer scalars are parameters. |
| 48 | |
| 49 | ## The tolerance contract |
| 50 | |
| 51 | `exact` is for outputs made by at most one correctly rounded operation per element (add, mul, |
| 52 | copy, max, integer work). Everything else uses `bound` with a bound you can defend: state it in the |
| 53 | `why` field, and derive any numeric constant through the Wolfram Language evaluator, quoting the |
| 54 | query and result (`WolframLanguageEvaluator: <query> -> <result>`). The bound must hold for any |
| 55 | summation order and for fused multiply-add, because OpenCL C contracts `a*b+c` by default and GPU |
| 56 | compilers fuse; a recursive dot product of length n in any order is within gamma(n)·Σ|a_i b_i| |
| 57 | (Higham Thm 3.1/3.5), so pass that sum as the `scale` from `reference.py` and use `rtol: gamma(n)`. |
| 58 | For a transcendental, budget its documented accuracy: OpenCL `exp` ≤ 3 ulp (OpenCL C 3.0 §7.4), |
| 59 | CUDA `expf` ≤ 2 ulp (CUDA C Programming Guide, Mathematical Functions), so allow at least 4·u |
| 60 | relative per exp on top of the arithmetic bound. A bound so loose that a wrong kernel passes is a |
| 61 | defect: every workload family needs a control that fails it. |
| 62 | |
| 63 | ## reference.py |
| 64 | |
| 65 | ```python |
| 66 | import numpy as np |
| 67 | |
| 68 | def reference(p, x): # p: params dict, x: inputs as numpy arrays (manifest dtypes) |
| 69 | a = x["a"].astype(np.float64) |
| 70 | return {"y": a * 2.0} # or {"y": (value, scale)} with an elementwise scale for "bound" |
| 71 | ``` |
| 72 | |
| 73 | Compute in float64 from the exact input values, in the obvious order, with no float32 shortcut. The |
| 74 | reference shape must equal the manifest shape. |
| 75 | |
| 76 | ## Entrants |
| 77 | |
| 78 | All entrants see the same case: parameters in manifest order (`params`, then `derived`), inputs in |
| 79 | manifest order, outputs in manifest order. Outputs start filled with 0xFF bytes (NaN for f32), so an |
| 80 | element the kernel never writes fails the check. |
| 81 | |
| 82 | **Omega (`omega.om`).** Compiled as `lib/omega/oal.om` + `omega.om` by the pinned omc |
| 83 | (aien-dev/omega-mv 369dbc96), program mode, Linux AArch64. The prelude reads the case and gives: |
| 84 | `oal_begin`, `oal_p k` (parameter k, long), `oal_warm`, `oal_reps`, loaders that copy input k into a |
| 85 | pool and return its element count (`oal_fa..oal_fe k` into f32 pools `fa..fe`, `oal_ia/oal_ic k` into |
| 86 | int pools `ia/ic`, `oal_qa k` into byte pool `qa`, values −128..127), stores that copy a pool to output |
| 87 | k (`oal_so k n` from `fo`, `oal_sp k n` from `fp`, `oal_sio k n` from `io`, `oal_sqo k n` from `qo`), |
| 88 | `oal_tick ns` (one timing line), `oal_end` (writes stdout) and `oal_expf x` (binary64 e^x). Pools hold |
| 89 | 4194304 elements; a workload may declare its own globals (names up to 8 characters, not clashing with |
| 90 | the prelude). Globals cannot be passed as arguments (error 19); functions use them by name. A |
| 91 | function takes at most 6 parameters. The pattern is `workloads/vecadd/omega.om`: a `kern` function |
| 92 | over globals, a main that loads, times `oal_reps` calls after `oal_warm` untimed ones with `monons`, |
| 93 | stores and ends. Language reference: `docs/one_layer/OMEGESE.md` in aien-dev/omega-mv; the pinned |
| 94 | compiler is the authority on what compiles. |
| 95 | |
| 96 | **OpenCL (`kernel.cl` + `launches`).** Each launch is `{"kernel", "global": [exprs], "local": |
| 97 | [exprs] or null, "args": [...]}`, run in order once per timed repeat. An argument is a tensor name |
| 98 | (input, output or scratch buffer), a parameter expression (passed as `int`), `"f32:<expr>"` (a float), |
| 99 | or `"local:<dtype>:<count expr>"` (`__local` memory). No fast-math options. |
| 100 | |
| 101 | **Triton (`triton_kernel.py`).** Define `launch(p, t)`; `t` maps every input, output and scratch name |
| 102 | to a contiguous CPU torch tensor. CI runs with `TRITON_INTERPRET=1`: keep shapes small and blocks |
| 103 | powers of two. |
| 104 | |
| 105 | **CUDA and HIP (`kernel.cu`).** `#include <cuda_runtime.h>`, the kernels, then |
| 106 | `void oal_launch(const long long *p, void *const *buf)` that launches them; `buf` holds device |
| 107 | pointers for inputs, outputs, then scratch, in manifest order. The HIP entrant is this file through |
| 108 | `hipify-perl -hip-kernel-execution-syntax`, built against HIP-CPU (ROCm/HIP-CPU e112c935) with g++: |
| 109 | use static `__shared__` arrays only, no templates on kernels, no warp intrinsics, no dynamic |
| 110 | parallelism, and `__syncthreads()` only where every thread of the block reaches it. CUDA is built |
| 111 | with nvcc on CI and run only on rented GPUs (`runners/cuda/run_all.sh`). |
| 112 | |
| 113 | ## Controls |
| 114 | |
| 115 | A control is a copy of a workload with the same defect written into every entrant (for example the |
| 116 | k loop stops one short). It sets `"expect": "fail"` and `"reference_from": "<workload>"`. `oal-gate` |
| 117 | fails CI if a control passes anywhere it ran. |