diff --git a/clean/fg/FORMATS.md b/clean/fg/FORMATS.md index b25fc565..1af55c55 100644 --- a/clean/fg/FORMATS.md +++ b/clean/fg/FORMATS.md @@ -63,12 +63,37 @@ delivered by the spec side; the code never guesses it (it raises, or takes the c from blobs (`params_from_blobs`: stored weights gathered with the map's offsets) or from the converter's `--raw` output of the preset-FG map (`params_from_raw`, stages `dl1.lNN` / `dl2.lNN`, tensors `W` and `b`; every value checked to be exact in float16). -- `run_layer`: one convolution with `ops.conv2d`. `run_graph`: a network as a list of steps (`conv` with its layer and - ReLU flag, `pool` with its kind, `upscale`, `add`, `concat`, `slice`), checked before it runs (known steps, exact - fields, buffers written once before they are read, each layer used at most once). `net1_forward` adds the 2 x 2 - fold before and the unfold (16 values per cell to 4 per pixel) after; `net2_forward` returns the named buffers. -- Required arguments (OPEN): `wiring` (the steps), `pad`, `fold_order`. The weight order and net1's blob 2 are settled - by map v1.1 and are no longer arguments. The real wiring plugs in as data; the layers do not change. +- Also from the converter's model file (`params_from_model_file`, container version 1, checksum checked). +- `net1_forward(values, params)`: net1 with its delivered wiring and arithmetic (next section); no other arguments. +- net2 (wiring not delivered): `run_layer` (one convolution with `ops.conv2d`), `run_graph` (a list of steps: `conv` + with its layer and ReLU flag, `pool` with its kind, `upscale`, `add`, `concat`, `slice`; checked before it runs) + and `net2_forward`. Required arguments (OPEN): `wiring`, `pad`. + +## net1 (`reference/net1.py`) + +- Input: 10 float16 values per pixel of the input grid, folded 2 x 2 into cells of 48 channels: channel 4 v + q with + q = 2 dy + dx; channels 40..47 and pixels outside the input area are zero. Output: 16 channels per cell, unfolded the + same way to 4 values per pixel. +- 32 steps (`NET1_WIRING`, data): five levels down (convolution, then 2 x 2 max-pool), the two bottom 1 x 1 + convolutions, five levels up (upscale to the skip's size, float16 add of the skip, two 1 x 1 convolutions). Level + sizes halve with floor; the skips are the encoder outputs after the activation. +- Convolution: stride 1, zero border, cross-correlation over the natural weights. Float16 accumulator started at the + bias; 16-term chunks (k = (3 ky + kx) in + c, or c for 1 x 1) each summed exactly and added with one float16 + rounding (`fp16acc.acc_dot16`, integer arithmetic). Chunk orders: 1 x 1 ascending; blobs 1, 3, 4 channel block outer, + then the taps with kx outer and ky inner; blob 5 kx outer, then channel block, then ky. +- Activation: leaky ReLU y = max(x, h(s x)) after every convolution but the last (blob 17, linear), with the delivered + slope s = 0.0099945068359375 (float16 0x211E). Nearest-even rounding of 0.01 would give 0x211F; the delivered value + is used as given. +- Max-pool: 2 x 2, stride 2, floor sizes, m <- (x > m) x + (x <= m) m in float16 (inf and NaN give the same result in + any tap order; two +inf give NaN, as the formula does). +- Upscale: bilinear, align corners, from the coarser level to the skip's size: single-precision positions + s = ((n_in - 1) / (n_out - 1)) i, x0 = trunc(s), x1 = trunc(s + 1) (both n_in - 1 at the edge), float16 weights, + every product and sum rounded to float16. The original's ratio is an approximate division; here it is the correctly + rounded single-precision quotient. +- Add: h(a + b). +- `tools/net1_digest.py`: net1 on a converter model file with a fixed synthetic input; records digests only. +- Not part of net1 and still open: what the 10 input values are made of (images A-D, the warps) and the input grid + size relative to the output. ## Configuration (`reference/config.py`) @@ -85,14 +110,14 @@ delivered by the spec side; the code never guesses it (it raises, or takes the c 1. (closed: map v1.1, natural order, is `data/fg.v1.map.json`.) 2. (closed: the in-blob axis order is the map's offsets.) -3. Which launch reads which blob (upload order is known; launch order is not). +3. net2's fused kernels: stride inside a kernel, the residual add, the three-way splits, the upsample and merges. + (net1's launch order is delivered.) 4. (closed: net1's blob 2 is a 1 x 1 convolution.) 5. Both networks' grid sizes relative to the output size. 6. What the UI-recomposition preset changes in the stage order. -7. Launch order, the motion-field scatter, the pull pyramid and the UI handling (part 3). +7. The motion-field scatter, the pull pyramid and the UI handling; net1's input images A-D and the warps. 8. Whether the networks run once per real frame or once per generated frame. -9. The float16 accumulation order of the matrix unit; the convolution border rule; which net2 layers end in ReLU; - the pooling kind; net1's feature channel order, the fold order and what the cell padding holds; how the warp for - the warp errors samples. +9. For net2: the border rule and the accumulation order. (For net1 both are delivered, with the activation, the + pooling kind, the fold order and the cell padding.) 10. The cap on the multi-frame count; the further mode that accepts index N + 1 and what it does; which maximum the "last index" flag compares with; the private override that can turn UI recomposition off. diff --git a/clean/fg/README.md b/clean/fg/README.md index 89687d4a..871febfd 100644 --- a/clean/fg/README.md +++ b/clean/fg/README.md @@ -11,8 +11,12 @@ front end or the launcher yet. Status, open items and formats: [FORMATS.md](FORM | `tests/test_fg_layout.py` | the table against the totals the spec side gives | | `reference/ops.py` | NumPy CPU reference of the networks' building blocks: float16 convolution (3 x 3, 1 x 1) with bias and ReLU, 2x pooling, bilinear 2x upscale (align corners), element-wise combines, net1's input features and 2 x 2 fold; each open behaviour is a required argument or raises `OpenItem` | | `tests/test_fg_reference.py` | the building blocks against plain loop versions, on synthetic data | -| `reference/forward.py` | both networks' forward pass: each layer from its stage's natural parameters (from blobs or the converter's raw output), and a network run from a wiring the caller supplies (conv / pool / upscale / add / concat / slice steps); the wiring, the border rule and the fold order are required arguments | +| `reference/forward.py` | both networks' forward pass: parameters (from blobs, the converter's raw output or its model file); `net1_forward` with net1's delivered wiring; net2 from a wiring the caller supplies (conv / pool / upscale / add / concat / slice steps), the wiring and the border rule required | | `tests/test_fg_forward.py` | every layer of both networks against a direct NumPy convolution on synthetic weights; each step kind; wiring checks; stand-in wirings against layers composed by hand; parameters from blobs and from raw files | +| `reference/net1.py` | net1 complete: the fold, the 32 steps as data, float16 chunk accumulation from the bias in each layer's chunk order, zero border, leaky ReLU, max-pool, the align-corners upscale with float16 steps, float16 adds, the unfold | +| `reference/fp16acc.py` | exact float16 chunk accumulation: RN16(acc + 16 products) with integer arithmetic | +| `tests/test_fg_net1.py`, `tests/test_fg_fp16acc.py` | net1 end to end against a hand-composed version; one convolution per chunk order against exact rationals; the upscale against the scalar rule; pooling with inf / NaN; the accumulation against exact rationals | +| `tools/net1_digest.py` | net1 on a converter model file with a fixed synthetic input; records digests of the output only | | `../converter` (preset FG) | the converter reads a version-1 map with `"preset": "FG"`: one block source per weight blob; see the converter's README, "Preset FG" | | `../../data/fg.v1.map.json` | the spec side's converter map, revision v1.1 (weights in natural order); `tests/test_fg_map.py` pins its digest and checks it against `layout.py` | | `reference/natural.py` | the natural weight order from the map itself: its offsets as gather indices (stored blob weights to natural), natural to canonical `[out, k, k, in]` | diff --git a/clean/fg/reference/forward.py b/clean/fg/reference/forward.py index fa827ba9..85b3a040 100644 --- a/clean/fg/reference/forward.py +++ b/clean/fg/reference/forward.py @@ -2,21 +2,21 @@ """Frame generation (issue #96): CPU reference of both networks' forward pass, as far as it is delivered. Written for this project. The layers are settled: their shapes (clean/fg/layout.py), the weights in natural order and -the biases the converter map gives per stage (data/fg.v1.map.json, revision v1.1; clean/fg/reference/natural.py), -float16 convolutions with bias, ReLU, 2x pooling, bilinear 2x upscale, element-wise combines, and net1's 2 x 2 fold -(clean/fg/reference/ops.py). net1's blob 2 is a 1 x 1 convolution. How the layers are connected is not delivered, so a -network is run from a wiring the caller supplies. When the real wiring arrives it is passed in as data; the layers do -not change. +the biases the converter map gives per stage (data/fg.v1.map.json, revision v1.1; clean/fg/reference/natural.py). -Required arguments for what is OPEN (clean/fg/FORMATS.md): +net1 is complete: net1_forward runs its delivered wiring and arithmetic (clean/fg/reference/net1.py: the fold, 32 +steps, leaky ReLU, zero border, float16 chunk accumulation, the unfold); it takes no other arguments. + +net2's wiring is not delivered, so net2 is run from a wiring the caller supplies (run_graph, net2_forward). Required +arguments for what is OPEN there (clean/fg/FORMATS.md): - `wiring`: the list of steps (which layer reads which buffers, where the pooling, upscales, combines and channel - splits sit, ReLU after each convolution, the pooling kind). Format: see run_graph. + splits sit, the activation after each convolution, the pooling kind). Format: see run_graph. - `pad`: the border rule of the 3 x 3 convolutions (ops.PAD_MODES). -- `fold_order`: the order of the values inside net1's 48-value cells. Parameters are {layer name: (weights [out, k, k, in] float16, bias [out] float16)} with the layout's layer names (net1_01 .. net1_17, net2_01 .. net2_25): from blobs (params_from_blobs: stored weights put in natural order by the -map's own offsets) or from the converter's raw output (params_from_raw: stages dl1.lNN / dl2.lNN, tensors W and b). +map's own offsets), from the converter's raw output (params_from_raw) or from its model file (params_from_model_file); +stages dl1.lNN / dl2.lNN, tensors W and b. """ import os @@ -62,6 +62,64 @@ def params_from_raw(raw_dir, m=None): return out +def read_model_file(path): + """{stage: {tensor: float32 array}} from a converter output file model.wgcm (container version 1, float32 tensors; + clean/converter/OUTPUT-FORMAT.md). The trailing SHA-256 is checked.""" + import hashlib + import struct + with open(path, "rb") as f: + data = f.read() + if data[:8] != b"WGCMODEL": + raise ValueError("not a WrongGPU model file") + if hashlib.sha256(data[:-32]).digest() != data[-32:]: + raise ValueError("model file checksum does not match") + version, nstages = struct.unpack_from(" [4, H, W].""" - if features.shape[0] != ops.NET1_FEATURES: - raise ValueError("net1 takes %d features per pixel" % ops.NET1_FEATURES) - cells = ops.fold2x2(features, order=fold_order) - buf = run_graph(wiring, {"cells": cells}, params, pad=pad) - out = buf[output] - if out.shape[0] != 16: - raise ValueError("net1's output has 16 values per cell, got %d" % out.shape[0]) - return ops.unfold2x2(out, 4, order=fold_order) +def net1_forward(values, params): + """net1 on its per-pixel values [10, G1h, G1w] -> [4, G1h, G1w], with the delivered wiring and arithmetic + (clean/fg/reference/net1.py: fold, 32 steps, leaky ReLU, zero border, float16 chunk accumulation, unfold).""" + from clean.fg.reference import net1 as _net1 + return _net1.forward(values, params) def net2_forward(inputs, params, *, wiring, pad, outputs): diff --git a/clean/fg/reference/fp16acc.py b/clean/fg/reference/fp16acc.py new file mode 100644 index 00000000..763c5d4d --- /dev/null +++ b/clean/fg/reference/fp16acc.py @@ -0,0 +1,95 @@ +# SPDX-License-Identifier: MIT +"""Frame generation (issue #96): exact float16 chunk accumulation, as a CPU reference. + +Written for this project. net1's convolutions accumulate in float16, starting at the bias: each chunk of 16 products +is summed exactly and added to the accumulator, which is rounded to float16 once per chunk (round to nearest, ties to +even). This module computes acc' = RN16(acc + sum of 16 products a_i * b_i) exactly, for arrays of accumulators. + +How: every finite float16 value is an integer multiple of 2^-24, |m| < 2^40. Each m is split into a high and a low +20-bit part, so the three partial dot products (high*high, high*low + low*high, low*low) are exact in int64. The +exact total, in units of 2^-48, is reduced to units of 2^-25 (half the smallest subnormal) with a sticky bit, and +rounded to float16 with integer arithmetic. No floating-point rounding happens before the single float16 rounding. + +Non-finite operands (an accumulator or factor that is inf or NaN) follow IEEE arithmetic: those elements are computed +in float64 and rounded to float16 (inf - inf and 0 * inf give NaN). What the original's matrix unit does with them is +not specified; finite inputs are exact. +""" +import numpy as np + +_S24 = float(1 << 24) +_LOW = (1 << 20) - 1 + + +def h(x): + """Round to float16 (nearest, ties to even) and return float64.""" + return np.asarray(x, dtype=np.float64).astype(np.float16).astype(np.float64) + + +def _ints(x): + """Finite float16 values (as float64) -> exact integers m with x = m * 2^-24.""" + return np.round(np.asarray(x, dtype=np.float64) * _S24).astype(np.int64) + + +def _split(m): + return m >> 20, m & _LOW + + +def round16_parts(hh, mid, ll): + """RN16 of the exact value (hh * 2^40 + mid * 2^20 + ll) * 2^-48, with ll >= 0. Returns float64 float16 values.""" + hh = np.asarray(hh, dtype=np.int64) + mid = np.asarray(mid, dtype=np.int64) + ll = np.asarray(ll, dtype=np.int64) + big = np.abs(hh) > (1 << 30) # |value| >= 2^22: infinite whatever the rest adds + hhc = np.clip(hh, -(1 << 30), 1 << 30) + # value * 2^25 = hh * 2^17 + mid2 / 8 + llr / 2^23, with mid2 = mid + (ll >> 20) and 0 <= llr < 2^20 + l1 = ll >> 20 + llr = ll & _LOW + mid2 = mid + l1 + m1 = mid2 >> 3 + midr = mid2 & 7 + q = (hhc << 17) + m1 # floor of the value in units of 2^-25 + sticky = (midr != 0) | (llr != 0) # the fraction (midr * 2^20 + llr) / 2^23 is not zero + neg = q < 0 + mag = np.where(neg, np.where(sticky, -q - 1, -q), q) + # quantum in units of 2^-25: 2 for subnormals (value < 2^-14 = 2^11 units), else 2^(e - 10) in binade 2^e units + _, ex = np.frexp(np.maximum(mag, 1).astype(np.float64)) + e = ex.astype(np.int64) - 1 + qexp = np.where(mag < (1 << 11), 1, np.maximum(e - 10, 1)) + quantum = np.left_shift(np.int64(1), qexp) + r = mag % quantum + down = mag - r + half = quantum >> 1 + odd = ((down // quantum) & 1) == 1 + up = (r > half) | ((r == half) & (sticky | odd)) + res = down + np.where(up, quantum, 0) + val = res.astype(np.float64) * 2.0 ** -25 + val = np.where((val >= 65536.0) | big, np.inf, val) + sign_neg = np.where(big, hh < 0, neg) + return np.where(sign_neg, -val, val) + + +def acc_dot16(acc, a, b, axis=-1): + """RN16(acc + sum over `axis` of a * b), exact for finite float16 inputs. a and b broadcast against each other; + the reduced axis must have length <= 16; acc broadcasts against the result.""" + a = np.asarray(a, dtype=np.float64) + b = np.asarray(b, dtype=np.float64) + acc = np.asarray(acc, dtype=np.float64) + a, b = np.broadcast_arrays(a, b) + if a.shape[axis] > 16: + raise ValueError("a chunk has at most 16 products") + fin_a, fin_b = np.isfinite(a), np.isfinite(b) + ah, al = _split(_ints(np.where(fin_a, a, 0.0))) + bh, bl = _split(_ints(np.where(fin_b, b, 0.0))) + hh = np.sum(ah * bh, axis=axis) + mid = np.sum(ah * bl + al * bh, axis=axis) + ll = np.sum(al * bl, axis=axis) + fin_acc = np.isfinite(acc) + m_acc = _ints(np.where(fin_acc, acc, 0.0)) + hh, mid, ll, m_acc = np.broadcast_arrays(hh, mid, ll, m_acc) + out = round16_parts(hh, mid + (m_acc << 4), ll) + bad = ~(np.all(fin_a & fin_b, axis=axis) & fin_acc) + if np.any(bad): + with np.errstate(invalid="ignore", over="ignore"): + slow = h(acc + np.sum(a * b, axis=axis)) + out = np.where(bad, slow, out) + return out diff --git a/clean/fg/reference/net1.py b/clean/fg/reference/net1.py new file mode 100644 index 00000000..de19cfbb --- /dev/null +++ b/clean/fg/reference/net1.py @@ -0,0 +1,231 @@ +# SPDX-License-Identifier: MIT +"""Frame generation (issue #96): net1 (the 17-blob U-Net) end to end, as a CPU reference with the delivered arithmetic. + +Written for this project from the spec side's part 3 (net1 complete). Everything here is fixed; nothing is an argument: +- the 2 x 2 fold of the 10 per-pixel values into cells: cell channel c = 4 v + q with q = 2 dy + dx (value v of the + pixel (2X + dx, 2Y + dy)); channels 40..47 are zero; pixels outside the input area are zero; +- 32 steps: 17 convolutions, five 2 x 2 max-pools, five bilinear upscales, five float16 adds (NET1_WIRING, as data); + level sizes halve with floor; skips are the encoder outputs after the activation, added to the upscaled tensor; +- convolution: stride 1, zero border, cross-correlation over the natural weights; float16 accumulator started at the + bias; chunks of 16 consecutive k (k = (3 ky + kx) * in + c, or c for 1 x 1) each summed exactly and added with one + float16 rounding (fp16acc.acc_dot16), in the chunk order of the layer (CHUNK_ORDERS); +- activation: leaky ReLU y = max(x, h(s * x)) with the delivered slope s = 0.0099945068359375, after every + convolution except the last (blob 17); +- max-pool: window 2 x 2, stride 2, m <- (x > m) * x + (x <= m) * m in float16 from the first tap; +- upscale: bilinear, align corners, from (wi, hi) to (wo, ho) with single-precision positions and float16 weights and + products (upscale_to); +- add: h(a + b); +- output: the 16 channels per cell unfold to 4 values per pixel, channel c = 4 t + q. + +Not part of net1 itself and still open: what the 10 input values are made of (images A-D, the warps) and the size of +the input grid relative to the output. The ratio (wi - 1) / (wo - 1) of the upscale is an approximate division in the +original; here it is the correctly rounded single-precision quotient (the two can differ in the last bit). +""" +import numpy as np + +from clean.fg import layout as L +from clean.fg.reference.fp16acc import acc_dot16, h + +# The slope as delivered: 0.0099945068359375 (float16 bits 0x211E). Note: nearest-even rounding of 0.01 gives the +# next value up (0x211F, 0.01000213623046875); the delivered constant is the one below it, so it is used as given. +LEAKY_SLOPE = 0.0099945068359375 +VALUES = 10 # per-pixel input values v0..v9 +CELL = 48 # cell channels: 40 used, 8 zero + +# chunk orders: "ascending" (1 x 1: channel blocks in order); "block_kx_ky": channel block outer, then the taps with +# kx outer and ky inner; "kx_block_ky": kx outer, then channel block, then ky +CHUNK_ORDERS = ("ascending", "block_kx_ky", "kx_block_ky") +LAYER_CHUNKS = {"net1_01": "block_kx_ky", "net1_03": "block_kx_ky", "net1_04": "block_kx_ky", + "net1_05": "kx_block_ky"} + + +def _conv(layer, src, out, act="leaky"): + return {"op": "conv", "layer": layer, "in": src, "out": out, "act": act, + "chunks": LAYER_CHUNKS.get(layer, "ascending")} + + +def _wiring(): + w = [_conv("net1_01", "cells", "a0"), + {"op": "pool", "in": "a0", "out": "p1"}, _conv("net1_02", "p1", "a1"), + {"op": "pool", "in": "a1", "out": "p2"}, _conv("net1_03", "p2", "a2"), + {"op": "pool", "in": "a2", "out": "p3"}, _conv("net1_04", "p3", "a3"), + {"op": "pool", "in": "a3", "out": "p4"}, _conv("net1_05", "p4", "a4"), + {"op": "pool", "in": "a4", "out": "p5"}, _conv("net1_06", "p5", "b5"), _conv("net1_07", "b5", "d5")] + prev = "d5" + for lvl, (l1, l2) in zip((4, 3, 2, 1, 0), (("net1_08", "net1_09"), ("net1_10", "net1_11"), ("net1_12", "net1_13"), + ("net1_14", "net1_15"), ("net1_16", "net1_17"))): + w += [{"op": "upscale", "in": prev, "like": "a%d" % lvl, "out": "u%d" % lvl}, + {"op": "add", "in": ["u%d" % lvl, "a%d" % lvl], "out": "s%d" % lvl}, + _conv(l1, "s%d" % lvl, "e%d" % lvl), + _conv(l2, "e%d" % lvl, "d%d" % lvl if lvl else "out", act="leaky" if lvl else "none")] + prev = "d%d" % lvl + return w + + +NET1_WIRING = _wiring() # 32 steps; the network output is buffer "out" (16 channels per cell) + + +# ---- the operations ----------------------------------------------------------------------------------------------- + +def leaky(x): + """y = max(x, h(s * x)) with s = LEAKY_SLOPE: one float16 multiply, then the max.""" + x = np.asarray(x, dtype=np.float64) + return np.maximum(x, h(LEAKY_SLOPE * x)) + + +def _chunks(order, k, n_blocks): + if order == "ascending": + if k != 1: + raise ValueError("ascending is the 1 x 1 order") + return [(0, 0, cb) for cb in range(n_blocks)] + if order == "block_kx_ky": + return [(ky, kx, cb) for cb in range(n_blocks) for kx in range(k) for ky in range(k)] + if order == "kx_block_ky": + return [(ky, kx, cb) for kx in range(k) for cb in range(n_blocks) for ky in range(k)] + raise ValueError("unknown chunk order %r" % (order,)) + + +def conv16(x, w, b, *, chunks, act): + """One net1 convolution: x [cin, H, W], natural canonical weights w [out, k, k, cin], bias b [out] (all float16 + values). Zero border, stride 1; float16 accumulator from the bias, 16-term chunks in `chunks` order; act "leaky" or + "none".""" + x = h(x) + w = h(w) + cout, k, _, cin = w.shape + if cin % 16 or x.shape[0] != cin: + raise ValueError("net1 convolutions take a multiple of 16 input channels matching the weights") + H, W = x.shape[1:] + p = k // 2 + xp = np.pad(x, ((0, 0), (p, p), (p, p))) if p else x + acc = np.broadcast_to(h(b)[:, None, None], (cout, H, W)).copy() + for ky, kx, cb in _chunks(chunks, k, cin // 16): + a = w[:, ky, kx, 16 * cb:16 * cb + 16][:, :, None, None] # out, 16, 1, 1 + xs = xp[16 * cb:16 * cb + 16, ky:ky + H, kx:kx + W] # 16, H, W + acc = acc_dot16(acc, a, xs[None], axis=1) + if act == "leaky": + return leaky(acc) + if act == "none": + return acc + raise ValueError("act must be 'leaky' or 'none'") + + +def maxpool2(x): + """2 x 2 max-pool, stride 2, floor sizes: m <- (x > m) * x + (x <= m) * m in float16, from the first tap (the + result does not depend on the tap order, also for inf and NaN).""" + x = h(x) + c, H, W = x.shape + Ho, Wo = H // 2, W // 2 + t = [x[:, dy:2 * Ho:2, dx:2 * Wo:2] for dy in (0, 1) for dx in (0, 1)] + m = t[0] + with np.errstate(invalid="ignore"): + for v in t[1:]: + m = h((v > m) * v + (v <= m) * m) + return m + + +def _axis(n_in, n_out): + """Per output index: (i0, i1, w0, w1) of the align-corners rule along one axis.""" + f32 = np.float32 + r = f32(n_in - 1) / f32(n_out - 1) if n_out > 1 else f32(0) + i0s, i1s, w0s, w1s = [], [], [], [] + for o in range(n_out): + s = f32(r * f32(o)) + i0 = int(np.trunc(s)) + i1 = int(np.trunc(f32(s + f32(1)))) + if i1 >= n_in: + i0 = i1 = n_in - 1 + w1 = float(h(f32(s - f32(i0)))) + w0 = float(h(f32(f32(f32(i0) - s) + f32(1)))) + i0s.append(i0); i1s.append(i1); w0s.append(w0); w1s.append(w1) + return np.array(i0s), np.array(i1s), np.array(w0s), np.array(w1s) + + +def upscale_to(p, wo, ho): + """Bilinear upscale, align corners, from p [c, hi, wi] to [c, ho, wo]: single-precision positions, float16 weights, + every product and sum rounded to float16: top = h(h(wx0 P00) + h(wx1 P01)), out = h(h(wy0 top) + h(wy1 bottom)).""" + p = h(p) + _, hi, wi = p.shape + x0, x1, wx0, wx1 = _axis(wi, wo) + y0, y1, wy0, wy1 = _axis(hi, ho) + + def row(r): + return h(h(wx0 * r[:, :, x0]) + h(wx1 * r[:, :, x1])) + top = row(p[:, y0, :]) + bot = row(p[:, y1, :]) + return h(h(wy0[None, :, None] * top) + h(wy1[None, :, None] * bot)) + + +def add16(a, b): + return h(h(a) + h(b)) + + +# ---- fold, wiring, network ---------------------------------------------------------------------------------------- + +def fold(values): + """values [10, G1h, G1w] -> cells [48, ceil(G1h / 2), ceil(G1w / 2)]: channel 4 v + 2 dy + dx; pixels outside the + input area and channels 40..47 are zero.""" + v = h(values) + if v.shape[0] != VALUES: + raise ValueError("net1 takes %d values per pixel" % VALUES) + _, gh, gw = v.shape + H0, W0 = -(-gh // 2), -(-gw // 2) + padded = np.zeros((VALUES, 2 * H0, 2 * W0)) + padded[:, :gh, :gw] = v + cells = np.zeros((CELL, H0, W0)) + for dy in (0, 1): + for dx in (0, 1): + cells[2 * dy + dx:4 * VALUES:4] = padded[:, dy::2, dx::2] + return cells + + +def unfold(out, gh, gw): + """out [16, H0, W0] -> [4, gh, gw]: value t of pixel (2X + dx, 2Y + dy) is channel 4 t + 2 dy + dx.""" + _, H0, W0 = out.shape + px = np.zeros((4, 2 * H0, 2 * W0)) + for dy in (0, 1): + for dx in (0, 1): + px[:, dy::2, dx::2] = out[2 * dy + dx::4][:4] + return px[:, :gh, :gw] + + +def run(cells, params, wiring=NET1_WIRING): + """Runs net1's steps on cells [48, H0, W0]; params: {layer: (canonical weights [out, k, k, in], bias)} (forward.py's + parameters). Returns every buffer.""" + buf = {"cells": h(cells)} + for s in wiring: + kind = s["op"] + if kind == "conv": + w, b = params[s["layer"]] + y = conv16(buf[s["in"]], w, b, chunks=s["chunks"], act=s["act"]) + elif kind == "pool": + y = maxpool2(buf[s["in"]]) + elif kind == "upscale": + ref = buf[s["like"]] + y = upscale_to(buf[s["in"]], ref.shape[2], ref.shape[1]) + elif kind == "add": + y = add16(buf[s["in"][0]], buf[s["in"][1]]) + else: + raise ValueError("unknown step %r" % kind) + buf[s["out"]] = y + return buf + + +def min_input_size(): + """The smallest input grid side (pixels) for which every level has at least one cell: 2 * 2^5.""" + return 64 + + +def forward(values, params): + """net1 on its per-pixel values [10, G1h, G1w] -> [4, G1h, G1w].""" + _, gh, gw = np.shape(values) + if min(gh, gw) < min_input_size(): + n = min_input_size() + raise ValueError("net1 needs an input of at least %d x %d pixels (five pooling levels)" % (n, n)) + buf = run(fold(values), params) + return unfold(buf["out"], gh, gw) + + +def check_wiring_layers(): + """Every net1 layer is used exactly once, in upload order, with the activation rule.""" + convs = [s for s in NET1_WIRING if s["op"] == "conv"] + return [s["layer"] for s in convs] == [l.name for l in L.NET1] diff --git a/clean/fg/tests/test_fg_forward.py b/clean/fg/tests/test_fg_forward.py index cfe24ecf..8a972da6 100644 --- a/clean/fg/tests/test_fg_forward.py +++ b/clean/fg/tests/test_fg_forward.py @@ -157,23 +157,18 @@ def test_net1_stand_in(self): {"op": "conv", "layer": "net1_16", "in": "d0", "out": "d0b", "relu": True}, # 32 -> 32 {"op": "conv", "layer": "net1_17", "in": "d0b", "out": "out", "relu": False}, # 32 -> 16 ] + # the generic engine (kept for net2) still runs any wiring over net1's layers; net1 itself has its delivered + # wiring (clean/fg/reference/net1.py, tested in test_fg_net1.py) + cells = ops.fold2x2(feats, order="c dy dx") for pad in ("zero", "replicate"): - got = F.net1_forward(feats, self.params, wiring=wiring, pad=pad, fold_order="dy dx c", output="out") - cells = ops.fold2x2(feats, order="dy dx c") + got = F.run_graph(wiring, {"cells": cells}, self.params, pad=pad)["out"] e1 = self.direct("net1_02", self.direct("net1_01", cells, pad, True), pad, True) d1 = self.direct("net1_14", ops.pool2x(e1, kind="max"), pad, True) s1 = f16(ops.upscale2x_bilinear(d1) + e1) d0 = self.direct("net1_16", self.direct("net1_15", s1, pad, True), pad, True) - want = ops.unfold2x2(self.direct("net1_17", d0, pad, False), 4, order="dy dx c") - self.assertEqual(got.shape, (4, 8, 12)) - np.testing.assert_array_equal(got, want, err_msg=pad) - for missing in ("wiring", "pad", "fold_order", "output"): - kw = dict(wiring=wiring, pad="zero", fold_order="dy dx c", output="out") - del kw[missing] - with self.assertRaises(TypeError, msg=missing): - F.net1_forward(feats, self.params, **kw) - with self.assertRaises(ValueError): # the output must have 16 values per cell - F.net1_forward(feats, self.params, wiring=wiring, pad="zero", fold_order="dy dx c", output="d0") + np.testing.assert_array_equal(got, self.direct("net1_17", d0, pad, False), err_msg=pad) + with self.assertRaises(TypeError): # net1_forward takes no wiring: it has its own + F.net1_forward(feats, self.params, wiring=wiring) def test_net2_stand_in(self): rng = np.random.default_rng(8) diff --git a/clean/fg/tests/test_fg_fp16acc.py b/clean/fg/tests/test_fg_fp16acc.py new file mode 100644 index 00000000..6d88ccab --- /dev/null +++ b/clean/fg/tests/test_fg_fp16acc.py @@ -0,0 +1,126 @@ +# SPDX-License-Identifier: MIT +"""Frame generation (#96): exact float16 chunk accumulation (clean/fg/reference/fp16acc.py) against an exact rational +reference written here (fractions.Fraction, nearest-even rounding to float16 by comparing neighbours). + + python -m unittest clean.fg.tests.test_fg_fp16acc +""" +import unittest +from fractions import Fraction + +import numpy as np + +from clean.fg.reference import fp16acc as A + + +def rn16_exact(q): + """Fraction -> nearest float16 (ties to even), as a Python float; overflow to +-inf at 65520.""" + if q >= 65520: + return float("inf") + if q <= -65520: + return float("-inf") + c = np.float16(float(q)) # within one float16 step of the answer + cands = {float(c), float(np.nextafter(c, np.float16(np.inf))), float(np.nextafter(c, np.float16(-np.inf)))} + cands = [x for x in cands if np.isfinite(x)] + best = min(cands, key=lambda x: (abs(Fraction(x) - q), + int(np.float16(x).view(np.uint16)) & 1)) # nearest, then even mantissa + if best == 0.0 and q < 0: + return -0.0 + return best + + +def exact_acc(acc, a, b): + return rn16_exact(Fraction(float(acc)) + sum(Fraction(float(x)) * Fraction(float(y)) for x, y in zip(a, b))) + + +def f16_values(rng, n, kind): + if kind == "small": + return rng.uniform(-1, 1, n).astype(np.float16) + if kind == "wide": # every exponent, both signs + bits = rng.integers(0, 0x7C00, n, dtype=np.uint16) | (rng.integers(0, 2, n, dtype=np.uint16) << 15) + return bits.view(np.float16) + if kind == "subnormal": + bits = rng.integers(0, 0x400, n, dtype=np.uint16) | (rng.integers(0, 2, n, dtype=np.uint16) << 15) + return bits.view(np.float16) + if kind == "large": + return rng.uniform(-65504, 65504, n).astype(np.float16) + raise ValueError(kind) + + +class Exactness(unittest.TestCase): + def check(self, acc, a, b): + got = A.acc_dot16(acc.astype(np.float64), a.astype(np.float64), b.astype(np.float64)) + for i in range(len(acc)): + want = exact_acc(acc[i], a[i], b[i]) + g = float(got[i]) + ok = (g == want and np.signbit(g) == np.signbit(want)) or (np.isnan(g) and np.isnan(want)) + self.assertTrue(ok, "row %d: got %r want %r (acc %r, a %r, b %r)" % (i, g, want, acc[i], a[i], b[i])) + + def test_random_kinds(self): + rng = np.random.default_rng(1) + for ka, kb in (("small", "small"), ("wide", "small"), ("wide", "wide"), ("subnormal", "wide"), + ("subnormal", "subnormal"), ("large", "small"), ("large", "large")): + n = 300 + acc = f16_values(rng, n, "wide" if ka == "wide" else "small") + a = f16_values(rng, n * 16, ka).reshape(n, 16) + b = f16_values(rng, n * 16, kb).reshape(n, 16) + with self.subTest(a=ka, b=kb): + self.check(acc, a, b) + + def test_ties_and_cancellation(self): + # acc + exact half-ulp sums: ties to even, and a sticky bit far below breaking the tie + one = np.float16(1.0) + ulp = float(np.nextafter(one, np.float16(2)) - one) # 2^-10 + rows_acc, rows_a, rows_b = [], [], [] + for extra in (0.0, 2.0 ** -40, -2.0 ** -40): + a = np.zeros(16, dtype=np.float16) + b = np.zeros(16, dtype=np.float16) + a[0], b[0] = np.float16(ulp / 2), np.float16(1) # + half an ulp: a tie + if extra: + a[1], b[1] = np.float16(np.sign(extra) * 2.0 ** -20), np.float16(2.0 ** -20) + for start in (1.0, 1.0 + ulp): + rows_acc.append(np.float16(start)) + rows_a.append(a.copy()) + rows_b.append(b.copy()) + # exact cancellation to zero, and to a tiny negative value (-0 after rounding) + a = np.zeros(16, dtype=np.float16); b = np.zeros(16, dtype=np.float16) + a[0], b[0], a[1], b[1] = 3, 5, -3, 5 + rows_acc.append(np.float16(0)); rows_a.append(a.copy()); rows_b.append(b.copy()) + a[2], b[2] = np.float16(-2.0 ** -24), np.float16(2.0 ** -10) + rows_acc.append(np.float16(0)); rows_a.append(a.copy()); rows_b.append(b.copy()) + self.check(np.array(rows_acc), np.array(rows_a), np.array(rows_b)) + + def test_overflow(self): + a = np.full((3, 16), 255.0, dtype=np.float16) + b = np.full((3, 16), 255.0, dtype=np.float16) + b[1] *= -1 + acc = np.array([0, 0, 65504], dtype=np.float16) + got = A.acc_dot16(acc.astype(np.float64), a.astype(np.float64), b.astype(np.float64)) + self.assertEqual(list(got[:2]), [np.inf, -np.inf]) + # 65504 + 16 * 255^2 overflows; just below the threshold stays finite + self.assertEqual(got[2], np.inf) + z = np.zeros((1, 16)); z[0, 0] = 15.0 + self.assertEqual(float(A.acc_dot16(np.array([65504.0]), z, np.ones((1, 16)))[0]), 65504.0) # 65519 -> 65504 + z[0, 0] = 16.0 + self.assertEqual(float(A.acc_dot16(np.array([65504.0]), z, np.ones((1, 16)))[0]), np.inf) # 65520 -> inf + + def test_non_finite(self): + a = np.zeros((4, 16)); b = np.ones((4, 16)) + a[0, 0] = np.inf + a[1, 0], a[1, 1] = np.inf, -np.inf + a[2, 0], b[2, 0] = np.inf, 0.0 + a[3, 0] = np.nan + got = A.acc_dot16(np.zeros(4), a, b) + self.assertEqual(got[0], np.inf) + self.assertTrue(np.isnan(got[1]) and np.isnan(got[2]) and np.isnan(got[3])) + self.assertTrue(np.isnan(A.acc_dot16(np.array([np.nan]), np.zeros((1, 16)), np.zeros((1, 16)))[0])) + + def test_shape_rules(self): + with self.assertRaises(ValueError): + A.acc_dot16(np.zeros(2), np.zeros((2, 17)), np.zeros((2, 17))) + out = A.acc_dot16(np.zeros((3, 1, 1)), np.ones((3, 16, 1, 1)), np.ones((16, 4, 5)), axis=1) + self.assertEqual(out.shape, (3, 4, 5)) + self.assertTrue(np.all(out == 16.0)) + + +if __name__ == "__main__": + unittest.main() diff --git a/clean/fg/tests/test_fg_net1.py b/clean/fg/tests/test_fg_net1.py new file mode 100644 index 00000000..ddd07f54 --- /dev/null +++ b/clean/fg/tests/test_fg_net1.py @@ -0,0 +1,371 @@ +# SPDX-License-Identifier: MIT +"""Frame generation (#96): net1 with the delivered wiring and arithmetic (clean/fg/reference/net1.py), on synthetic +weights and inputs, against versions written separately here: scalar loops for the upscale rule, exact rationals for +one convolution, and a hand-composed network (float64 chunk sums, which are exact for this data, numpy max for the +pool, the upscale tables from the scalar rule). + + python -m unittest clean.fg.tests.test_fg_net1 +""" +import itertools +import os +import struct +import tempfile +import unittest +from fractions import Fraction + +import numpy as np + +from clean.fg import layout as L +from clean.fg.reference import forward as F +from clean.fg.reference import net1 as N +from clean.fg.reference import ops +from clean.fg.tests.test_fg_fp16acc import rn16_exact + +S = 0.0099945068359375 # the delivered slope (float16 0x211E) + + +def f16(x): + return np.asarray(x, dtype=np.float64).astype(np.float16).astype(np.float64) + + +def synthetic_params(rng, scale=0.05): + return {name: (rng.uniform(-scale, scale, ops.weight_shape(l)).astype(np.float16), + rng.uniform(-0.1, 0.1, l.out).astype(np.float16)) for name, l in F.LAYERS.items()} + + +# ---- scalar / independent versions ---------------------------------------------------------------------------------- + +def axis_scalar(n_in, n_out): + """The align-corners rule of part 3 for one axis, one index at a time with numpy float32 scalars.""" + r = np.float32(n_in - 1) / np.float32(n_out - 1) if n_out > 1 else np.float32(0.0) + rows = [] + for o in range(n_out): + s = np.float32(r * np.float32(o)) + x0 = int(s) # trunc (s >= 0) + x1 = int(np.float32(s + np.float32(1.0))) + if x1 >= n_in: + x0 = x1 = n_in - 1 + w1 = np.float16(np.float32(s - np.float32(x0))) + w0 = np.float16(np.float32(np.float32(np.float32(x0) - s) + np.float32(1.0))) + rows.append((x0, x1, float(w0), float(w1))) + return rows + + +def upscale_scalar(p, wo, ho): + c, hi, wi = p.shape + ax, ay = axis_scalar(wi, wo), axis_scalar(hi, ho) + out = np.zeros((c, ho, wo)) + for ch in range(c): + for y, (y0, y1, wy0, wy1) in enumerate(ay): + for x, (x0, x1, wx0, wx1) in enumerate(ax): + top = np.float16(np.float16(wx0 * p[ch, y0, x0]) + np.float16(wx1 * p[ch, y0, x1])) + bot = np.float16(np.float16(wx0 * p[ch, y1, x0]) + np.float16(wx1 * p[ch, y1, x1])) + out[ch, y, x] = np.float16(np.float16(wy0 * float(top)) + np.float16(wy1 * float(bot))) + return out + + +def chunk_list(order, k, nb): + if order == "ascending": + return [(0, 0, cb) for cb in range(nb)] + if order == "block_kx_ky": + return [(ky, kx, cb) for cb in range(nb) for kx in range(k) for ky in range(k)] + return [(ky, kx, cb) for kx in range(k) for cb in range(nb) for ky in range(k)] + + +def conv_exact(x, w, b, order, act): + """One convolution, element by element, with exact rational chunk sums and nearest-even float16 rounding.""" + cout, k, _, cin = w.shape + _, H, W = x.shape + p = k // 2 + out = np.zeros((cout, H, W)) + for o in range(cout): + for yy in range(H): + for xx in range(W): + acc = float(b[o]) + for ky, kx, cb in chunk_list(order, k, cin // 16): + sy, sx = yy + ky - p, xx + kx - p + s = Fraction(acc) + if 0 <= sy < H and 0 <= sx < W: + for c in range(16 * cb, 16 * cb + 16): + s += Fraction(float(w[o, ky, kx, c])) * Fraction(float(x[c, sy, sx])) + acc = rn16_exact(s) + if act == "leaky": + acc = max(acc, float(np.float16(S * acc))) + out[o, yy, xx] = acc + return out + + +def conv_hand(x, w, b, order, act): + """Hand-composed convolution: per chunk a float64 sum (exact for small synthetic data), then float16.""" + cout, k, _, cin = w.shape + _, H, W = x.shape + p = k // 2 + xp = np.pad(x, ((0, 0), (p, p), (p, p))) + acc = np.broadcast_to(np.asarray(b, dtype=np.float64)[:, None, None], (cout, H, W)).astype(np.float16) + for ky, kx, cb in chunk_list(order, k, cin // 16): + part = np.einsum("oc,chw->ohw", np.asarray(w[:, ky, kx, 16 * cb:16 * cb + 16], dtype=np.float64), + xp[16 * cb:16 * cb + 16, ky:ky + H, kx:kx + W]) + acc = (acc.astype(np.float64) + part).astype(np.float16) + acc = acc.astype(np.float64) + if act == "leaky": + acc = np.maximum(acc, (np.float16(S) * acc.astype(np.float16)).astype(np.float64)) + return acc + + +def net1_hand(values, params): + """The 32 steps written out by hand.""" + gh, gw = values.shape[1:] + H0, W0 = (gh + 1) // 2, (gw + 1) // 2 + pad = np.zeros((10, 2 * H0, 2 * W0)) + pad[:, :gh, :gw] = f16(values) + cells = np.zeros((48, H0, W0)) + for v in range(10): + for dy in range(2): + for dx in range(2): + cells[4 * v + 2 * dy + dx] = pad[v, dy::2, dx::2] + + def C(name, x, act="leaky"): + w, b = params[name] + order = {"net1_01": "block_kx_ky", "net1_03": "block_kx_ky", "net1_04": "block_kx_ky", + "net1_05": "kx_block_ky"}.get(name, "ascending") + return conv_hand(x, w, b, order, act) + + def P(x): + c, h, w = x.shape + return x[:, :h // 2 * 2, :w // 2 * 2].reshape(c, h // 2, 2, w // 2, 2).max(axis=(2, 4)) + + def U(x, like): + c, hi, wi = x.shape + ax, ay = axis_scalar(wi, like.shape[2]), axis_scalar(hi, like.shape[1]) + x0 = [r[0] for r in ax]; x1 = [r[1] for r in ax] + wx0 = np.array([r[2] for r in ax]); wx1 = np.array([r[3] for r in ax]) + y0 = [r[0] for r in ay]; y1 = [r[1] for r in ay] + wy0 = np.array([r[2] for r in ay])[None, :, None]; wy1 = np.array([r[3] for r in ay])[None, :, None] + t = f16(f16(wx0 * x[:, y0][:, :, x0]) + f16(wx1 * x[:, y0][:, :, x1])) + bt = f16(f16(wx0 * x[:, y1][:, :, x0]) + f16(wx1 * x[:, y1][:, :, x1])) + return f16(f16(wy0 * t) + f16(wy1 * bt)) + + def A(a, b): + return f16(a + b) + + a0 = C("net1_01", cells) + a1 = C("net1_02", P(a0)) + a2 = C("net1_03", P(a1)) + a3 = C("net1_04", P(a2)) + a4 = C("net1_05", P(a3)) + d = C("net1_07", C("net1_06", P(a4))) + d = C("net1_09", C("net1_08", A(U(d, a4), a4))) + d = C("net1_11", C("net1_10", A(U(d, a3), a3))) + d = C("net1_13", C("net1_12", A(U(d, a2), a2))) + d = C("net1_15", C("net1_14", A(U(d, a1), a1))) + out = C("net1_17", C("net1_16", A(U(d, a0), a0)), act="none") + px = np.zeros((4, 2 * H0, 2 * W0)) + for dy in range(2): + for dx in range(2): + for t in range(4): + px[t, dy::2, dx::2] = out[4 * t + 2 * dy + dx] + return px[:, :gh, :gw] + + +# ---- tests ---------------------------------------------------------------------------------------------------------- + +class Operations(unittest.TestCase): + def test_leaky(self): + self.assertEqual(N.LEAKY_SLOPE, 0.0099945068359375) + self.assertEqual(int(np.float16(N.LEAKY_SLOPE).view(np.uint16)), 0x211E) + x = f16(np.array([-65504, -100, -1, -2 ** -24, -0.0, 0, 2 ** -24, 1, 65504, np.inf, -np.inf])) + want = [max(v, float(np.float16(S * v))) for v in x] + np.testing.assert_array_equal(N.leaky(x), want) + self.assertEqual(N.leaky(np.array([-1.0]))[0], float(np.float16(-S))) + + def test_maxpool_finite_and_floor(self): + rng = np.random.default_rng(1) + x = f16(rng.uniform(-5, 5, (3, 7, 9))) + got = N.maxpool2(x) + self.assertEqual(got.shape, (3, 3, 4)) + np.testing.assert_array_equal(got, x[:, :6, :8].reshape(3, 3, 2, 4, 2).max(axis=(2, 4))) + + def test_maxpool_specials_do_not_depend_on_tap_order(self): + vals = [1.0, -2.0, np.inf, -np.inf, np.nan, 0.0, 65504.0, -65504.0] + got_any = [] + for window in itertools.product(vals, repeat=4): + x = np.array(window, dtype=np.float64).reshape(1, 2, 2) + got = N.maxpool2(x)[0, 0, 0] + results = set() + for perm in itertools.permutations(window): + m = perm[0] + with np.errstate(invalid="ignore"): + for v in perm[1:]: + m = float(np.float16((v > m) * v + (v <= m) * m)) + results.add("nan" if np.isnan(m) else m) + self.assertEqual(len(results), 1, window) + want = results.pop() + self.assertTrue((want == "nan" and np.isnan(got)) or got == want, (window, got, want)) + got_any.append(got) + self.assertTrue(any(np.isnan(g) for g in got_any)) + + def test_upscale_against_scalar_rule(self): + rng = np.random.default_rng(2) + for (hi, wi), (ho, wo) in (((1, 1), (2, 2)), ((2, 2), (4, 5)), ((3, 4), (7, 9)), ((5, 2), (11, 4)), + ((1, 3), (1, 7)), ((4, 4), (8, 8)), ((2, 3), (2, 3))): + p = f16(rng.uniform(-3, 3, (2, hi, wi))) + np.testing.assert_array_equal(N.upscale_to(p, wo, ho), upscale_scalar(p, wo, ho), + err_msg="%s -> %s" % ((hi, wi), (ho, wo))) + + def test_upscale_corners_and_extent_one(self): + p = f16(np.array([[[1.0, 2.0], [3.0, 4.0]]])) + y = N.upscale_to(p, 5, 5) + self.assertEqual((y[0, 0, 0], y[0, 0, -1], y[0, -1, 0], y[0, -1, -1]), (1.0, 2.0, 3.0, 4.0)) + np.testing.assert_array_equal(N.upscale_to(p, 1, 1), [[[1.0]]]) # ratio 0: the first sample + + def test_conv_against_exact_rationals(self): + rng = np.random.default_rng(3) + for k, cin, order in ((3, 32, "block_kx_ky"), (3, 32, "kx_block_ky"), (1, 48, "ascending")): + x = f16(rng.uniform(-2, 2, (cin, 3, 4))) + w = f16(rng.uniform(-1, 1, (2, k, k, cin))) + b = f16(rng.uniform(-1, 1, 2)) + for act in ("leaky", "none"): + got = N.conv16(x, w, b, chunks=order, act=act) + np.testing.assert_array_equal(got, conv_exact(x, w, b, order, act), err_msg="%s %s %s" % (k, order, act)) + + def test_chunk_order_is_honoured(self): + # a large partial sum that later cancels: the float16 rounding of intermediate sums depends on the order + x = np.zeros((32, 1, 1)); w = np.zeros((1, 3, 3, 32)) + x[0, 0, 0] = 1.0; x[16, 0, 0] = 1.0 + w[0, 1, 1, 0] = 2048.0; w[0, 1, 1, 16] = 1.0 # centre tap, both channel blocks + b = np.array([1.0]) + a = N.conv16(x, w, b, chunks="block_kx_ky", act="none") # 1 + 2048 = 2049 -> 2048, then + 1 -> 2049 -> 2048 + self.assertEqual(a[0, 0, 0], 2048.0) + w2 = np.zeros((1, 3, 3, 32)); w2[0, 1, 1, 16] = 2048.0; w2[0, 1, 1, 0] = -2048.0 + self.assertEqual(N.conv16(x, w2, b, chunks="kx_block_ky", act="none")[0, 0, 0], 1.0) + with self.assertRaises(ValueError): + N.conv16(x, w, b, chunks="ascending", act="none") # 3 x 3 has no ascending order + with self.assertRaises(ValueError): + N.conv16(x, w, b, chunks="block_kx_ky", act="relu") + + def test_zero_border(self): + x = np.ones((16, 2, 2)); w = np.ones((1, 3, 3, 16)) / 16; b = np.zeros(1) + y = N.conv16(x, f16(w), b, chunks="block_kx_ky", act="none") + np.testing.assert_array_equal(y[0], [[4.0, 4.0], [4.0, 4.0]]) # a 2 x 2 grid: 4 taps inside per output + + +class FoldAndWiring(unittest.TestCase): + def test_fold_unfold(self): + rng = np.random.default_rng(4) + v = f16(rng.uniform(0, 1, (10, 5, 7))) + cells = N.fold(v) + self.assertEqual(cells.shape, (48, 3, 4)) + self.assertEqual(cells[4 * 6 + 2 * 1 + 0, 1, 2], v[6, 3, 4]) # value 6 of pixel (x=4, y=3): dx 0, dy 1 + np.testing.assert_array_equal(cells[40:], 0.0) + self.assertEqual(cells[4 * 2 + 1, 2, 3], 0.0) # pixel (7, 4) is outside the 7 x 5 input + out = np.zeros((16, 3, 4)); out[4 * 3 + 2 * 1 + 1] = 5.0 + px = N.unfold(out, 5, 7) + self.assertEqual(px.shape, (4, 5, 7)) + self.assertEqual(px[3, 1, 1], 5.0) + self.assertEqual(px[3, 0, 0], 0.0) + + def test_wiring_data(self): + w = N.NET1_WIRING + self.assertEqual(len(w), 32) + self.assertEqual([s["op"] for s in w].count("conv"), 17) + self.assertEqual([s["op"] for s in w].count("pool"), 5) + self.assertEqual([s["op"] for s in w].count("upscale"), 5) + self.assertEqual([s["op"] for s in w].count("add"), 5) + self.assertTrue(N.check_wiring_layers()) + acts = [s["act"] for s in w if s["op"] == "conv"] + self.assertEqual(acts, ["leaky"] * 16 + ["none"]) + chunks = {s["layer"]: s["chunks"] for s in w if s["op"] == "conv"} + self.assertEqual({k for k, v in chunks.items() if v != "ascending"}, {"net1_01", "net1_03", "net1_04", "net1_05"}) + self.assertEqual(chunks["net1_05"], "kx_block_ky") + self.assertEqual(w[-1]["out"], "out") + + def test_level_sizes(self): + rng = np.random.default_rng(5) + params = synthetic_params(rng) + buf = N.run(N.fold(f16(rng.uniform(0, 1, (10, 70, 133)))), params) + sizes = [buf["a%d" % i].shape[1:] for i in range(5)] + [buf["d5"].shape[1:]] + self.assertEqual(sizes, [(35, 67), (17, 33), (8, 16), (4, 8), (2, 4), (1, 2)]) + self.assertEqual(buf["out"].shape, (16, 35, 67)) + with self.assertRaises(ValueError): + N.forward(np.zeros((10, 63, 64)), params) + + +class EndToEnd(unittest.TestCase): + def test_against_hand_composition(self): + rng = np.random.default_rng(6) + params = synthetic_params(rng) + for gh, gw in ((64, 64), (66, 71)): + v = f16(rng.uniform(0, 1, (10, gh, gw))) + got = F.net1_forward(v, params) + self.assertEqual(got.shape, (4, gh, gw)) + np.testing.assert_array_equal(got, net1_hand(v, params), err_msg="%dx%d" % (gh, gw)) + self.assertTrue(np.all(np.isfinite(got))) + + +def write_model_file(path, params, m): + """A minimal model.wgcm (container version 1) holding stages dl1.lNN / dl2.lNN with W (natural map shape) and b, + written here from the format description; digests and key are zeros (the reader does not check them).""" + from clean.fg.reference import natural as Nat + stages = [] + for name, l in F.LAYERS.items(): + w, b = params[name] + nat = w.transpose(0, 3, 1, 2) if w.shape[1] == 3 else w[:, 0, 0, :] + assert nat.shape == Nat.weight_shape(l, m) + stages.append((Nat.stage_name(l), [("W", nat.astype(" [--record ] + +The input is synthetic and fixed (seeded random values in [0, 1), SIZE x SIZE pixels; value 9 is 1 - value 8 as in +net1's input packing). The record holds the SHA-256 of the output (float16, little endian, [4, SIZE, SIZE] row-major), +its shape, the number of non-finite values and the model file's SHA-256: digests only, never weights or values. +""" +import argparse +import hashlib +import json +import sys + +import numpy as np + +from clean.fg.reference import forward as F + +SIZE = 64 # pixels per side: the smallest grid with a cell on every level +SEED = 20261114 + + +def fixed_input(): + rng = np.random.default_rng(SEED) + v = rng.uniform(0.0, 1.0, (10, SIZE, SIZE)).astype(np.float16).astype(np.float64) + v[9] = (1.0 - v[8]).astype(np.float16) + return v + + +def digest(model_path): + params = F.params_from_model_file(model_path) + out = F.net1_forward(fixed_input(), params).astype("