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
# Browser Integration Guide

RusTorch WASMモジュールをブラウザアプリケーションに統合する完全ガイド

## 🚀 Setup & Installation

### 1. Build WASM Package

```bash
# Project root directory
cd rustorch
wasm-pack build --target web --features wasm --no-default-features

# Generated files will be in pkg/ directory
ls pkg/
# rustorch.js, rustorch_bg.wasm, rustorch.d.ts, package.json
```

### 2. HTML Setup

```html
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>RusTorch ML in Browser</title>
</head>
<body>
    <script type="module">
        import init, * as rustorch from './pkg/rustorch.js';
        
        async function run() {
            // Initialize WASM module
            await init();
            rustorch.initialize_wasm_runtime();
            
            console.log('RusTorch WASM initialized!');
            
            // Your ML code here
            main();
        }
        
        run();
    </script>
</body>
</html>
```

### 3. Modern JavaScript/TypeScript Project

```javascript
// main.js
import init, * as rustorch from './pkg/rustorch.js';

async function initializeML() {
    await init();
    rustorch.initialize_wasm_runtime();
    
    return rustorch;
}

export { initializeML };
```

```typescript
// types.d.ts
declare module './pkg/rustorch.js' {
    export function initialize_wasm_runtime(): void;
    export class WasmPreprocessor {
        static compute_stats(data: number[]): number[];
        static min_max_normalize(data: number[], min: number, max: number): number[];
    }
    // ... other type definitions
}
```

## 🔬 Complete ML Pipeline Example

### Real-time Image Classification

```html
<!DOCTYPE html>
<html>
<head>
    <title>Real-time ML Classification</title>
</head>
<body>
    <video id="video" width="640" height="480" autoplay></video>
    <canvas id="canvas" width="224" height="224" style="display: none;"></canvas>
    <div id="results"></div>

    <script type="module">
        import init, * as rustorch from './pkg/rustorch.js';
        
        class MLPipeline {
            constructor() {
                // Vision processing
                this.vision = rustorch.WasmVision;
                this.imagenet_mean = [0.485, 0.456, 0.406];
                this.imagenet_std = [0.229, 0.224, 0.225];
                
                // Neural network model
                this.conv1 = new rustorch.WasmConv2d(3, 32, 3, 1, 1, true);
                this.conv2 = new rustorch.WasmConv2d(32, 64, 3, 2, 1, true);
                this.conv3 = new rustorch.WasmConv2d(64, 128, 3, 2, 1, true);
                this.classifier = new rustorch.WasmLinear(128 * 56 * 56, 1000, true);
                
                this.metrics = new rustorch.WasmMetrics();
                this.performance = new rustorch.WasmPerformance();
            }
            
            async processFrame(imageData, width, height) {
                this.performance.start();
                
                // Convert canvas ImageData to float array
                const rgbData = [];
                for (let i = 0; i < imageData.data.length; i += 4) {
                    rgbData.push(imageData.data[i] / 255.0);     // R
                    rgbData.push(imageData.data[i + 1] / 255.0); // G
                    rgbData.push(imageData.data[i + 2] / 255.0); // B
                }
                
                // Resize to 224x224 for model input
                let processed = this.vision.resize(rgbData, height, width, 224, 224, 3);
                
                // Normalize with ImageNet statistics
                processed = this.vision.normalize(processed, this.imagenet_mean, this.imagenet_std, 3);
                
                // Forward pass through CNN
                let features = this.conv1.forward(processed, 1, 224, 224);
                features = rustorch.relu(features);
                
                features = this.conv2.forward(features, 1, 224, 224);
                features = rustorch.relu(features);
                
                features = this.conv3.forward(features, 1, 112, 112);  // After stride=2
                features = rustorch.relu(features);
                
                // Classification
                const logits = this.classifier.forward(features, 1);
                const predictions = rustorch.softmax(logits);
                
                const processingTime = this.performance.elapsed();
                
                return {
                    predictions: predictions,
                    processingTime: processingTime
                };
            }
        }
        
        async function setupWebcam() {
            const video = document.getElementById('video');
            const canvas = document.getElementById('canvas');
            const ctx = canvas.getContext('2d');
            const resultsDiv = document.getElementById('results');
            
            // Initialize RusTorch
            await init();
            rustorch.initialize_wasm_runtime();
            const mlPipeline = new MLPipeline();
            
            // Setup webcam
            const stream = await navigator.mediaDevices.getUserMedia({ video: true });
            video.srcObject = stream;
            
            // Process frames
            const processFrame = () => {
                // Draw video frame to canvas
                ctx.drawImage(video, 0, 0, 224, 224);
                const imageData = ctx.getImageData(0, 0, 224, 224);
                
                // Run inference
                mlPipeline.processFrame(imageData, 224, 224).then(result => {
                    const topPrediction = Math.max(...result.predictions);
                    const topIndex = result.predictions.indexOf(topPrediction);
                    
                    resultsDiv.innerHTML = `
                        <h3>Prediction Results</h3>
                        <p>Top class: ${topIndex} (confidence: ${(topPrediction * 100).toFixed(2)}%)</p>
                        <p>Processing time: ${result.processingTime.toFixed(2)}ms</p>
                    `;
                });
                
                requestAnimationFrame(processFrame);
            };
            
            video.addEventListener('loadeddata', processFrame);
        }
        
        setupWebcam();
    </script>
</body>
</html>
```

