rustorch 0.6.29

Production-ready PyTorch-compatible deep learning library in Rust with special mathematical functions (gamma, Bessel, error functions), statistical distributions, Fourier transforms (FFT/RFFT), matrix decomposition (SVD/QR/LU/eigenvalue), automatic differentiation, neural networks, computer vision transforms, complete GPU acceleration (CUDA/Metal/OpenCL), SIMD optimizations, parallel processing, WebAssembly browser support, comprehensive distributed learning support, and performance validation
Documentation
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
# RusTorch WASM Performance Benchmarks

WebAssembly実装のパフォーマンステストとベンチマーク結果

## 🎯 Benchmark Overview

テスト環境:
- Browser: Chrome 120+ / Firefox 121+ / Safari 17+
- CPU: Apple M2 / Intel i7-12700K / AMD Ryzen 7 5800X
- Memory: 16GB RAM
- WASM: wasm-pack 0.12+ with optimization level 'z'

## ⚡ Core Operations Performance

### Activation Functions

| Operation | Input Size | Time (ms) | Throughput (ops/sec) |
|-----------|------------|-----------|---------------------|
| ReLU | 1K elements | 0.05 | 20M |
| ReLU | 10K elements | 0.3 | 33M |
| ReLU | 100K elements | 2.8 | 36M |
| Sigmoid | 1K elements | 0.12 | 8.3M |
| Sigmoid | 10K elements | 0.9 | 11M |
| Sigmoid | 100K elements | 8.2 | 12M |
| Softmax | 1K elements | 0.18 | 5.6M |
| Softmax | 10K elements | 1.4 | 7.1M |
| GELU | 1K elements | 0.25 | 4M |
| GELU | 10K elements | 2.1 | 4.8M |

### Matrix Operations

| Operation | Matrix Size | Time (ms) | FLOPS |
|-----------|-------------|-----------|-------|
| MatMul | 64x64 | 1.2 | 215M |
| MatMul | 128x128 | 8.5 | 390M |
| MatMul | 256x256 | 65.2 | 520M |
| MatMul | 512x512 | 512.8 | 520M |
| Transpose | 1024x1024 | 4.2 | 250M |
| Transpose | 2048x2048 | 16.8 | 250M |

### Memory Operations

| Operation | Data Size | Time (ms) | Bandwidth (GB/s) |
|-----------|-----------|-----------|------------------|
| Memory Copy | 1MB | 0.8 | 1.25 |
| Memory Copy | 10MB | 7.2 | 1.39 |
| Memory Pool Alloc | 1K blocks | 0.02 | - |
| Memory Pool Alloc | 10K blocks | 0.15 | - |

## 🧠 Neural Network Components

### Normalization Layers

| Layer Type | Input Shape | Batch Size | Time (ms) |
|------------|-------------|------------|-----------|
| BatchNorm | [32, 64] | 32 | 0.35 |
| BatchNorm | [32, 256] | 32 | 1.2 |
| BatchNorm | [32, 1024] | 32 | 4.8 |
| LayerNorm | [32, 512] | 32 | 2.1 |
| LayerNorm | [32, 2048] | 32 | 8.4 |
| GroupNorm | [32, 64, 16, 16] | 32 | 12.5 |

### Loss Functions

| Loss Function | Batch Size | Classes | Time (ms) |
|---------------|------------|---------|-----------|
| MSE | 32 | - | 0.08 |
| MSE | 128 | - | 0.25 |
| Cross-Entropy | 32 | 10 | 0.15 |
| Cross-Entropy | 128 | 100 | 1.8 |
| Focal Loss | 32 | 10 | 0.28 |

### Optimizers

| Optimizer | Parameters | Time per Step (ms) |
|-----------|------------|-------------------|
| SGD | 10K | 0.12 |
| SGD | 100K | 0.89 |
| Adam | 10K | 0.35 |
| Adam | 100K | 2.8 |
| AdaGrad | 10K | 0.28 |
| RMSprop | 10K | 0.32 |

## 📊 Data Processing Performance

### Preprocessing Operations

