@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.
package/README.md CHANGED
@@ -12,6 +12,7 @@ WebAssembly bindings for the ruvector-attention package, providing high-performa
12
12
  - Flash Attention (memory-efficient)
13
13
  - Local-Global Attention
14
14
  - Mixture of Experts (MoE) Attention
15
+ - **CGT Sheaf Attention** (coherence-gated via Prime-Radiant)
15
16
 
16
17
  - **Training Utilities**:
17
18
  - InfoNCE contrastive loss
@@ -159,8 +160,34 @@ wasm-pack test --headless --firefox
159
160
  - `FlashAttention` - Memory-efficient attention
160
161
  - `LocalGlobalAttention` - Combined local and global attention
161
162
  - `MoEAttention` - Mixture of Experts attention
163
+ - `CGTSheafAttention` - Coherence-gated via Prime-Radiant energy
162
164
  - `scaledDotAttention()` - Functional API for basic attention
163
165
 
166
+ ### CGT Sheaf Attention (Prime-Radiant Integration)
167
+
168
+ The CGT (Coherence-Gated Transformer) Sheaf Attention mechanism uses Prime-Radiant's sheaf Laplacian energy to gate attention based on mathematical consistency:
169
+
170
+ ```typescript
171
+ import { CGTSheafAttention } from 'ruvector-attention-wasm';
172
+
173
+ const cgtAttention = new CGTSheafAttention({
174
+ dim: 128,
175
+ numHeads: 8,
176
+ coherenceThreshold: 0.3, // Block if energy > threshold
177
+ });
178
+
179
+ // Attention is gated by coherence energy
180
+ const result = cgtAttention.compute(query, keys, values);
181
+ console.log('Coherence energy:', result.energy);
182
+ console.log('Is coherent:', result.isCoherent);
183
+ ```
184
+
185
+ **Key features:**
186
+ - Energy-weighted attention: Lower coherence energy → higher attention
187
+ - Automatic hallucination detection via residual analysis
188
+ - GPU-accelerated with wgpu WGSL shaders (vec4 optimized)
189
+ - SIMD fallback (AVX-512/AVX2/NEON)
190
+
164
191
  ### Training
165
192
 
166
193
  - `InfoNCELoss` - Contrastive loss function
package/package.json CHANGED
@@ -1,45 +1,32 @@
1
1
  {
2
2
  "name": "@ruvector/attention-wasm",
3
- "version": "0.1.0",
4
- "description": "WebAssembly bindings for ruvector-attention - high-performance attention mechanisms",
5
- "main": "pkg/ruvector_attention_wasm.js",
6
- "types": "js/index.ts",
7
- "files": [
8
- "pkg/",
9
- "js/"
3
+ "type": "module",
4
+ "collaborators": [
5
+ "Ruvector Team"
10
6
  ],
11
- "scripts": {
12
- "build": "wasm-pack build --target web --out-dir pkg",
13
- "build:node": "wasm-pack build --target nodejs --out-dir pkg-node",
14
- "build:bundler": "wasm-pack build --target bundler --out-dir pkg-bundler",
15
- "build:all": "npm run build && npm run build:node && npm run build:bundler",
16
- "test": "wasm-pack test --headless --firefox",
17
- "test:chrome": "wasm-pack test --headless --chrome",
18
- "clean": "rm -rf pkg pkg-node pkg-bundler target"
19
- },
7
+ "description": "High-performance WebAssembly attention mechanisms: Multi-Head, Flash, Hyperbolic, MoE, CGT Sheaf Attention with GPU acceleration for transformers and LLMs",
8
+ "version": "2.0.4",
9
+ "license": "MIT",
20
10
  "repository": {
21
11
  "type": "git",
22
12
  "url": "https://github.com/ruvnet/ruvector"
23
13
  },
14
+ "files": [
15
+ "ruvector_attention_wasm_bg.wasm",
16
+ "ruvector_attention_wasm.js",
17
+ "ruvector_attention_wasm.d.ts"
18
+ ],
19
+ "main": "ruvector_attention_wasm.js",
20
+ "homepage": "https://ruv.io/ruvector",
21
+ "types": "ruvector_attention_wasm.d.ts",
22
+ "sideEffects": [
23
+ "./snippets/*"
24
+ ],
24
25
  "keywords": [
25
26
  "wasm",
26
- "webassembly",
27
27
  "attention",
28
28
  "transformer",
29
- "machine-learning",
30
- "neural-networks",
31
- "hyperbolic",
32
- "moe",
33
- "flash-attention"
34
- ],
35
- "author": "rUv",
36
- "license": "MIT OR Apache-2.0",
37
- "bugs": {
38
- "url": "https://github.com/ruvnet/ruvector/issues"
39
- },
40
- "homepage": "https://ruv.io/ruvector",
41
- "devDependencies": {
42
- "@types/node": "^20.0.0",
43
- "typescript": "^5.0.0"
44
- }
29
+ "flash-attention",
30
+ "llm"
31
+ ]
45
32
  }
