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
use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::Arc;

use ndarray::{Array, Array1, Array2, Axis, IxDyn};
use ort::environment::Environment;
use ort::tensor::{FromArray, InputTensor};
use ort::{GraphOptimizationLevel, SessionBuilder};

use crate::common::Device;
use crate::common::{apply_device, match_to_inputs};
use crate::error::Result;
use crate::{try_extract_to_f32, ORTSession};

pub enum PoolingStrategy {
    Mean,
    Max,
    First,
}

/// Onnx inference session wrapper for the conditional generation models.
pub struct EmbeddingModel<'a> {
    model_session: ORTSession<'a>,
    token_type_support: bool,
    pub pooling: PoolingStrategy,
}

/// Holds text embedding with model .
pub struct Embedding {
    pub embedding: Array1<f32>,
}

impl<'a> EmbeddingModel<'a> {
    pub fn new_from_memory(
        env: Arc<Environment>,
        model_bytes: &'a [u8],
        pooling: PoolingStrategy,
        device: Device,
        optimization_level: GraphOptimizationLevel,
    ) -> Result<Self> {
        let mut session_builder = SessionBuilder::new(&env)?;

        session_builder = apply_device(session_builder, device)?;
        let session = session_builder
            .with_optimization_level(optimization_level)?
            .with_model_from_memory(model_bytes)?;
        let token_type_support = session.inputs.len() == 3
            && session
                .inputs
                .iter()
                .filter(|i| i.name == "token_type_ids")
                .count()
                > 0;
        Ok(Self {
            model_session: ORTSession::InMemory(session),
            token_type_support,
            pooling,
        })
    }

    pub fn new_from_file(
        env: Arc<Environment>,
        model_path: PathBuf,
        pooling: PoolingStrategy,
        device: Device,
        optimization_level: GraphOptimizationLevel,
    ) -> Result<Self> {
        let mut session_builder = SessionBuilder::new(&env)?;
        session_builder = apply_device(session_builder, device)?;
        let session = session_builder
            .with_optimization_level(optimization_level)?
            .with_model_from_file(model_path)?;
        let token_type_support = session.inputs.len() == 3
            && session
                .inputs
                .iter()
                .filter(|i| i.name == "token_type_ids")
                .count()
                > 0;
        Ok(Self {
            model_session: ORTSession::Owned(session),
            token_type_support,
            pooling,
        })
    }

    pub fn get_token_type_support(&self) -> bool {
        self.token_type_support
    }

    /// Does inference
    ///
    /// Returns a vector of Embedding class that contains the embedding.
    pub fn forward(
        &self,
        input_ids: Array2<u32>,
        attention_mask: Option<Array2<u32>>,
        token_type_ids: Option<Array2<u32>>,
    ) -> Result<Vec<Embedding>> {
        let input_map = self.prepare_input_map(input_ids, attention_mask, token_type_ids)?;
        let model = match &self.model_session {
            ORTSession::Owned(s) => s,
            ORTSession::InMemory(s) => s,
        };
        let input_tensor = match_to_inputs(&model.inputs, input_map)?;
        let output_names = model
            .outputs
            .iter()
            .map(|o| o.name.clone())
            .collect::<Vec<_>>();
        let outputs_tensors = model.run(input_tensor)?;

        let mut output_map = HashMap::new();
        for (name, tensor) in output_names.iter().zip(outputs_tensors) {
            let extracted = try_extract_to_f32(tensor)?;
            let dimensionality = extracted.into_dimensionality::<IxDyn>()?;
            output_map.insert(name.to_string(), dimensionality);
        }
        // Use last_hidden_state as embedding and compute pooling on it
        let embeddings = match self.pooling {
            PoolingStrategy::Mean => {
                let last_hidden_state = output_map.get("last_hidden_state").unwrap();
                let embeddings = last_hidden_state.mean_axis(Axis(1)).unwrap();
                embeddings
            }
            PoolingStrategy::Max => {
                let last_hidden_state = output_map.get_mut("last_hidden_state").unwrap();
                last_hidden_state.accumulate_axis_inplace(Axis(1), |a, b| *b = a.max(*b));
                last_hidden_state.to_owned()
            }
            PoolingStrategy::First => {
                let last_hidden_state = output_map.get("last_hidden_state").unwrap();
                let embeddings = last_hidden_state.slice_axis(Axis(1), (0..1).into());
                embeddings.to_owned()
            }
        };
        let embeddings = embeddings
            .axis_iter(Axis(0))
            .map(|embedding| {
                Ok(Embedding {
                    embedding: embedding.to_owned().into_dimensionality()?,
                })
            })
            .collect::<Result<Vec<_>>>()?;
        Ok(embeddings)
    }

