feat(solidjs): set up Vite + SolidJS project with WASM inference

- Vite config with COOP/COEP headers for SharedArrayBuffer
- SolidJS app renders at root URL without JS errors
- WASM ML engine loads and inference returns 126 bounded [0,1] outputs
- Debug probe (window.__nisps) exposed only with ?debug=1
- TypeScript strict mode with complete type coverage
- Playwright e2e tests (8 tests) for VAL-PROJ-001/002/003
- WasmIML TypeScript port from playground/js/nisps/nisps-wasm.js
- Dataset TypeScript port with FIFO ring buffer and sample weighting

Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com>
This commit is contained in:
monkey-w1n5t0n 2026-04-07 16:17:08 +02:00
parent 13112cf447
commit 7337e8eaed
18 changed files with 3212 additions and 2 deletions

View file

@ -4,6 +4,7 @@ commands:
typecheck: npx tsc --noEmit typecheck: npx tsc --noEmit
build: npx vite build build: npx vite build
test-old: npx playwright test --reporter=line test-old: npx playwright test --reporter=line
test-solidjs: npx playwright test --config playwright-solidjs.config.ts --reporter=line
lint: npx eslint src/ --ext .ts,.tsx 2>/dev/null || true lint: npx eslint src/ --ext .ts,.tsx 2>/dev/null || true
dev: npx vite --port 5174 dev: npx vite --port 5174

40
index.html Normal file
View file

@ -0,0 +1,40 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<title>MEMLNaut NISPS</title>
<style>
:root {
--glass-bg: rgba(10, 10, 20, 0.65);
--glass-border: rgba(255, 255, 255, 0.12);
--glass-blur: 12px;
--accent: #00d4ff;
--accent-dim: rgba(0, 212, 255, 0.3);
--danger: #ff3b5c;
--font-mono: 'JetBrains Mono', monospace;
}
* { margin: 0; padding: 0; box-sizing: border-box; }
html, body, #root {
width: 100%;
height: 100%;
overflow: hidden;
background: #0a0a14;
color: #e0e0e8;
font-family: var(--font-mono);
}
#root {
display: flex;
align-items: center;
justify-content: center;
}
</style>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/index.tsx"></script>
</body>
</html>

1760
package-lock.json generated Normal file

File diff suppressed because it is too large Load diff

View file

@ -1,13 +1,23 @@
{ {
"name": "memlnaut-nisps-tests", "name": "memlnaut-nisps-solidjs",
"version": "1.0.0", "version": "1.0.0",
"private": true, "private": true,
"type": "module",
"scripts": { "scripts": {
"dev": "vite --port 5174",
"build": "vite build",
"preview": "vite preview --port 5174",
"typecheck": "tsc --noEmit",
"test": "playwright test", "test": "playwright test",
"test:ui": "playwright test --ui", "test:ui": "playwright test --ui",
"test:headed": "playwright test --headed" "test:headed": "playwright test --headed"
}, },
"devDependencies": { "devDependencies": {
"@playwright/test": "^1.59.1" "@playwright/test": "^1.59.1",
"solid-js": "^1.9.12",
"typescript": "^6.0.2",
"vite": "^8.0.6",
"vite-plugin-cross-origin-isolation": "^0.1.6",
"vite-plugin-solid": "^2.11.12"
} }
} }

View file

@ -0,0 +1,23 @@
import { defineConfig } from '@playwright/test';
export default defineConfig({
testDir: './tests/solidjs',
timeout: 30_000,
expect: { timeout: 10_000 },
fullyParallel: true,
use: {
baseURL: 'http://localhost:5174',
headless: true,
ignoreHTTPSErrors: true,
},
webServer: {
command: 'npx vite --port 5174',
url: 'http://localhost:5174',
reuseExistingServer: true,
timeout: 15_000,
},
projects: [
{ name: 'chromium', use: { browserName: 'chromium' } },
],
reporter: [['list']],
});

BIN
public/nisps.wasm Executable file

Binary file not shown.

128
src/App.tsx Normal file
View file

@ -0,0 +1,128 @@
import { onMount, onCleanup, createSignal } from 'solid-js';
import { WasmIML } from './core/iml';
const N_INPUTS = 2;
const HIDDEN_LAYERS = [32, 48, 64];
const N_OUTPUTS = 126;
/**
* Root application component for the NISPS immersive app.
* Initializes WASM IML, runs initial inference, and exposes debug probe.
*/
export default function App() {
const [status, setStatus] = createSignal<string>('Loading WASM...');
const [ready, setReady] = createSignal(false);
// IML instance lives outside reactive state (opaque handle)
let iml: WasmIML | null = null;
onMount(async () => {
try {
iml = await WasmIML.create(N_INPUTS, N_OUTPUTS, HIDDEN_LAYERS);
// Run initial inference at center position
iml.setInput(0, 0.5);
iml.setInput(1, 0.5);
iml.process();
const outputs = iml.getOutputs();
const allBounded = outputs.every((v: number) => v >= 0 && v <= 1);
if (!allBounded) {
setStatus('ERROR: outputs out of bounds');
console.error('Unbounded outputs:', outputs);
return;
}
setReady(true);
setStatus(`Ready — ${outputs.length} outputs loaded`);
// Expose debug probe if ?debug=1
const params = new URLSearchParams(window.location.search);
if (params.get('debug') === '1') {
exposeDebugProbe(iml);
}
} catch (err) {
setStatus(`Error: ${err}`);
console.error('WASM init failed:', err);
}
});
onCleanup(() => {
iml?.destroy();
});
return (
<div style={{
display: 'flex',
'flex-direction': 'column',
'align-items': 'center',
'justify-content': 'center',
height: '100%',
'font-family': 'var(--font-mono)',
color: '#e0e0e8',
}}>
<h1 style={{
'font-size': '1.5rem',
'font-weight': 300,
'margin-bottom': '0.5rem',
'letter-spacing': '0.1em',
}}>
MEMLNaut NISPS
</h1>
<p style={{
'font-size': '0.8rem',
opacity: ready() ? 0.7 : 1,
color: ready() ? '#e0e0e8' : '#00d4ff',
}}>
{status()}
</p>
</div>
);
}
/**
* Expose window.__nisps debug probe for Playwright tests.
* All methods are synchronous (bypass SolidJS batching).
*/
function exposeDebugProbe(iml: WasmIML): void {
const probe = {
getOutputs: () => iml.getOutputs(),
getLoss: () => iml.lastLoss,
getWeights: () => iml._getFlatWeights(),
getExampleCount: () => iml.exampleCount,
setInputs: (x: number, y: number) => {
iml.setInput(0, Math.max(0, Math.min(1, x)));
iml.setInput(1, Math.max(0, Math.min(1, y)));
iml.process();
},
thumbsUp: () => {
iml.addExample(iml.inputState.slice(), iml.outputState.slice());
return iml.trainAsync();
},
thumbsDown: () => {
const spread = 0.6;
const speed = 0.15 * (1 - spread) + 0.05 * spread;
iml.moveWeights(speed, spread);
},
train: () => iml.train(),
trainAsync: () => iml.trainAsync(),
randomise: () => iml.randomiseWeights(0.6),
clearExamples: () => iml.clearDataset(),
saveState: () => {
const state = {
weights: iml._getFlatWeights(),
inputState: iml.inputState.slice(),
outputState: iml.outputState.slice(),
exampleCount: iml.exampleCount,
lossHistory: iml.lossHistory.slice(),
};
localStorage.setItem('nisps-a-immersive', JSON.stringify(state));
},
evalLoss: () => iml.evalLoss(),
inferBatch: (points: number[][]) => iml.inferBatch(points),
getLayerStats: () => iml.getLayerStats(),
};
(window as any).__nisps = probe;
console.log('[NISPS] Debug probe exposed at window.__nisps');
}

