candle-semantic-router 0.4.0

Go bindings for Candle BERT semantic similarity model for LLM routing
Documentation
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
// ModernBERT binding for classification tasks
// Based on ModernBERT implementation in candle-transformers


use std::ffi::{c_char, CStr};
use std::sync::Arc;
use std::sync::Mutex;
use std::path::Path;

use anyhow::{Error as E, Result};
use candle_core::{DType, Device, Tensor};
use candle_nn::VarBuilder;
use candle_transformers::models::modernbert::{ModernBert, Config, ClassifierConfig, ClassifierPooling};
use candle_nn::Module;
use candle_core::{D, IndexOp};
use candle_nn::ops;
use std::collections::HashMap;
use tokenizers::{Tokenizer, PaddingParams, PaddingStrategy};
use serde_json;
use libc;

// ================================================================================================
// FIXED MODERNBERT IMPLEMENTATION
// ================================================================================================
// This implementation fixes the bugs in candle-transformers ModernBERT:
// 1. Proper token ID to embedding conversion
// 2. Correct pooling logic (CLS vs MEAN)
// 3. Proper error handling and validation

/// Fixed ModernBERT classifier that handles embeddings correctly
#[derive(Clone)]
pub struct FixedModernBertClassifier {
    classifier: candle_nn::Linear,
}

impl FixedModernBertClassifier {
    fn load(vb: VarBuilder, config: &Config) -> Result<Self> {
        let num_classes = config
            .classifier_config
            .as_ref()
            .map(|cc| cc.id2label.len())
            .unwrap_or(2);
            
        let classifier = candle_nn::Linear::new(
            vb.get((num_classes, config.hidden_size), "classifier.weight")?,
            Some(vb.get((num_classes,), "classifier.bias")?),
        );
        
        Ok(Self { classifier })
    }
}

impl Module for FixedModernBertClassifier {
    fn forward(&self, xs: &Tensor) -> candle_core::Result<Tensor> {
        let logits = xs.apply(&self.classifier)?;
        // Apply softmax to get probabilities
        ops::softmax(&logits, D::Minus1)
    }
}

/// Fixed ModernBERT head (dense layer + layer norm)
#[derive(Clone)]
pub struct FixedModernBertHead {
    dense: candle_nn::Linear,
    layer_norm: candle_nn::LayerNorm,
}

impl FixedModernBertHead {
    fn load(vb: VarBuilder, config: &Config) -> Result<Self> {
        let dense = candle_nn::Linear::new(
            vb.get((config.hidden_size, config.hidden_size), "dense.weight")?,
            None,
        );
        
        // Load layer norm - it's called "norm" not "layer_norm" in this model!
        // And no bias based on actual model inspection
        let layer_norm = candle_nn::LayerNorm::new(
            vb.get((config.hidden_size,), "norm.weight")?,
            // Create a zero bias tensor since LayerNorm::new requires it but the model doesn't have one
            Tensor::zeros((config.hidden_size,), DType::F32, &vb.device())?,
            1e-12,
        );
        
        Ok(Self { dense, layer_norm })
    }
}

impl Module for FixedModernBertHead {
    fn forward(&self, xs: &Tensor) -> candle_core::Result<Tensor> {
        let xs = xs.apply(&self.dense)?;
        // Apply GELU activation
        let xs = xs.gelu()?;
        xs.apply(&self.layer_norm)
    }
}

/// Fixed ModernBERT sequence classification model that properly handles embeddings
#[derive(Clone)]
pub struct FixedModernBertForSequenceClassification {
    model: ModernBert, // Use the base model (this should work)
    head: Option<FixedModernBertHead>, // Head might not exist in some ModernBERT models
    classifier: FixedModernBertClassifier,
    classifier_pooling: ClassifierPooling,
}

/// Fixed ModernBERT token classifier for token-level predictions
#[derive(Clone)]
pub struct FixedModernBertTokenClassifier {
    classifier: candle_nn::Linear,
}

impl FixedModernBertTokenClassifier {
    fn load(vb: VarBuilder, config: &Config) -> Result<Self> {
        let num_classes = config
            .classifier_config
            .as_ref()
            .map(|cc| cc.id2label.len())
            .unwrap_or(2);
            
        let classifier = candle_nn::Linear::new(
            vb.get((num_classes, config.hidden_size), "classifier.weight")?,
            Some(vb.get((num_classes,), "classifier.bias")?),
        );
        
        Ok(Self { classifier })
    }
}

