Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions clean/fg/FORMATS.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ delivered by the spec side; the code never guesses it (it raises, or takes the c
- **net2**: two small encoder-decoders (sub-nets A and B) of 3 x 3 convolutions; 25 layers, a weight blob and a bias
blob each.
- Shapes and byte sizes per layer: [`layout.py`](layout.py) (checked by `tests/test_fg_layout.py`).
- Splitting a blob into its parts: [`reference/blobs.py`](reference/blobs.py). net1: the weights in stored order, the
zero area checked to hold only +0 / -0, the bias as copy 0 of each group of eight with the other fifteen copies
checked to be bit-identical (`check=False` skips both checks). net2: weight and bias blob sizes checked. The weights
stay flat until the in-blob axis order (OPEN item 2) is delivered.

## Converter map

Expand Down
2 changes: 2 additions & 0 deletions clean/fg/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ front end or the launcher yet. Status, open items and formats: [FORMATS.md](FORM
| Path | Content |
|---|---|
| `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 |
| `reference/blobs.py` | splits blob bytes into flat float16 weights (stored order) and distinct biases: net1's zero area skipped and checked, copy 0 of each bias group taken and the other copies checked; net2's weight / bias pairs; `build_net1` for synthetic blobs |
| `tests/test_fg_blobs.py` | round trips and every check, on synthetic blobs of every layer |
| `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 |
Expand Down
83 changes: 83 additions & 0 deletions clean/fg/reference/blobs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# SPDX-License-Identifier: MIT
"""Frame generation (issue #96): splitting the networks' weight blobs into their parts, as a CPU reference.

Written for this project from the blob layout in clean/fg/layout.py. Input: the bytes of one blob (float16, little
endian). Output: flat float16 arrays in the order they are stored, plus checks of what the layout says must hold.

- net1 blob: [weights: out * K][zero area: out * K][bias: out * 16, groups of 8 channels each stored 16 times].
split_net1 returns the weights, and the out distinct biases (copy 0 of each group); it checks that the zero area
holds only zeros (+0 or -0) and that the 16 copies of every group are bit-identical, unless told not to.
- net2: a weight blob of 9 * in * out values and a bias blob of out values; split_net2 checks the sizes.

The order of the weights inside a blob (tap, input and output channel, any tiling) is OPEN, so the weights stay flat in
stored order; reshaping them into clean/fg/reference/ops.py's canonical order waits for it.
"""
import numpy as np

from clean.fg import layout as L


class BlobError(ValueError):
"""A blob that does not have the layout's size or structure."""


def _halves(data, what):
if len(data) % 2:
raise BlobError("%s: odd byte count %d" % (what, len(data)))
return np.frombuffer(bytes(data), dtype="<f2")


def split_net1(layer, data, *, check=True):
"""(weights [out * K] float16, bias [out] float16) of one net1 blob. check: verify the zero area and the bias
copies (BlobError on a mismatch)."""
if len(data) != layer.blob_bytes:
raise BlobError("%s: %d bytes, the layout says %d" % (layer.name, len(data), layer.blob_bytes))
h = _halves(data, layer.name)
n = layer.weight_elements
weights = h[:n].copy()
zero = h[layer.zero_offset:layer.bias_offset]
stored = h[layer.bias_offset:].view("<u2").reshape(layer.out // L.NET1_BIAS_GROUP, L.NET1_BIAS_COPIES,
L.NET1_BIAS_GROUP)
if check:
if np.any(zero.view("<u2") & 0x7FFF):
raise BlobError("%s: the zero area holds a non-zero value" % layer.name)
if np.any(stored != stored[:, :1, :]):
raise BlobError("%s: the copies of a bias group differ" % layer.name)
bias = stored[:, 0, :].reshape(layer.out).view("<f2").copy()
return weights, bias


def split_net2(layer, weight_data, bias_data):
"""(weights [9 * in * out] float16, bias [out] float16) of one net2 layer's two blobs."""
if len(weight_data) != layer.weight_bytes:
raise BlobError("%s_w: %d bytes, the layout says %d" % (layer.name, len(weight_data), layer.weight_bytes))
if len(bias_data) != layer.bias_bytes:
raise BlobError("%s_b: %d bytes, the layout says %d" % (layer.name, len(bias_data), layer.bias_bytes))
return _halves(weight_data, layer.name + "_w").copy(), _halves(bias_data, layer.name + "_b").copy()


def split_all(blobs, *, check=True):
"""{layer name: (weights, bias)} for every layer of both networks. blobs: {blob name: bytes} with the names of
layout.blobs() (net1_NN, net2_NN_w, net2_NN_b). A missing or extra blob is a BlobError."""
want = {b.name for b in L.blobs()}
if set(blobs) != want:
missing, extra = sorted(want - set(blobs)), sorted(set(blobs) - want)
raise BlobError("blob set differs: missing %s, extra %s" % (missing[:4], extra[:4]))
out = {}
for l in L.NET1:
out[l.name] = split_net1(l, blobs[l.name], check=check)
for l in L.NET2:
out[l.name] = split_net2(l, blobs[l.name + "_w"], blobs[l.name + "_b"])
return out


def build_net1(layer, weights, bias):
"""The inverse of split_net1: a blob from weights [out * K] and bias [out] (zero area of +0, 16 copies of each
bias group). For synthetic tests."""
w = np.asarray(weights, dtype="<f2").reshape(-1)
b = np.asarray(bias, dtype="<f2").reshape(-1)
if w.size != layer.weight_elements or b.size != layer.out:
raise BlobError("%s: wrong sizes" % layer.name)
groups = b.reshape(-1, 1, L.NET1_BIAS_GROUP)
rep = np.repeat(groups, L.NET1_BIAS_COPIES, axis=1).reshape(-1)
return w.tobytes() + np.zeros(layer.weight_elements, dtype="<f2").tobytes() + rep.tobytes()
104 changes: 104 additions & 0 deletions clean/fg/tests/test_fg_blobs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# SPDX-License-Identifier: MIT
"""Frame generation (#96): splitting weight blobs (clean/fg/reference/blobs.py), on synthetic blobs only.

python -m unittest clean.fg.tests.test_fg_blobs
"""
import struct
import unittest

import numpy as np

from clean.fg import layout as L
from clean.fg.reference import blobs as B


def rand_halves(rng, n):
return rng.uniform(-2, 2, n).astype("<f2")


def synthetic_blobs(rng):
out = {}
for l in L.NET1:
out[l.name] = B.build_net1(l, rand_halves(rng, l.weight_elements), rand_halves(rng, l.out))
for l in L.NET2:
out[l.name + "_w"] = rand_halves(rng, l.weight_elements).tobytes()
out[l.name + "_b"] = rand_halves(rng, l.out).tobytes()
return out


class SplitNet1(unittest.TestCase):
def test_round_trip_and_layout(self):
rng = np.random.default_rng(1)
l = L.NET1[0]
w, b = rand_halves(rng, l.weight_elements), rand_halves(rng, l.out)
blob = B.build_net1(l, w, b)
self.assertEqual(len(blob), l.blob_bytes)
gw, gb = B.split_net1(l, blob)
np.testing.assert_array_equal(gw.view("<u2"), w.view("<u2"))
np.testing.assert_array_equal(gb.view("<u2"), b.view("<u2"))
# the bias of channel c is at element bias_offset + net1_bias_index(c), read here byte by byte
for c in (0, 7, 8, l.out - 1):
e = l.bias_offset + L.net1_bias_index(c)
self.assertEqual(blob[2 * e:2 * e + 2], struct.pack("<e", float(b[c])))

def test_checks(self):
rng = np.random.default_rng(2)
l = L.NET1[16]
blob = bytearray(B.build_net1(l, rand_halves(rng, l.weight_elements), rand_halves(rng, l.out)))
# -0 in the zero area is still zero
neg = bytearray(blob)
neg[2 * l.zero_offset + 1] = 0x80
B.split_net1(l, bytes(neg))
bad_zero = bytearray(blob)
bad_zero[2 * (l.bias_offset - 1)] = 0x01
with self.assertRaises(B.BlobError):
B.split_net1(l, bytes(bad_zero))
B.split_net1(l, bytes(bad_zero), check=False)
bad_copy = bytearray(blob)
e = l.bias_offset + L.net1_bias_index(9) + 5 * L.NET1_BIAS_GROUP # copy 5 of channel 9
bad_copy[2 * e] ^= 0x01
with self.assertRaises(B.BlobError):
B.split_net1(l, bytes(bad_copy))
_, gb = B.split_net1(l, bytes(bad_copy), check=False) # copy 0 is still taken
_, want = B.split_net1(l, bytes(blob))
np.testing.assert_array_equal(gb.view("<u2"), want.view("<u2"))
with self.assertRaises(B.BlobError):
B.split_net1(l, bytes(blob[:-2]))


class SplitAll(unittest.TestCase):
def test_every_layer(self):
rng = np.random.default_rng(3)
blobs = synthetic_blobs(rng)
self.assertEqual(sum(len(v) for v in blobs.values()), L.totals()["bytes"])
parts = B.split_all(blobs)
self.assertEqual(len(parts), len(L.NET1) + len(L.NET2))
for l in L.NET1:
w, b = parts[l.name]
self.assertEqual((w.size, b.size, w.dtype.str, b.dtype.str), (l.weight_elements, l.out, "<f2", "<f2"))
self.assertEqual(B.build_net1(l, w, b), blobs[l.name])
for l in L.NET2:
w, b = parts[l.name]
self.assertEqual(w.tobytes(), blobs[l.name + "_w"])
self.assertEqual(b.tobytes(), blobs[l.name + "_b"])
self.assertEqual(sum(w.size + b.size for w, b in parts.values()),
L.totals()["net1_params"] + L.totals()["net2_params"])

def test_blob_set_and_sizes(self):
rng = np.random.default_rng(4)
blobs = synthetic_blobs(rng)
less = dict(blobs)
del less["net2_25_b"]
with self.assertRaises(B.BlobError):
B.split_all(less)
with self.assertRaises(B.BlobError):
B.split_all(dict(blobs, extra=b"\0\0"))
l = L.NET2[0]
with self.assertRaises(B.BlobError):
B.split_net2(l, blobs[l.name + "_w"][:-2], blobs[l.name + "_b"])
with self.assertRaises(B.BlobError):
B.split_net2(l, blobs[l.name + "_w"], blobs[l.name + "_b"] + b"\0\0")


if __name__ == "__main__":
unittest.main()
Loading