1use chrono::{DateTime, Utc};
7use serde::{Deserialize, Serialize};
8use std::collections::HashMap;
9
10#[derive(Debug, Clone, Serialize, Deserialize)]
12pub struct AttentionAnalysisResult {
13 pub timestamp: DateTime<Utc>,
15 pub attention_weights: HashMap<String, AttentionLayerResult>,
17 pub attention_patterns: AttentionPatterns,
19 pub head_specialization: HeadSpecializationAnalysis,
21 pub attention_flow: AttentionFlowAnalysis,
23 pub attention_stats: AttentionStatistics,
25}
26
27#[derive(Debug, Clone, Serialize, Deserialize)]
29pub struct AttentionLayerResult {
30 pub layer_index: usize,
32 pub heads: HashMap<usize, AttentionHeadResult>,
34 pub layer_patterns: LayerAttentionPatterns,
36 pub layer_stats: LayerAttentionStats,
38}
39
40#[derive(Debug, Clone, Serialize, Deserialize)]
42pub struct AttentionHeadResult {
43 pub head_index: usize,
45 pub attention_matrix: Vec<Vec<f64>>,
47 pub token_scores: Vec<TokenAttentionScore>,
49 pub specialization_type: HeadSpecializationType,
51 pub entropy: f64,
53 pub max_attention: f64,
55 pub sparsity: f64,
57}
58
59#[derive(Debug, Clone, Serialize, Deserialize)]
61pub struct TokenAttentionScore {
62 pub token: String,
64 pub position: usize,
66 pub attention_received: f64,
68 pub attention_given: f64,
70 pub self_attention: f64,
72 pub most_attended: Vec<(String, f64)>,
74}
75
76#[derive(Debug, Clone, Serialize, Deserialize, Hash, Eq, PartialEq)]
78pub enum HeadSpecializationType {
79 Local,
81 Global,
83 Syntactic,
85 Semantic,
87 Positional,
89 Mixed,
91 SpecialToken,
93}
94
95#[derive(Debug, Clone, Serialize, Deserialize)]
97pub struct AttentionPatterns {
98 pub diagonal_patterns: Vec<DiagonalPattern>,
100 pub vertical_patterns: Vec<VerticalPattern>,
102 pub block_patterns: Vec<BlockPattern>,
104 pub repetitive_patterns: Vec<RepetitivePattern>,
106}
107
108#[derive(Debug, Clone, Serialize, Deserialize)]
110pub struct DiagonalPattern {
111 pub layer_head: (usize, usize),
113 pub offset: i32,
115 pub strength: f64,
117 pub coverage: f64,
119}
120
121#[derive(Debug, Clone, Serialize, Deserialize)]
123pub struct VerticalPattern {
124 pub layer_head: (usize, usize),
126 pub target_position: usize,
128 pub strength: f64,
130 pub attending_tokens: usize,
132}
133
134#[derive(Debug, Clone, Serialize, Deserialize)]
136pub struct BlockPattern {
137 pub layer_head: (usize, usize),
139 pub start_position: usize,
141 pub end_position: usize,
143 pub internal_strength: f64,
145 pub external_attention: f64,
147}
148
149#[derive(Debug, Clone, Serialize, Deserialize)]
151pub struct RepetitivePattern {
152 pub layer_head: (usize, usize),
154 pub period: usize,
156 pub strength: f64,
158 pub repetitions: usize,
160}
161
162#[derive(Debug, Clone, Serialize, Deserialize)]
164pub struct LayerAttentionPatterns {
165 pub avg_attention_distance: f64,
167 pub concentration: f64,
169 pub inter_head_similarity: f64,
171 pub diversity: f64,
173}
174
175#[derive(Debug, Clone, Serialize, Deserialize)]
177pub struct LayerAttentionStats {
178 pub mean_attention: f64,
180 pub attention_variance: f64,
182 pub entropy: f64,
184 pub significant_connections: usize,
186 pub sparsity_ratio: f64,
188}
189
190#[derive(Debug, Clone, Serialize, Deserialize)]
192pub struct HeadSpecializationAnalysis {
193 pub layer_specialization: HashMap<usize, Vec<HeadSpecializationType>>,
195 pub head_clusters: Vec<HeadCluster>,
197 pub specialization_evolution: SpecializationEvolution,
199 pub redundancy_analysis: HeadRedundancyAnalysis,
201}
202
203#[derive(Debug, Clone, Serialize, Deserialize)]
205pub struct HeadCluster {
206 pub cluster_id: usize,
208 pub heads: Vec<(usize, usize)>,
210 pub centroid_pattern: Vec<f64>,
212 pub cohesion: f64,
214 pub specialization: HeadSpecializationType,
216}
217
218#[derive(Debug, Clone, Serialize, Deserialize)]
220pub struct SpecializationEvolution {
221 pub layer_distribution: HashMap<usize, HashMap<HeadSpecializationType, usize>>,
223 pub transitions: Vec<SpecializationTransition>,
225 pub trend: SpecializationTrend,
227}
228
229#[derive(Debug, Clone, Serialize, Deserialize)]
231pub struct SpecializationTransition {
232 pub from_layer: usize,
234 pub to_layer: usize,
236 pub from_specialization: HeadSpecializationType,
238 pub to_specialization: HeadSpecializationType,
240 pub strength: f64,
242}
243
244#[derive(Debug, Clone, Serialize, Deserialize)]
246pub enum SpecializationTrend {
247 Increasing,
249 Decreasing,
251 Stable,
253 Mixed,
255}
256
257#[derive(Debug, Clone, Serialize, Deserialize)]
259pub struct HeadRedundancyAnalysis {
260 pub redundant_pairs: Vec<RedundantHeadPair>,
262 pub redundancy_score: f64,
264 pub pruning_recommendations: Vec<PruningRecommendation>,
266 pub essential_heads: Vec<(usize, usize)>,
268}
269
270#[derive(Debug, Clone, Serialize, Deserialize)]
272pub struct RedundantHeadPair {
273 pub head1: (usize, usize),
275 pub head2: (usize, usize),
277 pub similarity: f64,
279 pub redundancy_type: RedundancyType,
281}
282
283#[derive(Debug, Clone, Serialize, Deserialize)]
285pub enum RedundancyType {
286 Identical,
288 Correlated,
290 Functional,
292 Partial,
294}
295
296#[derive(Debug, Clone, Serialize, Deserialize)]
298pub struct PruningRecommendation {
299 pub head_to_prune: (usize, usize),
301 pub confidence: f64,
303 pub expected_impact: PruningImpact,
305 pub reason: String,
307}
308
309#[derive(Debug, Clone, Serialize, Deserialize)]
311pub struct PruningImpact {
312 pub performance_drop: f64,
314 pub memory_savings: f64,
316 pub computational_savings: f64,
318 pub risk_level: RiskLevel,
320}
321
322#[derive(Debug, Clone, Serialize, Deserialize)]
324pub enum RiskLevel {
325 Low,
327 Medium,
329 High,
331 Critical,
333}
334
335#[derive(Debug, Clone, Serialize, Deserialize)]
337pub struct AttentionFlowAnalysis {
338 pub flow_paths: Vec<AttentionFlowPath>,
340 pub bottlenecks: Vec<AttentionBottleneck>,
342 pub efficiency_metrics: FlowEfficiencyMetrics,
344 pub layer_flow_stats: HashMap<usize, LayerFlowStats>,
346}
347
348#[derive(Debug, Clone, Serialize, Deserialize)]
350pub struct AttentionFlowPath {
351 pub path_id: String,
353 pub start_position: usize,
355 pub end_position: usize,
357 pub flow_steps: Vec<LayerFlowStep>,
359 pub total_strength: f64,
361 pub path_length: usize,
363}
364
365#[derive(Debug, Clone, Serialize, Deserialize)]
367pub struct LayerFlowStep {
368 pub layer_index: usize,
370 pub head_index: usize,
372 pub strength: f64,
374 pub transformation: FlowTransformation,
376 pub involved_tokens: Vec<usize>,
378}
379
380#[derive(Debug, Clone, Serialize, Deserialize)]
382pub enum FlowTransformation {
383 Direct,
385 Diffusion,
387 Concentration,
389 Routing,
391}
392
393#[derive(Debug, Clone, Serialize, Deserialize)]
395pub struct AttentionBottleneck {
396 pub location: (usize, usize),
398 pub bottleneck_type: BottleneckType,
400 pub severity: f64,
402 pub affected_paths: Vec<String>,
404}
405
406#[derive(Debug, Clone, Serialize, Deserialize)]
408pub enum BottleneckType {
409 Information,
411 Attention,
413 Capacity,
415 Flow,
417}
418
419#[derive(Debug, Clone, Serialize, Deserialize)]
421pub struct FlowEfficiencyMetrics {
422 pub overall_efficiency: f64,
424 pub information_preservation: f64,
426 pub flow_redundancy: f64,
428 pub bottleneck_impact: f64,
430}
431
432#[derive(Debug, Clone, Serialize, Deserialize)]
434pub struct LayerFlowStats {
435 pub incoming_flow: f64,
437 pub outgoing_flow: f64,
439 pub retention_ratio: f64,
441 pub transformation_ratio: f64,
443}
444
445#[derive(Debug, Clone, Serialize, Deserialize)]
447pub struct AttentionStatistics {
448 pub avg_entropy: f64,
450 pub concentration_distribution: HashMap<String, f64>,
452 pub sparsity_distribution: SparsityDistribution,
454 pub insights: Vec<AttentionInsight>,
456}
457
458#[derive(Debug, Clone, Serialize, Deserialize)]
460pub struct SparsityDistribution {
461 pub by_layer: HashMap<usize, f64>,
463 pub by_head: HashMap<(usize, usize), f64>,
465 pub overall_sparsity: f64,
467 pub sparsity_variance: f64,
469}
470
471#[derive(Debug, Clone, Serialize, Deserialize)]
473pub struct AttentionInsight {
474 pub insight_type: InsightType,
476 pub description: String,
478 pub confidence: f64,
480 pub evidence: Vec<String>,
482}
483
484#[derive(Debug, Clone, Serialize, Deserialize)]
486pub enum InsightType {
487 HeadSpecialization,
489 PatternDiscovery,
491 FlowAnalysis,
493 RedundancyDetection,
495 OptimizationOpportunity,
497 BehaviorExplanation,
499}