124
src/core/dataset.ts Normal file
View file

@ -0,0 +1,124 @@
/**
* NISPS Dataset — TypeScript port of playground/js/nisps/dataset.js
* Manages feature-label pairs for training with FIFO ring buffer
* and configurable sample weighting.
*/
export class Dataset {
features: number[][] = [];
labels: number[][] = [];
maxExamples: number;
constructor(maxExamples = 100) {
this.maxExamples = maxExamples;
}
add(feature: number[], label: number[]): boolean {
if (this.features.length > 0) {
if (
feature.length !== this.features[0].length ||
label.length !== this.labels[0].length
) {
return false;
}
}
if (this.features.length >= this.maxExamples) {
// FIFO: remove oldest
this.features.shift();
this.labels.shift();
}
this.features.push([...feature]);
this.labels.push([...label]);
return true;
}
clear(): void {
this.features = [];
this.labels = [];
}
getFeatures(withBias = true): number[][] {
return this.features.map((f) => (withBias ? [...f, 1.0] : [...f]));
}
getLabels(): number[][] {
return this.labels;
}
/**
* Compute per-sample training weights. Returns Float32Array normalized to sum to 1.
*/
computeWeights(
mode: 'global' | 'local' | 'combined' = 'global',
params: {
recencyBias?: number;
queryInput?: number[];
radius?: number;
} = {}
): Float32Array {
const n = this.features.length;
if (n === 0) return new Float32Array(0);
if (n === 1) return new Float32Array([1.0]);
const weights = new Float32Array(n).fill(1.0);
// Global recency: exponential decay
if (mode === 'global' || mode === 'combined') {
const bias = params.recencyBias ?? 0.6;
if (bias > 0) {
const decay = 1 - 0.3 * bias;
for (let i = n - 2; i >= 0; i--) {
weights[i] = weights[i + 1] * decay;
}
}
}
// Local recency: newer examples near the query suppress older nearby ones
if ((mode === 'local' || mode === 'combined') && params.queryInput) {
const query = params.queryInput;
const radius = params.radius ?? 0.15;
const radiusSq = radius * radius;
for (let i = 0; i < n; i++) {
const feat = this.features[i];
let distSq = 0;
for (let d = 0; d < feat.length; d++) {
const diff = feat[d] - (query[d] ?? 0);
distSq += diff * diff;
}
if (distSq < radiusSq) {
const proximity = 1 - Math.sqrt(distSq) / radius;
let newerNearby = 0;
for (let j = i + 1; j < n; j++) {
let djSq = 0;
for (let d = 0; d < feat.length; d++) {
const diff = feat[d] - this.features[j][d];
djSq += diff * diff;
}
if (djSq < radiusSq) newerNearby++;
}
if (newerNearby > 0) {
weights[i] *= Math.pow(1 - proximity, newerNearby);
}
}
}
}
// Normalize to sum to 1
let sum = 0;
for (let i = 0; i < n; i++) sum += weights[i];
if (sum > 0) {
for (let i = 0; i < n; i++) weights[i] /= sum;
}
return weights;
}
get size(): number {
return this.features.length;
}
isEmpty(): boolean {
return this.features.length === 0;
}
}

633
src/core/iml.ts Normal file
View file