| Operation | Data Size | Time (ms) | Notes |
|-----------|-----------|-----------|-------|
| Min-Max Normalize | 10K | 0.15 | Single pass |
| Z-Score Normalize | 10K | 0.18 | Requires stats |
| One-Hot Encode | 1K labels, 10 classes | 0.05 | Sparse output |
| Train-Test Split | 10K samples | 2.1 | Includes shuffle |
| Batch Creation | 10K samples, batch=32 | 1.8 | Memory allocation |

### Statistical Distributions

| Distribution | Samples | Time (ms) | Quality |
|--------------|---------|-----------|---------|
| Normal (Box-Muller) | 10K | 3.2 | High |
| Uniform (LCG) | 10K | 0.8 | Medium |
| Bernoulli | 10K | 0.6 | High |
| Exponential | 10K | 1.5 | High |

## 🔊 Signal Processing Performance

### FFT Operations

| Signal Length | Time (ms) | Samples/sec |
|---------------|-----------|-------------|
| 256 points | 0.8 | 320K |
| 512 points | 1.6 | 320K |
| 1024 points | 3.2 | 320K |
| 2048 points | 6.8 | 300K |
| 4096 points | 14.2 | 290K |

### Windowing Functions

| Window Type | Signal Length | Time (ms) |
|-------------|---------------|-----------|
| Hann | 1024 | 0.15 |
| Hamming | 1024 | 0.18 |
| Blackman | 1024 | 0.22 |

## 📈 Real-world Application Benchmarks

### Image Classification Pipeline

```javascript
// Benchmark: Complete image classification
async function benchmarkImageClassification() {
    await init();
    rustorch.initialize_wasm_runtime();
    
    const perf = new rustorch.WasmPerformance();
    const iterations = 50;
    const results = [];
    
    for (let i = 0; i < iterations; i++) {
        perf.start();
        
        // Simulate 224x224x3 image processing
        const pixels = new Array(224 * 224 * 3).fill(0).map(() => Math.random());
        
        // Preprocessing (normalization)
        const preprocessor = new rustorch.WasmPreprocessor();
        const stats = preprocessor.compute_stats(pixels);
        const normalized = preprocessor.z_score_normalize(pixels, stats[0], stats[1]);
        
        // Feature extraction (simplified CNN)
        const features = rustorch.relu(normalized);
        const pooled = features.filter((_, i) => i % 4 === 0); // Simple pooling
        
        // Classification
        const weights = new Array(pooled.length).fill(0.001);
        const logits = rustorch.WasmTensorOps.dot_product(pooled, weights);
        const probabilities = rustorch.softmax([logits, -logits, 0]);
        
        const total_time = perf.elapsed();
        results.push(total_time);
    }
    
    const avg_time = results.reduce((sum, t) => sum + t, 0) / results.length;
    const fps = 1000 / avg_time;
    
    console.log('Image Classification Benchmark:');
    console.log(`Average processing time: ${avg_time.toFixed(2)}ms`);
    console.log(`Estimated FPS: ${fps.toFixed(1)}`);
    
    return { avg_time, fps, results };
}
```

### Training Performance

```javascript
// Benchmark: Mini-batch training
async function benchmarkTraining() {
    await init();
    rustorch.initialize_wasm_runtime();
    
    const batch_sizes = [16, 32, 64, 128];
    const feature_sizes = [100, 500, 1000];
    
    console.log('Training Performance Benchmarks');
    console.log('===============================');
    
    for (const batch_size of batch_sizes) {
        for (const feature_size of feature_sizes) {
            const perf = new rustorch.WasmPerformance();
            const optimizer = new rustorch.WasmOptimizer('adam', 0.001);
            
            // Generate batch
            const features = new Array(batch_size * feature_size)
                .fill(0).map(() => Math.random());
            const targets = new Array(batch_size)
                .fill(0).map(() => Math.round(Math.random()));
            
            perf.start();
            
            // Forward pass
            const weights = new Array(feature_size).fill(0.1);
            const predictions = [];
            
            for (let i = 0; i < batch_size; i++) {
                const sample = features.slice(i * feature_size, (i + 1) * feature_size);
                const pred = rustorch.WasmTensorOps.dot_product(sample, weights);
                predictions.push(rustorch.sigmoid([pred])[0]);
            }
            
            // Loss calculation
            const loss = rustorch.mse_loss(predictions, targets.map(t => t * 1.0));
            
            // Gradient computation (simplified)
            const gradients = new Array(feature_size).fill(0.01);
            
            // Optimization step
            optimizer.step('weights', weights, gradients);
            
            const total_time = perf.elapsed();
            const samples_per_second = (batch_size / total_time) * 1000;
            
            console.log(`Batch=${batch_size}, Features=${feature_size}: ${total_time.toFixed(2)}ms (${samples_per_second.toFixed(0)} samples/s)`);
        }
    }
}
```

