File size: 3,527 Bytes
d9782fd
 
 
6e246aa
 
 
 
 
 
 
 
 
 
 
 
d9782fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6e246aa
d9782fd
 
 
 
 
6e246aa
 
 
d9782fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const maxModelSize = 1024 * 1024;
const maxPixelHiddenSize = 128;

// Input order shared with PIXEL_FEATURES in me.py and createModelInputs() in me.js.
export const PIXEL_FEATURES = [
	"x",
	"y",
	"velocityX",
	"velocityY",
	"pointerDx",
	"pointerDy",
	"hasPointer",
	"excitement",
];

function readFloatArray(view, offset, length) {
	const values = new Float32Array(length);
	for (let index = 0; index < length; index++) {
		values[index] = view.getFloat32(offset + index * 4, true);
		if (!Number.isFinite(values[index])) {
			throw new Error("DODL model contains a non-finite value.");
		}
	}
	return values;
}

export function parsePixelModel(buffer) {
	if (buffer.byteLength > maxModelSize) {
		throw new Error("DODL model exceeds the 1 MiB size limit.");
	}
	const bytes = new Uint8Array(buffer);
	if (bytes.byteLength < 16 || String.fromCharCode(...bytes.subarray(0, 4)) !== "DODL") {
		throw new Error("Invalid DODL model header.");
	}

	const view = new DataView(buffer);
	const formatVersion = view.getUint16(4, true);
	const metadataSize = view.getUint32(8, true);
	const networkSize = view.getUint32(12, true);
	if (formatVersion !== 2 || 16 + metadataSize + networkSize !== buffer.byteLength) {
		throw new Error("Unsupported or corrupt DODL model.");
	}

	const metadata = JSON.parse(
		new TextDecoder().decode(new Uint8Array(buffer, 16, metadataSize)),
	);
	const { inputSize, hiddenSize, outputSize } = metadata.networkShape;
	if (
		inputSize !== PIXEL_FEATURES.length ||
		!Number.isSafeInteger(hiddenSize) ||
		hiddenSize < 1 ||
		hiddenSize > maxPixelHiddenSize ||
		outputSize !== 2
	) {
		throw new Error(
			`Expected a pixel model with ${PIXEL_FEATURES.length} inputs, 2 outputs, and at most 128 hidden units.`,
		);
	}
	const lengths = [inputSize * hiddenSize, hiddenSize, hiddenSize * outputSize, outputSize];
	if (networkSize !== lengths.reduce((total, length) => total + length * 4, 0)) {
		throw new Error("DODL network dimensions do not match its data.");
	}

	let offset = 16 + metadataSize;
	const arrays = lengths.map((length) => {
		const values = readFloatArray(view, offset, length);
		offset += length * 4;
		return values;
	});
	return {
		inputSize,
		hiddenSize,
		outputSize,
		weights1: arrays[0],
		bias1: arrays[1],
		weights2: arrays[2],
		bias2: arrays[3],
		hiddenScratch: new Float32Array(hiddenSize),
		outputScratch: new Float32Array(outputSize),
	};
}

export async function loadPixelModel(url = "./noe-pixel-model.dodl") {
	const response = await fetch(url, { cache: "no-cache" });
	if (!response.ok) {
		throw new Error(`Model request failed: ${response.status}`);
	}
	return parsePixelModel(await response.arrayBuffer());
}

export function runPixelModel(model, inputs) {
	const hidden = model.hiddenScratch;
	const outputs = model.outputScratch;
	for (let hiddenIndex = 0; hiddenIndex < model.hiddenSize; hiddenIndex++) {
		let total = model.bias1[hiddenIndex];
		const start = hiddenIndex * model.inputSize;
		for (let inputIndex = 0; inputIndex < model.inputSize; inputIndex++) {
			total += inputs[inputIndex] * model.weights1[start + inputIndex];
		}
		hidden[hiddenIndex] = Math.max(0, total);
	}
	for (let outputIndex = 0; outputIndex < model.outputSize; outputIndex++) {
		let total = model.bias2[outputIndex];
		const start = outputIndex * model.hiddenSize;
		for (let hiddenIndex = 0; hiddenIndex < model.hiddenSize; hiddenIndex++) {
			total += hidden[hiddenIndex] * model.weights2[start + hiddenIndex];
		}
		outputs[outputIndex] = total;
	}
	return outputs;
}