libgrammstein 0.1.0

Hybrid language model (N-gram + Embeddings) for WFST text correction
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
//! GraphCodeBERT embedder using ONNX Runtime.
//!
//! GraphCodeBERT is a pre-trained model from Microsoft that incorporates
//! code structure through data flow graphs. It achieves strong performance
//! on code search, clone detection, and code completion tasks.
//!
//! Model: microsoft/graphcodebert-base
//! - 125M parameters
//! - 768-dim embeddings
//! - 512 max sequence length
//! - Leverages data flow information
//!
//! Reference: https://huggingface.co/microsoft/graphcodebert-base

use std::path::Path;
use std::sync::Arc;

use ndarray::Array2;
use ort::session::{builder::GraphOptimizationLevel, Session};
use ort::value::Tensor;
use parking_lot::Mutex;
use tokenizers::Tokenizer;

use super::{
    CodeEmbedder, CodeEmbeddingCache, CodeEmbeddingCacheConfig, CodeEmbeddingError, CodeLanguage,
    Result,
};

/// Configuration for GraphCodeBERT embedder.
#[derive(Clone, Debug)]
pub struct GraphCodeBertConfig {
    /// Path to ONNX model file.
    pub model_path: String,
    /// Path to tokenizer.json file.
    pub tokenizer_path: String,
    /// Maximum sequence length (default: 512).
    pub max_length: usize,
    /// Number of threads for inference.
    pub num_threads: usize,
    /// Graph optimization level (0-3).
    pub optimization_level: u8,
    /// Cache configuration (None to disable caching).
    pub cache_config: Option<CodeEmbeddingCacheConfig>,
    /// Whether to normalize embeddings.
    pub normalize: bool,
    /// Embedding dimension (768 for graphcodebert-base).
    pub embedding_dim: usize,
    /// Whether to use data flow input (if model supports it).
    pub use_data_flow: bool,
}

impl Default for GraphCodeBertConfig {
    fn default() -> Self {
        Self {
            model_path: String::new(),
            tokenizer_path: String::new(),
            max_length: 512,
            num_threads: 4,
            optimization_level: 3,
            cache_config: Some(CodeEmbeddingCacheConfig::default()),
            normalize: true,
            embedding_dim: 768,
            use_data_flow: false, // Most ONNX exports don't include DFG inputs
        }
    }
}

impl GraphCodeBertConfig {
    /// Create config for graphcodebert-base model.
    pub fn graphcodebert_base(model_dir: impl AsRef<Path>) -> Self {
        let model_dir = model_dir.as_ref();
        Self {
            model_path: model_dir.join("model.onnx").to_string_lossy().to_string(),
            tokenizer_path: model_dir
                .join("tokenizer.json")
                .to_string_lossy()
                .to_string(),
            embedding_dim: 768,
            ..Default::default()
        }
    }

    fn graph_optimization_level(&self) -> GraphOptimizationLevel {
        match self.optimization_level {
            0 => GraphOptimizationLevel::Disable,
            1 => GraphOptimizationLevel::Level1,
            2 => GraphOptimizationLevel::Level2,
            _ => GraphOptimizationLevel::Level3,
        }
    }
}

/// GraphCodeBERT embedder using ONNX Runtime for inference.
///
/// GraphCodeBERT excels at tasks where code structure matters:
/// - Code search with structural understanding
/// - Clone detection (including semantic clones)
/// - Code completion
/// - Variable misuse detection
///
/// The model was pre-trained on data flow graphs from source code,
/// making it particularly effective at understanding variable relationships.
///
/// # Thread Safety
///
/// The embedder is thread-safe via mutex-protected ONNX session.
pub struct GraphCodeBertEmbedder {
    session: Arc<Mutex<Session>>,
    tokenizer: Tokenizer,
    config: GraphCodeBertConfig,
    cache: Option<CodeEmbeddingCache>,
    embedding_dim: usize,
    input_ids_name: String,
    attention_mask_name: String,
    output_name: String,
    /// Whether the model has position_ids input
    has_position_ids: bool,
}

