trustformers-wasm 0.2.0

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
<!--
  TensorVisualization.svelte
  
  Interactive tensor visualization component with multiple display modes.
-->

<script>
  import { onMount, onDestroy, createEventDispatcher } from 'svelte';
  import { writable, derived } from 'svelte/store';
  import { 
    getTensorData, 
    createTensorSubscription,
    tensorVisualization 
  } from '../svelte_bindings.js';

  // Props
  export let tensorId = null;
  export let width = 300;
  export let height = 300;
  export let visualizationType = 'heatmap'; // 'heatmap', 'bar', 'line', 'histogram'
  export let colorScheme = 'viridis'; // 'viridis', 'plasma', 'coolwarm', 'grayscale'
  export let showControls = true;
  export let showStats = true;
  export let maxDisplayElements = 1000;
  export let updateInterval = 100; // ms

  // Event dispatcher
  const dispatch = createEventDispatcher();

  // State
  let canvasElement;
  let ctx;
  let animationFrame;
  let lastUpdateTime = 0;
  let isHovering = false;
  let hoverInfo = null;

  // Reactive stores
  const tensorSubscription = tensorId ? createTensorSubscription(tensorId) : writable(null);
  
  const tensorStats = derived(tensorSubscription, ($tensor) => {
    if (!$tensor?.data) return null;
    
    const data = Array.from($tensor.data);
    const sortedData = [...data].sort((a, b) => a - b);
    
    return {
      min: sortedData[0] || 0,
      max: sortedData[sortedData.length - 1] || 0,
      mean: data.reduce((sum, val) => sum + val, 0) / data.length || 0,
      median: sortedData[Math.floor(sortedData.length / 2)] || 0,
      std: Math.sqrt(data.reduce((sum, val) => sum + Math.pow(val - (data.reduce((s, v) => s + v, 0) / data.length), 2), 0) / data.length) || 0,
      zeros: data.filter(val => val === 0).length,
      nonZeros: data.filter(val => val !== 0).length,
      shape: $tensor.shape || [],
      dtype: $tensor.dtype || 'unknown',
      memoryUsage: $tensor.memoryUsage || 0
    };
  });

  // Color schemes
  const colorSchemes = {
    viridis: [
      [68, 1, 84],
      [72, 40, 120],
      [62, 74, 137],
      [49, 104, 142],
      [38, 130, 142],
      [31, 158, 137],
      [53, 183, 121],
      [109, 205, 89],
      [180, 222, 44],
      [253, 231, 37]
    ],
    plasma: [
      [13, 8, 135],
      [75, 3, 161],
      [125, 3, 168],
      [168, 34, 150],
      [199, 77, 125],
      [220, 124, 93],
      [234, 171, 53],
      [240, 219, 10]
    ],
    coolwarm: [
      [59, 76, 192],
      [144, 178, 254],
      [220, 220, 220],
      [245, 156, 125],
      [180, 4, 38]
    ],
    grayscale: [
      [0, 0, 0],
      [64, 64, 64],
      [128, 128, 128],
      [192, 192, 192],
      [255, 255, 255]
    ]
  };

  onMount(() => {
    if (canvasElement) {
      ctx = canvasElement.getContext('2d');
      startVisualization();
    }
  });

  onDestroy(() => {
    stopVisualization();
  });

  function startVisualization() {
    const update = () => {
      const now = performance.now();
      if (now - lastUpdateTime >= updateInterval) {
        renderVisualization();
        lastUpdateTime = now;
      }
      animationFrame = requestAnimationFrame(update);
    };
    update();
  }

  function stopVisualization() {
    if (animationFrame) {
      cancelAnimationFrame(animationFrame);
    }
  }

  function renderVisualization() {
    if (!ctx || !tensorId) return;

    try {
      const tensor = getTensorData(tensorId);
      if (!tensor?.data) return;

      // Clear canvas
      ctx.clearRect(0, 0, width, height);

      switch (visualizationType) {
        case 'heatmap':
          renderHeatmap(tensor);
          break;
        case 'bar':
          renderBarChart(tensor);
          break;
        case 'line':
          renderLineChart(tensor);
          break;
        case 'histogram':
          renderHistogram(tensor);
          break;
        default:
          renderHeatmap(tensor);
      }

      dispatch('render', { tensor, timestamp: performance.now() });
    } catch (error) {
      console.error('Visualization render error:', error);
      dispatch('error', { error });
    }
  }

  function renderHeatmap(tensor) {
    if (tensor.shape.length !== 2) {
      renderFallbackVisualization(tensor);
      return;
    }

    const [rows, cols] = tensor.shape;
    const cellWidth = width / cols;
    const cellHeight = height / rows;

    // Calculate value range for normalization
    const data = Array.from(tensor.data);
    const minVal = Math.min(...data);
    const maxVal = Math.max(...data);
    const range = maxVal - minVal || 1;

    for (let i = 0; i < rows; i++) {
      for (let j = 0; j < cols; j++) {
        const value = tensor.data[i * cols + j];
        const normalized = (value - minVal) / range;
        const color = getColor(normalized, colorScheme);

        ctx.fillStyle = `rgb(${color[0]}, ${color[1]}, ${color[2]})`;
        ctx.fillRect(j * cellWidth, i * cellHeight, cellWidth, cellHeight);

        // Add value text for small matrices
        if (rows <= 10 && cols <= 10) {
          ctx.fillStyle = normalized > 0.5 ? 'white' : 'black';
          ctx.font = `${Math.min(cellWidth, cellHeight) / 3}px Arial`;
          ctx.textAlign = 'center';
          ctx.textBaseline = 'middle';
          ctx.fillText(
            value.toFixed(2),
            j * cellWidth + cellWidth / 2,
            i * cellHeight + cellHeight / 2
          );
        }
      }
    }
  }

  function renderBarChart(tensor) {
    const data = Array.from(tensor.data.slice(0, maxDisplayElements));
    const maxVal = Math.max(...data.map(Math.abs));
    const barWidth = width / data.length;

    ctx.strokeStyle = '#ccc';
    ctx.lineWidth = 1;
    ctx.beginPath();
    ctx.moveTo(0, height / 2);
    ctx.lineTo(width, height / 2);
    ctx.stroke();

    data.forEach((value, index) => {
      const barHeight = Math.abs(value) / maxVal * (height / 2);
      const x = index * barWidth;
      const y = value >= 0 ? height / 2 - barHeight : height / 2;

      ctx.fillStyle = value >= 0 ? '#4CAF50' : '#F44336';
      ctx.fillRect(x, y, barWidth - 1, barHeight);
    });
  }

  function renderLineChart(tensor) {
    const data = Array.from(tensor.data.slice(0, maxDisplayElements));
    const minVal = Math.min(...data);
    const maxVal = Math.max(...data);
    const range = maxVal - minVal || 1;

    ctx.strokeStyle = '#2196F3';
    ctx.lineWidth = 2;
    ctx.beginPath();

    data.forEach((value, index) => {
      const x = (index / (data.length - 1)) * width;
      const y = height - ((value - minVal) / range) * height;
      
      if (index === 0) {
        ctx.moveTo(x, y);
      } else {
        ctx.lineTo(x, y);
      }
    });

    ctx.stroke();

    // Add points
    ctx.fillStyle = '#1976D2';
    data.forEach((value, index) => {
      const x = (index / (data.length - 1)) * width;
      const y = height - ((value - minVal) / range) * height;
      
      ctx.beginPath();
      ctx.arc(x, y, 3, 0, 2 * Math.PI);
      ctx.fill();
    });
  }

  function renderHistogram(tensor) {
    const data = Array.from(tensor.data);
    const bins = 20;
    const minVal = Math.min(...data);
    const maxVal = Math.max(...data);
    const binWidth = (maxVal - minVal) / bins;

    // Calculate histogram
    const histogram = new Array(bins).fill(0);
    data.forEach(value => {
      const binIndex = Math.min(Math.floor((value - minVal) / binWidth), bins - 1);
      histogram[binIndex]++;
    });

    const maxCount = Math.max(...histogram);
    const barWidth = width / bins;

    // Draw histogram bars
    histogram.forEach((count, index) => {
      const barHeight = (count / maxCount) * height;
      const x = index * barWidth;
      const y = height - barHeight;

      ctx.fillStyle = '#9C27B0';
      ctx.fillRect(x, y, barWidth - 1, barHeight);

      // Add count labels
      if (count > 0) {
        ctx.fillStyle = 'black';
        ctx.font = '10px Arial';
        ctx.textAlign = 'center';
        ctx.fillText(count.toString(), x + barWidth / 2, y - 5);
      }
    });
  }

  function renderFallbackVisualization(tensor) {
    const data = Array.from(tensor.data.slice(0, maxDisplayElements));
    renderBarChart(tensor);
    
    // Add shape info
    ctx.fillStyle = 'black';
    ctx.font = '12px Arial';
    ctx.textAlign = 'left';
    ctx.fillText(`Shape: [${tensor.shape.join(', ')}]`, 10, 20);
  }

  function getColor(normalized, scheme) {
    const colors = colorSchemes[scheme] || colorSchemes.viridis;
    const index = Math.floor(normalized * (colors.length - 1));
    const nextIndex = Math.min(index + 1, colors.length - 1);
    const t = (normalized * (colors.length - 1)) - index;

    const color1 = colors[index];
    const color2 = colors[nextIndex];

    return [
      Math.round(color1[0] + (color2[0] - color1[0]) * t),
      Math.round(color1[1] + (color2[1] - color1[1]) * t),
      Math.round(color1[2] + (color2[2] - color1[2]) * t)
    ];
  }

  function handleCanvasHover(event) {
    if (!tensorId) return;

    const rect = canvasElement.getBoundingClientRect();
    const x = event.clientX - rect.left;
    const y = event.clientY - rect.top;

    try {
      const tensor = getTensorData(tensorId);
      if (!tensor?.data || tensor.shape.length !== 2) return;

      const [rows, cols] = tensor.shape;
      const cellWidth = width / cols;
      const cellHeight = height / rows;

      const col = Math.floor(x / cellWidth);
      const row = Math.floor(y / cellHeight);

      if (row >= 0 && row < rows && col >= 0 && col < cols) {
        const index = row * cols + col;
        const value = tensor.data[index];

        hoverInfo = {
          x: col,
          y: row,
          value: value,
          index: index,
          mouseX: event.clientX,
          mouseY: event.clientY
        };
      } else {
        hoverInfo = null;
      }
    } catch (error) {
      hoverInfo = null;
    }
  }

  function handleCanvasLeave() {
    hoverInfo = null;
    isHovering = false;
  }

  // Reactive updates
  $: if (tensorId && canvasElement) {
    renderVisualization();
  }

  $: if (visualizationType || colorScheme) {
    renderVisualization();
  }