    fn prepare_input_map(
        &self,
        input_ids: Array2<u32>,
        attention_mask: Option<Array2<u32>>,
        token_type_ids: Option<Array2<u32>>,
    ) -> Result<HashMap<String, InputTensor>> {
        let mut input_map = HashMap::<String, InputTensor>::new();
        let attention_mask = if attention_mask.is_none() {
            Array::ones((input_ids.shape()[0], input_ids.shape()[1]))
        } else {
            attention_mask.unwrap()
        };
        if self.token_type_support {
            if let Some(token_types_array) = token_type_ids {
                input_map.insert(
                    "token_type_ids".to_string(),
                    InputTensor::from_array(token_types_array.into_dimensionality()?),
                );
            } else {
                input_map.insert(
                    "token_type_ids".to_string(),
                    InputTensor::from_array(
                        Array2::<u32>::zeros((input_ids.nrows(), input_ids.ncols()))
                            .into_dimensionality()?,
                    ),
                );
            }
        }
        input_map.insert(
            "input_ids".to_string(),
            InputTensor::from_array(input_ids.into_dimensionality()?),
        );
        input_map.insert(
            "attention_mask".to_string(),
            InputTensor::from_array(attention_mask.into_dimensionality()?),
        );
        Ok(input_map)
    }
}

impl Embedding {
    pub fn new(embedding: Array1<f32>) -> Self {
        Embedding { embedding }
    }

    pub fn dot(&self, other: &Embedding) -> f32 {
        self.embedding.dot(&other.embedding)
    }

    /// Compute cosine similarity to other embedding
    pub fn similarity(&self, other: &Self) -> f32 {
        self.embedding.dot(&other.embedding)
            / (self
                .embedding
                .iter()
                .map(|x| x.powf(2f32))
                .sum::<f32>()
                .sqrt()
                * other
                    .embedding
                    .iter()
                    .map(|x| x.powf(2f32))
                    .sum::<f32>()
                    .sqrt())
    }
}

#[cfg(test)]
mod tests {
    use crate::hf_hub::hf_hub_download;

    use super::*;

    #[test]
    fn test_bert() {
        let env = Environment::builder().build().unwrap();
        let bert = EmbeddingModel::new_from_file(
            env.into_arc(),
            hf_hub_download("optimum/all-MiniLM-L6-v2", "model.onnx", None, None).unwrap(),
            PoolingStrategy::Mean,
            Device::CPU,
            GraphOptimizationLevel::Disable,
        )
        .unwrap();
        let input_ids1 =
            Array2::from_shape_vec((1, 8), vec![101, 2000, 1037, 1037, 1037, 1037, 1037, 102])
                .unwrap();
        let input_ids2 =
            Array2::from_shape_vec((1, 8), vec![101, 2000, 1037, 1037, 1037, 1037, 1037, 102])
                .unwrap();
        let attention_mask = Array2::from_shape_vec((1, 8), vec![1, 1, 1, 1, 1, 1, 1, 1]).unwrap();
        let embeddings = bert
            .forward(input_ids1, Some(attention_mask), None)
            .unwrap();
        let attention_mask = Array2::from_shape_vec((1, 8), vec![1, 1, 1, 1, 1, 1, 1, 1]).unwrap();
        let embeddings2 = bert
            .forward(input_ids2, Some(attention_mask), None)
            .unwrap();
        assert_eq!(embeddings.len(), 1);
        assert!(embeddings[0].similarity(&embeddings2[0]) > 0.9);
    }
}