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: 3 additions & 1 deletion clean/fg/FORMATS.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ delivered by the spec side; the code never guesses it (it raises, or takes the c
- Two networks, float16 elements, ReLU activations, no attention, no FP8:
- **net1**: a six-level encoder-decoder on a 2 x 2-folded grid; 17 blobs, each `[weights][zero area][bias x 16]`.
- **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.
blob each. Each layer's grid level relative to the network grid (`Net2Layer.level`, and `upscaled` for the two
layers whose output is upscaled 2x) is in the table; sub-net A's last layer has no stated level (OPEN), and
`net2_macs_per_pixel` takes it as an argument.
- Shapes and byte sizes per layer: [`layout.py`](layout.py) (checked by `tests/test_fg_layout.py`).

## Converter map
Expand Down
34 changes: 27 additions & 7 deletions clean/fg/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ class Net2Layer:
cin: int # stored input channels
cin_used: int # input channels that carry data (layer 1 of A stores 16, uses 10)
out: int
# The grid the layer runs on, as the divisor d of the network grid W2 x H2 (1: W2, 2: W2/2, 4: W2/4, 8: W2/8);
# None where it is not given (sub-net A's last layer: OPEN).
level: object = None
upscaled: bool = False # its output is upscaled 2x (bilinear) to the next level up

@property
def weight_elements(self):
Expand Down Expand Up @@ -125,13 +129,14 @@ def _net1():


def _net2():
rows = [("A", 16, 10, 32), ("A", 32, 32, 32), ("A", 32, 32, 64)]
rows += [("A", 64, 64, 64)] * 8 # the eight-layer block
rows += [("A", 64, 64, 96), ("A", 32, 32, 8)]
rows += [("B", 18, 18, 16), ("B", 16, 16, 32)]
rows += [("B", 32, 32, 32)] * 8 # the eight-layer block
rows += [("B", 32, 32, 48), ("B", 16, 16, 8)]
return tuple(Net2Layer(i + 1, s, c, cu, o) for i, (s, c, cu, o) in enumerate(rows))
# (sub-net, in stored, in used, out, level divisor, output upscaled)
rows = [("A", 16, 10, 32, 2, False), ("A", 32, 32, 32, 4, False), ("A", 32, 32, 64, 8, False)]
rows += [("A", 64, 64, 64, 8, False)] * 8 # the eight-layer block
rows += [("A", 64, 64, 96, 8, True), ("A", 32, 32, 8, None, False)]
rows += [("B", 18, 18, 16, 2, False), ("B", 16, 16, 32, 4, False)]
rows += [("B", 32, 32, 32, 4, False)] * 8 # the eight-layer block
rows += [("B", 32, 32, 48, 4, True), ("B", 16, 16, 8, 1, False)]
return tuple(Net2Layer(i + 1, s, c, cu, o, lv, up) for i, (s, c, cu, o, lv, up) in enumerate(rows))


NET1 = _net1()
Expand Down Expand Up @@ -160,6 +165,21 @@ def net1_bias_index(channel):
return g * NET1_BIAS_GROUP * NET1_BIAS_COPIES + j


def net2_macs_per_pixel(subnet, *, used_inputs=True, open_level=None):
"""Multiply-adds of one sub-net per pixel of the network grid W2: each layer's 9 * in * out at its own grid
(divided by d * d). used_inputs: count only the input channels that carry data. open_level: the divisor to assume
for a layer whose level is OPEN (required when the sub-net has one). Computed, not measured."""
total = 0
for l in NET2:
if l.subnet != subnet:
continue
d = l.level if l.level is not None else open_level
if d is None:
raise ValueError("%s: level OPEN; pass open_level" % l.name)
total += 9 * (l.cin_used if used_inputs else l.cin) * l.out / (d * d)
return total


def totals():
net1 = sum(l.blob_bytes for l in NET1)
net2 = sum(l.weight_bytes + l.bias_bytes for l in NET2)
Expand Down
22 changes: 22 additions & 0 deletions clean/fg/tests/test_fg_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,28 @@ def test_net2_blob_sizes(self):
self.assertEqual([l.subnet for l in L.NET2].count("A"), 13)
self.assertEqual(L.NET2[0].cin_used, 10)

def test_net2_levels(self):
self.assertEqual([l.level for l in L.NET2 if l.subnet == "A"], [2, 4, 8] + [8] * 8 + [8, None])
self.assertEqual([l.level for l in L.NET2 if l.subnet == "B"], [2, 4] + [4] * 8 + [4, 1])
self.assertEqual([l.index for l in L.NET2 if l.upscaled], [12, 24])
# an upscaled output of 3 groups feeds a layer one level up whose input is one group (96 = 3 x 32, 48 = 3 x 16)
for i in (12, 24):
up, nxt = L.NET2[i - 1], L.NET2[i]
self.assertEqual(up.out, 3 * nxt.cin)
# each sub-net's eight-layer block keeps width and level
for s, w in (("A", 64), ("B", 32)):
block = [l for l in L.NET2 if l.subnet == s and l.cin == l.out == w]
self.assertEqual(len(block), 8)
self.assertEqual(len({l.level for l in block}), 1)

def test_net2_work(self):
# the spec side's per-pixel work: sub-net A about 7.6k, B 6.7k to 7.6k (computed, counting used inputs)
self.assertEqual(L.net2_macs_per_pixel("B"), 7560)
self.assertEqual(L.net2_macs_per_pixel("A", open_level=2), 7632)
with self.assertRaises(ValueError):
L.net2_macs_per_pixel("A")
self.assertGreater(L.net2_macs_per_pixel("A", used_inputs=False, open_level=2), 7632)

def test_blob_order(self):
names = [b.name for b in L.blobs()]
self.assertEqual(names[:17], ["net1_%02d" % i for i in range(1, 18)])
Expand Down
Loading