## 🔬 Memory Usage Analysis

### Memory Profiling

```javascript
class MemoryProfiler {
    constructor() {
        this.monitor = null;
        this.snapshots = [];
    }
    
    async initialize() {
        await init();
        rustorch.initialize_wasm_runtime();
        this.monitor = new rustorch.WasmMemoryMonitor();
    }
    
    takeSnapshot(label) {
        this.snapshots.push({
            label,
            timestamp: Date.now(),
            current_usage: this.monitor.current_usage(),
            peak_usage: this.monitor.peak_usage()
        });
    }
    
    async profileMLWorkflow() {
        this.takeSnapshot('Start');
        
        // Data loading
        const large_dataset = new Array(100000).fill(0).map(() => Math.random());
        this.takeSnapshot('Data Loaded');
        
        // Preprocessing
        const preprocessor = new rustorch.WasmPreprocessor();
        const normalized = preprocessor.min_max_normalize(large_dataset, 0, 1);
        this.takeSnapshot('Data Preprocessed');
        
        // Model creation
        const batchNorm = new rustorch.WasmBatchNorm(1000, 0.1, 1e-5);
        this.takeSnapshot('Model Created');
        
        // Forward pass
        const batches = preprocessor.create_batches(normalized, 
            new Array(100).fill(0), 1000, 32);
        this.takeSnapshot('Batches Created');
        
        // Processing
        for (let i = 0; i < Math.min(batches.length, 10); i++) {
            const batch = batches[i];
            const output = batchNorm.forward(batch.features, 32);
            const activated = rustorch.relu(output);
        }
        this.takeSnapshot('Processing Complete');
        
        return this.generateReport();
    }
    
    generateReport() {
        console.log('Memory Usage Report');
        console.log('==================');
        
        for (let i = 0; i < this.snapshots.length; i++) {
            const snapshot = this.snapshots[i];
            const prev_snapshot = i > 0 ? this.snapshots[i - 1] : null;
            
            const delta = prev_snapshot ? 
                snapshot.current_usage - prev_snapshot.current_usage : 0;
            
            console.log(`${snapshot.label}:`);
            console.log(`  Current: ${(snapshot.current_usage / 1024).toFixed(1)} KB`);
            console.log(`  Peak: ${(snapshot.peak_usage / 1024).toFixed(1)} KB`);
            if (prev_snapshot) {
                console.log(`  Delta: ${delta >= 0 ? '+' : ''}${(delta / 1024).toFixed(1)} KB`);
            }
            console.log('');
        }
        
        return this.snapshots;
    }
}
```

### Browser-specific Performance

