diff --git a/clean/fg/FORMATS.md b/clean/fg/FORMATS.md index 859722e8..a7235398 100644 --- a/clean/fg/FORMATS.md +++ b/clean/fg/FORMATS.md @@ -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 diff --git a/clean/fg/README.md b/clean/fg/README.md index 72965024..e352c648 100644 --- a/clean/fg/README.md +++ b/clean/fg/README.md @@ -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 | diff --git a/clean/fg/reference/blobs.py b/clean/fg/reference/blobs.py new file mode 100644 index 00000000..585ae26d --- /dev/null +++ b/clean/fg/reference/blobs.py @@ -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="