## 📈 Training in Browser

### Mini-batch Training Example

```javascript
import init, * as rustorch from './pkg/rustorch.js';

class BrowserTrainer {
    constructor() {
        this.optimizer = new rustorch.WasmOptimizer('adam', 0.001);
        this.batchNorm = new rustorch.WasmBatchNorm(10, 0.1, 1e-5);
        this.preprocessor = new rustorch.WasmPreprocessor();
        this.metrics = new rustorch.WasmMetrics();
    }
    
    async train(features, targets, epochs = 100, batch_size = 32) {
        // Preprocessing
        const stats = this.preprocessor.compute_stats(features);
        const normalized_features = this.preprocessor.z_score_normalize(
            features, stats[0], stats[1]
        );
        
        // Train-test split
        const split = this.preprocessor.train_test_split(
            normalized_features, targets, 10, 0.2, 42
        );
        
        const train_history = [];
        
        for (let epoch = 0; epoch < epochs; epoch++) {
            // Create batches
            const batches = this.preprocessor.create_batches(
                split.trainFeatures, split.trainTargets, 10, batch_size
            );
            
            let epoch_loss = 0.0;
            
            // Training loop
            for (let batch_idx = 0; batch_idx < batches.length; batch_idx++) {
                const batch = batches[batch_idx];
                
                // Forward pass
                this.batchNorm.set_training(true);
                let output = this.batchNorm.forward(batch.features, batch.batchSize);
                output = rustorch.relu(output);
                
                // Simple linear layer (placeholder)
                const weights = new Array(output.length).fill(0.5);
                const predictions = output.map((x, i) => x * weights[i % weights.length]);
                
                // Loss calculation
                const loss = rustorch.mse_loss(predictions, batch.targets);
                epoch_loss += loss;
                
                // Compute gradients (simplified)
                const gradients = output.map((x, i) => 
                    2 * (predictions[i] - batch.targets[i]) * x / batch.batchSize
                );
                
                // Optimization step
                const updated_weights = this.optimizer.step('weights', weights, gradients);
            }
            
            // Validation
            if (epoch % 10 === 0) {
                this.batchNorm.set_training(false);
                const val_output = this.batchNorm.forward(split.testFeatures, split.testFeatures.length / 10);
                const val_predictions = val_output.map(x => x > 0.5 ? 1 : 0);
                const val_accuracy = this.metrics.accuracy(val_predictions, split.testTargets);
                
                train_history.push({
                    epoch,
                    loss: epoch_loss / batches.length,
                    val_accuracy
                });
                
                console.log(`Epoch ${epoch}: Loss=${train_history[train_history.length-1].loss.toFixed(4)}, Val Acc=${val_accuracy.toFixed(4)}`);
            }
        }
        
        return train_history;
    }
}

// Usage
async function trainModel() {
    await init();
    rustorch.initialize_wasm_runtime();
    
    const trainer = new BrowserTrainer();
    
    // Generate sample data
    const features = new Array(1000 * 10).fill(0).map(() => Math.random());
    const targets = new Array(1000).fill(0).map(() => Math.round(Math.random()));
    
    const history = await trainer.train(features, targets, 100, 32);
    console.log('Training completed:', history);
}
```