impl Module for FixedModernBertTokenClassifier {
    fn forward(&self, xs: &Tensor) -> candle_core::Result<Tensor> {
        // For token classification, we don't apply softmax here
        // as we need raw logits for each token position
        xs.apply(&self.classifier)
    }
}

/// Fixed ModernBERT token classification model that properly handles embeddings
#[derive(Clone)]
pub struct FixedModernBertForTokenClassification {
    model: ModernBert, // Use the base model
    head: Option<FixedModernBertHead>, // Head might not exist in some ModernBERT models
    classifier: FixedModernBertTokenClassifier,
}

impl FixedModernBertForTokenClassification {
    pub fn load(vb: VarBuilder, config: &Config) -> Result<Self> {
        let model = ModernBert::load(vb.clone(), config)?;
        
        // Try to load head - it might not exist in all ModernBERT models  
        let head = match vb.get((config.hidden_size, config.hidden_size), "head.dense.weight") {
            Ok(_) => {
                let head_vb = vb.pp("head");
                Some(FixedModernBertHead::load(head_vb, config)?)
            }
            Err(_) => None
        };
        
        let classifier = FixedModernBertTokenClassifier::load(vb.clone(), config)?;
            
        Ok(Self {
            model,
            head,
            classifier,
        })
    }

    pub fn forward(&self, xs: &Tensor, mask: &Tensor) -> Result<Tensor> {
        // Get embeddings from the base model
        let output = self.model.forward(xs, mask).map_err(|e| {
            let error_str = format!("{}", e);
            E::msg(format!("Base model failed: {}", error_str))
        })?;
        
        // Apply head (dense + layer norm) if it exists
        let classifier_input = match &self.head {
            Some(head) => head.forward(&output).map_err(E::msg)?,
            None => output,
        };
        
        // Apply token classifier to get logits for each token position
        let logits = self.classifier.forward(&classifier_input).map_err(E::msg)?;
        
        Ok(logits)
    }
}

impl FixedModernBertForSequenceClassification {
    pub fn load(vb: VarBuilder, config: &Config) -> Result<Self> {
        let model = ModernBert::load(vb.clone(), config)?;
        
        // Try to load head - it might not exist in all ModernBERT models  
        let head = match vb.get((config.hidden_size, config.hidden_size), "head.dense.weight") {
            Ok(_) => {
                let head_vb = vb.pp("head");
                Some(FixedModernBertHead::load(head_vb, config)?)
            }
            Err(_) => None
        };
        
        let classifier = FixedModernBertClassifier::load(vb.clone(), config)?;
        
        let classifier_pooling = config
            .classifier_config
            .as_ref()
            .map(|cc| cc.classifier_pooling)
            .unwrap_or(ClassifierPooling::CLS);
            
        Ok(Self {
            model,
            head,
            classifier,
            classifier_pooling,
        })
    }

    pub fn forward(&self, xs: &Tensor, mask: &Tensor) -> Result<Tensor> {
        // Get embeddings from the base model
        let output = self.model.forward(xs, mask).map_err(|e| {
            let error_str = format!("{}", e);
                E::msg(format!("Base model failed: {}", error_str))
        })?;
        
        // Apply correct pooling logic
        let pooled = match self.classifier_pooling {
            ClassifierPooling::CLS => {
                output.i((.., 0, ..))?
            }
            ClassifierPooling::MEAN => {
                let mask_expanded = mask.unsqueeze(D::Minus1)?.to_dtype(DType::F32)?;
                let masked_output = output.broadcast_mul(&mask_expanded)?;
                let sum_output = masked_output.sum(1)?;
                let mask_sum = mask.sum_keepdim(1)?.to_dtype(DType::F32)?;
                sum_output.broadcast_div(&mask_sum)?
            }
        };
        
        // Apply head (dense + layer norm) if it exists
        let classifier_input = match &self.head {
            Some(head) => head.forward(&pooled).map_err(E::msg)?,
            None => pooled,
        };
        
        // Apply classifier (linear + softmax)
        let probabilities = self.classifier.forward(&classifier_input).map_err(E::msg)?;
        
        Ok(probabilities)
    }
}