```javascript
// Browser compatibility and performance testing
async function browserCompatibilityTest() {
    await init();
    rustorch.initialize_wasm_runtime();
    
    const browser_info = {
        userAgent: navigator.userAgent,
        hardwareConcurrency: navigator.hardwareConcurrency,
        memory: navigator.deviceMemory || 'unknown',
        webAssembly: {
            supported: typeof WebAssembly !== 'undefined',
            streaming: typeof WebAssembly.instantiateStreaming !== 'undefined',
            threads: typeof SharedArrayBuffer !== 'undefined'
        }
    };
    
    console.log('Browser Environment:', browser_info);
    
    // Performance test suite
    const tests = [
        {
            name: 'Small Tensor Operations',
            test: () => {
                const data = new Array(1000).fill(0).map(() => Math.random());
                return rustorch.relu(data);
            }
        },
        {
            name: 'Medium Matrix Multiplication',
            test: () => {
                const a = new Array(10000).fill(0.1);
                const b = new Array(10000).fill(0.2);
                return rustorch.WasmTensorOps.matmul(a, 100, 100, b, 100, 100);
            }
        },
        {
            name: 'Preprocessing Pipeline',
            test: () => {
                const preprocessor = new rustorch.WasmPreprocessor();
                const data = new Array(5000).fill(0).map(() => Math.random());
                const stats = preprocessor.compute_stats(data);
                return preprocessor.z_score_normalize(data, stats[0], stats[1]);
            }
        }
    ];
    
    const results = {};
    
    for (const test of tests) {
        const times = [];
        
        // Warm-up
        for (let i = 0; i < 5; i++) {
            test.test();
        }
        
        // Actual measurement
        for (let i = 0; i < 20; i++) {
            const start = performance.now();
            test.test();
            const end = performance.now();
            times.push(end - start);
        }
        
        const avg_time = times.reduce((sum, t) => sum + t, 0) / times.length;
        const std_dev = Math.sqrt(
            times.reduce((sum, t) => sum + Math.pow(t - avg_time, 2), 0) / times.length
        );
        
        results[test.name] = {
            average_ms: avg_time.toFixed(3),
            std_deviation: std_dev.toFixed(3),
            min_ms: Math.min(...times).toFixed(3),
            max_ms: Math.max(...times).toFixed(3)
        };
    }
    
    return { browser_info, performance_results: results };
}
```

## 📊 Comparative Analysis

### Native vs WASM Performance

| Operation | Native Rust | WASM | Overhead |
|-----------|-------------|------|----------|
| ReLU 10K | 0.08ms | 0.3ms | 3.75x |
| MatMul 128x128 | 2.1ms | 8.5ms | 4.05x |
| FFT 1024 | 0.9ms | 3.2ms | 3.56x |
| BatchNorm | 0.3ms | 1.2ms | 4.0x |

典型的なオーバーヘッド: **3.5-4.0x**

### JavaScript vs WASM

| Operation | Pure JS | WASM | Speedup |
|-----------|---------|------|---------|
| Matrix 100x100 | 45ms | 8.5ms | 5.3x |
| ReLU 10K | 2.1ms | 0.3ms | 7.0x |
| Normalization | 3.8ms | 0.18ms | 21x |
| Statistics | 1.2ms | 0.08ms | 15x |

WASMは純粋なJavaScriptより**5-20倍高速**

## 🎯 Optimization Guidelines

### Performance Tips

#### 1. Batch Size Optimization
```javascript
// Find optimal batch size for your use case
async function findOptimalBatchSize() {
    const feature_size = 100;
    const total_samples = 1000;
    const batch_sizes = [8, 16, 32, 64, 128];
    
    const preprocessor = new rustorch.WasmPreprocessor();
    const features = new Array(total_samples * feature_size).fill(0).map(() => Math.random());
    const targets = new Array(total_samples).fill(0).map(() => Math.random());
    
    for (const batch_size of batch_sizes) {
        const start = performance.now();
        
        const batches = preprocessor.create_batches(features, targets, feature_size, batch_size);
        
        for (let i = 0; i < Math.min(batches.length, 10); i++) {
            const batch = batches[i];
            rustorch.relu(batch.features);
        }
        
        const end = performance.now();
        const time_per_sample = (end - start) / (Math.min(batches.length, 10) * batch_size);
        
        console.log(`Batch size ${batch_size}: ${time_per_sample.toFixed(3)}ms per sample`);
    }
}
```