## 🎮 Interactive Visualization

### Real-time Training Dashboard

```javascript
class TrainingDashboard {
    constructor() {
        this.chart_canvas = document.getElementById('loss-chart');
        this.ctx = this.chart_canvas.getContext('2d');
        this.loss_history = [];
        this.accuracy_history = [];
    }
    
    updateMetrics(epoch, loss, accuracy) {
        this.loss_history.push({ epoch, loss });
        this.accuracy_history.push({ epoch, accuracy });
        
        // Redraw chart
        this.drawChart();
    }
    
    drawChart() {
        const ctx = this.ctx;
        const width = this.chart_canvas.width;
        const height = this.chart_canvas.height;
        
        ctx.clearRect(0, 0, width, height);
        
        // Draw loss curve
        ctx.strokeStyle = 'red';
        ctx.beginPath();
        this.loss_history.forEach((point, i) => {
            const x = (i / this.loss_history.length) * width;
            const y = height - (point.loss / Math.max(...this.loss_history.map(p => p.loss))) * height;
            
            if (i === 0) ctx.moveTo(x, y);
            else ctx.lineTo(x, y);
        });
        ctx.stroke();
        
        // Draw accuracy curve
        ctx.strokeStyle = 'blue';
        ctx.beginPath();
        this.accuracy_history.forEach((point, i) => {
            const x = (i / this.accuracy_history.length) * width;
            const y = height - point.accuracy * height;
            
            if (i === 0) ctx.moveTo(x, y);
            else ctx.lineTo(x, y);
        });
        ctx.stroke();
    }
}
```

## 🔧 Performance Optimization

### Memory Management

```javascript
// Efficient memory usage
const monitor = new rustorch.WasmMemoryMonitor();

function processLargeBatch(data) {
    const chunks = [];
    const chunk_size = 1000;
    
    for (let i = 0; i < data.length; i += chunk_size) {
        const chunk = data.slice(i, i + chunk_size);
        
        monitor.record_allocation(chunk.length * 4); // 4 bytes per f32
        
        const result = rustorch.relu(chunk);
        chunks.push(result);
        
        monitor.record_deallocation(chunk.length * 4);
    }
    
    console.log('Peak memory usage:', monitor.peak_usage(), 'bytes');
    return chunks.flat();
}
```

### Parallel Processing with Web Workers

```javascript
// main.js
class ParallelMLProcessor {
    constructor(num_workers = 4) {
        this.workers = [];
        this.task_queue = [];
        
        for (let i = 0; i < num_workers; i++) {
            const worker = new Worker('./ml-worker.js', { type: 'module' });
            this.workers.push(worker);
        }
    }
    
    async processParallel(batches) {
        const promises = batches.map((batch, i) => {
            const worker = this.workers[i % this.workers.length];
            return new Promise(resolve => {
                worker.onmessage = (e) => resolve(e.data);
                worker.postMessage({ batch, operation: 'forward_pass' });
            });
        });
        
        return Promise.all(promises);
    }
}

// ml-worker.js
import init, * as rustorch from './pkg/rustorch.js';

let initialized = false;

self.onmessage = async function(e) {
    if (!initialized) {
        await init();
        rustorch.initialize_wasm_runtime();
        initialized = true;
    }
    
    const { batch, operation } = e.data;
    
    let result;
    switch (operation) {
        case 'forward_pass':
            const activated = rustorch.relu(batch.features);
            const output = rustorch.softmax(activated);
            result = { output, batch_id: batch.id };
            break;
        default:
            result = { error: 'Unknown operation' };
    }
    
    self.postMessage(result);
};
```

## 📊 Integration with Chart Libraries

### Chart.js Integration

