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
//! Core encoder traits for GLiNER/ModernBERT-style bi-encoder extraction.
use crate::;
use RaggedBatch;
// Core Encoder Traits (GLiNER/ModernBERT Alignment)
// =============================================================================
/// Text encoder trait for transformer-based encoders.
///
/// # Motivation
///
/// Modern NER systems require converting raw text into dense vector representations
/// that capture semantic meaning. This trait abstracts the encoding step, allowing
/// different transformer architectures to be used interchangeably.
///
/// # Supported Architectures
///
/// | Architecture | Context | Key Features | Speed |
/// |--------------|---------|--------------|-------|
/// | ModernBERT | 8,192 | RoPE, GeGLU, unpadded inference | 3x faster |
/// | DeBERTaV3 | 512 | Disentangled attention | Baseline |
/// | BERT/RoBERTa | 512 | Classic, widely available | Baseline |
///
/// # Research Alignment (ModernBERT, Dec 2024)
///
/// From ModernBERT paper (arXiv:2412.13663):
/// > "Pareto improvements to BERT... encoder-only models offer great
/// > performance-size tradeoff for retrieval and classification."
///
/// Key innovations:
/// - **Alternating Attention**: Global attention every 3 layers, local (128-token
/// window) elsewhere. Reduces complexity for long sequences.
/// - **Unpadding**: "ModernBERT unpads inputs *before* the token embedding layer
/// and optionally repads model outputs leading to a 10-to-20 percent
/// performance improvement over previous methods."
/// - **RoPE**: Rotary positional embeddings enable extrapolation to longer sequences.
/// - **GeGLU**: Gated activation function improves over GELU.
///
/// # Example
///
/// ```ignore
/// use anno::TextEncoder;
///
/// fn process_document(encoder: &dyn TextEncoder, text: &str) {
/// let output = encoder.encode(text).unwrap();
/// println!("Encoded {} tokens into {} dimensions",
/// output.num_tokens, output.hidden_dim);
///
/// // Token offsets map back to character positions
/// for (i, (start, end)) in output.token_offsets.iter().enumerate() {
/// println!("Token {}: chars {}..{}", i, start, end);
/// }
/// }
/// ```
/// Output from text encoding.
// =============================================================================