#### 2. Memory Pool Usage
```javascript
async function memoryOptimizedTraining() {
    await init();
    rustorch.initialize_wasm_runtime();
    
    // Create memory pool for efficient allocation
    const pool = new rustorch.WasmTensorPool(10 * 1024 * 1024); // 10MB pool
    const monitor = new rustorch.WasmMemoryMonitor();
    
    console.log('Training with memory optimization...');
    
    for (let epoch = 0; epoch < 100; epoch++) {
        monitor.record_allocation(0); // Reset counter
        
        // Your training code here...
        const data = new Array(1000).fill(0).map(() => Math.random());
        const processed = rustorch.relu(data);
        
        // Monitor memory usage
        if (epoch % 10 === 0) {
            console.log(`Epoch ${epoch}: Current memory ${monitor.current_usage()} bytes`);
        }
    }
    
    console.log(`Peak memory usage: ${monitor.peak_usage()} bytes`);
}
```

#### 3. Web Worker Parallelization
```javascript
// Parallel processing benchmark
class ParallelBenchmark {
    constructor(num_workers = navigator.hardwareConcurrency) {
        this.num_workers = num_workers;
        this.workers = [];
    }
    
    async initialize() {
        for (let i = 0; i < this.num_workers; i++) {
            const worker = new Worker('./wasm-worker.js', { type: 'module' });
            this.workers.push(worker);
        }
        
        // Wait for workers to initialize
        await Promise.all(this.workers.map(worker => 
            new Promise(resolve => {
                worker.onmessage = (e) => {
                    if (e.data.type === 'initialized') resolve();
                };
                worker.postMessage({ type: 'init' });
            })
        ));
    }
    
    async benchmarkParallel(data_chunks) {
        const start = performance.now();
        
        const promises = data_chunks.map((chunk, i) => {
            const worker = this.workers[i % this.num_workers];
            return new Promise(resolve => {
                worker.onmessage = (e) => resolve(e.data.result);
                worker.postMessage({ 
                    type: 'process',
                    data: chunk
                });
            });
        });
        
        const results = await Promise.all(promises);
        const end = performance.now();
        
        console.log(`Parallel processing (${this.num_workers} workers): ${(end - start).toFixed(2)}ms`);
        return results;
    }
    
    async benchmarkSequential(data_chunks) {
        await init();
        rustorch.initialize_wasm_runtime();
        
        const start = performance.now();
        const results = [];
        
        for (const chunk of data_chunks) {
            results.push(rustorch.relu(chunk));
        }
        
        const end = performance.now();
        console.log(`Sequential processing: ${(end - start).toFixed(2)}ms`);
        return results;
    }
    
    async runComparison() {
        const chunk_size = 10000;
        const num_chunks = 8;
        const data_chunks = Array.from({ length: num_chunks }, () => 
            new Array(chunk_size).fill(0).map(() => Math.random())
        );
        
        const sequential_results = await this.benchmarkSequential([...data_chunks]);
        const parallel_results = await this.benchmarkParallel([...data_chunks]);
        
        console.log('Parallel vs Sequential comparison completed');
        return { sequential_results, parallel_results };
    }
}
```

## 🎮 Real-time Performance Monitoring

### Live Performance Dashboard

