-
Notifications
You must be signed in to change notification settings - Fork 249
/
Copy pathlib.rs
109 lines (93 loc) · 2.58 KB
/
lib.rs
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
#[cfg(feature = "clap")]
use clap::ValueEnum;
use nohash_hasher::IntMap;
use std::fmt;
use thiserror::Error;
#[derive(Debug)]
pub struct Batch {
pub input_ids: Vec<u32>,
pub token_type_ids: Vec<u32>,
pub position_ids: Vec<u32>,
pub cumulative_seq_lengths: Vec<u32>,
pub max_length: u32,
pub pooled_indices: Vec<u32>,
pub raw_indices: Vec<u32>,
}
impl Batch {
pub fn len(&self) -> usize {
self.cumulative_seq_lengths.len() - 1
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
}
pub enum Embedding {
Pooled(Vec<f32>),
All(Vec<Vec<f32>>),
}
pub type Embeddings = IntMap<usize, Embedding>;
pub type Predictions = IntMap<usize, Vec<f32>>;
pub trait Backend {
fn health(&self) -> Result<(), BackendError>;
fn max_batch_size(&self) -> Option<usize> {
None
}
fn is_padded(&self) -> bool;
fn embed(&self, batch: Batch) -> Result<Embeddings, BackendError>;
fn predict(&self, batch: Batch) -> Result<Predictions, BackendError>;
}
#[derive(Debug, PartialEq, Clone)]
pub enum ModelType {
Classifier,
Embedding(Pool),
}
#[derive(Debug, PartialEq, Clone)]
#[cfg_attr(feature = "clap", derive(ValueEnum))]
pub enum Pool {
/// Select the CLS token as embedding
Cls,
/// Apply Mean pooling to the model embeddings
Mean,
/// Apply SPLADE (Sparse Lexical and Expansion) to the model embeddings.
/// This option is only available if the loaded model is a `ForMaskedLM` Transformer
/// model.
Splade,
/// Apply BM42 to the model embeddings.
/// This option is only availale if the loaded model is Qdrant/all_miniLM_L6_v2_with_attentions
BM42,
/// Select the last token as embedding
LastToken,
}
#[derive(Debug, Clone)]
pub struct Bm42Params {
pub invert_vocab: std::collections::HashMap<u32, String>,
pub stopwords: Vec<String>,
pub special_tokens: Vec<String>,
}
#[derive(Debug, Clone)]
pub enum ModelParams {
Bm42(Bm42Params),
None
}
impl fmt::Display for Pool {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Pool::Cls => write!(f, "cls"),
Pool::Mean => write!(f, "mean"),
Pool::Splade => write!(f, "splade"),
Pool::BM42 => write!(f, "bm42"),
Pool::LastToken => write!(f, "last_token"),
}
}
}
#[derive(Debug, Error, Clone)]
pub enum BackendError {
#[error("No backend found")]
NoBackend,
#[error("Could not start backend: {0}")]
Start(String),
#[error("{0}")]
Inference(String),
#[error("Backend is unhealthy")]
Unhealthy,
}