File size: 13,685 Bytes
5f55368 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 | """Compile exact Boolean policies to static graphs for Myriad 2."""
import argparse
import hashlib
from itertools import combinations
import json
from pathlib import Path
import numpy as np
import onnx
from onnx import TensorProto, helper as h, numpy_helper
from policy import Policy
from radix import CAPACITY, encode_ranks
class Graph:
def __init__(self):
self.nodes = []
self.constants = []
def const(self, data):
key = "k%d" % len(self.constants)
self.constants.append(numpy_helper.from_array(np.asarray(data, dtype=np.float32), key))
return key
def op(self, op, inputs, **attributes):
value = "v%d" % len(self.nodes)
self.nodes.append(h.make_node(op, inputs, [value], name=value, **attributes))
return value
def conv(self, inputs, weights, bias):
return self.op("Conv", [inputs, self.const(weights), self.const(bias)],
kernel_shape=list(weights.shape[2:]))
def dense_head(g, policy, features):
# Inverted edges allow both AND and OR to use one affine/ReLU gate each.
n = len(policy.literals)
gates = []
levels = [0] * n
def lower(node):
if node[0] == "literal":
return policy.literals.index(node[1]), False
if node[0] == "not":
i, invert = lower(node[1])
return i, not invert
disjunction = node[0] == "or"
args = tuple(sorted((i, inv ^ disjunction) for i, inv in map(lower, node[1:])))
if args not in gates:
gates.append(args)
levels.append(1 + max(levels[i] for i, _ in args))
return n + gates.index(args), disjunction
ends = [lower(node) for node in policy.outputs]
live = list(range(n))
for depth in range(1, max(levels) + 1):
needed = {i for i, _ in ends}
for j, pair in enumerate(gates, n):
if levels[j] > depth:
needed.update(i for i, _ in pair)
layer = [i for i in live if i in needed] + [i for i, d in enumerate(levels) if d == depth]
matrix = np.zeros((len(layer), len(live), 1, 1), dtype=np.float32)
bias = np.zeros(len(layer), dtype=np.float32)
for row, key in enumerate(layer):
if key in live:
matrix[row, live.index(key)] = 1
else:
bias[row] = -1
for i, inv in gates[key-n]:
matrix[row, live.index(i)] += -1 if inv else 1
bias[row] += int(inv)
features = g.op("Relu", [g.conv(features, matrix, bias)])
live = layer
matrix = np.zeros((len(ends), len(live), 1, 1), dtype=np.float32)
bias = []
for row, (i, inv) in enumerate(ends):
matrix[row, live.index(i)] = -1 if inv else 1
bias.append(int(inv))
return g.conv(features, matrix, bias)
def gates_head(g, policy, features):
slices = ["atom%d" % i for i in range(len(policy.literals))]
g.nodes.append(h.make_node("Split", [features], slices, axis=1, split=[1]*len(slices)))
one = g.const(np.ones((1, 1, 1, 1)))
seen = {}
def emit(node):
if node not in seen:
if node[0] == "literal":
v = slices[policy.literals.index(node[1])]
elif node[0] == "not":
v = g.op("Sub", [one, emit(node[1])])
else:
v = g.op("Mul" if node[0] == "and" else "Max", [emit(n) for n in node[1:]])
seen[node] = v
return seen[node]
return g.op("Concat", [emit(node) for node in policy.outputs], axis=1)
def scanner(g, policy, side):
# Exact equality on small integer word IDs using adjacent ReLU hinges.
vocab = len(policy.word_ids)
hinges = np.ones((vocab+2, 1, 1, 1), dtype=np.float32)
cuts = g.op("Relu", [g.conv("lexemes", hinges, -np.arange(vocab+2))])
tents = np.zeros((vocab, vocab+2, 1, 1), dtype=np.float32)
for i in range(vocab):
tents[i, i:i+3, 0, 0] = [1, -2, 1]
equal = g.conv(cuts, tents, np.zeros(vocab))
width = max(map(len, policy.patterns))
w = np.zeros((len(policy.patterns), vocab, 1, width), dtype=np.float32)
for row, phrase in enumerate(policy.patterns):
for j, token in enumerate(phrase):
w[row, token-1, 0, width - len(phrase) + j] = 1
scores = g.op("Relu", [g.conv(equal, w, [1-len(p) for p in policy.patterns])])
remaining = side
while remaining > 1:
size = 4 if remaining % 4 == 0 else 2
scores = g.op("MaxPool", [scores], kernel_shape=[size,size], strides=[size,size])
remaining //= size
return g.op("Max", [scores, "prior"])
def _two_symbol_rows(codes, facts, base):
if facts < 3 or base * base * 256 > CAPACITY:
return None
def masks(bits):
return [sum(((mask >> index) & 1) << fact for index, fact in enumerate(bits))
for mask in range(1 << len(bits))]
for lane_facts in combinations(range(facts), 3):
address_facts = tuple(i for i in range(facts) if i not in lane_facts)
lanes = masks(lane_facts)
words = []
for address in masks(address_facts):
row = [codes[address | lane] for lane in lanes]
low, high = min(row), max(row)
if any(code not in (low, high) for code in row):
break
selectors = sum((code != low) << lane for lane, code in enumerate(row))
words.append((low * base + high) * 256 + selectors)
else:
return words, lane_facts + address_facts
return None
def packed_table(policy, codec="binary"):
"""Encode reachable output vectors using signed, exact FP16 integers."""
facts = len(policy.literals)
if facts > 10:
raise ValueError("Packed lookup supports up to 10 literal predicates")
rows = [tuple(map(int, policy.reference([(mask >> j) & 1 for j in range(facts)])))
for mask in range(1 << facts)]
codebook = sorted(set(rows))
indices = {row: i for i, row in enumerate(codebook)}
if codec == "radix":
base = len(codebook)
codes = [indices[row] for row in rows]
lanes = 1
while lanes < len(rows) and base ** (lanes + 1) <= CAPACITY:
lanes += 1
weights = [sum(code * base ** lane
for lane, code in enumerate(codes[first:first + lanes]))
for first in range(0, len(rows), lanes)]
metadata = dict(codebook=codebook, radix_digits=lanes)
if facts >= 3 and (1 << (facts - 3)) < len(weights):
paired = _two_symbol_rows(codes, facts, base)
if paired is not None:
weights, order = paired
metadata.update(radix_digits=8, pair_fact_order=order)
return encode_ranks(weights).astype(np.float32), metadata
code_width = max(1, (len(codebook) - 1).bit_length())
lane_bits = min(facts, (12 // code_width).bit_length() - 1)
lanes = 1 << lane_bits
# FP16 represents every integer in [-2048, 2047] exactly. Centering
# the word at zero fits 12 bits where an unsigned word would fit 11.
offset = 1 << (code_width * lanes - 1)
words = [sum(indices[row] << (lane * code_width)
for lane, row in enumerate(rows[first:first + lanes])) - offset
for first in range(0, len(rows), lanes)]
table = np.asarray(words, dtype=np.float32).reshape(1, -1, 1, 1)
return table, dict(codebook=codebook, code_width=code_width, lane_bits=lane_bits, offset=offset)
def _write_bundle(g, shapes, outputs, directory, manifest):
variant = manifest["variant"]
def ports(mapping):
return [h.make_tensor_value_info(
name, TensorProto.INT32 if variant == "gather" and name == "features" else TensorProto.FLOAT, shape
) for name, shape in mapping.items()]
graph = h.make_graph(g.nodes, "boolean_" + variant, ports(shapes), ports(outputs), g.constants)
model = h.make_model(graph, opset_imports=[h.make_opsetid("", 9)], ir_version=6)
onnx.checker.check_model(model)
directory.mkdir(exist_ok=True, parents=True)
blob = model.SerializeToString()
(directory/"model.onnx").write_bytes(blob)
manifest.update(input_shapes=shapes, onnx_sha256=hashlib.sha256(blob).hexdigest(),
fp16_coefficient_bytes=2*sum(np.prod(v.dims, dtype=int).item() for v in g.constants),
learned_parameters=0)
(directory/"model.json").write_text(json.dumps(manifest, indent=2)+"\n", encoding="utf8")
def compile_banks(sources, directory, slots=128):
if slots < 1 or not isinstance(sources, dict) or not sources:
raise ValueError("Expected named policy banks and a positive Gather capacity")
g = Graph()
tables, banks, locations = [], {}, {}
offset = 0
for bank, source in sources.items():
if not isinstance(bank, str) or not bank or not isinstance(source, Path):
raise ValueError("Each bank needs a name and a policy path")
spec = json.loads(source.read_bytes())
table, metadata = packed_table(Policy(spec), "radix")
key = table.tobytes()
if key not in locations:
locations[key] = offset
tables.append(table)
offset += table.size
banks[bank] = dict(policy=spec, bank_offset=locations[key], **metadata)
shapes = {"features": [slots]}
decisions = g.op("Gather", [g.const(np.concatenate(tables)), "features"], axis=0)
_write_bundle(g, shapes, {decisions: [slots]}, directory,
dict(name="magiceightball", variant="gather", decisions=decisions,
default_bank=next(iter(banks)), banks=banks))
def compile_policy(source, directory, variant, side=64, slots=1, codec="binary"):
if side < 2 or side & (side-1):
raise ValueError("Tile side must be a power of two")
if slots < 1 or (slots != 1 and variant != "gather"):
raise ValueError("Multiple positive slots are supported only with Gather")
if codec not in ("binary", "radix") or (codec == "radix" and variant != "gather"):
raise ValueError("The radix codec is supported only with Gather")
spec = json.loads(source.read_text(encoding="utf8"))
policy = Policy(spec)
g = Graph()
nf = len(policy.literals)
shapes = {"features": [1, nf, 1, 1]}
outputs = {}
metadata = {}
if variant == "scan":
shapes = {"lexemes": [1, 1, side, side + max(map(len,policy.patterns)) - 1],
"prior": [1, nf, 1, 1]}
features = scanner(g, policy, side)
else:
features = "features"
if variant in ("packed", "gather"):
table, metadata = packed_table(policy, codec)
if variant == "gather":
shapes = {"features": [slots]}
decisions = g.op("Gather", [g.const(table.reshape(-1)), "features"], axis=0)
outputs[decisions] = [slots]
else:
shapes = {"features": [1, table.shape[1], 1, 1]}
decisions = g.conv("features", table, [0])
outputs[decisions] = [1, 1, 1, 1]
elif variant == "lookup":
if nf > 10:
raise ValueError("The lookup form is intended for at most 10 literal predicates")
count = 1 << nf
words = (len(policy.outputs)+9)//10
table = np.zeros((words,count,1,1),dtype=np.float32)
for index in range(count):
facts = [(index >> j) & 1 for j in range(nf)]
for k,bit in enumerate(policy.reference(facts)):
table[k//10,index,0,0] += int(bit) << (k % 10)
shapes = {"features":[1,count,1,1]}
decisions = g.conv("features",table,np.zeros(words))
outputs[decisions] = [1,words,1,1]
else:
head = gates_head if variant == "gates" else dense_head
decisions = head(g, policy, features)
if variant == "scan":
decisions = g.op("Concat", [features, decisions], axis=1)
outputs[decisions] = [1, len(policy.outputs) + (nf if variant == "scan" else 0), 1, 1]
# OpenVINO uses producer node names when naming outputs, so keep those names in the manifest.
_write_bundle(g, shapes, outputs, directory, dict(
policy=spec, variant=variant, decisions=decisions,
state_channels=nf if variant=="scan" else 0, tile_side=side, **metadata,
))
if __name__ == "__main__":
p = argparse.ArgumentParser(description=__doc__)
p.add_argument("policy", type=Path)
p.add_argument("output", type=Path)
p.add_argument("--variant", choices=["gates", "dense", "scan", "lookup", "packed", "gather", "banks"], required=True)
p.add_argument("--side", type=int, default=64)
p.add_argument("--slots", type=int, default=1, help="Independent decisions per Gather request")
p.add_argument("--codec", choices=["binary", "radix"], default="binary")
args = p.parse_args()
if args.variant == "banks":
sources = json.loads(args.policy.read_bytes())
if not isinstance(sources, dict) or any(not isinstance(path, str) for path in sources.values()):
p.error("A bank configuration maps names to policy filenames")
compile_banks({name: args.policy.parent / path for name, path in sources.items()},
args.output, args.slots)
else:
compile_policy(args.policy, args.output, args.variant, args.side, args.slots, args.codec)
|