@ -0,0 +1,633 @@
/**
* WasmIML — TypeScript port of playground/js/nisps/nisps-wasm.js
* WASM-backed IML using nisps-core compiled to WASM.
* Construction is async: use `await WasmIML.create(...)`.
*/
import NispsModuleFactory from './wasm/nisps.js';
import { Dataset } from './dataset';
import type { WrappedModule, LayerStats, WeightSnapshot } from './wasm-types';
/** Activation function IDs matching C++ nisps::ACTIVATION_FUNCTIONS enum */
const ACTIVATION = { SIGMOID: 0, TANH: 1, LINEAR: 2, RELU: 3 } as const;
/**
* Load the Emscripten module. Returns the initialized module.
*/
async function loadNispsModule(): Promise<any> {
const mod = await NispsModuleFactory({
locateFile: (path: string) => {
// WASM binary is in public/ (served at root), glue code is in src/
if (path.endsWith('.wasm')) return `/${path}`;
return path;
},
});
return mod;
}
/**
* Wrap raw Emscripten module with typed JS helpers.
*/
function wrapModule(mod: any): WrappedModule {
const cwrap = mod.cwrap.bind(mod);
return {
mod,
create: cwrap('nisps_mlp_create', 'number', ['number', 'number', 'number', 'number']),
destroy: cwrap('nisps_mlp_destroy', null, ['number']),
weightCount: cwrap('nisps_mlp_weight_count', 'number', ['number']),
getWeights: cwrap('nisps_mlp_get_weights', null, ['number', 'number']),
setWeights: cwrap('nisps_mlp_set_weights', null, ['number', 'number']),
inference: cwrap('nisps_mlp_inference', null, ['number', 'number', 'number', 'number', 'number']),
train: cwrap('nisps_mlp_train', 'number', [
'number', 'number', 'number', 'number',
'number', 'number', 'number',
'number', 'number', 'number',
]),
trainEx: cwrap('nisps_mlp_train_ex', 'number', [
'number', 'number', 'number', 'number',
'number', 'number', 'number',
'number', 'number', 'number', 'number',
]),
drawWeightsSpread: cwrap('nisps_mlp_draw_weights_spread', null, ['number', 'number']),
moveWeightsSpread: cwrap('nisps_mlp_move_weights_spread', null, ['number', 'number', 'number']),
moveWeightsEx: cwrap('nisps_mlp_move_weights_ex', null, ['number', 'number', 'number', 'number', 'number']),
inferBatch: cwrap('nisps_mlp_infer_batch', null, ['number', 'number', 'number', 'number', 'number', 'number']),
evalLoss: cwrap('nisps_mlp_eval_loss', 'number', [
'number', 'number', 'number', 'number',
'number', 'number', 'number',
]),
getLayerStats: cwrap('nisps_mlp_get_layer_stats', null, ['number', 'number', 'number']),
alloc: cwrap('nisps_alloc', 'number', ['number']),
free: cwrap('nisps_free', null, ['number']),
allocInt: cwrap('nisps_alloc_int', 'number', ['number']),
freeInt: cwrap('nisps_free_int', null, ['number']),
};
}
/** Write a Float32Array into WASM heap, returning the pointer */
function toHeapF32(w: WrappedModule, arr: Float32Array | number[]): number {
const typed = arr instanceof Float32Array ? arr : new Float32Array(arr);
const ptr = w.alloc(typed.length);
w.mod.HEAPF32.set(typed, ptr >> 2);
return ptr;
}
/** Write an Int32Array into WASM heap, returning the pointer */
function toHeapI32(w: WrappedModule, arr: Int32Array): number {
const ptr = w.allocInt(arr.length);
w.mod.HEAP32.set(arr, ptr >> 2);
return ptr;
}
/** Read a Float32Array from WASM heap */
function fromHeapF32(w: WrappedModule, ptr: number, length: number): number[] {
const offset = ptr >> 2;
return Array.from(w.mod.HEAPF32.subarray(offset, offset + length));
}
export class WasmIML {
// Architecture
nInputs: number;
nOutputs: number;
maxIterations: number;
learningRate: number;
convergenceThreshold: number;
recencyBias = 0.6;
weightingMode: 'global' | 'local' | 'combined' = 'global';
localRadius = 0.15;
layerSizes: number[];
activationIds: number[];
// WASM internals
private _w: WrappedModule;
private _mlp: number = 0;
private _weightCount: number = 0;
private _inputPtr: number;
private _outputPtr: number;
private _inputDim: number;
// State
inputState: number[];
outputState: number[];
performInference = true;
inputUpdated = true;
storedWeights: number[] | null = null;
weightsRandomised = false;
lastLoss: number | null = null;
bestLoss: number | null = null;
lossHistory: number[] = [];
totalTrainingIterations = 0;
logFn: ((msg: string) => void) | null = null;
// Dataset (JS-side for persistence/visualization and sample weighting)
dataset: Dataset;
// Worker for async training
private _worker: Worker | null = null;
private _training = false;
/**
* Async factory — loads WASM and constructs the IML.
*/
static async create(
nInputs: number,
nOutputs: number,
hiddenLayers: number[] = [32, 48, 64],
maxIterations = 1000,
learningRate = 1.0,
convergenceThreshold = 0.00001
): Promise<WasmIML> {
const mod = await loadNispsModule();
const iml = new WasmIML(
mod, nInputs, nOutputs, hiddenLayers,
maxIterations, learningRate, convergenceThreshold
);
return iml;
}
constructor(
mod: any,
nInputs: number,
nOutputs: number,
hiddenLayers: number[],
maxIterations: number,
learningRate: number,
convergenceThreshold: number
) {
this.nInputs = nInputs;
this.nOutputs = nOutputs;
this.maxIterations = maxIterations;
this.learningRate = learningRate;
this.convergenceThreshold = convergenceThreshold;
const BIAS = 1;
this.layerSizes = [nInputs + BIAS, ...hiddenLayers, nOutputs];
this.activationIds = [
...hiddenLayers.map(() => ACTIVATION.RELU),
ACTIVATION.SIGMOID,
];
this._w = wrapModule(mod);
this._createMLP();
this.inputState = new Array(nInputs).fill(0.5);
this.outputState = new Array(nOutputs).fill(0);
this.dataset = new Dataset(100);
// Persistent WASM buffers for inference
const inputDim = nInputs + BIAS;
this._inputPtr = this._w.alloc(inputDim);
this._outputPtr = this._w.alloc(nOutputs);
this._inputDim = inputDim;
}
private _createMLP(): void {
const w = this._w;
const layerPtr = toHeapI32(w, new Int32Array(this.layerSizes));
const actPtr = toHeapI32(w, new Int32Array(this.activationIds));
this._mlp = w.create(layerPtr, this.layerSizes.length, actPtr, this.activationIds.length);
w.freeInt(layerPtr);
w.freeInt(actPtr);
this._weightCount = w.weightCount(this._mlp);
}
// ---- Logging ----
setLogger(fn: (msg: string) => void): void { this.logFn = fn; }
log(msg: string): void { if (this.logFn) this.logFn(msg); }
// ---- Input / Output ----
setInput(index: number, value: number): void {
if (index >= this.nInputs) return;
this.inputState[index] = Math.max(0, Math.min(1, value));
this.inputUpdated = true;
}
setInputs(values: number[]): void {
for (let i = 0; i < values.length && i < this.nInputs; i++) {
this.inputState[i] = Math.max(0, Math.min(1, values[i]));
}
this.inputUpdated = true;
}
getOutputs(): number[] { return this.outputState; }
setOutput(index: number, value: number): void {
if (index >= this.nOutputs) return;
this.outputState[index] = Math.max(0, Math.min(1, value));
}
setOutputs(values: number[]): void {
for (let i = 0; i < values.length && i < this.nOutputs; i++) {
this.outputState[i] = Math.max(0, Math.min(1, values[i]));
}
}
// ---- Inference (WASM, synchronous) ----
process(): void {
if (!this.performInference || !this.inputUpdated) return;
const heap = this._w.mod.HEAPF32;
const inOff = this._inputPtr >> 2;
for (let i = 0; i < this.nInputs; i++) {
heap[inOff + i] = this.inputState[i];
}
heap[inOff + this.nInputs] = 1.0; // bias
this._w.inference(
this._mlp, this._inputPtr, this._inputDim, this._outputPtr, this.nOutputs
);
const outOff = this._outputPtr >> 2;
for (let i = 0; i < this.nOutputs; i++) {
this.outputState[i] = heap[outOff + i];
}
this.inputUpdated = false;
}
// ---- Batch inference (WASM) ----
inferBatch(inputPoints: number[][]): number[][] {
const nPoints = inputPoints.length;
const inputDim = this.nInputs + 1; // +bias
const inFlat = new Float32Array(nPoints * inputDim);
for (let i = 0; i < nPoints; i++) {
for (let j = 0; j < this.nInputs; j++) {
inFlat[i * inputDim + j] = inputPoints[i][j];
}
inFlat[i * inputDim + this.nInputs] = 1.0;
}
const inPtr = toHeapF32(this._w, inFlat);
const outPtr = this._w.alloc(nPoints * this.nOutputs);
this._w.inferBatch(this._mlp, inPtr, nPoints, inputDim, outPtr, this.nOutputs);
const results: number[][] = [];
const heap = this._w.mod.HEAPF32;
const outOff = outPtr >> 2;
for (let i = 0; i < nPoints; i++) {
const row = new Array(this.nOutputs);
for (let j = 0; j < this.nOutputs; j++) {
row[j] = heap[outOff + i * this.nOutputs + j];
}
results.push(row);
}
this._w.free(inPtr);
this._w.free(outPtr);
return results;
}
// ---- Dataset ----
addExample(inputs: number[], outputs: number[]): void {
const inVec = inputs.slice(0, this.nInputs);
while (inVec.length < this.nInputs) inVec.push(0);
const outVec = outputs.slice(0, this.nOutputs);
while (outVec.length < this.nOutputs) outVec.push(0);
this.dataset.add(inVec, outVec);
}
clearDataset(): void {
this.dataset.clear();
this.log('Dataset cleared.');
}
get exampleCount(): number { return this.dataset.features.length; }
// ---- Training (WASM, synchronous) ----
train(_options: Record<string, unknown> = {}): number | null {
if (this.weightsRandomised && this.storedWeights) {
this._setFlatWeights(this.storedWeights);
this.weightsRandomised = false;
}
if (this.dataset.features.length === 0) {
this.log('Empty dataset, skipping training.');
return null;
}
this.log('Training...');
const features = this.dataset.features;
const labels = this.dataset.labels;
const sampleWeights = this.dataset.computeWeights(this.weightingMode, {
recencyBias: this.recencyBias,
queryInput: this.inputState,
radius: this.localRadius,
});
const featureDim = this.nInputs + 1;
const nSamples = features.length;
const featFlat = new Float32Array(nSamples * featureDim);
const labFlat = new Float32Array(nSamples * this.nOutputs);
for (let i = 0; i < nSamples; i++) {
for (let j = 0; j < this.nInputs; j++) {
featFlat[i * featureDim + j] = features[i][j];
}
featFlat[i * featureDim + this.nInputs] = 1.0;
for (let j = 0; j < this.nOutputs; j++) {
labFlat[i * this.nOutputs + j] = labels[i][j] || 0;
}
}
const featPtr = toHeapF32(this._w, featFlat);
const labPtr = toHeapF32(this._w, labFlat);
const weightPtr = toHeapF32(this._w, sampleWeights);
const lossHistPtr = this._w.alloc(this.maxIterations);
const itersRun = this._w.trainEx(
this._mlp, featPtr, nSamples, featureDim,
labPtr, this.nOutputs,
weightPtr,
this.learningRate, this.maxIterations, this.convergenceThreshold,
lossHistPtr
);
const lossHist = fromHeapF32(this._w, lossHistPtr, itersRun);
this._w.free(featPtr);
this._w.free(labPtr);
this._w.free(weightPtr);
this._w.free(lossHistPtr);
const loss = itersRun > 0 ? lossHist[itersRun - 1] : 0;
this.lastLoss = loss;
for (let i = 0; i < lossHist.length; i++) {
this.lossHistory.push(lossHist[i]);
}
this.totalTrainingIterations += itersRun;
if (this.lossHistory.length > 1200) {
this.lossHistory = this.lossHistory.slice(this.lossHistory.length - 1200);
}
this.bestLoss = this.bestLoss === null ? loss : Math.min(this.bestLoss, loss);
this.inputUpdated = true;
this.process();
this.log(`Training complete. Loss: ${loss.toFixed(6)}`);
return loss;
}
// ---- Loss evaluation (WASM, no weight update) ----
evalLoss(): number | null {
if (this.dataset.features.length === 0) return null;
const features = this.dataset.features;
const labels = this.dataset.labels;
const sampleWeights = this.dataset.computeWeights(this.weightingMode, {
recencyBias: this.recencyBias,
queryInput: this.inputState,
radius: this.localRadius,
});
const featureDim = this.nInputs + 1;
const nSamples = features.length;
const featFlat = new Float32Array(nSamples * featureDim);
const labFlat = new Float32Array(nSamples * this.nOutputs);
for (let i = 0; i < nSamples; i++) {
for (let j = 0; j < this.nInputs; j++) {
featFlat[i * featureDim + j] = features[i][j];
}
featFlat[i * featureDim + this.nInputs] = 1.0;
for (let j = 0; j < this.nOutputs; j++) {
labFlat[i * this.nOutputs + j] = labels[i][j] || 0;
}
}
const featPtr = toHeapF32(this._w, featFlat);
const labPtr = toHeapF32(this._w, labFlat);
const weightPtr = toHeapF32(this._w, sampleWeights);
const loss = this._w.evalLoss(
this._mlp, featPtr, nSamples, featureDim,
labPtr, this.nOutputs, weightPtr
);
this._w.free(featPtr);
this._w.free(labPtr);
this._w.free(weightPtr);
return loss;
}
// ---- Weight manipulation ----
randomiseWeights(spread = 0): void {
this.storedWeights = this._getFlatWeights();
this._w.drawWeightsSpread(this._mlp, spread);
this.weightsRandomised = true;
this.inputUpdated = true;
this.process();
this.log('Weights randomised.');
}
moveWeights(speed: number, spread = 0, outputPinMask: number[] | null = null): void {
let pinMaskPtr = 0;
if (outputPinMask && outputPinMask.some(v => v)) {
const pinI32 = new Int32Array(this.nOutputs);
for (let i = 0; i < this.nOutputs; i++) {
pinI32[i] = outputPinMask[i] ? 1 : 0;
}
pinMaskPtr = toHeapI32(this._w, pinI32);
}
this._w.moveWeightsEx(this._mlp, speed, spread, pinMaskPtr, this.nOutputs);
if (pinMaskPtr) this._w.freeInt(pinMaskPtr);
this.inputUpdated = true;
this.process();
}
// ---- Weight snapshot for warm-start transfer ----
extractWeights(): WeightSnapshot {
return {
layerSizes: [...this.layerSizes],
weights: this._getFlatWeights(),
};
}
static async createWithWarmStart(
snapshot: WeightSnapshot,
newOutputCount: number,
maxIter: number,
learningRate: number,
convergenceThreshold: number
): Promise<WasmIML> {
const oldLayerSizes = snapshot.layerSizes;
const nInputs = oldLayerSizes[0] - 1;
const hiddenLayers = oldLayerSizes.slice(1, -1);
const oldOutputCount = oldLayerSizes[oldLayerSizes.length - 1];
const newIml = await WasmIML.create(
nInputs, newOutputCount, hiddenLayers, maxIter, learningRate, convergenceThreshold
);
let prefixCount = 0;
for (let l = 1; l < oldLayerSizes.length - 1; l++) {
prefixCount += oldLayerSizes[l] * (oldLayerSizes[l - 1] + 1);
}
const lastHidden = hiddenLayers[hiddenLayers.length - 1];
const weightsPerOutputNode = lastHidden + 1;
const newWeights = newIml._getFlatWeights();
const oldWeights = snapshot.weights;
for (let i = 0; i < prefixCount; i++) {
newWeights[i] = oldWeights[i];
}
const sharedOutputNodes = Math.min(oldOutputCount, newOutputCount);
for (let n = 0; n < sharedOutputNodes; n++) {
const oldOff = prefixCount + n * weightsPerOutputNode;
const newOff = prefixCount + n * weightsPerOutputNode;
for (let w = 0; w < weightsPerOutputNode; w++) {
newWeights[newOff + w] = oldWeights[oldOff + w];
}
}
newIml._setFlatWeights(newWeights);
return newIml;
}
// ---- Flat weight get/set ----
_getFlatWeights(): number[] {
const ptr = this._w.alloc(this._weightCount);
this._w.getWeights(this._mlp, ptr);
const weights = fromHeapF32(this._w, ptr, this._weightCount);
this._w.free(ptr);
return weights;
}
_setFlatWeights(flatWeights: number[]): void {
const ptr = toHeapF32(this._w, new Float32Array(flatWeights));
this._w.setWeights(this._mlp, ptr);
this._w.free(ptr);
}
// ---- Per-layer weight statistics ----
getLayerStats(): LayerStats[] {
const nLayers = this.layerSizes.length - 1;
const statsPtr = this._w.alloc(nLayers * 4);
this._w.getLayerStats(this._mlp, statsPtr, nLayers);
const stats: LayerStats[] = [];
const heap = this._w.mod.HEAPF32;
const off = statsPtr >> 2;
for (let l = 0; l < nLayers; l++) {
stats.push({
meanAbs: heap[off + l * 4 + 0],
maxAbs: heap[off + l * 4 + 1],
deadFrac: heap[off + l * 4 + 2],
satFrac: heap[off + l * 4 + 3],
});
}
this._w.free(statsPtr);
return stats;
}
// ---- Async training via Web Worker ----
get isTraining(): boolean { return this._training; }
trainAsync(onComplete?: (result: { loss: number; outputs: number[] }) => void): Promise<number | null> {
if (this._training) {
this.log('Training already in progress, skipping.');
return Promise.resolve(null);
}
if (this.weightsRandomised && this.storedWeights) {
this._setFlatWeights(this.storedWeights);
this.weightsRandomised = false;
}
if (this.dataset.features.length === 0) {
this.log('Empty dataset, skipping training.');
return Promise.resolve(null);
}
this._training = true;
this.log('Training (async)...');
const flatWeights = this._getFlatWeights();
const features = this.dataset.features;
const labels = this.dataset.labels;
const sampleWeights = Array.from(this.dataset.computeWeights(this.weightingMode, {
recencyBias: this.recencyBias,
queryInput: this.inputState,
radius: this.localRadius,
}));
// Lazy-init worker
if (!this._worker) {
const workerUrl = new URL('./wasm/nisps-wasm-worker.js', import.meta.url);
this._worker = new Worker(workerUrl, { type: 'module' });
}
return new Promise((resolve) => {
const handler = (e: MessageEvent) => {
if (e.data.type === 'trained') {
this._worker!.removeEventListener('message', handler);
this._training = false;
const { weights, loss, lossHistory } = e.data.payload;
this._setFlatWeights(weights);
this.lastLoss = loss;
if (lossHistory && lossHistory.length > 0) {
for (let i = 0; i < lossHistory.length; i++) {
this.lossHistory.push(lossHistory[i]);
}
this.totalTrainingIterations += lossHistory.length;
} else {
this.lossHistory.push(loss);
this.totalTrainingIterations += 1;
}
if (this.lossHistory.length > 1200) {
this.lossHistory = this.lossHistory.slice(this.lossHistory.length - 1200);
}
this.bestLoss = this.bestLoss === null ? loss : Math.min(this.bestLoss, loss);
this.inputUpdated = true;
this.process();
this.log(`Training complete. Loss: ${loss.toFixed(6)}`);
if (onComplete) onComplete({ loss, outputs: [...this.outputState] });
resolve(loss);
}
};
this._worker!.addEventListener('message', handler);
this._worker!.postMessage({
type: 'train',
payload: {
layerSizes: this.layerSizes,
activationIds: this.activationIds,
weights: flatWeights,
features,
labels,
sampleWeights,
nInputs: this.nInputs,
nOutputs: this.nOutputs,
learningRate: this.learningRate,
maxIterations: this.maxIterations,
convergenceThreshold: this.convergenceThreshold,
},
});
});
}
// ---- Cleanup ----
destroy(): void {
if (this._mlp) {
this._w.free(this._inputPtr);
this._w.free(this._outputPtr);
this._w.destroy(this._mlp);
(this as any)._mlp = null;
}
if (this._worker) {
this._worker.terminate();
this._worker = null;
}
}
}