// Enum to hold different types of ModernBERT models
pub enum ModernBertModel {
    Sequence(FixedModernBertForSequenceClassification),
    Token(FixedModernBertForTokenClassification),
}



// Structure to hold ModernBERT model and tokenizer for text classification
pub struct ModernBertClassifier {
    model: ModernBertModel,
    tokenizer: Tokenizer,
    device: Device,
    pad_token_id: u32,
    is_token_classification: bool,
}

lazy_static::lazy_static! {
    static ref MODERNBERT_CLASSIFIER: Arc<Mutex<Option<ModernBertClassifier>>> = Arc::new(Mutex::new(None));
    static ref MODERNBERT_PII_CLASSIFIER: Arc<Mutex<Option<ModernBertClassifier>>> = Arc::new(Mutex::new(None));
    static ref MODERNBERT_JAILBREAK_CLASSIFIER: Arc<Mutex<Option<ModernBertClassifier>>> = Arc::new(Mutex::new(None));
}

// Structure to hold classification result
#[repr(C)]
pub struct ModernBertClassificationResult {
    pub class: i32,
    pub confidence: f32,
}

// Structure to hold token classification entity result
#[repr(C)]
pub struct ModernBertTokenEntity {
    pub entity_type: *mut c_char,
    pub start: i32,
    pub end: i32,
    pub text: *mut c_char,
    pub confidence: f32,
}

// Structure to hold token classification result (array of entities)
#[repr(C)]
pub struct ModernBertTokenClassificationResult {
    pub entities: *mut ModernBertTokenEntity,
    pub num_entities: i32,
}

impl ModernBertClassifier {
    pub fn new(model_id: &str, use_cpu: bool) -> Result<Self> {
        Self::new_internal(model_id, use_cpu, false)
    }

    pub fn new_token_classification(model_id: &str, use_cpu: bool) -> Result<Self> {
        Self::new_internal(model_id, use_cpu, true)
    }

