omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / seed / kflow / TILE-WORDS.md
| 1 | # Tile words for OMEGESE: proposal (PROPOSED, not implemented) |
| 2 | |
| 3 | Status: PROPOSED. No compiler, encoder or table changed for this proposal. The two hand-written listings |
| 4 | `sm121/tmatmul16.s2` and `sm121/tsoftmax.s2` are what these words should generate, instruction for |
| 5 | instruction; they are EXERCISED on the host (kflow encode, verify, package, emulate) and NOT_RUN on a GPU. |
| 6 | |
| 7 | ## Why words, not a library |
| 8 | |
| 9 | Both listings were written by hand from the PTX ISA 9.4 fragment tables. Most of their 46 and 38 instructions (plus a padding NOP each) |
| 10 | are fragment addressing (lane to row and column, offsets, packing two f16 into one register), and the one real |
| 11 | bug found so far was a register clobber in a butterfly (README, "Tile kernels"). Those are exactly the parts a |
| 12 | compiler should own: the writer states the tile shapes, the compiler picks the fragment layout and registers. |
| 13 | |
| 14 | ## Words |
| 15 | |
| 16 | Prefix order and argument conventions as in OM-2. Types: `f16`, `bf16`, `f32` element types; a tile value |
| 17 | is `tile T R C` (rows, columns), held in registers spread over the 32 lanes of a warp. Only shapes the target |
| 18 | has a table row for are accepted; anything else is a compile error, never a silent fallback. |
| 19 | |
| 20 | | word | meaning | sm_121 lowering (rows in `seed/sm121/sm121.tbl`) | |
| 21 | | --- | --- | --- | |
| 22 | | `tload T R C P ld` | load an R x C tile of T from global P (row stride `ld` elements) into the fragment layout the next `tmma` needs | `ldg.0`/`ldg.w.1`/`ldg.2` (32-bit pairs along a row), `ldg.u16.2` + IMAD pack (pairs down a column) | |
| 23 | | `tmma A B C` | D = A x B + C, f32 accumulate; A `tile f16 16 16`, B `tile f16 16 N` (N = 8k), C `tile f32 16 N` or `0` | one `hmma.0`/`hmma.f16.2` (bf16: `hmma.bf16.2`) per 8 columns; `0` lowers to RZ | |
| 24 | | `tstore P ld D` | store an f32 tile from the accumulator layout | `stg.0`/`stg.1`/`stg.2` | |
| 25 | | `wmax x` / `wsum x` | the maximum / sum of x over the 32 lanes, the same value in every lane | 5 x (`shfl.0`/`shfl.bfly.1`/`shfl.2` into a fresh register, then `fmnmx.max.2` or `fadd`) | |
| 26 | | `ex2 x` / `rcp x` | approximate 2^x and 1/x (PTX ex2.approx.f32: 2 ulp; rcp.approx.f32: 1 ulp) | `mufu.ex2.2`, `mufu.rcp.2` | |
| 27 | |
| 28 | With these, the two kernels are short (sketch, not compiled): |
| 29 | |
| 30 | ``` |
| 31 | fn tmatmul16 a ptr b ptr d ptr to void |
| 32 | ta = tload f16 16 16 a 16 |
| 33 | tb = tload f16 16 16 b 16 |
| 34 | tstore d 16 tmma ta tb 0 |
| 35 | end |
| 36 | |
| 37 | fn tsoftmax x ptr y ptr to void |
| 38 | i = add mul ctaid 32 tid |
| 39 | v = get x i |
| 40 | m = wmax v |
| 41 | e = ex2 mul sub v m 1.44269502162933349609375f32 |
| 42 | put y i mul e rcp wsum e |
| 43 | end |
| 44 | ``` |
| 45 | |
| 46 | ## Required checks before any word is accepted |
| 47 | |
| 48 | 1. Every row the lowering emits is checked against nvcc/cuobjdump words (not against this emitter), as done for |
| 49 | the 48 instructions in the README table, and every emitted instruction against `nvdisasm -b SM121`. |
| 50 | 2. kemu runs the generated kernel against an f64 reference within a tolerance derived with Wolfram (the |
| 51 | README gives the two used here), with a negative control per word (wrong fragment layout, transposed B, |
| 52 | butterfly turned into SHFL.DOWN). |
| 53 | 3. The generated listing for the two sketches above is byte-identical to the hand listings, or every |
| 54 | difference is explained. |
| 55 | 4. A chip run on sm_121 in an authorized window before anything is called QUALIFIED. |
| 56 | |
| 57 | ## Open questions |
| 58 | |
| 59 | - Warp-collective words need every lane to reach them together. The language has no notion of a warp yet; |
| 60 | kemu reports `DIVERGED` when that is broken, but the compiler should reject it statically. |
| 61 | - Tile values span 32 lanes. OM-2 values are per thread, so `tile` needs a type rule (no `get`/`put` on a |
| 62 | tile, only the tile words). |
| 63 | - Larger shapes (K loops, multiple warps, shared memory staging with LDSM) and an sm_80 lowering (the same |
| 64 | HMMA.16816 exists there) are follow-ons; so is a CUDA Tile IR round-trip. |