import init, * as rustorch from '../pkg/rustorch.js';
class NeuralNetworkDemo {
constructor() {
this.model = null;
this.initialized = false;
}
async initialize() {
try {
await init();
console.log('✓ RusTorch WASM initialized successfully');
this.initialized = true;
} catch (error) {
console.error('❌ Failed to initialize WASM:', error);
throw error;
}
}
createXORNetwork() {
if (!this.initialized) {
throw new Error('WASM not initialized');
}
this.model = new rustorch.WasmModel();
this.model.add_linear(2, 4, true); this.model.add_relu(); this.model.add_linear(4, 1, true);
console.log(`✓ Created XOR network with ${this.model.num_layers()} layers`);
return this.model;
}
generateXORData() {
const inputs = [
[0, 0], [0, 1], [1, 0], [1, 1] ];
const targets = [0, 1, 1, 0];
return { inputs, targets };
}
forward(input) {
if (!this.model) {
throw new Error('Model not created');
}
const inputTensor = new rustorch.WasmTensor(input, [1, input.length]);
return this.model.forward(inputTensor);
}
evaluateXOR() {
const { inputs, targets } = this.generateXORData();
const loss = new rustorch.WasmMSELoss();
console.log('\n=== XOR Evaluation ===');
let totalLoss = 0;
for (let i = 0; i < inputs.length; i++) {
const output = this.forward(inputs[i]);
const prediction = output.data()[0];
const target = targets[i];
const error = Math.abs(prediction - target);
console.log(`Input: [${inputs[i].join(', ')}] → Prediction: ${prediction.toFixed(4)}, Target: ${target}, Error: ${error.toFixed(4)}`);
const targetTensor = new rustorch.WasmTensor([target], [1]);
const sampleLoss = loss.compute(output, targetTensor);
totalLoss += sampleLoss;
}
const avgLoss = totalLoss / inputs.length;
console.log(`Average Loss: ${avgLoss.toFixed(6)}`);
return avgLoss;
}
demonstrateTensorOps() {
console.log('\n=== Tensor Operations Demo ===');
const interop = new rustorch.JsInterop();
const shape = new Array(3, 3);
const tensor1 = interop.random_tensor(shape, -1.0, 1.0);
const tensor2 = interop.random_tensor(shape, -1.0, 1.0);
console.log('Tensor 1:', Array.from(tensor1.data()).map(x => x.toFixed(3)));
console.log('Tensor 2:', Array.from(tensor2.data()).map(x => x.toFixed(3)));
const sum = tensor1.add(tensor2);
console.log('Sum:', Array.from(sum.data()).map(x => x.toFixed(3)));
const product = tensor1.multiply(tensor2);
console.log('Product:', Array.from(product.data()).map(x => x.toFixed(3)));
const relu = tensor1.relu();
const sigmoid = tensor1.sigmoid();
console.log('ReLU:', Array.from(relu.data()).map(x => x.toFixed(3)));
console.log('Sigmoid:', Array.from(sigmoid.data()).map(x => x.toFixed(3)));
console.log(`Sum of elements: ${tensor1.sum().toFixed(3)}`);
console.log(`Mean: ${tensor1.mean().toFixed(3)}`);
}
async performanceBenchmark() {
console.log('\n=== Performance Benchmark ===');
const interop = new rustorch.JsInterop();
const runtime = new rustorch.JsRuntime();
runtime.reset_stats();
const iterations = 1000;
const results = interop.benchmark_operations(iterations);
console.log(`Matrix Multiplication (${iterations} iterations): ${results.matmulTimeMs.toFixed(2)} ms`);
console.log(`Element-wise Addition (${iterations} iterations): ${results.addTimeMs.toFixed(2)} ms`);
console.log(`ReLU Activation (${iterations} iterations): ${results.reluTimeMs.toFixed(2)} ms`);
console.log(`Operations per second: ${results.operationsPerSecond.toFixed(0)}`);
const memory = new rustorch.JsMemoryManager();
console.log(`Memory usage: ${memory.get_memory_usage_mb().toFixed(2)} MB`);
console.log(`Memory utilization: ${memory.get_memory_utilization().toFixed(1)}%`);
console.log(`Fragmentation ratio: ${memory.get_fragmentation_ratio().toFixed(3)}`);
}
memoryDemo() {
console.log('\n=== Memory Management Demo ===');
const memory = new rustorch.JsMemoryManager();
const interop = new rustorch.JsInterop();
console.log(`Initial memory: ${memory.get_memory_usage()} bytes`);
const tensors = [];
for (let i = 0; i < 100; i++) {
const shape = new Array(10, 10);
const tensor = interop.random_tensor(shape, 0, 1);
tensors.push(tensor);
}
console.log(`After creating tensors: ${memory.get_memory_usage()} bytes`);
tensors.length = 0;
const freedBytes = memory.garbage_collect();
console.log(`Garbage collected: ${freedBytes} bytes`);
console.log(`After GC: ${memory.get_memory_usage()} bytes`);
const recommendations = memory.get_memory_recommendations();
console.log(`Recommendations: ${recommendations}`);
}
async runDemo() {
try {
await this.initialize();
this.createXORNetwork();
this.evaluateXOR();
this.demonstrateTensorOps();
await this.performanceBenchmark();
this.memoryDemo();
console.log('\n=== System Information ===');
const interop = new rustorch.JsInterop();
const sysInfo = interop.get_system_info();
console.log(`Runtime: ${sysInfo.runtime}`);
console.log(`Architecture: ${sysInfo.architecture}`);
console.log(`Hardware threads: ${sysInfo.hardwareConcurrency}`);
console.log('\n✓ Demo completed successfully!');
} catch (error) {
console.error('❌ Demo failed:', error);
}
}
}
if (typeof module !== 'undefined' && module.exports) {
module.exports = NeuralNetworkDemo;
} else {
window.NeuralNetworkDemo = NeuralNetworkDemo;
}
if (typeof require !== 'undefined' && require.main === module) {
const demo = new NeuralNetworkDemo();
demo.runDemo();
}