88
src/core/wasm-types.ts Normal file
View file

@ -0,0 +1,88 @@
/**
* Type definitions for the Emscripten-generated nisps WASM module.
*/
export interface NispsModule {
cwrap(
ident: string,
returnType: string | null,
argTypes: string[],
opts?: unknown
): (...args: unknown[]) => unknown;
HEAPF32: Float32Array;
HEAP32: Int32Array;
_nisps_mlp_create: (layerPtr: number, nLayers: number, actPtr: number, nActs: number) => number;
_nisps_mlp_destroy: (ptr: number) => void;
_nisps_mlp_weight_count: (ptr: number) => number;
_nisps_mlp_get_weights: (mlp: number, ptr: number) => void;
_nisps_mlp_set_weights: (mlp: number, ptr: number) => void;
_nisps_mlp_inference: (mlp: number, inPtr: number, inDim: number, outPtr: number, outDim: number) => void;
_nisps_mlp_train: (
mlp: number, featPtr: number, nSamples: number, featDim: number,
labPtr: number, outDim: number, weightPtr: number,
lr: number, maxIter: number, convThresh: number
) => number;
_nisps_mlp_train_ex: (
mlp: number, featPtr: number, nSamples: number, featDim: number,
labPtr: number, outDim: number, weightPtr: number,
lr: number, maxIter: number, convThresh: number, lossHistPtr: number
) => number;
_nisps_mlp_draw_weights_spread: (mlp: number, spread: number) => void;
_nisps_mlp_move_weights_spread: (mlp: number, speed: number, spread: number) => void;
_nisps_mlp_move_weights_ex: (mlp: number, speed: number, spread: number, pinMaskPtr: number, nOut: number) => void;
_nisps_mlp_infer_batch: (mlp: number, inPtr: number, nPoints: number, inDim: number, outPtr: number, outDim: number) => void;
_nisps_mlp_eval_loss: (
mlp: number, featPtr: number, nSamples: number, featDim: number,
labPtr: number, outDim: number, weightPtr: number
) => number;
_nisps_mlp_get_layer_stats: (mlp: number, statsPtr: number, nLayers: number) => void;
_nisps_alloc: (size: number) => number;
_nisps_free: (ptr: number) => void;
_nisps_alloc_int: (size: number) => number;
_nisps_free_int: (ptr: number) => void;
}
export interface WrappedModule {
mod: NispsModule;
create: (layerPtr: number, nLayers: number, actPtr: number, nActs: number) => number;
destroy: (ptr: number) => void;
weightCount: (ptr: number) => number;
getWeights: (mlp: number, ptr: number) => void;
setWeights: (mlp: number, ptr: number) => void;
inference: (mlp: number, inPtr: number, inDim: number, outPtr: number, outDim: number) => void;
train: (
mlp: number, featPtr: number, nSamples: number, featDim: number,
labPtr: number, outDim: number, weightPtr: number,
lr: number, maxIter: number, convThresh: number
) => number;
trainEx: (
mlp: number, featPtr: number, nSamples: number, featDim: number,
labPtr: number, outDim: number, weightPtr: number,
lr: number, maxIter: number, convThresh: number, lossHistPtr: number
) => number;
drawWeightsSpread: (mlp: number, spread: number) => void;
moveWeightsSpread: (mlp: number, speed: number, spread: number) => void;
moveWeightsEx: (mlp: number, speed: number, spread: number, pinMaskPtr: number, nOut: number) => void;
inferBatch: (mlp: number, inPtr: number, nPoints: number, inDim: number, outPtr: number, outDim: number) => void;
evalLoss: (
mlp: number, featPtr: number, nSamples: number, featDim: number,
labPtr: number, outDim: number, weightPtr: number
) => number;
getLayerStats: (mlp: number, statsPtr: number, nLayers: number) => void;
alloc: (size: number) => number;
free: (ptr: number) => void;
allocInt: (size: number) => number;
freeInt: (ptr: number) => void;
}
export interface LayerStats {
meanAbs: number;
maxAbs: number;
deadFrac: number;
satFrac: number;
}
export interface WeightSnapshot {
layerSizes: number[];
weights: number[];
}