</script>

<div class="tensor-visualization">
  {#if showControls}
    <div class="controls">
      <label>
        Type:
        <select bind:value={visualizationType}>
          <option value="heatmap">Heatmap</option>
          <option value="bar">Bar Chart</option>
          <option value="line">Line Chart</option>
          <option value="histogram">Histogram</option>
        </select>
      </label>

      <label>
        Colors:
        <select bind:value={colorScheme}>
          <option value="viridis">Viridis</option>
          <option value="plasma">Plasma</option>
          <option value="coolwarm">Cool-Warm</option>
          <option value="grayscale">Grayscale</option>
        </select>
      </label>
    </div>
  {/if}

  <div class="canvas-container">
    <canvas
      bind:this={canvasElement}
      {width}
      {height}
      on:mousemove={handleCanvasHover}
      on:mouseenter={() => isHovering = true}
      on:mouseleave={handleCanvasLeave}
      role="img"
      aria-label="Tensor visualization"
    ></canvas>

    {#if hoverInfo}
      <div 
        class="tooltip" 
        style="left: {hoverInfo.mouseX + 10}px; top: {hoverInfo.mouseY - 30}px;"
      >
        [{hoverInfo.x}, {hoverInfo.y}]: {hoverInfo.value.toFixed(4)}
      </div>
    {/if}
  </div>

  {#if showStats && $tensorStats}
    <div class="stats">
      <div class="stats-grid">
        <div class="stat">
          <span class="label">Shape:</span>
          <span class="value">[{$tensorStats.shape.join(', ')}]</span>
        </div>
        <div class="stat">
          <span class="label">Min:</span>
          <span class="value">{$tensorStats.min.toFixed(4)}</span>
        </div>
        <div class="stat">
          <span class="label">Max:</span>
          <span class="value">{$tensorStats.max.toFixed(4)}</span>
        </div>
        <div class="stat">
          <span class="label">Mean:</span>
          <span class="value">{$tensorStats.mean.toFixed(4)}</span>
        </div>
        <div class="stat">
          <span class="label">Std:</span>
          <span class="value">{$tensorStats.std.toFixed(4)}</span>
        </div>
        <div class="stat">
          <span class="label">Memory:</span>
          <span class="value">{($tensorStats.memoryUsage / 1024).toFixed(1)} KB</span>
        </div>
      </div>
    </div>
  {/if}

  {#if !tensorId}
    <div class="no-tensor">
      <p>No tensor selected for visualization</p>
    </div>
  {/if}
</div>

<style>
  .tensor-visualization {
    border: 1px solid #ddd;
    border-radius: 4px;
    padding: 1rem;
    background: white;
    font-family: 'Segoe UI', Arial, sans-serif;
  }

  .controls {
    display: flex;
    gap: 1rem;
    margin-bottom: 1rem;
    flex-wrap: wrap;
  }

  .controls label {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 0.9rem;
  }

  .controls select {
    padding: 0.25rem;
    border: 1px solid #ccc;
    border-radius: 3px;
  }

  .canvas-container {
    position: relative;
    display: inline-block;
    border: 1px solid #eee;
    border-radius: 3px;
  }

  canvas {
    display: block;
    cursor: crosshair;
  }

  .tooltip {
    position: fixed;
    background: rgba(0, 0, 0, 0.8);
    color: white;
    padding: 0.25rem 0.5rem;
    border-radius: 3px;
    font-size: 0.8rem;
    font-family: monospace;
    z-index: 1000;
    pointer-events: none;
    white-space: nowrap;
  }

  .stats {
    margin-top: 1rem;
    padding-top: 1rem;
    border-top: 1px solid #eee;
  }

  .stats-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
    gap: 0.5rem;
  }

  .stat {
    display: flex;
    justify-content: space-between;
    padding: 0.25rem;
    background: #f9f9f9;
    border-radius: 3px;
  }

  .stat .label {
    font-weight: bold;
    color: #666;
  }

  .stat .value {
    font-family: monospace;
    color: #333;
  }

  .no-tensor {
    text-align: center;
    color: #999;
    padding: 2rem;
    border: 2px dashed #ddd;
    border-radius: 4px;
  }

  /* Responsive design */
  @media (max-width: 768px) {
    .controls {
      flex-direction: column;
    }
    
    .stats-grid {
      grid-template-columns: 1fr;
    }
  }

  /* Dark mode support */
  @media (prefers-color-scheme: dark) {
    .tensor-visualization {
      background: #2d2d2d;
      border-color: #555;
      color: white;
    }
    
    .stat {
      background: #3d3d3d;
    }
    
    .controls select {
      background: #3d3d3d;
      color: white;
      border-color: #555;
    }
  }
</style>