    /// Internal implementation using the fixed ModernBERT
    fn new_internal(model_id: &str, use_cpu: bool, is_token_classification: bool) -> Result<Self> {
        let device = if use_cpu {
            Device::Cpu
        } else {
            Device::cuda_if_available(0)?
        };

        // Check if this is a SentenceTransformer ModernBERT model
        let _is_sentence_transformer = Path::new(model_id).join("modules.json").exists();

        let (config_filename, tokenizer_filename, weights_filename, use_pth) = if Path::new(model_id).exists() {
            // Local model path
            let config_path = Path::new(model_id).join("config.json");
            let tokenizer_path = Path::new(model_id).join("tokenizer.json");
            
            // Check for safetensors first, fall back to PyTorch
            let weights_path = if Path::new(model_id).join("model.safetensors").exists() {
                (Path::new(model_id).join("model.safetensors").to_string_lossy().to_string(), false)
            } else if Path::new(model_id).join("pytorch_model.bin").exists() {
                (Path::new(model_id).join("pytorch_model.bin").to_string_lossy().to_string(), true)
            } else {
                return Err(E::msg(format!("No model weights found in {}", model_id)));
            };
            
            (
                config_path.to_string_lossy().to_string(),
                tokenizer_path.to_string_lossy().to_string(),
                weights_path.0,
                weights_path.1
            )
        } else {
            return Err(E::msg(format!("HuggingFace Hub loading for ModernBERT {} not yet implemented", model_id)));
        };

        let config_str = std::fs::read_to_string(&config_filename)?;
        let config: Config = serde_json::from_str(&config_str)?;
        let tokenizer = Tokenizer::from_file(tokenizer_filename).map_err(E::msg)?;

        let vb = if use_pth {
            VarBuilder::from_pth(&weights_filename, DType::F32, &device)?
        } else {
            unsafe { VarBuilder::from_mmaped_safetensors(&[weights_filename], DType::F32, &device)? }
        };


        
        // Check if we have id2label and label2id mappings either in classifier_config or at the top level
        let mut config = config;
        
        // Check if classifier_config exists and has mappings
        let has_classifier_config = config.classifier_config
            .as_ref()
            .map(|cc| !cc.id2label.is_empty())
            .unwrap_or(false);
        
        // If no classifier_config or it's empty, check for top-level id2label/label2id
        if !has_classifier_config {
            // Try to access top-level id2label and label2id fields
            
            let config_str = std::fs::read_to_string(config_filename)?;
            let config_json: serde_json::Value = serde_json::from_str(&config_str)?;
            
            if let (Some(id2label), Some(label2id)) = (
                config_json.get("id2label").and_then(|v| v.as_object()),
                config_json.get("label2id").and_then(|v| v.as_object())
            ) {
                // Convert JSON objects to HashMap<String, String>
                let id2label_map: HashMap<String, String> = id2label
                    .iter()
                    .map(|(k, v)| (k.clone(), v.as_str().unwrap_or("UNKNOWN").to_string()))
                    .collect();
                    
                let label2id_map: HashMap<String, String> = label2id
                    .iter()
                    .map(|(k, v)| (k.clone(), v.as_i64().unwrap_or(0).to_string()))
                    .collect();
                
                // Extract classifier_pooling from top-level config
                let classifier_pooling = config_json.get("classifier_pooling")
                    .and_then(|v| v.as_str())
                    .map(|s| match s {
                        "cls" => ClassifierPooling::CLS,
                        "mean" => ClassifierPooling::MEAN,
                        _ => ClassifierPooling::CLS, // Default to CLS
                    })
                    .unwrap_or(ClassifierPooling::CLS);
                
                let classifier_config = ClassifierConfig {
                    id2label: id2label_map,
                    label2id: label2id_map,
                    classifier_pooling,
                };
                
                config.classifier_config = Some(classifier_config);
            } else {
                return Err(E::msg("No id2label/label2id mappings found in config - required for classification"));
            }
        }
        
        // Load the appropriate ModernBERT model based on task type
        // Try standard naming first, then _orig_mod prefix if that fails
        let model = if is_token_classification {
            match FixedModernBertForTokenClassification::load(vb.clone(), &config) {
                Ok(model) => ModernBertModel::Token(model),
                Err(_) => {
                    // Try with _orig_mod prefix (torch.compile models)
                    ModernBertModel::Token(FixedModernBertForTokenClassification::load(vb.pp("_orig_mod"), &config)?)
                }
            }
        } else {
            match FixedModernBertForSequenceClassification::load(vb.clone(), &config) {
                Ok(model) => ModernBertModel::Sequence(model),
                Err(_) => {
                    // Try with _orig_mod prefix (torch.compile models)
                    ModernBertModel::Sequence(FixedModernBertForSequenceClassification::load(vb.pp("_orig_mod"), &config)?)
                }
            }
        };

        Ok(Self {
            model,
            tokenizer,
            device,
            pad_token_id: config.pad_token_id,
            is_token_classification,
        })
    }

