#[cfg(feature = "ort-backend")]
use crate::{GlinerError, Result};
#[cfg(feature = "ort-backend")]
pub const MAX_BATCH_SIZE: usize = 32;
#[cfg(feature = "ort-backend")]
pub const MAX_ENTITY_LABELS: usize = 256;
#[cfg(feature = "ort-backend")]
pub const MAX_ENTITY_LABEL_CHARS: usize = 128;
#[cfg(feature = "ort-backend")]
pub const MAX_WORDS_PER_SEQUENCE: usize = 4096;
#[cfg(feature = "ort-backend")]
pub const MAX_SPAN_WIDTH: usize = 128;
#[cfg(feature = "ort-backend")]
pub const MAX_SPANS_PER_SEQUENCE: usize = 262_144;
#[derive(Debug, Clone)]
pub struct Parameters {
pub threshold: f32,
pub flat_ner: bool,
pub dup_label: bool,
pub multi_label: bool,
pub max_width: usize,
pub max_length: Option<usize>,
}
impl Default for Parameters {
fn default() -> Self {
Self {
threshold: 0.5,
flat_ner: true,
dup_label: false,
multi_label: false,
max_width: 12,
max_length: Some(512),
}
}
}
impl Parameters {
#[cfg(feature = "ort-backend")]
pub(crate) fn validate(&self) -> Result<()> {
if !(0.0..=1.0).contains(&self.threshold) || !self.threshold.is_finite() {
return Err(GlinerError::InvalidInput(format!(
"threshold must be finite and between 0.0 and 1.0, got {}",
self.threshold
)));
}
if self.max_width == 0 {
return Err(GlinerError::InvalidInput("max_width must be at least 1".to_string()));
}
if self.max_width > MAX_SPAN_WIDTH {
return Err(GlinerError::InvalidInput(format!(
"max_width must be at most {MAX_SPAN_WIDTH}, got {}",
self.max_width
)));
}
if self.max_length == Some(0) {
return Err(GlinerError::InvalidInput(
"max_length must be at least 1 when set".to_string(),
));
}
if self
.max_length
.is_some_and(|max_length| max_length > MAX_WORDS_PER_SEQUENCE)
{
return Err(GlinerError::InvalidInput(format!(
"max_length must be at most {MAX_WORDS_PER_SEQUENCE} when set"
)));
}
Ok(())
}
pub fn with_threshold(mut self, threshold: f32) -> Self {
self.threshold = threshold;
self
}
pub fn with_max_width(mut self, max_width: usize) -> Self {
self.max_width = max_width;
self
}
pub fn with_max_length(mut self, max_length: Option<usize>) -> Self {
self.max_length = max_length;
self
}
pub fn with_flat_ner(mut self, flat_ner: bool) -> Self {
self.flat_ner = flat_ner;
self
}
pub fn with_dup_label(mut self, dup_label: bool) -> Self {
self.dup_label = dup_label;
self
}
pub fn with_multi_label(mut self, multi_label: bool) -> Self {
self.multi_label = multi_label;
self
}
}
#[derive(Debug, Clone)]
pub struct RuntimeConfig {
pub intra_threads: usize,
}
impl Default for RuntimeConfig {
fn default() -> Self {
Self { intra_threads: 4 }
}
}
impl RuntimeConfig {
pub fn with_intra_threads(mut self, intra_threads: usize) -> Self {
self.intra_threads = intra_threads.max(1);
self
}
}