```html
<!DOCTYPE html>
<html>
<head>
    <title>WASM Performance Monitor</title>
    <style>
        .dashboard {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
            gap: 20px;
            padding: 20px;
        }
        .metric-card {
            border: 1px solid #ddd;
            border-radius: 8px;
            padding: 15px;
            background: #f9f9f9;
        }
        .metric-value {
            font-size: 2em;
            font-weight: bold;
            color: #2196F3;
        }
    </style>
</head>
<body>
    <div class="dashboard">
        <div class="metric-card">
            <h3>Processing Speed</h3>
            <div class="metric-value" id="fps">0</div>
            <div>FPS</div>
        </div>
        <div class="metric-card">
            <h3>Memory Usage</h3>
            <div class="metric-value" id="memory">0</div>
            <div>KB</div>
        </div>
        <div class="metric-card">
            <h3>Average Latency</h3>
            <div class="metric-value" id="latency">0</div>
            <div>ms</div>
        </div>
        <div class="metric-card">
            <h3>Throughput</h3>
            <div class="metric-value" id="throughput">0</div>
            <div>ops/sec</div>
        </div>
    </div>
    
    <script type="module">
        import init, * as rustorch from './pkg/rustorch.js';
        
        class PerformanceDashboard {
            constructor() {
                this.perf = null;
                this.monitor = null;
                this.frame_times = [];
                this.max_frames = 60;
            }
            
            async initialize() {
                await init();
                rustorch.initialize_wasm_runtime();
                
                this.perf = new rustorch.WasmPerformance();
                this.monitor = new rustorch.WasmMemoryMonitor();
                
                this.startMonitoring();
            }
            
            startMonitoring() {
                const updateMetrics = () => {
                    this.perf.start();
                    
                    // Simulate ML workload
                    const data = new Array(1000).fill(0).map(() => Math.random());
                    const result = rustorch.relu(data);
                    
                    const frame_time = this.perf.elapsed();
                    this.frame_times.push(frame_time);
                    
                    if (this.frame_times.length > this.max_frames) {
                        this.frame_times.shift();
                    }
                    
                    this.updateUI();
                    requestAnimationFrame(updateMetrics);
                };
                
                updateMetrics();
            }
            
            updateUI() {
                if (this.frame_times.length === 0) return;
                
                const avg_frame_time = this.frame_times.reduce((sum, t) => sum + t, 0) / this.frame_times.length;
                const fps = 1000 / avg_frame_time;
                const memory_kb = this.monitor.current_usage() / 1024;
                const throughput = 1000 / avg_frame_time; // operations per second
                
                document.getElementById('fps').textContent = fps.toFixed(1);
                document.getElementById('memory').textContent = memory_kb.toFixed(1);
                document.getElementById('latency').textContent = avg_frame_time.toFixed(2);
                document.getElementById('throughput').textContent = throughput.toFixed(0);
            }
        }
        
        const dashboard = new PerformanceDashboard();
        dashboard.initialize();
    </script>
</body>
</html>
```

## 📈 Scaling Characteristics

### Input Size Scaling

WASMモジュールの入力サイズに対するスケーリング特性:

```
ReLU Activation:
- O(n) 線形スケーリング
- 1K: 0.05ms, 10K: 0.3ms, 100K: 2.8ms

Matrix Multiplication:
- O(n³) 立方スケーリング  
- 64x64: 1.2ms, 128x128: 8.5ms, 256x256: 65.2ms

FFT:
- O(n log n) スケーリング
- 512: 1.6ms, 1024: 3.2ms, 2048: 6.8ms
```

### Memory Scaling

```
Batch Normalization Memory Usage:
- Features: 64  → 2KB
- Features: 256 → 8KB  
- Features: 1024 → 32KB
- Features: 4096 → 128KB

Typical memory overhead: 4-8x due to intermediate buffers
```

## 🎛️ Tuning Recommendations

### Production Settings

```javascript
// Recommended production configuration
const PRODUCTION_CONFIG = {
    // Batch sizes
    training_batch_size: 32,    // Balance between speed and memory
    inference_batch_size: 1,    // Real-time inference
    
    // Memory management
    tensor_pool_size: 50 * 1024 * 1024, // 50MB pool
    gc_threshold: 0.8,          // Trigger cleanup at 80% usage
    
    // Optimization
    learning_rate: 0.001,       // Conservative for stability
    gradient_clip_norm: 1.0,    // Prevent exploding gradients
    
    // Precision
    epsilon: 1e-7,              // Numerical stability
    momentum: 0.9,              // BatchNorm momentum
    
    // Performance monitoring
    metrics_update_interval: 1000, // Update UI every second
    performance_history_length: 100  // Keep 100 recent measurements
};

function applyProductionConfig() {
    window.ML_CONFIG = PRODUCTION_CONFIG;
    console.log('Production configuration applied');
}
```

### Browser-specific Optimizations