@@ -0,0 +1,422 @@
1
+ /* tslint:disable */
2
+ /* eslint-disable */
3
+
4
+ export class WasmAdam {
5
+ free(): void;
6
+ [Symbol.dispose](): void;
7
+ /**
8
+ * Create a new Adam optimizer
9
+ *
10
+ * # Arguments
11
+ * * `param_count` - Number of parameters
12
+ * * `learning_rate` - Learning rate
13
+ */
14
+ constructor(param_count: number, learning_rate: number);
15
+ /**
16
+ * Perform optimization step
17
+ *
18
+ * # Arguments
19
+ * * `params` - Current parameter values (will be updated in-place)
20
+ * * `gradients` - Gradient values
21
+ */
22
+ step(params: Float32Array, gradients: Float32Array): void;
23
+ /**
24
+ * Reset optimizer state
25
+ */
26
+ reset(): void;
27
+ /**
28
+ * Get current learning rate
29
+ */
30
+ learning_rate: number;
31
+ }
32
+
33
+ export class WasmAdamW {
34
+ free(): void;
35
+ [Symbol.dispose](): void;
36
+ /**
37
+ * Create a new AdamW optimizer
38
+ *
39
+ * # Arguments
40
+ * * `param_count` - Number of parameters
41
+ * * `learning_rate` - Learning rate
42
+ * * `weight_decay` - Weight decay coefficient
43
+ */
44
+ constructor(param_count: number, learning_rate: number, weight_decay: number);
45
+ /**
46
+ * Perform optimization step with weight decay
47
+ */
48
+ step(params: Float32Array, gradients: Float32Array): void;
49
+ /**
50
+ * Reset optimizer state
51
+ */
52
+ reset(): void;
53
+ /**
54
+ * Get weight decay
55
+ */
56
+ readonly weight_decay: number;
57
+ /**
58
+ * Get current learning rate
59
+ */
60
+ learning_rate: number;
61
+ }
62
+
63
+ export class WasmFlashAttention {
64
+ free(): void;
65
+ [Symbol.dispose](): void;
66
+ /**
67
+ * Create a new flash attention instance
68
+ *
69
+ * # Arguments
70
+ * * `dim` - Embedding dimension
71
+ * * `block_size` - Block size for tiling
72
+ */
73
+ constructor(dim: number, block_size: number);
74
+ /**
75
+ * Compute flash attention
76
+ */
77
+ compute(query: Float32Array, keys: any, values: any): Float32Array;
78
+ }
79
+
80
+ export class WasmHyperbolicAttention {
81
+ free(): void;
82
+ [Symbol.dispose](): void;
83
+ /**
84
+ * Create a new hyperbolic attention instance
85
+ *
86
+ * # Arguments
87
+ * * `dim` - Embedding dimension
88
+ * * `curvature` - Hyperbolic curvature parameter
89
+ */
90
+ constructor(dim: number, curvature: number);
91
+ /**
92
+ * Compute hyperbolic attention
93
+ */
94
+ compute(query: Float32Array, keys: any, values: any): Float32Array;
95
+ /**
96
+ * Get the curvature
97
+ */
98
+ readonly curvature: number;
99
+ }
100
+
101
+ export class WasmInfoNCELoss {
102
+ free(): void;
103
+ [Symbol.dispose](): void;
104
+ /**
105
+ * Create a new InfoNCE loss instance
106
+ *
107
+ * # Arguments
108
+ * * `temperature` - Temperature parameter for softmax
109
+ */
110
+ constructor(temperature: number);
111
+ /**
112
+ * Compute InfoNCE loss
113
+ *
114
+ * # Arguments
115
+ * * `anchor` - Anchor embedding
116
+ * * `positive` - Positive example embedding
117
+ * * `negatives` - Array of negative example embeddings
118
+ */
119
+ compute(anchor: Float32Array, positive: Float32Array, negatives: any): number;
120
+ }
121
+
122
+ export class WasmLRScheduler {
123
+ free(): void;
124
+ [Symbol.dispose](): void;
125
+ /**
126
+ * Create a new learning rate scheduler with warmup and cosine decay
127
+ *
128
+ * # Arguments
129
+ * * `initial_lr` - Initial learning rate
130
+ * * `warmup_steps` - Number of warmup steps
131
+ * * `total_steps` - Total training steps
132
+ */
133
+ constructor(initial_lr: number, warmup_steps: number, total_steps: number);
134
+ /**
135
+ * Advance to next step
136
+ */
137
+ step(): void;
138
+ /**
139
+ * Reset scheduler
140
+ */
141
+ reset(): void;
142
+ /**
143
+ * Get learning rate for current step
144
+ */
145
+ get_lr(): number;
146
+ }
147
+
148
+ export class WasmLinearAttention {
149
+ free(): void;
150
+ [Symbol.dispose](): void;
151
+ /**
152
+ * Create a new linear attention instance
153
+ *
154
+ * # Arguments
155
+ * * `dim` - Embedding dimension
156
+ * * `num_features` - Number of random features
157
+ */
158
+ constructor(dim: number, num_features: number);
159
+ /**
160
+ * Compute linear attention
161
+ */
162
+ compute(query: Float32Array, keys: any, values: any): Float32Array;
163
+ }
164
+
165
+ export class WasmLocalGlobalAttention {
166
+ free(): void;
167
+ [Symbol.dispose](): void;
168
+ /**
169
+ * Create a new local-global attention instance
170
+ *
171
+ * # Arguments
172
+ * * `dim` - Embedding dimension
173
+ * * `local_window` - Size of local attention window
174
+ * * `global_tokens` - Number of global attention tokens
175
+ */
176
+ constructor(dim: number, local_window: number, global_tokens: number);
177
+ /**
178
+ * Compute local-global attention
179
+ */
180
+ compute(query: Float32Array, keys: any, values: any): Float32Array;
181
+ }
182
+
183
+ export class WasmMoEAttention {
184
+ free(): void;
185
+ [Symbol.dispose](): void;
186
+ /**
187
+ * Create a new MoE attention instance
188
+ *
189
+ * # Arguments
190
+ * * `dim` - Embedding dimension
191
+ * * `num_experts` - Number of expert attention mechanisms
192
+ * * `top_k` - Number of experts to use per query
193
+ */
194
+ constructor(dim: number, num_experts: number, top_k: number);
195
+ /**
196
+ * Compute MoE attention
197
+ */
198
+ compute(query: Float32Array, keys: any, values: any): Float32Array;
199
+ }
200
+
201
+ export class WasmMultiHeadAttention {
202
+ free(): void;
203
+ [Symbol.dispose](): void;
204
+ /**
205
+ * Create a new multi-head attention instance
206
+ *
207
+ * # Arguments
208
+ * * `dim` - Embedding dimension
209
+ * * `num_heads` - Number of attention heads
210
+ */
211
+ constructor(dim: number, num_heads: number);
212
+ /**
213
+ * Compute multi-head attention
214
+ */
215
+ compute(query: Float32Array, keys: any, values: any): Float32Array;
216
+ /**
217
+ * Get the dimension
218
+ */
219
+ readonly dim: number;
220
+ /**
221
+ * Get the number of heads
222
+ */
223
+ readonly num_heads: number;
224
+ }
225
+
226
+ export class WasmSGD {
227
+ free(): void;
228
+ [Symbol.dispose](): void;
229
+ /**
230
+ * Create a new SGD optimizer
231
+ *
232
+ * # Arguments
233
+ * * `param_count` - Number of parameters
234
+ * * `learning_rate` - Learning rate
235
+ * * `momentum` - Momentum coefficient (default: 0)
236
+ */
237
+ constructor(param_count: number, learning_rate: number, momentum?: number | null);
238
+ /**
239
+ * Perform optimization step
240
+ */
241
+ step(params: Float32Array, gradients: Float32Array): void;
242
+ /**
243
+ * Reset optimizer state
244
+ */
245
+ reset(): void;
246
+ /**
247
+ * Get current learning rate
248
+ */
249
+ learning_rate: number;
250
+ }
251
+
252
+ /**
253
+ * Compute attention weights from scores
254
+ */
255
+ export function attention_weights(scores: Float32Array, temperature?: number | null): void;
256
+
257
+ /**
258
+ * Get information about available attention mechanisms
259
+ */
260
+ export function available_mechanisms(): any;
261
+
262
+ /**
263
+ * Batch normalize vectors
264
+ */
265
+ export function batch_normalize(vectors: any, epsilon?: number | null): Float32Array;
266
+
267
+ /**
268
+ * Compute cosine similarity between two vectors
269
+ */
270
+ export function cosine_similarity(a: Float32Array, b: Float32Array): number;
271
+
272
+ /**
273
+ * Initialize the WASM module with panic hook
274
+ */
275
+ export function init(): void;
276
+
277
+ /**
278
+ * Compute L2 norm of a vector
279
+ */
280
+ export function l2_norm(vec: Float32Array): number;
281
+
282
+ /**
283
+ * Log a message to the browser console
284
+ */
285
+ export function log(message: string): void;
286
+
287
+ /**
288
+ * Log an error to the browser console
289
+ */
290
+ export function log_error(message: string): void;
291
+
292
+ /**
293
+ * Normalize a vector to unit length
294
+ */
295
+ export function normalize(vec: Float32Array): void;
296
+
297
+ /**
298
+ * Compute pairwise distances between vectors
299
+ */
300
+ export function pairwise_distances(vectors: any): Float32Array;
301
+
302
+ /**
303
+ * Generate random orthogonal matrix (for initialization)
304
+ */
305
+ export function random_orthogonal_matrix(dim: number): Float32Array;
306
+
307
+ /**
308
+ * Compute scaled dot-product attention
309
+ *
310
+ * # Arguments
311
+ * * `query` - Query vector as Float32Array
312
+ * * `keys` - Array of key vectors
313
+ * * `values` - Array of value vectors
314
+ * * `scale` - Optional scaling factor (defaults to 1/sqrt(dim))
315
+ */
316
+ export function scaled_dot_attention(query: Float32Array, keys: any, values: any, scale?: number | null): Float32Array;
317
+
318
+ /**
319
+ * Compute softmax of a vector
320
+ */
321
+ export function softmax(vec: Float32Array): void;
322
+
323
+ /**
324
+ * Get the version of the ruvector-attention-wasm crate
325
+ */
326
+ export function version(): string;
327
+
328
+ export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;
329
+
330
+ export interface InitOutput {
331
+ readonly memory: WebAssembly.Memory;
332
+ readonly __wbg_wasmadam_free: (a: number, b: number) => void;
333
+ readonly __wbg_wasmadamw_free: (a: number, b: number) => void;
334
+ readonly __wbg_wasmflashattention_free: (a: number, b: number) => void;
335
+ readonly __wbg_wasmhyperbolicattention_free: (a: number, b: number) => void;
336
+ readonly __wbg_wasminfonceloss_free: (a: number, b: number) => void;
337
+ readonly __wbg_wasmlinearattention_free: (a: number, b: number) => void;
338
+ readonly __wbg_wasmmoeattention_free: (a: number, b: number) => void;
339
+ readonly __wbg_wasmmultiheadattention_free: (a: number, b: number) => void;
340
+ readonly __wbg_wasmsgd_free: (a: number, b: number) => void;
341
+ readonly attention_weights: (a: number, b: number, c: number, d: number) => void;
342
+ readonly available_mechanisms: () => number;
343
+ readonly batch_normalize: (a: number, b: number, c: number) => void;
344
+ readonly cosine_similarity: (a: number, b: number, c: number, d: number, e: number) => void;
345
+ readonly l2_norm: (a: number, b: number) => number;
346
+ readonly log: (a: number, b: number) => void;
347
+ readonly log_error: (a: number, b: number) => void;
348
+ readonly normalize: (a: number, b: number, c: number, d: number) => void;
349
+ readonly pairwise_distances: (a: number, b: number) => void;
350
+ readonly random_orthogonal_matrix: (a: number, b: number) => void;
351
+ readonly scaled_dot_attention: (a: number, b: number, c: number, d: number, e: number, f: number) => void;
352
+ readonly softmax: (a: number, b: number, c: number) => void;
353
+ readonly version: (a: number) => void;
354
+ readonly wasmadam_learning_rate: (a: number) => number;
355
+ readonly wasmadam_new: (a: number, b: number) => number;
356
+ readonly wasmadam_reset: (a: number) => void;
357
+ readonly wasmadam_set_learning_rate: (a: number, b: number) => void;
358
+ readonly wasmadam_step: (a: number, b: number, c: number, d: number, e: number, f: number) => void;
359
+ readonly wasmadamw_new: (a: number, b: number, c: number) => number;
360
+ readonly wasmadamw_reset: (a: number) => void;
361
+ readonly wasmadamw_step: (a: number, b: number, c: number, d: number, e: number, f: number) => void;
362
+ readonly wasmadamw_weight_decay: (a: number) => number;
363
+ readonly wasmflashattention_compute: (a: number, b: number, c: number, d: number, e: number, f: number) => void;
364
+ readonly wasmflashattention_new: (a: number, b: number) => number;
365
+ readonly wasmhyperbolicattention_compute: (a: number, b: number, c: number, d: number, e: number, f: number) => void;
366
+ readonly wasmhyperbolicattention_curvature: (a: number) => number;
367
+ readonly wasmhyperbolicattention_new: (a: number, b: number) => number;
368
+ readonly wasminfonceloss_compute: (a: number, b: number, c: number, d: number, e: number, f: number, g: number) => void;
369
+ readonly wasminfonceloss_new: (a: number) => number;
370
+ readonly wasmlinearattention_compute: (a: number, b: number, c: number, d: number, e: number, f: number) => void;
371
+ readonly wasmlinearattention_new: (a: number, b: number) => number;
372
+ readonly wasmlocalglobalattention_compute: (a: number, b: number, c: number, d: number, e: number, f: number) => void;
373
+ readonly wasmlocalglobalattention_new: (a: number, b: number, c: number) => number;
374
+ readonly wasmlrscheduler_get_lr: (a: number) => number;
375
+ readonly wasmlrscheduler_new: (a: number, b: number, c: number) => number;
376
+ readonly wasmlrscheduler_reset: (a: number) => void;
377
+ readonly wasmlrscheduler_step: (a: number) => void;
378
+ readonly wasmmoeattention_compute: (a: number, b: number, c: number, d: number, e: number, f: number) => void;
379
+ readonly wasmmoeattention_new: (a: number, b: number, c: number) => number;
380
+ readonly wasmmultiheadattention_compute: (a: number, b: number, c: number, d: number, e: number, f: number) => void;
381
+ readonly wasmmultiheadattention_dim: (a: number) => number;
382
+ readonly wasmmultiheadattention_new: (a: number, b: number, c: number) => void;
383
+ readonly wasmmultiheadattention_num_heads: (a: number) => number;
384
+ readonly wasmsgd_learning_rate: (a: number) => number;
385
+ readonly wasmsgd_new: (a: number, b: number, c: number) => number;
386
+ readonly wasmsgd_reset: (a: number) => void;
387
+ readonly wasmsgd_set_learning_rate: (a: number, b: number) => void;
388
+ readonly wasmsgd_step: (a: number, b: number, c: number, d: number, e: number, f: number) => void;
389
+ readonly init: () => void;
390
+ readonly wasmadamw_set_learning_rate: (a: number, b: number) => void;
391
+ readonly wasmadamw_learning_rate: (a: number) => number;
392
+ readonly __wbg_wasmlocalglobalattention_free: (a: number, b: number) => void;
393
+ readonly __wbg_wasmlrscheduler_free: (a: number, b: number) => void;
394
+ readonly __wbindgen_export: (a: number, b: number) => number;
395
+ readonly __wbindgen_export2: (a: number, b: number, c: number, d: number) => number;
396
+ readonly __wbindgen_export3: (a: number) => void;
397
+ readonly __wbindgen_export4: (a: number, b: number, c: number) => void;
398
+ readonly __wbindgen_add_to_stack_pointer: (a: number) => number;
399
+ readonly __wbindgen_start: () => void;
400
+ }
401
+
402
+ export type SyncInitInput = BufferSource | WebAssembly.Module;
403
+
404
+ /**
405
+ * Instantiates the given `module`, which can either be bytes or
406
+ * a precompiled `WebAssembly.Module`.
407
+ *
408
+ * @param {{ module: SyncInitInput }} module - Passing `SyncInitInput` directly is deprecated.
409
+ *
410
+ * @returns {InitOutput}
411
+ */
412
+ export function initSync(module: { module: SyncInitInput } | SyncInitInput): InitOutput;
413
+
414
+ /**
415
+ * If `module_or_path` is {RequestInfo} or {URL}, makes a request and
416
+ * for everything else, calls `WebAssembly.instantiate` directly.
417
+ *
418
+ * @param {{ module_or_path: InitInput | Promise<InitInput> }} module_or_path - Passing `InitInput` directly is deprecated.
419
+ *
420
+ * @returns {Promise<InitOutput>}
421
+ */
422
+ export default function __wbg_init (module_or_path?: { module_or_path: InitInput | Promise<InitInput> } | InitInput | Promise<InitInput>): Promise<InitOutput>;