From 65e62a386d24948a5f65a5613711e89e5386313b Mon Sep 17 00:00:00 2001 From: phantoslayer21 <156617979+phantoslayer21@users.noreply.github.com> Date: Sat, 14 Nov 2026 00:18:33 -0500 Subject: [PATCH] Frame generation: CPU reference of the networks' building blocks clean/fg/reference/ops.py (NumPy, float64 with the float16 roundings): float16 convolution (3x3, 1x1) with bias and ReLU, 2x pooling, bilinear 2x upscale in the align-corners form, element-wise combines, net1's input features (luma, warp error, mask pair) and the 2x2 fold into 48-value cells. Behaviour not yet delivered is a required argument (border rule, ReLU per layer, pooling kind, channel and fold order) or raises OpenItem (float16 accumulation order, both networks' wiring). FORMATS.md lists them. Refs #96 Signed-off-by: phantoslayer21 <156617979+phantoslayer21@users.noreply.github.com> --- clean/fg/FORMATS.md | 20 +++ clean/fg/README.md | 2 + clean/fg/reference/__init__.py | 1 + clean/fg/reference/ops.py | 218 ++++++++++++++++++++++++++++ clean/fg/tests/test_fg_reference.py | 196 +++++++++++++++++++++++++ 5 files changed, 437 insertions(+) create mode 100644 clean/fg/reference/__init__.py create mode 100644 clean/fg/reference/ops.py create mode 100644 clean/fg/tests/test_fg_reference.py diff --git a/clean/fg/FORMATS.md b/clean/fg/FORMATS.md index b3eda42a..859722e8 100644 --- a/clean/fg/FORMATS.md +++ b/clean/fg/FORMATS.md @@ -24,6 +24,23 @@ delivered by the spec side; the code never guesses it (it raises, or takes the c - A conversion of a real library will be checked by digests only (the converter compares every stage digest the map gives); no value or byte of it is ever written into the repository or any public text. +## CPU reference (`reference/ops.py`) + +- Tensors are channel-first `x[c, y, x]`; weights use the module's own canonical order `w[out, ky, kx, in]` + (`weight_shape(layer)` for every layout layer). Converting a blob into that order waits for item 2 below. +- Settled and implemented: float16 operands and outputs; convolution with bias, one float16 rounding of the output, + then ReLU; 2x pooling over 2 x 2 blocks; bilinear 2x upscale in the align-corners form (output sample i reads input + position i (n - 1) / (2n - 1)); element-wise add and channel concatenation; net1's features: luma with fixed RGB + weights, warp error as the mean absolute RGB difference clamped to [0, 1], a mask and its complement; the 2 x 2 fold + of ten features into a cell of 40 values padded to 48. +- Arithmetic: float64 sums with a single float16 rounding per output (`acc="exact"`). The original sums in float16 in + its matrix unit's order (OPEN): `acc="f16"` raises `OpenItem`, and a later GPU comparison has to allow for that + difference until the order is known. +- Required arguments where the behaviour is OPEN: the convolution border rule (`pad`: zero, replicate or mirror), + ReLU after a net2 layer (`relu_after`), the pooling kind (`kind`: max or avg), net1's feature channel order + (`order`), the fold order inside a cell (`order`). What the cell padding holds is OPEN too (zeros here). +- `net1_forward` and `net2_forward` raise `OpenItem`: the wiring of both networks is not delivered. + ## OPEN (waiting for the spec side) 1. The converter map file. @@ -34,3 +51,6 @@ delivered by the spec side; the code never guesses it (it raises, or takes the c 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). 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. diff --git a/clean/fg/README.md b/clean/fg/README.md index 9d4f704f..72965024 100644 --- a/clean/fg/README.md +++ b/clean/fg/README.md @@ -7,6 +7,8 @@ front end or the launcher yet. Status, open items and formats: [FORMATS.md](FORM |---|---| | `layout.py` | the weight layout of both networks as structure only: per layer its shape, and the byte size and inner layout of each blob, in the library's upload order; totals | | `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 | | `../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" | ``` diff --git a/clean/fg/reference/__init__.py b/clean/fg/reference/__init__.py new file mode 100644 index 00000000..548d2d44 --- /dev/null +++ b/clean/fg/reference/__init__.py @@ -0,0 +1 @@ +# SPDX-License-Identifier: MIT diff --git a/clean/fg/reference/ops.py b/clean/fg/reference/ops.py new file mode 100644 index 00000000..c4e7603e --- /dev/null +++ b/clean/fg/reference/ops.py @@ -0,0 +1,218 @@ +# SPDX-License-Identifier: MIT +"""Frame generation (issue #96): NumPy CPU reference of the building blocks of its two convolution networks. + +Written for this project. float64 arithmetic with only the prescribed float16 roundings, like the other oracles here. +What is settled: float16 tensors in and out, convolutions with a bias, ReLU, a 2x pooling, a bilinear 2x upscale in +the align-corners form, element-wise combines, and net1's per-pixel input features (two lumas with fixed weights, +two warp errors clamped to [0, 1], a mask and its complement) folded 2 x 2 into cells padded to 48 values. + +What is not settled is OPEN (clean/fg/FORMATS.md). The code never picks a default for it: such a choice is a required +argument (the caller states it, and tests run every allowed value), or the function raises OpenItem. + +Tensors are channel-first: x[c, y, x]. Weights here are in this module's own canonical order w[out, ky, kx, in]; +the order inside the library's blobs is OPEN, so converting a blob into this order is not part of this module. +""" +import numpy as np + +from clean.reference.fp8 import f16 + + +class OpenItem(NotImplementedError): + """A behaviour the spec side has not delivered yet (clean/fg/FORMATS.md, "OPEN").""" + + +# ---- settled pieces ------------------------------------------------------------------------------------------------- + +LUMA_WEIGHTS = (0.299, 0.587, 0.114) # net1's luma inputs: fixed weights on R, G, B +NET1_FEATURES = 10 # float16 values per pixel of net1's sampling grid +NET1_CELL = 48 # one 2 x 2 cell: 4 * 10 = 40 values, padded to 48 +PAD_MODES = ("zero", "replicate", "mirror") # the 3 x 3 border rule is OPEN: the caller picks one of these +POOL_KINDS = ("max", "avg") # the 2x pooling kind is OPEN: the caller picks one of these + + +def relu(x): + return np.maximum(np.asarray(x, dtype=np.float64), 0.0) + + +def _pad(x, p, mode): + if p == 0: + return x + if mode == "zero": + return np.pad(x, ((0, 0), (p, p), (p, p)), mode="constant") + if mode == "replicate": + return np.pad(x, ((0, 0), (p, p), (p, p)), mode="edge") + if mode == "mirror": + return np.pad(x, ((0, 0), (p, p), (p, p)), mode="reflect") + raise ValueError("pad must be one of %s (the border rule is OPEN; no default)" % (PAD_MODES,)) + + +def conv2d(x, w, b, *, pad, relu_after, acc="exact"): + """One convolution layer: stride 1, 'same' size, odd square kernel (3 x 3 or 1 x 1). + + x [cin, H, W], w [out, k, k, cin], b [out]: inputs, weights and bias are rounded to float16 first (the layers + take float16 operands). acc="exact": products summed in float64, plus the bias, then one float16 rounding, then + ReLU if relu_after. The original sums in float16 with the matrix unit's order, which is OPEN, so acc="f16" + raises OpenItem. pad: the border rule for k > 1 (PAD_MODES; OPEN, so required). relu_after: required, because + which net2 layers end in ReLU is OPEN (every net1 layer does). + """ + if acc == "f16": + raise OpenItem("float16 accumulation order of the matrix unit") + if acc != "exact": + raise ValueError("acc must be 'exact' (or 'f16', OPEN)") + x = f16(x) + w = f16(w) + b = f16(b) + cout, kh, kw, cin = w.shape + if kh != kw or kh % 2 != 1: + raise ValueError("kernel must be square and odd") + if x.shape[0] != cin or b.shape != (cout,): + raise ValueError("shape mismatch: x %s, w %s, b %s" % (x.shape, w.shape, b.shape)) + H, W = x.shape[1:] + p = kh // 2 + xp = _pad(x, p, pad) if p else x + y = np.zeros((cout, H, W), dtype=np.float64) + for ky in range(kh): + for kx in range(kw): + patch = xp[:, ky:ky + H, kx:kx + W] # [cin, H, W] + y += np.einsum("oc,chw->ohw", w[:, ky, kx, :], patch) # float64 sums + y = f16(y + b[:, None, None]) + return relu(y) if relu_after else y + + +def pool2x(x, *, kind): + """2x pooling over 2 x 2 blocks (H and W even). kind: "max" or "avg" (OPEN, so required). Output float16.""" + x = f16(x) + c, H, W = x.shape + if H % 2 or W % 2: + raise ValueError("pool2x needs even H and W") + blocks = x.reshape(c, H // 2, 2, W // 2, 2) + if kind == "max": + return blocks.max(axis=(2, 4)) + if kind == "avg": + return f16(blocks.mean(axis=(2, 4))) + raise ValueError("kind must be one of %s (OPEN; no default)" % (POOL_KINDS,)) + + +def upscale2x_bilinear(x): + """Bilinear 2x upscale, align-corners form: output n' = 2n per axis, output i samples input position + i * (n - 1) / (n' - 1), so the corner samples are copied exactly. float64 interpolation, one float16 rounding.""" + x = f16(x) + + def axis_weights(n): + m = 2 * n + if n == 1: + return np.zeros(m, dtype=np.int64), np.zeros(m, dtype=np.int64), np.zeros(m) + pos = np.arange(m, dtype=np.float64) * (n - 1) / (m - 1) + i0 = np.minimum(np.floor(pos).astype(np.int64), n - 2) + return i0, i0 + 1, pos - i0 + + _, H, W = x.shape + y0, y1, fy = axis_weights(H) + x0, x1, fx = axis_weights(W) + rows = x[:, y0, :] * (1.0 - fy)[None, :, None] + x[:, y1, :] * fy[None, :, None] + out = rows[:, :, x0] * (1.0 - fx)[None, None, :] + rows[:, :, x1] * fx[None, None, :] + return f16(out) + + +def add(a, b): + """Element-wise sum of two float16 tensors, one float16 rounding (which combines the network uses is OPEN).""" + return f16(f16(a) + f16(b)) + + +def concat(*xs): + """Channel concatenation (an input merge); values unchanged.""" + return np.concatenate([f16(x) for x in xs], axis=0) + + +# ---- net1's per-pixel input features -------------------------------------------------------------------------------- + +def luma(rgb): + """rgb [3, H, W] -> [H, W] with the fixed weights, rounded to float16.""" + rgb = np.asarray(rgb, dtype=np.float64) + return f16(LUMA_WEIGHTS[0] * rgb[0] + LUMA_WEIGHTS[1] * rgb[1] + LUMA_WEIGHTS[2] * rgb[2]) + + +def warp_error(frame, warped_other): + """Mean absolute RGB difference between a frame and the other frame already warped onto it, clamped to [0, 1], + rounded to float16. How the warp samples (motion field, filter, borders) is OPEN, so the warped frame is an input.""" + d = np.abs(np.asarray(frame, dtype=np.float64) - np.asarray(warped_other, dtype=np.float64)).mean(axis=0) + return f16(np.clip(d, 0.0, 1.0)) + + +def mask_pair(mask): + """A mask value and its complement (x, 1 - x), float16.""" + m = f16(mask) + return m, f16(1.0 - m) + + +def net1_features(features, *, order): + """Stacks net1's 10 per-pixel features into [10, H, W]. features: {name: [H, W]} with the names + "luma0", "luma1", "err0", "err1", "in0".."in3", "mask", "mask_c". order: the channel order, a permutation of those + ten names. Which order the original uses is OPEN, so it is required.""" + names = {"luma0", "luma1", "err0", "err1", "in0", "in1", "in2", "in3", "mask", "mask_c"} + if sorted(order) != sorted(names) or len(order) != NET1_FEATURES: + raise ValueError("order must be a permutation of %s" % sorted(names)) + return np.stack([f16(features[n]) for n in order]) + + +def fold2x2(x, *, order, pad_to=NET1_CELL): + """Folds each 2 x 2 block of pixels of x [c, H, W] into one cell of 4c values, zero-padded to pad_to: + -> [pad_to, H/2, W/2]. order: the place of each value in the cell, a permutation of (dy, dx, channel) given as + a string of the three axis names from slowest to fastest, e.g. "dy dx c" or "c dy dx". The original's order is + OPEN (so is what the pad holds: zeros here), so order is required.""" + axes = order.split() + if sorted(axes) != ["c", "dx", "dy"]: + raise ValueError("order must name dy, dx and c once each") + x = f16(x) + c, H, W = x.shape + if H % 2 or W % 2: + raise ValueError("fold2x2 needs even H and W") + if 4 * c > pad_to: + raise ValueError("cell too small") + v = x.reshape(c, H // 2, 2, W // 2, 2) # c, Y, dy, X, dx + pos = {"c": 0, "dy": 2, "dx": 4} + v = v.transpose([pos[a] for a in axes] + [1, 3]).reshape(4 * c, H // 2, W // 2) + out = np.zeros((pad_to, H // 2, W // 2), dtype=np.float64) + out[:4 * c] = v + return out + + +def unfold2x2(cells, c, *, order): + """The inverse of fold2x2 for the first 4c values of each cell: [>= 4c, h, w] -> [c, 2h, 2w].""" + axes = order.split() + if sorted(axes) != ["c", "dx", "dy"]: + raise ValueError("order must name dy, dx and c once each") + cells = f16(cells) + _, h, w = cells.shape + size = {"c": c, "dy": 2, "dx": 2} + v = cells[:4 * c].reshape([size[a] for a in axes] + [h, w]) + # current axes: axes..., Y, X -> c, Y, dy, X, dx + cur = axes + ["Y", "X"] + v = v.transpose([cur.index(a) for a in ("c", "Y", "dy", "X", "dx")]) + return v.reshape(c, 2 * h, 2 * w) + + +# ---- the layers of clean/fg/layout.py in this module's canonical order ----------------------------------------------- + +def weight_shape(layer): + """Canonical weight shape [out, k, k, in] of a layout layer (layout.Net1Layer or layout.Net2Layer). net1's blob 2 + (1 x 1 or 2 x 2) raises OpenItem.""" + taps = getattr(layer, "taps", 9) + if taps is None: + raise OpenItem("net1 blob 2: 1 x 1 or 2 x 2") + k = 3 if taps == 9 else 1 + return (layer.out, k, k, layer.cin) + + +# ---- the networks as a whole ---------------------------------------------------------------------------------------- + +def net1_forward(*args, **kwargs): + """net1 end to end: OPEN (decoder levels and skip connections, blob 2's tap count, the upsampling, the grid size + and the launch order are not delivered).""" + raise OpenItem("net1 wiring: launch order, decoder levels, skip connections, blob 2 (1 x 1 or 2 x 2)") + + +def net2_forward(*args, **kwargs): + """net2 end to end: OPEN (which layer reads which buffer, where the pooling, upscales and combines sit, which + layers end in ReLU, the grid size).""" + raise OpenItem("net2 wiring: launch order, pooling / upscale / combine placement, ReLU per layer") diff --git a/clean/fg/tests/test_fg_reference.py b/clean/fg/tests/test_fg_reference.py new file mode 100644 index 00000000..f4af4840 --- /dev/null +++ b/clean/fg/tests/test_fg_reference.py @@ -0,0 +1,196 @@ +# SPDX-License-Identifier: MIT +"""Frame generation (#96): self-checks of the CPU reference's building blocks (clean/fg/reference/ops.py) on +synthetic data, against plain loop implementations written separately here. + + python -m unittest clean.fg.tests.test_fg_reference +""" +import unittest + +import numpy as np + +from clean.fg import layout as L +from clean.fg.reference import ops + + +def f16(x): + return np.asarray(x, dtype=np.float64).astype(np.float16).astype(np.float64) + + +def halves(rng, shape, scale=1.0): + return f16(rng.uniform(-scale, scale, size=shape)) + + +def conv_loops(x, w, b, pad): + """A plain loop convolution: float64 sums, one float16 rounding at the end.""" + 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): + s = 0.0 + for ky in range(k): + for kx in range(k): + sy, sx = yy + ky - p, xx + kx - p + if pad == "zero" and not (0 <= sy < H and 0 <= sx < W): + continue + if pad == "replicate": + sy, sx = min(max(sy, 0), H - 1), min(max(sx, 0), W - 1) + if pad == "mirror": + sy = -sy if sy < 0 else (2 * (H - 1) - sy if sy >= H else sy) + sx = -sx if sx < 0 else (2 * (W - 1) - sx if sx >= W else sx) + for c in range(cin): + s += w[o, ky, kx, c] * x[c, sy, sx] + out[o, yy, xx] = s + b[o] + return f16(out) + + +class Conv(unittest.TestCase): + def test_against_loops(self): + rng = np.random.default_rng(1) + for k in (1, 3): + for pad in ops.PAD_MODES: + x = halves(rng, (5, 6, 7)) + w = halves(rng, (4, k, k, 5), 0.5) + b = halves(rng, (4,), 0.25) + want = conv_loops(x, w, b, pad) + got = ops.conv2d(x, w, b, pad=pad, relu_after=False) + np.testing.assert_array_equal(got, want, err_msg="k=%d pad=%s" % (k, pad)) + np.testing.assert_array_equal(ops.conv2d(x, w, b, pad=pad, relu_after=True), np.maximum(want, 0.0)) + + def test_output_is_float16(self): + rng = np.random.default_rng(2) + y = ops.conv2d(halves(rng, (3, 4, 4)), halves(rng, (2, 3, 3, 3)), halves(rng, (2,)), pad="zero", relu_after=False) + np.testing.assert_array_equal(y, f16(y)) + + def test_open_items_have_no_default(self): + rng = np.random.default_rng(3) + x, w, b = halves(rng, (2, 4, 4)), halves(rng, (2, 3, 3, 2)), halves(rng, (2,)) + with self.assertRaises(TypeError): + ops.conv2d(x, w, b, relu_after=False) # pad is required + with self.assertRaises(TypeError): + ops.conv2d(x, w, b, pad="zero") # relu_after is required + with self.assertRaises(ValueError): + ops.conv2d(x, w, b, pad="wrap", relu_after=False) + with self.assertRaises(ops.OpenItem): + ops.conv2d(x, w, b, pad="zero", relu_after=False, acc="f16") + + def test_shape_checks(self): + rng = np.random.default_rng(4) + with self.assertRaises(ValueError): + ops.conv2d(halves(rng, (3, 4, 4)), halves(rng, (2, 3, 3, 2)), halves(rng, (2,)), pad="zero", relu_after=False) + with self.assertRaises(ValueError): + ops.conv2d(halves(rng, (2, 4, 4)), halves(rng, (2, 2, 2, 2)), halves(rng, (2,)), pad="zero", relu_after=False) + + +class PoolUpscale(unittest.TestCase): + def test_pool(self): + x = f16(np.arange(2 * 4 * 6).reshape(2, 4, 6) * 0.25 - 3.0) + mx = ops.pool2x(x, kind="max") + av = ops.pool2x(x, kind="avg") + for c in range(2): + for i in range(2): + for j in range(3): + blk = x[c, 2 * i:2 * i + 2, 2 * j:2 * j + 2] + self.assertEqual(mx[c, i, j], blk.max()) + self.assertEqual(av[c, i, j], f16(blk.mean())) + with self.assertRaises(TypeError): + ops.pool2x(x) + with self.assertRaises(ValueError): + ops.pool2x(x, kind="min") + + def test_upscale_align_corners(self): + rng = np.random.default_rng(5) + x = halves(rng, (3, 5, 4)) + y = ops.upscale2x_bilinear(x) + self.assertEqual(y.shape, (3, 10, 8)) + # corners copied exactly + for (i, j), (p, q) in (((0, 0), (0, 0)), ((0, -1), (0, -1)), ((-1, 0), (-1, 0)), ((-1, -1), (-1, -1))): + np.testing.assert_array_equal(y[:, i, j], x[:, p, q]) + # a linear ramp stays linear: value at output i is the ramp at i * (n - 1) / (2n - 1) + ramp = f16(np.tile(np.arange(4.0)[None, None, :], (1, 3, 1))) + yr = ops.upscale2x_bilinear(ramp) + np.testing.assert_array_equal(yr[0, 0], f16(np.arange(8) * 3.0 / 7.0)) + # one sample against a hand formula + fy, fx = 3 * 4 / 9, 5 * 3 / 7 + y0, x0 = int(fy), int(fx) + ay, ax = fy - y0, fx - x0 + want = ((1 - ay) * ((1 - ax) * x[1, y0, x0] + ax * x[1, y0, x0 + 1]) + + ay * ((1 - ax) * x[1, y0 + 1, x0] + ax * x[1, y0 + 1, x0 + 1])) + self.assertAlmostEqual(y[1, 3, 5], f16(want), delta=abs(want) * 2 ** -10 + 1e-7) + + def test_upscale_single_row(self): + x = f16(np.array([[[1.5, -2.0]]])) + y = ops.upscale2x_bilinear(x) + self.assertEqual(y.shape, (1, 2, 4)) + np.testing.assert_array_equal(y[0, 0], y[0, 1]) + + +class Features(unittest.TestCase): + def test_luma_and_error(self): + rng = np.random.default_rng(6) + a = rng.uniform(0, 1, (3, 4, 4)) + b = rng.uniform(0, 3, (3, 4, 4)) + np.testing.assert_array_equal(ops.luma(a), f16(0.299 * a[0] + 0.587 * a[1] + 0.114 * a[2])) + e = ops.warp_error(a, b) + self.assertTrue(np.all((e >= 0) & (e <= 1))) + np.testing.assert_array_equal(e, f16(np.minimum(np.abs(a - b).mean(axis=0), 1.0))) + m, mc = ops.mask_pair(np.array([0.0, 0.25, 1.0])) + np.testing.assert_array_equal(mc, [1.0, 0.75, 0.0]) + + def test_net1_features_and_fold(self): + rng = np.random.default_rng(7) + names = ["luma0", "luma1", "err0", "err1", "in0", "in1", "in2", "in3", "mask", "mask_c"] + feats = {n: halves(rng, (6, 8)) for n in names} + with self.assertRaises(TypeError): + ops.net1_features(feats) + with self.assertRaises(ValueError): + ops.net1_features(feats, order=names[:-1] + ["luma0"]) + x = ops.net1_features(feats, order=names) + self.assertEqual(x.shape, (ops.NET1_FEATURES, 6, 8)) + for order in ("dy dx c", "c dy dx", "dx c dy"): + cells = ops.fold2x2(x, order=order) + self.assertEqual(cells.shape, (ops.NET1_CELL, 3, 4)) + np.testing.assert_array_equal(cells[40:], 0.0) + np.testing.assert_array_equal(ops.unfold2x2(cells, ops.NET1_FEATURES, order=order), x) + cells = ops.fold2x2(x, order="dy dx c") + # value (dy, dx, c) of cell (Y, X) is pixel (2Y + dy, 2X + dx) channel c + self.assertEqual(cells[(1 * 2 + 0) * 10 + 7, 2, 3], x[7, 5, 6]) + with self.assertRaises(TypeError): + ops.fold2x2(x) + + +class LayoutShapes(unittest.TestCase): + def test_weight_shapes(self): + self.assertEqual(ops.weight_shape(L.NET1[0]), (32, 3, 3, 48)) + self.assertEqual(ops.weight_shape(L.NET1[5]), (512, 1, 1, 256)) + with self.assertRaises(ops.OpenItem): + ops.weight_shape(L.NET1[1]) + for l in L.NET2: + s = ops.weight_shape(l) + self.assertEqual(s, (l.out, 3, 3, l.cin)) + self.assertEqual(int(np.prod(s)), l.weight_elements) + for l in L.NET1: + if l.taps is not None: + self.assertEqual(int(np.prod(ops.weight_shape(l))), l.weight_elements) + + def test_every_layer_runs(self): + """Each layer of both tables runs as one conv on a tiny grid with random weights (shapes fit).""" + rng = np.random.default_rng(8) + for l in [l for l in L.NET1 if l.taps is not None] + list(L.NET2): + shape = ops.weight_shape(l) + y = ops.conv2d(halves(rng, (shape[3], 2, 2)), halves(rng, shape, 0.05), halves(rng, (l.out,)), + pad="zero", relu_after=True) + self.assertEqual(y.shape, (l.out, 2, 2)) + self.assertTrue(np.all(np.isfinite(y)) and np.all(y >= 0)) + + def test_networks_open(self): + with self.assertRaises(ops.OpenItem): + ops.net1_forward() + with self.assertRaises(ops.OpenItem): + ops.net2_forward() + + +if __name__ == "__main__": + unittest.main()