    pub fn classify_text(&self, text: &str) -> Result<(usize, f32)> {
        if self.is_token_classification {
            return Err(E::msg("Use classify_tokens for token classification models"));
        }

        // Set up tokenizer
        let mut tokenizer = self.tokenizer.clone();
        
        // Set up padding - use config's pad_token_id and no truncation
        tokenizer
            .with_padding(Some(PaddingParams {
                strategy: PaddingStrategy::BatchLongest,
                pad_id: self.pad_token_id,
                ..Default::default()
            }))
            .with_truncation(None)
            .map_err(E::msg)?;

        // Tokenize input text
        let tokens = tokenizer.encode_batch(vec![text], true).map_err(E::msg)?;
        
        // Create tensors - convert to u32 for ModernBERT
        let token_ids = tokens
            .iter()
            .map(|tokens| {
                let tokens: Vec<u32> = tokens.get_ids().iter().map(|&x| x as u32).collect();
                Tensor::new(tokens.as_slice(), &self.device)
            })
            .collect::<candle_core::Result<Vec<_>>>()?;

        let attention_mask = tokens
            .iter()
            .map(|tokens| {
                let tokens: Vec<u32> = tokens.get_attention_mask().iter().map(|&x| x as u32).collect();
                Tensor::new(tokens.as_slice(), &self.device)
            })
            .collect::<candle_core::Result<Vec<_>>>()?;

        let input_ids = Tensor::stack(&token_ids, 0)?;
        let attention_mask = Tensor::stack(&attention_mask, 0)?;
        
        // Input validation
        if input_ids.dims().len() != 2 {
            return Err(E::msg(format!("Expected input_ids to have 2 dimensions [batch_size, seq_len], got {:?}", input_ids.dims())));
        }
        if attention_mask.dims().len() != 2 {
            return Err(E::msg(format!("Expected attention_mask to have 2 dimensions [batch_size, seq_len], got {:?}", attention_mask.dims())));
        }
        if input_ids.dims()[0] != attention_mask.dims()[0] || input_ids.dims()[1] != attention_mask.dims()[1] {
            return Err(E::msg(format!("input_ids and attention_mask must have same shape, got {:?} vs {:?}", input_ids.dims(), attention_mask.dims())));
        }

        // Run through ModernBERT model
        let output = match &self.model {
            ModernBertModel::Sequence(model) => model.forward(&input_ids, &attention_mask)?,
            ModernBertModel::Token(_) => return Err(E::msg("Internal error: token model in sequence classification")),
        };
        
        // Remove batch dimension if present
        let probabilities = if output.dims().len() > 1 {
            output.squeeze(0)?
        } else {
            output
        };
        
        // Convert to vector and find the class with highest probability
        let probabilities_vec = probabilities.to_vec1::<f32>()?;
        
        // Get the predicted class with highest probability
        let (predicted_idx, &max_prob) = probabilities_vec.iter()
            .enumerate()
            .max_by(|(_, a), (_, b)| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal))
            .unwrap_or((0, &0.0));
        
        Ok((predicted_idx, max_prob))
    }

    pub fn classify_tokens(&self, text: &str, id2label: &HashMap<String, String>) -> Result<Vec<TokenEntity>> {
        if !self.is_token_classification {
            return Err(E::msg("Use classify_text for sequence classification models"));
        }

        // Set up tokenizer with offset mapping for span reconstruction
        let mut tokenizer = self.tokenizer.clone();
        
        // Set up padding and enable offset mapping
        tokenizer
            .with_padding(Some(PaddingParams {
                strategy: PaddingStrategy::BatchLongest,
                pad_id: self.pad_token_id,
                ..Default::default()
            }))
            .with_truncation(None)
            .map_err(E::msg)?;

        // Tokenize input text with offset mapping
        let tokens = tokenizer.encode_batch(vec![text], true).map_err(E::msg)?;
        let token_encoding = &tokens[0];
        
        // Get offset mapping for span reconstruction
        let offsets = token_encoding.get_offsets();
        
        // Create tensors - convert to u32 for ModernBERT
        let token_ids = {
            let tokens: Vec<u32> = token_encoding.get_ids().iter().map(|&x| x as u32).collect();
            Tensor::new(tokens.as_slice(), &self.device)?.unsqueeze(0)?
        };

        let attention_mask = {
            let tokens: Vec<u32> = token_encoding.get_attention_mask().iter().map(|&x| x as u32).collect();
            Tensor::new(tokens.as_slice(), &self.device)?.unsqueeze(0)?
        };
        
        // Input validation
        if token_ids.dims().len() != 2 {
            return Err(E::msg(format!("Expected token_ids to have 2 dimensions [batch_size, seq_len], got {:?}", token_ids.dims())));
        }
        if attention_mask.dims().len() != 2 {
            return Err(E::msg(format!("Expected attention_mask to have 2 dimensions [batch_size, seq_len], got {:?}", attention_mask.dims())));
        }

        // Run through ModernBERT token classification model
        let logits = match &self.model {
            ModernBertModel::Token(model) => model.forward(&token_ids, &attention_mask)?,
            ModernBertModel::Sequence(_) => return Err(E::msg("Internal error: sequence model in token classification")),
        };
        
        // Apply softmax to get probabilities for each token position
        let probabilities = ops::softmax(&logits, D::Minus1)?;
        
        // Remove batch dimension
        let probabilities = probabilities.squeeze(0)?;
        let logits = logits.squeeze(0)?;
        
        // Get predictions for each token
        let predictions = logits.argmax(D::Minus1)?;
        
        // Convert to vectors for processing
        let predictions_vec = predictions.to_vec1::<u32>()?;
        let probabilities_2d = probabilities.to_vec2::<f32>()?;
        
        // Extract entities from BIO tags
        let mut entities = Vec::new();
        let mut current_entity: Option<TokenEntity> = None;
        
        for (i, (&pred_id, offset)) in predictions_vec.iter().zip(offsets.iter()).enumerate() {
            // Skip special tokens (they have offset (0,0))
            if offset.0 == 0 && offset.1 == 0 && i > 0 {
                continue;
            }
            
            // Get label from prediction ID
            let label = id2label.get(&pred_id.to_string()).unwrap_or(&"O".to_string()).clone();
            let confidence = probabilities_2d[i][pred_id as usize];
            
            if label.starts_with("B-") {
                // Beginning of new entity
                if let Some(entity) = current_entity.take() {
                    entities.push(entity);
                }
                
                let entity_type = label[2..].to_string(); // Remove 'B-' prefix
                current_entity = Some(TokenEntity {
                    entity_type,
                    start: offset.0,
                    end: offset.1,
                    text: text[offset.0..offset.1].to_string(),
                    confidence,
                });
            } else if label.starts_with("I-") {
                // Inside current entity
                if let Some(ref mut entity) = current_entity {
                    let entity_type = &label[2..]; // Remove 'I-' prefix
                    if entity.entity_type == entity_type {
                        // Extend current entity
                        entity.end = offset.1;
                        entity.text = text[entity.start..entity.end].to_string();
                        // Update confidence with average
                        entity.confidence = (entity.confidence + confidence) / 2.0;
                    } else {
                        // Different entity type, finish current and don't start new
                        entities.push(entity.clone());
                        current_entity = None;
                    }
                } // If no current entity, ignore I- tag
            } else {
                // Outside entity (O tag or different entity type)
                if let Some(entity) = current_entity.take() {
                    entities.push(entity);
                }
            }
        }
        
        // Don't forget the last entity
        if let Some(entity) = current_entity {
            entities.push(entity);
        }
        
        Ok(entities)
    }
}