```javascript
import Chart from 'chart.js/auto';

class MLVisualization {
    constructor() {
        this.loss_chart = new Chart(document.getElementById('loss-chart'), {
            type: 'line',
            data: {
                labels: [],
                datasets: [{
                    label: 'Training Loss',
                    data: [],
                    borderColor: 'red',
                    backgroundColor: 'rgba(255, 0, 0, 0.1)'
                }, {
                    label: 'Validation Accuracy',
                    data: [],
                    borderColor: 'blue',
                    backgroundColor: 'rgba(0, 0, 255, 0.1)'
                }]
            },
            options: {
                responsive: true,
                scales: {
                    y: {
                        beginAtZero: true
                    }
                }
            }
        });
    }
    
    updateTrainingMetrics(epoch, loss, accuracy) {
        this.loss_chart.data.labels.push(epoch);
        this.loss_chart.data.datasets[0].data.push(loss);
        this.loss_chart.data.datasets[1].data.push(accuracy);
        this.loss_chart.update();
    }
}
```

## 🎯 Production Deployment

### Bundle Optimization

```javascript
// webpack.config.js
const path = require('path');

module.exports = {
    entry: './src/index.js',
    mode: 'production',
    output: {
        path: path.resolve(__dirname, 'dist'),
        filename: 'bundle.js',
    },
    experiments: {
        asyncWebAssembly: true,
        syncWebAssembly: true
    },
    module: {
        rules: [
            {
                test: /\.wasm$/,
                type: 'webassembly/async'
            }
        ]
    },
    optimization: {
        splitChunks: {
            chunks: 'all',
            cacheGroups: {
                wasm: {
                    test: /\.wasm$/,
                    name: 'wasm',
                    chunks: 'all'
                }
            }
        }
    }
};
```

### CDN Deployment

```html
<!-- Load from CDN -->
<script type="module">
    import init from 'https://cdn.jsdelivr.net/npm/rustorch-wasm@latest/rustorch.js';
    // ... rest of your code
</script>
```

## 🔒 Security Considerations

### Content Security Policy

```html
<meta http-equiv="Content-Security-Policy" 
      content="default-src 'self'; 
               wasm-eval 'unsafe-eval'; 
               script-src 'self' 'wasm-unsafe-eval';">
```

### Safe Data Handling

```javascript
class SecureMLProcessor {
    constructor() {
        this.max_input_size = 1024 * 1024; // 1MB limit
        this.sanitizer = new rustorch.WasmPreprocessor();
    }
    
    validateInput(data) {
        if (data.length > this.max_input_size) {
            throw new Error('Input too large');
        }
        
        // Check for NaN or infinite values
        if (data.some(x => !isFinite(x))) {
            throw new Error('Invalid numeric data');
        }
        
        return true;
    }
    
    processSecurely(data) {
        this.validateInput(data);
        
        try {
            return this.sanitizer.min_max_normalize(data, 0.0, 1.0);
        } catch (error) {
            console.error('Processing failed:', error);
            return null;
        }
    }
}
```

## 🧪 Testing

### Unit Testing with Jest

```javascript
// ml.test.js
import init, * as rustorch from '../pkg/rustorch.js';

describe('RusTorch WASM', () => {
    beforeAll(async () => {
        await init();
        rustorch.initialize_wasm_runtime();
    });
    
    test('activation functions', () => {
        const input = [-1.0, 0.0, 1.0];
        
        const relu_output = rustorch.relu(input);
        expect(relu_output).toEqual([0.0, 0.0, 1.0]);
        
        const sigmoid_output = rustorch.sigmoid(input);
        expect(sigmoid_output[1]).toBeCloseTo(0.5, 5);
    });
    
    test('preprocessing utilities', () => {
        const data = [1, 2, 3, 4, 5];
        const normalized = rustorch.WasmPreprocessor.min_max_normalize(data, 1, 5);
        expect(normalized).toEqual([0.0, 0.25, 0.5, 0.75, 1.0]);
    });
    
    test('metrics calculation', () => {
        const predictions = [0, 1, 1, 0];
        const targets = [0, 1, 0, 0];
        const accuracy = rustorch.WasmMetrics.accuracy(predictions, targets);
        expect(accuracy).toBe(0.75);
    });
});
```

### Performance Testing

