trustformers-wasm 0.1.4

WebAssembly bindings for TrustformeRS transformer library
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
# TrustformeRS WASM Performance Guide

This guide covers best practices and techniques for optimizing TrustformeRS WASM performance in web and edge environments.

## Table of Contents

- [Performance Overview]#performance-overview
- [WebGPU Acceleration]#webgpu-acceleration
- [Memory Optimization]#memory-optimization
- [Model Optimization]#model-optimization
- [Browser Optimization]#browser-optimization
- [Edge Computing]#edge-computing
- [Profiling and Monitoring]#profiling-and-monitoring
- [Common Bottlenecks]#common-bottlenecks
- [Performance Tuning]#performance-tuning

## Performance Overview

TrustformeRS WASM can achieve significant performance improvements through careful optimization:

- **WebGPU Acceleration**: 10-100x speedup over CPU
- **Model Quantization**: 50-75% memory reduction, 2-4x faster
- **Batch Processing**: 3-5x throughput improvement
- **Streaming**: Real-time text generation
- **Caching**: 90%+ reduction in loading time

## WebGPU Acceleration

### Enable WebGPU

Always use WebGPU when available for maximum performance:

```javascript
import { is_webgpu_supported, InferenceSession } from 'trustformers-wasm';

const session = new InferenceSession('text-generation');

if (is_webgpu_supported()) {
    await session.initialize_with_device('GPU');
    console.log('Using WebGPU acceleration');
} else {
    await session.initialize_with_device('CPU');
    console.log('Falling back to CPU');
}
```

### Device Selection

Choose the optimal device based on workload:

```javascript
const capabilities = session.get_device_capabilities();

// For large models (>1GB)
if (capabilities.memory_mb > 4000) {
    session.force_device_type('GPU');
}

// For small models on mobile
if (capabilities.memory_mb < 2000) {
    session.force_device_type('CPU');
}
```

### GPU Memory Management

Optimize GPU memory usage:

```javascript
import { get_memory_stats } from 'trustformers-wasm';

// Monitor GPU memory usage
function monitorGPUMemory() {
    const stats = get_memory_stats();
    console.log('GPU Memory:', stats.gpu_memory / 1024 / 1024, 'MB');
    
    // Free unused memory if approaching limit
    if (stats.gpu_memory > 0.8 * capabilities.memory_mb * 1024 * 1024) {
        session.cleanup();
    }
}
```

### Workgroup Optimization

For custom shaders, optimize workgroup sizes:

```javascript
// Optimal workgroup sizes for different operations
const WORKGROUP_SIZES = {
    'matrix_multiply': [16, 16],
    'layer_norm': [256],
    'attention': [32, 8],
    'elementwise': [256]
};
```

## Memory Optimization

### Quantization

Enable quantization to reduce memory usage:

```javascript
import { QuantizationConfig, QuantizationPrecision } from 'trustformers-wasm';

const quantConfig = new QuantizationConfig();

// For deployment (best compression)
quantConfig.precision = QuantizationPrecision.Int8;
quantConfig.calibration_samples = 100;
quantConfig.enable_dynamic = true;

// For development (balanced)
quantConfig.precision = QuantizationPrecision.FP16;
quantConfig.preserve_accuracy = true;

session.enable_quantization(quantConfig);
```

### Memory Monitoring

Track memory usage to prevent OOM:

```javascript
import { get_memory_stats } from 'trustformers-wasm';

function checkMemoryUsage() {
    const stats = get_memory_stats();
    const totalMB = stats.wasm_memory / 1024 / 1024;
    
    console.log(`Memory usage: ${totalMB.toFixed(1)} MB`);
    
    if (totalMB > 1000) {
        console.warn('High memory usage detected');
        // Consider enabling quantization or reducing batch size
    }
}

// Check every 30 seconds
setInterval(checkMemoryUsage, 30000);
```

### Model Caching

Use IndexedDB caching to avoid repeated downloads:

```javascript
// Initialize storage with size limit
await session.initialize_storage(500); // 500MB limit

// Load with automatic caching
await session.load_model_with_cache(
    'gpt2-small',
    'https://example.com/gpt2-small.bin',
    'GPT-2 Small',
    'gpt2',
    '1.0'
);
```

### Memory Cleanup

Implement proper cleanup to prevent memory leaks:

```javascript
// Clean up after inference
function cleanup() {
    session.cleanup();
    
    // Force garbage collection (if available)
    if (window.gc) {
        window.gc();
    }
}

// Clean up on page unload
window.addEventListener('beforeunload', cleanup);
```

## Model Optimization

### Model Size Selection

Choose appropriate model sizes for your use case:

```javascript
const RECOMMENDED_MODEL_SIZES = {
    'mobile': '< 100MB',
    'desktop': '< 500MB', 
    'server': '< 2GB',
    'edge': '< 50MB'
};

function getOptimalModelSize() {
    const isMobile = /Android|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
    const memory = navigator.deviceMemory || 4; // GB
    
    if (isMobile && memory < 4) {
        return 'mobile';
    } else if (memory < 8) {
        return 'desktop';
    } else {
        return 'server';
    }
}
```

