Skip to main content

anno/backends/
deberta_v3.rs

1//! DeBERTa-v3 NER Backend
2//!
3//! DeBERTa-v3 (Disentangled Attention BERT) uses improved attention mechanisms
4//! for better NER performance compared to standard BERT.
5//!
6//! # Architecture
7//!
8//! DeBERTa-v3 improves upon BERT with:
9//! - **Disentangled attention**: Separates content and position embeddings
10//! - **Enhanced mask decoder**: Better masked language modeling
11//! - **Improved performance**: often stronger than standard BERT in published evaluations
12//!
13//! # Usage
14//!
15//! ```rust,ignore
16//! use anno::backends::deberta_v3::DeBERTaV3NER;
17//!
18//! let model = DeBERTaV3NER::new("microsoft/deberta-v3-base")?;
19//! let entities = model.extract_entities("Steve Jobs founded Apple.", None)?;
20//! ```
21
22use crate::{Entity, EntityType, Model, Result};
23
24#[cfg(feature = "onnx")]
25use crate::backends::onnx::BertNEROnnx;
26
27/// DeBERTa-v3 NER backend using ONNX Runtime.
28///
29/// Currently wraps BertNEROnnx with DeBERTa-v3 model support.
30/// DeBERTa-v3 models use the same ONNX interface as BERT.
31pub struct DeBERTaV3NER {
32    #[cfg(feature = "onnx")]
33    inner: BertNEROnnx,
34    /// Model name for debugging/logging (e.g., "microsoft/deberta-v3-base")
35    #[allow(dead_code)] // Reserved for future logging/debugging
36    model_name: String,
37}
38
39impl DeBERTaV3NER {
40    /// Create a new DeBERTa-v3 NER model.
41    ///
42    /// # Arguments
43    /// * `model_name` - HuggingFace model ID (e.g., "microsoft/deberta-v3-base")
44    pub fn new(model_name: &str) -> Result<Self> {
45        #[cfg(feature = "onnx")]
46        {
47            // DeBERTa-v3 uses same ONNX interface as BERT
48            let inner = BertNEROnnx::new(model_name)?;
49            Ok(Self {
50                inner,
51                model_name: model_name.to_string(),
52            })
53        }
54        #[cfg(not(feature = "onnx"))]
55        {
56            Err(crate::Error::FeatureNotAvailable(
57                "DeBERTa-v3 NER requires 'onnx' feature".to_string(),
58            ))
59        }
60    }
61}
62
63impl Model for DeBERTaV3NER {
64    fn extract_entities(&self, text: &str, language: Option<&str>) -> Result<Vec<Entity>> {
65        #[cfg(feature = "onnx")]
66        {
67            self.inner.extract_entities(text, language)
68        }
69        #[cfg(not(feature = "onnx"))]
70        {
71            Err(crate::Error::FeatureNotAvailable(
72                "DeBERTa-v3 NER requires 'onnx' feature".to_string(),
73            ))
74        }
75    }
76
77    fn supported_types(&self) -> Vec<EntityType> {
78        vec![
79            EntityType::Person,
80            EntityType::Organization,
81            EntityType::Location,
82        ]
83    }
84
85    fn is_available(&self) -> bool {
86        #[cfg(feature = "onnx")]
87        {
88            self.inner.is_available()
89        }
90        #[cfg(not(feature = "onnx"))]
91        {
92            false
93        }
94    }
95
96    fn name(&self) -> &'static str {
97        "deberta_v3"
98    }
99
100    fn description(&self) -> &'static str {
101        "DeBERTa-v3 NER with disentangled attention (wraps BERT ONNX backend)"
102    }
103}
104
105impl crate::BatchCapable for DeBERTaV3NER {
106    fn extract_entities_batch(
107        &self,
108        texts: &[&str],
109        language: Option<&str>,
110    ) -> Result<Vec<Vec<Entity>>> {
111        #[cfg(feature = "onnx")]
112        {
113            self.inner.extract_entities_batch(texts, language)
114        }
115        #[cfg(not(feature = "onnx"))]
116        {
117            Err(crate::Error::FeatureNotAvailable(
118                "DeBERTa-v3 NER requires 'onnx' feature".to_string(),
119            ))
120        }
121    }
122}
123
124impl crate::StreamingCapable for DeBERTaV3NER {
125    fn extract_entities_streaming(&self, chunk: &str, offset: usize) -> Result<Vec<Entity>> {
126        #[cfg(feature = "onnx")]
127        {
128            self.inner.extract_entities_streaming(chunk, offset)
129        }
130        #[cfg(not(feature = "onnx"))]
131        {
132            Err(crate::Error::FeatureNotAvailable(
133                "DeBERTa-v3 NER requires 'onnx' feature".to_string(),
134            ))
135        }
136    }
137}
138
139#[cfg(test)]
140mod tests {
141    use super::*;
142
143    #[test]
144    fn test_deberta_v3_name() {
145        if let Ok(model) = DeBERTaV3NER::new("microsoft/deberta-v3-base") {
146            assert_eq!(model.name(), "deberta_v3");
147        }
148        // If model creation fails (e.g., feature not enabled), test is skipped
149    }
150}