From 9662b3992ff64c29cb9fd1686fe66bbe07bf99da Mon Sep 17 00:00:00 2001 From: rUv Date: Sun, 4 Jan 2026 17:33:35 +0000 Subject: [PATCH] checkpoint: File edit: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 🚀 Generated with [Claude Code](https://claude.com/claude-code) 📊 V3 Development Progress Checkpoint Co-Authored-By: Claude Sonnet 4 --- v3/integration/attention-coordinator.ts | 56 +++++++++++++++++++++++-- 1 file changed, 52 insertions(+), 4 deletions(-) diff --git a/v3/integration/attention-coordinator.ts b/v3/integration/attention-coordinator.ts index b7bfeea28d..fff009d43c 100644 --- a/v3/integration/attention-coordinator.ts +++ b/v3/integration/attention-coordinator.ts @@ -492,6 +492,13 @@ export class AttentionCoordinator extends EventEmitter { // This is a no-op in the simplified implementation } + /** + * Perform attention computation + * + * ADR-001: For sequences longer than 512 tokens, delegates to + * agentic-flow's native Flash Attention for 2.49x-7.47x speedup + * and 50-75% memory reduction. + */ private async performAttention(params: { query: number[] | Float32Array; key: number[] | Float32Array; @@ -500,13 +507,53 @@ export class AttentionCoordinator extends EventEmitter { }): Promise { const { query, key, value, mask } = params; - // Simplified attention computation - // In production, this would delegate to agentic-flow's native implementations - const qArray = Array.isArray(query) ? query : Array.from(query); const kArray = Array.isArray(key) ? key : Array.from(key); const vArray = Array.isArray(value) ? value : Array.from(value); + // Calculate sequence length for delegation decision + const sequenceLength = qArray.length; + + // ADR-001: Delegate to agentic-flow for long sequences + // Flash Attention provides 2.49x-7.47x speedup for sequences > 512 tokens + if ( + this.isDelegationEnabled() && + this.agenticFlowAttention && + sequenceLength > DELEGATION_SEQUENCE_THRESHOLD + ) { + try { + const result = await this.agenticFlowAttention.compute({ + query: qArray, + key: kArray, + value: vArray, + mask, + mechanism: this.config.mechanism, + }); + + this.emit('attention-delegated', { + sequenceLength, + mechanism: result.mechanism, + latencyMs: result.latencyMs, + target: 'agentic-flow', + }); + + return result.output; + } catch (error) { + // Log delegation failure and fall back to local implementation + this.emit('delegation-failed', { + method: 'performAttention', + sequenceLength, + error: (error as Error).message, + fallback: 'local', + }); + // Continue with local implementation below + } + } + + // Local implementation (fallback or for short sequences) + // For short sequences, local JS implementation is sufficient + // and avoids overhead of cross-boundary calls + // Compute attention scores (Q * K^T) let score = this.dotProduct(qArray, kArray); @@ -523,7 +570,8 @@ export class AttentionCoordinator extends EventEmitter { switch (this.config.mechanism) { case 'flash': // Flash attention optimization: fused operations - // In production, this uses optimized CUDA/native kernels + // For short sequences, the JS implementation is used + // Native Flash Attention is used via delegation for longer sequences break; case 'linear': // Linear attention: O(n) instead of O(n^2)