### Model Splitting

For large models, use splitting for progressive loading:

```javascript
import { ModelSplitter, LoadingStrategy } from 'trustformers-wasm';

const splitter = new ModelSplitter();

// Configure splitting
const chunkConfig = {
    max_chunk_size_mb: 50,
    strategy: LoadingStrategy.Progressive,
    priority_order: ['embeddings', 'attention', 'feedforward']
};

// Split and load progressively
const session = await splitter.load_split_model(modelUrl, chunkConfig);
```

### Weight Compression

Apply weight compression for deployment:

```javascript
import { WeightCompressor, CompressionStrategy } from 'trustformers-wasm';

const compressor = new WeightCompressor();

// Configure compression
const compressionConfig = {
    strategy: CompressionStrategy.MagnitudePruning,
    sparsity_ratio: 0.9, // Remove 90% of smallest weights
    preserve_important_layers: ['attention', 'layer_norm']
};

// Compress model
const compressedModel = await compressor.compress(modelData, compressionConfig);
console.log('Compression ratio:', compressedModel.compression_ratio);
```

## Browser Optimization

### Loading Optimization

Optimize WASM loading for faster startup:

```javascript
// Use streaming compilation
const wasmPromise = WebAssembly.compileStreaming(fetch('trustformers_wasm_bg.wasm'));

// Preload while displaying loading screen
async function initialize() {
    const module = await wasmPromise;
    const instance = await WebAssembly.instantiate(module);
    // Continue with initialization
}
```

### Web Workers

Use Web Workers for background processing:

```javascript
import { WorkerPool, WorkerPriority } from 'trustformers-wasm';

const workerPool = new WorkerPool();
await workerPool.initialize(4); // 4 workers

// Process inference in background
const result = await workerPool.submit_task(
    inputTensor,
    WorkerPriority.High
);
```

### Shared Memory

Enable SharedArrayBuffer for multi-threading:

```html
<!-- Required headers for SharedArrayBuffer -->
<meta http-equiv="Cross-Origin-Opener-Policy" content="same-origin">
<meta http-equiv="Cross-Origin-Embedder-Policy" content="require-corp">
```

```javascript
import { is_cross_origin_isolated, ThreadPool } from 'trustformers-wasm';

if (is_cross_origin_isolated()) {
    const threadPool = new ThreadPool();
    await threadPool.initialize(navigator.hardwareConcurrency);
    console.log('Multi-threading enabled');
}
```

## Edge Computing

### Cold Start Optimization

Minimize cold start latency in edge environments:

```javascript
import { EdgeRuntime, EdgeInferenceConfig } from 'trustformers-wasm';

const edgeRuntime = new EdgeRuntime();
const config = EdgeInferenceConfig.for_cold_start_optimization();

// Pre-warm the runtime
await edgeRuntime.initialize(config);

// Use smaller models for edge
const modelSize = edgeRuntime.recommended_model_size_mb();
console.log('Recommended model size:', modelSize, 'MB');
```

### Geographic Distribution

Use geo-distributed caching:

```javascript
import { GeoDistributionManager } from 'trustformers-wasm';

const geoManager = new GeoDistributionManager();

// Automatically select nearest edge location
const nearestEdge = await geoManager.get_nearest_edge_location();
console.log('Using edge location:', nearestEdge.region);

// Load model from nearest edge
const modelUrl = nearestEdge.get_model_url('gpt2-small');
```

### Edge Caching

Implement intelligent edge caching:

```javascript
import { EdgeCacheManager, EvictionPolicy } from 'trustformers-wasm';

const cacheManager = new EdgeCacheManager();

// Configure cache for edge environment
const cacheConfig = {
    max_size_mb: 100,
    eviction_policy: EvictionPolicy.LRU,
    compression_level: 'high',
    replication_factor: 2
};

await cacheManager.initialize(cacheConfig);
```

## Profiling and Monitoring

### Performance Profiler

Use the built-in profiler to identify bottlenecks:

```javascript
import { PerformanceProfiler, create_development_profiler } from 'trustformers-wasm';

const profiler = create_development_profiler();

// Profile inference
profiler.start_profiling();
const result = session.predict(inputTensor);
const profile = profiler.stop_profiling();

console.log('Profile results:', profile);
console.log('Bottlenecks:', profile.bottlenecks);
```

### Debug Logging

Enable comprehensive logging for performance analysis:

```javascript
import { DebugConfig, LogLevel } from 'trustformers-wasm';

const debugConfig = new DebugConfig();
debugConfig.level = LogLevel.Info;
debugConfig.enable_performance_monitoring = true;
debugConfig.enable_memory_tracking = true;
debugConfig.log_tensor_shapes = true;

session.enable_debug_logging(debugConfig);
```

### Benchmarking

Regular benchmarking to track performance:

```javascript
async function runBenchmark() {
    const iterations = 100;
    const times = [];
    
    for (let i = 0; i < iterations; i++) {
        const start = performance.now();
        await session.predict(testInput);
        const end = performance.now();
        times.push(end - start);
    }
    
    const avgTime = times.reduce((a, b) => a + b) / times.length;
    const p95Time = times.sort()[Math.floor(times.length * 0.95)];
    
    console.log('Average inference time:', avgTime.toFixed(2), 'ms');
    console.log('P95 inference time:', p95Time.toFixed(2), 'ms');
}
```

