@openfluke/welvet 0.1.5 → 0.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -151,6 +151,57 @@ const [output2] = loadedNetwork.ForwardCPU(
151
151
  - ✅ Same behavior as Python, C#, C, and WASM
152
152
 
153
153
  See `example/grid-scatter.ts` for a complete working example.
154
+
155
+ ### ⚡ Stepping API - Fine-Grained Execution Control
156
+
157
+ **NEW:** Execute networks one step at a time for online learning:
158
+
159
+ ```typescript
160
+ import { init, createNetwork, StepState } from "@openfluke/welvet";
161
+
162
+ await init();
163
+
164
+ // Create network
165
+ const config = { batch_size: 1, layers: [
166
+ { type: "dense", input_height: 4, output_height: 8, activation: "relu" },
167
+ { type: "lstm", input_size: 8, hidden_size: 12, seq_length: 1 },
168
+ { type: "dense", input_height: 12, output_height: 3, activation: "softmax" }
169
+ ]};
170
+ const network = createNetwork(config);
171
+
172
+ // Initialize stepping state
173
+ const state: StepState = network.createStepState(4);
174
+
175
+ // Training loop - update weights after EACH step
176
+ for (let step = 0; step < 100000; step++) {
177
+ state.setInput(new Float32Array([0.1, 0.2, 0.1, 0.3]));
178
+ state.stepForward();
179
+ const output = state.getOutput();
180
+
181
+ // Calculate gradients
182
+ const gradients = new Float32Array(output.length);
183
+ for (let i = 0; i < output.length; i++)
184
+ gradients[i] = output[i] - target[i];
185
+
186
+ // Backward pass
187
+ state.stepBackward(gradients);
188
+
189
+ // Update weights immediately
190
+ network.ApplyGradients(JSON.stringify([learningRate]));
191
+ }
192
+ ```
193
+
194
+ **Stepping API:**
195
+ - `network.createStepState(inputSize)` - Initialize stepping state
196
+ - `state.setInput(data)` - Set input for current step
197
+ - `state.stepForward()` - Execute forward pass
198
+ - `state.getOutput()` - Get output from last layer
199
+ - `state.stepBackward(gradients)` - Execute backward pass
200
+ - `network.ApplyGradients(paramsJSON)` - Update network weights
201
+
202
+ See `example/step_train_v3.ts` for a complete example achieving 100% accuracy.
203
+
204
+
154
205
  {
155
206
  type: "parallel",
156
207
  combine_mode: "add",
@@ -24,7 +24,9 @@ export async function init() {
24
24
  * Wrapper around the global createLoomNetwork function exposed by WASM
25
25
  */
26
26
  export function createNetwork(config) {
27
- const jsonConfig = typeof config === 'string' ? config : JSON.stringify(config);
27
+ const jsonConfig = typeof config === "string"
28
+ ? config
29
+ : JSON.stringify(config);
28
30
  return createLoomNetwork(jsonConfig);
29
31
  }
30
32
  /**
package/dist/index.js CHANGED
@@ -25,7 +25,9 @@ export async function initBrowser() {
25
25
  * Wrapper around the global createLoomNetwork function exposed by WASM
26
26
  */
27
27
  export function createNetwork(config) {
28
- const jsonConfig = typeof config === 'string' ? config : JSON.stringify(config);
28
+ const jsonConfig = typeof config === "string"
29
+ ? config
30
+ : JSON.stringify(config);
29
31
  return createLoomNetwork(jsonConfig);
30
32
  }
31
33
  /**
package/dist/loader.js CHANGED
@@ -8,19 +8,31 @@ import { dirname, join } from "path";
8
8
  const __filename = fileURLToPath(import.meta.url);
9
9
  const __dirname = dirname(__filename);
10
10
  export async function loadLoomWASM() {
11
+ // __dirname points to:
12
+ // - dist/ → in production
13
+ // - src/ → when running via Bun, ts-node, or example files
14
+ let root;
15
+ if (__dirname.endsWith("dist")) {
16
+ // Normal production layout
17
+ root = __dirname;
18
+ }
19
+ else {
20
+ // Running from src/ or example/
21
+ // Point to project’s dist/ directory
22
+ root = join(__dirname, "..", "dist");
23
+ }
11
24
  // Load wasm_exec.js
12
- const wasmExecPath = join(__dirname, "../assets/wasm_exec.js");
25
+ const wasmExecPath = join(root, "wasm_exec.js");
13
26
  const wasmExecCode = readFileSync(wasmExecPath, "utf-8");
14
27
  // Execute wasm_exec.js to get the Go runtime
15
28
  eval(wasmExecCode);
16
29
  // Load main.wasm
17
- const wasmPath = join(__dirname, "../assets/main.wasm");
30
+ const wasmPath = join(root, "main.wasm");
18
31
  const wasmBuffer = readFileSync(wasmPath);
19
- // @ts-ignore - Go is defined by wasm_exec.js
32
+ // @ts-ignore - Go runtime from wasm_exec.js
20
33
  const go = new Go();
21
34
  const { instance } = await WebAssembly.instantiate(wasmBuffer, go.importObject);
22
- // Run the Go WASM module
23
35
  go.run(instance);
24
- // Wait a bit for initialization
36
+ // Wait for WASM runtime to finish bootstrapping
25
37
  await new Promise((resolve) => setTimeout(resolve, 100));
26
38
  }
package/dist/main.wasm CHANGED
Binary file
package/dist/types.d.ts CHANGED
@@ -95,6 +95,20 @@ export interface Network {
95
95
  ListMethods(paramsJSON: string): string;
96
96
  HasMethod(paramsJSON: string): string;
97
97
  GetMethodSignature(paramsJSON: string): string;
98
+ ApplyGradients(paramsJSON: string): string;
99
+ ApplyGradientsAdamW(paramsJSON: string): string;
100
+ ApplyGradientsRMSprop(paramsJSON: string): string;
101
+ ApplyGradientsSGDMomentum(paramsJSON: string): string;
102
+ createStepState(inputSize: number): StepState;
103
+ }
104
+ /**
105
+ * StepState interface for stepping execution
106
+ */
107
+ export interface StepState {
108
+ setInput(data: Float32Array | number[]): void;
109
+ stepForward(): number;
110
+ getOutput(): Float32Array;
111
+ stepBackward(gradients: Float32Array | number[]): Float32Array;
98
112
  }
99
113
  /**
100
114
  * Global WASM functions exposed by main.go
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openfluke/welvet",
3
- "version": "0.1.5",
3
+ "version": "0.1.6",
4
4
  "description": "TypeScript/JavaScript bindings for LOOM neural network framework with WebAssembly support - GPU-accelerated machine learning in the browser",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -58,4 +58,4 @@
58
58
  "@types/node": "^22.7.5",
59
59
  "typescript": "^5.6.3"
60
60
  }
61
- }
61
+ }