File size: 18,332 Bytes
40d7073 | 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 | "use strict";
/**
* Attention Fallbacks - Safe wrapper around @ruvector/attention with automatic array conversion
*
* This wrapper handles the array type conversion automatically, allowing users
* to pass either regular arrays or Float32Arrays.
*
* @ruvector/attention requires Float32Array inputs.
* This wrapper handles the conversion automatically.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.AdamOptimizer = exports.DotProductAttention = exports.DualSpaceAttention = exports.EdgeFeaturedAttention = exports.GraphRoPeAttention = exports.MoEAttention = exports.LocalGlobalAttention = exports.LinearAttention = exports.HyperbolicAttention = exports.FlashAttention = exports.MultiHeadAttention = void 0;
exports.projectToPoincareBall = projectToPoincareBall;
exports.poincareDistance = poincareDistance;
exports.mobiusAddition = mobiusAddition;
exports.expMap = expMap;
exports.logMap = logMap;
exports.isAttentionAvailable = isAttentionAvailable;
exports.getAttentionVersion = getAttentionVersion;
exports.parallelAttentionCompute = parallelAttentionCompute;
exports.batchAttentionCompute = batchAttentionCompute;
exports.computeFlashAttentionAsync = computeFlashAttentionAsync;
exports.computeHyperbolicAttentionAsync = computeHyperbolicAttentionAsync;
exports.infoNceLoss = infoNceLoss;
exports.mineHardNegatives = mineHardNegatives;
exports.benchmarkAttention = benchmarkAttention;
// Lazy load to avoid import errors if not installed
let attentionModule = null;
let loadError = null;
function getAttentionModule() {
if (attentionModule)
return attentionModule;
if (loadError)
throw loadError;
try {
attentionModule = require('@ruvector/attention');
return attentionModule;
}
catch (e) {
loadError = new Error(`@ruvector/attention is not installed or failed to load: ${e.message}\n` +
`Install with: npm install @ruvector/attention`);
throw loadError;
}
}
/**
* Convert any array-like input to Float32Array
*/
function toFloat32Array(input) {
if (input instanceof Float32Array) {
return input;
}
return new Float32Array(input);
}
/**
* Convert nested arrays to Float32Arrays
*/
function toFloat32Arrays(inputs) {
return inputs.map(arr => toFloat32Array(arr));
}
/**
* Convert Float32Array result back to regular array if needed
*/
function fromFloat32Array(input) {
return Array.from(input);
}
/**
* Multi-head attention mechanism
*
* This wrapper automatically converts array inputs to Float32Array.
*/
class MultiHeadAttention {
/**
* Create a new multi-head attention instance
*
* @param dim - Embedding dimension (must be divisible by numHeads)
* @param numHeads - Number of attention heads
*/
constructor(dim, numHeads) {
const attention = getAttentionModule();
this.inner = new attention.MultiHeadAttention(dim, numHeads);
this.dim = dim;
this.numHeads = numHeads;
}
/**
* Compute multi-head attention
*
* @param query - Query vector
* @param keys - Array of key vectors
* @param values - Array of value vectors
* @returns Attention output
*
* @example
* ```typescript
* const mha = new MultiHeadAttention(64, 4);
*
* // Works with regular arrays
* const result1 = mha.compute([...64 values], [[...64], [...64]], [[...64], [...64]]);
*
* // Also works with Float32Array
* const q = new Float32Array(64);
* const k = [new Float32Array(64)];
* const v = [new Float32Array(64)];
* const result2 = mha.compute(q, k, v);
* ```
*/
compute(query, keys, values) {
const raw = this.inner.compute(toFloat32Array(query), toFloat32Arrays(keys), toFloat32Arrays(values));
return {
values: fromFloat32Array(raw),
raw
};
}
/**
* Compute and return raw Float32Array (faster, no conversion)
*/
computeRaw(query, keys, values) {
return this.inner.compute(query, keys, values);
}
get headDim() {
return this.dim / this.numHeads;
}
}
exports.MultiHeadAttention = MultiHeadAttention;
/**
* Flash attention with tiled computation
*/
class FlashAttention {
/**
* Create a new flash attention instance
*
* @param dim - Embedding dimension
* @param blockSize - Block size for tiled computation (default: 512)
*/
constructor(dim, blockSize = 512) {
const attention = getAttentionModule();
this.inner = new attention.FlashAttention(dim, blockSize);
this.dim = dim;
this.blockSize = blockSize;
}
/**
* Compute flash attention
*/
compute(query, keys, values) {
const raw = this.inner.compute(toFloat32Array(query), toFloat32Arrays(keys), toFloat32Arrays(values));
return {
values: fromFloat32Array(raw),
raw
};
}
computeRaw(query, keys, values) {
return this.inner.compute(query, keys, values);
}
}
exports.FlashAttention = FlashAttention;
/**
* Hyperbolic attention in Poincare ball model
*/
class HyperbolicAttention {
/**
* Create a new hyperbolic attention instance
*
* @param dim - Embedding dimension
* @param curvature - Hyperbolic curvature (typically 1.0)
*/
constructor(dim, curvature = 1.0) {
const attention = getAttentionModule();
this.inner = new attention.HyperbolicAttention(dim, curvature);
this.dim = dim;
this.curvature = curvature;
}
/**
* Compute hyperbolic attention
*/
compute(query, keys, values) {
const raw = this.inner.compute(toFloat32Array(query), toFloat32Arrays(keys), toFloat32Arrays(values));
return {
values: fromFloat32Array(raw),
raw
};
}
computeRaw(query, keys, values) {
return this.inner.compute(query, keys, values);
}
}
exports.HyperbolicAttention = HyperbolicAttention;
/**
* Linear attention (Performer-style) with O(n) complexity
*/
class LinearAttention {
/**
* Create a new linear attention instance
*
* @param dim - Embedding dimension
* @param numFeatures - Number of random features
*/
constructor(dim, numFeatures) {
const attention = getAttentionModule();
this.inner = new attention.LinearAttention(dim, numFeatures);
this.dim = dim;
this.numFeatures = numFeatures;
}
/**
* Compute linear attention
*/
compute(query, keys, values) {
const raw = this.inner.compute(toFloat32Array(query), toFloat32Arrays(keys), toFloat32Arrays(values));
return {
values: fromFloat32Array(raw),
raw
};
}
computeRaw(query, keys, values) {
return this.inner.compute(query, keys, values);
}
}
exports.LinearAttention = LinearAttention;
/**
* Local-global attention (Longformer-style)
*/
class LocalGlobalAttention {
/**
* Create a new local-global attention instance
*
* @param dim - Embedding dimension
* @param localWindow - Size of local attention window
* @param globalTokens - Number of global attention tokens
*/
constructor(dim, localWindow, globalTokens) {
const attention = getAttentionModule();
this.inner = new attention.LocalGlobalAttention(dim, localWindow, globalTokens);
this.dim = dim;
this.localWindow = localWindow;
this.globalTokens = globalTokens;
}
/**
* Compute local-global attention
*/
compute(query, keys, values) {
const raw = this.inner.compute(toFloat32Array(query), toFloat32Arrays(keys), toFloat32Arrays(values));
return {
values: fromFloat32Array(raw),
raw
};
}
computeRaw(query, keys, values) {
return this.inner.compute(query, keys, values);
}
}
exports.LocalGlobalAttention = LocalGlobalAttention;
/**
* Mixture of Experts attention
*/
class MoEAttention {
/**
* Create a new MoE attention instance
*
* @param config - MoE configuration
*/
constructor(config) {
const attention = getAttentionModule();
this.inner = new attention.MoEAttention({
dim: config.dim,
num_experts: config.numExperts,
top_k: config.topK,
expert_capacity: config.expertCapacity ?? 1.25,
});
this.config = config;
}
/**
* Create with simple parameters
*/
static simple(dim, numExperts, topK) {
return new MoEAttention({ dim, numExperts, topK });
}
/**
* Compute MoE attention
*/
compute(query, keys, values) {
const raw = this.inner.compute(toFloat32Array(query), toFloat32Arrays(keys), toFloat32Arrays(values));
return {
values: fromFloat32Array(raw),
raw
};
}
computeRaw(query, keys, values) {
return this.inner.compute(query, keys, values);
}
}
exports.MoEAttention = MoEAttention;
// Hyperbolic math utilities
/**
* Project a vector into the Poincare ball
*/
function projectToPoincareBall(vector, curvature = 1.0) {
const attention = getAttentionModule();
const result = attention.projectToPoincareBall(toFloat32Array(vector), curvature);
return fromFloat32Array(result);
}
/**
* Compute hyperbolic (Poincare) distance between two points
*/
function poincareDistance(a, b, curvature = 1.0) {
const attention = getAttentionModule();
return attention.poincareDistance(toFloat32Array(a), toFloat32Array(b), curvature);
}
/**
* Mobius addition in hyperbolic space
*/
function mobiusAddition(a, b, curvature = 1.0) {
const attention = getAttentionModule();
const result = attention.mobiusAddition(toFloat32Array(a), toFloat32Array(b), curvature);
return fromFloat32Array(result);
}
/**
* Exponential map from tangent space to hyperbolic space
*/
function expMap(base, tangent, curvature = 1.0) {
const attention = getAttentionModule();
const result = attention.expMap(toFloat32Array(base), toFloat32Array(tangent), curvature);
return fromFloat32Array(result);
}
/**
* Logarithmic map from hyperbolic space to tangent space
*/
function logMap(base, point, curvature = 1.0) {
const attention = getAttentionModule();
const result = attention.logMap(toFloat32Array(base), toFloat32Array(point), curvature);
return fromFloat32Array(result);
}
/**
* Check if attention module is available
*/
function isAttentionAvailable() {
try {
getAttentionModule();
return true;
}
catch {
return false;
}
}
/**
* Get attention module version
*/
function getAttentionVersion() {
try {
const attention = getAttentionModule();
return attention.version?.() ?? null;
}
catch {
return null;
}
}
// ============================================================================
// Graph-based Attention (for code structure)
// ============================================================================
/**
* Graph attention with Rotary Position Embeddings
* Excellent for code AST and dependency graphs
*/
class GraphRoPeAttention {
constructor(dim, numHeads = 4, maxSeqLen = 4096) {
const attention = getAttentionModule();
this.inner = new attention.GraphRoPeAttention(dim, numHeads, maxSeqLen);
this.dim = dim;
this.numHeads = numHeads;
this.maxSeqLen = maxSeqLen;
}
compute(query, keys, values, positions) {
const raw = this.inner.compute(toFloat32Array(query), toFloat32Arrays(keys), toFloat32Arrays(values), positions ? new Int32Array(positions) : undefined);
return { values: fromFloat32Array(raw), raw };
}
}
exports.GraphRoPeAttention = GraphRoPeAttention;
/**
* Edge-featured attention for graphs with edge attributes
* Useful for weighted dependency graphs
*/
class EdgeFeaturedAttention {
constructor(dim, edgeDim = 16) {
const attention = getAttentionModule();
this.inner = new attention.EdgeFeaturedAttention(dim, edgeDim);
this.dim = dim;
this.edgeDim = edgeDim;
}
compute(query, keys, values, edgeFeatures) {
const raw = this.inner.compute(toFloat32Array(query), toFloat32Arrays(keys), toFloat32Arrays(values), edgeFeatures ? toFloat32Arrays(edgeFeatures) : undefined);
return { values: fromFloat32Array(raw), raw };
}
}
exports.EdgeFeaturedAttention = EdgeFeaturedAttention;
/**
* Dual-space attention (Euclidean + Hyperbolic)
* Best of both worlds for hierarchical + semantic similarity
*/
class DualSpaceAttention {
constructor(dim, curvature = 1.0, alpha = 0.5) {
const attention = getAttentionModule();
this.inner = new attention.DualSpaceAttention(dim, curvature, alpha);
this.dim = dim;
this.curvature = curvature;
this.alpha = alpha;
}
compute(query, keys, values) {
const raw = this.inner.compute(toFloat32Array(query), toFloat32Arrays(keys), toFloat32Arrays(values));
return { values: fromFloat32Array(raw), raw };
}
}
exports.DualSpaceAttention = DualSpaceAttention;
/**
* Basic dot-product attention
*/
class DotProductAttention {
constructor(dim) {
const attention = getAttentionModule();
this.inner = new attention.DotProductAttention(dim);
this.dim = dim;
}
compute(query, keys, values) {
const raw = this.inner.compute(toFloat32Array(query), toFloat32Arrays(keys), toFloat32Arrays(values));
return { values: fromFloat32Array(raw), raw };
}
}
exports.DotProductAttention = DotProductAttention;
// ============================================================================
// Parallel/Batch Attention Compute
// ============================================================================
/**
* Compute attention in parallel across multiple queries
*/
async function parallelAttentionCompute(queries, keys, values, attentionType = 'multi-head') {
const attention = getAttentionModule();
const results = await attention.parallelAttentionCompute(toFloat32Arrays(queries), toFloat32Arrays(keys), toFloat32Arrays(values), attentionType);
return results.map((r) => fromFloat32Array(r));
}
/**
* Batch attention compute for multiple query-key-value sets
*/
async function batchAttentionCompute(batches, attentionType = 'multi-head') {
const attention = getAttentionModule();
const nativeBatches = batches.map(b => ({
query: toFloat32Array(b.query),
keys: toFloat32Arrays(b.keys),
values: toFloat32Arrays(b.values),
}));
const results = await attention.batchAttentionCompute(nativeBatches, attentionType);
return results.map((r) => fromFloat32Array(r));
}
/**
* Async flash attention with callback
*/
function computeFlashAttentionAsync(query, keys, values) {
const attention = getAttentionModule();
return new Promise((resolve, reject) => {
attention.computeFlashAttentionAsync(toFloat32Array(query), toFloat32Arrays(keys), toFloat32Arrays(values), (err, result) => {
if (err)
reject(err);
else
resolve(fromFloat32Array(result));
});
});
}
/**
* Async hyperbolic attention
*/
function computeHyperbolicAttentionAsync(query, keys, values, curvature = 1.0) {
const attention = getAttentionModule();
return new Promise((resolve, reject) => {
attention.computeHyperbolicAttentionAsync(toFloat32Array(query), toFloat32Arrays(keys), toFloat32Arrays(values), curvature, (err, result) => {
if (err)
reject(err);
else
resolve(fromFloat32Array(result));
});
});
}
// ============================================================================
// Training Utilities (for SONA integration)
// ============================================================================
/**
* Adam optimizer for attention training
*/
class AdamOptimizer {
constructor(learningRate = 0.001, beta1 = 0.9, beta2 = 0.999) {
const attention = getAttentionModule();
this.inner = new attention.AdamOptimizer(learningRate, beta1, beta2);
}
step(gradients, params) {
const result = this.inner.step(toFloat32Array(gradients), toFloat32Array(params));
return fromFloat32Array(result);
}
}
exports.AdamOptimizer = AdamOptimizer;
/**
* InfoNCE contrastive loss
*/
function infoNceLoss(anchor, positive, negatives, temperature = 0.07) {
const attention = getAttentionModule();
return attention.InfoNceLoss.compute(toFloat32Array(anchor), toFloat32Array(positive), toFloat32Arrays(negatives), temperature);
}
/**
* Hard negative mining for contrastive learning
*/
function mineHardNegatives(anchor, candidates, topK = 5) {
const attention = getAttentionModule();
const miner = new attention.HardNegativeMiner(topK);
const results = miner.mine(toFloat32Array(anchor), toFloat32Arrays(candidates));
return results.map((r) => fromFloat32Array(r));
}
// ============================================================================
// Benchmarking
// ============================================================================
/**
* Benchmark attention implementations
*/
async function benchmarkAttention(dim, seqLen, iterations = 100) {
const attention = getAttentionModule();
return attention.benchmarkAttention(dim, seqLen, iterations);
}
exports.default = {
// Core attention types
DotProductAttention,
MultiHeadAttention,
FlashAttention,
HyperbolicAttention,
LinearAttention,
LocalGlobalAttention,
MoEAttention,
// Graph attention types
GraphRoPeAttention,
EdgeFeaturedAttention,
DualSpaceAttention,
// Parallel/batch compute
parallelAttentionCompute,
batchAttentionCompute,
computeFlashAttentionAsync,
computeHyperbolicAttentionAsync,
// Training utilities
AdamOptimizer,
infoNceLoss,
mineHardNegatives,
// Hyperbolic math
projectToPoincareBall,
poincareDistance,
mobiusAddition,
expMap,
logMap,
// Utilities
isAttentionAvailable,
getAttentionVersion,
benchmarkAttention,
};
|