## Common Bottlenecks

### 1. Memory Bandwidth

**Symptoms**: High GPU usage but slow performance
**Solutions**:
- Use tensor fusion to reduce memory transfers
- Optimize data layouts (use texture storage)
- Batch operations to amortize transfer costs

```javascript
// Enable kernel fusion
session.enable_kernel_fusion(['layer_norm', 'activation']);
```

### 2. Model Loading

**Symptoms**: Long initialization times
**Solutions**:
- Use model caching
- Enable streaming compilation
- Implement progressive loading

```javascript
// Progressive model loading
const loadingProgress = await session.load_model_progressively(modelData, {
    chunk_size_mb: 10,
    on_progress: (progress) => console.log('Loading:', progress.percentage, '%')
});
```

### 3. CPU/GPU Synchronization

**Symptoms**: GPU utilization gaps
**Solutions**:
- Use asynchronous operations
- Pipeline multiple requests
- Minimize CPU-GPU data transfers

```javascript
// Asynchronous inference
const promises = [];
for (const input of inputs) {
    promises.push(session.predict_async(input));
}
const results = await Promise.all(promises);
```

### 4. JavaScript Overhead

**Symptoms**: High CPU usage in main thread
**Solutions**:
- Use Web Workers for heavy computations
- Minimize JavaScript tensor operations
- Batch API calls

```javascript
// Batch multiple predictions
const batchInput = concatenateTensors(inputs);
const batchResult = session.predict(batchInput);
const results = splitTensorResults(batchResult, inputs.length);
```

## Performance Tuning

### Device-Specific Optimizations

```javascript
function optimizeForDevice() {
    const capabilities = session.get_device_capabilities();
    
    if (capabilities.vendor.includes('NVIDIA')) {
        // NVIDIA-specific optimizations
        session.set_workgroup_size([32, 32]);
        session.enable_tensor_cores(true);
    } else if (capabilities.vendor.includes('AMD')) {
        // AMD-specific optimizations
        session.set_workgroup_size([64, 16]);
        session.enable_wave64(true);
    } else if (capabilities.vendor.includes('Intel')) {
        // Intel-specific optimizations
        session.set_workgroup_size([16, 16]);
        session.enable_subgroups(true);
    }
}
```

### Network Optimization

```javascript
import { is_low_data_mode, NetworkType } from 'trustformers-wasm';

function optimizeForNetwork() {
    const networkType = getNetworkType();
    
    if (networkType === NetworkType.Slow2G || is_low_data_mode()) {
        // Use aggressive compression
        session.enable_quantization(QuantizationPrecision.Int4);
        session.set_cache_compression('maximum');
    } else if (networkType === NetworkType.WiFi) {
        // Use balanced settings
        session.enable_quantization(QuantizationPrecision.FP16);
        session.set_cache_compression('balanced');
    }
}
```

### Battery Optimization

```javascript
import { BatteryInfo, get_battery_info } from 'trustformers-wasm';

async function optimizeForBattery() {
    const battery = await get_battery_info();
    
    if (battery.level < 0.2) {
        // Low battery: prioritize efficiency
        session.force_device_type('CPU');
        session.set_power_mode('efficiency');
    } else if (battery.charging) {
        // Charging: prioritize performance
        session.force_device_type('GPU');
        session.set_power_mode('performance');
    }
}
```

## Performance Checklist

- [ ] Enable WebGPU when available
- [ ] Use appropriate quantization settings
- [ ] Implement model caching
- [ ] Monitor memory usage
- [ ] Use batch processing for multiple requests
- [ ] Enable streaming for long generations
- [ ] Use Web Workers for background tasks
- [ ] Enable SharedArrayBuffer for multi-threading
- [ ] Optimize for target deployment environment
- [ ] Profile and benchmark regularly
- [ ] Implement proper cleanup
- [ ] Use device-specific optimizations

## Performance Targets

### Latency Targets

- **Text Generation**: < 100ms first token, < 50ms subsequent tokens
- **Classification**: < 50ms for single input
- **Image Captioning**: < 500ms for 224x224 image
- **Translation**: < 200ms for sentence-length input

### Throughput Targets

- **Batch Processing**: 100+ requests/second
- **Streaming**: 20+ tokens/second
- **Model Loading**: < 5 seconds for models under 500MB

### Memory Targets

- **Mobile**: < 500MB total memory usage
- **Desktop**: < 2GB total memory usage
- **Edge**: < 100MB total memory usage

## Conclusion

Optimal TrustformeRS WASM performance requires careful attention to:

1. **Hardware utilization** - Use WebGPU and multi-threading
2. **Memory management** - Enable quantization and caching
3. **Model optimization** - Choose appropriate sizes and compression
4. **Environment adaptation** - Optimize for deployment target
5. **Continuous monitoring** - Profile and benchmark regularly

Following these guidelines can achieve 10-100x performance improvements over naive implementations.