// Structure to hold token entity information
#[derive(Debug, Clone)]
pub struct TokenEntity {
    pub entity_type: String,
    pub start: usize,
    pub end: usize,
    pub text: String,
    pub confidence: f32,
}

// Initialize the ModernBERT classifier model (called from Go)
#[no_mangle]
pub extern "C" fn init_modernbert_classifier(model_id: *const c_char, use_cpu: bool) -> bool {
    let model_id = unsafe {
        match CStr::from_ptr(model_id).to_str() {
            Ok(s) => s,
            Err(_) => return false,
        }
    };

    match ModernBertClassifier::new(model_id, use_cpu) {
        Ok(classifier) => {
            let mut bert_opt = MODERNBERT_CLASSIFIER.lock().unwrap();
            *bert_opt = Some(classifier);
            true
        }
        Err(e) => {
            eprintln!("Failed to initialize ModernBERT classifier: {}", e);
            false
        }
    }
}

// Initialize the ModernBERT PII classifier model (called from Go)
#[no_mangle]
pub extern "C" fn init_modernbert_pii_classifier(model_id: *const c_char, use_cpu: bool) -> bool {
    let model_id = unsafe {
        match CStr::from_ptr(model_id).to_str() {
            Ok(s) => s,
            Err(_) => return false,
        }
    };

    match ModernBertClassifier::new(model_id, use_cpu) {
        Ok(classifier) => {
            let mut bert_opt = MODERNBERT_PII_CLASSIFIER.lock().unwrap();
            *bert_opt = Some(classifier);
            true
        }
        Err(e) => {
            eprintln!("Failed to initialize ModernBERT PII classifier: {}", e);
            false
        }
    }
}

// Initialize the ModernBERT PII token classifier model (called from Go)
#[no_mangle]
pub extern "C" fn init_modernbert_pii_token_classifier(model_id: *const c_char, use_cpu: bool) -> bool {
    let model_id = unsafe {
        match CStr::from_ptr(model_id).to_str() {
            Ok(s) => s,
            Err(_) => return false,
        }
    };

    match ModernBertClassifier::new_token_classification(model_id, use_cpu) {
        Ok(classifier) => {
            let mut bert_opt = MODERNBERT_PII_CLASSIFIER.lock().unwrap();
            *bert_opt = Some(classifier);
            true
        }
        Err(e) => {
            eprintln!("Failed to initialize ModernBERT PII token classifier: {}", e);
            false
        }
    }
}

