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    fn capabilities(&self) -> crate::ModelCapabilities {
105        crate::ModelCapabilities {
106            batch_capable: true,
107            streaming_capable: true,
108            ..Default::default()
109        }
110    }
111}
112
113impl crate::NamedEntityCapable for DeBERTaV3NER {}
114
115impl crate::BatchCapable for DeBERTaV3NER {
116    fn extract_entities_batch(
117        &self,
118        texts: &[&str],
119        language: Option<&str>,
120    ) -> Result<Vec<Vec<Entity>>> {
121        #[cfg(feature = "onnx")]
122        {
123            self.inner.extract_entities_batch(texts, language)
124        }
125        #[cfg(not(feature = "onnx"))]
126        {
127            Err(crate::Error::FeatureNotAvailable(
128                "DeBERTa-v3 NER requires 'onnx' feature".to_string(),
129            ))
130        }
131    }
132}
133
134impl crate::StreamingCapable for DeBERTaV3NER {
135    fn extract_entities_streaming(&self, chunk: &str, offset: usize) -> Result<Vec<Entity>> {
136        #[cfg(feature = "onnx")]
137        {
138            self.inner.extract_entities_streaming(chunk, offset)
139        }
140        #[cfg(not(feature = "onnx"))]
141        {
142            Err(crate::Error::FeatureNotAvailable(
143                "DeBERTa-v3 NER requires 'onnx' feature".to_string(),
144            ))
145        }
146    }
147}
148
149#[cfg(test)]
150mod tests {
151    use super::*;
152
153    #[test]
154    fn test_deberta_v3_name() {
155        if let Ok(model) = DeBERTaV3NER::new("microsoft/deberta-v3-base") {
156            assert_eq!(model.name(), "deberta_v3");
157        }
158        // If model creation fails (e.g., feature not enabled), test is skipped
159    }
160}