Skip to content

Commit 2aa40ac

Browse files
Frame generation: CPU reference for splitting the weight blobs
clean/fg/reference/blobs.py: blob bytes to flat float16 weights (stored order) and distinct biases. net1: the zero area skipped and checked to be zero, copy 0 of each bias group taken and the other copies checked; net2: the weight and bias blob sizes checked. The in-blob axis order is OPEN, so weights stay flat. Refs #96 Signed-off-by: phantoslayer21 <156617979+phantoslayer21@users.noreply.github.com>
1 parent 92d5d13 commit 2aa40ac

4 files changed

Lines changed: 193 additions & 0 deletions

File tree

‎clean/fg/FORMATS.md‎

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

1620
## Converter map
1721

‎clean/fg/README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ front end or the launcher yet. Status, open items and formats: [FORMATS.md](FORM
66
| Path | Content |
77
|---|---|
88
| `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 |
9+
| `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 |
10+
| `tests/test_fg_blobs.py` | round trips and every check, on synthetic blobs of every layer |
911
| `tests/test_fg_layout.py` | the table against the totals the spec side gives |
1012
| `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` |
1113
| `tests/test_fg_reference.py` | the building blocks against plain loop versions, on synthetic data |

‎clean/fg/reference/blobs.py‎

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# SPDX-License-Identifier: MIT
2+
"""Frame generation (issue #96): splitting the networks' weight blobs into their parts, as a CPU reference.
3+
4+
Written for this project from the blob layout in clean/fg/layout.py. Input: the bytes of one blob (float16, little
5+
endian). Output: flat float16 arrays in the order they are stored, plus checks of what the layout says must hold.
6+
7+
- net1 blob: [weights: out * K][zero area: out * K][bias: out * 16, groups of 8 channels each stored 16 times].
8+
split_net1 returns the weights, and the out distinct biases (copy 0 of each group); it checks that the zero area
9+
holds only zeros (+0 or -0) and that the 16 copies of every group are bit-identical, unless told not to.
10+
- net2: a weight blob of 9 * in * out values and a bias blob of out values; split_net2 checks the sizes.
11+
12+
The order of the weights inside a blob (tap, input and output channel, any tiling) is OPEN, so the weights stay flat in
13+
stored order; reshaping them into clean/fg/reference/ops.py's canonical order waits for it.
14+
"""
15+
import numpy as np
16+
17+
from clean.fg import layout as L
18+
19+
20+
class BlobError(ValueError):
21+
"""A blob that does not have the layout's size or structure."""
22+
23+
24+
def _halves(data, what):
25+
if len(data) % 2:
26+
raise BlobError("%s: odd byte count %d" % (what, len(data)))
27+
return np.frombuffer(bytes(data), dtype="<f2")
28+
29+
30+
def split_net1(layer, data, *, check=True):
31+
"""(weights [out * K] float16, bias [out] float16) of one net1 blob. check: verify the zero area and the bias
32+
copies (BlobError on a mismatch)."""
33+
if len(data) != layer.blob_bytes:
34+
raise BlobError("%s: %d bytes, the layout says %d" % (layer.name, len(data), layer.blob_bytes))
35+
h = _halves(data, layer.name)
36+
n = layer.weight_elements
37+
weights = h[:n].copy()
38+
zero = h[layer.zero_offset:layer.bias_offset]
39+
stored = h[layer.bias_offset:].view("<u2").reshape(layer.out // L.NET1_BIAS_GROUP, L.NET1_BIAS_COPIES,
40+
L.NET1_BIAS_GROUP)
41+
if check:
42+
if np.any(zero.view("<u2") & 0x7FFF):
43+
raise BlobError("%s: the zero area holds a non-zero value" % layer.name)
44+
if np.any(stored != stored[:, :1, :]):
45+
raise BlobError("%s: the copies of a bias group differ" % layer.name)
46+
bias = stored[:, 0, :].reshape(layer.out).view("<f2").copy()
47+
return weights, bias
48+
49+
50+
def split_net2(layer, weight_data, bias_data):
51+
"""(weights [9 * in * out] float16, bias [out] float16) of one net2 layer's two blobs."""
52+
if len(weight_data) != layer.weight_bytes:
53+
raise BlobError("%s_w: %d bytes, the layout says %d" % (layer.name, len(weight_data), layer.weight_bytes))
54+
if len(bias_data) != layer.bias_bytes:
55+
raise BlobError("%s_b: %d bytes, the layout says %d" % (layer.name, len(bias_data), layer.bias_bytes))
56+
return _halves(weight_data, layer.name + "_w").copy(), _halves(bias_data, layer.name + "_b").copy()
57+
58+
59+
def split_all(blobs, *, check=True):
60+
"""{layer name: (weights, bias)} for every layer of both networks. blobs: {blob name: bytes} with the names of
61+
layout.blobs() (net1_NN, net2_NN_w, net2_NN_b). A missing or extra blob is a BlobError."""
62+
want = {b.name for b in L.blobs()}
63+
if set(blobs) != want:
64+
missing, extra = sorted(want - set(blobs)), sorted(set(blobs) - want)
65+
raise BlobError("blob set differs: missing %s, extra %s" % (missing[:4], extra[:4]))
66+
out = {}
67+
for l in L.NET1:
68+
out[l.name] = split_net1(l, blobs[l.name], check=check)
69+
for l in L.NET2:
70+
out[l.name] = split_net2(l, blobs[l.name + "_w"], blobs[l.name + "_b"])
71+
return out
72+
73+
74+
def build_net1(layer, weights, bias):
75+
"""The inverse of split_net1: a blob from weights [out * K] and bias [out] (zero area of +0, 16 copies of each
76+
bias group). For synthetic tests."""
77+
w = np.asarray(weights, dtype="<f2").reshape(-1)
78+
b = np.asarray(bias, dtype="<f2").reshape(-1)
79+
if w.size != layer.weight_elements or b.size != layer.out:
80+
raise BlobError("%s: wrong sizes" % layer.name)
81+
groups = b.reshape(-1, 1, L.NET1_BIAS_GROUP)
82+
rep = np.repeat(groups, L.NET1_BIAS_COPIES, axis=1).reshape(-1)
83+
return w.tobytes() + np.zeros(layer.weight_elements, dtype="<f2").tobytes() + rep.tobytes()

‎clean/fg/tests/test_fg_blobs.py‎

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# SPDX-License-Identifier: MIT
2+
"""Frame generation (#96): splitting weight blobs (clean/fg/reference/blobs.py), on synthetic blobs only.
3+
4+
python -m unittest clean.fg.tests.test_fg_blobs
5+
"""
6+
import struct
7+
import unittest
8+
9+
import numpy as np
10+
11+
from clean.fg import layout as L
12+
from clean.fg.reference import blobs as B
13+
14+
15+
def rand_halves(rng, n):
16+
return rng.uniform(-2, 2, n).astype("<f2")
17+
18+
19+
def synthetic_blobs(rng):
20+
out = {}
21+
for l in L.NET1:
22+
out[l.name] = B.build_net1(l, rand_halves(rng, l.weight_elements), rand_halves(rng, l.out))
23+
for l in L.NET2:
24+
out[l.name + "_w"] = rand_halves(rng, l.weight_elements).tobytes()
25+
out[l.name + "_b"] = rand_halves(rng, l.out).tobytes()
26+
return out
27+
28+
29+
class SplitNet1(unittest.TestCase):
30+
def test_round_trip_and_layout(self):
31+
rng = np.random.default_rng(1)
32+
l = L.NET1[0]
33+
w, b = rand_halves(rng, l.weight_elements), rand_halves(rng, l.out)
34+
blob = B.build_net1(l, w, b)
35+
self.assertEqual(len(blob), l.blob_bytes)
36+
gw, gb = B.split_net1(l, blob)
37+
np.testing.assert_array_equal(gw.view("<u2"), w.view("<u2"))
38+
np.testing.assert_array_equal(gb.view("<u2"), b.view("<u2"))
39+
# the bias of channel c is at element bias_offset + net1_bias_index(c), read here byte by byte
40+
for c in (0, 7, 8, l.out - 1):
41+
e = l.bias_offset + L.net1_bias_index(c)
42+
self.assertEqual(blob[2 * e:2 * e + 2], struct.pack("<e", float(b[c])))
43+
44+
def test_checks(self):
45+
rng = np.random.default_rng(2)
46+
l = L.NET1[16]
47+
blob = bytearray(B.build_net1(l, rand_halves(rng, l.weight_elements), rand_halves(rng, l.out)))
48+
# -0 in the zero area is still zero
49+
neg = bytearray(blob)
50+
neg[2 * l.zero_offset + 1] = 0x80
51+
B.split_net1(l, bytes(neg))
52+
bad_zero = bytearray(blob)
53+
bad_zero[2 * (l.bias_offset - 1)] = 0x01
54+
with self.assertRaises(B.BlobError):
55+
B.split_net1(l, bytes(bad_zero))
56+
B.split_net1(l, bytes(bad_zero), check=False)
57+
bad_copy = bytearray(blob)
58+
e = l.bias_offset + L.net1_bias_index(9) + 5 * L.NET1_BIAS_GROUP # copy 5 of channel 9
59+
bad_copy[2 * e] ^= 0x01
60+
with self.assertRaises(B.BlobError):
61+
B.split_net1(l, bytes(bad_copy))
62+
_, gb = B.split_net1(l, bytes(bad_copy), check=False) # copy 0 is still taken
63+
_, want = B.split_net1(l, bytes(blob))
64+
np.testing.assert_array_equal(gb.view("<u2"), want.view("<u2"))
65+
with self.assertRaises(B.BlobError):
66+
B.split_net1(l, bytes(blob[:-2]))
67+
68+
69+
class SplitAll(unittest.TestCase):
70+
def test_every_layer(self):
71+
rng = np.random.default_rng(3)
72+
blobs = synthetic_blobs(rng)
73+
self.assertEqual(sum(len(v) for v in blobs.values()), L.totals()["bytes"])
74+
parts = B.split_all(blobs)
75+
self.assertEqual(len(parts), len(L.NET1) + len(L.NET2))
76+
for l in L.NET1:
77+
w, b = parts[l.name]
78+
self.assertEqual((w.size, b.size, w.dtype.str, b.dtype.str), (l.weight_elements, l.out, "<f2", "<f2"))
79+
self.assertEqual(B.build_net1(l, w, b), blobs[l.name])
80+
for l in L.NET2:
81+
w, b = parts[l.name]
82+
self.assertEqual(w.tobytes(), blobs[l.name + "_w"])
83+
self.assertEqual(b.tobytes(), blobs[l.name + "_b"])
84+
self.assertEqual(sum(w.size + b.size for w, b in parts.values()),
85+
L.totals()["net1_params"] + L.totals()["net2_params"])
86+
87+
def test_blob_set_and_sizes(self):
88+
rng = np.random.default_rng(4)
89+
blobs = synthetic_blobs(rng)
90+
less = dict(blobs)
91+
del less["net2_25_b"]
92+
with self.assertRaises(B.BlobError):
93+
B.split_all(less)
94+
with self.assertRaises(B.BlobError):
95+
B.split_all(dict(blobs, extra=b"\0\0"))
96+
l = L.NET2[0]
97+
with self.assertRaises(B.BlobError):
98+
B.split_net2(l, blobs[l.name + "_w"][:-2], blobs[l.name + "_b"])
99+
with self.assertRaises(B.BlobError):
100+
B.split_net2(l, blobs[l.name + "_w"], blobs[l.name + "_b"] + b"\0\0")
101+
102+
103+
if __name__ == "__main__":
104+
unittest.main()

0 commit comments

Comments
 (0)