impl GraphCodeBertEmbedder {
    /// Load a GraphCodeBERT model from files.
    pub fn load(config: GraphCodeBertConfig) -> Result<Self> {
        let tokenizer = Tokenizer::from_file(&config.tokenizer_path).map_err(|e| {
            CodeEmbeddingError::ModelLoad(format!(
                "Failed to load tokenizer from {}: {}",
                config.tokenizer_path, e
            ))
        })?;

        let session = Session::builder()
            .map_err(|e| {
                CodeEmbeddingError::Onnx(format!("Failed to create session builder: {}", e))
            })?
            .with_optimization_level(config.graph_optimization_level())
            .map_err(|e| {
                CodeEmbeddingError::Onnx(format!("Failed to set optimization level: {}", e))
            })?
            .with_intra_threads(config.num_threads)
            .map_err(|e| CodeEmbeddingError::Onnx(format!("Failed to set thread count: {}", e)))?
            .commit_from_file(&config.model_path)
            .map_err(|e| {
                CodeEmbeddingError::ModelLoad(format!(
                    "Failed to load ONNX model from {}: {}",
                    config.model_path, e
                ))
            })?;

        // Detect input/output names
        let input_ids_name = session
            .inputs
            .iter()
            .find(|i| i.name.contains("input_ids"))
            .map(|i| i.name.to_string())
            .unwrap_or_else(|| "input_ids".to_string());

        let attention_mask_name = session
            .inputs
            .iter()
            .find(|i| i.name.contains("attention_mask"))
            .map(|i| i.name.to_string())
            .unwrap_or_else(|| "attention_mask".to_string());

        let has_position_ids = session
            .inputs
            .iter()
            .any(|i| i.name.contains("position_ids"));

        let output_name = session
            .outputs
            .first()
            .map(|o| o.name.to_string())
            .unwrap_or_else(|| "last_hidden_state".to_string());

        let embedding_dim = config.embedding_dim;
        let cache = config
            .cache_config
            .as_ref()
            .map(|c| CodeEmbeddingCache::new(c.clone()));

        Ok(Self {
            session: Arc::new(Mutex::new(session)),
            tokenizer,
            config,
            cache,
            embedding_dim,
            input_ids_name,
            attention_mask_name,
            output_name,
            has_position_ids,
        })
    }

    /// Load from a HuggingFace model directory.
    pub fn from_directory(dir: impl AsRef<Path>) -> Result<Self> {
        let config = GraphCodeBertConfig::graphcodebert_base(dir);
        Self::load(config)
    }

    fn tokenize(&self, code: &str) -> Result<(Vec<i64>, Vec<i64>)> {
        let encoding = self
            .tokenizer
            .encode(code, true)
            .map_err(|e| CodeEmbeddingError::Tokenization(e.to_string()))?;

        let max_len = self.config.max_length;
        let ids = encoding.get_ids();
        let attention = encoding.get_attention_mask();

        let (ids, attention) = if ids.len() > max_len {
            (
                ids[..max_len].iter().map(|&x| x as i64).collect(),
                attention[..max_len].iter().map(|&x| x as i64).collect(),
            )
        } else {
            (
                ids.iter().map(|&x| x as i64).collect(),
                attention.iter().map(|&x| x as i64).collect(),
            )
        };

        Ok((ids, attention))
    }

    fn run_inference(&self, input_ids: Vec<i64>, attention_mask: Vec<i64>) -> Result<Vec<f32>> {
        use std::borrow::Cow;

        let seq_len = input_ids.len();

        let input_ids_array =
            Array2::from_shape_vec((1, seq_len), input_ids.clone()).map_err(|e| {
                CodeEmbeddingError::Inference(format!("Failed to create input_ids array: {}", e))
            })?;
        let attention_mask_array =
            Array2::from_shape_vec((1, seq_len), attention_mask).map_err(|e| {
                CodeEmbeddingError::Inference(format!(
                    "Failed to create attention_mask array: {}",
                    e
                ))
            })?;

        let input_ids_tensor = Tensor::from_array(input_ids_array).map_err(|e| {
            CodeEmbeddingError::Onnx(format!("Failed to create input_ids tensor: {}", e))
        })?;
        let attention_mask_tensor = Tensor::from_array(attention_mask_array).map_err(|e| {
            CodeEmbeddingError::Onnx(format!("Failed to create attention_mask tensor: {}", e))
        })?;

        let mut inputs: Vec<(Cow<'_, str>, ort::value::DynValue)> = vec![
            (
                Cow::Owned(self.input_ids_name.clone()),
                input_ids_tensor.into_dyn(),
            ),
            (
                Cow::Owned(self.attention_mask_name.clone()),
                attention_mask_tensor.into_dyn(),
            ),
        ];

        // Add position_ids if model requires it
        if self.has_position_ids {
            let position_ids: Vec<i64> = (0..seq_len as i64).collect();
            let position_ids_array =
                Array2::from_shape_vec((1, seq_len), position_ids).map_err(|e| {
                    CodeEmbeddingError::Inference(format!(
                        "Failed to create position_ids array: {}",
                        e
                    ))
                })?;
            let position_ids_tensor = Tensor::from_array(position_ids_array).map_err(|e| {
                CodeEmbeddingError::Onnx(format!("Failed to create position_ids tensor: {}", e))
            })?;
            inputs.push((
                Cow::Borrowed("position_ids"),
                position_ids_tensor.into_dyn(),
            ));
        }

        let mut session = self.session.lock();
        let outputs = session
            .run(inputs)
            .map_err(|e| CodeEmbeddingError::Inference(format!("Inference failed: {}", e)))?;

        let output = outputs.get(&self.output_name).ok_or_else(|| {
            CodeEmbeddingError::Inference(format!("Output '{}' not found", self.output_name))
        })?;

        let (shape, data) = output.try_extract_tensor::<f32>().map_err(|e| {
            CodeEmbeddingError::Inference(format!("Failed to extract tensor: {}", e))
        })?;

        let shape_dims: Vec<usize> = shape.iter().map(|&d| d as usize).collect();

        // GraphCodeBERT outputs [batch, seq_len, hidden_dim]
        // Use CLS token embedding (first token)
        let embedding: Vec<f32> = match shape_dims.len() {
            2 => data.to_vec(),
            3 => {
                let hidden_dim = shape_dims[2];
                data[..hidden_dim].to_vec()
            }
            _ => {
                return Err(CodeEmbeddingError::Inference(format!(
                    "Unexpected output shape: {:?}",
                    shape_dims
                )));
            }
        };

        Ok(embedding)
    }