// Initialize the ModernBERT jailbreak classifier model (called from Go)
#[no_mangle]
pub extern "C" fn init_modernbert_jailbreak_classifier(model_id: *const c_char, use_cpu: bool) -> bool {
    let model_id = unsafe {
        match CStr::from_ptr(model_id).to_str() {
            Ok(s) => s,
            Err(_) => return false,
        }
    };

    match ModernBertClassifier::new(model_id, use_cpu) {
        Ok(classifier) => {
            let mut bert_opt = MODERNBERT_JAILBREAK_CLASSIFIER.lock().unwrap();
            *bert_opt = Some(classifier);
            true
        }
        Err(e) => {
            eprintln!("Failed to initialize ModernBERT jailbreak classifier: {}", e);
            false
        }
    }
}

// Classify text using ModernBERT (called from Go)
#[no_mangle]
pub extern "C" fn classify_modernbert_text(text: *const c_char) -> ModernBertClassificationResult {
    let default_result = ModernBertClassificationResult {
        class: -1,
        confidence: 0.0,
    };

    let text = unsafe {
        match CStr::from_ptr(text).to_str() {
            Ok(s) => s,
            Err(_) => return default_result,
        }
    };

    let bert_opt = MODERNBERT_CLASSIFIER.lock().unwrap();
    match &*bert_opt {
        Some(classifier) => match classifier.classify_text(text) {
            Ok((class_idx, confidence)) => ModernBertClassificationResult {
                class: class_idx as i32,
                confidence,
            },
            Err(e) => {
                eprintln!("Error classifying text with ModernBERT: {}", e);
                default_result
            }
        },
        None => {
            eprintln!("ModernBERT classifier not initialized");
            default_result
        }
    }
}

// Classify text for PII using ModernBERT (called from Go)
#[no_mangle]
pub extern "C" fn classify_modernbert_pii_text(text: *const c_char) -> ModernBertClassificationResult {
    let default_result = ModernBertClassificationResult {
        class: -1,
        confidence: 0.0,
    };

    let text = unsafe {
        match CStr::from_ptr(text).to_str() {
            Ok(s) => s,
            Err(_) => return default_result,
        }
    };

    let bert_opt = MODERNBERT_PII_CLASSIFIER.lock().unwrap();
    match &*bert_opt {
        Some(classifier) => match classifier.classify_text(text) {
            Ok((class_idx, confidence)) => ModernBertClassificationResult {
                class: class_idx as i32,
                confidence,
            },
            Err(e) => {
                eprintln!("Error classifying PII text with ModernBERT: {}", e);
                default_result
            }
        },
        None => {
            eprintln!("ModernBERT PII classifier not initialized");
            default_result
        }
    }
}

// Classify text for jailbreak detection using ModernBERT (called from Go)
#[no_mangle]
pub extern "C" fn classify_modernbert_jailbreak_text(text: *const c_char) -> ModernBertClassificationResult {
    let default_result = ModernBertClassificationResult {
        class: -1,
        confidence: 0.0,
    };

    let text = unsafe {
        match CStr::from_ptr(text).to_str() {
            Ok(s) => s,
            Err(_) => return default_result,
        }
    };

    let bert_opt = MODERNBERT_JAILBREAK_CLASSIFIER.lock().unwrap();
    match &*bert_opt {
        Some(classifier) => match classifier.classify_text(text) {
            Ok((class_idx, confidence)) => ModernBertClassificationResult {
                class: class_idx as i32,
                confidence,
            },
            Err(e) => {
                eprintln!("Error classifying jailbreak text with ModernBERT: {}", e);
                default_result
            }
        },
        None => {
            eprintln!("ModernBERT jailbreak classifier not initialized");
            default_result
        }
    }
}