View file

@ -0,0 +1,140 @@
// Web Worker for async WASM training (module worker)
// Loads its own nisps WASM instance for off-thread training
import NispsModule from './nisps.js';
let mod = null;
let w = null;
let mlp = null;
let currentLayerSizes = null;
async function ensureModule() {
if (mod) return;
mod = await NispsModule({
locateFile: (path) => {
// WASM binary is in public/ (served at root)
if (path.endsWith('.wasm')) return '/' + path;
return path;
}
});
w = {
mod,
create: mod.cwrap('nisps_mlp_create', 'number', ['number', 'number', 'number', 'number']),
destroy: mod.cwrap('nisps_mlp_destroy', null, ['number']),
weightCount: mod.cwrap('nisps_mlp_weight_count', 'number', ['number']),
getWeights: mod.cwrap('nisps_mlp_get_weights', null, ['number', 'number']),
setWeights: mod.cwrap('nisps_mlp_set_weights', null, ['number', 'number']),
train: mod.cwrap('nisps_mlp_train', 'number', ['number', 'number', 'number', 'number', 'number', 'number', 'number', 'number', 'number', 'number']),
trainEx: mod.cwrap('nisps_mlp_train_ex', 'number', ['number', 'number', 'number', 'number', 'number', 'number', 'number', 'number', 'number', 'number', 'number']),
alloc: mod.cwrap('nisps_alloc', 'number', ['number']),
free: mod.cwrap('nisps_free', null, ['number']),
allocInt: mod.cwrap('nisps_alloc_int', 'number', ['number']),
freeInt: mod.cwrap('nisps_free_int', null, ['number']),
};
}
function toHeapF32(arr) {
const ptr = w.alloc(arr.length);
w.mod.HEAPF32.set(arr, ptr >> 2);
return ptr;
}
function toHeapI32(arr) {
const ptr = w.allocInt(arr.length);
w.mod.HEAP32.set(arr, ptr >> 2);
return ptr;
}
function fromHeapF32(ptr, length) {
const offset = ptr >> 2;
return Array.from(w.mod.HEAPF32.subarray(offset, offset + length));
}
function arraysEqual(a, b) {
if (!a || !b || a.length !== b.length) return false;
for (let i = 0; i < a.length; i++) if (a[i] !== b[i]) return false;
return true;
}
self.onmessage = async function(e) {
const { type, payload } = e.data;
if (type === 'train') {
await ensureModule();
const {
layerSizes, activationIds, weights,
features, labels, sampleWeights,
nInputs, nOutputs,
learningRate, maxIterations, convergenceThreshold,
} = payload;
// Recreate MLP if architecture changed
if (!mlp || !arraysEqual(currentLayerSizes, layerSizes)) {
if (mlp) w.destroy(mlp);
const layerPtr = toHeapI32(new Int32Array(layerSizes));
const actPtr = toHeapI32(new Int32Array(activationIds));
mlp = w.create(layerPtr, layerSizes.length, actPtr, activationIds.length);
w.freeInt(layerPtr);
w.freeInt(actPtr);
currentLayerSizes = [...layerSizes];
}
// Load weights from main thread
const weightCount = w.weightCount(mlp);
const wPtr = toHeapF32(new Float32Array(weights));
w.setWeights(mlp, wPtr);
w.free(wPtr);
// Build flat training arrays with bias
const featureDim = nInputs + 1;
const nSamples = features.length;
const featFlat = new Float32Array(nSamples * featureDim);
const labFlat = new Float32Array(nSamples * nOutputs);
for (let i = 0; i < nSamples; i++) {
for (let j = 0; j < nInputs; j++) {
featFlat[i * featureDim + j] = features[i][j];
}
featFlat[i * featureDim + nInputs] = 1.0;
for (let j = 0; j < nOutputs; j++) {
labFlat[i * nOutputs + j] = labels[i][j] || 0;
}
}
const featPtr = toHeapF32(featFlat);
const labPtr = toHeapF32(labFlat);
const weightPtr = sampleWeights ? toHeapF32(new Float32Array(sampleWeights)) : 0;
const lossHistPtr = w.alloc(maxIterations);
// Train (extended — captures per-iteration loss history)
const itersRun = w.trainEx(
mlp, featPtr, nSamples, featureDim,
labPtr, nOutputs,
weightPtr,
learningRate, maxIterations, convergenceThreshold,
lossHistPtr
);
// Read per-iteration loss history
const lossHistory = fromHeapF32(lossHistPtr, itersRun);
w.free(featPtr);
w.free(labPtr);
if (weightPtr) w.free(weightPtr);
w.free(lossHistPtr);
const loss = itersRun > 0 ? lossHistory[itersRun - 1] : 0;
// Extract trained weights
const outPtr = w.alloc(weightCount);
w.getWeights(mlp, outPtr);
const trainedWeights = fromHeapF32(outPtr, weightCount);
w.free(outPtr);
self.postMessage({
type: 'trained',
payload: { weights: trainedWeights, loss, lossHistory },
});
}
};

