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
//! # Cross-Attention Variants
//!
//! This module implements various cross-attention mechanisms that can be used
//! across different transformer architectures for enhanced model capabilities.
//!
//! ## Cross-Attention Types
//!
//! Cross-attention enables models to attend to information from different sequences
//! or modalities. This module provides several variants:
//!
//! - **Standard Cross-Attention**: Basic cross-attention mechanism
//! - **Multi-Head Cross-Attention**: Parallel attention heads for different representations
//! - **Sparse Cross-Attention**: Efficient attention with sparsity patterns
//! - **Hierarchical Cross-Attention**: Multi-scale attention across different levels
//! - **Adaptive Cross-Attention**: Dynamic attention based on input characteristics
//! - **Gated Cross-Attention**: Controllable attention with gating mechanisms
//!
//! ## Use Cases
//!
//! Cross-attention is essential for:
//! - **Encoder-Decoder Models**: Decoder attending to encoder states
//! - **Multimodal Models**: Text attending to vision features
//! - **Retrieval-Augmented Models**: Attending to retrieved knowledge
//! - **Memory-Augmented Models**: Attending to external memory
//! - **Multi-Document Models**: Attending across multiple documents
//!
//! ## Architecture Overview
//!
//! Cross-attention computes attention between:
//! - Query (Q): From the target sequence
//! - Key (K) and Value (V): From the source sequence
//!
//! This allows the model to selectively focus on relevant parts of the source
//! when processing each element of the target sequence.
//!
//! ## Example Usage
//!
//! ```rust,no_run
//! use trustformers_models::cross_attention::{CrossAttention, CrossAttentionConfig};
//!
//! let config = CrossAttentionConfig {
//! hidden_size: 512,
//! num_heads: 8,
//! attention_dropout: 0.1,
//! ..Default::default()
//! };
//!
//! let cross_attn = CrossAttention::new(config)?;
//!
//! // Query from target sequence, Key/Value from source sequence
//! let output = cross_attn.forward(query_states, key_states, value_states)?;
//! ```
pub use CrossAttentionConfig;
pub use ;
pub use ;