// Helper function to create id2label mapping from config
fn load_id2label_from_config(config_path: &str) -> Result<HashMap<String, String>> {
    let config_str = std::fs::read_to_string(config_path)?;
    let config_json: serde_json::Value = serde_json::from_str(&config_str)?;
    
    // Try to get id2label from classifier_config first
    if let Some(classifier_config) = config_json.get("classifier_config") {
        if let Some(id2label) = classifier_config.get("id2label").and_then(|v| v.as_object()) {
            let id2label_map: HashMap<String, String> = id2label
                .iter()
                .map(|(k, v)| (k.clone(), v.as_str().unwrap_or("UNKNOWN").to_string()))
                .collect();
            return Ok(id2label_map);
        }
    }
    
    // Fall back to top-level id2label
    if let Some(id2label) = config_json.get("id2label").and_then(|v| v.as_object()) {
        let id2label_map: HashMap<String, String> = id2label
            .iter()
            .map(|(k, v)| (k.clone(), v.as_str().unwrap_or("UNKNOWN").to_string()))
            .collect();
        return Ok(id2label_map);
    }
    
    Err(E::msg("No id2label mapping found in config"))
}

// Classify text for PII token classification using ModernBERT (called from Go)
#[no_mangle]
pub extern "C" fn classify_modernbert_pii_tokens(text: *const c_char, model_config_path: *const c_char) -> ModernBertTokenClassificationResult {
    let default_result = ModernBertTokenClassificationResult {
        entities: std::ptr::null_mut(),
        num_entities: -1,
    };

    let text = unsafe {
        match CStr::from_ptr(text).to_str() {
            Ok(s) => s,
            Err(_) => return default_result,
        }
    };

    let config_path = unsafe {
        match CStr::from_ptr(model_config_path).to_str() {
            Ok(s) => s,
            Err(_) => return default_result,
        }
    };

    // Load id2label mapping from config
    let id2label = match load_id2label_from_config(config_path) {
        Ok(mapping) => mapping,
        Err(e) => {
            eprintln!("Error loading id2label mapping: {}", e);
            return default_result;
        }
    };

    let bert_opt = MODERNBERT_PII_CLASSIFIER.lock().unwrap();
    match &*bert_opt {
        Some(classifier) => match classifier.classify_tokens(text, &id2label) {
            Ok(entities) => {
                // Convert Rust entities to C-compatible format
                let num_entities = entities.len() as i32;
                if num_entities == 0 {
                    return ModernBertTokenClassificationResult {
                        entities: std::ptr::null_mut(),
                        num_entities: 0,
                    };
                }

                // Allocate memory for entities array
                let entities_ptr = unsafe {
                    libc::malloc(num_entities as usize * std::mem::size_of::<ModernBertTokenEntity>()) as *mut ModernBertTokenEntity
                };

                if entities_ptr.is_null() {
                    eprintln!("Failed to allocate memory for entities");
                    return default_result;
                }

                // Fill the entities array
                for (i, entity) in entities.iter().enumerate() {
                    let entity_type_cstr = std::ffi::CString::new(entity.entity_type.clone()).unwrap_or_default();
                    let text_cstr = std::ffi::CString::new(entity.text.clone()).unwrap_or_default();

                    unsafe {
                        (*entities_ptr.add(i)) = ModernBertTokenEntity {
                            entity_type: entity_type_cstr.into_raw(),
                            start: entity.start as i32,
                            end: entity.end as i32,
                            text: text_cstr.into_raw(),
                            confidence: entity.confidence,
                        };
                    }
                }

                ModernBertTokenClassificationResult {
                    entities: entities_ptr,
                    num_entities,
                }
            },
            Err(e) => {
                eprintln!("Error classifying PII tokens with ModernBERT: {}", e);
                default_result
            }
        },
        None => {
            eprintln!("ModernBERT PII classifier not initialized");
            default_result
        }
    }
}

// Free memory allocated for token classification results (called from Go)
#[no_mangle]
pub extern "C" fn free_modernbert_token_result(result: ModernBertTokenClassificationResult) {
    if result.entities.is_null() || result.num_entities <= 0 {
        return;
    }

    unsafe {
        // Free individual strings in each entity
        for i in 0..result.num_entities {
            let entity = &*result.entities.add(i as usize);
            if !entity.entity_type.is_null() {
                let _ = std::ffi::CString::from_raw(entity.entity_type);
            }
            if !entity.text.is_null() {
                let _ = std::ffi::CString::from_raw(entity.text);
            }
        }

        // Free the entities array
        libc::free(result.entities as *mut libc::c_void);
    }
}