2
src/core/wasm/nisps.js Normal file

File diff suppressed because one or more lines are too long

8
src/index.tsx Normal file
View file

@ -0,0 +1,8 @@
/* @refresh reload */
import { render } from 'solid-js/web';
import App from './App';
const root = document.getElementById('root');
if (!root) throw new Error('Root element not found');
render(() => <App />, root);

30
src/vite-env.d.ts vendored Normal file
View file

@ -0,0 +1,30 @@
/// <reference types="vite/client" />
// Type declaration for vite-plugin-cross-origin-isolation
declare module 'vite-plugin-cross-origin-isolation' {
interface Plugin {
name: string;
configureServer?: (server: any) => void;
configurePreviewServer?: (server: any) => void;
}
export default function crossOriginIsolation(): Plugin;
}
// Type declaration for Emscripten-generated nisps.js module (relative to src/core/)
declare module '*/wasm/nisps.js' {
interface NispsModuleInstance {
cwrap(
ident: string,
returnType: string | null,
argTypes: string[],
opts?: unknown
): (...args: unknown[]) => unknown;
HEAPF32: Float32Array;
HEAP32: Int32Array;
[key: string]: any;
}
type NispsModuleFactory = (moduleArg?: Record<string, any>) => Promise<NispsModuleInstance>;
const NispsModule: NispsModuleFactory;
export default NispsModule;
}