    /// Get the configuration.
    pub fn config(&self) -> &GraphCodeBertConfig {
        &self.config
    }

    /// Clear the embedding cache.
    pub fn clear_cache(&self) {
        if let Some(cache) = &self.cache {
            cache.clear();
        }
    }

    /// Get cache statistics.
    pub fn cache_stats(&self) -> Option<usize> {
        self.cache.as_ref().map(|c| c.len())
    }

    /// Check if model has position_ids input.
    pub fn has_position_ids(&self) -> bool {
        self.has_position_ids
    }
}

impl CodeEmbedder for GraphCodeBertEmbedder {
    fn embed_code(&self, code: &str, language: CodeLanguage) -> Result<Vec<f32>> {
        if let Some(cache) = &self.cache {
            if let Some(embedding) = cache.get(code, language) {
                return Ok(embedding.to_vec());
            }
        }

        let (input_ids, attention_mask) = self.tokenize(code)?;
        let mut embedding = self.run_inference(input_ids, attention_mask)?;

        if self.config.normalize {
            super::normalize_embedding(&mut embedding);
        }

        if let Some(cache) = &self.cache {
            cache.insert(code, language, embedding.clone());
        }

        Ok(embedding)
    }

    fn embed_code_batch(
        &self,
        codes: &[&str],
        languages: &[CodeLanguage],
    ) -> Result<Vec<Vec<f32>>> {
        if codes.is_empty() {
            return Ok(vec![]);
        }

        codes
            .iter()
            .zip(
                languages
                    .iter()
                    .chain(std::iter::repeat(&CodeLanguage::Unknown)),
            )
            .map(|(code, lang)| self.embed_code(code, *lang))
            .collect()
    }

    fn embedding_dim(&self) -> usize {
        self.embedding_dim
    }

    fn model_name(&self) -> &str {
        "GraphCodeBERT"
    }

    fn max_sequence_length(&self) -> usize {
        self.config.max_length
    }

    fn supported_languages(&self) -> &[CodeLanguage] {
        // GraphCodeBERT was trained on CodeSearchNet (6 languages)
        &[
            CodeLanguage::Python,
            CodeLanguage::Java,
            CodeLanguage::JavaScript,
            CodeLanguage::Go,
            CodeLanguage::Ruby,
            CodeLanguage::Php,
        ]
    }
}

unsafe impl Send for GraphCodeBertEmbedder {}
unsafe impl Sync for GraphCodeBertEmbedder {}

impl std::fmt::Debug for GraphCodeBertEmbedder {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("GraphCodeBertEmbedder")
            .field("model_path", &self.config.model_path)
            .field("embedding_dim", &self.embedding_dim)
            .field("max_length", &self.config.max_length)
            .field("has_position_ids", &self.has_position_ids)
            .field("cache_size", &self.cache.as_ref().map(|c| c.len()))
            .finish()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_config_default() {
        let config = GraphCodeBertConfig::default();
        assert_eq!(config.max_length, 512);
        assert_eq!(config.embedding_dim, 768);
        assert!(config.normalize);
        assert!(!config.use_data_flow);
    }

    #[test]
    fn test_config_from_directory() {
        let config = GraphCodeBertConfig::graphcodebert_base("/tmp/graphcodebert");
        assert!(config.model_path.contains("model.onnx"));
        assert!(config.tokenizer_path.contains("tokenizer.json"));
        assert_eq!(config.embedding_dim, 768);
    }
}