|
| 1 | +import assert from 'assert'; |
| 2 | +import { IGPUTextureSettings, Kernel, Texture } from 'gpu.js'; |
| 3 | +import { ILayerTemplate, IPraxis, IPraxisSettings } from '../src/praxis/base-praxis'; |
| 4 | +import { BaseLayer, ILayerSettings, ILayer } from '../src/layer/base-layer'; |
| 5 | + |
| 6 | +export function onePlusPlus3D(width: number, height: number, depth: number): number[][][] { |
| 7 | + const grid = []; |
| 8 | + let i = 1; |
| 9 | + for (let z = 0; z < depth; z++) { |
| 10 | + const rows = []; |
| 11 | + for (let y = 0; y < height; y++) { |
| 12 | + const columns = []; |
| 13 | + for (let x = 0; x < width; x++) { |
| 14 | + columns.push(i++); |
| 15 | + } |
| 16 | + rows.push(columns); |
| 17 | + } |
| 18 | + grid.push(rows); |
| 19 | + } |
| 20 | + return grid; |
| 21 | +} |
| 22 | + |
| 23 | +export function onePlusPlus2D(width: number, height: number): number[][] { |
| 24 | + const rows = []; |
| 25 | + let i = 1; |
| 26 | + for (let y = 0; y < height; y++) { |
| 27 | + const columns = []; |
| 28 | + for (let x = 0; x < width; x++) { |
| 29 | + columns.push(i++); |
| 30 | + } |
| 31 | + rows.push(columns); |
| 32 | + } |
| 33 | + return rows; |
| 34 | +} |
| 35 | + |
| 36 | +export function zero3D(width: number, height: number, depth: number): number[][][] { |
| 37 | + const grid = []; |
| 38 | + for (let z = 0; z < depth; z++) { |
| 39 | + const rows = []; |
| 40 | + for (let y = 0; y < height; y++) { |
| 41 | + const columns = []; |
| 42 | + for (let x = 0; x < width; x++) { |
| 43 | + columns.push(0); |
| 44 | + } |
| 45 | + rows.push(columns); |
| 46 | + } |
| 47 | + grid.push(rows); |
| 48 | + } |
| 49 | + return grid; |
| 50 | +} |
| 51 | + |
| 52 | +export function zero2D(width: number, height: number): number[][] { |
| 53 | + const rows = []; |
| 54 | + for (let y = 0; y < height; y++) { |
| 55 | + const columns = []; |
| 56 | + for (let x = 0; x < width; x++) { |
| 57 | + columns.push(0); |
| 58 | + } |
| 59 | + rows.push(columns); |
| 60 | + } |
| 61 | + return rows; |
| 62 | +} |
| 63 | + |
| 64 | +// export function allWeights(model, fn) { |
| 65 | +// fn(model.input.weights); |
| 66 | +// model.hiddenLayers.forEach((layer) => { |
| 67 | +// for (const p in layer) { |
| 68 | +// if (!layer.hasOwnProperty(p)) continue; |
| 69 | +// assert(fn(layer[p].weights)); |
| 70 | +// } |
| 71 | +// }); |
| 72 | +// fn(model.output.weights); |
| 73 | +// |
| 74 | +// model.equations.forEach((equation) => { |
| 75 | +// equation.states.forEach((state) => { |
| 76 | +// if (state.left && state.left.weights) fn(state.left.weights); |
| 77 | +// if (state.right && state.right.weights) fn(state.right.weights); |
| 78 | +// if (state.product && state.product.weights) fn(state.product.weights); |
| 79 | +// }); |
| 80 | +// }); |
| 81 | +// } |
| 82 | + |
| 83 | +export function allDeltas(model: any, fn: any): void { |
| 84 | + fn(model.input.deltas); |
| 85 | + model.hiddenLayers.forEach((layer: any) => { |
| 86 | + for (const p in layer) { |
| 87 | + if (!layer.hasOwnProperty(p)) continue; |
| 88 | + assert(fn(layer[p].deltas)); |
| 89 | + } |
| 90 | + }); |
| 91 | + fn(model.output.deltas); |
| 92 | + |
| 93 | + model.equations.forEach((equation: any) => { |
| 94 | + equation.states.forEach((state: any) => { |
| 95 | + if (state.left && state.left.deltas) fn(state.left.deltas); |
| 96 | + if (state.right && state.right.deltas) fn(state.right.deltas); |
| 97 | + if (state.product && state.product.deltas) fn(state.product.deltas); |
| 98 | + }); |
| 99 | + }); |
| 100 | +} |
| 101 | + |
| 102 | +export function allMatrices(model: any, fn: any): void { |
| 103 | + fn(model.input.weights); |
| 104 | + model.hiddenLayers.forEach((layer: any) => { |
| 105 | + for (const p in layer) { |
| 106 | + if (!layer.hasOwnProperty(p)) continue; |
| 107 | + fn(layer[p].weights); |
| 108 | + } |
| 109 | + }); |
| 110 | + fn(model.output.weights); |
| 111 | + |
| 112 | + model.equations.forEach((equation: any) => { |
| 113 | + equation.states.forEach((state: any) => { |
| 114 | + if (state.left && state.left.weights) fn(state.left.weights); |
| 115 | + if (state.right && state.right.weights) fn(state.right.weights); |
| 116 | + if (state.product && state.product.weights) fn(state.product.weights); |
| 117 | + }); |
| 118 | + }); |
| 119 | + |
| 120 | + fn(model.input.deltas); |
| 121 | + model.hiddenLayers.forEach((layer: any) => { |
| 122 | + for (const p in layer) { |
| 123 | + if (!layer.hasOwnProperty(p)) continue; |
| 124 | + fn(layer[p].deltas); |
| 125 | + } |
| 126 | + }); |
| 127 | + fn(model.output.deltas); |
| 128 | + |
| 129 | + model.equations.forEach((equation: any) => { |
| 130 | + equation.states.forEach((state: any) => { |
| 131 | + if (state.left && state.left.deltas) fn(state.left.deltas); |
| 132 | + if (state.right && state.right.deltas) fn(state.right.deltas); |
| 133 | + if (state.product && state.product.deltas) fn(state.product.deltas); |
| 134 | + }); |
| 135 | + }); |
| 136 | +} |
| 137 | + |
| 138 | +export function shave(value: Float32Array): Float32Array { |
| 139 | + const resultRow = new Float32Array(value.length); |
| 140 | + for (let x = 0; x < value.length; x++) { |
| 141 | + resultRow[x] = parseFloat((value[x]).toFixed(8)); |
| 142 | + } |
| 143 | + return resultRow; |
| 144 | +} |
| 145 | + |
| 146 | +export function shave2D(value: Float32Array[]): Float32Array[] { |
| 147 | + const resultMatrix = new Array(value.length); |
| 148 | + for (let y = 0; y < value.length; y++) { |
| 149 | + resultMatrix[y] = shave(value[y]); |
| 150 | + } |
| 151 | + return resultMatrix; |
| 152 | +} |
| 153 | + |
| 154 | +export function shave3D(value: Float32Array[][]): Float32Array[][] { |
| 155 | + const resultCube = new Array(value.length); |
| 156 | + for (let z = 0; z < value.length; z++) { |
| 157 | + resultCube[z] = shave2D(value[z]); |
| 158 | + } |
| 159 | + return resultCube; |
| 160 | +} |
| 161 | + |
| 162 | +// it was found that coverage breaks when you compare leftFunction.toString() === rightString.toString() |
| 163 | +// this does a check on the first line of the function source, which is good enough for knowing the function signature |
| 164 | +export function expectFunction(source: string, fn: Function): void { |
| 165 | + expect(source.toString().split(/\n/g)[0]).toBe(fn.toString().split(/\n/g)[0]); |
| 166 | +} |
| 167 | + |
| 168 | +export class TestLayer extends BaseLayer { |
| 169 | + get width(): number { |
| 170 | + return this.settings.width as number; |
| 171 | + } |
| 172 | + get height(): number { |
| 173 | + return this.settings.height as number; |
| 174 | + } |
| 175 | + get depth(): number { |
| 176 | + return this.settings.depth as number; |
| 177 | + } |
| 178 | + constructor(settings: ILayerSettings) { |
| 179 | + super(settings); |
| 180 | + } |
| 181 | +} |
| 182 | + |
| 183 | +export function mockLayer(settings: ILayerSettings = {}): ILayer { |
| 184 | + return new TestLayer({ id: 'MockLayer', ...settings }); |
| 185 | +} |
| 186 | + |
| 187 | +export function mockTexture(settings?: Partial<IGPUTextureSettings>): Texture { |
| 188 | + return new Texture({ |
| 189 | + ...settings, |
| 190 | + texture: {} as any, |
| 191 | + size: [1, 1], |
| 192 | + dimensions: [1, 1], |
| 193 | + output: [1, 1], |
| 194 | + context: {} as any, |
| 195 | + kernel: {} as Kernel, |
| 196 | + }); |
| 197 | +} |
| 198 | + |
| 199 | +export function mockPraxis(layerTemplate: ILayerTemplate, praxisSettings: Partial<IPraxisSettings> = {}): IPraxis { |
| 200 | + return { |
| 201 | + layerTemplate, |
| 202 | + settings: praxisSettings, |
| 203 | + kernel: null, |
| 204 | + get width() { |
| 205 | + return layerTemplate.width; |
| 206 | + }, |
| 207 | + get height() { |
| 208 | + return layerTemplate.height; |
| 209 | + }, |
| 210 | + get depth() { |
| 211 | + return layerTemplate.depth; |
| 212 | + }, |
| 213 | + run: () => {}, |
| 214 | + setupKernels: () => {}, |
| 215 | + toJSON: () => { |
| 216 | + return praxisSettings; |
| 217 | + }, |
| 218 | + }; |
| 219 | +} |
0 commit comments