16
tests/solidjs/helpers.ts Normal file
View file

@ -0,0 +1,16 @@
/**
* Shared helpers for SolidJS e2e tests.
*/
import { Page } from '@playwright/test';
/**
* Navigate to SolidJS app with ?debug=1 and wait for WASM engine init.
*/
export async function loadSolidApp(page: Page, extraParams = '') {
await page.addInitScript(() => {
localStorage.removeItem('nisps-a-immersive');
localStorage.setItem('nisps-help-seen', '1');
});
await page.goto(`/?debug=1${extraParams}`);
await page.waitForFunction(() => (window as any).__nisps !== undefined, { timeout: 20_000 });
}

View file

@ -0,0 +1,150 @@
/**
* e2e tests for f01-vite-wasm-spike milestone.
* Validates:
* VAL-PROJ-001: Vite dev server starts with COOP/COEP headers
* VAL-PROJ-002: SolidJS app renders without errors
* VAL-PROJ-003: WASM ML engine loads and inference runs
*/
import { test, expect, Page } from '@playwright/test';
import { loadSolidApp } from './helpers';
test.describe('Vite + WASM spike', () => {
test('VAL-PROJ-001: COOP/COEP headers are set', async ({ page }) => {
const response = await page.goto('/');
expect(response).not.toBeNull();
const headers = response!.headers();
expect(headers['cross-origin-opener-policy']).toBe('same-origin');
expect(headers['cross-origin-embedder-policy']).toBe('require-corp');
});
test('VAL-PROJ-002: SolidJS app renders without console errors', async ({ page }) => {
const errors: string[] = [];
page.on('pageerror', (err) => errors.push(err.message));
await page.goto('/');
// Wait for app to render — the status text should appear
await page.waitForFunction(() => {
const root = document.getElementById('root');
return root && root.textContent && root.textContent.includes('MEMLNaut');
}, { timeout: 10_000 });
// Filter out 404s for non-critical resources (e.g. favicon)
const criticalErrors = errors.filter(e =>
!e.includes('404') && !e.includes('Failed to load resource')
);
expect(criticalErrors).toEqual([]);
});
test('VAL-PROJ-003: WASM loads and inference returns 126 bounded outputs', async ({ page }) => {
await loadSolidApp(page);
const result = await page.evaluate(() => {
const probe = (window as any).__nisps;
const outputs = probe.getOutputs();
return {
length: outputs.length,
min: Math.min(...outputs),
max: Math.max(...outputs),
allBounded: outputs.every((v: number) => v >= 0 && v <= 1),
isArray: Array.isArray(outputs),
};
});
expect(result.length).toBe(126);
expect(result.min).toBeGreaterThanOrEqual(0);
expect(result.max).toBeLessThanOrEqual(1);
expect(result.allBounded).toBe(true);
});
test('WASM inference changes with different inputs', async ({ page }) => {
await loadSolidApp(page);
const result = await page.evaluate(() => {
const probe = (window as any).__nisps;
// Get outputs at default position
const outputs1 = [...probe.getOutputs()];
// Set different inputs and get new outputs
probe.setInputs(0.75, 0.25);
const outputs2 = [...probe.getOutputs()];
return {
same: outputs1.every((v: number, i: number) => v === outputs2[i]),
outputs2Length: outputs2.length,
outputs2Bounded: outputs2.every((v: number) => v >= 0 && v <= 1),
};
});
expect(result.same).toBe(false);
expect(result.outputs2Length).toBe(126);
expect(result.outputs2Bounded).toBe(true);
});
test('debug probe not exposed without ?debug=1', async ({ page }) => {
await page.goto('/');
await page.waitForFunction(() => {
const root = document.getElementById('root');
return root && root.textContent && root.textContent.includes('MEMLNaut');
}, { timeout: 10_000 });
const probeType = await page.evaluate(() => typeof (window as any).__nisps);
expect(probeType).toBe('undefined');
});
test('initial state is untrained', async ({ page }) => {
await loadSolidApp(page);
const result = await page.evaluate(() => {
const probe = (window as any).__nisps;
return {
exampleCount: probe.getExampleCount(),
loss: probe.getLoss(),
};
});
expect(result.exampleCount).toBe(0);
expect(result.loss).toBeNull();
});
test('randomise changes outputs', async ({ page }) => {
await loadSolidApp(page);
const result = await page.evaluate(() => {
const probe = (window as any).__nisps;
const before = [...probe.getOutputs()];
probe.randomise();
const after = [...probe.getOutputs()];
return {
changed: !before.every((v: number, i: number) => v === after[i]),
allBounded: after.every((v: number) => v >= 0 && v <= 1),
};
});
expect(result.changed).toBe(true);
expect(result.allBounded).toBe(true);
});
test('setInputs clamps out-of-range values', async ({ page }) => {
await loadSolidApp(page);
const result = await page.evaluate(() => {
const probe = (window as any).__nisps;
// Set extreme values — should be clamped to [0,1]
probe.setInputs(-5, 99);
const outputs = probe.getOutputs();
return {
length: outputs.length,
allBounded: outputs.every((v: number) => v >= 0 && v <= 1),
allFinite: outputs.every((v: number) => Number.isFinite(v)),
};
});
expect(result.length).toBe(126);
expect(result.allBounded).toBe(true);
expect(result.allFinite).toBe(true);
});
});

