@ruvector/attention-wasm 0.1.0 → 2.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,1494 @@
1
+ let wasm;
2
+
3
+ function addHeapObject(obj) {
4
+ if (heap_next === heap.length) heap.push(heap.length + 1);
5
+ const idx = heap_next;
6
+ heap_next = heap[idx];
7
+
8
+ heap[idx] = obj;
9
+ return idx;
10
+ }
11
+
12
+ function debugString(val) {
13
+ // primitive types
14
+ const type = typeof val;
15
+ if (type == 'number' || type == 'boolean' || val == null) {
16
+ return `${val}`;
17
+ }
18
+ if (type == 'string') {
19
+ return `"${val}"`;
20
+ }
21
+ if (type == 'symbol') {
22
+ const description = val.description;
23
+ if (description == null) {
24
+ return 'Symbol';
25
+ } else {
26
+ return `Symbol(${description})`;
27
+ }
28
+ }
29
+ if (type == 'function') {
30
+ const name = val.name;
31
+ if (typeof name == 'string' && name.length > 0) {
32
+ return `Function(${name})`;
33
+ } else {
34
+ return 'Function';
35
+ }
36
+ }
37
+ // objects
38
+ if (Array.isArray(val)) {
39
+ const length = val.length;
40
+ let debug = '[';
41
+ if (length > 0) {
42
+ debug += debugString(val[0]);
43
+ }
44
+ for(let i = 1; i < length; i++) {
45
+ debug += ', ' + debugString(val[i]);
46
+ }
47
+ debug += ']';
48
+ return debug;
49
+ }
50
+ // Test for built-in
51
+ const builtInMatches = /\[object ([^\]]+)\]/.exec(toString.call(val));
52
+ let className;
53
+ if (builtInMatches && builtInMatches.length > 1) {
54
+ className = builtInMatches[1];
55
+ } else {
56
+ // Failed to match the standard '[object ClassName]'
57
+ return toString.call(val);
58
+ }
59
+ if (className == 'Object') {
60
+ // we're a user defined class or Object
61
+ // JSON.stringify avoids problems with cycles, and is generally much
62
+ // easier than looping through ownProperties of `val`.
63
+ try {
64
+ return 'Object(' + JSON.stringify(val) + ')';
65
+ } catch (_) {
66
+ return 'Object';
67
+ }
68
+ }
69
+ // errors
70
+ if (val instanceof Error) {
71
+ return `${val.name}: ${val.message}\n${val.stack}`;
72
+ }
73
+ // TODO we could test for more things here, like `Set`s and `Map`s.
74
+ return className;
75
+ }
76
+
77
+ function dropObject(idx) {
78
+ if (idx < 132) return;
79
+ heap[idx] = heap_next;
80
+ heap_next = idx;
81
+ }
82
+
83
+ function getArrayF32FromWasm0(ptr, len) {
84
+ ptr = ptr >>> 0;
85
+ return getFloat32ArrayMemory0().subarray(ptr / 4, ptr / 4 + len);
86
+ }
87
+
88
+ function getArrayU8FromWasm0(ptr, len) {
89
+ ptr = ptr >>> 0;
90
+ return getUint8ArrayMemory0().subarray(ptr / 1, ptr / 1 + len);
91
+ }
92
+
93
+ let cachedDataViewMemory0 = null;
94
+ function getDataViewMemory0() {
95
+ if (cachedDataViewMemory0 === null || cachedDataViewMemory0.buffer.detached === true || (cachedDataViewMemory0.buffer.detached === undefined && cachedDataViewMemory0.buffer !== wasm.memory.buffer)) {
96
+ cachedDataViewMemory0 = new DataView(wasm.memory.buffer);
97
+ }
98
+ return cachedDataViewMemory0;
99
+ }
100
+
101
+ let cachedFloat32ArrayMemory0 = null;
102
+ function getFloat32ArrayMemory0() {
103
+ if (cachedFloat32ArrayMemory0 === null || cachedFloat32ArrayMemory0.byteLength === 0) {
104
+ cachedFloat32ArrayMemory0 = new Float32Array(wasm.memory.buffer);
105
+ }
106
+ return cachedFloat32ArrayMemory0;
107
+ }
108
+
109
+ function getStringFromWasm0(ptr, len) {
110
+ ptr = ptr >>> 0;
111
+ return decodeText(ptr, len);
112
+ }
113
+
114
+ let cachedUint8ArrayMemory0 = null;
115
+ function getUint8ArrayMemory0() {
116
+ if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
117
+ cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
118
+ }
119
+ return cachedUint8ArrayMemory0;
120
+ }
121
+
122
+ function getObject(idx) { return heap[idx]; }
123
+
124
+ function handleError(f, args) {
125
+ try {
126
+ return f.apply(this, args);
127
+ } catch (e) {
128
+ wasm.__wbindgen_export3(addHeapObject(e));
129
+ }
130
+ }
131
+
132
+ let heap = new Array(128).fill(undefined);
133
+ heap.push(undefined, null, true, false);
134
+
135
+ let heap_next = heap.length;
136
+
137
+ function isLikeNone(x) {
138
+ return x === undefined || x === null;
139
+ }
140
+
141
+ function passArrayF32ToWasm0(arg, malloc) {
142
+ const ptr = malloc(arg.length * 4, 4) >>> 0;
143
+ getFloat32ArrayMemory0().set(arg, ptr / 4);
144
+ WASM_VECTOR_LEN = arg.length;
145
+ return ptr;
146
+ }
147
+
148
+ function passStringToWasm0(arg, malloc, realloc) {
149
+ if (realloc === undefined) {
150
+ const buf = cachedTextEncoder.encode(arg);
151
+ const ptr = malloc(buf.length, 1) >>> 0;
152
+ getUint8ArrayMemory0().subarray(ptr, ptr + buf.length).set(buf);
153
+ WASM_VECTOR_LEN = buf.length;
154
+ return ptr;
155
+ }
156
+
157
+ let len = arg.length;
158
+ let ptr = malloc(len, 1) >>> 0;
159
+
160
+ const mem = getUint8ArrayMemory0();
161
+
162
+ let offset = 0;
163
+
164
+ for (; offset < len; offset++) {
165
+ const code = arg.charCodeAt(offset);
166
+ if (code > 0x7F) break;
167
+ mem[ptr + offset] = code;
168
+ }
169
+ if (offset !== len) {
170
+ if (offset !== 0) {
171
+ arg = arg.slice(offset);
172
+ }
173
+ ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0;
174
+ const view = getUint8ArrayMemory0().subarray(ptr + offset, ptr + len);
175
+ const ret = cachedTextEncoder.encodeInto(arg, view);
176
+
177
+ offset += ret.written;
178
+ ptr = realloc(ptr, len, offset, 1) >>> 0;
179
+ }
180
+
181
+ WASM_VECTOR_LEN = offset;
182
+ return ptr;
183
+ }
184
+
185
+ function takeObject(idx) {
186
+ const ret = getObject(idx);
187
+ dropObject(idx);
188
+ return ret;
189
+ }
190
+
191
+ let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
192
+ cachedTextDecoder.decode();
193
+ const MAX_SAFARI_DECODE_BYTES = 2146435072;
194
+ let numBytesDecoded = 0;
195
+ function decodeText(ptr, len) {
196
+ numBytesDecoded += len;
197
+ if (numBytesDecoded >= MAX_SAFARI_DECODE_BYTES) {
198
+ cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
199
+ cachedTextDecoder.decode();
200
+ numBytesDecoded = len;
201
+ }
202
+ return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
203
+ }
204
+
205
+ const cachedTextEncoder = new TextEncoder();
206
+
207
+ if (!('encodeInto' in cachedTextEncoder)) {
208
+ cachedTextEncoder.encodeInto = function (arg, view) {
209
+ const buf = cachedTextEncoder.encode(arg);
210
+ view.set(buf);
211
+ return {
212
+ read: arg.length,
213
+ written: buf.length
214
+ };
215
+ }
216
+ }
217
+
218
+ let WASM_VECTOR_LEN = 0;
219
+
220
+ const WasmAdamFinalization = (typeof FinalizationRegistry === 'undefined')
221
+ ? { register: () => {}, unregister: () => {} }
222
+ : new FinalizationRegistry(ptr => wasm.__wbg_wasmadam_free(ptr >>> 0, 1));
223
+
224
+ const WasmAdamWFinalization = (typeof FinalizationRegistry === 'undefined')
225
+ ? { register: () => {}, unregister: () => {} }
226
+ : new FinalizationRegistry(ptr => wasm.__wbg_wasmadamw_free(ptr >>> 0, 1));
227
+
228
+ const WasmFlashAttentionFinalization = (typeof FinalizationRegistry === 'undefined')
229
+ ? { register: () => {}, unregister: () => {} }
230
+ : new FinalizationRegistry(ptr => wasm.__wbg_wasmflashattention_free(ptr >>> 0, 1));
231
+
232
+ const WasmHyperbolicAttentionFinalization = (typeof FinalizationRegistry === 'undefined')
233
+ ? { register: () => {}, unregister: () => {} }
234
+ : new FinalizationRegistry(ptr => wasm.__wbg_wasmhyperbolicattention_free(ptr >>> 0, 1));
235
+
236
+ const WasmInfoNCELossFinalization = (typeof FinalizationRegistry === 'undefined')
237
+ ? { register: () => {}, unregister: () => {} }
238
+ : new FinalizationRegistry(ptr => wasm.__wbg_wasminfonceloss_free(ptr >>> 0, 1));
239
+
240
+ const WasmLRSchedulerFinalization = (typeof FinalizationRegistry === 'undefined')
241
+ ? { register: () => {}, unregister: () => {} }
242
+ : new FinalizationRegistry(ptr => wasm.__wbg_wasmlrscheduler_free(ptr >>> 0, 1));
243
+
244
+ const WasmLinearAttentionFinalization = (typeof FinalizationRegistry === 'undefined')
245
+ ? { register: () => {}, unregister: () => {} }
246
+ : new FinalizationRegistry(ptr => wasm.__wbg_wasmlinearattention_free(ptr >>> 0, 1));
247
+
248
+ const WasmLocalGlobalAttentionFinalization = (typeof FinalizationRegistry === 'undefined')
249
+ ? { register: () => {}, unregister: () => {} }
250
+ : new FinalizationRegistry(ptr => wasm.__wbg_wasmlocalglobalattention_free(ptr >>> 0, 1));
251
+
252
+ const WasmMoEAttentionFinalization = (typeof FinalizationRegistry === 'undefined')
253
+ ? { register: () => {}, unregister: () => {} }
254
+ : new FinalizationRegistry(ptr => wasm.__wbg_wasmmoeattention_free(ptr >>> 0, 1));
255
+
256
+ const WasmMultiHeadAttentionFinalization = (typeof FinalizationRegistry === 'undefined')
257
+ ? { register: () => {}, unregister: () => {} }
258
+ : new FinalizationRegistry(ptr => wasm.__wbg_wasmmultiheadattention_free(ptr >>> 0, 1));
259
+
260
+ const WasmSGDFinalization = (typeof FinalizationRegistry === 'undefined')
261
+ ? { register: () => {}, unregister: () => {} }
262
+ : new FinalizationRegistry(ptr => wasm.__wbg_wasmsgd_free(ptr >>> 0, 1));
263
+
264
+ /**
265
+ * Adam optimizer
266
+ */
267
+ export class WasmAdam {
268
+ __destroy_into_raw() {
269
+ const ptr = this.__wbg_ptr;
270
+ this.__wbg_ptr = 0;
271
+ WasmAdamFinalization.unregister(this);
272
+ return ptr;
273
+ }
274
+ free() {
275
+ const ptr = this.__destroy_into_raw();
276
+ wasm.__wbg_wasmadam_free(ptr, 0);
277
+ }
278
+ /**
279
+ * Get current learning rate
280
+ * @returns {number}
281
+ */
282
+ get learning_rate() {
283
+ const ret = wasm.wasmadam_learning_rate(this.__wbg_ptr);
284
+ return ret;
285
+ }
286
+ /**
287
+ * Set learning rate
288
+ * @param {number} lr
289
+ */
290
+ set learning_rate(lr) {
291
+ wasm.wasmadam_set_learning_rate(this.__wbg_ptr, lr);
292
+ }
293
+ /**
294
+ * Create a new Adam optimizer
295
+ *
296
+ * # Arguments
297
+ * * `param_count` - Number of parameters
298
+ * * `learning_rate` - Learning rate
299
+ * @param {number} param_count
300
+ * @param {number} learning_rate
301
+ */
302
+ constructor(param_count, learning_rate) {
303
+ const ret = wasm.wasmadam_new(param_count, learning_rate);
304
+ this.__wbg_ptr = ret >>> 0;
305
+ WasmAdamFinalization.register(this, this.__wbg_ptr, this);
306
+ return this;
307
+ }
308
+ /**
309
+ * Perform optimization step
310
+ *
311
+ * # Arguments
312
+ * * `params` - Current parameter values (will be updated in-place)
313
+ * * `gradients` - Gradient values
314
+ * @param {Float32Array} params
315
+ * @param {Float32Array} gradients
316
+ */
317
+ step(params, gradients) {
318
+ var ptr0 = passArrayF32ToWasm0(params, wasm.__wbindgen_export);
319
+ var len0 = WASM_VECTOR_LEN;
320
+ const ptr1 = passArrayF32ToWasm0(gradients, wasm.__wbindgen_export);
321
+ const len1 = WASM_VECTOR_LEN;
322
+ wasm.wasmadam_step(this.__wbg_ptr, ptr0, len0, addHeapObject(params), ptr1, len1);
323
+ }
324
+ /**
325
+ * Reset optimizer state
326
+ */
327
+ reset() {
328
+ wasm.wasmadam_reset(this.__wbg_ptr);
329
+ }
330
+ }
331
+ if (Symbol.dispose) WasmAdam.prototype[Symbol.dispose] = WasmAdam.prototype.free;
332
+
333
+ /**
334
+ * AdamW optimizer (Adam with decoupled weight decay)
335
+ */
336
+ export class WasmAdamW {
337
+ __destroy_into_raw() {
338
+ const ptr = this.__wbg_ptr;
339
+ this.__wbg_ptr = 0;
340
+ WasmAdamWFinalization.unregister(this);
341
+ return ptr;
342
+ }
343
+ free() {
344
+ const ptr = this.__destroy_into_raw();
345
+ wasm.__wbg_wasmadamw_free(ptr, 0);
346
+ }
347
+ /**
348
+ * Get weight decay
349
+ * @returns {number}
350
+ */
351
+ get weight_decay() {
352
+ const ret = wasm.wasmadamw_weight_decay(this.__wbg_ptr);
353
+ return ret;
354
+ }
355
+ /**
356
+ * Get current learning rate
357
+ * @returns {number}
358
+ */
359
+ get learning_rate() {
360
+ const ret = wasm.wasmadam_learning_rate(this.__wbg_ptr);
361
+ return ret;
362
+ }
363
+ /**
364
+ * Set learning rate
365
+ * @param {number} lr
366
+ */
367
+ set learning_rate(lr) {
368
+ wasm.wasmadam_set_learning_rate(this.__wbg_ptr, lr);
369
+ }
370
+ /**
371
+ * Create a new AdamW optimizer
372
+ *
373
+ * # Arguments
374
+ * * `param_count` - Number of parameters
375
+ * * `learning_rate` - Learning rate
376
+ * * `weight_decay` - Weight decay coefficient
377
+ * @param {number} param_count
378
+ * @param {number} learning_rate
379
+ * @param {number} weight_decay
380
+ */
381
+ constructor(param_count, learning_rate, weight_decay) {
382
+ const ret = wasm.wasmadamw_new(param_count, learning_rate, weight_decay);
383
+ this.__wbg_ptr = ret >>> 0;
384
+ WasmAdamWFinalization.register(this, this.__wbg_ptr, this);
385
+ return this;
386
+ }
387
+ /**
388
+ * Perform optimization step with weight decay
389
+ * @param {Float32Array} params
390
+ * @param {Float32Array} gradients
391
+ */
392
+ step(params, gradients) {
393
+ var ptr0 = passArrayF32ToWasm0(params, wasm.__wbindgen_export);
394
+ var len0 = WASM_VECTOR_LEN;
395
+ const ptr1 = passArrayF32ToWasm0(gradients, wasm.__wbindgen_export);
396
+ const len1 = WASM_VECTOR_LEN;
397
+ wasm.wasmadamw_step(this.__wbg_ptr, ptr0, len0, addHeapObject(params), ptr1, len1);
398
+ }
399
+ /**
400
+ * Reset optimizer state
401
+ */
402
+ reset() {
403
+ wasm.wasmadamw_reset(this.__wbg_ptr);
404
+ }
405
+ }
406
+ if (Symbol.dispose) WasmAdamW.prototype[Symbol.dispose] = WasmAdamW.prototype.free;
407
+
408
+ /**
409
+ * Flash attention mechanism
410
+ */
411
+ export class WasmFlashAttention {
412
+ __destroy_into_raw() {
413
+ const ptr = this.__wbg_ptr;
414
+ this.__wbg_ptr = 0;
415
+ WasmFlashAttentionFinalization.unregister(this);
416
+ return ptr;
417
+ }
418
+ free() {
419
+ const ptr = this.__destroy_into_raw();
420
+ wasm.__wbg_wasmflashattention_free(ptr, 0);
421
+ }
422
+ /**
423
+ * Create a new flash attention instance
424
+ *
425
+ * # Arguments
426
+ * * `dim` - Embedding dimension
427
+ * * `block_size` - Block size for tiling
428
+ * @param {number} dim
429
+ * @param {number} block_size
430
+ */
431
+ constructor(dim, block_size) {
432
+ const ret = wasm.wasmflashattention_new(dim, block_size);
433
+ this.__wbg_ptr = ret >>> 0;
434
+ WasmFlashAttentionFinalization.register(this, this.__wbg_ptr, this);
435
+ return this;
436
+ }
437
+ /**
438
+ * Compute flash attention
439
+ * @param {Float32Array} query
440
+ * @param {any} keys
441
+ * @param {any} values
442
+ * @returns {Float32Array}
443
+ */
444
+ compute(query, keys, values) {
445
+ try {
446
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
447
+ const ptr0 = passArrayF32ToWasm0(query, wasm.__wbindgen_export);
448
+ const len0 = WASM_VECTOR_LEN;
449
+ wasm.wasmflashattention_compute(retptr, this.__wbg_ptr, ptr0, len0, addHeapObject(keys), addHeapObject(values));
450
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
451
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
452
+ var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
453
+ var r3 = getDataViewMemory0().getInt32(retptr + 4 * 3, true);
454
+ if (r3) {
455
+ throw takeObject(r2);
456
+ }
457
+ var v2 = getArrayF32FromWasm0(r0, r1).slice();
458
+ wasm.__wbindgen_export4(r0, r1 * 4, 4);
459
+ return v2;
460
+ } finally {
461
+ wasm.__wbindgen_add_to_stack_pointer(16);
462
+ }
463
+ }
464
+ }
465
+ if (Symbol.dispose) WasmFlashAttention.prototype[Symbol.dispose] = WasmFlashAttention.prototype.free;
466
+
467
+ /**
468
+ * Hyperbolic attention mechanism
469
+ */
470
+ export class WasmHyperbolicAttention {
471
+ __destroy_into_raw() {
472
+ const ptr = this.__wbg_ptr;
473
+ this.__wbg_ptr = 0;
474
+ WasmHyperbolicAttentionFinalization.unregister(this);
475
+ return ptr;
476
+ }
477
+ free() {
478
+ const ptr = this.__destroy_into_raw();
479
+ wasm.__wbg_wasmhyperbolicattention_free(ptr, 0);
480
+ }
481
+ /**
482
+ * Create a new hyperbolic attention instance
483
+ *
484
+ * # Arguments
485
+ * * `dim` - Embedding dimension
486
+ * * `curvature` - Hyperbolic curvature parameter
487
+ * @param {number} dim
488
+ * @param {number} curvature
489
+ */
490
+ constructor(dim, curvature) {
491
+ const ret = wasm.wasmhyperbolicattention_new(dim, curvature);
492
+ this.__wbg_ptr = ret >>> 0;
493
+ WasmHyperbolicAttentionFinalization.register(this, this.__wbg_ptr, this);
494
+ return this;
495
+ }
496
+ /**
497
+ * Compute hyperbolic attention
498
+ * @param {Float32Array} query
499
+ * @param {any} keys
500
+ * @param {any} values
501
+ * @returns {Float32Array}
502
+ */
503
+ compute(query, keys, values) {
504
+ try {
505
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
506
+ const ptr0 = passArrayF32ToWasm0(query, wasm.__wbindgen_export);
507
+ const len0 = WASM_VECTOR_LEN;
508
+ wasm.wasmhyperbolicattention_compute(retptr, this.__wbg_ptr, ptr0, len0, addHeapObject(keys), addHeapObject(values));
509
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
510
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
511
+ var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
512
+ var r3 = getDataViewMemory0().getInt32(retptr + 4 * 3, true);
513
+ if (r3) {
514
+ throw takeObject(r2);
515
+ }
516
+ var v2 = getArrayF32FromWasm0(r0, r1).slice();
517
+ wasm.__wbindgen_export4(r0, r1 * 4, 4);
518
+ return v2;
519
+ } finally {
520
+ wasm.__wbindgen_add_to_stack_pointer(16);
521
+ }
522
+ }
523
+ /**
524
+ * Get the curvature
525
+ * @returns {number}
526
+ */
527
+ get curvature() {
528
+ const ret = wasm.wasmhyperbolicattention_curvature(this.__wbg_ptr);
529
+ return ret;
530
+ }
531
+ }
532
+ if (Symbol.dispose) WasmHyperbolicAttention.prototype[Symbol.dispose] = WasmHyperbolicAttention.prototype.free;
533
+
534
+ /**
535
+ * InfoNCE contrastive loss for training
536
+ */
537
+ export class WasmInfoNCELoss {
538
+ __destroy_into_raw() {
539
+ const ptr = this.__wbg_ptr;
540
+ this.__wbg_ptr = 0;
541
+ WasmInfoNCELossFinalization.unregister(this);
542
+ return ptr;
543
+ }
544
+ free() {
545
+ const ptr = this.__destroy_into_raw();
546
+ wasm.__wbg_wasminfonceloss_free(ptr, 0);
547
+ }
548
+ /**
549
+ * Create a new InfoNCE loss instance
550
+ *
551
+ * # Arguments
552
+ * * `temperature` - Temperature parameter for softmax
553
+ * @param {number} temperature
554
+ */
555
+ constructor(temperature) {
556
+ const ret = wasm.wasminfonceloss_new(temperature);
557
+ this.__wbg_ptr = ret >>> 0;
558
+ WasmInfoNCELossFinalization.register(this, this.__wbg_ptr, this);
559
+ return this;
560
+ }
561
+ /**
562
+ * Compute InfoNCE loss
563
+ *
564
+ * # Arguments
565
+ * * `anchor` - Anchor embedding
566
+ * * `positive` - Positive example embedding
567
+ * * `negatives` - Array of negative example embeddings
568
+ * @param {Float32Array} anchor
569
+ * @param {Float32Array} positive
570
+ * @param {any} negatives
571
+ * @returns {number}
572
+ */
573
+ compute(anchor, positive, negatives) {
574
+ try {
575
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
576
+ const ptr0 = passArrayF32ToWasm0(anchor, wasm.__wbindgen_export);
577
+ const len0 = WASM_VECTOR_LEN;
578
+ const ptr1 = passArrayF32ToWasm0(positive, wasm.__wbindgen_export);
579
+ const len1 = WASM_VECTOR_LEN;
580
+ wasm.wasminfonceloss_compute(retptr, this.__wbg_ptr, ptr0, len0, ptr1, len1, addHeapObject(negatives));
581
+ var r0 = getDataViewMemory0().getFloat32(retptr + 4 * 0, true);
582
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
583
+ var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
584
+ if (r2) {
585
+ throw takeObject(r1);
586
+ }
587
+ return r0;
588
+ } finally {
589
+ wasm.__wbindgen_add_to_stack_pointer(16);
590
+ }
591
+ }
592
+ }
593
+ if (Symbol.dispose) WasmInfoNCELoss.prototype[Symbol.dispose] = WasmInfoNCELoss.prototype.free;
594
+
595
+ /**
596
+ * Learning rate scheduler
597
+ */
598
+ export class WasmLRScheduler {
599
+ __destroy_into_raw() {
600
+ const ptr = this.__wbg_ptr;
601
+ this.__wbg_ptr = 0;
602
+ WasmLRSchedulerFinalization.unregister(this);
603
+ return ptr;
604
+ }
605
+ free() {
606
+ const ptr = this.__destroy_into_raw();
607
+ wasm.__wbg_wasmlrscheduler_free(ptr, 0);
608
+ }
609
+ /**
610
+ * Create a new learning rate scheduler with warmup and cosine decay
611
+ *
612
+ * # Arguments
613
+ * * `initial_lr` - Initial learning rate
614
+ * * `warmup_steps` - Number of warmup steps
615
+ * * `total_steps` - Total training steps
616
+ * @param {number} initial_lr
617
+ * @param {number} warmup_steps
618
+ * @param {number} total_steps
619
+ */
620
+ constructor(initial_lr, warmup_steps, total_steps) {
621
+ const ret = wasm.wasmlrscheduler_new(initial_lr, warmup_steps, total_steps);
622
+ this.__wbg_ptr = ret >>> 0;
623
+ WasmLRSchedulerFinalization.register(this, this.__wbg_ptr, this);
624
+ return this;
625
+ }
626
+ /**
627
+ * Advance to next step
628
+ */
629
+ step() {
630
+ wasm.wasmlrscheduler_step(this.__wbg_ptr);
631
+ }
632
+ /**
633
+ * Reset scheduler
634
+ */
635
+ reset() {
636
+ wasm.wasmlrscheduler_reset(this.__wbg_ptr);
637
+ }
638
+ /**
639
+ * Get learning rate for current step
640
+ * @returns {number}
641
+ */
642
+ get_lr() {
643
+ const ret = wasm.wasmlrscheduler_get_lr(this.__wbg_ptr);
644
+ return ret;
645
+ }
646
+ }
647
+ if (Symbol.dispose) WasmLRScheduler.prototype[Symbol.dispose] = WasmLRScheduler.prototype.free;
648
+
649
+ /**
650
+ * Linear attention (Performer-style)
651
+ */
652
+ export class WasmLinearAttention {
653
+ __destroy_into_raw() {
654
+ const ptr = this.__wbg_ptr;
655
+ this.__wbg_ptr = 0;
656
+ WasmLinearAttentionFinalization.unregister(this);
657
+ return ptr;
658
+ }
659
+ free() {
660
+ const ptr = this.__destroy_into_raw();
661
+ wasm.__wbg_wasmlinearattention_free(ptr, 0);
662
+ }
663
+ /**
664
+ * Create a new linear attention instance
665
+ *
666
+ * # Arguments
667
+ * * `dim` - Embedding dimension
668
+ * * `num_features` - Number of random features
669
+ * @param {number} dim
670
+ * @param {number} num_features
671
+ */
672
+ constructor(dim, num_features) {
673
+ const ret = wasm.wasmlinearattention_new(dim, num_features);
674
+ this.__wbg_ptr = ret >>> 0;
675
+ WasmLinearAttentionFinalization.register(this, this.__wbg_ptr, this);
676
+ return this;
677
+ }
678
+ /**
679
+ * Compute linear attention
680
+ * @param {Float32Array} query
681
+ * @param {any} keys
682
+ * @param {any} values
683
+ * @returns {Float32Array}
684
+ */
685
+ compute(query, keys, values) {
686
+ try {
687
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
688
+ const ptr0 = passArrayF32ToWasm0(query, wasm.__wbindgen_export);
689
+ const len0 = WASM_VECTOR_LEN;
690
+ wasm.wasmlinearattention_compute(retptr, this.__wbg_ptr, ptr0, len0, addHeapObject(keys), addHeapObject(values));
691
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
692
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
693
+ var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
694
+ var r3 = getDataViewMemory0().getInt32(retptr + 4 * 3, true);
695
+ if (r3) {
696
+ throw takeObject(r2);
697
+ }
698
+ var v2 = getArrayF32FromWasm0(r0, r1).slice();
699
+ wasm.__wbindgen_export4(r0, r1 * 4, 4);
700
+ return v2;
701
+ } finally {
702
+ wasm.__wbindgen_add_to_stack_pointer(16);
703
+ }
704
+ }
705
+ }
706
+ if (Symbol.dispose) WasmLinearAttention.prototype[Symbol.dispose] = WasmLinearAttention.prototype.free;
707
+
708
+ /**
709
+ * Local-global attention mechanism
710
+ */
711
+ export class WasmLocalGlobalAttention {
712
+ __destroy_into_raw() {
713
+ const ptr = this.__wbg_ptr;
714
+ this.__wbg_ptr = 0;
715
+ WasmLocalGlobalAttentionFinalization.unregister(this);
716
+ return ptr;
717
+ }
718
+ free() {
719
+ const ptr = this.__destroy_into_raw();
720
+ wasm.__wbg_wasmlocalglobalattention_free(ptr, 0);
721
+ }
722
+ /**
723
+ * Create a new local-global attention instance
724
+ *
725
+ * # Arguments
726
+ * * `dim` - Embedding dimension
727
+ * * `local_window` - Size of local attention window
728
+ * * `global_tokens` - Number of global attention tokens
729
+ * @param {number} dim
730
+ * @param {number} local_window
731
+ * @param {number} global_tokens
732
+ */
733
+ constructor(dim, local_window, global_tokens) {
734
+ const ret = wasm.wasmlocalglobalattention_new(dim, local_window, global_tokens);
735
+ this.__wbg_ptr = ret >>> 0;
736
+ WasmLocalGlobalAttentionFinalization.register(this, this.__wbg_ptr, this);
737
+ return this;
738
+ }
739
+ /**
740
+ * Compute local-global attention
741
+ * @param {Float32Array} query
742
+ * @param {any} keys
743
+ * @param {any} values
744
+ * @returns {Float32Array}
745
+ */
746
+ compute(query, keys, values) {
747
+ try {
748
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
749
+ const ptr0 = passArrayF32ToWasm0(query, wasm.__wbindgen_export);
750
+ const len0 = WASM_VECTOR_LEN;
751
+ wasm.wasmlocalglobalattention_compute(retptr, this.__wbg_ptr, ptr0, len0, addHeapObject(keys), addHeapObject(values));
752
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
753
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
754
+ var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
755
+ var r3 = getDataViewMemory0().getInt32(retptr + 4 * 3, true);
756
+ if (r3) {
757
+ throw takeObject(r2);
758
+ }
759
+ var v2 = getArrayF32FromWasm0(r0, r1).slice();
760
+ wasm.__wbindgen_export4(r0, r1 * 4, 4);
761
+ return v2;
762
+ } finally {
763
+ wasm.__wbindgen_add_to_stack_pointer(16);
764
+ }
765
+ }
766
+ }
767
+ if (Symbol.dispose) WasmLocalGlobalAttention.prototype[Symbol.dispose] = WasmLocalGlobalAttention.prototype.free;
768
+
769
+ /**
770
+ * Mixture of Experts (MoE) attention
771
+ */
772
+ export class WasmMoEAttention {
773
+ __destroy_into_raw() {
774
+ const ptr = this.__wbg_ptr;
775
+ this.__wbg_ptr = 0;
776
+ WasmMoEAttentionFinalization.unregister(this);
777
+ return ptr;
778
+ }
779
+ free() {
780
+ const ptr = this.__destroy_into_raw();
781
+ wasm.__wbg_wasmmoeattention_free(ptr, 0);
782
+ }
783
+ /**
784
+ * Create a new MoE attention instance
785
+ *
786
+ * # Arguments
787
+ * * `dim` - Embedding dimension
788
+ * * `num_experts` - Number of expert attention mechanisms
789
+ * * `top_k` - Number of experts to use per query
790
+ * @param {number} dim
791
+ * @param {number} num_experts
792
+ * @param {number} top_k
793
+ */
794
+ constructor(dim, num_experts, top_k) {
795
+ const ret = wasm.wasmmoeattention_new(dim, num_experts, top_k);
796
+ this.__wbg_ptr = ret >>> 0;
797
+ WasmMoEAttentionFinalization.register(this, this.__wbg_ptr, this);
798
+ return this;
799
+ }
800
+ /**
801
+ * Compute MoE attention
802
+ * @param {Float32Array} query
803
+ * @param {any} keys
804
+ * @param {any} values
805
+ * @returns {Float32Array}
806
+ */
807
+ compute(query, keys, values) {
808
+ try {
809
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
810
+ const ptr0 = passArrayF32ToWasm0(query, wasm.__wbindgen_export);
811
+ const len0 = WASM_VECTOR_LEN;
812
+ wasm.wasmmoeattention_compute(retptr, this.__wbg_ptr, ptr0, len0, addHeapObject(keys), addHeapObject(values));
813
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
814
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
815
+ var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
816
+ var r3 = getDataViewMemory0().getInt32(retptr + 4 * 3, true);
817
+ if (r3) {
818
+ throw takeObject(r2);
819
+ }
820
+ var v2 = getArrayF32FromWasm0(r0, r1).slice();
821
+ wasm.__wbindgen_export4(r0, r1 * 4, 4);
822
+ return v2;
823
+ } finally {
824
+ wasm.__wbindgen_add_to_stack_pointer(16);
825
+ }
826
+ }
827
+ }
828
+ if (Symbol.dispose) WasmMoEAttention.prototype[Symbol.dispose] = WasmMoEAttention.prototype.free;
829
+
830
+ /**
831
+ * Multi-head attention mechanism
832
+ */
833
+ export class WasmMultiHeadAttention {
834
+ __destroy_into_raw() {
835
+ const ptr = this.__wbg_ptr;
836
+ this.__wbg_ptr = 0;
837
+ WasmMultiHeadAttentionFinalization.unregister(this);
838
+ return ptr;
839
+ }
840
+ free() {
841
+ const ptr = this.__destroy_into_raw();
842
+ wasm.__wbg_wasmmultiheadattention_free(ptr, 0);
843
+ }
844
+ /**
845
+ * Get the dimension
846
+ * @returns {number}
847
+ */
848
+ get dim() {
849
+ const ret = wasm.wasmmultiheadattention_dim(this.__wbg_ptr);
850
+ return ret >>> 0;
851
+ }
852
+ /**
853
+ * Create a new multi-head attention instance
854
+ *
855
+ * # Arguments
856
+ * * `dim` - Embedding dimension
857
+ * * `num_heads` - Number of attention heads
858
+ * @param {number} dim
859
+ * @param {number} num_heads
860
+ */
861
+ constructor(dim, num_heads) {
862
+ try {
863
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
864
+ wasm.wasmmultiheadattention_new(retptr, dim, num_heads);
865
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
866
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
867
+ var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
868
+ if (r2) {
869
+ throw takeObject(r1);
870
+ }
871
+ this.__wbg_ptr = r0 >>> 0;
872
+ WasmMultiHeadAttentionFinalization.register(this, this.__wbg_ptr, this);
873
+ return this;
874
+ } finally {
875
+ wasm.__wbindgen_add_to_stack_pointer(16);
876
+ }
877
+ }
878
+ /**
879
+ * Compute multi-head attention
880
+ * @param {Float32Array} query
881
+ * @param {any} keys
882
+ * @param {any} values
883
+ * @returns {Float32Array}
884
+ */
885
+ compute(query, keys, values) {
886
+ try {
887
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
888
+ const ptr0 = passArrayF32ToWasm0(query, wasm.__wbindgen_export);
889
+ const len0 = WASM_VECTOR_LEN;
890
+ wasm.wasmmultiheadattention_compute(retptr, this.__wbg_ptr, ptr0, len0, addHeapObject(keys), addHeapObject(values));
891
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
892
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
893
+ var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
894
+ var r3 = getDataViewMemory0().getInt32(retptr + 4 * 3, true);
895
+ if (r3) {
896
+ throw takeObject(r2);
897
+ }
898
+ var v2 = getArrayF32FromWasm0(r0, r1).slice();
899
+ wasm.__wbindgen_export4(r0, r1 * 4, 4);
900
+ return v2;
901
+ } finally {
902
+ wasm.__wbindgen_add_to_stack_pointer(16);
903
+ }
904
+ }
905
+ /**
906
+ * Get the number of heads
907
+ * @returns {number}
908
+ */
909
+ get num_heads() {
910
+ const ret = wasm.wasmmultiheadattention_num_heads(this.__wbg_ptr);
911
+ return ret >>> 0;
912
+ }
913
+ }
914
+ if (Symbol.dispose) WasmMultiHeadAttention.prototype[Symbol.dispose] = WasmMultiHeadAttention.prototype.free;
915
+
916
+ /**
917
+ * SGD optimizer with momentum
918
+ */
919
+ export class WasmSGD {
920
+ __destroy_into_raw() {
921
+ const ptr = this.__wbg_ptr;
922
+ this.__wbg_ptr = 0;
923
+ WasmSGDFinalization.unregister(this);
924
+ return ptr;
925
+ }
926
+ free() {
927
+ const ptr = this.__destroy_into_raw();
928
+ wasm.__wbg_wasmsgd_free(ptr, 0);
929
+ }
930
+ /**
931
+ * Get current learning rate
932
+ * @returns {number}
933
+ */
934
+ get learning_rate() {
935
+ const ret = wasm.wasmsgd_learning_rate(this.__wbg_ptr);
936
+ return ret;
937
+ }
938
+ /**
939
+ * Set learning rate
940
+ * @param {number} lr
941
+ */
942
+ set learning_rate(lr) {
943
+ wasm.wasmsgd_set_learning_rate(this.__wbg_ptr, lr);
944
+ }
945
+ /**
946
+ * Create a new SGD optimizer
947
+ *
948
+ * # Arguments
949
+ * * `param_count` - Number of parameters
950
+ * * `learning_rate` - Learning rate
951
+ * * `momentum` - Momentum coefficient (default: 0)
952
+ * @param {number} param_count
953
+ * @param {number} learning_rate
954
+ * @param {number | null} [momentum]
955
+ */
956
+ constructor(param_count, learning_rate, momentum) {
957
+ const ret = wasm.wasmsgd_new(param_count, learning_rate, isLikeNone(momentum) ? 0x100000001 : Math.fround(momentum));
958
+ this.__wbg_ptr = ret >>> 0;
959
+ WasmSGDFinalization.register(this, this.__wbg_ptr, this);
960
+ return this;
961
+ }
962
+ /**
963
+ * Perform optimization step
964
+ * @param {Float32Array} params
965
+ * @param {Float32Array} gradients
966
+ */
967
+ step(params, gradients) {
968
+ var ptr0 = passArrayF32ToWasm0(params, wasm.__wbindgen_export);
969
+ var len0 = WASM_VECTOR_LEN;
970
+ const ptr1 = passArrayF32ToWasm0(gradients, wasm.__wbindgen_export);
971
+ const len1 = WASM_VECTOR_LEN;
972
+ wasm.wasmsgd_step(this.__wbg_ptr, ptr0, len0, addHeapObject(params), ptr1, len1);
973
+ }
974
+ /**
975
+ * Reset optimizer state
976
+ */
977
+ reset() {
978
+ wasm.wasmsgd_reset(this.__wbg_ptr);
979
+ }
980
+ }
981
+ if (Symbol.dispose) WasmSGD.prototype[Symbol.dispose] = WasmSGD.prototype.free;
982
+
983
+ /**
984
+ * Compute attention weights from scores
985
+ * @param {Float32Array} scores
986
+ * @param {number | null} [temperature]
987
+ */
988
+ export function attention_weights(scores, temperature) {
989
+ var ptr0 = passArrayF32ToWasm0(scores, wasm.__wbindgen_export);
990
+ var len0 = WASM_VECTOR_LEN;
991
+ wasm.attention_weights(ptr0, len0, addHeapObject(scores), isLikeNone(temperature) ? 0x100000001 : Math.fround(temperature));
992
+ }
993
+
994
+ /**
995
+ * Get information about available attention mechanisms
996
+ * @returns {any}
997
+ */
998
+ export function available_mechanisms() {
999
+ const ret = wasm.available_mechanisms();
1000
+ return takeObject(ret);
1001
+ }
1002
+
1003
+ /**
1004
+ * Batch normalize vectors
1005
+ * @param {any} vectors
1006
+ * @param {number | null} [epsilon]
1007
+ * @returns {Float32Array}
1008
+ */
1009
+ export function batch_normalize(vectors, epsilon) {
1010
+ try {
1011
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
1012
+ wasm.batch_normalize(retptr, addHeapObject(vectors), isLikeNone(epsilon) ? 0x100000001 : Math.fround(epsilon));
1013
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
1014
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
1015
+ var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
1016
+ var r3 = getDataViewMemory0().getInt32(retptr + 4 * 3, true);
1017
+ if (r3) {
1018
+ throw takeObject(r2);
1019
+ }
1020
+ var v1 = getArrayF32FromWasm0(r0, r1).slice();
1021
+ wasm.__wbindgen_export4(r0, r1 * 4, 4);
1022
+ return v1;
1023
+ } finally {
1024
+ wasm.__wbindgen_add_to_stack_pointer(16);
1025
+ }
1026
+ }
1027
+
1028
+ /**
1029
+ * Compute cosine similarity between two vectors
1030
+ * @param {Float32Array} a
1031
+ * @param {Float32Array} b
1032
+ * @returns {number}
1033
+ */
1034
+ export function cosine_similarity(a, b) {
1035
+ try {
1036
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
1037
+ const ptr0 = passArrayF32ToWasm0(a, wasm.__wbindgen_export);
1038
+ const len0 = WASM_VECTOR_LEN;
1039
+ const ptr1 = passArrayF32ToWasm0(b, wasm.__wbindgen_export);
1040
+ const len1 = WASM_VECTOR_LEN;
1041
+ wasm.cosine_similarity(retptr, ptr0, len0, ptr1, len1);
1042
+ var r0 = getDataViewMemory0().getFloat32(retptr + 4 * 0, true);
1043
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
1044
+ var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
1045
+ if (r2) {
1046
+ throw takeObject(r1);
1047
+ }
1048
+ return r0;
1049
+ } finally {
1050
+ wasm.__wbindgen_add_to_stack_pointer(16);
1051
+ }
1052
+ }
1053
+
1054
+ /**
1055
+ * Initialize the WASM module with panic hook
1056
+ */
1057
+ export function init() {
1058
+ wasm.init();
1059
+ }
1060
+
1061
+ /**
1062
+ * Compute L2 norm of a vector
1063
+ * @param {Float32Array} vec
1064
+ * @returns {number}
1065
+ */
1066
+ export function l2_norm(vec) {
1067
+ const ptr0 = passArrayF32ToWasm0(vec, wasm.__wbindgen_export);
1068
+ const len0 = WASM_VECTOR_LEN;
1069
+ const ret = wasm.l2_norm(ptr0, len0);
1070
+ return ret;
1071
+ }
1072
+
1073
+ /**
1074
+ * Log a message to the browser console
1075
+ * @param {string} message
1076
+ */
1077
+ export function log(message) {
1078
+ const ptr0 = passStringToWasm0(message, wasm.__wbindgen_export, wasm.__wbindgen_export2);
1079
+ const len0 = WASM_VECTOR_LEN;
1080
+ wasm.log(ptr0, len0);
1081
+ }
1082
+
1083
+ /**
1084
+ * Log an error to the browser console
1085
+ * @param {string} message
1086
+ */
1087
+ export function log_error(message) {
1088
+ const ptr0 = passStringToWasm0(message, wasm.__wbindgen_export, wasm.__wbindgen_export2);
1089
+ const len0 = WASM_VECTOR_LEN;
1090
+ wasm.log_error(ptr0, len0);
1091
+ }
1092
+
1093
+ /**
1094
+ * Normalize a vector to unit length
1095
+ * @param {Float32Array} vec
1096
+ */
1097
+ export function normalize(vec) {
1098
+ try {
1099
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
1100
+ var ptr0 = passArrayF32ToWasm0(vec, wasm.__wbindgen_export);
1101
+ var len0 = WASM_VECTOR_LEN;
1102
+ wasm.normalize(retptr, ptr0, len0, addHeapObject(vec));
1103
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
1104
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
1105
+ if (r1) {
1106
+ throw takeObject(r0);
1107
+ }
1108
+ } finally {
1109
+ wasm.__wbindgen_add_to_stack_pointer(16);
1110
+ }
1111
+ }
1112
+
1113
+ /**
1114
+ * Compute pairwise distances between vectors
1115
+ * @param {any} vectors
1116
+ * @returns {Float32Array}
1117
+ */
1118
+ export function pairwise_distances(vectors) {
1119
+ try {
1120
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
1121
+ wasm.pairwise_distances(retptr, addHeapObject(vectors));
1122
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
1123
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
1124
+ var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
1125
+ var r3 = getDataViewMemory0().getInt32(retptr + 4 * 3, true);
1126
+ if (r3) {
1127
+ throw takeObject(r2);
1128
+ }
1129
+ var v1 = getArrayF32FromWasm0(r0, r1).slice();
1130
+ wasm.__wbindgen_export4(r0, r1 * 4, 4);
1131
+ return v1;
1132
+ } finally {
1133
+ wasm.__wbindgen_add_to_stack_pointer(16);
1134
+ }
1135
+ }
1136
+
1137
+ /**
1138
+ * Generate random orthogonal matrix (for initialization)
1139
+ * @param {number} dim
1140
+ * @returns {Float32Array}
1141
+ */
1142
+ export function random_orthogonal_matrix(dim) {
1143
+ try {
1144
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
1145
+ wasm.random_orthogonal_matrix(retptr, dim);
1146
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
1147
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
1148
+ var v1 = getArrayF32FromWasm0(r0, r1).slice();
1149
+ wasm.__wbindgen_export4(r0, r1 * 4, 4);
1150
+ return v1;
1151
+ } finally {
1152
+ wasm.__wbindgen_add_to_stack_pointer(16);
1153
+ }
1154
+ }
1155
+
1156
+ /**
1157
+ * Compute scaled dot-product attention
1158
+ *
1159
+ * # Arguments
1160
+ * * `query` - Query vector as Float32Array
1161
+ * * `keys` - Array of key vectors
1162
+ * * `values` - Array of value vectors
1163
+ * * `scale` - Optional scaling factor (defaults to 1/sqrt(dim))
1164
+ * @param {Float32Array} query
1165
+ * @param {any} keys
1166
+ * @param {any} values
1167
+ * @param {number | null} [scale]
1168
+ * @returns {Float32Array}
1169
+ */
1170
+ export function scaled_dot_attention(query, keys, values, scale) {
1171
+ try {
1172
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
1173
+ const ptr0 = passArrayF32ToWasm0(query, wasm.__wbindgen_export);
1174
+ const len0 = WASM_VECTOR_LEN;
1175
+ wasm.scaled_dot_attention(retptr, ptr0, len0, addHeapObject(keys), addHeapObject(values), isLikeNone(scale) ? 0x100000001 : Math.fround(scale));
1176
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
1177
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
1178
+ var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
1179
+ var r3 = getDataViewMemory0().getInt32(retptr + 4 * 3, true);
1180
+ if (r3) {
1181
+ throw takeObject(r2);
1182
+ }
1183
+ var v2 = getArrayF32FromWasm0(r0, r1).slice();
1184
+ wasm.__wbindgen_export4(r0, r1 * 4, 4);
1185
+ return v2;
1186
+ } finally {
1187
+ wasm.__wbindgen_add_to_stack_pointer(16);
1188
+ }
1189
+ }
1190
+
1191
+ /**
1192
+ * Compute softmax of a vector
1193
+ * @param {Float32Array} vec
1194
+ */
1195
+ export function softmax(vec) {
1196
+ var ptr0 = passArrayF32ToWasm0(vec, wasm.__wbindgen_export);
1197
+ var len0 = WASM_VECTOR_LEN;
1198
+ wasm.softmax(ptr0, len0, addHeapObject(vec));
1199
+ }
1200
+
1201
+ /**
1202
+ * Get the version of the ruvector-attention-wasm crate
1203
+ * @returns {string}
1204
+ */
1205
+ export function version() {
1206
+ let deferred1_0;
1207
+ let deferred1_1;
1208
+ try {
1209
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
1210
+ wasm.version(retptr);
1211
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
1212
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
1213
+ deferred1_0 = r0;
1214
+ deferred1_1 = r1;
1215
+ return getStringFromWasm0(r0, r1);
1216
+ } finally {
1217
+ wasm.__wbindgen_add_to_stack_pointer(16);
1218
+ wasm.__wbindgen_export4(deferred1_0, deferred1_1, 1);
1219
+ }
1220
+ }
1221
+
1222
+ const EXPECTED_RESPONSE_TYPES = new Set(['basic', 'cors', 'default']);
1223
+
1224
+ async function __wbg_load(module, imports) {
1225
+ if (typeof Response === 'function' && module instanceof Response) {
1226
+ if (typeof WebAssembly.instantiateStreaming === 'function') {
1227
+ try {
1228
+ return await WebAssembly.instantiateStreaming(module, imports);
1229
+ } catch (e) {
1230
+ const validResponse = module.ok && EXPECTED_RESPONSE_TYPES.has(module.type);
1231
+
1232
+ if (validResponse && module.headers.get('Content-Type') !== 'application/wasm') {
1233
+ console.warn("`WebAssembly.instantiateStreaming` failed because your server does not serve Wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n", e);
1234
+
1235
+ } else {
1236
+ throw e;
1237
+ }
1238
+ }
1239
+ }
1240
+
1241
+ const bytes = await module.arrayBuffer();
1242
+ return await WebAssembly.instantiate(bytes, imports);
1243
+ } else {
1244
+ const instance = await WebAssembly.instantiate(module, imports);
1245
+
1246
+ if (instance instanceof WebAssembly.Instance) {
1247
+ return { instance, module };
1248
+ } else {
1249
+ return instance;
1250
+ }
1251
+ }
1252
+ }
1253
+
1254
+ function __wbg_get_imports() {
1255
+ const imports = {};
1256
+ imports.wbg = {};
1257
+ imports.wbg.__wbg_Error_52673b7de5a0ca89 = function(arg0, arg1) {
1258
+ const ret = Error(getStringFromWasm0(arg0, arg1));
1259
+ return addHeapObject(ret);
1260
+ };
1261
+ imports.wbg.__wbg_String_8f0eb39a4a4c2f66 = function(arg0, arg1) {
1262
+ const ret = String(getObject(arg1));
1263
+ const ptr1 = passStringToWasm0(ret, wasm.__wbindgen_export, wasm.__wbindgen_export2);
1264
+ const len1 = WASM_VECTOR_LEN;
1265
+ getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true);
1266
+ getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true);
1267
+ };
1268
+ imports.wbg.__wbg___wbindgen_boolean_get_dea25b33882b895b = function(arg0) {
1269
+ const v = getObject(arg0);
1270
+ const ret = typeof(v) === 'boolean' ? v : undefined;
1271
+ return isLikeNone(ret) ? 0xFFFFFF : ret ? 1 : 0;
1272
+ };
1273
+ imports.wbg.__wbg___wbindgen_copy_to_typed_array_db832bc4df7216c1 = function(arg0, arg1, arg2) {
1274
+ new Uint8Array(getObject(arg2).buffer, getObject(arg2).byteOffset, getObject(arg2).byteLength).set(getArrayU8FromWasm0(arg0, arg1));
1275
+ };
1276
+ imports.wbg.__wbg___wbindgen_debug_string_adfb662ae34724b6 = function(arg0, arg1) {
1277
+ const ret = debugString(getObject(arg1));
1278
+ const ptr1 = passStringToWasm0(ret, wasm.__wbindgen_export, wasm.__wbindgen_export2);
1279
+ const len1 = WASM_VECTOR_LEN;
1280
+ getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true);
1281
+ getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true);
1282
+ };
1283
+ imports.wbg.__wbg___wbindgen_is_function_8d400b8b1af978cd = function(arg0) {
1284
+ const ret = typeof(getObject(arg0)) === 'function';
1285
+ return ret;
1286
+ };
1287
+ imports.wbg.__wbg___wbindgen_is_object_ce774f3490692386 = function(arg0) {
1288
+ const val = getObject(arg0);
1289
+ const ret = typeof(val) === 'object' && val !== null;
1290
+ return ret;
1291
+ };
1292
+ imports.wbg.__wbg___wbindgen_jsval_loose_eq_766057600fdd1b0d = function(arg0, arg1) {
1293
+ const ret = getObject(arg0) == getObject(arg1);
1294
+ return ret;
1295
+ };
1296
+ imports.wbg.__wbg___wbindgen_number_get_9619185a74197f95 = function(arg0, arg1) {
1297
+ const obj = getObject(arg1);
1298
+ const ret = typeof(obj) === 'number' ? obj : undefined;
1299
+ getDataViewMemory0().setFloat64(arg0 + 8 * 1, isLikeNone(ret) ? 0 : ret, true);
1300
+ getDataViewMemory0().setInt32(arg0 + 4 * 0, !isLikeNone(ret), true);
1301
+ };
1302
+ imports.wbg.__wbg___wbindgen_string_get_a2a31e16edf96e42 = function(arg0, arg1) {
1303
+ const obj = getObject(arg1);
1304
+ const ret = typeof(obj) === 'string' ? obj : undefined;
1305
+ var ptr1 = isLikeNone(ret) ? 0 : passStringToWasm0(ret, wasm.__wbindgen_export, wasm.__wbindgen_export2);
1306
+ var len1 = WASM_VECTOR_LEN;
1307
+ getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true);
1308
+ getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true);
1309
+ };
1310
+ imports.wbg.__wbg___wbindgen_throw_dd24417ed36fc46e = function(arg0, arg1) {
1311
+ throw new Error(getStringFromWasm0(arg0, arg1));
1312
+ };
1313
+ imports.wbg.__wbg_call_abb4ff46ce38be40 = function() { return handleError(function (arg0, arg1) {
1314
+ const ret = getObject(arg0).call(getObject(arg1));
1315
+ return addHeapObject(ret);
1316
+ }, arguments) };
1317
+ imports.wbg.__wbg_done_62ea16af4ce34b24 = function(arg0) {
1318
+ const ret = getObject(arg0).done;
1319
+ return ret;
1320
+ };
1321
+ imports.wbg.__wbg_error_7534b8e9a36f1ab4 = function(arg0, arg1) {
1322
+ let deferred0_0;
1323
+ let deferred0_1;
1324
+ try {
1325
+ deferred0_0 = arg0;
1326
+ deferred0_1 = arg1;
1327
+ console.error(getStringFromWasm0(arg0, arg1));
1328
+ } finally {
1329
+ wasm.__wbindgen_export4(deferred0_0, deferred0_1, 1);
1330
+ }
1331
+ };
1332
+ imports.wbg.__wbg_error_7bc7d576a6aaf855 = function(arg0) {
1333
+ console.error(getObject(arg0));
1334
+ };
1335
+ imports.wbg.__wbg_get_6b7bd52aca3f9671 = function(arg0, arg1) {
1336
+ const ret = getObject(arg0)[arg1 >>> 0];
1337
+ return addHeapObject(ret);
1338
+ };
1339
+ imports.wbg.__wbg_get_af9dab7e9603ea93 = function() { return handleError(function (arg0, arg1) {
1340
+ const ret = Reflect.get(getObject(arg0), getObject(arg1));
1341
+ return addHeapObject(ret);
1342
+ }, arguments) };
1343
+ imports.wbg.__wbg_instanceof_ArrayBuffer_f3320d2419cd0355 = function(arg0) {
1344
+ let result;
1345
+ try {
1346
+ result = getObject(arg0) instanceof ArrayBuffer;
1347
+ } catch (_) {
1348
+ result = false;
1349
+ }
1350
+ const ret = result;
1351
+ return ret;
1352
+ };
1353
+ imports.wbg.__wbg_instanceof_Uint8Array_da54ccc9d3e09434 = function(arg0) {
1354
+ let result;
1355
+ try {
1356
+ result = getObject(arg0) instanceof Uint8Array;
1357
+ } catch (_) {
1358
+ result = false;
1359
+ }
1360
+ const ret = result;
1361
+ return ret;
1362
+ };
1363
+ imports.wbg.__wbg_isArray_51fd9e6422c0a395 = function(arg0) {
1364
+ const ret = Array.isArray(getObject(arg0));
1365
+ return ret;
1366
+ };
1367
+ imports.wbg.__wbg_iterator_27b7c8b35ab3e86b = function() {
1368
+ const ret = Symbol.iterator;
1369
+ return addHeapObject(ret);
1370
+ };
1371
+ imports.wbg.__wbg_length_22ac23eaec9d8053 = function(arg0) {
1372
+ const ret = getObject(arg0).length;
1373
+ return ret;
1374
+ };
1375
+ imports.wbg.__wbg_length_d45040a40c570362 = function(arg0) {
1376
+ const ret = getObject(arg0).length;
1377
+ return ret;
1378
+ };
1379
+ imports.wbg.__wbg_log_1d990106d99dacb7 = function(arg0) {
1380
+ console.log(getObject(arg0));
1381
+ };
1382
+ imports.wbg.__wbg_new_25f239778d6112b9 = function() {
1383
+ const ret = new Array();
1384
+ return addHeapObject(ret);
1385
+ };
1386
+ imports.wbg.__wbg_new_6421f6084cc5bc5a = function(arg0) {
1387
+ const ret = new Uint8Array(getObject(arg0));
1388
+ return addHeapObject(ret);
1389
+ };
1390
+ imports.wbg.__wbg_new_8a6f238a6ece86ea = function() {
1391
+ const ret = new Error();
1392
+ return addHeapObject(ret);
1393
+ };
1394
+ imports.wbg.__wbg_next_138a17bbf04e926c = function(arg0) {
1395
+ const ret = getObject(arg0).next;
1396
+ return addHeapObject(ret);
1397
+ };
1398
+ imports.wbg.__wbg_next_3cfe5c0fe2a4cc53 = function() { return handleError(function (arg0) {
1399
+ const ret = getObject(arg0).next();
1400
+ return addHeapObject(ret);
1401
+ }, arguments) };
1402
+ imports.wbg.__wbg_prototypesetcall_dfe9b766cdc1f1fd = function(arg0, arg1, arg2) {
1403
+ Uint8Array.prototype.set.call(getArrayU8FromWasm0(arg0, arg1), getObject(arg2));
1404
+ };
1405
+ imports.wbg.__wbg_random_cc1f9237d866d212 = function() {
1406
+ const ret = Math.random();
1407
+ return ret;
1408
+ };
1409
+ imports.wbg.__wbg_set_7df433eea03a5c14 = function(arg0, arg1, arg2) {
1410
+ getObject(arg0)[arg1 >>> 0] = takeObject(arg2);
1411
+ };
1412
+ imports.wbg.__wbg_stack_0ed75d68575b0f3c = function(arg0, arg1) {
1413
+ const ret = getObject(arg1).stack;
1414
+ const ptr1 = passStringToWasm0(ret, wasm.__wbindgen_export, wasm.__wbindgen_export2);
1415
+ const len1 = WASM_VECTOR_LEN;
1416
+ getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true);
1417
+ getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true);
1418
+ };
1419
+ imports.wbg.__wbg_value_57b7b035e117f7ee = function(arg0) {
1420
+ const ret = getObject(arg0).value;
1421
+ return addHeapObject(ret);
1422
+ };
1423
+ imports.wbg.__wbindgen_cast_2241b6af4c4b2941 = function(arg0, arg1) {
1424
+ // Cast intrinsic for `Ref(String) -> Externref`.
1425
+ const ret = getStringFromWasm0(arg0, arg1);
1426
+ return addHeapObject(ret);
1427
+ };
1428
+ imports.wbg.__wbindgen_object_drop_ref = function(arg0) {
1429
+ takeObject(arg0);
1430
+ };
1431
+
1432
+ return imports;
1433
+ }
1434
+
1435
+ function __wbg_finalize_init(instance, module) {
1436
+ wasm = instance.exports;
1437
+ __wbg_init.__wbindgen_wasm_module = module;
1438
+ cachedDataViewMemory0 = null;
1439
+ cachedFloat32ArrayMemory0 = null;
1440
+ cachedUint8ArrayMemory0 = null;
1441
+
1442
+
1443
+ wasm.__wbindgen_start();
1444
+ return wasm;
1445
+ }
1446
+
1447
+ function initSync(module) {
1448
+ if (wasm !== undefined) return wasm;
1449
+
1450
+
1451
+ if (typeof module !== 'undefined') {
1452
+ if (Object.getPrototypeOf(module) === Object.prototype) {
1453
+ ({module} = module)
1454
+ } else {
1455
+ console.warn('using deprecated parameters for `initSync()`; pass a single object instead')
1456
+ }
1457
+ }
1458
+
1459
+ const imports = __wbg_get_imports();
1460
+ if (!(module instanceof WebAssembly.Module)) {
1461
+ module = new WebAssembly.Module(module);
1462
+ }
1463
+ const instance = new WebAssembly.Instance(module, imports);
1464
+ return __wbg_finalize_init(instance, module);
1465
+ }
1466
+
1467
+ async function __wbg_init(module_or_path) {
1468
+ if (wasm !== undefined) return wasm;
1469
+
1470
+
1471
+ if (typeof module_or_path !== 'undefined') {
1472
+ if (Object.getPrototypeOf(module_or_path) === Object.prototype) {
1473
+ ({module_or_path} = module_or_path)
1474
+ } else {
1475
+ console.warn('using deprecated parameters for the initialization function; pass a single object instead')
1476
+ }
1477
+ }
1478
+
1479
+ if (typeof module_or_path === 'undefined') {
1480
+ module_or_path = new URL('ruvector_attention_wasm_bg.wasm', import.meta.url);
1481
+ }
1482
+ const imports = __wbg_get_imports();
1483
+
1484
+ if (typeof module_or_path === 'string' || (typeof Request === 'function' && module_or_path instanceof Request) || (typeof URL === 'function' && module_or_path instanceof URL)) {
1485
+ module_or_path = fetch(module_or_path);
1486
+ }
1487
+
1488
+ const { instance, module } = await __wbg_load(await module_or_path, imports);
1489
+
1490
+ return __wbg_finalize_init(instance, module);
1491
+ }
1492
+
1493
+ export { initSync };
1494
+ export default __wbg_init;