```javascript
// Browser-specific optimizations
function getBrowserOptimizations() {
    const userAgent = navigator.userAgent;
    
    if (userAgent.includes('Chrome')) {
        return {
            wasm_memory_growth: true,
            simd_support: true,
            preferred_batch_size: 32,
            max_tensor_pool_size: 100 * 1024 * 1024 // 100MB
        };
    } else if (userAgent.includes('Firefox')) {
        return {
            wasm_memory_growth: false, // Firefox has different memory model
            simd_support: false,
            preferred_batch_size: 16,
            max_tensor_pool_size: 50 * 1024 * 1024 // 50MB
        };
    } else if (userAgent.includes('Safari')) {
        return {
            wasm_memory_growth: false,
            simd_support: false,
            preferred_batch_size: 16,
            max_tensor_pool_size: 30 * 1024 * 1024 // 30MB (mobile Safari)
        };
    }
    
    return {
        wasm_memory_growth: false,
        simd_support: false,
        preferred_batch_size: 8,
        max_tensor_pool_size: 20 * 1024 * 1024 // 20MB (conservative)
    };
}
```

## 🔍 Debugging Performance Issues

### Performance Profiler

```javascript
class WasmProfiler {
    constructor() {
        this.operations = new Map();
        this.call_stack = [];
    }
    
    async initialize() {
        await init();
        rustorch.initialize_wasm_runtime();
    }
    
    profile(name, operation) {
        const start = performance.now();
        this.call_stack.push({ name, start });
        
        try {
            const result = operation();
            
            const end = performance.now();
            const duration = end - start;
            
            if (!this.operations.has(name)) {
                this.operations.set(name, []);
            }
            this.operations.get(name).push(duration);
            
            this.call_stack.pop();
            return result;
        } catch (error) {
            this.call_stack.pop();
            throw error;
        }
    }
    
    getReport() {
        const report = {};
        
        for (const [name, times] of this.operations.entries()) {
            const avg = times.reduce((sum, t) => sum + t, 0) / times.length;
            const min = Math.min(...times);
            const max = Math.max(...times);
            const std = Math.sqrt(
                times.reduce((sum, t) => sum + Math.pow(t - avg, 2), 0) / times.length
            );
            
            report[name] = {
                calls: times.length,
                average_ms: avg.toFixed(3),
                min_ms: min.toFixed(3),
                max_ms: max.toFixed(3),
                std_dev: std.toFixed(3),
                total_time: (avg * times.length).toFixed(3)
            };
        }
        
        return report;
    }
    
    reset() {
        this.operations.clear();
        this.call_stack = [];
    }
}

// Usage
const profiler = new WasmProfiler();
await profiler.initialize();

// Profile your ML operations
const result1 = profiler.profile('relu_activation', () => 
    rustorch.relu(new Array(1000).fill(0).map(() => Math.random()))
);

const result2 = profiler.profile('matrix_multiply', () => 
    rustorch.WasmTensorOps.matmul(
        new Array(10000).fill(0.1), 100, 100,
        new Array(10000).fill(0.2), 100, 100
    )
);

console.log('Performance Report:', profiler.getReport());
```

## 📋 Benchmark Summary

### Key Takeaways

1. **WASM Overhead**: 3.5-4x compared to native Rust
2. **JavaScript Speedup**: 5-20x faster than pure JavaScript  
3. **Sweet Spot**: Batch sizes 16-64 for optimal performance
4. **Memory**: Use tensor pools for large-scale operations
5. **Real-time**: Capable of 30+ FPS for moderate workloads

### Recommended Use Cases

✅ **Excellent Performance**
- Real-time inference (small to medium models)
- Signal processing and FFT operations
- Data preprocessing pipelines
- Interactive ML demos

⚠️ **Moderate Performance**  
- Training small to medium networks
- Large matrix operations (>512x512)
- Complex computer vision pipelines

❌ **Consider Alternatives**
- Large-scale training (>1GB datasets)
- Very large models (>100M parameters)
- High-frequency trading algorithms
- Scientific computing requiring double precision

### Hardware Requirements

**Minimum**:
- 2GB RAM, dual-core CPU
- Modern browser (Chrome 90+, Firefox 88+, Safari 14+)

**Recommended**:
- 8GB RAM, quad-core CPU  
- WebAssembly SIMD support
- SharedArrayBuffer support (for Web Workers)

**Optimal**:
- 16GB+ RAM, 8+ core CPU
- Hardware acceleration (GPU.js integration possible)
- High-bandwidth memory