25
tsconfig.json Normal file
View file

@ -0,0 +1,25 @@
{
"compilerOptions": {
"strict": true,
"target": "ESNext",
"module": "ESNext",
"moduleResolution": "bundler",
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"jsx": "preserve",
"jsxImportSource": "solid-js",
"types": ["vite/client"],
"noEmit": true,
"isolatedModules": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"resolveJsonModule": true,
"ignoreDeprecations": "6.0",
"baseUrl": ".",
"paths": {
"~/*": ["src/*"]
}
},
"include": ["src/**/*.ts", "src/**/*.tsx", "vite.config.ts"],
"exclude": ["node_modules", "playground", "src/daisysp", "src/memllib", "src/memlp"]
}

32
vite.config.ts Normal file
View file

@ -0,0 +1,32 @@
import { defineConfig } from 'vite';
import solidPlugin from 'vite-plugin-solid';
import crossOriginIsolation from 'vite-plugin-cross-origin-isolation';
export default defineConfig({
plugins: [
solidPlugin(),
crossOriginIsolation(),
],
cacheDir: 'node_modules/.vite',
server: {
port: 5174,
watch: {
usePolling: true,
interval: 1000,
},
headers: {
'Cross-Origin-Opener-Policy': 'same-origin',
'Cross-Origin-Embedder-Policy': 'require-corp',
},
},
preview: {
port: 5174,
headers: {
'Cross-Origin-Opener-Policy': 'same-origin',
'Cross-Origin-Embedder-Policy': 'require-corp',
},
},
build: {
target: 'esnext',
},
});