205 lines
7.5 KiB
TypeScript
205 lines
7.5 KiB
TypeScript
/**
|
|
* Attention Analysis Simulation Tests
|
|
*
|
|
* Tests multi-head attention mechanism validation, query enhancement,
|
|
* and learning convergence for RuVector GNN integration.
|
|
*
|
|
* Target Metrics:
|
|
* - 8-head attention (optimal)
|
|
* - Forward pass <5ms (target: 3.8ms)
|
|
* - Query enhancement +12.4%
|
|
* - Convergence: 35 epochs to 95% performance
|
|
* - Transferability: 91% on unseen data
|
|
*/
|
|
|
|
import { describe, it, expect, beforeAll } from 'vitest';
|
|
import { attentionAnalysisScenario } from '../../scenarios/latent-space/attention-analysis';
|
|
import type { SimulationReport } from '../../types';
|
|
|
|
describe('AttentionAnalysis', () => {
|
|
let report: SimulationReport;
|
|
|
|
beforeAll(async () => {
|
|
report = await attentionAnalysisScenario.run(attentionAnalysisScenario.config);
|
|
}, 60000); // 60s timeout
|
|
|
|
describe('Optimal Configuration', () => {
|
|
it('should use 8-head attention configuration', () => {
|
|
const bestConfig = report.summary.bestConfiguration;
|
|
expect(bestConfig.attentionConfig.heads).toBe(8);
|
|
});
|
|
|
|
it('should have 2-3 GNN layers', () => {
|
|
const bestConfig = report.summary.bestConfiguration;
|
|
expect(bestConfig.attentionConfig.layers).toBeGreaterThanOrEqual(2);
|
|
expect(bestConfig.attentionConfig.layers).toBeLessThanOrEqual(3);
|
|
});
|
|
|
|
it('should use GAT attention type', () => {
|
|
const bestConfig = report.summary.bestConfiguration;
|
|
expect(bestConfig.attentionConfig.attentionType).toBe('gat');
|
|
});
|
|
});
|
|
|
|
describe('Performance Metrics', () => {
|
|
it('should achieve forward pass <5ms', () => {
|
|
const bestResult = report.summary.bestConfiguration;
|
|
expect(bestResult.metrics.performance.forwardPassMs).toBeLessThan(5.0);
|
|
});
|
|
|
|
it('should target ~3.8ms forward pass latency', () => {
|
|
const bestResult = report.summary.bestConfiguration;
|
|
expect(bestResult.metrics.performance.forwardPassMs).toBeCloseTo(3.8, 1);
|
|
});
|
|
|
|
it('should have reasonable backward pass time', () => {
|
|
const results = report.detailedResults as any[];
|
|
const avgBackward = results.reduce((sum, r) =>
|
|
sum + r.metrics.performance.backwardPassMs, 0) / results.length;
|
|
expect(avgBackward).toBeLessThan(15); // Should be <15ms
|
|
});
|
|
|
|
it('should track memory usage', () => {
|
|
const bestResult = report.summary.bestConfiguration;
|
|
expect(bestResult.metrics.performance.memoryMB).toBeGreaterThan(0);
|
|
expect(bestResult.metrics.performance.memoryMB).toBeLessThan(1000); // Reasonable limit
|
|
});
|
|
});
|
|
|
|
describe('Query Enhancement', () => {
|
|
it('should improve recall by >10%', () => {
|
|
const bestResult = report.summary.bestConfiguration;
|
|
expect(bestResult.metrics.queryEnhancement.recallImprovement).toBeGreaterThan(0.10);
|
|
});
|
|
|
|
it('should target +12.4% recall improvement', () => {
|
|
const bestResult = report.summary.bestConfiguration;
|
|
expect(bestResult.metrics.queryEnhancement.recallImprovement).toBeCloseTo(0.124, 0.03);
|
|
});
|
|
|
|
it('should improve NDCG scores', () => {
|
|
const bestResult = report.summary.bestConfiguration;
|
|
expect(bestResult.metrics.queryEnhancement.ndcgImprovement).toBeGreaterThan(0);
|
|
});
|
|
|
|
it('should increase cosine similarity', () => {
|
|
const bestResult = report.summary.bestConfiguration;
|
|
expect(bestResult.metrics.queryEnhancement.cosineSimilarityGain).toBeGreaterThan(0.5);
|
|
});
|
|
});
|
|
|
|
describe('Learning Convergence', () => {
|
|
it('should converge within 50 epochs', () => {
|
|
const bestResult = report.summary.bestConfiguration;
|
|
expect(bestResult.metrics.learning.convergenceEpochs).toBeLessThan(50);
|
|
});
|
|
|
|
it('should target ~35 epochs to 95% performance', () => {
|
|
const bestResult = report.summary.bestConfiguration;
|
|
expect(bestResult.metrics.learning.convergenceEpochs).toBeCloseTo(35, 15);
|
|
});
|
|
|
|
it('should have high sample efficiency', () => {
|
|
const bestResult = report.summary.bestConfiguration;
|
|
expect(bestResult.metrics.learning.sampleEfficiency).toBeGreaterThan(0.85);
|
|
});
|
|
|
|
it('should achieve >90% transferability', () => {
|
|
const bestResult = report.summary.bestConfiguration;
|
|
expect(bestResult.metrics.learning.transferability).toBeGreaterThan(0.90);
|
|
});
|
|
|
|
it('should target 91% transferability on unseen data', () => {
|
|
const bestResult = report.summary.bestConfiguration;
|
|
expect(bestResult.metrics.learning.transferability).toBeCloseTo(0.91, 0.05);
|
|
});
|
|
});
|
|
|
|
describe('Attention Weight Distribution', () => {
|
|
it('should calculate entropy correctly', () => {
|
|
const results = report.detailedResults as any[];
|
|
const hasEntropy = results.some(r => r.metrics.weightDistribution.entropy > 0);
|
|
expect(hasEntropy).toBe(true);
|
|
});
|
|
|
|
it('should measure concentration (Gini coefficient)', () => {
|
|
const results = report.detailedResults as any[];
|
|
const ginis = results.map(r => r.metrics.weightDistribution.concentration);
|
|
expect(ginis.every(g => g >= 0 && g <= 1)).toBe(true);
|
|
});
|
|
|
|
it('should track sparsity percentage', () => {
|
|
const results = report.detailedResults as any[];
|
|
const sparsities = results.map(r => r.metrics.weightDistribution.sparsity);
|
|
expect(sparsities.every(s => s >= 0 && s <= 1)).toBe(true);
|
|
});
|
|
});
|
|
|
|
describe('Scalability Analysis', () => {
|
|
it('should test multiple vector counts', () => {
|
|
const vectorCounts = attentionAnalysisScenario.config.vectorCounts;
|
|
expect(vectorCounts.length).toBeGreaterThan(1);
|
|
expect(vectorCounts).toContain(100000);
|
|
});
|
|
|
|
it('should test multiple dimensions', () => {
|
|
const dimensions = attentionAnalysisScenario.config.dimensions;
|
|
expect(dimensions).toContain(384);
|
|
expect(dimensions).toContain(768);
|
|
});
|
|
|
|
it('should scale performance metrics', () => {
|
|
const scalability = report.metrics.scalabilityAnalysis;
|
|
expect(Array.isArray(scalability)).toBe(true);
|
|
expect(scalability.length).toBeGreaterThan(0);
|
|
});
|
|
});
|
|
|
|
describe('Industry Comparison', () => {
|
|
it('should compare with Pinterest PinSage', () => {
|
|
const comparison = report.summary.industryComparison;
|
|
expect(comparison).toHaveProperty('pinterestPinSage');
|
|
});
|
|
|
|
it('should compare with Google Maps', () => {
|
|
const comparison = report.summary.industryComparison;
|
|
expect(comparison).toHaveProperty('googleMaps');
|
|
});
|
|
|
|
it('should provide competitive assessment', () => {
|
|
const comparison = report.summary.industryComparison;
|
|
expect(comparison).toHaveProperty('comparison');
|
|
expect(typeof comparison.comparison).toBe('string');
|
|
});
|
|
});
|
|
|
|
describe('Report Generation', () => {
|
|
it('should generate complete simulation report', () => {
|
|
expect(report).toBeDefined();
|
|
expect(report.scenarioId).toBe('attention-analysis');
|
|
expect(report.timestamp).toBeDefined();
|
|
});
|
|
|
|
it('should include detailed analysis', () => {
|
|
expect(report.analysis).toBeDefined();
|
|
expect(report.analysis.length).toBeGreaterThan(100);
|
|
});
|
|
|
|
it('should provide recommendations', () => {
|
|
expect(report.recommendations).toBeDefined();
|
|
expect(report.recommendations.length).toBeGreaterThan(0);
|
|
expect(report.recommendations[0]).toContain('8');
|
|
});
|
|
|
|
it('should generate artifacts', () => {
|
|
expect(report.artifacts).toBeDefined();
|
|
expect(report.artifacts.attentionHeatmaps).toBeDefined();
|
|
expect(report.artifacts.weightDistributions).toBeDefined();
|
|
});
|
|
|
|
it('should complete within reasonable time', () => {
|
|
expect(report.executionTimeMs).toBeLessThan(60000); // <60s
|
|
});
|
|
});
|
|
});
|