```javascript
async function benchmarkWASM() {
    await init();
    rustorch.initialize_wasm_runtime();
    
    const perf = new rustorch.WasmPerformance();
    const sizes = [100, 1000, 10000];
    
    for (const size of sizes) {
        const data = new Array(size).fill(0).map(() => Math.random());
        
        perf.start();
        const result = rustorch.relu(data);
        const time = perf.elapsed();
        
        console.log(`ReLU ${size} elements: ${time.toFixed(2)}ms`);
        
        perf.start();
        const normalized = rustorch.WasmPreprocessor.min_max_normalize(data, 0, 1);
        const normalize_time = perf.elapsed();
        
        console.log(`Normalize ${size} elements: ${normalize_time.toFixed(2)}ms`);
    }
}
```

## 🔄 Integration Patterns

### React Integration

```jsx
import React, { useEffect, useState } from 'react';
import init, * as rustorch from './pkg/rustorch.js';

function MLComponent() {
    const [wasmReady, setWasmReady] = useState(false);
    const [result, setResult] = useState(null);
    
    useEffect(() => {
        async function initWasm() {
            await init();
            rustorch.initialize_wasm_runtime();
            setWasmReady(true);
        }
        initWasm();
    }, []);
    
    const processData = () => {
        if (!wasmReady) return;
        
        const data = [1, 2, 3, 4, 5];
        const output = rustorch.relu(data);
        setResult(output);
    };
    
    return (
        <div>
            <button onClick={processData} disabled={!wasmReady}>
                Process Data
            </button>
            {result && <div>Result: {result.join(', ')}</div>}
        </div>
    );
}
```

### Vue.js Integration

```vue
<template>
    <div>
        <button @click="processData" :disabled="!wasmReady">
            Process Data
        </button>
        <div v-if="result">Result: {{ result.join(', ') }}</div>
    </div>
</template>

<script>
import init, * as rustorch from './pkg/rustorch.js';

export default {
    data() {
        return {
            wasmReady: false,
            result: null
        };
    },
    
    async mounted() {
        await init();
        rustorch.initialize_wasm_runtime();
        this.wasmReady = true;
    },
    
    methods: {
        processData() {
            const data = [1, 2, 3, 4, 5];
            this.result = rustorch.relu(data);
        }
    }
};
</script>
```

## 📱 Mobile Considerations

### Progressive Web App (PWA)

```javascript
// sw.js - Service Worker for offline ML
const CACHE_NAME = 'ml-cache-v1';
const urlsToCache = [
    '/',
    '/pkg/rustorch.js',
    '/pkg/rustorch_bg.wasm'
];

self.addEventListener('install', event => {
    event.waitUntil(
        caches.open(CACHE_NAME)
            .then(cache => cache.addAll(urlsToCache))
    );
});

self.addEventListener('fetch', event => {
    event.respondWith(
        caches.match(event.request)
            .then(response => response || fetch(event.request))
    );
});
```

## 🐛 Debugging

### WASM Debugging Tools

```javascript
// Debug utilities
class WasmDebugger {
    constructor() {
        this.monitor = new rustorch.WasmMemoryMonitor();
        this.performance = new rustorch.WasmPerformance();
    }
    
    profile(operation, ...args) {
        this.performance.start();
        const start_memory = this.monitor.current_usage();
        
        try {
            const result = operation(...args);
            
            const end_memory = this.monitor.current_usage();
            const elapsed = this.performance.elapsed();
            
            console.log('Profile:', {
                operation: operation.name,
                time_ms: elapsed,
                memory_delta: end_memory - start_memory,
                peak_memory: this.monitor.peak_usage()
            });
            
            return result;
        } catch (error) {
            console.error('Operation failed:', error);
            throw error;
        }
    }
}

// Usage
const debugger = new WasmDebugger();
const result = debugger.profile(rustorch.relu, [1, 2, 3]);
```

## 🔗 External Resources

- [WebAssembly Documentation]https://webassembly.org/
- [wasm-bindgen Book]https://rustwasm.github.io/wasm-bindgen/
- [RusTorch Main Documentation]https://docs.rs/rustorch
- [Performance Benchmarks]./benchmarks.md